mirror of
https://github.com/pytorch/pytorch.git
synced 2026-11-20 09:04:25 +08:00
Add named submodule support to nn::Sequential (#17552)
Summary:
Previously, we were not able to assign names to `nn::Sequential`'s submodules. This PR adds this feature to match the Python API. Example use:
```cpp
Sequential sequential(named_submodule({
{"linear", Linear(10, 3)},
{"conv2d", Conv2d(1, 2, 3)},
{"dropout", Dropout(0.5)},
{"batchnorm", BatchNorm(5)},
{"embedding", Embedding(4, 10)},
{"lstm", LSTM(4, 5)}
}));
```
It also enables loading parameters of Python `nn.Sequential` module with custom submodules names into C++ frontend, unblocking https://github.com/pytorch/vision/pull/728#issuecomment-466661344.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/17552
Differential Revision: D14246834
Pulled By: yf225
fbshipit-source-id: 3030b5c5d68f6dd5d3e37ac4b4f98dc6d6d9ba72
This commit is contained in:
committed by
Facebook Github Bot
parent
e73be58ff7
commit
6ebfbdf4c6
@@ -5,6 +5,7 @@
|
||||
#include <torch/nn/modules/conv.h>
|
||||
#include <torch/nn/modules/dropout.h>
|
||||
#include <torch/nn/modules/linear.h>
|
||||
#include <torch/nn/modules/named_any.h>
|
||||
#include <torch/nn/modules/rnn.h>
|
||||
#include <torch/nn/modules/sequential.h>
|
||||
#include <torch/types.h>
|
||||
@@ -32,20 +33,47 @@ TEST_F(SequentialTest, ConstructsFromSharedPointer) {
|
||||
Sequential sequential(
|
||||
std::make_shared<M>(1), std::make_shared<M>(2), std::make_shared<M>(3));
|
||||
ASSERT_EQ(sequential->size(), 3);
|
||||
|
||||
Sequential sequential_named(modules_ordered_dict({
|
||||
{"m1", std::make_shared<M>(1)},
|
||||
{std::string("m2"), std::make_shared<M>(2)},
|
||||
{"m3", std::make_shared<M>(3)}
|
||||
}));
|
||||
ASSERT_EQ(sequential->size(), 3);
|
||||
}
|
||||
|
||||
TEST_F(SequentialTest, ConstructsFromConcreteType) {
|
||||
static int copy_count;
|
||||
|
||||
struct M : torch::nn::Module {
|
||||
explicit M(int value_) : value(value_) {}
|
||||
M(const M& other) : torch::nn::Module(other) {
|
||||
copy_count++;
|
||||
}
|
||||
int value;
|
||||
int forward() {
|
||||
return value;
|
||||
}
|
||||
};
|
||||
|
||||
copy_count = 0;
|
||||
Sequential sequential(M(1), M(2), M(3));
|
||||
ASSERT_EQ(sequential->size(), 3);
|
||||
// NOTE: The current implementation expects each module to be copied exactly once,
|
||||
// which happens when the module is passed into `std::make_shared<T>()`.
|
||||
// TODO: Find a way to avoid copying, and then delete the copy constructor of `M`.
|
||||
ASSERT_EQ(copy_count, 3);
|
||||
|
||||
copy_count = 0;
|
||||
Sequential sequential_named(modules_ordered_dict({
|
||||
{"m1", M(1)},
|
||||
{std::string("m2"), M(2)},
|
||||
{"m3", M(3)}
|
||||
}));
|
||||
ASSERT_EQ(sequential->size(), 3);
|
||||
ASSERT_EQ(copy_count, 3);
|
||||
}
|
||||
|
||||
TEST_F(SequentialTest, ConstructsFromModuleHolder) {
|
||||
struct MImpl : torch::nn::Module {
|
||||
explicit MImpl(int value_) : value(value_) {}
|
||||
@@ -62,6 +90,13 @@ TEST_F(SequentialTest, ConstructsFromModuleHolder) {
|
||||
|
||||
Sequential sequential(M(1), M(2), M(3));
|
||||
ASSERT_EQ(sequential->size(), 3);
|
||||
|
||||
Sequential sequential_named(modules_ordered_dict({
|
||||
{"m1", M(1)},
|
||||
{std::string("m2"), M(2)},
|
||||
{"m3", M(3)}
|
||||
}));
|
||||
ASSERT_EQ(sequential->size(), 3);
|
||||
}
|
||||
|
||||
TEST_F(SequentialTest, PushBackAddsAnElement) {
|
||||
@@ -72,6 +107,8 @@ TEST_F(SequentialTest, PushBackAddsAnElement) {
|
||||
}
|
||||
int value;
|
||||
};
|
||||
|
||||
// Test unnamed submodules
|
||||
Sequential sequential;
|
||||
ASSERT_EQ(sequential->size(), 0);
|
||||
ASSERT_TRUE(sequential->is_empty());
|
||||
@@ -81,6 +118,32 @@ TEST_F(SequentialTest, PushBackAddsAnElement) {
|
||||
ASSERT_EQ(sequential->size(), 2);
|
||||
sequential->push_back(M(2));
|
||||
ASSERT_EQ(sequential->size(), 3);
|
||||
|
||||
// Mix named and unnamed submodules
|
||||
Sequential sequential_named;
|
||||
ASSERT_EQ(sequential_named->size(), 0);
|
||||
ASSERT_TRUE(sequential_named->is_empty());
|
||||
|
||||
sequential_named->push_back(Linear(3, 4));
|
||||
ASSERT_EQ(sequential_named->size(), 1);
|
||||
ASSERT_EQ(sequential_named->named_children()[0].key(), "0");
|
||||
sequential_named->push_back(std::string("linear2"), Linear(3, 4));
|
||||
ASSERT_EQ(sequential_named->size(), 2);
|
||||
ASSERT_EQ(sequential_named->named_children()[1].key(), "linear2");
|
||||
|
||||
sequential_named->push_back("shared_m1", std::make_shared<M>(1));
|
||||
ASSERT_EQ(sequential_named->size(), 3);
|
||||
ASSERT_EQ(sequential_named->named_children()[2].key(), "shared_m1");
|
||||
sequential_named->push_back(std::make_shared<M>(1));
|
||||
ASSERT_EQ(sequential_named->size(), 4);
|
||||
ASSERT_EQ(sequential_named->named_children()[3].key(), "3");
|
||||
|
||||
sequential_named->push_back(M(1));
|
||||
ASSERT_EQ(sequential_named->size(), 5);
|
||||
ASSERT_EQ(sequential_named->named_children()[4].key(), "4");
|
||||
sequential_named->push_back(std::string("m2"), M(1));
|
||||
ASSERT_EQ(sequential_named->size(), 6);
|
||||
ASSERT_EQ(sequential_named->named_children()[5].key(), "m2");
|
||||
}
|
||||
|
||||
TEST_F(SequentialTest, AccessWithAt) {
|
||||
@@ -342,4 +405,23 @@ TEST_F(SequentialTest, PrettyPrintSequential) {
|
||||
" (4): torch::nn::Embedding(count=4, dimension=10)\n"
|
||||
" (5): torch::nn::LSTM(input_size=4, hidden_size=5, layers=1, dropout=0)\n"
|
||||
")");
|
||||
|
||||
Sequential sequential_named(modules_ordered_dict({
|
||||
{"linear", Linear(10, 3)},
|
||||
{"conv2d", Conv2d(1, 2, 3)},
|
||||
{"dropout", Dropout(0.5)},
|
||||
{"batchnorm", BatchNorm(5)},
|
||||
{"embedding", Embedding(4, 10)},
|
||||
{"lstm", LSTM(4, 5)}
|
||||
}));
|
||||
ASSERT_EQ(
|
||||
c10::str(sequential_named),
|
||||
"torch::nn::Sequential(\n"
|
||||
" (linear): torch::nn::Linear(in=10, out=3, with_bias=true)\n"
|
||||
" (conv2d): torch::nn::Conv2d(input_channels=1, output_channels=2, kernel_size=[3, 3], stride=[1, 1])\n"
|
||||
" (dropout): torch::nn::Dropout(rate=0.5)\n"
|
||||
" (batchnorm): torch::nn::BatchNorm(features=5, eps=1e-05, momentum=0.1, affine=true, stateful=true)\n"
|
||||
" (embedding): torch::nn::Embedding(count=4, dimension=10)\n"
|
||||
" (lstm): torch::nn::LSTM(input_size=4, hidden_size=5, layers=1, dropout=0)\n"
|
||||
")");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user