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

Use new OpenVINO Python API features #2

Merged
merged 4 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ tqdm
more-itertools
transformers>=4.19.0
ffmpeg-python==0.2.0
openvino
openvino>=2023.1.0
24 changes: 14 additions & 10 deletions whisper/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from torch import nn

from huggingface_hub import hf_hub_download
from openvino.runtime import Core
from openvino import Core

from .transcribe import transcribe as transcribe_function
from .decoding import detect_language as detect_language_function, decode as decode_function
Expand Down Expand Up @@ -39,8 +39,12 @@ def __init__(self, model: str):
self.model = self.core.compile_model(self._model, "CPU")

def forward(self, x: Tensor):
result = self.model.infer_new_request(x.numpy())
return torch.from_numpy(next(iter(result.values())))
result = self.model(
x,
share_inputs=True,
share_outputs=True,
)
return torch.from_numpy(result[0])


class OpenVinoTextDecoder(nn.Module):
Expand All @@ -55,17 +59,17 @@ def __init__(self, model: str):
self.model = self.core.compile_model(self._model, "CPU")

def forward(self, x: Tensor, xa: Union[Tensor, np.ndarray], kv_cache: Tensor, offset: int):
if torch.is_tensor(xa):
xa = xa.numpy()
output, kv_cache = self.model.infer_new_request(
output = self.model(
{
"tokens": x.numpy(),
"tokens": x,
"audio_features": xa,
"kv_cache": kv_cache,
"offset": np.array(offset, dtype=int),
}
).values()
return torch.from_numpy(output), kv_cache
},
share_inputs=True,
share_outputs=True,
)
return torch.from_numpy(output["logits"]), output["output_kv_cache"]


class Whisper(nn.Module):
Expand Down