amari-gpu 0.24.1

GPU acceleration for mathematical computations
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
//! GPU-accelerated holographic memory operations
//!
//! This module provides GPU acceleration for Vector Symbolic Architecture (VSA)
//! operations using WebGPU/wgpu compute shaders.
//!
//! The 0.20 public v1 surface is intentionally explicit about its validated
//! scope: `GpuHolographic` operates on the canonical 256-dimensional
//! `ProductCl3x32` representation, uses GPU kernels for batch binding and
//! similarity when batch sizes cross the GPU heuristic, and keeps unbinding and
//! bundling on the CPU correctness path. `GpuOpticalField` provides GPU-backed
//! rotor binding, similarity reduction, and Lee encoding for finite optical
//! fields with matching dimensions.
//!
//! # Supported Operations
//!
//! - **Batch Binding**: GPU-accelerated geometric product for many key-value pairs
//! - **Batch Unbinding**: Parallel retrieval operations
//! - **Batch Similarity**: Efficient similarity computation across large codebooks
//! - **Batch Bundling**: Parallel superposition of multiple representations
//!
//! # Performance
//!
//! GPU acceleration is beneficial for:
//! - Batch sizes > 100 items
//! - Large codebook operations (resonator cleanup)
//! - Memory operations with many stored items
//!
//! For small operations, CPU fallback is used automatically.

use amari_holographic::{AlgebraConfig, BindingAlgebra, HolographicMemory, ProductCliffordAlgebra};
use thiserror::Error;
use wgpu::util::DeviceExt;

/// Type alias for commonly used ProductClifford algebra
pub type ProductCl3x32 = ProductCliffordAlgebra<32>;

const PRODUCT_CL3X32_DIMENSION: usize = ProductCl3x32::DIMENSION;

/// Errors specific to GPU holographic operations
#[derive(Error, Debug)]
pub enum GpuHolographicError {
    #[error("GPU initialization failed: {0}")]
    InitializationError(String),

    #[error("GPU buffer error: {0}")]
    BufferError(String),

    #[error("Shader compilation error: {0}")]
    ShaderError(String),

    #[error("Dimension mismatch: expected {expected}, got {actual}")]
    DimensionMismatch { expected: usize, actual: usize },

    #[error("Holographic algebra error: {0}")]
    AlgebraError(String),
}

/// Result type for GPU holographic operations
pub type GpuHolographicResult<T> = Result<T, GpuHolographicError>;

/// GPU-accelerated holographic operations
///
/// Provides batch operations for VSA using WebGPU compute shaders.
/// Automatically falls back to CPU for small batch sizes.
pub struct GpuHolographic {
    device: wgpu::Device,
    queue: wgpu::Queue,
    bind_pipeline: wgpu::ComputePipeline,
    similarity_pipeline: wgpu::ComputePipeline,
    bundle_pipeline: wgpu::ComputePipeline,
    dimension: usize,
}

impl GpuHolographic {
    /// Initialize GPU context for holographic operations
    ///
    /// # Arguments
    /// * `dimension` - The dimension of the algebra. The validated v1 GPU path supports
    ///   only 256-dimensional `ProductCl3x32` vectors.
    pub async fn new(dimension: usize) -> GpuHolographicResult<Self> {
        if dimension != PRODUCT_CL3X32_DIMENSION {
            return Err(GpuHolographicError::DimensionMismatch {
                expected: PRODUCT_CL3X32_DIMENSION,
                actual: dimension,
            });
        }

        let instance = wgpu::Instance::default();

        let adapter = instance
            .request_adapter(&wgpu::RequestAdapterOptions {
                power_preference: wgpu::PowerPreference::HighPerformance,
                compatible_surface: None,
                force_fallback_adapter: false,
            })
            .await
            .ok_or_else(|| {
                GpuHolographicError::InitializationError("No GPU adapter found".to_string())
            })?;

        let (device, queue) = adapter
            .request_device(
                &wgpu::DeviceDescriptor {
                    label: Some("Amari Holographic GPU Device"),
                    required_features: wgpu::Features::empty(),
                    required_limits: wgpu::Limits::default(),
                },
                None,
            )
            .await
            .map_err(|e| GpuHolographicError::InitializationError(e.to_string()))?;

        // Create compute pipelines
        let bind_pipeline = Self::create_bind_pipeline(&device, dimension)?;
        let similarity_pipeline = Self::create_similarity_pipeline(&device, dimension)?;
        let bundle_pipeline = Self::create_bundle_pipeline(&device, dimension)?;

        Ok(Self {
            device,
            queue,
            bind_pipeline,
            similarity_pipeline,
            bundle_pipeline,
            dimension,
        })
    }

    /// Create GPU context for ProductCl3x32 (256-dimensional)
    pub async fn new_product_cl3x32() -> GpuHolographicResult<Self> {
        Self::new(256).await
    }

    /// Batch binding operation (geometric product for holographic binding)
    ///
    /// Computes `result[i] = keys[i] ⊛ values[i]` for all i.
    ///
    /// # Arguments
    /// * `keys` - Flat array of key coefficients (batch_size * dimension)
    /// * `values` - Flat array of value coefficients (batch_size * dimension)
    ///
    /// # Returns
    /// Flat array of bound pair coefficients
    pub async fn batch_bind(&self, keys: &[f64], values: &[f64]) -> GpuHolographicResult<Vec<f64>> {
        let batch_size = self.validate_flat_pair_inputs(keys, values)?;

        // For small batches, use CPU
        if batch_size < 100 {
            return self.batch_bind_cpu(keys, values);
        }

        self.batch_bind_gpu(keys, values).await
    }

    /// Batch unbinding operation.
    ///
    /// Computes `result[i] = keys[i]⁻¹ ⊛ bounds[i]` for all i. This path remains
    /// CPU-backed for correctness because the GPU inverse kernel is not part of
    /// the validated v1 surface.
    pub async fn batch_unbind(
        &self,
        keys: &[f64],
        bounds: &[f64],
    ) -> GpuHolographicResult<Vec<f64>> {
        let batch_size = self.validate_flat_pair_inputs(keys, bounds)?;

        // For small batches, use CPU
        if batch_size < 100 {
            return self.batch_unbind_cpu(keys, bounds);
        }

        // GPU unbinding requires computing inverse first
        // For now, use CPU for correctness
        self.batch_unbind_cpu(keys, bounds)
    }

    /// Batch similarity computation
    ///
    /// Computes cosine similarity between all pairs.
    ///
    /// # Arguments
    /// * `a_batch` - First set of vectors (batch_size * dimension)
    /// * `b_batch` - Second set of vectors (batch_size * dimension)
    ///
    /// # Returns
    /// Array of similarity values (batch_size)
    pub async fn batch_similarity(
        &self,
        a_batch: &[f64],
        b_batch: &[f64],
    ) -> GpuHolographicResult<Vec<f64>> {
        let batch_size = self.validate_flat_pair_inputs(a_batch, b_batch)?;

        // For small batches, use CPU
        if batch_size < 100 {
            return self.batch_similarity_cpu(a_batch, b_batch);
        }

        self.batch_similarity_gpu(a_batch, b_batch).await
    }

    /// Batch bundling operation.
    ///
    /// Computes `ProductCl3x32::bundle(..., beta = 1.0)` for each pair. This is
    /// intentionally CPU-backed in v1 so it matches `amari-holographic` soft
    /// bundling semantics instead of the older experimental element-wise shader.
    pub async fn batch_bundle(
        &self,
        a_batch: &[f64],
        b_batch: &[f64],
    ) -> GpuHolographicResult<Vec<f64>> {
        let _batch_size = self.validate_flat_pair_inputs(a_batch, b_batch)?;

        self.batch_bundle_cpu(a_batch, b_batch)
    }

    /// Find most similar item in codebook (for resonator cleanup)
    ///
    /// # Arguments
    /// * `query` - Query vector coefficients (dimension)
    /// * `codebook` - Codebook vectors (codebook_size * dimension)
    ///
    /// # Returns
    /// (best_index, best_similarity)
    pub async fn find_most_similar(
        &self,
        query: &[f64],
        codebook: &[f64],
    ) -> GpuHolographicResult<(usize, f64)> {
        if query.len() != self.dimension {
            return Err(GpuHolographicError::DimensionMismatch {
                expected: self.dimension,
                actual: query.len(),
            });
        }

        self.validate_flat_input(query)?;
        self.validate_flat_input(codebook)?;
        let codebook_size = codebook.len() / self.dimension;
        if codebook_size == 0 {
            return Err(GpuHolographicError::DimensionMismatch {
                expected: self.dimension,
                actual: 0,
            });
        }

        // Compute similarities with all codebook items
        let queries = query.repeat(codebook_size);
        let similarities = self.batch_similarity(&queries, codebook).await?;

        // Find maximum
        let (best_idx, best_sim) = similarities
            .iter()
            .enumerate()
            .max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
            .map(|(i, s)| (i, *s))
            .unwrap_or((0, 0.0));

        Ok((best_idx, best_sim))
    }

