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

Go to the source code of this file.

Functions

int ck_full_softmax_topk_unrenormalized_f32_checked (const float *logits, int32_t *ids, float *scores, float *work, size_t rows, size_t experts, size_t top_k, size_t logits_elements, size_t route_elements)
int ck_geglu_tanh_bf16_checked (const uint16_t *, const uint16_t *, uint16_t *, float *, size_t)
int ck_linear_rows_bf16_blas_checked (const uint16_t *, size_t, const uint16_t *, size_t, uint16_t *, size_t, uint16_t *, size_t, double *, size_t, size_t, size_t, size_t)
int ck_linear_rows_bf16_cblas_checked (const uint16_t *, size_t, const uint16_t *, size_t, uint16_t *, size_t, float *, size_t, size_t, size_t, size_t)
int ck_linear_rows_bf16_checked (const uint16_t *, size_t, const uint16_t *, size_t, uint16_t *, size_t, uint16_t *, size_t, size_t, size_t, size_t)
int ck_routed_geglu_bf16_cblas_checked (const uint16_t *input, const int32_t *ids, const float *scores, const uint16_t *gate_up, const uint16_t *down, uint16_t *out, uint16_t *routes, void *workspace, size_t workspace_bytes, size_t rows, size_t width, size_t inner, size_t experts, size_t top_k, size_t hidden_elements, size_t selected_elements, size_t route_elements, size_t gate_elements, size_t down_elements)
int ck_routed_geglu_bf16_checked (const uint16_t *input, const int32_t *ids, const float *scores, const uint16_t *gate_up, const uint16_t *down, uint16_t *out, uint16_t *routes, void *workspace, size_t workspace_bytes, size_t rows, size_t width, size_t inner, size_t experts, size_t top_k, int use_blas, size_t hidden_elements, size_t selected_elements, size_t route_elements, size_t gate_elements, size_t down_elements)
int ck_routed_softcapped_experts_checked_bf16 (const uint16_t *input, const int32_t *ids, const float *scores, const uint16_t *gate_up, const uint16_t *down, uint16_t *out, uint16_t *routes, void *workspace, size_t workspace_bytes, size_t rows, size_t width, size_t inner, size_t experts, size_t top_k, float gate_cap, float up_cap, size_t hidden_elements, size_t selected_elements, size_t route_elements, size_t gate_elements, size_t down_elements)
int ck_routed_swiglu_fp32_route_sum_bf16_checked (const uint16_t *input, const int32_t *ids, const float *scores, const uint16_t *gate_up, const uint16_t *down, uint16_t *out, uint16_t *routes, void *workspace, size_t workspace_bytes, size_t rows, size_t width, size_t inner, size_t experts, size_t top_k, size_t hidden_elements, size_t selected_elements, size_t route_elements, size_t gate_elements, size_t down_elements)
int ck_swiglu_bf16_checked (const uint16_t *, const uint16_t *, uint16_t *, float *, size_t)
static int overlaps (const void *a, size_t a_bytes, const void *b, size_t b_bytes)
static int project (const uint16_t *input, const uint16_t *weight, uint16_t *out, size_t rows, size_t in_width, size_t out_width, projection_workspace work)
static int routed_experts_bf16_checked (const uint16_t *input, const int32_t *ids, const float *scores, const uint16_t *gate_up, const uint16_t *down, uint16_t *out, uint16_t *routes, void *workspace, size_t workspace_bytes, size_t rows, size_t width, size_t inner, size_t experts, size_t top_k, expert_arithmetic math, size_t hidden_elements, size_t selected_elements, size_t route_elements, size_t gate_elements, size_t down_elements)
static int softcapped_swiglu (const uint16_t *gate, const uint16_t *up, uint16_t *output, size_t inner, expert_arithmetic math)
static int span (const void *ptr, size_t bytes, size_t alignment)

Function Documentation

◆ ck_full_softmax_topk_unrenormalized_f32_checked()

int ck_full_softmax_topk_unrenormalized_f32_checked ( const float * logits,
int32_t * ids,
float * scores,
float * work,
size_t rows,
size_t experts,
size_t top_k,
size_t logits_elements,
size_t route_elements )

