Skip to content

Commit

Permalink
Upgrade whisper.cpp library to 1.7.1
Browse files Browse the repository at this point in the history
  • Loading branch information
GiviMAD committed Jan 3, 2025
1 parent 8b8d23e commit 425a8fe
Show file tree
Hide file tree
Showing 14 changed files with 214 additions and 187 deletions.
67 changes: 47 additions & 20 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
download-model:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Download tiny ggml model
run: |
git submodule update --init
Expand All @@ -23,10 +23,10 @@ jobs:
needs: [download-model]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
- name: Docker Setup Buildx
uses: docker/setup-buildx-action@v1.6.0
uses: docker/setup-buildx-action@v3
with:
install: true
- name: Setup project dir
Expand All @@ -47,12 +47,12 @@ jobs:
needs: [download-model]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
uses: docker/setup-qemu-action@v3
- name: Docker Setup Buildx
uses: docker/setup-buildx-action@v1.6.0
uses: docker/setup-buildx-action@v3
with:
install: true
- name: Setup project dir
Expand All @@ -73,12 +73,12 @@ jobs:
needs: [download-model]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
uses: docker/setup-qemu-action@v3
- name: Docker Setup Buildx
uses: docker/setup-buildx-action@v1.6.0
uses: docker/setup-buildx-action@v3
with:
install: true
- name: Setup project dir
Expand All @@ -99,7 +99,7 @@ jobs:
needs: [download-model]
runs-on: windows-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
- uses: actions/setup-java@v3
with:
Expand All @@ -126,11 +126,11 @@ jobs:
name: windows-binary-x86_64
path: dist/*.dll

build-lib-macos:
build-lib-macos-amd64:
needs: [download-model]
runs-on: macos-latest
runs-on: macos-13
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
- uses: actions/setup-java@v3
with:
Expand All @@ -151,15 +151,41 @@ jobs:
run: |
export DYLD_LIBRARY_PATH=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/
./build_macos.sh amd64
./build_macos.sh arm64
# mvn test
mv src/main/resources/macos-amd64/*.dylib dist/
mv src/main/resources/macos-arm64/*.dylib dist_arm64/
- name: Artifact macos amd64
uses: actions/upload-artifact@v3
with:
name: macos-binary-amd64
path: dist/*.dylib

build-lib-macos-arm64:
needs: [download-model]
runs-on: macos-14
steps:
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
- uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: '17'
# - name: Set up Maven
# uses: stCarolas/[email protected]
# with:
# maven-version: 3.9.2
- name: Setup project dir
run: |
export DYLD_LIBRARY_PATH=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/
mkdir -p dist
mkdir -p dist_arm64
git submodule update --init
mv ./tiny-ggml-model/ggml-tiny.bin ./ggml-tiny.bin
- name: Build binary
run: |
export DYLD_LIBRARY_PATH=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/
./build_macos.sh arm64
# mvn test
mv src/main/resources/macos-arm64/*.dylib dist_arm64/
- name: Artifact macos arm64
uses: actions/upload-artifact@v3
with:
Expand All @@ -168,12 +194,12 @@ jobs:

deploy:
if: startsWith(github.ref, 'refs/tags/')
needs: [build-lib-debian-x86_64,build-lib-debian-arm,build-lib-debian-arm64,build-lib-windows,build-lib-macos]
needs: [build-lib-debian-x86_64,build-lib-debian-arm,build-lib-debian-arm64,build-lib-windows,build-lib-macos-amd64, build-lib-macos-arm64]
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
- name: check tree
run: ls -R
Expand All @@ -189,12 +215,13 @@ jobs:
mv ./macos-binary-amd64/*.dylib ./src/main/resources/macos-amd64/
mv ./macos-binary-arm64/*.dylib ./src/main/resources/macos-arm64/
- name: Set up Maven Central Repository
uses: actions/setup-java@v1
uses: actions/setup-java@v4
with:
java-version: 17
distribution: 'zulu'
server-id: sonatype-nexus-staging
server-username: MAVEN_USERNAME
server-password: MAVEN_PASSWORD
server-password: MAVEN_CENTRAL_TOKEN
gpg-private-key: ${{ secrets.MAVEN_GPG_PRIVATE_KEY }}
gpg-passphrase: MAVEN_GPG_PASSPHRASE
- name: Deploy with Maven
Expand All @@ -207,7 +234,7 @@ jobs:
echo "WHISPER_VERSION=$WHISPER_VERSION" >> $GITHUB_OUTPUT
env:
MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }}
MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }}
MAVEN_CENTRAL_TOKEN: ${{ secrets.OSSRH_TOKEN }}
MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }}
- name: Release
uses: softprops/action-gh-release@v1
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ target/
!**/src/main/**/target/
!**/src/test/**/target/
src/main/resources/**/*.so
src/main/resources/**/*.so.*
src/main/resources/**/*.dylib
src/main/resources/**/*.dll
ggml-*.bin
Expand Down
25 changes: 16 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
cmake_minimum_required(VERSION 3.13)
cmake_minimum_required(VERSION 3.5)

