← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
transformer_rows_checked.c File Reference
#include "ckernel_transformer_rows_checked.h"
#include "checked_float_buffer.h"
#include "ckernel_engine.h"
#include <float.h>
#include <math.h>
#include <stdint.h>
#include <stdbool.h>
#include <limits.h>
#include <string.h>

Go to the source code of this file.

Functions

int add_rows_checked_f32 (const float *left, size_t left_elements, const float *right, size_t right_elements, float *output, size_t output_elements, size_t elements)
int attention_causal_bounded_checked_f32 (const float *q, size_t q_elements, const float *k, size_t k_elements, const float *v, size_t v_elements, float *work, size_t work_elements, float *output, size_t output_elements, size_t heads, size_t kv_heads, size_t tokens, size_t head_dim)
int attention_sliding_bounded_checked_f32 (const float *q, size_t q_elements, const float *k, size_t k_elements, const float *v, size_t v_elements, float *work, size_t work_elements, float *output, size_t output_elements, size_t heads, size_t kv_heads, size_t tokens, size_t head_dim, size_t sliding_window)
int channel_scale_rows_checked_f32 (const float *input, size_t input_elements, size_t input_stride, const float *scale, size_t scale_elements, float *output, size_t output_elements, size_t output_stride, size_t rows, size_t channels)
int rmsnorm_f32_square_f64_sum_rows_checked (const float *input, size_t input_elements, size_t input_stride, const float *gamma, size_t gamma_elements, float *output, size_t output_elements, size_t output_stride, size_t rows, size_t width, float eps)
static int rmsnorm_rows_checked (const float *input, size_t input_elements, size_t input_stride, const float *gamma, size_t gamma_elements, float *output, size_t output_elements, size_t output_stride, size_t rows, size_t width, float eps, bool f64_sum)
int rmsnorm_rows_checked_f32 (const float *input, size_t input_elements, size_t input_stride, const float *gamma, size_t gamma_elements, float *output, size_t output_elements, size_t output_stride, size_t rows, size_t width, float eps)
static float rmsnorm_scale (const float *x, size_t width, float eps, bool f64_sum)
int rope_qk_bounded_checked_f32 (float *q, size_t q_elements, float *k, size_t k_elements, size_t heads, size_t kv_heads, size_t tokens, size_t head_dim, size_t pos_offset, float freq_base)
int silu_mul_rows_checked_f32 (const float *gate, size_t gate_elements, const float *up, size_t up_elements, float *output, size_t output_elements, size_t elements)
static float softcapped_swiglu (float gate, float up, float gate_cap, float up_cap)
int softcapped_swiglu_rows_checked_f32 (const float *gate, size_t gate_elements, const float *up, size_t up_elements, float *output, size_t output_elements, size_t elements, float gate_cap, float up_cap)
int token_head_layout_checked_f32 (const float *input, size_t input_elements, float *output, size_t output_elements, size_t tokens, size_t heads, size_t head_dim, int head_to_token)

Function Documentation

◆ add_rows_checked_f32()

int add_rows_checked_f32 ( const float * left,
size_t left_elements,
const float * right,
size_t right_elements,
float * output,
size_t output_elements,
size_t elements )

Definition at line 113 of file transformer_rows_checked.c.

