Skip to content

Commit

Permalink
Sync with whisper.cpp 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Nov 25, 2023
1 parent ac3558c commit fd70d31
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/whisper/bindings/javascript/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "whisper.cpp",
"version": "1.5.0",
"version": "1.5.1",
"description": "Whisper speech recognition",
"main": "whisper.js",
"scripts": {
Expand Down
5 changes: 4 additions & 1 deletion src/whisper/examples/server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ struct whisper_params {
bool no_fallback = false;
bool print_special = false;
bool print_colors = false;
bool print_realtime = false;
bool print_progress = false;
bool no_timestamps = false;
bool use_gpu = true;
Expand Down Expand Up @@ -144,6 +145,7 @@ void whisper_print_usage(int /*argc*/, char ** argv, const whisper_params & para
fprintf(stderr, " -nf, --no-fallback [%-7s] do not use temperature fallback while decoding\n", params.no_fallback ? "true" : "false");
fprintf(stderr, " -ps, --print-special [%-7s] print special tokens\n", params.print_special ? "true" : "false");
fprintf(stderr, " -pc, --print-colors [%-7s] print colors\n", params.print_colors ? "true" : "false");
fprintf(stderr, " -pr, --print-realtime [%-7s] print output in realtime\n", params.print_realtime ? "true" : "false");
fprintf(stderr, " -pp, --print-progress [%-7s] print progress\n", params.print_progress ? "true" : "false");
fprintf(stderr, " -nt, --no-timestamps [%-7s] do not print timestamps\n", params.no_timestamps ? "true" : "false");
fprintf(stderr, " -l LANG, --language LANG [%-7s] spoken language ('auto' for auto-detect)\n", params.language.c_str());
Expand Down Expand Up @@ -188,6 +190,7 @@ bool whisper_params_parse(int argc, char ** argv, whisper_params & params, serve
else if (arg == "-fp" || arg == "--font-path") { params.font_path = argv[++i]; }
else if (arg == "-ps" || arg == "--print-special") { params.print_special = true; }
else if (arg == "-pc" || arg == "--print-colors") { params.print_colors = true; }
else if (arg == "-pr" || arg == "--print-realtime") { params.print_realtime = true; }
else if (arg == "-pp" || arg == "--print-progress") { params.print_progress = true; }
else if (arg == "-nt" || arg == "--no-timestamps") { params.no_timestamps = true; }
else if (arg == "-l" || arg == "--language") { params.language = argv[++i]; }
Expand Down Expand Up @@ -544,7 +547,7 @@ int main(int argc, char ** argv) {
whisper_print_user_data user_data = { &params, &pcmf32s, 0 };

// this callback is called on each new segment
if (!wparams.print_realtime) {
if (params.print_realtime) {
wparams.new_segment_callback = whisper_print_segment_callback;
wparams.new_segment_callback_user_data = &user_data;
}
Expand Down
94 changes: 62 additions & 32 deletions src/whisper/ggml-cuda.cu

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions src/whisper/ggml-metal.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ void ggml_metal_free(struct ggml_metal_context * ctx);
void * ggml_metal_host_malloc(size_t n);
void ggml_metal_host_free (void * data);

// helper to check if the device supports a specific family
// ideally, the user code should be doing these checks
// ref: https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
bool ggml_metal_supports_family(struct ggml_metal_context * ctx, int family);

// set the number of command buffers to use
void ggml_metal_set_n_cb(struct ggml_metal_context * ctx, int n_cb);

Expand Down Expand Up @@ -100,6 +105,8 @@ GGML_API bool ggml_backend_is_metal(ggml_backend_t backend);

GGML_API void ggml_backend_metal_set_n_cb(ggml_backend_t backend, int n_cb);

GGML_API bool ggml_backend_metal_supports_family(ggml_backend_t backend, int family);

#ifdef __cplusplus
}
#endif
Expand Down
10 changes: 10 additions & 0 deletions src/whisper/ggml-metal.m
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,10 @@ void ggml_metal_host_free(void * data) {
free(data);
}

bool ggml_metal_supports_family(struct ggml_metal_context * ctx, int family) {
return [ctx->device supportsFamily:(MTLGPUFamilyApple1 + family - 1)];
}

void ggml_metal_set_n_cb(struct ggml_metal_context * ctx, int n_cb) {
ctx->n_cb = MIN(n_cb, GGML_METAL_MAX_BUFFERS);
}
Expand Down Expand Up @@ -1751,3 +1755,9 @@ void ggml_backend_metal_set_n_cb(ggml_backend_t backend, int n_cb) {

ggml_metal_set_n_cb(ctx, n_cb);
}

bool ggml_backend_metal_supports_family(ggml_backend_t backend, int family) {
struct ggml_metal_context * ctx = (struct ggml_metal_context *)backend->context;

return ggml_metal_supports_family(ctx, family);
}
18 changes: 17 additions & 1 deletion src/whisper/whisper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,11 @@ static ggml_backend_t whisper_backend_init(const whisper_context_params & params
if (!backend_gpu) {
WHISPER_LOG_ERROR("%s: ggml_backend_metal_init() failed\n", __func__);
}
if (!ggml_backend_metal_supports_family(backend_gpu, 7)) {
WHISPER_LOG_ERROR("%s: Metal GPU does not support family 7 - falling back to CPU\n", __func__);
ggml_backend_free(backend_gpu);
backend_gpu = NULL;
}
}
#endif

Expand Down Expand Up @@ -3593,6 +3598,17 @@ const char * whisper_lang_str(int id) {
return nullptr;
}

const char * whisper_lang_str_full(int id) {
for (const auto & kv : g_lang) {
if (kv.second.first == id) {
return kv.second.second.c_str();
}
}

WHISPER_LOG_ERROR("%s: unknown language id %d\n", __func__, id);
return nullptr;
}

int whisper_lang_auto_detect_with_state(
struct whisper_context * ctx,
struct whisper_state * state,
Expand Down Expand Up @@ -5180,7 +5196,7 @@ int whisper_full_with_state(
ctx, state, progress_cur, params.progress_callback_user_data);
}

// of only 1 second left, then stop
// if only 1 second left, then stop
if (seek + 100 >= seek_end) {
break;
}
Expand Down
5 changes: 4 additions & 1 deletion src/whisper/whisper.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ extern "C" {
// ...
//
// whisper_context_params cparams = whisper_context_default_params();
//
//
// struct whisper_context * ctx = whisper_init_from_file_with_params("/path/to/ggml-base.en.bin", cparams);
//
// if (whisper_full(ctx, wparams, pcmf32.data(), pcmf32.size()) != 0) {
Expand Down Expand Up @@ -315,6 +315,9 @@ extern "C" {
// Return the short string of the specified language id (e.g. 2 -> "de"), returns nullptr if not found
WHISPER_API const char * whisper_lang_str(int id);

// Return the short string of the specified language name (e.g. 2 -> "german"), returns nullptr if not found
WHISPER_API const char * whisper_lang_str_full(int id);

// Use mel data at offset_ms to try and auto-detect the spoken language
// Make sure to call whisper_pcm_to_mel() or whisper_set_mel() first
// Returns the top language id or negative on failure
Expand Down

0 comments on commit fd70d31

Please sign in to comment.