mold-ai-inference 0.13.1

Candle-based inference engine for mold — FLUX, SDXL, SD3.5, Z-Image diffusion models
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
use anyhow::{bail, Result};
use mold_core::{Config, ModelPaths, Scheduler};
use std::sync::{Arc, Mutex};

use crate::engine::{InferenceEngine, LoadStrategy};
use crate::flux::FluxEngine;
use crate::flux2::Flux2Engine;
use crate::ltx2::Ltx2Engine;
use crate::ltx_video::LtxVideoEngine;
use crate::qwen_image::QwenImageEngine;
use crate::sd15::SD15Engine;
use crate::sd3::SD3Engine;
use crate::sdxl::SDXLEngine;
use crate::shared_pool::SharedPool;
use crate::wuerstchen::WuerstchenEngine;
use crate::zimage::ZImageEngine;

/// Determine the model family from config or manifest, defaulting to "flux".
fn resolve_family(model_name: &str, config: &Config) -> String {
    let model_cfg = config.resolved_model_config(model_name);
    if let Some(family) = model_cfg.family {
        return family;
    }
    // Default to flux for backward compatibility
    "flux".to_string()
}

/// Detect a Civitai single-file SD1.5 / SDXL checkpoint from a `ModelPaths`.
///
/// The Civitai single-file convention is one `.safetensors` containing
/// UNet + VAE + CLIP-L (+ CLIP-G for SDXL); diffusers layouts ship each
/// component as a separate file under its own subdir. The two are
/// distinguishable purely by `paths.transformer == paths.vae` plus the
/// `.safetensors` extension — a duck-type that avoids a new
/// `single_file: Option<PathBuf>` field threaded through every caller.
///
/// Used by the SD1.5 + SDXL match arms to dispatch to `from_single_file`
/// instead of the diffusers-layout `new` constructor.
fn is_single_file(paths: &ModelPaths) -> bool {
    paths.transformer == paths.vae
        && paths
            .transformer
            .extension()
            .is_some_and(|ext| ext == "safetensors")
}

/// Civitai / ComfyUI Flux.2 fine-tunes ship the transformer as one
/// BFL-native single-file `.safetensors` (every key prefixed
/// `model.diffusion_model.`) and rely on a separate `flux2-vae`
/// companion for the VAE — so the SD-style `paths.transformer ==
/// paths.vae` heuristic doesn't apply. Header-peek the transformer
/// instead. Returns `true` for `BflNative`, `BflNativeRoot`, and
/// `Nvfp4` — all three route through `SingleFileBackend` (NVFP4
/// exposes packed FP4, FP8 block scales, and tensor scales as streaming
/// subkeys; the BFL variants pass tensors through directly). Returns `false` for sharded
/// transformers and for any non-`safetensors` path so HF diffusers
/// layouts and GGUF quantised weights stay on the existing load paths.
fn is_flux2_bfl_native_single_file(paths: &ModelPaths) -> bool {
    if !paths.transformer_shards.is_empty() {
        return false;
    }
    let path = &paths.transformer;
    let is_safetensors = path
        .extension()
        .and_then(|e| e.to_str())
        .is_some_and(|e| e.eq_ignore_ascii_case("safetensors"));
    if !is_safetensors {
        return false;
    }
    matches!(
        crate::flux2::detect_format(path),
        Ok(crate::flux2::Flux2SingleFileFormat::BflNative)
            | Ok(crate::flux2::Flux2SingleFileFormat::BflNativeRoot)
            | Ok(crate::flux2::Flux2SingleFileFormat::Nvfp4)
    )
}

/// Create an inference engine for the given model, auto-detecting the family.
///
/// Returns the appropriate engine (FluxEngine, SD15Engine, SDXLEngine, or ZImageEngine)
/// based on the model's family from config or manifest.
///
/// `load_strategy` controls whether components are loaded eagerly (server) or
/// sequentially (CLI one-shot) to reduce peak memory.
pub fn create_engine(
    model_name: String,
    paths: ModelPaths,
    config: &Config,
    load_strategy: LoadStrategy,
    gpu_ordinal: usize,
    offload: bool,
) -> Result<Box<dyn InferenceEngine>> {
    create_engine_with_pool(
        model_name,
        paths,
        config,
        load_strategy,
        gpu_ordinal,
        offload,
        None,
    )
}

