Skip to content

Commit 5c2325f

Browse files
committed
whisper : new API with params & deprecate old API
1 parent 3d1369e commit 5c2325f

File tree

4 files changed

+84
-20
lines changed

4 files changed

+84
-20
lines changed

examples/bench/bench.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ int whisper_bench_full(const whisper_params & params) {
5757

5858
struct whisper_context_params cparams;
5959
cparams.use_gpu = params.use_gpu;
60-
struct whisper_context * ctx = whisper_init_from_file(params.model.c_str(), cparams);
60+
struct whisper_context * ctx = whisper_init_from_file_with_params(params.model.c_str(), cparams);
6161

6262
{
6363
fprintf(stderr, "\n");

examples/main/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ int main(int argc, char ** argv) {
846846

847847
struct whisper_context_params cparams;
848848
cparams.use_gpu = params.use_gpu;
849-
struct whisper_context * ctx = whisper_init_from_file(params.model.c_str(), cparams);
849+
struct whisper_context * ctx = whisper_init_from_file_with_params(params.model.c_str(), cparams);
850850

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

whisper.cpp

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3032,7 +3032,14 @@ int whisper_ctx_init_openvino_encoder(
30323032
#endif
30333033
}
30343034

3035-
struct whisper_context * whisper_init_from_file_no_state(const char * path_model, whisper_context_params params) {
3035+
struct whisper_context_params whisper_context_default_params() {
3036+
struct whisper_context_params result = {
3037+
/*.use_gpu =*/ true,
3038+
};
3039+
return result;
3040+
}
3041+
3042+
struct whisper_context * whisper_init_from_file_with_params_no_state(const char * path_model, struct whisper_context_params params) {
30363043
log("%s: loading model from '%s'\n", __func__, path_model);
30373044

30383045
auto fin = std::ifstream(path_model, std::ios::binary);
@@ -3061,7 +3068,7 @@ struct whisper_context * whisper_init_from_file_no_state(const char * path_model
30613068
fin->close();
30623069
};
30633070

3064-
auto ctx = whisper_init_no_state(&loader, params);
3071+
auto ctx = whisper_init_with_params_no_state(&loader, params);
30653072

30663073
if (ctx) {
30673074
ctx->path_model = path_model;
@@ -3070,7 +3077,7 @@ struct whisper_context * whisper_init_from_file_no_state(const char * path_model
30703077
return ctx;
30713078
}
30723079

3073-
struct whisper_context * whisper_init_from_buffer_no_state(void * buffer, size_t buffer_size, whisper_context_params params) {
3080+
struct whisper_context * whisper_init_from_buffer_with_params_no_state(void * buffer, size_t buffer_size, struct whisper_context_params params) {
30743081
struct buf_context {
30753082
uint8_t* buffer;
30763083
size_t size;
@@ -3104,10 +3111,10 @@ struct whisper_context * whisper_init_from_buffer_no_state(void * buffer, size_t
31043111

31053112
loader.close = [](void * /*ctx*/) { };
31063113

3107-
return whisper_init_no_state(&loader, params);
3114+
return whisper_init_with_params_no_state(&loader, params);
31083115
}
31093116

3110-
struct whisper_context * whisper_init_no_state(struct whisper_model_loader * loader, whisper_context_params params) {
3117+
struct whisper_context * whisper_init_with_params_no_state(struct whisper_model_loader * loader, struct whisper_context_params params) {
31113118
ggml_time_init();
31123119

31133120
whisper_context * ctx = new whisper_context;
@@ -3125,8 +3132,8 @@ struct whisper_context * whisper_init_no_state(struct whisper_model_loader * loa
31253132
return ctx;
31263133
}
31273134

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);
3135+
struct whisper_context * whisper_init_from_file_with_params(const char * path_model, struct whisper_context_params params) {
3136+
whisper_context * ctx = whisper_init_from_file_with_params_no_state(path_model, params);
31303137
if (!ctx) {
31313138
return nullptr;
31323139
}
@@ -3140,8 +3147,8 @@ struct whisper_context * whisper_init_from_file(const char * path_model, whisper
31403147
return ctx;
31413148
}
31423149

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);
3150+
struct whisper_context * whisper_init_from_buffer_with_params(void * buffer, size_t buffer_size, struct whisper_context_params params) {
3151+
whisper_context * ctx = whisper_init_from_buffer_with_params_no_state(buffer, buffer_size, params);
31453152
if (!ctx) {
31463153
return nullptr;
31473154
}
@@ -3155,8 +3162,8 @@ struct whisper_context * whisper_init_from_buffer(void * buffer, size_t buffer_s
31553162
return ctx;
31563163
}
31573164

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);
3165+
struct whisper_context * whisper_init_with_params(struct whisper_model_loader * loader, struct whisper_context_params params) {
3166+
whisper_context * ctx = whisper_init_with_params_no_state(loader, params);
31603167
if (!ctx) {
31613168
return nullptr;
31623169
}
@@ -3170,6 +3177,30 @@ struct whisper_context * whisper_init(struct whisper_model_loader * loader, whis
31703177
return ctx;
31713178
}
31723179

3180+
struct whisper_context * whisper_init_from_file(const char * path_model) {
3181+
return whisper_init_from_file_with_params(path_model, whisper_context_default_params());
3182+
}
3183+
3184+
struct whisper_context * whisper_init_from_buffer(void * buffer, size_t buffer_size) {
3185+
return whisper_init_from_buffer_with_params(buffer, buffer_size, whisper_context_default_params());
3186+
}
3187+
3188+
struct whisper_context * whisper_init(struct whisper_model_loader * loader) {
3189+
return whisper_init_with_params(loader, whisper_context_default_params());
3190+
}
3191+
3192+
struct whisper_context * whisper_init_from_file_no_state(const char * path_model) {
3193+
return whisper_init_from_file_with_params_no_state(path_model, whisper_context_default_params());
3194+
}
3195+
3196+
struct whisper_context * whisper_init_from_buffer_no_state(void * buffer, size_t buffer_size) {
3197+
return whisper_init_from_buffer_with_params_no_state(buffer, buffer_size, whisper_context_default_params());
3198+
}
3199+
3200+
struct whisper_context * whisper_init_no_state(struct whisper_model_loader * loader) {
3201+
return whisper_init_with_params_no_state(loader, whisper_context_default_params());
3202+
}
3203+
31733204
void whisper_free_state(struct whisper_state * state)
31743205
{
31753206
if (state) {

whisper.h

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55
#include <stdint.h>
66
#include <stdbool.h>
77

8+
#ifdef __GNUC__
9+
# define WHISPER_DEPRECATED(func, hint) func __attribute__((deprecated(hint)))
10+
#elif defined(_MSC_VER)
11+
# define WHISPER_DEPRECATED(func, hint) __declspec(deprecated(hint)) func
12+
#else
13+
# define WHISPER_DEPRECATED(func, hint) func
14+
#endif
15+
816
#ifdef WHISPER_SHARED
917
# ifdef _WIN32
1018
# ifdef WHISPER_BUILD
@@ -66,7 +74,7 @@ extern "C" {
6674
//
6775

6876
struct whisper_context_params {
69-
bool use_gpu = true;
77+
bool use_gpu;
7078
};
7179
struct whisper_context;
7280
struct whisper_state;
@@ -102,15 +110,40 @@ extern "C" {
102110
// Various functions for loading a ggml whisper model.
103111
// Allocate (almost) all memory needed for the model.
104112
// Return NULL on failure
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);
113+
WHISPER_API struct whisper_context * whisper_init_from_file_with_params(const char * path_model, struct whisper_context_params params);
114+
WHISPER_API struct whisper_context * whisper_init_from_buffer_with_params(void * buffer, size_t buffer_size, struct whisper_context_params params);
115+
WHISPER_API struct whisper_context * whisper_init_with_params(struct whisper_model_loader * loader, struct whisper_context_params params);
108116

109117
// These are the same as the above, but the internal state of the context is not allocated automatically
110118
// It is the responsibility of the caller to allocate the state using whisper_init_state() (#523)
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);
119+
WHISPER_API struct whisper_context * whisper_init_from_file_with_params_no_state(const char * path_model, struct whisper_context_params params);
120+
WHISPER_API struct whisper_context * whisper_init_from_buffer_with_params_no_state(void * buffer, size_t buffer_size, struct whisper_context_params params);
121+
WHISPER_API struct whisper_context * whisper_init_with_params_no_state(struct whisper_model_loader * loader, struct whisper_context_params params);
122+
123+
WHISPER_DEPRECATED(
124+
WHISPER_API struct whisper_context * whisper_init_from_file(const char * path_model),
125+
"use whisper_init_from_file_with_params instead"
126+
);
127+
WHISPER_DEPRECATED(
128+
WHISPER_API struct whisper_context * whisper_init_from_buffer(void * buffer, size_t buffer_size),
129+
"use whisper_init_from_buffer_with_params instead"
130+
);
131+
WHISPER_DEPRECATED(
132+
WHISPER_API struct whisper_context * whisper_init(struct whisper_model_loader * loader, whisper_context_params params),
133+
"use whisper_init_with_params instead"
134+
);
135+
WHISPER_DEPRECATED(
136+
WHISPER_API struct whisper_context * whisper_init_from_file_no_state(const char * path_model),
137+
"use whisper_init_from_file_with_params_no_state instead"
138+
);
139+
WHISPER_DEPRECATED(
140+
WHISPER_API struct whisper_context * whisper_init_from_buffer_no_state(void * buffer, size_t buffer_size),
141+
"use whisper_init_from_buffer_with_params_no_state instead"
142+
);
143+
WHISPER_DEPRECATED(
144+
WHISPER_API struct whisper_context * whisper_init_no_state(struct whisper_model_loader * loader, whisper_context_params params),
145+
"use whisper_init_with_params_no_state instead"
146+
);
114147

115148
WHISPER_API struct whisper_state * whisper_init_state(struct whisper_context * ctx);
116149

0 commit comments

Comments
 (0)