RuntimeError: Unknown IValue type for pickling: Device

178 views Asked by At

i want convert pytorch model to torchscript, the model is a pytorch LSTM module.

step 1: score_pos is normal as expected

step 2: convert to torchscript module

step 3: the score score_pos_scripted run by scripted module is same with score_pos but with wrong order

step 4: save torchscript module error!

# result is right
score_pos = model(src, src_len, trg_pos, trg_len_pos)
scripted_model = torch.jit.script(model)
score_pos_scripted = scripted_model(src, src_len, trg_pos, trg_len_pos)
# error
scripted_model.save("dual_encoder_scripted.pt")

the error message(my pytorch version:1.3.1):

RuntimeError: Unknown IValue type for pickling: Device (pushIValueImpl at /pytorch/torch/csrc/jit/pickler.cpp:125)
frame #0: c10::Error::Error(c10::SourceLocation, std::string const&) + 0x33 (0x7fa8194cd813 in /mnt/d/anaconda3/lib/python3.6/site-packages/torch/lib/libc10.so)
frame #1: <unknown function> + 0x3e1de5e (0x7fa7c5076e5e in /mnt/d/anaconda3/lib/python3.6/site-packages/torch/lib/libtorch.so)
frame #2: <unknown function> + 0x3e1e11b (0x7fa7c507711b in /mnt/d/anaconda3/lib/python3.6/site-packages/torch/lib/libtorch.so)
frame #3: <unknown function> + 0x3e1da36 (0x7fa7c5076a36 in /mnt/d/anaconda3/lib/python3.6/site-packages/torch/lib/libtorch.so)
frame #4: <unknown function> + 0x3e1e13b (0x7fa7c507713b in /mnt/d/anaconda3/lib/python3.6/site-packages/torch/lib/libtorch.so)
frame #5: <unknown function> + 0x3e1da36 (0x7fa7c5076a36 in /mnt/d/anaconda3/lib/python3.6/site-packages/torch/lib/libtorch.so)
frame #6: <unknown function> + 0x3e1e13b (0x7fa7c507713b in /mnt/d/anaconda3/lib/python3.6/site-packages/torch/lib/libtorch.so)
frame #7: <unknown function> + 0x411f045 (0x7fa7c5378045 in /mnt/d/anaconda3/lib/python3.6/site-packages/torch/lib/libtorch.so)
frame #8: <unknown function> + 0x4126df6 (0x7fa7c537fdf6 in /mnt/d/anaconda3/lib/python3.6/site-packages/torch/lib/libtorch.so)
frame #9: torch::jit::ExportModule(torch::jit::script::Module const&, std::string const&, std::unordered_map<std::string, std::string, std::hash<std::string>, std::equal_to<std::string>, std::allocator<std::pair<std::string const, std::string> > > const&, bool) + 0x20d (0x7fa7c538291d in /mnt/d/anaconda3/lib/python3.6/site-packages/torch/lib/libtorch.so)
frame #10: <unknown function> + 0x57b95f (0x7fa819eaf95f in /mnt/d/anaconda3/lib/python3.6/site-packages/torch/lib/libtorch_python.so)
frame #11: <unknown function> + 0x211014 (0x7fa819b45014 in /mnt/d/anaconda3/lib/python3.6/site-packages/torch/lib/libtorch_python.so)
frame #12: _PyCFunction_FastCallDict + 0x154 (0x7fa83a30eb94 in /mnt/d/anaconda3/bin/python)
frame #13: _PyObject_FastCallDict + 0x2bf (0x7fa83a30efaf in /mnt/d/anaconda3/bin/python)
frame #14: _PyObject_Call_Prepend + 0x63 (0x7fa83a313a03 in /mnt/d/anaconda3/bin/python)
frame #15: PyObject_Call + 0x3e (0x7fa83a30e99e in /mnt/d/anaconda3/bin/python)
frame #16: _PyEval_EvalFrameDefault + 0x1ab0 (0x7fa83a3c2470 in /mnt/d/anaconda3/bin/python)
frame #17: <unknown function> + 0x197a94 (0x7fa83a397a94 in /mnt/d/anaconda3/bin/python)
frame #18: <unknown function> + 0x198941 (0x7fa83a398941 in /mnt/d/anaconda3/bin/python)
frame #19: <unknown function> + 0x19e755 (0x7fa83a39e755 in /mnt/d/anaconda3/bin/python)
frame #20: _PyEval_EvalFrameDefault + 0x2fa (0x7fa83a3c0cba in /mnt/d/anaconda3/bin/python)
frame #21: PyEval_EvalCodeEx + 0x329 (0x7fa83a399459 in /mnt/d/anaconda3/bin/python)
frame #22: PyEval_EvalCode + 0x1c (0x7fa83a39a1ec in /mnt/d/anaconda3/bin/python)
frame #23: <unknown function> + 0x1be6cb (0x7fa83a3be6cb in /mnt/d/anaconda3/bin/python)
frame #24: _PyCFunction_FastCallDict + 0x91 (0x7fa83a30ead1 in /mnt/d/anaconda3/bin/python)
frame #25: <unknown function> + 0x19e860 (0x7fa83a39e860 in /mnt/d/anaconda3/bin/python)
frame #26: _PyEval_EvalFrameDefault + 0x2fa (0x7fa83a3c0cba in /mnt/d/anaconda3/bin/python)
frame #27: <unknown function> + 0x197a94 (0x7fa83a397a94 in /mnt/d/anaconda3/bin/python)
frame #28: <unknown function> + 0x198941 (0x7fa83a398941 in /mnt/d/anaconda3/bin/python)
frame #29: <unknown function> + 0x19e755 (0x7fa83a39e755 in /mnt/d/anaconda3/bin/python)
frame #30: _PyEval_EvalFrameDefault + 0x2fa (0x7fa83a3c0cba in /mnt/d/anaconda3/bin/python)
frame #31: <unknown function> + 0x197a94 (0x7fa83a397a94 in /mnt/d/anaconda3/bin/python)
frame #32: <unknown function> + 0x198941 (0x7fa83a398941 in /mnt/d/anaconda3/bin/python)
frame #33: <unknown function> + 0x19e755 (0x7fa83a39e755 in /mnt/d/anaconda3/bin/python)
frame #34: _PyEval_EvalFrameDefault + 0x2fa (0x7fa83a3c0cba in /mnt/d/anaconda3/bin/python)
frame #35: <unknown function> + 0x19870b (0x7fa83a39870b in /mnt/d/anaconda3/bin/python)
frame #36: <unknown function> + 0x19e755 (0x7fa83a39e755 in /mnt/d/anaconda3/bin/python)
frame #37: _PyEval_EvalFrameDefault + 0x2fa (0x7fa83a3c0cba in /mnt/d/anaconda3/bin/python)
frame #38: PyEval_EvalCodeEx + 0x329 (0x7fa83a399459 in /mnt/d/anaconda3/bin/python)
frame #39: PyEval_EvalCode + 0x1c (0x7fa83a39a1ec in /mnt/d/anaconda3/bin/python)
frame #40: <unknown function> + 0x2149a4 (0x7fa83a4149a4 in /mnt/d/anaconda3/bin/python)
frame #41: PyRun_FileExFlags + 0xa1 (0x7fa83a414da1 in /mnt/d/anaconda3/bin/python)
frame #42: PyRun_SimpleFileExFlags + 0x1c4 (0x7fa83a414fa4 in /mnt/d/anaconda3/bin/python)
frame #43: Py_Main + 0x63e (0x7fa83a418a9e in /mnt/d/anaconda3/bin/python)
frame #44: main + 0xee (0x7fa83a2e04be in /mnt/d/anaconda3/bin/python)
frame #45: __libc_start_main + 0xe7 (0x7fa839801b97 in /lib/x86_64-linux-gnu/libc.so.6)
frame #46: <unknown function> + 0x1c7773 (0x7fa83a3c7773 in /mnt/d/anaconda3/bin/python)

can somebody help me about step 2 and step 4 error?

1

There are 1 answers

0
Addison Klinke On

I had a similar error in 1.4.0 and it is gone after upgrading to 1.8.1. The release notes for 1.8.0 reference PR #46441 which

adds hashing for many other types including Tuple , bool , device by implementing generic hashing on IValue

I believe this is the improvement that resolved the root issue