Skip to content

Commit 0787949

Browse files
committed
whisper : add whisper_context_params { use_gpu }
1 parent 44a58ea commit 0787949

File tree

4 files changed

+41
-26
lines changed

4 files changed

+41
-26
lines changed

examples/bench/bench.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ struct whisper_params {
1010
int32_t what = 0; // what to benchmark: 0 - whisper ecoder, 1 - memcpy, 2 - ggml_mul_mat
1111

1212
std::string model = "models/ggml-base.en.bin";
13+
bool use_gpu = true;
1314
};
1415

1516
void whisper_print_usage(int argc, char ** argv, const whisper_params & params);
@@ -24,7 +25,8 @@ bool whisper_params_parse(int argc, char ** argv, whisper_params & params) {
2425
}
2526
else if (arg == "-t" || arg == "--threads") { params.n_threads = std::stoi(argv[++i]); }
2627
else if (arg == "-m" || arg == "--model") { params.model = argv[++i]; }
27-
else if (arg == "-w" || arg == "--what") { params.what = atoi(argv[++i]); }
28+
else if (arg == "-w" || arg == "--what") { params.what = atoi(argv[++i]); }
29+
else if (arg == "-ng" || arg == "--no-gpu") { params.use_gpu = false; }
2830
else {
2931
fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
3032
whisper_print_usage(argc, argv, params);
@@ -53,7 +55,9 @@ void whisper_print_usage(int /*argc*/, char ** argv, const whisper_params & para
5355
int whisper_bench_full(const whisper_params & params) {
5456
// whisper init
5557

56-
struct whisper_context * ctx = whisper_init_from_file(params.model.c_str());
58+
struct whisper_context_params cparams;
59+
cparams.use_gpu = params.use_gpu;
60+
struct whisper_context * ctx = whisper_init_from_file(params.model.c_str(), cparams);
5761

5862
{
5963
fprintf(stderr, "\n");

examples/main/main.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ struct whisper_params {
102102

103103
std::vector<std::string> fname_inp = {};
104104
std::vector<std::string> fname_out = {};
105+
106+
bool use_gpu = true;
105107
};
106108

107109
void whisper_print_usage(int argc, char ** argv, const whisper_params & params);
@@ -163,6 +165,7 @@ bool whisper_params_parse(int argc, char ** argv, whisper_params & params) {
163165
else if (arg == "-f" || arg == "--file") { params.fname_inp.emplace_back(argv[++i]); }
164166
else if (arg == "-oved" || arg == "--ov-e-device") { params.openvino_encode_device = argv[++i]; }
165167
else if (arg == "-ls" || arg == "--log-score") { params.log_score = true; }
168+
else if (arg == "-ng" || arg == "--no-gpu") { params.use_gpu = false; }
166169
else {
167170
fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
168171
whisper_print_usage(argc, argv, params);
@@ -841,7 +844,9 @@ int main(int argc, char ** argv) {
841844

842845
// whisper init
843846

844-
struct whisper_context * ctx = whisper_init_from_file(params.model.c_str());
847+
struct whisper_context_params cparams;
848+
cparams.use_gpu = params.use_gpu;
849+
struct whisper_context * ctx = whisper_init_from_file(params.model.c_str(), cparams);
845850

846851
if (ctx == nullptr) {
847852
fprintf(stderr, "error: failed to initialize whisper context\n");

whisper.cpp

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,7 @@ struct whisper_context {
762762
whisper_state * state = nullptr;
763763

764764
std::string path_model; // populated by whisper_init_from_file()
765+
whisper_context_params params;
765766
};
766767

767768
static void whisper_default_log(const char * text) {
@@ -2917,12 +2918,13 @@ struct whisper_state * whisper_init_state(whisper_context * ctx) {
29172918
}
29182919

29192920
#ifdef GGML_USE_METAL
2920-
// TODO: Param for enable GPU
2921-
state->ctx_metal = ggml_metal_init(1);
2922-
if (!state->ctx_metal) {
2923-
log("%s: ggml_metal_init() failed\n", __func__);
2924-
delete state;
2925-
return nullptr;
2921+
if (ctx->params.use_gpu) {
2922+
state->ctx_metal = ggml_metal_init(1);
2923+
if (!state->ctx_metal) {
2924+
log("%s: ggml_metal_init() failed\n", __func__);
2925+
delete state;
2926+
return nullptr;
2927+
}
29262928
}
29272929

29282930
if (state->ctx_metal) {
@@ -3030,7 +3032,7 @@ int whisper_ctx_init_openvino_encoder(
30303032
#endif
30313033
}
30323034

3033-
struct whisper_context * whisper_init_from_file_no_state(const char * path_model) {
3035+
struct whisper_context * whisper_init_from_file_no_state(const char * path_model, whisper_context_params params) {
30343036
log("%s: loading model from '%s'\n", __func__, path_model);
30353037

30363038
auto fin = std::ifstream(path_model, std::ios::binary);
@@ -3059,7 +3061,7 @@ struct whisper_context * whisper_init_from_file_no_state(const char * path_model
30593061
fin->close();
30603062
};
30613063

3062-
auto ctx = whisper_init_no_state(&loader);
3064+
auto ctx = whisper_init_no_state(&loader, params);
30633065

30643066
if (ctx) {
30653067
ctx->path_model = path_model;
@@ -3068,7 +3070,7 @@ struct whisper_context * whisper_init_from_file_no_state(const char * path_model
30683070
return ctx;
30693071
}
30703072

3071-
struct whisper_context * whisper_init_from_buffer_no_state(void * buffer, size_t buffer_size) {
3073+
struct whisper_context * whisper_init_from_buffer_no_state(void * buffer, size_t buffer_size, whisper_context_params params) {
30723074
struct buf_context {
30733075
uint8_t* buffer;
30743076
size_t size;
@@ -3102,13 +3104,14 @@ struct whisper_context * whisper_init_from_buffer_no_state(void * buffer, size_t
31023104

31033105
loader.close = [](void * /*ctx*/) { };
31043106

3105-
return whisper_init_no_state(&loader);
3107+
return whisper_init_no_state(&loader, params);
31063108
}
31073109

3108-
struct whisper_context * whisper_init_no_state(struct whisper_model_loader * loader) {
3110+
struct whisper_context * whisper_init_no_state(struct whisper_model_loader * loader, whisper_context_params params) {
31093111
ggml_time_init();
31103112

31113113
whisper_context * ctx = new whisper_context;
3114+
ctx->params = params;
31123115

31133116
if (!whisper_model_load(loader, *ctx)) {
31143117
loader->close(loader->context);
@@ -3122,8 +3125,8 @@ struct whisper_context * whisper_init_no_state(struct whisper_model_loader * loa
31223125
return ctx;
31233126
}
31243127

3125-
struct whisper_context * whisper_init_from_file(const char * path_model) {
3126-
whisper_context * ctx = whisper_init_from_file_no_state(path_model);
3128+
struct whisper_context * whisper_init_from_file(const char * path_model, whisper_context_params params) {
3129+
whisper_context * ctx = whisper_init_from_file_no_state(path_model, params);
31273130
if (!ctx) {
31283131
return nullptr;
31293132
}
@@ -3137,8 +3140,8 @@ struct whisper_context * whisper_init_from_file(const char * path_model) {
31373140
return ctx;
31383141
}
31393142

3140-
struct whisper_context * whisper_init_from_buffer(void * buffer, size_t buffer_size) {
3141-
whisper_context * ctx = whisper_init_from_buffer_no_state(buffer, buffer_size);
3143+
struct whisper_context * whisper_init_from_buffer(void * buffer, size_t buffer_size, whisper_context_params params) {
3144+
whisper_context * ctx = whisper_init_from_buffer_no_state(buffer, buffer_size, params);
31423145
if (!ctx) {
31433146
return nullptr;
31443147
}
@@ -3152,8 +3155,8 @@ struct whisper_context * whisper_init_from_buffer(void * buffer, size_t buffer_s
31523155
return ctx;
31533156
}
31543157

3155-
struct whisper_context * whisper_init(struct whisper_model_loader * loader) {
3156-
whisper_context * ctx = whisper_init_no_state(loader);
3158+
struct whisper_context * whisper_init(struct whisper_model_loader * loader, whisper_context_params params) {
3159+
whisper_context * ctx = whisper_init_no_state(loader, params);
31573160
if (!ctx) {
31583161
return nullptr;
31593162
}

whisper.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ extern "C" {
6565
// understanding of how the model works.
6666
//
6767

68+
struct whisper_context_params {
69+
bool use_gpu = true;
70+
};
6871
struct whisper_context;
6972
struct whisper_state;
7073
struct whisper_full_params;
@@ -99,15 +102,15 @@ extern "C" {
99102
// Various functions for loading a ggml whisper model.
100103
// Allocate (almost) all memory needed for the model.
101104
// Return NULL on failure
102-
WHISPER_API struct whisper_context * whisper_init_from_file(const char * path_model);
103-
WHISPER_API struct whisper_context * whisper_init_from_buffer(void * buffer, size_t buffer_size);
104-
WHISPER_API struct whisper_context * whisper_init(struct whisper_model_loader * loader);
105+
WHISPER_API struct whisper_context * whisper_init_from_file(const char * path_model, whisper_context_params params);
106+
WHISPER_API struct whisper_context * whisper_init_from_buffer(void * buffer, size_t buffer_size, whisper_context_params params);
107+
WHISPER_API struct whisper_context * whisper_init(struct whisper_model_loader * loader, whisper_context_params params);
105108

106109
// These are the same as the above, but the internal state of the context is not allocated automatically
107110
// It is the responsibility of the caller to allocate the state using whisper_init_state() (#523)
108-
WHISPER_API struct whisper_context * whisper_init_from_file_no_state(const char * path_model);
109-
WHISPER_API struct whisper_context * whisper_init_from_buffer_no_state(void * buffer, size_t buffer_size);
110-
WHISPER_API struct whisper_context * whisper_init_no_state(struct whisper_model_loader * loader);
111+
WHISPER_API struct whisper_context * whisper_init_from_file_no_state(const char * path_model, whisper_context_params params);
112+
WHISPER_API struct whisper_context * whisper_init_from_buffer_no_state(void * buffer, size_t buffer_size, whisper_context_params params);
113+
WHISPER_API struct whisper_context * whisper_init_no_state(struct whisper_model_loader * loader, whisper_context_params params);
111114

112115
WHISPER_API struct whisper_state * whisper_init_state(struct whisper_context * ctx);
113116

0 commit comments

Comments
 (0)