Skip to content

Commit c59f414

Browse files
committed
use single unified struct
1 parent 148bfdf commit c59f414

File tree

3 files changed

+89
-125
lines changed

3 files changed

+89
-125
lines changed

examples/cli/main.cpp

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,8 +1056,7 @@ struct SDGenerationParams {
10561056

10571057
std::string cache_mode;
10581058
std::string cache_option;
1059-
sd_easycache_params_t easycache_params;
1060-
sd_ucache_params_t ucache_params;
1059+
sd_cache_params_t cache_params;
10611060

10621061
float moe_boundary = 0.875f;
10631062
int video_frames = 1;
@@ -1548,8 +1547,7 @@ struct SDGenerationParams {
15481547
return false;
15491548
}
15501549

1551-
easycache_params.enabled = false;
1552-
ucache_params.enabled = false;
1550+
cache_params.mode = SD_CACHE_DISABLED;
15531551

15541552
if (!cache_mode.empty()) {
15551553
std::string option_str = cache_option;
@@ -1610,18 +1608,15 @@ struct SDGenerationParams {
16101608
return false;
16111609
}
16121610

1611+
cache_params.reuse_threshold = values[0];
1612+
cache_params.start_percent = values[1];
1613+
cache_params.end_percent = values[2];
1614+
cache_params.error_decay_rate = values[3];
1615+
cache_params.use_relative_threshold = (values[4] != 0.0f);
16131616
if (cache_mode == "easycache") {
1614-
easycache_params.enabled = true;
1615-
easycache_params.reuse_threshold = values[0];
1616-
easycache_params.start_percent = values[1];
1617-
easycache_params.end_percent = values[2];
1617+
cache_params.mode = SD_CACHE_EASYCACHE;
16181618
} else {
1619-
ucache_params.enabled = true;
1620-
ucache_params.reuse_threshold = values[0];
1621-
ucache_params.start_percent = values[1];
1622-
ucache_params.end_percent = values[2];
1623-
ucache_params.error_decay_rate = values[3];
1624-
ucache_params.use_relative_threshold = (values[4] != 0.0f);
1619+
cache_params.mode = SD_CACHE_UCACHE;
16251620
}
16261621
}
16271622

@@ -1719,16 +1714,12 @@ struct SDGenerationParams {
17191714
<< " high_noise_sample_params: " << high_noise_sample_params_str << ",\n"
17201715
<< " cache_mode: \"" << cache_mode << "\",\n"
17211716
<< " cache_option: \"" << cache_option << "\",\n"
1722-
<< " easycache: "
1723-
<< (easycache_params.enabled ? "enabled" : "disabled")
1724-
<< " (threshold=" << easycache_params.reuse_threshold
1725-
<< ", start=" << easycache_params.start_percent
1726-
<< ", end=" << easycache_params.end_percent << "),\n"
1727-
<< " ucache: "
1728-
<< (ucache_params.enabled ? "enabled" : "disabled")
1729-
<< " (threshold=" << ucache_params.reuse_threshold
1730-
<< ", start=" << ucache_params.start_percent
1731-
<< ", end=" << ucache_params.end_percent << "),\n"
1717+
<< " cache: "
1718+
<< (cache_params.mode == SD_CACHE_DISABLED ? "disabled" :
1719+
(cache_params.mode == SD_CACHE_EASYCACHE ? "easycache" : "ucache"))
1720+
<< " (threshold=" << cache_params.reuse_threshold
1721+
<< ", start=" << cache_params.start_percent
1722+
<< ", end=" << cache_params.end_percent << "),\n"
17321723
<< " moe_boundary: " << moe_boundary << ",\n"
17331724
<< " video_frames: " << video_frames << ",\n"
17341725
<< " fps: " << fps << ",\n"
@@ -2253,8 +2244,7 @@ int main(int argc, const char* argv[]) {
22532244
gen_params.pm_style_strength,
22542245
}, // pm_params
22552246
ctx_params.vae_tiling_params,
2256-
gen_params.easycache_params,
2257-
gen_params.ucache_params,
2247+
gen_params.cache_params,
22582248
};
22592249

22602250
results = generate_image(sd_ctx, &img_gen_params);
@@ -2279,8 +2269,7 @@ int main(int argc, const char* argv[]) {
22792269
gen_params.seed,
22802270
gen_params.video_frames,
22812271
gen_params.vace_strength,
2282-
gen_params.easycache_params,
2283-
gen_params.ucache_params,
2272+
gen_params.cache_params,
22842273
};
22852274

22862275
results = generate_video(sd_ctx, &vid_gen_params, &num_results);

stable-diffusion.cpp

Lines changed: 62 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,8 +1487,7 @@ class StableDiffusionGGML {
14871487
ggml_tensor* denoise_mask = nullptr,
14881488
ggml_tensor* vace_context = nullptr,
14891489
float vace_strength = 1.f,
1490-
const sd_easycache_params_t* easycache_params = nullptr,
1491-
const sd_ucache_params_t* ucache_params = nullptr) {
1490+
const sd_cache_params_t* cache_params = nullptr) {
14921491
if (shifted_timestep > 0 && !sd_version_is_sdxl(version)) {
14931492
LOG_WARN("timestep shifting is only supported for SDXL models!");
14941493
shifted_timestep = 0;
@@ -1505,65 +1504,54 @@ class StableDiffusionGGML {
15051504
}
15061505

15071506
EasyCacheState easycache_state;
1507+
UCacheState ucache_state;
15081508
bool easycache_enabled = false;
1509-
if (easycache_params != nullptr && easycache_params->enabled) {
1510-
bool easycache_supported = sd_version_is_dit(version);
1511-
if (!easycache_supported) {
1512-
LOG_WARN("EasyCache requested but not supported for this model type");
1513-
} else {
1514-
EasyCacheConfig easycache_config;
1515-
easycache_config.enabled = true;
1516-
easycache_config.reuse_threshold = std::max(0.0f, easycache_params->reuse_threshold);
1517-
easycache_config.start_percent = easycache_params->start_percent;
1518-
easycache_config.end_percent = easycache_params->end_percent;
1519-
bool percent_valid = easycache_config.start_percent >= 0.0f &&
1520-
easycache_config.start_percent < 1.0f &&
1521-
easycache_config.end_percent > 0.0f &&
1522-
easycache_config.end_percent <= 1.0f &&
1523-
easycache_config.start_percent < easycache_config.end_percent;
1524-
if (!percent_valid) {
1525-
LOG_WARN("EasyCache disabled due to invalid percent range (start=%.3f, end=%.3f)",
1526-
easycache_config.start_percent,
1527-
easycache_config.end_percent);
1509+
bool ucache_enabled = false;
1510+
1511+
if (cache_params != nullptr && cache_params->mode != SD_CACHE_DISABLED) {
1512+
bool percent_valid = cache_params->start_percent >= 0.0f &&
1513+
cache_params->start_percent < 1.0f &&
1514+
cache_params->end_percent > 0.0f &&
1515+
cache_params->end_percent <= 1.0f &&
1516+
cache_params->start_percent < cache_params->end_percent;
1517+
1518+
if (!percent_valid) {
1519+
LOG_WARN("Cache disabled due to invalid percent range (start=%.3f, end=%.3f)",
1520+
cache_params->start_percent,
1521+
cache_params->end_percent);
1522+
} else if (cache_params->mode == SD_CACHE_EASYCACHE) {
1523+
bool easycache_supported = sd_version_is_dit(version);
1524+
if (!easycache_supported) {
1525+
LOG_WARN("EasyCache requested but not supported for this model type");
15281526
} else {
1527+
EasyCacheConfig easycache_config;
1528+
easycache_config.enabled = true;
1529+
easycache_config.reuse_threshold = std::max(0.0f, cache_params->reuse_threshold);
1530+
easycache_config.start_percent = cache_params->start_percent;
1531+
easycache_config.end_percent = cache_params->end_percent;
15291532
easycache_state.init(easycache_config, denoiser.get());
15301533
if (easycache_state.enabled()) {
15311534
easycache_enabled = true;
1532-
LOG_INFO("EasyCache enabled - threshold: %.3f, start_percent: %.2f, end_percent: %.2f",
1535+
LOG_INFO("EasyCache enabled - threshold: %.3f, start: %.2f, end: %.2f",
15331536
easycache_config.reuse_threshold,
15341537
easycache_config.start_percent,
15351538
easycache_config.end_percent);
15361539
} else {
15371540
LOG_WARN("EasyCache requested but could not be initialized for this run");
15381541
}
15391542
}
1540-
}
1541-
}
1542-
1543-
UCacheState ucache_state;
1544-
bool ucache_enabled = false;
1545-
if (ucache_params != nullptr && ucache_params->enabled) {
1546-
bool ucache_supported = sd_version_is_unet(version);
1547-
if (!ucache_supported) {
1548-
LOG_WARN("UCache requested but not supported for this model type (only UNET models)");
1549-
} else {
1550-
UCacheConfig ucache_config;
1551-
ucache_config.enabled = true;
1552-
ucache_config.reuse_threshold = std::max(0.0f, ucache_params->reuse_threshold);
1553-
ucache_config.start_percent = ucache_params->start_percent;
1554-
ucache_config.end_percent = ucache_params->end_percent;
1555-
ucache_config.error_decay_rate = std::max(0.0f, std::min(1.0f, ucache_params->error_decay_rate));
1556-
ucache_config.use_relative_threshold = ucache_params->use_relative_threshold;
1557-
bool percent_valid = ucache_config.start_percent >= 0.0f &&
1558-
ucache_config.start_percent < 1.0f &&
1559-
ucache_config.end_percent > 0.0f &&
1560-
ucache_config.end_percent <= 1.0f &&
1561-
ucache_config.start_percent < ucache_config.end_percent;
1562-
if (!percent_valid) {
1563-
LOG_WARN("UCache disabled due to invalid percent range (start=%.3f, end=%.3f)",
1564-
ucache_config.start_percent,
1565-
ucache_config.end_percent);
1543+
} else if (cache_params->mode == SD_CACHE_UCACHE) {
1544+
bool ucache_supported = sd_version_is_unet(version);
1545+
if (!ucache_supported) {
1546+
LOG_WARN("UCache requested but not supported for this model type (only UNET models)");
15661547
} else {
1548+
UCacheConfig ucache_config;
1549+
ucache_config.enabled = true;
1550+
ucache_config.reuse_threshold = std::max(0.0f, cache_params->reuse_threshold);
1551+
ucache_config.start_percent = cache_params->start_percent;
1552+
ucache_config.end_percent = cache_params->end_percent;
1553+
ucache_config.error_decay_rate = std::max(0.0f, std::min(1.0f, cache_params->error_decay_rate));
1554+
ucache_config.use_relative_threshold = cache_params->use_relative_threshold;
15671555
ucache_state.init(ucache_config, denoiser.get());
15681556
if (ucache_state.enabled()) {
15691557
ucache_enabled = true;
@@ -2611,22 +2599,14 @@ enum lora_apply_mode_t str_to_lora_apply_mode(const char* str) {
26112599
return LORA_APPLY_MODE_COUNT;
26122600
}
26132601

2614-
void sd_easycache_params_init(sd_easycache_params_t* easycache_params) {
2615-
*easycache_params = {};
2616-
easycache_params->enabled = false;
2617-
easycache_params->reuse_threshold = 0.2f;
2618-
easycache_params->start_percent = 0.15f;
2619-
easycache_params->end_percent = 0.95f;
2620-
}
2621-
2622-
void sd_ucache_params_init(sd_ucache_params_t* ucache_params) {
2623-
*ucache_params = {};
2624-
ucache_params->enabled = false;
2625-
ucache_params->reuse_threshold = 1.0f;
2626-
ucache_params->start_percent = 0.15f;
2627-
ucache_params->end_percent = 0.95f;
2628-
ucache_params->error_decay_rate = 1.0f;
2629-
ucache_params->use_relative_threshold = true;
2602+
void sd_cache_params_init(sd_cache_params_t* cache_params) {
2603+
*cache_params = {};
2604+
cache_params->mode = SD_CACHE_DISABLED;
2605+
cache_params->reuse_threshold = 1.0f;
2606+
cache_params->start_percent = 0.15f;
2607+
cache_params->end_percent = 0.95f;
2608+
cache_params->error_decay_rate = 1.0f;
2609+
cache_params->use_relative_threshold = true;
26302610
}
26312611

26322612
void sd_ctx_params_init(sd_ctx_params_t* sd_ctx_params) {
@@ -2785,8 +2765,7 @@ void sd_img_gen_params_init(sd_img_gen_params_t* sd_img_gen_params) {
27852765
sd_img_gen_params->control_strength = 0.9f;
27862766
sd_img_gen_params->pm_params = {nullptr, 0, nullptr, 20.f};
27872767
sd_img_gen_params->vae_tiling_params = {false, 0, 0, 0.5f, 0.0f, 0.0f};
2788-
sd_easycache_params_init(&sd_img_gen_params->easycache);
2789-
sd_ucache_params_init(&sd_img_gen_params->ucache);
2768+
sd_cache_params_init(&sd_img_gen_params->cache);
27902769
}
27912770

27922771
char* sd_img_gen_params_to_str(const sd_img_gen_params_t* sd_img_gen_params) {
@@ -2830,12 +2809,18 @@ char* sd_img_gen_params_to_str(const sd_img_gen_params_t* sd_img_gen_params) {
28302809
sd_img_gen_params->pm_params.id_images_count,
28312810
SAFE_STR(sd_img_gen_params->pm_params.id_embed_path),
28322811
BOOL_STR(sd_img_gen_params->vae_tiling_params.enabled));
2812+
const char* cache_mode_str = "disabled";
2813+
if (sd_img_gen_params->cache.mode == SD_CACHE_EASYCACHE) {
2814+
cache_mode_str = "easycache";
2815+
} else if (sd_img_gen_params->cache.mode == SD_CACHE_UCACHE) {
2816+
cache_mode_str = "ucache";
2817+
}
28332818
snprintf(buf + strlen(buf), 4096 - strlen(buf),
2834-
"easycache: %s (threshold=%.3f, start=%.2f, end=%.2f)\n",
2835-
sd_img_gen_params->easycache.enabled ? "enabled" : "disabled",
2836-
sd_img_gen_params->easycache.reuse_threshold,
2837-
sd_img_gen_params->easycache.start_percent,
2838-
sd_img_gen_params->easycache.end_percent);
2819+
"cache: %s (threshold=%.3f, start=%.2f, end=%.2f)\n",
2820+
cache_mode_str,
2821+
sd_img_gen_params->cache.reuse_threshold,
2822+
sd_img_gen_params->cache.start_percent,
2823+
sd_img_gen_params->cache.end_percent);
28392824
free(sample_params_str);
28402825
return buf;
28412826
}
@@ -2852,8 +2837,7 @@ void sd_vid_gen_params_init(sd_vid_gen_params_t* sd_vid_gen_params) {
28522837
sd_vid_gen_params->video_frames = 6;
28532838
sd_vid_gen_params->moe_boundary = 0.875f;
28542839
sd_vid_gen_params->vace_strength = 1.f;
2855-
sd_easycache_params_init(&sd_vid_gen_params->easycache);
2856-
sd_ucache_params_init(&sd_vid_gen_params->ucache);
2840+
sd_cache_params_init(&sd_vid_gen_params->cache);
28572841
}
28582842

28592843
struct sd_ctx_t {
@@ -2931,8 +2915,7 @@ sd_image_t* generate_image_internal(sd_ctx_t* sd_ctx,
29312915
bool increase_ref_index,
29322916
ggml_tensor* concat_latent = nullptr,
29332917
ggml_tensor* denoise_mask = nullptr,
2934-
const sd_easycache_params_t* easycache_params = nullptr,
2935-
const sd_ucache_params_t* ucache_params = nullptr) {
2918+
const sd_cache_params_t* cache_params = nullptr) {
29362919
if (seed < 0) {
29372920
// Generally, when using the provided command line, the seed is always >0.
29382921
// However, to prevent potential issues if 'stable-diffusion.cpp' is invoked as a library
@@ -3221,8 +3204,7 @@ sd_image_t* generate_image_internal(sd_ctx_t* sd_ctx,
32213204
denoise_mask,
32223205
nullptr,
32233206
1.0f,
3224-
easycache_params,
3225-
ucache_params);
3207+
cache_params);
32263208
int64_t sampling_end = ggml_time_ms();
32273209
if (x_0 != nullptr) {
32283210
// print_ggml_tensor(x_0);
@@ -3556,8 +3538,7 @@ sd_image_t* generate_image(sd_ctx_t* sd_ctx, const sd_img_gen_params_t* sd_img_g
35563538
sd_img_gen_params->increase_ref_index,
35573539
concat_latent,
35583540
denoise_mask,
3559-
&sd_img_gen_params->easycache,
3560-
&sd_img_gen_params->ucache);
3541+
&sd_img_gen_params->cache);
35613542

35623543
size_t t2 = ggml_time_ms();
35633544

@@ -3924,8 +3905,7 @@ SD_API sd_image_t* generate_video(sd_ctx_t* sd_ctx, const sd_vid_gen_params_t* s
39243905
denoise_mask,
39253906
vace_context,
39263907
sd_vid_gen_params->vace_strength,
3927-
&sd_vid_gen_params->easycache,
3928-
&sd_vid_gen_params->ucache);
3908+
&sd_vid_gen_params->cache);
39293909

39303910
int64_t sampling_end = ggml_time_ms();
39313911
LOG_INFO("sampling(high noise) completed, taking %.2fs", (sampling_end - sampling_start) * 1.0f / 1000);
@@ -3962,8 +3942,7 @@ SD_API sd_image_t* generate_video(sd_ctx_t* sd_ctx, const sd_vid_gen_params_t* s
39623942
denoise_mask,
39633943
vace_context,
39643944
sd_vid_gen_params->vace_strength,
3965-
&sd_vid_gen_params->easycache,
3966-
&sd_vid_gen_params->ucache);
3945+
&sd_vid_gen_params->cache);
39673946

39683947
int64_t sampling_end = ggml_time_ms();
39693948
LOG_INFO("sampling completed, taking %.2fs", (sampling_end - sampling_start) * 1.0f / 1000);

stable-diffusion.h

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -235,21 +235,20 @@ typedef struct {
235235
float style_strength;
236236
} sd_pm_params_t; // photo maker
237237

238-
typedef struct {
239-
bool enabled;
240-
float reuse_threshold;
241-
float start_percent;
242-
float end_percent;
243-
} sd_easycache_params_t;
238+
enum sd_cache_mode_t {
239+
SD_CACHE_DISABLED = 0,
240+
SD_CACHE_EASYCACHE,
241+
SD_CACHE_UCACHE,
242+
};
244243

245244
typedef struct {
246-
bool enabled;
245+
enum sd_cache_mode_t mode;
247246
float reuse_threshold;
248247
float start_percent;
249248
float end_percent;
250249
float error_decay_rate;
251250
bool use_relative_threshold;
252-
} sd_ucache_params_t;
251+
} sd_cache_params_t;
253252

254253
typedef struct {
255254
bool is_high_noise;
@@ -279,8 +278,7 @@ typedef struct {
279278
float control_strength;
280279
sd_pm_params_t pm_params;
281280
sd_tiling_params_t vae_tiling_params;
282-
sd_easycache_params_t easycache;
283-
sd_ucache_params_t ucache;
281+
sd_cache_params_t cache;
284282
} sd_img_gen_params_t;
285283

286284
typedef struct {
@@ -302,8 +300,7 @@ typedef struct {
302300
int64_t seed;
303301
int video_frames;
304302
float vace_strength;
305-
sd_easycache_params_t easycache;
306-
sd_ucache_params_t ucache;
303+
sd_cache_params_t cache;
307304
} sd_vid_gen_params_t;
308305

309306
typedef struct sd_ctx_t sd_ctx_t;
@@ -333,8 +330,7 @@ SD_API enum preview_t str_to_preview(const char* str);
333330
SD_API const char* sd_lora_apply_mode_name(enum lora_apply_mode_t mode);
334331
SD_API enum lora_apply_mode_t str_to_lora_apply_mode(const char* str);
335332

336-
SD_API void sd_easycache_params_init(sd_easycache_params_t* easycache_params);
337-
SD_API void sd_ucache_params_init(sd_ucache_params_t* ucache_params);
333+
SD_API void sd_cache_params_init(sd_cache_params_t* cache_params);
338334

339335
SD_API void sd_ctx_params_init(sd_ctx_params_t* sd_ctx_params);
340336
SD_API char* sd_ctx_params_to_str(const sd_ctx_params_t* sd_ctx_params);

0 commit comments

Comments
 (0)