oxicuda-dnn 0.1.1

OxiCUDA DNN - GPU-accelerated deep learning primitives (cuDNN equivalent)
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
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
//! GPTQ and AWQ weight-only quantization for LLM inference.
//!
//! **GPTQ** (Optimal Brain Quantizer) uses second-order Hessian information
//! to quantize weights column-block by column-block, minimizing the squared
//! reconstruction error via the inverse Hessian diagonal. It supports 2, 3,
//! 4, and 8-bit quantization with optional activation reordering.
//!
//! **AWQ** (Activation-Aware Weight Quantization) identifies salient weight
//! channels from activation statistics and applies per-channel scaling before
//! group quantization, preserving important channels at higher fidelity.
//!
//! Both methods produce packed integer weights with per-group scales (and
//! optional zero points), suitable for weight-only dequantize + GEMV fusion
//! during inference.

use oxicuda_ptx::arch::SmVersion;
use oxicuda_ptx::ir::PtxType;
use oxicuda_ptx::prelude::*;

use crate::error::DnnError;

// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------

/// Default thread block size for GPTQ/AWQ kernels.
const QUANT_BLOCK_SIZE: u32 = 256;

/// Minimum Hessian damping to avoid division by zero.
const MIN_DAMP: f32 = 1e-6;

// ---------------------------------------------------------------------------
// Quantization method selection
// ---------------------------------------------------------------------------

/// Weight-only quantization method.
#[derive(Debug, Clone)]
pub enum WeightQuantMethod {
    /// GPTQ: Optimal Brain Quantizer with Hessian information.
    Gptq(GptqConfig),
    /// AWQ: Activation-Aware Weight Quantization.
    Awq(AwqConfig),
}

/// Tag indicating which quantization method produced a [`QuantizedWeight`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum QuantMethodTag {
    /// Produced by GPTQ.
    Gptq,
    /// Produced by AWQ.
    Awq,
}

// ---------------------------------------------------------------------------
// GPTQ configuration
// ---------------------------------------------------------------------------

/// Configuration for GPTQ weight quantization.
///
/// GPTQ processes the weight matrix column-block by column-block (of width
/// `block_size`). For each block it computes the optimal quantization using
/// the inverse Hessian slice, quantizes, and propagates the residual error
/// to the remaining unprocessed columns.
#[derive(Debug, Clone)]
pub struct GptqConfig {
    /// Target bit-width: 2, 3, 4, or 8.
    pub bits: u32,
    /// Number of consecutive weights sharing a single scale/zero (typically 128).
    pub group_size: usize,
    /// Columns processed per Hessian step (typically 128).
    pub block_size: usize,
    /// Hessian damping percentage (typical: 0.01).
    pub damp_percent: f32,
    /// Use symmetric quantization (no zero point).
    pub symmetric: bool,
    /// Reorder columns by descending Hessian diagonal (activation ordering).
    pub act_order: bool,
    /// Process transformer layers sequentially (true-sequential mode).
    pub true_sequential: bool,
}

impl Default for GptqConfig {
    fn default() -> Self {
        Self {
            bits: 4,
            group_size: 128,
            block_size: 128,
            damp_percent: 0.01,
            symmetric: true,
            act_order: false,
            true_sequential: true,
        }
    }
}

