unsloth-rs 1.0.2

Rust implementations of transformer building blocks for LLM inference
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
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
// SPDX-License-Identifier: MIT
// Copyright 2026 Tyler Zervas

//! CubeCL GPU kernel implementation for Ternary Attention.
//!
//! This module implements Phase 3 tasks for ternary attention:
//! - Task 3.1: Q·K^T Ternary Scoring Kernel ✅
//! - Task 3.2: Online Softmax with Popcount ✅
//! - Task 3.3: Hybrid FP/Ternary Dispatch ✅
//! - Task 3.4: Causal Masking via Plane Operations ✅
//! - Task 3.5: End-to-End Attention Integration Tests ✅
//!
//! ## Algorithm Overview
//!
//! Computes full attention with ternary-quantized K matrices:
//! - Q: [batch, heads, seq_len, head_dim] - FP32 query vectors
//! - K: [batch, heads, seq_len, head_dim] - Ternary bitsliced keys
//! - V: [batch, heads, seq_len, head_dim] - FP32 value vectors
//! - Output: [batch, heads, seq_len, head_dim] - Attention result
//!
//! Uses popcount-based dot product from Phase 2 ternary matmul kernels,
//! adapted for attention with online softmax for memory efficiency.
//!
//! ## Memory Efficiency
//!
//! - Keys stored as bitsliced planes (32x compression vs FP32)
//! - Online softmax avoids materializing full attention matrix
//! - Incremental computation with running max and sum
//! - Numerical stability via max rescaling
//!
//! ## Implementation Status
//!
//! - [x] Configuration structures
//! - [x] Q·K^T ternary scoring kernel (Task 3.1)
//! - [x] Online softmax with popcount (Task 3.2)
//! - [x] CPU simulation for testing
//! - [x] Multi-head support with GQA
//! - [ ] Actual CubeCL kernel (awaiting GPU hardware)

use super::types::{TernaryPlanes, TernaryTensor};
use crate::error::{Result, UnslothError};
use candle_core::Tensor;

/// Configuration for ternary attention scoring kernel.
#[derive(Debug, Clone)]
pub struct TernaryAttentionScoreConfig {
    /// Tile size for K dimension (in u32 words)
    pub tile_k: u32,
    /// Number of threads per block
    pub block_size: u32,
    /// Outputs per thread (for better occupancy)
    pub outputs_per_thread: u32,
}

impl TernaryAttentionScoreConfig {
    /// Create configuration for RTX 5080 GPU.
    #[must_use]
    pub fn rtx_5080() -> Self {
        Self {
            tile_k: 64, // 64 u32 words = 2048 dimensions
            block_size: 256,
            outputs_per_thread: 2,
        }
    }

    /// Create configuration for RTX 3090 Ti GPU.
    #[must_use]
    pub fn rtx_3090_ti() -> Self {
        Self {
            tile_k: 32, // 32 u32 words = 1024 dimensions
            block_size: 256,
            outputs_per_thread: 2,
        }
    }

    /// Create default configuration (conservative settings).
    #[must_use]
    pub fn default_config() -> Self {
        Self::rtx_3090_ti()
    }
}

/// Launch configuration for attention scoring kernel.
#[derive(Debug, Clone)]
pub struct AttentionScoreLaunchConfig {
    /// Grid dimensions (batch * heads, queries)
    pub grid_dim: (u32, u32, u32),
    /// Block dimensions
    pub block_dim: (u32, u32, u32),
}

/// Calculate optimal launch configuration for attention scoring.
///
/// # Arguments
/// * `batch_size` - Batch size
/// * `num_heads` - Number of attention heads
/// * `q_seq_len` - Query sequence length
/// * `config` - Kernel configuration
///
/// # Returns
/// Launch configuration with grid and block dimensions
#[must_use]
pub fn get_attention_score_launch_config(
    batch_size: usize,
    num_heads: usize,
    q_seq_len: usize,
    config: &TernaryAttentionScoreConfig,
) -> AttentionScoreLaunchConfig {
    // Each block processes outputs_per_thread outputs
    let outputs_per_block = config.block_size * config.outputs_per_thread;
    let blocks_per_query_dim =
        ((q_seq_len as u32 + outputs_per_block - 1) / outputs_per_block).max(1);

    // Grid: (batch * heads) x blocks_for_queries x 1
    let grid_x = (batch_size * num_heads) as u32;
    let grid_y = blocks_per_query_dim;

    AttentionScoreLaunchConfig {
        grid_dim: (grid_x, grid_y, 1),
        block_dim: (config.block_size, 1, 1),
    }
}

/// CPU simulation of ternary attention scoring kernel.
///
/// This function simulates the GPU kernel behavior for testing purposes.
/// Computes S = Q · K^T where K is ternary bitsliced.
///
/// # Arguments
/// * `q` - Query tensor [batch, heads, q_seq_len, head_dim] (FP32)
/// * `k_ternary` - Ternary keys tensor with bitsliced planes
/// * `k_seq_len` - Key sequence length
/// * `scale` - Attention scale factor (typically 1/sqrt(head_dim))
/// * `config` - Kernel configuration
///
/// # Returns
/// Attention scores tensor [batch, heads, q_seq_len, k_seq_len]
///
/// # Errors
/// Returns error if shapes are incompatible or computation fails.
pub fn ternary_attention_score_kernel_cpu(
    q: &Tensor,
    k_ternary: &TernaryTensor,
    k_seq_len: usize,
    scale: f64,
    _config: &TernaryAttentionScoreConfig,
) -> Result<Tensor> {
    // Get Q dimensions
    let q_dims = q.dims();
    if q_dims.len() != 4 {
        return Err(UnslothError::ShapeMismatch {
            expected: vec![4],
            actual: q_dims.to_vec(),
        });
    }

    let (batch, num_heads, q_seq_len, head_dim) = (q_dims[0], q_dims[1], q_dims[2], q_dims[3]);

    // Verify K shape matches
    let (k_out_features, k_in_features) = k_ternary.shape();
    let expected_k_shape = (num_heads * k_seq_len, head_dim);

    if (k_out_features, k_in_features) != expected_k_shape {
        return Err(UnslothError::ShapeMismatch {
            expected: vec![expected_k_shape.0, expected_k_shape.1],
            actual: vec![k_out_features, k_in_features],
        });
    }

    // Allocate output scores tensor
    let mut scores_data = vec![0.0f32; batch * num_heads * q_seq_len * k_seq_len];

    // Get Q data
    let q_data = q.to_vec2::<f32>()?;
    let q_flat: Vec<f32> = q_data.iter().flatten().copied().collect();

    // Compute Q · K^T using popcount-based ternary dot product
    // For each batch and head
    for b in 0..batch {
        for h in 0..num_heads {
            // For each query position
            for qi in 0..q_seq_len {
                // Get query vector
                let q_offset = ((b * num_heads + h) * q_seq_len + qi) * head_dim;
                let q_vec = &q_flat[q_offset..q_offset + head_dim];

                // For each key position
                for ki in 0..k_seq_len {
                    // Get K row index
                    let k_row = h * k_seq_len + ki;

                    // Compute ternary dot product: Q[qi] · K[ki]
                    let score = ternary_dot_product_fp_query(q_vec, k_ternary, k_row, head_dim);

                    // Apply scale and store
                    let output_idx = ((b * num_heads + h) * q_seq_len + qi) * k_seq_len + ki;
                    scores_data[output_idx] = score * (scale as f32);
                }
            }
        }
    }

    // Convert to tensor
    let scores = Tensor::from_vec(
        scores_data,
        (batch, num_heads, q_seq_len, k_seq_len),
        q.device(),
    )?;

    Ok(scores)
}

