koharu-diffusion 0.62.0-alpha.1

Safe Rust bindings for stable-diffusion.cpp with runtime dynamic loading.
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
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
use std::{ffi::CString, fmt, os::raw::c_char, path::PathBuf, ptr};

use crate::{
    CacheMode, Error, GrayImage, HiresUpscaler, LoraApplyMode, Prediction, Result, RgbImage,
    RngType, SampleMethod, Scheduler, VaeFormat, WeightType,
    ffi::{c_int_len, cstring, path_cstring, u32_len},
    image::{optional_raw_gray_image, optional_raw_rgb_image, raw_rgb_images},
    sys,
};

#[derive(Default)]
struct StringPool {
    values: Vec<CString>,
}

impl StringPool {
    fn add(&mut self, value: &str, field: &'static str) -> Result<*const c_char> {
        let value = cstring(value, field)?;
        let pointer = value.as_ptr();
        self.values.push(value);
        Ok(pointer)
    }

    fn add_optional(&mut self, value: Option<&str>, field: &'static str) -> Result<*const c_char> {
        value
            .map(|value| self.add(value, field))
            .transpose()
            .map(|pointer| pointer.unwrap_or(ptr::null()))
    }

    fn add_path(&mut self, path: &std::path::Path, field: &'static str) -> Result<*const c_char> {
        let path = path_cstring(path, field)?;
        let pointer = path.as_ptr();
        self.values.push(path);
        Ok(pointer)
    }

    fn add_optional_path(
        &mut self,
        path: Option<&std::path::Path>,
        field: &'static str,
    ) -> Result<*const c_char> {
        path.map(|path| self.add_path(path, field))
            .transpose()
            .map(|pointer| pointer.unwrap_or(ptr::null()))
    }
}

fn mut_ptr_or_null<T>(values: &mut [T]) -> *mut T {
    if values.is_empty() {
        ptr::null_mut()
    } else {
        values.as_mut_ptr()
    }
}

fn ptr_or_null<T>(values: &[T]) -> *const T {
    if values.is_empty() {
        ptr::null()
    } else {
        values.as_ptr()
    }
}

/// A textual-inversion embedding loaded with a model context.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Embedding {
    pub name: String,
    pub path: PathBuf,
}

/// Model files and backend settings used to create a [`crate::Context`].
#[derive(Debug, Clone)]
pub struct ContextParams {
    pub model_path: Option<PathBuf>,
    pub clip_l_path: Option<PathBuf>,
    pub clip_g_path: Option<PathBuf>,
    pub clip_vision_path: Option<PathBuf>,
    pub t5xxl_path: Option<PathBuf>,
    pub llm_path: Option<PathBuf>,
    pub llm_vision_path: Option<PathBuf>,
    pub diffusion_model_path: Option<PathBuf>,
    pub high_noise_diffusion_model_path: Option<PathBuf>,
    pub uncond_diffusion_model_path: Option<PathBuf>,
    pub embeddings_connectors_path: Option<PathBuf>,
    pub vae_path: Option<PathBuf>,
    pub audio_vae_path: Option<PathBuf>,
    pub taesd_path: Option<PathBuf>,
    pub control_net_path: Option<PathBuf>,
    pub embeddings: Vec<Embedding>,
    pub photo_maker_path: Option<PathBuf>,
    pub pulid_weights_path: Option<PathBuf>,
    pub tensor_type_rules: Option<String>,
    pub n_threads: i32,
    pub weight_type: WeightType,
    pub rng_type: RngType,
    pub sampler_rng_type: RngType,
    pub prediction: Prediction,
    pub lora_apply_mode: LoraApplyMode,
    pub enable_mmap: bool,
    pub flash_attention: bool,
    pub diffusion_flash_attention: bool,
    pub tae_preview_only: bool,
    pub diffusion_conv_direct: bool,
    pub vae_conv_direct: bool,
    pub force_sdxl_vae_conv_scale: bool,
    pub vae_format: VaeFormat,
    pub max_vram: Option<String>,
    pub stream_layers: bool,
    pub eager_load: bool,
    pub backend: Option<String>,
    pub params_backend: Option<String>,
    pub split_mode: Option<String>,
    pub auto_fit: bool,
    pub rpc_servers: Option<String>,
    pub model_args: Option<String>,
}

