1

我想将 pytorch 模型转换为 torchscript,这model是一个 pytorchLSTM模块。

第1步:score_pos如预期正常

第 2 步:转换为 torchscript 模块

score_pos_scripted 第3步:脚本模块运行的分数相同score_pos但顺序错误

第4步:保存torchscript模块错误!

# 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")

错误消息(我的 pytorch 版本: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)

有人可以帮我解决第 2 步和第 4 步的错误吗?

4

1 回答 1

0

我遇到了类似的错误,1.4.0升级到1.8.1. 1.8.0参考 PR #46441的发行说明

通过在 IValue 上实现通用散列为许多其他类型添加散列,包括 Tuple 、 bool 、 device

我相信这是解决根本问题的改进

于 2021-06-04T17:21:27.940 回答