Repository navigation
[UT] Pin device_type to CPU in test_aux_loss SPMD tests - #4792
Merged
Merged
Conversation
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]>
XueHuCH
requested review from
fegin,
tianyu-l,
wconstab and
wwwjn
as code owners
September 18, 2026 22:43
|
|
tianyu-l
approved these changes
Sep 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TestMicrobatchWiseLossSpmdTypesintests/unit_tests/cpu/test_aux_loss.pyis CPU-only by construction:_build_dimspatchestorchtitan.distributed.parallel_dims.device_typeto "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_typeit inherits fromDTensorTestMixin. That property returns the accelerator type whenever an accelerator is visible and DEVICE_COUNT >= world_size, andDTensorTestBase.backendderives the process group backend from it viadist.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: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.