Definition at line 284 of file routed_geglu_bf16_checked.c.

288{
289 if (!rows || !experts || !top_k || top_k > experts || experts > INT32_MAX ||
290 rows > SIZE_MAX / sizeof(float) / experts ||
291 logits_elements != rows * experts || route_elements != rows * top_k)
292 return -2;
293 const void *ptr[] = {logits, ids, scores, work};
294 const size_t bytes[] = {logits_elements * 4, route_elements * 4, route_elements * 4, experts * 4};
295 for (size_t i = 0; i < 4; ++i) {
296 if (!span(ptr[i], bytes[i], 4)) return -2;
297 for (size_t j = 0; j < i; ++j)
298 if (overlaps(ptr[i], bytes[i], ptr[j], bytes[j])) return -2;
299 }
300 for (size_t i = 0; i < logits_elements; ++i)
301 if (!isfinite(logits[i])) return -3;
302 for (size_t row = 0; row < rows; ++row) {
303 const float *x = logits + row * experts;
304 float largest = x[0];
305 for (size_t i = 1; i < experts; ++i)
306 if (x[i] > largest) largest = x[i];
307 double total = 0;
308 for (size_t i = 0; i < experts; ++i) {
309 work[i] = expf(x[i] - largest);
310 total += work[i];
311 }
312 for (size_t i = 0; i < experts; ++i)
313 work[i] = (float)(work[i] / total);
314 for (size_t slot = 0; slot < top_k; ++slot) {
315 size_t best = 0;
316 for (size_t i = 1; i < experts; ++i)
317 if (work[i] > work[best]) best = i;
318 ids[row * top_k + slot] = (int32_t)best;
319 scores[row * top_k + slot] = work[best];
320 work[best] = -1.0f;
321 }
322 }
323 return 0;
324}
static int slot(const ck_diffusion_session *s, span *out, size_t offset, size_t count, size_t width)
static int overlaps(const void *a, size_t a_bytes, const void *b, size_t b_bytes)
static int span(const void *ptr, size_t bytes, size_t alignment)
const int32_t * ids
Definition tokenizer.h:447

References ids, overlaps(), slot(), and span().

◆ ck_geglu_tanh_bf16_checked()

int ck_geglu_tanh_bf16_checked ( const uint16_t * gate,
const uint16_t * up,
uint16_t * out,
float * stage,
size_t elements )

Definition at line 161 of file bf16_checked_tensor_ops.c.

163{
164 return gated_bf16(gate, up, out, stage, elements, gelu_pytorch_tanh_bf16_storage);
165}
static int gated_bf16(const uint16_t *gate, const uint16_t *up, uint16_t *out, float *stage, size_t n, void(*activate)(float *, size_t))
void gelu_pytorch_tanh_bf16_storage(float *data, size_t n)

References gated_bf16(), and gelu_pytorch_tanh_bf16_storage().

Referenced by routed_experts_bf16_checked().

◆ ck_linear_rows_bf16_blas_checked()

int ck_linear_rows_bf16_blas_checked ( const uint16_t * input,
size_t input_elements,
const uint16_t * weight,
size_t weight_elements,
uint16_t * output,
size_t output_elements,
uint16_t * staged,
size_t staged_elements,
double * work,
size_t work_elements,
size_t rows,
size_t input_width,
size_t output_width )

Definition at line 523 of file linear_rows_bf16_checked.c.

