burn_trellis 0.1.0

Trellis2 integration utilities and runtime scaffolding for burn_synth
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
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
use std::collections::HashMap;
use std::fs::File;
use std::path::{Path, PathBuf};
#[cfg(feature = "runtime-model-wgpu")]
use std::sync::OnceLock;
use std::sync::{Arc, Mutex};

use burn::module::{Module, Param, ParamId};
use burn::prelude::*;
#[cfg(feature = "runtime-model-wgpu")]
use burn::tensor::TensorData;
#[cfg(feature = "runtime-model-wgpu")]
use burn::tensor::activation::sigmoid;
#[cfg(feature = "runtime-model-wgpu")]
use burn_flex_gmm::kernel_rows;
#[cfg(feature = "runtime-model-wgpu")]
use burn_flex_gmm::wgpu::{
    DefaultWgpuBackend, SparseWgpuForwardConfig, SparseWgpuKernelVariant,
    neighbor_rows_tensor_from_coords, sparse_subm_conv_forward_wgpu_with_config,
};
use burn_flex_gmm::{
    SparseSubmConvConfig as FlexConvConfig, SparseSubmConvWeights, build_neighbor_rows,
    pack_flex_weight, sparse_subm_conv_forward_flex_precomputed,
};
use burn_store::{BurnpackStore, ModuleSnapshot};
#[cfg(feature = "runtime-model-wgpu")]
use burn_wgpu::WgpuDevice;
use half::{bf16, f16};
use memmap2::{Mmap, MmapOptions};
use safetensors::{Dtype, SafeTensors};
use serde::Deserialize;

const F16_SUFFIX: &str = "_f16";
const LAYER_NORM32_EPS: f32 = 1.0e-6;
const F_LAYER_NORM_EPS: f32 = 1.0e-5;
const DECODER_NEIGHBOR_CACHE_MAX: usize = 128;
#[cfg(feature = "runtime-model-wgpu")]
const DECODER_WGPU_TENSOR_CACHE_MAX: usize = 64;

#[derive(Debug, Clone, Default)]
pub(crate) struct DecoderConvBlockTelemetry {
    pub context: String,
    pub conv_calls: u64,
    pub wgpu_calls: u64,
    pub wgpu_successes: u64,
    pub wgpu_failures: u64,
    pub dispatches: u64,
    pub chunked_calls: u64,
    pub max_chunk_rows: usize,
    pub input_bytes: u64,
    pub output_bytes: u64,
    pub neighbor_elements: u64,
}

#[derive(Debug, Clone, Default)]
pub(crate) struct DecoderConvTelemetry {
    pub conv_calls: u64,
    pub wgpu_calls: u64,
    pub wgpu_successes: u64,
    pub wgpu_failures: u64,
    pub dispatches: u64,
    pub chunked_calls: u64,
    pub max_chunk_rows: usize,
    pub input_bytes: u64,
    pub output_bytes: u64,
    pub neighbor_elements: u64,
    pub blocks: Vec<DecoderConvBlockTelemetry>,
}

#[cfg(feature = "runtime-model-wgpu")]
#[derive(Debug, Default)]
struct DecoderConvTelemetryState {
    total: DecoderConvBlockTelemetry,
    blocks: HashMap<String, DecoderConvBlockTelemetry>,
}

#[cfg(feature = "runtime-model-wgpu")]
static DECODER_CONV_TELEMETRY: OnceLock<Mutex<DecoderConvTelemetryState>> = OnceLock::new();

#[derive(Module, Debug)]
struct BinaryBlob<B: Backend> {
    bytes: Param<Tensor<B, 1, Int>>,
}

#[derive(Debug, Clone, Deserialize)]
struct BlobMetadata {
    bytes_len: usize,
}

enum WeightsBacking {
    Mmap(Mmap),
    Bytes(Vec<u8>),
}

impl WeightsBacking {
    fn as_slice(&self) -> &[u8] {
        match self {
            Self::Mmap(mmap) => mmap.as_ref(),
            Self::Bytes(bytes) => bytes.as_slice(),
        }
    }
}

#[derive(Debug, Clone, Deserialize)]
pub(crate) struct DecoderConfigFile {
    #[allow(dead_code)]
    pub name: String,
    pub args: DecoderArgs,
}

#[derive(Debug, Clone, Deserialize)]
pub(crate) struct DecoderArgs {
    #[serde(default)]
    pub out_channels: Option<usize>,
    pub model_channels: Vec<usize>,
    pub latent_channels: usize,
    pub num_blocks: Vec<usize>,
    #[allow(dead_code)]
    pub block_type: Vec<String>,
    #[allow(dead_code)]
    pub up_block_type: Vec<String>,
    #[allow(dead_code)]
    pub block_args: Vec<serde_json::Value>,
    #[serde(default)]
    pub pred_subdiv: Option<bool>,
    #[serde(default)]
    #[allow(dead_code)]
    pub resolution: Option<usize>,
    #[serde(default)]
    pub voxel_margin: Option<f32>,
    #[serde(default)]
    pub use_fp16: Option<bool>,
}

#[derive(Debug, Clone)]
pub(crate) struct SparseSubdivisionLogits {
    pub coords: Vec<[u32; 4]>,
    pub logits: Vec<f32>,
    pub spatial_shape: [u32; 3],
}

#[derive(Debug, Clone)]
pub(crate) struct SparseDecodeResult {
    pub coords: Vec<[u32; 4]>,
    pub feats: Vec<f32>,
    pub out_channels: usize,
    pub subdivisions: Vec<SparseSubdivisionLogits>,
}

#[derive(Debug, Clone)]
pub(crate) struct SparseUnetDecoderRuntime {
    out_channels: usize,
    pred_subdiv: bool,
    voxel_margin: f32,
    compute_fp16: bool,
    model_channels: Vec<usize>,
    from_latent: LinearLayer,
    output_layer: LinearLayer,
    stages: Vec<DecoderStage>,
    conv_cache: Arc<Mutex<DecoderConvCache>>,
    #[cfg(feature = "runtime-model-wgpu")]
    wgpu_context: Option<Arc<Mutex<DecoderWgpuConvContext>>>,
}

#[derive(Debug, Clone)]
struct DecoderStage {
    convnext_blocks: Vec<ConvNeXtBlock>,
    upsample_block: Option<C2SUpsampleBlock>,
}

#[derive(Debug, Clone)]
struct ConvNeXtBlock {
    conv: SparseConvLayer,
    norm_weight: Vec<f32>,
    norm_bias: Vec<f32>,
    mlp_0: LinearLayer,
    mlp_2: LinearLayer,
}

#[derive(Debug, Clone)]
struct C2SUpsampleBlock {
    in_channels: usize,
    out_channels: usize,
    norm1_weight: Vec<f32>,
    norm1_bias: Vec<f32>,
    to_subdiv: Option<LinearLayer>,
    conv1: SparseConvLayer,
    conv2: SparseConvLayer,
}

#[derive(Debug, Clone)]
struct LinearLayer {
    in_channels: usize,
    out_channels: usize,
    // Row-major [out, in] as stored by PyTorch linear layers.
    weight: Vec<f32>,
    bias: Vec<f32>,
}

#[derive(Debug, Clone)]
struct SparseConvLayer {
    in_channels: usize,
    out_channels: usize,
    kernel_d: usize,
    kernel_h: usize,
    kernel_w: usize,
    in_channels_per_group: usize,
    out_channels_per_group: usize,
    groups: usize,
    // Row-major [out, kd, kh, kw, in_per_group]
    weight: Vec<f32>,
    bias: Vec<f32>,
    flex_packed_weight: Option<Vec<f32>>,
}

#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
struct NeighborRowsCacheKey {
    coords_hash: u64,
    rows: usize,
    kernel_d: usize,
    kernel_h: usize,
    kernel_w: usize,
    axis_order: [usize; 3],
    axis_sign: [i32; 3],
}

impl NeighborRowsCacheKey {
    fn new(config: &FlexConvConfig, coords: &[[u32; 4]]) -> Self {
        Self {
            coords_hash: hash_coords(coords),
            rows: coords.len(),
            kernel_d: config.kernel_d,
            kernel_h: config.kernel_h,
            kernel_w: config.kernel_w,
            axis_order: config.axis_order,
            axis_sign: config.axis_sign,
        }
    }
}

#[derive(Debug, Default)]
struct DecoderConvCache {
    neighbor_rows: HashMap<NeighborRowsCacheKey, Vec<i32>>,
}

impl DecoderConvCache {
    fn neighbor_rows_with_key<'a>(
        &'a mut self,
        config: &FlexConvConfig,
        coords: &[[u32; 4]],
    ) -> Result<(NeighborRowsCacheKey, &'a [i32]), String> {
        let key = NeighborRowsCacheKey::new(config, coords);
        if !self.neighbor_rows.contains_key(&key) {
            trim_hashmap(&mut self.neighbor_rows, DECODER_NEIGHBOR_CACHE_MAX);
            let neighbor_rows = build_neighbor_rows(config, coords)?;
            self.neighbor_rows.insert(key, neighbor_rows);
        }
        let rows = self
            .neighbor_rows
            .get(&key)
            .map(|rows| rows.as_slice())
            .ok_or_else(|| "decoder neighbor-row cache lookup failed".to_string())?;
        Ok((key, rows))
    }
}

#[cfg(feature = "runtime-model-wgpu")]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
struct LayerTensorCacheKey {
    weight_ptr: usize,
    bias_ptr: usize,
}

#[cfg(feature = "runtime-model-wgpu")]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
struct LinearTensorCacheKey {
    weight_ptr: usize,
    bias_ptr: usize,
    in_channels: usize,
    out_channels: usize,
}

#[cfg(feature = "runtime-model-wgpu")]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
struct VectorTensorCacheKey {
    ptr: usize,
    len: usize,
}

#[cfg(feature = "runtime-model-wgpu")]
#[derive(Debug)]
struct DecoderWgpuConvContext {
    device: WgpuDevice,
    weight_tensors: HashMap<LayerTensorCacheKey, Tensor<DefaultWgpuBackend, 5>>,
    bias_tensors: HashMap<LayerTensorCacheKey, Tensor<DefaultWgpuBackend, 1>>,
    neighbor_tensors: HashMap<NeighborRowsCacheKey, Tensor<DefaultWgpuBackend, 2, Int>>,
    linear_weight_tensors: HashMap<LinearTensorCacheKey, Tensor<DefaultWgpuBackend, 2>>,
    linear_bias_tensors: HashMap<LinearTensorCacheKey, Tensor<DefaultWgpuBackend, 1>>,
    vector_tensors: HashMap<VectorTensorCacheKey, Tensor<DefaultWgpuBackend, 1>>,
    wgpu_failed: bool,
}

#[cfg(feature = "runtime-model-wgpu")]
impl DecoderWgpuConvContext {
    fn new() -> Result<Self, String> {
        let device = WgpuDevice::default();
        let _ = Tensor::<DefaultWgpuBackend, 1>::zeros([1], &device);
        Ok(Self {
            device,
            weight_tensors: HashMap::new(),
            bias_tensors: HashMap::new(),
            neighbor_tensors: HashMap::new(),
            linear_weight_tensors: HashMap::new(),
            linear_bias_tensors: HashMap::new(),
            vector_tensors: HashMap::new(),
            wgpu_failed: false,
        })
    }

    fn layer_key(layer: &SparseConvLayer) -> LayerTensorCacheKey {
        LayerTensorCacheKey {
            weight_ptr: layer.weight.as_ptr() as usize,
            bias_ptr: layer.bias.as_ptr() as usize,
        }
    }

    fn linear_key(layer: &LinearLayer) -> LinearTensorCacheKey {
        LinearTensorCacheKey {
            weight_ptr: layer.weight.as_ptr() as usize,
            bias_ptr: layer.bias.as_ptr() as usize,
            in_channels: layer.in_channels,
            out_channels: layer.out_channels,
        }
    }

    fn vector_key(values: &[f32]) -> VectorTensorCacheKey {
        VectorTensorCacheKey {
            ptr: values.as_ptr() as usize,
            len: values.len(),
        }
    }

    fn weight_tensor(&mut self, layer: &SparseConvLayer) -> Tensor<DefaultWgpuBackend, 5> {
        if !decoder_wgpu_use_tensor_cache() {
            return Tensor::<DefaultWgpuBackend, 1>::from_floats(
                layer.weight.as_slice(),
                &self.device,
            )
            .reshape([
                layer.out_channels,
                layer.kernel_d,
                layer.kernel_h,
                layer.kernel_w,
                layer.in_channels_per_group,
            ]);
        }
        let key = Self::layer_key(layer);
        if let Some(tensor) = self.weight_tensors.get(&key) {
            return tensor.clone();
        }
        trim_hashmap(&mut self.weight_tensors, decoder_wgpu_tensor_cache_max());
        let tensor =
            Tensor::<DefaultWgpuBackend, 1>::from_floats(layer.weight.as_slice(), &self.device)
                .reshape([
                    layer.out_channels,
                    layer.kernel_d,
                    layer.kernel_h,
                    layer.kernel_w,
                    layer.in_channels_per_group,
                ]);
        self.weight_tensors.insert(key, tensor.clone());
        tensor
    }

    fn bias_tensor(&mut self, layer: &SparseConvLayer) -> Tensor<DefaultWgpuBackend, 1> {
        if !decoder_wgpu_use_tensor_cache() {
            return Tensor::<DefaultWgpuBackend, 1>::from_floats(
                layer.bias.as_slice(),
                &self.device,
            );
        }
        let key = Self::layer_key(layer);
        if let Some(tensor) = self.bias_tensors.get(&key) {
            return tensor.clone();
        }
        trim_hashmap(&mut self.bias_tensors, decoder_wgpu_tensor_cache_max());
        let tensor =
            Tensor::<DefaultWgpuBackend, 1>::from_floats(layer.bias.as_slice(), &self.device);
        self.bias_tensors.insert(key, tensor.clone());
        tensor
    }

    fn linear_weight_tensor(&mut self, layer: &LinearLayer) -> Tensor<DefaultWgpuBackend, 2> {
        if !decoder_wgpu_use_tensor_cache() {
            return Tensor::<DefaultWgpuBackend, 1>::from_floats(
                layer.weight.as_slice(),
                &self.device,
            )
            .reshape([layer.out_channels, layer.in_channels]);
        }
        let key = Self::linear_key(layer);
        if let Some(tensor) = self.linear_weight_tensors.get(&key) {
            return tensor.clone();
        }
        trim_hashmap(
            &mut self.linear_weight_tensors,
            decoder_wgpu_tensor_cache_max(),
        );
        let tensor =
            Tensor::<DefaultWgpuBackend, 1>::from_floats(layer.weight.as_slice(), &self.device)
                .reshape([layer.out_channels, layer.in_channels]);
        self.linear_weight_tensors.insert(key, tensor.clone());
        tensor
    }

    fn linear_bias_tensor(&mut self, layer: &LinearLayer) -> Tensor<DefaultWgpuBackend, 1> {
        if !decoder_wgpu_use_tensor_cache() {
            return Tensor::<DefaultWgpuBackend, 1>::from_floats(
                layer.bias.as_slice(),
                &self.device,
            );
        }
        let key = Self::linear_key(layer);
        if let Some(tensor) = self.linear_bias_tensors.get(&key) {
            return tensor.clone();
        }
        trim_hashmap(
            &mut self.linear_bias_tensors,
            decoder_wgpu_tensor_cache_max(),
        );
        let tensor =
            Tensor::<DefaultWgpuBackend, 1>::from_floats(layer.bias.as_slice(), &self.device);
        self.linear_bias_tensors.insert(key, tensor.clone());
        tensor
    }

