Skip to content

fix: use device-agnostic autocast and tensor placement for ROCm/XPU compatibility#15921

Open
zhihuidu-amd wants to merge 1 commit into
NVIDIA-NeMo:mainfrom
zhihuidu-amd:rocm/device-agnostic-autocast
Open

fix: use device-agnostic autocast and tensor placement for ROCm/XPU compatibility#15921
zhihuidu-amd wants to merge 1 commit into
NVIDIA-NeMo:mainfrom
zhihuidu-amd:rocm/device-agnostic-autocast

Conversation

@zhihuidu-amd

Copy link
Copy Markdown

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 in avoid_bfloat16_autocast_context() and avoid_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 several torch.amp.autocast calls and uses .cuda() in a few tensor placement sites. This causes failures on non-CUDA accelerators even when torch.cuda.is_available() returns True via 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: no behavior change (function returns 'cuda' as before)
  • ROCm: validated on AMD MI300X, NeMo ASR import + inference correct
  • CI: please run existing unit tests

Related

AMD ROCm port: https://github.com/AMD-AIOSS/ROCm-NeMo

…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>
@copy-pr-bot

copy-pr-bot Bot commented Jul 17, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@svcnvidia-nemo-ci svcnvidia-nemo-ci added the waiting-on-maintainers Waiting on maintainers to respond label Jul 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ASR community-request waiting-on-maintainers Waiting on maintainers to respond

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants