diffusion_rs_core/pipelines/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
mod flux;
mod sampling;
mod scheduler;

use std::{
    collections::HashMap,
    fmt::Display,
    sync::{Arc, Mutex},
};

use anyhow::Result;
use diffusion_rs_common::core::{DType, Device, Tensor};
use flux::FluxLoader;
use image::{DynamicImage, RgbImage};
use serde::Deserialize;

use diffusion_rs_common::{FileData, FileLoader, ModelSource, NiceProgressBar, TokenSource};
use tracing::info;

use crate::TryIntoDType;

/// Generation parameters.
#[derive(Debug, Clone)]
pub struct DiffusionGenerationParams {
    pub height: usize,
    pub width: usize,
    /// The number of denoising steps. More denoising steps usually lead to a higher quality image at the
    /// expense of slower inference but depends on the model being used.
    pub num_steps: usize,
    /// Higher guidance scale encourages to generate images that are closely linked to the text `prompt`,
    /// usually at the expense of lower image quality.
    pub guidance_scale: f64,
}

#[derive(Debug)]
pub(crate) enum ComponentElem {
    Model {
        safetensors: HashMap<String, FileData>,
        config: FileData,
    },
    Config {
        files: HashMap<String, FileData>,
    },
    Other {
        files: HashMap<String, FileData>,
    },
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ComponentName {
    Scheduler,
    TextEncoder(usize),
    Tokenizer(usize),
    Transformer,
    Vae,
}

impl Display for ComponentName {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Scheduler => write!(f, "scheduler"),
            Self::Transformer => write!(f, "transformer"),
            Self::Vae => write!(f, "vae"),
            Self::TextEncoder(1) => write!(f, "text_encoder"),
            Self::TextEncoder(x) => write!(f, "text_encoder_{x}"),
            Self::Tokenizer(1) => write!(f, "tokenizer"),
            Self::Tokenizer(x) => write!(f, "tokenizer_{x}"),
        }
    }
}

/// Offloading setting during loading.
///
/// - Full: offload the largest components of the model to CPU memory and copy them into VRAM as necessary.
#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
pub enum Offloading {
    Full,
}

pub(crate) trait Loader {
    fn name(&self) -> &'static str;
    fn required_component_names(&self) -> Vec<ComponentName>;
    fn load_from_components(
        &self,
        components: HashMap<ComponentName, ComponentElem>,
        device: &Device,
        dtype: DType,
        silent: bool,
        offloading_type: Option<Offloading>,
        source: Arc<ModelSource>,
    ) -> Result<Arc<Mutex<dyn ModelPipeline>>>;
}

pub trait ModelPipeline: Send + Sync {
    fn forward(
        &mut self,
        prompts: Vec<String>,
        params: DiffusionGenerationParams,
        offloading_type: Option<Offloading>,
    ) -> diffusion_rs_common::core::Result<Tensor>;
}

#[derive(Clone, Debug, Deserialize)]
struct ModelIndex {
    #[serde(rename = "_class_name")]
    name: String,
}

/// Represents the model and provides methods to load and interact with it.
pub struct Pipeline {
    model: Arc<Mutex<dyn ModelPipeline>>,
    offloading_type: Option<Offloading>,
}

