许可协议: apache-2.0
数据集:
- librispeech_asr
评估指标:
- wer
流水线标签: 自动语音识别
标签:
- 自动语音识别
- ONNX
- 英特尔®神经压缩器
- neural-compressor
库名称: transformers
INT4 Whisper large-v2 ONNX模型
Whisper是一个预训练的自动语音识别(ASR)和语音翻译模型。经过68万小时标注数据的训练,Whisper模型展现出强大的泛化能力,无需微调即可适应多种数据集和领域。此仓库包含ONNX格式的Whisper large v2模型的INT4仅权重量化版本,由英特尔®神经压缩器和英特尔®Transformers扩展驱动。
该INT4 ONNX模型由英特尔®神经压缩器的仅权重量化方法生成。
模型详情 |
描述 |
模型作者 - 公司 |
英特尔 |
日期 |
2023年10月8日 |
版本 |
1 |
类型 |
语音识别 |
论文或其他资源 |
- |
许可协议 |
Apache 2.0 |
问题或评论 |
社区讨论区 |
预期用途 |
描述 |
主要用途 |
可用于自动语音识别推理 |
主要用户 |
任何进行自动语音识别推理的用户 |
非适用范围 |
大多数情况下需针对特定任务微调。不可用于故意制造敌对或疏远环境。 |
导出为ONNX模型
FP32模型通过openai/whisper-large-v2导出:
optimum-cli export onnx --model openai/whisper-large-v2 whisper-large-v2-with-past/ --task automatic-speech-recognition-with-past --opset 13
安装ONNX Runtime
安装onnxruntime>=1.16.0
以支持MatMulFpQ4
运算符。
运行量化
从主分支构建英特尔®神经压缩器并运行INT4仅权重量化。
权重量化配置如下:
数据类型 |
分组大小 |
方案 |
算法 |
INT4 |
32 |
对称 |
RTN |
关键代码如下。完整脚本请参考whisper示例。
from neural_compressor import quantization, PostTrainingQuantConfig
from neural_compressor.utils.constant import FP32
model_list = ['encoder_model.onnx', 'decoder_model.onnx', 'decoder_with_past_model.onnx']
for model in model_list:
config = PostTrainingQuantConfig(
approach="weight_only",
calibration_sampling_size=[8],
op_type_dict={".*": {"weight": {"bits": 4,
"algorithm": ["RTN"],
"scheme": ["sym"],
"group_size": 32}}},)
q_model = quantization.fit(
os.path.join("/path/to/whisper-large-v2-with-past", model),
config,
calib_dataloader=dataloader)
q_model.save(os.path.join("/path/to/whisper-large-v2-onnx-int4", model))
评估
运算符统计
INT4 ONNX模型的运算符统计如下:
模型 |
运算符类型 |
总数 |
INT4权重 |
FP32权重 |
编码器模型 |
MatMul |
256 |
192 |
64 |
解码器模型 |
MatMul |
449 |
321 |
128 |
带历史解码器模型 |
MatMul |
385 |
257 |
128 |
词错率评估
在librispeech_asr
数据集上评估模型:
import os
from evaluate import load
from datasets import load_dataset
from transformers import WhisperForConditionalGeneration, WhisperProcessor, AutoConfig
model_name = 'openai/whisper-large-v2'
model_path = 'whisper-large-v2-onnx-int4'
processor = WhisperProcessor.from_pretrained(model_name)
model = WhisperForConditionalGeneration.from_pretrained(model_name)
config = AutoConfig.from_pretrained(model_name)
wer = load("wer")
librispeech_test_clean = load_dataset("librispeech_asr", "clean", split="test")
from optimum.onnxruntime import ORTModelForSpeechSeq2Seq
from transformers import PretrainedConfig
model_config = PretrainedConfig.from_pretrained(model_name)
predictions = []
references = []
sessions = ORTModelForSpeechSeq2Seq.load_model(
os.path.join(model_path, 'encoder_model.onnx'),
os.path.join(model_path, 'decoder_model.onnx'),
os.path.join(model_path, 'decoder_with_past_model.onnx'))
model = ORTModelForSpeechSeq2Seq(sessions[0], sessions[1], model_config, model_path, sessions[2])
for idx, batch in enumerate(librispeech_test_clean):
audio = batch["audio"]
input_features = processor(audio["array"], sampling_rate=audio["sampling_rate"], return_tensors="pt").input_features
reference = processor.tokenizer._normalize(batch['text'])
references.append(reference)
predicted_ids = model.generate(input_features)[0]
transcription = processor.decode(predicted_ids)
prediction = processor.tokenizer._normalize(transcription)
predictions.append(prediction)
wer_result = wer.compute(references=references, predictions=predictions)
print(f"词错率结果: {wer_result * 100}")
性能指标:
模型 |
模型大小(GB) |
词错率 |
FP32 |
15.2 |
2.87 |
INT4 |
1.9 |
2.99 |