#Referencing and loading native libraries at runtime - TorchSharp integration

1 messages · Page 1 of 1 (latest)

eternal heron
#

Hi all, I'm wanting to experiment with some machine learning in Unity and whilst ML-Agents seems very capable I would also like a solution that is .NET only. There is a wrapper for libtorch to provide the fundamentals and I'm trying to get that integrated into Unity. There seem to be a number of successful attempts at doing this and it should be a good learning exercise for me to understand the guts of C# a little (assemblies etc.). I have got the required packages installed in my Visual Studio solution via Nuget, which seem to be loaded ok, but I'm struggling getting Unity to load all the libraries. By copying the generic libraries into 'Assets/Plugins' Unity picks them up and tries to load it, but I don't know how to get the native runtime binaries to be referenced. Entering Play in Unity I get:

"NotSupportedException: This application or script uses TorchSharp but doesn't contain a reference to libtorch-cpu-win-x64, Version=2.5.1.0.

Consider referencing one of the combination packages TorchSharp-cpu, TorchSharp-cuda-linux, TorchSharp-cuda-windows or call System.Runtime.InteropServices.NativeLibrary.Load(path-to-LibTorchSharp.dll) explicitly for a Python install of pytorch. See https://github.com/dotnet/TorchSharp/issues/169.".

There is additional trace info just logging what and where it is trying to load, but is unable to find the libraries.
I have tried to create a response file, but get an error about missing metadata and won't compile any more:

'Assets\Plugins\TorchSharp\Windows\x86_64\LibTorchSharp.dll' could not be opened -- PE image doesn't contain managed metadata.

Unity knows it is native though, not managed. I also don't seem to be able to load it through script using the System... call above as NativeLibrary does not exist in .NET framework, only .NET core.

I am fully aware it is a lack of knowledge on my part, but I just can't seem to find the right resources/direction to learn from (likely missing something simple)!
Thanks, Daf!

GitHub

A .NET library that provides access to the library that powers PyTorch. - dotnet/TorchSharp

tropic pollen
#

It probably not gonna run anyway, as it likely targets one of the recent dotnet version, while unity doesn't.

#

Your best bet is to use solution that already exist for unity, or native libraries, or an external process that you communicate with via interprocess communication.

#

I'd just stick with ml agents as long in unity and use a different solution(if you really need to) outside of unity.

eternal heron
#

Thanks for the reply, there are other libraries that apparently run off it (RL_Matrix i think its called), and needs only .NET Standard 2.0 (https://github.com/dotnet/TorchSharp/discussions/874) hence why I'm sure it's a 'me' limitation not Unity 😃 It is more of an experiment rather than for anything actually planned and happy to play around with ML-Agents/python rather than spending too long on this.
(Edit)It was more of a general question regarding referencing libraries at runtime in Unity as my searching doesn't really come up with much information, and I hoped this was a fairly common (and probably simple!) function other devs might be doing.

GitHub

Hi, I was wondering if anyone has had success using TorchSharp in Unity. I've seen some discussion elsewhere of people having mixed results trying to use TorchSharp in Unity, but none were very...

tropic pollen
eternal heron
#

I've been going through those docs, but couldn't really figure out what I needed to do (hence a 'me' issue), but I'll take another pass through them. Cheers

tropic pollen
#

Maybe you can ask about specific things that you couldn't figure out then. This would be easier to answer.

eternal heron
#

ok, we've got there! I wasn't sure how to use the [DllImport] attribute, as I wasn't calling any functions within those dll's myself. I found how to load native libraries in Windows by writing the extern wrapper around kernel32.dll and its loadlibrary function, so rather than [DllImport] the specific libraries, its [DllImport("kernel32.dll")] loadlibrary with the path of the required libs and gets the IntPtr that Unity uses.
Thanks for you pointers in this 🙂

#

I guess the key issue here finding out how Windows itself loads libraries and hijacking that

tropic pollen
#

It might be just that you need to place it somewhere it expects to find it.

#

Maybe make a few steps back and analyze the actual issue that you're having/share more info/errors/screenshots.

eternal heron
tropic pollen
tribal niche