llama-cpp-sys-4 0.9.0

Low Level Bindings to llama.cpp
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
#include "ggml-et-ops.h"

#include "ggml-et-cpu-compare.h"
#include "ggml-et-kernels.h"
#include "ggml-impl.h"

#include <stdio.h>

#include <cstdint>

// CPU comparison configuration - can be enabled for debugging
static ggml_et_cpu_compare_config rope_cpu_compare_config = {
    /* .enabled = */ false,
    /* .use_cpu_result = */ false,  // Replace ET result with CPU result
    /* .log_differences = */ true,
    /* .tolerance = */ 1e-5f,
    /* .max_log_elements = */ 4096
};

static ggml_et_cpu_compare_config rms_norm_cpu_compare_config = {
    /* .enabled = */ false,
    /* .use_cpu_result = */ false,
    /* .log_differences = */ true,
    /* .tolerance = */ 1e-5f,
    /* .max_log_elements = */ 4096
};

static ggml_et_cpu_compare_config norm_cpu_compare_config = {
    /* .enabled = */ false,
    /* .use_cpu_result = */ false,
    /* .log_differences = */ true,
    /* .tolerance = */ 1e-5f,
    /* .max_log_elements = */ 4096
};

static ggml_et_cpu_compare_config l2_norm_cpu_compare_config = {
    /* .enabled = */ false,
    /* .use_cpu_result = */ false,
    /* .log_differences = */ true,
    /* .tolerance = */ 1e-5f,
    /* .max_log_elements = */ 4096
};

static ggml_et_cpu_compare_config group_norm_cpu_compare_config = {
    /* .enabled = */ false,
    /* .use_cpu_result = */ false,
    /* .log_differences = */ true,
    /* .tolerance = */ 1e-5f,
    /* .max_log_elements = */ 4096
};

static ggml_et_cpu_compare_config im2col_cpu_compare_config = {
    /* .enabled = */ false,
    /* .use_cpu_result = */ false,
    /* .log_differences = */ true,
    /* .tolerance = */ 1e-5f,
    /* .max_log_elements = */ 4096
};

static ggml_et_cpu_compare_config unary_cpu_compare_config = {
    /* .enabled = */ false,
    /* .use_cpu_result = */ false,
    /* .log_differences = */ true,
    /* .tolerance = */ 1e-4f,
    /* .max_log_elements = */ 4096
};

static ggml_et_cpu_compare_config sum_rows_cpu_compare_config = {
    /* .enabled = */ false,
    /* .use_cpu_result = */ false,
    /* .log_differences = */ true,
    /* .tolerance = */ 1e-5f,
    /* .max_log_elements = */ 4096
};

static ggml_et_cpu_compare_config clamp_cpu_compare_config = {
    /* .enabled = */ false,
    /* .use_cpu_result = */ false,
    /* .log_differences = */ true,
    /* .tolerance = */ 1e-6f,
    /* .max_log_elements = */ 4096
};

static ggml_et_cpu_compare_config mean_cpu_compare_config = {
    /* .enabled = */ false,
    /* .use_cpu_result = */ false,
    /* .log_differences = */ true,
    /* .tolerance = */ 1e-5f,
    /* .max_log_elements = */ 4096
};

static ggml_et_cpu_compare_config sqr_cpu_compare_config = {
    /* .enabled = */ false,
    /* .use_cpu_result = */ false,
    /* .log_differences = */ true,
    /* .tolerance = */ 1e-6f,
    /* .max_log_elements = */ 4096
};

static ggml_et_cpu_compare_config elmap_cpu_compare_config = {
    /* .enabled = */ false,
    /* .use_cpu_result = */ false,
    /* .log_differences = */ true,
    /* .tolerance = */ 1e-6f,
    /* .max_log_elements = */ 4096
};

static ggml_et_cpu_compare_config glu_cpu_compare_config = {
    /* .enabled = */ false,
    /* .use_cpu_result = */ false,
    /* .log_differences = */ true,
    /* .tolerance = */ 1e-5f,
    /* .max_log_elements = */ 4096
};

static ggml_et_cpu_compare_config mul_mat_cpu_compare_config = {
    /* .enabled = */ false,
    /* .use_cpu_result = */ false,
    /* .log_differences = */ true,
    /* .tolerance = */ 0.01,
    /* .max_log_elements = */ 4096
};

static ggml_et_cpu_compare_config mul_mat_id_cpu_compare_config = {
    /* .enabled = */ false,
    /* .use_cpu_result = */ false,
    /* .log_differences = */ true,
    /* .tolerance = */ 0.01,
    /* .max_log_elements = */ 4096
};

static ggml_et_cpu_compare_config softmax_cpu_compare_config = {
    /* .enabled = */ false,
    /* .use_cpu_result = */ false,
    /* .log_differences = */ true,
    /* .tolerance = */ 1e-5f,
    /* .max_log_elements = */ 1024
};

static ggml_et_cpu_compare_config get_rows_cpu_compare_config = {
    /* .enabled = */ false,
    /* .use_cpu_result = */ false,
    /* .log_differences = */ true,
    /* .tolerance = */ 1e-6f,
    /* .max_log_elements = */ 2048
};

static ggml_et_cpu_compare_config pad_cpu_compare_config = {
    /* .enabled = */ false,
    /* .use_cpu_result = */ false,
    /* .log_differences = */ true,
    /* .tolerance = */ 1e-6f,
    /* .max_log_elements = */ 4096
};

static ggml_et_cpu_compare_config cont_cpu_compare_config = {
    /* .enabled = */ false,
    /* .use_cpu_result = */ false,
    /* .log_differences = */ true,
    /* .tolerance = */ 1e-6f,
    /* .max_log_elements = */ 4096
};

static ggml_et_cpu_compare_config concat_cpu_compare_config = {
    /* .enabled = */ false,
    /* .use_cpu_result = */ false,
    /* .log_differences = */ true,
    /* .tolerance = */ 1e-6f,
    /* .max_log_elements = */ 4096
};

static ggml_et_cpu_compare_config cumsum_cpu_compare_config = {
    /* .enabled = */ false,
    /* .use_cpu_result = */ false,
    /* .log_differences = */ true,
    /* .tolerance = */ 1e-6f,
    /* .max_log_elements = */ 4096
};

static ggml_et_cpu_compare_config repeat_cpu_compare_config = {
    /* .enabled = */ false,
    /* .use_cpu_result = */ false,
    /* .log_differences = */ true,
    /* .tolerance = */ 1e-6f,
    /* .max_log_elements = */ 4096
};

static ggml_et_cpu_compare_config ssm_conv_cpu_compare_config = {
    /* .enabled = */ false,
    /* .use_cpu_result = */ false,
    /* .log_differences = */ true,
    /* .tolerance = */ 1e-6f,
    /* .max_log_elements = */ 4096
};

static ggml_et_cpu_compare_config rwkv_wkv6_cpu_compare_config = {
    /* .enabled = */ false,
    /* .use_cpu_result = */ false,
    /* .log_differences = */ true,
    /* .tolerance = */ 1e-4f,
    /* .max_log_elements = */ 4096
};

static ggml_et_cpu_compare_config rwkv_wkv7_cpu_compare_config = {
    /* .enabled = */ false,
    /* .use_cpu_result = */ false,
    /* .log_differences = */ true,
    /* .tolerance = */ 1e-4f,
    /* .max_log_elements = */ 4096
};

static ggml_et_cpu_compare_config set_rows_cpu_compare_config = {
    /* .enabled = */ false,
    /* .use_cpu_result = */ false,
    /* .log_differences = */ true,
    /* .tolerance = */ 1e-6f,
    /* .max_log_elements = */ 2048
};

bool ggml_et_op_rms_norm_mul(ggml_backend_et_device_context * dev_ctx,
                             const ggml_tensor *              rms_norm_node,
                             const ggml_tensor *              mul_node) {
    ET_PERF_START();

    if (!dev_ctx || !rms_norm_node || !mul_node) {
        GGML_LOG_ERROR("ET: Invalid parameters for fused RMS_NORM_MUL operation\n");
        return false;
    }

    if (!rms_norm_node->src[0]) {
        GGML_LOG_ERROR("ET: Fused RMS_NORM_MUL missing required input\n");
        return false;
    }

    // Extract weights: the MUL operand that isn't the rms_norm output
    const ggml_tensor * weights = (mul_node->src[0] == rms_norm_node) ? mul_node->src[1] : mul_node->src[0];

    if (!weights) {
        GGML_LOG_ERROR("ET: Fused RMS_NORM_MUL missing weights tensor\n");
        return false;
    }

    float eps;
    memcpy(&eps, rms_norm_node->op_params, sizeof(float));

    ggml_et_rms_norm_mul_params params;
    params.src0 = *rms_norm_node->src[0];  // input to normalize
    params.src1 = *weights;                // normalization weights
    params.dst  = *mul_node;               // final output
    params.eps  = eps;

    bool kernel_result = ggml_et_launch_kernel(dev_ctx, "rms_norm_mul_f32", &params, sizeof(params), 0xFFFFFFFF);

    ET_PERF_END_EXT("RMS_NORM_MUL", "rms_norm_mul_f32", mul_node, "eps=%.6f", (double) eps);
    return kernel_result;
}

bool ggml_et_op_scale(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    ET_PERF_START();

    if (!dev_ctx || !node) {
        GGML_LOG_ERROR("ET: Invalid parameters for SCALE operation\n");
        return false;
    }

    if (!node->src[0]) {
        GGML_LOG_ERROR("ET: SCALE operation missing required input\n");
        return false;
    }

    if (node->type != GGML_TYPE_F32 || node->src[0]->type != GGML_TYPE_F32) {
        GGML_LOG_ERROR("ET: SCALE operation with unsupported types: dst=%s src0=%s\n", ggml_type_name(node->type),
                       ggml_type_name(node->src[0]->type));
        return false;
    }

    float scale, bias;
    memcpy(&scale, (const float *) node->op_params + 0, sizeof(float));
    memcpy(&bias, (const float *) node->op_params + 1, sizeof(float));

    ggml_et_scale_params params;
    params.src0  = *node->src[0];
    params.dst   = *node;
    params.scale = scale;
    params.bias  = bias;

    bool kernel_result = ggml_et_launch_kernel(dev_ctx, "scale_f32", &params, sizeof(params), 0xFFFFFFFF);

    ET_PERF_END_EXT("SCALE", "scale_f32", node, "scale=%.6f|bias=%.6f", (double) scale, (double) bias);
    return kernel_result;
}

bool ggml_et_op_sqr(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    ET_PERF_START();

    if (!dev_ctx || !node) {
        GGML_LOG_ERROR("ET: Invalid parameters for SQR operation\n");
        return false;
    }

    if (!node->src[0]) {
        GGML_LOG_ERROR("ET: SQR operation missing required input\n");
        return false;
    }

    if (node->type != GGML_TYPE_F32 || node->src[0]->type != GGML_TYPE_F32) {
        GGML_LOG_ERROR("ET: SQR operation with unsupported types: dst=%s src0=%s\n", ggml_type_name(node->type),
                       ggml_type_name(node->src[0]->type));
        return false;
    }

    ggml_et_sqr_params params;
    params.src0 = *node->src[0];  // F32 input tensor
    params.dst  = *node;          // F32 output tensor

    // Phase 1: Initialize CPU comparison context and copy source buffers (before ET kernel)
    ggml_et_cpu_compare_ctx cpu_cmp_ctx;
    bool                    cpu_comparison_active = false;
    if (sqr_cpu_compare_config.enabled) {
        if (ggml_et_cpu_compare_init_pre(&cpu_cmp_ctx, node, GGML_OP_SQR)) {
            cpu_comparison_active = true;
        } else {
            GGML_LOG_WARN("ET: Failed to initialize CPU comparison for SQR operation\n");
        }
    }

    bool kernel_result = ggml_et_launch_kernel(dev_ctx, "sqr_f32", &params, sizeof(params), 0xFFFFFFFF);

    // Phase 2: Execute CPU computation and compare with ET result (after ET kernel)
    if (cpu_comparison_active) {
        if (!ggml_et_cpu_compare_compute_and_check(&cpu_cmp_ctx, node, &sqr_cpu_compare_config)) {
            GGML_LOG_WARN("ET: CPU comparison failed for SQR operation\n");
        }
        ggml_et_cpu_compare_free(&cpu_cmp_ctx);
    }

    ET_PERF_END("SQR", "sqr_f32", node);
    return kernel_result;
}

bool ggml_et_op_sum_rows(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    ET_PERF_START();

    if (!dev_ctx || !node) {
        GGML_LOG_ERROR("ET: Invalid parameters for SUM_ROWS operation\n");
        return false;
    }

    if (!node->src[0]) {
        GGML_LOG_ERROR("ET: SUM_ROWS operation missing required input\n");
        return false;
    }

    if (node->type != GGML_TYPE_F32 || node->src[0]->type != GGML_TYPE_F32) {
        GGML_LOG_ERROR("ET: SUM_ROWS operation with unsupported types: dst=%s src0=%s\n", ggml_type_name(node->type),
                       ggml_type_name(node->src[0]->type));
        return false;
    }

    ggml_et_sum_rows_params params;
    params.src0 = *node->src[0];
    params.dst  = *node;

    // Phase 1: Initialize CPU comparison context
    ggml_et_cpu_compare_ctx cpu_cmp_ctx;
    bool                    cpu_comparison_active = false;
    if (sum_rows_cpu_compare_config.enabled) {
        if (ggml_et_cpu_compare_init_pre(&cpu_cmp_ctx, node, GGML_OP_SUM_ROWS)) {
            cpu_comparison_active = true;
        } else {
            GGML_LOG_WARN("ET: Failed to initialize CPU comparison for SUM_ROWS operation\n");
        }
    }

    bool kernel_result = ggml_et_launch_kernel(dev_ctx, "sum_rows_f32", &params, sizeof(params), 0xFFFFFFFF);

    // Phase 2: Execute CPU computation and compare
    if (cpu_comparison_active) {
        if (!ggml_et_cpu_compare_compute_and_check(&cpu_cmp_ctx, node, &sum_rows_cpu_compare_config)) {
            GGML_LOG_WARN("ET: CPU comparison failed for SUM_ROWS operation\n");
        }
        ggml_et_cpu_compare_free(&cpu_cmp_ctx);
    }

    ET_PERF_END("SUM_ROWS", "sum_rows_f32", node);
    return kernel_result;
}

