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

Go to the source code of this file.

Macros

#define ARGS
#define PASS

Functions

int ck_indexed_cosine_rows_backward_f32 (const float *left, size_t left_elements, const float *right, size_t right_elements, const int32_t *indices, size_t indices_elements, const float *d_output, size_t d_output_elements, float *d_left, size_t d_left_elements, float *d_right, size_t d_right_elements, float *scratch, size_t scratch_elements, size_t groups, size_t rows, size_t channels, float epsilon)
int ck_indexed_normalized_dot_backward_f32 (const float *left, size_t left_elements, const float *right, size_t right_elements, const int32_t *indices, size_t indices_elements, const float *d_output, size_t d_output_elements, float *d_left, size_t d_left_elements, float *d_right, size_t d_right_elements, float *scratch, size_t scratch_elements, size_t groups, size_t rows, size_t channels, float epsilon)
int ck_indexed_pair_features_backward_f32 (const float *left, size_t left_elements, const float *right, size_t right_elements, const int32_t *indices, size_t indices_elements, const float *d_output, size_t d_output_elements, float *d_left, size_t d_left_elements, float *d_right, size_t d_right_elements, float *scratch, size_t scratch_elements, size_t groups, size_t rows, size_t channels)
int ck_scalar_exp_clamped_backward_f32 (const float *input, size_t input_elements, const float *d_output, size_t d_output_elements, float *d_input, size_t d_input_elements, float maximum)
int ck_scalar_sigmoid_backward_f32 (const float *input, size_t input_elements, const float *d_output, size_t d_output_elements, float *d_input, size_t d_input_elements)
int ck_scale_vector_backward_f32 (const float *input, size_t input_elements, const float *gain, size_t gain_elements, const float *d_output, size_t d_output_elements, float *d_input, size_t d_input_elements, float *d_gain, size_t d_gain_elements, float *scratch, size_t scratch_elements, size_t rows)
static int cosine_backward (const float *left, size_t left_elements, const float *right, size_t right_elements, const int32_t *indices, size_t indices_elements, const float *d_output, size_t d_output_elements, float *d_left, size_t d_left_elements, float *d_right, size_t d_right_elements, float *scratch, size_t scratch_elements, size_t groups, size_t rows, size_t channels, float epsilon, int straight_through_clamp)
static int product (size_t a, size_t b, size_t *out)
static int publish (const int32_t *indices, float *dl, float *dr, float *scratch, size_t groups, size_t rows, size_t channels, size_t nl, size_t nr)
static int scalar_vjp_validate (const float *input, size_t input_elements, const float *dy, size_t dy_elements, float *dx, size_t dx_elements)
static int store (float *p, double value)
static int validate (const float *left, size_t ln, const float *right, size_t rn, const int32_t *indices, size_t in, const float *dy, size_t yn, float *dl, size_t dln, float *dr, size_t drn, float *scratch, size_t sn, size_t groups, size_t rows, size_t channels, int pair, size_t *nl, size_t *nr)

Macro Definition Documentation

◆ ARGS

#define ARGS
Value:
const float *left,size_t left_elements,const float *right,size_t right_elements, \
const int32_t *indices,size_t indices_elements,const float *d_output,size_t d_output_elements, \
float *d_left,size_t d_left_elements,float *d_right,size_t d_right_elements, \
float *scratch,size_t scratch_elements,size_t groups,size_t rows,size_t channels,float epsilon
const char * left
Definition true_bpe.h:140
const char const char * right
Definition true_bpe.h:141

Definition at line 106 of file decision_scoring_backward.c.

106#define ARGS const float *left,size_t left_elements,const float *right,size_t right_elements, \
107 const int32_t *indices,size_t indices_elements,const float *d_output,size_t d_output_elements, \
108 float *d_left,size_t d_left_elements,float *d_right,size_t d_right_elements, \
109 float *scratch,size_t scratch_elements,size_t groups,size_t rows,size_t channels,float epsilon

Referenced by ck_indexed_cosine_rows_backward_f32(), and ck_indexed_normalized_dot_backward_f32().

