← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
rmsnorm_bf16_checked.c File Reference
#include <limits.h>
#include <math.h>
#include <stddef.h>
#include <stdint.h>
#include "bf16_utils.h"
#include "ckernel_engine.h"

Go to the source code of this file.

Typedefs

typedef void(* rmsnorm_forward_fn) (const float *, const float *, float *, float *, int, int, int, float)

Functions

int ck_adaptive_rmsnorm_rows_bf16_checked (const uint16_t *input, size_t input_elements, const uint16_t *modulation, size_t modulation_elements, const uint16_t *register_weight, size_t register_weight_elements, uint16_t *output, size_t output_elements, uint16_t *gate, size_t gate_elements, float *work, size_t work_elements, size_t rows, size_t width, size_t register_rows, float epsilon)
int ck_rmsnorm_rows_bf16_checked (const uint16_t *input, size_t input_elements, const uint16_t *gamma, size_t gamma_elements, uint16_t *output, size_t output_elements, float *work, size_t work_elements, size_t rows, size_t width, float epsilon)
int ck_rmsnorm_rows_centered_single_round_bf16_checked (const uint16_t *input, size_t input_elements, const uint16_t *gamma, size_t gamma_elements, uint16_t *output, size_t output_elements, float *work, size_t work_elements, size_t rows, size_t width, float epsilon)
int ck_rmsnorm_rows_weighted_single_round_bf16_checked (const uint16_t *input, size_t input_elements, const uint16_t *gamma, size_t gamma_elements, uint16_t *output, size_t output_elements, float *work, size_t work_elements, size_t rows, size_t width, float epsilon)
static int overlap (const void *a, size_t a_bytes, const void *b, size_t b_bytes)
static int rmsnorm_rows_bf16_checked (const uint16_t *input, size_t input_elements, const uint16_t *gamma, size_t gamma_elements, uint16_t *output, size_t output_elements, float *work, size_t work_elements, size_t rows, size_t width, float epsilon, rmsnorm_forward_fn normalize)
static int valid_span (const void *base, size_t length, size_t element_size)

Typedef Documentation

◆ rmsnorm_forward_fn

typedef void(* rmsnorm_forward_fn) (const float *, const float *, float *, float *, int, int, int, float)

Definition at line 22 of file rmsnorm_bf16_checked.c.

Function Documentation

◆ ck_adaptive_rmsnorm_rows_bf16_checked()

int ck_adaptive_rmsnorm_rows_bf16_checked ( const uint16_t * input,
size_t input_elements,
const uint16_t * modulation,
size_t modulation_elements,
const uint16_t * register_weight,
size_t register_weight_elements,
uint16_t * output,
size_t output_elements,
uint16_t * gate,
size_t gate_elements,
float * work,
size_t work_elements,
size_t rows,
size_t width,
size_t register_rows,
float epsilon )

Definition at line 113 of file rmsnorm_bf16_checked.c.

