Compare commits

...

3 Commits

Author SHA1 Message Date
  Rinne 609e2968ad
Merge branch 'master' of github.com:SciSharp/LLamaSharp into preview 1 year ago
  Rinne b79387fd76
Merge pull request #365 from Onkitova/preview 1 year ago
  AlTonkas 4f1bda18b6 Using CUDA while decoupling from the CUDA Toolkit as a hard-dependency 1 year ago
1 changed files with 67 additions and 2 deletions
Split View
  1. +67
    -2
      LLama/Native/NativeApi.Load.cs

+ 67
- 2
LLama/Native/NativeApi.Load.cs 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


Loading…
Cancel
Save