I have trained my model by means of Lightning framework, and saved a checkpoint to a file.
Now I need to convert it into TorchScript format, in order to use it in C++ for producing.
Is a Lightning checkpoint file in the same format as a regular Pytorch checkpoint?
Can I convert it into TorchScript format only according to the tutorial?
When I trying to do so, I get error as following:
File “/home/leon/py-cpu/lib/python3.11/site-packages/torch/_sources.py”, line 33, in get_source_lines_and_file
raise OSError(msg) from e
OSError: Can’t get source for <class ‘torch.cuda.FloatTensor’>. TorchScript requires source access in order to carry out compilation, make sure original .py files are available.
My code looks like:
# trainning on a server with CUDA(GPU)...
# loading on a PC only with CPU
model = Engine.load_from_checkpoint( 'path/to/checkpoint.file' )
# testing on PC
trainer.test( model, dataloaders=test_ld, verbose=True )
# No errors so far when reached here
# error occurs here
torch.jit.script( model ).save('torchscript_model.pt')
Environments:
- I define my model in another source file, not in the file calling to
torch.jit.script()
; - I trained it on my server with CUDA(GPU), but tried to convert it on my PC(CPU);
Are these relavant?
Thanks!