Skip to content

Commit 630ee03

Browse files
authored
refactor: move all cache parameter defaults to the library (#1327)
1 parent f6968bc commit 630ee03

6 files changed

Lines changed: 24 additions & 154 deletions

File tree

docs/caching.md

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Uses Taylor series approximation to predict block outputs:
8080
Combines DBCache and TaylorSeer:
8181

8282
```bash
83-
--cache-mode cache-dit --cache-preset fast
83+
--cache-mode cache-dit
8484
```
8585

8686
#### Parameters
@@ -92,14 +92,6 @@ Combines DBCache and TaylorSeer:
9292
| `threshold` | L1 residual difference threshold | 0.08 |
9393
| `warmup` | Steps before caching starts | 8 |
9494

95-
#### Presets
96-
97-
Available presets: `slow`, `medium`, `fast`, `ultra` (or `s`, `m`, `f`, `u`).
98-
99-
```bash
100-
--cache-mode cache-dit --cache-preset fast
101-
```
102-
10395
#### SCM Options
10496

10597
Steps Computation Mask controls which steps can be cached:

examples/cli/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ Generation Options:
144144
threshold=,start=,end=,decay=,relative=,reset=; dbcache/taylorseer/cache-dit: Fn=,Bn=,threshold=,warmup=;
145145
spectrum: w=,m=,lam=,window=,flex=,warmup=,stop=. Examples:
146146
"threshold=0.25" or "threshold=1.5,reset=0" or "w=0.4,window=2"
147-
--cache-preset cache-dit preset: 'slow'/'s', 'medium'/'m', 'fast'/'f', 'ultra'/'u'
148147
--scm-mask SCM steps mask for cache-dit: comma-separated 0/1 (e.g., "1,1,1,0,0,1,0,0,1,0") - 1=compute, 0=can cache
149148
--scm-policy SCM policy: 'dynamic' (default) or 'static'
150149
```

examples/common/common.hpp

Lines changed: 6 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,6 @@ struct SDGenerationParams {
10471047

10481048
std::string cache_mode;
10491049
std::string cache_option;
1050-
std::string cache_preset;
10511050
std::string scm_mask;
10521051
bool scm_policy_dynamic = true;
10531052
sd_cache_params_t cache_params{};
@@ -1461,21 +1460,6 @@ struct SDGenerationParams {
14611460
return 1;
14621461
};
14631462

1464-
auto on_cache_preset_arg = [&](int argc, const char** argv, int index) {
1465-
if (++index >= argc) {
1466-
return -1;
1467-
}
1468-
cache_preset = argv_to_utf8(index, argv);
1469-
if (cache_preset != "slow" && cache_preset != "s" && cache_preset != "S" &&
1470-
cache_preset != "medium" && cache_preset != "m" && cache_preset != "M" &&
1471-
cache_preset != "fast" && cache_preset != "f" && cache_preset != "F" &&
1472-
cache_preset != "ultra" && cache_preset != "u" && cache_preset != "U") {
1473-
fprintf(stderr, "error: invalid cache preset '%s', must be 'slow'/'s', 'medium'/'m', 'fast'/'f', or 'ultra'/'u'\n", cache_preset.c_str());
1474-
return -1;
1475-
}
1476-
return 1;
1477-
};
1478-
14791463
options.manual_options = {
14801464
{"-s",
14811465
"--seed",
@@ -1519,10 +1503,6 @@ struct SDGenerationParams {
15191503
"--cache-option",
15201504
"named cache params (key=value format, comma-separated). easycache/ucache: threshold=,start=,end=,decay=,relative=,reset=; dbcache/taylorseer/cache-dit: Fn=,Bn=,threshold=,warmup=; spectrum: w=,m=,lam=,window=,flex=,warmup=,stop=. Examples: \"threshold=0.25\" or \"threshold=1.5,reset=0\"",
15211505
on_cache_option_arg},
1522-
{"",
1523-
"--cache-preset",
1524-
"cache-dit preset: 'slow'/'s', 'medium'/'m', 'fast'/'f', 'ultra'/'u'",
1525-
on_cache_preset_arg},
15261506
{"",
15271507
"--scm-mask",
15281508
"SCM steps mask for cache-dit: comma-separated 0/1 (e.g., \"1,1,1,0,0,1,0,0,1,0\") - 1=compute, 0=can cache",
@@ -1575,7 +1555,6 @@ struct SDGenerationParams {
15751555
load_if_exists("negative_prompt", negative_prompt);
15761556
load_if_exists("cache_mode", cache_mode);
15771557
load_if_exists("cache_option", cache_option);
1578-
load_if_exists("cache_preset", cache_preset);
15791558
load_if_exists("scm_mask", scm_mask);
15801559

15811560
load_if_exists("clip_skip", clip_skip);
@@ -1810,48 +1789,17 @@ struct SDGenerationParams {
18101789

18111790
if (!cache_mode.empty()) {
18121791
if (cache_mode == "easycache") {
1813-
cache_params.mode = SD_CACHE_EASYCACHE;
1814-
cache_params.reuse_threshold = 0.2f;
1815-
cache_params.start_percent = 0.15f;
1816-
cache_params.end_percent = 0.95f;
1817-
cache_params.error_decay_rate = 1.0f;
1818-
cache_params.use_relative_threshold = true;
1819-
cache_params.reset_error_on_compute = true;
1792+
cache_params.mode = SD_CACHE_EASYCACHE;
18201793
} else if (cache_mode == "ucache") {
1821-
cache_params.mode = SD_CACHE_UCACHE;
1822-
cache_params.reuse_threshold = 1.0f;
1823-
cache_params.start_percent = 0.15f;
1824-
cache_params.end_percent = 0.95f;
1825-
cache_params.error_decay_rate = 1.0f;
1826-
cache_params.use_relative_threshold = true;
1827-
cache_params.reset_error_on_compute = true;
1794+
cache_params.mode = SD_CACHE_UCACHE;
18281795
} else if (cache_mode == "dbcache") {
1829-
cache_params.mode = SD_CACHE_DBCACHE;
1830-
cache_params.Fn_compute_blocks = 8;
1831-
cache_params.Bn_compute_blocks = 0;
1832-
cache_params.residual_diff_threshold = 0.08f;
1833-
cache_params.max_warmup_steps = 8;
1796+
cache_params.mode = SD_CACHE_DBCACHE;
18341797
} else if (cache_mode == "taylorseer") {
1835-
cache_params.mode = SD_CACHE_TAYLORSEER;
1836-
cache_params.Fn_compute_blocks = 8;
1837-
cache_params.Bn_compute_blocks = 0;
1838-
cache_params.residual_diff_threshold = 0.08f;
1839-
cache_params.max_warmup_steps = 8;
1798+
cache_params.mode = SD_CACHE_TAYLORSEER;
18401799
} else if (cache_mode == "cache-dit") {
1841-
cache_params.mode = SD_CACHE_CACHE_DIT;
1842-
cache_params.Fn_compute_blocks = 8;
1843-
cache_params.Bn_compute_blocks = 0;
1844-
cache_params.residual_diff_threshold = 0.08f;
1845-
cache_params.max_warmup_steps = 8;
1800+
cache_params.mode = SD_CACHE_CACHE_DIT;
18461801
} else if (cache_mode == "spectrum") {
1847-
cache_params.mode = SD_CACHE_SPECTRUM;
1848-
cache_params.spectrum_w = 0.40f;
1849-
cache_params.spectrum_m = 3;
1850-
cache_params.spectrum_lam = 1.0f;
1851-
cache_params.spectrum_window_size = 2;
1852-
cache_params.spectrum_flex_window = 0.50f;
1853-
cache_params.spectrum_warmup_steps = 4;
1854-
cache_params.spectrum_stop_percent = 0.9f;
1802+
cache_params.mode = SD_CACHE_SPECTRUM;
18551803
}
18561804

18571805
if (!cache_option.empty()) {

examples/server/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ Default Generation Options:
133133
--cache-option named cache params (key=value format, comma-separated). easycache/ucache:
134134
threshold=,start=,end=,decay=,relative=,reset=; dbcache/taylorseer/cache-dit: Fn=,Bn=,threshold=,warmup=. Examples:
135135
"threshold=0.25" or "threshold=1.5,reset=0"
136-
--cache-preset cache-dit preset: 'slow'/'s', 'medium'/'m', 'fast'/'f', 'ultra'/'u'
137136
--scm-mask SCM steps mask for cache-dit: comma-separated 0/1 (e.g., "1,1,1,0,0,1,0,0,1,0") - 1=compute, 0=can cache
138137
--scm-policy SCM policy: 'dynamic' (default) or 'static'
139138
```

src/cache_dit.hpp

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -603,87 +603,6 @@ inline std::vector<int> generate_scm_mask(
603603
return mask;
604604
}
605605

606-
inline std::vector<int> get_scm_preset(const std::string& preset, int total_steps) {
607-
struct Preset {
608-
std::vector<int> compute_bins;
609-
std::vector<int> cache_bins;
610-
};
611-
612-
Preset slow = {{8, 3, 3, 2, 1, 1}, {1, 2, 2, 2, 3}};
613-
Preset medium = {{6, 2, 2, 2, 2, 1}, {1, 3, 3, 3, 3}};
614-
Preset fast = {{6, 1, 1, 1, 1, 1}, {1, 3, 4, 5, 4}};
615-
Preset ultra = {{4, 1, 1, 1, 1}, {2, 5, 6, 7}};
616-
617-
Preset* p = nullptr;
618-
if (preset == "slow" || preset == "s" || preset == "S")
619-
p = &slow;
620-
else if (preset == "medium" || preset == "m" || preset == "M")
621-
p = &medium;
622-
else if (preset == "fast" || preset == "f" || preset == "F")
623-
p = &fast;
624-
else if (preset == "ultra" || preset == "u" || preset == "U")
625-
p = &ultra;
626-
else
627-
return {};
628-
629-
if (total_steps != 28 && total_steps > 0) {
630-
float scale = static_cast<float>(total_steps) / 28.0f;
631-
std::vector<int> scaled_compute, scaled_cache;
632-
633-
for (int v : p->compute_bins) {
634-
scaled_compute.push_back(std::max(1, static_cast<int>(v * scale + 0.5f)));
635-
}
636-
for (int v : p->cache_bins) {
637-
scaled_cache.push_back(std::max(1, static_cast<int>(v * scale + 0.5f)));
638-
}
639-
640-
return generate_scm_mask(scaled_compute, scaled_cache, total_steps);
641-
}
642-
643-
return generate_scm_mask(p->compute_bins, p->cache_bins, total_steps);
644-
}
645-
646-
inline float get_preset_threshold(const std::string& preset) {
647-
if (preset == "slow" || preset == "s" || preset == "S")
648-
return 0.20f;
649-
if (preset == "medium" || preset == "m" || preset == "M")
650-
return 0.25f;
651-
if (preset == "fast" || preset == "f" || preset == "F")
652-
return 0.30f;
653-
if (preset == "ultra" || preset == "u" || preset == "U")
654-
return 0.34f;
655-
return 0.08f;
656-
}
657-
658-
inline int get_preset_warmup(const std::string& preset) {
659-
if (preset == "slow" || preset == "s" || preset == "S")
660-
return 8;
661-
if (preset == "medium" || preset == "m" || preset == "M")
662-
return 6;
663-
if (preset == "fast" || preset == "f" || preset == "F")
664-
return 6;
665-
if (preset == "ultra" || preset == "u" || preset == "U")
666-
return 4;
667-
return 8;
668-
}
669-
670-
inline int get_preset_Fn(const std::string& preset) {
671-
if (preset == "slow" || preset == "s" || preset == "S")
672-
return 8;
673-
if (preset == "medium" || preset == "m" || preset == "M")
674-
return 8;
675-
if (preset == "fast" || preset == "f" || preset == "F")
676-
return 6;
677-
if (preset == "ultra" || preset == "u" || preset == "U")
678-
return 4;
679-
return 8;
680-
}
681-
682-
inline int get_preset_Bn(const std::string& preset) {
683-
(void)preset;
684-
return 0;
685-
}
686-
687606
inline void parse_dbcache_options(const std::string& opts, DBCacheConfig& cfg) {
688607
if (opts.empty())
689608
return;

src/stable-diffusion.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,19 @@ void suppress_pp(int step, int steps, float time, void* data) {
9898
return;
9999
}
100100

101+
static float get_cache_reuse_threshold(const sd_cache_params_t& params) {
102+
float reuse_threshold = params.reuse_threshold;
103+
if (reuse_threshold == INFINITY) {
104+
if (params.mode == SD_CACHE_EASYCACHE) {
105+
reuse_threshold = 0.2;
106+
}
107+
else if (params.mode == SD_CACHE_UCACHE) {
108+
reuse_threshold = 1.0;
109+
}
110+
}
111+
return std::max(0.0f, reuse_threshold);
112+
}
113+
101114
/*=============================================== StableDiffusionGGML ================================================*/
102115

103116
class StableDiffusionGGML {
@@ -1715,7 +1728,7 @@ class StableDiffusionGGML {
17151728
} else {
17161729
EasyCacheConfig easycache_config;
17171730
easycache_config.enabled = true;
1718-
easycache_config.reuse_threshold = std::max(0.0f, cache_params->reuse_threshold);
1731+
easycache_config.reuse_threshold = get_cache_reuse_threshold(*cache_params);
17191732
easycache_config.start_percent = cache_params->start_percent;
17201733
easycache_config.end_percent = cache_params->end_percent;
17211734
easycache_state.init(easycache_config, denoiser.get());
@@ -1736,7 +1749,7 @@ class StableDiffusionGGML {
17361749
} else {
17371750
UCacheConfig ucache_config;
17381751
ucache_config.enabled = true;
1739-
ucache_config.reuse_threshold = std::max(0.0f, cache_params->reuse_threshold);
1752+
ucache_config.reuse_threshold = get_cache_reuse_threshold(*cache_params);
17401753
ucache_config.start_percent = cache_params->start_percent;
17411754
ucache_config.end_percent = cache_params->end_percent;
17421755
ucache_config.error_decay_rate = std::max(0.0f, std::min(1.0f, cache_params->error_decay_rate));
@@ -2983,7 +2996,7 @@ enum lora_apply_mode_t str_to_lora_apply_mode(const char* str) {
29832996
void sd_cache_params_init(sd_cache_params_t* cache_params) {
29842997
*cache_params = {};
29852998
cache_params->mode = SD_CACHE_DISABLED;
2986-
cache_params->reuse_threshold = 1.0f;
2999+
cache_params->reuse_threshold = INFINITY;
29873000
cache_params->start_percent = 0.15f;
29883001
cache_params->end_percent = 0.95f;
29893002
cache_params->error_decay_rate = 1.0f;
@@ -3229,7 +3242,7 @@ char* sd_img_gen_params_to_str(const sd_img_gen_params_t* sd_img_gen_params) {
32293242
snprintf(buf + strlen(buf), 4096 - strlen(buf),
32303243
"cache: %s (threshold=%.3f, start=%.2f, end=%.2f)\n",
32313244
cache_mode_str,
3232-
sd_img_gen_params->cache.reuse_threshold,
3245+
get_cache_reuse_threshold(sd_img_gen_params->cache),
32333246
sd_img_gen_params->cache.start_percent,
32343247
sd_img_gen_params->cache.end_percent);
32353248
free(sample_params_str);

0 commit comments

Comments
 (0)