forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofiling_record.cpp
132 lines (111 loc) · 3.42 KB
/
profiling_record.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
#include <torch/csrc/jit/interpreter.h>
#include <torch/csrc/jit/passes/constant_propagation.h>
#include <torch/csrc/jit/profiling_record.h>
namespace torch {
namespace jit {
ProfilingRecord::ProfilingRecord(std::shared_ptr<Graph> g)
: profiled_graph_(std::move(g)), profiling_count_(1) {}
ProfileOp* ProfilingRecord::createProfileNode(
const std::function<void(Stack&)>& fp,
at::ArrayRef<Value*> inputs) {
auto pn = new ProfileOp(profiled_graph_.get(), fp);
for (auto in : inputs) {
pn->addInput(in);
}
return pn;
}
static void unprofileGraphInputs(const std::shared_ptr<Graph> &graph) {
for (auto i : graph->inputs()) {
if (i->type()->isSubtypeOf(TensorType::get())) {
i->setType(unshapedType(i->type()));
}
}
}
static void unprofileBlock(Block* start_block) {
std::vector<Block*> stack;
stack.push_back(start_block);
while (!stack.empty()) {
Block* block = stack.back();
stack.pop_back();
for (auto n : block->nodes()) {
for (auto o : n->outputs()) {
if (o->type()->isSubtypeOf(TensorType::get())) {
o->setType(unshapedType(o->type()));
}
}
stack.insert(stack.end(), n->blocks().begin(), n->blocks().end());
}
}
}
void ProfilingRecord::insertShapeProfile(Node *n, Value *i) {
auto pn = createProfileNode(nullptr, {i});
auto pno = pn->addOutput();
bool first = true;
pno->setType(TensorType::get());
std::function<void(Stack &)> shape_profiler = [this, pno,
first](Stack &stack) mutable {
IValue t;
pop(stack, t);
if (t.isTensor()) {
if (t.toTensor().defined()) {
auto pttp = tensorTypeInCurrentExecutionContext(t.toTensor());
std::lock_guard<std::mutex> lock(this->mutex_);
if (auto type = pno->type()->cast<TensorType>()) {
if (!first) {
pttp = pttp->merge(type);
}
pno->setType(pttp);
first = false;
}
} else {
pno->setType(TensorType::get()->withUndefined());
}
}
// passing t through
push(stack, t);
};
pn->setCallback(shape_profiler);
pn->insertBefore(n);
n->replaceInputWith(i, pn->output());
}
void ProfilingRecord::instrumentBlock(Block *block) {
for (auto it = block->nodes().begin(); it != block->nodes().end(); ++it) {
auto n = *it;
for (auto i : n->inputs()) {
if (!i->type()->isSubtypeOf(TensorType::get()) ||
i->node()->kind() == prim::profile) {
continue;
}
insertShapeProfile(n, i);
}
for (auto b : n->blocks()) {
instrumentBlock(b);
}
}
}
std::unique_ptr<ProfilingRecord> ProfilingRecord::instrumentGraph(
const std::shared_ptr<Graph>& graph) {
auto new_g = graph->copy();
auto pr = std::unique_ptr<ProfilingRecord>(new ProfilingRecord(new_g));
auto raw_pr = pr.get();
unprofileGraphInputs(new_g);
unprofileBlock(new_g->block());
pr->instrumentBlock(new_g->block());
for (auto i : new_g->return_node()->inputs()) {
if (i->type()->isSubtypeOf(TensorType::get())) {
pr->insertShapeProfile(new_g->return_node(), i);
}
}
std::function<void(Stack&)> counter = [raw_pr](Stack&) {
std::lock_guard<std::mutex> lock(raw_pr->mutex_);
if (raw_pr->profiling_count_ > 0)
{
raw_pr->profiling_count_--;
}
};
auto pop = pr->createProfileNode(counter, {});
new_g->appendNode(pop);
return pr;
}
} // namespace jit
} // namespace torch