Skip to content

Commit e9759cf

Browse files
Preload ARM64 native dependencies for NuGet package loading
Add preloading of ARM Performance Libraries and other libtorch dependencies on Windows ARM64 before loading torch_cpu.dll and LibTorchSharp.dll. This fixes native library resolution when consuming TorchSharp from NuGet packages, where implicit DLL dependencies cannot be found in the NuGet cache directory. Follows the same pattern as the existing CUDA DLL preloading. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 4edf2e6 commit e9759cf

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/TorchSharp/Torch.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,19 @@ private static void LoadNativeBackend(bool useCudaBackend, out StringBuilder? tr
154154
ok = TryLoadNativeLibraryByName("torch_cuda", typeof(torch).Assembly, trace);
155155
ok = TryLoadNativeLibraryByName("LibTorchSharp", typeof(torch).Assembly, trace);
156156
} else {
157+
var isWindowsArm64 = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) &&
158+
RuntimeInformation.ProcessArchitecture == Architecture.Arm64;
159+
if (isWindowsArm64) {
160+
trace.AppendLine($" Try loading Windows ARM64 native components");
161+
// Preloading these DLLs on Windows ARM64 ensures dependencies are resolved
162+
// when loading from NuGet package directories, similar to the CUDA preloading above.
163+
// ARM64 libtorch uses ARM Performance Libraries instead of Intel OpenMP.
164+
ok = TryLoadNativeLibraryByName("armpl_lp64", typeof(torch).Assembly, trace);
165+
ok = TryLoadNativeLibraryByName("uv", typeof(torch).Assembly, trace);
166+
ok = TryLoadNativeLibraryByName("c10", typeof(torch).Assembly, trace);
167+
ok = TryLoadNativeLibraryByName("torch_global_deps", typeof(torch).Assembly, trace);
168+
}
169+
157170
ok = TryLoadNativeLibraryByName("torch_cpu", typeof(torch).Assembly, trace);
158171
ok = TryLoadNativeLibraryByName("LibTorchSharp", typeof(torch).Assembly, trace);
159172
}

0 commit comments

Comments
 (0)