impl Default for ContextParams {
    fn default() -> Self {
        Self {
            model_path: None,
            clip_l_path: None,
            clip_g_path: None,
            clip_vision_path: None,
            t5xxl_path: None,
            llm_path: None,
            llm_vision_path: None,
            diffusion_model_path: None,
            high_noise_diffusion_model_path: None,
            uncond_diffusion_model_path: None,
            embeddings_connectors_path: None,
            vae_path: None,
            audio_vae_path: None,
            taesd_path: None,
            control_net_path: None,
            embeddings: Vec::new(),
            photo_maker_path: None,
            pulid_weights_path: None,
            tensor_type_rules: None,
            n_threads: crate::physical_core_count(),
            weight_type: WeightType::Auto,
            rng_type: RngType::Cuda,
            sampler_rng_type: RngType::Auto,
            prediction: Prediction::Auto,
            lora_apply_mode: LoraApplyMode::Auto,
            enable_mmap: false,
            flash_attention: false,
            diffusion_flash_attention: false,
            tae_preview_only: false,
            diffusion_conv_direct: false,
            vae_conv_direct: false,
            force_sdxl_vae_conv_scale: false,
            vae_format: VaeFormat::Auto,
            max_vram: None,
            stream_layers: false,
            eager_load: false,
            backend: None,
            params_backend: None,
            split_mode: None,
            auto_fit: false,
            rpc_servers: None,
            model_args: None,
        }
    }
}

pub(crate) struct NativeContextParams {
    pub(crate) raw: sys::sd_ctx_params_t,
    _strings: StringPool,
    _embeddings: Vec<sys::sd_embedding_t>,
}

impl ContextParams {
    pub(crate) fn to_native(&self) -> Result<NativeContextParams> {
        if self.n_threads <= 0 {
            return Err(Error::InvalidParameter {
                name: "n_threads",
                reason: "must be greater than zero",
            });
        }
        let mut strings = StringPool::default();
        let model_path = strings.add_optional_path(self.model_path.as_deref(), "model_path")?;
        let clip_l_path = strings.add_optional_path(self.clip_l_path.as_deref(), "clip_l_path")?;
        let clip_g_path = strings.add_optional_path(self.clip_g_path.as_deref(), "clip_g_path")?;
        let clip_vision_path =
            strings.add_optional_path(self.clip_vision_path.as_deref(), "clip_vision_path")?;
        let t5xxl_path = strings.add_optional_path(self.t5xxl_path.as_deref(), "t5xxl_path")?;
        let llm_path = strings.add_optional_path(self.llm_path.as_deref(), "llm_path")?;
        let llm_vision_path =
            strings.add_optional_path(self.llm_vision_path.as_deref(), "llm_vision_path")?;
        let diffusion_model_path = strings
            .add_optional_path(self.diffusion_model_path.as_deref(), "diffusion_model_path")?;
        let high_noise_diffusion_model_path = strings.add_optional_path(
            self.high_noise_diffusion_model_path.as_deref(),
            "high_noise_diffusion_model_path",
        )?;
        let uncond_diffusion_model_path = strings.add_optional_path(
            self.uncond_diffusion_model_path.as_deref(),
            "uncond_diffusion_model_path",
        )?;
        let embeddings_connectors_path = strings.add_optional_path(
            self.embeddings_connectors_path.as_deref(),
            "embeddings_connectors_path",
        )?;
        let vae_path = strings.add_optional_path(self.vae_path.as_deref(), "vae_path")?;
        let audio_vae_path =
            strings.add_optional_path(self.audio_vae_path.as_deref(), "audio_vae_path")?;
        let taesd_path = strings.add_optional_path(self.taesd_path.as_deref(), "taesd_path")?;
        let control_net_path =
            strings.add_optional_path(self.control_net_path.as_deref(), "control_net_path")?;

        let mut embeddings = Vec::with_capacity(self.embeddings.len());
        for embedding in &self.embeddings {
            embeddings.push(sys::sd_embedding_t {
                name: strings.add(&embedding.name, "embedding name")?,
                path: strings.add_path(&embedding.path, "embedding path")?,
            });
        }
        let embedding_count = u32_len(embeddings.len(), "embeddings")?;

        let photo_maker_path =
            strings.add_optional_path(self.photo_maker_path.as_deref(), "photo_maker_path")?;
        let pulid_weights_path =
            strings.add_optional_path(self.pulid_weights_path.as_deref(), "pulid_weights_path")?;
        let tensor_type_rules =
            strings.add_optional(self.tensor_type_rules.as_deref(), "tensor_type_rules")?;
        let max_vram = strings.add_optional(self.max_vram.as_deref(), "max_vram")?;
        let backend = strings.add_optional(self.backend.as_deref(), "backend")?;
        let params_backend =
            strings.add_optional(self.params_backend.as_deref(), "params_backend")?;
        let split_mode = strings.add_optional(self.split_mode.as_deref(), "split_mode")?;
        let rpc_servers = strings.add_optional(self.rpc_servers.as_deref(), "rpc_servers")?;
        let model_args = strings.add_optional(self.model_args.as_deref(), "model_args")?;

        let raw = sys::sd_ctx_params_t {
            model_path,
            clip_l_path,
            clip_g_path,
            clip_vision_path,
            t5xxl_path,
            llm_path,
            llm_vision_path,
            diffusion_model_path,
            high_noise_diffusion_model_path,
            uncond_diffusion_model_path,
            embeddings_connectors_path,
            vae_path,
            audio_vae_path,
            taesd_path,
            control_net_path,
            embeddings: ptr_or_null(&embeddings),
            embedding_count,
            photo_maker_path,
            pulid_weights_path,
            tensor_type_rules,
            n_threads: self.n_threads,
            wtype: self.weight_type.as_raw(),
            rng_type: self.rng_type.as_raw(),
            sampler_rng_type: self.sampler_rng_type.as_raw(),
            prediction: self.prediction.as_raw(),
            lora_apply_mode: self.lora_apply_mode.as_raw(),
            enable_mmap: self.enable_mmap,
            flash_attn: self.flash_attention,
            diffusion_flash_attn: self.diffusion_flash_attention,
            tae_preview_only: self.tae_preview_only,
            diffusion_conv_direct: self.diffusion_conv_direct,
            vae_conv_direct: self.vae_conv_direct,
            force_sdxl_vae_conv_scale: self.force_sdxl_vae_conv_scale,
            vae_format: self.vae_format.as_raw(),
            max_vram,
            stream_layers: self.stream_layers,
            eager_load: self.eager_load,
            backend,
            params_backend,
            split_mode,
            auto_fit: self.auto_fit,
            rpc_servers,
            model_args,
        };
        Ok(NativeContextParams {
            raw,
            _strings: strings,
            _embeddings: embeddings,
        })
    }
}

