forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
canonicalize_modified_loop.cpp
68 lines (60 loc) · 2.31 KB
/
canonicalize_modified_loop.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
#include <functional>
#include <memory>
#include <string>
#include <torch/csrc/Export.h>
#include <torch/csrc/jit/frontend/canonicalize_modified_loop.h>
#include <torch/csrc/jit/ir/ir.h>
#include <torch/csrc/jit/ir/ir_views.h>
namespace torch::jit {
// Transforms a Loop that has both a trip count specified and a loop
// body condition so that the iter count is no longer specified
// and it is recognizable as a python while loop.
static void canonicalizeModifiedLoop(Node* n) {
LoopView loop(n);
if (loop.loopType() != LoopView::ModifiedLoop) {
return;
}
auto g = n->owningGraph();
WithInsertPoint node_insert(n);
auto zero = g->insertConstant(0);
auto one = g->insertConstant(1);
auto max_trip_count = loop.maxTripCount();
auto condition = g->insert(aten::gt, {max_trip_count, zero});
loop.replaceMaxTripCount(
g->insertConstant(std::numeric_limits<int64_t>::max()));
auto inp_condition = toIValue(loop.inputCond());
if (inp_condition == c10::nullopt || inp_condition->toBool() == false) {
condition = g->insert(aten::__and__, {condition, loop.inputCond()});
}
loop.replaceInputCondition(condition);
n->addOutput()->setType(IntType::get());
WithInsertPoint loop_insert(loop.bodyBlock());
n->addInput(zero);
auto new_iter = loop.bodyBlock()->addInput()->setType(IntType::get());
// unset unique name for jitter, its replacement does not have a name
loop.currentTripCount()->setDebugName("")->replaceAllUsesWith(new_iter);
auto inc_iter = g->insert(aten::add, {new_iter, one});
loop.bodyBlock()->registerOutput(inc_iter);
auto less_than_max_trip = g->insert(aten::lt, {inc_iter, max_trip_count});
auto loop_continue = loop.nextCond();
auto new_condition =
g->insert(aten::__and__, {less_than_max_trip, loop_continue});
loop.bodyBlock()->eraseOutput(0);
loop.bodyBlock()->insertOutput(0, new_condition);
}
static void canonicalizeModifiedLoops(Block* block) {
for (Node* n : block->nodes()) {
for (Block* b : n->blocks()) {
canonicalizeModifiedLoops(b);
}
if (n->kind() == prim::Loop) {
canonicalizeModifiedLoop(n);
}
}
}
// Transforms loops so that they can be represented as python
// for or while loops
TORCH_API void CanonicalizeModifiedLoops(std::shared_ptr<Graph>& graph) {
canonicalizeModifiedLoops(graph->block());
}
} // namespace torch::jit