Skip to content

Commit 4d0338b

Browse files
committed
add recurrent mode
+ fix when -DSD_EXAMPLES_GLOVE_GUI=OFF
1 parent 5f9c118 commit 4d0338b

File tree

5 files changed

+5576
-5249
lines changed

5 files changed

+5576
-5249
lines changed

README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@ This repository is a fork of [stable-diffusion.cpp](https://github.com/leejet/st
1313
```cmake
1414
-DSD_BUILD_EXAMPLES=ON -DSD_EXAMPLES_GLOVE_GUI=ON
1515
```
16-
16+
1717
- Comes with precompiled binaries for Windows requiring no further installation. Compatible with Nvidia graphic card GeForce GTX 950 and after (ie: RTX series are compatible).
1818

1919
## Features
2020

21+
- The model is reloaded only if its parameters are changed
22+
23+
- Possibility to generate images with different prompts on the fly
24+
2125
- Parameters saving as *json*, upon acceptance (*Ok* button):
2226

2327
- The parameters are saved automatically at the execution location
@@ -29,9 +33,9 @@ This repository is a fork of [stable-diffusion.cpp](https://github.com/leejet/st
2933
- Parameters can be loaded
3034
- by using the *Load* button
3135
- or: <code>sd -glove 'path-to-parameters-file'</code>
32-
33-
- Progress :
3436

37+
- Progress :
38+
3539
- Monitoring of progression
3640
- Possibility to cancel
3741

@@ -40,21 +44,24 @@ This repository is a fork of [stable-diffusion.cpp](https://github.com/leejet/st
4044
</p>
4145

4246
- Warning/error messages display
43-
47+
4448
## Misc
4549

4650
- By default, the setup is configured for a desktop application (i.e. : launch using executable icon on desktop).
4751
One can disable this default behaviour by additionaly setting the following CMake command:
52+
4853
```cmake
4954
-DSD_EXAMPLES_GLOVE_GUI_DESKTOP=OFF
5055
```
56+
5157
In this case, the GUI will be accessible using the following CLI argument:
58+
5259
```sh
5360
sd -glove
5461
```
62+
5563
and the terminal will be visible.
5664
In both cases, CLI arguments of SD examples remain operational.
57-
5865

5966
- On Windows, if DLLs are missing, go to the <code>sd</code> executable directory and apply the command:
6067

denoiser.hpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,9 +486,13 @@ static void sample_k_diffusion(sample_method_t method,
486486
struct ggml_tensor* noise = ggml_dup_tensor(work_ctx, x);
487487
struct ggml_tensor* d = ggml_dup_tensor(work_ctx, x);
488488

489+
#ifdef SD_EXAMPLES_GLOVE_GUI
489490
SlvProgressionQt& p = *GlvApp::get_progression("Generating image");
490491
for (p = 0; p << steps; p++) {
491492
int i = p;
493+
#else
494+
for (int i = 0; i < steps; i++) {
495+
#endif
492496
float sigma = sigmas[i];
493497

494498
// denoise
@@ -541,9 +545,13 @@ static void sample_k_diffusion(sample_method_t method,
541545
{
542546
struct ggml_tensor* d = ggml_dup_tensor(work_ctx, x);
543547

548+
#ifdef SD_EXAMPLES_GLOVE_GUI
544549
SlvProgressionQt& p = *GlvApp::get_progression("Generating image");
545550
for (p = 0; p << steps; p++) {
546551
int i = p;
552+
#else
553+
for (int i = 0; i < steps; i++) {
554+
#endif
547555
float sigma = sigmas[i];
548556

549557
// denoise
@@ -576,9 +584,13 @@ static void sample_k_diffusion(sample_method_t method,
576584
struct ggml_tensor* d = ggml_dup_tensor(work_ctx, x);
577585
struct ggml_tensor* x2 = ggml_dup_tensor(work_ctx, x);
578586

587+
#ifdef SD_EXAMPLES_GLOVE_GUI
579588
SlvProgressionQt& p = *GlvApp::get_progression("Generating image");
580589
for (p = 0; p << steps; p++) {
581590
int i = p;
591+
#else
592+
for (int i = 0; i < steps; i++) {
593+
#endif
582594
// denoise
583595
ggml_tensor* denoised = model(x, sigmas[i], -(i + 1));
584596

@@ -628,9 +640,13 @@ static void sample_k_diffusion(sample_method_t method,
628640
struct ggml_tensor* d = ggml_dup_tensor(work_ctx, x);
629641
struct ggml_tensor* x2 = ggml_dup_tensor(work_ctx, x);
630642

643+
#ifdef SD_EXAMPLES_GLOVE_GUI
631644
SlvProgressionQt& p = *GlvApp::get_progression("Generating image");
632645
for (p = 0; p << steps; p++) {
633646
int i = p;
647+
#else
648+
for (int i = 0; i < steps; i++) {
649+
#endif
634650
// denoise
635651
ggml_tensor* denoised = model(x, sigmas[i], i + 1);
636652

@@ -683,9 +699,13 @@ static void sample_k_diffusion(sample_method_t method,
683699
struct ggml_tensor* d = ggml_dup_tensor(work_ctx, x);
684700
struct ggml_tensor* x2 = ggml_dup_tensor(work_ctx, x);
685701

702+
#ifdef SD_EXAMPLES_GLOVE_GUI
686703
SlvProgressionQt& p = *GlvApp::get_progression("Generating image");
687704
for (p = 0; p << steps; p++) {
688705
int i = p;
706+
#else
707+
for (int i = 0; i < steps; i++) {
708+
#endif
689709
// denoise
690710
ggml_tensor* denoised = model(x, sigmas[i], i + 1);
691711

@@ -759,9 +779,13 @@ static void sample_k_diffusion(sample_method_t method,
759779

760780
auto t_fn = [](float sigma) -> float { return -log(sigma); };
761781

782+
#ifdef SD_EXAMPLES_GLOVE_GUI
762783
SlvProgressionQt& p = *GlvApp::get_progression("Generating image");
763784
for (p = 0; p << steps; p++) {
764785
int i = p;
786+
#else
787+
for (int i = 0; i < steps; i++) {
788+
#endif
765789
// denoise
766790
ggml_tensor* denoised = model(x, sigmas[i], i + 1);
767791

@@ -800,9 +824,13 @@ static void sample_k_diffusion(sample_method_t method,
800824

801825
auto t_fn = [](float sigma) -> float { return -log(sigma); };
802826

827+
#ifdef SD_EXAMPLES_GLOVE_GUI
803828
SlvProgressionQt& p = *GlvApp::get_progression("Generating image");
804829
for (p = 0; p << steps; p++) {
805830
int i = p;
831+
#else
832+
for (int i = 0; i < steps; i++) {
833+
#endif
806834
// denoise
807835
ggml_tensor* denoised = model(x, sigmas[i], i + 1);
808836

@@ -845,9 +873,13 @@ static void sample_k_diffusion(sample_method_t method,
845873
ggml_tensor* x_next = x;
846874
std::vector<ggml_tensor*> buffer_model;
847875

876+
#ifdef SD_EXAMPLES_GLOVE_GUI
848877
SlvProgressionQt& p = *GlvApp::get_progression("Generating image");
849878
for (p = 0; p << steps; p++) {
850879
int i = p;
880+
#else
881+
for (int i = 0; i < steps; i++) {
882+
#endif
851883
float sigma = sigmas[i];
852884
float sigma_next = sigmas[i + 1];
853885

@@ -922,9 +954,13 @@ static void sample_k_diffusion(sample_method_t method,
922954
std::vector<ggml_tensor*> buffer_model;
923955
ggml_tensor* x_next = x;
924956

957+
#ifdef SD_EXAMPLES_GLOVE_GUI
925958
SlvProgressionQt& p = *GlvApp::get_progression("Generating image");
926959
for (p = 0; p << steps; p++) {
927960
int i = p;
961+
#else
962+
for (int i = 0; i < steps; i++) {
963+
#endif
928964
float sigma = sigmas[i];
929965
float t_next = sigmas[i + 1];
930966

@@ -997,9 +1033,13 @@ static void sample_k_diffusion(sample_method_t method,
9971033
struct ggml_tensor* noise = ggml_dup_tensor(work_ctx, x);
9981034
struct ggml_tensor* d = ggml_dup_tensor(work_ctx, x);
9991035

1036+
#ifdef SD_EXAMPLES_GLOVE_GUI
10001037
SlvProgressionQt& p = *GlvApp::get_progression("Generating image");
10011038
for (p = 0; p << steps; p++) {
10021039
int i = p;
1040+
#else
1041+
for (int i = 0; i < steps; i++) {
1042+
#endif
10031043
float sigma = sigmas[i];
10041044

10051045
// denoise

examples/cli/main.cpp

Lines changed: 104 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -779,8 +779,33 @@ void sd_log_cb(enum sd_log_level_t level, const char* log, void* data) {
779779
#ifdef SD_EXAMPLES_GLOVE_GUI_DESKTOP
780780
#pragma GLOVE_APP_MSVC_NO_CONSOLE
781781
#endif
782+
struct RecurrentStruct {
783+
sd_ctx_t* sd_ctx = NULL;
784+
GlvSDParams params;
785+
bool model_updated(const GlvSDParams& _params) {
786+
bool l_model_upddated = _params.get_model() != params.get_model();
787+
l_model_upddated |= _params.get_diffusion_model() != params.get_diffusion_model();
788+
l_model_upddated |= _params.get_model_addons() != params.get_model_addons();
789+
l_model_upddated |= _params.get_advanced_params().get_taesd() != params.get_advanced_params().get_taesd();
790+
l_model_upddated |= _params.get_advanced_params().get_control_net() != params.get_advanced_params().get_control_net();
791+
l_model_upddated |= _params.get_advanced_params().get_embd_dir() != params.get_advanced_params().get_embd_dir();
792+
l_model_upddated |= _params.get_photomaker_params().get_stacked_id_embd_dir() != params.get_photomaker_params().get_stacked_id_embd_dir();
793+
l_model_upddated |= _params.get_advanced_params().get_vae_tiling() != params.get_advanced_params().get_vae_tiling();
794+
l_model_upddated |= _params.get_advanced_params().get_threads() != params.get_advanced_params().get_threads();
795+
l_model_upddated |= _params.get_advanced_params().get_type() != params.get_advanced_params().get_type();
796+
l_model_upddated |= _params.get_rng() != params.get_rng();
797+
l_model_upddated |= _params.get_advanced_params().get_schedule() != params.get_advanced_params().get_schedule();
798+
l_model_upddated |= _params.get_advanced_params().get_clip_on_cpu() != params.get_advanced_params().get_clip_on_cpu();
799+
l_model_upddated |= _params.get_advanced_params().get_control_net_cpu() != params.get_advanced_params().get_control_net_cpu();
800+
l_model_upddated |= _params.get_advanced_params().get_vae_on_cpu() != params.get_advanced_params().get_vae_on_cpu();
801+
l_model_upddated |= _params.get_advanced_params().get_diffusion_fa() != params.get_advanced_params().get_diffusion_fa();
802+
return l_model_upddated;
803+
}
804+
};
782805
#endif
783806

807+
808+
784809
int main(int argc, char* argv[]) {
785810

786811
#ifdef SD_EXAMPLES_GLOVE_GUI
@@ -797,10 +822,14 @@ int main(int argc, char* argv[]) {
797822
GlvApp::get_progression("Result");
798823

799824
#ifdef SD_EXAMPLES_GLOVE_GUI_DESKTOP
800-
GLOVE_APP_PARAM_AUTO(GlvSDParams);
801-
#else
802-
GLOVE_APP_PARAM(GlvSDParams);
825+
#define GLOVE_APP_AUTO true
803826
#endif
827+
#define GLOVE_APP_RECURRENT_MODE true
828+
#define GLOVE_APP_RECURRENT_TYPE RecurrentStruct
829+
RecurrentStruct glove_recurrent_var;
830+
831+
GLOVE_APP_PARAM(GlvSDParams);
832+
804833
#endif
805834

806835
SDParams params;
@@ -893,31 +922,53 @@ int main(int argc, char* argv[]) {
893922
}
894923
}
895924

896-
sd_ctx_t* sd_ctx = new_sd_ctx(params.model_path.c_str(),
897-
params.clip_l_path.c_str(),
898-
params.clip_g_path.c_str(),
899-
params.t5xxl_path.c_str(),
900-
params.diffusion_model_path.c_str(),
901-
params.vae_path.c_str(),
902-
params.taesd_path.c_str(),
903-
params.controlnet_path.c_str(),
904-
params.lora_model_dir.c_str(),
905-
params.embeddings_path.c_str(),
906-
params.stacked_id_embeddings_path.c_str(),
907-
vae_decode_only,
908-
params.vae_tiling,
909-
true,
910-
params.n_threads,
911-
params.wtype,
912-
params.rng_type,
913-
params.schedule,
914-
params.clip_on_cpu,
915-
params.control_net_cpu,
916-
params.vae_on_cpu,
917-
params.diffusion_flash_attn);
925+
sd_ctx_t* sd_ctx;
926+
#ifdef SD_EXAMPLES_GLOVE_GUI
927+
if (!is_glove_recurrent || !glove_recurrent_var.sd_ctx || glove_recurrent_var.model_updated(glove_parametrization)) {
928+
if (glove_recurrent_var.sd_ctx) {
929+
free_sd_ctx(glove_recurrent_var.sd_ctx);
930+
}
931+
#endif
932+
sd_ctx = new_sd_ctx(params.model_path.c_str(),
933+
params.clip_l_path.c_str(),
934+
params.clip_g_path.c_str(),
935+
params.t5xxl_path.c_str(),
936+
params.diffusion_model_path.c_str(),
937+
params.vae_path.c_str(),
938+
params.taesd_path.c_str(),
939+
params.controlnet_path.c_str(),
940+
params.lora_model_dir.c_str(),
941+
params.embeddings_path.c_str(),
942+
params.stacked_id_embeddings_path.c_str(),
943+
vae_decode_only,
944+
params.vae_tiling,
945+
#ifdef SD_EXAMPLES_GLOVE_GUI
946+
!is_glove_recurrent,
947+
#else
948+
true,
949+
#endif
950+
params.n_threads,
951+
params.wtype,
952+
params.rng_type,
953+
params.schedule,
954+
params.clip_on_cpu,
955+
params.control_net_cpu,
956+
params.vae_on_cpu,
957+
params.diffusion_flash_attn);
958+
#ifdef SD_EXAMPLES_GLOVE_GUI
959+
if (is_glove_recurrent) {
960+
glove_recurrent_var.sd_ctx = sd_ctx;
961+
}
962+
} else {
963+
sd_ctx = glove_recurrent_var.sd_ctx;
964+
}
965+
#endif
918966

919967
if (sd_ctx == NULL) {
920968
printf("new_sd_ctx_t failed\n");
969+
#ifdef SD_EXAMPLES_GLOVE_GUI
970+
GlvApp::show(SlvStatus(SlvStatus::statusType::warning, "new_sd_ctx_t failed"), true);
971+
#endif
921972
return 1;
922973
}
923974

@@ -992,7 +1043,12 @@ int main(int argc, char* argv[]) {
9921043
params.seed);
9931044
if (results == NULL) {
9941045
printf("generate failed\n");
995-
free_sd_ctx(sd_ctx);
1046+
#ifdef SD_EXAMPLES_GLOVE_GUI
1047+
GlvApp::show(SlvStatus(SlvStatus::statusType::warning, "generate failed"), true);
1048+
if (!is_glove_recurrent)
1049+
#endif
1050+
free_sd_ctx(sd_ctx);
1051+
9961052
return 1;
9971053
}
9981054
size_t last = params.output_path.find_last_of(".");
@@ -1009,7 +1065,10 @@ int main(int argc, char* argv[]) {
10091065
results[i].data = NULL;
10101066
}
10111067
free(results);
1012-
free_sd_ctx(sd_ctx);
1068+
#ifdef SD_EXAMPLES_GLOVE_GUI
1069+
if (!is_glove_recurrent)
1070+
#endif
1071+
free_sd_ctx(sd_ctx);
10131072
return 0;
10141073
} else {
10151074
results = img2img(sd_ctx,
@@ -1041,7 +1100,12 @@ int main(int argc, char* argv[]) {
10411100

10421101
if (results == NULL) {
10431102
printf("generate failed\n");
1044-
free_sd_ctx(sd_ctx);
1103+
#ifdef SD_EXAMPLES_GLOVE_GUI
1104+
GlvApp::show(SlvStatus(SlvStatus::statusType::warning, "generate failed"), true);
1105+
if (!is_glove_recurrent)
1106+
#endif
1107+
free_sd_ctx(sd_ctx);
1108+
10451109
return 1;
10461110
}
10471111

@@ -1074,10 +1138,14 @@ int main(int argc, char* argv[]) {
10741138

10751139
size_t last = params.output_path.find_last_of(".");
10761140
std::string dummy_name = last != std::string::npos ? params.output_path.substr(0, last) : params.output_path;
1141+
#ifdef SD_EXAMPLES_GLOVE_GUI
10771142
GlvApp::get_progression("Result")->set_message("Saving images");
10781143
SlvProgressionQt& p = *GlvApp::get_progression("Result");
10791144
for (p = 0; p << params.batch_count; p++) {
10801145
int i = p;
1146+
#else
1147+
for (int i = 0; i < params.batch_count; i++) {
1148+
#endif
10811149
if (results[i].data == NULL) {
10821150
continue;
10831151
}
@@ -1089,9 +1157,16 @@ int main(int argc, char* argv[]) {
10891157
results[i].data = NULL;
10901158
}
10911159
free(results);
1092-
free_sd_ctx(sd_ctx);
1160+
#ifdef SD_EXAMPLES_GLOVE_GUI
1161+
if (!is_glove_recurrent)
1162+
#endif
1163+
free_sd_ctx(sd_ctx);
10931164
free(control_image_buffer);
10941165
free(input_image_buffer);
10951166

1167+
#ifdef SD_EXAMPLES_GLOVE_GUI
1168+
glove_recurrent_var.params = glove_parametrization;
1169+
#endif
1170+
10961171
return 0;
10971172
}

0 commit comments

Comments
 (0)