    fn vector_tensor(&mut self, values: &[f32]) -> Tensor<DefaultWgpuBackend, 1> {
        if !decoder_wgpu_use_tensor_cache() {
            return Tensor::<DefaultWgpuBackend, 1>::from_floats(values, &self.device);
        }
        let key = Self::vector_key(values);
        if let Some(tensor) = self.vector_tensors.get(&key) {
            return tensor.clone();
        }
        trim_hashmap(&mut self.vector_tensors, decoder_wgpu_tensor_cache_max());
        let tensor = Tensor::<DefaultWgpuBackend, 1>::from_floats(values, &self.device);
        self.vector_tensors.insert(key, tensor.clone());
        tensor
    }

    fn neighbor_tensor(
        &mut self,
        key: NeighborRowsCacheKey,
        config: &FlexConvConfig,
        rows: usize,
        neighbor_rows: &[i32],
    ) -> Result<Tensor<DefaultWgpuBackend, 2, Int>, String> {
        if !decoder_wgpu_use_tensor_cache() {
            let kernel_rows = kernel_rows(config)?;
            return Ok(Tensor::<DefaultWgpuBackend, 1, Int>::from_data(
                TensorData::new(neighbor_rows.to_vec(), [rows.saturating_mul(kernel_rows)]),
                &self.device,
            )
            .reshape([rows, kernel_rows]));
        }
        if let Some(tensor) = self.neighbor_tensors.get(&key) {
            return Ok(tensor.clone());
        }
        let kernel_rows = kernel_rows(config)?;
        trim_hashmap(&mut self.neighbor_tensors, decoder_wgpu_tensor_cache_max());
        let tensor = Tensor::<DefaultWgpuBackend, 1, Int>::from_data(
            TensorData::new(neighbor_rows.to_vec(), [rows.saturating_mul(kernel_rows)]),
            &self.device,
        )
        .reshape([rows, kernel_rows]);
        self.neighbor_tensors.insert(key, tensor.clone());
        Ok(tensor)
    }

    fn forward_with_neighbor_rows(
        &mut self,
        config: &FlexConvConfig,
        layer: &SparseConvLayer,
        input: &[f32],
        context: &str,
        cache_key: NeighborRowsCacheKey,
        neighbor_rows: &[i32],
    ) -> Result<Vec<f32>, String> {
        if self.wgpu_failed {
            return Err("wgpu sparse conv disabled after prior failure".to_string());
        }
        if config.in_channels == 0 {
            return Ok(Vec::new());
        }
        if !input.len().is_multiple_of(config.in_channels) {
            return Err(format!(
                "wgpu sparse conv input len mismatch: len={} in_channels={}",
                input.len(),
                config.in_channels
            ));
        }
        let rows = input.len() / config.in_channels;
        let kernel_rows = kernel_rows(config)?;
        if neighbor_rows.len() != rows.saturating_mul(kernel_rows) {
            return Err(format!(
                "wgpu sparse conv neighbor len mismatch: len={} expected={}",
                neighbor_rows.len(),
                rows.saturating_mul(kernel_rows)
            ));
        }
        let neighbor_t = self.neighbor_tensor(cache_key, config, rows, neighbor_rows)?;
        self.forward_with_neighbor_tensor(
            config,
            layer,
            input,
            context,
            rows,
            kernel_rows,
            neighbor_t,
        )
    }

    fn forward_with_coords(
        &mut self,
        config: &FlexConvConfig,
        layer: &SparseConvLayer,
        input: &[f32],
        context: &str,
        coords: &[[u32; 4]],
    ) -> Result<Vec<f32>, String> {
        if self.wgpu_failed {
            return Err("wgpu sparse conv disabled after prior failure".to_string());
        }
        if config.in_channels == 0 {
            return Ok(Vec::new());
        }
        if !input.len().is_multiple_of(config.in_channels) {
            return Err(format!(
                "wgpu sparse conv input len mismatch: len={} in_channels={}",
                input.len(),
                config.in_channels
            ));
        }
        let rows = input.len() / config.in_channels;
        if coords.len() != rows {
            return Err(format!(
                "wgpu sparse conv coord/input row mismatch: coords={} rows={rows}",
                coords.len()
            ));
        }
        let kernel_rows = kernel_rows(config)?;
        let neighbor_t = neighbor_rows_tensor_from_coords(config, coords, &self.device)?;
        self.forward_with_neighbor_tensor(
            config,
            layer,
            input,
            context,
            rows,
            kernel_rows,
            neighbor_t,
        )
    }

    fn forward_with_coords_tensor(
        &mut self,
        config: &FlexConvConfig,
        layer: &SparseConvLayer,
        input_t: Tensor<DefaultWgpuBackend, 2>,
        context: &str,
        coords: &[[u32; 4]],
    ) -> Result<Tensor<DefaultWgpuBackend, 2>, String> {
        if self.wgpu_failed {
            return Err("wgpu sparse conv disabled after prior failure".to_string());
        }
        if config.in_channels == 0 {
            return Ok(input_t);
        }
        let [rows, in_channels] = input_t.dims();
        if in_channels != config.in_channels {
            return Err(format!(
                "wgpu sparse conv input channel mismatch: input_channels={} in_channels={}",
                in_channels, config.in_channels
            ));
        }
        if coords.len() != rows {
            return Err(format!(
                "wgpu sparse conv coord/input row mismatch: coords={} rows={rows}",
                coords.len()
            ));
        }
        let output_bytes = rows
            .checked_mul(config.out_channels)
            .and_then(|value| value.checked_mul(core::mem::size_of::<f32>()))
            .ok_or_else(|| "wgpu sparse conv output-byte-size overflow".to_string())?;
        let max_output_bytes = decoder_wgpu_max_output_bytes();
        if output_bytes > max_output_bytes {
            return Err(format!(
                "wgpu sparse conv tensor output exceeds per-dispatch guard: bytes={} max_bytes={}",
                output_bytes, max_output_bytes
            ));
        }
        let kernel_rows = kernel_rows(config)?;
        let neighbor_t = neighbor_rows_tensor_from_coords(config, coords, &self.device)?;
        self.forward_with_neighbor_tensor_tensor(
            config,
            layer,
            input_t,
            context,
            rows,
            kernel_rows,
            neighbor_t,
        )
    }

    #[allow(clippy::too_many_arguments)]
    fn forward_with_neighbor_tensor(
        &mut self,
        config: &FlexConvConfig,
        layer: &SparseConvLayer,
        input: &[f32],
        context: &str,
        rows: usize,
        kernel_rows: usize,
        neighbor_t: Tensor<DefaultWgpuBackend, 2, Int>,
    ) -> Result<Vec<f32>, String> {
        let input_t = Tensor::<DefaultWgpuBackend, 1>::from_floats(input, &self.device)
            .reshape([rows, config.in_channels]);
        let output = self.forward_with_neighbor_tensor_tensor(
            config,
            layer,
            input_t,
            context,
            rows,
            kernel_rows,
            neighbor_t,
        )?;
        output
            .into_data()
            .convert::<f32>()
            .to_vec::<f32>()
            .map_err(|err| format!("failed to read wgpu sparse conv output: {err:?}"))
    }

    #[allow(clippy::too_many_arguments)]
    fn forward_with_neighbor_tensor_tensor(
        &mut self,
        config: &FlexConvConfig,
        layer: &SparseConvLayer,
        input_t: Tensor<DefaultWgpuBackend, 2>,
        context: &str,
        rows: usize,
        kernel_rows: usize,
        neighbor_t: Tensor<DefaultWgpuBackend, 2, Int>,
    ) -> Result<Tensor<DefaultWgpuBackend, 2>, String> {
        let [query_rows, neighbor_kernel_rows] = neighbor_t.dims();
        if query_rows != rows {
            return Err(format!(
                "wgpu sparse conv neighbor row mismatch: rows={rows} neighbor_rows={query_rows}"
            ));
        }
        if neighbor_kernel_rows != kernel_rows {
            return Err(format!(
                "wgpu sparse conv neighbor kernel rows mismatch: got={} expected={}",
                neighbor_kernel_rows, kernel_rows
            ));
        }
        let input_elements = rows
            .checked_mul(config.in_channels)
            .ok_or_else(|| "wgpu sparse conv input-element overflow".to_string())?;
        let input_bytes = input_elements
            .checked_mul(core::mem::size_of::<f32>())
            .ok_or_else(|| "wgpu sparse conv input-byte-size overflow".to_string())?;
        let max_input_bytes = decoder_wgpu_max_input_bytes();
        if input_bytes > max_input_bytes {
            return Err(format!(
                "wgpu sparse conv input too large: bytes={} max_bytes={}",
                input_bytes, max_input_bytes
            ));
        }
        let [input_rows, input_channels] = input_t.dims();
        if input_rows != rows || input_channels != config.in_channels {
            return Err(format!(
                "wgpu sparse conv tensor dims mismatch: got=[{},{}] expected=[{},{}]",
                input_rows, input_channels, rows, config.in_channels
            ));
        }
        let weight_t = self.weight_tensor(layer);
        let bias_t = self.bias_tensor(layer);
        let bytes_per_row = config
            .out_channels
            .checked_mul(core::mem::size_of::<f32>())
            .ok_or_else(|| "wgpu sparse conv bytes-per-row overflow".to_string())?;
        let max_output_bytes = decoder_wgpu_max_output_bytes();
        let output_bytes = rows
            .checked_mul(bytes_per_row)
            .ok_or_else(|| "wgpu sparse conv output-byte-size overflow".to_string())?;
        // TODO(perf-kernel-2): Replace heuristic hotspot overrides with dedicated fused
        // sparse decoder kernels (fused gather+GEMM plus subgroup-tiling schedule).
        let forward_cfg =
            decoder_wgpu_forward_config_for_call(config, rows, output_bytes, max_output_bytes);
        if output_bytes <= max_output_bytes {
            let output = sparse_subm_conv_forward_wgpu_with_config(
                config,
                input_t,
                neighbor_t,
                weight_t,
                bias_t,
                forward_cfg,
            )?;
            telemetry_record_wgpu_success(
                context,
                1,
                false,
                rows,
                input_bytes,
                output_bytes,
                rows.saturating_mul(kernel_rows),
            );
            return Ok(output);
        }

        let chunk_rows = decoder_wgpu_chunk_rows(rows, bytes_per_row, max_output_bytes);
        if decoder_conv_debug_enabled() {
            eprintln!(
                "burn_trellis: chunking wgpu sparse conv rows={} chunk_rows={} out_channels={} bytes={} max_bytes={}",
                rows, chunk_rows, config.out_channels, output_bytes, max_output_bytes
            );
        }
        let mut start = 0usize;
        let mut dispatches = 0u64;
        let mut chunk_tensors: Vec<Tensor<DefaultWgpuBackend, 2>> = Vec::new();
        while start < rows {
            let end = (start + chunk_rows).min(rows);
            let chunk_neighbor_t = neighbor_t.clone().slice([start..end, 0..kernel_rows]);
            let chunk_out = sparse_subm_conv_forward_wgpu_with_config(
                config,
                input_t.clone(),
                chunk_neighbor_t,
                weight_t.clone(),
                bias_t.clone(),
                forward_cfg,
            )?;
            chunk_tensors.push(chunk_out);
            start = end;
            dispatches = dispatches.saturating_add(1);
        }
        telemetry_record_wgpu_success(
            context,
            dispatches.max(1),
            true,
            chunk_rows,
            input_bytes,
            output_bytes,
            rows.saturating_mul(kernel_rows),
        );
        if chunk_tensors.is_empty() {
            return Ok(Tensor::<DefaultWgpuBackend, 2>::zeros(
                [rows, config.out_channels],
                &self.device,
            ));
        }
        if chunk_tensors.len() == 1 {
            return Ok(chunk_tensors.remove(0));
        }
        Ok(Tensor::cat(chunk_tensors, 0))
    }

    fn clear_caches(&mut self) {
        self.weight_tensors.clear();
        self.bias_tensors.clear();
        self.neighbor_tensors.clear();
        self.linear_weight_tensors.clear();
        self.linear_bias_tensors.clear();
        self.vector_tensors.clear();
    }
}

#[cfg(feature = "runtime-model-wgpu")]
fn create_wgpu_decoder_context() -> Option<Arc<Mutex<DecoderWgpuConvContext>>> {
    let context = std::panic::catch_unwind(DecoderWgpuConvContext::new)
        .ok()?
        .ok()?;
    Some(Arc::new(Mutex::new(context)))
}

fn hash_coords(coords: &[[u32; 4]]) -> u64 {
    const OFFSET: u64 = 0xcbf29ce484222325;
    const PRIME: u64 = 0x0000_0100_0000_01B3;
    let mut hash = OFFSET;
    for coord in coords {
        for value in coord {
            hash ^= *value as u64;
            hash = hash.wrapping_mul(PRIME);
        }
    }
    hash ^= coords.len() as u64;
    hash
}

fn trim_hashmap<K, V>(map: &mut HashMap<K, V>, max_entries: usize)
where
    K: Eq + std::hash::Hash + Copy,
{
    if map.len() < max_entries.max(1) {
        return;
    }
    if let Some(key) = map.keys().next().copied() {
        map.remove(&key);
    }
}

