Repository navigation
[Testcase Refactoring] Make with_comms backend dynamically resolved for out-of-tree accelerators - #192417
[Testcase Refactoring] Make with_comms backend dynamically resolved for out-of-tree accelerators#192417youlituao wants to merge 2 commits into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/192417
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. |
|
@pytorchbot label "module: tests/distributed" |
a293a4a to
374b0c4
Compare
| else: | ||
| resolved_backend = backend | ||
|
|
||
| if resolved_backend in ["nccl", "xccl", "hccl"]: |
There was a problem hiding this comment.
The change to the default backend to fix out-of-tree accelerators is reasonable, but it still retains a hard-coded check(backend in ["nccl", "xccl", "hccl"]). It is recommended to consider using c10d.is_backend_available() instead.
There was a problem hiding this comment.
Thanks for the feedback! You're right — the hard-coded ["nccl", "xccl", "hccl"] list is not ideal since it requires updating whenever a new accelerator backend is added.
I've updated the PR to replace the hard-coded list with dist.is_backend_available():
Before:
if resolved_backend in ["nccl", "xccl", "hccl"]:
if (
acc is None
or resolved_backend != dist.get_default_backend_for_device(acc)
or torch.accelerator.device_count() < self.world_size
):
sys.exit(TEST_SKIPS[f"multi-gpu-{self.world_size}"].exit_code)
After:
if resolved_backend != "gloo":
if not dist.is_backend_available(resolved_backend):
sys.exit(TEST_SKIPS["backend_unavailable"].exit_code)
acc = torch.accelerator.current_accelerator()
if (
acc is None
or resolved_backend != dist.get_default_backend_for_device(acc)
or torch.accelerator.device_count() < self.world_size
):
sys.exit(TEST_SKIPS[f"multi-gpu-{self.world_size}"].exit_code)
This way, new accelerator backends are automatically supported without modifying the check. The resolved_backend != "gloo" condition preserves the original intent: gloo (CPU backend) does not require an accelerator, so the device-count and accelerator-match checks are skipped for it.
Additionally, I addressed another issue discovered during testing: when instantiate_device_type_tests generates per-device test variants (e.g., TestReshardCPU, TestReshardPRIVATEUSE1), using torch.accelerator.current_accelerator() to resolve the backend causes the CPU variant to incorrectly use the host accelerator's backend (e.g., hccl) instead of gloo. The fix uses self.device_type (set by instantiate_device_type_tests) to determine the correct backend per variant.
Verified on NPU (Ascend 910B3):
TestReshardPRIVATEUSE1: correctly resolves tohccl, 2 tests pass (OK)TestReshardCPU: correctly resolves togloo(no longer attemptshccl)
…ty gating Summary: - Replace @requires_nccl() with @requires_capabilities(Capability.distributed.backend) - Replace .cuda(self.rank) with .to(device) - Replace hardcoded placement with dynamic device_type - Add hw_classification = HardwareClassification.ACCELERATOR - Add instantiate_device_type_tests for device-variant generation Part of pytorch#185590. Depends on: pytorch#187650, pytorch#190528, pytorch#191919, pytorch#192417, pytorch#193080
…ty gating Summary: - Replace @requires_nccl() with @requires_capabilities(Capability.distributed.backend) - Replace .cuda(self.rank) with .to(device) - Replace hardcoded placement with dynamic device_type - Add hw_classification = HardwareClassification.ACCELERATOR - Add instantiate_device_type_tests for device-variant generation Part of pytorch#185590. Depends on: pytorch#187650, pytorch#190528, pytorch#191919, pytorch#192417, pytorch#193080
…of-tree accelerators - Change default from backend="nccl" to backend=None (dynamic resolution) - Use self.device_type to determine correct backend per test variant - Replace hard-coded ["nccl", "xccl", "hccl"] with dist.is_backend_available() - CPU variants correctly use gloo even when accelerator is present on host Verified on NPU (Ascend 910B3): - TestReshardPRIVATEUSE1: correctly resolves to hccl, 2 tests pass - TestReshardCPU: correctly resolves to gloo
374b0c4 to
d328415
Compare
| # present on the host. | ||
| device_type = getattr(self, "device_type", "cpu") | ||
| if backend is None: | ||
| if device_type == "cpu": |
There was a problem hiding this comment.
If the device type is correct, there is no need to add the judgment if device_type == "cpu": resolved_backend = "gloo". get_default_backend_for_device is capable of returning the corresponding backend for the cpu device‑type.
There was a problem hiding this comment.
Thanks for the review. Updated in the latest push and verified locally.
Change: Simplified the resolution to a single lookup as you suggested:
device_type = getattr(self, "device_type", "cpu")
if backend is None:
resolved_backend = dist.get_default_backend_for_device(device_type)
else:
resolved_backend = backend
get_default_backend_for_device("cpu") returns "gloo" via Backend.default_device_backend_map ("cpu": GLOO), so the explicit device_type == "cpu" branch was redundant. This also drops the current_accelerator() call and the else "gloo" fallback from the resolution path — the downstream skip check (acc is None / resolved_backend != get_default_backend_for_device(acc)) already covers those cases.
As a side benefit, resolving from device_type (the variant's own device) rather than current_accelerator() (the host's accelerator) is more correct on cross-device hosts: each variant gets its own backend instead of inheriting the host's. For example, a cuda variant on an NPU-only host now resolves to nccl and is correctly skipped, rather than being mis-resolved to hccl.
Note: get_default_backend_for_device raises ValueError for an unregistered device_type, but this doesn't introduce a new failure mode — the previous code queried the same map via current_accelerator(). Out-of-tree backends register themselves into default_device_backend_map via register_backend(..., devices=[...]), which both approaches rely on.
Local verification (PrivateUse1, torch_npu 2.13.0, Ascend 910B3, NPU host):
device_type="npu" → backend resolved to hccl ✅
device_type="cpu" (with NPU present on host) → backend resolved to gloo ✅
get_default_backend_for_device("cpu") returns "gloo" as expected, confirming the simplified lookup is equivalent to the removed explicit branch.
…vice get_default_backend_for_device(device_type) already maps "cpu" -> "gloo" via Backend.default_device_backend_map, so the explicit device_type == "cpu" branch is redundant. Drop it along with the current_accelerator() fallback in the resolution path; the downstream skip check already covers the no-accelerator / backend-mismatch cases. Resolving from device_type (the variant's device) rather than current_accelerator() (the host's accelerator) is also more correct on cross-device hosts: each variant gets its own backend instead of the host accelerator's backend.
JamesD18
left a comment
There was a problem hiding this comment.
The default value has been changed from NCCL to GLOO, and this modification is not recommended.
Summary
The
with_commsdecorator inShardedTensorTestBasehardcodesbackend="nccl"as the default. This causes test variants for non-NCCLaccelerator backends (e.g. PrivateUse1 backends using HCCL) to be
skipped at the wrapper level, because
backend != dist.get_default_backend_for_device(acc).This PR changes the default from
"nccl"toNone, and resolves thebackend dynamically in the wrapper via a single lookup:
dist.get_default_backend_for_device(self.device_type)(whereself.device_typeis set byinstantiate_device_type_tests, with agetattrfallback to"cpu"for classes not using it).get_default_backend_for_devicequeriesBackend.default_device_backend_map(
"cpu": GLOO,"cuda": NCCL,"xpu": XCCL,"mps": GLOO, plusout-of-tree entries registered via
register_backend(..., devices=[...])),so each device variant gets its own backend:
glooncclxcclrename_privateuse1_backend) →hcclResolving from
self.device_type(the variant's own device) rather thantorch.accelerator.current_accelerator()(the host's accelerator) ensuresper-device test variants get the correct backend even on cross-device hosts.
For example, a CPU variant on an NPU host resolves to
glooinstead of thehost's
hccl(which would causeProcessGroupHCCL::scatterto receive aCPU tensor when an NPU tensor is expected); likewise a
cudavariant on anNPU-only host resolves to
nccland is correctly skipped, rather than beingmis-resolved to
hccl.The accelerator-backend skip check uses
resolved_backend != "gloo"+dist.is_backend_available()instead of a hard-coded backend list, sonew accelerator backends are automatically supported without modifying
the check.
Changes
with_comms(func=None, init_rpc=True, backend="nccl")towith_comms(func=None, init_rpc=True, backend=None).backend is None,resolve via a single lookup
dist.get_default_backend_for_device(device_type), wheredevice_typeis obtained from
getattr(self, "device_type", "cpu")(the"cpu"default ensures backward compatibility with classes not using
instantiate_device_type_tests).if resolved_backend in ["nccl", "xccl", "hccl"]:withif resolved_backend != "gloo":, and addeddist.is_backend_available(resolved_backend)check to skip tests whenthe backend is not compiled/available. New accelerator backends are
automatically supported without modifying the check.
torch.accelerator.current_accelerator()call into theaccelerator-backend branch (only needed when
resolved_backendis not"gloo"), avoiding unnecessary accelerator initialization for CPUvariants.
backendtoresolved_backendin theskip-check and
init_commscall.Before vs After
nccl→ skip ✗gloo✓nccl→ skip ✗gloo✓nccl→ skip ✗hccl✓nccl✓nccl✓backend="gloo"gloo✓gloo✓xccl)is_backend_available→ skip ✓Not changed
backend=are unaffected (backwardcompatible).
skipped if the backend is not available, does not match the current
accelerator, or if device count is insufficient.
init_pgis not modified (already handles dynamic device binding viatorch.accelerator.set_device_index).Test plan
(PrivateUse1, torch_npu 2.13.0+git45fbeae on PyTorch 2.13.0a0+gitfad7424,
Ascend 910B3, NPU host):
device_type="npu"→ backend resolved tohccl✅device_type="cpu"(with NPU present on host) → backend resolved togloo✅ (previously mis-resolved to the host'shccl)get_default_backend_for_device("cpu")returns"gloo"as expected,confirming the single-lookup approach is equivalent to the removed
device_type == "cpu"special-case.test_sharded_tensor_reshard.pydoes not callinstantiate_device_type_tests, soTestReshardhas no per-devicevariants (e.g.
TestReshardCPU/TestReshardNPU);device_typefallsback to the
"cpu"default. The per-device variant paths were verifiedvia mock test classes (
FakeNPUTest/FakeCPUTest) that setdevice_typeand assert the backend passed toinit_comms.test_sharded_tensor_reshard.pyare decoratedwith
@requires_nccl(), which skips them on hosts where NCCL is notcompiled (independent of this PR). Replacing
@requires_nccl()with acapability-based decorator is tracked as a separate follow-up.
with_commstoconfirm no regressions.