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

Go to the source code of this file.

Functions

int ck_layernorm_rows_step_bf16_checked (const uint16_t *input, const uint16_t *gamma, const uint16_t *beta, uint16_t *output, uint16_t *stage, size_t rows, size_t channels, size_t elements, float epsilon)
int ck_spatial_im2rows_bf16_checked (const uint16_t *input, uint16_t *rows, size_t input_elements, size_t row_elements, size_t height, size_t width, size_t channels, size_t kernel, size_t stride, size_t pad)
static int overlap (const uint16_t *a, size_t na, const uint16_t *b, size_t nb)
static float round_bf16 (float v)
static int valid (const uint16_t *p, size_t elements)

Function Documentation

◆ ck_layernorm_rows_step_bf16_checked()

int ck_layernorm_rows_step_bf16_checked ( const uint16_t * input,
const uint16_t * gamma,
const uint16_t * beta,
uint16_t * output,
uint16_t * stage,
size_t rows,
size_t channels,
size_t elements,
float epsilon )

Definition at line 54 of file spatial_neck_bf16_checked.c.

57{
58 if (!rows || rows > 4356 || !channels || channels > 1024 ||
59 elements != rows*channels || !isfinite(epsilon) || epsilon <= 0.0f)
60 return -2;
61 const uint16_t *read[] = {input,gamma,beta};
62 const size_t size[] = {elements,channels,channels};
63 if (!valid(output,elements) || !valid(stage,elements) || overlap(output,elements,stage,elements))
64 return -1;
65 for (size_t i = 0; i < 3; ++i)
66 if (!valid(read[i],size[i]) || overlap(read[i],size[i],output,elements) ||
67 overlap(read[i],size[i],stage,elements))
68 return -1;
69
70 // The source uses separate BF16 tensor operations rather than fused LayerNorm.
71 for (size_t r = 0; r < rows; ++r) {
72 const uint16_t *src = input+r*channels;
73 uint16_t *dst = stage+r*channels;
74 double sum = 0.0;
75 for (size_t c = 0; c < channels; ++c)
76 sum += bf16_to_float(src[c]);
77 const float mean = round_bf16((float)(sum / channels));
78 if (!isfinite(mean)) return -3;
79 sum = 0.0;
80 for (size_t c = 0; c < channels; ++c) {
81 dst[c] = float_to_bf16(bf16_to_float(src[c])-mean);
82 const float centered = bf16_to_float(dst[c]);
84 }
85 const float variance = round_bf16((float)(sum / channels));
86 const float denom = round_bf16(sqrtf(round_bf16(variance + epsilon)));
87 if (!isfinite(denom) || denom <= 0.0f) return -3;
88 for (size_t c = 0; c < channels; ++c) {
89 const float normalized = round_bf16(bf16_to_float(dst[c]) / denom);
90 const float scaled = round_bf16(normalized * bf16_to_float(gamma[c]));
91 dst[c] = float_to_bf16(scaled+bf16_to_float(beta[c]));
92 if (!isfinite(bf16_to_float(dst[c]))) return -3;
93 }
94 }
95 memcpy(output,stage,elements*sizeof(*output));
96 return 0;
97}
static float centered(float value, int zero)
static uint16_t float_to_bf16(float f)
Definition bf16_utils.h:90
static float bf16_to_float(uint16_t v)
Definition bf16_utils.h:38
static float round_bf16(float v)
static int valid(const uint16_t *p, size_t elements)
static int overlap(const uint16_t *a, size_t na, const uint16_t *b, size_t nb)

References bf16_to_float(), centered(), float_to_bf16(), overlap(), round_bf16(), and valid().

◆ ck_spatial_im2rows_bf16_checked()

int ck_spatial_im2rows_bf16_checked ( const uint16_t * input,
uint16_t * rows,
size_t input_elements,
size_t row_elements,
size_t height,
size_t width,
size_t channels,
size_t kernel,
size_t stride,
size_t pad )

Definition at line 18 of file spatial_neck_bf16_checked.c.

21{
22 if (!height || height > 64 || !width || width > 64 || !channels || channels > 1024 ||
23 !kernel || kernel > 3 || !stride || stride > 2 || pad > 1 ||
24 height + 2*pad < kernel || width + 2*pad < kernel)
25 return -2;
26 const size_t oh = (height + 2*pad - kernel) / stride + 1;
27 const size_t ow = (width + 2*pad - kernel) / stride + 1;
28 if (input_elements != height * width * channels ||
29 row_elements != oh * ow * channels * kernel * kernel)
30 return -2;
31 if (!valid(input,input_elements) || !valid(rows,row_elements) ||
32 overlap(input,input_elements,rows,row_elements))
33 return -1;
34 if (kernel == 1 && stride == 1 && pad == 0) {
35 memcpy(rows,input,input_elements*sizeof(*input));
36 return 0;
37 }
38 for (size_t y = 0; y < oh; ++y)
39 for (size_t x = 0; x < ow; ++x)
40 for (size_t c = 0; c < channels; ++c)
41 for (size_t ky = 0; ky < kernel; ++ky)
42 for (size_t kx = 0; kx < kernel; ++kx) {
43 const size_t iy = y*stride+ky-pad, ix = x*stride+kx-pad;
44 *rows++ = iy < height && ix < width ? input[(iy*width+ix)*channels+c] : 0;
45 }
46 return 0;
47}
int32_t int32_t int32_t int32_t pad
Definition tokenizer.h:234

References overlap(), pad, and valid().

◆ overlap()

int overlap ( const uint16_t * a,
size_t na,
const uint16_t * b,
size_t nb )
static

Definition at line 12 of file spatial_neck_bf16_checked.c.

13{
14 return (uintptr_t)a < (uintptr_t)b + nb * sizeof(*b) &&
15 (uintptr_t)b < (uintptr_t)a + na * sizeof(*a);
16}

Referenced by ck_layernorm_rows_step_bf16_checked(), and ck_spatial_im2rows_bf16_checked().

◆ round_bf16()

float round_bf16 ( float v)
static

Definition at line 49 of file spatial_neck_bf16_checked.c.

50{
52}

References bf16_to_float(), and float_to_bf16().

Referenced by ck_layernorm_rows_step_bf16_checked().

◆ valid()

int valid ( const uint16_t * p,
size_t elements )
static

Definition at line 6 of file spatial_neck_bf16_checked.c.

7{
8 return p && (uintptr_t)p % sizeof(*p) == 0 &&
9 elements <= (UINTPTR_MAX - (uintptr_t)p) / sizeof(*p);
10}

Referenced by ck_layernorm_rows_step_bf16_checked(), and ck_spatial_im2rows_bf16_checked().