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
|
# mypy: ignore-errors
import abc
import collections
import contextlib
import copy
import dataclasses
import enum
import functools
import inspect
import itertools
import logging
import math
import operator
import random
import re
import sys
import types
import warnings
import weakref
from typing import (
Any,
Callable,
Dict,
FrozenSet,
List,
MutableMapping,
NamedTuple,
Optional,
Set,
TYPE_CHECKING,
Union,
)
import sympy
import torch
from torch import SymInt
from torch._guards import GuardSource, TracingContext
from torch._higher_order_ops.torchbind import call_torchbind
from torch._ops import HigherOrderOperator
from torch._subclasses.fake_tensor import FakeTensor, is_fake, maybe_get_fake_mode
from torch._subclasses.meta_utils import is_sparse_any, safe_grad
from torch._utils_internal import justknobs_check
from torch.fx.experimental._backward_state import BackwardState
from torch.fx.experimental.symbolic_shapes import (
_constrain_range_for_size,
_nested_int_aware_sort,
DimDynamic,
RelaxedUnspecConstraint,
StatefulSymbolicContext,
SubclassSymbolicContext,
SymbolicContext,
)
from torch.fx.immutable_collections import immutable_dict, immutable_list
from torch.utils._python_dispatch import is_traceable_wrapper_subclass
from torch.utils._sympy.value_ranges import ValueRanges
from torch.utils.weak import TensorWeakRef
from .. import config, mutation_guard, replay_record, trace_rules
from ..device_interface import get_registered_device_interfaces
from ..exc import InternalTorchDynamoError, unimplemented
from ..guards import GuardBuilder, install_guard, make_dupe_guard
from ..pgo import (
auto_dynamic,
auto_unset,
FrameStateSizeEntry,
InferStride,
process_automatic_dynamic,
)
from ..side_effects import SideEffects
from ..source import (
AttrProxySource,
AttrSource,
CallMethodItemSource,
ConstantSource,
ConstDictKeySource,
ConvertIntSource,
FloatTensorSource,
GetItemSource,
GradSource,
is_constant_source,
is_from_defaults,
is_from_optimizer_source,
LocalSource,
NumpyTensorSource,
OptimizerSource,
RandomValueSource,
Source,
SubclassAttrListSource,
TupleIteratorGetItemSource,
)
from ..trace_rules import (
is_callable_allowed,
is_numpy,
is_numpy_dtype,
is_numpy_type_info,
)
from ..utils import (
_extract_tensor_dict,
build_checkpoint_variable,
build_invoke_subgraph_variable,
clone_input,
common_constant_types,
get_fake_value,
get_locals_to_steal,
get_static_address_type,
is_frozen_dataclass,
is_function_or_wrapper,
is_invoke_subgraph,
is_lru_cache_wrapped_function,
is_namedtuple,
is_parameter_freezing,
is_typing,
is_utils_checkpoint,
is_wrapper_or_member_descriptor,
istype,
namedtuple_fields,
odict_values,
proxy_args_kwargs,
range_iterator,
set_example_value,
tensor_always_has_static_shape,
tuple_iterator,
tuple_iterator_getitem,
tuple_iterator_len,
unwrap_with_attr_name_if_wrapper,
wrap_fake_exception,
)
from .base import typestr, ValueMutationNew, VariableTracker, VariableTrackerMeta
from .constant import ConstantVariable, EnumVariable
from .ctx_manager import (
AutocastModeVariable,
EventVariable,
NullContextVariable,
PreserveVersionContextVariable,
StreamContextVariable,
StreamVariable,
)
from .dicts import (
ConstDictVariable,
CustomizedDictVariable,
DefaultDictVariable,
FrozensetVariable,
HFPretrainedConfigVariable,
PythonSysModulesVariable,
SetVariable,
)
from .distributed import (
DeviceMeshVariable,
PlacementClassVariable,
PlacementVariable,
ProcessGroupVariable,
WorldMetaClassVariable,
)
from .functions import (
CollectiveFunctionRewriteVariable,
CreateTMADescriptorVariable,
FunctoolsPartialVariable,
TritonKernelVariable,
UserFunctionVariable,
UserMethodVariable,
WrapperUserFunctionVariable,
)
from .higher_order_ops import TorchHigherOrderOperatorVariable
from .iter import ItertoolsVariable
from .lazy import LazyVariableTracker
from .lists import (
BaseListVariable,
ListIteratorVariable,
ListVariable,
NamedTupleVariable,
RangeVariable,
RestrictedListSubclassVariable,
SizeVariable,
SliceVariable,
TupleIteratorVariable,
TupleVariable,
)
from .misc import (
AutogradEngineVariable,
AutogradFunctionContextVariable,
AutogradFunctionVariable,
ComptimeVariable,
DebuggingVariable,
DelayGraphBreakVariable,
GetAttrVariable,
GetSetDescriptorVariable,
InspectSignatureVariable,
LambdaVariable,
LoggingLoggerVariable,
MethodWrapperVariable,
NumpyDTypeVariable,
NumpyTypeInfoVariable,
NumpyVariable,
PythonModuleVariable,
RandomClassVariable,
RandomVariable,
RegexPatternVariable,
SavedTensorBox,
TorchVersionVariable,
TypingVariable,
WeakRefVariable,
)
from .nn_module import (
FSDPManagedNNModuleVariable,
UnspecializedBuiltinNNModuleVariable,
UnspecializedNNModuleVariable,
)
from .optimizer import OptimizerVariable
from .script_object import TorchScriptObjectVariable
from .sdpa import SDPAParamsVariable
from .tensor import (
NumpyNdarrayVariable,
supported_const_comparison_op_values,
SymNodeVariable,
TensorSubclassVariable,
TensorVariable,
UnspecializedPythonVariable,
)
from .torch import TorchCtxManagerClassVariable, TorchInGraphFunctionVariable
from .torch_function import (
build_torch_function_fn,
TensorWithTFOverrideVariable,
torch_function_mode_stack_state_mgr,
TorchFunctionModeVariable,
)
from .user_defined import (
FrozenDataClassVariable,
KeyedJaggedTensorVariable,
MutableMappingVariable,
SourcelessGraphModuleVariable,
UserDefinedClassVariable,
UserDefinedObjectVariable,
)
try:
import numpy as np
except ModuleNotFoundError:
np = None
if TYPE_CHECKING:
from torch._dynamo.symbolic_convert import InstructionTranslator
log = logging.getLogger(__name__)
static_inputs_log = torch._logging.getArtifactLogger(
__name__, "cudagraph_static_inputs"
)
DimList = List
def safe_has_grad(t):
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "The .grad attribute of a Tensor")
return hasattr(t, "grad")
class _missing:
pass
@dataclasses.dataclass
class GraphArg:
source: Source
# TODO: storing a SymInt here but not a FakeTensor is a pretty strange
# thing to do. Probably should have example (which stores an int) and
# fake_example
_example: Union[TensorWeakRef, torch.SymInt]
# When True, this indicates that this GraphArg is a Python quantity (e.g.,
# a float or int) which we pass to the FX graph as a Tensor. This
# controls how we codegen calls into the Dynamo graph: we will call
# torch.as_tensor on the quantity before passing it in.
#
# Note that we typically do not pass dynamic integers as tensors, because
# they will most frequently just be used for size computation. But this
# is a policy decision that we can change our mind on; in particular, when
# an int comes from a random number generator (e.g., random.randint), we
# DO pass it as a tensor.
#
# It's also worth noting that our current tracing rules for
# pass_arg_as_tensor as subtly broken: we just pun the variable as a
# 0d scalar Tensor and pray that the semantics are the same. Which they
# often are, but not necessarily. ezyang(May 2024) plans to fix this
# soon.
pass_arg_as_tensor: bool
fake_tensor: Optional[torch._subclasses.fake_tensor.FakeTensor]
# UnspecializedPythonVariable often masquerades as a tensor.
# We MUST NOT generate shape guard code
# that actually tries to access tensor properties on these values.
# is_tensor lets us tell if this graph arg actually is a tensor
# or not.
is_tensor: bool = True
# Sometimes, the Tensor we pass to example is freshly allocated (smh).
# Then we cannot only keep a weak reference to it. This lets you
# stash a strong reference too.
example_strong_ref: Optional[torch.Tensor] = None
@property
def example(self):
if isinstance(self._example, TensorWeakRef):
r = self._example()
assert r is not None
return r
else:
return self._example
def __post_init__(self):
if isinstance(self._example, torch.Tensor):
self._example = TensorWeakRef(self._example)
assert is_fake(self.fake_tensor)
def reconstruct(self, codegen):
self.source.reconstruct(codegen)
def erase(self):
self._example = None
self.example_strong_ref = None
def __eq__(self, other):
return self.source.name() == other.source.name()
class BackwardStateGraphArg(GraphArg):
def __init__(self) -> None:
super().__init__(
source=None,
_example=BackwardState(),
pass_arg_as_tensor=False,
fake_tensor=None,
is_tensor=False,
)
def reconstruct(self, codegen):
assert codegen.tx.output.backward_state_var
codegen.add_push_null(
lambda: codegen.load_import_from(BackwardState.__module__, "BackwardState")
)
codegen.call_function(0, False)
codegen.dup_top()
codegen.store(codegen.tx.output.backward_state_var)
# All class-based iterators in itertools
# NOTE: use id() because some objects are not hashable, it will raise error during lookup
ITERTOOLS_TYPE_IDS: FrozenSet[int] = frozenset(
id(member)
for name, member in vars(itertools).items()
if not name.startswith("_") and inspect.isclass(member)
)
# Will be updated later in substitute_in_graph in torch/_dynamo/polyfills/itertools.py
ITERTOOLS_POLYFILLED_TYPE_IDS: Set[int] = set()
class VariableBuilder:
"""Wrap a python value in a VariableTracker() instance"""
def __init__(
self,
tx,
source: Source,
) -> None:
assert (
source is not None
), "Consider SourcelessBuilder for ephemeral objects, usually objects created locally."
assert TracingContext.try_get() is not None, "Expected active TracingContext"
super().__init__()
self.tx = tx
self.source = source
self.name = source.name()
def __call__(self, value):
if value in self.tx.output.side_effects:
side_effect_result = self.tx.output.side_effects[value]
dup_guard = make_dupe_guard(self.source, side_effect_result.source)
if dup_guard:
self.install_guards(dup_guard)
return side_effect_result
cached_vt = self.tx.output.variable_tracker_cache.lookup(value, self.source)
if cached_vt:
return cached_vt
vt = self._wrap(value)
vt.source = self.source
if (
self._can_lift_attrs_to_inputs(vt)
and value not in self.tx.output.side_effects
and not is_wrapper_or_member_descriptor(value)
):
vt = self.tx.output.side_effects.track_object_existing(value, vt)
self.tx.output.variable_tracker_cache.add(value, self.source, vt)
return vt
def _can_lift_attrs_to_inputs(self, vt):
return type(vt) in {
TensorVariable,
TensorWithTFOverrideVariable,
UserDefinedObjectVariable,
NumpyNdarrayVariable,
}
@staticmethod
@functools.lru_cache(None)
def _common_constants():
return {
# We zero-one specialize shapes, so specialize these constants
# too
0,
1,
# NB: There used to be more constants here, but honestly it was
# pretty confusing. Note we specialize floats by default, and
# DON'T specialize ints by default. This all only matters with
# dynamic_shapes
}
def get_source(self):
return self.source
def install_guards(self, *guards):
source = self.get_source()
if (
isinstance(source, ConstantSource)
or source.guard_source() == GuardSource.CONSTANT
):
return None
install_guard(*[source.make_guard(guard) for guard in guards], skip=1)
return {}
@classmethod
def _type_dispatch(cls):
return cls._type_dispatch_impl(config.trace_numpy)
@classmethod
@functools.lru_cache(None)
def _type_dispatch_impl(cls, trace_numpy):
# NB: Careful not to close over self to avoid ref cycle from lru_cache
entries = [
(
(
torch.Tensor,
torch.nn.Parameter,
torch._subclasses.FakeTensor,
torch._subclasses.functional_tensor.FunctionalTensor,
),
cls.wrap_tensor,
),
(
(tuple, list, odict_values, collections.deque, torch.Size),
cls.wrap_listlike,
),
(tuple_iterator, cls.wrap_tuple_iterator),
(range_iterator, cls.wrap_range_iterator),
((slice, range), cls.wrap_slice_range),
(tuple(common_constant_types), cls.wrap_literal),
(re.Pattern, cls.wrap_regex_pattern),
(weakref.ReferenceType, cls.wrap_weakref),
(torch.utils.hooks.RemovableHandle, cls.wrap_removable_handle),
(torch.jit.ScriptFunction, cls.wrap_jit_function),
]
if trace_numpy and np:
entries.append((np.ndarray, cls.wrap_numpy_ndarray))
result = {}
for ts, fn in entries:
for t in ts if isinstance(ts, tuple) else (ts,):
assert t not in result
result[t] = fn
return result
def wrap_regex_pattern(self, value: re.Pattern):
# TODO(jansel): something like a REPR_MATCH might be more robust here
self.install_guards(GuardBuilder.ID_MATCH)
return RegexPatternVariable(value)
def wrap_weakref(self, value: weakref.ReferenceType):
self.install_guards(GuardBuilder.TYPE_MATCH)
return WeakRefVariable.build(self.tx, value, source=self.source)
def wrap_removable_handle(self, value):
# This means that the removable handle was created in some other frame.
# Our current infra requires the hook to be registered and removed in
# the same frame. So graph break.
# Related test - PYTORCH_TEST_WITH_DYNAMO=1 python test/test_autograd.py -k TestAutograd.test_hooks
unimplemented("unregistered hook removable handle")
def wrap_jit_function(self, value):
self.install_guards(GuardBuilder.TYPE_MATCH)
return WrapperUserFunctionVariable(
value, "_torchdynamo_inline", source=self.source
)
@classmethod
@functools.lru_cache(None)
def _id_dispatch(
cls,
) -> Dict[int, Callable[["VariableBuilder", Any], VariableTracker]]:
from ..comptime import comptime
entries = [
(
inspect.signature,
lambda self, value: LambdaVariable(
InspectSignatureVariable.create,
source=self.source,
**self.install_guards(GuardBuilder.CLOSURE_MATCH),
),
),
(comptime, lambda self, value: ComptimeVariable()),
(
dataclasses.fields,
lambda self, value: LambdaVariable(
_dataclasses_fields_lambda,
source=self.source,
**self.install_guards(GuardBuilder.FUNCTION_MATCH),
),
),
(torch.__version__, lambda self, value: TorchVersionVariable()),
]
result = {}
for ts, fn in entries:
for t in ts if isinstance(ts, (tuple, list)) else (ts,):
assert t not in result
result[id(t)] = fn
return result
def _wrap(self, value):
# import here to avoid circular dependencies
from torch.utils._triton import has_triton, has_triton_tma
if has_triton():
from triton.runtime.autotuner import Autotuner
from triton.runtime.jit import JITFunction
else:
class JITFunction:
pass
class Autotuner:
pass
if has_triton_tma():
from triton.tools.experimental_descriptor import (
create_1d_tma_descriptor,
create_2d_tma_descriptor,
)
else:
def create_1d_tma_descriptor():
pass
def create_2d_tma_descriptor():
pass
# Handle exact type() match
type_dispatch = self._type_dispatch().get(type(value))
if type_dispatch is not None:
return type_dispatch(self, value)
# Handle exact id() match
id_dispatch = self._id_dispatch().get(id(value))
if id_dispatch is not None:
return id_dispatch(self, value)
# Everything else (NB: order matters!)
if is_traceable_wrapper_subclass(value) or istype(
value, config.traceable_tensor_subclasses
):
return self.wrap_tensor(value)
elif is_namedtuple(value):
self.install_guards(GuardBuilder.SEQUENCE_LENGTH)
output = [
LazyVariableTracker.create(
getattr(value, name),
source=AttrSource(self.source, name),
)
for name in namedtuple_fields(type(value))
]
result = NamedTupleVariable(
output, tuple_cls=type(value), source=self.source
)
return result
elif value is torch.utils._pytree.SUPPORTED_NODES:
# For SUPPORTED_NODES, we guard on the dictionary version (PEP509)
# under the assumption that the values themselves don't change.
self.install_guards(GuardBuilder.DICT_VERSION)
# The keys on the SUPPORTED_NODES can be arbitrary, so save on the
# key order.
self.tx.output.guard_on_key_order.add(self.source.name())
result = {
TypingVariable(k): UserDefinedObjectVariable(
v,
source=GetItemSource(
self.get_source(), ConstDictKeySource(self.get_source(), i)
),
)
for i, (k, v) in enumerate(value.items())
}
return ConstDictVariable(result, type(value))
elif value is sys.modules:
self.install_guards(GuardBuilder.FUNCTION_MATCH)
return PythonSysModulesVariable(source=self.source)
elif CustomizedDictVariable.is_matching_cls_hf(type(value)):
self.install_guards(GuardBuilder.TYPE_MATCH)
result = CustomizedDictVariable.wrap(self, value)
return self.tx.output.side_effects.track_object_existing(value, result)
elif istype(value, (dict, collections.defaultdict, collections.OrderedDict)):
self.install_guards(GuardBuilder.SEQUENCE_LENGTH)
# Optimisation for the common case strings, ints, etc
all_const = all(ConstantVariable.is_literal(k) for k in value.keys())
if all_const:
# TODO(anijain2305) - Do we have to guard on all the keys? Can
# keys be guarded lazily, similar to values?
self.install_guards(GuardBuilder.DICT_CONST_KEYS)
else:
# Guard on the key order
# This is not ideal, i.e., there is no need to guard on the key
# order. But we guard on the key order because of the complexity
#
# 1) For non-constant objects, we can't save the key in the
# guard context because it can be memory heavy. We can add
# weakrefs but this complicates the accesses.
#
# 2) For non-constant objects, we also have to guard on the keys
# (like TENSOR_MATCH on tensor). We might also have guards on
# the attributes of the keys (like tensor.grad). To make this
# work in tree strucutre is complicated.
#
# So, instead we guard on the key order. While guarding on key
# order, we just save the indices and use it to access keys and
# values. Indices are cheap to save.
self.tx.output.guard_on_key_order.add(self.source.name())
# We need all the keys to be hashable. We do this within the
# _HashableTracker class in dicts.py
def build_key_value(i, k, v):
if all_const:
key = ConstantVariable.create(k)
source_key = k
else:
source_key = ConstDictKeySource(self.get_source(), i)
key = LazyVariableTracker.create(k, source_key)
source_value = GetItemSource(self.get_source(), source_key)
value = LazyVariableTracker.create(v, source_value)
return key, value
result = dict(
build_key_value(i, k, v) for i, (k, v) in enumerate(value.items())
)
if istype(value, collections.defaultdict):
factory_source = AttrSource(self.source, "default_factory")
result = DefaultDictVariable(
result,
type(value),
default_factory=VariableBuilder(self.tx, factory_source)(
value.default_factory
),
source=self.source,
)
else:
result = ConstDictVariable(
result, user_cls=type(value), source=self.source
)
return self.tx.output.side_effects.track_mutable(value, result)
elif isinstance(value, torch.nn.Module):
return self.wrap_module(value)
elif ConstantVariable.is_literal(value): # non-atomic literals
return self.wrap_literal(value)
elif isinstance(value, torch.overrides.TorchFunctionMode):
var = TorchFunctionModeVariable(value, source=self.source)
self.tx.output.side_effects.track_object_existing(value, var)
return var
elif istype(value, frozenset) and all(
(
# For DBR quantization, we could get a frozenset of torch funcs.
(type(x) is types.BuiltinMethodType and x.__module__ == "torch")
or
# Another commonly used frozenset of types.
x in torch.utils._pytree.BUILTIN_TYPES
)
for x in value
):
# For the limited cases of frozenset here, we know the items won't
# change across runs, so we can safely create sourceless VTs for
# them and only guard on the frozenset id.
# TODO support source for sets and remove the special logics here.
items = [SourcelessBuilder.create(self.tx, v) for v in value]
self.install_guards(GuardBuilder.ID_MATCH)
return FrozensetVariable(items, source=self.source)
elif isinstance(value, enum.Enum):
self.install_guards(GuardBuilder.ID_MATCH)
return EnumVariable(value=value, source=self.source)
elif DebuggingVariable.is_reorderable_logging_function(value):
# Put this above builtin_callable so that print() can be handled
# along with other builtin debugging functions
self.install_guards(GuardBuilder.BUILTIN_MATCH)
return DebuggingVariable(value, source=self.source)
elif isinstance(value, logging.Logger):
self.install_guards(GuardBuilder.FUNCTION_MATCH)
return LoggingLoggerVariable(value, source=self.source)
elif is_utils_checkpoint(value):
return build_checkpoint_variable(source=self.source)
elif is_invoke_subgraph(value):
return build_invoke_subgraph_variable(source=self.source)
elif isinstance(value, functools.partial):
func_src = AttrSource(self.get_source(), "func")
func_obj = VariableBuilder(self.tx, func_src)(value.func)
args = []
args_source = AttrSource(self.get_source(), "args")
for i, arg in enumerate(value.args):
args.append(
VariableBuilder(self.tx, GetItemSource(args_source, i))(arg)
)
keywords = {}
keywords_source = AttrSource(self.get_source(), "keywords")
for k, v in value.keywords.items():
if not ConstantVariable.is_literal(k):
unimplemented("functools.partial with non-literal keyword")
keywords[k] = VariableBuilder(
self.tx, GetItemSource(keywords_source, k)
)(v)
install_guard(
self.get_source().make_guard(GuardBuilder.TYPE_MATCH),
keywords_source.make_guard(GuardBuilder.DICT_KEYS),
args_source.make_guard(GuardBuilder.SEQUENCE_LENGTH),
)
return FunctoolsPartialVariable(func_obj, args, keywords)
elif is_typing(value):
# typing.List, typing.Mapping, etc.
self.install_guards(GuardBuilder.ID_MATCH)
return TypingVariable(
value,
source=self.source,
)
elif np is not None and isinstance(value, np.generic):
# numpy array scalars: convert to 0D arrays
return self.wrap_numpy_ndarray(np.asarray(value))
elif is_numpy(value):
assert np
self.install_guards(
GuardBuilder.FUNCTION_MATCH
if callable(value)
else GuardBuilder.TYPE_MATCH
)
return NumpyVariable(value, source=self.source)
elif is_numpy_dtype(value):
self.install_guards(GuardBuilder.ID_MATCH)
return NumpyDTypeVariable(value, source=self.source)
elif is_numpy_type_info(value):
if isinstance(value, np.iinfo):
self.install_guards(GuardBuilder.TYPE_MATCH)
dt_source = AttrSource(self.source, "dtype")
install_guard(dt_source.make_guard(GuardBuilder.ID_MATCH))
else:
self.install_guards(GuardBuilder.ID_MATCH)
return NumpyTypeInfoVariable(value, source=self.source)
# NB: These can't be put in type_dispatch, they have to run later
elif CollectiveFunctionRewriteVariable.can_rewrite(value):
self.install_guards(GuardBuilder.FUNCTION_MATCH)
return CollectiveFunctionRewriteVariable.create(
self.tx,
value,
source=self.source,
)
elif istype(value, torch.autograd.function.FunctionMeta):
self.install_guards(GuardBuilder.FUNCTION_MATCH)
return AutogradFunctionVariable(
value,
source=self.source,
)
elif isinstance(value, torch.autograd.function.FunctionCtx):
actual_saved_tensors = None
try:
actual_saved_tensors = value.saved_tensors
except RuntimeError:
pass
saved_tensors = []
guards = [self.source.make_guard(GuardBuilder.TYPE_MATCH)]
if isinstance(actual_saved_tensors, tuple):
saved_tensors_source = AttrSource(self.source, "saved_tensors")
guards.append(
saved_tensors_source.make_guard(GuardBuilder.SEQUENCE_LENGTH)
)
for i, v in enumerate(actual_saved_tensors):
saved_tensors.append(
VariableBuilder(
self.tx, GetItemSource(saved_tensors_source, i)
)(v)
)
install_guard(*guards)
return self.tx.output.side_effects.track_object_existing(
value,
AutogradFunctionContextVariable(
value,
source=self.source,
saved_tensors=SavedTensorBox(saved_tensors),
),
)
elif (
isinstance(value, types.MethodType)
and istype(
getattr(value, "__self__", None), torch.autograd.function.FunctionMeta
)
and getattr(value, "__name__", "") == "apply"
and value == getattr(value.__self__, "apply", None)
):
# handle aliased autograd function `apply` calls
self.install_guards(GuardBuilder.FUNCTION_MATCH)
return GetAttrVariable(
AutogradFunctionVariable(
value.__self__, source=AttrSource(self.source, member="__self__")
),
"apply",
)
elif isinstance(value, torch._C._ImperativeEngine):
self.install_guards(GuardBuilder.ID_MATCH)
return AutogradEngineVariable(value, source=self.source)
elif (
value
is torch._dynamo.external_utils.FakeCompiledAutogradEngine._exec_final_callbacks_stub
):
self.install_guards(GuardBuilder.FUNCTION_MATCH)
return LambdaVariable(
lambda: UserFunctionVariable(
torch._dynamo.external_utils.FakeCompiledAutogradEngine.exec_final_callbacks,
).call_function(
self.tx,
(self.tx.output.side_effects.get_ca_final_callbacks_var(),),
{},
)
)
elif callable(value) and trace_rules.lookup_callable(value) is not None:
if is_callable_allowed(value):
self.tx.output.has_user_defined_allowed_in_graph = True
return trace_rules.lookup_callable(value).create_with_source(
value, source=self.source
)
elif np and isinstance(value, np.number):
return self.wrap_unspecialized_primitive(value)
elif HFPretrainedConfigVariable.is_matching_object(value):
self.install_guards(GuardBuilder.TYPE_MATCH)
return HFPretrainedConfigVariable(value)
elif isinstance(value, HigherOrderOperator):
if value is torch._higher_order_ops.invoke_subgraph:
unimplemented(
"Directly using invoke_subgraph is not supported. Use mark_compile_region"
)
self.install_guards(GuardBuilder.TYPE_MATCH, GuardBuilder.NAME_MATCH)
return TorchHigherOrderOperatorVariable.make(value, source=self.source)
elif isinstance(value, torch.cuda.StreamContext):
self.install_guards(GuardBuilder.ID_MATCH)
stream_source = AttrSource(self.source, "stream")
stream_var = VariableBuilder(self.tx, stream_source)(value.stream)
return StreamContextVariable.create(self.tx, stream_var)
elif isinstance(value, torch.Stream):
self.install_guards(GuardBuilder.ID_MATCH)
stream_proxy = self.tx.output.create_proxy(
"call_function",
type(value),
(),
{
"stream_id": value.stream_id,
"device_index": value.device_index,
"device_type": value.device_type,
},
)
set_example_value(stream_proxy.node, value)
return StreamVariable(
stream_proxy,
value,
value.device,
source=self.source,
)
elif isinstance(value, (torch._C._SDPAParams)):
self.install_guards(GuardBuilder.TYPE_MATCH)
return SDPAParamsVariable.create(self.tx, value, self.source)
elif isinstance(value, torch.Event):
self.install_guards(GuardBuilder.ID_MATCH)
torch._dynamo.utils.store_user_object_weakref(value)
event_proxy = self.tx.output.create_proxy(
"call_function",
torch._dynamo.utils.get_user_object_from_id,
(id(value),),
{},
)
set_example_value(event_proxy.node, value)
return EventVariable(
event_proxy,
value,
source=self.source,
)
elif (
isinstance(value, torch._C._TensorMeta)
and value in config.traceable_tensor_subclasses
):
return TensorSubclassVariable(value, source=self.source)
elif (
istype(value, contextlib.nullcontext)
and inspect.getattr_static(value, "enter_result", None) is None
):
self.install_guards(GuardBuilder.TYPE_MATCH)
return NullContextVariable(source=self.source)
elif KeyedJaggedTensorVariable.is_matching_object(value):
self.install_guards(GuardBuilder.TYPE_MATCH)
result = KeyedJaggedTensorVariable(value, source=self.source)
# TODO: this doing it manually is bad
return self.tx.output.side_effects.track_object_existing(value, result)
elif isinstance(value, torch.optim.Optimizer):
self.install_guards(GuardBuilder.ID_MATCH)
self.source = OptimizerSource(self.source)
return OptimizerVariable(value, source=self.source)
elif WorldMetaClassVariable.is_group_member_type(value):
return WorldMetaClassVariable(value, source=self.source)
elif ProcessGroupVariable.is_process_group(value):
self.install_guards(GuardBuilder.ID_MATCH)
return ProcessGroupVariable(value, source=self.source)
elif DeviceMeshVariable.is_device_mesh(value):
# TODO: see if we need to add custom guard instead of a simple ID_MATCH
self.install_guards(GuardBuilder.EQUALS_MATCH)
return DeviceMeshVariable(value, source=self.source)
elif PlacementClassVariable.is_placement_type(value):
# TODO: see if we need to add custom guard instead of a simple ID_MATCH
self.install_guards(GuardBuilder.ID_MATCH)
return PlacementClassVariable(value, source=self.source)
elif PlacementVariable.is_placement(value):
# TODO: see if we need to add custom guard instead of a simple ID_MATCH
self.install_guards(GuardBuilder.EQUALS_MATCH)
return PlacementVariable(
value,
source=self.source,
)
elif (
id(value) in ITERTOOLS_TYPE_IDS
and id(value) not in ITERTOOLS_POLYFILLED_TYPE_IDS
):
self.install_guards(GuardBuilder.FUNCTION_MATCH)
return ItertoolsVariable(value, source=self.source)
elif isinstance(value, torch.SymBool):
# Note: the idea here is to re-use the infra we've built for SymInt by simulating the
# user provided SymBool with a SymInt in dynamo.
# Concretely,
# 1. We create a SymInt in dynamo's shape_env, whose source is constructed as ConvertIntSource(self.source).
# so that guards on the SymInts can be effectively applied on the original SymBool in user program.
# 2. We create a SymBool based on the SymInt in dynamo's ShapeEnv. Because the original user program
# depends on the value being a SymBool. This allows dynamo to interpret the user's program correctly.
new_source = ConvertIntSource(self.source)
if value.node.has_hint():
value_hint = value.node.require_hint()
new_symint = (
self.tx.output.shape_env.create_unspecified_symint_and_symbol(
int(value_hint),
new_source,
dynamic_dim=DimDynamic.DYNAMIC,
)
)
else:
# We need to create an unbacked symint to replace the unbacked symbool.
new_symint = self.tx.output.shape_env.create_unbacked_symint()
sym_node_proxy = self.tx.output.root_tracer.create_graph_input(
re.sub(r"[^a-zA-Z0-9]+", "_", self.name),
type(new_symint),
new_symint,
source=new_source,
)
sym_node_proxy.node.meta["grapharg"] = GraphArg(
new_source,
new_symint,
False,
None,
is_tensor=False,
example_strong_ref=new_symint,
)
# We bind the new_symint to graph input.
sym_expr = new_symint.node.expr
assert isinstance(
sym_expr, sympy.Symbol
), f"{sym_expr} is not a basic Symbol."
self.tx.output.tracked_fakes.append(
TrackedFake(new_symint, new_source, None)
)
return SymNodeVariable(
sym_node_proxy,
new_symint == 1,
)
elif isinstance(value, (JITFunction, Autotuner)):
self.install_guards(GuardBuilder.ID_MATCH)
return TritonKernelVariable(
value,
None, # No kernel idx provided
None, # No grid provided
source=self.source,
)
elif value is create_1d_tma_descriptor:
return CreateTMADescriptorVariable(rank=1)
elif value is create_2d_tma_descriptor:
return CreateTMADescriptorVariable(rank=2)
elif isinstance(value, torch.amp.autocast_mode.autocast):
self.install_guards(GuardBuilder.ID_MATCH)
return AutocastModeVariable(
target_values=[
value.device,
value.fast_dtype,
value._enabled,
value._cache_enabled,
],
source=self.source,
)
elif TorchCtxManagerClassVariable.is_matching_cls(value):
self.install_guards(GuardBuilder.FUNCTION_MATCH)
return TorchCtxManagerClassVariable(value, source=self.source)
elif inspect.getattr_static(value, "__script_if_tracing_wrapper", False):
self.install_guards(GuardBuilder.TYPE_MATCH)
return WrapperUserFunctionVariable(
value, "__original_fn", source=self.source
)
elif is_lru_cache_wrapped_function(value):
self.install_guards(GuardBuilder.TYPE_MATCH)
return WrapperUserFunctionVariable(value, "__wrapped__", source=self.source)
elif is_function_or_wrapper(value) and inspect.getattr_static(
value, "_torchdynamo_inline", False
):
self.install_guards(GuardBuilder.TYPE_MATCH)
return WrapperUserFunctionVariable(
value, "_torchdynamo_inline", source=self.source
)
elif is_function_or_wrapper(value):
value, attr_name = unwrap_with_attr_name_if_wrapper(value)
# For these wrappers, Dynamo points to the wrapped function,
# so source needs to be updated as well.
if attr_name is not None:
self.source = AttrSource(self.source, attr_name)
return trace_rules.lookup(value).create_with_source(
value, source=self.source
)
elif value is random.Random:
self.install_guards(GuardBuilder.ID_MATCH)
return RandomClassVariable(source=self.source)
elif istype(value, random.Random) and RandomVariable.is_supported_random_obj(
value
):
self.install_guards(GuardBuilder.TYPE_MATCH)
result = RandomVariable(value, source=self.source)
self.tx.output.side_effects.track_mutable(value, result)
return result
# Don't use istype, since some python modules are not subclasses of types.ModuleType directly.
# E.g, type(torch.ops) -> <class 'torch._ops._Ops'>,
# type(torch.backends.cudnn) -> <class 'torch.backends.cudnn.CudnnModule'>
elif isinstance(value, (types.ModuleType, replay_record.DummyModule)):
self.install_guards(GuardBuilder.FUNCTION_MATCH)
result = PythonModuleVariable(
value,
source=self.source,
)
self.tx.output.side_effects.track_object_existing(value, result)
return result
elif isinstance(value, types.MethodType) and isinstance(
value.__self__, (torch.nn.Module, torch.utils._pytree.TreeSpec)
):
# don't let MethodTypes fall through to UserDefinedObject,
# which doesn't support 'CALL_FUNCTION'
# TODO(whc): Why do we limit this to methods on NNModules?
# I don't have a good reason for this, but it preserves the existing behavior
# for MBartForConditionalGeneration, which generates many graph breaks and OOMs otherwise.
# I suspect we probably want to relax this check and dig deeper there.
# In order to construct a MethodVariable in Dynamo, we start with an actual method obj from python,
# but need to separately wrap its underlying `__func__` and its `self` argument. We wrap `self` here
# and then `__func__` gets wrapped inside UserMethodVariable.
self_obj = VariableBuilder(
self.tx, source=AttrSource(self.source, "__self__")
)(value.__self__)
assert self_obj and isinstance(
self_obj, VariableTracker
), "Failed to produce a valid self obj"
self.install_guards(GuardBuilder.FUNCTION_MATCH)
return UserMethodVariable(
value.__func__,
self_obj,
source=self.source,
)
elif isinstance(value, types.GetSetDescriptorType):
# GetSet descriptors are C functions attached to an attribute lookup
# using PyGetSetDef. Python, on attribute lookup, can decide to
# create a new object on the fly, and therefore the `id` of the
# descriptors is not guaranteed to be same for different attribute
# accesses. Since these are unlikely to change during the program
# execution, we can skip guarding on them.
return GetSetDescriptorVariable(value)
elif isinstance(value, types.MethodWrapperType):
# Method-wrappers are written in C, and they are not guaranteed to
# return the same object on attribute lookup. Therefore, we cannot
# insert a FUNCTION_MATCH guard here. method-wrappers are very
# unlikely to change, so its ok to skip the guard here.
return MethodWrapperVariable(value)
elif issubclass(type(value), type):
if value in (
torch.utils.hooks.BackwardHook,
torch.nn.Parameter,
torch.nn.Buffer,
):
# TODO(jansel): combine this case with the one above
return trace_rules.lookup(value).create_with_source(
value, source=self.source
)
if value is torch.autograd._unsafe_preserve_version_counter:
self.install_guards(GuardBuilder.FUNCTION_MATCH)
return PreserveVersionContextVariable.constructor(self.tx)
# This is a userdefined class, so install an ID_MATCH even if its a
# global variable.
self.install_guards(GuardBuilder.ID_MATCH)
return UserDefinedClassVariable(
value,
source=self.source,
)
elif RestrictedListSubclassVariable.is_matching_cls(type(value)):
self.install_guards(GuardBuilder.SEQUENCE_LENGTH)
return self.tx.output.side_effects.track_mutable(
value,
RestrictedListSubclassVariable(
[
LazyVariableTracker.create(
value=value[i], source=GetItemSource(self.source, i)
)
for i in range(len(value))
],
user_cls=type(value),
user_cls_source=AttrSource(self.source, "__class__"),
source=self.source,
),
)
elif TorchScriptObjectVariable.is_matching_cls(type(value)):
from ..source import (
FlattenScriptObjectSource,
ScriptObjectQualifiedNameSource,
)
if torch._library.fake_class_registry.tracing_with_real(value):
proxy = self.tx.output.root_tracer.create_graph_input(
re.sub(r"[^a-zA-Z0-9]+", "_", self.name),
type(value),
value,
source=self.source,
)
# setting is_unspecialized=False to not insert a as_tensor call in reconstruct by default
# seting example to be real value because these example values will be used
# as example_inputs for user compiler.
proxy.node.meta["grapharg"] = GraphArg(
self.source, value, False, None, False, value
)
return TorchScriptObjectVariable.create(
proxy,
value,
source=self.source,
)
# This exists to allow a smoother transition.
# The implications are:
# The script objects won't be tracked as proxies.
# Methods on these objects won't show up in the graph.
# The original script object might be mutated.
if not hasattr(value, "__obj_flatten__"):
return self.wrap_user_defined(value)
# Install the guards on the fully qualified name of the script object
LazyVariableTracker.realize_all(
VariableBuilder(self.tx, ScriptObjectQualifiedNameSource(self.source))(
value._type().qualified_name() # type: ignore[attr-defined]
)
)
# Install the guards on the content of the script object by setting the source
# to be FlattenScriptObjectSource, which calls __obj_flatten__() to get the contents.
LazyVariableTracker.realize_all(
VariableBuilder(self.tx, FlattenScriptObjectSource(self.source))(
value.__obj_flatten__()
)
)
fake_script_obj = torch._library.fake_class_registry.maybe_to_fake_obj(
self.tx.output.fake_mode, value
)
proxy = self.tx.output.root_tracer.create_graph_input(
re.sub(r"[^a-zA-Z0-9]+", "_", self.name),
type(value),
fake_script_obj,
source=self.source,
)
# setting is_unspecialized=False to not insert a as_tensor call in reconstruct by default
# seting example to be real value because these example values will be used
# as example_inputs for user compiler.
proxy.node.meta["grapharg"] = GraphArg(
self.source, value, False, None, False, fake_script_obj
)
return TorchScriptObjectVariable.create(
proxy,
fake_script_obj,
source=self.source,
)
elif issubclass(type(value), MutableMapping):
self.install_guards(GuardBuilder.TYPE_MATCH)
return MutableMappingVariable(value, source=self.source)
elif is_frozen_dataclass(value):
self.install_guards(GuardBuilder.TYPE_MATCH)
result = FrozenDataClassVariable.create(self.tx, value, source=self.source)
return self.tx.output.side_effects.track_object_existing(value, result)
else:
return self.wrap_user_defined(value)
def wrap_user_defined(self, value: Any):
self.install_guards(GuardBuilder.TYPE_MATCH)
result = UserDefinedObjectVariable(value, source=self.source)
if not SideEffects.cls_supports_mutation_side_effects(type(value)):
# don't allow STORE_ATTR mutation with custom __setattr__
return result
return self.tx.output.side_effects.track_object_existing(value, result)
def wrap_listlike(self, value: Union[tuple, list, odict_values, NamedTuple]):
if config.specialize_int and type(value) is torch.Size:
self.install_guards(GuardBuilder.CONSTANT_MATCH)
return ConstantVariable.create(value=value)
# One can index a tensor with a list/tuple. Therefore, we need to
# have a stricter match.
self.install_guards(GuardBuilder.SEQUENCE_LENGTH)
# Tuples are immutable objects, so we should mark its items static. This
# avoids wrapping of tuple items as symints. This helps for nn module
# attributes like conv2d strides, dilations.
if (
istype(value, tuple)
and all(ConstantVariable.is_literal(item) for item in value)
and self.source.guard_source().is_unspecialized_nn_module()
):
self.install_guards(GuardBuilder.CONSTANT_MATCH)
return TupleVariable([ConstantVariable.create(item) for item in value])
output = [
LazyVariableTracker.create(
item,
source=GetItemSource(self.get_source(), i),
)
for i, item in enumerate(value)
]
maybe_gm = self.tx.output.local_scope.get("self")
if isinstance(
self.source, LocalSource
) and self.source.local_name in get_locals_to_steal(maybe_gm):
# The input tensor list to dynamo from compiled autograd may contain activations
# which are freed as they are used in inductor. Dynamo's default behavior is to
# lift all tensors to the graph inputs, but this will cause dynamo to hold an
# extra reference to the activation tensors and increase peak memory usage.
# To allow freeing ASAP, we keep the list as graph argument to the dynamo output
# graph, and unpack it locally.
# e.g. instead of `def forward(self, L_inputs_0_, L_inputs_1_, ...):`, we have
# `def forward(self, L_inputs_):`
source = self.source
assert isinstance(value, list)
tensor_list_proxy = self.tx.output.root_tracer.create_graph_input(
re.sub(r"[^a-zA-Z0-9]+", "_", self.name),
type(value),
value,
source=source,
)
tensor_list_proxy.node.meta["steal_arg"] = True
list_variable = wrap_fx_proxy_cls(
target_cls=TensorVariable,
tx=self.tx,
proxy=tensor_list_proxy,
example_value=value,
subclass_type=None,
source=source,
)
guards = []
for i, tensor_variable in enumerate(list_variable.items):
source_i = GetItemSource(base=source, index=i, index_is_slice=False)
# access unpacked tensor from this list instead of from a lifted arg
self.tx.output.input_source_to_var[source_i] = tensor_variable
tensor_variable.proxy.node.meta["tensor_dict"] = _extract_tensor_dict(
value[i]
)
guard = functools.partial(
GuardBuilder.TENSOR_MATCH, value=TensorWeakRef(value[i])
)
guards.append(source_i.make_guard(guard))
install_guard(*guards, skip=1)
grapharg = GraphArg(
source,
value,
pass_arg_as_tensor=False,
fake_tensor=None,
is_tensor=False,
)
tensor_list_proxy.node.meta["grapharg"] = grapharg
result = BaseListVariable.cls_for_instance(value)(output, source=self.source)
if istype(value, (list, collections.deque)):
return self.tx.output.side_effects.track_mutable(value, result)
return result
def wrap_tuple_iterator(self, value: tuple_iterator):
self.install_guards(GuardBuilder.TUPLE_ITERATOR_LEN)
output = [
VariableBuilder(self.tx, TupleIteratorGetItemSource(self.get_source(), i))(
tuple_iterator_getitem(value, i)
)
for i in range(tuple_iterator_len(value))
]
result = TupleIteratorVariable(output, source=self.source)
return self.tx.output.side_effects.track_mutable(value, result)
def wrap_range_iterator(self, value: range_iterator):
self.install_guards(GuardBuilder.RANGE_ITERATOR_MATCH)
# Get all the values from the range iterator; no need to install guards
# on items since `RANGE_ITERATOR_MATCH` guarantees the same items.
items = [ConstantVariable.create(v) for v in copy.deepcopy(value)]
result = ListIteratorVariable(items, source=self.source)
return self.tx.output.side_effects.track_mutable(value, result)
def wrap_slice_range(self, value: Union[slice, range]):
items = [
VariableBuilder(self.tx, AttrSource(self.get_source(), k))(
getattr(value, k)
)
for k in ("start", "stop", "step")
]
self.install_guards(GuardBuilder.TYPE_MATCH)
if isinstance(value, slice):
return SliceVariable(items, source=self.source)
else:
return RangeVariable(items, source=self.source)
def mark_static_input(self, value: torch.Tensor, guard: bool):
from ..decorators import mark_static_address
static_inputs_log.debug(
"Marking static input %s, id: %s)", self.source.name(), id(value)
)
mark_static_address(value, guard=guard)
# Check if we've seen this tensor before and update graph metadata if needed
# As long as this runs before AOT this is sound
if value in self.tx.output.side_effects:
var = self.tx.output.side_effects[value]
var.proxy.node.meta["tensor_dict"][
"_dynamo_static_input_type"
] = value._dynamo_static_input_type
def wrap_module(self, value: torch.nn.Module):
from ..eval_frame import OptimizedModule
if len(value.__dict__) == 0:
unimplemented(f"uninitialized nn.Module: {typestr(value)}")
if istype(value, OptimizedModule):
# Check if the optimized module was disabled
if inspect.getattr_static(value.forward, "_torchdynamo_disable", False):
# This bytecode is mostly of kind LOAD_ATTR or LOAD_METHOD. If
# we graph break here, Dynamo does not know how to create
# continuation functions for such bytecodes. So, we delay the
# graph break to CALL_FUNCTION.
return DelayGraphBreakVariable(source=self.source)
self.install_guards(GuardBuilder.TYPE_MATCH)
self.source = AttrSource(self.source, "_orig_mod")
return self.wrap_module(value._orig_mod)
if (
isinstance(value, (torch.nn.RNN, torch.nn.GRU, torch.nn.LSTM))
and not config.allow_rnn
):
unimplemented("TorchDynamo purposely graph breaks on RNN, GRU, LSTMs")
if getattr(value, "_is_fsdp_managed_module", False):
# See note [Dynamo treats FSDP wrapped modules as UnspecializedNNModule]
# in fully_sharded_data_parallel.py for more information
# we can't do this assert inside FSDP constructor,
# since we don't know yet whether dynamo will be used
assert getattr(
value, "_fsdp_use_orig_params", False
), "Dynamo only supports FSDP with use_orig_params=True"
# Note on FSDP guarding
# Eager FSDP already assumes (requires, but without enforcement)
# that users don't mutate their model parameters/structure after
# FSDP wrapping, because FSDP wouldn't notice or update its
# FlatParams.
#
# Therefore, torch.compile can skip guarding on params or submodule
# structure of fsdp_managed modules, by using FSDPNNModuleSource as
# the guard source. This behavior is gated on
# config.skip_fsdp_guards.
self.install_guards(GuardBuilder.TYPE_MATCH)
result = FSDPManagedNNModuleVariable(value, source=self.get_source())
if not SideEffects.cls_supports_mutation_side_effects(type(value)):
# don't allow STORE_ATTR mutation with custom __setattr__
return result
return self.tx.output.side_effects.track_object_existing(value, result)
elif mutation_guard.is_dynamic_nn_module(value, self.tx.export):
# created dynamically, don't specialize on it
# Note [Tracing a torch.compiled function]
# when make_fx tracing a compiled function, we need
if isinstance(value, torch.fx.experimental.proxy_tensor._AttrProxy):
value = value.get_base()
self.source = AttrProxySource(self.source)
self.install_guards(GuardBuilder.TYPE_MATCH)
if torch._dynamo.config.inline_inbuilt_nn_modules:
freezing = is_parameter_freezing()
for p in value.parameters():
self.mark_static_input(p, guard=freezing)
for b in value.buffers():
self.mark_static_input(b, guard=freezing)
if freezing:
# we need to add the module to tracing context
# in order to allow its params to get invalidated
# this will get cleaned up once compile ends
self.tx.output.nn_modules[self.name] = value
if value.__module__.startswith(("torch.nn.", "torch.ao.")) or getattr(
value.__class__, "_dynamo_marked_static", False
):
result = UnspecializedBuiltinNNModuleVariable(value, source=self.source)
else:
result = UnspecializedNNModuleVariable(value, source=self.source)
if not SideEffects.cls_supports_mutation_side_effects(type(value)):
# don't allow STORE_ATTR mutation with custom __setattr__
return result
return self.tx.output.side_effects.track_object_existing(value, result)
elif issubclass(
value.__class__, torch.nn.parallel.distributed.DistributedDataParallel
):
self.install_guards(GuardBuilder.TYPE_MATCH)
return UnspecializedNNModuleVariable(value, source=self.get_source())
else:
return self.tx.output.register_attr_or_module(
value,
self.name,
source=self.get_source(),
# Guards are added inside register_attr_or_module
)
def wrap_literal(self, value):
if not config.specialize_int and type(value) is int:
# unspecializing int by default, but still
# specialize for the following conditions
if not TracingContext.get().force_unspec_int_unbacked_size_like and (
# Assume integers from global variables want to be specialized
not self.source.guard_source().is_local()
# Assume that integers that came from NN modules want to be
# specialized (as we don't expect users to be changing the
# NN modules on the fly)
or self.source.guard_source().is_specialized_nn_module()
or self.source.guard_source().is_unspecialized_builtin_nn_module()
or is_from_defaults(self.source)
# TODO: Delete this condition when rollout is done. NB: this
# condition never evaluates True in open source
or (
not justknobs_check(
"pytorch/dynamo:enable_unspecialize_zero_one_plain_int"
)
and value in self._common_constants()
)
):
self.install_guards(GuardBuilder.CONSTANT_MATCH)
return ConstantVariable.create(value=value, source=self.source)
else:
return self.wrap_symint(value)
elif not config.specialize_float and type(value) is float:
return self.wrap_symfloat(value)
else:
self.install_guards(GuardBuilder.CONSTANT_MATCH)
result = ConstantVariable.create(value=value, source=self.source)
if isinstance(value, (list, set)):
return self.tx.output.side_effects.track_mutable(value, result)
return result
def assert_not_wrapped_by_this_graph(self, value: torch.Tensor):
if is_fake(value) and maybe_get_fake_mode(value) is self.tx.fake_mode:
raise InternalTorchDynamoError(
"Cannot wrap a Tensor that has already been",
"wrapped by this instance of Dynamo",
)
def wrap_tensor(self, value: torch.Tensor):
source = self.get_source()
# We cannot already be tracking the tensor, which implies
# it would have already been wrapped
assert value not in self.tx.output.side_effects
is_static_input = get_static_address_type(value) is not None
if (
config.inline_inbuilt_nn_modules
and not is_static_input
and (
isinstance(value, torch.nn.Parameter)
# mark tensor attributes of nn modules static. This is done to keep inline_inbuilt_nn_modules behavior
# compatible with previous behavior.
or (source and source.guard_source().is_unspecialized_nn_module())
)
):
self.mark_static_input(value, guard=is_parameter_freezing())
is_static_input = True
make_graph_attribute = is_static_input and (
not config.inline_inbuilt_nn_modules or is_parameter_freezing()
)
if (
source.guard_source().is_specialized_nn_module() or make_graph_attribute
) and not source.guard_source().is_fsdp_module():
self.assert_not_wrapped_by_this_graph(value)
return self.tx.output.register_attr_or_module(
value, self.name, source=source
)
if is_constant_source(source):
self.assert_not_wrapped_by_this_graph(value)
return self.tx.output.register_attr_or_module(
value,
re.sub(r"[^a-zA-Z0-9]+", "_", self.name),
source=source,
# Guards are added inside register_attr_or_module
)
if type(value) in config.traceable_tensor_subclasses:
# Ordinarily, we would fakeify a tensor so that it can get dynamic
# shapes and be computed on without triggering actual operations.
# However, how can we fakeify a tensor subclass? Ordinary
# inheritance (nor multiple inheritance) won't work work.
#
# Instead, our plan is to *manually simulate* the tensor subclass
# inheriting from a fake tensor with dynamo. This means our
# data representation for a tensor subclass will be a fake tensor
# + tensor subclass type + any extra data the subclass may have
# been storing on the tensor. Because all Python accesses are
# mediated through TensorWithTFOverrideVariable, we can ensure
# that we dispatch differently, e.g., according to
# __torch_function__
#
# To simplify things for now, the __dict__ tracking bits haven't
# been implemented yet, but they can be added into this design at
# a later point in time.
subclass_type = type(value)
else:
assert type(value) in (
torch.Tensor,
torch.nn.Parameter,
torch._subclasses.fake_tensor.FakeTensor,
torch._subclasses.functional_tensor.FunctionalTensor,
) or is_traceable_wrapper_subclass(value), type(value)
subclass_type = None
# NB: this just says we accessed a tensor from the same source again
# (e.g., a tensor lives in a global foo, and we LOAD_GLOBAL it twice).
# This is distinct from two distinct sources mapping to the same
# Tensor (per id())! No guard is necessary here. See below for the
# other case.
is_duplicate_tensor = source in self.tx.output.input_source_to_var
if is_duplicate_tensor:
return self.tx.output.input_source_to_var[source]
if get_static_address_type(value) == "guarded":
self.install_guards(GuardBuilder.ID_MATCH)
# By this point, we should have deduplicated all tensors
self.assert_not_wrapped_by_this_graph(value)
options = {}
if type(value) in config.traceable_tensor_subclasses:
options["torch_function_fn"] = build_torch_function_fn(
self.tx, value, self.source
)
self.install_guards(GuardBuilder.TYPE_MATCH)
if (
isinstance(value, torch.Tensor)
and value.is_nested
and not isinstance(value, torch.nested._internal.nested_tensor.NestedTensor)
):
unimplemented("torch.compile does not support strided NestedTensor")
# TODO(pearu,sparse-team) - Add the corresponding SPARSE_TENSOR_MATCH guards
if (
isinstance(value, torch.Tensor)
and is_sparse_any(value)
and (not self.tx.export or not config.capture_sparse_compute)
):
# A hot fix for sparse tensors + torch.compile. Support for
# export + sparsity is being added but we need to create
# SPARSE_TENSOR_GUARDS for guards to work propertly.
unimplemented("torch.compile does not support sparse Tensors")
if (
safe_has_grad(value)
and safe_grad(value) is not None
and value.dtype != safe_grad(value).dtype
):
unimplemented(
"Inconsistent dtype between tensor and its gradient. "
"This can happen in FSDP and crashes meta tensor creation. "
"This is potentially a workaround. Fixing it correctly "
"requires some design around FSDP + torch.compile."
)
# tx.output has multiple tracers if we're introspecting HigherOrderOperator.
# When we've discovered an untracked tensor, then we actually need
# to get Dynamo to track the tensor (which is what this function does)
# and put it as a graph input on the root tracer. Later on,
# if the input is actually used in the body of the HigherOrderOperator,
# then the relevant SubgraphTracer will lift it to being an input of
# the subgraph.
# See NOTE [HigherOrderOperator tracing design] for more details.
example_value = wrap_to_fake_tensor_and_record(
value, tx=self.tx, is_tensor=True, source=source
)
tensor_proxy = self.tx.output.root_tracer.create_graph_input(
re.sub(r"[^a-zA-Z0-9]+", "_", self.name),
type(value),
example_value,
source=source,
)
cache_real_value_when_export(self.tx, tensor_proxy, value)
tensor_variable = wrap_fx_proxy(
tx=self.tx,
proxy=tensor_proxy,
example_value=example_value,
subclass_type=subclass_type,
source=source,
**options,
)
if value._is_view():
# If value is a view, add its base tensor to the tracked fakes list.
# This is so we are able to access the correct source for its symbolic
# shape values, in case we need them.
wrap_to_fake_tensor_and_record(
value._base,
tx=self.tx,
source=AttrSource(source, "_base"),
is_tensor=True,
)
guard_type = GuardBuilder.TENSOR_MATCH
if isinstance(source, GradSource) and is_from_optimizer_source(source):
guard_type = GuardBuilder.NOT_NONE_MATCH
self.install_guards(
functools.partial(
guard_type,
value=(
value
if isinstance(source, NumpyTensorSource)
else TensorWeakRef(value)
),
)
)
# We install TYPE_MATCH guards for traceable wrapper subclass object,
# and recursively install corresponding guard for each inner attribute.
if is_traceable_wrapper_subclass(value):
self.install_guards(GuardBuilder.TENSOR_SUBCLASS_METADATA_MATCH)
self.install_guards(GuardBuilder.TYPE_MATCH)
install_guard(
SubclassAttrListSource(source).make_guard(GuardBuilder.EQUALS_MATCH)
)
attrs, _ = value.__tensor_flatten__()
for attr in attrs:
inner_value = getattr(value, attr)
inner_source = AttrSource(self.source, attr)
LazyVariableTracker.realize_all(
VariableBuilder(self.tx, inner_source)(inner_value)
)
self.tx.output.input_source_to_var[source] = tensor_variable
assert "tensor_dict" not in tensor_proxy.node.meta
tensor_proxy.node.meta["tensor_dict"] = _extract_tensor_dict(value)
# Note: this information is conveyed via subclass_type now
fake_tensor_value = tensor_variable.proxy.node.meta["example_value"]
if maybe_get_fake_mode(fake_tensor_value) is not self.tx.fake_mode:
raise InternalTorchDynamoError("Wrapped Tensor must be this graph's fake")
grapharg = GraphArg(source, value, False, fake_tensor_value)
tensor_proxy.node.meta["grapharg"] = grapharg
return tensor_variable
def wrap_numpy_ndarray(self, value):
assert np is not None
assert isinstance(value, np.ndarray)
source = NumpyTensorSource(self.get_source())
from torch._numpy import _util
readonly = not value.flags.writeable
if readonly:
try:
value.flags.writeable = True
except ValueError:
# One can not easily make nditer elements writable,
# but warning is not the end of the world
assert isinstance(value.base, np.nditer)
with torch_function_mode_stack_state_mgr.temp_restore_stack():
try:
tensor_value = _util._try_convert_to_tensor(value)
if readonly:
from torch._prims_common import clone_preserve_strides
tensor_value = clone_preserve_strides(tensor_value)
except NotImplementedError as e:
# failed to convert to tensor, graph break
unimplemented(str(e))
# We do this because we want the full behavior of guarding the numpy ndarray as if it were
# a tensor. It's a little annoying to make a VT to throw out, but there's so many side effects here
# that there's not another great way to do this atm.
# This creates the right graphargs, as well as registration for guards in tensor names and shape env.
LazyVariableTracker.realize_all(VariableBuilder(self.tx, source)(tensor_value))
example_value = wrap_to_fake_tensor_and_record(
tensor_value,
tx=self.tx,
is_tensor=False,
source=source,
)
proxy = self.tx.output.root_tracer.create_graph_input(
re.sub(r"[^a-zA-Z0-9]+", "_", self.name),
type(tensor_value),
example_value,
source=source,
)
cache_real_value_when_export(self.tx, proxy, tensor_value)
options = {"source": source}
numpy_ndarray_variable = wrap_fx_proxy_cls(
target_cls=NumpyNdarrayVariable,
tx=self.tx,
proxy=proxy,
example_value=example_value,
**options,
)
self.tx.output.input_source_to_var[source] = numpy_ndarray_variable
example_value = numpy_ndarray_variable.proxy.node.meta["example_value"]
# pass_arg_as_tensor should be true because we are wrapping a np.ndarray as argument input, and it needs to be
# converted to a tensor.
grapharg = GraphArg(
source,
tensor_value,
pass_arg_as_tensor=True,
fake_tensor=example_value,
is_tensor=True,
example_strong_ref=tensor_value,
)
proxy.node.meta["grapharg"] = grapharg
return numpy_ndarray_variable
def wrap_symint(self, value):
assert type(value) is int
if self.name in self.tx.output.unspec_variable_map:
return self.tx.output.unspec_variable_map[self.name]
shape_env = self.tx.output.shape_env
if TracingContext.get().force_unspec_int_unbacked_size_like:
wrapped_value = shape_env.create_unbacked_symint()
_constrain_range_for_size(wrapped_value)
self.tx.output.tracked_fakes.append(
TrackedFake(wrapped_value, self.source, None)
)
# NB: We do not do float. For motivation, see
# https://docs.google.com/document/d/1INSCdYu1PxXcr43HrD82OudeEuS-qxQe1yZmLg2wy6A/edit
# but the general idea is that we generate kernels that can
# take unspecialized floats and use them in sizevar computation
elif not is_constant_source(self.get_source()):
if torch._dynamo.config.specialize_int:
# If specialize_int is False, also return
# a constant (but this should have been handled
# in the caller, TBH)
self.install_guards(GuardBuilder.CONSTANT_MATCH)
return ConstantVariable.create(value=value, source=self.source)
name = self.source.name()
frame_state_entry = process_automatic_dynamic(
self.tx,
name,
FrameStateSizeEntry.make_scalar(value),
is_unspecialized_nn_module=self.source.guard_source().is_unspecialized_nn_module(),
)
# TODO: This should be dynamic, as we in general do not
# know if bare integers are actually going to be sizevars
# and it is inappropriate to eagerly duck size them with
# real sizevars
if (
config.automatic_dynamic_shapes
and frame_state_entry.scalar is auto_dynamic
):
dynamic_dim = get_automatic_dynamic_shapes_mark_as()
elif not config.assume_static_by_default:
dynamic_dim = DimDynamic.DYNAMIC
else: # assume_static_by_default
# TODO: dynamic_dim = DimDynamic.STATIC should work but
# for some reason it doesn't
self.install_guards(GuardBuilder.CONSTANT_MATCH)
return ConstantVariable.create(value=value)
wrapped_value = shape_env.create_unspecified_symint_and_symbol(
value,
source=self.source,
dynamic_dim=dynamic_dim,
)
self.tx.output.tracked_fakes.append(
TrackedFake(wrapped_value, self.source, None)
)
else:
assert is_constant_source(self.get_source())
# TODO: Do I actually need guard for constant source?
self.install_guards(GuardBuilder.CONSTANT_MATCH)
return ConstantVariable.create(value=value, source=self.source)
assert not isinstance(self.get_source(), RandomValueSource)
install_guard(self.get_source().make_guard(GuardBuilder.TYPE_MATCH))
options = {"source": self.get_source()}
proxy = self.tx.output.root_tracer.create_graph_input(
re.sub(r"[^a-zA-Z0-9]+", "_", self.name),
type(wrapped_value),
wrapped_value,
source=self.get_source(),
)
sym_expr = wrapped_value.node.expr
assert isinstance(sym_expr, sympy.Symbol), f"{sym_expr} is not a basic Symbol."
self.tx.output.root_tracer.bound_symbols[sym_expr] = proxy
unspec_var = SymNodeVariable(proxy, wrapped_value, **options)
self.tx.output.unspec_variable_map[self.name] = unspec_var
if not is_constant_source(self.get_source()):
if self.tx.export and not isinstance(self.get_source(), LocalSource):
raise AssertionError(
f"Dynamo attempts to add additional input during export: value={wrapped_value}, source={self.get_source()}"
)
example_value = unspec_var.proxy.node.meta["example_value"]
proxy.node.meta["grapharg"] = GraphArg(
self.get_source(),
wrapped_value,
pass_arg_as_tensor=False,
fake_tensor=None,
is_tensor=False,
example_strong_ref=wrapped_value,
)
return unspec_var
def wrap_symfloat(self, value):
# To prevent circular import
from ..symbolic_convert import TensorifyState
# SymFloat wrapping is special. We first wrap it in the same way we
# do an unspecialized primitive, and then we item() it into a
# SymFloat. Removal of the item() call is left to a later FX pass,
# mostly because that pass is more easily done after we have lowered
# to ATen ops. (Dynamo doesn't do decomposition right now).
if self.name in self.tx.output.unspec_variable_map:
return self.tx.output.unspec_variable_map[self.name]
frame_state_entry = process_automatic_dynamic(
self.tx,
self.source.name(),
FrameStateSizeEntry.make_scalar(value),
is_unspecialized_nn_module=self.source.guard_source().is_unspecialized_nn_module(),
)
# NB: we specialize on nan input, because our guard modeling in
# ShapeEnv cannot deal with nan
if (
torch._dynamo.config.specialize_float
or is_constant_source(self.get_source())
or math.isnan(value)
or math.isinf(value)
# We don't support cudagraphs for now. Without this cudagraphs
# break because they expect all cuda inputs but our tensorified
# float will be a f64[] cpu tensor. Fixes the following test
# when specialize_float=False
# python test/inductor/test_compiled_optimizers.py CompiledOptimizerTests.test_rmsprop_weight_decay_maximize_capturable_cuda # noqa: B950
or torch._inductor.config.triton.cudagraphs
or justknobs_check("pytorch/compiler:unspecialize_float_killswitch", False)
or frame_state_entry.scalar is not auto_dynamic
or TensorifyState.should_specialize(self.source)
):
self.install_guards(GuardBuilder.CONSTANT_MATCH)
return ConstantVariable.create(value=value, source=self.source)
# NB: At the point we've gotten here, we don't assume static by
# default. Since we have a guard mechanism, there isn't really any
# downside to trying to be dynamic for float all the time. Unlike
# ints, this won't make codegen perf worse. Modest cost to compile
# time.
wrapped_value = torch.tensor(value, dtype=torch.float64)
# We don't support specializing floats for grad checking tensors
# See https://github.com/pytorch/pytorch/pull/140828 for more
# context.
if torch._C._functorch.is_gradtrackingtensor(wrapped_value):
self.install_guards(GuardBuilder.CONSTANT_MATCH)
return ConstantVariable.create(value=value, source=self.source)
# TODO: Switch RandomValueSource over to use this, this is more
# accurate
assert not isinstance(self.get_source(), RandomValueSource)
install_guard(self.get_source().make_guard(GuardBuilder.TYPE_MATCH))
# The FloatTensorSource here is just for pedantic correctness: if you
# guard against an UnspecializedPythonVariable, you need to guard
# against the tensor-ified version of the local, otherwise it's not a
# Tensor. However, we never let the UnspecializedPythonVariable escape
# here, so there should never actually be any guards against this
# source.
source = FloatTensorSource(self.get_source())
options = {"source": source, "raw_value": value}
# TODO: Maybe the tensor-ification should be built into the source,
# rather than by special pattern match
example_value = wrap_to_fake_tensor_and_record(
wrapped_value, tx=self.tx, is_tensor=False, source=source
)
proxy = self.tx.output.root_tracer.create_graph_input(
re.sub(r"[^a-zA-Z0-9]+", "_", self.name),
type(wrapped_value),
example_value,
source=source,
)
cache_real_value_when_export(self.tx, proxy, wrapped_value)
unspec_var = wrap_fx_proxy_cls(
UnspecializedPythonVariable,
tx=self.tx,
proxy=proxy,
example_value=example_value,
**options,
)
assert isinstance(unspec_var, UnspecializedPythonVariable)
self.tx.output.unspec_variable_map[self.name] = unspec_var
if self.tx.export and not isinstance(self.get_source(), LocalSource):
raise AssertionError(
f"Dynamo attempts to add additional input during export: value={wrapped_value}, source={self.get_source()}"
)
fake_tensor_value = None
example_value = unspec_var.proxy.node.meta["example_value"]
assert is_fake(example_value)
fake_tensor_value = example_value
assert fake_tensor_value.fake_mode is self.tx.fake_mode, (
f"fake mode ({fake_tensor_value.fake_mode}) from fake tensor metadata doesn't match mode"
"({self.tx.fake_mode}) from InstructionTranslator"
)
# There's something a bit incoherent about pass_arg_as_tensor,
# specifically regarding sources.
#
# Specifically, suppose we have "x: float" local argument. We
# eventually end up with an UnspecializedPythonVariable denoting
# torch.as_tensor(x)... but it's source is still L['x'] (which if you
# accessed it directly is a float!) So you gotta be careful when
# setting up your guards, because it's still going to be a float at
# this point, the conversion happens only precisely at the point we're
# actually calling the FX graph. This happens to be what we want for
# shape guard generation, but it's kind of unintuitive.
proxy.node.meta["grapharg"] = GraphArg(
self.get_source(),
wrapped_value,
pass_arg_as_tensor=True,
fake_tensor=fake_tensor_value,
is_tensor=False,
example_strong_ref=wrapped_value,
)
# Directly do item to bypass capture_scalar_outputs
r = wrap_fx_proxy(
self.tx,
self.tx.output.create_proxy(
"call_method",
"item",
*proxy_args_kwargs([unspec_var], {}),
),
)
self.tx.output.tracked_fakes.append(TrackedFake(r.sym_num, self.source, None))
return r
def wrap_unspecialized_primitive(self, value):
if self.name in self.tx.output.unspec_variable_map:
return self.tx.output.unspec_variable_map[self.name]
wrapped_value = torch.tensor(value)
if not isinstance(self.get_source(), RandomValueSource):
install_guard(self.get_source().make_guard(GuardBuilder.TYPE_MATCH))
options = {"source": self.get_source()}
options.update({"raw_value": value})
example_value = wrap_to_fake_tensor_and_record(
wrapped_value, tx=self.tx, is_tensor=False, source=self.get_source()
)
proxy = self.tx.output.root_tracer.create_graph_input(
re.sub(r"[^a-zA-Z0-9]+", "_", self.name),
type(wrapped_value),
example_value,
source=self.get_source(),
)
cache_real_value_when_export(self.tx, proxy, wrapped_value)
unspec_var = wrap_fx_proxy_cls(
UnspecializedPythonVariable,
tx=self.tx,
proxy=proxy,
example_value=example_value,
**options,
)
self.tx.output.unspec_variable_map[self.name] = unspec_var
if not is_constant_source(self.get_source()):
if self.tx.export and not isinstance(self.get_source(), LocalSource):
raise AssertionError(
f"Dynamo attempts to add additional input during export: value={wrapped_value}, source={self.get_source()}"
)
fake_tensor_value = None
if isinstance(unspec_var, ConstantVariable):
# TODO: when can this happen?
example_value = unspec_var.value
else:
example_value = unspec_var.proxy.node.meta["example_value"]
assert is_fake(example_value)
fake_tensor_value = example_value
assert fake_tensor_value.fake_mode is self.tx.fake_mode, (
f"fake mode ({fake_tensor_value.fake_mode}) from fake tensor metadata doesn't match mode"
"({self.tx.fake_mode}) from InstructionTranslator"
)
proxy.node.meta["grapharg"] = GraphArg(
self.get_source(),
wrapped_value,
pass_arg_as_tensor=True,
fake_tensor=fake_tensor_value,
is_tensor=False,
example_strong_ref=wrapped_value,
)
return unspec_var
def _dataclasses_fields_lambda(obj):
if isinstance(obj, UserDefinedObjectVariable):
value = obj.value
elif isinstance(obj, CustomizedDictVariable):
value = obj.user_cls
else:
unimplemented(f"Dataclass fields handling fails for type {obj}")
items = []
for field in dataclasses.fields(value):
source = None
if obj.source:
source = GetItemSource(
AttrSource(obj.source, "__dataclass_fields__"), field.name
)
items.append(UserDefinedObjectVariable(field, source=source))
return TupleVariable(items)
def _clone_input(value, fake_mode):
if isinstance(value, torch.Tensor):
# tensor subclasses will not be converted to FakeTensors and need to be cloned
if not (
isinstance(value, FakeTensor)
or (
# Is functional tensor fakeified by this instance of Dynamo
torch._is_functional_tensor(value)
and maybe_get_fake_mode(value) is fake_mode
)
or value.is_nested
):
# NB: ensure strides are preserved
value = clone_input(value)
return value
def wrap_fx_proxy(
tx, proxy, example_value=None, subclass_type=None, **options
) -> VariableTracker:
kwargs = {
"tx": tx,
"proxy": proxy,
"example_value": example_value,
"subclass_type": subclass_type,
**options,
}
if subclass_type is None:
return wrap_fx_proxy_cls(target_cls=TensorVariable, **kwargs)
else:
result = wrap_fx_proxy_cls(target_cls=TensorWithTFOverrideVariable, **kwargs)
result.install_global(tx)
return result
def cache_real_value_when_export(tx, proxy, example_value):
if tx.export:
# The legacy behavior for real value cache with subclasses was
# to perform a clone WITHOUT preserving the subclass. It's
# not entirely clear this is what you actually want though.
with torch._C.DisableTorchFunctionSubclass():
proxy.tracer.real_value_cache[proxy.node] = _clone_input(
example_value, tx.fake_mode
)
# Note: Unfortunate split due to some gross classes existing that subclass TensorVariable
# Should be compositional instead
#
# This is a horribly complicated function that does too many things, to
# explain what it does, let's first talk about the classic usage wrap_fx_proxy
# for a TensorVariable. There are two primary modes of use:
#
# 1. Wrapping a pre-existing Tensor. In this case, example_value is set
# to the pre-existing Tensor. (Note that this example_value will NOT
# be the final example_value we put into node.meta['example_value'],
# instead it is converted into a fake tensor using
# wrap_to_fake_tensor_and_record and registered as a graph input.)
#
# 2. "Wrapping" the result of some Tensor operation Dynamo traced over. In
# this case, example_value is None (and we are going to figure it out
# ourselves using FakeTensors, via get_fake_value, which will run
# the operation represented by the (singular!) FX node referenced by
# the passed in proxy.)
#
# The expectation is you end up with a Tensor output, and everything is
# straightforwardly traced into the graph.
#
# In all cases, the returned `TensorVariable` subclass will have an `example_value`
# and that `example_value` must be a `FakeTensor` produced by the currently running
# instance of Dynamo.
#
# Upon closer inspection, you may notice that there are a slurry of non-Tensor
# output cases in handle_traced_output. What gives? Well, we sometimes trace operations into the
# graph that don't involve tensors.
#
# * Some operators return tuples; we need to recursively handle their
# contents
#
# * Some operators have side effects that will affect subsequent AOTAutograd
# tracing but don't otherwise return anything.
#
# * Some operators return symbolic ints/floats/bools which can go in the
# graph and be traced (but only if they're actually symbolic! If they're
# static you don't want to put them in the graph, which means you
# shouldn't call this function.)
#
# The common theme is that you only use this function WHEN YOU ARE TRACING
# SOMETHING INTO THE GRAPH. This is sort of obvious, because you can't call
# this function without a proxy.
def wrap_fx_proxy_cls(
target_cls, tx, proxy, example_value=None, subclass_type=None, **options
):
if example_value is None:
return _wrap_fx_proxy(
target_cls, tx, proxy, example_value, subclass_type, **options
)
elif isinstance(example_value, torch.Tensor):
return _wrap_fx_preexisting_tensor(
target_cls, tx, proxy, example_value, subclass_type, **options
)
else:
# This will skip tracing an op and recursively reinvoke wrap_fx_proxy_cls on supported
# data structures. In essence this just handles tracing some other value which may
# contain Fake Tensors or is otherwise proxyable.
return handle_traced_output(
example_value, tx, proxy, options, subclass_type, target_cls
)
# This is 1 above (wrapping a preexisting tensor)
def _wrap_fx_preexisting_tensor(
target_cls, tx, proxy, tensor, subclass_type=None, **options
):
from ..symbolic_convert import InstructionTranslatorBase
assert isinstance(
tensor, torch.Tensor
), f"_wrap_fx_preexisting_tensor expected tensor, got {type(tensor)}"
assert isinstance(tx, InstructionTranslatorBase)
if "guards" in options and options["guards"] is not None:
tx.output.guards.update(options["guards"])
# Placeholders always carry example_value in node.meta.
# non-placeholders always have no example_value in node.meta
if proxy.node.op == "placeholder":
assert (
"example_value" in proxy.node.meta
), f"placeholder {proxy} doesn't have 'example_value' in node.meta"
else:
assert (
"example_value" not in proxy.node.meta
), f"{proxy.node.meta['example_value']}"
# See NOTE: [Deferring tensor pack/unpack hooks until runtime]
with torch._dynamo.utils._disable_saved_tensors_hooks_during_tracing():
# Handle recursive calls here
if maybe_get_fake_mode(tensor) is tx.fake_mode:
pass
else:
cache_real_value_when_export(tx, proxy, tensor)
if tx.export:
# The legacy behavior for real value cache with subclasses was
# to perform a clone WITHOUT preserving the subclass. It's
# not entirely clear this is what you actually want though.
with torch._C.DisableTorchFunctionSubclass():
proxy.tracer.real_value_cache[proxy.node] = _clone_input(
tensor, tx.fake_mode
)
# NB: If we're ignoring subclass, then the expectation is you will
# take the returned TensorVariable and wrap it into a more
# accurate TensorVariable that is able to track subclass-ness;
# otherwise this is wrong!
kwargs = {
"is_tensor": target_cls
in (TensorVariable, TensorWithTFOverrideVariable),
}
assert "source" in options and options["source"] is not None
kwargs["source"] = options["source"]
tensor = wrap_to_fake_tensor_and_record(tensor, tx=tx, **kwargs)
if tensor.device.type != "meta" and (
maybe_get_fake_mode(tensor) is not tx.fake_mode
):
raise InternalTorchDynamoError(
"`tensor` needs to be a `FakeTensor`"
f"wrapped by this instance of Dynamo. Found: {tensor}"
)
return handle_traced_output(tensor, tx, proxy, options, subclass_type, target_cls)
# This is 2 in the above comment (wrapping the output of a traced op)
def _wrap_fx_proxy(
target_cls, tx, proxy, example_value=None, subclass_type=None, **options
):
from ..symbolic_convert import InstructionTranslatorBase
assert isinstance(tx, InstructionTranslatorBase)
if "guards" in options and options["guards"] is not None:
tx.output.guards.update(options["guards"])
assert "example_value" not in proxy.node.meta, f"{proxy.node.meta['example_value']}"
# See NOTE: [Deferring tensor pack/unpack hooks until runtime]
with torch._dynamo.utils._disable_saved_tensors_hooks_during_tracing():
# with preserve_rng_state():
# only allow_non_graph_fake in this instance because we handle the non-fake
# cases properly below.
example_value = get_fake_value(proxy.node, tx, allow_non_graph_fake=True)
return handle_traced_output(
example_value, tx, proxy, options, subclass_type, target_cls
)
# This handles wrapping of the output of an op traced into the graph
def handle_traced_output(example_value, tx, proxy, options, subclass_type, target_cls):
import torch._functorch.vmap
import torch._subclasses.fake_tensor
import torch._utils
if isinstance(example_value, torch.Tensor):
is_parameter = isinstance(example_value, torch.nn.Parameter)
is_buffer = isinstance(example_value, torch.nn.Buffer)
# NB: In most (all?) cases, this does not actually do a clone.
# (WARNING: this means that if we mutate metadata on the fake
# tensor, the stored example value will update too!)
example_value = _clone_input(example_value, tx.fake_mode)
set_example_value(proxy.node, example_value)
# We bind the unbacked symints in sizes/trdies of tensor lazily.
# So that subgraphs can access the unbacked symbol's proxy in parent graph
# when lifting unbacked symbols of input tensors to subgraph inputs.
# We do it lazily because the tensor may not be used in subgraphs.
tx.output.current_tracer.track_unbacked_symbols(example_value, proxy)
specialized_props = target_cls.specialize(example_value)
# TODO: not sure about this fake mode test
if (
isinstance(example_value, torch._subclasses.fake_tensor.FakeTensor)
and example_value.fake_mode is tx.fake_mode
):
tensor_type = subclass_type if subclass_type else torch.Tensor
specialized_props["class_type"] = (
torch.nn.Parameter
if is_parameter
else torch.nn.Buffer
if is_buffer
else tensor_type
)
options.update(specialized_props)
return target_cls(proxy, **options)
elif (
hasattr(proxy.node.target, "__name__")
and proxy.node.target.__name__ == "set_state"
and isinstance(proxy.node.target.__self__, torch._C.Generator)
or proxy.node.target == torch.random.set_rng_state
):
return TorchInGraphFunctionVariable(proxy.node.target)
elif (
proxy.node.target == torch._C._DisableFuncTorch
or proxy.node.target == torch.cuda._is_in_bad_fork
):
return UserDefinedObjectVariable(example_value)
elif istype(example_value, torch.Size) and all(
isinstance(x, int) for x in example_value
):
sizes = [ConstantVariable.create(x) for x in example_value]
return SizeVariable(sizes, **options)
elif isinstance(example_value, (tuple, list)):
set_example_value(proxy.node, example_value)
unpacked = []
for i, val in enumerate(example_value):
if val is None:
# nn.MultiheadAttention() can return None, see issue #175
unpacked.append(
ConstantVariable.create(None, **options),
)
else:
proxy_i = proxy.tracer.create_proxy(
kind="call_function",
target=operator.getitem,
args=(proxy, i),
kwargs={},
)
if "source" in options:
# This path should only trigger for list stealing, so it's
# safe to use `GetItemSource`.
assert isinstance(example_value, list)
source = options["source"]
options_i = options.copy()
options_i["source"] = GetItemSource(
base=source, index=i, index_is_slice=False
)
else:
# use the same options object as parent
options_i = options
# WARNING: this assumes the same target_cls as this tuple/list call
unpacked.append(
wrap_fx_proxy_cls(
target_cls=target_cls,
tx=tx,
proxy=proxy_i,
example_value=val,
**options_i,
)
)
if isinstance(example_value, torch.Size):
# NB: Keep the old proxy around. See SizeVariable for an
# explanation why
return SizeVariable(unpacked, proxy, **options)
elif istype(example_value, tuple):
return TupleVariable(unpacked, **options)
elif istype(example_value, (list, immutable_list)):
return ListVariable(unpacked, **options)
else:
assert example_value.__class__.__module__ == "torch.return_types" or hasattr(
example_value, "_fields"
), f"expected {example_value.__class__.__module__} == torch.return_types or named tuple but got {type(example_value)}"
return NamedTupleVariable(unpacked, example_value.__class__, **options)
elif example_value is None or proxy.node.target is torch.manual_seed:
return ConstantVariable.create(None, **options)
elif isinstance(example_value, (torch.SymInt, torch.SymFloat, torch.SymBool)):
tx.output.current_tracer.track_unbacked_symbols(example_value, proxy)
set_example_value(proxy.node, example_value)
return SymNodeVariable(proxy, example_value, **options)
elif (
inspect.isclass(proxy.node.target)
and issubclass(proxy.node.target, torch.Stream)
) or proxy.node.target in [
device_interface.current_stream
for _, device_interface in get_registered_device_interfaces()
]:
set_example_value(proxy.node, example_value)
return StreamVariable(proxy, example_value, example_value.device, **options)
elif (
inspect.isclass(proxy.node.target)
and issubclass(proxy.node.target, torch.Event)
) or proxy.node.target in [
device_interface.Event
for _, device_interface in get_registered_device_interfaces()
]:
set_example_value(proxy.node, example_value)
return EventVariable(proxy, example_value, **options)
elif proxy.node.target == "query" and proxy.node.op == "call_method":
set_example_value(proxy.node, example_value)
return ConstantVariable(example_value, **options)
elif (
example_value is not None
and isinstance(example_value, torch.Event)
and proxy.node.target == "record_event"
and proxy.node.op == "call_method"
):
set_example_value(proxy.node, example_value)
return EventVariable(proxy, example_value, **options)
elif isinstance(example_value, int) and (
proxy.node.target
in [
torch.sym_int,
getattr,
operator.getitem,
torch._utils._element_size,
torch.seed,
operator.mod,
torch._functorch.vmap._validate_and_get_batch_size,
# some mac builds are missing torch.distributed.get_rank()
getattr(torch.distributed, "get_rank", _missing),
getattr(torch.distributed, "get_world_size", _missing),
# This always wants to be in the graph, even if the constraint
# results in a constant int
torch._constrain_as_size,
]
or (
# TODO: this is a little sus, because we didn't check what the self is
proxy.node.op == "call_method"
and proxy.node.target in ["bit_length"]
)
):
set_example_value(proxy.node, example_value)
return ConstantVariable.create(example_value, **options)
elif isinstance(example_value, torch.backends.cuda.SDPAParams):
from .sdpa import SDPAParamsVariable
set_example_value(proxy.node, example_value)
return SDPAParamsVariable(proxy, **options)
elif isinstance(example_value, bool) and (
proxy.node.target
in [
torch._C._are_functorch_transforms_active,
torch.backends.cuda.is_flash_attention_available,
torch.backends.cuda.can_use_flash_attention,
torch.backends.cuda.can_use_efficient_attention,
"is_integer",
]
+ list(supported_const_comparison_op_values.keys())
):
set_example_value(proxy.node, example_value)
return ConstantVariable.create(example_value, **options)
elif (
isinstance(example_value, (int, float, bool))
and proxy.node.target is call_torchbind
):
set_example_value(proxy.node, example_value)
return ConstantVariable.create(example_value, **options)
elif isinstance(example_value, float) or proxy.node.target in ["hex", "__round__"]:
set_example_value(proxy.node, example_value)
return ConstantVariable.create(example_value, **options)
else:
unimplemented(
"torch.* op returned non-Tensor "
+ f"{typestr(example_value)} {proxy.node.op} {proxy.node.target}",
case_name="unsupported_operator",
)
def get_automatic_dynamic_shapes_mark_as():
if config.automatic_dynamic_shapes_mark_as == "dynamic":
return DimDynamic.DYNAMIC
elif config.automatic_dynamic_shapes_mark_as == "unbacked":
return DimDynamic.SIZE_LIKE_UNBACKED
elif config.automatic_dynamic_shapes_mark_as == "oblivious":
return DimDynamic.OBLIVIOUS_SIZE
else:
raise ValueError(
f"invalid automatic_dynamic_shapes_mark_as = {config.automatic_dynamic_shapes_mark_as}"
)
# Tracks the sources of all fake tensors we wrap in Dynamo.
# Used by shape guard computation.
@dataclasses.dataclass
class TrackedFake:
fake: Union[FakeTensor, SymInt]
source: Source
# Is None when fake is SymInt
symbolic_context: Optional[SymbolicContext]
def __hash__(self) -> int:
return hash((self.fake, self.source.name()))
def __eq__(self, other: object) -> bool:
if isinstance(other, TrackedFake):
return self.fake is other.fake and self.source.name() == other.source.name()
return False
# Performs automatic dynamic dim determination.
# Returns a SymbolicContext
def _automatic_dynamic(
e, tx, source, static_shapes, outer_only=False
) -> SymbolicContext:
# strided NT not supported
if e.is_nested and not isinstance(
e, torch.nested._internal.nested_tensor.NestedTensor
):
unimplemented("torch.compile does not support strided NestedTensor")
name = source.name()
prior_policy = tx.output.tracing_context.tensor_to_context.get(e, None)
shape_env_to_source_to_symbol_cache = (
prior_policy.shape_env_to_source_to_symbol_cache if prior_policy else None
)
# Get base context if the tensor is a view
view_base_context: Optional[SymbolicContext] = None
if e._is_view():
base_source = AttrSource(source, "_base")
view_base_context = _automatic_dynamic(e._base, tx, base_source, static_shapes)
if is_traceable_wrapper_subclass(e) and not outer_only:
# Get symbolic context for outer tensor
outer_context = _automatic_dynamic(
e, tx, source, static_shapes, outer_only=True
)
# Get symbolic contexts for inner tensors
inner_contexts = {} # mapping from attr -> symbolic context
attrs, _ = type(e).__tensor_flatten__(e)
for attr in attrs:
inner_tensor = getattr(e, attr)
inner_source = AttrSource(source, attr)
inner_contexts[attr] = _automatic_dynamic(
inner_tensor, tx, inner_source, static_shapes
)
return SubclassSymbolicContext(
dynamic_sizes=outer_context.dynamic_sizes,
dynamic_strides=outer_context.dynamic_strides,
constraint_sizes=outer_context.constraint_sizes,
constraint_strides=outer_context.constraint_strides,
view_base_context=view_base_context,
tensor_source=outer_context.tensor_source,
shape_env_to_source_to_symbol_cache=outer_context.shape_env_to_source_to_symbol_cache,
inner_contexts=inner_contexts,
)
if static_shapes:
return StatefulSymbolicContext(
dynamic_sizes=[DimDynamic.STATIC] * e.dim(),
dynamic_strides=[DimDynamic.INFER_STRIDE] * e.dim(),
constraint_sizes=[None] * e.dim(),
constraint_strides=[None] * e.dim(),
view_base_context=view_base_context,
tensor_source=source,
shape_env_to_source_to_symbol_cache=shape_env_to_source_to_symbol_cache,
)
# We preserve the dynamism of inputs. For example, when users call
# make_fx(torch.cond, tracing_mode="symbolic")(*args), inputs have SymInt sizes.
from torch.fx.experimental.symbolic_shapes import is_nested_int
if any(isinstance(s, SymInt) and not is_nested_int(s) for s in e.size()):
return StatefulSymbolicContext(
dynamic_sizes=[
DimDynamic.DYNAMIC if isinstance(s, SymInt) else DimDynamic.STATIC
for s in e.size()
],
dynamic_strides=[DimDynamic.INFER_STRIDE] * e.dim(),
constraint_sizes=[None] * e.dim(),
constraint_strides=[None] * e.dim(),
view_base_context=view_base_context,
tensor_source=source,
shape_env_to_source_to_symbol_cache=shape_env_to_source_to_symbol_cache,
)
# Prep for automatic dynamic
# This mimics stride inference algorithm in _create_symbolic_sizes_strides_storage_offset
ex_size = e.size()
if not is_sparse_any(e):
ex_stride = e.stride()
dim = e.dim()
stride = [None] * dim
while any(x is None for x in stride):
candidates = {
ex_size[i] * ex_stride[i]: InferStride(i)
for i in range(dim)
if stride[i] is not None and ex_stride[i] >= 0
}
val_list = sorted(
[(ex_stride[i], i) for i in range(dim) if stride[i] is None],
key=_nested_int_aware_sort,
)
for _, i in val_list:
if stride[i] is None and ex_stride[i] in candidates:
stride[i] = candidates[ex_stride[i]]
candidates[ex_stride[i] * ex_size[i]] = InferStride(i)
if any(x is None for x in stride):
# bind the smallest unbound stride to a new variable
val, i = min(
[(ex_stride[i], i) for i in range(dim) if stride[i] is None],
key=_nested_int_aware_sort,
)
stride[i] = val
else:
stride = []
frame_state_entry = process_automatic_dynamic(
tx, name, FrameStateSizeEntry.make_tensor(tuple(ex_size), tuple(stride))
)
# TODO: index export_constraints ahead of time so we don't have to
# do a linear scan every time here
t_id = id(e)
dim2constraint = {}
def update_dim2constraint(dim, constraint_range, name):
if dim in dim2constraint:
from torch.fx.experimental.symbolic_shapes import StrictMinMaxConstraint
old_constraint_range, old_name = dim2constraint[dim]
new_constraint_range = StrictMinMaxConstraint(
vr=constraint_range.vr & old_constraint_range.vr,
warn_only=False,
)
# It is possible for (non-None) old_name and name to be different
# but this will only happen the corresponding Dims can be derived equal.
new_name = old_name or name
dim2constraint[dim] = new_constraint_range, new_name
else:
dim2constraint[dim] = constraint_range, name
from torch.export.dynamic_shapes import _RelaxedConstraint
if tx.output.export_constraints:
for constraint in tx.output.export_constraints:
if isinstance(constraint, _RelaxedConstraint):
continue
if constraint.t_id == t_id:
update_dim2constraint(
constraint.dim, constraint.constraint_range, constraint.name
)
dynamic_sizes = []
dynamic_strides = []
constraint_sizes = []
constraint_strides = []
for i in range(e.dim()):
# NB: mark dynamic has precedence over static
marked_unbacked = i in getattr(e, "_dynamo_unbacked_indices", set())
marked_dynamic = i in getattr(e, "_dynamo_dynamic_indices", set())
marked_weak_dynamic = i in getattr(e, "_dynamo_weak_dynamic_indices", set())
marked_static = i in getattr(e, "_dynamo_static_indices", set())
# Reflect the user directive in the frame_state
# For dynamic, apply None always
if marked_dynamic:
# TODO: This can be batched
# TODO: Doing this here is kind of sus, maybe better to set this
# up when we initially created the FrameStateSizeEntry to bong
# into the mutable state
log.debug("automatic dynamic %s marked dynamic", name)
mark_size = [auto_unset] * e.dim()
mark_size[i] = auto_dynamic
frame_state_entry |= FrameStateSizeEntry.make_size(size=mark_size)
# NB: both static and dynamic have precedence over
automatic_dynamic_size = (
config.automatic_dynamic_shapes and frame_state_entry.is_size_dynamic(i)
)
# NB: previously, if size was dynamic, we wouldn't make its stride
# dynamic. But now, because of InferStride concept, we will properly
# not make stride dynamic even if it's wobbling
automatic_dynamic_stride = (
config.automatic_dynamic_shapes and frame_state_entry.is_stride_dynamic(i)
)
automatic_dynamic = automatic_dynamic_size or automatic_dynamic_stride
# We will process constraints first, as they will imply that we
# have a dynamic dimension
# Precedence: export constraints > eager constraints
constraint = dim2constraint.get(i)
if constraint is None:
constraint_size = None
constraint_stride = None
if marked_dynamic and not config.allow_ignore_mark_dynamic:
# constraint_stride is deliberaly kept None because no easy way to provide value ranges for mark dynamic
constraint_stride = None
if hasattr(e, "_dynamo_dynamic_range"):
dim_range = [
dr for dr in e._dynamo_dynamic_range if dr.dim == i
].pop()
if dim_range.min is None and dim_range.max is None:
constraint_size = RelaxedUnspecConstraint(warn_only=False)
else:
from torch.fx.experimental.symbolic_shapes import (
StrictMinMaxConstraint,
)
constraint_size = StrictMinMaxConstraint(
vr=ValueRanges(lower=dim_range.min, upper=dim_range.max),
warn_only=False,
)
else:
constraint_size = RelaxedUnspecConstraint(warn_only=False)
elif not marked_static and automatic_dynamic:
if automatic_dynamic_size:
constraint_size = RelaxedUnspecConstraint(warn_only=True)
if automatic_dynamic_stride:
constraint_stride = RelaxedUnspecConstraint(warn_only=True)
else:
constraint_size = None
constraint_stride = None
else:
constraint_size, name_ = constraint
constraint_stride = None
dim_name = f"{name}.size()[{i}]"
tx.output.shape_env.source_name_to_debug_name[dim_name] = name_
constraint_sizes.append(constraint_size)
constraint_strides.append(constraint_stride)
if marked_unbacked:
dynamic_size = DimDynamic.SIZE_LIKE_UNBACKED
elif (
constraint_size is not None
or marked_dynamic
or marked_weak_dynamic
or is_nested_int(e.size()[i])
):
# NB: We could assert static_shapes is False here, but it
# seems better to allow the user to override symbolic_context in this
# case
if automatic_dynamic:
dynamic_size = get_automatic_dynamic_shapes_mark_as()
else:
dynamic_size = DimDynamic.DYNAMIC
elif static_shapes or config.assume_static_by_default or marked_static:
dynamic_size = DimDynamic.STATIC
else:
# TODO: When does this show up?
dynamic_size = DimDynamic.DUCK
if constraint_stride is not None:
dynamic_stride = DimDynamic.DYNAMIC
else:
dynamic_stride = DimDynamic.INFER_STRIDE
dynamic_sizes.append(dynamic_size)
dynamic_strides.append(dynamic_stride)
return StatefulSymbolicContext(
dynamic_sizes=dynamic_sizes,
dynamic_strides=dynamic_strides,
constraint_sizes=constraint_sizes,
constraint_strides=constraint_strides,
view_base_context=view_base_context,
tensor_source=source,
shape_env_to_source_to_symbol_cache=shape_env_to_source_to_symbol_cache,
)
# See note [Tensor Fakification and Symbol Caching]
def wrap_to_fake_tensor_and_record(
e, tx, *, source: Optional[Source], is_tensor: bool, parent_context=None
):
if (
type(e) in (torch.Tensor, torch.nn.Parameter, FakeTensor)
or isinstance(e, torch.Tensor)
or is_traceable_wrapper_subclass(e)
):
assert source is not None
static_shapes, reason = tensor_always_has_static_shape(
e,
is_tensor,
tensor_source=source,
)
if not parent_context:
symbolic_context = _automatic_dynamic(e, tx, source, static_shapes)
else:
# Parent contexts are passed in when we are recursively creating
# fake tensors for subclasses. A better design would be not to create a
# parent/child relationship, but to recursively call _automatic_dynamic
# as we recursively call wrap_to_fake_tensor_and_record. This runs
# into bugs around how meta_utils knows and works to create fake tensors
# with tensor subclasses. Ideally, dynamo would drive both the recursive
# wrap_to_fake_tensor_and_record and _automatic_dynamic policy creation.
assert isinstance(source, AttrSource)
inner_context_name = source.member
symbolic_context = parent_context.inner_contexts[inner_context_name]
log.debug(
"wrap_to_fake %s %s %s %s",
source.name(),
tuple(e.shape),
symbolic_context,
type(e),
)
fake_e = wrap_fake_exception(
lambda: tx.fake_mode.from_tensor(
e,
source=source,
symbolic_context=symbolic_context,
)
)
if (
source is not None
and isinstance(fake_e, FakeTensor)
and (sym_val := fake_e.item_memo) is not None
):
tx.output.tracked_fakes.append(
TrackedFake(sym_val, CallMethodItemSource(source), symbolic_context)
)
if is_traceable_wrapper_subclass(fake_e):
attrs, _ = fake_e.__tensor_flatten__()
for attr in attrs:
fake_inner = getattr(fake_e, attr)
inner = getattr(e, attr)
inner_source = AttrSource(source, attr)
wrap_to_fake_tensor_and_record(
inner,
tx,
source=inner_source,
is_tensor=isinstance(fake_inner, torch.Tensor),
parent_context=symbolic_context,
)
tx.output.tracing_context.tensor_to_context[e] = symbolic_context
if is_sparse_any(fake_e):
# TODO: for TensorGuards, this eventually may need more
# fields for the size/stride of any other constituents
values = fake_e._values() if fake_e.is_sparse else fake_e.values()
tx.output.input_source_to_sizes_strides[source] = {
"size": fake_e.size(),
# TODO: revise this, but for now this stride instead of ()
# avoids SegFault with PYTORCH_TEST_WITH_DYNAMO=1
"stride": (1,) * fake_e.ndim,
"values_size": values.size(),
"values_stride": values.stride(),
}
else:
tx.output.input_source_to_sizes_strides[source] = {
"size": fake_e.size(),
"stride": fake_e.stride(),
}
if (
is_tensor
and not (static_shapes and source.is_specialized_nn_module())
and not is_constant_source(source)
):
tx.output.tracked_fakes.append(
TrackedFake(fake_e, source, symbolic_context)
)
tx.output.tracked_fakes_id_to_source[id(e)].append(source)
return fake_e
else:
return e
class SourcelessBuilder:
"""
Like builder, but stateless and does not require a source. Useful for simple type->VT objects, or objects
that are being created/evaporated during inlining (ex: consider a locally made list of tensors we then iterate over
.), such a list should not show up as an artifact from inputs, nor in reconstruction, nor in the graph. However,
there may be reasons to represent it as a ListVariable internally.
NOTE - Objects produced here are born UNGUARDED due to the nature of sources!
NOTE - This class is very new! It will have some rough edges, but it was created to stem the bleeding of giant
if/else type->VariableTracker trees that were cropping up all over dynamo.
"""
def __init__(self) -> None:
raise AssertionError("Use SourcelessBuilder.create()")
@staticmethod
def create(tx: "InstructionTranslator", value) -> VariableTracker:
value_type = type(value)
fast_handler = SourcelessBuilder._type_handlers.get(value_type)
if fast_handler:
return fast_handler(tx, value)
if isinstance(value, VariableTracker):
# This is always valid to call, and useful for recursive calls.
return value
elif isinstance(value, dataclasses._HAS_DEFAULT_FACTORY_CLASS):
return UserDefinedObjectVariable(value)
elif ConstantVariable.is_literal(value):
return ConstantVariable.create(value)
elif callable(value) and trace_rules.lookup_callable(value) is not None:
if is_callable_allowed(value):
tx.output.has_user_defined_allowed_in_graph = True
return trace_rules.lookup_callable(value)(value)
elif is_function_or_wrapper(value):
return trace_rules.lookup(value)(value)
elif isinstance(value, enum.Enum):
return EnumVariable(value)
elif isinstance(value, (type, abc.ABCMeta)):
return UserDefinedClassVariable(value)
elif isinstance(value, types.MethodWrapperType):
return MethodWrapperVariable(value)
elif isinstance(value, torch.fx.graph_module.GraphModule):
return SourcelessGraphModuleVariable(value)
elif isinstance(
value, (torch.utils._pytree.TreeSpec, torch.utils._pytree.LeafSpec)
):
return UserDefinedObjectVariable(value)
elif PlacementVariable.is_placement(value):
return PlacementVariable(value)
elif DeviceMeshVariable.is_device_mesh(value):
return DeviceMeshVariable(value)
elif isinstance(value, re.Pattern):
return RegexPatternVariable(value)
elif isinstance(value, torch._dynamo.variables.lazy.LazySymNodeFormatString):
return ConstantVariable.create(str(value))
unimplemented(
f"Unexpected type in sourceless builder {value_type.__module__}.{value_type.__qualname__}"
)
@staticmethod
def wrap_constant_literal(value):
assert ConstantVariable.is_literal(value)
return ConstantVariable.create(value=value)
@staticmethod
def make_type_handlers():
create = SourcelessBuilder.create
handlers = {}
for t in common_constant_types:
handlers[t] = lambda tx, value: ConstantVariable(value)
handlers[set] = lambda tx, value: SetVariable(
[create(tx, x) for x in value], mutation_type=ValueMutationNew()
)
handlers[dict] = lambda tx, value: ConstDictVariable(
{create(tx, k): create(tx, v) for k, v in value.items()},
type(value),
mutation_type=ValueMutationNew(),
)
handlers[list] = lambda tx, value: ListVariable(
[create(tx, x) for x in value], mutation_type=ValueMutationNew()
)
handlers[tuple] = lambda tx, value: TupleVariable(
[create(tx, x) for x in value]
)
handlers[torch.Size] = lambda tx, value: SizeVariable(
[create(tx, x) for x in value]
)
handlers[collections.OrderedDict] = handlers[dict]
handlers[immutable_dict] = handlers[dict]
handlers[immutable_list] = handlers[list]
handlers[random.Random] = lambda tx, value: RandomClassVariable()
handlers[types.ModuleType] = lambda tx, value: PythonModuleVariable(value)
handlers[
torch.distributions.constraints._Real
] = lambda tx, value: UserDefinedObjectVariable(
value, mutation_type=ValueMutationNew()
)
handlers[
torch.distributions.constraints._Interval
] = lambda tx, value: UserDefinedObjectVariable(
value, mutation_type=ValueMutationNew()
)
handlers[
torch.distributions.constraints.Constraint
] = lambda tx, value: UserDefinedObjectVariable(
value, mutation_type=ValueMutationNew()
)
def passthrough(tx: "InstructionTranslator", value):
return value
for cls in VariableTrackerMeta.all_subclasses:
handlers[cls] = passthrough
return handlers
SourcelessBuilder._type_handlers = SourcelessBuilder.make_type_handlers()
class SourcelessUserDefinedObjectBuilder:
"""
SourceLessBuilder does not return a UserDefinedObjectVariable, but in some
cases it might be ok to return UserDefinedObjects. In such case, use this
builder.
"""
def __init__(self) -> None:
raise AssertionError("Use SourcelessUserDefinedObjectBuilder.create()")
@staticmethod
def create(tx: "InstructionTranslator", value) -> VariableTracker:
value_type = type(value)
if issubclass(value_type, MutableMapping):
return MutableMappingVariable(value, mutation_type=ValueMutationNew())
elif isinstance(value, torch.nn.Module):
return UnspecializedNNModuleVariable(
value, mutation_type=ValueMutationNew()
)
else:
return UserDefinedObjectVariable(value, mutation_type=ValueMutationNew())
|