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
@@ -1061,7 +1062,7 @@ SDError new_sd_ctx(sd_ctx_t** sd_ctx,
10611062 const char * model_path_c_str,
10621063 const char * clip_l_path_c_str,
10631064 const char * clip_g_path_c_str,
1064- const char * t5xxl_path_c_str,
1065+ const char * t5xxl_path_c_str,
10651066 const char * diffusion_model_path_c_str,
10661067 const char * vae_path_c_str,
10671068 const char * taesd_path_c_str,
@@ -1079,33 +1080,37 @@ SDError new_sd_ctx(sd_ctx_t** sd_ctx,
10791080 bool keep_clip_on_cpu,
10801081 bool keep_control_net_cpu,
10811082 bool keep_vae_on_cpu) {
1082- try {
1083- *sd_ctx = (sd_ctx_t *)malloc (sizeof (sd_ctx_t ));
1084- if (*sd_ctx == NULL ) {
1085- return SD_ERROR_MEMORY_ALLOCATION;
1086- }
1083+ ggml_error_jmp_set = 1 ;
1084+ if (setjmp (ggml_error_jmp_buf) == 0 ) {
1085+ try {
1086+ *sd_ctx = (sd_ctx_t *)malloc (sizeof (sd_ctx_t ));
1087+ if (*sd_ctx == NULL ) {
1088+ ggml_error_jmp_set = 0 ;
1089+ return SD_ERROR_MEMORY_ALLOCATION;
1090+ }
10871091
1088- std::string model_path (model_path_c_str);
1089- std::string clip_l_path (clip_l_path_c_str);
1090- std::string clip_g_path (clip_g_path_c_str);
1091- std::string t5xxl_path (t5xxl_path_c_str);
1092- std::string diffusion_model_path (diffusion_model_path_c_str);
1093- std::string vae_path (vae_path_c_str);
1094- std::string taesd_path (taesd_path_c_str);
1095- std::string control_net_path (control_net_path_c_str);
1096- std::string embd_path (embed_dir_c_str);
1097- std::string id_embd_path (id_embed_dir_c_str);
1098- std::string lora_model_dir (lora_model_dir_c_str);
1099-
1100- (*sd_ctx)->sd = new StableDiffusionGGML (n_threads,
1101- vae_decode_only,
1102- free_params_immediately,
1103- lora_model_dir,
1104- rng_type);
1105- if ((*sd_ctx)->sd == NULL ) {
1106- free (*sd_ctx);
1107- return SD_ERROR_MEMORY_ALLOCATION;
1108- }
1092+ std::string model_path (model_path_c_str);
1093+ std::string clip_l_path (clip_l_path_c_str);
1094+ std::string clip_g_path (clip_g_path_c_str);
1095+ std::string t5xxl_path (t5xxl_path_c_str);
1096+ std::string diffusion_model_path (diffusion_model_path_c_str);
1097+ std::string vae_path (vae_path_c_str);
1098+ std::string taesd_path (taesd_path_c_str);
1099+ std::string control_net_path (control_net_path_c_str);
1100+ std::string embd_path (embed_dir_c_str);
1101+ std::string id_embd_path (id_embed_dir_c_str);
1102+ std::string lora_model_dir (lora_model_dir_c_str);
1103+
1104+ (*sd_ctx)->sd = new StableDiffusionGGML (n_threads,
1105+ vae_decode_only,
1106+ free_params_immediately,
1107+ lora_model_dir,
1108+ rng_type);
1109+ if ((*sd_ctx)->sd == NULL ) {
1110+ free (*sd_ctx);
1111+ ggml_error_jmp_set = 0 ;
1112+ return SD_ERROR_MEMORY_ALLOCATION;
1113+ }
11091114
11101115 if (!(*sd_ctx)->sd ->load_from_file (model_path,
11111116 clip_l_path,
@@ -1124,15 +1129,24 @@ SDError new_sd_ctx(sd_ctx_t** sd_ctx,
11241129 keep_vae_on_cpu)) {
11251130 delete (*sd_ctx)->sd ;
11261131 free (*sd_ctx);
1132+ ggml_error_jmp_set = 0 ;
11271133 return SD_ERROR_PROCESSING;
11281134 }
11291135
1130- return SD_SUCCESS;
1131- } catch (const std::exception& e) {
1132- LOG_ERROR (" Exception in new_sd_ctx: %s" , e.what ());
1133- return SD_ERROR_PROCESSING;
1134- } catch (...) {
1135- LOG_ERROR (" Unknown exception in new_sd_ctx" );
1136+ ggml_error_jmp_set = 0 ;
1137+ return SD_SUCCESS;
1138+ } catch (const std::exception& e) {
1139+ LOG_ERROR (" Exception in new_sd_ctx: %s" , e.what ());
1140+ ggml_error_jmp_set = 0 ;
1141+ return SD_ERROR_PROCESSING;
1142+ } catch (...) {
1143+ LOG_ERROR (" Unknown exception in new_sd_ctx" );
1144+ ggml_error_jmp_set = 0 ;
1145+ return SD_ERROR_PROCESSING;
1146+ }
1147+ } else {
1148+ LOG_ERROR (" GGML error in new_sd_ctx: %s" , ggml_get_error_message ());
1149+ ggml_error_jmp_set = 0 ;
11361150 return SD_ERROR_PROCESSING;
11371151 }
11381152}
@@ -1469,55 +1483,59 @@ SDError txt2img(sd_ctx_t* sd_ctx,
14691483 float skip_layer_start,
14701484 float skip_layer_end) {
14711485 struct ggml_context * work_ctx = NULL ;
1472- try {
1473- LOG_DEBUG (" txt2img %dx%d" , width, height);
1474- if (sd_ctx == NULL ) {
1475- return SD_ERROR_INVALID_CONTEXT;
1476- }
1486+ ggml_error_jmp_set = 1 ;
1487+ if (setjmp (ggml_error_jmp_buf) == 0 ) {
1488+ try {
1489+ LOG_DEBUG (" txt2img %dx%d" , width, height);
1490+ if (sd_ctx == NULL ) {
1491+ ggml_error_jmp_set = 0 ;
1492+ return SD_ERROR_INVALID_CONTEXT;
1493+ }
14771494
1478- struct ggml_init_params params;
1479- params.mem_size = static_cast <size_t >(10 * 1024 * 1024 ); // 10 MB
1480- if (sd_ctx->sd ->version == VERSION_SD3_2B || sd_ctx->sd ->version == VERSION_SD3_5_8B || sd_ctx->sd ->version == VERSION_SD3_5_2B) {
1481- params.mem_size *= 3 ;
1495+ struct ggml_init_params params;
1496+ params.mem_size = static_cast <size_t >(10 * 1024 * 1024 ); // 10 MB
1497+ if (sd_ctx->sd ->version == VERSION_SD3_2B || sd_ctx->sd ->version == VERSION_SD3_5_8B || sd_ctx->sd ->version == VERSION_SD3_5_2B) {
1498+ params.mem_size *= 3 ;
14821499 }
14831500 if (sd_ctx->sd ->version == VERSION_FLUX_DEV || sd_ctx->sd ->version == VERSION_FLUX_SCHNELL) {
14841501 params.mem_size *= 4 ;
1485- }
1486- if (sd_ctx->sd ->stacked_id ) {
1487- params.mem_size += static_cast <size_t >(10 * 1024 * 1024 ); // 10 MB
1488- }
1489- params.mem_size += width * height * 3 * sizeof (float );
1490- params.mem_size *= batch_count;
1491- params.mem_buffer = NULL ;
1492- params.no_alloc = false ;
1493- // LOG_DEBUG("mem_size %u ", params.mem_size);
1494-
1495- work_ctx = ggml_init (params);
1496- if (!work_ctx) {
1497- LOG_ERROR (" ggml_init() failed" );
1498- return SD_ERROR_MEMORY_ALLOCATION;
1499- }
1502+ }
1503+ if (sd_ctx->sd ->stacked_id ) {
1504+ params.mem_size += static_cast <size_t >(10 * 1024 * 1024 ); // 10 MB
1505+ }
1506+ params.mem_size += width * height * 3 * sizeof (float );
1507+ params.mem_size *= batch_count;
1508+ params.mem_buffer = NULL ;
1509+ params.no_alloc = false ;
1510+ // LOG_DEBUG("mem_size %u ", params.mem_size);
1511+
1512+ work_ctx = ggml_init (params);
1513+ if (!work_ctx) {
1514+ LOG_ERROR (" ggml_init() failed" );
1515+ ggml_error_jmp_set = 0 ;
1516+ return SD_ERROR_MEMORY_ALLOCATION;
1517+ }
15001518
1501- size_t t0 = ggml_time_ms ();
1519+ size_t t0 = ggml_time_ms ();
15021520
1503- std::vector<float > sigmas = sd_ctx->sd ->denoiser ->get_sigmas (sample_steps);
1521+ std::vector<float > sigmas = sd_ctx->sd ->denoiser ->get_sigmas (sample_steps);
15041522
1505- int C = 4 ;
1506- if (sd_ctx->sd ->version == VERSION_SD3_2B || sd_ctx->sd ->version == VERSION_SD3_5_8B || sd_ctx->sd ->version == VERSION_SD3_5_2B) {
1523+ int C = 4 ;
1524+ if (sd_ctx->sd ->version == VERSION_SD3_2B || sd_ctx->sd ->version == VERSION_SD3_5_8B || sd_ctx->sd ->version == VERSION_SD3_5_2B) {
15071525 C = 16 ;
15081526 } else if (sd_ctx->sd ->version == VERSION_FLUX_DEV || sd_ctx->sd ->version == VERSION_FLUX_SCHNELL) {
1509- C = 16 ;
1510- }
1511- int W = width / 8 ;
1512- int H = height / 8 ;
1513- ggml_tensor* init_latent = ggml_new_tensor_4d (work_ctx, GGML_TYPE_F32, W, H, C, 1 );
1514- if (sd_ctx->sd ->version == VERSION_SD3_2B || sd_ctx->sd ->version == VERSION_SD3_5_8B || sd_ctx->sd ->version == VERSION_SD3_5_2B) {
1515- ggml_set_f32 (init_latent, 0 .0609f );
1527+ C = 16 ;
1528+ }
1529+ int W = width / 8 ;
1530+ int H = height / 8 ;
1531+ ggml_tensor* init_latent = ggml_new_tensor_4d (work_ctx, GGML_TYPE_F32, W, H, C, 1 );
1532+ if (sd_ctx->sd ->version == VERSION_SD3_2B || sd_ctx->sd ->version == VERSION_SD3_5_8B || sd_ctx->sd ->version == VERSION_SD3_5_2B) {
1533+ ggml_set_f32 (init_latent, 0 .0609f );
15161534 } else if (sd_ctx->sd ->version == VERSION_FLUX_DEV || sd_ctx->sd ->version == VERSION_FLUX_SCHNELL) {
15171535 ggml_set_f32 (init_latent, 0 .1159f );
1518- } else {
1519- ggml_set_f32 (init_latent, 0 .f );
1520- }
1536+ } else {
1537+ ggml_set_f32 (init_latent, 0 .f );
1538+ }
15211539
15221540 *result_images = generate_image (sd_ctx,
15231541 work_ctx,
@@ -1543,30 +1561,42 @@ SDError txt2img(sd_ctx_t* sd_ctx,
15431561 skip_layer_start,
15441562 skip_layer_end);
15451563
1546- if (*result_images == NULL ) {
1547- ggml_free (work_ctx);
1548- return SD_ERROR_PROCESSING;
1549- }
1564+ if (*result_images == NULL ) {
1565+ ggml_free (work_ctx);
1566+ ggml_error_jmp_set = 0 ;
1567+ return SD_ERROR_PROCESSING;
1568+ }
15501569
1551- *result_count = batch_count;
1570+ *result_count = batch_count;
15521571
1553- size_t t1 = ggml_time_ms ();
1572+ size_t t1 = ggml_time_ms ();
15541573
1555- LOG_INFO (" txt2img completed in %.2fs" , (t1 - t0) * 1 .0f / 1000 );
1574+ LOG_INFO (" txt2img completed in %.2fs" , (t1 - t0) * 1 .0f / 1000 );
15561575
1557- ggml_free (work_ctx);
1558- return SD_SUCCESS;
1559- } catch (const std::exception& e) {
1560- LOG_ERROR (" Exception in txt2img: %s" , e.what ());
1561- if (work_ctx) {
15621576 ggml_free (work_ctx);
1577+ ggml_error_jmp_set = 0 ;
1578+ return SD_SUCCESS;
1579+ } catch (const std::exception& e) {
1580+ LOG_ERROR (" Exception in txt2img: %s" , e.what ());
1581+ if (work_ctx) {
1582+ ggml_free (work_ctx);
1583+ }
1584+ ggml_error_jmp_set = 0 ;
1585+ return SD_ERROR_PROCESSING;
1586+ } catch (...) {
1587+ LOG_ERROR (" Unknown exception in txt2img" );
1588+ if (work_ctx) {
1589+ ggml_free (work_ctx);
1590+ }
1591+ ggml_error_jmp_set = 0 ;
1592+ return SD_ERROR_PROCESSING;
15631593 }
1564- return SD_ERROR_PROCESSING;
1565- } catch (...) {
1566- LOG_ERROR (" Unknown exception in txt2img" );
1594+ } else {
1595+ LOG_ERROR (" GGML error in txt2img: %s" , ggml_get_error_message ());
15671596 if (work_ctx) {
15681597 ggml_free (work_ctx);
15691598 }
1599+ ggml_error_jmp_set = 0 ;
15701600 return SD_ERROR_PROCESSING;
15711601 }
15721602}
0 commit comments