TorchSharp built from source results OverflowException

109 views Asked by At

I am trying to compile TorchSharp library. While building is going rather without problems, I cannot successfully run the example program because I am getting overflow error.

I run along instructions from Developer Guide: https://github.com/dotnet/TorchSharp/blob/main/DEVGUIDE.md.

I ran:

dotnet build /p:SkipNative=true
dotnet build
dotnet test
dotnet pack -o myBuiltPackage

When running test, I was skipping failing ones.

In my project, I installed libtorch-cuda-11.7-win-x64 from Nuget.org and then built TorchSharp from local file system.

I tried to run example program from TorchSharp Github page:

using TorchSharp;
using static TorchSharp.torch.nn;

var lin1 = Linear(1000, 100);
var lin2 = Linear(100, 10);
var seq = Sequential(("lin1", lin1), ("relu1", ReLU()), ("drop1", Dropout(0.1)), ("lin2", lin2));

var x = torch.randn(64, 1000);
var y = torch.randn(64, 10);

var optimizer = torch.optim.Adam(seq.parameters());

for (int i = 0; i < 10; i++) {
    var eval = seq.forward(x);
    var output = functional.mse_loss(eval, y, Reduction.Sum);

    optimizer.zero_grad();

    output.backward();

    optimizer.step();
}

Unfortunately, it results with Overflow error:

[W C:\actions-runner\_work\pytorch\pytorch\builder\windows\pytorch\torch\csrc\jit\codegen\cuda\interface.cpp:47] Warning: Loading nvfuser library failed with: error in LoadLibrary for nvfuser_codegen.dll. WinError 126: The specified module could not be found.
 (function LoadingNvfuserLibrary)
Unhandled exception. System.OverflowException: Arithmetic operation resulted in an overflow.
   at TorchSharp.PinnedArray`1.CreateArray(Int32 length)
   at TorchSharp.PinnedArray`1.CreateArray(IntPtr length)
   at TorchSharp.PInvoke.NativeMethods.THSNN_Module_get_named_parameters(HType module, AllocatePinnedArray allocator1, AllocatePinnedArray allocator2)
   at TorchSharp.torch.nn.Module._named_parameters()
   at TorchSharp.torch.nn.Module.register_p_and_b()
   at TorchSharp.torch.nn.Module..ctor(IntPtr handle, Nullable`1 boxedHandle, Boolean ownsHandle)
   at TorchSharp.torch.nn.HookableModule`2..ctor(IntPtr handle, IntPtr boxedHandle)
   at TorchSharp.torch.nn.Module`2..ctor(IntPtr handle, IntPtr boxedHandle)
   at TorchSharp.Modules.Linear..ctor(IntPtr handle, IntPtr boxedHandle)
   at TorchSharp.torch.nn.Linear(Int64 inputSize, Int64 outputSize, Boolean hasBias, Device device, Nullable`1 dtype)
   at Program.<Main>$(String[] args) in D:\sharptorch\sharptorch_example\ExampleSharpTorch\ExampleSharpTorch\Program.cs:line 4

D:\sharptorch\sharptorch_example\ExampleSharpTorch\ExampleSharpTorch\bin\Debug\net6.0\ExampleSharpTorch.exe (process 7720) exited with code -532462766.

I am using Windows 10, Visual Studio 17.6.4, dotnet 7.0.304. When installing TorchSharp from Nuget.org, everything works fine.

0

There are 0 answers