Skip to content

[UT] Pin device_type to CPU in test_aux_loss SPMD tests - #4792

Merged
tianyu-l merged 1 commit into
pytorch:mainfrom
XueHuCH:xue/cpu-test-aux-loss-device-type
Sep 19, 2026
Merged

tianyu-l merged 1 commit into
pytorch:mainfrom
XueHuCH:xue/cpu-test-aux-loss-device-type

Conversation

@XueHuCH

@XueHuCH XueHuCH commented Sep 18, 2026

Copy link
Copy Markdown
Collaborator

TestMicrobatchWiseLossSpmdTypes in tests/unit_tests/cpu/test_aux_loss.py is CPU-only by construction: _build_dims patches
torchtitan.distributed.parallel_dims.device_type to "cpu", the dense and sparse meshes are built on CPU, and the router scores/routing maps are CPU float64 tensors.

It does not, however, pin the device_type it inherits from DTensorTestMixin. That property returns the accelerator type whenever an accelerator is visible and DEVICE_COUNT >= world_size, and DTensorTestBase.backend derives the process group backend from it via dist.get_default_backend_for_device(self.device_type). So on a host with at least 8 accelerator devices the process group is initialized with the accelerator backend only (xccl on XPU, nccl on CUDA), while the collectives the test actually issues run on CPU tensors. The all_reduce that MicrobatchWiseLoadBalanceLoss._reduce_token_partials performs through spmd.redistribute then fails:

RuntimeError: No backend type associated with device type cpu

On CPU-only CI hosts torch.accelerator.device_count() is 0, so device_type already resolves to "cpu" and gloo is selected, which is why this passes upstream today.

Override device_type to return "cpu" so gloo is selected regardless of which accelerators happen to be visible. This is the convention the other CPU SPMD test cases in the same directory already follow: TestRouterGateLinearSPMD in tests/unit_tests/cpu/test_router_gate_linear.py and TestChunkedLossWrapperSPMD in tests/unit_tests/cpu/test_loss.py both pin device_type this way. Behavior on CPU-only hosts is unchanged; the change only affects which backend is chosen on accelerator hosts.

TestMicrobatchWiseLossSpmdTypes in tests/unit_tests/cpu/test_aux_loss.py is
CPU-only by construction: _build_dims patches
torchtitan.distributed.parallel_dims.device_type to "cpu", the dense and
sparse meshes are built on CPU, and the router scores/routing maps are CPU
float64 tensors.

It does not, however, pin the device_type it inherits from DTensorTestMixin.
That property returns the accelerator type whenever an accelerator is visible
and DEVICE_COUNT >= world_size, and DTensorTestBase.backend derives the
process group backend from it via
dist.get_default_backend_for_device(self.device_type). So on a host with at
least 8 accelerator devices the process group is initialized with the
accelerator backend only (xccl on XPU, nccl on CUDA), while the collectives
the test actually issues run on CPU tensors. The all_reduce that
MicrobatchWiseLoadBalanceLoss._reduce_token_partials performs through
spmd.redistribute then fails:

    RuntimeError: No backend type associated with device type cpu

On CPU-only CI hosts torch.accelerator.device_count() is 0, so device_type
already resolves to "cpu" and gloo is selected, which is why this passes
upstream today.

Override device_type to return "cpu" so gloo is selected regardless of which
accelerators happen to be visible. This is the convention the other CPU SPMD
test cases in the same directory already follow: TestRouterGateLinearSPMD in
tests/unit_tests/cpu/test_router_gate_linear.py and
TestChunkedLossWrapperSPMD in tests/unit_tests/cpu/test_loss.py both pin
device_type this way. Behavior on CPU-only hosts is unchanged; the change
only affects which backend is chosen on accelerator hosts.

Co-Authored-By: Claude Opus 5 <[email protected]>
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Sep 18, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 18, 2026
@pytorch-bot

pytorch-bot Bot commented Sep 18, 2026 •

Copy link
Copy Markdown

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

@tianyu-l
tianyu-l merged commit 23cd613 into pytorch:main Sep 19, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants