forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbolic_shape_analysis.cpp
529 lines (484 loc) · 20.1 KB
/
symbolic_shape_analysis.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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
#include <ATen/core/interned_strings.h>
#include <c10/util/Exception.h>
#include <c10/util/irange.h>
#include <torch/csrc/jit/ir/alias_analysis.h>
#include <torch/csrc/jit/ir/constants.h>
#include <torch/csrc/jit/ir/ir.h>
#include <torch/csrc/jit/ir/ir_views.h>
#include <torch/csrc/jit/jit_log.h>
#include <torch/csrc/jit/passes/common_subexpression_elimination.h>
#include <torch/csrc/jit/passes/constant_pooling.h>
#include <torch/csrc/jit/passes/constant_propagation.h>
#include <torch/csrc/jit/passes/dead_code_elimination.h>
#include <torch/csrc/jit/passes/integer_value_refinement.h>
#include <torch/csrc/jit/passes/loop_unrolling.h>
#include <torch/csrc/jit/passes/lower_tuples.h>
#include <torch/csrc/jit/passes/peephole.h>
#include <torch/csrc/jit/passes/peephole_list_idioms.h>
#include <torch/csrc/jit/passes/peephole_non_tensor.h>
#include <torch/csrc/jit/passes/remove_mutation.h>
#include <torch/csrc/jit/passes/shape_analysis.h>
#include <torch/csrc/jit/passes/symbolic_shape_analysis.h>
#include <torch/csrc/jit/runtime/exception_message.h>
#include <torch/csrc/jit/runtime/symbolic_shape_registry.h>
#include <torch/csrc/utils/memory.h>
#include <memory>
#include <unordered_map>
#include <vector>
/*
XXX: this is still in prototype phase and has much work left to do, including
but not limited to:
- Refactor APIs
- Add decent coverage of common ops
- Add shape analysis pass on Graph that handles Loops
- Allow concurrent reads to the operator map
- Successive applications of same inputs to same shape function (e.g. series of
pointwise ops)
- Supporting returning partially evaluated shape compute graph
*/
static bool symbolic_shape_analysis_test_mode = false;
namespace torch {
namespace jit {
// This is similar to c10::SymbolicShape, but instead of either having
// a concrete dimension or a symbolic dimension, an argument may be:
// - A Symbolic Dimension
// - A Constant Integer
// - Neither of the above. The third case can occur due to inputs to
// ops like view that accept negative values. Maintaining the distinction
// between an unknown symbolic dimension and an unknown integer allows
// us to optimize out comparisons to values < 0 (symbolic shapes are always >=
// 0) For example, a call like graph(%y: Tensor(SS(-1), 10, 10), %inp: int):
// %five: int = prim::Constant[value=5]()
// %zero: int = prim::Constant[value=0]()
// %1 : int = aten::size(%y, %zero)
// %2 : int[] = prim::ListConstruct(%five, %1, %inp)
// %y.2: Tensor(5, SS(-1), (New Symbolic Shape)) = aten::view(%y, %2)
//
// x.view([5, y.size(0), inp])
// will have inputs equal to [5, SS(-1), c10::nullopt]
struct ShapeArg
: public std::
pair<c10::optional<c10::ShapeSymbol>, c10::optional<int64_t>> {
using pair::pair;
static ShapeArg unknownInteger() {
return ShapeArg();
}
ShapeArg(int64_t int_value) {
this->first = c10::nullopt;
this->second = int_value;
}
ShapeArg(c10::ShapeSymbol ss) {
if (ss.is_static()) {
this->first = c10::nullopt;
this->second = ss.value();
} else {
this->first = ss;
this->second = c10::nullopt;
}
}
c10::optional<int64_t> asConstantInt() {
return this->second;
}
c10::optional<c10::ShapeSymbol> asShapeSymbol() {
return this->first;
}
private:
ShapeArg() {
this->first = c10::nullopt;
this->second = c10::nullopt;
}
};
struct ShapeArguments {
ShapeArguments(const c10::SymbolicShape& ss) {
TORCH_INTERNAL_ASSERT(ss.rank())
for (size_t i = 0; i < *ss.rank(); ++i) {
maybe_shape_symbols_.push_back(ShapeArg(ss.at(i)));
}
}
ShapeArguments(std::vector<ShapeArg> ss) {
maybe_shape_symbols_ = std::move(ss);
}
int64_t len() {
return maybe_shape_symbols_.size();
}
ShapeArg at(size_t i) {
return maybe_shape_symbols_.at(i);
}
private:
std::vector<ShapeArg> maybe_shape_symbols_;
;
};
bool setSymbolicShapeAnalysisTestMode(bool value) {
bool old_value = symbolic_shape_analysis_test_mode;
symbolic_shape_analysis_test_mode = value;
return old_value;
}
bool symbolicShapeAnalysisTestModeEnabled() {
return symbolic_shape_analysis_test_mode;
}
c10::optional<size_t> normIndex(int64_t index, size_t len) {
if (index < 0) {
index = index + len;
}
if (index >= 0 && index < static_cast<int64_t>(len)) {
return index;
} else {
return c10::nullopt;
}
}
void replaceWithIValue(Value* v, IValue val) {
WithInsertPoint guard(*v->node()->owningBlock()->nodes().begin());
v->replaceAllUsesWith(v->owningGraph()->insertConstant(val));
}
// Symbolic Shape Analysis works through iteratively partially evaluating
// a TorchScript shape compute graph by inputing properties from input
// Tensors. We can substitute in properties like `len(x)` and `x[1]`
// if they are statically on the input Tensors. We can also use
// assertions like `assert len(x) == 4` in order to refine the input
// length and unroll loops over its elements. We iteratively optimize and
// substitute in properties until we are unable to make any further
// optimizations. Finally, we try to extract Tensor properties from the output.
// For instance `return [1, 2, inp[2] + 1, inp[3]]` we know that the ouptut
// will be length 4 with first two dimensions equal to 1 and 2. We can also
// deduce that the 4th dimension has the same symbolic shape as inp[3], which
// means that we do know its concrete value statically but we can asssign sets
// of tensor dimensions which must be equal at runtime.
struct SymbolicShapeAnalyzer {
SymbolicShapeAnalyzer(
Node* n,
std::shared_ptr<Graph> shape_compute_graph,
const AliasDb& db)
: graph_(shape_compute_graph->copy()), node_(n) {
for (size_t i = 0; i < node_->inputs().size(); i++) {
auto type = node_->input(i)->type();
if (auto opt_type =
graph_->inputs().at(i)->type()->cast<OptionalType>()) {
// None will get handled with constant substitution later
if (!type->cast<OptionalType>() &&
!NoneType::get()->isSubtypeOf(type)) {
graph_->inputs().at(i)->setType(opt_type->getElementType());
}
} else if (graph_->inputs().at(i)->type()->cast<NumberType>()) {
graph_->inputs().at(i)->setType(type);
}
if (auto tt = type->castRaw<TensorType>()) {
// NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
c10::SymbolicShape symbolic_shapes = tt->symbolic_sizes();
// for testing, we don't insert complete tensor shapes and rely on our
// partial evaluation pipeline to propagate information.
// this is a good proxy for our ability to propagate non-complete shape
// information.
if (symbolic_shapes.isComplete() &&
!symbolic_shape_analysis_test_mode) {
replaceWithIValue(
graph_->inputs().at(i), *tt->sizes().concrete_sizes());
continue;
}
// TODO: remove, all constant tensors should have typed sizes
if (toIValue(node_->input(i)) && !symbolic_shape_analysis_test_mode) {
replaceWithIValue(
graph_->inputs().at(i),
constant_as<at::Tensor>(node_->input(i))->sizes());
continue;
}
// we can't optimize a tensor without fixed rank
if (symbolic_shapes.rank()) {
node_symbolic_input_indices_.emplace_back(i, symbolic_shapes);
}
} else if (
type->cast<ListType>() &&
type->cast<ListType>()->getElementType()->cast<TensorType>()) {
TORCH_INTERNAL_ASSERT(false); // not handled yet
} else if (auto ival = toIValue(node_->input(i))) {
replaceWithIValue(graph_->inputs().at(i), *ival);
} else if (
type->cast<ListType>() &&
type->cast<ListType>()->getElementType()->cast<IntType>()) {
if (node_->input(i)->node()->kind() == prim::ListConstruct &&
!db.hasWriters(node_->input(i))) {
// it is a very common in graphs to see patterns like:
// z = x.view(y.size())
// or:
// z = x.view(1, 10, y.size(0), y.size(1))
// We want to propagate symbolic dimensions and concrete sizes
// from y to z. To do this we try to associate symbolic dimensions
// or concrete sizes with the integer list inputs that have a
// constructor taken from constants or y.size() or y.size(0)
auto list_construct = node_->input(i)->node();
std::vector<ShapeArg> shape;
for (Value* v : list_construct->inputs()) {
if (auto constant = constant_as<int64_t>(v)) {
shape.emplace_back(*constant);
} else if (v->node()->kind() == aten::size) {
auto const_index = constant_as<int64_t>(v->node()->input(1));
auto tt = v->node()->input(0)->type()->expect<TensorType>();
auto ss = tt->symbolic_sizes();
if (!ss.rank() || !const_index) {
// if we are getting a size of a tensor, it is an unknown
// symbolic dimension instead of an unknown integer (must be
// >=0)
shape.emplace_back(at::ShapeSymbol::newSymbol());
continue;
}
auto norm_index = normIndex(*const_index, *ss.rank());
if (!norm_index) {
shape.emplace_back(at::ShapeSymbol::newSymbol());
continue;
}
shape.emplace_back(ss[*norm_index]);
} else {
shape.emplace_back(ShapeArg::unknownInteger());
}
}
node_symbolic_input_indices_.emplace_back(i, std::move(shape));
} else if (
node_->input(i)->node()->kind() == aten::size &&
!db.hasWriters(node_->input(i))) {
auto ten_inp = node_->input(i)->node()->input();
auto ss = ten_inp->type()->expect<TensorType>()->symbolic_sizes();
node_symbolic_input_indices_.emplace_back(i, ss);
}
}
}
}
c10::SymbolicShape run() {
bool made_change = true;
constexpr size_t MAX_ATTEMPTS = 8;
size_t curr_attempt = 0;
while (made_change && curr_attempt < MAX_ATTEMPTS) {
curr_attempt++;
made_change = false;
// symbolic shape concrete values are only used in final shape extraction
substituteInputTensorProperties(/*symbolic_shape_values*/ nullptr);
// TODO: lower simple tuples ?
made_change |= RemoveListMutation(graph_);
made_change |= UnrollConstantLoops(graph_);
made_change |= ConstantPropagation(graph_);
made_change |= PeepholeOptimizeNonTensor(graph_);
made_change |=
PeepholeOptimizeListIdioms(graph_, /*refine_list_len*/ true);
made_change |= RefineIntegerValues(graph_);
made_change |= ConstantPropagation(graph_);
made_change |= EliminateCommonSubexpression(graph_);
EliminateDeadCode(graph_);
}
std::unordered_map<Value*, int64_t> symbolic_shape_values;
substituteInputTensorProperties(&symbolic_shape_values);
GRAPH_DUMP("Done with partial evaluation", graph_);
return extractOutputShape(symbolic_shape_values);
}
private:
void substituteInputTensorProperties(
std::unordered_map<Value*, int64_t>* symbolic_shape_values) {
// clang-format off
// here we iteratively substitute properties of the node's input tensors
// into the shape compute graph. we can substitute constants into the
// like len(inp) or inp[0] if the tensor has a fixed length or a fixed
// first dimension. we also try to resolve symbolic shapes of the same
// symbolic value to the same Value * in the shape compute graph.
// for the shape logic:
// dim1 = inp1[0]
// dim2 = inp2[0]
// return dim1 if dim2 == 1 else dim2
// if we see that inp1[0] and inp2[0] both have the same symbolic shape
// value, then it is a valid transformation to replace dim2 with dim1 or
// vice versa. to do this we collect all Value * for a particular symbolic
// shape. Then, we replace all Value * within that set with their dominator.
// In the example above, this allows us to infer that the output will be the
// symbolic dimension value of dim1.
// if `symbolic_shape_values` is not null, record list accesses
// which resolve to symbolic dimension values with their concrete symbolic
// shape value. Because symbolic dimensions are represented as negative numbers and
// are not real values, inserting them as constants in the graph would invalidate
// the graph for further use. Instead, we keep track of what their value would be
// for extracting output shapes.
// clang-format on
std::unordered_map<int64_t, std::vector<Value*>> symbolic_shape_map;
for (const auto& index_symbolic_shape : node_symbolic_input_indices_) {
auto index = index_symbolic_shape.first;
auto shape_arguments = index_symbolic_shape.second;
for (const auto& use : graph_->inputs().at(index)->uses()) {
// TODO: either decompose composite ops like slice or add handling here
switch (use.user->kind()) {
case aten::len: {
size_t len = shape_arguments.len();
replaceWithIValue(use.user->output(), static_cast<int64_t>(len));
} break;
case aten::__getitem__: {
auto index = constant_as<int64_t>(use.user->inputs().at(1));
if (!index) {
continue;
}
auto norm_index = normIndex(*index, shape_arguments.len());
if (!norm_index) {
continue;
}
auto shape_arg = shape_arguments.at(*norm_index);
if (auto const_int = shape_arg.asConstantInt()) {
replaceWithIValue(use.user->output(), const_int);
continue;
}
auto maybe_shape_symbol = shape_arg.asShapeSymbol();
if (!maybe_shape_symbol) {
continue;
}
auto shape_symbol = *maybe_shape_symbol;
if (symbolic_shape_values) {
symbolic_shape_values->emplace(
use.user->output(), shape_symbol.value());
} else {
int64_t symbolic_index = shape_symbol.value();
symbolic_shape_map[symbolic_index].push_back(use.user->output());
}
for (const auto& sym_uses : use.user->output()->uses()) {
auto k = sym_uses.user->kind();
if (k != aten::ge && k != aten::le && k != aten::ne &&
k != aten::eq && k != aten::lt && k != aten::gt) {
break;
}
auto other_index = 1 - sym_uses.offset;
auto other_value =
constant_as<int64_t>(sym_uses.user->input(other_index));
if (!other_value) {
continue;
}
// check for dim >= 0, 0 <= dim
// dim >= 0
if (k == aten::ge && *other_value == 0 && other_index == 1) {
replaceWithIValue(sym_uses.user->output(), true);
continue;
}
// 0 <= dim
if (k == aten::le && *other_value == 0 && other_index == 0) {
replaceWithIValue(sym_uses.user->output(), true);
continue;
}
// check for dim comparisons to negative number
if (*other_value >= 0) {
continue;
}
if (k == aten::eq || k == aten::ne) {
// True if:
// -2 != {Positive}
replaceWithIValue(sym_uses.user->output(), k == aten::ne);
} else {
// True if:
// -2 <= / < {Positive}
// {Positive} >= / > {-2}
bool true_val =
((other_index == 0 && (k == aten::le || k == aten::lt)) ||
(other_index == 1 && (k == aten::ge || k == aten::gt)));
replaceWithIValue(sym_uses.user->output(), true_val);
}
}
}
}
}
for (const auto& symbolic_set : symbolic_shape_map) {
mergeSymbolicShapeSets(symbolic_set.second);
}
}
}
void mergeSymbolicShapeSets(const std::vector<Value*>& symbolic_set) {
// `symbolic_set` represents a set of Value * which are all equal
// to each other. Here, we optimize the graph by replacing values
// in the set with other dominating values.
// in the following example, where a, b and c are all in the same
// symbolic set:
// if cond:
// a = li[0]
// b = li[1]
// return [a, b]
// else:
// c = li[0]
// return [c, c]
// we can replace `b` with `a` because it is dominated by `a`,
// but we cannot replace `c` with another dominating value
// there are ways to compute this more efficiently but typically number of
// Values for each symbolic set is low and this is cheap to run
for (const auto i : c10::irange(symbolic_set.size())) {
Value* v = symbolic_set[i];
Value* dominating_value = v;
for (const auto& sym_set : symbolic_set) {
if (dominating_value->node()->isDominatedBy(sym_set->node())) {
dominating_value = sym_set;
}
}
if (dominating_value != v) {
v->replaceAllUsesWith(dominating_value);
}
}
}
c10::SymbolicShape extractOutputShape(
std::unordered_map<Value*, int64_t>& symbolic_shape_values) {
TORCH_INTERNAL_ASSERT(graph_->outputs().size() == 1);
auto output = graph_->outputs().at(0);
TORCH_INTERNAL_ASSERT(
output->type()->cast<ListType>() &&
output->type()->cast<ListType>()->getElementType()->cast<IntType>());
if (output->node()->kind() == prim::Constant) {
auto int_list = toIValue(output)->toIntVector();
return c10::SymbolicShape(int_list);
}
// TODO: would be nice if there were easy facility to look at uses and see
// if they are all pure instead of instanting db.
AliasDb db(graph_);
// If it is not a single list construct or constant, bail,
// otherwise we cannot analyze its output and it might be modified
if (output->node()->kind() != prim::ListConstruct ||
db.hasWriters(output)) {
GRAPH_DEBUG("Could not extract shape ", getHeader(node_));
return c10::SymbolicShape();
}
Node* list_construct = output->node();
std::vector<c10::optional<int64_t>> output_shape;
for (Value* input : list_construct->inputs()) {
if (symbolic_shape_values.count(input)) {
output_shape.push_back(symbolic_shape_values[input]);
} else {
output_shape.push_back(constant_as<int64_t>(input));
}
}
return c10::SymbolicShape(output_shape);
}
// node input indices that are TensorType and we need to iteratively
// substitute properties of. We only substitute properties
// of TensorTypes with a fixed dimension but not a complete shape,
// because a complete shape we can completely replace with a constant
// and non-fixed dimensions we cannot reason about at all
// TODO: might be cleaner to store as a pair of index -> symbolic shape
// but there were weird lifetime issues
std::vector<std::pair<int64_t, ShapeArguments>> node_symbolic_input_indices_;
std::shared_ptr<Graph> graph_;
Node* node_;
};
void PropagateShapesWithShapeFunction(
Node* n,
std::shared_ptr<Graph>& shape_compute_graph,
const AliasDb& db) {
c10::SymbolicShape out =
SymbolicShapeAnalyzer(n, shape_compute_graph, db).run();
n->output()->setType(
n->output()->type()->expect<TensorType>()->withSymbolicShapes(out));
}
void PropagateShapesOnBlock(Block* b, const AliasDb& db) {
for (Node* n : b->nodes()) {
// TODO: handle loop
if (n->kind() == prim::If) {
IfView if_v(n);
PropagateShapesOnBlock(if_v.thenBlock(), db);
PropagateShapesOnBlock(if_v.elseBlock(), db);
mergeTypes(if_v.thenOutputs(), if_v.elseOutputs(), if_v.outputs());
} else if (n->maybeSchema()) {
if (auto maybe_graph = shapeComputeGraphForSchema(n->schema())) {
PropagateShapesWithShapeFunction(n, *maybe_graph, db);
}
}
}
}
void PropagateShapesOnGraph(std::shared_ptr<Graph>& graph) {
AliasDb db(graph);
PropagateShapesOnBlock(graph->block(), db);
}
} // namespace jit
} // namespace torch