44 const uint16_t *query,
size_t query_elements,
const uint16_t *key,
size_t key_elements,
45 const uint16_t *value,
size_t value_elements,
const uint16_t *relative,
size_t relative_elements,
46 const uint16_t *sink,
size_t sink_elements, uint16_t *output,
size_t output_elements,
47 float *scores,
size_t score_elements, uint16_t *staged,
size_t staged_elements,
48 size_t tokens,
size_t heads,
size_t kv_heads,
size_t dim,
size_t capacity,
size_t position,
49 size_t relative_extent,
size_t window)
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;
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},
70 for (
size_t i = 0; i <
sizeof(spans) /
sizeof(spans[0]); ++i)
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;
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;
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;
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;
90 float maximum = sink_logit;
91 for (
size_t index = 0; index <
count; ++index) {
93 for (
size_t channel = 0; channel < 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;
100 if ((score_bits & 0x7f80u) == 0x7f80u)
return -3;
102 scores[index] =
score;
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];
111 denominator += expf(sink_logit - maximum);
112 for (
size_t index = 0; index <
count; ++index)
113 scores[index] =
rounded_bf16(scores[index] / denominator);
115 for (
size_t channel = 0; channel < dim; ++channel) {
117 for (
size_t index = 0; index <
count; ++index)
118 sum += scores[index] *
bf16_to_float(value_rows[index * dim + channel]);
120 if ((result & 0x7f80u) == 0x7f80u)
return -3;
121 staged[row * dim + channel] = result;
124 memcpy(output, staged, output_count *
sizeof(*output));
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)