impl fmt::Display for ContextParams {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(formatter, "model_path: {:?}", self.model_path)?;
        writeln!(
            formatter,
            "diffusion_model_path: {:?}",
            self.diffusion_model_path
        )?;
        writeln!(
            formatter,
            "high_noise_diffusion_model_path: {:?}",
            self.high_noise_diffusion_model_path
        )?;
        writeln!(formatter, "vae_path: {:?}", self.vae_path)?;
        writeln!(formatter, "n_threads: {}", self.n_threads)?;
        writeln!(formatter, "weight_type: {}", self.weight_type)?;
        writeln!(formatter, "rng_type: {}", self.rng_type)?;
        writeln!(formatter, "sampler_rng_type: {}", self.sampler_rng_type)?;
        writeln!(formatter, "prediction: {}", self.prediction)?;
        writeln!(formatter, "backend: {:?}", self.backend)?;
        writeln!(formatter, "params_backend: {:?}", self.params_backend)?;
        write!(formatter, "auto_fit: {}", self.auto_fit)
    }
}

/// Skip-layer guidance settings.
#[derive(Debug, Clone, PartialEq)]
pub struct SkipLayerGuidance {
    pub layers: Vec<i32>,
    pub layer_start: f32,
    pub layer_end: f32,
    pub scale: f32,
}

impl Default for SkipLayerGuidance {
    fn default() -> Self {
        Self {
            layers: Vec::new(),
            layer_start: 0.01,
            layer_end: 0.2,
            scale: 0.0,
        }
    }
}

/// Text, image, distilled, and skip-layer guidance settings.
#[derive(Debug, Clone, PartialEq)]
pub struct GuidanceParams {
    pub text_cfg: f32,
    pub image_cfg: f32,
    pub distilled_guidance: f32,
    pub skip_layer: SkipLayerGuidance,
}

impl Default for GuidanceParams {
    fn default() -> Self {
        Self {
            text_cfg: 7.0,
            image_cfg: f32::INFINITY,
            distilled_guidance: 3.5,
            skip_layer: SkipLayerGuidance::default(),
        }
    }
}

/// Sampling settings shared by image and video generation.
#[derive(Debug, Clone, PartialEq)]
pub struct SampleParams {
    pub guidance: GuidanceParams,
    pub scheduler: Scheduler,
    pub sample_method: SampleMethod,
    pub sample_steps: i32,
    pub eta: f32,
    pub shifted_timestep: i32,
    pub custom_sigmas: Vec<f32>,
    pub flow_shift: f32,
    pub extra_args: Option<String>,
}

impl Default for SampleParams {
    fn default() -> Self {
        Self {
            guidance: GuidanceParams::default(),
            scheduler: Scheduler::Auto,
            sample_method: SampleMethod::Auto,
            sample_steps: 20,
            eta: f32::INFINITY,
            shifted_timestep: 0,
            custom_sigmas: Vec::new(),
            flow_shift: f32::INFINITY,
            extra_args: None,
        }
    }
}

struct NativeSampleParams {
    raw: sys::sd_sample_params_t,
    _layers: Vec<i32>,
    _sigmas: Vec<f32>,
}

