Skip to content

[BE] Extend dropout support to complex numbers - #195373

Closed
malfet wants to merge 3 commits into
mainfrom
malfet/complex-dropout-cpu
Closed

malfet wants to merge 3 commits into
mainfrom
malfet/complex-dropout-cpu

Conversation

@malfet

@malfet malfet commented Aug 30, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

nn.functional.dropout raised NotImplementedError for complex inputs on CPU (and CUDA), while silently "working" on MPS.

Root cause: _dropout_impl allocated the Bernoulli mask (noise) in the input's dtype and called bernoulli_ on it. For complex inputs that requires a complex bernoulli_, which CPU and CUDA do not implement (both dispatch via AT_DISPATCH_ALL_TYPES_AND3, no complex). MPS only accepted it because of incidental complex bernoulli Metal kernels that wrote the {0,1} mask into the real component and zeroed the imaginary part -- a semantically-undefined result that masked the CPU/MPS divergence.

Fix: The dropout mask is real-valued regardless of input dtype, so allocate it in the real value type (c10::toRealValueType). complex_input * real_mask is well-defined and numerically identical to the old MPS behaviour. Complex inputs are also excluded from is_fused_kernel_acceptable, so on CUDA/XPU/lazy/privateuseone (whose fused kernels are float-only) they take the same composite _dropout_impl path -- mirroring how MPS already excludes non-floating inputs there. Complex dropout now works uniformly across backends.

