studio-worker 0.4.5

Pull-based image-generation worker for the minis.gg studio.
Documentation
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//! Real image generation via [`candle-transformers`] Stable Diffusion.
//!
//! Compiled in with `--features image-candle`.  Runs SD v1.5 on CPU
//! (or whatever candle backend the user's build supports).  Models are
//! downloaded via `hf-hub` on first use into the standard HF cache —
//! they don't live in `<models_root>` because candle's loaders expect
//! the HF directory layout.
use crate::engine::{Engine, EngineCapabilities};
use crate::types::*;
use anyhow::{anyhow, bail, Context, Result};
use candle_core::{DType, Device, IndexOp, Module, Tensor};
use candle_transformers::models::stable_diffusion;
use parking_lot::Mutex;
use std::collections::BTreeMap;
use std::io::Cursor;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Instant;
use tokenizers::Tokenizer;
use tracing::{debug, info, warn};

/// Tracing target for the candle image engine.  Stable so operators can
/// filter with `RUST_LOG=studio_worker::engine::candle_image=debug`.
const TRACE_TARGET: &str = "studio_worker::engine::candle_image";

const HF_REPO_V1_5: &str = "stable-diffusion-v1-5/stable-diffusion-v1-5";
const HF_TOKENIZER: &str = "openai/clip-vit-base-patch32";
const MODEL_ID: &str = "stable-diffusion-v1-5";

pub struct CandleImageEngine {
    cached: Mutex<Option<CachedModel>>,
}

struct CachedModel {
    id: String,
    pipeline: Arc<Pipeline>,
}

struct Pipeline {
    device: Device,
    dtype: DType,
    sd_config: stable_diffusion::StableDiffusionConfig,
    tokenizer: Tokenizer,
    pad_id: u32,
    text_model: stable_diffusion::clip::ClipTextTransformer,
    unet: stable_diffusion::unet_2d::UNet2DConditionModel,
    vae: stable_diffusion::vae::AutoEncoderKL,
    vae_scale: f64,
}

impl CandleImageEngine {
    pub fn new() -> Self {
        Self {
            cached: Mutex::new(None),
        }
    }

    fn build_pipeline(&self, width: usize, height: usize) -> Result<Pipeline> {
        use hf_hub::api::sync::Api;
        info!(
            target: TRACE_TARGET,
            op = "build_pipeline",
            model = MODEL_ID,
            width,
            height,
            "building SD pipeline (may download weights)"
        );
        let build_started = Instant::now();
        let api = Api::new().context("creating hf-hub api")?;

        let tokenizer_path = download_with_trace(&api, HF_TOKENIZER, "tokenizer.json")
            .context("downloading clip tokenizer")?;
        let tokenizer =
            Tokenizer::from_file(&tokenizer_path).map_err(|e| anyhow!("loading tokenizer: {e}"))?;
        let pad_id = tokenizer
            .get_vocab(true)
            .get("<|endoftext|>")
            .copied()
            .ok_or_else(|| anyhow!("clip tokenizer missing <|endoftext|>"))?;

        let sd_config =
            stable_diffusion::StableDiffusionConfig::v1_5(None, Some(height), Some(width));

        let clip_weights =
            download_with_trace(&api, HF_REPO_V1_5, "text_encoder/model.safetensors")
                .context("downloading clip weights")?;
        let unet_weights = download_with_trace(
            &api,
            HF_REPO_V1_5,
            "unet/diffusion_pytorch_model.safetensors",
        )
        .context("downloading unet weights")?;
        let vae_weights = download_with_trace(
            &api,
            HF_REPO_V1_5,
            "vae/diffusion_pytorch_model.safetensors",
        )
        .context("downloading vae weights")?;

        let device = Device::Cpu;
        let dtype = DType::F32;

        let text_model = stable_diffusion::build_clip_transformer(
            &sd_config.clip,
            clip_weights,
            &device,
            dtype,
        )?;
        let unet = sd_config.build_unet(unet_weights, &device, 4, false, dtype)?;
        let vae = sd_config.build_vae(vae_weights, &device, dtype)?;
        info!(
            target: TRACE_TARGET,
            op = "build_pipeline",
            model = MODEL_ID,
            elapsed_ms = build_started.elapsed().as_millis() as u64,
            "SD pipeline ready"
        );

        Ok(Pipeline {
            device,
            dtype,
            sd_config,
            tokenizer,
            pad_id,
            text_model,
            unet,
            vae,
            vae_scale: 0.18215,
        })
    }

