fix: use device-agnostic autocast and tensor placement for ROCm/XPU compatibility#15921
Open
zhihuidu-amd wants to merge 1 commit into
Open
fix: use device-agnostic autocast and tensor placement for ROCm/XPU compatibility#15921zhihuidu-amd wants to merge 1 commit into
zhihuidu-amd wants to merge 1 commit into
Conversation
…ompatibility
Replace hardcoded torch.amp.autocast('cuda', ...) with a _current_device_type()
helper that returns the active accelerator type, and replace .cuda() tensor
placements with .to(device=...) equivalents.
This allows NeMo to run on AMD ROCm and Intel XPU backends without changes,
while remaining fully backwards-compatible on CUDA. Validated on AMD MI300X
(gfx942, ROCm 7.2) with stt_en_jasper10x5dr (332M params): imports OK,
inference correct, FP32/BF16 throughput 1145x/2685x real-time.
Files changed:
- nemo/utils/cast_utils.py: add _current_device_type(), replace 4x hardcoded 'cuda'
- nemo/utils/callbacks/cuda_graph.py: .cuda() -> .to(src.device)
- nemo/collections/asr/losses/rnnt_pytorch.py: .cuda() -> .to(device=acts.device)
- nemo/collections/asr/models/confidence_ensemble.py: .cuda() -> .to(device=...)
Signed-off-by: Zhihui Du <zhihuidu-amd@users.noreply.github.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.
Summary
Replace hardcoded
torch.amp.autocast('cuda', ...)with a_current_device_type()helper, and replace.cuda()tensor placements with.to(device=...)equivalents. This makes NeMo run correctly on AMD ROCm and Intel XPU without changes, while remaining fully backwards-compatible on CUDA.Changes
nemo/utils/cast_utils.py: Add_current_device_type()helper; replace 4 hardcoded'cuda'strings inavoid_bfloat16_autocast_context()andavoid_float16_autocast_context()nemo/utils/callbacks/cuda_graph.py:.cuda()→.to(src.device)nemo/collections/asr/losses/rnnt_pytorch.py:.cuda()→.to(device=acts.device)nemo/collections/asr/models/confidence_ensemble.py:.cuda()→.to(device=next(self.parameters()).device)Motivation
NeMo currently hardcodes
'cuda'as the device type in severaltorch.amp.autocastcalls and uses.cuda()in a few tensor placement sites. This causes failures on non-CUDA accelerators even whentorch.cuda.is_available()returnsTruevia compatibility layers.This fix was validated on AMD MI300X (gfx942, ROCm 7.2) with
stt_en_jasper10x5dr(332M params): full import chain passes, FP32 and BF16 inference are correct and consistent, throughput is 1145×/2685× real-time at batch size 8.Testing
'cuda'as before)Related
AMD ROCm port: https://github.com/AMD-AIOSS/ROCm-NeMo