singe-kernel 0.1.0-alpha.4

Reusable CPU and GPU kernels.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
#[cutile::module]
mod kernels {
    use cutile::core::*;

    #[cutile::entry()]
    pub unsafe fn moe_align_block_size_i32(
        topk_ids: *mut i32,
        sorted_token_ids: *mut i32,
        expert_ids: *mut i32,
        num_tokens_post_pad: *mut i32,
        cumsum: *mut i32,
        max_expert_count: *mut i32,
        element_count: i32,
        expert_count: i32,
        block_size: i32,
        sorted_token_ids_len: i32,
        expert_ids_len: i32,
        output_len: i32,
    ) {
        let tile_shape = const_shape![128];
        let pid: (i32, i32, i32) = get_tile_block_id();

        // This combines the TileGym multi-stage align-block flow into one pass over experts.
        // Each lane owns one position in the padded sorted token buffer.
        // It discovers which expert block that position belongs to.
        let offsets: Tile<i32, { [128] }> =
            iota(tile_shape) + broadcast_scalar(pid.0 * 128i32, tile_shape);
        let valid = cmpi(
            offsets,
            broadcast_scalar(output_len, tile_shape),
            predicate::LessThan,
        );
        let one: Tile<i32, { [128] }> = constant(1i32, tile_shape);
        let zero: Tile<i32, { [128] }> = constant(0i32, tile_shape);
        let all_lanes: Tile<bool, { [128] }> = constant(true, tile_shape);
        let sentinel = broadcast_scalar(element_count, tile_shape);

        let mut running = zero;
        let mut max_count = zero;
        let mut selected_expert = constant(-1i32, tile_shape);
        let mut selected_start = zero;
        let mut selected_count = zero;
        for expert in 0i32..expert_count {
            let expert_tile = broadcast_scalar(expert, tile_shape);

            // Count how many flattened top-k token assignments route to this expert.
            // Round the count up to `block_size` for block-M GEMM.
            let mut count = zero;
            for token in 0i32..element_count {
                let token_id = load_i32_vector(
                    topk_ids,
                    broadcast_scalar(token, tile_shape),
                    all_lanes,
                    0i32,
                );
                let matches = cmpi(token_id, expert_tile, predicate::Equal);
                count = count + select(matches, one, zero);
            }

            let padded_count = ((count + broadcast_scalar(block_size - 1, tile_shape))
                / broadcast_scalar(block_size, tile_shape))
                * broadcast_scalar(block_size, tile_shape);

            // `cumsum[expert]` is the starting offset of this expert's padded token block in `sorted_token_ids`.
            let cumsum_mask = valid & cmpi(offsets, expert_tile, predicate::Equal);
            store_i32_vector(cumsum, offsets, running, cumsum_mask);

            max_count = select(
                cmpi(count, max_count, predicate::GreaterThan),
                count,
                max_count,
            );

            let in_sorted_range = valid
                & cmpi(offsets, running, predicate::GreaterThanOrEqual)
                & cmpi(offsets, running + padded_count, predicate::LessThan);
            selected_expert = select(in_sorted_range, expert_tile, selected_expert);
            selected_start = select(in_sorted_range, running, selected_start);
            selected_count = select(in_sorted_range, count, selected_count);

            // `expert_ids` is indexed by padded block, not by token.
            // Each block stores the expert whose weight matrix should be used by fused_moe.
            let block_start = running / broadcast_scalar(block_size, tile_shape);
            let block_count = padded_count / broadcast_scalar(block_size, tile_shape);
            let expert_block_mask = valid
                & cmpi(
                    offsets,
                    broadcast_scalar(expert_ids_len, tile_shape),
                    predicate::LessThan,
                )
                & cmpi(offsets, block_start, predicate::GreaterThanOrEqual)
                & cmpi(offsets, block_start + block_count, predicate::LessThan);
            store_i32_vector(expert_ids, offsets, expert_tile, expert_block_mask);

            running = running + padded_count;
        }

        let final_cumsum_mask = valid
            & cmpi(
                offsets,
                broadcast_scalar(expert_count, tile_shape),
                predicate::Equal,
            );
        store_i32_vector(cumsum, offsets, running, final_cumsum_mask);

        let first_lane = valid & cmpi(offsets, zero, predicate::Equal);
        store_i32_vector(num_tokens_post_pad, zero, running, first_lane);
        store_i32_vector(max_expert_count, zero, max_count, first_lane);

        let sorted_mask = valid
            & cmpi(
                offsets,
                broadcast_scalar(sorted_token_ids_len, tile_shape),
                predicate::LessThan,
            );
        let local_index = offsets - selected_start;
        let mut seen = zero;
        let mut token_value = sentinel;

        // Reconstruct the token id at this expert-local rank by scanning the original top-k assignments.
        // Padded slots receive the sentinel.
        for token in 0i32..element_count {
            let token_id = load_i32_vector(
                topk_ids,
                broadcast_scalar(token, tile_shape),
                all_lanes,
                0i32,
            );
            let matches = cmpi(token_id, selected_expert, predicate::Equal);
            let target = matches & cmpi(seen, local_index, predicate::Equal);
            token_value = select(target, broadcast_scalar(token, tile_shape), token_value);
            seen = seen + select(matches, one, zero);
        }
        let real_token = cmpi(local_index, selected_count, predicate::LessThan);
        let token_value = select(real_token, token_value, sentinel);
        store_i32_vector(sorted_token_ids, offsets, token_value, sorted_mask);
    }

