Shortcuts

Template Class ModuleHolder

Inheritance Relationships

Base Type

  • private torch::detail::ModuleHolderIndicator

Class Documentation

template<typename Contained>
class ModuleHolder : private torch::detail::ModuleHolderIndicator

A ModuleHolder is essentially a wrapper around std::shared_ptr<M> where M is an nn::Module subclass, with convenient constructors defined for the kind of constructions we want to allow for our modules.

Public Types

using ContainedType = Contained

Public Functions

inline ModuleHolder()

Default constructs the contained module if if has a default constructor, else produces a static error.

NOTE: This uses the behavior of template classes in C++ that constructors (or any methods) are only compiled when actually used.

inline ModuleHolder(std::nullptr_t)

Constructs the ModuleHolder with an empty contained value.

Access to the underlying module is not permitted and will throw an exception, until a value is assigned.

template<typename Head, typename ...Tail, typename = typename std::enable_if<!(torch::detail::is_module_holder_of<Head, ContainedType>::value && (sizeof...(Tail) == 0))>::type>
inline explicit ModuleHolder(Head &&head, Tail&&... tail)

Constructs the ModuleHolder with a contained module, forwarding all arguments to its constructor.

inline ModuleHolder(std::shared_ptr<Contained> module)

Constructs the ModuleHolder from a pointer to the contained type.

Example: Linear(std::make_shared<LinearImpl>(...)).

inline explicit operator bool() const noexcept

Returns true if the ModuleHolder contains a module, or false if it is nullptr.

inline Contained *operator->()

Forwards to the contained module.

inline const Contained *operator->() const

Forwards to the contained module.

inline Contained &operator*()

Returns a reference to the contained module.

inline const Contained &operator*() const

Returns a const reference to the contained module.

inline const std::shared_ptr<Contained> &ptr() const

Returns a shared pointer to the underlying module.

inline Contained *get()

Returns a pointer to the underlying module.

inline const Contained *get() const

Returns a const pointer to the underlying module.

template<typename ...Args>
inline auto operator()(Args&&... args) -> torch::detail::return_type_of_forward_t<Contained, Args...>

Calls the forward() method of the contained module.

template<typename Arg>
inline decltype(auto) operator[](Arg &&arg)

Forwards to the subscript operator of the contained module.

NOTE: std::forward is qualified to prevent VS2017 emitting error C2872: ‘std’: ambiguous symbol

inline bool is_empty() const noexcept

Returns true if the ModuleHolder does not contain a module.

Protected Attributes

std::shared_ptr<Contained> impl_

The module pointer this class wraps.

NOTE: Must be placed at the top of the class so that we can use it with trailing return types below.

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources