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

Go to the source code of this file.

Functions

int ck_attention_cross_backward_f32 (const float *query, size_t query_elements, const float *key, size_t key_elements, const float *value, size_t value_elements, const float *d_output, size_t d_output_elements, float *d_query, size_t d_query_elements, float *d_key, size_t d_key_elements, float *d_value, size_t d_value_elements, float *scratch, size_t scratch_elements, size_t query_tokens, size_t memory_tokens, size_t heads, size_t head_dim)
int ck_gelu_erf_backward_bf16 (const uint16_t *input, size_t input_elements, size_t input_stride, const uint16_t *d_output, size_t d_output_elements, size_t d_output_stride, uint16_t *d_input, size_t d_input_elements, size_t d_input_stride, float *scratch, size_t scratch_elements, size_t rows, size_t channels)
int ck_gelu_erf_backward_f32 (const float *input, size_t input_elements, size_t input_stride, const float *d_output, size_t d_output_elements, size_t d_output_stride, float *d_input, size_t d_input_elements, size_t d_input_stride, float *scratch, size_t scratch_elements, size_t rows, size_t channels)
int ck_grouped_weighted_rows_backward_bf16 (const uint16_t *options, size_t option_elements, size_t option_stride, const uint16_t *fields, size_t field_elements, size_t field_stride, const int32_t *group_counts, size_t count_elements, const uint16_t *d_output, size_t d_output_elements, size_t d_output_stride, uint16_t *d_options, size_t d_options_elements, size_t d_options_stride, uint16_t *d_fields, size_t d_fields_elements, size_t d_fields_stride, float *scratch, size_t scratch_elements, size_t groups, size_t option_rows, size_t channels)
int ck_grouped_weighted_rows_backward_f32 (const float *options, size_t option_elements, size_t option_stride, const float *fields, size_t field_elements, size_t field_stride, const int32_t *group_counts, size_t count_elements, const float *d_output, size_t d_output_elements, size_t d_output_stride, float *d_options, size_t d_options_elements, size_t d_options_stride, float *d_fields, size_t d_fields_elements, size_t d_fields_stride, float *scratch, size_t scratch_elements, size_t groups, size_t option_rows, size_t channels)
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)
int ck_token_span_mean_backward_bf16 (const uint16_t *d_output, size_t d_output_elements, size_t d_output_stride, const int32_t *spans, size_t span_elements, size_t span_count, uint16_t *d_input, size_t d_input_elements, size_t d_input_stride, float *scratch, size_t scratch_elements, size_t token_capacity, size_t valid_tokens, size_t channels)
int ck_token_span_mean_backward_f32 (const float *d_output, size_t d_output_elements, size_t d_output_stride, const int32_t *spans, size_t span_elements, size_t span_count, float *d_input, size_t d_input_elements, size_t d_input_stride, float *scratch, size_t scratch_elements, size_t token_capacity, size_t valid_tokens, size_t channels)

Function Documentation

◆ ck_attention_cross_backward_f32()

int ck_attention_cross_backward_f32 ( const float * query,
size_t query_elements,
const float * key,
size_t key_elements,
const float * value,
size_t value_elements,
const float * d_output,
size_t d_output_elements,
float * d_query,
size_t d_query_elements,
float * d_key,
size_t d_key_elements,
float * d_value,
size_t d_value_elements,
float * scratch,
size_t scratch_elements,
size_t query_tokens,
size_t memory_tokens,
size_t heads,
size_t head_dim )

Definition at line 116 of file attention_cross_token_major_checked.c.