    #[cutile::entry()]
    pub unsafe fn fused_moe_f32(
        out: *mut f32,
        input: *mut f32,
        weight: *mut f32,
        routed_weight: *mut f32,
        sorted_token_ids: *mut i32,
        expert_ids: *mut i32,
        num_tokens_post_pad: *mut i32,
        element_count: i32,
        columns: i32,
        reduction: i32,
        top_k: i32,
        block_size: i32,
        input_row_stride: i32,
        expert_stride: i32,
        weight_row_stride: i32,
        output_row_stride: i32,
        mul_routed_weight: i32,
        output_len: i32,
    ) {
        let tile_shape = const_shape![128];
        let pid: (i32, i32, i32) = get_tile_block_id();

        // Each lane computes one output column for one sorted token/expert assignment.
        // The sorted position determines both the token row and the expert weight matrix.
        let offsets: Tile<i32, { [128] }> =
            iota(tile_shape) + broadcast_scalar(pid.0 * 128i32, tile_shape);
        let valid = cmpi(
            offsets,
            broadcast_scalar(output_len, tile_shape),
            predicate::LessThan,
        );
        let sorted_position = offsets / broadcast_scalar(columns, tile_shape);
        let column = offsets - sorted_position * broadcast_scalar(columns, tile_shape);
        let zero_offsets: Tile<i32, { [128] }> = constant(0i32, tile_shape);
        let padded_total = load_i32_vector(num_tokens_post_pad, zero_offsets, valid, 0i32);
        let sorted_valid = valid & cmpi(sorted_position, padded_total, predicate::LessThan);
        let token_id = load_i32_vector(
            sorted_token_ids,
            sorted_position,
            sorted_valid,
            element_count,
        );
        let token_valid = sorted_valid
            & cmpi(
                token_id,
                broadcast_scalar(element_count, tile_shape),
                predicate::LessThan,
            );
        let expert_offsets = sorted_position / broadcast_scalar(block_size, tile_shape);
        let expert = load_i32_vector(expert_ids, expert_offsets, token_valid, 0i32);
        let token_row = token_id / broadcast_scalar(top_k, tile_shape);

        // Dot the routed token row with the selected expert's output column.
        let mut sum: Tile<f32, { [128] }> = constant(0.0f32, tile_shape);
        for reduction_index in 0i32..reduction {
            let input_offsets = token_row * broadcast_scalar(input_row_stride, tile_shape)
                + broadcast_scalar(reduction_index, tile_shape);
            let weight_offsets = expert * broadcast_scalar(expert_stride, tile_shape)
                + column * broadcast_scalar(weight_row_stride, tile_shape)
                + broadcast_scalar(reduction_index, tile_shape);
            let input_values = load_f32_vector(input, input_offsets, token_valid, 0.0f32);
            let weight_values = load_f32_vector(weight, weight_offsets, token_valid, 0.0f32);
            sum = sum + input_values * weight_values;
        }

        if mul_routed_weight != 0 {
            // Optional top-k routing weight scales the expert contribution.
            // The contribution is then scattered back to the unsorted token slot.
            let route = load_f32_vector(routed_weight, token_id, token_valid, 0.0f32);
            sum = sum * route;
        }
        let output_offsets = token_id * broadcast_scalar(output_row_stride, tile_shape) + column;
        store_f32_vector(out, output_offsets, sum, token_valid);
    }