527{
528 if (!rows || !input_width || !output_width || rows > INT_MAX ||
529 input_width > INT_MAX || output_width > INT_MAX ||
530 rows > SIZE_MAX / input_width || output_width > SIZE_MAX / input_width ||
531 rows > SIZE_MAX / output_width)
532 return -2;
533 const size_t a = rows * input_width, b = output_width * input_width, c = rows * output_width;
534 if (a > SIZE_MAX - b || a + b > SIZE_MAX - c)
535 return -2;
536 const size_t work_count = a + b + c;
537 if (a > input_elements || b > weight_elements || c > output_elements ||
538 c > staged_elements || work_count > work_elements || !work ||
539 (uintptr_t)work % _Alignof(double) || work_count > SIZE_MAX / sizeof(double))
540 return -2;
541 const size_t work_bits = work_count * (sizeof(double) / sizeof(uint16_t));
542 const uint16_t *work_region = (const uint16_t*)work;
543 if (!region_valid(input, a) || !region_valid(weight, b) ||
544 !region_valid(output, c) || !region_valid(staged, c) ||
545 !region_valid(work_region, work_bits) ||
546 overlap(output, c, input, a) || overlap(output, c, weight, b) ||
547 overlap(staged, c, input, a) || overlap(staged, c, weight, b) ||
548 overlap(output, c, staged, c) ||
549 overlap(work_region, work_bits, input, a) || overlap(work_region, work_bits, weight, b) ||
550 overlap(work_region, work_bits, staged, c) || overlap(work_region, work_bits, output, c))
551 return -1;
552 pthread_once(&dgemm_once, bind_dgemm);
553 if (!dgemm)
554 return -4;
555 double *matrix_a = work, *matrix_b = work + a, *matrix_c = work + a + b;
556 for (size_t i = 0; i < a; ++i) {
557 matrix_a[i] = bf16_to_float(input[i]);
558 if (!isfinite(matrix_a[i]))
559 return -3;
560 }
561 for (size_t i = 0; i < b; ++i) {
562 matrix_b[i] = bf16_to_float(weight[i]);
563 if (!isfinite(matrix_b[i]))
564 return -3;
565 }
566 const char trans = 'T', no_trans = 'N';
567 const int m = (int)rows, n = (int)output_width, k = (int)input_width;
568 const double one = 1.0, zero = 0.0;
569 dgemm(&trans, &no_trans, &n, &m, &k, &one, matrix_b, &k, matrix_a, &k, &zero, matrix_c, &n);
570 for (size_t i = 0; i < c; ++i) {
571 staged[i] = float_to_bf16((float)matrix_c[i]);
572 if (!isfinite(bf16_to_float(staged[i])))
573 return -3;
574 }
575 memcpy(output, staged, c * sizeof(*output));
576 return 0;
577}
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 pthread_once_t dgemm_once
static void bind_dgemm(void)
static int region_valid(const uint16_t *ptr, size_t elements)
static ck_dgemm_fn dgemm
static int overlap(const uint16_t *a, size_t a_count, const uint16_t *b, size_t b_count)

References bf16_to_float(), bind_dgemm(), dgemm, dgemm_once, float_to_bf16(), overlap(), and region_valid().

Referenced by project().

◆ ck_linear_rows_bf16_cblas_checked()

int ck_linear_rows_bf16_cblas_checked ( const uint16_t * input,
size_t input_elements,
const uint16_t * weight,
size_t weight_elements,
uint16_t * output,
size_t output_elements,
float * work,
size_t work_elements,
size_t rows,
size_t input_width,
size_t output_width )

Definition at line 596 of file linear_rows_bf16_checked.c.

