Add name() to C++ modules (#7409)

* Add name() to C++ modules

* Use RTTI to get module name by default

* Add functional.cpp to CMakeLists.txt

* Call typeid() inside name() instead of constructor

* Add tests and use default constructor
This commit is contained in:
Peter Goldsborough
2018-05-10 08:52:38 -07:00
committed by Edward Z. Yang
parent ab5c391100
commit c5de3314cf
19 changed files with 137 additions and 41 deletions
+32
View File
@@ -6,6 +6,26 @@ using namespace torch;
using namespace torch::nn;
using Catch::StartsWith;
struct AGIUnit : CloneableModule<AGIUnit> {
variable_list forward(variable_list) {
return {};
}
};
namespace test {
struct AGIUnit : CloneableModule<AGIUnit> {
variable_list forward(variable_list) {
return {};
}
};
struct AGIUnit2 : CloneableModule<AGIUnit2> {
AGIUnit2() : CloneableModule<AGIUnit2>("Foo") {}
variable_list forward(variable_list) {
return {};
}
};
} // namespace test
TEST_CASE("module/training-mode") {
auto model = make(Linear(3, 4));
REQUIRE(model->is_training());
@@ -37,6 +57,18 @@ TEST_CASE("module/zero-grad") {
}
}
TEST_CASE("module/name") {
AGIUnit agi;
// Call it twice just to make sure there are no bugs in the lazy
// initialization semantics.
REQUIRE(agi.name() == "AGIUnit");
REQUIRE(agi.name() == "AGIUnit");
SECTION("correctly demangled") {
REQUIRE(test::AGIUnit().name() == "test::AGIUnit");
REQUIRE(test::AGIUnit2().name() == "Foo");
}
}
TEST_CASE("module/conversions", "[cuda]") {
auto model = make(LSTM(128, 64).nlayers(3).dropout(0.2));
SECTION("starts as float on CPU") {