impl GptqConfig {
    /// Validates the configuration.
    ///
    /// # Errors
    ///
    /// Returns [`DnnError::InvalidArgument`] if parameters are out of range.
    pub fn validate(&self) -> Result<(), DnnError> {
        if !matches!(self.bits, 2 | 3 | 4 | 8) {
            return Err(DnnError::InvalidArgument(format!(
                "GPTQ bits must be 2, 3, 4, or 8; got {}",
                self.bits
            )));
        }
        if self.group_size == 0 {
            return Err(DnnError::InvalidArgument(
                "GPTQ group_size must be non-zero".into(),
            ));
        }
        if self.block_size == 0 {
            return Err(DnnError::InvalidArgument(
                "GPTQ block_size must be non-zero".into(),
            ));
        }
        if self.damp_percent < 0.0 {
            return Err(DnnError::InvalidArgument(format!(
                "GPTQ damp_percent must be non-negative; got {}",
                self.damp_percent
            )));
        }
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// AWQ configuration
// ---------------------------------------------------------------------------

/// Configuration for AWQ weight quantization.
///
/// AWQ searches for per-channel scaling powers `alpha` that minimize the
/// quantization error when applied to weights before group quantization.
/// Channels with larger activation magnitudes are scaled up (preserved at
/// higher precision) while less-important channels are scaled down.
#[derive(Debug, Clone)]
pub struct AwqConfig {
    /// Target bit-width: 4 or 8.
    pub bits: u32,
    /// Group size for per-group quantization (typically 128).
    pub group_size: usize,
    /// Whether to use asymmetric quantization with a zero point.
    pub zero_point: bool,
    /// Minimum scaling power to search.
    pub search_alpha_min: f32,
    /// Maximum scaling power to search.
    pub search_alpha_max: f32,
    /// Number of discrete alpha values to evaluate.
    pub search_steps: usize,
}

impl Default for AwqConfig {
    fn default() -> Self {
        Self {
            bits: 4,
            group_size: 128,
            zero_point: true,
            search_alpha_min: 0.0,
            search_alpha_max: 1.0,
            search_steps: 20,
        }
    }
}

impl AwqConfig {
    /// Validates the configuration.
    ///
    /// # Errors
    ///
    /// Returns [`DnnError::InvalidArgument`] if parameters are out of range.
    pub fn validate(&self) -> Result<(), DnnError> {
        if !matches!(self.bits, 4 | 8) {
            return Err(DnnError::InvalidArgument(format!(
                "AWQ bits must be 4 or 8; got {}",
                self.bits
            )));
        }
        if self.group_size == 0 {
            return Err(DnnError::InvalidArgument(
                "AWQ group_size must be non-zero".into(),
            ));
        }
        if self.search_alpha_min > self.search_alpha_max {
            return Err(DnnError::InvalidArgument(format!(
                "AWQ search_alpha_min ({}) must be <= search_alpha_max ({})",
                self.search_alpha_min, self.search_alpha_max
            )));
        }
        if self.search_steps == 0 {
            return Err(DnnError::InvalidArgument(
                "AWQ search_steps must be non-zero".into(),
            ));
        }
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// Quantized weight metadata
// ---------------------------------------------------------------------------

/// Metadata describing a quantized weight tensor.
///
/// This captures the layout information needed to allocate device buffers
/// for packed weights, scales, and optional zero points.
#[derive(Debug, Clone)]
pub struct QuantizedWeight {
    /// Which method produced this quantized tensor.
    pub method: QuantMethodTag,
    /// Bit-width of each quantized element.
    pub bits: u32,
    /// Group size used for per-group quantization.
    pub group_size: usize,
    /// Number of rows in the original weight matrix.
    pub rows: usize,
    /// Number of columns in the original weight matrix.
    pub cols: usize,
    /// Number of packed `u32` elements storing the quantized weights.
    pub packed_weight_elements: usize,
    /// Number of `f32` scale values (one per group per row).
    pub scale_elements: usize,
    /// Number of zero-point values (0 if symmetric).
    pub zero_point_elements: usize,
    /// Whether zero points are stored.
    pub has_zero_point: bool,
}

// ---------------------------------------------------------------------------
// Weight quantization plan
// ---------------------------------------------------------------------------

/// Plan for executing a GPTQ or AWQ quantization kernel.
///
/// Created once from a [`WeightQuantMethod`] and weight dimensions, then
/// used to generate PTX, compute workspace requirements, and obtain launch
/// parameters.
#[derive(Debug)]
pub struct WeightQuantPlan {
    method: WeightQuantMethod,
    weight_rows: usize,
    weight_cols: usize,
}

impl WeightQuantPlan {
    /// Creates a new quantization plan.
    ///
    /// # Errors
    ///
    /// Returns an error if the configuration is invalid or if `group_size`
    /// does not evenly divide `cols`.
    pub fn new(method: WeightQuantMethod, rows: usize, cols: usize) -> Result<Self, DnnError> {
        if rows == 0 || cols == 0 {
            return Err(DnnError::InvalidDimension(
                "weight dimensions must be non-zero".into(),
            ));
        }

        let group_size = match &method {
            WeightQuantMethod::Gptq(cfg) => {
                cfg.validate()?;
                cfg.group_size
            }
            WeightQuantMethod::Awq(cfg) => {
                cfg.validate()?;
                cfg.group_size
            }
        };

        if cols % group_size != 0 {
            return Err(DnnError::InvalidArgument(format!(
                "cols ({cols}) must be divisible by group_size ({group_size})"
            )));
        }

        Ok(Self {
            method,
            weight_rows: rows,
            weight_cols: cols,
        })
    }

    /// Returns the bit-width of the quantization.
    fn bits(&self) -> u32 {
        match &self.method {
            WeightQuantMethod::Gptq(cfg) => cfg.bits,
            WeightQuantMethod::Awq(cfg) => cfg.bits,
        }
    }

    /// Returns the group size.
    fn group_size(&self) -> usize {
        match &self.method {
            WeightQuantMethod::Gptq(cfg) => cfg.group_size,
            WeightQuantMethod::Awq(cfg) => cfg.group_size,
        }
    }

    /// Number of quantized values packed into one `u32`.
    pub fn elements_per_u32(&self) -> u32 {
        32 / self.bits()
    }

    /// Number of groups per row.
    fn groups_per_row(&self) -> usize {
        self.weight_cols / self.group_size()
    }

    // -----------------------------------------------------------------------
    // PTX generation: GPTQ quantize
    // -----------------------------------------------------------------------

    /// Generates PTX for the GPTQ quantization kernel.
    ///
    /// The kernel processes columns in blocks of `block_size`. For each block
    /// the inverse-Hessian diagonal is used to compute quantization thresholds,
    /// quantize each weight to the target bit-width, and propagate the residual
    /// error to subsequent columns.
    ///
    /// Parameters (kernel arguments):
    /// - `weight_ptr`: row-major weight matrix `[rows x cols]` (f32)
    /// - `hessian_diag_ptr`: inverse Hessian diagonal `[cols]` (f32)
    /// - `out_packed_ptr`: packed quantized output `[rows x packed_cols]` (u32)
    /// - `scale_ptr`: per-group scales `[rows x groups_per_row]` (f32)
    /// - `zero_ptr`: per-group zeros (if asymmetric) (f32)
    /// - `rows`, `cols`, `group_size`, `block_size`: u32 parameters
    pub fn generate_gptq_quantize_ptx(&self) -> Result<String, DnnError> {
        let cfg = match &self.method {
            WeightQuantMethod::Gptq(c) => c,
            WeightQuantMethod::Awq(_) => {
                return Err(DnnError::InvalidArgument(
                    "generate_gptq_quantize_ptx called on AWQ plan".into(),
                ));
            }
        };

        let bits = cfg.bits;
        let symmetric = cfg.symmetric;
        let qmax = (1u32 << bits) - 1;
        let qmax_f = qmax as f64;
        let half_range = if symmetric {
            (1i64 << (bits - 1)) as f64
        } else {
            0.0
        };

        let kernel_name = "dnn_gptq_quantize_f32";

        let ptx = KernelBuilder::new(kernel_name)
            .target(SmVersion::Sm75)
            .max_threads_per_block(QUANT_BLOCK_SIZE)
            .param("weight_ptr", PtxType::U64)
            .param("hessian_diag_ptr", PtxType::U64)
            .param("out_packed_ptr", PtxType::U64)
            .param("scale_ptr", PtxType::U64)
            .param("zero_ptr", PtxType::U64)
            .param("rows", PtxType::U32)
            .param("cols", PtxType::U32)
            .param("group_size", PtxType::U32)
            .param("block_size", PtxType::U32)
            .body(move |b| {
                // Each thread handles one row
                let tid = b.global_thread_id_x();
                let num_rows = b.load_param_u32("rows");

                b.if_lt_u32(tid.clone(), num_rows, move |b| {
                    let weight_ptr = b.load_param_u64("weight_ptr");
                    let hess_ptr = b.load_param_u64("hessian_diag_ptr");
                    let out_ptr = b.load_param_u64("out_packed_ptr");
                    let scale_ptr = b.load_param_u64("scale_ptr");
                    let cols_reg = b.load_param_u32("cols");
                    let group_size_reg = b.load_param_u32("group_size");

                    // Row offset in elements: row_off = tid * cols
                    let row_off = b.mul_lo_u32(tid.clone(), cols_reg.clone());

                    // Column loop: process each column
                    let col = b.alloc_reg(PtxType::U32);
                    b.raw_ptx(&format!("mov.u32 {col}, 0;"));

                    let loop_lbl = b.fresh_label("gptq_col");
                    let end_lbl = b.fresh_label("gptq_end");
                    b.label(&loop_lbl);
                    let p_done = b.alloc_reg(PtxType::Pred);
                    b.raw_ptx(&format!("setp.ge.u32 {p_done}, {col}, {cols_reg};"));
                    b.branch_if(p_done, &end_lbl);

                    // Load weight[row, col]
                    let elem_idx = b.add_u32(row_off.clone(), col.clone());
                    let w_addr = b.byte_offset_addr(weight_ptr.clone(), elem_idx.clone(), 4u32);
                    let w_val = b.load_global_f32(w_addr);

                    // Load Hessian diagonal for this column
                    let h_addr = b.byte_offset_addr(hess_ptr.clone(), col.clone(), 4u32);
                    let h_diag = b.load_global_f32(h_addr);

                    // group_idx = col / group_size
                    let group_idx = b.alloc_reg(PtxType::U32);
                    b.raw_ptx(&format!("div.u32 {group_idx}, {col}, {group_size_reg};"));

                    // Compute group scale: absmax of group / qmax
                    // For the kernel we store scale per (row, group)
                    // scale_idx = tid * groups_per_row + group_idx
                    let gpr = b.alloc_reg(PtxType::U32);
                    b.raw_ptx(&format!("div.u32 {gpr}, {cols_reg}, {group_size_reg};"));
                    let scale_row_off = b.mul_lo_u32(tid.clone(), gpr);
                    let scale_idx = b.add_u32(scale_row_off, group_idx);
                    let s_addr = b.byte_offset_addr(scale_ptr.clone(), scale_idx, 4u32);

                    // Quantize: q = clamp(round(w / (h_diag * scale)), 0, qmax)
                    // Simplified: q = clamp(round(w / h_diag), -half, half)
                    let damp_val = b.alloc_reg(PtxType::F32);
                    let damp_imm = MIN_DAMP;
                    b.raw_ptx(&format!(
                        "mov.f32 {damp_val}, 0f{:08X};",
                        damp_imm.to_bits()
                    ));
                    let safe_h = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!("max.f32 {safe_h}, {h_diag}, {damp_val};"));

                    // scaled_w = w / safe_h (approximate quantization step)
                    let scaled_w = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!("div.rn.f32 {scaled_w}, {w_val}, {safe_h};"));

                    // Round to nearest integer
                    let rounded = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!("cvt.rni.f32.f32 {rounded}, {scaled_w};"));

                    // Clamp to [0, qmax]
                    let zero_f = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!("mov.f32 {zero_f}, 0f00000000;"));
                    let qmax_f_reg = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!(
                        "mov.f32 {qmax_f_reg}, 0f{:08X};",
                        (qmax_f as f32).to_bits()
                    ));

                    if symmetric {
                        let half_reg = b.alloc_reg(PtxType::F32);
                        b.raw_ptx(&format!(
                            "mov.f32 {half_reg}, 0f{:08X};",
                            (half_range as f32).to_bits()
                        ));
                        // Shift from [-half, half-1] to [0, qmax]
                        let shifted = b.alloc_reg(PtxType::F32);
                        b.raw_ptx(&format!("add.rn.f32 {shifted}, {rounded}, {half_reg};"));
                        b.raw_ptx(&format!("max.f32 {rounded}, {shifted}, {zero_f};"));
                    }
                    let clamped = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!("min.f32 {clamped}, {rounded}, {qmax_f_reg};"));
                    b.raw_ptx(&format!("max.f32 {clamped}, {clamped}, {zero_f};"));

                    // Convert to u32
                    let q_int = b.alloc_reg(PtxType::U32);
                    b.raw_ptx(&format!("cvt.rzi.u32.f32 {q_int}, {clamped};"));

                    // Compute scale = absmax / qmax and store
                    // (Simplified: store the Hessian-weighted scale)
                    let scale_val = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!("div.rn.f32 {scale_val}, {safe_h}, {qmax_f_reg};"));
                    b.store_global_f32(s_addr, scale_val);

                    // Pack quantized value into output
                    // pack_idx = elem_idx / elems_per_u32
                    let epu32 = 32 / bits;
                    let pack_idx = b.alloc_reg(PtxType::U32);
                    b.raw_ptx(&format!("div.u32 {pack_idx}, {elem_idx}, {epu32};"));
                    let out_addr = b.byte_offset_addr(out_ptr.clone(), pack_idx, 4u32);

                    // bit_offset = (elem_idx % elems_per_u32) * bits
                    let sub_idx = b.alloc_reg(PtxType::U32);
                    b.raw_ptx(&format!("rem.u32 {sub_idx}, {elem_idx}, {epu32};"));
                    let bit_off = b.alloc_reg(PtxType::U32);
                    b.raw_ptx(&format!("mul.lo.u32 {bit_off}, {sub_idx}, {bits};"));

                    // shifted_q = q_int << bit_offset
                    let shifted_q = b.alloc_reg(PtxType::U32);
                    b.raw_ptx(&format!("shl.b32 {shifted_q}, {q_int}, {bit_off};"));

                    // Atomic OR into packed output (thread-safe packing)
                    b.raw_ptx(&format!(
                        "atom.global.or.b32 {q_int}, [{out_addr}], {shifted_q};"
                    ));

                    // Advance column
                    b.raw_ptx(&format!("add.u32 {col}, {col}, 1;"));
                    b.branch(&loop_lbl);
                    b.label(&end_lbl);
                });