600{
601 if (!rows || !input_width || !output_width || rows > INT_MAX ||
602 input_width > INT_MAX || output_width > INT_MAX ||
603 rows > SIZE_MAX / input_width || output_width > SIZE_MAX / input_width ||
604 rows > SIZE_MAX / output_width)
605 return -2;
606 const size_t a = rows * input_width, b = output_width * input_width, c = rows * output_width;
607 if (a > input_elements || b > weight_elements || c > output_elements || c > work_elements ||
608 !work || (uintptr_t)work % _Alignof(float) || c > SIZE_MAX / sizeof(float))
609 return -2;
610 const uint16_t *work_region = (const uint16_t *)work;
611 const size_t work_bits = c * (sizeof(float) / sizeof(uint16_t));
612 if (!region_valid(input, a) || !region_valid(weight, b) || !region_valid(output, c) ||
613 !region_valid(work_region, work_bits) || overlap(output, c, input, a) ||
614 overlap(output, c, weight, b) || overlap(work_region, work_bits, input, a) ||
615 overlap(work_region, work_bits, weight, b) || overlap(work_region, work_bits, output, c))
616 return -1;
617 for (size_t i = 0; i < a; ++i)
618 if (!isfinite(bf16_to_float(input[i]))) return -3;
619 for (size_t i = 0; i < b; ++i)
620 if (!isfinite(bf16_to_float(weight[i]))) return -3;
621 pthread_once(&bf16_gemm_once, bind_bf16_gemm);
622 if (!bf16_gemm)
623 return -4;
624
625 enum { ROW_MAJOR = 101, NO_TRANSPOSE = 111, TRANSPOSE = 112 };
626 const int m = (int)rows, n = (int)output_width, k = (int)input_width;
627 bf16_gemm(ROW_MAJOR, NO_TRANSPOSE, TRANSPOSE, m, n, k,
628 1.0f, input, k, weight, k, 0.0f, work, n);
629 for (size_t i = 0; i < c; ++i)
630 if (!isfinite(bf16_to_float(float_to_bf16(work[i])))) return -3;
631 for (size_t i = 0; i < c; ++i)
632 output[i] = float_to_bf16(work[i]);
633 return 0;
634}
static ck_bf16_gemm_fn bf16_gemm
static pthread_once_t bf16_gemm_once
static void bind_bf16_gemm(void)

References bf16_gemm, bf16_gemm_once, bf16_to_float(), bind_bf16_gemm(), float_to_bf16(), overlap(), and region_valid().

Referenced by project().

◆ ck_linear_rows_bf16_checked()

int ck_linear_rows_bf16_checked ( const uint16_t * input,
size_t input_elements,
const uint16_t * weight,
size_t weight_elements,
uint16_t * output,
size_t output_elements,
uint16_t * staged,
size_t staged_elements,
size_t rows,
size_t input_width,
size_t output_width )

Definition at line 479 of file linear_rows_bf16_checked.c.

485{
486 return linear_rows_bf16_checked(input, input_elements, weight, weight_elements,
487 NULL, 0, output, output_elements, staged, staged_elements,
488 rows, input_width, output_width);
489}
static int linear_rows_bf16_checked(const uint16_t *input, size_t input_elements, const uint16_t *weight, size_t weight_elements, const uint16_t *bias, size_t bias_elements, uint16_t *output, size_t output_elements, uint16_t *staged, size_t staged_elements, size_t rows, size_t input_width, size_t output_width)

References linear_rows_bf16_checked().

Referenced by project().

◆ ck_routed_geglu_bf16_cblas_checked()

int ck_routed_geglu_bf16_cblas_checked ( const uint16_t * input,
const int32_t * ids,
const float * scores,
const uint16_t * gate_up,
const uint16_t * down,
uint16_t * out,
uint16_t * routes,
void * workspace,
size_t workspace_bytes,
size_t rows,
size_t width,
size_t inner,
size_t experts,
size_t top_k,
size_t hidden_elements,
size_t selected_elements,
size_t route_elements,
size_t gate_elements,
size_t down_elements )

Definition at line 243 of file routed_geglu_bf16_checked.c.

250{
251 return routed_experts_bf16_checked(input, ids, scores, gate_up, down, out, routes,
252 workspace, workspace_bytes, rows, width, inner, experts, top_k, (expert_arithmetic){2, 0, 0, 0},
253 hidden_elements, selected_elements, route_elements, gate_elements, down_elements);
254}
static int routed_experts_bf16_checked(const uint16_t *input, const int32_t *ids, const float *scores, const uint16_t *gate_up, const uint16_t *down, uint16_t *out, uint16_t *routes, void *workspace, size_t workspace_bytes, size_t rows, size_t width, size_t inner, size_t experts, size_t top_k, expert_arithmetic math, size_t hidden_elements, size_t selected_elements, size_t route_elements, size_t gate_elements, size_t down_elements)

References ids, and routed_experts_bf16_checked().

◆ ck_routed_geglu_bf16_checked()