    #[cutile::entry()]
    pub unsafe fn fused_moe_f16(
        out: *mut f16,
        input: *mut f16,
        weight: *mut f16,
        routed_weight: *mut f32,
        sorted_token_ids: *mut i32,
        expert_ids: *mut i32,
        num_tokens_post_pad: *mut i32,
        element_count: i32,
        columns: i32,
        reduction: i32,
        top_k: i32,
        block_size: i32,
        input_row_stride: i32,
        expert_stride: i32,
        weight_row_stride: i32,
        output_row_stride: i32,
        mul_routed_weight: i32,
        output_len: i32,
    ) {
        let tile_shape = const_shape![128];
        let pid: (i32, i32, i32) = get_tile_block_id();
        let offsets: Tile<i32, { [128] }> =
            iota(tile_shape) + broadcast_scalar(pid.0 * 128i32, tile_shape);
        let valid = cmpi(
            offsets,
            broadcast_scalar(output_len, tile_shape),
            predicate::LessThan,
        );
        let sorted_position = offsets / broadcast_scalar(columns, tile_shape);
        let column = offsets - sorted_position * broadcast_scalar(columns, tile_shape);
        let zero_offsets: Tile<i32, { [128] }> = constant(0i32, tile_shape);
        let padded_total = load_i32_vector(num_tokens_post_pad, zero_offsets, valid, 0i32);
        let sorted_valid = valid & cmpi(sorted_position, padded_total, predicate::LessThan);
        let token_id = load_i32_vector(
            sorted_token_ids,
            sorted_position,
            sorted_valid,
            element_count,
        );
        let token_valid = sorted_valid
            & cmpi(
                token_id,
                broadcast_scalar(element_count, tile_shape),
                predicate::LessThan,
            );
        let expert_offsets = sorted_position / broadcast_scalar(block_size, tile_shape);
        let expert = load_i32_vector(expert_ids, expert_offsets, token_valid, 0i32);
        let token_row = token_id / broadcast_scalar(top_k, tile_shape);

        let mut sum: Tile<f32, { [128] }> = constant(0.0f32, tile_shape);
        for reduction_index in 0i32..reduction {
            let input_offsets = token_row * broadcast_scalar(input_row_stride, tile_shape)
                + broadcast_scalar(reduction_index, tile_shape);
            let weight_offsets = expert * broadcast_scalar(expert_stride, tile_shape)
                + column * broadcast_scalar(weight_row_stride, tile_shape)
                + broadcast_scalar(reduction_index, tile_shape);
            let input_values = load_f16_vector_as_f32(input, input_offsets, token_valid);
            let weight_values = load_f16_vector_as_f32(weight, weight_offsets, token_valid);
            sum = sum + input_values * weight_values;
        }

        if mul_routed_weight != 0 {
            let route = load_f32_vector(routed_weight, token_id, token_valid, 0.0f32);
            sum = sum * route;
        }
        let output_offsets = token_id * broadcast_scalar(output_row_stride, tile_shape) + column;
        store_f16_vector_from_f32(out, output_offsets, sum, token_valid);
    }

