Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running tutorials on MacOS results in error. #1424

Open
NiklasGustafsson opened this issue Dec 12, 2024 · 7 comments · May be fixed by #1436
Open

Running tutorials on MacOS results in error. #1424

NiklasGustafsson opened this issue Dec 12, 2024 · 7 comments · May be fixed by #1436
Assignees

Comments

@NiklasGustafsson
Copy link
Contributor

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.

@ozanMSFT
Copy link
Contributor

ozanMSFT commented Dec 13, 2024

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.

@NiklasGustafsson
Copy link
Contributor Author

NiklasGustafsson commented Dec 13, 2024

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).

@KevinRansom
Copy link
Member

@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. 

@GeorgeS2019
Copy link

More people with Mac need to check

@KevinRansom
Copy link
Member

KevinRansom commented Jan 23, 2025

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

@KevinRansom
Copy link
Member

Here:

const string libtorchPackageVersion = "2.5.1.0";

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

@alinpahontu2912
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants