Avoid inspect.getattr_static when wrapping a module in OptimizedModule (#197191)
Fixes #190500
torch.compile() on an nn.Module can run while Dynamo's eval frame callback is
installed: compiled autograd compiles the captured backward graph by calling
torch.compile() on it from inside the region being compiled. The three
inspect.getattr_static() lookups on that path therefore execute with the
callback live, so Dynamo intercepts CPython's inspect internals and traces
them. On CPython 3.12+ inspect.getattr_static() memoizes a helper with
functools.lru_cache, and tracing that emits "Dynamo detected a call to a
`functools.lru_cache`-wrapped function at 'inspect.py:1720'".
The warning is raised while user code is running, which is how it surfaced.
test_to_r_to_c (test_autograd.py) wraps its body in assertNotWarn, and under
PYTORCH_TEST_WITH_DYNAMO that body -- including the backward that triggers the
compiled autograd compile -- is compiled inside the recording window, so a
compiler diagnostic fails the test (issue #190500). The first run in a process
does not reach the traced call, which is why the test passes standalone and
fails on every later run in the same process: 1 pass and 49 failures in each
nightly rerun_disabled_tests job.
eval_frame.py already has _static_getattr for exactly this hazard ("this runs
while the eval frame callback is installed, so its Python frames land in
Dynamo's frame counters"); these three call sites missed it. Switching to it
removes the accidental trace and with it the warning, verified as emitted zero
times rather than merely re-timed outside the assertion window.
Two suppression-based alternatives were considered and rejected, since both
hide future mistakes instead of fixing this one: allowlisting the CPython memo
through allow_lru_cache_wrapper_trace_without_warning(), and filtering
compiler-stack warnings out of assertNotWarn.
Closing that issue re-enables test_to_r_to_c_cpu in CI.
Test Plan:
The new test fails without the fix and passes with it:
```
python -m pytest test/dynamo/test_modules.py -k wrapping_does_not_use_getattr_static
```
Reproducing the reported failure needs repeats in one process, since the first
run passes; before the fix this reports 2 failures, after it passes, as do 50
repeats (the count rerun_disabled_tests uses):
```
T=TestAutogradDeviceTypeCPU.test_to_r_to_c_cpu
PYTORCH_TEST_WITH_DYNAMO=1 python test/test_autograd.py $T $T $T
```
Regression runs on CPython 3.14 (151 passed; 922 passed, 31 skipped, 6
xfailed; 1 passed):
```
python -m pytest test/dynamo/test_modules.py
python -m pytest test/inductor/test_compiled_autograd.py
python -m pytest test/inductor/test_config.py -k compiler_config
```
Authored with the assistance of Claude Code.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/197191
Approved by: https://github.com/pytorchgreenlight A
Aaron Orenstein committed
617a6f77d63e8254c9dd233b352dc1412dd41ec4
Parent: 070e6e3
Committed by PyTorch MergeBot <pytorchmergebot@users.noreply.github.com>
on 10/1/2026, 2:04:47 AM