project(whisper-jni C CXX)

# ---- Set options ----

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
option(WHISPER_NO_AVX "whisper: disable AVX" OFF)
option(WHISPER_NO_AVX2 "whisper: disable AVX2" OFF)
option(WHISPER_NO_FMA "whisper: disable FMA" OFF)
option(WHISPER_NO_F16C "whisper: disable F16c" OFF)
option(BUILD_SHARED_LIBS "whisper: build shared libs" ON)

option(GGML_AVX "ggml: enable AVX" ON )
option(GGML_AVX2 "ggml: enable AVX2" ON )
option(GGML_FMA "ggml: enable FMA" ON )
option(GGML_F16C "ggml: enable F16c" ON )
option(GGML_STATIC "ggml: static link libraries" OFF)
option(GGML_NATIVE "ggml: enable -march=native flag" OFF)
option(BUILD_SHARED_LIBS "whisper: build shared libs" ON )

set(JAVA_AWT_LIBRARY NotNeeded)
set(JAVA_JVM_LIBRARY NotNeeded)
Expand All @@ -26,6 +29,7 @@ if(MSVC)
elseif(NOT APPLE)
string(APPEND CMAKE_CXX_FLAGS " -Wall -Wextra -Wl,-rpath,'$ORIGIN'")
string(APPEND CMAKE_C_FLAGS " -Wall -Wextra")
set(CMAKE_INSTALL_RPATH '$ORIGIN')
elseif(APPLE)
set(CMAKE_INSTALL_RPATH "@loader_path")
endif()
Expand All @@ -42,7 +46,8 @@ include(ExternalProject)
whisper-cpp
PREFIX "${WHISPER_INSTALL_DIR}"
URL "${CMAKE_CURRENT_LIST_DIR}/src/main/native/whisper"
CMAKE_ARGS -DBUILD_SHARED_LIBS=${BUILD_SHARED_LIBS} -DCMAKE_C_FLAGS:STRING=${CMAKE_C_FLAGS} -DWHISPER_NO_AVX=${WHISPER_NO_AVX} -DWHISPER_NO_AVX2=${WHISPER_NO_AVX2} -DWHISPER_NO_FMA=${WHISPER_NO_FMA} -DWHISPER_NO_F16C=${WHISPER_NO_F16C} -DWHISPER_STANDALONE=1 -DWHISPER_METAL=0 -DCMAKE_INSTALL_PREFIX:PATH=${WHISPER_DIR} -DCMAKE_OSX_ARCHITECTURES:STRING=${CMAKE_OSX_ARCHITECTURES} -DCMAKE_OSX_DEPLOYMENT_TARGET:STRING=${CMAKE_OSX_DEPLOYMENT_TARGET} )
CMAKE_ARGS -DCMAKE_INSTALL_RPATH:STRING=${CMAKE_INSTALL_RPATH} -DBUILD_SHARED_LIBS=${BUILD_SHARED_LIBS} -DCMAKE_C_FLAGS:STRING=${CMAKE_C_FLAGS} -DGGML_STATIC=${GGML_STATIC} -DGGML_NATIVE=${GGML_NATIVE} -DGGML_AVX=${GGML_AVX} -DGGML_AVX2=${GGML_AVX2} -DGGML_FMA=${GGML_FMA} -DGGML_F16C=${GGML_F16C} -DWHISPER_STANDALONE=1 -DCMAKE_INSTALL_PREFIX:PATH=${WHISPER_DIR} -DCMAKE_OSX_ARCHITECTURES:STRING=${CMAKE_OSX_ARCHITECTURES} -DCMAKE_OSX_DEPLOYMENT_TARGET:STRING=${CMAKE_OSX_DEPLOYMENT_TARGET}
)
add_dependencies(whisper-jni whisper-cpp)
endif()