int ck_routed_geglu_bf16_checked ( const uint16_t * input,
const int32_t * ids,
const float * scores,
const uint16_t * gate_up,
const uint16_t * down,
uint16_t * out,
uint16_t * routes,
void * workspace,
size_t workspace_bytes,
size_t rows,
size_t width,
size_t inner,
size_t experts,
size_t top_k,
int use_blas,
size_t hidden_elements,
size_t selected_elements,
size_t route_elements,
size_t gate_elements,
size_t down_elements )

Definition at line 228 of file routed_geglu_bf16_checked.c.

235{
236 if (use_blas != 0 && use_blas != 1)
237 return -2;
238 return routed_experts_bf16_checked(input, ids, scores, gate_up, down, out, routes,
239 workspace, workspace_bytes, rows, width, inner, experts, top_k, (expert_arithmetic){use_blas, 0, 0, 0},
240 hidden_elements, selected_elements, route_elements, gate_elements, down_elements);
241}

References ids, and routed_experts_bf16_checked().

◆ ck_routed_softcapped_experts_checked_bf16()

int ck_routed_softcapped_experts_checked_bf16 ( const uint16_t * input,
const int32_t * ids,
const float * scores,
const uint16_t * gate_up,
const uint16_t * down,
uint16_t * out,
uint16_t * routes,
void * workspace,
size_t workspace_bytes,
size_t rows,
size_t width,
size_t inner,
size_t experts,
size_t top_k,
float gate_cap,
float up_cap,
size_t hidden_elements,
size_t selected_elements,
size_t route_elements,
size_t gate_elements,
size_t down_elements )

Definition at line 256 of file routed_geglu_bf16_checked.c.

263{
264 if (!isfinite(gate_cap) || gate_cap <= 0 || !isfinite(1.0f / gate_cap) ||
265 !isfinite(up_cap) || up_cap <= 0 || !isfinite(1.0f / up_cap)) return -2;
266 return routed_experts_bf16_checked(input, ids, scores, gate_up, down, out, routes,
267 workspace, workspace_bytes, rows, width, inner, experts, top_k, (expert_arithmetic){0, gate_cap, up_cap, 0},
268 hidden_elements, selected_elements, route_elements, gate_elements, down_elements);
269}

References ids, and routed_experts_bf16_checked().

◆ ck_routed_swiglu_fp32_route_sum_bf16_checked()

int ck_routed_swiglu_fp32_route_sum_bf16_checked ( const uint16_t * input,
const int32_t * ids,
const float * scores,
const uint16_t * gate_up,
const uint16_t * down,
uint16_t * out,
uint16_t * routes,
void * workspace,
size_t workspace_bytes,
size_t rows,
size_t width,
size_t inner,
size_t experts,
size_t top_k,
size_t hidden_elements,
size_t selected_elements,
size_t route_elements,
size_t gate_elements,
size_t down_elements )

Definition at line 271 of file routed_geglu_bf16_checked.c.

278{
279 return routed_experts_bf16_checked(input, ids, scores, gate_up, down, out, routes,
280 workspace, workspace_bytes, rows, width, inner, experts, top_k, (expert_arithmetic){0, 0, 0, 1},
281 hidden_elements, selected_elements, route_elements, gate_elements, down_elements);
282}

References ids, and routed_experts_bf16_checked().

◆ ck_swiglu_bf16_checked()

int ck_swiglu_bf16_checked ( const uint16_t * gate,
const uint16_t * up,
uint16_t * out,
float * stage,
size_t elements )

Definition at line 175 of file bf16_checked_tensor_ops.c.

177{
178 return gated_bf16(gate, up, out, stage, elements, silu_bf16_storage);
179}
static void silu_bf16_storage(float *values, size_t count)

References gated_bf16(), and silu_bf16_storage().

Referenced by routed_experts_bf16_checked().

◆ overlaps()

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

Definition at line 49 of file routed_geglu_bf16_checked.c.

50{
51 return (uintptr_t)a < (uintptr_t)b + b_bytes && (uintptr_t)b < (uintptr_t)a + a_bytes;
52}

Referenced by ck_full_softmax_topk_unrenormalized_f32_checked(), and routed_experts_bf16_checked().

◆ project()

