← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
attention_spatial_terms_checked.c File Reference
#include "bf16_utils.h"
#include "checked_parallel.h"
#include <float.h>
#include <math.h>
#include <stdatomic.h>
#include <string.h>

Go to the source code of this file.

Functions

static void attend_lanes (size_t begin, size_t end, void *ctx)
static int attend_rows (const struct spatial_attention *a, size_t begin, size_t end, float *scores)
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 double dot (const uint16_t *q, const uint16_t *k, size_t channels)
static int overlaps (const void *a, size_t na, const void *b, size_t nb)
static int valid_region (const void *p, size_t n, size_t width)

Function Documentation

◆ attend_lanes()

void attend_lanes ( size_t begin,
size_t end,
void * ctx )
static

Definition at line 76 of file attention_spatial_terms_checked.c.

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}
static int attend_rows(const struct spatial_attention *a, size_t begin, size_t end, float *scores)
uint32_t end
Definition utf8.c:215

References attend_rows(), and end.

Referenced by ck_attention_spatial_terms_bf16_checked().

◆ attend_rows()

int attend_rows ( const struct spatial_attention * a,
size_t begin,
size_t end,
float * scores )
static

Definition at line 35 of file attention_spatial_terms_checked.c.

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}
static double dot(const uint16_t *q, const uint16_t *k, size_t channels)
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
int32_t float * score
Definition tokenizer.h:329

References bf16_to_float(), dot(), end, float_to_bf16(), and score.

Referenced by attend_lanes(), and attention_impl().

◆ ck_attention_spatial_terms_bf16_checked()

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 )

Definition at line 91 of file attention_spatial_terms_checked.c.

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)
static void attend_lanes(size_t begin, size_t end, void *ctx)
static int valid_region(const void *p, size_t n, size_t width)
static void ck_checked_parallel_for(size_t count, int grain, ck_checked_range_fn fn, void *args)
#define CK_THREADPOOL_MAX_THREADS
const uint8_t size_t count
Definition true_bpe.h:211

References attend_lanes(), ck_checked_parallel_for(), CK_THREADPOOL_MAX_THREADS, count, overlaps(), and valid_region().

◆ dot()

double dot ( const uint16_t * q,
const uint16_t * k,
size_t channels )
static

Definition at line 19 of file attention_spatial_terms_checked.c.

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}

References bf16_to_float().

Referenced by attend_rows().

◆ overlaps()

int overlaps ( const void * a,
size_t na,
const void * b,
size_t nb )
static

Definition at line 14 of file attention_spatial_terms_checked.c.

15{
16 return (uintptr_t)a < (uintptr_t)b + nb && (uintptr_t)b < (uintptr_t)a + na;
17}

Referenced by ck_attention_spatial_terms_bf16_checked().

◆ valid_region()

int valid_region ( const void * p,
size_t n,
size_t width )
static

Definition at line 9 of file attention_spatial_terms_checked.c.

10{
11 return p && (uintptr_t)p % width == 0 && (uintptr_t)p <= UINTPTR_MAX - n * width;
12}

Referenced by ck_attention_spatial_terms_bf16_checked().