-
Notifications
You must be signed in to change notification settings - Fork 21.4k
/
check_alias_annotation.cpp
260 lines (227 loc) · 7.63 KB
/
check_alias_annotation.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
#include <torch/csrc/jit/passes/utils/check_alias_annotation.h>
#include <torch/csrc/jit/passes/constant_propagation.h>
#include <torch/csrc/jit/passes/normalize_ops.h>
#include <torch/csrc/jit/runtime/operator.h>
namespace torch {
namespace jit {
namespace {
IValue deepCopy(const IValue& self) {
// primitive types can be copied directly
if (!self.isPtrType()) {
return self;
}
// Tensors need special handling, since copy assignment creates an alias
if (self.isTensor()) {
return IValue(self.toTensor().clone(at::MemoryFormat::Preserve));
}
if (self.isTensorList()) {
c10::List<at::Tensor> newList;
for (const at::Tensor& oldTensor : self.toTensorVector()) {
newList.push_back(oldTensor.clone(at::MemoryFormat::Preserve));
}
return newList;
}
// Lists of ivalues should recursively deep copy their contents
if (self.isList()) {
auto source = std::move(self).toList();
auto newList = c10::impl::GenericList(source.elementType());
newList.reserve(source.size());
for (const IValue& value : source) {
newList.push_back(deepCopy(value));
}
return newList;
}
// Regular lists can copy assign
if (self.isIntList()) {
return IValue(self.toIntList().copy());
} else if (self.isDoubleList()) {
return IValue(self.toDoubleList().copy());
} else if (self.isBoolList()) {
return IValue(self.toBoolList().copy());
} else if (self.isString()) {
return IValue(self.toStringRef());
}
// If in the future we add more reference types that are used in aten ops,
// we'll have to add them as cases here.
AT_ASSERT(false);
}
Stack deepCopy(const Stack& stack) {
Stack ret;
ret.reserve(stack.size());
for (const auto& v : stack) {
ret.push_back(deepCopy(v));
}
return ret;
}
bool deepEquals(const IValue& lhs, const IValue& rhs) {
if (lhs.isTensor() && rhs.isTensor()) {
return lhs.toTensor().equal(rhs.toTensor());
}
return lhs == rhs;
}
struct AliasAndIValue {
AliasAndIValue(c10::optional<at::AliasInfo> aliasInfo, IValue iValue)
: aliasInfo(std::move(aliasInfo)), iValue(std::move(iValue)) {}
const c10::optional<at::AliasInfo> aliasInfo;
const IValue iValue;
};
// No inputs should alias each other
void checkInputPreconditions(const Stack& inputs) {
for (size_t i = 0; i < inputs.size(); i++) {
for (size_t j = 0; j < inputs.size(); j++) {
if (i == j) {
continue;
}
const auto& lhs = inputs.at(i);
const auto& rhs = inputs.at(j);
AT_ASSERT(!lhs.isAliasOf(rhs));
}
}
}
// If two ivalues alias, they must share an alias set
void checkAliases(
const std::vector<AliasAndIValue>& inputs,
const std::vector<AliasAndIValue>& outputs) {
for (const auto& output : outputs) {
// if this output aliases any input, make sure that they share an alias set
for (const auto& input : inputs) {
if (output.iValue.isAliasOf(input.iValue)) {
const auto inputSet = input.aliasInfo;
const auto outputSet = output.aliasInfo;
AT_ASSERT(inputSet && outputSet);
bool found = false;
for (const auto& set : inputSet->beforeSets()) {
if (outputSet->beforeSets().count(set)) {
found = true;
break;
}
}
AT_ASSERT(found);
}
}
}
}
// If we didn't specify that we write to an input value, it must have not
// changed
void checkWrites(
const std::vector<AliasAndIValue>& inputs,
const std::vector<IValue>& deepCopiedInputs) {
AT_ASSERT(inputs.size() == deepCopiedInputs.size());
for (size_t i = 0; i < inputs.size(); i++) {
const auto& input = inputs[i];
const auto& deepCopiedInput = deepCopiedInputs[i];
if (!input.aliasInfo || !input.aliasInfo->isWrite()) {
AT_ASSERT(deepEquals(input.iValue, deepCopiedInput));
}
}
}
const Node* findNodeForOp(
const Graph& g,
const std::string& unqualifiedOpName) {
const auto opName = Symbol::fromQualString("aten::" + unqualifiedOpName);
for (const auto node : g.nodes()) {
if (node->kind() == opName) {
return node;
}
}
// Check for alias-ed operator names
const auto aliasOp = torch::jit::getOperatorAliasMap().find(opName);
AT_ASSERT(aliasOp != torch::jit::getOperatorAliasMap().end());
for (const auto node : g.nodes()) {
if (node->kind() == aliasOp->second) {
return node;
}
}
AT_ASSERT(false);
}
// Handle a few special cases where we need to propagate constants
// manually
// TODO(suo): we should be able to move this stuff to constant prop
c10::optional<IValue> toIValueProp(const Value* v) {
if (v->node()->kind() == prim::ListConstruct) {
std::vector<IValue> genericList;
for (auto input : v->node()->inputs()) {
if (auto elem = toIValue(input)) {
genericList.push_back(*elem);
} else {
// One of the list elements isn't constant.
return c10::nullopt;
}
}
// Specialize the list based on ListConstruct's return type
auto listType = v->node()->output()->type();
auto containedType = listType->containedTypes().at(0);
if (containedType == IntType::get()) {
return IValue(
fmap(genericList, [](const IValue& v) { return v.toInt(); }));
} else if (containedType == FloatType::get()) {
return IValue(
fmap(genericList, [](const IValue& v) { return v.toDouble(); }));
} else if (containedType->isSubtypeOf(TensorType::get())) {
return IValue(
fmap(genericList, [](const IValue& v) { return v.toTensor(); }));
} else {
return c10::nullopt;
}
}
if (v->node()->kind() == aten::Float) {
if (auto maybe_stack = runNodeIfInputsAreConstant(v->node())) {
return maybe_stack->at(0);
}
}
return c10::nullopt;
}
} // namespace
void checkAliasAnnotation(
const std::shared_ptr<Graph>& graph,
std::vector<IValue> pythonInputs,
const std::string& unqualifiedOpName) {
// Find the node that corresponds to our op name
const auto node = findNodeForOp(*graph, unqualifiedOpName);
// Build the stack to use as input to the op
Stack stack;
for (const auto input : node->inputs()) {
if (input->node() == graph->param_node()) {
// This value was passed as an input in python
push(stack, pythonInputs.at(input->offset()));
} else {
// This a generated constant, which we need to evaluate
auto inputValue = toIValue(input);
if (!inputValue) {
inputValue = toIValueProp(input);
}
if (inputValue) {
push(stack, *inputValue);
} else {
AT_ASSERT(input->type()->kind() == TypeKind::OptionalType);
push(stack, IValue());
}
}
}
// Precondition: no inputs should alias each other. So if we find an alias,
// it was created by the op.
checkInputPreconditions(stack);
const auto schema = node->schema();
std::vector<AliasAndIValue> inputsToCheck;
for (size_t i = 0; i < schema.arguments().size(); i++) {
inputsToCheck.emplace_back(
schema.arguments().at(i).alias_info(), stack.at(i));
}
// Save a copy of the inputs so we can check whether the original inputs were
// written to.
const auto inputsDeepCopy = deepCopy(stack);
// Run the op
node->getOperation()(&stack);
const auto outputs = std::move(stack);
std::vector<AliasAndIValue> outputsToCheck;
for (size_t i = 0; i < schema.returns().size(); i++) {
outputsToCheck.emplace_back(
schema.returns().at(i).alias_info(), outputs.at(i));
}
// Check that if any alias was created, we annotated it properly.
checkAliases(inputsToCheck, outputsToCheck);
// Check that if nothing was accidentally written to.
checkWrites(inputsToCheck, inputsDeepCopy);
}
} // namespace jit
} // namespace torch