← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
routed_geglu_bf16_checked.c
Go to the documentation of this file.
1#include <limits.h>
2#include <math.h>
3#include <stddef.h>
4#include <stdint.h>
5#include <string.h>
6
7#include "bf16_utils.h"
8
9int ck_linear_rows_bf16_checked(const uint16_t *, size_t, const uint16_t *, size_t,
10 uint16_t *, size_t, uint16_t *, size_t, size_t, size_t, size_t);
11int ck_linear_rows_bf16_blas_checked(const uint16_t *, size_t, const uint16_t *, size_t,
12 uint16_t *, size_t, uint16_t *, size_t, double *, size_t,
13 size_t, size_t, size_t);
14int ck_linear_rows_bf16_cblas_checked(const uint16_t *, size_t, const uint16_t *, size_t,
15 uint16_t *, size_t, float *, size_t, size_t, size_t, size_t);
16int ck_geglu_tanh_bf16_checked(const uint16_t *, const uint16_t *, uint16_t *, float *, size_t);
17int ck_swiglu_bf16_checked(const uint16_t *, const uint16_t *, uint16_t *, float *, size_t);
18
19typedef struct {
20 double *blas;
21 size_t blas_elements;
22 uint16_t *stage;
23 size_t stage_elements;
24 int projection_mode;
25} projection_workspace;
26
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)
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}
43
44static int span(const void *ptr, size_t bytes, size_t alignment)
45{
46 return ptr && (uintptr_t)ptr % alignment == 0 && bytes <= UINTPTR_MAX - (uintptr_t)ptr;
47}
48
49static int overlaps(const void *a, size_t a_bytes, const void *b, size_t b_bytes)
50{
51 return (uintptr_t)a < (uintptr_t)b + b_bytes && (uintptr_t)b < (uintptr_t)a + a_bytes;
52}
53
54typedef struct {
55 int projection_mode;
56 float gate_cap, up_cap;
57 int staged_swiglu;
58} expert_arithmetic;
59
60static int softcapped_swiglu(const uint16_t *gate, const uint16_t *up,
61 uint16_t *output, size_t inner, expert_arithmetic math)
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}
75
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)
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}
226
227
229 const uint16_t *input, const int32_t *ids, const float *scores,
230 const uint16_t *gate_up, const uint16_t *down, uint16_t *out, uint16_t *routes,
231 void *workspace, size_t workspace_bytes, size_t rows, size_t width,
232 size_t inner, size_t experts, size_t top_k, int use_blas,
233 size_t hidden_elements, size_t selected_elements, size_t route_elements,
234 size_t gate_elements, size_t down_elements)
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}
242
244 const uint16_t *input, const int32_t *ids, const float *scores,
245 const uint16_t *gate_up, const uint16_t *down, uint16_t *out, uint16_t *routes,
246 void *workspace, size_t workspace_bytes, size_t rows, size_t width,
247 size_t inner, size_t experts, size_t top_k,
248 size_t hidden_elements, size_t selected_elements, size_t route_elements,
249 size_t gate_elements, size_t down_elements)
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}
255
257 const uint16_t *input, const int32_t *ids, const float *scores,
258 const uint16_t *gate_up, const uint16_t *down, uint16_t *out, uint16_t *routes,
259 void *workspace, size_t workspace_bytes, size_t rows, size_t width,
260 size_t inner, size_t experts, size_t top_k, float gate_cap, float up_cap,
261 size_t hidden_elements, size_t selected_elements, size_t route_elements,
262 size_t gate_elements, size_t down_elements)
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}
270
272 const uint16_t *input, const int32_t *ids, const float *scores,
273 const uint16_t *gate_up, const uint16_t *down, uint16_t *out, uint16_t *routes,
274 void *workspace, size_t workspace_bytes, size_t rows, size_t width,
275 size_t inner, size_t experts, size_t top_k, size_t hidden_elements,
276 size_t selected_elements, size_t route_elements, size_t gate_elements,
277 size_t down_elements)
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}
283
285 const float *logits, int32_t *ids, float *scores, float *work,
286 size_t rows, size_t experts, size_t top_k, size_t logits_elements,
287 size_t route_elements)
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 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 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)
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)
static int softcapped_swiglu(const uint16_t *gate, const uint16_t *up, uint16_t *output, size_t inner, expert_arithmetic math)
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 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)
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)
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_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_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_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_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)
const int32_t * ids
Definition tokenizer.h:447
const uint8_t size_t count
Definition true_bpe.h:211