impl SampleParams {
    fn to_native(&self, strings: &mut StringPool) -> Result<NativeSampleParams> {
        let mut layers = self.guidance.skip_layer.layers.clone();
        let mut sigmas = self.custom_sigmas.clone();
        let layer_count = layers.len();
        let custom_sigmas_count = c_int_len(sigmas.len(), "custom sigmas")?;
        let extra_sample_args =
            strings.add_optional(self.extra_args.as_deref(), "extra_sample_args")?;
        Ok(NativeSampleParams {
            raw: sys::sd_sample_params_t {
                guidance: sys::sd_guidance_params_t {
                    txt_cfg: self.guidance.text_cfg,
                    img_cfg: self.guidance.image_cfg,
                    distilled_guidance: self.guidance.distilled_guidance,
                    slg: sys::sd_slg_params_t {
                        layers: mut_ptr_or_null(&mut layers),
                        layer_count,
                        layer_start: self.guidance.skip_layer.layer_start,
                        layer_end: self.guidance.skip_layer.layer_end,
                        scale: self.guidance.skip_layer.scale,
                    },
                },
                scheduler: self.scheduler.as_raw(),
                sample_method: self.sample_method.as_raw(),
                sample_steps: self.sample_steps,
                eta: self.eta,
                shifted_timestep: self.shifted_timestep,
                custom_sigmas: mut_ptr_or_null(&mut sigmas),
                custom_sigmas_count,
                flow_shift: self.flow_shift,
                extra_sample_args,
            },
            _layers: layers,
            _sigmas: sigmas,
        })
    }
}

impl fmt::Display for SampleParams {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            formatter,
            "(text_cfg: {:.2}, image_cfg: {:.2}, distilled_guidance: {:.2}, scheduler: {}, sample_method: {}, sample_steps: {}, eta: {:.2}, shifted_timestep: {}, flow_shift: {:.2}, extra_args: {:?})",
            self.guidance.text_cfg,
            if self.guidance.image_cfg.is_finite() {
                self.guidance.image_cfg
            } else {
                self.guidance.text_cfg
            },
            self.guidance.distilled_guidance,
            self.scheduler,
            self.sample_method,
            self.sample_steps,
            self.eta,
            self.shifted_timestep,
            self.flow_shift,
            self.extra_args,
        )
    }
}

/// VAE tiling controls.
#[derive(Debug, Clone, PartialEq)]
pub struct TilingParams {
    pub enabled: bool,
    pub temporal_tiling: bool,
    pub tile_size_x: i32,
    pub tile_size_y: i32,
    pub target_overlap: f32,
    pub relative_size_x: f32,
    pub relative_size_y: f32,
    pub extra_args: Option<String>,
}

impl Default for TilingParams {
    fn default() -> Self {
        Self {
            enabled: false,
            temporal_tiling: false,
            tile_size_x: 0,
            tile_size_y: 0,
            target_overlap: 0.5,
            relative_size_x: 0.0,
            relative_size_y: 0.0,
            extra_args: None,
        }
    }
}

impl TilingParams {
    fn to_native(&self, strings: &mut StringPool) -> Result<sys::sd_tiling_params_t> {
        Ok(sys::sd_tiling_params_t {
            enabled: self.enabled,
            temporal_tiling: self.temporal_tiling,
            tile_size_x: self.tile_size_x,
            tile_size_y: self.tile_size_y,
            target_overlap: self.target_overlap,
            rel_size_x: self.relative_size_x,
            rel_size_y: self.relative_size_y,
            extra_tiling_args: strings
                .add_optional(self.extra_args.as_deref(), "extra_tiling_args")?,
        })
    }
}

/// Diffusion feature-cache controls.
#[derive(Debug, Clone, PartialEq)]
pub struct CacheParams {
    pub mode: CacheMode,
    pub reuse_threshold: f32,
    pub start_percent: f32,
    pub end_percent: f32,
    pub error_decay_rate: f32,
    pub use_relative_threshold: bool,
    pub reset_error_on_compute: bool,
    pub fn_compute_blocks: i32,
    pub bn_compute_blocks: i32,
    pub residual_diff_threshold: f32,
    pub max_warmup_steps: i32,
    pub max_cached_steps: i32,
    pub max_continuous_cached_steps: i32,
    pub taylorseer_derivatives: i32,
    pub taylorseer_skip_interval: i32,
    pub scm_mask: Option<String>,
    pub scm_policy_dynamic: bool,
    pub spectrum_w: f32,
    pub spectrum_m: i32,
    pub spectrum_lambda: f32,
    pub spectrum_window_size: i32,
    pub spectrum_flex_window: f32,
    pub spectrum_warmup_steps: i32,
    pub spectrum_stop_percent: f32,
}