117{
118 if (!elements || left_elements < elements || right_elements < elements ||
119 output_elements < elements) return -2;
120 if (!ck_checked_region(left, elements) ||
121 !ck_checked_region(right, elements) ||
122 !ck_checked_region(output, elements) ||
123 ck_checked_overlap(left, elements, output, elements) ||
124 ck_checked_overlap(right, elements, output, elements)) return -1;
125 size_t i = 0;
126#if defined(__AVX512F__)
127 const __m512i exponent = _mm512_set1_epi32(0x7f800000);
128 for (; elements-i >= 16; i += 16) {
129 const __m512 a = _mm512_loadu_ps(left+i), b = _mm512_loadu_ps(right+i);
130 const __m512i ai = _mm512_and_si512(_mm512_castps_si512(a),exponent);
131 const __m512i bi = _mm512_and_si512(_mm512_castps_si512(b),exponent);
132 if (_mm512_cmpeq_epi32_mask(ai,exponent) || _mm512_cmpeq_epi32_mask(bi,exponent))
133 return -3;
134 const __m512 sum = _mm512_add_ps(a,b);
135 if (_mm512_cmpeq_epi32_mask(_mm512_and_si512(_mm512_castps_si512(sum),exponent),exponent))
136 return -3;
137 }
138#elif defined(__AVX2__)
139 const __m256i exponent = _mm256_set1_epi32(0x7f800000);
140 for (; elements-i >= 8; i+=8) {
141 const __m256 a=_mm256_loadu_ps(left+i), b=_mm256_loadu_ps(right+i);
142 const __m256i ai=_mm256_cmpeq_epi32(_mm256_and_si256(_mm256_castps_si256(a),exponent),exponent);
143 const __m256i bi=_mm256_cmpeq_epi32(_mm256_and_si256(_mm256_castps_si256(b),exponent),exponent);
144 if (_mm256_movemask_epi8(_mm256_or_si256(ai,bi))) return -3;
145 const __m256 sum=_mm256_add_ps(a,b);
146 const __m256i si=_mm256_and_si256(_mm256_castps_si256(sum),exponent);
147 if (_mm256_movemask_epi8(_mm256_cmpeq_epi32(si,exponent))) return -3;
148 }
149#endif
150 for (; i < elements; ++i)
151 if (!isfinite(left[i]) || !isfinite(right[i]) ||
152 !isfinite(left[i] + right[i])) return -3;
153 i = 0;
154#if defined(__AVX512F__)
155 for (; elements-i >= 16; i += 16)
156 _mm512_storeu_ps(output+i,_mm512_add_ps(_mm512_loadu_ps(left+i),_mm512_loadu_ps(right+i)));
157#elif defined(__AVX2__)
158 for (; elements-i>=8; i+=8)
159 _mm256_storeu_ps(output+i,_mm256_add_ps(_mm256_loadu_ps(left+i),_mm256_loadu_ps(right+i)));
160#endif
161 for (; i < elements; ++i)
162 output[i] = left[i] + right[i];
163 return 0;
164}
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)
const char * left
Definition true_bpe.h:140
const char const char * right
Definition true_bpe.h:141

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

◆ attention_causal_bounded_checked_f32()

int attention_causal_bounded_checked_f32 ( const float * q,
size_t q_elements,
const float * k,
size_t k_elements,
const float * v,
size_t v_elements,
float * work,
size_t work_elements,
float * output,
size_t output_elements,
size_t heads,
size_t kv_heads,
size_t tokens,
size_t head_dim )

Definition at line 298 of file transformer_rows_checked.c.

305{
306 if (!heads || !kv_heads || heads < kv_heads || heads % kv_heads ||
307 !tokens || !head_dim || heads > INT_MAX || kv_heads > INT_MAX ||
308 tokens > INT_MAX || head_dim > INT_MAX ||
309 heads > SIZE_MAX / tokens || kv_heads > SIZE_MAX / tokens ||
310 heads * tokens > SIZE_MAX / head_dim ||
311 kv_heads * tokens > SIZE_MAX / head_dim)
312 return -2;
313 const size_t nq = heads * tokens * head_dim;
314 const size_t nk = kv_heads * tokens * head_dim;
315 if (q_elements < nq || k_elements < nk || v_elements < nk ||
316 work_elements < nq || output_elements < nq) return -2;
317 if (!ck_checked_region(q, nq) || !ck_checked_region(k, nk) ||
318 !ck_checked_region(v, nk) || !ck_checked_region(work, nq) ||
319 !ck_checked_region(output, nq) ||
320 ck_checked_overlap(q, nq, work, nq) ||
321 ck_checked_overlap(k, nk, work, nq) ||
322 ck_checked_overlap(v, nk, work, nq) ||
323 ck_checked_overlap(work, nq, output, nq) ||
324 ck_checked_overlap(q, nq, output, nq) ||
325 ck_checked_overlap(k, nk, output, nq) ||
326 ck_checked_overlap(v, nk, output, nq)) return -1;
327 if (!ck_checked_finite(q, nq) || !ck_checked_finite(k, nk) ||
328 !ck_checked_finite(v, nk)) return -3;
330 q, k, v, work, (int)heads, (int)kv_heads, (int)tokens,
331 (int)head_dim, (int)head_dim, (int)tokens);
332 if (!ck_checked_finite(work, nq)) return -3;
333 memcpy(output, work, nq * sizeof(float));
334 return 0;
335}
static int ck_checked_finite(const float *ptr, size_t n)
void attention_forward_causal_head_major_gqa_flash_strided(const float *q, const float *k, const float *v, float *output, int num_heads, int num_kv_heads, int num_tokens, int head_dim, int aligned_head_dim, int kv_stride_tokens)

