forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeephole_dict_idioms.cpp
272 lines (230 loc) · 7 KB
/
peephole_dict_idioms.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include <torch/csrc/jit/ir/alias_analysis.h>
#include <torch/csrc/jit/passes/peephole_dict_idioms.h>
namespace torch {
namespace jit {
namespace {
class DictNodeImplBase {
public:
virtual ~DictNodeImplBase() = default;
virtual bool contains(const IValue&) const = 0;
virtual size_t size() const = 0;
virtual Value* get(const IValue&) const = 0;
bool canOptimize() {
return !has_overlap_ && !has_non_const_key_;
}
protected:
bool has_overlap_ = false;
bool has_non_const_key_ = false;
};
template <class KeyType>
class DictNodeImpl : public DictNodeImplBase {
public:
DictNodeImpl(
std::function<KeyType(const IValue&)> ivalue_converter,
Node* dict_creation_node)
: ivalue_converter_(std::move(ivalue_converter)) {
for (size_t i = 0; i < dict_creation_node->inputs().size(); i += 2) {
auto key_opt = toIValue(dict_creation_node->input(i));
// Key is not constant if we cannot convert to IValue
if (key_opt == c10::nullopt) {
has_non_const_key_ = true;
continue;
}
KeyType key = ivalue_converter_(*key_opt);
if (dict_.find(key) == dict_.end()) {
dict_.emplace(key, dict_creation_node->input(i + 1));
} else {
has_overlap_ = true;
}
}
}
bool contains(const IValue& ivalue) const override {
auto key = ivalue_converter_(ivalue);
return dict_.find(key) != dict_.end();
}
size_t size() const override {
return dict_.size();
}
Value* get(const IValue& ivalue) const override {
auto val = ivalue_converter_(ivalue);
auto loc = dict_.find(val);
if (loc != dict_.end()) {
return loc->second;
}
TORCH_CHECK(false, "Cannot get non-existent key");
}
private:
std::unordered_map<KeyType, Value*> dict_;
std::function<KeyType(const IValue&)> ivalue_converter_;
};
class DictNode {
public:
explicit DictNode(Node* dict_creation_node) {
auto dict_type = dict_creation_node->output()->type();
auto key_value_types = dict_type->containedTypes();
TORCH_CHECK(
key_value_types.size() == 2, "Dict must have 2 contained types");
const auto& key_type = key_value_types[0];
switch (key_type->kind()) {
case TypeKind::IntType: {
auto ivalue_converter = [](const IValue& ival) { return ival.toInt(); };
impl_ = std::make_unique<DictNodeImpl<int64_t>>(
std::move(ivalue_converter), dict_creation_node);
break;
}
case TypeKind::FloatType: {
auto ivalue_converter = [](const IValue& ival) {
return ival.toDouble();
};
impl_ = std::make_unique<DictNodeImpl<double>>(
std::move(ivalue_converter), dict_creation_node);
break;
}
case TypeKind::StringType: {
auto ivalue_converter = [](const IValue& ival) {
return *ival.toString();
};
impl_ = std::make_unique<DictNodeImpl<std::string>>(
std::move(ivalue_converter), dict_creation_node);
break;
}
default:
impl_ = nullptr;
}
}
bool canOptimize() const {
if (impl_) {
return impl_->canOptimize();
}
return false;
}
size_t size() const {
if (impl_) {
return impl_->size();
}
return 0;
}
c10::optional<Value*> getOrNullopt(const IValue& key) const {
if (impl_ && impl_->contains(key)) {
return impl_->get(key);
}
return c10::nullopt;
}
private:
std::unique_ptr<DictNodeImplBase> impl_;
};
bool isDict(Value* v) {
return v->type()->castRaw<DictType>() != nullptr;
}
class PeepholeOptimizeDictIdiomsImpl {
public:
explicit PeepholeOptimizeDictIdiomsImpl(std::shared_ptr<Graph> graph)
: graph_(std::move(graph)), aliasDb_(std::make_unique<AliasDb>(graph_)) {}
bool run() {
collectMutatedDicts(graph_->block());
return runBlock(graph_->block());
}
private:
void checkForMutatedDicts(Value* v) {
if (isDict(v) && aliasDb_->hasWriters(v)) {
mutated_dicts_.insert(v);
}
}
void collectMutatedDicts(Block* b) {
for (Value* v : b->inputs()) {
checkForMutatedDicts(v);
}
for (Node* n : b->nodes()) {
for (Value* v : n->outputs()) {
checkForMutatedDicts(v);
}
for (Block* block : n->blocks()) {
collectMutatedDicts(block);
}
}
}
const DictNode& getDictNode(Node* creation_node) {
auto cached = dict_cache_.find(creation_node);
if (cached == dict_cache_.end()) {
cached =
dict_cache_.emplace(creation_node, DictNode(creation_node)).first;
}
return cached->second;
}
c10::optional<Value*> getValueFromDict(Node* dict_creation_node, Value* key) {
const DictNode& dict_node = getDictNode(dict_creation_node);
auto key_opt = toIValue(key);
// Key is not constant if we cannot convert to IValue
if (key_opt == c10::nullopt) {
return c10::nullopt;
}
IValue key_ival = *key_opt;
if (dict_node.canOptimize()) {
return dict_node.getOrNullopt(key_ival);
}
return c10::nullopt;
}
c10::optional<int64_t> computeLen(Node* dict_creation_node) {
const DictNode& dict_node = getDictNode(dict_creation_node);
if (dict_node.canOptimize()) {
return static_cast<int64_t>(dict_node.size());
}
return c10::nullopt;
}
bool optimizeLen(Node* len_node, Node* creation_node) {
if (creation_node->kind() == prim::DictConstruct) {
auto len = computeLen(creation_node);
if (len != c10::nullopt) {
WithInsertPoint guard(len_node);
len_node->output()->replaceAllUsesWith(graph_->insertConstant(len));
return true;
}
}
return false;
}
bool optimizeGetItem(Node* getitem_node, Node* creation_node) {
if (creation_node->kind() == prim::DictConstruct) {
auto key = getitem_node->input(1);
auto value = getValueFromDict(creation_node, key);
if (value != c10::nullopt) {
getitem_node->output()->replaceAllUsesWith(*value);
return true;
}
}
return false;
}
bool runBlock(Block* block) {
bool changed = false;
for (Node* node : block->nodes()) {
for (Block* b : node->blocks()) {
changed |= runBlock(b);
}
// only optimizing dict ops
if (node->inputs().size() == 0 || !isDict(node->input(0))) {
continue;
}
auto first_input = node->input(0);
// only optimizing ops with unmutated inputs
if (mutated_dicts_.count(first_input)) {
continue;
}
if (node->kind() == aten::len) {
changed |= optimizeLen(node, first_input->node());
} else if (node->kind() == aten::__getitem__) {
changed |= optimizeGetItem(node, first_input->node());
}
}
return changed;
}
std::shared_ptr<Graph> graph_;
std::unordered_set<Value*> mutated_dicts_;
std::unique_ptr<AliasDb> aliasDb_;
std::unordered_map<Node*, DictNode> dict_cache_;
};
} // namespace
bool PeepholeOptimizeDictIdioms(const std::shared_ptr<Graph>& graph) {
PeepholeOptimizeDictIdiomsImpl opt(graph);
return opt.run();
}
} // namespace jit
} // namespace torch