27static int project(
const uint16_t *input,
const uint16_t *weight, uint16_t *out,
28 size_t rows,
size_t in_width,
size_t out_width, projection_workspace work)
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);
77 const uint16_t *input,
const int32_t *
ids,
const float *scores,
78 const uint16_t *gate_up,
const uint16_t *down, uint16_t *out, uint16_t *routes,
79 void *workspace,
size_t workspace_bytes,
size_t rows,
size_t width,
80 size_t inner,
size_t experts,
size_t top_k, expert_arithmetic math,
81 size_t hidden_elements,
size_t selected_elements,
size_t route_elements,
82 size_t gate_elements,
size_t down_elements)
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)
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)
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))
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))
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]))
126 for (
size_t previous = 0; previous < k; ++previous)
127 if (
ids[index] ==
ids[row * top_k + previous])
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};
147 for (
size_t expert = 0; expert < experts; ++expert) {
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);
156 int status =
project(gathered, gate_up + expert * 2 * hi, gate,
count, width, 2 * inner, projection);
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;
164 else if (staged_swiglu)
171 status =
project(active, down + expert * hi, projected,
count, inner, width, projection);
175 for (
size_t index = 0; index < rk; ++index) {
176 if ((
size_t)
ids[index] != expert)
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);
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];
194 for (
size_t col = 0; col < width; ++col) {
199 destination[col] = sum;
205 for (
size_t row = 0; row < rows; ++row)
206 for (
size_t col = 0; col < width; ++col) {
209 const size_t index = row * top_k +
slot;
210 volatile float scaled =
bf16_to_float(route_stage[index * width + col]) * scores[index];
217 }
else if (softcapped)
218 for (
size_t i = 0; i < rh; ++i) {
222 memcpy(out, accum, rh * 2);
223 memcpy(routes, route_stage, routed * 2);
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_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)
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)