← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
linear_rows_sgemm_checked.c
Go to the documentation of this file.
1/* Explicit LP64 CBLAS row-major projection. BLAS owns the FP32 reduction. */
3#include "checked_parallel.h"
4#include <dlfcn.h>
5#include <limits.h>
6#include <math.h>
7#include <pthread.h>
8#include <stdint.h>
9#include <stdlib.h>
10#include <string.h>
11
12typedef void (*sgemm_fn)(int, int, int, int, int, int, float, const float *,
13 int, const float *, int, float, float *, int);
15typedef void (*vector_erf_fn)(int, const float *, float *);
17static pthread_once_t once = PTHREAD_ONCE_INIT;
18
19static void bind_sgemm(void)
20{
21 const char *path = getenv("CK_BLAS_LIBRARY");
22 if (!path || !*path) return;
23 void *handle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
24 if (handle) {
25 void *symbol = dlsym(handle, "cblas_sgemm");
26 memcpy(&sgemm, &symbol, sizeof(sgemm));
27 symbol = dlsym(handle, "vsErf");
28 memcpy(&vector_erf, &symbol, sizeof(vector_erf));
29 if (!sgemm && !vector_erf) dlclose(handle);
30 }
31}
32
33static int valid_region(const float *p, size_t count)
34{
35 return p && (uintptr_t)p % _Alignof(float) == 0 &&
36 count <= (UINTPTR_MAX - (uintptr_t)p) / sizeof(float);
37}
38
39static int intersects(const float *a, size_t an, const float *b, size_t bn)
40{
41 return (uintptr_t)a < (uintptr_t)b + bn*sizeof(float) &&
42 (uintptr_t)b < (uintptr_t)a + an*sizeof(float);
43}
44
45int ck_linear_rows_sgemm_checked(const float *x, size_t xn, const float *w, size_t wn,
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)
48{
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 ||
51 !valid_region(x,xn) || !valid_region(w,wn) || !valid_region(bias,bn) ||
52 !valid_region(out,on) || !valid_region(stage,sn)) return -2;
53 if (intersects(out,on,stage,sn) || intersects(stage,sn,x,xn) ||
54 intersects(stage,sn,w,wn) || intersects(stage,sn,bias,bn) ||
55 intersects(out,on,x,xn) || intersects(out,on,w,wn) ||
56 intersects(out,on,bias,bn)) return -2;
57 pthread_once(&once, bind_sgemm);
58 if (!sgemm) return -4;
59 /* RowMajor, NoTrans, Trans. beta=0 permits uninitialized scratch. */
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];
64 }
65 if (!ck_checked_finite_bulk(stage,m*n)) return -1;
66 memcpy(out,stage,m*n*sizeof(float));
67 return 0;
68}
69
70struct attention_rows {
71 const float *query, *key, *value;
72 float *scores, *output;
73 size_t memory, heads, dim, width;
74 float scale;
75};
76static void attend_rows(size_t begin, size_t end, void *opaque)
77{
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;
88 float peak = row[0];
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];
92 }
93 double sum = 0;
94 for (size_t j=0; j<memory; ++j) {
95 row[j] = expf(row[j] - peak);
96 sum += row[j];
97 }
98 float inv = (float)(1.0/sum);
99 for (size_t j=0; j<memory; ++j) row[j] *= inv;
100 }
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);
103 }
104}
105
106static int attention_impl(const float *query, size_t qn, const float *key,
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)
109{
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;
115 if (!valid_region(query,qn) || !valid_region(key,kn) || !valid_region(value,vn) ||
116 !valid_region(out,on) || !valid_region(scratch,sn) || intersects(out,on,scratch,sn) ||
117 intersects(out,on,query,qn) || intersects(out,on,key,kn) || intersects(out,on,value,vn) ||
118 intersects(scratch,sn,query,qn) || intersects(scratch,sn,key,kn) ||
119 intersects(scratch,sn,value,vn)) return -1;
120 pthread_once(&once, bind_sgemm);
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};
125 if (pool) ck_checked_parallel_for(queries,8,attend_rows,&work);
126 else attend_rows(0,queries,&work);
127 if (!ck_checked_finite_bulk(stage,queries*width)) return -1;
128 memcpy(out,stage,queries*width*sizeof(float));
129 return 0;
130}
131
132struct gelu_rows {const float *x; float *stage; size_t xs, channels;};
133static void gelu_range(size_t begin,size_t end,void *opaque)
134{
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;
142 }
143 vector_erf((int)count,stage,stage);
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]);
147}
148
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)
151{
152 size_t input_span, output_span;
153 if (!ck_checked_span(rows,channels,xs,&input_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;
157 if (!valid_region(x,input_span) || !valid_region(out,output_span) || !valid_region(stage,count) ||
158 intersects(x,input_span,stage,count) || intersects(out,output_span,x,input_span) ||
159 intersects(out,output_span,stage,count)) return -1;
160 pthread_once(&once, bind_sgemm);
161 if (!vector_erf) return -4;
162 for (size_t r=0; r<rows; ++r)
163 if (!ck_checked_finite_bulk(x+r*xs,channels)) return -1;
164 struct gelu_rows work={x,stage,xs,channels};
165 if (pool) ck_checked_parallel_for(rows,8,gelu_range,&work);
166 else gelu_range(0,rows,&work);
167 if (!ck_checked_finite_bulk(stage,count)) return -1;
168 for (size_t r=0; r<rows; ++r) memcpy(out+r*os,stage+r*channels,channels*sizeof(float));
169 return 0;
170}
171
172int ck_attention_cross_sgemm_f32(const float *query, size_t qn, const float *key,
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); }
176
177int ck_attention_cross_sgemm_pool_f32(const float *query, size_t qn, const float *key,
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); }
181
182int ck_gelu_rows_vector_erf_f32(const float *x, size_t xn, size_t xs, float *out,
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); }
185
186int ck_gelu_rows_vector_erf_pool_f32(const float *x, size_t xn, size_t xs, float *out,
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 sgemm_fn sgemm
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)
static sgemm_fn sgemm
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
Definition true_bpe.h:211
uint32_t end
Definition utf8.c:215