15
15
#include " pmid.hpp"
16
16
#include " tae.hpp"
17
17
#include " vae.hpp"
18
+ #include < setjmp.h>
18
19
19
20
#define STB_IMAGE_IMPLEMENTATION
20
21
#define STB_IMAGE_STATIC
@@ -1061,7 +1062,7 @@ SDError new_sd_ctx(sd_ctx_t** sd_ctx,
1061
1062
const char * model_path_c_str,
1062
1063
const char * clip_l_path_c_str,
1063
1064
const char * clip_g_path_c_str,
1064
- const char * t5xxl_path_c_str,
1065
+ const char * t5xxl_path_c_str,
1065
1066
const char * diffusion_model_path_c_str,
1066
1067
const char * vae_path_c_str,
1067
1068
const char * taesd_path_c_str,
@@ -1079,33 +1080,37 @@ SDError new_sd_ctx(sd_ctx_t** sd_ctx,
1079
1080
bool keep_clip_on_cpu,
1080
1081
bool keep_control_net_cpu,
1081
1082
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
+ }
1087
1091
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
+ }
1109
1114
1110
1115
if (!(*sd_ctx)->sd ->load_from_file (model_path,
1111
1116
clip_l_path,
@@ -1124,15 +1129,24 @@ SDError new_sd_ctx(sd_ctx_t** sd_ctx,
1124
1129
keep_vae_on_cpu)) {
1125
1130
delete (*sd_ctx)->sd ;
1126
1131
free (*sd_ctx);
1132
+ ggml_error_jmp_set = 0 ;
1127
1133
return SD_ERROR_PROCESSING;
1128
1134
}
1129
1135
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 ;
1136
1150
return SD_ERROR_PROCESSING;
1137
1151
}
1138
1152
}
@@ -1469,55 +1483,59 @@ SDError txt2img(sd_ctx_t* sd_ctx,
1469
1483
float skip_layer_start,
1470
1484
float skip_layer_end) {
1471
1485
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
+ }
1477
1494
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 ;
1482
1499
}
1483
1500
if (sd_ctx->sd ->version == VERSION_FLUX_DEV || sd_ctx->sd ->version == VERSION_FLUX_SCHNELL) {
1484
1501
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
+ }
1500
1518
1501
- size_t t0 = ggml_time_ms ();
1519
+ size_t t0 = ggml_time_ms ();
1502
1520
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);
1504
1522
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) {
1507
1525
C = 16 ;
1508
1526
} 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 );
1516
1534
} else if (sd_ctx->sd ->version == VERSION_FLUX_DEV || sd_ctx->sd ->version == VERSION_FLUX_SCHNELL) {
1517
1535
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
+ }
1521
1539
1522
1540
*result_images = generate_image (sd_ctx,
1523
1541
work_ctx,
@@ -1543,30 +1561,42 @@ SDError txt2img(sd_ctx_t* sd_ctx,
1543
1561
skip_layer_start,
1544
1562
skip_layer_end);
1545
1563
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
+ }
1550
1569
1551
- *result_count = batch_count;
1570
+ *result_count = batch_count;
1552
1571
1553
- size_t t1 = ggml_time_ms ();
1572
+ size_t t1 = ggml_time_ms ();
1554
1573
1555
- LOG_INFO (" txt2img completed in %.2fs" , (t1 - t0) * 1 .0f / 1000 );
1574
+ LOG_INFO (" txt2img completed in %.2fs" , (t1 - t0) * 1 .0f / 1000 );
1556
1575
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) {
1562
1576
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;
1563
1593
}
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 ());
1567
1596
if (work_ctx) {
1568
1597
ggml_free (work_ctx);
1569
1598
}
1599
+ ggml_error_jmp_set = 0 ;
1570
1600
return SD_ERROR_PROCESSING;
1571
1601
}
1572
1602
}
0 commit comments