bool ggml_et_op_mean(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    ET_PERF_START();

    if (!dev_ctx || !node) {
        GGML_LOG_ERROR("ET: Invalid parameters for MEAN operation\n");
        return false;
    }

    if (!node->src[0]) {
        GGML_LOG_ERROR("ET: MEAN operation missing required input\n");
        return false;
    }

    if (node->type != GGML_TYPE_F32 || node->src[0]->type != GGML_TYPE_F32) {
        GGML_LOG_ERROR("ET: MEAN operation with unsupported types: dst=%s src0=%s\n", ggml_type_name(node->type),
                       ggml_type_name(node->src[0]->type));
        return false;
    }

    ggml_et_mean_params params;
    params.src0 = *node->src[0];
    params.dst  = *node;

    ggml_et_cpu_compare_ctx cpu_cmp_ctx;
    bool                    cpu_comparison_active = false;
    if (mean_cpu_compare_config.enabled) {
        if (ggml_et_cpu_compare_init_pre(&cpu_cmp_ctx, node, GGML_OP_MEAN)) {
            cpu_comparison_active = true;
        } else {
            GGML_LOG_WARN("ET: Failed to initialize CPU comparison for MEAN operation\n");
        }
    }

    bool kernel_result = ggml_et_launch_kernel(dev_ctx, "mean_f32", &params, sizeof(params), 0xFFFFFFFF);

    if (cpu_comparison_active) {
        if (!ggml_et_cpu_compare_compute_and_check(&cpu_cmp_ctx, node, &mean_cpu_compare_config)) {
            GGML_LOG_WARN("ET: CPU comparison failed for MEAN operation\n");
        }
        ggml_et_cpu_compare_free(&cpu_cmp_ctx);
    }

    ET_PERF_END("MEAN", "mean_f32", node);
    return kernel_result;
}

bool ggml_et_op_clamp(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    ET_PERF_START();

    if (!dev_ctx || !node) {
        GGML_LOG_ERROR("ET: Invalid parameters for CLAMP operation\n");
        return false;
    }

    if (!node->src[0]) {
        GGML_LOG_ERROR("ET: CLAMP operation missing required input\n");
        return false;
    }

    if (node->type != GGML_TYPE_F32 || node->src[0]->type != GGML_TYPE_F32) {
        GGML_LOG_ERROR("ET: CLAMP operation with unsupported types: dst=%s src0=%s\n", ggml_type_name(node->type),
                       ggml_type_name(node->src[0]->type));
        return false;
    }

    ggml_et_clamp_params params;
    params.src0 = *node->src[0];
    params.dst  = *node;
    // op_params layout per ggml.c::ggml_clamp: { min, max } as floats
    memcpy(&params.min_val, (const float *) node->op_params + 0, sizeof(float));
    memcpy(&params.max_val, (const float *) node->op_params + 1, sizeof(float));

    ggml_et_cpu_compare_ctx cpu_cmp_ctx;
    bool                    cpu_comparison_active = false;
    if (clamp_cpu_compare_config.enabled) {
        if (ggml_et_cpu_compare_init_pre(&cpu_cmp_ctx, node, GGML_OP_CLAMP)) {
            cpu_comparison_active = true;
        } else {
            GGML_LOG_WARN("ET: Failed to initialize CPU comparison for CLAMP operation\n");
        }
    }

    bool kernel_result = ggml_et_launch_kernel(dev_ctx, "clamp_f32", &params, sizeof(params), 0xFFFFFFFF);

    if (cpu_comparison_active) {
        if (!ggml_et_cpu_compare_compute_and_check(&cpu_cmp_ctx, node, &clamp_cpu_compare_config)) {
            GGML_LOG_WARN("ET: CPU comparison failed for CLAMP operation\n");
        }
        ggml_et_cpu_compare_free(&cpu_cmp_ctx);
    }

    ET_PERF_END("CLAMP", "clamp_f32", node);
    return kernel_result;
}

bool ggml_et_op_unary(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    ET_PERF_START();

    if (!dev_ctx || !node) {
        GGML_LOG_ERROR("ET: Invalid parameters for UNARY operation\n");
        return false;
    }

    if (!node->src[0]) {
        GGML_LOG_ERROR("ET: UNARY operation missing required input\n");
        return false;
    }

    if (node->type != GGML_TYPE_F32 || node->src[0]->type != GGML_TYPE_F32) {
        GGML_LOG_ERROR("ET: UNARY operation with unsupported types: dst=%s src0=%s\n", ggml_type_name(node->type),
                       ggml_type_name(node->src[0]->type));
        return false;
    }

    const ggml_unary_op uop     = ggml_get_unary_op(node);
    const char *        op_name = ggml_unary_op_name(uop);

    ggml_et_unary_params params;
    params.src0     = *node->src[0];  // F32 input tensor
    params.dst      = *node;          // F32 output tensor
    params.unary_op = (int32_t) uop;

    // Phase 1: Initialize CPU comparison context and copy source buffers (before ET kernel)
    ggml_et_cpu_compare_ctx cpu_cmp_ctx;
    bool                    cpu_comparison_active = false;
    if (unary_cpu_compare_config.enabled) {
        if (ggml_et_cpu_compare_init_pre(&cpu_cmp_ctx, node, GGML_OP_UNARY)) {
            cpu_comparison_active = true;
        } else {
            GGML_LOG_WARN("ET: Failed to initialize CPU comparison for UNARY/%s operation\n", op_name);
        }
    }

    bool kernel_result = ggml_et_launch_kernel(dev_ctx, "unary_f32", &params, sizeof(params), 0xFFFFFFFF);

    // Phase 2: Execute CPU computation and compare with ET result (after ET kernel)
    if (cpu_comparison_active) {
        if (!ggml_et_cpu_compare_compute_and_check(&cpu_cmp_ctx, node, &unary_cpu_compare_config)) {
            GGML_LOG_WARN("ET: CPU comparison failed for UNARY/%s operation\n", op_name);
        }
        ggml_et_cpu_compare_free(&cpu_cmp_ctx);
    }

    ET_PERF_END_EXT("UNARY", "unary_f32", node, "op=%s", op_name);
    return kernel_result;
}

bool ggml_et_op_mul(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    // Delegate to generic element map operation
    return ggml_et_op_elmap(dev_ctx, node);
}

bool ggml_et_op_add(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    // Delegate to generic element map operation
    return ggml_et_op_elmap(dev_ctx, node);
}

bool ggml_et_op_sub(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    // Delegate to generic element map operation
    return ggml_et_op_elmap(dev_ctx, node);
}

bool ggml_et_op_elmap(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    ET_PERF_START();

    if (!dev_ctx || !node) {
        GGML_LOG_ERROR("ET: Invalid parameters for element map operation\n");
        return false;
    }

    if (!node->src[0] || !node->src[1]) {
        GGML_LOG_ERROR("ET: Element map operation missing required inputs\n");
        return false;
    }

    if (node->type != GGML_TYPE_F32 || node->src[0]->type != GGML_TYPE_F32 || node->src[1]->type != GGML_TYPE_F32) {
        GGML_LOG_ERROR("ET: Element map operation with unsupported types: dst=%s src0=%s src1=%s\n",
                       ggml_type_name(node->type), ggml_type_name(node->src[0]->type),
                       ggml_type_name(node->src[1]->type));
        return false;
    }

    const char * op_name = ggml_op_name(node->op);

    ggml_et_elmap_params params;
    params.src0 = *node->src[0];
    params.src1 = *node->src[1];
    params.dst  = *node;  // F32 output tensor (op type stored in dst.op)

    // Phase 1: Initialize CPU comparison context and copy source buffers (before ET kernel)
    ggml_et_cpu_compare_ctx cpu_cmp_ctx;
    bool                    cpu_comparison_active = false;
    if (elmap_cpu_compare_config.enabled) {
        if (ggml_et_cpu_compare_init_pre(&cpu_cmp_ctx, node, node->op)) {
            cpu_comparison_active = true;
        } else {
            GGML_LOG_WARN("ET: Failed to initialize CPU comparison for %s operation\n", op_name);
        }
    }

    // fprintf(stderr, "ET: el_map s0 [%ld, %ld, %ld, %ld] s1 [%ld, %ld, %ld, %ld]\n",
    //     node->src[0]->ne[0], node->src[0]->ne[1], node->src[0]->ne[2], node->src[0]->ne[3],
    //     node->src[1]->ne[0], node->src[1]->ne[1], node->src[1]->ne[2], node->src[1]->ne[3]);

    bool kernel_result = ggml_et_launch_kernel(dev_ctx, "el_map_f32", &params, sizeof(params), 0xFFFFFFFF);

    // Phase 2: Execute CPU computation and compare with ET result (after ET kernel)
    if (cpu_comparison_active) {
        if (!ggml_et_cpu_compare_compute_and_check(&cpu_cmp_ctx, node, &elmap_cpu_compare_config)) {
            GGML_LOG_WARN("ET: CPU comparison failed for %s operation\n", op_name);
        }
        ggml_et_cpu_compare_free(&cpu_cmp_ctx);
    }

    ET_PERF_END(op_name, "el_map_f32", node);
    return kernel_result;
}

bool ggml_et_op_glu(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    ET_PERF_START();

    // Validate inputs
    if (!dev_ctx || !node) {
        GGML_LOG_ERROR("ET: Invalid parameters for GLU operation\n");
        return false;
    }

    if (!node->src[0]) {
        GGML_LOG_ERROR("ET: GLU operation missing required input\n");
        return false;
    }

    const bool is_split_mode = node->src[1] != nullptr;

    // Only support F32 (as validated by supports_op)
    if (node->type != GGML_TYPE_F32 || node->src[0]->type != GGML_TYPE_F32 ||
        (is_split_mode && node->src[1]->type != GGML_TYPE_F32)) {
        return false;
    }

    // Extract GLU operation parameters from op_params
    int32_t glu_op_type = ggml_get_op_params_i32(node, 0);  // GLU variant (REGLU, GEGLU, SWIGLU, etc.)
    int32_t swapped     = ggml_get_op_params_i32(node, 1);  // Whether gate/value are swapped

    // Supported variants
    switch (glu_op_type) {
        case GGML_GLU_OP_REGLU:
        case GGML_GLU_OP_GEGLU:
        case GGML_GLU_OP_SWIGLU:
        case GGML_GLU_OP_SWIGLU_OAI:
        case GGML_GLU_OP_SWIGLU_CLAMP:
        case GGML_GLU_OP_GEGLU_ERF:
        case GGML_GLU_OP_GEGLU_QUICK:
            break;
        default:
            GGML_LOG_ERROR("ET: GLU operation with unsupported variant: %s\n",
                           ggml_glu_op_name((ggml_glu_op) glu_op_type));
            return false;
    }

    // Get GLU operation name for logging
    const char * glu_op_name = ggml_glu_op_name((ggml_glu_op) glu_op_type);

    // Pack parameters. Single-tensor mode is encoded by zeroing src1.
    ggml_et_glu_params params = {};
    params.src0               = *node->src[0];
    if (is_split_mode) {
        params.src1 = *node->src[1];
    }
    params.dst         = *node;
    params.glu_op_type = glu_op_type;
    params.swapped     = swapped;
    params.alpha       = 0.0f;
    params.limit       = 0.0f;
    if (glu_op_type == GGML_GLU_OP_SWIGLU_OAI) {
        params.alpha = ggml_get_op_params_f32(node, 2);
    }
    if (glu_op_type == GGML_GLU_OP_SWIGLU_OAI || glu_op_type == GGML_GLU_OP_SWIGLU_CLAMP) {
        params.limit = ggml_get_op_params_f32(node, 3);
    }
    // Phase 1: Initialize CPU comparison context and copy source buffers (before ET kernel)
    ggml_et_cpu_compare_ctx cpu_cmp_ctx;
    bool                    cpu_comparison_active = false;
    if (glu_cpu_compare_config.enabled) {
        if (ggml_et_cpu_compare_init_pre(&cpu_cmp_ctx, node, GGML_OP_GLU)) {
            cpu_comparison_active = true;
        } else {
            GGML_LOG_WARN("ET: Failed to initialize CPU comparison for %s operation\n", glu_op_name);
        }
    }

    // Launch ET kernel
    bool kernel_result = ggml_et_launch_kernel(dev_ctx, "glu_f32", &params, sizeof(params), 0xFFFFFFFF);

    // Phase 2: Execute CPU computation and compare with ET result (after ET kernel)
    if (cpu_comparison_active) {
        if (!ggml_et_cpu_compare_compute_and_check(&cpu_cmp_ctx, node, &glu_cpu_compare_config)) {
            GGML_LOG_WARN("ET: CPU comparison failed for %s operation\n", glu_op_name);
        }
        ggml_et_cpu_compare_free(&cpu_cmp_ctx);
    }

    ET_PERF_END("GLU", "glu_f32", node);
    return kernel_result;
}

