forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
source_range_serialization.cpp
152 lines (128 loc) · 4.72 KB
/
source_range_serialization.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <torch/csrc/jit/source_range_serialization.h>
#include <torch/csrc/jit/source_range_serialization_impl.h>
#include <ATen/core/ivalue.h>
#include <torch/csrc/jit/pickle.h>
namespace torch {
namespace jit {
class SourceRangeSerializer {
public:
// Serialize SourceRange as Tuple[SourceType, int, int]
// where SourceType = Tuple[str, Optional[str], int, List[int]],
// the serialized form of Source
c10::IValue serialize(const SourceRange& sr);
private:
// Serialize Source as Tuple[str, Optional[str], int, List[int]]
// This caches serialized sources, since many SourceRanges can
// refer to the same one.
c10::IValue serialize_source(const std::shared_ptr<Source>& s);
std::unordered_map<std::shared_ptr<Source>, c10::IValue> serialized_sources;
};
class SourceRangeDeserializer {
public:
SourceRange deserialize(const c10::IValue& iv) {
auto tup_elems = iv.toTuple()->elements();
TORCH_INTERNAL_ASSERT(tup_elems.size() == 3);
std::shared_ptr<Source> source_ = deserialize_source(tup_elems[0]);
int64_t start_ = tup_elems[1].toInt();
int64_t end_ = tup_elems[2].toInt();
return SourceRange(source_, start_, end_);
}
private:
std::shared_ptr<Source> deserialize_source(const c10::IValue& iv) {
auto tup = iv.toTuple();
if (cached_sources.count(tup)) {
return cached_sources.at(tup);
}
auto tup_elems = tup->elements();
TORCH_INTERNAL_ASSERT(tup_elems.size() == 3);
std::string text_ = tup_elems[0].toString()->string();
c10::optional<std::string> filename_ =
tup_elems[1].toOptional<std::string>();
int64_t starting_line_no_ = tup_elems[2].toInt();
auto source = std::make_shared<Source>(
std::move(text_), std::move(filename_), starting_line_no_);
cached_sources[tup] = source;
return source;
}
std::unordered_map<
c10::intrusive_ptr<c10::ivalue::Tuple>,
std::shared_ptr<Source>>
cached_sources;
};
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));
}
c10::IValue SourceRangeSerializer::serialize_source(
const std::shared_ptr<Source>& s) {
if (serialized_sources.count(s)) {
return serialized_sources.at(s);
}
std::vector<c10::IValue> elements;
if (s == nullptr) {
elements = {"", "", 0};
} else {
elements = {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;
}
SourceRangePickler::SourceRangePickler()
: srs(new SourceRangeSerializer()) {}
std::vector<char> SourceRangePickler::pickle(const SourceRangeRecords& ranges) {
std::vector<c10::IValue> ivalues;
for (const auto& range : ranges) {
std::vector<c10::IValue> row_elems{(int64_t)range.bytes,
srs->serialize(range.range)};
ivalues.emplace_back(c10::ivalue::Tuple::create(std::move(row_elems)));
}
std::vector<at::Tensor> table;
auto ivalue = c10::ivalue::Tuple::create(std::move(ivalues));
auto result = jit::pickle(ivalue, &table);
TORCH_CHECK(table.size() == 0, "Expected 0 tensors to be written");
return result;
}
ConcreteSourceRangeUnpickler::ConcreteSourceRangeUnpickler(
at::DataPtr&& data,
size_t size)
: data(std::move(data)),
size(size),
deserializer(new SourceRangeDeserializer()),
unpickled_records(nullptr) {}
void ConcreteSourceRangeUnpickler::unpickle() {
if (unpickled_records) {
return;
}
auto ivalues = jit::unpickle(reinterpret_cast<const char*>(data.get()), size)
.toTuple()
->elements();
unpickled_records = std::make_shared<SourceRangeRecords>();
for (auto& val : ivalues) {
auto tup_elems = val.toTuple()->elements();
int64_t offset = tup_elems[0].toInt();
auto source_range = deserializer->deserialize(tup_elems[1]);
unpickled_records->emplace_back(offset, std::move(source_range));
}
}
c10::optional<SourceRange> ConcreteSourceRangeUnpickler::
findSourceRangeThatGenerated(const SourceRange& range) {
unpickle();
auto query = TaggedRange(range.start(), SourceRange{});
auto entry = std::upper_bound(
unpickled_records->begin(),
unpickled_records->end(),
query,
[](const TaggedRange& a, const TaggedRange& b) -> bool {
return a.bytes < b.bytes;
});
// NB: must decrement iterator since upper_bound finds the element
// *greater than* the query.
if (entry != unpickled_records->begin()) {
return (entry - 1)->range;
}
return c10::nullopt;
}
} // namespace jit
} // namespace torch