Skip to content

[Test] Make test_multiprocessing.py device-agnostic for out-of-tree backends - #184737

Closed
RiyaP2508 wants to merge 4 commits into
pytorch:mainfrom
RiyaP2508:openreg-refactor-test-multiprocessing
Closed

RiyaP2508 wants to merge 4 commits into
pytorch:mainfrom
RiyaP2508:openreg-refactor-test-multiprocessing

Conversation

@RiyaP2508

@RiyaP2508 RiyaP2508 commented May 21, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Refactors test/test_multiprocessing.py to make device-agnostic tests run automatically on all available backends using instantiate_device_type_tests.

Previously, device coverage was achieved by manually duplicating tests (e.g. test_variable_sharing + test_cuda_variable_sharing), which meant out-of-tree backends had no coverage. This PR:

  • Extracts a _MultiprocessingTestMixin with 4 shared helpers used by both test classes
  • Creates TestMultiprocessingDeviceType with 8 device-generic test methods that replace 13 previously duplicated CPU/CUDA methods
  • Fixes _test_autograd_sharing to derive device from var.device instead of hardcoding "cuda" if var.is_cuda else "cpu"
  • Moves 3 @staticmethod child-process targets to module level for better pickle safety with mp.Process

All 13 CUDA-IPC-specific tests (events, IPC handles, _share_cuda_(), fork-poisoning, multi-GPU) remain unchanged in TestMultiprocessing with their original names and guards -- these use CUDA-only APIs with no accelerator-generic equivalent.

Tests moved to TestMultiprocessingDeviceType

New method Replaces
test_simple_sharing test_cuda_simple
test_empty_tensor_sharing test_empty_tensor_sharing + test_empty_tensor_sharing_cuda
test_variable_sharing test_variable_sharing + test_cuda_variable_sharing
test_parameter_sharing test_parameter_sharing + test_cuda_parameter_sharing
test_integer_parameter_serialization test_integer_parameter_serialization_cpu + _cuda
test_leaf_variable_sharing test_leaf_variable_sharing (had internal device loop)
test_non_leaf_variable_sharing test_non_leaf_variable_sharing (had internal device loop)
test_is_shared_accelerator test_is_shared_cuda

Test plan

  • CI should generate TestMultiprocessingDeviceTypeCPU and TestMultiprocessingDeviceTypeCUDA classes (visible with -v flag)
  • All existing TestMultiprocessing tests pass unchanged
  • On machines with XPU or OpenReg, additional device variants run automatically
  • python test/test_multiprocessing.py -v to verify test discovery

cc: @fffrog @albanD @jbschlosser @mansiag05

…stantiate_device_type_tests

Move 8 device-agnostic multiprocessing tests into TestMultiprocessingDeviceType,
enabling automatic coverage on CPU, CUDA, XPU, and OpenReg/PrivateUse1 backends.
Merge 13 duplicated CPU/CUDA test methods into device-parametrized equivalents.
Fix _test_autograd_sharing to derive device from tensor instead of hardcoding CUDA.
Move static child-process methods to module level for better pickle safety.
@pytorch-bot pytorch-bot Bot added the topic: not user facing topic category label May 21, 2026
@pytorch-bot

pytorch-bot Bot commented May 21, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ No Failures

As of commit a9895a2 with merge base 6cd007b (image):
💚 Looks good so far! There are no failures yet. 💚

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

@albanD
albanD requested a review from fffrog May 26, 2026 13:35
@albanD albanD added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label May 26, 2026
@RiyaP2508
RiyaP2508 requested review from albanD and jbschlosser June 2, 2026 02:38
@RiyaP2508

Copy link
Copy Markdown
Collaborator Author

@pytorchbot rebase

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

@pytorchbot started a rebase job onto refs/remotes/origin/viable/strict. Check the current status here

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Rebase failed due to Command git -C /home/runner/work/pytorch/pytorch rebase refs/remotes/origin/viable/strict pull/184737/head returned non-zero exit code 1

