| 360 | } |
| 361 | |
| 362 | string TensorSizeHistogram::ToString() const { |
| 363 | string r; |
| 364 | char buf[200]; |
| 365 | snprintf(buf, sizeof(buf), "Count: %lld, Average: ", num_elem_); |
| 366 | r.append(buf); |
| 367 | r.append(strings::HumanReadableNumBytes(Average())); |
| 368 | r.append(", Min: "); |
| 369 | r.append(strings::HumanReadableNumBytes(min_)); |
| 370 | r.append(", Max: "); |
| 371 | r.append(strings::HumanReadableNumBytes(max_)); |
| 372 | r.append("\n------------------------------------------------------\n"); |
| 373 | const double mult = num_elem_ > 0 ? 100.0 / num_elem_ : 0.0; |
| 374 | uint64 cumul_sum = 0; |
| 375 | |
| 376 | const int size_string_width = 12; |
| 377 | for (int i = 0; i < buckets_.size(); i++) { |
| 378 | if (buckets_[i] == 0) continue; |
| 379 | cumul_sum += buckets_[i]; |
| 380 | r.append("[ "); |
| 381 | if (i == 0) { |
| 382 | r.append(size_string_width - 2, ' '); |
| 383 | r.append("0B"); |
| 384 | } else { |
| 385 | uint64 left = 1ULL << (i - 1); |
| 386 | const auto left_string = strings::HumanReadableNumBytes(left); |
| 387 | r.append(size_string_width - left_string.size(), ' '); |
| 388 | r.append(left_string); |
| 389 | } |
| 390 | r.append(", "); |
| 391 | uint64 right = 1ULL << i; |
| 392 | const auto right_string = strings::HumanReadableNumBytes(right); |
| 393 | r.append(size_string_width - right_string.size(), ' '); |
| 394 | r.append(right_string); |
| 395 | snprintf(buf, sizeof(buf), ") %7lld %7.3f%% %7.3f%% ", |
| 396 | buckets_[i], // count |
| 397 | mult * buckets_[i], // percentage |
| 398 | mult * cumul_sum); // cum percentage |
| 399 | r.append(buf); |
| 400 | |
| 401 | // Add hash marks based on percentage; 40 marks for 100%. |
| 402 | auto marks = static_cast<int>( |
| 403 | (static_cast<double>(40 * buckets_[i] + (num_elem_ >> 1)) / num_elem_)); |
| 404 | r.append(marks, '#'); |
| 405 | r.push_back('\n'); |
| 406 | } |
| 407 | return r; |
| 408 | } |
| 409 | |
| 410 | const int TensorSizeHistogram::Index(const uint64 value) const { |
| 411 | // Log2Floor64 returns -1 for 0, 0 for 1, 1 for 2-3, 2 for 4-7, ... |