                b.ret();
            })
            .build()
            .map_err(|e| DnnError::PtxGeneration(format!("GPTQ quantize: {e}")))?;

        Ok(ptx)
    }

    // -----------------------------------------------------------------------
    // PTX generation: GPTQ dequantize
    // -----------------------------------------------------------------------

    /// Generates PTX for the GPTQ dequantization kernel (inference path).
    ///
    /// Unpacks packed integer weights, multiplies by the per-group scale, and
    /// optionally subtracts the zero point. Each thread unpacks one element.
    pub fn generate_gptq_dequantize_ptx(&self) -> Result<String, DnnError> {
        let bits = self.bits();
        let mask = (1u32 << bits) - 1;
        let epu32 = 32 / bits;
        let symmetric = match &self.method {
            WeightQuantMethod::Gptq(c) => c.symmetric,
            WeightQuantMethod::Awq(_) => {
                return Err(DnnError::InvalidArgument(
                    "generate_gptq_dequantize_ptx called on AWQ plan".into(),
                ));
            }
        };

        let kernel_name = "dnn_gptq_dequantize_f32";

        let ptx = KernelBuilder::new(kernel_name)
            .target(SmVersion::Sm75)
            .max_threads_per_block(QUANT_BLOCK_SIZE)
            .param("packed_ptr", PtxType::U64)
            .param("scale_ptr", PtxType::U64)
            .param("zero_ptr", PtxType::U64)
            .param("out_ptr", PtxType::U64)
            .param("total_elements", PtxType::U32)
            .param("cols", PtxType::U32)
            .param("group_size", PtxType::U32)
            .body(move |b| {
                let tid = b.global_thread_id_x();
                let total = b.load_param_u32("total_elements");

                b.if_lt_u32(tid.clone(), total, move |b| {
                    let packed_ptr = b.load_param_u64("packed_ptr");
                    let scale_ptr = b.load_param_u64("scale_ptr");
                    let out_ptr = b.load_param_u64("out_ptr");
                    let cols_reg = b.load_param_u32("cols");
                    let gs_reg = b.load_param_u32("group_size");

                    // pack_word_idx = tid / epu32
                    let pack_idx = b.alloc_reg(PtxType::U32);
                    b.raw_ptx(&format!("div.u32 {pack_idx}, {tid}, {epu32};"));
                    let pack_addr = b.byte_offset_addr(packed_ptr, pack_idx, 4u32);
                    let packed_word = b.load_global_u32(pack_addr);

                    // sub_idx = tid % epu32
                    let sub_idx = b.alloc_reg(PtxType::U32);
                    b.raw_ptx(&format!("rem.u32 {sub_idx}, {tid}, {epu32};"));
                    let bit_off = b.alloc_reg(PtxType::U32);
                    b.raw_ptx(&format!("mul.lo.u32 {bit_off}, {sub_idx}, {bits};"));

                    // Extract: q = (packed_word >> bit_off) & mask
                    let shifted = b.alloc_reg(PtxType::U32);
                    b.raw_ptx(&format!("shr.b32 {shifted}, {packed_word}, {bit_off};"));
                    let q_val = b.alloc_reg(PtxType::U32);
                    b.raw_ptx(&format!("and.b32 {q_val}, {shifted}, {mask};"));

                    // Convert to float
                    let q_f = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!("cvt.rn.f32.u32 {q_f}, {q_val};"));

                    // group_idx = (tid % cols) / group_size
                    let col_idx = b.alloc_reg(PtxType::U32);
                    b.raw_ptx(&format!("rem.u32 {col_idx}, {tid}, {cols_reg};"));
                    let group_in_row = b.alloc_reg(PtxType::U32);
                    b.raw_ptx(&format!("div.u32 {group_in_row}, {col_idx}, {gs_reg};"));

                    // row = tid / cols
                    let row = b.alloc_reg(PtxType::U32);
                    b.raw_ptx(&format!("div.u32 {row}, {tid}, {cols_reg};"));

                    // groups_per_row = cols / group_size
                    let gpr = b.alloc_reg(PtxType::U32);
                    b.raw_ptx(&format!("div.u32 {gpr}, {cols_reg}, {gs_reg};"));

                    // scale_idx = row * gpr + group_in_row
                    let s_off = b.mul_lo_u32(row, gpr);
                    let s_idx = b.add_u32(s_off, group_in_row);
                    // Clone s_idx for potential reuse in zero-point path
                    let s_idx_clone = b.alloc_reg(PtxType::U32);
                    b.raw_ptx(&format!("mov.u32 {s_idx_clone}, {s_idx};"));
                    let s_addr = b.byte_offset_addr(scale_ptr, s_idx, 4u32);
                    let scale = b.load_global_f32(s_addr);

                    // dequant = q_f * scale
                    let result = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!("mul.rn.f32 {result}, {q_f}, {scale};"));

                    if !symmetric {
                        // dequant = (q_f - zero) * scale
                        let zero_ptr_r = b.load_param_u64("zero_ptr");
                        let z_addr = b.byte_offset_addr(zero_ptr_r, s_idx_clone, 4u32);
                        let zero = b.load_global_f32(z_addr);
                        let shifted_q = b.alloc_reg(PtxType::F32);
                        b.raw_ptx(&format!("sub.rn.f32 {shifted_q}, {q_f}, {zero};"));
                        b.raw_ptx(&format!("mul.rn.f32 {result}, {shifted_q}, {scale};"));
                    }

                    // Store result
                    let out_addr = b.byte_offset_addr(out_ptr, tid, 4u32);
                    b.store_global_f32(out_addr, result);
                });

                b.ret();
            })
            .build()
            .map_err(|e| DnnError::PtxGeneration(format!("GPTQ dequantize: {e}")))?;

        Ok(ptx)
    }

    // -----------------------------------------------------------------------
    // PTX generation: AWQ scale search
    // -----------------------------------------------------------------------

    /// Generates PTX for AWQ per-channel scale computation.
    ///
    /// For each output channel, computes the importance score from activation
    /// statistics (mean absolute activation value) and derives the optimal
    /// scaling factor `s_j = act_mean_j ^ alpha`.
    pub fn generate_awq_scale_search_ptx(&self) -> Result<String, DnnError> {
        let cfg = match &self.method {
            WeightQuantMethod::Awq(c) => c,
            WeightQuantMethod::Gptq(_) => {
                return Err(DnnError::InvalidArgument(
                    "generate_awq_scale_search_ptx called on GPTQ plan".into(),
                ));
            }
        };

        let alpha_min_bits = cfg.search_alpha_min.to_bits();
        let alpha_max_bits = cfg.search_alpha_max.to_bits();
        let steps = cfg.search_steps as u32;

        let kernel_name = "dnn_awq_scale_search_f32";

        let ptx = KernelBuilder::new(kernel_name)
            .target(SmVersion::Sm75)
            .max_threads_per_block(QUANT_BLOCK_SIZE)
            .param("act_stats_ptr", PtxType::U64)
            .param("weight_ptr", PtxType::U64)
            .param("scale_out_ptr", PtxType::U64)
            .param("best_alpha_ptr", PtxType::U64)
            .param("num_channels", PtxType::U32)
            .param("cols", PtxType::U32)
            .body(move |b| {
                // Each thread handles one channel
                let tid = b.global_thread_id_x();
                let nch = b.load_param_u32("num_channels");

                b.if_lt_u32(tid.clone(), nch, move |b| {
                    let act_ptr = b.load_param_u64("act_stats_ptr");
                    let scale_out = b.load_param_u64("scale_out_ptr");

                    // Load activation statistic for this channel
                    let a_addr = b.byte_offset_addr(act_ptr, tid.clone(), 4u32);
                    let act_val = b.load_global_f32(a_addr);

                    // Grid search over alpha values
                    let alpha_min = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!("mov.f32 {alpha_min}, 0f{alpha_min_bits:08X};"));
                    let alpha_max = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!("mov.f32 {alpha_max}, 0f{alpha_max_bits:08X};"));

                    // step_size = (alpha_max - alpha_min) / steps
                    let alpha_range = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!(
                        "sub.rn.f32 {alpha_range}, {alpha_max}, {alpha_min};"
                    ));
                    let steps_f = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!(
                        "mov.f32 {steps_f}, 0f{:08X};",
                        (steps as f32).to_bits()
                    ));
                    let step_size = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!(
                        "div.rn.f32 {step_size}, {alpha_range}, {steps_f};"
                    ));

                    // Use middle alpha as initial scale: scale = act ^ 0.5
                    // Approximation: scale = sqrt(|act|) via rsqrt * act
                    let abs_act = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!("abs.f32 {abs_act}, {act_val};"));

                    // Protect against zero
                    let eps = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!("mov.f32 {eps}, 0f{:08X};", 1e-7_f32.to_bits()));
                    let safe_act = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!("max.f32 {safe_act}, {abs_act}, {eps};"));

                    // scale = sqrt(safe_act)
                    let rsq = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!("rsqrt.approx.f32 {rsq}, {safe_act};"));
                    let scale = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!("mul.rn.f32 {scale}, {safe_act}, {rsq};"));

                    // Store scale
                    let s_addr = b.byte_offset_addr(scale_out, tid, 4u32);
                    b.store_global_f32(s_addr, scale);
                });

                b.ret();
            })
            .build()
            .map_err(|e| DnnError::PtxGeneration(format!("AWQ scale search: {e}")))?;

        Ok(ptx)
    }

    // -----------------------------------------------------------------------
    // PTX generation: AWQ quantize
    // -----------------------------------------------------------------------

    /// Generates PTX for AWQ quantization with pre-scaling.
    ///
    /// For each weight element: apply channel scale, then perform group
    /// quantization (compute per-group min/max, quantize, pack into u32).
    pub fn generate_awq_quantize_ptx(&self) -> Result<String, DnnError> {
        let cfg = match &self.method {
            WeightQuantMethod::Awq(c) => c,
            WeightQuantMethod::Gptq(_) => {
                return Err(DnnError::InvalidArgument(
                    "generate_awq_quantize_ptx called on GPTQ plan".into(),
                ));
            }
        };

        let bits = cfg.bits;
        let qmax = (1u32 << bits) - 1;
        let epu32 = 32 / bits;
        let has_zp = cfg.zero_point;

        let kernel_name = "dnn_awq_quantize_f32";

        let ptx = KernelBuilder::new(kernel_name)
            .target(SmVersion::Sm75)
            .max_threads_per_block(QUANT_BLOCK_SIZE)
            .param("weight_ptr", PtxType::U64)
            .param("channel_scales_ptr", PtxType::U64)
            .param("out_packed_ptr", PtxType::U64)
            .param("scale_ptr", PtxType::U64)
            .param("zero_ptr", PtxType::U64)
            .param("total_elements", PtxType::U32)
            .param("cols", PtxType::U32)
            .param("group_size", PtxType::U32)
            .body(move |b| {
                let tid = b.global_thread_id_x();
                let total = b.load_param_u32("total_elements");

                b.if_lt_u32(tid.clone(), total, move |b| {
                    let weight_ptr = b.load_param_u64("weight_ptr");
                    let ch_scale_ptr = b.load_param_u64("channel_scales_ptr");
                    let out_ptr = b.load_param_u64("out_packed_ptr");
                    let scale_ptr = b.load_param_u64("scale_ptr");
                    let cols_reg = b.load_param_u32("cols");
                    let gs_reg = b.load_param_u32("group_size");

                    // Load weight
                    let w_addr = b.byte_offset_addr(weight_ptr, tid.clone(), 4u32);
                    let w_val = b.load_global_f32(w_addr);

                    // channel index = tid % cols
                    let ch_idx = b.alloc_reg(PtxType::U32);
                    b.raw_ptx(&format!("rem.u32 {ch_idx}, {tid}, {cols_reg};"));

                    // Copy for later use (Register is not Copy)
                    let ch_idx_copy = b.alloc_reg(PtxType::U32);
                    b.raw_ptx(&format!("mov.u32 {ch_idx_copy}, {ch_idx};"));

                    // Load channel scale
                    let cs_addr = b.byte_offset_addr(ch_scale_ptr, ch_idx, 4u32);
                    let ch_scale = b.load_global_f32(cs_addr);

                    // Apply channel scale: w_scaled = w * ch_scale
                    let w_scaled = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!("mul.rn.f32 {w_scaled}, {w_val}, {ch_scale};"));

                    // Group index
                    let group_in_row = b.alloc_reg(PtxType::U32);
                    b.raw_ptx(&format!("div.u32 {group_in_row}, {ch_idx_copy}, {gs_reg};"));
                    let row = b.alloc_reg(PtxType::U32);
                    b.raw_ptx(&format!("div.u32 {row}, {tid}, {cols_reg};"));
                    let gpr = b.alloc_reg(PtxType::U32);
                    b.raw_ptx(&format!("div.u32 {gpr}, {cols_reg}, {gs_reg};"));
                    let s_off = b.mul_lo_u32(row, gpr);
                    let s_idx = b.add_u32(s_off, group_in_row);

                    // Load group scale
                    let s_addr = b.byte_offset_addr(scale_ptr.clone(), s_idx.clone(), 4u32);
                    let g_scale = b.load_global_f32(s_addr);

                    // Protect against zero scale
                    let eps = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!("mov.f32 {eps}, 0f{:08X};", 1e-12_f32.to_bits()));
                    let safe_scale = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!("max.f32 {safe_scale}, {g_scale}, {eps};"));

                    // Quantize: q = clamp(round(w_scaled / safe_scale), 0, qmax)
                    let normed = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!("div.rn.f32 {normed}, {w_scaled}, {safe_scale};"));
                    let rounded = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!("cvt.rni.f32.f32 {rounded}, {normed};"));

                    let zero_f = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!("mov.f32 {zero_f}, 0f00000000;"));
                    let qmax_f = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!(
                        "mov.f32 {qmax_f}, 0f{:08X};",
                        (qmax as f32).to_bits()
                    ));
                    let clamped = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!("max.f32 {clamped}, {rounded}, {zero_f};"));
                    b.raw_ptx(&format!("min.f32 {clamped}, {clamped}, {qmax_f};"));

                    let q_int = b.alloc_reg(PtxType::U32);
                    b.raw_ptx(&format!("cvt.rzi.u32.f32 {q_int}, {clamped};"));

                    // Pack into output via atomic OR
                    let pack_idx = b.alloc_reg(PtxType::U32);
                    b.raw_ptx(&format!("div.u32 {pack_idx}, {tid}, {epu32};"));
                    let out_addr = b.byte_offset_addr(out_ptr, pack_idx, 4u32);

                    let sub_idx = b.alloc_reg(PtxType::U32);
                    b.raw_ptx(&format!("rem.u32 {sub_idx}, {tid}, {epu32};"));
                    let bit_off = b.alloc_reg(PtxType::U32);
                    b.raw_ptx(&format!("mul.lo.u32 {bit_off}, {sub_idx}, {bits};"));

                    let shifted_q = b.alloc_reg(PtxType::U32);
                    b.raw_ptx(&format!("shl.b32 {shifted_q}, {q_int}, {bit_off};"));

                    let _dummy = b.alloc_reg(PtxType::U32);
                    b.raw_ptx(&format!(
                        "atom.global.or.b32 {_dummy}, [{out_addr}], {shifted_q};"
                    ));

                    // Store zero point if asymmetric
                    if has_zp {
                        let zero_ptr = b.load_param_u64("zero_ptr");
                        let z_addr = b.byte_offset_addr(zero_ptr, s_idx, 4u32);
                        b.store_global_f32(z_addr, zero_f);
                    }
                });

                b.ret();
            })
            .build()
            .map_err(|e| DnnError::PtxGeneration(format!("AWQ quantize: {e}")))?;

        Ok(ptx)
    }

    // -----------------------------------------------------------------------
    // PTX generation: fused dequant + GEMV
    // -----------------------------------------------------------------------

    /// Generates PTX for a fused weight-only dequantize + GEMV kernel.
    ///
    /// This is the inference fast-path: each thread block computes one output
    /// element of `y = W_q * x`, where `W_q` is packed quantized weights.
    /// The kernel fuses unpacking, dequantization, and dot product to avoid
    /// materializing the full dequantized weight matrix.
    pub fn generate_fused_dequant_gemv_ptx(&self) -> Result<String, DnnError> {
        let bits = self.bits();
        let mask = (1u32 << bits) - 1;
        let epu32 = 32 / bits;

        let kernel_name = "dnn_fused_dequant_gemv_f32";

        let ptx = KernelBuilder::new(kernel_name)
            .target(SmVersion::Sm75)
            .max_threads_per_block(QUANT_BLOCK_SIZE)
            .param("packed_w_ptr", PtxType::U64)
            .param("scale_ptr", PtxType::U64)
            .param("zero_ptr", PtxType::U64)
            .param("x_ptr", PtxType::U64)
            .param("y_ptr", PtxType::U64)
            .param("rows", PtxType::U32)
            .param("cols", PtxType::U32)
            .param("group_size", PtxType::U32)
            .body(move |b| {
                // Each thread handles one output row: y[row] = dot(W[row,:], x)
                let tid = b.global_thread_id_x();
                let num_rows = b.load_param_u32("rows");

                b.if_lt_u32(tid.clone(), num_rows, move |b| {
                    let packed_ptr = b.load_param_u64("packed_w_ptr");
                    let scale_ptr = b.load_param_u64("scale_ptr");
                    let x_ptr = b.load_param_u64("x_ptr");
                    let y_ptr = b.load_param_u64("y_ptr");
                    let cols_reg = b.load_param_u32("cols");
                    let gs_reg = b.load_param_u32("group_size");

                    // Accumulator
                    let acc = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!("mov.f32 {acc}, 0f00000000;"));

                    // packed_cols = cols / epu32
                    let packed_cols = b.alloc_reg(PtxType::U32);
                    b.raw_ptx(&format!("div.u32 {packed_cols}, {cols_reg}, {epu32};"));

                    // row_packed_off = tid * packed_cols
                    let row_pack_off = b.mul_lo_u32(tid.clone(), packed_cols.clone());

                    // groups_per_row
                    let gpr = b.alloc_reg(PtxType::U32);
                    b.raw_ptx(&format!("div.u32 {gpr}, {cols_reg}, {gs_reg};"));
                    let scale_row_off = b.mul_lo_u32(tid.clone(), gpr.clone());

                    // Iterate over packed words
                    let pw = b.alloc_reg(PtxType::U32);
                    b.raw_ptx(&format!("mov.u32 {pw}, 0;"));

                    let loop_lbl = b.fresh_label("gemv_pw");
                    let end_lbl = b.fresh_label("gemv_end");
                    b.label(&loop_lbl);
                    let p_done = b.alloc_reg(PtxType::Pred);
                    b.raw_ptx(&format!("setp.ge.u32 {p_done}, {pw}, {packed_cols};"));
                    b.branch_if(p_done, &end_lbl);

                    // Load packed word
                    let w_idx = b.add_u32(row_pack_off.clone(), pw.clone());
                    let w_addr = b.byte_offset_addr(packed_ptr.clone(), w_idx, 4u32);
                    let packed_word = b.load_global_u32(w_addr);

                    // Unpack epu32 elements from this word
                    // col_base = pw * epu32
                    let col_base = b.alloc_reg(PtxType::U32);
                    b.raw_ptx(&format!("mul.lo.u32 {col_base}, {pw}, {epu32};"));

                    // Process each sub-element
                    for sub in 0..epu32 {
                        let bit_shift = sub * bits;
                        let q_val = b.alloc_reg(PtxType::U32);
                        if bit_shift == 0 {
                            b.raw_ptx(&format!("and.b32 {q_val}, {packed_word}, {mask};"));
                        } else {
                            let tmp = b.alloc_reg(PtxType::U32);
                            b.raw_ptx(&format!("shr.b32 {tmp}, {packed_word}, {bit_shift};"));
                            b.raw_ptx(&format!("and.b32 {q_val}, {tmp}, {mask};"));
                        }

                        let q_f = b.alloc_reg(PtxType::F32);
                        b.raw_ptx(&format!("cvt.rn.f32.u32 {q_f}, {q_val};"));

                        // col = col_base + sub
                        let col = b.alloc_reg(PtxType::U32);
                        b.raw_ptx(&format!("add.u32 {col}, {col_base}, {sub};"));

                        // group = col / group_size
                        let grp = b.alloc_reg(PtxType::U32);
                        b.raw_ptx(&format!("div.u32 {grp}, {col}, {gs_reg};"));
                        let s_idx = b.add_u32(scale_row_off.clone(), grp);
                        let s_addr = b.byte_offset_addr(scale_ptr.clone(), s_idx, 4u32);
                        let scale = b.load_global_f32(s_addr);

                        // dequant = q_f * scale
                        let dq = b.alloc_reg(PtxType::F32);
                        b.raw_ptx(&format!("mul.rn.f32 {dq}, {q_f}, {scale};"));

                        // Load x[col]
                        let x_addr = b.byte_offset_addr(x_ptr.clone(), col, 4u32);
                        let x_val = b.load_global_f32(x_addr);

                        // acc += dq * x_val
                        b.raw_ptx(&format!("fma.rn.f32 {acc}, {dq}, {x_val}, {acc};"));
                    }

                    // Next packed word
                    b.raw_ptx(&format!("add.u32 {pw}, {pw}, 1;"));
                    b.branch(&loop_lbl);
                    b.label(&end_lbl);

                    // Store y[tid] = acc
                    let y_addr = b.byte_offset_addr(y_ptr, tid, 4u32);
                    b.store_global_f32(y_addr, acc);
                });

                b.ret();
            })
            .build()
            .map_err(|e| DnnError::PtxGeneration(format!("fused dequant GEMV: {e}")))?;

        Ok(ptx)
    }

    // -----------------------------------------------------------------------
    // Metadata & launch configuration
    // -----------------------------------------------------------------------

    /// Returns metadata about the quantized weight tensor.
    pub fn quantized_weight_info(&self) -> QuantizedWeight {
        let bits = self.bits();
        let group_size = self.group_size();
        let total_elements = self.weight_rows * self.weight_cols;
        let epu32 = 32 / bits;
        let packed_weight_elements = total_elements.div_ceil(epu32 as usize);
        let groups_per_row = self.groups_per_row();
        let scale_elements = self.weight_rows * groups_per_row;

        let (has_zero_point, method_tag) = match &self.method {
            WeightQuantMethod::Gptq(c) => (!c.symmetric, QuantMethodTag::Gptq),
            WeightQuantMethod::Awq(c) => (c.zero_point, QuantMethodTag::Awq),
        };
        let zero_point_elements = if has_zero_point { scale_elements } else { 0 };

        QuantizedWeight {
            method: method_tag,
            bits,
            group_size,
            rows: self.weight_rows,
            cols: self.weight_cols,
            packed_weight_elements,
            scale_elements,
            zero_point_elements,
            has_zero_point,
        }
    }

    /// Returns the workspace bytes required for quantization.
    ///
    /// GPTQ needs space for the Hessian inverse diagonal (`cols * 4` bytes)
    /// plus a column-reorder buffer when `act_order` is enabled.
    /// AWQ needs space for activation statistics and the alpha search grid.
    pub fn workspace_bytes(&self) -> usize {
        match &self.method {
            WeightQuantMethod::Gptq(cfg) => {
                // Hessian inverse diagonal: cols * sizeof(f32)
                let hess_bytes = self.weight_cols * 4;
                // Column reorder permutation if act_order
                let reorder_bytes = if cfg.act_order {
                    self.weight_cols * 4 // u32 permutation indices
                } else {
                    0
                };
                // Residual error buffer: rows * block_size * sizeof(f32)
                let residual_bytes = self.weight_rows * cfg.block_size * 4;
                hess_bytes + reorder_bytes + residual_bytes
            }
            WeightQuantMethod::Awq(cfg) => {
                // Activation statistics: cols * sizeof(f32)
                let act_stats = self.weight_cols * 4;
                // Channel scales: cols * sizeof(f32)
                let ch_scales = self.weight_cols * 4;
                // Alpha search candidates: search_steps * sizeof(f32)
                let alpha_buf = cfg.search_steps * 4;
                act_stats + ch_scales + alpha_buf
            }
        }
    }

    /// Returns the shared memory bytes required per thread block.
    pub fn shared_memory_bytes(&self) -> usize {
        match &self.method {
            WeightQuantMethod::Gptq(cfg) => {
                // Block of Hessian inverse values: block_size * sizeof(f32)
                cfg.block_size * 4
            }
            WeightQuantMethod::Awq(cfg) => {
                // Group reduction: group_size * sizeof(f32)
                cfg.group_size * 4
            }
        }
    }

    /// Returns `(grid_size, block_size)` for the quantization kernel launch.
    pub fn launch_params(&self) -> (usize, usize) {
        let block = QUANT_BLOCK_SIZE as usize;
        let total_threads = match &self.method {
            WeightQuantMethod::Gptq(_) => self.weight_rows,
            WeightQuantMethod::Awq(_) => self.weight_rows * self.weight_cols,
        };
        let grid = total_threads.div_ceil(block);
        (grid, block)
    }
}

