Skip to content

[Testcase Refactoring] Bind ShardedTensor test ranks for runtime accelerator backends - #190528

Closed
shichuanliujie9527 wants to merge 1 commit into
pytorch:mainfrom
shichuanliujie9527:test/sharded-tensor-pg-device-bind
Closed

shichuanliujie9527 wants to merge 1 commit into
pytorch:mainfrom
shichuanliujie9527:test/sharded-tensor-pg-device-bind

Conversation

@shichuanliujie9527

@shichuanliujie9527 shichuanliujie9527 commented Jul 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Makes ShardedTensorTestBase.init_pg bind the per-rank device based on the
accelerator actually present, instead of matching a hard-coded backend list.

Motivation

init_pg had two hard-coded lists that leave out-of-tree backends behind:

if backend not in ["nccl", "gloo", "mpi", "hccl", "xccl"]:
    raise RuntimeError(f"Backend {backend} not supported!")
...
# set device for nccl pg for collectives
if backend == "nccl" or backend == "xccl":
    torch.accelerator.set_device_index(self.rank)

The second one is the functional problem: every rank ends up on device 0 for any
backend outside that pair, so collectives fail with an unhelpful error. The
first rejects backends by name even when the distributed framework has them
registered and available.

Changes

  • The admission check asks the framework: dist.is_backend_available(backend).
    That covers the built-in backends via their is_*_available() functions and
    third-party ones through Backend.register_backend, so a device that
    registers its own backend is accepted the same way the built-ins are — and it
    additionally catches "requested but not compiled into this build", which the
    name-only list did not.
  • The device binding resolves the accelerator at runtime instead of matching a
    backend pair:
if torch.accelerator.is_available() and backend != dist.Backend.GLOO:
    torch.accelerator.set_device_index(self.rank)
  • with_comms keeps its original three-part admission check: an accelerator has
    to be present, the requested backend has to be that accelerator's default
    backend, and the device count has to cover world_size. Only the outer
    hard-coded nccl/xccl/hccl list is replaced by a Gloo exclusion, so an
    out-of-tree backend reaches the same check the built-in ones do. Every in-tree
    caller of this base class passes gloo, the default nccl, or
    get_default_backend_for_device(device_type), so their skip/run outcome is
    unchanged — including nccl requested on a non-CUDA accelerator host, which
    still skips.

    Because the wrapper already establishes that relationship before
    init_comms runs, init_pg does not repeat the default-backend comparison.

gloo is excluded deliberately: Backend.default_device_backend_map maps both
cpu and mps to gloo, so without that guard an MPS run would take the
binding path even though there is no per-rank accelerator index to set.

No special case is made for any individual device. HPU registers its backend
through the same third-party mechanism as any other out-of-tree device, so the
generic path covers it; singling it out would reintroduce exactly the kind of
hard-coding this change removes.

Test Plan

ruff check and ruff format --check pass.

Verified on an out-of-tree accelerator backend (PrivateUse1, Ascend NPU, 4
devices) through the two suites that use this base class:
test_checkpoint.py runs Ran 8 tests ... OK and
test_file_system_checkpoint.py runs Ran 9 tests ... OK. Before this change
every rank bound to device 0 and those suites failed during process group setup.

CPU (gloo) runs are unaffected: current_accelerator() returns None on a
CPU-only host, and where an accelerator is present but the process group is
gloo, the Backend.GLOO guard short-circuits before the default-backend lookup
and keeps the binding from firing.

Related

Part of #185590.

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

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

✅ No Failures