References attention_forward_causal_head_major_gqa_flash_strided(), ck_checked_finite(), ck_checked_overlap(), and ck_checked_region().

◆ attention_sliding_bounded_checked_f32()

int attention_sliding_bounded_checked_f32 ( const float * q,
size_t q_elements,
const float * k,
size_t k_elements,
const float * v,
size_t v_elements,
float * work,
size_t work_elements,
float * output,
size_t output_elements,
size_t heads,
size_t kv_heads,
size_t tokens,
size_t head_dim,
size_t sliding_window )

Definition at line 337 of file transformer_rows_checked.c.

345{
346 if (!heads || !kv_heads || heads < kv_heads || heads % kv_heads ||
347 !tokens || !head_dim || !sliding_window ||
348 heads > INT_MAX || kv_heads > INT_MAX || tokens > INT_MAX ||
349 head_dim > INT_MAX || sliding_window > INT_MAX ||
350 heads > SIZE_MAX / tokens || kv_heads > SIZE_MAX / tokens ||
351 heads * tokens > SIZE_MAX / head_dim ||
352 kv_heads * tokens > SIZE_MAX / head_dim)
353 return -2;
354 const size_t nq = heads * tokens * head_dim;
355 const size_t nk = kv_heads * tokens * head_dim;
356 if (q_elements < nq || k_elements < nk || v_elements < nk ||
357 work_elements < nq || output_elements < nq) return -2;
358 if (!ck_checked_region(q, nq) || !ck_checked_region(k, nk) ||
359 !ck_checked_region(v, nk) || !ck_checked_region(work, nq) ||
360 !ck_checked_region(output, nq) ||
361 ck_checked_overlap(q, nq, work, nq) ||
362 ck_checked_overlap(k, nk, work, nq) ||
363 ck_checked_overlap(v, nk, work, nq) ||
364 ck_checked_overlap(work, nq, output, nq) ||
365 ck_checked_overlap(q, nq, output, nq) ||
366 ck_checked_overlap(k, nk, output, nq) ||
367 ck_checked_overlap(v, nk, output, nq)) return -1;
368 if (!ck_checked_finite(q, nq) || !ck_checked_finite(k, nk) ||
369 !ck_checked_finite(v, nk)) return -3;
370
371 const size_t group = heads / kv_heads;
372 const float scale = 1.0f / sqrtf((float)head_dim);
373 for (size_t head = 0; head < heads; ++head) {
374 const size_t kv_head = head / group;
375 for (size_t row = 0; row < tokens; ++row) {
376 const float *query = q + (head * tokens + row) * head_dim;
377 float *acc = work + (head * tokens + row) * head_dim;
378 memset(acc, 0, head_dim * sizeof(float));
379 float maximum = -INFINITY;
380 float sum = 0.0f;
381 const size_t begin = row + 1 > sliding_window
382 ? row + 1 - sliding_window : 0;
383 for (size_t col = begin; col <= row; ++col) {
384 const float *key = k + (kv_head * tokens + col) * head_dim;
385 const float *value = v + (kv_head * tokens + col) * head_dim;
386 float score = 0.0f;
387 for (size_t dim = 0; dim < head_dim; ++dim)
388 score += query[dim] * key[dim];
389 score *= scale;
390 if (!isfinite(score)) return -3;
391 if (score > maximum) {
392 const float factor = maximum == -INFINITY
393 ? 0.0f : expf(maximum - score);
394 sum = sum * factor + 1.0f;
395 for (size_t dim = 0; dim < head_dim; ++dim)
396 acc[dim] = acc[dim] * factor + value[dim];
397 maximum = score;
398 } else {
399 const float factor = expf(score - maximum);
400 sum += factor;
401 for (size_t dim = 0; dim < head_dim; ++dim)
402 acc[dim] += factor * value[dim];
403 }
404 if (!isfinite(sum) || !ck_checked_finite(acc, head_dim))
405 return -3;
406 }
407 if (sum <= 0.0f || !isfinite(sum)) return -3;
408 const float inv_sum = 1.0f / sum;
409 for (size_t dim = 0; dim < head_dim; ++dim)
410 acc[dim] *= inv_sum;
411 if (!ck_checked_finite(acc, head_dim)) return -3;
412 }
413 }
414 memcpy(output, work, nq * sizeof(float));
415 return 0;
416}
int32_t float * score
Definition tokenizer.h:329

