Skip to content

Commit bc166ed

Browse files
committed
Log compilation memory usage for CID
1 parent 1570b97 commit bc166ed

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

src/plugins/intel_npu/src/compiler_adapter/src/driver_compiler_adapter.cpp

+71
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,72 @@
7979
#include "ir_serializer.hpp"
8080
#include "openvino/core/model.hpp"
8181

82+
#if defined _WIN32
83+
#ifndef NOMINMAX
84+
# define NOMINMAX
85+
#endif
86+
87+
#include <windows.h>
88+
#include <psapi.h>
89+
#else
90+
#include <fstream>
91+
#include <regex>
92+
#include <sstream>
93+
#endif
94+
8295
namespace {
8396

97+
#if defined _WIN32
98+
99+
int64_t get_peak_memory_usage() {
100+
PROCESS_MEMORY_COUNTERS mem_counters;
101+
if (!GetProcessMemoryInfo(GetCurrentProcess(), &mem_counters, sizeof(mem_counters))) {
102+
throw std::runtime_error("Can't get system memory values");
103+
}
104+
105+
// Linux tracks memory usage in pages and then converts them to kB.
106+
// Thus, there is always some room for inaccuracy as pages are not guaranteed to be fully used.
107+
// In Windows, the situation is different: the system returns the memory usage in bytes, not in pages.
108+
// To align the output between the two operating systems as closely as possible, we have two options:
109+
// 1. Use rounding to the nearest integer.
110+
// 2. Try to estimate the number of pages used in Windows. However,
111+
// this approach is likely to be inaccurate as well, so option 1 was chosen.
112+
static constexpr double bytes_in_kilobyte = 1024.0;
113+
114+
// please note then we calculate difference
115+
// to get peak memory increment value, so we return int64, not size_t
116+
return static_cast<int64_t>(std::round(mem_counters.PeakWorkingSetSize / bytes_in_kilobyte));
117+
}
118+
119+
#else
120+
121+
int64_t get_peak_memory_usage() {
122+
size_t peak_mem_usage_kB = 0;
123+
124+
std::ifstream status_file("/proc/self/status");
125+
std::string line;
126+
std::regex vm_peak_regex("VmPeak:");
127+
std::smatch vm_match;
128+
bool mem_values_found = false;
129+
while (std::getline(status_file, line)) {
130+
if (std::regex_search(line, vm_match, vm_peak_regex)) {
131+
std::istringstream iss(vm_match.suffix());
132+
iss >> peak_mem_usage_kB;
133+
mem_values_found = true;
134+
}
135+
}
136+
137+
if (!mem_values_found) {
138+
throw std::runtime_error("Can't get system memory values");
139+
}
140+
141+
// please note then we calculate difference
142+
// to get peak memory increment value, so we return int64, not size_t
143+
return static_cast<int64_t>(peak_mem_usage_kB);
144+
}
145+
146+
#endif
147+
84148
constexpr std::string_view INPUTS_PRECISIONS_KEY = "--inputs_precisions";
85149
constexpr std::string_view INPUTS_LAYOUTS_KEY = "--inputs_layouts";
86150
constexpr std::string_view OUTPUTS_PRECISIONS_KEY = "--outputs_precisions";
@@ -444,6 +508,7 @@ std::vector<std::shared_ptr<IGraph>> DriverCompilerAdapter::compileWS(const std:
444508
size_t callNumber = 0;
445509
bool compilationDone = false;
446510

511+
auto compile_model_mem_start = get_peak_memory_usage();
447512
while (!compilationDone) {
448513
_logger.debug("compileWS iteration %d", callNumber);
449514

@@ -482,6 +547,12 @@ std::vector<std::shared_ptr<IGraph>> DriverCompilerAdapter::compileWS(const std:
482547
mainGraphHandle = graphHandle;
483548
}
484549
}
550+
auto compile_model_mem_end = get_peak_memory_usage();
551+
552+
std::cout << "Start of compilation memory usage: Peak " << compile_model_mem_start << " KB" << std::endl;
553+
std::cout << "End of compilation memory usage: Peak " << compile_model_mem_end << " KB" << std::endl;
554+
std::cout << "Compilation memory usage: Peak " << compile_model_mem_end - compile_model_mem_start << " KB"
555+
<< std::endl;
485556

486557
std::vector<std::shared_ptr<IGraph>> driverGraphs;
487558
for (size_t handleIndex = 0; handleIndex < initGraphHandles.size(); ++handleIndex) {

0 commit comments

Comments
 (0)