← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
linear_rows_sgemm_checked.c File Reference
#include "checked_float_buffer.h"
#include "checked_parallel.h"
#include <dlfcn.h>
#include <limits.h>
#include <math.h>
#include <pthread.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

Go to the source code of this file.

Typedefs

typedef void(* sgemm_fn) (int, int, int, int, int, int, float, const float *, int, const float *, int, float, float *, int)
typedef void(* vector_erf_fn) (int, const float *, float *)

Functions

static void attend_rows (size_t begin, size_t end, void *opaque)
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_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)
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)
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_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)
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 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)
static void gelu_range (size_t begin, size_t end, void *opaque)
static int intersects (const float *a, size_t an, const float *b, size_t bn)
static int valid_region (const float *p, size_t count)

Variables

static pthread_once_t once = PTHREAD_ONCE_INIT
static sgemm_fn sgemm
static vector_erf_fn vector_erf

Typedef Documentation

◆ sgemm_fn

typedef void(* sgemm_fn) (int, int, int, int, int, int, float, const float *, int, const float *, int, float, float *, int)

Definition at line 12 of file linear_rows_sgemm_checked.c.

◆ vector_erf_fn

typedef void(* vector_erf_fn) (int, const float *, float *)

Definition at line 15 of file linear_rows_sgemm_checked.c.

Function Documentation

◆ attend_rows()

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

Definition at line 76 of file linear_rows_sgemm_checked.c.

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}
static sgemm_fn sgemm
uint32_t end
Definition utf8.c:215

References end, and sgemm.

Referenced by attention_impl().

◆ attention_impl()

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

Definition at line 106 of file linear_rows_sgemm_checked.c.

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}
static int attend_rows(const struct spatial_attention *a, size_t begin, size_t end, float *scores)
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)
static sgemm_fn sgemm
static void bind_sgemm(void)
static void attend_rows(size_t begin, size_t end, void *opaque)
static int intersects(const float *a, size_t an, const float *b, size_t bn)
static pthread_once_t once
static int valid_region(const float *p, size_t count)

References attend_rows(), attend_rows(), bind_sgemm(), ck_checked_finite_bulk(), ck_checked_parallel_for(), intersects(), once, sgemm, and valid_region().

Referenced by ck_attention_cross_sgemm_f32(), and ck_attention_cross_sgemm_pool_f32().

◆ bind_sgemm()

void bind_sgemm ( void )
static

Definition at line 19 of file linear_rows_sgemm_checked.c.

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}
static vector_erf_fn vector_erf

References sgemm, and vector_erf.

Referenced by attention_impl(), ck_linear_rows_sgemm_checked(), and gelu_impl().

◆ ck_attention_cross_sgemm_f32()

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 )

Definition at line 172 of file linear_rows_sgemm_checked.c.

175{ return attention_impl(query,qn,key,kn,value,vn,out,on,scratch,sn,queries,memory,heads,dim,0); }
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)

References attention_impl().

◆ ck_attention_cross_sgemm_pool_f32()

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 )

Definition at line 177 of file linear_rows_sgemm_checked.c.

180{ return attention_impl(query,qn,key,kn,value,vn,out,on,scratch,sn,queries,memory,heads,dim,1); }

References attention_impl().

◆ ck_gelu_rows_vector_erf_f32()

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 )

Definition at line 182 of file linear_rows_sgemm_checked.c.

184{ return gelu_impl(x,xn,xs,out,on,os,stage,sn,rows,channels,0); }
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)

References gelu_impl().

◆ ck_gelu_rows_vector_erf_pool_f32()

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 )

Definition at line 186 of file linear_rows_sgemm_checked.c.

188{ return gelu_impl(x,xn,xs,out,on,os,stage,sn,rows,channels,1); }

References gelu_impl().

◆ ck_linear_rows_sgemm_checked()

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 )

Definition at line 45 of file linear_rows_sgemm_checked.c.

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}

References bind_sgemm(), ck_checked_finite_bulk(), intersects(), once, sgemm, sgemm, and valid_region().

◆ gelu_impl()

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 )
static

Definition at line 149 of file linear_rows_sgemm_checked.c.

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}
static int ck_checked_span(size_t rows, size_t width, size_t stride, size_t *span)
static void gelu_range(size_t begin, size_t end, void *opaque)
const uint8_t size_t count
Definition true_bpe.h:211

References bind_sgemm(), ck_checked_finite_bulk(), ck_checked_parallel_for(), ck_checked_span(), count, gelu_range(), intersects(), once, valid_region(), and vector_erf.

Referenced by ck_gelu_rows_vector_erf_f32(), and ck_gelu_rows_vector_erf_pool_f32().

◆ gelu_range()

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

Definition at line 133 of file linear_rows_sgemm_checked.c.

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}

References count, end, and vector_erf.

Referenced by gelu_impl().

◆ intersects()

int intersects ( const float * a,
size_t an,
const float * b,
size_t bn )
static

Definition at line 39 of file linear_rows_sgemm_checked.c.

40{
41 return (uintptr_t)a < (uintptr_t)b + bn*sizeof(float) &&
42 (uintptr_t)b < (uintptr_t)a + an*sizeof(float);
43}

Referenced by attention_impl(), ck_linear_rows_sgemm_checked(), and gelu_impl().

◆ valid_region()

int valid_region ( const float * p,
size_t count )
static

Definition at line 33 of file linear_rows_sgemm_checked.c.

34{
35 return p && (uintptr_t)p % _Alignof(float) == 0 &&
36 count <= (UINTPTR_MAX - (uintptr_t)p) / sizeof(float);
37}

References count.

Referenced by attention_impl(), ck_linear_rows_sgemm_checked(), and gelu_impl().

Variable Documentation

◆ once

pthread_once_t once = PTHREAD_ONCE_INIT
static

◆ sgemm

sgemm_fn sgemm
static

Definition at line 14 of file linear_rows_sgemm_checked.c.

Referenced by attend_rows(), and ck_linear_rows_sgemm_checked().

◆ vector_erf

vector_erf_fn vector_erf
static

Definition at line 16 of file linear_rows_sgemm_checked.c.

Referenced by bind_sgemm(), gelu_impl(), and gelu_range().