-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathops.cpp
More file actions
3860 lines (3715 loc) · 232 KB
/
Copy pathops.cpp
File metadata and controls
3860 lines (3715 loc) · 232 KB
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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// vllm.cpp original (vt runtime, inventory deviation §9.1); no upstream mirror.
#include "vt/ops.h"
#include <array>
#include <vector>
// CheckConvCommon asks the BACKEND whether it can address a compressed
// conv_state in place, rather than naming a device type.
#include "vt/backend.h"
namespace vt {
namespace {
// The op table itself moved to src/vt/op_provider.cpp — it is now the
// acceleration-provider registry (include/vt/op_provider.h,
// .agents/specs/metal-mlx-reuse-study.md §6), not a flat one-slot-per-device
// array with silent last-writer-wins. `RegisterOp`, `GetOp` and `OpRegistered`
// keep their exact signatures and semantics, so every one of the ~70 op wrappers
// below is UNCHANGED: the seam is inserted at `GetOp` and picked up by all of
// them at once with zero call-site edits.
bool IsFloat(DType d) { return d == DType::kF32 || d == DType::kF16 || d == DType::kBF16; }
bool IsOutFloat(DType d) { return d == DType::kF32 || d == DType::kBF16; }
} // namespace
ScalarTypeId ToScalarType(DType dtype) {
switch (dtype) {
case DType::kF32: return scalar_type::kF32;
case DType::kF16: return scalar_type::kF16;
case DType::kBF16: return scalar_type::kBF16;
case DType::kI8: return scalar_type::kI8;
case DType::kI32: return scalar_type::kI32;
case DType::kI64: return scalar_type::kI64;
// Block-quantized encodings have no single scalar type: a block mixes
// scales and packed codes. Kernels consume them through the quant traits
// table, never through a KernelTensorDesc scalar type.
case DType::kQ4_0:
case DType::kQ8_0:
case DType::kQ2_K:
case DType::kQ3_K:
case DType::kQ4_K:
case DType::kQ5_K:
case DType::kQ6_K:
case DType::kQ8_K:
case DType::kIQ2_XXS:
case DType::kIQ3_XXS:
case DType::kIQ2_S:
case DType::kMXFP4:
break;
}
VT_CHECK(false, "unsupported storage dtype for scalar-type conversion");
return scalar_type::kF32;
}
KernelTensorDesc Describe(const Tensor& tensor, ScalarTypeId semantic_type,
KernelLayout layout) {
VT_CHECK(tensor.rank >= 1 && tensor.rank <= kMaxRank,
"kernel tensor descriptor rank out of range");
VT_CHECK(tensor.data != nullptr, "kernel tensor descriptor requires non-null data");
for (int d = 0; d < tensor.rank; ++d) {
VT_CHECK(tensor.shape[d] > 0, "kernel tensor descriptor requires positive dimensions");
VT_CHECK(tensor.stride[d] >= 0, "kernel tensor descriptor rejects negative strides");
}
switch (layout) {
case KernelLayout::kStrided:
VT_CHECK(semantic_type == ToScalarType(tensor.dtype),
"strided layout semantic type must match its storage dtype");
break;
case KernelLayout::kPackedTwoFp4PerByte:
VT_CHECK(tensor.dtype == DType::kI8 && semantic_type == scalar_type::kFE2M1f,
"packed-two-fp4 layout requires i8 storage with explicit FE2M1 semantics");
break;
case KernelLayout::kBlockScaleLinear:
case KernelLayout::kBlockScaleSwizzled:
VT_CHECK(tensor.dtype == DType::kI8 &&
(semantic_type == scalar_type::kFE4M3fn ||
semantic_type == scalar_type::kFE8M0fnu),
"block-scale layout requires i8 storage with explicit FP8 scale semantics");
break;
case KernelLayout::kMarlinInterleaved:
VT_CHECK(tensor.dtype == DType::kI8 &&
(semantic_type == scalar_type::kFE2M1f ||
semantic_type == scalar_type::kI4 || semantic_type == scalar_type::kU4),
"Marlin layout requires i8 storage with an explicit 4-bit semantic type");
break;
}
KernelTensorDesc desc;
desc.data = tensor.data;
desc.storage_dtype = tensor.dtype;
desc.scalar_type = semantic_type;
desc.device = tensor.device;
desc.rank = tensor.rank;
desc.layout = layout;
for (int d = 0; d < kMaxRank; ++d) {
desc.shape[d] = tensor.shape[d];
desc.stride[d] = tensor.stride[d];
}
return desc;
}
WorkspaceKey MakeWorkspaceKey(const Queue& q, OpId op, WorkspaceSlot slot) {
VT_CHECK(q.id != 0, "workspace key requires a live queue identity");
return WorkspaceKey{q.device, q.id, reinterpret_cast<uintptr_t>(q.handle), op, slot};
}
void Matmul(Queue& q, Tensor& out, const Tensor& a, const Tensor& b) {
VT_CHECK(a.rank == 2 && b.rank == 2 && out.rank == 2, "matmul: rank-2 tensors required");
VT_CHECK(a.shape[1] == b.shape[0], "matmul: inner dims mismatch");
VT_CHECK(out.shape[0] == a.shape[0] && out.shape[1] == b.shape[1],
"matmul: output shape mismatch");
VT_CHECK(IsFloat(a.dtype) && IsFloat(b.dtype) && IsOutFloat(out.dtype),
"matmul: float inputs and f32/bf16 output required");
VT_CHECK(a.IsContiguous() && b.IsContiguous() && out.IsContiguous(),
"matmul: contiguous tensors required");
VT_CHECK(a.device == b.device && a.device == out.device && a.device == q.device,
"matmul: device mismatch");
reinterpret_cast<MatmulFn>(GetOp(OpId::kMatmul, q.device.type))(q, out, a, b);
}
void DropinProbe(Queue& q, Tensor& out, const Tensor& in,
const DropinProbeArgs& args) {
VT_CHECK(q.id != 0, "dropin_probe: live queue required");
VT_CHECK(in.rank == 2 && out.rank == 2, "dropin_probe: rank-2 tensors required");
VT_CHECK(in.shape[0] == out.shape[0] && in.shape[1] == out.shape[1],
"dropin_probe: input/output shape mismatch");
VT_CHECK(in.device == q.device && out.device == q.device,
"dropin_probe: input/output/queue device mismatch");
VT_CHECK(args.workspace_slot != args.scalar_slot,
"dropin_probe: workspace and scalar slots must not alias");
VT_CHECK(args.workspace_bytes >= sizeof(uint32_t),
"dropin_probe: workspace must hold the raw-launch marker");
(void)Describe(in, args.scalar_type, args.layout);
(void)Describe(out, args.scalar_type, args.layout);
VT_CHECK(args.scalar_type == scalar_type::kF32 && args.layout == KernelLayout::kStrided,
"dropin_probe: W0 raw kernel supports f32 strided tensors only");
GetTypedOp<DropinProbeFn>(OpId::kDropinProbe, q.device.type)(q, out, in, args);
}
void MatmulBT(Queue& q, Tensor& out, const Tensor& a, const Tensor& b) {
// GGUF compute-in-quant (QUANT-GGUF-CIQ-GEMM work row G4). A block-quantized
// weight is NOT an elementwise tensor — it has no per-element stride and
// cannot be read by kMatmulBT — but it IS in exactly the [N, K] orientation
// this entry point defines, which is ggml's src0 layout and GGUF's disk
// order (ggml-cpu.c:1245-1443). Dispatching it to kMatmulBTQuant here is
// what routes the MODEL: every matmul helper already sends an `nk == true`
// weight to MatmulBT (qwen3_5.cpp:1067,1078 device-in/out and :743,760
// host), so the keep-quant loader's block-typed OwnedTensor reaches the
// quantized GEMM with no signature, call-site or forward change. Elementwise
// weights fall through to the unchanged validation + kMatmulBT below, so
// every safetensors path is bit-identical by construction.
if (IsBlockQuant(b.dtype)) {
MatmulBTQuant(q, out, a, b);
return;
}
VT_CHECK(a.rank == 2 && b.rank == 2 && out.rank == 2, "matmul_bt: rank-2 tensors required");
VT_CHECK(a.shape[1] == b.shape[1], "matmul_bt: inner dims mismatch (b is [N,K])");
VT_CHECK(out.shape[0] == a.shape[0] && out.shape[1] == b.shape[0],
"matmul_bt: output shape mismatch");
VT_CHECK(IsFloat(a.dtype) && IsFloat(b.dtype) && IsOutFloat(out.dtype),
"matmul_bt: float inputs and f32/bf16 output required");
// The ACTIVATION may be ROW-STRIDED (relaxed at MLA campaign W6): upstream's
// `kv_b_proj(kv_c)` inside `_compute_prefill_context`
// (mla_attention.py:2141-2160) is applied to a COLUMN SLICE of the 576-wide
// chunked-prefill workspace, i.e. a torch view whose row stride is 576 while
// K is 512 — and `F.linear` accepts exactly that. Only the innermost dim must
// be packed; for a CONTIGUOUS activation the row stride IS K, so every
// existing caller passes byte-identical arguments (same cuBLASLt ld, so the
// same algo, so bit-identical results).
VT_CHECK(a.stride[1] == 1 && a.stride[0] >= a.shape[1],
"matmul_bt: activation rows must be packed (innermost stride 1) and "
"non-overlapping");
VT_CHECK(b.IsContiguous() && out.IsContiguous(),
"matmul_bt: contiguous weight and output required");
VT_CHECK(a.device == b.device && a.device == out.device && a.device == q.device,
"matmul_bt: device mismatch");
reinterpret_cast<MatmulFn>(GetOp(OpId::kMatmulBT, q.device.type))(q, out, a, b);
}
// vt::MatmulBTQuant — see ops.h. Validation mirrors MatmulBT except for the
// weight, whose block layout replaces the elementwise stride contract.
void MatmulBTQuant(Queue& q, Tensor& out, const Tensor& a, const Tensor& b) {
VT_CHECK(a.rank == 2 && b.rank == 2 && out.rank == 2,
"matmul_bt_quant: rank-2 tensors required");
VT_CHECK(a.shape[1] == b.shape[1],
"matmul_bt_quant: inner dims mismatch (b is [N,K])");
VT_CHECK(out.shape[0] == a.shape[0] && out.shape[1] == b.shape[0],
"matmul_bt_quant: output shape mismatch");
VT_CHECK(IsBlockQuant(b.dtype),
"matmul_bt_quant: weight must be a block-quantized dtype (use "
"MatmulBT for elementwise weights)");
VT_CHECK(IsFloat(a.dtype) && IsOutFloat(out.dtype),
"matmul_bt_quant: float activation and f32/bf16 output required");
// ggml_row_size asserts the row is whole blocks; a GEMM weight whose K is
// not block-aligned is not keep-quant eligible in the first place.
VT_CHECK(b.shape[1] % BlockElems(b.dtype) == 0,
"matmul_bt_quant: K must be a whole number of weight blocks");
// Same relaxed activation contract as MatmulBT: only the innermost dim must
// be packed, so a column slice of a wider workspace is consumed as-is.
VT_CHECK(a.stride[1] == 1 && a.stride[0] >= a.shape[1],
"matmul_bt_quant: activation rows must be packed (innermost stride "
"1) and non-overlapping");
VT_CHECK(out.IsContiguous(), "matmul_bt_quant: contiguous output required");
VT_CHECK(a.device == b.device && a.device == out.device && a.device == q.device,
"matmul_bt_quant: device mismatch");
reinterpret_cast<MatmulFn>(GetOp(OpId::kMatmulBTQuant, q.device.type))(q, out, a, b);
}
// vt::MatmulBTQuantGrouped — see ops.h. out[P,N], act[P,K], weight[E*N,K]
// block-quant, expert_ids[P] i32. The per-group weight row-block is selected by
// expert_ids[p]; validation mirrors MatmulBTQuant plus the expert-index contract.
void MatmulBTQuantGrouped(Queue& q, Tensor& out, const Tensor& act,
const Tensor& weight, const Tensor& expert_ids) {
VT_CHECK(out.rank == 2 && act.rank == 2 && weight.rank == 2,
"matmul_bt_quant_grouped: rank-2 out/act/weight required");
const int64_t P = out.shape[0], N = out.shape[1], K = act.shape[1];
VT_CHECK(act.shape[0] == P || act.shape[0] == 1,
"matmul_bt_quant_grouped: act rows must be P (per-expert) or 1 (broadcast)");
VT_CHECK(weight.shape[1] == K, "matmul_bt_quant_grouped: weight K mismatch (b is [E*N,K])");
VT_CHECK(weight.shape[0] % N == 0,
"matmul_bt_quant_grouped: weight rows must be a whole multiple of N");
VT_CHECK(IsBlockQuant(weight.dtype),
"matmul_bt_quant_grouped: weight must be a block-quantized dtype");
VT_CHECK(IsFloat(act.dtype) && IsOutFloat(out.dtype),
"matmul_bt_quant_grouped: float activation and f32/bf16 output required");
VT_CHECK(weight.shape[1] % BlockElems(weight.dtype) == 0,
"matmul_bt_quant_grouped: K must be a whole number of weight blocks");
VT_CHECK(expert_ids.Numel() == P && expert_ids.dtype == DType::kI32,
"matmul_bt_quant_grouped: expert_ids must be i32 [P]");
VT_CHECK(act.stride[1] == 1 && act.stride[0] >= K,
"matmul_bt_quant_grouped: activation rows must be packed (innermost stride 1)");
VT_CHECK(out.IsContiguous() && expert_ids.IsContiguous(),
"matmul_bt_quant_grouped: contiguous out + expert_ids required");
VT_CHECK(act.device == q.device && weight.device == q.device &&
out.device == q.device && expert_ids.device == q.device,
"matmul_bt_quant_grouped: device mismatch");
reinterpret_cast<MatmulBTQuantGroupedFn>(GetOp(OpId::kMatmulBTQuantGrouped, q.device.type))(
q, out, act, weight, expert_ids);
}
// vt::MoeGateUpSwiGLUGrouped — see ops.h. out[P,N] f32, act[Pa,K] (Pa==1 broadcast),
// gate_w/up_w[E*N,K] SAME block-quant dtype, expert_ids[P] i32, float limit. Validation
// mirrors MatmulBTQuantGrouped for BOTH weight towers plus the same-dtype/f32-out
// contract the fused epilogue requires.
void MoeGateUpSwiGLUGrouped(Queue& q, Tensor& out, const Tensor& act, const Tensor& gate_w,
const Tensor& up_w, const Tensor& expert_ids, float limit) {
VT_CHECK(out.rank == 2 && act.rank == 2 && gate_w.rank == 2 && up_w.rank == 2,
"moe_gate_up_swiglu: rank-2 out/act/gate_w/up_w required");
const int64_t P = out.shape[0], N = out.shape[1], K = act.shape[1];
VT_CHECK(act.shape[0] == P || act.shape[0] == 1,
"moe_gate_up_swiglu: act rows must be P (per-expert) or 1 (broadcast)");
VT_CHECK(gate_w.shape[1] == K && up_w.shape[1] == K,
"moe_gate_up_swiglu: gate_w/up_w K mismatch (both are [E*N,K])");
VT_CHECK(gate_w.shape[0] % N == 0 && up_w.shape[0] == gate_w.shape[0],
"moe_gate_up_swiglu: gate_w/up_w rows must be a whole multiple of N and equal");
VT_CHECK(IsBlockQuant(gate_w.dtype) && gate_w.dtype == up_w.dtype,
"moe_gate_up_swiglu: gate_w/up_w must be the SAME block-quantized dtype");
VT_CHECK(IsFloat(act.dtype) && out.dtype == DType::kF32,
"moe_gate_up_swiglu: float activation and f32 output required");
VT_CHECK(gate_w.shape[1] % BlockElems(gate_w.dtype) == 0,
"moe_gate_up_swiglu: K must be a whole number of weight blocks");
VT_CHECK(expert_ids.Numel() == P && expert_ids.dtype == DType::kI32,
"moe_gate_up_swiglu: expert_ids must be i32 [P]");
VT_CHECK(act.stride[1] == 1 && act.stride[0] >= K,
"moe_gate_up_swiglu: activation rows must be packed (innermost stride 1)");
VT_CHECK(out.IsContiguous() && expert_ids.IsContiguous(),
"moe_gate_up_swiglu: contiguous out + expert_ids required");
VT_CHECK(act.device == q.device && gate_w.device == q.device && up_w.device == q.device &&
out.device == q.device && expert_ids.device == q.device,
"moe_gate_up_swiglu: device mismatch");
reinterpret_cast<MoeGateUpSwiGLUGroupedFn>(
GetOp(OpId::kMoeGateUpSwiGLUGrouped, q.device.type))(q, out, act, gate_w, up_w, expert_ids,
limit);
}
// vt::BatchedMatmul — `torch.bmm` (mla_attention.py:789 q-side W_UK absorption,
// :1034 W_UV v-up-projection). Stride-driven on every operand because BOTH
// upstream call sites pass transposed views; only the innermost dim must be
// unit-stride.
void BatchedMatmul(Queue& q, Tensor& out, const Tensor& a, const Tensor& b) {
VT_CHECK(a.rank == 3 && b.rank == 3 && out.rank == 3,
"batched_matmul: rank-3 tensors required (out[G,M,N] = a[G,M,K] @ b[G,K,N])");
VT_CHECK(a.shape[0] == b.shape[0] && a.shape[0] == out.shape[0],
"batched_matmul: batch dim mismatch");
VT_CHECK(a.shape[2] == b.shape[1], "batched_matmul: inner dims mismatch");
VT_CHECK(out.shape[1] == a.shape[1] && out.shape[2] == b.shape[2],
"batched_matmul: output shape mismatch");
VT_CHECK(IsFloat(a.dtype) && IsFloat(b.dtype) && IsOutFloat(out.dtype),
"batched_matmul: float inputs and f32/bf16 output required");
VT_CHECK(a.dtype == b.dtype, "batched_matmul: a/b dtype must match");
// Only the innermost stride is constrained; the batch/row strides are free so
// a transposed view (upstream's `q_nope.transpose(0,1)` / `out.transpose(0,1)`)
// is consumed without a copy.
VT_CHECK(a.stride[2] == 1 && b.stride[2] == 1 && out.stride[2] == 1,
"batched_matmul: innermost dimension must be unit-stride");
VT_CHECK(a.stride[1] >= a.shape[2] && b.stride[1] >= b.shape[2] &&
out.stride[1] >= out.shape[2],
"batched_matmul: row stride must not overlap the next row");
VT_CHECK(a.device == b.device && a.device == out.device && a.device == q.device,
"batched_matmul: device mismatch");
GetTypedOp<BatchedMatmulFn>(OpId::kBatchedMatmul, q.device.type)(q, out, a, b);
}
// vt::ConcatMlaNopeRope — the generalization of upstream's two MLA head-concat
// sites: `concat_mla_q` (cache_kernels.cu:1555-1600, the decode 512+64 query)
// and `_concat_k_nope_k_pe` (mla_attention.py:2063-2092, the prefill 128+64 key
// with a head-BROADCAST rope part). Stride checks mirror
// cache_kernels.cu:1572-1577.
void ConcatMlaNopeRope(Queue& q, Tensor& out, const Tensor& nope, const Tensor& rope) {
VT_CHECK(out.rank == 3 && nope.rank == 3 && rope.rank == 3,
"concat_mla_nope_rope: rank-3 [tokens, heads, dim] tensors required");
const int64_t tokens = out.shape[0], heads = out.shape[1];
const int64_t dn = nope.shape[2], dr = rope.shape[2];
VT_CHECK(nope.shape[0] == tokens && rope.shape[0] == tokens,
"concat_mla_nope_rope: token count mismatch");
VT_CHECK(nope.shape[1] == heads, "concat_mla_nope_rope: nope head count mismatch");
VT_CHECK(rope.shape[1] == heads || rope.shape[1] == 1,
"concat_mla_nope_rope: rope must carry `heads` heads or exactly 1 "
"(the single shared k_pe head, broadcast — mla_attention.py:2063-2092)");
VT_CHECK(out.shape[2] == dn + dr,
"concat_mla_nope_rope: out last dim must be nope_dim + rope_dim");
VT_CHECK(dn > 0 && dr > 0, "concat_mla_nope_rope: both parts must be non-empty");
VT_CHECK(out.dtype == nope.dtype && out.dtype == rope.dtype,
"concat_mla_nope_rope: all tensors must share one dtype");
VT_CHECK(IsOutFloat(out.dtype) || out.dtype == DType::kF16,
"concat_mla_nope_rope: f32/bf16/f16 only");
VT_CHECK(out.stride[2] == 1 && nope.stride[2] == 1 && rope.stride[2] == 1,
"concat_mla_nope_rope: innermost dimension must be unit-stride "
"(upstream cache_kernels.cu:1572-1577)");
VT_CHECK(out.device == q.device && nope.device == q.device && rope.device == q.device,
"concat_mla_nope_rope: device mismatch");
if (tokens == 0 || heads == 0) return; // `if (num_tokens == 0) return;` (:1584)
GetTypedOp<ConcatMlaNopeRopeFn>(OpId::kConcatMlaNopeRope, q.device.type)(q, out, nope, rope);
}
void MatmulNvfp4(Queue& q, Tensor& out, const Tensor& act, const Tensor& weight_packed,
const Tensor& weight_scale, float weight_scale_2) {
VT_CHECK(act.rank == 2 && weight_packed.rank == 2 && weight_scale.rank == 2 && out.rank == 2,
"matmul_nvfp4: act/weight_packed/weight_scale/out must be rank-2");
const int64_t m = act.shape[0], k = act.shape[1], n = weight_packed.shape[0];
VT_CHECK(k % 16 == 0, "matmul_nvfp4: K (act inner dim) must be a multiple of 16");
VT_CHECK(weight_packed.shape[1] == k / 2,
"matmul_nvfp4: weight_packed must be [N, K/2] (two fp4 codes per byte)");
VT_CHECK(weight_scale.shape[0] == n && weight_scale.shape[1] == k / 16,
"matmul_nvfp4: weight_scale must be [N, K/16] (one fp8 scale per 16-elem group)");
VT_CHECK(out.shape[0] == m && out.shape[1] == n, "matmul_nvfp4: out must be [M, N]");
VT_CHECK(IsFloat(act.dtype) && IsOutFloat(out.dtype),
"matmul_nvfp4: float act, f32/bf16 out");
VT_CHECK(weight_packed.dtype == DType::kI8 && weight_scale.dtype == DType::kI8,
"matmul_nvfp4: weight_packed/weight_scale must be i8 (raw fp4/fp8 bytes)");
VT_CHECK(act.IsContiguous() && weight_packed.IsContiguous() && weight_scale.IsContiguous() &&
out.IsContiguous(),
"matmul_nvfp4: contiguous tensors required");
VT_CHECK(act.device == q.device && weight_packed.device == q.device &&
weight_scale.device == q.device && out.device == q.device,
"matmul_nvfp4: device mismatch (act/weight_packed/weight_scale/out/queue)");
reinterpret_cast<MatmulNvfp4Fn>(GetOp(OpId::kMatmulNvfp4, q.device.type))(
q, out, act, weight_packed, weight_scale, weight_scale_2);
}
void ScaledFp4Quant(Queue& q, Tensor& out_packed, Tensor& out_scale, const Tensor& x,
float input_global_scale_inv, Fp4ScaleLayout scale_layout) {
VT_CHECK(x.rank == 2 && out_packed.rank == 2 && out_scale.rank == 2,
"scaled_fp4_quant: x/out_packed/out_scale must be rank-2");
const int64_t m = x.shape[0], k = x.shape[1];
VT_CHECK(k % 16 == 0, "scaled_fp4_quant: K (inner dim) must be a multiple of 16");
VT_CHECK(out_packed.shape[0] == m && out_packed.shape[1] == k / 2,
"scaled_fp4_quant: out_packed must be [M, K/2]");
const auto round_up = [](int64_t value, int64_t multiple) {
return (value + multiple - 1) / multiple * multiple;
};
if (scale_layout == Fp4ScaleLayout::kLinear) {
VT_CHECK(out_scale.shape[0] == m && out_scale.shape[1] == k / 16,
"scaled_fp4_quant: linear out_scale must be [M, K/16]");
} else {
VT_CHECK(scale_layout == Fp4ScaleLayout::kCutlassSwizzled,
"scaled_fp4_quant: invalid scale layout");
VT_CHECK(out_scale.shape[0] == round_up(m, 128) &&
out_scale.shape[1] == round_up(k / 16, 4),
"scaled_fp4_quant: swizzled out_scale must be "
"[round_up(M,128), round_up(K/16,4)]");
}
VT_CHECK(IsFloat(x.dtype), "scaled_fp4_quant: float x required");
VT_CHECK(out_packed.dtype == DType::kI8 && out_scale.dtype == DType::kI8,
"scaled_fp4_quant: out_packed/out_scale must be i8 (raw fp4/fp8 bytes)");
VT_CHECK(x.IsContiguous() && out_packed.IsContiguous() && out_scale.IsContiguous(),
"scaled_fp4_quant: contiguous tensors required");
VT_CHECK(x.device == q.device && out_packed.device == q.device && out_scale.device == q.device,
"scaled_fp4_quant: device mismatch (x/out_packed/out_scale/queue)");
reinterpret_cast<ScaledFp4QuantFn>(GetOp(OpId::kScaledFp4Quant, q.device.type))(
q, out_packed, out_scale, x, input_global_scale_inv, scale_layout);
}
void SiluMulFp4Quant(Queue& q, Tensor& out_packed, Tensor& out_scale, const Tensor& gate,
const Tensor& up, float input_global_scale_inv,
Fp4ScaleLayout scale_layout) {
VT_CHECK(gate.rank == 2 && up.rank == 2 && out_packed.rank == 2 && out_scale.rank == 2,
"silu_mul_fp4_quant: gate/up/out_packed/out_scale must be rank-2");
const int64_t m = gate.shape[0], i = gate.shape[1];
VT_CHECK(up.shape[0] == m && up.shape[1] == i, "silu_mul_fp4_quant: gate/up shape mismatch");
VT_CHECK(i % 16 == 0, "silu_mul_fp4_quant: I (inner dim) must be a multiple of 16");
VT_CHECK(out_packed.shape[0] == m && out_packed.shape[1] == i / 2,
"silu_mul_fp4_quant: out_packed must be [M, I/2]");
const auto round_up = [](int64_t value, int64_t multiple) {
return (value + multiple - 1) / multiple * multiple;
};
if (scale_layout == Fp4ScaleLayout::kLinear) {
VT_CHECK(out_scale.shape[0] == m && out_scale.shape[1] == i / 16,
"silu_mul_fp4_quant: linear out_scale must be [M, I/16]");
} else {
VT_CHECK(scale_layout == Fp4ScaleLayout::kCutlassSwizzled,
"silu_mul_fp4_quant: invalid scale layout");
VT_CHECK(out_scale.shape[0] == round_up(m, 128) &&
out_scale.shape[1] == round_up(i / 16, 4),
"silu_mul_fp4_quant: swizzled out_scale must be "
"[round_up(M,128), round_up(I/16,4)]");
}
VT_CHECK(IsFloat(gate.dtype) && gate.dtype == up.dtype,
"silu_mul_fp4_quant: gate/up must be the same float dtype");
VT_CHECK(out_packed.dtype == DType::kI8 && out_scale.dtype == DType::kI8,
"silu_mul_fp4_quant: out_packed/out_scale must be i8 (raw fp4/fp8 bytes)");
VT_CHECK(gate.IsContiguous() && up.IsContiguous() && out_packed.IsContiguous() &&
out_scale.IsContiguous(),
"silu_mul_fp4_quant: contiguous tensors required");
VT_CHECK(gate.device == q.device && up.device == q.device && out_packed.device == q.device &&
out_scale.device == q.device,
"silu_mul_fp4_quant: device mismatch");
reinterpret_cast<SiluMulFp4QuantFn>(GetOp(OpId::kSiluMulFp4Quant, q.device.type))(
q, out_packed, out_scale, gate, up, input_global_scale_inv, scale_layout);
}
void SiluAndMulFp4Quant(Queue& q, Tensor& out_packed, Tensor& out_scale,
const Tensor& gate_up, float input_global_scale_inv,
Fp4ScaleLayout scale_layout) {
VT_CHECK(gate_up.rank == 2 && out_packed.rank == 2 && out_scale.rank == 2,
"silu_and_mul_fp4_quant: gate_up/out_packed/out_scale must be rank-2");
const int64_t m = gate_up.shape[0];
VT_CHECK(gate_up.shape[1] % 2 == 0,
"silu_and_mul_fp4_quant: gate_up inner dim must be even");
const int64_t i = gate_up.shape[1] / 2;
VT_CHECK(i % 16 == 0,
"silu_and_mul_fp4_quant: I (half inner dim) must be a multiple of 16");
VT_CHECK(out_packed.shape[0] == m && out_packed.shape[1] == i / 2,
"silu_and_mul_fp4_quant: out_packed must be [M, I/2]");
const auto round_up = [](int64_t value, int64_t multiple) {
return (value + multiple - 1) / multiple * multiple;
};
if (scale_layout == Fp4ScaleLayout::kLinear) {
VT_CHECK(out_scale.shape[0] == m && out_scale.shape[1] == i / 16,
"silu_and_mul_fp4_quant: linear out_scale must be [M, I/16]");
} else {
VT_CHECK(scale_layout == Fp4ScaleLayout::kCutlassSwizzled,
"silu_and_mul_fp4_quant: invalid scale layout");
VT_CHECK(out_scale.shape[0] == round_up(m, 128) &&
out_scale.shape[1] == round_up(i / 16, 4),
"silu_and_mul_fp4_quant: swizzled out_scale must be "
"[round_up(M,128), round_up(I/16,4)]");
}
VT_CHECK(gate_up.dtype == DType::kF32 || gate_up.dtype == DType::kBF16,
"silu_and_mul_fp4_quant: gate_up must be f32 or bf16");
VT_CHECK(out_packed.dtype == DType::kI8 && out_scale.dtype == DType::kI8,
"silu_and_mul_fp4_quant: outputs must be i8 (raw fp4/fp8 bytes)");
VT_CHECK(gate_up.IsContiguous() && out_packed.IsContiguous() &&
out_scale.IsContiguous(),
"silu_and_mul_fp4_quant: contiguous tensors required");
VT_CHECK(gate_up.device == q.device && out_packed.device == q.device &&
out_scale.device == q.device,
"silu_and_mul_fp4_quant: device mismatch");
reinterpret_cast<SiluAndMulFp4QuantFn>(
GetOp(OpId::kSiluAndMulFp4Quant, q.device.type))(
q, out_packed, out_scale, gate_up, input_global_scale_inv, scale_layout);
}
void SigmoidGateFp4Quant(Queue& q, Tensor& out_packed, Tensor& out_scale,
const Tensor& attn, const Tensor& gate,
float input_global_scale_inv, Fp4ScaleLayout scale_layout) {
VT_CHECK(attn.rank == 2 && gate.rank == 2 && out_packed.rank == 2 && out_scale.rank == 2,
"sigmoid_gate_fp4_quant: attn/gate/out_packed/out_scale must be rank-2");
const int64_t m = attn.shape[0], i = attn.shape[1];
VT_CHECK(gate.shape[0] == m && gate.shape[1] == i,
"sigmoid_gate_fp4_quant: attn/gate shape mismatch");
VT_CHECK(i % 16 == 0, "sigmoid_gate_fp4_quant: K (inner dim) must be a multiple of 16");
VT_CHECK(out_packed.shape[0] == m && out_packed.shape[1] == i / 2,
"sigmoid_gate_fp4_quant: out_packed must be [M, K/2]");
const auto round_up = [](int64_t value, int64_t multiple) {
return (value + multiple - 1) / multiple * multiple;
};
if (scale_layout == Fp4ScaleLayout::kLinear) {
VT_CHECK(out_scale.shape[0] == m && out_scale.shape[1] == i / 16,
"sigmoid_gate_fp4_quant: linear out_scale must be [M, K/16]");
} else {
VT_CHECK(scale_layout == Fp4ScaleLayout::kCutlassSwizzled,
"sigmoid_gate_fp4_quant: invalid scale layout");
VT_CHECK(out_scale.shape[0] == round_up(m, 128) &&
out_scale.shape[1] == round_up(i / 16, 4),
"sigmoid_gate_fp4_quant: swizzled out_scale must be "
"[round_up(M,128), round_up(K/16,4)]");
}
VT_CHECK(attn.dtype == DType::kF32 || attn.dtype == DType::kBF16,
"sigmoid_gate_fp4_quant: attn must be f32 or bf16");
VT_CHECK(gate.dtype == DType::kF32,
"sigmoid_gate_fp4_quant: gate must be f32 (sigmoid input unrounded)");
VT_CHECK(out_packed.dtype == DType::kI8 && out_scale.dtype == DType::kI8,
"sigmoid_gate_fp4_quant: out_packed/out_scale must be i8 (raw fp4/fp8 bytes)");
VT_CHECK(attn.IsContiguous() && gate.IsContiguous() && out_packed.IsContiguous() &&
out_scale.IsContiguous(),
"sigmoid_gate_fp4_quant: contiguous tensors required");
VT_CHECK(attn.device == q.device && gate.device == q.device &&
out_packed.device == q.device && out_scale.device == q.device,
"sigmoid_gate_fp4_quant: device mismatch");
reinterpret_cast<SigmoidGateFp4QuantFn>(GetOp(OpId::kSigmoidGateFp4Quant, q.device.type))(
q, out_packed, out_scale, attn, gate, input_global_scale_inv, scale_layout);
}
void MatmulNvfp4Fp4(Queue& q, Tensor& out, const Tensor& a_packed, const Tensor& a_scale,
const Tensor& b_packed, const Tensor& b_scale, float alpha) {
VT_CHECK(out.rank == 2 && a_packed.rank == 2 && a_scale.rank == 2 && b_packed.rank == 2 &&
b_scale.rank == 2,
"matmul_nvfp4_fp4: all tensors must be rank-2");
const int64_t m = a_packed.shape[0], k = a_packed.shape[1] * 2, n = b_packed.shape[0];
VT_CHECK(k % 16 == 0, "matmul_nvfp4_fp4: K (inner dim) must be a multiple of 16");
VT_CHECK(a_scale.shape[0] == m && a_scale.shape[1] == k / 16,
"matmul_nvfp4_fp4: a_scale must be [M, K/16]");
VT_CHECK(b_packed.shape[1] == k / 2,
"matmul_nvfp4_fp4: b_packed must be [N, K/2] (K matches a_packed)");
VT_CHECK(b_scale.shape[0] == n && b_scale.shape[1] == k / 16,
"matmul_nvfp4_fp4: b_scale must be [N, K/16]");
VT_CHECK(out.shape[0] == m && out.shape[1] == n, "matmul_nvfp4_fp4: out must be [M, N]");
VT_CHECK(IsOutFloat(out.dtype), "matmul_nvfp4_fp4: f32/bf16 out");
VT_CHECK(a_packed.dtype == DType::kI8 && a_scale.dtype == DType::kI8 &&
b_packed.dtype == DType::kI8 && b_scale.dtype == DType::kI8,
"matmul_nvfp4_fp4: packed/scale operands must be i8 (raw fp4/fp8 bytes)");
VT_CHECK(out.IsContiguous() && a_packed.IsContiguous() && a_scale.IsContiguous() &&
b_packed.IsContiguous() && b_scale.IsContiguous(),
"matmul_nvfp4_fp4: contiguous tensors required");
VT_CHECK(out.device == q.device && a_packed.device == q.device && a_scale.device == q.device &&
b_packed.device == q.device && b_scale.device == q.device,
"matmul_nvfp4_fp4: device mismatch");
reinterpret_cast<MatmulNvfp4Fp4Fn>(GetOp(OpId::kMatmulNvfp4Fp4, q.device.type))(
q, out, a_packed, a_scale, b_packed, b_scale, alpha);
}
void SwizzleBlockscale(Queue& q, Tensor& out_swizzled, const Tensor& in_linear) {
VT_CHECK(in_linear.rank == 2 && out_swizzled.rank == 2,
"swizzle_blockscale: rank-2 tensors required");
const int64_t rows = in_linear.shape[0], cols = in_linear.shape[1];
auto round_up = [](int64_t x, int64_t y) { return (x + y - 1) / y * y; };
VT_CHECK(out_swizzled.shape[0] == round_up(rows, 128) &&
out_swizzled.shape[1] == round_up(cols, 4),
"swizzle_blockscale: out must be [round_up(rows,128), round_up(cols,4)]");
VT_CHECK(in_linear.dtype == DType::kI8 && out_swizzled.dtype == DType::kI8,
"swizzle_blockscale: i8 (raw fp8) operands required");
VT_CHECK(in_linear.IsContiguous() && out_swizzled.IsContiguous(),
"swizzle_blockscale: contiguous tensors required");
VT_CHECK(in_linear.device == q.device && out_swizzled.device == q.device,
"swizzle_blockscale: device mismatch");
reinterpret_cast<SwizzleBlockscaleFn>(GetOp(OpId::kSwizzleBlockscale, q.device.type))(
q, out_swizzled, in_linear);
}
namespace {
void ValidateMatmulNvfp4Cutlass(Queue& q, Tensor& out,
const Tensor& a_packed,
const Tensor& a_sf_sw,
const Tensor& b_packed,
const Tensor& b_sf_sw) {
VT_CHECK(out.rank == 2 && a_packed.rank == 2 && a_sf_sw.rank == 2 && b_packed.rank == 2 &&
b_sf_sw.rank == 2,
"matmul_nvfp4_cutlass: all tensors must be rank-2");
const int64_t m = a_packed.shape[0], k = a_packed.shape[1] * 2, n = b_packed.shape[0];
VT_CHECK(k % 32 == 0 && n % 32 == 0, "matmul_nvfp4_cutlass: K and N must be multiples of 32");
VT_CHECK(b_packed.shape[1] == k / 2,
"matmul_nvfp4_cutlass: b_packed must be [N, K/2] (K matches a_packed)");
VT_CHECK(out.shape[0] == m && out.shape[1] == n, "matmul_nvfp4_cutlass: out must be [M, N]");
VT_CHECK(out.dtype == DType::kBF16 || out.dtype == DType::kF32,
"matmul_nvfp4_cutlass: out must be bf16 or f32 (bf16 epilogue, f32 via cast)");
VT_CHECK(a_packed.dtype == DType::kI8 && a_sf_sw.dtype == DType::kI8 &&
b_packed.dtype == DType::kI8 && b_sf_sw.dtype == DType::kI8,
"matmul_nvfp4_cutlass: packed/scale operands must be i8 (raw fp4/fp8 bytes)");
auto round_up = [](int64_t x, int64_t y) { return (x + y - 1) / y * y; };
VT_CHECK(a_sf_sw.shape[0] == round_up(m, 128) && a_sf_sw.shape[1] == round_up(k / 16, 4),
"matmul_nvfp4_cutlass: a_sf must be swizzled [round_up(M,128), round_up(K/16,4)]");
VT_CHECK(b_sf_sw.shape[0] == round_up(n, 128) && b_sf_sw.shape[1] == round_up(k / 16, 4),
"matmul_nvfp4_cutlass: b_sf must be swizzled [round_up(N,128), round_up(K/16,4)]");
VT_CHECK(out.device == q.device && a_packed.device == q.device && a_sf_sw.device == q.device &&
b_packed.device == q.device && b_sf_sw.device == q.device,
"matmul_nvfp4_cutlass: device mismatch");
}
void DispatchMatmulNvfp4Cutlass(Queue& q, Tensor& out,
const Tensor& a_packed,
const Tensor& a_sf_sw,
const Tensor& b_packed,
const Tensor& b_sf_sw,
const Tensor* alpha_device,
float alpha_host) {
reinterpret_cast<MatmulNvfp4CutlassFn>(GetOp(OpId::kMatmulNvfp4Cutlass, q.device.type))(
q, out, a_packed, a_sf_sw, b_packed, b_sf_sw, alpha_device,
alpha_host);
}
} // namespace
void MatmulNvfp4Cutlass(Queue& q, Tensor& out, const Tensor& a_packed,
const Tensor& a_sf_sw, const Tensor& b_packed,
const Tensor& b_sf_sw, const Tensor& alpha) {
ValidateMatmulNvfp4Cutlass(q, out, a_packed, a_sf_sw, b_packed, b_sf_sw);
VT_CHECK(alpha.rank == 0 || alpha.rank == 1,
"matmul_nvfp4_cutlass: alpha must be a rank-0 or rank-1 scalar tensor");
VT_CHECK(alpha.Numel() == 1,
"matmul_nvfp4_cutlass: alpha must contain exactly one element");
VT_CHECK(alpha.dtype == DType::kF32,
"matmul_nvfp4_cutlass: alpha must be f32");
VT_CHECK(alpha.data != nullptr,
"matmul_nvfp4_cutlass: alpha must have non-null storage");
VT_CHECK(alpha.IsContiguous(),
"matmul_nvfp4_cutlass: alpha must be contiguous");
VT_CHECK(alpha.device == q.device,
"matmul_nvfp4_cutlass: alpha device mismatch");
DispatchMatmulNvfp4Cutlass(q, out, a_packed, a_sf_sw, b_packed,
b_sf_sw, &alpha, 0.0F);
}
void MatmulNvfp4Cutlass(Queue& q, Tensor& out, const Tensor& a_packed,
const Tensor& a_sf_sw, const Tensor& b_packed,
const Tensor& b_sf_sw, float alpha) {
ValidateMatmulNvfp4Cutlass(q, out, a_packed, a_sf_sw, b_packed, b_sf_sw);
DispatchMatmulNvfp4Cutlass(q, out, a_packed, a_sf_sw, b_packed,
b_sf_sw, nullptr, alpha);
}
void QuantFp8Static(Queue& q, Tensor& out_fp8, const Tensor& x, float input_scale) {
VT_CHECK(x.rank == 2 && out_fp8.rank == 2, "quant_fp8_static: x/out must be rank-2");
VT_CHECK(out_fp8.shape[0] == x.shape[0] && out_fp8.shape[1] == x.shape[1],
"quant_fp8_static: out must match x shape [M,K]");
VT_CHECK(IsFloat(x.dtype), "quant_fp8_static: float x (f32/bf16) required");
VT_CHECK(out_fp8.dtype == DType::kI8, "quant_fp8_static: out must be i8 (raw fp8-e4m3fn bytes)");
VT_CHECK(x.IsContiguous() && out_fp8.IsContiguous(),
"quant_fp8_static: contiguous tensors required");
VT_CHECK(x.device == q.device && out_fp8.device == q.device,
"quant_fp8_static: device mismatch (x/out/queue)");
reinterpret_cast<QuantFp8StaticFn>(GetOp(OpId::kQuantFp8Static, q.device.type))(q, out_fp8, x,
input_scale);
}
void RmsNormQuantFp8(Queue& q, Tensor& out_fp8, Tensor* out_bf16, const Tensor& x,
const Tensor& weight, const RmsNormArgs& args, Tensor* residual,
float input_scale) {
VT_CHECK(x.rank == 2 && out_fp8.rank == 2 && weight.rank == 1,
"rmsnorm_quant_fp8: x/out_fp8 rank-2, weight rank-1");
VT_CHECK(x.shape[0] == out_fp8.shape[0] && x.shape[1] == out_fp8.shape[1],
"rmsnorm_quant_fp8: out_fp8 must match x shape [T,H]");
VT_CHECK(weight.shape[0] == x.shape[1], "rmsnorm_quant_fp8: weight size mismatch");
VT_CHECK(IsFloat(x.dtype) && IsFloat(weight.dtype), "rmsnorm_quant_fp8: float x/weight required");
VT_CHECK(out_fp8.dtype == DType::kI8,
"rmsnorm_quant_fp8: out_fp8 must be i8 (raw fp8-e4m3fn bytes)");
VT_CHECK(x.IsContiguous() && out_fp8.IsContiguous() && weight.IsContiguous(),
"rmsnorm_quant_fp8: contiguous tensors required");
if (out_bf16 != nullptr) {
VT_CHECK(out_bf16->dtype == DType::kBF16 && out_bf16->rank == 2 &&
out_bf16->shape[0] == x.shape[0] && out_bf16->shape[1] == x.shape[1] &&
out_bf16->IsContiguous() && out_bf16->device == x.device,
"rmsnorm_quant_fp8: out_bf16 must be bf16 [T,H] contiguous on x's device");
}
if (residual != nullptr) {
VT_CHECK((residual->dtype == DType::kF32 || residual->dtype == DType::kBF16) &&
residual->rank == 2 && residual->shape[0] == x.shape[0] &&
residual->shape[1] == x.shape[1] && residual->IsContiguous() &&
residual->device == x.device,
"rmsnorm_quant_fp8: residual must be f32/bf16 [T,H] contiguous on x's device");
}
VT_CHECK(x.device == out_fp8.device && weight.device == x.device && x.device == q.device,
"rmsnorm_quant_fp8: device mismatch (x/out_fp8/weight/queue)");
reinterpret_cast<RmsNormQuantFp8Fn>(GetOp(OpId::kRmsNormQuantFp8, q.device.type))(
q, out_fp8, out_bf16, x, weight, args, residual, input_scale);
}
void RmsNormGatedQuantFp8(Queue& q, Tensor& out_fp8, const Tensor& x, const Tensor& gate,
const Tensor& weight, const RmsNormGatedArgs& args, float input_scale) {
const int64_t d = x.rank == 0 ? 0 : x.shape[x.rank - 1];
VT_CHECK(weight.rank == 1 && weight.shape[0] == d,
"rmsnorm_gated_quant_fp8: weight must be rank-1 [D] matching x's last dim");
VT_CHECK(out_fp8.rank == x.rank, "rmsnorm_gated_quant_fp8: out_fp8 rank must match x");
for (int i = 0; i < x.rank; ++i)
VT_CHECK(out_fp8.shape[i] == x.shape[i],
"rmsnorm_gated_quant_fp8: out_fp8 shape must match x");
VT_CHECK(IsFloat(x.dtype) && IsFloat(weight.dtype) && IsFloat(gate.dtype),
"rmsnorm_gated_quant_fp8: float x/gate/weight required");
VT_CHECK(gate.dtype == x.dtype && weight.dtype == x.dtype,
"rmsnorm_gated_quant_fp8: gate/weight dtype must match x");
VT_CHECK(out_fp8.dtype == DType::kI8,
"rmsnorm_gated_quant_fp8: out_fp8 must be i8 (raw fp8-e4m3fn bytes)");
VT_CHECK(x.IsContiguous() && out_fp8.IsContiguous() && weight.IsContiguous(),
"rmsnorm_gated_quant_fp8: contiguous x/out_fp8/weight required");
VT_CHECK(x.device == out_fp8.device && weight.device == x.device && gate.device == x.device &&
x.device == q.device,
"rmsnorm_gated_quant_fp8: device mismatch (x/out_fp8/gate/weight/queue)");
reinterpret_cast<RmsNormGatedQuantFp8Fn>(GetOp(OpId::kRmsNormGatedQuantFp8, q.device.type))(
q, out_fp8, x, gate, weight, args, input_scale);
}
void MatmulFp8Cutlass(Queue& q, Tensor& out, const Tensor& a_fp8, const Tensor& b_fp8,
float alpha) {
VT_CHECK(out.rank == 2 && a_fp8.rank == 2 && b_fp8.rank == 2,
"matmul_fp8_cutlass: all tensors must be rank-2");
const int64_t m = a_fp8.shape[0], k = a_fp8.shape[1], n = b_fp8.shape[0];
VT_CHECK(k % 16 == 0 && n % 16 == 0, "matmul_fp8_cutlass: K and N must be multiples of 16");
VT_CHECK(b_fp8.shape[1] == k, "matmul_fp8_cutlass: b_fp8 must be [N, K] (K matches a_fp8)");
VT_CHECK(out.shape[0] == m && out.shape[1] == n, "matmul_fp8_cutlass: out must be [M, N]");
VT_CHECK(out.dtype == DType::kBF16 || out.dtype == DType::kF32,
"matmul_fp8_cutlass: out must be bf16 or f32 (bf16 epilogue, f32 via cast)");
VT_CHECK(a_fp8.dtype == DType::kI8 && b_fp8.dtype == DType::kI8,
"matmul_fp8_cutlass: a_fp8/b_fp8 must be i8 (raw fp8-e4m3fn bytes)");
VT_CHECK(out.IsContiguous() && a_fp8.IsContiguous() && b_fp8.IsContiguous(),
"matmul_fp8_cutlass: contiguous tensors required");
VT_CHECK(out.device == q.device && a_fp8.device == q.device && b_fp8.device == q.device,
"matmul_fp8_cutlass: device mismatch");
reinterpret_cast<MatmulFp8CutlassFn>(GetOp(OpId::kMatmulFp8Cutlass, q.device.type))(
q, out, a_fp8, b_fp8, alpha);
}
void MatmulFp8CublasLt(Queue& q, Tensor& out, const Tensor& a_fp8, const Tensor& b_fp8,
float alpha, bool claims_splitk1_premise) {
// Same argument contract as MatmulFp8Cutlass (drop-in fp8 dense GEMM).
VT_CHECK(out.rank == 2 && a_fp8.rank == 2 && b_fp8.rank == 2,
"matmul_fp8_cublaslt: all tensors must be rank-2");
const int64_t m = a_fp8.shape[0], k = a_fp8.shape[1], n = b_fp8.shape[0];
VT_CHECK(k % 16 == 0 && n % 16 == 0, "matmul_fp8_cublaslt: K and N must be multiples of 16");
VT_CHECK(b_fp8.shape[1] == k, "matmul_fp8_cublaslt: b_fp8 must be [N, K] (K matches a_fp8)");
VT_CHECK(out.shape[0] == m && out.shape[1] == n, "matmul_fp8_cublaslt: out must be [M, N]");
VT_CHECK(out.dtype == DType::kBF16 || out.dtype == DType::kF32,
"matmul_fp8_cublaslt: out must be bf16 or f32");
VT_CHECK(a_fp8.dtype == DType::kI8 && b_fp8.dtype == DType::kI8,
"matmul_fp8_cublaslt: a_fp8/b_fp8 must be i8 (raw fp8-e4m3fn bytes)");
VT_CHECK(out.IsContiguous() && a_fp8.IsContiguous() && b_fp8.IsContiguous(),
"matmul_fp8_cublaslt: contiguous tensors required");
VT_CHECK(out.device == q.device && a_fp8.device == q.device && b_fp8.device == q.device,
"matmul_fp8_cublaslt: device mismatch");
reinterpret_cast<MatmulFp8CublasLtFn>(GetOp(OpId::kMatmulFp8CublasLt, q.device.type))(
q, out, a_fp8, b_fp8, alpha, claims_splitk1_premise);
}
void MatmulFp8CublasLtAlphaVec(Queue& q, Tensor& out, const Tensor& a_fp8, const Tensor& b_fp8,
const Tensor& alpha_vec, bool claims_splitk1_premise) {
// Same operand contract as MatmulFp8CublasLt, plus the per-column alpha.
//
// The output was f32 ONLY, because the fallback arm applies the vector with
// vt::MulColVecF32 and that op was f32-typed -- accepting bf16 would have been
// a capability the fallback could not honor. PERF-FP8-ALPHA-FOLD / #417 removed
// that blocker (MulColVecF32 now carries a bf16 store arm), so bf16 is
// admitted: the GEMM emits a bf16 D and the column pass runs at bf16, halving
// the bytes it moves. bf16 is also what vLLM's ModelOptFp8LinearMethod emits
// here (out_dtype = the model dtype, modelopt.py:458).
//
// A bf16 `out` always takes the TWO-LAUNCH arm, whatever
// VT_FP8_ALPHA_VEC_EPILOGUE says -- see the CUDA implementation for why: at
// bf16 the epilogue would round ONCE and the fallback rounds TWICE, so letting
// the toggle choose between them would turn a performance switch into a
// numerics switch. The toggle stays a pure performance A/B at every dtype.
VT_CHECK(out.rank == 2 && a_fp8.rank == 2 && b_fp8.rank == 2,
"matmul_fp8_cublaslt_alpha_vec: all tensors must be rank-2");
const int64_t m = a_fp8.shape[0], k = a_fp8.shape[1], n = b_fp8.shape[0];
VT_CHECK(k % 16 == 0 && n % 16 == 0,
"matmul_fp8_cublaslt_alpha_vec: K and N must be multiples of 16");
VT_CHECK(b_fp8.shape[1] == k,
"matmul_fp8_cublaslt_alpha_vec: b_fp8 must be [N, K] (K matches a_fp8)");
VT_CHECK(out.shape[0] == m && out.shape[1] == n,
"matmul_fp8_cublaslt_alpha_vec: out must be [M, N]");
VT_CHECK(out.dtype == DType::kF32 || out.dtype == DType::kBF16,
"matmul_fp8_cublaslt_alpha_vec: out must be f32 or bf16");
VT_CHECK(a_fp8.dtype == DType::kI8 && b_fp8.dtype == DType::kI8,
"matmul_fp8_cublaslt_alpha_vec: a_fp8/b_fp8 must be i8 (raw fp8-e4m3fn bytes)");
VT_CHECK(out.IsContiguous() && a_fp8.IsContiguous() && b_fp8.IsContiguous(),
"matmul_fp8_cublaslt_alpha_vec: contiguous tensors required");
// cuBLASLt reads the alpha vector as one entry per OUTPUT ROW of its
// column-major D, which is our row-major out's COLUMN count, N.
VT_CHECK(alpha_vec.rank == 1 && alpha_vec.shape[0] == n,
"matmul_fp8_cublaslt_alpha_vec: alpha_vec must be [N] (one alpha per output column)");
VT_CHECK(alpha_vec.dtype == DType::kF32 && alpha_vec.IsContiguous(),
"matmul_fp8_cublaslt_alpha_vec: alpha_vec must be contiguous f32");
VT_CHECK(out.device == q.device && a_fp8.device == q.device && b_fp8.device == q.device &&
alpha_vec.device == q.device,
"matmul_fp8_cublaslt_alpha_vec: device mismatch");
reinterpret_cast<MatmulFp8CublasLtAlphaVecFn>(
GetOp(OpId::kMatmulFp8CublasLtAlphaVec, q.device.type))(q, out, a_fp8, b_fp8, alpha_vec,
claims_splitk1_premise);
}
void MoeGroupedGemmNvfp4(Queue& q, Tensor& out, const Tensor& act, const Tensor& expert_ids,
const Tensor* row_map, const Tensor& packed_ptrs,
const Tensor& scale_ptrs, const Tensor& scale2s) {
VT_CHECK(out.rank == 2 && act.rank == 2, "moe_grouped_gemm_nvfp4: out/act must be rank-2");
const int64_t p = out.shape[0], k = act.shape[1], e = scale2s.shape[0];
VT_CHECK(k % 16 == 0, "moe_grouped_gemm_nvfp4: K (act inner dim) must be a multiple of 16");
VT_CHECK(expert_ids.Numel() == p,
"moe_grouped_gemm_nvfp4: expert_ids must have P entries (one per out row)");
VT_CHECK(expert_ids.dtype == DType::kI32, "moe_grouped_gemm_nvfp4: expert_ids must be i32");
VT_CHECK(packed_ptrs.Numel() == e && scale_ptrs.Numel() == e,
"moe_grouped_gemm_nvfp4: packed_ptrs/scale_ptrs must have E entries");
VT_CHECK(packed_ptrs.dtype == DType::kI64 && scale_ptrs.dtype == DType::kI64,
"moe_grouped_gemm_nvfp4: packed_ptrs/scale_ptrs must be i64 (device pointers)");
VT_CHECK(scale2s.dtype == DType::kF32, "moe_grouped_gemm_nvfp4: scale2s must be f32");
VT_CHECK(IsFloat(act.dtype) && IsOutFloat(out.dtype),
"moe_grouped_gemm_nvfp4: float act, f32/bf16 out");
VT_CHECK(act.IsContiguous() && out.IsContiguous() && expert_ids.IsContiguous() &&
packed_ptrs.IsContiguous() && scale_ptrs.IsContiguous() && scale2s.IsContiguous(),
"moe_grouped_gemm_nvfp4: contiguous tensors required");
VT_CHECK(act.device == q.device && out.device == q.device && expert_ids.device == q.device &&
packed_ptrs.device == q.device && scale_ptrs.device == q.device &&
scale2s.device == q.device,
"moe_grouped_gemm_nvfp4: device mismatch");
if (row_map != nullptr) {
VT_CHECK(row_map->Numel() == p && row_map->dtype == DType::kI32 && row_map->IsContiguous() &&
row_map->device == q.device,
"moe_grouped_gemm_nvfp4: row_map must be contiguous i32 [P] on the queue device");
}
reinterpret_cast<MoeGroupedGemmNvfp4Fn>(GetOp(OpId::kMoeGroupedGemmNvfp4, q.device.type))(
q, out, act, expert_ids, row_map, packed_ptrs, scale_ptrs, scale2s);
}
void MoeGroupedGemmBf16(Queue& q, Tensor& out, const Tensor& act, const Tensor& expert_ids,
const Tensor* row_map, const Tensor& weight_ptrs) {
VT_CHECK(out.rank == 2 && act.rank == 2, "moe_grouped_gemm_bf16: out/act must be rank-2");
const int64_t p = out.shape[0], e = weight_ptrs.shape[0];
VT_CHECK(act.dtype == DType::kBF16, "moe_grouped_gemm_bf16: act must be bf16");
VT_CHECK(IsOutFloat(out.dtype), "moe_grouped_gemm_bf16: out must be f32/bf16");
VT_CHECK(expert_ids.Numel() == p,
"moe_grouped_gemm_bf16: expert_ids must have P entries (one per out row)");
VT_CHECK(expert_ids.dtype == DType::kI32, "moe_grouped_gemm_bf16: expert_ids must be i32");
VT_CHECK(weight_ptrs.Numel() == e && weight_ptrs.dtype == DType::kI64,
"moe_grouped_gemm_bf16: weight_ptrs must be i64 [E] (device pointers)");
VT_CHECK(act.IsContiguous() && out.IsContiguous() && expert_ids.IsContiguous() &&
weight_ptrs.IsContiguous(),
"moe_grouped_gemm_bf16: contiguous tensors required");
VT_CHECK(act.device == q.device && out.device == q.device && expert_ids.device == q.device &&
weight_ptrs.device == q.device,
"moe_grouped_gemm_bf16: device mismatch");
if (row_map != nullptr) {
VT_CHECK(row_map->Numel() == p && row_map->dtype == DType::kI32 && row_map->IsContiguous() &&
row_map->device == q.device,
"moe_grouped_gemm_bf16: row_map must be contiguous i32 [P] on the queue device");
}
reinterpret_cast<MoeGroupedGemmBf16Fn>(GetOp(OpId::kMoeGroupedGemmBf16, q.device.type))(
q, out, act, expert_ids, row_map, weight_ptrs);
}
void MoeGroupedGemmBf16GateUpSilu(Queue& q, Tensor& out, const Tensor& act,
const Tensor& expert_ids, const Tensor* row_map,
const Tensor& gate_ptrs, const Tensor& up_ptrs) {
VT_CHECK(out.rank == 2 && act.rank == 2,
"moe_grouped_gemm_bf16_gate_up_silu: out/act must be rank-2");
const int64_t p = out.shape[0], e = gate_ptrs.shape[0];
VT_CHECK(act.dtype == DType::kBF16, "moe_grouped_gemm_bf16_gate_up_silu: act must be bf16");
VT_CHECK(out.dtype == DType::kBF16,
"moe_grouped_gemm_bf16_gate_up_silu: out must be bf16 (fused silu store)");
VT_CHECK(expert_ids.Numel() == p,
"moe_grouped_gemm_bf16_gate_up_silu: expert_ids must have P entries (one per out row)");
VT_CHECK(expert_ids.dtype == DType::kI32,
"moe_grouped_gemm_bf16_gate_up_silu: expert_ids must be i32");
VT_CHECK(gate_ptrs.Numel() == e && gate_ptrs.dtype == DType::kI64 && up_ptrs.Numel() == e &&
up_ptrs.dtype == DType::kI64,
"moe_grouped_gemm_bf16_gate_up_silu: gate_ptrs/up_ptrs must be i64 [E] device pointers");
VT_CHECK(act.IsContiguous() && out.IsContiguous() && expert_ids.IsContiguous() &&
gate_ptrs.IsContiguous() && up_ptrs.IsContiguous(),
"moe_grouped_gemm_bf16_gate_up_silu: contiguous tensors required");
VT_CHECK(act.device == q.device && out.device == q.device && expert_ids.device == q.device &&
gate_ptrs.device == q.device && up_ptrs.device == q.device,
"moe_grouped_gemm_bf16_gate_up_silu: device mismatch");
if (row_map != nullptr) {
VT_CHECK(row_map->Numel() == p && row_map->dtype == DType::kI32 && row_map->IsContiguous() &&
row_map->device == q.device,
"moe_grouped_gemm_bf16_gate_up_silu: row_map must be contiguous i32 [P] on the device");
}
reinterpret_cast<MoeGroupedGemmBf16GateUpSiluFn>(
GetOp(OpId::kMoeGroupedGemmBf16GateUpSilu, q.device.type))(q, out, act, expert_ids, row_map,
gate_ptrs, up_ptrs);
}
void MoeGroupedGemmNvfp4Marlin(Queue& q, Tensor& c, const Tensor& a, const Tensor& b_q_weight,
const Tensor& b_scales, const Tensor& global_scale,
Tensor& workspace, const Tensor& sorted_token_ids,
const Tensor& expert_ids, const Tensor& num_tokens_past_padded,
const Tensor& topk_weights, const MoeMarlinArgs& args) {
VT_CHECK(a.rank == 2 && c.rank == 2, "moe_marlin: a/c must be rank-2");
VT_CHECK(a.dtype == DType::kBF16 && c.dtype == DType::kBF16, "moe_marlin: a/c must be bf16");
VT_CHECK(args.size_k % 16 == 0, "moe_marlin: size_k must be a multiple of 16 (group size)");
VT_CHECK(a.shape[0] == args.size_m && a.shape[1] == args.size_k,
"moe_marlin: a shape must be [size_m, size_k]");
VT_CHECK(b_q_weight.rank == 3, "moe_marlin: b_q_weight must be rank-3 [E, K/16, N*8/pack]");
VT_CHECK(expert_ids.dtype == DType::kI32 && sorted_token_ids.dtype == DType::kI32 &&
num_tokens_past_padded.dtype == DType::kI32,
"moe_marlin: align tensors must be i32");
VT_CHECK(global_scale.dtype == DType::kF32 && topk_weights.dtype == DType::kF32,
"moe_marlin: global_scale/topk_weights must be f32");
VT_CHECK(workspace.dtype == DType::kI32, "moe_marlin: workspace must be i32 (reduction locks)");
reinterpret_cast<MoeGroupedGemmNvfp4MarlinFn>(
GetOp(OpId::kMoeGroupedGemmNvfp4Marlin, q.device.type))(
q, c, a, b_q_weight, b_scales, global_scale, workspace, sorted_token_ids, expert_ids,
num_tokens_past_padded, topk_weights, args);
}
void MarlinDenseGemm(Queue& q, Tensor& c, const Tensor& a, const Tensor& b_q_weight,
const Tensor& b_scales, const Tensor& global_scale, Tensor& workspace,
const MarlinDenseArgs& args) {
VT_CHECK(a.rank == 2 && c.rank == 2, "marlin_dense: a/c must be rank-2");
VT_CHECK(a.dtype == DType::kBF16 && c.dtype == DType::kBF16, "marlin_dense: a/c must be bf16");
VT_CHECK(args.size_k % 16 == 0, "marlin_dense: size_k must be a multiple of 16 (group size)");
VT_CHECK(args.group_size == 16 || args.group_size == 32,
"marlin_dense: group_size must be 16 (nvfp4) or 32 (mxfp4)");
VT_CHECK(a.shape[0] == args.size_m && a.shape[1] == args.size_k,
"marlin_dense: a shape must be [size_m, size_k]");
VT_CHECK(c.shape[0] == args.size_m && c.shape[1] == args.size_n,
"marlin_dense: c shape must be [size_m, size_n]");
VT_CHECK(b_q_weight.rank == 2, "marlin_dense: b_q_weight must be rank-2 [K/16, N*8/pack]");
VT_CHECK(global_scale.dtype == DType::kF32, "marlin_dense: global_scale must be f32");
VT_CHECK(workspace.dtype == DType::kI32, "marlin_dense: workspace must be i32 (reduction locks)");
reinterpret_cast<MarlinDenseGemmFn>(GetOp(OpId::kMarlinDenseGemm, q.device.type))(
q, c, a, b_q_weight, b_scales, global_scale, workspace, args);
}
void MoeSiluMul(Queue& q, Tensor& out, const Tensor& gate, const Tensor& up) {
VT_CHECK(gate.Numel() == out.Numel() && up.Numel() == out.Numel(),
"moe_silu_mul: out/gate/up must have the same element count");
VT_CHECK(IsFloat(gate.dtype) && IsFloat(up.dtype) && IsOutFloat(out.dtype),
"moe_silu_mul: float gate/up, f32/bf16 out");
VT_CHECK(out.IsContiguous() && gate.IsContiguous() && up.IsContiguous(),
"moe_silu_mul: contiguous tensors required");
VT_CHECK(out.device == q.device && gate.device == q.device && up.device == q.device,
"moe_silu_mul: device mismatch (out/gate/up/queue)");
reinterpret_cast<MoeSiluMulFn>(GetOp(OpId::kMoeSiluMul, q.device.type))(q, out, gate, up);
}
void RmsNorm(Queue& q, Tensor& out, const Tensor& x, const Tensor& weight,
const RmsNormArgs& args, Tensor* residual) {
VT_CHECK(x.rank == 2 && out.rank == 2 && weight.rank == 1, "rmsnorm: x/out rank-2, w rank-1");
VT_CHECK(x.shape[0] == out.shape[0] && x.shape[1] == out.shape[1], "rmsnorm: shape mismatch");
VT_CHECK(weight.shape[0] == x.shape[1], "rmsnorm: weight size mismatch");
VT_CHECK(IsFloat(x.dtype) && IsFloat(weight.dtype) && IsOutFloat(out.dtype),
"rmsnorm: float in, f32/bf16 out");
VT_CHECK(x.IsContiguous() && out.IsContiguous() && weight.IsContiguous(),
"rmsnorm: contiguous required");
if (residual != nullptr) {
VT_CHECK((residual->dtype == DType::kF32 || residual->dtype == DType::kBF16) &&
residual->rank == 2 &&
residual->shape[0] == x.shape[0] && residual->shape[1] == x.shape[1] &&
residual->IsContiguous() && residual->device == x.device,
"rmsnorm: residual must be f32/bf16 [T,H] contiguous on x's device");
}
VT_CHECK(x.device == out.device && weight.device == x.device && x.device == q.device,
"rmsnorm: device mismatch (x/out/weight/queue)");
reinterpret_cast<RmsNormFn>(GetOp(OpId::kRmsNorm, q.device.type))(q, out, x, weight, args,
residual);
}
namespace {
// Fetch the tensor bound to operand slot `idx`, checked non-null.
Tensor* FusedOp(const FusedBinding& b, uint8_t idx, const char* what) {
VT_CHECK(idx < b.n, "fused_chain: operand index out of range");
VT_CHECK(b.op[idx] != nullptr, what);
return b.op[idx];
}
// Tier-0 composite: walk the recipe DISPATCHING each opcode to the already-
// registered standalone vt:: op. Device-agnostic — every op self-dispatches on
// q.device, so the same walker realizes CPU and CUDA. Byte-exact by construction
// to the unfused standalone-op sequence the model hand-calls (that IS the golden).
// The residual-add idiom (kAdd writing the residual) folds into the following
// norm's RmsNorm(residual) call — the only form whose f32 add-then-normalize the
// standalone op reproduces bit-for-bit.
void FusedChainCompositeImpl(Queue& q, const FusedRecipe& r, const FusedBinding& b,
const FusedParams& p) {
Tensor* add_x = nullptr; // pending residual-add: x operand
Tensor* add_res = nullptr; // pending residual-add: residual operand (also the out)
bool add_pending = false;
for (int s = 0; s < r.n; ++s) {
const FStep& st = r.steps[s];
switch (st.op) {
case FOp::kAdd:
// Residual-add producing the residual stream: fold into the next kRmsNorm.
VT_CHECK(st.nin == 2 && st.out == st.in[1],
"fused_chain composite: kAdd must be residual-add (out==in[1])");
add_x = FusedOp(b, st.in[0], "fused_chain: null add input");
add_res = FusedOp(b, st.out, "fused_chain: null residual");
add_pending = true;
break;
case FOp::kRmsNorm: {
Tensor* out = FusedOp(b, st.out, "fused_chain: null rmsnorm out");
Tensor* w = FusedOp(b, st.in[1], "fused_chain: null rmsnorm weight");
if (add_pending) {
RmsNorm(q, *out, *add_x, *w, RmsNormArgs{p.eps, st.gemma}, add_res);
add_pending = false;
} else {
Tensor* a = FusedOp(b, st.in[0], "fused_chain: null rmsnorm input");
RmsNorm(q, *out, *a, *w, RmsNormArgs{p.eps, st.gemma}, nullptr);
}
break;
}
case FOp::kRmsNormGated: {
Tensor* out = FusedOp(b, st.out, "fused_chain: null gated-norm out");
Tensor* x = FusedOp(b, st.in[0], "fused_chain: null gated-norm x");
Tensor* gate = FusedOp(b, st.in[1], "fused_chain: null gated-norm gate");
Tensor* w = FusedOp(b, st.in[2], "fused_chain: null gated-norm weight");
RmsNormGated(q, *out, *x, *gate, *w, RmsNormGatedArgs{p.eps, st.sigmoid_gate});
break;
}
case FOp::kSiluMul: {