← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
attention_full_token_major_online.c
Go to the documentation of this file.
2
3#include <float.h>
4#include <math.h>
5#include <stdatomic.h>
6#include <stdint.h>
7#include <string.h>
8
9#include "bf16_utils.h"
10#include "checked_parallel.h"
11
12static int checked_geometry(size_t tokens, size_t heads, size_t head_dim,
13 size_t *width, size_t *elements)
14{
15 if (!tokens || !heads || !head_dim || heads > SIZE_MAX / head_dim)
16 return 0;
17 *width = heads * head_dim;
18 if (tokens > SIZE_MAX / *width)
19 return 0;
20 *elements = tokens * *width;
21 return 1;
22}
23
24static double score(const float *query, const float *key, size_t head_dim,
25 double scale)
26{
27 double sum = 0.0;
28 for (size_t d = 0; d < head_dim; ++d) {
29 volatile double product = (double)query[d] * (double)key[d];
30 sum += product;
31 }
32 return sum * scale;
33}
34
35static int float_region(const float *ptr, size_t count)
36{
37 return ptr && (uintptr_t)ptr % _Alignof(float) == 0 &&
38 count <= (UINTPTR_MAX-(uintptr_t)ptr)/sizeof(float);
39}
40
41static int float_overlap(const float *a, size_t ac, const float *b, size_t bc)
42{
43 return (uintptr_t)a < (uintptr_t)b + bc*sizeof(float) &&
44 (uintptr_t)b < (uintptr_t)a + ac*sizeof(float);
45}
46
47struct online_attention_job {
48 const float *query, *key, *value;
49 float *stage;
50 size_t tokens, heads, head_dim, width;
51 double scale;
52 atomic_int failed;
53};
54
55enum { ONLINE_BLOCK_KEYS = 512 };
56
57static void online_attention_rows(size_t begin, size_t end, void *opaque)
58{
59 struct online_attention_job *job = opaque;
60 const size_t tokens = job->tokens, heads = job->heads;
61 const size_t head_dim = job->head_dim, width = job->width;
62 float scratch[ONLINE_BLOCK_KEYS];
63 for (size_t row = begin; row < end; ++row) {
64 const size_t t = row / heads, offset = (row % heads)*head_dim;
65 const float *q = job->query+t*width+offset;
66 float *dst = job->stage+t*width+offset;
67 memset(dst,0,head_dim*sizeof(float));
68 float maximum = -INFINITY, denominator = 0.0f;
69 for (size_t start = 0; start < tokens;) {
70 const size_t block = tokens-start < ONLINE_BLOCK_KEYS ? tokens-start : ONLINE_BLOCK_KEYS;
71 float next_max = maximum;
72 for (size_t k = 0; k < block; ++k) {
73 const double s = score(q,job->key+(start+k)*width+offset,head_dim,job->scale);
74 if (!isfinite(s) || fabs(s) > FLT_MAX) {
75 atomic_store_explicit(&job->failed,1,memory_order_relaxed);
76 return;
77 }
78 scratch[k] = (float)s;
79 if (scratch[k] > next_max) next_max = scratch[k];
80 }
81 const float carry = expf(maximum-next_max);
82 float sum = 0.0f;
83 // The denominator uses unrounded exponentials.
84 for (size_t k = 0; k < block; ++k) {
85 const float e = expf(scratch[k]-next_max);
86 sum += e;
87 scratch[k] = bf16_to_float(float_to_bf16(e));
88 }
89 volatile const float old_sum = denominator*carry;
90 denominator = old_sum + sum;
91 for (size_t d = 0; d < head_dim; ++d) {
92 double weighted = 0;
93 for (size_t k = 0; k < block; ++k) {
94 volatile const double product = (double)scratch[k]*job->value[(start+k)*width+offset+d];
95 weighted += product;
96 }
97 volatile const float old_weighted = dst[d]*carry;
98 dst[d] = old_weighted + (float)weighted;
99 }
100 maximum = next_max;
101 start += block;
102 }
103 for (size_t d = 0; d < head_dim; ++d) {
104 dst[d] /= denominator;
105 if (!isfinite(dst[d])) {
106 atomic_store_explicit(&job->failed,1,memory_order_relaxed);
107 return;
108 }
109 }
110 }
111}
112
114 const float *query, size_t query_elements, const float *key, size_t key_elements,
115 const float *value, size_t value_elements, float *output, size_t output_elements,
116 float *scratch, size_t scratch_bytes, size_t tokens, size_t heads, size_t head_dim)
117{
118 size_t width, elements;
119 if (!checked_geometry(tokens, heads, head_dim, &width, &elements) ||
120 elements > scratch_bytes/sizeof(float) ||
121 query_elements < elements || key_elements < elements ||
122 value_elements < elements || output_elements < elements ||
123 !float_region(output,elements) || !float_region(scratch,elements) ||
124 float_overlap(output,elements,scratch,elements))
125 return -1;
126 const float *inputs[] = {query,key,value};
127 for (size_t j = 0; j < 3; ++j) {
128 if (!float_region(inputs[j],elements) || float_overlap(output,elements,inputs[j],elements) ||
129 float_overlap(scratch,elements,inputs[j],elements))
130 return -1;
131 for (size_t i = 0; i < elements; ++i)
132 if (!isfinite(inputs[j][i])) return -1;
133 }
134 struct online_attention_job job = {query,key,value,scratch,tokens,heads,head_dim,width,
135 1.0/sqrt((double)head_dim),0};
137 if (atomic_load_explicit(&job.failed,memory_order_relaxed))
138 return -1;
139 memcpy(output,scratch,elements*sizeof(float));
140 return 0;
141}
static int checked_geometry(size_t tokens, size_t heads, size_t head_dim, size_t *width, size_t *elements)
static void online_attention_rows(size_t begin, size_t end, void *opaque)
static int float_overlap(const float *a, size_t ac, const float *b, size_t bc)
int attention_full_token_major_online_bf16_exp_checked(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_bytes, size_t tokens, size_t heads, size_t head_dim)
static int float_region(const float *ptr, size_t count)
static int product(size_t a, size_t b, size_t *out)
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)
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
uint32_t start
Definition utf8.c:214