Skip to content

fix: use torch.amp via compatibility shim to avoid deprecated torch.cuda.amp warnings - #3518

Open
xyf5432 wants to merge 1 commit into
modelscope:mainfrom
xyf5432:fix/amp-deprecation-shim
Open

fix: use torch.amp via compatibility shim to avoid deprecated torch.cuda.amp warnings#3518
xyf5432 wants to merge 1 commit into
modelscope:mainfrom
xyf5432:fix/amp-deprecation-shim

Conversation

@xyf5432

@xyf5432 xyf5432 commented Aug 20, 2026

Copy link
Copy Markdown

Fixes #3517torch.cuda.amp.autocast has been deprecated since torch 2.4 and torch.cuda.amp.GradScaler since torch 2.3.

Summary

Both APIs still work but emit a FutureWarning when used; the
replacement device-agnostic APIs live in torch.amp.

Why migrate now, beyond warning noise: two entry points
(funasr/utils/export_utils.py, funasr/bin/realtime_ws.py) already
call warnings.filterwarnings("ignore") at module level, so the
FutureWarning is not visible on those paths today. The real risk is the
announced removal of torch.cuda.amp ("will be removed in a future
release", no version pinned; still present in 2.11): the moment torch
drops it, all 37 from torch.cuda.amp import ... sites in this repo
raise ImportError at import time — a hard failure no warning filter
can mask.

Changes:

  • Adds funasr/utils/amp.py, a compatibility shim that re-exports
    torch.amp names when available (torch >= 2.3) and falls back to
    torch.cuda.amp otherwise.
  • The shim supplies device_type="cuda" by default for autocast,
    matching the old torch.cuda.amp.autocast signature — existing call
    sites (with autocast(enabled=..., dtype=...)) work unchanged.
  • Replaces all 37 from torch.cuda.amp import ... imports with the shim.
  • Rewrites the 7 torch.cuda.amp.autocast(...) attribute call sites
    (llm_asr/model.py ×6, trainer_ds.py ×1) to use the imported name, so
    the deprecation path is actually avoided.

Type of change

  • [√] Bug fix — deprecated API usage that becomes a hard ImportError when torch removes torch.cuda.amp
  • Documentation
  • Example or demo
  • Runtime or deployment
  • Benchmark or evaluation
  • Model/training change

Validation

Verified on torch 2.11.0 with warnings.simplefilter("error", FutureWarning)
(re-asserted after importing funasr — export_utils.py / realtime_ws.py
set a catch-all filterwarnings("ignore") at module level that would
otherwise mask the test filter):

  • from funasr.utils.amp import autocast, GradScaler — no FutureWarning

  • with autocast(): / with autocast(enabled=True, dtype=None, cache_enabled=False): — no FutureWarning

  • GradScaler(enabled=True) — no FutureWarning

  • explicit device_type and positional args still pass through

  • torch < 2.3 fallback path is identical to today (not yet deprecated there)

  • [√] python -m compileall funasr examples tests — passes. (The SyntaxWarnings in funasr/models/fsmn_kws/encoder.py are pre-existing invalid escape sequences; that file is not touched by this PR.)

  • Docs or links checked

  • Runtime/deployment command tested

User impact

Users of FunASR on torch >= 2.3 — including the current PyPI default
torch 2.11, which the documented README install path resolves to — no
longer see the deprecation FutureWarning on training/inference paths,
and every entry point keeps importing when torch eventually removes
torch.cuda.amp. No behavior change on torch < 2.3.

Notes for reviewers

  • The shim mirrors the old torch.cuda.amp.autocast signature via a
    device_type="cuda" default, because torch.amp.autocast requires a
    positional device_type.
  • Migration boundary is 2.3, not 2.0: the shim imports both autocast
    and GradScaler from torch.amp together, and GradScaler only
    exists there since 2.3.
  • bat/model.py keeps its torch < 1.6 guard — the shim's fallback
    imports torch.cuda.amp, which only exists since 1.6.
  • The two entry points that already silence all warnings see no visible
    change; the fix is future-proofing for the removal.

Reference

  • PyTorch AMP docs — official deprecation notice (torch.cuda.amp.autocast / GradScaler deprecated since 2.4 / 2.3, "will be removed in a future release")
  • ultralytics/yolov5#13244 — same torch.cuda.amptorch.amp migration, merged

…uda.amp warnings

torch.cuda.amp.autocast has been deprecated since torch 2.4 and
torch.cuda.amp.GradScaler since torch 2.3. Both still work but emit a
FutureWarning on every use; the replacement device-agnostic APIs live
in torch.amp.

Add funasr/utils/amp.py, a shim that re-exports torch.amp names when
available (torch >= 2.3) and falls back to torch.cuda.amp otherwise.
The shim also supplies device_type='cuda' by default for autocast,
matching the old torch.cuda.amp.autocast signature, so existing call
sites work unchanged.

Replace all 37 `from torch.cuda.amp import ...` imports with the shim,
and rewrite the 7 `torch.cuda.amp.autocast(...)` call sites to use the
imported name so the deprecation path is actually avoided.

Co-Authored-By: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

torch.cuda.amp.autocast / GradScaler usage across FunASR emits FutureWarning on torch ≥2.4 — migrate to torch.amp

1 participant