    /// Heuristic to determine if GPU should be used
    pub fn should_use_gpu(batch_size: usize) -> bool {
        batch_size >= 100
    }

    fn validate_flat_pair_inputs(&self, a: &[f64], b: &[f64]) -> GpuHolographicResult<usize> {
        if a.len() != b.len() {
            return Err(GpuHolographicError::DimensionMismatch {
                expected: a.len(),
                actual: b.len(),
            });
        }
        self.validate_flat_input(a)
    }

    fn validate_flat_input(&self, input: &[f64]) -> GpuHolographicResult<usize> {
        if !input.len().is_multiple_of(self.dimension) {
            return Err(GpuHolographicError::DimensionMismatch {
                expected: input.len().next_multiple_of(self.dimension),
                actual: input.len(),
            });
        }
        if let Some((index, _)) = input
            .iter()
            .enumerate()
            .find(|(_, value)| !value.is_finite())
        {
            return Err(GpuHolographicError::BufferError(format!(
                "holographic coefficient {index} is not finite"
            )));
        }
        Ok(input.len() / self.dimension)
    }

    // ========================================================================
    // CPU fallback implementations
    // ========================================================================

    fn batch_bind_cpu(&self, keys: &[f64], values: &[f64]) -> GpuHolographicResult<Vec<f64>> {
        let batch_size = keys.len() / self.dimension;
        let mut results = Vec::with_capacity(keys.len());

        for i in 0..batch_size {
            let start = i * self.dimension;
            let end = start + self.dimension;

            let key = ProductCl3x32::from_coefficients(&keys[start..end])
                .map_err(|e| GpuHolographicError::AlgebraError(format!("{}", e)))?;
            let value = ProductCl3x32::from_coefficients(&values[start..end])
                .map_err(|e| GpuHolographicError::AlgebraError(format!("{}", e)))?;

            let bound = key.bind(&value);
            results.extend(bound.to_coefficients());
        }

        Ok(results)
    }

    fn batch_unbind_cpu(&self, keys: &[f64], bounds: &[f64]) -> GpuHolographicResult<Vec<f64>> {
        let batch_size = keys.len() / self.dimension;
        let mut results = Vec::with_capacity(keys.len());

        for i in 0..batch_size {
            let start = i * self.dimension;
            let end = start + self.dimension;

            let key = ProductCl3x32::from_coefficients(&keys[start..end])
                .map_err(|e| GpuHolographicError::AlgebraError(format!("{}", e)))?;
            let bound = ProductCl3x32::from_coefficients(&bounds[start..end])
                .map_err(|e| GpuHolographicError::AlgebraError(format!("{}", e)))?;

            let unbound = key
                .unbind(&bound)
                .map_err(|e| GpuHolographicError::AlgebraError(format!("{}", e)))?;
            results.extend(unbound.to_coefficients());
        }

        Ok(results)
    }

    fn batch_similarity_cpu(&self, a: &[f64], b: &[f64]) -> GpuHolographicResult<Vec<f64>> {
        let batch_size = a.len() / self.dimension;
        let mut results = Vec::with_capacity(batch_size);

        for i in 0..batch_size {
            let start = i * self.dimension;
            let end = start + self.dimension;

            let a_vec = ProductCl3x32::from_coefficients(&a[start..end])
                .map_err(|e| GpuHolographicError::AlgebraError(format!("{}", e)))?;
            let b_vec = ProductCl3x32::from_coefficients(&b[start..end])
                .map_err(|e| GpuHolographicError::AlgebraError(format!("{}", e)))?;

            results.push(a_vec.similarity(&b_vec));
        }

        Ok(results)
    }

    fn batch_bundle_cpu(&self, a: &[f64], b: &[f64]) -> GpuHolographicResult<Vec<f64>> {
        let batch_size = a.len() / self.dimension;
        let mut results = Vec::with_capacity(a.len());

        for i in 0..batch_size {
            let start = i * self.dimension;
            let end = start + self.dimension;

            let a_vec = ProductCl3x32::from_coefficients(&a[start..end])
                .map_err(|e| GpuHolographicError::AlgebraError(format!("{}", e)))?;
            let b_vec = ProductCl3x32::from_coefficients(&b[start..end])
                .map_err(|e| GpuHolographicError::AlgebraError(format!("{}", e)))?;

            let bundled = a_vec
                .bundle(&b_vec, 1.0)
                .map_err(|e| GpuHolographicError::AlgebraError(format!("{}", e)))?;
            results.extend(bundled.to_coefficients());
        }

        Ok(results)
    }

    // ========================================================================
    // GPU implementations
    // ========================================================================

    async fn batch_bind_gpu(&self, keys: &[f64], values: &[f64]) -> GpuHolographicResult<Vec<f64>> {
        let batch_size = keys.len() / self.dimension;

        // Convert to f32 for GPU
        let keys_f32: Vec<f32> = keys.iter().map(|&x| x as f32).collect();
        let values_f32: Vec<f32> = values.iter().map(|&x| x as f32).collect();

        // Create GPU buffers
        let keys_buffer = self
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("Keys Buffer"),
                contents: bytemuck::cast_slice(&keys_f32),
                usage: wgpu::BufferUsages::STORAGE,
            });

        let values_buffer = self
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("Values Buffer"),
                contents: bytemuck::cast_slice(&values_f32),
                usage: wgpu::BufferUsages::STORAGE,
            });

        let output_size = (keys.len() * std::mem::size_of::<f32>()) as u64;
        let output_buffer = self.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Output Buffer"),
            size: output_size,
            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
            mapped_at_creation: false,
        });

        let staging_buffer = self.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Staging Buffer"),
            size: output_size,
            usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });

        // Create bind group
        let bind_group_layout = self.bind_pipeline.get_bind_group_layout(0);
        let bind_group = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("Bind Compute Group"),
            layout: &bind_group_layout,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: keys_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: values_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 2,
                    resource: output_buffer.as_entire_binding(),
                },
            ],
        });

        // Dispatch compute shader
        let mut encoder = self
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some("Bind Compute Encoder"),
            });

        {
            let mut compute_pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("Bind Compute Pass"),
                timestamp_writes: None,
            });

            compute_pass.set_pipeline(&self.bind_pipeline);
            compute_pass.set_bind_group(0, &bind_group, &[]);
            compute_pass.dispatch_workgroups(batch_size as u32, 1, 1);
        }

        encoder.copy_buffer_to_buffer(&output_buffer, 0, &staging_buffer, 0, output_size);

        self.queue.submit(Some(encoder.finish()));

        // Read back results
        let buffer_slice = staging_buffer.slice(..);
        let (sender, receiver) = futures::channel::oneshot::channel();
        buffer_slice.map_async(wgpu::MapMode::Read, move |result| {
            let _ = sender.send(result);
        });

        self.device.poll(wgpu::Maintain::Wait);
        receiver
            .await
            .map_err(|_| GpuHolographicError::BufferError("Channel error".to_string()))?
            .map_err(|e| GpuHolographicError::BufferError(e.to_string()))?;

        let data = buffer_slice.get_mapped_range();
        let result_f32: &[f32] = bytemuck::cast_slice(&data);
        let result: Vec<f64> = result_f32.iter().map(|&x| x as f64).collect();

        drop(data);
        staging_buffer.unmap();

        Ok(result)
    }

    async fn batch_similarity_gpu(&self, a: &[f64], b: &[f64]) -> GpuHolographicResult<Vec<f64>> {
        let batch_size = a.len() / self.dimension;

        // Convert to f32 for GPU
        let a_f32: Vec<f32> = a.iter().map(|&x| x as f32).collect();
        let b_f32: Vec<f32> = b.iter().map(|&x| x as f32).collect();

        // Create GPU buffers
        let a_buffer = self
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("A Buffer"),
                contents: bytemuck::cast_slice(&a_f32),
                usage: wgpu::BufferUsages::STORAGE,
            });

        let b_buffer = self
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("B Buffer"),
                contents: bytemuck::cast_slice(&b_f32),
                usage: wgpu::BufferUsages::STORAGE,
            });

        let output_size = (batch_size * std::mem::size_of::<f32>()) as u64;
        let output_buffer = self.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Similarity Output Buffer"),
            size: output_size,
            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
            mapped_at_creation: false,
        });

        let staging_buffer = self.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Staging Buffer"),
            size: output_size,
            usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });

        // Create bind group
        let bind_group_layout = self.similarity_pipeline.get_bind_group_layout(0);
        let bind_group = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("Similarity Compute Group"),
            layout: &bind_group_layout,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: a_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: b_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 2,
                    resource: output_buffer.as_entire_binding(),
                },
            ],
        });

        // Dispatch compute shader
        let mut encoder = self
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some("Similarity Compute Encoder"),
            });

        {
            let mut compute_pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("Similarity Compute Pass"),
                timestamp_writes: None,
            });

            compute_pass.set_pipeline(&self.similarity_pipeline);
            compute_pass.set_bind_group(0, &bind_group, &[]);
            compute_pass.dispatch_workgroups(batch_size.div_ceil(64) as u32, 1, 1);
        }

        encoder.copy_buffer_to_buffer(&output_buffer, 0, &staging_buffer, 0, output_size);

        self.queue.submit(Some(encoder.finish()));

        // Read back results
        let buffer_slice = staging_buffer.slice(..);
        let (sender, receiver) = futures::channel::oneshot::channel();
        buffer_slice.map_async(wgpu::MapMode::Read, move |result| {
            let _ = sender.send(result);
        });

        self.device.poll(wgpu::Maintain::Wait);
        receiver
            .await
            .map_err(|_| GpuHolographicError::BufferError("Channel error".to_string()))?
            .map_err(|e| GpuHolographicError::BufferError(e.to_string()))?;

        let data = buffer_slice.get_mapped_range();
        let result_f32: &[f32] = bytemuck::cast_slice(&data);
        let result: Vec<f64> = result_f32.iter().map(|&x| x as f64).collect();

        drop(data);
        staging_buffer.unmap();

        Ok(result)
    }

    #[allow(dead_code)] // retained as redesign-pending shader path; public v1 uses CPU bundling
    async fn batch_bundle_gpu(&self, a: &[f64], b: &[f64]) -> GpuHolographicResult<Vec<f64>> {
        let batch_size = a.len() / self.dimension;

        // Convert to f32 for GPU
        let a_f32: Vec<f32> = a.iter().map(|&x| x as f32).collect();
        let b_f32: Vec<f32> = b.iter().map(|&x| x as f32).collect();

        // Create GPU buffers
        let a_buffer = self
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("A Buffer"),
                contents: bytemuck::cast_slice(&a_f32),
                usage: wgpu::BufferUsages::STORAGE,
            });

        let b_buffer = self
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("B Buffer"),
                contents: bytemuck::cast_slice(&b_f32),
                usage: wgpu::BufferUsages::STORAGE,
            });

        let output_size = (a.len() * std::mem::size_of::<f32>()) as u64;
        let output_buffer = self.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Bundle Output Buffer"),
            size: output_size,
            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
            mapped_at_creation: false,
        });

        let staging_buffer = self.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Staging Buffer"),
            size: output_size,
            usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });

        // Create bind group
        let bind_group_layout = self.bundle_pipeline.get_bind_group_layout(0);
        let bind_group = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("Bundle Compute Group"),
            layout: &bind_group_layout,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: a_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: b_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 2,
                    resource: output_buffer.as_entire_binding(),
                },
            ],
        });

        // Dispatch compute shader
        let mut encoder = self
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some("Bundle Compute Encoder"),
            });

        {
            let mut compute_pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("Bundle Compute Pass"),
                timestamp_writes: None,
            });

            compute_pass.set_pipeline(&self.bundle_pipeline);
            compute_pass.set_bind_group(0, &bind_group, &[]);
            compute_pass.dispatch_workgroups(batch_size as u32, 1, 1);
        }

        encoder.copy_buffer_to_buffer(&output_buffer, 0, &staging_buffer, 0, output_size);

        self.queue.submit(Some(encoder.finish()));

        // Read back results
        let buffer_slice = staging_buffer.slice(..);
        let (sender, receiver) = futures::channel::oneshot::channel();
        buffer_slice.map_async(wgpu::MapMode::Read, move |result| {
            let _ = sender.send(result);
        });

        self.device.poll(wgpu::Maintain::Wait);
        receiver
            .await
            .map_err(|_| GpuHolographicError::BufferError("Channel error".to_string()))?
            .map_err(|e| GpuHolographicError::BufferError(e.to_string()))?;

        let data = buffer_slice.get_mapped_range();
        let result_f32: &[f32] = bytemuck::cast_slice(&data);
        let result: Vec<f64> = result_f32.iter().map(|&x| x as f64).collect();

        drop(data);
        staging_buffer.unmap();

        Ok(result)
    }

    // ========================================================================
    // Pipeline creation
    // ========================================================================

    fn create_bind_pipeline(
        device: &wgpu::Device,
        dimension: usize,
    ) -> GpuHolographicResult<wgpu::ComputePipeline> {
        let shader_source = Self::get_bind_shader(dimension);
        let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
            label: Some("Holographic Bind Shader"),
            source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Owned(shader_source)),
        });

        Ok(
            device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
                label: Some("Holographic Bind Pipeline"),
                layout: None,
                module: &shader,
                entry_point: "main",
            }),
        )
    }

    fn create_similarity_pipeline(
        device: &wgpu::Device,
        dimension: usize,
    ) -> GpuHolographicResult<wgpu::ComputePipeline> {
        let shader_source = Self::get_similarity_shader(dimension);
        let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
            label: Some("Holographic Similarity Shader"),
            source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Owned(shader_source)),
        });

        Ok(
            device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
                label: Some("Holographic Similarity Pipeline"),
                layout: None,
                module: &shader,
                entry_point: "main",
            }),
        )
    }

    fn create_bundle_pipeline(
        device: &wgpu::Device,
        dimension: usize,
    ) -> GpuHolographicResult<wgpu::ComputePipeline> {
        let shader_source = Self::get_bundle_shader(dimension);
        let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
            label: Some("Holographic Bundle Shader"),
            source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Owned(shader_source)),
        });

        Ok(
            device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
                label: Some("Holographic Bundle Pipeline"),
                layout: None,
                module: &shader,
                entry_point: "main",
            }),
        )
    }

    // ========================================================================
    // WGSL Shader generation
    // ========================================================================

    fn get_bind_shader(dimension: usize) -> String {
        format!(
            r#"
// Holographic binding shader (element-wise product for ProductClifford)
// For ProductClifford, binding is component-wise Cl3 geometric product

const DIMENSION: u32 = {dim}u;
const CL3_DIM: u32 = 8u;
const NUM_COMPONENTS: u32 = {dim}u / 8u;

@group(0) @binding(0)
var<storage, read> keys: array<f32>;

@group(0) @binding(1)
var<storage, read> values: array<f32>;

@group(0) @binding(2)
var<storage, read_write> output: array<f32>;

fn cl3_geometric_product(a0: f32, a1: f32, a2: f32, a3: f32, a4: f32, a5: f32, a6: f32, a7: f32,
                         b0: f32, b1: f32, b2: f32, b3: f32, b4: f32, b5: f32, b6: f32, b7: f32,
                         component_offset: u32) {{
    // Matches amari-holographic Cl3 coefficient order:
    // [s, e1, e2, e3, e12, e13, e23, e123].
    output[component_offset + 0u] = a0*b0 + a1*b1 + a2*b2 + a3*b3 - a4*b4 - a5*b5 - a6*b6 - a7*b7;
    output[component_offset + 1u] = a0*b1 + a1*b0 - a2*b4 - a3*b5 + a4*b2 + a5*b3 - a6*b7 - a7*b6;
    output[component_offset + 2u] = a0*b2 + a2*b0 + a1*b4 - a3*b6 - a4*b1 - a5*b7 + a6*b3 + a7*b5;
    output[component_offset + 3u] = a0*b3 + a3*b0 + a1*b5 + a2*b6 + a4*b7 - a5*b1 - a6*b2 - a7*b4;
    output[component_offset + 4u] = a0*b4 + a4*b0 + a1*b2 - a2*b1 - a3*b7 + a5*b6 - a6*b5 - a7*b3;
    output[component_offset + 5u] = a0*b5 + a5*b0 + a1*b3 - a3*b1 + a2*b7 - a4*b6 + a6*b4 + a7*b2;
    output[component_offset + 6u] = a0*b6 + a6*b0 + a2*b3 - a3*b2 - a1*b7 + a4*b5 - a5*b4 - a7*b1;
    output[component_offset + 7u] = a0*b7 + a7*b0 + a1*b6 + a2*b5 + a3*b4 + a4*b3 + a5*b2 + a6*b1;
}}

@compute @workgroup_size(1)
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {{
    let batch_idx = global_id.x;
    let offset = batch_idx * DIMENSION;

    // For each Cl3 component
    for (var comp = 0u; comp < NUM_COMPONENTS; comp = comp + 1u) {{
        let comp_offset = offset + comp * CL3_DIM;

        cl3_geometric_product(
            keys[comp_offset + 0u], keys[comp_offset + 1u], keys[comp_offset + 2u], keys[comp_offset + 3u],
            keys[comp_offset + 4u], keys[comp_offset + 5u], keys[comp_offset + 6u], keys[comp_offset + 7u],
            values[comp_offset + 0u], values[comp_offset + 1u], values[comp_offset + 2u], values[comp_offset + 3u],
            values[comp_offset + 4u], values[comp_offset + 5u], values[comp_offset + 6u], values[comp_offset + 7u],
            comp_offset
        );
    }}
}}
"#,
            dim = dimension
        )
    }

    fn get_similarity_shader(dimension: usize) -> String {
        format!(
            r#"
// Holographic similarity shader (cosine similarity)

const DIMENSION: u32 = {dim}u;

@group(0) @binding(0)
var<storage, read> a_batch: array<f32>;

@group(0) @binding(1)
var<storage, read> b_batch: array<f32>;

@group(0) @binding(2)
var<storage, read_write> output: array<f32>;

@compute @workgroup_size(64)
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {{
    let batch_idx = global_id.x;
    if (batch_idx >= arrayLength(&output)) {{
        return;
    }}
    let offset = batch_idx * DIMENSION;

    var dot_product: f32 = 0.0;
    var norm_a_sq: f32 = 0.0;
    var norm_b_sq: f32 = 0.0;

    for (var i = 0u; i < DIMENSION; i = i + 1u) {{
        let a = a_batch[offset + i];
        let b = b_batch[offset + i];

        dot_product += a * b;
        norm_a_sq += a * a;
        norm_b_sq += b * b;
    }}

    let norm_a = sqrt(norm_a_sq);
    let norm_b = sqrt(norm_b_sq);

    var similarity: f32 = 0.0;
    if (norm_a > 1e-10 && norm_b > 1e-10) {{
        similarity = dot_product / (norm_a * norm_b);
        similarity = clamp(similarity, -1.0, 1.0);
    }}

    output[batch_idx] = similarity;
}}
"#,
            dim = dimension
        )
    }

    fn get_bundle_shader(dimension: usize) -> String {
        format!(
            r#"
// Holographic bundling shader (element-wise sum)

const DIMENSION: u32 = {dim}u;

@group(0) @binding(0)
var<storage, read> a_batch: array<f32>;

@group(0) @binding(1)
var<storage, read> b_batch: array<f32>;

@group(0) @binding(2)
var<storage, read_write> output: array<f32>;

@compute @workgroup_size(1)
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {{
    let batch_idx = global_id.x;
    let offset = batch_idx * DIMENSION;

    for (var i = 0u; i < DIMENSION; i = i + 1u) {{
        output[offset + i] = a_batch[offset + i] + b_batch[offset + i];
    }}
}}
"#,
            dim = dimension
        )
    }
}

