cera 0.5.0

Rust-native LLM inference engine
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
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
//! WGPU-accelerated audio detokenizer (spectrum).
//!
//! Mirrors `metal_audio_decoder`, but drives the batched WGPU
//! kernels the LFM2 prefill path already ships (`mul_mat_reg_tile` f32,
//! `rmsnorm_batch`, `add_rmsnorm_batch`, `qk_norm_rope_batch`,
//! `conv1d_fused_batch`, `flash_attention`, `silu_mul`, `bias_add`) rather than
//! the per-token Metal helpers. No new kernels.
//!
//! The 6 upsampled tokens of one frame are run as a single 6-token batch
//! (LFM2's shortconv is sequential per channel inside `conv1d_fused_batch`; the
//! detokenizer attention is non-causal over the sliding window, so each token's
//! query is attended with the decode `flash_attention` kernel that reads the
//! whole live window). Weights are dequantized to f32 and uploaded, matching the
//! Metal decoder's precision path. KV caches are f32 (the WGPU `flash_attention`
//! kernel reads `array<f32>`), so there is no f16 cast and the path is a touch
//! more accurate than Metal's f16 cache.
//!
//! Scope (PR1): the detokenizer only, which is the validated win. The
//! depthformer (code sampling) stays on CPU; `supports_depthformer` reports
//! that, so `CERA_GPU_DF=1` keeps sampling on the CPU rather than reaching the
//! `sample_audio_frame` panic here. A WGPU depthformer is a follow-up.
//!
//! The final ISTFT (`istft_to_pcm`) runs on the GPU too: `exp_polar` maps the
//! polar half-spectrum to complex, a reg-tile GEMM against a precomputed real
//! inverse-DFT basis does the iDFT, and `overlap_add` windows and folds the
//! frames into PCM. Only the startup-pad strip stays on the CPU after readback.

use std::path::Path;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};

use anyhow::{Context, Result};
use wgpu::Buffer;

#[cfg(not(target_arch = "wasm32"))]
use crate::backend::wgpu::DevicePollExt;
use crate::backend::wgpu::{GpuContext, shaders};
use crate::gguf::GgufFile;
use crate::model::audio_decoder::{
    DecoderConfig, DepthformerConfig, DetokenizerConfig, DetokenizerWeights,
};
use crate::model::gpu_lfm2::{
    MUL_MAT_TILE_K, MUL_MAT_TILE_M, MUL_MAT_TILE_N, MUL_MAT_TILE_WG_M, MUL_MAT_TILE_WG_N,
};

/// Tokens per detokenizer frame (fixed upsample factor, mirrors the CPU and
/// Metal paths).
const N_FRAMES: usize = 6;

/// F32 weight matrix on the GPU, row-major `[m rows x k cols]` (an `[out, in]`
/// projection), plus the reg-tile geometry needed to dispatch it.
struct GpuWeight {
    buf: Buffer,
    m: u32,
    k: u32,
}

/// Per-layer depthformer weights and pre-baked bind groups on GPU.
struct DfLayerGpu {
    operator_norm_bg: wgpu::BindGroup,
    gemm_qkv_bg: wgpu::BindGroup,
    rope_bgs: Vec<wgpu::BindGroup>,    // 8 entries for pos 0..8
    kv_copy_bgs: Vec<wgpu::BindGroup>, // 8 entries for pos 0..8
    attn_bgs: Vec<wgpu::BindGroup>,    // 8 entries for pos 0..8
    gemm_wo_accum_bg: wgpu::BindGroup,
    ffn_norm_bg: wgpu::BindGroup,
    ffn_gate_up_bg: wgpu::BindGroup,
    gemm_w2_accum_bg: wgpu::BindGroup,
}

/// Per-layer detokenizer weights on the GPU (conv XOR attention, plus FFN).
struct DetokLayerGpu {
    operator_norm: Buffer,
    ffn_norm: Buffer,
    ffn_w1: GpuWeight,
    ffn_w2: GpuWeight,
    ffn_w3: GpuWeight,
    // Conv-only.
    conv_in_proj: Option<GpuWeight>,
    conv_out_proj: Option<GpuWeight>,
    conv_weight: Option<Buffer>,
    // Attention-only.
    wq: Option<GpuWeight>,
    wk: Option<GpuWeight>,
    wv: Option<GpuWeight>,
    wo: Option<GpuWeight>,
    q_norm: Option<Buffer>,
    k_norm: Option<Buffer>,
}

#[derive(Clone)]
pub(crate) struct Pipelines {
    gemm_f32: wgpu::ComputePipeline,
    gemv_f32: wgpu::ComputePipeline,
    gemv_f32_accum: wgpu::ComputePipeline,
    rmsnorm_batch: wgpu::ComputePipeline,
    add_rmsnorm_batch: wgpu::ComputePipeline,
    qk_norm_rope_batch: wgpu::ComputePipeline,
    conv1d_fused_batch: wgpu::ComputePipeline,
    flash_attention: wgpu::ComputePipeline,
    silu_mul: wgpu::ComputePipeline,
    add_inplace: wgpu::ComputePipeline,
    bias_add: wgpu::ComputePipeline,
    exp_polar: wgpu::ComputePipeline,
    overlap_add: wgpu::ComputePipeline,
    argmax_f32: wgpu::ComputePipeline,
    df_embed_add: wgpu::ComputePipeline,
    df_kv_copy: wgpu::ComputePipeline,
    df_sample_logits: wgpu::ComputePipeline,
    df_ffn_gate_up: wgpu::ComputePipeline,
    df_qkv_gemv: wgpu::ComputePipeline,
}

pub struct WgpuAudioDecoder {
    ctx: GpuContext,
    cfg: DetokenizerConfig,
    pipes: Arc<Pipelines>,
    depthformer: Option<WgpuDepthformer>,

    layers: Vec<DetokLayerGpu>,
    output_norm: Buffer,
    lin_w: GpuWeight,
    lin_b: Buffer,

    // GPU ISTFT: real inverse-DFT basis `[n_fft x 2·n_fft_bins]` and the Hann
    // window `[n_fft]`, both derived from the config and uploaded once.
    idft_basis: GpuWeight,
    hann: Buffer,

    // Scratch (all sized for N_FRAMES tokens).
    hidden_buf: Buffer,           // residual stream [N x hs]
    normed_buf: Buffer,           // rmsnorm out / attn out [N x max(hs, q_dim)]
    proj_buf: Buffer,             // conv in_proj [N x 3hs] / attn Q [N x q_dim]
    gate_buf: Buffer,             // attn K / op residual / ffn gate [N x max(hs, kv_dim, ffn)]
    up_buf: Buffer,               // attn V / ffn up / ffn down [N x max(hs, kv_dim, ffn)]
    q_single: Buffer,             // one token's Q [q_dim]
    attn_single: Buffer,          // one token's attn out [q_dim]
    spectrum_buf: Buffer,         // [N x (n_fft_bins*2)]
    spectrum_staging_buf: Buffer, // pre-allocated staging buffer for zero-alloc spectrum readback
    rope_freqs_dummy: Buffer,     // 1-element buffer bound at qk_norm_rope binding 5

    // Persistent state.
    conv_bufs: Vec<Option<Buffer>>, // [d_conv x hs] per conv layer
    kv_k: Vec<Option<Buffer>>,      // [swa x kv_dim] f32 per attn layer
    kv_v: Vec<Option<Buffer>>,
    n_past: AtomicUsize,
}

fn get_detok_tensor(gguf: &GgufFile, name1: &str, name2: &str) -> Result<crate::tensor::Tensor> {
    gguf.get_tensor(name1)
        .or_else(|_| gguf.get_tensor(name2))
        .with_context(|| format!("tensor not found: neither `{name1}` nor `{name2}`"))
}

fn get_detok_mmap_weight(
    gguf: &Arc<GgufFile>,
    name1: &str,
    name2: &str,
) -> Result<crate::model::weights::MmapWeight> {
    crate::model::weights::MmapWeight::from_gguf(gguf, name1)
        .or_else(|_| crate::model::weights::MmapWeight::from_gguf(gguf, name2))
        .with_context(|| format!("tensor weight not found: neither `{name1}` nor `{name2}`"))
}

impl WgpuAudioDecoder {
    pub fn from_gguf(gguf: &Arc<GgufFile>, _vocoder_path: &Path) -> Result<Self> {
        let ctx = GpuContext::new()?;
        Self::from_gguf_with_context(ctx, gguf)
    }

    pub fn from_gguf_with_context(ctx: GpuContext, gguf: &Arc<GgufFile>) -> Result<Self> {
        Self::from_ggufs_with_context(ctx, gguf, None)
    }

