1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914
|
# Owner(s): ["oncall: pt2"]
# Copyright (c) Facebook, Inc. and its affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
import copy
import itertools
import unittest
import warnings
from contextlib import ContextDecorator, nullcontext
from functools import partial, wraps
from typing import Any, Callable, Dict, List, Optional, Union
from unittest.mock import patch
from common_utils import decorate, decorateForModules, skip, skipOps, xfail
import torch
import torch._dynamo as torchdynamo
import torch.nn as nn
import torch.utils._pytree as pytree
from functorch import grad, jacrev, make_fx, vjp, vmap
from functorch.compile import (
aot_function,
aot_module,
aot_module_simplified,
compiled_function,
compiled_module,
default_decompositions,
default_partition,
get_aot_compilation_context,
make_boxed_compiler,
make_boxed_func,
memory_efficient_fusion,
min_cut_rematerialization_partition,
nnc_jit,
nop,
)
from functorch.experimental import control_flow
from torch._decomp import decomposition_table
from torch._functorch._aot_autograd.autograd_cache import AOTAutogradCache
from torch._functorch.aot_autograd import (
aot_export_joint_simple,
aot_export_module,
SerializableAOTDispatchCompiler,
)
from torch._higher_order_ops.out_dtype import out_dtype
from torch._inductor.codecache import compiled_fx_graph_hash
from torch._inductor.output_code import MockFXGraphCacheOutput
from torch._subclasses.fake_tensor import DynamicOutputShapeException, FakeTensorMode
from torch.fx.experimental.proxy_tensor import is_sym_node
from torch.fx.experimental.symbolic_shapes import GuardOnDataDependentSymNode, ShapeEnv
from torch.nn.utils.rnn import PackedSequence
from torch.testing._internal.common_device_type import (
instantiate_device_type_tests,
ops,
tol,
toleranceOverride,
)
from torch.testing._internal.common_methods_invocations import op_db
from torch.testing._internal.common_modules import module_db, modules
from torch.testing._internal.common_utils import (
compare_equal_outs_and_grads,
instantiate_parametrized_tests,
IS_ARM64,
IS_MACOS,
IS_WINDOWS,
IS_X86,
outs_and_grads,
parametrize,
run_tests,
skipIfRocm,
skipIfTorchDynamo,
TestCase,
xfail_inherited_tests,
xfailIfS390X,
xfailIfTorchDynamo,
)
from torch.testing._internal.custom_tensor import ConstantExtraMetadataTensor
from torch.testing._internal.hop_db import hop_db
from torch.testing._internal.optests import (
_test_aot_autograd_forwards_backwards_helper,
aot_autograd_check,
)
from torch.testing._internal.subclasses import WrapperSubclass
from torch.testing._internal.two_tensor import TwoTensor, TwoTensorMode
USE_TORCHVISION = False
try:
import torchvision
USE_TORCHVISION = True
except ImportError:
warnings.warn(
"Couldn't import torchvision. Some of our tests use it, try "
"to install it with commands from pytorch.org, post-fixed with "
"`--no-deps` to avoid overwriting the pytorch installation",
UserWarning,
)
USE_NETWORKX = False
try:
import networkx # noqa: F401
USE_NETWORKX = True
except ImportError:
warnings.warn("Some tests use networkx but it was not installed", UserWarning)
# NB: numpy is a testing dependency!
class AOTTestCase(TestCase):
pass
class TestPythonKey(AOTTestCase):
def test_make_fx(self, device):
def f(x):
return torch.sin(x)
inp = torch.randn(3)
fx_f = make_fx(f)(inp)
new_inp = torch.randn(3)
self.assertEqual(fx_f(new_inp), f(new_inp))
def test_make_fx_grad(self, device):
def f(x):
return torch.sin(x).sum()
inp = torch.randn(3)
f = grad(f)
fx_f = make_fx(f)(inp)
new_inp = torch.randn(3)
self.assertEqual(fx_f(new_inp), f(new_inp))
def test_scalar_device(self, device):
def f(a, b):
return a + b
inps = [torch.randn(3, device=device), torch.tensor(5)]
fx_f = make_fx(f)(*inps)
self.assertEqual(fx_f(*inps), f(*inps))
def test_make_fx_vmap(self, device):
def f(x):
return torch.sin(x)
inp = torch.randn(5, 3)
f = vmap(f)
fx_f = make_fx(f)(inp)
new_inp = torch.randn(5, 3)
self.assertEqual(fx_f(new_inp), f(new_inp))
def test_make_fx_jacrev(self, device):
def f(x):
return x.sin().sum()
inp = torch.randn(3)
f = jacrev(jacrev(f))
fx_f = make_fx(f)(inp)
new_inp = torch.randn(3)
self.assertEqual(fx_f(new_inp), f(new_inp))
def test_make_fx_vjp(self, device):
def f(x):
return torch.sin(x).sum()
primals = torch.randn(3)
_, vjp_fn = vjp(f, primals)
cotangent = torch.randn(())
fx_f = make_fx(vjp_fn)(cotangent, True, True)
new_cotangent = torch.randn(())
self.assertEqual(fx_f(new_cotangent, True, True), vjp_fn(new_cotangent))
def test_make_fx_functionalize(self, device):
from functorch.experimental import functionalize
def fn(a):
a = a * 2
a.relu_()
return a
a = torch.randn(3, device=device)
symbolic_gm = torch.fx.symbolic_trace(fn)
includes_method_relu_ = any(
str(n.target) == "relu_" for n in symbolic_gm.graph.nodes
)
self.assertTrue(includes_method_relu_)
# Also verifies fix for https://github.com/pytorch/pytorch/issues/84570
gm = make_fx(functionalize(symbolic_gm))(a)
includes_aten_relu = any(
n.target == torch.ops.aten.relu.default for n in gm.graph.nodes
)
self.assertTrue(includes_aten_relu)
def test_make_fx_no_decompose(self, device):
# FIXME
return self.skipTest("error: maximum recursion reached")
def f(x):
return torch.tanh(x).sum()
fx_f = make_fx(grad(f))(torch.randn(5))
ops = {i.target for i in fx_f.graph.nodes}
self.assertEqual(torch.ops.aten.tanh_backward in ops, True)
fx_f = make_fx(grad(f), decomposition_table)(torch.randn(5))
ops = {i.target for i in fx_f.graph.nodes}
self.assertEqual(torch.ops.aten.tanh_backward in ops, False)
def test_nnc_jit(self, device):
def f(x):
return torch.sin(x)
jit_f = nnc_jit(f)
inp = torch.randn(3)
self.assertEqual(jit_f(inp), f(inp))
def test_nnc_scalar(self, device):
def f(x):
return torch.sin(x)
jit_f = nnc_jit(f)
inp = torch.randn(())
self.assertEqual(jit_f(inp), f(inp))
def test_nnc_pytrees(self, device):
def f(x):
return [torch.sin(x[0])]
jit_f = nnc_jit(f)
inp = [torch.randn(3)]
self.assertEqual(jit_f(inp), f(inp))
def test_external_calls(self, device):
def f(a, b):
return torch.mv(a, b)
jit_f = nnc_jit(f)
inp = [torch.randn(3, 3), torch.randn(3)]
self.assertEqual(jit_f(*inp), f(*inp))
def test_nnc_passthrough(self, device):
def f(x, y):
return x + y, y
inp = (torch.randn(3), torch.randn(3))
jit_f = nnc_jit(f)
self.assertEqual(jit_f(*inp), f(*inp))
def f(x):
x["a"] = x["a"] * 2
return x
inp = ({"a": torch.randn(3), "b": torch.randn(3)},)
jit_f = nnc_jit(f)
self.assertEqual(jit_f(*inp), f(*inp))
@unittest.skipIf(not USE_TORCHVISION, "test requires torchvision")
def test_resnet18_backward_trace(self, device):
mod = torchvision.models.resnet18()
def f(x):
out = mod(x)
out.sum().backward()
return [a.grad for a in mod.parameters()]
inp = torch.randn(3, 3, 250, 250, requires_grad=True)
grads = f(inp)
mod.zero_grad()
mod(inp).sum().backward()
grads2 = [a.grad for a in mod.parameters()]
self.assertEqual(grads, grads2)
def get_base(t):
return t._base if t._is_view() else t
def is_in_base(t, maybe_tensors):
t_base = get_base(t)
for maybe_tensor in maybe_tensors:
if isinstance(maybe_tensor, torch.Tensor):
if t_base is get_base(maybe_tensor):
return True
return False
def skipIfDynamoInput(reason):
"""
Skip TestAOTAutograd if running with dynamo input
"""
def decorator(func):
@wraps(func)
def wrapper(self, *args, **kwargs):
if isinstance(self, TestAOTAutogradWithDynamo):
self.skipTest(
f"Skipping {self._testMethodName} in TestAOTAutogradWithDynamo because {reason}"
)
else:
func(self, *args, **kwargs)
return wrapper
return decorator
class TestAOTAutograd(AOTTestCase):
def run_autograd(
self,
f: Callable,
fw_graph_cell: List[Optional[Callable]],
decompositions: Optional[Dict],
keep_input_mutations: bool,
dynamic: bool,
):
"""
Runs aot_autograd with the specified settings on f.
"""
if isinstance(f, nn.Module):
compiled_f = aot_module(
f,
fw_compiler=make_boxed_compiler(
partial(extract_graph, graph_cell=fw_graph_cell)
),
bw_compiler=nop,
decompositions=decompositions,
keep_inference_input_mutations=keep_input_mutations,
dynamic=dynamic,
)
else:
compiled_f = aot_function(
f,
fw_compiler=make_boxed_compiler(
partial(extract_graph, graph_cell=fw_graph_cell)
),
bw_compiler=nop,
decompositions=decompositions,
keep_inference_input_mutations=keep_input_mutations,
dynamic=dynamic,
)
return compiled_f
# test_mutation will:
# - Ensure that inputs are non-leaves, so our graphs can mutate them
# - try to mutate outputs of the graph (to ensure that autograd meta is set properly on outputs)
@patch("functorch.compile.config.debug_assert", True)
def verify_aot_autograd(
self,
f,
inp_: Union[Callable, List[Any]],
*,
test_mutation: bool = False,
keep_inp_mutations: bool = False,
decompositions: Optional[Dict] = None,
dynamic: bool = False,
# Only active when inp_ is Callable.
# TODO: probably consolidate all tests to make inp a Callable.
make_inputs_subclasses: bool = False,
):
def make_inputs(inp_):
# Some tests pass in a callable for inp, to generate the inputs
# (useful if we want to generate complicated aliasing inputs)
if isinstance(inp_, Callable):
inp_callable = inp_
# The callable should return a tuple of f_inputs, f_graph_inputs
# (The idea is that we might want to compile a function with the graph inputs,
# but test autograd backprop all the way through the actual inputs)
with TwoTensorMode() if make_inputs_subclasses else nullcontext():
inp, graph_inps = inp_callable()
else:
inp = []
# Our input clones need to mimic when inputs are duplicates of one another
dupes_map = {}
for i, x in enumerate(inp_):
if x in dupes_map:
x_dupe_idx = dupes_map[x]
inp.append(inp[x_dupe_idx])
else:
dupes_map[x] = i
if not isinstance(x, torch.Tensor):
x_copy = x
else:
x_copy = x.detach().clone().requires_grad_(x.requires_grad)
if x.requires_grad and not x.is_leaf:
x_copy = x_copy.clone()
inp.append(x_copy)
if test_mutation:
# For graphs where we mutate inputs, need our test to make sure inputs aren't leaves
graph_inps = [x.add(1) for x in inp]
else:
graph_inps = inp
return inp, graph_inps
def check_results(
ref_results,
test_results,
ref_graph_inps,
test_graph_inps,
ref_inp,
test_inp,
):
ref_out, ref_grad = ref_results
test_out, test_grad = test_results
self.assertEqual(ref_grad, test_grad)
if isinstance(ref_out, torch.Tensor):
self.assertTrue(isinstance(test_out, torch.Tensor))
ref_out, test_out = [ref_out], [test_out]
for ref_o, test_o in zip(ref_out, test_out):
if isinstance(ref_o, torch.Tensor):
self.assertEqual(ref_o.requires_grad, test_o.requires_grad)
self.assertEqual(ref_o.is_leaf, test_o.is_leaf)
ref_is_view_of_non_interm = is_in_base(
ref_o, ref_graph_inps
) or is_in_base(ref_o, ref_out)
test_is_view_of_non_interm = is_in_base(
test_o, test_graph_inps
) or is_in_base(test_o, test_out)
self.assertEqual(
ref_is_view_of_non_interm, test_is_view_of_non_interm
)
self.assertEqual(ref_o, test_o)
if test_mutation:
# This tests that autograd meta is set properly on the output we can
# mutate it.
ref_o.add_(2)
test_o.add_(2)
self.assertEqual(ref_o, test_o)
# Reverse the modification
ref_o.sub_(2)
test_o.sub_(2)
self.assertEqual(ref_o, test_o)
for ref_i, test_i in zip(ref_inp, test_inp):
if isinstance(ref_i, torch.Tensor):
self.assertEqual(ref_i.requires_grad, test_i.requires_grad)
self.assertEqual(ref_i, test_i)
for keep_input_mutations in [True] if keep_inp_mutations else [True, False]:
inp, graph_inps = make_inputs(inp_)
test_inp, test_graph_inps = make_inputs(inp_)
fw_graph_cell = [None]
compiled_f = self.run_autograd(
f, fw_graph_cell, decompositions, keep_input_mutations, dynamic
)
ref_results = outs_and_grads(f, graph_inps, inp)
test_results = outs_and_grads(compiled_f, test_graph_inps, test_inp)
check_results(
ref_results, test_results, graph_inps, test_graph_inps, inp, test_inp
)
if isinstance(self, TestAOTAutogradWithCache):
# When testing with cache, run compiled_f a second time
cached_inp, cached_graph_inps = make_inputs(inp_)
cached_results = outs_and_grads(
compiled_f, cached_graph_inps, cached_inp
)
check_results(
ref_results,
cached_results,
graph_inps,
cached_graph_inps,
inp,
cached_inp,
)
return fw_graph_cell[0]
def test_non_tensor_and_none_inputs(self):
# int, None, Tensor
def f(a, b, c):
return a * c
inp = [2, None, torch.ones(3, 3, dtype=torch.float32, requires_grad=True)]
self.verify_aot_autograd(f, inp)
inp = [2, None, torch.ones(3, 3, dtype=torch.float32, requires_grad=False)]
self.verify_aot_autograd(f, inp)
def test_single_output(self):
def f(a, b):
return a + b
inp = [torch.randn(3, 3, requires_grad=True), torch.randn(3, 3)]
self.verify_aot_autograd(f, inp)
inp = [torch.randn(3, 3, requires_grad=False), torch.randn(3, 3)]
self.verify_aot_autograd(f, inp)
def test_multi_output(self):
def f(a, b):
return a + b, a - b
inp = [torch.randn(3, 3, requires_grad=True), torch.randn(3, 3)]
self.verify_aot_autograd(f, inp)
inp = [torch.randn(3, 3, requires_grad=False), torch.randn(3, 3)]
self.verify_aot_autograd(f, inp)
def test_multi_output_list(self):
def f(a, b):
return [a + b, a - b]
inp = [torch.randn(3, 3, requires_grad=True), torch.randn(3, 3)]
self.verify_aot_autograd(f, inp)
inp = [torch.randn(3, 3, requires_grad=False), torch.randn(3, 3)]
self.verify_aot_autograd(f, inp)
# Test for bug occurring at the intersection of fake tensors & functionalization.
def test_squeeze_mutation(self):
def f(a):
b = a.clone().squeeze(-1)
b.add_(1.0)
return a + b
inp = [torch.randn(3, 1, requires_grad=True)]
self.verify_aot_autograd(f, inp, dynamic=True)
inp = [torch.randn(3, 1, requires_grad=False)]
self.verify_aot_autograd(f, inp, dynamic=True)
def test_complex_linear(self):
# https://github.com/pytorch/pytorch/issues/93424
inp = [torch.randn(1, 10, 10, dtype=torch.complex64)]
class F(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
self.linear = nn.Linear(10, 10, dtype=torch.complex64)
def forward(self, x):
return self.linear(x).sum().abs()
self.verify_aot_autograd(F(), inp)
def test_embedding_bag_view_dynamic(self):
# Backwards pass tries to wrap a sparse tensor in a FunctionalTensorWrapper;
# test that this works even though the sparse tensor has no storage.
class F(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
self.emb = torch.nn.EmbeddingBag(100, 8, sparse=True)
def forward(self, x, y):
return self.emb(x, y).view(-1)
x = torch.arange(3)
y = torch.arange(3)
self.verify_aot_autograd(F(), [x, y], dynamic=False)
self.verify_aot_autograd(F(), [x, y], dynamic=True)
def test_input_mutation_simple(self):
def f(a):
a.mul_(2)
return a * 3
inp = [torch.ones(3, 3, requires_grad=True)]
fw_graph = self.verify_aot_autograd(f, inp, test_mutation=True)
inp = [torch.ones(3, 3, requires_grad=False)]
self.verify_aot_autograd(f, inp, test_mutation=True)
# Things to note:
# - the extra clone is because we need to pass the pre-mutated input to grad(),
# but autograd operates above functionalization so we need to manually clone.
# Hopefully backends can optimize this easily.
# - The extra return arg is because the compiled forward returns (mutated inputs + outputs)
self.assertExpectedInline(
fw_graph.code.strip(),
"""\
def forward(self, primals_1):
clone = torch.ops.aten.clone.default(primals_1); primals_1 = None
mul = torch.ops.aten.mul.Tensor(clone, 2); clone = None
mul_1 = torch.ops.aten.mul.Tensor(mul, 3)
return (mul, mul_1)""",
)
def test_input_mutation_set__input_mutation(self):
def f(a):
b = torch.arange(9, dtype=a.dtype).reshape(3, 3)
with torch.no_grad():
a.set_(b)
return a * b
inp = [torch.ones(3, 3, requires_grad=True)]
self.verify_aot_autograd(f, inp, test_mutation=True, keep_inp_mutations=True)
inp = [torch.ones(3, 3, requires_grad=False)]
self.verify_aot_autograd(f, inp, test_mutation=True, keep_inp_mutations=True)
def test_set__steals_view_chain(self):
def f(a, b):
a_ = a.mul(2)
b_ = b.mul(2)
b_slice = b_[1].view(3, 3)
# a_clone should inherit the view chain from b_slice
a_.set_(b_slice)
# Also mutates b_,
a_.view(-1).mul_(2)
return a_ * b_slice
inp = [
torch.ones(3, 3, requires_grad=False),
torch.zeros(3, 9, requires_grad=False),
]
self.verify_aot_autograd(f, inp, keep_inp_mutations=True)
@skipIfDynamoInput(
"Test doesn't make sense with dynamo, which changes order of mutations"
)
def test_set__and_data_mutation_good(self):
def f(a, b):
# The data mutation happens *after* the set_(). This is ok (see the graph below)
with torch.no_grad():
a.set_(b)
b.mul_(2)
return a + b
inp = [
torch.ones(3, 3, requires_grad=True),
torch.ones(3, 3, requires_grad=True),
]
fw_graph = self.verify_aot_autograd(
f, inp, test_mutation=True, keep_inp_mutations=True
)
inp = [
torch.ones(3, 3, requires_grad=False),
torch.zeros(3, 3, requires_grad=False),
]
self.verify_aot_autograd(f, inp, test_mutation=True, keep_inp_mutations=True)
# Important things to note:
# - "return a.set_(b)" desugars into "return b"
# - Both a and b are recorded as experiencing mutations,
# which is why we see "b_updated" (output of the mul) twice in the graph outputs.
# a is recorded as both a data mutation and a metadata mutation (due to set_ swapping its storage).
# - the runtime epilogue for a is "a.set_(mul)"
# - the runtime epilogue for b is "b.copy_(mul)"
self.assertExpectedInline(
fw_graph.code.strip(),
"""\
def forward(self, primals_1, primals_2):
mul = torch.ops.aten.mul.Tensor(primals_2, 2)
add = torch.ops.aten.add.Tensor(mul, mul)
set_ = torch.ops.aten.set_.source_Tensor(primals_1, mul); primals_1 = set_ = None
copy_ = torch.ops.aten.copy_.default(primals_2, mul); primals_2 = mul = copy_ = None
return (add,)""",
)
# This is a (hopefully) extremely rare case that is difficult to handle,
# so we ban it.
# https://github.com/pytorch/pytorch/issues/126236
# https://github.com/pytorch/pytorch/pull/126113
@xfailIfTorchDynamo
def test_set__and_data_mutation_bad(self):
def f(a):
a_view = a.view(-1)
tmp = torch.ones(3, 3, requires_grad=True)
# Now, any mutations on either tmp
# will be tracked as graph input mutations.
with torch.no_grad():
a.set_(tmp)
# BAD: a_view is now detached from every graph input,
# so we won't recognize that this caused an input mutation!
a_view.mul_(2)
return a + tmp
inp = [torch.ones(3, 3, requires_grad=True)]
with self.assertRaisesRegex(
RuntimeError, "cannot mutate tensors with frozen storage"
):
self.verify_aot_autograd(
f, inp, test_mutation=True, keep_inp_mutations=True
)
@skipIfDynamoInput(
"Test doesn't make sense with dynamo, which changes order of mutations"
)
def test_set__not_allowed(self):
def f(a, b):
with torch.no_grad():
a.set_(b)
# Mutating a will change a's grad_fn, which requires us to replay the mutation outside of the graph.
# We currently ban this today, when the input also received a set_() input mutation.
a.mul_(2)
return a + b
inp = [
torch.ones(3, 3, requires_grad=True),
torch.ones(3, 3, requires_grad=True),
]
with self.assertRaisesRegex(
AssertionError, "but the input has other mutations that we cannot"
):
fw_graph = self.verify_aot_autograd(
f, inp, test_mutation=True, keep_inp_mutations=True
)
def test_input_mutation_set__nop(self):
def f(a):
b = torch.arange(9, dtype=a.dtype)
a_old = torch.ops.aten.alias.default(a)
with torch.no_grad():
a.set_(b)
a.set_(a_old)
return a + b.reshape(3, 3)
inp = [torch.ones(3, 3, requires_grad=True)]
fw_graph = self.verify_aot_autograd(
f, inp, test_mutation=True, keep_inp_mutations=True
)
inp = [torch.ones(3, 3, requires_grad=False)]
self.verify_aot_autograd(f, inp, test_mutation=True, keep_inp_mutations=True)
# Things to note:
# - There are no set_() calls in the graph (we functionalize a.set_(b) into "b")
# - There is only **1** graph output. We properly realized that the two set_() calls
# undo each other, and so effectively no inputs are mutated.
self.assertExpectedInline(
fw_graph.code.strip(),
"""\
def forward(self, primals_1):
arange = torch.ops.aten.arange.default(9, dtype = torch.float32, device = device(type='cpu'), pin_memory = False)
alias = torch.ops.aten.alias.default(primals_1); primals_1 = None
view = torch.ops.aten.view.default(arange, [3, 3]); arange = None
add = torch.ops.aten.add.Tensor(alias, view); alias = view = None
return (add,)""",
)
def test_input_mutation_simple_with_none_and_nontensor(self):
# Tensor, None, int
def f(a, b, c):
return a * c
f_compiled = aot_function(f, nop)
for req_grad in [True, False]:
inp = [torch.ones(3, 3, requires_grad=req_grad), None, 3]
out_ref = f(*inp)
out_test = f_compiled(*inp)
self.assertEqual(out_ref, out_test)
# https://github.com/pytorch/pytorch/issues/93363
def test_mutates_input_noncontiguous(self):
def f(a):
a.add_(1)
return ()
f_compiled = aot_function(f, nop)
ref = torch.ones(4, requires_grad=True) + 0
ref_view = ref[0::2]
test = torch.ones(4, requires_grad=True) + 0
test_view = test[0::2]
out_ref = f(ref_view)
out_test = f_compiled(test_view)
self.assertEqual(ref, test)
def test_input_mutation_modifies_autograd_meta_of_aliases(self):
def f(a):
a.mul_(2)
out = a + 1
return out.detach()
x_ref = torch.ones(3, 3, requires_grad=True).clone()
x_ref_view = x_ref.view(3, 3)
x_test = torch.ones(3, 3, requires_grad=True).clone()
x_test_view = x_test.view(3, 3)
f_compiled = aot_function(f, nop, keep_inference_input_mutations=True)
f(x_ref)
f_compiled(x_test)
# f will mutate aliases of the input, including its autograd metadata!
# y.grad_fn is AsStridedBackward
self.assertEqual(x_ref_view, x_test_view)
self.assertEqual(x_ref_view._version, x_test_view._version)
self.assertEqual(x_ref_view.grad_fn.__class__, x_test_view.grad_fn.__class__)
# Test the actual gradients are correct
(x_ref * x_ref_view).sum().backward()
(x_test * x_test_view).sum().backward()
self.assertEqual(x_ref.grad, x_test.grad)
self.assertEqual(x_ref_view.grad, x_test_view.grad)
@skipIfTorchDynamo("https://github.com/pytorch/pytorch/issues/127470")
def test_nested_subclasses(self):
@torch.compile(backend="aot_eager")
def f(x):
return x.sin().cos()
a = torch.ones(4, requires_grad=True)
a2 = a.detach().clone().requires_grad_()
a3 = a.detach().clone().requires_grad_()
a4 = a.detach().clone().requires_grad_()
aa = TwoTensor(a, a2)
aa2 = TwoTensor(a3, a4)
aaaa = TwoTensor(aa, aa2)
out = f(aaaa)
self.assertTrue(isinstance(out, TwoTensor))
self.assertTrue(isinstance(out.a, TwoTensor))
self.assertTrue(isinstance(out.b, TwoTensor))
self.assertTrue(isinstance(out.a.a, torch.Tensor))
self.assertTrue(isinstance(out.a.b, torch.Tensor))
self.assertTrue(isinstance(out.b.a, torch.Tensor))
self.assertTrue(isinstance(out.b.b, torch.Tensor))
out.sum().backward()
self.assertTrue(isinstance(aaaa.grad, TwoTensor))
self.assertTrue(isinstance(aaaa.grad.a, TwoTensor))
self.assertTrue(isinstance(aaaa.grad.b, TwoTensor))
@skipIfTorchDynamo("https://github.com/pytorch/pytorch/issues/127470")
def test_nested_subclasses_non_nested_grad(self):
@torch.compile(backend="aot_eager")
def f(x):
return x.sin().cos()
a = torch.ones(4, requires_grad=True)
a2 = a.detach().clone().requires_grad_()
a3 = a.detach().clone().requires_grad_()
a4 = a.detach().clone().requires_grad_()
new_aa = TwoTensor(a3, a4)
aa = TwoTensor(a, a2)
aa2 = aa.detach().clone().requires_grad_()
aaaa = TwoTensor(aa, aa2)
out = f(new_aa)
new_out = out + aaaa
with self.assertRaisesRegex(
RuntimeError,
"""
During the backward, we encountered a tensor subclass where we guessed its
metadata incorrectly.
""", # noqa: F541
):
new_out.sum().backward()
@unittest.skipIf(IS_WINDOWS, "Windows isn't supported for this case")
@skipIfTorchDynamo("https://github.com/pytorch/pytorch/issues/127470")
def test_custom_tensor_metadata(self):
def f(x):
x_elem = x.elem
x_elem_elem = x_elem.elem
x_elem_metadata = x_elem.constant_attribute
return x * x_elem * x_elem_elem * x_elem_metadata
a = torch.ones(4, requires_grad=True)
custom_a = ConstantExtraMetadataTensor(a)
custom_a.constant_attribute = 6
custom_aa = ConstantExtraMetadataTensor(custom_a)
custom_aa.constant_attribute = 4
custom_aa_compile = custom_aa.detach().clone().requires_grad_()
custom_aa_compile.elem.constant_attribute = 6
out_eager = f(custom_aa)
compiled_f = torch.compile(f, backend="aot_eager")
out = compiled_f(custom_aa_compile)
self.assertTrue(torch.allclose(out_eager, out))
out.sum().backward()
self.assertTrue(isinstance(custom_aa_compile.grad, ConstantExtraMetadataTensor))
self.assertTrue(
isinstance(custom_aa_compile.grad.elem, ConstantExtraMetadataTensor)
)
@skipIfTorchDynamo("https://github.com/pytorch/pytorch/issues/127470")
def test_nested_subclasses_complicated_inps(self):
def f(x, y, z):
temp = x + y
temp_plain = x.a + y.b
res = temp.sum() + temp_plain.sum()
return x.sin().cos() + res
x = torch.ones(4, requires_grad=True)
x2 = x.detach().clone().requires_grad_()
xx = TwoTensor(x, x2)
xx2 = xx.detach().clone().requires_grad_()
x_nested = TwoTensor(xx, xx2)
x_nested_compile = x_nested.detach().clone().requires_grad_()
y_nested = x_nested.detach().clone().requires_grad_()
y_nested_compile = y_nested.detach().clone().requires_grad_()
z = x.detach().clone().requires_grad_()
z_compile = z.detach().clone().requires_grad_()
out_eager = f(x_nested, y_nested, z)
compiled_f = torch.compile(f, backend="aot_eager")
out = compiled_f(x_nested_compile, y_nested_compile, z_compile)
self.assertTrue(torch.allclose(out_eager, out))
self.assertTrue(isinstance(out, TwoTensor))
self.assertTrue(isinstance(out.a, TwoTensor))
self.assertTrue(isinstance(out.b, TwoTensor))
self.assertTrue(isinstance(out.a.a, torch.Tensor))
self.assertTrue(isinstance(out.a.b, torch.Tensor))
self.assertTrue(isinstance(out.b.a, torch.Tensor))
self.assertTrue(isinstance(out.b.b, torch.Tensor))
out.sum().backward()
out_eager.sum().backward()
self.assertTrue(isinstance(x_nested_compile.grad, TwoTensor))
self.assertTrue(isinstance(x_nested_compile.grad.a, TwoTensor))
self.assertTrue(isinstance(x_nested_compile.grad.b, TwoTensor))
self.assertTrue(isinstance(y_nested_compile.grad, TwoTensor))
self.assertTrue(isinstance(y_nested_compile.grad.a, TwoTensor))
self.assertTrue(isinstance(y_nested_compile.grad.b, TwoTensor))
self.assertTrue(torch.allclose(x_nested_compile.grad.a.a, x_nested.grad.a.a))
self.assertTrue(torch.allclose(x_nested_compile.grad.a.b, x_nested.grad.a.b))
self.assertTrue(torch.allclose(y_nested_compile.grad.a.a, y_nested.grad.a.a))
self.assertTrue(torch.allclose(y_nested_compile.grad.a.b, y_nested.grad.a.b))
@unittest.skipIf(IS_WINDOWS, "Windows isn't supported for this case")
@skipIfTorchDynamo("https://github.com/pytorch/pytorch/issues/127470")
def test_nested_subclasses_complicated_inps_mixed(self):
def f(x, y):
y_elem = y.elem
y_elem_elem = y_elem.elem
y_elem_metadata = y_elem.constant_attribute
return y * y_elem * y_elem_elem * y_elem_metadata + x
x = torch.ones(4, requires_grad=True)
x2 = x.detach().clone().requires_grad_()
xx = TwoTensor(x, x2)
xx2 = xx.detach().clone().requires_grad_()
x_nested = TwoTensor(xx, xx2)
x_nested_compile = x_nested.detach().clone().requires_grad_()
a = torch.ones(4, requires_grad=True)
custom_a = ConstantExtraMetadataTensor(a)
custom_a.constant_attribute = 6
custom_aa = ConstantExtraMetadataTensor(custom_a)
custom_aa.constant_attribute = 4
custom_aa_compile = custom_aa.detach().clone().requires_grad_()
custom_aa_compile.constant_attribute = 4
custom_aa_compile.elem.constant_attribute = 6
compiled_f = torch.compile(f, backend="aot_eager")
out_eager = f(x_nested, custom_aa)
out = compiled_f(x_nested_compile, custom_aa_compile)
self.assertTrue(torch.allclose(out_eager, out))
out.sum().backward()
out_eager.sum().backward()
self.assertTrue(torch.allclose(x_nested_compile.grad, x_nested.grad))
self.assertTrue(torch.allclose(custom_aa_compile.grad, custom_aa.grad))
@skipIfTorchDynamo("This test suite already uses dynamo")
def test_composite_impl_compile(self):
class Foo(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
self.linear = torch.nn.Linear(3, 3)
def forward(self, a):
return self.linear(a)
inp = [torch.ones(3, 3, requires_grad=True)]
fw_graph = self.verify_aot_autograd(Foo(), inp, test_mutation=True)
inp = [torch.ones(3, 3, requires_grad=False)]
self.assertExpectedInline(
fw_graph.code.strip(),
"""\
def forward(self, primals_1, primals_2, primals_3):
t = torch.ops.aten.t.default(primals_1); primals_1 = None
addmm = torch.ops.aten.addmm.default(primals_2, primals_3, t); primals_2 = None
return (addmm, primals_3, t)""",
)
with torch.inference_mode():
fw_graph = self.verify_aot_autograd(Foo(), inp, test_mutation=True)
inp = [torch.ones(3, 3, requires_grad=False)]
self.assertExpectedInline(
fw_graph.code.strip(),
"""\
def forward(self, arg0_1, arg1_1, arg2_1):
t = torch.ops.aten.t.default(arg0_1); arg0_1 = None
addmm = torch.ops.aten.addmm.default(arg1_1, arg2_1, t); arg1_1 = arg2_1 = t = None
return (addmm,)""",
)
def test_outputs_are_aliased(self):
# Tensor, None, int
def f(a):
b = a.mul(2)
c = b.view(-1)
return b, c
f_compiled = aot_function(f, nop)
for req_grad in [True, False]:
inp = torch.ones(3, requires_grad=req_grad)
out_ref = f(inp)
out_test = f_compiled(inp)
self.assertEqual(out_ref[0], out_test[0])
self.assertEqual(out_ref[1], out_test[1])
# Try mutating one of the outputs, which is aliased.
out_ref[0].mul_(3)
out_test[0].mul_(3)
# Assert that the aliasing relationship was preserved
self.assertEqual(out_ref[0], out_test[0])
self.assertEqual(out_ref[1], out_test[1])
def test_input_mutation_is_output(self):
def f(a):
a.mul_(2)
return a
inp = [torch.ones(3, 3, requires_grad=True)]
fw_graph = self.verify_aot_autograd(f, inp, test_mutation=True)
inp = [torch.ones(3, 3, requires_grad=False)]
self.verify_aot_autograd(f, inp, test_mutation=True)
self.assertExpectedInline(
fw_graph.code.strip(),
"""\
def forward(self, primals_1):
clone = torch.ops.aten.clone.default(primals_1); primals_1 = None
mul = torch.ops.aten.mul.Tensor(clone, 2); clone = None
return (mul, mul)""",
)
def test_input_mutation_multiple(self):
def f(a, b, c):
a.mul_(2)
c.mul_(2)
return a + b + c
def create_inp(req_grad):
return [
torch.ones(3, 3, requires_grad=req_grad),
torch.ones(3, 3, requires_grad=req_grad),
torch.ones(3, 3, requires_grad=req_grad),
]
self.verify_aot_autograd(f, create_inp(False), test_mutation=True)
fw_graph = self.verify_aot_autograd(f, create_inp(True), test_mutation=True)
self.assertExpectedInline(
fw_graph.code.strip(),
"""\
def forward(self, primals_1, primals_2, primals_3):
clone = torch.ops.aten.clone.default(primals_1); primals_1 = None
clone_1 = torch.ops.aten.clone.default(primals_3); primals_3 = None
mul = torch.ops.aten.mul.Tensor(clone, 2); clone = None
mul_1 = torch.ops.aten.mul.Tensor(clone_1, 2); clone_1 = None
add = torch.ops.aten.add.Tensor(mul, primals_2); primals_2 = None
add_1 = torch.ops.aten.add.Tensor(add, mul_1); add = None
return (mul, mul_1, add_1)""",
)
def test_input_mutation_return(self):
def f(a, b):
return torch.sin(a, out=b)
inp = [torch.randn(3, 3), torch.ones(3, 3)]
fw_graph = self.verify_aot_autograd(
f, inp, test_mutation=True, keep_inp_mutations=True
)
self.assertExpectedInline(
fw_graph.code.strip(),
"""\
def forward(self, arg0_1, arg1_1):
sin = torch.ops.aten.sin.default(arg0_1); arg0_1 = None
copy_ = torch.ops.aten.copy_.default(arg1_1, sin); arg1_1 = sin = None
return (copy_,)""",
)
def test_input_mutation_metadata(self):
def f(a, b):
a.transpose_(1, 0)
return a + b
def create_inp(req_grad):
return [
torch.ones(3, 3, requires_grad=req_grad),
torch.ones(3, 3, requires_grad=req_grad),
]
self.verify_aot_autograd(f, create_inp(True), test_mutation=True)
self.verify_aot_autograd(f, create_inp(False), test_mutation=True)
def test_input_mutation_storage_resize_up(self):
def f(a):
torch.ops.inductor.resize_storage_bytes_(a, 32)
# float32, 4 bytes per element, 32 bytes == 8 elements
with torch.no_grad():
a.copy_(torch.ones(8))
return a + 1
inp = torch.zeros(8, requires_grad=True)
# Input starts with zero-size-storage
inp.untyped_storage().resize_(0)
fw_graph_cell = [None]
compiled_f = aot_function(
f,
fw_compiler=make_boxed_compiler(
partial(extract_graph, graph_cell=fw_graph_cell)
),
bw_compiler=nop,
decompositions={},
keep_inference_input_mutations=True,
dynamic=False,
)
out = compiled_f(inp)
# Final functionalized graph has two mutation ops:
# (1) a resize_() to resize input tensor up
# (2) a copy_() to fill in the resized input with valid data
self.assertExpectedInline(
fw_graph_cell[0].code.strip(),
"""\
def forward(self, primals_1):
resize_storage_bytes_ = torch.ops.inductor.resize_storage_bytes_.default(primals_1, 32); resize_storage_bytes_ = None
ones = torch.ops.aten.ones.default([8], device = device(type='cpu'), pin_memory = False)
copy = torch.ops.aten.copy.default(primals_1, ones); ones = None
add = torch.ops.aten.add.Tensor(copy, 1)
copy_ = torch.ops.aten.copy_.default(primals_1, copy); primals_1 = copy = copy_ = None
return (add,)""",
)
def test_input_mutation_storage_resize_down(self):
def f(a):
out = a.sin()
torch.ops.inductor.resize_storage_bytes_(a, 0)
return out
inp = torch.zeros(8, requires_grad=True)
fw_graph_cell = [None]
compiled_f = aot_function(
f,
fw_compiler=make_boxed_compiler(
partial(extract_graph, graph_cell=fw_graph_cell)
),
bw_compiler=nop,
decompositions={},
keep_inference_input_mutations=True,
dynamic=False,
)
out = compiled_f(inp)
# Final functionalized graph has one mutation ops:
# (1) a resize_() to resize input tensor down
# Even though there was technically a "data mutation" on the input (from a.copy_()),
# We don't include it in the graph since the final input size has zero storage
self.assertExpectedInline(
fw_graph_cell[0].code.strip(),
"""\
def forward(self, primals_1):
sin = torch.ops.aten.sin.default(primals_1)
resize_storage_bytes_ = torch.ops.inductor.resize_storage_bytes_.default(primals_1, 0); resize_storage_bytes_ = None
return (sin, primals_1)""",
)
# def test_input_mutation_storage_resize_up_down(self):
# def f(a):
# torch.ops.inductor.resize_storage_bytes_(a, 32)
# # float32, 4 bytes per element, 32 bytes == 8 elements
# with torch.no_grad():
# a.copy_(torch.ones(8))
# out = a.sin()
# torch.ops.inductor.resize_storage_bytes_(a, 0)
# return out
# inp = torch.zeros(8, requires_grad=True)
# # Input starts with zero-size-storage
# inp.untyped_storage().resize_(0)
# fw_graph_cell = [None]
# compiled_f = aot_function(
# f,
# fw_compiler=make_boxed_compiler(
# partial(extract_graph, graph_cell=fw_graph_cell)
# ),
# bw_compiler=nop,
# decompositions={},
# keep_inference_input_mutations=True,
# dynamic=False,
# )
# out = compiled_f(inp)
# # Final graph has two interesting properties:
# # (1) no resizes in the functional graph, since the two resizes cancel out
# # and the final size is zero
# # (2) no copy_ in the functional graph, even though we copied data into the input,
# # because the input has no storage at the end of graph execution (so no data to copy)
# self.assertExpectedInline(
# fw_graph_cell[0].code.strip(),
# """\
# def forward(self, primals_1):
# ones = torch.ops.aten.ones.default([8], device = device(type='cpu'), pin_memory = False)
# copy = torch.ops.aten.copy.default(primals_1, ones); primals_1 = ones = None
# sin = torch.ops.aten.sin.default(copy)
# return [sin, copy]""",
# )
def test_input_mutation_storage_resize_down_and_set_(self):
# Meant to mimic ppFSDP
class TracableCreateParameter(torch.autograd.Function):
@staticmethod
def forward(ctx, tensor, placeholder):
assert not tensor.requires_grad
return placeholder.set_(tensor)
@staticmethod
def backward(ctx, grad):
return None, grad # grad flows to placeholder
def f(dummy_param, param_shard):
# simulate allgather
with torch.no_grad():
allgather_param = torch.cat([param_shard, param_shard])
# simulate propagating grad state through dummy param, using data of allgather param
dummy_param_with_grad_state = TracableCreateParameter.apply(
allgather_param, dummy_param
)
out = dummy_param.sin()
# Resize out dummy param, which now has the allgather data
torch.ops.inductor.resize_storage_bytes_(dummy_param, 0)
return out
# Simulates the local shard of our param
param_shard = torch.zeros(8, requires_grad=True)
# The dummy, zero-sized allgathered param that autograd will actually compute gradients on
dummy_param = torch.zeros(16, requires_grad=True)
dummy_param.untyped_storage().resize_(0)
fw_graph_cell = [None]
compiled_f = aot_function(
f,
fw_compiler=make_boxed_compiler(
partial(extract_graph, graph_cell=fw_graph_cell)
),
bw_compiler=nop,
decompositions={},
keep_inference_input_mutations=True,
dynamic=False,
)
out = compiled_f(dummy_param, param_shard)
# Important stuff to point out:
# (1) We save cat for backward (input to the sin()).
# While the original code was dummy_param.sin(),
# dummy_param actually contains the `cat` tensor due to the set_() call
# (2) We emit a cat.resize_storage_(0) in the graph.
# After the set_(), cat is the actually data of dummy_param, which is what we call resize_() on
self.assertExpectedInline(
fw_graph_cell[0].code.strip(),
"""\
def forward(self, primals_1, primals_2):
cat = torch.ops.aten.cat.default([primals_2, primals_2]); primals_2 = None
sin = torch.ops.aten.sin.default(cat)
resize_storage_bytes_ = torch.ops.inductor.resize_storage_bytes_.default(cat, 0); resize_storage_bytes_ = None
set_ = torch.ops.aten.set_.source_Tensor(primals_1, cat); primals_1 = set_ = None
return (sin, cat)""",
)
def test_input_mutation_storage_resize_before_set_(self):
def f(a):
with torch.no_grad():
torch.ops.inductor.resize_storage_bytes_(a, 0)
a.set_(torch.ones(2))
inp = torch.zeros(8, requires_grad=True)
compiled_f = aot_function(
f,
fw_compiler=nop,
bw_compiler=nop,
decompositions={},
keep_inference_input_mutations=True,
dynamic=False,
)
out = compiled_f(inp)
# def test_input_mutation_storage_resize_not_supported(self):
# def f(a):
# a.mul_(2)
# torch.ops.inductor.resize_storage_bytes_(a, 0)
# return a
# inp = torch.zeros(8, requires_grad=True)
# with self.assertRaisesRegex(
# AssertionError, "the input has other mutations that we cannot"
# ):
# compiled_f = aot_function(
# f,
# fw_compiler=nop,
# bw_compiler=nop,
# decompositions={},
# keep_inference_input_mutations=True,
# dynamic=False,
# )
# out = compiled_f(inp)
def test_input_output_aliase_custom_autograd_function(self):
class Foo(torch.autograd.Function):
@staticmethod
def forward(ctx, x):
return x
@staticmethod
def backward(ctx, gx):
return gx * 0.5
def f(x):
return Foo.apply(x)
inp = [torch.ones(2, 2, requires_grad=True)]
self.verify_aot_autograd(f, inp, test_mutation=False)
def test_input_mutation_requires_grad_detach(self):
# Here, "a" requires grad, and gets mutated, so we append a copy_() to the end of the graph.
# Its mutation doesn't take part in autograd though, because we mutated a detach'd view.
# Need to make sure that this copy_() doesn't error, and doesn't participate in autograd either.
def f(a):
a.detach().mul_(2)
return a + 3
inp = [torch.ones(4, requires_grad=True)]
self.verify_aot_autograd(f, inp, test_mutation=False)
inp = [torch.ones(4, requires_grad=True)]
# test_mutation=True will first do some compute on inp, so it is no longer an autograd leaf
# by the time it becomes a graph input. Good to test both cases.
self.verify_aot_autograd(f, inp, test_mutation=True)
def test_input_mutation_hidden_from_autograd_aliasing(self):
def f(a):
a_alias = a.view(-1)
with torch.no_grad():
a_alias.mul_(2)
return a + 1
inp = [torch.ones(4, requires_grad=True)]
# The important bit: we detected that the input mutation is safe
# to include **inside** the graph, since it was under no_grad
# (so all we need to do is use mark_dirty() on the input to bump the VC)
fw_graph = self.verify_aot_autograd(
f, inp, test_mutation=True, keep_inp_mutations=True
)
self.assertExpectedInline(
fw_graph.code.strip(),
"""\
def forward(self, primals_1):
view = torch.ops.aten.view.default(primals_1, [-1])
mul = torch.ops.aten.mul.Tensor(view, 2); view = None
view_1 = torch.ops.aten.view.default(mul, [4]); mul = None
add = torch.ops.aten.add.Tensor(view_1, 1)
copy_ = torch.ops.aten.copy_.default(primals_1, view_1); primals_1 = view_1 = copy_ = None
return (add,)""",
)
def test_input_mutation_requires_grad_no_grad(self):
def f(a):
with torch.no_grad():
a.mul_(2)
return a + 3
inp = [torch.ones(4, requires_grad=True)]
fw_graph = self.verify_aot_autograd(
f, inp, test_mutation=True, keep_inp_mutations=True
)
# Even though the input requires_grad, we expect the keep the input mutation in the graph
# (Even though this is a training graph!)
self.assertExpectedInline(
fw_graph.code.strip(),
"""\
def forward(self, primals_1):
mul = torch.ops.aten.mul.Tensor(primals_1, 2)
add = torch.ops.aten.add.Tensor(mul, 3)
copy_ = torch.ops.aten.copy_.default(primals_1, mul); primals_1 = mul = copy_ = None
return (add,)""",
)
def test_input_mutation_requires_grad_no_grad_inference_graph(self):
def f(a):
with torch.no_grad():
a.mul_(2)
return a + 3
inp = [torch.ones(4, requires_grad=True)]
# Even though the input requires_grad, we expect the keep the input mutation in the graph
fw_graph = self.verify_aot_autograd(
f, inp, test_mutation=True, keep_inp_mutations=True
)
self.assertExpectedInline(
fw_graph.code.strip(),
"""\
def forward(self, arg0_1):
mul = torch.ops.aten.mul.Tensor(arg0_1, 2)
add = torch.ops.aten.add.Tensor(mul, 3)
copy_ = torch.ops.aten.copy_.default(arg0_1, mul); arg0_1 = mul = copy_ = None
return (add,)""",
)
def test_input_mutation_requires_grad_no_grad_detach_mixed(self):
# Perform a mix of mutations on a:
# 1 normal, 1 in no_grad, 1 on a detach'd tensor.
# Only the first should participate in gradient computation.
def f(a):
a.detach().mul_(2)
a.mul_(3)
with torch.no_grad():
a.mul_(4)
return a + 5
inp = [torch.ones(4, requires_grad=True)]
fw_graph = self.verify_aot_autograd(f, inp, test_mutation=True)
def test_input_mutation_metadata2(self):
def f(a):
a.transpose_(1, 0)
a.mul_(2)
return a + 1
inp = [torch.ones(3, 3, requires_grad=True)]
self.verify_aot_autograd(f, inp, test_mutation=True)
inp = [torch.ones(3, 3, requires_grad=False)]
self.verify_aot_autograd(f, inp, test_mutation=True)
def test_input_mutation_batchnorm(self):
def f(inpt, weight, bias, running_mean, running_var):
# This is additionally a good test, because the input tensors that we mutate
# are *also* saved for backwards.
# This tests that what we save for the backward is actually cloned inputs,
# and not the original inputs that got mutated.
return torch._native_batch_norm_legit(
inpt, weight, bias, running_mean, running_var, True, 0.5, 1e-5
)
def create_inp(req_grad):
return [
torch.ones(2, 5, 5, 5, requires_grad=req_grad),
torch.ones(5, requires_grad=req_grad),
torch.ones(5, requires_grad=req_grad),
torch.ones(5),
torch.ones(5),
]
from torch._decomp import get_decompositions
# This simulates what inductor does (running the fw + bw decompositions)
decompositions = get_decompositions(
[
torch.ops.aten._native_batch_norm_legit_functional,
torch.ops.aten.native_batch_norm_backward,
]
)
self.verify_aot_autograd(
f, create_inp(True), test_mutation=True, decompositions=decompositions
)
self.verify_aot_autograd(
f, create_inp(False), test_mutation=True, decompositions=decompositions
)
def test_batchnorm_inference(self):
inp = [
torch.ones(2, 5, 5, 5, requires_grad=True),
torch.ones(5, requires_grad=True),
torch.ones(5, requires_grad=True),
torch.ones(5),
torch.ones(5),
]
m = torch.nn.BatchNorm2d(4, 4)
m.eval()
fw_graph_cell = [None]
inp = torch.ones(4, 4, 4, 4)
fw_graph_cell = [None]
compiled_m = aot_module(
m,
fw_compiler=partial(extract_graph, graph_cell=fw_graph_cell),
bw_compiler=nop,
keep_inference_input_mutations=True,
)
inp = torch.ones(4, 4, 4, 4)
with torch.no_grad():
out = compiled_m(inp)
# expectation: there are no copy_() calls in the decomposed batch norm when running under training=False (eval mode)
code = fw_graph_cell[0].code.strip()
self.assertTrue("copy_" not in str(code))
def test_input_output_view_simple(self):
def f(a):
return a.view(-1)
inp = [torch.ones(2, 2, requires_grad=False).add(1)]
self.verify_aot_autograd(f, inp, test_mutation=True)
inp = [torch.ones(2, 2, requires_grad=True).add(1)]
fw_graph = self.verify_aot_autograd(f, inp, test_mutation=True)
# Outputs that alias inputs are pulled out of the graph entirely, so we don't compile anything here
self.assertExpectedInline(
fw_graph.code.strip(),
"""\
def forward(self, arg0_1):
view = torch.ops.aten.view.default(arg0_1, [-1]); arg0_1 = None
return (view,)""",
)
def test_input_output_view_mutate_multiple(self):
def f(a, b, c):
a.mul_(2)
c.mul_(3)
return b.view(2, 2), c.view(2, 2)
def create_inp(req_grad):
return [
torch.ones(2, 2, requires_grad=req_grad).add(1),
torch.ones(2, 2, requires_grad=req_grad).add(1),
torch.ones(2, 2, requires_grad=req_grad).add(1),
]
self.verify_aot_autograd(f, create_inp(False), test_mutation=True)
fw_graph = self.verify_aot_autograd(f, create_inp(True), test_mutation=True)
# The original function returned two outputs, both of which aliased inputs.
# We expect two outputs in the functional graph, a_updated and c_updated.
# The actual aliased outputs themselves aren't in the compiled forward graph;
# Instead, they're generated outside of the graph.
self.assertExpectedInline(
fw_graph.code.strip(),
"""\
def forward(self, primals_1, primals_2, primals_3):
clone = torch.ops.aten.clone.default(primals_1); primals_1 = None
clone_1 = torch.ops.aten.clone.default(primals_3); primals_3 = None
mul = torch.ops.aten.mul.Tensor(clone, 2); clone = None
mul_1 = torch.ops.aten.mul.Tensor(clone_1, 3); clone_1 = None
view = torch.ops.aten.view.default(primals_2, [2, 2]); primals_2 = None
view_2 = torch.ops.aten.view.default(mul_1, [2, 2])
return (mul, mul_1, view, view_2)""",
)
def test_input_output_view_metadata_mutate_multiple(self):
def f(a, b, c):
b.mul_(3)
c.t_()
return a.view(2, 2), b.view(2, 2), c.view(2, 2)
def create_inp(req_grad):
return [
torch.ones(2, 2, requires_grad=req_grad).add(1),
torch.ones(2, 2, requires_grad=req_grad).add(1),
torch.ones(2, 2, requires_grad=req_grad).add(1),
]
self.verify_aot_autograd(f, create_inp(False), test_mutation=True)
fw_graph = self.verify_aot_autograd(f, create_inp(True), test_mutation=True)
# Important thing to check here: of the three inputs:
# Only the b.mul_(3) should show up in the graph (we functionalize it and return it).
# Everything else that does not show up in the graph includes:
# - The metadata mutation on c (we do it outside the graph)
# - All 3 original fw outputs, which are aliases of inputs (we regenerate them outside of the graph)
self.assertExpectedInline(
fw_graph.code.strip(),
"""\
def forward(self, primals_1, primals_2, primals_3):
clone = torch.ops.aten.clone.default(primals_2); primals_2 = None
view = torch.ops.aten.view.default(primals_3, [2, 2]); primals_3 = None
mul = torch.ops.aten.mul.Tensor(clone, 3); clone = None
t = torch.ops.aten.t.default(view); view = None
view_1 = torch.ops.aten.view.default(primals_1, [2, 2]); primals_1 = None
view_3 = torch.ops.aten.view.default(t, [2, 2])
view_4 = torch.ops.aten.view.default(mul, [2, 2])
return (mul, t, view_1, view_4, view_3)""",
)
def test_input_mutation_and_output_view(self):
def f(a):
a.add_(1)
return a.view(-1)
inp = [torch.ones(2, 2, requires_grad=False).add(1)]
self.verify_aot_autograd(f, inp, test_mutation=True)
inp = [torch.ones(2, 2, requires_grad=True).add(1)]
fw_graph = self.verify_aot_autograd(f, inp, test_mutation=True)
# Here, total # of outputs is 1 because:
# - num_mutated_inps = 1 (a_updated)
# - num_fw_outputs = 0 (the output is an alias of the input, so we move it outside the compiled fw)
self.assertExpectedInline(
fw_graph.code.strip(),
"""\
def forward(self, primals_1):
clone = torch.ops.aten.clone.default(primals_1); primals_1 = None
add = torch.ops.aten.add.Tensor(clone, 1); clone = None
view_1 = torch.ops.aten.view.default(add, [-1])
return (add, view_1)""",
)
def test_input_mutation_output_view_multiple(self):
def f(a, b, c, d):
b.transpose_(1, 0)
c.add_(1)
return d + 1, b.diagonal(), a + c
def create_inp(req_grad):
return [
torch.arange(4, requires_grad=req_grad, dtype=torch.float32)
.view(2, 2)
.add(1),
torch.arange(4, requires_grad=req_grad, dtype=torch.float32)
.view(2, 2)
.add(1),
torch.ones(2, 2, requires_grad=req_grad).add(1),
torch.ones(2, 2, requires_grad=req_grad).add(1),
]
self.verify_aot_autograd(f, create_inp(False), test_mutation=True)
fw_graph = self.verify_aot_autograd(f, create_inp(True), test_mutation=True)
self.assertExpectedInline(
fw_graph.code.strip(),
"""\
def forward(self, primals_1, primals_2, primals_3, primals_4):
view = torch.ops.aten.view.default(primals_2, [2, 2]); primals_2 = None
clone = torch.ops.aten.clone.default(primals_3); primals_3 = None
transpose = torch.ops.aten.transpose.int(view, 1, 0); view = None
add = torch.ops.aten.add.Tensor(clone, 1); clone = None
add_1 = torch.ops.aten.add.Tensor(primals_4, 1); primals_4 = None
diagonal = torch.ops.aten.diagonal.default(transpose)
add_2 = torch.ops.aten.add.Tensor(primals_1, add); primals_1 = None
return (transpose, add, add_1, diagonal, add_2)""",
)
def test_output_aliases_intermediate_single(self):
def f(a):
out = torch.mul(a, 3)
return out.view(-1)
inp = [torch.ones(3, 3, requires_grad=False)]
self.verify_aot_autograd(f, inp, test_mutation=True)
inp = [torch.ones(3, 3, requires_grad=True)]
fw_graph = self.verify_aot_autograd(f, inp, test_mutation=True)
# In AOTAutograd, we are obligated to make the compiled forward directly return `out`,
# and reconstruct `out.view(-1)` as a fresh output.
self.assertExpectedInline(
fw_graph.code.strip(),
"""\
def forward(self, primals_1):
mul = torch.ops.aten.mul.Tensor(primals_1, 3); primals_1 = None
view = torch.ops.aten.view.default(mul, [-1]); mul = None
return (view,)""",
)
def test_output_aliases_input_multi_output_view_should_raise_autograd_error(self):
def f1(a):
return list(a.unbind(0))
f1_compiled = aot_function(f1, nop)
inp1 = torch.ones(3, 3, requires_grad=True).clone()
inp2 = torch.ones(3, 3, requires_grad=True).clone()
inp3 = torch.ones(3, 3, requires_grad=True).clone()
with self.assertRaisesRegex(
RuntimeError, "Such functions do not allow the output views"
):
out_test1 = f1_compiled(inp1)
# This raises a runtime error from autograd in eager mode
out_test1[0].mul_(2)
with self.assertRaisesRegex(
RuntimeError, "Such functions do not allow the output views"
):
out_test2 = f1_compiled(inp2)
inp2.mul_(2)
# In eager mode, if we mutate a tensor, any multi-output-view aliases
# get their grad_fn replaced with error nodes, so accessing grad_fn should error
grad_fn = out_test2[0].grad_fn
with self.assertRaisesRegex(
RuntimeError, "Such functions do not allow the output views"
):
out_test3 = f1_compiled(inp3)
out_test1[0].detach().mul_(2)
# The above case also applies to detached aliases (they turn the multi-output-view
# alias's grad_fns into error nodes)
grad_fn = out_test2[0].grad_fn
def test_output_aliases_input_multi_output_view(self):
# All aliased outs are from multi-output views, so AOTAutograd will hide the aliasing from autograd.
def f1(a):
return list(a.unbind(0))
inp = torch.ones(3, 3, requires_grad=True)
inp_ref = torch.ones(3, 3, requires_grad=True)
f1_compiled = aot_function(f1, nop)
out_ref = f1(inp_ref)
out_test = f1_compiled(inp)
# Assert that we get CompiledFunctionBackward in the backward graph,
# and not AsStridedBackward. No view-regeneration necessary for this mult-output view case.
# See Note: [AOTAutograd: differentiable outputs that alias each other from a multi-output view call]
self.assertTrue(
all("CompiledFunctionBackward" in str(o.grad_fn) for o in out_test)
)
sum(out_ref).sum().backward()
sum(out_test).sum().backward()
self.assertEqual(inp_ref.grad, inp.grad)
# Several of the outputs are from multi-output views.
# However: they are part of the same alias set as "a", and "a.view(out.shape)",
# which are both user-visible.
# AOTAutograd will not try to be smart here and hide the aliasing relationships from autograd.
# Instead, it will perform its "output aliases input" logic, and regenerate all aliases.
def f3(a):
return *list(a.unbind(0)), a.view(a.shape)
inp = torch.ones(3, 3, requires_grad=True)
inp_ref = torch.ones(3, 3, requires_grad=True)
f3_compiled = aot_function(f3, nop)
inp_ref_clone = inp_ref.clone()
inp_clone = inp.clone()
out_ref = f3(inp_ref_clone)
out_test = f3_compiled(inp_clone)
self.assertTrue(all("UnbindBackward" in str(o.grad_fn) for o in out_test[:3]))
# The last output is not from a multi-output view, so autograd will let us mutate it.
out_ref[-1].mul_(2)
out_test[-1].mul_(2)
# Also mutate the input, which should affect the aliased output.
inp_ref_clone.view(-1).mul_(3)
inp_clone.view(-1).mul_(3)
# Do backward
(inp_ref + out_ref[-1]).sum().backward()
(inp + out_test[-1]).sum().backward()
self.assertEqual(inp_ref.grad, inp.grad)
def test_output_aliases_intermediate_multi_output_view(self):
# All aliased outs are from multi-output views, so AOTAutograd will hide the aliasing from autograd.
def f1(a):
out = torch.mul(a, 3)
return list(out.unbind(0))
inp = torch.ones(3, 3, requires_grad=True)
inp_ref = torch.ones(3, 3, requires_grad=True)
f1_compiled = aot_function(f1, nop)
out_ref = f1(inp_ref)
out_test = f1_compiled(inp)
# Assert that we get CompiledFunctionBackward in the backward graph,
# and not AsStridedBackward. No view-regeneration necessary for this mult-output view case.
# See Note: [AOTAutograd: differentiable outputs that alias each other from a multi-output view call]
self.assertTrue(
all("CompiledFunctionBackward" in str(o.grad_fn) for o in out_test)
)
sum(out_ref).sum().backward()
sum(out_test).sum().backward()
self.assertEqual(inp_ref.grad, inp.grad)
# All aliased outs but one are from multi-output views, so AOTAutograd will hide the aliasing from autograd.
def f2(a):
out = torch.mul(a, 3)
return *list(out.unbind(0)), out
inp = torch.ones(3, 3, requires_grad=True)
inp_ref = torch.ones(3, 3, requires_grad=True)
f2_compiled = aot_function(f2, nop)
out_ref = f2(inp_ref)
out_test = f2_compiled(inp)
# Assert that we get CompiledFunctionBackward in the backward graph,
# and not AsStridedBackward. No view-regeneration necessary for this mult-output view case.
# See Note: [AOTAutograd: differentiable outputs that alias each other from a multi-output view call]
self.assertTrue(
all("CompiledFunctionBackward" in str(o.grad_fn) for o in out_test)
)
# The last output is not from a multi-output view, so autograd will let us mutate it.
out_ref[-1].mul_(2)
out_test[-1].mul_(2)
out_ref[-1].sum().backward()
out_test[-1].sum().backward()
self.assertEqual(inp_ref.grad, inp.grad)
# All aliased outs but one are from multi-output views, so AOTAutograd will hide the aliasing from autograd.
def f3(a):
out = torch.mul(a, 3)
return *list(out.unbind(0)), out.view(out.shape)
inp = torch.ones(3, 3, requires_grad=True)
inp_ref = torch.ones(3, 3, requires_grad=True)
f3_compiled = aot_function(f3, nop)
out_ref = f3(inp_ref)
out_test = f3_compiled(inp)
# Assert that we get CompiledFunctionBackward in the backward graph,
# and not AsStridedBackward. No view-regeneration necessary for this mult-output view case.
# See Note: [AOTAutograd: differentiable outputs that alias each other from a multi-output view call]
self.assertTrue(
all("CompiledFunctionBackward" in str(o.grad_fn) for o in out_test)
)
# The last output is not from a multi-output view, so autograd will let us mutate it.
out_ref[-1].mul_(2)
out_test[-1].mul_(2)
out_ref[-1].sum().backward()
out_test[-1].sum().backward()
self.assertEqual(inp_ref.grad, inp.grad)
# There are 5 outputs that all alias each other.
# 3 of them come from multi-output views, but the other 3 are "ordinary" aliases.
# Therefore, AOTAutograd will not attempt the multi-output-view optimization,
# and apply the intermediate_base logic to all aliases.
# (In theory we could probably get AOTAutograd to only apply the intermediate base
# logic to the last 2 outputs and not the first 3. We should probably
# just do the graph partitioning defined in this doc instead though).
# https://docs.google.com/document/d/1DlfFq8TKbuAn2zyJxLfoW-X1qkkm5PLdHFtySo03QAk/edit
def f4(a):
out = torch.mul(a, 3)
# also return the graph intermediate directly,
# which will force AOTAutograd to do the "intermediate base" logic.
# (Why? The user can mutate "out", which should change the autograd metadata
# of the other aliased outputs)
return *list(out.unbind(0)), out, out.view(out.shape)
inp = torch.ones(3, 3, requires_grad=True)
inp_ref = torch.ones(3, 3, requires_grad=True)
f4_compiled = aot_function(f4, nop)
out_ref = f4(inp_ref)
out_test = f4_compiled(inp)
# Mutate the last output of f4 (autograd will allow this, since it is not a multi-output view,
# as long as *only* the non-multi-output views participate in the backward)
# Note: We could probably try to hide **only** the multi-output views from autograd here
# and only do the intermediate base logic for the last two aliases.
# Longer term solution of graph partitioning is probably cleaner though (see the note).
out_ref[-1].mul_(2)
out_test[-1].mul_(2)
out_ref_sum = out_ref[-1] + out_ref[-2]
out_test_sum = out_test[-1] + out_test[-2]
out_ref_sum.sum().backward()
out_test_sum.sum().backward()
self.assertEqual(inp_ref.grad, inp.grad)
def test_output_aliases_intermediate_mutation_linear(self):
def f(x):
return (x + 1).view(-1)
inp = [torch.ones(3, 3, requires_grad=True)]
# use inductor's decomps (which will e.g. turn _unsafe_view() into view())
from torch._inductor.decomposition import decompositions
f_compiled = aot_function(f, nop, decompositions=decompositions)
out_ref = f(*inp)
out_test = f_compiled(*inp)
out_ref.mul_(2)
out_test.mul_(2)
self.assertEqual(out_ref, out_test)
def test_output_aliases_intermediate_no_grad(self):
def f(a, b):
out = torch.mul(a, 3)
# First output is an alias of an intermediate that doesn't require grad
return out.view(-1), b.add(1)
inp = [torch.ones(3, 3), torch.ones(3, 3, requires_grad=False)]
self.verify_aot_autograd(f, inp, test_mutation=True)
inp = [torch.ones(3, 3), torch.ones(3, 3, requires_grad=True)]
fw_graph = self.verify_aot_autograd(f, inp, test_mutation=True)
# important bit: we don't bother generating an intermediate base as an output in the graph,
# because the intermediate base itself didn't require gradients.
# (the only problematic case is when both the base and the aliasesed output require gradients).
self.assertExpectedInline(
fw_graph.code.strip(),
"""\
def forward(self, primals_1, primals_2):
mul = torch.ops.aten.mul.Tensor(primals_1, 3); primals_1 = None
view = torch.ops.aten.view.default(mul, [-1]); mul = None
add = torch.ops.aten.add.Tensor(primals_2, 1); primals_2 = None
return (view, add)""",
)
def test_output_aliases_intermediate_returned_multiple_times(self):
def f(a):
out = torch.mul(a, 3)
out_view = out.view(-1)
return out, out_view, out
inp = [torch.ones(3, 3, requires_grad=False)]
self.verify_aot_autograd(f, inp, test_mutation=True)
inp = [torch.ones(3, 3, requires_grad=True)]
fw_graph = self.verify_aot_autograd(f, inp, test_mutation=True)
def test_output_aliases_intermediate_multiple(self):
def f(a):
out = torch.mul(a, 3)
# AOTAutograd should manually generate these two output views in the epilogue.
return out.view(-1), out.view(-1)
inp = [torch.ones(3, 3, requires_grad=False)]
self.verify_aot_autograd(f, inp, test_mutation=True)
inp = [torch.ones(3, 3, requires_grad=True)]
fw_graph = self.verify_aot_autograd(f, inp, test_mutation=True)
self.assertExpectedInline(
fw_graph.code.strip(),
"""\
def forward(self, primals_1):
mul = torch.ops.aten.mul.Tensor(primals_1, 3); primals_1 = None
view = torch.ops.aten.view.default(mul, [-1])
view_1 = torch.ops.aten.view.default(mul, [-1])
return (view, view_1, mul)""",
)
def test_output_aliases_intermediate_and_returned(self):
def f(a):
out = torch.mul(a, 3)
# AOTAutograd should manually generate the first output (a view of an intermediate)
# but not the second (which is itself the intermediate for the first)
return out.view(-1), out
inp = [torch.ones(3, 3, requires_grad=False)]
self.verify_aot_autograd(f, inp, test_mutation=True)
inp = [torch.ones(3, 3, requires_grad=True)]
fw_graph = self.verify_aot_autograd(f, inp, test_mutation=True)
self.assertExpectedInline(
fw_graph.code.strip(),
"""\
def forward(self, primals_1):
mul = torch.ops.aten.mul.Tensor(primals_1, 3); primals_1 = None
view = torch.ops.aten.view.default(mul, [-1])
return (view, mul)""",
)
def test_output_aliases_intermediate_and_returned_flipped(self):
def f(a):
out = torch.mul(a, 3)
# AOTAutograd should manually generate the first output (a view of an intermediate)
# but not the second (which is itself the intermediate for the first)
return out, out.view(-1)
inp = [torch.ones(3, 3, requires_grad=False)]
self.verify_aot_autograd(f, inp, test_mutation=True)
inp = [torch.ones(3, 3, requires_grad=True)]
fw_graph = self.verify_aot_autograd(f, inp, test_mutation=True)
self.assertExpectedInline(
fw_graph.code.strip(),
"""\
def forward(self, primals_1):
mul = torch.ops.aten.mul.Tensor(primals_1, 3); primals_1 = None
view = torch.ops.aten.view.default(mul, [-1])
return (mul, view)""",
)
def test_output_aliases_intermediate_and_returned_different_grad(self):
def f(a):
out = torch.mul(a, 3)
# AOTAutograd should manually generate the first output (a view of an intermediate)
# but not the second (which is itself the intermediate for the first)
return out.view(-1), out, out[0].detach()
inp = [torch.ones(3, 3, requires_grad=False)]
self.verify_aot_autograd(f, inp, test_mutation=True)
inp = [torch.ones(3, 3, requires_grad=True)]
fw_graph = self.verify_aot_autograd(f, inp, test_mutation=True)
self.assertExpectedInline(
fw_graph.code.strip(),
"""\
def forward(self, primals_1):
mul = torch.ops.aten.mul.Tensor(primals_1, 3); primals_1 = None
view = torch.ops.aten.view.default(mul, [-1])
select = torch.ops.aten.select.int(mul, 0, 0)
detach = torch.ops.aten.detach.default(select); select = None
detach_1 = torch.ops.aten.detach.default(detach); detach = None
detach_2 = torch.ops.aten.detach.default(detach_1); detach_1 = None
return (view, mul, detach_2)""",
)
def test_output_aliases_intermediate_inplace_view(self):
def f(a):
out = torch.mul(a, 3)
out.t_()
return out
inp = [torch.ones(2, 4, requires_grad=True)]
# TODO: fix this test.
# See https://github.com/pytorch/pytorch/issues/90507
# self.verify_aot_autograd(f, inp, test_mutation=True)
def test_output_aliases_intermediate_inplace_view_with_detach(self):
def f(a):
out = torch.mul(a, 3)
out.t_()
out.detach_()
# Thanks to the detach_() AOT Autograd doesn't need to do anything.
# `out` will show up as having OutputType.non_alias,
# and ._is_view() == False
return out, a + 1
inp = [torch.ones(2, 4, requires_grad=False)]
self.verify_aot_autograd(f, inp, test_mutation=True)
inp = [torch.ones(2, 4, requires_grad=True)]
fw_graph = self.verify_aot_autograd(f, inp, test_mutation=True)
self.assertExpectedInline(
fw_graph.code.strip(),
"""\
def forward(self, primals_1):
mul = torch.ops.aten.mul.Tensor(primals_1, 3)
t = torch.ops.aten.t.default(mul); mul = None
add = torch.ops.aten.add.Tensor(primals_1, 1); primals_1 = None
return (t, add)""",
)
def test_output_aliases_intermediate_inplace_view_and_view(self):
def f(a):
out = torch.mul(a, 3)
out_view = out.unsqueeze(0)
out.t_()
out_view2 = out.unsqueeze(0)
return out_view, out, out_view2
inp = [torch.ones(2, 4, requires_grad=True)]
# TODO: fix this test.
# See <github issue link>
# self.verify_aot_autograd(f, inp, test_mutation=True)
def test_output_aliases_intermediate_multiple_mixed(self):
def f(a):
out1 = torch.mul(a, 3)
out2 = torch.mul(a, 4)
# AOTAutograd should manually generate these two output views in the epilogue.
return out1.view(-1), out2.transpose(1, 0), out1.transpose(1, 0)
inp = [torch.ones(3, 3, requires_grad=False)]
self.verify_aot_autograd(f, inp, test_mutation=True)
inp = [torch.ones(3, 3, requires_grad=True)]
fw_graph = self.verify_aot_autograd(f, inp, test_mutation=True)
self.assertExpectedInline(
fw_graph.code.strip(),
"""\
def forward(self, primals_1):
mul = torch.ops.aten.mul.Tensor(primals_1, 3)
mul_1 = torch.ops.aten.mul.Tensor(primals_1, 4); primals_1 = None
view = torch.ops.aten.view.default(mul, [-1])
transpose = torch.ops.aten.transpose.int(mul_1, 1, 0); mul_1 = None
transpose_1 = torch.ops.aten.transpose.int(mul, 1, 0)
return (view, transpose, transpose_1, mul)""",
)
def test_output_all_alias_types(self):
# There are 3 types of aliasing that require us to return metadata in the compiled fw:
# (1) outputs that are views of inputs
# (2) outputs that are views of intermediates
# (3) inputs that get metadata mutations
# test all 3 of them here
def f(a):
a.transpose_(1, 0)
tmp = a.mul(2)
return tmp.squeeze(), tmp.transpose(1, 0), a.unsqueeze(0)
def inp_callable(req_grad):
x = torch.ones(1, 2, 4, requires_grad=req_grad).clone()
return [(x,), (x,)]
self.verify_aot_autograd(
f, partial(inp_callable, req_grad=False), test_mutation=True
)
fw_graph = self.verify_aot_autograd(
f, partial(inp_callable, req_grad=True), test_mutation=True
)
# TODO: make this test run with dynamic shapes so it is more meaningful
# metadata output order: (a_updated_meta, out1_meta, out2_meta, out3_meta)
self.assertExpectedInline(
fw_graph.code.strip(),
"""\
def forward(self, primals_1):
view = torch.ops.aten.view.default(primals_1, [1, 2, 4]); primals_1 = None
transpose = torch.ops.aten.transpose.int(view, 1, 0); view = None
mul = torch.ops.aten.mul.Tensor(transpose, 2)
squeeze = torch.ops.aten.squeeze.default(mul)
transpose_1 = torch.ops.aten.transpose.int(mul, 1, 0)
unsqueeze = torch.ops.aten.unsqueeze.default(transpose, 0)
return (transpose, squeeze, transpose_1, unsqueeze, mul)""",
)
@parametrize("req_grad", [False, True])
def test_subclass_metadata_mutation(self, req_grad):
def f(a):
a.transpose_(1, 0)
tmp = a.mul(2)
return tmp.transpose(1, 0)
def inp_callable(req_grad):
x = torch.ones(1, 2, 4, requires_grad=req_grad).clone()
return [(x,), (x,)]
# See https://github.com/pytorch/pytorch/issues/114975
with self.assertRaisesRegex(
RuntimeError,
"Metadata mutations are currently not allowed on tensor subclasses",
):
self.verify_aot_autograd(
f,
partial(inp_callable, req_grad=req_grad),
test_mutation=True,
make_inputs_subclasses=True,
)
def test_input_data_and_metadata_mutation(self):
def f(a):
a.t_()
a[0].mul_(2)
return a.view(a.shape)
inp = [torch.ones(3, 3, requires_grad=False)]
self.verify_aot_autograd(f, inp, test_mutation=True)
inp = [torch.ones(3, 3, requires_grad=True)]
fw_graph = self.verify_aot_autograd(f, inp, test_mutation=True)
self.assertExpectedInline(
fw_graph.code.strip(),
"""\
def forward(self, primals_1):
clone = torch.ops.aten.clone.default(primals_1); primals_1 = None
t = torch.ops.aten.t.default(clone)
select = torch.ops.aten.select.int(t, 0, 0); t = None
mul = torch.ops.aten.mul.Tensor(select, 2); select = None
t_1 = torch.ops.aten.t.default(clone); clone = None
select_scatter = torch.ops.aten.select_scatter.default(t_1, mul, 0, 0); t_1 = mul = None
t_2 = torch.ops.aten.t.default(select_scatter); select_scatter = None
t_4 = torch.ops.aten.t.default(t_2)
t_6 = torch.ops.aten.t.default(t_2); t_2 = None
view_1 = torch.ops.aten.view.default(t_6, [3, 3]); t_6 = None
return (t_4, view_1)""",
)
def test_view_and_inplace_view(self):
def f(a, b):
a.t_()
return b.view(b.shape), a.view(a.shape)
def create_inp(req_grad):
return [
torch.ones(3, 3, requires_grad=req_grad),
torch.ones(3, 3, requires_grad=req_grad),
]
self.verify_aot_autograd(f, create_inp(False), test_mutation=True)
fw_graph = self.verify_aot_autograd(f, create_inp(True), test_mutation=True)
self.assertExpectedInline(
fw_graph.code.strip(),
"""\
def forward(self, arg0_1, arg1_1):
t = torch.ops.aten.t.default(arg0_1); arg0_1 = None
view = torch.ops.aten.view.default(arg1_1, [3, 3]); arg1_1 = None
view_1 = torch.ops.aten.view.default(t, [3, 3])
return (t, view, view_1)""",
)
def test_view_detach(self):
def f(a):
tmp = a.detach()
a.mul_(2)
return a, tmp
inp = [torch.ones(3, 3, requires_grad=True)]
self.verify_aot_autograd(f, inp, test_mutation=True)
inp = [torch.ones(3, 3, requires_grad=False)]
self.verify_aot_autograd(f, inp, test_mutation=True)
def test_input_inplace_requires_grad_true(self):
def f(a, b):
a.requires_grad_(True)
return a.mul(3), b.mul(4)
inp = [
# First inp doesnt require grad, but we switch it on
torch.ones(3, 3, requires_grad=False),
torch.ones(3, 3, requires_grad=True),
]
fw_graph = self.verify_aot_autograd(f, inp, test_mutation=True)
self.assertExpectedInline(
fw_graph.code.strip(),
"""\
def forward(self, primals_1, primals_2):
mul = torch.ops.aten.mul.Tensor(primals_1, 3); primals_1 = None
mul_1 = torch.ops.aten.mul.Tensor(primals_2, 4); primals_2 = None
return (mul, mul_1)""",
)
# This is a torture test:
# a and b get turned into a synthetic base in the compiled graph
# One gets a data mutation, the other gets a metadata mutation.
# We need to make sure that the metadata mutation gets propagated
# back to the original input.
@skipIfDynamoInput("Dynamo removes runtime error")
def test_input_data_and_metadata_mutation_aliases_other_input(self):
# a and b are aliased
def f(a, b):
a.mul_(2)
b.t_()
return a.mul(b)
def inp_callable(req_grad):
base = torch.ones(2, 2, requires_grad=req_grad)
# Note: in our test, the add() is important because we need the graph inputs to be non-leaves so we can mutate them.
x = base.add(1)
inp1 = x[0]
inp2 = x[0]
return [base], [inp1, inp2]
self.verify_aot_autograd(
f, partial(inp_callable, req_grad=False), test_mutation=True
)
self.verify_aot_autograd(
f, partial(inp_callable, req_grad=True), test_mutation=True
)
with self.assertRaisesRegex(
RuntimeError,
"Encountered aliased inputs that are mutated in the graph, but",
):
self.verify_aot_autograd(
f,
partial(inp_callable, req_grad=False),
test_mutation=True,
make_inputs_subclasses=True,
)
with self.assertRaisesRegex(
RuntimeError,
"Encountered aliased inputs that are mutated in the graph, but",
):
self.verify_aot_autograd(
f,
partial(inp_callable, req_grad=True),
test_mutation=True,
make_inputs_subclasses=True,
)
# https://github.com/pytorch/pytorch/issues/106456
@skipIfTorchDynamo()
def test_input_mutation_noncontiguous(self):
def f(a):
a.mul_(2)
return a + 1
def inp_callable(req_grad):
base = torch.ones(2, 2, requires_grad=req_grad)
x = base.add(1)
# create a non-contiguous view to pass as an input to the compiler
inp = x[:, 0]
return [base], [inp]
self.verify_aot_autograd(
f, partial(inp_callable, req_grad=False), test_mutation=True
)
self.verify_aot_autograd(
f, partial(inp_callable, req_grad=True), test_mutation=True
)
self.verify_aot_autograd(
f,
partial(inp_callable, req_grad=False),
test_mutation=True,
make_inputs_subclasses=True,
)
self.verify_aot_autograd(
f,
partial(inp_callable, req_grad=True),
test_mutation=True,
make_inputs_subclasses=True,
)
def test_backward_mutation_data(self):
class BwMutation(torch.autograd.Function):
@staticmethod
def forward(ctx, x):
ctx.save_for_backward(x)
return x.clone()
@staticmethod
def backward(ctx, grad_output):
(x,) = ctx.saved_tensors
# bw mutation
x.mul_(2)
return grad_output.clone()
def f(a, b):
out = BwMutation.apply(b)
return a * out
inp_no_grad = [
torch.ones(3, 3, requires_grad=True),
torch.ones(3, 3, requires_grad=False),
]
# Mutation on buffer that does not require grad during the backward is allowed
self.verify_aot_autograd(f, inp_no_grad, test_mutation=True)
inp_grad = [
torch.ones(3, 3, requires_grad=True),
torch.ones(3, 3, requires_grad=True),
]
self.verify_aot_autograd(f, inp_grad, test_mutation=True)
def test_backward_mutation_metadata(self):
class BwMutation(torch.autograd.Function):
@staticmethod
def forward(ctx, a, b):
ctx.save_for_backward(b)
return a.clone(), b.clone()
@staticmethod
def backward(ctx, grad_a, grad_b):
(b,) = ctx.saved_tensors
# bw metadata mutation
b.transpose_(1, 0)
return grad_a.clone(), grad_b.clone()
def f(a, b):
a_, b_ = BwMutation.apply(a, b)
out = a_ * b_
return out
inp_no_grad = [
torch.ones(3, 3, requires_grad=True),
torch.ones(3, 3, requires_grad=False),
]
with self.assertRaisesRegex(
AssertionError, "input that had its metadata mutated in the backward"
):
self.verify_aot_autograd(f, inp_no_grad, test_mutation=True)
def test_backward_mutation_on_grad_out(self):
class BwMutation(torch.autograd.Function):
@staticmethod
def forward(ctx, x):
return x.clone()
@staticmethod
def backward(ctx, grad_output):
grad_output.mul_(2)
return grad_output.clone()
def f(a, b):
tmp = a * b
out = BwMutation.apply(tmp)
return out
inp_grad = [
torch.ones(3, 3, requires_grad=True),
torch.ones(3, 3, requires_grad=True),
]
f_compiled = aot_function(f, nop)
with self.assertRaisesRegex(
AssertionError, "input to the backward that was mutated during the backward"
):
out = f_compiled(*inp_grad)
def test_backward_mutation_forward_inputs(self):
@torch.library.custom_op("_test::_clone", mutates_args={})
def f(x: torch.Tensor, x1: torch.Tensor) -> torch.Tensor:
return x.clone()
def f_fake(x, x1):
return torch.empty_like(x)
def backward(ctx, grad):
with torch.no_grad():
ctx.x1.zero_()
return grad * 2, None
def setup_context(ctx, inputs, output):
(x, x1) = inputs
ctx.x = x
ctx.x1 = x1
f.register_fake(f_fake)
f.register_autograd(backward, setup_context=setup_context)
def fn(x: torch.Tensor, x1: torch.Tensor, x2: torch.Tensor) -> torch.Tensor:
x2.mul_(5)
return torch.ops._test._clone(x, x1) + x2
inp_x, inp_x1, inp_x2 = (
torch.randn(3, requires_grad=True),
torch.randn(3, requires_grad=False),
torch.randn(3, requires_grad=False),
)
ref_x, ref_x1, ref_x2 = inp_x.clone(), inp_x1.clone(), inp_x2.clone()
ref_y = fn(ref_x, ref_x1, ref_x2)
compiled_f = aot_function(fn, nop, keep_inference_input_mutations=True)
x, x1, x2 = inp_x.clone(), inp_x1.clone(), inp_x2.clone()
y = compiled_f(x, x1, x2)
# Verify mutation in forward applied and mutation in backward is not in forward
self.assertEqual(ref_x, x)
self.assertEqual(ref_x1, x1)
self.assertEqual(ref_x2, x2)
self.assertEqual(ref_y, y)
ref_y.sum().backward()
y.sum().backward()
# Verify mutations in backward applied
self.assertEqual(ref_x, x)
self.assertEqual(ref_x1, x1)
self.assertEqual(ref_x2, x2)
self.assertEqual(ref_y, y)
self.assertEqual(ref_x.grad, x.grad)
self.assertEqual(ref_x1.grad, x1.grad)
self.assertEqual(ref_x2.grad, x2.grad)
def test_backward_mutation_forward_inputs_create_graph(self):
@torch.library.custom_op("_test::_clone_create_graph", mutates_args={})
def f(x: torch.Tensor, x1: torch.Tensor) -> torch.Tensor:
return x.clone()
def f_fake(x, x1):
return torch.empty_like(x)
def backward(ctx, grad):
with torch.no_grad():
ctx.x1.zero_()
return grad * 2, None
def setup_context(ctx, inputs, output):
(x, x1) = inputs
ctx.x = x
ctx.x1 = x1
f.register_fake(f_fake)
f.register_autograd(backward, setup_context=setup_context)
def fn(x: torch.Tensor, x1: torch.Tensor) -> torch.Tensor:
return torch.ops._test._clone_create_graph(x, x1)
inp_x, inp_x1 = torch.randn(3, requires_grad=True), torch.randn(
3, requires_grad=True
)
ref_x, ref_x1 = inp_x.clone(), inp_x1.clone()
ref_y = f(ref_x, ref_x1)
ref_y.sum().backward()
x, x1 = inp_x.clone(), inp_x1.clone()
compiled_f = aot_function(fn, nop)
y = compiled_f(x, x1)
loss = y.sum()
with self.assertRaisesRegex(
RuntimeError,
"aot_autograd does not support input mutations with requires_grad in backward for create_graph=True",
):
torch.autograd.grad(loss, inp_x, create_graph=True)
# Not checking equality of ref and x as Exception is expected
# Partially addresses https://github.com/pytorch/pytorch/issues/106457
@skipIfTorchDynamo()
def test_input_mutation_false_aliasing(self):
def f(a, b):
a.mul_(3)
b.mul_(2)
return a.clone().view(-1) + b.clone().view(-1)
# No overlap, contiguous
def inp_callable1(req_grad):
base = torch.ones(4, 4, requires_grad=req_grad)
x = base.add(1)
# create two views that share storage, but are actually non-overlapping
a = x[0:2]
b = x[2:4]
return [base], [a, b]
fw_graph = self.verify_aot_autograd(
f, partial(inp_callable1, req_grad=False), test_mutation=True
)
self.verify_aot_autograd(
f, partial(inp_callable1, req_grad=True), test_mutation=True
)
self.verify_aot_autograd(
f,
partial(inp_callable1, req_grad=False),
test_mutation=True,
make_inputs_subclasses=True,
)
self.verify_aot_autograd(
f,
partial(inp_callable1, req_grad=True),
test_mutation=True,
make_inputs_subclasses=True,
)
# Important characteristic: the graph takes in 2 inputs!
# That shows that we didn't try to run our complicated synthetic base logic,
# because we successfully detected false aliasing across the two inputs.
self.assertExpectedInline(
fw_graph.code.strip(),
"""\
def forward(self, arg0_1, arg1_1):
mul = torch.ops.aten.mul.Tensor(arg0_1, 3); arg0_1 = None
mul_1 = torch.ops.aten.mul.Tensor(arg1_1, 2); arg1_1 = None
clone = torch.ops.aten.clone.default(mul)
view = torch.ops.aten.view.default(clone, [-1]); clone = None
clone_1 = torch.ops.aten.clone.default(mul_1)
view_1 = torch.ops.aten.view.default(clone_1, [-1]); clone_1 = None
add = torch.ops.aten.add.Tensor(view, view_1); view = view_1 = None
return (mul, mul_1, add)""",
)
# No overlap, non-contiguous: first tensor ends before second tensor start
def inp_callable2(req_grad):
base = torch.ones(256, requires_grad=req_grad)
x = base.add(1)
a = x.as_strided((4, 4), (8, 1), storage_offset=0)
b = x.as_strided((4, 4), (8, 1), storage_offset=28)
return [base], [a, b]
# No overlap, non-contiguous: tensors are perfectly interleaved
def inp_callable3(req_grad):
base = torch.ones(4, 4, requires_grad=req_grad)
x = base.add(1)
a = x[:, 0:2]
b = x[:, 2:4]
return [base], [a, b]
# No overlap, non-contiguous
def inp_callable4(req_grad):
base = torch.ones(256, requires_grad=req_grad)
x = base.add(1)
a = x.as_strided((4, 4), (9, 1), storage_offset=0)
b = x.as_strided((4, 4), (9, 1), storage_offset=22)
return [base], [a, b]
# No overlap, non-contiguous
def inp_callable5(req_grad):
base = torch.ones(256, requires_grad=req_grad)
x = base.add(1)
a = x.as_strided((4, 4), (9, 1), storage_offset=0)
b = x.as_strided((4, 4), (9, 1), storage_offset=23)
return [base], [a, b]
# No overlap, non-contiguous
def inp_callable6(req_grad):
base = torch.ones(256, requires_grad=req_grad)
x = base.add(1)
# a's last element is at offset 195 (24 total elements)
a = x.as_strided((2, 4, 3), (110, 24, 4), storage_offset=5)
# b's first element is at offset 196: no overlap
b = x[196 : 196 + a.numel()]
return [base], [a, b]
# overlap! non-contiguous
def inp_callable_overlap1(req_grad):
base = torch.ones(256, requires_grad=req_grad)
x = base.add(1)
a = x.as_strided((4, 4), (9, 1), storage_offset=0)
b = x.as_strided((4, 4), (9, 1), storage_offset=24)
return [base], [a, b]
# overlap! non-contiguous
def inp_callable_overlap2(req_grad):
base = torch.ones(256, requires_grad=req_grad)
x = base.add(1)
a = x.as_strided((4, 4), (9, 1), storage_offset=0)
b = x.as_strided((4, 4), (9, 1), storage_offset=25)
return [base], [a, b]
# overlap! non-contiguous
def inp_callable_overlap3(req_grad):
base = torch.ones(256, requires_grad=req_grad)
x = base.add(1)
# a's last element is at offset 195 (24 total elements)
a = x.as_strided((2, 4, 3), (110, 24, 4), storage_offset=5)
# b's first element is at offset 195: overlap!
b = x[195 : 195 + a.numel()]
return [base], [a, b]
fw_graph2 = self.verify_aot_autograd(
f, partial(inp_callable2, req_grad=False), test_mutation=True
)
fw_graph3 = self.verify_aot_autograd(
f, partial(inp_callable3, req_grad=False), test_mutation=True
)
fw_graph4 = self.verify_aot_autograd(
f, partial(inp_callable4, req_grad=False), test_mutation=True
)
fw_graph5 = self.verify_aot_autograd(
f, partial(inp_callable5, req_grad=False), test_mutation=True
)
fw_graph6 = self.verify_aot_autograd(
f, partial(inp_callable6, req_grad=False), test_mutation=True
)
fw_graph_overlap1 = self.verify_aot_autograd(
f, partial(inp_callable_overlap2, req_grad=False), test_mutation=True
)
fw_graph_overlap2 = self.verify_aot_autograd(
f, partial(inp_callable_overlap1, req_grad=False), test_mutation=True
)
# All non-overlap graphs should be the same since we detected false aliasing
self.assertEqual(str(fw_graph.code), str(fw_graph2.code))
self.assertEqual(str(fw_graph.code), str(fw_graph3.code))
self.assertEqual(str(fw_graph.code), str(fw_graph4.code))
self.assertEqual(str(fw_graph.code), str(fw_graph5.code))
self.assertEqual(str(fw_graph.code), str(fw_graph6.code))
# All overlap graphs should be the same since we detected real aliasing
self.assertNotEqual(str(fw_graph.code), str(fw_graph_overlap1.code))
self.assertNotEqual(str(fw_graph.code), str(fw_graph_overlap2.code))
self.assertTrue("as_strided_scatter" in str(fw_graph_overlap1.code))
self.assertTrue("as_strided_scatter" in str(fw_graph_overlap2.code))
@unittest.skipIf(not torch.cuda.is_available(), "CUDA is unavailable")
def test_mem_leak_from_save_for_bw(self):
# See a full diagnosis at this issue: https://github.com/pytorch/pytorch/issues/94990
# Note [Detaching saved tensors in AOTAutograd]
# This program creates a ref-cycle. Long term, we should fix this ref cycle
# (since it can arise, naturally albeit rarely, from uses of autograd.Function).
# But AOTAutograd makes it more likely to show up from tracing user programs,
# so we deal with it by manually detaching the tensors that we save for backward.
# This is completely wrong and would give wrong results if we were to do double backward.
# Fortunately today, double backward is explicitly banned in AOTAutograd.
def f(a, b):
add = a + a
split = torch.functional.split(add, [4, 4], dim=1)
getitem_2 = split[1]
unsqueeze = getitem_2.unsqueeze(-1)
mul = unsqueeze * b
return (getitem_2, mul)
f_compiled = aot_function(f, nop)
inps = [
torch.ones(8, 8, device="cuda", requires_grad=True),
torch.ones(1, 4, 1, device="cuda", requires_grad=True),
]
mem_before = torch.cuda.memory_allocated()
f_compiled(*inps)
mem_after = torch.cuda.memory_allocated()
self.assertTrue(mem_after == mem_before)
def test_output_aliases_multiple_inputs_get_correct_one(self):
# a and b are aliased, but have different shapes
# The first output should view off the first input, the 2nd output should view off the 2nd input
def f(a, b):
return a.view(a.shape), b.view(b.shape)
def inp_callable(req_grad):
base = torch.ones(2, 2, requires_grad=req_grad)
# Note: in our test, the add() is important because we need the graph inputs to be non-leaves so we can mutate them.
x = base.mul(2)
inp1 = x.view(-1)
inp2 = x[0]
return [base], [inp1, inp2]
self.verify_aot_autograd(
f, partial(inp_callable, req_grad=False), test_mutation=True
)
self.verify_aot_autograd(
f, partial(inp_callable, req_grad=True), test_mutation=True
)
self.verify_aot_autograd(
f,
partial(inp_callable, req_grad=False),
test_mutation=True,
make_inputs_subclasses=True,
)
self.verify_aot_autograd(
f,
partial(inp_callable, req_grad=True),
test_mutation=True,
make_inputs_subclasses=True,
)
def test_input_mutation_aliases_other_input(self):
def f(a, b):
a.add_(1)
return a + b
def inp_callable(req_grad):
base = torch.ones(4, 2, requires_grad=req_grad)
# Note: in our test, the add() is important because we need the graph inputs to be non-leaves so we can mutate them.
x = base.add(1)
inp1 = x[0]
inp2 = x[0]
return [base], [inp1, inp2]
self.verify_aot_autograd(
f, partial(inp_callable, req_grad=False), test_mutation=True
)
fw_graph = self.verify_aot_autograd(
f, partial(inp_callable, req_grad=True), test_mutation=True
)
# Important parts of the graph:
# - the compiled graph takes in a base, and we generate a and b (the views) off of the base
# - clone() is still in the graph, because we need to call grad() on the original (non-mutated) inputs
# - We re-generate the views *after* the clone, to preserve view relationships.
self.assertExpectedInline(
fw_graph.code.strip(),
"""\
def forward(self, primals_1):
clone = torch.ops.aten.clone.default(primals_1); primals_1 = None
as_strided = torch.ops.aten.as_strided.default(clone, [2], [1], 0)
add = torch.ops.aten.add.Tensor(as_strided, 1); as_strided = None
as_strided_scatter = torch.ops.aten.as_strided_scatter.default(clone, add, [2], [1], 0); clone = add = None
as_strided_2 = torch.ops.aten.as_strided.default(as_strided_scatter, [2], [1], 0)
as_strided_5 = torch.ops.aten.as_strided.default(as_strided_scatter, [2], [1], 0)
add_1 = torch.ops.aten.add.Tensor(as_strided_2, as_strided_5); as_strided_2 = as_strided_5 = None
return (as_strided_scatter, add_1)""",
) # noqa: B950
def test_input_mutation_aliases_other_input2(self):
def f(a, b):
a.add_(1)
return a + b
def inp_callable(req_grad):
base = torch.ones(2, 2, requires_grad=req_grad)
x = base.add(1)
inp1 = x[0]
# Here, one of the aliased inputs is the base itself
inp2 = x
return [base], [inp1, inp2]
self.verify_aot_autograd(
f, partial(inp_callable, req_grad=False), test_mutation=True
)
fw_graph = self.verify_aot_autograd(
f, partial(inp_callable, req_grad=True), test_mutation=True
)
self.assertExpectedInline(
fw_graph.code.strip(),
"""\
def forward(self, primals_1):
clone = torch.ops.aten.clone.default(primals_1); primals_1 = None
as_strided = torch.ops.aten.as_strided.default(clone, [2], [1], 0)
add = torch.ops.aten.add.Tensor(as_strided, 1); as_strided = None
as_strided_scatter = torch.ops.aten.as_strided_scatter.default(clone, add, [2], [1], 0); clone = add = None
as_strided_2 = torch.ops.aten.as_strided.default(as_strided_scatter, [2], [1], 0)
as_strided_5 = torch.ops.aten.as_strided.default(as_strided_scatter, [2, 2], [2, 1], 0)
add_1 = torch.ops.aten.add.Tensor(as_strided_2, as_strided_5); as_strided_2 = as_strided_5 = None
return (as_strided_scatter, add_1)""",
) # noqa: B950
def test_input_mutation_aliases_and_output_alias(self):
def f(a, b):
# Here, we need to take care:that because and b are aliased
# since a and b are aliased, we generate a view off of "updated b"
a.add_(1)
return b.view(b.shape)
def inp_callable(req_grad):
base = torch.ones(2, 2, requires_grad=req_grad)
x = base.add(1)
return [base], [x.view(-1), x.view(-1)]
self.verify_aot_autograd(
f, partial(inp_callable, req_grad=False), test_mutation=True
)
fw_graph = self.verify_aot_autograd(
f, partial(inp_callable, req_grad=True), test_mutation=True
)
self.assertExpectedInline(
fw_graph.code.strip(),
"""\
def forward(self, primals_1):
clone = torch.ops.aten.clone.default(primals_1); primals_1 = None
as_strided = torch.ops.aten.as_strided.default(clone, [4], [1], 0)
add = torch.ops.aten.add.Tensor(as_strided, 1); as_strided = None
as_strided_scatter = torch.ops.aten.as_strided_scatter.default(clone, add, [4], [1], 0); clone = add = None
as_strided_8 = torch.ops.aten.as_strided.default(as_strided_scatter, [4], [1], 0)
view_1 = torch.ops.aten.view.default(as_strided_8, [4]); as_strided_8 = None
return (as_strided_scatter, view_1)""",
) # noqa: B950
def test_input_aliased_with_mutation_output_alias(self):
def f(a, b, c):
# a and c alias
c.mul_(2)
# The main thing we're testing here is that
# (1) We need to reconstruct c.view(-1) from the 3rd input to the forward
# (2) But we need to be careful to do this *before* converting aliased inputs into synthetic bases.
# The original fw takes in 3 args, but the compiled fw takes in only 2 args.
return b.add(1), c.view(-1)
def inp_callable(req_grad):
base1 = torch.ones(2, 2, requires_grad=req_grad)
base2 = torch.ones(2, 2, requires_grad=req_grad)
x = base1.add(1)
y = base2.add(1)
return [base1, base2], [x.view(-1), y, x.view(-1)]
self.verify_aot_autograd(
f, partial(inp_callable, req_grad=False), test_mutation=True
)
fw_graph = self.verify_aot_autograd(
f, partial(inp_callable, req_grad=True), test_mutation=True
)
self.assertExpectedInline(
fw_graph.code.strip(),
"""\
def forward(self, primals_1, primals_2):
clone = torch.ops.aten.clone.default(primals_1); primals_1 = None
as_strided_1 = torch.ops.aten.as_strided.default(clone, [4], [1], 0)
mul = torch.ops.aten.mul.Tensor(as_strided_1, 2); as_strided_1 = None
as_strided_scatter = torch.ops.aten.as_strided_scatter.default(clone, mul, [4], [1], 0); clone = mul = None
add = torch.ops.aten.add.Tensor(primals_2, 1); primals_2 = None
as_strided_7 = torch.ops.aten.as_strided.default(as_strided_scatter, [4], [1], 0)
view_1 = torch.ops.aten.view.default(as_strided_7, [-1]); as_strided_7 = None
return (as_strided_scatter, add, view_1)""",
) # noqa: B950
def test_input_metadata_mutation_aliases(self):
def f(a, b):
# a and b alias, and we do a metadata mutation on a
# Since we're not mutating data, then b isn't affected at all.
# We expect aot autograd to not bother with constructing a synthetic base.
a.t_()
return a + b
def inp_callable(req_grad):
base = torch.ones(2, 2, requires_grad=req_grad)
x = base.add(1)
return [base], [x.view(-1), x.view(-1)]
self.verify_aot_autograd(
f, partial(inp_callable, req_grad=False), test_mutation=True
)
fw_graph = self.verify_aot_autograd(
f, partial(inp_callable, req_grad=True), test_mutation=True
)
# Expectation: fwd() takes in 2 args, and we don't construct a synthetic base.
self.assertExpectedInline(
fw_graph.code.strip(),
"""\
def forward(self, primals_1, primals_2):
t = torch.ops.aten.t.default(primals_1); primals_1 = None
add = torch.ops.aten.add.Tensor(t, primals_2); t = primals_2 = None
return (add,)""",
)
def test_input_mutation_aliases_and_none_require_gradients(self):
def f(a, b, c):
# a and b alias, but neither require gradients (so they don't have a _base)
# aot autograd should construct the synthetic base from `torch.Tensor(a.storage())`
a.mul_(2)
return b + 1, c + 1
def inp_callable(req_grad):
base = torch.ones(2, 2)
c_arg = torch.ones(2, 2, requires_grad=req_grad)
x = base.add(1)
return [base, c_arg], [x.view(-1), x.view(-1), c_arg]
self.verify_aot_autograd(
f, partial(inp_callable, req_grad=False), test_mutation=True
)
with self.assertRaisesRegex(
RuntimeError, "is a tensor subclass. This is not supported today"
):
self.verify_aot_autograd(
f,
partial(inp_callable, req_grad=False),
test_mutation=True,
make_inputs_subclasses=True,
)
fw_graph = self.verify_aot_autograd(
f, partial(inp_callable, req_grad=True), test_mutation=True
)
self.assertExpectedInline(
fw_graph.code.strip(),
"""\
def forward(self, primals_1, primals_2):
as_strided = torch.ops.aten.as_strided.default(primals_1, [4], [1], 0)
mul = torch.ops.aten.mul.Tensor(as_strided, 2); as_strided = None
as_strided_scatter = torch.ops.aten.as_strided_scatter.default(primals_1, mul, [4], [1], 0); primals_1 = mul = None
as_strided_3 = torch.ops.aten.as_strided.default(as_strided_scatter, [4], [1], 0)
add = torch.ops.aten.add.Tensor(as_strided_3, 1); as_strided_3 = None
add_1 = torch.ops.aten.add.Tensor(primals_2, 1); primals_2 = None
return (as_strided_scatter, add, add_1)""",
) # noqa: B950
@skipIfDynamoInput("Fails with dynamo")
def test_input_mutation_aliases_bases_out_of_order(self):
# This tests our calling convention: if b and d are aliased, then the outer calling convention
# that we send to the compiled forward becomes:
# (b_d_base, a, c)
# Importantly, even though a and c alias in our test, neither inputs are mutated,
# So we don't need to do the base construction / deconstruction
def f(a, b, c, d):
b.add_(1)
d.unsqueeze_(0)
return a + c + d, b.view(-1)
def inp_callable(req_grad):
base1 = torch.ones(2, 2, requires_grad=req_grad)
base2 = torch.ones(2, 2, requires_grad=req_grad)
x1 = base1.add(1)
x2 = base2.add(1)
# a and c alias, b and d alias
return [base1, base2], [x1.view(-1), x2.view(-1), x1.view(-1), x2.view(-1)]
self.verify_aot_autograd(
f, partial(inp_callable, req_grad=False), test_mutation=True
)
with self.assertRaisesRegex(
RuntimeError,
"Metadata mutations are currently not allowed on tensor subclasses",
):
self.verify_aot_autograd(
f,
partial(inp_callable, req_grad=False),
test_mutation=True,
make_inputs_subclasses=True,
)
fw_graph = self.verify_aot_autograd(
f, partial(inp_callable, req_grad=True), test_mutation=True
)
# 3 graph inputs: (b_d_base, a, c)
# 2 returns: (b_updated, a+c+d)
# (there are 2 original fw outs, but one is a view of b so it's not part of the graph)
# (there are also 2 input mutations, but one is a metadata-only mutation so the compiled forward doesn't return it)
self.assertExpectedInline(
fw_graph.code.strip(),
"""\
def forward(self, primals_1, primals_2, primals_3):
clone = torch.ops.aten.clone.default(primals_1); primals_1 = None
as_strided = torch.ops.aten.as_strided.default(clone, [4], [1], 0)
add = torch.ops.aten.add.Tensor(as_strided, 1); as_strided = None
as_strided_scatter = torch.ops.aten.as_strided_scatter.default(clone, add, [4], [1], 0); clone = add = None
add_1 = torch.ops.aten.add.Tensor(primals_2, primals_3); primals_2 = primals_3 = None
as_strided_5 = torch.ops.aten.as_strided.default(as_strided_scatter, [4], [1], 0)
unsqueeze_1 = torch.ops.aten.unsqueeze.default(as_strided_5, 0); as_strided_5 = None
add_2 = torch.ops.aten.add.Tensor(add_1, unsqueeze_1); add_1 = None
as_strided_14 = torch.ops.aten.as_strided.default(as_strided_scatter, [4], [1], 0)
view_2 = torch.ops.aten.view.default(as_strided_14, [-1]); as_strided_14 = None
return (as_strided_scatter, add_2, view_2, unsqueeze_1)""",
) # noqa: B950
@unittest.skipIf(not torch.cuda.is_available(), "CUDA is unavailable")
def test_synthetic_base_base_attribute_is_none(self):
def f(a, b):
a.add_(1)
return a + b
def inp_callable():
base = torch.ones(4, 4, device="cuda")
# detach() so that none of the inputs have a ._base attribute.
a = base[0].detach()
b = base[1].detach()
base2 = torch.ones(2, 2, requires_grad=True)
return [base], [a, b]
self.verify_aot_autograd(f, inp_callable, test_mutation=True)
def test_input_mutation_alias_everything(self):
# Mondo test that tests a combination of:
# input is mutated, that aliases another input (so we make a synthetic base)
# an output is an alias of another output
# an output is an alias of an intermediate
# a and c are aliased
def f(a, b, c):
c.mul_(2) # mutates c
b.t_() # metadata mutate b
tmp = a + c
out1 = tmp.view(-1)
out2 = b.t()
out3 = out1.unsqueeze(0)
# out1 and out3 are aliases of an intermediate, and alias each other!
# out2 aliases an input, so we don't return it
return out1, out2, out3
def inp_callable(req_grad):
base1 = torch.ones(2, 2, requires_grad=req_grad)
base2 = torch.ones(2, 2, requires_grad=req_grad)
# Note: in our test, the add() is important because we need the graph inputs to be non-leaves so we can mutate them.
base1_ = base1.add(1)
base2_ = base2.add(1)
a = base1_.view(-1)
b = base2_
c = base1_.view(-1)
return [base1, base2], [a, b, c]
self.verify_aot_autograd(
f, partial(inp_callable, req_grad=False), test_mutation=True
)
fw_graph = self.verify_aot_autograd(
f, partial(inp_callable, req_grad=True), test_mutation=True
)
# Expected:
# - 2 inputs in the forward: synthetic_base_a_c, b
# - 1 output in the forward: "tmp"
# out2 is an alias of an input, and will be generated off of b outside of the compiled fn
# out1 and out3 are aliases of tmp, that we generate outside of the compiled function
self.assertExpectedInline(
fw_graph.code.strip(),
"""\
def forward(self, primals_1, primals_2):
clone = torch.ops.aten.clone.default(primals_1); primals_1 = None
view = torch.ops.aten.view.default(primals_2, [2, 2]); primals_2 = None
as_strided_1 = torch.ops.aten.as_strided.default(clone, [4], [1], 0)
mul = torch.ops.aten.mul.Tensor(as_strided_1, 2); as_strided_1 = None
as_strided_scatter = torch.ops.aten.as_strided_scatter.default(clone, mul, [4], [1], 0); clone = mul = None
as_strided_2 = torch.ops.aten.as_strided.default(as_strided_scatter, [4], [1], 0)
t = torch.ops.aten.t.default(view); view = None
as_strided_5 = torch.ops.aten.as_strided.default(as_strided_scatter, [4], [1], 0)
add = torch.ops.aten.add.Tensor(as_strided_5, as_strided_2); as_strided_5 = as_strided_2 = None
view_1 = torch.ops.aten.view.default(add, [-1])
t_1 = torch.ops.aten.t.default(t)
unsqueeze = torch.ops.aten.unsqueeze.default(view_1, 0)
return (as_strided_scatter, t, view_1, t_1, unsqueeze, add)""",
) # noqa: B950
def test_dynamic_shape_output_not_in_bw_graph(self):
def f(x):
return [x + 1, x.shape[0]]
inp = torch.ones(5, requires_grad=True)
bw_graph_cell = [None]
compiled_f = aot_function(
f,
fw_compiler=nop,
bw_compiler=partial(extract_graph, graph_cell=bw_graph_cell),
decompositions={},
keep_inference_input_mutations=False,
dynamic=True,
)
out = compiled_f(inp)
out[0].sum().backward()
# The important bit: the forward fn returns 2 outputs,
# but one of them is a symint so we should only see
# 1 grad_output as an input to the backward graph.
# (Otherwise, autograd will plumb a None as the value of the grad_output,
# which causes inductor to complain).
self.assertExpectedInline(
bw_graph_cell[0].code.strip(),
"""\
def forward(self, tangents_1):
return (tangents_1,)""",
)
def test_no_grad_input_output(self):
def f(a, b):
return a.cos(), b.cos(), a * b
inp_thunks = [
lambda: torch.randn(5, requires_grad=True),
lambda: torch.randn(5, requires_grad=False),
]
for inps in itertools.product(inp_thunks, repeat=2):
inps = [i() for i in inps]
self.verify_aot_autograd(f, inps)
def test_some_output_requires_grad_input_doesnt(self):
def f(a, b):
a_view = a.view(-1)
a_view.requires_grad_(True)
return a_view
inp = [torch.randn(3, 3), torch.randn(3, 3, requires_grad=True)]
self.verify_aot_autograd(f, inp)
def test_some_outputs_dont_require_grad_view(self):
def f(a, b):
return a.detach(), b
inp = [
torch.randn(3, 3, requires_grad=True),
torch.randn(3, 3, requires_grad=True),
]
self.verify_aot_autograd(f, inp)
def test_some_outputs_dont_require_grad_non_view(self):
def f(a, b):
return a.add(1).detach(), b
inp = [
torch.randn(3, 3, requires_grad=True),
torch.randn(3, 3, requires_grad=True),
]
self.verify_aot_autograd(f, inp)
def test_inner_grad(self):
def foo(x):
y = torch.exp(x)
z = torch.autograd.grad(y, x)
return z
inps = [torch.randn((), requires_grad=True)]
self.verify_aot_autograd(foo, inps)
def test_grad_context(self):
def foo(x):
return x * 2
inps = [torch.randn((), requires_grad=True)]
graph_size = None
def get_graph_size(fx_g, _):
nonlocal graph_size
graph_size = len(fx_g.graph.nodes)
return fx_g
f = aot_function(foo, nop, get_graph_size)
with torch.set_grad_enabled(False):
f(*inps)
self.assertIsNone(graph_size)
f = aot_function(foo, nop, get_graph_size)
with torch.set_grad_enabled(True):
out = f(*inps)
self.assertIsNone(graph_size)
out.sum().backward()
self.assertTrue(graph_size > 2)
def test_output_dict(self):
def f(x):
return {"a": x, "b": x}
inp = [torch.randn(3, 3, requires_grad=True)]
self.verify_aot_autograd(f, inp)
def f(x, y):
return {"a": x, "b": y + x}
inp = [torch.randn(3, requires_grad=True), torch.randn(3)]
self.verify_aot_autograd(f, inp)
def f(x):
new_d = {}
for k in x:
new_d[k] = x[k] * 2
return new_d
a = torch.randn(3, requires_grad=True)
b = torch.randn(3, requires_grad=True)
def inp_callable():
inps = [{"a": a, "b": b}]
return inps, inps
self.verify_aot_autograd(f, inp_callable)
def test_module(self):
mod = nn.Sequential(nn.Linear(32, 32), nn.ReLU())
compiled_mod = compiled_module(mod, nop, nop)
inp = torch.randn(32, 32)
ref_out = mod(inp)
ref_out.sum().backward()
ref_grads = sorted([(name, p.grad) for name, p in mod.named_parameters()])
out = compiled_mod(inp)
out.sum().backward()
grads = sorted([(name, p.grad) for name, p in mod.named_parameters()])
self.assertEqual((out, grads), (ref_out, ref_grads))
def test_batchnorm(self):
mod = compiled_module(nn.BatchNorm2d(4), nop, nop)
x = torch.ones(1, 4, 2, 2)
mod(x).sum().backward()
def test_list_codegen(self):
def list_nop(f, _):
def g(inps):
return f(*inps)
g._boxed_call = True
return g
def f(a, b, c):
return a.sin() * b.cos() * c.sin()
f = aot_function(f, list_nop)
inp = [torch.randn(5, requires_grad=True) for _ in range(3)]
f(*inp).sum().backward()
@patch("torch._functorch.aot_autograd.AOT_COUNTER", new_callable=itertools.count)
def test_compilation_context(self, counter):
def f(x):
return x.sin().sin()
count = []
def compiler(fx_g, _):
context = get_aot_compilation_context()
count.append((context[0], len(fx_g.graph.nodes)))
return fx_g
f = aot_function(f, compiler)
out = f(torch.randn(5, requires_grad=True))
f = aot_function(f, compiler)
f(torch.randn(5))
out.sum().backward()
self.assertExpectedInline(
str(count),
"""[(['0_forward'], 4), (['1_inference'], 4), (['0_backward'], 8)]""",
)
def test_dupe_arg(self):
def f(x, y):
return x + y
x = torch.randn(3, 3, requires_grad=True)
self.verify_aot_autograd(f, [x, x])
def test_dupe_arg_torture(self):
def f(x, y):
x.t_()
y.unsqueeze_(0)
return x + y
x = torch.randn(3, 3, requires_grad=True).clone()
self.verify_aot_autograd(f, [x, x])
# See https://github.com/pytorch/pytorch/issues/100224
def test_dupe_arg_returned_as_output(self):
def f(a, b, a_):
a[0].add_(1)
return a_
f_compiled = aot_function(f, nop)
a = torch.ones(2)
b = torch.ones(2)
out_ref = f(a, b, a)
a2 = torch.ones(2)
b2 = torch.ones(2)
out_test = f_compiled(a2, b2, a2)
self.assertEqual(out_ref, out_test)
self.assertEqual(a, a2)
@patch("torch._functorch.aot_autograd.AOT_COUNTER", new_callable=itertools.count)
@patch("torch._functorch.config.debug_assert", True)
def test_invalid_dupe_left_bias(self, counter):
# This test checks that, just because only the first
# argument did a metadata mutation, we still correctly
# switch to strategy 2 (deduplicate)
# See: https://github.com/pytorch/pytorch/pull/89896#discussion_r1036224447
class F(torch.nn.Module):
def forward(self, x, y):
x.t_()
return (x + y,)
x = torch.randn(3, 3, requires_grad=True).clone()
y = torch.randn(3, 3, requires_grad=True)
self.verify_aot_autograd(F(), [x, x])
fxx = aot_module_simplified(F(), (x, x), nop)
self.assertExpectedRaisesInline(
AssertionError,
lambda: fxx(x, y),
"""At compilation time, graph 2 was compiled under the assumption that input 1 would be a duplicate of input 0, but at runtime this was not the case. This indicates a guard bug in AOTAutograd or Dynamo, please file a bug to PyTorch.""", # noqa: B950
)
@patch("torch._functorch.aot_autograd.AOT_COUNTER", new_callable=itertools.count)
@patch("torch._functorch.config.debug_assert", True)
def test_invalid_dupe(self, counter):
self._test_invalid_dupe(counter, fake=False)
# See Note: Dynamo recompilation guarding invalid grad for why this test exists
@patch("torch._functorch.aot_autograd.AOT_COUNTER", new_callable=itertools.count)
@patch("torch._functorch.config.debug_assert", True)
def test_invalid_dupe_fake(self, counter):
self._test_invalid_dupe(counter, fake=True)
def _test_invalid_dupe(self, counter, fake):
class F(torch.nn.Module):
def forward(self, x, y):
x.unsqueeze_(0)
y.unsqueeze_(0)
return (x + y,)
x = torch.randn(3, 3, requires_grad=True).clone()
y = torch.randn(3, 3, requires_grad=True).clone()
if fake:
shape_env = ShapeEnv()
fake_mode = FakeTensorMode(shape_env=shape_env)
fake_x = fake_mode.from_tensor(x)
fake_y = fake_mode.from_tensor(y)
if fake:
fxy = aot_module_simplified(F(), (fake_x, fake_y), nop)
else:
fxy = aot_module_simplified(F(), (x, y), nop)
fxy(x, y)
x = torch.randn(3, 3, requires_grad=True).clone()
y = torch.randn(3, 3, requires_grad=True).clone()
fxy(x, x) # is ok!
if fake:
fxx = aot_module_simplified(F(), (fake_x, fake_x), nop)
else:
fxx = aot_module_simplified(F(), (x, x), nop)
x = torch.randn(3, 3, requires_grad=True).clone()
y = torch.randn(3, 3, requires_grad=True).clone()
fxx(x, x)
# Note This should not raise! Once we have guards in place here,
# we will have this working correctly, as it should recompile.
x = torch.randn(3, 3, requires_grad=True).clone()
y = torch.randn(3, 3, requires_grad=True).clone()
self.assertExpectedRaisesInline(
AssertionError,
lambda: fxx(x, y),
"""At compilation time, graph 1 was compiled under the assumption that input 1 would be a duplicate of input 0, but at runtime this was not the case. This indicates a guard bug in AOTAutograd or Dynamo, please file a bug to PyTorch.""", # noqa: B950
)
@patch("torch._functorch.aot_autograd.AOT_COUNTER", new_callable=itertools.count)
@patch("torch._functorch.config.debug_assert", True)
def test_invalid_requires_grad(self, counter):
self._test_invalid_requires_grad(counter, fake=False)
# See Note: Dynamo recompilation guarding invalid grad for why this test exists
@patch("torch._functorch.aot_autograd.AOT_COUNTER", new_callable=itertools.count)
@patch("torch._functorch.config.debug_assert", True)
def test_invalid_requires_grad_fake(self, counter):
self._test_invalid_requires_grad(counter, fake=True)
def _test_invalid_requires_grad(self, counter, fake):
class F(torch.nn.Module):
def forward(self, x, y):
return (x + y,)
x = torch.randn(3, 3, requires_grad=True)
y = torch.randn(3, 3, requires_grad=True)
z = torch.randn(3, 3, requires_grad=False)
if fake:
shape_env = ShapeEnv()
fake_mode = FakeTensorMode(shape_env=shape_env)
fake_x = fake_mode.from_tensor(x)
fake_y = fake_mode.from_tensor(y)
fake_z = fake_mode.from_tensor(z)
if fake:
fxy = aot_module_simplified(F(), (fake_x, fake_y), nop)
else:
fxy = aot_module_simplified(F(), (x, y), nop)
compare_equal_outs_and_grads(self, F(), fxy, (x, y))
compare_equal_outs_and_grads(self, F(), fxy, (x, z))
if fake:
fxz = aot_module_simplified(F(), (fake_x, fake_z), nop)
else:
fxz = aot_module_simplified(F(), (x, z), nop)
compare_equal_outs_and_grads(self, F(), fxz, (x, z))
self.assertExpectedRaisesInline(
AssertionError,
lambda: fxz(x, y),
"""At compilation time, graph 1 was compiled under the assumption that input 1 would not require grad, but at runtime this was not the case. This indicates a guard bug in AOTAutograd or Dynamo, please file a bug to PyTorch.""", # noqa: B950
)
def test_custom_autograd(self):
class CustomFn(torch.autograd.Function):
@staticmethod
def forward(ctx, x):
return x.clone()
@staticmethod
def backward(ctx, grad_output):
return grad_output + 1
def f(x):
return CustomFn.apply(x)
self.verify_aot_autograd(f, [torch.randn(3)])
@unittest.skipIf(not torch.cuda.is_available(), "CUDA is unavailable")
def test_autocast_disable_guard(self):
with torch._C._DisableAutocast():
x = torch.rand([4, 4]).cuda()
y = x @ x
self.assertEqual(y.dtype, torch.float32)
@unittest.skipIf(not torch.cuda.is_available(), "CUDA is unavailable")
def test_nonidempotent_amp(self):
def f(self_s_emb, add_3):
einsum_2 = torch.functional.einsum("ah,th->t", self_s_emb, add_3)
log_softmax_2 = einsum_2.log_softmax(-1)
return (log_softmax_2,)
args = [
torch.rand((1, 256), dtype=torch.float32, device="cuda"),
torch.rand((30, 256), dtype=torch.float16, device="cuda"),
]
with torch.cuda.amp.autocast(enabled=True):
self.verify_aot_autograd(f, args)
args = [e.requires_grad_(True) for e in args]
with torch.cuda.amp.autocast(enabled=True):
self.verify_aot_autograd(f, args)
@unittest.skipIf(not torch.cuda.is_available(), "CUDA is unavailable")
@unittest.skipIf(not torch.backends.cudnn.is_available(), "CUDNN is unavailable")
@skipIfRocm # https://github.com/pytorch/pytorch/issues/96560
def test_batch_norm_amp(self):
device = "cuda"
input_dtype = torch.float16
param_dtype = torch.float32
weight, bias = (
torch.ones(64, device=device, dtype=param_dtype, requires_grad=True)
for _ in range(2)
)
running_mean, running_var = (
torch.ones(64, device=device, dtype=param_dtype) for _ in range(2)
)
def bn(x):
return torch.ops.aten.cudnn_batch_norm(
x,
weight,
bias,
running_mean,
running_var,
False,
0.1,
1e-05,
)
inp = torch.ones(
torch.Size([16, 64, 112, 112]), dtype=input_dtype, device=device
)
ref = bn(inp)
cudnn_batch_norm_decomp = torch._decomp.get_decompositions(
{torch.ops.aten.cudnn_batch_norm}
)
aot_fn = make_fx(bn, decomposition_table=cudnn_batch_norm_decomp)(inp)
res = aot_fn(inp)
for a, b in zip(ref, res):
assert torch.allclose(a, b)
def test_output_op_depending_on_symint(self):
"""
It won't be obvious from reading this test what it's testing for. We should probably make it into a more
focused unit test.
An issue with the following program was the expand op would end up depending on a symint whose proxy was
incorrectly associated with one of the grad tensors rather than input tensors. It broke partitioner logic
and the net result was aot_function failed to produce a function and threw an exception instead.
"""
inp = torch.randn(5, requires_grad=True)
def f(x):
return x.expand(x.shape)
# TODO(whc) make this work (test setup is wrong somehow)
# joint_forward_backward = create_joint_forward_backward(f)
# out = f(inp)
# joint_inputs = ([inp], [out.detach().contiguous()])
# fx_g = make_fx(joint_forward_backward)(*joint_inputs)
# TODO: assert outputs of fwd graph trace to correct symint
# e2e test that fails without symint clone fix
af = aot_function(
f,
nop,
partition_fn=partial(
min_cut_rematerialization_partition, compiler="inductor"
),
dynamic=True,
)
out = af(inp)
self.assertEqual(out, f(inp))
def test_inference_mode(self):
m = torch.nn.Linear(4, 4)
inp = torch.randn(4, 4)
aot_mod = aot_module(m, fw_compiler=nop)
with torch.inference_mode():
out_ref = m(inp)
out_test = aot_mod(inp)
self.assertEqual(out_ref, out_test)
def test_default_partitioner_saves_symints_not_tensors_for_bw(self):
"""
In this test, the important thing is that primals_1 is **only** needed in the backward
in order to grab its sizes.
We need to assert that what we save for the backward are the tensor's sizes, and not the tensor itself.
The way this test is set up, it will actually fail if we try to save the input tensor for backward.
Why?
b.masked_fill_(c, 0) has a backward that requires knowing a's sizes
b.masked_fill_(c, 0) **also** mutates a (because b and a are aliased)
The autograd engine yells at us if we save "a" for backward, and then try to mutate it.
"""
inp = torch.randn(2, 2, requires_grad=True)
def f(a):
b = a[0]
c = torch.ones_like(b, dtype=torch.bool)
d = b.masked_fill_(c, 0)
return d
compiled_f = aot_function(f, nop, dynamic=True)
inp_ref = torch.ones(2, 2, requires_grad=True)
inp_test = torch.ones(2, 2, requires_grad=True)
out_ref = f(inp_ref.clone())
out_test = compiled_f(inp_test.clone())
self.assertEqual(out_ref, out_test)
out_ref.sum().backward()
out_test.sum().backward()
self.assertEqual(inp_ref.grad, inp_test.grad)
def test_buffer_copied_in_graph(self):
class MyModel(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
self.buf = torch.nn.Buffer(torch.zeros(1))
self.w1 = torch.nn.Parameter(torch.zeros(1))
self.w2 = torch.nn.Parameter(torch.zeros(1))
def forward(self, x):
self.buf.add_(1)
return (self.w1 * x * self.w2).sum() + self.buf.sum()
model_for_eager = MyModel()
model_for_compile = copy.deepcopy(model_for_eager)
fw_graph_cell = [None]
compiled_f = aot_module(
model_for_compile,
fw_compiler=make_boxed_compiler(
partial(extract_graph, graph_cell=fw_graph_cell)
),
bw_compiler=nop,
keep_inference_input_mutations=True,
)
inp_ref = torch.ones(1, requires_grad=True)
inp_test = torch.ones(1, requires_grad=True)
out_ref = model_for_eager(inp_ref.clone())
out_test = compiled_f(inp_test.clone())
self.assertExpectedInline(
fw_graph_cell[0].code.strip(),
"""\
def forward(self, primals_1, primals_2, primals_3, primals_4):
add = torch.ops.aten.add.Tensor(primals_3, 1)
mul = torch.ops.aten.mul.Tensor(primals_1, primals_4)
mul_1 = torch.ops.aten.mul.Tensor(mul, primals_2)
sum_1 = torch.ops.aten.sum.default(mul_1); mul_1 = None
sum_2 = torch.ops.aten.sum.default(add)
add_1 = torch.ops.aten.add.Tensor(sum_1, sum_2); sum_1 = sum_2 = None
copy_ = torch.ops.aten.copy_.default(primals_3, add); primals_3 = add = copy_ = None
return (add_1, primals_1, primals_2, primals_4, mul)""",
)
self.assertEqual(out_ref, out_test)
out_ref.sum().backward()
out_test.sum().backward()
eager_grads = [p.grad for _, p in model_for_eager.named_parameters()]
compile_grads = [p.grad for _, p in model_for_compile.named_parameters()]
self.assertEqual(eager_grads, compile_grads)
self.assertEqual(inp_ref.grad, inp_test.grad)
def test_buffer_copied_in_graph_with_different_shapes(self):
class MyModel(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
self.buf = torch.nn.Buffer(torch.ones(4, 4))
self.w = torch.nn.Parameter(
torch.Tensor([[4, 5], [1, 2], [6, 7], [8, 9]])
)
def forward(self, x):
self.buf.add_(1)
return (self.w @ x).sum() + self.buf.sum()
model_for_eager = MyModel()
model_for_compile = copy.deepcopy(model_for_eager)
fw_graph_cell = [None]
compiled_f = aot_module(
model_for_compile,
fw_compiler=make_boxed_compiler(
partial(extract_graph, graph_cell=fw_graph_cell)
),
bw_compiler=nop,
keep_inference_input_mutations=True,
)
inp_ref = torch.ones(2, 4, requires_grad=True)
inp_test = torch.ones(2, 4, requires_grad=True)
out_ref = model_for_eager(inp_ref.clone())
out_test = compiled_f(inp_test.clone())
self.assertExpectedInline(
fw_graph_cell[0].code.strip(),
"""\
def forward(self, primals_1, primals_2, primals_3):
add = torch.ops.aten.add.Tensor(primals_2, 1)
mm = torch.ops.aten.mm.default(primals_1, primals_3)
sum_1 = torch.ops.aten.sum.default(mm); mm = None
sum_2 = torch.ops.aten.sum.default(add)
add_1 = torch.ops.aten.add.Tensor(sum_1, sum_2); sum_1 = sum_2 = None
copy_ = torch.ops.aten.copy_.default(primals_2, add); primals_2 = add = copy_ = None
return (add_1, primals_1, primals_3)""",
)
self.assertEqual(out_ref, out_test)
out_ref.sum().backward()
out_test.sum().backward()
eager_grads = [p.grad for _, p in model_for_eager.named_parameters()]
compile_grads = [p.grad for _, p in model_for_compile.named_parameters()]
self.assertEqual(eager_grads, compile_grads)
self.assertEqual(inp_ref.grad, inp_test.grad)
def test_buffer_batch_norm(self):
class MyModel(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
self.m = torch.nn.BatchNorm1d(100)
def forward(self, x):
return self.m(x)
model_for_eager = MyModel()
model_for_compile = copy.deepcopy(model_for_eager)
fw_graph_cell = [None]
bw_graph_cell = [None]
compiled_f = aot_module(
model_for_compile,
fw_compiler=make_boxed_compiler(
partial(extract_graph, graph_cell=fw_graph_cell)
),
bw_compiler=make_boxed_compiler(
partial(extract_graph, graph_cell=bw_graph_cell)
),
keep_inference_input_mutations=True,
)
inp_ref = torch.ones(20, 100, requires_grad=True)
inp_test = torch.ones(20, 100, requires_grad=True)
out_ref = model_for_eager(inp_ref.clone())
out_test = compiled_f(inp_test.clone())
self.assertExpectedInline(
fw_graph_cell[0].code.strip(),
"""\
def forward(self, primals_1, primals_2, primals_3, primals_4, primals_5, primals_6):
add = torch.ops.aten.add.Tensor(primals_5, 1)
_native_batch_norm_legit_functional = torch.ops.aten._native_batch_norm_legit_functional.default(primals_6, primals_1, primals_2, primals_3, primals_4, True, 0.1, 1e-05); primals_2 = None
getitem = _native_batch_norm_legit_functional[0]
getitem_1 = _native_batch_norm_legit_functional[1]
getitem_2 = _native_batch_norm_legit_functional[2]
getitem_3 = _native_batch_norm_legit_functional[3]
getitem_4 = _native_batch_norm_legit_functional[4]; _native_batch_norm_legit_functional = None
copy_ = torch.ops.aten.copy_.default(primals_3, getitem_3); primals_3 = copy_ = None
copy__1 = torch.ops.aten.copy_.default(primals_4, getitem_4); primals_4 = copy__1 = None
copy__2 = torch.ops.aten.copy_.default(primals_5, add); primals_5 = add = copy__2 = None
return (getitem, primals_1, primals_6, getitem_1, getitem_2, getitem_3, getitem_4)""", # noqa: B950
)
self.assertEqual(out_ref, out_test)
out_ref.sum().backward()
out_test.sum().backward()
eager_grads = [p.grad for _, p in model_for_eager.named_parameters()]
compile_grads = [p.grad for _, p in model_for_compile.named_parameters()]
self.assertEqual(eager_grads, compile_grads)
self.assertExpectedInline(
bw_graph_cell[0].code.strip(),
"""\
def forward(self, primals_1, primals_6, getitem_1, getitem_2, getitem_3, getitem_4, tangents_1):
native_batch_norm_backward = torch.ops.aten.native_batch_norm_backward.default(tangents_1, primals_6, primals_1, getitem_3, getitem_4, getitem_1, getitem_2, True, 1e-05, [True, True, True]); tangents_1 = primals_6 = primals_1 = getitem_3 = getitem_4 = getitem_1 = getitem_2 = None
getitem_5 = native_batch_norm_backward[0]
getitem_6 = native_batch_norm_backward[1]
getitem_7 = native_batch_norm_backward[2]; native_batch_norm_backward = None
return (getitem_6, getitem_7, None, None, None, getitem_5)""", # noqa: B950
)
self.assertEqual(inp_ref.grad, inp_test.grad)
def test_new_inp_requires_grad_now(self):
def f(x, y):
return x.add_(y)
fw_graph_cell = [None]
bw_graph_cell = [None]
compiled_f = aot_function(
f,
fw_compiler=make_boxed_compiler(
partial(extract_graph, graph_cell=fw_graph_cell)
),
bw_compiler=make_boxed_compiler(
partial(extract_graph, graph_cell=bw_graph_cell)
),
keep_inference_input_mutations=True,
)
inp_ref = (
torch.ones(20, 100, requires_grad=False),
torch.ones(20, 100, requires_grad=True),
)
inp_test = (
torch.ones(20, 100, requires_grad=False),
torch.ones(20, 100, requires_grad=True),
)
out_ref = f(*inp_ref)
out_test = compiled_f(*inp_test)
# There is no copy_ method
self.assertExpectedInline(
fw_graph_cell[0].code.strip(),
"""\
def forward(self, primals_1, primals_2):
clone = torch.ops.aten.clone.default(primals_1); primals_1 = None
add = torch.ops.aten.add.Tensor(clone, primals_2); clone = primals_2 = None
return (add, add)""",
) # noqa: B950
self.assertEqual(out_ref, out_test)
out_ref.sum().backward()
out_test.sum().backward()
self.assertExpectedInline(
bw_graph_cell[0].code.strip(),
"""\
def forward(self, tangents_1):
return (None, tangents_1)""",
) # noqa: B950
def test_real_weights_in_symbolic_mode(self):
from functorch.experimental import functionalize
class M(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
self.linear = torch.nn.Linear(5, 5)
def forward(self, x):
x = self.linear(x)
return x
m = M().eval()
inp = torch.randn(2, 5)
gm = make_fx(m, tracing_mode="symbolic", _allow_non_fake_inputs=True)(inp)
self.assertEqual(gm(torch.ones(2, 5)), m(torch.ones(2, 5)))
gm_functionalized = make_fx(
functionalize(
gm,
),
tracing_mode="symbolic",
_allow_non_fake_inputs=True,
)(inp)
self.assertEqual(gm_functionalized(torch.ones(2, 5)), m(torch.ones(2, 5)))
inp_count = 0
for node in gm.graph.nodes:
if node.op == "placeholder":
inp_count += 1
# No more param lifting
self.assertEqual(inp_count, 1)
inp_count = 0
for node in gm_functionalized.graph.nodes:
if node.op == "placeholder":
inp_count += 1
# No more param lifting
self.assertEqual(inp_count, 1)
with self.assertRaisesRegex(
Exception, "Please convert all Tensors to FakeTensors"
):
make_fx(m, tracing_mode="symbolic", _allow_non_fake_inputs=False)(
torch.randn(2, 5)
)
def test_real_weights_in_symbolic_mode_with_inplace_ops(self):
class M(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
self.buffer = torch.nn.Buffer(torch.ones(4, 5))
def forward(self, x):
y = self.buffer.add_(3)
y.resize_([20])
assert y.shape == self.buffer.shape
return x.sum() + self.buffer.sum()
m = M().eval()
inp = torch.randn(2, 5)
# inplace mutation on attr is not allowed
with self.assertRaisesRegex(Exception, "Can't call metadata"):
make_fx(m, tracing_mode="symbolic", _allow_non_fake_inputs=True)(inp)
def _compile_and_erase_bases(self, *output_view_indices):
# Overrides _base and _view_func tensor attributes, so as to avoid the view-replay
# execution path when reconstructing views.
class NoViewReplayTensor(torch.Tensor):
@property
def _base(self):
return None
@property
def _view_func(self):
return None
# Wraps the outputs that are views of the FX graph 'g' with NoViewReplayTensor,
# since they are the only ones that will get reconstructed.
def wrapper(g, *args, **kwargs):
outs = list(g(*args, **kwargs))
for i in output_view_indices:
outs[i] = NoViewReplayTensor(outs[i])
return tuple(outs)
return lambda f: aot_function(f, fw_compiler=lambda g, _: partial(wrapper, g))
def test_output_aliases_input_view_meta_replay(self):
@self._compile_and_erase_bases(0)
def f(a):
return a.view(-1)
inp = torch.ones(2, 2, requires_grad=True)
out = f(inp)
self.assertIsNotNone(out.grad_fn)
self.assertExpectedInline(
str(out.grad_fn.__class__), """<class 'ViewBackward0'>"""
)
def test_output_aliases_intermediate_view_meta_replay(self):
@self._compile_and_erase_bases(0, 1)
def f(a):
b = a.clone()
return b.view(-1), b.view(-1)
inp = torch.ones(2, 2, requires_grad=True)
out1, out2 = f(inp)
self.assertIsNotNone(out1.grad_fn)
self.assertExpectedInline(
str(out1.grad_fn.__class__), """<class 'ViewBackward0'>"""
)
self.assertIsNotNone(out2.grad_fn)
self.assertExpectedInline(
str(out2.grad_fn.__class__), """<class 'ViewBackward0'>"""
)
def test_output_aliases_output_view_meta_replay(self):
@self._compile_and_erase_bases(1)
def f(a):
b = a.add(10)
return b, b.view(-1)
inp = torch.ones(2, 2, requires_grad=True)
out1, out2 = f(inp)
self.assertEqual(out1.untyped_storage(), out2.untyped_storage())
self.assertIsNotNone(out2.grad_fn)
self.assertExpectedInline(
str(out2.grad_fn.__class__), """<class 'ViewBackward0'>"""
)
@skipIfTorchDynamo()
@patch("torch._dynamo.config.assume_static_by_default", False)
def test_dynamic_output_aliases_input_view_meta_replay(self):
# - torch.compile: using it so we can have a SymInt in the FX graph.
# - Compiling with inductor, so that tensor._base isn't tracked.
#
# This should force the use of as_strided in the view reconstruction path.
# The first 2 view-replay paths won't be taken because:
# - target_functional_tensor will be symbolic (_functionalize_is_symbolic call)
# - tensor._base will be None
@torch.compile(backend="inductor")
def f(a, sz):
return a.view(sz), a.view(-1)
inp = torch.ones(2, 2, requires_grad=True)
out1, out2 = f(inp, (4,))
self.assertIsNotNone(out1.grad_fn)
self.assertExpectedInline(
str(out1.grad_fn.__class__), """<class 'AsStridedBackward0'>"""
)
self.assertIsNotNone(out2.grad_fn)
self.assertExpectedInline(
str(out2.grad_fn.__class__), """<class 'ViewBackward0'>"""
)
def extract_graph(fx_g, _, graph_cell):
graph_cell[0] = fx_g
return fx_g
def get_ins_outs(fx_g):
ins = []
outs = []
for n in fx_g.graph.nodes:
if n.op == "placeholder":
ins.append(n)
elif n.op == "output":
outs = tuple(n.args[0])
return ins, outs
def get_num_ins_outs(fx_g):
return tuple(len(i) for i in get_ins_outs(fx_g))
def get_fw_bw_graph(
f, inps, partitioner=min_cut_rematerialization_partition, dynamic=False
):
fw_graph_cell = [None]
bw_graph_cell = [None]
aot_function(
f,
fw_compiler=partial(extract_graph, graph_cell=fw_graph_cell),
bw_compiler=partial(extract_graph, graph_cell=bw_graph_cell),
partition_fn=partitioner,
decompositions=default_decompositions,
dynamic=dynamic,
)(*inps).sum().backward()
return (fw_graph_cell[0], bw_graph_cell[0])
class TestMod(torch.nn.Module):
def __init__(self, fn):
super().__init__()
self.p = torch.nn.Parameter(torch.ones(2, requires_grad=True))
self.fn = fn
def forward(self, *args):
return self.fn(self.p, *args)
class TestAOTExport(AOTTestCase):
def test_aot_export_ban_dropout_mut_pre_dispatch(self):
def fn(p, x):
y = torch.ops.aten.dropout.default(x, 0.1, train=False)
y.add_(1)
return (y,)
mod = TestMod(fn)
inp = torch.randn(2, 2)
with self.assertRaisesRegex(
RuntimeError, "cannot mutate tensors with frozen storage"
):
aot_export_module(mod, [inp], trace_joint=False, pre_dispatch=True)
gm, _ = aot_export_module(mod, [inp], trace_joint=False, pre_dispatch=False)
self.assertExpectedInline(
str(gm.code).strip(),
"""\
def forward(self, arg0_1, arg1_1):
clone = torch.ops.aten.clone.default(arg1_1); arg1_1 = None
add = torch.ops.aten.add.Tensor(clone, 1); clone = None
return (add,)""",
)
fw_graph_cell = [None]
bw_graph_cell = [None]
compiled_outs = aot_function(
fn,
fw_compiler=partial(extract_graph, graph_cell=fw_graph_cell),
bw_compiler=partial(extract_graph, graph_cell=bw_graph_cell),
partition_fn=default_partition,
decompositions=default_decompositions,
dynamic=True,
)(*inp)
fw_graph = fw_graph_cell[0]
bw_graph = bw_graph_cell[0]
self.assertExpectedInline(
str(fw_graph.code).strip(),
"""\
def forward(self, arg0_1, arg1_1):
clone = torch.ops.aten.clone.default(arg1_1); arg1_1 = None
add = torch.ops.aten.add.Tensor(clone, 1); clone = None
return (add,)""",
)
def test_aot_export_predispatch_func_simple(self):
def fn(p, x):
y = x + 2
with torch.no_grad():
y.add_(2)
return (x * 2 + y,)
mod = TestMod(fn)
inp = torch.randn(2, 2)
with torch.no_grad():
gm, _ = aot_export_module(mod, [inp], trace_joint=False, pre_dispatch=True)
self.assertExpectedInline(
str(gm.code).strip(),
"""\
def forward(self, arg0_1, arg1_1):
add = torch.ops.aten.add.Tensor(arg1_1, 2)
_set_grad_enabled = torch._C._set_grad_enabled(False); _set_grad_enabled = None
add_1 = torch.ops.aten.add.Tensor(add, 2); add = None
_set_grad_enabled_1 = torch._C._set_grad_enabled(False); _set_grad_enabled_1 = None
mul = torch.ops.aten.mul.Tensor(arg1_1, 2); arg1_1 = None
add_2 = torch.ops.aten.add.Tensor(mul, add_1); mul = add_1 = None
return (add_2,)""",
)
def test_aot_export_predispatch_func_composite_implicit(self):
def fn(p, x):
with torch.enable_grad():
y = x @ x
y.add_(2)
return (x.sum() + y.sum(),)
mod = TestMod(fn)
inp = torch.randn(2, 2)
with torch.no_grad():
gm, _ = aot_export_module(mod, [inp], trace_joint=False, pre_dispatch=True)
self.assertExpectedInline(
str(gm.code).strip(),
"""\
def forward(self, arg0_1, arg1_1):
_set_grad_enabled = torch._C._set_grad_enabled(True); _set_grad_enabled = None
matmul = torch.ops.aten.matmul.default(arg1_1, arg1_1)
_set_grad_enabled_1 = torch._C._set_grad_enabled(False); _set_grad_enabled_1 = None
add = torch.ops.aten.add.Tensor(matmul, 2); matmul = None
sum_1 = torch.ops.aten.sum.default(arg1_1); arg1_1 = None
sum_2 = torch.ops.aten.sum.default(add); add = None
add_1 = torch.ops.aten.add.Tensor(sum_1, sum_2); sum_1 = sum_2 = None
return (add_1,)""",
)
def test_aot_export_predispatch_composite_implicit_inplace(self):
def fn(x, p):
return (torch.ops.aten.absolute_.default(x.clone()),)
mod = TestMod(fn)
inp = torch.randn(2, 2)
gm, _ = aot_export_module(mod, [inp], trace_joint=False, pre_dispatch=True)
self.assertExpectedInline(
str(gm.code).strip(),
"""\
def forward(self, arg0_1, arg1_1):
clone = torch.ops.aten.clone.default(arg0_1); arg0_1 = None
abs_1 = torch.ops.aten.abs.default(clone); clone = None
return (abs_1,)""",
)
def test_aot_export_predispatch_composite_implicit_linear(self):
class MM(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
self.linear = torch.nn.Linear(2, 2)
def forward(self, x):
return (self.linear(x),)
mod = MM()
inp = torch.randn(2, 2)
gm, _ = aot_export_module(mod, [inp], trace_joint=False, pre_dispatch=True)
self.assertExpectedInline(
str(gm.code).strip(),
"""\
def forward(self, arg0_1, arg1_1, arg2_1):
linear = torch.ops.aten.linear.default(arg2_1, arg0_1, arg1_1); arg2_1 = arg0_1 = arg1_1 = None
return (linear,)""",
)
@unittest.expectedFailure
def test_aot_export_predispatch_outdtype(self):
class M(torch.nn.Module):
def __init__(self, weight):
super().__init__()
self.weight = weight
def forward(self, x):
y = x + 2
y.add_(5)
return (
out_dtype(torch.ops.aten.mm.default, torch.int32, y, self.weight),
)
weight = torch.randint(-128, 127, (5, 5), dtype=torch.int8)
mod = M(weight)
inp = torch.randint(-128, 127, (5, 5), dtype=torch.int8)
gm, _ = aot_export_module(mod, [inp], trace_joint=False, pre_dispatch=True)
self.assertExpectedInline(
str(gm.code).strip(),
"""\
def forward(self, arg0_1, arg1_1):
_set_grad_enabled = torch._C._set_grad_enabled(True); _set_grad_enabled = None
mm = torch.ops.aten.mm.default(arg1_1, arg1_1)
_set_grad_enabled_1 = torch._C._set_grad_enabled(False); _set_grad_enabled_1 = None
add = torch.ops.aten.add.Tensor(mm, 2); mm = None
sum_1 = torch.ops.aten.sum.default(arg1_1); arg1_1 = None
sum_2 = torch.ops.aten.sum.default(add); add = None
add_1 = torch.ops.aten.add.Tensor(sum_1, sum_2); sum_1 = sum_2 = None
return (add_1,)""",
)
def test_aot_export_predispatch_func_view(self):
def fn(p, x):
y = x @ x
y.add_(2)
return (x.sum() + y.view(1, 4).sum(),)
mod = TestMod(fn)
inp = torch.randn(2, 2)
gm, _ = aot_export_module(mod, [inp], trace_joint=False, pre_dispatch=True)
self.assertExpectedInline(
str(gm.code).strip(),
"""\
def forward(self, arg0_1, arg1_1):
matmul = torch.ops.aten.matmul.default(arg1_1, arg1_1)
add = torch.ops.aten.add.Tensor(matmul, 2); matmul = None
sum_1 = torch.ops.aten.sum.default(arg1_1); arg1_1 = None
view_1 = torch.ops.aten.view.default(add, [1, 4]); add = None
sum_2 = torch.ops.aten.sum.default(view_1); view_1 = None
add_1 = torch.ops.aten.add.Tensor(sum_1, sum_2); sum_1 = sum_2 = None
return (add_1,)""",
)
def test_aot_export_predispatch_buffer_mutation_metadata(self):
class Foo(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
self.foo = torch.nn.Buffer(torch.zeros(2, 2))
def forward(self, x):
self.foo.add_(4)
return (x.sum() + self.foo.sum(),)
inp = torch.randn(2, 2)
gm, graph_sig = aot_export_module(
Foo(), [inp], trace_joint=False, pre_dispatch=True
)
self.assertExpectedInline(
str(gm.code).strip(),
"""\
def forward(self, arg0_1, arg1_1):
add = torch.ops.aten.add.Tensor(arg0_1, 4); arg0_1 = None
sum_1 = torch.ops.aten.sum.default(arg1_1); arg1_1 = None
sum_2 = torch.ops.aten.sum.default(add)
add_1 = torch.ops.aten.add.Tensor(sum_1, sum_2); sum_1 = sum_2 = None
return (add, add_1)""",
)
eager_mod = Foo()
output_1, output_2 = gm(torch.zeros(2, 2), inp)
eager_output = eager_mod(inp)
self.assertTrue(torch.allclose(output_2, eager_output[0]))
_, output_2 = gm(output_1, inp)
eager_output = eager_mod(inp)
self.assertTrue(torch.allclose(output_2, eager_output[0]))
self.assertTrue("foo" in graph_sig.buffers)
self.assertTrue(graph_sig.inputs_to_buffers["arg0_1"] == "foo")
def test_aot_export_predispatch_with_autograd_op(self):
def foo(p, x):
with torch.enable_grad():
y = x + 5
y.add_(5)
y.add_(7)
return (x.cos() + y.sin(),)
inp = torch.randn(2, 2)
mod = TestMod(foo)
with torch.no_grad():
gm, _ = aot_export_module(mod, [inp], trace_joint=False, pre_dispatch=True)
self.assertExpectedInline(
str(gm.code).strip(),
"""\
def forward(self, arg0_1, arg1_1):
_set_grad_enabled = torch._C._set_grad_enabled(True); _set_grad_enabled = None
add = torch.ops.aten.add.Tensor(arg1_1, 5)
add_1 = torch.ops.aten.add.Tensor(add, 5); add = None
add_2 = torch.ops.aten.add.Tensor(add_1, 7); add_1 = None
cos = torch.ops.aten.cos.default(arg1_1); arg1_1 = None
sin = torch.ops.aten.sin.default(add_2); add_2 = None
add_3 = torch.ops.aten.add.Tensor(cos, sin); cos = sin = None
_set_grad_enabled_1 = torch._C._set_grad_enabled(False); _set_grad_enabled_1 = None
return (add_3,)""",
)
@unittest.skipIf(IS_WINDOWS, "Windows isn't supported for this case")
@unittest.skipIf(
not torchdynamo.is_dynamo_supported(), "TorchDynamo is not supported"
)
def test_aot_export_predispatch_with_cond_nested(self):
class M(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
def forward(self, x):
def true_fn(x):
y = x.sin()
y.add_(5)
def true_true_fn(x):
y = x.sin()
y.add_(7)
return y.sin()
def true_false_fn(x):
return x.cos()
return torch.cond(
y.cos().sum() > 5, true_true_fn, true_false_fn, [y.cos()]
)
def false_fn(x):
z = x.cos()
z.add_(6)
return z.sin()
a = torch.cond(x.sum() > 4, true_fn, false_fn, [x])
return (a + 3, a + 4)
inp = torch.randn(2, 2)
gm, _ = aot_export_module(M(), [inp], trace_joint=False, pre_dispatch=True)
self.assertExpectedInline(
str(gm.code).strip(),
"""\
def forward(self, arg0_1):
sum_1 = torch.ops.aten.sum.default(arg0_1)
gt = torch.ops.aten.gt.Scalar(sum_1, 4); sum_1 = None
true_graph_0 = self.true_graph_0
false_graph_0 = self.false_graph_0
cond = torch.ops.higher_order.cond(gt, true_graph_0, false_graph_0, [arg0_1]); gt = true_graph_0 = false_graph_0 = arg0_1 = None
getitem = cond[0]; cond = None
add = torch.ops.aten.add.Tensor(getitem, 3)
add_1 = torch.ops.aten.add.Tensor(getitem, 4); getitem = None
return (add, add_1)""", # noqa: B950
)
self.assertExpectedInline(
str(gm.true_graph_0.code).strip(),
"""\
def forward(self, arg0_1):
sin = torch.ops.aten.sin.default(arg0_1); arg0_1 = None
add = torch.ops.aten.add.Tensor(sin, 5); sin = None
cos = torch.ops.aten.cos.default(add)
sum_1 = torch.ops.aten.sum.default(cos); cos = None
gt = torch.ops.aten.gt.Scalar(sum_1, 5); sum_1 = None
cos_1 = torch.ops.aten.cos.default(add); add = None
true_graph_0 = self.true_graph_0
false_graph_0 = self.false_graph_0
cond = torch.ops.higher_order.cond(gt, true_graph_0, false_graph_0, [cos_1]); gt = true_graph_0 = false_graph_0 = cos_1 = None
getitem = cond[0]; cond = None
return (getitem,)""", # noqa: B950
)
self.assertExpectedInline(
str(gm.true_graph_0.true_graph_0.code).strip(),
"""\
def forward(self, arg0_1):
sin = torch.ops.aten.sin.default(arg0_1); arg0_1 = None
add = torch.ops.aten.add.Tensor(sin, 7); sin = None
sin_1 = torch.ops.aten.sin.default(add); add = None
return (sin_1,)""",
)
@unittest.skipIf(IS_WINDOWS, "Windows isn't supported for this case")
@unittest.skipIf(
not torchdynamo.is_dynamo_supported(), "TorchDynamo is not supported"
)
def test_aot_export_predispatch_map_1(self):
class M(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
def forward(self, x, y):
def true_fn(x, r):
y = x.sin()
y.add_(5)
return y.cos() + r.sum()
def false_fn(x, r):
z = x.cos()
def f(x, y):
a = x.cos()
a.add_(5)
return a + y
return (
z
+ control_flow.map(f, z, r).sum()
+ control_flow.map(f, z, r).sum()
)
a = torch.cond(x.sum() > 4, true_fn, false_fn, [x, y])
return (a + 3, a + 4)
inps = [torch.randn(2, 2), torch.ones(2)]
gm, _ = aot_export_module(M(), inps, trace_joint=False, pre_dispatch=True)
self.assertExpectedInline(
str(gm.code).strip(),
"""\
def forward(self, arg0_1, arg1_1):
sum_1 = torch.ops.aten.sum.default(arg0_1)
gt = torch.ops.aten.gt.Scalar(sum_1, 4); sum_1 = None
true_graph_0 = self.true_graph_0
false_graph_0 = self.false_graph_0
cond = torch.ops.higher_order.cond(gt, true_graph_0, false_graph_0, [arg0_1, arg1_1]); gt = true_graph_0 = false_graph_0 = arg0_1 = arg1_1 = None
getitem = cond[0]; cond = None
add = torch.ops.aten.add.Tensor(getitem, 3)
add_1 = torch.ops.aten.add.Tensor(getitem, 4); getitem = None
return (add, add_1)""", # noqa: B950
)
self.assertExpectedInline(
str(gm.true_graph_0.code).strip(),
"""\
def forward(self, arg0_1, arg1_1):
sin = torch.ops.aten.sin.default(arg0_1); arg0_1 = None
add = torch.ops.aten.add.Tensor(sin, 5); sin = None
cos = torch.ops.aten.cos.default(add); add = None
sum_1 = torch.ops.aten.sum.default(arg1_1); arg1_1 = None
add_1 = torch.ops.aten.add.Tensor(cos, sum_1); cos = sum_1 = None
return (add_1,)""",
)
self.assertExpectedInline(
str(gm.false_graph_0.code).strip(),
"""\
def forward(self, arg0_1, arg1_1):
cos = torch.ops.aten.cos.default(arg0_1); arg0_1 = None
select = torch.ops.aten.select.int(cos, 0, 0); select = None
body_graph_0 = self.body_graph_0
map_impl = torch.ops.higher_order.map_impl(body_graph_0, [cos], [arg1_1]); body_graph_0 = None
getitem = map_impl[0]; map_impl = None
sum_1 = torch.ops.aten.sum.default(getitem); getitem = None
add = torch.ops.aten.add.Tensor(cos, sum_1); sum_1 = None
select_1 = torch.ops.aten.select.int(cos, 0, 0); select_1 = None
body_graph_1 = self.body_graph_1
map_impl_1 = torch.ops.higher_order.map_impl(body_graph_1, [cos], [arg1_1]); body_graph_1 = cos = arg1_1 = None
getitem_1 = map_impl_1[0]; map_impl_1 = None
sum_2 = torch.ops.aten.sum.default(getitem_1); getitem_1 = None
add_1 = torch.ops.aten.add.Tensor(add, sum_2); add = sum_2 = None
return (add_1,)""",
)
self.assertExpectedInline(
str(gm.false_graph_0.body_graph_0.code).strip(),
"""\
def forward(self, arg0_1, arg1_1):
cos = torch.ops.aten.cos.default(arg0_1); arg0_1 = None
add = torch.ops.aten.add.Tensor(cos, 5); cos = None
add_1 = torch.ops.aten.add.Tensor(add, arg1_1); add = arg1_1 = None
return (add_1,)""",
)
def test_aot_export_predispatch_map_2(self):
class M(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
def forward(self, x, y):
z = x.cos()
def f(x, y):
a = x.cos()
a.add_(5)
return a + y
return (z + control_flow.map(f, z, y).sum(),)
inps = [torch.randn(2, 2), torch.ones(2)]
gm, _ = aot_export_module(M(), inps, trace_joint=False, pre_dispatch=True)
self.assertExpectedInline(
str(gm.code).strip(),
"""\
def forward(self, arg0_1, arg1_1):
cos = torch.ops.aten.cos.default(arg0_1); arg0_1 = None
body_graph_0 = self.body_graph_0
map_impl = torch.ops.higher_order.map_impl(body_graph_0, [cos], [arg1_1]); body_graph_0 = arg1_1 = None
getitem = map_impl[0]; map_impl = None
sum_1 = torch.ops.aten.sum.default(getitem); getitem = None
add = torch.ops.aten.add.Tensor(cos, sum_1); cos = sum_1 = None
return (add,)""",
) # noqa: B950
self.assertExpectedInline(
str(gm.body_graph_0.code).strip(),
"""\
def forward(self, arg0_1, arg1_1):
cos = torch.ops.aten.cos.default(arg0_1); arg0_1 = None
add = torch.ops.aten.add.Tensor(cos, 5); cos = None
add_1 = torch.ops.aten.add.Tensor(add, arg1_1); add = arg1_1 = None
return [add_1]""",
)
@unittest.skipIf(IS_WINDOWS, "Windows isn't supported for this case")
@unittest.skipIf(
not torchdynamo.is_dynamo_supported(), "TorchDynamo is not supported"
)
def test_aot_export_predispatch_with_cond(self):
class M(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
def forward(self, x):
def true_fn(x):
y = x.sin()
z = torch.ops.aten.linear.default(y, torch.randn(2, 2))
z.add_(5)
return z.cos()
def false_fn(x):
z = x.cos()
z.add_(6)
return z.sin()
a = torch.cond(x.sum() > 4, true_fn, false_fn, [x])
return (a + 3, a + 4)
inp = torch.randn(2, 2)
gm, _ = aot_export_module(M(), [inp], trace_joint=False, pre_dispatch=True)
self.assertExpectedInline(
str(gm.code).strip(),
"""\
def forward(self, arg0_1):
sum_1 = torch.ops.aten.sum.default(arg0_1)
gt = torch.ops.aten.gt.Scalar(sum_1, 4); sum_1 = None
true_graph_0 = self.true_graph_0
false_graph_0 = self.false_graph_0
cond = torch.ops.higher_order.cond(gt, true_graph_0, false_graph_0, [arg0_1]); gt = true_graph_0 = false_graph_0 = arg0_1 = None
getitem = cond[0]; cond = None
add = torch.ops.aten.add.Tensor(getitem, 3)
add_1 = torch.ops.aten.add.Tensor(getitem, 4); getitem = None
return (add, add_1)""", # noqa: B950
)
self.assertExpectedInline(
str(gm.true_graph_0.code).strip(),
"""\
def forward(self, arg0_1):
sin = torch.ops.aten.sin.default(arg0_1); arg0_1 = None
randn = torch.ops.aten.randn.default([2, 2], device = device(type='cpu'), pin_memory = False)
linear = torch.ops.aten.linear.default(sin, randn); sin = randn = None
add = torch.ops.aten.add.Tensor(linear, 5); linear = None
cos = torch.ops.aten.cos.default(add); add = None
return (cos,)""",
)
def test_aot_export_predispatch_conv_and_bn(self):
class ConvBatchnorm(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
self.conv = torch.nn.Conv2d(1, 3, 1, 1)
self.bn = torch.nn.BatchNorm2d(3)
def forward(self, x):
x = self.conv(x)
x = self.bn(x)
return (x,)
mod = ConvBatchnorm()
mod.train()
inp = torch.randn(1, 1, 3, 3)
gm, _ = aot_export_module(mod, [inp], trace_joint=False, pre_dispatch=True)
self.assertExpectedInline(
str(gm.code).strip(),
"""\
def forward(self, arg0_1, arg1_1, arg2_1, arg3_1, arg4_1, arg5_1, arg6_1, arg7_1):
conv2d = torch.ops.aten.conv2d.default(arg7_1, arg0_1, arg1_1); arg7_1 = arg0_1 = arg1_1 = None
add = torch.ops.aten.add.Tensor(arg6_1, 1); arg6_1 = None
_native_batch_norm_legit_functional = torch.ops.aten._native_batch_norm_legit_functional.default(conv2d, arg2_1, arg3_1, arg4_1, arg5_1, True, 0.1, 1e-05); conv2d = arg2_1 = arg3_1 = arg4_1 = arg5_1 = None
getitem = _native_batch_norm_legit_functional[0]
getitem_3 = _native_batch_norm_legit_functional[3]
getitem_4 = _native_batch_norm_legit_functional[4]; _native_batch_norm_legit_functional = None
return (getitem_3, getitem_4, add, getitem)""", # noqa: B950
)
def test_aot_export_predispatch_reshape(self):
class Reshape(torch.nn.Module):
def forward(self, x):
y = x.reshape(4, 4)
return (y.sum(),)
mod = Reshape()
inp = torch.randn(2, 8)
gm, _ = aot_export_module(mod, [inp], trace_joint=False, pre_dispatch=True)
self.assertExpectedInline(
str(gm.code).strip(),
"""\
def forward(self, arg0_1):
view = torch.ops.aten.view.default(arg0_1, [4, 4]); arg0_1 = None
sum_1 = torch.ops.aten.sum.default(view); view = None
return (sum_1,)""",
) # noqa: B950
def test_aot_export_predispatch_contiguous(self):
class Cont(torch.nn.Module):
def forward(self, x):
y = torch.ops.aten.contiguous.default(x)
return (y.sum(),)
mod = Cont()
inp = torch.randn(2, 8)
gm, _ = aot_export_module(mod, [inp], trace_joint=False, pre_dispatch=True)
self.assertExpectedInline(
str(gm.code).strip(),
"""\
def forward(self, arg0_1):
sum_1 = torch.ops.aten.sum.default(arg0_1); arg0_1 = None
return (sum_1,)""",
) # noqa: B950
def test_aot_export_module_joint(self):
class ConvBatchnormRelu(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
self.conv = torch.nn.Conv2d(1, 3, 1, 1)
self.bn = torch.nn.BatchNorm2d(3)
def forward(self, x):
x = self.conv(x)
x = self.bn(x)
user_out = torch.nn.functional.relu(x)
loss = user_out.sum()
return loss, user_out.detach()
mod = ConvBatchnormRelu()
mod.train()
inp = torch.randn(1, 1, 3, 3)
o_ref = mod(inp)
fx_g, signature = aot_export_module(
mod, [inp], trace_joint=True, output_loss_index=0
)
# Some important characteristics of the exported graph below:
# 8 arguments: 2 params from conv, 2 params from batchnorm, 2 buffers from 1 batchnorm, 1 user input
# 9 outputs: 3 mutated buffers (from batchnorm), 2 user outputs and 4 gradients (since there were 4 parameters)
for node in fx_g.graph.nodes:
node.meta.pop("stack_trace", None)
self.assertExpectedInline(
fx_g.print_readable(print_output=False),
"""\
class <lambda>(torch.nn.Module):
def forward(self, arg0_1: "f32[3, 1, 1, 1]", arg1_1: "f32[3]", arg2_1: "f32[3]", arg3_1: "f32[3]", arg4_1: "f32[3]", arg5_1: "f32[3]", arg6_1: "i64[]", arg7_1: "f32[1, 1, 3, 3]"):
# No stacktrace found for following nodes
convolution: "f32[1, 3, 3, 3]" = torch.ops.aten.convolution.default(arg7_1, arg0_1, arg1_1, [1, 1], [0, 0], [1, 1], False, [0, 0], 1); arg1_1 = None
add: "i64[]" = torch.ops.aten.add.Tensor(arg6_1, 1); arg6_1 = None
_native_batch_norm_legit_functional = torch.ops.aten._native_batch_norm_legit_functional.default(convolution, arg2_1, arg3_1, arg4_1, arg5_1, True, 0.1, 1e-05); arg3_1 = arg4_1 = arg5_1 = None
getitem: "f32[1, 3, 3, 3]" = _native_batch_norm_legit_functional[0]
getitem_1: "f32[3]" = _native_batch_norm_legit_functional[1]
getitem_2: "f32[3]" = _native_batch_norm_legit_functional[2]
getitem_3: "f32[3]" = _native_batch_norm_legit_functional[3]
getitem_4: "f32[3]" = _native_batch_norm_legit_functional[4]; _native_batch_norm_legit_functional = None
relu: "f32[1, 3, 3, 3]" = torch.ops.aten.relu.default(getitem); getitem = None
detach: "f32[1, 3, 3, 3]" = torch.ops.aten.detach.default(relu); detach = None
detach_1: "f32[1, 3, 3, 3]" = torch.ops.aten.detach.default(relu)
detach_2: "f32[1, 3, 3, 3]" = torch.ops.aten.detach.default(detach_1); detach_1 = None
detach_3: "f32[1, 3, 3, 3]" = torch.ops.aten.detach.default(detach_2); detach_2 = None
detach_4: "f32[1, 3, 3, 3]" = torch.ops.aten.detach.default(detach_3); detach_3 = None
sum_1: "f32[]" = torch.ops.aten.sum.default(relu)
detach_5: "f32[1, 3, 3, 3]" = torch.ops.aten.detach.default(relu); relu = None
detach_6: "f32[1, 3, 3, 3]" = torch.ops.aten.detach.default(detach_5); detach_5 = None
detach_7: "f32[1, 3, 3, 3]" = torch.ops.aten.detach.default(detach_6); detach_6 = None
detach_8: "f32[1, 3, 3, 3]" = torch.ops.aten.detach.default(detach_7); detach_7 = None
detach_9: "f32[1, 3, 3, 3]" = torch.ops.aten.detach.default(detach_8); detach_8 = None
detach_10: "f32[1, 3, 3, 3]" = torch.ops.aten.detach.default(detach_9); detach_9 = None
ones_like: "f32[]" = torch.ops.aten.ones_like.default(sum_1, pin_memory = False, memory_format = torch.preserve_format)
expand: "f32[1, 3, 3, 3]" = torch.ops.aten.expand.default(ones_like, [1, 3, 3, 3]); ones_like = None
detach_11: "f32[1, 3, 3, 3]" = torch.ops.aten.detach.default(detach_4); detach_4 = None
detach_12: "f32[1, 3, 3, 3]" = torch.ops.aten.detach.default(detach_11); detach_11 = None
detach_13: "f32[1, 3, 3, 3]" = torch.ops.aten.detach.default(detach_12); detach_12 = None
detach_14: "f32[1, 3, 3, 3]" = torch.ops.aten.detach.default(detach_13); detach_13 = None
threshold_backward: "f32[1, 3, 3, 3]" = torch.ops.aten.threshold_backward.default(expand, detach_14, 0); expand = detach_14 = None
native_batch_norm_backward = torch.ops.aten.native_batch_norm_backward.default(threshold_backward, convolution, arg2_1, getitem_3, getitem_4, getitem_1, getitem_2, True, 1e-05, [True, True, True]); threshold_backward = convolution = arg2_1 = getitem_1 = getitem_2 = None
getitem_5: "f32[1, 3, 3, 3]" = native_batch_norm_backward[0]
getitem_6: "f32[3]" = native_batch_norm_backward[1]
getitem_7: "f32[3]" = native_batch_norm_backward[2]; native_batch_norm_backward = None
convolution_backward = torch.ops.aten.convolution_backward.default(getitem_5, arg7_1, arg0_1, [3], [1, 1], [0, 0], [1, 1], False, [0, 0], 1, [False, True, True]); getitem_5 = arg7_1 = arg0_1 = None
getitem_8 = convolution_backward[0]; getitem_8 = None
getitem_9: "f32[3, 1, 1, 1]" = convolution_backward[1]
getitem_10: "f32[3]" = convolution_backward[2]; convolution_backward = None
return (getitem_3, getitem_4, add, sum_1, detach_10, getitem_9, getitem_10, getitem_6, getitem_7)
""", # noqa: B950
)
self.assertExpectedInline(
str(signature.parameters),
"""['conv.weight', 'conv.bias', 'bn.weight', 'bn.bias']""",
)
self.assertExpectedInline(
str(signature.buffers),
"""['bn.running_mean', 'bn.running_var', 'bn.num_batches_tracked']""",
)
self.assertExpectedInline(str(signature.user_inputs), """['arg7_1']""")
self.assertExpectedInline(
str(signature.inputs_to_parameters),
"""{'arg0_1': 'conv.weight', 'arg1_1': 'conv.bias', 'arg2_1': 'bn.weight', 'arg3_1': 'bn.bias'}""",
) # noqa: B950
self.assertExpectedInline(
str(signature.inputs_to_buffers),
"""{'arg4_1': 'bn.running_mean', 'arg5_1': 'bn.running_var', 'arg6_1': 'bn.num_batches_tracked'}""",
) # noqa: B950
self.assertExpectedInline(
str(signature.buffers_to_mutate),
"""{'getitem_3': 'bn.running_mean', 'getitem_4': 'bn.running_var', 'add': 'bn.num_batches_tracked'}""",
) # noqa: B950
self.assertExpectedInline(
str(signature.backward_signature.gradients_to_parameters),
"""{'getitem_9': 'conv.weight', 'getitem_10': 'conv.bias', 'getitem_6': 'bn.weight', 'getitem_7': 'bn.bias'}""",
) # noqa: B950
self.assertExpectedInline(
str(signature.backward_signature.gradients_to_user_inputs), """{}"""
)
self.assertExpectedInline(
str(signature.backward_signature.loss_output), """getitem_3"""
)
# Also check the inference graph
# Main important thing here is that there are 5 total outputs: 3 total mutated buffers (from batchnorm), 2 user outputs.
fx_g_inference, signature_inference = aot_export_module(
mod, [inp], trace_joint=False
)
for node in fx_g_inference.graph.nodes:
node.meta.pop("stack_trace", None)
self.assertExpectedInline(
fx_g_inference.print_readable(print_output=False),
"""\
class <lambda>(torch.nn.Module):
def forward(self, arg0_1: "f32[3, 1, 1, 1]", arg1_1: "f32[3]", arg2_1: "f32[3]", arg3_1: "f32[3]", arg4_1: "f32[3]", arg5_1: "f32[3]", arg6_1: "i64[]", arg7_1: "f32[1, 1, 3, 3]"):
# No stacktrace found for following nodes
convolution: "f32[1, 3, 3, 3]" = torch.ops.aten.convolution.default(arg7_1, arg0_1, arg1_1, [1, 1], [0, 0], [1, 1], False, [0, 0], 1); arg7_1 = arg0_1 = arg1_1 = None
add: "i64[]" = torch.ops.aten.add.Tensor(arg6_1, 1); arg6_1 = None
_native_batch_norm_legit_functional = torch.ops.aten._native_batch_norm_legit_functional.default(convolution, arg2_1, arg3_1, arg4_1, arg5_1, True, 0.1, 1e-05); convolution = arg2_1 = arg3_1 = arg4_1 = arg5_1 = None
getitem: "f32[1, 3, 3, 3]" = _native_batch_norm_legit_functional[0]
getitem_3: "f32[3]" = _native_batch_norm_legit_functional[3]
getitem_4: "f32[3]" = _native_batch_norm_legit_functional[4]; _native_batch_norm_legit_functional = None
relu: "f32[1, 3, 3, 3]" = torch.ops.aten.relu.default(getitem); getitem = None
sum_1: "f32[]" = torch.ops.aten.sum.default(relu)
detach: "f32[1, 3, 3, 3]" = torch.ops.aten.detach.default(relu); relu = None
detach_1: "f32[1, 3, 3, 3]" = torch.ops.aten.detach.default(detach); detach = None
detach_2: "f32[1, 3, 3, 3]" = torch.ops.aten.detach.default(detach_1); detach_1 = None
return (getitem_3, getitem_4, add, sum_1, detach_2)
""", # noqa: B950
)
# Some important characteristics of the exported graph below:
# 8 arguments: 2 params from conv, 2 params from batchnorm, 2 buffers from 1 batchnorm, 1 user input
# 9 outputs: 2 mutated buffers (from batchnorm), 2 user outputs and 4 gradients (since there were 4 parameters)
def test_aot_export_simplified_basic(self):
def f(x, y):
return x * y, y * y.detach()
x = torch.randn(2, requires_grad=True)
y = torch.randn(2, requires_grad=True)
f_graph_fw = aot_export_joint_simple(f, [x, y], trace_joint=False)
out_ref = f(x, y)
# No calling convention changes necessary to invoke the traced graph
out_test = f_graph_fw(x, y)
self.assertEqual(out_ref, out_test)
# Now test the backward
x = torch.randn(2, requires_grad=True)
y = torch.randn(2, requires_grad=True)
x2 = x.detach().clone().requires_grad_(True)
y2 = y.detach().clone().requires_grad_(True)
x3 = x.detach().clone().requires_grad_(True)
y3 = y.detach().clone().requires_grad_(True)
f_graph_joint = aot_export_joint_simple(f, [x, y], trace_joint=True)
num_fw_outputs = 2
fw_g, bw_g = default_partition(
f_graph_joint, [x, y], num_fwd_outputs=num_fw_outputs
)
out_ref2 = f(x2, y2)
fw_outs = fw_g(x3, y3)
out_test2, activations = fw_outs[:num_fw_outputs], fw_outs[num_fw_outputs:]
self.assertEqual(out_ref2, out_test2)
# Test running the traced backward graph with a mocked-up grad_output
grad_outs = [torch.ones_like(x) for x in out_ref2]
grads_ref = torch.autograd.grad(out_ref2, [x2, y2], grad_outputs=grad_outs)
grads_test = bw_g(*activations, *grad_outs)
for g_ref, g_test in zip(grads_ref, grads_test):
self.assertEqual(g_ref, g_test)
def test_aot_export_metadata_mutation_banned(self):
def fn(p, x):
x.t_()
return (x * 2,)
mod = TestMod(fn)
inp = torch.randn(2, 4)
with self.assertRaisesRegex(
RuntimeError, "Found an input that received a metadata mutation"
):
aot_export_joint_simple(fn, [mod.p, inp], trace_joint=False)
aot_export_joint_simple(fn, [mod.p, inp], trace_joint=True)
aot_export_module(mod, [inp], trace_joint=False)
def test_aot_export_forward_mutation_no_buffer_mut(self):
class M(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
self.buffer1 = torch.nn.Buffer(torch.ones(6, 4))
def forward(self, x):
x.add_(4)
return (x.cos().sum() + self.buffer1.sum(),)
mod = M()
inp = torch.ones(6, 4)
gm, sig = aot_export_module(mod, [inp], trace_joint=False)
self.assertExpectedInline(
str(gm.code).strip(),
"""\
def forward(self, arg0_1, arg1_1):
add = torch.ops.aten.add.Tensor(arg1_1, 4); arg1_1 = None
cos = torch.ops.aten.cos.default(add)
sum_1 = torch.ops.aten.sum.default(cos); cos = None
sum_2 = torch.ops.aten.sum.default(arg0_1); arg0_1 = None
add_1 = torch.ops.aten.add.Tensor(sum_1, sum_2); sum_1 = sum_2 = None
return (add, add_1)""",
) # noqa: B950
self.assertEqual(sig.user_inputs_to_mutate, {"add": "arg1_1"})
def test_aot_export_forward_mutation_multiple_mut(self):
class M(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
self.buffer1 = torch.nn.Buffer(torch.ones(6, 4))
def forward(self, x, y):
y.add_(4)
self.buffer1.add_(5)
return (
x.cos().sum() + y.sin().sum(),
self.buffer1.sum(),
)
mod = M()
inp = [torch.ones(6, 4), torch.zeros(6, 4)]
gm, sig = aot_export_module(mod, inp, trace_joint=False)
self.assertExpectedInline(
str(gm.code).strip(),
"""\
def forward(self, arg0_1, arg1_1, arg2_1):
add = torch.ops.aten.add.Tensor(arg2_1, 4); arg2_1 = None
add_1 = torch.ops.aten.add.Tensor(arg0_1, 5); arg0_1 = None
cos = torch.ops.aten.cos.default(arg1_1); arg1_1 = None
sum_1 = torch.ops.aten.sum.default(cos); cos = None
sin = torch.ops.aten.sin.default(add)
sum_2 = torch.ops.aten.sum.default(sin); sin = None
add_2 = torch.ops.aten.add.Tensor(sum_1, sum_2); sum_1 = sum_2 = None
sum_3 = torch.ops.aten.sum.default(add_1)
return (add_1, add, add_2, sum_3)""",
) # noqa: B950
self.assertEqual(sig.user_inputs_to_mutate, {"add": "arg2_1"})
self.assertEqual(sig.buffers_to_mutate, {"add_1": "buffer1"})
def test_aot_export_input_mutation_on_input_requiring_grad_banned(self):
class M(torch.nn.Module):
def forward(self, x):
x.add_(4)
return (x,)
mod = M()
inp = torch.randn(2, requires_grad=True)
with self.assertRaisesRegex(
RuntimeError,
"Found a graph input that requires gradients, and received a mutation",
):
aot_export_module(mod, [inp], trace_joint=False)
def test_aot_export_input_mutation_on_parameter_banned(self):
def fn(p, x):
p.mul_(2)
return (p + x,)
mod = TestMod(fn)
inp = torch.randn(2)
with self.assertRaisesRegex(
RuntimeError,
"Found a graph input that requires gradients, and received a mutation",
):
aot_export_joint_simple(fn, [mod.p, inp], trace_joint=False)
aot_export_joint_simple(fn, [mod.p, inp], trace_joint=True)
aot_export_module(mod, [inp], trace_joint=False)
def test_aot_export_synthetic_bases_banned(self):
def fn(p, x, y):
x.mul_(2)
return (x + y,)
mod = TestMod(fn)
inp = torch.randn(2)
inp2 = inp.view(-1)
with self.assertRaisesRegex(
RuntimeError, "Encountered aliased inputs that are mutated"
):
aot_export_joint_simple(fn, [mod.p, inp, inp2], trace_joint=False)
aot_export_joint_simple(fn, [mod.p, inp, inp2], trace_joint=True)
aot_export_module(mod, [inp, inp2], trace_joint=False)
def test_aot_export_input_dupes_banned(self):
def fn(p, x, y):
x.mul_(2)
return (x + y,)
mod = TestMod(fn)
inp = torch.randn(2)
with self.assertRaisesRegex(
RuntimeError, "Encountered duplicated inputs that are mutated in the graph"
):
aot_export_joint_simple(fn, [mod.p, inp, inp], trace_joint=False)
aot_export_joint_simple(fn, [mod.p, inp, inp], trace_joint=True)
aot_export_module(mod, [inp, inp], trace_joint=False)
def test_aot_export_multiple_outputs_require_grad_banned(self):
def fn(p, x):
out = p * x
return out, out.sum()
mod = TestMod(fn)
inp = torch.randn(2)
with self.assertRaisesRegex(
RuntimeError,
"Found an output of the forward that requires gradients, that was not",
):
aot_export_module(mod, [inp], trace_joint=True, output_loss_index=1)
@unittest.skipIf(IS_WINDOWS, "Windows isn't supported for this case")
@unittest.skipIf(
not torch._dynamo.is_dynamo_supported(), "Cond needs dynamo to run"
)
def test_aot_export_with_torch_cond(self):
class M(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
def forward(self, x):
def true_fn(x):
y = x + 4
y.add_(5)
return x.cos()
def false_fn(x):
y = x + 5
y.add_(6)
return x.sin()
a = torch.cond(x.sum() > 4, true_fn, false_fn, [x])
return (a + 3, a + 4)
inp = torch.randn(3, 4)
gm, _ = aot_export_module(M(), (inp,), trace_joint=False)
self.assertExpectedInline(
gm.code.strip(),
"""\
def forward(self, arg0_1):
sum_1 = torch.ops.aten.sum.default(arg0_1)
gt = torch.ops.aten.gt.Scalar(sum_1, 4); sum_1 = None
true_graph_0 = self.true_graph_0
false_graph_0 = self.false_graph_0
cond = torch.ops.higher_order.cond(gt, true_graph_0, false_graph_0, [arg0_1]); gt = true_graph_0 = false_graph_0 = arg0_1 = None
getitem = cond[0]; cond = None
add = torch.ops.aten.add.Tensor(getitem, 3)
add_1 = torch.ops.aten.add.Tensor(getitem, 4); getitem = None
return (add, add_1)""", # noqa: B950
)
self.assertExpectedInline(
gm.true_graph_0.code.strip(),
"""\
def forward(self, arg0_1):
add = torch.ops.aten.add.Tensor(arg0_1, 4)
add_1 = torch.ops.aten.add.Tensor(add, 5); add = add_1 = None
cos = torch.ops.aten.cos.default(arg0_1); arg0_1 = None
return (cos,)""",
)
self.assertExpectedInline(
gm.false_graph_0.code.strip(),
"""\
def forward(self, arg0_1):
add = torch.ops.aten.add.Tensor(arg0_1, 5)
add_1 = torch.ops.aten.add.Tensor(add, 6); add = add_1 = None
sin = torch.ops.aten.sin.default(arg0_1); arg0_1 = None
return (sin,)""",
)
def test_aot_export_simplified_pytrees_banned(self):
def fn(inps):
return (inps[0] + inps[1],)
inp1 = torch.randn(2)
inp2 = torch.randn(2)
inps = [inp1, inp2]
with self.assertRaisesRegex(
RuntimeError,
"aot_export_joint_simple requires individual inputs not to be pytrees",
):
aot_export_joint_simple(fn, [inps], trace_joint=False)
aot_export_joint_simple(fn, [inps], trace_joint=True)
def test_aot_export_functionalized_rng_banned(self):
def fn(p, x):
return (p + x,)
mod = TestMod(fn)
inp = torch.randn(2)
with patch(
"functorch.compile.config.functionalize_rng_ops", True
), self.assertRaisesRegex(
RuntimeError,
"Functionalized RNG is not currently supported in the aot_export",
):
aot_export_joint_simple(fn, [mod.p, inp], trace_joint=False)
aot_export_joint_simple(fn, [mod.p, inp], trace_joint=True)
aot_export_module(mod, [inp], trace_joint=False)
def test_aot_export_unbacked_arg(self):
class M(torch.nn.Module):
def forward(self):
full = torch.full((), 11)
i0 = full.item()
return (torch.full((i0,), 0),)
gm, _ = aot_export_module(
mod=M(), args=(), trace_joint=False, dynamic_shapes=True
)
self.assertExpectedInline(
gm.code.strip(),
"""\
def forward(self):
full = torch.ops.aten.full.default([], 11, device = device(type='cpu'), pin_memory = False)
_local_scalar_dense = torch.ops.aten._local_scalar_dense.default(full); full = None
full_1 = torch.ops.aten.full.default([_local_scalar_dense], 0, device = device(type='cpu'), pin_memory = False); _local_scalar_dense = None
return (full_1,)""", # noqa: B950
)
class TestPartitioning(AOTTestCase):
@unittest.skipIf(not USE_NETWORKX, "networkx not available")
def test_recompute_partitioning(self):
def fn(a, b):
return torch.sin(torch.sin(a)) + b
# Reference calculation
ref_a = torch.rand(10, 10, requires_grad=True)
ref_b = torch.rand(10, 10, requires_grad=True)
ref = fn(ref_a, ref_b)
ref.sum().backward()
# Compiled function calculation
res_a = ref_a.detach().clone().requires_grad_(True)
res_b = ref_b.detach().clone().requires_grad_(True)
def compile_fn(x, _):
return x
compiled_fn = compiled_function(
fn, compile_fn, compile_fn, min_cut_rematerialization_partition
)
res = compiled_fn(res_a, res_b)
res.sum().backward()
assert torch.allclose(ref, res, atol=1e-3, rtol=1e-3)
assert torch.allclose(ref_a.grad, res_a.grad, atol=1e-3, rtol=1e-3)
assert torch.allclose(ref_b.grad, res_b.grad, atol=1e-3, rtol=1e-3)
def test_meta_tensor_inplace_op(self):
# Following module results in inplace ops while tracing. The test checks
# that the meta tensor information is stored for inplace ops.
class MockModule(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
self.weight = torch.nn.Parameter(
torch.randn(3072, 768, requires_grad=True)
)
self.bias = torch.nn.Parameter(torch.randn(3072, requires_grad=True))
def forward(self, add_4):
linear_4 = torch.nn.functional.linear(
add_4, self.weight, bias=self.bias
)
gelu = torch.nn.functional.gelu(linear_4)
return gelu
def check_meta_tensor(fx_g, _):
for node in fx_g.graph.nodes:
if node.op != "output":
assert "tensor_meta" in node.meta
return fx_g
inp0 = torch.randn(16, 128, 768, requires_grad=True)
inputs = [
inp0,
]
mod = MockModule().to(device="cpu")
aot_mod = aot_module(mod, fw_compiler=check_meta_tensor)
aot_mod(*inputs)
def test_default_partitioner_getitem(self):
mod = nn.LayerNorm([10])
def f(x, mod_weight, mod_bias):
return torch.nn.functional.layer_norm(
x, [10], mod_weight, mod_bias, eps=1e-6
)
fw_graph, bw_graph = get_fw_bw_graph(
f,
[torch.randn(3, 10, requires_grad=True), mod.weight, mod.bias],
partitioner=default_partition,
)
self.assertEqual(get_num_ins_outs(fw_graph), (3, 6))
self.assertEqual(get_num_ins_outs(bw_graph), (6, 3))
@unittest.skipIf(not USE_NETWORKX, "networkx not available")
def test_min_cut_partitioner_save_shape(self):
def f(x):
s = x.sum(dim=1)
return s
inp = [torch.ones([10, 10], requires_grad=True)]
fw_graph, bw_graph = get_fw_bw_graph(f, inp, dynamic=True)
_, fw_output = get_ins_outs(fw_graph)
self.assertEqual(get_num_ins_outs(fw_graph), (1, 3))
self.assertEqual(get_num_ins_outs(bw_graph), (3, 1))
self.assertEqual(str(fw_output[0]), "sum_1")
# make sure we don't do the suboptimal thing of saving the bigger primals input to sum,
# rather than saving the sizes of the primals input for use in backward expand
self.assertEqual(str(fw_output[1]), "sym_size_int")
self.assertEqual(str(fw_output[2]), "sym_size_int_1")
inp = [
torch.randn(10, requires_grad=True),
torch.randn((3, 10), requires_grad=True),
torch.randn((2, 10), requires_grad=True),
]
def f(a, b, c):
# tried to test what happens if we save a size tuple in the graph;
# turns out we never will due to how we trace, but this is probably
# still a good test case for various size manipulations
sb = torch.ops.aten.sym_size(b)
sc = c.size()
x = sb[0] + sc[0]
a_sz = (x, a.size(0))
return torch.cat([a.expand(a_sz), b, c])
fw_graph, bw_graph = get_fw_bw_graph(f, inp, dynamic=True)
self.assertEqual(get_num_ins_outs(fw_graph), (3, 4))
self.assertEqual(get_num_ins_outs(bw_graph), (4, 3))
_, outs = get_ins_outs(fw_graph)
self.assertTrue(all(is_sym_node(n) for n in outs[1:]))
def test_default_partitioner_output_tensor_shape_tensor(self):
inp = [
torch.randn(10, requires_grad=True),
torch.randn((3, 10), requires_grad=True),
torch.randn((2, 10), requires_grad=True),
torch.randn((10, 1), requires_grad=True),
]
def f(a, b, c, d):
# Try to force symints intermixed with outputs in the function's returns
sb = b.size()
sc = c.size()
x = sb[0] + sc[0]
a_sz = (x, a.size(0))
cat = torch.cat([a.expand(a_sz), b, c])
mm = torch.mm(cat, d)
mm2 = torch.mm(
mm, a.view(mm.size(1), a.size(0))
) # this saves 4 new ints for backward. why?
# and what do i have to do to make it save a tensor for backward?
return cat, sb, c, mm2
fw_graph_cell = [None]
bw_graph_cell = [None]
compiled_outs = aot_function(
f,
fw_compiler=partial(extract_graph, graph_cell=fw_graph_cell),
bw_compiler=partial(extract_graph, graph_cell=bw_graph_cell),
partition_fn=default_partition,
decompositions=default_decompositions,
dynamic=True,
)(*inp)
fw_graph = fw_graph_cell[0]
(compiled_outs[0].sum() + compiled_outs[2].sum()).backward()
bw_graph = bw_graph_cell[0]
# in the fwd graph, 13 outs because:
# - 5 original outputs (sb is a tuple, gets expanded to 2 symints)
# - 8 saved outputs for backward: 5 tensors, 3 symints
self.assertEqual(get_num_ins_outs(fw_graph), (4, 13))
# in the bwd graph, 10 inputs (grad outs) because:
# - The fwd graph had 13 outputs
# - 1 was a view of an input, which gets regenerated outside of the graph
# and doesn't participate in the backward
# - 2 user outs were symints (b.size()), which don't get tangents in the backward
self.assertEqual(get_num_ins_outs(bw_graph), (10, 4))
_, fw_graph_out_nodes = get_ins_outs(fw_graph)
self.assertEqual(
# fw outputs include b.size() which expands to 2 symints,
#
# TODO(whc)- are the saved-tensors/saved-symints correct here?
# i just made the test pass based on what default partition did
# Of the 5 original forward outputs, the 4th (c) is an input,
# which won't show up in the compiled forward graph
[False, True, True, False, False] + [False] * 4 + [True] * 4,
[is_sym_node(n) for n in fw_graph_out_nodes],
)
real_outs = f(*inp)
self.assertEqual(compiled_outs, real_outs)
self.assertTrue(isinstance(real_outs[1], torch.Size))
# TODO(whc) we should learn to return torch.Sizes
self.assertFalse(isinstance(compiled_outs[1], torch.Size))
@unittest.skipIf(not USE_NETWORKX, "networkx not available")
def test_min_cut_partitioner_output_tensor_shape_tensor(self):
inp = [
torch.randn(10, requires_grad=True),
torch.randn((3, 10), requires_grad=True),
torch.randn((2, 10), requires_grad=True),
torch.randn((10, 1), requires_grad=True),
]
def f(a, b, c, d):
# Try to force symints intermixed with outputs in the function's returns
sb = b.size()
sc = c.size()
x = sb[0] + sc[0]
a_sz = (x, a.size(0))
cat = torch.cat([a.expand(a_sz), b, c])
mm = torch.mm(cat, d)
mm2 = torch.mm(
mm, a.view(mm.size(1), a.size(0))
) # this saves 4 new ints for backward. why?
# and what do i have to do to make it save a tensor for backward?
return cat, sb, c, mm2
fw_graph_cell = [None]
bw_graph_cell = [None]
compiled_outs = aot_function(
f,
fw_compiler=partial(extract_graph, graph_cell=fw_graph_cell),
bw_compiler=partial(extract_graph, graph_cell=bw_graph_cell),
partition_fn=min_cut_rematerialization_partition,
decompositions=default_decompositions,
dynamic=True,
)(*inp)
fw_graph = fw_graph_cell[0]
(compiled_outs[0].sum() + compiled_outs[2].sum()).backward()
bw_graph = bw_graph_cell[0]
self.assertEqual(get_num_ins_outs(fw_graph), (4, 12))
self.assertEqual(get_num_ins_outs(bw_graph), (9, 4))
_, fw_graph_out_nodes = get_ins_outs(fw_graph)
self.assertEqual(
# fw outputs include b.size() which expands to 2 symints,
# then 4 tensors (transposes of matricies used for mm) are saved
# finally 3 symints are saved
[False, True, True, False, False] + [False] * 4 + [True] * 3,
[is_sym_node(n) for n in fw_graph_out_nodes],
)
real_outs = f(*inp)
self.assertEqual(compiled_outs, real_outs)
self.assertTrue(isinstance(real_outs[1], torch.Size))
# TODO(whc) we should learn to return torch.Sizes
self.assertFalse(isinstance(compiled_outs[1], torch.Size))
@unittest.skipIf(not USE_NETWORKX, "networkx not available")
def test_min_cut_partitioner(self):
def f(x):
return x.cos().cos().cos()
fw_graph, bw_graph = get_fw_bw_graph(f, [torch.randn(3, requires_grad=True)])
self.assertEqual(get_num_ins_outs(fw_graph), (1, 2))
self.assertEqual(get_num_ins_outs(bw_graph), (2, 1))
def f(a, b, c, d):
x = a + b + c + d
return x.cos().cos()
fw_graph, bw_graph = get_fw_bw_graph(
f, [torch.randn(3, requires_grad=True) for _ in range(4)]
)
self.assertEqual(get_num_ins_outs(fw_graph), (4, 2))
self.assertEqual(get_num_ins_outs(bw_graph), (2, 4))
def test_contiguous(self):
# The test simulates the condition where transpose followed by view
# happens in the backward pass.
# https://discuss.pytorch.org/t/error-on-transpose-and-view/434
def f(x):
return x.view(2, 3).t()
inp = torch.randn(6, requires_grad=True)
out = aot_function(f, nop)(inp)
torch.autograd.grad(out, inp, torch.randn(3, 2))
def test_preserve_random(self):
def fn(x):
return torch.nn.functional.dropout(x, 0.5) + x
x = torch.randn(4)
torch.manual_seed(0)
ref = fn(x)
torch.manual_seed(0)
aot_fn = aot_function(fn, nop)
res = aot_fn(x)
assert torch.allclose(ref, res)
# https://github.com/pytorch/pytorch/issues/110666
def test_generate_gives_inference_graph(self):
# We expect this to give an inference graph
def generate(x):
with torch.no_grad():
return torch.mul(x, x)
inference_graph_cell = [None]
inference_compiler = make_boxed_compiler(
partial(extract_graph, graph_cell=inference_graph_cell)
)
aot_fn = aot_function(generate, nop, inference_compiler=inference_compiler)
# Even though x requires grad, we should still get an inference graph
x = torch.randn(4, requires_grad=True)
res = aot_fn(x)
self.assertTrue(inference_graph_cell[0] is not None)
@unittest.skipIf(not torch.cuda.is_available(), "CUDA is unavailable")
@unittest.skipIf(not USE_TORCHVISION, "test requires torchvision")
def test_autocast(self):
mod = torchvision.models.resnet18().cuda()
mod.train()
x = torch.randn(16, 3, 32, 32, device="cuda")
aot_mod = memory_efficient_fusion(mod)
# Ensure that AOT Autograd works with AMP
with torch.cuda.amp.autocast(True):
res = aot_mod(x)
res.sum().backward()
class TestAOTDispatch(AOTTestCase):
# Tests to add cases for (non-exhaustive list, mostly for my notes):
# - subclass / mode introduced in the middle of the compiled fn
# - various input mutation / intermediate base tests
# - input mutation that changes a tensor into a subclass
# - metadata mutation? (TBD)
# - guard tests (fw guards *and* bw guards)
# - subclass test involving _indices_of_inps_to_detach
def test_aot_dispatch_simple(self):
# a is a subclass, b is not
def f(a, b):
aa = torch.mul(a, 6)
bb = torch.div(b, 2)
return aa + bb
a1_ref = torch.ones(3, 3, requires_grad=True)
a2_ref = torch.ones(3, 3, requires_grad=True)
a_ref = TwoTensor(a1_ref, a2_ref)
b_ref = torch.ones(3, 3, requires_grad=True)
a1_test = a1_ref.detach().clone().requires_grad_(True)
a2_test = a2_ref.detach().clone().requires_grad_(True)
a_test = TwoTensor(a1_test, a2_test)
b_test = b_ref.detach().clone().requires_grad_(True)
fw_graph_cell = [None]
bw_graph_cell = [None]
compiled_f = aot_function(
f,
fw_compiler=partial(extract_graph, graph_cell=fw_graph_cell),
bw_compiler=partial(extract_graph, graph_cell=bw_graph_cell),
partition_fn=min_cut_rematerialization_partition,
)
out_ref = f(a_ref, b_ref)
out_test = compiled_f(a_test, b_test)
# Output is a TwoTensor (check both inner tensors)
self.assertEqual(out_ref.a, out_test.a)
self.assertEqual(out_ref.b, out_test.b)
out_ref.sum().backward()
out_test.sum().backward()
# Both grad_inputs are TwoTensor
self.assertEqual(a_ref.grad.a, a_test.grad.a)
self.assertEqual(a_ref.grad.b, a_test.grad.b)
self.assertEqual(b_ref.grad.a, b_test.grad.a)
self.assertEqual(b_ref.grad.b, b_test.grad.b)
# Important pieces of the graph:
# - mul() and div() show up twice, because we called them on a TwoTensor
# - add() shows up once, because we called it on a plain Tensor
# - The user forward() fn returns 1 output (the result of add),
# while the graph itself returns two outputs (add, add_1)
# - add, add_1 correspond to the two inner dense tensors that will be wrapped
# - into a single TwoTensor output.
self.assertExpectedInline(
fw_graph_cell[0].code.strip(),
"""\
def forward(self, primals_1, primals_2, primals_3):
mul = torch.ops.aten.mul.Tensor(primals_1, 6); primals_1 = None
mul_1 = torch.ops.aten.mul.Tensor(primals_2, 6); primals_2 = None
div = torch.ops.aten.div.Tensor(primals_3, 2); primals_3 = None
add = torch.ops.aten.add.Tensor(mul, div); mul = None
add_1 = torch.ops.aten.add.Tensor(mul_1, div); mul_1 = div = None
return (add, add_1)""",
)
# Important pieces of the graph:
# - 4 total dense outputs.
# This corresponds to the fact that each user fwd inpt (a, b)
# will get a gradient that is a TwoTensor subclass,
# so (mul_2, mul_3) will be wrapped into a.grad
# and (div_1, div_2) will be wrapped into b.grad
# - 4 total dense outputs,
self.assertExpectedInline(
bw_graph_cell[0].code.strip(),
"""\
def forward(self, tangents_1, tangents_2):
div_1 = torch.ops.aten.div.Tensor(tangents_1, 2)
div_2 = torch.ops.aten.div.Tensor(tangents_2, 2)
mul_2 = torch.ops.aten.mul.Tensor(tangents_1, 6); tangents_1 = None
mul_3 = torch.ops.aten.mul.Tensor(tangents_2, 6); tangents_2 = None
return (mul_2, mul_3, div_1, div_2)""",
)
def test_aot_dispatch_inference(self):
# a is a subclass, b is not
def f(a, b):
aa = torch.mul(a, 6)
bb = torch.div(b, 2)
return aa + bb
a1_ref = torch.ones(3, 3)
a2_ref = torch.ones(3, 3)
a_ref = TwoTensor(a1_ref, a2_ref)
b_ref = torch.ones(3, 3)
a1_test = a1_ref.clone()
a2_test = a2_ref.clone()
a_test = TwoTensor(a1_test, a2_test)
b_test = b_ref.clone()
compiled_f = aot_function(
f,
fw_compiler=nop,
bw_compiler=nop,
partition_fn=min_cut_rematerialization_partition,
)
out_ref = f(a_ref, b_ref)
out_test = compiled_f(a_test, b_test)
# Output is a TwoTensor (check both inner tensors)
self.assertEqual(out_ref.a, out_test.a)
self.assertEqual(out_ref.b, out_test.b)
@skipIfTorchDynamo()
def test_aot_dispatch_incorrect_backward(self):
# a is a subclass, b is not
def f(a, b):
aa = torch.mul(a, 2)
bb = torch.add(b, 3)
out_subclass = torch.div(aa, bb)
out_reg = torch.add(b, b)
# When creating the joint, we assume that the second grad_out
# is not a subclass.
# In the below test case though, we end up being wrong.
# This would require re-tracing and recompiling the backward.
return out_subclass, out_reg
a1_ref = torch.ones(3, 3, requires_grad=True)
a2_ref = torch.ones(3, 3, requires_grad=True)
a_ref = TwoTensor(a1_ref, a2_ref)
b_ref = torch.ones(3, 3, requires_grad=True)
a1_test = a1_ref.detach().clone().requires_grad_(True)
a2_test = a2_ref.detach().clone().requires_grad_(True)
a_test = TwoTensor(a1_test, a2_test)
b_test = b_ref.detach().clone().requires_grad_(True)
compiled_f = aot_function(
f,
fw_compiler=nop,
bw_compiler=nop,
partition_fn=min_cut_rematerialization_partition,
)
out_ref = f(a_ref, b_ref)
out_test = compiled_f(a_test, b_test)
# First out is a TwoTensor, second is an ordinary tensor
self.assertEqual(out_ref[0].a, out_test[0].a)
self.assertEqual(out_ref[0].b, out_test[0].b)
self.assertEqual(out_ref[1], out_test[1])
# We compiled our graph assuming type(grad_out[1]) == torch.Tensor,
# but we were wrong: in the below tests, it is a subclass.
# This will eventually require a repartition + recompile
with self.assertRaisesRegex(
RuntimeError,
"""
During the backward, we encountered a tensor subclass where we guessed its
metadata incorrectly.
""", # noqa: F541
):
(out_test[0] + out_test[1]).sum().backward()
def test_aot_dispatch_output_alias(self):
# a is a tensor, b is a TwoTensor
def f(a, b):
return b.view(b.shape), a * b
b1_ref = torch.ones(3, 3, requires_grad=True)
b2_ref = torch.ones(3, 3, requires_grad=True)
b_ref = TwoTensor(b1_ref, b2_ref)
a_ref = torch.ones(3, 3, requires_grad=True)
b1_test = b1_ref.detach().clone().requires_grad_(True)
b2_test = b2_ref.detach().clone().requires_grad_(True)
b_test = TwoTensor(b1_test, b2_test)
a_test = a_ref.detach().clone().requires_grad_(True)
compiled_f = aot_function(
f,
fw_compiler=nop,
bw_compiler=nop,
partition_fn=min_cut_rematerialization_partition,
)
out_ref1, out_ref2 = f(a_ref, b_ref)
out_test1, out_test2 = compiled_f(a_test, b_test)
self.assertEqual(out_ref1, out_test1)
self.assertEqual(out_ref2.a, out_test2.a)
self.assertEqual(out_ref2.b, out_test2.b)
(out_ref1 + out_ref2).sum().backward()
(out_test1 + out_test2).sum().backward()
# Both grad_inputs are TwoTensor
self.assertEqual(a_ref.grad.a, a_test.grad.a)
self.assertEqual(a_ref.grad.b, a_test.grad.b)
self.assertEqual(b_ref.grad.a, b_test.grad.a)
self.assertEqual(b_ref.grad.b, b_test.grad.b)
@torch._functorch.config.patch(
{
"disable_guess_zero_tangent_for_mutated_input_subclass": True,
}
)
def test_aot_dispatch_input_mutation(self):
def f(a, b):
a.mul_(2)
b.mul_(3)
return a + b
b1_ref = torch.ones(3, 3, requires_grad=True)
b2_ref = torch.ones(3, 3, requires_grad=True)
b_ref_base = TwoTensor(b1_ref, b2_ref)
a_ref_base = torch.ones(3, 3, requires_grad=True)
b_ref = b_ref_base + 1
a_ref = a_ref_base + 1
b1_test = b1_ref.detach().clone().requires_grad_(True)
b2_test = b2_ref.detach().clone().requires_grad_(True)
b_test_base = TwoTensor(b1_test, b2_test)
a_test_base = a_ref_base.detach().clone().requires_grad_(True)
b_test = b_test_base + 1
a_test = a_test_base + 1
compiled_f = aot_function(
f,
fw_compiler=nop,
bw_compiler=nop,
partition_fn=min_cut_rematerialization_partition,
)
out_ref = f(a_ref, b_ref)
out_test = compiled_f(a_test, b_test)
self.assertEqual(out_ref.a, out_test.a)
self.assertEqual(out_ref.b, out_test.b)
# confirm input mutations worked
self.assertEqual(a_test, a_ref)
self.assertEqual(b_test.a, b_ref.a)
self.assertEqual(b_test.b, b_ref.b)
# NOTE: we need to use b in our gradient compute. Otherwise we will need to recompile teh backward.
(b_ref * out_ref).sum().backward()
(b_test * out_test).sum().backward()
# Both grad_inputs are TwoTensor
self.assertEqual(a_ref_base.grad.a, a_test_base.grad.a)
self.assertEqual(a_ref_base.grad.b, a_test_base.grad.b)
self.assertEqual(b_ref_base.grad.a, b_test_base.grad.a)
self.assertEqual(b_ref_base.grad.b, b_test_base.grad.b)
# NB: Metadata mutation for subclasses is currently broken and disabled
# See https://github.com/pytorch/pytorch/issues/114975
@unittest.expectedFailure
def test_aot_dispatch_input_metadata_mutation(self):
def f(a, b):
a.t_()
b.unsqueeze_(0)
return a + b
b1_ref = torch.arange(9, requires_grad=True, dtype=torch.float32).reshape(3, 3)
b2_ref = torch.arange(9, requires_grad=True, dtype=torch.float32).reshape(3, 3)
b_ref_base = TwoTensor(b1_ref, b2_ref)
a_ref_base = (
torch.arange(9, dtype=torch.float32)
.reshape(3, 3)
.detach()
.requires_grad_(True)
)
b_ref = b_ref_base + 1
a_ref = a_ref_base + 1
b1_test = b1_ref.detach().clone().requires_grad_(True)
b2_test = b2_ref.detach().clone().requires_grad_(True)
b_test_base = TwoTensor(b1_test, b2_test)
a_test_base = a_ref_base.detach().clone().requires_grad_(True)
b_test = b_test_base + 1
a_test = a_test_base + 1
compiled_f = aot_function(
f,
fw_compiler=nop,
bw_compiler=nop,
partition_fn=min_cut_rematerialization_partition,
)
out_ref = f(a_ref, b_ref)
out_test = compiled_f(a_test, b_test)
self.assertEqual(out_ref.a, out_test.a)
self.assertEqual(out_ref.b, out_test.b)
# confirm input mutations worked
self.assertEqual(a_test, a_ref)
self.assertEqual(b_test.a, b_ref.a)
self.assertEqual(b_test.b, b_ref.b)
# NOTE: we need to use b in our gradient compute. Otherwise we will need to recompile the backward.
(b_ref * out_ref).sum().backward()
(b_test * out_test).sum().backward()
# Both grad_inputs are TwoTensor
self.assertEqual(a_ref_base.grad.a, a_test_base.grad.a)
self.assertEqual(a_ref_base.grad.b, a_test_base.grad.b)
self.assertEqual(b_ref_base.grad.a, b_test_base.grad.a)
self.assertEqual(b_ref_base.grad.b, b_test_base.grad.b)
# NB: Metadata mutation for subclasses is currently broken and disabled
# See https://github.com/pytorch/pytorch/issues/114975
@unittest.expectedFailure
def test_aot_dispatch_input_data_and_metadata_mutation(self):
def f(a, b):
a.t_()
b.unsqueeze_(0)
a.mul_(2)
b.mul_(3)
return a + b
b1_ref = torch.arange(9, requires_grad=True, dtype=torch.float32).reshape(3, 3)
b2_ref = torch.arange(9, requires_grad=True, dtype=torch.float32).reshape(3, 3)
b_ref_base = TwoTensor(b1_ref, b2_ref)
a_ref_base = (
torch.arange(9, dtype=torch.float32)
.reshape(3, 3)
.detach()
.requires_grad_(True)
)
b_ref = b_ref_base + 1
a_ref = a_ref_base + 1
b1_test = b1_ref.detach().clone().requires_grad_(True)
b2_test = b2_ref.detach().clone().requires_grad_(True)
b_test_base = TwoTensor(b1_test, b2_test)
a_test_base = a_ref_base.detach().clone().requires_grad_(True)
b_test = b_test_base + 1
a_test = a_test_base + 1
compiled_f = aot_function(
f,
fw_compiler=nop,
bw_compiler=nop,
partition_fn=min_cut_rematerialization_partition,
)
out_ref = f(a_ref, b_ref)
out_test = compiled_f(a_test, b_test)
self.assertEqual(out_ref.a, out_test.a)
self.assertEqual(out_ref.b, out_test.b)
# confirm input mutations worked
self.assertEqual(a_test, a_ref)
self.assertEqual(b_test.a, b_ref.a)
self.assertEqual(b_test.b, b_ref.b)
# NOTE: we need to use b in our gradient compute. Otherwise we will need to recompile the backward.
(b_ref * out_ref).sum().backward()
(b_test * out_test).sum().backward()
# Both grad_inputs are TwoTensor
self.assertEqual(a_ref_base.grad.a, a_test_base.grad.a)
self.assertEqual(a_ref_base.grad.b, a_test_base.grad.b)
self.assertEqual(b_ref_base.grad.a, b_test_base.grad.a)
self.assertEqual(b_ref_base.grad.b, b_test_base.grad.b)
@torch._functorch.config.patch(
{
"disable_guess_zero_tangent_for_mutated_input_subclass": True,
}
)
def test_aot_dispatch_input_mutation_and_output_alias(self):
def f(a, b):
a.mul_(2)
b.mul_(3)
return b.view(b.shape), a + b
b1_ref = torch.arange(9, requires_grad=True, dtype=torch.float32).reshape(3, 3)
b2_ref = torch.arange(9, requires_grad=True, dtype=torch.float32).reshape(3, 3)
b_ref_base = TwoTensor(b1_ref, b2_ref)
a_ref_base = (
torch.arange(9, dtype=torch.float32)
.reshape(3, 3)
.detach()
.requires_grad_(True)
)
b_ref = b_ref_base + 1
a_ref = a_ref_base + 1
b1_test = b1_ref.detach().clone().requires_grad_(True)
b2_test = b2_ref.detach().clone().requires_grad_(True)
b_test_base = TwoTensor(b1_test, b2_test)
a_test_base = a_ref_base.detach().clone().requires_grad_(True)
b_test = b_test_base + 1
a_test = a_test_base + 1
compiled_f = aot_function(
f,
fw_compiler=nop,
bw_compiler=nop,
partition_fn=min_cut_rematerialization_partition,
)
out_ref1, out_ref2 = f(a_ref, b_ref)
out_test1, out_test2 = compiled_f(a_test, b_test)
self.assertEqual(out_ref1.a, out_test1.a)
self.assertEqual(out_ref1.b, out_test1.b)
self.assertEqual(out_ref2.a, out_test2.a)
self.assertEqual(out_ref2.b, out_test2.b)
# confirm input mutations worked
self.assertEqual(a_test, a_ref)
self.assertEqual(b_test.a, b_ref.a)
self.assertEqual(b_test.b, b_ref.b)
(out_ref1 * out_ref2).sum().backward()
(out_test1 * out_test2).sum().backward()
# Both grad_inputs are TwoTensors
self.assertEqual(a_ref_base.grad.a, a_test_base.grad.a)
self.assertEqual(a_ref_base.grad.b, a_test_base.grad.b)
def test_aot_dispatch_output_requires_grad_in_no_grad(self):
def fn(x):
out1 = x.sin()
with torch.enable_grad():
out2 = x.cos()
return out1, out2
inp_fns = [
lambda: torch.ones(10, requires_grad=True),
lambda: torch.ones(10, requires_grad=False),
]
compiled_f = aot_function(fn, nop)
for inp_fn in inp_fns:
with torch.no_grad():
ref_x = inp_fn()
ref_out = fn(ref_x)
x = inp_fn()
out = compiled_f(x)
for r, o in zip(ref_out, out):
self.assertEqual(r.requires_grad, o.requires_grad)
if ref_x.requires_grad:
with torch.enable_grad():
(ref_out[0] + ref_out[1]).sum().backward()
(out[0] + out[1]).sum().backward()
self.assertEqual(ref_x.grad, x.grad)
assert torch.allclose(ref_x.grad, x.grad, atol=1e-3, rtol=1e-3)
def test_aot_dispatch_output_requires_grad_in_no_grad_views(self):
# view-type ops preserve requires_grad even in no_grad.
def fn(x):
return x.view(-1), x.sin()
inference_graph_cell = [None]
inference_compiler = make_boxed_compiler(
partial(extract_graph, graph_cell=inference_graph_cell)
)
compiled_fn = aot_function(fn, nop, inference_compiler=inference_compiler)
inp_x0 = torch.ones(2, 3, requires_grad=True)
# Clone in no_grad will make requires_grad=False tensors, keep clone outside of no_grad
ref_x0 = inp_x0.clone()
x0 = inp_x0.clone()
with torch.no_grad():
ref_out1, ref_out2 = fn(ref_x0)
out1, out2 = compiled_fn(x0)
# Assert that we executed inference graph
self.assertTrue(inference_graph_cell[0] is not None)
self.assertEqual(ref_out1.requires_grad, out1.requires_grad)
self.assertEqual(ref_out2.requires_grad, out2.requires_grad)
class GradsNoForceContiguousContextManager(ContextDecorator):
def __enter__(self):
# flake8: noqa: TOR901
self.lib = torch.library.Library("_mylib", "FRAGMENT")
self.d = {
torch.channels_last: 0,
torch.contiguous_format: 0,
}
self.lib.define("foo(Tensor x) -> Tensor")
self.lib.define("foo2(Tensor x) -> Tensor")
def foo_impl(a):
return a.clone()
def foo_meta(a):
return a.clone()
def foo2_impl(x):
self.d[torch._prims_common.suggest_memory_format(x)] += 1
return x.clone()
def foo2_meta(a):
return a.clone()
for backend in ["CPU", "CUDA"]:
self.lib.impl("foo", foo_impl, backend)
self.lib.impl("foo2", foo2_impl, backend)
self.lib.impl("foo", foo_meta, "Meta")
self.lib.impl("foo2", foo2_meta, "Meta")
def foo_bwd(ctx, grad):
torch.ops._mylib.foo2(grad)
return grad.clone()
torch.library.register_autograd("_mylib::foo", foo_bwd, lib=self.lib)
from torch._higher_order_ops.effects import _EffectType, _register_effectful_op
_register_effectful_op(torch.ops._mylib.foo.default, _EffectType.ORDERED)
_register_effectful_op(torch.ops._mylib.foo2.default, _EffectType.ORDERED)
return self
def __exit__(self, type, value, tb):
self.lib._destroy()
return False
def reset_counters(self):
self.d = {
torch.channels_last: 0,
torch.contiguous_format: 0,
}
class TestAOTModuleSimplified(AOTTestCase):
def test_aot_module_simplified(self):
class MockModule(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
self.linear = torch.nn.Linear(20, 30)
def forward(self, x, y):
return (self.linear(x) + y,)
mod = MockModule()
mod.zero_grad()
x = torch.randn(128, 20, requires_grad=True)
y = torch.randn(128, 30, requires_grad=True)
inputs = [x, y]
cloned_inputs = [x.detach().clone().requires_grad_(True) for x in inputs]
ref = mod(*inputs)
ref[0].sum().backward()
compiled_f = aot_module_simplified(mod, cloned_inputs, nop)
mod.zero_grad()
res = compiled_f(*cloned_inputs)
res[0].sum().backward()
assert torch.allclose(ref[0], res[0])
assert torch.allclose(inputs[0].grad, cloned_inputs[0].grad)
assert torch.allclose(inputs[1].grad, cloned_inputs[1].grad)
def test_aot_module_simplified_dynamic(self):
class MockModule(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
self.linear = torch.nn.Linear(20, 30)
def forward(self, x, y):
return (self.linear(x) + y,)
mod = MockModule()
shape_env = ShapeEnv()
fake_mode = FakeTensorMode(shape_env=shape_env)
x = torch.randn(128, 20, requires_grad=True)
y = torch.randn(128, 30, requires_grad=True)
inputs = [x, y]
fake_inputs = [fake_mode.from_tensor(x) for x in inputs]
compiled_f = aot_module_simplified(mod, fake_inputs, nop)
ref = mod(*inputs)
ref[0].sum().backward()
cloned_inputs = [x.detach().clone().requires_grad_(True) for x in inputs]
res = compiled_f(*cloned_inputs)
res[0].sum().backward()
self.assertExpectedInline(
shape_env.format_guards(),
"""\
- Eq(s1, 20)
- Eq(s2, 30)""",
)
assert torch.allclose(ref[0], res[0])
assert torch.allclose(inputs[0].grad, cloned_inputs[0].grad)
assert torch.allclose(inputs[1].grad, cloned_inputs[1].grad)
# https://github.com/pytorch/pytorch/issues/105327
def test_lift_fresh_copy_in_graph(self):
class MyMod(torch.nn.Module):
def forward(self, x):
_tensor_constant0 = torch.tensor([1])
lift_fresh_copy = torch.ops.aten.lift_fresh_copy.default(
_tensor_constant0
)
y = x.mul(lift_fresh_copy)
return (y,)
mod = MyMod()
shape_env = ShapeEnv()
fake_mode = FakeTensorMode(shape_env=shape_env)
x = torch.ones(4, requires_grad=True)
inputs = [x]
fake_inputs = [fake_mode.from_tensor(x) for x in inputs]
compiled_f = aot_module_simplified(mod, fake_inputs, nop)
out_ref = mod(x)
out_test = compiled_f(x)
self.assertEqual(out_ref[0].detach(), out_test[0].detach())
def test_inference_python_dispatcher(self):
# Extracted from unet
class MockModule(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
self.upsample = torch.nn.Upsample(
scale_factor=2, mode="bilinear", align_corners=True
)
def forward(self, x):
return (self.upsample(x),)
mod = MockModule()
shape_env = ShapeEnv()
fake_mode = FakeTensorMode(shape_env=shape_env)
x = torch.randn(2, 512, 40, 59) # NB: must not require grad
inputs = [x]
fake_inputs = [fake_mode.from_tensor(x) for x in inputs]
compiled_f = aot_module_simplified(mod, fake_inputs, nop)
def test_aot_module_simplified_preserves_stack_trace(self):
class MockModule(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
self.linear = torch.nn.Linear(20, 30)
def forward(self, x, y):
z = self.linear(x)
z = z + y
z = z.relu()
return (z,)
tracer = torch.fx.Tracer()
tracer.record_stack_traces = True
graph = tracer.trace(MockModule())
mod = torch.fx.GraphModule(tracer.root, graph)
for node in mod.graph.nodes:
if node.op == "output":
continue
self.assertTrue(node.stack_trace is not None)
assert "test_aotdispatch.py" in node.stack_trace
def assert_compiler(gm: torch.fx.GraphModule, _):
for node in gm.graph.nodes:
if node.op == "output" or node.op == "placeholder":
continue
self.assertTrue(node.stack_trace is not None)
assert "test_aotdispatch.py" in node.stack_trace
return gm.forward # return a python callable
x = torch.randn(128, 20, requires_grad=True)
y = torch.randn(128, 30, requires_grad=True)
inputs = [x, y]
compiled_f = aot_module_simplified(
mod, inputs, fw_compiler=assert_compiler, bw_compiler=assert_compiler
)
res = compiled_f(*inputs)
res[0].sum().backward()
def test_aot_module_simplified_preserves_stack_trace_from_mutation(self):
class MockModule(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
def forward(self, x):
x_view = x[0]
x_view.mul_(2)
return (x + x,)
tracer = torch.fx.Tracer()
tracer.record_stack_traces = True
graph = tracer.trace(MockModule())
mod = torch.fx.GraphModule(tracer.root, graph)
for node in mod.graph.nodes:
if node.op == "output":
continue
self.assertTrue(node.stack_trace is not None)
assert "test_aotdispatch.py" in node.stack_trace
def assert_compiler(gm: torch.fx.GraphModule, _):
assert torch.ops.aten.copy_.default in [x.target for x in gm.graph.nodes]
for node in gm.graph.nodes:
if node.target == torch.ops.aten.copy_.default:
assert "stack_trace" in node.meta
assert "x_view.mul_(2)" in node.meta["stack_trace"]
return gm.forward # return a python callable
x = torch.randn(128, 20)
inputs = [x]
aot_module_simplified(
mod,
inputs,
fw_compiler=assert_compiler,
bw_compiler=assert_compiler,
keep_inference_input_mutations=True,
)
def test_aot_module_simplified_fake_tensor_gm_raises(self):
fake_mode = torch._subclasses.fake_tensor.FakeTensorMode()
real_x = torch.randn(4, requires_grad=True)
fake_x = fake_mode.from_tensor(real_x)
real_z = torch.randn(4)
fake_z = fake_mode.from_tensor(real_z)
class MockModule(torch.nn.Module):
def forward(self, x):
# Accessing a free variable fake tensor will look like a
# constant to make_fx, and result in the tensor being traced
# into the graph, which is an error condition. Make sure we
# report adequately in this case.
return (x + fake_z,)
with self.assertRaisesRegex(AssertionError, "Unexpected fake"):
aot_module_simplified(MockModule(), (fake_x,), nop)
def test_aot_test_subclasses_with_tensor_factories(self):
from torch.testing._internal.common_subclass import SubclassWithTensorFactory
inp = SubclassWithTensorFactory(torch.zeros(3, 5))
def fn(x):
return 2 * x
ref_out = fn(inp)
out = torch.compile(fn, backend="aot_eager", fullgraph=True)(inp)
self.assertEqual(ref_out, out)
# Next several tests are related to issue:
# https://github.com/pytorch/pytorch/issues/134644
# AOTD tries to predict tangents for tracing ahead of time.
# The first strategy was to coerce traced_tangents and runtime_tangents to be contiguous().
# But for models working in channels_last memory format this will add additional contiguous() calls.
# The fix is predicting tangents memory format to be similar to outputs memory format.
# And coerce runtime tangents to that traced memory format.
def test_grads_no_force_contiguous_dense(self):
with GradsNoForceContiguousContextManager() as ctx:
class M(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
self.conv = torch.nn.Conv2d(3, 3, 3)
def forward(self, x, y, cont_inp):
z = y + 3
y.mul_(2)
r = self.conv(x)
r = torch.ops._mylib.foo(r)
return (
r,
r.transpose(0, 1),
z.view(-1),
z.transpose(0, 1),
cont_inp * 2,
)
m = M()
m.to(memory_format=torch.channels_last)
m.train()
def dense_inps():
return (
torch.randn(2, 3, 5, 5, requires_grad=True).to(
memory_format=torch.channels_last
),
torch.randn(3, 2, 1, 1, requires_grad=True).to(
memory_format=torch.channels_last
),
torch.randn(3, 2, 1, 1, requires_grad=True),
)
ref_inps = dense_inps()
ref_outs = m(*ref_inps)
ref_outs[0].sum().backward()
ctx.reset_counters()
inps = dense_inps()
outs = torch.compile(m, backend="inductor", fullgraph=True)(*inps)
outs[0].sum().backward()
self.assertEqual(ctx.d[torch.channels_last], 1)
self.assertEqual(ctx.d[torch.contiguous_format], 0)
def test_grads_no_force_contiguous_subclass(self):
with GradsNoForceContiguousContextManager() as ctx:
class M(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
self.conv = torch.nn.Conv2d(3, 3, 3)
def forward(self, x, y):
r = self.conv(x)
r = torch.ops._mylib.foo(r)
return r, y + 1
m = M()
m.to(memory_format=torch.channels_last)
m.train()
def inps_fn():
return (
TwoTensor(
torch.randn(2, 3, 5, 5, requires_grad=True).to(
memory_format=torch.channels_last
),
torch.randn(2, 3, 5, 5, requires_grad=True).to(
memory_format=torch.channels_last
),
),
torch.randn(3, 2, requires_grad=True).clone(),
)
ref_outs = m(*inps_fn())
ref_outs[0].sum().backward()
ctx.reset_counters()
mc = M()
mc.to(memory_format=torch.channels_last)
mc.train()
outs = torch.compile(mc, backend="aot_eager", fullgraph=True)(*inps_fn())
outs[0].sum().backward()
self.assertEqual(ctx.d[torch.channels_last], 2)
self.assertEqual(ctx.d[torch.contiguous_format], 0)
def test_grads_no_force_contiguous_nested_subclass(self):
with GradsNoForceContiguousContextManager() as ctx:
class M(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
self.conv = torch.nn.Conv2d(3, 3, 3)
def forward(self, x):
r = self.conv(x)
r = torch.ops._mylib.foo(r)
return r
m = M()
m.to(memory_format=torch.channels_last)
m.train()
def inps_fn(x):
return (
TwoTensor(
TwoTensor(x.clone(), x.clone()), TwoTensor(x.clone(), x.clone())
),
)
x = torch.randn(2, 3, 5, 5, requires_grad=True).to(
memory_format=torch.channels_last
)
ref_inps = inps_fn(x)
ref_outs = m(*ref_inps)
ref_outs[0].sum().backward()
ctx.reset_counters()
mc = M()
mc.to(memory_format=torch.channels_last)
mc.train()
x = torch.randn(2, 3, 5, 5, requires_grad=True).to(
memory_format=torch.channels_last
)
inps = inps_fn(x)
outs = torch.compile(mc, backend="aot_eager", fullgraph=True)(*inps)
outs[0].sum().backward()
self.assertEqual(ctx.d[torch.channels_last], 4)
self.assertEqual(ctx.d[torch.contiguous_format], 0)
def test_grads_no_force_contiguous_nested_tensor_tangent(self):
# NestedTensor setattr could fails with AttributeError for attr "_min_seqlen_tensor"
# Adding test to verify that it is handled.
def fn(x):
return x.clone()
a = torch.randn(2, 3, requires_grad=True, dtype=torch.float64)
b = torch.randn(3, 3, requires_grad=True, dtype=torch.float64)
c = torch.randn(4, 3, requires_grad=True, dtype=torch.float64)
nt = torch.nested.as_nested_tensor([a, b, c], layout=torch.jagged)
out = torch.compile(fn, backend="aot_eager", fullgraph=True)(nt)
out_buffer = out.values()
ga, gb, gc = torch.autograd.grad(out_buffer.sum(), (a, b, c))
@skipIfTorchDynamo()
def test_wrong_guess_tangent_type(self):
def fn(x):
return x.clone()
ref_x = TwoTensor(
torch.randn(2, 3, requires_grad=True), torch.randn(2, 3, requires_grad=True)
)
ref_y = fn(ref_x)
ref_y.backward(gradient=TwoTensor(torch.randn(2, 3), torch.randn(2, 3)))
fn_comp = torch.compile(fn, fullgraph=True)
x = TwoTensor(
torch.randn(2, 3, requires_grad=True), torch.randn(2, 3, requires_grad=True)
)
y = fn_comp(x)
y.backward(gradient=TwoTensor(torch.randn(2, 3), torch.randn(2, 3)))
x2 = TwoTensor(
torch.randn(2, 3, requires_grad=True), torch.randn(2, 3, requires_grad=True)
)
y2 = fn_comp(x2)
with self.assertRaisesRegex(
RuntimeError,
"""
During the backward, we encountered a tensor subclass where we guessed its
metadata incorrectly.
""", # noqa: F541
):
y2.backward(gradient=torch.randn(2, 3))
def test_tangent_type_coercion(self):
def fn(x):
return x.clone()
ref_y = fn(WrapperSubclass(torch.randn(2, 3, requires_grad=True)))
ref_y.sum().backward()
fn_comp = torch.compile(fn, fullgraph=True)
x = TwoTensor(
torch.randn(2, 3, requires_grad=True), torch.randn(2, 3, requires_grad=True)
)
y = fn_comp(x)
y.backward(gradient=TwoTensor(torch.randn(2, 3), torch.randn(2, 3)))
x2 = TwoTensor(
torch.randn(2, 3, requires_grad=True), torch.randn(2, 3, requires_grad=True)
)
y2 = fn_comp(x2)
# Test coercion WrapperSubclass -> TwoTensor
y2.backward(gradient=WrapperSubclass(torch.randn(2, 3)))
y3 = torch.compile(fn, fullgraph=True)(torch.randn(2, 3, requires_grad=True))
# Test coercion WrapperSubclass -> Tensor
y3.backward(gradient=WrapperSubclass(torch.randn(2, 3)))
@torch._inductor.config.patch({"freezing": True})
def test_inductor_freezing_with_subclasses(self):
class M(torch.nn.Module):
def __init__(self):
super().__init__()
self.w = TwoTensor(torch.randn(3, 4), torch.randn(3, 4))
self.wt = torch.randn(3, 4)
def forward(self, x):
return (
x.index_select(
dim=0, index=torch.tensor([0, 2, 1], dtype=torch.int64)
)
+ self.w
+ self.wt
)
m = M()
inp = torch.randn(3, 4)
with torch.no_grad():
torch.compile(m, fullgraph=True)(inp)
def test_rrelu(self):
def fn(x):
return torch.rrelu(x, training=True)
def fn_(x):
torch.rrelu_(x, training=True)
return x
x = torch.randn(4, 4)
torch.compile(fn, backend="inductor", fullgraph=True)(x)
torch.compile(fn_, backend="inductor", fullgraph=True)(x)
def test_subclass_parameters(self):
class _M(torch.nn.Module):
def __init__(self):
super().__init__()
self.p = torch.nn.Parameter(
TwoTensor(
TwoTensor(torch.zeros(3, 4), torch.randn(3, 4)),
torch.ones(3, 4),
)
)
def forward(self, x):
return x + self.p
class M(torch.nn.Module):
def __init__(self):
super().__init__()
self.p1 = torch.nn.Parameter(torch.ones(3, 4))
self.p2 = torch.nn.Parameter(
TwoTensor(
torch.ones(3, 4),
TwoTensor(torch.randn(3, 4), torch.randn(3, 4)),
)
)
self._m = _M()
def forward(self, x):
return self._m(x) + x + 2 * self.p1 + self.p2
m = M()
ref_x = torch.randn(3, 4)
ref_out = m(ref_x)
ref_out.sum().backward()
m.zero_grad()
from torch._functorch._aot_autograd.subclass_parametrization import (
unwrap_tensor_subclass_parameters,
)
unwrap_tensor_subclass_parameters(m)
ref_x2 = ref_x.detach().clone()
ref_out2 = m(ref_x2)
self.assertEqual(ref_out2, ref_out)
ref_out2.sum().backward()
self.assertEqual(ref_x2.grad, ref_x.grad)
m.zero_grad()
x = ref_x.detach().clone()
comp_fn = torch.compile(m, backend="aot_eager", fullgraph=True)
out = comp_fn(x)
self.assertEqual(ref_out, out)
out.sum().backward()
self.assertEqual(ref_x.grad, x.grad)
def test_rrelu_with_noise_mutation(self):
def fn_functional(x):
noise = torch.ones_like(x)
result, noise_out = torch.ops.aten.rrelu_with_noise_functional(
x, noise, 0.2, 0.8, True
)
return result, noise_out
def fn_mutation(x):
noise = torch.ones_like(x)
result = torch.ops.aten.rrelu_with_noise(x, noise, 0.2, 0.8, True)
return result, noise
def fn_inplace(x):
noise = torch.ones_like(x, requires_grad=False)
torch.ops.aten.rrelu_with_noise_(x, noise, 0.2, 0.8, True)
return x, noise
def _test_fn(fn, check_backward=True):
x = -torch.abs(torch.randn(4, 4, dtype=torch.bfloat16, requires_grad=True))
ref_y, ref_noise = fn(x)
self.assertTrue(torch.all(ref_noise < torch.ones_like(ref_noise)).item())
comp_y, comp_noise = torch.compile(fn, backend="inductor", fullgraph=True)(
x
)
if check_backward:
comp_y.sum().backward()
self.assertTrue(torch.all(comp_noise < torch.ones_like(comp_noise)).item())
_test_fn(fn_functional)
_test_fn(fn_mutation)
_test_fn(fn_inplace, check_backward=False)
# entries in here don't work and need to be fixed.
# Each one of these is a bug (or needs to be investigated)
aot_autograd_failures = {
# data-dependent control flow
xfail("cov"),
xfail("nn.functional.gaussian_nll_loss"),
xfail("tensor_split"),
xfail("corrcoef"),
xfail("quantile"),
xfail("nanquantile"),
xfail("narrow"),
xfail("istft"),
xfail("linalg.eig"),
skip("as_strided_scatter"),
skip("as_strided", "partial_views"), # flaky
# Given input size: (s0xs1x2). Calculated output size: ...
skip("max_pool2d_with_indices_backward"),
skip("nn.functional.nll_loss", ""), # UBSAN failure!
# Misc
xfail("to_sparse"),
xfail("corrcoef"),
xfail("cov"),
xfail("chalf"), # RuntimeError: "sum_cpu" not implemented for 'ComplexHalf'
xfail("sparse.sampled_addmm"),
xfail("sparse.mm", "reduce"),
skip("nn.functional.binary_cross_entropy_with_logits"), # seems to fail sometimes?
skip("nn.functional.margin_ranking_loss"), # seems flaky
skip("linalg.lu_solve"), # flaky
decorate("matmul", decorator=unittest.skipIf(IS_ARM64, "flaky")),
decorate("__rmatmul__", decorator=unittest.skipIf(IS_ARM64, "flaky")),
# overrides atol=1e-4, rtol=1e-5 would do as well
decorate(
"svd_lowrank",
decorator=toleranceOverride({torch.float32: tol(atol=1e-04, rtol=1e-05)}),
),
decorate(
"linalg.householder_product",
decorator=unittest.skipIf(IS_MACOS and IS_X86, "flaky"),
),
decorate(
"linalg.pinv",
"singular",
decorator=toleranceOverride({torch.float32: tol(atol=1e-05, rtol=1e-05)}),
),
decorate(
"nn.functional.interpolate",
"bicubic",
decorator=toleranceOverride({torch.float32: tol(atol=1e-04, rtol=1e-05)}),
),
# conv2d sometimes nondeterministic in this config?
decorate("nn.functional.conv2d", decorator=unittest.skipIf(IS_ARM64, "flaky")),
}
symbolic_aot_autograd_failures = {
xfail("combinations", ""), # aten.masked_select.default
xfail(
"index_fill", ""
), # Cannot call sizes() on tensor with symbolic sizes/strides
xfail(
"linalg.lstsq", ""
), # aten.linalg_lstsq.default - couldn't find symbolic meta function/decomposition
xfail(
"linalg.lstsq", "grad_oriented"
), # aten.linalg_lstsq.default - couldn't find symbolic meta funct...
xfail(
"linalg.lu_solve", ""
), # aten.linalg_lu_solve.default - couldn't find symbolic meta function/deco...
skip(
"nn.functional.batch_norm", ""
), # '0 is not tracked with proxy for <torch.fx.experimental.proxy_te..
xfail(
"nn.functional.binary_cross_entropy", ""
), # aten.fill_.Scalar - couldn't find symbolic meta funct...
xfail(
"nn.functional.cross_entropy", ""
), # Cannot call sizes() on tensor with symbolic sizes/strides
xfail(
"nn.functional.ctc_loss", ""
), # aten._ctc_loss.Tensor - couldn't find symbolic meta function/deco...
xfail(
"nn.functional.fractional_max_pool3d", ""
), # rand() received an invalid combination of arguments - g...
xfail(
"nn.functional.group_norm", ""
), # Cannot call sizes() on tensor with symbolic sizes/strides
xfail(
"nn.functional.nll_loss", ""
), # Cannot call sizes() on tensor with symbolic sizes/strides
xfail("trace", ""), # Cannot call sizes() on tensor with symbolic sizes/strides
xfail(
"_upsample_bilinear2d_aa"
), # RuntimeError: isIntList() INTERNAL ASSERT FAILED Expected IntList but got GenericList
decorate(
"linalg.householder_product",
decorator=unittest.skipIf(IS_MACOS and IS_X86, "flaky"),
),
# many complex operators incorrect striding, metadata
xfail("fft.fft", ""),
xfail("fft.hfft2", ""),
xfail("fft.hfft", ""),
xfail("fft.hfftn", ""),
xfail("fft.ifft", ""),
xfail("fft.ihfft2", ""),
xfail("fft.ihfft", ""),
xfail("fft.ihfftn", ""),
xfail("fft.irfft2", ""),
xfail("fft.irfft", ""),
xfail("fft.irfftn", ""),
xfail("fft.rfft2", ""),
xfail("fft.rfft", ""),
xfail("fft.rfftn", ""),
xfail("stft", ""), # Cannot call sizes() on tensor with symbolic sizes/strides
}
def _test_aot_autograd_helper(self, device, dtype, op, dynamic=False):
if not op.supports_autograd:
self.skipTest("Op does not support autograd")
# aot_autograd_check is able to check data specialization by
# randomizing the inputs. Here's a list of ops that really do not
# like random inputs for which we want to disable that.
cant_check_data_specialization = set(
{
"nn.functional.max_unpool1d",
"nn.functional.max_unpool2d",
"nn.functional.max_unpool3d",
}
)
try_check_data_specialization = op.name not in cant_check_data_specialization
sample_inputs_itr = op.sample_inputs(device, dtype, requires_grad=True)
for sample_input in sample_inputs_itr:
t_args = [sample_input.input] + list(sample_input.args)
t_kwargs = sample_input.kwargs
try:
aot_autograd_check(
op.op,
t_args,
t_kwargs,
dynamic,
self.assertRaisesRegex,
self.assertEqual,
check_gradients=True,
try_check_data_specialization=try_check_data_specialization,
skip_correctness_check=op.skip_correctness_check_compile_vs_eager,
)
except DynamicOutputShapeException:
self.skipTest("Dynamic output shape operation in trace")
except GuardOnDataDependentSymNode:
# Carveout for getitem; I don't want to xfail the entire test
# because that will reject known to be good tests see
# https://github.com/pytorch/pytorch/issues/94705
if op.name == "__getitem__":
self.skipTest("Dynamic output shape operation in trace")
else:
raise
def _test_aot_autograd_module_helper(
self, device, dtype, training, module_info, *, dynamic=False
):
module_cls = module_info.module_cls
module_inputs = module_info.module_inputs_func(
module_info, device=device, dtype=dtype, requires_grad=True, training=training
)
for module_input in module_inputs:
if module_input.forward_input is None:
continue
args, kwargs = (
module_input.constructor_input.args,
module_input.constructor_input.kwargs,
)
m = module_cls(*args, **kwargs)
m.to(device).to(dtype)
m.train(training)
# Lazy modules need to see an input first to initialize params.
args, kwargs = (
module_input.forward_input.args,
module_input.forward_input.kwargs,
)
flat_args, args_spec = pytree.tree_flatten((args, kwargs))
# PackedSequence is only used for RNNs. It might be possible to fake-ify if they're pytrees but
# torchdynamo already doesn't support RNNs
if any(tuple(isinstance(flat_arg, PackedSequence) for flat_arg in flat_args)):
continue
if issubclass(module_info.module_cls, torch.nn.modules.lazy.LazyModuleMixin):
with torch.no_grad():
m(*args, **kwargs)
sentinel_val = -42
is_tensor_spec = [
sentinel_val if isinstance(arg, torch.Tensor) else arg for arg in flat_args
]
args = [arg for arg in flat_args if isinstance(arg, torch.Tensor)]
def f(params_buffers_args):
named_params, named_buffers, args = params_buffers_args
cur_flat_args = list(is_tensor_spec)
args = iter(args)
for idx, v in enumerate(cur_flat_args):
if v == sentinel_val:
cur_flat_args[idx] = next(args)
c_args, c_kwargs = pytree.tree_unflatten(cur_flat_args, args_spec)
params_and_buffers = {**named_params, **named_buffers}
return torch.func.functional_call(m, params_and_buffers, c_args, c_kwargs)
named_params = dict(m.named_parameters(remove_duplicate=False))
named_buffers = dict(m.named_buffers(remove_duplicate=False))
num_params_buffers = len(named_params) + len(named_buffers)
compiled_f = aot_function(
f, nop, num_params_buffers=num_params_buffers, dynamic=dynamic
)
params_buffers_args = [named_params, named_buffers, args]
_test_aot_autograd_forwards_backwards_helper(
f,
compiled_f,
params_buffers_args,
self.assertRaisesRegex,
self.assertEqual,
True,
)
class TestEagerFusionOpInfo(AOTTestCase):
@ops(op_db + hop_db, allowed_dtypes=(torch.float,))
@skipOps(
"TestEagerFusionOpInfo", "test_aot_autograd_exhaustive", aot_autograd_failures
)
def test_aot_autograd_exhaustive(self, device, dtype, op):
_test_aot_autograd_helper(self, device, dtype, op)
@xfailIfS390X
@ops(op_db + hop_db, allowed_dtypes=(torch.float,))
@patch("functorch.compile.config.debug_assert", True)
@skipOps(
"TestEagerFusionOpInfo",
"test_aot_autograd_symbolic_exhaustive",
aot_autograd_failures | symbolic_aot_autograd_failures,
)
def test_aot_autograd_symbolic_exhaustive(self, device, dtype, op):
_test_aot_autograd_helper(self, device, dtype, op, dynamic=True)
aot_autograd_module_failures = set(
{
torch.nn.CTCLoss, # torch._subclasses.fake_tensor.DynamicOutputShapeException: aten._ctc_loss.default
torch.nn.GaussianNLLLoss, # RuntimeError: It appears that you're trying to get value out
# of a tracing tensor with aten._local_scalar_dense.default -
# erroring out! It's likely that this is caused by data-dependent
# control flow or similar.
torch.nn.MultiLabelMarginLoss, # AssertionError: The values for attribute 'shape' do not match:
# torch.Size([1]) != torch.Size([]). Outputs of the operator are different in
# eager-mode PyTorch vs AOTAutograd. This means the operator will have incorrect
# output underneath torch.compile. This could be because the operator's
# implementation not traceable or that there is a bug in AOTAutograd.
torch.nn.TransformerEncoder, # DataDependentOutputException: aten.eq compares a mask input
# to a causal mask tensor, to see if Boolean is_causal should be set
# for TrnasformerEncoder layers, MHA and sdp custom kernels
torch.nn.Transformer, # DataDependentOutputException: aten.equal compares a mask input
# to a causal mask tensor, to see if Boolean is_causal should be set
# for TransformerEncoder layers, MHA and sdp custom kernels
# (this bubbles up to Transformer)
}
)
symbolic_aot_autograd_module_failures = {
torch.nn.Transformer, # DataDependentOutputException: aten.equal compares a mask input to a mask producing a bool
torch.nn.TransformerEncoder, # DataDependentOutputException: aten.equal compares a mask input to a mask producing a bool
torch.nn.GaussianNLLLoss, # NotImplementedError: local_scalar_dense/item NYI for torch.bool
torch.nn.GroupNorm, # in native_group_norm_backward cpg, _rem = divmod(C, group)
# TypeError: unsupported operand type(s) for divmod(): 'SymInt' and 'int'
torch.nn.FractionalMaxPool3d, # int() argument must be a string, a bytes-like object or a number, not 'SymFloat'
torch.nn.BCELoss, # new_size = _infer_size(target.size(), weight.size())
# RuntimeError: expected int at position 0, but got: SymInt
}
class TestEagerFusionModuleInfo(AOTTestCase):
@xfailIfS390X
@modules(module_db, allowed_dtypes=(torch.float,))
@decorateForModules(unittest.expectedFailure, aot_autograd_module_failures)
def test_aot_autograd_module_exhaustive(self, device, dtype, training, module_info):
_test_aot_autograd_module_helper(self, device, dtype, training, module_info)
@xfailIfS390X
@modules(module_db, allowed_dtypes=(torch.float,))
@decorateForModules(
unittest.expectedFailure,
aot_autograd_module_failures | symbolic_aot_autograd_module_failures,
)
def test_aot_autograd_symbolic_module_exhaustive(
self, device, dtype, training, module_info
):
_test_aot_autograd_module_helper(
self, device, dtype, training, module_info, dynamic=True
)
instantiate_parametrized_tests(TestAOTAutograd)
only_for = "cpu"
instantiate_device_type_tests(
TestPythonKey,
globals(),
only_for=only_for,
)
instantiate_device_type_tests(TestEagerFusionOpInfo, globals(), only_for=only_for)
instantiate_device_type_tests(TestEagerFusionModuleInfo, globals(), only_for=only_for)
@xfail_inherited_tests(
[
"test_set__and_data_mutation_bad",
"test_subclass_metadata_mutation_req_grad_True",
"test_subclass_metadata_mutation_req_grad_False",
]
)
@skipIfTorchDynamo("This test suite already uses dynamo")
class TestAOTAutogradWithDynamo(TestAOTAutograd):
"""
These are the same as TestAOTAutograd tests, but we run dynamo first to get a graph module.
"""
def assertExpectedInline(self, *args, **kwargs):
# These will have different outputs because dynamo returns a different graph module
# But we don't really care about that assertion when testing with dynamo,
# only that the outputs match, etc.
pass
def make_compiler(self, graph_cell):
return make_boxed_compiler(partial(extract_graph, graph_cell=graph_cell))
# Compiler to passes to dynamo
def run_autograd(
self,
f: Callable,
fw_graph_cell: List[Optional[Callable]],
decompositions: Optional[Dict],
keep_input_mutations: bool,
dynamic: bool,
):
"""
Runs dynamo and aot_autograd with the specified settings
"""
def dynamo_compiler(gm, inputs, **kwargs):
result = aot_module_simplified(
gm,
inputs,
fw_compiler=self.make_compiler(fw_graph_cell),
bw_compiler=self.make_compiler([None]),
decompositions=decompositions,
keep_inference_input_mutations=keep_input_mutations,
# Dynamic is calculated from whether the inputs have fake tensors
)
return result
def torch_compile_wrapper(*args, **kwargs):
torch._dynamo.reset()
fn = torch.compile(f, backend=dynamo_compiler)
try:
result = fn(*args, **kwargs)
except torch._dynamo.exc.BackendCompilerFailed as e:
# So that assertRaises works properly
raise e.inner_exception from e
return result
return torch_compile_wrapper
def test_inputs_overlapping_unsqueeze_with_mutation(self):
def f(x, y):
x.add_(1)
y.add_(1)
return x
def run(f):
base = torch.ones(10)
inputs = [base.unsqueeze(0), base.unsqueeze(0)]
return f(*inputs)
optf = torch.compile(backend="aot_eager", dynamic=True)(f)
out = run(f)
optout = run(optf)
self.assertEqual(out, optout)
def test_inputs_overlapping_with_mutation_guard_base(self):
def f(x, y):
x.add_(1)
y.add_(1)
return x
def run(f):
base = torch.ones(10)
inputs = [base[1:], base[1:]]
return f(*inputs)
optf = torch.compile(backend="aot_eager", dynamic=True)(f)
out = run(f)
optout = run(optf)
self.assertEqual(out, optout)
class MockFXGraphCache:
"""
In memory version of FXGraphCache so we can isolate testing for FXGraphCache
"""
def __init__(self) -> None:
self.cache = {}
def save(self, key, gm):
self.cache[key] = gm
def load(self, gm, inputs):
key, _ = compiled_fx_graph_hash(gm, inputs, {}, [])
if key not in self.cache:
self.cache[key] = gm
gm, _ = self.load_with_key(key, [], inputs, None, None, None, None)
return gm
def load_with_key(
self, key, debug_lines, inputs, local, remote_cache, is_backward, constants
):
gm = self.cache.get(key)
if gm is not None:
gm = make_boxed_func(gm)
gm = MockFXGraphCacheOutput(gm)
gm._fx_graph_cache_key = key
gm._time_taken_ns = 0
return gm, {}
# The following tests fail in strict caching mode (i.e. they bypass or
# cache miss instead of cache hitting). They will be fixed in the PRs above this.
FAILING_CACHE_TESTS = (
# BypassAOTAutogradCache: unsupported nodes
"test_backward_mutation_data", # Custom Autograd Function
"test_backward_mutation_metadata", # Custom Autograd Function
"test_custom_autograd", # Custom Autograd Function
"test_input_output_aliase_custom_autograd_function",
)
@xfail_inherited_tests(FAILING_CACHE_TESTS)
class TestAOTAutogradWithCache(TestAOTAutogradWithDynamo):
"""
In memory version of FXGraphCache so we can isolate testing for FXGraphCache
"""
def make_compiler(self, fw_graph_cell):
mock_inductor_cache = self.inductor_cache
def compiler(gm, example_inputs):
nonlocal mock_inductor_cache, fw_graph_cell
result = mock_inductor_cache.load(gm, example_inputs)
fw_graph_cell[0] = gm
return result
compiler = SerializableAOTDispatchCompiler(MockFXGraphCacheOutput, compiler)
return compiler
def run_autograd(
self,
f: Callable,
fw_graph_cell: List[Optional[Callable]],
decompositions: Optional[Dict],
keep_input_mutations: bool,
dynamic: bool,
):
return super().run_autograd(
f,
fw_graph_cell,
decompositions,
keep_input_mutations,
dynamic,
)
@torch._functorch.config.patch(
{
"enable_autograd_cache": True,
"strict_autograd_cache": True,
"view_replay_for_aliased_outputs": False,
}
)
@torch._inductor.config.patch("fx_graph_cache", True)
def verify_aot_autograd(
self,
f,
inp_: Union[Callable, List[Any]],
*,
test_mutation: bool = False,
keep_inp_mutations: bool = False,
decompositions: Optional[Dict] = None,
dynamic: bool = False,
# Only active when inp_ is Callable.
# TODO: probably consolidate all tests to make inp a Callable.
make_inputs_subclasses: bool = False,
):
self.inductor_cache = MockFXGraphCache()
AOTAutogradCache.clear()
with patch(
"torch._inductor.codecache.FxGraphCache.load_with_key",
new=self.inductor_cache.load_with_key,
):
return super().verify_aot_autograd(
f,
inp_,
test_mutation=test_mutation,
keep_inp_mutations=keep_inp_mutations,
decompositions=decompositions,
dynamic=dynamic,
make_inputs_subclasses=make_inputs_subclasses,
)
def test_input_mutation_false_aliasing(self):
# This test is disabled because it fails in strict cache mode
# But also can't be xfailed because it causes undefined behavior for
# ASAN
self.skipTest("Skipping because it fails in strict cache mode")
if __name__ == "__main__":
run_tests()
|