bool ggml_et_op_mul_mat(ggml_backend_et_device_context * dev_ctx,
                        const ggml_tensor *              node,
                        const ggml_tensor *              add_node) {
    ET_PERF_START();

    if (!dev_ctx || !node) {
        GGML_LOG_ERROR("ET: Invalid parameters for MUL_MAT operation\n");
        return false;
    }

    if (!node->src[0] || !node->src[1]) {
        GGML_LOG_ERROR("ET: MUL_MAT operation missing required inputs\n");
        return false;
    }

    // Fused MM+ADD: when add_node is non-NULL the caller has already validated
    // (Q8_0 weights, F32 acts, exact-shape ADD with stride parity to dst) via
    // ggml_et_can_fuse({MUL_MAT, ADD}). The kernel writes dst = mm + bias and
    // the ADD's output replaces MM's as the actual dst.
    const ggml_tensor * fused_dst   = add_node ? add_node : node;
    const ggml_tensor * bias_tensor = nullptr;
    if (add_node) {
        bias_tensor = (add_node->src[0] == node) ? add_node->src[1] : add_node->src[0];
    }

    const char * kernel_name;
    const char * src0_type_name;

    if (node->type == GGML_TYPE_F32 && node->src[0]->type == GGML_TYPE_Q4_0 && node->src[1]->type == GGML_TYPE_F32 &&
        node->src[1]->ne[1] >= 53 &&      // N >= 53
        node->src[0]->ne[1] % 16 == 0 &&  // M % TILE_M
        node->src[0]->ne[0] % 32 == 0) {  // K % BLOCK_K (Q4_0 block)

        // Matrix engine for N >= 53; partial N (via n_cur-1) and errata padding are handled in-kernel.
        kernel_name    = "mul_mat_Q4_0_matrix_engine";
        src0_type_name = "Q4_0";

    } else if (node->type == GGML_TYPE_F32 && node->src[0]->type == GGML_TYPE_Q4_0 &&
               node->src[1]->type == GGML_TYPE_F32) {
        kernel_name    = "mul_mat_Q4_0";  // N < 53, or M % 16 != 0 or K % 32 != 0
        src0_type_name = "Q4_0";

    } else if (node->type == GGML_TYPE_F32 && node->src[0]->type == GGML_TYPE_Q8_0 &&
               node->src[1]->type == GGML_TYPE_F32) {
        kernel_name    = "mul_mat_Q8_0";
        src0_type_name = "Q8_0";

    } else if (node->type == GGML_TYPE_F32 && node->src[0]->type == GGML_TYPE_F16 &&
               node->src[1]->type == GGML_TYPE_F16 && node->ne[0] % 16 == 0 && node->src[0]->ne[0] % 16 == 0 &&
               node->src[0]->ne[1] % 16 == 0 && node->src[1]->ne[0] != 1) {
        kernel_name    = "mul_mat_f16_matrix_engine";
        src0_type_name = "F16";

    } else if (node->type == GGML_TYPE_F32 && node->src[0]->type == GGML_TYPE_F16 &&
               (node->src[1]->type == GGML_TYPE_F16 || node->src[1]->type == GGML_TYPE_F32)) {
        kernel_name    = "mul_mat_f16";
        src0_type_name = "F16";

    } else if (node->type == GGML_TYPE_F32 && node->src[0]->type == GGML_TYPE_F32 &&
               node->src[1]->type == GGML_TYPE_F32 && node->ne[0] % 16 == 0 && node->src[0]->ne[0] % 16 == 0 &&
               node->src[0]->ne[1] % 16 == 0 && node->src[1]->ne[0] != 1) {  // GEMV is faster with the generic path

        kernel_name    = "mul_mat_f32_matrix_engine";
        src0_type_name = "F32";
    } else if (node->type == GGML_TYPE_F32 && node->src[0]->type == GGML_TYPE_F32 &&
               (node->src[1]->type == GGML_TYPE_F16 || node->src[1]->type == GGML_TYPE_F32)) {
        kernel_name    = "mul_mat_f32";
        src0_type_name = "F32";
    } else {
        GGML_LOG_ERROR("ET: MUL_MAT operation with unsupported types: dst=%s src0=%s src1=%s\n",
                       ggml_type_name(node->type), ggml_type_name(node->src[0]->type),
                       ggml_type_name(node->src[1]->type));
        return false;
    }

    ggml_et_binary_params params;
    params.src0 = *node->src[0];  // weight matrix
    params.src1 = *node->src[1];  // activation matrix
    params.dst  = *fused_dst;     // output (= add_node when fused, else node)

    ggml_et_cpu_compare_ctx cpu_cmp_ctx;
    bool                    cpu_comparison_active = false;
    if (mul_mat_cpu_compare_config.enabled) {
        if (ggml_et_cpu_compare_init_pre(&cpu_cmp_ctx, fused_dst, GGML_OP_MUL_MAT)) {
            cpu_comparison_active = true;
        } else {
            GGML_LOG_WARN("ET: Failed to initialize CPU comparison for MUL_MAT operation\n");
        }
    }

    bool kernel_result;
    if (node->src[0]->type == GGML_TYPE_Q8_0) {
        // Q8_0 kernel always takes the extended struct. bias.data is non-NULL
        // only on the fused path; otherwise the kernel skips the add entirely.
        ggml_et_mm_q8_params q8_params = {};
        q8_params.src0                 = params.src0;
        q8_params.src1                 = params.src1;
        q8_params.dst                  = params.dst;
        if (bias_tensor) {
            q8_params.bias = *bias_tensor;
        }
        kernel_result = ggml_et_launch_kernel(dev_ctx, kernel_name, &q8_params, sizeof(q8_params), 0xFFFFFFFF);
    } else {
        // Non-Q8 MM kernels don't yet support fused-add; the graph fuse check
        // already rejects non-Q8 pairs, so add_node is always nullptr here.
        kernel_result = ggml_et_launch_kernel(dev_ctx, kernel_name, &params, sizeof(params), 0xFFFFFFFF);
    }

    // printf("Tensor error:");
    // if (params.src0.data != NULL)
    // {
    //     printf("Ptr OK\n");
    //     printf("node->data ptr = %p\n", node->data);
    //     // if (once < 100){
    //     //     // uint64_t * host_data = (uint64_t *) node->data;
    //     //     // printf("Tensor error: %lu\n", host_data[0]);

    //     //     // printf("Tensor error:");
    //     //     once++;
    //     // }
    // }

    // Phase 2: Execute CPU computation and compare with ET result (after ET kernel)
    if (cpu_comparison_active) {
        if (!ggml_et_cpu_compare_compute_and_check(&cpu_cmp_ctx, fused_dst, &mul_mat_cpu_compare_config)) {
            GGML_LOG_WARN("ET: CPU comparison failed for MUL_MAT operation\n");
        }
        ggml_et_cpu_compare_free(&cpu_cmp_ctx);
    }

    {
        // Calculate actual FLOPs including batch/sequence dimensions
        // dst shape: [M, N, ne2, ne3] where M=ne[1], N=ne[0]
        int64_t m   = node->ne[1];
        int64_t n   = node->ne[0];
        int64_t k   = node->src[0]->ne[0];
        int64_t ne2 = node->ne[2];
        int64_t ne3 = node->ne[3];

        // Total FLOPs = (batch_size) * M * N * (2*K - 1)
        // Each MxN matrix-matrix multiply does M*N*(2*K-1) FLOPs
        // Broadcasting is handled by repeating computation, so count actual operations
        int64_t batch_size  = ne2 * ne3;
        int64_t total_flops = batch_size * m * n * (2 * k - 1);

        char kernel_variant[64];
        snprintf(kernel_variant, sizeof(kernel_variant), "%s_%sx%s", kernel_name, src0_type_name,
                 ggml_type_name(node->src[1]->type));
        ET_PERF_END_EXT("MUL_MAT", kernel_variant, node, "flops=%" PRId64, total_flops);
    }
    return kernel_result;
}

bool ggml_et_op_mul_mat_id(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    ET_PERF_START();
    if (!dev_ctx || !node) {
        GGML_LOG_ERROR("ET: Invalid parameters for MUL_MAT_ID operation\n");
        return false;
    }

    if (!node->src[0] || !node->src[1] || !node->src[2]) {
        GGML_LOG_ERROR("ET: MUL_MAT_ID operation missing required inputs\n");
        return false;
    }

    const char * kernel_name;
    const char * src0_type_name;

    // Support Q8_0/Q4_0/F16/F32 x F32 -> F32 matrix multiplication with expert selection
    if (node->type == GGML_TYPE_F32 && node->src[0]->type == GGML_TYPE_Q8_0 && node->src[1]->type == GGML_TYPE_F32 &&
        node->src[2]->type == GGML_TYPE_I32) {
        kernel_name    = "mul_mat_id_Q8_0";
        src0_type_name = "Q8_0";

    } else if (node->type == GGML_TYPE_F32 && node->src[0]->type == GGML_TYPE_Q4_0 &&
               node->src[1]->type == GGML_TYPE_F32 && node->src[2]->type == GGML_TYPE_I32) {
        kernel_name    = "mul_mat_id_Q4_0";
        src0_type_name = "Q4_0";

    } else if (node->type == GGML_TYPE_F32 && node->src[0]->type == GGML_TYPE_F16 &&
               node->src[1]->type == GGML_TYPE_F32 && node->src[2]->type == GGML_TYPE_I32) {
        kernel_name    = "mul_mat_id_f32";
        src0_type_name = "F16";

    } else if (node->type == GGML_TYPE_F32 && node->src[0]->type == GGML_TYPE_F32 &&
               node->src[1]->type == GGML_TYPE_F32 && node->src[2]->type == GGML_TYPE_I32) {
        kernel_name    = "mul_mat_id_f32";
        src0_type_name = "F32";

    } else {
        GGML_LOG_ERROR("ET: MUL_MAT_ID operation with unsupported types: dst=%s src0=%s src1=%s src2=%s\n",
                       ggml_type_name(node->type), ggml_type_name(node->src[0]->type),
                       ggml_type_name(node->src[1]->type), ggml_type_name(node->src[2]->type));
        return false;
    }

    // Pack parameters - copy full tensor structures
    ggml_et_mul_mat_id_params params;
    params.src0 = *node->src[0];  // Expert weight matrices (Q8_0/F16/F32)
    params.src1 = *node->src[1];  // Activation matrix (F32)
    params.src2 = *node->src[2];  // Expert indices (I32)
    params.dst  = *node;          // Output matrix (F32)

    // Phase 1: Initialize CPU comparison context and copy source buffers (before ET kernel)
    ggml_et_cpu_compare_ctx cpu_cmp_ctx;
    bool                    cpu_comparison_active = false;
    if (mul_mat_id_cpu_compare_config.enabled) {
        if (ggml_et_cpu_compare_init_pre(&cpu_cmp_ctx, node, GGML_OP_MUL_MAT_ID)) {
            cpu_comparison_active = true;
        } else {
            GGML_LOG_WARN("ET: Failed to initialize CPU comparison for MUL_MAT_ID operation\n");
        }
    }

    // Launch ET kernel
    bool kernel_result = ggml_et_launch_kernel(dev_ctx, kernel_name, &params, sizeof(params), 0xFFFFFFFF);

    // Phase 2: Execute CPU computation and compare with ET result (after ET kernel)
    if (cpu_comparison_active) {
        if (!ggml_et_cpu_compare_compute_and_check(&cpu_cmp_ctx, node, &mul_mat_id_cpu_compare_config)) {
            GGML_LOG_WARN("ET: CPU comparison failed for MUL_MAT_ID operation\n");
        }
        ggml_et_cpu_compare_free(&cpu_cmp_ctx);
    }

    // Calculate FLOPs (approximate - similar to MUL_MAT but with expert routing overhead)
    // Each expert computation is similar to a MUL_MAT, but we only compute for selected experts
    int64_t K             = node->src[0]->ne[0];
    int64_t M             = node->src[0]->ne[1];
    int64_t n_expert_used = node->src[2]->ne[0];
    int64_t batch         = node->src[2]->ne[1];

    int64_t total_flops = batch * n_expert_used * M * (2 * K - 1);

    char kernel_variant[64];
    snprintf(kernel_variant, sizeof(kernel_variant), "%s_%sx%s", kernel_name, src0_type_name,
             ggml_type_name(node->src[1]->type));
    ET_PERF_END_EXT("MUL_MAT_ID", kernel_variant, node, "flops=%" PRId64 "|n_expert=%lld|n_expert_used=%lld",
                    total_flops, (long long) node->src[0]->ne[2], (long long) n_expert_used);

    return kernel_result;
}

