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 | ✗ | 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 | ✗ | const double it = iterations > 0 ? static_cast<double>(iterations) : 1.0; | |
| 50 | ✗ | std::string out = head; | |
| 51 | ✗ | if (acts.empty()) { | |
| 52 | ✗ | out += " no GPU work was recorded\n"; | |
| 53 | ✗ | return out; | |
| 54 | } | ||
| 55 | ✗ | 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 | ✗ | for (const Activity& a : acts) { | |
| 63 | ✗ | Group& g = groups[a.name]; | |
| 64 | ✗ | if (g.name.empty()) g.name = a.name; | |
| 65 | ✗ | 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 | ✗ | if (gap < kShortGapMs) { idle_short += gap; ++n_short; } | |
| 69 | ✗ | else { idle_long += gap; ++n_long; } | |
| 70 | ✗ | } | |
| 71 | ✗ | cur_end = std::max(cur_end, a.end_ns); | |
| 72 | ✗ | last = std::max(last, a.end_ns); | |
| 73 | |||
| 74 | ✗ | 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 | ✗ | if (a.has_work) { | |
| 78 | ✗ | g.work_ms += ms; | |
| 79 | ✗ | g.flops += a.flops; | |
| 80 | ✗ | g.bytes += a.bytes; | |
| 81 | ✗ | g.tc = g.tc || a.tc; | |
| 82 | ✗ | if (!a.shape.empty()) { | |
| 83 | ✗ | 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 | ✗ | 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 | ✗ | 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 | ✗ | 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 | ✗ | out += host; | |
| 104 | ✗ | out += "\n"; | |
| 105 | |||
| 106 | ✗ | std::vector<const Group*> sorted; | |
| 107 | ✗ | for (const auto& kv : groups) sorted.push_back(&kv.second); | |
| 108 | ✗ | std::sort(sorted.begin(), sorted.end(), [](const Group* a, const Group* b) { return a->ms > b->ms; }); | |
| 109 | |||
| 110 | ✗ | 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 | ✗ | for (std::size_t i = 0; i < sorted.size(); ++i) { | |
| 115 | ✗ | const Group& g = *sorted[i]; | |
| 116 | ✗ | if (i >= kRows) { | |
| 117 | ✗ | rest.calls += g.calls; | |
| 118 | ✗ | rest.ms += g.ms; | |
| 119 | ✗ | rest.idle_before += g.idle_before; | |
| 120 | ✗ | continue; | |
| 121 | } | ||
| 122 | ✗ | const std::string nm = g.name.size() > 44 ? g.name.substr(0, 41) + "..." : g.name; | |
| 123 | ✗ | appendf(out, " %-44s %8.1f %8.3f %5.1f%% %9.3f %8.1f", nm.c_str(), static_cast<double>(g.calls) / it, | |
| 124 | ✗ | g.ms / it, busy > 0 ? 100.0 * g.ms / busy : 0.0, g.idle_before / it, | |
| 125 | ✗ | g.calls ? g.ms / static_cast<double>(g.calls) * 1e3 : 0.0); | |
| 126 | ✗ | if (g.fill_w > 0) appendf(out, " %4.0f%%", 100.0 * g.fill_ms / g.fill_w); | |
| 127 | ✗ | else appendf(out, " %5s", "-"); | |
| 128 | ✗ | if (g.work_ms > 0 && g.flops > 0) { | |
| 129 | ✗ | const double fl = g.flops / (g.work_ms * 1e-3); | |
| 130 | ✗ | const double p = g.tc ? pk.tc : pk.fp32; | |
| 131 | ✗ | if (p > 0) appendf(out, " %8.1f %4.0f%%", fl * 1e-12, 100.0 * fl / p); | |
| 132 | ✗ | else appendf(out, " %8.1f %5s", fl * 1e-12, "-"); | |
| 133 | ✗ | } else { | |
| 134 | ✗ | appendf(out, " %8s %5s", "-", "-"); | |
| 135 | } | ||
| 136 | ✗ | if (g.work_ms > 0 && g.bytes > 0) { | |
| 137 | ✗ | const double bw = g.bytes / (g.work_ms * 1e-3); | |
| 138 | ✗ | if (pk.bw > 0) appendf(out, " %7.0f %4.0f%%", bw * 1e-9, 100.0 * bw / pk.bw); | |
| 139 | ✗ | else appendf(out, " %7.0f %5s", bw * 1e-9, "-"); | |
| 140 | ✗ | } else { | |
| 141 | ✗ | appendf(out, " %7s %5s", "-", "-"); | |
| 142 | } | ||
| 143 | ✗ | out += "\n"; | |
| 144 | ✗ | } | |
| 145 | ✗ | if (rest.calls) { | |
| 146 | char nm[64]; | ||
| 147 | ✗ | std::snprintf(nm, sizeof(nm), "(%zu more ops)", sorted.size() - kRows); | |
| 148 | ✗ | appendf(out, " %-44s %8.1f %8.3f %5.1f%% %9.3f\n", nm, static_cast<double>(rest.calls) / it, rest.ms / it, | |
| 149 | ✗ | 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 | ✗ | for (const Group* gp : sorted) { | |
| 156 | ✗ | if (shown == kShapeOps) break; | |
| 157 | ✗ | if (gp->shapes.empty()) continue; | |
| 158 | ✗ | if (shown++ == 0) out += "\n by shape (heaviest ops that report one)\n"; | |
| 159 | ✗ | appendf(out, " %s\n", gp->name.c_str()); | |
| 160 | ✗ | std::vector<std::pair<const std::string*, const Group::Shape*>> shs; | |
| 161 | ✗ | for (const auto& kv : gp->shapes) shs.emplace_back(&kv.first, &kv.second); | |
| 162 | ✗ | std::sort(shs.begin(), shs.end(), [](const auto& a, const auto& b) { return a.second->ms > b.second->ms; }); | |
| 163 | ✗ | for (std::size_t j = 0; j < shs.size() && j < kShapesPerOp; ++j) { | |
| 164 | ✗ | const Group::Shape& sh = *shs[j].second; | |
| 165 | ✗ | const double p = gp->tc ? pk.tc : pk.fp32; | |
| 166 | ✗ | const double fl = sh.ms > 0 ? sh.flops / (sh.ms * 1e-3) : 0.0; | |
| 167 | ✗ | const double bw = sh.ms > 0 ? sh.bytes / (sh.ms * 1e-3) : 0.0; | |
| 168 | ✗ | 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 | ✗ | sh.ms / static_cast<double>(sh.calls) * 1e3, fl * 1e-12, p > 0 ? 100.0 * fl / p : 0.0, | |
| 171 | ✗ | bw * 1e-9, pk.bw > 0 ? 100.0 * bw / pk.bw : 0.0); | |
| 172 | ✗ | } | |
| 173 | ✗ | if (shs.size() > kShapesPerOp) appendf(out, " (%zu more shapes)\n", shs.size() - kShapesPerOp); | |
| 174 | ✗ | } | |
| 175 | |||
| 176 | ✗ | 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 |