impl SparseUnetDecoderRuntime {
    pub fn load_from_stem(
        weights_root: &Path,
        image_large_root: Option<&Path>,
        model_stem: &str,
    ) -> Result<Self, String> {
        let config_path =
            resolve_model_source_path(model_stem, "json", weights_root, image_large_root);
        let config_bytes = std::fs::read(&config_path).map_err(|err| {
            format!(
                "failed to read sparse decoder config '{}': {err}",
                config_path.display()
            )
        })?;
        let parsed: DecoderConfigFile = serde_json::from_slice(&config_bytes).map_err(|err| {
            format!(
                "failed to parse sparse decoder config '{}': {err}",
                config_path.display()
            )
        })?;
        if parsed.args.model_channels.is_empty() {
            return Err(format!(
                "sparse decoder config '{}' has empty model_channels",
                config_path.display()
            ));
        }
        if parsed.args.num_blocks.len() != parsed.args.model_channels.len() {
            return Err(format!(
                "sparse decoder config '{}' has mismatched num_blocks/model_channels lengths",
                config_path.display()
            ));
        }

        let weight_path =
            resolve_model_weight_candidates(model_stem, weights_root, image_large_root)
                .into_iter()
                .next()
                .ok_or_else(|| {
                    format!("unable to resolve decoder weights for stem '{model_stem}'")
                })?;

        let weight_backing = load_weight_backing(&weight_path)?;
        let safetensors = SafeTensors::deserialize(weight_backing.as_slice()).map_err(|err| {
            format!(
                "failed to deserialize sparse decoder weights '{}' as safetensors: {err}",
                weight_path.display()
            )
        })?;

        let out_channels = parsed.args.out_channels.unwrap_or_else(|| {
            if parsed.name == "FlexiDualGridVaeDecoder" {
                7
            } else {
                6
            }
        });

        let from_latent = load_linear(
            &safetensors,
            "from_latent.weight",
            "from_latent.bias",
            parsed.args.latent_channels,
            parsed.args.model_channels[0],
        )?;
        let output_layer = load_linear(
            &safetensors,
            "output_layer.weight",
            "output_layer.bias",
            *parsed
                .args
                .model_channels
                .last()
                .expect("checked non-empty model_channels"),
            out_channels,
        )?;

        let mut stages = Vec::with_capacity(parsed.args.num_blocks.len());
        for stage_idx in 0..parsed.args.num_blocks.len() {
            let stage_channels = parsed.args.model_channels[stage_idx];
            let mut convnext_blocks = Vec::with_capacity(parsed.args.num_blocks[stage_idx]);
            for block_idx in 0..parsed.args.num_blocks[stage_idx] {
                let prefix = format!("blocks.{stage_idx}.{block_idx}");
                convnext_blocks.push(ConvNeXtBlock {
                    conv: load_sparse_conv(
                        &safetensors,
                        format!("{prefix}.conv.weight").as_str(),
                        format!("{prefix}.conv.bias").as_str(),
                        stage_channels,
                        stage_channels,
                    )?,
                    norm_weight: load_vector(
                        &safetensors,
                        format!("{prefix}.norm.weight").as_str(),
                        stage_channels,
                    )?,
                    norm_bias: load_vector(
                        &safetensors,
                        format!("{prefix}.norm.bias").as_str(),
                        stage_channels,
                    )?,
                    mlp_0: load_linear_dynamic(
                        &safetensors,
                        format!("{prefix}.mlp.0.weight").as_str(),
                        format!("{prefix}.mlp.0.bias").as_str(),
                        stage_channels,
                    )?,
                    mlp_2: load_linear_dynamic(
                        &safetensors,
                        format!("{prefix}.mlp.2.weight").as_str(),
                        format!("{prefix}.mlp.2.bias").as_str(),
                        0,
                    )?,
                });
            }

            let upsample_block = if stage_idx + 1 < parsed.args.model_channels.len() {
                let up_idx = parsed.args.num_blocks[stage_idx];
                let prefix = format!("blocks.{stage_idx}.{up_idx}");
                let in_channels = parsed.args.model_channels[stage_idx];
                let out_channels = parsed.args.model_channels[stage_idx + 1];
                let conv1_out = out_channels
                    .checked_mul(8)
                    .ok_or_else(|| "conv1_out channels overflow".to_string())?;
                let to_subdiv = match parsed.args.pred_subdiv.unwrap_or(true) {
                    true => Some(load_linear(
                        &safetensors,
                        format!("{prefix}.to_subdiv.weight").as_str(),
                        format!("{prefix}.to_subdiv.bias").as_str(),
                        in_channels,
                        8,
                    )?),
                    false => None,
                };

                Some(C2SUpsampleBlock {
                    in_channels,
                    out_channels,
                    norm1_weight: load_vector(
                        &safetensors,
                        format!("{prefix}.norm1.weight").as_str(),
                        in_channels,
                    )?,
                    norm1_bias: load_vector(
                        &safetensors,
                        format!("{prefix}.norm1.bias").as_str(),
                        in_channels,
                    )?,
                    to_subdiv,
                    conv1: load_sparse_conv(
                        &safetensors,
                        format!("{prefix}.conv1.weight").as_str(),
                        format!("{prefix}.conv1.bias").as_str(),
                        in_channels,
                        conv1_out,
                    )?,
                    conv2: load_sparse_conv(
                        &safetensors,
                        format!("{prefix}.conv2.weight").as_str(),
                        format!("{prefix}.conv2.bias").as_str(),
                        out_channels,
                        out_channels,
                    )?,
                })
            } else {
                None
            };

            stages.push(DecoderStage {
                convnext_blocks,
                upsample_block,
            });
        }

        Ok(Self {
            out_channels,
            pred_subdiv: parsed.args.pred_subdiv.unwrap_or(true),
            voxel_margin: parsed.args.voxel_margin.unwrap_or(0.5),
            compute_fp16: parsed.args.use_fp16.unwrap_or(false) && !decoder_force_fp32(),
            model_channels: parsed.args.model_channels,
            from_latent,
            output_layer,
            stages,
            conv_cache: Arc::new(Mutex::new(DecoderConvCache::default())),
            #[cfg(feature = "runtime-model-wgpu")]
            wgpu_context: create_wgpu_decoder_context(),
        })
    }

    pub fn out_channels(&self) -> usize {
        self.out_channels
    }

    pub fn pred_subdiv(&self) -> bool {
        self.pred_subdiv
    }

    pub fn voxel_margin(&self) -> f32 {
        self.voxel_margin
    }

    pub fn decode(
        &self,
        coords: &[[u32; 4]],
        rows: &[[f32; 32]],
        guide_subdivisions: Option<&[SparseSubdivisionLogits]>,
    ) -> Result<SparseDecodeResult, String> {
        let count = coords.len().min(rows.len());
        if count == 0 {
            return Ok(SparseDecodeResult {
                coords: Vec::new(),
                feats: Vec::new(),
                out_channels: self.out_channels,
                subdivisions: Vec::new(),
            });
        }

        let mut state_coords = coords[..count].to_vec();
        let mut state_feats = flatten_rows_32(&rows[..count]);
        #[cfg(feature = "runtime-model-wgpu")]
        let mut state_feats_wgpu: Option<Tensor<DefaultWgpuBackend, 2>> = None;
        let mut conv_cache = self
            .conv_cache
            .lock()
            .map_err(|_| "decoder conv cache lock poisoned".to_string())?;
        #[cfg(feature = "runtime-model-wgpu")]
        let mut wgpu_context = if let Some(context) = self.wgpu_context.as_ref() {
            Some(
                context
                    .lock()
                    .map_err(|_| "decoder wgpu context lock poisoned".to_string())?,
            )
        } else {
            None
        };
        state_feats = linear_forward(
            state_feats.as_slice(),
            count,
            &self.from_latent,
            "from_latent",
        )?;
        if self.compute_fp16 {
            quantize_f16_inplace(state_feats.as_mut_slice());
        }

        let mut subdivisions = Vec::new();
        for (stage_idx, stage) in self.stages.iter().enumerate() {
            let stage_channels = self.model_channels[stage_idx];
            #[allow(unused_mut)]
            let mut convnext_device_complete = false;
            #[cfg(feature = "runtime-model-wgpu")]
            if decoder_wgpu_device_math_enabled()
                && (!self.compute_fp16 || decoder_wgpu_device_math_allow_fp16())
                && !stage.convnext_blocks.is_empty()
                && let Some(context_gpu) = wgpu_context.as_deref_mut()
            {
                let row_count = state_coords.len();
                let state_t = if let Some(state_t) = state_feats_wgpu.take() {
                    let [rows_device, channels_device] = state_t.dims();
                    if rows_device == row_count && channels_device == stage_channels {
                        state_t
                    } else {
                        Tensor::<DefaultWgpuBackend, 1>::from_floats(
                            state_feats.as_slice(),
                            &context_gpu.device,
                        )
                        .reshape([row_count, stage_channels])
                    }
                } else {
                    Tensor::<DefaultWgpuBackend, 1>::from_floats(
                        state_feats.as_slice(),
                        &context_gpu.device,
                    )
                    .reshape([row_count, stage_channels])
                };
                match convnext_blocks_forward_wgpu_tensor(
                    context_gpu,
                    state_coords.as_slice(),
                    state_t,
                    stage_idx,
                    stage_channels,
                    stage.convnext_blocks.as_slice(),
                ) {
                    Ok(next_state_feats) => {
                        state_feats_wgpu = Some(next_state_feats);
                        convnext_device_complete = true;
                    }
                    Err(err) => {
                        state_feats_wgpu = None;
                        if decoder_conv_debug_enabled() {
                            eprintln!(
                                "burn_trellis: wgpu convnext stage fallback to cpu stage={} reason={err}",
                                stage_idx
                            );
                        }
                    }
                }
            }
            if !convnext_device_complete {
                #[cfg(feature = "runtime-model-wgpu")]
                if let Some(state_t) = state_feats_wgpu.take() {
                    let context =
                        format!("decoder stage {stage_idx} convnext fallback state readback");
                    state_feats = tensor_to_vec_f32(state_t, context.as_str())?;
                }
                for (block_idx, block) in stage.convnext_blocks.iter().enumerate() {
                    let row_count = state_coords.len();
                    if row_count == 0 {
                        break;
                    }
                    let residual = state_feats.clone();
                    let mut h = sparse_subm_conv_forward(
                        state_coords.as_slice(),
                        state_feats.as_slice(),
                        &block.conv,
                        format!("stage {stage_idx} block {block_idx} conv").as_str(),
                        &mut conv_cache,
                        #[cfg(feature = "runtime-model-wgpu")]
                        wgpu_context.as_deref_mut(),
                    )?;
                    if self.compute_fp16 {
                        quantize_f16_inplace(h.as_mut_slice());
                    }
                    layer_norm_inplace(
                        h.as_mut_slice(),
                        row_count,
                        stage_channels,
                        Some(block.norm_weight.as_slice()),
                        Some(block.norm_bias.as_slice()),
                        LAYER_NORM32_EPS,
                    )?;
                    if self.compute_fp16 {
                        quantize_f16_inplace(h.as_mut_slice());
                    }
                    h = linear_forward(
                        h.as_slice(),
                        row_count,
                        &block.mlp_0,
                        format!("stage {stage_idx} block {block_idx} mlp_0").as_str(),
                    )?;
                    if self.compute_fp16 {
                        quantize_f16_inplace(h.as_mut_slice());
                    }
                    silu_inplace(h.as_mut_slice());
                    if self.compute_fp16 {
                        quantize_f16_inplace(h.as_mut_slice());
                    }
                    h = linear_forward(
                        h.as_slice(),
                        row_count,
                        &block.mlp_2,
                        format!("stage {stage_idx} block {block_idx} mlp_2").as_str(),
                    )?;
                    if self.compute_fp16 {
                        quantize_f16_inplace(h.as_mut_slice());
                    }
                    add_inplace(h.as_mut_slice(), residual.as_slice());
                    if self.compute_fp16 {
                        quantize_f16_inplace(h.as_mut_slice());
                    }
                    state_feats = h;
                }
            }

            if let Some(up) = stage.upsample_block.as_ref() {
                #[cfg(feature = "runtime-model-wgpu")]
                if let Some(state_t) = state_feats_wgpu.take() {
                    let context = format!("decoder stage {stage_idx} upsample state readback");
                    state_feats = tensor_to_vec_f32(state_t, context.as_str())?;
                }
                let parent_coords = state_coords.clone();
                let parent_feats = state_feats.clone();
                let parent_rows = parent_coords.len();
                if parent_rows == 0 {
                    continue;
                }

                let subdiv_logits = if let Some(to_subdiv) = up.to_subdiv.as_ref() {
                    let mut logits = linear_forward(
                        parent_feats.as_slice(),
                        parent_rows,
                        to_subdiv,
                        format!("stage {stage_idx} to_subdiv").as_str(),
                    )?;
                    if self.compute_fp16 {
                        quantize_f16_inplace(logits.as_mut_slice());
                    }
                    if should_center_subdivision_logits() {
                        row_center_logits(logits.as_mut_slice(), parent_rows);
                    }
                    logits
                } else {
                    let guide = guide_subdivisions
                        .and_then(|levels| levels.get(stage_idx))
                        .ok_or_else(|| {
                            format!(
                                "decoder stage {stage_idx} requires guide_subdivisions but none were provided"
                            )
                        })?;
                    map_guide_subdivision_logits(parent_coords.as_slice(), guide)?
                };

                let subdivision_mask =
                    logits_to_mask(subdiv_logits.as_slice(), parent_rows, false)?;
                if self.pred_subdiv {
                    subdivisions.push(SparseSubdivisionLogits {
                        spatial_shape: spatial_shape_from_coords(parent_coords.as_slice()),
                        coords: parent_coords.clone(),
                        logits: subdiv_logits.clone(),
                    });
                }

                let mut h_norm = parent_feats.clone();
                layer_norm_inplace(
                    h_norm.as_mut_slice(),
                    parent_rows,
                    up.in_channels,
                    Some(up.norm1_weight.as_slice()),
                    Some(up.norm1_bias.as_slice()),
                    LAYER_NORM32_EPS,
                )?;
                if self.compute_fp16 {
                    quantize_f16_inplace(h_norm.as_mut_slice());
                }
                silu_inplace(h_norm.as_mut_slice());
                if self.compute_fp16 {
                    quantize_f16_inplace(h_norm.as_mut_slice());
                }
                let h_conv1 = sparse_subm_conv_forward(
                    parent_coords.as_slice(),
                    h_norm.as_slice(),
                    &up.conv1,
                    format!("stage {stage_idx} up conv1").as_str(),
                    &mut conv_cache,
                    #[cfg(feature = "runtime-model-wgpu")]
                    wgpu_context.as_deref_mut(),
                )?;
                let mut h_conv1 = h_conv1;
                if self.compute_fp16 {
                    quantize_f16_inplace(h_conv1.as_mut_slice());
                }
                let (child_coords, mut h_up) = channel2spatial(
                    parent_coords.as_slice(),
                    h_conv1.as_slice(),
                    up.out_channels
                        .checked_mul(8)
                        .ok_or_else(|| "up.out_channels * 8 overflow".to_string())?,
                    subdivision_mask.as_slice(),
                )?;
                let (child_coords_skip, x_up) = channel2spatial(
                    parent_coords.as_slice(),
                    parent_feats.as_slice(),
                    up.in_channels,
                    subdivision_mask.as_slice(),
                )?;
                if child_coords != child_coords_skip {
                    return Err(format!(
                        "decoder stage {stage_idx} channel2spatial coord mismatch between conv and skip branches"
                    ));
                }

                let skip_in_channels = up.in_channels / 8;
                if skip_in_channels == 0 || up.out_channels % skip_in_channels != 0 {
                    return Err(format!(
                        "decoder stage {stage_idx} invalid skip channel ratio (in={}, out={})",
                        up.in_channels, up.out_channels
                    ));
                }
                let repeat_factor = up.out_channels / skip_in_channels;
                let skip = repeat_interleave_channels(
                    x_up.as_slice(),
                    child_coords.len(),
                    skip_in_channels,
                    repeat_factor,
                );

                let child_rows = child_coords.len();
                #[allow(unused_mut)]
                let mut upsample_device_complete = false;
                #[cfg(feature = "runtime-model-wgpu")]
                if decoder_wgpu_device_math_enabled()
                    && (!self.compute_fp16 || decoder_wgpu_device_math_allow_fp16())
                    && child_rows > 0
                    && let Some(context_gpu) = wgpu_context.as_deref_mut()
                {
                    let h_up_t = Tensor::<DefaultWgpuBackend, 1>::from_floats(
                        h_up.as_slice(),
                        &context_gpu.device,
                    )
                    .reshape([child_rows, up.out_channels]);
                    let h_up_t = layer_norm_wgpu(
                        context_gpu,
                        h_up_t,
                        child_rows,
                        up.out_channels,
                        None,
                        None,
                        LAYER_NORM32_EPS,
                    )?;
                    let h_up_t = silu_wgpu(h_up_t);
                    let config = flex_config_for_layer(&up.conv2);
                    match context_gpu.forward_with_coords_tensor(
                        &config,
                        &up.conv2,
                        h_up_t,
                        format!("stage {stage_idx} up conv2(wgpu_math)").as_str(),
                        child_coords.as_slice(),
                    ) {
                        Ok(h_t) => {
                            let skip_t = Tensor::<DefaultWgpuBackend, 1>::from_floats(
                                skip.as_slice(),
                                &context_gpu.device,
                            )
                            .reshape([child_rows, up.out_channels]);
                            state_feats_wgpu = Some(h_t.add(skip_t));
                            upsample_device_complete = true;
                        }
                        Err(err) => {
                            state_feats_wgpu = None;
                            if decoder_conv_debug_enabled() {
                                eprintln!(
                                    "burn_trellis: wgpu upsample conv2 fallback to cpu stage={} reason={err}",
                                    stage_idx
                                );
                            }
                        }
                    }
                }

                if !upsample_device_complete {
                    layer_norm_inplace(
                        h_up.as_mut_slice(),
                        child_rows,
                        up.out_channels,
                        None,
                        None,
                        LAYER_NORM32_EPS,
                    )?;
                    if self.compute_fp16 {
                        quantize_f16_inplace(h_up.as_mut_slice());
                    }
                    silu_inplace(h_up.as_mut_slice());
                    if self.compute_fp16 {
                        quantize_f16_inplace(h_up.as_mut_slice());
                    }
                    let mut h = sparse_subm_conv_forward(
                        child_coords.as_slice(),
                        h_up.as_slice(),
                        &up.conv2,
                        format!("stage {stage_idx} up conv2").as_str(),
                        &mut conv_cache,
                        #[cfg(feature = "runtime-model-wgpu")]
                        wgpu_context.as_deref_mut(),
                    )?;
                    if self.compute_fp16 {
                        quantize_f16_inplace(h.as_mut_slice());
                    }
                    add_inplace(h.as_mut_slice(), skip.as_slice());
                    if self.compute_fp16 {
                        quantize_f16_inplace(h.as_mut_slice());
                    }
                    state_feats = h;
                    #[cfg(feature = "runtime-model-wgpu")]
                    {
                        state_feats_wgpu = None;
                    }
                } else {
                    state_feats.clear();
                }
                state_coords = child_coords;
            }
        }

        let rows_final = state_coords.len();
        let final_channels = *self
            .model_channels
            .last()
            .expect("checked non-empty model_channels");
        let state_feats = {
            #[cfg(feature = "runtime-model-wgpu")]
            {
                if let Some(state_t) = state_feats_wgpu.take() {
                    if decoder_wgpu_device_math_enabled()
                        && (!self.compute_fp16 || decoder_wgpu_device_math_allow_fp16())
                    {
                        if let Some(context_gpu) = wgpu_context.as_deref_mut() {
                            let state_t = layer_norm_wgpu(
                                context_gpu,
                                state_t,
                                rows_final,
                                final_channels,
                                None,
                                None,
                                F_LAYER_NORM_EPS,
                            )?;
                            let state_t = linear_forward_wgpu(
                                context_gpu,
                                state_t,
                                &self.output_layer,
                                "output_layer(wgpu_math)",
                            )?;
                            tensor_to_vec_f32(state_t, "output_layer(wgpu_math)")?
                        } else {
                            let mut state_feats =
                                tensor_to_vec_f32(state_t, "output_layer state readback")?;
                            layer_norm_inplace(
                                state_feats.as_mut_slice(),
                                rows_final,
                                final_channels,
                                None,
                                None,
                                F_LAYER_NORM_EPS,
                            )?;
                            linear_forward(
                                state_feats.as_slice(),
                                rows_final,
                                &self.output_layer,
                                "output_layer",
                            )?
                        }
                    } else {
                        let mut state_feats =
                            tensor_to_vec_f32(state_t, "output_layer state readback")?;
                        layer_norm_inplace(
                            state_feats.as_mut_slice(),
                            rows_final,
                            final_channels,
                            None,
                            None,
                            F_LAYER_NORM_EPS,
                        )?;
                        linear_forward(
                            state_feats.as_slice(),
                            rows_final,
                            &self.output_layer,
                            "output_layer",
                        )?
                    }
                } else {
                    layer_norm_inplace(
                        state_feats.as_mut_slice(),
                        rows_final,
                        final_channels,
                        None,
                        None,
                        F_LAYER_NORM_EPS,
                    )?;
                    linear_forward(
                        state_feats.as_slice(),
                        rows_final,
                        &self.output_layer,
                        "output_layer",
                    )?
                }
            }
            #[cfg(not(feature = "runtime-model-wgpu"))]
            {
                layer_norm_inplace(
                    state_feats.as_mut_slice(),
                    rows_final,
                    final_channels,
                    None,
                    None,
                    F_LAYER_NORM_EPS,
                )?;
                linear_forward(
                    state_feats.as_slice(),
                    rows_final,
                    &self.output_layer,
                    "output_layer",
                )?
            }
        };

        #[cfg(feature = "runtime-model-wgpu")]
        if decoder_wgpu_clear_cache_after_decode()
            && let Some(context) = wgpu_context.as_deref_mut()
        {
            context.clear_caches();
        }

        Ok(SparseDecodeResult {
            coords: state_coords,
            feats: state_feats,
            out_channels: self.out_channels,
            subdivisions,
        })
    }