125 {
126 if (!query_tokens || !memory_tokens || !heads || !head_dim || heads > SIZE_MAX / head_dim) return -2;
127 size_t width = heads * head_dim;
128 if (query_tokens > SIZE_MAX / width || memory_tokens > SIZE_MAX / width) return -2;
129 size_t nq = query_tokens * width, nm = memory_tokens * width;
130 if (nm > (SIZE_MAX - nq) / 2) return -2;
131 size_t staged = nq + 2 * nm;
132 if (memory_tokens > (SIZE_MAX - staged) / 2) return -2;
133 size_t needed = staged + 2 * memory_tokens;
134 if (query_elements < nq || key_elements < nm || value_elements < nm ||
135 d_output_elements < nq || d_query_elements < nq || d_key_elements < nm ||
136 d_value_elements < nm || scratch_elements < needed) return -2;
137 const float *readers[] = {query, key, value, d_output};
138 size_t nr[] = {query_elements, key_elements, value_elements, d_output_elements};
139 float *writers[] = {d_query, d_key, d_value, scratch};
140 size_t nw[] = {d_query_elements, d_key_elements, d_value_elements, scratch_elements};
141 for (size_t i = 0; i < 4; ++i) {
142 if (!ck_checked_region(readers[i], nr[i]) || !ck_checked_region(writers[i], nw[i])) return -1;
143 }
144 for (size_t i = 0; i < 4; ++i) {
145 for (size_t j = 0; j < 4; ++j)
146 if (ck_checked_overlap(writers[i], nw[i], readers[j], nr[j])) return -1;
147 for (size_t j = 0; j < i; ++j)
148 if (ck_checked_overlap(writers[i], nw[i], writers[j], nw[j])) return -1;
149 }
150 if (!ck_checked_finite(query, nq) || !ck_checked_finite(key, nm) ||
151 !ck_checked_finite(value, nm) || !ck_checked_finite(d_output, nq)) return -3;
152 float *dq = scratch, *dk = dq + nq, *dv = dk + nm;
153 float *scores = dv + nm, *ds = scores + memory_tokens;
154 memset(scratch, 0, staged * sizeof(float));
155 double scale = 1.0 / sqrt((double)head_dim);
156 for (size_t t = 0; t < query_tokens; ++t) {
157 for (size_t h = 0; h < heads; ++h) {
158 const float *q = query + t * width + h * head_dim;
159 const float *dy = d_output + t * width + h * head_dim;
160 double denominator;
161 if (attention_scores(q, key, memory_tokens, width, h, head_dim, scores, &denominator)) return -4;
162 double weighted = 0.0;
163 for (size_t k = 0; k < memory_tokens; ++k) {
164 double dp = dot(dy, value + k * width + h * head_dim, head_dim);
165 volatile double term = ((double)scores[k] / denominator) * dp;
166 weighted += term;
167 }
168 for (size_t k = 0; k < memory_tokens; ++k) {
169 double dp = dot(dy, value + k * width + h * head_dim, head_dim);
170 double result = ((double)scores[k] / denominator) * (dp - weighted) * scale;
171 if (!isfinite(result) || fabs(result) > FLT_MAX) return -4;
172 ds[k] = (float)result;
173 }
174 for (size_t d = 0; d < head_dim; ++d) {
175 double sum = 0.0;
176 for (size_t k = 0; k < memory_tokens; ++k) {
177 volatile double term = (double)ds[k] * (double)key[k * width + h * head_dim + d];
178 sum += term;
179 }
180 if (!isfinite(sum) || fabs(sum) > FLT_MAX) return -4;
181 dq[t * width + h * head_dim + d] = (float)sum;
182 }
183 for (size_t k = 0; k < memory_tokens; ++k) {
184 for (size_t d = 0; d < head_dim; ++d) {
185 size_t index = k * width + h * head_dim + d;
186 volatile double kp = (double)ds[k] * (double)q[d];
187 volatile double vp = ((double)scores[k] / denominator) * (double)dy[d];
188 double kg = (double)dk[index] + kp, vg = (double)dv[index] + vp;
189 if (!isfinite(kg) || !isfinite(vg) || fabs(kg) > FLT_MAX || fabs(vg) > FLT_MAX) return -4;
190 dk[index] = (float)kg;
191 dv[index] = (float)vg;
192 }
193 }
194 }
195 }
196 memcpy(d_query, dq, nq * sizeof(float));
197 memcpy(d_key, dk, nm * sizeof(float));
198 memcpy(d_value, dv, nm * sizeof(float));
199 return 0;
200}
static int attention_scores(const float *q, const float *key, size_t memory_tokens, size_t width, size_t head, size_t dim, float *scores, double *denominator)
static double dot(const float *a, const float *b, size_t n)
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 attention_scores(), ck_checked_finite(), ck_checked_overlap(), ck_checked_region(), and dot().

◆ ck_gelu_erf_backward_bf16()

int ck_gelu_erf_backward_bf16 ( const uint16_t * input,
size_t input_elements,
size_t input_stride,
const uint16_t * d_output,
size_t d_output_elements,
size_t d_output_stride,
uint16_t * d_input,
size_t d_input_elements,
size_t d_input_stride,
float * scratch,
size_t scratch_elements,
size_t rows,
size_t channels )