Rebasing (1/1)
Auto-merging test/test_multiprocessing.py
CONFLICT (content): Merge conflict in test/test_multiprocessing.py
error: could not apply 2d07daa9e58... Refactor test_multiprocessing.py: extract device-generic tests via instantiate_device_type_tests
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Could not apply 2d07daa9e58... # Refactor test_multiprocessing.py: extract device-generic tests via instantiate_device_type_tests Move 8 device-agnostic multiprocessing tests into TestMultiprocessingDeviceType, enabling automatic coverage on CPU, CUDA, XPU, and OpenReg/PrivateUse1 backends. Merge 13 duplicated CPU/CUDA test methods into device-parametrized equivalents. Fix _test_autograd_sharing to derive device from tensor instead of hardcoding CUDA. Move static child-process methods to module level for better pickle safety.

Raised by https://github.com/pytorch/pytorch/actions/runs/26795047228

@RiyaP2508

Copy link
Copy Markdown
Collaborator Author

@pytorchbot fix-lint

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Successfully applied lint patches in https://github.com/pytorch/pytorch/actions/runs/26795720228. Please pull locally before pushing more changes.

@mansiag05 mansiag05 moved this from Backlog to Ready for Review(Initial) in PyTorch Test Refactoring Jun 2, 2026
@RiyaP2508
RiyaP2508 requested review from Skylion007 and jansel June 5, 2026 18:12
@jansel

jansel commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

@claude review these changes

@claude

claude Bot commented Jun 5, 2026 •

Copy link
Copy Markdown

Claude finished @jansel's task in 3m 55s —— View job


Review of PR #184737

  • Read the PR diff and understand the changes
  • Review code quality, correctness, and patterns
  • Post review feedback

Summary

This PR refactors test/test_multiprocessing.py to introduce instantiate_device_type_tests for device-agnostic test coverage. The approach is sound: extract shared helpers into _MultiprocessingTestMixin, create TestMultiprocessingDeviceType with device-parameterized tests, and leave CUDA-IPC-specific tests in the original TestMultiprocessing class.

Issues

1. Duplicate tests after merge (Major)

The merge with main (8e97f6a) re-introduced the original test_variable_sharing, test_leaf_variable_sharing, test_non_leaf_variable_sharing, and test_cuda_variable_sharing methods into TestMultiprocessing (lines 1045-1098). These were supposed to be removed by this PR and replaced by the device-generic versions in TestMultiprocessingDeviceType (lines 507-550).

This means these tests now run twice — once in TestMultiprocessing with hardcoded device loops, and again via TestMultiprocessingDeviceTypeCPU / TestMultiprocessingDeviceTypeCUDA. This defeats the purpose of the refactoring and doubles test execution time for these cases.

The duplicated methods at lines 1045-1098 should be removed.

Fix this →

2. Unused imports (Minor)

The merge also brought in imports that are only used by the duplicate tests:

  • skipIfRocm (line 27) — only used at line 1089 on the duplicate test_cuda_variable_sharing
  • IS_LINUX (line 22) and TEST_WITH_SLOW (line 31) — used only by test_meta_simple skip decorator (line 1013), which came from main and is fine

If the duplicate tests are removed, skipIfRocm becomes unused and should be removed.

3. test_simple_sharing does not test test_cuda_simple's initialization (Minor)

The original test_cuda_simple (removed by this PR) had a line:

torch.cuda.FloatTensor([1])  # initialize CUDA outside of leak checker

The new test_simple_sharing in TestMultiprocessingDeviceType doesn't replicate this. For the CUDA path, this could potentially make leak_checker less reliable if CUDA initialization happens to open file descriptors inside the checker. This is likely a non-issue in practice (the leak checker's fd check is already disabled), but worth noting.

Design

The overall refactoring approach is clean and follows the established instantiate_device_type_tests pattern used elsewhere in PyTorch. Key positives:

  • _MultiprocessingTestMixin cleanly separates shared logic
  • _get_ctx(device) correctly handles spawn vs fork context selection
  • _test_autograd_sharing now derives device from var.device instead of var.is_cuda — correct generalization
  • Module-level functions for subprocess targets improve pickle safety
  • allow_xpu=True on instantiate_device_type_tests is appropriate

Recommendation

The duplicate tests from the merge conflict resolution need to be removed before merging. Otherwise the refactoring goal of deduplication is not achieved.


@jansel jansel left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

address automated feedback from claude

@RiyaP2508

Copy link
Copy Markdown
Collaborator Author

address automated feedback from claude

Thanks for review @jansel , addressed them in latest push. PTAL:)

@RiyaP2508

Copy link
Copy Markdown
Collaborator Author

@pytorchbot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Jun 7, 2026
@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

