Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using CUDA while decoupling from the CUDA Toolkit as a hard-dependency #365

Merged
merged 1 commit into from
Dec 15, 2023
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
69 changes: 67 additions & 2 deletions LLama/Native/NativeApi.Load.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Linq;
using System.Runtime.InteropServices;
using System.Text.Json;
using System.Text.RegularExpressions;

namespace LLama.Native
{
Expand Down Expand Up @@ -69,9 +70,12 @@ private static int GetCudaMajorVersion()
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))
{
Expand Down Expand Up @@ -115,6 +119,67 @@ private static int GetCudaMajorVersion()
}
}

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
Expand Down