/// GPU-accelerated holographic memory
///
/// Wrapper around `HolographicMemory` that uses GPU for batch operations.
pub struct GpuHolographicMemory {
    memory: HolographicMemory<ProductCl3x32>,
    gpu: Option<GpuHolographic>,
}

impl GpuHolographicMemory {
    /// Create a new GPU-accelerated holographic memory
    pub async fn new() -> GpuHolographicResult<Self> {
        let memory = HolographicMemory::new(AlgebraConfig::default());
        let gpu = GpuHolographic::new_product_cl3x32().await.ok();

        Ok(Self { memory, gpu })
    }

    /// Store a key-value pair
    pub fn store(&mut self, key: &ProductCl3x32, value: &ProductCl3x32) {
        self.memory.store(key, value);
    }

    /// Store multiple key-value pairs.
    ///
    /// This wrapper currently delegates to `amari-holographic`'s validated CPU
    /// memory implementation. It validates equal batch lengths instead of
    /// silently dropping unpaired keys or values.
    pub async fn store_batch(
        &mut self,
        keys: &[ProductCl3x32],
        values: &[ProductCl3x32],
    ) -> GpuHolographicResult<()> {
        if keys.len() != values.len() {
            return Err(GpuHolographicError::DimensionMismatch {
                expected: keys.len(),
                actual: values.len(),
            });
        }
        let pairs: Vec<_> = keys.iter().cloned().zip(values.iter().cloned()).collect();
        self.memory.store_batch(&pairs);
        Ok(())
    }

    /// Retrieve a value by key
    pub fn retrieve(
        &self,
        key: &ProductCl3x32,
    ) -> amari_holographic::RetrievalResult<ProductCl3x32> {
        self.memory.retrieve(key)
    }

    /// Get memory statistics
    pub fn capacity_info(&self) -> amari_holographic::CapacityInfo {
        self.memory.capacity_info()
    }

    /// Clear the memory
    pub fn clear(&mut self) {
        self.memory.clear();
    }

    /// Check if GPU is available
    pub fn has_gpu(&self) -> bool {
        self.gpu.is_some()
    }
}

// ============================================================================
// GPU-accelerated Optical Field Operations
// ============================================================================

use amari_holographic::optical::{BinaryHologram, LeeEncoderConfig, OpticalRotorField};

/// GPU-accelerated optical rotor field operations.
///
/// Provides batch operations for optical fields using WebGPU compute shaders.
/// The optical field uses a SoA (Structure of Arrays) layout with separate
/// f32 arrays for scalar, bivector, and amplitude components.
///
/// # Supported Operations
///
/// - **Batch Bind**: Parallel rotor multiplication (phase addition)
/// - **Batch Similarity**: Parallel inner product computation
/// - **Batch Bundle**: Parallel weighted sum with normalization
/// - **Batch Lee Encoding**: Parallel binary hologram generation
///
/// # Performance
///
/// GPU acceleration is beneficial for:
/// - Field dimensions > 64×64 (4096 pixels)
/// - Batch operations with many fields
/// - Lee encoding of large holograms
///
/// # Example
///
/// ```ignore
/// use amari_gpu::GpuOpticalField;
/// use amari_holographic::optical::OpticalRotorField;
///
/// let gpu = GpuOpticalField::new((256, 256)).await?;
///
/// let field_a = OpticalRotorField::random((256, 256), 1);
/// let field_b = OpticalRotorField::random((256, 256), 2);
///
/// // GPU-accelerated binding
/// let bound = gpu.bind(&field_a, &field_b).await?;
///
/// // GPU-accelerated similarity
/// let sim = gpu.similarity(&field_a, &field_b).await?;
/// ```
pub struct GpuOpticalField {
    device: wgpu::Device,
    queue: wgpu::Queue,
    bind_pipeline: wgpu::ComputePipeline,
    similarity_pipeline: wgpu::ComputePipeline,
    #[allow(dead_code)] // Bundle shader is placeholder - full implementation pending
    bundle_pipeline: wgpu::ComputePipeline,
    lee_encode_pipeline: wgpu::ComputePipeline,
    dimensions: (usize, usize),
}

impl GpuOpticalField {
    /// Initialize GPU context for optical field operations.
    ///
    /// # Arguments
    ///
    /// * `dimensions` - Field dimensions (width, height)
    pub async fn new(dimensions: (usize, usize)) -> GpuHolographicResult<Self> {
        if dimensions.0 == 0 || dimensions.1 == 0 {
            return Err(GpuHolographicError::DimensionMismatch {
                expected: 1,
                actual: 0,
            });
        }

        let instance = wgpu::Instance::default();

        let adapter = instance
            .request_adapter(&wgpu::RequestAdapterOptions {
                power_preference: wgpu::PowerPreference::HighPerformance,
                compatible_surface: None,
                force_fallback_adapter: false,
            })
            .await
            .ok_or_else(|| {
                GpuHolographicError::InitializationError("No GPU adapter found".to_string())
            })?;

        let (device, queue) = adapter
            .request_device(
                &wgpu::DeviceDescriptor {
                    label: Some("Amari Optical GPU Device"),
                    required_features: wgpu::Features::empty(),
                    required_limits: wgpu::Limits::default(),
                },
                None,
            )
            .await
            .map_err(|e| GpuHolographicError::InitializationError(e.to_string()))?;

        // Create compute pipelines
        let bind_pipeline = Self::create_bind_pipeline(&device)?;
        let similarity_pipeline = Self::create_similarity_pipeline(&device)?;
        let bundle_pipeline = Self::create_bundle_pipeline(&device)?;
        let lee_encode_pipeline = Self::create_lee_encode_pipeline(&device)?;

        Ok(Self {
            device,
            queue,
            bind_pipeline,
            similarity_pipeline,
            bundle_pipeline,
            lee_encode_pipeline,
            dimensions,
        })
    }

