Include iteration_ in SGD optimizer serialization (#26906)

Summary:
This PR fixes https://github.com/pytorch/pytorch/issues/24192 by including the private field `iteration_` in SGD optimizer serialization. Under the hood, `iteration_` is serialized into an `IValue`, then stored in a JIT module as an attribute.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/26906

Differential Revision: D17628359

Pulled By: yf225

fbshipit-source-id: beec1367459e973a1c9080dc86f502e4c7bc5ebd
This commit is contained in:
Will Feng
2019-09-27 09:35:29 -07:00
committed by Facebook Github Bot
parent 0a393f6ef5
commit 3acbcb96d4
9 changed files with 95 additions and 1 deletions
+37
View File
@@ -207,6 +207,27 @@ TEST(SerializeTest, Optim) {
}
}
TEST(SerializeTest, SerializationShouldPreserveIteration_SGD) {
std::vector<torch::Tensor> parameters = {
torch::randn({2, 2}), torch::randn({3, 3})};
torch::optim::SGD optimizer(parameters, 1.0);
optimizer.step();
optimizer.step();
ASSERT_EQ(optimizer.iteration(), 2);
auto tempfile = c10::make_tempfile();
torch::save(optimizer, tempfile.name);
torch::optim::SGD optimizer_out(parameters, 1.0);
ASSERT_EQ(optimizer_out.iteration(), 0);
torch::load(optimizer_out, tempfile.name);
ASSERT_EQ(optimizer_out.iteration(), 2);
}
TEST(SerializeTest, XOR_CUDA) {
torch::manual_seed(0);
// We better be able to save and load a XOR model!
@@ -318,6 +339,22 @@ TEST(SerializeTest, VectorOfTensors) {
}
}
TEST(SerializeTest, IValue) {
c10::IValue ivalue(1);
auto tempfile = c10::make_tempfile();
torch::serialize::OutputArchive output_archive;
output_archive.write("value", ivalue);
output_archive.save_to(tempfile.name);
torch::serialize::InputArchive input_archive;
input_archive.load_from(tempfile.name);
c10::IValue ivalue_out;
input_archive.read("value", ivalue_out);
ASSERT_EQ(ivalue_out.toInt(), 1);
ASSERT_THROWS_WITH(input_archive.read("bad_key", ivalue_out), "No such serialized IValue");
}
// NOTE: if a `Module` contains unserializable submodules (e.g. `nn::Functional`),
// we expect those submodules to be skipped when the `Module` is being serialized.
TEST(SerializeTest, UnserializableSubmoduleIsSkippedWhenSavingModule) {