Commit 4b7abce
Fix fake tensor caching when output has unbacked (pytorch#153034)
We handle fake tensor caching in two ways:
1. If the inputs have no symbols (SymInt, etc) then we cache on the FakeTensorMode.
2. If the inputs have symbols then we cache on the ShapeEnv.
This way the symbols in the inputs and outputs are associated with the guards in place at the time of the call.
However - it's possible to have an op where there are no symbols in the inputs but there is an unbacked symbol in the output. In this case we shouldn't cache at all because what would that really mean?
So this PR changes the caching behavior so that if there's a symbol in the output which doesn't come in some way from the input then we refuse to cache that op.
Added a test which checks for this case.
While in there I also did a couple other related changes:
1. Added negative caching - if we see that an (op, args) failed to cache previously we don't even bother trying to cache it again.
2. Reworked the inner behavior of _cached_dispatch_impl a little to make it more clear which bits we expect to be able to throw _BypassDispatchCache and add some comments.
The latest version of this also:
1. Addresses the problem that caused pytorch#153891.
The issue was that with caching ops are required to support `__eq__`. Unfortunately _RecordFunction is minimalistic and doesn't support that - so in the off-chance that two keys hash to the same value the `__eq__` check would raise an exception.
Apparently this was much more common on MacOS where memory patterns end up with more reuse (so the object IDs are the same and give you the same hash value for objects that use pointer hash).
Tested locally on MacOS where running
```
python test/inductor/test_torchinductor.py GPUTests
```
was pretty much guaranteed to fail (at least for me) somewhere around test 100-200 and passed all 800 tests after this change.
Another way to test this is to run the inductor tests with `torch._subclasses.fake_tensor._DispatchCacheKey.__hash__` monkey-patched to return a constant (causing all values to hash-collide) but this can't really be checked-in since it causes the cache lookup to turn into an O(n) lookup which takes a crazy long time to run through all the tests...
2. Folds in pytorch#153780 to ensure that exceptions raised from the op don't include the context from the cache key bypass.
Pull Request resolved: pytorch#153034
Approved by: https://github.com/masnesral, https://github.com/tugsbayasgalan1 parent 866142f commit 4b7abce
File tree
3 files changed
+155
-48
lines changed- test
- torch/_subclasses
3 files changed
+155
-48
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2265 | 2265 | | |
2266 | 2266 | | |
2267 | 2267 | | |
2268 | | - | |
2269 | | - | |
2270 | 2268 | | |
2271 | 2269 | | |
2272 | 2270 | | |
2273 | 2271 | | |
2274 | | - | |
2275 | 2272 | | |
2276 | 2273 | | |
2277 | 2274 | | |
| |||
2317 | 2314 | | |
2318 | 2315 | | |
2319 | 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 | + | |
2320 | 2344 | | |
2321 | 2345 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
218 | 218 | | |
219 | 219 | | |
220 | 220 | | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
221 | 226 | | |
222 | 227 | | |
223 | 228 | | |
| |||
226 | 231 | | |
227 | 232 | | |
228 | 233 | | |
| 234 | + | |
229 | 235 | | |
230 | 236 | | |
231 | 237 | | |
| |||
247 | 253 | | |
248 | 254 | | |
249 | 255 | | |
| 256 | + | |
250 | 257 | | |
251 | 258 | | |
252 | 259 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
74 | 74 | | |
75 | 75 | | |
76 | 76 | | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | 77 | | |
84 | 78 | | |
85 | 79 | | |
| |||
1118 | 1112 | | |
1119 | 1113 | | |
1120 | 1114 | | |
1121 | | - | |
| 1115 | + | |
1122 | 1116 | | |
1123 | 1117 | | |
1124 | 1118 | | |
| |||
1131 | 1125 | | |
1132 | 1126 | | |
1133 | 1127 | | |
| 1128 | + | |
| 1129 | + | |
| 1130 | + | |
| 1131 | + | |
| 1132 | + | |
| 1133 | + | |
| 1134 | + | |
| 1135 | + | |
| 1136 | + | |
| 1137 | + | |
| 1138 | + | |
| 1139 | + | |
| 1140 | + | |
| 1141 | + | |
1134 | 1142 | | |
1135 | 1143 | | |
1136 | 1144 | | |
| |||
1418 | 1426 | | |
1419 | 1427 | | |
1420 | 1428 | | |
1421 | | - | |
| 1429 | + | |
| 1430 | + | |
1422 | 1431 | | |
1423 | 1432 | | |
1424 | 1433 | | |
1425 | | - | |
1426 | | - | |
1427 | | - | |
1428 | | - | |
1429 | | - | |
1430 | | - | |
1431 | | - | |
1432 | | - | |
1433 | | - | |
1434 | | - | |
1435 | | - | |
1436 | | - | |
1437 | | - | |
1438 | | - | |
1439 | | - | |
1440 | | - | |
1441 | | - | |
1442 | | - | |
1443 | | - | |
1444 | | - | |
1445 | | - | |
1446 | 1434 | | |
| 1435 | + | |
1447 | 1436 | | |
1448 | 1437 | | |
1449 | | - | |
1450 | | - | |
| 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 | + | |
1451 | 1473 | | |
| 1474 | + | |
| 1475 | + | |
| 1476 | + | |
| 1477 | + | |
| 1478 | + | |
| 1479 | + | |
| 1480 | + | |
| 1481 | + | |
| 1482 | + | |
| 1483 | + | |
| 1484 | + | |
| 1485 | + | |
| 1486 | + | |
| 1487 | + | |
| 1488 | + | |
| 1489 | + | |
| 1490 | + | |
| 1491 | + | |
| 1492 | + | |
| 1493 | + | |
| 1494 | + | |
1452 | 1495 | | |
1453 | 1496 | | |
1454 | 1497 | | |
| |||
1634 | 1677 | | |
1635 | 1678 | | |
1636 | 1679 | | |
1637 | | - | |
1638 | | - | |
| 1680 | + | |
| 1681 | + | |
| 1682 | + | |
1639 | 1683 | | |
1640 | 1684 | | |
1641 | 1685 | | |
1642 | | - | |
1643 | | - | |
1644 | | - | |
1645 | | - | |
1646 | | - | |
1647 | | - | |
| 1686 | + | |
| 1687 | + | |
| 1688 | + | |
| 1689 | + | |
| 1690 | + | |
1648 | 1691 | | |
1649 | 1692 | | |
1650 | 1693 | | |
| |||
1718 | 1761 | | |
1719 | 1762 | | |
1720 | 1763 | | |
1721 | | - | |
| 1764 | + | |
1722 | 1765 | | |
1723 | 1766 | | |
1724 | 1767 | | |
| |||
1742 | 1785 | | |
1743 | 1786 | | |
1744 | 1787 | | |
1745 | | - | |
| 1788 | + | |
1746 | 1789 | | |
1747 | 1790 | | |
1748 | 1791 | | |
| |||
1773 | 1816 | | |
1774 | 1817 | | |
1775 | 1818 | | |
1776 | | - | |
| 1819 | + | |
1777 | 1820 | | |
1778 | 1821 | | |
1779 | 1822 | | |
| |||
1794 | 1837 | | |
1795 | 1838 | | |
1796 | 1839 | | |
1797 | | - | |
| 1840 | + | |
1798 | 1841 | | |
1799 | 1842 | | |
1800 | 1843 | | |
1801 | 1844 | | |
1802 | 1845 | | |
1803 | 1846 | | |
1804 | 1847 | | |
1805 | | - | |
| 1848 | + | |
1806 | 1849 | | |
1807 | 1850 | | |
1808 | 1851 | | |
| |||
1882 | 1925 | | |
1883 | 1926 | | |
1884 | 1927 | | |
1885 | | - | |
| 1928 | + | |
1886 | 1929 | | |
1887 | 1930 | | |
1888 | 1931 | | |
| |||
2886 | 2929 | | |
2887 | 2930 | | |
2888 | 2931 | | |
| 2932 | + | |
| 2933 | + | |
| 2934 | + | |
| 2935 | + | |
| 2936 | + | |
| 2937 | + | |
| 2938 | + | |
| 2939 | + | |
| 2940 | + | |
| 2941 | + | |
| 2942 | + | |
| 2943 | + | |
| 2944 | + | |
2889 | 2945 | | |
2890 | 2946 | | |
2891 | 2947 | | |
| |||
2951 | 3007 | | |
2952 | 3008 | | |
2953 | 3009 | | |
| 3010 | + | |
| 3011 | + | |
| 3012 | + | |
| 3013 | + | |
| 3014 | + | |
| 3015 | + | |
| 3016 | + | |
| 3017 | + | |
| 3018 | + | |
| 3019 | + | |
| 3020 | + | |
| 3021 | + | |
| 3022 | + | |
| 3023 | + | |
| 3024 | + | |
| 3025 | + | |
| 3026 | + | |
2954 | 3027 | | |
2955 | 3028 | | |
2956 | 3029 | | |
| |||
3042 | 3115 | | |
3043 | 3116 | | |
3044 | 3117 | | |
| 3118 | + | |
| 3119 | + | |
| 3120 | + | |
3045 | 3121 | | |
3046 | 3122 | | |
3047 | 3123 | | |
| |||
0 commit comments