Skip to content

Add base framework for flat bindings #3068

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ option(WHISPER_ALL_WARNINGS_3RD_PARTY "whisper: enable all compiler warnings in
option(WHISPER_FATAL_WARNINGS "whisper: enable -Werror flag" OFF)
option(WHISPER_USE_SYSTEM_GGML "whisper: use system-installed GGML library" OFF)

# flat bindings
option(BINDINGS_FLAT "Add extra flat definitions to Whisper + GGML" OFF)

# sanitizers
option(WHISPER_SANITIZE_THREAD "whisper: enable thread sanitizer" OFF)
option(WHISPER_SANITIZE_ADDRESS "whisper: enable address sanitizer" OFF)
Expand Down
7 changes: 7 additions & 0 deletions examples/bench/bench.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include "whisper.h"
#if defined(GGML_BACKEND_DL) or defined(BINDINGS_FLAT)
#include "whisper-flat.h"
#endif

#include <cstdio>
#include <cstring>
Expand Down Expand Up @@ -61,6 +64,10 @@ void whisper_print_usage(int /*argc*/, char ** argv, const whisper_params & para
static int whisper_bench_full(const whisper_params & params) {
// whisper init

#if defined(GGML_BACKEND_DL) or defined(BINDINGS_FLAT)
whisper_flat_backend_load_all();
#endif

struct whisper_context_params cparams = whisper_context_default_params();

cparams.use_gpu = params.use_gpu;
Expand Down
7 changes: 7 additions & 0 deletions examples/cli/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#include "common-whisper.h"

#include "whisper.h"
#if defined(GGML_BACKEND_DL) or defined(BINDINGS_FLAT)
#include "whisper-flat.h"
#endif
#include "grammar-parser.h"

#include <cmath>
Expand Down Expand Up @@ -1004,6 +1007,10 @@ int main(int argc, char ** argv) {

// whisper init

#if defined(GGML_BACKEND_DL) or defined(BINDINGS_FLAT)
whisper_flat_backend_load_all();
#endif

struct whisper_context_params cparams = whisper_context_default_params();

cparams.use_gpu = params.use_gpu;
Expand Down
7 changes: 7 additions & 0 deletions examples/command/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#include "common-sdl.h"
#include "common.h"
#include "whisper.h"
#if defined(GGML_BACKEND_DL) or defined(BINDINGS_FLAT)
#include "whisper-flat.h"
#endif
#include "grammar-parser.h"

#include <algorithm>
Expand Down Expand Up @@ -692,6 +695,10 @@ int main(int argc, char ** argv) {

// whisper init

#if defined(GGML_BACKEND_DL) or defined(BINDINGS_FLAT)
whisper_flat_backend_load_all();
#endif

struct whisper_context_params cparams = whisper_context_default_params();

cparams.use_gpu = params.use_gpu;
Expand Down
7 changes: 7 additions & 0 deletions examples/server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#include "common-whisper.h"

#include "whisper.h"
#if defined(GGML_BACKEND_DL) or defined(BINDINGS_FLAT)
#include "whisper-flat.h"
#endif
#include "httplib.h"
#include "json.hpp"

Expand Down Expand Up @@ -544,6 +547,10 @@ int main(int argc, char ** argv) {
check_ffmpeg_availibility();
}
// whisper init
#if defined(GGML_BACKEND_DL) or defined(BINDINGS_FLAT)
whisper_flat_backend_load_all();
#endif

struct whisper_context_params cparams = whisper_context_default_params();

cparams.use_gpu = params.use_gpu;
Expand Down
7 changes: 7 additions & 0 deletions examples/stream/stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include "common.h"
#include "common-whisper.h"
#include "whisper.h"
#if defined(GGML_BACKEND_DL) or defined(BINDINGS_FLAT)
#include "whisper-flat.h"
#endif

#include <chrono>
#include <cstdio>
Expand Down Expand Up @@ -155,6 +158,10 @@ int main(int argc, char ** argv) {
exit(0);
}

#if defined(GGML_BACKEND_DL) or defined(BINDINGS_FLAT)
whisper_flat_backend_load_all();
#endif

struct whisper_context_params cparams = whisper_context_default_params();

cparams.use_gpu = params.use_gpu;
Expand Down
7 changes: 7 additions & 0 deletions examples/talk-llama/talk-llama.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include "common.h"
#include "common-whisper.h"
#include "whisper.h"
#if defined(GGML_BACKEND_DL) or defined(BINDINGS_FLAT)
#include "whisper-flat.h"
#endif
#include "llama.h"

#include <chrono>
Expand Down Expand Up @@ -287,6 +290,10 @@ int main(int argc, char ** argv) {

// whisper init

#if defined(GGML_BACKEND_DL) or defined(BINDINGS_FLAT)
whisper_flat_backend_load_all();
#endif

struct whisper_context_params cparams = whisper_context_default_params();

cparams.use_gpu = params.use_gpu;
Expand Down
7 changes: 7 additions & 0 deletions examples/wchess/wchess.cmd/wchess.cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

#include "WChess.h"
#include "common-sdl.h"
#if defined(GGML_BACKEND_DL) or defined(BINDINGS_FLAT)
#include "whisper-flat.h"
#endif
#include <iostream>

#include <memory>
Expand Down Expand Up @@ -182,6 +185,10 @@ int main(int argc, char ** argv) {

// whisper init

#if defined(GGML_BACKEND_DL) or defined(BINDINGS_FLAT)
whisper_flat_backend_load_all();
#endif

struct whisper_context_params cparams = whisper_context_default_params();

cparams.use_gpu = params.use_gpu;
Expand Down
2 changes: 2 additions & 0 deletions ggml/include/ggml-backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,8 @@ extern "C" {
// CPU buffer types are always available
GGML_API ggml_backend_buffer_t ggml_backend_cpu_buffer_from_ptr(void * ptr, size_t size);
GGML_API ggml_backend_buffer_type_t ggml_backend_cpu_buffer_type(void);

GGML_API ggml_backend_reg_t ggml_backend_try_load_best(const char * name, const char * dir_path);

#ifdef __cplusplus
}
Expand Down
16 changes: 15 additions & 1 deletion ggml/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,22 @@ if (GGML_BACKEND_DL)
target_compile_definitions(ggml-base PUBLIC GGML_BACKEND_DL)
endif()

set(GGML_LIBRARY_SOURCES
ggml-backend-reg.cpp)

if(BINDINGS_FLAT)
message(STATUS "Adding FLAT GGML binding extras")

set(FLAT_GGML_SOURCES
ggml-flat.cpp
)

list(APPEND GGML_LIBRARY_SOURCES ${FLAT_GGML_SOURCES})
endif()

add_library(ggml
ggml-backend-reg.cpp)
${GGML_LIBRARY_SOURCES}
)

target_link_libraries(ggml PUBLIC ggml-base)

Expand Down
10 changes: 10 additions & 0 deletions ggml/src/ggml-backend-reg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -584,3 +584,13 @@ void ggml_backend_load_all_from_path(const char * dir_path) {
ggml_backend_load(backend_path);
}
}

ggml_backend_reg_t ggml_backend_try_load_best(const char * name, const char * dir_path) {
#ifdef NDEBUG
bool silent = true;
#else
bool silent = false;
#endif

return ggml_backend_load_best(name, silent, dir_path);
}
35 changes: 35 additions & 0 deletions ggml/src/ggml-flat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#ifndef NOMINMAX
# define NOMINMAX
#endif
#include <windows.h>
#endif

#include "ggml-backend.h"
#include "ggml-backend-impl.h"
#include "ggml-alloc.h"
#include "ggml-impl.h"

#include <assert.h>
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#include <algorithm>

#ifdef __APPLE__
#include <sys/types.h>
#include <sys/sysctl.h>
#endif

#include "ggml-flat.h"





28 changes: 28 additions & 0 deletions ggml/src/ggml-flat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#ifdef BINDINGS_FLAT
#endif

#ifdef GGML_SHARED
# if defined(_WIN32) && !defined(__MINGW32__)
# ifdef GGML_BUILD
# define GGML_FLAT_API __declspec(dllexport) extern
# else
# define GGML_FLAT_API __declspec(dllimport) extern
# endif
# else
# define GGML_FLAT_API __attribute__ ((visibility ("default"))) extern
# endif
#else
# define GGML_FLAT_API extern
#endif

#ifdef __cplusplus
extern "C" {
#endif



#ifdef __cplusplus
}
#endif
8 changes: 5 additions & 3 deletions ggml/src/ggml-opencl/ggml-opencl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
#define CL_USE_DEPRECATED_OPENCL_1_2_APIS

// suppress warnings in CL headers for GCC and Clang
#pragma GCC diagnostic ignored "-Woverlength-strings"
#ifdef __clang__
#pragma GCC diagnostic ignored "-Wgnu-anonymous-struct"
#ifndef _MSC_VER
#pragma GCC diagnostic ignored "-Woverlength-strings"
#ifdef __clang__
#pragma GCC diagnostic ignored "-Wgnu-anonymous-struct"
#endif
#endif

#include "ggml-opencl.h"
Expand Down
9 changes: 9 additions & 0 deletions include/whisper.h
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,15 @@ extern "C" {
// Get the no_speech probability for the specified segment
WHISPER_API float whisper_full_get_segment_no_speech_prob (struct whisper_context * ctx, int i_segment);
WHISPER_API float whisper_full_get_segment_no_speech_prob_from_state(struct whisper_state * state, int i_segment);

// For whisper-flat.cpp to expose
const char * whisper_get_system_info_json(void);
struct whisper_state * whisper_get_state_from_context(struct whisper_context * ctx);
struct whisper_timings * whisper_get_timings_with_state(struct whisper_state * state);
ggml_backend_t whisper_get_preferred_backend(struct whisper_state * state);
ggml_backend_t whisper_get_indexed_backend(struct whisper_state* state, size_t i);
size_t whisper_get_backend_count(struct whisper_state* state);

#ifdef __cplusplus
}
#endif
Expand Down
20 changes: 17 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,24 @@ endif()

# whisper

set(WHISPER_LIBRARY_SOURCES
../include/whisper.h
whisper-arch.h
whisper.cpp
)

if(BINDINGS_FLAT)
message(STATUS "Adding FLAT Whisper binding extras")

set(FLAT_WHISPER_SOURCES
whisper-flat.cpp
)

list(APPEND WHISPER_LIBRARY_SOURCES ${FLAT_WHISPER_SOURCES})
endif()

add_library(whisper
../include/whisper.h
whisper-arch.h
whisper.cpp
${WHISPER_LIBRARY_SOURCES}
)

# Set the version numbers
Expand Down
58 changes: 58 additions & 0 deletions src/whisper-flat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include "whisper.h"
#include "whisper-arch.h"

#include "ggml.h"
#include "ggml-cpp.h"
#include "ggml-alloc.h"
#include "ggml-backend.h"

#include <atomic>
#include <algorithm>
#include <cassert>
#define _USE_MATH_DEFINES
#include <cmath>
#include <climits>
#include <codecvt>
#include <cstdarg>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <functional>
#include <map>
#include <mutex>
#include <random>
#include <regex>
#include <set>
#include <string>
#include <thread>
#include <vector>

#include "whisper-flat.h"

void whisper_flat_backend_load_all(void) {
ggml_backend_load_all();
}

const char * whisper_flat_get_system_info_json(void) {
return whisper_get_system_info_json();
}

struct whisper_state * whisper_flat_get_state_from_context(struct whisper_context * ctx) {
return whisper_get_state_from_context(ctx);
}

struct whisper_timings * whisper_flat_get_timings_with_state(struct whisper_state * state) {
return whisper_get_timings_with_state(state);
}

ggml_backend_t whisper_flat_get_preferred_backend(struct whisper_state * state) {
return whisper_get_preferred_backend(state);
}

ggml_backend_t whisper_flat_get_indexed_backend(struct whisper_state* state, size_t i) {
return whisper_get_indexed_backend(state, i);
}

size_t whisper_flat_get_backend_count(struct whisper_state* state) {
return whisper_get_backend_count(state);
}
Loading