11static int region(span *out,
const void *ptr,
size_t elements,
size_t width)
13 const uintptr_t begin = (uintptr_t)ptr;
14 if (!ptr || begin % width || !elements || elements > (UINTPTR_MAX - begin) / width)
16 *out = (span){begin, begin + elements * width};
22 return a.end <= b.begin || b.end <= a.begin;
27 span arena, state, bindings;
29 region(&state, s,
sizeof(*s), 1) &&
62 span arena, bindings, state;
65 !
region(&state, s,
sizeof(*s), 1) ||
75 const size_t counts[] = {p,p,4*p,b,b,4*b,b,4*b,4*b,1,n,n,2,n,n,b,n};
76 const size_t widths[] = {4,4,4,4,4,4,4,4,4,4,8,8,8,4,4,4,8};
78 const size_t fixed_count =
sizeof fixed /
sizeof fixed[0] - (d->
noise == SIZE_MAX);
79 for (
size_t i = 0; i < fixed_count; ++i) {
80 if (!
slot(s, &fixed[i],
offsets[i], counts[i], widths[i]))
return 0;
81 for (
size_t j = 0; j < i; ++j)
82 if (!
disjoint(fixed[i], fixed[j]))
return 0;
88 for (
size_t k = 0; k < 3; ++k) {
89 for (
size_t j = 0; j < fixed_count; ++j)
90 if (!
disjoint(here[k], fixed[j]))
return 0;
91 for (
size_t j = 0; j < k; ++j)
92 if (!
disjoint(here[k], here[j]))
return 0;
94 for (
size_t j = 0; j < i; ++j) {
97 for (
size_t k = 0; k < 3; ++k)
98 for (
size_t l = 0; l < 3; ++l)
99 if (!
disjoint(here[k], previous[l]))
return 0;
107 return cancel && cancel(context);
110static void bind_visibility(uint8_t *arena,
size_t prefix,
size_t capacity,
size_t rows,
int causal,
111 size_t positions_offset,
size_t ranges_offset)
113 int32_t *positions = (int32_t *)(arena + positions_offset);
114 int32_t *ranges = (int32_t *)(arena + ranges_offset);
115 for (
size_t i = 0; i < rows; ++i) {
116 positions[i] = (int32_t)(prefix + i);
118 ranges[4*i+1] = (int32_t)prefix;
119 ranges[4*i+2] = (int32_t)capacity;
120 ranges[4*i+3] = (int32_t)(capacity + (causal ? i + 1 : rows));
128 const size_t prefix = reset ? 0 : s->
valid_rows;
131 rows > s->
capacity - prefix || (!reset && !prefix) ||
134 for (
size_t i = 0; i < rows; ++i)
136 if (
cancelled(cancel, context))
return -7;
140 (compiled_rows - rows) *
sizeof(*
ids));
146 if (status)
return status;
147 if (
cancelled(cancel, context))
return -7;
149 const size_t stride = s->
kv_width *
sizeof(uint16_t);
152 uint8_t *destination = s->
arena + plane->
cache;
154 memcpy(destination + prefix * stride, s->
arena + source, rows * stride);
156 memset(destination + rows * stride, 0, (s->
capacity - rows) * stride);
184 if (!
region(&operands[0], r,
sizeof(*r), 1) ||
194 for (
size_t i = 0; i < 5u + (size_t)noisy; ++i) {
195 if (!
external(s, operands[i]))
return -1;
196 for (
size_t j = 0; j < i; ++j)
197 if (!
disjoint(operands[i], operands[j]))
return -1;
205 const int32_t row[4] = {0,
end,
end,
end};
206 memcpy(ranges + 4*i, row,
sizeof(row));
217 if (status)
return status;
219 if (status)
return status;
238 if (!
region(&operands[0], r,
sizeof(*r), 1) ||
245 for (
size_t i = 0; i <
sizeof operands /
sizeof operands[0]; ++i) {
246 if (!
external(s, operands[i]))
return -1;
247 for (
size_t j = 0; j < i; ++j)
248 if (!
disjoint(operands[i], operands[j]))
return -1;
257 if (status)
return status;
261 .latent_elements = n, .schedule_count = r->
schedule_count, .noise_elements = samples - n,
268 for (
size_t i = 0; i < samples; ++i)
272 if (status)
return status;
static int cancelled(ck_diffusion_cancel cancel, void *context)
int ck_generate_seeded_diffusion(ck_diffusion_session *s, ck_diffusion_seeded_request *r)
static void bind_visibility(uint8_t *arena, size_t prefix, size_t capacity, size_t rows, int causal, size_t positions_offset, size_t ranges_offset)
int ck_prefill_diffusion_session(ck_diffusion_session *session, const int32_t *ids, size_t rows, ck_diffusion_cancel cancel, void *context)
static int slot(const ck_diffusion_session *s, span *out, size_t offset, size_t count, size_t width)
static int disjoint(span a, span b)
static int encode_clean(ck_diffusion_session *s, const int32_t *ids, size_t rows, int reset, ck_diffusion_cancel cancel, void *context)
static int external(const ck_diffusion_session *s, span value)
static int plane_slots(const ck_diffusion_session *s, size_t i, span slots[3])
static int valid_session(const ck_diffusion_session *s)
int ck_generate_diffusion_block(ck_diffusion_session *s, const ck_diffusion_block_request *r)
int ck_append_diffusion_session(ck_diffusion_session *session, const int32_t *ids, size_t rows, ck_diffusion_cancel cancel, void *context)
static int region(span *out, const void *ptr, size_t elements, size_t width)
int ck_run_checked_diffusion_schedule(const ck_diffusion_schedule_request *request)
int(* ck_diffusion_cancel)(void *context)
static uint64_t ck_rng_gaussian_domain(uint64_t seed, uint64_t stream)
static double ck_rng_gaussian_f64(uint64_t domain, uint64_t index)
static int region(const void *p, size_t n)
ck_diffusion_cancel should_cancel
ck_diffusion_arena_entry entry
size_t workspace_elements
ck_diffusion_cancel should_cancel
const int32_t * prompt_ids
size_t unconditional_ranges
ck_diffusion_clean_entry prefill
ck_diffusion_clean_entry append
ck_diffusion_step_bindings step_bindings
ck_diffusion_arena_entry step
size_t conditional_ranges
const ck_diffusion_cache_plane * planes
int const int32_t * offsets
const uint8_t size_t count