int project ( const uint16_t * input,
const uint16_t * weight,
uint16_t * out,
size_t rows,
size_t in_width,
size_t out_width,
projection_workspace work )
static

Definition at line 27 of file routed_geglu_bf16_checked.c.

29{
30 if (work.projection_mode == 2)
32 input, rows * in_width, weight, in_width * out_width, out,
33 rows * out_width, (float *)work.blas, work.blas_elements, rows, in_width, out_width);
34 if (work.projection_mode == 1)
36 input, rows * in_width, weight, in_width * out_width, out,
37 rows * out_width, work.stage, work.stage_elements, work.blas,
38 work.blas_elements, rows, in_width, out_width);
40 input, rows * in_width, weight, in_width * out_width, out,
41 rows * out_width, work.stage, work.stage_elements, rows, in_width, out_width);
42}
int ck_linear_rows_bf16_cblas_checked(const uint16_t *, size_t, const uint16_t *, size_t, uint16_t *, size_t, float *, size_t, size_t, size_t, size_t)
int ck_linear_rows_bf16_checked(const uint16_t *, size_t, const uint16_t *, size_t, uint16_t *, size_t, uint16_t *, size_t, size_t, size_t, size_t)
int ck_linear_rows_bf16_blas_checked(const uint16_t *, size_t, const uint16_t *, size_t, uint16_t *, size_t, uint16_t *, size_t, double *, size_t, size_t, size_t, size_t)

References ck_linear_rows_bf16_blas_checked(), ck_linear_rows_bf16_cblas_checked(), and ck_linear_rows_bf16_checked().

Referenced by linear_rows_bf16_checked(), and routed_experts_bf16_checked().

◆ routed_experts_bf16_checked()

int routed_experts_bf16_checked ( const uint16_t * input,
const int32_t * ids,
const float * scores,
const uint16_t * gate_up,
const uint16_t * down,
uint16_t * out,
uint16_t * routes,
void * workspace,
size_t workspace_bytes,
size_t rows,
size_t width,
size_t inner,
size_t experts,
size_t top_k,
expert_arithmetic math,
size_t hidden_elements,
size_t selected_elements,
size_t route_elements,
size_t gate_elements,
size_t down_elements )
static

Definition at line 76 of file routed_geglu_bf16_checked.c.

