Dev gzf (#1467)
* qwenaudio qwenaudiochat
* qwenaudio qwenaudiochat
* whisper
* whisper
* llm
* llm
* llm
* llm
* llm
* llm
* llm
* llm
* export onnx
* export onnx
* export onnx
* dingding
* dingding
* llm
* doc
* onnx
| | |
| | | |
| | | ### Command-line usage |
| | | ```shell |
| | | funasr-export ++model=paraformer ++quantize=false |
| | | funasr-export ++model=paraformer ++quantize=false ++device=cpu |
| | | ``` |
| | | |
| | | ### python |
| | | ### Python |
| | | ```python |
| | | from funasr import AutoModel |
| | | |
| | | model = AutoModel(model="paraformer") |
| | | model = AutoModel(model="paraformer", device="cpu") |
| | | |
| | | res = model.export(quantize=False) |
| | | ``` |
| | |
| | | from funasr import AutoModel |
| | | |
| | | model = AutoModel(model="damo/speech_paraformer-large-vad-punc_asr_nat-zh-cn-16k-common-vocab8404-pytorch", |
| | | model_revision="v2.0.4") |
| | | model_revision="v2.0.4", device="cpu") |
| | | |
| | | res = model.export(type="onnx", quantize=False) |
| | | print(res) |
| | |
| | | # method2, inference from local path |
| | | from funasr import AutoModel |
| | | |
| | | model = AutoModel(model="/Users/zhifu/.cache/modelscope/hub/iic/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch") |
| | | model = AutoModel(model="/Users/zhifu/.cache/modelscope/hub/iic/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch", device="cpu") |
| | | |
| | | res = model.export(type="onnx", quantize=False) |
| | | print(res) |
| | |
| | | ++model=${model} \ |
| | | ++model_revision=${model_revision} \ |
| | | ++type="onnx" \ |
| | | ++quantize=false |
| | | ++quantize=false \ |
| | | ++device="cpu" |
| | | |
| | | # method2, inference from local path |
| | | model="/Users/zhifu/.cache/modelscope/hub/iic/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch" |
| | |
| | | python -m funasr.bin.export \ |
| | | ++model=${model} \ |
| | | ++type="onnx" \ |
| | | ++quantize=false |
| | | ++quantize=false \ |
| | | ++device="cpu" |
| | |
| | | ++model=${model} \ |
| | | ++model_revision=${model_revision} \ |
| | | ++type="onnx" \ |
| | | ++quantize=false |
| | | ++quantize=false \ |
| | | ++device="cpu" |
| | | |
| | | |
| | | # method2, inference from local path |
| | |
| | | python -m funasr.bin.export \ |
| | | ++model=${model} \ |
| | | ++type="onnx" \ |
| | | ++quantize=false |
| | | ++quantize=false \ |
| | | ++device="cpu" |
| | |
| | | kwargs["batch_size"] = 1 |
| | | kwargs["device"] = device |
| | | |
| | | if kwargs.get("ncpu", None): |
| | | if kwargs.get("ncpu", 4): |
| | | torch.set_num_threads(kwargs.get("ncpu")) |
| | | |
| | | # build tokenizer |
| | |
| | | calib_num: int = 100, |
| | | opset_version: int = 14, |
| | | **cfg): |
| | | os.environ['EXPORTING_MODEL'] = 'TRUE' |
| | | |
| | | device = cfg.get("device", "cpu") |
| | | model = self.model.to(device=device) |
| | | kwargs = self.kwargs |
| | | deep_update(kwargs, cfg) |
| | | kwargs["device"] = device |
| | | del kwargs["model"] |
| | | model = self.model |
| | | model.eval() |
| | | |
| | | batch_size = 1 |
| | |
| | | if kwargs.get("debug", False): |
| | | import pdb; pdb.set_trace() |
| | | |
| | | |
| | | if "device" not in kwargs: |
| | | kwargs["device"] = "cpu" |
| | | model = AutoModel(**kwargs) |
| | | |
| | | res = model.export(input=kwargs.get("input", None), |
| | |
| | | from funasr.utils.timestamp_tools import ts_prediction_lfr6_standard |
| | | from funasr.models.transformer.utils.nets_utils import make_pad_mask, pad_list |
| | | from funasr.utils.load_utils import load_audio_text_image_video, extract_fbank |
| | | |
| | | from funasr.train_utils.device_funcs import to_device |
| | | |
| | | if LooseVersion(torch.__version__) >= LooseVersion("1.6.0"): |
| | | from torch.cuda.amp import autocast |
| | |
| | | max_seq_len=512, |
| | | **kwargs, |
| | | ): |
| | | self.device = kwargs.get("device") |
| | | is_onnx = kwargs.get("type", "onnx") == "onnx" |
| | | encoder_class = tables.encoder_classes.get(kwargs["encoder"] + "Export") |
| | | self.encoder = encoder_class(self.encoder, onnx=is_onnx) |
| | |
| | | |
| | | return self |
| | | |
| | | def _export_forward( |
| | | def export_forward( |
| | | self, |
| | | speech: torch.Tensor, |
| | | speech_lengths: torch.Tensor, |
| | | ): |
| | | # a. To device |
| | | batch = {"speech": speech, "speech_lengths": speech_lengths} |
| | | # batch = to_device(batch, device=self.device) |
| | | batch = to_device(batch, device=self.device) |
| | | |
| | | enc, enc_len = self.encoder(**batch) |
| | | mask = self.make_pad_mask(enc_len)[:, None, :] |
| | |
| | | from funasr.models.transformer.utils.nets_utils import make_pad_mask |
| | | from funasr.models.ct_transformer.utils import split_to_mini_sentence, split_words |
| | | |
| | | |
| | | if LooseVersion(torch.__version__) >= LooseVersion("1.6.0"): |
| | | from torch.cuda.amp import autocast |
| | | else: |
| | |
| | | |
| | | return self |
| | | |
| | | def _export_forward(self, inputs: torch.Tensor, text_lengths: torch.Tensor): |
| | | def export_forward(self, inputs: torch.Tensor, text_lengths: torch.Tensor): |
| | | """Compute loss value from buffer sequences. |
| | | |
| | | Args: |
| | |
| | | |
| | | return self |
| | | |
| | | def _export_forward(self, inputs: torch.Tensor, |
| | | def export_forward(self, inputs: torch.Tensor, |
| | | text_lengths: torch.Tensor, |
| | | vad_indexes: torch.Tensor, |
| | | sub_masks: torch.Tensor, |
| | |
| | | |
| | | return self |
| | | |
| | | def _export_forward(self, feats: torch.Tensor, *args, **kwargs): |
| | | def export_forward(self, feats: torch.Tensor, *args, **kwargs): |
| | | |
| | | scores, out_caches = self.encoder(feats, *args) |
| | | |
| | |
| | | from funasr.models.transformer.utils.add_sos_eos import add_sos_eos |
| | | from funasr.models.transformer.utils.nets_utils import make_pad_mask, pad_list |
| | | from funasr.utils.load_utils import load_audio_text_image_video, extract_fbank |
| | | |
| | | from funasr.train_utils.device_funcs import to_device |
| | | |
| | | @tables.register("model_classes", "Paraformer") |
| | | class Paraformer(torch.nn.Module): |
| | |
| | | max_seq_len=512, |
| | | **kwargs, |
| | | ): |
| | | |
| | | self.device = kwargs.get("device") |
| | | is_onnx = kwargs.get("type", "onnx") == "onnx" |
| | | encoder_class = tables.encoder_classes.get(kwargs["encoder"]+"Export") |
| | | self.encoder = encoder_class(self.encoder, onnx=is_onnx) |
| | |
| | | |
| | | return self |
| | | |
| | | def _export_forward( |
| | | def export_forward( |
| | | self, |
| | | speech: torch.Tensor, |
| | | speech_lengths: torch.Tensor, |
| | | ): |
| | | # a. To device |
| | | batch = {"speech": speech, "speech_lengths": speech_lengths} |
| | | # batch = to_device(batch, device=self.device) |
| | | batch = to_device(batch, device=self.device) |
| | | |
| | | enc, enc_len = self.encoder(**batch) |
| | | mask = self.make_pad_mask(enc_len)[:, None, :] |
| | |
| | | max_seq_len=512, |
| | | **kwargs, |
| | | ): |
| | | |
| | | self.device = kwargs.get("device") |
| | | is_onnx = kwargs.get("type", "onnx") == "onnx" |
| | | encoder_class = tables.encoder_classes.get(kwargs["encoder"] + "Export") |
| | | self.encoder = encoder_class(self.encoder, onnx=is_onnx) |
| | |
| | | |
| | | return encoder_model, decoder_model |
| | | |
| | | def _export_encoder_forward( |
| | | def export_encoder_forward( |
| | | self, |
| | | speech: torch.Tensor, |
| | | speech_lengths: torch.Tensor, |
| | |
| | | def export_encoder_name(self): |
| | | return "model.onnx" |
| | | |
| | | def _export_decoder_forward( |
| | | def export_decoder_forward( |
| | | self, |
| | | enc: torch.Tensor, |
| | | enc_len: torch.Tensor, |
| | |
| | | "For the users in China, you could install with the command:\n" \ |
| | | "\npip3 install -U funasr -i https://mirror.sjtu.edu.cn/pypi/web/simple" |
| | | |
| | | model = AutoModel(model=cache_dir) |
| | | model_dir = model.export(type="onnx", quantize=quantize, device="cpu") |
| | | model = AutoModel(model=model_dir) |
| | | model_dir = model.export(type="onnx", quantize=quantize) |
| | | |
| | | config_file = os.path.join(model_dir, 'config.yaml') |
| | | cmvn_file = os.path.join(model_dir, 'am.mvn') |
| | |
| | | "For the users in China, you could install with the command:\n" \ |
| | | "\npip3 install -U funasr -i https://mirror.sjtu.edu.cn/pypi/web/simple" |
| | | |
| | | model = AutoModel(model=cache_dir) |
| | | model_dir = model.export(type="onnx", quantize=quantize, device="cpu") |
| | | model = AutoModel(model=model_dir) |
| | | model_dir = model.export(type="onnx", quantize=quantize) |
| | | |
| | | config_file = os.path.join(model_dir, 'config.yaml') |
| | | cmvn_file = os.path.join(model_dir, 'am.mvn') |
| | |
| | | "For the users in China, you could install with the command:\n" \ |
| | | "\npip3 install -U funasr -i https://mirror.sjtu.edu.cn/pypi/web/simple" |
| | | |
| | | model = AutoModel(model=cache_dir) |
| | | model_dir = model.export(type="onnx", quantize=quantize, device="cpu") |
| | | model = AutoModel(model=model_dir) |
| | | model_dir = model.export(type="onnx", quantize=quantize) |
| | | |
| | | config_file = os.path.join(model_dir, 'punc.yaml') |
| | | config = read_yaml(config_file) |
| | |
| | | "For the users in China, you could install with the command:\n" \ |
| | | "\npip3 install -U funasr -i https://mirror.sjtu.edu.cn/pypi/web/simple" |
| | | |
| | | model = AutoModel(model=cache_dir) |
| | | model_dir = model.export(type="onnx", quantize=quantize, device="cpu") |
| | | model = AutoModel(model=model_dir) |
| | | model_dir = model.export(type="onnx", quantize=quantize) |
| | | config_file = os.path.join(model_dir, 'vad.yaml') |
| | | cmvn_file = os.path.join(model_dir, 'vad.mvn') |
| | | config = read_yaml(config_file) |
| | |
| | | "For the users in China, you could install with the command:\n" \ |
| | | "\npip3 install -U funasr -i https://mirror.sjtu.edu.cn/pypi/web/simple" |
| | | |
| | | model = AutoModel(model=cache_dir) |
| | | model_dir = model.export(type="onnx", quantize=quantize, device="cpu") |
| | | model = AutoModel(model=model_dir) |
| | | model_dir = model.export(type="onnx", quantize=quantize) |
| | | |
| | | config_file = os.path.join(model_dir, 'vad.yaml') |
| | | cmvn_file = os.path.join(model_dir, 'vad.mvn') |