@github-project-automation github-project-automation Bot moved this from Ready for Review(Initial) to Done in PyTorch Test Refactoring Jun 7, 2026
pytorchmergebot pushed a commit that referenced this pull request Jun 16, 2026
…zation` (#186630)

Otherwise #184737 seems to expose a preexisting issue that causes `test_simple_sharing_cuda` to fail

authored with codex

Pull Request resolved: #186630
Approved by: https://github.com/ezyang
pytorchmergebot pushed a commit that referenced this pull request Jun 16, 2026
…187314)

# Motivation
When #184737 generalize `test_multiprocess.py`, it helps enable `TestMultiprocessingDeviceType` test on XPU. However, XPU doesn't support this test suite because lack of IPC support currently. So we would like to skip this test case and restore XPU CI to green.

Fixes #186585

Code originates from #186793
Co-authored-by: @xuhancn
Pull Request resolved: #187314
Approved by: https://github.com/EikanWang, https://github.com/atalman
ghstack dependencies: #181233, #187137, #187232, #187306
atalman pushed a commit that referenced this pull request Jun 16, 2026
…187416)

Skip TestMultiprocessingDeviceType on XPU due to lack of IPC support (#187314)

# Motivation
When #184737 generalize `test_multiprocess.py`, it helps enable `TestMultiprocessingDeviceType` test on XPU. However, XPU doesn't support this test suite because lack of IPC support currently. So we would like to skip this test case and restore XPU CI to green.

Fixes #186585

Code originates from #186793
Co-authored-by: @xuhancn
Pull Request resolved: #187314
Approved by: https://github.com/EikanWang, https://github.com/atalman
ghstack dependencies: #181233, #187137, #187232, #187306

(cherry picked from commit 4f26a57)

Co-authored-by: Yu, Guangye <guangye.yu@intel.com>
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
…ackends (pytorch#184737)

## Summary

Refactors `test/test_multiprocessing.py` to make device-agnostic tests run automatically on all available backends using `instantiate_device_type_tests`.

Previously, device coverage was achieved by manually duplicating tests (e.g. `test_variable_sharing` + `test_cuda_variable_sharing`), which meant out-of-tree backends had no coverage. This PR:

- Extracts a `_MultiprocessingTestMixin` with 4 shared helpers used by both test classes
- Creates `TestMultiprocessingDeviceType` with 8 device-generic test methods that replace 13 previously duplicated CPU/CUDA methods
- Fixes `_test_autograd_sharing` to derive device from `var.device` instead of hardcoding `"cuda" if var.is_cuda else "cpu"`
- Moves 3 `@staticmethod` child-process targets to module level for better pickle safety with `mp.Process`

All 13 CUDA-IPC-specific tests (events, IPC handles, `_share_cuda_()`, fork-poisoning, multi-GPU) remain unchanged in `TestMultiprocessing` with their original names and guards -- these use CUDA-only APIs with no accelerator-generic equivalent.

### Tests moved to `TestMultiprocessingDeviceType`

| New method | Replaces |
|---|---|
| `test_simple_sharing` | `test_cuda_simple` |
| `test_empty_tensor_sharing` | `test_empty_tensor_sharing` + `test_empty_tensor_sharing_cuda` |
| `test_variable_sharing` | `test_variable_sharing` + `test_cuda_variable_sharing` |
| `test_parameter_sharing` | `test_parameter_sharing` + `test_cuda_parameter_sharing` |
| `test_integer_parameter_serialization` | `test_integer_parameter_serialization_cpu` + `_cuda` |
| `test_leaf_variable_sharing` | `test_leaf_variable_sharing` (had internal device loop) |
| `test_non_leaf_variable_sharing` | `test_non_leaf_variable_sharing` (had internal device loop) |
| `test_is_shared_accelerator` | `test_is_shared_cuda` |

## Test plan

- CI should generate `TestMultiprocessingDeviceTypeCPU` and `TestMultiprocessingDeviceTypeCUDA` classes (visible with `-v` flag)
- All existing `TestMultiprocessing` tests pass unchanged
- On machines with XPU or OpenReg, additional device variants run automatically
- `python test/test_multiprocessing.py -v` to verify test discovery

Pull Request resolved: pytorch#184737
Approved by: https://github.com/jansel

Co-authored-by: PyTorch MergeBot <pytorchmergebot@users.noreply.github.com>
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
…zation` (pytorch#186630)

Otherwise pytorch#184737 seems to expose a preexisting issue that causes `test_simple_sharing_cuda` to fail

authored with codex

Pull Request resolved: pytorch#186630
Approved by: https://github.com/ezyang
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
…ytorch#187314)

# Motivation
When pytorch#184737 generalize `test_multiprocess.py`, it helps enable `TestMultiprocessingDeviceType` test on XPU. However, XPU doesn't support this test suite because lack of IPC support currently. So we would like to skip this test case and restore XPU CI to green.

Fixes pytorch#186585

Code originates from pytorch#186793
Co-authored-by: @xuhancn
Pull Request resolved: pytorch#187314
Approved by: https://github.com/EikanWang, https://github.com/atalman
ghstack dependencies: pytorch#181233, pytorch#187137, pytorch#187232, pytorch#187306
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
…ackends (pytorch#184737)

## Summary

Refactors `test/test_multiprocessing.py` to make device-agnostic tests run automatically on all available backends using `instantiate_device_type_tests`.

Previously, device coverage was achieved by manually duplicating tests (e.g. `test_variable_sharing` + `test_cuda_variable_sharing`), which meant out-of-tree backends had no coverage. This PR:

- Extracts a `_MultiprocessingTestMixin` with 4 shared helpers used by both test classes
- Creates `TestMultiprocessingDeviceType` with 8 device-generic test methods that replace 13 previously duplicated CPU/CUDA methods
- Fixes `_test_autograd_sharing` to derive device from `var.device` instead of hardcoding `"cuda" if var.is_cuda else "cpu"`
- Moves 3 `@staticmethod` child-process targets to module level for better pickle safety with `mp.Process`

All 13 CUDA-IPC-specific tests (events, IPC handles, `_share_cuda_()`, fork-poisoning, multi-GPU) remain unchanged in `TestMultiprocessing` with their original names and guards -- these use CUDA-only APIs with no accelerator-generic equivalent.

### Tests moved to `TestMultiprocessingDeviceType`

| New method | Replaces |
|---|---|
| `test_simple_sharing` | `test_cuda_simple` |
| `test_empty_tensor_sharing` | `test_empty_tensor_sharing` + `test_empty_tensor_sharing_cuda` |
| `test_variable_sharing` | `test_variable_sharing` + `test_cuda_variable_sharing` |
| `test_parameter_sharing` | `test_parameter_sharing` + `test_cuda_parameter_sharing` |
| `test_integer_parameter_serialization` | `test_integer_parameter_serialization_cpu` + `_cuda` |
| `test_leaf_variable_sharing` | `test_leaf_variable_sharing` (had internal device loop) |
| `test_non_leaf_variable_sharing` | `test_non_leaf_variable_sharing` (had internal device loop) |
| `test_is_shared_accelerator` | `test_is_shared_cuda` |

## Test plan

- CI should generate `TestMultiprocessingDeviceTypeCPU` and `TestMultiprocessingDeviceTypeCUDA` classes (visible with `-v` flag)
- All existing `TestMultiprocessing` tests pass unchanged
- On machines with XPU or OpenReg, additional device variants run automatically
- `python test/test_multiprocessing.py -v` to verify test discovery

Pull Request resolved: pytorch#184737
Approved by: https://github.com/jansel

Co-authored-by: PyTorch MergeBot <pytorchmergebot@users.noreply.github.com>
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
…zation` (pytorch#186630)

Otherwise pytorch#184737 seems to expose a preexisting issue that causes `test_simple_sharing_cuda` to fail

authored with codex

Pull Request resolved: pytorch#186630
Approved by: https://github.com/ezyang
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
…ytorch#187314)

# Motivation
When pytorch#184737 generalize `test_multiprocess.py`, it helps enable `TestMultiprocessingDeviceType` test on XPU. However, XPU doesn't support this test suite because lack of IPC support currently. So we would like to skip this test case and restore XPU CI to green.

Fixes pytorch#186585

Code originates from pytorch#186793
Co-authored-by: @xuhancn
Pull Request resolved: pytorch#187314
Approved by: https://github.com/EikanWang, https://github.com/atalman
ghstack dependencies: pytorch#181233, pytorch#187137, pytorch#187232, pytorch#187306
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request Merged open source topic: not user facing topic category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants