Skip to content

Commit 50461e3

Browse files
authored
Merge pull request #106 from absadiki/feat/openvino
feat: openvino support
2 parents 4ab9616 + 9b7feff commit 50461e3

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

pywhispercpp/model.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ def __init__(self,
7575
models_dir: str = None,
7676
params_sampling_strategy: int = 0,
7777
redirect_whispercpp_logs_to: Union[bool, TextIO, str, None] = False,
78+
use_openvino: bool = False,
79+
openvino_model_path: str = None,
80+
openvino_device: str = 'CPU',
81+
openvino_cache_dir: str = None,
7882
**params):
7983
"""
8084
:param model: The name of the model, one of the [AVAILABLE_MODELS](/pywhispercpp/#pywhispercpp.constants.AVAILABLE_MODELS),
@@ -83,6 +87,10 @@ def __init__(self,
8387
exist, default to [MODELS_DIR](/pywhispercpp/#pywhispercpp.constants.MODELS_DIR) <user_data_dir/pywhsipercpp/models>
8488
:param params_sampling_strategy: 0 -> GREEDY, else BEAM_SEARCH
8589
:param redirect_whispercpp_logs_to: where to redirect the whisper.cpp logs, default to False (no redirection), accepts str file path, sys.stdout, sys.stderr, or use None to redirect to devnull
90+
:param use_openvino: whether to use OpenVINO or not
91+
:param openvino_model_path: path to the OpenVINO model
92+
:param openvino_device: OpenVINO device, default to CPU
93+
:param openvino_cache_dir: OpenVINO cache directory
8694
:param params: keyword arguments for different whisper.cpp parameters,
8795
see [PARAMS_SCHEMA](/pywhispercpp/#pywhispercpp.constants.PARAMS_SCHEMA)
8896
"""
@@ -95,8 +103,13 @@ def __init__(self,
95103
pw.whisper_sampling_strategy.WHISPER_SAMPLING_BEAM_SEARCH
96104
self._params = pw.whisper_full_default_params(self._sampling_strategy)
97105
# assign params
106+
self.params = params
98107
self._set_params(params)
99108
self.redirect_whispercpp_logs_to = redirect_whispercpp_logs_to
109+
self.use_openvino = use_openvino
110+
self.openvino_model_path = openvino_model_path
111+
self.openvino_device = openvino_device
112+
self.openvino_cache_dir = openvino_cache_dir
100113
# init the model
101114
self._init_model()
102115

@@ -256,6 +269,10 @@ def _init_model(self) -> None:
256269
logger.info("Initializing the model ...")
257270
with utils.redirect_stderr(to=self.redirect_whispercpp_logs_to):
258271
self._ctx = pw.whisper_init_from_file(self.model_path)
272+
if self.use_openvino:
273+
pw.whisper_ctx_init_openvino_encoder(self._ctx, self.openvino_model_path, self.openvino_device, self.openvino_cache_dir)
274+
275+
259276

260277
def _set_params(self, kwargs: dict) -> None:
261278
"""

src/main.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,12 @@ float whisper_full_get_token_p_wrapper(struct whisper_context_wrapper * ctx, int
284284
return whisper_full_get_token_p(ctx->ptr, i_segment, i_token);
285285
}
286286

287+
int whisper_ctx_init_openvino_encoder_wrapper(struct whisper_context_wrapper * ctx, const char * model_path,
288+
const char * device,
289+
const char * cache_dir){
290+
return whisper_ctx_init_openvino_encoder(ctx->ptr, model_path, device, cache_dir);
291+
}
292+
287293
class WhisperFullParamsWrapper : public whisper_full_params {
288294
std::string initial_prompt_str;
289295
std::string suppress_regex_str;
@@ -667,6 +673,9 @@ PYBIND11_MODULE(_pywhispercpp, m) {
667673

668674
m.def("whisper_full_get_token_p", &whisper_full_get_token_p_wrapper, "Get the probability of the specified token in the specified segment.");
669675

676+
m.def("whisper_ctx_init_openvino_encoder", &whisper_ctx_init_openvino_encoder_wrapper, "Given a context, enable use of OpenVINO for encode inference.");
677+
678+
670679
////////////////////////////////////////////////////////////////////////////
671680

672681
m.def("whisper_bench_memcpy", &whisper_bench_memcpy, "Temporary helpers needed for exposing ggml interface");

0 commit comments

Comments
 (0)