Repository navigation
fix(fsdp): run torch_musa's FSDP2 replacements only on MUSA meshes - #126
Closed
yeahdongcn wants to merge 1 commit into
Closed
yeahdongcn wants to merge 1 commit into
yeahdongcn wants to merge 1 commit into
Conversation
torch_musa replaces parts of FSDP2 with versions that create MUSA streams
whatever the device mesh is, so fully_shard on a CPU (gloo) mesh fails in
the root pre-forward: with MUSA devices visible a MUSA stream waits on a CPU
stream ('NoneType' object has no attribute 'wait'), and with
MUSA_VISIBLE_DEVICES= the comm-context setup raises "No MUSA GPUs are
available". PyTorch runs the same CPU-mesh FSDP2 without error.
Route torch_musa's FSDP2 replacements by device type:
- torch.distributed.fsdp.fully_shard sends a non-MUSA mesh straight to
PyTorch's fully_shard; MUSA meshes and mesh=None keep torch_musa's
wrapper. A reference to torch_musa's wrapper bound before
`import torchada` is not routed.
- FSDPCommContext.lazy_init, FSDPParamGroup.wait_for_unshard and
post_backward, and FSDPState._root_post_backward_final_callback dispatch
per call: a "musa" device runs torch_musa's function, any other device
runs PyTorch's. The torch_musa module globals that hold these functions,
including the ones its lazy FSDP2 setup assigns from, are the same
dispatchers, so the methods pickle by reference as torch_musa's do.
- PyTorch's methods that torch_musa replaces at import are rebuilt from
the installed source after checking that it matches the loaded code. If
any cannot be recovered, nothing is installed and torch_musa's FSDP2
stays in place.
- A warning names any other method of these classes that torch_musa
replaces, which still runs for every mesh, and a fully_shard attribute
that is not torch_musa's wrapper and so is not routed.
MUSA meshes keep running torch_musa's functions, with its streams, overlap
level and collectives.
The tests run a fake torch_musa FSDP2 layer on CPU, including multi-process
gloo runs whose results and FSDP2 call traces match PyTorch's, and add
MUSA-host tests against the real torch_musa. A MUSA-mesh comparison is
skipped, naming the configuration and torch_musa's error or timeout, where
torch_musa alone cannot complete it.
Signed-off-by: Xiaodong Ye <xiaodong.ye@mthreads.com>
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.
What / Why
torch_musa replaces parts of FSDP2 at import time with versions that create MUSA streams whatever the device mesh is.
fully_shardon a CPU (gloo) mesh therefore fails in the root pre-forward under torch_musa:AttributeError: 'NoneType' object has no attribute 'wait';MUSA_VISIBLE_DEVICES=, the communication-context setup callstorch.musa.current_stream():RuntimeError: No MUSA GPUs are available.PyTorch runs the same CPU-mesh FSDP2 without error. vLLM-Omni's MAGI-2
test_compiled_regions_match_eager_under_dlo_and_hsdp(HSDP on a 4-rank gloo CPU mesh) fails this way on MUSA.Change
New
src/torchada/_fsdp2.py, installed by_patch_fsdp2_device_dispatchin_patch.py:torch.distributed.fsdp.fully_shardsends a non-MUSA mesh straight to PyTorch'sfully_shard; MUSA meshes andmesh=Nonekeep torch_musa's wrapper.FSDPCommContext.lazy_init,FSDPParamGroup.wait_for_unshardandpost_backward, andFSDPState._root_post_backward_final_callbackdispatch per call: amusadevice runs torch_musa's function, any other device runs PyTorch's. The torch_musa module globals that hold these functions are the same dispatchers, so the methods still pickle by reference.fully_shardattribute that is neither torch_musa's wrapper nor PyTorch's and so is not routed. A reference to torch_musa's wrapper bound beforeimport torchadais not routed.MUSA meshes keep running torch_musa's functions, with its streams, overlap level and collectives. Nothing is imported that torchada would not already import.
Verification
Tests:
tests/test_fsdp2_device_dispatch.py, with a fake torch_musa FSDP2 layer (tests/_fsdp2_fakes.py) and a multi-process worker (tests/_fsdp2_mesh_worker.py):musaandcudameshes, 1-D and 1x2, default, overlap level 4 and both custom collective types) that must match torch_musa without this change. A MUSA-mesh comparison is skipped, naming the configuration and torch_musa's error or timeout, where torch_musa alone cannot complete it.Results on MTT S5000 (torch 2.11.0.post2, torch_musa 2.11.0.post2+musa5.2.0, 2 cards):
RuntimeError: MCCL error ... unhandled musa errorin the gradient all-reduce (with this change it fails the same way), andTORCH_MUSA_FSDP2_OVERLAP_LEVEL=4hangs. On origin/main 7 of the MUSA-host tests fail; the ones that run FSDP2 on a CPU mesh fail with the two errors above.test_compiled_regions_match_eager_under_dlo_and_hsdpon vllm-project/vllm-omni main and on #8595, with this change and fix(device): register the device factory as the FX device builtin聽#124: passes with devices visible and hidden.pytest tests/(other files): 2 failed, 591 passed, 9 skipped with and without this change, the same twoTestInductorTemplateHeuristicsfailures.Not covered / notes
ShardedGradScaleron a CPU mesh is still torch_musa's version.AI assistance: Claude Code drafted the change, the tests and this description and ran the MUSA validation listed above.