impl Default for CacheParams {
    fn default() -> Self {
        Self {
            mode: CacheMode::Disabled,
            reuse_threshold: f32::INFINITY,
            start_percent: 0.15,
            end_percent: 0.95,
            error_decay_rate: 1.0,
            use_relative_threshold: true,
            reset_error_on_compute: true,
            fn_compute_blocks: 8,
            bn_compute_blocks: 0,
            residual_diff_threshold: 0.08,
            max_warmup_steps: 8,
            max_cached_steps: -1,
            max_continuous_cached_steps: -1,
            taylorseer_derivatives: 1,
            taylorseer_skip_interval: 1,
            scm_mask: None,
            scm_policy_dynamic: true,
            spectrum_w: 0.4,
            spectrum_m: 3,
            spectrum_lambda: 1.0,
            spectrum_window_size: 2,
            spectrum_flex_window: 0.5,
            spectrum_warmup_steps: 4,
            spectrum_stop_percent: 0.9,
        }
    }
}

impl CacheParams {
    fn to_native(&self, strings: &mut StringPool) -> Result<sys::sd_cache_params_t> {
        Ok(sys::sd_cache_params_t {
            mode: self.mode.as_raw(),
            reuse_threshold: self.reuse_threshold,
            start_percent: self.start_percent,
            end_percent: self.end_percent,
            error_decay_rate: self.error_decay_rate,
            use_relative_threshold: self.use_relative_threshold,
            reset_error_on_compute: self.reset_error_on_compute,
            Fn_compute_blocks: self.fn_compute_blocks,
            Bn_compute_blocks: self.bn_compute_blocks,
            residual_diff_threshold: self.residual_diff_threshold,
            max_warmup_steps: self.max_warmup_steps,
            max_cached_steps: self.max_cached_steps,
            max_continuous_cached_steps: self.max_continuous_cached_steps,
            taylorseer_n_derivatives: self.taylorseer_derivatives,
            taylorseer_skip_interval: self.taylorseer_skip_interval,
            scm_mask: strings.add_optional(self.scm_mask.as_deref(), "scm_mask")?,
            scm_policy_dynamic: self.scm_policy_dynamic,
            spectrum_w: self.spectrum_w,
            spectrum_m: self.spectrum_m,
            spectrum_lam: self.spectrum_lambda,
            spectrum_window_size: self.spectrum_window_size,
            spectrum_flex_window: self.spectrum_flex_window,
            spectrum_warmup_steps: self.spectrum_warmup_steps,
            spectrum_stop_percent: self.spectrum_stop_percent,
        })
    }
}

/// High-resolution second-pass settings.
#[derive(Debug, Clone, PartialEq)]
pub struct HiresParams {
    pub enabled: bool,
    pub upscaler: HiresUpscaler,
    pub model_path: Option<PathBuf>,
    pub scale: f32,
    pub target_width: i32,
    pub target_height: i32,
    pub steps: i32,
    pub denoising_strength: f32,
    pub upscale_tile_size: i32,
    pub custom_sigmas: Vec<f32>,
}

impl Default for HiresParams {
    fn default() -> Self {
        Self {
            enabled: false,
            upscaler: HiresUpscaler::Latent,
            model_path: None,
            scale: 2.0,
            target_width: 0,
            target_height: 0,
            steps: 0,
            denoising_strength: 0.7,
            upscale_tile_size: 128,
            custom_sigmas: Vec::new(),
        }
    }
}

struct NativeHiresParams {
    raw: sys::sd_hires_params_t,
    _sigmas: Vec<f32>,
}

impl HiresParams {
    fn to_native(&self, strings: &mut StringPool) -> Result<NativeHiresParams> {
        let mut sigmas = self.custom_sigmas.clone();
        let custom_sigmas_count = c_int_len(sigmas.len(), "high-resolution custom sigmas")?;
        Ok(NativeHiresParams {
            raw: sys::sd_hires_params_t {
                enabled: self.enabled,
                upscaler: self.upscaler.as_raw(),
                model_path: strings
                    .add_optional_path(self.model_path.as_deref(), "hires model_path")?,
                scale: self.scale,
                target_width: self.target_width,
                target_height: self.target_height,
                steps: self.steps,
                denoising_strength: self.denoising_strength,
                upscale_tile_size: self.upscale_tile_size,
                custom_sigmas: mut_ptr_or_null(&mut sigmas),
                custom_sigmas_count,
            },
            _sigmas: sigmas,
        })
    }
}

/// A LoRA adapter applied for one generation request.
#[derive(Debug, Clone, PartialEq)]
pub struct Lora {
    pub path: PathBuf,
    pub multiplier: f32,
    pub high_noise: bool,
}

/// PhotoMaker identity inputs.
#[derive(Debug, Clone, PartialEq)]
pub struct PhotoMakerParams {
    pub id_images: Vec<RgbImage>,
    pub id_embedding_path: Option<PathBuf>,
    pub style_strength: f32,
}

