@@ -1477,8 +1477,7 @@ class StableDiffusionGGML {
14771477 ggml_tensor* denoise_mask = nullptr ,
14781478 ggml_tensor* vace_context = nullptr ,
14791479 float vace_strength = 1 .f,
1480- const sd_easycache_params_t * easycache_params = nullptr ,
1481- const sd_ucache_params_t * ucache_params = nullptr ) {
1480+ const sd_cache_params_t * cache_params = nullptr ) {
14821481 if (shifted_timestep > 0 && !sd_version_is_sdxl (version)) {
14831482 LOG_WARN (" timestep shifting is only supported for SDXL models!" );
14841483 shifted_timestep = 0 ;
@@ -1495,65 +1494,54 @@ class StableDiffusionGGML {
14951494 }
14961495
14971496 EasyCacheState easycache_state;
1497+ UCacheState ucache_state;
14981498 bool easycache_enabled = false ;
1499- if (easycache_params != nullptr && easycache_params->enabled ) {
1500- bool easycache_supported = sd_version_is_dit (version);
1501- if (!easycache_supported) {
1502- LOG_WARN (" EasyCache requested but not supported for this model type" );
1503- } else {
1504- EasyCacheConfig easycache_config;
1505- easycache_config.enabled = true ;
1506- easycache_config.reuse_threshold = std::max (0 .0f , easycache_params->reuse_threshold );
1507- easycache_config.start_percent = easycache_params->start_percent ;
1508- easycache_config.end_percent = easycache_params->end_percent ;
1509- bool percent_valid = easycache_config.start_percent >= 0 .0f &&
1510- easycache_config.start_percent < 1 .0f &&
1511- easycache_config.end_percent > 0 .0f &&
1512- easycache_config.end_percent <= 1 .0f &&
1513- easycache_config.start_percent < easycache_config.end_percent ;
1514- if (!percent_valid) {
1515- LOG_WARN (" EasyCache disabled due to invalid percent range (start=%.3f, end=%.3f)" ,
1516- easycache_config.start_percent ,
1517- easycache_config.end_percent );
1499+ bool ucache_enabled = false ;
1500+
1501+ if (cache_params != nullptr && cache_params->mode != SD_CACHE_DISABLED) {
1502+ bool percent_valid = cache_params->start_percent >= 0 .0f &&
1503+ cache_params->start_percent < 1 .0f &&
1504+ cache_params->end_percent > 0 .0f &&
1505+ cache_params->end_percent <= 1 .0f &&
1506+ cache_params->start_percent < cache_params->end_percent ;
1507+
1508+ if (!percent_valid) {
1509+ LOG_WARN (" Cache disabled due to invalid percent range (start=%.3f, end=%.3f)" ,
1510+ cache_params->start_percent ,
1511+ cache_params->end_percent );
1512+ } else if (cache_params->mode == SD_CACHE_EASYCACHE) {
1513+ bool easycache_supported = sd_version_is_dit (version);
1514+ if (!easycache_supported) {
1515+ LOG_WARN (" EasyCache requested but not supported for this model type" );
15181516 } else {
1517+ EasyCacheConfig easycache_config;
1518+ easycache_config.enabled = true ;
1519+ easycache_config.reuse_threshold = std::max (0 .0f , cache_params->reuse_threshold );
1520+ easycache_config.start_percent = cache_params->start_percent ;
1521+ easycache_config.end_percent = cache_params->end_percent ;
15191522 easycache_state.init (easycache_config, denoiser.get ());
15201523 if (easycache_state.enabled ()) {
15211524 easycache_enabled = true ;
1522- LOG_INFO (" EasyCache enabled - threshold: %.3f, start_percent : %.2f, end_percent : %.2f" ,
1525+ LOG_INFO (" EasyCache enabled - threshold: %.3f, start : %.2f, end : %.2f" ,
15231526 easycache_config.reuse_threshold ,
15241527 easycache_config.start_percent ,
15251528 easycache_config.end_percent );
15261529 } else {
15271530 LOG_WARN (" EasyCache requested but could not be initialized for this run" );
15281531 }
15291532 }
1530- }
1531- }
1532-
1533- UCacheState ucache_state;
1534- bool ucache_enabled = false ;
1535- if (ucache_params != nullptr && ucache_params->enabled ) {
1536- bool ucache_supported = sd_version_is_unet (version);
1537- if (!ucache_supported) {
1538- LOG_WARN (" UCache requested but not supported for this model type (only UNET models)" );
1539- } else {
1540- UCacheConfig ucache_config;
1541- ucache_config.enabled = true ;
1542- ucache_config.reuse_threshold = std::max (0 .0f , ucache_params->reuse_threshold );
1543- ucache_config.start_percent = ucache_params->start_percent ;
1544- ucache_config.end_percent = ucache_params->end_percent ;
1545- ucache_config.error_decay_rate = std::max (0 .0f , std::min (1 .0f , ucache_params->error_decay_rate ));
1546- ucache_config.use_relative_threshold = ucache_params->use_relative_threshold ;
1547- bool percent_valid = ucache_config.start_percent >= 0 .0f &&
1548- ucache_config.start_percent < 1 .0f &&
1549- ucache_config.end_percent > 0 .0f &&
1550- ucache_config.end_percent <= 1 .0f &&
1551- ucache_config.start_percent < ucache_config.end_percent ;
1552- if (!percent_valid) {
1553- LOG_WARN (" UCache disabled due to invalid percent range (start=%.3f, end=%.3f)" ,
1554- ucache_config.start_percent ,
1555- ucache_config.end_percent );
1533+ } else if (cache_params->mode == SD_CACHE_UCACHE) {
1534+ bool ucache_supported = sd_version_is_unet (version);
1535+ if (!ucache_supported) {
1536+ LOG_WARN (" UCache requested but not supported for this model type (only UNET models)" );
15561537 } else {
1538+ UCacheConfig ucache_config;
1539+ ucache_config.enabled = true ;
1540+ ucache_config.reuse_threshold = std::max (0 .0f , cache_params->reuse_threshold );
1541+ ucache_config.start_percent = cache_params->start_percent ;
1542+ ucache_config.end_percent = cache_params->end_percent ;
1543+ ucache_config.error_decay_rate = std::max (0 .0f , std::min (1 .0f , cache_params->error_decay_rate ));
1544+ ucache_config.use_relative_threshold = cache_params->use_relative_threshold ;
15571545 ucache_state.init (ucache_config, denoiser.get ());
15581546 if (ucache_state.enabled ()) {
15591547 ucache_enabled = true ;
@@ -2589,22 +2577,14 @@ enum lora_apply_mode_t str_to_lora_apply_mode(const char* str) {
25892577 return LORA_APPLY_MODE_COUNT;
25902578}
25912579
2592- void sd_easycache_params_init (sd_easycache_params_t * easycache_params) {
2593- *easycache_params = {};
2594- easycache_params->enabled = false ;
2595- easycache_params->reuse_threshold = 0 .2f ;
2596- easycache_params->start_percent = 0 .15f ;
2597- easycache_params->end_percent = 0 .95f ;
2598- }
2599-
2600- void sd_ucache_params_init (sd_ucache_params_t * ucache_params) {
2601- *ucache_params = {};
2602- ucache_params->enabled = false ;
2603- ucache_params->reuse_threshold = 1 .0f ;
2604- ucache_params->start_percent = 0 .15f ;
2605- ucache_params->end_percent = 0 .95f ;
2606- ucache_params->error_decay_rate = 1 .0f ;
2607- ucache_params->use_relative_threshold = true ;
2580+ void sd_cache_params_init (sd_cache_params_t * cache_params) {
2581+ *cache_params = {};
2582+ cache_params->mode = SD_CACHE_DISABLED;
2583+ cache_params->reuse_threshold = 1 .0f ;
2584+ cache_params->start_percent = 0 .15f ;
2585+ cache_params->end_percent = 0 .95f ;
2586+ cache_params->error_decay_rate = 1 .0f ;
2587+ cache_params->use_relative_threshold = true ;
26082588}
26092589
26102590void sd_ctx_params_init (sd_ctx_params_t * sd_ctx_params) {
@@ -2763,8 +2743,7 @@ void sd_img_gen_params_init(sd_img_gen_params_t* sd_img_gen_params) {
27632743 sd_img_gen_params->control_strength = 0 .9f ;
27642744 sd_img_gen_params->pm_params = {nullptr , 0 , nullptr , 20 .f };
27652745 sd_img_gen_params->vae_tiling_params = {false , 0 , 0 , 0 .5f , 0 .0f , 0 .0f };
2766- sd_easycache_params_init (&sd_img_gen_params->easycache );
2767- sd_ucache_params_init (&sd_img_gen_params->ucache );
2746+ sd_cache_params_init (&sd_img_gen_params->cache );
27682747}
27692748
27702749char * sd_img_gen_params_to_str (const sd_img_gen_params_t * sd_img_gen_params) {
@@ -2808,12 +2787,18 @@ char* sd_img_gen_params_to_str(const sd_img_gen_params_t* sd_img_gen_params) {
28082787 sd_img_gen_params->pm_params .id_images_count ,
28092788 SAFE_STR (sd_img_gen_params->pm_params .id_embed_path ),
28102789 BOOL_STR (sd_img_gen_params->vae_tiling_params .enabled ));
2790+ const char * cache_mode_str = " disabled" ;
2791+ if (sd_img_gen_params->cache .mode == SD_CACHE_EASYCACHE) {
2792+ cache_mode_str = " easycache" ;
2793+ } else if (sd_img_gen_params->cache .mode == SD_CACHE_UCACHE) {
2794+ cache_mode_str = " ucache" ;
2795+ }
28112796 snprintf (buf + strlen (buf), 4096 - strlen (buf),
2812- " easycache : %s (threshold=%.3f, start=%.2f, end=%.2f)\n " ,
2813- sd_img_gen_params-> easycache . enabled ? " enabled " : " disabled " ,
2814- sd_img_gen_params->easycache .reuse_threshold ,
2815- sd_img_gen_params->easycache .start_percent ,
2816- sd_img_gen_params->easycache .end_percent );
2797+ " cache : %s (threshold=%.3f, start=%.2f, end=%.2f)\n " ,
2798+ cache_mode_str ,
2799+ sd_img_gen_params->cache .reuse_threshold ,
2800+ sd_img_gen_params->cache .start_percent ,
2801+ sd_img_gen_params->cache .end_percent );
28172802 free (sample_params_str);
28182803 return buf;
28192804}
@@ -2830,8 +2815,7 @@ void sd_vid_gen_params_init(sd_vid_gen_params_t* sd_vid_gen_params) {
28302815 sd_vid_gen_params->video_frames = 6 ;
28312816 sd_vid_gen_params->moe_boundary = 0 .875f ;
28322817 sd_vid_gen_params->vace_strength = 1 .f ;
2833- sd_easycache_params_init (&sd_vid_gen_params->easycache );
2834- sd_ucache_params_init (&sd_vid_gen_params->ucache );
2818+ sd_cache_params_init (&sd_vid_gen_params->cache );
28352819}
28362820
28372821struct sd_ctx_t {
@@ -2909,8 +2893,7 @@ sd_image_t* generate_image_internal(sd_ctx_t* sd_ctx,
29092893 bool increase_ref_index,
29102894 ggml_tensor* concat_latent = nullptr ,
29112895 ggml_tensor* denoise_mask = nullptr ,
2912- const sd_easycache_params_t * easycache_params = nullptr ,
2913- const sd_ucache_params_t * ucache_params = nullptr ) {
2896+ const sd_cache_params_t * cache_params = nullptr ) {
29142897 if (seed < 0 ) {
29152898 // Generally, when using the provided command line, the seed is always >0.
29162899 // However, to prevent potential issues if 'stable-diffusion.cpp' is invoked as a library
@@ -3199,8 +3182,7 @@ sd_image_t* generate_image_internal(sd_ctx_t* sd_ctx,
31993182 denoise_mask,
32003183 nullptr ,
32013184 1 .0f ,
3202- easycache_params,
3203- ucache_params);
3185+ cache_params);
32043186 int64_t sampling_end = ggml_time_ms ();
32053187 if (x_0 != nullptr ) {
32063188 // print_ggml_tensor(x_0);
@@ -3527,8 +3509,7 @@ sd_image_t* generate_image(sd_ctx_t* sd_ctx, const sd_img_gen_params_t* sd_img_g
35273509 sd_img_gen_params->increase_ref_index ,
35283510 concat_latent,
35293511 denoise_mask,
3530- &sd_img_gen_params->easycache ,
3531- &sd_img_gen_params->ucache );
3512+ &sd_img_gen_params->cache );
35323513
35333514 size_t t2 = ggml_time_ms ();
35343515
@@ -3861,8 +3842,7 @@ SD_API sd_image_t* generate_video(sd_ctx_t* sd_ctx, const sd_vid_gen_params_t* s
38613842 denoise_mask,
38623843 vace_context,
38633844 sd_vid_gen_params->vace_strength ,
3864- &sd_vid_gen_params->easycache ,
3865- &sd_vid_gen_params->ucache );
3845+ &sd_vid_gen_params->cache );
38663846
38673847 int64_t sampling_end = ggml_time_ms ();
38683848 LOG_INFO (" sampling(high noise) completed, taking %.2fs" , (sampling_end - sampling_start) * 1 .0f / 1000 );
@@ -3899,8 +3879,7 @@ SD_API sd_image_t* generate_video(sd_ctx_t* sd_ctx, const sd_vid_gen_params_t* s
38993879 denoise_mask,
39003880 vace_context,
39013881 sd_vid_gen_params->vace_strength ,
3902- &sd_vid_gen_params->easycache ,
3903- &sd_vid_gen_params->ucache );
3882+ &sd_vid_gen_params->cache );
39043883
39053884 int64_t sampling_end = ggml_time_ms ();
39063885 LOG_INFO (" sampling completed, taking %.2fs" , (sampling_end - sampling_start) * 1 .0f / 1000 );
0 commit comments