-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathprofiler.cc
More file actions
61 lines (56 loc) · 2.35 KB
/
profiler.cc
File metadata and controls
61 lines (56 loc) · 2.35 KB
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
#include "profiler.h"
#include <NvInfer.h>
#include <algorithm>
#include <iomanip>
#include <string>
void Profiler::reportLayerTime(const char* layerName, float ms) noexcept {
mProfile[layerName].count++;
mProfile[layerName].time += ms;
if (std::find(mLayerNames.begin(), mLayerNames.end(), layerName) == mLayerNames.end()) {
mLayerNames.emplace_back(layerName);
}
}
Profiler::Profiler(const char* name, const std::vector<Profiler>& srcProfilers) : mName(name) {
for (const auto& srcProfiler : srcProfilers) {
for (const auto& rec : srcProfiler.mProfile) {
auto it = mProfile.find(rec.first);
if (it == mProfile.end()) {
mProfile.insert(rec);
} else {
it->second.time += rec.second.time;
it->second.count += rec.second.count;
}
}
}
}
std::ostream& operator<<(std::ostream& out, const Profiler& value) {
out << "========== " << value.mName << " ==========\n";
float totalTime = 0;
std::string layerNameStr = "TensorRT layer name";
int maxLayerNameLength = std::max(static_cast<int>(layerNameStr.size()), 70);
for (const auto& elem : value.mProfile) {
totalTime += elem.second.time;
maxLayerNameLength = std::max(maxLayerNameLength, static_cast<int>(elem.first.size()));
}
auto old_settings = out.flags();
auto old_precision = out.precision();
// Output header
{
out << std::setfill(' ') << std::setw(maxLayerNameLength) << layerNameStr << " ";
out << std::setw(12) << "Runtime, " << "%" << " ";
out << std::setw(12) << "Invocations" << " ";
out << std::setw(12) << "Runtime, ms\n";
}
for (size_t i = 0; i < value.mLayerNames.size(); i++) {
const std::string layerName = value.mLayerNames[i];
auto elem = value.mProfile.at(layerName);
out << std::setw(maxLayerNameLength) << layerName << " ";
out << std::setw(12) << std::fixed << std::setprecision(1) << (elem.time * 100.0F / totalTime) << "%" << " ";
out << std::setw(12) << elem.count << " ";
out << std::setw(12) << std::fixed << std::setprecision(2) << elem.time << "\n";
}
out.flags(old_settings);
out.precision(old_precision);
out << "========== " << value.mName << " total runtime = " << totalTime << " ms ==========\n";
return out;
}