Definition at line 72 of file head_backward_bf16.c.

76 {
77 size_t n,needed;
78 if (!product(rows,channels,&n) || !product(n,4,&needed) || scratch_elements<needed ||
79 !extent(rows,channels,input_stride,input_elements) ||
80 !extent(rows,channels,d_output_stride,d_output_elements) ||
81 !extent(rows,channels,d_input_stride,d_input_elements)) return -2;
82 Region r[]={{input,input_elements,2},{d_output,d_output_elements,2},
83 {d_input,d_input_elements,2},{scratch,scratch_elements,4}};
84 if (!regions(r,4,2)) return -1;
85 if (!expand(input,scratch,rows,channels,input_stride) ||
86 !expand(d_output,scratch+n,rows,channels,d_output_stride)) return -1;
87 int rc=ck_gelu_erf_backward_f32(scratch,n,channels,scratch+n,n,channels,
88 scratch+2*n,n,channels,scratch+3*n,n,rows,channels);
89 if (rc) return rc;
90 if (!roundable(scratch+2*n,n)) return -1;
91 publish(d_input,scratch+2*n,rows,channels,d_input_stride); return 0;
92}
int ck_gelu_erf_backward_f32(const float *input, size_t input_elements, size_t input_stride, const float *d_output, size_t d_output_elements, size_t d_output_stride, float *d_input, size_t d_input_elements, size_t d_input_stride, float *scratch, size_t scratch_elements, size_t rows, size_t channels)
static int product(size_t a, size_t b, size_t *v)
static int roundable(const float *src, size_t n)
static int regions(const Region *r, size_t count, size_t reads)
static int expand(const uint16_t *src, float *dst, size_t rows, size_t cols, size_t stride)
static void publish(uint16_t *dst, const float *src, size_t rows, size_t cols, size_t stride)
static int extent(size_t rows, size_t cols, size_t stride, size_t size)

References ck_gelu_erf_backward_f32(), expand(), extent(), product(), publish(), regions(), and roundable().

◆ ck_gelu_erf_backward_f32()

int ck_gelu_erf_backward_f32 ( const float * input,
size_t input_elements,
size_t input_stride,
const float * d_output,
size_t d_output_elements,
size_t d_output_stride,
float * d_input,
size_t d_input_elements,
size_t d_input_stride,
float * scratch,
size_t scratch_elements,
size_t rows,
size_t channels )

Definition at line 69 of file head_backward.c.

73 {
74 size_t staged;
75 if (!mul(rows,channels,&staged) || scratch_elements<staged ||
76 !span(rows,channels,input_stride,input_elements) ||
77 !span(rows,channels,d_output_stride,d_output_elements) ||
78 !span(rows,channels,d_input_stride,d_input_elements)) return -2;
79 const void *ptrs[]={input,d_output,d_input,scratch};
80 size_t sizes[]={input_elements,d_output_elements,d_input_elements,scratch_elements};
81 if (!check_regions(ptrs,sizes,4,2) || !finite_rows(input,rows,channels,input_stride) ||
82 !finite_rows(d_output,rows,channels,d_output_stride)) return -1;
83 const double inv_sqrt_two=0.707106781186547524400844362104849;
84 const double inv_sqrt_two_pi=0.398942280401432677939946059934382;
85 for (size_t r=0; r<rows; ++r) for (size_t c=0; c<channels; ++c) {
86 double x=input[r*input_stride+c];
87 double derivative=0.5*(1.0+erf(x*inv_sqrt_two)) + x*exp(-0.5*x*x)*inv_sqrt_two_pi;
88 if (!store(scratch+r*channels+c,(double)d_output[r*d_output_stride+c]*derivative)) return -1;
89 }
90 publish(d_input,d_input_stride,scratch,rows,channels); return 0;
91}
static int finite_rows(const float *p, size_t rows, size_t cols, size_t stride)
static int store(float *p, double x)
static int span(size_t rows, size_t cols, size_t stride, size_t elements)
static void publish(float *dest, size_t stride, const float *src, size_t rows, size_t cols)
static int check_regions(const void **ptrs, const size_t *sizes, size_t count, size_t read_count)
static int mul(size_t a, size_t b, size_t *out)

References check_regions(), finite_rows(), mul(), publish(), span(), and store().

Referenced by ck_gelu_erf_backward_bf16().

