← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
gqa_relative_sink_bf16_checked.c File Reference
#include "bf16_utils.h"
#include "checked_float_buffer.h"
#include <string.h>

Go to the source code of this file.

Functions

int ck_gqa_relative_sink_checked_bf16 (const uint16_t *query, size_t query_elements, const uint16_t *key, size_t key_elements, const uint16_t *value, size_t value_elements, const uint16_t *relative, size_t relative_elements, const uint16_t *sink, size_t sink_elements, uint16_t *output, size_t output_elements, float *scores, size_t score_elements, uint16_t *staged, size_t staged_elements, size_t tokens, size_t heads, size_t kv_heads, size_t dim, size_t capacity, size_t position, size_t relative_extent, size_t window)
static int finite_bits (const uint16_t *input, size_t count)
static int overlap (attention_span first, attention_span second)
static float rounded_bf16 (float value)
static int valid_span (attention_span span)

Function Documentation

◆ ck_gqa_relative_sink_checked_bf16()

int ck_gqa_relative_sink_checked_bf16 ( const uint16_t * query,
size_t query_elements,
const uint16_t * key,
size_t key_elements,
const uint16_t * value,
size_t value_elements,
const uint16_t * relative,
size_t relative_elements,
const uint16_t * sink,
size_t sink_elements,
uint16_t * output,
size_t output_elements,
float * scores,
size_t score_elements,
uint16_t * staged,
size_t staged_elements,
size_t tokens,
size_t heads,
size_t kv_heads,
size_t dim,
size_t capacity,
size_t position,
size_t relative_extent,
size_t window )

Definition at line 43 of file gqa_relative_sink_bf16_checked.c.

50{
51 if (!tokens || !heads || !kv_heads || heads % kv_heads || !dim || dim > 1024 ||
52 heads > 256 || !capacity || capacity > 16384 || position >= capacity ||
53 tokens > capacity - position || !relative_extent || relative_extent > 16384 ||
54 window > 16384) return -2;
55 const size_t queries = tokens * heads;
56 size_t output_count, cache_count, bias_count;
57 if (!ck_checked_span(queries, dim, dim, &output_count) ||
58 !ck_checked_span(kv_heads * capacity, dim, dim, &cache_count) ||
59 !ck_checked_span(queries, relative_extent, relative_extent, &bias_count)) return -2;
60 const size_t active = position + tokens;
61 const size_t visible = window && window < active ? window : active;
62 if (query_elements < output_count || key_elements < cache_count || value_elements < cache_count ||
63 relative_elements < bias_count || sink_elements < queries || output_elements < output_count ||
64 staged_elements < output_count || score_elements < visible) return -2;
65 const attention_span spans[] = {
66 {query, output_count * 2, 2}, {key, cache_count * 2, 2}, {value, cache_count * 2, 2},
67 {relative, bias_count * 2, 2}, {sink, queries * 2, 2}, {output, output_count * 2, 2},
68 {scores, visible * sizeof(float), _Alignof(float)}, {staged, output_count * 2, 2},
69 };
70 for (size_t i = 0; i < sizeof(spans) / sizeof(spans[0]); ++i)
71 if (!valid_span(spans[i])) return -2;
72 for (size_t i = 5; i < sizeof(spans) / sizeof(spans[0]); ++i)
73 for (size_t j = 0; j < i; ++j)
74 if (overlap(spans[i], spans[j])) return -2;
75 if (!finite_bits(query, output_count) || !finite_bits(relative, bias_count) ||
76 !finite_bits(sink, queries)) return -3;
77 for (size_t head = 0; head < kv_heads; ++head)
78 if (!finite_bits(key + head * capacity * dim, active * dim) ||
79 !finite_bits(value + head * capacity * dim, active * dim)) return -3;
80
81 const float scale = 1.0f / sqrtf((float)dim);
82 for (size_t row = 0; row < queries; ++row) {
83 const size_t current = position + row / heads;
84 const size_t start = window && current >= window ? current + 1 - window : 0;
85 const size_t count = current - start + 1;
86 const size_t kv_head = (row % heads) / (heads / kv_heads);
87 const uint16_t *key_rows = key + (kv_head * capacity + start) * dim;
88 const uint16_t *value_rows = value + (kv_head * capacity + start) * dim;
89 const float sink_logit = bf16_to_float(sink[row]);
90 float maximum = sink_logit;
91 for (size_t index = 0; index < count; ++index) {
92 float dot = 0.0f;
93 for (size_t channel = 0; channel < dim; ++channel)
94 dot += bf16_to_float(query[row * dim + channel]) *
95 bf16_to_float(key_rows[index * dim + channel]);
96 const size_t distance = count - index - 1;
97 const float bias = distance < relative_extent
98 ? bf16_to_float(relative[row * relative_extent + distance]) : 0.0f;
99 const uint16_t score_bits = float_to_bf16(rounded_bf16(rounded_bf16(dot) * scale) + bias);
100 if ((score_bits & 0x7f80u) == 0x7f80u) return -3;
101 const float score = bf16_to_float(score_bits);
102 scores[index] = score;
103 if (score > maximum) maximum = score;
104 }
105
106 float denominator = 0.0f;
107 for (size_t index = 0; index < count; ++index) {
108 scores[index] = expf(scores[index] - maximum);
109 denominator += scores[index];
110 }
111 denominator += expf(sink_logit - maximum);
112 for (size_t index = 0; index < count; ++index)
113 scores[index] = rounded_bf16(scores[index] / denominator);
114
115 for (size_t channel = 0; channel < dim; ++channel) {
116 float sum = 0.0f;
117 for (size_t index = 0; index < count; ++index)
118 sum += scores[index] * bf16_to_float(value_rows[index * dim + channel]);
119 const uint16_t result = float_to_bf16(sum);
120 if ((result & 0x7f80u) == 0x7f80u) return -3;
121 staged[row * dim + channel] = result;
122 }
123 }
124 memcpy(output, staged, output_count * sizeof(*output));
125 return 0;
126}
static double dot(const float *a, const float *b, size_t n)
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 int ck_checked_span(size_t rows, size_t width, size_t stride, size_t *span)
static float rounded_bf16(float value)
static int finite_bits(const uint16_t *input, size_t count)
static int valid_span(attention_span span)
static int overlap(attention_span first, attention_span second)
int32_t float * score
Definition tokenizer.h:329
const uint8_t size_t count
Definition true_bpe.h:211
uint32_t start
Definition utf8.c:214

