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

Go to the source code of this file.

Enumerations

enum  { ONLINE_BLOCK_KEYS = 512 }

Functions

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 checked_geometry (size_t tokens, size_t heads, size_t head_dim, size_t *width, size_t *elements)
static int float_overlap (const float *a, size_t ac, const float *b, size_t bc)
static int float_region (const float *ptr, size_t count)
static void online_attention_rows (size_t begin, size_t end, void *opaque)
static double score (const float *query, const float *key, size_t head_dim, double scale)

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
ONLINE_BLOCK_KEYS 

Definition at line 55 of file attention_full_token_major_online.c.

Function Documentation

◆ attention_full_token_major_online_bf16_exp_checked()

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 )

Definition at line 113 of file attention_full_token_major_online.c.

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)
static int float_region(const float *ptr, size_t count)
static void ck_checked_parallel_for(size_t count, int grain, ck_checked_range_fn fn, void *args)

References checked_geometry(), ck_checked_parallel_for(), float_overlap(), float_region(), and online_attention_rows().

◆ checked_geometry()

int checked_geometry ( size_t tokens,
size_t heads,
size_t head_dim,
size_t * width,
size_t * elements )
static

Definition at line 12 of file attention_full_token_major_online.c.

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}

Referenced by attention_full_token_major_online_bf16_exp_checked().

◆ float_overlap()

int float_overlap ( const float * a,
size_t ac,
const float * b,
size_t bc )
static

Definition at line 41 of file attention_full_token_major_online.c.

42{
43 return (uintptr_t)a < (uintptr_t)b + bc*sizeof(float) &&
44 (uintptr_t)b < (uintptr_t)a + ac*sizeof(float);
45}

Referenced by attention_full_token_major_online_bf16_exp_checked().

◆ float_region()

int float_region ( const float * ptr,
size_t count )
static

Definition at line 35 of file attention_full_token_major_online.c.

36{
37 return ptr && (uintptr_t)ptr % _Alignof(float) == 0 &&
38 count <= (UINTPTR_MAX-(uintptr_t)ptr)/sizeof(float);
39}
const uint8_t size_t count
Definition true_bpe.h:211

References count.

Referenced by attention_full_token_major_online_bf16_exp_checked().

◆ online_attention_rows()

void online_attention_rows ( size_t begin,
size_t end,
void * opaque )
static

Definition at line 57 of file attention_full_token_major_online.c.

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}
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
int32_t float * score
Definition tokenizer.h:329
uint32_t end
Definition utf8.c:215
uint32_t start
Definition utf8.c:214

References bf16_to_float(), end, float_to_bf16(), ONLINE_BLOCK_KEYS, product(), score, and start.

Referenced by attention_full_token_major_online_bf16_exp_checked().

◆ score()

double score ( const float * query,
const float * key,
size_t head_dim,
double scale )
static

Definition at line 24 of file attention_full_token_major_online.c.

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}

References product().