◆ ck_grouped_weighted_rows_backward_bf16()

int ck_grouped_weighted_rows_backward_bf16 ( const uint16_t * options,
size_t option_elements,
size_t option_stride,
const uint16_t * fields,
size_t field_elements,
size_t field_stride,
const int32_t * group_counts,
size_t count_elements,
const uint16_t * d_output,
size_t d_output_elements,
size_t d_output_stride,
uint16_t * d_options,
size_t d_options_elements,
size_t d_options_stride,
uint16_t * d_fields,
size_t d_fields_elements,
size_t d_fields_stride,
float * scratch,
size_t scratch_elements,
size_t groups,
size_t option_rows,
size_t channels )

Definition at line 93 of file head_backward_bf16.c.

101 {
102 size_t oc,gc,twice_o,three_gc,inner,needed,three_oc,six_gc;
103 if (!product(option_rows,channels,&oc) || !product(groups,channels,&gc) ||
104 !product(option_rows,2,&twice_o) || !product(gc,3,&three_gc) ||
105 !sum_size(twice_o,three_gc,&inner) || !sum_size(inner,oc,&inner) ||
106 !product(oc,3,&three_oc) || !product(gc,6,&six_gc) ||
107 !sum_size(three_oc,six_gc,&needed) || !sum_size(needed,twice_o,&needed) ||
108 scratch_elements<needed || count_elements<groups ||
109 !extent(option_rows,channels,option_stride,option_elements) ||
110 !extent(groups,channels,field_stride,field_elements) ||
111 !extent(groups,channels,d_output_stride,d_output_elements) ||
112 !extent(option_rows,channels,d_options_stride,d_options_elements) ||
113 !extent(groups,channels,d_fields_stride,d_fields_elements)) return -2;
114 Region r[]={{options,option_elements,2},{fields,field_elements,2},
115 {group_counts,count_elements,4},{d_output,d_output_elements,2},
116 {d_options,d_options_elements,2},{d_fields,d_fields_elements,2},
117 {scratch,scratch_elements,4}};
118 if (!regions(r,7,4)) return -1;
119 float *x=scratch,*f=x+oc,*dy=f+gc,*dx=dy+gc,*df=dx+oc,*work=df+gc;
120 if (!expand(options,x,option_rows,channels,option_stride) ||
121 !expand(fields,f,groups,channels,field_stride) ||
122 !expand(d_output,dy,groups,channels,d_output_stride)) return -1;
123 int rc=ck_grouped_weighted_rows_backward_f32(x,oc,channels,f,gc,channels,
124 group_counts,count_elements,dy,gc,channels,dx,oc,channels,df,gc,channels,
125 work,inner,groups,option_rows,channels);
126 if (rc) return rc;
127 if (!roundable(dx,oc) || !roundable(df,gc)) return -1;
128 publish(d_options,dx,option_rows,channels,d_options_stride);
129 publish(d_fields,df,groups,channels,d_fields_stride); return 0;
130}
int ck_grouped_weighted_rows_backward_f32(const float *options, size_t option_elements, size_t option_stride, const float *fields, size_t field_elements, size_t field_stride, const int32_t *group_counts, size_t count_elements, const float *d_output, size_t d_output_elements, size_t d_output_stride, float *d_options, size_t d_options_elements, size_t d_options_stride, float *d_fields, size_t d_fields_elements, size_t d_fields_stride, float *scratch, size_t scratch_elements, size_t groups, size_t option_rows, size_t channels)
static int sum_size(size_t a, size_t b, size_t *v)

References ck_grouped_weighted_rows_backward_f32(), expand(), extent(), product(), publish(), regions(), roundable(), and sum_size().

◆ ck_grouped_weighted_rows_backward_f32()

int ck_grouped_weighted_rows_backward_f32 ( const float * options,
size_t option_elements,
size_t option_stride,
const float * fields,
size_t field_elements,
size_t field_stride,
const int32_t * group_counts,
size_t count_elements,
const float * d_output,
size_t d_output_elements,
size_t d_output_stride,
float * d_options,
size_t d_options_elements,
size_t d_options_stride,
float * d_fields,
size_t d_fields_elements,
size_t d_fields_stride,
float * scratch,
size_t scratch_elements,
size_t groups,
size_t option_rows,
size_t channels )

Definition at line 92 of file head_backward.c.