bool ggml_et_op_rope(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    ET_PERF_START();

    if (!dev_ctx || !node) {
        GGML_LOG_ERROR("ET: Invalid parameters for ROPE operation\n");
        return false;
    }

    if (!node->src[0] || !node->src[1]) {
        GGML_LOG_ERROR("ET: ROPE operation missing required inputs\n");
        return false;
    }

    const char * kernel_name;

    if (node->type == GGML_TYPE_F32 && node->src[0]->type == GGML_TYPE_F32 && node->src[1]->type == GGML_TYPE_I32) {
        kernel_name = "rope_f32";
    } else {
        return false;
    }

    // Pack parameters - copy full tensor structures and op_params
    ggml_et_rope_params params;
    params.src0 = *node->src[0];                       // F32 input tensor
    params.src1 = *node->src[1];                       // I32 position tensor
    if (node->src[2]) {
        params.src2 = *node->src[2];                   // F32 frequency factors (optional)
    } else {
        memset(&params.src2, 0, sizeof(params.src2));  // Zero if not provided
    }
    params.dst = *node;                                // F32 output tensor

    params.rope_params.n_past     = ((const int32_t *) node->op_params)[0];
    params.rope_params.n_dims     = ((const int32_t *) node->op_params)[1];
    params.rope_params.mode       = ((const int32_t *) node->op_params)[2];
    params.rope_params.n_ctx      = ((const int32_t *) node->op_params)[3];
    params.rope_params.n_ctx_orig = ((const int32_t *) node->op_params)[4];
    memcpy(&params.rope_params.freq_base, (const int32_t *) node->op_params + 5, sizeof(float));
    memcpy(&params.rope_params.freq_scale, (const int32_t *) node->op_params + 6, sizeof(float));
    memcpy(&params.rope_params.ext_factor, (const int32_t *) node->op_params + 7, sizeof(float));
    memcpy(&params.rope_params.attn_factor, (const int32_t *) node->op_params + 8, sizeof(float));
    memcpy(&params.rope_params.beta_fast, (const int32_t *) node->op_params + 9, sizeof(float));
    memcpy(&params.rope_params.beta_slow, (const int32_t *) node->op_params + 10, sizeof(float));
    if (params.rope_params.mode & GGML_ROPE_TYPE_MROPE) {
        memcpy(params.rope_params.sections, (const int32_t *) node->op_params + 11, sizeof(int32_t) * 4);
    } else {
        memset(params.rope_params.sections, 0, sizeof(params.rope_params.sections));
    }

    // Phase 1: Initialize CPU comparison context and copy source buffers (before ET kernel)
    ggml_et_cpu_compare_ctx cpu_cmp_ctx;
    bool                    cpu_comparison_active = false;
    if (rope_cpu_compare_config.enabled) {
        GGML_LOG_DEBUG("ET: Initializing CPU comparison for ROPE operation\n");
        if (ggml_et_cpu_compare_init_pre(&cpu_cmp_ctx, node, GGML_OP_ROPE)) {
            cpu_comparison_active = true;
        } else {
            GGML_LOG_WARN("ET: Failed to initialize CPU comparison for ROPE operation\n");
        }
    }

    bool kernel_result = ggml_et_launch_kernel(dev_ctx, kernel_name, &params, sizeof(params), 0xFFFFFFFF);

    // Phase 2: Execute CPU computation and compare with ET result (after ET kernel)
    if (cpu_comparison_active) {
        if (!ggml_et_cpu_compare_compute_and_check(&cpu_cmp_ctx, node, &rope_cpu_compare_config)) {
            GGML_LOG_WARN("ET: CPU comparison failed for ROPE operation\n");
        }
        ggml_et_cpu_compare_free(&cpu_cmp_ctx);
    }

    ET_PERF_END_EXT("ROPE", kernel_name, node, "mode=0x%x|n_dims=%d|freq_base=%.2f|freq_scale=%.2f",
                    params.rope_params.mode, params.rope_params.n_dims, (double) params.rope_params.freq_base,
                    (double) params.rope_params.freq_scale);
    return kernel_result;
}

bool ggml_et_op_rms_norm(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    ET_PERF_START();

    if (!dev_ctx || !node) {
        GGML_LOG_ERROR("ET: Invalid parameters for RMS_NORM operation\n");
        return false;
    }

    if (!node->src[0]) {
        GGML_LOG_ERROR("ET: RMS_NORM operation missing required input\n");
        return false;
    }

    const char * kernel_name;

    if (node->type == GGML_TYPE_F32 && node->src[0]->type == GGML_TYPE_F32) {
        kernel_name = "rms_norm_f32";

    } else {
        GGML_LOG_ERROR("ET: RMS_NORM operation with unsupported types: dst=%s src0=%s\n", ggml_type_name(node->type),
                       ggml_type_name(node->src[0]->type));
        return false;
    }

    float eps;
    memcpy(&eps, node->op_params, sizeof(float));

    ggml_et_rms_norm_params params;
    params.src0 = *node->src[0];  // F32 input tensor
    params.dst  = *node;          // F32 output tensor
    params.eps  = eps;            // Epsilon parameter for numerical stability

    // Phase 1: Initialize CPU comparison context and copy source buffers (before ET kernel)
    ggml_et_cpu_compare_ctx cpu_cmp_ctx;
    bool                    cpu_comparison_active = false;
    if (rms_norm_cpu_compare_config.enabled) {
        if (ggml_et_cpu_compare_init_pre(&cpu_cmp_ctx, node, GGML_OP_RMS_NORM)) {
            cpu_comparison_active = true;
        } else {
            GGML_LOG_WARN("ET: Failed to initialize CPU comparison for RMS_NORM operation\n");
        }
    }

    bool kernel_result = ggml_et_launch_kernel(dev_ctx, kernel_name, &params, sizeof(params), 0xFFFFFFFF);

    // Phase 2: Execute CPU computation and compare with ET result (after ET kernel)
    if (cpu_comparison_active) {
        if (!ggml_et_cpu_compare_compute_and_check(&cpu_cmp_ctx, node, &rms_norm_cpu_compare_config)) {
            GGML_LOG_WARN("ET: CPU comparison failed for RMS_NORM operation\n");
        }
        ggml_et_cpu_compare_free(&cpu_cmp_ctx);
    }

    ET_PERF_END_EXT("RMS_NORM", kernel_name, node, "eps=%.6f", (double) eps);
    return kernel_result;
}

bool ggml_et_op_norm(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    ET_PERF_START();

    if (!dev_ctx || !node) {
        GGML_LOG_ERROR("ET: Invalid parameters for NORM operation\n");
        return false;
    }

    if (!node->src[0]) {
        GGML_LOG_ERROR("ET: NORM operation missing required input\n");
        return false;
    }

    const char * kernel_name;

    if (node->type == GGML_TYPE_F32 && node->src[0]->type == GGML_TYPE_F32) {
        kernel_name = "norm_f32";

    } else {
        GGML_LOG_ERROR("ET: NORM operation with unsupported types: dst=%s src0=%s\n", ggml_type_name(node->type),
                       ggml_type_name(node->src[0]->type));
        return false;
    }

    float eps;
    memcpy(&eps, node->op_params, sizeof(float));

    ggml_et_norm_params params;
    params.src0 = *node->src[0];  // F32 input tensor
    params.dst  = *node;          // F32 output tensor
    params.eps  = eps;            // Epsilon parameter for numerical stability

    // Phase 1: Initialize CPU comparison context and copy source buffers (before ET kernel)
    ggml_et_cpu_compare_ctx cpu_cmp_ctx;
    bool                    cpu_comparison_active = false;
    if (norm_cpu_compare_config.enabled) {
        if (ggml_et_cpu_compare_init_pre(&cpu_cmp_ctx, node, GGML_OP_NORM)) {
            cpu_comparison_active = true;
        } else {
            GGML_LOG_WARN("ET: Failed to initialize CPU comparison for NORM operation\n");
        }
    }

    bool kernel_result = ggml_et_launch_kernel(dev_ctx, kernel_name, &params, sizeof(params), 0xFFFFFFFF);

    // Phase 2: Execute CPU computation and compare with ET result (after ET kernel)
    if (cpu_comparison_active) {
        if (!ggml_et_cpu_compare_compute_and_check(&cpu_cmp_ctx, node, &norm_cpu_compare_config)) {
            GGML_LOG_WARN("ET: CPU comparison failed for NORM operation\n");
        }
        ggml_et_cpu_compare_free(&cpu_cmp_ctx);
    }

    ET_PERF_END_EXT("NORM", kernel_name, node, "eps=%.6f", (double) eps);
    return kernel_result;
}

bool ggml_et_op_l2_norm(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    ET_PERF_START();

    if (!dev_ctx || !node) {
        GGML_LOG_ERROR("ET: Invalid parameters for L2_NORM operation\n");
        return false;
    }

    if (!node->src[0]) {
        GGML_LOG_ERROR("ET: L2_NORM operation missing required input\n");
        return false;
    }

    const char * kernel_name;

    if (node->type == GGML_TYPE_F32 && node->src[0]->type == GGML_TYPE_F32) {
        kernel_name = "l2_norm_f32";

    } else {
        GGML_LOG_ERROR("ET: L2_NORM operation with unsupported types: dst=%s src0=%s\n", ggml_type_name(node->type),
                       ggml_type_name(node->src[0]->type));
        return false;
    }

    float eps;
    memcpy(&eps, node->op_params, sizeof(float));

    ggml_et_l2_norm_params params;
    params.src0 = *node->src[0];  // F32 input tensor
    params.dst  = *node;          // F32 output tensor
    params.eps  = eps;            // Epsilon parameter for numerical stability

    // Phase 1: Initialize CPU comparison context and copy source buffers (before ET kernel)
    ggml_et_cpu_compare_ctx cpu_cmp_ctx;
    bool                    cpu_comparison_active = false;
    if (l2_norm_cpu_compare_config.enabled) {
        if (ggml_et_cpu_compare_init_pre(&cpu_cmp_ctx, node, GGML_OP_L2_NORM)) {
            cpu_comparison_active = true;
        } else {
            GGML_LOG_WARN("ET: Failed to initialize CPU comparison for L2_NORM operation\n");
        }
    }

    bool kernel_result = ggml_et_launch_kernel(dev_ctx, kernel_name, &params, sizeof(params), 0xFFFFFFFF);

    // Phase 2: Execute CPU computation and compare with ET result (after ET kernel)
    if (cpu_comparison_active) {
        if (!ggml_et_cpu_compare_compute_and_check(&cpu_cmp_ctx, node, &l2_norm_cpu_compare_config)) {
            GGML_LOG_WARN("ET: CPU comparison failed for L2_NORM operation\n");
        }
        ggml_et_cpu_compare_free(&cpu_cmp_ctx);
    }

    ET_PERF_END_EXT("L2_NORM", kernel_name, node, "eps=%.6f", (double) eps);
    return kernel_result;
}

bool ggml_et_op_group_norm(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    ET_PERF_START();

    if (!dev_ctx || !node) {
        GGML_LOG_ERROR("ET: Invalid parameters for GROUP_NORM operation\n");
        return false;
    }

    if (!node->src[0]) {
        GGML_LOG_ERROR("ET: GROUP_NORM operation missing required input\n");
        return false;
    }

    if (node->type != GGML_TYPE_F32 || node->src[0]->type != GGML_TYPE_F32) {
        GGML_LOG_ERROR("ET: GROUP_NORM operation with unsupported types: dst=%s src0=%s\n", ggml_type_name(node->type),
                       ggml_type_name(node->src[0]->type));
        return false;
    }

    const int32_t n_groups = ggml_get_op_params_i32(node, 0);
    float         eps;
    memcpy(&eps, (const float *) node->op_params + 1, sizeof(float));

    ggml_et_group_norm_params params;
    params.src0     = *node->src[0];
    params.dst      = *node;
    params.n_groups = n_groups;
    params.eps      = eps;

    ggml_et_cpu_compare_ctx cpu_cmp_ctx;
    bool                    cpu_comparison_active = false;
    if (group_norm_cpu_compare_config.enabled) {
        if (ggml_et_cpu_compare_init_pre(&cpu_cmp_ctx, node, GGML_OP_GROUP_NORM)) {
            cpu_comparison_active = true;
        } else {
            GGML_LOG_WARN("ET: Failed to initialize CPU comparison for GROUP_NORM operation\n");
        }
    }

    bool kernel_result = ggml_et_launch_kernel(dev_ctx, "group_norm_f32", &params, sizeof(params), 0xFFFFFFFF);

    if (cpu_comparison_active) {
        if (!ggml_et_cpu_compare_compute_and_check(&cpu_cmp_ctx, node, &group_norm_cpu_compare_config)) {
            GGML_LOG_WARN("ET: CPU comparison failed for GROUP_NORM operation\n");
        }
        ggml_et_cpu_compare_free(&cpu_cmp_ctx);
    }

    ET_PERF_END_EXT("GROUP_NORM", "group_norm_f32", node, "eps=%.6f|n_groups=%d", (double) eps, n_groups);
    return kernel_result;
}

bool ggml_et_op_im2col(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    ET_PERF_START();

    if (!dev_ctx || !node) {
        GGML_LOG_ERROR("ET: Invalid parameters for IM2COL operation\n");
        return false;
    }

    if (!node->src[0] || !node->src[1]) {
        GGML_LOG_ERROR("ET: IM2COL operation missing required inputs\n");
        return false;
    }

    const bool supported_types =
        (node->type == GGML_TYPE_F32 && node->src[1]->type == GGML_TYPE_F32) ||
        (node->type == GGML_TYPE_F16 && (node->src[1]->type == GGML_TYPE_F16 || node->src[1]->type == GGML_TYPE_F32));

    if (!supported_types) {
        GGML_LOG_ERROR("ET: IM2COL operation with unsupported types: dst=%s src1=%s\n", ggml_type_name(node->type),
                       ggml_type_name(node->src[1]->type));
        return false;
    }

    ggml_et_im2col_params params;
    params.src0 = *node->src[0];
    params.src1 = *node->src[1];
    params.dst  = *node;

    ggml_et_cpu_compare_ctx cpu_cmp_ctx;
    bool                    cpu_comparison_active = false;
    if (im2col_cpu_compare_config.enabled) {
        if (ggml_et_cpu_compare_init_pre(&cpu_cmp_ctx, node, GGML_OP_IM2COL)) {
            cpu_comparison_active = true;
        } else {
            GGML_LOG_WARN("ET: Failed to initialize CPU comparison for IM2COL operation\n");
        }
    }

    bool kernel_result = ggml_et_launch_kernel(dev_ctx, "im2col", &params, sizeof(params), 0xFFFFFFFF);

    if (cpu_comparison_active) {
        if (!ggml_et_cpu_compare_compute_and_check(&cpu_cmp_ctx, node, &im2col_cpu_compare_config)) {
            GGML_LOG_WARN("ET: CPU comparison failed for IM2COL operation\n");
        }
        ggml_et_cpu_compare_free(&cpu_cmp_ctx);
    }

    ET_PERF_END("IM2COL", "im2col", node);
    return kernel_result;
}

