TorchSharp icon indicating copy to clipboard operation
TorchSharp copied to clipboard

torch.nn.Sequential throws exception from test project

Open xhuan8 opened this issue 3 years ago • 10 comments

With a simple call to Sequential

var a = torch.nn.Sequential();

from unit test it can work, but when call from test project it throws exception.

System.OverflowException: Arithmetic operation resulted in an overflow.at System.lntPtr.op Explicit(lntPtr value)at TorchSharp.PinnedArray 1.CreateArray(lntPtr length)at
TorchSharp.PInvoke.LibTorchSharp.THSNN Module get named parameters(HType module, AllocatePinnedArray allocator1, AllocatePinnedArray allocator2)at TorchSharp.torch.nn.Module. named parameters()at TorchSharp.torch.nn.Module..ctor(lntPtr handle, Nullable'1 boxedHandle,Boolean ownsHandle)
at TorchSharp.torch.nn.Module2..ctor(lntPtr handle, IntPtr boxedHandle)at TorchSharp.Modules.Sequential..ctor(lntPtr handle)
at TorchSharp.torch.nn.Sequential0)
at TorchSharpTest.MainWindow.button3 Click(Object senderRoutedEventArgs e) in
D:\Code\TorchSharpTest TorchSharpTest TorchSharpTest MainWindow.xaml.cs:line 157

try to accesse the variable still got exception

void THSNN_Module_get_named_parameters(const NNModule module, Tensor* (*allocator1)(size_t length), const char** (*allocator2)(size_t length))
{
    auto parameters = (*module)->named_parameters();
    auto size = parameters.size(); // try it here
    Tensor* result1 = allocator1(parameters.size());
    const char** result2 = allocator2(parameters.size());

    for (size_t i = 0; i < parameters.size(); i++)
    {
        result1[i] = ResultTensor(parameters[i].value());
        result2[i] = make_sharable_string(parameters[i].key());
    }
}
System.AccessViolationException: Attempted to read or write protectedmemory. This is often an indication that other memory is corrupt
at
[orchSharp.PInvoke.LibTorchSharp.THSNN Module get named parameters(HType module, AllocatePinnedArray allocator1, AllocatePinnedArray allocator2)at TorchSharp.torch.nn.Module. named parameters()at TorchSharp.torch.nn.Module..ctor(lntPtr handle, Nullable'1 boxedHandle,Boolean ownsHandle)
at TorchSharp.torch.nn.Module 2..ctor(lntPtr handle, IntPtr boxedHandle)at TorchSharp.Modules.Sequential..ctor(lntPtr handle)
at TorchSharp.torch.nn.Sequential0
at TorchSharpTest.MainWindow.button3 Click(Object sender,RoutedEventArgs e) in
D:\Code\TorchSharpTest)TorchSharpTest TorchSharpTest\MainWindow.xaml.cs:line 157

xhuan8 avatar Jan 18 '23 10:01 xhuan8

Could you post a little bit more of your code? I'd like to reproduce it, but what does the code look like that causes the error?

NiklasGustafsson avatar Jan 19 '23 01:01 NiklasGustafsson

thanks, there is only one line of code in a WPF project

private void button3_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var resNet = torch.nn.Sequential();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

xhuan8 avatar Jan 19 '23 01:01 xhuan8

So, nothing happens in a console app, presumably? I wonder if things work better if there's some call to TorchSharp on the main thread when the app starts -- some threading issue... If there's a first call on the main thread, maybe that will initialize things.

I'm really grasping at straws. I'll have to look into this later this week.

NiklasGustafsson avatar Jan 19 '23 01:01 NiklasGustafsson

ok, it's strange, also occurs in a console application

xhuan8 avatar Jan 19 '23 01:01 xhuan8

I wonder if it's because the Sequential is empty.

NiklasGustafsson avatar Jan 19 '23 01:01 NiklasGustafsson

I tried version 99.2 it's gone, maybe because my local code is corrupt.

xhuan8 avatar Jan 19 '23 01:01 xhuan8

It happens on latest code when build from source, all tests passed, but Sequential still throws excpetion.

even with some submodules as parameter

var resNet = torch.nn.Sequential(("lin1", nn.Linear(1000, 100, false)));

but version 99.2 from nuget can work, so maybe the packing process has some problem.

xhuan8 avatar Jan 28 '23 05:01 xhuan8

THSNN_Module_get_named_parameters failed to retriver parameters from module, seems the pointer point to some invalid address

System.OverflowException: Arithmetic operation resulted in an overflow.at System.lntPtr.op Explicit(lntPtr value)at TorchSharp.PinnedArray 1.CreateArray(lntPtr length)at
TorchSharp.PInvoke.LibTorchSharp.THSNN Module get named parameters(HType module, AllocatePinnedArray allocator1, AllocatePinnedArray allocator2)at TorchSharp.torch.nn.Module. named parameters()at TorchSharp.torch.nn.Module..ctor(lntPtr handle, Nullable'1 boxedHandle,
Boolean ownsHandle)at TorchSharp.torch.nn.Module2..ctor(IntPtr handle, IntPtr boxedHandle)at TorchSharp.ModulesLinear..ctor(lntPtr handle, IntPtr boxedHandle)at TorchSharp.torch.nn.Linear(lnt64 inputSize, Int64 outputSize, BooleanhasBias, Device device, Nullable'1 dtype)at TorchSharpTest.MainWindow.button3 Click(Object sender,RoutedEventArgs e) inD:\Code TorchSharpTest TorchSharpTest TorchSharpTest MainWindow.xaml.cs

xhuan8 avatar Jan 28 '23 05:01 xhuan8

@xhuan8 -- is this still happening?

NiklasGustafsson avatar Nov 14 '23 21:11 NiklasGustafsson

haven't tested on the latest version.

xhuan8 avatar Nov 15 '23 10:11 xhuan8

@xhuan8 is this still relevant, can you check with the latest 0.105 version ?

alinpahontu2912 avatar Mar 12 '25 10:03 alinpahontu2912

@alinpahontu2912 I didn't work on it for a long time, so close it first if you cannot reproduce it.

xhuan8 avatar Mar 12 '25 12:03 xhuan8