← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
diffusion_flow_unipc.c File Reference
#include <math.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>

Go to the source code of this file.

Functions

int ck_diffusion_flow_unipc_step_f32 (const float *latent, const float *velocity, const float *last_sample, const float *last_clean, const float *older_clean, float *next_latent, float *corrected_sample, float *clean, float *scratch, size_t count, size_t scratch_count, size_t step, float older_sigma, float last_sigma, float sigma, float next_sigma)
static FlowUpdate flow_correct (float from, float to, float older)
static float flow_log_snr (float sigma)
static FlowUpdate flow_update (float from, float to, float older)
static int overlaps (const float *a, size_t ac, const float *b, size_t bc)
static int valid_region (const float *ptr, size_t count)

Function Documentation

◆ ck_diffusion_flow_unipc_step_f32()

int ck_diffusion_flow_unipc_step_f32 ( const float * latent,
const float * velocity,
const float * last_sample,
const float * last_clean,
const float * older_clean,
float * next_latent,
float * corrected_sample,
float * clean,
float * scratch,
size_t count,
size_t scratch_count,
size_t step,
float older_sigma,
float last_sigma,
float sigma,
float next_sigma )

Definition at line 57 of file diffusion_flow_unipc.c.

63{
64 if (!count || count > SIZE_MAX / 3 || scratch_count < 3 * count ||
65 !valid_region(scratch, 3 * count) || step > 1023)
66 return -2;
67 const float *ports[] = {latent, velocity, last_sample, last_clean, older_clean,
68 next_latent, corrected_sample, clean};
69 for (size_t i = 0; i < 8; ++i) {
70 if (!valid_region(ports[i], count) || overlaps(ports[i], count, scratch, 3 * count))
71 return -2;
72 if (i >= 5)
73 for (size_t j = 0; j < i; ++j)
74 if (overlaps(ports[i], count, ports[j], count))
75 return -2;
76 }
77 if (!(sigma < 1.0f && sigma > next_sigma && next_sigma >= 0.0f) ||
78 (step == 0 ? last_sigma != 0.0f : !(last_sigma < 1.0f && last_sigma > sigma)) ||
79 (step < 2 ? older_sigma != 0.0f : !(older_sigma < 1.0f && older_sigma > last_sigma)))
80 return -2;
81
82 const int terminal = next_sigma == 0.0f;
83 FlowUpdate correction = {0}, prediction = {0};
84 if (step)
85 correction = flow_correct(last_sigma, sigma, older_sigma);
86 if (!terminal)
87 prediction = flow_update(sigma, next_sigma, last_sigma);
88 for (size_t i = 0; i < count; ++i) {
89 const float step_clean = latent[i] - sigma * velocity[i];
90 float sample = latent[i];
91 if (step) {
92 const float delta = step_clean - last_clean[i];
93 float residual = correction.current_weight * delta;
94 if (step > 1)
95 residual += correction.older_weight * ((older_clean[i] - last_clean[i]) / correction.ratio);
96 sample = correction.sample_scale * last_sample[i] - correction.clean_scale * last_clean[i];
97 sample -= correction.clean_scale * residual;
98 }
99 float next = step_clean;
100 if (!terminal) {
101 next = prediction.sample_scale * sample - prediction.clean_scale * step_clean;
102 if (step)
103 next -= prediction.clean_scale * (prediction.older_weight * ((last_clean[i] - step_clean) / prediction.ratio));
104 }
105 if (!isfinite(step_clean) || !isfinite(sample) || !isfinite(next))
106 return -3;
107 scratch[i] = next;
108 scratch[count + i] = sample;
109 scratch[2 * count + i] = step_clean;
110 }
111 memcpy(next_latent, scratch, count * sizeof(float));
112 memcpy(corrected_sample, scratch + count, count * sizeof(float));
113 memcpy(clean, scratch + 2 * count, count * sizeof(float));
114 return 0;
115}
static int overlaps(const float *a, size_t ac, const float *b, size_t bc)
static int valid_region(const float *ptr, size_t count)
static FlowUpdate flow_update(float from, float to, float older)
static FlowUpdate flow_correct(float from, float to, float older)
const uint8_t size_t count
Definition true_bpe.h:211

References count, flow_correct(), flow_update(), overlaps(), and valid_region().

◆ flow_correct()

FlowUpdate flow_correct ( float from,
float to,
float older )
static

Definition at line 28 of file diffusion_flow_unipc.c.

29{
30 FlowUpdate update = flow_update(from, to, older);
31 if (older > 0.0f) {
32 const float hh = flow_log_snr(from) - flow_log_snr(to);
33 const float bh = expm1f(hh);
34 const float phi2 = bh / hh - 1.0f;
35 const float b0 = phi2 / bh;
36 const float b1 = 2.0f * (phi2 / hh - 0.5f) / bh;
37 update.older_weight = (b0 - b1) / (1.0f - update.ratio);
38 update.current_weight = b0 - update.older_weight;
39 }
40 return update;
41}
static float flow_log_snr(float sigma)

References flow_log_snr(), and flow_update().

Referenced by ck_diffusion_flow_unipc_step_f32().

◆ flow_log_snr()

float flow_log_snr ( float sigma)
static

Definition at line 10 of file diffusion_flow_unipc.c.

11{
12 return logf(1.0f - sigma) - logf(sigma);
13}

Referenced by flow_correct(), and flow_update().

◆ flow_update()

FlowUpdate flow_update ( float from,
float to,
float older )
static

Definition at line 15 of file diffusion_flow_unipc.c.

16{
17 const float h = flow_log_snr(to) - flow_log_snr(from);
18 const float phi = expm1f(-h);
19 return (FlowUpdate){
20 .sample_scale = to / from,
21 .clean_scale = (1.0f - to) * phi,
22 .ratio = older > 0.0f ? (flow_log_snr(older) - flow_log_snr(from)) / h : 1.0f,
23 .older_weight = 0.5f,
24 .current_weight = 0.5f,
25 };
26}

References flow_log_snr().

Referenced by ck_diffusion_flow_unipc_step_f32(), and flow_correct().

◆ overlaps()

int overlaps ( const float * a,
size_t ac,
const float * b,
size_t bc )
static

Definition at line 49 of file diffusion_flow_unipc.c.

50{
51 return (uintptr_t)a < (uintptr_t)b + bc * sizeof(float) &&
52 (uintptr_t)b < (uintptr_t)a + ac * sizeof(float);
53}

Referenced by ck_diffusion_flow_unipc_step_f32().

◆ valid_region()

int valid_region ( const float * ptr,
size_t count )
static

Definition at line 43 of file diffusion_flow_unipc.c.

44{
45 return ptr && (uintptr_t)ptr % _Alignof(float) == 0 &&
46 count <= (UINTPTR_MAX - (uintptr_t)ptr) / sizeof(float);
47}

References count.

Referenced by ck_diffusion_flow_unipc_step_f32().