    pub fn from_ggufs_with_context(
        ctx: GpuContext,
        detok_gguf: &Arc<GgufFile>,
        depthformer_gguf: Option<&Arc<GgufFile>>,
    ) -> Result<Self> {
        let gguf = detok_gguf;
        // Config from tensor shapes (same derivation as DetokenizerWeights).
        let conv_in = get_detok_mmap_weight(
            gguf,
            "lfm.layers.0.conv.in_proj.weight",
            "blk.0.shortconv.in_proj.weight",
        )?;
        let n_embd = conv_in.cols;
        let q_norm_t = get_detok_tensor(
            gguf,
            "lfm.layers.2.self_attn.q_layernorm.weight",
            "blk.2.attn_q_norm.weight",
        )?;
        let head_dim = q_norm_t.shape()[0];
        // `n_head` and `n_kv` below both divide by this, so a corrupt vocoder
        // GGUF reporting an empty q_layernorm shape would be a div-by-zero
        // panic. Same wording as the CPU loader's check in `audio_decoder.rs`,
        // since the two read the same tensor and fail for the same reason.
        anyhow::ensure!(
            head_dim > 0,
            "detokenizer n_embd_head must be > 0 (q_layernorm shape was empty)"
        );
        let q_w = get_detok_mmap_weight(
            gguf,
            "lfm.layers.2.self_attn.q_proj.weight",
            "blk.2.attn_q.weight",
        )?;
        let n_head = q_w.rows / head_dim;
        let k_w = get_detok_mmap_weight(
            gguf,
            "lfm.layers.2.self_attn.k_proj.weight",
            "blk.2.attn_k.weight",
        )?;
        let n_kv = k_w.rows / head_dim;
        let ffn_w1_0 = get_detok_mmap_weight(
            gguf,
            "lfm.layers.0.feed_forward.w1.weight",
            "blk.0.ffn_gate.weight",
        )?;
        let ffn_dim = ffn_w1_0.rows;

        let layer_is_conv = vec![true, true, false, true, false, true, false, true];
        let n_layer = layer_is_conv.len();
        let kv_dim = n_kv * head_dim;
        let q_dim = n_head * head_dim;

        // Kernel contracts, checked once at construction so a future model or
        // config drift fails loudly here instead of silently corrupting output:
        //   - `flash_attention` sizes `q_shared`/`acc` at MAX_HEAD_DIM (128).
        //   - GQA: the kernel derives `group_size = n_head / n_kv`.
        //   - the single-chunk ring write below assumes `swa % N_FRAMES == 0`
        //     (so 6 consecutive positions never straddle the ring wrap).
        anyhow::ensure!(
            head_dim <= 128,
            "detokenizer head_dim {head_dim} > 128; wgpu flash_attention cannot size q_shared/acc"
        );
        anyhow::ensure!(
            n_kv > 0 && n_head.is_multiple_of(n_kv),
            "detokenizer GQA requires n_kv > 0 and n_head divisible by n_kv (n_head={n_head}, n_kv={n_kv})"
        );

        let cfg = DetokenizerConfig {
            n_layer,
            n_embd,
            n_head,
            n_head_kv: n_kv,
            n_embd_head: head_dim,
            ffn_dim,
            d_conv: 2,
            rms_norm_eps: 1e-5,
            rope_freq_base: 1_000_000.0,
            swa_window_size: 30,
            n_codes: 8,
            n_fft: 1280,
            hop_length: 320,
            sample_rate: 24000,
            layer_is_conv,
        };
        anyhow::ensure!(
            cfg.swa_window_size.is_multiple_of(N_FRAMES),
            "detokenizer swa_window_size {} must be a multiple of {N_FRAMES} for the single-chunk KV ring write",
            cfg.swa_window_size
        );

        // f32 reg-tile GEMM matches the LFM2 prefill's dense fallback kernel.
        let gemm_f32 = ctx.create_pipeline_with_defines(
            shaders::MUL_MAT_REG_TILE,
            "main",
            "audio_detok_gemm_f32",
            &[
                ("SRC0_INNER_TYPE", "f32"),
                ("INIT_SRC0_SHMEM_FLOAT", ""),
                ("WORKGROUP_SIZE_M", &format!("{MUL_MAT_TILE_WG_M}u")),
                ("WORKGROUP_SIZE_N", &format!("{MUL_MAT_TILE_WG_N}u")),
                ("TILE_M", &format!("{MUL_MAT_TILE_M}u")),
                ("TILE_N", &format!("{MUL_MAT_TILE_N}u")),
                ("TILE_K", &format!("{MUL_MAT_TILE_K}u")),
            ],
        );
        let pipes = Pipelines {
            gemm_f32,
            gemv_f32: ctx.create_pipeline(shaders::GEMV_F32, "gemv_f32", "audio_df_gemv_f32"),
            gemv_f32_accum: ctx.create_pipeline(
                shaders::GEMV_F32,
                "gemv_f32_accum",
                "audio_df_gemv_f32_accum",
            ),
            rmsnorm_batch: ctx.create_pipeline(
                shaders::RMSNORM_BATCH,
                "rmsnorm_batch",
                "audio_detok_rmsnorm_batch",
            ),
            add_rmsnorm_batch: ctx.create_pipeline(
                shaders::RMSNORM_BATCH,
                "add_rmsnorm_batch",
                "audio_detok_add_rmsnorm_batch",
            ),
            qk_norm_rope_batch: ctx.create_pipeline(
                shaders::QK_NORM_ROPE_BATCH,
                "qk_norm_rope_batch",
                "audio_detok_qk_norm_rope_batch",
            ),
            conv1d_fused_batch: ctx.create_pipeline(
                shaders::CONV1D_FUSED_BATCH,
                "conv1d_fused_batch",
                "audio_detok_conv1d_fused_batch",
            ),
            flash_attention: ctx.create_pipeline(
                shaders::FLASH_ATTENTION,
                "flash_attention",
                "audio_detok_flash_attention",
            ),
            silu_mul: ctx.create_pipeline(
                shaders::ELEMENTWISE,
                "silu_mul_inplace",
                "audio_detok_silu_mul",
            ),
            add_inplace: ctx.create_pipeline(
                shaders::ELEMENTWISE,
                "add_inplace",
                "audio_detok_add_inplace",
            ),
            bias_add: ctx.create_pipeline(shaders::BIAS_ADD, "bias_add", "audio_detok_bias_add"),
            exp_polar: ctx.create_pipeline(
                shaders::EXP_POLAR,
                "exp_polar",
                "audio_istft_exp_polar",
            ),
            overlap_add: ctx.create_pipeline(
                shaders::OVERLAP_ADD,
                "overlap_add",
                "audio_istft_overlap_add",
            ),
            argmax_f32: ctx.create_pipeline(
                shaders::ARGMAX_F32,
                "argmax_f32",
                "audio_df_argmax_f32",
            ),
            df_embed_add: ctx.create_pipeline(
                include_str!("../backend/shaders/df_embed_add.wgsl"),
                "df_embed_add",
                "audio_df_embed_add",
            ),
            df_kv_copy: ctx.create_pipeline(
                include_str!("../backend/shaders/df_kv_cache_copy.wgsl"),
                "df_kv_cache_copy",
                "audio_df_kv_copy",
            ),
            df_sample_logits: ctx.create_pipeline(
                include_str!("../backend/shaders/df_sample_logits.wgsl"),
                "df_sample_logits",
                "audio_df_sample_logits",
            ),
            df_ffn_gate_up: ctx.create_pipeline(
                include_str!("../backend/shaders/df_ffn_gate_up.wgsl"),
                "df_ffn_gate_up",
                "audio_df_ffn_gate_up",
            ),
            df_qkv_gemv: ctx.create_pipeline(
                include_str!("../backend/shaders/df_qkv_gemv.wgsl"),
                "df_qkv_gemv",
                "audio_df_qkv_gemv",
            ),
        };

        // Dequantize each weight to f32 (CPU-matching precision) and upload.
        let make_weight = |name1: &str, name2: &str| -> Result<GpuWeight> {
            let t = get_detok_tensor(gguf, name1, name2)?;
            let f32_data = t.to_f32_vec();
            let shape = t.shape();
            let (rows, cols) = match shape.len() {
                1 => (1, shape[0]),
                2 => (shape[1], shape[0]),
                _ => anyhow::bail!("unexpected rank for {name1} / {name2}"),
            };
            let buf = ctx.upload_f32(&f32_data, name1);
            Ok(GpuWeight {
                buf,
                m: rows as u32,
                k: cols as u32,
            })
        };
        let upload_vec = |name1: &str, name2: &str| -> Result<Buffer> {
            Ok(ctx.upload_f32(&get_detok_tensor(gguf, name1, name2)?.to_f32_vec(), name1))
        };

        let mut layers = Vec::with_capacity(n_layer);
        for i in 0..n_layer {
            let pfx_lfm = format!("lfm.layers.{i}");
            let pfx_blk = format!("blk.{i}");
            let is_conv = cfg.layer_is_conv[i];
            let (cin, cop, cw) = if is_conv {
                (
                    Some(make_weight(
                        &format!("{pfx_lfm}.conv.in_proj.weight"),
                        &format!("{pfx_blk}.shortconv.in_proj.weight"),
                    )?),
                    Some(make_weight(
                        &format!("{pfx_lfm}.conv.out_proj.weight"),
                        &format!("{pfx_blk}.shortconv.out_proj.weight"),
                    )?),
                    Some(upload_vec(
                        &format!("{pfx_lfm}.conv.conv.weight"),
                        &format!("{pfx_blk}.shortconv.conv.weight"),
                    )?),
                )
            } else {
                (None, None, None)
            };
            let (wq, wk, wv, wo, qn, kn) = if !is_conv {
                (
                    Some(make_weight(
                        &format!("{pfx_lfm}.self_attn.q_proj.weight"),
                        &format!("{pfx_blk}.attn_q.weight"),
                    )?),
                    Some(make_weight(
                        &format!("{pfx_lfm}.self_attn.k_proj.weight"),
                        &format!("{pfx_blk}.attn_k.weight"),
                    )?),
                    Some(make_weight(
                        &format!("{pfx_lfm}.self_attn.v_proj.weight"),
                        &format!("{pfx_blk}.attn_v.weight"),
                    )?),
                    Some(make_weight(
                        &format!("{pfx_lfm}.self_attn.out_proj.weight"),
                        &format!("{pfx_blk}.attn_output.weight"),
                    )?),
                    Some(upload_vec(
                        &format!("{pfx_lfm}.self_attn.q_layernorm.weight"),
                        &format!("{pfx_blk}.attn_q_norm.weight"),
                    )?),
                    Some(upload_vec(
                        &format!("{pfx_lfm}.self_attn.k_layernorm.weight"),
                        &format!("{pfx_blk}.attn_k_norm.weight"),
                    )?),
                )
            } else {
                (None, None, None, None, None, None)
            };
            layers.push(DetokLayerGpu {
                operator_norm: upload_vec(
                    &format!("{pfx_lfm}.operator_norm.weight"),
                    &format!("{pfx_blk}.attn_norm.weight"),
                )?,
                ffn_norm: upload_vec(
                    &format!("{pfx_lfm}.ffn_norm.weight"),
                    &format!("{pfx_blk}.ffn_norm.weight"),
                )?,
                ffn_w1: make_weight(
                    &format!("{pfx_lfm}.feed_forward.w1.weight"),
                    &format!("{pfx_blk}.ffn_gate.weight"),
                )?,
                ffn_w2: make_weight(
                    &format!("{pfx_lfm}.feed_forward.w2.weight"),
                    &format!("{pfx_blk}.ffn_down.weight"),
                )?,
                ffn_w3: make_weight(
                    &format!("{pfx_lfm}.feed_forward.w3.weight"),
                    &format!("{pfx_blk}.ffn_up.weight"),
                )?,
                conv_in_proj: cin,
                conv_out_proj: cop,
                conv_weight: cw,
                wq,
                wk,
                wv,
                wo,
                q_norm: qn,
                k_norm: kn,
            });
        }

        let output_norm = upload_vec("lfm.embedding_norm.weight", "token_embd_norm.weight")?;
        let lin_w = make_weight("lin.weight", "dense_2.weight")?;
        let lin_b = upload_vec("lin.bias", "dense_2.bias")?;
        // The lin head GEMM writes `lin_w.m` rows at a `spec_per_frame` stride into
        // `spectrum_buf`. Both are pinned to `n_fft`, but assert the shared
        // assumption so a vocoder with a different head width fails at load rather
        // than silently overrunning the spectrum buffer.
        anyhow::ensure!(
            lin_w.m as usize == n_embd_bins(&cfg),
            "detokenizer lin.weight out-dim {} != spectrum width {}",
            lin_w.m,
            n_embd_bins(&cfg)
        );

        // GPU ISTFT tables, built once from the config. `idft_basis` is an
        // `[n_fft x 2·n_fft_bins]` weight consumed by the reg-tile GEMM exactly
        // like `lin_w`; `hann` is the length-`n_fft` window read by `overlap_add`.
        let basis = crate::model::audio_decoder::build_idft_basis(cfg.n_fft);
        let idft_basis = GpuWeight {
            buf: ctx.upload_f32(&basis, "audio_istft_idft_basis"),
            m: cfg.n_fft as u32,
            k: n_embd_bins(&cfg) as u32,
        };
        let hann = ctx.upload_f32(
            &crate::model::audio_decoder::build_hann(cfg.n_fft),
            "audio_istft_hann",
        );

        let pipes = Arc::new(pipes);

        let alloc = |n: usize, label: &str| ctx.create_storage_rw((n * 4) as u64, label);
        let big = n_embd.max(kv_dim).max(ffn_dim);
        let hidden_buf = alloc(N_FRAMES * n_embd, "audio_detok_hidden");
        let normed_buf = alloc(N_FRAMES * n_embd.max(q_dim), "audio_detok_normed");
        let proj_buf = alloc(N_FRAMES * (3 * n_embd).max(q_dim), "audio_detok_proj");
        let gate_buf = alloc(N_FRAMES * big, "audio_detok_gate");
        let up_buf = alloc(N_FRAMES * big, "audio_detok_up");
        let q_single = alloc(q_dim, "audio_detok_q_single");
        let attn_single = alloc(q_dim, "audio_detok_attn_single");
        let spec_len = N_FRAMES * (n_embd_bins(&cfg));
        let spectrum_buf = alloc(spec_len, "audio_detok_spectrum");
        let spectrum_staging_buf = ctx.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("audio_spectrum_staging"),
            size: (spec_len * std::mem::size_of::<f32>()) as u64,
            usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });
        let rope_freqs_dummy = ctx.upload_f32(&[1.0f32], "audio_detok_rope_dummy");

        let mut conv_bufs = vec![None; n_layer];
        let mut kv_k = vec![None; n_layer];
        let mut kv_v = vec![None; n_layer];
        for i in 0..n_layer {
            if cfg.layer_is_conv[i] {
                conv_bufs[i] = Some(alloc(cfg.d_conv * n_embd, "audio_detok_conv_rbuf"));
            } else {
                kv_k[i] = Some(alloc(cfg.swa_window_size * kv_dim, "audio_detok_kv_k"));
                kv_v[i] = Some(alloc(cfg.swa_window_size * kv_dim, "audio_detok_kv_v"));
            }
        }

        let df_src = depthformer_gguf.unwrap_or(detok_gguf);
        let depthformer = match WgpuDepthformer::try_from_gguf(&ctx, &pipes, df_src) {
            Ok(df) => {
                tracing::info!("[cera:wgpu_audio_decoder] WebGPU depthformer loaded successfully");
                Some(df)
            }
            Err(e) => {
                tracing::warn!(
                    "[cera:wgpu_audio_decoder] WebGPU depthformer unavailable ({e:#}), using CPU depthformer"
                );
                None
            }
        };

        Ok(Self {
            ctx,
            cfg,
            pipes,
            depthformer,
            layers,
            output_norm,
            lin_w,
            lin_b,
            idft_basis,
            hann,
            hidden_buf,
            normed_buf,
            proj_buf,
            gate_buf,
            up_buf,
            q_single,
            attn_single,
            spectrum_buf,
            spectrum_staging_buf,
            rope_freqs_dummy,
            conv_bufs,
            kv_k,
            kv_v,
            n_past: AtomicUsize::new(0),
        })
    }

    pub fn config(&self) -> &DetokenizerConfig {
        &self.cfg
    }

    pub fn reset(&self) {
        self.n_past.store(0, Ordering::Relaxed);
        // Zero the conv rolling buffers. The KV caches need no clearing: n_past=0
        // bounds `flash_attention` to the entries this generation writes.
        let zeros_len = self.cfg.d_conv * self.cfg.n_embd;
        let zeros = vec![0.0f32; zeros_len];
        for b in self.conv_bufs.iter().flatten() {
            self.ctx
                .queue
                .write_buffer(b, 0, bytemuck::cast_slice(&zeros));
        }
    }

    // ── encode helpers (add passes to a shared encoder) ─────────────────────

    fn encode(
        &self,
        enc: &mut wgpu::CommandEncoder,
        pipeline: &wgpu::ComputePipeline,
        bufs: &[&Buffer],
        workgroups: (u32, u32, u32),
        label: &str,
    ) {
        let entries: Vec<wgpu::BindGroupEntry> = bufs
            .iter()
            .enumerate()
            .map(|(i, b)| wgpu::BindGroupEntry {
                binding: i as u32,
                resource: b.as_entire_binding(),
            })
            .collect();
        let bind_group = self
            .ctx
            .device
            .create_bind_group(&wgpu::BindGroupDescriptor {
                label: Some(label),
                layout: &pipeline.get_bind_group_layout(0),
                entries: &entries,
            });
        let mut pass = self.ctx.begin_pass(enc, label);
        pass.set_pipeline(pipeline);
        pass.set_bind_group(0, &bind_group, &[]);
        pass.dispatch_workgroups(workgroups.0, workgroups.1, workgroups.2);
    }

    fn params(&self, data: &[u32], label: &str) -> Buffer {
        self.ctx.upload_storage(bytemuck::cast_slice(data), label)
    }

    /// `y[n, m] = x[n, k] * wᵀ` via the f32 reg-tile GEMM. `x_stride` / `y_stride`
    /// are f32 elements between consecutive token rows.
    #[allow(clippy::too_many_arguments)]
    fn gemm(
        &self,
        enc: &mut wgpu::CommandEncoder,
        w: &GpuWeight,
        x: &Buffer,
        y: &Buffer,
        n: u32,
        x_stride: u32,
        y_stride: u32,
    ) {
        let params = self.params(
            &[w.m, w.k, n, x_stride, y_stride],
            "audio_detok_gemm_params",
        );
        let wg_m = w.m.div_ceil(MUL_MAT_TILE_WG_M * MUL_MAT_TILE_M);
        let wg_n = n.div_ceil(MUL_MAT_TILE_WG_N * MUL_MAT_TILE_N);
        self.encode(
            enc,
            &self.pipes.gemm_f32,
            &[&w.buf, x, y, &params],
            (wg_m, wg_n, 1),
            "audio_detok_gemm",
        );
    }

    fn rmsnorm_batch(
        &self,
        enc: &mut wgpu::CommandEncoder,
        src: &Buffer,
        dst: &Buffer,
        weight: &Buffer,
        n: u32,
    ) {
        let hs = self.cfg.n_embd as u32;
        let p = self.params(
            &[
                hs,
                self.cfg.rms_norm_eps.to_bits(),
                hs,
                hs,
                1.0f32.to_bits(),
            ],
            "audio_detok_rmsnorm_params",
        );
        self.encode(
            enc,
            &self.pipes.rmsnorm_batch,
            &[src, dst, weight, &p],
            (n, 1, 1),
            "audio_detok_rmsnorm",
        );
    }

    /// `src += residual; dst = rmsnorm(src, weight)`. `dst` and `residual` must
    /// be distinct buffers (WGPU rejects binding-aliasing).
    fn add_rmsnorm_batch(
        &self,
        enc: &mut wgpu::CommandEncoder,
        src: &Buffer,
        dst: &Buffer,
        weight: &Buffer,
        residual: &Buffer,
        n: u32,
    ) {
        let hs = self.cfg.n_embd as u32;
        let p = self.params(
            &[
                hs,
                self.cfg.rms_norm_eps.to_bits(),
                hs,
                hs,
                1.0f32.to_bits(),
            ],
            "audio_detok_add_rmsnorm_params",
        );
        self.encode(
            enc,
            &self.pipes.add_rmsnorm_batch,
            &[src, dst, weight, &p, residual],
            (n, 1, 1),
            "audio_detok_add_rmsnorm",
        );
    }

    fn silu_mul(&self, enc: &mut wgpu::CommandEncoder, gate: &Buffer, up: &Buffer, total: u32) {
        let p = self.params(&[total, 0], "audio_detok_silu_params");
        self.encode(
            enc,
            &self.pipes.silu_mul,
            &[gate, up, &p],
            (total.div_ceil(256), 1, 1),
            "audio_detok_silu_mul",
        );
    }

    fn add_inplace(&self, enc: &mut wgpu::CommandEncoder, a: &Buffer, b: &Buffer, n: u32) {
        let p = self.params(&[n, 0], "audio_detok_add_params");
        self.encode(
            enc,
            &self.pipes.add_inplace,
            &[a, b, &p],
            (n.div_ceil(256), 1, 1),
            "audio_detok_add",
        );
    }

    // ── public forward ──────────────────────────────────────────────────────

    fn encode_detokenize_to_spectrum(
        &self,
        enc: &mut wgpu::CommandEncoder,
        cpu_weights: &DetokenizerWeights,
        codes: &[i32],
    ) -> (usize, usize) {
        let hs = self.cfg.n_embd;
        let hd = self.cfg.n_embd_head;
        let n_head = self.cfg.n_head as u32;
        let n_kv = self.cfg.n_head_kv as u32;
        let q_dim = n_head * hd as u32;
        let kv_dim = n_kv * hd as u32;
        let ffn = self.cfg.ffn_dim as u32;
        let n = N_FRAMES as u32;
        let hs_u = hs as u32;
        let spec_per_frame = n_embd_bins(&self.cfg);

        // 1. Embed + upsample on CPU, upload as the residual stream.
        let tokens = {
            use crate::model::audio_decoder::{detok_embed_codes, upsample};
            let emb = detok_embed_codes(cpu_weights, codes);
            upsample(&emb, hs, N_FRAMES)
        };
        self.ctx
            .queue
            .write_buffer(&self.hidden_buf, 0, bytemuck::cast_slice(&tokens));

        let n_past = self.n_past.load(Ordering::Relaxed);
        let scale = 1.0f32 / (hd as f32).sqrt();
        let seq_len = (n_past + N_FRAMES).min(self.cfg.swa_window_size) as u32;

        for (il, lw) in self.layers.iter().enumerate() {
            // Phase 1: (add prev-layer residual then) rmsnorm with operator_norm.
            if il == 0 {
                self.rmsnorm_batch(
                    enc,
                    &self.hidden_buf,
                    &self.normed_buf,
                    &lw.operator_norm,
                    n,
                );
            } else {
                self.add_rmsnorm_batch(
                    enc,
                    &self.hidden_buf,
                    &self.normed_buf,
                    &lw.operator_norm,
                    &self.up_buf,
                    n,
                );
            }

            if self.cfg.layer_is_conv[il] {
                let cin = lw.conv_in_proj.as_ref().unwrap();
                let cop = lw.conv_out_proj.as_ref().unwrap();
                let cw = lw.conv_weight.as_ref().unwrap();
                let rbuf = self.conv_bufs[il].as_ref().unwrap();

                // in_proj: normed → proj [n x 3hs].
                self.gemm(
                    enc,
                    cin,
                    &self.normed_buf,
                    &self.proj_buf,
                    n,
                    hs_u,
                    3 * hs_u,
                );
                // fused conv1d (b*x, depthwise conv, c-gate) → normed [n x hs].
                let cp = self.params(
                    &[
                        hs_u,
                        (self.cfg.d_conv + 1) as u32,
                        self.cfg.d_conv as u32,
                        n,
                        3 * hs_u,
                        hs_u,
                    ],
                    "audio_detok_conv_params",
                );
                self.encode(
                    enc,
                    &self.pipes.conv1d_fused_batch,
                    &[&self.proj_buf, rbuf, cw, &self.normed_buf, &cp],
                    (hs_u.div_ceil(256), 1, 1),
                    "audio_detok_conv1d",
                );
                // out_proj: normed → gate (op residual scratch) [n x hs].
                self.gemm(enc, cop, &self.normed_buf, &self.gate_buf, n, hs_u, hs_u);
            } else {
                let wq = lw.wq.as_ref().unwrap();
                let wk = lw.wk.as_ref().unwrap();
                let wv = lw.wv.as_ref().unwrap();
                let wo = lw.wo.as_ref().unwrap();
                let qn = lw.q_norm.as_ref().unwrap();
                let kn = lw.k_norm.as_ref().unwrap();
                let k_cache = self.kv_k[il].as_ref().unwrap();
                let v_cache = self.kv_v[il].as_ref().unwrap();

                // Q → proj, K → gate, V → up.
                self.gemm(enc, wq, &self.normed_buf, &self.proj_buf, n, hs_u, q_dim);
                self.gemm(enc, wk, &self.normed_buf, &self.gate_buf, n, hs_u, kv_dim);
                self.gemm(enc, wv, &self.normed_buf, &self.up_buf, n, hs_u, kv_dim);

                // Per-head QK-norm + NeoX RoPE on Q (proj) and K (gate).
                let p = self.params(
                    &[
                        n_past as u32,
                        n,
                        n_head,
                        n_kv,
                        hd as u32,
                        self.cfg.rms_norm_eps.to_bits(),
                        self.cfg.rope_freq_base.to_bits(),
                        0, // rope_type = NeoX
                        q_dim,
                        kv_dim,
                        0, // has_freq_factors
                        1, // has_qk_norm
                    ],
                    "audio_detok_rope_params",
                );
                let tg = n * (n_head + n_kv);
                self.encode(
                    enc,
                    &self.pipes.qk_norm_rope_batch,
                    &[
                        &self.proj_buf,
                        &self.gate_buf,
                        qn,
                        kn,
                        &p,
                        &self.rope_freqs_dummy,
                    ],
                    (tg, 1, 1),
                    "audio_detok_qk_rope",
                );

                // Write this frame's K/V into the ring cache. swa (30) is a
                // multiple of N_FRAMES (6), so the 6-slot write never straddles
                // the wrap; one contiguous copy suffices.
                let ring = (n_past % self.cfg.swa_window_size) as u64;
                let f32_bytes = std::mem::size_of::<f32>() as u64;
                let dst_off = ring * kv_dim as u64 * f32_bytes;
                let chunk = (N_FRAMES as u64) * kv_dim as u64 * f32_bytes;
                enc.copy_buffer_to_buffer(&self.gate_buf, 0, k_cache, dst_off, chunk);
                enc.copy_buffer_to_buffer(&self.up_buf, 0, v_cache, dst_off, chunk);

                // Non-causal attention: each token's query attends the whole live
                // window. `flash_attention` reads the cache linearly; the live set
                // is order-independent under softmax, so the ring order is fine.
                let attn_params = self.params(
                    &[
                        n_head,
                        n_kv,
                        hd as u32,
                        kv_dim,
                        seq_len,
                        scale.to_bits(),
                        0,
                        0,
                    ],
                    "audio_detok_attn_params",
                );
                for t in 0..N_FRAMES {
                    let q_off = (t * q_dim as usize) as u64 * 4;
                    enc.copy_buffer_to_buffer(
                        &self.proj_buf,
                        q_off,
                        &self.q_single,
                        0,
                        q_dim as u64 * 4,
                    );
                    self.encode(
                        enc,
                        &self.pipes.flash_attention,
                        &[
                            &self.q_single,
                            k_cache,
                            v_cache,
                            &self.attn_single,
                            &attn_params,
                        ],
                        (n_head, 1, 1),
                        "audio_detok_flash",
                    );
                    enc.copy_buffer_to_buffer(
                        &self.attn_single,
                        0,
                        &self.normed_buf,
                        q_off,
                        q_dim as u64 * 4,
                    );
                }

                // out_proj: attn out (normed, stride q_dim) → gate (op residual).
                self.gemm(enc, wo, &self.normed_buf, &self.gate_buf, n, q_dim, hs_u);
            }

            // FFN: fused (hidden += op residual) + ffn_norm → normed.
            self.add_rmsnorm_batch(
                enc,
                &self.hidden_buf,
                &self.normed_buf,
                &lw.ffn_norm,
                &self.gate_buf,
                n,
            );
            self.gemm(
                enc,
                &lw.ffn_w1,
                &self.normed_buf,
                &self.gate_buf,
                n,
                hs_u,
                ffn,
            );
            self.gemm(
                enc,
                &lw.ffn_w3,
                &self.normed_buf,
                &self.up_buf,
                n,
                hs_u,
                ffn,
            );
            self.silu_mul(enc, &self.gate_buf, &self.up_buf, n * ffn);
            // down → up (next layer's residual scratch).
            self.gemm(enc, &lw.ffn_w2, &self.gate_buf, &self.up_buf, n, ffn, hs_u);
        }

        // Final residual add (last layer's FFN down lives in up_buf).
        self.add_inplace(enc, &self.hidden_buf, &self.up_buf, n * hs_u);

        // Output norm + linear head + bias per frame.
        self.rmsnorm_batch(
            enc,
            &self.hidden_buf,
            &self.normed_buf,
            &self.output_norm,
            n,
        );
        self.gemm(
            enc,
            &self.lin_w,
            &self.normed_buf,
            &self.spectrum_buf,
            n,
            hs_u,
            spec_per_frame as u32,
        );
        let bias_params = self.params(
            &[(N_FRAMES * spec_per_frame) as u32, spec_per_frame as u32],
            "audio_detok_bias_params",
        );
        self.encode(
            enc,
            &self.pipes.bias_add,
            &[&self.spectrum_buf, &self.lin_b, &bias_params],
            (((N_FRAMES * spec_per_frame) as u32).div_ceil(256), 1, 1),
            "audio_detok_bias",
        );

        self.n_past.store(n_past + N_FRAMES, Ordering::Relaxed);
        (N_FRAMES, spec_per_frame)
    }

    pub fn detokenize_to_spectrum(
        &self,
        cpu_weights: &DetokenizerWeights,
        codes: &[i32],
    ) -> Vec<f32> {
        let mut enc = self.ctx.device.create_command_encoder(&Default::default());
        let (n_frames, spec_per_frame) =
            self.encode_detokenize_to_spectrum(&mut enc, cpu_weights, codes);
        self.ctx.submit_encoder(enc);
        self.ctx
            .download_f32(&self.spectrum_buf, n_frames * spec_per_frame)
    }

    pub async fn detokenize_to_spectrum_async(
        &self,
        cpu_weights: &DetokenizerWeights,
        codes: &[i32],
    ) -> Result<Vec<f32>> {
        let mut enc = self.ctx.device.create_command_encoder(&Default::default());
        let (n_frames, spec_per_frame) =
            self.encode_detokenize_to_spectrum(&mut enc, cpu_weights, codes);
        let size = (n_frames * spec_per_frame * std::mem::size_of::<f32>()) as u64;
        enc.copy_buffer_to_buffer(&self.spectrum_buf, 0, &self.spectrum_staging_buf, 0, size);
        self.ctx.submit_encoder(enc);

        let (tx, rx) = futures_channel::oneshot::channel();
        self.spectrum_staging_buf
            .slice(0..size)
            .map_async(wgpu::MapMode::Read, move |result| {
                let _ = tx.send(result);
            });

        #[cfg(not(target_arch = "wasm32"))]
        self.ctx.device.poll_wait();

        let res = rx.await;
        let mut result = vec![0f32; n_frames * spec_per_frame];
        match res {
            Ok(Ok(())) => {
                let parse_res: Result<Vec<f32>> = {
                    let slice = self.spectrum_staging_buf.slice(0..size);
                    match slice.get_mapped_range() {
                        Ok(data) => {
                            bytemuck::cast_slice_mut(&mut result).copy_from_slice(&data);
                            Ok(result)
                        }
                        Err(e) => Err(anyhow::anyhow!("get_mapped_range failed: {e:?}")),
                    }
                };
                self.spectrum_staging_buf.unmap();
                parse_res
            }
            Ok(Err(e)) => anyhow::bail!("GPU readback failed: {e:?}"),
            Err(_) => anyhow::bail!("GPU readback channel closed"),
        }
    }

    fn encode_istft_to_pcm(
        &self,
        enc: &mut wgpu::CommandEncoder,
        spectrum: &[f32],
        n_fft: usize,
        hop_length: usize,
    ) -> Option<(Buffer, usize)> {
        let bins = n_fft / 2 + 1;
        let frame_size = bins * 2;
        let n_frames = spectrum.len() / frame_size;
        if n_frames == 0 {
            return None;
        }

        let spec_buf = self.ctx.upload_f32(spectrum, "audio_istft_spectrum_in");
        let n_spec_floats = n_frames * frame_size;
        let complex_buf = self.ctx.create_storage_rw(
            (n_spec_floats * std::mem::size_of::<f32>()) as u64,
            "audio_istft_complex_half_spectrum",
        );
        let ep_params = self.params(
            &[n_frames as u32, bins as u32],
            "audio_istft_exp_polar_params",
        );
        self.encode(
            enc,
            &self.pipes.exp_polar,
            &[&spec_buf, &complex_buf, &ep_params],
            (((n_frames * bins) as u32).div_ceil(256), 1, 1),
            "audio_istft_exp_polar",
        );

        let windowed_buf = self.ctx.create_storage_rw(
            (n_frames * n_fft * std::mem::size_of::<f32>()) as u64,
            "audio_istft_windowed_frames",
        );
        self.gemm(
            enc,
            &self.idft_basis,
            &complex_buf,
            &windowed_buf,
            n_frames as u32,
            frame_size as u32,
            n_fft as u32,
        );

        let total_samples = n_frames * hop_length;
        let pcm_buf = self.ctx.create_storage_rw(
            (total_samples * std::mem::size_of::<f32>()) as u64,
            "audio_istft_pcm_out",
        );
        let oa_params = self.params(
            &[n_frames as u32, n_fft as u32, hop_length as u32, 0u32],
            "audio_istft_overlap_add_params",
        );
        self.encode(
            enc,
            &self.pipes.overlap_add,
            &[&windowed_buf, &self.hann, &pcm_buf, &oa_params],
            ((total_samples as u32).div_ceil(256), 1, 1),
            "audio_istft_overlap_add",
        );

        Some((pcm_buf, total_samples))
    }

    /// Convert the accumulated spectrum to PCM on the GPU: `exp_polar` →
    /// iDFT-matmul → windowed `overlap_add`. Numerically mirrors the CPU
    /// `istft_to_pcm` (the iDFT basis folds the Hermitian mirror and the
    /// `1/n_fft` scale), down to the startup-pad strip, which stays on the CPU
    /// after readback. Runs once at end-of-generation, so the frame-sized
    /// scratch is allocated per call rather than persisted.
    pub fn istft_to_pcm(&self, spectrum: &[f32], n_fft: usize, hop_length: usize) -> Vec<f32> {
        // `idft_basis` and `hann` are built once from `self.cfg`, but every
        // buffer below is sized from these arguments, so a mismatch does not
        // compute a different transform: it indexes a basis that is the wrong
        // shape for the buffers around it. This was a `debug_assert`, which is
        // compiled out of exactly the builds that run this. The CPU
        // implementation takes both as parameters and is tied to neither, so
        // hand the call over rather than refuse it.
        if n_fft != self.cfg.n_fft || hop_length != self.cfg.hop_length {
            return crate::model::audio_decoder::istft_to_pcm(spectrum, n_fft, hop_length);
        }
        let bins = n_fft / 2 + 1;
        let frame_size = bins * 2;
        let n_frames = spectrum.len() / frame_size;
        if n_frames == 0 {
            return vec![];
        }

        // A single 1D dispatch caps the frame count at the device's
        // `max_compute_workgroups_per_dimension` (65535 by default); the CPU
        // `istft_to_pcm` has no such limit, so fall back to it for very long
        // audio rather than truncating.
        if n_frames > (u16::MAX as usize) {
            return crate::model::audio_decoder::istft_to_pcm(spectrum, n_fft, hop_length);
        }

        let mut enc = self.ctx.device.create_command_encoder(&Default::default());
        let (pcm_buf, total_samples) =
            match self.encode_istft_to_pcm(&mut enc, spectrum, n_fft, hop_length) {
                Some(res) => res,
                None => return vec![],
            };

        self.ctx.submit_encoder(enc);

        let mut pcm = self.ctx.download_f32(&pcm_buf, total_samples);
        let padding = (n_fft - hop_length) / 2;
        if pcm.len() > padding {
            pcm.drain(..padding);
        }
        pcm
    }

    pub async fn istft_to_pcm_async(
        &self,
        spectrum: &[f32],
        n_fft: usize,
        hop_length: usize,
    ) -> Result<Vec<f32>> {
        if n_fft != self.cfg.n_fft || hop_length != self.cfg.hop_length {
            return Ok(crate::model::audio_decoder::istft_to_pcm(
                spectrum, n_fft, hop_length,
            ));
        }
        let bins = n_fft / 2 + 1;
        let frame_size = bins * 2;
        let n_frames = spectrum.len() / frame_size;
        if n_frames == 0 {
            return Ok(vec![]);
        }

        if n_frames > (u16::MAX as usize) {
            return Ok(crate::model::audio_decoder::istft_to_pcm(
                spectrum, n_fft, hop_length,
            ));
        }

        let mut enc = self.ctx.device.create_command_encoder(&Default::default());
        let (pcm_buf, total_samples) =
            match self.encode_istft_to_pcm(&mut enc, spectrum, n_fft, hop_length) {
                Some(res) => res,
                None => return Ok(vec![]),
            };

        let pending = self.ctx.begin_download_with_encoder(
            enc,
            &pcm_buf,
            (total_samples * std::mem::size_of::<f32>()) as u64,
        );
        let bytes = pending.recv().await?;
        let mut pcm = vec![0f32; total_samples];
        bytemuck::cast_slice_mut(&mut pcm).copy_from_slice(&bytes);
        let padding = (n_fft - hop_length) / 2;
        if pcm.len() > padding {
            pcm.drain(..padding);
        }
        Ok(pcm)
    }

    /// Whether this GPU audio decoder has an active GPU depthformer.
    pub fn supports_depthformer(&self) -> bool {
        self.depthformer.is_some()
    }
}

