Skip to content

Commit 58e594d

Browse files
committed
Add gradient estimation sampler
1 parent 3c1187c commit 58e594d

File tree

4 files changed

+63
-3
lines changed

4 files changed

+63
-3
lines changed

denoiser.hpp

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,8 @@ static void sample_k_diffusion(sample_method_t method,
672672
ggml_tensor* x,
673673
std::vector<float> sigmas,
674674
std::shared_ptr<RNG> rng,
675-
float eta) {
675+
float eta,
676+
float ge_gamma) {
676677
size_t steps = sigmas.size() - 1;
677678
// sample_euler_ancestral
678679
switch (method) {
@@ -1558,7 +1559,52 @@ static void sample_k_diffusion(sample_method_t method,
15581559
}
15591560
}
15601561
} break;
1562+
case GRADIENT_ESTIMATION: {
1563+
struct ggml_tensor* d = ggml_dup_tensor(work_ctx, x);
1564+
struct ggml_tensor* old_d = ggml_dup_tensor(work_ctx, x);
1565+
bool has_old_d = false;
15611566

1567+
for (int i = 0; i < steps; i++) {
1568+
float sigma = sigmas[i];
1569+
1570+
ggml_tensor* denoised = model(x, sigma, i + 1);
1571+
1572+
// d = (x - denoised) / sigma
1573+
float* vec_d = (float*)d->data;
1574+
float* vec_x = (float*)x->data;
1575+
float* vec_denoised = (float*)denoised->data;
1576+
1577+
for (int j = 0; j < ggml_nelements(d); j++) {
1578+
vec_d[j] = (vec_x[j] - vec_denoised[j]) / sigma;
1579+
}
1580+
1581+
float dt = sigmas[i + 1] - sigma;
1582+
1583+
if (sigmas[i + 1] == 0) {
1584+
// Denoising step
1585+
for (int j = 0; j < ggml_nelements(x); j++) {
1586+
vec_x[j] = vec_denoised[j];
1587+
}
1588+
} else {
1589+
// Euler method
1590+
for (int j = 0; j < ggml_nelements(x); j++) {
1591+
vec_x[j] = vec_x[j] + vec_d[j] * dt;
1592+
}
1593+
}
1594+
1595+
if (has_old_d) {
1596+
// Gradient estimation
1597+
float* vec_old_d = (float*)old_d->data;
1598+
for (int j = 0; j < ggml_nelements(x); j++) {
1599+
float d_bar = (ge_gamma - 1.f) * (vec_d[j] - vec_old_d[j]);
1600+
vec_x[j] = vec_x[j] + d_bar * dt;
1601+
}
1602+
}
1603+
// old_d = d
1604+
copy_ggml_tensor(old_d, d);
1605+
has_old_d = true;
1606+
}
1607+
} break;
15621608
default:
15631609
LOG_ERROR("Attempting to sample with nonexisting sample method %i", method);
15641610
abort();