impl Default for PhotoMakerParams {
    fn default() -> Self {
        Self {
            id_images: Vec::new(),
            id_embedding_path: None,
            style_strength: 20.0,
        }
    }
}

/// PuLID identity input.
#[derive(Debug, Clone, PartialEq)]
pub struct PulidParams {
    pub id_embedding_path: Option<PathBuf>,
    pub weight: f32,
}

impl Default for PulidParams {
    fn default() -> Self {
        Self {
            id_embedding_path: None,
            weight: 1.0,
        }
    }
}

/// Complete image-generation request.
#[derive(Debug, Clone, PartialEq)]
pub struct ImageGenerationParams {
    pub loras: Vec<Lora>,
    pub prompt: String,
    pub negative_prompt: String,
    pub clip_skip: i32,
    pub init_image: Option<RgbImage>,
    pub reference_images: Vec<RgbImage>,
    pub auto_resize_reference_images: bool,
    pub increase_reference_index: bool,
    pub mask_image: Option<GrayImage>,
    pub width: i32,
    pub height: i32,
    pub sample: SampleParams,
    pub strength: f32,
    pub seed: i64,
    pub batch_count: i32,
    pub control_image: Option<RgbImage>,
    pub control_strength: f32,
    pub photo_maker: PhotoMakerParams,
    pub pulid: PulidParams,
    pub vae_tiling: TilingParams,
    pub cache: CacheParams,
    pub hires: HiresParams,
    pub qwen_image_layers: i32,
    pub circular_x: bool,
    pub circular_y: bool,
}

impl Default for ImageGenerationParams {
    fn default() -> Self {
        Self {
            loras: Vec::new(),
            prompt: String::new(),
            negative_prompt: String::new(),
            clip_skip: -1,
            init_image: None,
            reference_images: Vec::new(),
            auto_resize_reference_images: false,
            increase_reference_index: false,
            mask_image: None,
            width: 512,
            height: 512,
            sample: SampleParams::default(),
            strength: 0.75,
            seed: -1,
            batch_count: 1,
            control_image: None,
            control_strength: 0.9,
            photo_maker: PhotoMakerParams::default(),
            pulid: PulidParams::default(),
            vae_tiling: TilingParams::default(),
            cache: CacheParams::default(),
            hires: HiresParams::default(),
            qwen_image_layers: 3,
            circular_x: false,
            circular_y: false,
        }
    }
}

pub(crate) struct NativeImageGenerationParams {
    pub(crate) raw: sys::sd_img_gen_params_t,
    _strings: StringPool,
    _loras: Vec<sys::sd_lora_t>,
    _reference_images: Vec<sys::sd_image_t>,
    _photo_maker_images: Vec<sys::sd_image_t>,
    _sample: NativeSampleParams,
    _hires: NativeHiresParams,
}

fn build_loras(loras: &[Lora], strings: &mut StringPool) -> Result<Vec<sys::sd_lora_t>> {
    let mut native = Vec::with_capacity(loras.len());
    for lora in loras {
        native.push(sys::sd_lora_t {
            is_high_noise: lora.high_noise,
            multiplier: lora.multiplier,
            path: strings.add_path(&lora.path, "LoRA path")?,
        });
    }
    Ok(native)
}