References ck_checked_finite(), ck_checked_overlap(), ck_checked_region(), and score.

◆ channel_scale_rows_checked_f32()

int channel_scale_rows_checked_f32 ( const float * input,
size_t input_elements,
size_t input_stride,
const float * scale,
size_t scale_elements,
float * output,
size_t output_elements,
size_t output_stride,
size_t rows,
size_t channels )

Definition at line 166 of file transformer_rows_checked.c.

171{
172 size_t input_span, output_span;
173 if (!ck_checked_span(rows, channels, input_stride, &input_span) ||
174 !ck_checked_span(rows, channels, output_stride, &output_span) ||
175 input_elements < input_span || output_elements < output_span ||
176 scale_elements < channels) return -2;
177 if (!ck_checked_region(input, input_span) ||
178 !ck_checked_region(scale, channels) ||
179 !ck_checked_region(output, output_span) ||
180 ck_checked_overlap(input, input_span, output, output_span) ||
181 ck_checked_overlap(scale, channels, output, output_span)) return -1;
182 if (!ck_checked_finite(scale, channels)) return -3;
183 for (size_t row = 0; row < rows; ++row)
184 for (size_t channel = 0; channel < channels; ++channel) {
185 const float value = input[row * input_stride + channel];
186 if (!isfinite(value) || !isfinite(value * scale[channel]))
187 return -3;
188 }
189 for (size_t row = 0; row < rows; ++row)
190 for (size_t channel = 0; channel < channels; ++channel)
191 output[row * output_stride + channel] =
192 input[row * input_stride + channel] * scale[channel];
193 return 0;
194}
static int ck_checked_span(size_t rows, size_t width, size_t stride, size_t *span)

References ck_checked_finite(), ck_checked_overlap(), ck_checked_region(), and ck_checked_span().

◆ rmsnorm_f32_square_f64_sum_rows_checked()

int rmsnorm_f32_square_f64_sum_rows_checked ( const float * input,
size_t input_elements,
size_t input_stride,
const float * gamma,
size_t gamma_elements,
float * output,
size_t output_elements,
size_t output_stride,
size_t rows,
size_t width,
float eps )

Definition at line 75 of file transformer_rows_checked.c.

80{
81 return rmsnorm_rows_checked(input, input_elements, input_stride, gamma, gamma_elements,
82 output, output_elements, output_stride, rows, width, eps, true);
83}
static int rmsnorm_rows_checked(const float *input, size_t input_elements, size_t input_stride, const float *gamma, size_t gamma_elements, float *output, size_t output_elements, size_t output_stride, size_t rows, size_t width, float eps, bool f64_sum)