examples/cli/main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,8 @@ void parse_args(int argc, const char** argv, SDParams& params) {
725725
"eta in DDIM, only for DDIM and TCD (default: 0)",
726726
&params.sample_params.eta},
727727
{"",
728-
"--high-noise-cfg-scale",
728+
"--ge-gamma", "", &params.sample_params.ge_gamma},
729+
{"", "--high-noise-cfg-scale",
729730
"(high noise) unconditional guidance scale: (default: 7.0)",
730731
&params.high_noise_sample_params.guidance.txt_cfg},
731732
{"",

stable-diffusion.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ const char* sampling_methods_str[] = {
6060
"LCM",
6161
"DDIM \"trailing\"",
6262
"TCD",
63+
"Gradient Estimation",
6364
};
6465

6566
/*================================================== Helper Functions ================================================*/
@@ -1461,6 +1462,7 @@ class StableDiffusionGGML {
14611462
float eta,
14621463
int shifted_timestep,
14631464
sample_method_t method,
1465+
float ge_gamma,
14641466
const std::vector<float>& sigmas,
14651467
int start_merge_step,
14661468
SDCondition id_cond,
@@ -1817,7 +1819,7 @@ class StableDiffusionGGML {
18171819
return denoised;
18181820
};
18191821

1820-
sample_k_diffusion(method, denoise, work_ctx, x, sigmas, sampler_rng, eta);
1822+
sample_k_diffusion(method, denoise, work_ctx, x, sigmas, sampler_rng, eta, ge_gamma);
18211823

18221824
if (easycache_enabled) {
18231825
size_t total_steps = sigmas.size() > 0 ? sigmas.size() - 1 : 0;
@@ -2323,6 +2325,7 @@ const char* sample_method_to_str[] = {
23232325
"lcm",
23242326
"ddim_trailing",
23252327
"tcd",
2328+
"gradient_estimation",
23262329
};
23272330

23282331
const char* sd_sample_method_name(enum sample_method_t sample_method) {
@@ -2554,6 +2557,7 @@ void sd_sample_params_init(sd_sample_params_t* sample_params) {
25542557
sample_params->scheduler = SCHEDULER_COUNT;
25552558
sample_params->sample_method = SAMPLE_METHOD_COUNT;
25562559
sample_params->sample_steps = 20;
2560+
sample_params->ge_gamma = 2.0f;
25572561
}
25582562

25592563
char* sd_sample_params_to_str(const sd_sample_params_t* sample_params) {
@@ -2574,6 +2578,7 @@ char* sd_sample_params_to_str(const sd_sample_params_t* sample_params) {
25742578
"sample_method: %s, "
25752579
"sample_steps: %d, "
25762580
"eta: %.2f, "
2581+
"ge_gamma: %.2f, "
25772582
"shifted_timestep: %d)",
25782583
sample_params->guidance.txt_cfg,
25792584
std::isfinite(sample_params->guidance.img_cfg)
@@ -2588,6 +2593,7 @@ char* sd_sample_params_to_str(const sd_sample_params_t* sample_params) {
25882593
sd_sample_method_name(sample_params->sample_method),
25892594
sample_params->sample_steps,
25902595
sample_params->eta,
2596+
sample_params->ge_gamma,
25912597
sample_params->shifted_timestep);
25922598

25932599
return buf;
@@ -2739,6 +2745,7 @@ sd_image_t* generate_image_internal(sd_ctx_t* sd_ctx,
27392745
int width,
27402746
int height,
27412747
enum sample_method_t sample_method,
2748+
float ge_gamma,
27422749
const std::vector<float>& sigmas,
27432750
int64_t seed,
27442751
int batch_count,
@@ -3033,6 +3040,7 @@ sd_image_t* generate_image_internal(sd_ctx_t* sd_ctx,
30333040
eta,
30343041
shifted_timestep,
30353042
sample_method,
3043+
ge_gamma,
30363044
sigmas,
30373045
start_merge_step,
30383046
id_cond,
@@ -3350,6 +3358,7 @@ sd_image_t* generate_image(sd_ctx_t* sd_ctx, const sd_img_gen_params_t* sd_img_g
33503358
width,
33513359
height,
33523360
sample_method,
3361+
sd_img_gen_params->sample_params.ge_gamma,
33533362
sigmas,
33543363
seed,
33553364
sd_img_gen_params->batch_count,
@@ -3686,6 +3695,7 @@ SD_API sd_image_t* generate_video(sd_ctx_t* sd_ctx, const sd_vid_gen_params_t* s
36863695
sd_vid_gen_params->high_noise_sample_params.eta,
36873696
sd_vid_gen_params->high_noise_sample_params.shifted_timestep,
36883697
high_noise_sample_method,
3698+
sd_vid_gen_params->high_noise_sample_params.ge_gamma,
36893699
high_noise_sigmas,
36903700
-1,
36913701
{},
@@ -3723,6 +3733,7 @@ SD_API sd_image_t* generate_video(sd_ctx_t* sd_ctx, const sd_vid_gen_params_t* s
37233733
sd_vid_gen_params->sample_params.eta,
37243734
sd_vid_gen_params->sample_params.shifted_timestep,
37253735
sample_method,
3736+
sd_vid_gen_params->sample_params.ge_gamma,
37263737
sigmas,
37273738
-1,
37283739
{},

stable-diffusion.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ enum sample_method_t {
4848
LCM_SAMPLE_METHOD,
4949
DDIM_TRAILING_SAMPLE_METHOD,
5050
TCD_SAMPLE_METHOD,
51+
GRADIENT_ESTIMATION,
5152
SAMPLE_METHOD_COUNT
5253
};
5354

@@ -219,6 +220,7 @@ typedef struct {
219220
enum sample_method_t sample_method;
220221
int sample_steps;
221222
float eta;
223+
float ge_gamma;
222224
int shifted_timestep;
223225
} sd_sample_params_t;
224226

0 commit comments

Comments
 (0)