Skip to content

Commit d3a4d5a

Browse files
committed
Add gradient estimation sampler
1 parent d05e46c commit d3a4d5a

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
@@ -59,6 +59,7 @@ const char* sampling_methods_str[] = {
5959
"DDIM \"trailing\"",
6060
"TCD",
6161
"Euler A",
62+
"Gradient Estimation",
6263
};
6364

6465
/*================================================== Helper Functions ================================================*/
@@ -1124,6 +1125,7 @@ class StableDiffusionGGML {
11241125
float eta,
11251126
int shifted_timestep,
11261127
sample_method_t method,
1128+
float ge_gamma,
11271129
const std::vector<float>& sigmas,
11281130
int start_merge_step,
11291131
SDCondition id_cond,
@@ -1354,7 +1356,7 @@ class StableDiffusionGGML {
13541356
return denoised;
13551357
};
13561358

1357-
sample_k_diffusion(method, denoise, work_ctx, x, sigmas, rng, eta);
1359+
sample_k_diffusion(method, denoise, work_ctx, x, sigmas, rng, eta, ge_gamma);
13581360

13591361
if (inverse_noise_scaling) {
13601362
x = denoiser->inverse_noise_scaling(sigmas[sigmas.size() - 1], x);
@@ -1725,6 +1727,7 @@ const char* sample_method_to_str[] = {
17251727
"ddim_trailing",
17261728
"tcd",
17271729
"euler_a",
1730+
"gradient_estimation",
17281731
};
17291732

17301733
const char* sd_sample_method_name(enum sample_method_t sample_method) {
@@ -1896,6 +1899,7 @@ void sd_sample_params_init(sd_sample_params_t* sample_params) {
18961899
sample_params->scheduler = DEFAULT;
18971900
sample_params->sample_method = SAMPLE_METHOD_DEFAULT;
18981901
sample_params->sample_steps = 20;
1902+
sample_params->ge_gamma = 2.0f;
18991903
}
19001904

19011905
char* sd_sample_params_to_str(const sd_sample_params_t* sample_params) {
@@ -1916,6 +1920,7 @@ char* sd_sample_params_to_str(const sd_sample_params_t* sample_params) {
19161920
"sample_method: %s, "
19171921
"sample_steps: %d, "
19181922
"eta: %.2f, "
1923+
"ge_gamma: %.2f, "
19191924
"shifted_timestep: %d)",
19201925
sample_params->guidance.txt_cfg,
19211926
isfinite(sample_params->guidance.img_cfg)
@@ -1930,6 +1935,7 @@ char* sd_sample_params_to_str(const sd_sample_params_t* sample_params) {
19301935
sd_sample_method_name(sample_params->sample_method),
19311936
sample_params->sample_steps,
19321937
sample_params->eta,
1938+
sample_params->ge_gamma,
19331939
sample_params->shifted_timestep);
19341940

19351941
return buf;
@@ -2065,6 +2071,7 @@ sd_image_t* generate_image_internal(sd_ctx_t* sd_ctx,
20652071
int width,
20662072
int height,
20672073
enum sample_method_t sample_method,
2074+
float ge_gamma,
20682075
const std::vector<float>& sigmas,
20692076
int64_t seed,
20702077
int batch_count,
@@ -2353,6 +2360,7 @@ sd_image_t* generate_image_internal(sd_ctx_t* sd_ctx,
23532360
eta,
23542361
shifted_timestep,
23552362
sample_method,
2363+
ge_gamma,
23562364
sigmas,
23572365
start_merge_step,
23582366
id_cond,
@@ -2692,6 +2700,7 @@ sd_image_t* generate_image(sd_ctx_t* sd_ctx, const sd_img_gen_params_t* sd_img_g
26922700
width,
26932701
height,
26942702
sample_method,
2703+
sd_img_gen_params->sample_params.ge_gamma,
26952704
sigmas,
26962705
seed,
26972706
sd_img_gen_params->batch_count,
@@ -3019,6 +3028,7 @@ SD_API sd_image_t* generate_video(sd_ctx_t* sd_ctx, const sd_vid_gen_params_t* s
30193028
sd_vid_gen_params->high_noise_sample_params.eta,
30203029
sd_vid_gen_params->high_noise_sample_params.shifted_timestep,
30213030
sd_vid_gen_params->high_noise_sample_params.sample_method,
3031+
sd_vid_gen_params->high_noise_sample_params.ge_gamma,
30223032
high_noise_sigmas,
30233033
-1,
30243034
{},
@@ -3055,6 +3065,7 @@ SD_API sd_image_t* generate_video(sd_ctx_t* sd_ctx, const sd_vid_gen_params_t* s
30553065
sd_vid_gen_params->sample_params.eta,
30563066
sd_vid_gen_params->sample_params.shifted_timestep,
30573067
sd_vid_gen_params->sample_params.sample_method,
3068+
sd_vid_gen_params->sample_params.ge_gamma,
30583069
sigmas,
30593070
-1,
30603071
{},

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)