Skip to content

[Testcase Refactoring] Resolve default backend from device in common test base classes - #194992

Closed
shichuanliujie9527 wants to merge 7 commits into
pytorch:mainfrom
shichuanliujie9527:test/sharded-tensor-base-backend
Closed

shichuanliujie9527 wants to merge 7 commits into
pytorch:mainfrom
shichuanliujie9527:test/sharded-tensor-base-backend

Conversation

@shichuanliujie9527

@shichuanliujie9527 shichuanliujie9527 commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

Device-instantiated subclasses of ShardedTensorTestBase already receive
device_type, but a bare @with_comms still selects the fixed "nccl" default.
Use the device's registered default backend for these subclasses while retaining
the existing decorator argument for callers that have not yet adopted
instantiate_device_type_tests.

Changes

Only torch/testing/_internal/distributed/_shard/sharded_tensor/__init__.py
changes:

  • Add backend(self, device), using the existing dist import to call
    dist.get_default_backend_for_device(device).
  • In with_comms, resolve the backend from self.device_type when it is
    present and non-None; otherwise use the decorator's backend argument.
  • Store the result in backend_name, avoiding shadowing the outer decorator
    argument, and use it for both the device-count check and init_comms.
  • When an accelerator is available and the selected backend is not Gloo,
    skip if there are fewer devices than world_size. Remove the
    current_accelerator() probe and the comparison against its default backend.

The "nccl" defaults of with_comms, init_comms, and init_pg remain.
Existing backend availability checks, rank binding, RPC initialization, and
cleanup methods are unchanged. There are no changes to common_distributed.py.

Behavior

Caller Backend selection
Device-instantiated class Registered default for self.device_type; this takes precedence over the decorator argument
Legacy class without device_type Decorator argument, defaulting to "nccl"
Direct init_pg / init_comms call Explicit argument, or the unchanged "nccl" default

Validation

  • Patch replay, Python syntax, PYFMT, and Ruff checks passed.
  • Isolated wrapper checks covered legacy defaults, explicit Gloo, CPU/NPU
    backend selection, reuse of the same closure, RPC flag forwarding, and
    the insufficient-device skip.
  • Ran the existing DCP caching test from [Testcase Refactoring] Select checkpoint file-system test backends by device #192324 on two NPU devices with HCCL:
    TestDistributedStateDictSaveLoadWithCachingNPU.test_read_write_shard_tensor_npu
    in test/distributed/checkpoint/test_file_system_checkpoint.py.
Compared version Selected Passed Failed / errors / skipped Exit code
Implementation before the latest import cleanup 1 1 0 / 0 / 0 0
Previous PR implementation (control run) 1 1 0 / 0 / 0 0

The unmodified business test completed two save/load rounds, data equality
checks, and process-group destruction. Parent and worker imports were verified.
The run reused an existing PyTorch/torch_npu binary environment with isolated
test-helper sources and device-test capability support from #191919 and #192043.

The final review cleanup reuses dist.get_default_backend_for_device instead
of importing distributed_c10d separately; both names refer to the same function.
That two-line cleanup passed patch replay, syntax, PYFMT, and Ruff checks.
The device test was run before this import-only cleanup.

@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/194992

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

✅ No Failures

