Skip to content

[Testcase Refactoring] Refactor test_stage with instantiate_device_type_tests and hw-classification - #192044

Open
PlatformLC wants to merge 9 commits into
pytorch:mainfrom
PlatformLC:test/test_stage
Open

PlatformLC wants to merge 9 commits into
pytorch:mainfrom
PlatformLC:test/test_stage

Conversation

@PlatformLC

@PlatformLC PlatformLC commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Refactor test_stage.py with instantiate_device_type_tests and hardware-classification labels, so the multi-process StageTest/StageNegativeTest run as per-device variants (including out-of-tree PrivateUse1 accelerators) instead of relying on module-level device/backend globals.

Changes

  • test/distributed/pipelining/test_stage.py:
    • StageTest and StageNegativeTest now instantiate via instantiate_device_type_tests(..., except_for=["cpu"], allow_xpu=True) and are classified HardwareClassification.ACCELERATOR; this replaces main's instantiate_parametrized_tests(StageTest) (the device-type instantiation also expands the @parametrize combinations, so coverage is unchanged: 8 + 2 tests per device variant). The single-rank TestCase classes keep instantiate_parametrized_tests and are classified HardwareClassification.GENERIC — PipelineStageMetadataInferenceTest in the original change; PipelineStageBackendWarningTest, PipelineStageForwardContextTest, and OutputSlotReleaseTest, added upstream since, get the same label so the file is uniformly classified. The labels only filter tests when --hw-classification is passed.
    • The two classes share a small PipelineStageTestBase(MultiProcContinuousTest) holding backend_str and _rank_device. backend_str derives the per-device default backend from the device the framework passes (torch.device(device).type), and _rank_device(device) resolves the per-rank device from the framework-injected primary device. The module-level device_type/backend globals and the device_type/device properties that read them are removed.
    • Every test method takes the framework-injected device parameter and uses rank_device = self._rank_device(device) for model placement, input creation, and PipelineStage construction. This includes test_schedule_releases_output_before_backward, added upstream after the original change, which is converted the same way.
    • Removed the @requires_accelerator_dist_backend(["nccl", "xccl"]) decorators on the test methods — the backend availability check lives in MultiProcContinuousTest._ensure_processes_spawned via c10d.is_backend_available, which covers out-of-tree backends, so the decorator is redundant, and its hardcoded list excludes PrivateUse1's backend. The TEST_MULTIACCELERATOR skip gates are kept with the uniform "Test requires 2+ accelerators" message.
  • tools/linter/adapters/test_linter_allowlist.json: dropped this file's entry; it is classified and passes the test linter rules.

Motivation

Part of the test-case refactoring initiative tracked in #185590, decoupling tests from hardcoded device assumptions so out-of-tree accelerators can run the suite. This file switches to instantiate_device_type_tests (per-device variants) and is classified so the suite can filter it by hardware category.

