Skip to content

[Testcase Refactoring] Make ComposabilityTest backend resolution hardware-agnostic in… - #190518

Open
zjq-86 wants to merge 6 commits into
pytorch:mainfrom
zjq-86:test/composability_decoupling
Open

zjq-86 wants to merge 6 commits into
pytorch:mainfrom
zjq-86:test/composability_decoupling

Conversation

@zjq-86

@zjq-86 zjq-86 commented Jul 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR migrates ComposabilityTest in test/distributed/test_composability.py to device-type instantiation per test/REVIEW.md (#202723), making the test hardware-agnostic, and removes the file from tools/linter/adapters/test_linter_allowlist.json.

Changes

Updated ComposabilityTest in test/distributed/test_composability.py:

  • Added hw_classification = HardwareClassification.ACCELERATOR and switched instantiation from instantiate_parametrized_tests to instantiate_device_type_tests(..., except_for="cpu", allow_xpu=True), so every test runs once per accelerator device instead of assuming CUDA.
  • Every test method now takes the injected device parameter; tensors, pipeline stages, and meshes are built on it. The module-level device_type/backend globals and the device property are removed.
  • backend_str resolves the process-group backend dynamically via torch.distributed.get_default_backend_for_device() (nccl on CUDA, hccl on NPU, xccl on XPU, ...) instead of hard-coding "nccl". A small compatibility shim keeps the override callable under framework revisions that invoke backend_str() without a device argument.
  • test_pp_fsdp_deferred_reduction_rejects_shared_comm_ctx drops its @requires_nccl() gate: share_comm_ctx only rebinds FSDPModule comm contexts (no NCCL coupling) and the "Do not call share_comm_ctx" validation is generic pipeline-schedule logic, so the test belongs in the ACCELERATOR class and runs on every accelerator with >= 2 devices.
  • Added a setUpClass probe that skips the device variant cleanly (instead of erroring at process-group setup) when the accelerator has no registered distributed backend: get_default_backend_for_device raises inside _ensure_processes_spawned() before any per-test skip can run; the probe converts that into a class-level unittest.SkipTest. Registered backends keep their current path.
  • Removed test/distributed/test_composability.py from tools/linter/adapters/test_linter_allowlist.json.

Motivation

ComposabilityTest hard-coded "nccl" as the distributed backend and used CUDA-specific gating, so on accelerators whose default backend is not NCCL (e.g., NPU uses HCCL, XPU uses XCCL) the tests were skipped or failed at process-group initialization. Following test/REVIEW.md rules 1/2/4/6/7, the tests now adapt to any accelerator registered with torch.accelerator, including PrivateUse1-based out-of-tree backends such as NPU.

Test Plan

Verified on CPU and across multiple accelerator types:

python test/distributed/test_composability.py -v

Notes

@pytorch-bot

pytorch-bot Bot commented Jul 20, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

⚠️ 4 Awaiting Approval

As of commit 637b549 with merge base 5cda694 (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.

@pytorch-bot pytorch-bot Bot added the topic: not user facing topic category label Jul 20, 2026
@albanD
albanD requested a review from fffrog July 20, 2026 22:36
@albanD albanD added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Jul 20, 2026
@zjq-86

zjq-86 commented Jul 25, 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 Jul 25, 2026
@fffrog fffrog moved this to Developing in PyTorch Test Refactoring Jul 27, 2026
@zjq-86 zjq-86 changed the title [Test] Make ComposabilityTest backend resolution hardware-agnostic in… [Testcase Refactoring] Make ComposabilityTest backend resolution hardware-agnostic in… Jul 27, 2026
@zjq-86
zjq-86 force-pushed the test/composability_decoupling branch 3 times, most recently from a928044 to 39cba16 Compare August 6, 2026 06:58

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

requires_accelerator_dist_backend suggest modifying it to use the capability registration mechanism to determine whether the backend is available. #191919

@zjq-86
zjq-86 force-pushed the test/composability_decoupling branch from 39cba16 to ea2e307 Compare August 13, 2026 07:55
@zjq-86

zjq-86 commented Aug 13, 2026

Copy link
Copy Markdown
Contributor Author

requires_accelerator_dist_backend suggest modifying it to use the capability registration mechanism to determine whether the backend is available. #191919

The changes have been made in accordance with the suggestions.

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

Thanks for the update — the earlier review comments have been properly addressed: requires_accelerator_dist_backend() has been removed and replaced with @requires_capabilities(Capability.distributed.backend) from #191919, applied to all three test methods.
hw_classification = HardwareClassification.ACCELERATOR together with instantiate_device_type_tests using except_for="cpu" is the correct classification: the tests now accept a device parameter, are gated via the framework's capability‑registration mechanism, and dynamically resolve the backend through torch.device(device).type, instead of hard‑coding "nccl"/"cuda".

kerer-ai pushed a commit to kerer-ai/pytorch-nv that referenced this pull request Aug 20, 2026
Comment thread test/distributed/test_composability.py Outdated

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.

When we remove def device here, can we call self.device directly here?

Comment on lines -99 to -106
@classmethod
def backend_str(cls) -> str:
# Testing with NCCL backend
return "nccl"

@property
def device(self) -> torch.device:
return torch.device(device_type, self.rank)

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.

Please inherit MultiProcContinuousTest to create a new base class named ComposabilityBaseTest, and add new function into this new class:

  • def backend_str(device) -> return the backend
  • def current_device -> return the correct device with the current rank

You can refer to C10dTorchCommsTestBase for reference

Then ComposabilityTest should inhert ComposabilityBaseTest.

That said, I think adding those two functions into MultiProcContinuousTest should be better way, but we need to evaluate the effect radius.

  • all the self.device -> self.current_device

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.

The current change adds a new base class that overrides the backend_str class method and implements the device property. Tests pass.

An alternative approach would be to modify backend_str and add the device method directly in MultiProcContinuousTest . Tests also pass with this approach, but since many test classes inherit from MultiProcContinuousTest , the blast radius would be considerably larger.

Comment thread test/distributed/test_composability.py Outdated
skip_if_lt_x_gpu,
)
from torch.testing._internal.common_device_type import (
Capability,

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.

Do NOT use Capability here, please.

Comment thread test/distributed/test_composability.py Outdated
@@ -195,9 +189,9 @@ def _build_pp_schedule(
)
return pipeline_schedule, partial_models, offsets

@requires_nccl()
@requires_capabilities(Capability.distributed.backend)

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.

We can try to refactor this requires_accelerator_dist_backend to support new accelerator.

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.

I’d skip a generated device variant when its device type has no registered distributed backend.

AI-assisted verification at 457a4cef1f. The test now imports with the existing MultiProcContinuousTest and resolves XPU to XCCL. With a controlled four-device PrivateUse1 module and no default process-group backend registered for it, the test file generated 22 variants. Calling the generated class’s _ensure_processes_spawned() raised ValueError: Default backend not registered for device : privateuseone before any worker was spawned. The exception comes from backend_str(device_type), before the fixture can check backend availability. Could that missing mapping produce a class skip? A registered backend should keep its current path.

@fffrog
fffrog requested a review from JamesD18 August 21, 2026 07:29
@zjq-86
zjq-86 force-pushed the test/composability_decoupling branch from ea2e307 to 1ad6226 Compare August 25, 2026 09:15
kerer-ai pushed a commit to kerer-ai/pytorch-nv that referenced this pull request Aug 26, 2026
youlituao added a commit to youlituao/pytorch that referenced this pull request Aug 26, 2026
…ynamic backend resolution

Add a new CommonDistributedBackend(MultiProcessTestCase) intermediate
class that provides dynamic backend resolution via
dist.get_default_backend_for_device(self.device_type), following the
same pattern as DTensorTestBase (PR pytorch#191288).

Change with_comms default from backend='nccl' to backend=None, which
resolves to self.backend at runtime. ShardedTensorTestBase is preserved
unchanged — adding backend/device methods to existing intermediate
classes triggers TypeError due to instantiate_device_type_tests
conflicts.

Test classes can use multiple inheritance to combine
CommonDistributedBackend with existing intermediate classes:
  class TestFoo(CommonDistributedBackend, ShardedTensorTestBase):
      @with_comms(init_rpc=False)  # backend resolved dynamically
      def test_foo(self, device):
          ...

Reference: DTensorTestBase (PR pytorch#191288), PR pytorch#190518
youlituao added a commit to youlituao/pytorch that referenced this pull request Aug 27, 2026
…ynamic backend resolution

Add a new CommonDistributedBackend(MultiProcessTestCase) intermediate
class that provides dynamic backend resolution via
dist.get_default_backend_for_device(self.device_type), following the
same pattern as DTensorTestBase (PR pytorch#191288).

Change with_comms default from backend='nccl' to backend=None, which
resolves to self.backend at runtime. ShardedTensorTestBase is preserved
unchanged — adding backend/device methods to existing intermediate
classes triggers TypeError due to instantiate_device_type_tests
conflicts.

Test classes can use multiple inheritance to combine
CommonDistributedBackend with existing intermediate classes:
  class TestFoo(CommonDistributedBackend, ShardedTensorTestBase):
      @with_comms(init_rpc=False)  # backend resolved dynamically
      def test_foo(self, device):
          ...

Reference: DTensorTestBase (PR pytorch#191288), PR pytorch#190518
youlituao added a commit to youlituao/pytorch that referenced this pull request Aug 27, 2026
…ynamic backend resolution

Add a new CommonDistributedBackend(MultiProcessTestCase) intermediate
class that provides dynamic backend resolution via
dist.get_default_backend_for_device(self.device_type), following the
same pattern as DTensorTestBase (PR pytorch#191288).

Change with_comms default from backend='nccl' to backend=None, which
resolves to self.backend at runtime. ShardedTensorTestBase is preserved
unchanged — adding backend/device methods to existing intermediate
classes triggers TypeError due to instantiate_device_type_tests
conflicts.

Test classes can use multiple inheritance to combine
CommonDistributedBackend with existing intermediate classes:
  class TestFoo(CommonDistributedBackend, ShardedTensorTestBase):
      @with_comms(init_rpc=False)  # backend resolved dynamically
      def test_foo(self, device):
          ...

Reference: DTensorTestBase (PR pytorch#191288), PR pytorch#190518
@zjq-86
zjq-86 force-pushed the test/composability_decoupling branch from 1ad6226 to 4c0f36d Compare August 27, 2026 09:10

@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 revised per fffrog’s feedback.

@zjq-86
zjq-86 force-pushed the test/composability_decoupling branch from 4c0f36d to 78e1f6e Compare September 3, 2026 06:55
kerer-ai pushed a commit to kerer-ai/pytorch-nv that referenced this pull request Sep 7, 2026
@can-gaa-hou

Copy link
Copy Markdown
Collaborator

Please resolve the conflicts @zjq-86, thanks

@zjq-86
zjq-86 force-pushed the test/composability_decoupling branch from 78e1f6e to 0b3b122 Compare December 2, 2026 14:42
@zjq-86
zjq-86 requested review from atalman and huydhn as code owners December 3, 2026 01:20
@zjq-86
zjq-86 force-pushed the test/composability_decoupling branch from 4a48630 to 537ac85 Compare December 3, 2026 01:42
@zjq-86
zjq-86 force-pushed the test/composability_decoupling branch from 457a4ce to aa49cef Compare December 3, 2026 06:44

instantiate_parametrized_tests(ComposabilityTest)
instantiate_device_type_tests(
ComposabilityTest, globals(), except_for="cpu", allow_xpu=True

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.

Use "[cpu]" instead please, and I think It's better to have each parameter on its own line.

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. 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: Developing

Development

Successfully merging this pull request may close these issues.

8 participants