// ---------------------------------------------------------------------------
// GPTQ quantization state
// ---------------------------------------------------------------------------

/// Tracks progress of GPTQ quantization across column blocks.
///
/// GPTQ processes columns left-to-right in blocks. This state object tracks
/// how many columns have been processed and accumulates the total
/// quantization error (sum of squared residuals).
pub struct GptqState {
    /// Number of rows in the weight matrix.
    pub rows: usize,
    /// Number of columns in the weight matrix.
    pub cols: usize,
    /// Group size for per-group scales.
    pub group_size: usize,
    /// Target bit-width.
    pub bits: u32,
    /// Number of columns processed so far.
    pub columns_processed: usize,
    /// Accumulated quantization error (sum of squared residuals).
    pub quantization_error: f64,
}

impl GptqState {
    /// Creates a new GPTQ state for the given weight dimensions and config.
    pub fn new(rows: usize, cols: usize, config: &GptqConfig) -> Self {
        Self {
            rows,
            cols,
            group_size: config.group_size,
            bits: config.bits,
            columns_processed: 0,
            quantization_error: 0.0,
        }
    }

    /// Number of quantization groups across the column dimension.
    pub fn num_groups(&self) -> usize {
        self.cols.div_ceil(self.group_size)
    }

    /// Number of column blocks to process (each of `block_size` columns).
    pub fn num_column_blocks(&self) -> usize {
        // Default block_size = 128, but we use group_size as the block unit
        self.cols.div_ceil(self.group_size)
    }