    #[cutile::entry()]
    pub unsafe fn fused_moe_bf16(
        out: *mut bf16,
        input: *mut bf16,
        weight: *mut bf16,
        routed_weight: *mut f32,
        sorted_token_ids: *mut i32,
        expert_ids: *mut i32,
        num_tokens_post_pad: *mut i32,
        element_count: i32,
        columns: i32,
        reduction: i32,
        top_k: i32,
        block_size: i32,
        input_row_stride: i32,
        expert_stride: i32,
        weight_row_stride: i32,
        output_row_stride: i32,
        mul_routed_weight: i32,
        output_len: i32,
    ) {
        let tile_shape = const_shape![128];
        let pid: (i32, i32, i32) = get_tile_block_id();
        let offsets: Tile<i32, { [128] }> =
            iota(tile_shape) + broadcast_scalar(pid.0 * 128i32, tile_shape);
        let valid = cmpi(
            offsets,
            broadcast_scalar(output_len, tile_shape),
            predicate::LessThan,
        );
        let sorted_position = offsets / broadcast_scalar(columns, tile_shape);
        let column = offsets - sorted_position * broadcast_scalar(columns, tile_shape);
        let zero_offsets: Tile<i32, { [128] }> = constant(0i32, tile_shape);
        let padded_total = load_i32_vector(num_tokens_post_pad, zero_offsets, valid, 0i32);
        let sorted_valid = valid & cmpi(sorted_position, padded_total, predicate::LessThan);
        let token_id = load_i32_vector(
            sorted_token_ids,
            sorted_position,
            sorted_valid,
            element_count,
        );
        let token_valid = sorted_valid
            & cmpi(
                token_id,
                broadcast_scalar(element_count, tile_shape),
                predicate::LessThan,
            );
        let expert_offsets = sorted_position / broadcast_scalar(block_size, tile_shape);
        let expert = load_i32_vector(expert_ids, expert_offsets, token_valid, 0i32);
        let token_row = token_id / broadcast_scalar(top_k, tile_shape);

        let mut sum: Tile<f32, { [128] }> = constant(0.0f32, tile_shape);
        for reduction_index in 0i32..reduction {
            let input_offsets = token_row * broadcast_scalar(input_row_stride, tile_shape)
                + broadcast_scalar(reduction_index, tile_shape);
            let weight_offsets = expert * broadcast_scalar(expert_stride, tile_shape)
                + column * broadcast_scalar(weight_row_stride, tile_shape)
                + broadcast_scalar(reduction_index, tile_shape);
            let input_values = load_bf16_vector_as_f32(input, input_offsets, token_valid);
            let weight_values = load_bf16_vector_as_f32(weight, weight_offsets, token_valid);
            sum = sum + input_values * weight_values;
        }

        if mul_routed_weight != 0 {
            let route = load_f32_vector(routed_weight, token_id, token_valid, 0.0f32);
            sum = sum * route;
        }
        let output_offsets = token_id * broadcast_scalar(output_row_stride, tile_shape) + column;
        store_bf16_vector_from_f32(out, output_offsets, sum, token_valid);
    }