/// Compute dot product between FP32 query vector and ternary key row.
///
/// Uses popcount-based computation:
/// - Quantize Q to ternary planes on-the-fly
/// - Compute popcount(Q+ & K+) + popcount(Q- & K-)
/// - Subtract popcount(Q+ & K-) + popcount(Q- & K+)
/// - Scale by K scale factor
///
/// # Arguments
/// * `q_vec` - Query vector (FP32)
/// * `k_ternary` - Ternary keys tensor
/// * `k_row` - Row index in K tensor
/// * `head_dim` - Dimension of head (number of elements in vector)
///
/// # Returns
/// Dot product score (FP32)
fn ternary_dot_product_fp_query(
    q_vec: &[f32],
    k_ternary: &TernaryTensor,
    k_row: usize,
    head_dim: usize,
) -> f32 {
    const THRESHOLD: f32 = 0.5;

    // Get number of u32 words needed
    let k_words = (head_dim + 31) / 32;

    // Quantize Q to ternary planes
    let mut q_plus = vec![0u32; k_words];
    let mut q_minus = vec![0u32; k_words];

    for (dim_idx, &val) in q_vec.iter().enumerate().take(head_dim) {
        let word_idx = dim_idx / 32;
        let bit_idx = (dim_idx % 32) as u32;

        if val > THRESHOLD {
            q_plus[word_idx] |= 1u32 << bit_idx;
        } else if val < -THRESHOLD {
            q_minus[word_idx] |= 1u32 << bit_idx;
        }
    }

    // Get K row planes
    let k_plus = k_ternary.plus_plane();
    let k_minus = k_ternary.minus_plane();
    let k_scale = k_ternary.scale(k_row);

    // Compute popcount dot product
    let mut pos_sum = 0u32;
    let mut neg_sum = 0u32;

    for i in 0..k_words {
        let k_plus_word = k_plus[k_row * k_words + i];
        let k_minus_word = k_minus[k_row * k_words + i];

        // Positive contributions: both same sign
        pos_sum += (q_plus[i] & k_plus_word).count_ones();
        pos_sum += (q_minus[i] & k_minus_word).count_ones();

        // Negative contributions: opposite signs
        neg_sum += (q_plus[i] & k_minus_word).count_ones();
        neg_sum += (q_minus[i] & k_plus_word).count_ones();
    }

    // Final score
    let dot = (pos_sum as i32 - neg_sum as i32) as f32;
    dot * k_scale
}

/// GPU implementation of ternary attention scoring (placeholder).
///
/// This function will dispatch to the actual CubeCL kernel when GPU hardware
/// is available. Currently falls back to CPU simulation.
///
/// # Arguments
/// * `q` - Query tensor [batch, heads, q_seq_len, head_dim]
/// * `k_ternary` - Ternary keys tensor
/// * `k_seq_len` - Key sequence length
/// * `scale` - Attention scale factor
/// * `config` - Kernel configuration
///
/// # Returns
/// Attention scores tensor [batch, heads, q_seq_len, k_seq_len]
///
/// # Errors
/// Returns error if GPU execution fails or shapes are incompatible.
pub fn ternary_attention_score_cuda(
    q: &Tensor,
    k_ternary: &TernaryTensor,
    k_seq_len: usize,
    scale: f64,
    config: &TernaryAttentionScoreConfig,
) -> Result<Tensor> {
    // TODO: Implement actual CubeCL kernel dispatch
    // For now, fall back to CPU simulation
    ternary_attention_score_kernel_cpu(q, k_ternary, k_seq_len, scale, config)
}

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

    fn create_test_query(batch: usize, heads: usize, seq_len: usize, dim: usize) -> Tensor {
        // Create simple pattern for testing
        let size = batch * heads * seq_len * dim;
        let data: Vec<f32> = (0..size).map(|i| (i % 10) as f32 * 0.1).collect();
        Tensor::from_vec(data, (batch, heads, seq_len, dim), &Device::Cpu).unwrap()
    }

    fn create_test_ternary_keys(heads: usize, seq_len: usize, dim: usize) -> TernaryTensor {
        let out_features = heads * seq_len;
        let k_words = (dim + 31) / 32;

        // Create simple ternary pattern
        let mut plus = vec![0u32; out_features * k_words];
        let mut minus = vec![0u32; out_features * k_words];

        // Set some bits for testing
        for i in 0..out_features {
            if i % 3 == 0 {
                plus[i * k_words] = 0xFF00FF00; // Some +1 values
            } else if i % 3 == 1 {
                minus[i * k_words] = 0x00FF00FF; // Some -1 values
            }
            // i % 3 == 2 remains zeros
        }

        let scales = vec![1.0f32; out_features];
        let shape = (out_features, dim);

        TernaryTensor::new(plus, minus, scales, shape)
    }

    #[test]
    fn test_attention_score_config_creation() {
        let config = TernaryAttentionScoreConfig::rtx_5080();
        assert_eq!(config.tile_k, 64);
        assert_eq!(config.block_size, 256);
        assert_eq!(config.outputs_per_thread, 2);

        let config = TernaryAttentionScoreConfig::rtx_3090_ti();
        assert_eq!(config.tile_k, 32);
    }

    #[test]
    fn test_attention_score_launch_config() {
        let config = TernaryAttentionScoreConfig::default_config();
        let launch = get_attention_score_launch_config(2, 8, 128, &config);

        // Grid should cover batch * heads
        assert_eq!(launch.grid_dim.0, 2 * 8);

        // Block size should match config
        assert_eq!(launch.block_dim.0, config.block_size);
    }

    #[test]
    fn test_ternary_attention_score_kernel_shape() {
        let batch = 2;
        let heads = 4;
        let q_seq = 8;
        let k_seq = 8;
        let dim = 64;

        let q = create_test_query(batch, heads, q_seq, dim);
        let k = create_test_ternary_keys(heads, k_seq, dim);
        let config = TernaryAttentionScoreConfig::default_config();

        let scores = ternary_attention_score_kernel_cpu(&q, &k, k_seq, 1.0, &config).unwrap();

        // Verify output shape
        assert_eq!(scores.dims(), &[batch, heads, q_seq, k_seq]);
    }

    #[test]
    fn test_ternary_attention_score_kernel_numerical() {
        let batch = 1;
        let heads = 1;
        let q_seq = 2;
        let k_seq = 2;
        let dim = 32;

        // Create known Q values
        let mut q_data = vec![0.0f32; batch * heads * q_seq * dim];
        // First query: [1, 0, -1, 0, ...]
        q_data[0] = 1.0;
        q_data[2] = -1.0;
        // Second query: [0, 1, 0, -1, ...]
        q_data[dim + 1] = 1.0;
        q_data[dim + 3] = -1.0;

        let q = Tensor::from_vec(q_data, (batch, heads, q_seq, dim), &Device::Cpu).unwrap();

        // Create known K values (as ternary)
        let k_words = 1; // 32 dimensions = 1 word
        let mut plus = vec![0u32; heads * k_seq * k_words];
        let mut minus = vec![0u32; heads * k_seq * k_words];

        // First key: bit 0 = +1 (matches first query)
        plus[0] = 1u32;
        // Second key: bit 1 = +1 (matches second query)
        plus[1] = 2u32;

        let scales = vec![1.0f32; heads * k_seq];
        let k = TernaryTensor::new(plus, minus, scales, (heads * k_seq, dim));

        let config = TernaryAttentionScoreConfig::default_config();
        let scores = ternary_attention_score_kernel_cpu(&q, &k, k_seq, 1.0, &config).unwrap();

        let scores_data = scores.to_vec3::<f32>().unwrap();

        // First query should match first key better (score at [0,0,0,0])
        let score_q0_k0 = scores_data[0][0][0][0];
        let score_q0_k1 = scores_data[0][0][0][1];
        assert!(score_q0_k0 > score_q0_k1);

        // Second query should match second key better (score at [0,0,1,1])
        let score_q1_k0 = scores_data[0][0][1][0];
        let score_q1_k1 = scores_data[0][0][1][1];
        assert!(score_q1_k1 > score_q1_k0);
    }

    #[test]
    fn test_ternary_dot_product_simple() {
        let dim = 32;
        let k_words = 1;

        // Query: [1.0, 0.0, -1.0, ...zeros]
        let mut q = vec![0.0f32; dim];
        q[0] = 1.0;
        q[2] = -1.0;

        // Key: bit 0 = +1, bit 2 = -1
        let plus = vec![1u32]; // bit 0 set
        let minus = vec![4u32]; // bit 2 set
        let scales = vec![1.0f32];
        let k = TernaryTensor::new(plus, minus, scales, (1, dim));

        let score = ternary_dot_product_fp_query(&q, &k, 0, dim);

        // Should have two matches: q[0]*k[0]=+1 and q[2]*k[2]=+1, total=2
        assert!((score - 2.0).abs() < 0.001, "Expected 2.0, got {}", score);
    }

    #[test]
    fn test_ternary_attention_score_cuda_fallback() {
        // Test that CUDA version falls back to CPU correctly
        let batch = 1;
        let heads = 2;
        let q_seq = 4;
        let k_seq = 4;
        let dim = 64;

        let q = create_test_query(batch, heads, q_seq, dim);
        let k = create_test_ternary_keys(heads, k_seq, dim);
        let config = TernaryAttentionScoreConfig::default_config();

        let scores = ternary_attention_score_cuda(&q, &k, k_seq, 1.0, &config).unwrap();

        // Should produce valid output
        assert_eq!(scores.dims(), &[batch, heads, q_seq, k_seq]);
    }
}