    fn load_or_get(&self, model: &str, width: usize, height: usize) -> Result<Arc<Pipeline>> {
        let mut guard = self.cached.lock();
        if let Some(c) = &*guard {
            if c.id == model {
                debug!(
                    target: TRACE_TARGET,
                    op = "load",
                    model,
                    cache = "hit",
                    "reusing cached pipeline"
                );
                return Ok(c.pipeline.clone());
            }
        }
        let pipeline = Arc::new(self.build_pipeline(width, height).inspect_err(|e| {
            warn!(
                target: TRACE_TARGET,
                op = "load",
                model,
                error = %e,
                "failed to build pipeline"
            );
        })?);
        *guard = Some(CachedModel {
            id: model.to_string(),
            pipeline: pipeline.clone(),
        });
        Ok(pipeline)
    }
}

fn download_with_trace(
    api: &hf_hub::api::sync::Api,
    repo: &str,
    file: &str,
) -> Result<PathBuf, hf_hub::api::sync::ApiError> {
    debug!(
        target: TRACE_TARGET,
        op = "download",
        repo,
        file,
        "requesting weight file"
    );
    let started = Instant::now();
    let result = api.model(repo.to_string()).get(file);
    let elapsed_ms = started.elapsed().as_millis() as u64;
    match &result {
        Ok(path) => debug!(
            target: TRACE_TARGET,
            op = "download",
            repo,
            file,
            path = %path.display(),
            elapsed_ms,
            "weight file ready"
        ),
        Err(e) => warn!(
            target: TRACE_TARGET,
            op = "download",
            repo,
            file,
            elapsed_ms,
            error = %e,
            "weight download failed"
        ),
    }
    result
}

impl Default for CandleImageEngine {
    fn default() -> Self {
        Self::new()
    }
}

fn encode_text(pipeline: &Pipeline, prompt: &str) -> Result<Tensor> {
    let max_pos = pipeline.sd_config.clip.max_position_embeddings;
    let mut ids = pipeline
        .tokenizer
        .encode(prompt, true)
        .map_err(|e| anyhow!("tokenize: {e}"))?
        .get_ids()
        .to_vec();
    if ids.len() > max_pos {
        ids.truncate(max_pos);
    }
    while ids.len() < max_pos {
        ids.push(pipeline.pad_id);
    }
    let tokens = Tensor::new(ids.as_slice(), &pipeline.device)?.unsqueeze(0)?;
    let cond = pipeline.text_model.forward(&tokens)?;

    // Unconditional embeddings (empty prompt) for classifier-free guidance.
    let mut uncond_ids = pipeline
        .tokenizer
        .encode("", true)
        .map_err(|e| anyhow!("tokenize uncond: {e}"))?
        .get_ids()
        .to_vec();
    while uncond_ids.len() < max_pos {
        uncond_ids.push(pipeline.pad_id);
    }
    let uncond_tokens = Tensor::new(uncond_ids.as_slice(), &pipeline.device)?.unsqueeze(0)?;
    let uncond = pipeline.text_model.forward(&uncond_tokens)?;

    Tensor::cat(&[uncond, cond], 0)?
        .to_dtype(pipeline.dtype)
        .map_err(Into::into)
}

fn run_diffusion(
    pipeline: &Pipeline,
    text_embeddings: &Tensor,
    n_steps: usize,
    guidance_scale: f64,
    seed: u64,
) -> Result<Tensor> {
    pipeline.device.set_seed(seed)?;
    let mut scheduler = pipeline.sd_config.build_scheduler(n_steps)?;
    let mut latents = Tensor::randn(
        0f32,
        1f32,
        (
            1,
            4,
            pipeline.sd_config.height / 8,
            pipeline.sd_config.width / 8,
        ),
        &pipeline.device,
    )?;
    latents = (latents * scheduler.init_noise_sigma())?;
    latents = latents.to_dtype(pipeline.dtype)?;
    let timesteps = scheduler.timesteps().to_vec();

    for &timestep in &timesteps {
        let latent_model_input = Tensor::cat(&[&latents, &latents], 0)?;
        let latent_model_input = scheduler.scale_model_input(latent_model_input, timestep)?;
        let noise_pred =
            pipeline
                .unet
                .forward(&latent_model_input, timestep as f64, text_embeddings)?;
        let noise_pred = noise_pred.chunk(2, 0)?;
        let noise_pred = (&noise_pred[0] + ((&noise_pred[1] - &noise_pred[0])? * guidance_scale)?)?;
        latents = scheduler.step(&noise_pred, timestep, &latents)?;
    }
    Ok(latents)
}

