← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
relative_position_terms_bf16_checked.c
Go to the documentation of this file.
1#include "bf16_utils.h"
2
3static int valid(const uint16_t *p, size_t n)
4{
5 return p && (uintptr_t)p % sizeof(*p) == 0 &&
6 (uintptr_t)p <= UINTPTR_MAX - n * sizeof(*p);
7}
8
9static int overlaps(const uint16_t *a, size_t na, const uint16_t *b, size_t nb)
10{
11 return (uintptr_t)a < (uintptr_t)b + nb * sizeof(*b) &&
12 (uintptr_t)b < (uintptr_t)a + na * sizeof(*a);
13}
14
15static int finite_bf16(const uint16_t *p, size_t n)
16{
17 for (size_t i = 0; i < n; ++i)
18 if ((p[i] & 0x7f80u) == 0x7f80u)
19 return 0;
20 return 1;
21}
22
23static uint16_t relative_dot(const uint16_t *q, const uint16_t *table, size_t channels)
24{
25 double sum = 0.0;
26 for (size_t c = 0; c < channels; ++c)
27 sum += (double)bf16_to_float(q[c]) * (double)bf16_to_float(table[c]);
28 return float_to_bf16((float)sum);
29}
30
32 const uint16_t *query, const uint16_t *height_table, const uint16_t *width_table,
33 uint16_t *terms, size_t query_elements, size_t height_elements, size_t width_elements,
34 size_t term_elements, size_t planes, size_t height, size_t width, size_t channels)
35{
36 if (!planes || planes > 384 || !height || height > 64 || !width || width > 64 ||
37 !channels || channels > 128)
38 return -2;
39 const size_t pixels = height * width, rows = planes * pixels, stride = height + width;
40 if (rows > 64 * 64 * 64 || query_elements != rows * channels ||
41 height_elements != (2 * height - 1) * channels ||
42 width_elements != (2 * width - 1) * channels || term_elements != rows * stride)
43 return -2;
44 if (!valid(query, query_elements) || !valid(height_table, height_elements) ||
45 !valid(width_table, width_elements) || !valid(terms, term_elements) ||
46 overlaps(terms, term_elements, query, query_elements) ||
47 overlaps(terms, term_elements, height_table, height_elements) ||
48 overlaps(terms, term_elements, width_table, width_elements))
49 return -1;
50 if (!finite_bf16(query, query_elements) || !finite_bf16(height_table, height_elements) ||
51 !finite_bf16(width_table, width_elements))
52 return -3;
53
54 for (size_t row = 0; row < rows; ++row) {
55 const size_t y = row % pixels / width, x = row % width;
56 const uint16_t *q = query + row * channels;
57 uint16_t *dst = terms + row * stride;
58 // Materialize only the two axes, in key order, not a dense attention bias.
59 for (size_t ky = 0; ky < height; ++ky)
60 dst[ky] = relative_dot(q, height_table + (y + height - 1 - ky) * channels, channels);
61 for (size_t kx = 0; kx < width; ++kx)
62 dst[height + kx] = relative_dot(q, width_table + (x + width - 1 - kx) * channels, channels);
63 }
64 return 0;
65}
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
static int valid(const uint16_t *p, size_t n)
static int finite_bf16(const uint16_t *p, size_t n)
static uint16_t relative_dot(const uint16_t *q, const uint16_t *table, size_t channels)
static int overlaps(const uint16_t *a, size_t na, const uint16_t *b, size_t nb)
int ck_relative_position_terms_bf16_checked(const uint16_t *query, const uint16_t *height_table, const uint16_t *width_table, uint16_t *terms, size_t query_elements, size_t height_elements, size_t width_elements, size_t term_elements, size_t planes, size_t height, size_t width, size_t channels)