forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_schema.h
179 lines (162 loc) · 5.01 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
#pragma once
#include <ATen/core/jit_type.h>
#include <ATen/core/interned_strings.h>
#include <ATen/core/ivalue.h>
#include <ATen/core/alias_info.h>
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 {
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)
: 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)) {
if (default_value_ && default_value_->isTensor()) {
auto t = default_value_->toTensor();
AT_ASSERT(!t.defined() || t.is_variable());
}
}
const std::string& name() const {
return name_;
}
TypePtr type() const {
return type_;
}
c10::optional<int32_t> N() const {
return N_;
}
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_;
}
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_;
};
struct FunctionSchema {
FunctionSchema(
std::string name,
std::vector<Argument> arguments,
std::vector<Argument> returns,
bool is_vararg = false,
bool is_varret = false)
: name_(std::move(name)),
arguments_(std::move(arguments)),
returns_(std::move(returns)),
is_vararg_(is_vararg),
is_varret_(is_varret) {}
FunctionSchema(
Symbol name,
std::vector<Argument> arguments,
std::vector<Argument> returns,
bool is_vararg = false,
bool is_varret = false,
std::vector<std::string> writes = {})
: FunctionSchema(
name.toQualString(),
std::move(std::move(arguments)),
std::move(std::move(returns)),
is_vararg,
is_varret) {}
private:
const std::string name_;
const std::vector<Argument> arguments_;
const 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 'primtive' operators whose
// arguments are not checked by schema
const bool is_vararg_;
const bool is_varret_;
public:
const std::string& name() const {
return 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;
}
};
// for debugging, make sure we can describe the call site
inline std::ostream& operator<<(std::ostream& out, const Argument& arg) {
return out << arg.type()->str() << " " << arg.name() << (arg.default_value() ? "=<default>" : "");
}
inline std::ostream& operator<<(std::ostream& out, const FunctionSchema& schema) {
// eventually this should look almost identical to python arg parser, but
// it is simpler for now to work directly on this schema
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;
}
out << schema.arguments()[i];
}
if(schema.is_vararg()) {
if(schema.arguments().size() > 0)
out << ", ";
out << "...";
}
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;
}
} // namespace c10