    #[cutile::entry()]
    pub unsafe fn fused_moe_f8e4m3_block_scaled_f32(
        out: *mut f32,
        input: *mut f8e4m3fn,
        weight: *mut f8e4m3fn,
        input_scales: *mut f32,
        weight_scales: *mut f32,
        routed_weight: *mut f32,
        sorted_token_ids: *mut i32,
        expert_ids: *mut i32,
        num_tokens_post_pad: *mut i32,
        element_count: i32,
        columns: i32,
        reduction: i32,
        top_k: i32,
        block_size: i32,
        group_n: i32,
        group_k: i32,
        k_groups: i32,
        input_row_stride: i32,
        expert_stride: i32,
        weight_row_stride: i32,
        output_row_stride: i32,
        mul_routed_weight: i32,
        output_len: i32,
    ) {
        let tile_shape = const_shape![128];
        let pid: (i32, i32, i32) = get_tile_block_id();
        let offsets: Tile<i32, { [128] }> =
            iota(tile_shape) + broadcast_scalar(pid.0 * 128i32, tile_shape);
        let valid = cmpi(
            offsets,
            broadcast_scalar(output_len, tile_shape),
            predicate::LessThan,
        );
        let sorted_position = offsets / broadcast_scalar(columns, tile_shape);
        let column = offsets - sorted_position * broadcast_scalar(columns, tile_shape);
        let zero_offsets: Tile<i32, { [128] }> = constant(0i32, tile_shape);
        let padded_total = load_i32_vector(num_tokens_post_pad, zero_offsets, valid, 0i32);
        let sorted_valid = valid & cmpi(sorted_position, padded_total, predicate::LessThan);
        let token_id = load_i32_vector(
            sorted_token_ids,
            sorted_position,
            sorted_valid,
            element_count,
        );
        let token_valid = sorted_valid
            & cmpi(
                token_id,
                broadcast_scalar(element_count, tile_shape),
                predicate::LessThan,
            );
        let expert_offsets = sorted_position / broadcast_scalar(block_size, tile_shape);
        let expert = load_i32_vector(expert_ids, expert_offsets, token_valid, 0i32);
        let token_row = token_id / broadcast_scalar(top_k, tile_shape);
        let weight_scale_row = column / broadcast_scalar(group_n, tile_shape);

        let mut sum: Tile<f32, { [128] }> = constant(0.0f32, tile_shape);
        for reduction_index in 0i32..reduction {
            let k_group = broadcast_scalar(reduction_index / group_k, tile_shape);
            let input_offsets = token_row * broadcast_scalar(input_row_stride, tile_shape)
                + broadcast_scalar(reduction_index, tile_shape);
            let weight_offsets = expert * broadcast_scalar(expert_stride, tile_shape)
                + column * broadcast_scalar(weight_row_stride, tile_shape)
                + broadcast_scalar(reduction_index, tile_shape);
            let input_scale_offsets = token_row * broadcast_scalar(k_groups, tile_shape) + k_group;
            let weight_scale_offsets = expert
                * broadcast_scalar((columns + group_n - 1) / group_n * k_groups, tile_shape)
                + weight_scale_row * broadcast_scalar(k_groups, tile_shape)
                + k_group;
            let input_values = load_f8e4m3_vector_as_f32(input, input_offsets, token_valid);
            let weight_values = load_f8e4m3_vector_as_f32(weight, weight_offsets, token_valid);
            let input_scale_values =
                load_f32_vector(input_scales, input_scale_offsets, token_valid, 0.0f32);
            let weight_scale_values =
                load_f32_vector(weight_scales, weight_scale_offsets, token_valid, 0.0f32);
            sum = sum + input_values * weight_values * input_scale_values * weight_scale_values;
        }

        if mul_routed_weight != 0 {
            let route = load_f32_vector(routed_weight, token_id, token_valid, 0.0f32);
            sum = sum * route;
        }
        let output_offsets = token_id * broadcast_scalar(output_row_stride, tile_shape) + column;
        store_f32_vector(out, output_offsets, sum, token_valid);
    }

    fn load_i32_vector(
        input: *mut i32,
        offsets: Tile<i32, { [128] }>,
        mask: Tile<bool, { [128] }>,
        fill: i32,
    ) -> Tile<i32, { [128] }> {
        let input_base: PointerTile<*mut i32, { [] }> = pointer_to_tile(input);
        let input_base: PointerTile<*mut i32, { [1] }> = input_base.reshape(const_shape![1]);
        let input_ptrs: PointerTile<*mut i32, { [128] }> = input_base.broadcast(const_shape![128]);
        let input_ptrs: PointerTile<*mut i32, { [128] }> = input_ptrs.offset_tile(offsets);
        let result: (Tile<i32, { [128] }>, Token) = load_ptr_tko(
            input_ptrs,
            ordering::Weak,
            None::<scope::TileBlock>,
            Some(mask),
            Some(fill),
            None,
            Latency::<0>,
        );
        result.0
    }

    fn store_i32_vector(
        out: *mut i32,
        offsets: Tile<i32, { [128] }>,
        values: Tile<i32, { [128] }>,
        mask: Tile<bool, { [128] }>,
    ) {
        let out_base: PointerTile<*mut i32, { [] }> = pointer_to_tile(out);
        let out_base: PointerTile<*mut i32, { [1] }> = out_base.reshape(const_shape![1]);
        let out_ptrs: PointerTile<*mut i32, { [128] }> = out_base.broadcast(const_shape![128]);
        let out_ptrs: PointerTile<*mut i32, { [128] }> = out_ptrs.offset_tile(offsets);
        store_ptr_tko(
            out_ptrs,
            values,
            ordering::Weak,
            None::<scope::TileBlock>,
            Some(mask),
            None,
            Latency::<0>,
        );
    }

