28 const uint16_t *query, *key, *value, *terms;
31 size_t rows, height, width, channels, lanes;
35static int attend_rows(
const struct spatial_attention *a,
size_t begin,
size_t end,
float *scores)
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) {
47 for (
size_t x = 0; x < width; ++x, ++j) {
50 const double score =
dot(q, a->key + base + j * channels, channels) * scale + bias;
53 scores[j] = (float)
score;
54 if (scores[j] > largest)
59 for (
size_t j = 0; j < tokens; ++j) {
60 scores[j] = (float)exp((
double)scores[j] - largest);
63 for (
size_t c = 0; c < channels; ++c) {
65 for (
size_t j = 0; j < tokens; ++j)
66 sum += (
double)scores[j] *
bf16_to_float(a->value[base + j * channels + c]);
68 if ((result & 0x7f80u) == 0x7f80u)
70 a->staged[row * channels + c] = result;
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))
84 a->rows * (lane + 1) / a->lanes, a->scores + lane * tokens)) {
85 atomic_store_explicit(&a->failed, 1, memory_order_relaxed);
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)
97 if (!planes || planes > 384 || !height || height > 64 || !width || width > 64 ||
98 !channels || channels > 128)
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 ||
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)) ||
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))
118 for (
size_t i = 0; i < 4; ++i) {
119 const size_t bytes =
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))
125 const uint16_t *bits = input[i];
126 for (
size_t j = 0; j <
count[i]; ++j)
127 if ((bits[j] & 0x7f80u) == 0x7f80u)
131 struct spatial_attention attention = {
132 query, key, value, terms, staged, scores, rows, height, width, channels, lanes, 0,
135 if (atomic_load_explicit(&attention.failed, memory_order_relaxed))
137 memcpy(output, staged, qkv_bytes);
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)