    /// Get the dimensions this GPU context operates on.
    pub fn dimensions(&self) -> (usize, usize) {
        self.dimensions
    }

    /// Total number of pixels.
    pub fn field_size(&self) -> usize {
        self.dimensions.0 * self.dimensions.1
    }

    /// Heuristic to determine if GPU should be used.
    ///
    /// GPU is beneficial for fields larger than 64×64 (4096 pixels).
    pub fn should_use_gpu(field_size: usize) -> bool {
        field_size >= 4096
    }

    // ========================================================================
    // GPU Bind Operation
    // ========================================================================

    /// GPU-accelerated rotor multiplication (phase addition).
    ///
    /// Computes the geometric product of two rotor fields:
    /// ```text
    /// R_out = R_a · R_b
    /// scalar_out = scalar_a·scalar_b - bivector_a·bivector_b
    /// bivector_out = scalar_a·bivector_b + bivector_a·scalar_b
    /// amplitude_out = amplitude_a · amplitude_b
    /// ```
    pub async fn bind(
        &self,
        a: &OpticalRotorField,
        b: &OpticalRotorField,
    ) -> GpuHolographicResult<OpticalRotorField> {
        self.validate_field_pair(a, b)?;

        // For small fields, use CPU
        if !Self::should_use_gpu(self.field_size()) {
            return self.bind_cpu(a, b);
        }

        self.bind_gpu(a, b).await
    }

    fn bind_cpu(
        &self,
        a: &OpticalRotorField,
        b: &OpticalRotorField,
    ) -> GpuHolographicResult<OpticalRotorField> {
        let n = a.len();
        let mut scalar = Vec::with_capacity(n);
        let mut bivector = Vec::with_capacity(n);
        let mut amplitude = Vec::with_capacity(n);

        let a_s = a.scalars();
        let a_b = a.bivectors();
        let b_s = b.scalars();
        let b_b = b.bivectors();
        let a_amp = a.amplitudes();
        let b_amp = b.amplitudes();

        for i in 0..n {
            scalar.push(a_s[i] * b_s[i] - a_b[i] * b_b[i]);
            bivector.push(a_s[i] * b_b[i] + a_b[i] * b_s[i]);
            amplitude.push(a_amp[i] * b_amp[i]);
        }

        Ok(OpticalRotorField::new(
            // Convert from scalar/bivector to phase
            scalar
                .iter()
                .zip(bivector.iter())
                .map(|(&s, &b)| b.atan2(s))
                .collect(),
            amplitude,
            self.dimensions,
        ))
    }

