forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
operator.cpp
317 lines (284 loc) · 9.61 KB
/
operator.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include <ATen/ATen.h>
#include <torch/csrc/jit/alias_info.h>
#include <torch/csrc/jit/operator.h>
#include <torch/csrc/jit/passes/alias_analysis.h>
#include <torch/csrc/jit/passes/python_print.h>
#include <torch/csrc/jit/script/edit_distance.h>
#include <torch/csrc/jit/script/error_report.h>
#include <queue>
#include <utility>
#include <vector>
namespace torch {
namespace jit {
namespace {
using OperatorMap =
std::unordered_map<Symbol, std::vector<std::shared_ptr<Operator>>>;
struct OperatorRegistry {
private:
std::mutex lock;
OperatorMap operators;
// list of operators whose schema have not yet been parsed, and must
// be registered before any call to lookup an operator
std::vector<std::shared_ptr<Operator>> to_register;
// Those two maps are used to implement lookupByLiteral, which is needed for
// the n->match(...) calls. Basically, every function schema is assigned a
// unique string you can use to match it. However, parsing those strings or
// comparing and hashing them character by character would be very slow, so we
// use a trick here! Every string literal in your program is guaranteed to
// have static storage duration and so its address won't change at runtime.
// This allows us to memoize answers for every pointer, which is done by the
// operators_by_sig_literal map. Still, this map is initially empty, and so we
// still need to do the complete string matching at the first time, which is
// implemented by performing a lookup in the operators_by_sig map.
std::unordered_map<std::string, std::shared_ptr<Operator>> operators_by_sig;
std::unordered_map<const char*, std::shared_ptr<Operator>>
operators_by_sig_literal;
// XXX - caller must be holding lock
void registerPendingOperators() {
for (const auto& op : to_register) {
Symbol sym = Symbol::fromQualString(op->schema().name());
operators[sym].push_back(op);
operators_by_sig[canonicalSchemaString(op->schema())] = op;
}
to_register.clear();
}
public:
void registerOperator(Operator&& op) {
std::lock_guard<std::mutex> guard(lock);
to_register.push_back(std::make_shared<Operator>(std::move(op)));
}
const std::shared_ptr<Operator>& lookupByLiteral(const char* name) {
std::lock_guard<std::mutex> guard(lock);
registerPendingOperators();
auto it = operators_by_sig_literal.find(name);
if (it == operators_by_sig_literal.end()) {
auto op_ptr_it =
operators_by_sig.find(canonicalSchemaString(parseSchema(name)));
// Handy debugging code that dumps all operators we know about on mismatch
#if 0
if (op_ptr_it == operators_by_sig.end()) {
for (auto & entry : operators_by_sig) {
std::cout << entry.first << std::endl;
}
}
#endif
TORCH_CHECK(
op_ptr_it != operators_by_sig.end(),
"Couldn't find an operator for ",
name,
". Do you have to update a set of hardcoded JIT ops?");
it = operators_by_sig_literal.emplace_hint(it, name, op_ptr_it->second);
}
return it->second;
}
const std::vector<std::shared_ptr<Operator>>& getOperators(Symbol name) {
std::lock_guard<std::mutex> guard(lock);
registerPendingOperators();
static std::vector<std::shared_ptr<Operator>> empty;
auto it = operators.find(name);
if (it != operators.end())
return it->second;
return empty;
}
std::vector<Symbol> findSimilarOperators(Symbol input_op) {
std::lock_guard<std::mutex> guard(lock);
registerPendingOperators();
using EntryPair = std::pair<int64_t, Symbol>;
auto cmp = [](const EntryPair& lhs, const EntryPair& rhs) {
return lhs.first > rhs.first;
};
std::priority_queue<EntryPair, std::vector<EntryPair>, decltype(cmp)>
rankings(cmp);
static constexpr size_t MAX_EDIT_DIST = 2u;
for (const auto& op : operators) {
auto edit_dist = script::ComputeEditDistance(
input_op.toQualString(), op.first.toQualString(), MAX_EDIT_DIST);
if (edit_dist <= MAX_EDIT_DIST) {
rankings.emplace(edit_dist, op.first);
}
}
std::vector<Symbol> ret;
while (!rankings.empty()) {
ret.push_back(rankings.top().second);
rankings.pop();
}
return ret;
}
const std::vector<std::shared_ptr<Operator>> getAllOperators() {
std::lock_guard<std::mutex> guard(lock);
registerPendingOperators();
std::vector<std::shared_ptr<Operator>> values;
values.clear();
for (auto & kv : operators) {
values.insert(values.end(), kv.second.begin(), kv.second.end());
}
return values;
}
};
OperatorRegistry& getRegistry() {
static OperatorRegistry r;
return r;
}
} // anonymous namespace
void registerOperator(Operator&& op) {
if (op.schema().is_varret()) {
Symbol s = Symbol::fromQualString(op.schema().name());
if (!printerHasSpecialCaseFor(s)) {
AT_ERROR(
"Missing special case in python printer for non-schematized"
" operator ",
op.schema().name(),
". File a bug to add a case for this operator.\n");
}
if (!aliasAnalysisHasSpecialCaseFor(s) &&
op.aliasAnalysisKind() == AliasAnalysisKind::CONSERVATIVE) {
AT_ERROR(
"Missing special case in alias analysis for non-schematized"
" operator ",
op.schema().name(),
". File a bug to add a case for this operator.\n");
}
if (aliasAnalysisHasSpecialCaseFor(s) &&
op.aliasAnalysisKind() == AliasAnalysisKind::FROM_SCHEMA) {
AT_ERROR(
"The operator ",
op.schema().name(),
" is special cased and cannot use explicit alias analysis.");
}
}
getRegistry().registerOperator(std::move(op));
}
const std::vector<std::shared_ptr<Operator>> getAllOperators() {
return getRegistry().getAllOperators();
}
const std::vector<std::shared_ptr<Operator>>& getAllOperatorsFor(Symbol name) {
return getRegistry().getOperators(name);
}
std::vector<Symbol> findSimilarOperators(Symbol input_op) {
return getRegistry().findSimilarOperators(input_op);
}
Operator& sig(const char* signature) {
return *getRegistry().lookupByLiteral(signature);
}
std::string canonicalSchemaString(const FunctionSchema& schema) {
std::ostringstream out;
out << schema.name();
out << "(";
bool seen_kwarg_only = false;
for (size_t i = 0; i < schema.arguments().size(); ++i) {
if (i > 0)
out << ", ";
if (schema.arguments()[i].kwarg_only() && !seen_kwarg_only) {
out << "*, ";
seen_kwarg_only = true;
}
const auto& arg = schema.arguments()[i];
out << arg.type()->str() << " " << arg.name();
}
out << ") -> ";
if (schema.returns().size() == 1) {
out << schema.returns().at(0).type()->str();
} else if (schema.returns().size() > 1) {
out << "(";
for (size_t i = 0; i < schema.returns().size(); ++i) {
if (i > 0)
out << ", ";
out << schema.returns()[i].type()->str();
}
out << ")";
}
return out.str();
}
bool Operator::matches(const Node* node) const {
// wrong name
if (node->kind().toQualString() != schema().name()) {
return false;
}
at::ArrayRef<const Value*> actuals = node->inputs();
const auto& formals = schema().arguments();
// not enough inputs
if (actuals.size() < formals.size()) {
return false;
}
TypeEnv type_env;
for (size_t i = 0; i < formals.size(); ++i) {
auto formal = formals[i].type();
const MatchTypeReturn matched_type = matchTypeVariables(
formal, actuals[i]->type(), type_env);
if (!matched_type.success()) {
return false;
}
TypePtr resolved = tryEvalTypeVariables(formal, type_env);
if (resolved) {
formal = resolved;
}
// note: it is possible at this point that type variable matching has
// not resolved all type variables, e.g. if None was matched to Optional[T]
// we will not succeed at matching T. However None <: Optional[T] so this
// check can still succeed.
if (!actuals[i]->type()->isSubtypeOf(formal)) {
return false;
}
}
// too many inputs
if (!schema().is_vararg() && actuals.size() != formals.size()) {
return false;
}
return true;
}
std::shared_ptr<Operator> findOperatorFor(const Node* node) {
const auto& candidates = getAllOperatorsFor(node->kind());
for (const auto& candidate : candidates) {
if (candidate->matches(node)) {
return candidate;
}
}
return nullptr;
}
const Operator& getOperatorFor(const Node* node) {
auto op = findOperatorFor(node);
if (op)
return *op;
auto er = script::ErrorReport(node->sourceRange());
er << "Schema not found for node. File a bug report.\n";
er << "Node: " << *node << "\n";
er << "Input types:";
for (size_t i = 0; i < node->inputs().size(); ++i) {
if (i > 0)
er << ", ";
er << *node->inputs()[i]->type();
}
const auto& candidates = getAllOperatorsFor(node->kind());
if (candidates.size() > 0) {
er << "\ncandidates were:\n";
for (auto& candidate : candidates) {
er << " " << candidate->schema() << "\n";
}
} else {
er << "\nno candidates found\n";
}
er << "within the graph:\n";
er << *node->owningGraph() << "\n";
throw er;
}
OperatorSet::OperatorSet(std::initializer_list<const char*> sig_literals) {
auto& registry = getRegistry();
for (const char* sig : sig_literals) {
auto op = registry.lookupByLiteral(sig);
ops[Symbol::fromQualString(op->schema().name())].push_back(op);
}
}
Operator* OperatorSet::find(const Node* n) const {
auto it = ops.find(n->kind());
if (it == ops.end()) {
return nullptr;
}
for (auto& op : it->second) {
if (op->matches(n)) {
return op.get();
}
}
return nullptr;
}
} // namespace jit
} // namespace torch