References rmsnorm_rows_checked().

◆ rmsnorm_rows_checked()

int rmsnorm_rows_checked ( const float * input,
size_t input_elements,
size_t input_stride,
const float * gamma,
size_t gamma_elements,
float * output,
size_t output_elements,
size_t output_stride,
size_t rows,
size_t width,
float eps,
bool f64_sum )
static

Definition at line 30 of file transformer_rows_checked.c.

35{
36 size_t ni, no;
37 if (!ck_checked_span(rows, width, input_stride, &ni) ||
38 !ck_checked_span(rows, width, output_stride, &no) ||
39 input_elements < ni || output_elements < no || gamma_elements < width)
40 return -2;
41 if (!isfinite(eps) || eps <= 0 ||
42 !ck_checked_region(input, ni) || !ck_checked_region(gamma, width) ||
43 !ck_checked_region(output, no) ||
44 ck_checked_overlap(input, ni, output, no) ||
45 ck_checked_overlap(gamma, width, output, no)) return -1;
46 if (!ck_checked_finite(gamma, width)) return -3;
47 for (size_t row = 0; row < rows; ++row) {
48 const float *x = input + row * input_stride;
49 if (!ck_checked_finite(x, width)) return -3;
50 const float scale = rmsnorm_scale(x, width, eps, f64_sum);
51 if (!isfinite(scale) || (f64_sum && scale <= 0.0f)) return -3;
52 for (size_t col = 0; col < width; ++col)
53 if (!isfinite(x[col] * scale * gamma[col])) return -3;
54 }
55 for (size_t row = 0; row < rows; ++row) {
56 const float *x = input + row * input_stride;
57 float *y = output + row * output_stride;
58 const float scale = rmsnorm_scale(x, width, eps, f64_sum);
59 for (size_t col = 0; col < width; ++col)
60 y[col] = x[col] * scale * gamma[col];
61 }
62 return 0;
63}
static float rmsnorm_scale(const float *x, size_t width, float eps, bool f64_sum)

References ck_checked_finite(), ck_checked_overlap(), ck_checked_region(), ck_checked_span(), and rmsnorm_scale().

Referenced by rmsnorm_f32_square_f64_sum_rows_checked(), and rmsnorm_rows_checked_f32().

◆ rmsnorm_rows_checked_f32()

int rmsnorm_rows_checked_f32 ( const float * input,
size_t input_elements,
size_t input_stride,
const float * gamma,
size_t gamma_elements,
float * output,
size_t output_elements,
size_t output_stride,
size_t rows,
size_t width,
float eps )

Definition at line 65 of file transformer_rows_checked.c.

70{
71 return rmsnorm_rows_checked(input, input_elements, input_stride, gamma, gamma_elements,
72 output, output_elements, output_stride, rows, width, eps, false);
73}

References rmsnorm_rows_checked().

◆ rmsnorm_scale()

float rmsnorm_scale ( const float * x,
size_t width,
float eps,
bool f64_sum )
static

Definition at line 15 of file transformer_rows_checked.c.

16{
17 if (f64_sum) {
18 double sum = 0;
19 for (size_t col = 0; col < width; ++col) {
20 const float square = x[col] * x[col];
21 sum += (double)square;
22 }
23 return 1.0f / sqrtf((float)(sum / (double)width) + eps);
24 }
25 float sum = 0;
26 for (size_t col = 0; col < width; ++col) sum += x[col] * x[col];
27 return 1.0f / sqrtf(sum / (float)width + eps);
28}

Referenced by rmsnorm_rows_checked().

◆ rope_qk_bounded_checked_f32()

int rope_qk_bounded_checked_f32 ( float * q,
size_t q_elements,
float * k,
size_t k_elements,
size_t heads,
size_t kv_heads,
size_t tokens,
size_t head_dim,
size_t pos_offset,
float freq_base )

Definition at line 256 of file transformer_rows_checked.c.