120{
121 if (!rows || !width || width > SIZE_MAX / 3 || rows > SIZE_MAX / width ||
122 register_rows > rows || !isfinite(epsilon) || epsilon <= 0.0f) {
123 return -2;
124 }
125 const size_t count = rows * width;
126 if (input_elements < count || output_elements < count || gate_elements < count ||
127 work_elements < count || modulation_elements < 3 * width ||
128 register_weight_elements < width) {
129 return -2;
130 }
131 const void *buffers[] = {input, modulation, register_weight, output, gate, work};
132 const size_t lengths[] = {count, 3 * width, width, count, count, count};
133 const size_t sizes[] = {2, 2, 2, 2, 2, 4};
134 for (size_t i = 0; i < 6; ++i) {
135 if (!valid_span(buffers[i], lengths[i], sizes[i])) {
136 return -1;
137 }
138 }
139 for (size_t i = 3; i < 6; ++i) {
140 for (size_t j = 0; j < i; ++j) {
141 if (overlap(buffers[i], lengths[i] * sizes[i], buffers[j], lengths[j] * sizes[j])) {
142 return -1;
143 }
144 }
145 }
146 for (size_t c = 0; c < width; ++c) {
147 if (!isfinite(bf16_to_float(register_weight[c])) ||
148 !isfinite(bf16_to_float(modulation[c])) ||
149 !isfinite(bf16_to_float(modulation[width + c])) ||
150 !isfinite(bf16_to_float(modulation[2 * width + c]))) {
151 return -3;
152 }
153 }
154 for (size_t r = 0; r < rows; ++r) {
155 const size_t start = r * width;
156 const int is_register = r < register_rows;
157 double sum = 0.0;
158 for (size_t c = 0; c < width; ++c) {
159 const float value = bf16_to_float(input[start + c]);
160 const float square = value * value;
161 sum += square;
162 }
163 const float mean = (float)(sum / (double)width);
164 if (!isfinite(mean + epsilon)) {
165 return -3;
166 }
167 const float reciprocal = 1.0f / sqrtf(mean + epsilon);
168 for (size_t c = 0; c < width; ++c) {
169 const float normed = bf16_to_float(input[start + c]) * reciprocal;
170 const float scale = bf16_to_float(is_register ? register_weight[c] : modulation[c]);
171 const float shift = is_register ? 0.0f : bf16_to_float(modulation[width + c]);
172 const float scaled = normed * (1.0f + scale);
173 const float result = is_register ? scaled : scaled + shift;
174 if (!isfinite(result) || !isfinite(bf16_to_float(float_to_bf16(result)))) {
175 return -3;
176 }
177 work[start + c] = result;
178 }
179 }
180 for (size_t r = 0; r < rows; ++r) {
181 for (size_t c = 0; c < width; ++c) {
182 const size_t index = r * width + c;
183 output[index] = float_to_bf16(work[index]);
184 gate[index] = r < register_rows ? float_to_bf16(1.0f) : modulation[2 * width + c];
185 }
186 }
187 return 0;
188}
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 int buffers(const void **inputs, const size_t *sizes, size_t count, float *out, size_t on, float *scratch, size_t sn, size_t needed)
static int overlap(const void *a, size_t a_bytes, const void *b, size_t b_bytes)
static int valid_span(const void *base, size_t length, size_t element_size)
const uint8_t size_t count
Definition true_bpe.h:211
uint32_t start
Definition utf8.c:214

References bf16_to_float(), buffers(), count, float_to_bf16(), overlap(), start, and valid_span().

◆ ck_rmsnorm_rows_bf16_checked()

int ck_rmsnorm_rows_bf16_checked ( const uint16_t * input,
size_t input_elements,
const uint16_t * gamma,
size_t gamma_elements,
uint16_t * output,
size_t output_elements,
float * work,
size_t work_elements,
size_t rows,
size_t width,
float epsilon )

Definition at line 83 of file rmsnorm_bf16_checked.c.