Notes

  • The prerequisite framework change ([Testcase Refactoring] Use is_backend_available for MultiProcContinuousTest backend check #192186, spawn-time backend availability via c10d.is_backend_available) has already landed on main.
  • Test coverage is preserved: the set of test methods and parametrize combinations is unchanged from main; the multi-process tests (10 across the two classes) now run per device variant, and the single-rank classes are untouched apart from the classification label.

@pytorch-bot

pytorch-bot Bot commented Aug 4, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

⚠️ 4 Awaiting Approval

As of commit db05c1c with merge base 1c51a45 (image):

AWAITING APPROVAL - The following workflows need approval before CI can run:

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

@PlatformLC

Copy link
Copy Markdown
Contributor Author

@pytorchbot label "module: tests/distributed"

@pytorch-bot pytorch-bot Bot added the module: tests/distributed This label covers all test cases within the Distributed sheet. label Aug 4, 2026
PlatformLC added a commit to PlatformLC/pytorch that referenced this pull request Aug 4, 2026
…e_type_tests and add hw-classification in test_dtensor_pp_unit_tests

Enable TestDTensorPPUnitTests on out-of-tree PrivateUse1 accelerators
by switching it to
instantiate_device_type_tests(only_for=["cuda","xpu","privateuse1"],
allow_xpu=True); the framework auto-discovers the registered PrivateUse1
backend and generates a per-device variant.

Classify TestDTensorPPUnitTests as ACCELERATOR (device-exercising,
multi-process).

Drop @requires_accelerator_dist_backend from the _requires_multi_gpu
helper. The backend availability check is centralized in
MultiProcContinuousTest._ensure_processes_spawned via
c10d.is_backend_available (provided by pytorch#192044), making the decorator's
gate redundant.

Add a DTensorPPTestBase intermediate base holding backend_str. It is
defined on the base (not on TestDTensorPPUnitTests) so
instantiate_device_type_tests inherits rather than ports it -- a ported
classmethod is bound to the generic class and cannot see the generated
variant's device_type. backend_str delegates to a _resolved_device_type
helper that resolves the generic "privateuse1" token to the registered
backend name, since MultiProcContinuousTest subprocesses run tests without
calling setUpClass (where PrivateUse1TestBase would otherwise resolve it).

Switch init_pg to torch.accelerator.set_device_index and resolve the
device type per-variant in _make_mesh.

Test Plan:
```
python test/distributed/pipelining/test_dtensor_pp_unit_tests.py -v --hw-classification ACCELERATOR
```
--hw-classification ACCELERATOR selects the device-exercising tests;
--hw-classification GENERIC/CPU filter the ACCELERATOR class out (Ran 0).
@jishuangfeng

Copy link
Copy Markdown

The common_distributed.py and test_stage.py changes look good — c10d.is_backend_available() correctly covers third-party backends, and _resolved_device_type() properly handles the privateuse1 token in subprocesses.

One ask: please split the common_distributed.py change into a separate PR landing first, so test_stage.py rebases on top — per the convention of keeping base-class/tooling changes separate.

PlatformLC added a commit to PlatformLC/pytorch that referenced this pull request Aug 5, 2026
…e_type_tests and add hw-classification in test_dtensor_pp_integration

Enable TestDTensorPPModes on out-of-tree PrivateUse1 accelerators
by switching it to
instantiate_device_type_tests(only_for=["cuda","xpu","privateuse1"],
allow_xpu=True); the framework auto-discovers the registered PrivateUse1
backend and generates a per-device variant.

Classify TestDTensorPPModes as ACCELERATOR (device-exercising,
multi-process).

Drop @requires_accelerator_dist_backend from the _requires_multi_gpu
helper. The backend availability check is centralized in
MultiProcContinuousTest._ensure_processes_spawned via
c10d.is_backend_available (provided by pytorch#192044), making the decorator's
gate redundant.

Add a _resolved_device_type() helper to the existing
DTensorPPIntegrationBase intermediate base (which already holds
backend_str), resolving the generic "privateuse1" token to the registered
backend name since MultiProcContinuousTest subprocesses run tests without
calling setUpClass. backend_str, the device property, init_pg, and
_make_mesh all resolve the device type per-variant. Switch init_pg to
torch.accelerator.set_device_index (Pattern D).

Test Plan:
```
python test/distributed/pipelining/test_dtensor_pp_integration.py -v --hw-classification ACCELERATOR
```
--hw-classification ACCELERATOR selects the device-exercising tests;
--hw-classification GENERIC/CPU filter them out (Ran 0).
PlatformLC added a commit to PlatformLC/pytorch that referenced this pull request Aug 5, 2026
…e_device_type_tests and hw-classification

Switch ComposabilityTest to instantiate_device_type_tests(only_for=["cuda","xpu","privateuse1"], allow_xpu=True) and classify it ACCELERATOR. Add a ComposabilityTestBase intermediate base with backend_str + a _resolved_device_type() helper that resolves the generic "privateuse1" token (MultiProcContinuousTest subprocesses skip setUpClass, so cls.device_type stays the token). Remove the old backend_str()/device_type() classmethods and @requires_accelerator_dist_backend() (the backend check is centralized via is_backend_available in pytorch#192044). Keep @skip_if_lt_x_gpu(8) and @skip_but_pass_in_sandcastle_if(not at_least_x_gpu(8), ...), made device-agnostic by pytorch#187650 and the previous commit.

Test Plan:
```
python test/distributed/_composable/test_composability/test_pp_composability.py -v --hw-classification ACCELERATOR
```
--hw-classification ACCELERATOR selects the tests; GENERIC/CPU filter them out (Ran 0).
@PlatformLC PlatformLC changed the title [Testcase Refactoring] Use is_backend_available for MultiProcContinuousTest and refactor test_stage [Testcase Refactoring] Enable PrivateUse1 tests via instantiate_device_type_tests and add hw-classification in test_stage Aug 5, 2026
@PlatformLC PlatformLC changed the title [Testcase Refactoring] Enable PrivateUse1 tests via instantiate_device_type_tests and add hw-classification in test_stage [Testcase Refactoring] Refactor test_stage with instantiate_device_type_tests and hw-classification Aug 5, 2026
@PlatformLC

Copy link
Copy Markdown
Contributor Author

The common_distributed.py and test_stage.py changes look good — c10d.is_backend_available() correctly covers third-party backends, and _resolved_device_type() properly handles the privateuse1 token in subprocesses.

One ask: please split the common_distributed.py change into a separate PR landing first, so test_stage.py rebases on top — per the convention of keeping base-class/tooling changes separate.

splited the common change, PTAL #192186

@pytorch-bot pytorch-bot Bot added the topic: not user facing topic category label Aug 5, 2026
@jishuangfeng

Copy link
Copy Markdown

The base-class split is submitted as a standalone PR as discussed(#192186 ), looks as expected.

@jishuangfeng jishuangfeng left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTM.

@jbschlosser jbschlosser added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Aug 5, 2026
PlatformLC added a commit to PlatformLC/pytorch that referenced this pull request Aug 7, 2026
…e_type_tests and add hw-classification in test_dtensor_pp_unit_tests

Enable TestDTensorPPUnitTests on out-of-tree PrivateUse1 accelerators
by switching it to
instantiate_device_type_tests(only_for=["cuda","xpu","privateuse1"],
allow_xpu=True); the framework auto-discovers the registered PrivateUse1
backend and generates a per-device variant.

Classify TestDTensorPPUnitTests as ACCELERATOR (device-exercising,
multi-process).

Drop @requires_accelerator_dist_backend from the _requires_multi_gpu
helper. The backend availability check is centralized in
MultiProcContinuousTest._ensure_processes_spawned via
c10d.is_backend_available (provided by pytorch#192044), making the decorator's
gate redundant.

Add a DTensorPPTestBase intermediate base holding backend_str. It is
defined on the base (not on TestDTensorPPUnitTests) so
instantiate_device_type_tests inherits rather than ports it -- a ported
classmethod is bound to the generic class and cannot see the generated
variant's device_type. backend_str delegates to a _resolved_device_type
helper that resolves the generic "privateuse1" token to the registered
backend name, since MultiProcContinuousTest subprocesses run tests without
calling setUpClass (where PrivateUse1TestBase would otherwise resolve it).

Switch init_pg to torch.accelerator.set_device_index and resolve the
device type per-variant in _make_mesh.

Test Plan:
```
python test/distributed/pipelining/test_dtensor_pp_unit_tests.py -v --hw-classification ACCELERATOR
```
--hw-classification ACCELERATOR selects the device-exercising tests;
--hw-classification GENERIC/CPU filter the ACCELERATOR class out (Ran 0).
PlatformLC added a commit to PlatformLC/pytorch that referenced this pull request Aug 7, 2026
…e_type_tests and add hw-classification in test_dtensor_pp_integration

Enable TestDTensorPPModes on out-of-tree PrivateUse1 accelerators
by switching it to
instantiate_device_type_tests(only_for=["cuda","xpu","privateuse1"],
allow_xpu=True); the framework auto-discovers the registered PrivateUse1
backend and generates a per-device variant.

Classify TestDTensorPPModes as ACCELERATOR (device-exercising,
multi-process).

Drop @requires_accelerator_dist_backend from the _requires_multi_gpu
helper. The backend availability check is centralized in
MultiProcContinuousTest._ensure_processes_spawned via
c10d.is_backend_available (provided by pytorch#192044), making the decorator's
gate redundant.

Add a _resolved_device_type() helper to the existing
DTensorPPIntegrationBase intermediate base (which already holds
backend_str), resolving the generic "privateuse1" token to the registered
backend name since MultiProcContinuousTest subprocesses run tests without
calling setUpClass. backend_str, the device property, init_pg, and
_make_mesh all resolve the device type per-variant. Switch init_pg to
torch.accelerator.set_device_index (Pattern D).

Test Plan:
```
python test/distributed/pipelining/test_dtensor_pp_integration.py -v --hw-classification ACCELERATOR
```
--hw-classification ACCELERATOR selects the device-exercising tests;
--hw-classification GENERIC/CPU filter them out (Ran 0).
PlatformLC added a commit to PlatformLC/pytorch that referenced this pull request Aug 7, 2026
…e_device_type_tests and hw-classification

Switch ComposabilityTest to instantiate_device_type_tests(only_for=["cuda","xpu","privateuse1"], allow_xpu=True) and classify it ACCELERATOR. Add a ComposabilityTestBase intermediate base with backend_str + a _resolved_device_type() helper that resolves the generic "privateuse1" token (MultiProcContinuousTest subprocesses skip setUpClass, so cls.device_type stays the token). Remove the old backend_str()/device_type() classmethods and @requires_accelerator_dist_backend() (the backend check is centralized via is_backend_available in pytorch#192044). Keep @skip_if_lt_x_gpu(8) and @skip_but_pass_in_sandcastle_if(not at_least_x_gpu(8), ...), made device-agnostic by pytorch#187650 and the previous commit.

Test Plan:
```
python test/distributed/_composable/test_composability/test_pp_composability.py -v --hw-classification ACCELERATOR
```
--hw-classification ACCELERATOR selects the tests; GENERIC/CPU filter them out (Ran 0).
@fffrog fffrog moved this to Backlog in PyTorch Test Refactoring Aug 7, 2026

@fffrog fffrog left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think we need to discuss this, because it's a very common scenario in distributed test cases.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Removing this one, and using device parameter instreadly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

updated. PTAL, thx~

@fffrog fffrog moved this from Backlog to Ready for Review(Initial) in PyTorch Test Refactoring Aug 18, 2026
@PlatformLC
PlatformLC requested a review from fffrog August 19, 2026 09:26
kerer-ai pushed a commit to kerer-ai/pytorch-nv that referenced this pull request Aug 20, 2026

@jishuangfeng jishuangfeng left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code looks good. Good to merge after #192186 lands.
LGTM.

pytorchmergebot pushed a commit that referenced this pull request Aug 24, 2026
…usTest backend check (#192186)

## Summary
Replace `MultiProcContinuousTest`'s hardcoded backend-availability dict in `_ensure_processes_spawned` with `c10d.is_backend_available(backend)`. The generic API covers any backend registered via `Backend.register_backend`, including out-of-tree PrivateUse1 accelerators' backends, so the spawn-time check no longer silently passes through unlisted backends.

## Changes
- `torch/testing/_internal/common_distributed.py`: Replaced the hardcoded `{nccl, gloo, mpi, xccl}` backend-availability dict in `MultiProcContinuousTest._ensure_processes_spawned` with `c10d.is_backend_available(backend)`. This is the prerequisite for test-file PRs (e.g. test_stage, #192044) to drop their `@requires_accelerator_dist_backend` gate: with the backend availability check centralized here, the decorator becomes redundant.

## Motivation
Part of the test-case refactoring initiative (#185590) to decouple tests from hardcoded CUDA/NCCL assumptions so out-of-tree PrivateUse1 accelerators can run the suite. Split out from #192044 per review so the base-class/tooling change lands first and the test-file changes rebase on top.

## Notes
This is part of the test case refactoring initiative tracked in #185590.
Pull Request resolved: #192186
Approved by: https://github.com/jishuangfeng, https://github.com/fffrog, https://github.com/albanD
kerer-ai pushed a commit to kerer-ai/pytorch-nv that referenced this pull request Aug 26, 2026
orrangetabby17 pushed a commit to orrangetabby17/pytorch that referenced this pull request Sep 2, 2026
…usTest backend check (pytorch#192186)

## Summary
Replace `MultiProcContinuousTest`'s hardcoded backend-availability dict in `_ensure_processes_spawned` with `c10d.is_backend_available(backend)`. The generic API covers any backend registered via `Backend.register_backend`, including out-of-tree PrivateUse1 accelerators' backends, so the spawn-time check no longer silently passes through unlisted backends.

## Changes
- `torch/testing/_internal/common_distributed.py`: Replaced the hardcoded `{nccl, gloo, mpi, xccl}` backend-availability dict in `MultiProcContinuousTest._ensure_processes_spawned` with `c10d.is_backend_available(backend)`. This is the prerequisite for test-file PRs (e.g. test_stage, pytorch#192044) to drop their `@requires_accelerator_dist_backend` gate: with the backend availability check centralized here, the decorator becomes redundant.

## Motivation
Part of the test-case refactoring initiative (pytorch#185590) to decouple tests from hardcoded CUDA/NCCL assumptions so out-of-tree PrivateUse1 accelerators can run the suite. Split out from pytorch#192044 per review so the base-class/tooling change lands first and the test-file changes rebase on top.

## Notes
This is part of the test case refactoring initiative tracked in pytorch#185590.
Pull Request resolved: pytorch#192186
Approved by: https://github.com/jishuangfeng, https://github.com/fffrog, https://github.com/albanD
aeytkn pushed a commit to aeytkn/pytorch that referenced this pull request Sep 3, 2026
…usTest backend check (pytorch#192186)

## Summary
Replace `MultiProcContinuousTest`'s hardcoded backend-availability dict in `_ensure_processes_spawned` with `c10d.is_backend_available(backend)`. The generic API covers any backend registered via `Backend.register_backend`, including out-of-tree PrivateUse1 accelerators' backends, so the spawn-time check no longer silently passes through unlisted backends.

## Changes
- `torch/testing/_internal/common_distributed.py`: Replaced the hardcoded `{nccl, gloo, mpi, xccl}` backend-availability dict in `MultiProcContinuousTest._ensure_processes_spawned` with `c10d.is_backend_available(backend)`. This is the prerequisite for test-file PRs (e.g. test_stage, pytorch#192044) to drop their `@requires_accelerator_dist_backend` gate: with the backend availability check centralized here, the decorator becomes redundant.

## Motivation
Part of the test-case refactoring initiative (pytorch#185590) to decouple tests from hardcoded CUDA/NCCL assumptions so out-of-tree PrivateUse1 accelerators can run the suite. Split out from pytorch#192044 per review so the base-class/tooling change lands first and the test-file changes rebase on top.

## Notes
This is part of the test case refactoring initiative tracked in pytorch#185590.
Pull Request resolved: pytorch#192186
Approved by: https://github.com/jishuangfeng, https://github.com/fffrog, https://github.com/albanD
@can-gaa-hou

Copy link
Copy Markdown
Collaborator

@claude review this pr with pr review skill

@claude

claude Bot commented Sep 7, 2026 •

Copy link
Copy Markdown

Claude finished @can-gaa-hou's task in 18m 51s —— View job


PR Review: #192044

Reviewed with the /pr-review skill — this converts test_stage.py's multi-process pipeline-stage tests to instantiate_device_type_tests so PrivateUse1 accelerators get a generated variant, and I found four issues worth resolving before merge (the most important being that the privateuse1 -> real-backend-name resolution helper was dropped in the final commit, and that except_for=["cpu"] contradicts the PR description and admits an HPU variant that hard-errors).

Full review details

Summary

Switches StageTest/StageNegativeTest to instantiate_device_type_tests, adds a PipelineStageTestBase holding backend_str, injects a device parameter into the ACCELERATOR test methods, and annotates hw_classification. The mechanism is sound and the hw_classification attributes do propagate correctly through class generation (common_device_type.py:1168 copies non-test members). The problems below are in backend_str's device-type resolution, the device-base selection filter, and a skip predicate that no longer matches the variant it guards.

Infrastructure

1. backend_str() dropped the privateuse1 token resolution — the case this PR exists to enable.

test/distributed/pipelining/test_stage.py:128-130:

@classmethod
def backend_str(cls) -> str:
    return dist.get_default_backend_for_device(cls.device_type)

PrivateUse1TestBase.device_type is the literal string "privateuse1" (common_device_type.py:880) and is only rewritten to the registered backend name in PrivateUse1TestBase.setUpClass (common_device_type.py:902). get_default_backend_for_device (distributed_c10d.py:2047-2067) looks up Backend.default_device_backend_map, which holds only cpu/cuda/xpu/mps (distributed_c10d.py:472-477) plus entries added by Backend.register_backend(..., devices=[...]) — and those are keyed on the real device name ("openreg"), never "privateuse1". So if cls.device_type is still the generic token when backend_str() runs, this raises ValueError: Default backend not registered for device : privateuse1.

Commit 83bb28e4 had exactly this guard and commit 8cee2da3 removed it:

    def _resolved_device_type(cls) -> str:
        dt = cls.device_type
        if dt == "privateuse1":
            dt = torch._C._get_privateuse1_backend_name()
        return dt

Today the token happens to get resolved because instantiate_test_helper calls cls._init_and_get_primary_device() at import time (common_device_type.py:609), which falls into its except branch and calls cls.setUpClass() (common_device_type.py:529-537) — and that only fires because the test methods now take a device parameter. That is an incidental ordering dependency, not a guarantee: _worker_loop (common_distributed.py:1902) and _ensure_processes_spawned (common_distributed.py:2081) both invoke backend_str() without any setUpClass call of their own, and common_device_type.py:958-962 documents that whether device_type has been rewritten is itself state-dependent across instantiate_device_type_tests calls.

Please restore _resolved_device_type() (or equivalent). Note also that the PR description still documents _resolved_device_type as present and as the reason the base class exists, so either the description is stale or the removal was unintentional. Fix this →

2. except_for=["cpu"] is an open-ended denylist; it contradicts the PR description and admits an HPU variant that hard-errors.

test_stage.py:402 and 479-484 use except_for=["cpu"], but the PR description states only_for=["cuda", "xpu", "privateuse1"]. The denylist is not equivalent: get_desired_device_type_test_bases appends HPUTestBase unconditionally whenever TEST_HPU (common_device_type.py:1037-1038), and PYTORCH_TESTING_DEVICE_FOR_CUSTOM / PYTORCH_TESTING_DEVICE_ONLY_FOR can inject further bases (common_device_type.py:1061-1086). "hpu" is not in Backend.default_device_backend_map, so an HPU variant makes backend_str() raise ValueError out of _ensure_processes_spawned (common_distributed.py:2081) — an error, not a skip, which turns the suite red on HPU hosts rather than skipping it. Switch to the only_for the description already promises. Fix this →

3. backend_str duplicates DeviceTypeTestBase.distributed_backend().

common_device_type.py:448-454 already is:

@classmethod
def distributed_backend(cls) -> str:
    import torch.distributed as dist
    return dist.get_default_backend_for_device(cls.device_type)

That is character-for-character what the new backend_str body does, and it is reachable on every generated class through the MRO. Delegate to it (return cls.distributed_backend()) rather than re-deriving it here, so the resolution rule lives in one place. This does not by itself fix finding 1 — distributed_backend reads cls.device_type the same way — so if the shared helper is the right home, the token resolution belongs there too.

Testing

4. The TEST_MULTIACCELERATOR skip no longer matches the variant it guards.

All seven methods keep @skip_but_pass_in_sandcastle_if(not TEST_MULTIACCELERATOR, "Test requires 2+ accelerators") (e.g. test_stage.py:142-144). TEST_MULTIACCELERATOR = LazyVal(lambda: torch.accelerator.device_count() > 1) (common_utils.py:1714) is scoped to the current accelerator. Before this PR that was self-consistent, because the class was pinned to torch.accelerator.current_accelerator(). Now that one variant is generated per device type, the predicate and the variant can disagree in both directions: on a host with 1 CUDA GPU and 8 PrivateUse1 devices the privateuse1 variant is wrongly skipped, and on a host with 2 CUDA GPUs but 1 PrivateUse1 device the privateuse1 variant is not skipped and instead fails somewhere downstream.

It is also frozen at import: not TEST_MULTIACCELERATOR invokes LazyVal.__bool__ at decoration time (common_utils.py:1670-1690 installs __bool__), so a late-registering device module cannot change the outcome. The device-count gate should be derived from the variant's own device type — MultiProcContinuousTest._get_world_size (common_distributed.py:2016-2037) already does torch.get_device_module(device_type).device_count() and skips at zero; extending that to require 2+ would make the per-method decorators both correct and unnecessary.

Code Quality

5. The comment in _rank_device misstates what the injected device is.

test_stage.py:133-134 says the value is "{type}:0", rank-0. It is the current device index, not index 0 — CUDATestBase.setUpClass sets f"cuda:{torch.cuda.current_device()}" (common_device_type.py:785), XPUTestBase likewise (:858), and PrivateUse1TestBase uses current_device() (:909). Since _rank_device discards the index and keeps only .type, the comment should just say that the injected index is deliberately ignored in favor of self.rank.

6. Please confirm the dependency actually provides the check the description cites.

The description justifies deleting all seven @requires_accelerator_dist_backend(["nccl", "xccl"]) decorators by pointing at is_backend_available in MultiProcContinuousTest._ensure_processes_spawned. On this branch that check is a hardcoded four-key dict and silently treats unknown backends as available (common_distributed.py:2083-2091):

backend_checks = {"nccl": ..., "gloo": ..., "mpi": ..., "xccl": ...}
check_fn = backend_checks.get(backend)
if check_fn is not None and not check_fn():
    raise unittest.SkipTest(...)

is_backend_available does not appear anywhere in common_distributed.py here, so #192186 is not yet in this branch. That is expected given the stated landing order, but it means the redundancy argument is currently unverified — for occl/hccl the gate would be a no-op. Worth re-running after #192186 lands to confirm those backends skip rather than fail.

Recommendation

Request Changes — findings 1 and 2 are correctness issues on the PrivateUse1/HPU paths this PR is specifically about, and finding 4 makes the skip predicate wrong for any non-current-accelerator variant. Findings 3, 5, and 6 are cleanups and a verification step.

· branch `test/test_stage`

kerer-ai pushed a commit to kerer-ai/pytorch-nv that referenced this pull request Sep 7, 2026
@PlatformLC

Copy link
Copy Markdown
Contributor Author

@pytorchbot label "no-stale"

@pytorch-bot pytorch-bot Bot added the no-stale label Oct 19, 2026
PlatformLC added a commit to PlatformLC/pytorch that referenced this pull request Dec 2, 2026
…e_type_tests and add hw-classification in test_dtensor_pp_unit_tests

Enable TestDTensorPPUnitTests on out-of-tree PrivateUse1 accelerators
by switching it to
instantiate_device_type_tests(only_for=["cuda","xpu","privateuse1"],
allow_xpu=True); the framework auto-discovers the registered PrivateUse1
backend and generates a per-device variant.

Classify TestDTensorPPUnitTests as ACCELERATOR (device-exercising,
multi-process).

Drop @requires_accelerator_dist_backend from the _requires_multi_gpu
helper. The backend availability check is centralized in
MultiProcContinuousTest._ensure_processes_spawned via
c10d.is_backend_available (provided by pytorch#192044), making the decorator's
gate redundant.

Add a DTensorPPTestBase intermediate base holding backend_str. It is
defined on the base (not on TestDTensorPPUnitTests) so
instantiate_device_type_tests inherits rather than ports it -- a ported
classmethod is bound to the generic class and cannot see the generated
variant's device_type. backend_str delegates to a _resolved_device_type
helper that resolves the generic "privateuse1" token to the registered
backend name, since MultiProcContinuousTest subprocesses run tests without
calling setUpClass (where PrivateUse1TestBase would otherwise resolve it).

Switch init_pg to torch.accelerator.set_device_index and resolve the
device type per-variant in _make_mesh.

Test Plan:
```
python test/distributed/pipelining/test_dtensor_pp_unit_tests.py -v --hw-classification ACCELERATOR
```
--hw-classification ACCELERATOR selects the device-exercising tests;
--hw-classification GENERIC/CPU filter the ACCELERATOR class out (Ran 0).
PlatformLC added a commit to PlatformLC/pytorch that referenced this pull request Dec 2, 2026
…e_type_tests and add hw-classification in test_dtensor_pp_integration

Enable TestDTensorPPModes on out-of-tree PrivateUse1 accelerators
by switching it to
instantiate_device_type_tests(only_for=["cuda","xpu","privateuse1"],
allow_xpu=True); the framework auto-discovers the registered PrivateUse1
backend and generates a per-device variant.

Classify TestDTensorPPModes as ACCELERATOR (device-exercising,
multi-process).

Drop @requires_accelerator_dist_backend from the _requires_multi_gpu
helper. The backend availability check is centralized in
MultiProcContinuousTest._ensure_processes_spawned via
c10d.is_backend_available (provided by pytorch#192044), making the decorator's
gate redundant.

Add a _resolved_device_type() helper to the existing
DTensorPPIntegrationBase intermediate base (which already holds
backend_str), resolving the generic "privateuse1" token to the registered
backend name since MultiProcContinuousTest subprocesses run tests without
calling setUpClass. backend_str, the device property, init_pg, and
_make_mesh all resolve the device type per-variant. Switch init_pg to
torch.accelerator.set_device_index (Pattern D).

Test Plan:
```
python test/distributed/pipelining/test_dtensor_pp_integration.py -v --hw-classification ACCELERATOR
```
--hw-classification ACCELERATOR selects the device-exercising tests;
--hw-classification GENERIC/CPU filter them out (Ran 0).
PlatformLC added a commit to PlatformLC/pytorch that referenced this pull request Dec 2, 2026
…e_device_type_tests and hw-classification

Switch ComposabilityTest to instantiate_device_type_tests(only_for=["cuda","xpu","privateuse1"], allow_xpu=True) and classify it ACCELERATOR. Add a ComposabilityTestBase intermediate base with backend_str + a _resolved_device_type() helper that resolves the generic "privateuse1" token (MultiProcContinuousTest subprocesses skip setUpClass, so cls.device_type stays the token). Remove the old backend_str()/device_type() classmethods and @requires_accelerator_dist_backend() (the backend check is centralized via is_backend_available in pytorch#192044). Keep @skip_if_lt_x_gpu(8) and @skip_but_pass_in_sandcastle_if(not at_least_x_gpu(8), ...), made device-agnostic by pytorch#187650 and the previous commit.

Test Plan:
```
python test/distributed/_composable/test_composability/test_pp_composability.py -v --hw-classification ACCELERATOR
```
--hw-classification ACCELERATOR selects the tests; GENERIC/CPU filter them out (Ran 0).
…e_type_tests and add hw-classification in test_stage

Enable StageTest/StageNegativeTest on out-of-tree PrivateUse1 accelerators
by switching them to
instantiate_device_type_tests(only_for=["cuda","xpu","privateuse1"],
allow_xpu=True); the framework auto-discovers the registered PrivateUse1
backend (via get_desired_device_type_test_bases /
_is_privateuse1_backend_available) and generates a per-device variant.

Classify StageTest/StageNegativeTest as ACCELERATOR (device-exercising,
multi-process) and PipelineStageMetadataInferenceTest as GENERIC (pure
metadata-inference logic, no device).

Drop @requires_accelerator_dist_backend from every test method: the backend
availability check now lives in MultiProcContinuousTest._ensure_processes_spawned
via is_backend_available (framework commit), so the decorator's gate is
redundant.

Add a PipelineStageTestBase intermediate base holding backend_str. It is
defined on the base (not on StageTest) so instantiate_device_type_tests
inherits rather than ports it -- a ported classmethod is bound to the
generic class and cannot see the generated variant's device_type.
backend_str delegates to a _resolved_device_type helper that resolves the
generic "privateuse1" token to the registered backend name, since
MultiProcContinuousTest subprocesses run tests without calling setUpClass
(where PrivateUse1TestBase would otherwise resolve it).

Test Plan:
```
python test/distributed/pipelining/test_stage.py -v --hw-classification ACCELERATOR
```
8 ACCELERATOR-classified tests pass on a PrivateUse1 host (8 devices);
--hw-classification GENERIC selects the 1 GENERIC metadata-inference test;
--hw-classification CPU filters the ACCELERATOR class out (Ran 0).
…t methods

TEST_LINTER (pytorch#190173) requires every method in an instantiated
ACCELERATOR class to take a `device` parameter; the 7 methods in
StageTest/StageNegativeTest lacked it. Add `device` to each plus a
`_rank_device(device)` helper on PipelineStageTestBase that resolves
the framework-injected primary device ("{type}:0", rank-0) to this
worker's per-rank device (torch.device(type, self.rank)), preserving
the value of the removed `device` @Property. As the per-rank device
now derives from the injected `device` rather than `cls.device_type`,
`backend_str` simplifies to `return backend` (module-level, matching
test_dtensor_pp_unit_tests/integration) and the obsolete
`_resolved_device_type` classmethod is removed.

Test Plan:
```python
python test/distributed/pipelining/test_stage.py -v
# --hw-classification ACCELERATOR selects the multi-proc stage tests,
# GENERIC the single-process metadata-inference test, CPU none
```
…in test_stage

The module-level `device_type`/`backend` globals were computed once at import
from `torch.accelerator.current_accelerator()`. Replace with per-variant
resolution: `backend_str` now returns
`dist.get_default_backend_for_device(cls.device_type)` (resolved at runtime
via the device-type test base's setUpClass), and the multi-accelerator skip
messages drop the backend-name prefix for a plain device-agnostic
"Test requires 2+ accelerators" string (the skip condition is unchanged).
`_rank_device` is kept.

Test Plan:
```
python test/distributed/pipelining/test_stage.py -v
```
Runs the multi-process pipeline stage tests on the available accelerator
variant; the per-variant backend resolves via cls.device_type.
MultiProcContinuousTest.backend_str now receives the device argument from
the framework; accept it in the override and derive the device type from
it (torch.device(device).type), matching _rank_device, instead of reading
cls.device_type.

Test Plan:
python3 tools/linter/adapters/test_linter.py test/distributed/pipelining/test_stage.py
…ut_before_backward to the device param

Upstream added this test after the original refactoring, with the old
pattern: @requires_accelerator_dist_backend(["nccl", "xccl"]) and the
module-global backend in its skip message. Apply the same conversion as
the other StageTest methods: drop the decorator (the spawn-time
c10d.is_backend_available check covers availability and the hardcoded
list excludes PrivateUse1's backend), take the framework-injected device,
resolve the per-rank device with _rank_device, and use the class's
uniform skip message.

Test Plan:
python3 tools/linter/adapters/test_linter.py test/distributed/pipelining/test_stage.py
The file is classified in this branch (GENERIC single-rank classes and
ACCELERATOR classes with instantiate_device_type_tests and
device-parametrized test methods), so it no longer needs the allowlist
exemption.

Test Plan:
python3 tools/linter/adapters/test_linter.py test/distributed/pipelining/test_stage.py
…stage as GENERIC

The hw_classification rule requires every test class in a classified
file to carry a label. PipelineStageMetadataInferenceTest already has
GENERIC; apply the same label to the other single-rank TestCase classes
(PipelineStageBackendWarningTest, PipelineStageForwardContextTest,
OutputSlotReleaseTest), which run with a single-rank gloo group or pure
mocks and have no device dependency. The label is metadata and only
filters tests when --hw-classification is passed.

Test Plan:
python3 tools/linter/adapters/test_linter.py test/distributed/pipelining/test_stage.py
lintrunner test/distributed/pipelining/test_stage.py
…ort in test_stage

The replay dropped the import together with the old
instantiate_parametrized_tests(StageTest) call, but upstream's new
single-rank classes (PipelineStageBackendWarningTest,
PipelineStageForwardContextTest) still expand their @parametrize
combinations through it. Keep it alongside
instantiate_device_type_tests, which the ACCELERATOR classes use.

Test Plan:
lintrunner test/distributed/pipelining/test_stage.py
python3 tools/linter/adapters/test_linter.py test/distributed/pipelining/test_stage.py
…mon_utils in test_stage

It lives in common_utils (as in the rest of the tree), not
common_device_type; the previous commit put it in the wrong block and
broke the import.

Test Plan:
lintrunner test/distributed/pipelining/test_stage.py
python3 tools/linter/adapters/test_linter.py test/distributed/pipelining/test_stage.py

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module: tests/distributed This label covers all test cases within the Distributed sheet. no-stale 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: Ready for Review(Initial)

Development

Successfully merging this pull request may close these issues.

6 participants