Shortcuts

Template Class ModuleHolder

Inheritance Relationships

Base Type

Class Documentation

template<typename Contained>
class torch::nn::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

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.

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>
ModuleHolder(Head &&head, Tail&&... tail)

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

ModuleHolder(std::shared_ptr<Contained> module)

Constructs the ModuleHolder from a pointer to the contained type.

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

operator bool() const noexcept

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

Contained *operator->()

Forwards to the contained module.

const Contained *operator->() const

Forwards to the contained module.

Contained &operator*()

Returns a reference to the contained module.

const Contained &operator*() const

Returns a const reference to the contained module.

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

Returns a shared pointer to the underlying module.

Contained *get()

Returns a pointer to the underlying module.

const Contained *get() const

Returns a const pointer to the underlying module.

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

Calls the forward() method of the contained module.

template<typename Arg>
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

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