Skip to content

Commit

Permalink
Add support for metal builds
Browse files Browse the repository at this point in the history
  • Loading branch information
Macoron committed Nov 29, 2023
1 parent f3a7730 commit 3aaad1f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 19 deletions.
58 changes: 41 additions & 17 deletions Packages/com.whisper.unity/Editor/WhisperProjectSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ public static SettingsProvider CreateMyCustomSettingsProvider()
guiHandler = (searchContext) =>
{
CudaEnabled = EditorGUILayout.Toggle("Enable CUDA", CudaEnabled);
MetalEnabled = EditorGUILayout.Toggle("Enable Metal", MetalEnabled);
},

keywords = new HashSet<string>(new[] { "CUDA", "cuBLAS" })
keywords = new HashSet<string>(new[] { "CUDA", "cuBLAS", "Metal" })
};

return provider;
Expand All @@ -39,28 +40,51 @@ public static bool CudaEnabled
{
if (value == CudaEnabled)
return;
SetDefine("WHISPER_CUDA", value);
}
}

public static bool MetalEnabled
{
get
{
#if WHISPER_METAL
return true;
#else
return false;
#endif
}
set
{
if (value == MetalEnabled)
return;
SetDefine("WHISPER_METAL", value);
}
}

string[] newDefines;
var defines = GetStandaloneDefines();

if (value)
{
if (defines.Contains("WHISPER_CUDA"))
return;
private static void SetDefine(string define, bool value)
{
string[] newDefines;
var defines = GetStandaloneDefines();

newDefines = defines.Append("WHISPER_CUDA").ToArray();
}
else
{
if (!defines.Contains("WHISPER_CUDA"))
return;
if (value)
{
if (defines.Contains(define))
return;

newDefines = defines.Where(x => x != "WHISPER_CUDA").ToArray();
}
newDefines = defines.Append(define).ToArray();
}
else
{
if (!defines.Contains(define))
return;

SetStandaloneDefines(newDefines);
newDefines = defines.Where(x => x != define).ToArray();
}

SetStandaloneDefines(newDefines);
}


// This is for older Unity compability
private static string[] GetStandaloneDefines()
Expand Down
15 changes: 13 additions & 2 deletions Packages/com.whisper.unity/Runtime/Native/WhisperNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,27 @@ public static unsafe class WhisperNative

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

#elif WHISPER_CUDA

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

#elif WHISPER_METAL

#if UNITY_EDITOR && UNITY_EDITOR_OSX
private const string LibraryName = "libwhisper_metal";
#elif !UNITY_EDITOR && UNITY_STANDALONE_OSX
private const string LibraryName = "libwhisper_metal";
#else
private const string LibraryName = "libwhisper";
#endif

#else
private const string LibraryName = "libwhisper";
#endif
Expand Down
24 changes: 24 additions & 0 deletions build_cpp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,28 @@ build_mac() {
echo "Build files copied to $target_path"
}

build_mac_metal() {
clean_build
echo "Starting building for Mac (Metal)..."

cmake -DCMAKE_OSX_ARCHITECTURES="x86_64;arm64" -DWHISPER_METAL=ON \
-DWHISPER_NO_AVX=ON -DWHISPER_NO_AVX2=ON -DWHISPER_NO_FMA=ON -DWHISPER_NO_F16C=ON \
-DWHISPER_BUILD_TESTS=OFF -DWHISPER_BUILD_EXAMPLES=OFF ../
make

echo "Build for Mac (Metal) complete!"

artifact_path="$build_path/libwhisper.dylib"
target_path="$unity_project/Packages/com.whisper.unity/Plugins/MacOS/libwhisper_metal.dylib"
cp "$artifact_path" "$target_path"

artifact_path="$build_path/bin/ggml-metal.metal"
target_path="$unity_project/Packages/com.whisper.unity/Plugins/MacOS/ggml-metal.metal"
cp "$artifact_path" "$target_path"

echo "Build files copied to $target_path"
}

build_ios() {
clean_build
echo "Starting building for ios..."
Expand Down Expand Up @@ -72,6 +94,8 @@ if [ "$targets" = "all" ]; then
build_android
elif [ "$targets" = "mac" ]; then
build_mac
elif [ "$targets" = "mac_metal" ]; then
build_mac_metal
elif [ "$targets" = "ios" ]; then
build_ios
elif [ "$targets" = "android" ]; then
Expand Down

0 comments on commit 3aaad1f

Please sign in to comment.