Skip to content

Commit ae45868

Browse files
committed
Send a callback every step, but the preview image only at the defined interval. -1 preview interval to disable image previews
1 parent fe246c8 commit ae45868

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

stable-diffusion.cpp

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,32 +1026,36 @@ class StableDiffusionGGML {
10261026
result_cb(number, image_data, result_cb_data);
10271027
}
10281028

1029-
sd_result_step_cb_t result_step_cb = nullptr;
1030-
int result_step_cb_interval = 1;
1031-
void* result_step_cb_data = nullptr;
1029+
sd_result_step_cb_t result_step_cb = nullptr;
1030+
int result_step_cb_preview_interval = -1;
1031+
void* result_step_cb_data = nullptr;
10321032

10331033
void send_result_step_callback(ggml_context* task_work_ctx, ggml_tensor* x, size_t number, size_t step) {
1034-
if (result_step_cb == nullptr || step % result_step_cb_interval != 0) {
1034+
if (result_step_cb == nullptr) {
10351035
return;
10361036
}
10371037

1038-
struct ggml_init_params params {};
1039-
params.mem_size = ggml_get_mem_size(task_work_ctx);
1040-
params.mem_buffer = nullptr;
1041-
params.no_alloc = false;
1038+
if (result_step_cb_preview_interval != -1 && step % result_step_cb_preview_interval == 0) {
1039+
struct ggml_init_params params {};
1040+
params.mem_size = ggml_get_mem_size(task_work_ctx);
1041+
params.mem_buffer = nullptr;
1042+
params.no_alloc = false;
10421043

1043-
struct ggml_context* work_ctx = ggml_init(params);
1044-
if (!work_ctx) {
1045-
return;
1046-
}
1044+
struct ggml_context* work_ctx = ggml_init(params);
1045+
if (!work_ctx) {
1046+
return;
1047+
}
10471048

1048-
struct ggml_tensor* result = ggml_dup_tensor(work_ctx, x);
1049-
copy_ggml_tensor(result, x);
1049+
struct ggml_tensor* result = ggml_dup_tensor(work_ctx, x);
1050+
copy_ggml_tensor(result, x);
10501051

1051-
struct ggml_tensor* img = decode_first_stage(work_ctx, result);
1052-
result_step_cb(number, step, sd_tensor_to_image(img), result_step_cb_data);
1052+
struct ggml_tensor* img = decode_first_stage(work_ctx, result);
1053+
result_step_cb(number, step, sd_tensor_to_image(img), result_step_cb_data);
10531054

1054-
ggml_free(work_ctx);
1055+
ggml_free(work_ctx);
1056+
} else {
1057+
result_step_cb(number, step, nullptr, result_step_cb_data);
1058+
}
10551059
}
10561060
};
10571061

@@ -1140,13 +1144,13 @@ void free_sd_ctx(sd_ctx_t* sd_ctx) {
11401144
}
11411145

11421146
void sd_ctx_set_result_callback(sd_ctx_t* sd_ctx, sd_result_cb_t cb, void* data) {
1143-
sd_ctx->sd->result_cb = cb;
1147+
sd_ctx->sd->result_cb = cb;
11441148
sd_ctx->sd->result_cb_data = data;
11451149
}
11461150

1147-
void sd_ctx_set_result_step_callback(sd_ctx_t* sd_ctx, sd_result_step_cb_t cb, int cb_interval, void* data) {
1148-
sd_ctx->sd->result_step_cb = cb;
1149-
sd_ctx->sd->result_step_cb_interval = cb_interval;
1151+
void sd_ctx_set_result_step_callback(sd_ctx_t* sd_ctx, sd_result_step_cb_t cb, int cb_preview_interval, void* data) {
1152+
sd_ctx->sd->result_step_cb = cb;
1153+
sd_ctx->sd->result_step_cb_preview_interval = cb_preview_interval;
11501154
sd_ctx->sd->result_step_cb_data = data;
11511155
}
11521156

stable-diffusion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ SD_API sd_ctx_t* new_sd_ctx(const char* model_path,
148148

149149
SD_API void free_sd_ctx(sd_ctx_t* sd_ctx);
150150
SD_API void sd_ctx_set_result_callback(sd_ctx_t* sd_ctx, sd_result_cb_t cb, void* data);
151-
SD_API void sd_ctx_set_result_step_callback(sd_ctx_t* sd_ctx, sd_result_step_cb_t cb, int cb_interval, void* data);
151+
SD_API void sd_ctx_set_result_step_callback(sd_ctx_t* sd_ctx, sd_result_step_cb_t cb, int cb_preview_interval, void* data);
152152

153153
SD_API sd_image_t* txt2img(sd_ctx_t* sd_ctx,
154154
const char* prompt,

0 commit comments

Comments
 (0)