    #[cfg_attr(not(test), allow(dead_code))]
    pub fn stage0_subdivision_logits(
        &self,
        coords: &[[u32; 4]],
        rows: &[[f32; 32]],
    ) -> Result<SparseSubdivisionLogits, String> {
        if self.stages.is_empty() {
            return Err("decoder has no stages".to_string());
        }
        let stage = &self.stages[0];
        let up = stage
            .upsample_block
            .as_ref()
            .ok_or_else(|| "decoder stage0 has no upsample block".to_string())?;
        let to_subdiv = up
            .to_subdiv
            .as_ref()
            .ok_or_else(|| "decoder stage0 has no to_subdiv head".to_string())?;

        let count = coords.len().min(rows.len());
        if count == 0 {
            return Ok(SparseSubdivisionLogits {
                coords: Vec::new(),
                logits: Vec::new(),
                spatial_shape: [1, 1, 1],
            });
        }

        let state_coords = coords[..count].to_vec();
        let mut state_feats = flatten_rows_32(&rows[..count]);
        #[cfg(feature = "runtime-model-wgpu")]
        let mut state_feats_wgpu: Option<Tensor<DefaultWgpuBackend, 2>> = None;
        let mut conv_cache = self
            .conv_cache
            .lock()
            .map_err(|_| "decoder conv cache lock poisoned".to_string())?;
        #[cfg(feature = "runtime-model-wgpu")]
        let mut wgpu_context = if let Some(context) = self.wgpu_context.as_ref() {
            Some(
                context
                    .lock()
                    .map_err(|_| "decoder wgpu context lock poisoned".to_string())?,
            )
        } else {
            None
        };
        state_feats = linear_forward(
            state_feats.as_slice(),
            count,
            &self.from_latent,
            "from_latent(stage0)",
        )?;
        if self.compute_fp16 {
            quantize_f16_inplace(state_feats.as_mut_slice());
        }

        let stage_channels = self.model_channels[0];
        #[allow(unused_mut)]
        let mut convnext_device_complete = false;
        #[cfg(feature = "runtime-model-wgpu")]
        if decoder_wgpu_device_math_enabled()
            && (!self.compute_fp16 || decoder_wgpu_device_math_allow_fp16())
            && !stage.convnext_blocks.is_empty()
            && let Some(context_gpu) = wgpu_context.as_deref_mut()
        {
            let row_count = state_coords.len();
            let state_t = if let Some(state_t) = state_feats_wgpu.take() {
                let [rows_device, channels_device] = state_t.dims();
                if rows_device == row_count && channels_device == stage_channels {
                    state_t
                } else {
                    Tensor::<DefaultWgpuBackend, 1>::from_floats(
                        state_feats.as_slice(),
                        &context_gpu.device,
                    )
                    .reshape([row_count, stage_channels])
                }
            } else {
                Tensor::<DefaultWgpuBackend, 1>::from_floats(
                    state_feats.as_slice(),
                    &context_gpu.device,
                )
                .reshape([row_count, stage_channels])
            };
            match convnext_blocks_forward_wgpu_tensor(
                context_gpu,
                state_coords.as_slice(),
                state_t,
                0,
                stage_channels,
                stage.convnext_blocks.as_slice(),
            ) {
                Ok(next_state_feats) => {
                    state_feats_wgpu = Some(next_state_feats);
                    convnext_device_complete = true;
                }
                Err(err) => {
                    state_feats_wgpu = None;
                    if decoder_conv_debug_enabled() {
                        eprintln!(
                            "burn_trellis: wgpu stage0 convnext fallback to cpu reason={err}"
                        );
                    }
                }
            }
        }

        if !convnext_device_complete {
            #[cfg(feature = "runtime-model-wgpu")]
            if let Some(state_t) = state_feats_wgpu.take() {
                state_feats = tensor_to_vec_f32(state_t, "stage0 convnext fallback readback")?;
            }
            for (block_idx, block) in stage.convnext_blocks.iter().enumerate() {
                let row_count = state_coords.len();
                if row_count == 0 {
                    break;
                }
                let residual = state_feats.clone();
                let mut h = sparse_subm_conv_forward(
                    state_coords.as_slice(),
                    state_feats.as_slice(),
                    &block.conv,
                    format!("stage0 block {block_idx} conv(stage0)").as_str(),
                    &mut conv_cache,
                    #[cfg(feature = "runtime-model-wgpu")]
                    wgpu_context.as_deref_mut(),
                )?;
                if self.compute_fp16 {
                    quantize_f16_inplace(h.as_mut_slice());
                }
                layer_norm_inplace(
                    h.as_mut_slice(),
                    row_count,
                    stage_channels,
                    Some(block.norm_weight.as_slice()),
                    Some(block.norm_bias.as_slice()),
                    LAYER_NORM32_EPS,
                )?;
                if self.compute_fp16 {
                    quantize_f16_inplace(h.as_mut_slice());
                }
                h = linear_forward(
                    h.as_slice(),
                    row_count,
                    &block.mlp_0,
                    format!("stage0 block {block_idx} mlp_0(stage0)").as_str(),
                )?;
                if self.compute_fp16 {
                    quantize_f16_inplace(h.as_mut_slice());
                }
                silu_inplace(h.as_mut_slice());
                if self.compute_fp16 {
                    quantize_f16_inplace(h.as_mut_slice());
                }
                h = linear_forward(
                    h.as_slice(),
                    row_count,
                    &block.mlp_2,
                    format!("stage0 block {block_idx} mlp_2(stage0)").as_str(),
                )?;
                if self.compute_fp16 {
                    quantize_f16_inplace(h.as_mut_slice());
                }
                add_inplace(h.as_mut_slice(), residual.as_slice());
                if self.compute_fp16 {
                    quantize_f16_inplace(h.as_mut_slice());
                }
                state_feats = h;
            }
        }

        let mut subdiv_logits = {
            #[cfg(feature = "runtime-model-wgpu")]
            {
                if let Some(state_t) = state_feats_wgpu.take() {
                    if decoder_wgpu_device_math_enabled()
                        && (!self.compute_fp16 || decoder_wgpu_device_math_allow_fp16())
                    {
                        if let Some(context_gpu) = wgpu_context.as_deref_mut() {
                            let logits_t = linear_forward_wgpu(
                                context_gpu,
                                state_t,
                                to_subdiv,
                                "stage0 to_subdiv(wgpu_math)",
                            )?;
                            tensor_to_vec_f32(logits_t, "stage0 to_subdiv(wgpu_math)")?
                        } else {
                            let host = tensor_to_vec_f32(state_t, "stage0 to_subdiv readback")?;
                            linear_forward(
                                host.as_slice(),
                                state_coords.len(),
                                to_subdiv,
                                "stage0 to_subdiv",
                            )?
                        }
                    } else {
                        let host = tensor_to_vec_f32(state_t, "stage0 to_subdiv readback")?;
                        linear_forward(
                            host.as_slice(),
                            state_coords.len(),
                            to_subdiv,
                            "stage0 to_subdiv",
                        )?
                    }
                } else {
                    linear_forward(
                        state_feats.as_slice(),
                        state_coords.len(),
                        to_subdiv,
                        "stage0 to_subdiv",
                    )?
                }
            }
            #[cfg(not(feature = "runtime-model-wgpu"))]
            {
                linear_forward(
                    state_feats.as_slice(),
                    state_coords.len(),
                    to_subdiv,
                    "stage0 to_subdiv",
                )?
            }
        };
        if self.compute_fp16 {
            quantize_f16_inplace(subdiv_logits.as_mut_slice());
        }
        if should_center_subdivision_logits() {
            row_center_logits(subdiv_logits.as_mut_slice(), state_coords.len());
        }

        #[cfg(feature = "runtime-model-wgpu")]
        if decoder_wgpu_clear_cache_after_decode()
            && let Some(context) = wgpu_context.as_deref_mut()
        {
            context.clear_caches();
        }

        Ok(SparseSubdivisionLogits {
            spatial_shape: spatial_shape_from_coords(state_coords.as_slice()),
            coords: state_coords,
            logits: subdiv_logits,
        })
    }
}

fn load_linear(
    safetensors: &SafeTensors<'_>,
    weight_key: &str,
    bias_key: &str,
    expected_in: usize,
    expected_out: usize,
) -> Result<LinearLayer, String> {
    let (w_shape, w_data) = load_tensor_f32(safetensors, weight_key)?;
    if w_shape.len() != 2 {
        return Err(format!(
            "tensor '{weight_key}' expected rank=2, got rank={}",
            w_shape.len()
        ));
    }
    let out_channels = w_shape[0];
    let in_channels = w_shape[1];
    if expected_in > 0 && in_channels != expected_in {
        return Err(format!(
            "tensor '{weight_key}' expected in_channels={expected_in}, got {in_channels}"
        ));
    }
    if expected_out > 0 && out_channels != expected_out {
        return Err(format!(
            "tensor '{weight_key}' expected out_channels={expected_out}, got {out_channels}"
        ));
    }

    let (b_shape, bias) = load_tensor_f32(safetensors, bias_key)?;
    if b_shape.len() != 1 || b_shape[0] != out_channels {
        return Err(format!(
            "tensor '{bias_key}' expected shape=[{out_channels}], got {:?}",
            b_shape
        ));
    }

    let weight = w_data;

    Ok(LinearLayer {
        in_channels,
        out_channels,
        weight,
        bias,
    })
}

fn load_linear_dynamic(
    safetensors: &SafeTensors<'_>,
    weight_key: &str,
    bias_key: &str,
    expected_in: usize,
) -> Result<LinearLayer, String> {
    let (w_shape, w_data) = load_tensor_f32(safetensors, weight_key)?;
    if w_shape.len() != 2 {
        return Err(format!(
            "tensor '{weight_key}' expected rank=2, got rank={}",
            w_shape.len()
        ));
    }
    let out_channels = w_shape[0];
    let in_channels = w_shape[1];
    if expected_in > 0 && in_channels != expected_in {
        return Err(format!(
            "tensor '{weight_key}' expected in_channels={expected_in}, got {in_channels}"
        ));
    }

    let (b_shape, bias) = load_tensor_f32(safetensors, bias_key)?;
    if b_shape.len() != 1 || b_shape[0] != out_channels {
        return Err(format!(
            "tensor '{bias_key}' expected shape=[{out_channels}], got {:?}",
            b_shape
        ));
    }

    let weight = w_data;

    Ok(LinearLayer {
        in_channels,
        out_channels,
        weight,
        bias,
    })
}

