Skip to content

Commit

Permalink
Metal support for MacOS (#64)
Browse files Browse the repository at this point in the history
* Add support for metal builds

* Update build_cpp.yml

* Update build_cpp.yml

* Add libs and other stuff
  • Loading branch information
Macoron committed Nov 29, 2023
1 parent f3a7730 commit 48cb786
Show file tree
Hide file tree
Showing 11 changed files with 3,210 additions and 44 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/build_cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,38 @@ jobs:
name: macos
path: ${{ github.workspace }}/whisper-cpp/build/libwhisper.dylib
if-no-files-found: error

build-macos-metal:
name: Build for MacOS Metal (ARM, x86_64)
runs-on: macOS-latest
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: Run build script
run: |
cd whisper-unity
sh build_cpp.sh ../whisper-cpp/ mac_metal
cd ${{ github.workspace }}/whisper-cpp/build/
mv libwhisper.dylib libwhisper_metal.dylib
- name: Upload results
uses: actions/upload-artifact@v3
with:
name: macos-metal
path: |
${{ github.workspace }}/whisper-cpp/build/libwhisper_metal.dylib
${{ github.workspace }}/whisper-cpp/build/bin/ggml-metal.metal
if-no-files-found: error

build-ios:
name: Build for iOS
Expand Down
40 changes: 40 additions & 0 deletions Packages/com.whisper.unity/Editor/WhisperPostBuild.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.IO;
using JetBrains.Annotations;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;


[UsedImplicitly]
public static class WhisperPostBuild
{

[PostProcessBuild]
public static void OnPostProcessBuild(BuildTarget buildTarget, string path)
{
if (buildTarget != BuildTarget.StandaloneOSX)
return;
if (!WhisperProjectSettingsProvider.MetalEnabled)
return;

// get source file
var metalFile = Path.GetFullPath("Packages/com.whisper.unity/Plugins/MacOS/ggml-metal.metal");
if (!File.Exists(metalFile))
{
throw new Exception("Can't find metal file in project files! " +
$"{metalFile} doesnt exist!");
}

// get target folder
var pluginsPath = Path.Combine(path, "Contents", "PlugIns");
if (!Directory.Exists(pluginsPath))
{
throw new Exception("Can't find plugins directory in build app! " +
$"{pluginsPath} doesnt exist!");
}

var targetPath = Path.Combine(pluginsPath, "ggml-metal.metal");
File.Copy(metalFile, targetPath, true);
}
}
11 changes: 11 additions & 0 deletions Packages/com.whisper.unity/Editor/WhisperPostBuild.cs.meta

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

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
Loading

0 comments on commit 48cb786

Please sign in to comment.