@@ -29,6 +29,13 @@ const char* modes_str[] = {
29
29
" convert" ,
30
30
};
31
31
32
+ const char * previews_str[] = {
33
+ " none" ,
34
+ " proj" ,
35
+ " tae" ,
36
+ " vae" ,
37
+ };
38
+
32
39
enum SDMode {
33
40
TXT2IMG,
34
41
IMG2IMG,
@@ -97,6 +104,11 @@ struct SDParams {
97
104
float slg_scale = 0 .;
98
105
float skip_layer_start = 0.01 ;
99
106
float skip_layer_end = 0.2 ;
107
+
108
+ sd_preview_policy_t preview_method = SD_PREVIEW_NONE;
109
+ int preview_interval = 1 ;
110
+ std::string preview_path = " preview.png" ;
111
+ bool taesd_preview = false ;
100
112
};
101
113
102
114
void print_params (SDParams params) {
@@ -145,23 +157,26 @@ void print_params(SDParams params) {
145
157
printf (" batch_count: %d\n " , params.batch_count );
146
158
printf (" vae_tiling: %s\n " , params.vae_tiling ? " true" : " false" );
147
159
printf (" upscale_repeats: %d\n " , params.upscale_repeats );
160
+ printf (" preview_mode: %d\n " , previews_str[params.preview_method ]);
161
+ printf (" preview_interval: %d\n " , params.preview_interval );
148
162
}
149
163
150
164
void print_usage (int argc, const char * argv[]) {
151
165
printf (" usage: %s [arguments]\n " , argv[0 ]);
152
166
printf (" \n " );
153
167
printf (" arguments:\n " );
154
168
printf (" -h, --help show this help message and exit\n " );
155
- printf (" -M, --mode [MODEL] run mode (txt2img or img2img or convert, default: txt2img)\n " );
169
+ printf (" -M, --mode [MODE] run mode (txt2img or img2img or convert, default: txt2img)\n " );
156
170
printf (" -t, --threads N number of threads to use during computation (default: -1)\n " );
157
171
printf (" If threads <= 0, then threads will be set to the number of CPU physical cores\n " );
158
172
printf (" -m, --model [MODEL] path to full model\n " );
159
- printf (" --diffusion-model path to the standalone diffusion model\n " );
160
- printf (" --clip_l path to the clip-l text encoder\n " );
161
- printf (" --clip_g path to the clip-g text encoder\n " );
162
- printf (" --t5xxl path to the the t5xxl text encoder\n " );
173
+ printf (" --diffusion-model [MODEL] path to the standalone diffusion model\n " );
174
+ printf (" --clip_l [ENCODER] path to the clip-l text encoder\n " );
175
+ printf (" --clip_g [ENCODER] path to the clip-g text encoder\n " );
176
+ printf (" --t5xxl [ENCODER] path to the the t5xxl text encoder\n " );
163
177
printf (" --vae [VAE] path to vae\n " );
164
- printf (" --taesd [TAESD_PATH] path to taesd. Using Tiny AutoEncoder for fast decoding (low quality)\n " );
178
+ printf (" --taesd [TAESD] path to taesd. Using Tiny AutoEncoder for fast decoding (low quality)\n " );
179
+ printf (" --taesd-preview-only prevents usage of taesd for decoding the final image. (for use with --preview %s)\n " , previews_str[SD_PREVIEW_TAE]);
165
180
printf (" --control-net [CONTROL_PATH] path to control net model\n " );
166
181
printf (" --embd-dir [EMBEDDING_PATH] path to embeddings\n " );
167
182
printf (" --stacked-id-embd-dir [DIR] path to PHOTOMAKER stacked id embeddings\n " );
@@ -207,6 +222,10 @@ void print_usage(int argc, const char* argv[]) {
207
222
printf (" This might crash if it is not supported by the backend.\n " );
208
223
printf (" --control-net-cpu keep controlnet in cpu (for low vram)\n " );
209
224
printf (" --canny apply canny preprocessor (edge detection)\n " );
225
+ printf (" --preview {%s,%s,%s,%s} preview method. (default is %s(disabled))\n " , previews_str[0 ], previews_str[1 ], previews_str[2 ], previews_str[3 ], previews_str[SD_PREVIEW_NONE]);
226
+ printf (" %s is the fastest\n " , previews_str[SD_PREVIEW_PROJ]);
227
+ printf (" --preview-interval [N] How often to save the image preview" );
228
+ printf (" --preview-path [PATH} path to write preview image to (default: ./preview.png)\n " );
210
229
printf (" --color Colors the logging tags according to level\n " );
211
230
printf (" -v, --verbose print extra info\n " );
212
231
}
@@ -465,6 +484,8 @@ void parse_args(int argc, const char** argv, SDParams& params) {
465
484
params.diffusion_flash_attn = true ; // can reduce MEM significantly
466
485
} else if (arg == " --canny" ) {
467
486
params.canny_preprocess = true ;
487
+ } else if (arg == " --taesd-preview-only" ) {
488
+ params.taesd_preview = true ;
468
489
} else if (arg == " -b" || arg == " --batch-count" ) {
469
490
if (++i >= argc) {
470
491
invalid_arg = true ;
@@ -587,6 +608,35 @@ void parse_args(int argc, const char** argv, SDParams& params) {
587
608
break ;
588
609
}
589
610
params.skip_layer_end = std::stof (argv[i]);
611
+ } else if (arg == " --preview" ) {
612
+ if (++i >= argc) {
613
+ invalid_arg = true ;
614
+ break ;
615
+ }
616
+ const char * preview = argv[i];
617
+ int preview_method = -1 ;
618
+ for (int m = 0 ; m < N_PREVIEWS; m++) {
619
+ if (!strcmp (preview, previews_str[m])) {
620
+ preview_method = m;
621
+ }
622
+ }
623
+ if (preview_method == -1 ) {
624
+ invalid_arg = true ;
625
+ break ;
626
+ }
627
+ params.preview_method = (sd_preview_policy_t )preview_method;
628
+ } else if (arg == " --preview-interval" ) {
629
+ if (++i >= argc) {
630
+ invalid_arg = true ;
631
+ break ;
632
+ }
633
+ params.preview_interval = std::stoi (argv[i]);
634
+ } else if (arg == " --preview-path" ) {
635
+ if (++i >= argc) {
636
+ invalid_arg = true ;
637
+ break ;
638
+ }
639
+ params.preview_path = argv[i];
590
640
} else {
591
641
fprintf (stderr, " error: unknown argument: %s\n " , arg.c_str ());
592
642
print_usage (argc, argv);
@@ -744,10 +794,17 @@ void sd_log_cb(enum sd_log_level_t level, const char* log, void* data) {
744
794
fflush (out_stream);
745
795
}
746
796
797
+ const char * preview_path;
798
+
799
+ void step_callback (int step, sd_image_t image) {
800
+ stbi_write_png (preview_path, image.width , image.height , image.channel , image.data , 0 );
801
+ }
802
+
747
803
int main (int argc, const char * argv[]) {
748
804
SDParams params;
749
805
750
806
parse_args (argc, argv, params);
807
+ preview_path = params.preview_path .c_str ();
751
808
752
809
sd_set_log_callback (sd_log_cb, (void *)¶ms);
753
810
@@ -857,7 +914,8 @@ int main(int argc, const char* argv[]) {
857
914
params.clip_on_cpu ,
858
915
params.control_net_cpu ,
859
916
params.vae_on_cpu ,
860
- params.diffusion_flash_attn );
917
+ params.diffusion_flash_attn ,
918
+ params.taesd_preview );
861
919
862
920
if (sd_ctx == NULL ) {
863
921
printf (" new_sd_ctx_t failed\n " );
@@ -923,7 +981,10 @@ int main(int argc, const char* argv[]) {
923
981
params.skip_layers .size (),
924
982
params.slg_scale ,
925
983
params.skip_layer_start ,
926
- params.skip_layer_end );
984
+ params.skip_layer_end ,
985
+ params.preview_method ,
986
+ params.preview_interval ,
987
+ (step_callback_t )step_callback);
927
988
} else {
928
989
sd_image_t input_image = {(uint32_t )params.width ,
929
990
(uint32_t )params.height ,
@@ -991,7 +1052,10 @@ int main(int argc, const char* argv[]) {
991
1052
params.skip_layers .size (),
992
1053
params.slg_scale ,
993
1054
params.skip_layer_start ,
994
- params.skip_layer_end );
1055
+ params.skip_layer_end ,
1056
+ params.preview_method ,
1057
+ params.preview_interval ,
1058
+ (step_callback_t )step_callback);
995
1059
}
996
1060
}
997
1061
0 commit comments