    fn load_f32_vector(
        input: *mut f32,
        offsets: Tile<i32, { [128] }>,
        mask: Tile<bool, { [128] }>,
        fill: f32,
    ) -> Tile<f32, { [128] }> {
        let input_base: PointerTile<*mut f32, { [] }> = pointer_to_tile(input);
        let input_base: PointerTile<*mut f32, { [1] }> = input_base.reshape(const_shape![1]);
        let input_ptrs: PointerTile<*mut f32, { [128] }> = input_base.broadcast(const_shape![128]);
        let input_ptrs: PointerTile<*mut f32, { [128] }> = input_ptrs.offset_tile(offsets);
        let result: (Tile<f32, { [128] }>, Token) = load_ptr_tko(
            input_ptrs,
            ordering::Weak,
            None::<scope::TileBlock>,
            Some(mask),
            Some(fill),
            None,
            Latency::<0>,
        );
        result.0
    }

    fn store_f32_vector(
        out: *mut f32,
        offsets: Tile<i32, { [128] }>,
        values: Tile<f32, { [128] }>,
        mask: Tile<bool, { [128] }>,
    ) {
        let out_base: PointerTile<*mut f32, { [] }> = pointer_to_tile(out);
        let out_base: PointerTile<*mut f32, { [1] }> = out_base.reshape(const_shape![1]);
        let out_ptrs: PointerTile<*mut f32, { [128] }> = out_base.broadcast(const_shape![128]);
        let out_ptrs: PointerTile<*mut f32, { [128] }> = out_ptrs.offset_tile(offsets);
        store_ptr_tko(
            out_ptrs,
            values,
            ordering::Weak,
            None::<scope::TileBlock>,
            Some(mask),
            None,
            Latency::<0>,
        );
    }

    fn load_f16_vector_as_f32(
        input: *mut f16,
        offsets: Tile<i32, { [128] }>,
        mask: Tile<bool, { [128] }>,
    ) -> Tile<f32, { [128] }> {
        let zero_offsets: Tile<i32, { [128] }> = constant(0i32, const_shape![128]);
        let offsets = select(mask, offsets, zero_offsets);
        let input_base: PointerTile<*mut f16, { [] }> = pointer_to_tile(input);
        let input_base: PointerTile<*mut f16, { [1] }> = input_base.reshape(const_shape![1]);
        let input_ptrs: PointerTile<*mut f16, { [128] }> = input_base.broadcast(const_shape![128]);
        let input_ptrs: PointerTile<*mut f16, { [128] }> = input_ptrs.offset_tile(offsets);
        let result: (Tile<f16, { [128] }>, Token) = load_ptr_tko(
            input_ptrs,
            ordering::Weak,
            None::<scope::TileBlock>,
            Some(mask),
            None,
            None,
            Latency::<0>,
        );
        let values: Tile<f32, { [128] }> = convert_tile(result.0);
        let zero: Tile<f32, { [128] }> = constant(0.0f32, const_shape![128]);
        select(mask, values, zero)
    }

    fn store_f16_vector_from_f32(
        out: *mut f16,
        offsets: Tile<i32, { [128] }>,
        values: Tile<f32, { [128] }>,
        mask: Tile<bool, { [128] }>,
    ) {
        let zero_offsets: Tile<i32, { [128] }> = constant(0i32, const_shape![128]);
        let offsets = select(mask, offsets, zero_offsets);
        let out_base: PointerTile<*mut f16, { [] }> = pointer_to_tile(out);
        let out_base: PointerTile<*mut f16, { [1] }> = out_base.reshape(const_shape![1]);
        let out_ptrs: PointerTile<*mut f16, { [128] }> = out_base.broadcast(const_shape![128]);
        let out_ptrs: PointerTile<*mut f16, { [128] }> = out_ptrs.offset_tile(offsets);
        let output: Tile<f16, { [128] }> = convert_tile(values);
        store_ptr_tko(
            out_ptrs,
            output,
            ordering::Weak,
            None::<scope::TileBlock>,
            Some(mask),
            None,
            Latency::<0>,
        );
    }

