[C++ API] Make pImpl easy to use in modules to enable happy reference semantics (#8347)

* Created TORCH_MODULE macro

Rewrote Linear

Rewrote Dropout and added default constructor to TORCH_MODULE macro

Turned TORCH_MODULE contens into a proper base class

Added some documentation

Got rid of the old Dropout module

Got rid of the old Embedding module

Got rid of the old BatchNorm module

Got rid of the old Conv module

Fixing optimizers

Rebase

Removed old RNN modules and the TORCH_ATTR macro

Removed temporary P:: namespace

Added cloning behavior to all modules

Got rid of some get() calls

self review nits

Remove noexcept from ModuleHolder methods that can throw

Remove spaces

Add missing override to reset() methods

Added examples to documentation in pimpl.h

* Post rebase fixes
This commit is contained in:
Peter Goldsborough
2018-06-18 19:45:53 -07:00
committed by GitHub
parent d3651585b8
commit 271406f276
33 changed files with 1013 additions and 526 deletions
+63 -12
View File
@@ -13,23 +13,75 @@ using namespace torch::nn;
using Catch::StartsWith;
TEST_CASE("sequential") {
SECTION("construction") {
SECTION("construction from shared pointer") {
struct M : nn::Module {
explicit M(int value_) : value(value_) {}
int value;
int forward() {
return value;
}
};
Sequential sequential(
Linear(2, 3).build(), Linear(2, 3), Linear(2, 3).build());
std::make_shared<M>(1), std::make_shared<M>(2), std::make_shared<M>(3));
REQUIRE(sequential.size() == 3);
}
SECTION("construction from concrete type") {
struct M : nn::Module {
explicit M(int value_) : value(value_) {}
int value;
int forward() {
return value;
}
};
Sequential sequential(M(1), M(2), M(3));
REQUIRE(sequential.size() == 3);
}
SECTION("construction from module holders") {
struct MImpl : nn::Module {
explicit MImpl(int value_) : value(value_) {}
int forward() {
return value;
}
int value;
};
struct M : torch::nn::ModuleHolder<MImpl> {
using torch::nn::ModuleHolder<MImpl>::ModuleHolder;
using torch::nn::ModuleHolder<MImpl>::get;
};
Sequential sequential(M(1), M(2), M(3));
REQUIRE(sequential.size() == 3);
}
SECTION("push_back") {
struct M : nn::Module {
explicit M(int value_) : value(value_) {}
int forward() {
return value;
}
int value;
};
Sequential sequential;
REQUIRE(sequential.size() == 0);
REQUIRE(sequential.is_empty());
sequential.push_back(Linear(3, 4).build());
sequential.push_back(Linear(3, 4));
REQUIRE(sequential.size() == 1);
sequential.push_back(Linear(4, 5).build());
sequential.push_back(std::make_shared<M>(1));
REQUIRE(sequential.size() == 2);
sequential.push_back(M(2));
REQUIRE(sequential.size() == 3);
}
SECTION("access") {
std::vector<std::shared_ptr<Linear>> modules = {
Linear(2, 3).build(), Linear(3, 4).build(), Linear(4, 5).build()};
struct M : nn::Module {
explicit M(int value_) : value(value_) {}
int forward() {
return value;
}
int value;
};
std::vector<std::shared_ptr<M>> modules = {
std::make_shared<M>(1), std::make_shared<M>(2), std::make_shared<M>(3)};
Sequential sequential;
for (auto& module : modules) {
@@ -40,15 +92,15 @@ TEST_CASE("sequential") {
SECTION("at()") {
SECTION("returns the correct module for a given index") {
for (size_t i = 0; i < modules.size(); ++i) {
REQUIRE(&sequential.at<Linear>(i) == modules[i].get());
REQUIRE(&sequential.at<M>(i) == modules[i].get());
}
}
SECTION("throws for a bad index") {
REQUIRE_THROWS_WITH(
sequential.at<Linear>(modules.size() + 1),
sequential.at<M>(modules.size() + 1),
StartsWith("Index out of range"));
REQUIRE_THROWS_WITH(
sequential.at<Linear>(modules.size() + 1000000),
sequential.at<M>(modules.size() + 1000000),
StartsWith("Index out of range"));
}
}
@@ -58,7 +110,7 @@ TEST_CASE("sequential") {
for (size_t i = 0; i < modules.size(); ++i) {
REQUIRE(sequential.ptr(i).get() == modules[i].get());
REQUIRE(sequential[i].get() == modules[i].get());
REQUIRE(sequential.ptr<Linear>(i).get() == modules[i].get());
REQUIRE(sequential.ptr<M>(i).get() == modules[i].get());
}
}
SECTION("throws for a bad index") {
@@ -123,8 +175,7 @@ TEST_CASE("sequential") {
}
SECTION("returns the last value") {
Sequential sequential(
Linear(10, 3).build(), Linear(3, 5).build(), Linear(5, 100).build());
Sequential sequential(Linear(10, 3), Linear(3, 5), Linear(5, 100));
auto x = torch::randn({1000, 10}, at::requires_grad());
auto y = sequential.forward<std::vector<Variable>>(std::vector<Variable>{x})