ankane / torch.rb

Deep learning for Ruby, powered by LibTorch
Other
704 stars 30 forks source link

RuntimeError isGenericDict() INTERNAL ASSERT FAILED #21

Closed 007lva closed 3 years ago

007lva commented 3 years ago

Hello, I've got this error when try to load my model:

irb(main):001:0> Torch.load('model_ruby_v2.pth')
Traceback (most recent call last):
        1: from (irb):1
RuntimeError (isGenericDict() INTERNAL ASSERT FAILED at "../aten/src/ATen/core/ivalue_inl.h":923, please report a bug to PyTorch. Expected GenericDict but got None)

I've tried also pointing to the current master branch with the hope that changes from #18 fixes it but still get the same error. This model was generated with Python, Pytorch 10.1 and Cuda 10.1, I've tried with more versions but still can't get it working

orlando-labs commented 3 years ago

Hi, @007lva. This is libtorch-related issue as the message says. I went through it several times. Before saving please convert python OrderedDict to common dict. Then it should load successfully. Something like:

your_model.state_dict().__class__
=> <class 'collections.OrderedDict'>

state_dict = {k: v for k, v in your_model.state_dict().items()}
torch.save(state_dict, 'path/to/file.pth')

or just

dict(your_model.state_dict())
orlando-labs commented 3 years ago

Hi, @ankane, what do you think of noticing this issue in readme as it is widespread with libtorch and we aren't able to fix it.

007lva commented 3 years ago

Thanks @orlando-labs, it works!

ankane commented 3 years ago

Thanks @007lva and @orlando-labs . Added a note to the readme to address both https://github.com/pytorch/pytorch/issues/37213 and https://github.com/pytorch/pytorch/issues/42651.

mikeyEcology commented 3 years ago

Following the response from @orlando-labs, I am doing this to save my models now in python when I plan to load them in R.

torch.save(dict(model.to(device='cpu').state_dict()), path2weights)