100 {
101 size_t gc,oc,required;
102 if (!mul(groups,channels,&gc) || !mul(option_rows,channels,&oc) ||
103 gc>SIZE_MAX/3 || option_rows>SIZE_MAX/2 ||
104 oc>SIZE_MAX-3*gc || 2*option_rows>SIZE_MAX-(oc+3*gc)) return -2;
105 required=2*option_rows+3*gc+oc;
106 if (scratch_elements<required || count_elements<groups ||
107 !span(option_rows,channels,option_stride,option_elements) ||
108 !span(groups,channels,field_stride,field_elements) ||
109 !span(groups,channels,d_output_stride,d_output_elements) ||
110 !span(option_rows,channels,d_options_stride,d_options_elements) ||
111 !span(groups,channels,d_fields_stride,d_fields_elements)) return -2;
112 const void *ptrs[]={options,fields,group_counts,d_output,d_options,d_fields,scratch};
113 size_t sizes[]={option_elements,field_elements,count_elements,d_output_elements,
114 d_options_elements,d_fields_elements,scratch_elements};
115 if (!check_regions(ptrs,sizes,7,4) || !finite_rows(d_output,groups,channels,d_output_stride)) return -1;
116 /* Recompute probabilities through the same forward provider, preserving
117 its FP32 storage stages. Scratch regions do not overlap one another. */
118 float *summary=scratch+option_rows+gc;
119 float *dx=summary+gc, *df=dx+oc, *dz=df+gc;
120 int status=ck_grouped_softmax_weighted_rows_f32(options,option_elements,option_stride,
121 fields,field_elements,field_stride,group_counts,count_elements,summary,gc,channels,
122 scratch,option_rows+gc,groups,option_rows,channels);
123 if (status) return status;
124 size_t first=0;
125 const double scale=1.0/sqrt((double)channels);
126 for (size_t q=0; q<groups; ++q) {
127 size_t count=(size_t)group_counts[q];
128 double weighted=0;
129 for (size_t i=0; i<count; ++i) {
130 size_t r=first+i; double sum=0;
131 for (size_t c=0; c<channels; ++c) {
132 volatile double term=(double)d_output[q*d_output_stride+c]*options[r*option_stride+c];
133 sum+=term;
134 }
135 if (!store(dz+r,sum)) return -1;
136 volatile double term=(double)scratch[r]*dz[r]; weighted+=term;
137 }
138 if (!isfinite(weighted)) return -1;
139 for (size_t i=0; i<count; ++i) {
140 size_t r=first+i;
141 if (!store(dz+r,(double)scratch[r]*((double)dz[r]-weighted)*scale)) return -1;
142 for (size_t c=0; c<channels; ++c) {
143 volatile float direct=scratch[r]*d_output[q*d_output_stride+c];
144 volatile float via_score=dz[r]*fields[q*field_stride+c];
145 if (!store(dx+r*channels+c,(double)direct+(double)via_score)) return -1;
146 }
147 }
148 for (size_t c=0; c<channels; ++c) {
149 double sum=0;
150 for (size_t i=0; i<count; ++i) {
151 volatile double term=(double)dz[first+i]*options[(first+i)*option_stride+c];
152 sum+=term;
153 }
154 if (!store(df+q*channels+c,sum)) return -1;
155 }
156 first+=count;
157 }
158 publish(d_options,d_options_stride,dx,option_rows,channels);
159 publish(d_fields,d_fields_stride,df,groups,channels); return 0;
160}
int ck_grouped_softmax_weighted_rows_f32(const float *options, size_t option_elements, size_t option_stride, const float *fields, size_t field_elements, size_t field_stride, const int32_t *group_counts, size_t count_elements, float *output, size_t output_elements, size_t output_stride, float *scratch, size_t scratch_elements, size_t groups, size_t option_rows, size_t channels)
const uint8_t size_t count
Definition true_bpe.h:211

References check_regions(), ck_grouped_softmax_weighted_rows_f32(), count, finite_rows(), mul(), publish(), span(), and store().

Referenced by ck_grouped_weighted_rows_backward_bf16().

◆ 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)
const char * left
Definition true_bpe.h:140
const char const char * right
Definition true_bpe.h:141

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)

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

◆ ck_token_span_mean_backward_bf16()

