dotnet/TorchSharp

Running tutorials on MacOS results in error.

NiklasGustafsson opened this issue · 7 comments

Trying to run tutorial1.ipynb on MacOS-Arm64

https://github.com/dotnet/TorchSharpExamples/blob/main/tutorials/CSharp/tutorial1.ipynb

results in an error on the first TorchSharp invocation. It's dynamically looking for the wrong libtorch version number, '2.5.1.0' instead of '2.5.1'

Installed Packages
libtorch-cpu-osx-arm64, 2.5.1
TorchSharp-cpu, 0.105.0

Error: System.TypeInitializationException: The type initializer for 'TorchSharp.torch' threw an exception.
---> System.NotSupportedException: The libtorch-cpu-osx-arm64 package version 2.5.1.0 is not restored on this system. If using F# Interactive or .NET Interactive you may need to add a reference to this package, e.g.
#r "nuget: libtorch-cpu-osx-arm64, 2.5.1.0". Trace from LoadNativeBackend:

TorchSharp: LoadNativeBackend: Initialising native backend, useCudaBackend = False

This only seems to happen on MacOS, running tutorials on Windows works fine.

Hey @NiklasGustafsson , thanks for the report.

By meaning the first invocation, are you referring this code section in the tutorial1.ipynb?

using TorchSharp;

using static TorchSharp.torch.nn;
using static TorchSharp.torch.nn.functional;
using static TorchSharp.TensorExtensionMethods;

I'm trying to verify if issue is related to notebook or TorchSharp itself.

No, you have to go farther down the notebook, when it first creates a tensor. Before then, nothing is run.

I believe it has to do with .NET Interactive, and how TorchSharp loads the backend dynamically. For some reason, it's not affecting Windows (haven't tried tutorials on Linux).

@NiklasGustafsson . So I have taken a look at this, I believe it is a problem with TorchSharp probing for it's native assemblies directly.

The failure occurs down here.

private static void LoadNativeBackend(bool useCudaBackend, out StringBuilder? trace)

In interactive we have assembly and native assembly resolution, which should resolve this fine, however, It looks to me, as if this TorchSharp API is failing aggressively, leaving us unable to do anything.

It seems to me as if the issue, may have something to do with the MAC having universal and arm64 native package dependencies, which also seem to have differing package version numbers, but I may be wrong about that. I would however guess, that on a when using a non arm64 build on the mac, it will work as expected. 

More people with Mac need to check

Here is the full error output:
Looking at the nuspec everything looks fine, so perhaps the TorchSharp loader just thinks the package version has 4 parts rather than 3.
Image

Here:

const string libtorchPackageVersion = "2.5.1.0";

and here:
https://www.nuget.org/packages/libtorch-cpu-osx-arm64

Hey @KevinRansom thanks for the very fast repky. I tried correcting the hardcoded libtorch version in the code and it still failed. I opened the issue on dotnet interactive because when I try to use the packages in a non notebook project (like a console application in visual studio) this error does not happen.