fix: use torch.amp via compatibility shim to avoid deprecated torch.cuda.amp warnings - #3518
Open
xyf5432 wants to merge 1 commit into
Open
fix: use torch.amp via compatibility shim to avoid deprecated torch.cuda.amp warnings#3518xyf5432 wants to merge 1 commit into
xyf5432 wants to merge 1 commit 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>
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.
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")torch.cuda.amp→torch.ampmigration, merged