Skip to content

Commit

Permalink
fix: cannot load library under some conditions.
Browse files Browse the repository at this point in the history
  • Loading branch information
AsakusaRinne committed Nov 11, 2023
1 parent d03e1db commit e11df78
Showing 1 changed file with 38 additions and 13 deletions.
51 changes: 38 additions & 13 deletions LLama/Native/NativeApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Text.Json;
using LLama.Exceptions;
Expand Down Expand Up @@ -132,7 +133,7 @@ private static string GetAvxLibraryPath(NativeLibraryConfig.AvxLevel avxLevel, s
{
avxStr += "/";
}
return $"{prefix}{avxStr}{libraryName}{suffix}";
return $"{prefix}{avxStr}";
}

private static List<string> GetLibraryTryOrder(NativeLibraryConfig.Description configuration)
Expand Down Expand Up @@ -180,8 +181,8 @@ private static List<string> GetLibraryTryOrder(NativeLibraryConfig.Description c
// if check skipped, we just try to load cuda libraries one by one.
if (configuration.SkipCheck)
{
result.Add($"{prefix}cuda12/{libraryName}{suffix}");
result.Add($"{prefix}cuda11/{libraryName}{suffix}");
result.Add($"{prefix}cuda12/");
result.Add($"{prefix}cuda11/");
}
else
{
Expand All @@ -190,11 +191,11 @@ private static List<string> GetLibraryTryOrder(NativeLibraryConfig.Description c
}
else if (cudaVersion == 11)
{
result.Add($"{prefix}cuda11/{libraryName}{suffix}");
result.Add($"{prefix}cuda11/");
}
else if (cudaVersion == 12)
{
result.Add($"{prefix}cuda12/{libraryName}{suffix}");
result.Add($"{prefix}cuda12/");
}
else if (cudaVersion > 0)
{
Expand Down Expand Up @@ -233,7 +234,7 @@ private static List<string> GetLibraryTryOrder(NativeLibraryConfig.Description c

if(platform == OSPlatform.OSX)
{
result.Add($"{prefix}{libraryName}{suffix}");
result.Add($"{prefix}");
}

return result;
Expand All @@ -252,29 +253,53 @@ private static IntPtr TryLoadLibrary()
if (!string.IsNullOrEmpty(configuration.Path))
{
// When loading the user specified library, there's no fallback.
var result = TryLoad(configuration.Path, true);
if (result is null || result == IntPtr.Zero)
var result = NativeLibrary.Load(configuration.Path);
if (result == IntPtr.Zero)
{
throw new RuntimeError($"Failed to load the native library [{configuration.Path}] you specified.");
}
return result ?? IntPtr.Zero;
return result;
}

var libraryTryLoadOrder = GetLibraryTryOrder(configuration);

string[] possiblePathPrefix = new string[] {
System.AppDomain.CurrentDomain.BaseDirectory,
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) ?? ""
};

var tryFindPath = (string filename) =>
{
int i = 0;
while (!File.Exists(filename))
{
if(i < possiblePathPrefix.Length)
{
filename = Path.Combine(possiblePathPrefix[i], filename);
i++;
}
else
{
break;
}
}
return filename;
};

foreach(var libraryPath in libraryTryLoadOrder)
{
var result = TryLoad(libraryPath, true);
var fullPath = tryFindPath(libraryPath);
var result = TryLoad(fullPath, true);
if(result is not null && result != IntPtr.Zero)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"[Native Library] {libraryPath} is loaded.");
Console.WriteLine($"[Native Library] {fullPath} is loaded.");
Console.ResetColor();
return result ?? IntPtr.Zero;
}
else
{
Console.WriteLine($"Tried to load {libraryPath}");
Console.WriteLine($"Tried to load {fullPath}");
}
}

Expand All @@ -296,7 +321,7 @@ private static IntPtr TryLoadLibrary()
if (!supported)
return null;

if (NativeLibrary.TryLoad(path, out var handle))
if (NativeLibrary.TryLoad(libraryName, System.Reflection.Assembly.GetExecutingAssembly(), ))
return handle;

return null;
Expand Down

0 comments on commit e11df78

Please sign in to comment.