|
5 | 5 | #include "mlx/core/moe_stream_op.h" |
6 | 6 | #include <iostream> |
7 | 7 | #include <chrono> |
| 8 | +#include <atomic> |
8 | 9 | #include "mlx/primitives.h" |
9 | 10 | #include "mlx/backend/metal/device.h" |
10 | 11 | #include "mlx/backend/metal/utils.h" |
11 | | -#include <iostream> |
| 12 | + |
| 13 | +// Static SSD metric trackers for aggregate logging |
| 14 | +static std::atomic<size_t> g_total_bytes_read{0}; |
| 15 | +static std::atomic<uint64_t> g_total_read_ns{0}; |
| 16 | +static std::atomic<size_t> g_read_count{0}; |
| 17 | +static std::atomic<uint64_t> g_last_log_ns{0}; |
12 | 18 |
|
13 | 19 | namespace mlx::core { |
14 | 20 |
|
@@ -68,8 +74,33 @@ class StreamedGatherMM : public Primitive { |
68 | 74 | auto start_read = std::chrono::high_resolution_clock::now(); |
69 | 75 | streamer_->load_sync(block_offset, matrix_bytes, w.data<void>()); |
70 | 76 | auto end_read = std::chrono::high_resolution_clock::now(); |
71 | | - std::chrono::duration<double, std::milli> read_ms = end_read - start_read; |
72 | | - std::cout << "[SSD] Read " << (matrix_bytes / 1024.0 / 1024.0) << " MB in " << read_ms.count() << " ms\n"; |
| 77 | + |
| 78 | + // ───────────────────────────────────────────────────────────────────── |
| 79 | + // AGGREGATE LOGGING — 1-second metric intervals |
| 80 | + // ───────────────────────────────────────────────────────────────────── |
| 81 | + g_total_bytes_read += matrix_bytes; |
| 82 | + g_total_read_ns += std::chrono::duration_cast<std::chrono::nanoseconds>(end_read - start_read).count(); |
| 83 | + g_read_count++; |
| 84 | + |
| 85 | + auto now_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now().time_since_epoch()).count(); |
| 86 | + uint64_t last = g_last_log_ns.load(); |
| 87 | + |
| 88 | + // Output aggregated metrics once per second (1,000,000,000 ns) |
| 89 | + if (now_ns - last >= 1000000000ULL) { |
| 90 | + if (g_last_log_ns.compare_exchange_strong(last, now_ns)) { |
| 91 | + size_t count = g_read_count.exchange(0); |
| 92 | + size_t bytes = g_total_bytes_read.exchange(0); |
| 93 | + uint64_t ns_time = g_total_read_ns.exchange(0); |
| 94 | + |
| 95 | + if (count > 0) { |
| 96 | + double avg_ms = (ns_time / 1000000.0) / count; |
| 97 | + double mb = bytes / (1024.0 * 1024.0); |
| 98 | + std::cout << "[⚡️ SSD Stream] " << mb << " MB/s over " |
| 99 | + << count << " chunks | Avg latency per chunk: " |
| 100 | + << avg_ms << " ms\n"; |
| 101 | + } |
| 102 | + } |
| 103 | + } |
73 | 104 |
|
74 | 105 | auto& d = metal::device(mlx::core::Device::gpu); |
75 | 106 |
|
|
0 commit comments