fix: use torch.amp via compatibility shim to avoid deprecated torch.cuda.amp warnings - #3518
fix: use torch.amp via compatibility shim to avoid deprecated torch.cuda.amp warnings#3518xyf5432 wants to merge 2 commits into
Conversation
…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>
Signed-off-by: LauraGPT <170200537+LauraGPT@users.noreply.github.com>
|
Pushed signed commit Verification:
The deselected |
LauraGPT
left a comment
There was a problem hiding this comment.
The fallback cannot run on PyTorch releases that do not expose torch.amp. PyTorch v1.9.1 provides torch.cuda.amp but has no torch.amp package: https://github.com/pytorch/pytorch/tree/v1.9.1/torch/cuda/amp
The condition accesses torch.amp before hasattr, so importing the shim raises AttributeError before reaching the else branch. I reproduced this with a torch stub that provides torch.cuda.amp.autocast and GradScaler but no torch.amp.
Please retrieve torch.amp safely (for example, with getattr(torch, "amp", None)) and add a regression test for the no-torch.amp path. Existing model guards retain PyTorch 1.6+ behavior, so this is within the compatibility scope of this PR.
Fixes #3517 —
torch.cuda.amp.autocasthas been deprecated since torch 2.4 andtorch.cuda.amp.GradScalersince torch 2.3.Summary
Both APIs still work but emit a
FutureWarningwhen used; thereplacement 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) alreadycall
warnings.filterwarnings("ignore")at module level, so theFutureWarningis not visible on those paths today. The real risk is theannounced removal of
torch.cuda.amp("will be removed in a futurerelease", no version pinned; still present in 2.11): the moment torch
drops it, all 37
from torch.cuda.amp import ...sites in this reporaise
ImportErrorat import time — a hard failure no warning filtercan mask.
Changes:
funasr/utils/amp.py, a compatibility shim that re-exportstorch.ampnames when available (torch >= 2.3) and falls back totorch.cuda.ampotherwise.device_type="cuda"by default forautocast,matching the old
torch.cuda.amp.autocastsignature — existing callsites (
with autocast(enabled=..., dtype=...)) work unchanged.from torch.cuda.amp import ...imports with the shim.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
ImportErrorwhen torch removestorch.cuda.ampValidation
Verified on torch 2.11.0 with
warnings.simplefilter("error", FutureWarning)(re-asserted after importing funasr —
export_utils.py/realtime_ws.pyset a catch-all
filterwarnings("ignore")at module level that wouldotherwise mask the test filter):
from funasr.utils.amp import autocast, GradScaler— no FutureWarningwith autocast():/with autocast(enabled=True, dtype=None, cache_enabled=False):— no FutureWarningGradScaler(enabled=True)— no FutureWarningexplicit
device_typeand positional args still pass throughtorch < 2.3 fallback path is identical to today (not yet deprecated there)
[√]
python -m compileall funasr examples tests— passes. (The SyntaxWarnings infunasr/models/fsmn_kws/encoder.pyare 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
FutureWarningon 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
torch.cuda.amp.autocastsignature via adevice_type="cuda"default, becausetorch.amp.autocastrequires apositional
device_type.autocastand
GradScalerfromtorch.amptogether, andGradScaleronlyexists there since 2.3.
bat/model.pykeeps its torch < 1.6 guard — the shim's fallbackimports
torch.cuda.amp, which only exists since 1.6.change; the fix is future-proofing for the removal.
Reference
torch.cuda.amp.autocast/GradScalerdeprecated since 2.4 / 2.3, "will be removed in a future release")