Skip to content

Commit b79b298

Browse files
committed
Using SetDllImportResolver to load llama DLLs as a prototype.
1 parent 3b2836e commit b79b298

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

LLama.Examples/Program.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using LLama.Native;
1+
using System.Runtime.InteropServices;
2+
using LLama.Batched;
3+
using LLama.Native;
24
using Spectre.Console;
35

46
AnsiConsole.MarkupLineInterpolated(
@@ -23,4 +25,5 @@ __ __ ____ __
2325

2426
NativeApi.llama_empty_call();
2527

26-
await ExampleRunner.Run();
28+
await ExampleRunner.Run();
29+

LLama/Native/NativeApi.Load.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
using System.IO;
77
using System.Runtime.InteropServices;
88
using System.Text.Json;
9+
using LLama.Batched;
910

1011
namespace LLama.Native
1112
{
1213
public static partial class NativeApi
1314
{
1415
static NativeApi()
1516
{
16-
// Try to load a preferred library, based on CPU feature detection
17-
TryLoadLibrary();
17+
TestSetDllResolver();
1818

1919
try
2020
{
@@ -33,6 +33,31 @@ static NativeApi()
3333
llama_backend_init();
3434
}
3535

36+
private static IntPtr _loaded;
37+
38+
private static void TestSetDllResolver()
39+
{
40+
#if NET5_0_OR_GREATER
41+
NativeLibrary.SetDllImportResolver(typeof(BatchedExecutor).Assembly, (name, assembly, path) =>
42+
{
43+
if (name != "llama")
44+
return IntPtr.Zero;
45+
46+
if (_loaded != IntPtr.Zero)
47+
return _loaded;
48+
49+
// Try to load a preferred library, based on CPU feature detection
50+
_loaded = TryLoadLibrary();
51+
52+
Console.WriteLine($"[red]Resolving: {name}[/]");
53+
Console.WriteLine($"[red]Path: {path}[/]");
54+
Console.WriteLine($"[red]Ptr: {_loaded}[/]");
55+
56+
return _loaded;
57+
});
58+
#endif
59+
}
60+
3661
private static void Log(string message, LogLevel level)
3762
{
3863
if (!enableLogging)

0 commit comments

Comments
 (0)