bool ggml_et_op_conv_2d(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    ET_PERF_START();

    if (!dev_ctx || !node) {
        return false;
    }
    if (!node->src[0] || !node->src[1]) {
        return false;
    }
    if (!node->data || !node->src[0]->data || !node->src[1]->data) {
        return false;
    }

    // Kernel constraints (mirror supports_op; recheck here as a guard).
    const ggml_tensor * flt = node->src[0];  // [Kw, Kh, Cin, Cout]
    const ggml_tensor * in  = node->src[1];  // [W,  H,  Cin, N]
    if (node->type != GGML_TYPE_F32 || flt->type != GGML_TYPE_F32 || in->type != GGML_TYPE_F32) {
        return false;
    }

    const int32_t s0 = ggml_get_op_params_i32(node, 0);
    const int32_t s1 = ggml_get_op_params_i32(node, 1);
    const int32_t p0 = ggml_get_op_params_i32(node, 2);
    const int32_t p1 = ggml_get_op_params_i32(node, 3);
    const int32_t d0 = ggml_get_op_params_i32(node, 4);
    const int32_t d1 = ggml_get_op_params_i32(node, 5);

    if (s0 < 1 || s1 < 1) {
        return false;
    }
    if (d0 != 1 || d1 != 1) {
        return false;
    }
    if (flt->ne[2] % 16 != 0 || flt->ne[3] % 16 != 0) {
        return false;
    }
    if (in->ne[3] != 1) {
        return false;
    }
    if (node->ne[0] <= 0) {
        return false;  // OW > 0 (any width OK; staging path handles non-16)
    }
    (void) p0;
    (void) p1;

    ggml_et_binary_params params;
    params.src0 = *node->src[0];
    params.src1 = *node->src[1];
    params.dst  = *node;

    bool kernel_result = ggml_et_launch_kernel(dev_ctx, "conv_2d_f32_me", &params, sizeof(params), 0xFFFFFFFFu);

    ET_PERF_END("CONV_2D", "conv_2d_f32_me", node);
    return kernel_result;
}

bool ggml_et_op_softmax(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    ET_PERF_START();

    if (!dev_ctx || !node) {
        GGML_LOG_ERROR("ET: Invalid parameters for SOFTMAX operation\n");
        return false;
    }

    if (!node->src[0]) {
        GGML_LOG_ERROR("ET: SOFTMAX operation missing required input\n");
        return false;
    }

    const char * kernel_name;

    if (node->type == GGML_TYPE_F32 && node->src[0]->type == GGML_TYPE_F32) {
        kernel_name = "softmax_f32";

    } else {
        GGML_LOG_ERROR("ET: SOFTMAX operation with unsupported types: dst=%s src0=%s\n", ggml_type_name(node->type),
                       ggml_type_name(node->src[0]->type));
        return false;
    }

    // Validate contiguity requirements
    if (!ggml_is_contiguous(node)) {
        GGML_LOG_ERROR("ET: SOFTMAX operation requires contiguous destination tensor\n");
        return false;
    }

    if (!ggml_is_contiguous(node->src[0])) {
        GGML_LOG_ERROR("ET: SOFTMAX operation requires contiguous source tensor\n");
        return false;
    }

    // Check optional mask tensor
    if (node->src[1]) {
        if (node->src[1]->type != GGML_TYPE_F32) {
            GGML_LOG_ERROR("ET: SOFTMAX operation with unsupported mask type: %s (F32 required)\n",
                           ggml_type_name(node->src[1]->type));
            return false;
        }
        if (!ggml_is_contiguous(node->src[1])) {
            GGML_LOG_ERROR("ET: SOFTMAX operation requires contiguous mask tensor\n");
            return false;
        }
    }

    // Check optional sinks tensor
    if (node->src[2]) {
        if (node->src[2]->type != GGML_TYPE_F32) {
            GGML_LOG_ERROR("ET: SOFTMAX operation with unsupported sinks type: %s (F32 required)\n",
                           ggml_type_name(node->src[2]->type));
            return false;
        }
        if (!ggml_is_contiguous(node->src[2])) {
            GGML_LOG_ERROR("ET: SOFTMAX operation requires contiguous sinks tensor\n");
            return false;
        }
    }

    // Extract scale and max_bias from op_params
    float scale    = 1.0f;
    float max_bias = 0.0f;
    if (node->op_params) {
        memcpy(&scale, (const float *) node->op_params + 0, sizeof(float));
        memcpy(&max_bias, (const float *) node->op_params + 1, sizeof(float));
    }

    ggml_et_softmax_params params;
    params.src0 = *node->src[0];                       // F32 input tensor
    if (node->src[1]) {
        params.src1 = *node->src[1];                   // F32 mask tensor
    } else {
        memset(&params.src1, 0, sizeof(params.src1));  // Zero if no mask
    }
    if (node->src[2]) {
        params.src2 = *node->src[2];                   // F32 sinks tensor
    } else {
        memset(&params.src2, 0, sizeof(params.src2));  // Zero if no sinks
    }
    params.dst      = *node;                           // F32 output tensor
    params.scale    = scale;                           // Scale factor
    params.max_bias = max_bias;                        // ALiBi bias

    // Phase 1: Initialize CPU comparison context and copy source buffers (before ET kernel)
    ggml_et_cpu_compare_ctx cpu_cmp_ctx;
    bool                    cpu_comparison_active = false;
    if (softmax_cpu_compare_config.enabled) {
        if (ggml_et_cpu_compare_init_pre(&cpu_cmp_ctx, node, GGML_OP_SOFT_MAX)) {
            cpu_comparison_active = true;
        } else {
            GGML_LOG_WARN("ET: Failed to initialize CPU comparison for SOFTMAX operation\n");
        }
    }

    bool kernel_result = ggml_et_launch_kernel(dev_ctx, kernel_name, &params, sizeof(params), 0xFFFFFFFF);

    // Phase 2: Execute CPU computation and compare with ET result (after ET kernel)
    if (cpu_comparison_active) {
        if (!ggml_et_cpu_compare_compute_and_check(&cpu_cmp_ctx, node, &softmax_cpu_compare_config)) {
            GGML_LOG_WARN("ET: CPU comparison failed for SOFTMAX operation\n");
        }
        ggml_et_cpu_compare_free(&cpu_cmp_ctx);
    }

    ET_PERF_END_EXT("SOFTMAX", kernel_name, node, "scale=%.6f|max_bias=%.6f|has_mask=%s", (double) scale,
                    (double) max_bias, node->src[1] ? "yes" : "no");
    return kernel_result;
}

bool ggml_et_op_flash_attn_ext(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    ET_PERF_START();

    if (!dev_ctx || !node) {
        GGML_LOG_ERROR("ET: Invalid parameters for FLASH_ATTN_EXT operation\n");
        return false;
    }

    if (!node->src[0] || !node->src[1] || !node->src[2]) {
        GGML_LOG_ERROR("ET: FLASH_ATTN_EXT operation missing required inputs\n");
        return false;
    }

    if (node->type != GGML_TYPE_F32 || node->src[0]->type != GGML_TYPE_F32) {
        GGML_LOG_ERROR("ET: FLASH_ATTN_EXT requires F32 Q and dst, got dst=%s q=%s\n", ggml_type_name(node->type),
                       ggml_type_name(node->src[0]->type));
        return false;
    }

    // K and V can be F16 or F32
    if ((node->src[1]->type != GGML_TYPE_F32 && node->src[1]->type != GGML_TYPE_F16) ||
        (node->src[2]->type != GGML_TYPE_F32 && node->src[2]->type != GGML_TYPE_F16)) {
        GGML_LOG_ERROR("ET: FLASH_ATTN_EXT K/V must be F16 or F32, got k=%s v=%s\n", ggml_type_name(node->src[1]->type),
                       ggml_type_name(node->src[2]->type));
        return false;
    }

    if (node->src[4] != nullptr) {
        GGML_LOG_ERROR("ET: FLASH_ATTN_EXT baseline kernel does not support sinks\n");
        return false;
    }

    // Mask is optional; if present must be F16 or F32
    if (node->src[3] != nullptr && node->src[3]->type != GGML_TYPE_F32 && node->src[3]->type != GGML_TYPE_F16) {
        GGML_LOG_ERROR("ET: FLASH_ATTN_EXT mask must be F16 or F32, got %s\n", ggml_type_name(node->src[3]->type));
        return false;
    }

    // Q and dst must be row-contiguous F32
    if (!ggml_is_contiguous_rows(node) || !ggml_is_contiguous_rows(node->src[0])) {
        GGML_LOG_ERROR("ET: FLASH_ATTN_EXT requires row-contiguous Q and dst\n");
        return false;
    }

    if (node->nb[0] != sizeof(float) || node->src[0]->nb[0] != sizeof(float)) {
        GGML_LOG_ERROR("ET: FLASH_ATTN_EXT requires contiguous F32 rows for Q and dst\n");
        return false;
    }

    // K/V must have element-sized stride in dim 0
    const size_t k_elem = node->src[1]->type == GGML_TYPE_F16 ? 2 : 4;
    const size_t v_elem = node->src[2]->type == GGML_TYPE_F16 ? 2 : 4;
    if (node->src[1]->nb[0] != k_elem || node->src[2]->nb[0] != v_elem) {
        GGML_LOG_ERROR("ET: FLASH_ATTN_EXT K/V must have element-sized stride in dim 0\n");
        return false;
    }

    float scale         = 1.0f;
    float max_bias      = 0.0f;
    float logit_softcap = 0.0f;
    memcpy(&scale, (const float *) node->op_params + 0, sizeof(scale));
    memcpy(&max_bias, (const float *) node->op_params + 1, sizeof(max_bias));
    memcpy(&logit_softcap, (const float *) node->op_params + 2, sizeof(logit_softcap));

    if (max_bias != 0.0f || logit_softcap != 0.0f) {
        GGML_LOG_ERROR("ET: FLASH_ATTN_EXT baseline kernel does not support max_bias or logit_softcap\n");
        return false;
    }

    const ggml_prec prec = ggml_flash_attn_ext_get_prec(node);
    if (prec != GGML_PREC_F32 && prec != GGML_PREC_DEFAULT) {
        GGML_LOG_ERROR("ET: FLASH_ATTN_EXT baseline kernel only supports F32 precision\n");
        return false;
    }

    // dk must match between Q and K; dv must match between V and dst
    if (node->src[0]->ne[0] != node->src[1]->ne[0]) {
        GGML_LOG_ERROR("ET: FLASH_ATTN_EXT dk mismatch: Q=%lld K=%lld\n", (long long) node->src[0]->ne[0],
                       (long long) node->src[1]->ne[0]);
        return false;
    }

    if (node->src[2]->ne[0] != node->ne[0]) {
        GGML_LOG_ERROR("ET: FLASH_ATTN_EXT dv mismatch: V=%lld dst=%lld\n", (long long) node->src[2]->ne[0],
                       (long long) node->ne[0]);
        return false;
    }

    if (node->src[2]->ne[0] > 512) {
        GGML_LOG_ERROR("ET: FLASH_ATTN_EXT dv=%lld exceeds maximum 512\n", (long long) node->src[2]->ne[0]);
        return false;
    }

    if (node->src[0]->ne[0] > 512) {
        GGML_LOG_ERROR("ET: FLASH_ATTN_EXT dk=%lld exceeds maximum 512\n", (long long) node->src[0]->ne[0]);
        return false;
    }

    // GQA: n_head_q must be a multiple of n_head_kv
    const int64_t nhq = node->src[0]->ne[2];
    const int64_t nhk = node->src[1]->ne[2];
    if (nhq % nhk != 0) {
        GGML_LOG_ERROR("ET: FLASH_ATTN_EXT n_head_q (%lld) not divisible by n_head_kv (%lld)\n", (long long) nhq,
                       (long long) nhk);
        return false;
    }

    // K and V must have matching sequence length, heads, and batch dims
    if (node->src[1]->ne[1] != node->src[2]->ne[1] || node->src[1]->ne[2] != node->src[2]->ne[2] ||
        node->src[1]->ne[3] != node->src[2]->ne[3]) {
        GGML_LOG_ERROR("ET: FLASH_ATTN_EXT K/V shape mismatch\n");
        return false;
    }

    // dst layout checks: [dv, nhq, nq, no]
    if (node->src[0]->ne[1] != node->ne[2] || node->src[0]->ne[2] != node->ne[1] ||
        node->src[0]->ne[3] != node->ne[3]) {
        GGML_LOG_ERROR("ET: FLASH_ATTN_EXT dst shape mismatch\n");
        return false;
    }

    // Batch dims: Q batch must match K batch
    if (node->src[0]->ne[3] != node->src[1]->ne[3]) {
        GGML_LOG_ERROR("ET: FLASH_ATTN_EXT batch dimension mismatch\n");
        return false;
    }

    ggml_et_flash_attn_ext_params params;
    memset(&params, 0, sizeof(params));
    params.src0 = *node->src[0];
    params.src1 = *node->src[1];
    params.src2 = *node->src[2];
    if (node->src[3] != nullptr) {
        params.mask     = *node->src[3];
        params.has_mask = 1;
    }
    params.dst   = *node;
    params.scale = scale;

    // Use matrix engine kernel when K/V are F16 and dk is a multiple of 32
    const char * kernel_name;
    if (node->src[1]->type == GGML_TYPE_F16 && node->src[2]->type == GGML_TYPE_F16 && (node->src[0]->ne[0] % 32) == 0) {
        kernel_name = "flash_attn_ext_f16_me";
    } else {
        kernel_name = "flash_attn_ext_f32";
    }

    const bool kernel_result = ggml_et_launch_kernel(dev_ctx, kernel_name, &params, sizeof(params), 0xFFFFFFFF);

    ET_PERF_END_EXT("FLASH_ATTN_EXT", kernel_name, node, "scale=%.6f", (double) scale);
    return kernel_result;
}

