Skip to content

Commit

Permalink
add logic to auto load library
Browse files Browse the repository at this point in the history
  • Loading branch information
AmSmart committed May 29, 2024
1 parent fd7311d commit 82e5020
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 131 deletions.
136 changes: 10 additions & 126 deletions LLama/LLamaSharp.Runtime.targets
Original file line number Diff line number Diff line change
Expand Up @@ -2,133 +2,17 @@
<PropertyGroup>
<IncludeBuiltInRuntimes Condition="'$(IncludeBuiltInRuntimes)' == ''">true</IncludeBuiltInRuntimes>
</PropertyGroup>
<ItemGroup Condition="'$(IncludeBuiltInRuntimes)' == 'true'">

<None Include="$(MSBuildThisFileDirectory)runtimes/deps/llama.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/win-x64/native/noavx/llama.dll</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/deps/avx/llama.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/win-x64/native/avx/llama.dll</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/deps/avx2/llama.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/win-x64/native/avx2/llama.dll</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/deps/avx512/llama.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/win-x64/native/avx512/llama.dll</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/deps/cu11.7.1/llama.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/win-x64/native/cuda11/llama.dll</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/deps/cu12.1.0/llama.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/win-x64/native/cuda12/llama.dll</Link>
</None>
<ItemGroup Condition="$(AndroidSupportedAbis.Contains('arm64-v8a')) or $(RuntimeIdentifiers.Contains('android-arm64'))">
<AndroidNativeLibrary Visible="false" Include="$(MSBuildThisFileDirectory)../runtimes/android-arm64-v8a/native/libllama.so">
<Link>libllama.so</Link>
<Abi>arm64-v8a</Abi>
</AndroidNativeLibrary>

<None Include="$(MSBuildThisFileDirectory)runtimes/deps/libllama.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/linux-x64/native/noavx/libllama.so</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/deps/avx/libllama.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/linux-x64/native/avx/libllama.so</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/deps/avx2/libllama.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/linux-x64/native/avx2/libllama.so</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/deps/avx512/libllama.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/linux-x64/native/avx512/libllama.so</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/deps/cu11.7.1/libllama.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/linux-x64/native/cuda11/libllama.so</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/deps/cu12.1.0/libllama.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/linux-x64/native/cuda12/libllama.so</Link>
</None>

<None Include="$(MSBuildThisFileDirectory)runtimes/deps/osx-arm64/libllama.dylib">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/osx-arm64/native/libllama.dylib</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/deps/osx-arm64/ggml-metal.metal">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/osx-arm64/native/ggml-metal.metal</Link>
</None>

<None Include="$(MSBuildThisFileDirectory)runtimes/deps/osx-x64/libllama.dylib">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/osx-x64/native/libllama.dylib</Link>
</None>

<None Include="$(MSBuildThisFileDirectory)runtimes/deps/llava_shared.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/win-x64/native/noavx/llava_shared.dll</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/deps/avx/llava_shared.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/win-x64/native/avx/llava_shared.dll</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/deps/avx2/llava_shared.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/win-x64/native/avx2/llava_shared.dll</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/deps/avx512/llava_shared.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/win-x64/native/avx512/llava_shared.dll</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/deps/cu11.7.1/llava_shared.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/win-x64/native/cuda11/llava_shared.dll</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/deps/cu12.1.0/llava_shared.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/win-x64/native/cuda12/llava_shared.dll</Link>
</None>


<None Include="$(MSBuildThisFileDirectory)runtimes/deps/libllava_shared.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/linux-x64/native/noavx/libllava_shared.so</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/deps/avx/libllava_shared.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/linux-x64/native/avx/libllava_shared.so</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/deps/avx2/libllava_shared.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/linux-x64/native/avx2/libllava_shared.so</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/deps/avx512/libllava_shared.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/linux-x64/native/avx512/libllava_shared.so</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/deps/cu11.7.1/libllava_shared.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/linux-x64/native/cuda11/libllava_shared.so</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/deps/cu12.1.0/libllava_shared.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/linux-x64/native/cuda12/libllava_shared.so</Link>
</None>