83{
84 // Each sized domain fits a signed 32-bit BLAS extent. Cap products before
85 // multiplying so all subsequent byte calculations also fit size_t.
86 if (!rows || !width || !inner || !experts || !top_k || top_k > experts ||
87 rows > INT_MAX || width > INT_MAX || inner > INT_MAX / 2 || experts > INT32_MAX ||
88 width > SIZE_MAX / 64 || inner > SIZE_MAX / 64 || rows > SIZE_MAX / (64 * (width + inner)) ||
89 experts > SIZE_MAX / (64 * width) / inner || top_k > SIZE_MAX / (64 * rows) / width)
90 return -2;
91 const int softcapped = math.gate_cap > 0.0f;
92 const int staged_swiglu = math.staged_swiglu;
93 const int projection_mode = math.projection_mode;
94 const size_t rh = rows * width, ri = rows * inner;
95 const size_t rk = rows * top_k, routed = rk * width;
96 const size_t hi = width * inner;
97 const size_t blas_elements = softcapped || staged_swiglu ? 0 : rh + 2 * ri + (projection_mode == 2 ? 0 : 2 * hi);
98 const size_t blas_bytes = blas_elements * (projection_mode == 2 ? sizeof(float) : sizeof(double));
99 const size_t float_elements = softcapped ? rh : inner;
100 const size_t work_bytes = blas_bytes + 4 * float_elements + 2 * (4 * rh + 5 * ri + routed);
101 if (workspace_bytes < work_bytes ||
102 hidden_elements != rh || selected_elements != rk || route_elements != routed ||
103 gate_elements != experts * 2 * hi || down_elements != experts * hi)
104 return -2;
105 const void *inputs[] = {input, ids, scores, gate_up, down};
106 const size_t sizes[] = {rh * 2, rk * 4, rk * 4, experts * hi * 4, experts * hi * 2};
107 const size_t aligns[] = {2, 4, 4, 2, 2};
108 if (!span(out, rh * 2, 2) || !span(routes, routed * 2, 2) ||
109 !span(workspace, work_bytes, _Alignof(double)) ||
110 overlaps(out, rh * 2, routes, routed * 2) ||
111 overlaps(out, rh * 2, workspace, work_bytes) ||
112 overlaps(routes, routed * 2, workspace, work_bytes))
113 return -2;
114 for (size_t i = 0; i < sizeof(inputs) / sizeof(inputs[0]); ++i)
115 if (!span(inputs[i], sizes[i], aligns[i]) ||
116 overlaps(inputs[i], sizes[i], out, rh * 2) ||
117 overlaps(inputs[i], sizes[i], routes, routed * 2) ||
118 overlaps(inputs[i], sizes[i], workspace, work_bytes))
119 return -2;
120
121 for (size_t row = 0; row < rows; ++row) {
122 for (size_t k = 0; k < top_k; ++k) {
123 const size_t index = row * top_k + k;
124 if (ids[index] < 0 || (size_t)ids[index] >= experts || !isfinite(scores[index]))
125 return -3;
126 for (size_t previous = 0; previous < k; ++previous)
127 if (ids[index] == ids[row * top_k + previous])
128 return -3;
129 }
130 }
131
132 double *blas = workspace;
133 float *float_work = (float *)((uint8_t *)workspace + blas_bytes);
134 uint16_t *gathered = (uint16_t *)(float_work + float_elements);
135 uint16_t *gate = gathered + rh;
136 uint16_t *active = gate + 2 * ri;
137 uint16_t *projected = active + ri;
138 uint16_t *stage = projected + rh;
139 uint16_t *accum = stage + rh + 2 * ri;
140 uint16_t *route_stage = accum + rh;
141 if (softcapped) memset(float_work, 0, rh * sizeof(*float_work));
142 else if (!staged_swiglu) memset(accum, 0, rh * sizeof(*accum));
143 const projection_workspace projection = {blas, blas_elements, stage, rh + 2 * ri, projection_mode};
144
145 // Match expert-major index_add order. The route slot is used only to
146 // associate the input row and scaled probability with this expert.
147 for (size_t expert = 0; expert < experts; ++expert) {
148 size_t count = 0;
149 for (size_t index = 0; index < rk; ++index)
150 if ((size_t)ids[index] == expert) {
151 memcpy(gathered + count * width, input + (index / top_k) * width, width * 2);
152 ++count;
153 }
154 if (!count)
155 continue;
156 int status = project(gathered, gate_up + expert * 2 * hi, gate, count, width, 2 * inner, projection);
157 if (status)
158 return status;
159 for (size_t row = 0; row < count; ++row) {
160 const uint16_t *gate_row = gate + row * 2 * inner;
161 uint16_t *active_row = active + row * inner;
162 if (softcapped)
163 status = softcapped_swiglu(gate_row, gate_row + inner, active_row, inner, math);
164 else if (staged_swiglu)
165 status = ck_swiglu_bf16_checked(gate_row, gate_row + inner, active_row, float_work, inner);
166 else
167 status = ck_geglu_tanh_bf16_checked(gate_row, gate_row + inner, active_row, float_work, inner);
168 if (status)
169 return status;
170 }
171 status = project(active, down + expert * hi, projected, count, inner, width, projection);
172 if (status)
173 return status;
174 size_t row = 0;
175 for (size_t index = 0; index < rk; ++index) {
176 if ((size_t)ids[index] != expert)
177 continue;
178 uint16_t *destination = accum + (index / top_k) * width;
179 const uint16_t *unweighted = projected + row * width;
180 memcpy(route_stage + index * width, unweighted, width * 2);
181 if (staged_swiglu) {
182 ++row;
183 continue;
184 }
185 if (softcapped) {
186 float *sum = float_work + (index / top_k) * width;
187 for (size_t col = 0; col < width; ++col) {
188 volatile float scaled = bf16_to_float(unweighted[col]) * scores[index];
189 sum[col] += scaled;
190 }
191 ++row;
192 continue;
193 }
194 for (size_t col = 0; col < width; ++col) {
195 const uint16_t scaled = float_to_bf16(bf16_to_float(unweighted[col]) * scores[index]);
196 const uint16_t sum = float_to_bf16(bf16_to_float(destination[col]) + bf16_to_float(scaled));
197 if (!isfinite(bf16_to_float(sum)))
198 return -3;
199 destination[col] = sum;
200 }
201 ++row;
202 }
203 }
204 if (staged_swiglu) {
205 for (size_t row = 0; row < rows; ++row)
206 for (size_t col = 0; col < width; ++col) {
207 float sum = 0.0f;
208 for (size_t slot = 0; slot < top_k; ++slot) {
209 const size_t index = row * top_k + slot;
210 volatile float scaled = bf16_to_float(route_stage[index * width + col]) * scores[index];
211 sum += scaled;
212 }
213 accum[row * width + col] = float_to_bf16(sum);
214 if (!isfinite(bf16_to_float(accum[row * width + col])))
215 return -3;
216 }
217 } else if (softcapped)
218 for (size_t i = 0; i < rh; ++i) {
219 accum[i] = float_to_bf16(float_work[i]);
220 if (!isfinite(bf16_to_float(accum[i]))) return -3;
221 }
222 memcpy(out, accum, rh * 2);
223 memcpy(routes, route_stage, routed * 2);
224 return 0;
225}
static int softcapped_swiglu(const uint16_t *gate, const uint16_t *up, uint16_t *output, size_t inner, expert_arithmetic math)
static int project(const uint16_t *input, const uint16_t *weight, uint16_t *out, size_t rows, size_t in_width, size_t out_width, projection_workspace work)
int ck_swiglu_bf16_checked(const uint16_t *, const uint16_t *, uint16_t *, float *, size_t)
int ck_geglu_tanh_bf16_checked(const uint16_t *, const uint16_t *, uint16_t *, float *, size_t)
const uint8_t size_t count
Definition true_bpe.h:211