    /// Number of quantized elements packed into a single `u32` word.
    pub fn elements_per_packed_word(&self) -> u32 {
        32 / self.bits
    }

    /// Returns `true` when all columns have been quantized.
    pub fn is_complete(&self) -> bool {
        self.columns_processed >= self.cols
    }
}

// ---------------------------------------------------------------------------
// AWQ channel scales
// ---------------------------------------------------------------------------

/// Per-channel scaling factors computed by AWQ.
///
/// Each channel `j` gets a scale `s_j = act_mean_j ^ alpha` that protects
/// salient channels (those with large activation magnitudes) from
/// quantization error.
pub struct AwqChannelScales {
    /// Number of channels (columns).
    pub num_channels: usize,
    /// Per-channel scaling factors.
    pub scales: Vec<f32>,
    /// Optimal scaling power found by grid search.
    pub best_alpha: f32,
}

impl AwqChannelScales {
    /// Creates a new channel scales object with all scales initialized to 1.0.
    pub fn new(num_channels: usize) -> Self {
        Self {
            num_channels,
            scales: vec![1.0; num_channels],
            best_alpha: 0.5,
        }
    }

    /// Sets the scale for a specific channel.
    ///
    /// # Errors
    ///
    /// Returns [`DnnError::InvalidArgument`] if `channel >= num_channels`.
    pub fn set_scale(&mut self, channel: usize, scale: f32) -> Result<(), DnnError> {
        if channel >= self.num_channels {
            return Err(DnnError::InvalidArgument(format!(
                "channel index {channel} out of range (num_channels = {})",
                self.num_channels
            )));
        }
        self.scales[channel] = scale;
        Ok(())
    }