    fn load_bf16_vector_as_f32(
        input: *mut bf16,
        offsets: Tile<i32, { [128] }>,
        mask: Tile<bool, { [128] }>,
    ) -> Tile<f32, { [128] }> {
        let zero_offsets: Tile<i32, { [128] }> = constant(0i32, const_shape![128]);
        let offsets = select(mask, offsets, zero_offsets);
        let input_base: PointerTile<*mut bf16, { [] }> = pointer_to_tile(input);
        let input_base: PointerTile<*mut bf16, { [1] }> = input_base.reshape(const_shape![1]);
        let input_ptrs: PointerTile<*mut bf16, { [128] }> = input_base.broadcast(const_shape![128]);
        let input_ptrs: PointerTile<*mut bf16, { [128] }> = input_ptrs.offset_tile(offsets);
        let result: (Tile<bf16, { [128] }>, Token) = load_ptr_tko(
            input_ptrs,
            ordering::Weak,
            None::<scope::TileBlock>,
            Some(mask),
            None,
            None,
            Latency::<0>,
        );
        let values: Tile<f32, { [128] }> = convert_tile(result.0);
        let zero: Tile<f32, { [128] }> = constant(0.0f32, const_shape![128]);
        select(mask, values, zero)
    }

    fn store_bf16_vector_from_f32(
        out: *mut bf16,
        offsets: Tile<i32, { [128] }>,
        values: Tile<f32, { [128] }>,
        mask: Tile<bool, { [128] }>,
    ) {
        let zero_offsets: Tile<i32, { [128] }> = constant(0i32, const_shape![128]);
        let offsets = select(mask, offsets, zero_offsets);
        let out_base: PointerTile<*mut bf16, { [] }> = pointer_to_tile(out);
        let out_base: PointerTile<*mut bf16, { [1] }> = out_base.reshape(const_shape![1]);
        let out_ptrs: PointerTile<*mut bf16, { [128] }> = out_base.broadcast(const_shape![128]);
        let out_ptrs: PointerTile<*mut bf16, { [128] }> = out_ptrs.offset_tile(offsets);
        let output: Tile<bf16, { [128] }> = convert_tile(values);
        store_ptr_tko(
            out_ptrs,
            output,
            ordering::Weak,
            None::<scope::TileBlock>,
            Some(mask),
            None,
            Latency::<0>,
        );
    }

    fn load_f8e4m3_vector_as_f32(
        input: *mut f8e4m3fn,
        offsets: Tile<i32, { [128] }>,
        mask: Tile<bool, { [128] }>,
    ) -> Tile<f32, { [128] }> {
        let zero_offsets: Tile<i32, { [128] }> = constant(0i32, const_shape![128]);
        let offsets = select(mask, offsets, zero_offsets);
        let input_base: PointerTile<*mut f8e4m3fn, { [] }> = pointer_to_tile(input);
        let input_base: PointerTile<*mut f8e4m3fn, { [1] }> = input_base.reshape(const_shape![1]);
        let input_ptrs: PointerTile<*mut f8e4m3fn, { [128] }> =
            input_base.broadcast(const_shape![128]);
        let input_ptrs: PointerTile<*mut f8e4m3fn, { [128] }> = input_ptrs.offset_tile(offsets);
        let result: (Tile<f8e4m3fn, { [128] }>, Token) = load_ptr_tko(
            input_ptrs,
            ordering::Weak,
            None::<scope::TileBlock>,
            Some(mask),
            None,
            None,
            Latency::<0>,
        );
        let values: Tile<f32, { [128] }> = convert_tile(result.0);
        let zero: Tile<f32, { [128] }> = constant(0.0f32, const_shape![128]);
        select(mask, values, zero)
    }
}

pub use kernels::*;