◆ PASS

#define PASS
Value:
left,left_elements,right,right_elements,indices,indices_elements,d_output,d_output_elements, \
d_left,d_left_elements,d_right,d_right_elements,scratch,scratch_elements,groups,rows,channels,epsilon

Definition at line 110 of file decision_scoring_backward.c.

110#define PASS left,left_elements,right,right_elements,indices,indices_elements,d_output,d_output_elements, \
111 d_left,d_left_elements,d_right,d_right_elements,scratch,scratch_elements,groups,rows,channels,epsilon

Referenced by ck_indexed_cosine_rows_backward_f32(), and ck_indexed_normalized_dot_backward_f32().

Function Documentation

◆ ck_indexed_cosine_rows_backward_f32()

int ck_indexed_cosine_rows_backward_f32 ( const float * left,
size_t left_elements,
const float * right,
size_t right_elements,
const int32_t * indices,
size_t indices_elements,
const float * d_output,
size_t d_output_elements,
float * d_left,
size_t d_left_elements,
float * d_right,
size_t d_right_elements,
float * scratch,
size_t scratch_elements,
size_t groups,
size_t rows,
size_t channels,
float epsilon )

Definition at line 113 of file decision_scoring_backward.c.

113{return cosine_backward(PASS,1);}
static int cosine_backward(const float *left, size_t left_elements, const float *right, size_t right_elements, const int32_t *indices, size_t indices_elements, const float *d_output, size_t d_output_elements, float *d_left, size_t d_left_elements, float *d_right, size_t d_right_elements, float *scratch, size_t scratch_elements, size_t groups, size_t rows, size_t channels, float epsilon, int straight_through_clamp)

References ARGS, cosine_backward(), and PASS.

◆ ck_indexed_normalized_dot_backward_f32()

int ck_indexed_normalized_dot_backward_f32 ( const float * left,
size_t left_elements,
const float * right,
size_t right_elements,
const int32_t * indices,
size_t indices_elements,
const float * d_output,
size_t d_output_elements,
float * d_left,
size_t d_left_elements,
float * d_right,
size_t d_right_elements,
float * scratch,
size_t scratch_elements,
size_t groups,
size_t rows,
size_t channels,
float epsilon )

Definition at line 112 of file decision_scoring_backward.c.

112{return cosine_backward(PASS,0);}

References ARGS, cosine_backward(), and PASS.

◆ ck_indexed_pair_features_backward_f32()

int ck_indexed_pair_features_backward_f32 ( const float * left,
size_t left_elements,
const float * right,
size_t right_elements,
const int32_t * indices,
size_t indices_elements,
const float * d_output,
size_t d_output_elements,
float * d_left,
size_t d_left_elements,
float * d_right,
size_t d_right_elements,
float * scratch,
size_t scratch_elements,
size_t groups,
size_t rows,
size_t channels )

Definition at line 46 of file decision_scoring_backward.c.

50 {
51 size_t nl,nr;
52 int rc=validate(left,left_elements,right,right_elements,indices,indices_elements,d_output,d_output_elements,
53 d_left,d_left_elements,d_right,d_right_elements,scratch,scratch_elements,groups,rows,channels,1,&nl,&nr);
54 if(rc) return rc;
55 for(size_t r=0;r<rows;++r) for(size_t c=0;c<channels;++c) {
56 double a=left[(size_t)indices[r]*channels+c],b=right[r*channels+c];
57 const float *dy=d_output+4*r*channels+c;
58 double sign=(a>b)-(a<b); /* abs'(0)=0, including signed zeros. */
59 volatile double pa=(double)dy[2*channels]*b, pb=(double)dy[2*channels]*a;
60 volatile double diff=(double)dy[3*channels]*sign;
61 if(!store(scratch+nl+r*channels+c,((double)dy[0]+pa)+diff) ||
62 !store(scratch+nl+nr+r*channels+c,((double)dy[channels]+pb)-diff)) return -4;
63 }
64 return publish(indices,d_left,d_right,scratch,groups,rows,channels,nl,nr);
65}
static int store(float *p, double value)
static int validate(const float *left, size_t ln, const float *right, size_t rn, const int32_t *indices, size_t in, const float *dy, size_t yn, float *dl, size_t dln, float *dr, size_t drn, float *scratch, size_t sn, size_t groups, size_t rows, size_t channels, int pair, size_t *nl, size_t *nr)
static int publish(const int32_t *indices, float *dl, float *dr, float *scratch, size_t groups, size_t rows, size_t channels, size_t nl, size_t nr)