Expand All @@ -57,6 +62,7 @@ endif()

target_link_libraries(whisper-jni
whisper
ggml
)

target_link_directories(whisper-jni PUBLIC
Expand All @@ -67,7 +73,7 @@ target_link_directories(whisper-jni PUBLIC
target_include_directories(whisper-jni PUBLIC
${JNI_INCLUDE_DIRS}
src/main/native
${WHISPER_INSTALL_DIR}/src/whisper-cpp
${WHISPER_DIR}/include
${WHISPER_INSTALL_DIR}/src/whisper-cpp/examples
)

Expand All @@ -86,6 +92,7 @@ install(
USE_SOURCE_PERMISSIONS
FILES_MATCHING
PATTERN "*.so.*"
PATTERN "*.so"
PATTERN "*.dylib"
)

Expand Down
19 changes: 3 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,11 @@ You can also find the package's jar attached to each [release](https://github.co

### Use external whisper shared library.

It's possible to use your own build of the whisper.cpp shared library with this project.
It's possible to use an external whisper.cpp shared library instead of the one packaged in the jar.

On `Linux/macOs` you need to provide the library path to the `loadLibrary` method.
On linux/mac set the property `io.github.givimad.whisperjni.libdir` to an alternative directory that contains the whisper-jni and whisper-cpp libraries.

```java
...
// load platform binaries
var loadOptions = new WhisperJNI.LoadOptions();
// Log library load to stdout
loadOptions.logger = System.out::println;
// Provide path to libwhisper so/dylib file.
loadOptions.whisperLib = Paths.get("/usr/local/lib/libwhisper.so");
// register the library
WhisperJNI.loadLibrary(loadOptions);
...
```

On `windows` it's automatically used if `whisper.dll` exists in some of the directories in the $env:PATH variable.
On `windows` an external `whisper.dll` it's automatically used if it exists in some of the directories in the $env:PATH variable.

## Basic Example

Expand Down
9 changes: 5 additions & 4 deletions build_debian.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ build_lib() {
cmake -B build $CMAKE_ARGS -DCMAKE_C_FLAGS="$CMAKE_CFLAGS" -DCMAKE_INSTALL_PREFIX=$TMP_DIR
cmake --build build --config Release
cmake --install build
cp $TMP_DIR/libwhisper.so.1 $TARGET_DIR/libwhisper$LIB_VARIANT.so.1
cp $TMP_DIR/libggml.so $TARGET_DIR/libggml$LIB_VARIANT.so
if [ "$ADD_WRAPPER" = true ]; then
cp $TMP_DIR/libwhisper.so.1 $TARGET_DIR/libwhisper.so.1
cp $TMP_DIR/libwhisper-jni.so $TARGET_DIR/libwhisper-jni.so
fi
fi
rm -rf $TMP_DIR
rm -rf build
}
AARCH=$(dpkg --print-architecture)
case $AARCH in
amd64)
LIB_VARIANT="+mf16c+mfma+mavx+mavx2" CMAKE_ARGS="-DWHISPER_NO_AVX=OFF -DWHISPER_NO_AVX2=OFF -DWHISPER_NO_FMA=OFF -DWHISPER_NO_F16C=OFF" build_lib
ADD_WRAPPER=true CMAKE_ARGS="-DWHISPER_NO_AVX=ON -DWHISPER_NO_AVX2=ON -DWHISPER_NO_FMA=ON -DWHISPER_NO_F16C=ON" build_lib
LIB_VARIANT="+mf16c+mfma+mavx+mavx2" CMAKE_ARGS="-DGGML_AVX=ON -DGGML_AVX2=ON -DGGML_FMA=ON -DGGML_F16C=ON" build_lib
ADD_WRAPPER=true CMAKE_ARGS="-DGGML_AVX=OFF -DGGML_AVX2=OFF -DGGML_FMA=OFF -DGGML_F16C=OFF" build_lib
;;
arm64)
LIB_VARIANT="+fp16" CMAKE_CFLAGS="-march=armv8.2-a+fp16" build_lib
Expand Down
1 change: 1 addition & 0 deletions build_macos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ cmake -Bbuild -DCMAKE_INSTALL_PREFIX=$TMP_DIR -DCMAKE_OSX_DEPLOYMENT_TARGET=$TAR
cmake --build build --config Release
cmake --install build
rm -rf build
cp $TMP_DIR/libggml.dylib $TARGET_DIR
cp $TMP_DIR/libwhisper.1.dylib $TARGET_DIR
cp $TMP_DIR/libwhisper-jni.dylib $TARGET_DIR
rm -rf $TMP_DIR
3 changes: 2 additions & 1 deletion build_win.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# build full version
cmake -B build -DCMAKE_INSTALL_PREFIX=src/main/resources/win-amd64 -DBUILD_SHARED_LIBS=0
cmake -B build -DCMAKE_INSTALL_PREFIX=src/main/resources/win-amd64 -DBUILD_SHARED_LIBS=0 -DGGML_STATIC=1
cmake --build build --config Release
cmake --install build
rm -r -fo build
Expand All @@ -11,3 +11,4 @@ cmake --install build
rm -r -fo build
rm -r -fo src/main/resources/win-amd64/*.lib
rm -r -fo src/main/resources/win-amd64/whisper.dll
rm -r -fo src/main/resources/win-amd64/ggml.dll
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<artifactId>whisper-jni</artifactId>
<name>whisper-jni</name>
<url>https://github.com/GiviMAD/whisper-jni</url>
<version>1.6.1</version>
<version>1.7.1</version>
<description>A JNI wrapper for [whisper.cpp](https://github.com/ggerganov/whisper.cpp), allows to transcribe speech to text in Java</description>

<licenses>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,6 @@ public class WhisperFullParams {
* Tokenizer option
*/
public boolean suppressNonSpeechTokens;
/**
* Speed-up the audio by 2x using Phase Vocoder
*/
public boolean speedUp;
/**
* Initial decoding temperature
*/
Expand Down
37 changes: 5 additions & 32 deletions src/main/java/io/github/givimad/whisperjni/WhisperJNI.java
Original file line number Diff line number Diff line change
Expand Up @@ -368,20 +368,17 @@ public static void loadLibrary() throws IOException {
/**
* Register the native library, should be called at first.
*
* @param options instance of {@link LoadOptions} to customize library load.
* @param logger instance of {@link LibraryLogger}.
* @throws IOException when unable to load the native library.
*/
public static void loadLibrary(LoadOptions options) throws IOException {
public static void loadLibrary(LibraryLogger logger) throws IOException {
if (libraryLoaded) {
return;
}
if (options == null) {
options = new LoadOptions();
if(logger == null) {
logger = (String ignored) -> {};
}
if(options.logger == null) {
options.logger = (String ignored) -> { };
}
LibraryUtils.loadLibrary(options);
LibraryUtils.loadLibrary(logger);
libraryLoaded = true;
}

Expand All @@ -405,30 +402,6 @@ public interface LibraryLogger {
void log(String text);
}

/**
* The class {@link LoadOptions} allows to customize the load of the required shared libraries.
*
* @author Miguel Álvarez Díez - Initial contribution
*/
public static class LoadOptions {
/**
* Logs the library registration process (platform detection and library extraction).
*/
public LibraryLogger logger;
/**
* Path to whisper jni library (so/dll/dylib).
* Takes prevalence over the bundled binary.
*/
public Path whisperJNILib;
/**
* Path to whisper library (so/dylib).
* Takes prevalence over the bundled binary.
* Only works on Linux and macOS.
* On windows the library search for the whisper.dll in the $env:PATH directories.
*/
public Path whisperLib;
}

/**
* Called from the cpp side of the library to proxy the whisper.cpp logs.
*
Expand Down
Loading

0 comments on commit 425a8fe

Please sign in to comment.