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
|
# Owner(s): ["module: functorch"]
# 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 functools
import itertools
import unittest
from common_utils import (
check_vmap_fallback,
decorate,
expectedFailureIf,
generate_vmap_inputs,
get_fallback_and_vmap_exhaustive,
is_batch_norm_training,
is_valid_inplace_sample_input,
loop,
loop2,
opsToleranceOverride,
skip,
skipOps,
tol1,
tol2,
xfail,
)
from functorch_additional_op_db import additional_op_db
import torch
import torch.autograd.forward_ad as fwAD
from functorch import grad, jacfwd, jacrev, vjp, vmap
from torch import Tensor
from torch._functorch.eager_transforms import _as_tuple, jvp
from torch.testing._internal.autograd_function_db import autograd_function_db
from torch.testing._internal.common_cuda import with_tf32_off
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_utils import (
is_iterable_of_tensors,
IS_MACOS,
IS_X86,
noncontiguous_like,
parametrize,
run_tests,
runOnRocm,
skipIfRocm,
TEST_WITH_ASAN,
TEST_WITH_ROCM,
TestCase,
unMarkDynamoStrictTest,
xfailIfS390X,
)
from torch.testing._internal.opinfo.core import SampleInput
from torch.utils import _pytree as pytree
from torch.utils._pytree import tree_flatten, tree_map, tree_unflatten
aten = torch.ops.aten
# Version of autograd.grad with some differences:
# - pytree inputs is allowed (but leaves of the pytree have to all
# be tensors)
# - if an input is not used as part of derivatives, we will return a
# zero-filled tensor for the result
def _autograd_grad(
outputs, inputs, grad_outputs=None, retain_graph=False, create_graph=True
):
inputs, inputs_spec = tree_flatten(inputs)
diff_inputs = tuple(inp for inp in inputs if inp.requires_grad)
if grad_outputs is None:
diff_outputs = tuple(out for out in outputs if out.requires_grad)
else:
diff_grad_outputs = [
(out, go) for out, go in zip(outputs, grad_outputs) if out.requires_grad
]
if len(diff_grad_outputs) == 0:
diff_outputs, grad_outputs = (), ()
else:
diff_outputs, grad_outputs = zip(*diff_grad_outputs)
grad_inputs = torch.autograd.grad(
diff_outputs,
diff_inputs,
grad_outputs,
retain_graph=retain_graph,
create_graph=create_graph,
allow_unused=True,
)
result = []
grad_inputs_iter = iter(grad_inputs)
for inp in inputs:
if inp.requires_grad:
grad_input = next(grad_inputs_iter)
if grad_input is None:
result.append(torch.zeros_like(inp))
else:
result.append(grad_input)
else:
result.append(torch.zeros_like(inp))
return tree_unflatten(result, inputs_spec)
def diff_arg(arg, requires_grad=True):
def is_differentiable_arg(arg):
if requires_grad:
return arg.requires_grad
else:
return arg.is_floating_point() or arg.is_complex()
if is_iterable_of_tensors(arg):
if all(is_differentiable_arg(a) for a in arg):
return True
if all(not is_differentiable_arg(a) for a in arg):
return False
raise RuntimeError("NYI: The test runner can't handle this")
return isinstance(arg, Tensor) and is_differentiable_arg(arg)
# Given f, returns an f' such that:
# - f' takes only positional arguments
# - All arguments to f' are floating-point Tensors
# - All outputs of f' are floating-point Tensors
def normalize_op_input_output2(
f, args, kwargs, output_process_fn_grad=None, requires_grad=True
):
flat_args, args_spec = tree_flatten(args)
diff_argnums = tuple(
i
for i, arg in enumerate(flat_args)
if diff_arg(arg, requires_grad=requires_grad)
)
assert len(diff_argnums) > 0
primals = tuple(flat_args[i] for i in diff_argnums)
@functools.wraps(f)
def wrapped(*primals):
_args = list(flat_args)
for num, arg in zip(diff_argnums, primals):
_args[num] = arg
_args = tree_unflatten(_args, args_spec)
result = f(*_args, **kwargs)
if output_process_fn_grad is not None:
result = output_process_fn_grad(result)
if isinstance(result, tuple):
result = tuple(r for r in result if torch.is_floating_point(r))
assert len(result) > 0
return result
return wrapped, primals
# TODO: consolidate with normalize_op_input_output2
def normalize_op_input_output3(
f, args, kwargs, sample_args, output_process_fn_grad=None
):
flat_args, args_spec = tree_flatten(args)
flat_sample_args = pytree.tree_leaves(sample_args)
diff_argnums = tuple(
i
for i, (arg, sample) in enumerate(zip(flat_args, flat_sample_args))
if diff_arg(sample, requires_grad=True)
)
assert len(diff_argnums) > 0
primals = tuple(flat_args[i] for i in diff_argnums)
@functools.wraps(f)
def wrapped(*primals):
_args = list(flat_args)
for num, arg in zip(diff_argnums, primals):
_args[num] = arg
_args = tree_unflatten(_args, args_spec)
result = f(*_args, **kwargs)
if output_process_fn_grad is not None:
result = output_process_fn_grad(result)
if isinstance(result, tuple):
result = tuple(r for r in result if torch.is_floating_point(r))
assert len(result) > 0
return result
return wrapped, primals
def normalize_op_input_output(f, sample, requires_grad=True):
args = tuple([sample.input] + list(sample.args))
return normalize_op_input_output2(
f,
args,
sample.kwargs,
sample.output_process_fn_grad,
requires_grad=requires_grad,
)
def ref_vjp(f, *primals):
result = f(*primals)
def wrapped(cotangents):
return _autograd_grad(_as_tuple(result), primals, _as_tuple(cotangents))
return result, wrapped
def simulate_jvp(f, primals, tangents):
primals_out, tangents_out = torch.autograd.functional.jvp(f, primals, tangents)
return primals_out, tangents_out
def ref_jvp(f, primals, tangents):
with fwAD.dual_level():
duals = tuple(fwAD.make_dual(p, t) for p, t in zip(primals, tangents))
result_duals = f(*duals)
result_duals, spec = tree_flatten(result_duals)
primals_out, tangents_out = zip(*(fwAD.unpack_dual(d) for d in result_duals))
return tree_unflatten(primals_out, spec), tree_unflatten(tangents_out, spec)
def get_sample_cotangents(f, sample):
fn, primals = normalize_op_input_output(f, sample)
output = fn(*primals)
return tree_map(torch.randn_like, output)
# returns a new function g(*args, *cotangents)
# that computes vjps and (*args, cotangents)
def get_vjp_fn_and_args_with_cotangents(f, sample, cotangents):
args = tuple([sample.input] + list(sample.args))
kwargs = sample.kwargs
flat_args, args_spec = tree_flatten(args)
flat_cotangents, cotangents_spec = tree_flatten(cotangents)
@functools.wraps(f)
def wrapped(*args):
assert len(args) == len(flat_args) + len(flat_cotangents)
actual_args = args[: len(flat_args)]
cotangents = args[len(flat_args) :]
actual_args = tree_unflatten(actual_args, args_spec)
cotangents = tree_unflatten(cotangents, cotangents_spec)
fn, primals = normalize_op_input_output3(
f, actual_args, kwargs, flat_args, sample.output_process_fn_grad
)
_, vjp_fn = vjp(fn, *primals)
return vjp_fn(cotangents)
return wrapped, tuple(flat_args + flat_cotangents)
# Returns a new function g(*args, *cotangents) that computes vjps and
# sample (*args, *cotangents)
def get_vjpfull_variant(f, sample):
fn, primals = normalize_op_input_output(f, sample)
return _get_vjpfull_variant(fn, primals)
def get_vjpfull_variant2(f, args, kwargs):
fn, primals = normalize_op_input_output2(f, args, kwargs)
return _get_vjpfull_variant(fn, primals)
def _get_vjpfull_variant(fn, primals):
result = fn(*primals)
cotangents = _as_tuple(
tree_map(lambda x: torch.randn_like(x, requires_grad=True), result)
)
num_primals = len(primals)
args = (*primals, *cotangents)
@functools.wraps(fn)
def wrapped(*args):
primals = args[:num_primals]
cotangents = args[num_primals:]
result, vjp_fn = vjp(fn, *primals)
if isinstance(result, torch.Tensor):
assert len(cotangents) == 1
cotangents = cotangents[0]
return vjp_fn(cotangents)
return wrapped, args
def get_jvp_variant(f, sample):
# We want this higher-order variant of jvp, so that it can
# be used to wrap vmap
fn, primals = normalize_op_input_output(f, sample, requires_grad=False)
tangents = _as_tuple(tree_map(lambda x: torch.randn_like(x), primals))
@functools.wraps(f)
def wrapped(*args):
tangents = args
primals_out, tangents_out = jvp(fn, primals, tangents)
if isinstance(primals_out, torch.Tensor):
return (primals_out, tangents_out)
else:
flat_primals_out = pytree.tree_leaves(primals_out)
flat_tangents_out = pytree.tree_leaves(tangents_out)
return tuple(flat_primals_out + flat_tangents_out)
return wrapped, tangents
def get_jvp_variant_primals_tangents2(
f, args, kwargs, output_process_fn_grad=None, requires_grad=False
):
fn, primals = normalize_op_input_output2(
f, args, kwargs, output_process_fn_grad, requires_grad
)
tangents = _as_tuple(tree_map(lambda x: torch.randn_like(x), primals))
return _get_jvp_variant(fn, primals, tangents)
def get_jvp_variant_primals_tangents(f, sample):
# We want this higher-order variant of jvp, so that it can
# be used to wrap vmap
fn, primals = normalize_op_input_output(f, sample, requires_grad=False)
tangents = _as_tuple(tree_map(lambda x: torch.randn_like(x), primals))
return _get_jvp_variant(fn, primals, tangents)
def _get_jvp_variant(fn, primals, tangents):
@functools.wraps(fn)
def wrapped(*args):
primals_in = args[: len(primals)]
tangents_in = args[len(primals) :]
primals_out, tangents_out = jvp(fn, primals_in, tangents_in)
if isinstance(primals_out, torch.Tensor):
return (primals_out, tangents_out)
else:
flat_primals_out = pytree.tree_leaves(primals_out)
flat_tangents_out = pytree.tree_leaves(tangents_out)
return tuple(flat_primals_out + flat_tangents_out)
return wrapped, primals + tangents
def is_inplace(op, variant):
if hasattr(variant, "__wrapped__"):
return variant.__wrapped__ is op.get_inplace()
return variant is op.get_inplace()
vjp_fail = {
xfail("tensor_split"), # data_ptr composite compliance
# Very minor accuracy issue on ROCm
decorate("nn.functional.scaled_dot_product_attention", decorator=skipIfRocm),
}
aliasing_ops = {
"T",
"broadcast_to",
"conj",
"contiguous",
"diagonal", # linalg.diagonal is an alias
"expand",
"flatten",
"imag",
"mH", # adjoint is an alias
"mT",
"movedim", # moveaxis is an alias
"narrow",
"permute",
"positive",
# 'ravel', is composite implicit autograd and may call clone
"real",
"reshape",
"resolve_conj",
"resolve_neg",
"select",
"squeeze",
"transpose", # swapdims and swapaxes are aliases
"unflatten",
"unfold",
"unsqueeze",
"view",
"view_as",
"view_as_complex",
"view_as_real",
}
aliasing_ops_list_return = {
"chunks",
"dsplit",
"hsplit",
"split",
"unbind",
"vsplit",
# 'tensor_split' not composite compliant, see vjp_fail
}
skip_noncontig = {
"_batch_norm_with_update",
"as_strided_copy",
}
@unittest.skipIf(TEST_WITH_ASAN, "tests time out with asan, are probably redundant")
@unMarkDynamoStrictTest
class TestOperators(TestCase):
@with_tf32_off # https://github.com/pytorch/pytorch/issues/86798
@ops(op_db + additional_op_db + autograd_function_db, allowed_dtypes=(torch.float,))
@skipOps(
"TestOperators",
"test_grad",
vjp_fail.union(
{
xfail(
"chalf", "", device_type="cpu"
), # RuntimeError: "sum_cpu" not implemented for 'ComplexHalf'
xfail(
"sparse.sampled_addmm", ""
), # RuntimeError: Sparse CSR tensors do not have strides
xfail(
"sparse.mm", "reduce"
), # RuntimeError: Sparse CSR tensors do not have strides
# Non-contiguous Bugs
#
# AssertionError: Tensor-likes are not close!
xfail("as_strided"),
xfail("as_strided", "partial_views"),
# RuntimeError: !self.requires_grad() || self.is_contiguous()
xfail("as_strided_scatter"),
# RuntimeError: Tensor must have a last dimension with stride 1
xfail("view_as_complex"),
# query: last dimension must be contiguous
# Fused attention kernels require last dim to be contiguous
decorate(
"nn.functional.scaled_dot_product_attention",
decorator=expectedFailureIf(not TEST_WITH_ROCM),
), # Works on ROCm
xfail("torch.ops.aten._flash_attention_forward"),
xfail("torch.ops.aten._efficient_attention_forward"),
# RuntimeError: Expected contiguous tensor, but got
# non-contiguous tensor for argument #2 'grad_output'
decorate(
"_batch_norm_with_update",
decorator=expectedFailureIf(TEST_WITH_ROCM),
device_type="cuda",
),
}
),
)
@opsToleranceOverride(
"TestOperators",
"test_grad",
(
tol1(
"nn.functional.binary_cross_entropy_with_logits",
{torch.float32: tol(atol=1e-04, rtol=1e-04)},
),
tol1("masked.cumprod", {torch.float32: tol(atol=1e-05, rtol=1e-05)}),
tol1("svd_lowrank", {torch.float32: tol(atol=3e-04, rtol=3e-04)}),
tol1(
"linalg.multi_dot",
{torch.float32: tol(atol=1e-05, rtol=8e-04)},
device_type="cuda",
),
tol1(
"linalg.tensorsolve",
{torch.float32: tol(atol=3e-04, rtol=3e-04)},
device_type="cuda",
),
tol1(
"nn.functional.multi_head_attention_forward",
{torch.float32: tol(atol=8e-04, rtol=1e-03)},
),
tol1(
"__rmatmul__",
{torch.float32: tol(atol=3e-04, rtol=3e-04)},
device_type="cuda",
),
tol1(
"matmul",
{torch.float32: tol(atol=3e-04, rtol=3e-04)},
device_type="cuda",
),
tol1(
"pca_lowrank",
{torch.float32: tol(atol=3e-05, rtol=4e-06)},
device_type="cpu",
),
),
)
def test_grad(self, device, dtype, op):
if op.name in vjp_fail:
self.skipTest("Skipped; Expected failures")
return
if not op.supports_autograd:
self.skipTest("Skipped! Autograd not supported.")
return
samples = op.sample_inputs(device, dtype, requires_grad=True)
if is_inplace(op, op.get_op()):
self.skipTest("Skipped for redundancy. test_vjp handles in-place testing.")
return
for sample in samples:
args = [sample.input] + list(sample.args)
kwargs = sample.kwargs
if op.name not in skip_noncontig:
noncontig_sample = sample.noncontiguous()
noncontig_args = [noncontig_sample.input] + list(noncontig_sample.args)
noncontig_kwargs = noncontig_sample.kwargs
diff_argnums = tuple(i for i, arg in enumerate(args) if diff_arg(arg))
assert len(diff_argnums) > 0
diff_args = tuple(args[i] for i in diff_argnums)
def wrapped_fn(*args, **kwargs):
result = op(*args, **kwargs)
if sample.output_process_fn_grad is not None:
result = sample.output_process_fn_grad(result)
def abs_if_complex(t):
if t.dtype.is_complex:
return t.abs()
return t
# Reduce into single value for grad
if isinstance(result, torch.Tensor):
return abs_if_complex(result.sum())
result = sum(abs_if_complex(res.sum()) for res in result)
return result
result = grad(wrapped_fn, diff_argnums)(*args, **kwargs)
expected = _autograd_grad(_as_tuple(wrapped_fn(*args, **kwargs)), diff_args)
self.assertEqual(result, expected)
if op.name not in skip_noncontig:
result_noncontig = grad(wrapped_fn, diff_argnums)(
*noncontig_args, **noncontig_kwargs
)
self.assertEqual(result_noncontig, expected)
@with_tf32_off # https://github.com/pytorch/pytorch/issues/86798
@ops(op_db + additional_op_db + autograd_function_db, allowed_dtypes=(torch.float,))
@skipOps(
"TestOperators",
"test_jvp",
set(
{
# Composite ops that do bad things. Need to be fixed in PyTorch core.
# RuntimeError: Cannot access data pointer of Tensor that doesn't have storage
xfail("tensor_split"),
# BUG: silent incorrectness: runs and produces numerical differences
skip("nn.functional.max_unpool1d"), # fails everywhere except on mac
skip(
"nn.functional.max_unpool2d"
), # fails everywhere except on windows
skip("nn.functional.max_unpool3d"), # fails everywhere except on mac
xfail(
"native_batch_norm"
), # TODO: fails comparing None to tensor of 0s for saved_mean/var tangents
xfail(
"_native_batch_norm_legit"
), # TODO: fails comparing None to tensor of 0s for saved_mean/var tangents
xfail(
"_batch_norm_with_update"
), # TODO: fails comparing None to tensor of 0s for saved_mean/var tangents
xfail("nn.functional.scaled_dot_product_attention"),
xfail("torch.ops.aten._flash_attention_forward"),
xfail("torch.ops.aten._efficient_attention_forward"),
xfail(
"nn.functional.rrelu"
), # in-place test errors out with no formula implemented
xfail(
"NumpyExpMarkDirtyAutogradFunction"
), # TODO: https://github.com/pytorch/pytorch/issues/91280
# --- Non-Contiguous Failures! ---
# This is expected to fail as the operator
# expects last dim to have stride=1
xfail("view_as_complex"),
# BUG
# AssertionError: Tensor-likes are not close!
xfail("as_strided"),
xfail("as_strided", "partial_views"),
xfail("as_strided_scatter"),
decorate(
"linalg.det",
"singular",
decorator=expectedFailureIf(IS_MACOS and IS_X86),
),
}
),
)
@opsToleranceOverride(
"TestOperators",
"test_jvp",
(
tol1(
"nn.functional.conv_transpose3d",
{torch.float32: tol(atol=1e-04, rtol=1.3e-06)},
device_type="cuda",
),
tol1(
"linalg.tensorsolve",
{torch.float32: tol(atol=1e-04, rtol=1.3e-05)},
device_type="cuda",
),
tol1(
"masked.prod",
{torch.float32: tol(atol=1e-05, rtol=1.3e-05)},
device_type="cuda",
),
tol1(
"nn.functional.binary_cross_entropy_with_logits",
{torch.float32: tol(atol=4e-04, rtol=4e-04)},
),
tol1(
"nn.functional.batch_norm", {torch.float32: tol(atol=4e-05, rtol=5e-05)}
),
tol1("nn.functional.conv2d", {torch.float32: tol(atol=4e-05, rtol=5e-05)}),
tol1("svd_lowrank", {torch.float32: tol(atol=5e-05, rtol=5e-05)}),
tol1("pca_lowrank", {torch.float32: tol(atol=5e-05, rtol=5e-05)}),
tol1(
"nn.functional.multi_head_attention_forward",
{torch.float32: tol(atol=6e-05, rtol=2e-05)},
),
tol2(
"linalg.pinv", "hermitian", {torch.float32: tol(atol=5e-5, rtol=2e-5)}
),
),
)
def test_jvp(self, device, dtype, op):
# TODO: get rid of vjp_decomp when we add decomposition support to
# PyTorch's forward-mode ad. Currently the decomposition support only
# works for functorch.jvp
VJP_DECOMP = {
"nn.functional.logsigmoid",
}
if op.name in VJP_DECOMP:
fixme_ref_jvp_local = simulate_jvp
else:
fixme_ref_jvp_local = ref_jvp
if not op.supports_forward_ad and op.name not in VJP_DECOMP:
self.skipTest("Skipped! Forward AD not supported.")
return
samples = op.sample_inputs(device, dtype, requires_grad=True)
outplace_variant = op if not is_inplace(op, op.get_op()) else None
inplace_variant = op.inplace_variant if op.supports_inplace_autograd else None
for sample in samples:
if outplace_variant:
self.jvp_opinfo_test(
outplace_variant,
sample,
sample.output_process_fn_grad,
clone_inputs=False,
fixme_ref_jvp_local=fixme_ref_jvp_local,
test_noncontig=op.name not in skip_noncontig,
)
if is_valid_inplace_sample_input(sample, op, inplace_variant):
self.jvp_opinfo_test(
inplace_variant,
sample,
sample.output_process_fn_grad,
clone_inputs=True,
fixme_ref_jvp_local=fixme_ref_jvp_local,
test_noncontig=op.name not in skip_noncontig,
)
def jvp_opinfo_test(
self,
fn,
sample,
output_process_fn,
clone_inputs,
fixme_ref_jvp_local,
test_noncontig,
):
# NB: we used requires_grad=True to determine where the primals are,
# but don't need that information otherwise
args = (sample.input,) + sample.args
kwargs = sample.kwargs
contig_fn, primals = normalize_op_input_output2(
fn, args, kwargs, output_process_fn, requires_grad=True
)
orig_primals = tree_map(lambda x: x.detach(), primals)
orig_tangents = tree_map(lambda x: torch.randn_like(x), primals)
def maybe_clone_inputs():
if clone_inputs:
primals = tree_map(torch.clone, orig_primals)
tangents = tree_map(torch.clone, orig_tangents)
return primals, tangents
return orig_primals, orig_tangents
primals, tangents = maybe_clone_inputs()
expected_primal_outs, expected_tangent_outs = fixme_ref_jvp_local(
contig_fn, primals, tangents
)
primals, tangents = maybe_clone_inputs()
primal_outs, tangent_outs = jvp(contig_fn, primals, tangents)
self.assertEqual(primal_outs, expected_primal_outs)
self.assertEqual(tangent_outs, expected_tangent_outs)
if test_noncontig:
noncontig_sample = sample.noncontiguous()
noncontig_args = (noncontig_sample.input,) + noncontig_sample.args
noncontig_kwargs = sample.kwargs
noncontig_fn, primals = normalize_op_input_output2(
fn,
noncontig_args,
noncontig_kwargs,
output_process_fn,
requires_grad=True,
)
noncontig_primals = tree_map(lambda x: x.detach(), primals)
noncontig_tangents = tree_map(
lambda x: noncontiguous_like(x), orig_tangents
)
noncontig_primal_outs, noncontig_tangent_outs = jvp(
noncontig_fn, noncontig_primals, noncontig_tangents
)
self.assertEqual(noncontig_primal_outs, expected_primal_outs)
self.assertEqual(noncontig_tangent_outs, expected_tangent_outs)
@with_tf32_off # https://github.com/pytorch/pytorch/issues/86798
@ops(op_db + additional_op_db + autograd_function_db, allowed_dtypes=(torch.float,))
@skipOps(
"TestOperators",
"test_vjp",
vjp_fail.union(
{
xfail("sparse.sampled_addmm", ""),
xfail("sparse.mm", "reduce"),
# ---- Non-Contiguous Failures ----
# This is expected to fail as the operator
# expects last dim to have stride=1
xfail("view_as_complex"),
# RuntimeError: query: last dimension must be contiguous
# The fused attention kernels require the last dim to be contiguous
decorate(
"nn.functional.scaled_dot_product_attention",
decorator=expectedFailureIf(not TEST_WITH_ROCM),
), # Works on ROCm
xfail("torch.ops.aten._flash_attention_forward"),
xfail("torch.ops.aten._efficient_attention_forward"),
# BUG
# AssertionError: Tensor-likes are not close!
xfail("as_strided"),
xfail("as_strided_scatter"),
xfail("as_strided", "partial_views"),
}
),
)
@opsToleranceOverride(
"TestOperators",
"test_vjp",
(
tol1(
"nn.functional.conv_transpose3d",
{torch.float32: tol(atol=5e-05, rtol=9e-05)},
device_type="cuda",
),
tol1(
"nn.functional.binary_cross_entropy_with_logits",
{torch.float32: tol(atol=1e-04, rtol=1e-04)},
),
tol1(
"nn.functional.multi_head_attention_forward",
{torch.float32: tol(atol=2e-03, rtol=2e-04)},
),
tol1("__rmatmul__", {torch.float32: tol(atol=1e-05, rtol=1e-05)}),
tol1("matmul", {torch.float32: tol(atol=1e-05, rtol=1e-05)}),
tol2(
"linalg.pinv", "hermitian", {torch.float32: tol(atol=1e-05, rtol=1e-05)}
),
tol1("linalg.tensorsolve", {torch.float32: tol(atol=9e-03, rtol=2e-04)}),
tol1("linalg.multi_dot", {torch.float32: tol(atol=1e-04, rtol=1e-04)}),
tol1("svd_lowrank", {torch.float32: tol(atol=1e-04, rtol=1e-04)}),
tol1("pca_lowrank", {torch.float32: tol(atol=1e-04, rtol=1e-04)}),
),
)
def test_vjp(self, device, dtype, op):
if not op.supports_autograd:
self.skipTest("Skipped! Autograd not supported.")
return
samples = op.sample_inputs(device, dtype, requires_grad=True)
def _test(_op, inplace=False):
for sample in samples:
if inplace and not is_valid_inplace_sample_input(
sample, op, op.inplace_variant
):
continue
fn, primals = normalize_op_input_output(_op, sample)
result = fn(*primals)
cotangents = tree_map(lambda x: torch.randn_like(x), result)
out, vjp_fn = vjp(fn, *primals)
self.assertEqual(out, result)
result_vjps = vjp_fn(cotangents)
_, vjp_fn = ref_vjp(fn, *primals)
expected_vjps = vjp_fn(cotangents)
self.assertEqual(result_vjps, expected_vjps)
if op.name not in skip_noncontig:
noncontig_fn, noncontig_primals = normalize_op_input_output(
_op, sample.noncontiguous()
)
noncontig_cotangents = tree_map(
lambda x: noncontiguous_like(x), cotangents
)
out_noncontig, vjp_fn = vjp(noncontig_fn, *noncontig_primals)
self.assertEqual(out_noncontig, result)
noncontig_result_vjps = vjp_fn(noncontig_cotangents)
self.assertEqual(noncontig_result_vjps, expected_vjps)
_test(op)
for a_op in op.aliases:
_test(a_op)
if op.inplace_variant:
def f(inp, *args, **kwargs):
return op.inplace_variant(inp.clone(), *args, **kwargs)
_test(f, inplace=True)
@ops(op_db + additional_op_db + autograd_function_db, allowed_dtypes=(torch.float,))
@skipOps(
"TestOperators",
"test_vjpvjp",
vjp_fail.union(
{
skip("nn.functional.max_unpool1d"), # silent incorrectness; Flaky
skip("nn.functional.max_unpool2d"), # silent incorrectness; Flaky
xfail("nn.functional.ctc_loss"), # Not Implemented
xfail(
"native_layer_norm", ""
), # Expected a proper Tensor but got None for argument #1 'other'
xfail("sparse.sampled_addmm", ""), # sparse tensors have no strides
xfail("sparse.mm", "reduce"), # sparse tensors have no strides
skip("nn.functional.scaled_dot_product_attention"),
xfail("torch.ops.aten._flash_attention_forward"),
xfail("torch.ops.aten._efficient_attention_forward"),
# AssertionError: Tensor-likes are not close!
# Mismatched elements: 1 / 15 (6.7%)
# Greatest absolute difference: 24.0 at index (2, 4) (up to 1e-05 allowed)
# Greatest relative difference: 1.7933241714393998e-06 at index (2, 4) (up to 1.3e-06 allowed)
# The failure occurred for item [0]
xfail("masked.prod"),
}
),
)
@opsToleranceOverride(
"TestOperators",
"test_vjpvjp",
(
tol1(
"nn.functional.conv_transpose3d",
{torch.float32: tol(atol=5e-05, rtol=9e-05)},
device_type="cuda",
),
tol1("prod", {torch.float32: tol(atol=2e-05, rtol=1e-04)}),
tol1("masked.cumprod", {torch.float32: tol(atol=5e-04, rtol=5e-04)}),
tol1("cumprod", {torch.float32: tol(atol=5e-04, rtol=5e-04)}),
tol1("linalg.vander", {torch.float32: tol(atol=5e-04, rtol=5e-04)}),
tol2(
"linalg.det", "singular", {torch.float32: tol(atol=2e-05, rtol=2e-05)}
),
),
)
def test_vjpvjp(self, device, dtype, op):
if not op.supports_autograd:
self.skipTest("Skipped! Autograd not supported.")
return
if not op.supports_gradgrad:
self.skipTest("Skipped! Operation does not support gradgrad")
return
samples = op.sample_inputs(device, dtype, requires_grad=True)
def test(_op, inplace=False):
for sample in samples:
if inplace and not is_valid_inplace_sample_input(
sample, op, op.inplace_variant
):
continue
fn, args = get_vjpfull_variant(_op, sample)
result = fn(*args)
cotangents = tree_map(lambda x: torch.randn_like(x), result)
# Compute vjp of vjp
_, vjp_fn = vjp(fn, *args)
result_vjps = vjp_fn(cotangents)
# Compute ref_vjp of vjp. We could have done ref_vjp of ref_vjp,
# but since we're confident that vjp works by itself, this is
# an equivalent way to test that.
_, vjp_fn = ref_vjp(fn, *args)
expected_vjps = vjp_fn(cotangents)
self.assertEqual(result_vjps, expected_vjps)
test(op)
if op.inplace_variant:
def fn(inp, *args, **kwargs):
return op.inplace_variant(inp.clone(), *args, **kwargs)
test(fn, inplace=True)
@with_tf32_off # https://github.com/pytorch/pytorch/issues/86798
@skipOps(
"TestOperators",
"test_vmapvjpvjp",
vjp_fail.union(
{
skip("atleast_1d"), # Takes too long
skip("atleast_2d"), # Takes too long
skip("atleast_3d"), # Takes too long
skip("ormqr"), # Takes too long
xfail("as_strided"), # incorrect output
xfail("as_strided", "partial_views"), # incorrect output
xfail("as_strided_scatter"), # incorrect output
skip("bernoulli"), # calls random op
xfail("bfloat16"), # rank 4 tensor for channels_last
xfail("cdouble"), # rank 4 tensor for channels_last
xfail("cfloat"), # rank 4 tensor for channels_last
xfail("chalf"), # rank 4 tensor for channels_last
xfail("double"), # rank 4 tensor for channels_last
xfail("float"), # rank 4 tensor for channels_last
xfail("half"), # rank 4 tensor for channels_last
xfail(
"NumpyCubeNotComposableAutogradFunction"
), # Not composable autograd.Function
# It looks like you're either (1) calling .item() on a Tensor or
# (2) attempting to use a Tensor in some data-dependent control flow or
# (3) encountering this error in PyTorch internals.
xfail("index_reduce", "prod"),
decorate(
"linalg.householder_product", decorator=runOnRocm
), # works on ROCm
xfail(
# nans
"masked.softmax",
device_type="cpu",
),
xfail(
"nanquantile", device_type="cpu"
), # vmap not implemented for at::equal.
xfail("native_layer_norm"), # vmap: inplace into a regular tensor
# got a batched tensor as input while the running_mean or running_var,
# which will be updated in place, were not batched.
xfail("nn.functional.batch_norm"),
xfail(
"nn.functional.binary_cross_entropy"
), # vmap: inplace into a regular tensor
xfail(
"nn.functional.ctc_loss"
), # derivate not implemented for _ctc_loss_backward
# flaky on ROCM needs investigation
decorate("nn.functional.conv_transpose2d", decorator=skipIfRocm),
skip("nn.functional.dropout"), # calls random op
skip("nn.functional.dropout2d"), # calls random op
skip("nn.functional.dropout3d"), # calls random op
skip("nn.functional.alpha_dropout"), # calls random op
skip(
"nn.functional.feature_alpha_dropout", "with_train"
), # calls random op
skip("nn.functional.fractional_max_pool2d"), # calls random op
skip("nn.functional.fractional_max_pool3d"), # calls random op
xfail("nn.functional.scaled_dot_product_attention"), # randomness
xfail("torch.ops.aten._efficient_attention_forward"), # outputs ints
xfail("nn.functional.multi_head_attention_forward"), # randomness
# It looks like you're either (1) calling .item() on a Tensor or
# (2) attempting to use a Tensor in some data-dependent control flow or
# (3) encountering this error in PyTorch internals.
xfail("nn.functional.gaussian_nll_loss"),
# got a batched tensor as input while the running_mean or running_var,
# which will be updated in place, were not batched.
xfail("nn.functional.instance_norm"),
xfail(
"nn.functional.layer_norm"
), # vmap: inplace into a regular tensor
# RuntimeError: NYI: querying is_contiguous inside of vmap
# for memory_format other than torch.contiguous_formats
xfail("nn.functional.max_pool2d"),
# RuntimeError: NYI: Tensor.clone(memory_format) inside vmap is only
# supported with memory_format torch.preserve_format or
# torch.contiguous_format (got ChannelsLast)
xfail("nn.functional.max_unpool2d"),
# RuntimeError: NYI: Tensor.clone(memory_format) inside vmap is only
# supported with memory_format torch.preserve_format
# or torch.contiguous_format (got ChannelsLast)s
xfail("nn.functional.max_unpool2d", "grad"),
xfail(
"nn.functional.rrelu"
), # RuntimeError: vmap: we do not yet support aten::rrelu_with_noise.
xfail("normal"), # calls random op
xfail("normal", "number_mean"), # calls random op
xfail("pca_lowrank"), # calls random op
xfail(
"quantile", device_type="cpu"
), # Batching rule not implemented for `at::equal`
xfail(
"scatter_reduce", "prod"
), # vmap (looks like you are calling item/data-dependent)
xfail(
"sparse.sampled_addmm"
), # RuntimeError: Sparse CSR tensors do not have strides
xfail(
"sparse.mm", "reduce"
), # RuntimeError: Sparse CSR tensors do not have strides
xfail("svd_lowrank"), # calls random op
xfail("to"), # rank 4 tensor for channels_last
xfail(
"view_as_complex"
), # RuntimeError: Tensor must have a last dimension with stride 1
# got a batched tensor as input while the running_mean or running_var,
# which will be updated in place, were not batched.
xfail("nn.functional.batch_norm", "without_cudnn"),
# view doesn't work on sparse
xfail("to_sparse"),
xfail("native_batch_norm"),
xfail("_native_batch_norm_legit"),
# TODO: implement batching rule
xfail("_batch_norm_with_update"),
decorate("linalg.tensorsolve", decorator=xfailIfS390X),
decorate("nn.functional.max_pool1d", decorator=xfailIfS390X),
decorate("nn.functional.max_unpool2d", decorator=xfailIfS390X),
decorate(
"nn.functional.multilabel_margin_loss", decorator=xfailIfS390X
),
}
),
)
@ops(op_db + additional_op_db + autograd_function_db, allowed_dtypes=(torch.float,))
@toleranceOverride({torch.float32: tol(atol=1e-04, rtol=1e-04)})
@opsToleranceOverride(
"TestOperators",
"test_vmapvjpvjp",
(
tol1("linalg.svd", {torch.float32: tol(atol=1e-03, rtol=5e-04)}),
tol1("linalg.lu", {torch.float32: tol(atol=5e-04, rtol=7e-04)}),
tol1("linalg.lu_factor", {torch.float32: tol(atol=2e-03, rtol=2e-02)}),
tol1("linalg.multi_dot", {torch.float32: tol(atol=2e-03, rtol=2e-04)}),
tol1("svd", {torch.float32: tol(atol=1e-03, rtol=5e-04)}),
tol1("matrix_exp", {torch.float32: tol(atol=1e-03, rtol=5e-04)}),
tol1("masked.prod", {torch.float32: tol(atol=2e-03, rtol=2e-04)}),
),
)
@skipOps(
"TestOperators",
"test_vmapvjpvjp",
{
xfail("as_strided", "partial_views"),
xfail("as_strided_copy"),
},
)
def test_vmapvjpvjp(self, device, dtype, op):
# Since, we test `vjpvjp` independently,
# for this test, we just verify that vmap
# of `vjpvjp` is correct.
if not op.supports_autograd:
self.skipTest("Skipped! Autograd not supported.")
return
if not op.supports_gradgrad:
self.skipTest("Skipped! Operation does not support gradgrad")
return
samples = op.sample_inputs(device, dtype, requires_grad=True)
# TODO: test in-place
if is_inplace(op, op.get_op()):
self.skipTest("Skipped! NYI: inplace-testing not supported.")
return
for sample in samples:
fn, args = get_vjpfull_variant(op, sample)
result = fn(*args)
cotangents = tree_map(lambda x: torch.randn_like(x), result)
cotangents = pytree.tree_leaves(cotangents)
num_args = len(args)
args_and_cotangents = tuple(args) + tuple(cotangents)
def vjp_of_vjp(*args_and_cotangents):
args = args_and_cotangents[:num_args]
cotangents = args_and_cotangents[num_args:]
result, vjp_fn = vjp(fn, *args)
result_vjps = vjp_fn(cotangents)
result = pytree.tree_leaves(result)
result_vjps = pytree.tree_leaves(result_vjps)
return (*result, *result_vjps)
is_batch_norm_and_training = is_batch_norm_training(op.name, sample.kwargs)
generator = get_fallback_and_vmap_exhaustive(
vjp_of_vjp,
args_and_cotangents,
{},
is_batch_norm_and_training=is_batch_norm_and_training,
)
for loop_out, batched_out in generator:
self.assertEqual(loop_out, batched_out)
vmapvjp_fail = vjp_fail.union(
{
# -------------------- ALLOWED FAILURES --------------------------------
# The following are not bugs and are expected behavior
xfail("masked_select"), # Not possible due to dynamic shapes
skip("bernoulli"), # randomness
skip("normal", ""), # randomness
skip("normal", "number_mean"), # randomness
skip("nn.functional.rrelu"), # randomness
skip("nn.functional.feature_alpha_dropout", "with_train"), # randomness
skip("nn.functional.feature_alpha_dropout", "without_train"), # randomness
skip("nn.functional.dropout"), # randomness
skip("nn.functional.dropout2d"), # randomness
skip("nn.functional.dropout3d", ""), # randomness
skip("nn.functional.alpha_dropout"), # randomness
skip("nn.functional.scaled_dot_product_attention"), # randomness
xfail("torch.ops.aten._efficient_attention_forward"), # outputs ints
skip("nn.functional.multi_head_attention_forward"), # randomness
xfail(
"index_put", ""
), # not possible due to dynamic shapes; we support a subset
xfail("nn.functional.fractional_max_pool2d"), # random
xfail("nn.functional.fractional_max_pool3d"), # random
xfail("pca_lowrank", ""), # randomness
xfail("svd_lowrank", ""), # randomness
xfail("to_sparse", ""), # non-dense output
skip(
"to"
), # RuntimeError: required rank 4 tensor to use channels_last format
xfail("as_strided", "partial_views"),
xfail(
"NumpyCubeNotComposableAutogradFunction"
), # Not composable autograd.Function
# ----------------------------------------------------------------------
# ---------------------------- BUGS ------------------------------------
# All of the following are bugs and need to be fixed
skip(
"linalg.svdvals"
), # # really annoying thing where it passes correctness check but not has_batch_rule
skip("native_batch_norm"),
skip("_native_batch_norm_legit"),
# TODO: implement batching rule
skip("_batch_norm_with_update"),
xfail("__getitem__", ""), # dynamic error
xfail("nanquantile", device_type="cpu"), # checks q via a .item() call
xfail("nn.functional.gaussian_nll_loss"), # checks var for if any value < 0
xfail("narrow"), # .item() call
xfail("quantile", device_type="cpu"), # checks q via a .item() call
xfail("view_as_complex"), # Tensor must have a last dimension with stride 1
# required rank 4 tensor to use channels_last format
xfail("bfloat16"),
xfail("double"),
xfail("float"),
xfail("half"),
xfail("cdouble", ""),
xfail("cfloat", ""),
xfail("chalf", ""),
xfail("scatter_reduce", "prod"), # item call
# Batching rule not implemented for aten::_use_cudnn_ctc_loss.Tensor
xfail("nn.functional.ctc_loss", device_type="cuda"),
# NYI: querying is_contiguous inside of vmap for memory_format other than torch.contiguous_format
xfail("nn.functional.max_unpool2d"),
xfail("nn.functional.max_unpool2d", "grad"),
xfail("sparse.sampled_addmm", ""),
xfail("sparse.mm", "reduce"),
xfail("as_strided_scatter", ""), # calls as_strided
xfail("index_reduce", "prod"), # .item() call
# ---------------------------------------------------------------------
}
)
@with_tf32_off # https://github.com/pytorch/pytorch/issues/86798
@ops(op_db + additional_op_db + autograd_function_db, allowed_dtypes=(torch.float,))
@toleranceOverride({torch.float32: tol(atol=1e-04, rtol=1e-04)})
@opsToleranceOverride(
"TestOperators",
"test_vmapvjp",
(
tol1(
"linalg.svd",
{torch.float32: tol(atol=5e-04, rtol=1e-04)},
device_type="cuda",
),
tol1(
"svd", {torch.float32: tol(atol=5e-04, rtol=1e-04)}, device_type="cuda"
),
tol1(
"linalg.householder_product",
{torch.float32: tol(atol=3e-04, rtol=9e-04)},
),
tol1(
"matrix_exp",
{torch.float32: tol(atol=5e-04, rtol=1e-04)},
device_type="cuda",
),
tol1(
"nn.functional.layer_norm",
{torch.float32: tol(atol=3e-4, rtol=1e-4)},
device_type="cpu",
),
tol1(
"native_layer_norm",
{torch.float32: tol(atol=3e-4, rtol=1e-4)},
device_type="cpu",
),
),
)
@skipOps(
"TestOperators",
"test_vmapvjp",
vmapvjp_fail.union(
{
xfail("as_strided"),
xfail("as_strided_copy"),
xfail("as_strided", "partial_views"),
}
),
)
def test_vmapvjp(self, device, dtype, op):
if not op.supports_autograd:
self.skipTest("Skipped! Autograd not supported.")
return
samples = op.sample_inputs(device, dtype, requires_grad=True)
# TODO: test in-place
if is_inplace(op, op.get_op()):
self.skipTest("Skipped! NYI: inplace-testing not supported.")
return
for sample in samples:
cotangents = get_sample_cotangents(op, sample)
fn, args = get_vjp_fn_and_args_with_cotangents(op, sample, cotangents)
is_batch_norm_and_training = is_batch_norm_training(op.name, sample.kwargs)
generator = get_fallback_and_vmap_exhaustive(
fn, args, {}, is_batch_norm_and_training=is_batch_norm_and_training
)
for loop_out, batched_out in generator:
self.assertEqual(loop_out, batched_out)
vmapjvpall_fail = {
# -------------------- ALLOWED FAILURES --------------------------------
# The following are expected (not a bug)
skip("bernoulli", ""), # randomness
skip("nn.functional.dropout"), # randomness
skip("nn.functional.rrelu"), # randomness
skip("nn.functional.dropout2d", ""),
skip("nn.functional.dropout3d", ""),
skip("nn.functional.scaled_dot_product_attention"), # randomness
xfail("torch.ops.aten._efficient_attention_forward"), # outputs ints
skip("nn.functional.multi_head_attention_forward"), # randomness
skip("nn.functional.alpha_dropout"), # randomness
skip("nn.functional.feature_alpha_dropout", "without_train"),
skip("nn.functional.feature_alpha_dropout", "with_train"),
xfail(
"nn.functional.fractional_max_pool2d"
), # Cannot access data pointer of Tensor that doesn't have storage
xfail(
"nn.functional.fractional_max_pool3d"
), # Cannot access data pointer of Tensor that doesn't have storage
# Not actually a problem: embedding with max_norm mutates the weight
# and causes different runs to produce different results.
# skip because this is flaky depending on what the max_norm is!
skip("nn.functional.embedding", ""),
skip("to"), # RuntimeError: required rank 4 tensor to use channels_last format
xfail(
"NumpyExpMarkDirtyAutogradFunction"
), # vmap: inplace into a regular tensor
# ----------------------------------------------------------------------
# ---------------------------- BUGS ------------------------------------
# The following are bugs that we should fix
xfail("masked.mean"), # silent incorrectness (nan difference)
xfail("as_strided", "partial_views"), # Tensor-likes are not close!
xfail(
"nn.functional.soft_margin_loss", ""
), # soft_margin_loss_backward does not support forward-ad
xfail("tensor_split"), # data_ptr composite compliance
xfail("quantile"), # at::equal batching rule (cpu), also, in-place vmap (cuda)
skip("as_strided"), # Test runner cannot handle this
# requires special handling, and does not yet have a batching rule. Feel free to file a github issue!
xfail("as_strided_scatter"),
xfail(
"nn.functional.gaussian_nll_loss"
), # .item or data-dependent control flow
xfail("scatter"), # forward-mode AD does not support at::scatter
xfail(
"nanquantile"
), # at::equal batching rule (cpu), also, in-place vmap (cuda)
xfail("view_as_complex"), # Tensor must have a last dimension with stride 1
skip("pca_lowrank", ""), # randomness
skip("svd_lowrank", ""), # randomness
xfail("double"), # required rank 4 tensor to use channels_last format
xfail("cdouble"), # required rank 4 tensor to use channels_last format
# potential silent incorrectness
skip(
"nn.functional.max_unpool1d"
), # Flaky, seems to sometimes his max_unpool2d
skip("nn.functional.max_unpool2d"), # fails everywhere except on mac
skip("nn.functional.max_unpool3d"), # fails everywhere except on mac
# erroring because running_mean and running_var aren't differentiable
xfail("nn.functional.batch_norm"),
xfail("nn.functional.batch_norm", "without_cudnn"),
xfail("native_batch_norm"),
xfail("_native_batch_norm_legit"),
# TODO: implement batching rule
xfail("_batch_norm_with_update"),
# ----------------------------------------------------------------------
}
@with_tf32_off # https://github.com/pytorch/pytorch/issues/86798
@ops(op_db + additional_op_db + autograd_function_db, allowed_dtypes=(torch.float,))
@toleranceOverride({torch.float32: tol(atol=1e-04, rtol=1e-04)})
@opsToleranceOverride(
"TestOperators",
"test_vmapjvpall",
(
tol1(
"nn.functional.conv_transpose3d",
{torch.float32: tol(atol=2e-04, rtol=9e-3)},
device_type="cuda",
),
tol1(
"linalg.householder_product",
{torch.float32: tol(atol=2e-04, rtol=9e-3)},
),
),
)
@skipOps(
"TestOperators",
"test_vmapjvpall",
vmapjvpall_fail.union(
{
xfail("as_strided_copy"),
decorate(
"linalg.det",
"singular",
decorator=expectedFailureIf(IS_MACOS and IS_X86),
),
}
),
)
# This is technically a superset of test_vmapjvp. We should either delete test_vmapjvp
# or figure out if we can split vmapjvpall. It's useful to keep test_vmapjvp intact
# because that corresponds to "batched forward-mode AD" testing in PyTorch core
def test_vmapjvpall(self, device, dtype, op):
if is_inplace(op, op.get_op()):
# TODO: test in-place
self.skipTest("Skipped! NYI: inplace-testing not supported.")
return
samples = op.sample_inputs(device, dtype, requires_grad=False)
if not op.supports_forward_ad:
self.skipTest("Skipped! Forward AD not supported.")
return
for sample in samples:
arg_values = [sample.input] + list(sample.args)
kwarg_values = sample.kwargs
args = tuple(arg_values) + tuple(kwarg_values)
fn, args = get_jvp_variant_primals_tangents(op, sample)
is_batch_norm_and_training = is_batch_norm_training(op.name, kwarg_values)
generator = get_fallback_and_vmap_exhaustive(
fn, args, {}, is_batch_norm_and_training=is_batch_norm_and_training
)
for loop_out, batched_out in generator:
self.assertEqual(loop_out, batched_out)
@ops(op_db + additional_op_db + autograd_function_db, allowed_dtypes=(torch.float,))
@skipOps(
"TestOperators",
"test_vmapjvpall_has_batch_rule",
vmapjvpall_fail.union(
{
skip(
"to"
), # RuntimeError: required rank 4 tensor to use channels_last format
xfail(
"cdouble"
), # RuntimeError: required rank 4 tensor to use channels_last format
xfail("cumprod"),
xfail("masked_fill"),
xfail("fill"),
skip("masked.mean"), # ???
xfail("masked_scatter"),
xfail("put"),
xfail("take"),
xfail("nn.functional.feature_alpha_dropout", "without_train"),
xfail("nn.functional.dropout2d", ""),
xfail("pca_lowrank", ""),
xfail("svd_lowrank", ""),
xfail("nn.functional.feature_alpha_dropout", "with_train"),
xfail("special.log_ndtr", ""),
xfail("fft.ihfft2"), # conj_physical fallback
xfail("fft.ihfftn"), # conj_physical fallback
xfail("nn.functional.max_unpool3d", "grad"),
xfail("nn.functional.max_unpool2d", "grad"),
xfail("nn.functional.soft_margin_loss", ""),
xfail("nn.functional.max_unpool1d", "grad"),
xfail("nn.functional.embedding", ""),
xfail("nn.functional.glu"),
xfail("nn.functional.bilinear"), # trilinear doesn't have batching rule
xfail("linalg.lu", ""),
xfail("nn.functional.dropout3d", ""),
xfail("as_strided_scatter", ""),
xfail("masked.cumprod", ""),
xfail("permute_copy"),
xfail("renorm"), # hit vmap fallback, which is disabled
xfail("squeeze_copy"),
xfail("t_copy"),
xfail("transpose_copy"),
xfail("unsqueeze_copy"),
}
),
)
@toleranceOverride({torch.float32: tol(atol=1e-04, rtol=1e-04)})
def test_vmapjvpall_has_batch_rule(self, device, dtype, op):
if is_inplace(op, op.get_op()):
# TODO: test in-place
self.skipTest("Skipped! NYI: inplace-testing not supported.")
return
samples = op.sample_inputs(device, dtype, requires_grad=False)
if not op.supports_forward_ad:
self.skipTest("Skipped! Forward AD not supported.")
return
def test():
for sample in samples:
arg_values = [sample.input] + list(sample.args)
kwarg_values = sample.kwargs
args = tuple(arg_values) + tuple(kwarg_values)
fn, args = get_jvp_variant_primals_tangents(op, sample)
is_batch_norm_and_training = is_batch_norm_training(
op.name, kwarg_values
)
for loop_out, batched_out in get_fallback_and_vmap_exhaustive(
fn,
args,
{},
is_batch_norm_and_training=is_batch_norm_and_training,
compute_loop_out=False,
):
pass
check_vmap_fallback(self, test, op, dry_run=False)
@ops(op_db + additional_op_db + autograd_function_db, allowed_dtypes=(torch.float,))
@toleranceOverride({torch.float32: tol(atol=1e-04, rtol=1e-04)})
@skipOps(
"TestOperators",
"test_vmapvjp_has_batch_rule",
vmapvjp_fail.union(
{
skip(
"to"
), # RuntimeError: required rank 4 tensor to use channels_last format
xfail("view_as_complex"),
xfail("cummax"),
xfail("cummin"),
xfail("fill"),
xfail(
"narrow"
), # Batching rule not implemented for `narrow.Tensor` (and view op)
xfail("special.log_ndtr"),
xfail("linalg.householder_product"),
xfail("masked_fill"),
xfail("masked_scatter"),
xfail("masked_select"),
xfail("nanquantile"),
xfail("ormqr"),
xfail("permute_copy"),
xfail("put"),
xfail("quantile"),
xfail("renorm"),
xfail("squeeze_copy"),
xfail("take"),
xfail("tensor_split"),
xfail("to_sparse"),
xfail("unfold"),
xfail("unfold_copy"),
xfail("nn.functional.dropout"),
xfail("fft.ihfft2"),
xfail("fft.ihfftn"),
xfail("nn.functional.gaussian_nll_loss"),
xfail("nn.functional.bilinear"),
xfail("nn.functional.fractional_max_pool3d"),
xfail("nn.functional.ctc_loss"),
xfail("nn.functional.rrelu"),
xfail("nn.functional.embedding_bag"),
xfail("nn.functional.fractional_max_pool2d"),
xfail("nn.functional.feature_alpha_dropout", "with_train"),
xfail("pca_lowrank", ""),
xfail("nn.functional.dropout2d", ""),
xfail("nn.functional.feature_alpha_dropout", "without_train"),
xfail("svd_lowrank", ""),
xfail("nn.functional.max_unpool2d", ""),
xfail("nn.functional.multi_margin_loss", ""),
xfail("nn.functional.multilabel_margin_loss", ""),
xfail("nn.functional.pdist", ""),
xfail("nn.functional.max_unpool1d", ""),
xfail("nn.functional.max_unpool3d", ""),
xfail("nn.functional.max_unpool3d", "grad"),
xfail("nn.functional.soft_margin_loss", ""),
xfail("nn.functional.max_unpool1d", "grad"),
xfail("nn.functional.max_unpool2d", "grad"),
xfail("linalg.lu", ""),
xfail("cdouble", ""),
xfail("cfloat", ""),
xfail("chalf", ""),
xfail(
"index_reduce", "prod"
), # aten::index_reduce hit the vmap fallback which is currently disabled
xfail(
"index_reduce", "mean"
), # aten::index_reduce hit the vmap fallback which is currently disabled
xfail(
"index_reduce", "amax"
), # aten::index_reduce hit the vmap fallback which is currently disabled
xfail(
"index_reduce", "amin"
), # aten::index_reduce hit the vmap fallback which is currently disabled
xfail("nn.functional.dropout3d", ""),
xfail("as_strided_scatter", ""),
xfail("_segment_reduce", "offsets"),
xfail("_segment_reduce", "lengths"),
xfail("sparse.sampled_addmm", ""),
xfail("sparse.mm", "reduce"),
xfail("native_batch_norm"),
xfail("_native_batch_norm_legit"),
# TODO: implement batching rule
xfail("_batch_norm_with_update"),
xfail(
"index_fill"
), # aten::_unique hit the vmap fallback which is currently disabled
xfail("squeeze_copy"),
xfail("t_copy"),
xfail("transpose_copy"),
xfail("unsqueeze_copy"),
}
),
)
def test_vmapvjp_has_batch_rule(self, device, dtype, op):
if not op.supports_autograd:
self.skipTest("Skipped! Autograd not supported.")
return
samples = op.sample_inputs(device, dtype, requires_grad=True)
# TODO: test in-place
if is_inplace(op, op.get_op()):
self.skipTest("Skipped! NYI: inplace-testing not supported.")
return
def test():
for sample in samples:
cotangents = get_sample_cotangents(op, sample)
fn, args = get_vjp_fn_and_args_with_cotangents(op, sample, cotangents)
is_batch_norm_and_training = is_batch_norm_training(
op.name, sample.kwargs
)
for loop_out, batched_out in get_fallback_and_vmap_exhaustive(
fn,
args,
{},
is_batch_norm_and_training=is_batch_norm_and_training,
compute_loop_out=False,
):
pass
for a_op in op.aliases:
fn, args = get_vjp_fn_and_args_with_cotangents(
a_op, sample, cotangents
)
for loop_out, batched_out in get_fallback_and_vmap_exhaustive(
fn,
args,
{},
is_batch_norm_and_training=is_batch_norm_and_training,
compute_loop_out=False,
):
pass
check_vmap_fallback(self, test, op, dry_run=False)
@ops(op_db + additional_op_db + autograd_function_db, allowed_dtypes=(torch.float,))
@skipOps(
"TestOperators",
"test_vjpvmap",
vjp_fail.union(
{
skip("bernoulli", ""), # vjpvmap testing can't handle randomness
skip("normal", ""), # vjpvmap testing can't handle randomness
skip(
"normal", "number_mean"
), # vjpvmap testing can't handle randomness
skip("nn.functional.rrelu"), # randomness
skip("nn.functional.feature_alpha_dropout", "with_train"), # randomness
skip(
"nn.functional.feature_alpha_dropout", "without_train"
), # randomness
skip("nn.functional.scaled_dot_product_attention"),
xfail("torch.ops.aten._efficient_attention_forward"), # outputs ints
skip("nn.functional.multi_head_attention_forward"), # randomness
skip("nn.functional.alpha_dropout"), # randomness
skip(
"to"
), # RuntimeError: required rank 4 tensor to use channels_last format
skip("to_sparse", ""), # non-dense output
skip("ormqr", ""), # takes too long
xfail(
"NumpyCubeNotComposableAutogradFunction"
), # Not composable autograd.Function
# fallback path doesn't work
# All of the following are bugs and need to be fixed
xfail("__getitem__", ""),
xfail("index_put", ""),
xfail("view_as_complex"),
xfail("nn.functional.gaussian_nll_loss"),
xfail("masked_select"),
xfail(
"narrow"
), # Batching rule not implemented for `narrow.Tensor` (and view op)
skip(
"nn.functional.fractional_max_pool3d"
), # generator works on cpu, fails on cuda
skip(
"nn.functional.fractional_max_pool2d"
), # generator works on cpu, fails on cuda
xfail("column_stack", ""),
xfail("nn.functional.dropout2d", ""),
xfail("svd_lowrank", ""),
xfail("pca_lowrank", ""),
xfail("clamp"),
# something weird happening with channels_last
xfail("bfloat16"),
xfail("double"),
xfail("float"),
xfail("half"),
xfail("cdouble"),
xfail("cfloat"),
xfail("nn.functional.dropout3d", ""),
xfail("as_strided_scatter", ""),
xfail("sparse.sampled_addmm", ""),
xfail("sparse.mm", "reduce"),
xfail("native_batch_norm"),
xfail("_native_batch_norm_legit"),
# TODO: implement batching rule
xfail("_batch_norm_with_update"),
xfail("as_strided", "partial_views"),
}
),
)
def test_vjpvmap(self, device, dtype, op):
# NB: there is no vjpvmap_has_batch_rule test because that is almost
# certainly redundant with the vmap_has_batch_rule test in test_vmap.py
# one-off skip
if op.name == "nn.functional.dropout":
self.skipTest("Skipped!")
if not op.supports_autograd:
# If the op doesn't support autograd, vmap(op) won't either
self.skipTest("Skipped! Autograd not supported.")
return
# TODO: test in-place
if is_inplace(op, op.get_op()):
self.skipTest("Skipped! NYI: inplace-testing not supported.")
return
samples = op.sample_inputs(device, dtype, requires_grad=True)
batch_norm_fns = (
"nn.functional.batch_norm",
"nn.functional.instance_norm",
) # instance norm calls batch norm
is_batch_norm = op.name in batch_norm_fns
for sample in samples:
args = [sample.input] + list(sample.args)
kwargs = sample.kwargs
is_batch_norm_and_training = is_batch_norm and is_batch_norm_training(
op.name, kwargs
)
generator = generate_vmap_inputs(
args, kwargs, is_batch_norm_and_training=is_batch_norm_and_training
)
for batched_args, in_dims, kwargs in generator:
vmapped_op = vmap(op, in_dims)
fn, primals = normalize_op_input_output2(
vmapped_op, batched_args, kwargs, sample.output_process_fn_grad
)
result = fn(*primals)
cotangents = tree_map(lambda x: torch.randn_like(x), result)
_, vjp_fn = vjp(fn, *primals)
result_vjps = vjp_fn(cotangents)
_, vjp_fn = ref_vjp(fn, *primals)
expected_vjps = vjp_fn(cotangents)
self.assertEqual(result_vjps, expected_vjps)
def _compare_jacobians_of_vjp(
self, fn, cotangents_and_primals, argnums=None, atol_rtol=None
):
if argnums is None:
argnums = tuple(range(len(cotangents_and_primals)))
def get_vjp(cotangents, *primals):
_, vjp_fn = vjp(fn, *primals)
return vjp_fn(cotangents)
jacobian_jvp = jacfwd(get_vjp, argnums)(*cotangents_and_primals)
jacobian_vjp = jacrev(get_vjp, argnums)(*cotangents_and_primals)
# For dtype changing operations, the jacobians have different dtype.
jacobian_jvp = tree_map(lambda x: x.to(torch.float), jacobian_jvp)
jacobian_vjp = tree_map(lambda x: x.to(torch.float), jacobian_vjp)
if atol_rtol is not None:
(atol, rtol) = atol_rtol
self.assertEqual(jacobian_jvp, jacobian_vjp, atol=atol, rtol=rtol)
else:
self.assertEqual(jacobian_jvp, jacobian_vjp)
@ops(op_db + additional_op_db + autograd_function_db, allowed_dtypes=(torch.float,))
@skipOps(
"TestOperators",
"test_jvpvjp",
vjp_fail.union(
{
xfail("to_sparse", ""), # NYI
# RuntimeError: Trying to set a forward gradient that has a different size than that of the original Tensor,
# this is not supported. Tensor is of size [5, 2, 3] while the given forward gradient is of size [1, 2, 3].
xfail("normal", ""),
xfail("cdist", ""), # NYI: forward-AD for _cdist_forward
xfail("cholesky", ""), # NYI: forward-AD for cholesky
xfail(
"nn.functional.embedding_bag", ""
), # NYI: forward-AD for _embedding_bag
xfail(
"nn.functional.grid_sample", ""
), # NYI: forward AD for grid_sampler_2d
xfail("grid_sampler_2d", ""), # NYI: forward AD for grid_sampler_2d
xfail(
"nn.functional.hardsigmoid", ""
), # NYI: forward AD for hardsigmoid_backward
xfail(
"nn.functional.huber_loss", ""
), # NYI: forward AD for huber_loss_backward
xfail("NumpyCubeNotComposableAutogradFunction"), # not composable
xfail("ormqr", ""), # NYI: forward AD for ormqr
xfail(
"nn.functional.multilabel_margin_loss", ""
), # NYI: multilabel_margin_loss_forward
xfail(
"nn.functional.soft_margin_loss", ""
), # NYI: forward-AD for soft_margin_loss_backward
xfail("nn.functional.ctc_loss", ""), # NYI: forward-AD for _ctc_loss
xfail("nn.functional.pdist", ""), # NYI: forward-AD with _pdist_forward
skip("nn.functional.scaled_dot_product_attention"),
xfail("torch.ops.aten._efficient_attention_forward"), # outputs ints
xfail(
"nn.functional.multi_margin_loss", ""
), # NYI: forward AD with multi_margin_loss
skip(
"linalg.householder_product", "", device_type="cuda"
), # flaky, I'm not sure why
xfail("sparse.sampled_addmm", ""), # Sparse tensors have no strides
xfail(
"_segment_reduce", "offsets"
), # NYI: forward-AD for _segment_reduce
xfail("sparse.mm", "reduce"), # Sparse tensors have no strides
xfail("index_reduce", "prod"), # NYI: forward-AD for index_reduce
xfail("index_reduce", "mean"), # NYI: forward-AD for index_reduce
xfail("index_reduce", "amax"), # NYI: forward-AD for index_reduce
xfail("index_reduce", "amin"), # NYI: forward-AD for index_reduce
xfail(
"_segment_reduce", "lengths"
), # NYI: forward-AD for _segment_reduce
xfail("native_dropout_backward"), # NYI
}
),
)
@opsToleranceOverride(
"TestOperators",
"test_jvpvjp",
(
tol1("masked.prod", {torch.float32: tol(atol=1e-04, rtol=1.3e-05)}),
tol1("masked.cumprod", {torch.float32: tol(atol=1e-04, rtol=5e-04)}),
tol1(
"cumprod",
{torch.float32: tol(atol=1e-03, rtol=5e-04)},
device_type="cuda",
),
tol1(
"linalg.det",
{torch.float32: tol(atol=3e-05, rtol=5e-06)},
device_type="cuda",
),
tol1(
"linalg.vander",
{torch.float32: tol(atol=1e-04, rtol=1.3e-05)},
device_type="cuda",
),
tol1(
"nn.functional.group_norm", {torch.float32: tol(atol=1e-03, rtol=1e-03)}
),
tol2(
"linalg.pinv", "hermitian", {torch.float32: tol(atol=5e-03, rtol=5e-03)}
),
),
)
def test_jvpvjp(self, device, dtype, op):
if not op.supports_autograd:
self.skipTest("Skipped! Autograd not supported.")
return
samples = op.sample_inputs(device, dtype, requires_grad=True)
# TODO: test in-place
if is_inplace(op, op.get_op()):
self.skipTest("Skipped! NYI: inplace-testing not supported.")
return
for sample in samples:
fn, primals = normalize_op_input_output(op, sample)
result = fn(*primals)
cotangents = tree_map(lambda x: torch.randn_like(x), result)
primals_tangents = tree_map(lambda x: torch.randn_like(x), primals)
cotangents_tangents = tree_map(lambda x: torch.randn_like(x), cotangents)
def push_vjp(primals, cotangents):
_, vjp_fn = vjp(fn, *primals)
return vjp_fn(cotangents)
result = jvp(
push_vjp, (primals, cotangents), (primals_tangents, cotangents_tangents)
)
self.assertEqual(len(result), 2)
def tree_map2(fn, first, second):
flat_first, spec_first = tree_flatten(first)
flat_second, spec_second = tree_flatten(second)
assert spec_first == spec_second
flat_result = [fn(f, s) for f, s in zip(flat_first, flat_second)]
return tree_unflatten(flat_result, spec_first)
def reference(primals, cotangents, primals_tangents, cotangents_tangents):
with fwAD.dual_level():
primal_duals = tree_map2(fwAD.make_dual, primals, primals_tangents)
_, vjp_fn = ref_vjp(fn, *primal_duals)
cotangent_duals = tree_map2(
fwAD.make_dual, cotangents, cotangents_tangents
)
result = vjp_fn(cotangent_duals)
flat_result, spec = tree_flatten(result)
primals_out, tangents_out = zip(
*[fwAD.unpack_dual(r) for r in flat_result]
)
tangents_out = [
t if t is not None else torch.zeros_like(p)
for p, t in zip(primals_out, tangents_out)
]
expected = (
tree_unflatten(primals_out, spec),
tree_unflatten(tangents_out, spec),
)
return expected
expected = reference(
primals, cotangents, primals_tangents, cotangents_tangents
)
self.assertEqual(result, expected)
@with_tf32_off # https://github.com/pytorch/pytorch/issues/86798
@skipOps(
"TestOperators",
"test_vmapjvpvjp",
vjp_fail.union(
{
# Following operators take too long, hence skipped
skip("atleast_1d"),
skip("atleast_2d"),
skip("atleast_3d"),
skip("meshgrid", "list_of_tensors"),
skip("meshgrid", "variadic_tensors"),
skip("broadcast_tensors"),
skip("linalg.lstsq"),
skip("nn.functional.bilinear"),
skip("native_layer_norm"),
skip("ormqr"),
# Not actually a problem
xfail("NumpyCubeNotComposableAutogradFunction"), # not composable
xfail(
"NumpyExpMarkDirtyAutogradFunction"
), # vmap: inplace into a regular tensor
# Potential bugs/errors
xfail("as_strided"), # AssertionError: Tensor-likes are not close!
xfail(
"as_strided", "partial_views"
), # AssertionError: Tensor-likes are not close!
xfail("as_strided_copy"), # AssertionError: Tensor-likes are not close!
xfail(
"as_strided_scatter"
), # AssertionError: Tensor-likes are not close!
xfail("bernoulli"), # calls random op
xfail("bfloat16"), # required rank 4 tensor to use channels_last format
xfail("cdist"), # Forward AD not implemented and no decomposition
xfail("cdouble"), # required rank 4 tensor to use channels_last format
xfail("cfloat"), # required rank 4 tensor to use channels_last format
xfail("chalf"), # required rank 4 tensor to use channels_last format
xfail("cholesky"), # Forward AD not implemented and no decomposition
xfail("ormqr"), # Forward AD not implemented and no decomposition
xfail("double"), # required rank 4 tensor to use channels_last format
xfail("float"), # required rank 4 tensor to use channels_last format
xfail("half"), # required rank 4 tensor to use channels_last format
xfail("index_reduce", "prod"), # NYI: forward AD for index_reduce
xfail("index_reduce", "mean"), # NYI: forward AD for index_reduce
xfail("index_reduce", "amax"), # NYI: forward AD for index_reduce
xfail("index_reduce", "amin"), # NYI: forward AD for index_reduce
xfail(
"mvlgamma", "mvlgamma_p_1"
), # vmap: inplace into a regular tensor
xfail(
"mvlgamma", "mvlgamma_p_3"
), # vmap: inplace into a regular tensor
xfail(
"mvlgamma", "mvlgamma_p_5"
), # vmap: inplace into a regular tensor
xfail("nanquantile"), # Batching rule not implemented for aten::equal
# RuntimeError: Batch norm got a batched tensor as input while the
# running_mean or running_var, which will be updated in place,
# were not batched.
xfail("nn.functional.batch_norm"),
xfail("nn.functional.batch_norm", "without_cudnn"),
xfail(
"nn.functional.ctc_loss"
), # ForwardAD not implemented and no decomposition
xfail("nn.functional.dropout2d"), # calls random op
xfail("nn.functional.dropout3d"), # calls random op
xfail("nn.functional.dropout"), # calls random op
xfail("nn.functional.scaled_dot_product_attention"), # randomness
xfail("torch.ops.aten._efficient_attention_forward"), # outputs ints
xfail("nn.functional.multi_head_attention_forward"), # randomness
xfail(
"nn.functional.embedding_bag"
), # Forward AD not implemented and no decomposition
xfail("nn.functional.alpha_dropout"), # calls randomn op
xfail(
"nn.functional.feature_alpha_dropout", "with_train"
), # calls random op
xfail("nn.functional.fractional_max_pool2d"), # calls random op
xfail("nn.functional.fractional_max_pool3d"), # calls random op
xfail("nn.functional.gaussian_nll_loss"), # data depenedant flow
xfail(
"nn.functional.grid_sample"
), # Forward AD not implemented and no decomposition
xfail(
"grid_sampler_2d"
), # Forward AD not implemented and no decomposition
xfail(
"nn.functional.hardsigmoid"
), # Forward AD not implemented and no decomposition
xfail(
"nn.functional.hinge_embedding_loss"
), # vmap: inplace into a regular tensor
xfail(
"nn.functional.huber_loss"
), # Forward AD not implemented and no decomposition
# RuntimeError: Batch norm got a batched tensor as input while the
# running_mean or running_var, which will be updated in place,
# were not batched.
xfail("nn.functional.instance_norm"),
# NYI: Tensor.clone(memory_format) inside vmap is only supported with
# memory_format torch.preserve_format or torch.contiguous_format (got ChannelsLast)
xfail("nn.functional.max_unpool2d"),
xfail("nn.functional.max_unpool2d", "grad"),
xfail(
"nn.functional.multi_margin_loss"
), # Forward AD not implemented and no decomposition
xfail(
"nn.functional.multilabel_margin_loss"
), # Forward AD not implemented and no decomposition
xfail(
"nn.functional.pdist"
), # Forward AD not implemented and no decomposition
xfail(
"nn.functional.rrelu"
), # vmap: we do not yet support aten::rrelu_with_noise.
xfail(
"nn.functional.soft_margin_loss"
), # Forward AD not implemented and no decomposition
xfail("normal"), # calls random op
xfail("normal", "number_mean"), # calls random op
xfail("pca_lowrank"), # calls random op
xfail("quantile"), # Batching rule not implemented for aten::equal
xfail(
"scatter_reduce", "prod"
), # Forward AD not implemented and no decomposition
xfail(
"_segment_reduce", "lengths"
), # Forward AD not implemented and no decomposition
xfail(
"_segment_reduce", "offsets"
), # Forward AD not implemented and no decomposition
xfail(
"sparse.sampled_addmm"
), # RuntimeError: Sparse CSR tensors do not have strides
xfail(
"sparse.mm", "reduce"
), # RuntimeError: Sparse CSR tensors do not have strides
xfail("svd_lowrank"), # calls random op
xfail(
"to"
), # RuntimeError: required rank 4 tensor to use channels_last format
xfail("to_sparse"), # Forward AD not implemented and no decomposition
xfail(
"view_as_complex"
), # RuntimeError: Tensor must have a last dimension with stride 1
# RuntimeError: Batch norm got a batched tensor as
# input while the running_mean or running_var, which will be updated in
# place, were not batched.
xfail("native_batch_norm"),
xfail("_native_batch_norm_legit"),
# TODO: implement batching rule
xfail("_batch_norm_with_update"),
xfail("native_dropout_backward"),
}
),
)
@ops(op_db + additional_op_db + autograd_function_db, allowed_dtypes=(torch.float,))
@toleranceOverride({torch.float32: tol(atol=1e-04, rtol=1e-04)})
@opsToleranceOverride(
"TestOperators",
"test_vmapjvpvjp",
(
tol1("linalg.svd", {torch.float32: tol(atol=5e-04, rtol=5e-04)}),
tol1(
"linalg.householder_product",
{torch.float32: tol(atol=5e-03, rtol=5e-03)},
),
tol1("linalg.multi_dot", {torch.float32: tol(atol=5e-04, rtol=5e-04)}),
tol2(
"linalg.pinv", "hermitian", {torch.float32: tol(atol=5e-04, rtol=5e-04)}
),
tol1(
"nn.functional.conv_transpose2d",
{torch.float32: tol(atol=5e-04, rtol=5e-04)},
),
tol1("svd", {torch.float32: tol(atol=5e-04, rtol=5e-04)}),
tol1("matrix_exp", {torch.float32: tol(atol=5e-04, rtol=5e-04)}),
),
)
def test_vmapjvpvjp(self, device, dtype, op):
# Since we test `jvpvjp` separately,
# in this we just check that vmap of `jvpvjp`
# is correct.
if not op.supports_autograd:
self.skipTest("Skipped! Autograd not supported.")
return
samples = op.sample_inputs(device, dtype, requires_grad=True)
# TODO: test in-place
if is_inplace(op, op.get_op()):
self.skipTest("Skipped! NYI: inplace-testing not supported.")
return
for sample in samples:
fn, primals = normalize_op_input_output(op, sample)
result = fn(*primals)
cotangents = tree_map(lambda x: torch.randn_like(x), result)
primals_tangents = tree_map(lambda x: torch.randn_like(x), primals)
cotangents_tangents = tree_map(lambda x: torch.randn_like(x), cotangents)
def push_vjp(primals, cotangents):
_, vjp_fn = vjp(fn, *primals)
return vjp_fn(cotangents)
args, spec = tree_flatten(
((primals, cotangents), (primals_tangents, cotangents_tangents))
)
def jvp_of_vjp(*args):
(primals, tangents) = tree_unflatten(args, spec)
primals_out, tangents_out = jvp(push_vjp, primals, tangents)
flat_primals_out = pytree.tree_leaves(primals_out)
flat_tangents_out = pytree.tree_leaves(tangents_out)
return tuple(flat_primals_out + flat_tangents_out)
is_batch_norm_and_training = is_batch_norm_training(op, sample.kwargs)
generator = get_fallback_and_vmap_exhaustive(
jvp_of_vjp,
args,
{},
is_batch_norm_and_training=is_batch_norm_and_training,
)
for loop_out, batched_out in generator:
self.assertEqual(loop_out, batched_out)
def _make_extremal_inputs(self, shape, device):
if shape is None:
return (None,)
return (
torch.full(shape, -1000.0, device=device),
torch.zeros(shape, device=device),
torch.full(shape, 1000.0, device=device),
)
def _arg_and_kwarg_options(self, args_options, kwargs_options):
return itertools.product(*args_options, kwargs_options)
def test_extremal_numerics_nll_loss(self, device):
N, C = 3, 4
d1, d2, d3 = 5, 6, 7
shapes = (
((N, C), (N,), (C,)),
((N, C), (N,), None),
((N, C, d1, d2, d3), (N, d1, d2, d3), (C,)),
((N, C, d1, d2, d3), (N, d1, d2, d3), None),
)
kwargs_options = (
{"ignore_index": 0, "reduction": "mean"},
{"reduction": "sum"},
{"reduction": "none"},
{},
)
for input_shape, target_shape, weight_shape in shapes:
input_options = self._make_extremal_inputs(input_shape, device)
for input, kwargs in self._arg_and_kwarg_options(
(input_options,), kwargs_options
):
if weight_shape is None:
weight = None
else:
weight = torch.randn(weight_shape, device=device)
target = torch.randint(0, C, target_shape, device=device)
target[
0
] = 1 # since we're ignoring index 0, at least one element must be non-zero
fn = functools.partial(
torch.nn.functional.nll_loss, target=target, weight=weight, **kwargs
)
result = fn(input)
cotangents = torch.randn_like(result, device=device)
self._compare_jacobians_of_vjp(fn, (cotangents, input))
def test_extremal_numerics_l1_loss(self, device):
N, C, H, W = 3, 4, 5, 6
shapes = ((N, C), (N, C, H), (N, C, H, W))
kwargs_options = ({"reduction": "sum"}, {"reduction": "none"}, {})
for shape in shapes:
input_options = self._make_extremal_inputs(shape, device)
target_options = self._make_extremal_inputs(shape, device)
for input, target, kwargs in self._arg_and_kwarg_options(
(input_options, target_options), kwargs_options
):
result = torch.nn.functional.l1_loss(input, target)
cotangents = torch.randn_like(result, device=device)
self._compare_jacobians_of_vjp(
torch.nn.functional.l1_loss, (cotangents, input, target)
)
def test_extremal_numerics_mse_loss(self, device):
N, C, H, W = 3, 4, 5, 6
shapes = ((N, C), (N, C, H), (N, C, H, W))
kwargs_options = ({"reduction": "sum"}, {"reduction": "none"}, {})
for shape in shapes:
input_options = self._make_extremal_inputs(shape, device)
target_options = self._make_extremal_inputs(shape, device)
for input, target, kwargs in self._arg_and_kwarg_options(
(input_options, target_options), kwargs_options
):
result = torch.nn.functional.mse_loss(input, target)
cotangents = torch.randn_like(result, device=device)
self._compare_jacobians_of_vjp(
torch.nn.functional.mse_loss, (cotangents, input, target)
)
def test_extremal_numerics_softmax(self, device):
N, C, H, W = 3, 4, 5, 6
shapes = ((N, C), (N, C, H), (N, C, H, W))
kwargs_options = ({"dim": 1}, {})
for shape in shapes:
input_options = self._make_extremal_inputs(shape, device)
for input, kwargs in self._arg_and_kwarg_options(
(input_options,), kwargs_options
):
result = torch.nn.functional.softmax(input)
cotangents = torch.randn_like(result, device=device)
self._compare_jacobians_of_vjp(
torch.nn.functional.softmax, (cotangents, input)
)
def test_extremal_numerics_log_softmax(self, device):
N, C, H, W = 3, 4, 5, 6
shapes = ((N, C), (N, C, H), (N, C, H, W))
kwargs_options = ({"dim": 1}, {})
for shape in shapes:
input_options = self._make_extremal_inputs(shape, device)
for input, kwargs in self._arg_and_kwarg_options(
(input_options,), kwargs_options
):
result = torch.nn.functional.log_softmax(input)
cotangents = torch.randn_like(result, device=device)
self._compare_jacobians_of_vjp(
torch.nn.functional.log_softmax, (cotangents, input)
)
def test_extremal_numerics_cross_entropy(self, device):
N, C = 3, 4
d1, d2, d3 = 5, 6, 7
shapes = (
((N, C), (N,), (C,)),
((N, C), (N,), None),
((N, C), (N, C), (C,)),
((N, C), (N, C), None),
((C,), (), (C,)),
((C,), (), None),
((C,), (C,), (C,)),
((C,), (C,), None),
((N, C, d1, d2, d3), (N, d1, d2, d3), (C,)),
((N, C, d1, d2, d3), (N, d1, d2, d3), None),
((N, C, d1, d2, d3), (N, C, d1, d2, d3), (C,)),
((N, C, d1, d2, d3), (N, C, d1, d2, d3), None),
)
for input_shape, target_shape, weight_shape in shapes:
input_options = self._make_extremal_inputs(input_shape, device)
kwargs_options = [{"reduction": "sum"}, {"reduction": "none"}, {}]
if input_shape != target_shape:
kwargs_options.append({"ignore_index": 0, "reduction": "mean"})
for input, kwargs in self._arg_and_kwarg_options(
(input_options,), kwargs_options
):
if weight_shape is None:
weight = None
else:
weight = torch.randn(weight_shape, device=device)
if input_shape == target_shape:
target = torch.rand(target_shape, device=device)
elif len(target_shape) == 0:
target = torch.tensor(
1, device=device
) # must be non-zero since ignore_index may be 0
else:
target = torch.randint(0, C, target_shape, device=device)
fn = functools.partial(
torch.nn.functional.cross_entropy,
target=target,
weight=weight,
**kwargs,
)
result = fn(input)
cotangents = torch.randn_like(result, device=device)
self._compare_jacobians_of_vjp(
fn, (cotangents, input), atol_rtol=(1e-4, 1e-5)
)
def test_extremal_numerics_binary_cross_entropy(self, device):
N, C, H, W = 3, 4, 5, 6
shapes = ((N, C), (N, C, H), (N, C, H, W))
for shape in shapes:
weight_options = self._make_extremal_inputs(shape, device)
kwargs_options = [{"reduction": "sum"}, {"reduction": "none"}, {}]
for weight, kwargs in self._arg_and_kwarg_options(
(weight_options,), kwargs_options
):
input = torch.rand(shape, device=device)
target = torch.rand(shape, device=device)
fn = functools.partial(
torch.nn.functional.binary_cross_entropy,
target=target,
weight=weight,
**kwargs,
)
result = fn(input)
cotangents = torch.randn_like(result, device=device)
self._compare_jacobians_of_vjp(
fn, (cotangents, input), atol_rtol=(1e-4, 2e-5)
)
def test_extremal_numerics_layer_norm(self, device):
N, C, H, W = 3, 4, 5, 6
shapes = ((N, C), (N, C, H), (N, C, H, W))
for shape in shapes:
input_options = self._make_extremal_inputs(shape, device)
normalized_shape = shape[1:]
weight_options = self._make_extremal_inputs(normalized_shape, device)
bias_options = self._make_extremal_inputs(normalized_shape, device)
for input, bias, weight in self._arg_and_kwarg_options(
(input_options, bias_options, weight_options), ()
):
def fn(input, weight, bias):
return torch.nn.functional.layer_norm(
input, normalized_shape, weight=weight, bias=bias
)
result = fn(input, weight, bias)
cotangents = torch.randn_like(result, device=device)
self._compare_jacobians_of_vjp(fn, (cotangents, input, weight, bias))
@with_tf32_off # https://github.com/pytorch/pytorch/issues/86798
@ops(
op_db + additional_op_db + autograd_function_db,
allowed_dtypes=(torch.float32, torch.double),
)
@skipOps(
"TestOperators",
"test_vmap_autograd_grad",
{
# The size of tensor a (4) must match the size of tensor b (10) at non-singleton dimension 0
xfail("masked_select"),
xfail("nn.functional.max_unpool2d", "grad"), # contiguous call
xfail("nn.functional.max_unpool2d"), # contiguous call
xfail("to_sparse"), # dispatch key issue
xfail("torch.ops.aten._efficient_attention_forward"), # outputs ints
# https://github.com/pytorch/pytorch/issues/96560#issuecomment-2151063723
# ** minor accuracy issue for float32 on ROCm
decorate("xlogy", decorator=skipIfRocm),
# numerical inconsistencies, look like bugs
skip(
"matrix_exp", dtypes=(torch.float32,), device_type="cuda"
), # fails on linux, passes on windows
skip(
"ldexp", dtypes=(torch.float32,), device_type="cpu"
), # fails on all but mac
skip("__rmatmul__"), # flaky needs investigation
skip("matmul"), # flaky needs investigation
skip("nn.functional.conv_transpose3d"), # flaky needs investigation
skip("nn.functional.conv_transpose2d"), # flaky needs investigation
skip("nn.functional.conv_transpose1d"), # flaky needs investigation
skip(
"nn.functional.layer_norm", dtypes=(torch.float32,), device_type="cpu"
), # fails on windows
skip(
"linalg.lu_factor", dtypes=(torch.float32,), device_type="cuda"
), # fails on all but windows
skip(
"linalg.lu_factor_ex", dtypes=(torch.float32,), device_type="cuda"
), # fails on all but windows
skip("linalg.multi_dot", "", device_type="cpu"),
skip("sparse.sampled_addmm", ""),
skip("sparse.mm", "reduce"),
skip("native_layer_norm", "", device_type="cpu"),
# RuntimeError: Expected contiguous tensor, but got
# non-contiguous tensor for argument #2 'grad_output'
decorate(
"_batch_norm_with_update",
decorator=expectedFailureIf(TEST_WITH_ROCM),
device_type="cuda",
),
},
)
@opsToleranceOverride(
"TestOperators",
"test_vmap_autograd_grad",
(
tol1(
"ldexp",
{torch.float32: tol(atol=3e-04, rtol=1.6e-06)},
device_type="cuda",
),
tol1(
"linalg.householder_product",
{torch.float32: tol(atol=5e-04, rtol=9e-03)},
device_type="cuda",
),
tol1(
"linalg.householder_product",
{torch.float32: tol(atol=6e-03, rtol=1e-03)},
device_type="cpu",
),
tol1(
"linalg.multi_dot",
{torch.float32: tol(atol=2e-04, rtol=1e-04)},
device_type="cuda",
),
tol2(
"linalg.pinv", "hermitian", {torch.float32: tol(atol=5e-06, rtol=5e-06)}
),
tol1("nn.functional.conv3d", {torch.float32: tol(atol=5e-04, rtol=9e-03)}),
tol1(
"nn.functional.conv2d",
{torch.float32: tol(atol=5e-05, rtol=5e-05)},
device_type="cuda",
),
tol1("svd_lowrank", {torch.float32: tol(atol=5e-05, rtol=5e-05)}),
tol1("pca_lowrank", {torch.float32: tol(atol=5e-05, rtol=5e-05)}),
),
)
def test_vmap_autograd_grad(self, device, dtype, op):
def is_differentiable(inp):
return isinstance(inp, Tensor) and (
inp.grad_fn is not None or inp.requires_grad
)
def get_flat_differentiable(tree):
flattened = pytree.tree_leaves(tree)
return tuple(i for i in flattened if is_differentiable(i))
def get_differentiable_linked(list1, list2):
paired_list = zip(list1, list2)
paired_list = tuple(
(first, second)
for (first, second) in paired_list
if is_differentiable(first)
)
return zip(*paired_list)
def filter_none(out):
flattened = pytree.tree_leaves(out)
return tuple(o for o in flattened if o is not None)
if not op.supports_autograd:
self.skipTest("Skipped! Autograd not supported.")
return
sample_inputs = op.sample_inputs(device, dtype, requires_grad=True)
for sample_input in sample_inputs:
fn, primals = normalize_op_input_output(op, sample_input)
out = fn(*primals)
cotangents = tree_map(torch.randn_like, out)
def compute_grad(cotangents):
out_flattened = out
cotangents_flattened = cotangents
if not isinstance(out_flattened, torch.Tensor):
out_flattened = pytree.tree_leaves(out)
cotangents_flattened = pytree.tree_leaves(cotangents)
out_flattened, cotangents_flattened = get_differentiable_linked(
out_flattened, cotangents_flattened
)
return filter_none(
torch.autograd.grad(
out_flattened,
get_flat_differentiable(primals),
cotangents_flattened,
retain_graph=True,
allow_unused=True,
)
)
is_batch_norm_and_training = is_batch_norm_training(op, sample_input.kwargs)
generator = get_fallback_and_vmap_exhaustive(
compute_grad,
(cotangents,),
{},
is_batch_norm_and_training=is_batch_norm_and_training,
)
for loop_out, batched_out in generator:
self.assertEqual(loop_out, batched_out)
def test_vmapvmapjvp_linalg_solve(self):
ops = [op for op in op_db if op.name == "linalg.solve"]
assert len(ops) > 0
# this specializes a lot of code from the get_fallback_and_vmap_exhaustive test. If we need this more
# generally, this could go for a refactor
B0 = 2
B1 = 3
# we want to check the case where A will be seen as contiguous by jvp but during the vmap calls will become
# non-contiguous because vmap will expand. This will happen during both levels of vmap
A = torch.randn(4, 4)
k = torch.randn(4, 5, B1, B0)
fn, args = get_jvp_variant_primals_tangents(
torch.linalg.solve, SampleInput(A, args=(k,))
)
in_dims_all = (None, -1, None, -1)
batched_out = vmap(vmap(fn, in_dims=in_dims_all), in_dims=in_dims_all)(*args)
loop_out = loop2(fn, in_dims_all, in_dims_all, 0, 0, B0, B1, *args)
self.assertEqual(loop_out, batched_out)
@ops(
filter(lambda op: op.name in aliasing_ops, op_db + additional_op_db),
allowed_dtypes=(torch.float,),
)
@parametrize("grad_op", ["jvp", "vjp"])
def test_view_then_inplace(self, device, dtype, op, grad_op):
for sample_input in op.sample_inputs(device, dtype):
def f(x):
op(sample_input.input, *sample_input.args, **sample_input.kwargs).copy_(
x
)
return x
without_grad = op(
sample_input.input, *sample_input.args, **sample_input.kwargs
)
if grad_op == "jvp":
with self.assertRaisesRegex(
RuntimeError,
"During a grad .* attempted to call in-place operation",
):
jvp(
f,
(torch.randn_like(without_grad),),
(torch.randn_like(without_grad),),
)
else:
assert grad_op == "vjp"
with self.assertRaisesRegex(
RuntimeError,
"During a grad .* attempted to call in-place operation",
):
vjp(f, torch.randn_like(without_grad))
@ops(
filter(
lambda op: op.name in aliasing_ops_list_return, op_db + additional_op_db
),
allowed_dtypes=(torch.float,),
)
@parametrize("grad_op", ["jvp", "vjp"])
def test_view_then_inplace_list_return(self, device, dtype, op, grad_op):
for sample_input in op.sample_inputs(device, dtype):
def f(x):
op(sample_input.input, *sample_input.args, **sample_input.kwargs)[
0
].copy_(x)
return x
without_grad = op(
sample_input.input, *sample_input.args, **sample_input.kwargs
)[0]
with self.assertRaisesRegex(
RuntimeError, "During a grad .* attempted to call in-place operation"
):
if grad_op == "jvp":
jvp(
f,
(torch.randn_like(without_grad),),
(torch.randn_like(without_grad),),
)
else:
assert grad_op == "vjp"
vjp(f, torch.randn_like(without_grad))
@parametrize("grad_op", ["jvp", "vjp"])
def test_view_then_inplace_special(self, grad_op):
# some things in __getitem__ use at::index, which doesn't alias, so this tests a subset of them that do alias
ops = [
lambda x: x[0],
lambda x: x[0, 0, 0],
lambda x: x[:1],
lambda x: x[:, :1],
lambda x: x[:, :1, :],
]
for op in ops:
def f(x):
op(captured).copy_(x)
return x
captured = torch.randn(4, 3, 3)
without_grad = op(captured)
if grad_op == "jvp":
with self.assertRaisesRegex(
RuntimeError,
"During a grad .* attempted to call in-place operation",
):
jvp(
f,
(torch.randn_like(without_grad),),
(torch.randn_like(without_grad),),
)
else:
assert grad_op == "vjp"
with self.assertRaisesRegex(
RuntimeError,
"During a grad .* attempted to call in-place operation",
):
vjp(f, torch.randn_like(without_grad))
@with_tf32_off # https://github.com/pytorch/pytorch/issues/86798
# NOTE: [three-transform testing]
# We only test the autograd_function_db tests here.
#
# Usually testing the composition of two transforms is sufficient to convince
# ourselves that an operator is correctly implemented. For the following cases,
# we want to be extra sure, so we send those through some three-transform tests:
# - autograd.Function. The mechanism is via PyDispatcher/HigherOrderOperator, not the
# regular PyTorch dispatcher, so it's good to exercise more caution.
@ops(autograd_function_db, allowed_dtypes=(torch.float32,))
@skipOps(
"TestOperators",
"test_vmapvjpvmap",
{
xfail("NumpyCubeNotComposableAutogradFunction"), # Not composable
},
)
def test_vmapvjpvmap(self, device, dtype, op):
samples = op.sample_inputs(device, dtype, requires_grad=True)
B = 2
for sample in samples:
args = [sample.input] + list(sample.args)
kwargs = sample.kwargs
generator = generate_vmap_inputs(args, kwargs, batch_size=B)
for batched_args, in_dims, kwargs in generator:
inner_vmapped_op = vmap(op, in_dims)
inner_mapped_op = functools.partial(loop, op, in_dims, 0, B)
inner_vmapped_fn, primals = normalize_op_input_output2(
inner_vmapped_op,
batched_args,
kwargs,
sample.output_process_fn_grad,
)
inner_mapped_fn, _ = normalize_op_input_output2(
inner_mapped_op, batched_args, kwargs, sample.output_process_fn_grad
)
result = inner_mapped_fn(*primals)
cotangents = tree_map(lambda x: torch.rand_like(x), result)
def apply_vjp(fn):
def inner(primals, cotangents):
_, vjp_fn = vjp(fn, *primals)
return vjp_fn(cotangents)
return inner
vjpvmap_fn = apply_vjp(inner_vmapped_fn)
vjpmap_fn = apply_vjp(inner_mapped_fn)
batched_args = (primals, cotangents)
generator = generate_vmap_inputs(batched_args, {})
for batched_args, in_dims, _ in generator:
# strategy: compare vmap(vjp(vmap(op)) vs map(vjp(map(op))
vmapvjpvmap_fn = vmap(vjpvmap_fn, in_dims)
mapvjpmap_fn = functools.partial(loop, vjpmap_fn, in_dims, 0, B)
result = vmapvjpvmap_fn(*batched_args)
expected = mapvjpmap_fn(*batched_args)
self.assertEqual(result, expected)
# See NOTE: [three-transform testing]
@ops(autograd_function_db, allowed_dtypes=(torch.float32,))
@skipOps(
"TestOperators",
"test_vjpvmapvmap",
{
xfail("NumpyCubeNotComposableAutogradFunction"), # Not composable
},
)
def test_vjpvmapvmap(self, device, dtype, op):
samples = op.sample_inputs(device, dtype, requires_grad=True)
B = 2
for sample in samples:
args = [sample.input] + list(sample.args)
kwargs = sample.kwargs
generator = generate_vmap_inputs(args, kwargs, batch_size=B)
for batched_args, inner_in_dims, kwargs in generator:
inner_vmapped_op = vmap(op, inner_in_dims)
inner_mapped_op = functools.partial(loop, op, inner_in_dims, 0, B)
generator = generate_vmap_inputs(batched_args, kwargs)
for batched_args, in_dims, kwargs in generator:
# strategy: compare vjp(vmap(vmap(op)) vs vjp(map(map(op))
vmapped_op = vmap(inner_vmapped_op, in_dims)
mapped_op = functools.partial(loop, inner_mapped_op, in_dims, 0, B)
vmapped_fn, primals = normalize_op_input_output2(
vmapped_op, batched_args, kwargs, sample.output_process_fn_grad
)
mapped_fn, _ = normalize_op_input_output2(
mapped_op, batched_args, kwargs, sample.output_process_fn_grad
)
result = mapped_fn(*primals)
cotangents = tree_map(lambda x: torch.rand_like(x), result)
_, vjp_fn = vjp(mapped_fn, *primals)
expected_vjps = vjp_fn(cotangents)
_, vjp_fn = vjp(vmapped_fn, *primals)
result_vjps = vjp_fn(cotangents)
self.assertEqual(result_vjps, expected_vjps)
# See NOTE: [three-transform testing]
@ops(autograd_function_db, allowed_dtypes=(torch.float32,))
@skipOps(
"TestOperators",
"test_vjpvjpvmap",
{
xfail("NumpyCubeNotComposableAutogradFunction"), # Not composable
},
)
def test_vjpvjpvmap(self, device, dtype, op):
samples = op.sample_inputs(device, dtype, requires_grad=True)
B = 2
for sample in samples:
args = [sample.input] + list(sample.args)
kwargs = sample.kwargs
generator = generate_vmap_inputs(args, kwargs, batch_size=B)
for batched_args, in_dims, kwargs in generator:
inner_vmapped_op = vmap(op, in_dims)
inner_mapped_op = functools.partial(loop, op, in_dims, 0, B)
vjpmap_fn, args = get_vjpfull_variant2(
inner_mapped_op, batched_args, kwargs
)
vjpvmap_fn, _ = get_vjpfull_variant2(
inner_vmapped_op, batched_args, kwargs
)
vjpvjpvmap_fn, new_args = get_vjpfull_variant2(vjpvmap_fn, args, {})
vjpvjpmap_fn, _ = get_vjpfull_variant2(vjpmap_fn, args, {})
expected = vjpvjpmap_fn(*new_args)
result = vjpvjpvmap_fn(*new_args)
self.assertEqual(result, expected)
# We're generally convinced that jvp x vmap works (vmap turns an operator
# into another operator and we test jvp support for operators). So
# we only test it on the things we're not sure about:
# - the autograd.Function <> functorch interaction
@ops(autograd_function_db, allowed_dtypes=(torch.float32,))
@skipOps(
"TestOperators",
"test_jvpvmap",
{
xfail("NumpyCubeNotComposableAutogradFunction"), # Not composable
},
)
def test_jvpvmap(self, device, dtype, op):
samples = op.sample_inputs(device, dtype, requires_grad=True)
B = 2
for sample in samples:
args = [sample.input] + list(sample.args)
kwargs = sample.kwargs
generator = generate_vmap_inputs(args, kwargs, batch_size=B)
for batched_args, in_dims, kwargs in generator:
inner_vmapped_op = vmap(op, in_dims)
inner_mapped_op = functools.partial(loop, op, in_dims, 0, B)
jvpvmap_op, primals = get_jvp_variant_primals_tangents2(
inner_vmapped_op,
batched_args,
kwargs,
sample.output_process_fn_grad,
)
jvpmap_op, _ = get_jvp_variant_primals_tangents2(
inner_mapped_op, batched_args, kwargs, sample.output_process_fn_grad
)
expected = jvpmap_op(*primals)
result = jvpvmap_op(*primals)
self.assertEqual(result, expected)
# See NOTE: [three-transform testing]
@ops(autograd_function_db, allowed_dtypes=(torch.float32,))
@skipOps(
"TestOperators",
"test_jvpvmapvmap",
{
xfail("NumpyCubeNotComposableAutogradFunction"), # Not composable
},
)
def test_jvpvmapvmap(self, device, dtype, op):
samples = op.sample_inputs(device, dtype, requires_grad=True)
B = 2
for sample in samples:
args = [sample.input] + list(sample.args)
kwargs = sample.kwargs
generator = generate_vmap_inputs(args, kwargs, batch_size=B)
for batched_args, inner_in_dims, kwargs in generator:
inner_vmapped_op = vmap(op, inner_in_dims)
inner_mapped_op = functools.partial(loop, op, inner_in_dims, 0, B)
generator = generate_vmap_inputs(batched_args, kwargs)
for batched_args, in_dims, kwargs in generator:
# strategy: compare jvp(vmap(vmap(op)) vs jvp(map(map(op))
vmapped_op = vmap(inner_vmapped_op, in_dims)
mapped_op = functools.partial(loop, inner_mapped_op, in_dims, 0, B)
jvpvmapvmap_fn, primals = get_jvp_variant_primals_tangents2(
vmapped_op, batched_args, kwargs, sample.output_process_fn_grad
)
jvpmapmap_fn, _ = get_jvp_variant_primals_tangents2(
mapped_op, batched_args, kwargs, sample.output_process_fn_grad
)
expected = jvpmapmap_fn(*primals)
result = jvpvmapvmap_fn(*primals)
self.assertEqual(result, expected)
# See NOTE: [three-transform testing]
@with_tf32_off # https://github.com/pytorch/pytorch/issues/86798
@ops(autograd_function_db, allowed_dtypes=(torch.float32,))
@skipOps(
"TestOperators",
"test_vmapjvpvmap",
{
xfail("NumpyCubeNotComposableAutogradFunction"), # Not composable
},
)
def test_vmapjvpvmap(self, device, dtype, op):
samples = op.sample_inputs(device, dtype, requires_grad=True)
B = 2
for sample in samples:
args = [sample.input] + list(sample.args)
kwargs = sample.kwargs
generator = generate_vmap_inputs(args, kwargs, batch_size=B)
for batched_args, in_dims, kwargs in generator:
inner_vmapped_op = vmap(op, in_dims)
inner_mapped_op = functools.partial(loop, op, in_dims, 0, B)
jvpvmap_fn, primals = get_jvp_variant_primals_tangents2(
inner_vmapped_op,
batched_args,
kwargs,
sample.output_process_fn_grad,
)
jvpmap_fn, _ = get_jvp_variant_primals_tangents2(
inner_mapped_op, batched_args, kwargs, sample.output_process_fn_grad
)
generator = generate_vmap_inputs(primals, {})
for batched_args, in_dims, _ in generator:
# strategy: compare vmap(jvp(vmap(op)) vs map(jvp(map(op))
vmapjvpvmap_fn = vmap(jvpvmap_fn, in_dims)
mapjvpmap_fn = functools.partial(loop, jvpmap_fn, in_dims, 0, B)
result = vmapjvpvmap_fn(*batched_args)
expected = mapjvpmap_fn(*batched_args)
self.assertEqual(result, expected)
# See NOTE: [three-transform testing]
@ops(autograd_function_db, allowed_dtypes=(torch.float32,))
@skipOps(
"TestOperators",
"test_jvpjvpvmap",
{
xfail("NumpyCubeNotComposableAutogradFunction"), # Not composable
},
)
def test_jvpjvpvmap(self, device, dtype, op):
samples = op.sample_inputs(device, dtype, requires_grad=True)
B = 2
for sample in samples:
args = [sample.input] + list(sample.args)
kwargs = sample.kwargs
generator = generate_vmap_inputs(args, kwargs, batch_size=B)
for batched_args, in_dims, kwargs in generator:
inner_vmapped_op = vmap(op, in_dims)
inner_mapped_op = functools.partial(loop, op, in_dims, 0, B)
jvpmap_fn, args = get_jvp_variant_primals_tangents2(
inner_mapped_op, batched_args, kwargs, sample.output_process_fn_grad
)
jvpvmap_fn, _ = get_jvp_variant_primals_tangents2(
inner_vmapped_op,
batched_args,
kwargs,
sample.output_process_fn_grad,
)
jvpjvpvmap_fn, new_args = get_jvp_variant_primals_tangents2(
jvpvmap_fn, args, {}
)
jvpjvpmap_fn, _ = get_jvp_variant_primals_tangents2(jvpmap_fn, args, {})
expected = jvpjvpmap_fn(*new_args)
result = jvpjvpvmap_fn(*new_args)
self.assertEqual(result, expected)
# See NOTE: [three-transform testing]
@ops(autograd_function_db, allowed_dtypes=(torch.float32,))
@skipOps(
"TestOperators",
"test_jvpvjpvmap",
{
xfail("NumpyCubeNotComposableAutogradFunction"), # Not composable
},
)
def test_jvpvjpvmap(self, device, dtype, op):
samples = op.sample_inputs(device, dtype, requires_grad=True)
B = 2
for sample in samples:
args = [sample.input] + list(sample.args)
kwargs = sample.kwargs
generator = generate_vmap_inputs(args, kwargs, batch_size=B)
for batched_args, in_dims, kwargs in generator:
inner_vmapped_op = vmap(op, in_dims)
inner_mapped_op = functools.partial(loop, op, in_dims, 0, B)
vjpmap_fn, args = get_vjpfull_variant2(
inner_mapped_op, batched_args, kwargs
)
vjpvmap_fn, _ = get_vjpfull_variant2(
inner_vmapped_op, batched_args, kwargs
)
jvpvjpvmap_fn, new_args = get_jvp_variant_primals_tangents2(
vjpvmap_fn, args, {}
)
jvpvjpmap_fn, _ = get_jvp_variant_primals_tangents2(vjpmap_fn, args, {})
expected = jvpvjpmap_fn(*new_args)
result = jvpvjpvmap_fn(*new_args)
self.assertEqual(result, expected)
def test_data_write_errors_under_transform(self, device):
t = torch.randn(3, 3, device=device)
def fn(t):
t.data = torch.randn(3, 3)
return t.sum()
msg = "mutating directly with `.data` inside functorch transform"
with self.assertRaisesRegex(RuntimeError, msg):
grad(fn)(t)
with self.assertRaisesRegex(RuntimeError, msg):
vjp(fn, t)
with self.assertRaisesRegex(RuntimeError, msg):
jvp(fn, (t,), (torch.randn_like(t),))
def test_tensor_with_scalar_list(self, device):
x = torch.randn((), device=device)
def func_list_of_scalar(x):
return torch.tensor([x], device=device)
def func(x):
return torch.tensor(x, device=device).view(1)
actual_o, actual_fn = vjp(func_list_of_scalar, x)
expected_o, expected_fn = vjp(func, x)
self.assertEqual(actual_o, expected_o)
self.assertEqual(
expected_fn(torch.ones_like(expected_o)),
actual_fn(torch.ones_like(actual_o)),
)
only_for = ("cpu", "cuda")
instantiate_device_type_tests(TestOperators, globals(), only_for=only_for)
if __name__ == "__main__":
run_tests()
|