Skip to content

Commit

Permalink
whisper : new API with params & deprecate old API
Browse files Browse the repository at this point in the history
  • Loading branch information
jhen0409 committed Sep 18, 2023
1 parent 3d1369e commit 5c2325f
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 20 deletions.
2 changes: 1 addition & 1 deletion examples/bench/bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ int whisper_bench_full(const whisper_params & params) {

struct whisper_context_params cparams;
cparams.use_gpu = params.use_gpu;
struct whisper_context * ctx = whisper_init_from_file(params.model.c_str(), cparams);
struct whisper_context * ctx = whisper_init_from_file_with_params(params.model.c_str(), cparams);

{
fprintf(stderr, "\n");
Expand Down
2 changes: 1 addition & 1 deletion examples/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ int main(int argc, char ** argv) {

struct whisper_context_params cparams;
cparams.use_gpu = params.use_gpu;
struct whisper_context * ctx = whisper_init_from_file(params.model.c_str(), cparams);
struct whisper_context * ctx = whisper_init_from_file_with_params(params.model.c_str(), cparams);

if (ctx == nullptr) {
fprintf(stderr, "error: failed to initialize whisper context\n");
Expand Down
53 changes: 42 additions & 11 deletions whisper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3032,7 +3032,14 @@ int whisper_ctx_init_openvino_encoder(
#endif
}

struct whisper_context * whisper_init_from_file_no_state(const char * path_model, whisper_context_params params) {
struct whisper_context_params whisper_context_default_params() {
struct whisper_context_params result = {
/*.use_gpu =*/ true,
};
return result;
}

struct whisper_context * whisper_init_from_file_with_params_no_state(const char * path_model, struct whisper_context_params params) {
log("%s: loading model from '%s'\n", __func__, path_model);

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

auto ctx = whisper_init_no_state(&loader, params);
auto ctx = whisper_init_with_params_no_state(&loader, params);

if (ctx) {
ctx->path_model = path_model;
Expand All @@ -3070,7 +3077,7 @@ struct whisper_context * whisper_init_from_file_no_state(const char * path_model
return ctx;
}

struct whisper_context * whisper_init_from_buffer_no_state(void * buffer, size_t buffer_size, whisper_context_params params) {
struct whisper_context * whisper_init_from_buffer_with_params_no_state(void * buffer, size_t buffer_size, struct whisper_context_params params) {
struct buf_context {
uint8_t* buffer;
size_t size;
Expand Down Expand Up @@ -3104,10 +3111,10 @@ struct whisper_context * whisper_init_from_buffer_no_state(void * buffer, size_t

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

return whisper_init_no_state(&loader, params);
return whisper_init_with_params_no_state(&loader, params);
}

struct whisper_context * whisper_init_no_state(struct whisper_model_loader * loader, whisper_context_params params) {
struct whisper_context * whisper_init_with_params_no_state(struct whisper_model_loader * loader, struct whisper_context_params params) {
ggml_time_init();

whisper_context * ctx = new whisper_context;
Expand All @@ -3125,8 +3132,8 @@ struct whisper_context * whisper_init_no_state(struct whisper_model_loader * loa
return ctx;
}

struct whisper_context * whisper_init_from_file(const char * path_model, whisper_context_params params) {
whisper_context * ctx = whisper_init_from_file_no_state(path_model, params);
struct whisper_context * whisper_init_from_file_with_params(const char * path_model, struct whisper_context_params params) {
whisper_context * ctx = whisper_init_from_file_with_params_no_state(path_model, params);
if (!ctx) {
return nullptr;
}
Expand All @@ -3140,8 +3147,8 @@ struct whisper_context * whisper_init_from_file(const char * path_model, whisper
return ctx;
}

struct whisper_context * whisper_init_from_buffer(void * buffer, size_t buffer_size, whisper_context_params params) {
whisper_context * ctx = whisper_init_from_buffer_no_state(buffer, buffer_size, params);
struct whisper_context * whisper_init_from_buffer_with_params(void * buffer, size_t buffer_size, struct whisper_context_params params) {
whisper_context * ctx = whisper_init_from_buffer_with_params_no_state(buffer, buffer_size, params);
if (!ctx) {
return nullptr;
}
Expand All @@ -3155,8 +3162,8 @@ struct whisper_context * whisper_init_from_buffer(void * buffer, size_t buffer_s
return ctx;
}

struct whisper_context * whisper_init(struct whisper_model_loader * loader, whisper_context_params params) {
whisper_context * ctx = whisper_init_no_state(loader, params);
struct whisper_context * whisper_init_with_params(struct whisper_model_loader * loader, struct whisper_context_params params) {
whisper_context * ctx = whisper_init_with_params_no_state(loader, params);
if (!ctx) {
return nullptr;
}
Expand All @@ -3170,6 +3177,30 @@ struct whisper_context * whisper_init(struct whisper_model_loader * loader, whis
return ctx;
}

struct whisper_context * whisper_init_from_file(const char * path_model) {
return whisper_init_from_file_with_params(path_model, whisper_context_default_params());
}

struct whisper_context * whisper_init_from_buffer(void * buffer, size_t buffer_size) {
return whisper_init_from_buffer_with_params(buffer, buffer_size, whisper_context_default_params());
}

struct whisper_context * whisper_init(struct whisper_model_loader * loader) {
return whisper_init_with_params(loader, whisper_context_default_params());
}

struct whisper_context * whisper_init_from_file_no_state(const char * path_model) {
return whisper_init_from_file_with_params_no_state(path_model, whisper_context_default_params());
}

struct whisper_context * whisper_init_from_buffer_no_state(void * buffer, size_t buffer_size) {
return whisper_init_from_buffer_with_params_no_state(buffer, buffer_size, whisper_context_default_params());
}

struct whisper_context * whisper_init_no_state(struct whisper_model_loader * loader) {
return whisper_init_with_params_no_state(loader, whisper_context_default_params());
}

void whisper_free_state(struct whisper_state * state)
{
if (state) {
Expand Down
47 changes: 40 additions & 7 deletions whisper.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
#include <stdint.h>
#include <stdbool.h>

#ifdef __GNUC__
# define WHISPER_DEPRECATED(func, hint) func __attribute__((deprecated(hint)))
#elif defined(_MSC_VER)
# define WHISPER_DEPRECATED(func, hint) __declspec(deprecated(hint)) func
#else
# define WHISPER_DEPRECATED(func, hint) func
#endif

#ifdef WHISPER_SHARED
# ifdef _WIN32
# ifdef WHISPER_BUILD
Expand Down Expand Up @@ -66,7 +74,7 @@ extern "C" {
//

struct whisper_context_params {
bool use_gpu = true;
bool use_gpu;
};
struct whisper_context;
struct whisper_state;
Expand Down Expand Up @@ -102,15 +110,40 @@ extern "C" {
// Various functions for loading a ggml whisper model.
// Allocate (almost) all memory needed for the model.
// Return NULL on failure
WHISPER_API struct whisper_context * whisper_init_from_file(const char * path_model, whisper_context_params params);
WHISPER_API struct whisper_context * whisper_init_from_buffer(void * buffer, size_t buffer_size, whisper_context_params params);
WHISPER_API struct whisper_context * whisper_init(struct whisper_model_loader * loader, whisper_context_params params);
WHISPER_API struct whisper_context * whisper_init_from_file_with_params(const char * path_model, struct whisper_context_params params);
WHISPER_API struct whisper_context * whisper_init_from_buffer_with_params(void * buffer, size_t buffer_size, struct whisper_context_params params);
WHISPER_API struct whisper_context * whisper_init_with_params(struct whisper_model_loader * loader, struct whisper_context_params params);

// These are the same as the above, but the internal state of the context is not allocated automatically
// It is the responsibility of the caller to allocate the state using whisper_init_state() (#523)
WHISPER_API struct whisper_context * whisper_init_from_file_no_state(const char * path_model, whisper_context_params params);
WHISPER_API struct whisper_context * whisper_init_from_buffer_no_state(void * buffer, size_t buffer_size, whisper_context_params params);
WHISPER_API struct whisper_context * whisper_init_no_state(struct whisper_model_loader * loader, whisper_context_params params);
WHISPER_API struct whisper_context * whisper_init_from_file_with_params_no_state(const char * path_model, struct whisper_context_params params);
WHISPER_API struct whisper_context * whisper_init_from_buffer_with_params_no_state(void * buffer, size_t buffer_size, struct whisper_context_params params);
WHISPER_API struct whisper_context * whisper_init_with_params_no_state(struct whisper_model_loader * loader, struct whisper_context_params params);

WHISPER_DEPRECATED(
WHISPER_API struct whisper_context * whisper_init_from_file(const char * path_model),
"use whisper_init_from_file_with_params instead"
);
WHISPER_DEPRECATED(
WHISPER_API struct whisper_context * whisper_init_from_buffer(void * buffer, size_t buffer_size),
"use whisper_init_from_buffer_with_params instead"
);
WHISPER_DEPRECATED(
WHISPER_API struct whisper_context * whisper_init(struct whisper_model_loader * loader, whisper_context_params params),
"use whisper_init_with_params instead"
);
WHISPER_DEPRECATED(
WHISPER_API struct whisper_context * whisper_init_from_file_no_state(const char * path_model),
"use whisper_init_from_file_with_params_no_state instead"
);
WHISPER_DEPRECATED(
WHISPER_API struct whisper_context * whisper_init_from_buffer_no_state(void * buffer, size_t buffer_size),
"use whisper_init_from_buffer_with_params_no_state instead"
);
WHISPER_DEPRECATED(
WHISPER_API struct whisper_context * whisper_init_no_state(struct whisper_model_loader * loader, whisper_context_params params),
"use whisper_init_with_params_no_state instead"
);

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

Expand Down

0 comments on commit 5c2325f

Please sign in to comment.