impl ImageGenerationParams {
    pub(crate) fn to_native(&self) -> Result<NativeImageGenerationParams> {
        if self.width <= 0 || self.height <= 0 {
            return Err(Error::InvalidParameter {
                name: "image dimensions",
                reason: "width and height must be greater than zero",
            });
        }
        if self.batch_count <= 0 {
            return Err(Error::InvalidParameter {
                name: "batch_count",
                reason: "must be greater than zero",
            });
        }
        let mut strings = StringPool::default();
        let prompt = strings.add(&self.prompt, "prompt")?;
        let negative_prompt = strings.add(&self.negative_prompt, "negative_prompt")?;
        let loras = build_loras(&self.loras, &mut strings)?;
        let lora_count = u32_len(loras.len(), "LoRAs")?;
        let mut reference_images = raw_rgb_images(&self.reference_images)?;
        let reference_image_count = c_int_len(reference_images.len(), "reference images")?;
        let mut photo_maker_images = raw_rgb_images(&self.photo_maker.id_images)?;
        let photo_maker_image_count =
            c_int_len(photo_maker_images.len(), "PhotoMaker identity images")?;
        let sample = self.sample.to_native(&mut strings)?;
        let vae_tiling = self.vae_tiling.to_native(&mut strings)?;
        let cache = self.cache.to_native(&mut strings)?;
        let hires = self.hires.to_native(&mut strings)?;
        let photo_maker_path = strings.add_optional_path(
            self.photo_maker.id_embedding_path.as_deref(),
            "PhotoMaker identity embedding path",
        )?;
        let pulid_path = strings.add_optional_path(
            self.pulid.id_embedding_path.as_deref(),
            "PuLID identity embedding path",
        )?;

        let raw = sys::sd_img_gen_params_t {
            loras: ptr_or_null(&loras),
            lora_count,
            prompt,
            negative_prompt,
            clip_skip: self.clip_skip,
            init_image: optional_raw_rgb_image(self.init_image.as_ref())?,
            ref_images: mut_ptr_or_null(&mut reference_images),
            ref_images_count: reference_image_count,
            auto_resize_ref_image: self.auto_resize_reference_images,
            increase_ref_index: self.increase_reference_index,
            mask_image: optional_raw_gray_image(self.mask_image.as_ref())?,
            width: self.width,
            height: self.height,
            sample_params: sample.raw,
            strength: self.strength,
            seed: self.seed,
            batch_count: self.batch_count,
            control_image: optional_raw_rgb_image(self.control_image.as_ref())?,
            control_strength: self.control_strength,
            pm_params: sys::sd_pm_params_t {
                id_images: mut_ptr_or_null(&mut photo_maker_images),
                id_images_count: photo_maker_image_count,
                id_embed_path: photo_maker_path,
                style_strength: self.photo_maker.style_strength,
            },
            pulid_params: sys::sd_pulid_params_t {
                id_embedding_path: pulid_path,
                id_weight: self.pulid.weight,
            },
            vae_tiling_params: vae_tiling,
            cache,
            hires: hires.raw,
            qwen_image_layers: self.qwen_image_layers,
            circular_x: self.circular_x,
            circular_y: self.circular_y,
        };
        Ok(NativeImageGenerationParams {
            raw,
            _strings: strings,
            _loras: loras,
            _reference_images: reference_images,
            _photo_maker_images: photo_maker_images,
            _sample: sample,
            _hires: hires,
        })
    }
}

impl fmt::Display for ImageGenerationParams {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(formatter, "prompt: {}", self.prompt)?;
        writeln!(formatter, "negative_prompt: {}", self.negative_prompt)?;
        writeln!(formatter, "clip_skip: {}", self.clip_skip)?;
        writeln!(formatter, "width: {}", self.width)?;
        writeln!(formatter, "height: {}", self.height)?;
        writeln!(formatter, "sample: {}", self.sample)?;
        writeln!(formatter, "strength: {:.2}", self.strength)?;
        writeln!(formatter, "seed: {}", self.seed)?;
        writeln!(formatter, "batch_count: {}", self.batch_count)?;
        writeln!(
            formatter,
            "reference_images: {}",
            self.reference_images.len()
        )?;
        writeln!(formatter, "control_strength: {:.2}", self.control_strength)?;
        writeln!(formatter, "vae_tiling: {}", self.vae_tiling.enabled)?;
        writeln!(formatter, "cache: {}", self.cache.mode)?;
        write!(
            formatter,
            "hires: enabled={}, upscaler={}, scale={:.2}, target={}x{}",
            self.hires.enabled,
            self.hires.upscaler,
            self.hires.scale,
            self.hires.target_width,
            self.hires.target_height,
        )
    }
}

/// Complete video-generation request.
#[derive(Debug, Clone, PartialEq)]
pub struct VideoGenerationParams {
    pub loras: Vec<Lora>,
    pub prompt: String,
    pub negative_prompt: String,
    pub clip_skip: i32,
    pub init_image: Option<RgbImage>,
    pub end_image: Option<RgbImage>,
    pub control_frames: Vec<RgbImage>,
    pub width: i32,
    pub height: i32,
    pub sample: SampleParams,
    pub high_noise_sample: SampleParams,
    pub moe_boundary: f32,
    pub strength: f32,
    pub seed: i64,
    pub video_frames: i32,
    pub fps: i32,
    pub vace_strength: f32,
    pub vae_tiling: TilingParams,
    pub cache: CacheParams,
    pub hires: HiresParams,
    pub circular_x: bool,
    pub circular_y: bool,
}

impl Default for VideoGenerationParams {
    fn default() -> Self {
        let high_noise_sample = SampleParams {
            sample_steps: -1,
            ..SampleParams::default()
        };
        Self {
            loras: Vec::new(),
            prompt: String::new(),
            negative_prompt: String::new(),
            clip_skip: 0,
            init_image: None,
            end_image: None,
            control_frames: Vec::new(),
            width: 512,
            height: 512,
            sample: SampleParams::default(),
            high_noise_sample,
            moe_boundary: 0.875,
            strength: 0.75,
            seed: -1,
            video_frames: 6,
            fps: 16,
            vace_strength: 1.0,
            vae_tiling: TilingParams::default(),
            cache: CacheParams::default(),
            hires: HiresParams::default(),
            circular_x: false,
            circular_y: false,
        }
    }
}