/// Spectrum floats per frame: `(n_fft/2 + 1) * 2` (log-magnitude, angle).
fn n_embd_bins(cfg: &DetokenizerConfig) -> usize {
    (cfg.n_fft / 2 + 1) * 2
}

impl crate::model::audio_decoder::AudioGpu for WgpuAudioDecoder {
    fn supports_depthformer(&self) -> bool {
        self.depthformer.is_some()
    }

    fn sample_audio_frame(&self, embedding: &[f32], temperature: f32, top_k: usize) -> [i32; 8] {
        if let Some(ref _df) = self.depthformer {
            #[cfg(not(target_arch = "wasm32"))]
            {
                pollster::block_on(_df.sample_frame_async(embedding, temperature, top_k))
                    .unwrap_or_else(|e| {
                        tracing::error!(
                            "[cera::wgpu_audio_decoder] sample_frame_async failed: {e:#}"
                        );
                        [0; 8]
                    })
            }
            #[cfg(target_arch = "wasm32")]
            {
                let _ = (embedding, temperature, top_k);
                [0; 8]
            }
        } else {
            [0; 8]
        }
    }

    fn sample_audio_frame_async<'a>(
        &'a self,
        embedding: &'a [f32],
        temperature: f32,
        top_k: usize,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<[i32; 8]>> + Send + 'a>> {
        if let Some(ref df) = self.depthformer {
            Box::pin(df.sample_frame_async(embedding, temperature, top_k))
        } else {
            Box::pin(async move {
                anyhow::bail!("WGPU depthformer not available");
            })
        }
    }

    #[cfg(feature = "gpu")]
    fn sample_audio_frame_from_gpu_hidden_async<'a>(
        &'a self,
        hidden_buf: &'a wgpu::Buffer,
        temperature: f32,
        top_k: usize,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<[i32; 8]>> + Send + 'a>> {
        if let Some(ref df) = self.depthformer {
            Box::pin(df.sample_frame_from_gpu_hidden_async(hidden_buf, temperature, top_k))
        } else {
            Box::pin(async move {
                anyhow::bail!("WGPU depthformer not available");
            })
        }
    }

    fn detokenize_to_spectrum(&self, cpu_weights: &DetokenizerWeights, codes: &[i32]) -> Vec<f32> {
        self.detokenize_to_spectrum(cpu_weights, codes)
    }

    fn detokenize_to_spectrum_async<'a>(
        &'a self,
        cpu_weights: &'a DetokenizerWeights,
        codes: &'a [i32],
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<Vec<f32>>> + Send + 'a>> {
        Box::pin(self.detokenize_to_spectrum_async(cpu_weights, codes))
    }

    fn istft_to_pcm(&self, spectrum: &[f32], n_fft: usize, hop_length: usize) -> Vec<f32> {
        self.istft_to_pcm(spectrum, n_fft, hop_length)
    }

    fn istft_to_pcm_async<'a>(
        &'a self,
        spectrum: &'a [f32],
        n_fft: usize,
        hop_length: usize,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<Vec<f32>>> + Send + 'a>> {
        Box::pin(self.istft_to_pcm_async(spectrum, n_fft, hop_length))
    }

    fn reset_depthformer(&self) {
        if let Some(ref df) = self.depthformer {
            df.reset();
        }
    }

    fn reset_detokenizer(&self) {
        self.reset();
    }
}

