← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
bf16_checked_tensor_ops.c
Go to the documentation of this file.
1#include <math.h>
2#include <stddef.h>
3#include <stdint.h>
4#include <string.h>
5
6#include "bf16_utils.h"
7#include "ckernel_engine.h"
8
9static int valid_span(const void *ptr, size_t count, size_t element)
10{
11 return ptr && (uintptr_t)ptr % element == 0 &&
12 count <= (UINTPTR_MAX - (uintptr_t)ptr) / element;
13}
14
15static int overlaps(const void *a, size_t abytes, const void *b, size_t bbytes)
16{
17 return (uintptr_t)a < (uintptr_t)b + bbytes &&
18 (uintptr_t)b < (uintptr_t)a + abytes;
19}
20
21int ck_prepend_weight_row_bf16_checked(const uint16_t *input, const uint16_t *row,
22 uint16_t *out, size_t input_elements,
23 size_t row_elements, size_t output_elements)
24{
25 if (!row_elements || !input_elements || input_elements % row_elements ||
26 input_elements > SIZE_MAX - row_elements ||
27 output_elements != input_elements + row_elements ||
28 !valid_span(input, input_elements, 2) || !valid_span(row, row_elements, 2) ||
29 !valid_span(out, output_elements, 2) ||
30 overlaps(out, output_elements * 2, input, input_elements * 2) ||
31 overlaps(out, output_elements * 2, row, row_elements * 2))
32 return -2;
33 memcpy(out, row, row_elements * 2);
34 memcpy(out + row_elements, input, input_elements * 2);
35 return 0;
36}
37
38static int binary_bf16(const uint16_t *left, const uint16_t *right, size_t rhs_count,
39 uint16_t *out, uint16_t *stage, size_t n, int multiply)
40{
41 if (!n || !rhs_count || n % rhs_count ||
42 !valid_span(left, n, 2) || !valid_span(right, rhs_count, 2) ||
43 !valid_span(out, n, 2) || !valid_span(stage, n, 2))
44 return -2;
45 const size_t bytes = n * 2;
46 if (overlaps(stage, bytes, left, bytes) || overlaps(stage, bytes, right, rhs_count * 2) ||
47 overlaps(stage, bytes, out, bytes) || overlaps(out, bytes, left, bytes) ||
48 overlaps(out, bytes, right, rhs_count * 2))
49 return -2;
50
51 size_t column = 0;
52 for (size_t i = 0; i < n; ++i) {
53 const float a = bf16_to_float(left[i]), b = bf16_to_float(right[column]);
54 if (++column == rhs_count)
55 column = 0;
56 const uint16_t value = float_to_bf16(multiply ? a * b : a + b);
57 if (!isfinite(a) || !isfinite(b) || !isfinite(bf16_to_float(value)))
58 return -3;
59 stage[i] = value;
60 }
61 memcpy(out, stage, bytes);
62 return 0;
63}
64
65int ck_add_bf16_checked(const uint16_t *left, const uint16_t *right,
66 uint16_t *out, uint16_t *stage, size_t elements)
67{
68 return binary_bf16(left, right, elements, out, stage, elements, 0);
69}
70
71int ck_mul_bf16_checked(const uint16_t *left, const uint16_t *right,
72 uint16_t *out, uint16_t *stage, size_t elements)
73{
74 return binary_bf16(left, right, elements, out, stage, elements, 1);
75}
76
77int ck_bias_rows_bf16_checked(const uint16_t *input, const uint16_t *bias,
78 uint16_t *out, uint16_t *stage, size_t elements,
79 size_t bias_elements)
80{
81 return binary_bf16(input, bias, bias_elements, out, stage, elements, 0);
82}
83
84int ck_scale_bf16_checked(const uint16_t *input, const uint16_t *weight,
85 uint16_t *out, uint16_t *stage, size_t elements, size_t gain_elements)
86{
87 if (gain_elements != 1)
88 return -2;
89 return binary_bf16(input, weight, 1, out, stage, elements, 1);
90}
91
92int ck_scale_rows_bf16_checked(const uint16_t *input, const uint16_t *weight,
93 uint16_t *out, uint16_t *stage, size_t elements, size_t gain_elements)
94{
95 return binary_bf16(input, weight, gain_elements, out, stage, elements, 1);
96}
97
98int ck_widen_bf16_checked(const uint16_t *input, float *out, size_t elements)
99{
100 if (!elements || !valid_span(input, elements, sizeof(*input)) ||
101 !valid_span(out, elements, sizeof(*out)) ||
102 overlaps(input, elements * sizeof(*input), out, elements * sizeof(*out)))
103 return -2;
104 for (size_t i = 0; i < elements; ++i)
105 if (!isfinite(bf16_to_float(input[i])))
106 return -3;
107 for (size_t i = 0; i < elements; ++i)
108 out[i] = bf16_to_float(input[i]);
109 return 0;
110}
111
112int ck_relu2_bf16_checked(const uint16_t *input, uint16_t *out,
113 float *stage, size_t elements)
114{
115 if (!elements || !valid_span(input, elements, 2) ||
116 !valid_span(out, elements, 2) || !valid_span(stage, elements, 4))
117 return -2;
118 const size_t bytes = elements * sizeof(*input);
119 const size_t stage_bytes = elements * sizeof(*stage);
120 if (overlaps(input, bytes, out, bytes) || overlaps(input, bytes, stage, stage_bytes) ||
121 overlaps(out, bytes, stage, stage_bytes))
122 return -2;
123 for (size_t i = 0; i < elements; ++i) {
124 const float x = bf16_to_float(input[i]);
125 const float positive = x > 0.0f ? x : 0.0f;
126 stage[i] = positive * positive;
127 if (!isfinite(x) || !isfinite(bf16_to_float(float_to_bf16(stage[i]))))
128 return -3;
129 }
130 for (size_t i = 0; i < elements; ++i)
131 out[i] = float_to_bf16(stage[i]);
132 return 0;
133}
134
135static int gated_bf16(const uint16_t *gate, const uint16_t *up, uint16_t *out,
136 float *stage, size_t n, void (*activate)(float *, size_t))
137{
138 if (!n || !valid_span(gate, n, 2) || !valid_span(up, n, 2) ||
139 !valid_span(out, n, 2) || !valid_span(stage, n, 4))
140 return -2;
141 if (overlaps(stage, n * 4, gate, n * 2) || overlaps(stage, n * 4, up, n * 2) ||
142 overlaps(stage, n * 4, out, n * 2) || overlaps(out, n * 2, gate, n * 2) ||
143 overlaps(out, n * 2, up, n * 2))
144 return -2;
145 for (size_t i = 0; i < n; ++i) {
146 stage[i] = bf16_to_float(gate[i]);
147 if (!isfinite(stage[i]) || !isfinite(bf16_to_float(up[i])))
148 return -3;
149 }
150 activate(stage, n);
151 for (size_t i = 0; i < n; ++i) {
152 stage[i] *= bf16_to_float(up[i]);
153 if (!isfinite(bf16_to_float(float_to_bf16(stage[i]))))
154 return -3;
155 }
156 for (size_t i = 0; i < n; ++i)
157 out[i] = float_to_bf16(stage[i]);
158 return 0;
159}
160
161int ck_geglu_tanh_bf16_checked(const uint16_t *gate, const uint16_t *up,
162 uint16_t *out, float *stage, size_t elements)
163{
164 return gated_bf16(gate, up, out, stage, elements, gelu_pytorch_tanh_bf16_storage);
165}
166
167static void silu_bf16_storage(float *values, size_t count)
168{
169 for (size_t i = 0; i < count; ++i) {
170 const float x = values[i];
171 values[i] = bf16_to_float(float_to_bf16(x / (1.0f + expf(-x))));
172 }
173}
174
175int ck_swiglu_bf16_checked(const uint16_t *gate, const uint16_t *up,
176 uint16_t *out, float *stage, size_t elements)
177{
178 return gated_bf16(gate, up, out, stage, elements, silu_bf16_storage);
179}
180
181static void sigmoid_bf16_storage(float *values, size_t count)
182{
183 for (size_t i = 0; i < count; ++i)
184 values[i] = bf16_to_float(float_to_bf16(1.0f / (1.0f + expf(-values[i]))));
185}
186
187int ck_sigmoid_mul_bf16_checked(const uint16_t *gate, const uint16_t *up,
188 uint16_t *out, float *stage, size_t elements)
189{
190 return gated_bf16(gate, up, out, stage, elements, sigmoid_bf16_storage);
191}
192
194 const uint16_t *input, const void *positions, const float *inv_freq,
195 uint16_t *out, uint16_t *stage, size_t tokens, size_t heads, size_t dim,
196 size_t elements, size_t frequencies, size_t axes, size_t position_elements,
197 size_t axis1_pairs, size_t axis2_pairs, int fp32_positions)
198{
199 if (!tokens || !heads || !dim || dim % 2 || tokens > SIZE_MAX / heads ||
200 tokens * heads > SIZE_MAX / dim || tokens > SIZE_MAX / axes ||
201 position_elements != tokens * axes)
202 return -2;
203 const size_t count = tokens * heads * dim;
204 const size_t half = dim / 2;
205 if (elements != count || frequencies != half ||
206 axis1_pairs > (half + 1) / 3 || axis2_pairs > half / 3)
207 return -2;
208 if (!valid_span(input, count, 2) || !valid_span(out, count, 2) ||
209 !valid_span(stage, count, 2) || !valid_span(positions, position_elements, 4) ||
210 !valid_span(inv_freq, half, 4))
211 return -2;
212 const size_t bytes = count * 2;
213 const size_t position_bytes = position_elements * 4;
214 if (overlaps(stage, bytes, out, bytes) || overlaps(stage, bytes, input, bytes) ||
215 overlaps(stage, bytes, positions, position_bytes) || overlaps(stage, bytes, inv_freq, half * 4) ||
216 overlaps(out, bytes, input, bytes) || overlaps(out, bytes, positions, position_bytes) ||
217 overlaps(out, bytes, inv_freq, half * 4))
218 return -2;
219 for (size_t d = 0; d < half; ++d)
220 if (!isfinite(inv_freq[d]) || inv_freq[d] < 0.0f)
221 return -3;
222 for (size_t t = 0; t < tokens; ++t) {
223 float position[3];
224 for (size_t axis = 0; axis < axes; ++axis) {
225 const size_t index = t * axes + axis;
226 position[axis] = fp32_positions ? ((const float *)positions)[index]
227 : (float)((const int32_t *)positions)[index];
228 if (!isfinite(position[axis]) || position[axis] < 0.0f)
229 return -2;
230 }
231 for (size_t d = 0; d < half; ++d) {
232 size_t axis = 0;
233 if (axes == 3) {
234 if (d % 3 == 1 && d / 3 < axis1_pairs)
235 axis = 1;
236 else if (d % 3 == 2 && d / 3 < axis2_pairs)
237 axis = 2;
238 }
239 const float angle = position[axis] * inv_freq[d];
240 if (!isfinite(angle))
241 return -3;
242 const float cosine = bf16_to_float(float_to_bf16(cosf(angle)));
243 const float sine = bf16_to_float(float_to_bf16(sinf(angle)));
244 for (size_t h = 0; h < heads; ++h) {
245 const size_t i = (t * heads + h) * dim + d;
246 const float x = bf16_to_float(input[i]), y = bf16_to_float(input[i + half]);
247 if (!isfinite(x) || !isfinite(y))
248 return -3;
249 const float xc = bf16_to_float(float_to_bf16(x * cosine));
250 const float ys = bf16_to_float(float_to_bf16(y * sine));
251 const float yc = bf16_to_float(float_to_bf16(y * cosine));
252 const float xs = bf16_to_float(float_to_bf16(x * sine));
253 stage[i] = float_to_bf16(xc - ys);
254 stage[i + half] = float_to_bf16(yc + xs);
255 if (!isfinite(bf16_to_float(stage[i])) || !isfinite(bf16_to_float(stage[i + half])))
256 return -3;
257 }
258 }
259 }
260 memcpy(out, stage, bytes);
261 return 0;
262}
263
265 const uint16_t *input, const int32_t *positions, const float *inv_freq,
266 uint16_t *out, uint16_t *stage, size_t tokens, size_t heads, size_t dim,
267 size_t elements, size_t frequencies)
268{
269 return rope_split_positions_bf16_checked(input, positions, inv_freq, out, stage,
270 tokens, heads, dim, elements, frequencies, 1, tokens, 0, 0, 0);
271}
272
274 const uint16_t *input, const int32_t *positions, const float *inv_freq,
275 uint16_t *out, uint16_t *stage, size_t tokens, size_t heads, size_t dim,
276 size_t elements, size_t frequencies, size_t position_elements,
277 size_t axis1_pairs, size_t axis2_pairs)
278{
279 return rope_split_positions_bf16_checked(input, positions, inv_freq, out, stage,
280 tokens, heads, dim, elements, frequencies, 3, position_elements, axis1_pairs, axis2_pairs, 0);
281}
282
284 const uint16_t *input, const float *positions, const float *inv_freq,
285 uint16_t *out, uint16_t *stage, size_t tokens, size_t heads, size_t dim,
286 size_t elements, size_t frequencies, size_t position_elements,
287 size_t axis1_pairs, size_t axis2_pairs)
288{
289 return rope_split_positions_bf16_checked(input, positions, inv_freq, out, stage,
290 tokens, heads, dim, elements, frequencies, 3, position_elements, axis1_pairs, axis2_pairs, 1);
291}
292
293int ck_rope_interleaved_partial_bf16_checked(const uint16_t *input, const int32_t *positions,
294 const float *inv_freq, uint16_t *out, uint16_t *stage,
295 size_t tokens, size_t heads, size_t dim,
296 size_t elements, size_t frequencies)
297{
298 if (!tokens || !heads || !dim || !frequencies || frequencies > dim / 2 ||
299 tokens > SIZE_MAX / heads || tokens * heads > SIZE_MAX / dim ||
300 elements != tokens * heads * dim)
301 return -2;
302 if (!valid_span(input, elements, 2) || !valid_span(out, elements, 2) ||
303 !valid_span(stage, elements, 2) || !valid_span(positions, tokens, 4) ||
304 !valid_span(inv_freq, frequencies, 4))
305 return -2;
306 const size_t bytes = elements * 2;
307 if (overlaps(out, bytes, stage, bytes))
308 return -2;
309 const void *operands[] = {input, positions, inv_freq};
310 const size_t sizes[] = {bytes, tokens * 4, frequencies * 4};
311 for (size_t i = 0; i < 3; ++i)
312 if (overlaps(out, bytes, operands[i], sizes[i]) ||
313 overlaps(stage, bytes, operands[i], sizes[i]))
314 return -2;
315
316 for (size_t i = 0; i < elements; ++i)
317 if (!isfinite(bf16_to_float(input[i])))
318 return -3;
319 memcpy(stage, input, bytes);
320 for (size_t token = 0; token < tokens; ++token) {
321 if (positions[token] < 0)
322 return -2;
323 for (size_t pair = 0; pair < frequencies; ++pair) {
324 const float angle = (float)positions[token] * inv_freq[pair];
325 if (!isfinite(angle) || inv_freq[pair] < 0.0f)
326 return -3;
327 const float cosine = cosf(angle), sine = sinf(angle);
328 for (size_t head = 0; head < heads; ++head) {
329 const size_t i = (token * heads + head) * dim + pair * 2;
330 const float x = bf16_to_float(input[i]), y = bf16_to_float(input[i + 1]);
331 const volatile float xc = x * cosine, ys = y * sine;
332 const volatile float yc = y * cosine, xs = x * sine;
333 const uint16_t even = float_to_bf16(xc - ys), odd = float_to_bf16(yc + xs);
334 if (!isfinite(bf16_to_float(even)) || !isfinite(bf16_to_float(odd)))
335 return -3;
336 stage[i] = even;
337 stage[i + 1] = odd;
338 }
339 }
340 }
341 memcpy(out, stage, bytes);
342 return 0;
343}
344
345int ck_scale_scalar_f32_bf16_checked(const uint16_t *input, uint16_t *out,
346 uint16_t *stage, size_t elements, float scale)
347{
348 if (!elements || !isfinite(scale) || !valid_span(input, elements, 2) ||
349 !valid_span(out, elements, 2) || !valid_span(stage, elements, 2))
350 return -2;
351 const size_t bytes = elements * 2;
352 if (overlaps(input, bytes, out, bytes) || overlaps(input, bytes, stage, bytes) ||
353 overlaps(stage, bytes, out, bytes))
354 return -2;
355 for (size_t i = 0; i < elements; ++i) {
356 const float value = bf16_to_float(input[i]);
357 stage[i] = float_to_bf16(value * scale);
358 if (!isfinite(value) || !isfinite(bf16_to_float(stage[i])))
359 return -3;
360 }
361 memcpy(out, stage, bytes);
362 return 0;
363}
364
365int ck_argmax_values_bf16_checked(const uint16_t *values, int32_t *index,
366 size_t elements, size_t index_elements)
367{
368 if (!elements || elements > INT32_MAX || index_elements != 1 || !valid_span(values, elements, 2) ||
369 !valid_span(index, 1, 4) || overlaps(values, elements * 2, index, 4))
370 return -2;
371 size_t best = 0;
372 float maximum = -INFINITY;
373 for (size_t i = 0; i < elements; ++i) {
374 const float value = bf16_to_float(values[i]);
375 if (!isfinite(value))
376 return -3;
377 if (value > maximum) {
378 maximum = value;
379 best = i;
380 }
381 }
382 *index = (int32_t)best;
383 return 0;
384}
385
386int ck_narrow_f32_bf16_checked(const float *input, uint16_t *out, size_t elements)
387{
388 if (!elements || !valid_span(input, elements, 4) || !valid_span(out, elements, 2) ||
389 overlaps(input, elements * 4, out, elements * 2))
390 return -2;
391 for (size_t i = 0; i < elements; ++i)
392 if (!isfinite(bf16_to_float(float_to_bf16(input[i]))))
393 return -3;
394 for (size_t i = 0; i < elements; ++i)
395 out[i] = float_to_bf16(input[i]);
396 return 0;
397}
398
399/* Table-driven split-half RoPE. The tables share the rows of the input,
400 * broadcast across heads, and contain only the unique half of each angle. */
402 const uint16_t *input, const uint16_t *cosine, const uint16_t *sine,
403 uint16_t *out, uint16_t *stage, size_t tokens, size_t heads, size_t dim,
404 size_t rotary_dim, size_t elements, size_t table_elements)
405{
406 if (!tokens || !heads || !dim || !rotary_dim || rotary_dim > dim || rotary_dim % 2 ||
407 tokens > SIZE_MAX / heads || tokens * heads > SIZE_MAX / dim)
408 return -2;
409 const size_t half = rotary_dim / 2;
410 if (elements != tokens * heads * dim || table_elements != tokens * half ||
411 !valid_span(input, elements, 2) || !valid_span(out, elements, 2) ||
412 !valid_span(stage, elements, 2) || !valid_span(cosine, table_elements, 2) ||
413 !valid_span(sine, table_elements, 2))
414 return -2;
415 const size_t bytes = elements * 2;
416 const void *operands[] = {input, cosine, sine};
417 const size_t lengths[] = {bytes, table_elements * 2, table_elements * 2};
418 if (overlaps(stage, bytes, out, bytes))
419 return -2;
420 for (size_t i = 0; i < 3; ++i) {
421 if (overlaps(stage, bytes, operands[i], lengths[i]) ||
422 overlaps(out, bytes, operands[i], lengths[i]))
423 return -2;
424 }
425 for (size_t t = 0; t < tokens; ++t) {
426 for (size_t h = 0; h < heads; ++h) {
427 const size_t base = (t * heads + h) * dim;
428 for (size_t d = 0; d < half; ++d) {
429 const size_t i = base + d, table = t * half + d;
430 const float x = bf16_to_float(input[i]), y = bf16_to_float(input[i + half]);
431 const float c = bf16_to_float(cosine[table]), s = bf16_to_float(sine[table]);
432 const float xc = bf16_to_float(float_to_bf16(x * c));
433 const float ys = bf16_to_float(float_to_bf16(y * s));
434 const float yc = bf16_to_float(float_to_bf16(y * c));
435 const float xs = bf16_to_float(float_to_bf16(x * s));
436 stage[i] = float_to_bf16(xc - ys);
437 stage[i + half] = float_to_bf16(yc + xs);
438 if (!isfinite(bf16_to_float(stage[i])) || !isfinite(bf16_to_float(stage[i + half])))
439 return -3;
440 }
441 for (size_t d = rotary_dim; d < dim; ++d) {
442 const uint16_t value = input[base + d];
443 if (!isfinite(bf16_to_float(value)))
444 return -3;
445 stage[base + d] = value;
446 }
447 }
448 }
449 memcpy(out, stage, bytes);
450 return 0;
451}
452
453int ck_slice_bf16_checked(const uint16_t *input, uint16_t *out,
454 size_t input_elements, size_t offset, size_t elements)
455{
456 if (!elements || offset > input_elements || elements > input_elements - offset ||
457 !valid_span(input, input_elements, sizeof(*input)) ||
458 !valid_span(out, elements, sizeof(*out)) ||
459 overlaps(input, input_elements * sizeof(*input), out, elements * sizeof(*out)))
460 return -2;
461 memcpy(out, input + offset, elements * sizeof(*out));
462 return 0;
463}
464
465/* Validate all row IDs and mutable spans before staging either transform. */
466static int indexed_transform_valid(const uint16_t *input, const int32_t *indices,
467 const uint16_t *scale, const uint16_t *other, size_t other_count,
468 uint16_t *out, uint16_t *stage, size_t rows, size_t width, size_t banks,
469 size_t elements, size_t bank_elements)
470{
471 if (!rows || !width || !banks || rows > SIZE_MAX / width || banks > SIZE_MAX / width ||
472 elements != rows * width || bank_elements != banks * width ||
473 !valid_span(input, elements, 2) || !valid_span(indices, rows, 4) ||
474 !valid_span(scale, bank_elements, 2) || !valid_span(other, other_count, 2) ||
475 !valid_span(out, elements, 2) || !valid_span(stage, elements, 2))
476 return 0;
477
478 const size_t bytes = elements * sizeof(*out);
479 const void *inputs[] = {input, indices, scale, other};
480 const size_t sizes[] = {bytes, rows * sizeof(*indices),
481 bank_elements * sizeof(*scale), other_count * sizeof(*other)};
482 if (overlaps(stage, bytes, out, bytes))
483 return 0;
484 for (size_t i = 0; i < sizeof(inputs) / sizeof(inputs[0]); ++i) {
485 if (overlaps(stage, bytes, inputs[i], sizes[i]) || overlaps(out, bytes, inputs[i], sizes[i]))
486 return 0;
487 }
488 for (size_t r = 0; r < rows; ++r) {
489 if (indices[r] < 0 || (size_t)indices[r] >= banks)
490 return 0;
491 }
492 return 1;
493}
494
495/* Apply one learned scale and shift per row ID with BF16 stage boundaries. */
497 const uint16_t *input, const int32_t *indices, const uint16_t *scale,
498 const uint16_t *shift, uint16_t *out, uint16_t *stage,
499 size_t rows, size_t width, size_t banks, size_t elements, size_t bank_elements)
500{
501 if (!indexed_transform_valid(input, indices, scale, shift, bank_elements, out, stage,
502 rows, width, banks, elements, bank_elements))
503 return -2;
504 const size_t bytes = elements * sizeof(*out);
505 for (size_t r = 0; r < rows; ++r) {
506 const size_t bank = (size_t)indices[r] * width;
507 for (size_t c = 0; c < width; ++c) {
508 const float gain = bf16_to_float(float_to_bf16(1.0f + bf16_to_float(scale[bank + c])));
509 const float value = bf16_to_float(input[r * width + c]);
510 const float product = bf16_to_float(float_to_bf16(value * gain));
511 const uint16_t result = float_to_bf16(product + bf16_to_float(shift[bank + c]));
512 if (!isfinite(product) || !isfinite(bf16_to_float(result)))
513 return -3;
514 stage[r * width + c] = result;
515 }
516 }
517 memcpy(out, stage, bytes);
518 return 0;
519}
520
522 const uint16_t *input, const int32_t *indices, const uint16_t *scale,
523 const uint16_t *update, uint16_t *out, uint16_t *stage,
524 size_t rows, size_t width, size_t banks, size_t elements, size_t bank_elements)
525{
526 if (!indexed_transform_valid(input, indices, scale, update, elements, out, stage,
527 rows, width, banks, elements, bank_elements))
528 return -2;
529 const size_t bytes = elements * sizeof(*out);
530 for (size_t r = 0; r < rows; ++r) {
531 const size_t bank = (size_t)indices[r] * width;
532 for (size_t c = 0; c < width; ++c) {
533 const size_t i = r * width + c;
534 const float product = bf16_to_float(float_to_bf16(
535 bf16_to_float(update[i]) * bf16_to_float(scale[bank + c])));
536 const uint16_t result = float_to_bf16(bf16_to_float(input[i]) + product);
537 if (!isfinite(product) || !isfinite(bf16_to_float(result)))
538 return -3;
539 stage[i] = result;
540 }
541 }
542 memcpy(out, stage, bytes);
543 return 0;
544}
545
546int ck_silu_bf16_checked(const uint16_t *input, uint16_t *out,
547 uint16_t *stage, size_t elements)
548{
549 if (!elements || !valid_span(input, elements, 2) ||
550 !valid_span(out, elements, 2) || !valid_span(stage, elements, 2))
551 return -2;
552 const size_t bytes = elements * sizeof(*input);
553 if (overlaps(input, bytes, out, bytes) || overlaps(input, bytes, stage, bytes) ||
554 overlaps(out, bytes, stage, bytes))
555 return -2;
556 for (size_t i = 0; i < elements; ++i) {
557 const float x = bf16_to_float(input[i]);
558 if (!isfinite(x))
559 return -3;
560 stage[i] = float_to_bf16(x / (1.0f + expf(-x)));
561 }
562 memcpy(out, stage, bytes);
563 return 0;
564}
static int product(size_t a, size_t b, size_t *out)
int ck_mul_bf16_checked(const uint16_t *left, const uint16_t *right, uint16_t *out, uint16_t *stage, size_t elements)
int ck_rope_split_tables_bf16_checked(const uint16_t *input, const uint16_t *cosine, const uint16_t *sine, uint16_t *out, uint16_t *stage, size_t tokens, size_t heads, size_t dim, size_t rotary_dim, size_t elements, size_t table_elements)
int ck_indexed_affine_bf16_checked(const uint16_t *input, const int32_t *indices, const uint16_t *scale, const uint16_t *shift, uint16_t *out, uint16_t *stage, size_t rows, size_t width, size_t banks, size_t elements, size_t bank_elements)
int ck_scale_rows_bf16_checked(const uint16_t *input, const uint16_t *weight, uint16_t *out, uint16_t *stage, size_t elements, size_t gain_elements)
int ck_widen_bf16_checked(const uint16_t *input, float *out, size_t elements)
int ck_swiglu_bf16_checked(const uint16_t *gate, const uint16_t *up, uint16_t *out, float *stage, size_t elements)
int ck_scale_scalar_f32_bf16_checked(const uint16_t *input, uint16_t *out, uint16_t *stage, size_t elements, float scale)
int ck_add_bf16_checked(const uint16_t *left, const uint16_t *right, uint16_t *out, uint16_t *stage, size_t elements)
int ck_rope_split_interleaved_3d_fp32_pos_bf16_checked(const uint16_t *input, const float *positions, const float *inv_freq, uint16_t *out, uint16_t *stage, size_t tokens, size_t heads, size_t dim, size_t elements, size_t frequencies, size_t position_elements, size_t axis1_pairs, size_t axis2_pairs)
int ck_prepend_weight_row_bf16_checked(const uint16_t *input, const uint16_t *row, uint16_t *out, size_t input_elements, size_t row_elements, size_t output_elements)
int ck_rope_split_interleaved_3d_bf16_checked(const uint16_t *input, const int32_t *positions, const float *inv_freq, uint16_t *out, uint16_t *stage, size_t tokens, size_t heads, size_t dim, size_t elements, size_t frequencies, size_t position_elements, size_t axis1_pairs, size_t axis2_pairs)
static int rope_split_positions_bf16_checked(const uint16_t *input, const void *positions, const float *inv_freq, uint16_t *out, uint16_t *stage, size_t tokens, size_t heads, size_t dim, size_t elements, size_t frequencies, size_t axes, size_t position_elements, size_t axis1_pairs, size_t axis2_pairs, int fp32_positions)
static int overlaps(const void *a, size_t abytes, const void *b, size_t bbytes)
int ck_sigmoid_mul_bf16_checked(const uint16_t *gate, const uint16_t *up, uint16_t *out, float *stage, size_t elements)
int ck_narrow_f32_bf16_checked(const float *input, uint16_t *out, size_t elements)
int ck_rope_split_positions_bf16_checked(const uint16_t *input, const int32_t *positions, const float *inv_freq, uint16_t *out, uint16_t *stage, size_t tokens, size_t heads, size_t dim, size_t elements, size_t frequencies)
int ck_geglu_tanh_bf16_checked(const uint16_t *gate, const uint16_t *up, uint16_t *out, float *stage, size_t elements)
static void sigmoid_bf16_storage(float *values, size_t count)
int ck_bias_rows_bf16_checked(const uint16_t *input, const uint16_t *bias, uint16_t *out, uint16_t *stage, size_t elements, size_t bias_elements)
int ck_rope_interleaved_partial_bf16_checked(const uint16_t *input, const int32_t *positions, const float *inv_freq, uint16_t *out, uint16_t *stage, size_t tokens, size_t heads, size_t dim, size_t elements, size_t frequencies)
static int gated_bf16(const uint16_t *gate, const uint16_t *up, uint16_t *out, float *stage, size_t n, void(*activate)(float *, size_t))
static void silu_bf16_storage(float *values, size_t count)
int ck_argmax_values_bf16_checked(const uint16_t *values, int32_t *index, size_t elements, size_t index_elements)
int ck_indexed_residual_bf16_checked(const uint16_t *input, const int32_t *indices, const uint16_t *scale, const uint16_t *update, uint16_t *out, uint16_t *stage, size_t rows, size_t width, size_t banks, size_t elements, size_t bank_elements)
int ck_relu2_bf16_checked(const uint16_t *input, uint16_t *out, float *stage, size_t elements)
static int indexed_transform_valid(const uint16_t *input, const int32_t *indices, const uint16_t *scale, const uint16_t *other, size_t other_count, uint16_t *out, uint16_t *stage, size_t rows, size_t width, size_t banks, size_t elements, size_t bank_elements)
int ck_slice_bf16_checked(const uint16_t *input, uint16_t *out, size_t input_elements, size_t offset, size_t elements)
int ck_scale_bf16_checked(const uint16_t *input, const uint16_t *weight, uint16_t *out, uint16_t *stage, size_t elements, size_t gain_elements)
static int binary_bf16(const uint16_t *left, const uint16_t *right, size_t rhs_count, uint16_t *out, uint16_t *stage, size_t n, int multiply)
static int valid_span(const void *ptr, size_t count, size_t element)
int ck_silu_bf16_checked(const uint16_t *input, uint16_t *out, uint16_t *stage, size_t elements)
static uint16_t float_to_bf16(float f)
Definition bf16_utils.h:90
static float bf16_to_float(uint16_t v)
Definition bf16_utils.h:38
void gelu_pytorch_tanh_bf16_storage(float *data, size_t n)
const char * token
Definition tokenizer.h:308
const char * left
Definition true_bpe.h:140
const uint8_t size_t count
Definition true_bpe.h:211
const char const char * right
Definition true_bpe.h:141