261{
262 if (!heads || !kv_heads || !tokens || head_dim < 2 || (head_dim & 1) ||
263 heads > INT_MAX || kv_heads > INT_MAX || tokens > INT_MAX ||
264 head_dim > INT_MAX || pos_offset > (size_t)INT_MAX - (tokens - 1) ||
265 heads > SIZE_MAX / tokens || kv_heads > SIZE_MAX / tokens ||
266 heads * tokens > SIZE_MAX / head_dim ||
267 kv_heads * tokens > SIZE_MAX / head_dim)
268 return -2;
269 const size_t nq = heads * tokens * head_dim;
270 const size_t nk = kv_heads * tokens * head_dim;
271 if (q_elements < nq || k_elements < nk) return -2;
272 if (!isfinite(freq_base) || freq_base <= 0 ||
273 !ck_checked_region(q, nq) || !ck_checked_region(k, nk) ||
274 ck_checked_overlap(q, nq, k, nk)) return -1;
275 /* The unchecked provider evaluates pos_offset + token as signed int and
276 * computes theta with powf before writing in place. Prove every angle is
277 * representable first so an invalid frequency cannot partially rotate Q/K.
278 */
279 const float theta_scale = powf(freq_base, -2.0f / (float)head_dim);
280 const float last_position = (float)(pos_offset + tokens - 1);
281 if (!isfinite(theta_scale) || theta_scale <= 0.0f) return -3;
282 for (size_t index = 0; index < head_dim / 2; ++index) {
283 const float factor = powf(theta_scale, (float)index);
284 const float angle = last_position * factor;
285 if (!isfinite(factor) || !isfinite(angle) ||
286 !isfinite(cosf(angle)) || !isfinite(sinf(angle))) return -3;
287 }
288 for (size_t i = 0; i < nq; ++i)
289 if (!isfinite(q[i]) || fabsf(q[i]) > FLT_MAX / 4) return -3;
290 for (size_t i = 0; i < nk; ++i)
291 if (!isfinite(k[i]) || fabsf(k[i]) > FLT_MAX / 4) return -3;
292 rope_forward_qk_split_direct_f32(q, k, NULL, 0, (int)heads,
293 (int)kv_heads, (int)tokens, (int)head_dim, (int)head_dim,
294 (int)pos_offset, (int)head_dim, freq_base);
295 return 0;
296}
void rope_forward_qk_split_direct_f32(float *q, float *k, const float *freq_factors, int use_freq_factors, int num_heads, int num_kv_heads, int num_tokens, int head_dim, int aligned_head_dim, int pos_offset, int rotary_dim, float freq_base)

References ck_checked_overlap(), ck_checked_region(), and rope_forward_qk_split_direct_f32().

◆ silu_mul_rows_checked_f32()

int silu_mul_rows_checked_f32 ( const float * gate,
size_t gate_elements,
const float * up,
size_t up_elements,
float * output,
size_t output_elements,
size_t elements )

Definition at line 196 of file transformer_rows_checked.c.

200{
201 if (!elements || gate_elements < elements || up_elements < elements ||
202 output_elements < elements) return -2;
203 if (!ck_checked_region(gate, elements) ||
204 !ck_checked_region(up, elements) ||
205 !ck_checked_region(output, elements) ||
206 ck_checked_overlap(gate, elements, output, elements) ||
207 ck_checked_overlap(up, elements, output, elements)) return -1;
208 for (size_t i = 0; i < elements; ++i) {
209 if (!isfinite(gate[i]) || !isfinite(up[i])) return -3;
210 const float value = (gate[i] / (1.0f + expf(-gate[i]))) * up[i];
211 if (!isfinite(value)) return -3;
212 }
213 for (size_t i = 0; i < elements; ++i)
214 output[i] = (gate[i] / (1.0f + expf(-gate[i]))) * up[i];
215 return 0;
216}

References ck_checked_overlap(), and ck_checked_region().

Referenced by prepare_expert_activation().

◆ softcapped_swiglu()

float softcapped_swiglu ( float gate,
float up,
float gate_cap,
float up_cap )
static

