forked from LykosAI/StabilityMatrix
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathWindowsPrerequisiteHelper.cs
More file actions
897 lines (761 loc) · 31.7 KB
/
WindowsPrerequisiteHelper.cs
File metadata and controls
897 lines (761 loc) · 31.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
using System.Diagnostics;
using System.Runtime.Versioning;
using Microsoft.Win32;
using NLog;
using StabilityMatrix.Core.Exceptions;
using StabilityMatrix.Core.Helper;
using StabilityMatrix.Core.Helper.HardwareInfo;
using StabilityMatrix.Core.Models;
using StabilityMatrix.Core.Models.FileInterfaces;
using StabilityMatrix.Core.Models.Packages;
using StabilityMatrix.Core.Models.Progress;
using StabilityMatrix.Core.Processes;
using StabilityMatrix.Core.Python;
using StabilityMatrix.Core.Services;
namespace StabilityMatrix.Avalonia.Helpers;
[SupportedOSPlatform("windows")]
public class WindowsPrerequisiteHelper(
IDownloadService downloadService,
ISettingsManager settingsManager,
IPyRunner pyRunner
) : IPrerequisiteHelper
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private const string PortableGitDownloadUrl =
"https://github.com/git-for-windows/git/releases/download/v2.41.0.windows.1/PortableGit-2.41.0-64-bit.7z.exe";
private const string VcRedistDownloadUrl = "https://aka.ms/vs/16/release/vc_redist.x64.exe";
private const string TkinterDownloadUrl =
"https://cdn.lykos.ai/tkinter-cpython-embedded-3.10.11-win-x64.zip";
private const string NodeDownloadUrl = "https://nodejs.org/dist/v20.19.3/node-v20.19.3-win-x64.zip";
private const string Dotnet7DownloadUrl =
"https://download.visualstudio.microsoft.com/download/pr/2133b143-9c4f-4daa-99b0-34fa6035d67b/193ede446d922eb833f1bfe0239be3fc/dotnet-sdk-7.0.405-win-x64.zip";
private const string Dotnet8DownloadUrl =
"https://download.visualstudio.microsoft.com/download/pr/6902745c-34bd-4d66-8e84-d5b61a17dfb7/e61732b00f7e144e162d7e6914291f16/dotnet-sdk-8.0.101-win-x64.zip";
private const string CppBuildToolsUrl = "https://aka.ms/vs/17/release/vs_BuildTools.exe";
private const string HipSdkDownloadUrl =
"https://download.amd.com/developer/eula/rocm-hub/AMD-Software-PRO-Edition-24.Q4-Win10-Win11-For-HIP.exe";
private const string PythonLibsDownloadUrl = "https://cdn.lykos.ai/python_libs_for_sage.zip";
private string HomeDir => settingsManager.LibraryDir;
private string VcRedistDownloadPath => Path.Combine(HomeDir, "vcredist.x64.exe");
private string AssetsDir => Path.Combine(HomeDir, "Assets");
private string SevenZipPath => Path.Combine(AssetsDir, "7za.exe");
private string PythonDownloadPath => Path.Combine(AssetsDir, "python-3.10.11-embed-amd64.zip");
private string PythonDir => Path.Combine(AssetsDir, "Python310");
private string PythonDllPath => Path.Combine(PythonDir, "python310.dll");
private string PythonLibraryZipPath => Path.Combine(PythonDir, "python310.zip");
private string GetPipPath => Path.Combine(PythonDir, "get-pip.pyc");
// Temporary directory to extract venv to during python install
private string VenvTempDir => Path.Combine(PythonDir, "venv");
private string PortableGitInstallDir => Path.Combine(HomeDir, "PortableGit");
private string PortableGitDownloadPath => Path.Combine(HomeDir, "PortableGit.7z.exe");
private string GitExePath => Path.Combine(PortableGitInstallDir, "bin", "git.exe");
private string TkinterZipPath => Path.Combine(AssetsDir, "tkinter.zip");
private string TkinterExtractPath => PythonDir;
private string TkinterExistsPath => Path.Combine(PythonDir, "tkinter");
private string NodeExistsPath => Path.Combine(AssetsDir, "nodejs", "npm.cmd");
private string NodeExePath => Path.Combine(AssetsDir, "nodejs", "node.exe");
private string NodeDownloadPath => Path.Combine(AssetsDir, "nodejs.zip");
private string Dotnet7DownloadPath => Path.Combine(AssetsDir, "dotnet-sdk-7.0.405-win-x64.zip");
private string Dotnet8DownloadPath => Path.Combine(AssetsDir, "dotnet-sdk-8.0.101-win-x64.zip");
private string DotnetExtractPath => Path.Combine(AssetsDir, "dotnet");
private string DotnetExistsPath => Path.Combine(DotnetExtractPath, "dotnet.exe");
private string Dotnet7SdkExistsPath => Path.Combine(DotnetExtractPath, "sdk", "7.0.405");
private string Dotnet8SdkExistsPath => Path.Combine(DotnetExtractPath, "sdk", "8.0.101");
private string VcBuildToolsDownloadPath => Path.Combine(AssetsDir, "vs_BuildTools.exe");
private string VcBuildToolsExistsPath =>
Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86),
"Microsoft Visual Studio",
"2022",
"BuildTools"
);
private string HipSdkDownloadPath => Path.Combine(AssetsDir, "AMD-HIP-SDK.exe");
private string HipInstalledPath =>
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "AMD", "ROCm", "6.2");
public string GitBinPath => Path.Combine(PortableGitInstallDir, "bin");
public bool IsPythonInstalled => File.Exists(PythonDllPath);
public bool IsVcBuildToolsInstalled => Directory.Exists(VcBuildToolsExistsPath);
public bool IsHipSdkInstalled => Directory.Exists(HipInstalledPath);
public async Task RunGit(
ProcessArgs args,
Action<ProcessOutput>? onProcessOutput = null,
string? workingDirectory = null
)
{
var process = ProcessRunner.StartAnsiProcess(
GitExePath,
args,
workingDirectory,
onProcessOutput,
environmentVariables: new Dictionary<string, string>
{
{ "PATH", Compat.GetEnvPathWithExtensions(GitBinPath) },
}
);
await process.WaitForExitAsync().ConfigureAwait(false);
if (process.ExitCode != 0)
{
throw new ProcessException($"Git exited with code {process.ExitCode}");
}
}
public Task<ProcessResult> GetGitOutput(ProcessArgs args, string? workingDirectory = null)
{
return ProcessRunner.GetProcessResultAsync(
GitExePath,
args,
workingDirectory: workingDirectory,
environmentVariables: new Dictionary<string, string>
{
{ "PATH", Compat.GetEnvPathWithExtensions(GitBinPath) },
}
);
}
public async Task RunNpm(
ProcessArgs args,
string? workingDirectory = null,
Action<ProcessOutput>? onProcessOutput = null,
IReadOnlyDictionary<string, string>? envVars = null
)
{
var result = await ProcessRunner
.GetProcessResultAsync(NodeExistsPath, args, workingDirectory, envVars)
.ConfigureAwait(false);
result.EnsureSuccessExitCode();
onProcessOutput?.Invoke(ProcessOutput.FromStdOutLine(result.StandardOutput));
onProcessOutput?.Invoke(ProcessOutput.FromStdErrLine(result.StandardError));
}
// NOTE TO FUTURE DEVS: if this is causing merge conflicts with dev, just nuke it we don't need anymore
private async Task<string> RunNode(
ProcessArgs args,
string? workingDirectory = null,
IReadOnlyDictionary<string, string>? envVars = null
)
{
var result = await ProcessRunner
.GetProcessResultAsync(NodeExePath, args, workingDirectory, envVars)
.ConfigureAwait(false);
result.EnsureSuccessExitCode();
return result.StandardOutput ?? result.StandardError ?? string.Empty;
}
public Task InstallPackageRequirements(BasePackage package, IProgress<ProgressReport>? progress = null) =>
InstallPackageRequirements(package.Prerequisites.ToList(), progress);
public async Task InstallPackageRequirements(
List<PackagePrerequisite> prerequisites,
IProgress<ProgressReport>? progress = null
)
{
await UnpackResourcesIfNecessary(progress);
if (prerequisites.Contains(PackagePrerequisite.HipSdk))
{
await InstallHipSdkIfNecessary(progress);
}
if (prerequisites.Contains(PackagePrerequisite.Python310))
{
await InstallPythonIfNecessary(progress);
await InstallVirtualenvIfNecessary(progress);
}
if (prerequisites.Contains(PackagePrerequisite.Git))
{
await InstallGitIfNecessary(progress);
}
if (prerequisites.Contains(PackagePrerequisite.VcRedist))
{
await InstallVcRedistIfNecessary(progress);
}
if (prerequisites.Contains(PackagePrerequisite.Node))
{
await InstallNodeIfNecessary(progress);
}
if (prerequisites.Contains(PackagePrerequisite.Dotnet))
{
await InstallDotnetIfNecessary(progress);
}
if (prerequisites.Contains(PackagePrerequisite.Tkinter))
{
await InstallTkinterIfNecessary(progress);
}
if (prerequisites.Contains(PackagePrerequisite.VcBuildTools))
{
await InstallVcBuildToolsIfNecessary(progress);
}
}
public async Task InstallAllIfNecessary(IProgress<ProgressReport>? progress = null)
{
await InstallVcRedistIfNecessary(progress);
await UnpackResourcesIfNecessary(progress);
await InstallPythonIfNecessary(progress);
await InstallGitIfNecessary(progress);
await InstallNodeIfNecessary(progress);
await InstallVcBuildToolsIfNecessary(progress);
await InstallHipSdkIfNecessary(progress);
}
public async Task UnpackResourcesIfNecessary(IProgress<ProgressReport>? progress = null)
{
// Array of (asset_uri, extract_to)
var assets = new[] { (Assets.SevenZipExecutable, AssetsDir), (Assets.SevenZipLicense, AssetsDir) };
progress?.Report(new ProgressReport(0, message: "Unpacking resources", isIndeterminate: true));
Directory.CreateDirectory(AssetsDir);
foreach (var (asset, extractDir) in assets)
{
await asset.ExtractToDir(extractDir);
}
progress?.Report(new ProgressReport(1, message: "Unpacking resources", isIndeterminate: false));
}
public async Task InstallPythonIfNecessary(IProgress<ProgressReport>? progress = null)
{
if (File.Exists(PythonDllPath))
{
Logger.Debug("Python already installed at {PythonDllPath}", PythonDllPath);
return;
}
Logger.Info("Python not found at {PythonDllPath}, downloading...", PythonDllPath);
Directory.CreateDirectory(AssetsDir);
// Delete existing python zip if it exists
if (File.Exists(PythonLibraryZipPath))
{
File.Delete(PythonLibraryZipPath);
}
var remote = Assets.PythonDownloadUrl;
var url = remote.Url.ToString();
Logger.Info($"Downloading Python from {url} to {PythonLibraryZipPath}");
// Cleanup to remove zip if download fails
try
{
// Download python zip
await downloadService.DownloadToFileAsync(url, PythonDownloadPath, progress: progress);
// Verify python hash
var downloadHash = await FileHash.GetSha256Async(PythonDownloadPath, progress);
if (downloadHash != remote.HashSha256)
{
var fileExists = File.Exists(PythonDownloadPath);
var fileSize = new FileInfo(PythonDownloadPath).Length;
var msg =
$"Python download hash mismatch: {downloadHash} != {remote.HashSha256} "
+ $"(file exists: {fileExists}, size: {fileSize})";
throw new Exception(msg);
}
progress?.Report(new ProgressReport(progress: 1f, message: "Python download complete"));
progress?.Report(new ProgressReport(-1, "Installing Python...", isIndeterminate: true));
// We also need 7z if it's not already unpacked
if (!File.Exists(SevenZipPath))
{
await Assets.SevenZipExecutable.ExtractToDir(AssetsDir);
await Assets.SevenZipLicense.ExtractToDir(AssetsDir);
}
// Delete existing python dir
if (Directory.Exists(PythonDir))
{
Directory.Delete(PythonDir, true);
}
// Unzip python
await ArchiveHelper.Extract7Z(PythonDownloadPath, PythonDir);
try
{
// Extract embedded venv folder
Directory.CreateDirectory(VenvTempDir);
foreach (var (resource, relativePath) in Assets.PyModuleVenv)
{
var path = Path.Combine(VenvTempDir, relativePath);
// Create missing directories
var dir = Path.GetDirectoryName(path);
if (dir != null)
{
Directory.CreateDirectory(dir);
}
await resource.ExtractTo(path);
}
// Add venv to python's library zip
await ArchiveHelper.AddToArchive7Z(PythonLibraryZipPath, VenvTempDir);
}
finally
{
// Remove venv
if (Directory.Exists(VenvTempDir))
{
Directory.Delete(VenvTempDir, true);
}
}
// Extract get-pip.pyc
await Assets.PyScriptGetPip.ExtractToDir(PythonDir);
// We need to uncomment the #import site line in python310._pth for pip to work
var pythonPthPath = Path.Combine(PythonDir, "python310._pth");
var pythonPthContent = await File.ReadAllTextAsync(pythonPthPath);
pythonPthContent = pythonPthContent.Replace("#import site", "import site");
await File.WriteAllTextAsync(pythonPthPath, pythonPthContent);
// Install TKinter
await InstallTkinterIfNecessary(progress);
progress?.Report(new ProgressReport(1f, "Python install complete"));
}
finally
{
// Always delete zip after download
if (File.Exists(PythonDownloadPath))
{
File.Delete(PythonDownloadPath);
}
}
}
private async Task InstallVirtualenvIfNecessary(IProgress<ProgressReport>? progress = null)
{
// python stuff
if (!PyRunner.PipInstalled || !PyRunner.VenvInstalled)
{
progress?.Report(
new ProgressReport(-1f, "Installing Python prerequisites...", isIndeterminate: true)
);
await pyRunner.Initialize().ConfigureAwait(false);
if (!PyRunner.PipInstalled)
{
await pyRunner.SetupPip().ConfigureAwait(false);
}
if (!PyRunner.VenvInstalled)
{
await pyRunner.InstallPackage("virtualenv").ConfigureAwait(false);
}
}
}
[SupportedOSPlatform("windows")]
public async Task InstallTkinterIfNecessary(IProgress<ProgressReport>? progress = null)
{
if (!Directory.Exists(TkinterExistsPath))
{
Logger.Info("Downloading Tkinter");
await downloadService.DownloadToFileAsync(TkinterDownloadUrl, TkinterZipPath, progress: progress);
progress?.Report(
new ProgressReport(
progress: 1f,
message: "Tkinter download complete",
type: ProgressType.Download
)
);
await ArchiveHelper.Extract(TkinterZipPath, TkinterExtractPath, progress);
File.Delete(TkinterZipPath);
}
progress?.Report(
new ProgressReport(progress: 1f, message: "Tkinter install complete", type: ProgressType.Generic)
);
}
public async Task InstallGitIfNecessary(IProgress<ProgressReport>? progress = null)
{
if (File.Exists(GitExePath))
{
Logger.Debug("Git already installed at {GitExePath}", GitExePath);
return;
}
Logger.Info("Git not found at {GitExePath}, downloading...", GitExePath);
// Download
if (!File.Exists(PortableGitDownloadPath))
{
await downloadService.DownloadToFileAsync(
PortableGitDownloadUrl,
PortableGitDownloadPath,
progress: progress
);
progress?.Report(new ProgressReport(progress: 1f, message: "Git download complete"));
}
await UnzipGit(progress);
await FixGitLongPaths();
}
[SupportedOSPlatform("windows")]
public async Task<bool> FixGitLongPaths()
{
if (!Compat.IsWindows)
return false;
try
{
await RunGit(["config", "--system", "core.longpaths", "true"]);
return true;
}
catch (Exception e)
{
Logger.Error(e, "Failed to set git longpaths");
}
return false;
}
[SupportedOSPlatform("windows")]
public async Task InstallVcRedistIfNecessary(IProgress<ProgressReport>? progress = null)
{
var registry = Registry.LocalMachine;
var key = registry.OpenSubKey(@"SOFTWARE\Microsoft\VisualStudio\14.0\VC\Runtimes\X64", false);
if (key != null)
{
var buildId = Convert.ToUInt32(key.GetValue("Bld"));
if (buildId >= 30139)
{
return;
}
}
Logger.Info("Downloading VC Redist");
await downloadService.DownloadToFileAsync(
VcRedistDownloadUrl,
VcRedistDownloadPath,
progress: progress
);
progress?.Report(
new ProgressReport(
progress: 1f,
message: "Visual C++ download complete",
type: ProgressType.Download
)
);
Logger.Info("Installing VC Redist");
progress?.Report(
new ProgressReport(
progress: 0.5f,
isIndeterminate: true,
type: ProgressType.Generic,
message: "Installing prerequisites..."
)
);
var process = ProcessRunner.StartAnsiProcess(VcRedistDownloadPath, "/install /quiet /norestart");
await process.WaitForExitAsync();
progress?.Report(
new ProgressReport(
progress: 1f,
message: "Visual C++ install complete",
type: ProgressType.Generic
)
);
File.Delete(VcRedistDownloadPath);
}
[SupportedOSPlatform("windows")]
public async Task InstallNodeIfNecessary(IProgress<ProgressReport>? progress = null)
{
// NOTE TO FUTURE DEVS: if this is causing merge conflicts with dev, just nuke it we don't need anymore
var nodeFolder = new DirectoryPath(AssetsDir, "nodejs");
if (nodeFolder.Exists)
{
try
{
var result = await RunNode("-v");
if (result.Contains("20.19.3"))
{
Logger.Debug("Node.js already installed at {NodeExistsPath}", NodeExistsPath);
return;
}
}
catch (Exception)
{
// ignored
}
Logger.Warn("Node.js version mismatch, reinstalling...");
await nodeFolder.DeleteAsync(true);
}
await DownloadAndExtractPrerequisite(progress, NodeDownloadUrl, NodeDownloadPath, AssetsDir);
var extractedNodeDir = Path.Combine(AssetsDir, "node-v20.19.3-win-x64");
if (Directory.Exists(extractedNodeDir))
{
Directory.Move(extractedNodeDir, Path.Combine(AssetsDir, "nodejs"));
}
}
[SupportedOSPlatform("windows")]
public async Task InstallDotnetIfNecessary(IProgress<ProgressReport>? progress = null)
{
if (!Directory.Exists(Dotnet7SdkExistsPath))
{
await DownloadAndExtractPrerequisite(
progress,
Dotnet7DownloadUrl,
Dotnet7DownloadPath,
DotnetExtractPath
);
}
if (!Directory.Exists(Dotnet8SdkExistsPath))
{
await DownloadAndExtractPrerequisite(
progress,
Dotnet8DownloadUrl,
Dotnet8DownloadPath,
DotnetExtractPath
);
}
}
[SupportedOSPlatform("windows")]
public async Task InstallVcBuildToolsIfNecessary(IProgress<ProgressReport>? progress = null)
{
if (
Directory.Exists(VcBuildToolsExistsPath)
&& Directory.EnumerateDirectories(VcBuildToolsExistsPath).Any()
)
return;
await downloadService.DownloadToFileAsync(
CppBuildToolsUrl,
VcBuildToolsDownloadPath,
progress: progress
);
Logger.Info("Installing VC Build Tools");
progress?.Report(
new ProgressReport(
progress: 0.5f,
isIndeterminate: true,
type: ProgressType.Generic,
message: "Installing prerequisites..."
)
);
var process = ProcessRunner.StartAnsiProcess(
VcBuildToolsDownloadPath,
"--quiet --wait --add Microsoft.VisualStudio.Workload.VCTools;includeRecommended;includeOptional",
outputDataReceived: output =>
progress?.Report(
new ProgressReport(
progress: 0.5f,
isIndeterminate: true,
type: ProgressType.Generic,
message: output.ApcMessage?.Data ?? output.Text
)
)
);
await process.WaitForExitAsync();
}
[SupportedOSPlatform("windows")]
public async Task InstallHipSdkIfNecessary(IProgress<ProgressReport>? progress = null)
{
if (IsHipSdkInstalled)
{
await PatchHipSdkIfNecessary(progress);
return;
}
await downloadService.DownloadToFileAsync(HipSdkDownloadUrl, HipSdkDownloadPath, progress: progress);
Logger.Info("Downloaded & installing HIP SDK");
progress?.Report(
new ProgressReport(
progress: 0.5f,
isIndeterminate: true,
type: ProgressType.Generic,
message: "Installing HIP SDK, this may take a few minutes..."
)
);
var info = new ProcessStartInfo
{
FileName = HipSdkDownloadPath,
Arguments = "-install -log hip_install.log",
UseShellExecute = true,
CreateNoWindow = true,
Verb = "runas",
};
if (Process.Start(info) is { } process)
{
await process.WaitForExitAsync();
}
await PatchHipSdkIfNecessary(progress);
}
public async Task<Process> RunDotnet(
ProcessArgs args,
string? workingDirectory = null,
Action<ProcessOutput>? onProcessOutput = null,
IReadOnlyDictionary<string, string>? envVars = null,
bool waitForExit = true
)
{
var process = ProcessRunner.StartAnsiProcess(
DotnetExistsPath,
args,
workingDirectory,
onProcessOutput,
envVars
);
if (!waitForExit)
return process;
await process.WaitForExitAsync();
if (process.ExitCode == 0)
return process;
Logger.Error(
"dotnet8 with args [{Args}] failed with exit code " + "{ExitCode}:\n{StdOut}\n{StdErr}",
args,
process.ExitCode,
process.StandardOutput,
process.StandardError
);
throw new ProcessException(
$"dotnet8 with args [{args}] failed with exit code"
+ $" {process.ExitCode}:\n{process.StandardOutput}\n{process.StandardError}"
);
}
[SupportedOSPlatform("Windows")]
public async Task AddMissingLibsToVenv(
DirectoryPath installedPackagePath,
IProgress<ProgressReport>? progress = null
)
{
var venvLibsDir = installedPackagePath.JoinDir("venv", "libs");
var venvIncludeDir = installedPackagePath.JoinDir("venv", "include");
if (
venvLibsDir.Exists
&& venvIncludeDir.Exists
&& venvLibsDir.JoinFile("python3.lib").Exists
&& venvLibsDir.JoinFile("python310.lib").Exists
)
{
Logger.Debug("Python libs already installed at {VenvLibsDir}", venvLibsDir);
return;
}
var downloadPath = installedPackagePath.JoinFile("python_libs_for_sage.zip");
var venvDir = installedPackagePath.JoinDir("venv");
await downloadService
.DownloadToFileAsync(PythonLibsDownloadUrl, downloadPath, progress)
.ConfigureAwait(false);
progress?.Report(
new ProgressReport(-1f, message: "Extracting Python libraries", isIndeterminate: true)
);
await ArchiveHelper.Extract7Z(downloadPath, venvDir, progress);
var includeFolder = venvDir.JoinDir("include");
var scriptsIncludeFolder = venvDir.JoinDir("Scripts").JoinDir("include");
await includeFolder.CopyToAsync(scriptsIncludeFolder);
await downloadPath.DeleteAsync();
}
private async Task DownloadAndExtractPrerequisite(
IProgress<ProgressReport>? progress,
string downloadUrl,
string downloadPath,
string extractPath
)
{
Logger.Info($"Downloading {downloadUrl} to {downloadPath}");
await downloadService.DownloadToFileAsync(downloadUrl, downloadPath, progress: progress);
Logger.Info("Extracting prerequisite");
progress?.Report(
new ProgressReport(
progress: 0.5f,
isIndeterminate: true,
type: ProgressType.Generic,
message: "Installing prerequisites..."
)
);
Directory.CreateDirectory(extractPath);
// unzip
await ArchiveHelper.Extract(downloadPath, extractPath, progress);
progress?.Report(
new ProgressReport(
progress: 1f,
message: "Prerequisite install complete",
type: ProgressType.Generic
)
);
File.Delete(downloadPath);
}
private async Task UnzipGit(IProgress<ProgressReport>? progress = null)
{
if (progress == null)
{
await ArchiveHelper.Extract7Z(PortableGitDownloadPath, PortableGitInstallDir);
}
else
{
await ArchiveHelper.Extract7Z(PortableGitDownloadPath, PortableGitInstallDir, progress);
}
Logger.Info("Extracted Git");
File.Delete(PortableGitDownloadPath);
}
private async Task PatchHipSdkIfNecessary(IProgress<ProgressReport>? progress)
{
var theGpu =
settingsManager.Settings.PreferredGpu
?? HardwareHelper.IterGpuInfo().FirstOrDefault(x => x.Name != null && x.Name.Contains("AMD"));
if (theGpu?.Name is null)
return;
var downloadUrl = GetDownloadUrlFromGpuName(theGpu.Name);
if (downloadUrl is null)
return;
progress?.Report(new ProgressReport(-1, "Patching ROCm for your GPU", isIndeterminate: true));
var rocmLibsDownloadPath = new FilePath(AssetsDir, "rocmLibs.7z");
await downloadService.DownloadToFileAsync(downloadUrl, rocmLibsDownloadPath, progress: progress);
var rocmLibsExtractPath = new DirectoryPath(AssetsDir, "rocmLibs");
await ArchiveHelper.Extract7Z(rocmLibsDownloadPath, rocmLibsExtractPath, progress);
var hipInstalledPath = new DirectoryPath(HipInstalledPath);
var zipFolderName = downloadUrl switch
{
_ when downloadUrl.Contains("gfx1201") => null,
_ when downloadUrl.Contains("gfx1150") => "rocm gfx1150 for hip skd 6.2.4",
_ when downloadUrl.Contains("gfx1103.AMD") =>
"rocm gfx1103 AMD 780M phoenix V5.0 for hip skd 6.2.4",
_ when downloadUrl.Contains("gfx1034") => "rocm gfx1034-gfx1035-gfx1036 for hip sdk 6.2.4",
_ when downloadUrl.Contains("gfx1032") => "rocm gfx1032 for hip skd 6.2.4(navi21 logic)",
_ when downloadUrl.Contains("gfx1031") => "rocm gfx1031 for hip skd 6.2.4 (littlewu's logic)",
_ when downloadUrl.Contains("gfx1010") =>
"rocm gfx1010-xnack-gfx1011-xnack-gfx1012-xnack- for hip sdk 6.2.4",
_ => null,
};
var librarySourceDir = rocmLibsExtractPath;
if (!string.IsNullOrWhiteSpace(zipFolderName))
{
librarySourceDir = librarySourceDir.JoinDir(zipFolderName);
}
librarySourceDir = librarySourceDir.JoinDir("library");
var libraryDestDir = hipInstalledPath.JoinDir("bin", "rocblas", "library");
await WindowsElevated.Robocopy(librarySourceDir, libraryDestDir);
var rocblasSource = rocmLibsExtractPath;
if (!string.IsNullOrWhiteSpace(zipFolderName))
{
rocblasSource = rocblasSource.JoinDir(zipFolderName);
}
var rocblasSourceFile = rocblasSource.JoinFile("rocblas.dll");
var rocblasDest = hipInstalledPath.JoinDir("bin").JoinFile("rocblas.dll");
if (rocblasSourceFile.Exists)
{
await WindowsElevated.MoveFiles((rocblasSourceFile.FullPath, rocblasDest.FullPath));
}
}
private string? GetDownloadUrlFromGpuName(string name)
{
// gfx1201
if (name.Contains("9060") || name.Contains("9070"))
{
return "https://github.com/likelovewant/ROCmLibs-for-gfx1103-AMD780M-APU/releases/download/v0.6.2.4/rocm.gfx1201.for.hip.skd.6.2.4-no-optimized.7z";
}
// gfx1150
if (
name.Contains("8050S")
|| name.Contains("8060S")
|| name.Contains("880M")
|| name.Contains("890M")
)
{
return "https://github.com/likelovewant/ROCmLibs-for-gfx1103-AMD780M-APU/releases/download/v0.6.2.4/rocm.gfx1150.for.hip.skd.6.2.4.7z";
}
// gfx1103
if (name.Contains("740M") || name.Contains("760M") || name.Contains("780M") || name.Contains("Z1"))
{
return "https://github.com/likelovewant/ROCmLibs-for-gfx1103-AMD780M-APU/releases/download/v0.6.2.4/rocm.gfx1103.AMD.780M.phoenix.V5.0.for.hip.sdk.6.2.4.7z";
}
// gfx1034, gfx1035, gfx1036
if (
name.Contains("6300")
|| name.Contains("6400")
|| name.Contains("6450")
|| name.Contains("6500")
|| name.Contains("6550")
|| name.Contains("660M")
|| name.Contains("680M")
|| name.Contains("Graphics 128SP")
)
{
return "https://github.com/likelovewant/ROCmLibs-for-gfx1103-AMD780M-APU/releases/download/v0.6.2.4/rocm.gfx1034-gfx1035-gfx1036.for.hip.sdk.6.2.4.7z";
}
// gfx1032
if (
name.Contains("6700S")
|| name.Contains("6800S")
|| name.Contains("6600")
|| name.Contains("6650")
)
{
return "https://github.com/likelovewant/ROCmLibs-for-gfx1103-AMD780M-APU/releases/download/v0.6.2.4/rocm.gfx1032.for.hip.sdk.6.2.4.navi21.logic.7z";
}
// gfx1031
if (
name.Contains("6700")
|| name.Contains("6750")
|| name.Contains("6800M")
|| name.Contains("6850M")
)
{
return "https://github.com/likelovewant/ROCmLibs-for-gfx1103-AMD780M-APU/releases/download/v0.6.2.4/rocm.gfx1031.for.hip.sdk.6.2.4.littlewu.s.logic.7z";
}
// gfx1010/1012
if (name.Contains("5700") || name.Contains("5600") || name.Contains("5300") || name.Contains("5500"))
{
return "https://github.com/likelovewant/ROCmLibs-for-gfx1103-AMD780M-APU/releases/download/v0.6.2.4/rocm.gfx1010-xnack-gfx1011-xnack-gfx1012-xnack-.for.hip.sdk.6.2.4.7z";
}
return null;
}
}