Follow-on cleanup enabled by the above:

  • MPS rejects complex bernoulli_ (matching CPU/CUDA), and the dead complex Metal kernel instantiations (float2/half2) are removed.
  • The dropout-family OpInfos declare complex dtypes (previously test_dtypes flagged them as "supported but unlisted"); _refs.nn.functional.alpha_dropout gains the conj/neg-view skips its _refs.nn.functional.dropout sibling already had (dropout's per-call random mask breaks view invariance).
  • The MPS OpInfo lists drop the now-passing dropout-family complex xfails and the already-dead new_empty/new_empty_strided complex entries (skipped for all dtypes by EMPTY_OPS_SKIPLIST).

This PR was authored with the assistance of an AI coding assistant (Claude); all changes were reviewed by the author.

@pytorch-bot

pytorch-bot Bot commented Aug 30, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/195373

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit e7645dd with merge base 9dbf0d7 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@pytorch-bot pytorch-bot Bot added ciflow/mps Run MPS tests (subset of trunk) release notes: mps Release notes category labels Aug 30, 2026
@malfet malfet added release notes: complex release notes category topic: bug fixes topic category and removed release notes: mps Release notes category labels Aug 30, 2026
@malfet
malfet marked this pull request as ready for review August 30, 2026 21:04
@malfet malfet changed the title Support complex dropout by allocating a real-valued mask [BE] Extend dropout support to complex numbers Aug 30, 2026
@malfet
malfet force-pushed the malfet/complex-dropout-cpu branch 2 times, most recently from db45f26 to 6348954 Compare August 31, 2026 00:12
@malfet
malfet force-pushed the malfet/complex-dropout-cpu branch from 6348954 to 02dd7bf Compare August 31, 2026 01:05
@malfet malfet added the ciflow/trunk Trigger trunk jobs on your pull request label Aug 31, 2026
@malfet

malfet commented Aug 31, 2026

Copy link
Copy Markdown
Collaborator Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

`nn.functional.dropout` raised `NotImplementedError` for complex inputs on
CPU (and CUDA), while silently "working" on MPS. Root cause: `_dropout_impl`
allocated the Bernoulli mask (`noise`) in the input's dtype and called
`bernoulli_` on it. For complex inputs that requires a complex `bernoulli_`,
which CPU and CUDA do not implement (both dispatch via
`AT_DISPATCH_ALL_TYPES_AND3`, which does not list complex). MPS only accepted
it because of incidental complex bernoulli Metal kernels that wrote the {0,1}
mask into the real component and zeroed the imaginary part -- a
semantically-undefined result that masked the CPU/MPS divergence.

The dropout mask is real-valued regardless of input dtype, so allocate it in
the real value type (`c10::toRealValueType`); `complex_input * real_mask` is
well-defined and numerically identical to the old MPS behaviour. Complex
inputs are also excluded from `is_fused_kernel_acceptable`, so that on
CUDA/XPU/lazy/privateuseone (whose fused kernels are float-only) they take the
same composite `_dropout_impl` path -- mirroring how MPS already excludes
non-floating inputs there. Complex dropout now works uniformly across backends.

Follow-on cleanup enabled by the above:
- MPS rejects complex `bernoulli_` (matching CPU/CUDA) and the dead complex
  Metal kernel instantiations are removed.
- The dropout-family OpInfos declare complex dtypes (previously
  `test_dtypes` flagged them as "supported but unlisted"); the
  `_refs.nn.functional.alpha_dropout` ref gains the conj/neg-view skips its
  `_refs.nn.functional.dropout` sibling already had (dropout's per-call random
  mask breaks view invariance).
- The `aten.dropout` decomposition inlines the real-mask math for complex
  inputs instead of calling `aten.native_dropout`, whose autograd node
  rejects complex outputs (the float path is unchanged).
- `_refs.nn.functional.dropout` skips `test_python_ref_executor` for complex
  dtypes: the aten-executor comparison, xfailed for float as non-comparable,
  becomes comparable for complex on CUDA/ROCm (unexpected success) but not
  MPS, so skip rather than xfail avoids the device-dependent flip.
- The MPS OpInfo lists drop the now-passing dropout-family complex xfails and
  the already-dead `new_empty`/`new_empty_strided` complex entries (skipped for
  all dtypes by `EMPTY_OPS_SKIPLIST`).

Test Plan:

Incremental build:

```
cd build && ninja torch_cpu
```

CPU OpInfo (was failing before the OpInfo dtype update):

```
python test/test_ops.py -k "dropout"    # Ran 338 tests ... OK (skipped=123)
```

MPS OpInfo consistency (Apple Silicon):

```
python test/test_mps.py -k "dropout"    # Ran 60 tests ... OK (expected failures=10)
python test/test_mps.py -k "bernoulli"  # Ran 14 tests ... OK (expected failures=5)
```

Manual checks: complex64/complex128 `F.dropout` returns a complex tensor with
the correct 1/(1-p) scaling on CPU and MPS; complex `bernoulli_` raises
`NotImplementedError` on MPS; real dtypes, `torch.native_dropout`, and float
dropout are unchanged. CUDA/XPU complex routing is by construction (composite
path) and validated by CI.

This PR was authored with the assistance of an AI coding assistant (Claude).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@malfet
malfet force-pushed the malfet/complex-dropout-cpu branch from 02dd7bf to ce9b619 Compare August 31, 2026 05:32
@malfet
malfet force-pushed the malfet/complex-dropout-cpu branch from 3552463 to 73d81d4 Compare August 31, 2026 12:26
The previous commit skipped `test_python_ref_executor` for complex dtypes on
`_refs.nn.functional.dropout` across all devices. The unexpected success only
occurs on CUDA/ROCm, where the aten-executor result for complex happens to
match eager; on MPS the executor comparison stays non-comparable and the
`expectedFailure` still holds. Scope the skip to `device_type="cuda"` (which
also covers ROCm) so the MPS xfail is preserved.

Test Plan: verified decorator resolution -- cuda+complex -> skip, cuda+float
-> xfail, mps+complex -> xfail. `test_python_ref_executor` is `@skipIfMPS`,
so this only affects the CUDA/ROCm accelerator runs in practice.

Authored with the assistance of an AI coding assistant (Claude).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@malfet
malfet force-pushed the malfet/complex-dropout-cpu branch from 73d81d4 to e7645dd Compare August 31, 2026 12:42
@Skylion007

Copy link
Copy Markdown
Collaborator

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

orrangetabby17 pushed a commit to orrangetabby17/pytorch that referenced this pull request Sep 2, 2026
## Summary

`nn.functional.dropout` raised `NotImplementedError` for complex inputs on CPU (and CUDA), while silently "working" on MPS.

**Root cause:** `_dropout_impl` allocated the Bernoulli mask (`noise`) in the input's dtype and called `bernoulli_` on it. For complex inputs that requires a complex `bernoulli_`, which CPU and CUDA do not implement (both dispatch via `AT_DISPATCH_ALL_TYPES_AND3`, no complex). MPS only accepted it because of incidental complex bernoulli Metal kernels that wrote the `{0,1}` mask into the real component and zeroed the imaginary part -- a semantically-undefined result that masked the CPU/MPS divergence.

**Fix:** The dropout mask is real-valued regardless of input dtype, so allocate it in the real value type (`c10::toRealValueType`). `complex_input * real_mask` is well-defined and numerically identical to the old MPS behaviour. Complex inputs are also excluded from `is_fused_kernel_acceptable`, so on CUDA/XPU/lazy/privateuseone (whose fused kernels are float-only) they take the same composite `_dropout_impl` path -- mirroring how MPS already excludes non-floating inputs there. Complex dropout now works uniformly across backends.

**Follow-on cleanup enabled by the above:**
- MPS rejects complex `bernoulli_` (matching CPU/CUDA), and the dead complex Metal kernel instantiations (`float2`/`half2`) are removed.
- The dropout-family OpInfos declare complex dtypes (previously `test_dtypes` flagged them as "supported but unlisted"); `_refs.nn.functional.alpha_dropout` gains the conj/neg-view skips its `_refs.nn.functional.dropout` sibling already had (dropout's per-call random mask breaks view invariance).
- The MPS OpInfo lists drop the now-passing dropout-family complex xfails and the already-dead `new_empty`/`new_empty_strided` complex entries (skipped for all dtypes by `EMPTY_OPS_SKIPLIST`).

> This PR was authored with the assistance of an AI coding assistant (Claude); all changes were reviewed by the author.

Pull Request resolved: pytorch#195373
Approved by: https://github.com/Skylion007

Co-authored-by: Claude Opus 4.8 <[email protected]>
Sanskaaar1 pushed a commit to Sanskaaar1/pytorch that referenced this pull request Sep 8, 2026
## Summary

`nn.functional.dropout` raised `NotImplementedError` for complex inputs on CPU (and CUDA), while silently "working" on MPS.

**Root cause:** `_dropout_impl` allocated the Bernoulli mask (`noise`) in the input's dtype and called `bernoulli_` on it. For complex inputs that requires a complex `bernoulli_`, which CPU and CUDA do not implement (both dispatch via `AT_DISPATCH_ALL_TYPES_AND3`, no complex). MPS only accepted it because of incidental complex bernoulli Metal kernels that wrote the `{0,1}` mask into the real component and zeroed the imaginary part -- a semantically-undefined result that masked the CPU/MPS divergence.

**Fix:** The dropout mask is real-valued regardless of input dtype, so allocate it in the real value type (`c10::toRealValueType`). `complex_input * real_mask` is well-defined and numerically identical to the old MPS behaviour. Complex inputs are also excluded from `is_fused_kernel_acceptable`, so on CUDA/XPU/lazy/privateuseone (whose fused kernels are float-only) they take the same composite `_dropout_impl` path -- mirroring how MPS already excludes non-floating inputs there. Complex dropout now works uniformly across backends.

**Follow-on cleanup enabled by the above:**
- MPS rejects complex `bernoulli_` (matching CPU/CUDA), and the dead complex Metal kernel instantiations (`float2`/`half2`) are removed.
- The dropout-family OpInfos declare complex dtypes (previously `test_dtypes` flagged them as "supported but unlisted"); `_refs.nn.functional.alpha_dropout` gains the conj/neg-view skips its `_refs.nn.functional.dropout` sibling already had (dropout's per-call random mask breaks view invariance).
- The MPS OpInfo lists drop the now-passing dropout-family complex xfails and the already-dead `new_empty`/`new_empty_strided` complex entries (skipped for all dtypes by `EMPTY_OPS_SKIPLIST`).

> This PR was authored with the assistance of an AI coding assistant (Claude); all changes were reviewed by the author.

Pull Request resolved: pytorch#195373
Approved by: https://github.com/Skylion007

Co-authored-by: Claude Opus 4.8 <[email protected]>
@github-actions
github-actions Bot deleted the malfet/complex-dropout-cpu branch November 12, 2026 01:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/inductor ciflow/mps Run MPS tests (subset of trunk) ciflow/trunk Trigger trunk jobs on your pull request Merged open source release notes: complex release notes category topic: bug fixes topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants