Skip to content

Commit

Permalink
C++ make constructor NamedAnyModule(name,any) public (pytorch#36869)
Browse files Browse the repository at this point in the history
Summary:
Allows creation of _NamedAnyModule_ directly from _AnyModule_, e.g.

```
  auto a=torch::nn::AnyModule(torch::nn::Linear(1,2));
  auto m=torch::nn::NamedAnyModule("fc", a);
```
Without the public constructor, it would be necessary to recast the AnyModule to underlying type,
then have the constructor cast it back to AnyModule.

With the public AnyModule constructor,
possible to do
```
auto q=Sequential({m});
```
or
```
q->push_back(m.name, m.module());
```

(works in conjunction with PR pytorch#36720 which allowed adding _AnyModule_ directly)
Pull Request resolved: pytorch#36869

Differential Revision: D21110074

Pulled By: yf225

fbshipit-source-id: aaea02282b9024824785e54d8732c0a12c850977
  • Loading branch information
meganset authored and facebook-github-bot committed Apr 19, 2020
1 parent 6ba734b commit 8b685a8
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions torch/csrc/api/include/torch/nn/modules/container/named_any.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ class NamedAnyModule {
NamedAnyModule(std::string name, const ModuleHolder<M>& module_holder)
: NamedAnyModule(std::move(name), module_holder.ptr()) {}

/// Creates a `NamedAnyModule` from a type-erased `AnyModule`.
NamedAnyModule(std::string name, AnyModule any_module)
: name_(std::move(name)), module_(std::move(any_module)) {}

/// Returns a reference to the name.
const std::string& name() const noexcept {
return name_;
Expand All @@ -83,10 +87,6 @@ class NamedAnyModule {
}

private:
/// Creates a `NamedAnyModule` from a type-erased `AnyModule`.
NamedAnyModule(std::string name, AnyModule any_module)
: name_(std::move(name)), module_(std::move(any_module)) {}

std::string name_;
AnyModule module_;
};
Expand Down

0 comments on commit 8b685a8

Please sign in to comment.