Eliminate c10::guts::to_string (#108480)

This PR replace c10::guts::to_string with std::to_string. The major part of changes is using void* as optimizer state key since string is used only for serialization and using pointers as hashing keys is more efficient than a string.
Some other guts functions in the affected source files are also replaced.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/108480
Approved by: https://github.com/Skylion007
This commit is contained in:
cyy
2023-09-04 08:12:50 +00:00
committed by PyTorch MergeBot
parent 06b173780d
commit 4146be192e
44 changed files with 164 additions and 225 deletions

View File

@ -63,7 +63,7 @@ void SGD::add_param_group(const SGDParamGroup& param_group) {
}
for (const auto& p : param_group_.params()) {
TORCH_CHECK(
state_.count(c10::guts::to_string(p.unsafeGetTensorImpl())) == 0,
state_.count(p.unsafeGetTensorImpl()) == 0,
"some parameters appear in more than one parameter group");
}
param_groups_.emplace_back(std::move(param_group_));
@ -104,14 +104,12 @@ Tensor SGD::step(const LossClosure& closure) {
}
if (momentum != 0) {
Tensor buf;
auto param_state =
state_.find(c10::guts::to_string(p.unsafeGetTensorImpl()));
auto param_state = state_.find(p.unsafeGetTensorImpl());
if (param_state == state_.end()) {
buf = torch::clone(d_p).detach();
auto state = std::make_unique<SGDParamState>();
state->momentum_buffer(buf);
state_[c10::guts::to_string(p.unsafeGetTensorImpl())] =
std::move(state);
state_[p.unsafeGetTensorImpl()] = std::move(state);
} else {
buf = static_cast<SGDParamState&>(*param_state->second)
.momentum_buffer();