Conversation
…elerator `_get_max_memory()` (used by `patch_mp_ddp` for the device_map + DDP path) queried per-device memory with `torch.cuda.mem_get_info` and warmed the devices up with a bare integer device id. On a non-CUDA accelerator (Ascend NPU, XPU, MUSA) the CUDA-only call raises `AssertionError: Torch not compiled with CUDA enabled`, so `infer_auto_device_map` aborts before the model is sharded. Resolve the accelerator namespace with the helpers this package already exposes (`get_torch_device()` / `get_device()`), the same way `torch_utils.get_max_reserved_memory` does, and cover it with a unit test. Signed-off-by: li-lizhe <147392333@qq.com>
… real device ordinal Signed-off-by: li-lizhe <147392333@qq.com>
This branch has not been deployed
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.
PR type
PR information
swift/model/patcher.py::_get_max_memory()(called frompatch_mp_ddp()for thedevice_map+ DDP path) pins the device handling to the CUDA namespace:torch.tensor([0], device=i)— which only resolves under CUDA, andtorch.cuda.mem_get_info(i).On a non-CUDA accelerator (Ascend NPU / XPU / MUSA) those calls abort inside
_infer_auto_device_map_patchbefore the model is sharded, so MP +device_mapcannot run there:This resolves the accelerator namespace through helpers the package already exposes —
get_torch_device()andget_device(i)— mirroringswift/utils/torch_utils.py::get_max_reserved_memory(), which already dispatches viaget_torch_device(). CUDA behaviour is unchanged:get_device(i)returnscuda:iandget_torch_device()returnstorch.cuda, i.e. the same calls as before. A unit test is added next to the analogoustests/utils/test_max_reserved_memory.py.Experiment results
Real Ascend NPU (910, CANN 9.2.0-beta.2, torch 2.15.0.dev20260917+cpu, torch_npu 2.15.0.dev20260917+gitec69335, 1 card). The real
_get_max_memorybody was extracted from the source via AST and executed withget_device_countmapped totorch.npu.device_count:_get_max_memory([0])AssertionError: Torch not compiled with CUDA enabled{0: 31285399552, 'cpu': 2140501688320}—max_memory[0]equalstorch.npu.mem_get_info(0)[0]_get_max_memory([])still returns{0: 0, 'cpu': ...}(device not in this shard ⇒ 0).Unit test
tests/general/test_mp_ddp_max_memory.py: passes against the patched source; against the upstream body it fails on CPU-only torch (RuntimeError: Cannot access accelerator device when none is available.). Formatting follows the repo gates (flake8,isort,yapf 0.43.0— all clean on the touched files).Not tested: CUDA / XPU / MUSA paths (no such hardware here); a full end-to-end MP +
device_maptorchrunrun was not exercised — only the changed helper.Notes