@@ -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
26322612void 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
27922771char * 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
28592843struct 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 );
0 commit comments