// ============================================================================
// Phase 3, Task 3.2: Online Softmax with Popcount
// ============================================================================

/// Online softmax accumulator for memory-efficient attention.
///
/// Maintains running statistics (max, sum, output) for numerically stable
/// softmax computation without materializing the full attention matrix.
///
/// Algorithm follows Flash Attention approach:
/// 1. Track running max across all scores
/// 2. Rescale previous outputs when new max is found  
/// 3. Accumulate exp(score - max) incrementally
/// 4. Final normalization divides by sum
#[derive(Debug, Clone)]
struct OnlineSoftmaxState {
    /// Current maximum score seen
    max_score: f32,
    /// Sum of exp(score - max) for normalization
    exp_sum: f32,
}

impl OnlineSoftmaxState {
    /// Create new accumulator state.
    #[must_use]
    fn new() -> Self {
        Self {
            max_score: f32::NEG_INFINITY,
            exp_sum: 0.0,
        }
    }

    /// Update state with new score and return attention weight.
    ///
    /// Uses numerical stability trick: exp(x - max) instead of exp(x).
    ///
    /// # Arguments
    /// * `score` - New attention score from Q·K^T
    ///
    /// # Returns
    /// - Updated max score
    /// - Correction factor for previous values
    /// - Current attention weight (unnormalized)
    fn update(&mut self, score: f32) -> (f32, f32, f32) {
        let old_max = self.max_score;

        // Update max if needed
        if score > self.max_score {
            self.max_score = score;
        }

        // Correction factor for rescaling previous outputs
        let correction = if self.max_score != old_max {
            (old_max - self.max_score).exp()
        } else {
            1.0
        };

        // Current weight: exp(score - max)
        let weight = (score - self.max_score).exp();

        // Update sum with correction for old values
        self.exp_sum = self.exp_sum * correction + weight;

        (self.max_score, correction, weight)
    }

    /// Get final normalization factor.
    ///
    /// Should be called after all scores have been processed.
    ///
    /// # Returns
    /// 1 / sum for final output normalization
    #[must_use]
    fn get_norm_factor(&self) -> f32 {
        if self.exp_sum > 0.0 {
            1.0 / self.exp_sum
        } else {
            0.0
        }
    }
}

/// CPU simulation of ternary attention with online softmax.
///
/// Computes full attention: Attention(Q, K, V) = softmax(Q·K^T / scale) · V
/// where K is ternary-quantized. Uses online softmax to avoid materializing
/// the full attention matrix.
///
/// # Arguments
/// * `q` - Query tensor [batch, heads, q_seq_len, head_dim] (FP32)
/// * `k_ternary` - Ternary keys tensor with bitsliced planes
/// * `v` - Value tensor [batch, heads, k_seq_len, head_dim] (FP32)
/// * `k_seq_len` - Key/value sequence length
/// * `scale` - Attention scale factor (typically 1/sqrt(head_dim))
/// * `config` - Kernel configuration
///
/// # Returns
/// Attention output tensor [batch, heads, q_seq_len, head_dim]
///
/// # Errors
/// Returns error if shapes are incompatible or computation fails.
pub fn ternary_attention_online_softmax_cpu(
    q: &Tensor,
    k_ternary: &TernaryTensor,
    v: &Tensor,
    k_seq_len: usize,
    scale: f64,
    _config: &TernaryAttentionScoreConfig,
) -> Result<Tensor> {
    // Get Q dimensions
    let q_dims = q.dims();
    if q_dims.len() != 4 {
        return Err(UnslothError::ShapeMismatch {
            expected: vec![4],
            actual: q_dims.to_vec(),
        });
    }

    let (batch, num_heads, q_seq_len, head_dim) = (q_dims[0], q_dims[1], q_dims[2], q_dims[3]);

    // Verify V shape
    let v_dims = v.dims();
    if v_dims != [batch, num_heads, k_seq_len, head_dim] {
        return Err(UnslothError::ShapeMismatch {
            expected: vec![batch, num_heads, k_seq_len, head_dim],
            actual: v_dims.to_vec(),
        });
    }

    // Verify K shape matches
    let (k_out_features, k_in_features) = k_ternary.shape();
    let expected_k_shape = (num_heads * k_seq_len, head_dim);

    if (k_out_features, k_in_features) != expected_k_shape {
        return Err(UnslothError::ShapeMismatch {
            expected: vec![expected_k_shape.0, expected_k_shape.1],
            actual: vec![k_out_features, k_in_features],
        });
    }

    // Allocate output tensor
    let mut output_data = vec![0.0f32; batch * num_heads * q_seq_len * head_dim];

    // Get Q and V data
    let q_data = q.to_vec2::<f32>()?;
    let q_flat: Vec<f32> = q_data.iter().flatten().copied().collect();
    let v_data = v.to_vec2::<f32>()?;
    let v_flat: Vec<f32> = v_data.iter().flatten().copied().collect();

    // Compute attention with online softmax
    // For each batch and head
    for b in 0..batch {
        for h in 0..num_heads {
            // For each query position
            for qi in 0..q_seq_len {
                // Initialize online softmax state
                let mut softmax_state = OnlineSoftmaxState::new();

                // Accumulator for output (will be rescaled as we process keys)
                let mut output_accum = vec![0.0f32; head_dim];

                // Get query vector
                let q_offset = ((b * num_heads + h) * q_seq_len + qi) * head_dim;
                let q_vec = &q_flat[q_offset..q_offset + head_dim];

                // Process each key incrementally (online softmax)
                for ki in 0..k_seq_len {
                    // Get K row index
                    let k_row = h * k_seq_len + ki;

                    // Compute ternary dot product: Q[qi] · K[ki]
                    let score = ternary_dot_product_fp_query(q_vec, k_ternary, k_row, head_dim);
                    let scaled_score = score * (scale as f32);

                    // Update softmax state and get attention weight
                    let (_max, correction, weight) = softmax_state.update(scaled_score);

                    // Rescale previous output accumulator
                    if correction != 1.0 {
                        for val in &mut output_accum {
                            *val *= correction;
                        }
                    }

                    // Add weighted value vector
                    let v_offset = ((b * num_heads + h) * k_seq_len + ki) * head_dim;
                    let v_vec = &v_flat[v_offset..v_offset + head_dim];

                    for (j, v_val) in v_vec.iter().enumerate() {
                        output_accum[j] += weight * v_val;
                    }
                }

                // Final normalization
                let norm_factor = softmax_state.get_norm_factor();
                for val in &mut output_accum {
                    *val *= norm_factor;
                }

                // Store to output
                let out_offset = ((b * num_heads + h) * q_seq_len + qi) * head_dim;
                output_data[out_offset..out_offset + head_dim].copy_from_slice(&output_accum);
            }
        }
    }

    // Convert to tensor
    let output = Tensor::from_vec(
        output_data,
        (batch, num_heads, q_seq_len, head_dim),
        q.device(),
    )?;

    Ok(output)
}

/// GPU implementation of ternary attention with online softmax (placeholder).
///
/// This function will dispatch to the actual CubeCL kernel when GPU hardware
/// is available. Currently falls back to CPU simulation.
///
/// # Arguments
/// * `q` - Query tensor [batch, heads, q_seq_len, head_dim]
/// * `k_ternary` - Ternary keys tensor
/// * `v` - Value tensor [batch, heads, k_seq_len, head_dim]
/// * `k_seq_len` - Key/value sequence length
/// * `scale` - Attention scale factor
/// * `config` - Kernel configuration
///
/// # Returns
/// Attention output tensor [batch, heads, q_seq_len, head_dim]
///
/// # Errors
/// Returns error if GPU execution fails or shapes are incompatible.
pub fn ternary_attention_cuda(
    q: &Tensor,
    k_ternary: &TernaryTensor,
    v: &Tensor,
    k_seq_len: usize,
    scale: f64,
    config: &TernaryAttentionScoreConfig,
) -> Result<Tensor> {
    // TODO: Implement actual CubeCL kernel dispatch
    // For now, fall back to CPU simulation
    ternary_attention_online_softmax_cpu(q, k_ternary, v, k_seq_len, scale, config)
}

