Added lunix support to RocmBackend

This commit is contained in:
Darth Affe 2024-03-25 00:33:56 +01:00
parent 371eb5a773
commit 6e8bbc7950
2 changed files with 43 additions and 6 deletions

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using StableDiffusion.NET.Helper;
namespace StableDiffusion.NET; namespace StableDiffusion.NET;
@ -12,14 +13,16 @@ public partial class RocmBackend : IBackend
public int Priority => 10; public int Priority => 10;
public bool IsAvailable => (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) public bool IsAvailable => ((RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
/*|| RuntimeInformation.IsOSPlatform(OSPlatform.Linux)*/) && RocmVersion is 5)
&& (RuntimeInformation.OSArchitecture == Architecture.X64) || (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
&& RocmVersion is 5; && RocmVersion is 6))
&& (RuntimeInformation.OSArchitecture == Architecture.X64);
public string PathPart => RocmVersion switch public string PathPart => RocmVersion switch
{ {
5 => "rocm5", 5 => "rocm5",
6 => "rocm6",
_ => string.Empty _ => string.Empty
}; };
@ -46,7 +49,6 @@ public partial class RocmBackend : IBackend
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{ {
string? rocmPath = Environment.GetEnvironmentVariable("HIP_PATH"); string? rocmPath = Environment.GetEnvironmentVariable("HIP_PATH");
if (rocmPath == null) return -1; if (rocmPath == null) return -1;
Match match = GetWindowsVersionRegex().Match(rocmPath); Match match = GetWindowsVersionRegex().Match(rocmPath);
@ -55,7 +57,12 @@ public partial class RocmBackend : IBackend
} }
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{ {
//TODO DarthAffe 23.03.2024: Get some info where it's located on linux string? hipconfig = ProcessHelper.RunCommand("hipconfig");
if (hipconfig == null) return -1;
Match match = GetLinuxVersionRegex().Match(hipconfig);
if (match.Success)
version = match.Groups["version"].Value;
} }
if (string.IsNullOrEmpty(version)) if (string.IsNullOrEmpty(version))
@ -73,5 +80,8 @@ public partial class RocmBackend : IBackend
[GeneratedRegex(@".*?\\(?<version>\d+.\d*)\\")] [GeneratedRegex(@".*?\\(?<version>\d+.\d*)\\")]
private static partial Regex GetWindowsVersionRegex(); private static partial Regex GetWindowsVersionRegex();
[GeneratedRegex(@"HIP_PATH\s*:\s*[\w\/]+-(?<version>[\d.]+)$")]
private static partial Regex GetLinuxVersionRegex();
#endregion #endregion
} }

View File

@ -0,0 +1,27 @@
using System.Diagnostics;
namespace StableDiffusion.NET.Helper;
internal static class ProcessHelper
{
public static string? RunCommand(string command)
{
try
{
using Process process = new();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.FileName = command;
process.Start();
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
return output;
}
catch
{
return null;
}
}
}