fn load_sparse_conv(
    safetensors: &SafeTensors<'_>,
    weight_key: &str,
    bias_key: &str,
    expected_in: usize,
    expected_out: usize,
) -> Result<SparseConvLayer, String> {
    let (w_shape, weight) = load_tensor_f32(safetensors, weight_key)?;
    if w_shape.len() != 5 {
        return Err(format!(
            "tensor '{weight_key}' expected rank=5, got rank={}",
            w_shape.len()
        ));
    }
    let out_channels = w_shape[0];
    let kd = w_shape[1];
    let kh = w_shape[2];
    let kw = w_shape[3];
    let in_channels_per_group = w_shape[4];
    if kd == 0 || kh == 0 || kw == 0 {
        return Err(format!(
            "tensor '{weight_key}' has invalid kernel dims ({kd},{kh},{kw})"
        ));
    }
    if in_channels_per_group == 0 {
        return Err(format!(
            "tensor '{weight_key}' has invalid in_channels_per_group=0"
        ));
    }
    if expected_out > 0 && out_channels != expected_out {
        return Err(format!(
            "tensor '{weight_key}' expected out_channels={expected_out}, got {out_channels}"
        ));
    }
    let in_channels = if expected_in > 0 {
        expected_in
    } else {
        in_channels_per_group
    };
    if in_channels < in_channels_per_group || !in_channels.is_multiple_of(in_channels_per_group) {
        return Err(format!(
            "tensor '{weight_key}' expected_in={in_channels} is incompatible with in_per_group={in_channels_per_group}"
        ));
    }
    let groups = in_channels / in_channels_per_group;
    if groups == 0 || !out_channels.is_multiple_of(groups) {
        return Err(format!(
            "tensor '{weight_key}' has incompatible grouped channels (groups={groups}, out_channels={out_channels})"
        ));
    }
    let out_channels_per_group = out_channels / groups;

    let (b_shape, bias) = load_tensor_f32(safetensors, bias_key)?;
    if b_shape.len() != 1 || b_shape[0] != out_channels {
        return Err(format!(
            "tensor '{bias_key}' expected shape=[{out_channels}], got {:?}",
            b_shape
        ));
    }

    let expected_weight_len = out_channels
        .checked_mul(kd)
        .and_then(|value| value.checked_mul(kh))
        .and_then(|value| value.checked_mul(kw))
        .and_then(|value| value.checked_mul(in_channels_per_group))
        .ok_or_else(|| format!("tensor '{weight_key}' weight shape product overflow"))?;
    if weight.len() != expected_weight_len {
        return Err(format!(
            "tensor '{weight_key}' element count mismatch: expected {expected_weight_len}, got {}",
            weight.len()
        ));
    }

    let flex_pack_config = FlexConvConfig {
        in_channels,
        out_channels,
        kernel_d: kd,
        kernel_h: kh,
        kernel_w: kw,
        in_channels_per_group,
        out_channels_per_group,
        groups,
        axis_order: [0, 1, 2],
        axis_sign: [1, 1, 1],
    };
    let flex_packed_weight = Some(pack_flex_weight(&flex_pack_config, weight.as_slice())?);

    Ok(SparseConvLayer {
        in_channels,
        out_channels,
        kernel_d: kd,
        kernel_h: kh,
        kernel_w: kw,
        in_channels_per_group,
        out_channels_per_group,
        groups,
        weight,
        bias,
        flex_packed_weight,
    })
}

fn load_vector(
    safetensors: &SafeTensors<'_>,
    key: &str,
    expected_len: usize,
) -> Result<Vec<f32>, String> {
    let (shape, data) = load_tensor_f32(safetensors, key)?;
    if shape.len() != 1 {
        return Err(format!(
            "tensor '{key}' expected rank=1, got rank={}",
            shape.len()
        ));
    }
    if expected_len > 0 && shape[0] != expected_len {
        return Err(format!(
            "tensor '{key}' expected len={expected_len}, got len={}",
            shape[0]
        ));
    }
    Ok(data)
}

fn load_tensor_f32(
    safetensors: &SafeTensors<'_>,
    key: &str,
) -> Result<(Vec<usize>, Vec<f32>), String> {
    let view = safetensors
        .tensor(key)
        .map_err(|err| format!("missing tensor '{key}' in safetensors: {err}"))?;
    let shape = view.shape().to_vec();
    let data = match view.dtype() {
        Dtype::F32 => bytes_to_f32(view.data())?,
        Dtype::F16 => bytes_to_f16(view.data())?,
        Dtype::BF16 => bytes_to_bf16(view.data())?,
        other => {
            return Err(format!(
                "tensor '{key}' has unsupported dtype {other:?}; expected f32/f16/bf16"
            ));
        }
    };
    let expected = shape
        .iter()
        .try_fold(1usize, |acc, value| acc.checked_mul(*value))
        .ok_or_else(|| format!("tensor '{key}' shape product overflow: {:?}", shape))?;
    if data.len() != expected {
        return Err(format!(
            "tensor '{key}' element count mismatch: expected {expected}, got {}",
            data.len()
        ));
    }
    Ok((shape, data))
}

fn bytes_to_f32(bytes: &[u8]) -> Result<Vec<f32>, String> {
    if !bytes.len().is_multiple_of(4) {
        return Err(format!(
            "invalid f32 tensor payload byte length {}; must be divisible by 4",
            bytes.len()
        ));
    }
    let mut out = Vec::with_capacity(bytes.len() / 4);
    for chunk in bytes.chunks_exact(4) {
        out.push(f32::from_le_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]));
    }
    Ok(out)
}

fn bytes_to_f16(bytes: &[u8]) -> Result<Vec<f32>, String> {
    if !bytes.len().is_multiple_of(2) {
        return Err(format!(
            "invalid f16 tensor payload byte length {}; must be divisible by 2",
            bytes.len()
        ));
    }
    let mut out = Vec::with_capacity(bytes.len() / 2);
    for chunk in bytes.chunks_exact(2) {
        let bits = u16::from_le_bytes([chunk[0], chunk[1]]);
        out.push(f16::from_bits(bits).to_f32());
    }
    Ok(out)
}

fn bytes_to_bf16(bytes: &[u8]) -> Result<Vec<f32>, String> {
    if !bytes.len().is_multiple_of(2) {
        return Err(format!(
            "invalid bf16 tensor payload byte length {}; must be divisible by 2",
            bytes.len()
        ));
    }
    let mut out = Vec::with_capacity(bytes.len() / 2);
    for chunk in bytes.chunks_exact(2) {
        let bits = u16::from_le_bytes([chunk[0], chunk[1]]);
        out.push(bf16::from_bits(bits).to_f32());
    }
    Ok(out)
}

fn flatten_rows_32(rows: &[[f32; 32]]) -> Vec<f32> {
    let mut out = Vec::with_capacity(rows.len() * 32);
    for row in rows {
        out.extend_from_slice(row);
    }
    out
}

fn linear_forward(
    input: &[f32],
    rows: usize,
    layer: &LinearLayer,
    context: &str,
) -> Result<Vec<f32>, String> {
    if rows == 0 {
        return Ok(Vec::new());
    }
    let expected = rows
        .checked_mul(layer.in_channels)
        .ok_or_else(|| format!("{context}: input size overflow"))?;
    if input.len() != expected {
        return Err(format!(
            "{context}: invalid input len {}, expected {} (rows={} in_channels={})",
            input.len(),
            expected,
            rows,
            layer.in_channels
        ));
    }
    if layer.bias.len() != layer.out_channels {
        return Err(format!(
            "{context}: bias len {} does not match out_channels {}",
            layer.bias.len(),
            layer.out_channels
        ));
    }
    let mut output = vec![0.0f32; rows * layer.out_channels];
    for row_idx in 0..rows {
        let base = row_idx * layer.out_channels;
        output[base..base + layer.out_channels].copy_from_slice(layer.bias.as_slice());
    }
    unsafe {
        matrixmultiply::sgemm(
            rows,
            layer.in_channels,
            layer.out_channels,
            1.0,
            input.as_ptr(),
            layer.in_channels as isize,
            1,
            layer.weight.as_ptr(),
            1,
            layer.in_channels as isize,
            1.0,
            output.as_mut_ptr(),
            layer.out_channels as isize,
            1,
        );
    }
    Ok(output)
}

#[cfg(feature = "runtime-model-wgpu")]
fn linear_forward_wgpu(
    context_gpu: &mut DecoderWgpuConvContext,
    input: Tensor<DefaultWgpuBackend, 2>,
    layer: &LinearLayer,
    context: &str,
) -> Result<Tensor<DefaultWgpuBackend, 2>, String> {
    let [rows, in_channels] = input.dims();
    if in_channels != layer.in_channels {
        return Err(format!(
            "{context}: invalid input channels {}, expected {}",
            in_channels, layer.in_channels
        ));
    }
    if layer.bias.len() != layer.out_channels {
        return Err(format!(
            "{context}: bias len {} does not match out_channels {}",
            layer.bias.len(),
            layer.out_channels
        ));
    }
    if rows == 0 {
        return Ok(Tensor::<DefaultWgpuBackend, 2>::zeros(
            [0, layer.out_channels],
            &context_gpu.device,
        ));
    }
    let weight_t = context_gpu.linear_weight_tensor(layer).swap_dims(0, 1);
    let bias_t = context_gpu
        .linear_bias_tensor(layer)
        .reshape([1, layer.out_channels]);
    Ok(input.matmul(weight_t).add(bias_t))
}

#[cfg(feature = "runtime-model-wgpu")]
fn layer_norm_wgpu(
    context_gpu: &mut DecoderWgpuConvContext,
    input: Tensor<DefaultWgpuBackend, 2>,
    rows: usize,
    channels: usize,
    weight: Option<&[f32]>,
    bias: Option<&[f32]>,
    eps: f32,
) -> Result<Tensor<DefaultWgpuBackend, 2>, String> {
    if rows == 0 || channels == 0 {
        return Ok(input);
    }
    let [input_rows, input_channels] = input.dims();
    if input_rows != rows || input_channels != channels {
        return Err(format!(
            "layer_norm_wgpu: invalid input dims [{},{}], expected [{rows},{channels}]",
            input_rows, input_channels
        ));
    }
    if let Some(weight) = weight
        && weight.len() != channels
    {
        return Err(format!(
            "layer_norm_wgpu: invalid weight len {}, expected {}",
            weight.len(),
            channels
        ));
    }
    if let Some(bias) = bias
        && bias.len() != channels
    {
        return Err(format!(
            "layer_norm_wgpu: invalid bias len {}, expected {}",
            bias.len(),
            channels
        ));
    }

    let mean = input.clone().mean_dim(1);
    let centered = input.sub(mean);
    let var = centered.clone().powf_scalar(2.0).mean_dim(1);
    let mut normalized = centered.mul(var.add_scalar(eps).sqrt().recip());
    if let Some(weight) = weight {
        let weight_t = context_gpu.vector_tensor(weight).reshape([1, channels]);
        normalized = normalized.mul(weight_t);
    }
    if let Some(bias) = bias {
        let bias_t = context_gpu.vector_tensor(bias).reshape([1, channels]);
        normalized = normalized.add(bias_t);
    }
    Ok(normalized)
}

#[cfg(feature = "runtime-model-wgpu")]
fn silu_wgpu(input: Tensor<DefaultWgpuBackend, 2>) -> Tensor<DefaultWgpuBackend, 2> {
    input.clone().mul(sigmoid(input))
}

#[cfg(feature = "runtime-model-wgpu")]
fn tensor_to_vec_f32(
    tensor: Tensor<DefaultWgpuBackend, 2>,
    context: &str,
) -> Result<Vec<f32>, String> {
    tensor
        .into_data()
        .convert::<f32>()
        .to_vec::<f32>()
        .map_err(|err| format!("{context}: failed to read wgpu tensor output: {err:?}"))
}

#[cfg(feature = "runtime-model-wgpu")]
fn convnext_blocks_forward_wgpu_tensor(
    context_gpu: &mut DecoderWgpuConvContext,
    coords: &[[u32; 4]],
    mut state_t: Tensor<DefaultWgpuBackend, 2>,
    stage_idx: usize,
    stage_channels: usize,
    blocks: &[ConvNeXtBlock],
) -> Result<Tensor<DefaultWgpuBackend, 2>, String> {
    let rows = coords.len();
    let [state_rows, state_channels] = state_t.dims();
    if state_rows != rows || state_channels != stage_channels {
        return Err(format!(
            "decoder wgpu convnext tensor dims mismatch: got=[{},{}] expected=[{},{}]",
            state_rows, state_channels, rows, stage_channels
        ));
    }
    for (block_idx, block) in blocks.iter().enumerate() {
        let residual = state_t.clone();
        let config = flex_config_for_layer(&block.conv);
        state_t = context_gpu.forward_with_coords_tensor(
            &config,
            &block.conv,
            state_t,
            format!("stage {stage_idx} block {block_idx} conv(wgpu_math)").as_str(),
            coords,
        )?;
        state_t = layer_norm_wgpu(
            context_gpu,
            state_t,
            rows,
            stage_channels,
            Some(block.norm_weight.as_slice()),
            Some(block.norm_bias.as_slice()),
            LAYER_NORM32_EPS,
        )?;
        state_t = linear_forward_wgpu(
            context_gpu,
            state_t,
            &block.mlp_0,
            format!("stage {stage_idx} block {block_idx} mlp_0(wgpu_math)").as_str(),
        )?;
        state_t = silu_wgpu(state_t);
        state_t = linear_forward_wgpu(
            context_gpu,
            state_t,
            &block.mlp_2,
            format!("stage {stage_idx} block {block_idx} mlp_2(wgpu_math)").as_str(),
        )?;
        state_t = state_t.add(residual);
    }
    Ok(state_t)
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum DecoderConvImpl {
    Legacy,
    #[cfg(not(feature = "runtime-model-wgpu"))]
    FlexGmm,
    #[cfg(feature = "runtime-model-wgpu")]
    Wgpu,
}

fn decoder_conv_impl() -> DecoderConvImpl {
    #[cfg(feature = "runtime-model-wgpu")]
    {
        DecoderConvImpl::Wgpu
    }
    #[cfg(not(feature = "runtime-model-wgpu"))]
    {
        DecoderConvImpl::FlexGmm
    }
}

fn decoder_conv_debug_enabled() -> bool {
    false
}

pub(crate) fn reset_decoder_conv_telemetry() {
    #[cfg(feature = "runtime-model-wgpu")]
    {
        if let Ok(mut state) = decoder_conv_telemetry_state().lock() {
            *state = DecoderConvTelemetryState::default();
        }
    }
}

pub(crate) fn decoder_conv_telemetry() -> DecoderConvTelemetry {
    #[cfg(feature = "runtime-model-wgpu")]
    {
        let Ok(state) = decoder_conv_telemetry_state().lock() else {
            return DecoderConvTelemetry::default();
        };
        let mut blocks = state.blocks.values().cloned().collect::<Vec<_>>();
        blocks.sort_by(|lhs, rhs| {
            rhs.dispatches
                .cmp(&lhs.dispatches)
                .then_with(|| rhs.wgpu_calls.cmp(&lhs.wgpu_calls))
                .then_with(|| rhs.conv_calls.cmp(&lhs.conv_calls))
                .then_with(|| lhs.context.cmp(&rhs.context))
        });
        DecoderConvTelemetry {
            conv_calls: state.total.conv_calls,
            wgpu_calls: state.total.wgpu_calls,
            wgpu_successes: state.total.wgpu_successes,
            wgpu_failures: state.total.wgpu_failures,
            dispatches: state.total.dispatches,
            chunked_calls: state.total.chunked_calls,
            max_chunk_rows: state.total.max_chunk_rows,
            input_bytes: state.total.input_bytes,
            output_bytes: state.total.output_bytes,
            neighbor_elements: state.total.neighbor_elements,
            blocks,
        }
    }
    #[cfg(not(feature = "runtime-model-wgpu"))]
    {
        DecoderConvTelemetry::default()
    }
}

#[cfg(feature = "runtime-model-wgpu")]
fn decoder_conv_telemetry_state() -> &'static Mutex<DecoderConvTelemetryState> {
    DECODER_CONV_TELEMETRY.get_or_init(|| Mutex::new(DecoderConvTelemetryState::default()))
}

