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 : Pass CPU threads parameter #2577

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 examples/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,7 @@ int main(int argc, char ** argv) {
}

// initialize openvino encoder. this has no effect on whisper.cpp builds that don't have OpenVINO configured
whisper_ctx_init_openvino_encoder(ctx, nullptr, params.openvino_encode_device.c_str(), nullptr);
whisper_ctx_init_openvino_encoder(ctx, nullptr, params.openvino_encode_device.c_str(), nullptr, params.n_threads);

if (!params.grammar.empty()) {
auto & grammar = params.grammar_parsed;
Expand Down
4 changes: 2 additions & 2 deletions examples/server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ int main(int argc, char ** argv) {
}

// initialize openvino encoder. this has no effect on whisper.cpp builds that don't have OpenVINO configured
whisper_ctx_init_openvino_encoder(ctx, nullptr, params.openvino_encode_device.c_str(), nullptr);
whisper_ctx_init_openvino_encoder(ctx, nullptr, params.openvino_encode_device.c_str(), nullptr, params.n_threads);

Server svr;
svr.set_default_headers({{"Server", "whisper.cpp"},
Expand Down Expand Up @@ -981,7 +981,7 @@ int main(int argc, char ** argv) {
}

// initialize openvino encoder. this has no effect on whisper.cpp builds that don't have OpenVINO configured
whisper_ctx_init_openvino_encoder(ctx, nullptr, params.openvino_encode_device.c_str(), nullptr);
whisper_ctx_init_openvino_encoder(ctx, nullptr, params.openvino_encode_device.c_str(), nullptr, params.n_threads);

const std::string success = "Load was successful!";
res.set_content(success, "application/text");
Expand Down
6 changes: 4 additions & 2 deletions include/whisper.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,15 @@ extern "C" {
struct whisper_state * state,
const char * model_path,
const char * device,
const char * cache_dir);
const char * cache_dir,
int n_threads);

WHISPER_API int whisper_ctx_init_openvino_encoder(
struct whisper_context * ctx,
const char * model_path,
const char * device,
const char * cache_dir);
const char * cache_dir,
int n_threads);

// Frees all allocated memory
WHISPER_API void whisper_free (struct whisper_context * ctx);
Expand Down
7 changes: 6 additions & 1 deletion src/openvino/whisper-openvino-encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ struct whisper_openvino_context {

struct whisper_openvino_context * whisper_openvino_init(const char* path_model,
const char* device,
const char* cache_dir)
const char* cache_dir,
int n_threads)
{
if (!path_model || !device) {
fprintf(stderr, "%s: path_model and/or device is null\n", __func__);
Expand All @@ -29,6 +30,10 @@ struct whisper_openvino_context * whisper_openvino_init(const char* path_model,
core.set_property(ov::cache_dir(cache_dir));
}

if (strncmp(device, "CPU", 3) == 0) {
core.set_property(ov::inference_num_threads(n_threads));
}

//Read the OpenVINO encoder IR (.xml/.bin) from disk, producing an ov::Model object.
std::shared_ptr<ov::Model> model = core.read_model(path_model);

Expand Down
3 changes: 2 additions & 1 deletion src/openvino/whisper-openvino-encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ struct whisper_openvino_context;
// path to cache_dir. Returns null upon failure.
struct whisper_openvino_context * whisper_openvino_init(const char * path_model,
const char * device,
const char * cache_dir);
const char * cache_dir,
int n_threads);

// clean up a ctx previously returned from whisper_openvino_init()
void whisper_openvino_free(struct whisper_openvino_context * ctx);
Expand Down
11 changes: 7 additions & 4 deletions src/whisper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3423,13 +3423,15 @@ int whisper_ctx_init_openvino_encoder_with_state(
struct whisper_state * state,
const char * model_path,
const char * device,
const char * cache_dir) {
const char * cache_dir,
int n_threads) {
#ifndef WHISPER_USE_OPENVINO
(void)(ctx);
(void)(state);
(void)(model_path);
(void)(device);
(void)(cache_dir);
(void)(n_threads);

return 1;
#else
Expand Down Expand Up @@ -3457,7 +3459,7 @@ int whisper_ctx_init_openvino_encoder_with_state(
WHISPER_LOG_INFO("%s: loading OpenVINO model from '%s'\n", __func__, path_encoder.c_str());
WHISPER_LOG_INFO("%s: first run on a device may take a while ...\n", __func__);

state->ctx_openvino = whisper_openvino_init(path_encoder.c_str(), device, path_cache.c_str());
state->ctx_openvino = whisper_openvino_init(path_encoder.c_str(), device, path_cache.c_str(), n_threads);
if (!state->ctx_openvino) {
WHISPER_LOG_ERROR("%s: failed to init OpenVINO encoder from '%s'\n", __func__, path_encoder.c_str());
return 1;
Expand All @@ -3473,8 +3475,9 @@ int whisper_ctx_init_openvino_encoder(
struct whisper_context * ctx,
const char * model_path,
const char * device,
const char * cache_dir) {
return whisper_ctx_init_openvino_encoder_with_state(ctx, ctx->state, model_path, device, cache_dir);
const char * cache_dir,
int n_threads) {
return whisper_ctx_init_openvino_encoder_with_state(ctx, ctx->state, model_path, device, cache_dir, n_threads);
}

struct whisper_context_params whisper_context_default_params() {
Expand Down