← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
decision_tensor_ops.c File Reference
#include "ckernel_decision_tensor_ops.h"
#include <math.h>
#include <float.h>
#include <string.h>

Go to the source code of this file.

Functions

static int all_finite (const float *x, size_t n)
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)
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_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)
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_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)
int ck_scalar_exp_clamped_f32 (const float *input, size_t input_elements, float *output, size_t output_elements, float maximum)
int ck_scalar_sigmoid_f32 (const float *input, size_t input_elements, float *output, size_t output_elements)
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 overlap (const void *a, size_t n, const void *b, size_t m)
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)
static int product (size_t a, size_t b, size_t *n)
static int publish (float *out, float *scratch, size_t n)
static int region (const void *p, size_t n)
static int scalar_check (const float *input, size_t in, float *output, size_t on)

Function Documentation

◆ all_finite()

int all_finite ( const float * x,
size_t n )
static

Definition at line 30 of file decision_tensor_ops.c.

30 {
31 for (size_t i=0; i<n; ++i) if (!isfinite(x[i])) return 0;
32 return 1;
33}

Referenced by ck_broadcast_add_rows_f32(), ck_prefix_softmax_rows_f32(), ck_scale_vector_f32(), pair_check(), and publish().

◆ buffers()

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

Definition at line 20 of file decision_tensor_ops.c.

21 {
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}
static int overlap(const void *a, size_t n, const void *b, size_t m)
static int region(const void *p, size_t n)
const uint8_t size_t count
Definition true_bpe.h:211

References count, overlap(), and region().

Referenced by ck_adaptive_rmsnorm_rows_bf16_checked(), ck_broadcast_add_rows_f32(), ck_ir_v2_parse_buffers(), ck_scale_vector_f32(), ck_video_spatial_attention_bf16_checked(), inverse_transform(), pair_check(), and routed_softcapped_experts().

◆ ck_broadcast_add_rows_f32()

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 )

Definition at line 38 of file decision_tensor_ops.c.

40 {
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}
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 publish(float *out, float *scratch, 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)
const char * left
Definition true_bpe.h:140
const char const char * right
Definition true_bpe.h:141

References all_finite(), buffers(), left, product(), publish(), and right.

◆ ck_indexed_cosine_rows_f32()

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 )

Definition at line 76 of file decision_tensor_ops.c.

79 {
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}
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)

References left, pair_check(), publish(), and right.

◆ ck_indexed_pair_features_f32()

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 )

Definition at line 60 of file decision_tensor_ops.c.

63 {
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}

References left, pair_check(), product(), publish(), and right.

◆ ck_indexed_scaled_dot_rows_f32()

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 )

Definition at line 103 of file decision_tensor_ops.c.

106 {
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}

References left, pair_check(), publish(), and right.

◆ ck_prefix_softmax_rows_f32()

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 )

Definition at line 150 of file decision_tensor_ops.c.

152 {
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}

References all_finite(), overlap(), product(), and region().

◆ ck_scalar_exp_clamped_f32()

int ck_scalar_exp_clamped_f32 ( const float * input,
size_t input_elements,
float * output,
size_t output_elements,
float maximum )

Definition at line 125 of file decision_tensor_ops.c.

126 {
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}
static int scalar_check(const float *input, size_t in, float *output, size_t on)

References scalar_check().

◆ ck_scalar_sigmoid_f32()

int ck_scalar_sigmoid_f32 ( const float * input,
size_t input_elements,
float * output,
size_t output_elements )

Definition at line 132 of file decision_tensor_ops.c.

133 {
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}

References scalar_check().

◆ ck_scale_vector_f32()

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 )

Definition at line 139 of file decision_tensor_ops.c.

141 {
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}

References all_finite(), buffers(), and publish().

◆ overlap()

int overlap ( const void * a,
size_t n,
const void * b,
size_t m )
static

Definition at line 11 of file decision_tensor_ops.c.

11 {
12 return (uintptr_t)a < (uintptr_t)b + m * 4 &&
13 (uintptr_t)b < (uintptr_t)a + n * 4;
14}

Referenced by audio_istft_mag_phase_f32(), buffers(), ck_prefix_softmax_rows_f32(), geglu_forward_ggml_native(), and scalar_check().

◆ pair_check()

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

Definition at line 49 of file decision_tensor_ops.c.

51 {
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}

References all_finite(), buffers(), left, product(), and right.

Referenced by ck_indexed_cosine_rows_f32(), ck_indexed_pair_features_f32(), and ck_indexed_scaled_dot_rows_f32().

◆ product()

int product ( size_t a,
size_t b,
size_t * n )
static

Definition at line 15 of file decision_tensor_ops.c.

15 {
16 if (!a || !b || a > SIZE_MAX / b) return 0;
17 *n = a * b; return 1;
18}

Referenced by ck_broadcast_add_rows_f32(), ck_indexed_pair_features_f32(), ck_prefix_softmax_rows_f32(), and pair_check().

◆ publish()

int publish ( float * out,
float * scratch,
size_t n )
static

Definition at line 34 of file decision_tensor_ops.c.

34 {
35 if (!all_finite(scratch,n)) return -1;
36 memcpy(out,scratch,n*sizeof(float)); return 0;
37}

References all_finite().

Referenced by ck_broadcast_add_rows_f32(), ck_indexed_cosine_rows_f32(), ck_indexed_pair_features_f32(), ck_indexed_scaled_dot_rows_f32(), and ck_scale_vector_f32().

◆ region()

int region ( const void * p,
size_t n )
static

Definition at line 7 of file decision_tensor_ops.c.

7 {
8 return p && n && (uintptr_t)p % 4 == 0 && n <= SIZE_MAX / 4 &&
9 (uintptr_t)p <= UINTPTR_MAX - n * 4;
10}

Referenced by buffers(), ck_generate_diffusion_block(), ck_generate_seeded_diffusion(), ck_linear_rows_bias_bf16_avx2_panel_checked(), ck_prefix_softmax_rows_f32(), encode_clean(), region_valid(), scalar_check(), and valid_session().

◆ scalar_check()

int scalar_check ( const float * input,
size_t in,
float * output,
size_t on )
static

Definition at line 122 of file decision_tensor_ops.c.

122 {
123 return region(input,in)&&region(output,on)&&!overlap(input,in,output,on)&&isfinite(input[0]);
124}

References overlap(), and region().

Referenced by ck_scalar_exp_clamped_f32(), and ck_scalar_sigmoid_f32().