Skip to content

Commit

Permalink
Add CUDA support for Linux (#63)
Browse files Browse the repository at this point in the history
* Add CUDA support for Linux

* Update build_cpp.yml

* Update build_cpp.yml

* Update build_cpp.yml

* Update build_cpp.yml

* Update build_cpp.yml

* Update build_cpp.yml

* Update build_cpp.yml

* Update build_cpp.yml

* Update build_cpp.yml

* Update build_cpp.yml

* Update build_cpp.yml

* Update build_cpp.yml

* Update build_cpp.yml

* Update build_cpp.yml

* Update build_cpp.yml

* Linux code support

* Update test.yml

* Test before commit

* Meta?

* Like that?

* Update README.md

* Some minor fixes
  • Loading branch information
Macoron committed Nov 26, 2023
1 parent 38c24cd commit f3a7730
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 36 deletions.
66 changes: 40 additions & 26 deletions .github/workflows/build_cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,8 @@ jobs:
if-no-files-found: error

build-linux:
name: Build for Linux (Ubuntu 18.04 x86_64)
runs-on: ubuntu-latest
container: ubuntu:18.04
name: Build for Linux (x86_64)
runs-on: ubuntu-20.04
steps:
- name: Clone whisper.unity
uses: actions/checkout@v3
Expand All @@ -99,40 +98,55 @@ jobs:
repository: ${{ github.event.inputs.whisper_cpp_repo }}
ref: ${{ github.event.inputs.whisper_cpp_repo_ref }}
path: whisper-cpp

- name: Latest Cmake
run: |
apt-get update \
&& apt-get install -y build-essential \
&& apt-get install -y wget \
&& rm -rf /var/lib/apt/lists/* \
&& wget https://github.com/Kitware/CMake/releases/download/v3.24.1/cmake-3.24.1-Linux-x86_64.sh \
-q -O /tmp/cmake-install.sh \
&& chmod u+x /tmp/cmake-install.sh \
&& mkdir /opt/cmake-3.24.1 \
&& /tmp/cmake-install.sh --skip-license --prefix=/opt/cmake-3.24.1 \
&& rm /tmp/cmake-install.sh \
&& ln -s /opt/cmake-3.24.1/bin/* /usr/local/bin
- name: Change gcc and g++
run: |
apt-get update
apt-get install -y gcc-8 g++-8

- name: Run build script
run: |
cd whisper-unity
sh build_cpp_linux.sh ../whisper-cpp/
env:
CC: gcc-8
CXX: g++-8
sh build_cpp_linux.sh ../whisper-cpp/ cpu
- name: Upload results
uses: actions/upload-artifact@v3
with:
name: linux
path: ${{ github.workspace }}/whisper-cpp/build/libwhisper.so
if-no-files-found: error

build-linux-cuda:
name: Build for Linux (x86_64, CUDA)
runs-on: ubuntu-20.04
steps:
- name: Clone whisper.unity
uses: actions/checkout@v3
with:
path: whisper-unity

- name: Clone whisper.cpp
uses: actions/checkout@v3
with:
repository: ${{ github.event.inputs.whisper_cpp_repo }}
ref: ${{ github.event.inputs.whisper_cpp_repo_ref }}
path: whisper-cpp

- name: Install CUDA Toolkit
id: cuda-toolkit
uses: Jimver/cuda-toolkit@v0.2.11
with:
cuda: '12.2.0'
method: 'network'

- name: Run build script
run: |
cd whisper-unity
sh build_cpp_linux.sh ../whisper-cpp cuda
cd ${{ github.workspace }}/whisper-cpp/build/
mv libwhisper.so libwhisper_cuda.so
- name: Upload results
uses: actions/upload-artifact@v3
with:
name: linux-cuda
path: ${{ github.workspace }}/whisper-cpp/build/libwhisper_cuda.so
if-no-files-found: error

build-macos:
name: Build for MacOS (ARM, x86_64)
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- 2019.4.40f1
steps:
- uses: actions/checkout@v3
- uses: game-ci/unity-test-runner@v2
- uses: game-ci/unity-test-runner@v4
id: tests
with:
testMode: playmode
Expand Down
Binary file modified Packages/com.whisper.unity/Plugins/Linux/libwhisper.so
Binary file not shown.
Binary file not shown.
63 changes: 63 additions & 0 deletions Packages/com.whisper.unity/Plugins/Linux/libwhisper_cuda.so.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions Packages/com.whisper.unity/Runtime/Native/WhisperNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ public static unsafe class WhisperNative
{

#if (UNITY_IOS || UNITY_ANDROID) && !UNITY_EDITOR
private const string LibraryName = "__Internal";
#else
#if UNITY_STANDALONE_WIN && WHISPER_CUDA
private const string LibraryName = "__Internal";
#elif WHISPER_CUDA

#if (UNITY_EDITOR && (UNITY_EDITOR_WIN || UNITY_EDITOR_LINUX))
private const string LibraryName = "libwhisper_cuda";
#elif (!UNITY_EDITOR && (UNITY_STANDALONE_WIN || UNITY_STANDALONE_LINUX))
private const string LibraryName = "libwhisper_cuda";
#else
private const string LibraryName = "libwhisper";
#endif

#else
private const string LibraryName = "libwhisper";
#endif

[DllImport(LibraryName)]
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ This is Unity3d bindings for the [whisper.cpp](https://github.com/ggerganov/whis
**Supported platforms:**
- [x] Windows (x86_64, [optional CUDA](#cuda-support))
- [x] MacOS (Intel and ARM)
- [x] Linux (x86_64, [optional CUDA](#cuda-support))
- [x] iOS (Device and Simulator)
- [x] Android (ARM64)
- [x] Linux (x86_64, Ubuntu 18.04 and newer)
- [ ] WebGL (see [this issue](https://github.com/Macoron/whisper.unity/issues/20))

## Samples
Expand Down Expand Up @@ -70,7 +70,7 @@ sh build_cpp.sh path/to/whisper all path/to/ndk/android.toolchain.cmake
```
6. If you are using **Linux** write
```bash
sh build_cpp_linux.sh path/to/whisper
sh build_cpp_linux.sh path/to/whisper cpu
```
7. If build was successful compiled libraries should be automatically update package `Plugins` folder.

Expand Down
34 changes: 30 additions & 4 deletions build_cpp_linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ clean_build(){
cd "$build_path"
}

build_linux() {
build_cpu() {
clean_build
echo "Starting building for Linux..."
echo "Starting building for CPU..."

cmake -DCMAKE_BUILD_TYPE=Release \
-DWHISPER_BUILD_TESTS=OFF -DWHISPER_BUILD_EXAMPLES=OFF ../
make

echo "Build for Linux complete!"
echo "Build for CPU complete!"

artifact_path="$build_path/libwhisper.so"
target_path="$unity_project/Packages/com.whisper.unity/Plugins/Linux/libwhisper.so"
Expand All @@ -28,4 +28,30 @@ build_linux() {
echo "Build files copied to $target_path"
}

build_linux
build_cuda() {
clean_build
echo "Starting building for CUDA..."

cmake -DWHISPER_CUBLAS=ON -DCMAKE_BUILD_TYPE=Release \
-DWHISPER_BUILD_TESTS=OFF -DWHISPER_BUILD_EXAMPLES=OFF ../
make

echo "Build for CUDA complete!"

artifact_path="$build_path/libwhisper.so"
target_path="$unity_project/Packages/com.whisper.unity/Plugins/Linux/libwhisper_cuda.so"
cp "$artifact_path" "$target_path"

echo "Build files copied to $target_path"
}

if [ "$targets" = "all" ]; then
build_cpu
build_cuda
elif [ "$targets" = "cpu" ]; then
build_cpu
elif [ "$targets" = "cuda" ]; then
build_cuda
else
echo "Unknown targets: $targets"
fi

0 comments on commit f3a7730

Please sign in to comment.