As of commit cf5505b with merge base ed1fcb4 (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 the release notes: distributed (sharded) release notes category label Aug 27, 2026
@shichuanliujie9527

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
@shichuanliujie9527
shichuanliujie9527 force-pushed the test/sharded-tensor-base-backend branch 3 times, most recently from 2c0d53c to 37b8902 Compare August 27, 2026 07:53
@shichuanliujie9527 shichuanliujie9527 changed the title [Testcase Refactoring] Resolve default backend from device in ShardedTensorTestBase [Testcase Refactoring] Resolve default backend from device in common test base classes Aug 27, 2026
@shichuanliujie9527
shichuanliujie9527 force-pushed the test/sharded-tensor-base-backend branch 2 times, most recently from 48adf7c to 5634c8c Compare August 27, 2026 09:02
@shichuanliujie9527
shichuanliujie9527 force-pushed the test/sharded-tensor-base-backend branch from 5634c8c to 1a3273e Compare August 27, 2026 10:44
@shichuanliujie9527
shichuanliujie9527 force-pushed the test/sharded-tensor-base-backend branch 5 times, most recently from 236d2cd to 444a80d Compare August 27, 2026 13:19
@fffrog fffrog moved this to Backlog in PyTorch Test Refactoring Aug 27, 2026
JamesD18
JamesD18 previously approved these changes Aug 28, 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.DistributedTestBase ( DynamoDistributedMultiProcTestCase )are referenced across 12 files, and ShardedTensorTestBase spans 15 files. All of these changes are within the scope of the test‑decoupling work.

@shichuanliujie9527
shichuanliujie9527 force-pushed the test/sharded-tensor-base-backend branch from 444a80d to f2386de Compare August 28, 2026 06:48
@shichuanliujie9527

Copy link
Copy Markdown
Contributor Author

The focused CPU run exposed one issue in the previous head: for a bare
@with_comms, the wrapper checked backend=None before init_pg could resolve
the registered CPU default, so the valid gloo path was skipped as if it needed
an accelerator.

The current head changes only that precheck to
(backend or self.backend) != "gloo"; it does not reintroduce a local
resolved_backend, a missing-device_type fallback, or a reverse device
lookup.

Targeted validation on the current implementation:

  • NPU/HCCL, 4 ranks: 6 passed, 0 skipped, 105.943 s;
  • CPU/gloo: 6 passed, 0 skipped, 27.563 s;
  • both runs restored the prepared source tree byte-for-byte.

@JamesD18, could you please re-review this one-line follow-up on the current
head? The four GitHub workflows are also still marked action_required; a
maintainer with write access will need to approve them before CI can start.

youlituao added a commit to youlituao/pytorch that referenced this pull request Aug 28, 2026
…ckend

- Remove module-level device_type and backend variables; use self.backend
  property from ShardedTensorTestBase (PR pytorch#194992) via @with_comms()
  default backend=None
- Revert init_rpc=False (unrelated to device-generic refactor)
- Add allow_xpu=True to instantiate_device_type_tests
- Change except_for=['cpu'] to except_for='cpu'
- Pass device parameter through get_device_specs and _test_common_failures
  using torch.device(device).type for per-variant device_type
@atalman

atalman commented Oct 21, 2026

Copy link
Copy Markdown
Collaborator

@pytorchmergebot revert -c ghfirst -m "breaks internal build"

@atalman

atalman commented Oct 21, 2026 •

Copy link
Copy Markdown
Collaborator

Issue reproduces in OSS:

  • Location: torch/testing/_internal/distributed/_shard/sharded_tensor/init.py, in the with_comms wrapper, in the skip guard just above self.init_comms(init_rpc=init_rpc, backend=backend_name). This is the only site.
  • Repro: run test/distributed/_shard/sharded_tensor/test_sharded_tensor.py (for example TestShardMetadata) on a host with no accelerator (torch.accelerator.is_available() is False). Make sure the CPU TestShardMetadata tests, which use @with_comms(init_rpc=True) and the default "nccl" backend, are selected.
  • Why OSS CI missed it: the accelerator-less path only runs on CPU-only hosts. OSS GPU runners skip or pass because an accelerator is present. Gloo-only or CPU runners may never select these nccl-default tests.
  • Suggested fix: keep skipping non-Gloo backends when no accelerator is available. For example, skip if not torch.accelerator.is_available() or the device count is below world_size, rather than only when an accelerator exists.

Diagnostic (sanitised):

ValueError: Node worker0 has source devices with invalid indices in its device map for worker3
device map = {device(type='cuda', index=0): device(type='cuda', index=3), device(type='cuda', index=3): device(type='cuda', index=0)}
device count = 0

Raised at torch/distributed/rpc/backend_registry.py in _validate_device_maps, reached from rpc.init_rpc in init_rpc, from init_comms, from with_comms.wrapper in torch/testing/_internal/distributed/_shard/sharded_tensor/init.py. Worker processes exit with code 10.

@atalman

atalman commented Oct 21, 2026

Copy link
Copy Markdown
Collaborator

@pytorchbot revert -c ghfirst -m "breaks internal build"

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

@pytorchbot successfully started a revert job. Check the current status here.
Questions? Feedback? Please reach out to the PyTorch DevX Team

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

@shichuanliujie9527 your PR has been successfully reverted.

pytorchmergebot added a commit that referenced this pull request Oct 21, 2026
@pytorchmergebot pytorchmergebot added Reverted ci-no-td Do not run TD on this PR labels Oct 21, 2026
@pytorch-bot
pytorch-bot Bot dismissed stale reviews from JamesD18, JamesD18, JamesD18, JamesD18, fffrog, and fffrog October 21, 2026 16:21

This PR was reopened (likely due to being reverted), so your approval was removed. Please request another review.

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

@pytorchbot successfully started a revert job. Check the current status here.
Questions? Feedback? Please reach out to the PyTorch DevX Team

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Reverting PR 194992 failed

Reason: Command git -C /home/runner/work/pytorch/pytorch revert --no-edit 8bf87dac85d367ee0cd46a7cde0da66dc16b5b6e returned non-zero exit code 1

On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean
Details for Dev Infra team

Raised by workflow job

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

Hi @shichuanliujie9527, this PR have been reverted, please update this PR, thank you

@shichuanliujie9527
shichuanliujie9527 force-pushed the test/sharded-tensor-base-backend branch from d5b9ec6 to cf5505b Compare October 22, 2026 12:36

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

SGTM

@fffrog

fffrog commented Oct 23, 2026

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

youlituao added a commit to youlituao/pytorch that referenced this pull request Oct 26, 2026
- Remove module-level device_type/backend globals; backend resolved via
  ShardedTensorTestBase.backend (PR pytorch#194992) through @with_comms()
  default backend=None
- Reorder decorators: @requires_capabilities before @with_comms so
  capability is checked before communicator initialization
- Pass device parameter to get_device_specs/_test_common_failures,
  derive device_type via torch.device(device).type
- Add allow_xpu=True and except_for='cpu' to instantiate_device_type_tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci-no-td Do not run TD on this PR ciflow/trunk Trigger trunk jobs on your pull request Merged module: tests/distributed This label covers all test cases within the Distributed sheet. module: tests/simple This label covers all the testcase that are very straightforward open source release notes: distributed (sharded) release notes category Reverted 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.

9 participants