← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
checked_diffusion_session.c
Go to the documentation of this file.
3
4#include <math.h>
5#include <string.h>
6
7typedef struct {
8 uintptr_t begin, end;
9} span;
10
11static int region(span *out, const void *ptr, size_t elements, size_t width)
12{
13 const uintptr_t begin = (uintptr_t)ptr;
14 if (!ptr || begin % width || !elements || elements > (UINTPTR_MAX - begin) / width)
15 return 0;
16 *out = (span){begin, begin + elements * width};
17 return 1;
18}
19
20static int disjoint(span a, span b)
21{
22 return a.end <= b.begin || b.end <= a.begin;
23}
24
25static int external(const ck_diffusion_session *s, span value)
26{
27 span arena, state, bindings;
28 return region(&arena, s->arena, s->arena_bytes, 1) &&
29 region(&state, s, sizeof(*s), 1) &&
30 region(&bindings, s->planes, s->plane_count * sizeof(*s->planes), 1) &&
31 disjoint(value, arena) && disjoint(value, state) && disjoint(value, bindings);
32}
33
34static int slot(const ck_diffusion_session *s, span *out, size_t offset, size_t count, size_t width)
35{
36 return offset <= s->arena_bytes && count <= (s->arena_bytes - offset) / width &&
37 region(out, s->arena + offset, count, width);
38}
39
40static int plane_slots(const ck_diffusion_session *s, size_t i, span slots[3])
41{
42 const ck_diffusion_cache_plane *p = &s->planes[i];
43 return slot(s, &slots[0], p->cache, s->capacity * s->kv_width, sizeof(uint16_t)) &&
44 slot(s, &slots[1], p->prefill_rows, s->prompt_rows * s->kv_width, sizeof(uint16_t)) &&
45 slot(s, &slots[2], p->append_rows, s->block_rows * s->kv_width, sizeof(uint16_t));
46}
47
49{
50 if (!s || (uintptr_t)s % _Alignof(ck_diffusion_session) || !s->arena || !s->planes ||
51 (uintptr_t)s->planes % _Alignof(ck_diffusion_cache_plane) ||
52 !s->prefill.entry || !s->append.entry || !s->step || !s->plane_count ||
53 s->plane_count > SIZE_MAX / sizeof(*s->planes) ||
54 !s->kv_width || !s->latent_elements || !s->prompt_rows || !s->block_rows ||
55 !s->vocabulary || s->vocabulary > INT32_MAX || s->capacity > INT32_MAX ||
56 s->block_rows > (size_t)INT32_MAX - s->capacity || s->prompt_rows > s->capacity ||
57 s->block_rows > s->capacity - s->prompt_rows || s->capacity > SIZE_MAX / s->kv_width ||
58 s->prompt_rows > SIZE_MAX / 4 || s->block_rows > SIZE_MAX / 4 ||
59 s->valid_rows > s->capacity)
60 return 0;
61
62 span arena, bindings, state;
63 if (!region(&arena, s->arena, s->arena_bytes, 1) ||
64 !region(&bindings, s->planes, s->plane_count * sizeof(*s->planes), 1) ||
65 !region(&state, s, sizeof(*s), 1) ||
66 !disjoint(arena, state) || !disjoint(bindings, state) || !disjoint(bindings, arena))
67 return 0;
68
69 const size_t p = s->prompt_rows, b = s->block_rows, n = s->latent_elements;
71 const size_t offsets[] = {s->prefill.token_ids, s->prefill.positions, s->prefill.ranges,
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};
77 span fixed[sizeof offsets / sizeof offsets[0]];
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;
83 }
84
85 for (size_t i = 0; i < s->plane_count; ++i) {
86 span here[3];
87 if (!plane_slots(s, i, here)) 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;
93 }
94 for (size_t j = 0; j < i; ++j) {
95 span previous[3];
96 if (!plane_slots(s, j, previous)) return 0;
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;
100 }
101 }
102 return 1;
103}
104
105static int cancelled(ck_diffusion_cancel cancel, void *context)
106{
107 return cancel && cancel(context);
108}
109
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)
112{
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);
117 ranges[4*i] = 0;
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));
121 }
122}
123
124static int encode_clean(ck_diffusion_session *s, const int32_t *ids, size_t rows, int reset,
125 ck_diffusion_cancel cancel, void *context)
126{
127 const ck_diffusion_clean_entry *entry = reset ? &s->prefill : &s->append;
128 const size_t prefix = reset ? 0 : s->valid_rows;
129 span tokens;
130 if ((reset ? rows > s->prompt_rows : rows != s->block_rows) ||
131 rows > s->capacity - prefix || (!reset && !prefix) ||
132 !region(&tokens, ids, rows, sizeof(*ids)) || !external(s, tokens))
133 return -1;
134 for (size_t i = 0; i < rows; ++i)
135 if (ids[i] < 0 || (size_t)ids[i] >= s->vocabulary) return -1;
136 if (cancelled(cancel, context)) return -7;
137 memcpy(s->arena + entry->token_ids, ids, rows * sizeof(*ids));
138 const size_t compiled_rows = reset ? s->prompt_rows : s->block_rows;
139 memset(s->arena + entry->token_ids + rows * sizeof(*ids), 0,
140 (compiled_rows - rows) * sizeof(*ids));
141 /* Clean attention is causal. Padding can read the live prefix, but
142 * live rows cannot read padding, and only live K/V rows are committed. */
143 bind_visibility(s->arena, prefix, reset ? 0 : s->capacity, compiled_rows, 1,
144 entry->positions, entry->ranges);
145 const int status = entry->entry(s->arena, s->arena_bytes);
146 if (status) return status;
147 if (cancelled(cancel, context)) return -7;
148
149 const size_t stride = s->kv_width * sizeof(uint16_t);
150 for (size_t i = 0; i < s->plane_count; ++i) {
151 const ck_diffusion_cache_plane *plane = &s->planes[i];
152 uint8_t *destination = s->arena + plane->cache;
153 const size_t source = reset ? plane->prefill_rows : plane->append_rows;
154 memcpy(destination + prefix * stride, s->arena + source, rows * stride);
155 if (reset)
156 memset(destination + rows * stride, 0, (s->capacity - rows) * stride);
157 }
158 s->valid_rows = prefix + rows;
159 return 0;
160}
161
162int ck_prefill_diffusion_session(ck_diffusion_session *session, const int32_t *ids, size_t rows,
163 ck_diffusion_cancel cancel, void *context)
164{
165 if (!valid_session(session)) return -1;
166 return encode_clean(session, ids, rows, 1, cancel, context);
167}
168
169int ck_append_diffusion_session(ck_diffusion_session *session, const int32_t *ids, size_t rows,
170 ck_diffusion_cancel cancel, void *context)
171{
172 if (!valid_session(session)) return -1;
173 return encode_clean(session, ids, rows, 0, cancel, context);
174}
175
177{
178 if (!r || (uintptr_t)r % _Alignof(ck_diffusion_block_request) || !valid_session(s) ||
179 !s->valid_rows || s->block_rows > s->capacity - s->valid_rows ||
180 !isfinite(r->guidance) || r->latent_elements != s->latent_elements ||
182 return -1;
183 span operands[6];
184 if (!region(&operands[0], r, sizeof(*r), 1) ||
185 !region(&operands[1], r->output_ids, r->output_capacity, sizeof(int32_t)) ||
186 !region(&operands[2], r->staged_ids, r->staged_capacity, sizeof(int32_t)) ||
187 !region(&operands[3], r->latent, r->latent_elements, sizeof(double)) ||
188 !region(&operands[4], r->schedule, r->schedule_count, sizeof(double)))
189 return -1;
190 const int noisy = s->step_bindings.noise != SIZE_MAX;
191 if (noisy ? !region(&operands[5], r->noise, r->noise_elements, sizeof(double))
192 : r->noise || r->noise_elements)
193 return -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;
198 }
199 if (cancelled(r->should_cancel, r->cancel_context)) return -7;
200
202 int32_t *ranges = (int32_t *)(s->arena + s->unconditional_ranges);
203 for (size_t i = 0; i < s->block_rows; ++i) {
204 const int32_t end = (int32_t)s->block_rows;
205 const int32_t row[4] = {0, end, end, end};
206 memcpy(ranges + 4*i, row, sizeof(row));
207 }
208 memcpy(s->arena + s->guidance, &r->guidance, sizeof(r->guidance));
209 const ck_diffusion_schedule_request step = {
210 .step = s->step, .arena = s->arena, .arena_bytes = s->arena_bytes, .bindings = s->step_bindings,
211 .latent_elements = s->latent_elements, .token_count = s->block_rows, .vocabulary = s->vocabulary,
212 .initial_latent = r->latent, .schedule = r->schedule, .schedule_count = r->schedule_count,
213 .noise = r->noise, .noise_elements = r->noise_elements, .output_ids = r->staged_ids,
214 .output_capacity = r->staged_capacity, .should_cancel = r->should_cancel, .cancel_context = r->cancel_context,
215 };
216 int status = ck_run_checked_diffusion_schedule(&step);
217 if (status) return status;
218 status = encode_clean(s, r->staged_ids, s->block_rows, 0, r->should_cancel, r->cancel_context);
219 if (status) return status;
220 memcpy(r->output_ids, r->staged_ids, s->block_rows * sizeof(int32_t));
221 return 0;
222}
223
225{
226 if (!r || (uintptr_t)r % _Alignof(ck_diffusion_seeded_request) || !valid_session(s) ||
227 !r->prompt_rows || r->prompt_rows > s->prompt_rows ||
228 !r->token_budget || r->token_budget % s->block_rows ||
230 r->schedule_count < 2 || s->latent_elements > SIZE_MAX / sizeof(double) / r->schedule_count ||
231 !isfinite(r->guidance) || r->staged_capacity < s->block_rows)
232 return -1;
233 const size_t n = s->latent_elements;
234 const int noisy = s->step_bindings.noise != SIZE_MAX;
235 const size_t samples = noisy ? n * r->schedule_count : n;
236 if (r->workspace_elements < samples) return -1;
237 span operands[6];
238 if (!region(&operands[0], r, sizeof(*r), 1) ||
239 !region(&operands[1], r->prompt_ids, r->prompt_rows, sizeof(int32_t)) ||
240 !region(&operands[2], r->schedule, r->schedule_count, sizeof(double)) ||
241 !region(&operands[3], r->workspace, r->workspace_elements, sizeof(double)) ||
242 !region(&operands[4], r->staged_ids, r->staged_capacity, sizeof(int32_t)) ||
243 !region(&operands[5], r->output_ids, r->output_capacity, sizeof(int32_t)))
244 return -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;
249 }
250 for (size_t i = 0; i < r->schedule_count; ++i)
251 if (!isfinite(r->schedule[i]) || (i && r->schedule[i] >= r->schedule[i - 1])) return -1;
252 for (size_t i = 0; i < r->prompt_rows; ++i)
253 if (r->prompt_ids[i] < 0 || (size_t)r->prompt_ids[i] >= s->vocabulary) return -1;
254
255 r->generated_rows = 0;
256 int status = encode_clean(s, r->prompt_ids, r->prompt_rows, 1, r->should_cancel, r->cancel_context);
257 if (status) return status;
258
260 .latent = r->workspace, .schedule = r->schedule, .noise = noisy ? r->workspace + n : NULL,
261 .latent_elements = n, .schedule_count = r->schedule_count, .noise_elements = samples - n,
262 .guidance = r->guidance, .staged_ids = r->staged_ids, .staged_capacity = r->staged_capacity,
263 .output_capacity = s->block_rows, .should_cancel = r->should_cancel, .cancel_context = r->cancel_context,
264 };
265 while (r->generated_rows < r->token_budget) {
266 if (cancelled(r->should_cancel, r->cancel_context)) return -7;
267 const uint64_t domain = ck_rng_gaussian_domain(r->seed, r->generated_rows / s->block_rows);
268 for (size_t i = 0; i < samples; ++i)
269 r->workspace[i] = ck_rng_gaussian_f64(domain, i);
270 block.output_ids = r->output_ids + r->generated_rows;
271 status = ck_generate_diffusion_block(s, &block);
272 if (status) return status;
273 r->generated_rows += s->block_rows;
274 }
275 return 0;
276}
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)
size_t positions
ck_diffusion_arena_entry entry
size_t token_ids
size_t ranges
ck_diffusion_step_bindings step_bindings
const ck_diffusion_cache_plane * planes
const int32_t * ids
Definition tokenizer.h:447
int const int32_t * offsets
Definition true_bpe.h:200
const uint8_t size_t count
Definition true_bpe.h:211
uint32_t end
Definition utf8.c:215