Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

openvino : fix convert-whisper-to-openvino.py for v2023.0.0 (#1870) #1890

Merged
merged 1 commit into from
Feb 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions models/convert-whisper-to-openvino.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from whisper import load_model
import os
from openvino.tools import mo
from openvino.frontend import FrontEndManager
from openvino.runtime import serialize
import shutil

Expand All @@ -11,14 +12,15 @@ def convert_encoder(hparams, encoder, mname):

mel = torch.zeros((1, hparams.n_mels, 3000))

onnx_folder=os.path.join(os.path.dirname(__file__),"onnx_encoder")
onnx_folder = os.path.join(os.path.dirname(__file__), "onnx_encoder")

#create a directory to store the onnx model, and other collateral that is saved during onnx export procedure
if not os.path.isdir(onnx_folder):
os.makedirs(onnx_folder)

onnx_path = os.path.join(onnx_folder, "whisper_encoder.onnx")

# Export the PyTorch model to ONNX
torch.onnx.export(
encoder,
mel,
Expand All @@ -27,11 +29,16 @@ def convert_encoder(hparams, encoder, mname):
output_names=["output_features"]
)

# use model optimizer to convert onnx to OpenVINO IR format
encoder_model = mo.convert_model(onnx_path, compress_to_fp16=True)
serialize(encoder_model, xml_path=os.path.join(os.path.dirname(__file__),"ggml-" + mname + "-encoder-openvino.xml"))
# Convert ONNX to OpenVINO IR format using the frontend
fem = FrontEndManager()
onnx_fe = fem.load_by_framework("onnx")
onnx_model = onnx_fe.load(onnx_path)
ov_model = onnx_fe.convert(onnx_model)

#cleanup
# Serialize the OpenVINO model to XML and BIN files
serialize(ov_model, xml_path=os.path.join(os.path.dirname(__file__), "ggml-" + mname + "-encoder-openvino.xml"))

# Cleanup
if os.path.isdir(onnx_folder):
shutil.rmtree(onnx_folder)

Expand Down