fn decode_to_png(pipeline: &Pipeline, latents: &Tensor) -> Result<Vec<u8>> {
    let images = pipeline.vae.decode(&(latents / pipeline.vae_scale)?)?;
    let images = ((images / 2.0)? + 0.5)?.to_device(&Device::Cpu)?;
    let images = (images.clamp(0f32, 1.0)? * 255.0)?.to_dtype(DType::U8)?;
    let image = images.i(0)?; // [3, H, W]
    let (channels, height, width) = image.dims3()?;
    if channels != 3 {
        bail!("expected 3-channel image, got {channels}");
    }
    let raw = image.permute((1, 2, 0))?.flatten_all()?.to_vec1::<u8>()?;
    let buffer =
        image::ImageBuffer::<image::Rgb<u8>, _>::from_raw(width as u32, height as u32, raw)
            .ok_or_else(|| anyhow!("RGB buffer wrong size"))?;
    let dyn_img = image::DynamicImage::ImageRgb8(buffer);
    let mut out = Cursor::new(Vec::<u8>::new());
    dyn_img.write_to(&mut out, image::ImageFormat::Png)?;
    Ok(out.into_inner())
}

impl Engine for CandleImageEngine {
    fn name(&self) -> &'static str {
        "image-candle"
    }

    fn capabilities(&self) -> EngineCapabilities {
        let mut map: BTreeMap<TaskKind, Vec<String>> = BTreeMap::new();
        map.insert(TaskKind::Image, vec![MODEL_ID.to_string()]);
        EngineCapabilities {
            supported_models_per_kind: map,
        }
    }

    fn dispatch(&self, model: &str, task: Task) -> Result<TaskResult> {
        let kind = task.kind();
        let started = Instant::now();
        if model != MODEL_ID {
            warn!(
                target: TRACE_TARGET,
                op = "dispatch",
                model,
                expected = MODEL_ID,
                "unsupported model id"
            );
            bail!("candle-image engine only serves `{MODEL_ID}`, got `{model}`");
        }
        let params = match task {
            Task::Image(p) => p,
            other => {
                warn!(
                    target: TRACE_TARGET,
                    op = "dispatch",
                    kind = kind.as_str(),
                    model,
                    "unsupported task kind"
                );
                bail!(
                    "candle-image engine cannot serve {} tasks",
                    other.kind().as_str()
                );
            }
        };
        let width = (params.width as usize).max(64);
        let height = (params.height as usize).max(64);
        // Dimensions must be multiples of 64 for SD's UNet.
        let width = width - (width % 64);
        let height = height - (height % 64);
        let pipeline = self.load_or_get(model, width, height)?;
        let n_steps = params.steps.max(1) as usize;
        let seed = params.seed.unwrap_or(0);
        debug!(
            target: TRACE_TARGET,
            op = "dispatch",
            kind = kind.as_str(),
            model,
            width,
            height,
            steps = n_steps,
            seed,
            "starting diffusion"
        );
        let text_embeddings = encode_text(&pipeline, &params.prompt)?;
        let latents =
            run_diffusion(&pipeline, &text_embeddings, n_steps, 7.5, seed).inspect_err(|e| {
                warn!(
                    target: TRACE_TARGET,
                    op = "dispatch",
                    kind = kind.as_str(),
                    model,
                    elapsed_ms = started.elapsed().as_millis() as u64,
                    error = %e,
                    "diffusion failed"
                );
            })?;
        let png = decode_to_png(&pipeline, &latents)?;
        info!(
            target: TRACE_TARGET,
            op = "dispatch",
            kind = kind.as_str(),
            model,
            bytes = png.len(),
            elapsed_ms = started.elapsed().as_millis() as u64,
            "image generated"
        );
        Ok(TaskResult::Image {
            bytes: png,
            ext: "png".into(),
        })
    }
}

/// Where hf-hub will cache downloaded model weights.  Reported for
/// diagnostics only — the engine doesn't manage this directory itself.
pub fn hf_cache_path() -> Option<PathBuf> {
    let api = hf_hub::api::sync::Api::new().ok()?;
    let path = api
        .model(HF_REPO_V1_5.to_string())
        .get("model_index.json")
        .ok()?;
    path.parent().map(|x| x.to_path_buf())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn engine_advertises_image_kind() {
        let engine = CandleImageEngine::new();
        let caps = engine.capabilities();
        assert_eq!(
            caps.supported_models_per_kind[&TaskKind::Image],
            vec![MODEL_ID.to_string()]
        );
        assert_eq!(engine.name(), "image-candle");
    }

    #[test]
    fn dispatch_rejects_wrong_model_id() {
        let engine = CandleImageEngine::new();
        let task = Task::Image(ImageParams {
            prompt: "x".into(),
            width: 64,
            height: 64,
            steps: 1,
            seed: None,
            ext: "png".into(),
            ..Default::default()
        });
        let err = engine.dispatch("not-the-model", task).unwrap_err();
        assert!(err.to_string().contains("only serves"));
    }

    #[test]
    fn dispatch_rejects_non_image_tasks() {
        let engine = CandleImageEngine::new();
        let task = Task::Llm(LlmParams {
            messages: vec![],
            max_tokens: 1,
            temperature: 0.0,
            ..Default::default()
        });
        let err = engine.dispatch(MODEL_ID, task).unwrap_err();
        assert!(err.to_string().contains("cannot serve llm"));
    }
}