File size: 110,007 Bytes
2415c4c | 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 | # SPDX-FileCopyrightText: © 2026 Tenstorrent AI ULC
# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations
import dataclasses
import importlib
import inspect
import math
from dataclasses import FrozenInstanceError
from types import SimpleNamespace
import pytest
import torch
import models.common.llm_runtime.prefill.inputs as prefill_inputs_module
import models.common.llm_runtime.prefill.postprocess as postprocess_module
import models.common.llm_runtime.prefill.result_collector as result_collector_module
import models.common.llm_runtime.prefill.runtime as prefill_module
import models.common.llm_runtime.prefill.sampling_helpers as sampling_helpers
import models.common.llm_runtime.tensor_resources as tensor_resources_module
import models.common.llm_runtime.trace_compiler as trace_compiler_module
import ttnn
from models.common.llm_runtime.config import PageTableLayout
from models.common.llm_runtime.output_reader import OutputReader
from models.common.llm_runtime.prefill.config import PrefillRuntimeConfig
from models.common.llm_runtime.prefill.inputs import PrefillDeviceInputs, PrefillHostInputs, PrefillPositionInputs
from models.common.llm_runtime.prefill.plan import _plan_prefill_requests
from models.common.llm_runtime.prefill.postprocess import fit_prefill_sampling_logits
from models.common.llm_runtime.prefill.result_collector import InvocationResult, process_output_tokens
from models.common.llm_runtime.prefill.runtime import PrefillRuntime
from models.common.llm_runtime.prefill.signatures import (
PrefillProgramSignature,
PrefillTraceSignature,
PreparedPrefill,
capture_schema_fingerprint,
workspace_fingerprint,
)
from models.common.llm_runtime.prefill.trace import PrefillHiddenPersistentInputs, PrefillReplayState
from models.common.llm_runtime.program_compiler import ProgramCompiler, ProgramKey
from models.common.llm_runtime.trace_compiler import TraceCapturePlan, TraceCompiler, TraceKey
from models.common.sampling import SamplingParams
class FakeReader(OutputReader):
def __init__(self, mesh_device):
self.mesh_device = mesh_device
def read(self, value, *, blocking):
assert blocking
return value
def read_synchronized(self, value):
return value
class FakeModel:
vocab_size = 8
def __init__(self, mesh_device, *, sampling_batch_size=32, allow_force_argmax=False):
self.config = SimpleNamespace(dim=32, mesh_device=mesh_device)
self.sampling = SimpleNamespace(
config=SimpleNamespace(
max_batch_size=sampling_batch_size,
max_top_k=32,
allow_force_argmax=allow_force_argmax,
)
)
self.chunk_starts = []
def embed_prefill(self, tokens):
return tokens
def prefill_forward(
self,
x_embed,
rot_mats,
user_id=0,
page_table=None,
chunk_page_table=None,
chunk_start_idx=None,
get_last_token=-1,
batch_size=1,
chunk_start_idx_tensor=None,
last_token_slice=None,
last_token_index=None,
):
self.chunk_starts.append(chunk_start_idx)
return SimpleNamespace(shape=(1, 1, 1, self.vocab_size))
def _runtime(
*,
trace_lengths=(128, 1024),
allow_force_argmax=False,
sampling_batch_size=32,
device_sampling_enabled=True,
supports_batched_prefill=True,
disable_batched_prefill=False,
max_prefill_batch_size=8,
batched_prefill_batched_extract=True,
trace_capture_prime_sequence_lengths=(),
):
mesh_device = SimpleNamespace(shape=(1, 1))
config = PrefillRuntimeConfig.resolve(
model=FakeModel(
mesh_device,
sampling_batch_size=sampling_batch_size,
allow_force_argmax=allow_force_argmax,
),
output_reader=FakeReader(mesh_device),
page_table_layout=PageTableLayout(
block_size=32,
raw_capacity_width=256,
prefill_width=264,
decode_width=256,
),
max_batch_size=32,
max_prefill_chunk_size=2048,
supports_batched_prefill=supports_batched_prefill,
disable_batched_prefill=disable_batched_prefill,
max_prefill_batch_size=max_prefill_batch_size,
batched_prefill_batched_extract=batched_prefill_batched_extract,
device_sampling_enabled=device_sampling_enabled,
can_enable_trace=lambda length, cached: cached == 0 and length in trace_lengths,
trace_capture_prime_sequence_lengths=trace_capture_prime_sequence_lengths,
)
return PrefillRuntime(config)
def _inputs(*, prompt_length, cached_tokens=0, token_width=None, page_width=256, rows=1):
token_width = prompt_length if token_width is None else token_width
tokens = torch.arange(rows * token_width, dtype=torch.long).reshape(rows, token_width)
page_table = torch.arange(rows * page_width, dtype=torch.int32).reshape(rows, page_width)
prompt_lens = torch.full((rows,), prompt_length, dtype=torch.long)
start_pos = torch.full((rows,), cached_tokens, dtype=torch.long)
return tokens, page_table, prompt_lens, start_pos
def _plan(
*,
prompt_length,
cached_tokens=0,
token_width=None,
page_width=256,
slots=(0,),
maximum=2048,
supports_batched_prefill=True,
disable_batched_prefill=False,
max_batch_size=32,
max_prefill_batch_size=8,
):
tokens, page_table, prompt_lens, start_pos = _inputs(
prompt_length=prompt_length,
cached_tokens=cached_tokens,
token_width=token_width,
page_width=page_width,
rows=len(slots),
)
return _plan_prefill_requests(
tokens=tokens,
page_table=page_table,
prompt_lens=prompt_lens,
empty_slots=slots,
start_pos=start_pos,
block_size=32,
max_batch_size=max_batch_size,
max_prefill_chunk_size=maximum,
supports_batched_prefill=supports_batched_prefill,
disable_batched_prefill=disable_batched_prefill,
max_prefill_batch_size=max_prefill_batch_size,
max_actual_page_table_width=256,
canonical_page_table_width=264,
)
def test_trace_applicability_classification_does_not_allocate_a_prefill_plan(monkeypatch):
runtime = _runtime()
tokens, _, prompt_lens, start_pos = _inputs(prompt_length=80, rows=2)
def fail_if_planned(
*,
tokens,
page_table,
prompt_lens,
empty_slots,
start_pos,
block_size,
max_batch_size,
max_prefill_chunk_size,
supports_batched_prefill,
disable_batched_prefill,
max_prefill_batch_size,
max_actual_page_table_width=None,
canonical_page_table_width=None,
):
raise AssertionError("planned")
monkeypatch.setattr(prefill_module, "_plan_prefill_requests", fail_if_planned)
assert runtime.can_trace(tokens=tokens, prompt_lens=prompt_lens, start_pos=start_pos)
assert runtime.can_trace(
tokens=tokens,
prompt_lens=prompt_lens,
start_pos=torch.tensor([0, 32]),
)
assert not runtime.can_trace(
tokens=tokens,
prompt_lens=prompt_lens,
start_pos=torch.tensor([0, 1]),
)
assert not runtime.can_trace(
tokens=torch.zeros((1, 2049), dtype=torch.long),
prompt_lens=torch.tensor([2049]),
)
def test_sampling_values_keep_logical_prefill_user_contract_for_partial_batch():
values = sampling_helpers._formatted_sampling_values(
SamplingParams(temperature=1.0, top_k=32, top_p=0.08),
1,
)
assert tuple(len(field) for field in values[:3]) == (1, 1, 1)
assert values[0][0] == 32
assert values[1][0] == pytest.approx(0.08)
assert values[2][0] == 1.0
def test_sampling_values_accept_vector_tensor_fields_for_full_batch():
values = sampling_helpers._formatted_sampling_values(
SamplingParams(
temperature=torch.zeros(32),
top_k=torch.ones(32, dtype=torch.int32),
top_p=torch.ones(32),
),
32,
)
assert tuple(len(field) for field in values[:3]) == (32, 32, 32)
assert values[0] == (1,) * 32
assert values[1] == (0.0,) * 32
assert values[2] == (1.0,) * 32
def test_prefill_tokens_supply_masked_prompt_history_when_plugin_history_is_absent():
tokens = torch.tensor([[11, 12, 0, 0], [21, 22, 23, 0]])
history = prefill_module._prompt_history_from_prefill_tokens(
tokens,
torch.tensor([2, 3]),
)
assert torch.equal(history, torch.tensor([[11, 12, -1, -1], [21, 22, 23, -1]]))
assert torch.equal(tokens, torch.tensor([[11, 12, 0, 0], [21, 22, 23, 0]]))
def test_single_greedy_prefill_uses_argmax_without_changing_batched_sampling():
runtime = _runtime(allow_force_argmax=True)
greedy = SamplingParams(temperature=0.0, top_k=32, top_p=0.08)
single_inputs = _inputs(prompt_length=80)
single = runtime.prepare(
tokens=single_inputs[0],
page_table=single_inputs[1],
prompt_lens=single_inputs[2],
sampling_params=greedy,
)
batched_inputs = _inputs(prompt_length=80, rows=2)
batched = runtime.prepare(
tokens=batched_inputs[0],
page_table=batched_inputs[1],
prompt_lens=batched_inputs[2],
empty_slots=(0, 1),
sampling_params=greedy,
)
assert single[0].sampling_path == "argmax"
assert batched[0].sampling_path == "topk"
assert single[0].program_signatures[0].sampling_path == "argmax"
assert single[0].program_signatures[0].last_token_tile_start == 64
def test_trace_finish_reuses_trace_owned_sample_output(monkeypatch):
runtime = _runtime()
request = _plan(prompt_length=80)[0]
prepared = SimpleNamespace(
request=request,
sampling_params=SamplingParams(temperature=1.0, top_k=32, top_p=0.08),
sampling_path="topk",
)
workspace = PrefillReplayState(
position_inputs=PrefillPositionInputs("start", "end", "row"),
kpt=("k", "p", "temperature"),
sampled_output="persistent-output",
)
seen = []
def finish_regular_prefill(
prepared,
hidden,
kpt,
position_inputs,
*,
sampled_output=None,
owned=None,
count_tokens=True,
):
seen.append(((prepared, hidden, kpt, position_inputs), {"sampled_output": sampled_output, "owned": owned}))
return "tokens", None
monkeypatch.setattr(runtime.postprocessor, "finish_regular_prefill", finish_regular_prefill)
result = runtime.finish_trace(prepared, "hidden", workspace)
assert result.value == ("tokens", None)
assert result.owned == ()
assert seen[0][1]["sampled_output"] == "persistent-output"
def test_trace_refresh_skips_unchanged_position_and_sampling_inputs(monkeypatch):
runtime = _runtime()
request = _plan(prompt_length=80)[0]
sampling = SamplingParams(temperature=0.0, top_k=1, top_p=1.0)
prepared = SimpleNamespace(request=request, sampling_params=sampling, sampling_path="topk")
k, p, temperature, _ = sampling_helpers._formatted_sampling_values(
sampling,
runtime.postprocessor.sampling_output_rows(prepared),
)
persistent = PrefillHiddenPersistentInputs(
device_inputs=PrefillDeviceInputs("tokens", "cos", "sin", "page", None, "positions", None),
)
workspace = PrefillReplayState(
position_inputs=PrefillPositionInputs("start", "end", "row"),
kpt=("k", "p", "temperature"),
position_signature=79,
kpt_signature=(k, p, temperature),
)
monkeypatch.setattr(prefill_inputs_module.ttnn, "ReplicateTensorToMesh", lambda mesh: "mapper")
# ttnn.from_torch is an overloaded backend API; this test only distinguishes source dtype.
monkeypatch.setattr(
prefill_inputs_module.ttnn,
"from_torch",
lambda value, **kwargs: "host-page" if value.dtype == torch.int32 else "host-tokens",
)
copied = []
monkeypatch.setattr(
prefill_inputs_module.ttnn,
"copy_host_to_device_tensor",
lambda host, device: copied.append((host, device)),
)
def fail_position_refresh(relative_last, sequence_length):
pytest.fail("position refreshed")
def fail_sampling_refresh(device_kpt, sampling_params, batch_size, force_topk):
pytest.fail("sampling refreshed")
monkeypatch.setattr(runtime.inputs, "prepare_position_inputs_host", fail_position_refresh)
monkeypatch.setattr(runtime.postprocessor, "refresh_kpt", fail_sampling_refresh)
runtime.refresh_trace(prepared, persistent, workspace)
assert copied == [("host-tokens", "tokens"), ("host-page", "page")]
def test_trace_refresh_updates_runtime_position_inputs_for_single_logits(monkeypatch):
# Host-sampling single requests pick their last token on device from runtime bounds (no
# offset-keyed programs), so a replay at a new prompt offset must refresh the position inputs.
runtime = _runtime()
request = _plan(prompt_length=80)[0]
prepared = SimpleNamespace(request=request, sampling_params=None, sampling_path="logits")
persistent = PrefillHiddenPersistentInputs(
device_inputs=PrefillDeviceInputs("tokens", "cos", "sin", "page", None, "positions", None),
)
workspace = PrefillReplayState(
position_inputs=PrefillPositionInputs("start", "end", "row"),
kpt=None,
position_signature=0,
)
monkeypatch.setattr(prefill_inputs_module.ttnn, "ReplicateTensorToMesh", lambda mesh: "mapper")
monkeypatch.setattr(
prefill_inputs_module.ttnn,
"from_torch",
lambda value, **kwargs: "host-page" if value.dtype == torch.int32 else "host-tokens",
)
copied = []
monkeypatch.setattr(
prefill_inputs_module.ttnn,
"copy_host_to_device_tensor",
lambda host, device: copied.append((host, device)),
)
refreshed = []
def prepare_position_inputs_host(relative_last, sequence_length):
refreshed.append((relative_last, sequence_length))
return PrefillPositionInputs("host-start", "host-end", "host-row")
monkeypatch.setattr(runtime.inputs, "prepare_position_inputs_host", prepare_position_inputs_host)
runtime.refresh_trace(prepared, persistent, workspace)
assert refreshed == [(79, request.padded_sequence_length)]
assert copied == [
("host-tokens", "tokens"),
("host-page", "page"),
("host-start", "start"),
("host-end", "end"),
("host-row", "row"),
]
assert workspace.position_signature == 79
refreshed.clear()
copied.clear()
runtime.refresh_trace(prepared, persistent, workspace)
assert refreshed == []
assert copied == [("host-tokens", "tokens"), ("host-page", "page")]
def test_eager_sampled_prefill_uses_preallocated_output(monkeypatch):
runtime = _runtime()
request = _plan(prompt_length=80)[0]
prepared = SimpleNamespace(
request=request,
sampling_params=SamplingParams(temperature=1.0, top_k=32, top_p=0.08),
sampling_path="topk",
)
events = []
monkeypatch.setattr(
runtime.inputs,
"stage_step",
lambda prepared, chunk, final_relative_last: events.append("stage") or ("device-inputs", "positions"),
)
monkeypatch.setattr(
runtime.postprocessor,
"make_device_kpt",
lambda sampling_params, batch_size, force_topk: events.append("kpt") or "kpt",
)
monkeypatch.setattr(
runtime.sequence_runner,
"_execute_step",
lambda prepared, chunk, device_inputs, position_inputs: events.append("execute") or "hidden",
)
monkeypatch.setattr(
runtime.postprocessor,
"make_sampling_output",
lambda batch_size: events.append("sample-output") or "sample-output",
)
seen = []
def finish_regular_prefill(
prepared,
hidden,
kpt,
position_inputs,
*,
sampled_output=None,
owned=None,
count_tokens=True,
):
events.append("finish")
seen.append(
(
(prepared, hidden, kpt, position_inputs),
{"sampled_output": sampled_output, "owned": owned},
)
)
return sampled_output
monkeypatch.setattr(runtime.postprocessor, "finish_regular_prefill", finish_regular_prefill)
result = runtime.sequence_runner.run(prepared)
assert result.value == "sample-output"
assert result.owned == ("device-inputs", "positions", "kpt", "hidden", "sample-output")
assert seen[0][1]["sampled_output"] == "sample-output"
assert seen[0][1]["owned"] is not None
assert events == ["stage", "kpt", "execute", "sample-output", "finish"]
def test_sampling_output_is_allocated_before_capture_with_device_shape(monkeypatch):
runtime = _runtime()
seen = []
# ttnn.from_torch is overloaded; allocation options are the behavior under test.
monkeypatch.setattr(
prefill_inputs_module.ttnn,
"from_torch",
lambda tensor, **kwargs: seen.append((tensor, kwargs)) or "device-output",
)
monkeypatch.setattr(postprocess_module.ttnn, "ReplicateTensorToMesh", lambda mesh: "replicate")
assert runtime.postprocessor.make_sampling_output(1) == "device-output"
assert tuple(seen[0][0].shape) == (1, 1, 1, 1)
assert seen[0][1]["device"] is runtime.config.mesh_device
assert seen[0][1]["mesh_mapper"] == "replicate"
@pytest.mark.parametrize("shape", [(1, 1, 1, 32), (1, 1, 32, 1)])
def test_sampled_token_normalization_converts_only_first_replica(monkeypatch, shape):
class DistributedTensor:
pass
first = torch.arange(32, dtype=torch.int32).reshape(shape)
second = torch.full(shape, 99, dtype=torch.int32)
converted = []
monkeypatch.setattr(postprocess_module.ttnn, "Tensor", DistributedTensor)
monkeypatch.setattr(postprocess_module.ttnn, "get_device_tensors", lambda value: [first, second])
monkeypatch.setattr(
postprocess_module.ttnn,
"to_torch",
lambda value: converted.append(value) or value,
)
tokens = process_output_tokens(DistributedTensor(), 32, (1, 2))
assert tokens.tolist() == list(range(32))
assert converted == [first]
@pytest.mark.parametrize("uncached_length", [1, 31, 32, 33, 127, 128, 129, 2048, 2049])
@pytest.mark.parametrize("cached_tokens", [0, 32, 64])
def test_planning_preserves_uncached_slice_and_absolute_chunk_positions(uncached_length, cached_tokens):
prompt_length = cached_tokens + uncached_length
request = _plan(prompt_length=prompt_length, cached_tokens=cached_tokens)[0]
assert request.cached_tokens == (cached_tokens,)
assert request.prompt_lengths == (prompt_length,)
assert request.last_token_indices == (prompt_length - 1,)
assert torch.equal(
request.tokens[0, :uncached_length],
torch.arange(cached_tokens, prompt_length, dtype=torch.long),
)
assert request.chunks[0].token_slice.start == 0
assert request.chunks[0].chunk_start_idx == cached_tokens
assert request.chunks[-1].contains_last_token
assert request.chunks[-1].token_slice.start <= uncached_length - 1 < request.chunks[-1].token_slice.stop
def test_four_regular_cached_and_multi_chunk_cases_share_one_plan_shape():
regular = _plan(prompt_length=128)[0]
cached_one = _plan(prompt_length=160, cached_tokens=32)[0]
uncached_multi = _plan(prompt_length=4096)[0]
cached_multi = _plan(prompt_length=4129, cached_tokens=96)[0]
assert not regular.uses_chunked_prefill and len(regular.chunks) == 1
assert cached_one.uses_chunked_prefill and len(cached_one.chunks) == 1
assert uncached_multi.uses_chunked_prefill and len(uncached_multi.chunks) == 2
assert cached_multi.uses_chunked_prefill and len(cached_multi.chunks) == 2
assert all(
chunk.chunk_page_table is not None
for request in (cached_one, uncached_multi, cached_multi)
for chunk in request.chunks
)
def test_chunk_mapping_uses_absolute_blocks_pads_sentinels_and_stops_after_last_token():
request = _plan(prompt_length=4129, cached_tokens=96)[0]
first, second = request.chunks
assert (first.chunk_start_idx, second.chunk_start_idx) == (96, 2144)
assert torch.equal(first.chunk_page_table[0], request.page_table[0, 3:67])
assert torch.equal(second.chunk_page_table[0, :63], request.page_table[0, 67:130])
assert second.chunk_page_table[0, 63].item() == -1
early_stop = _plan(prompt_length=4097)[0]
assert early_stop.padded_sequence_length == 8192
assert len(early_stop.chunks) == 3
assert early_stop.chunks[-1].token_slice == slice(4096, 6144)
assert early_stop.chunks[-1].contains_last_token
def test_regular_page_table_uses_skip_sentinel_for_unallocated_tail():
request = _plan(prompt_length=80)[0]
assert not request.uses_chunked_prefill
assert request.chunks[0].chunk_page_table is None
assert torch.equal(request.page_table[0, :3], torch.arange(3, dtype=torch.int32))
assert torch.all(request.page_table[0, 3:] == -1)
def test_chunked_full_table_keeps_in_range_filler_while_fill_table_uses_skip_sentinel():
prompt_length = 4129
cached_tokens = 96
actual_blocks = math.ceil(prompt_length / 32)
page_table = torch.full((1, 256), 10_000, dtype=torch.int32)
page_table[0, :actual_blocks] = 100 + torch.arange(actual_blocks, dtype=torch.int32)
tokens = torch.arange(prompt_length, dtype=torch.long).reshape(1, prompt_length)
request = _plan_prefill_requests(
tokens=tokens,
page_table=page_table,
prompt_lens=torch.tensor([prompt_length]),
empty_slots=(0,),
start_pos=torch.tensor([cached_tokens]),
block_size=32,
max_batch_size=32,
max_prefill_chunk_size=2048,
supports_batched_prefill=True,
max_actual_page_table_width=256,
canonical_page_table_width=264,
)[0]
assert request.uses_chunked_prefill
assert torch.equal(request.page_table[0, :actual_blocks], page_table[0, :actual_blocks])
assert torch.all(request.page_table[0, actual_blocks:] == 0)
assert torch.all(request.page_table >= 0)
assert not torch.any(request.page_table == 10_000)
first, second = request.chunks
assert torch.equal(first.chunk_page_table[0], page_table[0, 3:67])
assert torch.equal(second.chunk_page_table[0, :63], page_table[0, 67:130])
assert second.chunk_page_table[0, 63].item() == -1
assert not torch.any(first.chunk_page_table == 10_000)
assert not torch.any(second.chunk_page_table == 10_000)
def test_full_and_truncated_scheduler_tables_produce_equivalent_semantic_plans():
prompt_length = 160
cached_tokens = 32
actual_width = 5
full = _plan(prompt_length=prompt_length, cached_tokens=cached_tokens, page_width=256)[0]
truncated = _plan(prompt_length=prompt_length, cached_tokens=cached_tokens, page_width=actual_width)[0]
assert torch.equal(full.tokens, truncated.tokens)
assert torch.equal(full.page_table, truncated.page_table)
assert full.source_rows == truncated.source_rows
assert full.slots == truncated.slots
assert len(full.chunks) == len(truncated.chunks)
assert torch.equal(full.chunks[0].chunk_page_table, truncated.chunks[0].chunk_page_table)
def test_q128_batching_pads_whole_wave_and_maps_noncontiguous_slots_to_local_rows():
requests = _plan(prompt_length=80, slots=(7, 3, 11))
assert [request.kind for request in requests] == ["batched"]
assert [request.source_rows for request in requests] == [(0, 1, 2)]
assert [request.slots for request in requests] == [(7, 3, 11)]
assert [request.padded_batch_size for request in requests] == [4]
assert torch.equal(requests[0].tokens[0, :80], torch.arange(80))
assert torch.equal(requests[0].tokens[1, :80], torch.arange(80, 160))
def test_q128_batching_accepts_different_exact_prompt_lengths():
tokens = torch.arange(3 * 128, dtype=torch.long).reshape(3, 128)
page_table = torch.arange(3 * 256, dtype=torch.int32).reshape(3, 256)
requests = _plan_prefill_requests(
tokens=tokens,
page_table=page_table,
prompt_lens=torch.tensor([87, 115, 125]),
empty_slots=[0, 1, 2],
start_pos=torch.zeros(3, dtype=torch.long),
block_size=32,
max_batch_size=32,
max_prefill_chunk_size=2048,
supports_batched_prefill=True,
max_actual_page_table_width=256,
canonical_page_table_width=264,
)
assert len(requests) == 1
assert [request.kind for request in requests] == ["batched"]
assert requests[0].prompt_lengths == (87, 115, 125)
assert requests[0].padded_sequence_length == 128
assert requests[0].padded_batch_size == 4
@pytest.mark.parametrize(("prompt_lengths", "padded_length"), [((87, 115), 128), ((129, 900), 1024)])
def test_batched_prefill_copies_only_allocated_page_prefixes_and_sanitizes_tails(prompt_lengths, padded_length):
rows = len(prompt_lengths)
token_width = max(prompt_lengths)
tokens = torch.arange(rows * token_width, dtype=torch.long).reshape(rows, token_width)
page_table = 1000 + torch.arange(rows * 256, dtype=torch.int32).reshape(rows, 256)
request = _plan_prefill_requests(
tokens=tokens,
page_table=page_table,
prompt_lens=torch.tensor(prompt_lengths),
empty_slots=[7, 3],
start_pos=torch.zeros(rows, dtype=torch.long),
block_size=32,
max_batch_size=32,
max_prefill_chunk_size=2048,
supports_batched_prefill=True,
max_actual_page_table_width=256,
canonical_page_table_width=264,
)[0]
assert request.padded_sequence_length == padded_length
for row, prompt_length in enumerate(prompt_lengths):
actual_width = math.ceil(prompt_length / 32)
assert torch.equal(request.page_table[row, :actual_width], page_table[row, :actual_width])
assert torch.all(request.page_table[row, actual_width:] == -1)
assert torch.all(request.page_table[len(prompt_lengths) :] == -1)
def test_batched_prefill_short_row_reuse_does_not_copy_stale_long_row_tail():
tokens = torch.arange(3 * 128, dtype=torch.long).reshape(3, 128)
page_table = torch.full((3, 256), 9_999, dtype=torch.int32)
page_table[:, :4] = torch.arange(12, dtype=torch.int32).reshape(3, 4)
request = _plan_prefill_requests(
tokens=tokens,
page_table=page_table,
prompt_lens=torch.tensor([33, 80, 128]),
empty_slots=(0, 1, 2),
start_pos=torch.zeros(3, dtype=torch.long),
block_size=32,
max_batch_size=32,
max_prefill_chunk_size=2048,
supports_batched_prefill=True,
max_prefill_batch_size=8,
max_actual_page_table_width=256,
canonical_page_table_width=264,
)[0]
assert torch.equal(request.page_table[0, :2], page_table[0, :2])
assert torch.all(request.page_table[0, 2:] == -1)
assert not torch.any(request.page_table == 9_999)
def test_omitted_batched_policy_preserves_only_legacy_contiguous_q128_behavior():
contiguous = _inputs(prompt_length=80, rows=3)
arguments = dict(
tokens=contiguous[0],
page_table=contiguous[1],
prompt_lens=contiguous[2],
start_pos=contiguous[3],
block_size=32,
max_batch_size=32,
max_prefill_chunk_size=2048,
)
legacy = _plan_prefill_requests(empty_slots=(0, 1, 2), **arguments)
noncontiguous = _plan_prefill_requests(empty_slots=(7, 3, 11), **arguments)
longer = _plan_prefill_requests(
tokens=torch.zeros((2, 1024), dtype=torch.long),
page_table=torch.zeros((2, 256), dtype=torch.int32),
prompt_lens=torch.full((2,), 1024, dtype=torch.long),
empty_slots=(0, 1),
start_pos=torch.zeros(2, dtype=torch.long),
block_size=32,
max_batch_size=32,
max_prefill_chunk_size=2048,
)
assert len(legacy) == 1
assert legacy[0].kind == "batched"
assert legacy[0].padded_batch_size == 4
assert [request.kind for request in noncontiguous] == ["single", "single", "single"]
assert [request.kind for request in longer] == ["single", "single"]
def test_omitted_batched_policy_keeps_mixed_cache_hits_sequential():
requests = _plan_prefill_requests(
tokens=torch.zeros((3, 128), dtype=torch.long),
page_table=torch.zeros((3, 256), dtype=torch.int32),
prompt_lens=torch.tensor([80, 128, 96]),
empty_slots=(0, 1, 2),
start_pos=torch.tensor([0, 128, 0]),
block_size=32,
max_batch_size=32,
max_prefill_chunk_size=2048,
)
assert [request.kind for request in requests] == ["single", "single"]
assert [request.source_rows for request in requests] == [(0,), (2,)]
def test_omitted_batched_policy_keeps_legacy_padded_partial_trace_eligible():
runtime = _runtime(supports_batched_prefill=None)
tokens, page_table, prompt_lens, start_pos = _inputs(prompt_length=80, rows=3)
prepared = runtime.prepare(
tokens=tokens,
page_table=page_table,
prompt_lens=prompt_lens,
start_pos=start_pos,
empty_slots=(0, 1, 2),
)
assert len(prepared) == 1
assert prepared[0].request.padded_batch_size == 4
assert prepared[0].trace_signature is not None
assert not hasattr(prepared[0].trace_signature, "active_batch_size")
assert not hasattr(prepared[0].program_signatures[0], "active_batch_size")
assert torch.all(prepared[0].request.page_table[3] == -1)
tokens4, page_table4, prompt_lens4, start_pos4 = _inputs(prompt_length=80, rows=4)
full = runtime.prepare(
tokens=tokens4,
page_table=page_table4,
prompt_lens=prompt_lens4,
start_pos=start_pos4,
empty_slots=(0, 1, 2, 3),
)[0]
assert full.trace_signature is not None
assert full.trace_signature == prepared[0].trace_signature
assert full.program_signatures == prepared[0].program_signatures
seen = []
runtime.config.model.prefill_forward = lambda *args, **kwargs: seen.append(kwargs) or "hidden"
device_inputs = PrefillDeviceInputs("tokens", "cos", "sin", "page", None, "positions", None)
assert runtime._run_hidden_body(prepared[0].request, device_inputs) == "hidden"
assert seen[0]["user_id"] == [0, 1, 2]
def test_mixed_lengths_batch_per_bucket_and_preserve_source_mapping():
tokens = torch.arange(3 * 160, dtype=torch.long).reshape(3, 160)
page_table = torch.arange(3 * 256, dtype=torch.int32).reshape(3, 256)
requests = _plan_prefill_requests(
tokens=tokens,
page_table=page_table,
prompt_lens=torch.tensor([33, 128, 129]),
empty_slots=[4, 1, 9],
start_pos=torch.zeros(3, dtype=torch.long),
block_size=32,
max_batch_size=32,
max_prefill_chunk_size=2048,
supports_batched_prefill=True,
max_actual_page_table_width=256,
canonical_page_table_width=264,
)
assert [request.kind for request in requests] == ["batched", "single"]
assert [request.source_rows for request in requests] == [(0, 1), (2,)]
assert [request.slots for request in requests] == [(4, 1), (9,)]
assert [request.padded_sequence_length for request in requests] == [128, 1024]
def test_mixed_cache_hit_cached_and_batchable_rows_preserve_planning_page_rows_and_assembly(monkeypatch):
runtime = _runtime()
token_width = 1056
tokens = torch.arange(4 * token_width, dtype=torch.long).reshape(4, token_width)
page_table = 1000 + torch.arange(4 * 256, dtype=torch.int32).reshape(4, 256)
prepared = runtime.prepare(
tokens=tokens,
page_table=page_table,
prompt_lens=torch.tensor([87, 128, 120, 1056]),
start_pos=torch.tensor([0, 128, 0, 32]),
empty_slots=(7, 3, 11, 5),
)
assert [item.request.source_rows for item in prepared] == [(0, 2), (3,)]
assert [item.request.slots for item in prepared] == [(7, 11), (5,)]
assert torch.equal(prepared[0].request.page_table[0, :3], page_table[0, :3])
assert prepared[0].request.page_table[0, 3].item() == -1
assert torch.equal(prepared[0].request.page_table[1, :4], page_table[2, :4])
batched_host = torch.zeros(1, 1, 32, runtime.config.model.vocab_size)
batched_host[0, 0, 0, :] = 10
batched_host[0, 0, 1, :] = 20
cached_host = torch.zeros_like(batched_host)
cached_host[0, 0, 31, :] = 30
monkeypatch.setattr(runtime, "_release_or_retain_transient", lambda value: [])
output = runtime.assemble(
[
(prepared[0], InvocationResult(batched_host, "batched-owned")),
(prepared[1], InvocationResult(cached_host, "cached-owned")),
],
batch_size=4,
)
assert output[:, 0, 0].tolist() == [10, 0, 20, 30]
@pytest.mark.parametrize(
("overrides"),
[
{"supports_batched_prefill": False},
{"disable_batched_prefill": True},
],
)
def test_batched_prefill_policy_is_opt_in_and_has_runtime_escape_hatches(overrides):
requests = _plan(prompt_length=80, slots=(0, 1), **overrides)
assert [request.kind for request in requests] == ["single", "single"]
def test_disabled_batched_extract_keeps_batched_forward_and_uses_per_slot_postprocess(monkeypatch):
runtime = _runtime(batched_prefill_batched_extract=False)
tokens, page_table, prompt_lens, start_pos = _inputs(prompt_length=80, rows=2)
prepared = runtime.prepare(
tokens=tokens,
page_table=page_table,
prompt_lens=prompt_lens,
start_pos=start_pos,
empty_slots=(7, 3),
)[0]
hidden = torch.arange(2 * 128, dtype=torch.float32).reshape(1, 1, 256, 1)
seen = []
monkeypatch.setattr(postprocess_module.ttnn, "reshape", lambda value, shape: value.reshape(shape))
def post_process_prefill_output(slot_hidden, last_token):
seen.append((slot_hidden.clone(), last_token))
return torch.full((1, 1, 32, runtime.config.model.vocab_size), len(seen), dtype=torch.float32)
runtime.config.model.post_process_prefill_output = post_process_prefill_output
monkeypatch.setattr(postprocess_module.ttnn, "untilize", lambda logits, **kwargs: logits)
outputs = runtime.postprocessor.finish_regular_prefill(
prepared,
hidden,
None,
PrefillPositionInputs("unused-start", "unused-end", "unused-row"),
)
assert prepared.request.kind == "batched"
assert [last_token for _, last_token in seen] == [79, 79]
assert torch.equal(seen[0][0], hidden.reshape(2, 1, 128, 1)[0:1])
assert torch.equal(seen[1][0], hidden.reshape(2, 1, 128, 1)[1:2])
assert len(outputs) == 2
assembled = runtime.assemble(
[(prepared, InvocationResult(outputs, "owned"))],
batch_size=2,
)
assert assembled[:, 0, 0].tolist() == [1, 2]
def test_disabled_batched_extract_uses_sequential_path_for_device_sampling():
runtime = _runtime(batched_prefill_batched_extract=False)
tokens, page_table, prompt_lens, start_pos = _inputs(prompt_length=80, rows=2)
prepared = runtime.prepare(
tokens=tokens,
page_table=page_table,
prompt_lens=prompt_lens,
start_pos=start_pos,
empty_slots=(7, 3),
sampling_params=SamplingParams(temperature=1.0, top_k=32, top_p=0.08),
)
assert [item.request.kind for item in prepared] == ["single", "single"]
def test_batched_prefill_model_cap_falls_back_instead_of_splitting_wave():
requests = _plan(
prompt_length=1024,
slots=tuple(range(32)),
max_prefill_batch_size=8,
)
assert len(requests) == 32
assert all(request.kind == "single" for request in requests)
def test_batched_prefill_active_30_pads_one_whole_wave_to_32():
requests = _plan(
prompt_length=128,
slots=tuple(range(30)),
max_prefill_batch_size=32,
)
assert len(requests) == 1
assert requests[0].padded_batch_size == 32
assert requests[0].source_rows == tuple(range(30))
def test_batched_prefill_active_15_pads_one_whole_wave_to_16():
requests = _plan(
prompt_length=128,
slots=tuple(range(15)),
max_prefill_batch_size=16,
)
assert len(requests) == 1
assert requests[0].kind == "batched"
assert requests[0].padded_batch_size == 16
assert requests[0].source_rows == tuple(range(15))
assert torch.all(requests[0].tokens[15] == 0)
assert torch.all(requests[0].page_table[15] == -1)
def test_active_15_and_16_share_complete_program_and_trace_signatures(monkeypatch):
runtime = _runtime(max_prefill_batch_size=16)
def prepare(rows):
tokens, page_table, prompt_lens, start_pos = _inputs(prompt_length=80, rows=rows)
return runtime.prepare(
tokens=tokens,
page_table=page_table,
prompt_lens=prompt_lens,
start_pos=start_pos,
empty_slots=tuple(range(rows)),
)[0]
partial = prepare(15)
full = prepare(16)
assert partial.request.padded_batch_size == full.request.padded_batch_size == 16
assert partial.program_signatures == full.program_signatures
assert partial.trace_signature == full.trace_signature
monkeypatch.setattr(postprocess_module.ttnn, "synchronize_device", lambda mesh: None)
compiler = ProgramCompiler("mesh", lambda: object())
first = compiler.compile(partial.program_signatures[0], lambda _: torch.zeros(1))
second = compiler.compile(
full.program_signatures[0],
lambda _: pytest.fail("shared padded program was recompiled"),
)
assert second is first
fills = []
monkeypatch.setattr(
runtime,
"_run_hidden_body",
lambda request, device_inputs, *, fill_rows=None: fills.append(fill_rows) or "hidden",
)
persistent = PrefillHiddenPersistentInputs(device_inputs="device-inputs")
partial_plan = runtime.capture_plan(partial)
full_plan = runtime.capture_plan(full)
assert partial_plan.schema_fingerprint == full_plan.schema_fingerprint
assert partial_plan.capture(persistent) == full_plan.capture(persistent) == "hidden"
assert fills == [16, 16]
def test_dp2_lane_local_active_15_waves_each_pad_to_16():
lane_requests = [
_plan(
prompt_length=128,
slots=tuple(range(15)),
max_batch_size=16,
max_prefill_batch_size=16,
)[0]
for _ in range(2)
]
assert sum(len(request.source_rows) for request in lane_requests) == 30
assert [request.padded_batch_size for request in lane_requests] == [16, 16]
def test_batched_prefill_arbitrary_lane_capacity_falls_back_without_padding_to_capacity():
requests = _plan(
prompt_length=128,
slots=tuple(range(9)),
max_batch_size=12,
max_prefill_batch_size=16,
)
assert len(requests) == 9
assert all(request.kind == "single" for request in requests)
def test_batched_prefill_without_a_supported_whole_wave_size_falls_back_sequentially():
tokens, page_table, prompt_lens, start_pos = _inputs(prompt_length=128, rows=33)
requests = _plan_prefill_requests(
tokens=tokens,
page_table=page_table,
prompt_lens=prompt_lens,
empty_slots=tuple(range(33)),
start_pos=start_pos,
block_size=32,
max_batch_size=33,
max_prefill_chunk_size=2048,
supports_batched_prefill=True,
max_prefill_batch_size=32,
max_actual_page_table_width=256,
canonical_page_table_width=264,
)
assert len(requests) == 33
assert all(request.kind == "single" for request in requests)
@pytest.mark.parametrize("prompt_length", [129, 1024, 1025, 2048])
def test_batched_prefill_accepts_arbitrary_uniform_length_buckets_through_chunk_limit(prompt_length):
requests = _plan(prompt_length=prompt_length, slots=(0, 1))
assert len(requests) == 1
assert requests[0].kind == "batched"
assert requests[0].padded_sequence_length <= 2048
def test_batched_prefill_strict_token_guard_rejects_128k_fold():
requests = _plan(
prompt_length=4096,
slots=tuple(range(32)),
maximum=4096,
max_prefill_batch_size=32,
)
assert len(requests) == 32
assert all(request.kind == "single" for request in requests)
@pytest.mark.parametrize(
("prompt_length", "expected_kind"),
[(2048, "batched"), (4096, "single"), (4097, "single")],
)
def test_batched_prefill_strict_token_budget_boundary(prompt_length, expected_kind):
requests = _plan(
prompt_length=prompt_length,
slots=tuple(range(32)),
maximum=8192,
max_prefill_batch_size=32,
)
if expected_kind == "batched":
assert len(requests) == 1
else:
assert len(requests) == 32
assert all(request.kind == expected_kind for request in requests)
def test_disable_batched_prefill_environment_is_checked_per_prepare(monkeypatch):
runtime = _runtime()
tokens, page_table, prompt_lens, start_pos = _inputs(prompt_length=80, rows=2)
kwargs = {
"tokens": tokens,
"page_table": page_table,
"prompt_lens": prompt_lens,
"start_pos": start_pos,
"empty_slots": (0, 1),
}
assert [prepared.request.kind for prepared in runtime.prepare(**kwargs)] == ["batched"]
monkeypatch.setenv("DISABLE_BATCHED_PREFILL", "1")
assert [prepared.request.kind for prepared in runtime.prepare(**kwargs)] == ["single", "single"]
def test_program_signatures_are_material_and_trace_classification_is_separate_from_planning():
runtime = _runtime()
def prepare(prompt_length, cached_tokens=0, sampling_params=None):
tokens, page_table, prompt_lens, start_pos = _inputs(
prompt_length=prompt_length,
cached_tokens=cached_tokens,
)
return runtime.prepare(
tokens=tokens,
page_table=page_table,
prompt_lens=prompt_lens,
start_pos=start_pos,
sampling_params=sampling_params,
)[0]
logits = prepare(80)
topk = prepare(80, sampling_params=SamplingParams(temperature=1.0, top_k=32, top_p=0.08))
cached = prepare(112, cached_tokens=32)
multi = prepare(4096)
assert logits.program_signatures != topk.program_signatures
assert dict(logits.program_signatures[0].key_material())["sampling_path"] == "logits"
assert logits.trace_signature is not None
assert cached.trace_signature is not None
assert cached.trace_signature.operation_variant == "chunked"
assert multi.trace_signature is None
assert logits.request.uses_chunked_prefill is False
assert cached.request.uses_chunked_prefill is True
def test_prefill_signature_keys_and_trace_fingerprints_have_stable_goldens():
program_signature = PrefillProgramSignature(
operation_variant="regular-batched",
padded_batch_size=16,
invocation_sequence_length=128,
page_table_width=32,
chunk_page_table_width=None,
sampling_path="topk",
)
trace_signature = PrefillTraceSignature(
operation_variant="chunked",
padded_batch_size=1,
padded_sequence_length=512,
page_table_width=32,
chunk_page_table_width=4,
)
prepared = PreparedPrefill(
request=SimpleNamespace(),
sampling_params=SamplingParams(temperature=1.0, top_k=32, top_p=0.08),
sampling_path="topk",
program_signatures=(program_signature,),
trace_signature=trace_signature,
)
assert ProgramKey.from_signature(program_signature).digest == (
"1fb47a00a66b522391f24ce966d44cbfe133b2623f5e2f7ae03cf2c31f6a42c5"
)
assert TraceKey.from_signature(trace_signature).digest == (
"4b71c7b3d465fe51a661a621c45b7952f7cc7028adb171d7e9b17161b07113b0"
)
assert capture_schema_fingerprint(prepared) == (
"prefill-hidden-v2",
trace_signature,
(
"operation_variant",
"padded_batch_size",
"padded_sequence_length",
"page_table_width",
"chunk_page_table_width",
),
)
assert workspace_fingerprint(prepared, sampling_output_rows=32) == (
"prefill-postprocess-v1",
"topk",
32,
True,
)
assert ProgramKey.from_signature(
dataclasses.replace(program_signature, penalties_enabled=True)
) != ProgramKey.from_signature(program_signature)
assert ProgramKey.from_signature(
dataclasses.replace(program_signature, logprobs_enabled=True)
) != ProgramKey.from_signature(program_signature)
assert TraceKey.from_signature(
dataclasses.replace(trace_signature, sampling_path="topk")
) != TraceKey.from_signature(trace_signature)
def test_cached_offsets_share_one_chunk_trace_identity_and_can_trace_contract():
runtime = _runtime()
def prepare(prompt_length, cached_tokens):
tokens, page_table, prompt_lens, start_pos = _inputs(
prompt_length=prompt_length,
cached_tokens=cached_tokens,
)
assert runtime.can_trace(tokens=tokens, prompt_lens=prompt_lens, start_pos=start_pos)
return runtime.prepare(
tokens=tokens,
page_table=page_table,
prompt_lens=prompt_lens,
start_pos=start_pos,
)[0]
cached_32 = prepare(112, 32)
resumed_64 = prepare(144, 64)
assert cached_32.trace_signature == resumed_64.trace_signature
assert cached_32.program_signatures == resumed_64.program_signatures
assert cached_32.trace_signature.chunk_page_table_width == 4
assert torch.all(cached_32.request.page_table >= 0)
assert cached_32.request.chunks[0].chunk_page_table[0, -1].item() == -1
def test_fixed_chunk_trace_family_exposes_host_replay_steps_and_dynamic_capture_start():
runtime = _runtime(trace_lengths=(128, 1024, 2048))
tokens, page_table, prompt_lens, start_pos = _inputs(prompt_length=4096)
prepared = runtime.prepare(
tokens=tokens,
page_table=page_table,
prompt_lens=prompt_lens,
start_pos=start_pos,
)[0]
assert prepared.trace_signature is not None
assert prepared.trace_signature.operation_variant == "chunked"
assert prepared.trace_signature.padded_sequence_length == 2048
assert len(prepared.request.chunks) == 2
assert len(prepared.program_signatures) == 1
assert torch.all(prepared.request.page_table >= 0)
persistent = PrefillHiddenPersistentInputs(
device_inputs=PrefillDeviceInputs("tokens", "cos", "sin", "page", "chunk-page", "positions", "start")
)
captured = []
runtime.config.model.prefill_forward = lambda *args, **kwargs: captured.append(kwargs) or "hidden"
assert runtime.capture_plan(prepared).capture(persistent) == "hidden"
assert captured[0]["chunk_start_idx"] is None
assert captured[0]["chunk_start_idx_tensor"] == "start"
def test_chunk_trace_refresh_updates_token_start_tables_rotary_and_preserves_b6_tables(monkeypatch):
runtime = _runtime(trace_lengths=(128, 1024, 2048))
tokens, page_table, prompt_lens, start_pos = _inputs(prompt_length=4129)
prepared = runtime.prepare(
tokens=tokens,
page_table=page_table,
prompt_lens=prompt_lens,
start_pos=start_pos,
)[0]
chunk = prepared.request.chunks[-1]
assert chunk.chunk_start_idx == 4096
assert torch.all(prepared.request.page_table >= 0)
assert torch.any(chunk.chunk_page_table == -1)
host_calls = []
monkeypatch.setattr(
runtime.inputs,
"prepare_host_inputs",
lambda token_slice, full_table, **kwargs: host_calls.append((token_slice, full_table, kwargs))
or PrefillHostInputs("host-token", "host-position", "host-page", "host-chunk", "host-start"),
)
copies = []
monkeypatch.setattr(
prefill_inputs_module,
"copy_into_device_tensors",
lambda host, device=None, mesh_device=None: copies.append((host, device)) or device,
)
runtime.config.model.prepare_prefill_rot_mats = lambda positions: ("new-cos", "new-sin")
rotary_copies = []
monkeypatch.setattr(
postprocess_module.ttnn,
"copy",
lambda *, input_a, input_b: rotary_copies.append((input_a, input_b)),
)
released = []
monkeypatch.setattr(runtime.inputs, "_release_transient", lambda value: released.append(value) or [])
persistent = PrefillHiddenPersistentInputs(
device_inputs=PrefillDeviceInputs("tokens", "cos", "sin", "page", "chunk-page", "positions", "start")
)
workspace = PrefillReplayState(
position_inputs=PrefillPositionInputs("slice-start", "slice-end", "row"),
kpt=None,
position_signature=32,
)
runtime.refresh_trace(prepared, persistent, workspace, chunk)
assert host_calls[0][2]["start_pos"] == 4096
assert host_calls[0][2]["chunk_start_idx"] == 4096
assert host_calls[0][2]["chunk_page_table"] is chunk.chunk_page_table
assert copies == [
(
("host-token", "host-position", "host-page", "host-chunk", "host-start"),
("tokens", "positions", "page", "chunk-page", "start"),
)
]
assert rotary_copies == [("new-cos", "cos"), ("new-sin", "sin")]
assert released == [("new-cos", "new-sin")]
def test_q128_single_topk_tile_is_program_material_but_not_trace_material():
runtime = _runtime()
sampling = SamplingParams(temperature=1.0, top_k=32, top_p=0.08)
def prepare(prompt_length, sampling_params):
tokens, page_table, prompt_lens, start_pos = _inputs(prompt_length=prompt_length)
return runtime.prepare(
tokens=tokens,
page_table=page_table,
prompt_lens=prompt_lens,
start_pos=start_pos,
sampling_params=sampling_params,
)[0]
first_tile = prepare(32, sampling)
third_tile = prepare(96, sampling)
first_program = first_tile.program_signatures[0]
third_program = third_tile.program_signatures[0]
assert first_program.last_token_tile_start == 0
assert third_program.last_token_tile_start == 64
assert first_program != third_program
assert first_tile.trace_signature == third_tile.trace_signature
assert prepare(32, None).program_signatures[0].last_token_tile_start is None
def test_hidden_capture_schema_is_sampling_independent_with_distinct_trace_and_alias_identities():
runtime = _runtime(allow_force_argmax=True)
tokens, page_table, prompt_lens, start_pos = _inputs(prompt_length=80)
def prepare(sampling_params):
return runtime.prepare(
tokens=tokens,
page_table=page_table,
prompt_lens=prompt_lens,
start_pos=start_pos,
sampling_params=sampling_params,
)[0]
logits = prepare(None)
argmax = prepare(SamplingParams(temperature=0.0, top_k=1, top_p=1.0))
topk = prepare(SamplingParams(temperature=1.0, top_k=32, top_p=0.08))
plans = [runtime.capture_plan(prepared) for prepared in (logits, argmax, topk)]
assert {prepared.trace_signature.sampling_path for prepared in (logits, argmax, topk)} == {
"logits",
"argmax",
"topk",
}
assert len({prepared.trace_signature for prepared in (logits, argmax, topk)}) == 3
assert len({plan.schema_fingerprint for plan in plans}) == 1
assert len({plan.workspace_fingerprint for plan in plans}) == 3
assert all("topk" not in repr(plan.schema_fingerprint) for plan in plans)
assert all("argmax" not in repr(plan.schema_fingerprint) for plan in plans)
def test_finish_trace_reports_nested_persistent_logprob_and_intermediate_ownership(monkeypatch):
runtime = _runtime()
tokens, page_table, prompt_lens, start_pos = _inputs(prompt_length=80)
prepared = runtime.prepare(
tokens=tokens,
page_table=page_table,
prompt_lens=prompt_lens,
start_pos=start_pos,
sampling_params=SamplingParams(temperature=1.0, top_k=32, top_p=0.08),
)[0]
hidden = object()
sampled_output = object()
sampled_output_alias = object()
logprob_output = object()
logits = object()
selected = object()
workspace = PrefillReplayState(
position_inputs=PrefillPositionInputs("start", "end", "row"),
kpt=("k", "p", "t"),
sampled_output=sampled_output,
)
def finish(*args, owned, **kwargs):
owned.extend((hidden, logits, selected, (sampled_output_alias, logprob_output)))
return sampled_output_alias, logprob_output
monkeypatch.setattr(runtime.postprocessor, "finish_regular_prefill", finish)
result = runtime.finish_trace(prepared, hidden, workspace)
assert result.value == (sampled_output_alias, logprob_output)
assert result.owned == (logits, selected, (logprob_output,))
assert result.replay_ownership.trace_owned_hidden_output is hidden
assert result.replay_ownership.nested_persistent_output is sampled_output
assert result.replay_ownership.new_logprob_output is logprob_output
assert result.replay_ownership.replay_local_intermediates == (logits, selected)
def test_cached_chunk_trace_logits_row_pick_the_logical_last_token_for_assembly(monkeypatch):
runtime = _runtime(trace_lengths=(128, 1024, 2048))
tokens, page_table, prompt_lens, start_pos = _inputs(prompt_length=160, cached_tokens=32)
prepared = runtime.prepare(
tokens=tokens,
page_table=page_table,
prompt_lens=prompt_lens,
start_pos=start_pos,
)[0]
assert prepared.request.uses_chunked_prefill
assert prepared.request.last_token_indices == (159,)
assert prepared.request.cached_tokens == (32,)
seen = []
def postprocess(hidden, last_token, *, last_token_slice, last_token_index):
seen.append((hidden, last_token, last_token_slice, last_token_index))
if last_token_index is not None:
# Device-side row pick: the single output row carries the logical last token (127).
return torch.full((1, 1, 1, 8), float(last_token))
return torch.arange(32, dtype=torch.float32).reshape(1, 1, 32, 1).expand(-1, -1, -1, 8)
runtime.config.model.post_process_prefill_output = postprocess
monkeypatch.setattr(postprocess_module.ttnn, "untilize", lambda logits, **kwargs: logits)
workspace = PrefillReplayState(
position_inputs=PrefillPositionInputs("slice-start", "slice-end", "row-index"),
kpt=None,
sampled_output=None,
)
result = runtime.finish_trace(prepared, "hidden", workspace)
output = runtime.assemble([(prepared, result)], batch_size=1)
assert seen == [("hidden", 127, ("slice-start", "slice-end"), "row-index")]
assert torch.equal(output[0, 0], torch.full((runtime.config.model.vocab_size,), 127.0))
def test_static_q128_single_topk_uses_tile_output_and_exact_host_row(monkeypatch):
runtime = _runtime()
tokens, page_table, prompt_lens, start_pos = _inputs(prompt_length=80)
sampling = SamplingParams(temperature=0.0, top_k=1, top_p=1.0)
prepared = runtime.prepare(
tokens=tokens,
page_table=page_table,
prompt_lens=prompt_lens,
empty_slots=[0],
start_pos=start_pos,
sampling_params=sampling,
)[0]
seen = []
def post_process_prefill_output(
hidden_states,
last_token_idx,
last_token_slice=None,
last_token_index=None,
):
seen.append(((hidden_states, last_token_idx), {}))
return hidden_states
runtime.config.model.post_process_prefill_output = post_process_prefill_output
sliced = []
def slice_logits(value, start, end):
sliced.append((start, end))
return value[:, :, start[2] : end[2], :]
sampled = []
monkeypatch.setattr(postprocess_module.ttnn, "slice", slice_logits)
monkeypatch.setattr(
runtime.postprocessor,
"sample_device",
lambda logits, kpt, sampling, sampled_output=None, **kwargs: sampled.append((logits, sampled_output))
or sampled_output,
)
assert runtime.postprocessor.sampling_output_rows(prepared) == 32
assert (
runtime.postprocessor.finish_regular_prefill(
prepared,
torch.zeros(1, 1, 128, runtime.config.model.vocab_size),
"kpt",
PrefillPositionInputs("dynamic-start", "dynamic-end", "dynamic-row"),
sampled_output="sampled",
)
== "sampled"
)
assert len(seen) == 1
assert seen[0][0][0].shape == (1, 1, 128, runtime.config.model.vocab_size)
assert seen[0][0][1] == 79
assert seen[0][1] == {}
assert sliced == [((0, 0, 0, 0), (1, 1, 32, runtime.config.model.vocab_size))]
assert len(sampled) == 1
assert sampled[0][0].shape == (1, 1, 32, runtime.config.model.vocab_size)
assert sampled[0][1] == "sampled"
host_tokens = torch.zeros(1, 1, 32, 1, dtype=torch.int64)
host_tokens[0, 0, 15, 0] = 123
host_log_probs = torch.arange(32, dtype=torch.float32).reshape(1, 1, 1, 32)
released = []
monkeypatch.setattr(runtime, "_release_or_retain_transient", lambda value: released.append(value) or [])
output, log_probs = runtime.assemble(
[(prepared, InvocationResult((host_tokens, host_log_probs), "owned"))],
batch_size=1,
sampling_params=sampling,
)
assert output.tolist() == [123]
assert log_probs.item() == 15.0
assert released == ["owned"]
def test_static_q128_output_sizing_does_not_change_chunked_or_non_q128_paths():
sampling = SamplingParams(temperature=0.0, top_k=1, top_p=1.0)
def prepare(runtime, prompt_length, cached_tokens=0):
tokens, page_table, prompt_lens, start_pos = _inputs(
prompt_length=prompt_length,
cached_tokens=cached_tokens,
)
return runtime.prepare(
tokens=tokens,
page_table=page_table,
prompt_lens=prompt_lens,
empty_slots=[0],
start_pos=start_pos,
sampling_params=sampling,
)[0]
runtime = _runtime(sampling_batch_size=32)
static = prepare(runtime, 80)
assert runtime.postprocessor.sampling_output_rows(static) == 32
runtime = _runtime(sampling_batch_size=16)
static = prepare(runtime, 80)
assert runtime.postprocessor.sampling_output_rows(static) == 16
runtime = _runtime(sampling_batch_size=1)
cached = prepare(runtime, 160, cached_tokens=32)
non_q128 = prepare(runtime, 129)
assert runtime.postprocessor.sampling_output_rows(cached) == 1
assert runtime.postprocessor.sampling_output_rows(non_q128) == 1
def test_prefill_sampling_logits_are_fit_to_runtime_sampling_rows(monkeypatch):
runtime = _runtime()
too_many = torch.ones(1, 1, 128, runtime.config.model.vocab_size)
too_few = torch.ones(1, 1, 1, runtime.config.model.vocab_size)
exact = torch.ones(1, 1, 32, runtime.config.model.vocab_size)
sliced = []
padded = []
def slice_logits(value, start, end):
sliced.append((start, end))
return value[:, :, start[2] : end[2], :]
def pad_logits(tensor, padding, **kwargs):
fill = kwargs["value"]
padded.append((padding, fill))
pad_rows = padding[2][1]
return torch.cat(
[
tensor,
torch.full((1, 1, pad_rows, tensor.shape[3]), fill, dtype=tensor.dtype),
],
dim=2,
)
monkeypatch.setattr(postprocess_module.ttnn, "slice", slice_logits)
monkeypatch.setattr(postprocess_module.ttnn, "pad", pad_logits)
assert fit_prefill_sampling_logits(exact, 32) is exact
assert fit_prefill_sampling_logits(too_many, 32).shape[2] == 32
assert sliced == [((0, 0, 0, 0), (1, 1, 32, runtime.config.model.vocab_size))]
assert fit_prefill_sampling_logits(too_few, 32).shape[2] == 32
assert padded == [([(0, 0), (0, 0), (0, 31), (0, 0)], 0.0)]
@pytest.mark.parametrize(
("allow_force_argmax", "sampling_params", "expected_path"),
[
(True, SamplingParams(temperature=0.0, top_k=32, top_p=0.08), "argmax"),
(False, SamplingParams(temperature=0.0, top_k=32, top_p=0.08), "topk"),
(True, SamplingParams(temperature=1.0, top_k=32, top_p=0.08), "topk"),
],
)
def test_prepare_selects_single_prefill_sampling_path(allow_force_argmax, sampling_params, expected_path):
runtime = _runtime(allow_force_argmax=allow_force_argmax)
tokens, page_table, prompt_lens, start_pos = _inputs(prompt_length=80)
prepared = runtime.prepare(
tokens=tokens,
page_table=page_table,
prompt_lens=prompt_lens,
empty_slots=[0],
start_pos=start_pos,
sampling_params=sampling_params,
)[0]
assert prepared.sampling_path == expected_path
assert prepared.program_signatures[0].sampling_path == expected_path
def test_prepare_classifies_once_and_invoke_uses_only_sequence_runner(monkeypatch):
runtime = _runtime()
tokens, page_table, prompt_lens, start_pos = _inputs(prompt_length=160, cached_tokens=32)
prepared = runtime.prepare(
tokens=tokens,
page_table=page_table,
prompt_lens=prompt_lens,
empty_slots=[0],
start_pos=start_pos,
)[0]
seen = []
expected = InvocationResult("value", "owned")
def run_sequence(received, *, count_tokens=True):
seen.append((received, count_tokens))
return expected
monkeypatch.setattr(runtime.sequence_runner, "run", run_sequence)
assert runtime.invoke(prepared) is expected
assert seen == [(prepared, True)]
assert not hasattr(runtime, "_run_regular_prefill")
assert not hasattr(runtime, "_run_chunked_prefill")
def test_cached_one_chunk_uses_chunk_model_contract(monkeypatch):
runtime = _runtime()
request = _plan(prompt_length=160, cached_tokens=32)[0]
prepared = SimpleNamespace(request=request, sampling_params=None, sampling_path="logits")
chunk = request.chunks[0]
device_inputs = PrefillDeviceInputs("tokens", "cos", "sin", "page", "chunk-page", "pos", "chunk-start")
position_inputs = PrefillPositionInputs("slice-start", "slice-end", "row")
seen = []
def prefill_forward(
x_embed,
rot_mats,
user_id=0,
page_table=None,
chunk_page_table=None,
chunk_start_idx=None,
get_last_token=-1,
batch_size=1,
chunk_start_idx_tensor=None,
last_token_slice=None,
last_token_index=None,
):
seen.append(
(
(x_embed, rot_mats),
{
"user_id": user_id,
"page_table": page_table,
"chunk_page_table": chunk_page_table,
"chunk_start_idx": chunk_start_idx,
"get_last_token": get_last_token,
"chunk_start_idx_tensor": chunk_start_idx_tensor,
"last_token_slice": last_token_slice,
"last_token_index": last_token_index,
},
)
)
return "output"
def fail_regular_model_body(request, device_inputs):
pytest.fail("regular model body used")
runtime.config.model.prefill_forward = prefill_forward
monkeypatch.setattr(runtime, "_run_hidden_body", fail_regular_model_body)
assert runtime.sequence_runner._execute_step(prepared, chunk, device_inputs, position_inputs) == "output"
assert seen == [
(
("tokens", ["cos", "sin"]),
{
"user_id": 0,
"page_table": "page",
"chunk_page_table": "chunk-page",
"chunk_start_idx": 32,
"get_last_token": -1,
"chunk_start_idx_tensor": "chunk-start",
"last_token_slice": ("slice-start", "slice-end"),
"last_token_index": None,
},
)
]
def test_regular_batched_step_and_finalization_preserve_exact_model_contract(monkeypatch):
runtime = _runtime()
request = _plan(prompt_length=80, slots=(0, 1, 2))[0]
prepared = SimpleNamespace(request=request, sampling_params=None, sampling_path="logits")
chunk = request.chunks[0]
device_inputs = PrefillDeviceInputs("tokens", "cos", "sin", "page", None, "pos", None)
position_inputs = PrefillPositionInputs("slice-start", "slice-end", "row")
calls = []
runtime.config.model.embed_prefill = lambda tokens: calls.append(("embed", tokens)) or "embedded"
def prefill_forward(
x_embed,
rot_mats,
user_id=0,
page_table=None,
chunk_page_table=None,
chunk_start_idx=None,
get_last_token=-1,
batch_size=1,
chunk_start_idx_tensor=None,
last_token_slice=None,
last_token_index=None,
):
calls.append(
(
"forward",
(x_embed, rot_mats),
{
"user_id": user_id,
"page_table": page_table,
"chunk_page_table": chunk_page_table,
"get_last_token": get_last_token,
"batch_size": batch_size,
"chunk_start_idx_tensor": chunk_start_idx_tensor,
},
)
)
return "hidden"
def post_process_batched_prefill_output(
hidden_states,
last_token_idx_list,
padded_batch,
prefill_seq_len,
last_token_slice=None,
last_token_index=None,
):
calls.append(
(
"postprocess",
(hidden_states, last_token_idx_list, padded_batch, prefill_seq_len),
{
"last_token_slice": last_token_slice,
"last_token_index": last_token_index,
},
)
)
return "logits"
runtime.config.model.prefill_forward = prefill_forward
runtime.config.model.post_process_batched_prefill_output = post_process_batched_prefill_output
# ttnn.untilize is overloaded; this test intentionally records backend options.
monkeypatch.setattr(
postprocess_module.ttnn,
"untilize",
lambda value, **kwargs: calls.append(("untilize", value, kwargs)) or "output",
)
hidden = runtime.sequence_runner._execute_step(prepared, chunk, device_inputs, position_inputs)
output = runtime.postprocessor.finish_prefill_sequence(
prepared,
hidden,
None,
position_inputs,
sampled_output=None,
owned=[],
)
assert output == "output"
assert calls == [
("embed", "tokens"),
(
"forward",
("embedded", ["cos", "sin"]),
{
"user_id": [0, 1, 2],
"page_table": "page",
"chunk_page_table": None,
"get_last_token": -1,
"batch_size": 4,
"chunk_start_idx_tensor": None,
},
),
(
"postprocess",
("hidden", [79, 79, 79, 0], 4, 128),
{
"last_token_slice": None,
"last_token_index": None,
},
),
("untilize", "logits", {"use_multicore": True}),
]
def test_partial_batched_group_eager_executes_only_real_kv_users_at_padded_fold_size():
runtime = _runtime()
request = _plan(prompt_length=80, slots=(7, 3, 11))[0]
seen = []
def prefill_forward(*args, **kwargs):
seen.append(kwargs)
return "hidden"
runtime.config.model.prefill_forward = prefill_forward
device_inputs = PrefillDeviceInputs("tokens", "cos", "sin", "page", None, "positions", None)
assert runtime._run_hidden_body(request, device_inputs) == "hidden"
assert len(request.source_rows) == 3
assert request.padded_batch_size == 4
assert seen[0]["user_id"] == [0, 1, 2]
assert seen[0]["batch_size"] == 4
def test_batched_postprocess_uses_per_row_last_token_indices_for_mixed_exact_lengths(monkeypatch):
runtime = _runtime()
tokens = torch.arange(2 * 128, dtype=torch.long).reshape(2, 128)
page_table = torch.arange(2 * 256, dtype=torch.int32).reshape(2, 256)
prepared = runtime.prepare(
tokens=tokens,
page_table=page_table,
prompt_lens=torch.tensor([87, 115]),
start_pos=torch.zeros(2, dtype=torch.long),
empty_slots=(7, 3),
)[0]
seen = []
def post_process_batched_prefill_output(
hidden,
last_token_indices,
padded_batch_size,
padded_sequence_length,
last_token_slice=None,
last_token_index=None,
):
seen.append(
(
list(last_token_indices),
padded_batch_size,
padded_sequence_length,
last_token_slice,
last_token_index,
)
)
return "logits"
runtime.config.model.post_process_batched_prefill_output = post_process_batched_prefill_output
monkeypatch.setattr(postprocess_module.ttnn, "untilize", lambda logits, **kwargs: "output")
assert (
runtime.postprocessor.finish_regular_prefill(
prepared,
"hidden",
None,
PrefillPositionInputs("shared-start", "shared-end", "shared-row"),
)
== "output"
)
assert seen == [([86, 114], 2, 128, None, None)]
def test_partial_batched_group_shares_padded_trace_identity_with_full_group():
runtime = _runtime()
tokens, page_table, prompt_lens, start_pos = _inputs(prompt_length=80, rows=3)
prepared = runtime.prepare(
tokens=tokens,
page_table=page_table,
prompt_lens=prompt_lens,
start_pos=start_pos,
empty_slots=(7, 3, 11),
)
assert [len(item.request.source_rows) for item in prepared] == [3]
partial = prepared[0]
assert partial.request.padded_batch_size == 4
assert partial.trace_signature is not None
tokens, page_table, prompt_lens, start_pos = _inputs(prompt_length=80, rows=4)
full = runtime.prepare(
tokens=tokens,
page_table=page_table,
prompt_lens=prompt_lens,
start_pos=start_pos,
empty_slots=(7, 3, 11, 5),
)[0]
assert full.trace_signature == partial.trace_signature
assert full.program_signatures == partial.program_signatures
@pytest.mark.parametrize(
("sampling_path", "sampling_params", "expected_tail"),
[
("logits", None, ["postprocess", "untilize", "slice"]),
(
"argmax",
SamplingParams(temperature=0.0, top_k=1, top_p=1.0),
["postprocess", "pad", "sample"],
),
],
)
def test_regular_logits_and_argmax_preserve_operation_order(
monkeypatch,
sampling_path,
sampling_params,
expected_tail,
):
runtime = _runtime()
request = _plan(prompt_length=129)[0]
prepared = SimpleNamespace(
request=request,
sampling_params=sampling_params,
sampling_path=sampling_path,
)
events = []
monkeypatch.setattr(
runtime.inputs,
"stage_step",
lambda prepared, chunk, final_relative_last: events.append("stage")
or ("device", PrefillPositionInputs("start", "end", "row")),
)
monkeypatch.setattr(
runtime.postprocessor,
"make_device_kpt",
lambda sampling_params, batch_size, force_topk: events.append("kpt") or None,
)
monkeypatch.setattr(
runtime.sequence_runner,
"_execute_step",
lambda prepared, chunk, device_inputs, position_inputs: events.append("execute") or "hidden",
)
def post_process_prefill_output(
hidden_states,
last_token_idx,
last_token_slice=None,
last_token_index=None,
):
events.append("postprocess")
return "logits"
runtime.config.model.post_process_prefill_output = post_process_prefill_output
monkeypatch.setattr(
postprocess_module,
"fit_prefill_sampling_logits",
lambda logits, target_batch: events.append("pad") or "padded",
)
monkeypatch.setattr(
runtime.postprocessor,
"sample_device",
lambda logits, kpt, sampling, sampled_output=None, **kwargs: events.append("sample") or "sampled",
)
# ttnn.untilize is overloaded; this test only checks operation order.
monkeypatch.setattr(
postprocess_module.ttnn,
"untilize",
lambda *args, **kwargs: events.append("untilize") or torch.zeros(1, 1, 32, 64),
)
monkeypatch.setattr(
postprocess_module.ttnn,
"slice",
lambda *args, **kwargs: events.append("slice") or torch.zeros(1, 1, 1, 64),
)
monkeypatch.setattr(
runtime.postprocessor,
"make_sampling_output",
lambda batch_size: pytest.fail("output preallocated"),
)
runtime.sequence_runner.run(prepared)
assert events == ["stage", "kpt", "execute", *expected_tail]
def test_cached_one_chunk_stages_planned_chunk_metadata(monkeypatch):
runtime = _runtime()
request = _plan(prompt_length=160, cached_tokens=32)[0]
prepared = SimpleNamespace(request=request)
chunk = request.chunks[0]
seen = []
def prepare_inputs_host(
tokens,
page_table,
*,
start_pos=0,
chunk_page_table=None,
chunk_start_idx=None,
last_token_idx=None,
):
seen.append(
(
tokens,
page_table,
{
"start_pos": start_pos,
"chunk_page_table": chunk_page_table,
"chunk_start_idx": chunk_start_idx,
"last_token_idx": last_token_idx,
},
)
)
return "host"
monkeypatch.setattr(runtime.inputs, "prepare_host_inputs", prepare_inputs_host)
monkeypatch.setattr(runtime.inputs, "stage_device_inputs", lambda host_inputs: "device")
monkeypatch.setattr(
runtime.inputs,
"prepare_position_inputs_host",
lambda relative_last, sequence_length: PrefillPositionInputs(relative_last, sequence_length, "row"),
)
monkeypatch.setattr(
prefill_inputs_module,
"allocate_device_tensors",
lambda host_tensors, device_tensors=None, mesh_device=None: host_tensors,
)
assert runtime.inputs.stage_step(request, chunk, 127) == (
"device",
PrefillPositionInputs(127, 128, "row"),
)
assert torch.equal(seen[0][0], request.tokens[:, chunk.token_slice])
assert seen[0][1] is request.page_table
assert seen[0][2] == {
"start_pos": 32,
"chunk_page_table": chunk.chunk_page_table,
"chunk_start_idx": 32,
"last_token_idx": 159,
}
def test_chunk_sequence_allocates_kpt_before_steps_and_reuses_final_position(monkeypatch):
runtime = _runtime()
request = _plan(prompt_length=4097)[0]
prepared = SimpleNamespace(request=request, sampling_params=None, sampling_path="logits")
events = []
relative_positions = []
step_outputs = iter(object() for _ in request.chunks)
monkeypatch.setattr(
runtime.postprocessor,
"make_device_kpt",
lambda sampling_params, batch_size, force_topk: events.append("kpt") or None,
)
def stage(_prepared, chunk, relative_last):
events.append(("stage", chunk.chunk_start_idx))
relative_positions.append(relative_last)
return object(), object()
monkeypatch.setattr(runtime.inputs, "stage_step", stage)
monkeypatch.setattr(
runtime.sequence_runner,
"_execute_step",
lambda prepared, chunk, device_inputs, position_inputs: events.append(("execute", chunk.chunk_start_idx))
or next(step_outputs),
)
monkeypatch.setattr(runtime, "_release_or_retain_transient", lambda value: events.append("release") or [])
monkeypatch.setattr(
runtime.postprocessor,
"finish_prefill_sequence",
lambda prepared, final_step_output, kpt, position_inputs, *, sampled_output, owned, count_tokens=True: events.append(
"finish"
)
or final_step_output,
)
runtime.sequence_runner.run(prepared)
assert relative_positions == [0] * len(request.chunks)
assert events[0] == "kpt"
assert events[1:] == [
("stage", 0),
("execute", 0),
"release",
("stage", 2048),
("execute", 2048),
"release",
("stage", 4096),
("execute", 4096),
"finish",
]
@pytest.mark.parametrize(
("prompt_length", "cached_tokens", "sampling_path", "expect_preallocated"),
[
(80, 0, "topk", True),
(80, 0, "argmax", False),
(160, 32, "topk", False),
],
)
def test_sequence_preserves_sampling_output_preallocation_matrix(
monkeypatch,
prompt_length,
cached_tokens,
sampling_path,
expect_preallocated,
):
runtime = _runtime()
request = _plan(prompt_length=prompt_length, cached_tokens=cached_tokens)[0]
prepared = SimpleNamespace(
request=request,
sampling_params=SamplingParams(temperature=0.0, top_k=1, top_p=1.0),
sampling_path=sampling_path,
)
allocated = []
monkeypatch.setattr(runtime.postprocessor, "make_device_kpt", lambda sampling_params, batch_size, force_topk: None)
monkeypatch.setattr(
runtime.inputs,
"stage_step",
lambda prepared, chunk, final_relative_last: (object(), object()),
)
monkeypatch.setattr(
runtime.sequence_runner,
"_execute_step",
lambda prepared, chunk, device_inputs, position_inputs: object(),
)
monkeypatch.setattr(
runtime.postprocessor,
"make_sampling_output",
lambda rows: allocated.append(rows) or object(),
)
monkeypatch.setattr(
runtime.postprocessor,
"finish_prefill_sequence",
lambda prepared, final_step_output, kpt, position_inputs, *, sampled_output, owned, count_tokens=True: final_step_output,
)
runtime.sequence_runner.run(prepared)
assert bool(allocated) is expect_preallocated
def test_prefill_sequence_consumes_planned_chunks_and_releases_intermediate(monkeypatch):
runtime = _runtime()
request = _plan(prompt_length=4097)[0]
prepared = SimpleNamespace(request=request, sampling_params=None, sampling_path="logits")
device_inputs = [object() for _ in request.chunks]
position_inputs = [object() for _ in request.chunks]
step_outputs = [object() for _ in request.chunks]
final_output = object()
released = []
staged = iter(zip(device_inputs, position_inputs))
executed = iter(step_outputs)
monkeypatch.setattr(runtime.postprocessor, "make_device_kpt", lambda sampling_params, batch_size, force_topk: None)
monkeypatch.setattr(
runtime.inputs,
"stage_step",
lambda prepared, chunk, final_relative_last: next(staged),
)
monkeypatch.setattr(
runtime.sequence_runner,
"_execute_step",
lambda prepared, chunk, device_inputs, position_inputs: next(executed),
)
# ttnn.untilize is overloaded; this test only verifies its input and output.
monkeypatch.setattr(
postprocess_module.ttnn,
"untilize",
lambda value, **kwargs: final_output if value is step_outputs[-1] else pytest.fail("wrong final output"),
)
monkeypatch.setattr(runtime, "_release_or_retain_transient", lambda value: released.append(value) or [])
result = runtime.sequence_runner.run(prepared)
assert released == step_outputs[:-1]
assert result.value is final_output
assert result.owned == (
device_inputs[0],
position_inputs[0],
device_inputs[1],
position_inputs[1],
device_inputs[2],
position_inputs[2],
step_outputs[-1],
final_output,
)
def test_sequence_retains_raw_padded_and_sampled_outputs(monkeypatch):
runtime = _runtime()
regular_request = _plan(prompt_length=80)[0]
regular = SimpleNamespace(
request=regular_request,
sampling_params=SamplingParams(temperature=1.0, top_k=32, top_p=0.08),
sampling_path="topk",
)
raw_regular, padded_regular, sampled_regular = object(), object(), object()
regular_owned = []
def post_process_prefill_output(
hidden_states,
last_token_idx,
last_token_slice=None,
last_token_index=None,
):
return raw_regular
runtime.config.model.post_process_prefill_output = post_process_prefill_output
monkeypatch.setattr(
postprocess_module,
"fit_prefill_sampling_logits",
lambda logits, target_batch: padded_regular,
)
monkeypatch.setattr(
runtime.postprocessor,
"sample_device",
lambda logits, kpt, sampling, sampled_output=None, **kwargs: sampled_regular,
)
assert (
runtime.postprocessor.finish_regular_prefill(
regular,
"hidden",
"kpt",
PrefillPositionInputs("start", "end", "row"),
owned=regular_owned,
)
is sampled_regular
)
assert regular_owned == [raw_regular, padded_regular, sampled_regular]
chunked_request = _plan(prompt_length=160, cached_tokens=32)[0]
chunked = SimpleNamespace(
request=chunked_request,
sampling_params=regular.sampling_params,
sampling_path="topk",
)
raw_chunked, padded_chunked, sampled_chunked = object(), object(), object()
chunked_owned = [raw_chunked]
monkeypatch.setattr(
postprocess_module,
"fit_prefill_sampling_logits",
lambda logits, target_batch: padded_chunked,
)
monkeypatch.setattr(
runtime.postprocessor,
"sample_device",
lambda logits, kpt, sampling, sampled_output=None, **kwargs: sampled_chunked,
)
assert (
runtime.postprocessor.finish_prefill_sequence(
chunked,
raw_chunked,
"kpt",
PrefillPositionInputs("start", "end", "row"),
sampled_output=None,
owned=chunked_owned,
)
is sampled_chunked
)
assert chunked_owned == [raw_chunked, padded_chunked, sampled_chunked]
alias_owned = []
runtime.config.model.post_process_prefill_output = post_process_prefill_output
monkeypatch.setattr(
postprocess_module,
"fit_prefill_sampling_logits",
lambda logits, target_batch: raw_regular,
)
monkeypatch.setattr(
runtime.postprocessor,
"sample_device",
lambda logits, kpt, sampling, sampled_output=None, **kwargs: raw_regular,
)
assert (
runtime.postprocessor.finish_regular_prefill(
regular,
"hidden",
"kpt",
PrefillPositionInputs("start", "end", "row"),
owned=alias_owned,
)
is raw_regular
)
assert alias_owned == [raw_regular]
def test_sampling_output_failure_releases_all_sequence_resources(monkeypatch, expect_error):
runtime = _runtime()
request = _plan(prompt_length=80)[0]
prepared = SimpleNamespace(
request=request,
sampling_params=SamplingParams(temperature=1.0, top_k=32, top_p=0.08),
sampling_path="topk",
)
device_inputs, position_inputs, kpt, hidden = object(), object(), object(), object()
released = []
monkeypatch.setattr(
runtime.inputs,
"stage_step",
lambda prepared, chunk, final_relative_last: (device_inputs, position_inputs),
)
monkeypatch.setattr(
runtime.postprocessor,
"make_device_kpt",
lambda sampling_params, batch_size, force_topk: kpt,
)
monkeypatch.setattr(
runtime.sequence_runner,
"_execute_step",
lambda prepared, chunk, device_inputs, position_inputs: hidden,
)
monkeypatch.setattr(
runtime.postprocessor,
"make_sampling_output",
lambda batch_size: (_ for _ in ()).throw(RuntimeError("allocation failed")),
)
monkeypatch.setattr(runtime, "_release_or_retain_transient", lambda values: released.append(values) or [])
with expect_error(RuntimeError, "allocation failed"):
runtime.sequence_runner.run(prepared)
assert released == [(device_inputs, position_inputs, kpt, hidden)]
def test_step_execution_failure_releases_staged_sequence_resources(monkeypatch, expect_error):
runtime = _runtime()
request = _plan(prompt_length=80)[0]
prepared = SimpleNamespace(request=request, sampling_params=None, sampling_path="logits")
device_inputs, position_inputs = object(), object()
released = []
monkeypatch.setattr(
runtime.inputs,
"stage_step",
lambda prepared, chunk, final_relative_last: (device_inputs, position_inputs),
)
monkeypatch.setattr(runtime.postprocessor, "make_device_kpt", lambda sampling_params, batch_size, force_topk: None)
def fail_step_execution(prepared, chunk, device_inputs, position_inputs):
raise RuntimeError("execution failed")
monkeypatch.setattr(runtime.sequence_runner, "_execute_step", fail_step_execution)
monkeypatch.setattr(runtime, "_release_or_retain_transient", lambda values: released.append(values) or [])
with expect_error(RuntimeError, "execution failed"):
runtime.sequence_runner.run(prepared)
assert released == [(device_inputs, position_inputs)]
def test_staging_failure_after_prior_chunk_releases_prior_sequence_resources(monkeypatch, expect_error):
runtime = _runtime()
request = _plan(prompt_length=4097)[0]
prepared = SimpleNamespace(request=request, sampling_params=None, sampling_path="logits")
device_inputs, position_inputs, intermediate = object(), object(), object()
stage_calls = 0
released = []
monkeypatch.setattr(runtime.postprocessor, "make_device_kpt", lambda sampling_params, batch_size, force_topk: None)
def stage(prepared, chunk, final_relative_last):
nonlocal stage_calls
stage_calls += 1
if stage_calls == 2:
raise RuntimeError("second staging failed")
return device_inputs, position_inputs
monkeypatch.setattr(runtime.inputs, "stage_step", stage)
monkeypatch.setattr(
runtime.sequence_runner,
"_execute_step",
lambda prepared, chunk, device_inputs, position_inputs: intermediate,
)
monkeypatch.setattr(runtime, "_release_or_retain_transient", lambda values: released.append(values) or [])
with expect_error(RuntimeError, "second staging failed"):
runtime.sequence_runner.run(prepared)
assert released == [intermediate, (device_inputs, position_inputs)]
@pytest.mark.parametrize("failure_point", ["postprocess", "pad", "sample", "untilize"])
def test_finalization_failure_releases_every_resource_acquired_before_failure(monkeypatch, failure_point, expect_error):
runtime = _runtime()
request = _plan(prompt_length=129)[0]
sampled = failure_point in ("pad", "sample")
prepared = SimpleNamespace(
request=request,
sampling_params=(SamplingParams(temperature=0.0, top_k=1, top_p=1.0) if sampled else None),
sampling_path="argmax" if sampled else "logits",
)
device_inputs, hidden, raw, padded = (object() for _ in range(4))
position_inputs = PrefillPositionInputs("start", "end", "row")
released = []
monkeypatch.setattr(
runtime.inputs,
"stage_step",
lambda prepared, chunk, final_relative_last: (device_inputs, position_inputs),
)
monkeypatch.setattr(runtime.postprocessor, "make_device_kpt", lambda sampling_params, batch_size, force_topk: None)
monkeypatch.setattr(
runtime.sequence_runner,
"_execute_step",
lambda prepared, chunk, device_inputs, position_inputs: hidden,
)
def postprocess(
hidden_states,
last_token_idx,
last_token_slice=None,
last_token_index=None,
):
if failure_point == "postprocess":
raise RuntimeError("postprocess failed")
return raw
def pad(logits, target_batch):
if failure_point == "pad":
raise RuntimeError("pad failed")
return padded
def sample(logits, kpt, sampling, sampled_output=None, **kwargs):
if failure_point == "sample":
raise RuntimeError("sample failed")
return object()
# ttnn.untilize is overloaded; the failure hook must accept its backend options.
def untilize(*args, **kwargs):
if failure_point == "untilize":
raise RuntimeError("untilize failed")
return object()
runtime.config.model.post_process_prefill_output = postprocess
monkeypatch.setattr(postprocess_module, "fit_prefill_sampling_logits", pad)
monkeypatch.setattr(runtime.postprocessor, "sample_device", sample)
monkeypatch.setattr(postprocess_module.ttnn, "untilize", untilize)
monkeypatch.setattr(runtime, "_release_or_retain_transient", lambda values: released.append(values) or [])
with expect_error(RuntimeError, f"{failure_point} failed"):
runtime.sequence_runner.run(prepared)
expected = [device_inputs, position_inputs, hidden]
if failure_point != "postprocess":
expected.append(raw)
if failure_point == "sample":
expected.append(padded)
assert released == [tuple(expected)]
def test_unified_path_preserves_primary_error_and_retries_cleanup_orphan(monkeypatch, expect_error):
runtime = _runtime()
request = _plan(prompt_length=80)[0]
prepared = SimpleNamespace(request=request, sampling_params=None, sampling_path="logits")
primary = RuntimeError("execution failed")
cleanup_failure = RuntimeError("device busy")
calls = []
monkeypatch.setattr(
runtime.inputs,
"stage_step",
lambda prepared, chunk, final_relative_last: ("device", "position"),
)
monkeypatch.setattr(runtime.postprocessor, "make_device_kpt", lambda sampling_params, batch_size, force_topk: None)
def fail_step_execution(prepared, chunk, device_inputs, position_inputs):
raise primary
monkeypatch.setattr(runtime.sequence_runner, "_execute_step", fail_step_execution)
def release(values, completed):
calls.append(values)
return [cleanup_failure] if len(calls) == 1 else []
monkeypatch.setattr(prefill_module, "best_effort_deallocate_owned_tensors", release)
monkeypatch.setattr(tensor_resources_module, "best_effort_deallocate_owned_tensors", release)
with expect_error(RuntimeError, "execution failed") as caught:
runtime.sequence_runner.run(prepared)
assert caught.value is primary
assert primary.cleanup_failures == (cleanup_failure,)
assert runtime.transient_orphan_count == 1
runtime.cleanup()
assert calls == [("device", "position"), ("device", "position")]
assert runtime.transient_orphan_count == 0
def test_intermediate_release_failure_is_not_reowned_by_sequence(monkeypatch, expect_error):
runtime = _runtime()
request = _plan(prompt_length=4097)[0]
prepared = SimpleNamespace(request=request, sampling_params=None, sampling_path="logits")
device_inputs, position_inputs, intermediate = object(), object(), object()
released = []
monkeypatch.setattr(runtime.postprocessor, "make_device_kpt", lambda sampling_params, batch_size, force_topk: None)
monkeypatch.setattr(
runtime.inputs,
"stage_step",
lambda prepared, chunk, final_relative_last: (device_inputs, position_inputs),
)
monkeypatch.setattr(
runtime.sequence_runner,
"_execute_step",
lambda prepared, chunk, device_inputs, position_inputs: intermediate,
)
def release(values):
released.append(values)
return [RuntimeError("release failed")] if values is intermediate else []
monkeypatch.setattr(runtime, "_release_or_retain_transient", release)
with expect_error(RuntimeError, "release failed"):
runtime.sequence_runner.run(prepared)
assert released == [intermediate, (device_inputs, position_inputs)]
def test_trace_capture_uses_hidden_body_without_eager_sequence(monkeypatch):
runtime = _runtime()
tokens, page_table, prompt_lens, start_pos = _inputs(prompt_length=80)
prepared = runtime.prepare(
tokens=tokens,
page_table=page_table,
prompt_lens=prompt_lens,
empty_slots=[0],
start_pos=start_pos,
)[0]
persistent = PrefillHiddenPersistentInputs(device_inputs="device-inputs")
seen = []
monkeypatch.setattr(
runtime,
"_run_hidden_body",
lambda request, device_inputs, **kwargs: seen.append((request, device_inputs, kwargs)) or "hidden",
)
monkeypatch.setattr(runtime.sequence_runner, "run", lambda prepared: pytest.fail("eager sequence used"))
assert runtime.capture_plan(prepared).capture(persistent) == "hidden"
assert seen == [(prepared.request, "device-inputs", {"fill_rows": prepared.request.padded_batch_size})]
@pytest.mark.parametrize("prompt_length", (80, 700), ids=("q128", "q1024"))
def test_opt_in_trace_capture_prime_uses_padded_body_and_releases_output(monkeypatch, prompt_length):
padded_length = 128 if prompt_length <= 128 else 1024
runtime = _runtime(trace_capture_prime_sequence_lengths=(padded_length,))
tokens, page_table, prompt_lens, start_pos = _inputs(prompt_length=prompt_length, rows=3)
prepared = runtime.prepare(
tokens=tokens,
page_table=page_table,
prompt_lens=prompt_lens,
empty_slots=[0, 1, 2],
start_pos=start_pos,
)[0]
persistent = PrefillHiddenPersistentInputs(device_inputs="device-inputs")
seen = []
released = []
monkeypatch.setattr(
runtime,
"_run_hidden_body",
lambda request, device_inputs, **kwargs: seen.append((request, device_inputs, kwargs)) or "hidden",
)
monkeypatch.setattr(runtime, "_release_or_retain_transient", lambda value: released.append(value) or [])
plan = runtime.capture_plan(prepared)
assert plan.prime is not None
assert plan.release_prime_output is not None
output = plan.prime(persistent)
assert output == "hidden"
assert plan.release_prime_output(output) == []
assert seen == [(prepared.request, "device-inputs", {"fill_rows": prepared.request.padded_batch_size})]
assert prepared.request.padded_batch_size == 4
assert released == ["hidden"]
def test_opt_in_trace_capture_prime_does_not_expand_single_request_warmup():
runtime = _runtime(trace_capture_prime_sequence_lengths=(128, 1024))
tokens, page_table, prompt_lens, start_pos = _inputs(prompt_length=80, rows=1)
prepared = runtime.prepare(
tokens=tokens,
page_table=page_table,
prompt_lens=prompt_lens,
empty_slots=[0],
start_pos=start_pos,
)[0]
assert prepared.request.kind == "single"
assert runtime.capture_plan(prepared).prime is None
@pytest.mark.parametrize(
("prompt_length", "active_rows", "padded_rows"),
((80, 30, 32), (700, 3, 4)),
ids=("q128-active30-padded32", "q1024-active3-padded4"),
)
def test_partial_wave_primes_every_padded_child_program_before_capture(
monkeypatch,
prompt_length,
active_rows,
padded_rows,
):
padded_length = 128 if prompt_length <= 128 else 1024
runtime = _runtime(
max_prefill_batch_size=32,
trace_capture_prime_sequence_lengths=(padded_length,),
)
tokens, page_table, prompt_lens, start_pos = _inputs(prompt_length=prompt_length, rows=active_rows)
prepared = runtime.prepare(
tokens=tokens,
page_table=page_table,
prompt_lens=prompt_lens,
empty_slots=list(range(active_rows)),
start_pos=start_pos,
)[0]
assert prepared.request.kind == "batched"
assert len(prepared.request.source_rows) == active_rows
assert prepared.request.padded_batch_size == padded_rows
operation_plan = runtime.capture_plan(prepared)
persistent = PrefillHiddenPersistentInputs(device_inputs="persistent-inputs")
child_programs = set()
events = []
capture_active = False
def run_hidden_body(request, device_inputs, *, fill_rows=None):
rows = len(request.source_rows) if fill_rows is None else fill_rows
required = {(request.padded_sequence_length, slot) for slot in range(rows)}
if capture_active and not required.issubset(child_programs):
raise RuntimeError("new child program during capture")
if not capture_active:
child_programs.update(required)
events.append(("body", rows, capture_active))
return "hidden-output"
monkeypatch.setattr(runtime, "_run_hidden_body", run_hidden_body)
runtime._run_hidden_body(prepared.request, "eager-inputs")
monkeypatch.setattr(ttnn, "synchronize_device", lambda mesh: events.append(("sync", mesh)))
def begin(mesh, cq_id):
nonlocal capture_active
capture_active = True
events.append(("begin",))
return 7
def end(mesh, trace_id, cq_id):
nonlocal capture_active
capture_active = False
events.append(("end",))
monkeypatch.setattr(ttnn, "begin_trace_capture", begin)
monkeypatch.setattr(ttnn, "end_trace_capture", end)
monkeypatch.setattr(ttnn, "release_trace", lambda mesh, trace_id: None)
monkeypatch.setattr(trace_compiler_module, "_trim_host_allocator", lambda: None)
compiler = ProgramCompiler("mesh", lambda: object())
program = compiler.compile(
PrefillProgramSignature("regular-batched", padded_rows, padded_length, 32, None, "logits"),
lambda context: torch.zeros(1),
)
trace = TraceCompiler(compiler)
trace.register_capture_plan(
TraceCapturePlan(
program.key,
operation_plan.signature,
"prefill",
lambda: persistent,
lambda value: operation_plan.capture(value.values),
prime=lambda value: operation_plan.prime(value.values),
release_prime_output=operation_plan.release_prime_output,
)
)
trace.capture_all()
assert [event for event in events if event[0] == "body"] == [
("body", active_rows, False),
("body", padded_rows, False),
("body", padded_rows, True),
]
def test_assemble_restores_source_rows_and_releases_each_owned_result(monkeypatch):
runtime = _runtime()
requests = _plan(prompt_length=80, slots=(7, 3), disable_batched_prefill=True)
prepared = [SimpleNamespace(request=request, sampling_params=None) for request in requests]
first = torch.zeros(1, 1, 32, runtime.config.model.vocab_size)
second = torch.zeros_like(first)
first[0, 0, 0, :] = 1
second[0, 0, 0, :] = 2
released = []
monkeypatch.setattr(runtime, "_release_or_retain_transient", lambda value: released.append(value) or [])
output = runtime.assemble(
[(prepared[0], InvocationResult(first, "owned-0")), (prepared[1], InvocationResult(second, "owned-1"))],
batch_size=2,
)
assert output.shape == (2, 1, runtime.config.model.vocab_size)
assert torch.equal(output[0, 0], torch.ones(runtime.config.model.vocab_size))
assert torch.equal(output[1, 0], torch.full((runtime.config.model.vocab_size,), 2.0))
assert released == ["owned-0", "owned-1"]
def test_single_logits_prefill_uses_runtime_row_pick_and_skips_host_row_slice(monkeypatch):
runtime = _runtime()
request = _plan(prompt_length=80)[0]
prepared = SimpleNamespace(request=request, sampling_params=None, sampling_path="logits")
seen = []
def post_process_prefill_output(
hidden_states,
last_token_idx,
last_token_slice=None,
last_token_index=None,
):
seen.append((hidden_states, last_token_idx, last_token_slice, last_token_index))
assert last_token_index is not None
return torch.ones(1, 1, 1, runtime.config.model.vocab_size)
runtime.config.model.post_process_prefill_output = post_process_prefill_output
monkeypatch.setattr(postprocess_module.ttnn, "untilize", lambda logits, **kwargs: logits)
sliced = []
def slice_output(value, start, end):
sliced.append((start, end))
return value[:, :, start[2] : end[2], :]
monkeypatch.setattr(postprocess_module.ttnn, "slice", slice_output)
positions = PrefillPositionInputs("slice-start", "slice-end", "row-index")
logits = runtime.postprocessor.finish_regular_prefill(prepared, "hidden", None, positions)
assert seen == [("hidden", 79, ("slice-start", "slice-end"), "row-index")]
assert sliced == []
output = runtime.assemble([(prepared, InvocationResult(logits, "owned"))], batch_size=1)
assert torch.equal(output, logits[:, 0])
def test_assemble_maps_batched_extract_rows_independently_of_physical_slots(monkeypatch):
runtime = _runtime()
request = _plan(prompt_length=80, slots=(7, 3))[0]
prepared = SimpleNamespace(request=request, sampling_params=None)
host = torch.zeros(1, 1, 32, runtime.config.model.vocab_size)
host[0, 0, 0, :] = 1
host[0, 0, 1, :] = 2
released = []
concatenated = []
original_concat = result_collector_module.concat_host_output
monkeypatch.setattr(
result_collector_module,
"concat_host_output",
lambda value, shape: concatenated.append((value, shape)) or original_concat(value, shape),
)
monkeypatch.setattr(runtime, "_release_or_retain_transient", lambda value: released.append(value) or [])
output = runtime.assemble(
[(prepared, InvocationResult(host, "owned"))],
batch_size=2,
)
assert torch.equal(output[0, 0], torch.ones(runtime.config.model.vocab_size))
assert torch.equal(output[1, 0], torch.full((runtime.config.model.vocab_size,), 2.0))
assert concatenated == [(host, runtime.config.cluster_shape)]
assert released == ["owned"]
def test_transient_cleanup_retries_failed_release(monkeypatch):
runtime = _runtime()
calls = []
def release(value, completed):
calls.append(value)
return [RuntimeError("busy")] if len(calls) == 1 else []
monkeypatch.setattr(prefill_module, "best_effort_deallocate_owned_tensors", release)
monkeypatch.setattr(tensor_resources_module, "best_effort_deallocate_owned_tensors", release)
failures = runtime._release_or_retain_transient("tensor")
assert failures and runtime.transient_orphan_count == 1
runtime.cleanup()
assert calls == ["tensor", "tensor"]
assert runtime.transient_orphan_count == 0
def test_zero_uncached_tokens_preserve_current_empty_plan_and_output_contract():
runtime = _runtime()
tokens, page_table, prompt_lens, start_pos = _inputs(prompt_length=32, cached_tokens=32)
assert (
runtime.prepare(
tokens=tokens,
page_table=page_table,
prompt_lens=prompt_lens,
empty_slots=[0],
start_pos=start_pos,
)
== ()
)
logits = runtime.assemble([], batch_size=1)
assert logits.shape == (1, 1, runtime.config.model.vocab_size)
sampled, log_probs = runtime.assemble(
[],
batch_size=1,
sampling_params=SamplingParams(temperature=0.0, top_k=1, top_p=1.0),
)
assert sampled.dtype == torch.int64
assert sampled.tolist() == [0]
assert log_probs is None
def test_prefill_runtime_config_resolves_frozen_static_capabilities(expect_error):
runtime = _runtime(allow_force_argmax=True, sampling_batch_size=16)
config = runtime.config
assert config.cluster_shape == (1, 1)
assert config.allow_force_argmax
assert config.sampling_batch_size == 16
assert not config.static_q128_topk_supported
assert config.supports_batched_prefill
assert not config.disable_batched_prefill
assert config.max_prefill_batch_size == 8
assert config.batched_prefill_batched_extract
with expect_error(FrozenInstanceError, "cannot assign to field"):
config.max_batch_size = 8
def test_prefill_runtime_config_rejects_mesh_and_sampler_mismatches(expect_error):
mesh_device = SimpleNamespace(shape=(1, 1))
other_mesh = SimpleNamespace(shape=(1, 1))
model = FakeModel(mesh_device)
reader = FakeReader(mesh_device)
layout = PageTableLayout(32, 256, 264, 256)
arguments = dict(
model=model,
output_reader=reader,
page_table_layout=layout,
max_batch_size=32,
max_prefill_chunk_size=2048,
device_sampling_enabled=True,
can_enable_trace=lambda length, cached: True,
)
with expect_error(ValueError, "model and prefill runtime"):
PrefillRuntimeConfig.resolve(**(arguments | {"output_reader": FakeReader(other_mesh)}))
model.sampling = None
with expect_error(TypeError, "model.sampling.config"):
PrefillRuntimeConfig.resolve(**arguments)
def test_prefill_runtime_config_rejects_non_power_of_two_batched_prefill_cap(expect_error):
mesh_device = SimpleNamespace(shape=(1, 1))
with expect_error(ValueError, "max_prefill_batch_size must be one of"):
PrefillRuntimeConfig.resolve(
model=FakeModel(mesh_device),
output_reader=FakeReader(mesh_device),
page_table_layout=PageTableLayout(32, 256, 264, 256),
max_batch_size=32,
max_prefill_chunk_size=2048,
supports_batched_prefill=True,
max_prefill_batch_size=3,
device_sampling_enabled=True,
can_enable_trace=lambda length, cached: True,
)
def test_page_table_layout_replacement_is_immutable_and_bounded(expect_error):
runtime = _runtime()
original = runtime.config
smaller = PageTableLayout(32, 128, 136, 128)
runtime.configure_page_table_layout(smaller)
assert runtime.config is not original
assert runtime.config.page_table_layout is smaller
assert original.page_table_layout.raw_capacity_width == 256
with expect_error(ValueError, "block_size"):
runtime.configure_page_table_layout(PageTableLayout(16, 128, 136, 128))
with expect_error(ValueError, "capacity ceiling"):
runtime.configure_page_table_layout(PageTableLayout(32, 512, 520, 512))
with expect_error(ValueError, "canonical geometry"):
runtime.configure_page_table_layout(PageTableLayout(32, 128, 520, 128))
def test_disabled_device_sampling_rejects_sampling_at_runtime_boundary(expect_error):
runtime = _runtime(device_sampling_enabled=False)
tokens, page_table, prompt_lens, start_pos = _inputs(prompt_length=80)
with expect_error(ValueError, "device sampling is disabled"):
runtime.prepare(
tokens=tokens,
page_table=page_table,
prompt_lens=prompt_lens,
empty_slots=[0],
start_pos=start_pos,
sampling_params=SamplingParams(temperature=0.0, top_k=1, top_p=1.0),
)
def test_prefill_runtime_request_signatures_are_exact():
empty = inspect.Parameter.empty
positional = inspect.Parameter.POSITIONAL_OR_KEYWORD
keyword_only = inspect.Parameter.KEYWORD_ONLY
expected = {
PrefillRuntime.can_trace: (
(
("self", positional, empty, empty),
("tokens", keyword_only, empty, "torch.Tensor"),
("prompt_lens", keyword_only, None, "torch.Tensor | None"),
("start_pos", keyword_only, None, "torch.Tensor | None"),
),
"bool",
),
PrefillRuntime.prepare: (
(
("self", positional, empty, empty),
("tokens", keyword_only, empty, "torch.Tensor"),
("page_table", keyword_only, empty, "torch.Tensor"),
("prompt_lens", keyword_only, None, "torch.Tensor | None"),
("start_pos", keyword_only, None, "torch.Tensor | None"),
("empty_slots", keyword_only, None, "Sequence[int] | None"),
("sampling_params", keyword_only, None, "SamplingParams | None"),
("prompt_tokens", keyword_only, None, "Any"),
("output_tokens", keyword_only, None, "Any"),
("slot_remap", keyword_only, None, "Any"),
),
"tuple[PreparedPrefill, ...]",
),
}
for method, (expected_parameters, expected_return) in expected.items():
signature = inspect.signature(method)
parameters = tuple(
(parameter.name, parameter.kind, parameter.default, parameter.annotation)
for parameter in signature.parameters.values()
)
assert parameters == expected_parameters
assert signature.return_annotation == expected_return
def test_prefill_runtime_is_plain_orchestration_with_one_config_surface():
source = inspect.getsource(prefill_module)
assert hasattr(prefill_module, "PrefillRuntimeConfig")
assert not hasattr(PrefillRuntime, "from_config")
assert "LightweightModule" not in source
assert PrefillRuntime.__bases__ == (object,)
assert tuple(inspect.signature(PrefillRuntime).parameters) == ("config",)
def test_prefill_package_has_no_compatibility_barrel():
prefill_package = importlib.import_module("models.common.llm_runtime.prefill")
assert not hasattr(prefill_package, "PrefillRuntime")
assert not hasattr(prefill_package, "PrefillRequest")
assert not hasattr(prefill_package, "__all__")
|