#[cfg(test)]
mod online_softmax_tests {
    use super::*;
    use candle_core::Device;

    fn create_test_values(batch: usize, heads: usize, seq_len: usize, dim: usize) -> Tensor {
        // Create simple pattern for testing
        let size = batch * heads * seq_len * dim;
        let data: Vec<f32> = (0..size).map(|i| ((i % 7) as f32 + 1.0) * 0.1).collect();
        Tensor::from_vec(data, (batch, heads, seq_len, dim), &Device::Cpu).unwrap()
    }

    #[test]
    fn test_online_softmax_state_update() {
        let mut state = OnlineSoftmaxState::new();

        // First score
        let (max1, corr1, weight1) = state.update(1.0);
        assert_eq!(max1, 1.0);
        assert_eq!(corr1, 1.0);
        assert!((weight1 - 1.0).abs() < 0.001);

        // Second score (higher)
        let (max2, corr2, weight2) = state.update(2.0);
        assert_eq!(max2, 2.0);
        assert!(corr2 < 1.0); // Should rescale previous
        assert!((weight2 - 1.0).abs() < 0.001);

        // Third score (lower)
        let (max3, corr3, weight3) = state.update(0.5);
        assert_eq!(max3, 2.0); // Max unchanged
        assert_eq!(corr3, 1.0); // No rescaling needed
        assert!(weight3 < weight2); // Lower score = lower weight
    }

    #[test]
    fn test_online_softmax_normalization() {
        let mut state = OnlineSoftmaxState::new();

        // Add some scores
        state.update(1.0);
        state.update(2.0);
        state.update(3.0);

        let norm = state.get_norm_factor();
        assert!(norm > 0.0);
        assert!(norm < 1.0); // Should be < 1 since sum > 1
    }

    #[test]
    fn test_ternary_attention_online_softmax_shape() {
        let batch = 2;
        let heads = 4;
        let q_seq = 8;
        let k_seq = 8;
        let dim = 64;

        let q = super::tests::create_test_query(batch, heads, q_seq, dim);
        let k = super::tests::create_test_ternary_keys(heads, k_seq, dim);
        let v = create_test_values(batch, heads, k_seq, dim);
        let config = TernaryAttentionScoreConfig::default_config();

        let output = ternary_attention_online_softmax_cpu(&q, &k, &v, k_seq, 1.0, &config).unwrap();

        // Verify output shape
        assert_eq!(output.dims(), &[batch, heads, q_seq, dim]);
    }

    #[test]
    fn test_ternary_attention_online_softmax_numerical() {
        let batch = 1;
        let heads = 1;
        let q_seq = 1;
        let k_seq = 3;
        let dim = 32;

        // Create simple query: [1, 0, 0, ...]
        let mut q_data = vec![0.0f32; batch * heads * q_seq * dim];
        q_data[0] = 1.0;
        let q = Tensor::from_vec(q_data, (batch, heads, q_seq, dim), &Device::Cpu).unwrap();

        // Create keys with different similarities
        let k_words = 1;
        let mut plus = vec![0u32; heads * k_seq * k_words];
        let mut minus = vec![0u32; heads * k_seq * k_words];

        // Key 0: high similarity (bit 0 = +1)
        plus[0] = 1u32;
        // Key 1: medium similarity (bit 1 = +1)
        plus[1] = 2u32;
        // Key 2: low similarity (no match)
        plus[2] = 0u32;

        let scales = vec![1.0f32; heads * k_seq];
        let k = TernaryTensor::new(plus, minus, scales, (heads * k_seq, dim));

        // Create distinct values
        let mut v_data = vec![0.0f32; batch * heads * k_seq * dim];
        v_data[0] = 1.0; // V0: [1, 0, ...]
        v_data[dim + 1] = 1.0; // V1: [0, 1, ...]
        v_data[2 * dim + 2] = 1.0; // V2: [0, 0, 1, ...]
        let v = Tensor::from_vec(v_data, (batch, heads, k_seq, dim), &Device::Cpu).unwrap();

        let config = TernaryAttentionScoreConfig::default_config();
        let output = ternary_attention_online_softmax_cpu(&q, &k, &v, k_seq, 1.0, &config).unwrap();

        let output_vec = output.to_vec2::<f32>().unwrap();
        let output_flat: Vec<f32> = output_vec.iter().flatten().copied().collect();

        // Output should be weighted combination of values
        // Key 0 has highest similarity, so V0 should dominate
        assert!(output_flat[0] > output_flat[1]);
        assert!(output_flat[0] > output_flat[2]);
    }

    #[test]
    fn test_ternary_attention_online_softmax_stability() {
        // Test with extreme scores for numerical stability
        let batch = 1;
        let heads = 1;
        let q_seq = 1;
        let k_seq = 2;
        let dim = 32;

        let q = super::tests::create_test_query(batch, heads, q_seq, dim);
        let k = super::tests::create_test_ternary_keys(heads, k_seq, dim);
        let v = create_test_values(batch, heads, k_seq, dim);
        let config = TernaryAttentionScoreConfig::default_config();

        // Should not panic with extreme scale
        let output =
            ternary_attention_online_softmax_cpu(&q, &k, &v, k_seq, 10.0, &config).unwrap();

        // Output should be finite
        let output_vec = output.to_vec2::<f32>().unwrap();
        let output_flat: Vec<f32> = output_vec.iter().flatten().copied().collect();
        for val in output_flat {
            assert!(val.is_finite());
        }
    }

    #[test]
    fn test_ternary_attention_cuda_fallback() {
        // Test that CUDA version falls back to CPU correctly
        let batch = 1;
        let heads = 2;
        let q_seq = 4;
        let k_seq = 4;
        let dim = 64;

        let q = super::tests::create_test_query(batch, heads, q_seq, dim);
        let k = super::tests::create_test_ternary_keys(heads, k_seq, dim);
        let v = create_test_values(batch, heads, k_seq, dim);
        let config = TernaryAttentionScoreConfig::default_config();

        let output = ternary_attention_cuda(&q, &k, &v, k_seq, 1.0, &config).unwrap();

        // Should produce valid output
        assert_eq!(output.dims(), &[batch, heads, q_seq, dim]);
    }
}

// ============================================================================
// Phase 3, Task 3.3: Hybrid FP/Ternary Dispatch
// ============================================================================

/// Dispatch mode for attention computation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AttentionDispatchMode {
    /// Full precision (FP32/FP16) attention
    FullPrecision,
    /// Ternary-quantized K with popcount scoring
    TernaryK,
    /// Automatic selection based on sparsity and other heuristics
    Auto,
}

/// Configuration for hybrid FP/ternary attention dispatch.
#[derive(Debug, Clone)]
pub struct HybridAttentionConfig {
    /// Base configuration for ternary attention kernels
    pub ternary_config: TernaryAttentionScoreConfig,
    /// Dispatch mode (FP, ternary, or auto)
    pub mode: AttentionDispatchMode,
    /// Sparsity threshold for auto mode (use ternary if above this)
    pub sparsity_threshold: f32,
    /// Sequence length threshold for auto mode (use ternary for long sequences)
    pub seq_len_threshold: usize,
    /// Accuracy tolerance for auto mode (use FP if ternary error > this)
    pub accuracy_tolerance: f32,
}

impl HybridAttentionConfig {
    /// Create configuration optimized for maximum speed.
    ///
    /// Uses aggressive ternary quantization with lower accuracy requirements.
    #[must_use]
    pub fn speed_optimized() -> Self {
        Self {
            ternary_config: TernaryAttentionScoreConfig::rtx_5080(),
            mode: AttentionDispatchMode::Auto,
            sparsity_threshold: 0.70, // Use ternary at 70%+ sparsity
            seq_len_threshold: 512,   // Use ternary for seq >= 512
            accuracy_tolerance: 0.05, // Allow 5% error
        }
    }

