Skip to content
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

Support for OpenVINO #192

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ COPTS = [
"-std=c++11",
"-fPIC",
"-Wall",
"-DWHISPER_USE_OPENVINO",
] + selects.with_or({
"//conditions:default": [],
"@bazel_tools//src/conditions:linux_x86_64": [
Expand Down
54 changes: 54 additions & 0 deletions extern/openvino.BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
load("@bazel_skylib//lib:selects.bzl", "selects")

package(
default_visibility = ["//visibility:public"],
)

cc_library(
name = "openvino_old_headers",
hdrs = glob([
"include/ie/**/*.*"
]),
strip_include_prefix = "include/ie",
visibility = ["//visibility:public"],
)

cc_library(
name = "openvino_new_headers",
hdrs = glob([
#"include/openvino/**/*.*"
"include/**/*.*"
]),
strip_include_prefix = "include",
visibility = ["//visibility:public"],
deps = [
"@linux_openvino//:openvino_old_headers",
]
)

cc_library(
name = "ngraph",
hdrs = glob([
"include/ngraph/**/*.*"
]),
strip_include_prefix = "include",
visibility = ["//visibility:public"],
)

cc_library(
name = "openvino",
srcs = selects.with_or({
"@bazel_tools//src/conditions:linux_x86_64": [
"lib/intel64/libopenvino.so",
],
"@bazel_tools//src/conditions:linux_aarch64": [
"lib/aarch64/libopenvino.so",
],
}),
strip_include_prefix = "include/ie",
visibility = ["//visibility:public"],
deps = [
"@linux_openvino//:ngraph",
"@linux_openvino//:openvino_new_headers",
],
)
25 changes: 21 additions & 4 deletions extern/whispercpp.BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ HEADERS = [
"whisper.h",
]

OPENVINO_HEADERS = glob(["openvino/*.h"])
OPENVINO_SOURCES = glob(["openvino/*.cpp"])

EXAMPLE_HEADERS = [
"examples/common-sdl.h",
"examples/common.h",
Expand All @@ -30,6 +33,10 @@ CFLAGS = [
"-std=c11",
"-fPIC",
"-pthread",
"-DWHISPER_USE_OPENVINO",
"-D_POSIX_C_SOURCE=200809L",
"-D_USE_MATH_DEFINES",
"-D_GNU_SOURCE",
]

CXXFLAGS = [
Expand All @@ -39,6 +46,10 @@ CXXFLAGS = [
"-std=c++11",
"-fPIC",
"-pthread",
"-DWHISPER_USE_OPENVINO",
"-D_POSIX_C_SOURCE=200809L",
"-D_USE_MATH_DEFINES",
"-D_GNU_SOURCE",
]

cc_library(
Expand Down Expand Up @@ -90,7 +101,9 @@ cc_library(

cc_library(
name = "ggml",
srcs = ["ggml.c"],
srcs = [
"ggml.c",
],
hdrs = HEADERS,
copts = CFLAGS + selects.with_or({
"//conditions:default": [],
Expand All @@ -113,8 +126,8 @@ cc_library(

cc_library(
name = "whisper",
srcs = ["whisper.cpp"],
hdrs = HEADERS + EXAMPLE_HEADERS,
srcs = ["whisper.cpp"] + OPENVINO_SOURCES,
hdrs = HEADERS + EXAMPLE_HEADERS + OPENVINO_HEADERS,
copts = CXXFLAGS + selects.with_or({
"//conditions:default": [],
"@bazel_tools//src/conditions:linux_x86_64": [
Expand All @@ -132,5 +145,9 @@ cc_library(
"Accelerate",
],
}),
deps = [":ggml"],
deps = [
":ggml",
"@linux_openvino//:openvino_new_headers",
"@linux_openvino//:openvino",
],
)
8 changes: 7 additions & 1 deletion rules/deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,16 @@ def internal_deps():
init_submodules = True,
recursive_init_submodules = True,
remote = "https://github.com/ggerganov/whisper.cpp.git",
commit = "2f889132c66051b14c6f8770e9b3d4e3f159821d",
commit = "c5f9acf4b79780171e25b91d227c615e828568f6",
shallow_since = "1678217790 +0200",
)

native.new_local_repository(
name = "linux_openvino",
path = "/home/pi/code/v3/openvino/l_openvino_toolkit_debian9_2023.2.0.13089.cfd42bd2cb0_arm64/runtime/",
build_file = "//extern:openvino.BUILD",
)

http_archive(
name = "com_github_libsdl_sdl2",
build_file = Label("//extern:sdl2.BUILD"),
Expand Down
1 change: 0 additions & 1 deletion src/whispercpp/api.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ from numpy.typing import NDArray

SAMPLE_RATE: int = ...
N_FFT: int = ...
N_MEL: int = ...
HOP_LENGTH: int = ...
CHUNK_SIZE: int = ...

Expand Down
1 change: 0 additions & 1 deletion src/whispercpp/api_cpp2py_export.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ PYBIND11_MODULE(api_cpp2py_export, m) {
// NOTE: default attributes
m.attr("SAMPLE_RATE") = py::int_(WHISPER_SAMPLE_RATE);
m.attr("N_FFT") = py::int_(WHISPER_N_FFT);
m.attr("N_MEL") = py::int_(WHISPER_N_MEL);
m.attr("HOP_LENGTH") = py::int_(WHISPER_HOP_LENGTH);
m.attr("CHUNK_SIZE") = py::int_(WHISPER_CHUNK_SIZE);

Expand Down
4 changes: 4 additions & 0 deletions src/whispercpp/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Context Context::from_file(const char *filename, bool no_state) {
c.set_init_with_state(true);
}
RAISE_IF_NULL(c.wctx);
whisper_ctx_init_openvino_encoder(c.wctx, nullptr, "CPU", nullptr);
return c;
}

Expand All @@ -81,6 +82,7 @@ Context Context::from_buffer(void *buffer, size_t buffer_size, bool no_state) {
c.set_init_with_state(true);
}
RAISE_IF_NULL(c.wctx);
whisper_ctx_init_openvino_encoder(c.wctx, nullptr, "CPU", nullptr);
return c;
}

Expand Down Expand Up @@ -303,6 +305,8 @@ size_t Context::n_len() {
}
}

// Get number of mels per segment
size_t Context::model_n_mels() { return whisper_model_n_mels(wctx); }
// Get number of vocab
size_t Context::n_vocab() { return whisper_n_vocab(wctx); }
// Get number of text context
Expand Down
5 changes: 3 additions & 2 deletions src/whispercpp/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@ struct Context {
// language functions. Returns a vector of probabilities for each language.
std::vector<float> lang_detect(size_t offset_ms, size_t threads);

size_t model_n_mels();
size_t n_len();
size_t n_vocab();
size_t n_text_ctx();
Expand All @@ -551,8 +552,8 @@ struct Context {
whisper_token solm_token() { return whisper_token_solm(wctx); }
whisper_token not_token() { return whisper_token_not(wctx); }
whisper_token beg_token() { return whisper_token_beg(wctx); }
whisper_token token_translate() { return whisper_token_translate(); }
whisper_token token_transcribe() { return whisper_token_transcribe(); }
whisper_token token_translate() { return whisper_token_translate(wctx); }
whisper_token token_transcribe() { return whisper_token_transcribe(wctx); }
whisper_token lang_token(int lang_id) {
return whisper_token_lang(wctx, lang_id);
}
Expand Down