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
Changes from 1 commit
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
Next Next commit
Apply optimizations and new indexing
  • Loading branch information
jiwaszki committed Jun 2, 2023
commit a6ab6418e447f3dfb5e81c8c2c53c1a1e79a8ed7
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.runtime import Core, ReturnPolicy

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,
shared_memory=True,
return_policy=ReturnPolicy.VIEW,
)
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
},
shared_memory=True,
return_policy=ReturnPolicy.VIEW,
)
return torch.from_numpy(output["logits"]), output["output_kv_cache"]


class Whisper(nn.Module):
Expand Down