As of commit fa30eb3 with merge base b7930e4 (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 Jul 20, 2026
@albanD
albanD requested review from d4l3k and fffrog July 20, 2026 22:37
@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

@d4l3k d4l3k left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@d4l3k

d4l3k commented Jul 20, 2026

Copy link
Copy Markdown
Member

@pytorchbot merge

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

@shichuanliujie9527

Copy link
Copy Markdown
Contributor Author

Please hold the merge for a small follow-up. The generic backend check also changes the existing HPU/HCCL behavior, so I am adding an explicit HPU guard and will update this PR shortly.

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: GraphQL query
query ($owner: String!, $name: String!, $number: Int!, $cs_cursor: String, $cr_cursor: String!) {
repository(name: $name, owner: $owner) {
pullRequest(number: $number) {
commits(last: 1) {
nodes {
commit {
oid
checkSuites(first: 1, after: $cs_cursor) {
nodes {
checkRuns(first: 100, after: $cr_cursor) {
nodes {
name
conclusion
detailsUrl
databaseId
title
summary
}
pageInfo {
endCursor
hasNextPage
}
}
}
}
}
}
}
}
}
}
, args {'name': 'pytorch', 'owner': 'pytorch', 'number': 190528, 'cs_cursor': 'Y3Vyc29yOnYyOpHPAAAAEsfbqmk=', 'cr_cursor': 'Y3Vyc29yOnYyOpHPAAAAFJvJE_k='} failed: [{'message': 'Something went wrong while executing your query on 2026-07-21T03:31:54Z. Please include 3C11:35E307:F7EF60:34E822D:6A5EE825 when reporting this issue.'}]

Details for Dev Infra team Raised by workflow job

@pytorch-bot

pytorch-bot Bot commented Jul 21, 2026 •

Copy link
Copy Markdown

Workflows were awaiting approval. CI has now been triggered for the ciflow labels on this PR.

@shichuanliujie9527

Copy link
Copy Markdown
Contributor Author

@d4l3k The HPU compatibility follow-up has been pushed in 8e7ca21, and the PR description has been updated accordingly. Could you please approve the pending workflows when convenient? Thanks!

@shichuanliujie9527

Copy link
Copy Markdown
Contributor Author

@fffrog Could you please review the latest follow-up when convenient? It preserves the existing HPU/HCCL behavior. The ciflow/trunk workflow is also awaiting maintainer approval. Thanks!

@shichuanliujie9527

Copy link
Copy Markdown
Contributor Author

@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

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: 1 mandatory check(s) failed. The first few are:

Dig deeper by viewing the failures on hud

Details for Dev Infra team Raised by workflow job

Failing merge rule: Core Maintainers

@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 Jul 25, 2026
@shichuanliujie9527

Copy link
Copy Markdown
Contributor Author

@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

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: 3 mandatory check(s) failed. The first few are:

Dig deeper by viewing the failures on hud

Details for Dev Infra team Raised by workflow job

Failing merge rule: Core Maintainers

youlituao added a commit to youlituao/pytorch that referenced this pull request Aug 24, 2026
…on label

Summary:
- Refactor to instantiate_device_type_tests + spawn combination with framework-injected device parameter
- Replace implicit CUDA device-index moves (.to(rank)) with explicit torch.device(device_type, rank); bare device type derived from injected device via torch.device(device).type
- Switch backend admission to @requires_capabilities(Capability.distributed.backend)
- Add hw_classification = HardwareClassification.ACCELERATOR

Depends on: pytorch#187650, pytorch#190528, pytorch#191919, pytorch#193080

Test Plan:
- NPU (PrivateUse1, Ascend 910B3, 4 devices): 2 tests x 4 procs pass (Ran 2 tests - OK), both with PR-CI env vars and full run
- CPU: no variant generated (except_for="cpu"), NO TESTS RAN (expected, same as only_for precedent)
- CUDA: TODO - A100 regression run to be confirmed in PR thread
kerer-ai pushed a commit to kerer-ai/pytorch-nv that referenced this pull request Aug 26, 2026
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 27, 2026
…tic backend resolution

Add a  property to  that resolves the
default distributed backend from the test variant's  using
. This allows  and
 to dynamically resolve the correct backend per device variant
(CUDA/nccl, NPU/hccl, XPU/xccl, etc.) instead of hardcoding 'nccl'.

Changes:
- Add  property
- Change , ,  default from 'nccl' to None
- When backend=None, resolve from  at runtime
- with_comms admission check uses 'gloo' check instead of hardcoded list

Depends on pytorch#187650 (skip_if_lt_x_gpu hardware-agnostic) and pytorch#190528
(init_pg dynamic device setup).
youlituao added a commit to youlituao/pytorch that referenced this pull request Aug 27, 2026
…tic backend resolution

Add a  property to  that resolves the
default distributed backend from the test variant's  using
. This allows  and
 to dynamically resolve the correct backend per device variant
(CUDA/nccl, NPU/hccl, XPU/xccl, etc.) instead of hardcoding 'nccl'.

Changes:
- Add  property
- Change , ,  default from 'nccl' to None
- When backend=None, resolve from  at runtime
- with_comms admission check uses 'gloo' check instead of hardcoded list

Depends on pytorch#187650 (skip_if_lt_x_gpu hardware-agnostic) and pytorch#190528
(init_pg dynamic device setup).
youlituao added a commit to youlituao/pytorch that referenced this pull request Aug 27, 2026
…tic backend resolution

Add a  property to  that resolves the
default distributed backend from the test variant's  using
. This allows  and
 to dynamically resolve the correct backend per device variant
(CUDA/nccl, NPU/hccl, XPU/xccl, etc.) instead of hardcoding 'nccl'.

Changes:
- Add  property
- Change , ,  default from 'nccl' to None
- When backend=None, resolve from  at runtime
- with_comms admission check uses 'gloo' check instead of hardcoded list

Depends on pytorch#187650 (skip_if_lt_x_gpu hardware-agnostic) and pytorch#190528
(init_pg dynamic device setup).
youlituao added a commit to youlituao/pytorch that referenced this pull request Aug 27, 2026
…tic backend resolution

Add a  property to  that resolves the
default distributed backend from the test variant's  using
. This allows  and
 to dynamically resolve the correct backend per device variant
(CUDA/nccl, NPU/hccl, XPU/xccl, etc.) instead of hardcoding 'nccl'.

Changes:
- Add  property
- Change , ,  default from 'nccl' to None
- When backend=None, resolve from  at runtime
- with_comms admission check uses 'gloo' check instead of hardcoded list

Depends on pytorch#187650 (skip_if_lt_x_gpu hardware-agnostic) and pytorch#190528
(init_pg dynamic device setup).
youlituao added a commit to youlituao/pytorch that referenced this pull request Aug 27, 2026
…tic backend resolution

Add a  property to  that resolves the
default distributed backend from the test variant's  using
. This allows  and
 to dynamically resolve the correct backend per device variant
(CUDA/nccl, NPU/hccl, XPU/xccl, etc.) instead of hardcoding 'nccl'.

Changes:
- Add  property
- Change , ,  default from 'nccl' to None
- When backend=None, resolve from  at runtime
- with_comms admission check uses 'gloo' check instead of hardcoded list

Depends on pytorch#187650 (skip_if_lt_x_gpu hardware-agnostic) and pytorch#190528
(init_pg dynamic device setup).

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

SGMT, thank you.

@fffrog fffrog moved this from Ready for Review(Initial) to Ready for Review(Definitive) in PyTorch Test Refactoring Aug 27, 2026
youlituao added a commit to youlituao/pytorch that referenced this pull request Aug 28, 2026
…on label

Summary:
- Refactor to instantiate_device_type_tests + spawn combination with framework-injected device parameter
- Replace implicit CUDA device-index moves (.to(rank)) with explicit torch.device(device_type, rank); bare device type derived from injected device via torch.device(device).type
- Switch backend admission to @requires_capabilities(Capability.distributed.backend)
- Add hw_classification = HardwareClassification.ACCELERATOR

Depends on: pytorch#187650, pytorch#190528, pytorch#191919, pytorch#193080

Test Plan:
- NPU (PrivateUse1, Ascend 910B3, 4 devices): 2 tests x 4 procs pass (Ran 2 tests - OK), both with PR-CI env vars and full run
- CPU: no variant generated (except_for="cpu"), NO TESTS RAN (expected, same as only_for precedent)
- CUDA: TODO - A100 regression run to be confirmed in PR thread
@shichuanliujie9527

Copy link
Copy Markdown
Contributor Author

@d4l3k Both module reviews are approved on the current head, all 97 reported
check contexts are successful, and there are no active review threads. Could
you help land this PR when convenient?

youlituao added a commit to youlituao/pytorch that referenced this pull request Aug 29, 2026
…on label

Summary:
- Refactor to instantiate_device_type_tests + spawn combination with framework-injected device parameter
- Replace implicit CUDA device-index moves (.to(rank)) with explicit torch.device(device_type, rank); bare device type derived from injected device via torch.device(device).type
- Switch backend admission to @requires_capabilities(Capability.distributed.backend)
- Add hw_classification = HardwareClassification.ACCELERATOR

Depends on: pytorch#187650, pytorch#190528, pytorch#191919, pytorch#193080

Test Plan:
- NPU (PrivateUse1, Ascend 910B3, 4 devices): 2 tests x 4 procs pass (Ran 2 tests - OK), both with PR-CI env vars and full run
- CPU: no variant generated (except_for="cpu"), NO TESTS RAN (expected, same as only_for precedent)
- CUDA: TODO - A100 regression run to be confirmed in PR thread
youlituao added a commit to youlituao/pytorch that referenced this pull request Aug 31, 2026
…on label

Summary:
- Refactor to instantiate_device_type_tests + spawn combination with framework-injected device parameter
- Replace implicit CUDA device-index moves (.to(rank)) with explicit torch.device(device_type, rank); bare device type derived from injected device via torch.device(device).type
- Switch backend admission to @requires_capabilities(Capability.distributed.backend)
- Add hw_classification = HardwareClassification.ACCELERATOR

Depends on: pytorch#187650, pytorch#190528, pytorch#191919, pytorch#193080

Test Plan:
- NPU (PrivateUse1, Ascend 910B3, 4 devices): 2 tests x 4 procs pass (Ran 2 tests - OK), both with PR-CI env vars and full run
- CPU: no variant generated (except_for="cpu"), NO TESTS RAN (expected, same as only_for precedent)
- CUDA: TODO - A100 regression run to be confirmed in PR thread
youlituao added a commit to youlituao/pytorch that referenced this pull request Aug 31, 2026
…on label

Summary:
- Refactor to instantiate_device_type_tests + spawn combination with framework-injected device parameter
- Replace implicit CUDA device-index moves (.to(rank)) with explicit torch.device(device_type, rank); bare device type derived from injected device via torch.device(device).type
- Switch backend admission to @requires_capabilities(Capability.distributed.backend)
- Add hw_classification = HardwareClassification.ACCELERATOR

Depends on: pytorch#187650, pytorch#190528, pytorch#191919, pytorch#193080

Test Plan:
- NPU (PrivateUse1, Ascend 910B3, 4 devices): 2 tests x 4 procs pass (Ran 2 tests - OK), both with PR-CI env vars and full run
- CPU: no variant generated (except_for="cpu"), NO TESTS RAN (expected, same as only_for precedent)
- CUDA: TODO - A100 regression run to be confirmed in PR thread
youlituao added a commit to youlituao/pytorch that referenced this pull request Aug 31, 2026
…on label

Summary:
- Refactor to instantiate_device_type_tests + spawn combination with framework-injected device parameter
- Replace implicit CUDA device-index moves (.to(rank)) with explicit torch.device(device_type, rank); bare device type derived from injected device via torch.device(device).type
- Switch backend admission to @requires_capabilities(Capability.distributed.backend)
- Add hw_classification = HardwareClassification.ACCELERATOR

Depends on: pytorch#187650, pytorch#190528, pytorch#191919, pytorch#193080

Test Plan:
- NPU (PrivateUse1, Ascend 910B3, 4 devices): 2 tests x 4 procs pass (Ran 2 tests - OK), both with PR-CI env vars and full run
- CPU: no variant generated (except_for="cpu"), NO TESTS RAN (expected, same as only_for precedent)
- CUDA: TODO - A100 regression run to be confirmed in PR thread
@shichuanliujie9527

Copy link
Copy Markdown
Contributor Author

@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

@github-project-automation github-project-automation Bot moved this from Ready for Review(Definitive) to Done in PyTorch Test Refactoring Sep 2, 2026
Sanskaaar1 pushed a commit to Sanskaaar1/pytorch that referenced this pull request Sep 8, 2026
…lerator backends (pytorch#190528)

## Summary

Makes `ShardedTensorTestBase.init_pg` bind the per-rank device based on the
accelerator actually present, instead of matching a hard-coded backend list.

## Motivation

`init_pg` had two hard-coded lists that leave out-of-tree backends behind:

```python
if backend not in ["nccl", "gloo", "mpi", "hccl", "xccl"]:
    raise RuntimeError(f"Backend {backend} not supported!")
...
# set device for nccl pg for collectives
if backend == "nccl" or backend == "xccl":
    torch.accelerator.set_device_index(self.rank)
```

The second one is the functional problem: every rank ends up on device 0 for any
backend outside that pair, so collectives fail with an unhelpful error. The
first rejects backends by name even when the distributed framework has them
registered and available.

## Changes

- The admission check asks the framework: `dist.is_backend_available(backend)`.
  That covers the built-in backends via their `is_*_available()` functions and
  third-party ones through `Backend.register_backend`, so a device that
  registers its own backend is accepted the same way the built-ins are — and it
  additionally catches "requested but not compiled into this build", which the
  name-only list did not.
- The device binding resolves the accelerator at runtime instead of matching a
  backend pair:

```python
if torch.accelerator.is_available() and backend != dist.Backend.GLOO:
    torch.accelerator.set_device_index(self.rank)
```

- `with_comms` keeps its original three-part admission check: an accelerator has
  to be present, the requested backend has to be that accelerator's default
  backend, and the device count has to cover `world_size`. Only the outer
  hard-coded `nccl`/`xccl`/`hccl` list is replaced by a Gloo exclusion, so an
  out-of-tree backend reaches the same check the built-in ones do. Every in-tree
  caller of this base class passes `gloo`, the default `nccl`, or
  `get_default_backend_for_device(device_type)`, so their skip/run outcome is
  unchanged — including `nccl` requested on a non-CUDA accelerator host, which
  still skips.

  Because the wrapper already establishes that relationship before
  `init_comms` runs, `init_pg` does not repeat the default-backend comparison.

`gloo` is excluded deliberately: `Backend.default_device_backend_map` maps both
`cpu` and `mps` to `gloo`, so without that guard an MPS run would take the
binding path even though there is no per-rank accelerator index to set.

No special case is made for any individual device. HPU registers its backend
through the same third-party mechanism as any other out-of-tree device, so the
generic path covers it; singling it out would reintroduce exactly the kind of
hard-coding this change removes.

## Test Plan

`ruff check` and `ruff format --check` pass.

Verified on an out-of-tree accelerator backend (PrivateUse1, Ascend NPU, 4
devices) through the two suites that use this base class:
`test_checkpoint.py` runs `Ran 8 tests ... OK` and
`test_file_system_checkpoint.py` runs `Ran 9 tests ... OK`. Before this change
every rank bound to device 0 and those suites failed during process group setup.

CPU (gloo) runs are unaffected: `current_accelerator()` returns `None` on a
CPU-only host, and where an accelerator is present but the process group is
gloo, the `Backend.GLOO` guard short-circuits before the default-backend lookup
and keeps the binding from firing.

## Related

Part of pytorch#185590.

Pull Request resolved: pytorch#190528
Approved by: https://github.com/d4l3k, https://github.com/pjyFight, https://github.com/fffrog
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 module: tests/distributed This label covers all test cases within the Distributed sheet. open source release notes: distributed (sharded) release notes 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.

8 participants