File size: 291,674 Bytes
9dc9d2f 4a3e194 9dc9d2f 4a3e194 9dc9d2f 4a3e194 9dc9d2f 4a3e194 9dc9d2f 4a3e194 9dc9d2f 4a3e194 9dc9d2f 4a3e194 9dc9d2f 4a3e194 9dc9d2f 4a3e194 9dc9d2f 4a3e194 9dc9d2f 4a3e194 9dc9d2f 4a3e194 9dc9d2f 4a3e194 9dc9d2f 4a3e194 9dc9d2f 4a3e194 9dc9d2f 4a3e194 9dc9d2f 4a3e194 9dc9d2f 4a3e194 9dc9d2f 4a3e194 9dc9d2f 4a3e194 9dc9d2f 4a3e194 9dc9d2f 4a3e194 9dc9d2f 4a3e194 9dc9d2f 4a3e194 9dc9d2f 4a3e194 9dc9d2f 4a3e194 9dc9d2f 4a3e194 9dc9d2f 4a3e194 9dc9d2f 4a3e194 9dc9d2f 4a3e194 9dc9d2f 4a3e194 9dc9d2f 4a3e194 9dc9d2f 4a3e194 9dc9d2f 4a3e194 9dc9d2f 4a3e194 9dc9d2f 4a3e194 9dc9d2f 4a3e194 9dc9d2f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 | """Evaluates the threshold computers: the fitness suite, every variant, and the CPU programs."""
from __future__ import annotations
import argparse
import json
import os
import sys
import time
from collections import defaultdict
from dataclasses import dataclass, field
from pathlib import Path
from typing import Callable, Dict, List, Optional, Tuple, Any
import torch
from safetensors import safe_open
MODEL_PATH = os.path.join(os.path.dirname(os.path.dirname(__file__)), "neural_computer.safetensors") # repo root; this module lives in src/
@dataclass
class CircuitResult:
"""Result for a single circuit test."""
name: str
passed: int
total: int
failures: List[Tuple] = field(default_factory=list)
@property
def success(self) -> bool:
return self.passed == self.total
@property
def rate(self) -> float:
return self.passed / self.total if self.total > 0 else 0.0
def heaviside(x: torch.Tensor) -> torch.Tensor:
"""Threshold activation: 1 if x >= 0, else 0."""
return (x >= 0).float()
def load_model(path: str = MODEL_PATH) -> Dict[str, torch.Tensor]:
"""Load model tensors from safetensors."""
with safe_open(path, framework='pt') as f:
return {name: f.get_tensor(name).float() for name in f.keys()}
def load_metadata(path: str = MODEL_PATH) -> Dict:
"""Load metadata from safetensors (includes signal_registry)."""
with safe_open(path, framework='pt') as f:
meta = f.metadata()
if meta and 'signal_registry' in meta:
return {'signal_registry': json.loads(meta['signal_registry'])}
return {'signal_registry': {}}
def get_manifest(tensors: Dict[str, torch.Tensor]) -> Dict[str, int]:
"""Extract manifest values from tensors."""
return {
'data_bits': int(tensors.get('manifest.data_bits', torch.tensor([8.0])).item()),
'addr_bits': int(tensors.get('manifest.addr_bits',
tensors.get('manifest.pc_width', torch.tensor([16.0]))).item()),
'memory_bytes': int(tensors.get('manifest.memory_bytes', torch.tensor([65536.0])).item()),
'version': float(tensors.get('manifest.version', torch.tensor([1.0])).item()),
}
def create_population(
base_tensors: Dict[str, torch.Tensor],
pop_size: int,
device: str = 'cuda'
) -> Dict[str, torch.Tensor]:
"""Replicate base tensors for batched population evaluation."""
return {
name: tensor.unsqueeze(0).expand(pop_size, *tensor.shape).clone().to(device)
for name, tensor in base_tensors.items()
}
# =============================================================================
# CPU RUNTIME
# =============================================================================
FLAG_NAMES = ["Z", "N", "C", "V"]
CTRL_NAMES = ["HALT", "MEM_WE", "MEM_RE", "RESERVED"]
PC_BITS = 16
IR_BITS = 16
REG_BITS = 8
REG_COUNT = 4
FLAG_BITS = 4
SP_BITS = 16
CTRL_BITS = 4
MEM_BYTES = 65536
MEM_BITS = MEM_BYTES * 8
STATE_BITS = PC_BITS + IR_BITS + (REG_BITS * REG_COUNT) + FLAG_BITS + SP_BITS + CTRL_BITS + MEM_BITS
def int_to_bits(value: int, width: int) -> List[int]:
return [(value >> (width - 1 - i)) & 1 for i in range(width)]
def bits_to_int(bits: List[int]) -> int:
value = 0
for bit in bits:
value = (value << 1) | int(bit)
return value
def bits_msb_to_lsb(bits: List[int]) -> List[int]:
return list(reversed(bits))
@dataclass
class CPUState:
pc: int
ir: int
regs: List[int]
flags: List[int]
sp: int
ctrl: List[int]
mem: List[int]
def copy(self) -> 'CPUState':
return CPUState(
pc=int(self.pc),
ir=int(self.ir),
regs=[int(r) for r in self.regs],
flags=[int(f) for f in self.flags],
sp=int(self.sp),
ctrl=[int(c) for c in self.ctrl],
mem=[int(m) for m in self.mem],
)
def pack_state(state: CPUState) -> List[int]:
bits: List[int] = []
bits.extend(int_to_bits(state.pc, PC_BITS))
bits.extend(int_to_bits(state.ir, IR_BITS))
for reg in state.regs:
bits.extend(int_to_bits(reg, REG_BITS))
bits.extend([int(f) for f in state.flags])
bits.extend(int_to_bits(state.sp, SP_BITS))
bits.extend([int(c) for c in state.ctrl])
for byte in state.mem:
bits.extend(int_to_bits(byte, REG_BITS))
return bits
def unpack_state(bits: List[int]) -> CPUState:
if len(bits) != STATE_BITS:
raise ValueError(f"Expected {STATE_BITS} bits, got {len(bits)}")
idx = 0
pc = bits_to_int(bits[idx:idx + PC_BITS])
idx += PC_BITS
ir = bits_to_int(bits[idx:idx + IR_BITS])
idx += IR_BITS
regs = []
for _ in range(REG_COUNT):
regs.append(bits_to_int(bits[idx:idx + REG_BITS]))
idx += REG_BITS
flags = [int(b) for b in bits[idx:idx + FLAG_BITS]]
idx += FLAG_BITS
sp = bits_to_int(bits[idx:idx + SP_BITS])
idx += SP_BITS
ctrl = [int(b) for b in bits[idx:idx + CTRL_BITS]]
idx += CTRL_BITS
mem = []
for _ in range(MEM_BYTES):
mem.append(bits_to_int(bits[idx:idx + REG_BITS]))
idx += REG_BITS
return CPUState(pc=pc, ir=ir, regs=regs, flags=flags, sp=sp, ctrl=ctrl, mem=mem)
def decode_ir(ir: int) -> Tuple[int, int, int, int]:
opcode = (ir >> 12) & 0xF
rd = (ir >> 10) & 0x3
rs = (ir >> 8) & 0x3
imm8 = ir & 0xFF
return opcode, rd, rs, imm8
def flags_from_result(result: int, carry: int, overflow: int) -> Tuple[int, int, int, int]:
z = 1 if result == 0 else 0
n = 1 if (result & 0x80) else 0
c = 1 if carry else 0
v = 1 if overflow else 0
return z, n, c, v
def alu_add(a: int, b: int) -> Tuple[int, int, int]:
full = a + b
result = full & 0xFF
carry = 1 if full > 0xFF else 0
overflow = 1 if (((a ^ result) & (b ^ result)) & 0x80) else 0
return result, carry, overflow
def alu_sub(a: int, b: int) -> Tuple[int, int, int]:
full = (a - b) & 0x1FF
result = full & 0xFF
carry = 1 if a >= b else 0
overflow = 1 if (((a ^ b) & (a ^ result)) & 0x80) else 0
return result, carry, overflow
def ref_step(state: CPUState) -> CPUState:
"""Reference CPU cycle (pure Python arithmetic)."""
if state.ctrl[0] == 1:
return state.copy()
s = state.copy()
hi = s.mem[s.pc]
lo = s.mem[(s.pc + 1) & 0xFFFF]
s.ir = ((hi & 0xFF) << 8) | (lo & 0xFF)
next_pc = (s.pc + 2) & 0xFFFF
opcode, rd, rs, imm8 = decode_ir(s.ir)
a = s.regs[rd]
b = s.regs[rs]
addr16 = None
next_pc_ext = next_pc
if opcode in (0xA, 0xB, 0xC, 0xD, 0xE):
addr_hi = s.mem[next_pc]
addr_lo = s.mem[(next_pc + 1) & 0xFFFF]
addr16 = ((addr_hi & 0xFF) << 8) | (addr_lo & 0xFF)
next_pc_ext = (next_pc + 2) & 0xFFFF
write_result = True
result = a
carry = 0
overflow = 0
if opcode == 0x0:
result, carry, overflow = alu_add(a, b)
elif opcode == 0x1:
result, carry, overflow = alu_sub(a, b)
elif opcode == 0x2:
result = a & b
elif opcode == 0x3:
result = a | b
elif opcode == 0x4:
result = a ^ b
elif opcode == 0x5:
result = (a << 1) & 0xFF
elif opcode == 0x6:
result = (a >> 1) & 0xFF
elif opcode == 0x7:
result = (a * b) & 0xFF
elif opcode == 0x8:
if b == 0:
result = 0xFF
else:
result = a // b
elif opcode == 0x9:
result, carry, overflow = alu_sub(a, b)
write_result = False
elif opcode == 0xA:
result = s.mem[addr16]
elif opcode == 0xB:
s.mem[addr16] = b & 0xFF
write_result = False
elif opcode == 0xC:
s.pc = addr16 & 0xFFFF
write_result = False
elif opcode == 0xD:
cond_type = imm8 & 0x7
if cond_type == 0:
take_branch = s.flags[0] == 1
elif cond_type == 1:
take_branch = s.flags[0] == 0
elif cond_type == 2:
take_branch = s.flags[2] == 1
elif cond_type == 3:
take_branch = s.flags[2] == 0
elif cond_type == 4:
take_branch = s.flags[1] == 1
elif cond_type == 5:
take_branch = s.flags[1] == 0
elif cond_type == 6:
take_branch = s.flags[3] == 1
else:
take_branch = s.flags[3] == 0
if take_branch:
s.pc = addr16 & 0xFFFF
else:
s.pc = next_pc_ext
write_result = False
elif opcode == 0xE:
ret_addr = next_pc_ext & 0xFFFF
s.sp = (s.sp - 1) & 0xFFFF
s.mem[s.sp] = (ret_addr >> 8) & 0xFF
s.sp = (s.sp - 1) & 0xFFFF
s.mem[s.sp] = ret_addr & 0xFF
s.pc = addr16 & 0xFFFF
write_result = False
elif opcode == 0xF:
s.ctrl[0] = 1
write_result = False
# Flag policy: only ADD, SUB, MUL, and CMP write Z/N/C/V (MUL clears C
# and V). Bitwise, shift, DIV, LOAD, and STORE leave FLAGS unchanged.
if opcode in (0x0, 0x1, 0x7, 0x9):
s.flags = list(flags_from_result(result, carry, overflow))
if write_result:
s.regs[rd] = result & 0xFF
if opcode not in (0xC, 0xD, 0xE):
s.pc = next_pc_ext
return s
def ref_run_until_halt(state: CPUState, max_cycles: int = 256) -> Tuple[CPUState, int]:
"""Reference execution loop."""
s = state.copy()
for i in range(max_cycles):
if s.ctrl[0] == 1:
return s, i
s = ref_step(s)
return s, max_cycles
def encode_instr(opcode: int, rd: int, rs: int, imm8: int) -> int:
return ((opcode & 0xF) << 12) | ((rd & 0x3) << 10) | ((rs & 0x3) << 8) | (imm8 & 0xFF)
def write_instr(mem: List[int], addr: int, instr: int) -> None:
mem[addr & 0xFFFF] = (instr >> 8) & 0xFF
mem[(addr + 1) & 0xFFFF] = instr & 0xFF
def write_addr(mem: List[int], addr: int, value: int) -> None:
mem[addr & 0xFFFF] = (value >> 8) & 0xFF
mem[(addr + 1) & 0xFFFF] = value & 0xFF
def _fill_smoke_program(mem: List[int]) -> None:
"""LOAD/ADD/STORE, MUL with a high-bit operand, and a SUB/JNZ loop."""
write_instr(mem, 0x0000, encode_instr(0xA, 0, 0, 0x00))
write_addr(mem, 0x0002, 0x0100)
write_instr(mem, 0x0004, encode_instr(0xA, 1, 0, 0x00))
write_addr(mem, 0x0006, 0x0101)
write_instr(mem, 0x0008, encode_instr(0x0, 0, 1, 0x00))
write_instr(mem, 0x000A, encode_instr(0xB, 0, 0, 0x00))
write_addr(mem, 0x000C, 0x0102)
write_instr(mem, 0x000E, encode_instr(0xA, 2, 0, 0x00))
write_addr(mem, 0x0010, 0x0103)
write_instr(mem, 0x0012, encode_instr(0xA, 3, 0, 0x00))
write_addr(mem, 0x0014, 0x0104)
write_instr(mem, 0x0016, encode_instr(0x7, 2, 3, 0x00))
write_instr(mem, 0x0018, encode_instr(0xB, 0, 2, 0x00))
write_addr(mem, 0x001A, 0x0105)
write_instr(mem, 0x001C, encode_instr(0xA, 0, 0, 0x00))
write_addr(mem, 0x001E, 0x0106)
write_instr(mem, 0x0020, encode_instr(0xA, 1, 0, 0x00))
write_addr(mem, 0x0022, 0x0107)
write_instr(mem, 0x0024, encode_instr(0xA, 3, 0, 0x00))
write_addr(mem, 0x0026, 0x0108)
write_instr(mem, 0x0028, encode_instr(0x0, 1, 0, 0x00)) # loop: R1 += R0
write_instr(mem, 0x002A, encode_instr(0x1, 0, 3, 0x00)) # R0 -= 1
write_instr(mem, 0x002C, encode_instr(0xD, 0, 0, 0x01)) # JNZ loop
write_addr(mem, 0x002E, 0x0028)
write_instr(mem, 0x0030, encode_instr(0xB, 0, 1, 0x00))
write_addr(mem, 0x0032, 0x0109)
write_instr(mem, 0x0034, encode_instr(0xF, 0, 0, 0x00))
mem[0x0100] = 5
mem[0x0101] = 7
mem[0x0103] = 2
mem[0x0104] = 131
mem[0x0106] = 3
mem[0x0107] = 0
mem[0x0108] = 1
def run_smoke_test() -> int:
"""Programs through the gate-routed CPU runtime against the Python reference."""
import os
pass # lazy: avoids eval<->eval_all cycle
expected = {0x0102: 12, 0x0105: 6, 0x0109: 6}
print("Running reference implementation...")
ref_mem = [0] * 65536
_fill_smoke_program(ref_mem)
state = CPUState(pc=0, ir=0, regs=[0, 0, 0, 0], flags=[0, 0, 0, 0],
sp=0xFFFE, ctrl=[0, 0, 0, 0], mem=ref_mem)
final, cycles = ref_run_until_halt(state, max_cycles=40)
assert final.ctrl[0] == 1, "HALT flag not set"
for addr, want in expected.items():
assert final.mem[addr] == want, f"MEM[{addr:#06x}] expected {want}, got {final.mem[addr]}"
print(f" Reference: ADD={final.mem[0x0102]}, MUL={final.mem[0x0105]}, "
f"LOOP={final.mem[0x0109]}, cycles={cycles}")
print("Running threshold-weight implementation (GenericThresholdCPU, 1 KB)...")
path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "variants",
"neural_computer8_small.safetensors")
tensors = {}
with safe_open(path, framework="pt") as f:
for name in f.keys():
tensors[name] = f.get_tensor(name).float()
cpu = GenericThresholdCPU(tensors)
t_mem = [0] * 1024
_fill_smoke_program(t_mem)
t_state = {"pc": 0, "regs": [0] * 4, "flags": [0] * 4, "mem": t_mem, "halted": False}
t_final, t_cycles = cpu.run(t_state, max_cycles=40)
assert t_final["halted"], "Threshold HALT not reached"
for addr, want in expected.items():
assert t_final["mem"][addr] == want, (
f"Threshold MEM[{addr:#06x}] mismatch: {t_final['mem'][addr]} != {want}")
assert t_cycles == cycles, f"cycle count mismatch: {t_cycles} != {cycles}"
print(f" Threshold: ADD={t_final['mem'][0x0102]}, MUL={t_final['mem'][0x0105]}, "
f"LOOP={t_final['mem'][0x0109]}, cycles={t_cycles}")
print("\nSmoke test: PASSED")
return 0
# =============================================================================
# NETLIST EVALUATION (metadata-driven)
# =============================================================================
class NetlistEvaluator:
"""Evaluate a self-contained circuit from its shipped wiring metadata."""
def __init__(self, tensors: Dict[str, torch.Tensor],
signal_registry: Dict[str, str], prefix: str,
pop_size: int = 1, levels: bool = True):
id_to_name = {int(k): v for k, v in signal_registry.items()}
self.prefix = prefix
self.pop_size = pop_size
self.gates: Dict[str, Tuple[torch.Tensor, torch.Tensor, List[str]]] = {}
for key, t in tensors.items():
if not key.endswith('.inputs'):
continue
gate = key[: -len('.inputs')]
if gate != prefix and not gate.startswith(prefix + '.'):
continue
w = tensors.get(gate + '.weight')
b = tensors.get(gate + '.bias')
if w is None or b is None:
continue
inp = t.reshape(pop_size, -1)[0] if pop_size > 1 else t.flatten()
fan = inp.numel()
if w.numel() != fan * pop_size:
continue # packed multi-gate tensor; not a single netlist gate
names = [id_to_name[int(i)] for i in inp.tolist()]
self.gates[gate] = (w.float().view(pop_size, fan),
b.float().view(pop_size), names)
if not self.gates:
raise KeyError(f"no wired gates under prefix {prefix}")
# Topological order (Kahn). Inputs that are gates in this circuit are
# dependencies; everything else must be bound externally.
indeg = {g: 0 for g in self.gates}
consumers: Dict[str, List[str]] = {}
for g, (_, _, names) in self.gates.items():
for n in names:
if n in self.gates:
indeg[g] += 1
consumers.setdefault(n, []).append(g)
order = [g for g, d in indeg.items() if d == 0]
i = 0
while i < len(order):
for c in consumers.get(order[i], []):
indeg[c] -= 1
if indeg[c] == 0:
order.append(c)
i += 1
if len(order) != len(self.gates):
cyc = sorted(set(self.gates) - set(order))[:5]
raise ValueError(f"wiring cycle under {prefix}: {cyc}")
self.order = order
# Leveled plan: one padded tensor op per topological level instead of
# one Python step per gate. Turns thousands of tiny ops into ~depth
# batched ops (a MULH-class netlist drops from seconds to tens of ms).
self._plan = None
if levels:
self._build_levels(id_to_name)
def _build_levels(self, id_to_name):
# Signal slots: constants, all externals, all gate outputs.
ext = self.external_names()
names = ['#0', '#1'] + ext + self.order
self.slot = {n: i for i, n in enumerate(names)}
self.n_sig = len(names)
self.ext_names = ext
depth = {}
for n in names:
if n not in self.gates:
depth[n] = 0
for g in self.order:
_, _, ins = self.gates[g]
depth[g] = 1 + max((depth[n] for n in ins), default=0)
by_level: Dict[int, List[str]] = {}
for g in self.order:
by_level.setdefault(depth[g], []).append(g)
plan = []
for lvl in sorted(by_level):
gs = by_level[lvl]
max_fan = max(len(self.gates[g][2]) for g in gs)
n_g = len(gs)
idx = torch.zeros(n_g, max_fan, dtype=torch.long)
w = torch.zeros(n_g, max_fan, self.pop_size)
b = torch.zeros(n_g, self.pop_size)
out = torch.zeros(n_g, dtype=torch.long)
for r, g in enumerate(gs):
wt, bs, ins = self.gates[g]
out[r] = self.slot[g]
b[r] = bs
for c, n in enumerate(ins):
idx[r, c] = self.slot[n]
w[r, c] = wt[:, c]
plan.append((idx, w, b, out))
self._plan = plan
def external_names(self) -> List[str]:
"""Every non-gate, non-constant signal the circuit consumes."""
out = set()
for _, (_, _, names) in self.gates.items():
for n in names:
if n not in self.gates and n not in ('#0', '#1'):
out.add(n)
return sorted(out)
def run(self, external: Dict[str, torch.Tensor]) -> Dict[str, torch.Tensor]:
"""Evaluate the circuit."""
first = next(iter(external.values()))
if not torch.is_tensor(first):
num_tests = 1
else:
num_tests = first.shape[0] if first.dim() else 1
device = self.gates[self.order[0]][0].device
def expand(v):
t = v if torch.is_tensor(v) else torch.tensor(float(v))
t = t.float().to(device).reshape(-1)
return t.unsqueeze(1).expand(num_tests, self.pop_size)
if self._plan is not None:
V = torch.zeros(self.n_sig, num_tests, self.pop_size, device=device)
V[self.slot['#1']] = 1.0
for k, v in external.items():
V[self.slot[k]] = expand(v)
for idx, w, b, out in self._plan:
# gathered: [n_g, max_fan, num_tests, pop]
gathered = V[idx]
acc = (gathered * w[:, :, None, :]).sum(1) + b[:, None, :]
V[out] = (acc >= 0).float()
return _SlotView(V, self.slot)
values: Dict[str, torch.Tensor] = {
'#0': torch.zeros(num_tests, self.pop_size, device=device),
'#1': torch.ones(num_tests, self.pop_size, device=device),
}
for k, v in external.items():
values[k] = expand(v)
for g in self.order:
w, b, names = self.gates[g]
acc = b.unsqueeze(0).expand(num_tests, self.pop_size).clone()
for k, n in enumerate(names):
acc = acc + w[:, k] * values[n]
values[g] = (acc >= 0).float()
return values
class _SlotView:
"""Read gate outputs by name from a packed [n_sig, num_tests, pop] tensor."""
__slots__ = ("_V", "_slot")
def __init__(self, V, slot):
self._V = V
self._slot = slot
def __getitem__(self, name):
return self._V[self._slot[name]]
def __contains__(self, name):
return name in self._slot
def float_bits_to_value(word: int, exp_bits: int, frac_bits: int) -> float:
"""Decode an IEEE 754 word to a Python float, exactly."""
sign = (word >> (exp_bits + frac_bits)) & 1
exp = (word >> frac_bits) & ((1 << exp_bits) - 1)
frac = word & ((1 << frac_bits) - 1)
bias = (1 << (exp_bits - 1)) - 1
if exp == (1 << exp_bits) - 1:
if frac:
return float('nan')
return float('-inf') if sign else float('inf')
if exp == 0:
v = frac * 2.0 ** (1 - bias - frac_bits)
else:
v = (frac + (1 << frac_bits)) * 2.0 ** (exp - bias - frac_bits)
return -v if sign else v
def float_test_words(exp_bits: int, frac_bits: int) -> Tuple[List[int], List[int]]:
"""Directed IEEE edge encodings (every category, both signs) plus seeded random words for a family."""
import random
E, F = exp_bits, frac_bits
emax = (1 << E) - 1
fmax = (1 << F) - 1
bias = (1 << (E - 1)) - 1
def word(s, e, f):
return (s << (E + F)) | (e << F) | f
directed = []
for s in (0, 1):
directed += [
word(s, 0, 0), # +-0
word(s, 0, 1), # smallest subnormal
word(s, 0, fmax), # largest subnormal
word(s, 1, 0), # smallest normal
word(s, bias, 0), # +-1.0
word(s, bias, 1 << (F - 1)), # +-1.5
word(s, emax - 1, fmax), # largest normal
word(s, emax, 0), # +-inf
word(s, emax, 1), # NaN, minimal payload
word(s, emax, fmax), # NaN, full payload
]
rng = random.Random(0xF10A7 + E)
randoms = [rng.getrandbits(1 + E + F) for _ in range(24)]
return directed, randoms
def float_mul_oracle(aw: int, bw: int, exp_bits: int, frac_bits: int) -> int:
"""The expected IEEE product word: exact specials, subnormals, round to nearest even."""
E, F = exp_bits, frac_bits
emax = (1 << E) - 1
fmask = (1 << F) - 1
bias = (1 << (E - 1)) - 1
qnan = (emax << F) | (1 << (F - 1))
sa, ea, fa = (aw >> (E + F)) & 1, (aw >> F) & emax, aw & fmask
sb, eb, fb = (bw >> (E + F)) & 1, (bw >> F) & emax, bw & fmask
s = sa ^ sb
a_nan = ea == emax and fa != 0
b_nan = eb == emax and fb != 0
a_inf = ea == emax and fa == 0
b_inf = eb == emax and fb == 0
a_zero = ea == 0 and fa == 0
b_zero = eb == 0 and fb == 0
if a_nan or b_nan or (a_inf and b_zero) or (b_inf and a_zero):
return qnan
if a_inf or b_inf:
return (s << (E + F)) | (emax << F)
if a_zero or b_zero:
return s << (E + F)
Ma = ((1 << F) if ea else 0) | fa
Mb = ((1 << F) if eb else 0) | fb
eea = ea if ea else 1
eeb = eb if eb else 1
P = Ma * Mb
t = P.bit_length() - 1
er = t + eea + eeb - bias - 2 * F
if er >= 1:
drop = t - F
if drop <= 0:
mant = P << (-drop)
guard = sticky = 0
else:
mant = P >> drop
guard = (P >> (drop - 1)) & 1
sticky = 1 if (P & ((1 << (drop - 1)) - 1)) else 0
frac = mant & fmask
if guard and ((frac & 1) or sticky):
frac += 1
if frac > fmask:
frac = 0
er += 1
if er >= emax:
return (s << (E + F)) | (emax << F)
return (s << (E + F)) | (er << F) | frac
sh_q = eea + eeb - bias - F - 1 # subnormal: value in quanta
if sh_q >= 0:
sig = P << sh_q
guard = sticky = 0
else:
r = -sh_q
sig = P >> r
guard = (P >> (r - 1)) & 1
sticky = 1 if (P & ((1 << (r - 1)) - 1)) else 0
if guard and ((sig & 1) or sticky):
sig += 1
if sig == 0:
return s << (E + F)
if sig >= (1 << F):
return (s << (E + F)) | (1 << F)
return (s << (E + F)) | sig
def float_div_oracle(aw: int, bw: int, exp_bits: int, frac_bits: int) -> int:
"""The expected IEEE quotient word: exact specials, subnormals, round to nearest even."""
E, F = exp_bits, frac_bits
emax = (1 << E) - 1
fmask = (1 << F) - 1
bias = (1 << (E - 1)) - 1
qnan = (emax << F) | (1 << (F - 1))
sa, ea, fa = (aw >> (E + F)) & 1, (aw >> F) & emax, aw & fmask
sb, eb, fb = (bw >> (E + F)) & 1, (bw >> F) & emax, bw & fmask
s = sa ^ sb
a_nan = ea == emax and fa != 0
b_nan = eb == emax and fb != 0
a_inf = ea == emax and fa == 0
b_inf = eb == emax and fb == 0
a_zero = ea == 0 and fa == 0
b_zero = eb == 0 and fb == 0
if a_nan or b_nan or (a_zero and b_zero) or (a_inf and b_inf):
return qnan
if a_inf or b_zero:
return (s << (E + F)) | (emax << F)
if a_zero or b_inf:
return s << (E + F)
Ma = ((1 << F) if ea else 0) | fa
Mb = ((1 << F) if eb else 0) | fb
eea = ea if ea else 1
eeb = eb if eb else 1
ta = Ma.bit_length() - 1 # pre-normalize both mantissas
tb = Mb.bit_length() - 1
na = eea - (F - ta)
nb = eeb - (F - tb)
Ma <<= (F - ta)
Mb <<= (F - tb)
q0 = 1 if Ma >= Mb else 0
er = na - nb + bias - 1 + q0
num = Ma << (F + 2 - q0)
Q = num // Mb
rem = num % Mb
if er >= 1:
frac = (Q >> 1) & fmask
guard = Q & 1
sticky = 1 if rem else 0
if guard and ((frac & 1) or sticky):
frac += 1
if frac > fmask:
frac = 0
er += 1
if er >= emax:
return (s << (E + F)) | (emax << F)
return (s << (E + F)) | (er << F) | frac
rsh = (1 - er) + 1 # gradual underflow
sig = Q >> rsh
guard = (Q >> (rsh - 1)) & 1
sticky = 1 if ((Q & ((1 << (rsh - 1)) - 1)) or rem) else 0
if guard and ((sig & 1) or sticky):
sig += 1
if sig == 0:
return s << (E + F)
if sig >= (1 << F):
return (s << (E + F)) | (1 << F)
return (s << (E + F)) | sig
def float_fma_oracle(aw: int, bw: int, cw: int, exp_bits: int, frac_bits: int) -> int:
"""The expected word of round(a*b + c), rounded once."""
E, F = exp_bits, frac_bits
emax = (1 << E) - 1
fmask = (1 << F) - 1
bias = (1 << (E - 1)) - 1
qnan = (emax << F) | (1 << (F - 1))
def dec(w):
return (w >> (E + F)) & 1, (w >> F) & emax, w & fmask
sa, ea, fa = dec(aw)
sb, eb, fb = dec(bw)
sc, ec, fc = dec(cw)
sp = sa ^ sb
a_nan = ea == emax and fa != 0
b_nan = eb == emax and fb != 0
c_nan = ec == emax and fc != 0
a_inf = ea == emax and fa == 0
b_inf = eb == emax and fb == 0
c_inf = ec == emax and fc == 0
a_zero = ea == 0 and fa == 0
b_zero = eb == 0 and fb == 0
c_zero = ec == 0 and fc == 0
if a_nan or b_nan or c_nan:
return qnan
if (a_inf and b_zero) or (b_inf and a_zero):
return qnan
if a_inf or b_inf:
if c_inf and sc != sp:
return qnan
return (sp << (E + F)) | (emax << F)
if c_inf:
return (sc << (E + F)) | (emax << F)
Ma = ((1 << F) if ea else 0) | fa
Mb = ((1 << F) if eb else 0) | fb
Mc = ((1 << F) if ec else 0) | fc
eea = ea if ea else 1
eeb = eb if eb else 1
eec = ec if ec else 1
P = Ma * Mb
pe = eea + eeb - 2 * bias - 2 * F
ce = eec - bias - F
if P == 0 and c_zero:
return ((1 if (sp and sc) else 0) << (E + F))
g = min(pe, ce)
Pi = P << (pe - g)
Ci = Mc << (ce - g)
S = (-Pi if sp else Pi) + (-Ci if sc else Ci)
if S == 0:
return 0
s_out = 1 if S < 0 else 0
mag = -S if S < 0 else S
t = mag.bit_length() - 1
er = t + g + bias
if er >= 1:
drop = t - F
if drop <= 0:
m = mag << (-drop)
guard = sticky = 0
else:
m = mag >> drop
guard = (mag >> (drop - 1)) & 1
sticky = 1 if (mag & ((1 << (drop - 1)) - 1)) else 0
frac = m & fmask
if guard and ((frac & 1) or sticky):
frac += 1
if frac > fmask:
frac = 0
er += 1
if er >= emax:
return (s_out << (E + F)) | (emax << F)
return (s_out << (E + F)) | (er << F) | frac
sh = (1 - bias - F) - g
if sh <= 0:
sig = mag << (-sh)
guard = sticky = 0
else:
sig = mag >> sh
guard = (mag >> (sh - 1)) & 1
sticky = 1 if (mag & ((1 << (sh - 1)) - 1)) else 0
if guard and ((sig & 1) or sticky):
sig += 1
if sig == 0:
return s_out << (E + F)
if sig >= (1 << F):
return (s_out << (E + F)) | (1 << F)
return (s_out << (E + F)) | sig
def float_add_oracle(aw: int, bw: int, exp_bits: int, frac_bits: int) -> int:
"""The expected IEEE sum word: exact specials, subnormals, round to nearest even."""
E, F = exp_bits, frac_bits
emax = (1 << E) - 1
fmask = (1 << F) - 1
qnan = (emax << F) | (1 << (F - 1))
sa, ea, fa = (aw >> (E + F)) & 1, (aw >> F) & emax, aw & fmask
sb, eb, fb = (bw >> (E + F)) & 1, (bw >> F) & emax, bw & fmask
a_nan = ea == emax and fa != 0
b_nan = eb == emax and fb != 0
a_inf = ea == emax and fa == 0
b_inf = eb == emax and fb == 0
if a_nan or b_nan or (a_inf and b_inf and sa != sb):
return qnan
if a_inf or b_inf:
s = sa if a_inf else sb
return (s << (E + F)) | (emax << F)
a_zero = ea == 0 and fa == 0 # true zero, not subnormal
b_zero = eb == 0 and fb == 0
if a_zero and b_zero:
return (sa & sb) << (E + F)
if a_zero:
return bw
if b_zero:
return aw
Ma = ((1 << F) if ea else 0) | fa # implicit bit = (exp != 0)
Mb = ((1 << F) if eb else 0) | fb
eea = ea if ea else 1 # effective (denormal) exponent
eeb = eb if eb else 1
pla = (eea << (F + 1)) | Ma
plb = (eeb << (F + 1)) | Mb
if pla >= plb:
sL, ML, eL, MS, eS = sa, Ma, eea, Mb, eeb
else:
sL, ML, eL, MS, eS = sb, Mb, eeb, Ma, eea
d = eL - eS
# Exact fixed-point value with G extra low bits below the mantissa LSB.
G = F + 4
total = (ML << (d + G)) + (MS << G) if sa == sb else (ML << (d + G)) - (MS << G)
if total == 0:
return 0 # exact cancellation -> +0
t = total.bit_length() - 1
exp_r = eS + (t - G) - F
if exp_r >= 1:
lead = F + G
if t >= lead:
sh = t - lead
mant_ext = total >> sh
sticky_low = 1 if (total & ((1 << sh) - 1)) else 0
else:
mant_ext = total << (lead - t)
sticky_low = 0
frac = (mant_ext >> G) & fmask
guard = (mant_ext >> (G - 1)) & 1
below = (mant_ext & ((1 << (G - 1)) - 1)) or sticky_low
if guard and ((frac & 1) or below): # round-to-nearest-even
frac += 1
if frac > fmask:
frac = 0
exp_r += 1
if exp_r >= emax:
return (sL << (E + F)) | (emax << F)
return (sL << (E + F)) | (exp_r << F) | frac
# subnormal result: gradual underflow to exponent field 0
rshift = G + 1 - eS
if rshift <= 0:
sig = total << (-rshift)
else:
sig = total >> rshift
guard = (total >> (rshift - 1)) & 1
sticky = 1 if (total & ((1 << (rshift - 1)) - 1)) else 0
if guard and ((sig & 1) or sticky):
sig += 1
if sig == 0:
return sL << (E + F)
if sig >= (1 << F): # rounded up to smallest normal
return (sL << (E + F)) | (1 << F)
return (sL << (E + F)) | sig
# =============================================================================
# CIRCUIT EVALUATION
# =============================================================================
class BatchedFitnessEvaluator:
"""GPU-batched fitness evaluator with per-circuit reporting."""
def __init__(self, device: str = 'cuda', model_path: str = MODEL_PATH, tensors: Dict[str, torch.Tensor] = None):
self.device = device
self.model_path = model_path
self.metadata = load_metadata(model_path)
self.signal_registry = self.metadata.get('signal_registry', {})
self.results: List[CircuitResult] = []
self.category_scores: Dict[str, Tuple[float, int]] = {}
self.total_tests = 0
# Get manifest for N-bit support
if tensors is not None:
self.manifest = get_manifest(tensors)
else:
base_tensors = load_model(model_path)
self.manifest = get_manifest(base_tensors)
self.data_bits = self.manifest['data_bits']
self.addr_bits = self.manifest['addr_bits']
self._setup_tests()
def _setup_tests(self):
"""Pre-compute test vectors on device."""
d = self.device
# 2-input truth table [4, 2]
self.tt2 = torch.tensor(
[[0, 0], [0, 1], [1, 0], [1, 1]],
device=d, dtype=torch.float32
)
# 3-input truth table [8, 3]
self.tt3 = torch.tensor([
[0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1],
[1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 1]
], device=d, dtype=torch.float32)
# Boolean gate expected outputs
self.expected = {
'and': torch.tensor([0, 0, 0, 1], device=d, dtype=torch.float32),
'or': torch.tensor([0, 1, 1, 1], device=d, dtype=torch.float32),
'nand': torch.tensor([1, 1, 1, 0], device=d, dtype=torch.float32),
'nor': torch.tensor([1, 0, 0, 0], device=d, dtype=torch.float32),
'xor': torch.tensor([0, 1, 1, 0], device=d, dtype=torch.float32),
'xnor': torch.tensor([1, 0, 0, 1], device=d, dtype=torch.float32),
'implies': torch.tensor([1, 1, 0, 1], device=d, dtype=torch.float32),
'biimplies': torch.tensor([1, 0, 0, 1], device=d, dtype=torch.float32),
'not': torch.tensor([1, 0], device=d, dtype=torch.float32),
'ha_sum': torch.tensor([0, 1, 1, 0], device=d, dtype=torch.float32),
'ha_carry': torch.tensor([0, 0, 0, 1], device=d, dtype=torch.float32),
'fa_sum': torch.tensor([0, 1, 1, 0, 1, 0, 0, 1], device=d, dtype=torch.float32),
'fa_cout': torch.tensor([0, 0, 0, 1, 0, 1, 1, 1], device=d, dtype=torch.float32),
}
# NOT gate inputs
self.not_inputs = torch.tensor([[0], [1]], device=d, dtype=torch.float32)
# 8-bit test values
self.test_8bit = torch.tensor([
0, 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64, 127, 128, 255,
0b10101010, 0b01010101, 0b11110000, 0b00001111,
0b11001100, 0b00110011, 0b10000001, 0b01111110
], device=d, dtype=torch.long)
# Bit representations [num_vals, 8]
self.test_8bit_bits = torch.stack([
((self.test_8bit >> (7 - i)) & 1).float() for i in range(8)
], dim=1)
# Comparator test pairs
comp_tests = [
(0, 0), (1, 0), (0, 1), (5, 3), (3, 5), (5, 5),
(255, 0), (0, 255), (128, 127), (127, 128),
(100, 99), (99, 100), (64, 32), (32, 64),
(1, 1), (254, 255), (255, 254), (128, 128),
(0, 128), (128, 0), (64, 64), (192, 192),
(15, 16), (16, 15), (240, 239), (239, 240),
(85, 170), (170, 85), (0xAA, 0x55), (0x55, 0xAA),
(0x0F, 0xF0), (0xF0, 0x0F), (0x33, 0xCC), (0xCC, 0x33),
(2, 3), (3, 2), (126, 127), (127, 126),
(129, 128), (128, 129), (200, 199), (199, 200),
(50, 51), (51, 50), (10, 20), (20, 10),
(100, 100), (200, 200), (77, 77), (0, 0)
]
self.comp_a = torch.tensor([c[0] for c in comp_tests], device=d, dtype=torch.long)
self.comp_b = torch.tensor([c[1] for c in comp_tests], device=d, dtype=torch.long)
# Modular test range
self.mod_test = torch.arange(256, device=d, dtype=torch.long)
# 32-bit test values (strategic sampling)
self.test_32bit = torch.tensor([
0, 1, 2, 255, 256, 65535, 65536,
0x7FFFFFFF, 0x80000000, 0xFFFFFFFF,
0x12345678, 0xDEADBEEF, 0xCAFEBABE,
1000000, 1000000000, 2147483647,
0x55555555, 0xAAAAAAAA, 0x0F0F0F0F, 0xF0F0F0F0
], device=d, dtype=torch.long)
# 32-bit comparator test pairs
comp32_tests = [
(0, 0), (1, 0), (0, 1), (1000, 999), (999, 1000),
(0xFFFFFFFF, 0), (0, 0xFFFFFFFF),
(0x80000000, 0x7FFFFFFF), (0x7FFFFFFF, 0x80000000),
(1000000, 1000000), (0x12345678, 0x12345678),
(0xDEADBEEF, 0xCAFEBABE), (0xCAFEBABE, 0xDEADBEEF),
(256, 255), (255, 256), (65536, 65535), (65535, 65536),
]
self.comp32_a = torch.tensor([c[0] for c in comp32_tests], device=d, dtype=torch.long)
self.comp32_b = torch.tensor([c[1] for c in comp32_tests], device=d, dtype=torch.long)
def _record(self, name: str, passed: int, total: int, failures: List[Tuple] = None):
"""Record a circuit test result."""
self.results.append(CircuitResult(
name=name,
passed=passed,
total=total,
failures=failures or []
))
# =========================================================================
# BOOLEAN GATES
# =========================================================================
def _test_single_gate(self, pop: Dict, prefix: str, inputs: torch.Tensor,
expected: torch.Tensor) -> torch.Tensor:
"""Test single-layer gate (AND, OR, NAND, NOR, IMPLIES)."""
pop_size = next(iter(pop.values())).shape[0]
w = pop[f'{prefix}.weight']
b = pop[f'{prefix}.bias']
# [num_tests, pop_size]
out = heaviside(inputs @ w.view(pop_size, -1).T + b.view(pop_size))
correct = (out == expected.unsqueeze(1)).float().sum(0)
failures = []
if pop_size == 1:
for i, (inp, exp, got) in enumerate(zip(inputs, expected, out[:, 0])):
if exp.item() != got.item():
failures.append((inp.tolist(), exp.item(), got.item()))
self._record(prefix, int(correct[0].item()), len(expected), failures)
return correct
def _test_twolayer_gate(self, pop: Dict, prefix: str, inputs: torch.Tensor,
expected: torch.Tensor) -> torch.Tensor:
"""Test two-layer gate (XOR, XNOR, BIIMPLIES)."""
pop_size = next(iter(pop.values())).shape[0]
# Layer 1
w1_n1 = pop[f'{prefix}.layer1.neuron1.weight']
b1_n1 = pop[f'{prefix}.layer1.neuron1.bias']
w1_n2 = pop[f'{prefix}.layer1.neuron2.weight']
b1_n2 = pop[f'{prefix}.layer1.neuron2.bias']
h1 = heaviside(inputs @ w1_n1.view(pop_size, -1).T + b1_n1.view(pop_size))
h2 = heaviside(inputs @ w1_n2.view(pop_size, -1).T + b1_n2.view(pop_size))
hidden = torch.stack([h1, h2], dim=-1)
# Layer 2
w2 = pop[f'{prefix}.layer2.weight']
b2 = pop[f'{prefix}.layer2.bias']
out = heaviside((hidden * w2.view(pop_size, 2)).sum(-1) + b2.view(pop_size))
correct = (out == expected.unsqueeze(1)).float().sum(0)
failures = []
if pop_size == 1:
for i, (inp, exp, got) in enumerate(zip(inputs, expected, out[:, 0])):
if exp.item() != got.item():
failures.append((inp.tolist(), exp.item(), got.item()))
self._record(prefix, int(correct[0].item()), len(expected), failures)
return correct
def _test_xor_ornand(self, pop: Dict, prefix: str, inputs: torch.Tensor,
expected: torch.Tensor) -> torch.Tensor:
"""Test XOR with or/nand layer naming."""
pop_size = next(iter(pop.values())).shape[0]
w_or = pop[f'{prefix}.layer1.or.weight']
b_or = pop[f'{prefix}.layer1.or.bias']
w_nand = pop[f'{prefix}.layer1.nand.weight']
b_nand = pop[f'{prefix}.layer1.nand.bias']
h_or = heaviside(inputs @ w_or.view(pop_size, -1).T + b_or.view(pop_size))
h_nand = heaviside(inputs @ w_nand.view(pop_size, -1).T + b_nand.view(pop_size))
hidden = torch.stack([h_or, h_nand], dim=-1)
w2 = pop[f'{prefix}.layer2.weight']
b2 = pop[f'{prefix}.layer2.bias']
out = heaviside((hidden * w2.view(pop_size, 2)).sum(-1) + b2.view(pop_size))
correct = (out == expected.unsqueeze(1)).float().sum(0)
failures = []
if pop_size == 1:
for i, (inp, exp, got) in enumerate(zip(inputs, expected, out[:, 0])):
if exp.item() != got.item():
failures.append((inp.tolist(), exp.item(), got.item()))
self._record(prefix, int(correct[0].item()), len(expected), failures)
return correct
def _test_boolean_gates(self, pop: Dict, debug: bool) -> Tuple[torch.Tensor, int]:
"""Test all boolean gates."""
pop_size = next(iter(pop.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
total = 0
if debug:
print("\n=== BOOLEAN GATES ===")
# Single-layer gates
for gate in ['and', 'or', 'nand', 'nor', 'implies']:
scores += self._test_single_gate(pop, f'boolean.{gate}', self.tt2, self.expected[gate])
total += 4
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
# NOT gate
w = pop['boolean.not.weight']
b = pop['boolean.not.bias']
out = heaviside(self.not_inputs @ w.view(pop_size, -1).T + b.view(pop_size))
correct = (out == self.expected['not'].unsqueeze(1)).float().sum(0)
scores += correct
total += 2
failures = []
if pop_size == 1:
for inp, exp, got in zip(self.not_inputs, self.expected['not'], out[:, 0]):
if exp.item() != got.item():
failures.append((inp.tolist(), exp.item(), got.item()))
self._record('boolean.not', int(correct[0].item()), 2, failures)
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
# Two-layer gates
for gate in ['xnor', 'biimplies']:
scores += self._test_twolayer_gate(pop, f'boolean.{gate}', self.tt2, self.expected.get(gate, self.expected['xnor']))
total += 4
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
# XOR with neuron1/neuron2 naming (same as xnor/biimplies)
scores += self._test_twolayer_gate(pop, 'boolean.xor', self.tt2, self.expected['xor'])
total += 4
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
return scores, total
# =========================================================================
# ARITHMETIC - ADDERS
# =========================================================================
def _eval_xor(self, pop: Dict, prefix: str, a: torch.Tensor, b: torch.Tensor) -> torch.Tensor:
"""Evaluate XOR gate with or/nand decomposition."""
pop_size = next(iter(pop.values())).shape[0]
# Ensure inputs are [num_tests, pop_size]
if a.dim() == 1:
a = a.unsqueeze(1).expand(-1, pop_size)
if b.dim() == 1:
b = b.unsqueeze(1).expand(-1, pop_size)
# inputs: [num_tests, pop_size, 2]
inputs = torch.stack([a, b], dim=-1)
w_or = pop[f'{prefix}.layer1.or.weight'].view(pop_size, 2)
b_or = pop[f'{prefix}.layer1.or.bias'].view(pop_size)
w_nand = pop[f'{prefix}.layer1.nand.weight'].view(pop_size, 2)
b_nand = pop[f'{prefix}.layer1.nand.bias'].view(pop_size)
# [num_tests, pop_size]
h_or = heaviside((inputs * w_or).sum(-1) + b_or)
h_nand = heaviside((inputs * w_nand).sum(-1) + b_nand)
# hidden: [num_tests, pop_size, 2]
hidden = torch.stack([h_or, h_nand], dim=-1)
w2 = pop[f'{prefix}.layer2.weight'].view(pop_size, 2)
b2 = pop[f'{prefix}.layer2.bias'].view(pop_size)
return heaviside((hidden * w2).sum(-1) + b2)
def _eval_single_fa(self, pop: Dict, prefix: str,
a: torch.Tensor, b: torch.Tensor, cin: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
"""Evaluate single full adder."""
pop_size = next(iter(pop.values())).shape[0]
# Ensure inputs are [num_tests, pop_size]
if a.dim() == 1:
a = a.unsqueeze(1).expand(-1, pop_size)
if b.dim() == 1:
b = b.unsqueeze(1).expand(-1, pop_size)
if cin.dim() == 1:
cin = cin.unsqueeze(1).expand(-1, pop_size)
# Half adder 1: a XOR b -> [num_tests, pop_size]
ha1_sum = self._eval_xor(pop, f'{prefix}.ha1.sum', a, b)
# Half adder 1 carry: a AND b
ab = torch.stack([a, b], dim=-1) # [num_tests, pop_size, 2]
w_c1 = pop[f'{prefix}.ha1.carry.weight'].view(pop_size, 2)
b_c1 = pop[f'{prefix}.ha1.carry.bias'].view(pop_size)
ha1_carry = heaviside((ab * w_c1).sum(-1) + b_c1)
# Half adder 2: ha1_sum XOR cin
ha2_sum = self._eval_xor(pop, f'{prefix}.ha2.sum', ha1_sum, cin)
# Half adder 2 carry
sc = torch.stack([ha1_sum, cin], dim=-1)
w_c2 = pop[f'{prefix}.ha2.carry.weight'].view(pop_size, 2)
b_c2 = pop[f'{prefix}.ha2.carry.bias'].view(pop_size)
ha2_carry = heaviside((sc * w_c2).sum(-1) + b_c2)
# Carry out: ha1_carry OR ha2_carry
carries = torch.stack([ha1_carry, ha2_carry], dim=-1)
w_cout = pop[f'{prefix}.carry_or.weight'].view(pop_size, 2)
b_cout = pop[f'{prefix}.carry_or.bias'].view(pop_size)
cout = heaviside((carries * w_cout).sum(-1) + b_cout)
return ha2_sum, cout
def _test_halfadder(self, pop: Dict, debug: bool) -> Tuple[torch.Tensor, int]:
"""Test half adder."""
pop_size = next(iter(pop.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
total = 0
if debug:
print("\n=== HALF ADDER ===")
# Sum (XOR)
scores += self._test_xor_ornand(pop, 'arithmetic.halfadder.sum', self.tt2, self.expected['ha_sum'])
total += 4
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
# Carry (AND)
scores += self._test_single_gate(pop, 'arithmetic.halfadder.carry', self.tt2, self.expected['ha_carry'])
total += 4
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
return scores, total
def _test_fulladder(self, pop: Dict, debug: bool) -> Tuple[torch.Tensor, int]:
"""Test full adder with all 8 input combinations."""
pop_size = next(iter(pop.values())).shape[0]
if debug:
print("\n=== FULL ADDER ===")
a = self.tt3[:, 0]
b = self.tt3[:, 1]
cin = self.tt3[:, 2]
sum_out, cout = self._eval_single_fa(pop, 'arithmetic.fulladder', a, b, cin)
sum_correct = (sum_out == self.expected['fa_sum'].unsqueeze(1)).float().sum(0)
cout_correct = (cout == self.expected['fa_cout'].unsqueeze(1)).float().sum(0)
failures_sum = []
failures_cout = []
if pop_size == 1:
for i in range(8):
if sum_out[i, 0].item() != self.expected['fa_sum'][i].item():
failures_sum.append(([a[i].item(), b[i].item(), cin[i].item()],
self.expected['fa_sum'][i].item(), sum_out[i, 0].item()))
if cout[i, 0].item() != self.expected['fa_cout'][i].item():
failures_cout.append(([a[i].item(), b[i].item(), cin[i].item()],
self.expected['fa_cout'][i].item(), cout[i, 0].item()))
self._record('arithmetic.fulladder.sum', int(sum_correct[0].item()), 8, failures_sum)
self._record('arithmetic.fulladder.cout', int(cout_correct[0].item()), 8, failures_cout)
if debug:
for r in self.results[-2:]:
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
return sum_correct + cout_correct, 16
def _test_ripplecarry(self, pop: Dict, bits: int, debug: bool) -> Tuple[torch.Tensor, int]:
"""Test N-bit ripple carry adder."""
pop_size = next(iter(pop.values())).shape[0]
if debug:
print(f"\n=== RIPPLE CARRY {bits}-BIT ===")
prefix = f'arithmetic.ripplecarry{bits}bit'
max_val = 1 << bits
num_tests = min(max_val * max_val, 65536)
if bits <= 4:
# Exhaustive for small widths
test_a = torch.arange(max_val, device=self.device)
test_b = torch.arange(max_val, device=self.device)
a_vals, b_vals = torch.meshgrid(test_a, test_b, indexing='ij')
a_vals = a_vals.flatten()
b_vals = b_vals.flatten()
elif bits == 8:
# Strategic sampling for 8-bit
edge_vals = [0, 1, 2, 127, 128, 254, 255]
pairs = [(a, b) for a in edge_vals for b in edge_vals]
for i in range(0, 256, 16):
pairs.append((i, 255 - i))
pairs = list(set(pairs))
a_vals = torch.tensor([p[0] for p in pairs], device=self.device)
b_vals = torch.tensor([p[1] for p in pairs], device=self.device)
num_tests = len(pairs)
else:
# Width-scaled edges and bit patterns so the high full adders see
# ones and carries, not just the low byte.
half = 1 << (bits - 1)
top = max_val - 1
alt_a = int('AA' * (bits // 8), 16)
alt_5 = int('55' * (bits // 8), 16)
edge_vals = [0, 1, 2, 255, 256, half - 1, half, top - 1, top, alt_a, alt_5]
if bits == 32:
edge_vals += [0xFFFF, 0x10000, 0xDEADBEEF, 0x12345678, 1_000_000_000]
pairs = [(a, b) for a in edge_vals for b in edge_vals]
step = max_val // 16
for i in range(0, 16):
v = i * step
pairs.append((v, top - v))
pairs = list(set(pairs))
a_vals = torch.tensor([p[0] for p in pairs], device=self.device)
b_vals = torch.tensor([p[1] for p in pairs], device=self.device)
num_tests = len(pairs)
# Convert to bits [num_tests, bits]
a_bits = torch.stack([((a_vals >> (bits - 1 - i)) & 1).float() for i in range(bits)], dim=1)
b_bits = torch.stack([((b_vals >> (bits - 1 - i)) & 1).float() for i in range(bits)], dim=1)
# Evaluate ripple carry
carry = torch.zeros(len(a_vals), pop_size, device=self.device)
sum_bits = []
for bit in range(bits):
bit_idx = bits - 1 - bit # LSB first
s, carry = self._eval_single_fa(
pop, f'{prefix}.fa{bit}',
a_bits[:, bit_idx].unsqueeze(1).expand(-1, pop_size),
b_bits[:, bit_idx].unsqueeze(1).expand(-1, pop_size),
carry
)
sum_bits.append(s)
# Reconstruct result. float64 keeps 32-bit values exact; float32
# would round above 2^24 and corrupt the comparison.
sum_bits = torch.stack(sum_bits[::-1], dim=-1) # MSB first
result = torch.zeros(len(a_vals), pop_size, device=self.device, dtype=torch.float64)
for i in range(bits):
result += sum_bits[:, :, i].double() * (1 << (bits - 1 - i))
# Expected
expected = ((a_vals + b_vals) & (max_val - 1)).unsqueeze(1).expand(-1, pop_size).double()
correct = (result == expected).float().sum(0)
failures = []
if pop_size == 1:
for i in range(min(len(a_vals), 100)):
if result[i, 0].item() != expected[i, 0].item():
failures.append((
[int(a_vals[i].item()), int(b_vals[i].item())],
int(expected[i, 0].item()),
int(result[i, 0].item())
))
self._record(prefix, int(correct[0].item()), num_tests, failures)
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
return correct, num_tests
# =========================================================================
# 3-OPERAND ADDER
# =========================================================================
def _test_add3(self, pop: Dict, debug: bool) -> Tuple[torch.Tensor, int]:
"""Test 3-operand 8-bit adder (A + B + C)."""
pop_size = next(iter(pop.values())).shape[0]
if debug:
print(f"\n=== 3-OPERAND ADDER ===")
prefix = 'arithmetic.add3_8bit'
bits = 8
# Strategic test cases for 3-operand addition
# Include edge cases and overflow scenarios
test_cases = []
# Small values
for a in [0, 1, 2]:
for b in [0, 1, 2]:
for c in [0, 1, 2]:
test_cases.append((a, b, c))
# Edge values
edge = [0, 1, 127, 128, 254, 255]
for a in edge:
for b in edge:
for c in edge:
test_cases.append((a, b, c))
# Specific multi-operand expression tests
test_cases.extend([
(15, 27, 33), # Example from roadmap: 15 + 27 + 33 = 75
(100, 100, 55), # = 255 (exact fit)
(100, 100, 56), # = 256 -> 0 (overflow)
(85, 85, 85), # = 255 (exact fit)
(86, 85, 85), # = 256 -> 0 (overflow)
])
test_cases = list(set(test_cases))
a_vals = torch.tensor([t[0] for t in test_cases], device=self.device)
b_vals = torch.tensor([t[1] for t in test_cases], device=self.device)
c_vals = torch.tensor([t[2] for t in test_cases], device=self.device)
num_tests = len(test_cases)
# Convert to bits [num_tests, bits] MSB-first
a_bits = torch.stack([((a_vals >> (bits - 1 - i)) & 1).float() for i in range(bits)], dim=1)
b_bits = torch.stack([((b_vals >> (bits - 1 - i)) & 1).float() for i in range(bits)], dim=1)
c_bits = torch.stack([((c_vals >> (bits - 1 - i)) & 1).float() for i in range(bits)], dim=1)
# Stage 1: A + B
carry1 = torch.zeros(num_tests, pop_size, device=self.device)
stage1_bits = []
for bit in range(bits):
bit_idx = bits - 1 - bit # LSB first
s, carry1 = self._eval_single_fa(
pop, f'{prefix}.stage1.fa{bit}',
a_bits[:, bit_idx].unsqueeze(1).expand(-1, pop_size),
b_bits[:, bit_idx].unsqueeze(1).expand(-1, pop_size),
carry1
)
stage1_bits.append(s)
# Stage 2: stage1_result + C
carry2 = torch.zeros(num_tests, pop_size, device=self.device)
result_bits = []
for bit in range(bits):
bit_idx = bits - 1 - bit # LSB first
s, carry2 = self._eval_single_fa(
pop, f'{prefix}.stage2.fa{bit}',
stage1_bits[bit], # Already [num_tests, pop_size]
c_bits[:, bit_idx].unsqueeze(1).expand(-1, pop_size),
carry2
)
result_bits.append(s)
# Reconstruct result (bits are in LSB-first order, need to reverse for MSB-first)
result_bits = torch.stack(result_bits[::-1], dim=-1) # MSB first
result = torch.zeros(num_tests, pop_size, device=self.device)
for i in range(bits):
result += result_bits[:, :, i] * (1 << (bits - 1 - i))
# Expected (8-bit wrap)
expected = ((a_vals + b_vals + c_vals) & 0xFF).unsqueeze(1).expand(-1, pop_size).float()
correct = (result == expected).float().sum(0)
failures = []
if pop_size == 1:
for i in range(min(num_tests, 100)):
if result[i, 0].item() != expected[i, 0].item():
failures.append((
[int(a_vals[i].item()), int(b_vals[i].item()), int(c_vals[i].item())],
int(expected[i, 0].item()),
int(result[i, 0].item())
))
self._record(prefix, int(correct[0].item()), num_tests, failures)
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
if failures:
for inp, exp, got in failures[:5]:
print(f" FAIL: {inp[0]} + {inp[1]} + {inp[2]} = {exp}, got {got}")
return correct, num_tests
# =========================================================================
# ORDER OF OPERATIONS (A + B × C)
# =========================================================================
def _test_expr_add_mul(self, pop: Dict, debug: bool) -> Tuple[torch.Tensor, int]:
"""Test A + B × C expression circuit (order of operations)."""
pop_size = next(iter(pop.values())).shape[0]
if debug:
print(f"\n=== ORDER OF OPERATIONS (A + B × C) ===")
prefix = 'arithmetic.expr_add_mul'
bits = 8
# Test cases for order of operations
test_cases = []
# Specific examples from roadmap
test_cases.extend([
(5, 3, 2), # 5 + 3 × 2 = 5 + 6 = 11
(10, 4, 3), # 10 + 4 × 3 = 10 + 12 = 22
(1, 10, 10), # 1 + 10 × 10 = 1 + 100 = 101
(0, 15, 17), # 0 + 15 × 17 = 255
(1, 15, 17), # 1 + 15 × 17 = 256 -> 0 (overflow)
(100, 5, 5), # 100 + 5 × 5 = 100 + 25 = 125
])
# Edge cases
test_cases.extend([
(0, 0, 0), # 0 + 0 × 0 = 0
(255, 0, 0), # 255 + 0 × 0 = 255
(0, 255, 1), # 0 + 255 × 1 = 255
(0, 1, 255), # 0 + 1 × 255 = 255
(1, 1, 1), # 1 + 1 × 1 = 2
(0, 16, 16), # 0 + 16 × 16 = 256 -> 0 (overflow)
])
# Systematic small values
for a in [0, 1, 5, 10]:
for b in [0, 1, 2, 3]:
for c in [0, 1, 2, 3]:
test_cases.append((a, b, c))
# Remove duplicates
test_cases = list(set(test_cases))
a_vals = torch.tensor([t[0] for t in test_cases], device=self.device)
b_vals = torch.tensor([t[1] for t in test_cases], device=self.device)
c_vals = torch.tensor([t[2] for t in test_cases], device=self.device)
num_tests = len(test_cases)
# Convert to bits [num_tests, bits] MSB-first
a_bits = torch.stack([((a_vals >> (bits - 1 - i)) & 1).float() for i in range(bits)], dim=1)
b_bits = torch.stack([((b_vals >> (bits - 1 - i)) & 1).float() for i in range(bits)], dim=1)
c_bits = torch.stack([((c_vals >> (bits - 1 - i)) & 1).float() for i in range(bits)], dim=1)
# Evaluate mask stage: mask[stage][bit] = B[bit] AND C[stage]
# In the circuit: mask.s[stage].b[bit] operates on positional bits
# stage 0 = LSB of C (c_bits[:, 7]), stage 7 = MSB of C (c_bits[:, 0])
# bit 0 = LSB of B (b_bits[:, 7]), bit 7 = MSB of B (b_bits[:, 0])
masks = torch.zeros(8, num_tests, pop_size, 8, device=self.device) # [stage, tests, pop, bits]
for stage in range(8):
c_stage_bit = c_bits[:, 7 - stage].unsqueeze(1).expand(-1, pop_size) # C[stage]
for bit in range(8):
b_bit_val = b_bits[:, 7 - bit].unsqueeze(1).expand(-1, pop_size) # B[bit]
# AND gate
w = pop.get(f'{prefix}.mul.mask.s{stage}.b{bit}.weight')
bias = pop.get(f'{prefix}.mul.mask.s{stage}.b{bit}.bias')
if w is not None and bias is not None:
w = w.squeeze(-1) # [pop]
b_tensor = bias.squeeze(-1) # [pop]
# Properly broadcast for batch evaluation
inp = torch.stack([b_bit_val, c_stage_bit], dim=-1) # [tests, pop, 2]
out = heaviside(torch.einsum('tpi,pi->tp', inp, w) + b_tensor)
masks[stage, :, :, bit] = out
# Accumulator stages
# acc[0] = mask[0] (no shift)
# acc[1] = acc[0] + (mask[1] << 1)
# ...
# acc[7] = acc[6] + (mask[7] << 7)
acc = masks[0].clone() # [tests, pop, 8] - start with mask[0]
for stage in range(1, 8):
# Create shifted mask: (mask[stage] << stage)
# Shift left by 'stage' positions: bits 0..stage-1 become 0, bit k becomes mask[stage][k-stage]
shifted_mask = torch.zeros(num_tests, pop_size, 8, device=self.device)
for bit in range(8):
if bit >= stage:
shifted_mask[:, :, bit] = masks[stage, :, :, bit - stage]
# else: remains 0
# Add acc + shifted_mask using full adders
carry = torch.zeros(num_tests, pop_size, device=self.device)
new_acc = torch.zeros(num_tests, pop_size, 8, device=self.device)
for bit in range(8):
s, carry = self._eval_single_fa(
pop, f'{prefix}.mul.acc.s{stage}.fa{bit}',
acc[:, :, bit],
shifted_mask[:, :, bit],
carry
)
new_acc[:, :, bit] = s
acc = new_acc
# Final add stage: A + acc (multiplication result)
carry = torch.zeros(num_tests, pop_size, device=self.device)
result_bits = []
for bit in range(8):
a_bit_val = a_bits[:, 7 - bit].unsqueeze(1).expand(-1, pop_size)
s, carry = self._eval_single_fa(
pop, f'{prefix}.add.fa{bit}',
a_bit_val,
acc[:, :, bit],
carry
)
result_bits.append(s)
# Reconstruct result
result_bits = torch.stack(result_bits[::-1], dim=-1) # MSB first
result = torch.zeros(num_tests, pop_size, device=self.device)
for i in range(bits):
result += result_bits[:, :, i] * (1 << (bits - 1 - i))
# Expected: A + (B × C), with 8-bit wrap
expected = ((a_vals + b_vals * c_vals) & 0xFF).unsqueeze(1).expand(-1, pop_size).float()
correct = (result == expected).float().sum(0)
failures = []
if pop_size == 1:
for i in range(min(num_tests, 100)):
if result[i, 0].item() != expected[i, 0].item():
failures.append((
[int(a_vals[i].item()), int(b_vals[i].item()), int(c_vals[i].item())],
int(expected[i, 0].item()),
int(result[i, 0].item())
))
self._record(prefix, int(correct[0].item()), num_tests, failures)
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
if failures:
for inp, exp, got in failures[:5]:
print(f" FAIL: {inp[0]} + {inp[1]} × {inp[2]} = {exp}, got {got}")
return correct, num_tests
# =========================================================================
# COMPARATORS
# =========================================================================
def _eval_bit_cascade_compare(
self,
pop: Dict,
cmp_prefix: str,
out_gt: str,
out_lt: str,
out_ge: str,
out_le: str,
out_eq: str,
bits: int,
a_bits_2d: torch.Tensor,
b_bits_2d: torch.Tensor,
) -> Dict[str, torch.Tensor]:
"""Walk the ternary bit-cascade comparator generated by build.add_bit_cascade_compare."""
pop_size = next(iter(pop.values())).shape[0]
# Per-bit gt, lt, eq
gt_b: List[torch.Tensor] = []
lt_b: List[torch.Tensor] = []
eq_b: List[torch.Tensor] = []
for i in range(bits):
a_i = a_bits_2d[:, i].unsqueeze(1).expand(-1, pop_size)
b_i = b_bits_2d[:, i].unsqueeze(1).expand(-1, pop_size)
ab = torch.stack([a_i, b_i], dim=-1)
w = pop[f'{cmp_prefix}.bit{i}.gt.weight'].view(pop_size, 2)
bb = pop[f'{cmp_prefix}.bit{i}.gt.bias'].view(pop_size)
gt_b.append(heaviside((ab * w).sum(-1) + bb))
w = pop[f'{cmp_prefix}.bit{i}.lt.weight'].view(pop_size, 2)
bb = pop[f'{cmp_prefix}.bit{i}.lt.bias'].view(pop_size)
lt_b.append(heaviside((ab * w).sum(-1) + bb))
w = pop[f'{cmp_prefix}.bit{i}.eq.layer1.and.weight'].view(pop_size, 2)
bb = pop[f'{cmp_prefix}.bit{i}.eq.layer1.and.bias'].view(pop_size)
h_and = heaviside((ab * w).sum(-1) + bb)
w = pop[f'{cmp_prefix}.bit{i}.eq.layer1.nor.weight'].view(pop_size, 2)
bb = pop[f'{cmp_prefix}.bit{i}.eq.layer1.nor.bias'].view(pop_size)
h_nor = heaviside((ab * w).sum(-1) + bb)
hidden = torch.stack([h_and, h_nor], dim=-1)
w = pop[f'{cmp_prefix}.bit{i}.eq.weight'].view(pop_size, 2)
bb = pop[f'{cmp_prefix}.bit{i}.eq.bias'].view(pop_size)
eq_b.append(heaviside((hidden * w).sum(-1) + bb))
# eq_prefix[i] = AND of eq[0..i-1]
eq_pref: List[Optional[torch.Tensor]] = [None]
for i in range(1, bits):
eq_stack = torch.stack(eq_b[:i], dim=-1)
w = pop[f'{cmp_prefix}.cascade.eq_prefix.bit{i}.weight'].view(pop_size, i)
bb = pop[f'{cmp_prefix}.cascade.eq_prefix.bit{i}.bias'].view(pop_size)
eq_pref.append(heaviside((eq_stack * w).sum(-1) + bb))
# cascade gt[i], lt[i] = eq_prefix[i] AND gt_b[i] / lt_b[i]
casc_gt = [gt_b[0]]
casc_lt = [lt_b[0]]
for i in range(1, bits):
inp = torch.stack([eq_pref[i], gt_b[i]], dim=-1)
w = pop[f'{cmp_prefix}.cascade.gt.bit{i}.weight'].view(pop_size, 2)
bb = pop[f'{cmp_prefix}.cascade.gt.bit{i}.bias'].view(pop_size)
casc_gt.append(heaviside((inp * w).sum(-1) + bb))
inp = torch.stack([eq_pref[i], lt_b[i]], dim=-1)
w = pop[f'{cmp_prefix}.cascade.lt.bit{i}.weight'].view(pop_size, 2)
bb = pop[f'{cmp_prefix}.cascade.lt.bit{i}.bias'].view(pop_size)
casc_lt.append(heaviside((inp * w).sum(-1) + bb))
# Final OR for GT / LT
gt_stack = torch.stack(casc_gt, dim=-1)
w = pop[f'{out_gt}.weight'].view(pop_size, bits)
bb = pop[f'{out_gt}.bias'].view(pop_size)
final_gt = heaviside((gt_stack * w).sum(-1) + bb)
lt_stack = torch.stack(casc_lt, dim=-1)
w = pop[f'{out_lt}.weight'].view(pop_size, bits)
bb = pop[f'{out_lt}.bias'].view(pop_size)
final_lt = heaviside((lt_stack * w).sum(-1) + bb)
# Final AND for EQ
eq_stack = torch.stack(eq_b, dim=-1)
w = pop[f'{out_eq}.weight'].view(pop_size, bits)
bb = pop[f'{out_eq}.bias'].view(pop_size)
final_eq = heaviside((eq_stack * w).sum(-1) + bb)
# GE = NOT(LT) buffer pair, LE = NOT(GT) buffer pair
w = pop[f'{out_ge}.not_lt.weight'].view(pop_size)
bb = pop[f'{out_ge}.not_lt.bias'].view(pop_size)
not_lt = heaviside(final_lt * w + bb)
w = pop[f'{out_ge}.weight'].view(pop_size)
bb = pop[f'{out_ge}.bias'].view(pop_size)
final_ge = heaviside(not_lt * w + bb)
w = pop[f'{out_le}.not_gt.weight'].view(pop_size)
bb = pop[f'{out_le}.not_gt.bias'].view(pop_size)
not_gt = heaviside(final_gt * w + bb)
w = pop[f'{out_le}.weight'].view(pop_size)
bb = pop[f'{out_le}.bias'].view(pop_size)
final_le = heaviside(not_gt * w + bb)
return {
"gt": final_gt, "lt": final_lt, "eq": final_eq,
"ge": final_ge, "le": final_le,
}
def _test_comparators(self, pop: Dict, debug: bool) -> Tuple[torch.Tensor, int]:
"""Test 8-bit comparators (bit-cascade)."""
pop_size = next(iter(pop.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
total = 0
if debug:
print("\n=== COMPARATORS (8-bit bit-cascade) ===")
bits = 8
a_bits = torch.stack([((self.comp_a >> (bits - 1 - i)) & 1).float() for i in range(bits)], dim=1)
b_bits = torch.stack([((self.comp_b >> (bits - 1 - i)) & 1).float() for i in range(bits)], dim=1)
try:
outs = self._eval_bit_cascade_compare(
pop,
f"arithmetic.cmp{bits}bit",
f"arithmetic.greaterthan{bits}bit",
f"arithmetic.lessthan{bits}bit",
f"arithmetic.greaterorequal{bits}bit",
f"arithmetic.lessorequal{bits}bit",
f"arithmetic.equality{bits}bit",
bits,
a_bits,
b_bits,
)
except KeyError:
return scores, total
for kind, op in [
("gt", lambda a, b: a > b),
("lt", lambda a, b: a < b),
("ge", lambda a, b: a >= b),
("le", lambda a, b: a <= b),
("eq", lambda a, b: a == b),
]:
expected = torch.tensor(
[1.0 if op(a.item(), b.item()) else 0.0 for a, b in zip(self.comp_a, self.comp_b)],
device=self.device,
)
out = outs[kind]
correct = (out == expected.unsqueeze(1)).float().sum(0)
scores += correct
total += len(self.comp_a)
name_map = {
"gt": f"arithmetic.greaterthan{bits}bit",
"lt": f"arithmetic.lessthan{bits}bit",
"ge": f"arithmetic.greaterorequal{bits}bit",
"le": f"arithmetic.lessorequal{bits}bit",
"eq": f"arithmetic.equality{bits}bit",
}
failures = []
if pop_size == 1:
for i in range(len(self.comp_a)):
if out[i, 0].item() != expected[i].item():
failures.append((
[int(self.comp_a[i].item()), int(self.comp_b[i].item())],
expected[i].item(),
out[i, 0].item(),
))
self._record(name_map[kind], int(correct[0].item()), len(self.comp_a), failures)
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
return scores, total
def _test_comparators_nbits(self, pop: Dict, bits: int, debug: bool) -> Tuple[torch.Tensor, int]:
"""Test N-bit comparator circuits (GT, LT, GE, LE, EQ) via bit-cascade."""
pop_size = next(iter(pop.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
total = 0
if debug:
print(f"\n=== {bits}-BIT COMPARATORS (bit-cascade) ===")
if bits == 32:
comp_a = self.comp32_a
comp_b = self.comp32_b
elif bits == 16:
comp_a = self.comp_a.clamp(0, 65535)
comp_b = self.comp_b.clamp(0, 65535)
else:
comp_a = self.comp_a
comp_b = self.comp_b
num_tests = len(comp_a)
a_bits = torch.stack([((comp_a >> (bits - 1 - i)) & 1).float() for i in range(bits)], dim=1)
b_bits = torch.stack([((comp_b >> (bits - 1 - i)) & 1).float() for i in range(bits)], dim=1)
try:
outs = self._eval_bit_cascade_compare(
pop,
f"arithmetic.cmp{bits}bit",
f"arithmetic.greaterthan{bits}bit",
f"arithmetic.lessthan{bits}bit",
f"arithmetic.greaterorequal{bits}bit",
f"arithmetic.lessorequal{bits}bit",
f"arithmetic.equality{bits}bit",
bits,
a_bits,
b_bits,
)
except KeyError:
return scores, total
for kind, op in [
("gt", lambda a, b: a > b),
("lt", lambda a, b: a < b),
("ge", lambda a, b: a >= b),
("le", lambda a, b: a <= b),
("eq", lambda a, b: a == b),
]:
expected = torch.tensor(
[1.0 if op(a.item(), b.item()) else 0.0 for a, b in zip(comp_a, comp_b)],
device=self.device,
)
out = outs[kind]
correct = (out == expected.unsqueeze(1)).float().sum(0)
scores += correct
total += num_tests
name_map = {
"gt": f"arithmetic.greaterthan{bits}bit",
"lt": f"arithmetic.lessthan{bits}bit",
"ge": f"arithmetic.greaterorequal{bits}bit",
"le": f"arithmetic.lessorequal{bits}bit",
"eq": f"arithmetic.equality{bits}bit",
}
failures = []
if pop_size == 1:
for i in range(num_tests):
if out[i, 0].item() != expected[i].item():
failures.append((
[int(comp_a[i].item()), int(comp_b[i].item())],
expected[i].item(),
out[i, 0].item(),
))
self._record(name_map[kind], int(correct[0].item()), num_tests, failures)
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
return scores, total
def _test_subtractor_nbits(self, pop: Dict, bits: int, debug: bool) -> Tuple[torch.Tensor, int]:
"""Test N-bit subtractor circuit (A - B)."""
pop_size = next(iter(pop.values())).shape[0]
if debug:
print(f"\n=== {bits}-BIT SUBTRACTOR ===")
prefix = f'arithmetic.sub{bits}bit'
max_val = 1 << bits
if bits == 32:
test_pairs = [
(1000, 500), (5000, 3000), (1000000, 500000),
(0xFFFFFFFF, 1), (0x80000000, 1), (100, 100),
(0, 0), (1, 0), (0, 1), (256, 255),
(0xDEADBEEF, 0xCAFEBABE), (1000000000, 999999999),
]
else:
test_pairs = [(a, b) for a in [0, 1, 127, 128, 255] for b in [0, 1, 127, 128, 255]]
a_vals = torch.tensor([p[0] for p in test_pairs], device=self.device, dtype=torch.long)
b_vals = torch.tensor([p[1] for p in test_pairs], device=self.device, dtype=torch.long)
num_tests = len(test_pairs)
a_bits = torch.stack([((a_vals >> (bits - 1 - i)) & 1).float() for i in range(bits)], dim=1)
b_bits = torch.stack([((b_vals >> (bits - 1 - i)) & 1).float() for i in range(bits)], dim=1)
not_b_bits = torch.zeros_like(b_bits)
for bit in range(bits):
w = pop[f'{prefix}.not_b.bit{bit}.weight'].view(pop_size, -1)
b = pop[f'{prefix}.not_b.bit{bit}.bias'].view(pop_size)
not_b_bits[:, bit] = heaviside(b_bits[:, bit:bit+1] @ w.T + b)[:, 0]
carry = torch.ones(num_tests, pop_size, device=self.device)
sum_bits = []
for bit in range(bits):
bit_idx = bits - 1 - bit
s, carry = self._eval_single_fa(
pop, f'{prefix}.fa{bit}',
a_bits[:, bit_idx].unsqueeze(1).expand(-1, pop_size),
not_b_bits[:, bit_idx].unsqueeze(1).expand(-1, pop_size),
carry
)
sum_bits.append(s)
sum_bits = torch.stack(sum_bits[::-1], dim=-1)
result = torch.zeros(num_tests, pop_size, device=self.device, dtype=torch.float64)
for i in range(bits):
result += sum_bits[:, :, i].double() * (1 << (bits - 1 - i))
expected = ((a_vals - b_vals) & (max_val - 1)).unsqueeze(1).expand(-1, pop_size).double()
correct = (result == expected).float().sum(0)
failures = []
if pop_size == 1:
for i in range(min(num_tests, 20)):
if result[i, 0].item() != expected[i, 0].item():
failures.append((
[int(a_vals[i].item()), int(b_vals[i].item())],
int(expected[i, 0].item()),
int(result[i, 0].item())
))
self._record(prefix, int(correct[0].item()), num_tests, failures)
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
return correct, num_tests
def _test_bitwise_nbits(self, pop: Dict, bits: int, debug: bool) -> Tuple[torch.Tensor, int]:
"""Test N-bit bitwise operations (AND, OR, XOR, NOT)."""
pop_size = next(iter(pop.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
total = 0
if debug:
print(f"\n=== {bits}-BIT BITWISE OPS ===")
if bits == 32:
test_pairs = [
(0xAAAAAAAA, 0x55555555), (0xFFFFFFFF, 0x00000000),
(0x12345678, 0x87654321), (0xDEADBEEF, 0xCAFEBABE),
(0x0F0F0F0F, 0xF0F0F0F0), (0, 0), (0xFFFFFFFF, 0xFFFFFFFF),
]
else:
test_pairs = [(0xAA, 0x55), (0xFF, 0x00), (0x0F, 0xF0)]
a_vals = torch.tensor([p[0] for p in test_pairs], device=self.device, dtype=torch.long)
b_vals = torch.tensor([p[1] for p in test_pairs], device=self.device, dtype=torch.long)
num_tests = len(test_pairs)
ops = [
('and', lambda a, b: a & b),
('or', lambda a, b: a | b),
('xor', lambda a, b: a ^ b),
]
for op_name, op_fn in ops:
try:
result_bits = []
for bit in range(bits):
a_bit = ((a_vals >> (bits - 1 - bit)) & 1).float().unsqueeze(1).expand(-1, pop_size)
b_bit = ((b_vals >> (bits - 1 - bit)) & 1).float().unsqueeze(1).expand(-1, pop_size)
inp = torch.stack([a_bit, b_bit], dim=-1) # [tests, pop, 2]
if op_name == 'xor':
prefix = f'alu.alu{bits}bit.{op_name}.bit{bit}'
w_or = pop[f'{prefix}.layer1.or.weight'].view(pop_size, 2)
b_or = pop[f'{prefix}.layer1.or.bias'].view(pop_size)
w_nand = pop[f'{prefix}.layer1.nand.weight'].view(pop_size, 2)
b_nand = pop[f'{prefix}.layer1.nand.bias'].view(pop_size)
h_or = heaviside((inp * w_or).sum(-1) + b_or)
h_nand = heaviside((inp * w_nand).sum(-1) + b_nand)
hidden = torch.stack([h_or, h_nand], dim=-1)
w2 = pop[f'{prefix}.layer2.weight'].view(pop_size, 2)
b2 = pop[f'{prefix}.layer2.bias'].view(pop_size)
out = heaviside((hidden * w2).sum(-1) + b2)
else:
w = pop[f'alu.alu{bits}bit.{op_name}.bit{bit}.weight'].view(pop_size, 2)
b = pop[f'alu.alu{bits}bit.{op_name}.bit{bit}.bias'].view(pop_size)
out = heaviside((inp * w).sum(-1) + b)
result_bits.append(out) # [tests, pop]
results = torch.zeros(num_tests, pop_size, device=self.device, dtype=torch.float64)
for i in range(bits):
results += result_bits[i].double() * (1 << (bits - 1 - i))
expected = torch.tensor([op_fn(a.item(), b.item()) for a, b in zip(a_vals, b_vals)],
device=self.device, dtype=torch.float64).unsqueeze(1)
correct = (results == expected).float().sum(0) # [pop]
self._record(f'alu.alu{bits}bit.{op_name}', int(correct[0].item()), num_tests, [])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
scores += correct
total += num_tests
except KeyError as e:
if debug:
print(f" alu.alu{bits}bit.{op_name}: SKIP (missing {e})")
try:
test_vals = a_vals
result_bits = []
for bit in range(bits):
a_bit = ((test_vals >> (bits - 1 - bit)) & 1).float().unsqueeze(1).expand(-1, pop_size)
w = pop[f'alu.alu{bits}bit.not.bit{bit}.weight'].view(pop_size)
b = pop[f'alu.alu{bits}bit.not.bit{bit}.bias'].view(pop_size)
result_bits.append(heaviside(a_bit * w + b))
results = torch.zeros(num_tests, pop_size, device=self.device, dtype=torch.float64)
for i in range(bits):
results += result_bits[i].double() * (1 << (bits - 1 - i))
expected = torch.tensor([(~a.item()) & ((1 << bits) - 1) for a in test_vals],
device=self.device, dtype=torch.float64).unsqueeze(1)
correct = (results == expected).float().sum(0)
self._record(f'alu.alu{bits}bit.not', int(correct[0].item()), num_tests, [])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
scores += correct
total += num_tests
except KeyError as e:
if debug:
print(f" alu.alu{bits}bit.not: SKIP (missing {e})")
return scores, total
def _test_shifts_nbits(self, pop: Dict, bits: int, debug: bool) -> Tuple[torch.Tensor, int]:
"""Test N-bit shift operations (SHL, SHR)."""
pop_size = next(iter(pop.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
total = 0
if debug:
print(f"\n=== {bits}-BIT SHIFTS ===")
if bits == 32:
test_vals = [0x12345678, 0x80000001, 0x00000001, 0xFFFFFFFF, 0x55555555]
else:
test_vals = [0x81, 0x55, 0x01, 0xFF, 0xAA]
a_vals = torch.tensor(test_vals, device=self.device, dtype=torch.long)
num_tests = len(test_vals)
max_val = (1 << bits) - 1
for op_name, op_fn in [('shl', lambda x: (x << 1) & max_val), ('shr', lambda x: x >> 1)]:
try:
result_bits = []
for bit in range(bits):
w = pop[f'alu.alu{bits}bit.{op_name}.bit{bit}.weight'].view(pop_size)
b = pop[f'alu.alu{bits}bit.{op_name}.bit{bit}.bias'].view(pop_size)
if op_name == 'shl':
if bit < bits - 1:
src_bit = ((a_vals >> (bits - 2 - bit)) & 1).float()
else:
src_bit = torch.zeros(num_tests, device=self.device)
else:
if bit > 0:
src_bit = ((a_vals >> (bits - bit)) & 1).float()
else:
src_bit = torch.zeros(num_tests, device=self.device)
out = heaviside(src_bit.unsqueeze(1) * w + b) # [tests, pop]
result_bits.append(out)
results = torch.zeros(num_tests, pop_size, device=self.device, dtype=torch.float64)
for i in range(bits):
results += result_bits[i].double() * (1 << (bits - 1 - i))
expected = torch.tensor([op_fn(a.item()) for a in a_vals],
device=self.device, dtype=torch.float64).unsqueeze(1)
correct = (results == expected).float().sum(0) # [pop]
self._record(f'alu.alu{bits}bit.{op_name}', int(correct[0].item()), num_tests, [])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
scores += correct
total += num_tests
except KeyError as e:
if debug:
print(f" alu.alu{bits}bit.{op_name}: SKIP (missing {e})")
return scores, total
def _test_inc_dec_nbits(self, pop: Dict, bits: int, debug: bool) -> Tuple[torch.Tensor, int]:
"""Test N-bit INC and DEC operations."""
pop_size = next(iter(pop.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
total = 0
if debug:
print(f"\n=== {bits}-BIT INC/DEC ===")
if bits == 32:
test_vals = [0, 1, 0xFFFFFFFF, 0x7FFFFFFF, 0x80000000, 1000000, 0xFFFFFFFE]
else:
test_vals = [0, 1, 254, 255, 127, 128]
a_vals = torch.tensor(test_vals, device=self.device, dtype=torch.long)
num_tests = len(test_vals)
max_val = (1 << bits) - 1
for op_name, op_fn in [('inc', lambda x: (x + 1) & max_val), ('dec', lambda x: (x - 1) & max_val)]:
try:
carry = torch.ones(num_tests, pop_size, device=self.device)
result_bits = []
for bit in range(bits):
a_bit = ((a_vals >> bit) & 1).float().unsqueeze(1).expand(-1, pop_size)
prefix = f'alu.alu{bits}bit.{op_name}.bit{bit}'
w_or = pop[f'{prefix}.xor.layer1.or.weight'].view(pop_size, 2)
b_or = pop[f'{prefix}.xor.layer1.or.bias'].view(pop_size)
w_nand = pop[f'{prefix}.xor.layer1.nand.weight'].view(pop_size, 2)
b_nand = pop[f'{prefix}.xor.layer1.nand.bias'].view(pop_size)
inp = torch.stack([a_bit, carry], dim=-1) # [tests, pop, 2]
h_or = heaviside((inp * w_or).sum(-1) + b_or)
h_nand = heaviside((inp * w_nand).sum(-1) + b_nand)
w2 = pop[f'{prefix}.xor.layer2.weight'].view(pop_size, 2)
b2 = pop[f'{prefix}.xor.layer2.bias'].view(pop_size)
hidden = torch.stack([h_or, h_nand], dim=-1)
xor_out = heaviside((hidden * w2).sum(-1) + b2)
result_bits.append(xor_out)
if op_name == 'inc':
w_carry = pop[f'{prefix}.carry.weight'].view(pop_size, 2)
b_carry = pop[f'{prefix}.carry.bias'].view(pop_size)
carry = heaviside((inp * w_carry).sum(-1) + b_carry)
else:
w_not = pop[f'{prefix}.not_a.weight'].view(pop_size)
b_not = pop[f'{prefix}.not_a.bias'].view(pop_size)
not_a = heaviside(a_bit * w_not + b_not)
w_borrow = pop[f'{prefix}.borrow.weight'].view(pop_size, 2)
b_borrow = pop[f'{prefix}.borrow.bias'].view(pop_size)
binp = torch.stack([not_a, carry], dim=-1)
carry = heaviside((binp * w_borrow).sum(-1) + b_borrow)
# result_bits[bit] is [tests, pop]; bit index is LSB-first
results = torch.zeros(num_tests, pop_size, device=self.device, dtype=torch.float64)
for bit in range(bits):
results += result_bits[bit].double() * (1 << bit)
expected = torch.tensor([op_fn(a.item()) for a in a_vals],
device=self.device, dtype=torch.float64).unsqueeze(1)
correct = (results == expected).float().sum(0) # [pop]
self._record(f'alu.alu{bits}bit.{op_name}', int(correct[0].item()), num_tests, [])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
scores += correct
total += num_tests
except KeyError as e:
if debug:
print(f" alu.alu{bits}bit.{op_name}: SKIP (missing {e})")
return scores, total
def _test_neg_nbits(self, pop: Dict, bits: int, debug: bool) -> Tuple[torch.Tensor, int]:
"""Test N-bit NEG operation (two's complement negation)."""
pop_size = next(iter(pop.values())).shape[0]
if debug:
print(f"\n=== {bits}-BIT NEG ===")
if bits == 32:
test_vals = [0, 1, 0xFFFFFFFF, 0x7FFFFFFF, 0x80000000, 1000, 1000000]
else:
test_vals = [0, 1, 127, 128, 255, 100]
a_vals = torch.tensor(test_vals, device=self.device, dtype=torch.long)
num_tests = len(test_vals)
max_val = (1 << bits) - 1
try:
not_bits = []
for bit in range(bits):
a_bit = ((a_vals >> bit) & 1).float().unsqueeze(1).expand(-1, pop_size)
w = pop[f'alu.alu{bits}bit.neg.not.bit{bit}.weight'].view(pop_size)
b = pop[f'alu.alu{bits}bit.neg.not.bit{bit}.bias'].view(pop_size)
not_bits.append(heaviside(a_bit * w + b))
carry = torch.ones(num_tests, pop_size, device=self.device)
result_bits = []
for bit in range(bits):
prefix = f'alu.alu{bits}bit.neg.inc.bit{bit}'
not_bit = not_bits[bit]
w_or = pop[f'{prefix}.xor.layer1.or.weight'].view(pop_size, 2)
b_or = pop[f'{prefix}.xor.layer1.or.bias'].view(pop_size)
w_nand = pop[f'{prefix}.xor.layer1.nand.weight'].view(pop_size, 2)
b_nand = pop[f'{prefix}.xor.layer1.nand.bias'].view(pop_size)
inp = torch.stack([not_bit, carry], dim=-1) # [tests, pop, 2]
h_or = heaviside((inp * w_or).sum(-1) + b_or)
h_nand = heaviside((inp * w_nand).sum(-1) + b_nand)
w2 = pop[f'{prefix}.xor.layer2.weight'].view(pop_size, 2)
b2 = pop[f'{prefix}.xor.layer2.bias'].view(pop_size)
hidden = torch.stack([h_or, h_nand], dim=-1)
xor_out = heaviside((hidden * w2).sum(-1) + b2)
result_bits.append(xor_out)
w_carry = pop[f'{prefix}.carry.weight'].view(pop_size, 2)
b_carry = pop[f'{prefix}.carry.bias'].view(pop_size)
carry = heaviside((inp * w_carry).sum(-1) + b_carry)
results = torch.zeros(num_tests, pop_size, device=self.device, dtype=torch.float64)
for bit in range(bits):
results += result_bits[bit].double() * (1 << bit)
expected = torch.tensor([(-a.item()) & max_val for a in a_vals],
device=self.device, dtype=torch.float64).unsqueeze(1)
correct = (results == expected).float().sum(0) # [pop]
self._record(f'alu.alu{bits}bit.neg', int(correct[0].item()), num_tests, [])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
return correct, num_tests
except KeyError as e:
if debug:
print(f" alu.alu{bits}bit.neg: SKIP (missing {e})")
return torch.zeros(pop_size, device=self.device), 0
# =========================================================================
# THRESHOLD GATES
# =========================================================================
def _test_threshold_kofn(self, pop: Dict, k: int, name: str, debug: bool) -> Tuple[torch.Tensor, int]:
"""Test k-of-n threshold gate."""
pop_size = next(iter(pop.values())).shape[0]
prefix = f'threshold.{name}'
# Test all 256 8-bit patterns
inputs = self.test_8bit_bits if len(self.test_8bit_bits) == 24 else None
if inputs is None:
test_vals = torch.arange(256, device=self.device, dtype=torch.long)
inputs = torch.stack([((test_vals >> (7 - i)) & 1).float() for i in range(8)], dim=1)
# For k-of-8: output 1 if popcount >= k (for "at least k")
# For exact naming like "oneoutof8", it's exactly k=1
popcounts = inputs.sum(dim=1)
if 'atleast' in name:
expected = (popcounts >= k).float()
elif 'atmost' in name or 'minority' in name:
# minority = popcount <= 3 (less than half of 8)
expected = (popcounts <= k).float()
elif 'exactly' in name:
expected = (popcounts == k).float()
else:
# Standard k-of-n (at least k), including majority (>= 5)
expected = (popcounts >= k).float()
w = pop[f'{prefix}.weight']
b = pop[f'{prefix}.bias']
out = heaviside(inputs @ w.view(pop_size, -1).T + b.view(pop_size))
correct = (out == expected.unsqueeze(1)).float().sum(0)
failures = []
if pop_size == 1:
for i in range(min(len(inputs), 256)):
if out[i, 0].item() != expected[i].item():
val = int(sum(inputs[i, j].item() * (1 << (7 - j)) for j in range(8)))
failures.append((val, expected[i].item(), out[i, 0].item()))
self._record(prefix, int(correct[0].item()), len(inputs), failures[:10])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
return correct, len(inputs)
def _test_threshold_gates(self, pop: Dict, debug: bool) -> Tuple[torch.Tensor, int]:
"""Test all threshold gates."""
pop_size = next(iter(pop.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
total = 0
if debug:
print("\n=== THRESHOLD GATES ===")
# k-of-8 gates
kofn_gates = [
(1, 'oneoutof8'), (2, 'twooutof8'), (3, 'threeoutof8'), (4, 'fouroutof8'),
(5, 'fiveoutof8'), (6, 'sixoutof8'), (7, 'sevenoutof8'), (8, 'alloutof8'),
]
for k, name in kofn_gates:
try:
s, t = self._test_threshold_kofn(pop, k, name, debug)
scores += s
total += t
except KeyError:
pass
# Special gates
special = [
(5, 'majority'), (3, 'minority'),
(4, 'atleastk_4'), (4, 'atmostk_4'), (4, 'exactlyk_4'),
]
for k, name in special:
try:
s, t = self._test_threshold_kofn(pop, k, name, debug)
scores += s
total += t
except KeyError:
pass
return scores, total
# =========================================================================
# MODULAR ARITHMETIC
# =========================================================================
def _test_modular(self, pop: Dict, mod: int, debug: bool) -> Tuple[torch.Tensor, int]:
"""Test modular divisibility circuit."""
pop_size = next(iter(pop.values())).shape[0]
prefix = f'modular.mod{mod}'
inputs = torch.stack([((self.mod_test >> (7 - i)) & 1).float() for i in range(8)], dim=1)
expected = ((self.mod_test % mod) == 0).float()
out = None
# Bit-cascade equality structure (non-power-of-2 moduli)
multiples = list(range(0, 256, mod))
if (multiples
and f'{prefix}.eq.k{multiples[0]}.all.weight' in pop
and f'{prefix}.weight' in pop):
try:
match_outputs = []
for k in multiples:
per_bit = []
for i in range(8):
bit_in = inputs[:, i].unsqueeze(1).expand(-1, pop_size)
w = pop[f'{prefix}.eq.k{k}.bit{i}.match.weight'].view(pop_size)
b = pop[f'{prefix}.eq.k{k}.bit{i}.match.bias'].view(pop_size)
per_bit.append(heaviside(bit_in * w + b))
stacked = torch.stack(per_bit, dim=-1)
w_and = pop[f'{prefix}.eq.k{k}.all.weight'].view(pop_size, 8)
b_and = pop[f'{prefix}.eq.k{k}.all.bias'].view(pop_size)
match_outputs.append(heaviside((stacked * w_and).sum(-1) + b_and))
or_in = torch.stack(match_outputs, dim=-1)
w_or = pop[f'{prefix}.weight'].view(pop_size, len(multiples))
b_or = pop[f'{prefix}.bias'].view(pop_size)
out = heaviside((or_in * w_or).sum(-1) + b_or)
except (KeyError, RuntimeError):
out = None
# Single-layer ternary detector (powers of 2)
if out is None:
try:
w = pop[f'{prefix}.weight']
b = pop[f'{prefix}.bias']
out = heaviside(inputs @ w.view(pop_size, -1).T + b.view(pop_size))
except (KeyError, RuntimeError):
return torch.zeros(pop_size, device=self.device), 0
correct = (out == expected.unsqueeze(1)).float().sum(0)
failures = []
if pop_size == 1:
for i in range(256):
if out[i, 0].item() != expected[i].item():
failures.append((i, expected[i].item(), out[i, 0].item()))
self._record(prefix, int(correct[0].item()), 256, failures[:10])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
return correct, 256
def _test_modular_all(self, pop: Dict, debug: bool) -> Tuple[torch.Tensor, int]:
"""Test all modular arithmetic circuits."""
pop_size = next(iter(pop.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
total = 0
if debug:
print("\n=== MODULAR ARITHMETIC ===")
for mod in range(2, 13):
s, t = self._test_modular(pop, mod, debug)
scores += s
total += t
return scores, total
# =========================================================================
# PATTERN RECOGNITION
# =========================================================================
def _test_pattern(self, pop: Dict, name: str, expected_fn: Callable[[int], float],
debug: bool) -> Tuple[torch.Tensor, int]:
"""Test pattern recognition circuit."""
pop_size = next(iter(pop.values())).shape[0]
prefix = f'pattern_recognition.{name}'
test_vals = torch.arange(256, device=self.device, dtype=torch.long)
inputs = torch.stack([((test_vals >> (7 - i)) & 1).float() for i in range(8)], dim=1)
expected = torch.tensor([expected_fn(v.item()) for v in test_vals], device=self.device)
try:
w = pop[f'{prefix}.weight'].view(pop_size, -1)
b = pop[f'{prefix}.bias'].view(pop_size)
out = heaviside(inputs @ w.T + b)
except KeyError:
return torch.zeros(pop_size, device=self.device), 0
correct = (out == expected.unsqueeze(1)).float().sum(0)
failures = []
if pop_size == 1:
for i in range(256):
if out[i, 0].item() != expected[i].item():
failures.append((i, expected[i].item(), out[i, 0].item()))
self._record(prefix, int(correct[0].item()), 256, failures[:10])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
return correct, 256
def _test_patterns(self, pop: Dict, debug: bool) -> Tuple[torch.Tensor, int]:
"""Test pattern recognition circuits."""
pop_size = next(iter(pop.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
total = 0
if debug:
print("\n=== PATTERN RECOGNITION ===")
# Use correct naming: pattern_recognition.allzeros, pattern_recognition.allones
patterns = [
('allzeros', lambda v: 1.0 if v == 0 else 0.0),
('allones', lambda v: 1.0 if v == 255 else 0.0),
]
for name, fn in patterns:
s, t = self._test_pattern(pop, name, fn, debug)
scores += s
total += t
return scores, total
# =========================================================================
# ERROR DETECTION
# =========================================================================
def _eval_xor_tree_stage(self, pop: Dict, prefix: str, stage: int, idx: int,
a: torch.Tensor, b: torch.Tensor) -> torch.Tensor:
"""Evaluate a single XOR in the parity tree."""
pop_size = next(iter(pop.values())).shape[0]
xor_prefix = f'{prefix}.stage{stage}.xor{idx}'
# Ensure 2D: [256, pop_size]
if a.dim() == 1:
a = a.unsqueeze(1).expand(-1, pop_size)
if b.dim() == 1:
b = b.unsqueeze(1).expand(-1, pop_size)
# Layer 1: OR and NAND
w_or = pop[f'{xor_prefix}.layer1.or.weight'].view(pop_size, 2)
b_or = pop[f'{xor_prefix}.layer1.or.bias'].view(pop_size)
w_nand = pop[f'{xor_prefix}.layer1.nand.weight'].view(pop_size, 2)
b_nand = pop[f'{xor_prefix}.layer1.nand.bias'].view(pop_size)
inputs = torch.stack([a, b], dim=-1) # [256, pop_size, 2]
h_or = heaviside((inputs * w_or).sum(-1) + b_or)
h_nand = heaviside((inputs * w_nand).sum(-1) + b_nand)
# Layer 2
hidden = torch.stack([h_or, h_nand], dim=-1)
w2 = pop[f'{xor_prefix}.layer2.weight'].view(pop_size, 2)
b2 = pop[f'{xor_prefix}.layer2.bias'].view(pop_size)
return heaviside((hidden * w2).sum(-1) + b2)
def _test_parity_xor_tree(self, pop: Dict, prefix: str, debug: bool) -> Tuple[torch.Tensor, int]:
"""Test parity circuit with XOR tree structure."""
pop_size = next(iter(pop.values())).shape[0]
test_vals = torch.arange(256, device=self.device, dtype=torch.long)
inputs = torch.stack([((test_vals >> (7 - i)) & 1).float() for i in range(8)], dim=1)
# XOR of all bits: 1 if odd number of 1s
popcounts = inputs.sum(dim=1)
xor_result = (popcounts.long() % 2).float()
try:
# Stage 1: 4 XORs (pairs of bits)
s1_out = []
for i in range(4):
xor_out = self._eval_xor_tree_stage(pop, prefix, 1, i, inputs[:, i*2], inputs[:, i*2+1])
s1_out.append(xor_out)
# Stage 2: 2 XORs
s2_out = []
for i in range(2):
xor_out = self._eval_xor_tree_stage(pop, prefix, 2, i, s1_out[i*2], s1_out[i*2+1])
s2_out.append(xor_out)
# Stage 3: 1 XOR
s3_out = self._eval_xor_tree_stage(pop, prefix, 3, 0, s2_out[0], s2_out[1])
# Output NOT (for parity checker - inverts the XOR result)
if f'{prefix}.output.not.weight' in pop:
w_not = pop[f'{prefix}.output.not.weight'].view(pop_size)
b_not = pop[f'{prefix}.output.not.bias'].view(pop_size)
out = heaviside(s3_out * w_not + b_not)
# Checker outputs 1 if even parity (XOR=0), so expected is inverted xor_result
expected = 1.0 - xor_result
else:
out = s3_out
expected = xor_result
except KeyError as e:
return torch.zeros(pop_size, device=self.device), 0
correct = (out == expected.unsqueeze(1)).float().sum(0)
failures = []
if pop_size == 1:
for i in range(256):
if out[i, 0].item() != expected[i].item():
failures.append((i, expected[i].item(), out[i, 0].item()))
self._record(prefix, int(correct[0].item()), 256, failures[:10])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
return correct, 256
def _test_error_detection(self, pop: Dict, debug: bool) -> Tuple[torch.Tensor, int]:
"""Test error detection circuits."""
pop_size = next(iter(pop.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
total = 0
if debug:
print("\n=== ERROR DETECTION ===")
# XOR tree parity circuits
for prefix in ['error_detection.paritychecker8bit', 'error_detection.paritygenerator8bit']:
s, t = self._test_parity_xor_tree(pop, prefix, debug)
scores += s
total += t
return scores, total
# =========================================================================
# COMBINATIONAL LOGIC
# =========================================================================
def _test_mux2to1(self, pop: Dict, debug: bool) -> Tuple[torch.Tensor, int]:
"""Test 2-to-1 multiplexer."""
pop_size = next(iter(pop.values())).shape[0]
prefix = 'combinational.multiplexer2to1'
# Inputs: [a, b, sel] -> out = sel ? b : a
inputs = torch.tensor([
[0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1],
[1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 1],
], device=self.device, dtype=torch.float32)
expected = torch.tensor([0, 0, 0, 1, 1, 0, 1, 1], device=self.device, dtype=torch.float32)
try:
w = pop[f'{prefix}.weight']
b = pop[f'{prefix}.bias']
out = heaviside(inputs @ w.view(pop_size, -1).T + b.view(pop_size))
except KeyError:
return torch.zeros(pop_size, device=self.device), 0
correct = (out == expected.unsqueeze(1)).float().sum(0)
failures = []
if pop_size == 1:
for i in range(8):
if out[i, 0].item() != expected[i].item():
failures.append((inputs[i].tolist(), expected[i].item(), out[i, 0].item()))
self._record(prefix, int(correct[0].item()), 8, failures)
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
return correct, 8
def _test_decoder3to8(self, pop: Dict, debug: bool) -> Tuple[torch.Tensor, int]:
"""Test 3-to-8 decoder."""
pop_size = next(iter(pop.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
total = 0
if debug:
print("\n=== DECODER 3-TO-8 ===")
inputs = torch.tensor([
[0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1],
[1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 1],
], device=self.device, dtype=torch.float32)
for out_idx in range(8):
prefix = f'combinational.decoder3to8.out{out_idx}'
expected = torch.zeros(8, device=self.device)
expected[out_idx] = 1.0
try:
w = pop[f'{prefix}.weight']
b = pop[f'{prefix}.bias']
out = heaviside(inputs @ w.view(pop_size, -1).T + b.view(pop_size))
except KeyError:
continue
correct = (out == expected.unsqueeze(1)).float().sum(0)
scores += correct
total += 8
failures = []
if pop_size == 1:
for i in range(8):
if out[i, 0].item() != expected[i].item():
failures.append((inputs[i].tolist(), expected[i].item(), out[i, 0].item()))
self._record(prefix, int(correct[0].item()), 8, failures)
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
return scores, total
def _test_combinational(self, pop: Dict, debug: bool) -> Tuple[torch.Tensor, int]:
"""Test combinational logic circuits."""
pop_size = next(iter(pop.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
total = 0
if debug:
print("\n=== COMBINATIONAL LOGIC ===")
s, t = self._test_mux2to1(pop, debug)
scores += s
total += t
s, t = self._test_decoder3to8(pop, debug)
scores += s
total += t
s, t = self._test_barrel_shifter(pop, debug)
scores += s
total += t
s, t = self._test_priority_encoder(pop, debug)
scores += s
total += t
return scores, total
def _test_barrel_shifter(self, pop: Dict, debug: bool) -> Tuple[torch.Tensor, int]:
"""Test barrel shifter (shift by 0-7 positions)."""
pop_size = next(iter(pop.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
total = 0
if debug:
print("\n=== BARREL SHIFTER ===")
try:
# Test all shift amounts 0-7 with various input patterns
test_vals = [0b10000001, 0b11110000, 0b00001111, 0b10101010, 0xFF]
for val in test_vals:
for shift in range(8):
expected_val = (val << shift) & 0xFF # Left shift
shift_bits = [float((shift >> (2 - i)) & 1) for i in range(3)]
# Process through 3 layers; every intermediate stays
# per-slot ([pop_size]) so population members are
# evaluated independently.
layer_in = [torch.full((pop_size,), float((val >> (7 - i)) & 1),
device=self.device) for i in range(8)]
for layer in range(3):
shift_amount = 1 << (2 - layer) # 4, 2, 1
sel = torch.full((pop_size,), shift_bits[layer], device=self.device)
layer_out = []
for bit in range(8):
prefix = f'combinational.barrelshifter.layer{layer}.bit{bit}'
# NOT sel
w_not = pop[f'{prefix}.not_sel.weight'].view(pop_size)
b_not = pop[f'{prefix}.not_sel.bias'].view(pop_size)
not_sel = heaviside(sel * w_not + b_not)
# Source for shifted value
shifted_src = bit + shift_amount
if shifted_src < 8:
shifted_val = layer_in[shifted_src]
else:
shifted_val = torch.zeros(pop_size, device=self.device)
# AND a: original AND NOT sel
w_and_a = pop[f'{prefix}.and_a.weight'].view(pop_size, 2)
b_and_a = pop[f'{prefix}.and_a.bias'].view(pop_size)
inp_a = torch.stack([layer_in[bit], not_sel], dim=-1)
and_a = heaviside((inp_a * w_and_a).sum(-1) + b_and_a)
# AND b: shifted AND sel
w_and_b = pop[f'{prefix}.and_b.weight'].view(pop_size, 2)
b_and_b = pop[f'{prefix}.and_b.bias'].view(pop_size)
inp_b = torch.stack([shifted_val, sel], dim=-1)
and_b = heaviside((inp_b * w_and_b).sum(-1) + b_and_b)
# OR
w_or = pop[f'{prefix}.or.weight'].view(pop_size, 2)
b_or = pop[f'{prefix}.or.bias'].view(pop_size)
inp_or = torch.stack([and_a, and_b], dim=-1)
layer_out.append(heaviside((inp_or * w_or).sum(-1) + b_or))
layer_in = layer_out
# Check result per slot
result = torch.zeros(pop_size, device=self.device)
for i in range(8):
result += layer_in[i] * (1 << (7 - i))
scores += (result == expected_val).float()
total += 1
self._record('combinational.barrelshifter', int(scores[0].item()), total, [])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
except (KeyError, RuntimeError) as e:
if debug:
print(f" combinational.barrelshifter: SKIP ({e})")
return scores, total
def _test_priority_encoder(self, pop: Dict, debug: bool) -> Tuple[torch.Tensor, int]:
"""Test priority encoder (find highest set bit)."""
pop_size = next(iter(pop.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
total = 0
if debug:
print("\n=== PRIORITY ENCODER ===")
try:
# Test cases: input -> (valid, index of highest bit)
test_cases = [
(0b00000000, 0, 0), # No bits set, valid=0
(0b00000001, 1, 7), # Bit 7 (LSB)
(0b00000010, 1, 6),
(0b00000100, 1, 5),
(0b00001000, 1, 4),
(0b00010000, 1, 3),
(0b00100000, 1, 2),
(0b01000000, 1, 1),
(0b10000000, 1, 0), # Bit 0 (MSB)
(0b10000001, 1, 0), # Multiple bits, highest wins
(0b01010101, 1, 1),
(0b00001111, 1, 4),
(0b11111111, 1, 0),
]
for val, expected_valid, expected_idx in test_cases:
val_bits = torch.tensor([float((val >> (7 - i)) & 1) for i in range(8)],
device=self.device, dtype=torch.float32)
# Valid output: OR of all input bits
w_valid = pop['combinational.priorityencoder.valid.weight'].view(pop_size, 8)
b_valid = pop['combinational.priorityencoder.valid.bias'].view(pop_size)
out_valid = heaviside((val_bits * w_valid).sum(-1) + b_valid)
scores += (out_valid == float(expected_valid)).float()
total += 1
# Index outputs (3 bits)
if expected_valid == 1:
for idx_bit in range(3):
try:
w_idx = pop[f'combinational.priorityencoder.idx{idx_bit}.weight'].view(pop_size, 8)
b_idx = pop[f'combinational.priorityencoder.idx{idx_bit}.bias'].view(pop_size)
out_idx = heaviside((val_bits * w_idx).sum(-1) + b_idx)
expected_bit = (expected_idx >> (2 - idx_bit)) & 1
scores += (out_idx == float(expected_bit)).float()
total += 1
except KeyError:
pass
self._record('combinational.priorityencoder', int(scores[0].item()), total, [])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
except (KeyError, RuntimeError) as e:
if debug:
print(f" combinational.priorityencoder: SKIP ({e})")
return scores, total
def _test_barrel_shifter_nbits(self, pop: Dict, bits: int, debug: bool) -> Tuple[torch.Tensor, int]:
"""Test N-bit barrel shifter (shift by 0 to bits-1 positions)."""
import math
pop_size = next(iter(pop.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
total = 0
num_layers = max(1, math.ceil(math.log2(bits)))
max_val = (1 << bits) - 1
if debug:
print(f"\n=== {bits}-BIT BARREL SHIFTER ===")
prefix = f'combinational.barrelshifter{bits}'
try:
if bits == 16:
test_vals = [0x8001, 0xFF00, 0x00FF, 0xAAAA, 0xFFFF, 0x1234]
elif bits == 32:
test_vals = [0x80000001, 0xFFFF0000, 0x0000FFFF, 0xAAAAAAAA, 0xFFFFFFFF, 0x12345678]
else:
test_vals = [0b10000001, 0b11110000, 0b00001111, 0b10101010, max_val]
num_shifts = min(bits, 8)
for val in test_vals:
for shift in range(num_shifts):
expected_val = (val << shift) & max_val
shift_bits = [float((shift >> (num_layers - 1 - i)) & 1) for i in range(num_layers)]
layer_in = [torch.full((pop_size,), float((val >> (bits - 1 - i)) & 1),
device=self.device) for i in range(bits)]
for layer in range(num_layers):
shift_amount = 1 << (num_layers - 1 - layer)
sel = torch.full((pop_size,), shift_bits[layer], device=self.device)
layer_out = []
for bit in range(bits):
bit_prefix = f'{prefix}.layer{layer}.bit{bit}'
w_not = pop[f'{bit_prefix}.not_sel.weight'].view(pop_size)
b_not = pop[f'{bit_prefix}.not_sel.bias'].view(pop_size)
not_sel = heaviside(sel * w_not + b_not)
shifted_src = bit + shift_amount
if shifted_src < bits:
shifted_val = layer_in[shifted_src]
else:
shifted_val = torch.zeros(pop_size, device=self.device)
w_and_a = pop[f'{bit_prefix}.and_a.weight'].view(pop_size, 2)
b_and_a = pop[f'{bit_prefix}.and_a.bias'].view(pop_size)
inp_a = torch.stack([layer_in[bit], not_sel], dim=-1)
and_a = heaviside((inp_a * w_and_a).sum(-1) + b_and_a)
w_and_b = pop[f'{bit_prefix}.and_b.weight'].view(pop_size, 2)
b_and_b = pop[f'{bit_prefix}.and_b.bias'].view(pop_size)
inp_b = torch.stack([shifted_val, sel], dim=-1)
and_b = heaviside((inp_b * w_and_b).sum(-1) + b_and_b)
w_or = pop[f'{bit_prefix}.or.weight'].view(pop_size, 2)
b_or = pop[f'{bit_prefix}.or.bias'].view(pop_size)
inp_or = torch.stack([and_a, and_b], dim=-1)
layer_out.append(heaviside((inp_or * w_or).sum(-1) + b_or))
layer_in = layer_out
result = torch.zeros(pop_size, device=self.device, dtype=torch.float64)
for i in range(bits):
result += layer_in[i].double() * (1 << (bits - 1 - i))
scores += (result == float(expected_val)).float()
total += 1
self._record(prefix, int(scores[0].item()), total, [])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
except (KeyError, RuntimeError) as e:
if debug:
print(f" {prefix}: SKIP ({e})")
return scores, total
def _test_priority_encoder_nbits(self, pop: Dict, bits: int, debug: bool) -> Tuple[torch.Tensor, int]:
"""Test N-bit priority encoder (find highest set bit)."""
import math
pop_size = next(iter(pop.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
total = 0
out_bits = max(1, math.ceil(math.log2(bits)))
if debug:
print(f"\n=== {bits}-BIT PRIORITY ENCODER ===")
prefix = f'combinational.priorityencoder{bits}'
try:
test_cases = [(0, 0, 0)]
for i in range(bits):
test_cases.append((1 << i, 1, bits - 1 - i))
if bits == 16:
test_cases.extend([
(0x8001, 1, 0), (0x5555, 1, 1), (0x00FF, 1, 8), (0xFFFF, 1, 0)
])
elif bits == 32:
test_cases.extend([
(0x80000001, 1, 0), (0x55555555, 1, 1), (0x0000FFFF, 1, 16), (0xFFFFFFFF, 1, 0)
])
for val, expected_valid, expected_idx in test_cases:
val_bits = torch.tensor([float((val >> (bits - 1 - i)) & 1) for i in range(bits)],
device=self.device, dtype=torch.float32)
w_valid = pop[f'{prefix}.valid.weight'].view(pop_size, bits)
b_valid = pop[f'{prefix}.valid.bias'].view(pop_size)
out_valid = heaviside((val_bits * w_valid).sum(-1) + b_valid)
scores += (out_valid == float(expected_valid)).float()
total += 1
if expected_valid == 1:
any_higher = [None]
for pos in range(1, bits):
w = pop[f'{prefix}.any_higher{pos}.weight'].view(pop_size, -1)
b = pop[f'{prefix}.any_higher{pos}.bias'].view(pop_size)
inp = val_bits[:pos]
out = heaviside((inp * w[:, :len(inp)]).sum(-1) + b)
any_higher.append(out)
is_highest = []
for pos in range(bits):
if pos == 0:
is_high = val_bits[0].unsqueeze(0).expand(pop_size)
else:
w_not = pop[f'{prefix}.is_highest{pos}.not_higher.weight'].view(pop_size)
b_not = pop[f'{prefix}.is_highest{pos}.not_higher.bias'].view(pop_size)
not_higher = heaviside(any_higher[pos] * w_not + b_not)
w_and = pop[f'{prefix}.is_highest{pos}.and.weight'].view(pop_size, -1)
b_and = pop[f'{prefix}.is_highest{pos}.and.bias'].view(pop_size)
inp = torch.stack([val_bits[pos].expand(pop_size), not_higher], dim=-1)
is_high = heaviside((inp * w_and).sum(-1) + b_and)
is_highest.append(is_high)
for idx_bit in range(out_bits):
try:
w_idx = pop[f'{prefix}.out{idx_bit}.weight'].view(pop_size, -1)
b_idx = pop[f'{prefix}.out{idx_bit}.bias'].view(pop_size)
relevant = [is_highest[pos] for pos in range(bits) if (pos >> idx_bit) & 1]
if len(relevant) > 0:
inp = torch.stack(relevant[:w_idx.shape[1]], dim=-1)
out_idx = heaviside((inp * w_idx).sum(-1) + b_idx)
expected_bit = (expected_idx >> idx_bit) & 1
scores += (out_idx == float(expected_bit)).float()
total += 1
except KeyError:
pass
self._record(prefix, int(scores[0].item()), total, [])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
except (KeyError, RuntimeError) as e:
if debug:
print(f" {prefix}: SKIP ({e})")
return scores, total
# =========================================================================
# CONTROL FLOW
# =========================================================================
def _test_conditional_jump(self, pop: Dict, name: str, debug: bool) -> Tuple[torch.Tensor, int]:
"""Test conditional jump circuit (N-bit address aware)."""
pop_size = next(iter(pop.values())).shape[0]
prefix = f'control.{name}'
# Test cases: [pc_bit, target_bit, flag] -> out = flag ? target : pc
inputs = torch.tensor([
[0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1],
[1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 1],
], device=self.device, dtype=torch.float32)
expected = torch.tensor([0, 0, 0, 1, 1, 0, 1, 1], device=self.device, dtype=torch.float32)
scores = torch.zeros(pop_size, device=self.device)
total = 0
pc = inputs[:, 0].unsqueeze(1).expand(-1, pop_size) # [8, pop]
target = inputs[:, 1].unsqueeze(1).expand(-1, pop_size)
flag = inputs[:, 2].unsqueeze(1).expand(-1, pop_size)
for bit in range(self.addr_bits):
bit_prefix = f'{prefix}.bit{bit}'
try:
# NOT sel
w_not = pop[f'{bit_prefix}.not_sel.weight'].view(pop_size)
b_not = pop[f'{bit_prefix}.not_sel.bias'].view(pop_size)
not_sel = heaviside(flag * w_not + b_not)
# AND a (pc AND NOT sel)
w_and_a = pop[f'{bit_prefix}.and_a.weight'].view(pop_size, 2)
b_and_a = pop[f'{bit_prefix}.and_a.bias'].view(pop_size)
inp_a = torch.stack([pc, not_sel], dim=-1)
and_a = heaviside((inp_a * w_and_a).sum(-1) + b_and_a)
# AND b (target AND sel)
w_and_b = pop[f'{bit_prefix}.and_b.weight'].view(pop_size, 2)
b_and_b = pop[f'{bit_prefix}.and_b.bias'].view(pop_size)
inp_b = torch.stack([target, flag], dim=-1)
and_b = heaviside((inp_b * w_and_b).sum(-1) + b_and_b)
# OR
w_or = pop[f'{bit_prefix}.or.weight'].view(pop_size, 2)
b_or = pop[f'{bit_prefix}.or.bias'].view(pop_size)
ab = torch.stack([and_a, and_b], dim=-1) # [8, pop_size, 2]
out = heaviside((ab * w_or).sum(-1) + b_or) # [8, pop_size]
correct = (out == expected.unsqueeze(1)).float().sum(0) # [pop_size]
scores += correct
total += 8
except KeyError:
pass
if total > 0:
self._record(prefix, int(scores[0].item()), total, [])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
return scores, total
def _test_control_flow(self, pop: Dict, debug: bool) -> Tuple[torch.Tensor, int]:
"""Test control flow circuits."""
pop_size = next(iter(pop.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
total = 0
if debug:
print("\n=== CONTROL FLOW ===")
jumps = ['jz', 'jnz', 'jc', 'jnc', 'jn', 'jp', 'jv', 'jnv', 'conditionaljump']
for name in jumps:
s, t = self._test_conditional_jump(pop, name, debug)
scores += s
total += t
# Stack operations
s, t = self._test_stack_ops(pop, debug)
scores += s
total += t
return scores, total
def _test_stack_ops(self, pop: Dict, debug: bool) -> Tuple[torch.Tensor, int]:
"""Test PUSH/POP/RET stack operation circuits (N-bit address aware)."""
pop_size = next(iter(pop.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
total = 0
addr_bits = self.addr_bits
addr_mask = (1 << addr_bits) - 1
if debug:
print(f"\n=== STACK OPERATIONS ({addr_bits}-bit SP) ===")
# Test PUSH SP decrement (addr_bits wide, borrow chain)
try:
# Generate test values appropriate for addr_bits
sp_tests = [0, 1, addr_mask // 2, addr_mask]
if addr_bits >= 8:
sp_tests.append(0x100 & addr_mask)
if addr_bits >= 12:
sp_tests.append(0x1234 & addr_mask)
op_scores = torch.zeros(pop_size, device=self.device)
op_total = 0
for sp_val in sp_tests:
expected_val = (sp_val - 1) & addr_mask
sp_bits = [float((sp_val >> (addr_bits - 1 - i)) & 1) for i in range(addr_bits)]
borrow = torch.ones(pop_size, device=self.device)
out_bits = []
for bit in range(addr_bits - 1, -1, -1): # LSB to MSB
prefix = f'control.push.sp_dec.bit{bit}'
w_or = pop[f'{prefix}.xor.layer1.or.weight'].view(pop_size, 2)
b_or = pop[f'{prefix}.xor.layer1.or.bias'].view(pop_size)
w_nand = pop[f'{prefix}.xor.layer1.nand.weight'].view(pop_size, 2)
b_nand = pop[f'{prefix}.xor.layer1.nand.bias'].view(pop_size)
w2 = pop[f'{prefix}.xor.layer2.weight'].view(pop_size, 2)
b2 = pop[f'{prefix}.xor.layer2.bias'].view(pop_size)
sp_bit = torch.full((pop_size,), sp_bits[bit], device=self.device)
inp = torch.stack([sp_bit, borrow], dim=-1)
h_or = heaviside((inp * w_or).sum(-1) + b_or)
h_nand = heaviside((inp * w_nand).sum(-1) + b_nand)
hidden = torch.stack([h_or, h_nand], dim=-1)
diff_bit = heaviside((hidden * w2).sum(-1) + b2)
out_bits.insert(0, diff_bit)
# Borrow: NOT(sp) AND borrow_in
not_sp = torch.full((pop_size,), 1.0 - sp_bits[bit], device=self.device)
w_borrow = pop[f'{prefix}.borrow.weight'].view(pop_size, 2)
b_borrow = pop[f'{prefix}.borrow.bias'].view(pop_size)
borrow_inp = torch.stack([not_sp, borrow], dim=-1)
borrow = heaviside((borrow_inp * w_borrow).sum(-1) + b_borrow)
out = torch.stack(out_bits, dim=-1)
expected = torch.tensor([((expected_val >> (addr_bits - 1 - i)) & 1) for i in range(addr_bits)],
device=self.device, dtype=torch.float32)
correct = (out == expected.unsqueeze(0)).float().sum(1)
op_scores += correct
op_total += addr_bits
scores += op_scores
total += op_total
self._record('control.push.sp_dec', int(op_scores[0].item()), op_total, [])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
except (KeyError, RuntimeError) as e:
if debug:
print(f" control.push.sp_dec: SKIP ({e})")
# Test POP SP increment (addr_bits wide, carry chain)
try:
op_scores = torch.zeros(pop_size, device=self.device)
op_total = 0
for sp_val in sp_tests:
expected_val = (sp_val + 1) & addr_mask
sp_bits = [float((sp_val >> (addr_bits - 1 - i)) & 1) for i in range(addr_bits)]
carry = torch.ones(pop_size, device=self.device)
out_bits = []
for bit in range(addr_bits - 1, -1, -1): # LSB to MSB
prefix = f'control.pop.sp_inc.bit{bit}'
w_or = pop[f'{prefix}.xor.layer1.or.weight'].view(pop_size, 2)
b_or = pop[f'{prefix}.xor.layer1.or.bias'].view(pop_size)
w_nand = pop[f'{prefix}.xor.layer1.nand.weight'].view(pop_size, 2)
b_nand = pop[f'{prefix}.xor.layer1.nand.bias'].view(pop_size)
w2 = pop[f'{prefix}.xor.layer2.weight'].view(pop_size, 2)
b2 = pop[f'{prefix}.xor.layer2.bias'].view(pop_size)
sp_bit = torch.full((pop_size,), sp_bits[bit], device=self.device)
inp = torch.stack([sp_bit, carry], dim=-1)
h_or = heaviside((inp * w_or).sum(-1) + b_or)
h_nand = heaviside((inp * w_nand).sum(-1) + b_nand)
hidden = torch.stack([h_or, h_nand], dim=-1)
sum_bit = heaviside((hidden * w2).sum(-1) + b2)
out_bits.insert(0, sum_bit)
# Carry: sp AND carry_in
w_carry = pop[f'{prefix}.carry.weight'].view(pop_size, 2)
b_carry = pop[f'{prefix}.carry.bias'].view(pop_size)
carry = heaviside((inp * w_carry).sum(-1) + b_carry)
out = torch.stack(out_bits, dim=-1)
expected = torch.tensor([((expected_val >> (addr_bits - 1 - i)) & 1) for i in range(addr_bits)],
device=self.device, dtype=torch.float32)
correct = (out == expected.unsqueeze(0)).float().sum(1)
op_scores += correct
op_total += addr_bits
scores += op_scores
total += op_total
self._record('control.pop.sp_inc', int(op_scores[0].item()), op_total, [])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
except (KeyError, RuntimeError) as e:
if debug:
print(f" control.pop.sp_inc: SKIP ({e})")
# Test RET address buffer (addr_bits identity gates)
try:
op_scores = torch.zeros(pop_size, device=self.device)
op_total = 0
ret_tests = [0, addr_mask, addr_mask // 2, 1]
if addr_bits >= 12:
ret_tests.append(0x1234 & addr_mask)
for addr_val in ret_tests:
ret_bits_tensor = torch.tensor([float((addr_val >> (addr_bits - 1 - i)) & 1) for i in range(addr_bits)],
device=self.device, dtype=torch.float32)
out_bits = []
for bit in range(addr_bits):
w = pop[f'control.ret.addr.bit{bit}.weight'].view(pop_size)
b = pop[f'control.ret.addr.bit{bit}.bias'].view(pop_size)
out = heaviside(ret_bits_tensor[bit] * w + b)
out_bits.append(out)
out = torch.stack(out_bits, dim=-1)
correct = (out == ret_bits_tensor.unsqueeze(0)).float().sum(1)
op_scores += correct
op_total += addr_bits
scores += op_scores
total += op_total
self._record('control.ret.addr', int(op_scores[0].item()), op_total, [])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
except (KeyError, RuntimeError) as e:
if debug:
print(f" control.ret.addr: SKIP ({e})")
return scores, total
# =========================================================================
# ALU
# =========================================================================
def _test_alu_ops(self, pop: Dict, debug: bool) -> Tuple[torch.Tensor, int]:
"""Test ALU operations (8-bit bitwise)."""
pop_size = next(iter(pop.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
total = 0
if debug:
print("\n=== ALU OPERATIONS ===")
# Test ALU AND/OR/NOT on 8-bit values
# Each ALU op has weight [16] or [8] and bias [8]
# Structured as 8 parallel 2-input (or 1-input for NOT) gates
test_vals = [(0, 0), (255, 255), (0xAA, 0x55), (0x0F, 0xF0)]
# AND: weight [16] = 8 * [2], bias [8]
try:
w = pop['alu.alu8bit.and.weight'].view(pop_size, 8, 2) # [pop, 8, 2]
b = pop['alu.alu8bit.and.bias'].view(pop_size, 8) # [pop, 8]
op_scores = torch.zeros(pop_size, device=self.device)
op_total = 0
for a_val, b_val in test_vals:
a_bits = torch.tensor([((a_val >> (7 - i)) & 1) for i in range(8)],
device=self.device, dtype=torch.float32)
b_bits = torch.tensor([((b_val >> (7 - i)) & 1) for i in range(8)],
device=self.device, dtype=torch.float32)
# [8, 2]
inputs = torch.stack([a_bits, b_bits], dim=-1)
# [pop, 8]
out = heaviside((inputs * w).sum(-1) + b)
expected = torch.tensor([((a_val & b_val) >> (7 - i)) & 1 for i in range(8)],
device=self.device, dtype=torch.float32)
correct = (out == expected.unsqueeze(0)).float().sum(1) # [pop]
op_scores += correct
op_total += 8
scores += op_scores
total += op_total
self._record('alu.alu8bit.and', int(op_scores[0].item()), op_total, [])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
except (KeyError, RuntimeError):
pass
# OR
try:
w = pop['alu.alu8bit.or.weight'].view(pop_size, 8, 2)
b = pop['alu.alu8bit.or.bias'].view(pop_size, 8)
op_scores = torch.zeros(pop_size, device=self.device)
op_total = 0
for a_val, b_val in test_vals:
a_bits = torch.tensor([((a_val >> (7 - i)) & 1) for i in range(8)],
device=self.device, dtype=torch.float32)
b_bits = torch.tensor([((b_val >> (7 - i)) & 1) for i in range(8)],
device=self.device, dtype=torch.float32)
inputs = torch.stack([a_bits, b_bits], dim=-1)
out = heaviside((inputs * w).sum(-1) + b)
expected = torch.tensor([((a_val | b_val) >> (7 - i)) & 1 for i in range(8)],
device=self.device, dtype=torch.float32)
correct = (out == expected.unsqueeze(0)).float().sum(1)
op_scores += correct
op_total += 8
scores += op_scores
total += op_total
self._record('alu.alu8bit.or', int(op_scores[0].item()), op_total, [])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
except (KeyError, RuntimeError):
pass
# NOT
try:
w = pop['alu.alu8bit.not.weight'].view(pop_size, 8)
b = pop['alu.alu8bit.not.bias'].view(pop_size, 8)
op_scores = torch.zeros(pop_size, device=self.device)
op_total = 0
for a_val, _ in test_vals:
a_bits = torch.tensor([((a_val >> (7 - i)) & 1) for i in range(8)],
device=self.device, dtype=torch.float32)
out = heaviside(a_bits * w + b)
expected = torch.tensor([(((~a_val) & 0xFF) >> (7 - i)) & 1 for i in range(8)],
device=self.device, dtype=torch.float32)
correct = (out == expected.unsqueeze(0)).float().sum(1)
op_scores += correct
op_total += 8
scores += op_scores
total += op_total
self._record('alu.alu8bit.not', int(op_scores[0].item()), op_total, [])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
except (KeyError, RuntimeError):
pass
# SHL (shift left)
try:
op_scores = torch.zeros(pop_size, device=self.device)
op_total = 0
for a_val, _ in test_vals:
expected_val = (a_val << 1) & 0xFF
a_bits = torch.tensor([((a_val >> (7 - i)) & 1) for i in range(8)],
device=self.device, dtype=torch.float32)
out_bits = []
for bit in range(8):
w = pop[f'alu.alu8bit.shl.bit{bit}.weight'].view(pop_size)
b = pop[f'alu.alu8bit.shl.bit{bit}.bias'].view(pop_size)
if bit < 7:
inp = a_bits[bit + 1].unsqueeze(0).expand(pop_size)
else:
inp = torch.zeros(pop_size, device=self.device)
out = heaviside(inp * w + b)
out_bits.append(out)
out = torch.stack(out_bits, dim=-1) # [pop, 8]
expected = torch.tensor([((expected_val >> (7 - i)) & 1) for i in range(8)],
device=self.device, dtype=torch.float32)
correct = (out == expected.unsqueeze(0)).float().sum(1)
op_scores += correct
op_total += 8
scores += op_scores
total += op_total
self._record('alu.alu8bit.shl', int(op_scores[0].item()), op_total, [])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
except (KeyError, RuntimeError) as e:
if debug:
print(f" alu.alu8bit.shl: SKIP ({e})")
# SHR (shift right)
try:
op_scores = torch.zeros(pop_size, device=self.device)
op_total = 0
for a_val, _ in test_vals:
expected_val = (a_val >> 1) & 0xFF
a_bits = torch.tensor([((a_val >> (7 - i)) & 1) for i in range(8)],
device=self.device, dtype=torch.float32)
out_bits = []
for bit in range(8):
w = pop[f'alu.alu8bit.shr.bit{bit}.weight'].view(pop_size)
b = pop[f'alu.alu8bit.shr.bit{bit}.bias'].view(pop_size)
if bit > 0:
inp = a_bits[bit - 1].unsqueeze(0).expand(pop_size)
else:
inp = torch.zeros(pop_size, device=self.device)
out = heaviside(inp * w + b)
out_bits.append(out)
out = torch.stack(out_bits, dim=-1) # [pop, 8]
expected = torch.tensor([((expected_val >> (7 - i)) & 1) for i in range(8)],
device=self.device, dtype=torch.float32)
correct = (out == expected.unsqueeze(0)).float().sum(1)
op_scores += correct
op_total += 8
scores += op_scores
total += op_total
self._record('alu.alu8bit.shr', int(op_scores[0].item()), op_total, [])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
except (KeyError, RuntimeError) as e:
if debug:
print(f" alu.alu8bit.shr: SKIP ({e})")
# MUL (partial products only - just verify AND gates work)
try:
op_scores = torch.zeros(pop_size, device=self.device)
op_total = 0
mul_tests = [(3, 4), (7, 8), (15, 17), (0, 255)]
for a_val, b_val in mul_tests:
a_bits = torch.tensor([((a_val >> (7 - i)) & 1) for i in range(8)],
device=self.device, dtype=torch.float32)
b_bits = torch.tensor([((b_val >> (7 - i)) & 1) for i in range(8)],
device=self.device, dtype=torch.float32)
# Test partial product AND gates
for i in range(8):
for j in range(8):
w = pop[f'alu.alu8bit.mul.pp.a{i}b{j}.weight'].view(pop_size, 2)
b = pop[f'alu.alu8bit.mul.pp.a{i}b{j}.bias'].view(pop_size)
inp = torch.tensor([a_bits[i].item(), b_bits[j].item()], device=self.device)
out = heaviside((inp * w).sum(-1) + b)
expected = float(int(a_bits[i].item()) & int(b_bits[j].item()))
correct = (out == expected).float()
op_scores += correct
op_total += 1
scores += op_scores
total += op_total
self._record('alu.alu8bit.mul', int(op_scores[0].item()), op_total, [])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
except (KeyError, RuntimeError) as e:
if debug:
print(f" alu.alu8bit.mul: SKIP ({e})")
# DIV: drive each stage's bit-cascade GE comparator along the real
# restoring-division remainder trace for each operand pair.
try:
op_scores = torch.zeros(pop_size, device=self.device)
op_total = 0
div_tests = [(100, 10), (255, 17), (50, 7), (128, 16), (255, 255),
(254, 255), (7, 130), (200, 3), (9, 3), (1, 1)]
# Remainder value and expected GE at each stage, per pair.
stage_rems = [[] for _ in range(8)]
stage_ges = [[] for _ in range(8)]
for a_val, b_val in div_tests:
a_bits_int = [(a_val >> (7 - i)) & 1 for i in range(8)]
remainder = 0
for stage in range(8):
remainder = ((remainder << 1) | a_bits_int[stage]) & 0xFF
stage_rems[stage].append(remainder)
ge = 1.0 if remainder >= b_val else 0.0
stage_ges[stage].append(ge)
if ge:
remainder -= b_val
div_vals = torch.tensor([b for _, b in div_tests], device=self.device, dtype=torch.long)
div_bits = torch.stack([((div_vals >> (7 - i)) & 1).float() for i in range(8)], dim=1)
for stage in range(8):
rem_vals = torch.tensor(stage_rems[stage], device=self.device, dtype=torch.long)
rem_bits = torch.stack([((rem_vals >> (7 - i)) & 1).float() for i in range(8)], dim=1)
outs = self._eval_bit_cascade_compare(
pop,
f'alu.alu8bit.div.stage{stage}.cmp_bc',
f'alu.alu8bit.div.stage{stage}.cmp_bc.gt',
f'alu.alu8bit.div.stage{stage}.cmp_bc.lt',
f'alu.alu8bit.div.stage{stage}.cmp',
f'alu.alu8bit.div.stage{stage}.cmp_bc.le',
f'alu.alu8bit.div.stage{stage}.cmp_bc.eq',
8, rem_bits, div_bits,
)
expected = torch.tensor(stage_ges[stage], device=self.device)
correct = (outs['ge'] == expected.unsqueeze(1)).float().sum(0) # [pop]
op_scores += correct
op_total += len(div_tests)
scores += op_scores
total += op_total
self._record('alu.alu8bit.div', int(op_scores[0].item()), op_total, [])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
except (KeyError, RuntimeError) as e:
if debug:
print(f" alu.alu8bit.div: SKIP ({e})")
# INC (increment by 1)
try:
op_scores = torch.zeros(pop_size, device=self.device)
op_total = 0
inc_tests = [0, 1, 127, 128, 254, 255]
for a_val in inc_tests:
expected_val = (a_val + 1) & 0xFF
a_bits = torch.tensor([((a_val >> (7 - i)) & 1) for i in range(8)],
device=self.device, dtype=torch.float32)
# INC uses half-adder chain with initial carry = 1
carry = torch.ones(pop_size, device=self.device)
out_bits = []
for bit in range(7, -1, -1): # LSB to MSB
# XOR for sum
w_or = pop[f'alu.alu8bit.inc.bit{bit}.xor.layer1.or.weight'].view(pop_size, 2)
b_or = pop[f'alu.alu8bit.inc.bit{bit}.xor.layer1.or.bias'].view(pop_size)
w_nand = pop[f'alu.alu8bit.inc.bit{bit}.xor.layer1.nand.weight'].view(pop_size, 2)
b_nand = pop[f'alu.alu8bit.inc.bit{bit}.xor.layer1.nand.bias'].view(pop_size)
w2 = pop[f'alu.alu8bit.inc.bit{bit}.xor.layer2.weight'].view(pop_size, 2)
b2 = pop[f'alu.alu8bit.inc.bit{bit}.xor.layer2.bias'].view(pop_size)
inp = torch.stack([a_bits[bit].expand(pop_size), carry], dim=-1)
h_or = heaviside((inp * w_or).sum(-1) + b_or)
h_nand = heaviside((inp * w_nand).sum(-1) + b_nand)
hidden = torch.stack([h_or, h_nand], dim=-1)
sum_bit = heaviside((hidden * w2).sum(-1) + b2)
out_bits.insert(0, sum_bit)
# AND for carry
w_carry = pop[f'alu.alu8bit.inc.bit{bit}.carry.weight'].view(pop_size, 2)
b_carry = pop[f'alu.alu8bit.inc.bit{bit}.carry.bias'].view(pop_size)
carry = heaviside((inp * w_carry).sum(-1) + b_carry)
out = torch.stack(out_bits, dim=-1)
expected = torch.tensor([((expected_val >> (7 - i)) & 1) for i in range(8)],
device=self.device, dtype=torch.float32)
correct = (out == expected.unsqueeze(0)).float().sum(1)
op_scores += correct
op_total += 8
scores += op_scores
total += op_total
self._record('alu.alu8bit.inc', int(op_scores[0].item()), op_total, [])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
except (KeyError, RuntimeError) as e:
if debug:
print(f" alu.alu8bit.inc: SKIP ({e})")
# DEC (decrement by 1)
try:
op_scores = torch.zeros(pop_size, device=self.device)
op_total = 0
dec_tests = [0, 1, 127, 128, 254, 255]
for a_val in dec_tests:
expected_val = (a_val - 1) & 0xFF
a_bits = torch.tensor([((a_val >> (7 - i)) & 1) for i in range(8)],
device=self.device, dtype=torch.float32)
# DEC uses borrow chain
borrow = torch.ones(pop_size, device=self.device)
out_bits = []
for bit in range(7, -1, -1):
w_or = pop[f'alu.alu8bit.dec.bit{bit}.xor.layer1.or.weight'].view(pop_size, 2)
b_or = pop[f'alu.alu8bit.dec.bit{bit}.xor.layer1.or.bias'].view(pop_size)
w_nand = pop[f'alu.alu8bit.dec.bit{bit}.xor.layer1.nand.weight'].view(pop_size, 2)
b_nand = pop[f'alu.alu8bit.dec.bit{bit}.xor.layer1.nand.bias'].view(pop_size)
w2 = pop[f'alu.alu8bit.dec.bit{bit}.xor.layer2.weight'].view(pop_size, 2)
b2 = pop[f'alu.alu8bit.dec.bit{bit}.xor.layer2.bias'].view(pop_size)
inp = torch.stack([a_bits[bit].expand(pop_size), borrow], dim=-1)
h_or = heaviside((inp * w_or).sum(-1) + b_or)
h_nand = heaviside((inp * w_nand).sum(-1) + b_nand)
hidden = torch.stack([h_or, h_nand], dim=-1)
diff_bit = heaviside((hidden * w2).sum(-1) + b2)
out_bits.insert(0, diff_bit)
# Borrow logic: borrow_out = NOT(a) AND borrow_in
w_not = pop[f'alu.alu8bit.dec.bit{bit}.not_a.weight'].view(pop_size)
b_not = pop[f'alu.alu8bit.dec.bit{bit}.not_a.bias'].view(pop_size)
not_a = heaviside(a_bits[bit] * w_not + b_not)
w_borrow = pop[f'alu.alu8bit.dec.bit{bit}.borrow.weight'].view(pop_size, 2)
b_borrow = pop[f'alu.alu8bit.dec.bit{bit}.borrow.bias'].view(pop_size)
borrow_inp = torch.stack([not_a, borrow], dim=-1)
borrow = heaviside((borrow_inp * w_borrow).sum(-1) + b_borrow)
out = torch.stack(out_bits, dim=-1)
expected = torch.tensor([((expected_val >> (7 - i)) & 1) for i in range(8)],
device=self.device, dtype=torch.float32)
correct = (out == expected.unsqueeze(0)).float().sum(1)
op_scores += correct
op_total += 8
scores += op_scores
total += op_total
self._record('alu.alu8bit.dec', int(op_scores[0].item()), op_total, [])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
except (KeyError, RuntimeError) as e:
if debug:
print(f" alu.alu8bit.dec: SKIP ({e})")
# NEG (two's complement: NOT + 1)
try:
op_scores = torch.zeros(pop_size, device=self.device)
op_total = 0
neg_tests = [0, 1, 127, 128, 255]
for a_val in neg_tests:
expected_val = (-a_val) & 0xFF
a_bits = torch.tensor([((a_val >> (7 - i)) & 1) for i in range(8)],
device=self.device, dtype=torch.float32)
# First NOT each bit
not_bits = []
for bit in range(8):
w = pop[f'alu.alu8bit.neg.not.bit{bit}.weight'].view(pop_size)
b = pop[f'alu.alu8bit.neg.not.bit{bit}.bias'].view(pop_size)
not_bit = heaviside(a_bits[bit] * w + b)
not_bits.append(not_bit)
# Then INC
carry = torch.ones(pop_size, device=self.device)
out_bits = []
for bit in range(7, -1, -1):
w_or = pop[f'alu.alu8bit.neg.inc.bit{bit}.xor.layer1.or.weight'].view(pop_size, 2)
b_or = pop[f'alu.alu8bit.neg.inc.bit{bit}.xor.layer1.or.bias'].view(pop_size)
w_nand = pop[f'alu.alu8bit.neg.inc.bit{bit}.xor.layer1.nand.weight'].view(pop_size, 2)
b_nand = pop[f'alu.alu8bit.neg.inc.bit{bit}.xor.layer1.nand.bias'].view(pop_size)
w2 = pop[f'alu.alu8bit.neg.inc.bit{bit}.xor.layer2.weight'].view(pop_size, 2)
b2 = pop[f'alu.alu8bit.neg.inc.bit{bit}.xor.layer2.bias'].view(pop_size)
inp = torch.stack([not_bits[bit], carry], dim=-1)
h_or = heaviside((inp * w_or).sum(-1) + b_or)
h_nand = heaviside((inp * w_nand).sum(-1) + b_nand)
hidden = torch.stack([h_or, h_nand], dim=-1)
sum_bit = heaviside((hidden * w2).sum(-1) + b2)
out_bits.insert(0, sum_bit)
w_carry = pop[f'alu.alu8bit.neg.inc.bit{bit}.carry.weight'].view(pop_size, 2)
b_carry = pop[f'alu.alu8bit.neg.inc.bit{bit}.carry.bias'].view(pop_size)
carry = heaviside((inp * w_carry).sum(-1) + b_carry)
out = torch.stack(out_bits, dim=-1)
expected = torch.tensor([((expected_val >> (7 - i)) & 1) for i in range(8)],
device=self.device, dtype=torch.float32)
correct = (out == expected.unsqueeze(0)).float().sum(1)
op_scores += correct
op_total += 8
scores += op_scores
total += op_total
self._record('alu.alu8bit.neg', int(op_scores[0].item()), op_total, [])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
except (KeyError, RuntimeError) as e:
if debug:
print(f" alu.alu8bit.neg: SKIP ({e})")
# ROL (rotate left - MSB wraps to LSB)
try:
op_scores = torch.zeros(pop_size, device=self.device)
op_total = 0
rol_tests = [0b10000000, 0b00000001, 0b10101010, 0b01010101, 0xFF, 0x00]
for a_val in rol_tests:
expected_val = ((a_val << 1) | (a_val >> 7)) & 0xFF
a_bits = torch.tensor([((a_val >> (7 - i)) & 1) for i in range(8)],
device=self.device, dtype=torch.float32)
out_bits = []
for bit in range(8):
w = pop[f'alu.alu8bit.rol.bit{bit}.weight'].view(pop_size)
b = pop[f'alu.alu8bit.rol.bit{bit}.bias'].view(pop_size)
# ROL: bit[i] gets bit[i+1], bit[7] gets bit[0]
src_bit = (bit + 1) % 8
out = heaviside(a_bits[src_bit] * w + b)
out_bits.append(out)
out = torch.stack(out_bits, dim=-1)
expected = torch.tensor([((expected_val >> (7 - i)) & 1) for i in range(8)],
device=self.device, dtype=torch.float32)
correct = (out == expected.unsqueeze(0)).float().sum(1)
op_scores += correct
op_total += 8
scores += op_scores
total += op_total
self._record('alu.alu8bit.rol', int(op_scores[0].item()), op_total, [])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
except (KeyError, RuntimeError) as e:
if debug:
print(f" alu.alu8bit.rol: SKIP ({e})")
# ROR (rotate right - LSB wraps to MSB)
try:
op_scores = torch.zeros(pop_size, device=self.device)
op_total = 0
ror_tests = [0b10000000, 0b00000001, 0b10101010, 0b01010101, 0xFF, 0x00]
for a_val in ror_tests:
expected_val = ((a_val >> 1) | (a_val << 7)) & 0xFF
a_bits = torch.tensor([((a_val >> (7 - i)) & 1) for i in range(8)],
device=self.device, dtype=torch.float32)
out_bits = []
for bit in range(8):
w = pop[f'alu.alu8bit.ror.bit{bit}.weight'].view(pop_size)
b = pop[f'alu.alu8bit.ror.bit{bit}.bias'].view(pop_size)
# ROR: bit[i] gets bit[i-1], bit[0] gets bit[7]
src_bit = (bit - 1) % 8
out = heaviside(a_bits[src_bit] * w + b)
out_bits.append(out)
out = torch.stack(out_bits, dim=-1)
expected = torch.tensor([((expected_val >> (7 - i)) & 1) for i in range(8)],
device=self.device, dtype=torch.float32)
correct = (out == expected.unsqueeze(0)).float().sum(1)
op_scores += correct
op_total += 8
scores += op_scores
total += op_total
self._record('alu.alu8bit.ror', int(op_scores[0].item()), op_total, [])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
except (KeyError, RuntimeError) as e:
if debug:
print(f" alu.alu8bit.ror: SKIP ({e})")
return scores, total
# =========================================================================
# MANIFEST
# =========================================================================
def _test_manifest(self, pop: Dict, debug: bool) -> Tuple[torch.Tensor, int]:
"""Verify manifest values."""
pop_size = next(iter(pop.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
total = 0
if debug:
print("\n=== MANIFEST ===")
fixed_expected = {
'manifest.alu_operations': 16.0,
'manifest.flags': 4.0,
'manifest.instruction_width': 16.0,
'manifest.register_width': 8.0,
'manifest.registers': 4.0,
'manifest.version': 4.0,
}
for name, exp_val in fixed_expected.items():
try:
val = pop[name][0, 0].item()
if val == exp_val:
scores += 1
self._record(name, 1, 1, [])
else:
self._record(name, 0, 1, [(exp_val, val)])
total += 1
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
except KeyError:
pass
variable_checks = ['manifest.memory_bytes', 'manifest.pc_width', 'manifest.turing_complete']
for name in variable_checks:
try:
val = pop[name][0, 0].item()
valid = val >= 0
if valid:
scores += 1
self._record(name, 1, 1, [])
else:
self._record(name, 0, 1, [('>=0', val)])
total += 1
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'} (value={val})")
except KeyError:
pass
return scores, total
# =========================================================================
# MEMORY
# =========================================================================
def _test_memory(self, pop: Dict, debug: bool) -> Tuple[torch.Tensor, int]:
"""Test memory circuits (shape validation)."""
pop_size = next(iter(pop.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
total = 0
if debug:
print("\n=== MEMORY ===")
try:
mem_bytes = int(pop['manifest.memory_bytes'][0].item())
addr_bits = int(pop['manifest.pc_width'][0].item())
except KeyError:
mem_bytes = 65536
addr_bits = 16
if mem_bytes == 0:
if debug:
print(" No memory (pure ALU mode)")
return scores, 0
expected_shapes = {
'memory.addr_decode.weight': (mem_bytes, addr_bits),
'memory.addr_decode.bias': (mem_bytes,),
'memory.read.and.weight': (8, mem_bytes, 2),
'memory.read.and.bias': (8, mem_bytes),
'memory.read.or.weight': (8, mem_bytes),
'memory.read.or.bias': (8,),
'memory.write.sel.weight': (mem_bytes, 2),
'memory.write.sel.bias': (mem_bytes,),
'memory.write.nsel.weight': (mem_bytes, 1),
'memory.write.nsel.bias': (mem_bytes,),
'memory.write.and_old.weight': (mem_bytes, 8, 2),
'memory.write.and_old.bias': (mem_bytes, 8),
'memory.write.and_new.weight': (mem_bytes, 8, 2),
'memory.write.and_new.bias': (mem_bytes, 8),
'memory.write.or.weight': (mem_bytes, 8, 2),
'memory.write.or.bias': (mem_bytes, 8),
}
for name, expected_shape in expected_shapes.items():
try:
tensor = pop[name]
actual_shape = tuple(tensor.shape[1:]) # Skip pop_size dimension
if actual_shape == expected_shape:
scores += 1
self._record(name, 1, 1, [])
else:
self._record(name, 0, 1, [(expected_shape, actual_shape)])
total += 1
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
except KeyError:
pass
return scores, total
# =========================================================================
# FLOAT TESTS
#
# unpack/pack buffers and the classify subcircuit are functionally tested
# (inputs driven through the gates, outputs compared to IEEE 754
# semantics). The composed add/mul/div/cmp pipelines are self-contained:
# their full wiring ships as .inputs metadata, so each is reconstructed
# with NetlistEvaluator and evaluated end to end against exact integer
# oracles (round-to-nearest-even, bit-exact to IEEE hardware). The
# remaining per-stage shape checks below just confirm the gate inventory.
# =========================================================================
def _test_float_unpack_pack(self, pop: Dict, family: str, word_bits: int,
debug: bool) -> Tuple[torch.Tensor, int]:
"""Each unpack/pack identity buffer reproduces its input bit."""
pop_size = next(iter(pop.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
total = 0
if debug:
print(f"\n=== {family.upper()} UNPACK/PACK (functional) ===")
inputs = torch.tensor([[0.0], [1.0]], device=self.device)
expected = torch.tensor([0.0, 1.0], device=self.device)
for stage in ("unpack", "pack"):
try:
ok = torch.zeros(pop_size, device=self.device)
t = 0
for i in range(word_bits):
w = pop[f'{family}.{stage}.bit{i}.weight'].view(pop_size, 1)
b = pop[f'{family}.{stage}.bit{i}.bias'].view(pop_size)
out = heaviside(inputs @ w.T + b) # [2, pop]
ok += (out == expected.unsqueeze(1)).float().sum(0)
t += 2
except KeyError:
continue
scores += ok
total += t
self._record(f'{family}.{stage}', int(ok[0].item()), t, [])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
return scores, total
def _test_float_classify(self, pop: Dict, family: str, exp_bits: int,
frac_bits: int, debug: bool) -> Tuple[torch.Tensor, int]:
"""The classify subcircuit against the IEEE 754 categories on edge encodings."""
pop_size = next(iter(pop.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
total = 0
if debug:
print(f"\n=== {family.upper()} CLASSIFY (functional) ===")
exp_max_val = (1 << exp_bits) - 1
frac_max_val = (1 << frac_bits) - 1
# (exponent, fraction) pairs covering every IEEE category at both extremes.
cases = [
(0, 0), # zero
(0, 1), # smallest subnormal
(0, frac_max_val), # largest subnormal
(1, 0), # smallest normal
(exp_max_val - 1, frac_max_val), # largest normal
(exp_max_val // 2, 0), # mid-range normal
(exp_max_val, 0), # infinity
(exp_max_val, 1), # NaN, minimal payload
(exp_max_val, frac_max_val), # NaN, full payload
]
num = len(cases)
exp_vals = torch.tensor([c[0] for c in cases], device=self.device, dtype=torch.long)
frac_vals = torch.tensor([c[1] for c in cases], device=self.device, dtype=torch.long)
exp_in = torch.stack([((exp_vals >> (exp_bits - 1 - i)) & 1).float()
for i in range(exp_bits)], dim=1)
frac_in = torch.stack([((frac_vals >> (frac_bits - 1 - i)) & 1).float()
for i in range(frac_bits)], dim=1)
try:
def gate(name, inp):
w = pop[f'{name}.weight'].view(pop_size, -1)
b = pop[f'{name}.bias'].view(pop_size)
return heaviside(inp @ w.T + b) # [num, pop]
def and_gate(name, x, y):
w = pop[f'{name}.weight'].view(pop_size, 2)
b = pop[f'{name}.bias'].view(pop_size)
inp = torch.stack([x, y], dim=-1)
return heaviside((inp * w).sum(-1) + b)
exp_zero = gate(f'{family}.classify.exp_zero', exp_in)
exp_maxg = gate(f'{family}.classify.exp_max', exp_in)
frac_zero = gate(f'{family}.classify.frac_zero', frac_in)
frac_nz = gate(f'{family}.classify.frac_nonzero', frac_in)
checks = [
(f'{family}.classify.exp_zero', exp_zero,
[e == 0 for e, _ in cases]),
(f'{family}.classify.exp_max', exp_maxg,
[e == exp_max_val for e, _ in cases]),
(f'{family}.classify.frac_zero', frac_zero,
[f == 0 for _, f in cases]),
(f'{family}.classify.frac_nonzero', frac_nz,
[f != 0 for _, f in cases]),
(f'{family}.classify.is_zero.and',
and_gate(f'{family}.classify.is_zero.and', exp_zero, frac_zero),
[e == 0 and f == 0 for e, f in cases]),
(f'{family}.classify.is_subnormal.and',
and_gate(f'{family}.classify.is_subnormal.and', exp_zero, frac_nz),
[e == 0 and f != 0 for e, f in cases]),
(f'{family}.classify.is_inf.and',
and_gate(f'{family}.classify.is_inf.and', exp_maxg, frac_zero),
[e == exp_max_val and f == 0 for e, f in cases]),
(f'{family}.classify.is_nan.and',
and_gate(f'{family}.classify.is_nan.and', exp_maxg, frac_nz),
[e == exp_max_val and f != 0 for e, f in cases]),
]
for name, out, exp_list in checks:
expected = torch.tensor([1.0 if x else 0.0 for x in exp_list],
device=self.device)
correct = (out == expected.unsqueeze(1)).float().sum(0)
scores += correct
total += num
self._record(name, int(correct[0].item()), num, [])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
except KeyError as e:
if debug:
print(f" {family}.classify: SKIP (missing {e})")
return scores, total
def _test_float_cmp_composed(self, pop: Dict, family: str, exp_bits: int,
frac_bits: int, debug: bool) -> Tuple[torch.Tensor, int]:
"""The IEEE comparison rebuilt from the shipped wiring against exact IEEE semantics."""
pop_size = next(iter(pop.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
total = 0
if debug:
print(f"\n=== {family.upper()} CMP (composed, from .inputs netlist) ===")
prefix = f"{family}.cmp"
try:
ne = NetlistEvaluator(pop, self.signal_registry, prefix, pop_size=pop_size)
except (KeyError, ValueError) as e:
if debug:
print(f" {prefix} composed: SKIP ({e})")
return scores, 0
if f"{prefix}.same_sign" not in ne.gates:
if debug:
print(f" {prefix} composed: SKIP (pre-composition wiring)")
return scores, 0
directed, randoms = float_test_words(exp_bits, frac_bits)
pairs = [(x, y) for x in directed for y in directed]
pairs += list(zip(randoms, randoms[1:]))
pairs += [(r, r) for r in randoms[:8]]
W = 1 + exp_bits + frac_bits
a_words = torch.tensor([p[0] for p in pairs], dtype=torch.long)
b_words = torch.tensor([p[1] for p in pairs], dtype=torch.long)
ext = {}
for i in range(W):
ext[f"$a[{i}]"] = ((a_words >> (W - 1 - i)) & 1).float()
ext[f"$b[{i}]"] = ((b_words >> (W - 1 - i)) & 1).float()
try:
out = ne.run(ext)
except KeyError as e:
if debug:
print(f" {prefix} composed: SKIP (unbound signal {e})")
return scores, 0
av = [float_bits_to_value(p[0], exp_bits, frac_bits) for p in pairs]
bv = [float_bits_to_value(p[1], exp_bits, frac_bits) for p in pairs]
ops = [
("eq", lambda x, y: x == y),
("lt", lambda x, y: x < y),
("gt", lambda x, y: x > y),
("le", lambda x, y: x <= y),
("ge", lambda x, y: x >= y),
]
for op, fn in ops:
expected = torch.tensor([1.0 if fn(x, y) else 0.0 for x, y in zip(av, bv)],
device=self.device)
got = out[f"{prefix}.{op}.result"]
correct = (got == expected.unsqueeze(1)).float().sum(0)
scores += correct
total += len(pairs)
failures = []
if pop_size == 1:
for i in range(len(pairs)):
if got[i, 0].item() != expected[i].item():
failures.append((list(pairs[i]), expected[i].item(), got[i, 0].item()))
self._record(f"{prefix}.{op}.composed", int(correct[0].item()), len(pairs), failures[:10])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
for inp, exp_v, got_v in (failures or [])[:4]:
print(f" FAIL: a={inp[0]:#x} b={inp[1]:#x} expected {exp_v}, got {got_v}")
return scores, total
def _test_float_arith_composed(self, pop: Dict, family: str, op: str,
oracle, exp_bits: int, frac_bits: int,
debug: bool) -> Tuple[torch.Tensor, int]:
"""A float pipeline rebuilt from the shipped wiring against the exact integer oracle."""
pop_size = next(iter(pop.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
total = 0
if debug:
print(f"\n=== {family.upper()} {op.upper()} (composed, from .inputs netlist) ===")
prefix = f"{family}.{op}"
try:
ne = NetlistEvaluator(pop, self.signal_registry, prefix, pop_size=pop_size)
except (KeyError, ValueError) as e:
if debug:
print(f" {prefix} composed: SKIP ({e})")
return scores, 0
if (f"{prefix}.sel.norm" not in ne.gates
and f"{prefix}.sel.dp_norm" not in ne.gates):
if debug:
print(f" {prefix} composed: SKIP (pre-composition wiring)")
return scores, 0
E, F = exp_bits, frac_bits
W = 1 + E + F
directed, randoms = float_test_words(E, F)
pairs = [(x, y) for x in directed for y in directed]
pairs += list(zip(randoms, randoms[1:]))
pairs += [(r, r) for r in randoms[:8]]
a_words = torch.tensor([p[0] for p in pairs], dtype=torch.long)
b_words = torch.tensor([p[1] for p in pairs], dtype=torch.long)
ext = {}
for i in range(W):
ext[f"$a[{i}]"] = ((a_words >> (W - 1 - i)) & 1).float()
ext[f"$b[{i}]"] = ((b_words >> (W - 1 - i)) & 1).float()
try:
out = ne.run(ext)
except KeyError as e:
if debug:
print(f" {prefix} composed: SKIP (unbound signal {e})")
return scores, 0
# Assemble the output word: sign, exponent (LSB-first gate index),
# fraction (LSB-first gate index).
got = out[f"{prefix}.sign_out"].double() * float(1 << (E + F))
for k in range(E):
got = got + out[f"{prefix}.exp_out.bit{k}"].double() * float(1 << (F + k))
for k in range(F):
got = got + out[f"{prefix}.frac_out.bit{k}"].double() * float(1 << k)
expected = torch.tensor(
[float(oracle(p[0], p[1], E, F)) for p in pairs], dtype=torch.float64
).unsqueeze(1)
correct = (got == expected).float().sum(0)
scores += correct
total += len(pairs)
failures = []
if pop_size == 1:
for i in range(len(pairs)):
if got[i, 0].item() != expected[i, 0].item():
failures.append((list(pairs[i]), int(expected[i, 0].item()),
int(got[i, 0].item())))
self._record(f"{prefix}.composed", int(correct[0].item()), len(pairs), failures[:10])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
for inp, exp_v, got_v in (failures or [])[:4]:
print(f" FAIL: a={inp[0]:#x} b={inp[1]:#x} expected {exp_v:#x}, got {got_v:#x}")
return scores, total
def _test_float_fma_composed(self, pop: Dict, family: str, exp_bits: int,
frac_bits: int, debug: bool) -> Tuple[torch.Tensor, int]:
"""The fused multiply-add rebuilt from the shipped wiring against the single-rounding oracle."""
pop_size = next(iter(pop.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
prefix = f"{family}.fma"
try:
ne = NetlistEvaluator(pop, self.signal_registry, prefix, pop_size=pop_size)
except (KeyError, ValueError) as e:
if debug:
print(f" {prefix} composed: SKIP ({e})")
return scores, 0
if f"{prefix}.norm" not in ne.gates:
if debug:
print(f" {prefix} composed: SKIP (pre-composition wiring)")
return scores, 0
E, F = exp_bits, frac_bits
W = 1 + E + F
directed, randoms = float_test_words(E, F)
base = directed + randoms[:24]
triples = [(base[i], base[(i * 7 + 3) % len(base)], base[(i * 13 + 5) % len(base)])
for i in range(len(base))]
triples += list(zip(randoms, randoms[1:], randoms[2:]))
a_words = torch.tensor([t[0] for t in triples], dtype=torch.long)
b_words = torch.tensor([t[1] for t in triples], dtype=torch.long)
c_words = torch.tensor([t[2] for t in triples], dtype=torch.long)
ext = {}
for i in range(W):
ext[f"$a[{i}]"] = ((a_words >> (W - 1 - i)) & 1).float()
ext[f"$b[{i}]"] = ((b_words >> (W - 1 - i)) & 1).float()
ext[f"$c[{i}]"] = ((c_words >> (W - 1 - i)) & 1).float()
try:
out = ne.run(ext)
except KeyError as e:
if debug:
print(f" {prefix} composed: SKIP (unbound signal {e})")
return scores, 0
got = out[f"{prefix}.sign_out"].double() * float(1 << (E + F))
for k in range(E):
got = got + out[f"{prefix}.exp_out.bit{k}"].double() * float(1 << (F + k))
for k in range(F):
got = got + out[f"{prefix}.frac_out.bit{k}"].double() * float(1 << k)
expected = torch.tensor(
[float(float_fma_oracle(t[0], t[1], t[2], E, F)) for t in triples],
dtype=torch.float64).unsqueeze(1)
correct = (got == expected).float().sum(0)
scores += correct
failures = []
if pop_size == 1:
for i in range(len(triples)):
if got[i, 0].item() != expected[i, 0].item():
failures.append((list(triples[i]), int(expected[i, 0].item()),
int(got[i, 0].item())))
self._record(f"{prefix}.composed", int(correct[0].item()), len(triples), failures[:10])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
for inp, exp_v, got_v in (failures or [])[:4]:
print(f" FAIL: a={inp[0]:#x} b={inp[1]:#x} c={inp[2]:#x} expected {exp_v:#x}, got {got_v:#x}")
return scores, len(triples)
# =========================================================================
# FLOAT16 STRUCTURE CHECKS
# =========================================================================
def _test_float16_core(self, pop: Dict, debug: bool) -> Tuple[torch.Tensor, int]:
"""Structure (shape) checks for the float16 core gate inventory."""
pop_size = next(iter(pop.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
total = 0
if debug:
print("\n=== FLOAT16 CORE (structure) ===")
expected_gates = [
('float16.unpack.bit0.weight', (1,)),
('float16.classify.exp_zero.weight', (5,)),
('float16.classify.exp_max.weight', (5,)),
('float16.classify.frac_zero.weight', (10,)),
('float16.classify.is_zero.and.weight', (2,)),
('float16.classify.is_nan.and.weight', (2,)),
('float16.pack.bit0.weight', (1,)),
]
for name, expected_shape in expected_gates:
try:
tensor = pop[name]
actual_shape = tuple(tensor.shape[1:])
if actual_shape == expected_shape:
scores += 1
self._record(name, 1, 1, [])
else:
self._record(name, 0, 1, [(expected_shape, actual_shape)])
total += 1
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
except KeyError:
if debug:
print(f" {name}: SKIP (not found)")
return scores, total
def _test_float16_add(self, pop: Dict, debug: bool) -> Tuple[torch.Tensor, int]:
"""Structure (shape) checks for the float16 addition stage gates."""
pop_size = next(iter(pop.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
total = 0
if debug:
print("\n=== FLOAT16 ADD (structure) ===")
expected_gates = [
('float16.add.pl.gt.weight', (15,)), # payload-cascade final OR
('float16.add.exp_diff.fa0.ha1.sum.layer1.or.weight', (2,)),
('float16.add.align.s0.bit0.not_sel.weight', (1,)),
('float16.add.sign_xor.layer1.or.weight', (2,)),
('float16.add.addp.fa0.ha1.sum.layer1.or.weight', (2,)),
('float16.add.subp.not_s.bit0.weight', (1,)),
('float16.add.res.bit0.or.weight', (2,)),
('float16.add.lzc.nz.weight', (14,)),
('float16.add.sel.dp_norm.weight', (3,)),
]
for name, expected_shape in expected_gates:
try:
tensor = pop[name]
actual_shape = tuple(tensor.shape[1:])
if actual_shape == expected_shape:
scores += 1
self._record(name, 1, 1, [])
else:
self._record(name, 0, 1, [(expected_shape, actual_shape)])
total += 1
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
except KeyError:
if debug:
print(f" {name}: SKIP (not found)")
return scores, total
def _test_float16_mul(self, pop: Dict, debug: bool) -> Tuple[torch.Tensor, int]:
"""Structure (shape) checks for the float16 multiplication stage gates."""
pop_size = next(iter(pop.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
total = 0
if debug:
print("\n=== FLOAT16 MUL (structure) ===")
expected_gates = [
('float16.mul.sign_xor.layer1.or.weight', (2,)),
('float16.mul.exp_add.fa0.ha1.sum.layer1.or.weight', (2,)),
('float16.mul.exp_r.fa0.ha1.sum.layer1.or.weight', (2,)),
('float16.mul.mant_mul.pp.a0b0.weight', (2,)),
('float16.mul.mant_mul.acc.s0.fa0.ha1.sum.layer1.or.weight', (2,)),
('float16.mul.norm.bit0.or.weight', (2,)),
('float16.mul.sel.norm.weight', (3,)),
]
for name, expected_shape in expected_gates:
try:
tensor = pop[name]
actual_shape = tuple(tensor.shape[1:])
if actual_shape == expected_shape:
scores += 1
self._record(name, 1, 1, [])
else:
self._record(name, 0, 1, [(expected_shape, actual_shape)])
total += 1
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
except KeyError:
if debug:
print(f" {name}: SKIP (not found)")
return scores, total
def _test_float16_div(self, pop: Dict, debug: bool) -> Tuple[torch.Tensor, int]:
"""Structure (shape) checks for the float16 division stage gates."""
pop_size = next(iter(pop.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
total = 0
if debug:
print("\n=== FLOAT16 DIV (structure) ===")
expected_gates = [
('float16.div.sign_xor.layer1.or.weight', (2,)),
('float16.div.exp_nb.bit0.weight', (1,)),
('float16.div.exp_r.fa0.ha1.sum.layer1.or.weight', (2,)),
('float16.div.mant_div.stage0.cmp.weight', (1,)), # bit-cascaded GE = NOT(LT) buffer
('float16.div.mant_div.stage0.q.weight', (2,)),
('float16.div.mant_div.stage0.sub.not_d.bit0.weight', (1,)),
('float16.div.mant_div.stage0.mux.bit0.not_sel.weight', (1,)),
('float16.div.sel.norm.weight', (3,)),
]
for name, expected_shape in expected_gates:
try:
tensor = pop[name]
actual_shape = tuple(tensor.shape[1:])
if actual_shape == expected_shape:
scores += 1
self._record(name, 1, 1, [])
else:
self._record(name, 0, 1, [(expected_shape, actual_shape)])
total += 1
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
except KeyError:
if debug:
print(f" {name}: SKIP (not found)")
return scores, total
def _test_float16_cmp(self, pop: Dict, debug: bool) -> Tuple[torch.Tensor, int]:
"""Structure (shape) checks for the float16 comparison gate inventory."""
pop_size = next(iter(pop.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
total = 0
if debug:
print("\n=== FLOAT16 CMP (structure) ===")
expected_gates = [
('float16.cmp.a.exp_max.weight', (5,)),
('float16.cmp.a.frac_nz.weight', (10,)),
('float16.cmp.a.is_nan.weight', (2,)),
('float16.cmp.either_nan.weight', (2,)),
('float16.cmp.sign_xor.layer1.or.weight', (2,)),
('float16.cmp.both_zero.weight', (2,)),
('float16.cmp.mag_a_gt_b.weight', (15,)), # bit-cascaded final OR over 15 bits
('float16.cmp.eq.result.weight', (2,)),
('float16.cmp.lt.result.weight', (3,)),
('float16.cmp.gt.result.weight', (3,)),
]
for name, expected_shape in expected_gates:
try:
tensor = pop[name]
actual_shape = tuple(tensor.shape[1:])
if actual_shape == expected_shape:
scores += 1
self._record(name, 1, 1, [])
else:
self._record(name, 0, 1, [(expected_shape, actual_shape)])
total += 1
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
except KeyError:
if debug:
print(f" {name}: SKIP (not found)")
return scores, total
# =========================================================================
# FLOAT32 TESTS
# =========================================================================
def _test_float32_core(self, pop: Dict, debug: bool) -> Tuple[torch.Tensor, int]:
"""Structure (shape) checks for the float32 core gate inventory."""
pop_size = next(iter(pop.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
total = 0
if debug:
print("\n=== FLOAT32 CORE (structure) ===")
expected_gates = [
('float32.unpack.bit0.weight', (1,)),
('float32.classify.exp_zero.weight', (8,)),
('float32.classify.exp_max.weight', (8,)),
('float32.classify.frac_zero.weight', (23,)),
('float32.classify.is_zero.and.weight', (2,)),
('float32.classify.is_nan.and.weight', (2,)),
('float32.pack.bit0.weight', (1,)),
]
for name, expected_shape in expected_gates:
try:
tensor = pop[name]
actual_shape = tuple(tensor.shape[1:])
if actual_shape == expected_shape:
scores += 1
self._record(name, 1, 1, [])
else:
self._record(name, 0, 1, [(expected_shape, actual_shape)])
total += 1
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
except KeyError:
if debug:
print(f" {name}: SKIP (not found)")
return scores, total
def _test_float32_add(self, pop: Dict, debug: bool) -> Tuple[torch.Tensor, int]:
"""Structure (shape) checks for the float32 addition stage gates."""
pop_size = next(iter(pop.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
total = 0
if debug:
print("\n=== FLOAT32 ADD (structure) ===")
expected_gates = [
('float32.add.pl.gt.weight', (31,)), # payload-cascade final OR
('float32.add.exp_diff.fa0.ha1.sum.layer1.or.weight', (2,)),
('float32.add.align.s0.bit0.not_sel.weight', (1,)),
('float32.add.sign_xor.layer1.or.weight', (2,)),
('float32.add.addp.fa0.ha1.sum.layer1.or.weight', (2,)),
('float32.add.subp.not_s.bit0.weight', (1,)),
('float32.add.res.bit0.or.weight', (2,)),
('float32.add.lzc.nz.weight', (27,)),
('float32.add.sel.dp_norm.weight', (3,)),
]
for name, expected_shape in expected_gates:
try:
tensor = pop[name]
actual_shape = tuple(tensor.shape[1:])
if actual_shape == expected_shape:
scores += 1
self._record(name, 1, 1, [])
else:
self._record(name, 0, 1, [(expected_shape, actual_shape)])
total += 1
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
except KeyError:
if debug:
print(f" {name}: SKIP (not found)")
return scores, total
def _test_float32_mul(self, pop: Dict, debug: bool) -> Tuple[torch.Tensor, int]:
"""Structure (shape) checks for the float32 multiplication stage gates."""
pop_size = next(iter(pop.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
total = 0
if debug:
print("\n=== FLOAT32 MUL (structure) ===")
expected_gates = [
('float32.mul.sign_xor.layer1.or.weight', (2,)),
('float32.mul.exp_add.fa0.ha1.sum.layer1.or.weight', (2,)),
('float32.mul.exp_r.fa0.ha1.sum.layer1.or.weight', (2,)),
('float32.mul.mant_mul.pp.a0b0.weight', (2,)),
('float32.mul.mant_mul.acc.s0.fa0.ha1.sum.layer1.or.weight', (2,)),
('float32.mul.norm.bit0.or.weight', (2,)),
('float32.mul.sel.norm.weight', (3,)),
]
for name, expected_shape in expected_gates:
try:
tensor = pop[name]
actual_shape = tuple(tensor.shape[1:])
if actual_shape == expected_shape:
scores += 1
self._record(name, 1, 1, [])
else:
self._record(name, 0, 1, [(expected_shape, actual_shape)])
total += 1
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
except KeyError:
if debug:
print(f" {name}: SKIP (not found)")
return scores, total
def _test_float32_div(self, pop: Dict, debug: bool) -> Tuple[torch.Tensor, int]:
"""Structure (shape) checks for the float32 division stage gates."""
pop_size = next(iter(pop.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
total = 0
if debug:
print("\n=== FLOAT32 DIV (structure) ===")
expected_gates = [
('float32.div.sign_xor.layer1.or.weight', (2,)),
('float32.div.exp_nb.bit0.weight', (1,)),
('float32.div.exp_r.fa0.ha1.sum.layer1.or.weight', (2,)),
('float32.div.mant_div.stage0.cmp.weight', (1,)), # bit-cascaded GE = NOT(LT) buffer
('float32.div.mant_div.stage0.q.weight', (2,)),
('float32.div.mant_div.stage0.sub.not_d.bit0.weight', (1,)),
('float32.div.mant_div.stage0.mux.bit0.not_sel.weight', (1,)),
('float32.div.sel.norm.weight', (3,)),
]
for name, expected_shape in expected_gates:
try:
tensor = pop[name]
actual_shape = tuple(tensor.shape[1:])
if actual_shape == expected_shape:
scores += 1
self._record(name, 1, 1, [])
else:
self._record(name, 0, 1, [(expected_shape, actual_shape)])
total += 1
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
except KeyError:
if debug:
print(f" {name}: SKIP (not found)")
return scores, total
def _test_float32_cmp(self, pop: Dict, debug: bool) -> Tuple[torch.Tensor, int]:
"""Structure (shape) checks for the float32 comparison gate inventory."""
pop_size = next(iter(pop.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
total = 0
if debug:
print("\n=== FLOAT32 CMP (structure) ===")
expected_gates = [
('float32.cmp.a.exp_max.weight', (8,)),
('float32.cmp.a.frac_nz.weight', (23,)),
('float32.cmp.a.is_nan.weight', (2,)),
('float32.cmp.either_nan.weight', (2,)),
('float32.cmp.sign_xor.layer1.or.weight', (2,)),
('float32.cmp.both_zero.weight', (2,)),
('float32.cmp.mag_a_gt_b.weight', (31,)), # bit-cascaded final OR over 31 bits
('float32.cmp.eq.result.weight', (2,)),
('float32.cmp.lt.result.weight', (3,)),
('float32.cmp.gt.result.weight', (3,)),
]
for name, expected_shape in expected_gates:
try:
tensor = pop[name]
actual_shape = tuple(tensor.shape[1:])
if actual_shape == expected_shape:
scores += 1
self._record(name, 1, 1, [])
else:
self._record(name, 0, 1, [(expected_shape, actual_shape)])
total += 1
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
except KeyError:
if debug:
print(f" {name}: SKIP (not found)")
return scores, total
# =========================================================================
# INTEGRATION TESTS (Multi-circuit chains)
# =========================================================================
def _pop_modN(self, pop: Dict, pop_size: int, val_bits: torch.Tensor,
modulus: int) -> torch.Tensor:
"""Drive the bit-cascade modular.mod{N} divisibility detector."""
ks = [k for k in range(256) if k % modulus == 0]
alls = []
for k in ks:
matches = []
for i in range(8):
w = pop[f'modular.mod{modulus}.eq.k{k}.bit{i}.match.weight'].view(pop_size, 1)
b = pop[f'modular.mod{modulus}.eq.k{k}.bit{i}.match.bias'].view(pop_size)
matches.append(heaviside(val_bits[i] * w[:, 0] + b))
all_inp = torch.stack(matches, dim=-1)
w_all = pop[f'modular.mod{modulus}.eq.k{k}.all.weight'].view(pop_size, 8)
b_all = pop[f'modular.mod{modulus}.eq.k{k}.all.bias'].view(pop_size)
alls.append(heaviside((all_inp * w_all).sum(-1) + b_all))
top_inp = torch.stack(alls, dim=-1)
w_top = pop[f'modular.mod{modulus}.weight'].view(pop_size, len(ks))
b_top = pop[f'modular.mod{modulus}.bias'].view(pop_size)
return heaviside((top_inp * w_top).sum(-1) + b_top)
def _pop_cmp8bit(self, pop: Dict, pop_size: int,
a_bits: torch.Tensor, b_bits: torch.Tensor,
kind: str) -> torch.Tensor:
"""Drive the bit-cascade comparator (cmp8bit) over a population."""
def apply(name: str, inp: torch.Tensor, fan_in: int) -> torch.Tensor:
w = pop[f'{name}.weight'].view(pop_size, fan_in)
b = pop[f'{name}.bias'].view(pop_size)
return heaviside((inp * w).sum(-1) + b)
# Per-bit primitives.
bit_gt, bit_lt, bit_eq = [], [], []
for i in range(8):
ab = torch.stack([a_bits[i], b_bits[i]])
bit_gt.append(apply(f'arithmetic.cmp8bit.bit{i}.gt', ab, 2))
bit_lt.append(apply(f'arithmetic.cmp8bit.bit{i}.lt', ab, 2))
eq_and = apply(f'arithmetic.cmp8bit.bit{i}.eq.layer1.and', ab, 2)
eq_nor = apply(f'arithmetic.cmp8bit.bit{i}.eq.layer1.nor', ab, 2)
eq_in = torch.stack([eq_and, eq_nor], dim=-1)
w = pop[f'arithmetic.cmp8bit.bit{i}.eq.weight'].view(pop_size, 2)
b = pop[f'arithmetic.cmp8bit.bit{i}.eq.bias'].view(pop_size)
bit_eq.append(heaviside((eq_in * w).sum(-1) + b))
# Cascade.
cas_gt = [bit_gt[0]]
cas_lt = [bit_lt[0]]
for i in range(1, 8):
eq_pref_in = torch.stack(bit_eq[:i], dim=-1)
w_pref = pop[f'arithmetic.cmp8bit.cascade.eq_prefix.bit{i}.weight'].view(pop_size, i)
b_pref = pop[f'arithmetic.cmp8bit.cascade.eq_prefix.bit{i}.bias'].view(pop_size)
eq_pref = heaviside((eq_pref_in * w_pref).sum(-1) + b_pref)
cas_in = torch.stack([eq_pref, bit_gt[i]], dim=-1)
w_g = pop[f'arithmetic.cmp8bit.cascade.gt.bit{i}.weight'].view(pop_size, 2)
b_g = pop[f'arithmetic.cmp8bit.cascade.gt.bit{i}.bias'].view(pop_size)
cas_gt.append(heaviside((cas_in * w_g).sum(-1) + b_g))
cas_in_lt = torch.stack([eq_pref, bit_lt[i]], dim=-1)
w_l = pop[f'arithmetic.cmp8bit.cascade.lt.bit{i}.weight'].view(pop_size, 2)
b_l = pop[f'arithmetic.cmp8bit.cascade.lt.bit{i}.bias'].view(pop_size)
cas_lt.append(heaviside((cas_in_lt * w_l).sum(-1) + b_l))
if kind == 'gt':
inp = torch.stack(cas_gt, dim=-1)
return apply('arithmetic.greaterthan8bit', inp, 8)
if kind == 'lt':
inp = torch.stack(cas_lt, dim=-1)
return apply('arithmetic.lessthan8bit', inp, 8)
if kind == 'eq':
inp = torch.stack(bit_eq, dim=-1)
return apply('arithmetic.equality8bit', inp, 8)
raise ValueError(kind)
def _test_integration(self, pop: Dict, debug: bool) -> Tuple[torch.Tensor, int]:
"""Test complex operations that chain multiple circuit families."""
pop_size = next(iter(pop.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
total = 0
if debug:
print("\n=== INTEGRATION TESTS ===")
# Test 1: ADD then compare (A + B > C?)
# Uses: ripple carry adder + comparator
try:
op_scores = torch.zeros(pop_size, device=self.device)
op_total = 0
tests = [(10, 20, 25), (100, 50, 200), (255, 1, 0), (0, 0, 1)]
for a, b, c in tests:
sum_val = (a + b) & 0xFF
expected = float(sum_val > c)
# Compute sum bits
sum_bits = torch.tensor([((sum_val >> (7 - i)) & 1) for i in range(8)],
device=self.device, dtype=torch.float32)
c_bits = torch.tensor([((c >> (7 - i)) & 1) for i in range(8)],
device=self.device, dtype=torch.float32)
# Drive sum_bits vs c_bits through the bit-cascade comparator.
out = self._pop_cmp8bit(pop, pop_size, sum_bits, c_bits, 'gt')
correct = (out == expected).float()
op_scores += correct
op_total += 1
scores += op_scores
total += op_total
self._record('integration.add_then_compare', int(op_scores[0].item()), op_total, [])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
except (KeyError, RuntimeError) as e:
if debug:
print(f" integration.add_then_compare: SKIP ({e})")
# Test 2: MUL then MOD (A * B mod 3 == 0?)
# Uses: partial products + modular arithmetic concept
try:
op_scores = torch.zeros(pop_size, device=self.device)
op_total = 0
tests = [(3, 5), (4, 6), (7, 11), (9, 9)]
for a, b in tests:
product = (a * b) & 0xFF
expected = float(product % 3 == 0)
# Drive product bits through the bit-cascade mod3 detector;
# output is 1 iff product is divisible by 3.
prod_bits = torch.tensor([((product >> (7 - i)) & 1) for i in range(8)],
device=self.device, dtype=torch.float32)
out = self._pop_modN(pop, pop_size, prod_bits, 3)
op_scores += (out == expected).float()
op_total += 1
scores += op_scores
total += op_total
self._record('integration.mul_then_mod', int(op_scores[0].item()), op_total, [])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
except (KeyError, RuntimeError) as e:
if debug:
print(f" integration.mul_then_mod: SKIP ({e})")
# Test 3: Shift then AND (SHL(A) & B)
# Uses: shift + bitwise AND
try:
op_scores = torch.zeros(pop_size, device=self.device)
op_total = 0
tests = [(0b10101010, 0b11110000), (0b00001111, 0b01010101), (0xFF, 0x0F)]
for a, b in tests:
shifted_a = (a << 1) & 0xFF
expected = shifted_a & b
a_bits = torch.tensor([((a >> (7 - i)) & 1) for i in range(8)],
device=self.device, dtype=torch.float32)
b_bits = torch.tensor([((b >> (7 - i)) & 1) for i in range(8)],
device=self.device, dtype=torch.float32)
# Apply SHL
shifted_bits = []
for bit in range(8):
w = pop[f'alu.alu8bit.shl.bit{bit}.weight'].view(pop_size)
bias = pop[f'alu.alu8bit.shl.bit{bit}.bias'].view(pop_size)
if bit < 7:
inp = a_bits[bit + 1].expand(pop_size)
else:
inp = torch.zeros(pop_size, device=self.device)
shifted_bits.append(heaviside(inp * w + bias))
# Apply AND
and_bits = []
w_and = pop['alu.alu8bit.and.weight'].view(pop_size, 8, 2)
b_and = pop['alu.alu8bit.and.bias'].view(pop_size, 8)
for bit in range(8):
inp = torch.stack([shifted_bits[bit], b_bits[bit].expand(pop_size)], dim=-1)
and_bits.append(heaviside((inp * w_and[:, bit]).sum(-1) + b_and[:, bit]))
out_val = torch.zeros(pop_size, device=self.device)
for i in range(8):
out_val += and_bits[i] * (1 << (7 - i))
op_scores += (out_val == expected).float()
op_total += 1
scores += op_scores
total += op_total
self._record('integration.shift_then_and', int(op_scores[0].item()), op_total, [])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
except (KeyError, RuntimeError) as e:
if debug:
print(f" integration.shift_then_and: SKIP ({e})")
# Test 4: SUB then conditional (A - B, if result < 0 then NEG)
# Uses: subtractor + comparator + conditional logic
try:
op_scores = torch.zeros(pop_size, device=self.device)
op_total = 0
tests = [(50, 30), (30, 50), (100, 100), (0, 1)]
for a, b in tests:
diff = (a - b) & 0xFF
is_negative = a < b
expected = (-diff & 0xFF) if is_negative else diff
# Just verify the subtraction works correctly
# (Full conditional logic would require control flow)
a_bits = torch.tensor([((a >> (7 - i)) & 1) for i in range(8)],
device=self.device, dtype=torch.float32)
b_bits = torch.tensor([((b >> (7 - i)) & 1) for i in range(8)],
device=self.device, dtype=torch.float32)
# Drive a_bits vs b_bits through the bit-cascade LT comparator.
lt_out = self._pop_cmp8bit(pop, pop_size, a_bits, b_bits, 'lt')
op_scores += (lt_out == float(is_negative)).float()
op_total += 1
scores += op_scores
total += op_total
self._record('integration.sub_then_conditional', int(op_scores[0].item()), op_total, [])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
except (KeyError, RuntimeError) as e:
if debug:
print(f" integration.sub_then_conditional: SKIP ({e})")
# Test 5: Complex expression: ((A + B) * 2) & 0xF0
# Uses: adder + SHL + AND
try:
op_scores = torch.zeros(pop_size, device=self.device)
op_total = 0
tests = [(10, 20), (50, 50), (127, 1), (0, 0)]
for a, b in tests:
sum_val = (a + b) & 0xFF
doubled = (sum_val << 1) & 0xFF
expected = doubled & 0xF0
sum_bits = torch.tensor([((sum_val >> (7 - i)) & 1) for i in range(8)],
device=self.device, dtype=torch.float32)
mask_bits = torch.tensor([1, 1, 1, 1, 0, 0, 0, 0],
device=self.device, dtype=torch.float32)
# Apply SHL
shifted_bits = []
for bit in range(8):
w = pop[f'alu.alu8bit.shl.bit{bit}.weight'].view(pop_size)
bias = pop[f'alu.alu8bit.shl.bit{bit}.bias'].view(pop_size)
if bit < 7:
inp = sum_bits[bit + 1].expand(pop_size)
else:
inp = torch.zeros(pop_size, device=self.device)
shifted_bits.append(heaviside(inp * w + bias))
# Apply AND with mask
w_and = pop['alu.alu8bit.and.weight'].view(pop_size, 8, 2)
b_and = pop['alu.alu8bit.and.bias'].view(pop_size, 8)
result_bits = []
for bit in range(8):
inp = torch.stack([shifted_bits[bit], mask_bits[bit].expand(pop_size)], dim=-1)
result_bits.append(heaviside((inp * w_and[:, bit]).sum(-1) + b_and[:, bit]))
out_val = torch.zeros(pop_size, device=self.device)
for i in range(8):
out_val += result_bits[i] * (1 << (7 - i))
op_scores += (out_val == expected).float()
op_total += 1
scores += op_scores
total += op_total
self._record('integration.complex_expr', int(op_scores[0].item()), op_total, [])
if debug:
r = self.results[-1]
print(f" {r.name}: {r.passed}/{r.total} {'PASS' if r.success else 'FAIL'}")
except (KeyError, RuntimeError) as e:
if debug:
print(f" integration.complex_expr: SKIP ({e})")
return scores, total
# =========================================================================
# MAIN EVALUATE
# =========================================================================
def evaluate(self, population: Dict[str, torch.Tensor], debug: bool = False) -> torch.Tensor:
"""Evaluate population fitness with per-circuit reporting."""
self.results = []
self.category_scores = {}
pop_size = next(iter(population.values())).shape[0]
scores = torch.zeros(pop_size, device=self.device)
total_tests = 0
# Boolean gates
s, t = self._test_boolean_gates(population, debug)
scores += s
total_tests += t
self.category_scores['boolean'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
# Half adder
s, t = self._test_halfadder(population, debug)
scores += s
total_tests += t
self.category_scores['halfadder'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
# Full adder
s, t = self._test_fulladder(population, debug)
scores += s
total_tests += t
self.category_scores['fulladder'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
# Ripple carry adders
for bits in [2, 4, 8]:
s, t = self._test_ripplecarry(population, bits, debug)
scores += s
total_tests += t
self.category_scores[f'ripplecarry{bits}'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
# 16/32-bit circuits (if present)
for bits in [16, 32]:
if f'arithmetic.ripplecarry{bits}bit.fa0.ha1.sum.layer1.or.weight' in population:
if debug:
print(f"\n{'=' * 60}")
print(f" {bits}-BIT CIRCUITS")
print(f"{'=' * 60}")
s, t = self._test_ripplecarry(population, bits, debug)
scores += s
total_tests += t
self.category_scores[f'ripplecarry{bits}'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
s, t = self._test_comparators_nbits(population, bits, debug)
scores += s
total_tests += t
self.category_scores[f'comparators{bits}'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
if f'arithmetic.sub{bits}bit.not_b.bit0.weight' in population:
s, t = self._test_subtractor_nbits(population, bits, debug)
scores += s
total_tests += t
self.category_scores[f'subtractor{bits}'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
if f'alu.alu{bits}bit.and.bit0.weight' in population:
s, t = self._test_bitwise_nbits(population, bits, debug)
scores += s
total_tests += t
self.category_scores[f'bitwise{bits}'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
if f'alu.alu{bits}bit.shl.bit0.weight' in population:
s, t = self._test_shifts_nbits(population, bits, debug)
scores += s
total_tests += t
self.category_scores[f'shifts{bits}'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
if f'alu.alu{bits}bit.inc.bit0.xor.layer1.or.weight' in population:
s, t = self._test_inc_dec_nbits(population, bits, debug)
scores += s
total_tests += t
self.category_scores[f'incdec{bits}'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
if f'alu.alu{bits}bit.neg.not.bit0.weight' in population:
s, t = self._test_neg_nbits(population, bits, debug)
scores += s
total_tests += t
self.category_scores[f'neg{bits}'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
if f'combinational.barrelshifter{bits}.layer0.bit0.not_sel.weight' in population:
s, t = self._test_barrel_shifter_nbits(population, bits, debug)
scores += s
total_tests += t
self.category_scores[f'barrelshifter{bits}'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
if f'combinational.priorityencoder{bits}.valid.weight' in population:
s, t = self._test_priority_encoder_nbits(population, bits, debug)
scores += s
total_tests += t
self.category_scores[f'priorityencoder{bits}'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
# 3-operand adder
s, t = self._test_add3(population, debug)
scores += s
total_tests += t
self.category_scores['add3'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
# Order of operations (A + B × C)
s, t = self._test_expr_add_mul(population, debug)
scores += s
total_tests += t
self.category_scores['expr_add_mul'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
# Comparators
s, t = self._test_comparators(population, debug)
scores += s
total_tests += t
self.category_scores['comparators'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
# Threshold gates
s, t = self._test_threshold_gates(population, debug)
scores += s
total_tests += t
self.category_scores['threshold'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
# Modular arithmetic
s, t = self._test_modular_all(population, debug)
scores += s
total_tests += t
self.category_scores['modular'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
# Pattern recognition
s, t = self._test_patterns(population, debug)
scores += s
total_tests += t
self.category_scores['patterns'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
# Error detection
s, t = self._test_error_detection(population, debug)
scores += s
total_tests += t
self.category_scores['error_detection'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
# Combinational
s, t = self._test_combinational(population, debug)
scores += s
total_tests += t
self.category_scores['combinational'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
# Control flow
s, t = self._test_control_flow(population, debug)
scores += s
total_tests += t
self.category_scores['control'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
# ALU
s, t = self._test_alu_ops(population, debug)
scores += s
total_tests += t
self.category_scores['alu'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
# Manifest
s, t = self._test_manifest(population, debug)
scores += s
total_tests += t
self.category_scores['manifest'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
# Memory
s, t = self._test_memory(population, debug)
scores += s
total_tests += t
self.category_scores['memory'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
# Float16 circuits (if present)
if 'float16.unpack.bit0.weight' in population:
if debug:
print(f"\n{'=' * 60}")
print(f" FLOAT16 CIRCUITS")
print(f"{'=' * 60}")
s, t = self._test_float_unpack_pack(population, 'float16', 16, debug)
scores += s
total_tests += t
self.category_scores['float16_unpack_pack'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
s, t = self._test_float_classify(population, 'float16', 5, 10, debug)
scores += s
total_tests += t
self.category_scores['float16_classify'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
s, t = self._test_float_cmp_composed(population, 'float16', 5, 10, debug)
scores += s
total_tests += t
self.category_scores['float16_cmp_composed'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
s, t = self._test_float_arith_composed(population, 'float16', 'mul',
float_mul_oracle, 5, 10, debug)
scores += s
total_tests += t
self.category_scores['float16_mul_composed'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
s, t = self._test_float_arith_composed(population, 'float16', 'div',
float_div_oracle, 5, 10, debug)
scores += s
total_tests += t
self.category_scores['float16_div_composed'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
s, t = self._test_float_arith_composed(population, 'float16', 'add',
float_add_oracle, 5, 10, debug)
scores += s
total_tests += t
self.category_scores['float16_add_composed'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
if 'float16.fma.norm.weight' in population:
s, t = self._test_float_fma_composed(population, 'float16', 5, 10, debug)
scores += s
total_tests += t
self.category_scores['float16_fma_composed'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
s, t = self._test_float16_core(population, debug)
scores += s
total_tests += t
self.category_scores['float16_core'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
if 'float16.add.pl.gt.weight' in population:
s, t = self._test_float16_add(population, debug)
scores += s
total_tests += t
self.category_scores['float16_add'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
if 'float16.mul.sign_xor.layer1.or.weight' in population:
s, t = self._test_float16_mul(population, debug)
scores += s
total_tests += t
self.category_scores['float16_mul'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
if 'float16.div.sign_xor.layer1.or.weight' in population:
s, t = self._test_float16_div(population, debug)
scores += s
total_tests += t
self.category_scores['float16_div'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
if 'float16.cmp.a.exp_max.weight' in population:
s, t = self._test_float16_cmp(population, debug)
scores += s
total_tests += t
self.category_scores['float16_cmp'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
# Float32 circuits (if present)
if 'float32.unpack.bit0.weight' in population:
if debug:
print(f"\n{'=' * 60}")
print(f" FLOAT32 CIRCUITS")
print(f"{'=' * 60}")
s, t = self._test_float_unpack_pack(population, 'float32', 32, debug)
scores += s
total_tests += t
self.category_scores['float32_unpack_pack'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
s, t = self._test_float_classify(population, 'float32', 8, 23, debug)
scores += s
total_tests += t
self.category_scores['float32_classify'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
s, t = self._test_float_cmp_composed(population, 'float32', 8, 23, debug)
scores += s
total_tests += t
self.category_scores['float32_cmp_composed'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
s, t = self._test_float_arith_composed(population, 'float32', 'mul',
float_mul_oracle, 8, 23, debug)
scores += s
total_tests += t
self.category_scores['float32_mul_composed'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
s, t = self._test_float_arith_composed(population, 'float32', 'div',
float_div_oracle, 8, 23, debug)
scores += s
total_tests += t
self.category_scores['float32_div_composed'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
s, t = self._test_float_arith_composed(population, 'float32', 'add',
float_add_oracle, 8, 23, debug)
scores += s
total_tests += t
self.category_scores['float32_add_composed'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
if 'float32.fma.norm.weight' in population:
s, t = self._test_float_fma_composed(population, 'float32', 8, 23, debug)
scores += s
total_tests += t
self.category_scores['float32_fma_composed'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
s, t = self._test_float32_core(population, debug)
scores += s
total_tests += t
self.category_scores['float32_core'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
if 'float32.add.pl.gt.weight' in population:
s, t = self._test_float32_add(population, debug)
scores += s
total_tests += t
self.category_scores['float32_add'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
if 'float32.mul.sign_xor.layer1.or.weight' in population:
s, t = self._test_float32_mul(population, debug)
scores += s
total_tests += t
self.category_scores['float32_mul'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
if 'float32.div.sign_xor.layer1.or.weight' in population:
s, t = self._test_float32_div(population, debug)
scores += s
total_tests += t
self.category_scores['float32_div'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
if 'float32.cmp.a.exp_max.weight' in population:
s, t = self._test_float32_cmp(population, debug)
scores += s
total_tests += t
self.category_scores['float32_cmp'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
# Cross-family integration tests (chain ripple-carry, comparator,
# modular, shifts, subtractor). Each test is internally guarded with
# try/except, so unsupported variants silently skip individual tests.
if 'arithmetic.cmp8bit.bit0.gt.weight' in population:
s, t = self._test_integration(population, debug)
scores += s
total_tests += t
self.category_scores['integration'] = (s[0].item() if pop_size == 1 else s.mean().item(), t)
self.total_tests = total_tests
if debug:
print("\n" + "=" * 60)
print("CATEGORY SUMMARY")
print("=" * 60)
for cat, (got, expected) in sorted(self.category_scores.items()):
pct = 100 * got / expected if expected > 0 else 0
status = "PASS" if got == expected else "FAIL"
print(f" {cat:20} {int(got):6}/{expected:6} ({pct:6.2f}%) [{status}]")
print("\n" + "=" * 60)
print("CIRCUIT FAILURES")
print("=" * 60)
failed = [r for r in self.results if not r.success]
if failed:
for r in failed[:20]:
print(f" {r.name}: {r.passed}/{r.total}")
if r.failures:
print(f" First failure: {r.failures[0]}")
if len(failed) > 20:
print(f" ... and {len(failed) - 20} more")
else:
print(" None!")
return scores / total_tests if total_tests > 0 else scores
def main():
parser = argparse.ArgumentParser(description='Unified Evaluation Suite for 8-bit Threshold Computer')
parser.add_argument('--model', type=str, default=MODEL_PATH, help='Path to safetensors model')
parser.add_argument('--device', type=str, default='cuda', help='Device: cuda or cpu')
parser.add_argument('--pop_size', type=int, default=1, help='Population size for batched evaluation')
parser.add_argument('--quiet', action='store_true', help='Suppress detailed output')
parser.add_argument('--cpu-test', action='store_true', help='Run CPU smoke test (LOAD, ADD, STORE, HALT)')
args = parser.parse_args()
if args.cpu_test:
return run_smoke_test()
print("=" * 70)
print(" UNIFIED EVALUATION SUITE")
print("=" * 70)
print(f"\nLoading model from {args.model}...")
model = load_model(args.model)
print(f" Loaded {len(model)} tensors, {sum(t.numel() for t in model.values()):,} params")
print(f"\nInitializing evaluator on {args.device}...")
evaluator = BatchedFitnessEvaluator(device=args.device, model_path=args.model)
print(f"\nCreating population (size {args.pop_size})...")
population = create_population(model, pop_size=args.pop_size, device=args.device)
print("\nRunning evaluation...")
if args.device == 'cuda':
torch.cuda.synchronize()
start = time.perf_counter()
fitness = evaluator.evaluate(population, debug=not args.quiet)
if args.device == 'cuda':
torch.cuda.synchronize()
elapsed = time.perf_counter() - start
print("\n" + "=" * 70)
print("RESULTS")
print("=" * 70)
if args.pop_size == 1:
print(f" Fitness: {fitness[0].item():.6f}")
else:
print(f" Mean Fitness: {fitness.mean().item():.6f}")
print(f" Min Fitness: {fitness.min().item():.6f}")
print(f" Max Fitness: {fitness.max().item():.6f}")
print(f" Total tests: {evaluator.total_tests}")
print(f" Time: {elapsed * 1000:.2f} ms")
if args.pop_size > 1:
print(f" Throughput: {args.pop_size / elapsed:.0f} evals/sec")
perfect = (fitness >= 0.9999).sum().item()
print(f" Perfect (>=99.99%): {perfect}/{args.pop_size}")
if fitness[0].item() >= 0.9999:
print("\n STATUS: PASS")
return 0
else:
failed_count = int((1 - fitness[0].item()) * evaluator.total_tests)
print(f"\n STATUS: FAIL ({failed_count} tests failed)")
return 1
# ---------------------------------------------------------------------------
# Variant-aware threshold ALU + CPU
# ---------------------------------------------------------------------------
class GenericThresholdALU:
"""Variant-aware threshold ALU walking the gates of a loaded variant."""
def __init__(self, tensors: Dict[str, torch.Tensor], data_bits: int):
self.T = tensors
self.data_bits = data_bits
def _g(self, name, inputs):
w = self.T[name + ".weight"].view(-1)
b = self.T[name + ".bias"].view(-1)
return int(heaviside((torch.tensor(inputs, dtype=torch.float32) * w).sum() + b).item())
def _xor_or_nand(self, prefix, inputs):
a, b_ = inputs
h_or = self._g(f"{prefix}.layer1.or", [a, b_])
h_nand = self._g(f"{prefix}.layer1.nand", [a, b_])
return self._g(f"{prefix}.layer2", [h_or, h_nand])
def _fa(self, prefix, a, b, cin):
s1 = self._xor_or_nand(f"{prefix}.ha1.sum", [a, b])
c1 = self._g(f"{prefix}.ha1.carry", [a, b])
s2 = self._xor_or_nand(f"{prefix}.ha2.sum", [s1, cin])
c2 = self._g(f"{prefix}.ha2.carry", [s1, cin])
cout = self._g(f"{prefix}.carry_or", [c1, c2])
return s2, cout
def add8(self, a, b):
a_lsb = list(reversed(int_to_bits(a, 8)))
b_lsb = list(reversed(int_to_bits(b, 8)))
carry = 0
s_lsb = []
for i in range(8):
s, carry = self._fa(f"arithmetic.ripplecarry8bit.fa{i}", a_lsb[i], b_lsb[i], carry)
s_lsb.append(s)
return bits_to_int(list(reversed(s_lsb))), carry
def sub8(self, a, b):
a_lsb = list(reversed(int_to_bits(a, 8)))
b_lsb = list(reversed(int_to_bits(b, 8)))
carry = 1
d_lsb = []
for i in range(8):
notb = self._g(f"arithmetic.sub8bit.notb{i}", [b_lsb[i]])
x1 = self._xor_or_nand(f"arithmetic.sub8bit.fa{i}.xor1", [a_lsb[i], notb])
x2 = self._xor_or_nand(f"arithmetic.sub8bit.fa{i}.xor2", [x1, carry])
and1 = self._g(f"arithmetic.sub8bit.fa{i}.and1", [a_lsb[i], notb])
and2 = self._g(f"arithmetic.sub8bit.fa{i}.and2", [x1, carry])
carry = self._g(f"arithmetic.sub8bit.fa{i}.or_carry", [and1, and2])
d_lsb.append(x2)
return bits_to_int(list(reversed(d_lsb))), carry
_CMP_KIND = {"greaterthan": "gt", "lessthan": "lt", "eq": "eq", "equality": "eq",
"greaterorequal": "ge", "lessorequal": "le"}
def _cmp_bit_cascade(self, cmp_prefix: str, finals: Dict[str, str],
a_bits_msb: List[int], b_bits_msb: List[int], kind: str) -> int:
"""Walk an add_bit_cascade_compare structure (bit 0 is the MSB)."""
bits = len(a_bits_msb)
bit_gt, bit_lt, bit_eq = [], [], []
for i in range(bits):
ab = [a_bits_msb[i], b_bits_msb[i]]
bit_gt.append(self._g(f"{cmp_prefix}.bit{i}.gt", ab))
bit_lt.append(self._g(f"{cmp_prefix}.bit{i}.lt", ab))
eq_and = self._g(f"{cmp_prefix}.bit{i}.eq.layer1.and", ab)
eq_nor = self._g(f"{cmp_prefix}.bit{i}.eq.layer1.nor", ab)
bit_eq.append(self._g(f"{cmp_prefix}.bit{i}.eq", [eq_and, eq_nor]))
cas_gt, cas_lt = [bit_gt[0]], [bit_lt[0]]
for i in range(1, bits):
eq_pref = self._g(f"{cmp_prefix}.cascade.eq_prefix.bit{i}", bit_eq[:i])
cas_gt.append(self._g(f"{cmp_prefix}.cascade.gt.bit{i}", [eq_pref, bit_gt[i]]))
cas_lt.append(self._g(f"{cmp_prefix}.cascade.lt.bit{i}", [eq_pref, bit_lt[i]]))
if kind == "gt":
return self._g(finals["gt"], cas_gt)
if kind == "lt":
return self._g(finals["lt"], cas_lt)
if kind == "eq":
return self._g(finals["eq"], bit_eq)
if kind == "ge":
lt = self._g(finals["lt"], cas_lt)
not_lt = self._g(finals["ge"] + ".not_lt", [lt])
return self._g(finals["ge"], [not_lt])
if kind == "le":
gt = self._g(finals["gt"], cas_gt)
not_gt = self._g(finals["le"] + ".not_gt", [gt])
return self._g(finals["le"], [not_gt])
raise ValueError(kind)
def cmp8(self, a, b, kind):
return self.cmp_n(a, b, kind, 8)
def mul8(self, a, b):
ab = int_to_bits(a, 8)
bb = int_to_bits(b, 8)
result = 0
for j in range(8):
if bb[j] == 0:
continue
row = 0
for i in range(8):
pp = self._g(f"alu.alu8bit.mul.pp.a{i}b{j}", [ab[i], bb[j]])
row |= (pp << (7 - i))
shift = 7 - j
result, _ = self.add8(result & 0xFF, (row << shift) & 0xFF)
return result & 0xFF
def _packed_pair_op(self, name: str, a: int, b: int) -> int:
"""8 parallel 2-input gates stored packed: weight [16], bias [8]."""
a_bits = int_to_bits(a, 8)
b_bits = int_to_bits(b, 8)
w = self.T[f"{name}.weight"].view(-1)
bias = self.T[f"{name}.bias"].view(-1)
out = []
for bit in range(8):
inp = torch.tensor([float(a_bits[bit]), float(b_bits[bit])])
out.append(int(heaviside((inp * w[bit * 2:bit * 2 + 2]).sum() + bias[bit]).item()))
return bits_to_int(out)
def and8(self, a, b):
return self._packed_pair_op("alu.alu8bit.and", a, b)
def or8(self, a, b):
return self._packed_pair_op("alu.alu8bit.or", a, b)
def xor8(self, a, b):
a_bits = int_to_bits(a, 8)
b_bits = int_to_bits(b, 8)
w_or = self.T["alu.alu8bit.xor.layer1.or.weight"].view(-1)
b_or = self.T["alu.alu8bit.xor.layer1.or.bias"].view(-1)
w_nand = self.T["alu.alu8bit.xor.layer1.nand.weight"].view(-1)
b_nand = self.T["alu.alu8bit.xor.layer1.nand.bias"].view(-1)
w2 = self.T["alu.alu8bit.xor.layer2.weight"].view(-1)
b2 = self.T["alu.alu8bit.xor.layer2.bias"].view(-1)
out = []
for bit in range(8):
inp = torch.tensor([float(a_bits[bit]), float(b_bits[bit])])
h_or = heaviside((inp * w_or[bit * 2:bit * 2 + 2]).sum() + b_or[bit])
h_nand = heaviside((inp * w_nand[bit * 2:bit * 2 + 2]).sum() + b_nand[bit])
hidden = torch.stack([h_or, h_nand])
out.append(int(heaviside((hidden * w2[bit * 2:bit * 2 + 2]).sum() + b2[bit]).item()))
return bits_to_int(out)
def shl8(self, a):
a_bits = int_to_bits(a, 8)
out = []
for bit in range(8):
src = float(a_bits[bit + 1]) if bit < 7 else 0.0
out.append(self._g(f"alu.alu8bit.shl.bit{bit}", [src]))
return bits_to_int(out)
def shr8(self, a):
a_bits = int_to_bits(a, 8)
out = []
for bit in range(8):
src = float(a_bits[bit - 1]) if bit > 0 else 0.0
out.append(self._g(f"alu.alu8bit.shr.bit{bit}", [src]))
return bits_to_int(out)
def div8(self, a, b):
"""8-bit restoring division through the per-stage bit-cascade GE comparators and the sub8 subtractor."""
if b == 0:
return 0xFF, a
a_bits = int_to_bits(a, 8)
div_bits = int_to_bits(b, 8)
quotient = 0
remainder = 0
for stage in range(8):
remainder = ((remainder << 1) | a_bits[stage]) & 0xFF
prefix = f"alu.alu8bit.div.stage{stage}"
finals = {
"gt": f"{prefix}.cmp_bc.gt",
"lt": f"{prefix}.cmp_bc.lt",
"eq": f"{prefix}.cmp_bc.eq",
"ge": f"{prefix}.cmp",
"le": f"{prefix}.cmp_bc.le",
}
ge = self._cmp_bit_cascade(f"{prefix}.cmp_bc", finals,
int_to_bits(remainder, 8), div_bits, "ge")
if ge:
remainder, _ = self.sub8(remainder, b)
quotient = (quotient << 1) | 1
else:
quotient = quotient << 1
return quotient & 0xFF, remainder & 0xFF
# ----- N-bit primitives (for 16-bit and 32-bit variants) ----------------
def add_n(self, a: int, b: int, bits: int):
"""Width-generic ripple-carry add via arithmetic.ripplecarry{N}bit."""
prefix = f"arithmetic.ripplecarry{bits}bit"
a_lsb = list(reversed(int_to_bits(a, bits)))
b_lsb = list(reversed(int_to_bits(b, bits)))
carry = 0
s_lsb = []
for i in range(bits):
s, carry = self._fa(f"{prefix}.fa{i}", a_lsb[i], b_lsb[i], carry)
s_lsb.append(s)
return bits_to_int(list(reversed(s_lsb))), carry
def sub_n(self, a: int, b: int, bits: int):
"""N-bit two's-complement subtract via arithmetic.sub{N}bit (N >= 16)."""
prefix = f"arithmetic.sub{bits}bit"
a_lsb = list(reversed(int_to_bits(a, bits)))
b_lsb = list(reversed(int_to_bits(b, bits)))
# NOT each B bit
notb = [self._g(f"{prefix}.not_b.bit{i}", [b_lsb[i]]) for i in range(bits)]
carry = 1 # carry-in = 1 for two's-complement
d_lsb = []
for i in range(bits):
s, carry = self._fa(f"{prefix}.fa{i}", a_lsb[i], notb[i], carry)
d_lsb.append(s)
return bits_to_int(list(reversed(d_lsb))), carry
def cmp_n(self, a: int, b: int, kind: str, bits: int):
"""N-bit unsigned comparison through the bit-cascade comparator."""
short = self._CMP_KIND[kind]
finals = {
"gt": f"arithmetic.greaterthan{bits}bit",
"lt": f"arithmetic.lessthan{bits}bit",
"eq": f"arithmetic.equality{bits}bit",
"ge": f"arithmetic.greaterorequal{bits}bit",
"le": f"arithmetic.lessorequal{bits}bit",
}
return self._cmp_bit_cascade(f"arithmetic.cmp{bits}bit", finals,
int_to_bits(a, bits), int_to_bits(b, bits), short)
def mul_n(self, a: int, b: int, bits: int):
"""N-bit shift-add multiply (low N bits only)."""
ab = int_to_bits(a, bits)
bb = int_to_bits(b, bits)
mask = (1 << bits) - 1
result = 0
for j in range(bits):
if bb[j] == 0:
continue
row = 0
for i in range(bits):
pp = self._g(f"alu.alu{bits}bit.mul.pp.a{i}b{j}", [ab[i], bb[j]])
row |= (pp << (bits - 1 - i))
shift = (bits - 1) - j
result, _ = self.add_n(result & mask, (row << shift) & mask, bits)
return result & mask
class GenericThresholdCPU:
"""Variant-aware CPU runtime. Sized from the variant's manifest."""
def __init__(self, tensors: Dict[str, torch.Tensor]):
self.T = tensors
m = get_manifest(tensors)
self.data_bits = m["data_bits"]
self.addr_bits = m["addr_bits"]
self.mem_bytes = m["memory_bytes"]
# 8-bit CPU primitives (ripplecarry8bit, sub8bit, alu.alu8bit.*, memory.*,
# control.*) are present in every variant regardless of manifest data_bits.
# Wider data widths simply add additional standalone ALU primitives.
if self.mem_bytes == 0:
raise NotImplementedError(
"Pure-ALU variants have no memory; cannot run CPU programs"
)
self.alu = GenericThresholdALU(tensors, 8)
def _addr_decode(self, addr):
bits = torch.tensor(int_to_bits(addr, self.addr_bits), dtype=torch.float32)
w = self.T["memory.addr_decode.weight"]
b = self.T["memory.addr_decode.bias"]
return heaviside((w * bits).sum(dim=1) + b)
def mem_read(self, mem, addr):
sel = self._addr_decode(addr)
mem_bits = torch.tensor(
[int_to_bits(byte, 8) for byte in mem], dtype=torch.float32
)
and_w = self.T["memory.read.and.weight"]
and_b = self.T["memory.read.and.bias"]
or_w = self.T["memory.read.or.weight"]
or_b = self.T["memory.read.or.bias"]
out = []
for bit in range(8):
inp = torch.stack([mem_bits[:, bit], sel], dim=1)
and_out = heaviside((inp * and_w[bit]).sum(dim=1) + and_b[bit])
out.append(int(heaviside((and_out * or_w[bit]).sum() + or_b[bit]).item()))
return bits_to_int(out)
def mem_write(self, mem, addr, value):
sel = self._addr_decode(addr)
data_bits = torch.tensor(int_to_bits(value, 8), dtype=torch.float32)
mem_bits = torch.tensor(
[int_to_bits(byte, 8) for byte in mem], dtype=torch.float32
)
sel_w = self.T["memory.write.sel.weight"]
sel_b = self.T["memory.write.sel.bias"]
nsel_w = self.T["memory.write.nsel.weight"].squeeze(1)
nsel_b = self.T["memory.write.nsel.bias"]
and_old_w = self.T["memory.write.and_old.weight"]
and_old_b = self.T["memory.write.and_old.bias"]
and_new_w = self.T["memory.write.and_new.weight"]
and_new_b = self.T["memory.write.and_new.bias"]
or_w = self.T["memory.write.or.weight"]
or_b = self.T["memory.write.or.bias"]
we = torch.ones_like(sel)
sel_inp = torch.stack([sel, we], dim=1)
write_sel = heaviside((sel_inp * sel_w).sum(dim=1) + sel_b)
nsel = heaviside(write_sel * nsel_w + nsel_b)
for bit in range(8):
old = mem_bits[:, bit]
data_bit = data_bits[bit].expand(self.mem_bytes)
inp_old = torch.stack([old, nsel], dim=1)
inp_new = torch.stack([data_bit, write_sel], dim=1)
and_old = heaviside((inp_old * and_old_w[:, bit]).sum(dim=1) + and_old_b[:, bit])
and_new = heaviside((inp_new * and_new_w[:, bit]).sum(dim=1) + and_new_b[:, bit])
or_inp = torch.stack([and_old, and_new], dim=1)
new_bit = heaviside((or_inp * or_w[:, bit]).sum(dim=1) + or_b[:, bit])
mem_bits[:, bit] = new_bit
return [bits_to_int([int(b) for b in mem_bits[i].tolist()]) for i in range(self.mem_bytes)]
# Conditions 0..7 map to (mux circuit, flag index into [Z, N, C, V]).
# Odd conditions are the negated forms; the mux select is the complemented
# flag (the circuits are plain per-bit muxes -- see control.* .inputs,
# which name the select $not_zero / $not_carry / ... for the odd forms).
_JCC = [("control.jz", 0), ("control.jnz", 0), ("control.jc", 2), ("control.jnc", 2),
("control.jn", 1), ("control.jp", 1), ("control.jv", 3), ("control.jnv", 3)]
def _jcc_pc(self, circuit: str, pc: int, target: int, sel: int) -> int:
"""Per-bit 2:1 mux over the PC: sel ? target : pc, addr_bits wide."""
pc_bits = int_to_bits(pc, self.addr_bits)
t_bits = int_to_bits(target, self.addr_bits)
out = []
for bit in range(self.addr_bits):
bp = f"{circuit}.bit{bit}"
not_sel = self.alu._g(f"{bp}.not_sel", [float(sel)])
and_a = self.alu._g(f"{bp}.and_a", [float(pc_bits[bit]), not_sel])
and_b = self.alu._g(f"{bp}.and_b", [float(t_bits[bit]), float(sel)])
out.append(self.alu._g(f"{bp}.or", [and_a, and_b]))
return bits_to_int(out)
def _sp_dec(self, sp: int) -> int:
"""SP - 1 through the control.push.sp_dec borrow chain."""
bits = int_to_bits(sp, self.addr_bits)
out = [0] * self.addr_bits
borrow = 1
for bit in range(self.addr_bits - 1, -1, -1):
bp = f"control.push.sp_dec.bit{bit}"
h_or = self.alu._g(f"{bp}.xor.layer1.or", [float(bits[bit]), float(borrow)])
h_nand = self.alu._g(f"{bp}.xor.layer1.nand", [float(bits[bit]), float(borrow)])
out[bit] = self.alu._g(f"{bp}.xor.layer2", [h_or, h_nand])
borrow = self.alu._g(f"{bp}.borrow", [float(1 - bits[bit]), float(borrow)])
return bits_to_int(out)
def _decode_op(self, opcode):
"""4-to-16 opcode one-hot through the gate decoder."""
ob = [float((opcode >> j) & 1) for j in range(4)]
return [self.alu._g(f"control.decode.op{n}", ob) for n in range(16)]
def _inc(self, pc, name, start):
"""PC + 2^start through the gate increment chain (LSB-first)."""
ab = self.addr_bits
pcb = [(pc >> k) & 1 for k in range(ab)]
out = [0] * ab
carry = 0
for k in range(ab):
if k < start:
out[k] = self.alu._g(f"control.pcnext.{name}.bit{k}", [float(pcb[k])])
else:
cin = 1 if k == start else carry
o = self.alu._g(f"control.pcnext.{name}.bit{k}.xor.layer1.or", [float(pcb[k]), float(cin)])
nd = self.alu._g(f"control.pcnext.{name}.bit{k}.xor.layer1.nand", [float(pcb[k]), float(cin)])
out[k] = self.alu._g(f"control.pcnext.{name}.bit{k}.xor.layer2", [o, nd])
carry = self.alu._g(f"control.pcnext.{name}.bit{k}.carry", [float(pcb[k]), float(cin)])
return sum(out[k] << k for k in range(ab))
def _pcnext(self, pc2, pc4, addr, jcc, use_pc4, use_addr, use_jcc):
"""Next-PC priority mux through the gates: use_addr ? addr : (use_jcc ? jcc : (use_pc4 ? pc4 : pc2))."""
ab = self.addr_bits
p2 = [(pc2 >> k) & 1 for k in range(ab)]
p4 = [(pc4 >> k) & 1 for k in range(ab)]
aB = [(addr >> k) & 1 for k in range(ab)]
jB = [(jcc >> k) & 1 for k in range(ab)]
out = [0] * ab
for k in range(ab):
ns = self.alu._g(f"control.pcnext.m_pc4.bit{k}.not_sel", [float(use_pc4)])
aa = self.alu._g(f"control.pcnext.m_pc4.bit{k}.and_a", [float(p2[k]), ns])
bb = self.alu._g(f"control.pcnext.m_pc4.bit{k}.and_b", [float(p4[k]), float(use_pc4)])
m1 = self.alu._g(f"control.pcnext.m_pc4.bit{k}.or", [aa, bb])
ns = self.alu._g(f"control.pcnext.m_jcc.bit{k}.not_sel", [float(use_jcc)])
aa = self.alu._g(f"control.pcnext.m_jcc.bit{k}.and_a", [float(m1), ns])
bb = self.alu._g(f"control.pcnext.m_jcc.bit{k}.and_b", [float(jB[k]), float(use_jcc)])
m2 = self.alu._g(f"control.pcnext.m_jcc.bit{k}.or", [aa, bb])
ns = self.alu._g(f"control.pcnext.m_addr.bit{k}.not_sel", [float(use_addr)])
aa = self.alu._g(f"control.pcnext.m_addr.bit{k}.and_a", [float(m2), ns])
bb = self.alu._g(f"control.pcnext.m_addr.bit{k}.and_b", [float(aB[k]), float(use_addr)])
out[k] = self.alu._g(f"control.pcnext.m_addr.bit{k}.or", [aa, bb])
return sum(out[k] << k for k in range(ab))
def step(self, state):
if state["halted"]:
return state
s = dict(state)
s["mem"] = state["mem"][:]
s["regs"] = state["regs"][:]
s["flags"] = state["flags"][:]
addr_mask = (1 << self.addr_bits) - 1
pc = s["pc"]
hi = self.mem_read(s["mem"], pc & addr_mask)
lo = self.mem_read(s["mem"], (pc + 1) & addr_mask)
ir = ((hi & 0xFF) << 8) | (lo & 0xFF)
opcode = (ir >> 12) & 0xF
oh = self._decode_op(opcode) # gate opcode decode (one-hots)
rd = (ir >> 10) & 0x3
rs = (ir >> 8) & 0x3
imm = ir & 0xFF
addr = None
if oh[0xA] or oh[0xB] or oh[0xC] or oh[0xD] or oh[0xE]:
ah = self.mem_read(s["mem"], (pc + 2) & addr_mask)
al = self.mem_read(s["mem"], (pc + 3) & addr_mask)
addr = (((ah & 0xFF) << 8) | (al & 0xFF)) & addr_mask
pc2 = self._inc(pc, "pc2", 1) # PC+2 through the gate chain
pc4 = self._inc(pc, "pc4", 2) # PC+4 through the gate chain
a = s["regs"][rd]
b = s["regs"][rs]
result = a
carry = 0
overflow = 0
write_result = True
jcc_val = 0
if oh[0x0]:
result, carry = self.alu.add8(a, b)
overflow = 1 if (((a ^ result) & (b ^ result)) & 0x80) else 0
elif oh[0x1]:
result, carry = self.alu.sub8(a, b)
overflow = 1 if (((a ^ b) & (a ^ result)) & 0x80) else 0
elif oh[0x2]:
result = self.alu.and8(a, b)
elif oh[0x3]:
result = self.alu.or8(a, b)
elif oh[0x4]:
result = self.alu.xor8(a, b)
elif oh[0x5]:
result = self.alu.shl8(a)
elif oh[0x6]:
result = self.alu.shr8(a)
elif oh[0x7]:
result = self.alu.mul8(a, b)
elif oh[0x8]:
result, _ = self.alu.div8(a, b)
elif oh[0x9]: # CMP: set flags only
r2, carry = self.alu.sub8(a, b)
z = 1 if r2 == 0 else 0
n = 1 if (r2 & 0x80) else 0
v = 1 if (((a ^ b) & (a ^ r2)) & 0x80) else 0
s["flags"] = [z, n, carry, v]
write_result = False
elif oh[0xA]: # LOAD
result = self.mem_read(s["mem"], addr)
elif oh[0xB]: # STORE
s["mem"] = self.mem_write(s["mem"], addr, b & 0xFF)
write_result = False
elif oh[0xC]: # JMP (PC = addr via pcnext)
write_result = False
elif oh[0xD]: # JCC (PC via jcc mux + pcnext)
cond = imm & 0x7
circuit, flag_idx = self._JCC[cond]
flag = s["flags"][flag_idx]
sel = flag if cond % 2 == 0 else 1 - flag
jcc_val = self._jcc_pc(circuit, pc4, addr, sel)
write_result = False
elif oh[0xE]: # CALL: push PC+4, PC = addr
ret_addr = pc4 & 0xFFFF
sp = s.get("sp", addr_mask)
sp = self._sp_dec(sp)
s["mem"] = self.mem_write(s["mem"], sp, (ret_addr >> 8) & 0xFF)
sp = self._sp_dec(sp)
s["mem"] = self.mem_write(s["mem"], sp, ret_addr & 0xFF)
s["sp"] = sp
write_result = False
elif oh[0xF]: # HALT
s["halted"] = True
return s
if write_result:
s["regs"][rd] = result & 0xFF
if oh[0x0] or oh[0x1] or oh[0x7]:
z = 1 if (result & 0xFF) == 0 else 0
n = 1 if (result & 0x80) else 0
s["flags"] = [z, n, carry, overflow]
# PC sequencing through the gate mux.
use_pc4 = 1 if (oh[0xA] or oh[0xB]) else 0
use_addr = 1 if (oh[0xC] or oh[0xE]) else 0
s["pc"] = self._pcnext(pc2, pc4, addr or 0, jcc_val, use_pc4, use_addr, oh[0xD])
return s
def run(self, state, max_cycles=200):
s = state
cycles = 0
while not s["halted"] and cycles < max_cycles:
s = self.step(s)
cycles += 1
return s, cycles
def _encode_instr(opcode, rd, rs, imm):
return ((opcode & 0xF) << 12) | ((rd & 0x3) << 10) | ((rs & 0x3) << 8) | (imm & 0xFF)
def _w16(mem, addr, value):
mem[addr] = (value >> 8) & 0xFF
mem[addr + 1] = value & 0xFF
PROGRAM_MIN_BYTES = 0x84 # code 0x00..0x1F + data 0x80..0x83
def builtin_program(addr_bits: int) -> Tuple[List[int], int]:
"""Sum 5+4+3+2+1 via a loop."""
if (1 << addr_bits) < PROGRAM_MIN_BYTES:
raise ValueError(f"addr_bits={addr_bits} too small for builtin program")
mem = [0] * (1 << addr_bits)
mem[0x80] = 5 # initial counter
mem[0x81] = 1 # decrement
mem[0x82] = 0 # zero (for compare and accumulator init)
# mem[0x83] is the output
_w16(mem, 0x0000, _encode_instr(0xA, 1, 0, 0)); _w16(mem, 0x0002, 0x0080)
_w16(mem, 0x0004, _encode_instr(0xA, 2, 0, 0)); _w16(mem, 0x0006, 0x0081)
_w16(mem, 0x0008, _encode_instr(0xA, 3, 0, 0)); _w16(mem, 0x000A, 0x0082)
_w16(mem, 0x000C, _encode_instr(0xA, 0, 0, 0)); _w16(mem, 0x000E, 0x0082)
_w16(mem, 0x0010, _encode_instr(0x0, 0, 1, 0))
_w16(mem, 0x0012, _encode_instr(0x1, 1, 2, 0))
_w16(mem, 0x0014, _encode_instr(0x9, 1, 3, 0))
_w16(mem, 0x0016, _encode_instr(0xD, 0, 0, 0x01)); _w16(mem, 0x0018, 0x0010)
_w16(mem, 0x001A, _encode_instr(0xB, 0, 0, 0)); _w16(mem, 0x001C, 0x0083)
_w16(mem, 0x001E, _encode_instr(0xF, 0, 0, 0))
return mem, 15
# ---------------------------------------------------------------------------
# Eval driver
# ---------------------------------------------------------------------------
def _file_fingerprint(path: Path) -> str:
"""Stable cache key for a safetensors file: sha256 of its content."""
import hashlib
h = hashlib.sha256()
with open(path, "rb") as f:
for chunk in iter(lambda: f.read(1 << 20), b""):
h.update(chunk)
return h.hexdigest()
def _cache_key(path: Path, opts: Dict[str, Any]) -> str:
"""Cache key combining file content with the relevant evaluation options."""
fp = _file_fingerprint(path)
opt_str = json.dumps(opts, sort_keys=True)
import hashlib
suffix = hashlib.sha256(opt_str.encode("utf-8")).hexdigest()[:8]
return f"{fp}_{suffix}"
def _load_cache(cache_dir: Path, key: str) -> Dict[str, Any] | None:
p = cache_dir / f"{key}.json"
if not p.exists():
return None
try:
return json.loads(p.read_text(encoding="utf-8"))
except (json.JSONDecodeError, OSError):
return None
def _save_cache(cache_dir: Path, key: str, payload: Dict[str, Any]) -> None:
cache_dir.mkdir(parents=True, exist_ok=True)
p = cache_dir / f"{key}.json"
try:
p.write_text(json.dumps(payload, indent=2, default=str), encoding="utf-8")
except OSError:
pass
def list_safetensors(path: Path) -> List[Path]:
if path.is_file():
return [path]
if path.is_dir():
return sorted(p for p in path.glob("*.safetensors") if p.is_file())
return []
# Standalone machines carry their own circuit inventory and are verified by
# their own scripts, not the gate-fitness suite. Each maps to the tool that
# checks it.
MACHINE_VERIFIER = {
"subleq8": "src/machines.py",
"rv32": "src/machines.py",
"matrix8": "src/machines.py matrix8",
"subleq8io": "src/host.py constructor8",
"reflect": "src/reflect.py",
"attractor": "src/automata.py attractor",
"reversible": "src/automata.py reversible",
"ca": "src/automata.py ca",
"tile": "src/automata.py tile",
}
def evaluate_one(path: Path, device: str, pop_size: int, debug: bool, run_cpu_program: bool) -> Dict:
out: Dict = {"path": str(path), "filename": path.name}
# Skip standalone machines cleanly rather than error on missing standard gates.
with safe_open(str(path), framework="pt") as f:
meta = f.metadata() or {}
if meta.get("machine"):
m = meta["machine"]
out.update(status="SKIP", machine=m,
note=f"standalone machine — verify with {MACHINE_VERIFIER.get(m, 'machines.py')}")
return out
try:
tensors = load_model(str(path))
except Exception as e:
out.update(error=f"load failed: {e}", status="ERROR")
return out
manifest = get_manifest(tensors)
out.update(
size_mb=path.stat().st_size / (1024 * 1024),
tensors=len(tensors),
params=sum(t.numel() for t in tensors.values()),
manifest=manifest,
)
# Move to device
tensors = {k: v.to(device) for k, v in tensors.items()}
try:
evaluator = BatchedFitnessEvaluator(device=device, model_path=str(path), tensors=tensors)
population = create_population(tensors, pop_size=pop_size, device=device)
t0 = time.perf_counter()
fitness = evaluator.evaluate(population, debug=debug)
elapsed = time.perf_counter() - t0
f0 = float(fitness[0].item()) if pop_size == 1 else float(fitness.mean().item())
out.update(
fitness=f0,
total_tests=evaluator.total_tests,
elapsed_s=elapsed,
categories={k: (float(v[0]), int(v[1])) for k, v in evaluator.category_scores.items()},
status="PASS" if f0 >= 0.9999 else "FAIL",
)
except Exception as e:
out.update(error=f"eval failed: {type(e).__name__}: {e}", status="ERROR")
return out
# Optional: CPU program test (8-bit CPU primitives are in every variant)
if run_cpu_program:
if manifest["memory_bytes"] >= PROGRAM_MIN_BYTES:
try:
cpu_tensors = {k: v.cpu() for k, v in tensors.items()}
cpu = GenericThresholdCPU(cpu_tensors)
mem, expected = builtin_program(manifest["addr_bits"])
state = {"pc": 0, "regs": [0] * 4, "flags": [0] * 4, "mem": mem, "halted": False}
t0 = time.perf_counter()
final, cycles = cpu.run(state, max_cycles=200)
cpu_elapsed = time.perf_counter() - t0
got = final["mem"][0x83]
out["cpu_program"] = {
"ok": got == expected,
"got": got,
"expected": expected,
"cycles": cycles,
"elapsed_s": cpu_elapsed,
}
if got != expected:
out["status"] = "FAIL"
except Exception as e:
out["cpu_program"] = {"error": str(e)}
else:
out["cpu_program"] = {"skipped": f"mem={manifest['memory_bytes']}B < {PROGRAM_MIN_BYTES}"}
# Wider-ALU chain test for 16/32-bit variants
bits = manifest["data_bits"]
if bits in (16, 32):
try:
alu_tensors = {k: v.cpu() for k, v in tensors.items()}
alu = GenericThresholdALU(alu_tensors, bits)
t0 = time.perf_counter()
if bits == 16:
x, y = 1234, 5678
z, _ = alu.add_n(x, y, 16); assert z == (x + y) & 0xFFFF
w, _ = alu.sub_n(z, x, 16); assert w == (z - x) & 0xFFFF, (w, z - x)
gt = alu.cmp_n(z, x, "greaterthan", 16); assert gt == 1
lt = alu.cmp_n(x, z, "lessthan", 16); assert lt == 1
eq = alu.cmp_n(w, y, "eq", 16); assert eq == 1
p = alu.mul_n(123, 5, 16); assert p == (123 * 5) & 0xFFFF
else: # 32
x, y = 1_000_000, 999_000
z, _ = alu.sub_n(x, y, 32); assert z == 1_000
s, _ = alu.add_n(z, x, 32); assert s == 1_001_000
p = alu.mul_n(z, 100, 32); assert p == 100_000
gt = alu.cmp_n(x, y, "greaterthan", 32); assert gt == 1
lt = alu.cmp_n(y, x, "lessthan", 32); assert lt == 1
eq = alu.cmp_n(p, 100_000, "equality", 32); assert eq == 1
chain_dt = time.perf_counter() - t0
out[f"alu_chain_{bits}"] = {"ok": True, "elapsed_s": chain_dt}
except AssertionError as e:
out[f"alu_chain_{bits}"] = {"ok": False, "error": f"chain mismatch: {e}"}
out["status"] = "FAIL"
except Exception as e:
out[f"alu_chain_{bits}"] = {"ok": False, "error": f"{type(e).__name__}: {e}"}
out["status"] = "FAIL"
return out
def print_row(r: Dict, show_cpu: bool) -> None:
if r.get("status") == "SKIP":
m = r.get("machine", "machine")
print(f" {r['filename']:<48} SKIP ({m} — verify with {MACHINE_VERIFIER.get(m, 'machines.py')})")
return
if "error" in r:
print(f" {r['filename']:<48} ERROR: {r['error'][:80]}")
return
m = r["manifest"]
fit = f"{r['fitness']:.4f}" if r.get("fitness") is not None else "n/a"
cpu_col = ""
if show_cpu and "cpu_program" in r:
cp = r["cpu_program"]
if cp.get("ok"):
cpu_col = f" CPU OK ({cp['cycles']}cyc/{cp['elapsed_s']:.1f}s)"
elif "skipped" in cp:
cpu_col = f" CPU SKIP"
elif "error" in cp:
cpu_col = f" CPU ERR"
else:
cpu_col = f" CPU FAIL ({cp.get('got')}!={cp.get('expected')})"
chain_col = ""
if show_cpu:
for bits in (16, 32):
key = f"alu_chain_{bits}"
if key in r:
ch = r[key]
if ch.get("ok"):
chain_col = f" ALU{bits} OK ({ch['elapsed_s']:.2f}s)"
else:
chain_col = f" ALU{bits} FAIL"
print(
f" {r['filename']:<48} d={m['data_bits']:>2}b a={m['addr_bits']:>2}b "
f"mem={m['memory_bytes']:>6}B size={r['size_mb']:>6.1f}MB "
f"params={r['params']:>10,} fit={fit:>6} tests={r['total_tests']:>5} "
f"{r['status']:>5}{cpu_col}{chain_col}"
)
def main_all() -> int:
parser = argparse.ArgumentParser(description="Variant-agnostic eval harness")
parser.add_argument("path", help="Path to .safetensors file or directory of files")
parser.add_argument("--device", default="cpu", help="cpu (default) or cuda")
parser.add_argument("--pop_size", type=int, default=1)
parser.add_argument("--debug", action="store_true", help="Per-circuit detail per file")
parser.add_argument("--cpu-program", action="store_true",
help="Also run a small assembled program through the threshold CPU "
"(any variant with >= 132 B memory), plus a chained 16- or "
"32-bit ALU sequence on wider variants")
parser.add_argument("--json", action="store_true", help="Emit JSON results to stdout instead of a table")
parser.add_argument("--cache-dir", default=".eval_cache",
help="Directory for hash-keyed result cache "
"(default: ./.eval_cache). Set to '' to disable.")
parser.add_argument("--no-cache", action="store_true",
help="Disable the result cache for this run.")
args = parser.parse_args()
files = list_safetensors(Path(args.path))
if not files:
print(f"No .safetensors files found under {args.path}", file=sys.stderr)
return 2
print(f"Evaluating {len(files)} file(s) on {args.device}\n")
cache_enabled = bool(args.cache_dir) and not args.no_cache
cache_dir = Path(args.cache_dir) if cache_enabled else None
cache_opts = {
"device": args.device,
"pop_size": args.pop_size,
"cpu_program": bool(args.cpu_program),
}
cache_hits = 0
results = []
fail_count = 0
for f in files:
print(f"=== {f.name}")
cached = None
key = None
if cache_enabled:
try:
key = _cache_key(f, cache_opts)
cached = _load_cache(cache_dir, key)
except OSError:
cached = None
if cached is not None:
r = cached
cache_hits += 1
print(f" (cache hit)")
else:
r = evaluate_one(f, device=args.device, pop_size=args.pop_size,
debug=args.debug, run_cpu_program=args.cpu_program)
# Don't cache ERROR results: a transient failure (OOM, interrupt)
# would otherwise be replayed on every rerun of the same file.
if cache_enabled and key is not None and r.get("status") != "ERROR":
_save_cache(cache_dir, key, r)
results.append(r)
print_row(r, show_cpu=args.cpu_program)
if r.get("status") not in ("PASS", "SKIP"):
fail_count += 1
if args.json:
# Make it serialisable
for r in results:
r["manifest"] = {k: (int(v) if isinstance(v, float) and v.is_integer() else v)
for k, v in r.get("manifest", {}).items()}
print(json.dumps(results, indent=2, default=str))
return fail_count
# Summary
print()
print("=" * 100)
print(" SUMMARY")
print("=" * 100)
for r in results:
print_row(r, show_cpu=args.cpu_program)
print()
if fail_count == 0:
print(f"ALL {len(files)} variants PASS")
else:
print(f"{fail_count}/{len(files)} variants FAIL")
if cache_enabled:
print(f"(cache: {cache_hits}/{len(files)} hits, dir={cache_dir})")
return fail_count
# ----------------------------------------------------------------------------
# Mini assembler
# ----------------------------------------------------------------------------
_OPCODE_NAMES = {
"add": 0x0, "sub": 0x1, "and": 0x2, "or": 0x3, "xor": 0x4,
"shl": 0x5, "shr": 0x6, "mul": 0x7, "div": 0x8, "cmp": 0x9,
"load": 0xA, "store": 0xB, "jmp": 0xC, "jcc": 0xD,
"call": 0xE, "halt": 0xF,
}
_COND = {"jz": 0, "jnz": 1, "jc": 2, "jnc": 3,
"jn": 4, "jp": 5, "jv": 6, "jnv": 7}
def _enc(opcode: int, rd: int = 0, rs: int = 0, imm: int = 0) -> int:
return ((opcode & 0xF) << 12) | ((rd & 0x3) << 10) | ((rs & 0x3) << 8) | (imm & 0xFF)
class Asm:
"""Tiny assembler for the threshold-computer ISA."""
def __init__(self, size: int):
self.mem: List[int] = [0] * size
self.pc: int = 0
self.labels: Dict[str, int] = {}
self.fixups: List[Tuple[int, str]] = []
def org(self, addr: int) -> None:
self.pc = addr
def label(self, name: str) -> None:
if name in self.labels:
raise ValueError(f"duplicate label: {name}")
self.labels[name] = self.pc
def db(self, *values: int) -> None:
for v in values:
self.mem[self.pc] = v & 0xFF
self.pc += 1
def dw(self, value: int) -> None:
self.mem[self.pc] = (value >> 8) & 0xFF
self.mem[self.pc + 1] = value & 0xFF
self.pc += 2
def daddr(self, label: str) -> None:
self.fixups.append((self.pc, label))
self.dw(0)
# --- ALU ops (no immediate) ---
def _alu(self, op: int, rd: int, rs: int) -> None:
self.dw(_enc(op, rd, rs))
def add(self, rd: int, rs: int) -> None: self._alu(0x0, rd, rs)
def sub(self, rd: int, rs: int) -> None: self._alu(0x1, rd, rs)
def and_(self, rd: int, rs: int) -> None: self._alu(0x2, rd, rs)
def or_(self, rd: int, rs: int) -> None: self._alu(0x3, rd, rs)
def xor(self, rd: int, rs: int) -> None: self._alu(0x4, rd, rs)
def shl(self, rd: int) -> None: self._alu(0x5, rd, 0)
def shr(self, rd: int) -> None: self._alu(0x6, rd, 0)
def mul(self, rd: int, rs: int) -> None: self._alu(0x7, rd, rs)
def cmp(self, rd: int, rs: int) -> None: self._alu(0x9, rd, rs)
# --- Memory + control (address-extended) ---
def load(self, rd: int, label: str) -> None:
self.dw(_enc(0xA, rd, 0)); self.daddr(label)
def store(self, rs: int, label: str) -> None:
self.dw(_enc(0xB, 0, rs)); self.daddr(label)
def jmp(self, label: str) -> None:
self.dw(_enc(0xC)); self.daddr(label)
def jcc(self, cond: str, label: str) -> None:
self.dw(_enc(0xD, 0, 0, _COND[cond])); self.daddr(label)
def jz(self, label: str) -> None: self.jcc("jz", label)
def jnz(self, label: str) -> None: self.jcc("jnz", label)
def jc(self, label: str) -> None: self.jcc("jc", label)
def jnc(self, label: str) -> None: self.jcc("jnc", label)
def jn(self, label: str) -> None: self.jcc("jn", label)
def jp(self, label: str) -> None: self.jcc("jp", label)
def jv(self, label: str) -> None: self.jcc("jv", label)
def jnv(self, label: str) -> None: self.jcc("jnv", label)
def call(self, label: str) -> None:
self.dw(_enc(0xE)); self.daddr(label)
def halt(self) -> None:
self.dw(_enc(0xF))
def assemble(self) -> List[int]:
for offset, label in self.fixups:
if label not in self.labels:
raise ValueError(f"undefined label: {label}")
target = self.labels[label]
self.mem[offset] = (target >> 8) & 0xFF
self.mem[offset + 1] = target & 0xFF
return self.mem
# ----------------------------------------------------------------------------
# Programs
# ----------------------------------------------------------------------------
ProgramResult = Tuple[List[int], Dict[int, int], int, str]
def fib(n: int = 11, mem_size: int = 256) -> ProgramResult:
"""Iterative Fibonacci F(N), 8-bit wrap."""
a = Asm(mem_size)
a.org(0)
a.load(2, "n_addr") # R2 = n
a.load(0, "zero_addr") # R0 = 0 = F(0)
a.load(1, "one_addr") # R1 = 1 = F(1)
a.label("loop")
a.load(3, "zero_addr") # R3 = 0
a.cmp(2, 3) # n == 0?
a.jz("done")
a.load(3, "zero_addr") # R3 = 0
a.add(3, 1) # R3 = b (saved old b)
a.add(1, 0) # R1 = a + b (new b)
a.load(0, "zero_addr") # R0 = 0
a.add(0, 3) # R0 = old b (new a)
a.load(3, "one_addr") # R3 = 1
a.sub(2, 3) # n--
a.jmp("loop")
a.label("done")
a.store(0, "out") # OUT = a
a.halt()
a.org(0x80)
a.label("zero_addr"); a.db(0)
a.label("one_addr"); a.db(1)
a.label("n_addr"); a.db(n)
a.label("out"); a.db(0)
mem = a.assemble()
expected_a = 0
aa, bb = 0, 1
for _ in range(n):
aa, bb = bb, (aa + bb) & 0xFF
expected_a = aa
return mem, {a.labels["out"]: expected_a}, 16 * (n + 2), f"Fibonacci F({n}) = {expected_a}"
def sum_n(n: int = 10, mem_size: int = 256) -> ProgramResult:
"""Compute 1 + 2 + ... + N using the Z flag from SUB to terminate."""
a = Asm(mem_size)
a.org(0)
a.load(0, "zero_addr") # acc = 0
a.load(1, "n_addr") # i = n
a.load(2, "one_addr") # 1
a.label("loop")
a.add(0, 1) # acc += i
a.sub(1, 2) # i-- (Z set when i becomes 0)
a.jnz("loop")
a.store(0, "out")
a.halt()
a.org(0x80)
a.label("zero_addr"); a.db(0)
a.label("one_addr"); a.db(1)
a.label("n_addr"); a.db(n)
a.label("out"); a.db(0)
mem = a.assemble()
expected = (n * (n + 1) // 2) & 0xFF
return mem, {a.labels["out"]: expected}, 4 + 4 * n, f"sum 1..{n} = {expected}"
def self_mod_jmp(mem_size: int = 256) -> ProgramResult:
"""Self-modifying code: writes the JMP target's low byte at runtime."""
a = Asm(mem_size)
a.org(0)
a.label("start")
# Forward-declare the_jmp's LSB address. The first two instructions are
# each 4 bytes; the_jmp follows them, so the_jmp = pc + 8 from start.
# The JMP's address word is at the_jmp + 2; the LSB byte is at the_jmp + 3.
a.labels["jmp_target_lsb"] = a.pc + 8 + 3
a.load(0, "new_lsb") # R0 = LSB of path_b address (4 bytes)
a.store(0, "jmp_target_lsb") # patch the JMP's LSB (4 bytes)
a.label("the_jmp")
a.jmp("path_a") # initially -> path_a; after patch -> path_b
a.label("path_a")
a.load(1, "val_a"); a.store(1, "out"); a.halt()
a.label("path_b")
a.load(1, "val_b"); a.store(1, "out"); a.halt()
a.org(0x80)
a.label("val_a"); a.db(0xAA)
a.label("val_b"); a.db(0xBB)
# new_lsb_word stores path_b's full 16-bit address; new_lsb labels the LSB byte.
a.label("new_lsb_word"); a.daddr("path_b")
a.labels["new_lsb"] = a.labels["new_lsb_word"] + 1
a.label("out"); a.db(0)
mem = a.assemble()
return mem, {a.labels["out"]: 0xBB}, 30, "self-modifying JMP target"
def all_branches(mem_size: int = 256) -> ProgramResult:
"""Drive all eight conditional jumps; each path writes a unique marker."""
a = Asm(mem_size)
a.org(0)
# ----- JZ: equal compare -> Z=1 -----
a.load(0, "v5"); a.load(1, "v5"); a.cmp(0, 1); a.jz("ok_jz"); a.jmp("fail")
a.label("ok_jz"); a.load(2, "m_a0"); a.store(2, "out0")
# ----- JNZ: unequal compare -> Z=0 -----
a.load(0, "v5"); a.load(1, "v3"); a.cmp(0, 1); a.jnz("ok_jnz"); a.jmp("fail")
a.label("ok_jnz"); a.load(2, "m_a1"); a.store(2, "out1")
# ----- JC: 255+1 = 0 with carry -----
a.load(0, "v255"); a.load(1, "v1"); a.add(0, 1); a.jc("ok_jc"); a.jmp("fail")
a.label("ok_jc"); a.load(2, "m_a2"); a.store(2, "out2")
# ----- JNC: 1+1 = 2, no carry -----
a.load(0, "v1"); a.load(1, "v1"); a.add(0, 1); a.jnc("ok_jnc"); a.jmp("fail")
a.label("ok_jnc"); a.load(2, "m_a3"); a.store(2, "out3")
# ----- JN: 0 - 1 = 0xFF, MSB set -----
a.load(0, "v0"); a.load(1, "v1"); a.sub(0, 1); a.jn("ok_jn"); a.jmp("fail")
a.label("ok_jn"); a.load(2, "m_a4"); a.store(2, "out4")
# ----- JP: 0 + 1 = 1, MSB clear -----
a.load(0, "v0"); a.load(1, "v1"); a.add(0, 1); a.jp("ok_jp"); a.jmp("fail")
a.label("ok_jp"); a.load(2, "m_a5"); a.store(2, "out5")
# ----- JV: 127 + 1 = 128, signed overflow -----
a.load(0, "v127"); a.load(1, "v1"); a.add(0, 1); a.jv("ok_jv"); a.jmp("fail")
a.label("ok_jv"); a.load(2, "m_a6"); a.store(2, "out6")
# ----- JNV: 1 + 1 = 2, no signed overflow -----
a.load(0, "v1"); a.load(1, "v1"); a.add(0, 1); a.jnv("ok_jnv"); a.jmp("fail")
a.label("ok_jnv"); a.load(2, "m_a7"); a.store(2, "out7")
a.jmp("end")
a.label("fail")
a.load(2, "v_fail"); a.store(2, "fail_addr"); a.halt()
a.label("end")
a.halt()
# Code runs to ~0xDF; data starts safely after that.
a.org(0xE0)
a.label("v0"); a.db(0)
a.label("v1"); a.db(1)
a.label("v3"); a.db(3)
a.label("v5"); a.db(5)
a.label("v127"); a.db(127)
a.label("v255"); a.db(255)
a.label("m_a0"); a.db(0xA0)
a.label("m_a1"); a.db(0xA1)
a.label("m_a2"); a.db(0xA2)
a.label("m_a3"); a.db(0xA3)
a.label("m_a4"); a.db(0xA4)
a.label("m_a5"); a.db(0xA5)
a.label("m_a6"); a.db(0xA6)
a.label("m_a7"); a.db(0xA7)
a.label("v_fail"); a.db(0xEE)
a.label("out0"); a.db(0)
a.label("out1"); a.db(0)
a.label("out2"); a.db(0)
a.label("out3"); a.db(0)
a.label("out4"); a.db(0)
a.label("out5"); a.db(0)
a.label("out6"); a.db(0)
a.label("out7"); a.db(0)
a.label("fail_addr"); a.db(0)
mem = a.assemble()
expected = {
a.labels["out0"]: 0xA0,
a.labels["out1"]: 0xA1,
a.labels["out2"]: 0xA2,
a.labels["out3"]: 0xA3,
a.labels["out4"]: 0xA4,
a.labels["out5"]: 0xA5,
a.labels["out6"]: 0xA6,
a.labels["out7"]: 0xA7,
a.labels["fail_addr"]: 0, # must remain zero
}
return mem, expected, 200, "all 8 conditional jumps (JZ/JNZ/JC/JNC/JN/JP/JV/JNV)"
def call_pushes_pc(mem_size: int = 256) -> ProgramResult:
"""Verify CALL pushes the return address (next-instruction PC) onto the stack."""
a = Asm(mem_size)
a.org(0)
a.label("caller")
a.load(0, "marker_val")
a.store(0, "marker_addr") # write before CALL
a.label("call_site")
a.call("callee")
# If CALL did not transfer control, this fallthrough store would write 0xDD:
a.load(0, "noret_val")
a.store(0, "noret_addr")
a.halt()
a.label("callee")
a.load(0, "callee_val")
a.store(0, "callee_addr")
a.halt()
a.org(0x40)
a.label("marker_val"); a.db(0x11)
a.label("marker_addr"); a.db(0)
a.label("noret_val"); a.db(0xDD)
a.label("noret_addr"); a.db(0)
a.label("callee_val"); a.db(0x22)
a.label("callee_addr"); a.db(0)
mem = a.assemble()
addr_mask = mem_size - 1
return_addr = a.labels["call_site"] + 4 # 4-byte CALL instruction
expected = {
a.labels["marker_addr"]: 0x11, # pre-CALL store ran
a.labels["callee_addr"]: 0x22, # callee reached
a.labels["noret_addr"]: 0, # fallthrough did not run
(addr_mask - 2) & addr_mask: return_addr & 0xFF, # ret LSB on stack
(addr_mask - 1) & addr_mask: (return_addr >> 8) & 0xFF, # ret MSB
}
return mem, expected, 30, "CALL pushes return PC onto stack"
def bubble_sort_4(mem_size: int = 256) -> ProgramResult:
"""Sort a 4-byte array using unrolled compare-swap (3 passes of 3 compares)."""
a = Asm(mem_size)
addrs = ["a0", "a1", "a2", "a3"]
a.org(0)
for _outer in range(3):
for i in range(3):
x, y = addrs[i], addrs[i + 1]
# Load pair
a.load(0, x) # R0 = A[i]
a.load(1, y) # R1 = A[i+1]
a.cmp(0, 1) # compare A[i] - A[i+1]
# If A[i] <= A[i+1] (Z=1 or C=0 from sub-style cmp), skip swap
# SUB sets carry when no borrow (a >= b). So:
# a > b iff Z=0 and a >= b -> Z=0 and C=1 (the sub didn't borrow)
# We want to swap when a > b. JNC (no carry / borrow) means a < b -> skip swap.
# If a == b (Z=1) we also skip. So: jump-skip when JZ OR JNC.
# Easier: compute (a > b) by checking C=1 AND Z=0. Use JZ to skip on equal,
# then JNC to skip on a < b. Otherwise fall through to swap.
skip_lbl = f"skip_{_outer}_{i}"
a.jz(skip_lbl) # equal -> skip
a.jnc(skip_lbl) # a < b (sub borrowed) -> skip
# swap: store R0 -> y, R1 -> x
a.store(1, x)
a.store(0, y)
a.label(skip_lbl)
a.halt()
# Initial unsorted array; code runs to ~0xEC, so data starts at 0xF0.
initial = [42, 7, 200, 19]
a.org(0xF0)
for name, val in zip(addrs, initial):
a.label(name); a.db(val)
mem = a.assemble()
sorted_vals = sorted(initial)
expected = {a.labels[name]: v for name, v in zip(addrs, sorted_vals)}
return mem, expected, 800, f"bubble sort {initial} -> {sorted_vals}"
def cross_check_mul(mem_size: int = 256) -> ProgramResult:
"""Cross-check the threshold MUL circuit against repeated ADD."""
a = Asm(mem_size)
A_VAL = 17
B_VAL = 9
expected_product = (A_VAL * B_VAL) & 0xFF
a.org(0)
# --- direct multiply ---
a.load(0, "A")
a.load(1, "B")
a.mul(0, 1)
a.store(0, "out_mul")
# --- repeated-add multiply ---
a.load(0, "zero") # acc = 0
a.load(1, "A") # addend
a.load(2, "B") # counter
a.load(3, "one") # 1
a.label("rep_loop")
a.add(0, 1) # acc += A
a.sub(2, 3) # B--
a.jnz("rep_loop")
a.store(0, "out_add")
a.halt()
a.org(0x80)
a.label("A"); a.db(A_VAL)
a.label("B"); a.db(B_VAL)
a.label("zero"); a.db(0)
a.label("one"); a.db(1)
a.label("out_mul"); a.db(0)
a.label("out_add"); a.db(0)
mem = a.assemble()
expected = {
a.labels["out_mul"]: expected_product,
a.labels["out_add"]: expected_product,
}
return mem, expected, 80, f"MUL vs repeated ADD: {A_VAL} * {B_VAL} = {expected_product}"
def div_via_repeated_sub(mem_size: int = 256) -> ProgramResult:
"""Compute floor(A/B) and (A mod B) by repeated subtraction."""
A_VAL = 100
B_VAL = 7
expected_q = A_VAL // B_VAL # 14
expected_r = A_VAL % B_VAL # 2
a = Asm(mem_size)
a.org(0)
# ---- Repeated-subtraction division ----
a.load(0, "A") # R0 = A (will become remainder)
a.load(1, "B") # R1 = B (divisor)
a.load(2, "ZERO") # R2 = 0 (will become quotient)
a.load(3, "ONE") # R3 = 1 (increment)
a.label("loop")
a.cmp(0, 1) # CMP R0, R1; carry=1 (no-borrow) iff R0 >= R1
a.jnc("done") # if R0 < R1 (carry=0), exit loop
a.sub(0, 1) # R0 -= B
a.add(2, 3) # quotient += 1
a.jmp("loop")
a.label("done")
a.store(2, "OUT_Q_RPT") # quotient via repeated sub
a.store(0, "OUT_R_RPT") # remainder via repeated sub
# ---- Direct DIV opcode for cross-check ----
a.load(0, "A")
a.load(1, "B")
a.dw(_enc(0x8, 0, 1, 0)) # DIV R0, R1 -> R0 = R0 / R1 (8-bit DIV)
a.store(0, "OUT_Q_DIV")
a.halt()
a.org(0x80)
a.label("A"); a.db(A_VAL)
a.label("B"); a.db(B_VAL)
a.label("ZERO"); a.db(0)
a.label("ONE"); a.db(1)
a.label("OUT_Q_RPT"); a.db(0)
a.label("OUT_R_RPT"); a.db(0)
a.label("OUT_Q_DIV"); a.db(0)
mem = a.assemble()
expected = {
a.labels["OUT_Q_RPT"]: expected_q,
a.labels["OUT_R_RPT"]: expected_r,
a.labels["OUT_Q_DIV"]: expected_q,
}
return mem, expected, 4 * (A_VAL // B_VAL + 4) + 12, (
f"{A_VAL} / {B_VAL}: quotient {expected_q} (repeated SUB) "
f"matches DIV opcode result; remainder {expected_r}"
)
def bitwise_chain(mem_size: int = 256) -> ProgramResult:
"""Run a chain of bitwise ops and verify each intermediate value."""
A = 0xCC # 11001100
B = 0xF0 # 11110000
C = 0x0F # 00001111
D = 0xAA # 10101010
s1 = A & B # 0xC0
s2 = s1 | C # 0xCF
s3 = s2 ^ D # 0x65
s4 = (s3 << 1) & 0xFF # 0xCA
s5 = s4 >> 1 # 0x65
a = Asm(mem_size)
a.org(0)
a.load(0, "A"); a.load(1, "B"); a.and_(0, 1); a.store(0, "S1")
a.load(1, "C"); a.or_(0, 1); a.store(0, "S2")
a.load(1, "D"); a.xor(0, 1); a.store(0, "S3")
a.shl(0); a.store(0, "S4")
a.shr(0); a.store(0, "S5")
a.halt()
a.org(0x80)
a.label("A"); a.db(A)
a.label("B"); a.db(B)
a.label("C"); a.db(C)
a.label("D"); a.db(D)
a.label("S1"); a.db(0)
a.label("S2"); a.db(0)
a.label("S3"); a.db(0)
a.label("S4"); a.db(0)
a.label("S5"); a.db(0)
mem = a.assemble()
expected = {
a.labels["S1"]: s1,
a.labels["S2"]: s2,
a.labels["S3"]: s3,
a.labels["S4"]: s4,
a.labels["S5"]: s5,
}
return mem, expected, 30, (
f"bitwise chain AND/OR/XOR/SHL/SHR -> {s1:#x},{s2:#x},{s3:#x},{s4:#x},{s5:#x}"
)
def flags_policy(mem_size: int = 256) -> ProgramResult:
"""Only ADD, SUB, MUL and CMP write FLAGS; every other instruction leaves them."""
a = Asm(mem_size)
a.org(0)
# ----- Probe 1: Z=1 survives AND (nonzero result) -----
a.load(0, "v5"); a.load(1, "v5"); a.cmp(0, 1) # Z=1
a.load(2, "v3"); a.load(3, "v1"); a.and_(2, 3) # R2 = 3 & 1 = 1
a.jz("p1_ok")
a.load(2, "m_fail"); a.store(2, "out1"); a.jmp("p2")
a.label("p1_ok")
a.load(2, "m_b1"); a.store(2, "out1")
# ----- Probe 2: Z=0 survives XOR (zero result) -----
a.label("p2")
a.load(0, "v5"); a.load(1, "v3"); a.cmp(0, 1) # Z=0
a.xor(2, 2) # R2 = 0
a.jz("p2_fail")
a.load(2, "m_b2"); a.store(2, "out2"); a.jmp("p3")
a.label("p2_fail")
a.load(2, "m_fail"); a.store(2, "out2")
# ----- Probe 3: C=1 survives SHR -----
a.label("p3")
a.load(0, "v255"); a.load(1, "v1"); a.add(0, 1) # C=1
a.load(2, "v3"); a.shr(2) # R2 = 1
a.jc("p3_ok")
a.load(2, "m_fail"); a.store(2, "out3"); a.jmp("done")
a.label("p3_ok")
a.load(2, "m_b3"); a.store(2, "out3")
a.label("done")
a.halt()
a.org(0xA0)
a.label("v1"); a.db(1)
a.label("v3"); a.db(3)
a.label("v5"); a.db(5)
a.label("v255"); a.db(255)
a.label("m_b1"); a.db(0xB1)
a.label("m_b2"); a.db(0xB2)
a.label("m_b3"); a.db(0xB3)
a.label("m_fail"); a.db(0xEE)
a.label("out1"); a.db(0)
a.label("out2"); a.db(0)
a.label("out3"); a.db(0)
mem = a.assemble()
expected = {
a.labels["out1"]: 0xB1,
a.labels["out2"]: 0xB2,
a.labels["out3"]: 0xB3,
}
return mem, expected, 60, "flag policy: AND/XOR/SHR leave FLAGS unchanged"
SUITE = [
("fib", lambda mem_size: fib(11, mem_size)),
("sum_n", lambda mem_size: sum_n(10, mem_size)),
("self_mod_jmp", lambda mem_size: self_mod_jmp(mem_size)),
("all_branches", lambda mem_size: all_branches(mem_size)),
("call_pushes_pc", lambda mem_size: call_pushes_pc(mem_size)),
("bubble_sort_4", lambda mem_size: bubble_sort_4(mem_size)),
("cross_check_mul", lambda mem_size: cross_check_mul(mem_size)),
("div_via_repeated_sub", lambda mem_size: div_via_repeated_sub(mem_size)),
("bitwise_chain", lambda mem_size: bitwise_chain(mem_size)),
("flags_policy", lambda mem_size: flags_policy(mem_size)),
]
REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # this file lives in tools/
def load_tensors(path: Path):
out = {}
with safe_open(str(path), framework="pt") as f:
for name in f.keys():
out[name] = f.get_tensor(name).float()
return out
def run_program(cpu: GenericThresholdCPU, mem, max_cycles: int):
addr_mask = (1 << cpu.addr_bits) - 1
state = {
"pc": 0,
"regs": [0] * 4,
"flags": [0] * 4,
"mem": list(mem),
"halted": False,
"sp": addr_mask,
}
t0 = time.perf_counter()
final, cycles = cpu.run(state, max_cycles=max_cycles)
return final, cycles, time.perf_counter() - t0
def check_expected(final, expected: dict) -> tuple[bool, list[str]]:
failures = []
for addr, want in expected.items():
got = final["mem"][addr]
if got != want:
failures.append(f"M[0x{addr:04X}] = {got} (expected {want})")
return (len(failures) == 0), failures
def main_cpu() -> int:
parser = argparse.ArgumentParser(description="Run the CPU program suite")
parser.add_argument(
"--model", type=str,
default=os.path.join(REPO, "variants", "neural_computer8_small.safetensors"),
help="Path to .safetensors variant",
)
parser.add_argument(
"--only", type=str, default="",
help="Comma-separated subset of program names to run",
)
args = parser.parse_args()
print(f"Loading {args.model}")
tensors = load_tensors(Path(args.model))
manifest = get_manifest(tensors)
print(f"Manifest: data={manifest['data_bits']}-bit, addr={manifest['addr_bits']}-bit, "
f"mem={manifest['memory_bytes']}B")
if manifest["memory_bytes"] < 256:
print(f"ERROR: variant has {manifest['memory_bytes']}B memory; "
f"the suite needs at least 256B (scratchpad).")
return 2
cpu = GenericThresholdCPU(tensors)
only = set(s.strip() for s in args.only.split(",") if s.strip())
print()
print("=" * 80)
print(f" CPU PROGRAM SUITE ({manifest['memory_bytes']}B mem, {manifest['data_bits']}-bit ALU)")
print("=" * 80)
pass_count = 0
fail_count = 0
skip_count = 0
for name, builder in SUITE:
if only and name not in only:
skip_count += 1
continue
try:
mem, expected, max_cycles, desc = builder(manifest["memory_bytes"])
except Exception as e:
print(f" {name:18} BUILD ERROR: {e}")
fail_count += 1
continue
if len(mem) != manifest["memory_bytes"]:
print(f" {name:18} SKIP (program built {len(mem)}B, "
f"variant has {manifest['memory_bytes']}B)")
skip_count += 1
continue
final, cycles, elapsed = run_program(cpu, mem, max_cycles)
ok, failures = check_expected(final, expected)
if ok and not final["halted"]:
ok = False
failures.append(f"did not HALT within {max_cycles} cycles")
status = "PASS" if ok else "FAIL"
if ok:
pass_count += 1
else:
fail_count += 1
print(f" {name:18} {status} ({cycles:>3} cyc, {elapsed:>5.2f}s) {desc}")
if not ok:
for f in failures[:6]:
print(f" - {f}")
print()
print("=" * 80)
total = pass_count + fail_count
print(f" PASS: {pass_count}/{total} FAIL: {fail_count}/{total} SKIP: {skip_count}")
return 0 if fail_count == 0 else 1
COMMANDS = {'eval': main, 'variants': main_all, 'cpu': main_cpu}
if __name__ == "__main__":
if len(sys.argv) < 2 or sys.argv[1] not in COMMANDS:
sys.exit(COMMANDS['eval']())
cmd = sys.argv.pop(1)
sys.argv[0] += ' ' + cmd
sys.exit(COMMANDS[cmd]())
|