oxicuda-ptx 0.1.0

OxiCUDA PTX - PTX code generation DSL and IR for GPU kernel development
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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
//! Mixture of Experts (`MoE`) kernel templates.
//!
//! Generates PTX kernels for the four phases of `MoE` execution:
//!
//! 1. **Top-k gating**: Softmax over expert logits, select top-k experts per token,
//!    compute normalized gating weights.
//! 2. **Permute**: Scatter tokens to expert bins based on selected expert indices,
//!    tracking per-expert counts and enforcing capacity limits.
//! 3. **Expert GEMM**: Grouped matrix multiplication where each expert processes
//!    its assigned tokens using expert-specific weight matrices.
//! 4. **Unpermute**: Gather expert outputs back to original token order, applying
//!    gating weights and accumulating contributions from top-k experts.
//!
//! # Example
//!
//! ```
//! use oxicuda_ptx::templates::moe::MoETemplate;
//! use oxicuda_ptx::ir::PtxType;
//! use oxicuda_ptx::arch::SmVersion;
//!
//! let template = MoETemplate {
//!     num_experts: 8,
//!     top_k: 2,
//!     hidden_dim: 4096,
//!     expert_dim: 14336,
//!     capacity_factor: 1.25,
//!     sm_version: SmVersion::Sm80,
//!     float_type: PtxType::F32,
//! };
//! let ptx = template.generate_topk_gating().expect("gating kernel");
//! assert!(ptx.contains("topk_gating"));
//! ```

use std::fmt::Write as FmtWrite;

use crate::arch::SmVersion;
use crate::error::PtxGenError;
use crate::ir::PtxType;

/// Configuration for Mixture of Experts kernel generation.
///
/// Encapsulates all parameters needed to generate the four `MoE` phase kernels:
/// gating, permutation, grouped GEMM, and unpermutation. The `float_type`
/// determines whether `F32` or `F16` arithmetic is used throughout.
#[derive(Debug, Clone)]
pub struct MoETemplate {
    /// Number of experts in the `MoE` layer (e.g., 8, 16, 64).
    pub num_experts: u32,
    /// Number of experts selected per token (e.g., 1 or 2).
    pub top_k: u32,
    /// Hidden dimension of the model (input/output size per token).
    pub hidden_dim: u32,
    /// Expert feed-forward dimension (intermediate size).
    pub expert_dim: u32,
    /// Capacity factor controlling max tokens per expert (e.g., 1.25).
    /// The per-expert capacity is `ceil(capacity_factor * batch_size * top_k / num_experts)`.
    pub capacity_factor: f32,
    /// Target GPU architecture.
    pub sm_version: SmVersion,
    /// Floating-point type for computation (F32 or F16).
    pub float_type: PtxType,
}

