Skip to content

Commit 6237e33

Browse files
committed
Add gradient estimation sampler
1 parent 6103d86 commit 6237e33

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
@@ -576,7 +576,8 @@ static void sample_k_diffusion(sample_method_t method,
576576
ggml_tensor* x,
577577
std::vector<float> sigmas,
578578
std::shared_ptr<RNG> rng,
579-
float eta) {
579+
float eta,
580+
float ge_gamma) {
580581
size_t steps = sigmas.size() - 1;
581582
// sample_euler_ancestral
582583
switch (method) {
@@ -1462,7 +1463,52 @@ static void sample_k_diffusion(sample_method_t method,
14621463
}
14631464
}
14641465
} break;
1466+
case GRADIENT_ESTIMATION: {
1467+
struct ggml_tensor* d = ggml_dup_tensor(work_ctx, x);
1468+
struct ggml_tensor* old_d = ggml_dup_tensor(work_ctx, x);
1469+
bool has_old_d = false;
14651470

1471+
for (int i = 0; i < steps; i++) {
1472+
float sigma = sigmas[i];
1473+
1474+
ggml_tensor* denoised = model(x, sigma, i + 1);
1475+
1476+
// d = (x - denoised) / sigma
1477+
float* vec_d = (float*)d->data;
1478+
float* vec_x = (float*)x->data;
1479+
float* vec_denoised = (float*)denoised->data;
1480+
1481+
for (int j = 0; j < ggml_nelements(d); j++) {
1482+
vec_d[j] = (vec_x[j] - vec_denoised[j]) / sigma;
1483+
}
1484+
1485+
float dt = sigmas[i + 1] - sigma;
1486+
1487+
if (sigmas[i + 1] == 0) {
1488+
// Denoising step
1489+
for (int j = 0; j < ggml_nelements(x); j++) {
1490+
vec_x[j] = vec_denoised[j];
1491+
}
1492+
} else {
1493+
// Euler method
1494+
for (int j = 0; j < ggml_nelements(x); j++) {
1495+
vec_x[j] = vec_x[j] + vec_d[j] * dt;
1496+
}
1497+
}
1498+
1499+
if (has_old_d) {
1500+
// Gradient estimation
1501+
float* vec_old_d = (float*)old_d->data;
1502+
for (int j = 0; j < ggml_nelements(x); j++) {
1503+
float d_bar = (ge_gamma - 1.f) * (vec_d[j] - vec_old_d[j]);
1504+
vec_x[j] = vec_x[j] + d_bar * dt;
1505+
}
1506+
}
1507+
// old_d = d
1508+
copy_ggml_tensor(old_d, d);
1509+
has_old_d = true;
1510+
}
1511+
} break;
14661512
default:
14671513
LOG_ERROR("Attempting to sample with nonexisting sample method %i", method);
14681514
abort();

