in src/runtime/profiling.cc [472:653]
String ReportNode::AsTable(bool sort, bool aggregate, bool compute_col_sums) const {
// aggregate calls by op hash (or op name if hash is not set) + argument shapes
std::vector<Map<String, ffi::Any>> aggregated_calls;
if (aggregate) {
std::unordered_map<std::string, std::vector<size_t>> aggregates;
for (size_t i = 0; i < calls.size(); i++) {
auto& frame = calls[i];
auto it = frame.find("Hash");
std::string name = Downcast<String>(frame["Name"]);
if (it != frame.end()) {
name = Downcast<String>((*it).second);
}
if (frame.find("Argument Shapes") != frame.end()) {
name += Downcast<String>(frame["Argument Shapes"]);
}
if (frame.find("Device") != frame.end()) {
name += Downcast<String>(frame["Device"]);
}
if (aggregates.find(name) == aggregates.end()) {
aggregates[name] = {i};
} else {
aggregates[name].push_back(i);
}
}
for (const auto& p : aggregates) {
std::unordered_map<String, Any> aggregated;
std::unordered_set<std::string> metrics;
for (auto& call : calls) {
for (auto& metric : call) {
metrics.insert(metric.first);
}
}
for (const std::string& metric : metrics) {
std::vector<ObjectRef> per_call;
for (auto i : p.second) {
auto& call = calls[i];
auto it = std::find_if(call.begin(), call.end(),
[&metric](const std::pair<String, ffi::Any>& call_metric) {
return std::string(call_metric.first) == metric;
});
if (it != call.end()) {
per_call.push_back((*it).second.cast<ObjectRef>());
}
}
if (per_call.size() > 0) {
aggregated[metric] = AggregateMetric(per_call);
}
}
aggregated_calls.push_back(aggregated);
}
} else {
for (auto call : calls) {
aggregated_calls.push_back(call);
}
}
// sort rows by duration
if (sort) {
std::sort(aggregated_calls.begin(), aggregated_calls.end(),
[&](const Map<String, ffi::Any>& a, const Map<String, ffi::Any>& b) {
return a.at("Duration (us)").as<DurationNode>()->microseconds >
b.at("Duration (us)").as<DurationNode>()->microseconds;
});
}
// compute columnwise sums
if (compute_col_sums) {
std::unordered_map<String, ffi::Any> col_sums;
for (auto call : aggregated_calls) {
for (auto p : call) {
if (p.second.as<CountNode>()) {
int64_t val = p.second.as<CountNode>()->value;
auto it = col_sums.find(p.first);
if (it != col_sums.end()) {
val += it->second.as<CountNode>()->value;
}
col_sums[p.first] = ObjectRef(make_object<CountNode>(val));
} else if (p.second.as<DurationNode>()) {
double val = p.second.as<DurationNode>()->microseconds;
auto it = col_sums.find(p.first);
if (it != col_sums.end()) {
val += it->second.as<DurationNode>()->microseconds;
}
col_sums[p.first] = ObjectRef(make_object<DurationNode>(val));
} else if (p.second.as<PercentNode>()) {
double val = p.second.as<PercentNode>()->percent;
auto it = col_sums.find(p.first);
if (it != col_sums.end()) {
val += it->second.as<PercentNode>()->percent;
}
col_sums[p.first] = ObjectRef(make_object<PercentNode>(val));
} else if (p.second.as<RatioNode>()) {
// It does not make sense to sum ratios
}
}
}
col_sums["Name"] = String("Sum");
aggregated_calls.push_back({{String("Name"), String("----------")}}); // separator
aggregated_calls.push_back(col_sums);
}
// per-device metrics
for (auto p : device_metrics) {
Map<String, ffi::Any> metrics = p.second;
metrics.Set("Name", String("Total"));
aggregated_calls.push_back(metrics);
}
// Table formatting
std::set<std::string> unique_headers;
for (auto row : aggregated_calls) {
for (auto p : row) {
unique_headers.insert(p.first);
}
}
// always include these headers in this order
std::vector<std::string> headers = {"Name", "Duration (us)", "Percent",
"Device", "Count", "Argument Shapes"};
for (auto header : unique_headers) {
if (std::find(headers.begin(), headers.end(), header) == headers.end()) {
headers.push_back(header);
}
}
// Switch layout from row major to column major so we can easily compute column widths.
std::vector<std::vector<std::string>> cols;
for (auto header : headers) {
cols.push_back({header});
}
for (auto row : aggregated_calls) {
for (size_t i = 0; i < headers.size(); i++) {
auto it = row.find(headers[i]);
if (it == row.end()) {
// fill empty data with empty strings
cols[i].push_back("");
} else {
cols[i].push_back(print_metric((*it).second.cast<ObjectRef>()));
}
}
}
std::vector<size_t> widths;
for (auto v : cols) {
size_t width = 0;
for (auto x : v) {
width = std::max(width, x.size());
}
widths.push_back(width);
}
size_t length = 0;
for (auto v : cols) {
length = std::max(length, v.size());
}
std::stringstream s;
for (size_t row = 0; row < length; row++) {
for (size_t col = 0; col < cols.size(); col++) {
// left align first column
if (col == 0) {
s << std::left;
} else {
s << std::right;
}
if (row < cols[col].size()) {
s << std::setw(widths[col]) << cols[col][row] << " ";
} else {
s << std::setw(widths[col]) << ""
<< " ";
}
}
s << std::endl;
}
// Add configuration information. It will not be aligned with the columns.
s << std::endl << "Configuration" << std::endl << "-------------" << std::endl;
for (auto kv : configuration) {
s << kv.first << ": " << print_metric(kv.second.cast<ObjectRef>()) << std::endl;
}
return s.str();
}