Skip to content

[Testcase Refactoring] Add new class MultiProcContinuousForInstantiateTest - #195000

Closed
zjq-86 wants to merge 1 commit into
pytorch:mainfrom
zjq-86:test/composability_common
Closed

zjq-86 wants to merge 1 commit into
pytorch:mainfrom
zjq-86:test/composability_common

Conversation

@zjq-86

@zjq-86 zjq-86 commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Summary

Added a new test base class MultiProcContinuousForInstantiateTest in
torch/testing/_internal/common_distributed.py. It is a thin subclass of
MultiProcContinuousTest that bridges the continuous-worker distributed test
base with device-type-parameterized test classes generated by
instantiate_device_type_tests (the ComposabilityTest use case), so that
distributed tests can resolve the communication backend and the per-rank
device through the base class instead of re-implementing them per test.

Changes

New class: MultiProcContinuousForInstantiateTest (extends MultiProcContinuousTest)

  1. backend_str() classmethod

    • Returns c10d.get_default_backend_for_device(cls.device_type).
    • The backend (nccl / hccl / gloo / ...) is resolved dynamically from the
      framework registry, keyed by the device_type string class attribute
      that instantiate_device_type_tests injects on the generated subclass.
  2. current_device property

    • Gives every test a concrete per-rank device without duplicating the
      (device_type, rank) -> torch.device mapping in each test class.

What the class intentionally does NOT do

  • Does not override _init_pg: the parent's _init_pg already performs
    accelerator detection via torch.accelerator.current_accelerator() /
    torch.accelerator.device_count(), per-rank device binding via
    torch.accelerator.set_device_index(rank % device_count), and per-rank
    HCCL port segmentation (HCCL_NPU_SOCKET_PORT_RANGE) for the NPU/hccl case.
  • Does not define a device attribute: tests should use either the device
    kwarg injected by instantiate_test (string form, e.g. "npu:0") or
    self.current_device (torch.device form).

Motivation

MultiProcContinuousTest requires a working backend_str() to call
c10d.init_process_group inside _init_pg, but the parent class does not
define backend_str() or device_type itself. Device-type test classes
produced by instantiate_device_type_tests receive a device_type string
class attribute, but no backend resolution and no device helper. Before this
change, every distributed test that wanted to be parameterized over device
types (e.g. ComposabilityTest) had to re-implement both on its own, which
was redundant and error-prone (a previous bug where self.device_type
evaluated to a classmethod object rather than a string came from exactly
this gap).

Test Plan

Verified on the composability distributed test suite, e.g.:

python test/distributed/test_composability.py

Notes

@zjq-86
zjq-86 force-pushed the test/composability_common branch from 6bbbefd to 342cd1c Compare August 27, 2026 09:48
@pytorch-bot

pytorch-bot Bot commented Aug 27, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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

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

@pytorch-bot

pytorch-bot Bot commented Aug 27, 2026

Copy link
Copy Markdown

This PR needs a release notes: label

If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@zjq-86

zjq-86 commented Aug 27, 2026

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 27, 2026
@fffrog fffrog moved this to Backlog in PyTorch Test Refactoring Aug 27, 2026

@JamesD18 JamesD18 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.

LGTM.This has been extracted into Common per fffrog’s feedback in #190518.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

device_type in parent class MultiProcContinuousTest is classmethod, self.device_type here means it should be property. Please check this.

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.

After being instantiated through instantiate_device_type_tests, self.device_type here refers to an attribute within the device class (e.g., PrivateUse1TestBase)), not a class method within MultiProcContinuousTest.

@shichuanliujie9527

Copy link
Copy Markdown
Contributor

I stacked the current #192214 head (560ae5fd924) on #195000
(342cd1c9cf6) and ran the full target file in both NPU and CPU modes. Both
runs fail only
PgTransportCPUCPU.test_pg_transport_with_sharded_tensor_cpu with the same
error:

ValueError: Local shard tensor device does not match with local Shard's placement!
Found local shard tensor device: cpu, local shard metadata placement device: cpu:0

The common current_device property returns torch.device("cpu", rank) in a
worker. A CPU tensor created with that device is normalized to cpu, while the
ShardedTensor placement string remains cpu:<rank>.

Would you prefer fixing this in #195000 by keeping CPU unindexed, for example:

if self.rank < 0 or self.device_type == "cpu":
    return torch.device(self.device_type)
return torch.device(self.device_type, self.rank)

I validated that exact one-line candidate on the same stacked source. The NPU
run passed all 21 tests in 28.186 s and the CPU run passed all 17 tests in
5.582 s, both with zero skips and exit code 0. The NPU worker contract remained
npu:<rank>, while CPU workers used unindexed cpu.

I have not added a local device-resolution workaround back to #192214 because
the requested direction was to reuse the common base. The failing runs were
21 tests/1 error/0 skips in NPU mode and 17 tests/1 error/0 skips in CPU mode;
both source trees were restored after validation.

…eTest

MultiProcContinuousForInstantiateTest is a thin subclass of
MultiProcContinuousTest that bridges the continuous-worker distributed
test base with device-type-parameterized test classes generated by
instantiate_device_type_tests. It exists so that distributed test
classes can rely on the base class (instead of per-test overrides) to
resolve the backend string and the per-rank torch.device, while still
honoring the device_type string attribute injected by
instantiate_device_type_tests.

@KarhouTam KarhouTam 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.

Since the new class inherits from MultiProcContinuousTest, which needs refactoring for OOT device compatibility, I think we should hold this PR for now.

