Make Sequential ref-counted (#9151)

Summary:
In the C++ API, `Sequential` currently was not refcounted itself, but stored `shared_ptr<AnyModule>` to get the reference semantics. This is unfortunate because most modules in the API are accessed via `->`, e.g. `Linear l(1, 2); l->forward(...);`. `Sequential` was different in that it had value semantics itself, thus was accessed via `.`.

This PR makes `Sequential` store `AnyModule` (without extra indirection), and uses the same pImpl mechanism we use for all other modules to make `Sequential` have reference semantics itself. This makes it consistent with the rest of the library. It also removes one level of indirection inside of `Sequential`, which is cool.

One thing I had to change was that the `ModuleHolder` with which the whole pImpl thing is implemented previously did some tricks to make `Linear(3, 4)` actually construct `Linear(LinearOptions(3, 4))`. This doesn't work well with `Sequential` since it takes a variadic parameter pack. Instead, I made `ModuleHolder` forward all arguments to the underlying module, and then further pushed the trick to forward parameters to modules' options types into the actual Modules. This adds one constructor per Module in the library. This is not something user modules have to do (unless they want this nice forwarding themselves). It makes the code simpler overall.

ezyang ebetica apaszke
Pull Request resolved: https://github.com/pytorch/pytorch/pull/9151

Reviewed By: ezyang

Differential Revision: D8809298

Pulled By: goldsborough

fbshipit-source-id: da68452c3de912fbc67af330ba93b5220de6909f
This commit is contained in:
Peter Goldsborough
2018-07-11 17:15:08 -07:00
committed by Facebook Github Bot
parent 94bc4c6091
commit 153e2e96d4
19 changed files with 219 additions and 162 deletions
+67 -38
View File
@@ -24,7 +24,7 @@ TEST_CASE("sequential") {
};
Sequential sequential(
std::make_shared<M>(1), std::make_shared<M>(2), std::make_shared<M>(3));
REQUIRE(sequential.size() == 3);
REQUIRE(sequential->size() == 3);
}
SECTION("construction from concrete type") {
struct M : torch::nn::Module {
@@ -36,7 +36,7 @@ TEST_CASE("sequential") {
};
Sequential sequential(M(1), M(2), M(3));
REQUIRE(sequential.size() == 3);
REQUIRE(sequential->size() == 3);
}
SECTION("construction from module holders") {
struct MImpl : torch::nn::Module {
@@ -53,7 +53,7 @@ TEST_CASE("sequential") {
};
Sequential sequential(M(1), M(2), M(3));
REQUIRE(sequential.size() == 3);
REQUIRE(sequential->size() == 3);
}
SECTION("push_back") {
struct M : torch::nn::Module {
@@ -64,14 +64,14 @@ TEST_CASE("sequential") {
int value;
};
Sequential sequential;
REQUIRE(sequential.size() == 0);
REQUIRE(sequential.is_empty());
sequential.push_back(Linear(3, 4));
REQUIRE(sequential.size() == 1);
sequential.push_back(std::make_shared<M>(1));
REQUIRE(sequential.size() == 2);
sequential.push_back(M(2));
REQUIRE(sequential.size() == 3);
REQUIRE(sequential->size() == 0);
REQUIRE(sequential->is_empty());
sequential->push_back(Linear(3, 4));
REQUIRE(sequential->size() == 1);
sequential->push_back(std::make_shared<M>(1));
REQUIRE(sequential->size() == 2);
sequential->push_back(M(2));
REQUIRE(sequential->size() == 3);
}
SECTION("access") {
struct M : torch::nn::Module {
@@ -86,22 +86,22 @@ TEST_CASE("sequential") {
Sequential sequential;
for (auto& module : modules) {
sequential.push_back(module);
sequential->push_back(module);
}
REQUIRE(sequential.size() == 3);
REQUIRE(sequential->size() == 3);
SECTION("at()") {
SECTION("returns the correct module for a given index") {
for (size_t i = 0; i < modules.size(); ++i) {
REQUIRE(&sequential.at<M>(i) == modules[i].get());
REQUIRE(&sequential->at<M>(i) == modules[i].get());
}
}
SECTION("throws for a bad index") {
REQUIRE_THROWS_WITH(
sequential.at<M>(modules.size() + 1),
sequential->at<M>(modules.size() + 1),
StartsWith("Index out of range"));
REQUIRE_THROWS_WITH(
sequential.at<M>(modules.size() + 1000000),
sequential->at<M>(modules.size() + 1000000),
StartsWith("Index out of range"));
}
}
@@ -109,17 +109,17 @@ TEST_CASE("sequential") {
SECTION("ptr()") {
SECTION("returns the correct module for a given index") {
for (size_t i = 0; i < modules.size(); ++i) {
REQUIRE(sequential.ptr(i).get() == modules[i].get());
REQUIRE(sequential->ptr(i).get() == modules[i].get());
REQUIRE(sequential[i].get() == modules[i].get());
REQUIRE(sequential.ptr<M>(i).get() == modules[i].get());
REQUIRE(sequential->ptr<M>(i).get() == modules[i].get());
}
}
SECTION("throws for a bad index") {
REQUIRE_THROWS_WITH(
sequential.ptr(modules.size() + 1),
sequential->ptr(modules.size() + 1),
StartsWith("Index out of range"));
REQUIRE_THROWS_WITH(
sequential.ptr(modules.size() + 1000000),
sequential->ptr(modules.size() + 1000000),
StartsWith("Index out of range"));
}
}
@@ -128,7 +128,7 @@ TEST_CASE("sequential") {
SECTION("calling forward() on an empty sequential is disallowed") {
Sequential empty;
REQUIRE_THROWS_WITH(
empty.forward<int>(),
empty->forward<int>(),
StartsWith("Cannot call forward() on an empty Sequential"));
}
@@ -144,7 +144,7 @@ TEST_CASE("sequential") {
Sequential sequential(MockModule{1}, MockModule{2}, MockModule{3});
REQUIRE(sequential.forward<int>(1) == 4);
REQUIRE(sequential->forward<int>(1) == 4);
}
SECTION("calling forward() with the wrong return type throws") {
@@ -155,9 +155,9 @@ TEST_CASE("sequential") {
};
Sequential sequential(M{});
REQUIRE(sequential.forward<int>() == 5);
REQUIRE(sequential->forward<int>() == 5);
REQUIRE_THROWS_WITH(
sequential.forward<float>(),
sequential->forward<float>(),
StartsWith("The type of the return value "
"is int, but you asked for type float"));
}
@@ -171,7 +171,7 @@ TEST_CASE("sequential") {
Sequential sequential(M{});
auto variable = torch::ones({3, 3}, torch::requires_grad());
REQUIRE(sequential.forward(variable).equal(variable));
REQUIRE(sequential->forward(variable).equal(variable));
}
}
@@ -180,7 +180,7 @@ TEST_CASE("sequential") {
Sequential sequential(Linear(10, 3), Linear(3, 5), Linear(5, 100));
auto x = torch::randn({1000, 10}, torch::requires_grad());
auto y = sequential.forward(x);
auto y = sequential->forward(x);
REQUIRE(y.ndimension() == 2);
REQUIRE(y.size(0) == 1000);
REQUIRE(y.size(1) == 100);
@@ -205,42 +205,71 @@ TEST_CASE("sequential") {
Sequential sequential(M{});
torch::Tensor variable = torch::ones(5);
REQUIRE(sequential.forward(variable).sum().toCFloat() == 5);
REQUIRE(sequential->forward(variable).sum().toCFloat() == 5);
at::Tensor tensor_that_is_actually_a_variable = variable * 2;
REQUIRE(
sequential.forward(tensor_that_is_actually_a_variable)
sequential->forward(tensor_that_is_actually_a_variable)
.sum()
.toCFloat() == 10);
}
SECTION("extend() pushes modules from other Sequential") {
struct A : torch::nn::Module { int forward(int x) { return x; } };
struct B : torch::nn::Module { int forward(int x) { return x; } };
struct C : torch::nn::Module { int forward(int x) { return x; } };
struct D : torch::nn::Module { int forward(int x) { return x; } };
struct A : torch::nn::Module {
int forward(int x) {
return x;
}
};
struct B : torch::nn::Module {
int forward(int x) {
return x;
}
};
struct C : torch::nn::Module {
int forward(int x) {
return x;
}
};
struct D : torch::nn::Module {
int forward(int x) {
return x;
}
};
Sequential a(A{}, B{});
Sequential b(C{}, D{});
a.extend(b);
a->extend(*b);
REQUIRE(a.size() == 4);
REQUIRE(a->size() == 4);
REQUIRE(a[0]->as<A>());
REQUIRE(a[1]->as<B>());
REQUIRE(a[2]->as<C>());
REQUIRE(a[3]->as<D>());
REQUIRE(b.size() == 2);
REQUIRE(b->size() == 2);
REQUIRE(b[0]->as<C>());
REQUIRE(b[1]->as<D>());
std::vector<std::shared_ptr<A>> c = {std::make_shared<A>(),
std::make_shared<A>()};
b.extend(c);
b->extend(c);
REQUIRE(b.size() == 4);
REQUIRE(b->size() == 4);
REQUIRE(b[0]->as<C>());
REQUIRE(b[1]->as<D>());
REQUIRE(b[2]->as<A>());
REQUIRE(b[3]->as<A>());
}
SECTION("has reference semantics") {
Sequential first(Linear(2, 3), Linear(4, 4), Linear(4, 5));
Sequential second(first);
REQUIRE(first.get() == second.get());
REQUIRE(first->size() == second->size());
REQUIRE(std::equal(
first->begin(),
first->end(),
second->begin(),
[](const AnyModule& first, const AnyModule& second) {
return &first == &second;
}));
}
}