References left, publish(), right, store(), and validate().

◆ ck_scalar_exp_clamped_backward_f32()

int ck_scalar_exp_clamped_backward_f32 ( const float * input,
size_t input_elements,
const float * d_output,
size_t d_output_elements,
float * d_input,
size_t d_input_elements,
float maximum )

Definition at line 126 of file decision_scoring_backward.c.

128 {
129 int rc=scalar_vjp_validate(input,input_elements,d_output,d_output_elements,d_input,d_input_elements);
130 if (rc) return rc;
131 if (!isfinite(maximum)) return -2;
132 float y=expf(fminf(input[0],maximum)), result;
133 if (!isfinite(y)) return -4;
134 /* clamp(max=...) has derivative one AT the boundary, zero above it. */
135 if (!store(&result,input[0]<=maximum ? (double)d_output[0]*y : 0.0)) return -4;
136 d_input[0]=result; return 0;
137}
static int scalar_vjp_validate(const float *input, size_t input_elements, const float *dy, size_t dy_elements, float *dx, size_t dx_elements)

References scalar_vjp_validate(), and store().

◆ ck_scalar_sigmoid_backward_f32()

int ck_scalar_sigmoid_backward_f32 ( const float * input,
size_t input_elements,
const float * d_output,
size_t d_output_elements,
float * d_input,
size_t d_input_elements )

Definition at line 138 of file decision_scoring_backward.c.

140 {
141 int rc=scalar_vjp_validate(input,input_elements,d_output,d_output_elements,d_input,d_input_elements);
142 if (rc) return rc;
143 float x=input[0], e=expf(-fabsf(x));
144 float y=x>=0 ? 1.0f/(1.0f+e) : e/(1.0f+e), result;
145 volatile double slope=(double)y*(1.0-(double)y);
146 if (!store(&result,(double)d_output[0]*slope)) return -4;
147 d_input[0]=result; return 0;
148}

References scalar_vjp_validate(), and store().

◆ ck_scale_vector_backward_f32()

int ck_scale_vector_backward_f32 ( const float * input,
size_t input_elements,
const float * gain,
size_t gain_elements,
const float * d_output,
size_t d_output_elements,
float * d_input,
size_t d_input_elements,
float * d_gain,
size_t d_gain_elements,
float * scratch,
size_t scratch_elements,
size_t rows )

Definition at line 149 of file decision_scoring_backward.c.

152 {
153 if (!rows || rows==SIZE_MAX || input_elements<rows || gain_elements<1 ||
154 d_output_elements<rows || d_input_elements<rows || d_gain_elements<1 ||
155 scratch_elements<rows+1) return -2;
156 const void *ptrs[]={input,gain,d_output,d_input,d_gain,scratch};
157 size_t sizes[]={input_elements,gain_elements,d_output_elements,d_input_elements,d_gain_elements,scratch_elements};
158 for (size_t i=0;i<6;++i) if (!ck_checked_region(ptrs[i],sizes[i])) return -1;
159 for (size_t i=3;i<6;++i) for (size_t j=0;j<i;++j)
160 if (ck_checked_overlap(ptrs[i],sizes[i],ptrs[j],sizes[j])) return -1;
161 if (!ck_checked_finite(input,rows) || !ck_checked_finite(gain,1) || !ck_checked_finite(d_output,rows)) return -3;
162 double sum=0;
163 for (size_t r=0;r<rows;++r) {
164 if (!store(scratch+r,(double)d_output[r]*gain[0])) return -4;
165 volatile double product=(double)d_output[r]*input[r]; sum+=product;
166 }
167 if (!store(scratch+rows,sum)) return -4;
168 memcpy(d_input,scratch,rows*sizeof(float)); d_gain[0]=scratch[rows]; return 0;
169}
static int product(size_t a, size_t b, size_t *out)
static int ck_checked_finite(const float *ptr, size_t n)
static int ck_checked_region(const float *ptr, size_t n)
static int ck_checked_overlap(const float *a, size_t na, const float *b, size_t nb)