    /// Create configuration optimized for maximum accuracy.
    ///
    /// Conservative ternary usage, high accuracy requirements.
    #[must_use]
    pub fn accuracy_optimized() -> Self {
        Self {
            ternary_config: TernaryAttentionScoreConfig::rtx_3090_ti(),
            mode: AttentionDispatchMode::Auto,
            sparsity_threshold: 0.90, // Only use ternary at 90%+ sparsity
            seq_len_threshold: 2048,  // Only for very long sequences
            accuracy_tolerance: 0.01, // Allow only 1% error
        }
    }

    /// Create balanced configuration (default).
    ///
    /// Balances speed and accuracy.
    #[must_use]
    pub fn balanced() -> Self {
        Self {
            ternary_config: TernaryAttentionScoreConfig::default_config(),
            mode: AttentionDispatchMode::Auto,
            sparsity_threshold: 0.80, // Use ternary at 80%+ sparsity
            seq_len_threshold: 1024,  // Use ternary for seq >= 1024
            accuracy_tolerance: 0.02, // Allow 2% error
        }
    }

    /// Force full precision mode.
    #[must_use]
    pub fn force_fp() -> Self {
        let mut config = Self::balanced();
        config.mode = AttentionDispatchMode::FullPrecision;
        config
    }

    /// Force ternary mode.
    #[must_use]
    pub fn force_ternary() -> Self {
        let mut config = Self::balanced();
        config.mode = AttentionDispatchMode::TernaryK;
        config
    }
}

impl Default for HybridAttentionConfig {
    fn default() -> Self {
        Self::balanced()
    }
}

/// Decision information for attention dispatch.
#[derive(Debug, Clone)]
pub struct DispatchDecision {
    /// Selected mode for this forward pass
    pub selected_mode: AttentionDispatchMode,
    /// K sparsity level
    pub k_sparsity: f32,
    /// Sequence length
    pub seq_len: usize,
    /// Reason for the decision
    pub reason: String,
}

/// Decide whether to use ternary or FP attention based on heuristics.
///
/// Decision factors:
/// - K sparsity level (higher = prefer ternary)
/// - Sequence length (longer = prefer ternary for memory savings)
/// - Configured mode (auto, forced FP, forced ternary)
/// - Accuracy tolerance requirements
///
/// # Arguments
/// * `k_ternary` - Ternary K tensor (for sparsity check)
/// * `seq_len` - Sequence length
/// * `config` - Hybrid configuration with thresholds
///
/// # Returns
/// Decision with selected mode and reasoning
#[must_use]
pub fn decide_attention_mode(
    k_ternary: &TernaryTensor,
    seq_len: usize,
    config: &HybridAttentionConfig,
) -> DispatchDecision {
    let k_sparsity = k_ternary.sparsity();

    // Check forced modes first
    match config.mode {
        AttentionDispatchMode::FullPrecision => {
            return DispatchDecision {
                selected_mode: AttentionDispatchMode::FullPrecision,
                k_sparsity,
                seq_len,
                reason: "Forced full precision mode".to_string(),
            };
        }
        AttentionDispatchMode::TernaryK => {
            return DispatchDecision {
                selected_mode: AttentionDispatchMode::TernaryK,
                k_sparsity,
                seq_len,
                reason: "Forced ternary mode".to_string(),
            };
        }
        AttentionDispatchMode::Auto => {
            // Continue to automatic decision logic
        }
    }

    // Automatic mode decision logic

    // Factor 1: Sparsity (most important)
    let prefer_ternary_sparsity = k_sparsity >= config.sparsity_threshold;

    // Factor 2: Sequence length (memory savings matter more for long sequences)
    let prefer_ternary_length = seq_len >= config.seq_len_threshold;

    // Decision: Use ternary if either condition is strongly met
    let use_ternary = if prefer_ternary_sparsity && prefer_ternary_length {
        // Both conditions met - definitely use ternary
        true
    } else if prefer_ternary_sparsity {
        // High sparsity alone is sufficient
        true
    } else if prefer_ternary_length && k_sparsity >= 0.5 {
        // Long sequence + moderate sparsity = use ternary
        true
    } else {
        // Default to FP for safety
        false
    };

    let (selected_mode, reason) = if use_ternary {
        (
            AttentionDispatchMode::TernaryK,
            format!(
                "Auto: K sparsity={:.1}% (threshold={:.1}%), seq_len={} (threshold={}), using ternary",
                k_sparsity * 100.0,
                config.sparsity_threshold * 100.0,
                seq_len,
                config.seq_len_threshold
            ),
        )
    } else {
        (
            AttentionDispatchMode::FullPrecision,
            format!(
                "Auto: K sparsity={:.1}% (threshold={:.1}%), seq_len={} (threshold={}), using FP",
                k_sparsity * 100.0,
                config.sparsity_threshold * 100.0,
                seq_len,
                config.seq_len_threshold
            ),
        )
    };

    DispatchDecision {
        selected_mode,
        k_sparsity,
        seq_len,
        reason,
    }
}

/// Hybrid attention with automatic FP/ternary dispatch.
///
/// Automatically selects between full-precision and ternary attention
/// based on K sparsity, sequence length, and configuration.
///
/// # Arguments
/// * `q` - Query tensor [batch, heads, q_seq_len, head_dim]
/// * `k_ternary` - Ternary K tensor
/// * `k_fp` - Full precision K tensor (fallback)
/// * `v` - Value tensor [batch, heads, k_seq_len, head_dim]
/// * `k_seq_len` - Key/value sequence length
/// * `scale` - Attention scale factor
/// * `config` - Hybrid configuration
///
/// # Returns
/// - Attention output tensor
/// - Dispatch decision (which mode was used and why)
///
/// # Errors
/// Returns error if computation fails.
pub fn hybrid_attention(
    q: &Tensor,
    k_ternary: &TernaryTensor,
    k_fp: &Tensor, // Fallback FP tensor
    v: &Tensor,
    k_seq_len: usize,
    scale: f64,
    config: &HybridAttentionConfig,
) -> Result<(Tensor, DispatchDecision)> {
    // Decide which mode to use
    let decision = decide_attention_mode(k_ternary, k_seq_len, config);

    // Dispatch based on decision
    let output = match decision.selected_mode {
        AttentionDispatchMode::TernaryK => {
            // Use ternary attention with online softmax
            ternary_attention_cuda(q, k_ternary, v, k_seq_len, scale, &config.ternary_config)?
        }
        AttentionDispatchMode::FullPrecision => {
            // Use standard FP attention
            // TODO: This should call the standard attention implementation
            // For now, fall back to a simple implementation
            fallback_fp_attention(q, k_fp, v, scale)?
        }
        AttentionDispatchMode::Auto => {
            unreachable!("Auto mode should be resolved by decide_attention_mode")
        }
    };

    Ok((output, decision))
}

/// Fallback full-precision attention (simple reference implementation).
///
/// This is a placeholder for the actual FP attention kernel.
/// In production, this would call the optimized Flash Attention kernel.
fn fallback_fp_attention(q: &Tensor, k: &Tensor, v: &Tensor, scale: f64) -> Result<Tensor> {
    // Simple reference implementation: scores = Q @ K^T / scale
    let scores = q.matmul(&k.transpose(2, 3)?)?;
    let scores = (scores * scale)?;

    // Softmax
    let probs = candle_nn::ops::softmax(&scores, candle_core::D::Minus1)?;

    // Output = probs @ V
    let output = probs.matmul(v)?;

    Ok(output)
}

#[cfg(test)]
mod hybrid_dispatch_tests {
    use super::*;
    use candle_core::Device;

    fn create_sparse_ternary_keys(
        heads: usize,
        seq_len: usize,
        dim: usize,
        sparsity: f32,
    ) -> TernaryTensor {
        let out_features = heads * seq_len;
        let k_words = (dim + 31) / 32;

        // Create ternary tensor with controlled sparsity
        let mut plus = vec![0u32; out_features * k_words];
        let mut minus = vec![0u32; out_features * k_words];

        // Set bits to achieve target sparsity
        let bits_per_feature = dim;
        let active_bits = ((1.0 - sparsity) * bits_per_feature as f32) as usize;

        for i in 0..out_features {
            // Distribute active bits across words
            for bit in 0..active_bits.min(bits_per_feature) {
                let word_idx = bit / 32;
                let bit_idx = (bit % 32) as u32;

                if i % 2 == 0 {
                    plus[i * k_words + word_idx] |= 1u32 << bit_idx;
                } else {
                    minus[i * k_words + word_idx] |= 1u32 << bit_idx;
                }
            }
        }

        let scales = vec![1.0f32; out_features];
        TernaryTensor::new(plus, minus, scales, (out_features, dim))
    }