References bf16_to_float(), ck_checked_span(), count, dot(), finite_bits(), float_to_bf16(), overlap(), rounded_bf16(), score, start, and valid_span().

◆ finite_bits()

int finite_bits ( const uint16_t * input,
size_t count )
static

Definition at line 31 of file gqa_relative_sink_bf16_checked.c.

32{
33 for (size_t i = 0; i < count; ++i)
34 if ((input[i] & 0x7f80u) == 0x7f80u) return 0;
35 return 1;
36}

References count.

Referenced by ck_gqa_relative_sink_checked_bf16().

◆ overlap()

int overlap ( attention_span first,
attention_span second )
static

Definition at line 25 of file gqa_relative_sink_bf16_checked.c.

26{
27 const uintptr_t a = (uintptr_t)first.address, b = (uintptr_t)second.address;
28 return a < b + second.bytes && b < a + first.bytes;
29}

Referenced by ck_gqa_relative_sink_checked_bf16().

◆ rounded_bf16()

float rounded_bf16 ( float value)
inlinestatic

Definition at line 38 of file gqa_relative_sink_bf16_checked.c.

39{
40 return bf16_to_float(float_to_bf16(value));
41}

References bf16_to_float(), and float_to_bf16().

Referenced by ck_gqa_relative_sink_checked_bf16().

◆ valid_span()

int valid_span ( attention_span span)
static

Definition at line 19 of file gqa_relative_sink_bf16_checked.c.

20{
21 const uintptr_t start = (uintptr_t)span.address;
22 return start && start % span.alignment == 0 && span.bytes <= UINTPTR_MAX - start;
23}

References start.

Referenced by ck_gqa_relative_sink_checked_bf16().