PyTorch fixes dropout identity bug, speeds up no-op cases 4831x
A tiny decomposition change in PyTorch eliminates needless tensor cloning, dramatically boosting compiled model performance.
PyTorch has resolved a subtle but impactful bug in its `aten.dropout` decomposition that caused unnecessary tensor cloning and broke compiled model tracing. The issue, identified in PR #185335, arose because the decomposition used `aten.native_dropout` semantics for the no-op branch, returning `input.clone()` when training was disabled or dropout probability was zero. This differed from eager `aten.dropout`, which returns the original tensor object directly. For PyTorch's JIT compiler Dynamo, when tracing dropout on a `Parameter`, the cloned `FakeTensor` lost the original Parameter identity. This caused `Module.__setattr__` to take the registered-parameter error path, attempting to format `torch.typename` — an operation Dynamo intentionally skips, leading to errors.
The fix is elegantly simple: return the input tensor directly in the no-op decomposition branch, making tracing match eager dropout's aliasing semantics. A narrower fix targeting `Module.__setattr__` or `torch.typename` tracing would have only hidden the symptom. The change also delivers a massive performance boost: a CPU microbenchmark on a 4096x4096 tensor showed 0.00016 seconds total for 100 iterations (1.60 µs per iter) vs. 0.77176 seconds using the old clone behavior — a 4831x speedup. The patch also resolves two previously reported issues (#160241 and #115249), improving robustness for users relying on `torch.compile` and `torch.export` with dropout layers.
- Fix returns input tensor directly instead of clone in no-op dropout (eval mode or p=0), matching eager behavior.
- Benchmarks show 4831x speedup on 4096x4096 tensor: 1.60 µs/iter vs 7717.61 µs/iter old behavior.
- Resolves two open bugs (#160241, #115249) affecting torch.compile and torch.export with parameters and dropout.
Why It Matters
This fix eliminates a hidden slowdown in compiled PyTorch models and prevents parameter identity errors, improving reliability and speed for production deployments.