int ck_token_span_mean_backward_bf16 ( const uint16_t * d_output,
size_t d_output_elements,
size_t d_output_stride,
const int32_t * spans,
size_t span_elements,
size_t span_count,
uint16_t * d_input,
size_t d_input_elements,
size_t d_input_stride,
float * scratch,
size_t scratch_elements,
size_t token_capacity,
size_t valid_tokens,
size_t channels )

Definition at line 48 of file head_backward_bf16.c.

53 {
54 size_t sc,tc,vc,needed;
55 if (!valid_tokens || valid_tokens>token_capacity || span_count>SIZE_MAX/2 ||
56 !product(span_count,channels,&sc) || !product(token_capacity,channels,&tc) ||
57 !product(valid_tokens,channels,&vc) || !sum_size(sc,tc,&needed) ||
58 !sum_size(needed,vc,&needed) || scratch_elements<needed || span_elements<2*span_count ||
59 !extent(span_count,channels,d_output_stride,d_output_elements) ||
60 !extent(token_capacity,channels,d_input_stride,d_input_elements)) return -2;
61 Region r[]={{d_output,d_output_elements,2},{spans,span_elements,4},
62 {d_input,d_input_elements,2},{scratch,scratch_elements,4}};
63 if (!regions(r,4,2)) return -1;
64 if (!expand(d_output,scratch,span_count,channels,d_output_stride)) return -1;
65 float *result=scratch+sc,*work=result+tc;
66 int rc=ck_token_span_mean_backward_f32(scratch,sc,channels,spans,span_elements,span_count,
67 result,tc,channels,work,vc,token_capacity,valid_tokens,channels);
68 if (rc) return rc;
69 if (!roundable(result,vc)) return -1;
70 publish(d_input,result,valid_tokens,channels,d_input_stride); return 0;
71}
int ck_token_span_mean_backward_f32(const float *d_output, size_t d_output_elements, size_t d_output_stride, const int32_t *spans, size_t span_elements, size_t span_count, float *d_input, size_t d_input_elements, size_t d_input_stride, float *scratch, size_t scratch_elements, size_t token_capacity, size_t valid_tokens, size_t channels)

References ck_token_span_mean_backward_f32(), expand(), extent(), product(), publish(), regions(), roundable(), and sum_size().

◆ ck_token_span_mean_backward_f32()

int ck_token_span_mean_backward_f32 ( const float * d_output,
size_t d_output_elements,
size_t d_output_stride,
const int32_t * spans,
size_t span_elements,
size_t span_count,
float * d_input,
size_t d_input_elements,
size_t d_input_stride,
float * scratch,
size_t scratch_elements,
size_t token_capacity,
size_t valid_tokens,
size_t channels )

Definition at line 42 of file head_backward.c.

47 {
48 size_t staged;
49 if (!valid_tokens || valid_tokens>token_capacity || !span_count || span_count>SIZE_MAX/2 ||
50 !mul(valid_tokens,channels,&staged) || scratch_elements<staged || span_elements<2*span_count ||
51 !span(span_count,channels,d_output_stride,d_output_elements) ||
52 !span(token_capacity,channels,d_input_stride,d_input_elements)) return -2;
53 const void *ptrs[]={d_output,spans,d_input,scratch};
54 size_t sizes[]={d_output_elements,span_elements,d_input_elements,scratch_elements};
55 if (!check_regions(ptrs,sizes,4,2) || !finite_rows(d_output,span_count,channels,d_output_stride)) return -1;
56 for (size_t s=0; s<span_count; ++s)
57 if (spans[2*s]<0 || spans[2*s+1]<=spans[2*s] || (size_t)spans[2*s+1]>valid_tokens) return -2;
58 /* Ascending span order; one rounded sum per token/channel. Unreferenced
59 valid tokens receive zero. Inactive capacity and row padding stay intact. */
60 for (size_t t=0; t<valid_tokens; ++t) for (size_t c=0; c<channels; ++c) {
61 double sum=0;
62 for (size_t s=0; s<span_count; ++s)
63 if ((size_t)spans[2*s]<=t && t<(size_t)spans[2*s+1])
64 sum+=(double)d_output[s*d_output_stride+c]/(double)(spans[2*s+1]-spans[2*s]);
65 if (!store(scratch+t*channels+c,sum)) return -1;
66 }
67 publish(d_input,d_input_stride,scratch,valid_tokens,channels); return 0;
68}

References check_regions(), finite_rows(), mul(), publish(), span(), and store().

Referenced by ck_token_span_mean_backward_bf16().