`torch.cuda.amp.autocast` / `GradScaler` usage across FunASR emits FutureWarning on torch ≥2.4 — migrate to `torch.amp`
Summary
FunASR uses the deprecated torch.cuda.amp APIs in 37 files. The
hot paths that actually call them:
| File | Line |
|---|---|
funasr/models/llm_asr/model.py |
8 from torch.cuda.amp import autocast; 7 call sites (586, 621, 886, 1162, 1206, 1554 with torch.cuda.amp.autocast(...)) |
funasr/train_utils/trainer_ds.py |
9 from torch.cuda.amp import autocast, GradScaler; 35 with torch.cuda.amp.autocast(enabled=True, dtype=dtype, ...) |
funasr/models/bat/model.py |
27 from torch.cuda.amp import autocast (guard only checks torch < 1.6, not the deprecation) |
…34 more files reference torch.cuda.amp |
see below |
Verified on torch 2.11.0:
>>> with torch.cuda.amp.autocast():
FutureWarning: `torch.cuda.amp.autocast(args...)` is deprecated. Please
use `torch.amp.autocast('cuda', args...)` instead.
autocast has been deprecated since torch 2.4 and GradScaler since
torch 2.3. Both still work but print a FutureWarning on every call —
the current import guard in bat/model.py only covers torch < 1.6 and
does not protect against this.
Suggested fix
The replacement APIs (torch.amp.autocast('cuda', ...) since 2.0,
torch.amp.GradScaler('cuda', ...) since 2.3) are available in all
current torch versions. If older torch versions must remain supported,
gate on the version — 2.3 is the boundary because the import below needs
both names, and torch < 2.3's torch.cuda.amp is not yet deprecated:
if LooseVersion(torch.__version__) >= LooseVersion("2.3"):
from torch.amp import autocast, GradScaler
else:
from torch.cuda.amp import autocast, GradScaler
Installation has no torch version constraint
FunASR is a single-package monorepo (one setup.py → funasr; runtime/
holds platform deployments). The install path does not bound torch:
setup.pyinstall_requiresdoes not list torch at all — pip never installs or constrains it.- README.md:38-44
instructs
pip install torch torchaudio(latest PyPI = torch 2.11), thenpip install funasr.
So on a fresh install with current PyPI, torch 2.11 is the documented outcome and the deprecation warnings fire on every autocast/GradScaler use.
(Also observed: the Triton GPU deployment image
runtime/triton_gpu/Dockerfile/Dockerfile.server
pins pip3 install torch==2.4.1 torchaudio==2.4.1 — pinned to match the
prebuilt kaldifeat CUDA wheel. torch 2.4.1 is itself the first release
where torch.cuda.amp.autocast is deprecated, so even that pinned
deployment ships the deprecated API.)
Files referencing torch.cuda.amp (37 total)
funasr/bin/train.py, funasr/bin/train_ds.py, funasr/models/campplus/model.py,
funasr/models/data2vec/data2vec.py, funasr/models/llm_asr/model.py,
funasr/models/xvector/e2e_sv.py, funasr/models/sanm_kws/model.py,
funasr/models/fsmn_kws/model.py, funasr/models/e_paraformer/pif_predictor.py,
funasr/models/lcbnet/model.py, funasr/models/scama/model.py,
funasr/models/emotion2vec/model.py, funasr/train_utils/trainer.py,
funasr/models/uniasr/model.py, funasr/models/fsmn_kws_mt/model.py,
funasr/train_utils/trainer_ds.py, funasr/models/transducer/model.py,
funasr/models/mfcca/e2e_asr_mfcca.py, funasr/models/sa_asr/e2e_sa_asr.py,
plus 18 more under funasr/models/ and funasr/train_utils/.
A note on impact, in case it informs the discussion:
The FutureWarning is not visible to users of two entry points —
funasr/utils/export_utils.py:7 and funasr/bin/realtime_ws.py:32 —
because both call warnings.filterwarnings("ignore") at module level,
which silences all warnings including this one. On the other entry points
(training / inference CLIs, model loading) the warning does show up,
though Python's default filter dedups by message + source line, so users
see it once per distinct call site per process rather than on every call.
The warning noise is not the real problem though — the removal is. The
deprecation notice says these APIs "will be removed in a future release"
(no version pinned; both still exist and work in torch 2.11). When the
removal lands, every from torch.cuda.amp import ... site in this repo
(37 files) raises ImportError at import time — a hard failure that no
warnings.filterwarnings("ignore") can suppress, and one that breaks
every model/trainer entry point at once. Migrating to torch.amp is
about compatibility with that removal, not just warning hygiene.
Reopening this because I closed it immediately after the fix merged, before the reporter had a chance to verify it. #3518 merged as 9413d7d7688373d069ff825067e76c4537d43896 and is included in FunASR v1.4.4 and later. Current main routes AMP usage through the compatibility shim, with the legacy torch.cuda.amp import retained only as the fallback for older PyTorch versions. Please verify on your affected PyTorch version when convenient. I will keep this open until reporter confirmation, or until we have reproducible verification plus a reasonable feedback window.
Verified against the released v1.4.4 tag on torch 2.11.0+cpu in a clean venv:
funasr.utils.amp.autocastwrapstorch.amp.autocast('cuda', ...)andGradScaleristorch.amp.GradScaler— importing the shim emits zero FutureWarnings.- With
warnings.simplefilter("error", FutureWarning)re-asserted after import,with autocast(enabled=True, dtype=torch.float16):andGradScaler()both run clean. - All 38
torch.cuda.ampimport sites now route through the shim; notorch.cuda.amp.autocast/GradScalerattribute calls remain in the repo.
Confirmed fixed. Thanks for the fix; you can close this issue.
感谢 @xyf5432 在干净的 funasr v1.4.4 + torch 2.11.0+cpu 环境中完成发布版复测,并逐项确认兼容 shim、38 个导入点以及运行时路径均不再产生 FutureWarning。
这次关闭依据是报告者的明确验证结果,而不是 PR 合并本身。修复已包含在 v1.4.4 及后续版本;若其他 PyTorch / CUDA 组合仍出现同类告警,请带完整版本和调用入口新开 issue,我们会按新的可复现环境处理。