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
[Export] support gpu inference
wanchen.swccfc4d4020930
diff --git a/funasr/export/export_model.py b/funasr/export/export_model.pyindex b1161cbf8..d3d119c78 100644--- a/funasr/export/export_model.py+++ b/funasr/export/export_model.py@@ -19,6 +19,7 @@ class ModelExport:self,cache_dir: Union[Path, str] = None,onnx: bool = True,+ device: str = "cpu",quant: bool = True,fallback_num: int = 0,audio_in: str = None,@@ -36,6 +37,7 @@ class ModelExport:)print("output dir: {}".format(self.cache_dir))self.onnx = onnx+ self.device = deviceself.quant = quantself.fallback_num = fallback_numself.frontend = None@@ -112,6 +114,10 @@ class ModelExport:else:dummy_input = model.get_dummy_inputs()+ if self.device == 'cuda':+ model = model.cuda()+ dummy_input = tuple([i.cuda() for i in dummy_input])+# model_script = torch.jit.script(model)model_script = torch.jit.trace(model, dummy_input)model_script.save(os.path.join(path, f'{model.model_name}.torchscripts'))@@ -234,6 +240,7 @@ if __name__ == '__main__':parser.add_argument('--model-name', type=str, required=True)parser.add_argument('--export-dir', type=str, required=True)parser.add_argument('--type', type=str, default='onnx', help='["onnx", "torch"]')+ parser.add_argument('--device', type=str, default='cpu', help='["cpu", "cuda"]')parser.add_argument('--quantize', type=str2bool, default=False, help='export quantized model')parser.add_argument('--fallback-num', type=int, default=0, help='amp fallback number')parser.add_argument('--audio_in', type=str, default=None, help='["wav", "wav.scp"]')@@ -243,6 +250,7 @@ if __name__ == '__main__':export_model = ModelExport(cache_dir=args.export_dir,onnx=args.type == 'onnx',+ device=args.device,quant=args.quantize,fallback_num=args.fallback_num,audio_in=args.audio_in,diff --git a/funasr/runtime/python/libtorch/demo.py b/funasr/runtime/python/libtorch/demo.pyindex 58285a7a0..b2b65acf0 100644--- a/funasr/runtime/python/libtorch/demo.py+++ b/funasr/runtime/python/libtorch/demo.py@@ -2,9 +2,9 @@from funasr_torch import Paraformermodel_dir = "/nfs/zhifu.gzf/export/damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch"-model = Paraformer(model_dir, batch_size=1)+model = Paraformer(model_dir, batch_size=1, device_id=0)wav_path = ['/nfs/zhifu.gzf/export/damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch/example/asr_example.wav']result = model(wav_path)-print(result)\ No newline at end of file+print(result)diff --git a/funasr/runtime/python/libtorch/funasr_torch/paraformer_bin.py b/funasr/runtime/python/libtorch/funasr_torch/paraformer_bin.pyindex 3c0606d6c..f9232af4c 100644--- a/funasr/runtime/python/libtorch/funasr_torch/paraformer_bin.py+++ b/funasr/runtime/python/libtorch/funasr_torch/paraformer_bin.py@@ -58,6 +58,9 @@ class Paraformer():end_idx = min(waveform_nums, beg_idx + self.batch_size)feats, feats_len = self.extract_feat(waveform_list[beg_idx:end_idx])try:+ if int(device_id) != -1:+ feats = feats.cuda()+ feats_len = feats_len.cuda()outputs = self.ort_infer(feats, feats_len)am_scores, valid_token_lens = outputs[0], outputs[1]if len(outputs) == 4:diff --git a/funasr/runtime/python/onnxruntime/demo.py b/funasr/runtime/python/onnxruntime/demo.pyindex 248d2e1df..c938db438 100644--- a/funasr/runtime/python/onnxruntime/demo.py+++ b/funasr/runtime/python/onnxruntime/demo.py@@ -7,9 +7,9 @@ model_dir = "/Users/shixian/code/funasr/export/damo/speech_paraformer-tiny-comma# if you use paraformer-tiny-commandword_asr_nat-zh-cn-16k-vocab544-pytorch, you should set pred_bias=0# plot_timestamp_to works only when using speech_paraformer-large-vad-punc_asr_nat-zh-cn-16k-common-vocab8404-pytorch-model = Paraformer(model_dir, batch_size=2, plot_timestamp_to="./", pred_bias=0)+model = Paraformer(model_dir, batch_size=2, plot_timestamp_to="./", pred_bias=0, device_id=0)wav_path = "/Users/shixian/code/funasr/export/damo/speech_paraformer-tiny-commandword_asr_nat-zh-cn-16k-vocab544-pytorch/example/asr_example.wav"result = model(wav_path)-print(result)\ No newline at end of file+print(result)