Definition at line 218 of file transformer_rows_checked.c.

219{
220 const float exp_gate = expf(-fabsf(gate));
221 const float sigmoid = (gate < 0.0f ? exp_gate : 1.0f) / (1.0f + exp_gate);
222 const float capped_gate = gate_cap * tanhf(gate / gate_cap);
223 const float capped_up = up_cap * tanhf(up / up_cap);
224 return (capped_gate * sigmoid) * capped_up;
225}

Referenced by softcapped_swiglu_rows_checked_f32().

◆ softcapped_swiglu_rows_checked_f32()

int softcapped_swiglu_rows_checked_f32 ( const float * gate,
size_t gate_elements,
const float * up,
size_t up_elements,
float * output,
size_t output_elements,
size_t elements,
float gate_cap,
float up_cap )

Definition at line 227 of file transformer_rows_checked.c.

231{
232 if (!elements || gate_elements < elements || up_elements < elements ||
233 output_elements < elements) return -2;
234 if (!isfinite(gate_cap) || gate_cap <= 0.0f || !isfinite(up_cap) || up_cap <= 0.0f ||
235 !ck_checked_region(gate, elements) || !ck_checked_region(up, elements) ||
236 !ck_checked_region(output, elements) ||
237 ck_checked_overlap(gate, elements, output, elements) ||
238 ck_checked_overlap(up, elements, output, elements)) return -1;
239 /* Each tanh has magnitude at most one and the sigmoid stays in
240 * [0, 1]. With headroom below FLT_MAX, finite operands therefore
241 * guarantee a finite result before we publish any output. */
242 if ((double)gate_cap * (double)up_cap <= (double)FLT_MAX / 4.0) {
243 if (!ck_checked_finite(gate, elements) || !ck_checked_finite(up, elements))
244 return -3;
245 } else {
246 for (size_t i = 0; i < elements; ++i) {
247 if (!isfinite(gate[i]) || !isfinite(up[i]) ||
248 !isfinite(softcapped_swiglu(gate[i], up[i], gate_cap, up_cap))) return -3;
249 }
250 }
251 for (size_t i = 0; i < elements; ++i)
252 output[i] = softcapped_swiglu(gate[i], up[i], gate_cap, up_cap);
253 return 0;
254}
static float softcapped_swiglu(float gate, float up, float gate_cap, float up_cap)

References ck_checked_finite(), ck_checked_overlap(), ck_checked_region(), and softcapped_swiglu().

Referenced by prepare_expert_activation().

◆ token_head_layout_checked_f32()

int token_head_layout_checked_f32 ( const float * input,
size_t input_elements,
float * output,
size_t output_elements,
size_t tokens,
size_t heads,
size_t head_dim,
int head_to_token )

Definition at line 85 of file transformer_rows_checked.c.

89{
90 if (!tokens || !heads || !head_dim ||
91 tokens > SIZE_MAX / heads || tokens * heads > SIZE_MAX / head_dim)
92 return -2;
93 const size_t elements = tokens * heads * head_dim;
94 if (input_elements < elements || output_elements < elements) return -2;
95 if ((head_to_token != 0 && head_to_token != 1) ||
96 !ck_checked_region(input, elements) ||
97 !ck_checked_region(output, elements) ||
98 ck_checked_overlap(input, elements, output, elements) ||
99 !ck_checked_finite(input, elements)) return -1;
100 for (size_t token = 0; token < tokens; ++token)
101 for (size_t head = 0; head < heads; ++head)
102 for (size_t channel = 0; channel < head_dim; ++channel) {
103 const size_t token_offset = (token * heads + head) * head_dim + channel;
104 const size_t head_offset = (head * tokens + token) * head_dim + channel;
105 if (head_to_token)
106 output[token_offset] = input[head_offset];
107 else
108 output[head_offset] = input[token_offset];
109 }
110 return 0;
111}
const char * token
Definition tokenizer.h:308

References ck_checked_finite(), ck_checked_overlap(), ck_checked_region(), and token.