References ck_checked_finite(), ck_checked_overlap(), ck_checked_region(), product(), and store().

◆ cosine_backward()

int cosine_backward ( const float * left,
size_t left_elements,
const float * right,
size_t right_elements,
const int32_t * indices,
size_t indices_elements,
const float * d_output,
size_t d_output_elements,
float * d_left,
size_t d_left_elements,
float * d_right,
size_t d_right_elements,
float * scratch,
size_t scratch_elements,
size_t groups,
size_t rows,
size_t channels,
float epsilon,
int straight_through_clamp )
static

Definition at line 66 of file decision_scoring_backward.c.

71 {
72 size_t nl,nr;
73 if(!isfinite(epsilon)||epsilon<=0) return -2;
74 int rc=validate(left,left_elements,right,right_elements,indices,indices_elements,d_output,d_output_elements,
75 d_left,d_left_elements,d_right,d_right_elements,scratch,scratch_elements,groups,rows,channels,0,&nl,&nr);
76 if(rc) return rc;
77 for(size_t r=0;r<rows;++r) {
78 const float *a=left+(size_t)indices[r]*channels,*b=right+r*channels;
79 double aa=0,bb=0;
80 for(size_t c=0;c<channels;++c) {
81 volatile double ap=(double)a[c]*a[c],bp=(double)b[c]*b[c];aa+=ap;bb+=bp;
82 }
83 double na=sqrt(aa),nb=sqrt(bb);
84 if(!isfinite(na)||!isfinite(nb)||na>FLT_MAX||nb>FLT_MAX) return -4;
85 float raw_a=(float)na,raw_b=(float)nb,fa=fmaxf(raw_a,epsilon),fb=fmaxf(raw_b,epsilon);
86 double xy=0;
87 for(size_t c=0;c<channels;++c) {
88 float x=a[c]/fa,y=b[c]/fb;volatile float term=x*y;xy+=(double)term;
89 }
90 /* normalize(...).clamp_min has zero derivative below epsilon.
91 * PyTorch 2.12.1 cosine_similarity clamps a cloned norm under NoGrad,
92 * retaining the norm derivative there. Zero norm still has zero VJP. */
93 int da=raw_a>0 && (straight_through_clamp || raw_a>=epsilon);
94 int db=raw_b>0 && (straight_through_clamp || raw_b>=epsilon);
95 for(size_t c=0;c<channels;++c) {
96 float x=a[c]/fa,y=b[c]/fb;
97 double ga=(double)y/fa,gb=(double)x/fb;
98 if(da) ga-=xy*((double)a[c]/raw_a)/fa;
99 if(db) gb-=xy*((double)b[c]/raw_b)/fb;
100 if(!store(scratch+nl+r*channels+c,(double)d_output[r]*ga) ||
101 !store(scratch+nl+nr+r*channels+c,(double)d_output[r]*gb)) return -4;
102 }
103 }
104 return publish(indices,d_left,d_right,scratch,groups,rows,channels,nl,nr);
105}

References left, publish(), right, store(), and validate().

Referenced by ck_indexed_cosine_rows_backward_f32(), and ck_indexed_normalized_dot_backward_f32().

◆ product()

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

Definition at line 8 of file decision_scoring_backward.c.

8 {
9 if (!a || !b || a > SIZE_MAX/b) return 0;
10 *out=a*b; return 1;
11}

Referenced by validate().

◆ publish()

int publish ( const int32_t * indices,
float * dl,
float * dr,
float * scratch,
size_t groups,
size_t rows,
size_t channels,
size_t nl,
size_t nr )
static

Definition at line 36 of file decision_scoring_backward.c.

