← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
decision_tensor_ops.c
Go to the documentation of this file.
1/* Checked contiguous tensor operations. No allocation or model scheduling. */
3#include <math.h>
4#include <float.h>
5#include <string.h>
6
7static int region(const void *p, size_t n) {
8 return p && n && (uintptr_t)p % 4 == 0 && n <= SIZE_MAX / 4 &&
9 (uintptr_t)p <= UINTPTR_MAX - n * 4;
10}
11static int overlap(const void *a, size_t n, const void *b, size_t m) {
12 return (uintptr_t)a < (uintptr_t)b + m * 4 &&
13 (uintptr_t)b < (uintptr_t)a + n * 4;
14}
15static int product(size_t a, size_t b, size_t *n) {
16 if (!a || !b || a > SIZE_MAX / b) return 0;
17 *n = a * b; return 1;
18}
19/* Validate all regions before address arithmetic or any scratch mutation. */
20static int buffers(const void **inputs, const size_t *sizes, size_t count,
21 float *out, size_t on, float *scratch, size_t sn, size_t needed) {
22 if (on < needed || sn < needed || !region(out, on) || !region(scratch, sn)) return 0;
23 if (overlap(out, on, scratch, sn)) return 0;
24 for (size_t i=0; i<count; ++i) {
25 if (!region(inputs[i], sizes[i]) || overlap(out,on,inputs[i],sizes[i]) ||
26 overlap(scratch,sn,inputs[i],sizes[i])) return 0;
27 }
28 return 1;
29}
30static int all_finite(const float *x, size_t n) {
31 for (size_t i=0; i<n; ++i) if (!isfinite(x[i])) return 0;
32 return 1;
33}
34static int publish(float *out, float *scratch, size_t n) {
35 if (!all_finite(scratch,n)) return -1;
36 memcpy(out,scratch,n*sizeof(float)); return 0;
37}
38int ck_broadcast_add_rows_f32(const float *left, size_t left_elements,
39 const float *right, size_t right_elements, float *output, size_t output_elements,
40 float *scratch, size_t scratch_elements, size_t rows, size_t channels) {
41 size_t n;
42 if (!product(rows,channels,&n) || left_elements<n || right_elements<channels) return -2;
43 const void *in[]={left,right}; size_t sizes[]={left_elements,right_elements};
44 if (!buffers(in,sizes,2,output,output_elements,scratch,scratch_elements,n) ||
45 !all_finite(left,n) || !all_finite(right,channels)) return -1;
46 for (size_t i=0; i<n; ++i) scratch[i]=left[i]+right[i%channels];
47 return publish(output,scratch,n);
48}
49static int pair_check(const float *left,size_t ln,const float *right,size_t rn,
50 const int32_t *indices,size_t in,float *out,size_t on,float *scratch,size_t sn,
51 size_t groups,size_t rows,size_t channels,size_t need) {
52 size_t l,r;
53 if (!product(groups,channels,&l) || !product(rows,channels,&r) ||
54 ln<l || rn<r || in<rows) return 0;
55 const void *inputs[]={left,right,indices}; size_t sizes[]={ln,rn,in};
56 if (!buffers(inputs,sizes,3,out,on,scratch,sn,need) || !all_finite(left,l) || !all_finite(right,r)) return 0;
57 for (size_t i=0; i<rows; ++i) if (indices[i]<0 || (size_t)indices[i]>=groups) return 0;
58 return 1;
59}
60int ck_indexed_pair_features_f32(const float *left,size_t left_elements,
61 const float *right,size_t right_elements,const int32_t *indices,size_t indices_elements,
62 float *output,size_t output_elements,float *scratch,size_t scratch_elements,
63 size_t groups,size_t rows,size_t channels) {
64 size_t n;
65 if (!product(rows,channels,&n) || n>SIZE_MAX/4) return -2;
66 if (!pair_check(left,left_elements,right,right_elements,indices,indices_elements,
67 output,output_elements,scratch,scratch_elements,groups,rows,channels,n*4)) return -1;
68 for (size_t r=0;r<rows;++r) for (size_t c=0;c<channels;++c) {
69 float a=left[(size_t)indices[r]*channels+c], b=right[r*channels+c];
70 size_t k=r*channels*4+c;
71 scratch[k]=a; scratch[k+channels]=b;
72 scratch[k+2*channels]=a*b; scratch[k+3*channels]=fabsf(a-b);
73 }
74 return publish(output,scratch,n*4);
75}
76int ck_indexed_cosine_rows_f32(const float *left,size_t left_elements,
77 const float *right,size_t right_elements,const int32_t *indices,size_t indices_elements,
78 float *output,size_t output_elements,float *scratch,size_t scratch_elements,
79 size_t groups,size_t rows,size_t channels,float epsilon) {
80 if (!isfinite(epsilon) || epsilon<=0 || !pair_check(left,left_elements,right,right_elements,
81 indices,indices_elements,output,output_elements,scratch,scratch_elements,
82 groups,rows,channels,rows)) return -1;
83 for (size_t r=0;r<rows;++r) {
84 const float *a=left+(size_t)indices[r]*channels, *b=right+r*channels;
85 double aa=0,bb=0;
86 for(size_t c=0;c<channels;++c) {
87 volatile double x=(double)a[c]*a[c], y=(double)b[c]*b[c];
88 aa+=x; bb+=y;
89 }
90 double na=sqrt(aa),nb=sqrt(bb);
91 if (!isfinite(na)||!isfinite(nb)||na>FLT_MAX||nb>FLT_MAX) return -1;
92 float fa=fmaxf((float)na,epsilon), fb=fmaxf((float)nb,epsilon);
93 double sum=0;
94 for(size_t c=0;c<channels;++c) {
95 float x=a[c]/fa,y=b[c]/fb;
96 volatile float term=x*y;
97 sum+=(double)term;
98 }
99 scratch[r]=(float)sum;
100 }
101 return publish(output,scratch,rows);
102}
103int ck_indexed_scaled_dot_rows_f32(const float *left, size_t left_elements,
104 const float *right, size_t right_elements, const int32_t *indices, size_t indices_elements,
105 float *output, size_t output_elements, float *scratch, size_t scratch_elements,
106 size_t groups, size_t rows, size_t channels, float scale) {
107 if (!isfinite(scale) || !pair_check(left, left_elements, right, right_elements,
108 indices, indices_elements, output, output_elements, scratch, scratch_elements,
109 groups, rows, channels, rows)) return -1;
110 for (size_t row = 0; row < rows; ++row) {
111 const float *query = left + (size_t)indices[row] * channels;
112 const float *key = right + row * channels;
113 double sum = 0.0;
114 for (size_t channel = 0; channel < channels; ++channel)
115 sum += (double)query[channel] * key[channel];
116 if (!isfinite(sum) || fabs(sum) > FLT_MAX) return -1;
117 scratch[row] = (float)sum * scale;
118 }
119 return publish(output, scratch, rows);
120}
121
122static int scalar_check(const float *input,size_t in,float *output,size_t on) {
123 return region(input,in)&&region(output,on)&&!overlap(input,in,output,on)&&isfinite(input[0]);
124}
125int ck_scalar_exp_clamped_f32(const float *input,size_t input_elements,
126 float *output,size_t output_elements,float maximum) {
127 if (!scalar_check(input,input_elements,output,output_elements)||!isfinite(maximum)) return -1;
128 float value=expf(fminf(input[0],maximum));
129 if (!isfinite(value)) return -1;
130 output[0]=value; return 0;
131}
132int ck_scalar_sigmoid_f32(const float *input,size_t input_elements,
133 float *output,size_t output_elements) {
134 if (!scalar_check(input,input_elements,output,output_elements)) return -1;
135 float x=input[0],e=expf(-fabsf(x));
136 float value=x>=0 ? 1.0f/(1.0f+e) : e/(1.0f+e);
137 output[0]=value; return 0;
138}
139int ck_scale_vector_f32(const float *input,size_t input_elements,
140 const float *gain,size_t gain_elements,float *output,size_t output_elements,
141 float *scratch,size_t scratch_elements,size_t rows) {
142 if(!rows || input_elements<rows || gain_elements<1) return -2;
143 const void *inputs[]={input,gain}; size_t sizes[]={input_elements,gain_elements};
144 if(!buffers(inputs,sizes,2,output,output_elements,scratch,scratch_elements,rows)||
145 !all_finite(input,rows)||!all_finite(gain,1)) return -1;
146 for(size_t r=0;r<rows;++r) scratch[r]=input[r]*gain[0];
147 return publish(output,scratch,rows);
148}
149
150int ck_prefix_softmax_rows_f32(const float *logits, size_t logits_elements,
151 const int32_t *counts, size_t count_elements, float *output, size_t output_elements,
152 size_t rows, size_t columns) {
153 size_t used;
154 if (!product(rows, columns, &used) || logits_elements < used ||
155 output_elements < used || count_elements < rows ||
156 !region(logits, logits_elements) || !region(counts, count_elements) ||
157 !region(output, output_elements) ||
158 overlap(output, output_elements, logits, logits_elements) ||
159 overlap(output, output_elements, counts, count_elements)) return -1;
160
161 for (size_t row = 0; row < rows; ++row) {
162 int32_t depth = counts[row];
163 if (depth < 1 || (size_t)depth > columns ||
164 !all_finite(logits + row * columns, (size_t)depth)) return -1;
165 }
166 for (size_t row = 0; row < rows; ++row) {
167 const float *scores = logits + row * columns;
168 float *probabilities = output + row * columns;
169 size_t depth = (size_t)counts[row];
170 float maximum = scores[0];
171 for (size_t column = 1; column < depth; ++column)
172 if (scores[column] > maximum) maximum = scores[column];
173 double denominator = 0.0;
174 for (size_t column = 0; column < depth; ++column) {
175 probabilities[column] = (float)exp((double)scores[column] - maximum);
176 denominator += probabilities[column];
177 }
178 for (size_t column = 0; column < depth; ++column)
179 probabilities[column] = (float)(probabilities[column] / denominator);
180 for (size_t column = depth; column < columns; ++column)
181 probabilities[column] = 0.0f;
182 }
183 return 0;
184}
static int buffers(const void **inputs, const size_t *sizes, size_t count, float *out, size_t on, float *scratch, size_t sn, size_t needed)
static int overlap(const void *a, size_t n, const void *b, size_t m)
int ck_prefix_softmax_rows_f32(const float *logits, size_t logits_elements, const int32_t *counts, size_t count_elements, float *output, size_t output_elements, size_t rows, size_t columns)
static int pair_check(const float *left, size_t ln, const float *right, size_t rn, const int32_t *indices, size_t in, float *out, size_t on, float *scratch, size_t sn, size_t groups, size_t rows, size_t channels, size_t need)
int ck_scale_vector_f32(const float *input, size_t input_elements, const float *gain, size_t gain_elements, float *output, size_t output_elements, float *scratch, size_t scratch_elements, size_t rows)
static int publish(float *out, float *scratch, size_t n)
int ck_indexed_cosine_rows_f32(const float *left, size_t left_elements, const float *right, size_t right_elements, const int32_t *indices, size_t indices_elements, float *output, size_t output_elements, float *scratch, size_t scratch_elements, size_t groups, size_t rows, size_t channels, float epsilon)
static int region(const void *p, size_t n)
static int product(size_t a, size_t b, size_t *n)
static int all_finite(const float *x, size_t n)
int ck_broadcast_add_rows_f32(const float *left, size_t left_elements, const float *right, size_t right_elements, float *output, size_t output_elements, float *scratch, size_t scratch_elements, size_t rows, size_t channels)
int ck_indexed_pair_features_f32(const float *left, size_t left_elements, const float *right, size_t right_elements, const int32_t *indices, size_t indices_elements, float *output, size_t output_elements, float *scratch, size_t scratch_elements, size_t groups, size_t rows, size_t channels)
int ck_indexed_scaled_dot_rows_f32(const float *left, size_t left_elements, const float *right, size_t right_elements, const int32_t *indices, size_t indices_elements, float *output, size_t output_elements, float *scratch, size_t scratch_elements, size_t groups, size_t rows, size_t channels, float scale)
int ck_scalar_sigmoid_f32(const float *input, size_t input_elements, float *output, size_t output_elements)
static int scalar_check(const float *input, size_t in, float *output, size_t on)
int ck_scalar_exp_clamped_f32(const float *input, size_t input_elements, float *output, size_t output_elements, float maximum)
const char * left
Definition true_bpe.h:140
const uint8_t size_t count
Definition true_bpe.h:211
const char const char * right
Definition true_bpe.h:141