Skip to content

Commit e266996

Browse files
committed
whisper : minor OpenVINO refactoring (ggml-org#1037)
Hopefully I didn't break something - haven't tested
1 parent bf2b9b8 commit e266996

File tree

5 files changed

+48
-45
lines changed

5 files changed

+48
-45
lines changed

examples/common.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
4747
params.n_batch = std::stoi(argv[++i]);
4848
} else if (arg == "-m" || arg == "--model") {
4949
params.model = argv[++i];
50+
} else if (arg == "-i" || arg == "--interactive") {
51+
params.interactive = true;
52+
} else if (arg == "-ip" || arg == "--interactive-port") {
53+
params.interactive = true;
54+
params.interactive_port = std::stoi(argv[++i]);
5055
} else if (arg == "-h" || arg == "--help") {
5156
gpt_print_usage(argc, argv, params);
5257
exit(0);

examples/common.h

+8-6
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,24 @@
1515
//
1616

1717
struct gpt_params {
18-
int32_t seed = -1; // RNG seed
18+
int32_t seed = -1; // RNG seed
1919
int32_t n_threads = std::min(4, (int32_t) std::thread::hardware_concurrency());
2020
int32_t n_predict = 200; // new tokens to predict
21+
int32_t n_batch = 8; // batch size for prompt processing
2122

2223
// sampling parameters
23-
int32_t top_k = 40;
24-
float top_p = 0.9f;
25-
float temp = 0.9f;
24+
int32_t top_k = 40;
25+
float top_p = 0.9f;
26+
float temp = 0.9f;
2627
int32_t repeat_last_n = 64;
2728
float repeat_penalty = 1.00f;
2829

29-
int32_t n_batch = 8; // batch size for prompt processing
30-
3130
std::string model = "models/gpt-2-117M/ggml-model.bin"; // model path
3231
std::string prompt = "";
3332
std::string token_test = "";
33+
34+
bool interactive = false;
35+
int32_t interactive_port = -1;
3436
};
3537

3638
bool gpt_params_parse(int argc, char ** argv, gpt_params & params);

examples/main/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ int main(int argc, char ** argv) {
813813
return 3;
814814
}
815815

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

819819
for (int f = 0; f < (int) params.fname_inp.size(); ++f) {

whisper.cpp

+33-36
Original file line numberDiff line numberDiff line change
@@ -2654,7 +2654,7 @@ static std::string whisper_get_coreml_path_encoder(std::string path_bin) {
26542654

26552655
#ifdef WHISPER_USE_OPENVINO
26562656
// replace .bin with-encoder-openvino.xml
2657-
static std::string whisper_get_openvino_path_encoder(std::string path_bin) {
2657+
static std::string whisper_openvino_get_path_encoder(std::string path_bin) {
26582658
auto pos = path_bin.rfind('.');
26592659
if (pos != std::string::npos) {
26602660
path_bin = path_bin.substr(0, pos);
@@ -2665,7 +2665,7 @@ static std::string whisper_get_openvino_path_encoder(std::string path_bin) {
26652665
return path_bin;
26662666
}
26672667

2668-
static std::string whisper_get_openvino_path_cache(std::string path_bin) {
2668+
static std::string whisper_openvino_get_path_cache(std::string path_bin) {
26692669
auto pos = path_bin.rfind('.');
26702670
if (pos != std::string::npos) {
26712671
path_bin = path_bin.substr(0, pos);
@@ -2743,55 +2743,52 @@ struct whisper_state * whisper_init_state(whisper_context * ctx) {
27432743
return state;
27442744
}
27452745

2746-
int whisper_ctx_init_openvino_encoder(struct whisper_context* ctx,
2747-
const char* openvino_model_path,
2748-
const char* openvino_device,
2749-
const char* openvino_cache_dir)
2750-
{
2746+
int whisper_ctx_init_openvino_encoder(
2747+
struct whisper_context * ctx,
2748+
const char * model_path,
2749+
const char * device,
2750+
const char * cache_dir) {
27512751
#ifndef WHISPER_USE_OPENVINO
27522752
(void)(ctx);
2753-
(void)(openvino_model_path);
2754-
(void)(openvino_device);
2755-
(void)(openvino_cache_dir);
2756-
return 0;
2753+
(void)(model_path);
2754+
(void)(device);
2755+
(void)(cache_dir);
2756+
2757+
return 1;
27572758
#else
2758-
if (!openvino_model_path && ctx->path_model.empty())
2759-
{
2760-
fprintf(stderr, "%s: openvino_model_path is nullptr, and ctx has no model_path set.\n", __func__);
2761-
return 0;
2759+
if (!model_path && ctx->path_model.empty()) {
2760+
fprintf(stderr, "%s: model_path is nullptr, and ctx has no model_path set.\n", __func__);
2761+
return 1;
27622762
}
27632763

2764-
std::string path_openvino;
2765-
if (!openvino_model_path) {
2766-
//if openvino_model_path is not set, attempt to find it in the same directory as ggml-<model>.bin model
2767-
path_openvino = whisper_get_openvino_path_encoder(ctx->path_model);
2768-
}
2769-
else {
2770-
path_openvino = openvino_model_path;
2764+
std::string path_encoder;
2765+
if (!model_path) {
2766+
//if model_path is not set, attempt to find it in the same directory as ggml-<model>.bin model
2767+
path_encoder = whisper_openvino_get_path_encoder(ctx->path_model);
2768+
} else {
2769+
path_encoder = model_path;
27712770
}
27722771

2773-
std::string path_openvino_cache_dir;
2774-
if (!openvino_cache_dir) {
2775-
//if openvino_cache_dir is not set, set it as a dir residing next to ggml-<model>.bin
2776-
path_openvino_cache_dir = whisper_get_openvino_path_cache(ctx->path_model);
2777-
}
2778-
else {
2779-
path_openvino_cache_dir = openvino_cache_dir;
2772+
std::string path_cache;
2773+
if (!cache_dir) {
2774+
//if cache_dir is not set, set it as a dir residing next to ggml-<model>.bin
2775+
path_cache = whisper_openvino_get_path_cache(ctx->path_model);
2776+
} else {
2777+
path_cache = cache_dir;
27802778
}
27812779

2782-
fprintf(stderr, "%s: loading OpenVINO model from '%s'\n", __func__, path_openvino.c_str());
2780+
fprintf(stderr, "%s: loading OpenVINO model from '%s'\n", __func__, path_encoder.c_str());
27832781
fprintf(stderr, "%s: first run on a device may take a while ...\n", __func__);
27842782

2785-
ctx->state->ctx_openvino = whisper_openvino_init(path_openvino.c_str(), openvino_device, path_openvino_cache_dir.c_str());
2783+
ctx->state->ctx_openvino = whisper_openvino_init(path_encoder.c_str(), device, path_cache.c_str());
27862784
if (!ctx->state->ctx_openvino) {
2787-
fprintf(stderr, "%s: failed to init OpenVINO encoder from '%s'\n", __func__, path_openvino.c_str());
2788-
return 0;
2789-
}
2790-
else {
2785+
fprintf(stderr, "%s: failed to init OpenVINO encoder from '%s'\n", __func__, path_encoder.c_str());
2786+
return 1;
2787+
} else {
27912788
fprintf(stderr, "%s: OpenVINO model loaded\n", __func__);
27922789
}
27932790

2794-
return 1;
2791+
return 0;
27952792
#endif
27962793
}
27972794

whisper.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,7 @@ extern "C" {
120120
// cache_dir: Optional cache directory that can speed up init time, especially for
121121
// GPU, by caching compiled 'blobs' there.
122122
// Set to nullptr if not used.
123-
// Returns 1 on success. If OpenVINO is not enabled in build, this
124-
// simply returns 0.
123+
// Returns 0 on success. If OpenVINO is not enabled in build, this simply returns 1.
125124
WHISPER_API int whisper_ctx_init_openvino_encoder(
126125
struct whisper_context * ctx,
127126
const char * model_path,

0 commit comments

Comments
 (0)