brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
0 / 136
0.0%
Branches:
0 / 168
0.0%
Functions:
0 / 8
0.0%

src/profile_report.cpp
Line Branch Exec Source
1 #include "profile_report.h"
2
3 #include <algorithm>
4 #include <cstdarg>
5 #include <cstdio>
6 #include <unordered_map>
7
8 namespace brotensor::detail::prof {
9
10 namespace {
11
12 // A gap shorter than this is launch latency: the GPU finished before the host
13 // issued the next op. Longer ones are host work, syncs and waits.
14 constexpr double kShortGapMs = 0.020;
15
16 ✗ struct Group {
17 std::string name;
18 ✗ std::uint64_t calls = 0;
19 ✗ double ms = 0.0;
20 ✗ double idle_before = 0.0;
21 ✗ double work_ms = 0.0; // time of the calls whose work is known
22 ✗ double flops = 0.0;
23 ✗ double bytes = 0.0;
24 ✗ bool tc = false;
25 ✗ double fill_ms = 0.0; // sum of fill x ms over calls with a known fill
26 ✗ double fill_w = 0.0;
27 ✗ struct Shape {
28 ✗ std::uint64_t calls = 0;
29 ✗ double ms = 0.0;
30 ✗ double flops = 0.0;
31 ✗ double bytes = 0.0;
32 };
33 std::unordered_map<std::string, Shape> shapes; // calls whose op named its shape
34 };
35
36 } // namespace
37
38 ✗ void appendf(std::string& out, const char* fmt, ...) {
39 char buf[512];
40 va_list ap;
41 ✗ va_start(ap, fmt);
42 ✗ const int n = std::vsnprintf(buf, sizeof(buf), fmt, ap);
43 ✗ va_end(ap);
44
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (n > 0) out.append(buf, static_cast<std::size_t>(std::min(n, static_cast<int>(sizeof(buf)) - 1)));
45 ✗ }
46
47 ✗ std::string render(const std::string& head, const std::string& host, std::vector<Activity> acts,
48 const Peaks& pk, int iterations) {
49
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ const double it = iterations > 0 ? static_cast<double>(iterations) : 1.0;
50 ✗ std::string out = head;
51
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (acts.empty()) {
52
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ out += " no GPU work was recorded\n";
53 ✗ return out;
54 }
55
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ std::sort(acts.begin(), acts.end(), [](const Activity& a, const Activity& b) { return a.start_ns < b.start_ns; });
56
57 ✗ std::unordered_map<std::string, Group> groups;
58 ✗ const double ns_ms = 1e-6;
59 ✗ std::uint64_t cur_end = acts.front().start_ns, last = acts.front().end_ns;
60 ✗ double idle_short = 0.0, idle_long = 0.0;
61 ✗ std::uint64_t n_short = 0, n_long = 0;
62
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (const Activity& a : acts) {
63
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Group& g = groups[a.name];
64
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (g.name.empty()) g.name = a.name;
65
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (a.start_ns > cur_end) {
66 ✗ const double gap = static_cast<double>(a.start_ns - cur_end) * ns_ms;
67 ✗ g.idle_before += gap;
68
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (gap < kShortGapMs) { idle_short += gap; ++n_short; }
69 ✗ else { idle_long += gap; ++n_long; }
70 ✗ }
71
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ cur_end = std::max(cur_end, a.end_ns);
72
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ last = std::max(last, a.end_ns);
73
74
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ const double ms = a.end_ns > a.start_ns ? static_cast<double>(a.end_ns - a.start_ns) * ns_ms : 0.0;
75 ✗ ++g.calls;
76 ✗ g.ms += ms;
77
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (a.has_work) {
78 ✗ g.work_ms += ms;
79 ✗ g.flops += a.flops;
80 ✗ g.bytes += a.bytes;
81
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ g.tc = g.tc || a.tc;
82
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (!a.shape.empty()) {
83
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Group::Shape& sh = g.shapes[a.shape];
84 ✗ ++sh.calls;
85 ✗ sh.ms += ms;
86 ✗ sh.flops += a.flops;
87 ✗ sh.bytes += a.bytes;
88 ✗ }
89 ✗ }
90
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (a.fill >= 0.0) {
91 ✗ g.fill_ms += a.fill * ms;
92 ✗ g.fill_w += ms;
93 ✗ }
94 }
95 ✗ const double span = static_cast<double>(last - acts.front().start_ns) * ns_ms;
96 ✗ const double idle = idle_short + idle_long;
97 ✗ const double busy = span - idle;
98
99
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ appendf(out, " GPU span %.3f ms: busy %.3f ms (%.0f%%), idle %.3f ms: %.3f ms in %.0f gaps < %.0f us, "
100 "%.3f ms in %.0f longer\n",
101
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ span / it, busy / it, span > 0 ? 100.0 * busy / span : 0.0, idle / it, idle_short / it,
102 ✗ static_cast<double>(n_short) / it, kShortGapMs * 1e3, idle_long / it, static_cast<double>(n_long) / it);
103
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ out += host;
104
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ out += "\n";
105
106 ✗ std::vector<const Group*> sorted;
107
0/10
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ for (const auto& kv : groups) sorted.push_back(&kv.second);
108
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ std::sort(sorted.begin(), sorted.end(), [](const Group* a, const Group* b) { return a->ms > b->ms; });
109
110
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ appendf(out, " %-44s %8s %8s %6s %9s %8s %5s %8s %5s %7s %5s\n", "op", "calls", "ms", "busy", "idle-pre",
111 "avg-us", "fill", "TFLOP/s", "%pk", "GB/s", "%pk");
112 ✗ constexpr std::size_t kRows = 30;
113 ✗ Group rest;
114
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (std::size_t i = 0; i < sorted.size(); ++i) {
115 ✗ const Group& g = *sorted[i];
116
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (i >= kRows) {
117 ✗ rest.calls += g.calls;
118 ✗ rest.ms += g.ms;
119 ✗ rest.idle_before += g.idle_before;
120 ✗ continue;
121 }
122
0/12
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ const std::string nm = g.name.size() > 44 ? g.name.substr(0, 41) + "..." : g.name;
123
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ appendf(out, " %-44s %8.1f %8.3f %5.1f%% %9.3f %8.1f", nm.c_str(), static_cast<double>(g.calls) / it,
124
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ g.ms / it, busy > 0 ? 100.0 * g.ms / busy : 0.0, g.idle_before / it,
125
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ g.calls ? g.ms / static_cast<double>(g.calls) * 1e3 : 0.0);
126
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (g.fill_w > 0) appendf(out, " %4.0f%%", 100.0 * g.fill_ms / g.fill_w);
127
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ else appendf(out, " %5s", "-");
128
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (g.work_ms > 0 && g.flops > 0) {
129 ✗ const double fl = g.flops / (g.work_ms * 1e-3);
130
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ const double p = g.tc ? pk.tc : pk.fp32;
131
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (p > 0) appendf(out, " %8.1f %4.0f%%", fl * 1e-12, 100.0 * fl / p);
132
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ else appendf(out, " %8.1f %5s", fl * 1e-12, "-");
133 ✗ } else {
134
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ appendf(out, " %8s %5s", "-", "-");
135 }
136
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (g.work_ms > 0 && g.bytes > 0) {
137 ✗ const double bw = g.bytes / (g.work_ms * 1e-3);
138
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (pk.bw > 0) appendf(out, " %7.0f %4.0f%%", bw * 1e-9, 100.0 * bw / pk.bw);
139
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ else appendf(out, " %7.0f %5s", bw * 1e-9, "-");
140 ✗ } else {
141
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ appendf(out, " %7s %5s", "-", "-");
142 }
143
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ out += "\n";
144 ✗ }
145
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (rest.calls) {
146 char nm[64];
147 ✗ std::snprintf(nm, sizeof(nm), "(%zu more ops)", sorted.size() - kRows);
148
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ appendf(out, " %-44s %8.1f %8.3f %5.1f%% %9.3f\n", nm, static_cast<double>(rest.calls) / it, rest.ms / it,
149
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ busy > 0 ? 100.0 * rest.ms / busy : 0.0, rest.idle_before / it);
150 ✗ }
151
152 // The heaviest shapes of the ops that name them.
153 ✗ constexpr std::size_t kShapeOps = 8, kShapesPerOp = 6;
154 ✗ std::size_t shown = 0;
155
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (const Group* gp : sorted) {
156
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (shown == kShapeOps) break;
157
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (gp->shapes.empty()) continue;
158
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (shown++ == 0) out += "\n by shape (heaviest ops that report one)\n";
159
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ appendf(out, " %s\n", gp->name.c_str());
160 ✗ std::vector<std::pair<const std::string*, const Group::Shape*>> shs;
161
0/10
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ for (const auto& kv : gp->shapes) shs.emplace_back(&kv.first, &kv.second);
162
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ std::sort(shs.begin(), shs.end(), [](const auto& a, const auto& b) { return a.second->ms > b.second->ms; });
163
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ for (std::size_t j = 0; j < shs.size() && j < kShapesPerOp; ++j) {
164 ✗ const Group::Shape& sh = *shs[j].second;
165
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ const double p = gp->tc ? pk.tc : pk.fp32;
166
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ const double fl = sh.ms > 0 ? sh.flops / (sh.ms * 1e-3) : 0.0;
167
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ const double bw = sh.ms > 0 ? sh.bytes / (sh.ms * 1e-3) : 0.0;
168
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ appendf(out, " %-40s %8.1f %8.3f %8.1f us %8.1f TFLOP/s %4.0f%% %7.0f GB/s %4.0f%%\n",
169 ✗ shs[j].first->c_str(), static_cast<double>(sh.calls) / it, sh.ms / it,
170
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ sh.ms / static_cast<double>(sh.calls) * 1e3, fl * 1e-12, p > 0 ? 100.0 * fl / p : 0.0,
171
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ bw * 1e-9, pk.bw > 0 ? 100.0 * bw / pk.bw : 0.0);
172 ✗ }
173
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (shs.size() > kShapesPerOp) appendf(out, " (%zu more shapes)\n", shs.size() - kShapesPerOp);
174 ✗ }
175
176
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ out += "\n busy: share of GPU busy time; idle-pre: GPU idle just before the op ran; fill: blocks over the\n"
177 " GPU's resident-block slots, per wave (100% fills every SM every wave); %pk: of the peak above\n"
178 " (tensor peak for ops on the tensor cores), for ops whose work is known; GB/s counts the bytes\n"
179 " the op must move, so past 100% they came from L2.\n";
180 ✗ return out;
181 ✗ }
182
183 } // namespace brotensor::detail::prof
184