pub(crate) struct NativeVideoGenerationParams {
    pub(crate) raw: sys::sd_vid_gen_params_t,
    _strings: StringPool,
    _loras: Vec<sys::sd_lora_t>,
    _control_frames: Vec<sys::sd_image_t>,
    _sample: NativeSampleParams,
    _high_noise_sample: NativeSampleParams,
    _hires: NativeHiresParams,
}

impl VideoGenerationParams {
    pub(crate) fn to_native(&self) -> Result<NativeVideoGenerationParams> {
        if self.width <= 0 || self.height <= 0 {
            return Err(Error::InvalidParameter {
                name: "video dimensions",
                reason: "width and height must be greater than zero",
            });
        }
        if self.video_frames <= 0 {
            return Err(Error::InvalidParameter {
                name: "video_frames",
                reason: "must be greater than zero",
            });
        }
        if self.fps <= 0 {
            return Err(Error::InvalidParameter {
                name: "fps",
                reason: "must be greater than zero",
            });
        }
        let mut strings = StringPool::default();
        let prompt = strings.add(&self.prompt, "prompt")?;
        let negative_prompt = strings.add(&self.negative_prompt, "negative_prompt")?;
        let loras = build_loras(&self.loras, &mut strings)?;
        let lora_count = u32_len(loras.len(), "LoRAs")?;
        let mut control_frames = raw_rgb_images(&self.control_frames)?;
        let control_frames_size = c_int_len(control_frames.len(), "control frames")?;
        let sample = self.sample.to_native(&mut strings)?;
        let high_noise_sample = self.high_noise_sample.to_native(&mut strings)?;
        let vae_tiling = self.vae_tiling.to_native(&mut strings)?;
        let cache = self.cache.to_native(&mut strings)?;
        let hires = self.hires.to_native(&mut strings)?;

        let raw = sys::sd_vid_gen_params_t {
            loras: ptr_or_null(&loras),
            lora_count,
            prompt,
            negative_prompt,
            clip_skip: self.clip_skip,
            init_image: optional_raw_rgb_image(self.init_image.as_ref())?,
            end_image: optional_raw_rgb_image(self.end_image.as_ref())?,
            control_frames: mut_ptr_or_null(&mut control_frames),
            control_frames_size,
            width: self.width,
            height: self.height,
            sample_params: sample.raw,
            high_noise_sample_params: high_noise_sample.raw,
            moe_boundary: self.moe_boundary,
            strength: self.strength,
            seed: self.seed,
            video_frames: self.video_frames,
            fps: self.fps,
            vace_strength: self.vace_strength,
            vae_tiling_params: vae_tiling,
            cache,
            hires: hires.raw,
            circular_x: self.circular_x,
            circular_y: self.circular_y,
        };
        Ok(NativeVideoGenerationParams {
            raw,
            _strings: strings,
            _loras: loras,
            _control_frames: control_frames,
            _sample: sample,
            _high_noise_sample: high_noise_sample,
            _hires: hires,
        })
    }
}

#[cfg(test)]
mod tests {
    use ::image::{GrayImage, RgbImage};

    use super::{ImageGenerationParams, SampleParams, VideoGenerationParams};
    use crate::{SampleMethod, Scheduler};

    #[test]
    fn defaults_match_upstream_initializers() {
        let sample = SampleParams::default();
        assert_eq!(sample.sample_method, SampleMethod::Auto);
        assert_eq!(sample.scheduler, Scheduler::Auto);
        assert_eq!(sample.sample_steps, 20);
        assert!(sample.eta.is_infinite());

        let image = ImageGenerationParams::default();
        assert_eq!(
            (image.width, image.height, image.batch_count),
            (512, 512, 1)
        );

        let video = VideoGenerationParams::default();
        assert_eq!((video.video_frames, video.fps), (6, 16));
        assert_eq!(video.high_noise_sample.sample_steps, -1);
    }

    #[test]
    fn image_parameters_preserve_native_pixel_formats() {
        let init_image = RgbImage::from_raw(1, 1, vec![1, 2, 3]).unwrap();
        let mask_image = GrayImage::from_raw(1, 1, vec![255]).unwrap();
        let params = ImageGenerationParams {
            init_image: Some(init_image),
            mask_image: Some(mask_image),
            ..ImageGenerationParams::default()
        };

        let native = params.to_native().unwrap();
        assert_eq!(native.raw.init_image.channel, 3);
        assert_eq!(native.raw.mask_image.channel, 1);
        assert_eq!(
            native.raw.init_image.data,
            params
                .init_image
                .as_ref()
                .unwrap()
                .as_raw()
                .as_ptr()
                .cast_mut()
        );
        assert_eq!(
            native.raw.mask_image.data,
            params
                .mask_image
                .as_ref()
                .unwrap()
                .as_raw()
                .as_ptr()
                .cast_mut()
        );
    }
}