#[cfg(feature = "runtime-model-wgpu")]
fn telemetry_update<F>(context: &str, mut update: F)
where
    F: FnMut(&mut DecoderConvBlockTelemetry),
{
    let Ok(mut state) = decoder_conv_telemetry_state().lock() else {
        return;
    };
    update(&mut state.total);
    let block =
        state
            .blocks
            .entry(context.to_string())
            .or_insert_with(|| DecoderConvBlockTelemetry {
                context: context.to_string(),
                ..DecoderConvBlockTelemetry::default()
            });
    update(block);
}

#[cfg(feature = "runtime-model-wgpu")]
fn telemetry_record_conv_call(context: &str) {
    telemetry_update(context, |stats| {
        stats.conv_calls += 1;
    });
}

#[cfg(feature = "runtime-model-wgpu")]
fn telemetry_record_wgpu_call(context: &str) {
    telemetry_update(context, |stats| {
        stats.wgpu_calls += 1;
    });
}

#[cfg(feature = "runtime-model-wgpu")]
fn telemetry_record_wgpu_failure(context: &str) {
    telemetry_update(context, |stats| {
        stats.wgpu_failures += 1;
    });
}

#[cfg(feature = "runtime-model-wgpu")]
fn telemetry_record_wgpu_success(
    context: &str,
    dispatches: u64,
    chunked: bool,
    max_chunk_rows: usize,
    input_bytes: usize,
    output_bytes: usize,
    neighbor_elements: usize,
) {
    telemetry_update(context, |stats| {
        stats.wgpu_successes += 1;
        stats.dispatches += dispatches;
        if chunked {
            stats.chunked_calls += 1;
        }
        stats.max_chunk_rows = stats.max_chunk_rows.max(max_chunk_rows);
        stats.input_bytes = stats.input_bytes.saturating_add(input_bytes as u64);
        stats.output_bytes = stats.output_bytes.saturating_add(output_bytes as u64);
        stats.neighbor_elements = stats
            .neighbor_elements
            .saturating_add(neighbor_elements as u64);
    });
}

#[cfg(feature = "runtime-model-wgpu")]
fn decoder_wgpu_neighbor_from_coords() -> bool {
    true
}

#[cfg(feature = "runtime-model-wgpu")]
fn decoder_wgpu_clear_cache_after_decode() -> bool {
    // Persist decoder tensors by default to avoid per-inference cache rebuild stalls.
    false
}

#[cfg(feature = "runtime-model-wgpu")]
fn decoder_wgpu_tensor_cache_max() -> usize {
    DECODER_WGPU_TENSOR_CACHE_MAX
}

#[cfg(feature = "runtime-model-wgpu")]
fn decoder_wgpu_use_tensor_cache() -> bool {
    if decoder_wgpu_clear_cache_after_decode() {
        return false;
    }
    decoder_wgpu_tensor_cache_max() > 0
}

#[cfg(feature = "runtime-model-wgpu")]
fn decoder_wgpu_device_math_enabled() -> bool {
    if decoder_conv_impl() != DecoderConvImpl::Wgpu {
        return false;
    }
    true
}

#[cfg(feature = "runtime-model-wgpu")]
fn decoder_wgpu_device_math_allow_fp16() -> bool {
    true
}

fn flex_config_for_layer(layer: &SparseConvLayer) -> FlexConvConfig {
    FlexConvConfig {
        in_channels: layer.in_channels,
        out_channels: layer.out_channels,
        kernel_d: layer.kernel_d,
        kernel_h: layer.kernel_h,
        kernel_w: layer.kernel_w,
        in_channels_per_group: layer.in_channels_per_group,
        out_channels_per_group: layer.out_channels_per_group,
        groups: layer.groups,
        axis_order: conv_kernel_axis_order(),
        axis_sign: conv_kernel_axis_signs(),
    }
}

fn sparse_subm_conv_forward(
    coords: &[[u32; 4]],
    input: &[f32],
    layer: &SparseConvLayer,
    context: &str,
    conv_cache: &mut DecoderConvCache,
    #[cfg(feature = "runtime-model-wgpu")] wgpu_context: Option<&mut DecoderWgpuConvContext>,
) -> Result<Vec<f32>, String> {
    #[cfg(feature = "runtime-model-wgpu")]
    telemetry_record_conv_call(context);

    let config = flex_config_for_layer(layer);
    let weights = SparseSubmConvWeights {
        weight: layer.weight.as_slice(),
        bias: layer.bias.as_slice(),
    };
    let conv_impl = decoder_conv_impl();

    #[cfg(feature = "runtime-model-wgpu")]
    if conv_impl == DecoderConvImpl::Wgpu
        && let Some(context_gpu) = wgpu_context
    {
        telemetry_record_wgpu_call(context);
        let wgpu_result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            if decoder_wgpu_neighbor_from_coords() {
                context_gpu.forward_with_coords(&config, layer, input, context, coords)
            } else {
                let (neighbor_key, neighbor_rows) =
                    conv_cache.neighbor_rows_with_key(&config, coords)?;
                context_gpu.forward_with_neighbor_rows(
                    &config,
                    layer,
                    input,
                    context,
                    neighbor_key,
                    neighbor_rows,
                )
            }
        }));
        match wgpu_result {
            Ok(Ok(output)) => return Ok(output),
            Ok(Err(err)) => {
                telemetry_record_wgpu_failure(context);
                if err.contains("BufferTooBig") {
                    context_gpu.wgpu_failed = true;
                    if decoder_conv_debug_enabled() {
                        eprintln!(
                            "burn_trellis: wgpu conv disabling after buffer-too-big in '{context}': {err}"
                        );
                    }
                } else if decoder_conv_debug_enabled() {
                    eprintln!(
                        "burn_trellis: wgpu conv fallback to flex/legacy in '{context}': {err}"
                    );
                }
            }
            Err(payload) => {
                telemetry_record_wgpu_failure(context);
                context_gpu.wgpu_failed = true;
                if decoder_conv_debug_enabled() {
                    let panic_message = panic_payload_to_string(payload);
                    eprintln!(
                        "burn_trellis: wgpu conv panicked in '{context}', fallback to flex/legacy: {panic_message}"
                    );
                }
            }
        }
    }

    if conv_impl != DecoderConvImpl::Legacy {
        let (_neighbor_key, neighbor_rows) = conv_cache.neighbor_rows_with_key(&config, coords)?;
        match sparse_subm_conv_forward_flex_precomputed(
            &config,
            weights,
            input,
            neighbor_rows,
            layer.flex_packed_weight.as_deref(),
        ) {
            Ok(output) => return Ok(output),
            Err(err) => {
                if decoder_conv_debug_enabled() {
                    eprintln!("burn_trellis: flex conv fallback to legacy in '{context}': {err}");
                }
            }
        }
    }

    sparse_subm_conv_forward_legacy(coords, input, layer, context)
}

fn sparse_subm_conv_forward_legacy(
    coords: &[[u32; 4]],
    input: &[f32],
    layer: &SparseConvLayer,
    context: &str,
) -> Result<Vec<f32>, String> {
    let rows = coords.len();
    if rows == 0 {
        return Ok(Vec::new());
    }
    let expected = rows
        .checked_mul(layer.in_channels)
        .ok_or_else(|| format!("{context}: input size overflow"))?;
    if input.len() != expected {
        return Err(format!(
            "{context}: invalid input len {}, expected {} (rows={} in_channels={})",
            input.len(),
            expected,
            rows,
            layer.in_channels
        ));
    }
    if layer.bias.len() != layer.out_channels {
        return Err(format!(
            "{context}: bias len {} does not match out_channels {}",
            layer.bias.len(),
            layer.out_channels
        ));
    }

    let mut output = vec![0.0f32; rows * layer.out_channels];
    for row_idx in 0..rows {
        let base = row_idx * layer.out_channels;
        output[base..base + layer.out_channels].copy_from_slice(layer.bias.as_slice());
    }

    let mut coord_to_row = HashMap::with_capacity(rows.saturating_mul(2));
    for (row_idx, coord) in coords.iter().copied().enumerate() {
        coord_to_row.insert(coord, row_idx);
    }

    let center_d = (layer.kernel_d / 2) as i32;
    let center_h = (layer.kernel_h / 2) as i32;
    let center_w = (layer.kernel_w / 2) as i32;
    let axis_order = conv_kernel_axis_order();
    let axis_sign = conv_kernel_axis_signs();
    for (out_row_idx, out_coord) in coords.iter().copied().enumerate().take(rows) {
        let batch = out_coord[0];
        let ox = out_coord[1] as i32;
        let oy = out_coord[2] as i32;
        let oz = out_coord[3] as i32;
        let out_base = out_row_idx * layer.out_channels;

        for kd_idx in 0..layer.kernel_d {
            for kh_idx in 0..layer.kernel_h {
                for kw_idx in 0..layer.kernel_w {
                    let deltas = [
                        axis_sign[0] * (kd_idx as i32 - center_d),
                        axis_sign[1] * (kh_idx as i32 - center_h),
                        axis_sign[2] * (kw_idx as i32 - center_w),
                    ];
                    let mut spatial = [ox, oy, oz];
                    spatial[axis_order[0]] += deltas[0];
                    spatial[axis_order[1]] += deltas[1];
                    spatial[axis_order[2]] += deltas[2];
                    if spatial[0] < 0 || spatial[1] < 0 || spatial[2] < 0 {
                        continue;
                    }
                    let neighbor = [
                        batch,
                        spatial[0] as u32,
                        spatial[1] as u32,
                        spatial[2] as u32,
                    ];
                    let Some(in_row_idx) = coord_to_row.get(&neighbor).copied() else {
                        continue;
                    };
                    let in_row = &input
                        [in_row_idx * layer.in_channels..(in_row_idx + 1) * layer.in_channels];
                    for group_idx in 0..layer.groups {
                        let in_group_base = group_idx * layer.in_channels_per_group;
                        let out_group_base = group_idx * layer.out_channels_per_group;
                        for out_local in 0..layer.out_channels_per_group {
                            let out_idx = out_group_base + out_local;
                            let weight_base =
                                (((out_idx * layer.kernel_d + kd_idx) * layer.kernel_h + kh_idx)
                                    * layer.kernel_w
                                    + kw_idx)
                                    * layer.in_channels_per_group;
                            let mut accum = 0.0f32;
                            for in_local in 0..layer.in_channels_per_group {
                                accum += in_row[in_group_base + in_local]
                                    * layer.weight[weight_base + in_local];
                            }
                            output[out_base + out_idx] += accum;
                        }
                    }
                }
            }
        }
    }

    Ok(output)
}

fn conv_kernel_axis_order() -> [usize; 3] {
    [0, 1, 2]
}

fn conv_kernel_axis_signs() -> [i32; 3] {
    [1, 1, 1]
}

fn layer_norm_inplace(
    data: &mut [f32],
    rows: usize,
    channels: usize,
    weight: Option<&[f32]>,
    bias: Option<&[f32]>,
    eps: f32,
) -> Result<(), String> {
    if rows == 0 || channels == 0 {
        return Ok(());
    }
    if data.len() != rows * channels {
        return Err(format!(
            "layer_norm_inplace: invalid data len {}, expected {}",
            data.len(),
            rows * channels
        ));
    }
    if let Some(weight) = weight
        && weight.len() != channels
    {
        return Err(format!(
            "layer_norm_inplace: invalid weight len {}, expected {}",
            weight.len(),
            channels
        ));
    }
    if let Some(bias) = bias
        && bias.len() != channels
    {
        return Err(format!(
            "layer_norm_inplace: invalid bias len {}, expected {}",
            bias.len(),
            channels
        ));
    }

    for row_idx in 0..rows {
        let base = row_idx * channels;
        let row = &mut data[base..base + channels];
        let mean = row.iter().copied().sum::<f32>() / channels as f32;
        let var = row
            .iter()
            .map(|value| {
                let centered = *value - mean;
                centered * centered
            })
            .sum::<f32>()
            / channels as f32;
        let inv_std = 1.0 / (var + eps).sqrt();
        for ch in 0..channels {
            let mut value = (row[ch] - mean) * inv_std;
            if let Some(weight) = weight {
                value *= weight[ch];
            }
            if let Some(bias) = bias {
                value += bias[ch];
            }
            row[ch] = value;
        }
    }
    Ok(())
}

fn silu_inplace(data: &mut [f32]) {
    for value in data {
        *value = *value / (1.0 + (-*value).exp());
    }
}

fn quantize_f16_inplace(data: &mut [f32]) {
    for value in data {
        *value = f16::from_f32(*value).to_f32();
    }
}

fn row_center_logits(data: &mut [f32], rows: usize) {
    if rows == 0 {
        return;
    }
    if data.len() != rows * 8 {
        return;
    }
    for row_idx in 0..rows {
        let row = &mut data[row_idx * 8..(row_idx + 1) * 8];
        let mean = row.iter().copied().sum::<f32>() / 8.0;
        for value in row {
            *value -= mean;
        }
    }
}

fn should_center_subdivision_logits() -> bool {
    false
}

fn decoder_force_fp32() -> bool {
    false
}

fn add_inplace(lhs: &mut [f32], rhs: &[f32]) {
    if lhs.len() != rhs.len() {
        return;
    }
    for (left, right) in lhs.iter_mut().zip(rhs.iter()) {
        *left += *right;
    }
}

fn logits_to_mask(
    logits: &[f32],
    rows: usize,
    enforce_non_empty: bool,
) -> Result<Vec<[bool; 8]>, String> {
    if logits.len() != rows * 8 {
        return Err(format!(
            "subdivision logits len {} does not match rows*8={}",
            logits.len(),
            rows * 8
        ));
    }
    let mut out = Vec::with_capacity(rows);
    let max_children = decoder_max_children_per_parent();
    for row_idx in 0..rows {
        let mut mask = [false; 8];
        let row = &logits[row_idx * 8..(row_idx + 1) * 8];
        for child in 0..8 {
            mask[child] = row[child] > 0.0;
        }
        if let Some(max_children) = max_children {
            let selected = mask.iter().filter(|flag| **flag).count();
            if selected > max_children {
                let mut order = (0..8usize).collect::<Vec<_>>();
                order.sort_by(|a, b| {
                    row[*b]
                        .partial_cmp(&row[*a])
                        .unwrap_or(std::cmp::Ordering::Equal)
                });
                let mut limited = [false; 8];
                for idx in order.into_iter().take(max_children) {
                    limited[idx] = true;
                }
                mask = limited;
            }
        }
        if enforce_non_empty && !mask.iter().any(|flag| *flag) {
            let mut best_idx = 0usize;
            let mut best_val = row[0];
            for (idx, value) in row.iter().enumerate().skip(1) {
                if *value > best_val {
                    best_val = *value;
                    best_idx = idx;
                }
            }
            mask[best_idx] = true;
        }
        out.push(mask);
    }
    Ok(out)
}

fn decoder_max_children_per_parent() -> Option<usize> {
    // Default to uncapped subdivision for decoder parity.
    None
}

#[cfg(feature = "runtime-model-wgpu")]
fn panic_payload_to_string(payload: Box<dyn std::any::Any + Send>) -> String {
    if let Some(msg) = payload.downcast_ref::<&str>() {
        (*msg).to_string()
    } else if let Some(msg) = payload.downcast_ref::<String>() {
        msg.clone()
    } else {
        "non-string panic payload".to_string()
    }
}

#[cfg(feature = "runtime-model-wgpu")]
fn decoder_wgpu_max_output_bytes() -> usize {
    256 * 1024 * 1024
}

