← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
attention_spatial_terms_checked.c
Go to the documentation of this file.
1#include "bf16_utils.h"
2#include "checked_parallel.h"
3
4#include <float.h>
5#include <math.h>
6#include <stdatomic.h>
7#include <string.h>
8
9static int valid_region(const void *p, size_t n, size_t width)
10{
11 return p && (uintptr_t)p % width == 0 && (uintptr_t)p <= UINTPTR_MAX - n * width;
12}
13
14static int overlaps(const void *a, size_t na, const void *b, size_t nb)
15{
16 return (uintptr_t)a < (uintptr_t)b + nb && (uintptr_t)b < (uintptr_t)a + na;
17}
18
19static double dot(const uint16_t *q, const uint16_t *k, size_t channels)
20{
21 double sum = 0.0;
22 for (size_t c = 0; c < channels; ++c)
23 sum += (double)bf16_to_float(q[c]) * bf16_to_float(k[c]);
24 return sum;
25}
26
27struct spatial_attention {
28 const uint16_t *query, *key, *value, *terms;
29 uint16_t *staged;
30 float *scores;
31 size_t rows, height, width, channels, lanes;
32 atomic_int failed;
33};
34
35static int attend_rows(const struct spatial_attention *a, size_t begin, size_t end, float *scores)
36{
37 const size_t height = a->height, width = a->width, channels = a->channels;
38 const size_t tokens = height * width, axes = height + width;
39 const float scale = 1.0f / sqrtf((float)channels);
40 for (size_t row = begin; row < end; ++row) {
41 const size_t base = row / tokens * tokens * channels;
42 const uint16_t *q = a->query + row * channels;
43 const uint16_t *t = a->terms + row * axes;
44 float largest = -INFINITY;
45 for (size_t y = 0, j = 0; y < height; ++y) {
46 const float h = bf16_to_float(t[y]);
47 for (size_t x = 0; x < width; ++x, ++j) {
48 // The reference adds the two stored BF16 terms before the FP32 score.
49 const float bias = bf16_to_float(float_to_bf16(h + bf16_to_float(t[height + x])));
50 const double score = dot(q, a->key + base + j * channels, channels) * scale + bias;
51 if (!isfinite(score) || fabs(score) > FLT_MAX)
52 return -3;
53 scores[j] = (float)score;
54 if (scores[j] > largest)
55 largest = scores[j];
56 }
57 }
58 double denom = 0.0;
59 for (size_t j = 0; j < tokens; ++j) {
60 scores[j] = (float)exp((double)scores[j] - largest);
61 denom += scores[j];
62 }
63 for (size_t c = 0; c < channels; ++c) {
64 double sum = 0.0;
65 for (size_t j = 0; j < tokens; ++j)
66 sum += (double)scores[j] * bf16_to_float(a->value[base + j * channels + c]);
67 const uint16_t result = float_to_bf16((float)(sum / denom));
68 if ((result & 0x7f80u) == 0x7f80u)
69 return -3;
70 a->staged[row * channels + c] = result;
71 }
72 }
73 return 0;
74}
75
76static void attend_lanes(size_t begin, size_t end, void *ctx)
77{
78 struct spatial_attention *a = ctx;
79 const size_t tokens = a->height * a->width;
80 for (size_t lane = begin; lane < end; ++lane) {
81 if (atomic_load_explicit(&a->failed, memory_order_relaxed))
82 return;
83 if (attend_rows(a, a->rows * lane / a->lanes,
84 a->rows * (lane + 1) / a->lanes, a->scores + lane * tokens)) {
85 atomic_store_explicit(&a->failed, 1, memory_order_relaxed);
86 return;
87 }
88 }
89}
90
92 const uint16_t *query, const uint16_t *key, const uint16_t *value,
93 const uint16_t *terms, uint16_t *output, float *scores, uint16_t *staged,
94 size_t qkv_elements, size_t term_elements, size_t score_elements,
95 size_t planes, size_t height, size_t width, size_t channels)
96{
97 if (!planes || planes > 384 || !height || height > 64 || !width || width > 64 ||
98 !channels || channels > 128)
99 return -2;
100 const size_t tokens = height * width, rows = planes * tokens, axes = height + width;
101 const size_t lanes = score_elements / tokens;
102 if (rows > 64 * 64 * 64 || qkv_elements != rows * channels ||
103 term_elements != rows * axes || score_elements % tokens ||
104 !lanes || lanes > CK_THREADPOOL_MAX_THREADS || lanes > rows)
105 return -2;
106
107 const size_t qkv_bytes = qkv_elements * sizeof(*query);
108 const size_t score_bytes = score_elements * sizeof(*scores);
109 const void *input[] = {query, key, value, terms};
110 const size_t count[] = {qkv_elements, qkv_elements, qkv_elements, term_elements};
111 if (!valid_region(output, qkv_elements, sizeof(*output)) ||
112 !valid_region(staged, qkv_elements, sizeof(*staged)) ||
113 !valid_region(scores, score_elements, sizeof(*scores)) ||
114 overlaps(output, qkv_bytes, staged, qkv_bytes) ||
115 overlaps(output, qkv_bytes, scores, score_bytes) ||
116 overlaps(staged, qkv_bytes, scores, score_bytes))
117 return -1;
118 for (size_t i = 0; i < 4; ++i) {
119 const size_t bytes = count[i] * sizeof(*query);
120 if (!valid_region(input[i], count[i], sizeof(*query)) ||
121 overlaps(input[i], bytes, output, qkv_bytes) ||
122 overlaps(input[i], bytes, staged, qkv_bytes) ||
123 overlaps(input[i], bytes, scores, score_bytes))
124 return -1;
125 const uint16_t *bits = input[i];
126 for (size_t j = 0; j < count[i]; ++j)
127 if ((bits[j] & 0x7f80u) == 0x7f80u)
128 return -3;
129 }
130
131 struct spatial_attention attention = {
132 query, key, value, terms, staged, scores, rows, height, width, channels, lanes, 0,
133 };
134 ck_checked_parallel_for(lanes, 1, attend_lanes, &attention);
135 if (atomic_load_explicit(&attention.failed, memory_order_relaxed))
136 return -3;
137 memcpy(output, staged, qkv_bytes);
138 return 0;
139}
static int overlaps(const void *a, size_t na, const void *b, size_t nb)
int ck_attention_spatial_terms_bf16_checked(const uint16_t *query, const uint16_t *key, const uint16_t *value, const uint16_t *terms, uint16_t *output, float *scores, uint16_t *staged, size_t qkv_elements, size_t term_elements, size_t score_elements, size_t planes, size_t height, size_t width, size_t channels)
static int attend_rows(const struct spatial_attention *a, size_t begin, size_t end, float *scores)
static void attend_lanes(size_t begin, size_t end, void *ctx)
static double dot(const uint16_t *q, const uint16_t *k, size_t channels)
static int valid_region(const void *p, size_t n, size_t width)
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 void ck_checked_parallel_for(size_t count, int grain, ck_checked_range_fn fn, void *args)
#define CK_THREADPOOL_MAX_THREADS
int32_t float * score
Definition tokenizer.h:329
const uint8_t size_t count
Definition true_bpe.h:211
uint32_t end
Definition utf8.c:215