Compare commits

...

3 Commits

Author SHA1 Message Date
Rinne 609e2968ad
Merge branch 'master' of github.com:SciSharp/LLamaSharp into preview 2023-12-16 19:24:35 +08:00
Rinne b79387fd76
Merge pull request #365 from Onkitova/preview
feat: using CUDA while decoupling from the CUDA Toolkit as a hard-dependency
2023-12-15 08:52:55 +08:00
AlTonkas 4f1bda18b6 Using CUDA while decoupling from the CUDA Toolkit as a hard-dependency
Using CUDA while decoupling from the CUDA Toolkit as a hard-dependency

Possible solution for https://github.com/SciSharp/LLamaSharp/issues/350

Adding an alternative, fallback method of detection system-supported cuda version to make CUDA Toolkit installation optional. Technically, it uses output of the command line tool "nvidia-smi" (preinstalled with nvidia drivers), which also contains information about cuda version supported on system.

Can confirm it works only on Windows, but I suppose that similar approach can be utilized for Linux and MacOS as well. Didn't touch the code for these 2 platforms, nevertheless.

After that, cuda can be utilized simply by putting nvidia libraries from llama.cpp original repo, "bin-win-cublas-cu12.2.0-x64.zip" asset to the root folder of the built program. For example, to folder: "\LLama.Examples\bin\Debug\net8.0\".
2023-12-14 16:25:59 +03:00
1 changed files with 67 additions and 2 deletions

View File

@ -7,6 +7,7 @@ using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text.Json;
using System.Text.RegularExpressions;
namespace LLama.Native
{
@ -69,9 +70,12 @@ namespace LLama.Native
cudaPath = Environment.GetEnvironmentVariable("CUDA_PATH");
if (cudaPath is null)
{
return -1;
version = GetCudaVersionFromDriverUtils_windows();
}
else
{
version = GetCudaVersionFromPath(cudaPath);
}
version = GetCudaVersionFromPath(cudaPath);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
@ -115,6 +119,67 @@ namespace LLama.Native
}
}
private static string GetCudaVersionFromDriverUtils_windows()
{
try
{
var psi = new ProcessStartInfo
{
FileName = "nvidia-smi",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
using (var process = Process.Start(psi))
{
if (process != null)
{
using (StreamReader reader = process.StandardOutput)
{
string output = reader.ReadToEnd();
process.WaitForExit();
string cudaVersion = GetNvidiaSmiValue(output, "CUDA Version");
string pattern = @":\s(\d+\.\d+)";
Match match = Regex.Match(cudaVersion, pattern);
string extractedValue = string.Empty;
if (match.Success && match.Groups.Count > 1)
{
extractedValue = match.Groups[1].Value;
}
return extractedValue;
}
}
else
{
return string.Empty;
}
}
}
catch (Exception)
{
return string.Empty;
}
}
static string GetNvidiaSmiValue(string nvidiaSmiOutput, string key)
{
int startIndex = nvidiaSmiOutput.IndexOf(key);
if (startIndex == -1)
{
return "N/A";
}
startIndex += key.Length;
int endIndex = nvidiaSmiOutput.IndexOf('\n', startIndex);
if (endIndex == -1)
{
endIndex = nvidiaSmiOutput.Length;
}
string value = nvidiaSmiOutput.Substring(startIndex, endIndex - startIndex).Trim();
return value;
}
private static string GetCudaVersionFromPath(string cudaPath)
{
try