Open-source speech recognition toolkit for training, inference, streaming ASR, VAD, punctuation, speaker diarization pipelines, and OpenAI-compatible/MCP serving.
Python asraudiochineseemotion-recognitionfunasrmcp-servermultilingual-asropenai-compatible-apiparaformerpunctuationpytorchreal-time-asrspeaker-diarizationspeech-recognitionspeech-to-textstreaming-asrtranscriptionvllmvoice-activity-detectionwhisper-alternative
fix: use torch.amp via compatibility shim (#3518)
谢翊凡9413d7d76883
diff --git a/funasr/bin/train.py b/funasr/bin/train.pyindex d54cd0bbe..d7c20f8f0 100644--- a/funasr/bin/train.py+++ b/funasr/bin/train.py@@ -16,7 +16,7 @@ from contextlib import nullcontextimport torch.distributed as distfrom omegaconf import DictConfig, OmegaConf-from torch.cuda.amp import autocast, GradScaler+from funasr.utils.amp import autocast, GradScalerfrom torch.nn.parallel import DistributedDataParallel as DDPfrom torch.distributed.fsdp import FullyShardedDataParallel as FSDPfrom torch.distributed.algorithms.join import Joindiff --git a/funasr/bin/train_ds.py b/funasr/bin/train_ds.pyindex d1f25973e..1b18fe1b6 100644--- a/funasr/bin/train_ds.py+++ b/funasr/bin/train_ds.py@@ -15,7 +15,7 @@ from contextlib import nullcontextimport torch.distributed as distfrom omegaconf import DictConfig, OmegaConf-from torch.cuda.amp import autocast, GradScaler+from funasr.utils.amp import autocast, GradScalerfrom torch.nn.parallel import DistributedDataParallel as DDPfrom torch.distributed.fsdp import FullyShardedDataParallel as FSDPfrom torch.distributed.algorithms.join import Joindiff --git a/funasr/models/bat/model.py b/funasr/models/bat/model.pyindex 587eb56d1..ef88c81e9 100644--- a/funasr/models/bat/model.py+++ b/funasr/models/bat/model.py@@ -24,7 +24,7 @@ from funasr.models.transducer.beam_search_transducer import BeamSearchTransducerif LooseVersion(torch.__version__) >= LooseVersion("1.6.0"):- from torch.cuda.amp import autocast+ from funasr.utils.amp import autocastelse:# Nothing to do if torch<1.6.0@contextmanagerdiff --git a/funasr/models/bicif_paraformer/model.py b/funasr/models/bicif_paraformer/model.pyindex 0a9f86bae..6683aac85 100644--- a/funasr/models/bicif_paraformer/model.py+++ b/funasr/models/bicif_paraformer/model.py@@ -26,7 +26,7 @@ from funasr.utils.load_utils import load_audio_text_image_video, extract_fbankfrom funasr.train_utils.device_funcs import to_deviceif LooseVersion(torch.__version__) >= LooseVersion("1.6.0"):- from torch.cuda.amp import autocast+ from funasr.utils.amp import autocastelse:# Nothing to do if torch<1.6.0@contextmanagerdiff --git a/funasr/models/campplus/model.py b/funasr/models/campplus/model.pyindex 12b739137..751c30bcf 100644--- a/funasr/models/campplus/model.py+++ b/funasr/models/campplus/model.py@@ -26,7 +26,7 @@ from funasr.models.campplus.components import (if LooseVersion(torch.__version__) >= LooseVersion("1.6.0"):- from torch.cuda.amp import autocast+ from funasr.utils.amp import autocastelse:# Nothing to do if torch<1.6.0@contextmanagerdiff --git a/funasr/models/contextual_paraformer/model.py b/funasr/models/contextual_paraformer/model.pyindex 8fdce48d3..4c4df739b 100644--- a/funasr/models/contextual_paraformer/model.py+++ b/funasr/models/contextual_paraformer/model.py@@ -29,7 +29,7 @@ from funasr.utils.load_utils import load_audio_text_image_video, extract_fbankif LooseVersion(torch.__version__) >= LooseVersion("1.6.0"):- from torch.cuda.amp import autocast+ from funasr.utils.amp import autocastelse:# Nothing to do if torch<1.6.0@contextmanagerdiff --git a/funasr/models/ct_transformer/model.py b/funasr/models/ct_transformer/model.pyindex 1ee0bf566..c7beb1ba0 100644--- a/funasr/models/ct_transformer/model.py+++ b/funasr/models/ct_transformer/model.py@@ -23,7 +23,7 @@ try:except:passif LooseVersion(torch.__version__) >= LooseVersion("1.6.0"):- from torch.cuda.amp import autocast+ from funasr.utils.amp import autocastelse:# Nothing to do if torch<1.6.0@contextmanagerdiff --git a/funasr/models/ct_transformer_streaming/model.py b/funasr/models/ct_transformer_streaming/model.pyindex c646e2484..06add5f2e 100644--- a/funasr/models/ct_transformer_streaming/model.py+++ b/funasr/models/ct_transformer_streaming/model.py@@ -16,7 +16,7 @@ from funasr.models.ct_transformer.utils import split_to_mini_sentence, split_worif LooseVersion(torch.__version__) >= LooseVersion("1.6.0"):- from torch.cuda.amp import autocast+ from funasr.utils.amp import autocastelse:# Nothing to do if torch<1.6.0@contextmanagerdiff --git a/funasr/models/data2vec/data2vec.py b/funasr/models/data2vec/data2vec.pyindex c7386e35d..0ae788c6c 100644--- a/funasr/models/data2vec/data2vec.py+++ b/funasr/models/data2vec/data2vec.py@@ -22,7 +22,7 @@ from funasr.frontends.abs_frontend import AbsFrontendfrom funasr.train_utils.device_funcs import force_gatherableif LooseVersion(torch.__version__) >= LooseVersion("1.6.0"):- from torch.cuda.amp import autocast+ from funasr.utils.amp import autocastelse:# Nothing to do if torch<1.6.0@contextmanagerdiff --git a/funasr/models/e_paraformer/model.py b/funasr/models/e_paraformer/model.pyindex d59e1f887..8699032f7 100644--- a/funasr/models/e_paraformer/model.py+++ b/funasr/models/e_paraformer/model.py@@ -8,7 +8,7 @@ import timeimport copyimport torchimport logging-from torch.cuda.amp import autocast+from funasr.utils.amp import autocastfrom typing import Union, Dict, List, Tuple, Optionalfrom funasr.register import tablesdiff --git a/funasr/models/e_paraformer/pif_predictor.py b/funasr/models/e_paraformer/pif_predictor.pyindex 6cd666486..6a7307753 100644--- a/funasr/models/e_paraformer/pif_predictor.py+++ b/funasr/models/e_paraformer/pif_predictor.py@@ -11,7 +11,7 @@ import numpy as npfrom funasr.register import tablesfrom funasr.train_utils.device_funcs import to_devicefrom funasr.models.transformer.utils.nets_utils import make_pad_mask-from torch.cuda.amp import autocast+from funasr.utils.amp import autocast@tables.register("predictor_classes", "PifPredictor")diff --git a/funasr/models/emotion2vec/model.py b/funasr/models/emotion2vec/model.pyindex 3ecace8eb..1166e58d5 100644--- a/funasr/models/emotion2vec/model.py+++ b/funasr/models/emotion2vec/model.py@@ -23,7 +23,7 @@ from funasr.utils.load_utils import load_audio_text_image_videologger = logging.getLogger(__name__)if LooseVersion(torch.__version__) >= LooseVersion("1.6.0"):- from torch.cuda.amp import autocast+ from funasr.utils.amp import autocastelse:# Nothing to do if torch<1.6.0@contextmanagerdiff --git a/funasr/models/fsmn_kws/model.py b/funasr/models/fsmn_kws/model.pyindex fa6f5b712..03114bd3c 100644--- a/funasr/models/fsmn_kws/model.py+++ b/funasr/models/fsmn_kws/model.py@@ -6,7 +6,7 @@import timeimport torchimport logging-from torch.cuda.amp import autocast+from funasr.utils.amp import autocastfrom typing import Union, Dict, List, Tuple, Optionalfrom funasr.register import tablesdiff --git a/funasr/models/fsmn_kws_mt/model.py b/funasr/models/fsmn_kws_mt/model.pyindex 3ea002c34..1d7db97e1 100644--- a/funasr/models/fsmn_kws_mt/model.py+++ b/funasr/models/fsmn_kws_mt/model.py@@ -6,7 +6,7 @@import timeimport torchimport logging-from torch.cuda.amp import autocast+from funasr.utils.amp import autocastfrom typing import Union, Dict, List, Tuple, Optionalfrom funasr.register import tablesdiff --git a/funasr/models/lcbnet/model.py b/funasr/models/lcbnet/model.pyindex cc9aa4e2f..1516090e3 100644--- a/funasr/models/lcbnet/model.py+++ b/funasr/models/lcbnet/model.py@@ -9,7 +9,7 @@ from typing import Union, Dict, List, Tuple, Optionalimport timeimport torchimport torch.nn as nn-from torch.cuda.amp import autocast+from funasr.utils.amp import autocastfrom funasr.losses.label_smoothing_loss import LabelSmoothingLossfrom funasr.models.ctc.ctc import CTCdiff --git a/funasr/models/llm_asr/model.py b/funasr/models/llm_asr/model.pyindex 798ee103e..5a1ccd5f4 100644--- a/funasr/models/llm_asr/model.py+++ b/funasr/models/llm_asr/model.py@@ -5,7 +5,7 @@ import timeimport torchimport torch.nn as nnimport torch.nn.functional as F-from torch.cuda.amp import autocast+from funasr.utils.amp import autocastimport refrom funasr.models.scama.utils import sequence_maskfrom funasr.losses.label_smoothing_loss import LabelSmoothingLoss@@ -583,7 +583,7 @@ class LLMASR2(nn.Module):batch_size, frames, _ = speech.shape- with torch.cuda.amp.autocast(enabled=False):+ with autocast(enabled=False):# audio encoderencoder_out, encoder_out_lens = self.encode(speech, speech_lengths)@@ -618,7 +618,7 @@ class LLMASR2(nn.Module):batch_idx, :min_len, :]- with torch.cuda.amp.autocast(+ with autocast(enabled=True if self.llm_dtype != "fp32" else False, dtype=dtype_map[self.llm_dtype]):labels_ids[labels_ids == -1] = -100@@ -883,7 +883,7 @@ class LLMASR2(nn.Module):llm_dtype = "fp16" if kwargs.get("fp16", False) else llm_dtypellm_dtype = "bf16" if kwargs.get("bf16", False) else llm_dtype- with torch.cuda.amp.autocast(+ with autocast(enabled=True if llm_dtype != "fp32" else False, dtype=dtype_map[llm_dtype]):label = contents["assistant"][0]@@ -1159,7 +1159,7 @@ class LLMASR4(nn.Module):batch_size_speech, frames, _ = speech.shapebatch_size, token_num = input_ids.shape- with torch.cuda.amp.autocast(enabled=False):+ with autocast(enabled=False):# audio encoderencoder_out, encoder_out_lens = self.encode(speech, speech_lengths)@@ -1203,7 +1203,7 @@ class LLMASR4(nn.Module):speech_idx += 1- with torch.cuda.amp.autocast(+ with autocast(enabled=True if self.llm_dtype != "fp32" else False, dtype=dtype_map[self.llm_dtype]):labels_ids[labels_ids == -1] = -100@@ -1551,7 +1551,7 @@ class LLMASR4(nn.Module):llm_dtype = "fp16" if kwargs.get("fp16", False) else llm_dtypellm_dtype = "bf16" if kwargs.get("bf16", False) else llm_dtype- with torch.cuda.amp.autocast(+ with autocast(enabled=True if llm_dtype != "fp32" else False, dtype=dtype_map[llm_dtype]):label = contents["assistant"][-1]diff --git a/funasr/models/llm_asr_nar/model.py b/funasr/models/llm_asr_nar/model.pyindex b0d4f10d3..d0504f5f6 100644--- a/funasr/models/llm_asr_nar/model.py+++ b/funasr/models/llm_asr_nar/model.py@@ -5,7 +5,7 @@ import timeimport torchimport torch.nn as nnimport torch.nn.functional as F-from torch.cuda.amp import autocast+from funasr.utils.amp import autocastfrom funasr.models.scama.utils import sequence_maskfrom funasr.losses.label_smoothing_loss import LabelSmoothingLossdiff --git a/funasr/models/mfcca/e2e_asr_mfcca.py b/funasr/models/mfcca/e2e_asr_mfcca.pyindex f6cbb089f..514432bb3 100644--- a/funasr/models/mfcca/e2e_asr_mfcca.py+++ b/funasr/models/mfcca/e2e_asr_mfcca.py@@ -25,7 +25,7 @@ from funasr.train_utils.device_funcs import force_gatherablefrom funasr.models.base_model import FunASRModelif LooseVersion(torch.__version__) >= LooseVersion("1.6.0"):- from torch.cuda.amp import autocast+ from funasr.utils.amp import autocastelse:# Nothing to do if torch<1.6.0@contextmanagerdiff --git a/funasr/models/monotonic_aligner/model.py b/funasr/models/monotonic_aligner/model.pyindex d68e469d1..970ed8d6b 100644--- a/funasr/models/monotonic_aligner/model.py+++ b/funasr/models/monotonic_aligner/model.py@@ -6,7 +6,7 @@import timeimport copyimport torch-from torch.cuda.amp import autocast+from funasr.utils.amp import autocastfrom typing import Union, Dict, List, Tuple, Optionalfrom funasr.register import tablesdiff --git a/funasr/models/paraformer/cif_predictor.py b/funasr/models/paraformer/cif_predictor.pyindex 93645a8ff..927ae2195 100644--- a/funasr/models/paraformer/cif_predictor.py+++ b/funasr/models/paraformer/cif_predictor.py@@ -10,7 +10,7 @@ import numpy as npfrom funasr.register import tablesfrom funasr.train_utils.device_funcs import to_devicefrom funasr.models.transformer.utils.nets_utils import make_pad_mask-from torch.cuda.amp import autocast+from funasr.utils.amp import autocast@tables.register("predictor_classes", "CifPredictor")diff --git a/funasr/models/paraformer/model.py b/funasr/models/paraformer/model.pyindex 5060d8f20..3f5b01517 100644--- a/funasr/models/paraformer/model.py+++ b/funasr/models/paraformer/model.py@@ -7,7 +7,7 @@ import timeimport copyimport torchimport logging-from torch.cuda.amp import autocast+from funasr.utils.amp import autocastfrom typing import Union, Dict, List, Tuple, Optionalfrom funasr.register import tablesdiff --git a/funasr/models/paraformer_streaming/model.py b/funasr/models/paraformer_streaming/model.pyindex 287ffdfae..69826befa 100644--- a/funasr/models/paraformer_streaming/model.py+++ b/funasr/models/paraformer_streaming/model.py@@ -26,7 +26,7 @@ from funasr.utils.load_utils import load_audio_text_image_video, extract_fbankif LooseVersion(torch.__version__) >= LooseVersion("1.6.0"):- from torch.cuda.amp import autocast+ from funasr.utils.amp import autocastelse:# Nothing to do if torch<1.6.0@contextmanagerdiff --git a/funasr/models/paraformer_v2_community/model.py b/funasr/models/paraformer_v2_community/model.pyindex eb11ede5b..3a0dae5ff 100644--- a/funasr/models/paraformer_v2_community/model.py+++ b/funasr/models/paraformer_v2_community/model.py@@ -7,7 +7,7 @@ import timeimport copyimport torchimport logging-from torch.cuda.amp import autocast+from funasr.utils.amp import autocastfrom typing import Union, Dict, List, Tuple, Optionalfrom funasr.register import tablesdiff --git a/funasr/models/sa_asr/e2e_sa_asr.py b/funasr/models/sa_asr/e2e_sa_asr.pyindex 6d9422d2f..8b0a4fc61 100644--- a/funasr/models/sa_asr/e2e_sa_asr.py+++ b/funasr/models/sa_asr/e2e_sa_asr.py@@ -29,7 +29,7 @@ from funasr.train_utils.device_funcs import force_gatherablefrom funasr.models.base_model import FunASRModelif LooseVersion(torch.__version__) >= LooseVersion("1.6.0"):- from torch.cuda.amp import autocast+ from funasr.utils.amp import autocastelse:# Nothing to do if torch<1.6.0@contextmanagerdiff --git a/funasr/models/sanm_kws/model.py b/funasr/models/sanm_kws/model.pyindex 35a01837f..ecd48332e 100644--- a/funasr/models/sanm_kws/model.py+++ b/funasr/models/sanm_kws/model.py@@ -6,7 +6,7 @@import timeimport torchimport logging-from torch.cuda.amp import autocast+from funasr.utils.amp import autocastfrom typing import Union, Dict, List, Tuple, Optionalfrom funasr.register import tablesdiff --git a/funasr/models/sanm_kws_streaming/model.py b/funasr/models/sanm_kws_streaming/model.pyindex 3228a0643..43486d97f 100644--- a/funasr/models/sanm_kws_streaming/model.py+++ b/funasr/models/sanm_kws_streaming/model.py@@ -26,7 +26,7 @@ from funasr.utils.load_utils import load_audio_text_image_video, extract_fbankif LooseVersion(torch.__version__) >= LooseVersion("1.6.0"):- from torch.cuda.amp import autocast+ from funasr.utils.amp import autocastelse:# Nothing to do if torch<1.6.0@contextmanagerdiff --git a/funasr/models/scama/model.py b/funasr/models/scama/model.pyindex 76887e5b9..b1c6409a8 100644--- a/funasr/models/scama/model.py+++ b/funasr/models/scama/model.py@@ -28,7 +28,7 @@ from funasr.utils.load_utils import load_audio_text_image_video, extract_fbankfrom funasr.models.scama.utils import sequence_maskif LooseVersion(torch.__version__) >= LooseVersion("1.6.0"):- from torch.cuda.amp import autocast+ from funasr.utils.amp import autocastelse:# Nothing to do if torch<1.6.0@contextmanagerdiff --git a/funasr/models/seaco_paraformer/model.py b/funasr/models/seaco_paraformer/model.pyindex 720aca087..efa4745a3 100644--- a/funasr/models/seaco_paraformer/model.py+++ b/funasr/models/seaco_paraformer/model.py@@ -33,7 +33,7 @@ from funasr.utils.load_utils import load_audio_text_image_video, extract_fbankif LooseVersion(torch.__version__) >= LooseVersion("1.6.0"):- from torch.cuda.amp import autocast+ from funasr.utils.amp import autocastelse:# Nothing to do if torch<1.6.0@contextmanagerdiff --git a/funasr/models/sense_voice/model.py b/funasr/models/sense_voice/model.pyindex 3ec936c54..c10c663d9 100644--- a/funasr/models/sense_voice/model.py+++ b/funasr/models/sense_voice/model.py@@ -6,7 +6,7 @@ import torchimport torch.nn.functional as Ffrom torch import Tensorfrom torch import nn-from torch.cuda.amp import autocast+from funasr.utils.amp import autocastfrom funasr.metrics.compute_acc import compute_accuracy, th_accuracyfrom funasr.losses.label_smoothing_loss import LabelSmoothingLossfrom funasr.train_utils.device_funcs import force_gatherablediff --git a/funasr/models/sond/e2e_diar_sond.py b/funasr/models/sond/e2e_diar_sond.pyindex 44cf6d361..db61dc625 100644--- a/funasr/models/sond/e2e_diar_sond.py+++ b/funasr/models/sond/e2e_diar_sond.py@@ -29,7 +29,7 @@ from funasr.utils.misc import int2vecfrom funasr.utils.hinter import hint_onceif LooseVersion(torch.__version__) >= LooseVersion("1.6.0"):- from torch.cuda.amp import autocast+ from funasr.utils.amp import autocastelse:# Nothing to do if torch<1.6.0@contextmanagerdiff --git a/funasr/models/transducer/model.py b/funasr/models/transducer/model.pyindex 354413161..99fee01f5 100644--- a/funasr/models/transducer/model.py+++ b/funasr/models/transducer/model.py@@ -23,7 +23,7 @@ from funasr.models.transducer.beam_search_transducer import BeamSearchTransducerif LooseVersion(torch.__version__) >= LooseVersion("1.6.0"):- from torch.cuda.amp import autocast+ from funasr.utils.amp import autocastelse:# Nothing to do if torch<1.6.0@contextmanagerdiff --git a/funasr/models/transformer/model.py b/funasr/models/transformer/model.pyindex b36864bd8..2cd968773 100644--- a/funasr/models/transformer/model.py+++ b/funasr/models/transformer/model.py@@ -4,7 +4,7 @@ from typing import Union, Dict, List, Tuple, Optionalimport timeimport torchimport torch.nn as nn-from torch.cuda.amp import autocast+from funasr.utils.amp import autocastfrom funasr.losses.label_smoothing_loss import LabelSmoothingLossfrom funasr.models.ctc.ctc import CTCdiff --git a/funasr/models/uniasr/model.py b/funasr/models/uniasr/model.pyindex b6a767abb..95eea5256 100644--- a/funasr/models/uniasr/model.py+++ b/funasr/models/uniasr/model.py@@ -6,7 +6,7 @@import timeimport torchimport logging-from torch.cuda.amp import autocast+from funasr.utils.amp import autocastfrom typing import Union, Dict, List, Tuple, Optionalfrom funasr.register import tablesdiff --git a/funasr/models/whisper_lid/model.py b/funasr/models/whisper_lid/model.pyindex e56c01c69..6bd71f1e5 100644--- a/funasr/models/whisper_lid/model.py+++ b/funasr/models/whisper_lid/model.py@@ -5,7 +5,7 @@ import timeimport torchimport numpy as npimport torch.nn as nn-from torch.cuda.amp import autocast+from funasr.utils.amp import autocastfrom funasr.losses.label_smoothing_loss import LabelSmoothingLossfrom funasr.models.ctc.ctc import CTCdiff --git a/funasr/models/xvector/e2e_sv.py b/funasr/models/xvector/e2e_sv.pyindex c6062b59c..34051903e 100644--- a/funasr/models/xvector/e2e_sv.py+++ b/funasr/models/xvector/e2e_sv.py@@ -31,7 +31,7 @@ from funasr.train_utils.device_funcs import force_gatherablefrom funasr.models.base_model import FunASRModelif LooseVersion(torch.__version__) >= LooseVersion("1.6.0"):- from torch.cuda.amp import autocast+ from funasr.utils.amp import autocastelse:# Nothing to do if torch<1.6.0@contextmanagerdiff --git a/funasr/train_utils/trainer.py b/funasr/train_utils/trainer.pyindex 15654cd47..b0493c03f 100644--- a/funasr/train_utils/trainer.py+++ b/funasr/train_utils/trainer.py@@ -6,7 +6,7 @@ import loggingfrom tqdm import tqdmfrom datetime import datetimeimport torch.distributed as dist-from torch.cuda.amp import autocast, GradScaler+from funasr.utils.amp import autocast, GradScalerfrom contextlib import nullcontext, contextmanagerfrom pathlib import Pathdiff --git a/funasr/train_utils/trainer_ds.py b/funasr/train_utils/trainer_ds.pyindex 83cd19a67..9bd7ca31e 100644--- a/funasr/train_utils/trainer_ds.py+++ b/funasr/train_utils/trainer_ds.py@@ -6,7 +6,7 @@ import loggingfrom tqdm import tqdmfrom datetime import datetimeimport torch.distributed as dist-from torch.cuda.amp import autocast, GradScaler+from funasr.utils.amp import autocast, GradScalerfrom contextlib import nullcontext, contextmanagerfrom pathlib import Pathfrom torch.nn.parallel import DistributedDataParallel as DDP@@ -32,7 +32,7 @@ def maybe_autocast(dtype=None, use_deepspeed=False):use_deepspeed: TODO."""if use_deepspeed:- with torch.cuda.amp.autocast(enabled=True, dtype=dtype, cache_enabled=False):+ with autocast(enabled=True, dtype=dtype, cache_enabled=False):yieldelse:if dtype == torch.float16 or dtype == torch.bfloat16:diff --git a/funasr/utils/amp.py b/funasr/utils/amp.pynew file mode 100644index 000000000..2469d12cc--- /dev/null+++ b/funasr/utils/amp.py@@ -0,0 +1,39 @@+"""Compatibility shim for torch.amp (autocast / GradScaler).++``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, and the replacement device-agnostic+APIs live in ``torch.amp`` (``autocast('cuda', ...)`` since 2.0,+``GradScaler('cuda', ...)`` since 2.3).++This module re-exports the non-deprecated ``torch.amp`` names when the+installed torch provides them, and falls back to ``torch.cuda.amp`` on+older torch versions. Import it instead of ``torch.cuda.amp`` directly:++ from funasr.utils.amp import autocast, GradScaler++Note: ``torch.amp.autocast`` requires a ``device_type`` argument (e.g.+``'cuda'``) that the deprecated ``torch.cuda.amp.autocast`` did not, so+the shim supplies ``device_type="cuda"`` by default for callers that use+the old signature (``with autocast(enabled=..., dtype=...)``).+"""++import torch++torch_amp = getattr(torch, "amp", None)+if torch_amp is not None and hasattr(torch_amp, "autocast") and hasattr(+ torch_amp, "GradScaler"+):+ from torch.amp import autocast as _amp_autocast+ from torch.amp import GradScaler++ def autocast(enabled=True, dtype=None, cache_enabled=True):+ """torch.amp.autocast with the legacy CUDA autocast signature."""+ return _amp_autocast(+ "cuda", dtype=dtype, enabled=enabled, cache_enabled=cache_enabled+ )++else:+ from torch.cuda.amp import autocast, GradScaler++__all__ = ["autocast", "GradScaler"]diff --git a/tests/test_amp_compat.py b/tests/test_amp_compat.pynew file mode 100644index 000000000..63aadfd64--- /dev/null+++ b/tests/test_amp_compat.py@@ -0,0 +1,41 @@+import importlib.util+import sys+import types+from pathlib import Path++import pytest+import torch++from funasr.utils.amp import autocast++[email protected]("args", [(False,), (False, torch.float16, False)])+def test_autocast_preserves_legacy_positional_arguments(args):+ with autocast(*args):+ pass+++def test_amp_import_falls_back_when_torch_amp_is_missing(monkeypatch):+ legacy_autocast = object()+ legacy_grad_scaler = object()+ torch_stub = types.ModuleType("torch")+ torch_stub.__path__ = []+ cuda_stub = types.ModuleType("torch.cuda")+ cuda_stub.__path__ = []+ cuda_amp_stub = types.ModuleType("torch.cuda.amp")+ cuda_amp_stub.autocast = legacy_autocast+ cuda_amp_stub.GradScaler = legacy_grad_scaler+ torch_stub.cuda = cuda_stub+ cuda_stub.amp = cuda_amp_stub++ monkeypatch.setitem(sys.modules, "torch", torch_stub)+ monkeypatch.setitem(sys.modules, "torch.cuda", cuda_stub)+ monkeypatch.setitem(sys.modules, "torch.cuda.amp", cuda_amp_stub)++ module_path = Path(__file__).parents[1] / "funasr" / "utils" / "amp.py"+ spec = importlib.util.spec_from_file_location("funasr_amp_legacy_test", module_path)+ module = importlib.util.module_from_spec(spec)+ spec.loader.exec_module(module)++ assert module.autocast is legacy_autocast+ assert module.GradScaler is legacy_grad_scaler