11#include " stable-diffusion.h"
2- #include < cmath>
32#include < cstdint>
43#define GGML_MAX_NAME 128
54
2221#define STB_IMAGE_RESIZE_IMPLEMENTATION
2322#define STB_IMAGE_RESIZE_STATIC
2423#include " stb_image_resize.h"
25- #include < stdlib.h>
2624
2725// Names of the sampler method, same order as enum sample_method in stable-diffusion.h
2826const char * sample_method_str[] = {
@@ -57,68 +55,6 @@ const char* schedulers[] = {
5755
5856static_assert (std::size(schedulers) == SCHEDULER_COUNT , " schedulers mismatch" );
5957
60- // New enum string arrays
61- const char * rng_type_str[] = {
62- " std_default" ,
63- " cuda" ,
64- " cpu" ,
65- };
66- static_assert (std::size(rng_type_str) == RNG_TYPE_COUNT , " rng type mismatch" );
67-
68- const char * prediction_str[] = {
69- " default" ,
70- " epsilon" ,
71- " v" ,
72- " edm_v" ,
73- " sd3_flow" ,
74- " flux_flow" ,
75- " flux2_flow" ,
76- };
77- static_assert (std::size(prediction_str) == PREDICTION_COUNT , " prediction mismatch" );
78-
79- const char * lora_apply_mode_str[] = {
80- " auto" ,
81- " immediately" ,
82- " at_runtime" ,
83- };
84- static_assert (std::size(lora_apply_mode_str) == LORA_APPLY_MODE_COUNT , " lora apply mode mismatch" );
85-
86- const char * sd_type_str[] = {
87- [0 ] = " f32" ,
88- [1 ] = " f16" ,
89- [2 ] = " q4_0" ,
90- [3 ] = " q4_1" ,
91- [6 ] = " q5_0" ,
92- [7 ] = " q5_1" ,
93- [8 ] = " q8_0" ,
94- [9 ] = " q8_1" ,
95- [10 ] = " q2_k" ,
96- [11 ] = " q3_k" ,
97- [12 ] = " q4_k" ,
98- [13 ] = " q5_k" ,
99- [14 ] = " q6_k" ,
100- [15 ] = " q8_k" ,
101- [16 ] = " iq2_xxs" ,
102- [17 ] = " iq2_xs" ,
103- [18 ] = " iq3_xxs" ,
104- [19 ] = " iq1_s" ,
105- [20 ] = " iq4_nl" ,
106- [21 ] = " iq3_s" ,
107- [22 ] = " iq2_s" ,
108- [23 ] = " iq4_xs" ,
109- [24 ] = " i8" ,
110- [25 ] = " i16" ,
111- [26 ] = " i32" ,
112- [27 ] = " i64" ,
113- [28 ] = " f64" ,
114- [29 ] = " iq1_m" ,
115- [30 ] = " bf16" ,
116- [34 ] = " tq1_0" ,
117- [35 ] = " tq2_0" ,
118- [39 ] = " mxfp4" ,
119- };
120- static_assert (std::size(sd_type_str) == SD_TYPE_COUNT , " sd type mismatch" );
121-
12258sd_ctx_t * sd_c;
12359// Moved from the context (load time) to generation time params
12460scheduler_t scheduler = SCHEDULER_COUNT ;
@@ -174,41 +110,9 @@ int load_model(const char *model, char *model_path, char* options[], int threads
174110 const char *vae_path = " " ;
175111 const char *scheduler_str = " " ;
176112 const char *sampler = " " ;
177- const char *clip_vision_path = " " ;
178- const char *llm_path = " " ;
179- const char *llm_vision_path = " " ;
180- const char *diffusion_model_path = stableDiffusionModel;
181- const char *high_noise_diffusion_model_path = " " ;
182- const char *taesd_path = " " ;
183- const char *control_net_path = " " ;
184- const char *embedding_dir = " " ;
185- const char *photo_maker_path = " " ;
186- const char *tensor_type_rules = " " ;
187113 char *lora_dir = model_path;
188114 bool lora_dir_allocated = false ;
189115
190- bool vae_decode_only = true ;
191- bool free_params_immediately = true ;
192- int n_threads = threads;
193- enum sd_type_t wtype = SD_TYPE_COUNT ;
194- enum rng_type_t rng_type = STD_DEFAULT_RNG ;
195- enum rng_type_t sampler_rng_type = RNG_TYPE_COUNT ;
196- enum prediction_t prediction = PREDICTION_COUNT ;
197- enum lora_apply_mode_t lora_apply_mode = LORA_APPLY_MODE_COUNT ;
198- bool offload_params_to_cpu = false ;
199- bool keep_clip_on_cpu = false ;
200- bool keep_control_net_on_cpu = false ;
201- bool keep_vae_on_cpu = false ;
202- bool diffusion_flash_attn = false ;
203- bool tae_preview_only = false ;
204- bool diffusion_conv_direct = false ;
205- bool vae_conv_direct = false ;
206- bool force_sdxl_vae_conv_scale = false ;
207- bool chroma_use_dit_mask = true ;
208- bool chroma_use_t5_mask = false ;
209- int chroma_t5_mask_pad = 0 ;
210- float flow_shift = INFINITY ;
211-
212116 fprintf (stderr, " parsing options: %p\n " , options);
213117
214118 // If options is not NULL, parse options
@@ -252,113 +156,6 @@ int load_model(const char *model, char *model_path, char* options[], int threads
252156 fprintf (stderr, " No model path provided, using lora dir as-is: %s\n " , lora_dir);
253157 }
254158 }
255-
256- // New parsing
257- if (!strcmp (optname, " clip_vision_path" )) clip_vision_path = optval;
258- if (!strcmp (optname, " llm_path" )) llm_path = optval;
259- if (!strcmp (optname, " llm_vision_path" )) llm_vision_path = optval;
260- if (!strcmp (optname, " diffusion_model_path" )) diffusion_model_path = optval;
261- if (!strcmp (optname, " high_noise_diffusion_model_path" )) high_noise_diffusion_model_path = optval;
262- if (!strcmp (optname, " taesd_path" )) taesd_path = optval;
263- if (!strcmp (optname, " control_net_path" )) control_net_path = optval;
264- if (!strcmp (optname, " embedding_dir" )) embedding_dir = optval;
265- if (!strcmp (optname, " photo_maker_path" )) photo_maker_path = optval;
266- if (!strcmp (optname, " tensor_type_rules" )) tensor_type_rules = optval;
267-
268- if (!strcmp (optname, " vae_decode_only" )) vae_decode_only = (strcmp (optval, " true" ) == 0 || strcmp (optval, " 1" ) == 0 );
269- if (!strcmp (optname, " free_params_immediately" )) free_params_immediately = (strcmp (optval, " true" ) == 0 || strcmp (optval, " 1" ) == 0 );
270- if (!strcmp (optname, " offload_params_to_cpu" )) offload_params_to_cpu = (strcmp (optval, " true" ) == 0 || strcmp (optval, " 1" ) == 0 );
271- if (!strcmp (optname, " keep_clip_on_cpu" )) keep_clip_on_cpu = (strcmp (optval, " true" ) == 0 || strcmp (optval, " 1" ) == 0 );
272- if (!strcmp (optname, " keep_control_net_on_cpu" )) keep_control_net_on_cpu = (strcmp (optval, " true" ) == 0 || strcmp (optval, " 1" ) == 0 );
273- if (!strcmp (optname, " keep_vae_on_cpu" )) keep_vae_on_cpu = (strcmp (optval, " true" ) == 0 || strcmp (optval, " 1" ) == 0 );
274- if (!strcmp (optname, " diffusion_flash_attn" )) diffusion_flash_attn = (strcmp (optval, " true" ) == 0 || strcmp (optval, " 1" ) == 0 );
275- if (!strcmp (optname, " tae_preview_only" )) tae_preview_only = (strcmp (optval, " true" ) == 0 || strcmp (optval, " 1" ) == 0 );
276- if (!strcmp (optname, " diffusion_conv_direct" )) diffusion_conv_direct = (strcmp (optval, " true" ) == 0 || strcmp (optval, " 1" ) == 0 );
277- if (!strcmp (optname, " vae_conv_direct" )) vae_conv_direct = (strcmp (optval, " true" ) == 0 || strcmp (optval, " 1" ) == 0 );
278- if (!strcmp (optname, " force_sdxl_vae_conv_scale" )) force_sdxl_vae_conv_scale = (strcmp (optval, " true" ) == 0 || strcmp (optval, " 1" ) == 0 );
279- if (!strcmp (optname, " chroma_use_dit_mask" )) chroma_use_dit_mask = (strcmp (optval, " true" ) == 0 || strcmp (optval, " 1" ) == 0 );
280- if (!strcmp (optname, " chroma_use_t5_mask" )) chroma_use_t5_mask = (strcmp (optval, " true" ) == 0 || strcmp (optval, " 1" ) == 0 );
281-
282- if (!strcmp (optname, " n_threads" )) n_threads = atoi (optval);
283- if (!strcmp (optname, " chroma_t5_mask_pad" )) chroma_t5_mask_pad = atoi (optval);
284-
285- if (!strcmp (optname, " flow_shift" )) flow_shift = atof (optval);
286-
287- if (!strcmp (optname, " rng_type" )) {
288- int found = -1 ;
289- for (int m = 0 ; m < RNG_TYPE_COUNT ; m++) {
290- if (!strcmp (optval, rng_type_str[m])) {
291- found = m;
292- break ;
293- }
294- }
295- if (found != -1 ) {
296- rng_type = (rng_type_t )found;
297- fprintf (stderr, " Found rng_type: %s\n " , optval);
298- } else {
299- fprintf (stderr, " Invalid rng_type: %s, using default\n " , optval);
300- }
301- }
302- if (!strcmp (optname, " sampler_rng_type" )) {
303- int found = -1 ;
304- for (int m = 0 ; m < RNG_TYPE_COUNT ; m++) {
305- if (!strcmp (optval, rng_type_str[m])) {
306- found = m;
307- break ;
308- }
309- }
310- if (found != -1 ) {
311- sampler_rng_type = (rng_type_t )found;
312- fprintf (stderr, " Found sampler_rng_type: %s\n " , optval);
313- } else {
314- fprintf (stderr, " Invalid sampler_rng_type: %s, using default\n " , optval);
315- }
316- }
317- if (!strcmp (optname, " prediction" )) {
318- int found = -1 ;
319- for (int m = 0 ; m < PREDICTION_COUNT ; m++) {
320- if (!strcmp (optval, prediction_str[m])) {
321- found = m;
322- break ;
323- }
324- }
325- if (found != -1 ) {
326- prediction = (prediction_t )found;
327- fprintf (stderr, " Found prediction: %s\n " , optval);
328- } else {
329- fprintf (stderr, " Invalid prediction: %s, using default\n " , optval);
330- }
331- }
332- if (!strcmp (optname, " lora_apply_mode" )) {
333- int found = -1 ;
334- for (int m = 0 ; m < LORA_APPLY_MODE_COUNT ; m++) {
335- if (!strcmp (optval, lora_apply_mode_str[m])) {
336- found = m;
337- break ;
338- }
339- }
340- if (found != -1 ) {
341- lora_apply_mode = (lora_apply_mode_t )found;
342- fprintf (stderr, " Found lora_apply_mode: %s\n " , optval);
343- } else {
344- fprintf (stderr, " Invalid lora_apply_mode: %s, using default\n " , optval);
345- }
346- }
347- if (!strcmp (optname, " wtype" )) {
348- int found = -1 ;
349- for (int m = 0 ; m < SD_TYPE_COUNT ; m++) {
350- if (sd_type_str[m] && !strcmp (optval, sd_type_str[m])) {
351- found = m;
352- break ;
353- }
354- }
355- if (found != -1 ) {
356- wtype = (sd_type_t )found;
357- fprintf (stderr, " Found wtype: %s\n " , optval);
358- } else {
359- fprintf (stderr, " Invalid wtype: %s, using default\n " , optval);
360- }
361- }
362159 }
363160
364161 fprintf (stderr, " parsed options\n " );
@@ -369,40 +166,17 @@ int load_model(const char *model, char *model_path, char* options[], int threads
369166 ctx_params.model_path = model;
370167 ctx_params.clip_l_path = clip_l_path;
371168 ctx_params.clip_g_path = clip_g_path;
372- ctx_params.clip_vision_path = clip_vision_path;
373169 ctx_params.t5xxl_path = t5xxl_path;
374- ctx_params.llm_path = llm_path;
375- ctx_params.llm_vision_path = llm_vision_path;
376- ctx_params.diffusion_model_path = diffusion_model_path;
377- ctx_params.high_noise_diffusion_model_path = high_noise_diffusion_model_path;
170+ ctx_params.diffusion_model_path = stableDiffusionModel;
378171 ctx_params.vae_path = vae_path;
379- ctx_params.taesd_path = taesd_path ;
380- ctx_params.control_net_path = control_net_path ;
172+ ctx_params.taesd_path = " " ;
173+ ctx_params.control_net_path = " " ;
381174 ctx_params.lora_model_dir = lora_dir;
382- ctx_params.embedding_dir = embedding_dir;
383- ctx_params.photo_maker_path = photo_maker_path;
384- ctx_params.tensor_type_rules = tensor_type_rules;
385- ctx_params.vae_decode_only = vae_decode_only;
386- ctx_params.free_params_immediately = free_params_immediately;
387- ctx_params.n_threads = n_threads;
388- ctx_params.rng_type = rng_type;
389- ctx_params.keep_clip_on_cpu = keep_clip_on_cpu;
390- if (wtype != SD_TYPE_COUNT ) ctx_params.wtype = wtype;
391- if (sampler_rng_type != RNG_TYPE_COUNT ) ctx_params.sampler_rng_type = sampler_rng_type;
392- if (prediction != PREDICTION_COUNT ) ctx_params.prediction = prediction;
393- if (lora_apply_mode != LORA_APPLY_MODE_COUNT ) ctx_params.lora_apply_mode = lora_apply_mode;
394- ctx_params.offload_params_to_cpu = offload_params_to_cpu;
395- ctx_params.keep_control_net_on_cpu = keep_control_net_on_cpu;
396- ctx_params.keep_vae_on_cpu = keep_vae_on_cpu;
397- ctx_params.diffusion_flash_attn = diffusion_flash_attn;
398- ctx_params.tae_preview_only = tae_preview_only;
399- ctx_params.diffusion_conv_direct = diffusion_conv_direct;
400- ctx_params.vae_conv_direct = vae_conv_direct;
401- ctx_params.force_sdxl_vae_conv_scale = force_sdxl_vae_conv_scale;
402- ctx_params.chroma_use_dit_mask = chroma_use_dit_mask;
403- ctx_params.chroma_use_t5_mask = chroma_use_t5_mask;
404- ctx_params.chroma_t5_mask_pad = chroma_t5_mask_pad;
405- ctx_params.flow_shift = flow_shift;
175+ ctx_params.embedding_dir = " " ;
176+ ctx_params.vae_decode_only = false ;
177+ ctx_params.free_params_immediately = false ;
178+ ctx_params.n_threads = threads;
179+ ctx_params.rng_type = STD_DEFAULT_RNG ;
406180 sd_ctx_t * sd_ctx = new_sd_ctx (&ctx_params);
407181
408182 if (sd_ctx == NULL ) {
0 commit comments