-
Notifications
You must be signed in to change notification settings - Fork 196
macOS arm64 support #903
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
macOS arm64 support #903
Conversation
@osum4est, First of all, thank you for contributing to TorchSharp! Please add an introduction in the #333 discussion thread -- it is always nice to know who the contributors are, not just for myself, but for everyone using TorchSharp. Since I cannot test this myself, I wonder if there's someone out there with an M1/M2 who could help validate the PR? Also, I don't think MPS support is out of scope, to be honest. |
Done! I may look at adding full MPS support, but to be honest I wouldn't be using it, so I don't have much motivation to do so. I'm just relying on CPU for quick development/debugging of models, then pushing them over to a linux system with cuda for longer term training. I'd like to use MPS for development, but currently it doesn't support things like Conv3D, which I'm using in my models. Yeah hopefully someone with an M1 can take a look at this, it would be much nicer to be able to use the nuget package in my project instead of having to manually reference my custom built dlls 😅 |
I have an m1 Mac that I can use to help test. @osum4est, thanks for this amazing work. How do I build and test this? I am not familiar with building native libraries and I'm getting errors. Following devguide.md, I build with this on an m1 Mac using Then if I run
Finally, I get these errors in an F# script trying to load the nuget #i """nuget: /Users/user/Documents/GitHub/TorchSharp/bin/packages/Debug"""
#r "nuget: TorchSharp-cpu, 0.99.3"
open TorchSharp
let lin1 = torch.nn.Linear(1000,100)
>
Binding session to '/Users/user/.nuget/packages/torchsharp/0.99.3/lib/netcoreapp3.1/TorchSharp.dll'...
System.DllNotFoundException: Unable to load shared library 'LibTorchSharp' or one of its dependencies. In order to help diagnose loading problems, consider setting the DYLD_PRINT_LIBRARIES environment variable: dlopen(libLibTorchSharp, 0x0001): tried: 'libLibTorchSharp' (no such file), '/usr/local/lib/libLibTorchSharp' (no such file), '/usr/lib/libLibTorchSharp' (no such file), '/Users/user/Documents/GitHub/TorchSharp/libLibTorchSharp' (no such file), '/usr/local/lib/libLibTorchSharp' (no such file), '/usr/lib/libLibTorchSharp' (no such file)
at TorchSharp.PInvoke.LibTorchSharp.THSNN_Linear_ctor(Int64 input_size, Int64 output_size, Boolean bias, IntPtr& pBoxedModule)
at TorchSharp.torch.nn.Linear(Int64 inputSize, Int64 outputSize, Boolean hasBias, Device device, Nullable`1 dtype)
at <StartupCode$FSI_0005>.$FSI_0005.main@()
Stopped due to error |
This below works: #i """nuget: /Users/user/Documents/GitHub/TorchSharp/bin/packages/Debug"""
#r "nuget: TorchSharp, 0.99.3"
open System.Runtime.InteropServices
NativeLibrary.Load("bin/arm64.Debug/Native/libLibTorchSharp.dylib")
open TorchSharp
let t = torch.tensor([|1.0 .. 10.0 |])
>
Binding session to '/Users/user/.nuget/packages/torchsharp/0.99.3/lib/netcoreapp3.1/TorchSharp.dll'...
val t: torch.Tensor = [10], type = Float64, device = cpu
> t.std() |> float;;
val it: float = 3.027650354 |
@nhirschey Thank you for helping test!! A couple notes:
Hopefully this clears up a few things. If I have some time this weekend I'll see if I can fix the rpath issue, though I don't know a ton about how that works. |
In my case, all test has passed without extra action like building custom pytorch, I just execute the test in jb rider. As far as I remember, crc32c is used for tensorboard, maybe extra test on tensorboard is needed. |
#if defined(__x86_64__) || defined(__i386__) | ||
#include <cpuid.h> | ||
#include <x86intrin.h> | ||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe affect to tensorboard feature?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have no idea about how crc32c.c works so @NiklasGustafsson Can you look at this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I can tell, HW support was removed from crc32c in #2877164, so this shouldn't affect anything. I believe cpuid/x86intrin are only needed for the hardware crc instruction, which is no longer being used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then, how about just remove that include lines?
It would be great to get this merged, even if this is not bullet proof, so long it doesn't break the x64 backend and it helps people who want to contribute to this stack but are using macos-arm64. I gave a shot at it in DiffSharp: https://github.com/DiffSharp/DiffSharp/blob/f018184043fefae382bd04cba1756a3c348b6930/DEVGUIDE.md#torchsharp-backend-on-macos-arm64 there is only 1 failing test to investigate in their repository, using the backend I compiled out of this PR. |
I agree. The CLA is apparently not agreed, though. Since you first posted this, we have moved to PyTorch 2.0.1. Before merging, we would have to do the same here. Since the strategy to extract the native binaries is different, that may require some finagling. |
@dotnet-policy-service agree |
And by "we," I mean "you." :-) I say that, since this needs to be tested by someone with the right HW... |
I think this PR has broken now...
This is my part of log with just cloning with the PR then trying to run Maybe recent version of mac might break those things... Because if I tested on past year, it seems look fine
Also, this PR looks like using custom built version of libtorch rather then official version. Because official version of arm64 not uploaded to Official webpage. It looks like we need to provide some official built version of libtorch(including libtorchsharp) ourself. This PR seems like using the version someone uploaded to somewhere but I have no idea because I'm not farmilar with .NET's XML build script... |
One more: crc32c.c and crc32c.h uses HW dependent source codes which we doesn't uses anymore. How about just removing those lines: #if defined(__x86_64__) || defined(__i386__)
#include <cpuid.h>
#include <x86intrin.h>
#endif
#else
#include <intrin.h>
#endif This will gives better experience to support new platforms which will supported future |
@osum4est @dayo05 -- do we want to keep this PR open? It sounds like it needs some updating if it's going to work. I do think it's very valuable, but I don't have the HW to test it myself. |
Recently, it seems github enabled action which targets macos arm64(github/roadmap#528) Because I have native HW for it, I can make som validation for major backend change if you mention me @NiklasGustafsson . But this pr seems broken now(mayve version issue? But I'm not sure) It seems pr author not responding to this for long time so if he/she continues to not reply, may I reimplement it later? |
@osum4est -- I have it merged, and after installing the right version of 'cmake' on my Mac, it builds and tests run. Adding MPS support also doesn't seem too hard. It seems like version 2.20 has added the ARM64 binaries to the C++ download zip file, so we can rely on that instead of grabbing the conda package. It's been a while since you submitted this -- do you want to be involved in taking it all the way? |
Awesome, glad you got it working. Sorry, I'm busy with other things at the moment and am not currently using this library, so I don't have time to wrap this up. |
Okay, then I'll finish it up. I'll update TorchSharp to v2.2.0 of libtorch, first, since the config has changed. Your work was very much appreciated -- it's a solid foundation for what needs to be done. |
Let's keep this PR open for reference. |
Closing this PR as I'm about to post another one. So many thanks, @osum4est, for getting it started! |
Linked issues: #876 #652
Hi! This PR adds proper arm64 build support for M1 macs. Here's some notes:
13
), and adding relevant checks in THSModule/THSJIT. Though we would also want to addtorch.mps.is_available()
and the rest of the mps specific functions, tests, etc.TargetArchitecture
property tox64
orarm64
I think that's about it! Please let me know how it looks and if anything needs to be changed/if I missed anything. I'm using TorchSharp in a project I'm working on and needed M1 support, and would love to be able to pull the arm64 backend from nuget instead of having to manually load it.