1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584
|
from __future__ import annotations
import contextlib
import dataclasses
import functools
import itertools
import logging
import textwrap
import traceback
import typing
from contextlib import nullcontext
from enum import Enum
from functools import partial
from typing import (
Any,
Callable,
ClassVar,
ContextManager,
Dict,
Generator,
Iterable,
List,
Literal,
Optional,
overload,
Sequence,
Set,
Tuple,
TYPE_CHECKING,
TypeVar,
Union,
)
from typing_extensions import assert_never, Never, TypeAlias
from unittest.mock import patch
import sympy
from sympy import Expr, Integer, Symbol
import torch._export.serde.schema as export_schema
import torch._logging
import torch.fx
import torch.utils._pytree as pytree
from torch._dynamo.utils import identity
from torch._export.serde.serialize import GraphModuleSerializer
from torch._higher_order_ops.auto_functionalize import can_auto_functionalize
from torch._inductor import metrics
from torch._prims_common import (
compute_required_storage_length,
is_boolean_dtype,
is_float_dtype,
make_channels_last_strides_for,
StrideType,
)
from torch._subclasses.fake_tensor import get_schema_info
from torch.fx.experimental.symbolic_shapes import (
CallMethodKey,
compute_unbacked_bindings,
DivideByKey,
free_unbacked_symbols,
rebind_unbacked,
resolve_unbacked_bindings,
ShapeEnv,
SymTypes,
)
from torch.utils._ordered_set import OrderedSet
from torch.utils._sympy.functions import CleanDiv, FloorDiv, ModularIndexing
from torch.utils._sympy.symbol import SymT
from . import config, dependencies
from .codegen.common import BackendFeature, index_prevent_reordering
from .dependencies import (
Dep,
extract_free_unbacked_symbols,
extract_input_node_reduction_ranges,
extract_read_writes,
var_builder,
)
from .loop_body import LoopBody
from .ops_handler import OpCounterCSE, OpCountResult
from .runtime.benchmarking import benchmarker
from .runtime.hints import DeviceProperties, ReductionHint
from .utils import (
argsort,
argsort_sym,
cache_on_self,
ceildiv,
convert_shape_to_inductor,
convert_shape_to_symint,
developer_warning,
get_kernel_metadata,
ir_dataclass,
is_dynamic,
is_gpu,
sympy_dot,
sympy_index_symbol,
sympy_index_symbol_with_prefix,
sympy_product,
sympy_subs,
)
from .virtualized import ops, OpsValue, V
if TYPE_CHECKING:
from torch.fx.node import Node
from .codegen.cuda.cuda_template import CUDATemplate
from .graph import GraphLowering
from .utils import IndentedBuffer
else:
CUDATemplate: TypeAlias = object
_T = TypeVar("_T")
_U = TypeVar("_U")
_V = TypeVar("_V")
_IntLike: TypeAlias = Union[int, Expr]
_NumLike: TypeAlias = Union[int, float, Expr]
log = logging.getLogger(__name__)
indent = functools.partial(textwrap.indent, prefix=" ")
aten = torch.ops.aten
""" [Note: Inductor IR]
Inductor's IR is produced by executing 'lowering' code (see lowering.py). Each
lowering is registered to a particular aten operator, and expects inputs that
correspond to the aten schema. However, in place of torch Tensor inputs, lowerings
expect Inductor TensorBox inputs.
TensorBox IR represents torch tensors. Tensors are sometimes single objects owning
storage, and sometimes views of another Tensor's storage. Mutating tensor operations
(such as add_()) affect the underlying storage and any associated views. Other operations
(such as .t_()) update metadata about the current view but don't modify the underlying storage.
To model this in Inductor, the IR distinguishes between TensorBox, View, StorageBox and Buffer.
TensorBox is the top level IR construct that any lowering should produce and maps to a torch.Tensor
output from an operation. But just as torch.Tensors take different forms, TensorBox IR can
reference View IR or directly reference StorageBox IRs.
Some Inductor lowerings produce new sets of 'Box'es, while others (such as .t() or other view ops)
may take an existing TensorBox and point it to a new underlying View IR.
Tensors that directly own storage are represented as a chain of:
TensorBox -> StorageBox -> Buffer
where Buffer is a simple (1D) allocation, and StorageBox introduces the concept of a Layout.
If you mutate the data of such a tensor, we swing the StorageBox pointer to point to a new buffer
(leaving the old buffer unmodified and functionalizing the operation).
Tensors backed by views add one more indirection to the IR.
TensorBox -> View -> StorageBox -> Buffer
In these cases, the underlying StorageBox/Buffer will be shared with the pre-view TensorBox.
Computation is represented by Operation nodes, with each operation producing 1
or more output Buffers. In the case of mutations, these will be new Buffers that have the
mutated buffer listed in its get_mutation_names().
It is also possible to have an InputBuffer for which there is no corresponding Operation,
e.g. it may be a graph input or compile time constant.
"""
_NodeOrNodes: TypeAlias = Union[
int,
"TensorBox",
Dict[str, "TensorBox"],
"Symbol",
"IRNode",
Sequence[
Optional[Union[int, Dict[str, "TensorBox"], "TensorBox", "Symbol", "IRNode"]]
],
]
def validate_ir(node_or_nodes: Optional[_NodeOrNodes]) -> None:
def _check_tensorbox(nodes: Optional[_NodeOrNodes]) -> None:
# Could expand this to check deeper properties
# (e.g. TensorBox points to View or StorageBox)
if nodes is None:
pass
elif isinstance(nodes, (list, tuple)):
for node in nodes:
_check_tensorbox(node)
elif isinstance(nodes, dict):
for node in nodes.values():
_check_tensorbox(node)
else:
assert isinstance(
nodes,
(
torch._inductor.ir.ExpandView,
DynamicScalar,
AssertScalar,
TensorBox,
sympy.logic.boolalg.Boolean,
Expr,
int,
EffectfulKernel,
),
), f"Found {type(nodes)}, which is not a supported top level IR node. See [Note: Inductor IR]"
# Be picky about the accepted data structure (don't use pytree here)
_check_tensorbox(node_or_nodes)
def ops_wrapper(name: str) -> Callable[..., OpsValue]:
assert isinstance(name, str)
def fn(*args: object, **kwargs: object) -> OpsValue:
return getattr(ops, name)(*args, **kwargs)
return fn
def inverse_reorder(order: Sequence[int]) -> Callable[[Sequence[_T]], Sequence[_T]]:
inv_order = dict(zip(order, range(len(order))))
def reindex(index: Sequence[_T]) -> Sequence[_T]:
assert len(index) == len(inv_order)
return [index[inv_order[i]] for i in range(len(index))]
return reindex
def same_reorder(order: Sequence[int]) -> Callable[[Sequence[_T]], Sequence[_T]]:
def reindex(index: Sequence[_T]) -> Sequence[_T]:
assert len(index) == len(order)
return [index[order[i]] for i in range(len(index))]
return reindex
def fuse_reindexing(
reindex1: Callable[[Sequence[_U]], Sequence[_V]],
reindex2: Callable[[Sequence[_T]], Sequence[_U]],
) -> Callable[[Sequence[_T]], Sequence[_V]]:
def reindex(index: Sequence[_T]) -> Sequence[_V]:
return reindex1(reindex2(index))
return reindex
NHWC_STRIDE_ORDER = [3, 0, 2, 1]
NHWDC_STRIDE_ORDER = [4, 0, 3, 2, 1]
def get_fill_order(
seq: Sequence[Union[int, torch.SymInt, Expr]], shape_env: Optional[ShapeEnv] = None
) -> Sequence[int]:
"""
Convert strides to fill order (argsort)
"""
if shape_env is None:
sorted_idx: Sequence[int] = argsort(seq)
else:
# argsort_sym handles unbacked symints (with the help of the shape_env)
sorted_idx = argsort_sym(shape_env, seq)
return sorted_idx
def stride_order2fill_order(order: Sequence[Union[int, Integer]]) -> Sequence[int]:
"""
Convert stride order to fill order
For channel last format,
stride order = [3, 0, 2, 1] and fill order = [1, 3, 2, 0]
"""
lookup = {pos: idx for idx, pos in enumerate(order)}
fill_order = [lookup[i] for i in range(len(order))]
return fill_order
def get_stride_order(
seq: Sequence[Union[int, torch.SymInt, Expr]], shape_env: Optional[ShapeEnv] = None
) -> Sequence[int]:
"""
Convert strides to stride order
"""
sorted_idx: Sequence[int] = get_fill_order(seq, shape_env)
out = [0 for _ in range(len(seq))]
for i, elem in enumerate(sorted_idx):
out[elem] = i
return out
@overload
def ir_node_to_tensor(x: Literal[None], guard_shape: bool = True) -> None:
...
@overload
def ir_node_to_tensor(x: IRNode, guard_shape: bool = True) -> torch.Tensor:
...
def ir_node_to_tensor(
x: Optional[IRNode], guard_shape: bool = True
) -> Optional[torch.Tensor]:
if x is None:
return None
shape_fn: Callable[[Union[int, Expr]], Union[int, Expr]]
if not guard_shape:
shape_fn = V.graph.sizevars.size_hint
else:
shape_fn = identity
size = [shape_fn(s) for s in x.get_size()]
stride: StrideType
if is_storage_and_layout(x):
stride = [shape_fn(s) for s in x.get_layout().stride]
else:
stride = FlexibleLayout.contiguous_strides(size)
dtype = x.get_dtype()
device = x.get_device()
size = convert_shape_to_symint(size)
stride = convert_shape_to_symint(stride)
with V.graph.sizevars.shape_env.suppress_guards():
t = torch.empty_strided(
size=size, stride=stride, dtype=dtype, device=device
).zero_()
return t
def may_convert_to_optional(
value: Optional[Sequence[_T]],
) -> Optional[Sequence[Optional[_T]]]:
if isinstance(value, list) and not value:
# [None] makes sure the cpp wrapper codegen will generate something like
# {std::nullopt} instead of {}
return [None]
return value
def get_device_type(
x: Union[IRNode, OutputSpec, torch.device, None, str]
) -> Optional[str]:
if isinstance(x, str) or x is None:
return x
elif isinstance(x, torch.device):
return x.type
elif isinstance(x, (IRNode, OutputSpec)):
return get_device_type(x.get_device())
assert_never(f"get_device_type({x}: {type(x).__name__})")
def is_triton(x: Union[IRNode, torch.device, None, str]) -> bool:
return is_gpu(get_device_type(x))
def is_cpu(x: Union[IRNode, torch.device, None, str]) -> bool:
return get_device_type(x) == "cpu"
class IRNode:
_current_origins: ClassVar[OrderedSet[Any]] = OrderedSet()
# NB: These are kinda weird,
origins: OrderedSet[Any] = dataclasses.field(init=False)
traceback: Optional[List[str]] = dataclasses.field(init=False)
origin_node: Optional[torch.fx.Node] = dataclasses.field(init=False)
@staticmethod
@contextlib.contextmanager
def current_origins(origins: OrderedSet[Node]) -> Generator[None, None, None]:
old = IRNode._current_origins
IRNode._current_origins = old | origins
try:
yield
finally:
IRNode._current_origins = old
def _post_init_setattr(self, attr: str, value: Any) -> None:
# Intended for use in __post_init__ for enforcing an invariant on a dataclass
# If you must, can also be used for setting provenance info
# We would like to try and minimize these usages though
object.__setattr__(self, attr, value)
def __post_init__(self) -> None:
self._post_init_setattr("origins", OrderedSet(self._current_origins))
self._post_init_setattr(
"traceback", traceback.format_stack() if config.debug_ir_traceback else None
)
self._post_init_setattr("origin_node", None)
def get_read_names(self) -> OrderedSet[str]:
return OrderedSet(dep.name for dep in self.get_reads())
def get_traceback(self) -> Optional[List[str]]:
return self.traceback
def get_origin_node(self) -> Optional[torch.fx.Node]:
return self.origin_node
def get_defining_op(self) -> Optional[Operation]:
return None
def common_repr(self, shorten: bool = True) -> Sequence[str]:
origins = f"origins={getattr(self, 'origins', '')}"
if shorten and len(origins) > 64:
# this can get *very* long
origins = f"{origins[:61]}..."
return [origins]
def str_helper(
self, lines: Sequence[object], shorten: bool = True, multiline: bool = True
) -> str:
lines = list(lines) + list(self.common_repr(shorten))
lines = list(map(str, lines))
if multiline:
new_lines = indent(",\n".join(lines))
return f"{type(self).__name__}(\n{new_lines}\n)"
else:
return f"{type(self).__name__}({lines})"
def get_dtype(self) -> torch.dtype:
return self.dtype
def maybe_get_dtype(self) -> Optional[torch.dtype]:
try:
return self.get_dtype()
except NotImplementedError:
return None
def get_layout(self) -> Layout:
raise NotImplementedError(f"get_layout() is not implemented by {type(self)}!")
def maybe_get_layout(self) -> Optional[Layout]:
try:
return self.get_layout()
except NotImplementedError:
return None
def get_output_spec(self) -> OutputSpec:
return self.get_layout()
def maybe_get_output_spec(self) -> Optional[OutputSpec]:
try:
return self.get_output_spec()
except NotImplementedError:
return None
def has_tensor_output(self) -> bool:
"""True for single tensor output (excludes MultiOutput)"""
return isinstance(self.maybe_get_output_spec(), Layout)
def get_size(self) -> Sequence[Expr]:
raise NotImplementedError(f"get_size() is not implemented by {type(self)}!")
def maybe_get_size(self) -> Optional[Sequence[_IntLike]]:
try:
return self.get_size()
except NotImplementedError:
return None
@property
def shape(self) -> Union[_IntLike, sympy.Rel, Sequence[_IntLike]]:
return self.get_size()
def get_numel(self) -> Expr:
return sympy_product(self.get_size())
def is_zero_elements(self) -> bool:
return V.graph.sizevars.is_expr_static_and_true(sympy.Eq(self.get_numel(), 0))
def realize(self) -> Optional[str]:
"""
If the IRNode refers to data which has not been materialized (e.g.,
it is a Pointwise/Reduction that could potentially have more
compute fused into it), realize the IRNode into physical memory,
ending the possibility of fusing into it, but allowing, e.g., multiple
users to access the data without having to recompute.
Check StorageBox.realize for a particularly notable implementation.
TODO(ezyang): I think, in principle, every IRNode should have an
implementation of this, and most of the time no-op is OK, but you
really do have to audit each IRNode for this, so for now, raise
an error if it's not implemented. Note that some code in graph.py
will catch this thrown error and suppress it with a warning.
"""
raise NotImplementedError(f"realize NYI on {type(self)}")
def codegen_reference(self, writer: Optional[IndentedBuffer] = None) -> str:
raise NotImplementedError(f"codegen_reference NYI on {type(self)}")
def get_device(self) -> Optional[torch.device]:
return None
def get_device_or_error(self) -> torch.device:
device = self.get_device()
assert device is not None
return device
def has_exceeded_max_reads(self) -> bool:
return False
def make_loader(self) -> Callable[[Sequence[Expr]], OpsValue]:
raise NotImplementedError(type(self).__name__)
def make_indexer(self) -> Callable[[Sequence[Expr]], Expr]:
raise NotImplementedError(type(self).__name__)
def get_stride(self) -> Sequence[_IntLike]:
raise NotImplementedError(type(self).__name__)
def maybe_get_stride(self) -> Optional[Sequence[_IntLike]]:
try:
return self.get_stride()
except NotImplementedError:
return None
def get_name(self) -> str:
raise NotImplementedError(type(self).__name__)
def maybe_get_name(self) -> Optional[str]:
try:
return self.get_name()
except NotImplementedError:
return None
def has_large_inner_fn(self, threshold: Optional[int] = None) -> bool:
return False
def mark_reuse(self, users: int) -> None:
pass
def realize_hint(self) -> None:
pass
def unwrap_view(self) -> IRNode:
raise NotImplementedError(type(self).__name__)
def freeze_layout(self) -> None:
raise NotImplementedError(type(self).__name__)
def freeze_layout_with_stride_order(
self, order: List[int], allow_padding: bool = False
) -> None:
raise NotImplementedError(type(self).__name__)
def freeze_layout_with_fill_order(self, order: List[int]) -> None:
raise NotImplementedError(type(self).__name__)
def freeze_layout_with_same_order(self, stride: List[_IntLike]) -> None:
raise NotImplementedError(type(self).__name__)
def freeze_layout_with_exact_strides(
self, exact_strides: List[_IntLike], allow_padding: bool = False
) -> None:
raise NotImplementedError(type(self).__name__)
def get_read_writes(self) -> dependencies.ReadWrites:
raise NotImplementedError(type(self).__name__)
def get_reads(self) -> OrderedSet[Dep]:
return self.get_read_writes().reads
def num_reads(self) -> int:
return len(self.get_reads())
def get_storage_numel(self) -> _IntLike:
raise NotImplementedError(type(self).__name__)
def get_unbacked_symbol_uses(self) -> OrderedSet[Symbol]:
raise NotImplementedError(type(self).__name__)
def get_reduction_type(self) -> Optional[str]:
raise NotImplementedError(type(self).__name__)
def get_reduction_size(self) -> Sequence[sympy.Expr]:
raise NotImplementedError(type(self).__name__)
def is_extern(self) -> bool:
return False
def is_no_op(self) -> bool:
return False
def constant_to_device(self, device: torch.device) -> IRNode:
raise NotImplementedError(type(self).__name__)
def get_mutation_names(self) -> Sequence[str]:
raise NotImplementedError(type(self).__name__)
def get_operation_name(self) -> str:
raise NotImplementedError(type(self).__name__)
def get_inputs_that_alias_output(self) -> Sequence[str]:
raise NotImplementedError(type(self).__name__)
if TYPE_CHECKING:
@property
def dtype(self) -> torch.dtype:
...
@ir_dataclass(frozen=False)
class Operation:
def __post_init__(self) -> None:
self.operation_name: Optional[str] = None
def get_device(self) -> Optional[torch.device]:
raise NotImplementedError
def get_origin_node(self) -> Optional[torch.fx.Node]:
assert hasattr(self, "origin_node")
return self.origin_node
def get_origins(self) -> OrderedSet[Any]:
assert hasattr(self, "origins")
return self.origins
def get_operation_name(self) -> str:
assert self.operation_name is not None
return self.operation_name
def is_extern(self) -> bool:
return False
def is_no_op(self) -> bool:
return False
def get_read_writes(self) -> dependencies.ReadWrites:
raise NotImplementedError
def is_user_of(self, name: str) -> bool:
return name in self.get_read_names()
def get_read_names(self) -> OrderedSet[str]:
return OrderedSet(dep.name for dep in self.get_reads())
def get_reads(self) -> OrderedSet[Dep]:
return self.get_read_writes().reads
def get_outputs(self) -> List[Buffer]:
raise NotImplementedError
def get_unbacked_symbol_defs(self) -> OrderedSet[sympy.Symbol]:
return OrderedSet()
def get_unbacked_symbol_uses(self) -> OrderedSet[sympy.Symbol]:
"""
Returns the unbacked symbols which are required to be in scope in
order to successfully perform codegen for this buffer. For example,
a buffer that corresponds to an extern kernel call that takes i0 as
an argument would return {i0} here. This is used to generate necessary
dependencies that ensure we actually bind i0 in codegen before you
try to use it.
Note that this is NOT transitive; in particular, if this buffer takes
in as input another buffer with dynamic shape (e.g., (i0,)), we will
not report it here, because you will already have a dependency
on that buffer, which will eventually have a dependency on i0 if
necessary.
"""
return OrderedSet()
def get_workspace_size(self) -> int:
"""
Gets extra global memory size needed by this buffer.
Some algorithms (e.g. group gemm) may require extra global memory in the generated code.
"""
return 0
@ir_dataclass
class Loops(IRNode):
device: torch.device
dtype: torch.dtype
inner_fn: Callable[..., Any]
ranges: Sequence[_IntLike]
def get_unbacked_symbol_uses(self) -> OrderedSet[Symbol]:
return OrderedSet().union(
*(free_unbacked_symbols(e) for e in self.ranges),
self.inner_fn_free_unbacked_symbols(),
)
def _to_str(self, names: Sequence[str]) -> str:
return self.str_helper(
[
f"'{self.device.type}'",
str(self.dtype),
self.inner_fn_str(),
]
+ [f"{name}={getattr(self, name)}" for name in names]
+ [f"origin_node={self.origin_node!r}"]
)
def __post_init__(self) -> None:
super().__post_init__()
def __str__(self) -> str:
return self._to_str(("ranges",))
__repr__ = __str__
def get_device(self) -> Optional[torch.device]:
return self.device
def get_origin_node(self) -> Optional[torch.fx.Node]:
return self.origin_node
def get_size(self) -> Sequence[Expr]:
return self.ranges
def get_pointwise_size(self) -> Sequence[Expr]:
return self.ranges
@classmethod
def create(cls, *args: Any, **kwargs: Any) -> TensorBox:
origin_node = kwargs.pop("origin_node", None)
tb = kwargs.pop("traceback", None)
# if "origin_node" in kwargs:
# breakpoint()
r = cls(*args, **kwargs)
# Need to explicitly set origin_node here to propagate it down.
# todo(chilli): I think it would be better for IRNode to directly set
# origin_node
r._post_init_setattr("origin_node", origin_node)
r._post_init_setattr("traceback", tb or r.traceback)
return TensorBox.create(r)
@staticmethod
def _index(ranges: Sequence[_IntLike], prefix: SymT = SymT.INDEX) -> Sequence[Expr]:
return [
sympy.S.Zero if s == 1 else sympy_index_symbol_with_prefix(prefix, n)
for n, s in enumerate(ranges)
]
@cache_on_self
def inner_fn_opcount(self) -> OpCountResult:
opcounter = OpCounterCSE(V.MockHandler())
with V.set_ops_handler(opcounter), patch.object(
FlexibleLayout, "allow_indexing", True
):
self.inner_fn(*self.inner_fn_args())
return opcounter.getvalue()
def inner_fn_args(self) -> Sequence[Sequence[_IntLike]]:
return (self._index(self.ranges),)
@cache_on_self
def inner_fn_str(self) -> str:
return V.KernelFormatterHandler.ir_to_string(
self.inner_fn, *self.inner_fn_args()
)
def has_large_inner_fn(self, threshold: Optional[int] = None) -> bool:
if threshold is None:
threshold = 0
threshold = max(threshold, config.realize_opcount_threshold)
return self.inner_fn_opcount().num_ops > threshold
def inner_fn_free_unbacked_symbols(self) -> Set[Symbol]:
index = self._index(self.ranges)
return extract_free_unbacked_symbols(self.inner_fn, index)
def get_reads(self) -> OrderedSet[Dep]:
with patch.object(FlexibleLayout, "allow_indexing", True):
if self.get_reduction_type():
return extract_read_writes(
self.make_loader(),
self.get_size(),
self.get_reduction_size(),
).reads
else:
return extract_read_writes(
self.make_loader(),
self.get_size(),
).reads
def get_read_names(self) -> OrderedSet[str]:
return OrderedSet(self.inner_fn_opcount().read_buffers)
def num_reads(self) -> int:
return len(self.inner_fn_opcount().read_buffers)
def get_reduction_size(self) -> Sequence[sympy.Expr]:
raise NotImplementedError(
f"get_reduction_size() is not implemented by {type(self)}!"
)
def get_reduction_type(self) -> Optional[str]:
raise NotImplementedError(
f"get_reduction_type() is not implemented by {type(self)}!"
)
def constant_to_device(self, device: torch.device) -> IRNode:
raise NotImplementedError(
f"constant_to_device() is not implemented by {type(self)}!"
)
def nop_loader_fn(idx: Union[Expr, Sequence[Expr]], *, dtype: torch.dtype) -> OpsValue:
if dtype.is_floating_point:
return ops.constant(float("nan"), dtype)
else:
return ops.constant(0, dtype)
@ir_dataclass
class Pointwise(Loops):
def make_loader(self) -> Callable[[Sequence[Expr]], OpsValue]:
# Make zero-element loops into a no-op
if self.is_zero_elements():
return partial(nop_loader_fn, dtype=self.dtype)
return self.inner_fn
def get_reduction_size(self) -> Sequence[sympy.Expr]:
return []
def get_reduction_type(self) -> Optional[str]:
return None
def store_output(
self,
output_name: Optional[str],
indexer: Callable[[Sequence[Expr]], Never],
vars: Sequence[Expr],
) -> OpsValue:
loader = self.make_loader()
return ops.store(output_name, indexer(vars), loader(vars))
def constant_to_device(self, device: torch.device) -> IRNode:
"""Move this to a given device. Requires that all reads are to constants."""
loader = self.make_loader()
loader = patch.object(ConstantBuffer, "override_device", device)(loader)
return Pointwise(
device=device, dtype=self.dtype, inner_fn=loader, ranges=self.ranges
)
@ir_dataclass
class Scatter(Pointwise):
output_indexer: Callable[[Sequence[Expr]], Expr]
scatter_mode: Optional[str] = None
def constant_to_device(self, device: torch.device) -> IRNode:
"""Move this to a given device. Requires that all reads are to constants."""
loader = self.make_loader()
loader = patch.object(ConstantBuffer, "override_device", device)(loader)
return Scatter(
device=device,
dtype=self.dtype,
inner_fn=loader,
ranges=self.ranges,
output_indexer=self.output_indexer,
scatter_mode=self.scatter_mode,
)
def store_output(
self,
output_name: Optional[str],
indexer: Callable[[Sequence[Expr]], Never],
vars: Sequence[Expr],
) -> OpsValue:
loader = self.make_loader()
return ops.store(
output_name,
indexer(self.output_indexer(vars)),
loader(vars),
mode=self.scatter_mode,
)
REDUCTION_COMBINE_FN: Dict[str, Callable[..., OpsValue]] = {
"any": ops_wrapper("logical_or"),
"max": ops_wrapper("maximum"),
"min": ops_wrapper("minimum"),
"prod": ops_wrapper("mul"),
"sum": ops_wrapper("add"),
"xor_sum": ops_wrapper("bitwise_xor"),
}
def get_reduction_combine_fn(
reduction_type: str, dtype: torch.dtype, arg_break_ties_left: bool = True
) -> Callable[..., object]:
if reduction_type in REDUCTION_COMBINE_FN:
return REDUCTION_COMBINE_FN[reduction_type]
elif reduction_type in ("argmax", "argmin"):
def argmax_combine_fn(
a: Tuple[object, object], b: Tuple[object, object]
) -> Tuple[OpsValue, OpsValue]:
a_value, a_index = a
b_value, b_index = b
if reduction_type == "argmin":
mask = ops.lt(a_value, b_value)
else:
mask = ops.gt(a_value, b_value)
equal = ops.eq(a_value, b_value)
if is_float_dtype(dtype):
a_isnan = ops.ne(a_value, a_value)
b_isnan = ops.ne(b_value, b_value)
mask = ops.logical_or(mask, ops.gt(a_isnan, b_isnan))
equal = ops.logical_or(equal, ops.logical_and(a_isnan, b_isnan))
tie = (
ops.lt(a_index, b_index)
if arg_break_ties_left
else ops.gt(a_index, b_index)
)
mask = ops.logical_or(mask, ops.logical_and(equal, tie))
return (
ops.where(mask, a_value, b_value),
ops.where(mask, a_index, b_index),
)
return argmax_combine_fn
elif reduction_type == "welford_combine":
def welford_combine_fn(
a: Tuple[OpsValue, OpsValue, OpsValue],
b: Tuple[OpsValue, OpsValue, OpsValue],
) -> Tuple[OpsValue, OpsValue, OpsValue]:
a_mean, a_m2, a_weight = a
b_mean, b_m2, b_weight = b
delta = b_mean - a_mean
new_weight = a_weight + b_weight
w2_over_w = b_weight / new_weight
return (
a_mean + delta * w2_over_w,
a_m2 + b_m2 + delta * delta * a_weight * w2_over_w,
new_weight,
)
return welford_combine_fn
else:
raise NotImplementedError(f"unknown reduction_type={reduction_type}")
def significant_strides_equal(
strides1: Sequence[_IntLike], strides2: Sequence[_IntLike], size: Sequence[_IntLike]
) -> bool:
"""
Returns true if the strides are equal, ignoring dimensions of size 1 .
"""
non_1_indices = [
i
for i, dim in enumerate(size)
if V.graph.sizevars.size_hint(dim, fallback=2) != 1
]
strides1 = [V.graph.sizevars.size_hint(strides1[i]) for i in non_1_indices]
strides2 = [V.graph.sizevars.size_hint(strides2[i]) for i in non_1_indices]
return strides1 == strides2
@ir_dataclass
class Reduction(Loops):
reduction_ranges: Sequence[_IntLike]
reduction_type: str
# self.dtype represents the dst dtype
src_dtype: torch.dtype
reduction_hint: ReductionHint
def __str__(self) -> str:
return self._to_str(("ranges", "reduction_ranges", "reduction_type"))
__repr__ = __str__
def get_unbacked_symbol_uses(self) -> OrderedSet[Symbol]:
return super().get_unbacked_symbol_uses() | OrderedSet().union(
*(free_unbacked_symbols(e) for e in self.reduction_ranges)
)
def get_reduction_size(self) -> Sequence[sympy.Expr]:
return self.reduction_ranges
def get_reduction_type(self) -> Optional[str]:
return self.reduction_type
def store_reduction(
self,
output_name: Optional[str],
indexer: Callable[[Sequence[Expr]], Never],
vars: Sequence[Expr],
reduction_vars: Sequence[Symbol],
) -> OpsValue:
value = ops.reduction(
self.dtype,
self.src_dtype,
self.reduction_type,
self.inner_fn(vars, reduction_vars),
)
return ops.store_reduction(output_name, indexer(vars), value)
def index_length(self) -> int:
return len(self.ranges) + len(self.reduction_ranges)
def inner_fn_args(self) -> Sequence[Sequence[Expr]]:
index = self._index(self.ranges)
rindex = self._index(self.reduction_ranges, SymT.RINDEX)
return (index, rindex)
def inner_fn_free_unbacked_symbols(self) -> Set[Symbol]:
index = self._index(self.ranges)
rindex = self._index(self.reduction_ranges, SymT.RINDEX)
return extract_free_unbacked_symbols(self.inner_fn, index, rindex)
def constant_to_device(self, device: torch.device) -> IRNode:
"""Move this to a given device. Requires that all reads are to constants."""
loader = self.make_loader()
loader = patch.object(ConstantBuffer, "override_device", device)(loader)
return Reduction(
device=device,
dtype=self.dtype,
inner_fn=loader,
ranges=self.ranges,
reduction_ranges=self.reduction_ranges,
reduction_type=self.reduction_type,
src_dtype=self.src_dtype,
reduction_hint=ReductionHint.DEFAULT,
)
@staticmethod
def num_splits(
device: torch.device,
dst_dtype: torch.dtype,
src_dtype: torch.dtype,
inner_fn: Callable[..., OpsValue],
ranges: Sequence[_IntLike],
reduction_ranges: Sequence[_IntLike],
reduction_type: str,
reduction_numel: Expr,
input_node: Optional[IRNode] = None,
) -> Tuple[ReductionHint, _IntLike]:
def _is_static(x: object) -> bool:
return isinstance(x, (int, Integer))
reduction_numel_hint = V.graph.sizevars.symbolic_hint(reduction_numel)
numel_hint = V.graph.sizevars.symbolic_hint(sympy_product(ranges))
should_split = reduction_type == "scan" or (
not V.graph.has_feature(device, BackendFeature.REDUCE_TO_SINGLE_ELEMENT)
and reduction_type
not in (
"argmax",
"argmin",
)
and config.split_reductions
)
if not (_is_static(reduction_numel_hint) and _is_static(numel_hint)):
# We don't support unbacked symints
return ReductionHint.DEFAULT, 1
props = DeviceProperties.create(device)
num_sm = props.multi_processor_count
min_elements_per_thread = 32
if should_split:
inner_reduction_splits: Callable[[int, int], int] = functools.partial(
V.choices.reduction_split_factor, device, inner_reduction=True
)
outer_reduction_splits: Callable[[int, int], int] = functools.partial(
V.choices.reduction_split_factor, device, inner_reduction=False
)
else:
def inner_reduction_splits(
reduction_numel_hint: int,
numel_hint: int,
) -> int:
return 1
outer_reduction_splits = inner_reduction_splits
# easy cases
if numel_hint == 1:
split = inner_reduction_splits(reduction_numel_hint, numel_hint)
if split == 1:
# No need to split.
return ReductionHint.INNER, split
if input_node is not None and isinstance(input_node, TensorBox):
new_ranges, new_reduction_ranges = extract_input_node_reduction_ranges(
input_node
)
if new_ranges is not None and new_reduction_ranges is not None:
extracted_numel_hint = V.graph.sizevars.symbolic_hint(
sympy_product(new_ranges + new_reduction_ranges)
)
if reduction_numel_hint == extracted_numel_hint:
log.debug(
"Use previous IRNode's range and reduction_ranges instead of split. "
"current ranges: %s, current reduction ranges: %s, current split: %d, "
"new ranges: %s, new reduction ranges: %s",
ranges,
reduction_ranges,
split,
new_ranges,
new_reduction_ranges,
)
# If the input_node or its dependent nodes are also Reduction nodes,
# use reduction_sizes of this node or its dependent nodes directly.
return ReductionHint.INNER, -1
return ReductionHint.INNER, split
if (
reduction_numel_hint <= min_elements_per_thread
or numel_hint >= num_sm * 2 * 32
):
return ReductionHint.DEFAULT, 1
r = Reduction(
device=device,
dtype=dst_dtype,
inner_fn=inner_fn,
ranges=ranges,
reduction_ranges=reduction_ranges,
reduction_type=reduction_type,
src_dtype=src_dtype,
reduction_hint=ReductionHint.DEFAULT,
)
def get_read_indices(r: Reduction) -> Tuple[Sequence[Expr], bool]:
cb = ComputedBuffer(
name=None,
layout=FlexibleLayout(
device=r.get_device(),
dtype=r.get_dtype(),
size=r.get_size(),
),
data=r,
)
read_writes = cb.get_read_writes()
# try finding the full size producer
# TODO this will fail for something like ((1, N) * (N, 1)).sum()
# this would also possibly be wrong for producers with the different contiguity but we hope those cases are rare
assert read_writes.range_vars is not None
range_vars = [
r
for r in read_writes.range_vars
if isinstance(r, Expr) and not isinstance(r, sympy.Number)
]
indices = []
changed = False
for md in sorted(read_writes.reads, key=lambda x: x.name):
if all(r in md.index.free_symbols for r in range_vars):
indices.append(md.index)
if md.name in V.graph.name_to_buffer:
buf = V.graph.name_to_buffer[md.name]
original_stride = getattr(buf.layout, "stride", None)
buf.decide_layout()
if getattr(buf.layout, "stride", None) != original_stride:
changed = True
return indices, changed
indices, changed = get_read_indices(r)
if changed:
indices, _ = get_read_indices(r)
if len(indices) == 0:
# TODO determine splits when all inputs are broadcast
return ReductionHint.DEFAULT, 1
(_, reduction_vars), ranges1 = dependencies.index_vars_squeeze(
r.get_size(), r.get_reduction_size()
)
num_outer = 0
num_inner = 0
for i in indices:
j = V.graph.sizevars.simplify_with_ranges(i, ranges1)
strides = V.graph.sizevars.stride_hints(j, reduction_vars, ranges1.keys())
outer = all(s > 1 for s in strides)
if outer:
num_outer += 1
else:
num_inner += 1
if num_inner > num_outer:
return ReductionHint.INNER, inner_reduction_splits(
reduction_numel_hint, numel_hint
)
else:
return ReductionHint.OUTER, outer_reduction_splits(
reduction_numel_hint, numel_hint
)
@staticmethod
def _unroll_reduction_fn(
inner_fn: Callable[[Sequence[_IntLike], Sequence[_IntLike]], OpsValue],
reduction_ranges: Sequence[_IntLike],
reduction_type: str,
src_dtype: torch.dtype,
) -> Callable[[Sequence[_IntLike]], OpsValue]:
"""Convert inner_fn from a reduction to an pointwise"""
reduction_ranges = [
V.graph.sizevars.evaluate_static_shape(x) for x in reduction_ranges
]
combine_fn = get_reduction_combine_fn(reduction_type, src_dtype)
def fn(index: Sequence[_IntLike]) -> Any:
return functools.reduce(
combine_fn,
(
value_fn(index, rindex)
for rindex in itertools.product(
*[range(x) for x in reduction_ranges]
)
),
)
value_fn: Callable[[Sequence[_IntLike], Sequence[_IntLike]], Any]
if reduction_type in ("argmin", "argmax"):
flatten_index = FixedLayout(
None, # type: ignore[arg-type]
None, # type: ignore[arg-type]
reduction_ranges,
FlexibleLayout.contiguous_strides(reduction_ranges),
).make_indexer()
def value_fn(
index: Sequence[_IntLike], rindex: Sequence[_IntLike]
) -> Tuple[OpsValue, OpsValue]:
rindex = [sympy.expand(i) for i in rindex]
return (
inner_fn(index, rindex),
ops.index_expr(flatten_index(rindex), torch.int64),
)
return lambda index: fn(index)[1]
else:
value_fn = inner_fn
return fn
@classmethod
def create(
cls,
device: torch.device,
dst_dtype: torch.dtype,
src_dtype: torch.dtype,
inner_fn: Callable[..., Any],
ranges: Sequence[Expr],
reduction_ranges: Sequence[Expr],
reduction_type: str,
reduction_hint: ReductionHint = ReductionHint.DEFAULT,
input_node: Optional[IRNode] = None,
) -> TensorBox:
reduction_numel = V.graph.sizevars.simplify(sympy_product(reduction_ranges))
if reduction_numel == 0:
# N.B. This is a hack to generate the literal of the given type
# Ideally, we should be fixing `def constant` in triton.py
# but it breaks due to hardcoded dtypes in other places
def py_cnst(val: object) -> Union[bool, float, int]:
if dst_dtype == torch.bool:
return bool(val)
elif dst_dtype.is_floating_point:
assert isinstance(val, typing.SupportsFloat)
return float(val)
else:
assert isinstance(val, typing.SupportsInt)
return int(val)
rtypes_to_inits = {
"sum": py_cnst(0),
"xor_sum": py_cnst(0),
"prod": py_cnst(1),
"any": py_cnst(0),
# "all" is desugared to `!any(!val)`
}
assert (
reduction_type in rtypes_to_inits.keys()
), f"{reduction_type} not supported for zero-dimension tensors!"
def const_fn(index: int) -> OpsValue:
return ops.constant(rtypes_to_inits[reduction_type], dst_dtype)
return Pointwise.create(
device=device,
dtype=src_dtype,
inner_fn=const_fn,
ranges=list(ranges),
)
if reduction_numel == 1:
# this reduction is actually a pointwise op
if reduction_type in ("argmin", "argmax"):
def fn(index: int) -> OpsValue:
return ops.constant(0, dst_dtype)
else:
def fn(index: int) -> OpsValue:
reduction_index = [sympy.S.Zero for _ in reduction_ranges]
return inner_fn(index, reduction_index)
return Pointwise.create(
device=device, dtype=dst_dtype, inner_fn=fn, ranges=ranges
)
if (
isinstance(reduction_numel, Integer)
and V.graph.sizevars.size_hint(reduction_numel)
< config.unroll_reductions_threshold
and (sympy_product(ranges) != 1 or is_gpu(device.type))
):
# NB: This works around https://github.com/pytorch/pytorch/issues/140457
# since turning reductions into pointwise ops can exacerbate this problem
return Pointwise.create(
device=device,
dtype=dst_dtype,
inner_fn=cls._unroll_reduction_fn(
inner_fn, reduction_ranges, reduction_type, src_dtype
),
ranges=ranges,
)
# triton doesn't support reduce to single element well, so break it up
hint, split = cls.num_splits(
device,
dst_dtype,
src_dtype,
inner_fn,
ranges,
reduction_ranges,
reduction_type,
reduction_numel,
input_node,
)
# intermediate reduction in split can contain complex indexing,
# and num_splits will fail to correctly set the hint
# reuse the passed hint if available
if reduction_hint == ReductionHint.DEFAULT:
reduction_hint = hint
if split == -1:
assert input_node is not None
new_ranges, new_reduction_ranges = extract_input_node_reduction_ranges(
input_node
)
assert new_ranges is not None
assert new_reduction_ranges is not None
return cls.create_multilayer_existing_ranges(
device,
dst_dtype,
src_dtype,
inner_fn,
ranges,
reduction_ranges,
new_ranges,
new_reduction_ranges,
reduction_type,
reduction_hint,
)
elif split > 1:
# triton doesn't support reduce to single element well, so break it up
return cls.create_multilayer(
device,
dst_dtype,
src_dtype,
inner_fn,
ranges,
reduction_ranges,
reduction_type,
split,
reduction_hint,
)
return TensorBox.create(
Reduction(
device=device,
dtype=dst_dtype,
inner_fn=inner_fn,
ranges=ranges,
reduction_ranges=reduction_ranges,
reduction_type=reduction_type,
src_dtype=src_dtype,
reduction_hint=reduction_hint,
)
)
@staticmethod
def default_accumulator(
reduction_type: str, dtype: torch.dtype
) -> Union[_NumLike, Sequence[_NumLike]]:
if reduction_type in ("max", "argmax"):
if is_float_dtype(dtype):
return float("-inf")
elif is_boolean_dtype(dtype):
return 0
else:
return torch.iinfo(dtype).min
if reduction_type in ("min", "argmin"):
if is_float_dtype(dtype):
return float("inf")
elif is_boolean_dtype(dtype):
return 1
else:
return torch.iinfo(dtype).max
return {
"sum": 0,
"prod": 1,
"xor_sum": 0,
"any": 0,
"welford_reduce": (0, 0, 0),
"welford_combine": (0, 0, 0),
}[reduction_type]
@staticmethod
def default_value(
reduction_type: str, dtype: torch.dtype
) -> Union[_NumLike, Sequence[_NumLike]]:
if reduction_type == "welford_reduce":
return 0
return Reduction.default_accumulator(reduction_type, dtype)
@staticmethod
def _multilayer_second_step_hint(
split: _IntLike, numel_hint: int, reduction_hint: ReductionHint
) -> ReductionHint:
if split == -1:
return reduction_hint
if split <= 512 and numel_hint <= 512 and reduction_hint == ReductionHint.OUTER:
return ReductionHint.OUTER_TINY
if (
split <= 1024
and numel_hint <= 256
and reduction_hint == ReductionHint.OUTER
):
return ReductionHint.OUTER_TINY
return reduction_hint
@classmethod
def _multilayer_wrap_loader(
cls,
loader: Callable[..., OpsValue],
reduction_ranges: Sequence[_IntLike],
reduction_numel: _IntLike,
split: _IntLike,
block_size: _IntLike,
default: Union[_NumLike, Sequence[_NumLike]],
) -> Callable[..., object]:
reindex = View.dynamic_reshape_indexer(reduction_ranges, [reduction_numel])
need_mask = not V.graph.sizevars.is_expr_static_and_true(
sympy.Eq(reduction_numel % split, 0)
)
def wrapper_fn(
index: Sequence[Symbol], reduction_index: Sequence[Symbol]
) -> OpsValue:
(reduction_index,) = reduction_index
*new_index, reduction_block = index
indices = block_size * reduction_block + reduction_index
def body() -> OpsValue:
return loader(new_index, reindex([indices]))
if need_mask:
mask = ops.lt(
ops.index_expr(indices, torch.int32),
ops.index_expr(reduction_numel, torch.int32),
)
return ops.masked(mask, body, default)
else:
return body()
return wrapper_fn
@classmethod
def _multilayer_wrap_loader_existing_ranges(
cls,
loader: Callable[[Sequence[sympy.Expr], Sequence[sympy.Expr]], OpsValue],
original_ranges: Sequence[Expr],
original_reduction_ranges: Sequence[Expr],
new_ranges: Sequence[Integer],
new_reduction_ranges: Sequence[Integer],
) -> Callable[[Sequence[sympy.Expr], Sequence[sympy.Expr]], OpsValue]:
assert all(
r == 1 for r in original_ranges
), f"Only enabled for numel_hint == 1, found {original_ranges=}"
reindex = View.dynamic_reshape_indexer(
original_reduction_ranges, tuple(new_ranges) + tuple(new_reduction_ranges)
)
def wrapper_fn(
merged_index: Sequence[sympy.Expr],
new_reduction_index: Sequence[sympy.Expr],
) -> OpsValue:
original_idx = merged_index[: len(original_ranges)]
new_index = merged_index[len(original_ranges) :]
return loader(
original_idx,
reindex(tuple(new_index) + tuple(new_reduction_index)),
)
return wrapper_fn
@classmethod
def create_multilayer_helper(
cls,
device: torch.device,
dst_dtype: torch.dtype,
src_dtype: torch.dtype,
wrapper_fn: Callable[..., Any],
original_ranges: Sequence[Expr],
original_reduction_ranges: Sequence[Expr],
new_ranges: List[Expr],
new_reduction_ranges: List[Integer],
reduction_type: str,
split: _IntLike,
reduction_hint: ReductionHint,
) -> TensorBox:
"""
Break a large reduction up into multiple smaller reductions
recursively
"""
# triton will automatically compute reductions in fp32 if reducing over fp16/bf16
# within the kernel. keep the intermediate in fp32 so as to keep the whole reduction
# in fp32 and not reduce precision by breaking up the kernel into multiple layers
intermediate_dtype = (
dst_dtype
if dst_dtype not in (torch.float16, torch.bfloat16)
else torch.float
)
intermediate = Reduction.create(
device,
intermediate_dtype,
src_dtype,
wrapper_fn,
new_ranges,
new_reduction_ranges,
reduction_type,
reduction_hint,
)
intermediate.realize()
intermediate_loader = intermediate.make_loader()
def intermediate_fn(
index: Sequence[_IntLike], reduction_index: Sequence[_IntLike]
) -> OpsValue:
return intermediate_loader([*index, *reduction_index])
numel_hint = V.graph.sizevars.size_hint(sympy_product(original_ranges))
reduction_hint = cls._multilayer_second_step_hint(
split, numel_hint, reduction_hint
)
assert original_ranges == new_ranges[: len(original_ranges)]
return TensorBox.create(
Reduction(
device=device,
dtype=dst_dtype,
inner_fn=intermediate_fn,
ranges=original_ranges,
reduction_ranges=new_ranges[len(original_ranges) :],
reduction_type=reduction_type,
src_dtype=src_dtype,
reduction_hint=reduction_hint,
)
)
@classmethod
def create_multilayer(
cls,
device: torch.device,
dst_dtype: torch.dtype,
src_dtype: torch.dtype,
inner_fn: Callable[..., Any],
ranges: Sequence[Expr],
reduction_ranges: Sequence[Expr],
reduction_type: str,
split: _IntLike,
reduction_hint: ReductionHint,
) -> TensorBox:
"""
Break a large reduction up into multiple smaller reductions
recursively
"""
# TODO(jansel): realize the reduction so we can do dynamic indexing
reduction_numel = sympy_product(reduction_ranges)
block_size = FloorDiv(reduction_numel + (split - 1), split)
default = cls.default_value(reduction_type, dst_dtype)
wrapper_fn = cls._multilayer_wrap_loader(
inner_fn, reduction_ranges, reduction_numel, split, block_size, default
)
return cls.create_multilayer_helper(
device,
dst_dtype,
src_dtype,
wrapper_fn,
ranges,
reduction_ranges,
[*ranges, split],
[block_size],
reduction_type,
split,
reduction_hint,
)
@classmethod
def create_multilayer_existing_ranges(
cls,
device: torch.device,
dst_dtype: torch.dtype,
src_dtype: torch.dtype,
inner_fn: Callable[..., Any],
original_ranges: Sequence[Expr],
original_reduction_ranges: Sequence[Expr],
new_ranges: List[Integer],
new_reduction_ranges: List[Integer],
reduction_type: str,
reduction_hint: ReductionHint,
) -> TensorBox:
"""
Break a large reduction up into multiple smaller reductions
recursively
"""
wrapper_fn = cls._multilayer_wrap_loader_existing_ranges(
inner_fn,
original_ranges,
original_reduction_ranges,
new_ranges,
new_reduction_ranges,
)
return cls.create_multilayer_helper(
device,
dst_dtype,
src_dtype,
wrapper_fn,
original_ranges,
original_reduction_ranges,
[*original_ranges, *new_ranges],
new_reduction_ranges,
reduction_type,
-1,
reduction_hint,
)
class WelfordReduction(Reduction):
output_index: int
def __init__(
self,
device: torch.device,
dtype: torch.dtype,
inner_fns: Sequence[Callable[[Sequence[Expr], Sequence[Expr]], OpsValue]],
ranges: Sequence[Integer],
reduction_ranges: Sequence[Integer],
reduction_type: str,
reduction_hint: ReductionHint,
output_index: int,
) -> None:
loader: Callable[[Sequence[Expr], Sequence[Expr]], Any]
if len(inner_fns) == 1:
loader = inner_fns[0]
else:
def loader(
idx: Sequence[Expr], reduction_idx: Sequence[Expr]
) -> Tuple[OpsValue, ...]:
return tuple(fn(idx, reduction_idx) for fn in inner_fns)
super().__init__(
device=device,
dtype=dtype,
inner_fn=loader,
ranges=ranges,
reduction_ranges=reduction_ranges,
reduction_type=reduction_type,
src_dtype=dtype,
reduction_hint=reduction_hint,
)
self.output_index = output_index
def store_reduction(
self,
output_name: Optional[str],
indexer: Callable[[Sequence[Expr]], Never],
vars: Sequence[Expr],
reduction_vars: Sequence[Symbol],
) -> OpsValue:
values = ops.reduction(
self.dtype,
self.src_dtype,
self.reduction_type,
self.inner_fn(vars, reduction_vars),
)
value = values[self.output_index]
return ops.store_reduction(output_name, indexer(vars), value)
@classmethod
def create( # type: ignore[override]
cls,
device: torch.device,
dtype: torch.dtype,
inner_fns: Sequence[Callable[..., Any]],
ranges: List[Integer],
reduction_ranges: List[Integer],
reduction_type: str,
reduction_hint: ReductionHint = ReductionHint.DEFAULT,
) -> Sequence[TensorBox]:
assert reduction_type in ("welford_reduce", "welford_combine")
reduction_numel = V.graph.sizevars.simplify(sympy_product(reduction_ranges))
def const(val: int) -> TensorBox:
def inner_fn(idx: Sequence[Expr]) -> OpsValue:
return ops.constant(
val,
dtype,
)
return Pointwise.create(
device=device,
dtype=dtype,
inner_fn=inner_fn,
ranges=list(ranges),
)
if reduction_numel == 0:
mean = const(0)
m2 = const(0)
weight = const(0)
return mean, m2, weight
if reduction_numel == 1:
def copy(
loader: Callable[[Sequence[Expr], Sequence[Expr]], OpsValue]
) -> TensorBox:
def inner_fn(idx: Sequence[Expr]) -> OpsValue:
reduction_index = [sympy.S.Zero for _ in reduction_ranges]
return loader(idx, reduction_index)
return Pointwise.create(
device=device,
dtype=dtype,
inner_fn=inner_fn,
ranges=list(ranges),
)
if reduction_type == "welford_reduce":
return copy(inner_fns[0]), const(0), const(1)
else:
return tuple(copy(fn) for fn in inner_fns)
# TODO: Unrolled reduction
# if (
# isinstance(reduction_numel, Integer)
# and V.graph.sizevars.size_hint(reduction_numel)
# < config.unroll_reductions_threshold
# and sympy_product(ranges) != 1
# ):
# return Pointwise.create(
# device,
# dst_dtype,
# cls._unroll_reduction_fn(
# inner_fn, reduction_ranges, reduction_type, src_dtype
# ),
# ranges,
# )
# triton doesn't support reduce to single element well, so break it up
hint, split = Reduction.num_splits(
device,
dtype,
dtype,
inner_fns[0],
ranges,
reduction_ranges,
reduction_type=reduction_type,
reduction_numel=reduction_numel,
)
# intermediate reduction in split can contain complex indexing,
# and num_splits will fail to correctly set the hint
# reuse the passed hint if available
if reduction_hint == ReductionHint.DEFAULT:
reduction_hint = hint
if split > 1:
# triton doesn't support reduce to single element well, so break it up
return cls.create_multilayer(
device,
dtype,
inner_fns,
ranges,
reduction_ranges,
reduction_type,
split,
reduction_hint,
)
results = [
TensorBox.create(
WelfordReduction(
device,
dtype,
inner_fns,
ranges,
reduction_ranges,
reduction_type,
reduction_hint,
output_idx,
)
)
for output_idx in range(3)
]
for t in results:
t.realize()
return results
@staticmethod
def default_value(
reduction_type: str, dtype: torch.dtype
) -> Union[_NumLike, Sequence[_NumLike]]:
return (0, 0, 0)
@classmethod
def create_multilayer( # type: ignore[override]
cls,
device: torch.device,
dtype: torch.dtype,
inner_fns: Sequence[Callable[..., Any]],
ranges: List[Integer],
reduction_ranges: List[Integer],
reduction_type: str,
split: _IntLike,
reduction_hint: ReductionHint,
) -> Sequence[TensorBox]:
"""
Break a large reduction up into multiple smaller reductions
recursively
"""
reduction_numel = sympy_product(reduction_ranges)
need_mask = not V.graph.sizevars.is_expr_static_and_true(
sympy.Eq(reduction_numel % split, 0)
)
if need_mask and reduction_type != "welford_combine":
# If we need mask, then "welford_reduce" doesn't work because
# masked inputs shouldn't count towards the welford weight
def constant(
idx: Sequence[Expr], reduction_idx: Sequence[Expr], value: int
) -> OpsValue:
return ops.constant(value, dtype)
return cls.create_multilayer(
device=device,
dtype=dtype,
inner_fns=(
inner_fns[0],
partial(constant, value=0),
partial(constant, value=1),
),
ranges=ranges,
reduction_ranges=reduction_ranges,
reduction_type="welford_combine",
split=split,
reduction_hint=reduction_hint,
)
block_size = FloorDiv(reduction_numel + (split - 1), split)
intermediates = WelfordReduction.create(
device,
dtype,
tuple(
cls._multilayer_wrap_loader(
loader,
reduction_ranges,
reduction_numel,
split,
block_size,
default=0,
)
for loader in inner_fns
),
[*ranges, split],
[block_size],
reduction_type,
reduction_hint,
)
for i in intermediates:
i.realize()
def intermediate_loader_fn(
index: Sequence[Expr],
reduction_index: Sequence[Expr],
loader: Callable[[Sequence[Expr]], OpsValue],
) -> OpsValue:
return loader([*index, *reduction_index])
numel_hint = V.graph.sizevars.size_hint(sympy_product(ranges))
reduction_hint = cls._multilayer_second_step_hint(
split, numel_hint, reduction_hint
)
return WelfordReduction.create(
device,
dtype,
tuple(
partial(intermediate_loader_fn, loader=i.make_loader())
for i in intermediates
),
ranges,
[split],
# welford_reduce turns one input into three outputs, which are combined with welford_combine
"welford_combine",
reduction_hint,
)
@ir_dataclass
class Scan(Loops):
scan_ranges: List[Integer]
size: List[Integer]
combine_fn: Callable[[Tuple[Any, ...], Tuple[Any, ...]], Tuple[Any, ...]]
reindex: Callable[[Sequence[_IntLike], Sequence[_IntLike]], Sequence[_IntLike]]
reduction_hint: ReductionHint
output_index: int
# output_index indexes the following tuples
dtypes: Tuple[torch.dtype, ...]
inner_fns: Tuple[Callable[..., Any], ...]
# HACK we mimick reduction
def get_unbacked_symbol_uses(self) -> OrderedSet[Symbol]:
# TODO: Can combine_fn/reindex close over unbacked symbols? If so, we
# need to explicitly represent the closure so we can pull out unbacked
# symbols here
return (
super().get_unbacked_symbol_uses()
| OrderedSet().union(*(free_unbacked_symbols(e) for e in self.scan_ranges))
| OrderedSet().union(*(free_unbacked_symbols(e) for e in self.size))
)
def __post_init__(self) -> None:
assert len(self.ranges) + len(self.scan_ranges) == len(self.size)
super().__post_init__()
def store_reduction(
self,
output_name: Optional[str],
indexer: Callable[[Sequence[_IntLike]], Never],
vars: Sequence[Expr],
scan_vars: Sequence[Symbol],
) -> OpsValue:
idx = self.reindex(vars, scan_vars)
values = [inner_fn(idx) for inner_fn in self.inner_fns]
result = ops.scan(self.dtypes, self.combine_fn, values)
return ops.store(output_name, indexer(idx), result[self.output_index])
def get_reduction_type(self) -> Optional[str]:
# return self.scan_op
return "custom"
def get_reduction_size(self) -> Sequence[sympy.Expr]:
return self.scan_ranges
def get_size(self) -> Sequence[Expr]:
return self.size
def get_pointwise_size(self) -> Sequence[Expr]:
return self.ranges
def index_length(self) -> int:
return len(self.ranges) + len(self.scan_ranges)
def inner_fn_args(self) -> Sequence[Sequence[_IntLike]]:
index = self._index(self.ranges)
rindex = self._index(self.scan_ranges, SymT.RINDEX)
idx = self.reindex(index, rindex)
return (idx,)
def inner_fn_free_unbacked_symbols(self) -> Set[Symbol]:
index = self._index(self.ranges)
rindex = self._index(self.scan_ranges, SymT.RINDEX)
idx = self.reindex(index, rindex)
return extract_free_unbacked_symbols(self.inner_fn, idx)
@classmethod
def create( # type: ignore[override]
cls,
device: torch.device,
dtypes: Tuple[torch.dtype, ...],
inner_fns: Tuple[Callable[[Sequence[Expr]], Any], ...],
size: List[Integer],
axis: int,
combine_fn: Callable[[Tuple[Any, ...], Tuple[Any, ...]], Tuple[Any, ...]],
reduction_hint: ReductionHint = ReductionHint.DEFAULT,
*,
# Whether we have the option to fallback to aten
can_fallback_to_aten: bool = True,
**kwargs: Any,
) -> Sequence[Optional[TensorBox]]:
pointwise_ranges = [*size[:axis], *size[axis + 1 :]]
scan_ranges = [size[axis]]
if not V.graph.has_feature(device, BackendFeature.SCAN):
return [None] * len(dtypes)
if len(dtypes) > 1 and not V.graph.has_feature(
device, BackendFeature.TUPLE_REDUCTION
):
return [None] * len(dtypes)
sizevars = V.graph.sizevars
scan_numel = sizevars.simplify(sympy_product(scan_ranges))
assert len(dtypes) == len(inner_fns)
# Scan with a single element is just a copy
if sizevars.is_expr_static_and_true(sympy.Le(scan_numel, 1)):
return [
Pointwise.create(
device=device,
dtype=dtypes[output_index],
inner_fn=inner_fns[output_index],
ranges=size,
)
for output_index in range(len(dtypes))
]
reduction_hint, num_splits = cls.num_splits(
device=device,
dtype=dtypes[0],
inner_fn=inner_fns[0],
axis=axis,
pointwise_ranges=pointwise_ranges,
scan_ranges=scan_ranges,
combine_fn=combine_fn,
scan_numel=scan_numel,
)
scan_type = Scan
if num_splits > 1:
supports_split = torch.version.hip is None and len(dtypes) == 1
if not supports_split:
if can_fallback_to_aten:
# Fallback to ATen
return [None] * len(dtypes)
else:
num_splits = 1
else:
scan_type = SplitScan
def reindex(index: Sequence[Expr], scan_index: Sequence[Expr]) -> List[Expr]:
assert len(scan_index) == len(scan_ranges)
assert len(index) == len(pointwise_ranges)
return [*index[:axis], *scan_index, *index[axis:]]
results = [
TensorBox.create(
scan_type(
device=device,
dtype=dtypes[output_index],
dtypes=dtypes,
inner_fn=inner_fns[output_index],
inner_fns=inner_fns,
size=size,
ranges=pointwise_ranges,
scan_ranges=scan_ranges,
combine_fn=combine_fn,
reindex=reindex,
reduction_hint=reduction_hint,
output_index=output_index,
**kwargs,
)
)
for output_index in range(len(dtypes))
]
for result in results:
result.realize()
return results
@classmethod
def num_splits(
cls,
device: torch.device,
dtype: torch.dtype,
inner_fn: Callable[[Sequence[Expr]], OpsValue],
axis: int,
pointwise_ranges: List[Integer],
scan_ranges: List[Integer],
combine_fn: Callable[[Tuple[Any, ...], Tuple[Any, ...]], Tuple[Any, ...]],
scan_numel: Expr,
) -> Tuple[ReductionHint, _IntLike]:
# TODO: custom splitting heuristic for scan
def wrapper_fn(idx: Sequence[Expr], reduction_idx: Sequence[Expr]) -> OpsValue:
return inner_fn([*idx[:axis], *reduction_idx, *idx[axis:]])
return Reduction.num_splits(
device=device,
dst_dtype=dtype,
src_dtype=dtype,
inner_fn=wrapper_fn,
ranges=pointwise_ranges,
reduction_ranges=scan_ranges,
reduction_type="scan",
reduction_numel=scan_numel,
)
# This signifies a scan op that should go through TritonSplitScanKernel codegen on CUDA.
@ir_dataclass
class SplitScan(Scan):
pass
@ir_dataclass
class Sort(Loops):
# Sorts a tuple of key, value pairs
sort_ranges: List[Integer]
size: List[Integer]
reindex: Callable[[Sequence[Expr], Sequence[Expr]], Sequence[Expr]]
reduction_hint: ReductionHint
output_index: int
# output_index indexes the following tuples
dtypes: Tuple[torch.dtype, ...]
inner_fns: Tuple[Callable[..., Any], ...]
stable: bool
descending: bool
# HACK we mimick reduction
def get_unbacked_symbol_uses(self) -> OrderedSet[Symbol]:
return (
super().get_unbacked_symbol_uses()
| OrderedSet().union(*(free_unbacked_symbols(e) for e in self.sort_ranges))
| OrderedSet().union(*(free_unbacked_symbols(e) for e in self.size))
)
def __post_init__(self) -> None:
assert len(self.ranges) + len(self.sort_ranges) == len(self.size)
super().__post_init__()
def store_reduction(
self,
output_name: Optional[str],
indexer: Callable[[Sequence[Expr]], Expr],
vars: Sequence[Expr],
reduction_vars: Sequence[Expr],
) -> OpsValue:
idx = self.reindex(vars, reduction_vars)
values = [inner_fn(idx) for inner_fn in self.inner_fns]
result = ops.sort(self.dtypes, values, self.stable, self.descending)
return ops.store(output_name, indexer(idx), result[self.output_index])
def get_reduction_type(self) -> Optional[str]:
return "sort"
def get_reduction_size(self) -> Sequence[Expr]:
return self.sort_ranges
def get_size(self) -> Sequence[Expr]:
return self.size
def get_pointwise_size(self) -> Sequence[Expr]:
return self.ranges
def index_length(self) -> int:
return len(self.ranges) + len(self.sort_ranges)
def inner_fn_args(self) -> Sequence[Sequence[Expr]]:
index = self._index(self.ranges)
rindex = self._index(self.sort_ranges, SymT.RINDEX)
idx = self.reindex(index, rindex)
return (idx,)
def inner_fn_free_unbacked_symbols(self) -> Set[Symbol]:
index = self._index(self.ranges)
rindex = self._index(self.sort_ranges, SymT.RINDEX)
idx = self.reindex(index, rindex)
return extract_free_unbacked_symbols(self.inner_fn, idx)
@classmethod
def create( # type: ignore[override]
cls,
device: torch.device,
dtypes: Tuple[torch.dtype, ...],
inner_fns: Tuple[Callable[[List[Expr]], Any], ...],
size: List[Integer],
axis: int,
stable: bool,
descending: bool,
reduction_hint: ReductionHint = ReductionHint.DEFAULT,
**kwargs: Any,
) -> Sequence[Optional[TensorBox]]:
pointwise_ranges = [*size[:axis], *size[axis + 1 :]]
sort_ranges = [size[axis]]
if not V.graph.has_feature(device, BackendFeature.SORT):
return [None] * len(dtypes)
sizevars = V.graph.sizevars
sort_numel = sizevars.simplify(sympy_product(sort_ranges))
# Heuristic, smallest rblock where triton usually outperforms aten.sort
# It also isn't bandwidth bound so fusion is unlikely to help.
max_rblock = 512
is_persistent_kernel = (
config.triton.persistent_reductions
and sizevars.is_expr_static_and_true(sympy.Le(sort_numel, max_rblock))
)
if not is_persistent_kernel:
# We only support persistent triton kernels
return [None] * len(dtypes)
assert len(dtypes) == len(inner_fns)
# Sort with a single element is just a copy
if sizevars.is_expr_static_and_true(sympy.Le(sort_numel, 1)):
return [
Pointwise.create(
device=device,
dtype=dtypes[output_index],
inner_fn=inner_fns[output_index],
ranges=size,
)
for output_index in range(len(dtypes))
]
def reindex(index: Sequence[Expr], sort_index: Sequence[Expr]) -> List[Expr]:
assert len(sort_index) == len(sort_ranges)
assert len(index) == len(pointwise_ranges)
return [*index[:axis], *sort_index, *index[axis:]]
results = [
TensorBox.create(
Sort(
device=device,
dtype=dtypes[output_index],
dtypes=dtypes,
inner_fn=inner_fns[output_index],
inner_fns=inner_fns,
size=size,
ranges=pointwise_ranges,
sort_ranges=sort_ranges,
reindex=reindex,
reduction_hint=reduction_hint,
output_index=output_index,
stable=stable,
descending=descending,
**kwargs,
)
)
for output_index in range(len(dtypes))
]
for result in results:
result.realize()
return results
def is_storage_and_layout(x: IRNode) -> bool:
try:
as_storage_and_layout(x, freeze=False)
return True
except NotImplementedError:
return False
def is_contiguous_storage_and_layout(x: IRNode) -> bool:
try:
buffer, layout = as_storage_and_layout(x, freeze=False)
# pad the stride here so we will NOT claim an tensor as contiguous
# if a padding is gonna happen.
if layout.should_pad_strides():
layout.pad_strides()
return layout.is_contiguous()
except NotImplementedError:
return False
def as_storage_and_layout(
x: IRNode,
freeze: bool = True,
want_contiguous: bool = False,
stride_order: Optional[Sequence[Union[int, Integer]]] = None,
allow_padding: bool = False,
exact_strides: Optional[Sequence[Union[int, Integer]]] = None,
) -> Tuple[StorageBox, Layout]:
"""
Try to simplify x into a StorageBox and a Layout.
allow_padding only affect how we apply stride_order. When allow_padding
is True, we have the freedom to add padding when applying the stride_order.
"""
if isinstance(x, TensorBox):
return as_storage_and_layout(
x.data,
freeze=freeze,
want_contiguous=want_contiguous,
stride_order=stride_order,
allow_padding=allow_padding,
exact_strides=exact_strides,
)
if isinstance(x, StorageBox) and isinstance(x.data, Buffer):
if freeze:
if want_contiguous:
x.data.freeze_layout()
assert x.data.get_layout().is_contiguous()
elif stride_order is not None:
x.data.freeze_layout_with_stride_order(
stride_order, allow_padding=allow_padding
)
elif exact_strides is not None:
x.data.freeze_layout_with_exact_strides(
exact_strides, allow_padding=allow_padding
)
else:
x.data.decide_layout()
return x, x.data.get_layout()
if isinstance(x, ReinterpretView):
# making the base of x contiguous or stride_ordered will not necessarily make
# the ReinterpretView either, so don't pass along those arguments
buffer, _ = as_storage_and_layout(
x.data,
freeze=freeze,
)
return buffer, x.layout
raise NotImplementedError
as_contiguous_storage_and_layout = functools.partial(
as_storage_and_layout, want_contiguous=True
)
def is_stride_order_storage_and_layout(
x: IRNode, stride_order: Sequence[Union[int, Integer]]
) -> bool:
try:
buffer, layout = as_storage_and_layout(x, freeze=False)
return layout.is_stride_ordered(stride_order)
except NotImplementedError:
return False
@ir_dataclass
class BaseView(IRNode):
data: IRNode
def get_unbacked_symbol_uses(self) -> OrderedSet[Symbol]:
return self.data.get_unbacked_symbol_uses()
def make_reindexer(self) -> Callable[[Sequence[Expr]], Sequence[Expr]]:
raise NotImplementedError(f"make_reindexer NYI on {self}")
def make_indexer(self) -> Callable[[Sequence[Expr]], Expr]:
inner = self.data.make_indexer()
reindex = self.make_reindexer()
def indexer(idx: Sequence[Expr]) -> Expr:
return inner(reindex(idx))
return indexer
def make_loader(self) -> Callable[[Sequence[Expr]], OpsValue]:
inner = self.data.make_loader()
reindex = self.make_reindexer()
def loader(idx: Sequence[Expr]) -> OpsValue:
return inner(reindex(idx))
return loader
@property
def dtype(self) -> torch.dtype:
return self.data.get_dtype()
def get_layout(self) -> Layout:
return self.data.get_layout()
def get_device(self) -> Optional[torch.device]:
return self.data.get_device()
def get_origin_node(self) -> Optional[torch.fx.Node]:
return None
def get_name(self) -> str:
return self.data.get_name()
def get_pointwise_size(self) -> Sequence[Expr]:
return self.get_size()
def mark_reuse(self, users: int) -> None:
return self.data.mark_reuse(users)
def has_exceeded_max_reads(self) -> bool:
return self.data.has_exceeded_max_reads()
def realize(self) -> Optional[str]:
return self.data.realize()
def realize_hint(self): # type: ignore[no-untyped-def]
return self.data.realize_hint()
def get_storage_numel(self): # type: ignore[no-untyped-def]
return self.data.get_storage_numel()
def is_extern(self) -> bool:
return self.data.is_extern() # type: ignore[attr-defined]
def is_module_buffer(self) -> bool:
return self.data.is_module_buffer() # type: ignore[attr-defined]
def get_read_names(self) -> OrderedSet[str]:
return self.data.get_read_names()
def get_reads(self) -> OrderedSet[Dep]:
with patch.object(FlexibleLayout, "allow_indexing", True):
return extract_read_writes(
self.make_loader(),
self.get_size(), # type: ignore[arg-type]
).reads
def unwrap_view(self): # type: ignore[no-untyped-def]
x: IRNode = self
while isinstance(x, BaseView):
x = x.data
return x
def constant_to_device(self, device: torch.device) -> IRNode:
"""Move this to a given device. Requires that all reads are to constants."""
loader = self.make_loader()
loader = patch.object(ConstantBuffer, "override_device", device)(loader)
return Pointwise(
device=device,
dtype=self.get_dtype(),
inner_fn=loader,
ranges=self.get_size(),
)
@ir_dataclass
class ExpandView(BaseView):
size: List[Expr]
@staticmethod
def _normalize_size(x, new_size): # type: ignore[no-untyped-def]
"""Replace `-1` with correct sizes"""
sizevars = V.graph.sizevars
new_size = list(map(sympy.expand, new_size))
old_size = x.get_size()
old_size = [None] * (len(new_size) - len(old_size)) + list(old_size)
assert len(new_size) == len(old_size)
for i in range(len(new_size)):
if new_size[i] == -1:
assert old_size[i] is not None
new_size[i] = old_size[i]
elif old_size[i] is None or V.graph.sizevars.shape_env.evaluate_expr(
sympy.Eq(old_size[i], 1), size_oblivious=True
):
pass
else:
# Sanity check: Expect broadcast compatibility
#
# NB: new_size[i] == old_size[i] is expected to already be
# guarded because the meta formula was expected to have taught
# us this equality.
assert (
sizevars.size_hint(new_size[i] - old_size[i], fallback=0) == 0
), "Broadcast failed in ExpandView({x.get_size()}, {new_size}) on dimension {i}"
return new_size
@classmethod
def create(cls, x, new_size): # type: ignore[no-untyped-def]
new_size = cls._normalize_size(x, new_size)
if is_storage_and_layout(x):
storage, old_layout = as_storage_and_layout(x)
skip = len(new_size) - len(old_layout.size)
assert skip >= 0
new_stride = [sympy.S.Zero] * skip
for stride, size in zip(old_layout.stride, old_layout.size):
new_stride.append(
stride
if not V.graph.sizevars.shape_env.evaluate_expr(
sympy.Eq(size, 1), size_oblivious=True
)
else sympy.S.Zero
)
new_layout = FixedLayout(
old_layout.device,
old_layout.dtype,
list(new_size),
new_stride,
old_layout.offset,
)
return ReinterpretView(data=storage, layout=new_layout)
return ExpandView(data=x, size=new_size)
def get_size(self) -> Sequence[Expr]:
return self.size
def make_reindexer(self): # type: ignore[no-untyped-def]
target = self.get_size()
actual = self.data.get_size()
skip = len(target) - len(actual)
def reindex(index): # type: ignore[no-untyped-def]
index = list(index[skip:])
assert len(index) == len(actual)
for i in range(len(actual)):
if actual[i] == 1:
# zero out broadcast dimension
index[i] = sympy.S.Zero
return index
return reindex
@ir_dataclass
class PermuteView(BaseView):
dims: List[Expr]
@classmethod
def create(cls, x, dims): # type: ignore[no-untyped-def]
dims = cls._map_neg_dims(dims)
assert OrderedSet(dims) == OrderedSet(range(len(dims)))
if is_storage_and_layout(x):
storage, old_layout = as_storage_and_layout(x)
new_layout = FixedLayout(
old_layout.device,
old_layout.dtype,
[old_layout.size[i] for i in dims],
[old_layout.stride[i] for i in dims],
old_layout.offset,
)
return ReinterpretView(data=storage, layout=new_layout)
return PermuteView(data=x, dims=dims)
@classmethod
def _map_neg_dims(cls, dims): # type: ignore[no-untyped-def]
return [dim if dim >= 0 else len(dims) + dim for dim in dims]
def get_size(self) -> Sequence[Expr]:
assert OrderedSet(self._map_neg_dims(self.dims)) == OrderedSet(
range(len(self.dims))
)
size = self.data.get_size()
return [size[i] for i in self.dims]
def make_reindexer(self): # type: ignore[no-untyped-def]
inv = {j: i for i, j in enumerate(self.dims)}
inv = [inv[i] for i in range(len(self.dims))]
assert OrderedSet(inv) == OrderedSet(range(len(self.dims)))
def reindex(index): # type: ignore[no-untyped-def]
return [index[i] for i in inv]
return reindex
@ir_dataclass
class SqueezeView(BaseView):
@classmethod
def create(cls, x, *, dim=None): # type: ignore[no-untyped-def]
if is_storage_and_layout(x):
storage, old_layout = as_storage_and_layout(x)
new_size = []
new_stride = []
if dim is not None:
assert isinstance(dim, int), "expected integer dim argument"
assert 0 <= dim and dim < len(old_layout.size)
for i, (size, stride) in enumerate(zip(old_layout.size, old_layout.stride)):
if dim is None:
if size != 1:
new_size.append(size)
new_stride.append(stride)
else:
if i != dim:
new_size.append(size)
new_stride.append(stride)
else:
assert size == 1, "expected squeezed size to be 1"
new_layout = FixedLayout(
old_layout.device,
old_layout.dtype,
new_size,
new_stride,
old_layout.offset,
)
return ReinterpretView(data=storage, layout=new_layout)
if dim is None:
# redirect to a generic view
return View.create(x, [s for s in x.get_size() if s != 1])
else:
assert x.get_size()[dim] == 1
return View.create(x, [s for i, s in enumerate(x.get_size()) if i != dim])
@staticmethod
def squeezer(size: Sequence[sympy.Expr]): # type: ignore[no-untyped-def]
new_size = [s for s in size if s != 1]
not_one = [i for i, s in enumerate(size) if s != 1]
length = len(size)
def reindex(index: List[sympy.Expr]) -> Tuple[sympy.Expr, ...]:
assert len(index) == len(not_one), f"{index} {not_one}"
new_index = [sympy.S.Zero] * length
for idx, s in zip(not_one, index):
new_index[idx] = s
return tuple(new_index)
return new_size, reindex
def __init__(self, data) -> None: # type: ignore[no-untyped-def]
raise AssertionError("use SqueezeView.create()")
@ir_dataclass
class GenericView(BaseView):
size: List[Expr]
reindex: Callable[..., Any]
def make_reindexer(self): # type: ignore[no-untyped-def]
return self.reindex
def reindex_str(self) -> str:
index_old = [
sympy_index_symbol_with_prefix(SymT.INDEX, n) for n in range(len(self.size))
]
index_new = list(self.reindex(index_old))
return f"lambda {', '.join(map(str, index_old))}: {index_new}"
def __str__(self) -> str:
return self.str_helper(
[self.data, f"size={self.size}", f"reindex={self.reindex_str()}"]
)
__repr__ = __str__
@classmethod
def create(cls, x, new_size, reindex): # type: ignore[no-untyped-def]
return cls(data=x, size=list(new_size), reindex=reindex)
def get_size(self) -> Sequence[Expr]:
return self.size
@ir_dataclass
class View(GenericView):
@staticmethod
def handle_negative_index(idx, size): # type: ignore[no-untyped-def]
idx = sympy.expand(idx)
size = sympy.expand(size)
evaluate_expr = V.graph.sizevars.shape_env.evaluate_expr
if evaluate_expr(sympy.Lt(idx, 0)):
idx = idx + size
return idx
@classmethod
def create(cls, x, new_size): # type: ignore[no-untyped-def]
assert isinstance(new_size, (tuple, list))
old_size, new_size = cls.resolve_negative_size(x.get_size(), new_size)
# Skip pointless views
if V.graph.sizevars.statically_known_list_equals(old_size, new_size):
return x
unbacked_symbols_in_sizes = False
if (
len(free_unbacked_symbols(old_size)) > 0
or len(free_unbacked_symbols(new_size)) > 0
):
unbacked_symbols_in_sizes = True
if 0 in new_size:
def fake_reindex(index): # type: ignore[no-untyped-def]
return tuple([0] * len(old_size))
return cls(data=x, size=list(new_size), reindex=fake_reindex)
# TODO: a new class for FixedTransferLayout that output layout is constrained by input layout
elif is_contiguous_storage_and_layout(x) or unbacked_symbols_in_sizes:
if unbacked_symbols_in_sizes and (not is_contiguous_storage_and_layout(x)):
# realize x; otherwise, the dynamic_reshape_indexer below will fail
# due to the size_hint's inability to process unbacked SymInts
x = ExternKernel.realize_input(x)
storage, old_layout = as_contiguous_storage_and_layout(x)
new_layout = FixedLayout(
old_layout.device,
old_layout.dtype,
new_size,
FlexibleLayout.contiguous_strides(new_size),
old_layout.offset,
)
return ReinterpretView(data=storage, layout=new_layout)
reindex = cls.dynamic_reshape_indexer(old_size, new_size)
return cls(data=x, size=list(new_size), reindex=reindex)
@staticmethod
def resolve_negative_size(old_size, new_size): # type: ignore[no-untyped-def]
new_size = [V.graph.sizevars.simplify(x) for x in new_size]
old_size = [V.graph.sizevars.simplify(x) for x in old_size]
new_size = list(new_size)
for i in range(len(new_size)):
if new_size[i] == -1:
new_size[i] = sympy.S.One
new_size[i] = CleanDiv(sympy_product(old_size), sympy_product(new_size))
break
V.graph.sizevars.guard_equals(sympy_product(old_size), sympy_product(new_size))
return old_size, new_size
@classmethod
def dynamic_reshape_indexer(cls, old_size, new_size): # type: ignore[no-untyped-def]
try:
reindex = cls._dynamic_reshape_indexer(old_size, new_size)
except (AssertionError, IndexError):
# optimistic algorithm failed, lets do a fallback
flat = [sympy_product(old_size)]
reindex1 = cls._dynamic_reshape_indexer(old_size, flat)
reindex2 = cls._dynamic_reshape_indexer(flat, new_size)
reindex = fuse_reindexing(reindex1, reindex2)
return reindex
@staticmethod
def _dynamic_reshape_indexer(old_size, new_size): # type: ignore[no-untyped-def]
"""
Perform a reshape entirely by modifying indexing math
"""
size_hint = V.graph.sizevars.size_hint
# TODO: These symbols may not escape, if they don't assert so and
# treat them as temporary
vars = [
sympy_index_symbol_with_prefix(SymT.VIEW, i) for i in range(len(new_size))
]
stack_new = list(zip(vars, new_size))
stack_old = list(old_size)
view_expr = []
while stack_new and stack_old:
size_old = stack_old.pop()
var, size_new = stack_new.pop()
if size_old == 1:
view_expr.append(sympy.S.Zero)
stack_new.append((var, size_new)) # re-add
elif size_new == 1:
stack_old.append(size_old) # re-add
elif size_hint(size_new) == size_hint(size_old):
view_expr.append(var)
V.graph.sizevars.guard_equals(size_new, size_old)
elif size_hint(size_new) < size_hint(size_old):
while size_hint(size_new) < size_hint(size_old):
var2, size_new2 = stack_new.pop()
var = var2 * size_new + var
size_new = size_new * size_new2
view_expr.append(var)
V.graph.sizevars.guard_equals(size_new, size_old)
elif size_hint(size_new) > size_hint(size_old):
divisor = sympy.S.One
modulus = size_old
view_expr.append(ModularIndexing(var, divisor, modulus))
divisor = divisor * modulus
while size_hint(size_new) > size_hint(size_old):
modulus = stack_old.pop()
view_expr.append(ModularIndexing(var, divisor, modulus))
divisor = divisor * modulus
size_old = size_old * modulus
V.graph.sizevars.guard_equals(size_new, size_old)
else:
raise AssertionError
while stack_old:
size_old = stack_old.pop()
V.graph.sizevars.guard_equals(size_old, 1)
view_expr.append(sympy.S.Zero)
while stack_new:
var, size_new = stack_new.pop()
V.graph.sizevars.guard_equals(size_new, 1)
view_expr.reverse()
assert len(view_expr) == len(old_size)
def reindex(index): # type: ignore[no-untyped-def]
assert len(index) == len(vars), (len(index), len(vars))
replacements = dict(zip(vars, index))
return tuple(sympy_subs(x, replacements) for x in view_expr)
return reindex
@ir_dataclass
class ReinterpretView(BaseView):
"""Pretend our storage has a different layout"""
layout: Layout
def __post_init__(self) -> None:
super().__post_init__()
if isinstance(self.data, BaseView):
object.__setattr__(self, "data", self.data.unwrap_view())
def __str__(self) -> str:
return self.str_helper(
[
self.data,
self.layout,
]
)
__repr__ = __str__
def get_name(self): # type: ignore[no-untyped-def]
return self.data.get_name()
def get_device(self) -> Optional[torch.device]:
return self.layout.device
def get_origin_node(self) -> Optional[torch.fx.Node]:
return None
@property
def dtype(self): # type: ignore[no-untyped-def]
return self.layout.dtype
def get_size(self) -> Sequence[Expr]:
return list(self.layout.size)
def get_stride(self): # type: ignore[no-untyped-def]
return list(self.layout.stride)
def make_loader(self) -> Callable[[Sequence[Expr]], OpsValue]:
def loader(index: Sequence[Expr]) -> OpsValue:
indexer = self.layout.make_indexer()
tmp_loader = ops.load(self.get_name(), indexer(index))
if self.layout.dtype != self.data.dtype:
return ops.to_dtype_bitcast(tmp_loader, self.dtype, self.data.dtype)
else:
return tmp_loader
return loader
def make_indexer(self) -> Callable[[Sequence[Expr]], Expr]:
return self.layout.make_indexer()
def get_layout(self) -> Layout:
return self.layout
def freeze_layout(self): # type: ignore[no-untyped-def]
pass
def get_unbacked_symbol_uses(self) -> OrderedSet[sympy.Symbol]:
return (
free_unbacked_symbols(self.layout.size)
| free_unbacked_symbols(self.layout.stride)
| free_unbacked_symbols(self.layout.offset)
)
def codegen_reference(self, writer: Optional[IndentedBuffer] = None) -> str:
# reinterpret_tensor is similar to as_strided except:
# - offset is added to the existing offset (rather than replacing it)
# - view tracking is disabled similar to unsafe_view
return V.graph.wrapper_code.codegen_reinterpret_view(
self.data,
self.layout.size,
self.layout.stride,
self.layout.offset,
writer.writeline if writer is not None else V.graph.wrapper_code.writeline,
dtype=self.layout.dtype,
)
def num_reads(self) -> int:
return 1
@ir_dataclass
class DtypeView(BaseView):
"""Pretend our storage has a different type"""
target_dtype: torch.dtype
@classmethod
def create(cls, x, new_dtype): # type: ignore[no-untyped-def]
if is_storage_and_layout(x):
storage, old_layout = as_storage_and_layout(x)
new_layout = FixedLayout(
old_layout.device,
new_dtype,
old_layout.size,
old_layout.stride,
old_layout.offset,
)
return ReinterpretView(data=storage, layout=new_layout)
return DtypeView(data=x, target_dtype=new_dtype)
def __str__(self) -> str:
return self.str_helper([self.data, self.target_dtype])
__repr__ = __str__
@property
def dtype(self): # type: ignore[no-untyped-def]
return self.target_dtype
def get_size(self) -> Sequence[Expr]:
return self.data.get_size()
def make_loader(self) -> Callable[[Sequence[Expr]], OpsValue]:
inner = self.data.make_loader()
def loader(idx): # type: ignore[no-untyped-def]
return ops.to_dtype_bitcast(inner(idx), self.target_dtype, self.data.dtype)
return loader
class SliceView(View):
@classmethod
def normalize_start_end(cls, x, dim, start, end): # type: ignore[no-untyped-def]
"""
Normalize start and end such that both are in the range
[0, x.get_size()[dim]] and start <= end.
"""
sizevars = V.graph.sizevars
dim_size = x.get_size()[dim]
if any(free_unbacked_symbols(x) for x in (start, end, dim_size)):
def clamp(x, lower, upper): # type: ignore[no-untyped-def]
return sympy.Min(sympy.Max(x, lower), upper)
else:
def clamp(x, lower, upper): # type: ignore[no-untyped-def]
return sizevars.evaluate_min(sizevars.evaluate_max(x, lower), upper)
def clamp_wrap(val, lower, upper, default): # type: ignore[no-untyped-def]
if val is None:
return default
val = cls.handle_negative_index(val, dim_size)
return clamp(val, lower, upper)
start = clamp_wrap(start, 0, dim_size, 0)
end = clamp_wrap(end, start, dim_size, dim_size)
return start, end
@classmethod
def create(cls, x, dim, start, end, step=1, clamp=True): # type: ignore[no-untyped-def]
step = sympy.expand(step)
assert isinstance(step, sympy.Expr) or step > 0
try:
if start == 0 and end >= 2**63 - 1 and step == 1:
return x
except TypeError:
pass
sizevars = V.graph.sizevars
new_size = list(x.get_size())
# NB: Ordinarily we default to clamping.
# We only don't clamp for split_with_sizes. For split_with_sizes, sizes should be already valid
# failing in this situation is ok, since invalid sizes could trigger silent errors.
if clamp:
start, end = cls.normalize_start_end(x, dim, start, end)
new_size[dim] = FloorDiv(end - start + (step - 1), step)
if is_storage_and_layout(x):
# Fast path
storage, old_layout = as_storage_and_layout(x)
new_stride = list(old_layout.stride)
new_stride[dim] = new_stride[dim] * step
new_layout = FixedLayout(
old_layout.device,
old_layout.dtype,
new_size,
new_stride,
old_layout.offset + old_layout.stride[dim] * start,
)
return ReinterpretView(data=storage, layout=new_layout)
def reindex(index): # type: ignore[no-untyped-def]
assert len(index) == len(new_size), f"wrong ndim {index} {new_size}"
index = list(index)
index[dim] = index[dim] * step + start
return index
# redirect to a generic view
return SliceView(data=x, size=new_size, reindex=reindex)
@ir_dataclass
class BaseConstant(IRNode):
dtype: torch.dtype
device: torch.device
def get_size(self) -> Sequence[Expr]:
return ()
def get_device(self) -> Optional[torch.device]:
return self.device
def get_origin_node(self) -> Optional[torch.fx.Node]:
return None
def get_reads(self) -> OrderedSet[Dep]:
return OrderedSet()
@ir_dataclass
class Constant(BaseConstant):
value: Any
dtype: torch.dtype
device: torch.device
def make_loader(self) -> Callable[[Sequence[Expr]], OpsValue]:
def loader(index: Sequence[Expr]) -> OpsValue:
return ops.constant(self.value, self.dtype)
return loader
def realize(self) -> Optional[str]:
pass
def constant_to_device(self, device: torch.device) -> IRNode:
return Constant(value=self.value, dtype=self.dtype, device=device)
@ir_dataclass
class IndexingConstant(BaseConstant):
index: Any
dtype: torch.dtype
device: torch.device
def make_loader(self) -> Callable[[Sequence[Expr]], OpsValue]:
def loader(index: Sequence[Expr]) -> OpsValue:
return ops.index_expr(self.index, self.dtype)
return loader
def constant_to_device(self, device: torch.device) -> IRNode:
return IndexingConstant(index=self.index, dtype=self.dtype, device=device)
def is_contiguous_strides_for_shape(
stride: Sequence[_IntLike], shape: Sequence[_IntLike]
) -> bool:
return all(
size == 1 or left == right
for left, right, size in zip(
stride, FlexibleLayout.contiguous_strides(shape), shape
)
)
def get_align_for_dtype(dtype: torch.dtype) -> int:
return config.padding_alignment_bytes // dtype.itemsize
class OutputSpec:
"""Abstract base for Layout, MultiOutputLayout, NoneLayout.
Represents the memory layout of the output of an Operation."""
def get_device(self) -> Optional[torch.device]:
raise NotImplementedError(type(self).__name__)
def storage_size(self) -> int:
raise NotImplementedError(type(self).__name__)
@ir_dataclass
class Layout(OutputSpec):
def __init__(
self,
device: torch.device,
dtype: torch.dtype,
size: List[Expr],
stride: Optional[List[Expr]] = None,
offset: Expr = Integer(0),
) -> None:
if stride is None:
stride = FlexibleLayout.contiguous_strides(size)
self.device = device
self.dtype = dtype
assert len(size) == len(stride), f"size={size}, stride={stride}"
assert all(isinstance(s, (Expr, int)) for s in size)
self.size: List[Expr] = size
self.stride: List[Expr] = stride
self.offset: Expr = offset
def __str__(self) -> str:
offset = ""
if self.offset != 0:
offset = f", offset={self.offset}"
device_index_str = "" if self.device.index is None else f":{self.device.index}"
return (
f"{type(self).__name__}('{self.device.type}{device_index_str}', {self.dtype}, "
f"size={self.size}, stride={self.stride}{offset})"
)
__repr__ = __str__
def get_device(self) -> torch.device:
return self.device
def is_contiguous(self) -> bool:
return is_contiguous_strides_for_shape(self.stride, self.size)
@staticmethod
def is_channels_last_contiguous(
shape: Sequence[_IntLike], strides: Sequence[_IntLike]
) -> bool:
ndim = len(shape)
if ndim not in [4, 5] or shape[1] == 1:
return False
for left, right, size in zip(
strides, make_channels_last_strides_for(shape), shape
):
if size != 1 and left != right:
return False
return True
def is_transposed(self) -> bool:
for left, right, size in zip(
self.stride,
reversed(FlexibleLayout.contiguous_strides(list(reversed(self.size)))),
self.size,
):
if size != 1 and left != right:
return False
return True
def is_stride_ordered(self, order) -> bool: # type: ignore[no-untyped-def]
assert len(self.stride) == len(order)
# ignore dimensions of size 1, they dont affect layout
non_1_indices = [
i
for i, dim in enumerate(self.size)
if V.graph.sizevars.size_hint(dim, fallback=2) != 1
]
stride = [self.stride[i] for i in non_1_indices]
order = [order[i] for i in non_1_indices]
def sorted_indices(arr): # type: ignore[no-untyped-def]
sorted_arr = sorted(arr)
return [sorted_arr.index(element) for element in arr]
# since we may have removed dimensions, need to re-sort & re-index order
order = sorted_indices(order)
# reorder the stride given order
stride_ordered = [-1] * len(order)
for i in range(len(order)):
stride_ordered[order[i]] = stride[i]
# check if it is in ascending order
for i in range(len(order) - 1):
expr = stride_ordered[i] > stride_ordered[i + 1]
if not isinstance(expr, bool):
expr = V.graph._shape_env.evaluate_expr(
stride_ordered[i] > stride_ordered[i + 1], size_oblivious=True
)
if expr:
return False
return True
def is_channels_last_stride_ordered(self): # type: ignore[no-untyped-def]
# create channels_last order(NCHW, NCDHW, the C is the first order).
order = [0] + list(reversed(range(1, len(self.stride) - 1)))
order = [len(order)] + order
return self.is_stride_ordered(order)
@staticmethod
def _pad_strides(in_strides, size, dtype): # type: ignore[no-untyped-def]
"""
The padding does not change stride order but makes sure all strides larger
than the threshold are multiple of align.
"""
align = get_align_for_dtype(dtype)
if len(in_strides) == 0:
return in_strides
if not config.pad_channels_last and Layout.is_channels_last_contiguous(
size, in_strides
):
return in_strides
current_fx_node = V.get_current_node()
if hasattr(current_fx_node, "meta") and current_fx_node.meta.get(
"dislike_padding", False
):
return in_strides
# get_stride_order does not work with dynamic shape. Also we can not
# statically decide if a padding is needed or how much padding we should
# do for dynamic shape.
#
# Skip padding the strides for dynamic shape for now.
if not all(
isinstance(s, (int, sympy.Integer))
for s in itertools.chain(in_strides, size)
):
return in_strides
stride_order = get_stride_order(in_strides)
fill_order = stride_order2fill_order(stride_order)
new_strides = [0 for _ in range(len(in_strides))]
# since we pad when the layout is flexible, we can decide the
# smallest stride to be 1.
new_strides[fill_order[0]] = 1
padded = False
for rank, idx in enumerate(fill_order[1:], start=1):
prev_idx = fill_order[rank - 1]
stride = new_strides[prev_idx] * size[prev_idx]
if stride > config.padding_stride_threshold and stride % align != 0:
stride = ceildiv(stride, align) * align
padded = True
new_strides[idx] = stride
if not padded:
# Consider a tensor with shape [256, 1, 5, 5]
# Avoid strides like [25, 5, 5, 1] being padded to equivalent strides
# [25, 25, 5, 1].
return in_strides
metrics.num_comprehensive_padding += 1
return new_strides
def pad_strides(self): # type: ignore[no-untyped-def]
assert isinstance(self, FlexibleLayout)
assert self.stride is not None
self.stride = self._pad_strides(self.stride, self.size, self.dtype)
def should_pad_strides(self): # type: ignore[no-untyped-def]
return config.comprehensive_padding and isinstance(self, FlexibleLayout)
def as_fixed(self): # type: ignore[no-untyped-def]
if isinstance(self, FixedLayout):
return self
if self.should_pad_strides():
self.pad_strides()
return FixedLayout(
self.device,
self.dtype,
self.size,
self.stride,
self.offset,
)
def make_indexer(self) -> Callable[[Sequence[Expr]], Expr]:
assert (
FlexibleLayout.allow_indexing
), f"convert {type(self).__name__} to FixedLayout first"
return self.as_fixed().make_indexer()
def __eq__(self, other) -> bool: # type: ignore[no-untyped-def]
return (
self.device == other.device
and self.dtype == other.dtype
and self.size == other.size
and self.stride == other.stride
and self.offset == other.offset
)
def storage_size(self) -> sympy.Expr:
return compute_required_storage_length(self.size, self.stride, self.offset)
class FixedLayout(Layout):
"""A Tensor layout we cannot change"""
def make_indexer(self) -> Callable[[Sequence[Expr]], Expr]:
"""A closure containing math to read a given element"""
def indexer(index): # type: ignore[no-untyped-def]
assert len(index) == len(self.stride)
assert len(index) == len(self.size)
result = self.offset
for idx, stride, sz in zip(index, self.stride, self.size):
if sz != 1:
result = result + idx * stride
return result
return indexer
class FlexibleLayout(Layout):
"""A Tensor layout we are allowed to change"""
allow_indexing = False
# WARNING! This doesn't handle zero size tensors correctly
@staticmethod
def contiguous_strides(sizes): # type: ignore[no-untyped-def]
if len(sizes) == 0:
return []
reversed_strides = [sympy.S.One]
for size in reversed(sizes[1:]):
reversed_strides.append(size * reversed_strides[-1])
return list(reversed(reversed_strides))
@staticmethod
def fill_ordered(sizes, order): # type: ignore[no-untyped-def]
"""
Create a stride based on the order the dimensions should be filled in.
In this format, channels last would be:
[1, 3, 2, 0]
"""
assert OrderedSet(range(len(sizes))) == OrderedSet(order), (sizes, order)
next_stride = sympy.S.One
strides = [None] * len(order)
for i in order:
strides[i] = next_stride
next_stride = next_stride * sizes[i]
return strides
@staticmethod
def stride_ordered(sizes, order): # type: ignore[no-untyped-def]
"""
Create a stride based on the sorted order of a permuted range.
In this format, channels last would be:
[3, 0, 2, 1]
"""
assert OrderedSet(range(len(sizes))) == OrderedSet(order)
fill_order = stride_order2fill_order(order)
return FlexibleLayout.fill_ordered(sizes, fill_order)
@staticmethod
def stride_ordered_for_memory_format(sizes, memory_format): # type: ignore[no-untyped-def]
"""
Create a stride based on a memory format.
Memory format is translasted into a stride order,
so channels_last is the same as:
FlexibleLayout.stride_ordered(sizes, [3, 0, 2, 1])
This interface does not support memory_format `torch.preserve_format`
which should be used to deduce a format from another source
"""
if memory_format == torch.channels_last:
return FlexibleLayout.stride_ordered(sizes, NHWC_STRIDE_ORDER)
elif memory_format == torch.channels_last_3d:
return FlexibleLayout.stride_ordered(sizes, NHWDC_STRIDE_ORDER)
elif memory_format == torch.contiguous_format:
return FlexibleLayout.contiguous_strides(sizes)
else:
log.debug(
"stride_ordered_for_memory_format, unsuppored memory_format: %s",
memory_format,
)
raise NotImplementedError
@staticmethod
def same_ordered(sizes, stride): # type: ignore[no-untyped-def]
"""
Create a stride that has the same stride order as given stride
For example, if given stride is [1000, 1, 100, 10],
the fill order should be [1, 3, 2, 0]
"""
assert len(sizes) == len(stride)
stride = [V.graph.sizevars.size_hint(x) for x in stride]
fill_order = sorted(range(len(stride)), key=stride.__getitem__)
return FlexibleLayout.fill_ordered(sizes, fill_order)
def as_stride_order(self, order, allow_padding=False): # type: ignore[no-untyped-def]
new_stride = self.stride_ordered(self.size, order)
if self.should_pad_strides() and allow_padding:
new_stride = self._pad_strides(new_stride, self.size, self.dtype)
return FixedLayout(
self.device,
self.dtype,
self.size,
new_stride,
self.offset,
)
def as_exact_strides(self, exact_strides, allow_padding=False): # type: ignore[no-untyped-def]
new_stride = exact_strides
if self.should_pad_strides() and allow_padding:
new_stride = self._pad_strides(new_stride, self.size, self.dtype)
return FixedLayout(
self.device,
self.dtype,
self.size,
new_stride,
self.offset,
)
def as_fill_order(self, order): # type: ignore[no-untyped-def]
new_stride = self.fill_ordered(self.size, order)
if self.should_pad_strides():
new_stride = self._pad_strides(new_stride, self.size, self.dtype)
return FixedLayout(
self.device,
self.dtype,
self.size,
new_stride,
self.offset,
)
def as_same_order(self, stride): # type: ignore[no-untyped-def]
new_stride = self.same_ordered(self.size, stride)
if self.should_pad_strides():
new_stride = self._pad_strides(new_stride, self.size, self.dtype)
return FixedLayout(
self.device,
self.dtype,
self.size,
new_stride,
self.offset,
)
def __init__(self, device, dtype, size, stride_order=None) -> None: # type: ignore[no-untyped-def]
if stride_order:
strides = FlexibleLayout.fill_ordered(size, stride_order)
else:
strides = FlexibleLayout.contiguous_strides(size)
super().__init__(device, dtype, size, strides)
class NonOwningLayout(Layout):
"""Is a view into the storage of another tensor"""
def __init__(self, view: Union[BaseView, TensorBox]) -> None:
layout = view.get_layout()
super().__init__(
layout.device,
layout.dtype,
layout.size,
layout.stride,
)
self.view = view
def make_indexer(self) -> Callable[[Sequence[Expr]], Expr]:
return self.as_fixed().make_indexer()
def maybe_guard_aligned(self): # type: ignore[no-untyped-def]
offset = self.view.get_layout().offset
if offset == 0:
return True
from .utils import ALIGNMENT
return V.graph.sizevars.statically_known_multiple_of(offset, ALIGNMENT)
class CommBufferType(Enum):
SYMM_MEM = "symm_mem"
class CommBufferLayout(FixedLayout):
"""
A layout that signifies the buffer is a comm buffer.
In terms of striding, the layout is identical to `FixedLayout`.
Buffers with this layout do not participate in in-place reuse - it can be
neither the source nor the target for in-place reuse.
For detailed motivation and usage of this layout, see
NOTE [lowering-time collective optimization].
"""
comm_buffer_type: CommBufferType
group_name: str
def __init__(
self,
layout: FlexibleLayout,
comm_buffer_type: CommBufferType,
group_name: str,
):
if not isinstance(layout, FlexibleLayout):
raise AssertionError(
"A `CommBufferLayout` can only be initialized with "
f"a `FlexibleLayout` (got {layout})."
)
fixed = layout.as_fixed()
super().__init__(
device=fixed.device,
dtype=fixed.dtype,
size=fixed.size,
stride=fixed.stride,
offset=fixed.offset,
)
self.comm_buffer_type = comm_buffer_type
self.group_name = group_name
@ir_dataclass
class NoneLayout(OutputSpec):
# This is janky, I figured out what fields to populate by just running
# the model I was interested in and adding properties/methods as needed.
# This doesn't inherit from Layout because Layout assumes you have stuff
# like sizes, but I don't really have anything here.
#
# If you have an ir.Node with NoneLayout, you probably need to setup
# dependencies manually in scheduler
device: Optional[torch.device]
size: List[int] = dataclasses.field(default_factory=lambda: [0])
stride: List[int] = dataclasses.field(default_factory=lambda: [0])
def storage_size(self) -> int:
return 0
def as_fixed(self): # type: ignore[no-untyped-def]
return self
def get_device(self) -> Optional[torch.device]:
return self.device
class MutationLayoutSHOULDREMOVE(Layout):
def __init__(self, target: IRNode) -> None:
super().__init__(
target.get_device_or_error(),
target.get_dtype(),
target.get_size(), # type: ignore[arg-type]
None,
)
self.target = target
name = self.get_buffer().get_name()
V.graph.mark_buffer_mutated(name)
@property
def stride(self) -> List[Expr]:
return self.real_layout().stride
@stride.setter
def stride(self, value: Never) -> None:
pass # ignore setting of stride
def storage_size(self) -> sympy.Expr:
return self.real_layout().storage_size()
def get_buffer(self) -> Buffer:
def unwrap_views(target): # type: ignore[no-untyped-def]
if isinstance(target, MutationLayoutSHOULDREMOVE):
return unwrap_views(target.target)
if isinstance(target, BaseView):
return unwrap_views(target.unwrap_view())
if isinstance(target, MutableBox):
return unwrap_views(target.data)
return target
result = unwrap_views(self.target)
assert isinstance(
result, Buffer
), "MutationLayoutSHOULDREMOVE must refer to a buffer"
return result
def real_layout(self): # type: ignore[no-untyped-def]
return self.get_buffer().layout
@classmethod
def realize_into(cls, src, dst, unsafe_alias=False): # type: ignore[no-untyped-def]
dst.realize()
# NOTE: We must realize users of `dst` before we realize `src`, since
# realization order determines scheduling order. Otherwise, src's
# mutation would be scheduled before the existing users of dst!
V.graph.mark_buffer_mutated(dst.get_name())
if isinstance(src, TensorBox):
src = src.data
# We copy the contents of src into dst. In most cases this should
# be fused into a single kernel by the scheduler.
# NOTE: We cannot change src's layout to mutate dst directly as this
# would alias src to dst, which is not correct as further mutations to
# dst would effect users of src. However if there are no more users of
# dst, we can alias src to dst.
src.realize_hint()
if not unsafe_alias:
src = Pointwise.create(
device=src.get_device(),
dtype=src.get_dtype(),
inner_fn=src.make_loader(),
ranges=[
V.graph.sizevars.guard_equals(a, b)
for a, b in zip(src.get_size(), dst.get_size())
],
).data
src.realize()
assert isinstance(src.data.layout, FlexibleLayout)
src.data.layout = MutationLayoutSHOULDREMOVE(dst)
return src.data
def as_fixed(self): # type: ignore[no-untyped-def]
return self
def make_indexer(self) -> Callable[[Sequence[Expr]], Expr]:
return self.target.make_indexer()
@ir_dataclass(frozen=False)
class Buffer(IRNode):
# Name is sometimes None; e.g., ForceInPlace, where there isn't
# a meaningful name
name: Optional[str]
layout: OutputSpec
# Multi-output buffers will define 'outputs: List[Buffer]'. Confusingly,
# MultiOutput does NOT define this!
def __post_init__(self) -> None:
super().__post_init__()
self._post_init_setattr("origin_node", None)
def make_indexer(self) -> Callable[[Sequence[Expr]], Expr]:
return self.get_layout().make_indexer()
def get_name(self) -> str:
assert self.name, self
return self.name
def get_device(self) -> Optional[torch.device]:
return self.get_output_spec().get_device()
def get_defining_op(self) -> Optional[Operation]:
return None
@property
def dtype(self) -> torch.dtype:
return self.get_layout().dtype
def get_size(self) -> Sequence[Expr]:
return [*self.get_layout().size]
def get_stride(self) -> List[Expr]:
return [*self.get_layout().stride]
def get_offset(self) -> Expr:
return self.get_layout().offset
def get_layout(self) -> Layout:
if isinstance(self.layout, Layout):
return self.layout
raise NotImplementedError(type(self.layout).__name__)
def get_output_spec(self) -> OutputSpec:
return self.layout
def get_storage_numel(self): # type: ignore[no-untyped-def]
return self.get_numel()
def freeze_layout(self): # type: ignore[no-untyped-def]
if isinstance(self.layout, Layout) and not isinstance(
self.layout, NonOwningLayout
):
self.layout = self.layout.as_fixed()
def freeze_layout_with_stride_order(self, order, allow_padding=False) -> None: # type: ignore[no-untyped-def]
assert isinstance(self.layout, FlexibleLayout)
self.layout = self.layout.as_stride_order(order, allow_padding=allow_padding)
def freeze_layout_with_fill_order(self, order) -> None: # type: ignore[no-untyped-def]
assert isinstance(self.layout, FlexibleLayout)
self.layout = self.layout.as_fill_order(order)
def freeze_layout_with_same_order(self, stride) -> None: # type: ignore[no-untyped-def]
assert isinstance(self.layout, FlexibleLayout)
self.layout = self.layout.as_same_order(stride)
def freeze_layout_with_exact_strides(self, exact_strides, allow_padding=False) -> None: # type: ignore[no-untyped-def]
assert isinstance(self.layout, FlexibleLayout)
self.layout = self.layout.as_exact_strides(
exact_strides, allow_padding=allow_padding
)
def is_zero_elements(self): # type: ignore[no-untyped-def]
return V.graph.sizevars.is_expr_static_and_true(sympy.Eq(self.get_numel(), 0))
def make_loader(self) -> Callable[[Sequence[Expr]], OpsValue]:
# Loading from a zero-element buffer is a no-op
if self.is_zero_elements():
return partial(nop_loader_fn, dtype=self.get_dtype())
def loader(index): # type: ignore[no-untyped-def]
indexer = self.make_indexer()
return ops.load(self.name, indexer(index))
return loader
def codegen_reference(self, writer: Optional[IndentedBuffer] = None) -> str:
return self.get_name()
def decide_layout(self): # type: ignore[no-untyped-def]
pass
def get_inputs_that_alias_output(self) -> Sequence[str]:
if isinstance(self.layout, NonOwningLayout):
return [self.layout.view.get_name()]
return ()
def get_mutation_names(self) -> Sequence[str]:
if isinstance(self.layout, MutationLayoutSHOULDREMOVE):
return [self.layout.target.get_name()]
return ()
def get_read_names(self) -> OrderedSet[str]:
return OrderedSet([self.get_name()])
def get_unbacked_symbol_uses(self) -> OrderedSet[sympy.Symbol]:
return OrderedSet()
def get_unbacked_symbol_defs(self) -> OrderedSet[sympy.Symbol]:
return OrderedSet()
def realize(self) -> Optional[str]:
pass
def should_allocate(self) -> bool:
# Returns False by default.
return False
@ir_dataclass(frozen=False)
class OperationBuffer(Buffer, Operation):
# An operation that produces a single output buffer
def get_outputs(self) -> List[Buffer]:
return [self]
def get_defining_op(self) -> Operation:
return self
# Skip implementation in Buffer
get_operation_name = Operation.get_operation_name
def __post_init__(self) -> None:
Buffer.__post_init__(self)
Operation.__post_init__(self)
class InputBuffer(Buffer):
def num_reads(self) -> int:
return 1
class DonatedBuffer(InputBuffer):
"""
Represents a donated buffer which is a saved tensor that is not alias to any
fwd inputs, fwd user outputs, and bwd outputs. We generally cannot inplace
reuse the input tensor memory during backward since it might be used in another
function. However, donated buffer can be inplace reused during backward
to save memory.
"""
class ConstantBuffer(InputBuffer):
override_device: Optional[torch.device] = None
def make_loader(self) -> Callable[[Sequence[Expr]], OpsValue]:
def loader(index: Sequence[Expr]) -> OpsValue:
indexer = self.get_layout().make_indexer()
return ops.load(
V.graph.constant_name(self.get_name(), self.override_device),
indexer(index),
)
return loader
def constant_to_device(self, device: torch.device) -> IRNode:
return ConstantBuffer(
name=V.graph.constant_name(self.get_name(), device), layout=self.layout
)
@ir_dataclass
class NoneAsConstantBuffer(IRNode):
def get_unbacked_symbol_uses(self) -> OrderedSet[sympy.Symbol]:
return OrderedSet()
def codegen_reference(self, writer: Optional[IndentedBuffer] = None) -> str:
return V.graph.wrapper_code.none_str
def get_output_spec(self) -> OutputSpec:
return NoneLayout(device=None)
def has_tensor_output(self) -> bool:
return False
@ir_dataclass
class ShapeAsConstantBuffer(IRNode):
expr: Expr
def get_unbacked_symbol_uses(self) -> OrderedSet[sympy.Symbol]:
return free_unbacked_symbols(self.expr)
def codegen_reference(self, writer: Optional[IndentedBuffer] = None) -> str:
return V.graph.wrapper_code.codegen_sizevar(self.expr)
def has_tensor_output(self) -> bool:
return False
@ir_dataclass(frozen=False)
class ComputedBuffer(OperationBuffer):
data: Loops
def get_computed_buffer_name(self) -> Optional[str]:
"""
Returns self.name if it exists, otherwise returns the name of the data node if that exists.
If neither exist, returns None.
"""
if self.name is not None:
return self.name
if hasattr(self.data, "name"):
return self.data.name
return None
def num_reads(self) -> int:
return self.data.num_reads()
def get_reads(self) -> OrderedSet[Dep]:
return self.data.get_reads()
def get_read_names(self) -> OrderedSet[str]:
return self.data.get_read_names()
def get_read_writes(self) -> dependencies.ReadWrites:
with patch.object(FlexibleLayout, "allow_indexing", True):
if self.data.get_reduction_type():
return extract_read_writes(
self.get_store_function(),
self.data.get_pointwise_size(), # type: ignore[arg-type]
self.data.get_reduction_size(), # type: ignore[arg-type]
)
else:
return extract_read_writes(
self.get_store_function(),
self.data.get_size(), # type: ignore[arg-type]
)
def get_unbacked_symbol_uses(self) -> OrderedSet[sympy.Symbol]:
# Ordinarily, we'd like to just peek at the arguments list,
# but ComputedBuffers have no argument list.
#
# Morally, this logic needs to be synchronized with the
# KernelArgs.size calls, which are responsible for making symbols make
# there way as kernel arguments (and it is precisely passing in one of
# those symbols that establishes a dependency). However, we haven't
# started codegen yet so we can't directly reuse that logic.
#
# For now, I'm just yoloing with the size of the buffer. Not sure if
# it is enough.
#
# One thing you might wonder is if this is enough for a ComputedBuffer
# denoting a reduction over i0. Empirically, it is enough, but for an
# unusual reason: we only need accurate dependencies for item() call,
# but it's impossible to end up with a reduction over i0 from an
# item() call without a regular non-reduction buffer first.
return (
free_unbacked_symbols(self.get_size())
| free_unbacked_symbols(self.get_stride())
| free_unbacked_symbols(self.get_offset())
| self.data.get_unbacked_symbol_uses()
)
def make_loader(self) -> Callable[[Sequence[Expr]], OpsValue]:
if (
not self.get_reduction_type()
and self.name not in V.graph.mutated_buffers
and self.num_reads() == 0
):
# inline this op rather than generating ops.load()
return self.data.make_loader()
return super().make_loader()
def get_store_function(self) -> Callable[..., OpsValue]:
indexer = self.get_layout().as_fixed().make_indexer()
if isinstance(self.data, (Reduction, Scan, Sort)):
return partial(self.data.store_reduction, self.name, indexer)
else:
assert isinstance(self.data, Pointwise)
return partial(self.data.store_output, self.name, indexer)
def get_fill_order(self) -> Optional[List[int]]:
"""
If our layout is still flexible, try to determine the stride order based on stride orders of reads.
TODO(jansel): A better algorithm here would look at downstream consumers of this
value and try to do global graph-level layout optimization.
This is also something just begging to be autotuned.
"""
if isinstance(self.layout, FlexibleLayout):
(index_vars, reduction_vars), _ = dependencies.index_vars_squeeze(
self.data.get_pointwise_size(), self.data.get_reduction_size()
)
reads = self.get_read_writes().reads
# only consider reads to buffer of same size
# ignore StarDeps because they don't contribute stride information
assert all(
isinstance(r, (dependencies.StarDep, dependencies.MemoryDep))
for r in reads
)
reads = [
sympy_subs(r.index, {v: sympy.S.Zero for v in reduction_vars if v != 0})
for r in reads
if isinstance(r, dependencies.MemoryDep)
]
if reads:
if isinstance(self.data, (Scan, Sort)):
indices = self.data.reindex(index_vars, reduction_vars)
else:
indices = index_vars
stride_lengths = [
V.graph.sizevars.stride_hints(expr, indices) for expr in reads
]
from .scheduler import pick_loop_order
return pick_loop_order(stride_lengths, self.get_size())
return None
def decide_layout(self) -> None:
if isinstance(self.layout, FlexibleLayout):
order = self.get_fill_order()
if order:
self.freeze_layout_with_fill_order(order)
else:
self.freeze_layout()
@cache_on_self
def get_default_sizes_body(
self,
) -> Tuple[
Tuple[List[sympy.Expr], List[sympy.Expr]],
LoopBody,
Tuple[List[sympy.Expr], List[sympy.Expr]],
]:
args, var_ranges = dependencies.index_vars_squeeze(
self.data.get_pointwise_size(), self.data.get_reduction_size(), prefix="q"
)
with patch.object(ConstantBuffer, "override_device", self.get_device()):
body = LoopBody(
self.get_store_function(),
(args if self.get_reduction_type() else args[:1]),
var_ranges,
*args,
)
index_vars = []
reduce_vars: List[Any] = []
index_size = []
reduce_size = []
for v, s in var_ranges.items():
if v in args[0]:
assert not reduce_vars
index_vars.append(v)
index_size.append(s)
else:
assert v in args[1]
reduce_vars.append(v)
reduce_size.append(s)
return (index_size, reduce_size), body, (index_vars, reduce_vars)
def simplify_and_reorder(
self,
extra_indexing_constraints: Optional[Tuple[Dict[Any, Any], List[Any]]] = None,
recompute_sizes_body_func: Optional[Callable[..., Any]] = None,
) -> Tuple[Tuple[List[sympy.Expr], List[sympy.Expr]], LoopBody]:
"""
This is a main place where we do loop transformations in a
backend-agnostic way.
Here we:
1) Remove any 1 dimensions
2) Fuse contiguous dimensions together
3) Reorder dimensions based on stride orders
Optional argument extra_indexing_constraints can be used to append additional
indexing expressions to existing ones derived from buffer's body. This can be useful
to fuse scheduler nodes with compatible ranges, e.g. (s0*s1*...,) and (s0, s1, s2, ...)
on CPU by preventing indexing simplifications and obtaining index/reduce ranges for
the scheduler node compatible with other nodes.
Optional argument recompute_sizes_body_func can be used to recompute sizes and body
on the default body. This can be useful to append additional loop transformations.
"""
(
(index_size, reduce_size),
body,
(index_vars, reduce_vars),
) = self.get_default_sizes_body()
if recompute_sizes_body_func:
(
(index_size, reduce_size),
body,
(index_vars, reduce_vars),
) = recompute_sizes_body_func(
(index_size, reduce_size), body, (index_vars, reduce_vars)
)
index_formulas = [*body.indexing_exprs.values()]
if extra_indexing_constraints is not None:
assert (
isinstance(extra_indexing_constraints, tuple)
and len(extra_indexing_constraints) == 2
)
extra_indexing_ranges, extra_indexing_expr = extra_indexing_constraints
assert isinstance(extra_indexing_ranges, dict)
assert isinstance(extra_indexing_expr, list)
assert all(isinstance(f, Expr) for f in extra_indexing_expr)
expected_var_ranges = body.var_ranges
assert expected_var_ranges == extra_indexing_ranges, (
expected_var_ranges,
extra_indexing_ranges,
)
# remove already existing expressions
extra_indexing_expr = [
e for e in extra_indexing_expr if e not in index_formulas
]
index_formulas += extra_indexing_expr
memory_addrs = [*body.get_write_exprs()]
if not V.graph.has_feature(self, BackendFeature.PREFER_STORE_LOOP_ORDER):
memory_addrs.extend(body.get_read_exprs())
def simplify_and_reorder(x_vars, support_vars, sizes, simplify_loops): # type: ignore[no-untyped-def]
sizes, reindex0, reindex1 = self._apply_loop_reordering(
x_vars, support_vars, sizes, memory_addrs
)
# for NHWC: reindex0([0,1,2,3]) = [0,2,3,1], reindex1([0,1,2,3]) = [0,3,2,1]
x_vars = reindex0(x_vars)
if simplify_loops:
sizes, reindex2, prune = V.graph.sizevars._simplify_loops(
x_vars,
sizes,
index_prevent_reordering(index_formulas, x_vars, sizes),
)
reindex = fuse_reindexing(reindex1, reindex2)
else:
reindex = reindex1
return sizes, reindex, reindex1
support_vars = index_vars + reduce_vars
should_merge_loops = (
not is_gpu(get_device_type(self)) or not config.loop_ordering_after_fusion
)
iter_ranges, iter_reindex, _ = simplify_and_reorder(
index_vars,
support_vars,
index_size,
should_merge_loops,
)
# Like iteration dimensions, we may also want to delay merging reduction dimensions.
# E.g., if we reduce a tensor [M, N, K] for its M and N dimensions followed by a pointwise
# kernel, merging M and N dimension too early makes it hard to decide what loop order
# we should pick for the piontwise kernel so that it is fusible with the reduction.
reduce_ranges, reduce_reindex, _ = simplify_and_reorder(
reduce_vars, support_vars, reduce_size, should_merge_loops
)
# retrace the loop body with simplification and reordering applied
(iter_vars, reduce_vars), var_ranges = dependencies.index_vars_no_squeeze(
iter_ranges,
reduce_ranges,
prefix="p",
)
body = LoopBody(
body,
[iter_reindex(iter_vars), reduce_reindex(reduce_vars)],
var_ranges,
iter_vars,
reduce_vars,
)
return (iter_ranges, reduce_ranges), body
@staticmethod
def _apply_loop_reordering( # type: ignore[no-untyped-def]
index_vars,
support_vars,
sizes,
memory_addrs,
priority_idx=None,
):
"""
Shuffle the order of loops around to hopefully improve performance.
"""
from .scheduler import pick_loop_order
if priority_idx is None:
priority_idx = []
try:
strides = [
V.graph.sizevars.stride_hints(expr, index_vars, support_vars)
for expr in memory_addrs
]
assert len(strides) == len(memory_addrs) and len(strides[0]) == len(
index_vars
)
order = list(reversed(pick_loop_order(strides, sizes, priority_idx)))
except Exception:
if config.debug:
log.warning(
"Did not simplify complex index:\n%s\n%s",
dict(zip(index_vars, sizes)),
memory_addrs,
)
order = list(range(len(sizes)))
sizes = [sizes[i] for i in order]
return sizes, same_reorder(order), inverse_reorder(order)
def get_reduction_size(self) -> Sequence[sympy.Expr]:
return self.data.get_reduction_size()
def get_reduction_type(self) -> Optional[str]:
return self.data.get_reduction_type()
def is_no_op(self) -> bool:
return self.data.is_zero_elements()
def should_allocate(self) -> bool:
return True
def constant_to_device(self, device: torch.device) -> IRNode:
"""Move this to a given device. Requires that all reads are to constants."""
return self.data.constant_to_device(device)
class TemplateBuffer(OperationBuffer):
"""
Represents a Triton (in the future other type) of template operator
that we can fuse an epilogue onto.
"""
def __init__(
self,
layout: Layout,
inputs: List[IRNode],
make_kernel_render: Callable[..., Any],
) -> None:
super().__init__(name=None, layout=layout)
self.inputs = InputsKernel.unwrap_storage(inputs)
self.make_kernel_render = make_kernel_render
self.name = V.graph.register_buffer(self)
V.graph.register_operation(self)
def get_read_writes(self) -> dependencies.ReadWrites:
return self.extract_read_writes(normalize=True)
def extract_read_writes(self, normalize): # type: ignore[no-untyped-def]
name = self.get_name()
indexer = self.get_layout().make_indexer()
def dummy(index, rindex): # type: ignore[no-untyped-def]
assert len(rindex) == 0
return ops.store(name, indexer(index), "fake")
deps = dependencies.extract_read_writes(
dummy, self.get_size(), (), normalize=normalize
)
deps.reads = OrderedSet(dependencies.StarDep(x.get_name()) for x in self.inputs)
return deps
def get_reduction_size(self) -> Sequence[sympy.Expr]:
return sympy.S.One
def get_reduction_type(self) -> Optional[str]:
return None
def should_allocate(self) -> bool:
return True
def simplify_and_reorder( # type: ignore[no-untyped-def]
self,
extra_indexing_constraints: Optional[Tuple[Dict[Any, Any], List[Any]]] = None,
recompute_sizes_body_func: Optional[Callable[..., Any]] = None,
):
return (
(
self.get_size(),
(),
),
None,
)
class TritonTemplateBuffer(TemplateBuffer):
def __init__( # type: ignore[no-untyped-def]
self,
layout,
inputs,
make_kernel_render,
mutated_inputs: Optional[Iterable[IRNode]] = None,
) -> None:
"""
NOTE:[TritonTemplates with multiple outputs]
We want the ability for TritonTemplates to output multiple tensors. Triton
kernels have no notion of outputs and this is done by creating tensors that
are then mutated by the kernel. Currenlty our STORE_OUTPUT codegen doesn't
support creating multinode outputs for triton templates.
We work around this by creating an extra input buffer during the lowering
and we mark them as mutated inputs.
"""
super().__init__(layout, inputs, make_kernel_render)
self.mutated_inputs = mutated_inputs
self.outputs: List[Buffer] = [self]
if mutated_inputs is not None:
# Ensure that the mutated inputs are only allowed for certain nodes
allowed_set = (
torch.ops.higher_order.flex_attention,
torch.ops.higher_order.flex_attention_backward,
)
current_node = V.graph.current_node.target
assert (
current_node in allowed_set
), f"Mutated inputs are only allowed for {allowed_set} but got {current_node}"
device = self.inputs[0].get_device()
self.outputs += [
MutationOutput(NoneLayout(device=device), buf, self)
for buf in mutated_inputs
]
def get_outputs(self) -> List[Buffer]:
return self.outputs
def __str__(self) -> str:
out = f"TritonTemplateBuffer(layout={self.layout})"
return out
PrimitiveInfoType = Union[int, float, bool, str, List[Union[int, str, float, bool]]]
class ChoiceCaller:
"""
Represents a possible choice used in autotune_process.py.
During autotuning, self.benchmark() is first called to get benchmark result,
and if this choice is selected, self.output_node() is called to get the output_node.
Children classes: TritonTemplateCaller, CUDATemplateCaller.
"""
def __init__(
self,
name: str,
input_nodes: List[Buffer],
layout: Layout,
description: str,
) -> None:
super().__init__()
self.name = name
self.layout = layout
self.input_nodes = input_nodes
# An additional description used to describe the choice (useful for
# knowing what autotuning is choosing)
self.description = description
def benchmark(self, *args, out) -> float: # type: ignore[no-untyped-def]
algo = self.to_callable()
return benchmarker.benchmark(algo, args, {"out": out})
def call_name(self) -> str:
raise NotImplementedError
def to_callable(self): # type: ignore[no-untyped-def]
raise NotImplementedError
def hash_key(self) -> str:
raise NotImplementedError
def output_node(self) -> TensorBox:
raise NotImplementedError
def info_dict(self) -> Dict[str, Union[PrimitiveInfoType, List[PrimitiveInfoType]]]:
"""Information returned here is logged to the autotune log file when that is enabled."""
return {}
def autoheuristic_id(self) -> str:
return "unsupported_choice"
class TritonTemplateCallerBase(ChoiceCaller):
def get_make_kernel_render(self) -> Any:
raise NotImplementedError
class MultiTemplateBuffer(TritonTemplateBuffer):
"""
Represents a Buffer with multiple backing implementation choices.
Choices can be TritonTemplates or ExternKernels. During scheduling if there is a potential
epilogue we will benchmark each of the choices with the epilogue to determine an implementation.
Otherwise, the fastest base choice will be chosen.
"""
def __init__(
self,
layout: Layout,
inputs: List[IRNode],
choice_timings: Callable[[], Dict[ChoiceCaller, float]],
unfiltered_choices: List[ChoiceCaller],
) -> None:
super().__init__(layout=layout, inputs=inputs, make_kernel_render=None)
self._choice_timings_fn = choice_timings
self._choice_timings: Optional[Dict[ChoiceCaller, float]] = None
self.original_inputs = inputs
self._output_plannable = all(
isinstance(choice, TritonTemplateCallerBase)
or (
isinstance(choice, torch._inductor.select_algorithm.ExternKernelCaller)
and choice.has_out_variant
)
for choice in unfiltered_choices
)
@property
def output_plannable(self) -> bool:
"""
Are all possible choices TritonTemplates or Extern Kernels with out variants
"""
return self._output_plannable
@property
def choice_timings(self) -> Dict[ChoiceCaller, float]:
if self._choice_timings is None:
self._choice_timings = self._choice_timings_fn()
return self._choice_timings
@contextlib.contextmanager
def swap_as_triton_caller(self, caller: TritonTemplateCallerBase): # type: ignore[no-untyped-def]
assert isinstance(caller, torch._inductor.select_algorithm.TritonTemplateCaller)
assert self.layout == caller.layout
render = self.make_kernel_render
self.make_kernel_render = caller.get_make_kernel_render()
try:
yield
finally:
self.make_kernel_render = render
def finalize_as_triton_caller(self, caller: TritonTemplateCallerBase) -> None:
assert isinstance(caller, torch._inductor.select_algorithm.TritonTemplateCaller)
assert self.get_size() == caller.layout.size
assert self.get_stride() == caller.layout.stride
self.make_kernel_render = caller.get_make_kernel_render()
def get_min_choice(self) -> Tuple[ChoiceCaller, float]:
min_choice = min(self.choice_timings, key=self.choice_timings.get) # type: ignore[arg-type]
return (min_choice, self.choice_timings[min_choice])
class CUDATemplateBuffer(TemplateBuffer):
def __init__( # type: ignore[no-untyped-def]
self,
layout,
inputs,
make_kernel_render,
workspace_size: int,
template: CUDATemplate,
) -> None:
super().__init__(layout, inputs, make_kernel_render)
# Global memory (in bytes) needed for this template.
self.workspace_size = workspace_size
self.template = template
def get_workspace_size(self): # type: ignore[no-untyped-def]
return self.workspace_size if self.workspace_size is not None else 0
class CppTemplateBuffer(TemplateBuffer):
def __init__(self, layout, inputs, make_kernel_render, template, choice) -> None: # type: ignore[no-untyped-def]
super().__init__(layout, inputs, make_kernel_render)
self.template = template
self.choice = choice
@ir_dataclass(frozen=False)
class InputsKernel(OperationBuffer):
inputs: List[Buffer]
def get_read_writes(self) -> dependencies.ReadWrites:
reads: OrderedSet[dependencies.Dep] = OrderedSet()
StarDep = dependencies.StarDep
for input in self.inputs:
if isinstance(input, list):
reads.update(StarDep(x.get_name()) for x in input)
elif isinstance(input, ShapeAsConstantBuffer):
# Skip creating dependncy for symbolics as they're visible globally
continue
else:
reads.add(StarDep(input.get_name()))
writes: OrderedSet[dependencies.Dep] = OrderedSet(
StarDep(buf.get_name()) for buf in self.get_outputs()
)
return dependencies.ReadWrites(
reads=reads,
writes=writes,
index_exprs=OrderedSet(),
)
def get_reads(self) -> OrderedSet[Dep]:
return self.get_read_writes().reads
@classmethod
def unwrap_storage_for_input(cls, x: IRNode) -> IRNode:
if isinstance(x, TensorBox):
x = x.data
if isinstance(x, StorageBox):
x = x.data
if isinstance(x, BaseView) and not isinstance(x, ReinterpretView):
x = ExternKernel.realize_input(x)
if isinstance(x, TensorBox):
# when converting to ReinterpretView fails in the
# realize_input call above, the result will be wrapped
# into TensorBox / StorageBox pair as a result of the
# cls.copy_input call; so we should unwrap recursively
return cls.unwrap_storage_for_input(x)
if isinstance(x, TorchBindObject):
return x
assert isinstance(x, (Buffer, ReinterpretView)), x
return x
@staticmethod
def unwrap_storage(inputs): # type: ignore[no-untyped-def]
inputs_new = []
for x in inputs:
if isinstance(x, list):
x = [InputsKernel.unwrap_storage_for_input(i) for i in x]
else:
x = InputsKernel.unwrap_storage_for_input(x)
inputs_new.append(x)
return inputs_new
def is_extern(self) -> bool:
return True
def num_reads(self) -> int:
return 1
class NopKernel(InputsKernel):
def is_no_op(self) -> bool:
return True
def get_reads(self) -> OrderedSet[Dep]:
return OrderedSet()
class ConcatKernel(NopKernel):
"""
There isn't actually a real kernel for concat, we just change the
storage for the upstream data.
"""
@classmethod
def create(cls, inputs, dim): # type: ignore[no-untyped-def]
device = inputs[0].get_device()
dtype = inputs[0].get_dtype()
new_size = list(inputs[0].get_size())
offsets_start = [0]
offsets_end = [new_size[dim]]
assert 0 <= dim < len(new_size)
for i in range(1, len(inputs)):
input_size = inputs[i].get_size()
offsets_start.append(new_size[dim])
assert len(input_size) == len(new_size)
assert inputs[i].get_dtype() == dtype
assert inputs[i].get_device() == device
for j in range(len(new_size)):
if j == dim:
new_size[j] = new_size[j] + input_size[j]
else:
new_size[j] = V.graph.sizevars.guard_equals(
new_size[j], input_size[j]
)
offsets_end.append(new_size[dim])
output_stride = FlexibleLayout.contiguous_strides(new_size)
# If any of the inputs is in CL format, use CL format for the output
for i in range(len(inputs)):
x = inputs[i]
if is_storage_and_layout(x):
layout = x.get_layout()
if isinstance(
layout, FixedLayout
) and Layout.is_channels_last_contiguous(layout.size, layout.stride):
# use CL stride for the output
output_stride = make_channels_last_strides_for(new_size)
break
any_input_is_storage_and_layout = any(is_storage_and_layout(x) for x in inputs)
fx_node_args = V.graph.current_node.args[0]
assert isinstance(fx_node_args, list)
# If any of the inputs has meta tensor and the meta tensor is in CL format, use CL format for the output
if any_input_is_storage_and_layout is False and any(
"val" in arg.meta
and (
arg.meta["val"].is_contiguous(memory_format=torch.channels_last)
or arg.meta["val"].is_contiguous(memory_format=torch.channels_last_3d)
)
for arg in fx_node_args
):
output_stride = make_channels_last_strides_for(new_size)
concat_kernel = ConcatKernel(
name=None,
layout=FixedLayout(
device=device,
dtype=dtype,
size=new_size,
stride=output_stride,
),
inputs=[],
)
kernel = StorageBox(concat_kernel)
op_names = []
for i in range(len(inputs)):
input_buffer = cls.realize_into(
inputs[i],
SliceView.create(
kernel, dim, offsets_start[i], offsets_end[i], clamp=False
),
)
concat_kernel.inputs.append(input_buffer)
if isinstance(inputs[i].data, BaseView):
input_unwrapped = inputs[i].data.unwrap_view()
else:
input_unwrapped = inputs[i].data
if (
input_unwrapped.is_input_buffer()
and is_gpu(inputs[i].get_device().type)
and not is_dynamic(input_buffer)
):
op_names.append(input_buffer.get_operation_name())
if len(op_names) > 1 and V.graph.has_feature(device, BackendFeature.FOREACH):
V.graph.register_operation_list(op_names)
concat_kernel.name = V.graph.register_buffer(concat_kernel)
concat_kernel.inputs = cls.unwrap_storage(concat_kernel.inputs)
V.graph.register_operation(concat_kernel)
return kernel
@classmethod
def can_realize_into_without_copy(cls, src, dst=None): # type: ignore[no-untyped-def]
if isinstance(src, TensorBox):
# unwrap a TensorBox
return cls.can_realize_into_without_copy(src.data, dst)
if isinstance(src.data, MultiTemplateBuffer):
if (
not isinstance(src.data.layout, FixedLayout)
or not src.data.output_plannable
):
return False
# we call can_realize_into_without_copy in cat lowering before we've decided
# on output format, optimistically assume layout matches
if dst is None:
return True
# otherwise, check equality of layouts
if not len(src.get_stride()) == len(dst.get_stride()):
return False
return all(
V.graph.sizevars.statically_known_equals(s1, s2)
for s1, s2 in zip(src.get_stride(), dst.get_stride())
)
return isinstance(src.data.layout, FlexibleLayout) and not isinstance(
src.data, ExternKernelAlloc
)
@classmethod
def realize_into(cls, src, dst): # type: ignore[no-untyped-def]
# Attempt to turn this into a ReinterpretView rather than assert.
# This has concessions around layout, as as_storage_and_layout
# can cause us to go from flexible to fixed layout.
if not isinstance(dst, ReinterpretView):
if is_storage_and_layout(dst):
storage, layout = as_storage_and_layout(dst)
dst = ReinterpretView(data=storage, layout=layout)
assert isinstance(dst, ReinterpretView), dst
if isinstance(src, TensorBox):
# unwrap a TensorBox
return cls.realize_into(src.data, dst)
if isinstance(src, StorageBox):
src.realize()
# ExternKernelAlloc has specific requirements for output layout, should create a copy
assert hasattr(src.data, "layout")
if cls.can_realize_into_without_copy(src, dst):
src.data.layout = NonOwningLayout(dst)
return src.data
# introduce a copy
pw = Pointwise.create(
device=src.get_device(),
dtype=src.get_dtype(),
inner_fn=src.make_loader(),
ranges=[
V.graph.sizevars.guard_equals(a, b)
for a, b in zip(src.get_size(), dst.get_size())
],
)
return cls.realize_into(pw, dst)
def should_allocate(self) -> bool:
return True
@ir_dataclass(frozen=False)
class ExternKernel(InputsKernel):
constant_args: Tuple[Any, ...] = ()
kwargs: Dict[str, Any] = dataclasses.field(default_factory=dict)
output_view: Optional[ReinterpretView] = None
python_kernel_name: Optional[str] = None
cpp_kernel_name: Optional[str] = None
# FIXME: in some cases we sill need to explicitly pass in ordered_kwargs_for_cpp_kernel
# We shouldn't need to do this since the information can be retrieved from op_overload._schema.
ordered_kwargs_for_cpp_kernel: Iterable[str] = dataclasses.field(
default_factory=list
)
op_overload: Optional[
Union[torch._ops.OpOverload, torch._ops.HigherOrderOperator]
] = None
arg_properties: Optional[List[Dict[str, Any]]] = None
kwarg_properties: Optional[Dict[str, Dict[str, Any]]] = None
unbacked_bindings: Dict[sympy.Symbol, pytree.KeyPath] = dataclasses.field(
default_factory=dict
)
mutation_outputs: List[MutationOutput] = dataclasses.field(default_factory=list)
def __init__( # type: ignore[no-untyped-def]
self,
name,
layout,
inputs,
constant_args=(),
kwargs=None,
output_view=None,
python_kernel_name=None,
cpp_kernel_name=None,
ordered_kwargs_for_cpp_kernel=(),
op_overload=None,
) -> None:
super().__init__(
name=name,
layout=layout,
inputs=inputs,
)
self.constant_args = constant_args
self.kwargs = kwargs if kwargs else {}
self.output_view = output_view
self.op_overload = op_overload
self.set_cpp_kernel_name(cpp_kernel_name)
self.set_python_kernel_name(python_kernel_name)
self.ordered_kwargs_for_cpp_kernel = ordered_kwargs_for_cpp_kernel
self.collect_arg_kwarg_properties()
self.unbacked_bindings = {}
self.mutation_outputs = []
self.fx_node = V.graph.current_node
def get_outputs(self) -> List[Buffer]:
return [self, *self.mutation_outputs]
def get_unbacked_symbol_defs(self) -> OrderedSet[sympy.Symbol]:
return OrderedSet()
def collect_arg_kwarg_properties(self): # type: ignore[no-untyped-def]
# if self.op_overload is torch._ops.OpOverload, we can use its schema to collect additional
# information for args and kwargs, e.g. type and default value, to help with the cpp wrapper codegen
self.arg_properties = (
[
{
"name": x.name,
"type": x.real_type,
"default_value": x.default_value,
}
for x in self.op_overload._schema.arguments
if not x.kwarg_only
]
if isinstance(self.op_overload, torch._ops.OpOverload)
else [{} for i in range(len(self.inputs))]
)
self.allarg_properties = (
{
x.name: {"type": x.real_type, "default_value": x.default_value}
for x in self.op_overload._schema.arguments
}
if isinstance(self.op_overload, torch._ops.OpOverload)
else {}
)
# FIXME: self.kwargs does not always match kwargs defined in schema, so sometimes
# ordered_kwargs_for_cpp_kernel is explicilty passed in.
if isinstance(self.op_overload, torch._ops.OpOverload):
if not self.ordered_kwargs_for_cpp_kernel:
self.ordered_kwargs_for_cpp_kernel = [
x.name for x in self.op_overload._schema.arguments if x.kwarg_only
]
self.schema_kwargs = [
x for x in self.op_overload._schema.arguments if x.kwarg_only
]
def decide_layout(self): # type: ignore[no-untyped-def]
if isinstance(self.layout, FlexibleLayout):
self.apply_constraint()
self.freeze_layout()
def codegen_comment(self, wrapper) -> None: # type: ignore[no-untyped-def]
origin_str, detailed_origin_str = get_kernel_metadata(self, wrapper)
if origin_str:
wrapper.writeline(origin_str)
def codegen(self, wrapper): # type: ignore[no-untyped-def]
raise NotImplementedError
def set_cpp_kernel_name(self, cpp_kernel_name: Optional[str] = None) -> None:
self.cpp_kernel_name = cpp_kernel_name
if not V.graph.cpp_wrapper or not isinstance(
self.op_overload, torch._ops.OpOverload
):
return
kernel = self.op_overload
if self.cpp_kernel_name is None:
# Try to construct cpp_kernel_name from op_overload
if kernel.namespace == "aten":
# Calling with the default kernel name can lead to ambiguous behavior like the following example.
# repeat_interleave(const at::Tensor & repeats, std::optional<int64_t> output_size=std::nullopt)
# repeat_interleave(const at::Tensor & self, int64_t repeats,
# std::optional<int64_t> dim=std::nullopt, std::optional<int64_t> output_size=std::nullopt)
opname = (
kernel.__name__.split(".")[0]
if kernel._overloadname == "default"
else kernel.__name__.replace(".", "_")
)
self.cpp_kernel_name = f"at::_ops::{opname}::call"
else:
self.cpp_kernel_name = kernel._schema.name
def set_python_kernel_name(self, python_kernel_name: Optional[str]) -> None:
self.python_kernel_name = python_kernel_name
if python_kernel_name is not None:
return
kernel = self.op_overload
if kernel is None:
pass
elif isinstance(kernel, torch._ops.HigherOrderOperator):
self.python_kernel_name = f"torch.ops.higher_order.{kernel.__name__}"
else:
self.python_kernel_name = (
f"{kernel.__module__.replace('._ops.', '.ops.')}.{kernel.__name__}"
)
def get_kernel_name(self): # type: ignore[no-untyped-def]
return (
V.graph.wrapper_code.get_c_shim_func_name(self.cpp_kernel_name) # type: ignore[attr-defined]
if V.graph.cpp_wrapper
else self.python_kernel_name
)
@staticmethod
def copy_input(x): # type: ignore[no-untyped-def]
pw = Pointwise.create(
device=x.get_device(),
dtype=x.get_dtype(),
inner_fn=x.make_loader(),
ranges=x.get_size(),
origin_node=x.get_origin_node(),
traceback=x.get_traceback(),
)
pw.realize()
return pw
@classmethod
def process_kernel( # type: ignore[no-untyped-def]
cls, kernel, *args, **kwargs
) -> Tuple[
Any,
List[Any],
List[Any],
Callable[[Any, Any], Any],
Optional[Dict[sympy.Symbol, pytree.KeyPath]],
]:
binded_args = {"args": args, "kwargs": kwargs}
args_flat, args_spec = pytree.tree_flatten(binded_args)
is_arg_tensor = []
tensor_args = []
non_tensor_args: List[Any] = []
for arg in args_flat:
is_arg_tensor.append(isinstance(arg, IRNode))
if is_arg_tensor[-1]:
tensor_args.append(arg)
else:
if isinstance(arg, sympy.Expr):
arg = V.graph.sizevars.shape_env.create_symintnode(arg, hint=None)
non_tensor_args.append(arg)
def unflatten_args(new_tensor_args, new_non_tensor_args): # type: ignore[no-untyped-def]
result = []
it_tensors = iter(new_tensor_args)
it_non_tensors = iter(new_non_tensor_args)
for is_tensor in is_arg_tensor:
if is_tensor:
result.append(next(it_tensors))
else:
result.append(next(it_non_tensors))
r = pytree.tree_unflatten(result, args_spec)
return r.get("args", []), r.get("kwargs", {})
tensor_args = [cls.realize_input(x) for x in tensor_args]
# freeze layout otherwise our output stride calculation might
# become incorrect
for x in tensor_args:
if is_storage_and_layout(x):
as_storage_and_layout(x, freeze=True)
# Rerun fake tensor propagation, because Inductor may have changed the
# strides of inputs and we need to determine accurately what the
# output stride will be.
example_args: List[Union[torch.Tensor, torch._C.ScriptObject]] = []
# We need to retain the constant values of fake tensors that we originally
# propagated the graph with, because for some operators running without a
# constant would trigger an error / DataDependentException
for x in tensor_args:
# if x is a view of a constant, we need to realize the view
# (we can't pass the constant into the kernel directly)
if not isinstance(x, BaseView) and x.get_name() in V.graph.constants:
example_args.append(V.graph.constants[x.get_name()])
elif (
not isinstance(x, BaseView)
and x.get_name() in V.graph.torchbind_constants
):
example_args.append(V.graph.torchbind_constants[x.get_name()])
else:
example_args.append(ir_node_to_tensor(x, guard_shape=True))
new_args, new_kwargs = unflatten_args(example_args, non_tensor_args)
example_output = kernel(*new_args, **new_kwargs)
unbacked_bindings: Optional[Dict[sympy.Symbol, pytree.KeyPath]] = None
if shape_env := V.fake_mode.shape_env:
rebind_unbacked(shape_env, V.current_node, example_output)
unbacked_bindings = compute_unbacked_bindings(
shape_env, example_output, V.current_node.meta.get("val")
)
example_out_li = (
[example_output]
if not isinstance(example_output, (list, tuple))
else example_output
)
for t in example_out_li:
if isinstance(t, torch.Tensor) and t.is_sparse:
msg = "sparsity not handled. Please file issue for sparse inference weights."
if stack_trace := V.graph.current_node.meta.get("stack_trace", None):
msg = f"{msg} Found from : \n {stack_trace}"
V.graph.disable_cudagraphs_reason = msg
return (
example_output,
tensor_args,
non_tensor_args,
unflatten_args,
unbacked_bindings,
)
@classmethod
def convert_to_reinterpret_view(cls, x): # type: ignore[no-untyped-def]
"""
In order to pass this to an extern kernel we need a
ReinterpretView not a View. This allows us to avoid some
unneeded copies.
"""
assert isinstance(x, BaseView)
if isinstance(x, ReinterpretView):
return x
# NOTE: Don't use extract_read_writes here as it fails when
# make_loader() inlines the computation
x_unwrap_view = x.unwrap_view()
buf = V.graph.get_buffer(x_unwrap_view.get_name())
assert buf is not None
x_unwrap_view_fx_node = buf.get_origin_node()
# Prefer channels last format according to how the format is set from eager.
if (
x_unwrap_view_fx_node is not None
and "val" in x_unwrap_view_fx_node.meta
and isinstance(x_unwrap_view.layout, FlexibleLayout)
and (
x_unwrap_view_fx_node.meta["val"].is_contiguous(
memory_format=torch.channels_last
)
or x_unwrap_view_fx_node.meta["val"].is_contiguous(
memory_format=torch.channels_last_3d
)
)
):
x_unwrap_view.freeze_layout_with_same_order(
make_channels_last_strides_for(x_unwrap_view.get_size())
)
else:
x_unwrap_view.freeze_layout()
index_args, var_ranges = dependencies.index_vars_squeeze(
x.get_size(), prefix="r" # type: ignore[arg-type]
)
range_vars = index_args[0]
index = x.make_indexer()(range_vars)
index = V.graph.sizevars.simplify_with_ranges(index, var_ranges)
strides = V.graph.sizevars.stride_vars(index, range_vars)
offset = V.graph.sizevars.offset_var(index, range_vars)
expected = sympy_dot(range_vars, strides) + offset
if index != expected:
log.debug(
"convert_to_reinterpret_view failed: stride=%s offset=%s index=%s",
strides,
offset,
index,
)
raise NotImplementedError
return ReinterpretView(
data=x.data,
layout=FixedLayout(
device=x.get_device_or_error(),
dtype=x.get_dtype(),
size=x.get_size(), # type: ignore[arg-type]
stride=strides,
offset=offset,
),
)
@classmethod
def realize_input(cls, x): # type: ignore[no-untyped-def]
if x is None:
return NoneAsConstantBuffer()
if isinstance(x, (sympy.Expr, sympy.logic.boolalg.Boolean, int)):
return ShapeAsConstantBuffer(expr=x)
if isinstance(x, Constant):
return V.graph.add_tensor_constant(
torch.tensor(x.value, dtype=x.get_dtype(), device=x.get_device())
)
if isinstance(x, ConstantBuffer):
return x
if isinstance(x, TensorBox):
return cls.realize_input(x.data)
if isinstance(x, ReinterpretView):
return ReinterpretView(
data=cls.realize_input(x.data), layout=x.get_layout()
)
if isinstance(x, BaseView):
x.realize()
if is_storage_and_layout(x.unwrap_view()):
try:
return cls.convert_to_reinterpret_view(x)
except NotImplementedError:
pass
if isinstance(x, StorageBox):
# TODO(jansel): impose layout preference on realized buffer
x.realize()
return x
if isinstance(x, TorchBindObject):
return x
return cls.copy_input(x)
@classmethod
def require_stride1(cls, x): # type: ignore[no-untyped-def]
if is_storage_and_layout(x):
if len(x.get_stride()) == 0:
return x
for stride in x.get_stride():
if stride == 1:
return x
return cls.copy_input(x)
@classmethod
def require_strides( # type: ignore[no-untyped-def]
cls,
x,
order: Optional[Sequence[int]] = None,
exact_strides: Optional[Sequence[_IntLike]] = None,
allow_padding=False,
):
assert order is not None or exact_strides is not None
if x.get_numel() in (0, 1): # Layout doesn't matter
return x
# require x to have the layout
if is_storage_and_layout(x):
while isinstance(x.get_layout(), NonOwningLayout):
x = x.get_layout().view
if isinstance(x.get_layout(), FlexibleLayout):
if order:
# If the the FlexibleLayout already has the size and stride in the required order,
# freeze it to a FixedLayout by using its current size and stride.
# The behavior of using its current size and stride or the given order can be different
# if the size and stride has ambiguilty, for example for a 4D input where the iC = 1:
# size=[s0, 1, 28, 28], stride=[784, 784, 28, 1]. If the required order is [3, 0, 2, 1] (channels last),
# the current size and stride already satisfies this order.
# However by freezing it to the required order, the layout will be changed to:
# size=[s0, 1, 28, 28], stride=[784, 1, 28, 1]), which is not actually necessary.
# fix flexiblelayout to be FixedLayout with stride_order
as_storage_and_layout(
x,
freeze=True,
want_contiguous=False,
stride_order=(
get_stride_order(
V.graph.sizevars.size_hints(x.get_layout().stride)
)
if is_stride_order_storage_and_layout(x, order)
else order
),
allow_padding=allow_padding,
)
return x
else:
# If the exact_strides is given, freeze the FlexibleLayout to a FixedLayout with the exact_strides.
as_storage_and_layout(
x,
freeze=True,
want_contiguous=False,
stride_order=None,
allow_padding=allow_padding,
exact_strides=exact_strides,
)
return x
elif isinstance(x.get_layout(), FixedLayout) and (
(order and x.get_layout().is_stride_ordered(order))
or (
exact_strides
and significant_strides_equal(
exact_strides, x.get_layout().stride, x.get_size()
)
)
):
return x
elif isinstance(x.get_layout(), MutationLayoutSHOULDREMOVE):
if isinstance(x.get_layout().real_layout(), FlexibleLayout):
raise AssertionError(
"the MutationLayoutSHOULDREMOVE's real layout shouldn't be FlexibleLayout"
)
elif isinstance(x.get_layout().real_layout(), FixedLayout) and (
(order and x.get_layout().real_layout().is_stride_ordered(order))
or (
exact_strides
and significant_strides_equal(
exact_strides,
x.get_layout().real_layout().stride,
x.get_size(),
)
)
):
return x
# TODO - Storage to InputBuffer
if isinstance(x, InputBuffer) and (
(order and x.get_layout().is_stride_ordered(order))
or (
exact_strides
and significant_strides_equal(
exact_strides, x.get_layout().stride, x.get_size()
)
)
):
return x
if (
isinstance(x, TensorBox)
and isinstance(x.data, BaseView)
and not isinstance(x.data, ReinterpretView)
and is_storage_and_layout(x.unwrap_view())
and not isinstance(x.unwrap_view().data, ExternKernelAlloc) # type: ignore[attr-defined]
):
try:
x.data = cls.convert_to_reinterpret_view(x.data)
if order:
return cls.require_stride_order(
x, order, allow_padding=allow_padding
)
elif exact_strides:
return cls.require_exact_strides(
x, exact_strides, allow_padding=allow_padding
)
except NotImplementedError:
pass
# Although this is a clone, inductor is good about fusing clones into previous
# operations if they weren't realized and their layouts were flexible.
x = cls.copy_input(x)
as_storage_and_layout(
x,
freeze=True,
want_contiguous=False,
stride_order=order,
allow_padding=allow_padding,
exact_strides=exact_strides,
)
if order:
assert is_stride_order_storage_and_layout(x, order)
return x
@classmethod
def require_exact_strides(cls, x, exact_strides, allow_padding=False): # type: ignore[no-untyped-def]
return cls.require_strides(
x, exact_strides=exact_strides, allow_padding=allow_padding
)
@classmethod
def require_stride_order(cls, x, order, allow_padding=False): # type: ignore[no-untyped-def]
return cls.require_strides(x, order=order, allow_padding=allow_padding)
@classmethod
def require_channels_last(cls, x): # type: ignore[no-untyped-def]
return cls.require_stride_order(x, NHWC_STRIDE_ORDER)
@classmethod
def require_channels_last_3d(cls, x): # type: ignore[no-untyped-def]
return cls.require_stride_order(x, NHWDC_STRIDE_ORDER)
@classmethod
def require_contiguous(cls, x): # type: ignore[no-untyped-def]
return cls.require_stride_order(x, list(reversed(range(len(x.get_size())))))
def apply_constraint(self) -> None:
pass
def fill_non_provided_args(self, args, kwargs): # type: ignore[no-untyped-def]
# Previously, we want to maintain forward-compatibility by skipping
# default args in the serialized artifacts in fbcode. However,
# some of our shim interfaces require default values being OrderedSet.
# Discussed with Sherlock offline and we decided to allow serializing
# default args into the C++ wrapper code for now. We will refine this
# part if we see real FC requirement. More details related to FC
# can be found at:
# https://docs.google.com/document/d/1FzWm-sHYwmRi3x_g036kOxd99KaYquUsA-L5JwOn8ys/edit?usp=sharing
assert isinstance(args, (list, tuple))
if isinstance(args, tuple):
args = list(args)
assert self.arg_properties, "ExternKernel.arg_properties should not be empty"
n_args = len(args)
n_pos_args = len(self.arg_properties)
# For cpp wrapper, if some positional args are not provided, we need to check
# if they're in the kwargs or use their default value
if n_args < n_pos_args:
log.debug(
"%s has %d unprovided positional arguments. "
"Will check if they are in the keyword arguments or will use default values.",
self.op_overload,
n_pos_args - n_args,
)
for i in range(n_args, n_pos_args):
arg_name = self.arg_properties[i]["name"]
args.append(
kwargs[arg_name]
if arg_name in kwargs
else self.arg_properties[i]["default_value"]
)
return args
def codegen_const_args(self, names: Optional[List[str]] = None): # type: ignore[no-untyped-def]
if V.graph.cpp_wrapper:
result = []
# Aten ops follow the convention that tensor args are before non-tensor args,
# in which case the following 'len(self.inputs) + i' logic works. But this
# may not be true for other ops, and if that is the case, caller needs to
# pass in a list of const arg names for arg_properties lookup.
name_to_arg_properties = None
if names and self.arg_properties:
assert len(self.constant_args) == len(
names
), "names passed to codegen_const_args does not match self.constant_args"
name_to_arg_properties = {
arg.get("name"): arg for arg in self.arg_properties
}
for i, x in enumerate(self.constant_args):
if name_to_arg_properties is not None:
prop = name_to_arg_properties.get(names[i]) # type: ignore[index]
type_ = prop.get("type") if prop else None
else:
idx = len(self.inputs) + i
type_ = (
self.arg_properties[idx].get("type")
if self.arg_properties and idx < len(self.arg_properties)
else None
)
result.append(V.graph.wrapper_code.val_to_arg_str(x, type_))
return result
else:
return map(V.graph.wrapper_code.val_to_arg_str, self.constant_args)
def codegen_args(self): # type: ignore[no-untyped-def]
if V.graph.cpp_wrapper and self.op_overload is not None:
# cpp wrapper needs special logic to fill in missing args with default values
inputs = self.fill_non_provided_args(
[*self.inputs, *self.constant_args], self.kwargs
)
# fill_non_provided_args has handled constant args, so no need to codegen for that later
need_codegen_constant_args = False
else:
inputs = self.inputs
need_codegen_constant_args = True
args = []
for i, x in enumerate(inputs):
if V.graph.cpp_wrapper:
assert self.arg_properties and i < len(
self.arg_properties
), "Invalid access to ExternKernel.arg_properties"
type_ = self.arg_properties[i].get("type")
args.append(V.graph.wrapper_code.val_to_arg_str(x, type_))
else:
args.append(V.graph.wrapper_code.val_to_arg_str(x))
if need_codegen_constant_args:
args.extend(self.codegen_const_args())
return args
def get_kwargs_value(self, arg_name): # type: ignore[no-untyped-def]
if arg_name in self.kwargs:
return self.kwargs.get(arg_name)
if self.allarg_properties and self.allarg_properties.get(arg_name):
return self.allarg_properties.get(arg_name).get("default_value") # type: ignore[union-attr]
else:
raise AssertionError(f"{arg_name} not in self.allarg_properties")
def codegen_kwargs(self, skip_out=False): # type: ignore[no-untyped-def]
if V.graph.cpp_wrapper:
if self.op_overload is not None and len(self.schema_kwargs) == 0:
# All the args should have been generated by fill_non_provided_args in codegen_args
return []
kwargs = []
for arg_name in self.ordered_kwargs_for_cpp_kernel:
if skip_out and arg_name == "out":
# ExternKernelOut has its own logic for inserting the out parameter
continue
v = self.get_kwargs_value(arg_name)
if isinstance(v, sympy.Expr):
kwargs.append(v)
else:
type_ = (
self.allarg_properties.get(arg_name).get("type") # type: ignore[union-attr]
if self.allarg_properties and arg_name in self.allarg_properties
else None
)
kwargs.append(V.graph.wrapper_code.val_to_arg_str(v, type_))
else:
kwargs = [
f"{k}={V.graph.wrapper_code.val_to_arg_str(v)}"
for k, v in self.kwargs.items()
]
return kwargs
def codegen_size_asserts(self, wrapper) -> None: # type: ignore[no-untyped-def]
if config.size_asserts and not V.graph.cpp_wrapper:
# comparing strides for 0 size tensor is tricky. Ignore them for now.
if sympy_product(self.get_size()) == 0:
return
size = V.graph.wrapper_code.codegen_shape_tuple(self.get_size())
stride = V.graph.wrapper_code.codegen_shape_tuple(self.get_stride())
wrapper.writeline(
f"assert_size_stride({self.get_name()}, {size}, {stride})"
)
def get_group_stride(self): # type: ignore[no-untyped-def]
"""
get output sizes and strides, for template_codegen
"""
_size = self.get_size()
_stride = self.get_stride()
# iter_ranges = _size of output tensor, reduce_range = [] because no reduction
return [_size, []], _stride
def canonicalize(self): # type: ignore[no-untyped-def]
"""
Manually get canonicalization of the output index
"""
# manually generate index formula for conv
sizevars = V.graph.sizevars
sizes = self.get_size()
strides = self.get_stride()
strides = [sizevars.size_hint(x) for x in strides]
# TODO: I can't tell if the symbols here are temporary
index_vars = [sympy_index_symbol(f"d{i}") for i in range(len(sizes))]
# reorder index vars according to stride
index_order = sorted(range(len(strides)), key=strides.__getitem__, reverse=True)
lookup = {pos: idx for idx, pos in enumerate(index_order)}
order = [lookup[i] for i in range(len(lookup))]
index_vars = [index_vars[i] for i in order]
indexer = self.make_indexer()
index = indexer(index_vars)
new_sizes, reindex, prune = V.graph.sizevars._simplify_loops(
index_vars, sizes, [index]
)
# assign new variables each dimension to deal with numbering mismatches
# d0, d1, d2 could become d0, d2 -- which won't match d0, d1
_, add_var = var_builder("c")
replacement = dict(zip(index_vars, reindex([add_var(x) for x in new_sizes])))
index = sympy_subs(sympy.expand(index), replacement)
return index, tuple(new_sizes)
def get_unbacked_symbol_uses(self) -> OrderedSet[sympy.Symbol]:
# NB: It's not necessary to check regular inputs as we automatically
# have dependencies on them
r: OrderedSet[sympy.Symbol] = OrderedSet()
for arg in self.constant_args:
r |= maybe_free_unbacked_symbols(arg)
for arg in self.kwargs.values():
r |= maybe_free_unbacked_symbols(arg)
return r
def __str__(self) -> str:
kernel_name = getattr(self, "python_kernel_name", None)
lines = [
f"python_kernel_name={kernel_name!r}",
]
lines += [
f"{field.name}={getattr(self, field.name)}"
for field in dataclasses.fields(self)
]
lines.append(f"origin_node={self.origin_node!r}")
return self.str_helper(lines)
__repr__ = __str__
@ir_dataclass(frozen=False)
class ExternKernelOut(ExternKernel):
def codegen(self, wrapper) -> None: # type: ignore[no-untyped-def]
self.codegen_comment(wrapper)
args = [*self.codegen_args(), *self.codegen_kwargs(skip_out=True)]
kernel_name = self.get_kernel_name()
if (
V.graph.cpp_wrapper
and self.cpp_kernel_name == "torch::inductor::_mm_plus_mm"
):
# For https://github.com/pytorch/pytorch/issues/128474
kernel_name = "aoti_torch__mm_plus_mm_out"
else:
kernel_name = self.get_kernel_name()
wrapper.generate_extern_kernel_out(
kernel_name,
self.codegen_reference(),
self.output_view.codegen_reference() if self.output_view else None,
args,
)
def __init__( # type: ignore[no-untyped-def]
self,
layout,
inputs,
constant_args=(),
kwargs=None,
output_view=None,
python_kernel_name=None,
cpp_kernel_name=None,
ordered_kwargs_for_cpp_kernel=(),
op_overload=None,
) -> None:
super().__init__(
None,
layout,
self.unwrap_storage(inputs),
constant_args,
kwargs or {},
None,
python_kernel_name,
cpp_kernel_name,
ordered_kwargs_for_cpp_kernel,
op_overload,
)
self.name = V.graph.register_buffer(self)
V.graph.register_operation(self)
def should_allocate(self) -> bool:
return True
class RandomSeeds(ExternKernelOut):
def __init__(self, count: int, device: torch.device) -> None:
limits = torch.iinfo(torch.int64)
super().__init__(
layout=FixedLayout(
device=device,
dtype=torch.int64,
size=[count],
),
inputs=[],
constant_args=[limits.min, limits.max, [count]],
python_kernel_name="aten.randint.low_out",
# FIXME: Ideally we should only use at::_ops::randint_low_out::call here,
# but the signature is different from is at::randint_out. Again,
# we can simplify the code when only keeping an ABI-compatible version.
cpp_kernel_name="at::_ops::randint_low_out::call",
op_overload=aten.randint.low_out,
)
class ExternKernelAlloc(ExternKernel):
def codegen(self, wrapper) -> None: # type: ignore[no-untyped-def]
self.codegen_comment(wrapper)
args = [*self.codegen_args(), *self.codegen_kwargs()]
V.graph.wrapper_code.generate_extern_kernel_alloc(self, args)
if isinstance(self.layout, Layout):
self.codegen_size_asserts(wrapper)
def __init__( # type: ignore[no-untyped-def]
self,
layout,
inputs,
constant_args=(),
kwargs=None,
python_kernel_name=None,
cpp_kernel_name=None,
ordered_kwargs_for_cpp_kernel=(),
op_overload=None,
) -> None:
super().__init__(
None,
layout,
self.unwrap_storage(inputs),
constant_args,
kwargs or {},
None,
python_kernel_name,
cpp_kernel_name,
ordered_kwargs_for_cpp_kernel,
op_overload,
)
# We need output buffers for generating kernel arguments in the
# abi-compatible mode, where we retrieve outputs by pass each individual
# output through the abi-compatible interface.
self.outputs: Sequence[Any] = []
self.name = V.graph.register_buffer(self)
V.graph.register_operation(self)
def should_allocate(self) -> bool:
return False
def apply_constraint(self): # type: ignore[no-untyped-def]
raise NotImplementedError
class MutationOutput(Buffer):
"""
An output buffer that represents the mutation of a pre-existing buffer
"""
def __init__(self, layout, mutated_node, mutating_node: Operation) -> None: # type: ignore[no-untyped-def]
super().__init__(name=None, layout=layout)
mutated_node_name = mutated_node.get_name()
V.graph.mark_buffer_mutated(mutated_node_name)
self.mutation_names = [mutated_node_name]
self.mutating_node: Operation = mutating_node
self.name = V.graph.register_buffer(self)
def get_defining_op(self) -> Operation:
return self.mutating_node
def get_mutation_names(self): # type: ignore[no-untyped-def]
return self.mutation_names
def should_allocate(self) -> bool:
return False
class TMADescriptor(ExternKernel):
"""
An IR node representing a host-side TMA descriptor in the Triton API
(the ones obtained via create_{1d,2d}_tma_descriptor calls). Mostly
useful for user-defined Triton kernels relying on host-side TMA; but
can, in principle, be used for Inductor's Triton templates, too.
"""
# as TMA descriptors are immutable,
# we can dedup them by the input args
_CACHE: Dict[Any, TMADescriptor] = {}
@classmethod
def create( # type: ignore[no-untyped-def]
cls,
tensor: IRNode,
dims: List[Union[int, torch.SymInt]],
block_dims: List[Union[int, torch.SymInt]],
element_size: Optional[int] = None,
):
key = (id(tensor), dims, block_dims, element_size)
if key not in cls._CACHE:
cls._CACHE[key] = TMADescriptor(tensor, dims, block_dims, element_size)
return cls._CACHE[key]
def __init__(
self,
tensor: IRNode,
dims: List[Union[int, torch.SymInt]],
block_dims: List[Union[int, torch.SymInt]],
element_size: Optional[int] = None,
) -> None:
assert len(dims) in (1, 2)
assert len(dims) == len(block_dims)
if element_size is None:
element_size = tensor.get_dtype().itemsize
self.tensor = tensor
self.dims = dims
self.block_dims = block_dims
self.element_size = element_size
self.rank = len(self.dims)
inputs = [tensor]
constant_args = [
*self.dims,
*self.block_dims,
self.element_size,
]
super().__init__(
None,
# link back to the underlying tensor in terms of ownership
# to avoid getting the underlying tensor deleted *before*
# the TMADescriptor node can be deleted.
NonOwningLayout(
ReinterpretView(
data=tensor,
layout=tensor.get_layout(),
)
),
inputs,
tuple(constant_args),
None,
)
self.name = V.graph.register_buffer(self)
V.graph.register_operation(self)
def codegen(self, wrapper) -> None: # type: ignore[no-untyped-def]
wrapper.generate_tma_descriptor(self)
class UserDefinedTritonKernel(ExternKernel):
def get_kernel_and_metadata(self): # type: ignore[no-untyped-def]
from triton.runtime.autotuner import Autotuner
from torch._higher_order_ops.triton_kernel_wrap import kernel_side_table
kernel = kernel_side_table.get_kernel(self.kernel_idx)
configs = []
restore_value_args: List[str] = []
reset_to_zero_args: List[str] = []
if isinstance(kernel, Autotuner):
# https://github.com/triton-lang/triton/pull/5083
# changes kernel.restore_idx to kernel.restore_value
if hasattr(kernel, "restore_idx"):
restore_value_args.extend(
kernel.fn.arg_names[i] for i in kernel.restore_idx
)
else:
assert hasattr(kernel, "restore_value")
restore_value_args.extend(kernel.restore_value)
if hasattr(kernel, "reset_idx"):
for i in kernel.reset_idx:
reset_to_zero_args.append(kernel.fn.arg_names[i])
else:
assert hasattr(kernel, "reset_to_zero")
reset_to_zero_args.extend(kernel.reset_to_zero)
configs = kernel.configs
kernel = kernel.fn
return kernel, configs, restore_value_args, reset_to_zero_args
def codegen(self, wrapper) -> None: # type: ignore[no-untyped-def]
(
kernel,
configs,
restore_value_args,
reset_to_zero_args,
) = self.get_kernel_and_metadata()
# Definition of kernel
new_name, triton_meta = wrapper.define_user_defined_triton_kernel(
kernel, configs, self.kwargs, restore_value_args, reset_to_zero_args
)
raw_args = [
self.get_kwargs_value(k) for k in self.ordered_kwargs_for_cpp_kernel
]
# NOTE: raw_args doesn't include autotuned args.
# But, kernel.constexprs includes indices of autotuned args.
# So, let's recalculate constexpr indices wrt to raw_args.
constexpr_indices = []
for idx, kwarg in enumerate(self.ordered_kwargs_for_cpp_kernel):
if kernel.arg_names.index(kwarg) in kernel.constexprs:
constexpr_indices.append(idx)
"""
Filter out None args.
see https://github.com/pytorch/pytorch/issues/115344
Two cases for a None arg:
1. The arg is already tl.constexpr, so leave it in
2. The arg is not tl.constexpr so we have to remove it
"""
constexpr_indices_set = set(constexpr_indices)
REMOVED = object()
raw_args = [
(
(idx, arg)
if (arg is not None) or (arg is None and idx in constexpr_indices_set)
else (idx, REMOVED)
)
for idx, arg in enumerate(raw_args)
]
removed_none_args = [idx for idx, val in raw_args if val == REMOVED]
raw_args = [val for idx, val in raw_args if val != REMOVED]
# We have to compute the constexpr indices for the new, filtered raw_args
# We also have to adjust equal_to_1.
if removed_none_args:
eq1_indices_set = set(triton_meta["configs"][0].equal_to_1)
constexpr_indices = []
equal_to_1 = []
index_shift = 0
for idx, kwarg in enumerate(self.ordered_kwargs_for_cpp_kernel):
# every time we encounter an idx we removed, adjust by one to account for it
# So for example if we had [None, const X]
# iter 1:
# None was removed, adjust=1
# iter 2:
# X is const at idx=1, but the adjusted idx is 0 now, because None was removed
if idx in removed_none_args:
index_shift += 1
continue
arg_index = kernel.arg_names.index(kwarg)
if arg_index in kernel.constexprs:
constexpr_indices.append(idx - index_shift)
if arg_index in eq1_indices_set:
equal_to_1.append(idx - index_shift)
triton_meta["configs"][0].equal_to_1 = equal_to_1
# Call to kernel
self.codegen_comment(wrapper)
wrapper.generate_user_defined_triton_kernel(
new_name,
raw_args,
self.grid,
configs,
triton_meta,
constexpr_indices,
)
def get_unbacked_symbol_uses(self) -> OrderedSet[sympy.Symbol]:
# add unbacked symbols used in the grid to the ones used
# in the kwargs (the latter is generated by ExternKernel)
return super().get_unbacked_symbol_uses() | free_unbacked_symbols(self.grid)
def get_unbacked_symbol_defs(self) -> OrderedSet[sympy.Symbol]:
return OrderedSet()
def __init__(self, *, kernel_idx, grid, tma_descriptor_metadata, kernel_args) -> None: # type: ignore[no-untyped-def]
inputs = []
kwargs = {}
constant_args = []
for k, v in kernel_args.items():
if isinstance(v, TensorBox):
t = InputsKernel.unwrap_storage_for_input(self.realize_input(v))
if k in tma_descriptor_metadata:
t = TMADescriptor.create(t, *tma_descriptor_metadata[k])
inputs.append(t)
kwargs[k] = t
else:
constant_args.append(v)
kwargs[k] = v
assert len(inputs) != 0
self.device = inputs[0].get_device()
super().__init__(
None,
NoneLayout(device=self.device),
inputs,
tuple(constant_args),
kwargs,
)
self.kernel_idx = kernel_idx
self.grid = grid
kernel, configs, _, _ = self.get_kernel_and_metadata()
# If we are autotuning, not all arguments will be passed
self.ordered_kwargs_for_cpp_kernel = [
arg for arg in kernel.arg_names if arg in kernel_args
]
from torch._higher_order_ops.triton_kernel_wrap import identify_mutated_tensors
autotuned_kwargs = configs[0].kwargs if len(configs) > 0 else {}
self.mutable_args = [
kernel_args[key]
for key in identify_mutated_tensors(
kernel, {**kernel_args, **autotuned_kwargs}
)
]
self.mutation_outputs = [
MutationOutput(NoneLayout(device=self.device), buf, self)
for buf in self.mutable_args
]
V.graph.register_operation(self)
def get_outputs(self) -> List[Buffer]:
return list(self.mutation_outputs)
def get_device(self) -> Optional[torch.device]:
return self.device
class InplaceBernoulliFallback(ExternKernel):
"""
This needs to be a custom class to handle mutation properly
"""
def codegen(self, wrapper) -> None: # type: ignore[no-untyped-def]
(x,) = (t.codegen_reference() for t in self.inputs)
if V.graph.cpp_wrapper:
# Inductor doesn't really support aten Generator, so the Generator kwarg is always NULL here,
# which needs to be explicitly generated for cpp wrapper
wrapper.writeline(
f"{self.get_kernel_name()}({x}, {', '.join(map(repr, self.constant_args))}, NULL){wrapper.ending}"
)
else:
wrapper.writeline(
f"{self.get_kernel_name()}({x}, {', '.join(map(repr, self.constant_args))}){wrapper.ending}"
)
def should_allocate(self) -> bool:
return False
def get_mutation_names(self): # type: ignore[no-untyped-def]
return [self.inputs[0].get_name()]
def get_unbacked_symbol_defs(self) -> OrderedSet[sympy.Symbol]:
return OrderedSet()
def __init__(self, op_overload, x, *constant_args) -> None: # type: ignore[no-untyped-def]
super().__init__(
None,
NoneLayout(device=x.get_device()),
self.unwrap_storage([x]),
constant_args,
op_overload=op_overload,
)
V.graph.mark_buffer_mutated(x.get_name())
self.name = V.graph.register_buffer(self)
V.graph.register_operation(self)
# Used to deal with torch.complex types
class InplaceCopyFallback(ExternKernel):
"""
This needs to be a custom class to handle mutation properly
"""
def codegen(self, wrapper) -> None: # type: ignore[no-untyped-def]
(dst, src, non_blocking) = self.codegen_args()
wrapper.codegen_device_copy(src, dst, non_blocking)
def should_allocate(self) -> bool:
return False
def get_mutation_names(self): # type: ignore[no-untyped-def]
return [self.inputs[0].get_name()]
def get_unbacked_symbol_defs(self) -> OrderedSet[sympy.Symbol]:
return OrderedSet()
def __init__( # type: ignore[no-untyped-def]
self,
layout,
inputs,
constant_args,
) -> None:
super().__init__(
None,
layout,
inputs,
constant_args,
python_kernel_name="aten.copy_",
cpp_kernel_name="aoti_torch_copy_",
)
V.graph.mark_buffer_mutated(inputs[0].get_name())
self.name = V.graph.register_buffer(self)
V.graph.register_operation(self)
@classmethod
def create(cls, dst, src, non_blocking: bool = False): # type: ignore[no-untyped-def]
inputs = [cls.realize_input(t) for t in [dst, src]]
constant_args = (non_blocking,)
result = InplaceCopyFallback(
NoneLayout(device=dst.get_device()),
inputs,
constant_args,
)
return result
class MutatingFirstArgExternKernel(ExternKernel):
"""
This needs to be a custom class to handle mutation properly
"""
def codegen(self, wrapper) -> None: # type: ignore[no-untyped-def]
argrefs = [
*(t.codegen_reference() for t in self.inputs),
*map(repr, self.constant_args),
]
wrapper.writeline(
f"{self.get_kernel_name()}({', '.join(argrefs)}){wrapper.ending}"
)
def should_allocate(self) -> bool:
return False
def get_mutation_names(self): # type: ignore[no-untyped-def]
return [self.inputs[0].get_name()]
def get_unbacked_symbol_defs(self) -> OrderedSet[sympy.Symbol]:
return OrderedSet()
def has_side_effects(self) -> bool:
return True
class ResizeStorageBytes(MutatingFirstArgExternKernel):
def __init__(self, variable, new_size) -> None: # type: ignore[no-untyped-def]
assert isinstance(new_size, int), "TODO: dynamic shapes"
super().__init__(
None,
NoneLayout(device=variable.get_device()),
self.unwrap_storage([variable]),
constant_args=(new_size,),
)
V.graph.mark_buffer_mutated(variable.get_name())
self.name = V.graph.register_buffer(self)
V.graph.register_operation(self)
self.python_kernel_name = "inductor_ops.resize_storage_bytes_"
self.cpp_kernel_name = "torch::inductor::resize_storage_bytes_"
V.graph.never_reuse_buffers.add(variable.data.get_name())
class SetSourceTensorKernel(ExternKernelAlloc):
def __init__(self, self_tensor, storage_tensor) -> None: # type: ignore[no-untyped-def]
storage_tensor.freeze_layout()
super().__init__(
storage_tensor.get_layout(),
[self_tensor, storage_tensor],
python_kernel_name="torch.ops.aten.set_.source_Tensor",
op_overload=torch.ops.aten.set_.source_Tensor,
)
V.graph.never_reuse_buffers.add(self_tensor.data.get_name())
V.graph.never_reuse_buffers.add(storage_tensor.get_name())
V.graph.never_reuse_buffers.add(self.get_name())
device = storage_tensor.get_device()
self.mutation_outputs = [
MutationOutput(NoneLayout(device=device), self_tensor, self),
MutationOutput(NoneLayout(device=device), storage_tensor, self),
]
def get_inputs_that_alias_output(self) -> Sequence[str]:
return [self.inputs[0].get_name(), self.inputs[1].get_name()]
class ScatterFallback(ExternKernel):
"""
This needs to be a custom class to handle mutation properly.
This class handles both aten.scatter_ and aten.scatter_reduce_.
It also handle the case `src` being a scalar properly.
"""
def codegen(self, wrapper) -> None: # type: ignore[no-untyped-def]
reduce = self.kwargs["reduce"]
if V.graph.cpp_wrapper:
# Follow aten/src/ATen/native/ReductionType.h:get_operator_enum
get_operator_enum = {"add": "sum", "multiply": "prod"}
if reduce in get_operator_enum:
reduce = get_operator_enum[reduce]
if self.src_is_tensor:
(x, index, src) = (t.codegen_reference() for t in self.inputs)
else:
(x, index) = (t.codegen_reference() for t in self.inputs)
src = self.constant_args[1]
wrapper.generate_scatter_fallback(
x,
[x, self.constant_args[0], index, src],
self.cpp_kernel_name,
self.python_kernel_name,
self.src_is_tensor,
reduce,
self.codegen_kwargs(),
)
def should_allocate(self) -> bool:
return False
def get_mutation_names(self): # type: ignore[no-untyped-def]
return [self.inputs[0].get_name()]
def get_unbacked_symbol_defs(self) -> OrderedSet[sympy.Symbol]:
return OrderedSet()
def __init__( # type: ignore[no-untyped-def]
self,
op_overload,
x,
dim: int,
index,
src,
*,
reduce: Optional[str] = None,
include_self: bool = True,
) -> None:
self.src_is_tensor = isinstance(src, TensorBox)
constant_args: Tuple[Any, ...]
if self.src_is_tensor:
tensors = [self.realize_input(t) for t in [x, index, src]]
constant_args = (dim,)
else:
tensors = [self.realize_input(t) for t in [x, index]]
constant_args = (dim, src)
super().__init__(
None,
NoneLayout(device=x.get_device()),
self.unwrap_storage(tensors),
constant_args,
{"reduce": reduce, "include_self": include_self},
python_kernel_name=str(op_overload),
ordered_kwargs_for_cpp_kernel=["reduce", "include_self"],
op_overload=op_overload,
)
V.graph.mark_buffer_mutated(x.get_name())
self.name = V.graph.register_buffer(self)
V.graph.register_operation(self)
class IndexPutFallback(ExternKernel):
"""
This needs to be a custom class to handle mutation and indices properly
"""
def codegen(self, wrapper) -> None: # type: ignore[no-untyped-def]
(x, values, *valid_indices) = (t.codegen_reference() for t in self.inputs)
indices = []
iter_valid_indices = iter(valid_indices)
for i, _ in enumerate(self.indices):
if self.indices[i] is not None:
indices.append(next(iter_valid_indices))
else:
indices.append(V.graph.wrapper_code.none_str)
wrapper.generate_index_put_fallback(
self.get_kernel_name(), x, indices, values, *self.codegen_const_args()
)
def should_allocate(self) -> bool:
return False
def get_mutation_names(self): # type: ignore[no-untyped-def]
return [self.inputs[0].get_name()]
def get_unbacked_symbol_defs(self) -> OrderedSet[sympy.Symbol]:
return OrderedSet()
def __init__(self, op_overload, x, indices, values, accumulate) -> None: # type: ignore[no-untyped-def]
self.indices = indices
valid_indices = [i for i in indices if i is not None]
tensors = [self.realize_input(x) for x in [x, values, *valid_indices]]
cpp_kernel_name = "aoti_torch_index_put_out"
super().__init__(
None,
NoneLayout(device=x.get_device()),
self.unwrap_storage(tensors),
(accumulate,),
python_kernel_name="aten.index_put_",
cpp_kernel_name=cpp_kernel_name,
op_overload=op_overload,
)
V.graph.mark_buffer_mutated(self.inputs[0].get_name())
self.name = V.graph.register_buffer(self)
V.graph.register_operation(self)
class DeviceCopy(ExternKernelOut):
@classmethod
def create(cls, x, device, non_blocking): # type: ignore[no-untyped-def]
if (
not x.is_extern()
and all(r in V.graph.constants for r in x.get_read_names())
and not config.aot_inductor.use_runtime_constant_folding
):
return x.constant_to_device(device)
V.graph.add_device_info(device)
V.graph.add_device_info(x.get_device())
developer_warning("DeviceCopy in input program")
constant_args = (non_blocking,)
return DeviceCopy(
FlexibleLayout(
device=device,
dtype=x.get_dtype(),
size=x.get_size(),
),
[cls.realize_input(x)],
constant_args,
)
def codegen(self, wrapper) -> None: # type: ignore[no-untyped-def]
args = self.codegen_args()
assert len(args) == 2
if self.output_view:
wrapper.codegen_device_copy(
args[0], self.output_view.codegen_reference(), args[1]
)
else:
wrapper.codegen_device_copy(args[0], self.codegen_reference(), args[1])
class DynamicScalar(ExternKernel):
"""
The result of a call to aten._local_scalar_dense.
"""
def get_reads(self) -> OrderedSet[Dep]:
return OrderedSet()
def should_allocate(self) -> bool:
return False
def __init__(self, sym, keypath, data) -> None: # type: ignore[no-untyped-def]
data.realize()
super().__init__(
None, NoneLayout(device=torch.device("cpu")), self.unwrap_storage([data])
)
self.sym = sym
self.keypath = keypath
def get_unbacked_symbol_defs(self) -> OrderedSet[sympy.Symbol]:
return OrderedSet([self.sym])
def codegen(self, wrapper) -> None: # type: ignore[no-untyped-def]
wrapper.codegen_dynamic_scalar(self)
class AssertScalar(ExternKernel):
"""
The result of a call to aten._assert_scalar
"""
def get_reads(self) -> OrderedSet[Dep]:
return OrderedSet()
def should_allocate(self) -> bool:
return False
def __init__(self, scalar, msg) -> None: # type: ignore[no-untyped-def]
super().__init__(
# Buffer(name, layotu)
None,
NoneLayout(device=torch.device("cpu")),
# InputsKernel(inputs)
[],
)
self.scalar = scalar
self.msg = msg
def has_side_effects(self) -> bool:
return True
def get_unbacked_symbol_uses(self): # type: ignore[no-untyped-def]
return free_unbacked_symbols(self.scalar)
def codegen(self, wrapper) -> None: # type: ignore[no-untyped-def]
# NB: It is EXTREMELY important not to simplify the scalar under assertion here,
# because simplify is done with respect to runtime asserts. So if you have
# "u0 == 0" in the runtime asserts, if you subsequently try to
# simplify(u0 == 0), you will get True (because we've already runtime assert'ed
# that it's true). But we're code generating the actual runtime assert here!!
if V.graph.cpp_wrapper:
sizevar = V.graph.wrapper_code.codegen_cpp_sizevar(
self.scalar, simplify=False
)
# TODO: when we start compiling in C++20, annotate with [[unlikely]].
wrapper.writeline(
f'if (!({sizevar})) {{ throw std::runtime_error("{self.msg}"); }}'
)
else:
sizevar = V.graph.wrapper_code.codegen_python_sizevar(
self.scalar, simplify=False
)
wrapper.writeline(f"if not {sizevar}:")
wrapper.writeline(f" raise RuntimeError({repr(self.msg)})")
# No one should ever use this buffer, but for uniformity
# define the variable and assign it None
wrapper.writeline(f"{self.get_name()} = None")
@ir_dataclass(frozen=False)
class ExternKernelNode:
name: str
node: export_schema.Node
class FallbackKernel(ExternKernelAlloc):
def __init__( # type: ignore[no-untyped-def]
self,
layout,
kernel,
tensor_args,
nontensor_args,
unflatten_args,
kwargs=None,
*,
unbacked_bindings=None,
) -> None:
# When aten binary ops have constant second args, cpp wrapper expects the scalar
# version. This should long-term be handled as in
# https://github.com/pytorch/pytorch/issues/90923.
BINARY_OP_MAPPING = {
aten.add.Tensor: aten.add.Scalar,
aten.div.Tensor: aten.div.Scalar,
aten.divide.Tensor: aten.divide.Scalar,
aten.floor_divide: aten.floor_divide.Scalar,
aten.mul.Tensor: aten.mul.Scalar,
aten.multiply.Tensor: aten.multiply.Scalar,
aten.sub.Tensor: aten.sub.Scalar,
aten.subtract.Tensor: aten.subtract.Scalar,
aten.true_divide.Tensor: aten.true_divide.Scalar,
}
if (
kernel in BINARY_OP_MAPPING
and len(tensor_args) == 1
and len(nontensor_args) == 1
):
kernel = BINARY_OP_MAPPING[kernel]
super().__init__(
layout,
tuple(tensor_args),
tuple(nontensor_args),
op_overload=kernel,
)
self.use_runtime_dispatch = False
self.unbacked_bindings = unbacked_bindings
assert isinstance(
kernel,
(
torch._ops.OpOverload,
torch._ops.HigherOrderOperator,
),
), f"Fails to create FallbackKernel for {kernel}: {type(kernel)} not supported"
self.op_overload = kernel
self.unflatten_args = unflatten_args
self.kwargs = {} if kwargs is None else kwargs
V.graph.warn_fallback(self.python_kernel_name) # type: ignore[arg-type]
# args that are aliased
self.alias_names: List[str] = []
# args that are mutated AND returned from the op
self.mutation_names: List[str] = []
if isinstance(self.op_overload, torch._ops.HigherOrderOperator):
# We assume here that HOPs with FallbackKernel are functional.
# This may not always be true! HOPs must individually opt-in to
# FallbackKernel, so please check this if you opt-in.
return
if "_c10d_functional" in self.op_overload.name():
# _c10d_functional kernels are lowered into _CollectiveKernel which
# derives from FallbackKernel for the cpp codegen. The kernels
# don't pass the can_auto_functionalize check, but their mutation
# is handled properly by _CollectiveKernel.
return
schema = self.op_overload._schema
# NOTE: [FallbackKernel supported operators]
# We only support three types of operators:
# - functional ops
# - view ops
# - inplace aten ops
# - mutating ops that are auto-functionalizable. That is,
# the operator may mutate any number of inputs, but its outputs
# may not alias any of the inputs.
#
# The unsupported cases usually do not show up here (because
# AOTAutograd functionalized them away); the only way for an in-place
# op to show up here is if a lowering or pass introduced it.
if torch._library.utils.mutates_and_returns_first_arg(self.op_overload):
self.mutation_names.append(tensor_args[0].get_name())
return
if schema.is_mutable and not can_auto_functionalize(kernel):
raise NotImplementedError(
f"NYI: Can't generate FallbackKernel for {kernel}"
)
schema_args = schema.arguments
args, kwargs = self.unflatten_args(self.inputs, self.constant_args)
def handle_aliasing_and_mutation(info, arg) -> None: # type: ignore[no-untyped-def]
# Assertions to make sure we didn't mismatch args
if isinstance(info.type, torch.ListType):
assert isinstance(arg, (list, tuple))
is_optional_tensor = isinstance(
info.type, torch.OptionalType
) and isinstance(info.type.getElementType(), torch.TensorType)
is_list_tensor = isinstance(info.type, torch.ListType) and isinstance(
info.type.getElementType(), torch.TensorType
)
if is_optional_tensor or isinstance(info.type, torch.TensorType):
# PyTorch also accepts None and scalar types for args marked as "Tensor".
# We're not going to check all of them here.
assert not isinstance(arg, (tuple, list))
if arg is None:
return
if info.alias_info is None:
return
def add_alias(t) -> None: # type: ignore[no-untyped-def]
self.alias_names.append(t.get_name())
if info.alias_info.is_write:
self.mutation_outputs.append(
MutationOutput(NoneLayout(device=t.get_device()), t, self)
)
if is_list_tensor:
for tensor_arg in arg:
add_alias(tensor_arg)
else:
assert isinstance(info.type, torch.TensorType) or is_optional_tensor
add_alias(arg)
for info, arg in torch._library.utils.zip_schema(schema, args, kwargs):
handle_aliasing_and_mutation(info, arg)
def codegen_unbacked_symbol_defs(self, wrapper) -> None: # type: ignore[no-untyped-def]
if not hasattr(self, "unbacked_bindings"):
return
unbacked_bindings = resolve_unbacked_bindings(
V.graph.sizevars.shape_env, self.unbacked_bindings
)
if not unbacked_bindings:
return
for s, keypath in unbacked_bindings.items():
def go(expr, keypath): # type: ignore[no-untyped-def]
if keypath == ():
return expr
if (
len(keypath) >= 2
and isinstance(keypath[0], CallMethodKey)
and isinstance(keypath[1], pytree.SequenceKey)
):
return go(
f"{expr}.{keypath[0].name}({keypath[1].idx})", keypath[2:]
)
elif isinstance(keypath[0], CallMethodKey):
return go(f"{expr}.{keypath[0].name}()", keypath[1:])
elif isinstance(keypath[0], pytree.SequenceKey):
return (
go(f"std::get<{keypath[0].idx}>({expr})", keypath[1:])
if V.graph.cpp_wrapper
else go(f"{expr}[{keypath[0].idx}]", keypath[1:])
)
elif isinstance(keypath[0], DivideByKey):
# TODO: need to assert divisibility
# TODO: this is invalid C++ codegen
return go(f"{expr}.__floordiv__({keypath[0].divisor})", keypath[1:])
else:
raise AssertionError(f"unrecognized keypath {keypath}")
def go_outer(): # type: ignore[no-untyped-def]
if V.graph.cpp_wrapper:
# Special handling for the top level buffer access,
# because self.get_name() is actually never bound; the
# individual output arguments are bound by
# generate_c_shim_fallback_kernel
if len(self.outputs) == 1:
return go(self.outputs[0].get_name(), keypath)
else:
assert isinstance(keypath[0], pytree.SequenceKey)
return go(self.outputs[keypath[0].idx].get_name(), keypath[1:])
else:
return go(self.get_name(), keypath)
wrapper.writeline(
f"{wrapper.codegen_unbacked_symbol_decl(s)} = {go_outer()}{wrapper.ending}"
)
def get_unbacked_symbol_defs(self) -> OrderedSet[sympy.Symbol]:
if unbacked_bindings := getattr(self, "unbacked_bindings", None):
resolved = resolve_unbacked_bindings(
V.graph.sizevars.shape_env, unbacked_bindings
)
assert resolved is not None
return resolved.keys() # type: ignore[return-value]
else:
return OrderedSet()
def codegen_args(self): # type: ignore[no-untyped-def]
@dataclasses.dataclass
class Shim:
ref: Any
def __repr__(self) -> str:
return self.ref
tensor_args = [Shim(x.codegen_reference()) for x in self.inputs]
args, kwargs = self.unflatten_args(tensor_args, self.constant_args)
if V.graph.cpp_wrapper and isinstance(self.op_overload, torch._ops.OpOverload):
args = self.fill_non_provided_args(args, kwargs)
args = [
V.graph.wrapper_code.val_to_arg_str(x, param.real_type)
for param, x in zip(self.op_overload._schema.arguments, args)
]
else:
args = [V.graph.wrapper_code.val_to_arg_str(x) for x in args]
# let self.codegen_kwargs handle kwargs
self.kwargs.update(kwargs)
return args
@staticmethod
def find_device(tensor_args, example_output): # type: ignore[no-untyped-def]
if tensor_args:
devices = [arg.get_device() for arg in tensor_args if arg.get_device()]
return devices[0]
if isinstance(example_output, torch.Tensor):
return example_output.device
if isinstance(example_output, (list, tuple)):
device_set = OrderedSet(
FallbackKernel.find_device(None, x) for x in example_output
)
# Remove None
devices = [device for device in device_set if device]
if len(devices) == 1:
return devices[0]
for device in devices:
if is_gpu(device.type):
return device
return devices[0]
return None
def has_side_effects(self): # type: ignore[no-untyped-def]
if isinstance(self.op_overload, torch._ops.HigherOrderOperator):
return False
return get_schema_info(self.op_overload).is_mutable()
def get_inputs_that_alias_output(self): # type: ignore[no-untyped-def]
return self.alias_names
def get_mutation_names(self): # type: ignore[no-untyped-def]
assert len(self.mutation_names) <= 1
return self.mutation_names
# ProxyExecutor Design Note
# We export the ExternFallbackNodes (for custom ops) into a serialized file
# and run it with a host side proxy executor to address the ABI problem
# This is currently only implemented for fbcode. Eventually, we will also make this work for OSS.
# Detailed design doc can be found at
# https://docs.google.com/document/d/1wC4DOZFaYym2t1Esz0X5yxlLI3RDnSiyRbUus3bkJ64/edit?usp=sharing
def export_extern_kernel_node(self): # type: ignore[no-untyped-def]
log.debug(
"Extern kernel node added for node %s with target %s.",
self.get_name(),
self.op_overload,
)
assert isinstance(self, FallbackKernel)
args, kwargs = self.unflatten_args(self.inputs, self.constant_args)
args = self.fill_non_provided_args(args, kwargs)
ordered_kwargs = [
kwargs.get(key, None) for key in self.ordered_kwargs_for_cpp_kernel
]
if not V.graph.aot_mode:
# No need to serialize in the cpp wrapper JIT mode
return [*args, *ordered_kwargs]
serializer = GraphModuleSerializer(None, None) # type: ignore[arg-type]
named_arguments = serializer.serialize_inputs(self.op_overload, args, kwargs)
# serialize_outputs
def handle_single_output(return_type, output): # type: ignore[no-untyped-def]
if isinstance(return_type, torch.TensorType):
# For single Tensor
out = output
if isinstance(output, (list, tuple)):
assert len(output) == 1
out = output[0]
return export_schema.Argument.create(
as_tensor=export_schema.TensorArgument(name=out.get_name())
)
elif isinstance(return_type, torch.ListType) and isinstance(
return_type.getElementType(), torch.TensorType
):
# For single TensorList
return export_schema.Argument.create(
as_tensors=[
export_schema.TensorArgument(name=out.get_name())
for out in output
]
)
else:
raise RuntimeError(f"Unsupported return type {type(return_type)}")
target = self.op_overload
returns = target._schema.returns # type: ignore[union-attr]
if len(returns) == 1:
# NOTE: [special handling of all_reduce_coalesced_'s return value]
# all_reduce_coalesced_ return a list of tensors via self.mutation_outputs
outputs = self.outputs if self.outputs else self.mutation_outputs
return_type = returns[0].real_type
output_arguments = [handle_single_output(return_type, outputs)]
else:
# For tuple returns, e.g "-> (Tensor, Tensor)" or "-> (Tesnor, Tensor[])"
# Not generating output args for self.mutation_outputs
output_arguments = [
handle_single_output(return_schema.real_type, output)
for return_schema, output in zip(returns, self.outputs)
]
node = ExternKernelNode(
name=self.get_name(),
node=export_schema.Node(
target=self.op_overload.name(), # type: ignore[union-attr]
inputs=named_arguments,
outputs=output_arguments,
metadata={},
),
)
V.graph.extern_kernel_nodes.append(node)
return [*args, *ordered_kwargs]
def codegen(self, wrapper) -> None: # type: ignore[no-untyped-def]
kernel = self.op_overload
if kernel.namespace == "aten": # type: ignore[union-attr]
# Aten Fallback Ops
assert isinstance(kernel, torch._ops.OpOverload)
if V.graph.cpp_wrapper:
from torchgen.aoti.fallback_ops import inductor_fallback_ops
if str(kernel) not in inductor_fallback_ops:
# C shim v2 is torchgen-ed, which should cover all aten ops.
# If you do hit a missed op, please update fallback_ops.py.
log.warning(
"%s is missing a c-shim implementation, using proxy executor as fallback",
kernel,
)
self.use_runtime_dispatch = True
elif kernel.namespace == "_quantized": # type: ignore[union-attr]
# Internal Quantized Fallback Ops
assert isinstance(kernel, torch._ops.OpOverload)
else:
# For non-aten OpOverload, i.e. custom ops
if V.graph.cpp_wrapper:
self.use_runtime_dispatch = True
if self.use_runtime_dispatch:
self.codegen_comment(wrapper)
exported_args = None
args = None
exported_args = self.export_extern_kernel_node()
wrapper.generate_fallback_kernel_with_runtime_lookup(
self.get_name(),
self.python_kernel_name,
self.cpp_kernel_name,
args,
self.op_overload,
exported_args,
# NOTE: [special handling of all_reduce_coalesced_'s return value]
self.outputs if self.outputs else self.mutation_outputs,
)
else:
self.codegen_comment(wrapper)
args = [*self.codegen_args(), *self.codegen_kwargs()]
V.graph.wrapper_code.generate_fallback_kernel(self, args)
if isinstance(self.layout, Layout):
self.codegen_size_asserts(wrapper)
self.codegen_unbacked_symbol_defs(wrapper)
@staticmethod
def tensor_to_layout(output: torch.Tensor): # type: ignore[no-untyped-def]
return FixedLayout(
output.device,
output.dtype,
convert_shape_to_inductor(output.size()),
convert_shape_to_inductor(output.stride()),
)
@classmethod
def create(cls, kernel, *args, **kwargs): # type: ignore[no-untyped-def]
fake_incorrect_kernels = (aten._fused_moving_avg_obs_fq_helper_functional,)
context: ContextManager[None] = (
V.graph.fake_mode if kernel not in fake_incorrect_kernels else nullcontext() # type: ignore[assignment]
)
with context:
(
example_output,
tensor_args,
non_tensor_args,
unflatten_args,
unbacked_bindings,
) = cls.process_kernel(kernel, *args, **kwargs)
device = cls.find_device(tensor_args, example_output)
if example_output is None:
packed = cls(
NoneLayout(device=device),
kernel,
tensor_args,
non_tensor_args,
unflatten_args,
unbacked_bindings=unbacked_bindings,
)
else:
assert device, "Not sure where to find device info"
packed = cls(
MultiOutputLayout(device=device),
kernel,
tensor_args,
non_tensor_args,
unflatten_args,
unbacked_bindings=unbacked_bindings,
)
def generate_output(output, indices): # type: ignore[no-untyped-def]
if isinstance(output, (list, tuple)):
return type(output)(
generate_output(output[i], indices + [(type(output), i)])
for i in range(len(output))
)
elif isinstance(output, dict):
return {
key: generate_output(val, indices + [(type(output), key)])
for key, val in output.items()
}
elif isinstance(output, torch.Tensor):
return MultiOutput(
cls.tensor_to_layout(output),
packed,
indices,
)
elif isinstance(output, int):
return output
elif isinstance(output, torch.SymInt):
return output.node.expr
else:
assert (
output is None
), f"FallbackKernel output type {type(output)} is not supported"
return None
outputs = generate_output(example_output, [])
if isinstance(outputs, (list, tuple, dict)):
packed.outputs = outputs # type: ignore[assignment]
else:
packed.outputs = [outputs]
return outputs
def apply_constraint(self): # type: ignore[no-untyped-def]
return super().apply_constraint()
@ir_dataclass(frozen=False)
class ComplexView(FallbackKernel):
"""View a complex number as two dtyped numbers or vice versa"""
def should_allocate(self) -> bool:
return False
def get_inputs_that_alias_output(self) -> Sequence[str]:
# Signal to codegen that our output buffer isn't safe to reuse
return [self.inputs[0].get_name()]
def __init__( # type: ignore[no-untyped-def]
self,
layout,
kernel,
tensor_args,
nontensor_args,
unflatten_args,
*,
unbacked_bindings=None,
) -> None:
super().__init__(
layout,
kernel,
tensor_args,
nontensor_args,
unflatten_args,
unbacked_bindings=unbacked_bindings,
)
@ir_dataclass
class MultiOutputLayout(OutputSpec):
device: torch.device
def get_device(self) -> Optional[torch.device]:
return self.device
class MultiOutput(ExternKernel):
# Given an input MultiOutputLayout buffer, indexes out an actual buffer
# from that result. This doesn't actually produce multiple outputs,
# that's MultiOutputLayout!
def codegen_list_tuple_access(self, basename, indices): # type: ignore[no-untyped-def]
if len(indices) > 0:
itype, i = indices[0]
if issubclass(itype, list):
return self.codegen_list_tuple_access(f"{basename}[{i}]", indices[1:])
elif issubclass(itype, tuple):
# cpp wrapper code needs to use std::get<> to access a tuple
tuple_access = V.graph.wrapper_code.codegen_tuple_access(
basename, self.get_name(), str(i)
)
return self.codegen_list_tuple_access(tuple_access, indices[1:])
elif issubclass(itype, dict):
return self.codegen_list_tuple_access(f"{basename}['{i}']", indices[1:])
else:
raise AssertionError("non supported index type: ", itype)
else:
return basename
def codegen(self, wrapper) -> None: # type: ignore[no-untyped-def]
wrapper.codegen_multi_output(
self.get_name(),
self.codegen_list_tuple_access(self.inputs[0].get_name(), self.indices),
)
def __init__(self, layout: OutputSpec, input, indices: List[Tuple[Any, ...]]) -> None: # type: ignore[no-untyped-def]
super().__init__(None, layout, [input], ())
self.name = V.graph.register_buffer(self)
V.graph.register_operation(self)
self.indices = indices
def get_unbacked_symbol_uses(self) -> OrderedSet[sympy.Symbol]:
return self.inputs[0].get_unbacked_symbol_uses()
def should_allocate(self) -> bool:
return False
def get_inputs_that_alias_output(self) -> Sequence[str]:
return [
inp.get_name()
for inp in self.inputs
if isinstance(inp, FallbackKernel)
and len(inp.get_inputs_that_alias_output()) > 0
]
# We just use a normal dataclass for MutableBox/TensorBox/StorageBox since
# they're mainly lowering-time constructs that we expect to mutate and such.
@dataclasses.dataclass
class MutableBox(IRNode):
"""
TensorBox / StorageBox allow in-place mutation of Tensors
"""
data: IRNode
def has_exceeded_max_reads(self) -> bool:
return self.data.has_exceeded_max_reads()
def get_device(self) -> Optional[torch.device]:
return self.data.get_device()
def make_loader(self) -> Callable[[Sequence[Expr]], OpsValue]:
return self.data.make_loader()
def make_indexer(self) -> Callable[[Sequence[Expr]], Expr]:
return self.data.make_indexer()
def get_stride(self) -> Sequence[_IntLike]:
return self.data.get_stride()
def get_name(self) -> str:
return self.data.get_name()
def has_large_inner_fn(self, threshold: Optional[int] = None) -> bool:
return self.data.has_large_inner_fn(threshold)
def mark_reuse(self, users: int) -> None:
return self.data.mark_reuse(users)
def realize_hint(self) -> None:
return self.data.realize_hint()
def unwrap_view(self) -> IRNode:
return self.data.unwrap_view()
def freeze_layout(self) -> None:
return self.data.freeze_layout()
def freeze_layout_with_stride_order(
self, order: List[int], allow_padding: bool = False
) -> None:
return self.data.freeze_layout_with_stride_order(order, allow_padding)
def freeze_layout_with_fill_order(self, order: List[int]) -> None:
return self.data.freeze_layout_with_fill_order(order)
def freeze_layout_with_same_order(self, stride: List[_IntLike]) -> None:
return self.data.freeze_layout_with_same_order(stride)
def freeze_layout_with_exact_strides(
self, exact_strides: List[_IntLike], allow_padding: bool = False
) -> None:
return self.data.freeze_layout_with_exact_strides(exact_strides, allow_padding)
def get_read_writes(self) -> dependencies.ReadWrites:
return self.data.get_read_writes()
def get_reads(self) -> OrderedSet[Dep]:
return self.data.get_reads()
def num_reads(self) -> int:
return self.data.num_reads()
def get_storage_numel(self) -> _IntLike:
return self.data.get_storage_numel()
def get_reduction_type(self) -> Optional[str]:
return self.data.get_reduction_type()
def get_reduction_size(self) -> Sequence[sympy.Expr]:
return self.data.get_reduction_size()
def is_extern(self) -> bool:
return self.data.is_extern()
def is_no_op(self) -> bool:
return self.data.is_no_op()
def constant_to_device(self, device: torch.device) -> IRNode:
return self.data.constant_to_device(device)
def get_mutation_names(self) -> Sequence[str]:
return self.data.get_mutation_names()
def get_operation_name(self) -> str:
return self.data.get_operation_name()
def get_inputs_that_alias_output(self) -> Sequence[str]:
return self.data.get_inputs_that_alias_output()
def realize(self) -> Optional[str]:
return self.data.realize()
def get_unbacked_symbol_uses(self) -> OrderedSet[sympy.Symbol]:
return self.data.get_unbacked_symbol_uses()
def get_read_names(self) -> OrderedSet[str]:
return self.data.get_read_names()
def get_defining_op(self) -> Optional[Operation]:
return self.data.get_defining_op()
def codegen_reference(self, writer: Optional[IndentedBuffer] = None) -> str:
return self.data.codegen_reference(writer)
@property
def layout(self) -> OutputSpec:
# we intentionally call get_output_spec (rather than get_layout) since Buffer.layout is an OutputSpec
return self.data.get_output_spec()
def get_layout(self) -> Layout:
return self.data.get_layout()
def get_output_spec(self) -> OutputSpec:
return self.data.get_output_spec()
def get_size(self) -> Sequence[Expr]:
return self.data.get_size()
@property
def dtype(self): # type: ignore[no-untyped-def]
return self.data.dtype
def __str__(self) -> str:
if isinstance(self.data, MutableBox):
line0 = f"{type(self).__name__}({type(self.data).__name__}("
endl = "))"
inner = self.data.data
else:
line0 = f"{type(self).__name__}("
inner = self.data
endl = ")"
lines = [
line0,
indent(str(inner)),
endl,
]
return "\n".join(lines)
__repr__ = __str__
class TensorBox(MutableBox):
@staticmethod
def create(data): # type: ignore[no-untyped-def]
return TensorBox(StorageBox(data))
class StorageBox(MutableBox):
def is_input_buffer(self): # type: ignore[no-untyped-def]
if isinstance(self.data, (InputBuffer, ReinterpretView)):
return self.data.get_name() in V.graph.graph_inputs
return False
def is_module_buffer(self): # type: ignore[no-untyped-def]
return (
isinstance(self.data, (ConstantBuffer))
and self.data.get_name() in V.graph.constants
)
def realize(self) -> Optional[str]:
if isinstance(
self.data,
(
ComputedBuffer,
InputsKernel,
InputBuffer,
ReinterpretView,
TemplateBuffer,
),
):
return self.data.get_name()
assert isinstance(self.data, (Pointwise, Reduction, Scan, Sort)), type(
self.data
)
origin_node = self.data.get_origin_node()
traceback = self.data.get_traceback()
self.data = ComputedBuffer(
name=None,
layout=FlexibleLayout(
device=self.data.get_device(),
dtype=self.data.get_dtype(),
size=self.data.get_size(),
),
data=self.data,
)
self.data.name = V.graph.register_buffer(self.data)
V.graph.register_operation(self.data)
self.data.origins = self.origins
self.data.origin_node = origin_node
self.data.traceback = traceback
return self.data.name
def realize_hint(self) -> None:
"""
Called on buffers we expect to be forced to realize later.
"""
if (
isinstance(self.data, (Pointwise, Reduction))
and self.data.inner_fn_opcount().nontrivial_read_count > 1
):
self.realize()
def has_exceeded_max_reads(self) -> bool:
return isinstance(self.data, Pointwise) and (
self.num_reads() > config.realize_acc_reads_threshold
or self.has_large_inner_fn()
)
def should_realize_on_reuse(self, users): # type: ignore[no-untyped-def]
"""
A heuristic to decide if we should realize a tensor
that is used multiple times.
"""
if users > 1 and isinstance(self.data, (Pointwise, Reduction)):
if is_cpu(self.data):
# Heuristic for realizing reused result of heavy ops on cpu
opcount = self.data.inner_fn_opcount()
heavy_ops = ["exp", "sigmoid"] # a list of heavy ops
if any(x in opcount.used_ops for x in heavy_ops):
return True
return (
self.num_reads() > config.realize_reads_threshold
or self.has_large_inner_fn()
)
return False
def mark_reuse(self, users: int) -> None:
if self.should_realize_on_reuse(users):
self.realize()
def num_reads(self): # type: ignore[no-untyped-def]
return self.data.num_reads()
@ir_dataclass(frozen=False)
class Subgraph(IRNode):
name: str
graph_module: torch.fx.GraphModule
graph: Optional[GraphLowering] = None
def _has_aliased_buffers(buffers: Sequence[IRNode]) -> bool:
buffers = [
buffer.unwrap_view() if isinstance(buffer, ReinterpretView) else buffer
for buffer in buffers
]
# assuming the same buffer is represented by the same IRNode object
return len(OrderedSet(id(buffer) for buffer in buffers)) < len(buffers)
@ir_dataclass(frozen=False)
class InvokeSubgraph(ExternKernel):
subgraph: Optional[Subgraph] = None
operands: Optional[List[TensorBox]] = None
outputs: Optional[List[MultiOutput]] = None
def __init__(
self, subgraph: Subgraph, operands: List[TensorBox], layout: MultiOutputLayout
) -> None:
super().__init__(
name=None,
layout=layout,
inputs=operands,
)
self.subgraph = subgraph
self.name = V.graph.register_buffer(self)
V.graph.register_operation(self)
@classmethod
def create(cls, subgraph: Subgraph, operands): # type: ignore[no-untyped-def]
# TODO(anijain2305) - Support sym expr as operands in future.
fx_operands = V.graph.current_node.args[-1]
fake_operands = [x.meta["val"] for x in fx_operands] # type: ignore[union-attr]
# Realize the inputs. Also intermediates can have different strides than
# the inputs of the subgraph. So, force the intermediates to have same
# strides as that of subgraph inputs.
operands = [cls.realize_input(x) for x in operands]
def handle_sym_expr(stride): # type: ignore[no-untyped-def]
return [s.node.expr if isinstance(s, torch.SymInt) else s for s in stride]
new_operands = []
for idx, operand in enumerate(operands):
if isinstance(operand, ShapeAsConstantBuffer):
new_operands.append(operand)
else:
example_stride = handle_sym_expr(fake_operands[idx].stride())
new_operands.append(cls.require_exact_strides(operand, example_stride))
operands = new_operands
if subgraph.graph is None:
# create and lower subgraphs
subgraph.graph = V.graph.make_subgraph(
gm=subgraph.graph_module,
example_inputs=fake_operands,
subgraph_name=subgraph.name,
)
with V.set_graph_handler(subgraph.graph):
subgraph.graph.run(*fake_operands)
outputs = subgraph.graph.graph_outputs
# Find the device - operands could be integers from shapes, so we can't
# use operands[0]
device = None
for operand in operands:
if not isinstance(operand, ShapeAsConstantBuffer):
device = operand.get_device()
break
assert device is not None
invoke_subgraph = InvokeSubgraph(
subgraph=subgraph,
operands=operands,
layout=MultiOutputLayout(device=device),
)
outputs = [
MultiOutput(
FixedLayout(
device=output.get_device(),
dtype=output.get_dtype(),
size=output.get_size(), # type: ignore[arg-type]
stride=output.get_stride(),
offset=output.get_layout().offset,
),
invoke_subgraph,
[(list, i)],
)
for i, output in enumerate(outputs)
]
invoke_subgraph.outputs = outputs
return outputs
def codegen(self, wrapper) -> None: # type: ignore[no-untyped-def]
wrapper.codegen_invoke_subgraph(self)
@ir_dataclass(frozen=False)
class Conditional(ExternKernel):
predicate: Optional[IRNode] = None
operands: Optional[List[TensorBox]] = None
true_subgraph: Optional[Subgraph] = None
false_subgraph: Optional[Subgraph] = None
outputs: Optional[List[MultiOutput]] = None
def __init__(
self,
predicate: IRNode,
operands: List[TensorBox],
true_subgraph: Subgraph,
false_subgraph: Subgraph,
layout: MultiOutputLayout,
) -> None:
self.predicate = predicate
self.operands = operands
self.true_subgraph = true_subgraph
self.false_subgraph = false_subgraph
inputs = []
if not isinstance(predicate, ShapeAsConstantBuffer):
inputs.append(predicate)
inputs.extend(operands)
super().__init__(
name=None,
layout=layout,
inputs=inputs,
)
self.name = V.graph.register_buffer(self)
V.graph.register_operation(self)
@classmethod
def create( # type: ignore[no-untyped-def]
cls,
predicate: TensorBox,
true_fn: Subgraph,
false_fn: Subgraph,
operands: List[TensorBox],
):
predicate = cls.realize_input(predicate)
operands = [cls.realize_input(x) for x in operands]
fx_operands = V.graph.current_node.args[-1]
fake_operands = [x.meta["val"] for x in fx_operands] # type: ignore[union-attr]
for subgraph in (true_fn, false_fn):
if subgraph.graph is None:
# create and lower subgraphs
subgraph.graph = V.graph.make_subgraph(
gm=subgraph.graph_module,
example_inputs=fake_operands,
subgraph_name=subgraph.name,
)
with V.set_graph_handler(subgraph.graph):
subgraph.graph.run(*fake_operands)
true_outputs = true_fn.graph.graph_outputs # type: ignore[union-attr]
false_outputs = false_fn.graph.graph_outputs # type: ignore[union-attr]
for name, outputs in (("true_fn", true_outputs), ("false_fn", false_outputs)):
if _has_aliased_buffers(true_outputs):
raise AssertionError(
"Output aliasing is currently not supported in compiled torch.cond. "
f"The outputs of the {name} subgraph of torch.cond are aliased: {outputs}"
)
# make sure true and false outputs are structurally equivalent
assert len(true_outputs) == len(false_outputs), (true_outputs, false_outputs)
for i, (to, fo) in enumerate(zip(true_outputs, false_outputs)):
assert to.get_size() == fo.get_size(), (i, to, fo)
assert to.get_stride() == fo.get_stride(), (i, to, fo)
assert to.get_device() == fo.get_device(), (i, to, fo)
assert to.get_dtype() == fo.get_dtype(), (i, to, fo)
assert to.get_layout().offset == fo.get_layout().offset, (i, to, fo)
if not isinstance(predicate, ShapeAsConstantBuffer):
# use predicate device for consistent codegen-ing
device = predicate.get_device()
else:
# predicate is not a Tensor: use first operand's device
assert (
len(operands) > 0
), "When predicate is not a Tensor, there must be at least one operand in torch.cond."
device = operands[0].get_device()
conditional = Conditional(
predicate=predicate,
operands=operands,
true_subgraph=true_fn,
false_subgraph=false_fn,
layout=MultiOutputLayout(device=device),
)
outputs = [
MultiOutput(
FixedLayout(
device=output.get_device(),
dtype=output.get_dtype(),
size=output.get_size(),
stride=output.get_stride(),
offset=output.get_layout().offset,
),
conditional,
[(list, i)],
)
# as the true and false outputs are equivalent,
# we can use either of them here as a "template"
for i, output in enumerate(true_outputs)
]
conditional.outputs = outputs
return outputs
def codegen(self, wrapper) -> None: # type: ignore[no-untyped-def]
wrapper.codegen_conditional(self)
@ir_dataclass(frozen=False)
class WhileLoop(ExternKernel):
carried_inputs: Optional[List[TensorBox]] = None
additional_inputs: Optional[List[TensorBox]] = None
cond_subgraph: Optional[Subgraph] = None
body_subgraph: Optional[Subgraph] = None
outputs: Optional[List[MultiOutput]] = None
def __init__(
self,
carried_inputs: List[TensorBox],
additional_inputs: List[TensorBox],
cond_subgraph: Subgraph,
body_subgraph: Subgraph,
layout: MultiOutputLayout,
) -> None:
self.carried_inputs = carried_inputs
self.additional_inputs = additional_inputs
self.cond_subgraph = cond_subgraph
self.body_subgraph = body_subgraph
super().__init__(
name=None,
layout=layout,
inputs=carried_inputs + additional_inputs,
)
self.name = V.graph.register_buffer(self)
V.graph.register_operation(self)
@classmethod
def create( # type: ignore[no-untyped-def]
cls,
cond_fn: Subgraph,
body_fn: Subgraph,
carried_inputs: List[TensorBox],
additional_inputs: List[TensorBox],
):
carried_inputs = [cls.realize_input(x) for x in carried_inputs]
additional_inputs = [cls.realize_input(x) for x in additional_inputs]
all_inputs = carried_inputs + additional_inputs
fx_all_inputs = V.graph.current_node.args[-2] + V.graph.current_node.args[-1] # type: ignore[operator]
fake_all_inputs = [x.meta["val"] for x in fx_all_inputs] # type: ignore[union-attr]
for subgraph in (cond_fn, body_fn):
if subgraph.graph is None:
# create and lower subgraphs
subgraph.graph = V.graph.make_subgraph(
gm=subgraph.graph_module,
example_inputs=fx_all_inputs, # type: ignore[arg-type]
subgraph_name=subgraph.name,
)
with V.set_graph_handler(subgraph.graph):
subgraph.graph.run(*fake_all_inputs)
cond_outputs = cond_fn.graph.graph_outputs # type: ignore[union-attr]
body_outputs = body_fn.graph.graph_outputs # type: ignore[union-attr]
if _has_aliased_buffers(body_outputs):
raise AssertionError(
"Output aliasing is currently not supported in compiled torch.while_loop. "
f"The outputs of the body_fn subgraph of torch.while_loop are aliased: {body_outputs}"
)
# make sure cond_fn returns a boolean scalar Tensor
assert len(cond_outputs) == 1, cond_outputs
assert cond_outputs[0].get_dtype() == torch.bool, cond_outputs
assert len(cond_outputs[0].get_size()) == 0, cond_outputs
assert (
len(all_inputs) > 0
), "torch.while_loop is assumed to have at least one operand."
device = all_inputs[0].get_device()
# make sure carried_inputs and body outputs are structurally equivalent
assert len(carried_inputs) == len(body_outputs), (carried_inputs, body_outputs)
for i, (op, bo) in enumerate(zip(carried_inputs, body_outputs)):
def _guard_list_equals(
lhs_exprs: List[Union[int, sympy.expr]],
rhs_exprs: List[Union[int, sympy.expr]],
) -> None:
for lhs, rhs in zip(lhs_exprs, rhs_exprs):
V.graph.sizevars.guard_equals(lhs, rhs)
_guard_list_equals(op.get_size(), bo.get_size())
_guard_list_equals(op.get_stride(), bo.get_stride())
# assume all carried_inputs and outputs are on the same device
# as the MultiOutputLayout below requires single device
assert op.get_device() == bo.get_device() == device, (i, op, bo, device)
assert op.get_dtype() == bo.get_dtype(), (i, op, bo)
assert op.get_layout().offset == bo.get_layout().offset, (i, op, bo)
while_loop = WhileLoop(
carried_inputs=carried_inputs,
additional_inputs=additional_inputs,
cond_subgraph=cond_fn,
body_subgraph=body_fn,
# asserted above that there is at least one operand
layout=MultiOutputLayout(device=device),
)
outputs = [
MultiOutput(
FixedLayout(
device=output.get_device(),
dtype=output.get_dtype(),
size=output.get_size(),
stride=output.get_stride(),
offset=output.get_layout().offset,
),
while_loop,
[(list, i)],
)
for i, output in enumerate(body_outputs)
]
for inp, out in zip(carried_inputs, outputs):
if inp.get_name() in V.graph.graph_inputs:
# if a carried input of the while_loop is a graph input,
# it can be returned as is when the number of iterations
# is zero. due to this, we can't (generally) reuse the
# output buffers corresponding to the graph inputs, as
# the inputs may end up being mutated.
V.graph.never_reuse_buffers.add(out.get_name())
while_loop.outputs = outputs
return outputs
def codegen(self, wrapper) -> None: # type: ignore[no-untyped-def]
wrapper.codegen_while_loop(self)
class EffectfulKernel(FallbackKernel):
def __init__( # type: ignore[no-untyped-def]
self,
layout,
kernel,
tensor_args,
nontensor_args,
unflatten_args,
kwargs=None,
*,
unbacked_bindings=None,
) -> None:
super().__init__(
layout,
kernel,
tensor_args,
nontensor_args,
unflatten_args,
kwargs=None,
unbacked_bindings=unbacked_bindings,
)
from torch._higher_order_ops.effects import get_effect_key
effect_type = get_effect_key(kernel, (*nontensor_args, *tensor_args), kwargs)
assert effect_type is not None
self.effect_type = effect_type
self.prev_effect_buffer = V.graph.effectful_ops.get(effect_type, None)
V.graph.effectful_ops[effect_type] = self
def get_read_writes(self) -> dependencies.ReadWrites:
read_writes = super().get_read_writes()
if self.prev_effect_buffer is not None:
read_writes.reads.add(
dependencies.StarDep(self.prev_effect_buffer.get_name())
)
return read_writes
def has_side_effects(self) -> bool:
return True
@ir_dataclass
class TorchBindObject(IRNode):
name: str
value: torch._C.ScriptObject
def get_name(self): # type: ignore[no-untyped-def]
return self.name
def codegen_reference(self, writer: Optional[IndentedBuffer] = None) -> str:
return self.name
class _CollectiveKernel(FallbackKernel):
def should_allocate(self) -> bool:
return False
def has_side_effects(self) -> bool:
return True
# This is identical to FallbackKernel.set_cpp_kernel(), minus the
# part that checks against input aliasing and mutation.
def set_cpp_kernel_name(self, cpp_kernel_name: Optional[str] = None) -> None:
assert (
type(self.op_overload) is torch._ops.OpOverload
), "Setting cpp kernel needs a valid op_overload"
kernel = self.op_overload
self.cpp_kernel_name = kernel._schema.name
self.ordered_kwargs_for_cpp_kernel = [
x.name for x in kernel._schema.arguments if x.kwarg_only
]
# NOTE: [In-Place Collective Safety]
# Between the initiation and completion of an in-place collective, the
# input buffers are subject to both volatile reads and volatile writes.
# They must not be read, written to or reused by another kernel. To ensure
# the constraints, we model collective -> wait_tensor as as two-step
# mutation of the input buffers.
@classmethod
def create_inplace( # type: ignore[no-untyped-def]
cls, kernel, inputs: Union[TensorBox, List[TensorBox]], *args, **kwargs
) -> None:
with V.graph.fake_mode:
(
example_output,
tensor_args,
non_tensor_args,
unflatten_args,
unbacked_bindings,
) = cls.process_kernel(kernel, inputs, *args, **kwargs)
assert not unbacked_bindings, f"{kernel} {unbacked_bindings}"
for tensor_arg in tensor_args:
tensor_arg.realize()
device = tensor_args[0].get_device()
packed = cls(
NoneLayout(device=device),
kernel,
tensor_args,
non_tensor_args,
unflatten_args,
)
inps = pytree.tree_leaves(inputs)
packed.mutation_outputs.extend(
[MutationOutput(NoneLayout(device=device), buf, packed) for buf in inps]
)
# For inplace collective ops, the input is guaranteed to be alias of the returned value of op.
packed.alias_names.extend([inp.get_name() for inp in inps])
if "out" in kwargs:
packed.mutation_outputs.append(
MutationOutput(NoneLayout(device=device), kwargs["out"], packed)
)
# For out-variant collective ops, the `out=` arg is guaranteed to be alias of the returned value of op.
packed.alias_names.append(kwargs["out"].get_name())
# NOTE: [Out-of-Place Collective Safety]
# Between the initiation and completion of an out-of-place collective:
#
# Input buffers:
# - Are subject to volatile reads
# - Can be read by another kernel
# - Must not be written to or reused by another kernel
#
# Output buffers:
# - Are subject to volatile writes
# - Must not be read, written to or reused by another kernel
#
# To ensure the safety of input buffers without sacrificing read
# availability, we add input buffers as read deps of wait_tensor kernels.
#
# To ensure the safety of output buffers, we model wait_tensor as a
# mutation to the output buffer. Note we also assumes the user program being
# correct and the output buffer is not consumed by kernels other than
# wait_tensor.
#
# TODO(yifu): add a pre-grad pass to validate the correctness of collective
# usage in the user program.
@classmethod
def create_out_of_place( # type: ignore[no-untyped-def]
cls, kernel, inputs: Union[TensorBox, List[TensorBox]], *args, **kwargs
):
with V.graph.fake_mode:
(
example_output,
tensor_args,
non_tensor_args,
unflatten_args,
unbacked_bindings,
) = cls.process_kernel(kernel, inputs, *args, **kwargs)
assert not unbacked_bindings, f"{kernel}, {unbacked_bindings}"
for tensor_arg in tensor_args:
tensor_arg.realize()
if isinstance(example_output, list):
device = cls.find_device(tensor_args, example_output)
packed = cls(
MultiOutputLayout(device=device),
kernel,
tensor_args,
non_tensor_args,
unflatten_args,
)
packed.outputs = [
MultiOutput(
cls.tensor_to_layout(tensor),
packed,
[(list, i)],
)
for i, tensor in enumerate(example_output)
]
return packed.outputs
else:
packed = cls(
cls.tensor_to_layout(example_output),
kernel,
tensor_args,
non_tensor_args,
unflatten_args,
)
packed.outputs = [packed]
return packed
class _WaitKernel(_CollectiveKernel):
def get_volatile_reads(self): # type: ignore[no-untyped-def]
inp = self.inputs[0]
if isinstance(inp, _CollectiveKernel):
# Out-of-place single-output
return [inp.inputs[0]]
elif isinstance(inp, MultiOutput):
# This can be two things:
# 1. Out-of-place multi-output coll
# 2. In-place coll with inputs coming from another MultiOutput
coll = inp.inputs[0]
# Case 1
if isinstance(coll, _CollectiveKernel):
_, idx = inp.indices[0]
return [coll.inputs[idx]]
# Case 2
return []
else:
# In-place requires no additional deps handling for volatile
# reads since the inputs are mutated.
return []
@classmethod
def create_wait(cls, kernel, inp: TensorBox) -> None: # type: ignore[no-untyped-def]
with V.graph.fake_mode:
(
example_output,
tensor_args,
non_tensor_args,
unflatten_args,
unbacked_bindings,
) = cls.process_kernel(kernel, inp)
assert not unbacked_bindings, f"{kernel} {unbacked_bindings}"
packed = cls(
NoneLayout(device=inp.get_device()),
kernel,
tensor_args,
non_tensor_args,
unflatten_args,
)
packed.mutation_outputs.append(
MutationOutput(NoneLayout(device=inp.get_device()), inp, packed)
)
def get_read_writes(self) -> dependencies.ReadWrites:
read_writes = super().get_read_writes()
# See [Out-of-Place Collective Safety].
volatile_reads = self.get_volatile_reads()
for vr in volatile_reads:
read_writes.reads.add(dependencies.StarDep(vr.get_name()))
return read_writes
# NB: recursive structure here reflects val_to_arg_str, avoid
# calling free_unbacked_symbols on "exotic" types that don't get pexpr
# treatment
def maybe_free_unbacked_symbols(s: object) -> OrderedSet[Symbol]:
if isinstance(s, (SymTypes, Expr)):
# This branch should be impossible in return position
return free_unbacked_symbols(s)
elif isinstance(s, (tuple, list)):
r: OrderedSet[sympy.Symbol] = OrderedSet()
for t in s:
r |= maybe_free_unbacked_symbols(t)
return r
elif isinstance(s, torch.Tensor):
# This branch is impossible in constant-args position
return free_unbacked_symbols(s)
else:
return OrderedSet()
|