Skip to content

[CherryPick] forward compability for checkpoint (#71810) #71857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions paddle/fluid/pybind/generator_py.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,24 @@ void BindGenerator(py::module* m_ptr) {
return py::make_tuple(s.device, s.seed, s.offset, bitstream);
},
[](py::tuple s) { // __setstate__
if (s.size() != 4)
if (s.size() != 3 && s.size() != 4)
throw std::runtime_error(
"Invalid Random state. Please check the format(device, "
"current_seed, thread_offset ,rng_state).");
"current_seed, thread_offset [, rng_state]).");
int64_t device = s[0].cast<int64_t>();
int64_t seed = s[1].cast<int64_t>();
uint64_t offset = s[2].cast<uint64_t>();
// NOTE(zhangwl):switch bitstream to mt19937_64;
std::vector<uint8_t> bitstream = s[3].cast<std::vector<uint8_t>>();
std::string str(bitstream.begin(), bitstream.end());
std::stringstream ss(str);
std::mt19937_64 rng;
ss >> rng;
std::shared_ptr<std::mt19937_64> cpu_engine =
std::make_shared<std::mt19937_64>(rng);
std::shared_ptr<std::mt19937_64> cpu_engine = nullptr;
if (s.size() == 4) {
std::vector<uint8_t> bitstream =
s[3].cast<std::vector<uint8_t>>();
std::string str(bitstream.begin(), bitstream.end());
std::stringstream ss(str);
std::mt19937_64 rng;
ss >> rng;
cpu_engine = std::make_shared<std::mt19937_64>(rng);
}
phi::Generator::GeneratorState state(
device, seed, offset, cpu_engine);
return state;
Expand Down
Loading