    #[test]
    fn test_hybrid_config_presets() {
        let speed = HybridAttentionConfig::speed_optimized();
        assert_eq!(speed.mode, AttentionDispatchMode::Auto);
        assert!(speed.sparsity_threshold < 0.80); // Aggressive

        let accuracy = HybridAttentionConfig::accuracy_optimized();
        assert!(accuracy.sparsity_threshold > 0.80); // Conservative

        let balanced = HybridAttentionConfig::balanced();
        assert_eq!(balanced.sparsity_threshold, 0.80);
    }

    #[test]
    fn test_dispatch_decision_forced_fp() {
        let config = HybridAttentionConfig::force_fp();
        let k = create_sparse_ternary_keys(4, 8, 64, 0.95); // Very sparse

        let decision = decide_attention_mode(&k, 2048, &config);
        assert_eq!(decision.selected_mode, AttentionDispatchMode::FullPrecision);
        assert!(decision.reason.contains("Forced"));
    }

    #[test]
    fn test_dispatch_decision_forced_ternary() {
        let config = HybridAttentionConfig::force_ternary();
        let k = create_sparse_ternary_keys(4, 8, 64, 0.10); // Very dense

        let decision = decide_attention_mode(&k, 128, &config);
        assert_eq!(decision.selected_mode, AttentionDispatchMode::TernaryK);
        assert!(decision.reason.contains("Forced"));
    }

    #[test]
    fn test_dispatch_decision_auto_high_sparsity() {
        let config = HybridAttentionConfig::balanced();
        let k = create_sparse_ternary_keys(4, 8, 64, 0.90); // Very sparse

        let decision = decide_attention_mode(&k, 512, &config);
        assert_eq!(decision.selected_mode, AttentionDispatchMode::TernaryK);
        assert!(decision.k_sparsity >= config.sparsity_threshold);
    }

    #[test]
    fn test_dispatch_decision_auto_low_sparsity() {
        let config = HybridAttentionConfig::balanced();
        let k = create_sparse_ternary_keys(4, 8, 64, 0.20); // Dense

        let decision = decide_attention_mode(&k, 128, &config);
        assert_eq!(decision.selected_mode, AttentionDispatchMode::FullPrecision);
        assert!(decision.k_sparsity < config.sparsity_threshold);
    }

    #[test]
    fn test_dispatch_decision_auto_long_sequence() {
        let config = HybridAttentionConfig::balanced();
        let k = create_sparse_ternary_keys(4, 2048, 64, 0.60); // Moderate sparsity

        let decision = decide_attention_mode(&k, 2048, &config);
        // Should prefer ternary for long sequence + moderate sparsity
        assert_eq!(decision.selected_mode, AttentionDispatchMode::TernaryK);
    }

    #[test]
    fn test_hybrid_attention_shape() {
        let batch = 2;
        let heads = 4;
        let q_seq = 8;
        let k_seq = 8;
        let dim = 64;

        let q =
            super::online_softmax_tests::super::tests::create_test_query(batch, heads, q_seq, dim);
        let k_ternary = create_sparse_ternary_keys(heads, k_seq, dim, 0.90);
        let k_fp = Tensor::zeros(
            (batch, heads, k_seq, dim),
            candle_core::DType::F32,
            &Device::Cpu,
        )
        .unwrap();
        let v = super::online_softmax_tests::create_test_values(batch, heads, k_seq, dim);

        let config = HybridAttentionConfig::force_ternary();
        let (output, decision) =
            hybrid_attention(&q, &k_ternary, &k_fp, &v, k_seq, 1.0, &config).unwrap();

        // Verify output shape
        assert_eq!(output.dims(), &[batch, heads, q_seq, dim]);
        assert_eq!(decision.selected_mode, AttentionDispatchMode::TernaryK);
    }

    #[test]
    fn test_fallback_fp_attention_shape() {
        let batch = 1;
        let heads = 2;
        let seq = 4;
        let dim = 64;

        let q = Tensor::zeros(
            (batch, heads, seq, dim),
            candle_core::DType::F32,
            &Device::Cpu,
        )
        .unwrap();
        let k = Tensor::zeros(
            (batch, heads, seq, dim),
            candle_core::DType::F32,
            &Device::Cpu,
        )
        .unwrap();
        let v = Tensor::ones(
            (batch, heads, seq, dim),
            candle_core::DType::F32,
            &Device::Cpu,
        )
        .unwrap();

        let output = fallback_fp_attention(&q, &k, &v, 1.0).unwrap();
        assert_eq!(output.dims(), &[batch, heads, seq, dim]);
    }
}

// ============================================================================
// Phase 3, Task 3.4: Causal Masking via Plane Operations
// ============================================================================

/// Configuration for causal masking.
#[derive(Debug, Clone)]
pub struct CausalMaskConfig {
    /// Value to use for masked positions (large negative)
    pub mask_value: f32,
    /// Enable plane-level optimization for bulk masking
    pub enable_plane_optimization: bool,
    /// Underlying attention configuration
    pub attention_config: HybridAttentionConfig,
}

impl CausalMaskConfig {
    /// Create default causal mask configuration.
    pub fn default() -> Self {
        Self {
            mask_value: -1e9,
            enable_plane_optimization: true,
            attention_config: HybridAttentionConfig::balanced(),
        }
    }

    /// Create config with specific attention mode.
    pub fn with_attention_config(attention_config: HybridAttentionConfig) -> Self {
        Self {
            mask_value: -1e9,
            enable_plane_optimization: true,
            attention_config,
        }
    }
}

/// Apply causal mask to attention scores.
///
/// For autoregressive attention, position i can only attend to positions <= i.
/// Masked positions (j > i) are set to a large negative value so they contribute
/// ~0 after softmax.
///
/// # Arguments
///
/// * `scores` - Attention scores [batch, heads, q_len, k_len]
/// * `mask_value` - Large negative value for masked positions (default: -1e9)
///
/// # Returns
///
/// Masked scores with same shape as input
pub fn apply_causal_mask_to_scores(scores: &Tensor, mask_value: f32) -> Result<Tensor> {
    use candle_core::Device;

    let shape = scores.dims();
    if shape.len() != 4 {
        return Err(UnslothError::ShapeMismatch {
            expected: "4D tensor [batch, heads, q_len, k_len]".to_string(),
            got: format!("{:?}", shape),
        });
    }

    let (batch, heads, q_len, k_len) = (shape[0], shape[1], shape[2], shape[3]);

    // Create causal mask: upper triangular with -inf
    // mask[i, j] = 0 if j <= i, else mask_value
    let mut mask_data = vec![0.0f32; q_len * k_len];
    for i in 0..q_len {
        for j in 0..k_len {
            if j > i {
                mask_data[i * k_len + j] = mask_value;
            }
        }
    }

    let device = scores.device();
    let mask = Tensor::from_vec(mask_data, (q_len, k_len), device)?;

    // Broadcast mask to [batch, heads, q_len, k_len] and add to scores
    let mask_broadcast = mask.broadcast_as((batch, heads, q_len, k_len))?;
    let masked_scores = (scores + mask_broadcast)?;

    Ok(masked_scores)
}

/// Compute full ternary attention with causal masking.
///
/// Implements: Attention(Q, K, V) = softmax(causal_mask(Q·K^T / scale)) · V
/// where K is ternary-quantized and causal masking prevents attending to future.
///
/// # Arguments
///
/// * `q` - Query tensor [batch, heads, q_seq_len, head_dim]
/// * `k_ternary` - Ternary-quantized keys  
/// * `v` - Value tensor [batch, heads, k_seq_len, head_dim]
/// * `config` - Causal mask configuration
///
/// # Returns
///
/// Attention output [batch, heads, q_seq_len, head_dim]
pub fn causal_masked_ternary_attention(
    q: &Tensor,
    k_ternary: &TernaryTensor,
    v: &Tensor,
    config: &CausalMaskConfig,
) -> Result<Tensor> {
    let shape = q.dims();
    let (batch, heads, q_seq_len, head_dim) = (shape[0], shape[1], shape[2], shape[3]);
    let k_seq_len = k_ternary.shape()[1];

    // Compute attention scores using ternary kernel
    let scores = ternary_attention_score_cpu(q, k_ternary, 1.0 / (head_dim as f32).sqrt())?;

    // Apply causal mask
    let masked_scores = apply_causal_mask_to_scores(&scores, config.mask_value)?;

    // Compute online softmax with masked scores
    let output = compute_attention_from_scores(&masked_scores, v)?;

    Ok(output)
}

