You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix native library loading from NuGet packages on netstandard2.0 consumers
The netstandard2.0 build of TorchSharp uses a custom NativeLibrary polyfill
(in netstandard.cs) that calls LoadLibraryEx directly without probing
deps.json. This means native DLLs in the NuGet package cache are invisible
to Step 1 loading. Step 3's fallback only worked for F# Interactive
scenarios where TorchSharp.dll was loaded from the NuGet cache path.
This fix extends Step 3 with a second resolution path for regular projects:
when TorchSharp.dll is in the app output directory (not the NuGet cache),
it independently locates the NuGet global packages folder via the
NUGET_PACKAGES environment variable or the default ~/.nuget/packages path,
then consolidates native DLLs into a single directory for loading.
This fixes the issue where consuming CI-produced NuGet packages on
Windows ARM64 (targeting net6.0) would fail with:
'doesn't contain a reference to libtorch-cpu-win-arm64'
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
trace.AppendLine(" Giving up, TorchSharp.dll does not appear to have been loaded from package directories");
297
+
trace.AppendLine(" Giving up, could not locate NuGet packages directory");
253
298
}
254
299
if(!ok){
255
300
varmessage=$"This application or script uses TorchSharp but doesn't contain a reference to {(useCudaBackend?cudaRootPackage:cpuRootPackage)}, Version={libtorchPackageVersion}.\n\nConsider referencing one of the combination packages TorchSharp-cpu, TorchSharp-cuda-linux, TorchSharp-cuda-windows or call System.Runtime.InteropServices.NativeLibrary.Load(path-to-{target}) explicitly for a Python install of pytorch. See https://github.com/dotnet/TorchSharp/issues/169.\".\n\nFor CUDA, you may need to call 'TorchSharp.torch.InitializeDeviceType(TorchSharp.DeviceType.CUDA)' before any use of TorchSharp CUDA packages from scripts or notebooks.\n\nTrace from LoadNativeBackend:\n{trace}";
0 commit comments