forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
function_schema.h
377 lines (342 loc) · 11.2 KB
/
function_schema.h
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
#pragma once
#include <c10/util/StringUtil.h>
#include <ATen/core/jit_type.h>
#include <ATen/core/interned_strings.h>
#include <ATen/core/ivalue.h>
#include <ATen/core/alias_info.h>
#include <ATen/core/operator_name.h>
#include <unordered_map>
namespace c10 {
// schema as used in the compiler for resolving function calls and reporting
// errors. These objects should be constructed from C10 schema once those
// are available.
struct Argument;
struct FunctionSchema;
namespace detail {
inline bool defaultValueEquals_(
const c10::optional<IValue>& lhs,
const c10::optional<IValue>& rhs) {
if (lhs.has_value()) {
return rhs.has_value() && impl::shallowEquals(*lhs, *rhs);
} else {
return !rhs.has_value();
}
}
} // namespace detail
bool operator==(const Argument& lhs, const Argument& rhs);
struct Argument {
Argument(
std::string name = "",
TypePtr type = nullptr,
c10::optional<int32_t> N = c10::nullopt,
c10::optional<IValue> default_value = c10::nullopt,
bool kwarg_only = false,
c10::optional<AliasInfo> alias_info = c10::nullopt,
bool is_inferred_type = false)
: name_(std::move(name)),
type_(type ? type : TensorType::get()),
N_(std::move(N)),
default_value_(std::move(default_value)),
kwarg_only_(kwarg_only),
alias_info_(std::move(alias_info)),
is_inferred_type_(is_inferred_type) {
}
const std::string& name() const {
return name_;
}
TypePtr type() const {
return type_;
}
c10::optional<int32_t> N() const {
return N_;
}
const c10::optional<IValue>& default_value() const {
return default_value_;
}
bool kwarg_only() const {
return kwarg_only_;
}
const c10::optional<AliasInfo>& alias_info() const {
return alias_info_;
}
bool is_inferred_type() const {
return is_inferred_type_;
}
std::string formatTypeMismatchMsg(const std::string& actual_type) const {
std::string inferred_type_hint;
if (is_inferred_type()) {
inferred_type_hint = c10::str(
"Inferred '",
name(),
"' to be of type 'Tensor' ",
"because it was not annotated with an explicit type.\n");
}
return c10::str(
"Expected a value of type '",
type()->python_str(),
"' for argument '",
name(),
"' but instead found type '",
actual_type,
"'.\n",
inferred_type_hint);
}
Argument cloneWithType(TypePtr new_type) const {
return Argument(name_, new_type, N_, default_value_, kwarg_only_, alias_info_);
}
// this function check whether this Argument is backward compatible with
// the old one. we consider the following cases are backward compatible:
// 1) two arguments are equal
// 2) this arg's type should be subtype of old
// 3) this arg must provide the same default value if old arg has one,
bool isBackwardCompatibleWith(
const Argument& old,
std::ostream* why_not=nullptr) const;
private:
std::string name_;
TypePtr type_;
// for list types, an optional statically known length for the list
// e.g. for int[3]: type = ListType::ofInts(), N = 3
// If present, this will allow scalars to be broadcast to this length to
// become a list.
c10::optional<int32_t> N_;
c10::optional<IValue> default_value_;
// is this only specifyable as a keyword argument?
bool kwarg_only_;
c10::optional<AliasInfo> alias_info_;
bool is_inferred_type_;
};
inline bool operator==(const Argument& lhs, const Argument& rhs) {
return lhs.name() == rhs.name()
&& *lhs.type() == *rhs.type()
&& lhs.N() == rhs.N()
&& detail::defaultValueEquals_(lhs.default_value(), rhs.default_value())
&& lhs.kwarg_only() == rhs.kwarg_only()
&& lhs.alias_info() == rhs.alias_info();
}
bool operator==(const FunctionSchema& lhs, const FunctionSchema& rhs);
struct FunctionSchema {
FunctionSchema(
std::string name,
std::string overload_name,
std::vector<Argument> arguments,
std::vector<Argument> returns,
bool is_vararg = false,
bool is_varret = false)
: name_({std::move(name), std::move(overload_name)}),
arguments_(std::move(arguments)),
returns_(std::move(returns)),
is_vararg_(is_vararg),
is_varret_(is_varret) {
checkSchema();
}
FunctionSchema(
Symbol name,
std::string overload_name,
std::vector<Argument> arguments,
std::vector<Argument> returns,
bool is_vararg = false,
bool is_varret = false)
: FunctionSchema(
name.toQualString(),
std::move(overload_name),
std::move(arguments),
std::move(returns),
is_vararg,
is_varret) {
checkSchema();
}
// check whether this schema is backward compatible with the old one.
// the following conditions are considered as this schema is backward
// compatible with old:
// 1) two schemas are equal
// 2) this schema has the same or more positional args than old,
// and any positional arg in this schema is backward compatible
// with the corresponding one in old schema, which could be an arg
// or a kwarg, if it has, or it must provide a default value
// 3) this schema has the same or more kwargs than old, and all the kwargs
// in old schema can find the corresponding kwarg in this schema which
// is backward compatible with the old kwarg, and the extra kwargs in
// this schema must provide default values.
bool isBackwardCompatibleWith(
const FunctionSchema& old,
std::ostream* why_not = nullptr) const;
private:
OperatorName name_;
std::vector<Argument> arguments_;
std::vector<Argument> returns_;
// if true then this schema takes an arbitrary number of additional arguments
// after the argument specified in arguments
// currently this is used primarily to represent 'primitive' operators whose
// arguments are not checked by schema
bool is_vararg_;
bool is_varret_;
void checkArg(const IValue& value, const Argument& argument, optional<size_t> pos) const;
void checkSchema() const {
bool seen_default_arg = false;
for (const auto& arg : arguments()) {
if (arg.default_value()) {
seen_default_arg = true;
} else {
// we have historically serialized broadcasting lists wo/default values,
// so to not break BC allow lists here
if (arg.type()->kind() == ListType::Kind) {
continue;
}
TORCH_INTERNAL_ASSERT(
!seen_default_arg || arg.kwarg_only(),
"Non-default positional argument follows default argument. Parameter ",
arg.name(),
" in ",
*this);
}
}
}
public:
void dump() const;
const OperatorName& operator_name() const {
return name_;
}
const std::string& name() const {
return name_.name;
}
const std::string& overload_name() const {
return name_.overload_name;
}
const std::vector<Argument>& arguments() const {
return arguments_;
}
const std::vector<Argument>& returns() const {
return returns_;
}
bool is_vararg() const {
return is_vararg_;
}
bool is_varret() const {
return is_varret_;
}
bool is_mutable() const {
return std::any_of(
arguments_.cbegin(), arguments_.cend(), [](const Argument& arg) {
const auto& aliasInfo = arg.alias_info();
return aliasInfo && aliasInfo.value().isWrite();
});
}
c10::optional<int> argumentIndexWithName(const std::string& name) const {
for(size_t i = 0; i < arguments().size(); ++i) {
if(name == arguments()[i].name())
return i;
}
return c10::nullopt;
}
FunctionSchema cloneWithArguments(std::vector<Argument> new_arguments) const {
return FunctionSchema(
name(),
overload_name(),
std::move(new_arguments),
returns(),
is_vararg(),
is_varret());
}
FunctionSchema cloneWithReturns(std::vector<Argument> new_returns) const {
return FunctionSchema(
name(),
overload_name(),
arguments(),
std::move(new_returns),
is_vararg(),
is_varret());
}
std::string formatTypeMismatchMsg(
const Argument& expected,
const std::string& actual_type,
c10::optional<size_t> position = c10::nullopt,
c10::optional<std::string> value = c10::nullopt) const;
FunctionSchema cloneWithRemappedTypes(
const std::function<TypePtr(TypePtr)> type_map) const;
// Check that inputs have the correct types and appends any missing default
// values.
void checkAndNormalizeInputs(
std::vector<IValue>& inputs,
const std::unordered_map<std::string, IValue>& kwargs) const;
void findErrorInKwargs(const std::vector<std::string>& kwargs) const;
bool hasAnyAliasInfo() const {
for (const auto& arg : arguments_) {
if (arg.alias_info().has_value()) {
return true;
}
}
for (const auto& ret : returns_) {
if (ret.alias_info().has_value()) {
return true;
}
}
return false;
}
// can a function with this schema be substituted for a function of rhs's
// schema and have the program typecheck?
// as_method - if true, treat this schema as a method and ignore
// the first argument, which will be the object in both cases
bool isSubtypeOf(const FunctionSchema& rhs, bool as_method, std::ostream* why_not=nullptr) const;
};
inline bool operator==(const FunctionSchema& lhs, const FunctionSchema& rhs) {
return lhs.name() == rhs.name()
&& lhs.overload_name() == rhs.overload_name()
&& lhs.arguments() == rhs.arguments()
&& lhs.returns() == rhs.returns()
&& lhs.is_vararg() == rhs.is_vararg()
&& lhs.is_varret() == rhs.is_varret();
}
inline bool operator!=(const FunctionSchema& lhs, const FunctionSchema& rhs) {
return !(lhs == rhs);
}
// print out Argument, which is compatible with FunctionSchema parser
// full format: Type(alias)? name=default_value
inline std::ostream& operator<<(std::ostream& out, const Argument& arg) {
bool optional_type = arg.type()->kind() == OptionalType::Kind;
// for adjusting the ? position.
// in schema, we have Tensor?(a!) input, and t(a!)?.
// however, t?(a!) doesn't work with schema parser.
// so we always use Type(alias)? format
std::stringstream oss;
if (auto list = arg.type()->cast<c10::ListType>()) {
oss << list->getElementType()->str();
oss << "[";
if (arg.N()) {
oss << *arg.N();
}
oss << "]";
} else {
oss << arg.type()->str();
}
if (optional_type) {
oss.seekp(oss.str().size() - 1);
}
if (arg.alias_info()) {
oss << arg.alias_info().value();
}
if (optional_type) {
oss << "?";
}
out << oss.str();
if (!arg.name().empty()) {
out << " " << arg.name();
}
if (arg.default_value()) {
out << "=";
if (arg.type()->kind() == c10::TypeKind::StringType) {
printQuotedString(out, arg.default_value().value().toStringRef());
} else {
out << arg.default_value().value();
}
}
return out;
}
inline std::ostream& operator<<(std::ostream& out, const FunctionSchema& schema);
inline std::string toString(const FunctionSchema& schema) {
std::ostringstream str;
str << schema;
return str.str();
}
} // namespace c10
#include <ATen/core/function_schema_inl.h>