← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
decision_scoring_backward.c
Go to the documentation of this file.
1/* Checked indexed scoring VJPs. No allocation, scheduler or model identity. */
4#include <float.h>
5#include <math.h>
6#include <string.h>
7
8static int product(size_t a, size_t b, size_t *out) {
9 if (!a || !b || a > SIZE_MAX/b) return 0;
10 *out=a*b; return 1;
11}
12static int store(float *p, double value) {
13 if (!isfinite(value) || fabs(value)>FLT_MAX) return 0;
14 *p=(float)value; return 1;
15}
16static int validate(const float *left,size_t ln,const float *right,size_t rn,
17 const int32_t *indices,size_t in,const float *dy,size_t yn,
18 float *dl,size_t dln,float *dr,size_t drn,float *scratch,size_t sn,
19 size_t groups,size_t rows,size_t channels,int pair,size_t *nl,size_t *nr) {
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}
34/* One rounded row contribution, then ascending row FP64 scatter reduction.
35 * Never overwrite a repeated owner's gradient with its last option's value. */
36static int publish(const int32_t *indices,float *dl,float *dr,float *scratch,
37 size_t groups,size_t rows,size_t channels,size_t nl,size_t nr) {
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}
47 const float *left,size_t left_elements,const float *right,size_t right_elements,
48 const int32_t *indices,size_t indices_elements,const float *d_output,size_t d_output_elements,
49 float *d_left,size_t d_left_elements,float *d_right,size_t d_right_elements,
50 float *scratch,size_t scratch_elements,size_t groups,size_t rows,size_t channels) {
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}
66static int cosine_backward(
67 const float *left,size_t left_elements,const float *right,size_t right_elements,
68 const int32_t *indices,size_t indices_elements,const float *d_output,size_t d_output_elements,
69 float *d_left,size_t d_left_elements,float *d_right,size_t d_right_elements,
70 float *scratch,size_t scratch_elements,size_t groups,size_t rows,size_t channels,
71 float epsilon,int straight_through_clamp) {
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}
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
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
114#undef ARGS
115#undef PASS
116
117static int scalar_vjp_validate(const float *input, size_t input_elements,
118 const float *dy, size_t dy_elements, float *dx, size_t dx_elements) {
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}
126int ck_scalar_exp_clamped_backward_f32(const float *input, size_t input_elements,
127 const float *d_output, size_t d_output_elements,
128 float *d_input, size_t d_input_elements, float maximum) {
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}
138int ck_scalar_sigmoid_backward_f32(const float *input, size_t input_elements,
139 const float *d_output, size_t d_output_elements,
140 float *d_input, size_t d_input_elements) {
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}
149int ck_scale_vector_backward_f32(const float *input, size_t input_elements,
150 const float *gain, size_t gain_elements, const float *d_output, size_t d_output_elements,
151 float *d_input, size_t d_input_elements, float *d_gain, size_t d_gain_elements,
152 float *scratch, size_t scratch_elements, size_t rows) {
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)
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)
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)
static int store(float *p, double value)
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_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)
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)
static int product(size_t a, size_t b, size_t *out)
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)
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)
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)
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)
const char * left
Definition true_bpe.h:140
const char const char * right
Definition true_bpe.h:141