// ── WgpuDepthformer ────────────────────────────────────────────────────────
//
// F32 dequantized weights + F32 KV cache. Runs all 8 codebook steps of an audio
// frame on WebGPU compute shaders, reading back only the 2049 logits per step.

#[allow(dead_code)]
pub struct WgpuDepthformer {
    ctx: GpuContext,
    df_cfg: DepthformerConfig,
    dec_cfg: DecoderConfig,
    pipes: Arc<Pipelines>,
    layers: Vec<DfLayerGpu>,
    // Pre-baked bind groups:
    depth_linear_bgs: Vec<wgpu::BindGroup>,   // 8 entries
    bias_add_bgs: Vec<wgpu::BindGroup>,       // 8 entries
    embed_add_bgs: Vec<wgpu::BindGroup>,      // 7 entries (j=0..6 for prev codebooks 0..6)
    to_logits_norm_bgs: Vec<wgpu::BindGroup>, // 8 entries
    to_logits_gemm_bgs: Vec<wgpu::BindGroup>, // 8 entries
    argmax_bgs: Vec<wgpu::BindGroup>,         // 8 entries
    sample_bgs: Vec<wgpu::BindGroup>,         // 8 entries
    sample_params_bufs: Vec<Buffer>,          // 8 entries
    // Grid dispatch dimensions:
    depth_linear_wg_m: u32,
    dl_cols: usize,
    qkv_wg_m: u32,
    n_embd_wg_m: u32,
    ffn_dim_wg_m: u32,
    n_vocab_wg_m: u32,
    // Storage buffers:
    embedding_in_buf: Buffer,
    hidden_buf: Buffer,
    normed_buf: Buffer,
    q_buf: Buffer,
    k_buf: Buffer,
    v_buf: Buffer,
    attn_out_buf: Buffer,
    ffn_act_buf: Buffer,
    sampled_codes_buf: Buffer,
    staging_readback_buf: Buffer,
    logits_buf: Buffer,
    kv_k: Vec<Buffer>,
    kv_v: Vec<Buffer>,
}