zhaiyaxing001 added a commit to zhaiyaxing001/pytorch that referenced this pull request Nov 18, 2026
@danieldale2026

Copy link
Copy Markdown
Contributor

I’d like the new base class to keep working with the device argument that the current continuous-worker fixture passes when it starts a test.

AI-assisted review. On c756b16, calling the new override as backend_str() for a CPU subclass returns gloo. I also tried the override unchanged with MultiProcContinuousTest from main at 1d1dbb8. For a two-rank CPU subclass, _ensure_processes_spawned() calls cls.backend_str(device_type) and raises TypeError before it can start the workers. When resolving the common_distributed.py conflict, could you have the override accept device and pass it to get_default_backend_for_device(device), then exercise that startup path?

@zjq-86

zjq-86 commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

The core problem this PR set out to solve — bridging device_type for
test classes instantiated via instantiate_device_type_tests — has
meanwhile been addressed on main by #205504

MultiProcContinuousTest._ensure_processes_spawned() and _init_pg()
on main now normalize device_type across all four forms (the string
attribute injected by instantiate_device_type_tests / property /
callable / classmethod) and invoke backend_str(cls, device) with the
resolved device.

With that in place, the bridging half of this PR is redundant on main.

Closing in favor of #205504. Thanks @jialinw77 @danieldale2026 for the
reviews that clarified the situation.

@zjq-86 zjq-86 closed this Dec 3, 2026
@github-project-automation github-project-automation Bot moved this from Backlog to Done in PyTorch Test Refactoring Dec 3, 2026
zhaiyaxing001 added a commit to zhaiyaxing001/pytorch that referenced this pull request Dec 3, 2026
…conflict

The bot rebase onto viable/strict hit a content conflict when replaying the first PR commit over upstream changes to test_replicate_with_fsdp.py and common_distributed.py. Merging main (5cda694) resolves it as a whole-branch three-way merge: common_distributed.py only takes the upstream symm-mem ROCm arch tweak, and the MultiProcContinuousForInstantiateTest class kept from pytorch#195000 (closed without merge) stays compatible with the upstream MultiProcContinuousTest base, whose backend_str now takes the device argument per pytorch#205504.
zhaiyaxing001 added a commit to zhaiyaxing001/pytorch that referenced this pull request Dec 3, 2026
… rationale

pytorch#195000 closed in favor of pytorch#205504: main's MultiProcContinuousTest already normalizes device_type across the forms instantiate_device_type_tests injects and calls backend_str(cls, device), so the bridging subclass is redundant. Test classes now inherit the upstream base directly and override backend_str in the standardized per-class form, resolving the deterministic backend with dist.get_default_backend_for_device. The zero-caller current_device property is dropped with the subclass, leaving torch/testing/_internal/common_distributed.py identical to main.
zhaiyaxing001 added a commit to zhaiyaxing001/pytorch that referenced this pull request Dec 3, 2026
…g torch.accelerator APIs

Refactor test/distributed/_composable/test_replicate_with_fsdp.py to be device-agnostic: inject the test device via instantiate_device_type_tests, declare HardwareClassification.ACCELERATOR, and resolve the communication backend per class with the standardized backend_str(cls, device) override calling dist.get_default_backend_for_device. Test classes inherit the upstream MultiProcContinuousTest directly (the pytorch#195000 bridging subclass was dropped per its closing rationale: main already normalizes the injected device_type forms), and the file is removed from the test-linter allowlist.
pytorchmergebot pushed a commit to zhaiyaxing001/pytorch that referenced this pull request Dec 3, 2026
…g torch.accelerator APIs

Refactor test/distributed/_composable/test_replicate_with_fsdp.py to be device-agnostic: inject the test device via instantiate_device_type_tests, declare HardwareClassification.ACCELERATOR, and resolve the communication backend per class with the standardized backend_str(cls, device) override calling dist.get_default_backend_for_device. Test classes inherit the upstream MultiProcContinuousTest directly (the pytorch#195000 bridging subclass was dropped per its closing rationale: main already normalizes the injected device_type forms), and the file is removed from the test-linter allowlist.
zhaiyaxing001 added a commit to zhaiyaxing001/pytorch that referenced this pull request Dec 3, 2026
…ic using torch.accelerator APIs

Refactor test/distributed/_composable/test_replicate.py to be device-agnostic: ReplicateTest and ReplicateTestNoXPU inherit the upstream MultiProcContinuousTest directly and resolve the communication backend with the standardized per-class backend_str(cls, device) override calling dist.get_default_backend_for_device (the MultiProcContinuousForInstantiateTest bridging subclass from pytorch#195000 is dropped per its closing rationale: main already normalizes the injected device_type forms), test methods receive and use the injected device, and the file is removed from the test-linter allowlist. torch/testing/_internal/common_distributed.py stays identical to main.
pytorchmergebot pushed a commit to zhaiyaxing001/pytorch that referenced this pull request Dec 3, 2026
…g torch.accelerator APIs

Refactor test/distributed/_composable/test_replicate_with_fsdp.py to be device-agnostic: inject the test device via instantiate_device_type_tests, declare HardwareClassification.ACCELERATOR, and resolve the communication backend per class with the standardized backend_str(cls, device) override calling dist.get_default_backend_for_device. Test classes inherit the upstream MultiProcContinuousTest directly (the pytorch#195000 bridging subclass was dropped per its closing rationale: main already normalizes the injected device_type forms), and the file is removed from the test-linter allowlist.
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. open source triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants