Is there any way to convert jit::script::Module to nn::Module

Hi,
I rencently tried to load a trained Pytorch model which could do “Image Style transfer” to a C++ model.
The style transfer needs the gradient of loss w.r.t the input tensor, but it seems that the jit::script::Module can’t pass the gradients to the input tensor.

So I’d like to know is there any way to convert a jit::script::Module to nn::Moudle conveniently?

Many thanks!

Hi, there is no way to convert a jit:script::Module to an nn::Module. However, we should work on making it so that jit::script::Module can be used interchangeably. Can you comment on C++ API: Training and inference of torchscript modules on multiple GPU · Issue #46460 · pytorch/pytorch · GitHub with your specific use case ? Thanks.

Hi @eellison , could you please help me understand why jit::script::Module is needed at all in libtorch? In other words, why isn’t it a simple thing for libtorch to read a model file saved in Python and build a nn::Module directly from it? I’ve struggled with this issue for years now, and never found a good answer.

For example, I have a libtorch-based application for which I have written code to load HDF5 models from Keras. Since the HDF5 contains both the network graph (as a json string) and the weights, I can programmatically construct the nn::Module from it and populate its trainable parameters. All without going through any format conversion! And, ironically, I cannot do the same with a model saved from Pytorch in python!! Any insights would be appreciated. Thanks.