Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-sycl/memtrace.cpp # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-sycl / memtrace.cpp` 193 lines ยท 6461 bytes Raw Blame History 1 #include "memtrace.hpp" 2 3 #include "common.hpp" 4 #include "ggml-impl.h" 5 6 #include 7 #include 8 #include 9 10 constexpr size_t MIB = 1024 * 1024; 11 12 static const char * mem_type_name(ggml_sycl_mem_type type) { 13 switch (type) { 14 case GGML_SYCL_MEM_BUFFER: return "buffer"; 15 case GGML_SYCL_MEM_POOL_LEG: return "pool_leg"; 16 case GGML_SYCL_MEM_POOL_VMM: return "pool_vmm"; 17 case GGML_SYCL_MEM_ASYNC: return "async"; 18 case GGML_SYCL_MEM_DIRECT: return "direct"; 19 default: GGML_ABORT("[%s] The type value %d is not supported\n", __func__, (int) type); 20 } 21 } 22 23 struct mem_tracker { 24 std::mutex mutex; 25 std::unordered_map> live_by_ptr; 26 size_t live[GGML_SYCL_MEM_TYPE_COUNT] = {}; 27 size_t peak[GGML_SYCL_MEM_TYPE_COUNT] = {}; 28 size_t total_live = 0; 29 size_t total_peak = 0; 30 size_t last_logged_peak = 0; 31 }; 32 33 static mem_tracker & get_tracker() { 34 static mem_tracker t; 35 return t; 36 } 37 38 static size_t step_bytes() { 39 const int mib = g_ggml_sycl_memtrace_step > 0 ? g_ggml_sycl_memtrace_step : 64; 40 return (size_t) mib * MIB; 41 } 42 43 static void report_sites_locked() { 44 mem_tracker & t = get_tracker(); 45 for (int i = 0; i < GGML_SYCL_MEM_TYPE_COUNT; i++) { 46 if (t.peak[i] == 0) { 47 continue; 48 } 49 GGML_LOG_INFO(GGML_SYCL_MEMTRACE_TAG " %-9s allocated %5zu MiB, peak %5zu MiB\n", 50 mem_type_name((ggml_sycl_mem_type) i), t.live[i] / MIB, t.peak[i] / MIB); 51 } 52 } 53 54 static void report_locked(const char * tag) { 55 mem_tracker & t = get_tracker(); 56 57 const size_t allocated = t.total_live / MIB; 58 const size_t buffers = t.live[GGML_SYCL_MEM_BUFFER] / MIB; 59 60 GGML_LOG_INFO(GGML_SYCL_MEMTRACE_TAG " %s: allocated %5zu MiB (buffers %5zu + scratch %5zu)," 61 " peak %5zu MiB\n", 62 tag, allocated, buffers, allocated - buffers, t.total_peak / MIB); 63 report_sites_locked(); 64 } 65 66 static void log_event_locked(const char * op, ggml_sycl_mem_type type, const void * ptr, size_t bytes) { 67 GGML_LOG_INFO(GGML_SYCL_MEMTRACE_TAG " allocated %5zu MiB %-5s %-9s %9.3f MiB ptr=%p\n", 68 get_tracker().total_live / MIB, op, mem_type_name(type), 69 (double) bytes / MIB, ptr); 70 } 71 72 bool ggml_sycl_memtrace_enabled() { 73 return g_ggml_sycl_memtrace > 0; 74 } 75 76 void ggml_sycl_memtrace_add(ggml_sycl_mem_type type, const void * ptr, size_t bytes) { 77 if (!ggml_sycl_memtrace_enabled()) { 78 return; 79 } 80 GGML_ASSERT(ptr != nullptr); 81 GGML_ASSERT(bytes != 0); 82 83 mem_tracker & t = get_tracker(); 84 std::lock_guard lock(t.mutex); 85 86 auto it = t.live_by_ptr.find(ptr); 87 if (it != t.live_by_ptr.end()) { 88 t.live[it->second.first] -= it->second.second; 89 t.total_live -= it->second.second; 90 } 91 92 t.live_by_ptr[ptr] = { type, bytes }; 93 t.live[type] += bytes; 94 t.total_live += bytes; 95 96 if (t.live[type] > t.peak[type]) { 97 t.peak[type] = t.live[type]; 98 } 99 if (t.total_live > t.total_peak) { 100 t.total_peak = t.total_live; 101 } 102 103 if (g_ggml_sycl_memtrace >= 2) { 104 log_event_locked("alloc", type, ptr, bytes); 105 } 106 107 static const size_t step = step_bytes(); 108 if (t.total_peak >= t.last_logged_peak + step) { 109 t.last_logged_peak = t.total_peak; 110 char tag[96]; 111 std::snprintf(tag, sizeof(tag), "peak grew (+%zu MiB from %s)", bytes / MIB, 112 mem_type_name(type)); 113 report_locked(tag); 114 } 115 } 116 117 void ggml_sycl_memtrace_del(const void * ptr) { 118 if (!ggml_sycl_memtrace_enabled() || ptr == nullptr) { 119 return; 120 } 121 mem_tracker & t = get_tracker(); 122 std::lock_guard lock(t.mutex); 123 124 auto it = t.live_by_ptr.find(ptr); 125 if (it == t.live_by_ptr.end()) { 126 return; 127 } 128 const ggml_sycl_mem_type type = it->second.first; 129 const size_t bytes = it->second.second; 130 t.live[type] -= bytes; 131 t.total_live -= bytes; 132 t.live_by_ptr.erase(it); 133 134 if (g_ggml_sycl_memtrace >= 2) { 135 log_event_locked("free", type, ptr, bytes); 136 } 137 } 138 139 void ggml_sycl_memtrace_fail(ggml_sycl_mem_type type, size_t bytes) { 140 GGML_LOG_ERROR(GGML_SYCL_MEMTRACE_TAG " alloc FAILED: %9.3f MiB %s\n", 141 (double) bytes / MIB, mem_type_name(type)); 142 if (!ggml_sycl_memtrace_enabled()) { 143 return; 144 } 145 mem_tracker & t = get_tracker(); 146 std::lock_guard lock(t.mutex); 147 report_locked("at allocation failure"); 148 } 149 150 void ggml_sycl_memtrace_report(const char * tag) { 151 if (!ggml_sycl_memtrace_enabled()) { 152 return; 153 } 154 mem_tracker & t = get_tracker(); 155 std::lock_guard lock(t.mutex); 156 report_locked(tag); 157 } 158 159 static bool device_memory_is_dedicated(int device) { 160 if (device < 0 || device >= ggml_sycl_info().device_count) { 161 return false; 162 } 163 const sycl_device_info & info = ggml_sycl_info().devices[device]; 164 return info.l0_device_type_valid && info.l0_discrete_gpu; 165 } 166 167 void ggml_sycl_memtrace_report_device(const char * tag, int device, size_t dev_free, size_t dev_total) { 168 if (!ggml_sycl_memtrace_enabled()) { 169 return; 170 } 171 mem_tracker & t = get_tracker(); 172 std::lock_guard lock(t.mutex); 173 174 const size_t in_use = dev_total > dev_free ? dev_total - dev_free : 0; 175 const size_t total = dev_total / MIB; 176 const size_t freed = dev_free / MIB; 177 const size_t allocated = t.total_live / MIB; 178 const size_t buffers = t.live[GGML_SYCL_MEM_BUFFER] / MIB; 179 const size_t peak = t.total_peak / MIB; 180 181 if (in_use >= t.total_live && device_memory_is_dedicated(device) && total >= freed + allocated) { 182 GGML_LOG_INFO(GGML_SYCL_MEMTRACE_TAG " %s: total %5zu MiB = free %5zu + allocated %5zu" 183 " (buffers %5zu + scratch %5zu) + other %5zu, peak %5zu MiB\n", 184 tag, total, freed, allocated, buffers, allocated - buffers, 185 total - freed - allocated, peak); 186 } else { 187 GGML_LOG_INFO(GGML_SYCL_MEMTRACE_TAG " %s: total %5zu MiB, free %5zu, in use %5zu;" 188 " allocated %5zu (buffers %5zu + scratch %5zu), peak %5zu MiB\n", 189 tag, total, freed, in_use / MIB, allocated, buffers, 190 allocated - buffers, peak); 191 } 192 report_sites_locked(); 193 }