티스토리 뷰
[C++] torch scriptModule 실행 시 Unknown type name 'NoneType'
happy_dohee 2021. 10. 6. 21:51Local machine, 그리고 서버에서 잘 동작하는지 확인하고 온건데...
막상 이 모델을 실행시켜야하는 docker container 안에 들어오니 어떤 환경이 달라서인지 다음과 같은 에러가 떴다.
실행코드
#include <torch/torch.h>
#include <torch/script.h>
int main(int args, char **argv){
std::string modelfile = argv[1];
torch::jit::scriptModule model_;
model_ = torch::jit::load(modelfile);
return 0;
}
에러 메시지
terminate called after throwing an instance of 'torch::jit::ErrorReport'
what():
Unknown type name 'NoneType':
Serialized File "code/__torch__/torch/nn/modules/conv.py", line 7
bias : Optional[Tensor]
training : bool
_is_full_backward_hook : NoneType
~~~~~~~~ <--- HERE
transposed : bool
_reversed_padding_repeated_twice : List[int]
Aborted (core dumped)
Unknown type name 'NoneType'이라... 흠...
구글링.. 열심히 구글링..
해결방법
torch==1.6으로 downgrade한 후 scriptModule을 만들고 저장하면 됨!
(이유: 아래 issue를 참고하면 NoneType을 가지는 _is_full_backward_hook은 최근에 추가된 field라 아직 support되지 않을 수 있다고 한다. Triton이 뭔지는 모르겠음.)
https://github.com/pytorch/pytorch/issues/62313
On Triton inference server, the TorchScript .pt file, exported by 1.9.0, serves unsuccessfully · Issue #62313 · pytorch/pytorc
🐛 Bug I trained the model and export it to torchscript file by scripting (Torch == 1.9.0) Then I want to serve it on Triton server, but I found: E0728 06:06:45.028234 1 model_repository_manager.cc:...
github.com
나 역시도 scriptModule을 pytorch==1.9에서 만들어서 저장했다.
torch version을 낮춘 후 scriptModule을 저장하니 c++에서도 ㅇㅋㅇㅋ 잘 돌아갔다.
해 본 시도 중 왜 안되는지 모르겠는... 그래도 언젠간 이런 게 쓸모 있을 것 같아서 적어둠.
https://forums.developer.nvidia.com/t/torchscripted-pytorch-lightning-module-fails-to-load/181002
Torchscripted Pytorch Lightning Module Fails to Load
I’m attempting to launch a triton server instance with a torchscripted module. The module was trained with assistance from pytorch lightning so has many internal variables which I think is causing the below error. The torchscripted module runs without is
forums.developer.nvidia.com
여기서 본 대로 value가 None인 attributes를 찾아 없애고 저장하는 방법이 있다.
net.eval()
remove_attributes = []
for key, value in vars(net).items():
if value is None:
# print(key)
remove_attributes.append(key)
for key in remove_attributes:
delattr(net, key)
model_ = torch.jit.script(net)
torch.jit.save(model_, "model.pt")
이렇게 해봤는데 문제는... 지금 저 value가 None인 것의 key를 출력해보면 C++에서 봤던 에러 메시지와 동일하게
_is_full_backward_hook 이 출력된다.
그런데 문제는... 저 저장한 model을 다음에 다시 불러오게 되면 저 _is_full_backward_hook이 다시 생겨있다는 것..
(아래 코드 참조)
net.eval()
remove_attributes = []
for key, value in vars(net).items():
if value is None:
print(key) # _is_full_backward_hook
remove_attributes.append(key)
print(len(remove_attributes)) # 1
for key in remove_attributes:
delattr(net, key)
for key, value in vars(net).items():
if value is None:
remove_attributes.append(key)
print(len(remove_attributes)) # 0이 나옴으로써 value가 None인 attribute가 사라졌음을 의미
model_ = torch.jit.script(net)
torch.jit.save(model_, "model.pt")
net = torch.jit.load("model.pt")
for key, value in vars(net).items():
if value is None:
print(key) # _is_full_backward_hook 가 다시 나옴...
remove_attributes.append(key)
print(len(remove_attributes)) # 1
그래서 이 방법으로는 해결이 안됐다.
그러나 python 내에서만 save and load 해서 쓴다면 이 방법도 언젠가는 쓸모가 있을 것 같아서 적어둔다.
'이것저것 자료 > C++' 카테고리의 다른 글
[C++] RuntimeError: Cannot input a tensor of dimension other than 0 as a scalar argument (0) | 2021.10.06 |
---|---|
[C++] warning: backslash and newline separated by space (0) | 2021.03.25 |
- Total
- Today
- Yesterday
- dfs
- 코딩테스트
- 설치하기
- 이것이코딩테스트다
- shellscript
- tensorflow
- notfound
- 백트래킹
- Python
- BFS
- numpy
- 프로그래머스
- LGSVL
- matplotlib
- version
- 파이썬
- PIP
- torch
- 백준
- 카카오
- 다익스트라
- docker
- 설치
- error
- 최소신장트리
- 동적프로그래밍
- n과m
- CUDA
- pytorch
- torchscript
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |