TorchSharp icon indicating copy to clipboard operation
TorchSharp copied to clipboard

Using TorchSharp In Unity

Open StefanWRL opened this issue 1 year ago • 2 comments

I am trying to use the latest TorchSharp in my Unity project.

I keep running into this error: NotSupportedException: This application or script uses TorchSharp but doesn't contain a reference to libtorch-cpu-win-x64, Version=2.4.0.0.

I have TorchSharp and libtorch-cpu-win-x64 packages installed with Nuget for unity. I have also tried doing this manually with the same results.

Thanks for any help!

VisualStudio 2022 Pytorch 2.4.0+cpu Python 3.9 libtorch-cpu-win-x64 2.4.0.0 TorchSharp 2.4.0.0

StefanWRL avatar Aug 20 '24 13:08 StefanWRL

https://github.com/dotnet/TorchSharp/discussions/874 https://github.com/dotnet/TorchSharp/discussions/1244 https://github.com/dotnet/TorchSharp/discussions/1371

Perhaps eventually we may need a documentation on Torchsharp in Unity, involving how onnx could be used in some cases.

GeorgeS2019 avatar Aug 20 '24 13:08 GeorgeS2019

Maybe someone could compile this to a guide but here's what you need to do:

  1. Use powershell script to install nuget: https://www.nurupo.io/posts/unityhowtonuget/

  2. In Unity Directory create following: Plugins>Windows>X86_x64

  3. Paste all the TorchSharp dlls in there (I dont remember how to get them, just copied them from .NET 8.0 project I had around) image

  4. Make sure that all the .dll settings in the inspector are like this: image

asieradzk avatar Aug 28 '24 08:08 asieradzk

Maybe someone could compile this to a guide but here's what you need to do:

  1. Use powershell script to install nuget: https://www.nurupo.io/posts/unityhowtonuget/
  2. In Unity Directory create following: Plugins>Windows>X86_x64
  3. Paste all the TorchSharp dlls in there (I dont remember how to get them, just copied them from .NET 8.0 project I had around) ...
  4. Make sure that all the .dll settings in the inspector are like this: ...

Great Thanks. It works. Though it works, but I still have question about libtorchsharp, why after appling CPU config to x64, and it became back to "Any CPU"?

Image

Musosuke avatar Feb 25 '25 18:02 Musosuke

The error you're getting should be solved by https://github.com/dotnet/TorchSharp/pull/1461

alinpahontu2912 avatar Apr 16 '25 13:04 alinpahontu2912