    async fn bind_gpu(
        &self,
        a: &OpticalRotorField,
        b: &OpticalRotorField,
    ) -> GpuHolographicResult<OpticalRotorField> {
        let n = a.len();

        // Create input buffers (interleaved: scalar, bivector, amplitude for each pixel)
        let a_buffer = self
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("Optical A Scalar Buffer"),
                contents: bytemuck::cast_slice(a.scalars()),
                usage: wgpu::BufferUsages::STORAGE,
            });

        let a_biv_buffer = self
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("Optical A Bivector Buffer"),
                contents: bytemuck::cast_slice(a.bivectors()),
                usage: wgpu::BufferUsages::STORAGE,
            });

        let a_amp_buffer = self
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("Optical A Amplitude Buffer"),
                contents: bytemuck::cast_slice(a.amplitudes()),
                usage: wgpu::BufferUsages::STORAGE,
            });

        let b_buffer = self
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("Optical B Scalar Buffer"),
                contents: bytemuck::cast_slice(b.scalars()),
                usage: wgpu::BufferUsages::STORAGE,
            });

        let b_biv_buffer = self
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("Optical B Bivector Buffer"),
                contents: bytemuck::cast_slice(b.bivectors()),
                usage: wgpu::BufferUsages::STORAGE,
            });

        let b_amp_buffer = self
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("Optical B Amplitude Buffer"),
                contents: bytemuck::cast_slice(b.amplitudes()),
                usage: wgpu::BufferUsages::STORAGE,
            });

        let component_size = (n * std::mem::size_of::<f32>()) as u64;
        let output_size = component_size * 3;

        let output_buffer = self.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Optical Bind Output Buffer"),
            size: output_size,
            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
            mapped_at_creation: false,
        });

        let staging = self.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Optical Bind Staging"),
            size: output_size,
            usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });

        // Create bind group
        let bind_group_layout = self.bind_pipeline.get_bind_group_layout(0);
        let bind_group = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("Optical Bind Group"),
            layout: &bind_group_layout,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: a_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: a_biv_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 2,
                    resource: a_amp_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 3,
                    resource: b_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 4,
                    resource: b_biv_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 5,
                    resource: b_amp_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 6,
                    resource: output_buffer.as_entire_binding(),
                },
            ],
        });

        // Dispatch
        let mut encoder = self
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some("Optical Bind Encoder"),
            });

        {
            let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("Optical Bind Pass"),
                timestamp_writes: None,
            });
            pass.set_pipeline(&self.bind_pipeline);
            pass.set_bind_group(0, &bind_group, &[]);
            let workgroups = n.div_ceil(256) as u32;
            pass.dispatch_workgroups(workgroups, 1, 1);
        }

        encoder.copy_buffer_to_buffer(&output_buffer, 0, &staging, 0, output_size);

        self.queue.submit(Some(encoder.finish()));

        // Read back results as [scalar..., bivector..., amplitude...]
        let combined = self.read_buffer(&staging, n * 3).await?;
        let scalar_result = &combined[0..n];
        let bivector_result = &combined[n..2 * n];
        let amplitude_result = combined[2 * n..3 * n].to_vec();

        // Convert to phase
        let phase: Vec<f32> = scalar_result
            .iter()
            .zip(bivector_result.iter())
            .map(|(&s, &b)| b.atan2(s))
            .collect();

        Ok(OpticalRotorField::new(
            phase,
            amplitude_result,
            self.dimensions,
        ))
    }

    // ========================================================================
    // GPU Similarity Operation
    // ========================================================================

    /// GPU-accelerated similarity computation.
    ///
    /// Computes the normalized inner product of two rotor fields.
    pub async fn similarity(
        &self,
        a: &OpticalRotorField,
        b: &OpticalRotorField,
    ) -> GpuHolographicResult<f32> {
        self.validate_field_pair(a, b)?;

        // For small fields, use CPU
        if !Self::should_use_gpu(self.field_size()) {
            return self.similarity_cpu(a, b);
        }

        self.similarity_gpu(a, b).await
    }

    fn similarity_cpu(
        &self,
        a: &OpticalRotorField,
        b: &OpticalRotorField,
    ) -> GpuHolographicResult<f32> {
        let n = a.len();
        let mut sum = 0.0f32;
        let mut norm_a = 0.0f32;
        let mut norm_b = 0.0f32;

        let a_s = a.scalars();
        let a_biv = a.bivectors();
        let b_s = b.scalars();
        let b_biv = b.bivectors();
        let a_amp = a.amplitudes();
        let b_amp = b.amplitudes();

        for i in 0..n {
            // R_a† · R_b scalar part = a_s·b_s + a_b·b_b
            let inner = a_s[i] * b_s[i] + a_biv[i] * b_biv[i];
            sum += a_amp[i] * b_amp[i] * inner;
            norm_a += a_amp[i] * a_amp[i];
            norm_b += b_amp[i] * b_amp[i];
        }

        let denom = (norm_a * norm_b).sqrt();
        if denom > 1e-10 {
            Ok(sum / denom)
        } else {
            Ok(0.0)
        }
    }

    async fn similarity_gpu(
        &self,
        a: &OpticalRotorField,
        b: &OpticalRotorField,
    ) -> GpuHolographicResult<f32> {
        let n = a.len();

        // Create input buffers
        let a_s_buffer = self
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("A Scalar"),
                contents: bytemuck::cast_slice(a.scalars()),
                usage: wgpu::BufferUsages::STORAGE,
            });

        let a_b_buffer = self
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("A Bivector"),
                contents: bytemuck::cast_slice(a.bivectors()),
                usage: wgpu::BufferUsages::STORAGE,
            });

        let a_amp_buffer = self
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("A Amplitude"),
                contents: bytemuck::cast_slice(a.amplitudes()),
                usage: wgpu::BufferUsages::STORAGE,
            });

        let b_s_buffer = self
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("B Scalar"),
                contents: bytemuck::cast_slice(b.scalars()),
                usage: wgpu::BufferUsages::STORAGE,
            });

        let b_b_buffer = self
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("B Bivector"),
                contents: bytemuck::cast_slice(b.bivectors()),
                usage: wgpu::BufferUsages::STORAGE,
            });

        let b_amp_buffer = self
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("B Amplitude"),
                contents: bytemuck::cast_slice(b.amplitudes()),
                usage: wgpu::BufferUsages::STORAGE,
            });

        // Output buffer for partial sums (one per workgroup)
        let num_workgroups = n.div_ceil(256);
        let partial_size = (num_workgroups * 3 * std::mem::size_of::<f32>()) as u64;

        let partial_buffer = self.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Partial Sums"),
            size: partial_size,
            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
            mapped_at_creation: false,
        });

        let staging = self.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Staging"),
            size: partial_size,
            usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });

        // Create bind group
        let bind_group_layout = self.similarity_pipeline.get_bind_group_layout(0);
        let bind_group = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("Similarity Bind Group"),
            layout: &bind_group_layout,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: a_s_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: a_b_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 2,
                    resource: a_amp_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 3,
                    resource: b_s_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 4,
                    resource: b_b_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 5,
                    resource: b_amp_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 6,
                    resource: partial_buffer.as_entire_binding(),
                },
            ],
        });

        // Dispatch
        let mut encoder = self
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some("Similarity Encoder"),
            });

        {
            let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("Similarity Pass"),
                timestamp_writes: None,
            });
            pass.set_pipeline(&self.similarity_pipeline);
            pass.set_bind_group(0, &bind_group, &[]);
            pass.dispatch_workgroups(num_workgroups as u32, 1, 1);
        }

        encoder.copy_buffer_to_buffer(&partial_buffer, 0, &staging, 0, partial_size);
        self.queue.submit(Some(encoder.finish()));

        // Read partial results and reduce on CPU
        let partials = self.read_buffer(&staging, num_workgroups * 3).await?;

        let mut sum = 0.0f32;
        let mut norm_a = 0.0f32;
        let mut norm_b = 0.0f32;

        for i in 0..num_workgroups {
            sum += partials[i * 3];
            norm_a += partials[i * 3 + 1];
            norm_b += partials[i * 3 + 2];
        }

        let denom = (norm_a * norm_b).sqrt();
        if denom > 1e-10 {
            Ok(sum / denom)
        } else {
            Ok(0.0)
        }
    }

    // ========================================================================
    // GPU Lee Hologram Encoding
    // ========================================================================

    /// GPU-accelerated Lee hologram encoding.
    ///
    /// Encodes an optical rotor field as a binary hologram pattern.
    ///
    /// # Arguments
    ///
    /// * `field` - Input optical rotor field
    /// * `config` - Lee encoder configuration
    pub async fn encode_lee(
        &self,
        field: &OpticalRotorField,
        config: &LeeEncoderConfig,
    ) -> GpuHolographicResult<BinaryHologram> {
        if config.dimensions != self.dimensions {
            return Err(GpuHolographicError::DimensionMismatch {
                expected: self.field_size(),
                actual: config.dimensions.0 * config.dimensions.1,
            });
        }
        self.validate_field(field)?;

        // For small fields, use CPU
        if !Self::should_use_gpu(field.len()) {
            return self.encode_lee_cpu(field, config);
        }

        self.encode_lee_gpu(field, config).await
    }

    fn encode_lee_cpu(
        &self,
        field: &OpticalRotorField,
        config: &LeeEncoderConfig,
    ) -> GpuHolographicResult<BinaryHologram> {
        use amari_holographic::optical::GeometricLeeEncoder;
        let encoder = GeometricLeeEncoder::new(config.clone());
        Ok(encoder.encode(field))
    }

    async fn encode_lee_gpu(
        &self,
        field: &OpticalRotorField,
        config: &LeeEncoderConfig,
    ) -> GpuHolographicResult<BinaryHologram> {
        let n = field.len();
        let (w, h) = config.dimensions;

        // Compute carrier on CPU (it's a simple linear function)
        let cos_angle = config.carrier_angle.cos();
        let sin_angle = config.carrier_angle.sin();
        let mut carrier_s = Vec::with_capacity(n);
        let mut carrier_b = Vec::with_capacity(n);

        for y in 0..h {
            for x in 0..w {
                let t = x as f32 * cos_angle + y as f32 * sin_angle;
                let p = std::f32::consts::TAU * config.carrier_frequency * t;
                carrier_s.push(p.cos());
                carrier_b.push(p.sin());
            }
        }

        // Create buffers
        let field_s_buffer = self
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("Field Scalar"),
                contents: bytemuck::cast_slice(field.scalars()),
                usage: wgpu::BufferUsages::STORAGE,
            });

        let field_b_buffer = self
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("Field Bivector"),
                contents: bytemuck::cast_slice(field.bivectors()),
                usage: wgpu::BufferUsages::STORAGE,
            });

        let field_amp_buffer = self
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("Field Amplitude"),
                contents: bytemuck::cast_slice(field.amplitudes()),
                usage: wgpu::BufferUsages::STORAGE,
            });

        let carrier_s_buffer = self
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("Carrier Scalar"),
                contents: bytemuck::cast_slice(&carrier_s),
                usage: wgpu::BufferUsages::STORAGE,
            });

        let carrier_b_buffer = self
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("Carrier Bivector"),
                contents: bytemuck::cast_slice(&carrier_b),
                usage: wgpu::BufferUsages::STORAGE,
            });

        // Output: u32 packed bits
        let output_words = n.div_ceil(32);
        let output_size = (output_words * std::mem::size_of::<u32>()) as u64;

        let output_buffer = self.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Lee Output"),
            size: output_size,
            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
            mapped_at_creation: false,
        });

        let staging = self.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Staging"),
            size: output_size,
            usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });

        // Create bind group
        let bind_group_layout = self.lee_encode_pipeline.get_bind_group_layout(0);
        let bind_group = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("Lee Encode Bind Group"),
            layout: &bind_group_layout,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: field_s_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: field_b_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 2,
                    resource: field_amp_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 3,
                    resource: carrier_s_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 4,
                    resource: carrier_b_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 5,
                    resource: output_buffer.as_entire_binding(),
                },
            ],
        });

        // Dispatch
        let mut encoder = self
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some("Lee Encode Encoder"),
            });

        {
            let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("Lee Encode Pass"),
                timestamp_writes: None,
            });
            pass.set_pipeline(&self.lee_encode_pipeline);
            pass.set_bind_group(0, &bind_group, &[]);
            pass.dispatch_workgroups(output_words.div_ceil(64) as u32, 1, 1);
        }

        encoder.copy_buffer_to_buffer(&output_buffer, 0, &staging, 0, output_size);
        self.queue.submit(Some(encoder.finish()));

        // Read result and convert to BinaryHologram
        let packed = self.read_buffer_u32(&staging, output_words).await?;

        // Convert to bools
        let mut pattern = Vec::with_capacity(n);
        for i in 0..n {
            let word = i / 32;
            let bit = i % 32;
            pattern.push((packed[word] >> bit) & 1 != 0);
        }

        Ok(BinaryHologram::from_bools(&pattern, config.dimensions))
    }

    // ========================================================================
    // GPU Batch Bind Operation
    // ========================================================================

    /// GPU-accelerated batch binding of multiple field pairs.
    ///
    /// Computes `results[i] = bind(a_batch[i], b_batch[i])` for all i.
    pub async fn batch_bind(
        &self,
        a_batch: &[OpticalRotorField],
        b_batch: &[OpticalRotorField],
    ) -> GpuHolographicResult<Vec<OpticalRotorField>> {
        if a_batch.len() != b_batch.len() {
            return Err(GpuHolographicError::DimensionMismatch {
                expected: a_batch.len(),
                actual: b_batch.len(),
            });
        }

        for (a, b) in a_batch.iter().zip(b_batch.iter()) {
            self.validate_field_pair(a, b)?;
        }

        let mut results = Vec::with_capacity(a_batch.len());
        for (a, b) in a_batch.iter().zip(b_batch.iter()) {
            results.push(self.bind(a, b).await?);
        }
        Ok(results)
    }

    /// GPU-accelerated batch similarity of multiple field pairs.
    ///
    /// Computes `results[i] = similarity(a_batch[i], b_batch[i])` for all i.
    pub async fn batch_similarity(
        &self,
        a_batch: &[OpticalRotorField],
        b_batch: &[OpticalRotorField],
    ) -> GpuHolographicResult<Vec<f32>> {
        if a_batch.len() != b_batch.len() {
            return Err(GpuHolographicError::DimensionMismatch {
                expected: a_batch.len(),
                actual: b_batch.len(),
            });
        }

        for (a, b) in a_batch.iter().zip(b_batch.iter()) {
            self.validate_field_pair(a, b)?;
        }

        let mut results = Vec::with_capacity(a_batch.len());
        for (a, b) in a_batch.iter().zip(b_batch.iter()) {
            results.push(self.similarity(a, b).await?);
        }
        Ok(results)
    }

    // ========================================================================
    // Helper Methods
    // ========================================================================

    fn validate_field_pair(
        &self,
        a: &OpticalRotorField,
        b: &OpticalRotorField,
    ) -> GpuHolographicResult<()> {
        self.validate_field(a)?;
        self.validate_field(b)
    }

    fn validate_field(&self, field: &OpticalRotorField) -> GpuHolographicResult<()> {
        if field.dimensions() != self.dimensions {
            return Err(GpuHolographicError::DimensionMismatch {
                expected: self.field_size(),
                actual: field.len(),
            });
        }
        if field.len() != self.field_size() {
            return Err(GpuHolographicError::DimensionMismatch {
                expected: self.field_size(),
                actual: field.len(),
            });
        }

        let finite = field
            .scalars()
            .iter()
            .chain(field.bivectors().iter())
            .chain(field.amplitudes().iter())
            .all(|value| value.is_finite());
        if !finite {
            return Err(GpuHolographicError::BufferError(
                "optical field contains non-finite values".to_string(),
            ));
        }

        Ok(())
    }

    async fn read_buffer(
        &self,
        buffer: &wgpu::Buffer,
        len: usize,
    ) -> GpuHolographicResult<Vec<f32>> {
        let slice = buffer.slice(..);
        let (sender, receiver) = futures::channel::oneshot::channel();
        slice.map_async(wgpu::MapMode::Read, move |result| {
            let _ = sender.send(result);
        });

        self.device.poll(wgpu::Maintain::Wait);

        receiver
            .await
            .map_err(|_| GpuHolographicError::BufferError("Channel error".to_string()))?
            .map_err(|e| GpuHolographicError::BufferError(e.to_string()))?;

        let data = slice.get_mapped_range();
        let result: Vec<f32> = bytemuck::cast_slice(&data)[..len].to_vec();
        drop(data);
        buffer.unmap();

        Ok(result)
    }

    async fn read_buffer_u32(
        &self,
        buffer: &wgpu::Buffer,
        len: usize,
    ) -> GpuHolographicResult<Vec<u32>> {
        let slice = buffer.slice(..);
        let (sender, receiver) = futures::channel::oneshot::channel();
        slice.map_async(wgpu::MapMode::Read, move |result| {
            let _ = sender.send(result);
        });

        self.device.poll(wgpu::Maintain::Wait);

        receiver
            .await
            .map_err(|_| GpuHolographicError::BufferError("Channel error".to_string()))?
            .map_err(|e| GpuHolographicError::BufferError(e.to_string()))?;

        let data = slice.get_mapped_range();
        let result: Vec<u32> = bytemuck::cast_slice(&data)[..len].to_vec();
        drop(data);
        buffer.unmap();

        Ok(result)
    }

    // ========================================================================
    // Pipeline Creation
    // ========================================================================

    fn create_bind_pipeline(device: &wgpu::Device) -> GpuHolographicResult<wgpu::ComputePipeline> {
        let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
            label: Some("Optical Bind Shader"),
            source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Borrowed(OPTICAL_BIND_SHADER)),
        });

        Ok(
            device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
                label: Some("Optical Bind Pipeline"),
                layout: None,
                module: &shader,
                entry_point: "main",
            }),
        )
    }

    fn create_similarity_pipeline(
        device: &wgpu::Device,
    ) -> GpuHolographicResult<wgpu::ComputePipeline> {
        let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
            label: Some("Optical Similarity Shader"),
            source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Borrowed(OPTICAL_SIMILARITY_SHADER)),
        });

        Ok(
            device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
                label: Some("Optical Similarity Pipeline"),
                layout: None,
                module: &shader,
                entry_point: "main",
            }),
        )
    }

    fn create_bundle_pipeline(
        device: &wgpu::Device,
    ) -> GpuHolographicResult<wgpu::ComputePipeline> {
        let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
            label: Some("Optical Bundle Shader"),
            source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Borrowed(OPTICAL_BUNDLE_SHADER)),
        });

        Ok(
            device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
                label: Some("Optical Bundle Pipeline"),
                layout: None,
                module: &shader,
                entry_point: "main",
            }),
        )
    }

    fn create_lee_encode_pipeline(
        device: &wgpu::Device,
    ) -> GpuHolographicResult<wgpu::ComputePipeline> {
        let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
            label: Some("Lee Encode Shader"),
            source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Borrowed(LEE_ENCODE_SHADER)),
        });

        Ok(
            device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
                label: Some("Lee Encode Pipeline"),
                layout: None,
                module: &shader,
                entry_point: "main",
            }),
        )
    }
}

