Skip to content
Merged
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
37 changes: 37 additions & 0 deletions Runtime/LLMLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ public class LLMLib
static bool has_avx = false;
static bool has_avx2 = false;
static bool has_avx512 = false;
List<IntPtr> dependencyHandles = new List<IntPtr>();

#if (UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR

Expand Down Expand Up @@ -496,6 +497,12 @@ static LLMLib()
/// <exception cref="Exception"></exception>
public LLMLib(string arch)
{
foreach (string dependency in GetArchitectureDependencies(arch))
{
LLMUnitySetup.Log($"Loading {dependency}");
dependencyHandles.Add(LibraryLoader.LoadLibrary(dependency));
}

libraryHandle = LibraryLoader.LoadLibrary(GetArchitecturePath(arch));
if (libraryHandle == IntPtr.Zero)
{
Expand Down Expand Up @@ -550,6 +557,35 @@ public static string GetArchitectureCheckerPath()
return Path.Combine(LLMUnitySetup.libraryPath, filename);
}

/// <summary>
/// Gets additional dependencies for the specified architecture.
/// </summary>
/// <param name="arch">architecture</param>
/// <returns>paths of dependency dlls</returns>
public static List<string> GetArchitectureDependencies(string arch)
{
List<string> dependencies = new List<string>();
if (arch == "cuda-cu12.2.0-full")
{
if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsServer)
{
dependencies.Add(Path.Combine(LLMUnitySetup.libraryPath, $"windows-{arch}/cudart64_12.dll"));
dependencies.Add(Path.Combine(LLMUnitySetup.libraryPath, $"windows-{arch}/cublasLt64_12.dll"));
dependencies.Add(Path.Combine(LLMUnitySetup.libraryPath, $"windows-{arch}/cublas64_12.dll"));
}
} else if (arch == "vulkan") {
if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsServer)
{
dependencies.Add(Path.Combine(LLMUnitySetup.libraryPath, $"windows-{arch}/vulkan-1.dll"));
}
else if (Application.platform == RuntimePlatform.LinuxEditor || Application.platform == RuntimePlatform.LinuxPlayer || Application.platform == RuntimePlatform.LinuxServer)
{
dependencies.Add(Path.Combine(LLMUnitySetup.libraryPath, $"linux-{arch}/libvulkan.so.1"));
}
}
return dependencies;
}

/// <summary>
/// Gets the path of the llama.cpp library for the specified architecture.
/// </summary>
Expand Down Expand Up @@ -724,6 +760,7 @@ public string GetStringWrapperResult(IntPtr stringWrapper)
public void Destroy()
{
if (libraryHandle != IntPtr.Zero) LibraryLoader.FreeLibrary(libraryHandle);
foreach (IntPtr dependencyHandle in dependencyHandles) LibraryLoader.FreeLibrary(dependencyHandle);
}
}
}
Expand Down
Loading