37 {
38 float *row_left=scratch+nl, *row_right=row_left+nr;
39 for(size_t g=0;g<groups;++g) for(size_t c=0;c<channels;++c) {
40 double sum=0;
41 for(size_t r=0;r<rows;++r) if((size_t)indices[r]==g) sum+=(double)row_left[r*channels+c];
42 if(!store(scratch+g*channels+c,sum)) return -4;
43 }
44 memcpy(dl,scratch,nl*sizeof(float)); memcpy(dr,row_right,nr*sizeof(float)); return 0;
45}

References store().

Referenced by ck_indexed_pair_features_backward_f32(), cosine_backward(), linear_rows_q8_0_checked(), and rope_split_partial_position_f32_checked().

◆ scalar_vjp_validate()

int scalar_vjp_validate ( const float * input,
size_t input_elements,
const float * dy,
size_t dy_elements,
float * dx,
size_t dx_elements )
static

Definition at line 117 of file decision_scoring_backward.c.

118 {
119 if (!input_elements || !dy_elements || !dx_elements) return -2;
120 if (!ck_checked_region(input,input_elements) || !ck_checked_region(dy,dy_elements) ||
121 !ck_checked_region(dx,dx_elements) ||
122 ck_checked_overlap(dx,dx_elements,input,input_elements) ||
123 ck_checked_overlap(dx,dx_elements,dy,dy_elements)) return -1;
124 return isfinite(input[0]) && isfinite(dy[0]) ? 0 : -3;
125}

References ck_checked_overlap(), and ck_checked_region().

Referenced by ck_scalar_exp_clamped_backward_f32(), and ck_scalar_sigmoid_backward_f32().

◆ store()

int store ( float * p,
double value )
static

Definition at line 12 of file decision_scoring_backward.c.

12 {
13 if (!isfinite(value) || fabs(value)>FLT_MAX) return 0;
14 *p=(float)value; return 1;
15}

Referenced by ck_indexed_pair_features_backward_f32(), ck_scalar_exp_clamped_backward_f32(), ck_scalar_sigmoid_backward_f32(), ck_scale_vector_backward_f32(), cosine_backward(), and publish().

◆ validate()

int validate ( const float * left,
size_t ln,
const float * right,
size_t rn,
const int32_t * indices,
size_t in,
const float * dy,
size_t yn,
float * dl,
size_t dln,
float * dr,
size_t drn,
float * scratch,
size_t sn,
size_t groups,
size_t rows,
size_t channels,
int pair,
size_t * nl,
size_t * nr )
static

Definition at line 16 of file decision_scoring_backward.c.

19 {
20 size_t ny;
21 if (!product(groups,channels,nl) || !product(rows,channels,nr) ||
22 *nr>(SIZE_MAX-*nl)/2 || (pair && *nr>SIZE_MAX/4)) return -2;
23 ny=pair ? 4*(*nr) : rows;
24 if (ln<*nl || rn<*nr || in<rows || yn<ny || dln<*nl || drn<*nr || sn<*nl+2*(*nr)) return -2;
25 const void *ptrs[]={left,right,indices,dy,dl,dr,scratch};
26 size_t sizes[]={ln,rn,in,yn,dln,drn,sn};
27 for (size_t i=0;i<7;++i) if (!ck_checked_region(ptrs[i],sizes[i])) return -1;
28 for (size_t i=4;i<7;++i) for(size_t j=0;j<i;++j)
29 if (ck_checked_overlap(ptrs[i],sizes[i],ptrs[j],sizes[j])) return -1;
30 for(size_t r=0;r<rows;++r) if(indices[r]<0 || (size_t)indices[r]>=groups) return -2;
31 if (!ck_checked_finite(left,*nl) || !ck_checked_finite(right,*nr) || !ck_checked_finite(dy,ny)) return -3;
32 return 0;
33}
static int product(size_t a, size_t b, size_t *out)

References ck_checked_finite(), ck_checked_overlap(), ck_checked_region(), left, product(), and right.

Referenced by ck_indexed_pair_features_backward_f32(), and cosine_backward().