bool ggml_et_op_get_rows(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    ET_PERF_START();

    if (!dev_ctx || !node) {
        GGML_LOG_ERROR("ET: Invalid parameters for GET_ROWS operation\n");
        return false;
    }

    if (!node->src[0] || !node->src[1]) {
        GGML_LOG_ERROR("ET: GET_ROWS operation missing required inputs\n");
        return false;
    }

    const char * kernel_name;

    if (node->type == GGML_TYPE_F32 && node->src[1]->type == GGML_TYPE_I32 &&
        (node->src[0]->type == GGML_TYPE_F32 || node->src[0]->type == GGML_TYPE_F16 ||
         node->src[0]->type == GGML_TYPE_Q4_0 || node->src[0]->type == GGML_TYPE_Q8_0 ||
         node->src[0]->type == GGML_TYPE_Q4_K)) {
        kernel_name = "get_rows_f32";

    } else {
        GGML_LOG_ERROR("ET: GET_ROWS operation with unsupported types: dst=%s src0=%s src1=%s\n",
                       ggml_type_name(node->type), ggml_type_name(node->src[0]->type),
                       ggml_type_name(node->src[1]->type));
        return false;
    }

    // Validate contiguity requirements
    if (!ggml_is_contiguous(node)) {
        GGML_LOG_ERROR("ET: GET_ROWS operation requires contiguous destination tensor\n");
        return false;
    }

    if (!ggml_is_contiguous(node->src[0])) {
        GGML_LOG_ERROR("ET: GET_ROWS operation requires contiguous data tensor\n");
        return false;
    }

    if (!ggml_is_contiguous(node->src[1])) {
        GGML_LOG_ERROR("ET: GET_ROWS operation requires contiguous indices tensor\n");
        return false;
    }

    // Validate dimension constraints from ggml implementation
    if (node->src[0]->ne[2] != node->src[1]->ne[1] || node->src[1]->ne[3] != 1) {
        GGML_LOG_ERROR(
            "ET: GET_ROWS operation dimension constraint failed: src0.ne[2]=%lld != src1.ne[1]=%lld or src1.ne[3]=%lld "
            "!= 1\n",
            (long long) node->src[0]->ne[2], (long long) node->src[1]->ne[1], (long long) node->src[1]->ne[3]);
        return false;
    }

    ggml_et_get_rows_params params;
    params.src0 = *node->src[0];  // Data tensor (F32 or Q8_0)
    params.src1 = *node->src[1];  // Indices tensor (I32)
    params.dst  = *node;          // Output tensor (F32)

    // Phase 1: Initialize CPU comparison context and copy source buffers (before ET kernel)
    ggml_et_cpu_compare_ctx cpu_cmp_ctx;
    bool                    cpu_comparison_active = false;
    if (get_rows_cpu_compare_config.enabled) {
        if (ggml_et_cpu_compare_init_pre(&cpu_cmp_ctx, node, GGML_OP_GET_ROWS)) {
            cpu_comparison_active = true;
        } else {
            GGML_LOG_WARN("ET: Failed to initialize CPU comparison for GET_ROWS operation\n");
        }
    }

    bool kernel_result = ggml_et_launch_kernel(dev_ctx, kernel_name, &params, sizeof(params), 0xFFFFFFFF);

    // Phase 2: Execute CPU computation and compare with ET result (after ET kernel)
    if (cpu_comparison_active) {
        if (!ggml_et_cpu_compare_compute_and_check(&cpu_cmp_ctx, node, &get_rows_cpu_compare_config)) {
            GGML_LOG_WARN("ET: CPU comparison failed for GET_ROWS operation\n");
        }
        ggml_et_cpu_compare_free(&cpu_cmp_ctx);
    }

    ET_PERF_END("GET_ROWS", kernel_name, node);
    return kernel_result;
}

bool ggml_et_op_cont(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    ET_PERF_START();

    // Validate source tensor exists
    if (!node->src[0]) {
        GGML_LOG_ERROR("ET: CONT operation missing source tensor\n");
        return false;
    }

    // Validate types match (input and output must be same type)
    if (node->type != node->src[0]->type) {
        GGML_LOG_ERROR("ET: CONT operation type mismatch: src=%s dst=%s\n", ggml_type_name(node->src[0]->type),
                       ggml_type_name(node->type));
        return false;
    }

    // Validate supported types
    if (node->type != GGML_TYPE_F32 && node->type != GGML_TYPE_F16) {
        GGML_LOG_ERROR("ET: CONT operation unsupported type: %s (only F32 and F16 supported)\n",
                       ggml_type_name(node->type));
        return false;
    }

    // Validate contiguity - output must be contiguous, input can be non-contiguous
    if (!ggml_is_contiguous(node)) {
        GGML_LOG_ERROR("ET: CONT operation requires contiguous output tensor\n");
        return false;
    }

    // Select kernel based on type
    const char * kernel_name;
    if (node->type == GGML_TYPE_F32) {
        kernel_name = "cont_f32";
    } else if (node->type == GGML_TYPE_F16) {
        kernel_name = "cont_f16";
    } else {
        GGML_LOG_ERROR("ET: CONT operation with unsupported type: %s\n", ggml_type_name(node->type));
        return false;
    }

    ggml_et_cont_params params;
    params.src0 = *node->src[0];  // Input tensor (potentially non-contiguous)
    params.dst  = *node;          // Output tensor (contiguous)

    // Phase 1: Initialize CPU comparison context and copy source buffers (before ET kernel)
    ggml_et_cpu_compare_ctx cpu_cmp_ctx;
    bool                    cpu_comparison_active = false;
    if (cont_cpu_compare_config.enabled) {
        if (ggml_et_cpu_compare_init_pre(&cpu_cmp_ctx, node, GGML_OP_CONT)) {
            cpu_comparison_active = true;
        } else {
            GGML_LOG_WARN("ET: Failed to initialize CPU comparison for CONT operation\n");
        }
    }

    bool kernel_result = ggml_et_launch_kernel(dev_ctx, kernel_name, &params, sizeof(params), 0xFFFFFFFF);

    // Phase 2: Execute CPU computation and compare with ET result (after ET kernel)
    if (cpu_comparison_active) {
        if (!ggml_et_cpu_compare_compute_and_check(&cpu_cmp_ctx, node, &cont_cpu_compare_config)) {
            GGML_LOG_WARN("ET: CPU comparison failed for CONT operation\n");
        }
        ggml_et_cpu_compare_free(&cpu_cmp_ctx);
    }

    ET_PERF_END("CONT", kernel_name, node);
    return kernel_result;
}

bool ggml_et_op_cumsum(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    ET_PERF_START();

    if (!dev_ctx || !node || !node->src[0]) {
        GGML_LOG_ERROR("ET: Invalid parameters for CUMSUM operation\n");
        return false;
    }

    if (node->type != GGML_TYPE_F32 || node->src[0]->type != GGML_TYPE_F32) {
        GGML_LOG_ERROR("ET: CUMSUM operation with unsupported types: dst=%s src0=%s\n", ggml_type_name(node->type),
                       ggml_type_name(node->src[0]->type));
        return false;
    }

    const char * kernel_name = "cumsum_f32";

    ggml_et_cumsum_params params;
    params.src0 = *node->src[0];
    params.dst  = *node;

    ggml_et_cpu_compare_ctx cpu_cmp_ctx;
    bool                    cpu_comparison_active = false;
    if (cumsum_cpu_compare_config.enabled) {
        if (ggml_et_cpu_compare_init_pre(&cpu_cmp_ctx, node, GGML_OP_CUMSUM)) {
            cpu_comparison_active = true;
        } else {
            GGML_LOG_WARN("ET: Failed to initialize CPU comparison for CUMSUM operation\n");
        }
    }

    bool kernel_result = ggml_et_launch_kernel(dev_ctx, kernel_name, &params, sizeof(params), 0xFFFFFFFF);

    if (cpu_comparison_active) {
        if (!ggml_et_cpu_compare_compute_and_check(&cpu_cmp_ctx, node, &cumsum_cpu_compare_config)) {
            GGML_LOG_WARN("ET: CPU comparison failed for CUMSUM operation\n");
        }
        ggml_et_cpu_compare_free(&cpu_cmp_ctx);
    }

    ET_PERF_END("CUMSUM", kernel_name, node);
    return kernel_result;
}

bool ggml_et_op_cpy(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    ET_PERF_START();

    // CPY copies data from src[0] into the layout of dst (which matches src[1])
    // For same-type with contiguous dst, this is identical to CONT
    if (!node->src[0]) {
        GGML_LOG_ERROR("ET: CPY operation missing source tensor\n");
        return false;
    }

    // Scalar / zero-element special path: if any dimension is 0, nothing to copy
    const int64_t nelements = node->ne[0] * node->ne[1] * node->ne[2] * node->ne[3];
    if (nelements == 0) {
        GGML_LOG_DEBUG("ET: CPY no-op (zero elements): ne=[%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64 "]\n",
                       node->ne[0], node->ne[1], node->ne[2], node->ne[3]);
        ET_PERF_END("CPY", "noop", node);
        return true;
    }

    // Only F32 and F16 supported for dst
    if (node->type != GGML_TYPE_F32 && node->type != GGML_TYPE_F16) {
        GGML_LOG_ERROR("ET: CPY unsupported dst type: %s\n", ggml_type_name(node->type));
        return false;
    }

    // Select kernel based on src/dst type combination
    const char * kernel_name;
    if (node->src[0]->type == GGML_TYPE_F32 && node->type == GGML_TYPE_F32) {
        kernel_name = "cont_f32";
    } else if (node->src[0]->type == GGML_TYPE_F16 && node->type == GGML_TYPE_F16) {
        kernel_name = "cont_f16";
    } else if (node->src[0]->type == GGML_TYPE_F32 && node->type == GGML_TYPE_F16) {
        kernel_name = "cpy_f32_f16";
    } else {
        GGML_LOG_ERROR("ET: CPY unsupported type combination: src=%s dst=%s\n", ggml_type_name(node->src[0]->type),
                       ggml_type_name(node->type));
        return false;
    }

    ggml_et_cont_params params;
    params.src0 = *node->src[0];
    params.dst  = *node;

    // CPU comparison for debugging
    ggml_et_cpu_compare_ctx cpu_cmp_ctx;
    bool                    cpu_comparison_active = false;
    if (cont_cpu_compare_config.enabled) {
        if (ggml_et_cpu_compare_init_pre(&cpu_cmp_ctx, node, GGML_OP_CPY)) {
            cpu_comparison_active = true;
        } else {
            GGML_LOG_WARN("ET: Failed to initialize CPU comparison for CPY operation\n");
        }
    }

    bool kernel_result = ggml_et_launch_kernel(dev_ctx, kernel_name, &params, sizeof(params), 0xFFFFFFFF);

    if (cpu_comparison_active) {
        if (!ggml_et_cpu_compare_compute_and_check(&cpu_cmp_ctx, node, &cont_cpu_compare_config)) {
            GGML_LOG_WARN("ET: CPU comparison failed for CPY operation\n");
        }
        ggml_et_cpu_compare_free(&cpu_cmp_ctx);
    }

    ET_PERF_END("CPY", kernel_name, node);
    return kernel_result;
}

bool ggml_et_op_concat(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    ET_PERF_START();

    if (!dev_ctx || !node) {
        GGML_LOG_ERROR("ET: Invalid parameters for CONCAT operation\n");
        return false;
    }

    if (!node->src[0] || !node->src[1]) {
        GGML_LOG_ERROR("ET: CONCAT operation missing required inputs\n");
        return false;
    }

    const char * kernel_name;

    if (node->type == GGML_TYPE_F32 && node->src[0]->type == GGML_TYPE_F32 && node->src[1]->type == GGML_TYPE_F32) {
        kernel_name = "concat_f32";

    } else {
        GGML_LOG_ERROR("ET: CONCAT operation with unsupported types: dst=%s src0=%s src1=%s\n",
                       ggml_type_name(node->type), ggml_type_name(node->src[0]->type),
                       ggml_type_name(node->src[1]->type));
        return false;
    }

    int32_t dim;
    memcpy(&dim, node->op_params, sizeof(int32_t));

    ggml_et_concat_params params;
    params.src0 = *node->src[0];
    params.src1 = *node->src[1];
    params.dst  = *node;
    params.dim  = dim;

    // Phase 1: Initialize CPU comparison context and copy source buffers (before ET kernel)
    ggml_et_cpu_compare_ctx cpu_cmp_ctx;
    bool                    cpu_comparison_active = false;
    if (concat_cpu_compare_config.enabled) {
        if (ggml_et_cpu_compare_init_pre(&cpu_cmp_ctx, node, GGML_OP_CONCAT)) {
            cpu_comparison_active = true;
        } else {
            GGML_LOG_WARN("ET: Failed to initialize CPU comparison for CONCAT operation\n");
        }
    }

    bool kernel_result = ggml_et_launch_kernel(dev_ctx, kernel_name, &params, sizeof(params), 0xFFFFFFFF);

    // Phase 2: Execute CPU computation and compare with ET result (after ET kernel)
    if (cpu_comparison_active) {
        if (!ggml_et_cpu_compare_compute_and_check(&cpu_cmp_ctx, node, &concat_cpu_compare_config)) {
            GGML_LOG_WARN("ET: CPU comparison failed for CONCAT operation\n");
        }
        ggml_et_cpu_compare_free(&cpu_cmp_ctx);
    }

    ET_PERF_END_EXT("CONCAT", kernel_name, node, "dim=%d", dim);
    return kernel_result;
}