// ============================================================================
// WGSL Compute Shaders for Optical Operations
// ============================================================================

/// Optical rotor bind shader.
///
/// Computes element-wise rotor product:
/// scalar_out = a_s * b_s - a_b * b_b
/// bivector_out = a_s * b_b + a_b * b_s
/// amplitude_out = a_amp * b_amp
const OPTICAL_BIND_SHADER: &str = r#"
@group(0) @binding(0) var<storage, read> a_scalar: array<f32>;
@group(0) @binding(1) var<storage, read> a_bivector: array<f32>;
@group(0) @binding(2) var<storage, read> a_amplitude: array<f32>;
@group(0) @binding(3) var<storage, read> b_scalar: array<f32>;
@group(0) @binding(4) var<storage, read> b_bivector: array<f32>;
@group(0) @binding(5) var<storage, read> b_amplitude: array<f32>;
@group(0) @binding(6) var<storage, read_write> output: array<f32>;

@compute @workgroup_size(256)
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
    let idx = global_id.x;
    let n = arrayLength(&a_scalar);
    if (idx >= n) {
        return;
    }

    let a_s = a_scalar[idx];
    let a_b = a_bivector[idx];
    let b_s = b_scalar[idx];
    let b_b = b_bivector[idx];

    // Rotor product: (a_s + a_b*e12)(b_s + b_b*e12).
    // Packed output layout: [scalar..., bivector..., amplitude...].
    output[idx] = a_s * b_s - a_b * b_b;
    output[n + idx] = a_s * b_b + a_b * b_s;
    output[2u * n + idx] = a_amplitude[idx] * b_amplitude[idx];
}
"#;

/// Optical similarity shader with workgroup reduction.
///
/// Computes partial sums of: inner_product, norm_a_sq, norm_b_sq
const OPTICAL_SIMILARITY_SHADER: &str = r#"
@group(0) @binding(0) var<storage, read> a_scalar: array<f32>;
@group(0) @binding(1) var<storage, read> a_bivector: array<f32>;
@group(0) @binding(2) var<storage, read> a_amplitude: array<f32>;
@group(0) @binding(3) var<storage, read> b_scalar: array<f32>;
@group(0) @binding(4) var<storage, read> b_bivector: array<f32>;
@group(0) @binding(5) var<storage, read> b_amplitude: array<f32>;
@group(0) @binding(6) var<storage, read_write> partials: array<f32>;

var<workgroup> shared_sum: array<f32, 256>;
var<workgroup> shared_norm_a: array<f32, 256>;
var<workgroup> shared_norm_b: array<f32, 256>;

@compute @workgroup_size(256)
fn main(
    @builtin(global_invocation_id) global_id: vec3<u32>,
    @builtin(local_invocation_id) local_id: vec3<u32>,
    @builtin(workgroup_id) wg_id: vec3<u32>
) {
    let idx = global_id.x;
    let lid = local_id.x;
    let n = arrayLength(&a_scalar);

    // Load and compute local values
    var local_sum: f32 = 0.0;
    var local_norm_a: f32 = 0.0;
    var local_norm_b: f32 = 0.0;

    if (idx < n) {
        let a_s = a_scalar[idx];
        let a_b = a_bivector[idx];
        let b_s = b_scalar[idx];
        let b_b = b_bivector[idx];
        let a_amp = a_amplitude[idx];
        let b_amp = b_amplitude[idx];

        // Inner product: a_s*b_s + a_b*b_b (note: + because of conjugate)
        let inner = a_s * b_s + a_b * b_b;
        local_sum = a_amp * b_amp * inner;
        local_norm_a = a_amp * a_amp;
        local_norm_b = b_amp * b_amp;
    }

    shared_sum[lid] = local_sum;
    shared_norm_a[lid] = local_norm_a;
    shared_norm_b[lid] = local_norm_b;
    workgroupBarrier();

    // Tree reduction
    for (var s = 128u; s > 0u; s = s >> 1u) {
        if (lid < s) {
            shared_sum[lid] += shared_sum[lid + s];
            shared_norm_a[lid] += shared_norm_a[lid + s];
            shared_norm_b[lid] += shared_norm_b[lid + s];
        }
        workgroupBarrier();
    }

    // Write partial result
    if (lid == 0u) {
        let wg = wg_id.x;
        partials[wg * 3u] = shared_sum[0];
        partials[wg * 3u + 1u] = shared_norm_a[0];
        partials[wg * 3u + 2u] = shared_norm_b[0];
    }
}
"#;

