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
When planning your roadmap, please consider adding apple silicon support. I know that adding support is not trivial, but I think there’s value in supporting the platform.
Apple silicon support is important to allow TorchSharp use in an educational setting. My unscientific survey is that ~50% of students in university classes use apple silicon. It would also make it easier for downstream libraries such as Diffsharp1 and ML.NET's new TextClassification trainer to support Apple silicon.
libtorch-cpu on apple silicon would be sufficient, but gpu-acceleration is also available now in PyTorch 1.12.
Footnotes
It is possible to use the DiffSharp reference backend on apple silicon. ↩
We haven't upgraded to 1.12 yet -- does the Pytorch backend support Mac silicon? It didn't in 1.11, AFAIK. TorchSharp is just a wrapper on top of libtorch.dylib on MacOS, so any silicon support will come from the native implementation.
So, it's not in the stable build, then? We'll definitely wait until it is before picking it up. If it is available in a 1.11 stable build, then we can update the libtorch NuGet packages, and then add the relevant device API logic (device type, index, etc.)
I mainly use windows but I have access to an M1 Mac and can help test.
Also, I think that it is in 1.11 stable. My understanding from PyTorch forums is that the version number distinguishes stable from nightly. Stable is clean "1.11.0", nightly is something like "1.11.0.dev20201109".
This is what I get after installing 1.11 on an m1 Mac:
For what it's worth, this is what happens when I try to load LibTorch manually on M1:
>openSystem.Runtime.InteropServices> NativeLibrary.Load(@"/Users/xxxx/opt/miniconda3/lib/python3.9/site-packages/torch/lib/libtorch.dylib")valit:nativeint =8618041536n> #r "nuget: TorchSharp">openTorchSharp>letlin1= torch.nn.Linear(1000,100)
Binding session to '/Users/xxx/.nuget/packages/torchsharp/0.97.0/lib/netcoreapp3.1/TorchSharp.dll'...
error FS0193: Could not load file or assembly 'TorchSharp, Version=0.97.0.0, Culture=neutral, PublicKeyToken=67b0e5e1da516a1c'.
error FS0193: Could not load file or assembly 'TorchSharp, Version=0.97.0.0, Culture=neutral, PublicKeyToken=67b0e5e1da516a1c, processorArchitecture=AMD64'. The given assembly name or codebase was invalid. (0x80131047)
For what it's worth, I don't think you can point at a conda distribution's native libs. If you are going to replace LibTorch, I believe you have to download and unzip the C/C++ distribution.
Now that 1.13 is out, we should be getting closer to additional platform support. I will release the first version with 1.13 support as 0.99.0, but not do anything to add support for M1. I'll have to ask someone who has access to a machine with M1 support to work on that.
@derekchiang I haven't tested this in a while, bug if I recall correctly, TorchSharp-cpu was enough for Metal (unlike CUDA on Windows). You should be able to use some property to query whether mps mode is enabled or not or simply request 'mps' as the device type and see if you get an error or not.
With that being said, I never got a performance boost when running on mps. I think something was/is still missing to get that working properly. In other words, I was able to request and run on mps without errors, but it was way slower than using cpu (at least for me).
I contributed some PR that improved cpu performance on macs. At the time I thought the improvement was coming from mps, but it was solely from cpu after all... :(
Activity
dayo05 commentedon Jul 13, 2022
Maybe same with #643
NiklasGustafsson commentedon Jul 20, 2022
We haven't upgraded to 1.12 yet -- does the Pytorch backend support Mac silicon? It didn't in 1.11, AFAIK. TorchSharp is just a wrapper on top of libtorch.dylib on MacOS, so any silicon support will come from the native implementation.
dayo05 commentedon Jul 20, 2022
I can execute this code on my 14inch macbook pro which provides M1 Pro without rosetta2.
Also, this requires PyTorch 1.11 nightly
nhirschey commentedon Jul 20, 2022
Here’s the 1.12 link for Apple silicon gpu backend support link.
My understanding (as day05) illustrates was that cpu training on ARM64 Apple silicon was working already.
NiklasGustafsson commentedon Jul 20, 2022
So, it's not in the stable build, then? We'll definitely wait until it is before picking it up. If it is available in a 1.11 stable build, then we can update the libtorch NuGet packages, and then add the relevant device API logic (device type, index, etc.)
NiklasGustafsson commentedon Jul 20, 2022
Also, I don't have access to a M1-based Mac... Once we're on 1.12, I may have to ask someone in the community to do the integration.
nhirschey commentedon Jul 27, 2022
I mainly use windows but I have access to an M1 Mac and can help test.
Also, I think that it is in 1.11 stable. My understanding from PyTorch forums is that the version number distinguishes stable from nightly. Stable is clean "1.11.0", nightly is something like "1.11.0.dev20201109".
This is what I get after installing 1.11 on an m1 Mac:
Then in Python:
nhirschey commentedon Jul 27, 2022
For what it's worth, this is what happens when I try to load LibTorch manually on M1:
NiklasGustafsson commentedon Jul 27, 2022
For what it's worth, I don't think you can point at a conda distribution's native libs. If you are going to replace LibTorch, I believe you have to download and unzip the C/C++ distribution.
NiklasGustafsson commentedon Jul 27, 2022
Also, I've started work on moving on to 1.12 binaries. Running into some problems, though. Hopefully, we can be on 1.12 next week some time.
NiklasGustafsson commentedon Sep 22, 2022
We will skip 1.12 compat and integrate with 1.13 when that comes out later. That should give us M1 support, too.
nhirschey commentedon Sep 22, 2022
Sounds good, thank you Niklas.
ericstj commentedon Oct 7, 2022
@NiklasGustafsson would that give us ARM64 on linux too? I see that's mentioned here: https://pytorch.org/blog/prototype-features-now-available-apis-for-hardware-accelerated-mobile-and-arm64-builds/#arm64-builds-for-linux
NiklasGustafsson commentedon Nov 3, 2022
@nhirschey, @ericstj, @dayo05
Now that 1.13 is out, we should be getting closer to additional platform support. I will release the first version with 1.13 support as 0.99.0, but not do anything to add support for M1. I'll have to ask someone who has access to a machine with M1 support to work on that.
dayo05 commentedon Nov 4, 2022
Reminder: I've just tried to compile libtorchsharp, I got cpuid.h is only exists on x86 system. Maybe we should resolve this
dayo05 commentedon Nov 9, 2022
#837 Will be help @NiklasGustafsson
tugbargn commentedon May 4, 2023
Is there any update about this issue?
NiklasGustafsson commentedon Feb 27, 2024
Addressed in release v0.102.0
ejhg commentedon Jul 9, 2024
Thanks for adding this! Did this also enable the Metal backend (mps) or simply arm CPU?
https://pytorch.org/docs/stable/notes/mps.html
NiklasGustafsson commentedon Jul 9, 2024
It enables the 'MPS' backend, too.
NiklasGustafsson commentedon Jul 9, 2024
See:
TorchSharp/src/TorchSharp/Tensor/Tensor.cs
Line 787 in b6e4706
ejhg commentedon Jul 14, 2024
Awesome! Thank you very much @NiklasGustafsson !
ejhg commentedon Jul 27, 2024
@NiklasGustafsson These torch.mps apis are not implemented, right? https://pytorch.org/docs/stable/mps.html
I'm looking for
synchronizeandprofiler.*.derekchiang commentedon Sep 1, 2025
Just to be clear, do I need to do anything specific to make TorchSharp use Metal on Apple Silicon? It's not clear to me if
TorchSharp-cpuuses Metal.ejhg commentedon Sep 1, 2025
@derekchiang I haven't tested this in a while, bug if I recall correctly,
TorchSharp-cpuwas enough for Metal (unlike CUDA on Windows). You should be able to use some property to query whethermpsmode is enabled or not or simply request 'mps' as the device type and see if you get an error or not.for reference, from an old project, I had:
With that being said, I never got a performance boost when running on
mps. I think something was/is still missing to get that working properly. In other words, I was able to request and run onmpswithout errors, but it was way slower than usingcpu(at least for me).I contributed some PR that improved
cpuperformance on macs. At the time I thought the improvement was coming frommps, but it was solely fromcpuafter all... :(