Fix Module::zero_grad (#8964)

Summary:
`nn::Module::zero_grad` did not respect undefined `grad()` variables. This is fixed (the code now replicates PyTorch).

ebetica ezyang apaszke
Closes https://github.com/pytorch/pytorch/pull/8964

Reviewed By: ezyang

Differential Revision: D8677529

Pulled By: goldsborough

fbshipit-source-id: afdc4ba00dbf5012c37d1f794c731937ee5e422e
This commit is contained in:
Peter Goldsborough
2018-06-28 10:20:58 -07:00
committed by Facebook Github Bot
parent 63233f98ad
commit ccc14071f4
2 changed files with 29 additions and 1 deletions
+24
View File
@@ -56,6 +56,30 @@ TEST_CASE("module/zero-grad") {
}
}
TEST_CASE("module/zero-grad-with-undefined") {
struct TestModule : torch::nn::Module {
TestModule() {
x = register_parameter("x", torch::ones(5, at::requires_grad()));
y = register_parameter("y", torch::ones(5, at::requires_grad()));
}
torch::Tensor x, y;
};
TestModule module;
auto z = module.x * 2;
z.sum().backward();
REQUIRE(module.x.grad().defined());
REQUIRE(!module.y.grad().defined());
module.zero_grad();
REQUIRE(module.x.grad().defined());
REQUIRE(!module.y.grad().defined());
REQUIRE(module.x.grad().sum().toCFloat() == 0);
}
TEST_CASE("module/name") {
// CHECK instead of REQUIRE because demangling may fail.
AGIUnit agi;