forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
function_schema_parser.cpp
432 lines (408 loc) · 13.6 KB
/
function_schema_parser.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include <torch/csrc/jit/frontend/function_schema_parser.h>
#include <ATen/core/Reduction.h>
#include <ATen/core/jit_type.h>
#include <ATen/core/type_factory.h>
#include <torch/csrc/jit/frontend/lexer.h>
#include <torch/csrc/jit/frontend/parse_string_literal.h>
#include <torch/csrc/jit/frontend/schema_type_parser.h>
#include <optional>
#include <memory>
#include <vector>
using at::TypeKind;
using c10::Argument;
using c10::FunctionSchema;
using c10::IValue;
using c10::ListType;
using c10::OperatorName;
namespace torch::jit {
namespace {
struct SchemaParser {
explicit SchemaParser(const std::string& str, bool allow_typevars)
: L(std::make_shared<Source>(
std::string_view(str),
std::nullopt,
0,
nullptr,
Source::DONT_COPY)),
type_parser(L, /*parse_complete_tensor_types*/ false, allow_typevars) {}
std::variant<OperatorName, FunctionSchema> parseDeclaration() {
OperatorName name = parseName();
// If there is no parentheses coming, then this is just the operator name
// without an argument list
if (L.cur().kind != '(') {
return OperatorName(std::move(name));
}
std::vector<Argument> arguments;
std::vector<Argument> returns;
bool kwarg_only = false;
bool is_vararg = false;
bool is_varret = false;
size_t idx = 0;
parseList('(', ',', ')', [&] {
if (is_vararg)
throw(
ErrorReport(L.cur())
<< "... must be the last element of the argument list");
if (L.nextIf('*')) {
kwarg_only = true;
} else if (L.nextIf(TK_DOTS)) {
is_vararg = true;
} else {
arguments.push_back(parseArgument(
idx++, /*is_return=*/false, /*kwarg_only=*/kwarg_only));
}
});
// check if all arguments are not-default for vararg schemas
if (is_vararg) {
for (const auto& arg : arguments) {
if (arg.default_value().has_value()) {
throw(
ErrorReport(L.cur())
<< "schemas with vararg (...) can't have default value args");
}
}
}
idx = 0;
L.expect(TK_ARROW);
if (L.nextIf(TK_DOTS)) {
is_varret = true;
} else if (L.cur().kind == '(') {
parseList('(', ',', ')', [&] {
if (is_varret) {
throw(
ErrorReport(L.cur())
<< "... must be the last element of the return list");
}
if (L.nextIf(TK_DOTS)) {
is_varret = true;
} else {
returns.push_back(
parseArgument(idx++, /*is_return=*/true, /*kwarg_only=*/false));
}
});
} else {
returns.push_back(
parseArgument(0, /*is_return=*/true, /*kwarg_only=*/false));
}
return FunctionSchema(
std::move(name.name),
std::move(name.overload_name),
std::move(arguments),
std::move(returns),
is_vararg,
is_varret);
}
c10::OperatorName parseName() {
std::string name = L.expect(TK_IDENT).text();
if (L.nextIf(':')) {
L.expect(':');
name = name + "::" + L.expect(TK_IDENT).text();
}
std::string overload_name = "";
if (L.nextIf('.')) {
overload_name = L.expect(TK_IDENT).text();
}
// default is used as an attribute on the `OpOverloadPacket`
// (obtained using `torch.ops.aten.foo`) to get the operator
// overload with overload name as an empty string
// and so shouldn't be used as an overload name
// also disallow dunder attribute names to be overload names
bool is_a_valid_overload_name =
!((overload_name == "default") || (overload_name.rfind("__", 0) == 0));
TORCH_CHECK(
is_a_valid_overload_name,
overload_name,
" is not a legal overload name for aten operators");
return {name, overload_name};
}
std::vector<std::variant<OperatorName, FunctionSchema>> parseDeclarations() {
std::vector<std::variant<OperatorName, FunctionSchema>> results;
do {
results.emplace_back(parseDeclaration());
} while (L.nextIf(TK_NEWLINE));
L.expect(TK_EOF);
return results;
}
std::variant<OperatorName, FunctionSchema> parseExactlyOneDeclaration() {
auto result = parseDeclaration();
L.nextIf(TK_NEWLINE);
L.expect(TK_EOF);
return result;
}
Argument parseArgument(size_t /*idx*/, bool is_return, bool kwarg_only) {
// fake and real type coincide except for Layout/MemoryFormat/ScalarType
// the fake type for these is Int instead
auto p = type_parser.parseFakeAndRealType();
auto fake_type = std::move(std::get<0>(p));
auto real_type = std::move(std::get<1>(p));
auto alias_info = std::move(std::get<2>(p));
std::optional<int32_t> N;
std::optional<IValue> default_value;
std::optional<std::string> alias_set;
std::string name;
if (L.nextIf('[')) {
// note: an array with a size hint can only occur at the Argument level
fake_type = ListType::create(std::move(fake_type));
real_type = ListType::create(std::move(real_type));
N = std::stoll(L.expect(TK_NUMBER).text());
L.expect(']');
auto container = type_parser.parseAliasAnnotation();
if (alias_info) {
if (!container) {
container = std::optional<at::AliasInfo>(at::AliasInfo());
container->setIsWrite(alias_info->isWrite());
}
container->addContainedType(std::move(*alias_info));
}
alias_info = std::move(container);
if (L.nextIf('?')) {
fake_type =
c10::TypeFactory::create<c10::OptionalType>(std::move(fake_type));
real_type =
c10::TypeFactory::create<c10::OptionalType>(std::move(real_type));
}
}
if (is_return) {
// optionally field names in return values
if (L.cur().kind == TK_IDENT) {
name = L.next().text();
} else {
name = "";
}
} else {
name = L.expect(TK_IDENT).text();
if (L.nextIf('=')) {
// NB: this means we have to unswizzle default too
default_value =
parseDefaultValue(*fake_type, fake_type->kind(), *real_type, N);
}
}
return Argument(
std::move(name),
std::move(fake_type),
std::move(real_type),
N,
std::move(default_value),
!is_return && kwarg_only,
std::move(alias_info));
}
bool isPossiblyOptionalScalarType(const c10::Type& type) {
if (type.kind() == at::ScalarTypeType::Kind) {
return true;
}
if (type.kind() == at::OptionalType::Kind) {
for (const auto& inner : type.containedTypes()) {
if (isPossiblyOptionalScalarType(*inner))
return true;
}
}
return false;
}
IValue parseSingleConstant(
const c10::Type& type,
TypeKind kind,
const c10::Type& real_type) {
if (kind == c10::TypeKind::DynamicType) {
return parseSingleConstant(
type, type.expectRef<c10::DynamicType>().dynamicKind(), real_type);
}
const auto& str2dtype = c10::getStringToDtypeMap();
switch (L.cur().kind) {
case TK_TRUE:
L.next();
return true;
case TK_FALSE:
L.next();
return false;
case TK_NONE:
L.next();
return IValue();
case TK_STRINGLITERAL: {
auto token = L.next();
return parseStringLiteral(token.range, token.text());
}
case TK_IDENT: {
auto tok = L.next();
auto text = tok.text();
// NB: float/complex/long are here for BC purposes. Other dtypes
// are handled via str2dtype.
// Please don't add more cases to this if-else block.
if ("float" == text) {
return static_cast<int64_t>(at::kFloat);
} else if ("complex" == text) {
return static_cast<int64_t>(at::kComplexFloat);
} else if ("long" == text) {
return static_cast<int64_t>(at::kLong);
} else if ("strided" == text) {
return static_cast<int64_t>(at::kStrided);
} else if ("Mean" == text) {
return static_cast<int64_t>(at::Reduction::Mean);
} else if ("contiguous_format" == text) {
return static_cast<int64_t>(c10::MemoryFormat::Contiguous);
} else if (
isPossiblyOptionalScalarType(real_type) &&
str2dtype.count(text) > 0) {
return static_cast<int64_t>(str2dtype.at(text));
} else {
throw(ErrorReport(L.cur().range) << "invalid numeric default value");
}
}
default:
std::string n;
if (L.nextIf('-'))
n = "-" + L.expect(TK_NUMBER).text();
else
n = L.expect(TK_NUMBER).text();
if (kind == TypeKind::ComplexType || n.find('j') != std::string::npos) {
auto imag = std::stod(n.substr(0, n.size() - 1));
return c10::complex<double>(0, imag);
} else if (
kind == TypeKind::FloatType || n.find('.') != std::string::npos ||
n.find('e') != std::string::npos) {
return std::stod(n);
} else {
int64_t v = std::stoll(n);
return v;
}
}
}
IValue convertToList(
const c10::Type& type,
TypeKind kind,
const SourceRange& range,
const std::vector<IValue>& vs) {
switch (kind) {
case TypeKind::ComplexType:
return fmap(vs, [](const IValue& v) { return v.toComplexDouble(); });
case TypeKind::FloatType:
return fmap(vs, [](const IValue& v) { return v.toDouble(); });
case TypeKind::IntType:
return fmap(vs, [](const IValue& v) { return v.toInt(); });
case TypeKind::BoolType:
return fmap(vs, [](const IValue& v) { return v.toBool(); });
case TypeKind::DynamicType:
return convertToList(
type, type.expectRef<c10::DynamicType>().dynamicKind(), range, vs);
default:
throw(
ErrorReport(range)
<< "lists are only supported for float, int and complex types");
}
}
IValue parseConstantList(
const c10::Type& type,
TypeKind kind,
const c10::Type& real_type) {
auto tok = L.expect('[');
std::vector<IValue> vs;
if (L.cur().kind != ']') {
do {
vs.push_back(parseSingleConstant(type, kind, real_type));
} while (L.nextIf(','));
}
L.expect(']');
return convertToList(type, kind, tok.range, vs);
}
IValue parseTensorDefault(const SourceRange& /*range*/) {
L.expect(TK_NONE);
return IValue();
}
IValue parseDefaultValue(
const c10::Type& arg_type,
TypeKind kind,
const c10::Type& real_type,
std::optional<int32_t> arg_N) {
auto range = L.cur().range;
switch (kind) {
case TypeKind::TensorType:
case TypeKind::GeneratorType:
case TypeKind::QuantizerType: {
return parseTensorDefault(range);
} break;
case TypeKind::StringType:
case TypeKind::OptionalType:
case TypeKind::NumberType:
case TypeKind::IntType:
case TypeKind::BoolType:
case TypeKind::FloatType:
case TypeKind::ComplexType:
return parseSingleConstant(arg_type, kind, real_type);
break;
case TypeKind::DeviceObjType: {
auto device_text =
parseStringLiteral(range, L.expect(TK_STRINGLITERAL).text());
return c10::Device(device_text);
break;
}
case TypeKind::ListType: {
auto elem_type = arg_type.containedType(0);
auto real_elem_type = real_type.containedType(0);
if (L.cur().kind == TK_IDENT) {
return parseTensorDefault(range);
} else if (arg_N && L.cur().kind != '[') {
IValue v = parseSingleConstant(
*elem_type, elem_type->kind(), *real_elem_type);
std::vector<IValue> repeated(*arg_N, v);
return convertToList(*elem_type, elem_type->kind(), range, repeated);
} else {
return parseConstantList(
*elem_type, elem_type->kind(), *real_elem_type);
}
} break;
case TypeKind::DynamicType:
return parseDefaultValue(
arg_type,
arg_type.expectRef<c10::DynamicType>().dynamicKind(),
real_type,
arg_N);
default:
throw(ErrorReport(range) << "unexpected type, file a bug report");
}
return IValue(); // silence warnings
}
void parseList(
int begin,
int sep,
int end,
c10::function_ref<void()> callback) {
auto r = L.cur().range;
if (begin != TK_NOTHING)
L.expect(begin);
if (L.cur().kind != end) {
do {
callback();
} while (L.nextIf(sep));
}
if (end != TK_NOTHING)
L.expect(end);
}
Lexer L;
SchemaTypeParser type_parser;
};
} // namespace
std::variant<OperatorName, FunctionSchema> parseSchemaOrName(
const std::string& schemaOrName,
bool allow_typevars) {
// We're ignoring aten and prim for BC reasons
if (schemaOrName.rfind("aten::", 0) == 0 ||
schemaOrName.rfind("prim::", 0) == 0) {
allow_typevars = true;
}
return SchemaParser(schemaOrName, allow_typevars)
.parseExactlyOneDeclaration();
}
FunctionSchema parseSchema(const std::string& schema, bool allow_typevars) {
auto parsed = parseSchemaOrName(schema, allow_typevars);
TORCH_CHECK(
std::holds_alternative<FunctionSchema>(parsed),
"Tried to parse a function schema but only the operator name was given");
return std::get<FunctionSchema>(std::move(parsed));
}
OperatorName parseName(const std::string& name) {
auto parsed = parseSchemaOrName(name);
TORCH_CHECK(
std::holds_alternative<OperatorName>(parsed),
"Tried to parse an operator name but function schema was given");
return std::get<OperatorName>(std::move(parsed));
}
} // namespace torch::jit