forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofiling_graph_executor_impl.cpp
176 lines (154 loc) · 5.11 KB
/
profiling_graph_executor_impl.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
#include <torch/csrc/jit/jit_log.h>
#include <torch/csrc/jit/passes/bailout_graph.h>
#include <torch/csrc/jit/passes/canonicalize_ops.h>
#include <torch/csrc/jit/passes/clear_undefinedness.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/create_autodiff_subgraphs.h>
#include <torch/csrc/jit/passes/dead_code_elimination.h>
#include <torch/csrc/jit/passes/graph_fuser.h>
#include <torch/csrc/jit/passes/guard_elimination.h>
#include <torch/csrc/jit/passes/inline_autodiff_subgraphs.h>
#include <torch/csrc/jit/passes/inplace_check.h>
#include <torch/csrc/jit/passes/insert_guards.h>
#include <torch/csrc/jit/passes/lower_grad_of.h>
#include <torch/csrc/jit/passes/peephole.h>
#include <torch/csrc/jit/passes/remove_expands.h>
#include <torch/csrc/jit/passes/requires_grad_analysis.h>
#include <torch/csrc/jit/passes/shape_analysis.h>
#include <torch/csrc/jit/passes/specialize_autogradzero.h>
#include <torch/csrc/jit/profiling_graph_executor_impl.h>
namespace torch {
namespace jit {
#if defined (FBCODE_CAFFE2) || defined (C10_MOBILE)
static std::atomic<bool> executor_mode{false};
static std::atomic<bool> profiling_mode{false};
#else
static std::atomic<bool> executor_mode{true};
static std::atomic<bool> profiling_mode{true};
#endif
std::atomic<bool>& getProfilingMode() {
return profiling_mode;
}
std::atomic<bool>& getExecutorMode() {
return executor_mode;
}
static bool needsGradientInProfilingMode(Block* b) {
for (auto n : b->nodes()) {
if (n->kind() == prim::BailOut) {
auto ptt = n->output()->type()->expect<TensorType>();
if (ptt->requiresGrad() && *ptt->requiresGrad()) {
return true;
}
}
for (auto ib : n->blocks()) {
if (needsGradientInProfilingMode(ib)) {
return true;
}
}
}
return false;
}
void ProfilingGraphExecutorImpl::runProfilingOptimizations(
std::shared_ptr<Graph>& copy) {
if (!getGraphExecutorOptimize()) {
LowerGradOf(*copy);
runRequiredPasses(copy);
return;
}
InsertGuards(copy);
LowerGradOf(*copy);
EliminateRedundantGuards(copy);
InsertBailOuts(copy);
GRAPH_DUMP("After InsertBailOuts: ", copy);
specializeAutogradZero(*copy);
runRequiredPasses(copy);
ConstantPropagation(copy);
runOptimization(copy);
if (needsGradientInProfilingMode(copy->block())) {
auto diff_nodes = CreateAutodiffSubgraphs(
copy,
getAutodiffSubgraphInlining() ? autodiffSubgraphNodeThreshold : 1);
for (Node* dnode : diff_nodes) {
auto diff_graph = std::move(dnode->g(attr::Subgraph));
Gradient gradient = differentiate(diff_graph);
runOptimization(gradient.f);
// run non diff optimization on the forward graph
runNondiffOptimization(gradient.f);
packGradient(gradient, dnode);
}
InlineAutodiffSubgraphs(
copy,
getAutodiffSubgraphInlining() ? autodiffSubgraphInlineThreshold : 1);
} else {
runNondiffOptimization(copy);
}
EliminateDeadCode(copy);
GRAPH_DUMP("Optimized Graph : ", copy);
}
void ProfilingGraphExecutorImpl::runProfilingInsensitiveOptimizations(
std::shared_ptr<Graph>& copy) {
LowerGradOf(*copy);
GRAPH_DUMP("runProfilingInsensitiveOptimizations", copy);
if (getProfilingMode()) {
ClearUndefinedness(copy);
}
runRequiredPasses(copy);
if (!getGraphExecutorOptimize()) {
return;
}
ConstantPropagation(copy);
EliminateDeadCode(copy);
EliminateCommonSubexpression(copy);
ConstantPooling(copy);
PeepholeOptimize(copy);
EliminateDeadCode(copy);
CheckInplace(copy);
}
ProfilingGraphExecutorImpl::ProfilingGraphExecutorImpl(
const std::shared_ptr<Graph>& graph)
: GraphExecutorImplBase(graph) {}
ExecutionPlan ProfilingGraphExecutorImpl::getPlanFor(Stack& stack) {
std::lock_guard<std::mutex> lock(compile_mutex);
GRAPH_DEBUG("Running ProfilingGraphExecutorImpl ", this);
if (optimized_plan_) {
return *optimized_plan_;
}
// simple executor
if (!getProfilingMode()) {
auto copy = graph->copy();
runProfilingInsensitiveOptimizations(copy);
GRAPH_DUMP("Optimized SimpleExecutor Graph : ", copy);
optimized_plan_ = ExecutionPlan(copy);
return *optimized_plan_;
}
// if a profiling graph hasn't been created yet
if (!pr_) {
auto copy = graph->copy();
runProfilingInsensitiveOptimizations(copy);
pr_ = ProfilingRecord::instrumentGraph(copy);
auto pr_copy = pr_->graph()->copy();
GRAPH_DUMP("Profiled Graph: ", pr_copy);
profiling_plan_ = ExecutionPlan(pr_copy);
// fall-through
}
// profile until a graph is ready
if (!pr_->ready()) {
return *profiling_plan_;
}
auto copy = pr_->graph()->copy();
runProfilingOptimizations(copy);
// cache
optimized_plan_ = ExecutionPlan(copy);
return *optimized_plan_;
}
GraphExecutorState ProfilingGraphExecutorImpl::getDebugState() {
GraphExecutorState state;
TORCH_INTERNAL_ASSERT(optimized_plan_);
auto opt_plan = *optimized_plan_;
state.execution_plans.emplace(ArgumentSpec{0, 0}, opt_plan);
return state;
}
} // namespace jit
} // namespace torch