10static int valid_span(
const void *base,
size_t length,
size_t element_size)
12 return base && (uintptr_t)base % element_size == 0 &&
13 length <= (UINTPTR_MAX - (uintptr_t)base) / element_size;
16static int overlap(
const void *a,
size_t a_bytes,
const void *b,
size_t b_bytes)
18 return (uintptr_t)a < (uintptr_t)b + b_bytes &&
19 (uintptr_t)b < (uintptr_t)a + a_bytes;
23 int, int, int, float);
26 const uint16_t *input,
size_t input_elements,
const uint16_t *gamma,
27 size_t gamma_elements, uint16_t *output,
size_t output_elements,
28 float *work,
size_t work_elements,
size_t rows,
size_t width,
float epsilon,
31 if (!rows || !width || rows > INT_MAX || width > INT_MAX ||
32 rows > (SIZE_MAX - width) / width || !isfinite(epsilon) || epsilon <= 0.0f) {
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) {
44 !
valid_span(work, required_work,
sizeof(*work))) {
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)) {
57 float *expanded_gamma = work +
count;
58 for (
size_t i = 0; i < width; ++i) {
60 if (!isfinite(expanded_gamma[i])) {
64 for (
size_t i = 0; i <
count; ++i) {
66 if (!isfinite(work[i])) {
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])) {
77 for (
size_t i = 0; i <
count; ++i) {
84 const uint16_t *input,
size_t input_elements,
const uint16_t *gamma,
85 size_t gamma_elements, uint16_t *output,
size_t output_elements,
86 float *work,
size_t work_elements,
size_t rows,
size_t width,
float epsilon)
89 output, output_elements, work, work_elements, rows, width, epsilon,
94 const uint16_t *input,
size_t input_elements,
const uint16_t *gamma,
95 size_t gamma_elements, uint16_t *output,
size_t output_elements,
96 float *work,
size_t work_elements,
size_t rows,
size_t width,
float epsilon)
99 output, output_elements, work, work_elements, rows, width, epsilon,
104 const uint16_t *input,
size_t input_elements,
const uint16_t *gamma,
105 size_t gamma_elements, uint16_t *output,
size_t output_elements,
106 float *work,
size_t work_elements,
size_t rows,
size_t width,
float epsilon)
109 output, output_elements, work, work_elements, rows, width, epsilon,
114 const uint16_t *input,
size_t input_elements,
115 const uint16_t *modulation,
size_t modulation_elements,
116 const uint16_t *register_weight,
size_t register_weight_elements,
117 uint16_t *output,
size_t output_elements, uint16_t *gate,
size_t gate_elements,
118 float *work,
size_t work_elements,
size_t rows,
size_t width,
119 size_t register_rows,
float epsilon)
121 if (!rows || !width || width > SIZE_MAX / 3 || rows > SIZE_MAX / width ||
122 register_rows > rows || !isfinite(epsilon) || epsilon <= 0.0f) {
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) {
131 const void *
buffers[] = {input, modulation, register_weight, output, gate, work};
133 const size_t sizes[] = {2, 2, 2, 2, 2, 4};
134 for (
size_t i = 0; i < 6; ++i) {
139 for (
size_t i = 3; i < 6; ++i) {
140 for (
size_t j = 0; j < i; ++j) {
146 for (
size_t c = 0; c < width; ++c) {
154 for (
size_t r = 0; r < rows; ++r) {
155 const size_t start = r * width;
156 const int is_register = r < register_rows;
158 for (
size_t c = 0; c < width; ++c) {
160 const float square = value * value;
163 const float mean = (float)(sum / (
double)width);
164 if (!isfinite(mean + epsilon)) {
167 const float reciprocal = 1.0f / sqrtf(mean + epsilon);
168 for (
size_t c = 0; c < width; ++c) {
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;
177 work[
start + c] = result;
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;
184 gate[index] = r < register_rows ?
float_to_bf16(1.0f) : modulation[2 * width + c];
static uint16_t float_to_bf16(float f)
static float bf16_to_float(uint16_t v)
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)
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)
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)
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)
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_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)
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)
void(* rmsnorm_forward_fn)(const float *, const float *, float *, float *, int, int, int, float)
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)
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)
const uint8_t size_t count