impl Pipeline {
    /// Load the model.
    ///
    /// Note:
    /// - `token` and `revision` are only applicable for Hugging Face models.
    pub fn load(
        mut source: ModelSource,
        silent: bool,
        token: TokenSource,
        revision: Option<String>,
        offloading_type: Option<Offloading>,
        dtype: &dyn TryIntoDType,
    ) -> Result<Self> {
        info!("loading from source: {source}.");

        let mut components = HashMap::new();
        let model_loader = {
            let mut loader = FileLoader::from_model_source(&mut source, silent, token, revision)?;
            let files = loader.list_files()?;
            let transformer_files = loader.list_transformer_files()?;

            if !files.contains(&"model_index.json".to_string()) {
                anyhow::bail!("Expected `model_index.json` file present.");
            }

            let ModelIndex { name } = serde_json::from_str(
                &loader
                    .read_file_copied("model_index.json", false)?
                    .read_to_string_owned()?,
            )?;

            let model_loader: Box<dyn Loader> = match name.as_str() {
                "FluxPipeline" => Box::new(FluxLoader),
                other => anyhow::bail!("Unexpected loader type `{other:?}`."),
            };

            info!("model architecture is: {}", model_loader.name());

            for component in NiceProgressBar::<_, 'g'>(
                model_loader.required_component_names().into_iter(),
                "Loading components",
            ) {
                let (files, from_transformer, dir) =
                    if component == ComponentName::Transformer && transformer_files.is_some() {
                        (transformer_files.clone().unwrap(), true, "".to_string())
                    } else {
                        (files.clone(), false, format!("{component}/"))
                    };
                let files_for_component = files
                    .iter()
                    .filter(|file| file.starts_with(&dir))
                    .filter(|file| !file.ends_with('/'))
                    .cloned()
                    .collect::<Vec<_>>();

                // Try to determine the component's type.
                // 1) Model: models contain .safetensors and potentially a config.json
                // 2) Config: general config, a file ends with .json
                // 3) Other: doesn't have safetensors and is not all json
                let component_elem = if files_for_component
                    .iter()
                    .any(|file| file.ends_with(".safetensors"))
                {
                    let mut safetensors = HashMap::new();
                    for file in files_for_component
                        .iter()
                        .filter(|file| file.ends_with(".safetensors"))
                    {
                        safetensors.insert(file.clone(), loader.read_file(file, from_transformer)?);
                    }
                    ComponentElem::Model {
                        safetensors,
                        config: loader.read_file(&format!("{dir}config.json"), from_transformer)?,
                    }
                } else if files_for_component
                    .iter()
                    .all(|file| file.ends_with(".json"))
                {
                    let mut files = HashMap::new();
                    for file in files_for_component
                        .iter()
                        .filter(|file| file.ends_with(".json"))
                    {
                        files.insert(file.clone(), loader.read_file(file, from_transformer)?);
                    }
                    ComponentElem::Config { files }
                } else {
                    let mut files = HashMap::new();
                    for file in files_for_component {
                        files.insert(file.clone(), loader.read_file(&file, from_transformer)?);
                    }
                    ComponentElem::Other { files }
                };
                components.insert(component, component_elem);
            }

            model_loader
        };

        #[cfg(not(feature = "metal"))]
        let device = Device::cuda_if_available(0)?;
        #[cfg(feature = "metal")]
        let device = Device::new_metal(0)?;

        // NOTE: we can set the device to be just the primary even in the offloading case.
        // This will need to be updated!
        let dtype = dtype.try_into_dtype(&[&device], silent)?;

        let model = model_loader.load_from_components(
            components,
            &device,
            dtype,
            silent,
            offloading_type,
            Arc::new(source),
        )?;

        Ok(Self {
            model,
            offloading_type,
        })
    }

    /// Generate images based on prompts and generation parameters.
    ///
    /// If a multiple prompts are specified, they are padded and run together as a batch.
    pub fn forward(
        &self,
        prompts: Vec<String>,
        params: DiffusionGenerationParams,
    ) -> anyhow::Result<Vec<DynamicImage>> {
        let mut model = self.model.lock().expect("Could not lock model!");
        #[cfg(feature = "metal")]
        let img =
            objc::rc::autoreleasepool(|| model.forward(prompts, params, self.offloading_type))?;
        #[cfg(not(feature = "metal"))]
        let img = model.forward(prompts, params, self.offloading_type)?;

        let (_b, c, h, w) = img.dims4()?;
        let mut images = Vec::new();
        for b_img in img.chunk(img.dim(0)?, 0)? {
            let flattened = b_img.squeeze(0)?.permute((1, 2, 0))?.flatten_all()?;
            if c != 3 {
                anyhow::bail!("Expected 3 channels in image output");
            }
            #[allow(clippy::cast_possible_truncation)]
            images.push(DynamicImage::ImageRgb8(
                RgbImage::from_raw(w as u32, h as u32, flattened.to_vec1::<u8>()?).ok_or(
                    diffusion_rs_common::core::Error::Msg(
                        "RgbImage has invalid capacity.".to_string(),
                    ),
                )?,
            ));
        }
        Ok(images)
    }
}