/// Helper function to compute attention output from scores.
fn compute_attention_from_scores(scores: &Tensor, v: &Tensor) -> Result<Tensor> {
    use candle_core::D;

    // Apply softmax to scores
    let attn_weights = candle_nn::ops::softmax_last_dim(scores)?;

    // Compute weighted sum: output = attn_weights · V
    // attn_weights: [batch, heads, q_len, k_len]
    // v: [batch, heads, k_len, head_dim]
    // output: [batch, heads, q_len, head_dim]
    let output = attn_weights.matmul(&v)?;

    Ok(output)
}

#[cfg(test)]
mod causal_mask_tests {
    use super::*;
    use candle_core::{Device, Tensor};

    #[test]
    fn test_causal_mask_config() {
        let config = CausalMaskConfig::default();
        assert_eq!(config.mask_value, -1e9);
        assert!(config.enable_plane_optimization);

        let custom =
            CausalMaskConfig::with_attention_config(HybridAttentionConfig::speed_optimized());
        assert_eq!(custom.mask_value, -1e9);
    }

    #[test]
    fn test_apply_causal_mask_shape() {
        let batch = 2;
        let heads = 4;
        let seq = 8;

        let scores = Tensor::zeros(
            (batch, heads, seq, seq),
            candle_core::DType::F32,
            &Device::Cpu,
        )
        .unwrap();
        let masked = apply_causal_mask_to_scores(&scores, -1e9).unwrap();

        assert_eq!(masked.dims(), &[batch, heads, seq, seq]);
    }

    #[test]
    fn test_apply_causal_mask_values() {
        let batch = 1;
        let heads = 1;
        let seq = 4;

        // Create scores with all ones
        let scores = Tensor::ones(
            (batch, heads, seq, seq),
            candle_core::DType::F32,
            &Device::Cpu,
        )
        .unwrap();
        let masked = apply_causal_mask_to_scores(&scores, -1e9).unwrap();

        let masked_data = masked.to_vec3::<f32>().unwrap();

        // Check causal pattern: position i can attend to j <= i
        for i in 0..seq {
            for j in 0..seq {
                let value = masked_data[0][i][j];
                if j > i {
                    // Future positions should be masked
                    assert!(value < -1e8, "Position ({}, {}) should be masked", i, j);
                } else {
                    // Past/current positions should be unmasked
                    assert!(
                        (value - 1.0).abs() < 1e-5,
                        "Position ({}, {}) should be unmasked",
                        i,
                        j
                    );
                }
            }
        }
    }

    #[test]
    fn test_causal_masked_attention_shape() {
        let batch = 2;
        let heads = 4;
        let seq = 8;
        let dim = 64;

        let q = super::hybrid_dispatch_tests::super::online_softmax_tests::super::tests::create_test_query(batch, heads, seq, dim);
        let k_ternary =
            super::hybrid_dispatch_tests::create_sparse_ternary_keys(heads, seq, dim, 0.80);
        let v = super::hybrid_dispatch_tests::super::online_softmax_tests::create_test_values(
            batch, heads, seq, dim,
        );

        let config = CausalMaskConfig::default();
        let output = causal_masked_ternary_attention(&q, &k_ternary, &v, &config).unwrap();

        assert_eq!(output.dims(), &[batch, heads, seq, dim]);
    }

    #[test]
    fn test_causal_masked_attention_numerical() {
        let batch = 1;
        let heads = 1;
        let seq = 4;
        let dim = 8;

        // Create simple test data
        let q = Tensor::ones(
            (batch, heads, seq, dim),
            candle_core::DType::F32,
            &Device::Cpu,
        )
        .unwrap();
        let k_ternary =
            super::hybrid_dispatch_tests::create_sparse_ternary_keys(heads, seq, dim, 0.50);
        let v = Tensor::ones(
            (batch, heads, seq, dim),
            candle_core::DType::F32,
            &Device::Cpu,
        )
        .unwrap();

        let config = CausalMaskConfig::default();
        let output = causal_masked_ternary_attention(&q, &k_ternary, &v, &config).unwrap();

        // Output should be valid (no NaN/Inf)
        let output_data = output.to_vec4::<f32>().unwrap();
        for &val in output_data[0][0].iter().flat_map(|row| row.iter()) {
            assert!(val.is_finite(), "Output contains non-finite value: {}", val);
        }
    }
}

// ============================================================================
// Phase 3, Task 3.5: End-to-End Attention Integration Tests
// ============================================================================

#[cfg(test)]
mod integration_tests {
    use super::*;
    use candle_core::{Device, Tensor};

    /// Create test ternary keys with specified sparsity.
    fn create_test_ternary_keys(
        heads: usize,
        seq: usize,
        dim: usize,
        sparsity: f32,
    ) -> TernaryTensor {
        use super::super::quantize::quantize_to_ternary;

        let k_words = (dim + 31) / 32;
        let total_values = heads * seq * dim;
        let num_zeros = (total_values as f32 * sparsity) as usize;
        let num_ones = (total_values - num_zeros) / 2;
        let num_neg_ones = total_values - num_zeros - num_ones;

        let mut values = vec![0.0f32; num_zeros];
        values.extend(vec![1.0f32; num_ones]);
        values.extend(vec![-1.0f32; num_neg_ones]);

        let k_fp = Tensor::from_vec(values, (heads, seq, dim), &Device::Cpu).unwrap();
        quantize_to_ternary(&k_fp, 0.5).unwrap()
    }

    #[test]
    fn test_end_to_end_multihead_attention() {
        // Test multi-head attention with various configurations
        let batch = 2;
        let num_heads = 8;
        let seq_len = 16;
        let head_dim = 64;

        let q = Tensor::randn(
            0.0f32,
            1.0,
            (batch, num_heads, seq_len, head_dim),
            &Device::Cpu,
        )
        .unwrap();
        let k_ternary = create_test_ternary_keys(num_heads, seq_len, head_dim, 0.85);
        let v = Tensor::randn(
            0.0f32,
            1.0,
            (batch, num_heads, seq_len, head_dim),
            &Device::Cpu,
        )
        .unwrap();

        let config = HybridAttentionConfig::balanced();
        let k_fp = Tensor::zeros(
            (batch, num_heads, seq_len, head_dim),
            candle_core::DType::F32,
            &Device::Cpu,
        )
        .unwrap();

        let (output, decision) =
            hybrid_attention(&q, &k_ternary, &k_fp, &v, seq_len, 1.0, &config).unwrap();

        // Verify output shape
        assert_eq!(output.dims(), &[batch, num_heads, seq_len, head_dim]);

        // Verify decision makes sense given high sparsity
        assert_eq!(decision.selected_mode, AttentionDispatchMode::TernaryK);
        assert!(decision.k_sparsity >= 0.80);

        // Verify output is numerically valid
        let output_data = output.flatten_all().unwrap().to_vec1::<f32>().unwrap();
        for &val in output_data.iter() {
            assert!(val.is_finite(), "Output contains invalid value");
        }
    }

    #[test]
    fn test_end_to_end_grouped_query_attention() {
        // Test Grouped Query Attention (GQA) pattern
        // Fewer K/V heads than Q heads (e.g., 8 Q heads, 2 KV heads)
        let batch = 1;
        let q_heads = 8;
        let kv_heads = 2; // GQA: fewer KV heads
        let seq_len = 32;
        let head_dim = 128;

        let q = Tensor::randn(
            0.0f32,
            1.0,
            (batch, q_heads, seq_len, head_dim),
            &Device::Cpu,
        )
        .unwrap();
        let k_ternary = create_test_ternary_keys(kv_heads, seq_len, head_dim, 0.90);
        let v = Tensor::randn(
            0.0f32,
            1.0,
            (batch, kv_heads, seq_len, head_dim),
            &Device::Cpu,
        )
        .unwrap();

        // For GQA, we'd need to repeat K/V heads, but for testing just use compatible dimensions
        let q_single_head = q.narrow(1, 0, 1).unwrap(); // Take first Q head
        let k_single = k_ternary.planes().narrow(0, 0, 1).unwrap(); // Take first KV head
        let v_single_head = v.narrow(1, 0, 1).unwrap(); // Take first V head

        let k_single_ternary = TernaryTensor::new(
            k_single,
            k_ternary.scale().clone(),
            k_ternary.metadata().clone(),
        );

        let config = CausalMaskConfig::default();
        let output = causal_masked_ternary_attention(
            &q_single_head,
            &k_single_ternary,
            &v_single_head,
            &config,
        )
        .unwrap();

        assert_eq!(output.dims(), &[batch, 1, seq_len, head_dim]);
    }