87{
88 return rmsnorm_rows_bf16_checked(input, input_elements, gamma, gamma_elements,
89 output, output_elements, work, work_elements, rows, width, epsilon,
91}
void rmsnorm_forward_pytorch_bf16_storage(const float *input, const float *gamma, float *output, float *rstd_cache, int tokens, int d_model, int aligned_embed_dim, float eps)
static int rmsnorm_rows_bf16_checked(const uint16_t *input, size_t input_elements, const uint16_t *gamma, size_t gamma_elements, uint16_t *output, size_t output_elements, float *work, size_t work_elements, size_t rows, size_t width, float epsilon, rmsnorm_forward_fn normalize)

References rmsnorm_forward_pytorch_bf16_storage(), and rmsnorm_rows_bf16_checked().

◆ ck_rmsnorm_rows_centered_single_round_bf16_checked()

int ck_rmsnorm_rows_centered_single_round_bf16_checked ( const uint16_t * input,
size_t input_elements,
const uint16_t * gamma,
size_t gamma_elements,
uint16_t * output,
size_t output_elements,
float * work,
size_t work_elements,
size_t rows,
size_t width,
float epsilon )

Definition at line 103 of file rmsnorm_bf16_checked.c.

107{
108 return rmsnorm_rows_bf16_checked(input, input_elements, gamma, gamma_elements,
109 output, output_elements, work, work_elements, rows, width, epsilon,
111}
void rmsnorm_forward_muse_centered_pytorch_bf16_storage(const float *input, const float *zero_centered_weight, float *output, float *rstd_cache, int tokens, int d_model, int aligned_embed_dim, float eps)

References rmsnorm_forward_muse_centered_pytorch_bf16_storage(), and rmsnorm_rows_bf16_checked().

◆ ck_rmsnorm_rows_weighted_single_round_bf16_checked()

int ck_rmsnorm_rows_weighted_single_round_bf16_checked ( const uint16_t * input,
size_t input_elements,
const uint16_t * gamma,
size_t gamma_elements,
uint16_t * output,
size_t output_elements,
float * work,
size_t work_elements,
size_t rows,
size_t width,
float epsilon )

Definition at line 93 of file rmsnorm_bf16_checked.c.

97{
98 return rmsnorm_rows_bf16_checked(input, input_elements, gamma, gamma_elements,
99 output, output_elements, work, work_elements, rows, width, epsilon,
101}
void rmsnorm_forward_muse_weighted_pytorch_bf16_storage(const float *input, const float *weight, float *output, float *rstd_cache, int tokens, int d_model, int aligned_embed_dim, float eps)

References rmsnorm_forward_muse_weighted_pytorch_bf16_storage(), and rmsnorm_rows_bf16_checked().

◆ overlap()

int overlap ( const void * a,
size_t a_bytes,
const void * b,
size_t b_bytes )
static

Definition at line 16 of file rmsnorm_bf16_checked.c.

17{
18 return (uintptr_t)a < (uintptr_t)b + b_bytes &&
19 (uintptr_t)b < (uintptr_t)a + a_bytes;
20}

Referenced by ck_adaptive_rmsnorm_rows_bf16_checked(), and rmsnorm_rows_bf16_checked().

◆ rmsnorm_rows_bf16_checked()

int rmsnorm_rows_bf16_checked ( const uint16_t * input,
size_t input_elements,
const uint16_t * gamma,
size_t gamma_elements,
uint16_t * output,
size_t output_elements,
float * work,
size_t work_elements,
size_t rows,
size_t width,
float epsilon,
rmsnorm_forward_fn normalize )
static

Definition at line 25 of file rmsnorm_bf16_checked.c.

30{
31 if (!rows || !width || rows > INT_MAX || width > INT_MAX ||
32 rows > (SIZE_MAX - width) / width || !isfinite(epsilon) || epsilon <= 0.0f) {
33 return -2;
34 }
35 const size_t count = rows * width;
36 const size_t required_work = count + width;
37 if (input_elements < count || gamma_elements < width ||
38 output_elements < count || work_elements < required_work) {
39 return -2;
40 }
41 if (!valid_span(input, count, sizeof(*input)) ||
42 !valid_span(gamma, width, sizeof(*gamma)) ||
43 !valid_span(output, count, sizeof(*output)) ||
44 !valid_span(work, required_work, sizeof(*work))) {
45 return -1;
46 }
47 const size_t work_bytes = required_work * sizeof(*work);
48 const size_t row_bytes = count * sizeof(*output);
49 const size_t gamma_bytes = width * sizeof(*gamma);
50 if (overlap(work, work_bytes, input, row_bytes) ||
51 overlap(work, work_bytes, gamma, gamma_bytes) ||
52 overlap(work, work_bytes, output, row_bytes) ||
53 overlap(output, row_bytes, input, row_bytes) ||
54 overlap(output, row_bytes, gamma, gamma_bytes)) {
55 return -1;
56 }
57 float *expanded_gamma = work + count;
58 for (size_t i = 0; i < width; ++i) {
59 expanded_gamma[i] = bf16_to_float(gamma[i]);
60 if (!isfinite(expanded_gamma[i])) {
61 return -3;
62 }
63 }
64 for (size_t i = 0; i < count; ++i) {
65 work[i] = bf16_to_float(input[i]);
66 if (!isfinite(work[i])) {
67 return -3;
68 }
69 }
70 normalize(
71 work, expanded_gamma, work, NULL, (int)rows, (int)width, (int)width, epsilon);
72 for (size_t i = 0; i < count; ++i) {
73 if (!isfinite(work[i])) {
74 return -3;
75 }
76 }
77 for (size_t i = 0; i < count; ++i) {
78 output[i] = float_to_bf16(work[i]);
79 }
80 return 0;
81}

References bf16_to_float(), count, float_to_bf16(), overlap(), and valid_span().

Referenced by ck_rmsnorm_rows_bf16_checked(), ck_rmsnorm_rows_centered_single_round_bf16_checked(), and ck_rmsnorm_rows_weighted_single_round_bf16_checked().

◆ valid_span()

int valid_span ( const void * base,
size_t length,
size_t element_size )
static

Definition at line 10 of file rmsnorm_bf16_checked.c.

11{
12 return base && (uintptr_t)base % element_size == 0 &&
13 length <= (UINTPTR_MAX - (uintptr_t)base) / element_size;
14}

Referenced by ck_adaptive_rmsnorm_rows_bf16_checked(), and rmsnorm_rows_bf16_checked().