← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
attention_cross_token_major_checked.c
Go to the documentation of this file.
1#include <float.h>
2#include <math.h>
3#include <stdint.h>
4#include <stddef.h>
5#include <string.h>
8
9static int region_ok(const void *ptr, size_t count, size_t width) {
10 return ptr && (uintptr_t)ptr % width == 0 &&
11 count <= SIZE_MAX / width &&
12 (uintptr_t)ptr <= UINTPTR_MAX - count * width;
13}
14
15static int overlaps(const void *a, size_t a_bytes,
16 const void *b, size_t b_bytes) {
17 uintptr_t x = (uintptr_t)a, y = (uintptr_t)b;
18 return x < y + b_bytes && y < x + a_bytes;
19}
20
21static double dot(const float *a, const float *b, size_t n) {
22 double sum = 0.0;
23 for (size_t d = 0; d < n; ++d) {
24 volatile double product = (double)a[d] * (double)b[d];
25 sum += product;
26 }
27 return sum;
28}
29
30/* Preserve the forward score and exponential storage boundaries for replay. */
31static int attention_scores(const float *q, const float *key, size_t memory_tokens,
32 size_t width, size_t head, size_t dim, float *scores, double *denominator) {
33 double scale = 1.0 / sqrt((double)dim), maximum = -INFINITY;
34 for (size_t k = 0; k < memory_tokens; ++k) {
35 double value = dot(q, key + k * width + head * dim, dim) * scale;
36 if (!isfinite(value) || fabs(value) > FLT_MAX) return -1;
37 scores[k] = (float)value;
38 if (scores[k] > maximum) maximum = scores[k];
39 }
40 *denominator = 0.0;
41 for (size_t k = 0; k < memory_tokens; ++k) {
42 scores[k] = (float)exp((double)scores[k] - maximum);
43 *denominator += (double)scores[k];
44 }
45 return !isfinite(*denominator) || *denominator <= 0.0 ? -1 : 0;
46}
47
48/* Full unmasked cross attention, batch one, token-major packed heads. */
50 const float *query, size_t query_elements,
51 const float *key, size_t key_elements,
52 const float *value, size_t value_elements,
53 float *output, size_t output_elements,
54 float *scratch, size_t scratch_elements,
55 size_t query_tokens, size_t memory_tokens,
56 size_t heads, size_t head_dim) {
57 if (!query_tokens || !memory_tokens || !heads || !head_dim ||
58 heads > SIZE_MAX / head_dim) return -2;
59 size_t width = heads * head_dim;
60 if (query_tokens > SIZE_MAX / width ||
61 memory_tokens > SIZE_MAX / width) return -2;
62 size_t q_count = query_tokens * width;
63 size_t m_count = memory_tokens * width;
64 if (q_count > SIZE_MAX - memory_tokens ||
65 query_elements < q_count || key_elements < m_count ||
66 value_elements < m_count || output_elements < q_count ||
67 scratch_elements < memory_tokens + q_count) return -2;
68 if (!region_ok(query, query_elements, sizeof(float)) ||
69 !region_ok(key, key_elements, sizeof(float)) ||
70 !region_ok(value, value_elements, sizeof(float)) ||
71 !region_ok(output, output_elements, sizeof(float)) ||
72 !region_ok(scratch, scratch_elements, sizeof(float))) return -1;
73 size_t output_bytes = output_elements * sizeof(float);
74 size_t scratch_bytes = scratch_elements * sizeof(float);
75 if (overlaps(output, output_bytes, query, query_elements * sizeof(float)) ||
76 overlaps(output, output_bytes, key, key_elements * sizeof(float)) ||
77 overlaps(output, output_bytes, value, value_elements * sizeof(float)) ||
78 overlaps(output, output_bytes, scratch, scratch_bytes) ||
79 overlaps(scratch, scratch_bytes, query, query_elements * sizeof(float)) ||
80 overlaps(scratch, scratch_bytes, key, key_elements * sizeof(float)) ||
81 overlaps(scratch, scratch_bytes, value, value_elements * sizeof(float))) return -1;
82 for (size_t i = 0; i < q_count; ++i)
83 if (!isfinite(query[i])) return -1;
84 for (size_t i = 0; i < m_count; ++i)
85 if (!isfinite(key[i]) || !isfinite(value[i])) return -1;
86
87 float *scores = scratch;
88 float *staged = scratch + memory_tokens;
89 for (size_t t = 0; t < query_tokens; ++t) {
90 for (size_t h = 0; h < heads; ++h) {
91 const float *q = query + t * width + h * head_dim;
92 double denominator;
93 if (attention_scores(q, key, memory_tokens, width, h, head_dim, scores, &denominator)) return -1;
94 for (size_t d = 0; d < head_dim; ++d) {
95 double weighted = 0.0;
96 for (size_t k = 0; k < memory_tokens; ++k) {
97 volatile double product = (double)scores[k] *
98 (double)value[k * width + h * head_dim + d];
99 weighted += product;
100 }
101 float result = (float)(weighted / denominator);
102 if (!isfinite(result)) return -1;
103 staged[t * width + h * head_dim + d] = result;
104 }
105 }
106 }
107 for (size_t i = 0; i < q_count; ++i) output[i] = staged[i];
108 return 0;
109}
110
111/* VJP for full, unmasked token-major attention. Parameter-free and batch one.
112 * Stage all three outputs so even late overflow leaves caller gradients intact.
113 * FP64 dot/reduction arithmetic with explicit FP32 score/exponential/dS stores;
114 * shared dK/dV accumulate in ascending query order with an FP32 store per query.
115 */
117 const float *query, size_t query_elements,
118 const float *key, size_t key_elements,
119 const float *value, size_t value_elements,
120 const float *d_output, size_t d_output_elements,
121 float *d_query, size_t d_query_elements,
122 float *d_key, size_t d_key_elements,
123 float *d_value, size_t d_value_elements,
124 float *scratch, size_t scratch_elements,
125 size_t query_tokens, size_t memory_tokens, size_t heads, size_t head_dim) {
126 if (!query_tokens || !memory_tokens || !heads || !head_dim || heads > SIZE_MAX / head_dim) return -2;
127 size_t width = heads * head_dim;
128 if (query_tokens > SIZE_MAX / width || memory_tokens > SIZE_MAX / width) return -2;
129 size_t nq = query_tokens * width, nm = memory_tokens * width;
130 if (nm > (SIZE_MAX - nq) / 2) return -2;
131 size_t staged = nq + 2 * nm;
132 if (memory_tokens > (SIZE_MAX - staged) / 2) return -2;
133 size_t needed = staged + 2 * memory_tokens;
134 if (query_elements < nq || key_elements < nm || value_elements < nm ||
135 d_output_elements < nq || d_query_elements < nq || d_key_elements < nm ||
136 d_value_elements < nm || scratch_elements < needed) return -2;
137 const float *readers[] = {query, key, value, d_output};
138 size_t nr[] = {query_elements, key_elements, value_elements, d_output_elements};
139 float *writers[] = {d_query, d_key, d_value, scratch};
140 size_t nw[] = {d_query_elements, d_key_elements, d_value_elements, scratch_elements};
141 for (size_t i = 0; i < 4; ++i) {
142 if (!ck_checked_region(readers[i], nr[i]) || !ck_checked_region(writers[i], nw[i])) return -1;
143 }
144 for (size_t i = 0; i < 4; ++i) {
145 for (size_t j = 0; j < 4; ++j)
146 if (ck_checked_overlap(writers[i], nw[i], readers[j], nr[j])) return -1;
147 for (size_t j = 0; j < i; ++j)
148 if (ck_checked_overlap(writers[i], nw[i], writers[j], nw[j])) return -1;
149 }
150 if (!ck_checked_finite(query, nq) || !ck_checked_finite(key, nm) ||
151 !ck_checked_finite(value, nm) || !ck_checked_finite(d_output, nq)) return -3;
152 float *dq = scratch, *dk = dq + nq, *dv = dk + nm;
153 float *scores = dv + nm, *ds = scores + memory_tokens;
154 memset(scratch, 0, staged * sizeof(float));
155 double scale = 1.0 / sqrt((double)head_dim);
156 for (size_t t = 0; t < query_tokens; ++t) {
157 for (size_t h = 0; h < heads; ++h) {
158 const float *q = query + t * width + h * head_dim;
159 const float *dy = d_output + t * width + h * head_dim;
160 double denominator;
161 if (attention_scores(q, key, memory_tokens, width, h, head_dim, scores, &denominator)) return -4;
162 double weighted = 0.0;
163 for (size_t k = 0; k < memory_tokens; ++k) {
164 double dp = dot(dy, value + k * width + h * head_dim, head_dim);
165 volatile double term = ((double)scores[k] / denominator) * dp;
166 weighted += term;
167 }
168 for (size_t k = 0; k < memory_tokens; ++k) {
169 double dp = dot(dy, value + k * width + h * head_dim, head_dim);
170 double result = ((double)scores[k] / denominator) * (dp - weighted) * scale;
171 if (!isfinite(result) || fabs(result) > FLT_MAX) return -4;
172 ds[k] = (float)result;
173 }
174 for (size_t d = 0; d < head_dim; ++d) {
175 double sum = 0.0;
176 for (size_t k = 0; k < memory_tokens; ++k) {
177 volatile double term = (double)ds[k] * (double)key[k * width + h * head_dim + d];
178 sum += term;
179 }
180 if (!isfinite(sum) || fabs(sum) > FLT_MAX) return -4;
181 dq[t * width + h * head_dim + d] = (float)sum;
182 }
183 for (size_t k = 0; k < memory_tokens; ++k) {
184 for (size_t d = 0; d < head_dim; ++d) {
185 size_t index = k * width + h * head_dim + d;
186 volatile double kp = (double)ds[k] * (double)q[d];
187 volatile double vp = ((double)scores[k] / denominator) * (double)dy[d];
188 double kg = (double)dk[index] + kp, vg = (double)dv[index] + vp;
189 if (!isfinite(kg) || !isfinite(vg) || fabs(kg) > FLT_MAX || fabs(vg) > FLT_MAX) return -4;
190 dk[index] = (float)kg;
191 dv[index] = (float)vg;
192 }
193 }
194 }
195 }
196 memcpy(d_query, dq, nq * sizeof(float));
197 memcpy(d_key, dk, nm * sizeof(float));
198 memcpy(d_value, dv, nm * sizeof(float));
199 return 0;
200}
static int region_ok(const void *ptr, size_t count, size_t width)
static int attention_scores(const float *q, const float *key, size_t memory_tokens, size_t width, size_t head, size_t dim, float *scores, double *denominator)
static int overlaps(const void *a, size_t a_bytes, const void *b, size_t b_bytes)
static double dot(const float *a, const float *b, size_t n)
int ck_attention_cross_backward_f32(const float *query, size_t query_elements, const float *key, size_t key_elements, const float *value, size_t value_elements, const float *d_output, size_t d_output_elements, float *d_query, size_t d_query_elements, float *d_key, size_t d_key_elements, float *d_value, size_t d_value_elements, float *scratch, size_t scratch_elements, size_t query_tokens, size_t memory_tokens, size_t heads, size_t head_dim)
int ck_attention_cross_token_major_checked_f32(const float *query, size_t query_elements, const float *key, size_t key_elements, const float *value, size_t value_elements, float *output, size_t output_elements, float *scratch, size_t scratch_elements, size_t query_tokens, size_t memory_tokens, size_t heads, size_t head_dim)
static int product(size_t a, size_t b, size_t *out)
static int ck_checked_finite(const float *ptr, size_t n)
static int ck_checked_region(const float *ptr, size_t n)
static int ck_checked_overlap(const float *a, size_t na, const float *b, size_t nb)
const uint8_t size_t count
Definition true_bpe.h:211