MCPcopy Index your code
hub / github.com/modelscope/FunASR / export

Function export

funasr/utils/export_utils.py:10–63  ·  view source on GitHub ↗

Export. Args: model: Model instance or model name. data_in: Input data (audio samples, file paths, or text). quantize: TODO. opset_version: TODO. type: TODO. **kwargs: Additional keyword arguments.

(
    model, data_in=None, quantize: bool = False, opset_version: int = 14, type="onnx", **kwargs
)

Source from the content-addressed store, hash-verified

8
9
10def export(
11 model, data_in=None, quantize: bool = False, opset_version: int = 14, type="onnx", **kwargs
12):
13 """Export.
14
15 Args:
16 model: Model instance or model name.
17 data_in: Input data (audio samples, file paths, or text).
18 quantize: TODO.
19 opset_version: TODO.
20 type: TODO.
21 **kwargs: Additional keyword arguments.
22 """
23 model_scripts = model.export(**kwargs)
24 export_dir = kwargs.get("output_dir", os.path.dirname(kwargs.get("init_param")))
25 os.makedirs(export_dir, exist_ok=True)
26
27 if not isinstance(model_scripts, (list, tuple)):
28 model_scripts = (model_scripts,)
29 for m in model_scripts:
30 m.eval()
31 if type == "onnx":
32 _onnx(
33 m,
34 data_in=data_in,
35 quantize=quantize,
36 opset_version=opset_version,
37 export_dir=export_dir,
38 **kwargs,
39 )
40 elif type == "torchscript":
41 device = "cuda" if torch.cuda.is_available() else "xpu" if torch.xpu.is_available() else "mps" if torch.backends.mps.is_available() else "cpu"
42 print("Exporting torchscripts on device {}".format(device))
43 _torchscripts(m, path=export_dir, device=device)
44 elif type == "bladedisc":
45 assert (
46 torch.cuda.is_available() or torch.xpu.is_available() or torch.backends.mps.is_available()
47 ), "Currently bladedisc optimization for FunASR only supports GPU"
48 # bladedisc only optimizes encoder/decoder modules
49 if hasattr(m, "encoder") and hasattr(m, "decoder"):
50 _bladedisc_opt_for_encdec(m, path=export_dir, enable_fp16=True)
51 else:
52 print(f"export_dir: {export_dir}")
53 _torchscripts(m, path=export_dir, device="cuda")
54
55 elif type == "onnx_fp16":
56 assert (
57 torch.cuda.is_available() or torch.xpu.is_available() or torch.backends.mps.is_available()
58 ), "Currently onnx_fp16 optimization for FunASR only supports GPU"
59
60 if hasattr(m, "encoder") and hasattr(m, "decoder"):
61 _onnx_opt_for_encdec(m, path=export_dir, enable_fp16=True)
62
63 return export_dir
64
65
66def _onnx(

Callers

nothing calls this directly

Calls 6

_onnxFunction · 0.85
_torchscriptsFunction · 0.85
_onnx_opt_for_encdecFunction · 0.85
exportMethod · 0.45
evalMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…