Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed internal assertion with torch::pickle_load in LibTorch 1.5.0 #37213

Closed
ankane opened this issue Apr 24, 2020 · 2 comments
Closed

Failed internal assertion with torch::pickle_load in LibTorch 1.5.0 #37213

ankane opened this issue Apr 24, 2020 · 2 comments
Assignees
Labels
high priority module: crash Problem manifests as a hard crash, as opposed to a RuntimeError module: pickle Problems related to pickling of PyTorch objects module: serialization Issues related to serialization (e.g., via pickle, or otherwise) of PyTorch objects oncall: jit Add this issue/PR to JIT oncall triage queue triage review triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Comments

@ankane
Copy link

ankane commented Apr 24, 2020

馃悰 Bug

Hi, I'm running into a failed assertion with torch::pickle_load on PyTorch models.

libc++abi.dylib: terminating with uncaught exception of type c10::Error: isGenericDict() INTERNAL ASSERT FAILED at ../aten/src/ATen/core/ivalue_inl.h:715, please report a bug to PyTorch. Expected GenericDict but got None (toGenericDict at ../aten/src/ATen/core/ivalue_inl.h:715)

To Reproduce

Steps to reproduce the behavior:

  1. Save a tensor and a model in PyTorch
import torch
from torch import nn

torch.save(torch.tensor([1, 2, 3]), "tensor.pt", _use_new_zipfile_serialization=True)

class TheModelClass(nn.Module):
    def __init__(self):
        super(TheModelClass, self).__init__()
        self.conv1 = nn.Conv2d(3, 6, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = x.view(-1, 16 * 5 * 5)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

model = TheModelClass()

torch.save(model.state_dict(), "model.pt", _use_new_zipfile_serialization=True)
  1. Create and compile a C++ application
#include <torch/torch.h>
#include <iostream>
#include <iterator>

int main() {
  // tensor
  std::ifstream file("../tensor.pt", std::ios::binary);
  std::vector<char> data((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
  torch::IValue ivalue = torch::pickle_load(data);
  torch::Tensor tensor = ivalue.toTensor();
  std::cout << tensor << std::endl;

  // model
  std::ifstream file2("../model.pt", std::ios::binary);
  std::vector<char> data2((std::istreambuf_iterator<char>(file2)), std::istreambuf_iterator<char>());
  torch::IValue ivalue2 = torch::pickle_load(data2);
}
  1. Run it

The tensor loads as expected, but the model throws an exception.

Stack Trace

libc++abi.dylib: terminating with uncaught exception of type c10::Error: isGenericDict() INTERNAL ASSERT FAILED at ../aten/src/ATen/core/ivalue_inl.h:715, please report a bug to PyTorch. Expected GenericDict but got None (toGenericDict at ../aten/src/ATen/core/ivalue_inl.h:715)
frame #0: c10::Error::Error(c10::SourceLocation, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) + 135 (0x104aaaf47 in libc10.dylib)
frame #1: c10::IValue::toGenericDict() const & + 280 (0x104b547b8 in libtorch_cpu.dylib)
frame #2: torch::jit::Unpickler::readInstruction() + 5580 (0x10754cc1c in libtorch_cpu.dylib)
frame #3: torch::jit::Unpickler::run() + 472 (0x10754b2d8 in libtorch_cpu.dylib)
frame #4: torch::jit::Unpickler::parse_ivalue() + 34 (0x10754ae32 in libtorch_cpu.dylib)
frame #5: torch::jit::readArchiveAndTensors(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, c10::optional<std::__1::function<c10::StrongTypePtr (c10::QualifiedName const&)> >, c10::optional<std::__1::function<c10::intrusive_ptr<c10::ivalue::Object, c10::detail::intrusive_target_default_null_type<c10::ivalue::Object> > (c10::StrongTypePtr, c10::IValue)> >, c10::optional<c10::Device>, caffe2::serialize::PyTorchStreamReader&) + 838 (0x107580496 in libtorch_cpu.dylib)
frame #6: torch::jit::pickle_load(std::__1::vector<char, std::__1::allocator<char> > const&) + 347 (0x10758539b in libtorch_cpu.dylib)
frame #7: torch::pickle_load(std::__1::vector<char, std::__1::allocator<char> > const&) + 14 (0x1079944ee in libtorch_cpu.dylib)
frame #8: main + 341 (0x104a70745 in example-app)
frame #9: start + 1 (0x7fff79ac53d5 in libdyld.dylib)
frame #10: 0x0 + 1 (0x1 in ???)

Expected behavior

No exception.

Environment

  • PyTorch Version (e.g., 1.0): 1.5.0
  • OS (e.g., Linux): macOS 10.14
  • How you installed PyTorch (conda, pip, source): pip
  • Build command you used (if compiling from source):
  • Python version: 3.7
  • CUDA/cuDNN version: n/a
  • GPU models and configuration: n/a

cc @ezyang @gchanan @zou3519 @suo

@ankane ankane mentioned this issue Apr 24, 2020
17 tasks
@izdeby izdeby added high priority module: crash Problem manifests as a hard crash, as opposed to a RuntimeError labels Apr 26, 2020
@ezyang ezyang added module: serialization Issues related to serialization (e.g., via pickle, or otherwise) of PyTorch objects module: pickle Problems related to pickling of PyTorch objects oncall: jit Add this issue/PR to JIT oncall triage queue and removed triage review labels Apr 27, 2020
@wanchaol wanchaol added triage review triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels Apr 28, 2020
@suo suo self-assigned this May 1, 2020
@suo suo assigned voznesenskym and unassigned suo Jun 5, 2020
@dfalbel
Copy link
Contributor

dfalbel commented Aug 18, 2020

It seems that if you transform all nn.Parameters to true Tensors everything works as expected.

@ankane
Copy link
Author

ankane commented May 19, 2022

Cleaning up stale issues. As @dfalbel mentioned, converting Parameters to Tensors is a way around this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
high priority module: crash Problem manifests as a hard crash, as opposed to a RuntimeError module: pickle Problems related to pickling of PyTorch objects module: serialization Issues related to serialization (e.g., via pickle, or otherwise) of PyTorch objects oncall: jit Add this issue/PR to JIT oncall triage queue triage review triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

No branches or pull requests

7 participants