impl WgpuDepthformer {
    pub(crate) fn try_from_gguf(
        ctx: &GpuContext,
        pipes: &Arc<Pipelines>,
        gguf: &Arc<GgufFile>,
    ) -> Result<Self> {
        // Hyperparameters from GGUF metadata.
        let n_layer = gguf
            .get_u32("depthformer_n_layer")
            .context("missing depthformer_n_layer")? as usize;
        let n_embd = gguf
            .get_u32("depthformer_n_embd")
            .context("missing depthformer_n_embd")? as usize;

        // Derive head config from qkv_proj shape.
        let qkv = crate::model::weights::MmapWeight::from_gguf(
            gguf,
            "depthformer.layers.0.operator.qkv_proj.weight",
        )?;
        let q_norm_w =
            gguf.get_tensor("depthformer.layers.0.operator.attention.q_layernorm.weight")?;
        let n_embd_head = q_norm_w.shape()[0];
        let qkv_out = qkv.rows;
        let n_head = 32;
        let n_head_kv = 8;
        anyhow::ensure!(
            qkv_out == (n_head + 2 * n_head_kv) * n_embd_head,
            "qkv_proj shape mismatch"
        );

        let w1_0 = crate::model::weights::MmapWeight::from_gguf(
            gguf,
            "depthformer.layers.0.feed_forward.w1.weight",
        )?;
        let ffn_dim = w1_0.rows;

        let df_cfg = DepthformerConfig {
            n_layer,
            n_embd,
            n_head,
            n_head_kv,
            n_embd_head,
            ffn_dim,
            rms_norm_eps: 1e-5,
            rope_freq_base: 1_000_000.0,
            max_seq_len: 8,
        };

        let depth_linear_w =
            crate::model::weights::MmapWeight::from_gguf(gguf, "depth_linear.weight")?;
        let depth_linear_b = gguf.get_tensor("depth_linear.bias")?.to_f32_vec();
        let n_codebook = 8;
        let to_logits_0 = crate::model::weights::MmapWeight::from_gguf(
            gguf,
            "depth_embeddings.0.to_logits.weight",
        )?;
        let n_vocab = to_logits_0.rows;
        let n_embd_llm = depth_linear_w.cols;

        let dec_cfg = DecoderConfig {
            n_codebook,
            n_vocab,
            n_embd: n_embd_llm,
            rms_norm_eps: 1e-5,
        };

        Self::from_configs(ctx, pipes, gguf, &df_cfg, &dec_cfg, depth_linear_b)
    }

