TorchSharp
TorchSharp copied to clipboard
TorchSharp is slower on NetFX comparing to Net Core
We have enabled TorchSharp to run on .NET Framework. We noticed a performance degradation comparing when running against Net Core. One obvious place to look at is the pinvoke calls cost. measuring simple pinvoke seeing the numbers:
| Method | Mean | Error | StdDev |
| NetCore5OPInvoke | 4.371 ns | 0.0107 ns | 0.0095 ns |
| NetFxPInvoke | 7.885 ns | 0.0333 ns | 0.0295 ns |
Need to look in general on the full stack to find out if there is any opportunity for optimization.
FYI in Python.NET we ended up replacing PInvoke with C# unmanaged function pointers, and I have a fork of Roslynator that does conversion automatically: https://github.com/lostmsu/Roslynator/tree/features/ReplacePInvoke (need to be on the extern keyword`)
The interop is already internally using function pointers. It is worth trying to have all pinvokes signatures blittable and not use the built-in marshalling instead.
This is somewhat expected, since there are a ton of performance optimizations in every new version of .NET. Especially .NET 6 and 7 have changed a lot in favor of performance.
Hey @tarekgh, Since .Net 5 is no longer supported, with a new upgrade to .Net 8 coming soon, is this still relevant, can it be closed?
@alinpahontu2912 the issue here the performance degradation on the .NET Framework compared to the .NET Core. Upgrading to .NET 8.0 is irrelevant to this issue. If we don't target .NET Framework anymore then can be closed. Otherwise, I think need to keep it opened.