12typedef void (*
sgemm_fn)(int, int, int, int, int, int, float,
const float *,
13 int,
const float *, int, float,
float *, int);
17static pthread_once_t
once = PTHREAD_ONCE_INIT;
21 const char *path = getenv(
"CK_BLAS_LIBRARY");
22 if (!path || !*path)
return;
23 void *handle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
25 void *symbol = dlsym(handle,
"cblas_sgemm");
27 symbol = dlsym(handle,
"vsErf");
35 return p && (uintptr_t)p % _Alignof(
float) == 0 &&
36 count <= (UINTPTR_MAX - (uintptr_t)p) /
sizeof(
float);
39static int intersects(
const float *a,
size_t an,
const float *b,
size_t bn)
41 return (uintptr_t)a < (uintptr_t)b + bn*
sizeof(
float) &&
42 (uintptr_t)b < (uintptr_t)a + an*
sizeof(float);
46 const float *bias,
size_t bn,
float *out,
size_t on,
float *stage,
size_t sn,
47 size_t m,
size_t k,
size_t n)
49 if (!m || !k || !n || m > INT_MAX || n > INT_MAX || k > INT_MAX ||
50 m > xn/k || n > wn/k || n > bn || m > on/n || m > sn/n ||
58 if (!
sgemm)
return -4;
60 sgemm(101,111,112,(
int)m,(
int)n,(
int)k,1.0f,x,(
int)k,w,(
int)k,0.0f,stage,(
int)n);
61 for (
size_t i=0; i<m; ++i)
62 for (
size_t j=0; j<n; ++j) {
63 stage[i*n+j] += bias[j];
66 memcpy(out,stage,m*n*
sizeof(
float));
70struct attention_rows {
71 const float *query, *key, *value;
72 float *scores, *output;
73 size_t memory, heads, dim, width;
78 const struct attention_rows *a = opaque;
79 const size_t queries=
end-begin, memory=a->memory, heads=a->heads, dim=a->dim, width=a->width;
80 const float *query=a->query+begin*width, *key=a->key, *value=a->value;
81 float *scratch=a->scores+begin*memory, *stage=a->output+begin*width;
82 const float scale=a->scale;
83 for (
size_t h=0; h<heads; ++h) {
84 sgemm(101,111,112,(
int)queries,(
int)memory,(
int)dim,scale,query+h*dim,(
int)width,
85 key+h*dim,(
int)width,0.0f,scratch,(
int)memory);
86 for (
size_t r=0; r<queries; ++r) {
87 float *row = scratch + r*memory;
89 for (
size_t j=0; j<memory; ++j) {
90 if (!isfinite(row[j])) { stage[0] = NAN;
return; }
91 if (row[j] > peak) peak = row[j];
94 for (
size_t j=0; j<memory; ++j) {
95 row[j] = expf(row[j] - peak);
98 float inv = (float)(1.0/sum);
99 for (
size_t j=0; j<memory; ++j) row[j] *= inv;
101 sgemm(101,111,111,(
int)queries,(
int)dim,(
int)memory,1.0f,scratch,(
int)memory,
102 value+h*dim,(
int)width,0.0f,stage+h*dim,(
int)width);
107 size_t kn,
const float *value,
size_t vn,
float *out,
size_t on,
108 float *scratch,
size_t sn,
size_t queries,
size_t memory,
size_t heads,
size_t dim,
int pool)
110 if (!heads || !dim || !queries || !memory || dim > INT_MAX / heads ||
111 queries > INT_MAX || memory > INT_MAX)
return -2;
112 size_t width = heads * dim;
113 if (queries > qn/width || queries > on/width || memory > kn/width ||
114 memory > vn/width || queries > sn/(width + memory))
return -2;
121 if (!
sgemm)
return -4;
122 float *stage = scratch + queries * memory;
123 float scale = 1.0f / sqrtf((
float)dim);
124 struct attention_rows work = {query,key,value,scratch,stage,memory,heads,dim,width,scale};
128 memcpy(out,stage,queries*width*
sizeof(
float));
132struct gelu_rows {
const float *x;
float *stage;
size_t xs, channels;};
135 const struct gelu_rows *g=opaque;
136 size_t channels=g->channels, xs=g->xs, rows=
end-begin,
count=rows*channels;
137 const float *x=g->x+begin*xs;
138 float *stage=g->stage+begin*channels;
139 for (
size_t r=0; r<rows; ++r) {
140 for (
size_t j=0; j<channels; ++j)
141 stage[r*channels+j] = x[r*xs+j]*0.7071067811865475f;
144 for (
size_t r=0; r<rows; ++r)
145 for (
size_t j=0; j<channels; ++j)
146 stage[r*channels+j] = (0.5f*x[r*xs+j])*(1.0f+stage[r*channels+j]);
149static int gelu_impl(
const float *x,
size_t xn,
size_t xs,
float *out,
150 size_t on,
size_t os,
float *stage,
size_t sn,
size_t rows,
size_t channels,
int pool)
152 size_t input_span, output_span;
154 !
ck_checked_span(rows,channels,os,&output_span) || rows > INT_MAX/channels ||
155 xn < input_span || on < output_span || rows > sn/channels)
return -2;
156 size_t count = rows*channels;
162 for (
size_t r=0; r<rows; ++r)
164 struct gelu_rows work={x,stage,xs,channels};
168 for (
size_t r=0; r<rows; ++r) memcpy(out+r*os,stage+r*channels,channels*
sizeof(
float));
173 size_t kn,
const float *value,
size_t vn,
float *out,
size_t on,
174 float *scratch,
size_t sn,
size_t queries,
size_t memory,
size_t heads,
size_t dim)
175{
return attention_impl(query,qn,key,kn,value,vn,out,on,scratch,sn,queries,memory,heads,dim,0); }
178 size_t kn,
const float *value,
size_t vn,
float *out,
size_t on,
179 float *scratch,
size_t sn,
size_t queries,
size_t memory,
size_t heads,
size_t dim)
180{
return attention_impl(query,qn,key,kn,value,vn,out,on,scratch,sn,queries,memory,heads,dim,1); }
183 size_t on,
size_t os,
float *stage,
size_t sn,
size_t rows,
size_t channels)
184{
return gelu_impl(x,xn,xs,out,on,os,stage,sn,rows,channels,0); }
187 size_t on,
size_t os,
float *stage,
size_t sn,
size_t rows,
size_t channels)
188{
return gelu_impl(x,xn,xs,out,on,os,stage,sn,rows,channels,1); }
static int attend_rows(const struct spatial_attention *a, size_t begin, size_t end, float *scores)
static int ck_checked_span(size_t rows, size_t width, size_t stride, size_t *span)
static int ck_checked_finite_bulk(const float *ptr, size_t n)
static void ck_checked_parallel_for(size_t count, int grain, ck_checked_range_fn fn, void *args)
void(* sgemm_fn)(const char *, const char *, const int *, const int *, const int *, const float *, const float *, const int *, const float *, const int *, const float *, float *, const int *)
static int gelu_impl(const float *x, size_t xn, size_t xs, float *out, size_t on, size_t os, float *stage, size_t sn, size_t rows, size_t channels, int pool)
int ck_attention_cross_sgemm_f32(const float *query, size_t qn, const float *key, size_t kn, const float *value, size_t vn, float *out, size_t on, float *scratch, size_t sn, size_t queries, size_t memory, size_t heads, size_t dim)
static int attention_impl(const float *query, size_t qn, const float *key, size_t kn, const float *value, size_t vn, float *out, size_t on, float *scratch, size_t sn, size_t queries, size_t memory, size_t heads, size_t dim, int pool)
static void bind_sgemm(void)
int ck_gelu_rows_vector_erf_pool_f32(const float *x, size_t xn, size_t xs, float *out, size_t on, size_t os, float *stage, size_t sn, size_t rows, size_t channels)
static void attend_rows(size_t begin, size_t end, void *opaque)
static void gelu_range(size_t begin, size_t end, void *opaque)
int ck_gelu_rows_vector_erf_f32(const float *x, size_t xn, size_t xs, float *out, size_t on, size_t os, float *stage, size_t sn, size_t rows, size_t channels)
int ck_attention_cross_sgemm_pool_f32(const float *query, size_t qn, const float *key, size_t kn, const float *value, size_t vn, float *out, size_t on, float *scratch, size_t sn, size_t queries, size_t memory, size_t heads, size_t dim)
static vector_erf_fn vector_erf
int ck_linear_rows_sgemm_checked(const float *x, size_t xn, const float *w, size_t wn, const float *bias, size_t bn, float *out, size_t on, float *stage, size_t sn, size_t m, size_t k, size_t n)
static int intersects(const float *a, size_t an, const float *b, size_t bn)
void(* vector_erf_fn)(int, const float *, float *)
static pthread_once_t once
static int valid_region(const float *p, size_t count)
const uint8_t size_t count