← Back to C-Kernel-Engine Docs Doxygen Source Documentation
 
Loading...
Searching...
No Matches
layernorm_rows_checked.c File Reference
#include "ckernel_normalization_checked.h"
#include "ckernel_engine.h"
#include "checked_float_buffer.h"
#include <limits.h>
#include <string.h>

Go to the source code of this file.

Functions

int layernorm_rows_checked_f32 (const float *input, size_t input_elements, size_t input_stride, const float *gamma, size_t gamma_elements, const float *beta, size_t beta_elements, float *output, size_t output_elements, size_t output_stride, float *scratch, size_t scratch_elements, size_t rows, size_t channels, float epsilon)
 
int layernorm_rows_checked_workspace (size_t rows, size_t channels, size_t *elements)
 

Function Documentation

◆ layernorm_rows_checked_f32()

int layernorm_rows_checked_f32 ( const float *  input,
size_t  input_elements,
size_t  input_stride,
const float *  gamma,
size_t  gamma_elements,
const float *  beta,
size_t  beta_elements,
float *  output,
size_t  output_elements,
size_t  output_stride,
float *  scratch,
size_t  scratch_elements,
size_t  rows,
size_t  channels,
float  epsilon 
)

Definition at line 16 of file layernorm_rows_checked.c.

19 {
20 size_t ni, no, ns;
21 if (layernorm_rows_checked_workspace(rows, channels, &ns) ||
22 !ck_checked_span(rows, channels, input_stride, &ni) ||
23 !ck_checked_span(rows, channels, output_stride, &no) ||
24 input_elements < ni || output_elements < no || gamma_elements < channels ||
25 beta_elements < channels || scratch_elements < ns) return -2;
26 if (!isfinite(epsilon) || epsilon <= 0 || !ck_checked_region(input, ni) ||
27 !ck_checked_region(output, no) || !ck_checked_region(gamma, channels) ||
28 !ck_checked_region(beta, channels) || !ck_checked_region(scratch, ns)) return -1;
29 if (ck_checked_overlap(output, no, input, ni) ||
30 ck_checked_overlap(output, no, gamma, channels) ||
31 ck_checked_overlap(output, no, beta, channels) ||
32 ck_checked_overlap(output, no, scratch, ns) ||
33 ck_checked_overlap(scratch, ns, input, ni) ||
34 ck_checked_overlap(scratch, ns, gamma, channels) ||
35 ck_checked_overlap(scratch, ns, beta, channels)) return -1;
36 if (!ck_checked_finite(gamma, channels) || !ck_checked_finite(beta, channels)) return -3;
37 for (size_t r = 0; r < rows; ++r)
38 if (!ck_checked_finite(input + r * input_stride, channels)) return -3;
39 float *means = scratch + rows * channels;
40 float *rstd = means + rows;
41 for (size_t r = 0; r < rows; ++r) {
42 /* One row avoids signed token*channel indexing in the legacy ABI.
43 * Its existing reduction order is preserved; no arithmetic is duplicated. */
44 layernorm_naive_serial_matched_precision(input + r * input_stride, gamma, beta,
45 scratch + r * channels, means + r, rstd + r, 1, (int)channels, epsilon);
46 if (!isfinite(means[r]) || !isfinite(rstd[r]) ||
47 !ck_checked_finite(scratch + r * channels, channels)) return -4;
48 }
49 for (size_t r = 0; r < rows; ++r)
50 memcpy(output + r * output_stride, scratch + r * channels, channels * sizeof(float));
51 return 0;
52}
static int ck_checked_finite(const float *ptr, size_t n)
static int ck_checked_span(size_t rows, size_t width, size_t stride, size_t *span)
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)
void layernorm_naive_serial_matched_precision(const float *input, const float *gamma, const float *beta, float *output, float *mean_cache, float *rstd_cache, int tokens, int d_model, float eps)
int layernorm_rows_checked_workspace(size_t rows, size_t channels, size_t *elements)

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

◆ layernorm_rows_checked_workspace()

int layernorm_rows_checked_workspace ( size_t  rows,
size_t  channels,
size_t *  elements 
)

Definition at line 7 of file layernorm_rows_checked.c.

7 {
8 /* Keep the legacy provider's signed loop indices below addition overflow. */
9 if (!elements || !rows || !channels || channels > (size_t)INT_MAX - 8 ||
10 channels > SIZE_MAX - 2 || rows > SIZE_MAX / (channels + 2) ||
11 rows * (channels + 2) > SIZE_MAX / sizeof(float)) return -2;
12 *elements = rows * (channels + 2);
13 return 0;
14}

Referenced by layernorm_rows_checked_f32().