References bf16_to_float(), ck_geglu_tanh_bf16_checked(), ck_swiglu_bf16_checked(), count, float_to_bf16(), ids, overlaps(), project(), slot(), softcapped_swiglu(), and span().

Referenced by ck_routed_geglu_bf16_cblas_checked(), ck_routed_geglu_bf16_checked(), ck_routed_softcapped_experts_checked_bf16(), and ck_routed_swiglu_fp32_route_sum_bf16_checked().

◆ softcapped_swiglu()

int softcapped_swiglu ( const uint16_t * gate,
const uint16_t * up,
uint16_t * output,
size_t inner,
expert_arithmetic math )
static

Definition at line 60 of file routed_geglu_bf16_checked.c.

62{
63 const float gate_scale = 1.0f / math.gate_cap, up_scale = 1.0f / math.up_cap;
64 for (size_t i = 0; i < inner; ++i) {
65 const float g = bf16_to_float(gate[i]), u = bf16_to_float(up[i]);
66 const float bounded_gate = tanhf(g * gate_scale) * math.gate_cap;
67 const float bounded_up = tanhf(u * up_scale) * math.up_cap;
68 const float sigmoid = 1.0f / (1.0f + expf(-g));
69 const uint16_t value = float_to_bf16((bounded_gate * sigmoid) * bounded_up);
70 if (!isfinite(bf16_to_float(value))) return -3;
71 output[i] = value;
72 }
73 return 0;
74}

References bf16_to_float(), and float_to_bf16().

Referenced by routed_experts_bf16_checked().

◆ span()

int span ( const void * ptr,
size_t bytes,
size_t alignment )
static

Definition at line 44 of file routed_geglu_bf16_checked.c.

45{
46 return ptr && (uintptr_t)ptr % alignment == 0 && bytes <= UINTPTR_MAX - (uintptr_t)ptr;
47}

Referenced by ck_full_softmax_topk_unrenormalized_f32_checked(), and routed_experts_bf16_checked().