Template Class Cloneable

Inheritance Relationships

Base Type

Derived Types

Class Documentation

template<typename Derived>
class Cloneable : public virtual torch::nn::Module

The clone() method in the base Module class does not have knowledge of the concrete runtime type of its subclasses.

Therefore, clone() must either be called from within the subclass, or from a base class that has knowledge of the concrete type. Cloneable uses the CRTP to gain knowledge of the subclass’ static type and provide an implementation of the clone() method. We do not want to use this pattern in the base class, because then storing a module would always require templatizing it.

Subclassed by torch::nn::NormImplBase< D, Derived, BatchNormOptions >, torch::nn::NormImplBase< D, Derived, InstanceNormOptions >, torch::nn::AdaptiveAvgPoolImpl< D, output_size_t, Derived >, torch::nn::AdaptiveMaxPoolImpl< D, output_size_t, Derived >, torch::nn::AvgPoolImpl< D, Derived >, torch::nn::ConstantPadImpl< D, Derived >, torch::nn::ConvNdImpl< D, Derived >, torch::nn::detail::_DropoutNd< Derived >, torch::nn::detail::RNNCellImplBase< Derived >, torch::nn::detail::RNNImplBase< Derived >, torch::nn::LPPoolImpl< D, Derived >, torch::nn::MaxPoolImpl< D, Derived >, torch::nn::MaxUnpoolImpl< D, Derived >, torch::nn::NormImplBase< D, Derived, DerivedOptions >, torch::nn::ReflectionPadImpl< D, Derived >, torch::nn::ReplicationPadImpl< D, Derived >, torch::nn::ZeroPadImpl< D, Derived >

Public Functions

virtual void reset() = 0

reset() must perform initialization of all members with reference semantics, most importantly parameters, buffers and submodules.

inline virtual std::shared_ptr<Module> clone(const optional<Device> &device = nullopt) const override

Performs a recursive “deep copy” of the Module, such that all parameters and submodules in the cloned module are different from those in the original module.

explicit Module(std::string name)

Tells the base Module about the name of the submodule.


Constructs the module without immediate knowledge of the submodule’s name.

The name of the submodule is inferred via RTTI (if possible) the first time .name() is invoked.


Access comprehensive developer documentation for PyTorch

View Docs


Get in-depth tutorials for beginners and advanced developers

View Tutorials


Find development resources and get your questions answered

View Resources