<None Include="$(MSBuildThisFileDirectory)runtimes/deps/osx-arm64/libllava_shared.dylib">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/osx-arm64/native/libllava_shared.dylib</Link>
</None>

<None Include="$(MSBuildThisFileDirectory)runtimes/deps/osx-x64/libllava_shared.dylib">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/osx-x64/native/libllava_shared.dylib</Link>
</None>


<AndroidNativeLibrary Visible="false" Include="$(MSBuildThisFileDirectory)../runtimes/android-arm64-v8a/native/libllava_shared.so">
<Link>libllava_shared.so</Link>
<Abi>arm64-v8a</Abi>
</AndroidNativeLibrary>
</ItemGroup>

</Project>
9 changes: 8 additions & 1 deletion LLama/Native/NativeApi.Load.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using LLama.Exceptions;
using LLama.Exceptions;
using System;
using System.IO;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -55,6 +55,11 @@ private static void SetDllImportResolver()
// NativeLibrary is not available on older runtimes. We'll have to depend on
// the normal runtime dll resolution there.
#if NET5_0_OR_GREATER

// We don't need special dll resolution on Android
if (OperatingSystem.IsAndroid())
return;

NativeLibrary.SetDllImportResolver(typeof(NativeApi).Assembly, (name, _, _) =>
{
if (name == "llama")
Expand Down Expand Up @@ -316,7 +321,9 @@ private static void GetPlatformPathParts(out OSPlatform platform, out string os,
? "osx-arm64"
: "osx-x64";
libPrefix = "lib";
return;
}

else
{
throw new RuntimeError("Your operating system is not supported, please open an issue in LLamaSharp.");
Expand Down
32 changes: 32 additions & 0 deletions LLama/runtimes/build/LLamaSharp.Backend.Android.nuspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<package >
<metadata>
<id>LLamaSharp.Backend.Android</id>
<version>$version$</version>
<title>LLamaSharp.Backend.Android - Android Backend for LLamaSharp</title>
<authors>llama.cpp Authors</authors>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<license type="expression">MIT</license>
<icon>icon512.png</icon>
<projectUrl>https://github.com/SciSharp/LLamaSharp</projectUrl>
<description>LLamaSharp.Backend.Android is a backend for LLamaSharp to use with Android.</description>
<releaseNotes></releaseNotes>
<copyright>Copyright 2023 The llama.cpp Authors. All rights reserved.</copyright>
<tags>LLamaSharp LLama LLM GPT AI ChatBot SciSharp</tags>
</metadata>

<files>
<file src="../../LLamaSharp.Runtime.targets" target="build/LLamaSharp.Backend.Android.targets" />

<file src="../deps/android-x86/libllama.so" target="runtimes/android-x86/native/libllama.so" />
<file src="../deps/android-x86/libllava_shared.so" target="runtimes/android-x86/native/libllava_shared.so" />

<file src="../deps/android-x86_64/libllama.so" target="runtimes/android-x86_64/native/libllama.so" />
<file src="../deps/android-x86_64/libllava_shared.so" target="runtimes/android-x86_64/native/libllava_shared.so" />

<file src="../deps/android-arm64-v8a/libllama.so" target="runtimes/android-arm64-v8a/native/libllama.so" />
<file src="../deps/android-arm64-v8a/libllava_shared.so" target="runtimes/android-arm64-v8a/native/libllava_shared.so" />

<file src="icon512.png" target="icon512.png" />
</files>
</package>
5 changes: 1 addition & 4 deletions LLama/runtimes/build/LLamaSharp.Backend.Cpu.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@

<file src="runtimes/deps/osx-x64/libllava_shared.dylib" target="runtimes\osx-x64\native\libllava_shared.dylib" />
<file src="runtimes/deps/osx-arm64/libllava_shared.dylib" target="runtimes\osx-arm64\native\libllava_shared.dylib" />

<file src="runtimes/deps/android-arm64/libllama.so" target="runtimes\android-arm64\native\libllama.so" />
<file src="runtimes/deps/android-arm64/libllava_shared.so" target="runtimes\android-arm64\native\libllava_shared.so" />


<file src="icon512.png" target="icon512.png" />
</files>
</package>

0 comments on commit 82e5020

Please sign in to comment.