#[cfg(feature = "runtime-model-wgpu")]
fn decoder_wgpu_max_input_bytes() -> usize {
    1024 * 1024 * 1024
}

#[cfg(feature = "runtime-model-wgpu")]
fn decoder_wgpu_chunk_rows(rows: usize, bytes_per_row: usize, max_output_bytes: usize) -> usize {
    if rows == 0 {
        return 1;
    }
    if bytes_per_row == 0 {
        return rows;
    }
    let by_bytes = (max_output_bytes / bytes_per_row).max(1).min(rows);
    let aligned = by_bytes - (by_bytes % 64);
    if aligned > 0 { aligned } else { by_bytes }
}

#[cfg(feature = "runtime-model-wgpu")]
fn decoder_wgpu_hotspot_min_output_bytes() -> usize {
    384 * 1024 * 1024
}

#[cfg(feature = "runtime-model-wgpu")]
fn decoder_wgpu_hotspot_fused_enabled() -> bool {
    true
}

#[cfg(feature = "runtime-model-wgpu")]
fn decoder_wgpu_forward_config_for_call(
    config: &FlexConvConfig,
    rows: usize,
    output_bytes: usize,
    max_output_bytes: usize,
) -> SparseWgpuForwardConfig {
    if !decoder_wgpu_hotspot_fused_enabled() {
        return SparseWgpuForwardConfig::default();
    }

    let hotspot = output_bytes >= decoder_wgpu_hotspot_min_output_bytes()
        || output_bytes > max_output_bytes
        || rows >= 131_072;
    if hotspot && config.out_channels_per_group >= 4 && config.out_channels >= 4 {
        SparseWgpuForwardConfig {
            kernel_variant: SparseWgpuKernelVariant::FusedOc4,
            split_k: Some(1),
        }
    } else {
        SparseWgpuForwardConfig::default()
    }
}

fn channel2spatial(
    coords: &[[u32; 4]],
    feats: &[f32],
    in_channels: usize,
    subdivision_mask: &[[bool; 8]],
) -> Result<(Vec<[u32; 4]>, Vec<f32>), String> {
    let rows = coords.len();
    if rows == 0 {
        return Ok((Vec::new(), Vec::new()));
    }
    if feats.len() != rows * in_channels {
        return Err(format!(
            "channel2spatial: invalid feats len {}, expected {}",
            feats.len(),
            rows * in_channels
        ));
    }
    if !in_channels.is_multiple_of(8) {
        return Err(format!(
            "channel2spatial: in_channels={} is not divisible by 8",
            in_channels
        ));
    }
    if subdivision_mask.len() != rows {
        return Err(format!(
            "channel2spatial: subdivision rows {} do not match coords rows {}",
            subdivision_mask.len(),
            rows
        ));
    }

    let out_channels = in_channels / 8;
    let mut out_coords = Vec::new();
    let mut out_feats = Vec::new();
    for row_idx in 0..rows {
        let coord = coords[row_idx];
        let row_feats = &feats[row_idx * in_channels..(row_idx + 1) * in_channels];
        for (child, selected) in subdivision_mask[row_idx].iter().enumerate().take(8usize) {
            if !*selected {
                continue;
            }
            let cx = (child & 1) as u32;
            let cy = ((child >> 1) & 1) as u32;
            let cz = ((child >> 2) & 1) as u32;
            out_coords.push([
                coord[0],
                coord[1].saturating_mul(2).saturating_add(cx),
                coord[2].saturating_mul(2).saturating_add(cy),
                coord[3].saturating_mul(2).saturating_add(cz),
            ]);
            let child_base = child * out_channels;
            out_feats.extend_from_slice(&row_feats[child_base..child_base + out_channels]);
        }
    }
    Ok((out_coords, out_feats))
}

fn repeat_interleave_channels(
    feats: &[f32],
    rows: usize,
    in_channels: usize,
    repeat_factor: usize,
) -> Vec<f32> {
    if rows == 0 || in_channels == 0 || repeat_factor == 0 {
        return Vec::new();
    }
    let out_channels = in_channels * repeat_factor;
    let mut out = Vec::with_capacity(rows * out_channels);
    for row_idx in 0..rows {
        let row = &feats[row_idx * in_channels..(row_idx + 1) * in_channels];
        for value in row {
            for _ in 0..repeat_factor {
                out.push(*value);
            }
        }
    }
    out
}

fn map_guide_subdivision_logits(
    coords: &[[u32; 4]],
    guide: &SparseSubdivisionLogits,
) -> Result<Vec<f32>, String> {
    if guide.logits.len() != guide.coords.len() * 8 {
        return Err(format!(
            "guide subdivision logits invalid length: logits={} coords={}",
            guide.logits.len(),
            guide.coords.len()
        ));
    }
    let mut map = HashMap::with_capacity(guide.coords.len() * 2);
    for (idx, coord) in guide.coords.iter().enumerate() {
        let row = &guide.logits[idx * 8..(idx + 1) * 8];
        map.insert(*coord, row.to_vec());
    }

    let mut out = Vec::with_capacity(coords.len() * 8);
    let strict = false;
    for coord in coords {
        if let Some(row) = map.get(coord) {
            out.extend_from_slice(row);
        } else if strict {
            return Err(format!(
                "guide subdivision logits missing coord {:?} in parity strict mode",
                coord
            ));
        } else {
            // If the guide row is missing, keep all children disabled.
            out.extend_from_slice(&[-1.0; 8]);
        }
    }
    Ok(out)
}

fn spatial_shape_from_coords(coords: &[[u32; 4]]) -> [u32; 3] {
    if coords.is_empty() {
        return [1, 1, 1];
    }
    let mut max_x = 0u32;
    let mut max_y = 0u32;
    let mut max_z = 0u32;
    for coord in coords {
        max_x = max_x.max(coord[1]);
        max_y = max_y.max(coord[2]);
        max_z = max_z.max(coord[3]);
    }
    [
        max_x.saturating_add(1),
        max_y.saturating_add(1),
        max_z.saturating_add(1),
    ]
}

fn load_weight_backing(path: &Path) -> Result<WeightsBacking, String> {
    if path
        .extension()
        .and_then(|ext| ext.to_str())
        .is_some_and(|ext| ext.eq_ignore_ascii_case("bpk"))
    {
        let bytes = load_burnpack_blob_bytes(path)?;
        return Ok(WeightsBacking::Bytes(bytes));
    }

    let file = File::open(path).map_err(|err| {
        format!(
            "failed to open sparse decoder weights '{}': {err}",
            path.display()
        )
    })?;
    let mmap = unsafe { MmapOptions::new().map(&file) }.map_err(|err| {
        format!(
            "failed to mmap sparse decoder weights '{}': {err}",
            path.display()
        )
    })?;
    Ok(WeightsBacking::Mmap(mmap))
}

fn load_burnpack_blob_bytes(path: &Path) -> Result<Vec<u8>, String> {
    let metadata_path = metadata_path(path);
    let metadata: BlobMetadata =
        serde_json::from_slice(&std::fs::read(&metadata_path).map_err(|err| {
            format!(
                "failed to read burnpack metadata '{}': {err}",
                metadata_path.display()
            )
        })?)
        .map_err(|err| {
            format!(
                "failed to parse burnpack metadata '{}': {err}",
                metadata_path.display()
            )
        })?;

    match load_blob_bytes_with_backend::<burn::backend::NdArray<f32, u8>>(path, metadata.bytes_len)
    {
        Ok(bytes) => Ok(bytes),
        Err(u8_err) => load_blob_bytes_with_backend::<burn::backend::NdArray<f32, i64>>(
            path,
            metadata.bytes_len,
        )
        .map_err(|i64_err| {
            format!(
                "failed to load blob burnpack '{}' (u8 backend: {u8_err}; i64 fallback: {i64_err})",
                path.display()
            )
        }),
    }
}

fn load_blob_bytes_with_backend<B: Backend>(
    path: &Path,
    bytes_len: usize,
) -> Result<Vec<u8>, String>
where
    B::Device: Default,
{
    let device = <B as Backend>::Device::default();
    let zeros = Tensor::<B, 1, Int>::zeros([bytes_len], &device);
    let mut blob = BinaryBlob {
        bytes: Param::initialized(ParamId::new(), zeros),
    };

    let mut store = BurnpackStore::from_file(path).validate(true);
    blob.load_from(&mut store)
        .map_err(|err| format!("failed to load burnpack '{}': {err}", path.display()))?;

    let bytes = blob
        .bytes
        .val()
        .into_data()
        .convert::<u8>()
        .to_vec::<u8>()
        .map_err(|err| format!("failed to materialize burnpack bytes: {err:?}"))?;

    if bytes.len() != bytes_len {
        return Err(format!(
            "burnpack byte length mismatch for '{}': expected {}, got {}",
            path.display(),
            bytes_len,
            bytes.len()
        ));
    }
    Ok(bytes)
}

fn metadata_path(path: &Path) -> PathBuf {
    let file_name = path
        .file_name()
        .and_then(|value| value.to_str())
        .unwrap_or("model.bpk");
    path.with_file_name(format!("{file_name}.meta.json"))
}

fn resolve_model_weight_candidates(
    model_stem: &str,
    weights_root: &Path,
    image_large_root: Option<&Path>,
) -> Vec<PathBuf> {
    let source =
        resolve_model_source_path(model_stem, "safetensors", weights_root, image_large_root);
    let burnpack = source.with_extension("bpk");
    let burnpack_f16 = with_file_stem_suffix(&burnpack, F16_SUFFIX);
    let source_f16 = with_file_stem_suffix(&source, F16_SUFFIX);
    let prefer_f16 = prefer_f16_burnpack();
    let candidates = if prefer_f16 {
        vec![burnpack_f16, burnpack, source_f16, source]
    } else {
        vec![burnpack, burnpack_f16, source, source_f16]
    };
    candidates
        .into_iter()
        .filter(|path| path.exists())
        .collect::<Vec<_>>()
}

fn prefer_f16_burnpack() -> bool {
    true
}

fn resolve_model_source_path(
    stem: &str,
    ext: &str,
    weights_root: &Path,
    image_large_root: Option<&Path>,
) -> PathBuf {
    if stem.starts_with("ckpts/") {
        return weights_root.join(format!("{stem}.{ext}"));
    }
    if let Some((_, suffix)) = stem.split_once("/ckpts/") {
        let image_large_root = image_large_root.unwrap_or(weights_root);
        return image_large_root.join(format!("ckpts/{suffix}.{ext}"));
    }
    weights_root.join(format!("{stem}.{ext}"))
}

fn with_file_stem_suffix(path: &Path, suffix: &str) -> PathBuf {
    let Some(stem) = path.file_stem() else {
        return path.to_path_buf();
    };
    let stem = stem.to_string_lossy();
    if stem.ends_with(suffix) {
        return path.to_path_buf();
    }
    let ext = path
        .extension()
        .and_then(|value| value.to_str())
        .unwrap_or("");
    let mut file_name = format!("{stem}{suffix}");
    if !ext.is_empty() {
        file_name.push('.');
        file_name.push_str(ext);
    }
    path.with_file_name(file_name)
}

#[cfg(test)]
mod tests {
    use std::fs;
    use std::sync::{Mutex, MutexGuard};
    use std::time::Instant;
    use std::time::{SystemTime, UNIX_EPOCH};

    #[cfg(feature = "runtime-model-wgpu")]
    use super::decoder_wgpu_neighbor_from_coords;
    use super::{
        DecoderConvCache, DecoderConvImpl, LinearLayer, SparseConvLayer, decoder_conv_impl,
        linear_forward, logits_to_mask, resolve_model_weight_candidates, sparse_subm_conv_forward,
        sparse_subm_conv_forward_legacy,
    };
    #[cfg(feature = "runtime-model-wgpu")]
    use super::{
        decoder_wgpu_clear_cache_after_decode, decoder_wgpu_device_math_allow_fp16,
        decoder_wgpu_device_math_enabled, decoder_wgpu_tensor_cache_max,
        decoder_wgpu_use_tensor_cache,
    };

    static ENV_LOCK: Mutex<()> = Mutex::new(());