/// Create an inference engine with an optional shared tokenizer pool.
///
/// When `shared_pool` is provided, engines can cache and reuse tokenizers across
/// model switches (e.g. all FLUX variants share the same T5/CLIP tokenizers).
pub fn create_engine_with_pool(
    model_name: String,
    paths: ModelPaths,
    config: &Config,
    load_strategy: LoadStrategy,
    gpu_ordinal: usize,
    offload: bool,
    shared_pool: Option<Arc<Mutex<SharedPool>>>,
) -> Result<Box<dyn InferenceEngine>> {
    let family = resolve_family(&model_name, config);
    let model_cfg = config.resolved_model_config(&model_name);

    match family.as_str() {
        "flux" => {
            let is_schnell = model_cfg.is_schnell;
            let t5_variant = std::env::var("MOLD_T5_VARIANT")
                .ok()
                .or_else(|| config.t5_variant.clone());
            Ok(Box::new(FluxEngine::new(
                model_name,
                paths,
                is_schnell,
                t5_variant,
                load_strategy,
                gpu_ordinal,
                offload,
                shared_pool,
            )))
        }
        "sd15" | "sd1.5" | "stable-diffusion-1.5" => {
            let scheduler = model_cfg.scheduler.unwrap_or(Scheduler::Ddim);
            if is_single_file(&paths) {
                // Companion-pulled CLIP-L tokenizer (phase 2.7). Civitai
                // single-file checkpoints never bundle the tokenizer.
                let clip_tokenizer = paths
                    .clip_tokenizer
                    .clone()
                    .ok_or_else(|| anyhow::anyhow!(
                        "single-file SD1.5 dispatch requires a companion-pulled clip_tokenizer (phase 2.7)"
                    ))?;
                let single_file = paths.transformer.clone();
                Ok(Box::new(SD15Engine::from_single_file(
                    model_name,
                    single_file,
                    clip_tokenizer,
                    scheduler,
                    load_strategy,
                    gpu_ordinal,
                    shared_pool,
                )?))
            } else {
                Ok(Box::new(SD15Engine::new(
                    model_name,
                    paths,
                    scheduler,
                    load_strategy,
                    gpu_ordinal,
                    shared_pool,
                )))
            }
        }
        "sdxl" => {
            let is_turbo = model_cfg
                .is_turbo
                .unwrap_or_else(|| model_name.contains("turbo"));
            let scheduler = model_cfg.scheduler.unwrap_or(if is_turbo {
                Scheduler::EulerAncestral
            } else {
                Scheduler::Ddim
            });
            if is_single_file(&paths) {
                // Companion-pulled CLIP-L + CLIP-G tokenizers (phase 2.7).
                let clip_l_tokenizer = paths.clip_tokenizer.clone().ok_or_else(|| {
                    anyhow::anyhow!(
                        "single-file SDXL dispatch requires a companion-pulled clip_tokenizer (CLIP-L, phase 2.7)"
                    )
                })?;
                let clip_g_tokenizer = paths.clip_tokenizer_2.clone().ok_or_else(|| {
                    anyhow::anyhow!(
                        "single-file SDXL dispatch requires a companion-pulled clip_tokenizer_2 (CLIP-G, phase 2.7)"
                    )
                })?;
                let single_file = paths.transformer.clone();
                Ok(Box::new(SDXLEngine::from_single_file(
                    model_name,
                    single_file,
                    clip_l_tokenizer,
                    clip_g_tokenizer,
                    scheduler,
                    is_turbo,
                    load_strategy,
                    gpu_ordinal,
                    shared_pool,
                )?))
            } else {
                Ok(Box::new(SDXLEngine::new(
                    model_name,
                    paths,
                    scheduler,
                    is_turbo,
                    load_strategy,
                    gpu_ordinal,
                    shared_pool,
                )))
            }
        }
        "sd3" | "sd3.5" | "stable-diffusion-3" | "stable-diffusion-3.5" => {
            let is_turbo = model_cfg
                .is_turbo
                .unwrap_or_else(|| model_name.contains("turbo"));
            let is_medium = model_name.contains("medium");
            let t5_variant = std::env::var("MOLD_T5_VARIANT")
                .ok()
                .or_else(|| config.t5_variant.clone());
            Ok(Box::new(SD3Engine::new(
                model_name,
                paths,
                is_turbo,
                is_medium,
                t5_variant,
                load_strategy,
                gpu_ordinal,
                offload,
                shared_pool,
            )))
        }
        "z-image" => {
            let qwen3_variant = std::env::var("MOLD_QWEN3_VARIANT")
                .ok()
                .or_else(|| config.qwen3_variant.clone());
            Ok(Box::new(ZImageEngine::new(
                model_name,
                paths,
                qwen3_variant,
                load_strategy,
                gpu_ordinal,
                offload,
                shared_pool,
            )))
        }
        "flux2" | "flux.2" | "flux2-klein" => {
            let qwen3_variant = std::env::var("MOLD_QWEN3_VARIANT")
                .ok()
                .or_else(|| config.qwen3_variant.clone());
            if is_flux2_bfl_native_single_file(&paths) {
                // Civitai / ComfyUI fine-tunes ship as one BFL-native
                // safetensors. The VAE + Qwen3 text encoder come from the
                // `flux2-vae` and `flux2-te*` companions wired by the
                // catalog bridge — copy them through unchanged.
                Ok(Box::new(Flux2Engine::from_single_file(
                    model_name,
                    paths.transformer.clone(),
                    paths.vae.clone(),
                    paths.text_encoder_files.clone(),
                    paths.text_tokenizer.clone(),
                    qwen3_variant,
                    load_strategy,
                    gpu_ordinal,
                    offload,
                    shared_pool,
                )?))
            } else {
                Ok(Box::new(Flux2Engine::new(
                    model_name,
                    paths,
                    qwen3_variant,
                    load_strategy,
                    gpu_ordinal,
                    offload,
                    shared_pool,
                )))
            }
        }
        "qwen-image" | "qwen_image" => Ok(Box::new(QwenImageEngine::new(
            model_name,
            paths,
            load_strategy,
            gpu_ordinal,
            offload,
            shared_pool,
        ))),
        "qwen-image-edit" => Ok(Box::new(QwenImageEngine::new(
            model_name,
            paths,
            load_strategy,
            gpu_ordinal,
            offload,
            shared_pool,
        ))),
        "ltx-video" | "ltx_video" => {
            let t5_variant = std::env::var("MOLD_T5_VARIANT")
                .ok()
                .or_else(|| config.t5_variant.clone());
            if is_single_file(&paths) {
                // Civitai single-file dispatch (phase 5). `paths.vae` is
                // either the same checkpoint (combined transformer+VAE) or
                // the ltx-video-vae companion set by `populate_companion_paths`.
                let vae_path = if paths.vae != paths.transformer {
                    Some(paths.vae.clone())
                } else {
                    None
                };
                Ok(Box::new(LtxVideoEngine::from_single_file(
                    model_name,
                    paths.transformer.clone(),
                    vae_path,
                    paths.t5_encoder.clone(),
                    paths.t5_tokenizer.clone(),
                    t5_variant,
                    load_strategy,
                    gpu_ordinal,
                    shared_pool,
                )?))
            } else {
                Ok(Box::new(LtxVideoEngine::new(
                    model_name,
                    paths,
                    t5_variant,
                    load_strategy,
                    gpu_ordinal,
                    shared_pool,
                )))
            }
        }
        "ltx2" | "ltx-2" => {
            if is_single_file(&paths) {
                // Civitai single-file dispatch (phase 5). The combined
                // LTX-2 checkpoint includes the VAE under `vae.*`. Pass the
                // full `paths` so `text_encoder_files` (Gemma TE companion,
                // resolved by the catalog bridge) survives the
                // ModelPaths rebuild — the runtime's `gemma_root` reads it.
                let checkpoint = paths.transformer.clone();
                Ok(Box::new(Ltx2Engine::from_single_file(
                    model_name,
                    checkpoint,
                    paths,
                    load_strategy,
                    gpu_ordinal,
                )?))
            } else {
                Ok(Box::new(Ltx2Engine::new(
                    model_name,
                    paths,
                    load_strategy,
                    gpu_ordinal,
                )))
            }
        }
        "wuerstchen" | "wuerstchen-v2" => Ok(Box::new(WuerstchenEngine::new(
            model_name,
            paths,
            load_strategy,
            gpu_ordinal,
            shared_pool,
        ))),
        other => bail!(
            "unknown model family '{}' for model '{}'. Supported: flux, flux2, ltx-video, ltx2, sd15, sd3, sdxl, z-image, qwen-image, qwen-image-edit, wuerstchen",
            other,
            model_name
        ),
    }
}

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

    fn dummy_paths() -> ModelPaths {
        ModelPaths {
            transformer: PathBuf::from("/tmp/transformer"),
            transformer_shards: vec![],
            vae: PathBuf::from("/tmp/vae"),
            spatial_upscaler: None,
            temporal_upscaler: None,
            distilled_lora: None,
            t5_encoder: Some(PathBuf::from("/tmp/t5")),
            clip_encoder: Some(PathBuf::from("/tmp/clip")),
            t5_tokenizer: Some(PathBuf::from("/tmp/t5_tok")),
            clip_tokenizer: Some(PathBuf::from("/tmp/clip_tok")),
            clip_encoder_2: None,
            clip_tokenizer_2: None,
            text_encoder_files: vec![],
            text_tokenizer: None,
            decoder: None,
        }
    }

    #[test]
    fn resolve_family_from_manifest_sd15() {
        let config = Config::default();
        assert_eq!(resolve_family("sd15:fp16", &config), "sd15");
        assert_eq!(resolve_family("dreamshaper-v8:fp16", &config), "sd15");
        assert_eq!(resolve_family("realistic-vision-v5:fp16", &config), "sd15");
    }

    #[test]
    fn resolve_family_from_manifest_sdxl() {
        let config = Config::default();
        assert_eq!(resolve_family("sdxl-base:fp16", &config), "sdxl");
    }

    #[test]
    fn resolve_family_unknown_defaults_to_flux() {
        let config = Config::default();
        assert_eq!(resolve_family("totally-unknown-model", &config), "flux");
    }

    #[test]
    fn resolve_family_config_overrides_manifest() {
        let mut config = Config::default();
        config.models.insert(
            "custom-model".to_string(),
            mold_core::config::ModelConfig {
                family: Some("sd15".to_string()),
                ..Default::default()
            },
        );
        assert_eq!(resolve_family("custom-model", &config), "sd15");
    }

    #[test]
    fn create_engine_sd15() {
        let config = Config::default();
        let engine = create_engine(
            "sd15:fp16".to_string(),
            dummy_paths(),
            &config,
            LoadStrategy::Sequential,
            0,
            false,
        )
        .unwrap();
        assert_eq!(engine.model_name(), "sd15:fp16");
    }

    #[test]
    fn create_engine_sd15_family_alias() {
        // Config-based family alias "sd1.5" should work
        let mut config = Config::default();
        config.models.insert(
            "my-model".to_string(),
            mold_core::config::ModelConfig {
                family: Some("sd1.5".to_string()),
                ..Default::default()
            },
        );
        let engine = create_engine(
            "my-model".to_string(),
            dummy_paths(),
            &config,
            LoadStrategy::Sequential,
            0,
            false,
        )
        .unwrap();
        assert_eq!(engine.model_name(), "my-model");
    }

    #[test]
    fn resolve_family_from_manifest_sd3() {
        let config = Config::default();
        assert_eq!(resolve_family("sd3.5-large:q8", &config), "sd3");
        assert_eq!(resolve_family("sd3.5-medium:q8", &config), "sd3");
        assert_eq!(resolve_family("sd3.5-large-turbo:q8", &config), "sd3");
    }

    #[test]
    fn resolve_family_from_manifest_flux2() {
        let config = Config::default();
        assert_eq!(resolve_family("flux2-klein:bf16", &config), "flux2");
    }

    #[test]
    fn create_engine_flux2() {
        let config = Config::default();
        let engine = create_engine(
            "flux2-klein:bf16".to_string(),
            dummy_paths(),
            &config,
            LoadStrategy::Sequential,
            0,
            false,
        )
        .unwrap();
        assert_eq!(engine.model_name(), "flux2-klein:bf16");
    }

    #[test]
    fn unknown_family_error_lists_ltx_families() {
        let mut config = Config::default();
        config.models.insert(
            "my-model".to_string(),
            mold_core::config::ModelConfig {
                family: Some("mystery".to_string()),
                ..Default::default()
            },
        );
        let err = create_engine(
            "my-model".to_string(),
            dummy_paths(),
            &config,
            LoadStrategy::Sequential,
            0,
            false,
        )
        .err()
        .expect("unknown family should fail");
        let message = err.to_string();
        assert!(message.contains("ltx-video"));
        assert!(message.contains("ltx2"));
    }

    #[test]
    fn create_engine_flux2_family_alias() {
        // Config-based family alias "flux.2" should work
        let mut config = Config::default();
        config.models.insert(
            "my-flux2".to_string(),
            mold_core::config::ModelConfig {
                family: Some("flux.2".to_string()),
                ..Default::default()
            },
        );
        let engine = create_engine(
            "my-flux2".to_string(),
            dummy_paths(),
            &config,
            LoadStrategy::Sequential,
            0,
            false,
        )
        .unwrap();
        assert_eq!(engine.model_name(), "my-flux2");
    }

    #[test]
    fn resolve_family_from_manifest_qwen_image() {
        let config = Config::default();
        assert_eq!(resolve_family("qwen-image:bf16", &config), "qwen-image");
    }

    #[test]
    fn resolve_family_from_manifest_qwen_image_edit() {
        let config = Config::default();
        assert_eq!(
            resolve_family("qwen-image-edit-2511:q4", &config),
            "qwen-image-edit"
        );
    }

    #[test]
    fn create_engine_qwen_image() {
        let mut config = Config::default();
        config.models.insert(
            "my-qwen-image".to_string(),
            mold_core::config::ModelConfig {
                family: Some("qwen-image".to_string()),
                ..Default::default()
            },
        );
        let engine = create_engine(
            "my-qwen-image".to_string(),
            dummy_paths(),
            &config,
            LoadStrategy::Sequential,
            0,
            false,
        )
        .unwrap();
        assert_eq!(engine.model_name(), "my-qwen-image");
    }

    #[test]
    fn create_engine_unknown_family_fails() {
        let mut config = Config::default();
        config.models.insert(
            "bad-model".to_string(),
            mold_core::config::ModelConfig {
                family: Some("nosuchfamily".to_string()),
                ..Default::default()
            },
        );
        let result = create_engine(
            "bad-model".to_string(),
            dummy_paths(),
            &config,
            LoadStrategy::Sequential,
            0,
            false,
        );
        assert!(result.is_err());
        let err = format!("{}", result.err().unwrap());
        assert!(err.contains("nosuchfamily"));
        assert!(err.contains("qwen-image-edit"));
    }

    #[test]
    fn create_engine_qwen_image_edit_routes_to_qwen_engine() {
        let result = create_engine(
            "qwen-image-edit-2511:q4".to_string(),
            dummy_paths(),
            &Config::default(),
            LoadStrategy::Sequential,
            0,
            false,
        );
        assert!(result.is_ok());
    }

    #[test]
    fn resolve_family_from_manifest_wuerstchen() {
        let config = Config::default();
        assert_eq!(resolve_family("wuerstchen-v2:fp16", &config), "wuerstchen");
    }

    #[test]
    fn create_engine_wuerstchen() {
        let mut config = Config::default();
        config.models.insert(
            "my-wuerstchen".to_string(),
            mold_core::config::ModelConfig {
                family: Some("wuerstchen".to_string()),
                ..Default::default()
            },
        );
        let engine = create_engine(
            "my-wuerstchen".to_string(),
            dummy_paths(),
            &config,
            LoadStrategy::Sequential,
            0,
            false,
        )
        .unwrap();
        assert_eq!(engine.model_name(), "my-wuerstchen");
    }

    #[test]
    fn create_engine_wuerstchen_family_alias() {
        let mut config = Config::default();
        config.models.insert(
            "my-wurst".to_string(),
            mold_core::config::ModelConfig {
                family: Some("wuerstchen-v2".to_string()),
                ..Default::default()
            },
        );
        let engine = create_engine(
            "my-wurst".to_string(),
            dummy_paths(),
            &config,
            LoadStrategy::Sequential,
            0,
            false,
        )
        .unwrap();
        assert_eq!(engine.model_name(), "my-wurst");
    }

    // ----- Phase 2.6 single-file routing -----

    fn single_file_paths(checkpoint: &std::path::Path) -> ModelPaths {
        // The factory routing decision keys off transformer == vae +
        // `.safetensors` extension — every other path is a stand-in for
        // companion-pulled tokenizer / encoder assets the factory does
        // not consult when picking the constructor.
        ModelPaths {
            transformer: checkpoint.to_path_buf(),
            transformer_shards: vec![],
            vae: checkpoint.to_path_buf(),
            spatial_upscaler: None,
            temporal_upscaler: None,
            distilled_lora: None,
            t5_encoder: None,
            clip_encoder: Some(PathBuf::from("/tmp/will-be-clobbered-clip-l")),
            t5_tokenizer: None,
            clip_tokenizer: Some(PathBuf::from("/tmp/clip-l-tokenizer.json")),
            clip_encoder_2: Some(PathBuf::from("/tmp/will-be-clobbered-clip-g")),
            clip_tokenizer_2: Some(PathBuf::from("/tmp/clip-g-tokenizer.json")),
            text_encoder_files: vec![],
            text_tokenizer: None,
            decoder: None,
        }
    }

    #[test]
    fn is_single_file_true_when_transformer_eq_vae_safetensors() {
        let path = PathBuf::from("/tmp/some-civitai-checkpoint.safetensors");
        let paths = single_file_paths(&path);
        assert!(
            super::is_single_file(&paths),
            "transformer == vae + .safetensors must trigger single-file dispatch",
        );
    }

    #[test]
    fn is_single_file_false_when_transformer_neq_vae() {
        // Diffusers-layout shards have transformer + vae as distinct files.
        let mut paths = single_file_paths(&PathBuf::from("/tmp/x.safetensors"));
        paths.vae = PathBuf::from("/tmp/different-vae.safetensors");
        assert!(
            !super::is_single_file(&paths),
            "distinct transformer + vae paths must route to the diffusers `new` constructor",
        );
    }

    #[test]
    fn is_single_file_false_when_extension_not_safetensors() {
        // Diffusers shards with a `.bin` (legacy) or no-extension shard
        // must not be misclassified just because transformer == vae would
        // otherwise match (catches future ModelPaths defaults that point
        // both at the same placeholder dir).
        let mut paths = single_file_paths(&PathBuf::from("/tmp/x.bin"));
        paths.transformer = PathBuf::from("/tmp/x.bin");
        paths.vae = PathBuf::from("/tmp/x.bin");
        assert!(
            !super::is_single_file(&paths),
            "non-`.safetensors` extension must never route to single-file",
        );
    }

    /// Synthesise a minimal SD1.5-shaped single-file safetensors so the
    /// factory's `from_single_file` dispatch survives header parsing +
    /// `build_sd15_remap` validation. Tensor data is one zero F32 per key
    /// — no model weights are materialised.
    fn synth_sd15_for_factory(name: &str) -> PathBuf {
        use safetensors::tensor::{serialize_to_file, Dtype as SafeDtype, TensorView};
        use std::collections::HashMap;

        let path = std::env::temp_dir().join(format!(
            "mold-factory-sd15-{}-{}-{}.safetensors",
            name,
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos(),
        ));

        let keys: &[&str] = &[
            "model.diffusion_model.input_blocks.0.0.weight",
            "first_stage_model.encoder.down.0.block.0.norm1.weight",
            "cond_stage_model.transformer.text_model.encoder.layers.0.self_attn.q_proj.weight",
        ];
        let f32_zero = 0.0f32.to_le_bytes().to_vec();
        let buffers: Vec<Vec<u8>> = keys.iter().map(|_| f32_zero.clone()).collect();
        let mut tensors: HashMap<String, TensorView<'_>> = HashMap::new();
        for (key, buf) in keys.iter().zip(buffers.iter()) {
            tensors.insert(
                (*key).to_string(),
                TensorView::new(SafeDtype::F32, vec![1], buf).unwrap(),
            );
        }
        serialize_to_file(&tensors, &None, &path).unwrap();
        path
    }

    /// SDXL synthetic checkpoint — adds CLIP-G slabs (incl. the fused QKV
    /// row stack) so `build_sdxl_remap` exercises the `FusedSlice` path at
    /// dispatch time.
    fn synth_sdxl_for_factory(name: &str) -> PathBuf {
        use safetensors::tensor::{serialize_to_file, Dtype as SafeDtype, TensorView};
        use std::collections::HashMap;

        let path = std::env::temp_dir().join(format!(
            "mold-factory-sdxl-{}-{}-{}.safetensors",
            name,
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos(),
        ));

        let keys: &[&str] = &[
            "model.diffusion_model.input_blocks.0.0.weight",
            "first_stage_model.encoder.down.0.block.0.norm1.weight",
            "conditioner.embedders.0.transformer.text_model.encoder.layers.0.self_attn.q_proj.weight",
            "conditioner.embedders.1.model.transformer.resblocks.0.attn.in_proj_weight",
            "conditioner.embedders.1.model.text_projection",
        ];
        let f32_zero = 0.0f32.to_le_bytes().to_vec();
        let buffers: Vec<Vec<u8>> = keys.iter().map(|_| f32_zero.clone()).collect();
        let mut tensors: HashMap<String, TensorView<'_>> = HashMap::new();
        for (key, buf) in keys.iter().zip(buffers.iter()) {
            tensors.insert(
                (*key).to_string(),
                TensorView::new(SafeDtype::F32, vec![1], buf).unwrap(),
            );
        }
        serialize_to_file(&tensors, &None, &path).unwrap();
        path
    }

    #[test]
    fn factory_sd15_single_file_dispatches_to_from_single_file() {
        let single_file = synth_sd15_for_factory("dispatch");
        let paths = single_file_paths(&single_file);

        let engine = create_engine(
            "sd15:fp16".to_string(),
            paths,
            &Config::default(),
            LoadStrategy::Sequential,
            0,
            false,
        )
        .expect("single-file dispatch must succeed against a valid SD1.5 checkpoint");

        assert_eq!(engine.model_name(), "sd15:fp16");

        // `from_single_file` rebuilds `ModelPaths` so every component
        // resolves through the single file; the original
        // `clip_encoder = /tmp/will-be-clobbered-clip-l` we passed in
        // must be replaced by the checkpoint path. If the factory had
        // dispatched to `SD15Engine::new`, the input `clip_encoder`
        // would have survived verbatim.
        let resolved = engine
            .model_paths()
            .expect("SD15 engine must expose its ModelPaths via the trait");
        assert_eq!(
            resolved.clip_encoder.as_deref(),
            Some(single_file.as_path()),
            "single-file dispatch must materialise clip_encoder from the checkpoint",
        );

        let _ = std::fs::remove_file(single_file);
    }

    #[test]
    fn factory_sd15_diffusers_layout_routes_to_new_constructor() {
        // Distinct transformer + vae paths — the diffusers-layout signal.
        // `SD15Engine::new` does not validate path existence, so this
        // succeeds even with non-existent paths. If the factory had
        // misrouted to `from_single_file`, the missing-file check would
        // surface a `single-file checkpoint not found` error.
        let mut paths = dummy_paths();
        paths.transformer =
            PathBuf::from("/tmp/diffusers/unet/diffusion_pytorch_model.safetensors");
        paths.vae = PathBuf::from("/tmp/diffusers/vae/diffusion_pytorch_model.safetensors");

        let engine = create_engine(
            "sd15:fp16".to_string(),
            paths,
            &Config::default(),
            LoadStrategy::Sequential,
            0,
            false,
        )
        .expect("diffusers-layout SD1.5 must route to `new` and not validate paths");

        // The original `clip_encoder` path must survive untouched —
        // proof we did **not** take the single-file branch.
        let resolved = engine.model_paths().expect("SD15 must expose paths");
        assert_eq!(
            resolved.clip_encoder.as_deref(),
            Some(std::path::Path::new("/tmp/clip")),
        );
    }

    #[test]
    fn factory_sd15_single_file_missing_checkpoint_surfaces_error() {
        // transformer == vae + `.safetensors`, but the file does not
        // exist. The single-file path errors at the existence check; the
        // diffusers `new` path would instead succeed (no I/O at construct
        // time). This test pins down which branch ran.
        let bogus = std::env::temp_dir().join(format!(
            "mold-factory-sd15-missing-{}-{}.safetensors",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos(),
        ));
        let paths = single_file_paths(&bogus);

        let result = create_engine(
            "sd15:fp16".to_string(),
            paths,
            &Config::default(),
            LoadStrategy::Sequential,
            0,
            false,
        );

        let err = result
            .err()
            .expect("missing single-file checkpoint must surface as constructor error");
        assert!(
            err.to_string().contains("single-file checkpoint not found"),
            "expected single-file error, got: {err}",
        );
    }

    #[test]
    fn factory_sdxl_single_file_dispatches_to_from_single_file() {
        let single_file = synth_sdxl_for_factory("dispatch");
        let paths = single_file_paths(&single_file);

        let engine = create_engine(
            "sdxl-base:fp16".to_string(),
            paths,
            &Config::default(),
            LoadStrategy::Sequential,
            0,
            false,
        )
        .expect("single-file dispatch must succeed against a valid SDXL checkpoint");

        let resolved = engine
            .model_paths()
            .expect("SDXL engine must expose its ModelPaths via the trait");
        // SDXL adds the CLIP-G identity check on top of CLIP-L; both must
        // resolve to the single-file path after dispatch.
        assert_eq!(
            resolved.clip_encoder.as_deref(),
            Some(single_file.as_path()),
            "single-file dispatch must materialise clip_encoder (CLIP-L) from the checkpoint",
        );
        assert_eq!(
            resolved.clip_encoder_2.as_deref(),
            Some(single_file.as_path()),
            "single-file dispatch must materialise clip_encoder_2 (CLIP-G) from the checkpoint",
        );

        let _ = std::fs::remove_file(single_file);
    }

    #[test]
    fn factory_sdxl_single_file_threads_is_turbo_via_model_config() {
        // is_turbo is plumbed through model_cfg → from_single_file.
        // Field-level verification of the threaded value lives in
        // `sdxl/pipeline.rs::tests` where direct field access works; at
        // the factory boundary we only assert that an `is_turbo = true`
        // model config dispatches successfully (which proves the
        // constructor signature accepts the threaded value).
        let single_file = synth_sdxl_for_factory("turbo");
        let paths = single_file_paths(&single_file);

        let mut config = Config::default();
        config.models.insert(
            "sdxl-turbo:fp16".to_string(),
            mold_core::config::ModelConfig {
                family: Some("sdxl".to_string()),
                is_turbo: Some(true),
                ..Default::default()
            },
        );

        let engine = create_engine(
            "sdxl-turbo:fp16".to_string(),
            paths,
            &config,
            LoadStrategy::Sequential,
            0,
            false,
        )
        .expect("is_turbo = true must thread through to the single-file constructor");
        assert_eq!(engine.model_name(), "sdxl-turbo:fp16");

        let _ = std::fs::remove_file(single_file);
    }
}