mirror of
https://github.com/pytorch/pytorch.git
synced 2026-11-20 09:04:25 +08:00
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:
committed by
Edward Z. Yang
parent
ab5c391100
commit
c5de3314cf
@@ -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") {
|
||||
|
||||
Reference in New Issue
Block a user