/// Optical bundle shader (placeholder - full implementation would handle variable-length input).
const OPTICAL_BUNDLE_SHADER: &str = r#"
@group(0) @binding(0) var<storage, read> input_scalar: array<f32>;
@group(0) @binding(1) var<storage, read> input_bivector: array<f32>;
@group(0) @binding(2) var<storage, read> input_amplitude: array<f32>;
@group(0) @binding(3) var<storage, read> weights: array<f32>;
@group(0) @binding(4) var<storage, read_write> out_scalar: array<f32>;
@group(0) @binding(5) var<storage, read_write> out_bivector: array<f32>;
@group(0) @binding(6) var<storage, read_write> out_amplitude: array<f32>;

@compute @workgroup_size(256)
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
    // Placeholder - full implementation would handle multi-field bundle
    let idx = global_id.x;
    if (idx >= arrayLength(&input_scalar)) {
        return;
    }
    out_scalar[idx] = input_scalar[idx];
    out_bivector[idx] = input_bivector[idx];
    out_amplitude[idx] = input_amplitude[idx];
}
"#;

/// Lee hologram encoding shader.
///
/// Each thread handles 32 pixels and packs results into a u32.
const LEE_ENCODE_SHADER: &str = r#"
@group(0) @binding(0) var<storage, read> field_scalar: array<f32>;
@group(0) @binding(1) var<storage, read> field_bivector: array<f32>;
@group(0) @binding(2) var<storage, read> field_amplitude: array<f32>;
@group(0) @binding(3) var<storage, read> carrier_scalar: array<f32>;
@group(0) @binding(4) var<storage, read> carrier_bivector: array<f32>;
@group(0) @binding(5) var<storage, read_write> output: array<u32>;

const PI: f32 = 3.14159265359;

@compute @workgroup_size(64)
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
    let word_idx = global_id.x;
    if (word_idx >= arrayLength(&output)) {
        return;
    }

    let n = arrayLength(&field_scalar);
    var packed: u32 = 0u;

    for (var bit = 0u; bit < 32u; bit = bit + 1u) {
        let pixel_idx = word_idx * 32u + bit;
        if (pixel_idx >= n) {
            break;
        }

        let f_s = field_scalar[pixel_idx];
        let f_b = field_bivector[pixel_idx];
        let c_s = carrier_scalar[pixel_idx];
        let c_b = carrier_bivector[pixel_idx];
        let amp = field_amplitude[pixel_idx];

        // Modulated scalar = c_s * f_s - c_b * f_b
        let modulated_scalar = c_s * f_s - c_b * f_b;

        // Threshold = cos(π * amplitude)
        let threshold = cos(PI * amp);

        if (modulated_scalar > threshold) {
            packed = packed | (1u << bit);
        }
    }

    output[word_idx] = packed;
}
"#;

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

    #[test]
    fn test_should_use_gpu() {
        assert!(!GpuHolographic::should_use_gpu(10));
        assert!(GpuHolographic::should_use_gpu(100));
        assert!(GpuHolographic::should_use_gpu(1000));
    }

    #[ignore] // Requires GPU initialization
    #[tokio::test]
    async fn test_batch_similarity_cpu() {
        // Test CPU fallback
        let dim = 256;

        // Create test data
        let a: Vec<f64> = (0..dim).map(|i| (i as f64) / dim as f64).collect();
        let b = a.clone();

        // Use CPU path directly (small batch)
        if let Ok(gpu) = GpuHolographic::new(dim).await {
            let result = gpu.batch_similarity_cpu(&a, &b).unwrap();
            assert_eq!(result.len(), 1);
            // Self-similarity should be close to 1.0
            assert!((result[0] - 1.0).abs() < 0.01);
        }
    }

    #[ignore] // Requires GPU initialization
    #[tokio::test]
    async fn test_batch_bundle_cpu() {
        // Test CPU fallback for bundling
        let dim = 256;

        // Use small values to avoid numerical issues
        let a: Vec<f64> = (0..dim).map(|i| (i as f64) / 1000.0).collect();
        let b: Vec<f64> = (0..dim).map(|i| (i as f64) / 500.0).collect();

        if let Ok(gpu) = GpuHolographic::new(dim).await {
            let result = gpu.batch_bundle_cpu(&a, &b).unwrap();
            assert_eq!(result.len(), dim);
            // Check sum - bundle adds corresponding coefficients
            for i in 0..dim {
                assert!(
                    (result[i] - (a[i] + b[i])).abs() < 0.01,
                    "Mismatch at {}: {} vs {}",
                    i,
                    result[i],
                    a[i] + b[i]
                );
            }
        }
    }

    // ========================================================================
    // GPU Optical Field Tests
    // ========================================================================

    #[test]
    fn test_optical_should_use_gpu() {
        // Field size < 4096 should use CPU
        assert!(!GpuOpticalField::should_use_gpu(100));
        assert!(!GpuOpticalField::should_use_gpu(4095));

        // Field size >= 4096 should use GPU
        assert!(GpuOpticalField::should_use_gpu(4096));
        assert!(GpuOpticalField::should_use_gpu(65536)); // 256x256
    }

    #[ignore] // Requires GPU initialization
    #[tokio::test]
    async fn test_optical_bind_cpu() {
        // Test CPU fallback for bind operation (small field)
        let dims = (16, 16); // 256 pixels - uses CPU fallback

        if let Ok(gpu) = GpuOpticalField::new(dims).await {
            // Create two fields with known phases
            let field_a = OpticalRotorField::uniform(0.0, 1.0, dims);
            let field_b = OpticalRotorField::uniform(std::f32::consts::FRAC_PI_4, 1.0, dims);

            let bound = gpu.bind(&field_a, &field_b).await.unwrap();

            // Binding adds phases: 0 + π/4 = π/4
            for y in 0..16 {
                for x in 0..16 {
                    let phase = bound.phase_at(x, y);
                    assert!(
                        (phase - std::f32::consts::FRAC_PI_4).abs() < 1e-5,
                        "Phase mismatch at ({}, {}): {}",
                        x,
                        y,
                        phase
                    );
                }
            }
        }
    }

    #[ignore] // Requires GPU initialization
    #[tokio::test]
    async fn test_optical_similarity_cpu() {
        // Test CPU fallback for similarity
        let dims = (16, 16);

        if let Ok(gpu) = GpuOpticalField::new(dims).await {
            let field_a = OpticalRotorField::random(dims, 42);
            let field_b = field_a.clone();

            // Self-similarity should be 1.0
            let sim = gpu.similarity(&field_a, &field_b).await.unwrap();
            assert!(
                (sim - 1.0).abs() < 1e-5,
                "Self-similarity should be 1.0, got {}",
                sim
            );

            // Different fields should have low similarity
            let field_c = OpticalRotorField::random(dims, 99);
            let sim2 = gpu.similarity(&field_a, &field_c).await.unwrap();
            assert!(
                sim2.abs() < 0.3,
                "Random fields should have low similarity, got {}",
                sim2
            );
        }
    }

    #[ignore] // Requires GPU initialization
    #[tokio::test]
    async fn test_optical_lee_encode_cpu() {
        // Test CPU fallback for Lee encoding
        let dims = (32, 32);

        if let Ok(gpu) = GpuOpticalField::new(dims).await {
            let field = OpticalRotorField::uniform(0.0, 0.5, dims);
            let config = LeeEncoderConfig::new(dims, 0.25);

            let hologram = gpu.encode_lee(&field, &config).await.unwrap();

            assert_eq!(hologram.dimensions(), dims);

            // Fill factor should be reasonable
            let fill = hologram.fill_factor();
            assert!(fill > 0.2 && fill < 0.8, "Unexpected fill factor: {}", fill);
        }
    }
}