TorchSharp icon indicating copy to clipboard operation
TorchSharp copied to clipboard

TorchSharp is slower on NetFX comparing to Net Core

Open tarekgh opened this issue 3 years ago • 5 comments

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.

tarekgh avatar Mar 10 '22 18:03 tarekgh

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`)

lostmsu avatar Jul 05 '22 16:07 lostmsu

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.

tarekgh avatar Jul 05 '22 18:07 tarekgh

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.

MovGP0 avatar Feb 19 '23 13:02 MovGP0

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 avatar Mar 17 '25 10:03 alinpahontu2912

@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.

tarekgh avatar Mar 17 '25 14:03 tarekgh