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
@@ -1132,32 +1133,36 @@ SDError new_sd_ctx(sd_ctx_t** sd_ctx,
11321133 bool keep_control_net_cpu,
11331134 bool keep_vae_on_cpu,
11341135 bool diffusion_flash_attn) {
1135- try {
1136- *sd_ctx = (sd_ctx_t *)malloc (sizeof (sd_ctx_t ));
1137- if (*sd_ctx == NULL ) {
1138- return SD_ERROR_MEMORY_ALLOCATION;
1139- }
1140- std::string model_path (model_path_c_str);
1141- std::string clip_l_path (clip_l_path_c_str);
1142- std::string clip_g_path (clip_g_path_c_str);
1143- std::string t5xxl_path (t5xxl_path_c_str);
1144- std::string diffusion_model_path (diffusion_model_path_c_str);
1145- std::string vae_path (vae_path_c_str);
1146- std::string taesd_path (taesd_path_c_str);
1147- std::string control_net_path (control_net_path_c_str);
1148- std::string embd_path (embed_dir_c_str);
1149- std::string id_embd_path (id_embed_dir_c_str);
1150- std::string lora_model_dir (lora_model_dir_c_str);
1151-
1152- (*sd_ctx)->sd = new StableDiffusionGGML (n_threads,
1153- vae_decode_only,
1154- free_params_immediately,
1155- lora_model_dir,
1156- rng_type);
1157- if ((*sd_ctx)->sd == NULL ) {
1158- free (*sd_ctx);
1159- return SD_ERROR_MEMORY_ALLOCATION;
1160- }
1136+ ggml_error_jmp_set = 1 ;
1137+ if (setjmp (ggml_error_jmp_buf) == 0 ) {
1138+ try {
1139+ *sd_ctx = (sd_ctx_t *)malloc (sizeof (sd_ctx_t ));
1140+ if (*sd_ctx == NULL ) {
1141+ ggml_error_jmp_set = 0 ;
1142+ return SD_ERROR_MEMORY_ALLOCATION;
1143+ }
1144+ std::string model_path (model_path_c_str);
1145+ std::string clip_l_path (clip_l_path_c_str);
1146+ std::string clip_g_path (clip_g_path_c_str);
1147+ std::string t5xxl_path (t5xxl_path_c_str);
1148+ std::string diffusion_model_path (diffusion_model_path_c_str);
1149+ std::string vae_path (vae_path_c_str);
1150+ std::string taesd_path (taesd_path_c_str);
1151+ std::string control_net_path (control_net_path_c_str);
1152+ std::string embd_path (embed_dir_c_str);
1153+ std::string id_embd_path (id_embed_dir_c_str);
1154+ std::string lora_model_dir (lora_model_dir_c_str);
1155+
1156+ (*sd_ctx)->sd = new StableDiffusionGGML (n_threads,
1157+ vae_decode_only,
1158+ free_params_immediately,
1159+ lora_model_dir,
1160+ rng_type);
1161+ if ((*sd_ctx)->sd == NULL ) {
1162+ free (*sd_ctx);
1163+ ggml_error_jmp_set = 0 ;
1164+ return SD_ERROR_MEMORY_ALLOCATION;
1165+ }
11611166
11621167 if (!(*sd_ctx)->sd ->load_from_file (model_path,
11631168 clip_l_path,
@@ -1177,15 +1182,24 @@ SDError new_sd_ctx(sd_ctx_t** sd_ctx,
11771182 diffusion_flash_attn)) {
11781183 delete (*sd_ctx)->sd ;
11791184 free (*sd_ctx);
1185+ ggml_error_jmp_set = 0 ;
11801186 return SD_ERROR_PROCESSING;
11811187 }
11821188
1183- return SD_SUCCESS;
1184- } catch (const std::exception& e) {
1185- LOG_ERROR (" Exception in new_sd_ctx: %s" , e.what ());
1186- return SD_ERROR_PROCESSING;
1187- } catch (...) {
1188- LOG_ERROR (" Unknown exception in new_sd_ctx" );
1189+ ggml_error_jmp_set = 0 ;
1190+ return SD_SUCCESS;
1191+ } catch (const std::exception& e) {
1192+ LOG_ERROR (" Exception in new_sd_ctx: %s" , e.what ());
1193+ ggml_error_jmp_set = 0 ;
1194+ return SD_ERROR_PROCESSING;
1195+ } catch (...) {
1196+ LOG_ERROR (" Unknown exception in new_sd_ctx" );
1197+ ggml_error_jmp_set = 0 ;
1198+ return SD_ERROR_PROCESSING;
1199+ }
1200+ } else {
1201+ LOG_ERROR (" GGML error in new_sd_ctx: %s" , ggml_get_error_message ());
1202+ ggml_error_jmp_set = 0 ;
11891203 return SD_ERROR_PROCESSING;
11901204 }
11911205}
@@ -1567,55 +1581,59 @@ SDError txt2img(sd_ctx_t* sd_ctx,
15671581 float skip_layer_end= 0.2 ) {
15681582 std::vector<int > skip_layers_vec (skip_layers, skip_layers + skip_layers_count);
15691583 struct ggml_context * work_ctx = NULL ;
1570- try {
1571- LOG_DEBUG (" txt2img %dx%d" , width, height);
1572- if (sd_ctx == NULL ) {
1573- return SD_ERROR_INVALID_CONTEXT;
1574- }
1575-
1576- struct ggml_init_params params;
1577- params.mem_size = static_cast <size_t >(10 * 1024 * 1024 ); // 10 MB
1578- if (sd_version_is_sd3 (sd_ctx->sd ->version )) {
1579- params.mem_size *= 3 ;
1584+ ggml_error_jmp_set = 1 ;
1585+ if (setjmp (ggml_error_jmp_buf) == 0 ) {
1586+ try {
1587+ LOG_DEBUG (" txt2img %dx%d" , width, height);
1588+ if (sd_ctx == NULL ) {
1589+ ggml_error_jmp_set = 0 ;
1590+ return SD_ERROR_INVALID_CONTEXT;
1591+ }
1592+
1593+ struct ggml_init_params params;
1594+ params.mem_size = static_cast <size_t >(10 * 1024 * 1024 ); // 10 MB
1595+ if (sd_version_is_sd3 (sd_ctx->sd ->version )) {
1596+ params.mem_size *= 3 ;
15801597 }
15811598 if (sd_version_is_flux (sd_ctx->sd ->version )) {
15821599 params.mem_size *= 4 ;
1583- }
1584- if (sd_ctx->sd ->stacked_id ) {
1585- params.mem_size += static_cast <size_t >(10 * 1024 * 1024 ); // 10 MB
1586- }
1587- params.mem_size += width * height * 3 * sizeof (float );
1588- params.mem_size *= batch_count;
1589- params.mem_buffer = NULL ;
1590- params.no_alloc = false ;
1591- // LOG_DEBUG("mem_size %u ", params.mem_size);
1600+ }
1601+ if (sd_ctx->sd ->stacked_id ) {
1602+ params.mem_size += static_cast <size_t >(10 * 1024 * 1024 ); // 10 MB
1603+ }
1604+ params.mem_size += width * height * 3 * sizeof (float );
1605+ params.mem_size *= batch_count;
1606+ params.mem_buffer = NULL ;
1607+ params.no_alloc = false ;
1608+ // LOG_DEBUG("mem_size %u ", params.mem_size);
15921609
1593- work_ctx = ggml_init (params);
1594- if (!work_ctx) {
1595- LOG_ERROR (" ggml_init() failed" );
1596- return SD_ERROR_MEMORY_ALLOCATION;
1597- }
1610+ work_ctx = ggml_init (params);
1611+ if (!work_ctx) {
1612+ LOG_ERROR (" ggml_init() failed" );
1613+ ggml_error_jmp_set = 0 ;
1614+ return SD_ERROR_MEMORY_ALLOCATION;
1615+ }
15981616
1599- size_t t0 = ggml_time_ms ();
1617+ size_t t0 = ggml_time_ms ();
16001618
1601- std::vector<float > sigmas = sd_ctx->sd ->denoiser ->get_sigmas (sample_steps);
1619+ std::vector<float > sigmas = sd_ctx->sd ->denoiser ->get_sigmas (sample_steps);
16021620
1603- int C = 4 ;
1604- if (sd_version_is_sd3 (sd_ctx->sd ->version )) {
1621+ int C = 4 ;
1622+ if (sd_version_is_sd3 (sd_ctx->sd ->version )) {
16051623 C = 16 ;
16061624 } else if (sd_version_is_flux (sd_ctx->sd ->version )) {
1607- C = 16 ;
1608- }
1609- int W = width / 8 ;
1610- int H = height / 8 ;
1611- ggml_tensor* init_latent = ggml_new_tensor_4d (work_ctx, GGML_TYPE_F32, W, H, C, 1 );
1612- if (sd_version_is_sd3 (sd_ctx->sd ->version )) {
1613- ggml_set_f32 (init_latent, 0 .0609f );
1625+ C = 16 ;
1626+ }
1627+ int W = width / 8 ;
1628+ int H = height / 8 ;
1629+ ggml_tensor* init_latent = ggml_new_tensor_4d (work_ctx, GGML_TYPE_F32, W, H, C, 1 );
1630+ if (sd_version_is_sd3 (sd_ctx->sd ->version )) {
1631+ ggml_set_f32 (init_latent, 0 .0609f );
16141632 } else if (sd_version_is_flux (sd_ctx->sd ->version )) {
16151633 ggml_set_f32 (init_latent, 0 .1159f );
1616- } else {
1617- ggml_set_f32 (init_latent, 0 .f );
1618- }
1634+ } else {
1635+ ggml_set_f32 (init_latent, 0 .f );
1636+ }
16191637
16201638 if (sd_version_is_inpaint (sd_ctx->sd ->version )) {
16211639 LOG_WARN (" This is an inpainting model, this should only be used in img2img mode with a mask" );
@@ -1645,30 +1663,42 @@ SDError txt2img(sd_ctx_t* sd_ctx,
16451663 skip_layer_start,
16461664 skip_layer_end);
16471665
1648- if (*result_images == NULL ) {
1649- ggml_free (work_ctx);
1650- return SD_ERROR_PROCESSING;
1651- }
1666+ if (*result_images == NULL ) {
1667+ ggml_free (work_ctx);
1668+ ggml_error_jmp_set = 0 ;
1669+ return SD_ERROR_PROCESSING;
1670+ }
16521671
1653- *result_count = batch_count;
1672+ *result_count = batch_count;
16541673
1655- size_t t1 = ggml_time_ms ();
1674+ size_t t1 = ggml_time_ms ();
16561675
1657- LOG_INFO (" txt2img completed in %.2fs" , (t1 - t0) * 1 .0f / 1000 );
1676+ LOG_INFO (" txt2img completed in %.2fs" , (t1 - t0) * 1 .0f / 1000 );
16581677
1659- ggml_free (work_ctx);
1660- return SD_SUCCESS;
1661- } catch (const std::exception& e) {
1662- LOG_ERROR (" Exception in txt2img: %s" , e.what ());
1663- if (work_ctx) {
16641678 ggml_free (work_ctx);
1679+ ggml_error_jmp_set = 0 ;
1680+ return SD_SUCCESS;
1681+ } catch (const std::exception& e) {
1682+ LOG_ERROR (" Exception in txt2img: %s" , e.what ());
1683+ if (work_ctx) {
1684+ ggml_free (work_ctx);
1685+ }
1686+ ggml_error_jmp_set = 0 ;
1687+ return SD_ERROR_PROCESSING;
1688+ } catch (...) {
1689+ LOG_ERROR (" Unknown exception in txt2img" );
1690+ if (work_ctx) {
1691+ ggml_free (work_ctx);
1692+ }
1693+ ggml_error_jmp_set = 0 ;
1694+ return SD_ERROR_PROCESSING;
16651695 }
1666- return SD_ERROR_PROCESSING;
1667- } catch (...) {
1668- LOG_ERROR (" Unknown exception in txt2img" );
1696+ } else {
1697+ LOG_ERROR (" GGML error in txt2img: %s" , ggml_get_error_message ());
16691698 if (work_ctx) {
16701699 ggml_free (work_ctx);
16711700 }
1701+ ggml_error_jmp_set = 0 ;
16721702 return SD_ERROR_PROCESSING;
16731703 }
16741704}
0 commit comments