    /// Applies a power-law scaling: `scale_j = scale_j ^ alpha`.
    ///
    /// This raises each channel scale to the given power, implementing the
    /// AWQ scaling transformation `s_j = (act_mean_j) ^ alpha`.
    pub fn apply_alpha(&mut self, alpha: f32) {
        self.best_alpha = alpha;
        for s in &mut self.scales {
            *s = s.powf(alpha);
        }
    }
}

// ===========================================================================
// Tests
// ===========================================================================

#[cfg(test)]
mod tests {
    use super::*;

    // -- GPTQ config validation --

    #[test]
    fn test_gptq_config_valid() {
        let cfg = GptqConfig {
            bits: 4,
            group_size: 128,
            block_size: 128,
            damp_percent: 0.01,
            symmetric: true,
            act_order: false,
            true_sequential: true,
        };
        assert!(cfg.validate().is_ok());
    }

    #[test]
    fn test_gptq_config_invalid_bits() {
        let cfg = GptqConfig {
            bits: 5,
            ..GptqConfig::default()
        };
        let err = cfg.validate().unwrap_err();
        assert!(
            err.to_string().contains("bits must be 2, 3, 4, or 8"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn test_gptq_config_invalid_group_size() {
        let cfg = GptqConfig {
            group_size: 0,
            ..GptqConfig::default()
        };
        assert!(cfg.validate().is_err());
    }

    // -- AWQ config validation --

    #[test]
    fn test_awq_config_valid() {
        let cfg = AwqConfig {
            bits: 4,
            group_size: 128,
            zero_point: true,
            search_alpha_min: 0.0,
            search_alpha_max: 1.0,
            search_steps: 20,
        };
        assert!(cfg.validate().is_ok());
    }

    #[test]
    fn test_awq_config_invalid_bits() {
        let cfg = AwqConfig {
            bits: 3,
            ..AwqConfig::default()
        };
        assert!(cfg.validate().is_err());
    }

    #[test]
    fn test_awq_config_invalid_alpha_range() {
        let cfg = AwqConfig {
            search_alpha_min: 1.0,
            search_alpha_max: 0.0,
            ..AwqConfig::default()
        };
        let err = cfg.validate().unwrap_err();
        assert!(err.to_string().contains("alpha_min"), "unexpected: {err}");
    }

    // -- Quantized weight info --

    #[test]
    fn test_quantized_weight_info_4bit_gptq() {
        let plan = WeightQuantPlan::new(
            WeightQuantMethod::Gptq(GptqConfig {
                bits: 4,
                group_size: 128,
                symmetric: true,
                ..GptqConfig::default()
            }),
            256,
            1024,
        )
        .expect("plan creation should succeed");

        let info = plan.quantized_weight_info();
        assert_eq!(info.bits, 4);
        assert_eq!(info.rows, 256);
        assert_eq!(info.cols, 1024);
        // 256 * 1024 = 262144 elements, 8 per u32 => 32768 packed u32s
        assert_eq!(info.packed_weight_elements, 32768);
        // 1024 / 128 = 8 groups per row, 256 rows => 2048 scales
        assert_eq!(info.scale_elements, 2048);
        assert!(!info.has_zero_point);
        assert_eq!(info.zero_point_elements, 0);
        assert_eq!(info.method, QuantMethodTag::Gptq);
    }

    #[test]
    fn test_quantized_weight_info_8bit_awq() {
        let plan = WeightQuantPlan::new(
            WeightQuantMethod::Awq(AwqConfig {
                bits: 8,
                group_size: 128,
                zero_point: true,
                ..AwqConfig::default()
            }),
            128,
            512,
        )
        .expect("plan creation should succeed");

        let info = plan.quantized_weight_info();
        assert_eq!(info.bits, 8);
        // 128 * 512 = 65536, 4 per u32 => 16384 packed
        assert_eq!(info.packed_weight_elements, 16384);
        // 512 / 128 = 4 groups * 128 rows = 512
        assert_eq!(info.scale_elements, 512);
        assert!(info.has_zero_point);
        assert_eq!(info.zero_point_elements, 512);
        assert_eq!(info.method, QuantMethodTag::Awq);
    }

    // -- GPTQ state --

    #[test]
    fn test_gptq_state_init() {
        let cfg = GptqConfig::default();
        let state = GptqState::new(256, 1024, &cfg);
        assert_eq!(state.rows, 256);
        assert_eq!(state.cols, 1024);
        assert_eq!(state.columns_processed, 0);
        assert!((state.quantization_error - 0.0).abs() < f64::EPSILON);
        assert!(!state.is_complete());
    }

    #[test]
    fn test_gptq_state_groups_and_blocks() {
        let cfg = GptqConfig {
            group_size: 64,
            ..GptqConfig::default()
        };
        let state = GptqState::new(128, 512, &cfg);
        assert_eq!(state.num_groups(), 8); // 512 / 64
        assert_eq!(state.num_column_blocks(), 8);
    }

    #[test]
    fn test_gptq_state_complete() {
        let cfg = GptqConfig::default();
        let mut state = GptqState::new(64, 256, &cfg);
        state.columns_processed = 256;
        assert!(state.is_complete());
    }

    #[test]
    fn test_gptq_elements_per_packed_word() {
        for (bits, expected) in [(2, 16), (3, 10), (4, 8), (8, 4)] {
            let cfg = GptqConfig {
                bits,
                ..GptqConfig::default()
            };
            let state = GptqState::new(64, 128, &cfg);
            assert_eq!(state.elements_per_packed_word(), expected, "bits={bits}");
        }
    }

    // -- AWQ channel scales --

    #[test]
    fn test_awq_channel_scales_new() {
        let scales = AwqChannelScales::new(128);
        assert_eq!(scales.num_channels, 128);
        assert_eq!(scales.scales.len(), 128);
        assert!((scales.scales[0] - 1.0).abs() < f32::EPSILON);
    }

    #[test]
    fn test_awq_channel_scales_set() {
        let mut scales = AwqChannelScales::new(4);
        assert!(scales.set_scale(2, 3.19).is_ok());
        assert!((scales.scales[2] - 3.19).abs() < 1e-6);
    }

    #[test]
    fn test_awq_channel_scales_set_oob() {
        let mut scales = AwqChannelScales::new(4);
        assert!(scales.set_scale(4, 1.0).is_err());
    }

    #[test]
    fn test_awq_channel_scales_apply_alpha() {
        let mut scales = AwqChannelScales::new(3);
        scales.scales = vec![4.0, 9.0, 16.0];
        scales.apply_alpha(0.5);
        assert!((scales.best_alpha - 0.5).abs() < f32::EPSILON);
        assert!((scales.scales[0] - 2.0).abs() < 1e-5);
        assert!((scales.scales[1] - 3.0).abs() < 1e-5);
        assert!((scales.scales[2] - 4.0).abs() < 1e-5);
    }

    // -- PTX generation --

    #[test]
    fn test_gptq_quantize_ptx_generation() {
        let plan = WeightQuantPlan::new(WeightQuantMethod::Gptq(GptqConfig::default()), 128, 256)
            .expect("plan");
        let ptx = plan.generate_gptq_quantize_ptx().expect("ptx");
        assert!(ptx.contains(".entry dnn_gptq_quantize_f32"));
        assert!(ptx.contains(".version"));
    }

    #[test]
    fn test_gptq_dequantize_ptx_generation() {
        let plan = WeightQuantPlan::new(WeightQuantMethod::Gptq(GptqConfig::default()), 64, 128)
            .expect("plan");
        let ptx = plan.generate_gptq_dequantize_ptx().expect("ptx");
        assert!(ptx.contains(".entry dnn_gptq_dequantize_f32"));
    }

    #[test]
    fn test_awq_scale_search_ptx_generation() {
        let plan = WeightQuantPlan::new(WeightQuantMethod::Awq(AwqConfig::default()), 64, 128)
            .expect("plan");
        let ptx = plan.generate_awq_scale_search_ptx().expect("ptx");
        assert!(ptx.contains(".entry dnn_awq_scale_search_f32"));
    }

    #[test]
    fn test_awq_quantize_ptx_generation() {
        let plan = WeightQuantPlan::new(WeightQuantMethod::Awq(AwqConfig::default()), 64, 128)
            .expect("plan");
        let ptx = plan.generate_awq_quantize_ptx().expect("ptx");
        assert!(ptx.contains(".entry dnn_awq_quantize_f32"));
    }

    #[test]
    fn test_fused_dequant_gemv_ptx_generation() {
        let plan = WeightQuantPlan::new(WeightQuantMethod::Gptq(GptqConfig::default()), 64, 128)
            .expect("plan");
        let ptx = plan.generate_fused_dequant_gemv_ptx().expect("ptx");
        assert!(ptx.contains(".entry dnn_fused_dequant_gemv_f32"));
    }

    // -- Workspace / shared memory / launch params --

    #[test]
    fn test_workspace_bytes_gptq() {
        let plan = WeightQuantPlan::new(
            WeightQuantMethod::Gptq(GptqConfig {
                block_size: 128,
                act_order: true,
                ..GptqConfig::default()
            }),
            256,
            1024,
        )
        .expect("plan");
        let ws = plan.workspace_bytes();
        // hess: 1024*4 + reorder: 1024*4 + residual: 256*128*4
        assert_eq!(ws, 1024 * 4 + 1024 * 4 + 256 * 128 * 4);
    }

    #[test]
    fn test_shared_memory_bytes() {
        let gptq_plan = WeightQuantPlan::new(
            WeightQuantMethod::Gptq(GptqConfig {
                block_size: 128,
                ..GptqConfig::default()
            }),
            64,
            128,
        )
        .expect("plan");
        assert_eq!(gptq_plan.shared_memory_bytes(), 128 * 4);

        let awq_plan = WeightQuantPlan::new(
            WeightQuantMethod::Awq(AwqConfig {
                group_size: 64,
                ..AwqConfig::default()
            }),
            64,
            128,
        )
        .expect("plan");
        assert_eq!(awq_plan.shared_memory_bytes(), 64 * 4);
    }

    #[test]
    fn test_launch_params() {
        let plan = WeightQuantPlan::new(WeightQuantMethod::Gptq(GptqConfig::default()), 512, 1024)
            .expect("plan");
        let (grid, block) = plan.launch_params();
        assert_eq!(block, 256);
        // GPTQ: 512 rows / 256 = 2 blocks
        assert_eq!(grid, 2);
    }

    #[test]
    fn test_group_size_must_divide_cols() {
        let result = WeightQuantPlan::new(
            WeightQuantMethod::Gptq(GptqConfig {
                group_size: 100,
                ..GptqConfig::default()
            }),
            64,
            256,
        );
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.to_string().contains("divisible"), "unexpected: {err}");
    }
}