Skip to content

Commit 28dd691

Browse files
committed
Handle ggml aborts
1 parent 57ff967 commit 28dd691

File tree

3 files changed

+122
-88
lines changed

3 files changed

+122
-88
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@ test/
1010
*.gguf
1111
output*.png
1212
models*
13-
*.log
13+
*.log
14+
/cmake-build-debug
15+
/.idea
16+
/cmake-build-release

.gitmodules

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
[submodule "ggml"]
22
path = ggml
3-
url = https://github.com/ggerganov/ggml.git
3+
url = https://github.com/SkutteOleg/ggml.git
4+
branch = dreamio

stable-diffusion.cpp

Lines changed: 116 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "pmid.hpp"
1616
#include "tae.hpp"
1717
#include "vae.hpp"
18+
#include <setjmp.h>
1819

1920
#define STB_IMAGE_IMPLEMENTATION
2021
#define STB_IMAGE_STATIC
@@ -1019,7 +1020,7 @@ SDError new_sd_ctx(sd_ctx_t** sd_ctx,
10191020
const char* model_path_c_str,
10201021
const char* clip_l_path_c_str,
10211022
const char* clip_g_path_c_str,
1022-
const char* t5xxl_path_c_str,
1023+
const char* t5xxl_path_c_str,
10231024
const char* diffusion_model_path_c_str,
10241025
const char* vae_path_c_str,
10251026
const char* taesd_path_c_str,
@@ -1037,33 +1038,37 @@ SDError new_sd_ctx(sd_ctx_t** sd_ctx,
10371038
bool keep_clip_on_cpu,
10381039
bool keep_control_net_cpu,
10391040
bool keep_vae_on_cpu) {
1040-
try {
1041-
*sd_ctx = (sd_ctx_t*)malloc(sizeof(sd_ctx_t));
1042-
if (*sd_ctx == NULL) {
1043-
return SD_ERROR_MEMORY_ALLOCATION;
1044-
}
1041+
ggml_error_jmp_set = 1;
1042+
if (setjmp(ggml_error_jmp_buf) == 0) {
1043+
try {
1044+
*sd_ctx = (sd_ctx_t*)malloc(sizeof(sd_ctx_t));
1045+
if (*sd_ctx == NULL) {
1046+
ggml_error_jmp_set = 0;
1047+
return SD_ERROR_MEMORY_ALLOCATION;
1048+
}
10451049

1046-
std::string model_path(model_path_c_str);
1047-
std::string clip_l_path(clip_l_path_c_str);
1048-
std::string clip_g_path(clip_g_path_c_str);
1049-
std::string t5xxl_path(t5xxl_path_c_str);
1050-
std::string diffusion_model_path(diffusion_model_path_c_str);
1051-
std::string vae_path(vae_path_c_str);
1052-
std::string taesd_path(taesd_path_c_str);
1053-
std::string control_net_path(control_net_path_c_str);
1054-
std::string embd_path(embed_dir_c_str);
1055-
std::string id_embd_path(id_embed_dir_c_str);
1056-
std::string lora_model_dir(lora_model_dir_c_str);
1057-
1058-
(*sd_ctx)->sd = new StableDiffusionGGML(n_threads,
1059-
vae_decode_only,
1060-
free_params_immediately,
1061-
lora_model_dir,
1062-
rng_type);
1063-
if ((*sd_ctx)->sd == NULL) {
1064-
free(*sd_ctx);
1065-
return SD_ERROR_MEMORY_ALLOCATION;
1066-
}
1050+
std::string model_path(model_path_c_str);
1051+
std::string clip_l_path(clip_l_path_c_str);
1052+
std::string clip_g_path(clip_g_path_c_str);
1053+
std::string t5xxl_path(t5xxl_path_c_str);
1054+
std::string diffusion_model_path(diffusion_model_path_c_str);
1055+
std::string vae_path(vae_path_c_str);
1056+
std::string taesd_path(taesd_path_c_str);
1057+
std::string control_net_path(control_net_path_c_str);
1058+
std::string embd_path(embed_dir_c_str);
1059+
std::string id_embd_path(id_embed_dir_c_str);
1060+
std::string lora_model_dir(lora_model_dir_c_str);
1061+
1062+
(*sd_ctx)->sd = new StableDiffusionGGML(n_threads,
1063+
vae_decode_only,
1064+
free_params_immediately,
1065+
lora_model_dir,
1066+
rng_type);
1067+
if ((*sd_ctx)->sd == NULL) {
1068+
free(*sd_ctx);
1069+
ggml_error_jmp_set = 0;
1070+
return SD_ERROR_MEMORY_ALLOCATION;
1071+
}
10671072

10681073
if (!(*sd_ctx)->sd->load_from_file(model_path,
10691074
clip_l_path,
@@ -1082,15 +1087,24 @@ SDError new_sd_ctx(sd_ctx_t** sd_ctx,
10821087
keep_vae_on_cpu)) {
10831088
delete (*sd_ctx)->sd;
10841089
free(*sd_ctx);
1090+
ggml_error_jmp_set = 0;
10851091
return SD_ERROR_PROCESSING;
10861092
}
10871093

1088-
return SD_SUCCESS;
1089-
} catch (const std::exception& e) {
1090-
LOG_ERROR("Exception in new_sd_ctx: %s", e.what());
1091-
return SD_ERROR_PROCESSING;
1092-
} catch (...) {
1093-
LOG_ERROR("Unknown exception in new_sd_ctx");
1094+
ggml_error_jmp_set = 0;
1095+
return SD_SUCCESS;
1096+
} catch (const std::exception& e) {
1097+
LOG_ERROR("Exception in new_sd_ctx: %s", e.what());
1098+
ggml_error_jmp_set = 0;
1099+
return SD_ERROR_PROCESSING;
1100+
} catch (...) {
1101+
LOG_ERROR("Unknown exception in new_sd_ctx");
1102+
ggml_error_jmp_set = 0;
1103+
return SD_ERROR_PROCESSING;
1104+
}
1105+
} else {
1106+
LOG_ERROR("GGML error in new_sd_ctx: %s", ggml_get_error_message());
1107+
ggml_error_jmp_set = 0;
10941108
return SD_ERROR_PROCESSING;
10951109
}
10961110
}
@@ -1416,55 +1430,59 @@ SDError txt2img(sd_ctx_t* sd_ctx,
14161430
bool normalize_input,
14171431
const char* input_id_images_path_c_str) {
14181432
struct ggml_context* work_ctx = NULL;
1419-
try {
1420-
LOG_DEBUG("txt2img %dx%d", width, height);
1421-
if (sd_ctx == NULL) {
1422-
return SD_ERROR_INVALID_CONTEXT;
1423-
}
1433+
ggml_error_jmp_set = 1;
1434+
if (setjmp(ggml_error_jmp_buf) == 0) {
1435+
try {
1436+
LOG_DEBUG("txt2img %dx%d", width, height);
1437+
if (sd_ctx == NULL) {
1438+
ggml_error_jmp_set = 0;
1439+
return SD_ERROR_INVALID_CONTEXT;
1440+
}
14241441

1425-
struct ggml_init_params params;
1426-
params.mem_size = static_cast<size_t>(10 * 1024 * 1024); // 10 MB
1427-
if (sd_ctx->sd->version == VERSION_SD3_2B || sd_ctx->sd->version == VERSION_SD3_5_8B) {
1428-
params.mem_size *= 3;
1442+
struct ggml_init_params params;
1443+
params.mem_size = static_cast<size_t>(10 * 1024 * 1024); // 10 MB
1444+
if (sd_ctx->sd->version == VERSION_SD3_2B || sd_ctx->sd->version == VERSION_SD3_5_8B) {
1445+
params.mem_size *= 3;
14291446
}
14301447
if (sd_ctx->sd->version == VERSION_FLUX_DEV || sd_ctx->sd->version == VERSION_FLUX_SCHNELL) {
14311448
params.mem_size *= 4;
1432-
}
1433-
if (sd_ctx->sd->stacked_id) {
1434-
params.mem_size += static_cast<size_t>(10 * 1024 * 1024); // 10 MB
1435-
}
1436-
params.mem_size += width * height * 3 * sizeof(float);
1437-
params.mem_size *= batch_count;
1438-
params.mem_buffer = NULL;
1439-
params.no_alloc = false;
1440-
// LOG_DEBUG("mem_size %u ", params.mem_size);
1441-
1442-
work_ctx = ggml_init(params);
1443-
if (!work_ctx) {
1444-
LOG_ERROR("ggml_init() failed");
1445-
return SD_ERROR_MEMORY_ALLOCATION;
1446-
}
1449+
}
1450+
if (sd_ctx->sd->stacked_id) {
1451+
params.mem_size += static_cast<size_t>(10 * 1024 * 1024); // 10 MB
1452+
}
1453+
params.mem_size += width * height * 3 * sizeof(float);
1454+
params.mem_size *= batch_count;
1455+
params.mem_buffer = NULL;
1456+
params.no_alloc = false;
1457+
// LOG_DEBUG("mem_size %u ", params.mem_size);
1458+
1459+
work_ctx = ggml_init(params);
1460+
if (!work_ctx) {
1461+
LOG_ERROR("ggml_init() failed");
1462+
ggml_error_jmp_set = 0;
1463+
return SD_ERROR_MEMORY_ALLOCATION;
1464+
}
14471465

1448-
size_t t0 = ggml_time_ms();
1466+
size_t t0 = ggml_time_ms();
14491467

1450-
std::vector<float> sigmas = sd_ctx->sd->denoiser->get_sigmas(sample_steps);
1468+
std::vector<float> sigmas = sd_ctx->sd->denoiser->get_sigmas(sample_steps);
14511469

1452-
int C = 4;
1453-
if (sd_ctx->sd->version == VERSION_SD3_2B || sd_ctx->sd->version == VERSION_SD3_5_8B) {
1470+
int C = 4;
1471+
if (sd_ctx->sd->version == VERSION_SD3_2B || sd_ctx->sd->version == VERSION_SD3_5_8B) {
14541472
C = 16;
14551473
} else if (sd_ctx->sd->version == VERSION_FLUX_DEV || sd_ctx->sd->version == VERSION_FLUX_SCHNELL) {
1456-
C = 16;
1457-
}
1458-
int W = width / 8;
1459-
int H = height / 8;
1460-
ggml_tensor* init_latent = ggml_new_tensor_4d(work_ctx, GGML_TYPE_F32, W, H, C, 1);
1461-
if (sd_ctx->sd->version == VERSION_SD3_2B || sd_ctx->sd->version == VERSION_SD3_5_8B) {
1462-
ggml_set_f32(init_latent, 0.0609f);
1474+
C = 16;
1475+
}
1476+
int W = width / 8;
1477+
int H = height / 8;
1478+
ggml_tensor* init_latent = ggml_new_tensor_4d(work_ctx, GGML_TYPE_F32, W, H, C, 1);
1479+
if (sd_ctx->sd->version == VERSION_SD3_2B || sd_ctx->sd->version == VERSION_SD3_5_8B) {
1480+
ggml_set_f32(init_latent, 0.0609f);
14631481
} else if (sd_ctx->sd->version == VERSION_FLUX_DEV || sd_ctx->sd->version == VERSION_FLUX_SCHNELL) {
14641482
ggml_set_f32(init_latent, 0.1159f);
1465-
} else {
1466-
ggml_set_f32(init_latent, 0.f);
1467-
}
1483+
} else {
1484+
ggml_set_f32(init_latent, 0.f);
1485+
}
14681486

14691487
*result_images = generate_image(sd_ctx,
14701488
work_ctx,
@@ -1486,30 +1504,42 @@ SDError txt2img(sd_ctx_t* sd_ctx,
14861504
normalize_input,
14871505
input_id_images_path_c_str);
14881506

1489-
if (*result_images == NULL) {
1490-
ggml_free(work_ctx);
1491-
return SD_ERROR_PROCESSING;
1492-
}
1507+
if (*result_images == NULL) {
1508+
ggml_free(work_ctx);
1509+
ggml_error_jmp_set = 0;
1510+
return SD_ERROR_PROCESSING;
1511+
}
14931512

1494-
*result_count = batch_count;
1513+
*result_count = batch_count;
14951514

1496-
size_t t1 = ggml_time_ms();
1515+
size_t t1 = ggml_time_ms();
14971516

1498-
LOG_INFO("txt2img completed in %.2fs", (t1 - t0) * 1.0f / 1000);
1517+
LOG_INFO("txt2img completed in %.2fs", (t1 - t0) * 1.0f / 1000);
14991518

1500-
ggml_free(work_ctx);
1501-
return SD_SUCCESS;
1502-
} catch (const std::exception& e) {
1503-
LOG_ERROR("Exception in txt2img: %s", e.what());
1504-
if (work_ctx) {
15051519
ggml_free(work_ctx);
1520+
ggml_error_jmp_set = 0;
1521+
return SD_SUCCESS;
1522+
} catch (const std::exception& e) {
1523+
LOG_ERROR("Exception in txt2img: %s", e.what());
1524+
if (work_ctx) {
1525+
ggml_free(work_ctx);
1526+
}
1527+
ggml_error_jmp_set = 0;
1528+
return SD_ERROR_PROCESSING;
1529+
} catch (...) {
1530+
LOG_ERROR("Unknown exception in txt2img");
1531+
if (work_ctx) {
1532+
ggml_free(work_ctx);
1533+
}
1534+
ggml_error_jmp_set = 0;
1535+
return SD_ERROR_PROCESSING;
15061536
}
1507-
return SD_ERROR_PROCESSING;
1508-
} catch (...) {
1509-
LOG_ERROR("Unknown exception in txt2img");
1537+
} else {
1538+
LOG_ERROR("GGML error in txt2img: %s", ggml_get_error_message());
15101539
if (work_ctx) {
15111540
ggml_free(work_ctx);
15121541
}
1542+
ggml_error_jmp_set = 0;
15131543
return SD_ERROR_PROCESSING;
15141544
}
15151545
}

0 commit comments

Comments
 (0)