examples/cli/main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,8 @@ void parse_args(int argc, const char** argv, SDParams& params) {
679679
"eta in DDIM, only for DDIM and TCD (default: 0)",
680680
&params.sample_params.eta},
681681
{"",
682-
"--high-noise-cfg-scale",
682+
"--ge-gamma", "", &params.sample_params.ge_gamma},
683+
{"", "--high-noise-cfg-scale",
683684
"(high noise) unconditional guidance scale: (default: 7.0)",
684685
&params.high_noise_sample_params.guidance.txt_cfg},
685686
{"",

stable-diffusion.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const char* sampling_methods_str[] = {
5454
"DDIM \"trailing\"",
5555
"TCD",
5656
"Euler A",
57+
"Gradient Estimation",
5758
};
5859

5960
/*================================================== Helper Functions ================================================*/
@@ -1123,6 +1124,7 @@ class StableDiffusionGGML {
11231124
float eta,
11241125
int shifted_timestep,
11251126
sample_method_t method,
1127+
float ge_gamma,
11261128
const std::vector<float>& sigmas,
11271129
int start_merge_step,
11281130
SDCondition id_cond,
@@ -1353,7 +1355,7 @@ class StableDiffusionGGML {
13531355
return denoised;
13541356
};
13551357

1356-
sample_k_diffusion(method, denoise, work_ctx, x, sigmas, rng, eta);
1358+
sample_k_diffusion(method, denoise, work_ctx, x, sigmas, rng, eta, ge_gamma);
13571359

13581360
if (inverse_noise_scaling) {
13591361
x = denoiser->inverse_noise_scaling(sigmas[sigmas.size() - 1], x);
@@ -1782,6 +1784,7 @@ const char* sample_method_to_str[] = {
17821784
"ddim_trailing",
17831785
"tcd",
17841786
"euler_a",
1787+
"gradient_estimation",
17851788
};
17861789

17871790
const char* sd_sample_method_name(enum sample_method_t sample_method) {
@@ -1953,6 +1956,7 @@ void sd_sample_params_init(sd_sample_params_t* sample_params) {
19531956
sample_params->scheduler = DEFAULT;
19541957
sample_params->sample_method = SAMPLE_METHOD_DEFAULT;
19551958
sample_params->sample_steps = 20;
1959+
sample_params->ge_gamma = 2.0f;
19561960
}
19571961

19581962
char* sd_sample_params_to_str(const sd_sample_params_t* sample_params) {
@@ -1973,6 +1977,7 @@ char* sd_sample_params_to_str(const sd_sample_params_t* sample_params) {
19731977
"sample_method: %s, "
19741978
"sample_steps: %d, "
19751979
"eta: %.2f, "
1980+
"ge_gamma: %.2f, "
19761981
"shifted_timestep: %d)",
19771982
sample_params->guidance.txt_cfg,
19781983
std::isfinite(sample_params->guidance.img_cfg)
@@ -1987,6 +1992,7 @@ char* sd_sample_params_to_str(const sd_sample_params_t* sample_params) {
19871992
sd_sample_method_name(sample_params->sample_method),
19881993
sample_params->sample_steps,
19891994
sample_params->eta,
1995+
sample_params->ge_gamma,
19901996
sample_params->shifted_timestep);
19911997

19921998
return buf;
@@ -2122,6 +2128,7 @@ sd_image_t* generate_image_internal(sd_ctx_t* sd_ctx,
21222128
int width,
21232129
int height,
21242130
enum sample_method_t sample_method,
2131+
float ge_gamma,
21252132
const std::vector<float>& sigmas,
21262133
int64_t seed,
21272134
int batch_count,
@@ -2403,6 +2410,7 @@ sd_image_t* generate_image_internal(sd_ctx_t* sd_ctx,
24032410
eta,
24042411
shifted_timestep,
24052412
sample_method,
2413+
ge_gamma,
24062414
sigmas,
24072415
start_merge_step,
24082416
id_cond,
@@ -2711,6 +2719,7 @@ sd_image_t* generate_image(sd_ctx_t* sd_ctx, const sd_img_gen_params_t* sd_img_g
27112719
width,
27122720
height,
27132721
sample_method,
2722+
sd_img_gen_params->sample_params.ge_gamma,
27142723
sigmas,
27152724
seed,
27162725
sd_img_gen_params->batch_count,
@@ -3034,6 +3043,7 @@ SD_API sd_image_t* generate_video(sd_ctx_t* sd_ctx, const sd_vid_gen_params_t* s
30343043
sd_vid_gen_params->high_noise_sample_params.eta,
30353044
sd_vid_gen_params->high_noise_sample_params.shifted_timestep,
30363045
sd_vid_gen_params->high_noise_sample_params.sample_method,
3046+
sd_vid_gen_params->high_noise_sample_params.ge_gamma,
30373047
high_noise_sigmas,
30383048
-1,
30393049
{},
@@ -3070,6 +3080,7 @@ SD_API sd_image_t* generate_video(sd_ctx_t* sd_ctx, const sd_vid_gen_params_t* s
30703080
sd_vid_gen_params->sample_params.eta,
30713081
sd_vid_gen_params->sample_params.shifted_timestep,
30723082
sd_vid_gen_params->sample_params.sample_method,
3083+
sd_vid_gen_params->sample_params.ge_gamma,
30733084
sigmas,
30743085
-1,
30753086
{},

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
DDIM_TRAILING,
4949
TCD,
5050
EULER_A,
51+
GRADIENT_ESTIMATION,
5152
SAMPLE_METHOD_COUNT
5253
};
5354

@@ -199,6 +200,7 @@ typedef struct {
199200
enum sample_method_t sample_method;
200201
int sample_steps;
201202
float eta;
203+
float ge_gamma;
202204
int shifted_timestep;
203205
} sd_sample_params_t;
204206

0 commit comments

Comments
 (0)