mirror of
https://github.com/pytorch/pytorch.git
synced 2026-11-20 09:04:25 +08:00
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:
committed by
Facebook Github Bot
parent
63233f98ad
commit
ccc14071f4
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user