bool ggml_et_op_repeat(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    ET_PERF_START();

    if (!dev_ctx || !node) {
        GGML_LOG_ERROR("ET: Invalid parameters for REPEAT operation\n");
        return false;
    }

    if (!node->src[0]) {
        GGML_LOG_ERROR("ET: REPEAT operation missing required input\n");
        return false;
    }

    const char * kernel_name;

    if (node->type == GGML_TYPE_F32 && node->src[0]->type == GGML_TYPE_F32) {
        // No-op REPEAT (every repeat factor is 1): the output is just a copy
        // of the input. Route to cont_f32, whose contiguous fast path handles
        // arbitrary sizes (including those rejected by repeat_f32's gate,
        // e.g. ne[0]=1).
        if (ggml_are_same_shape(node->src[0], node)) {
            kernel_name = "cont_f32";
        } else {
            kernel_name = "repeat_f32";
        }

    } else {
        GGML_LOG_ERROR("ET: REPEAT operation with unsupported types: dst=%s src0=%s\n", ggml_type_name(node->type),
                       ggml_type_name(node->src[0]->type));
        return false;
    }

    // ggml_et_cont_params and ggml_et_repeat_params have identical layouts
    // (just src0 + dst), so the same payload works for either kernel.
    ggml_et_repeat_params params;
    params.src0 = *node->src[0];
    params.dst  = *node;

    // Phase 1: Initialize CPU comparison context and copy source buffers (before ET kernel)
    ggml_et_cpu_compare_ctx cpu_cmp_ctx;
    bool                    cpu_comparison_active = false;
    if (repeat_cpu_compare_config.enabled) {
        if (ggml_et_cpu_compare_init_pre(&cpu_cmp_ctx, node, GGML_OP_REPEAT)) {
            cpu_comparison_active = true;
        } else {
            GGML_LOG_WARN("ET: Failed to initialize CPU comparison for REPEAT operation\n");
        }
    }

    bool kernel_result = ggml_et_launch_kernel(dev_ctx, kernel_name, &params, sizeof(params), 0xFFFFFFFF);

    // Phase 2: Execute CPU computation and compare with ET result (after ET kernel)
    if (cpu_comparison_active) {
        if (!ggml_et_cpu_compare_compute_and_check(&cpu_cmp_ctx, node, &repeat_cpu_compare_config)) {
            GGML_LOG_WARN("ET: CPU comparison failed for REPEAT operation\n");
        }
        ggml_et_cpu_compare_free(&cpu_cmp_ctx);
    }

    ET_PERF_END("REPEAT", kernel_name, node);
    return kernel_result;
}

bool ggml_et_op_ssm_conv(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    ET_PERF_START();

    if (!dev_ctx || !node || !node->src[0] || !node->src[1]) {
        GGML_LOG_ERROR("ET: Invalid parameters for SSM_CONV operation\n");
        return false;
    }

    if (node->type != GGML_TYPE_F32 || node->src[0]->type != GGML_TYPE_F32 || node->src[1]->type != GGML_TYPE_F32) {
        GGML_LOG_ERROR("ET: SSM_CONV operation with unsupported types: dst=%s src0=%s src1=%s\n",
                       ggml_type_name(node->type), ggml_type_name(node->src[0]->type),
                       ggml_type_name(node->src[1]->type));
        return false;
    }

    const char * kernel_name = "ssm_conv_f32";

    ggml_et_ssm_conv_params params;
    params.src0 = *node->src[0];
    params.src1 = *node->src[1];
    params.dst  = *node;

    ggml_et_cpu_compare_ctx cpu_cmp_ctx;
    bool                    cpu_comparison_active = false;
    if (ssm_conv_cpu_compare_config.enabled) {
        if (ggml_et_cpu_compare_init_pre(&cpu_cmp_ctx, node, GGML_OP_SSM_CONV)) {
            cpu_comparison_active = true;
        } else {
            GGML_LOG_WARN("ET: Failed to initialize CPU comparison for SSM_CONV operation\n");
        }
    }

    bool kernel_result = ggml_et_launch_kernel(dev_ctx, kernel_name, &params, sizeof(params), 0xFFFFFFFF);

    if (cpu_comparison_active) {
        if (!ggml_et_cpu_compare_compute_and_check(&cpu_cmp_ctx, node, &ssm_conv_cpu_compare_config)) {
            GGML_LOG_WARN("ET: CPU comparison failed for SSM_CONV operation\n");
        }
        ggml_et_cpu_compare_free(&cpu_cmp_ctx);
    }

    ET_PERF_END("SSM_CONV", kernel_name, node);
    return kernel_result;
}

bool ggml_et_op_ssm_scan(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    ET_PERF_START();

    if (!dev_ctx || !node) {
        GGML_LOG_ERROR("ET: Invalid parameters for SSM_SCAN operation\n");
        return false;
    }

    for (int i = 0; i < 7; ++i) {
        if (!node->src[i]) {
            GGML_LOG_ERROR("ET: SSM_SCAN missing required input %d\n", i);
            return false;
        }
    }

    if (node->type != GGML_TYPE_F32 || node->src[0]->type != GGML_TYPE_F32 || node->src[1]->type != GGML_TYPE_F32 ||
        node->src[2]->type != GGML_TYPE_F32 || node->src[3]->type != GGML_TYPE_F32 ||
        node->src[4]->type != GGML_TYPE_F32 || node->src[5]->type != GGML_TYPE_F32 ||
        node->src[6]->type != GGML_TYPE_I32) {
        GGML_LOG_ERROR("ET: SSM_SCAN operation with unsupported types\n");
        return false;
    }

    ggml_et_ssm_scan_params params;
    params.src0 = *node->src[0];
    params.src1 = *node->src[1];
    params.src2 = *node->src[2];
    params.src3 = *node->src[3];
    params.src4 = *node->src[4];
    params.src5 = *node->src[5];
    params.src6 = *node->src[6];
    params.dst  = *node;
    params.K    = ggml_get_op_params_i32(node, 0);

    bool kernel_result = ggml_et_launch_kernel(dev_ctx, "ssm_scan_f32", &params, sizeof(params), 0xFFFFFFFF);

    ET_PERF_END("SSM_SCAN", "ssm_scan_f32", node);
    return kernel_result;
}

bool ggml_et_op_rwkv_wkv6(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    ET_PERF_START();

    if (!dev_ctx || !node) {
        GGML_LOG_ERROR("ET: Invalid parameters for RWKV_WKV6 operation\n");
        return false;
    }

    // Validate all 6 source tensors exist
    for (int i = 0; i <= 5; i++) {
        if (!node->src[i]) {
            GGML_LOG_ERROR("ET: RWKV_WKV6 operation missing src[%d]\n", i);
            return false;
        }
    }

    if (node->type != GGML_TYPE_F32) {
        GGML_LOG_ERROR("ET: RWKV_WKV6 only supports F32, got %s\n", ggml_type_name(node->type));
        return false;
    }

    const char * kernel_name = "rwkv_wkv6_f32";

    const int64_t S      = node->src[0]->ne[0];  // head_size
    const int64_t H      = node->src[0]->ne[1];  // num heads
    const int64_t T      = node->src[1]->ne[2];  // num tokens
    const int64_t n_seqs = node->src[5]->ne[1];  // num sequences
    const int64_t C      = S * H;

    ggml_et_rwkv_wkv6_params params;
    params.k        = (float *) node->src[0]->data;
    params.v        = (float *) node->src[1]->data;
    params.r        = (float *) node->src[2]->data;
    params.tf       = (float *) node->src[3]->data;
    params.td       = (float *) node->src[4]->data;
    params.state_in = (float *) node->src[5]->data;
    params.dst      = (float *) node->data;
    params.C        = (int32_t) C;
    params.H        = (int32_t) H;
    params.S        = (int32_t) S;
    params.T        = (int32_t) T;
    params.n_seqs   = (int32_t) n_seqs;

    // Phase 1: Initialize CPU comparison context
    ggml_et_cpu_compare_ctx cpu_cmp_ctx;
    bool                    cpu_comparison_active = false;
    if (rwkv_wkv6_cpu_compare_config.enabled) {
        if (ggml_et_cpu_compare_init_pre(&cpu_cmp_ctx, node, GGML_OP_RWKV_WKV6)) {
            cpu_comparison_active = true;
        } else {
            GGML_LOG_WARN("ET: Failed to initialize CPU comparison for RWKV_WKV6 operation\n");
        }
    }

    bool kernel_result = ggml_et_launch_kernel(dev_ctx, kernel_name, &params, sizeof(params), 0xFFFFFFFF);

    // Phase 2: Execute CPU computation and compare
    if (cpu_comparison_active) {
        if (!ggml_et_cpu_compare_compute_and_check(&cpu_cmp_ctx, node, &rwkv_wkv6_cpu_compare_config)) {
            GGML_LOG_WARN("ET: CPU comparison failed for RWKV_WKV6 operation\n");
        }
        ggml_et_cpu_compare_free(&cpu_cmp_ctx);
    }

    ET_PERF_END_EXT("RWKV_WKV6", kernel_name, node, "S=%d H=%d T=%d n_seqs=%d", (int) S, (int) H, (int) T,
                    (int) n_seqs);
    return kernel_result;
}

bool ggml_et_op_rwkv_wkv7(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    ET_PERF_START();

    if (!dev_ctx || !node) {
        GGML_LOG_ERROR("ET: Invalid parameters for RWKV_WKV7 operation\n");
        return false;
    }

    // Validate all 7 source tensors exist
    for (int i = 0; i <= 6; i++) {
        if (!node->src[i]) {
            GGML_LOG_ERROR("ET: RWKV_WKV7 operation missing src[%d]\n", i);
            return false;
        }
    }

    if (node->type != GGML_TYPE_F32) {
        GGML_LOG_ERROR("ET: RWKV_WKV7 only supports F32, got %s\n", ggml_type_name(node->type));
        return false;
    }

    const char * kernel_name = "rwkv_wkv7_f32";

    const int64_t S      = node->src[2]->ne[0];  // head_size
    const int64_t H      = node->src[2]->ne[1];  // num heads
    const int64_t T      = node->src[1]->ne[2];  // num tokens
    const int64_t n_seqs = node->src[6]->ne[1];  // num sequences
    const int64_t C      = S * H;

    ggml_et_rwkv_wkv7_params params;
    params.r        = (float *) node->src[0]->data;
    params.w        = (float *) node->src[1]->data;
    params.k        = (float *) node->src[2]->data;
    params.v        = (float *) node->src[3]->data;
    params.a        = (float *) node->src[4]->data;
    params.b        = (float *) node->src[5]->data;
    params.state_in = (float *) node->src[6]->data;
    params.dst      = (float *) node->data;
    params.C        = (int32_t) C;
    params.H        = (int32_t) H;
    params.S        = (int32_t) S;
    params.T        = (int32_t) T;
    params.n_seqs   = (int32_t) n_seqs;

    // Phase 1: Initialize CPU comparison context
    ggml_et_cpu_compare_ctx cpu_cmp_ctx;
    bool                    cpu_comparison_active = false;
    if (rwkv_wkv7_cpu_compare_config.enabled) {
        if (ggml_et_cpu_compare_init_pre(&cpu_cmp_ctx, node, GGML_OP_RWKV_WKV7)) {
            cpu_comparison_active = true;
        } else {
            GGML_LOG_WARN("ET: Failed to initialize CPU comparison for RWKV_WKV7 operation\n");
        }
    }

    bool kernel_result = ggml_et_launch_kernel(dev_ctx, kernel_name, &params, sizeof(params), 0xFFFFFFFF);

    // Phase 2: Execute CPU computation and compare
    if (cpu_comparison_active) {
        if (!ggml_et_cpu_compare_compute_and_check(&cpu_cmp_ctx, node, &rwkv_wkv7_cpu_compare_config)) {
            GGML_LOG_WARN("ET: CPU comparison failed for RWKV_WKV7 operation\n");
        }
        ggml_et_cpu_compare_free(&cpu_cmp_ctx);
    }

    ET_PERF_END_EXT("RWKV_WKV7", kernel_name, node, "S=%d H=%d T=%d n_seqs=%d", (int) S, (int) H, (int) T,
                    (int) n_seqs);
    return kernel_result;
}

static ggml_et_cpu_compare_config gated_delta_net_cpu_compare_config = {
    /* .enabled = */ false,
    /* .use_cpu_result = */ false,
    /* .log_differences = */ true,
    /* .tolerance = */ 1e-4f,
    /* .max_log_elements = */ 4096
};