    fn from_configs(
        ctx: &GpuContext,
        pipes: &Arc<Pipelines>,
        gguf: &Arc<GgufFile>,
        df_cfg: &DepthformerConfig,
        dec_cfg: &DecoderConfig,
        depth_linear_b: Vec<f32>,
    ) -> Result<Self> {
        let n_embd = df_cfg.n_embd;
        let hd = df_cfg.n_embd_head;
        let n_head = df_cfg.n_head;
        let n_kv = df_cfg.n_head_kv;
        let q_dim = n_head * hd;
        let kv_dim = n_kv * hd;
        let ffn_dim = df_cfg.ffn_dim;

        let upload_vec = |name: &str| -> Result<Buffer> {
            Ok(ctx.upload_f32(&gguf.get_tensor(name)?.to_f32_vec(), name))
        };

        let alloc = |n: usize, label: &str| ctx.create_storage_rw((n * 4) as u64, label);
        let hidden_buf = alloc(n_embd, "df_hidden");
        let normed_buf = alloc(n_embd, "df_normed");
        let q_buf = alloc(q_dim, "df_q");
        let k_buf = alloc(kv_dim, "df_k");
        let v_buf = alloc(kv_dim, "df_v");
        let attn_out_buf = alloc(q_dim, "df_attn_out");
        let ffn_act_buf = alloc(ffn_dim, "df_ffn_act");
        let dl_t = gguf.get_tensor("depth_linear.weight")?;
        let dl_f32 = dl_t.to_f32_vec();
        let dl_cols = dl_t.shape()[0]; // e.g. 2048 (n_embd_llm)
        let dl_rows = dl_t.shape()[1]; // e.g. 8 * 1024
        let n_embd_d = dl_rows / dec_cfg.n_codebook;

        let embedding_in_buf = alloc(dl_cols, "df_embedding_in");
        let logits_buf = alloc(dec_cfg.n_vocab, "df_logits");
        let sampled_codes_buf = alloc(dec_cfg.n_codebook, "df_sampled_codes");
        let staging_readback_buf = ctx.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("df_staging_readback"),
            size: (dec_cfg.n_codebook * 4) as u64,
            usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });
        let rope_freqs_dummy = ctx.upload_f32(&[1.0f32], "df_rope_dummy");

        let mut kv_k = Vec::with_capacity(df_cfg.n_layer);
        let mut kv_v = Vec::with_capacity(df_cfg.n_layer);
        for i in 0..df_cfg.n_layer {
            kv_k.push(alloc(df_cfg.max_seq_len * kv_dim, &format!("df_kv_k_{i}")));
            kv_v.push(alloc(df_cfg.max_seq_len * kv_dim, &format!("df_kv_v_{i}")));
        }

        let make_bg =
            |pipeline: &wgpu::ComputePipeline, bufs: &[&Buffer], label: &str| -> wgpu::BindGroup {
                let entries: Vec<wgpu::BindGroupEntry> = bufs
                    .iter()
                    .enumerate()
                    .map(|(i, b)| wgpu::BindGroupEntry {
                        binding: i as u32,
                        resource: b.as_entire_binding(),
                    })
                    .collect();
                ctx.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: Some(label),
                    layout: &pipeline.get_bind_group_layout(0),
                    entries: &entries,
                })
            };

        // Shared param buffers
        let hs = n_embd as u32;
        let rmsnorm_params_1 = ctx.upload_storage(
            bytemuck::cast_slice(&[hs, df_cfg.rms_norm_eps.to_bits(), hs, hs, 1.0f32.to_bits()]),
            "df_rmsnorm_params_1",
        );
        let add_params_n_embd = ctx.upload_storage(
            bytemuck::cast_slice(&[n_embd as u32, 0u32]),
            "df_add_params_n_embd",
        );

        let scale = 1.0f32 / (hd as f32).sqrt();
        let mut rope_params = Vec::with_capacity(df_cfg.max_seq_len);
        let mut attn_params = Vec::with_capacity(df_cfg.max_seq_len);
        let mut kv_copy_params = Vec::with_capacity(df_cfg.max_seq_len);
        for pos in 0..df_cfg.max_seq_len {
            rope_params.push(ctx.upload_storage(
                bytemuck::cast_slice(&[
                    pos as u32,
                    1,
                    n_head as u32,
                    n_kv as u32,
                    hd as u32,
                    df_cfg.rms_norm_eps.to_bits(),
                    df_cfg.rope_freq_base.to_bits(),
                    1, // rope_type = interleaved / NORM
                    q_dim as u32,
                    kv_dim as u32,
                    0,
                    1,
                ]),
                &format!("df_rope_params_{pos}"),
            ));
            attn_params.push(ctx.upload_storage(
                bytemuck::cast_slice(&[
                    n_head as u32,
                    n_kv as u32,
                    hd as u32,
                    kv_dim as u32,
                    (pos + 1) as u32,
                    scale.to_bits(),
                    0,
                    0,
                ]),
                &format!("df_attn_params_{pos}"),
            ));
            kv_copy_params.push(ctx.upload_storage(
                bytemuck::cast_slice(&[(pos * kv_dim) as u32, kv_dim as u32]),
                &format!("df_kv_copy_params_{pos}"),
            ));
        }

        // Layer GEMV params: vec4<u32>(m, k, row_base, 0)
        let qkv_params = ctx.upload_storage(
            bytemuck::cast_slice(&[q_dim as u32, kv_dim as u32, n_embd as u32, 0u32]),
            "df_qkv_params",
        );
        let wo_params = ctx.upload_storage(
            bytemuck::cast_slice(&[n_embd as u32, q_dim as u32, 0u32, 0u32]),
            "df_wo_params",
        );
        let w13_params = ctx.upload_storage(
            bytemuck::cast_slice(&[ffn_dim as u32, n_embd as u32, 0u32, 0u32]),
            "df_w13_params",
        );
        let w2_params = ctx.upload_storage(
            bytemuck::cast_slice(&[n_embd as u32, ffn_dim as u32, 0u32, 0u32]),
            "df_w2_params",
        );

        let mut layers = Vec::with_capacity(df_cfg.n_layer);
        for i in 0..df_cfg.n_layer {
            let pfx = format!("depthformer.layers.{i}");
            let qkv_t = gguf.get_tensor(&format!("{pfx}.operator.qkv_proj.weight"))?;
            let qkv_f32 = qkv_t.to_f32_vec();
            let qkv_buf = ctx.upload_f32(&qkv_f32, &format!("{pfx}.qkv"));

            let op_norm = upload_vec(&format!("{pfx}.operator_norm.weight"))?;
            let q_norm = upload_vec(&format!("{pfx}.operator.attention.q_layernorm.weight"))?;
            let k_norm = upload_vec(&format!("{pfx}.operator.attention.k_layernorm.weight"))?;
            let wo_buf = ctx.upload_f32(
                &gguf
                    .get_tensor(&format!("{pfx}.operator.out_proj.weight"))?
                    .to_f32_vec(),
                &format!("{pfx}.wo"),
            );
            let ffn_norm = upload_vec(&format!("{pfx}.ffn_norm.weight"))?;

            let w1_vec = gguf
                .get_tensor(&format!("{pfx}.feed_forward.w1.weight"))?
                .to_f32_vec();
            let w3_vec = gguf
                .get_tensor(&format!("{pfx}.feed_forward.w3.weight"))?
                .to_f32_vec();
            let mut w13_vec = Vec::with_capacity(w1_vec.len() + w3_vec.len());
            w13_vec.extend_from_slice(&w1_vec);
            w13_vec.extend_from_slice(&w3_vec);
            let w13_buf = ctx.upload_f32(&w13_vec, &format!("{pfx}.w13"));
            let w2_buf = ctx.upload_f32(
                &gguf
                    .get_tensor(&format!("{pfx}.feed_forward.w2.weight"))?
                    .to_f32_vec(),
                &format!("{pfx}.w2"),
            );

            let operator_norm_bg = make_bg(
                &pipes.rmsnorm_batch,
                &[&hidden_buf, &normed_buf, &op_norm, &rmsnorm_params_1],
                &format!("df_op_norm_bg_{i}"),
            );
            let gemm_qkv_bg = make_bg(
                &pipes.df_qkv_gemv,
                &[&qkv_buf, &normed_buf, &q_buf, &k_buf, &v_buf, &qkv_params],
                &format!("df_qkv_bg_{i}"),
            );

            let mut rope_bgs = Vec::with_capacity(df_cfg.max_seq_len);
            let mut kv_copy_bgs = Vec::with_capacity(df_cfg.max_seq_len);
            let mut attn_bgs = Vec::with_capacity(df_cfg.max_seq_len);
            for pos in 0..df_cfg.max_seq_len {
                rope_bgs.push(make_bg(
                    &pipes.qk_norm_rope_batch,
                    &[
                        &q_buf,
                        &k_buf,
                        &q_norm,
                        &k_norm,
                        &rope_params[pos],
                        &rope_freqs_dummy,
                    ],
                    &format!("df_rope_bg_{i}_{pos}"),
                ));
                kv_copy_bgs.push(make_bg(
                    &pipes.df_kv_copy,
                    &[&k_buf, &v_buf, &kv_k[i], &kv_v[i], &kv_copy_params[pos]],
                    &format!("df_kv_copy_bg_{i}_{pos}"),
                ));
                attn_bgs.push(make_bg(
                    &pipes.flash_attention,
                    &[&q_buf, &kv_k[i], &kv_v[i], &attn_out_buf, &attn_params[pos]],
                    &format!("df_attn_bg_{i}_{pos}"),
                ));
            }

            let gemm_wo_accum_bg = make_bg(
                &pipes.gemv_f32_accum,
                &[&wo_buf, &attn_out_buf, &hidden_buf, &wo_params],
                &format!("df_wo_accum_bg_{i}"),
            );
            let ffn_norm_bg = make_bg(
                &pipes.rmsnorm_batch,
                &[&hidden_buf, &normed_buf, &ffn_norm, &rmsnorm_params_1],
                &format!("df_ffn_norm_bg_{i}"),
            );
            let ffn_gate_up_bg = make_bg(
                &pipes.df_ffn_gate_up,
                &[&w13_buf, &normed_buf, &ffn_act_buf, &w13_params],
                &format!("df_ffn_gate_up_bg_{i}"),
            );
            let gemm_w2_accum_bg = make_bg(
                &pipes.gemv_f32_accum,
                &[&w2_buf, &ffn_act_buf, &hidden_buf, &w2_params],
                &format!("df_w2_accum_bg_{i}"),
            );

            layers.push(DfLayerGpu {
                operator_norm_bg,
                gemm_qkv_bg,
                rope_bgs,
                kv_copy_bgs,
                attn_bgs,
                gemm_wo_accum_bg,
                ffn_norm_bg,
                ffn_gate_up_bg,
                gemm_w2_accum_bg,
            });
        }

        // Slice depth_linear.weight and pre-bake depth_linear GEMM and bias_add
        let dl_params = ctx.upload_storage(
            bytemuck::cast_slice(&[n_embd_d as u32, dl_cols as u32, 0u32, 0u32]),
            "df_dl_params",
        );
        let mut depth_linear_bgs = Vec::with_capacity(dec_cfg.n_codebook);
        let mut bias_add_bgs = Vec::with_capacity(dec_cfg.n_codebook);

        for j in 0..dec_cfg.n_codebook {
            let start = j * n_embd_d * dl_cols;
            let end = start + n_embd_d * dl_cols;
            let slice_data = &dl_f32[start..end];
            let buf = ctx.upload_f32(slice_data, &format!("depth_linear_slice_{j}"));
            depth_linear_bgs.push(make_bg(
                &pipes.gemv_f32,
                &[&buf, &embedding_in_buf, &hidden_buf, &dl_params],
                &format!("df_dl_bg_{j}"),
            ));

            let b_start = j * n_embd_d;
            let b_end = b_start + n_embd_d;
            let b_slice = &depth_linear_b[b_start..b_end];
            let b_buf = ctx.upload_f32(b_slice, &format!("depth_linear_b_{j}"));
            bias_add_bgs.push(make_bg(
                &pipes.add_inplace,
                &[&hidden_buf, &b_buf, &add_params_n_embd],
                &format!("df_bias_add_bg_{j}"),
            ));
        }

        let tl_params = ctx.upload_storage(
            bytemuck::cast_slice(&[dec_cfg.n_vocab as u32, n_embd as u32, 0u32, 0u32]),
            "df_tl_params",
        );
        let mut embed_add_bgs = Vec::with_capacity(dec_cfg.n_codebook.saturating_sub(1));
        let mut to_logits_norm_bgs = Vec::with_capacity(dec_cfg.n_codebook);
        let mut to_logits_gemm_bgs = Vec::with_capacity(dec_cfg.n_codebook);
        let mut argmax_bgs = Vec::with_capacity(dec_cfg.n_codebook);

        for j in 0..dec_cfg.n_codebook {
            let pfx = format!("depth_embeddings.{j}");
            let norm_buf = upload_vec(&format!("{pfx}.embedding_norm.weight"))?;
            to_logits_norm_bgs.push(make_bg(
                &pipes.rmsnorm_batch,
                &[&hidden_buf, &normed_buf, &norm_buf, &rmsnorm_params_1],
                &format!("df_cb_norm_bg_{j}"),
            ));

            let tl_t = gguf.get_tensor(&format!("{pfx}.to_logits.weight"))?;
            let tl_f32 = tl_t.to_f32_vec();
            let tl_buf = ctx.upload_f32(&tl_f32, &format!("{pfx}.to_logits"));
            to_logits_gemm_bgs.push(make_bg(
                &pipes.gemv_f32,
                &[&tl_buf, &normed_buf, &logits_buf, &tl_params],
                &format!("df_tl_bg_{j}"),
            ));

            let am_params = ctx.upload_storage(
                bytemuck::cast_slice(&[dec_cfg.n_vocab as u32, j as u32]),
                &format!("df_argmax_params_{j}"),
            );
            argmax_bgs.push(make_bg(
                &pipes.argmax_f32,
                &[&logits_buf, &sampled_codes_buf, &am_params],
                &format!("df_argmax_bg_{j}"),
            ));

            if j < dec_cfg.n_codebook - 1 {
                let emb_t = gguf.get_tensor(&format!("{pfx}.embedding.weight"))?;
                let emb_buf = ctx.upload_f32(&emb_t.to_f32_vec(), &format!("{pfx}.emb"));
                let em_params = ctx.upload_storage(
                    bytemuck::cast_slice(&[n_embd as u32, j as u32]),
                    &format!("df_embed_params_{j}"),
                );
                embed_add_bgs.push(make_bg(
                    &pipes.df_embed_add,
                    &[&emb_buf, &sampled_codes_buf, &hidden_buf, &em_params],
                    &format!("df_embed_add_bg_{j}"),
                ));
            }
        }

        let mut sample_params_bufs = Vec::with_capacity(dec_cfg.n_codebook);
        let mut sample_bgs = Vec::with_capacity(dec_cfg.n_codebook);
        for j in 0..dec_cfg.n_codebook {
            let sm_params = alloc(4, &format!("df_sample_params_{j}"));
            sample_bgs.push(make_bg(
                &pipes.df_sample_logits,
                &[&logits_buf, &sampled_codes_buf, &sm_params],
                &format!("df_sample_bg_{j}"),
            ));
            sample_params_bufs.push(sm_params);
        }

        let depth_linear_wg_m = (n_embd_d as u32).div_ceil(8);
        let qkv_wg_m = ((q_dim + 2 * kv_dim) as u32).div_ceil(8);
        let n_embd_wg_m = (n_embd as u32).div_ceil(8);
        let ffn_dim_wg_m = (ffn_dim as u32).div_ceil(8);
        let n_vocab_wg_m = (dec_cfg.n_vocab as u32).div_ceil(8);

        Ok(Self {
            ctx: ctx.clone(),
            df_cfg: df_cfg.clone(),
            dec_cfg: dec_cfg.clone(),
            pipes: pipes.clone(),
            layers,
            depth_linear_bgs,
            bias_add_bgs,
            embed_add_bgs,
            to_logits_norm_bgs,
            to_logits_gemm_bgs,
            argmax_bgs,
            sample_bgs,
            sample_params_bufs,
            depth_linear_wg_m,
            dl_cols,
            qkv_wg_m,
            n_embd_wg_m,
            ffn_dim_wg_m,
            n_vocab_wg_m,
            embedding_in_buf,
            hidden_buf,
            normed_buf,
            q_buf,
            k_buf,
            v_buf,
            attn_out_buf,
            ffn_act_buf,
            sampled_codes_buf,
            staging_readback_buf,
            logits_buf,
            kv_k,
            kv_v,
        })
    }

    pub fn reset(&self) {}

    pub async fn sample_frame_async(
        &self,
        embedding: &[f32],
        temperature: f32,
        top_k: usize,
    ) -> Result<[i32; 8]> {
        let mut padded = vec![0.0f32; self.dl_cols];
        let copy_len = embedding.len().min(self.dl_cols);
        padded[..copy_len].copy_from_slice(&embedding[..copy_len]);
        self.ctx
            .queue
            .write_buffer(&self.embedding_in_buf, 0, bytemuck::cast_slice(&padded));
        self.sample_frame_internal_async(temperature, top_k, None)
            .await
    }

    pub async fn sample_frame_from_gpu_hidden_async(
        &self,
        hidden_buf: &wgpu::Buffer,
        temperature: f32,
        top_k: usize,
    ) -> Result<[i32; 8]> {
        self.sample_frame_internal_async(temperature, top_k, Some(hidden_buf))
            .await
    }

    async fn sample_frame_internal_async(
        &self,
        temperature: f32,
        top_k: usize,
        gpu_hidden_src: Option<&wgpu::Buffer>,
    ) -> Result<[i32; 8]> {
        let n_embd = self.df_cfg.n_embd;
        let hd = self.df_cfg.n_embd_head;
        let n_head = self.df_cfg.n_head as u32;
        let n_kv = self.df_cfg.n_head_kv as u32;
        let kv_dim = n_kv * hd as u32;

        let use_sampling = temperature > 0.0 && temperature.is_finite() && top_k > 1;
        let inv_temp = if use_sampling { 1.0 / temperature } else { 1.0 };

        if use_sampling {
            for j in 0..self.dec_cfg.n_codebook {
                let rand_val = rand::random::<f32>();
                let params = [self.dec_cfg.n_vocab as f32, j as f32, inv_temp, rand_val];
                self.ctx.queue.write_buffer(
                    &self.sample_params_bufs[j],
                    0,
                    bytemuck::cast_slice(&params),
                );
            }
        }

        let mut enc = self
            .ctx
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some("wgpu_depthformer_all_codebooks"),
            });

        if let Some(src) = gpu_hidden_src {
            let size = (self.dl_cols * 4) as u64;
            enc.copy_buffer_to_buffer(src, 0, &self.embedding_in_buf, 0, size);
        }

        let dispatch_pass = |enc: &mut wgpu::CommandEncoder,
                             pipeline: &wgpu::ComputePipeline,
                             bg: &wgpu::BindGroup,
                             workgroups: (u32, u32, u32),
                             label: &str| {
            let mut pass = self.ctx.begin_pass(enc, label);
            pass.set_pipeline(pipeline);
            pass.set_bind_group(0, bg, &[]);
            pass.dispatch_workgroups(workgroups.0, workgroups.1, workgroups.2);
        };

        for j in 0..self.dec_cfg.n_codebook {
            let pos = j;

            // Initial projections for this codebook
            dispatch_pass(
                &mut enc,
                &self.pipes.gemv_f32,
                &self.depth_linear_bgs[j],
                (self.depth_linear_wg_m, 1, 1),
                "df_depth_linear",
            );

            dispatch_pass(
                &mut enc,
                &self.pipes.add_inplace,
                &self.bias_add_bgs[j],
                ((n_embd as u32).div_ceil(256), 1, 1),
                "df_bias_add",
            );

            if j > 0 {
                dispatch_pass(
                    &mut enc,
                    &self.pipes.df_embed_add,
                    &self.embed_add_bgs[j - 1],
                    ((n_embd as u32).div_ceil(256), 1, 1),
                    "df_embed_add",
                );
            }

            // Transformer layers with fused QKV, fused FFN gate+up with inline SiLU, and direct residual accumulators
            for lw in &self.layers {
                // RMSnorm -> normed_buf
                dispatch_pass(
                    &mut enc,
                    &self.pipes.rmsnorm_batch,
                    &lw.operator_norm_bg,
                    (1, 1, 1),
                    "df_op_norm",
                );

                // Fused QKV GEMV -> q_buf, k_buf, v_buf
                dispatch_pass(
                    &mut enc,
                    &self.pipes.df_qkv_gemv,
                    &lw.gemm_qkv_bg,
                    (self.qkv_wg_m, 1, 1),
                    "df_qkv_gemv",
                );

                // QK norm + RoPE (interleaved / NORM)
                dispatch_pass(
                    &mut enc,
                    &self.pipes.qk_norm_rope_batch,
                    &lw.rope_bgs[pos],
                    (n_head + n_kv, 1, 1),
                    "df_qk_norm_rope",
                );

                // GPU-side KV cache slot copy inside the compute pass
                dispatch_pass(
                    &mut enc,
                    &self.pipes.df_kv_copy,
                    &lw.kv_copy_bgs[pos],
                    (kv_dim.div_ceil(256), 1, 1),
                    "df_kv_copy",
                );

                // Flash Attention -> attn_out_buf
                dispatch_pass(
                    &mut enc,
                    &self.pipes.flash_attention,
                    &lw.attn_bgs[pos],
                    (n_head, 1, 1),
                    "df_flash_attention",
                );

                // Out projection directly accumulated into hidden_buf (hidden_buf += Wo · attn_out)
                dispatch_pass(
                    &mut enc,
                    &self.pipes.gemv_f32_accum,
                    &lw.gemm_wo_accum_bg,
                    (self.n_embd_wg_m, 1, 1),
                    "df_wo_accum",
                );

                // FFN: RMSnorm -> normed_buf
                dispatch_pass(
                    &mut enc,
                    &self.pipes.rmsnorm_batch,
                    &lw.ffn_norm_bg,
                    (1, 1, 1),
                    "df_ffn_norm",
                );

                // FFN Fused Gate + Up with inline SiLU -> ffn_act_buf
                dispatch_pass(
                    &mut enc,
                    &self.pipes.df_ffn_gate_up,
                    &lw.ffn_gate_up_bg,
                    (self.ffn_dim_wg_m, 1, 1),
                    "df_ffn_gate_up",
                );

                // FFN W2 Down projection directly accumulated into hidden_buf (hidden_buf += W2 · ffn_act)
                dispatch_pass(
                    &mut enc,
                    &self.pipes.gemv_f32_accum,
                    &lw.gemm_w2_accum_bg,
                    (self.n_embd_wg_m, 1, 1),
                    "df_w2_accum",
                );
            }

            // to_logits: RMSnorm -> GEMV -> logits_buf
            dispatch_pass(
                &mut enc,
                &self.pipes.rmsnorm_batch,
                &self.to_logits_norm_bgs[j],
                (1, 1, 1),
                "df_to_logits_norm",
            );

            dispatch_pass(
                &mut enc,
                &self.pipes.gemv_f32,
                &self.to_logits_gemm_bgs[j],
                (self.n_vocab_wg_m, 1, 1),
                "df_to_logits_gemm",
            );

            // Sample on GPU (with temperature) or Argmax on GPU -> sampled_codes_buf[j]
            if use_sampling {
                dispatch_pass(
                    &mut enc,
                    &self.pipes.df_sample_logits,
                    &self.sample_bgs[j],
                    (1, 1, 1),
                    "df_sample_logits",
                );
            } else {
                dispatch_pass(
                    &mut enc,
                    &self.pipes.argmax_f32,
                    &self.argmax_bgs[j],
                    (1, 1, 1),
                    "df_argmax",
                );
            }
        }

        // Single 32-byte download of all 8 codes via pre-allocated staging buffer
        let size = (self.dec_cfg.n_codebook * 4) as u64;
        enc.copy_buffer_to_buffer(
            &self.sampled_codes_buf,
            0,
            &self.staging_readback_buf,
            0,
            size,
        );
        self.ctx.submit_encoder(enc);

        let (tx, rx) = futures_channel::oneshot::channel();
        self.staging_readback_buf
            .slice(0..size)
            .map_async(wgpu::MapMode::Read, move |result| {
                let _ = tx.send(result);
            });

        #[cfg(not(target_arch = "wasm32"))]
        self.ctx.device.poll_wait();

        let res = rx.await;
        let mut codes = [0i32; 8];
        match res {
            Ok(Ok(())) => {
                let parse_res: Result<[i32; 8]> = {
                    let slice = self.staging_readback_buf.slice(0..size);
                    match slice.get_mapped_range() {
                        Ok(data) => {
                            let sampled_u32: &[u32] = bytemuck::cast_slice(&data);
                            for (i, &c) in sampled_u32.iter().enumerate().take(8) {
                                codes[i] = c as i32;
                            }
                            Ok(codes)
                        }
                        Err(e) => Err(anyhow::anyhow!("get_mapped_range failed: {e:?}")),
                    }
                };
                self.staging_readback_buf.unmap();
                parse_res
            }
            Ok(Err(e)) => anyhow::bail!("GPU readback failed: {e:?}"),
            Err(_) => anyhow::bail!("GPU readback channel closed"),
        }
    }
}