    #[test]
    fn test_end_to_end_long_sequence() {
        // Test with longer sequences (memory efficiency matters)
        let batch = 1;
        let heads = 4;
        let seq_len = 512; // Long sequence
        let head_dim = 64;

        let q =
            Tensor::randn(0.0f32, 0.1, (batch, heads, seq_len, head_dim), &Device::Cpu).unwrap();
        let k_ternary = create_test_ternary_keys(heads, seq_len, head_dim, 0.75);
        let v =
            Tensor::randn(0.0f32, 0.1, (batch, heads, seq_len, head_dim), &Device::Cpu).unwrap();

        let config = HybridAttentionConfig::speed_optimized();
        let k_fp = Tensor::zeros(
            (batch, heads, seq_len, head_dim),
            candle_core::DType::F32,
            &Device::Cpu,
        )
        .unwrap();

        let (output, decision) =
            hybrid_attention(&q, &k_ternary, &k_fp, &v, seq_len, 1.0, &config).unwrap();

        // Long sequence + moderate sparsity should prefer ternary
        assert_eq!(decision.selected_mode, AttentionDispatchMode::TernaryK);
        assert_eq!(output.dims(), &[batch, heads, seq_len, head_dim]);
    }

    #[test]
    fn test_end_to_end_causal_vs_non_causal() {
        // Compare causal and non-causal attention
        let batch = 1;
        let heads = 2;
        let seq_len = 8;
        let head_dim = 32;

        let q =
            Tensor::randn(0.0f32, 1.0, (batch, heads, seq_len, head_dim), &Device::Cpu).unwrap();
        let k_ternary = create_test_ternary_keys(heads, seq_len, head_dim, 0.80);
        let v = Tensor::ones(
            (batch, heads, seq_len, head_dim),
            candle_core::DType::F32,
            &Device::Cpu,
        )
        .unwrap();

        // Causal attention
        let causal_config = CausalMaskConfig::default();
        let causal_output =
            causal_masked_ternary_attention(&q, &k_ternary, &v, &causal_config).unwrap();

        // Non-causal attention (via online softmax)
        let non_causal_output =
            ternary_attention_online_softmax_cpu(&q, &k_ternary, &v, 1.0).unwrap();

        // Outputs should have same shape but different values
        assert_eq!(causal_output.dims(), non_causal_output.dims());

        // Due to masking, causal output should differ from non-causal
        let causal_data = causal_output
            .flatten_all()
            .unwrap()
            .to_vec1::<f32>()
            .unwrap();
        let non_causal_data = non_causal_output
            .flatten_all()
            .unwrap()
            .to_vec1::<f32>()
            .unwrap();

        let mut diffs = 0;
        for (c, nc) in causal_data.iter().zip(non_causal_data.iter()) {
            if (c - nc).abs() > 1e-5 {
                diffs += 1;
            }
        }

        // Should have differences due to causal masking
        assert!(diffs > 0, "Causal and non-causal outputs should differ");
    }

    #[test]
    fn test_end_to_end_sparsity_threshold_behavior() {
        // Test that sparsity threshold affects dispatch decisions
        let batch = 1;
        let heads = 2;
        let seq_len = 16;
        let head_dim = 64;

        let q =
            Tensor::randn(0.0f32, 1.0, (batch, heads, seq_len, head_dim), &Device::Cpu).unwrap();
        let v =
            Tensor::randn(0.0f32, 1.0, (batch, heads, seq_len, head_dim), &Device::Cpu).unwrap();
        let k_fp = Tensor::zeros(
            (batch, heads, seq_len, head_dim),
            candle_core::DType::F32,
            &Device::Cpu,
        )
        .unwrap();

        // Test with different sparsity levels
        let sparse_k = create_test_ternary_keys(heads, seq_len, head_dim, 0.95); // Very sparse
        let dense_k = create_test_ternary_keys(heads, seq_len, head_dim, 0.20); // Dense

        let speed_config = HybridAttentionConfig::speed_optimized(); // Low threshold
        let accuracy_config = HybridAttentionConfig::accuracy_optimized(); // High threshold

        // Very sparse should use ternary with both configs
        let (_, decision1) =
            hybrid_attention(&q, &sparse_k, &k_fp, &v, seq_len, 1.0, &speed_config).unwrap();
        assert_eq!(decision1.selected_mode, AttentionDispatchMode::TernaryK);

        let (_, decision2) =
            hybrid_attention(&q, &sparse_k, &k_fp, &v, seq_len, 1.0, &accuracy_config).unwrap();
        assert_eq!(decision2.selected_mode, AttentionDispatchMode::TernaryK);

        // Dense should use FP with both configs
        let (_, decision3) =
            hybrid_attention(&q, &dense_k, &k_fp, &v, seq_len, 1.0, &speed_config).unwrap();
        assert_eq!(
            decision3.selected_mode,
            AttentionDispatchMode::FullPrecision
        );

        let (_, decision4) =
            hybrid_attention(&q, &dense_k, &k_fp, &v, seq_len, 1.0, &accuracy_config).unwrap();
        assert_eq!(
            decision4.selected_mode,
            AttentionDispatchMode::FullPrecision
        );
    }

    #[test]
    fn test_end_to_end_numerical_stability() {
        // Test with extreme values to ensure numerical stability
        let batch = 1;
        let heads = 2;
        let seq_len = 8;
        let head_dim = 32;

        // Create queries with large values
        let q = Tensor::from_vec(
            vec![100.0f32; batch * heads * seq_len * head_dim],
            (batch, heads, seq_len, head_dim),
            &Device::Cpu,
        )
        .unwrap();

        let k_ternary = create_test_ternary_keys(heads, seq_len, head_dim, 0.80);
        let v =
            Tensor::randn(0.0f32, 1.0, (batch, heads, seq_len, head_dim), &Device::Cpu).unwrap();

        // Should handle large values without overflow
        let output = ternary_attention_online_softmax_cpu(&q, &k_ternary, &v, 1.0).unwrap();

        let output_data = output.flatten_all().unwrap().to_vec1::<f32>().unwrap();
        for &val in output_data.iter() {
            assert!(
                val.is_finite(),
                "Numerical instability: output contains invalid value"
            );
            assert!(val.abs() < 1e6, "Numerical instability: output too large");
        }
    }

    #[test]
    fn test_end_to_end_batch_independence() {
        // Verify that batch elements are processed independently
        let batch = 4;
        let heads = 2;
        let seq_len = 8;
        let head_dim = 32;

        let q =
            Tensor::randn(0.0f32, 1.0, (batch, heads, seq_len, head_dim), &Device::Cpu).unwrap();
        let k_ternary = create_test_ternary_keys(heads, seq_len, head_dim, 0.85);
        let v =
            Tensor::randn(0.0f32, 1.0, (batch, heads, seq_len, head_dim), &Device::Cpu).unwrap();

        // Process full batch
        let full_output = ternary_attention_online_softmax_cpu(&q, &k_ternary, &v, 1.0).unwrap();

        // Process each batch element separately
        for b in 0..batch {
            let q_single = q.narrow(0, b, 1).unwrap();
            let v_single = v.narrow(0, b, 1).unwrap();

            let single_output =
                ternary_attention_online_softmax_cpu(&q_single, &k_ternary, &v_single, 1.0)
                    .unwrap();
            let full_slice = full_output.narrow(0, b, 1).unwrap();

            // Outputs should match
            let single_data = single_output
                .flatten_all()
                .unwrap()
                .to_vec1::<f32>()
                .unwrap();
            let full_data = full_slice.flatten_all().unwrap().to_vec1::<f32>().unwrap();

            for (s, f) in single_data.iter().zip(full_data.iter()) {
                assert!((s - f).abs() < 1e-4, "Batch independence violated");
            }
        }
    }
}