forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script_profile.h
101 lines (83 loc) · 2.47 KB
/
script_profile.h
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
#pragma once
#include <chrono>
#include <map>
#include <string>
#include <ATen/core/ivalue.h>
#include <c10/macros/Macros.h>
#include <torch/csrc/jit/frontend/source_ref.h>
#include <torch/csrc/jit/ir/ir.h>
namespace torch {
namespace jit {
namespace profiling {
struct Datapoint {
using Timepoint = std::chrono::time_point<std::chrono::steady_clock>;
SourceRange sourceRange;
Timepoint start;
Timepoint end;
explicit Datapoint(SourceRange sr)
: sourceRange(std::move(sr)), start(std::chrono::steady_clock::now()) {}
};
class TORCH_API InstructionSpan {
public:
explicit InstructionSpan(Node&);
~InstructionSpan();
InstructionSpan(InstructionSpan&&) = delete;
InstructionSpan& operator=(InstructionSpan&&) = delete;
private:
std::unique_ptr<Datapoint> datapoint_;
};
} // namespace profiling
struct TORCH_API InstructionStats : public CustomClassHolder {
int64_t count{0};
std::chrono::nanoseconds duration{0};
};
class TORCH_API SourceStats : public CustomClassHolder {
public:
using LineMap = c10::Dict<int64_t, c10::intrusive_ptr<InstructionStats>>;
SourceStats(SourceRef source, LineMap lineMap)
: source_(std::move(source)), lineMap_(std::move(lineMap)) {}
const SourceRef& getSourceRef() const {
return source_;
}
const LineMap& getLineMap() const {
return lineMap_;
}
private:
SourceRef source_;
LineMap lineMap_;
};
/**
* ScriptProfile is an underlying C++ implementation for TorchScript profiling.
* The profiling section is specified by calling enable() and disable():
*
* ...
* scriptProfile.enable();
* ...
* (scripts)
* ...
* scriptProfile.disable();
* ...
*
* To retrieve collected runtime data, users may call dumpStats() and do
* arbitrary filtering on the data they want. Note that dumpStats() should
* not be called inside a profiling section.
* In general, stats are aggregated per source function body, and then by line
* number.
*/
class TORCH_API ScriptProfile : public CustomClassHolder {
// Aggregates datapoints by function source id, then by line number.
using LineMap = std::map<int64_t, InstructionStats>;
using SourceMap = std::map<SourceRef, LineMap, std::less<>>;
public:
void enable();
void disable();
const SourceMap& dumpStats();
void addDatapoint(std::shared_ptr<profiling::Datapoint>);
~ScriptProfile() override;
private:
bool enabled_{false};
std::vector<std::shared_ptr<profiling::Datapoint>> datapoints_;
SourceMap sourceMap_;
};
} // namespace jit
} // namespace torch