Libtorch Polymorphism

Libtorch uses CRTP to make cloneable modules. For example, torch::nn::Softmax is a ModuleHolder<SoftmaxImpl> and SoftmaxImpl is a torch::nn::Cloneable<SoftmaxImpl>.

Say I want a std::vector<Module> or a function that returns a activation function that’s also a Module. I have to cast various modules such as torch::nn::Softmax to torch::nn:Module. This means I loose Cloneable which I’m not sure is a problem. If I return a reference by simply dereferencing ModuleHolder, since it’s a shared pointer, I’m not sure if the underlying module is guaranteed to survive.

Any advice on how to get around Cloneable and ModuleHolder to work with generic torch::nn::Module (so I can have a function that returns it or a std::vector of it) is much appreciated!

Solved: use AnyModule.