impl MoETemplate {
    /// Returns a type suffix string for kernel naming (e.g., `"f32"`, `"f16"`).
    const fn type_suffix(&self) -> &'static str {
        match self.float_type {
            PtxType::F16 => "f16",
            PtxType::F64 => "f64",
            _ => "f32",
        }
    }

    /// Returns the PTX type string for this template's float type.
    const fn ty(&self) -> &'static str {
        self.float_type.as_ptx_str()
    }

    /// Returns the byte size of the float type.
    const fn byte_size(&self) -> usize {
        self.float_type.size_bytes()
    }

    /// Returns the zero literal for the configured float type.
    const fn zero_lit(&self) -> &'static str {
        match self.float_type {
            PtxType::F64 => "0d0000000000000000",
            _ => "0f00000000",
        }
    }

    /// Returns the negative infinity literal for the configured float type.
    const fn neg_inf(&self) -> &'static str {
        match self.float_type {
            PtxType::F64 => "0dFFF0000000000000",
            _ => "0fFF800000",
        }
    }

    /// Validates the template configuration.
    fn validate(&self) -> Result<(), PtxGenError> {
        if !matches!(self.float_type, PtxType::F16 | PtxType::F32 | PtxType::F64) {
            return Err(PtxGenError::InvalidType(format!(
                "MoE requires F16, F32, or F64, got {}",
                self.float_type.as_ptx_str()
            )));
        }
        if self.num_experts == 0 {
            return Err(PtxGenError::GenerationFailed(
                "num_experts must be > 0".to_string(),
            ));
        }
        if self.top_k == 0 || self.top_k > self.num_experts {
            return Err(PtxGenError::GenerationFailed(format!(
                "top_k must be in [1, num_experts={}], got {}",
                self.num_experts, self.top_k
            )));
        }
        if self.hidden_dim == 0 {
            return Err(PtxGenError::GenerationFailed(
                "hidden_dim must be > 0".to_string(),
            ));
        }
        if self.expert_dim == 0 {
            return Err(PtxGenError::GenerationFailed(
                "expert_dim must be > 0".to_string(),
            ));
        }
        if self.capacity_factor <= 0.0 || !self.capacity_factor.is_finite() {
            return Err(PtxGenError::GenerationFailed(format!(
                "capacity_factor must be a positive finite value, got {}",
                self.capacity_factor
            )));
        }
        Ok(())
    }

    /// Writes the standard PTX header (version, target, address size).
    fn write_header(&self, ptx: &mut String) -> Result<(), PtxGenError> {
        writeln!(ptx, ".version {}", self.sm_version.ptx_version())
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, ".target {}", self.sm_version.as_ptx_str())
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, ".address_size 64").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;
        Ok(())
    }

    // -----------------------------------------------------------------------
    // Phase 1: Top-k Gating Kernel
    // -----------------------------------------------------------------------

    /// Returns the kernel name for the top-k gating kernel.
    #[must_use]
    pub fn topk_gating_kernel_name(&self) -> String {
        format!(
            "topk_gating_{}_{}e_top{}",
            self.type_suffix(),
            self.num_experts,
            self.top_k
        )
    }

    /// Generates a PTX kernel for top-k expert selection with softmax gating.
    ///
    /// The kernel performs per-token softmax over expert logits, then selects
    /// the top-k experts with the highest probability. Gating weights are
    /// the normalized probabilities of the selected experts.
    ///
    /// **Parameters:**
    /// - `logits`: pointer to expert logits `[batch_size, num_experts]`
    /// - `expert_indices`: output pointer `[batch_size, top_k]` (u32)
    /// - `expert_weights`: output pointer `[batch_size, top_k]` (float)
    /// - `batch_size`: number of tokens
    ///
    /// # Errors
    ///
    /// Returns [`PtxGenError`] if validation fails or formatting fails.
    #[allow(clippy::too_many_lines)]
    pub fn generate_topk_gating(&self) -> Result<String, PtxGenError> {
        self.validate()?;

        let ty = self.ty();
        let byte_size = self.byte_size();
        let kernel_name = self.topk_gating_kernel_name();
        let num_experts = self.num_experts;
        let top_k = self.top_k;
        let neg_inf = self.neg_inf();
        let zero_lit = self.zero_lit();

        let mut ptx = String::with_capacity(8192);
        self.write_header(&mut ptx)?;

        // Kernel signature
        writeln!(ptx, ".visible .entry {kernel_name}(").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    .param .u64 %param_logits,").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    .param .u64 %param_expert_indices,")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    .param .u64 %param_expert_weights,")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    .param .u32 %param_batch_size").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, ")").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "{{").map_err(PtxGenError::FormatError)?;

        // Register declarations
        writeln!(ptx, "    .reg .b32 %r<48>;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    .reg .b64 %rd<24>;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    .reg .f32 %f<32>;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    .reg .pred %p<8>;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Thread indexing: one thread per token
        writeln!(ptx, "    // Compute global thread index (token index)")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mov.u32 %r0, %tid.x;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mov.u32 %r1, %ctaid.x;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mov.u32 %r2, %ntid.x;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mad.lo.u32 %r3, %r1, %r2, %r0;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Bounds check
        writeln!(ptx, "    ld.param.u32 %r4, [%param_batch_size];")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    setp.ge.u32 %p0, %r3, %r4;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    @%p0 bra $GATING_DONE;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Load base pointers
        writeln!(ptx, "    ld.param.u64 %rd0, [%param_logits];")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    ld.param.u64 %rd1, [%param_expert_indices];")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    ld.param.u64 %rd2, [%param_expert_weights];")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Compute logits base address for this token: logits + token_idx * num_experts * byte_size
        let row_bytes = (num_experts as usize) * byte_size;
        writeln!(ptx, "    // Compute logits row address for token %r3")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    cvt.u64.u32 %rd3, %r3;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mul.lo.u64 %rd3, %rd3, {row_bytes};")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u64 %rd4, %rd0, %rd3;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Pass 1: Find maximum logit for numerical stability
        writeln!(
            ptx,
            "    // Pass 1: Find max logit across {num_experts} experts"
        )
        .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mov{ty} %f0, {neg_inf};").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mov.u32 %r5, 0;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "$GATING_MAX_LOOP:").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    setp.ge.u32 %p1, %r5, {num_experts};")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    @%p1 bra $GATING_MAX_DONE;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    cvt.u64.u32 %rd5, %r5;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mul.lo.u64 %rd5, %rd5, {byte_size};")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u64 %rd6, %rd4, %rd5;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    ld.global{ty} %f1, [%rd6];").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    max{ty} %f0, %f0, %f1;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u32 %r5, %r5, 1;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    bra $GATING_MAX_LOOP;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "$GATING_MAX_DONE:").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Pass 2: Compute exp(logit - max) and sum for softmax denominator
        writeln!(ptx, "    // Pass 2: Compute exp(logit_i - max) and sum")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mov{ty} %f2, {zero_lit};").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mov.u32 %r5, 0;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "$GATING_EXP_LOOP:").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    setp.ge.u32 %p1, %r5, {num_experts};")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    @%p1 bra $GATING_EXP_DONE;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    cvt.u64.u32 %rd5, %r5;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mul.lo.u64 %rd5, %rd5, {byte_size};")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u64 %rd6, %rd4, %rd5;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    ld.global{ty} %f3, [%rd6];").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    sub{ty} %f3, %f3, %f0;").map_err(PtxGenError::FormatError)?;
        // Use ex2 approximation for exp: exp(x) = 2^(x * log2(e))
        // log2(e) ~= 1.4426950408889634
        let log2e = match self.float_type {
            PtxType::F64 => "0d3FF71547652B82FE",
            _ => "0f3FB8AA3B",
        };
        writeln!(ptx, "    mul{ty} %f4, %f3, {log2e};").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    ex2.approx{ty} %f3, %f4;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add{ty} %f2, %f2, %f3;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u32 %r5, %r5, 1;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    bra $GATING_EXP_LOOP;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "$GATING_EXP_DONE:").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Compute reciprocal of sum for normalization
        writeln!(ptx, "    // Compute 1/sum for normalization")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    rcp.approx{ty} %f5, %f2;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Pass 3: Top-k selection via iterative argmax
        // For each k in 0..top_k:
        //   - Find expert with highest softmax prob (excluding already selected)
        //   - Store expert index and normalized weight
        //   - Mark expert as used by setting its prob to -inf
        writeln!(ptx, "    // Pass 3: Top-k selection (iterative argmax)")
            .map_err(PtxGenError::FormatError)?;

        // Use r10 as the top-k iteration counter
        writeln!(ptx, "    mov.u32 %r10, 0;").map_err(PtxGenError::FormatError)?;

        // Track selected expert weight sum for renormalization
        writeln!(ptx, "    mov{ty} %f20, {zero_lit};").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        writeln!(ptx, "$TOPK_ITER_LOOP:").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    setp.ge.u32 %p2, %r10, {top_k};").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    @%p2 bra $TOPK_ITER_DONE;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Find argmax across experts
        writeln!(ptx, "    // Find argmax among {num_experts} experts")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mov{ty} %f6, {neg_inf};").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mov.u32 %r11, 0;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mov.u32 %r12, 0;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "$TOPK_ARGMAX_LOOP:").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    setp.ge.u32 %p3, %r12, {num_experts};")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    @%p3 bra $TOPK_ARGMAX_DONE;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    cvt.u64.u32 %rd7, %r12;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mul.lo.u64 %rd7, %rd7, {byte_size};")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u64 %rd8, %rd4, %rd7;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    ld.global{ty} %f7, [%rd8];").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    setp.gt{ty} %p4, %f7, %f6;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    @%p4 mov{ty} %f6, %f7;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    @%p4 mov.u32 %r11, %r12;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u32 %r12, %r12, 1;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    bra $TOPK_ARGMAX_LOOP;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "$TOPK_ARGMAX_DONE:").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Compute softmax probability for this expert: exp(logit - max) / sum
        writeln!(ptx, "    // Compute softmax prob for selected expert")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    sub{ty} %f8, %f6, %f0;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mul{ty} %f9, %f8, {log2e};").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    ex2.approx{ty} %f8, %f9;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mul{ty} %f8, %f8, %f5;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Accumulate weight sum for renormalization
        writeln!(ptx, "    add{ty} %f20, %f20, %f8;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Store expert index: expert_indices[token * top_k + k]
        let out_idx_elem_bytes = 4_usize; // u32
        let out_weight_elem_bytes = byte_size;
        writeln!(ptx, "    // Store expert index and weight").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    cvt.u64.u32 %rd9, %r3;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mul.lo.u64 %rd9, %rd9, {top_k};").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    cvt.u64.u32 %rd10, %r10;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u64 %rd9, %rd9, %rd10;").map_err(PtxGenError::FormatError)?;

        // Store expert index
        writeln!(ptx, "    mul.lo.u64 %rd11, %rd9, {out_idx_elem_bytes};")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u64 %rd12, %rd1, %rd11;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    st.global.u32 [%rd12], %r11;").map_err(PtxGenError::FormatError)?;

        // Store expert weight (will be renormalized in a final pass)
        writeln!(ptx, "    mul.lo.u64 %rd13, %rd9, {out_weight_elem_bytes};")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u64 %rd14, %rd2, %rd13;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    st.global{ty} [%rd14], %f8;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Mark this expert as used: store -inf at its logit position
        writeln!(
            ptx,
            "    // Mark selected expert as used (set logit to -inf)"
        )
        .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    cvt.u64.u32 %rd15, %r11;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mul.lo.u64 %rd15, %rd15, {byte_size};")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u64 %rd16, %rd4, %rd15;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    st.global{ty} [%rd16], {neg_inf};").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Advance top-k counter
        writeln!(ptx, "    add.u32 %r10, %r10, 1;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    bra $TOPK_ITER_LOOP;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "$TOPK_ITER_DONE:").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Pass 4: Renormalize weights so they sum to 1
        writeln!(ptx, "    // Pass 4: Renormalize top-k weights to sum to 1")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    rcp.approx{ty} %f21, %f20;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mov.u32 %r13, 0;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "$RENORM_LOOP:").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    setp.ge.u32 %p5, %r13, {top_k};").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    @%p5 bra $RENORM_DONE;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    cvt.u64.u32 %rd17, %r3;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mul.lo.u64 %rd17, %rd17, {top_k};").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    cvt.u64.u32 %rd18, %r13;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u64 %rd17, %rd17, %rd18;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mul.lo.u64 %rd19, %rd17, {out_weight_elem_bytes};")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u64 %rd20, %rd2, %rd19;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    ld.global{ty} %f22, [%rd20];").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mul{ty} %f22, %f22, %f21;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    st.global{ty} [%rd20], %f22;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u32 %r13, %r13, 1;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    bra $RENORM_LOOP;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "$RENORM_DONE:").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        writeln!(ptx, "$GATING_DONE:").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    ret;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "}}").map_err(PtxGenError::FormatError)?;

        Ok(ptx)
    }

    // -----------------------------------------------------------------------
    // Phase 2: Permute Kernel
    // -----------------------------------------------------------------------

    /// Returns the kernel name for the permute kernel.
    #[must_use]
    pub fn permute_kernel_name(&self) -> String {
        format!(
            "moe_permute_{}_{}e_top{}",
            self.type_suffix(),
            self.num_experts,
            self.top_k
        )
    }

    /// Generates a PTX kernel for token permutation into expert bins.
    ///
    /// Each token is scattered to its assigned expert's bin. Per-expert token
    /// counts are tracked atomically, and a capacity limit is enforced so that
    /// no expert receives more than `capacity` tokens.
    ///
    /// **Parameters:**
    /// - `expert_indices`: input `[batch_size, top_k]` (u32)
    /// - `token_ids`: output `[num_experts, capacity]` mapping expert slot to source token (u32)
    /// - `expert_counts`: output `[num_experts]` atomic counters (u32)
    /// - `batch_size`: number of tokens
    /// - `capacity`: max tokens per expert
    ///
    /// # Errors
    ///
    /// Returns [`PtxGenError`] if validation fails or formatting fails.
    #[allow(clippy::too_many_lines)]
    pub fn generate_permute(&self) -> Result<String, PtxGenError> {
        self.validate()?;

        let kernel_name = self.permute_kernel_name();
        let num_experts = self.num_experts;
        let top_k = self.top_k;

        let mut ptx = String::with_capacity(4096);
        self.write_header(&mut ptx)?;

        writeln!(ptx, ".visible .entry {kernel_name}(").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    .param .u64 %param_expert_indices,")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    .param .u64 %param_token_ids,").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    .param .u64 %param_expert_counts,").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    .param .u32 %param_batch_size,").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    .param .u32 %param_capacity").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, ")").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "{{").map_err(PtxGenError::FormatError)?;

        writeln!(ptx, "    .reg .b32 %r<32>;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    .reg .b64 %rd<16>;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    .reg .pred %p<8>;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Thread index = token index
        writeln!(ptx, "    // Global thread index = token index")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mov.u32 %r0, %tid.x;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mov.u32 %r1, %ctaid.x;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mov.u32 %r2, %ntid.x;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mad.lo.u32 %r3, %r1, %r2, %r0;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Bounds check
        writeln!(ptx, "    ld.param.u32 %r4, [%param_batch_size];")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    setp.ge.u32 %p0, %r3, %r4;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    @%p0 bra $PERMUTE_DONE;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Load base pointers and capacity
        writeln!(ptx, "    ld.param.u64 %rd0, [%param_expert_indices];")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    ld.param.u64 %rd1, [%param_token_ids];")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    ld.param.u64 %rd2, [%param_expert_counts];")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    ld.param.u32 %r5, [%param_capacity];")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // For each of the top_k expert assignments for this token
        writeln!(ptx, "    // Process {top_k} expert assignment(s) per token")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mov.u32 %r6, 0;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "$PERMUTE_K_LOOP:").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    setp.ge.u32 %p1, %r6, {top_k};").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    @%p1 bra $PERMUTE_DONE;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Load expert index: expert_indices[token * top_k + k]
        writeln!(ptx, "    // Load expert index for (token, k)")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mad.lo.u32 %r7, %r3, {top_k}, %r6;")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    cvt.u64.u32 %rd3, %r7;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mul.lo.u64 %rd3, %rd3, 4;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u64 %rd4, %rd0, %rd3;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    ld.global.u32 %r8, [%rd4];").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Validate expert index
        writeln!(ptx, "    setp.ge.u32 %p2, %r8, {num_experts};")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    @%p2 bra $PERMUTE_K_NEXT;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Atomically increment expert count: slot = atomicAdd(&expert_counts[expert], 1)
        writeln!(ptx, "    // Atomic increment of expert count")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    cvt.u64.u32 %rd5, %r8;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mul.lo.u64 %rd5, %rd5, 4;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u64 %rd6, %rd2, %rd5;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    atom.global.add.u32 %r9, [%rd6], 1;")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Capacity check: if slot >= capacity, skip (token dropped)
        writeln!(ptx, "    // Enforce capacity limit").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    setp.ge.u32 %p3, %r9, %r5;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    @%p3 bra $PERMUTE_K_NEXT;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Store token id: token_ids[expert * capacity + slot] = token_index
        writeln!(ptx, "    // Store token index into expert bin")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mad.lo.u32 %r10, %r8, %r5, %r9;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    cvt.u64.u32 %rd7, %r10;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mul.lo.u64 %rd7, %rd7, 4;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u64 %rd8, %rd1, %rd7;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    st.global.u32 [%rd8], %r3;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        writeln!(ptx, "$PERMUTE_K_NEXT:").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u32 %r6, %r6, 1;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    bra $PERMUTE_K_LOOP;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        writeln!(ptx, "$PERMUTE_DONE:").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    ret;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "}}").map_err(PtxGenError::FormatError)?;

        Ok(ptx)
    }

    // -----------------------------------------------------------------------
    // Phase 3: Expert GEMM Kernel
    // -----------------------------------------------------------------------

    /// Returns the kernel name for the expert GEMM kernel.
    #[must_use]
    pub fn expert_gemm_kernel_name(&self) -> String {
        format!(
            "moe_expert_gemm_{}_{}e_{}x{}",
            self.type_suffix(),
            self.num_experts,
            self.hidden_dim,
            self.expert_dim
        )
    }

    /// Generates a PTX kernel for grouped GEMM across experts.
    ///
    /// Each expert has its own weight matrix `W[expert, expert_dim, hidden_dim]`.
    /// For each expert, the kernel multiplies the expert's assigned tokens by
    /// its weight matrix. Shared memory tiling is used for efficiency.
    ///
    /// **Parameters:**
    /// - `input`: pointer to token embeddings `[total_tokens, hidden_dim]`
    /// - `weights`: pointer to expert weights `[num_experts, expert_dim, hidden_dim]`
    /// - `output`: pointer to results `[total_tokens, expert_dim]`
    /// - `token_ids`: permuted token indices `[num_experts, capacity]` (u32)
    /// - `expert_counts`: actual tokens per expert `[num_experts]` (u32)
    /// - `capacity`: max tokens per expert
    ///
    /// # Errors
    ///
    /// Returns [`PtxGenError`] if validation fails or formatting fails.
    #[allow(clippy::too_many_lines)]
    pub fn generate_expert_gemm(&self) -> Result<String, PtxGenError> {
        self.validate()?;

        let ty = self.ty();
        let byte_size = self.byte_size();
        let kernel_name = self.expert_gemm_kernel_name();
        let num_experts = self.num_experts;
        let hidden_dim = self.hidden_dim;
        let expert_dim = self.expert_dim;
        let zero_lit = self.zero_lit();

        // Tile size for shared memory tiling
        let tile_size: u32 = 16;
        let tile_bytes = (tile_size as usize) * (tile_size as usize) * byte_size;
        let total_smem = tile_bytes * 2; // Two tiles: A tile and B tile

        let mut ptx = String::with_capacity(8192);
        self.write_header(&mut ptx)?;

        writeln!(ptx, ".visible .entry {kernel_name}(").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    .param .u64 %param_input,").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    .param .u64 %param_weights,").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    .param .u64 %param_output,").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    .param .u64 %param_token_ids,").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    .param .u64 %param_expert_counts,").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    .param .u32 %param_capacity").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, ")").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "{{").map_err(PtxGenError::FormatError)?;

        // Register and shared memory declarations
        writeln!(ptx, "    .reg .b32 %r<48>;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    .reg .b64 %rd<32>;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    .reg .f32 %f<24>;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    .reg .pred %p<8>;").map_err(PtxGenError::FormatError)?;
        writeln!(
            ptx,
            "    .shared .align {} .b8 smem_gemm[{}];",
            byte_size.max(4),
            total_smem
        )
        .map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Thread/block layout:
        // blockIdx.z = expert_id
        // blockIdx.y = tile row in expert_dim
        // blockIdx.x = token tile within this expert's assigned tokens
        // threadIdx.x = column within tile, threadIdx.y = row within tile
        writeln!(ptx, "    // Thread and block indexing").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mov.u32 %r0, %tid.x;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mov.u32 %r1, %tid.y;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mov.u32 %r2, %ctaid.x;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mov.u32 %r3, %ctaid.y;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mov.u32 %r4, %ctaid.z;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Validate expert_id < num_experts
        writeln!(ptx, "    setp.ge.u32 %p0, %r4, {num_experts};")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    @%p0 bra $GEMM_DONE;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Load parameters
        writeln!(ptx, "    ld.param.u64 %rd0, [%param_input];")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    ld.param.u64 %rd1, [%param_weights];")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    ld.param.u64 %rd2, [%param_output];")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    ld.param.u64 %rd3, [%param_token_ids];")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    ld.param.u64 %rd4, [%param_expert_counts];")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    ld.param.u32 %r5, [%param_capacity];")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Load expert token count: expert_counts[expert_id]
        writeln!(ptx, "    // Load number of tokens assigned to this expert")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    cvt.u64.u32 %rd5, %r4;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mul.lo.u64 %rd5, %rd5, 4;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u64 %rd6, %rd4, %rd5;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    ld.global.u32 %r6, [%rd6];").map_err(PtxGenError::FormatError)?;
        // Clamp to capacity
        writeln!(ptx, "    min.u32 %r6, %r6, %r5;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Compute global row/col this thread is responsible for
        // token_tile_idx = blockIdx.x * tile_size + threadIdx.y => local token index
        // expert_dim_idx = blockIdx.y * tile_size + threadIdx.x => expert_dim column
        writeln!(ptx, "    // Compute output coordinates").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mad.lo.u32 %r7, %r2, {tile_size}, %r1;")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mad.lo.u32 %r8, %r3, {tile_size}, %r0;")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Bounds check: local token index < expert_count, expert_dim_idx < expert_dim
        writeln!(ptx, "    setp.ge.u32 %p1, %r7, %r6;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    setp.ge.u32 %p2, %r8, {expert_dim};")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    or.pred %p3, %p1, %p2;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Resolve actual token index from token_ids: token_ids[expert * capacity + local_idx]
        writeln!(
            ptx,
            "    // Resolve global token index from permutation table"
        )
        .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mad.lo.u32 %r9, %r4, %r5, %r7;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    cvt.u64.u32 %rd7, %r9;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mul.lo.u64 %rd7, %rd7, 4;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u64 %rd8, %rd3, %rd7;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mov.u32 %r10, 0;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    @!%p1 ld.global.u32 %r10, [%rd8];").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Shared memory base pointers
        writeln!(ptx, "    mov.u64 %rd9, smem_gemm;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u64 %rd10, %rd9, {tile_bytes};")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Initialize accumulator
        writeln!(ptx, "    mov{ty} %f0, {zero_lit};").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Tiled GEMM: iterate over hidden_dim in chunks of tile_size
        // For each tile: load A[token, hidden_chunk] and B[expert, expert_dim_idx, hidden_chunk]
        let hidden_dim_val = hidden_dim;
        writeln!(
            ptx,
            "    // Tiled reduction over hidden_dim ({hidden_dim_val})"
        )
        .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mov.u32 %r11, 0;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "$GEMM_K_LOOP:").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    setp.ge.u32 %p4, %r11, {hidden_dim_val};")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    @%p4 bra $GEMM_K_DONE;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Load A tile element: input[global_token_id, k_offset + threadIdx.x]
        let hidden_bytes = (hidden_dim as usize) * byte_size;
        writeln!(ptx, "    // Load input element for tiled multiply")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u32 %r12, %r11, %r0;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    setp.lt.u32 %p5, %r12, {hidden_dim_val};")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mov{ty} %f1, {zero_lit};").map_err(PtxGenError::FormatError)?;
        // Compute address: input + global_token_id * hidden_dim * byte_size + (k + tid.x) * byte_size
        writeln!(ptx, "    cvt.u64.u32 %rd11, %r10;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mul.lo.u64 %rd11, %rd11, {hidden_bytes};")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u64 %rd12, %rd0, %rd11;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    cvt.u64.u32 %rd13, %r12;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mul.lo.u64 %rd13, %rd13, {byte_size};")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u64 %rd14, %rd12, %rd13;").map_err(PtxGenError::FormatError)?;
        // Conditional load (only if within bounds and token is valid)
        writeln!(ptx, "    and.pred %p6, %p5, !%p3;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    @%p6 ld.global{ty} %f1, [%rd14];").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Store to shared memory A tile: smem_A[tid.y * tile + tid.x]
        writeln!(ptx, "    // Store to shared memory tile A").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mad.lo.u32 %r13, %r1, {tile_size}, %r0;")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    cvt.u64.u32 %rd15, %r13;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mul.lo.u64 %rd15, %rd15, {byte_size};")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u64 %rd16, %rd9, %rd15;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    st.shared{ty} [%rd16], %f1;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Load B tile element: weights[expert * expert_dim * hidden_dim + expert_dim_idx * hidden_dim + k_offset + tid.y]
        let expert_weight_size = (expert_dim as usize) * (hidden_dim as usize) * byte_size;
        writeln!(ptx, "    // Load weight element for tiled multiply")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u32 %r14, %r11, %r1;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    setp.lt.u32 %p5, %r14, {hidden_dim_val};")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mov{ty} %f2, {zero_lit};").map_err(PtxGenError::FormatError)?;
        // Compute address: weights + expert_id * expert_dim * hidden_dim * byte_size
        //                          + expert_dim_idx * hidden_dim * byte_size
        //                          + (k_offset + tid.y) * byte_size
        writeln!(ptx, "    cvt.u64.u32 %rd17, %r4;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mul.lo.u64 %rd17, %rd17, {expert_weight_size};")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u64 %rd18, %rd1, %rd17;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    cvt.u64.u32 %rd19, %r8;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mul.lo.u64 %rd19, %rd19, {hidden_bytes};")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u64 %rd20, %rd18, %rd19;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    cvt.u64.u32 %rd21, %r14;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mul.lo.u64 %rd21, %rd21, {byte_size};")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u64 %rd22, %rd20, %rd21;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    and.pred %p7, %p5, !%p2;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    @%p7 ld.global{ty} %f2, [%rd22];").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Store to shared memory B tile: smem_B[tid.y * tile + tid.x]
        writeln!(ptx, "    // Store to shared memory tile B").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mad.lo.u32 %r15, %r1, {tile_size}, %r0;")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    cvt.u64.u32 %rd23, %r15;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mul.lo.u64 %rd23, %rd23, {byte_size};")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u64 %rd24, %rd10, %rd23;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    st.shared{ty} [%rd24], %f2;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Synchronize
        writeln!(ptx, "    bar.sync 0;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Dot product over tile: accumulate A[tid.y, t] * B[t, tid.x] for t in 0..tile_size
        writeln!(ptx, "    // Inner product over shared memory tile")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mov.u32 %r16, 0;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "$GEMM_TILE_DOT:").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    setp.ge.u32 %p4, %r16, {tile_size};")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    @%p4 bra $GEMM_TILE_DOT_DONE;").map_err(PtxGenError::FormatError)?;

        // Load A[tid.y, t] from smem
        writeln!(ptx, "    mad.lo.u32 %r17, %r1, {tile_size}, %r16;")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    cvt.u64.u32 %rd25, %r17;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mul.lo.u64 %rd25, %rd25, {byte_size};")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u64 %rd26, %rd9, %rd25;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    ld.shared{ty} %f3, [%rd26];").map_err(PtxGenError::FormatError)?;

        // Load B[t, tid.x] from smem
        writeln!(ptx, "    mad.lo.u32 %r18, %r16, {tile_size}, %r0;")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    cvt.u64.u32 %rd27, %r18;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mul.lo.u64 %rd27, %rd27, {byte_size};")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u64 %rd28, %rd10, %rd27;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    ld.shared{ty} %f4, [%rd28];").map_err(PtxGenError::FormatError)?;

        // FMA: acc += a * b
        writeln!(ptx, "    fma.rn{ty} %f0, %f3, %f4, %f0;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u32 %r16, %r16, 1;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    bra $GEMM_TILE_DOT;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "$GEMM_TILE_DOT_DONE:").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Sync before next tile
        writeln!(ptx, "    bar.sync 0;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u32 %r11, %r11, {tile_size};").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    bra $GEMM_K_LOOP;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "$GEMM_K_DONE:").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Write result if within bounds
        // output[global_token_id * expert_dim + expert_dim_idx]
        let expert_dim_bytes = (expert_dim as usize) * byte_size;
        writeln!(ptx, "    // Write result to output").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    @%p3 bra $GEMM_DONE;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    cvt.u64.u32 %rd29, %r10;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mul.lo.u64 %rd29, %rd29, {expert_dim_bytes};")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u64 %rd30, %rd2, %rd29;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    cvt.u64.u32 %rd31, %r8;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mul.lo.u64 %rd31, %rd31, {byte_size};")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u64 %rd30, %rd30, %rd31;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    st.global{ty} [%rd30], %f0;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        writeln!(ptx, "$GEMM_DONE:").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    ret;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "}}").map_err(PtxGenError::FormatError)?;

        Ok(ptx)
    }

    // -----------------------------------------------------------------------
    // Phase 4: Unpermute Kernel
    // -----------------------------------------------------------------------

    /// Returns the kernel name for the unpermute kernel.
    #[must_use]
    pub fn unpermute_kernel_name(&self) -> String {
        format!(
            "moe_unpermute_{}_{}e_top{}",
            self.type_suffix(),
            self.num_experts,
            self.top_k
        )
    }

    /// Generates a PTX kernel for result unpermutation with gating weight application.
    ///
    /// Gathers expert outputs back to original token order. For each token, the
    /// outputs from its top-k experts are weighted by the corresponding gating
    /// weights and accumulated to produce the final output.
    ///
    /// **Parameters:**
    /// - `expert_output`: input `[total_tokens, expert_dim]` (expert results)
    /// - `output`: final output `[batch_size, expert_dim]`
    /// - `expert_indices`: `[batch_size, top_k]` (u32)
    /// - `expert_weights`: `[batch_size, top_k]` (float)
    /// - `token_ids`: permuted mapping `[num_experts, capacity]` (u32)
    /// - `expert_counts`: `[num_experts]` (u32)
    /// - `batch_size`: number of tokens
    /// - `capacity`: max tokens per expert
    ///
    /// # Errors
    ///
    /// Returns [`PtxGenError`] if validation fails or formatting fails.
    #[allow(clippy::too_many_lines)]
    pub fn generate_unpermute(&self) -> Result<String, PtxGenError> {
        self.validate()?;

        let ty = self.ty();
        let byte_size = self.byte_size();
        let kernel_name = self.unpermute_kernel_name();
        let num_experts = self.num_experts;
        let top_k = self.top_k;
        let expert_dim = self.expert_dim;
        let zero_lit = self.zero_lit();

        let mut ptx = String::with_capacity(8192);
        self.write_header(&mut ptx)?;

        writeln!(ptx, ".visible .entry {kernel_name}(").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    .param .u64 %param_expert_output,").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    .param .u64 %param_output,").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    .param .u64 %param_expert_indices,")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    .param .u64 %param_expert_weights,")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    .param .u64 %param_token_ids,").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    .param .u64 %param_expert_counts,").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    .param .u32 %param_batch_size,").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    .param .u32 %param_capacity").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, ")").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "{{").map_err(PtxGenError::FormatError)?;

        writeln!(ptx, "    .reg .b32 %r<48>;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    .reg .b64 %rd<24>;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    .reg .f32 %f<16>;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    .reg .pred %p<8>;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Thread layout: each thread handles one (token, dim_element) pair
        // blockIdx.x * blockDim.x + threadIdx.x = global_idx
        // token_idx = global_idx / expert_dim
        // dim_idx = global_idx % expert_dim
        writeln!(ptx, "    // Compute token and dimension indices")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mov.u32 %r0, %tid.x;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mov.u32 %r1, %ctaid.x;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mov.u32 %r2, %ntid.x;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mad.lo.u32 %r3, %r1, %r2, %r0;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // token_idx = r3 / expert_dim, dim_idx = r3 % expert_dim
        writeln!(ptx, "    div.u32 %r4, %r3, {expert_dim};").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    rem.u32 %r5, %r3, {expert_dim};").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Bounds check
        writeln!(ptx, "    ld.param.u32 %r6, [%param_batch_size];")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    setp.ge.u32 %p0, %r4, %r6;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    @%p0 bra $UNPERM_DONE;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Load parameters
        writeln!(ptx, "    ld.param.u64 %rd0, [%param_expert_output];")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    ld.param.u64 %rd1, [%param_output];")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    ld.param.u64 %rd2, [%param_expert_indices];")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    ld.param.u64 %rd3, [%param_expert_weights];")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    ld.param.u64 %rd4, [%param_token_ids];")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    ld.param.u64 %rd5, [%param_expert_counts];")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    ld.param.u32 %r7, [%param_capacity];")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Initialize accumulator for this output element
        writeln!(ptx, "    mov{ty} %f0, {zero_lit};").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Loop over top_k experts for this token
        writeln!(
            ptx,
            "    // Accumulate weighted outputs from {top_k} expert(s)"
        )
        .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mov.u32 %r8, 0;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "$UNPERM_K_LOOP:").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    setp.ge.u32 %p1, %r8, {top_k};").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    @%p1 bra $UNPERM_K_DONE;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Load expert_index and expert_weight for (token, k)
        let weight_elem_bytes = byte_size;
        writeln!(ptx, "    // Load expert index and weight for (token, k)")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mad.lo.u32 %r9, %r4, {top_k}, %r8;")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    cvt.u64.u32 %rd6, %r9;").map_err(PtxGenError::FormatError)?;
        // Load expert index
        writeln!(ptx, "    mul.lo.u64 %rd7, %rd6, 4;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u64 %rd8, %rd2, %rd7;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    ld.global.u32 %r10, [%rd8];").map_err(PtxGenError::FormatError)?;
        // Load gating weight
        writeln!(ptx, "    mul.lo.u64 %rd9, %rd6, {weight_elem_bytes};")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u64 %rd10, %rd3, %rd9;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    ld.global{ty} %f1, [%rd10];").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Validate expert index
        writeln!(ptx, "    setp.ge.u32 %p2, %r10, {num_experts};")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    @%p2 bra $UNPERM_K_NEXT;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Find this token's position in the expert's bin by scanning token_ids
        // We need to find slot s such that token_ids[expert * capacity + s] == token_idx
        // and s < expert_counts[expert]
        writeln!(ptx, "    // Load expert count and search for token slot")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    cvt.u64.u32 %rd11, %r10;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mul.lo.u64 %rd11, %rd11, 4;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u64 %rd12, %rd5, %rd11;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    ld.global.u32 %r11, [%rd12];").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    min.u32 %r11, %r11, %r7;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Linear scan for token position within expert's bin
        writeln!(ptx, "    // Linear scan for token in expert bin")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mov.u32 %r12, 0;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mov.u32 %r13, 0xFFFFFFFF;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "$UNPERM_SCAN_LOOP:").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    setp.ge.u32 %p3, %r12, %r11;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    @%p3 bra $UNPERM_SCAN_DONE;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mad.lo.u32 %r14, %r10, %r7, %r12;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    cvt.u64.u32 %rd13, %r14;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mul.lo.u64 %rd13, %rd13, 4;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u64 %rd14, %rd4, %rd13;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    ld.global.u32 %r15, [%rd14];").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    setp.eq.u32 %p4, %r15, %r4;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    @%p4 mov.u32 %r13, %r12;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    @%p4 bra $UNPERM_SCAN_DONE;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u32 %r12, %r12, 1;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    bra $UNPERM_SCAN_LOOP;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "$UNPERM_SCAN_DONE:").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // If token not found (r13 == 0xFFFFFFFF), skip
        writeln!(ptx, "    setp.eq.u32 %p5, %r13, 0xFFFFFFFF;")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    @%p5 bra $UNPERM_K_NEXT;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Load expert output: expert_output[global_token_slot * expert_dim + dim_idx]
        // global_token_slot is the position in the flattened expert output
        // which is stored at index (expert * capacity + slot) in the permuted order
        let expert_dim_bytes = (expert_dim as usize) * byte_size;
        writeln!(ptx, "    // Load expert output and apply gating weight")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mad.lo.u32 %r16, %r10, %r7, %r13;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    cvt.u64.u32 %rd15, %r16;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mul.lo.u64 %rd15, %rd15, {expert_dim_bytes};")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u64 %rd16, %rd0, %rd15;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    cvt.u64.u32 %rd17, %r5;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mul.lo.u64 %rd17, %rd17, {byte_size};")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u64 %rd18, %rd16, %rd17;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    ld.global{ty} %f2, [%rd18];").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Apply gating weight: acc += weight * expert_output
        writeln!(ptx, "    fma.rn{ty} %f0, %f1, %f2, %f0;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        writeln!(ptx, "$UNPERM_K_NEXT:").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u32 %r8, %r8, 1;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    bra $UNPERM_K_LOOP;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "$UNPERM_K_DONE:").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        // Store final accumulated output: output[token_idx * expert_dim + dim_idx]
        writeln!(ptx, "    // Store weighted sum to output").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    cvt.u64.u32 %rd19, %r4;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mul.lo.u64 %rd19, %rd19, {expert_dim_bytes};")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u64 %rd20, %rd1, %rd19;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    cvt.u64.u32 %rd21, %r5;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    mul.lo.u64 %rd21, %rd21, {byte_size};")
            .map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    add.u64 %rd22, %rd20, %rd21;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    st.global{ty} [%rd22], %f0;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx).map_err(PtxGenError::FormatError)?;

        writeln!(ptx, "$UNPERM_DONE:").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "    ret;").map_err(PtxGenError::FormatError)?;
        writeln!(ptx, "}}").map_err(PtxGenError::FormatError)?;

        Ok(ptx)
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::arch::SmVersion;
    use crate::ir::PtxType;

    fn default_template() -> MoETemplate {
        MoETemplate {
            num_experts: 8,
            top_k: 2,
            hidden_dim: 4096,
            expert_dim: 14336,
            capacity_factor: 1.25,
            sm_version: SmVersion::Sm80,
            float_type: PtxType::F32,
        }
    }

    // -- Kernel name tests --

    #[test]
    fn topk_gating_kernel_name() {
        let t = default_template();
        assert_eq!(t.topk_gating_kernel_name(), "topk_gating_f32_8e_top2");
    }

    #[test]
    fn permute_kernel_name() {
        let t = default_template();
        assert_eq!(t.permute_kernel_name(), "moe_permute_f32_8e_top2");
    }

    #[test]
    fn expert_gemm_kernel_name() {
        let t = default_template();
        assert_eq!(
            t.expert_gemm_kernel_name(),
            "moe_expert_gemm_f32_8e_4096x14336"
        );
    }

    #[test]
    fn unpermute_kernel_name() {
        let t = default_template();
        assert_eq!(t.unpermute_kernel_name(), "moe_unpermute_f32_8e_top2");
    }

    // -- Gating kernel tests --

    #[test]
    fn generate_topk_gating_f32() {
        let t = default_template();
        let ptx = t
            .generate_topk_gating()
            .expect("should generate gating kernel");
        assert!(ptx.contains(".entry topk_gating_f32_8e_top2"));
        assert!(ptx.contains("ex2.approx"));
        assert!(ptx.contains("rcp.approx"));
        assert!(ptx.contains("max.f32"));
        assert!(ptx.contains("st.global.u32"));
        assert!(ptx.contains("st.global.f32"));
        assert!(ptx.contains(".version 7.0"));
        assert!(ptx.contains(".target sm_80"));
    }

    #[test]
    fn generate_topk_gating_f16() {
        let t = MoETemplate {
            float_type: PtxType::F16,
            ..default_template()
        };
        let ptx = t
            .generate_topk_gating()
            .expect("should generate f16 gating");
        assert!(ptx.contains("topk_gating_f16_8e_top2"));
        assert!(ptx.contains(".f16"));
    }

    #[test]
    fn generate_topk_gating_top1() {
        let t = MoETemplate {
            top_k: 1,
            ..default_template()
        };
        let ptx = t
            .generate_topk_gating()
            .expect("should generate top-1 gating");
        assert!(ptx.contains("topk_gating_f32_8e_top1"));
    }

    // -- Permute kernel tests --

    #[test]
    fn generate_permute_f32() {
        let t = default_template();
        let ptx = t
            .generate_permute()
            .expect("should generate permute kernel");
        assert!(ptx.contains(".entry moe_permute_f32_8e_top2"));
        assert!(ptx.contains("atom.global.add.u32"));
        assert!(ptx.contains("setp.ge.u32")); // capacity check
        assert!(ptx.contains("st.global.u32"));
    }

    #[test]
    fn generate_permute_capacity_enforcement() {
        let t = default_template();
        let ptx = t
            .generate_permute()
            .expect("should generate permute kernel");
        // Verify capacity check: slot >= capacity leads to skip
        assert!(ptx.contains("@%p3 bra $PERMUTE_K_NEXT"));
        // Verify atomic increment for expert count
        assert!(ptx.contains("atom.global.add.u32"));
    }

    // -- Expert GEMM kernel tests --

    #[test]
    fn generate_expert_gemm_f32() {
        let t = default_template();
        let ptx = t
            .generate_expert_gemm()
            .expect("should generate expert gemm kernel");
        assert!(ptx.contains(".entry moe_expert_gemm_f32_8e_4096x14336"));
        assert!(ptx.contains(".shared"));
        assert!(ptx.contains("bar.sync 0"));
        assert!(ptx.contains("fma.rn.f32"));
        assert!(ptx.contains("ld.shared.f32"));
        assert!(ptx.contains("st.shared.f32"));
    }

    #[test]
    fn generate_expert_gemm_uses_tiling() {
        let t = default_template();
        let ptx = t
            .generate_expert_gemm()
            .expect("should generate expert gemm");
        // Verify tiled loop structure
        assert!(ptx.contains("$GEMM_K_LOOP"));
        assert!(ptx.contains("$GEMM_K_DONE"));
        assert!(ptx.contains("$GEMM_TILE_DOT"));
    }

    // -- Unpermute kernel tests --

    #[test]
    fn generate_unpermute_f32() {
        let t = default_template();
        let ptx = t
            .generate_unpermute()
            .expect("should generate unpermute kernel");
        assert!(ptx.contains(".entry moe_unpermute_f32_8e_top2"));
        assert!(ptx.contains("fma.rn.f32")); // gating weight application
        assert!(ptx.contains("$UNPERM_K_LOOP")); // top-k accumulation loop
        assert!(ptx.contains("st.global.f32")); // final output store
    }

    #[test]
    fn generate_unpermute_gating_weights() {
        let t = default_template();
        let ptx = t.generate_unpermute().expect("should generate unpermute");
        // Verify gating weight is loaded and applied via FMA
        assert!(ptx.contains("ld.global.f32 %f1"));
        assert!(ptx.contains("fma.rn.f32 %f0, %f1, %f2, %f0"));
    }

    // -- Validation tests --

    #[test]
    fn invalid_float_type() {
        let t = MoETemplate {
            float_type: PtxType::U32,
            ..default_template()
        };
        assert!(t.generate_topk_gating().is_err());
        assert!(t.generate_permute().is_err());
        assert!(t.generate_expert_gemm().is_err());
        assert!(t.generate_unpermute().is_err());
    }

    #[test]
    fn invalid_num_experts_zero() {
        let t = MoETemplate {
            num_experts: 0,
            ..default_template()
        };
        assert!(t.generate_topk_gating().is_err());
    }

    #[test]
    fn invalid_top_k_exceeds_experts() {
        let t = MoETemplate {
            top_k: 16,
            num_experts: 8,
            ..default_template()
        };
        assert!(t.generate_topk_gating().is_err());
    }

    #[test]
    fn invalid_hidden_dim_zero() {
        let t = MoETemplate {
            hidden_dim: 0,
            ..default_template()
        };
        assert!(t.generate_expert_gemm().is_err());
    }

    #[test]
    fn invalid_capacity_factor() {
        let t = MoETemplate {
            capacity_factor: -1.0,
            ..default_template()
        };
        assert!(t.generate_permute().is_err());

        let t2 = MoETemplate {
            capacity_factor: f32::INFINITY,
            ..default_template()
        };
        assert!(t2.generate_permute().is_err());
    }

    // -- Architecture variation tests --

    #[test]
    fn generate_for_sm90() {
        let t = MoETemplate {
            sm_version: SmVersion::Sm90,
            ..default_template()
        };
        let ptx = t
            .generate_topk_gating()
            .expect("should generate for Hopper");
        assert!(ptx.contains(".target sm_90"));
        assert!(ptx.contains(".version 8.0"));
    }

    #[test]
    fn generate_16_experts_top1() {
        let t = MoETemplate {
            num_experts: 16,
            top_k: 1,
            ..default_template()
        };
        let ptx = t.generate_topk_gating().expect("should generate 16e top1");
        assert!(ptx.contains("topk_gating_f32_16e_top1"));
    }
}