    fn env_lock_guard() -> MutexGuard<'static, ()> {
        ENV_LOCK
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
    }

    fn make_unit_conv_3x1x1(weight: [f32; 3]) -> SparseConvLayer {
        SparseConvLayer {
            in_channels: 1,
            out_channels: 1,
            kernel_d: 3,
            kernel_h: 1,
            kernel_w: 1,
            in_channels_per_group: 1,
            out_channels_per_group: 1,
            groups: 1,
            weight: weight.to_vec(),
            bias: vec![0.0],
            flex_packed_weight: None,
        }
    }

    #[derive(Clone)]
    struct Lcg {
        state: u64,
    }

    impl Lcg {
        fn new(seed: u64) -> Self {
            Self { state: seed | 1 }
        }

        fn next_f32(&mut self) -> f32 {
            self.state = self.state.wrapping_mul(6364136223846793005).wrapping_add(1);
            let bits = ((self.state >> 40) as u32) | 1;
            (bits as f32 / u32::MAX as f32) * 2.0 - 1.0
        }
    }

    #[test]
    fn sparse_conv_uses_neighbor_voxels() {
        let _guard = env_lock_guard();
        unsafe {
            std::env::remove_var("TRELLIS2_PARITY_STRICT");
            std::env::set_var("TRELLIS2_CONV_AXIS_ORDER", "xyz");
            std::env::set_var("TRELLIS2_CONV_AXIS_SIGN", "+++");
        }
        let coords = vec![[0, 0, 0, 0], [0, 1, 0, 0]];
        let input = vec![1.0f32, 2.0f32];
        // kernel offsets: [-1, 0, +1]
        let layer = make_unit_conv_3x1x1([10.0, 1.0, 100.0]);

        let output = sparse_subm_conv_forward(
            coords.as_slice(),
            input.as_slice(),
            &layer,
            "test conv",
            &mut DecoderConvCache::default(),
            #[cfg(feature = "runtime-model-wgpu")]
            None,
        )
        .expect("sparse conv should succeed");
        assert_eq!(output.len(), 2);
        // x=0: center(1*1) + right-neighbor(2*100)
        assert!((output[0] - 201.0).abs() < 1.0e-5);
        // x=1: left-neighbor(1*10) + center(2*1)
        assert!((output[1] - 12.0).abs() < 1.0e-5);
        unsafe {
            std::env::remove_var("TRELLIS2_CONV_AXIS_ORDER");
            std::env::remove_var("TRELLIS2_CONV_AXIS_SIGN");
        }
    }

    #[test]
    fn sparse_conv_flex_matches_legacy_path() {
        let _guard = env_lock_guard();
        unsafe {
            std::env::remove_var("TRELLIS2_PARITY_STRICT");
            std::env::set_var("TRELLIS2_CONV_AXIS_ORDER", "xyz");
            std::env::set_var("TRELLIS2_CONV_AXIS_SIGN", "+++");
            std::env::set_var("TRELLIS2_DECODER_CONV_IMPL", "flex_gmm");
        }
        let mut rng = Lcg::new(123);
        let layer = SparseConvLayer {
            in_channels: 4,
            out_channels: 6,
            kernel_d: 3,
            kernel_h: 1,
            kernel_w: 1,
            in_channels_per_group: 2,
            out_channels_per_group: 3,
            groups: 2,
            weight: (0..(6 * 3 * 2)).map(|_| rng.next_f32()).collect(),
            bias: (0..6).map(|_| rng.next_f32()).collect(),
            flex_packed_weight: None,
        };
        let coords: Vec<[u32; 4]> = (0..32u32).map(|x| [0, x, 0, 0]).collect();
        let input: Vec<f32> = (0..coords.len() * layer.in_channels)
            .map(|_| rng.next_f32())
            .collect();
        let legacy =
            sparse_subm_conv_forward_legacy(coords.as_slice(), input.as_slice(), &layer, "legacy")
                .expect("legacy conv");
        let fused = sparse_subm_conv_forward(
            coords.as_slice(),
            input.as_slice(),
            &layer,
            "fused",
            &mut DecoderConvCache::default(),
            #[cfg(feature = "runtime-model-wgpu")]
            None,
        )
        .expect("fused conv");
        assert_eq!(legacy.len(), fused.len());
        for (idx, (lhs, rhs)) in legacy.iter().zip(fused.iter()).enumerate() {
            let diff = (lhs - rhs).abs();
            assert!(
                diff <= 1.0e-5,
                "mismatch idx={idx}: legacy={lhs} fused={rhs} diff={diff}"
            );
        }
        unsafe {
            std::env::remove_var("TRELLIS2_DECODER_CONV_IMPL");
            std::env::remove_var("TRELLIS2_CONV_AXIS_ORDER");
            std::env::remove_var("TRELLIS2_CONV_AXIS_SIGN");
        }
    }

    #[test]
    fn decoder_neighbor_cache_reuses_across_coord_allocations() {
        let _guard = env_lock_guard();
        unsafe {
            std::env::remove_var("TRELLIS2_CONV_AXIS_ORDER");
            std::env::remove_var("TRELLIS2_CONV_AXIS_SIGN");
        }
        let layer = make_unit_conv_3x1x1([0.1, 0.2, 0.3]);
        let config = super::flex_config_for_layer(&layer);
        let coords_a: Vec<[u32; 4]> = (0..16u32).map(|x| [0, x, 0, 0]).collect();
        let coords_b = coords_a.clone();
        let mut cache = DecoderConvCache::default();

        let key_a = {
            let (key, rows) = cache
                .neighbor_rows_with_key(&config, coords_a.as_slice())
                .expect("cache build");
            assert_eq!(rows.len(), coords_a.len() * 3);
            key
        };
        let len_after_a = cache.neighbor_rows.len();
        let key_b = {
            let (key, rows) = cache
                .neighbor_rows_with_key(&config, coords_b.as_slice())
                .expect("cache hit");
            assert_eq!(rows.len(), coords_b.len() * 3);
            key
        };

        assert_eq!(key_a, key_b);
        assert_eq!(cache.neighbor_rows.len(), len_after_a);
    }

    #[test]
    fn decoder_neighbor_cache_reuse_reduces_repeated_conv_time() {
        let _guard = env_lock_guard();
        unsafe {
            std::env::set_var("TRELLIS2_DECODER_CONV_IMPL", "flex_gmm");
            std::env::set_var("TRELLIS2_CONV_AXIS_ORDER", "xyz");
            std::env::set_var("TRELLIS2_CONV_AXIS_SIGN", "+++");
        }
        let mut rng = Lcg::new(991);
        let layer = SparseConvLayer {
            in_channels: 64,
            out_channels: 128,
            kernel_d: 3,
            kernel_h: 3,
            kernel_w: 3,
            in_channels_per_group: 64,
            out_channels_per_group: 128,
            groups: 1,
            weight: (0..(128 * 3 * 3 * 3 * 64))
                .map(|_| rng.next_f32())
                .collect(),
            bias: (0..128).map(|_| rng.next_f32()).collect(),
            flex_packed_weight: None,
        };
        let coords: Vec<[u32; 4]> = (0..4096u32).map(|x| [0, x, 0, 0]).collect();
        let input: Vec<f32> = (0..coords.len() * layer.in_channels)
            .map(|_| rng.next_f32())
            .collect();
        let iterations = 12usize;

        let cold_start = Instant::now();
        for _ in 0..iterations {
            let _ = sparse_subm_conv_forward(
                coords.as_slice(),
                input.as_slice(),
                &layer,
                "cold",
                &mut DecoderConvCache::default(),
                #[cfg(feature = "runtime-model-wgpu")]
                None,
            )
            .expect("cold conv");
        }
        let cold = cold_start.elapsed();

        let mut warm_cache = DecoderConvCache::default();
        let warm_start = Instant::now();
        for _ in 0..iterations {
            let _ = sparse_subm_conv_forward(
                coords.as_slice(),
                input.as_slice(),
                &layer,
                "warm",
                &mut warm_cache,
                #[cfg(feature = "runtime-model-wgpu")]
                None,
            )
            .expect("warm conv");
        }
        let warm = warm_start.elapsed();
        eprintln!(
            "decoder cache perf: cold={:?} warm={:?} ratio={:.3}",
            cold,
            warm,
            warm.as_secs_f64() / cold.as_secs_f64().max(1.0e-12)
        );
        assert!(
            warm <= cold,
            "expected persistent neighbor cache to be no slower than rebuilding; cold={cold:?} warm={warm:?}"
        );

        unsafe {
            std::env::remove_var("TRELLIS2_DECODER_CONV_IMPL");
            std::env::remove_var("TRELLIS2_CONV_AXIS_ORDER");
            std::env::remove_var("TRELLIS2_CONV_AXIS_SIGN");
        }
    }

    #[test]
    fn decoder_default_child_cap_is_uncapped_without_strict_mode() {
        let _guard = env_lock_guard();
        unsafe {
            std::env::remove_var("TRELLIS2_PARITY_STRICT");
            std::env::remove_var("TRELLIS2_DECODER_UNCAPPED");
            std::env::remove_var("TRELLIS2_DECODER_MAX_CHILDREN_PER_PARENT");
        }
        let logits = vec![1.0f32; 8];
        let mask = logits_to_mask(logits.as_slice(), 1, true).expect("mask");
        let selected = mask[0].iter().filter(|flag| **flag).count();
        assert_eq!(selected, 8);
    }

    #[test]
    fn parity_strict_defaults_to_uncapped_children() {
        let _guard = env_lock_guard();
        unsafe {
            std::env::set_var("TRELLIS2_PARITY_STRICT", "1");
            std::env::remove_var("TRELLIS2_DECODER_UNCAPPED");
            std::env::remove_var("TRELLIS2_DECODER_MAX_CHILDREN_PER_PARENT");
        }
        let logits = vec![1.0f32; 8];
        let mask = logits_to_mask(logits.as_slice(), 1, true).expect("mask");
        let selected = mask[0].iter().filter(|flag| **flag).count();
        assert_eq!(selected, 8);
        unsafe {
            std::env::remove_var("TRELLIS2_PARITY_STRICT");
        }
    }

    #[test]
    fn explicit_zero_child_cap_env_means_uncapped() {
        let _guard = env_lock_guard();
        unsafe {
            std::env::remove_var("TRELLIS2_PARITY_STRICT");
            std::env::remove_var("TRELLIS2_DECODER_UNCAPPED");
            std::env::set_var("TRELLIS2_DECODER_MAX_CHILDREN_PER_PARENT", "0");
        }
        let logits = vec![1.0f32; 8];
        let mask = logits_to_mask(logits.as_slice(), 1, true).expect("mask");
        let selected = mask[0].iter().filter(|flag| **flag).count();
        assert_eq!(selected, 8);
        unsafe {
            std::env::remove_var("TRELLIS2_DECODER_MAX_CHILDREN_PER_PARENT");
        }
    }

    #[test]
    fn decoder_conv_auto_defaults_to_flex() {
        let _guard = env_lock_guard();
        unsafe {
            std::env::remove_var("TRELLIS2_DECODER_CONV_IMPL");
            std::env::remove_var("TRELLIS2_PARITY_STRICT");
            std::env::remove_var("TRELLIS2_E2E_STRICT");
            std::env::remove_var("TRELLIS2_DECODER_DISABLE_WGPU");
        }
        #[cfg(feature = "runtime-model-wgpu")]
        assert_eq!(decoder_conv_impl(), DecoderConvImpl::Wgpu);
        #[cfg(not(feature = "runtime-model-wgpu"))]
        assert_eq!(decoder_conv_impl(), DecoderConvImpl::FlexGmm);
    }

    #[test]
    fn decoder_conv_auto_does_not_force_legacy_in_strict_mode() {
        let _guard = env_lock_guard();
        unsafe {
            std::env::remove_var("TRELLIS2_DECODER_CONV_IMPL");
            std::env::set_var("TRELLIS2_E2E_STRICT", "1");
        }
        #[cfg(feature = "runtime-model-wgpu")]
        assert_eq!(decoder_conv_impl(), DecoderConvImpl::Wgpu);
        #[cfg(not(feature = "runtime-model-wgpu"))]
        assert_eq!(decoder_conv_impl(), DecoderConvImpl::FlexGmm);
        unsafe {
            std::env::remove_var("TRELLIS2_E2E_STRICT");
        }
    }

    #[cfg(feature = "runtime-model-wgpu")]
    #[test]
    fn decoder_wgpu_neighbor_source_defaults_to_coords() {
        let _guard = env_lock_guard();
        assert!(decoder_wgpu_neighbor_from_coords());
    }

    #[cfg(feature = "runtime-model-wgpu")]
    #[test]
    fn decoder_wgpu_cache_controls_have_expected_defaults() {
        let _guard = env_lock_guard();
        assert!(!decoder_wgpu_clear_cache_after_decode());
        assert_eq!(decoder_wgpu_tensor_cache_max(), 64);
        assert!(decoder_wgpu_use_tensor_cache());
        // Runtime behavior is canonical and should not drift based on environment toggles.
        unsafe {
            std::env::set_var("TRELLIS2_DECODER_WGPU_CLEAR_CACHE_AFTER_DECODE", "1");
            std::env::set_var("TRELLIS2_DECODER_WGPU_TENSOR_CACHE_MAX", "8");
        }
        assert!(!decoder_wgpu_clear_cache_after_decode());
        assert_eq!(decoder_wgpu_tensor_cache_max(), 64);
        assert!(decoder_wgpu_use_tensor_cache());
        unsafe {
            std::env::remove_var("TRELLIS2_DECODER_WGPU_CLEAR_CACHE_AFTER_DECODE");
            std::env::remove_var("TRELLIS2_DECODER_WGPU_TENSOR_CACHE_MAX");
        }
    }

    #[cfg(feature = "runtime-model-wgpu")]
    #[test]
    fn decoder_wgpu_device_math_control_defaults_enabled() {
        let _guard = env_lock_guard();
        unsafe {
            std::env::remove_var("TRELLIS2_DECODER_WGPU_DEVICE_MATH");
            std::env::remove_var("TRELLIS2_DECODER_WGPU_DEVICE_MATH_FP16");
            std::env::set_var("TRELLIS2_DECODER_CONV_IMPL", "wgpu");
            std::env::remove_var("TRELLIS2_DECODER_DISABLE_WGPU");
        }
        assert!(decoder_wgpu_device_math_enabled());
        assert!(decoder_wgpu_device_math_allow_fp16());
        unsafe {
            std::env::set_var("TRELLIS2_DECODER_WGPU_DEVICE_MATH", "0");
        }
        assert!(decoder_wgpu_device_math_enabled());
        unsafe {
            std::env::set_var("TRELLIS2_DECODER_WGPU_DEVICE_MATH", "1");
            std::env::set_var("TRELLIS2_DECODER_WGPU_DEVICE_MATH_FP16", "0");
        }
        assert!(decoder_wgpu_device_math_allow_fp16());
        unsafe {
            std::env::set_var("TRELLIS2_DECODER_WGPU_DEVICE_MATH_FP16", "1");
            std::env::set_var("TRELLIS2_DECODER_CONV_IMPL", "legacy");
        }
        assert!(decoder_wgpu_device_math_enabled());
        unsafe {
            std::env::remove_var("TRELLIS2_DECODER_WGPU_DEVICE_MATH");
            std::env::remove_var("TRELLIS2_DECODER_WGPU_DEVICE_MATH_FP16");
            std::env::remove_var("TRELLIS2_DECODER_CONV_IMPL");
        }
    }

    #[test]
    fn linear_forward_matches_naive_matmul() {
        let layer = LinearLayer {
            in_channels: 3,
            out_channels: 2,
            // [out, in]
            weight: vec![
                1.0, 2.0, 3.0, // out0
                -1.0, 0.5, 4.0, // out1
            ],
            bias: vec![0.25, -0.5],
        };
        let input = vec![
            2.0, -1.0, 0.5, // row0
            -3.0, 4.0, 1.0, // row1
        ];
        let output = linear_forward(input.as_slice(), 2, &layer, "test linear")
            .expect("linear forward should succeed");
        assert_eq!(output.len(), 4);

        let mut expected = Vec::new();
        for row in 0..2 {
            let x = &input[row * 3..(row + 1) * 3];
            // out0
            expected.push(layer.bias[0] + x[0] * 1.0 + x[1] * 2.0 + x[2] * 3.0);
            // out1
            expected.push(layer.bias[1] - x[0] + x[1] * 0.5 + x[2] * 4.0);
        }
        for (got, want) in output.iter().zip(expected.iter()) {
            assert!((got - want).abs() < 1.0e-5, "got={got} want={want}");
        }
    }

    #[test]
    fn model_weight_candidates_prefer_bpk_variants() {
        let _guard = env_lock_guard();
        unsafe {
            std::env::remove_var("TRELLIS2_BPK_PRECISION");
            std::env::remove_var("BURN_SYNTH_BPK_PRECISION");
        }

        let unique = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("clock drift")
            .as_nanos();
        let root = std::env::temp_dir().join(format!("burn_trellis_decoder_candidates_{unique}"));
        let ckpts = root.join("ckpts");
        fs::create_dir_all(&ckpts).expect("create ckpts");
        fs::write(ckpts.join("shape.safetensors"), b"safe").expect("write safetensors");
        fs::write(ckpts.join("shape.bpk"), b"bpk").expect("write bpk");
        fs::write(ckpts.join("shape_f16.bpk"), b"bpk_f16").expect("write f16 bpk");

        let candidates = resolve_model_weight_candidates("ckpts/shape", root.as_path(), None);
        assert!(!candidates.is_empty(), "expected weight candidates");
        assert_eq!(candidates[0], ckpts.join("shape_f16.bpk"));

        let _ = fs::remove_dir_all(root);
    }

    #[test]
    fn model_weight_candidates_ignore_env_precision_overrides() {
        let _guard = env_lock_guard();
        unsafe {
            std::env::set_var("TRELLIS2_BPK_PRECISION", "f32");
            std::env::remove_var("BURN_SYNTH_BPK_PRECISION");
        }

        let unique = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("clock drift")
            .as_nanos();
        let root = std::env::temp_dir().join(format!("burn_trellis_decoder_candidates_{unique}"));
        let ckpts = root.join("ckpts");
        fs::create_dir_all(&ckpts).expect("create ckpts");
        fs::write(ckpts.join("shape.safetensors"), b"safe").expect("write safetensors");
        fs::write(ckpts.join("shape.bpk"), b"bpk").expect("write bpk");
        fs::write(ckpts.join("shape_f16.bpk"), b"bpk_f16").expect("write f16 bpk");

        let candidates = resolve_model_weight_candidates("ckpts/shape", root.as_path(), None);
        assert!(!candidates.is_empty(), "expected weight candidates");
        assert_eq!(candidates[0], ckpts.join("shape_f16.bpk"));

        unsafe {
            std::env::remove_var("TRELLIS2_BPK_PRECISION");
        }
        let _ = fs::remove_dir_all(root);
    }
}