[PyTorch] Adopt faster Tuple::create (#65381)

Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/65381

The previous diff adds a way to make Tuples of size 3 or less
more efficiently. This diff makes it easier to hit that path and
updates a bunch of callsites to hit it.
ghstack-source-id: 142065832

Test Plan: CI

Reviewed By: ezyang

Differential Revision: D31069538

fbshipit-source-id: d04da3709594ed68ab1c0a1471f8cffd8d001628
This commit is contained in:
Scott Wolchok
2021-11-02 10:08:49 -07:00
committed by Facebook GitHub Bot
parent 9e71ea292d
commit 7cd62621fb
10 changed files with 161 additions and 62 deletions

View File

@ -53,9 +53,8 @@ std::shared_ptr<SourceView> SourceRangeDeserializer::deserialize_source(
}
c10::IValue SourceRangeSerializer::serialize(const SourceRange& sr) {
std::vector<c10::IValue> elements = {
serialize_source(sr.source()), (int64_t)sr.start(), (int64_t)sr.end()};
return c10::ivalue::Tuple::create(std::move(elements));
return c10::ivalue::Tuple::create(
serialize_source(sr.source()), (int64_t)sr.start(), (int64_t)sr.end());
}
c10::IValue SourceRangeSerializer::serialize_source(
@ -63,13 +62,13 @@ c10::IValue SourceRangeSerializer::serialize_source(
if (serialized_sources.count(s)) {
return serialized_sources.at(s);
}
std::vector<c10::IValue> elements;
c10::intrusive_ptr<c10::ivalue::Tuple> serialized;
if (s == nullptr) {
elements = {"", "", 0};
serialized = c10::ivalue::Tuple::create({"", "", 0});
} else {
elements = {s->text(), s->filename(), (int64_t)s->starting_line_no()};
serialized = c10::ivalue::Tuple::create(
{s->text(), s->filename(), (int64_t)s->starting_line_no()});
}
auto serialized = c10::ivalue::Tuple::create(std::move(elements));
serialized_sources[s] = serialized;
return serialized;
}
@ -86,11 +85,10 @@ std::vector<char> SourceRangePickler::pickle(
if (it != source_range_tags.end()) {
source_range_tag = it->second;
}
std::vector<c10::IValue> row_elems{
(int64_t)range.bytes,
srs->serialize(range.range),
static_cast<int64_t>(source_range_tag)};
ivalues.emplace_back(c10::ivalue::Tuple::create(std::move(row_elems)));
ivalues.emplace_back(c10::ivalue::Tuple::create(
{(int64_t)range.bytes,
srs->serialize(range.range),
static_cast<int64_t>(source_range_tag)}));
}
std::vector<at::Tensor> table;
auto ivalue = c10::ivalue::Tuple::create(std::move(ivalues));