forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_autodiff_subgraphs.cpp
281 lines (249 loc) · 8.87 KB
/
create_autodiff_subgraphs.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
#include <torch/csrc/jit/passes/create_autodiff_subgraphs.h>
#include <c10/util/Exception.h>
#include <torch/csrc/jit/ir/alias_analysis.h>
#include <torch/csrc/jit/ir/ir.h>
#include <torch/csrc/jit/jit_log.h>
#include <torch/csrc/jit/passes/canonicalize.h>
#include <torch/csrc/jit/passes/common_subexpression_elimination.h>
#include <torch/csrc/jit/passes/utils/subgraph_utils.h>
#include <torch/csrc/jit/runtime/autodiff.h>
namespace torch {
namespace jit {
namespace {
struct WorkBlock : public std::pair<Node*, Node*> {
using pair::pair;
Node* begin() {
return this->first;
}
Node* end() {
return this->second;
}
};
class SubgraphSlicer {
public:
SubgraphSlicer(
Block* block,
std::shared_ptr<Graph> graph,
size_t minSubgraphSize,
AliasDb& aliasDb,
std::vector<Node*>& diff_nodes)
: block_(block),
graph_(std::move(graph)),
minSubgraphSize_(minSubgraphSize),
aliasDb_(aliasDb),
diff_nodes_(diff_nodes) {}
void run() {
// We maintain alias db correctness in-place while building up the autodiff
// subgraphs, however it is difficult to preserve correctness when
// un-inlining autodiff subgraphs. We first recursively construct all
// subgraphs and then recursively cleanup & unmerge the small subgraphs
buildupSubgraphs();
cleanupSubgraphs();
// Run CSE globally onceto eliminate duplicates that may have occurred
// while inlining subgraphs.
EliminateCommonSubexpression(graph_);
}
void cleanupSubgraphs() {
auto curNode = *block_->nodes().rbegin();
while (curNode != *block_->nodes().rend()) {
// Save the previous node, since we might delete `curNode` in next block
auto prevNode = curNode->prev();
if (curNode->kind() == prim::DifferentiableGraph) {
// Inlining nodes may cause some subexpression to come back in the
// subgraphs (for example, copying constants in repeatedly will generate
// redundant prim::Constants). Run CSE to clean them up.
EliminateCommonSubexpression(curNode->g(attr::Subgraph));
if (!inlineIfTooSmall(curNode)) {
diff_nodes_.push_back(curNode);
}
}
curNode = prevNode;
}
for (Node* n : block_->nodes()) {
for (Block* b : n->blocks()) {
SubgraphSlicer(b, graph_, minSubgraphSize_, aliasDb_, diff_nodes_)
.cleanupSubgraphs();
}
}
}
void buildupSubgraphs() {
// We need to run the slicer multiple times in order to get all merge
// opportunities. This is because moveBeforeTopologicalValid may reorder
// nodes to be AFTER the current iteration point. In order to properly
// consider those nodes for merging, we need run the pass until no changes
// have been made.
//
// Example:
// c = f(a, b)
// d = f(c)
// e = f(d) <- iter is here, moving upward
// After c.moveBeforeTopologicallyValid(e), we have:
// c = f(a, b)
// e = f(d) <- iter still here
// d = f(c) <- this was node moved on the other side.
// see [workblocks]
auto workblocks = buildWorkBlocks();
for (auto& workblock : workblocks) {
bool any_changed = true;
while (any_changed) {
any_changed = false;
for (auto it = workblock.end()->reverseIterator();
it != workblock.begin()->reverseIterator();) {
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
bool changed;
std::tie(it, changed) = scanNode(*it);
any_changed |= changed;
}
}
}
// Construct Subgraphs Recursively
for (Node* n : block_->nodes()) {
for (auto subBlock : n->blocks()) {
SubgraphSlicer(
subBlock, graph_, minSubgraphSize_, aliasDb_, diff_nodes_)
.buildupSubgraphs();
}
}
}
private:
std::vector<WorkBlock> buildWorkBlocks() {
// [workblocks]
// the IR has many nodes which can never be reordered around, such as a
// prim::Bailout. if a node N is surrounded by two nodes which cannot be
// reordered, A and B, then a differentiable subgraph that is created from N
// can only contain nodes from (A, B) The nodes from A to B represent one
// work block for the subgraph slicer to work on. By creating these up
// front, we avoid retraversing the whole graph block any time scanNode
// returns, and we can also avoid attempting to create differentiable
// subgraphs in work blocks that do not contain a # of differentiable nodes
// >= minSubgraphSize_
Node* end_bound_node = block_->return_node();
Node* curr = end_bound_node->prev();
std::vector<WorkBlock> worklist;
size_t differentiable_nodes = 0;
while (curr != block_->param_node()) {
differentiable_nodes += shouldConsiderForMerge(curr);
// cannot reorder around side effectful nodes
if (curr->hasSideEffects()) {
// not enough differentiable nodes to create a differentiable subgraph
if (differentiable_nodes >= minSubgraphSize_) {
worklist.emplace_back(curr, end_bound_node);
}
differentiable_nodes = 0;
end_bound_node = curr;
}
curr = curr->prev();
}
if (differentiable_nodes >= minSubgraphSize_) {
worklist.emplace_back(curr, end_bound_node);
}
return worklist;
}
// Inline this node's group subgraph into the outer graph if it's smaller
// than the specified minimum size.
//
// Returns true if an inlining has occurred, false otherwise.
bool inlineIfTooSmall(Node* n) {
AT_ASSERT(n->kind() == prim::DifferentiableGraph);
auto subgraph = SubgraphUtils::getSubgraph(n);
size_t i = 0;
for (auto it = subgraph->nodes().begin(); it != subgraph->nodes().end();
++it) {
i += !it->notExecutedOp();
if (i >= minSubgraphSize_) {
return false;
}
}
SubgraphUtils::unmergeSubgraph(n);
return true;
}
value_list sortReverseTopological(ArrayRef<Value*> inputs) {
value_list result;
for (auto i : inputs) {
if (i->node()->owningBlock() == block_) {
result.push_back(i);
}
}
// Sort in reverse topological order
std::sort(result.begin(), result.end(), [&](Value* a, Value* b) {
return a->node()->isAfter(b->node());
});
return result;
}
bool isViewOp(Node* n) {
switch (n->kind()) {
case aten::view:
case aten::view_as:
case aten::reshape:
case aten::reshape_as:
case aten::transpose:
case aten::expand:
case aten::expand_as:
return true;
}
return false;
}
bool shouldConsiderForMerge(Node* node) {
// if we're already in the process of merging
if (node->kind() == prim::DifferentiableGraph) {
return true;
}
if (node->kind() == prim::Constant) {
return false;
}
// view ops as outputs of differentiable subgraphs can cause incorrect
// differentiation for now, do not include them in the subgraph
if (isViewOp(node)) {
return false;
}
return isDifferentiable(node);
}
std::pair<graph_node_list::iterator, bool> scanNode(Node* consumer) {
if (shouldConsiderForMerge(consumer)) {
if (consumer->kind() != prim::DifferentiableGraph) {
consumer = SubgraphUtils::createSingletonSubgraphAndUpdateAliasing(
consumer, prim::DifferentiableGraph, aliasDb_);
}
auto inputs = sortReverseTopological(consumer->inputs());
for (auto input : inputs) {
if (auto group = tryMerge(consumer, input->node())) {
// we successfully merged, so the new group's `inputs` may have
// changed. So rescan the new group for more merging opportunities.
return std::make_pair(group.value()->reverseIterator(), true);
}
}
}
return std::make_pair(++consumer->reverseIterator(), false);
}
// Try to merge `producer` into `consumer`. If successful, this destroys
// `producer` and returns the `consumer` group.
c10::optional<Node*> tryMerge(Node* consumer, Node* producer) {
AT_ASSERT(consumer->kind() == prim::DifferentiableGraph);
bool canMerge = shouldConsiderForMerge(producer) &&
aliasDb_.moveBeforeTopologicallyValid(producer, consumer);
if (!canMerge) {
return c10::nullopt;
}
SubgraphUtils::mergeNodeIntoSubgraphAndUpdateAliasing(
producer, consumer, aliasDb_);
return consumer;
}
Block* block_;
std::shared_ptr<Graph> graph_;
size_t minSubgraphSize_;
AliasDb& aliasDb_;
std::vector<Node*>& diff_nodes_;
};
} // anonymous namespace
std::vector<Node*> CreateAutodiffSubgraphs(
const std::shared_ptr<Graph>& graph,
size_t threshold) {
std::vector<Node*> diff_nodes;
AliasDb db(graph);
GRAPH_DEBUG("Before creating autodiff subgraphs", *graph);
SubgraphSlicer(graph->block(), graph, threshold, db, diff_nodes).run();
GRAPH_DEBUG("After creating autodiff subgraphs", *graph);
return diff_nodes;
}
} // namespace jit
} // namespace torch