← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
ckernel_decision_tensor_ops.h File Reference
#include <stddef.h>
#include <stdint.h>

Go to the source code of this file.

Functions

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)

Function Documentation

◆ 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}
static int overlap(const void *a, size_t n, const void *b, size_t m)
static int region(const void *p, size_t n)

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().