bool ggml_et_op_gated_delta_net(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    ET_PERF_START();

    if (!dev_ctx || !node) {
        GGML_LOG_ERROR("ET: Invalid parameters for GATED_DELTA_NET operation\n");
        return false;
    }

    // Validate all 6 source tensors exist
    for (int i = 0; i <= 5; i++) {
        if (!node->src[i]) {
            GGML_LOG_ERROR("ET: GATED_DELTA_NET operation missing src[%d]\n", i);
            return false;
        }
    }

    if (node->type != GGML_TYPE_F32) {
        GGML_LOG_ERROR("ET: GATED_DELTA_NET only supports F32, got %s\n", ggml_type_name(node->type));
        return false;
    }

    const char * kernel_name = "gated_delta_net_f32";

    const ggml_tensor * src_q     = node->src[0];
    const ggml_tensor * src_k     = node->src[1];
    const ggml_tensor * src_v     = node->src[2];
    const ggml_tensor * src_g     = node->src[3];
    const ggml_tensor * src_beta  = node->src[4];
    const ggml_tensor * src_state = node->src[5];

    const int64_t S_v      = src_v->ne[0];
    const int64_t H        = src_v->ne[1];
    const int64_t n_tokens = src_v->ne[2];
    const int64_t n_seqs   = src_v->ne[3];
    const int64_t H_q      = src_q->ne[1];
    const int64_t H_k      = src_k->ne[1];
    const int64_t n_seqs_q = src_q->ne[3];
    const int64_t n_seqs_k = src_k->ne[3];

    ggml_et_gated_delta_net_params params;
    params.q        = *src_q;
    params.k        = *src_k;
    params.v        = *src_v;
    params.g        = *src_g;
    params.beta     = *src_beta;
    params.state_in = *src_state;
    params.dst      = *node;
    params.S_v      = (int32_t) S_v;
    params.H        = (int32_t) H;
    params.H_q      = (int32_t) H_q;
    params.H_k      = (int32_t) H_k;
    params.n_tokens = (int32_t) n_tokens;
    params.n_seqs   = (int32_t) n_seqs;
    params.n_seqs_q = (int32_t) n_seqs_q;
    params.n_seqs_k = (int32_t) n_seqs_k;
    params.kda      = (src_g->ne[0] == S_v) ? 1 : 0;
    params.K        = ggml_get_op_params_i32(node, 0);
    params.scale    = 1.0f / sqrtf((float) S_v);

    // CPU comparison for debugging
    ggml_et_cpu_compare_ctx cpu_cmp_ctx;
    bool                    cpu_comparison_active = false;
    if (gated_delta_net_cpu_compare_config.enabled) {
        if (ggml_et_cpu_compare_init_pre(&cpu_cmp_ctx, node, GGML_OP_GATED_DELTA_NET)) {
            cpu_comparison_active = true;
        } else {
            GGML_LOG_WARN("ET: Failed to initialize CPU comparison for GATED_DELTA_NET operation\n");
        }
    }

    bool kernel_result = ggml_et_launch_kernel(dev_ctx, kernel_name, &params, sizeof(params), 0xFFFFFFFF);

    if (cpu_comparison_active) {
        if (!ggml_et_cpu_compare_compute_and_check(&cpu_cmp_ctx, node, &gated_delta_net_cpu_compare_config)) {
            GGML_LOG_WARN("ET: CPU comparison failed for GATED_DELTA_NET operation\n");
        }
        ggml_et_cpu_compare_free(&cpu_cmp_ctx);
    }

    ET_PERF_END_EXT("GATED_DELTA_NET", kernel_name, node, "S_v=%d H=%d n_tokens=%d n_seqs=%d kda=%d", (int) S_v,
                    (int) H, (int) n_tokens, (int) n_seqs, params.kda);
    return kernel_result;
}

bool ggml_et_op_set_rows(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    ET_PERF_START();

    if (!dev_ctx || !node) {
        GGML_LOG_ERROR("ET: Invalid parameters for SET_ROWS operation\n");
        return false;
    }

    if (!node->src[0] || !node->src[1] || !node->src[2]) {
        GGML_LOG_ERROR(
            "ET: SET_ROWS operation missing required inputs (needs src[0]=base, src[1]=indices, src[2]=data)\n");
        return false;
    }

    const char * kernel_name;

    // Support F32 data with I64 indices -> F32/F16 output (scatter operation)
    if (node->src[0]->type == GGML_TYPE_F32 && node->src[1]->type == GGML_TYPE_I64 &&
        (node->type == GGML_TYPE_F32 || node->type == GGML_TYPE_F16)) {
        if (node->type == GGML_TYPE_F32 || node->type == GGML_TYPE_F16) {
            kernel_name = "set_rows_f32";
        } else {
            GGML_LOG_ERROR("ET: SET_ROWS unsupported output type: %s\n", ggml_type_name(node->type));
            return false;
        }

    } else {
        GGML_LOG_ERROR("ET: SET_ROWS operation with unsupported types: dst=%s src0=%s src1=%s\n",
                       ggml_type_name(node->type), ggml_type_name(node->src[0]->type),
                       ggml_type_name(node->src[1]->type));
        return false;
    }

    // Validate contiguity requirements
    if (!ggml_is_contiguous_rows(node)) {
        GGML_LOG_ERROR("ET: SET_ROWS operation requires contiguous-rows destination tensor\n");
        return false;
    }

    if (!ggml_is_contiguous_rows(node->src[0])) {
        GGML_LOG_ERROR("ET: SET_ROWS operation requires contiguous-rows source tensor\n");
        return false;
    }

    if (!ggml_is_contiguous(node->src[1])) {
        GGML_LOG_ERROR("ET: SET_ROWS operation requires contiguous indices tensor\n");
        return false;
    }

    // Validate dimension constraints from ggml implementation
    if (!(node->ne[0] == node->src[0]->ne[0] &&              // same number of columns
          node->ne[2] == node->src[0]->ne[2] &&              // same batch size
          node->ne[3] == node->src[0]->ne[3] &&              // same outer dimension
          node->src[0]->ne[1] == node->src[1]->ne[0] &&      // src rows = index count
          node->src[0]->ne[2] % node->src[1]->ne[1] == 0 &&  // batch constraint
          node->src[0]->ne[3] % node->src[1]->ne[2] == 0 &&  // outer constraint
          node->src[1]->ne[3] == 1)) {                       // indices constraint
        GGML_LOG_ERROR("ET: SET_ROWS operation dimension constraint failed\n");
        return false;
    }

    ggml_et_set_rows_params params;
    params.src0 = *node->src[0];  // F32 source data tensor
    params.src1 = *node->src[1];  // I64 indices tensor
    params.dst  = *node;          // F32/F16 destination tensor

    // Phase 1: Initialize CPU comparison context and copy source buffers (before ET kernel)
    ggml_et_cpu_compare_ctx cpu_cmp_ctx;
    bool                    cpu_comparison_active = false;
    if (set_rows_cpu_compare_config.enabled) {
        if (ggml_et_cpu_compare_init_pre(&cpu_cmp_ctx, node, GGML_OP_SET_ROWS)) {
            cpu_comparison_active = true;
        } else {
            GGML_LOG_WARN("ET: Failed to initialize CPU comparison for SET_ROWS operation\n");
        }
    }

    bool kernel_result = ggml_et_launch_kernel(dev_ctx, kernel_name, &params, sizeof(params), 0xFFFFFFFF);

    // Phase 2: Execute CPU computation and compare with ET result (after ET kernel)
    if (cpu_comparison_active) {
        if (!ggml_et_cpu_compare_compute_and_check(&cpu_cmp_ctx, node, &set_rows_cpu_compare_config)) {
            GGML_LOG_WARN("ET: CPU comparison failed for SET_ROWS operation\n");
        }
        ggml_et_cpu_compare_free(&cpu_cmp_ctx);
    }

    ET_PERF_END("SET_ROWS", kernel_name, node);
    return kernel_result;
}

bool ggml_et_op_fill(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    ET_PERF_START();

    ggml_et_fill_params params;
    params.dst = *node;
    memcpy(&params.c, node->op_params, sizeof(float));

    bool kernel_result = ggml_et_launch_kernel(dev_ctx, "fill_f32", &params, sizeof(params), 0xFFFFFFFF);

    ET_PERF_END("FILL", "fill_f32", node);
    return kernel_result;
}

bool ggml_et_op_diag(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    ET_PERF_START();

    if (!node->src[0]) {
        GGML_LOG_ERROR("ET: DIAG operation missing source tensor\n");
        return false;
    }

    ggml_et_diag_params params;
    params.src0 = *node->src[0];
    params.dst  = *node;

    bool kernel_result = ggml_et_launch_kernel(dev_ctx, "diag_f32", &params, sizeof(params), 0xFFFFFFFF);

    ET_PERF_END("DIAG", "diag_f32", node);
    return kernel_result;
}

bool ggml_et_op_tri(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    ET_PERF_START();

    if (!node->src[0]) {
        GGML_LOG_ERROR("ET: TRI operation missing source tensor\n");
        return false;
    }

    ggml_et_tri_params params;
    params.src0 = *node->src[0];
    params.dst  = *node;
    memcpy(&params.tri_type, node->op_params, sizeof(int32_t));

    bool kernel_result = ggml_et_launch_kernel(dev_ctx, "tri_f32", &params, sizeof(params), 0xFFFFFFFF);

    ET_PERF_END("TRI", "tri_f32", node);
    return kernel_result;
}

bool ggml_et_op_solve_tri(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    ET_PERF_START();

    if (!node->src[0] || !node->src[1]) {
        GGML_LOG_ERROR("ET: SOLVE_TRI operation missing source tensor(s)\n");
        return false;
    }

    ggml_et_solve_tri_params params;
    params.src0 = *node->src[0];  // A (lower-triangular)
    params.src1 = *node->src[1];  // B (RHS)
    params.dst  = *node;          // X (solution)

    bool kernel_result = ggml_et_launch_kernel(dev_ctx, "solve_tri_f32", &params, sizeof(params), 0xFFFFFFFF);

    ET_PERF_END("SOLVE_TRI", "solve_tri_f32", node);
    return kernel_result;
}

bool ggml_et_op_set(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    ET_PERF_START();

    if (!node->src[0] || !node->src[1]) {
        GGML_LOG_ERROR("ET: SET operation missing source tensor(s)\n");
        return false;
    }

    const bool   inplace = (bool) ((const int32_t *) node->op_params)[4];
    const size_t offset  = ((const int32_t *) node->op_params)[3];
    const size_t nb1     = ((const int32_t *) node->op_params)[0];
    const size_t nb2     = ((const int32_t *) node->op_params)[1];
    const size_t nb3     = ((const int32_t *) node->op_params)[2];

    if (!inplace) {
        GGML_LOG_ERROR("ET: SET only supports inplace (inplace=%d)\n", inplace);
        return false;
    }

    if (node->type != GGML_TYPE_F32 || node->src[0]->type != GGML_TYPE_F32 || node->src[1]->type != GGML_TYPE_F32) {
        GGML_LOG_ERROR("ET: SET only supports F32 (dst=%s src0=%s src1=%s)\n", ggml_type_name(node->type),
                       ggml_type_name(node->src[0]->type), ggml_type_name(node->src[1]->type));
        return false;
    }

    if (!ggml_are_same_shape(node, node->src[0])) {
        GGML_LOG_ERROR("ET: SET requires same-shape src0 and dst\n");
        return false;
    }

    if (!ggml_is_contiguous(node) || !ggml_is_contiguous(node->src[0]) || !ggml_is_contiguous(node->src[1])) {
        GGML_LOG_ERROR("ET: SET requires contiguous dst, src0, and src1\n");
        return false;
    }

    ggml_et_set_params params;
    params.src1   = *node->src[1];
    params.dst    = *node;
    params.nb1    = (int32_t) nb1;
    params.nb2    = (int32_t) nb2;
    params.nb3    = (int32_t) nb3;
    params.offset = (int32_t) offset;

    bool kernel_result = ggml_et_launch_kernel(dev_ctx, "set_f32", &params, sizeof(params), 0xFFFFFFFF);

    ET_PERF_END("SET", "set_f32", node);
    return kernel_result;
}

bool ggml_et_op_pad(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node) {
    ET_PERF_START();

    if (!node->src[0]) {
        GGML_LOG_ERROR("ET: PAD operation missing source tensor\n");
        return false;
    }

    if (node->type != GGML_TYPE_F32 || node->src[0]->type != GGML_TYPE_F32) {
        GGML_LOG_ERROR("ET: PAD only supports F32 (src=%s dst=%s)\n", ggml_type_name(node->src[0]->type),
                       ggml_type_name(node->type));
        return false;
    }

    if (!ggml_is_contiguous(node)) {
        GGML_LOG_ERROR("ET: PAD requires contiguous output tensor\n");
        return false;
    }

    if (node->src[0]->nb[0] != sizeof(float)) {
        GGML_LOG_ERROR("ET: PAD requires element-contiguous src dim0 (nb[0]=%zu)\n", (size_t) node->src[0]->nb[0]);
        return false;
    }

    // Extract padding parameters from op_params
    const int32_t * op_params = (const int32_t *) node->op_params;

    ggml_et_pad_params params;
    params.src0  = *node->src[0];
    params.dst   = *node;
    params.lp[0] = op_params[0];
    params.rp[0] = op_params[1];
    params.lp[1] = op_params[2];
    params.rp[1] = op_params[3];
    params.lp[2] = op_params[4];
    params.rp[2] = op_params[5];
    params.lp[3] = op_params[6];
    params.rp[3] = op_params[7];

    // v1: no dim0 padding
    if (params.lp[0] != 0 || params.rp[0] != 0) {
        GGML_LOG_ERROR("ET: PAD dim0 padding not supported (lp0=%d rp0=%d)\n", params.lp[0], params.rp[0]);
        return false;
    }

    ggml_et_cpu_compare_ctx cpu_cmp_ctx;
    bool                    cpu_comparison_active = false;
    if (pad_cpu_compare_config.enabled) {
        if (ggml_et_cpu_compare_init_pre(&cpu_cmp_ctx, node, GGML_OP_PAD)) {
            cpu_comparison_active = true;
        } else {
            GGML_LOG_WARN("ET: Failed to initialize CPU comparison for PAD operation\n");
        }
    }

    bool kernel_result = ggml_et_launch_kernel(dev_ctx, "pad_f32", &params, sizeof(params), 0xFFFFFFFF);

    if (cpu_comparison_active) {
        if (!ggml_et_cpu_compare_compute_and_check(&cpu_cmp_ctx, node, &pad_cpu_compare_config)) {
            GGML_LOG_WARN("ET: CPU comparison failed for PAD operation\n");
        }
        ggml_et_cpu_compare_free(&cpu_cmp_ctx);
    }

    ET_PERF_END("PAD", "pad_f32", node);
    return kernel_result;
}