Skip to content

Commit 3dbd1ff

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

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//
2+
// Copyright (C) 2024 Intel Corporation.
3+
// SPDX-License-Identifier: Apache 2.0
4+
//
5+
6+
#pragma once
7+
8+
namespace intel_npu {
9+
int64_t get_peak_memory_usage();
10+
}

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

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

82+
#include "mem_usage.hpp"
83+
8284
namespace {
8385

8486
constexpr std::string_view INPUTS_PRECISIONS_KEY = "--inputs_precisions";
@@ -444,6 +446,7 @@ std::vector<std::shared_ptr<IGraph>> DriverCompilerAdapter::compileWS(const std:
444446
size_t callNumber = 0;
445447
bool compilationDone = false;
446448

449+
auto compile_model_mem_start = get_peak_memory_usage();
447450
while (!compilationDone) {
448451
_logger.debug("compileWS iteration %d", callNumber);
449452

@@ -482,6 +485,12 @@ std::vector<std::shared_ptr<IGraph>> DriverCompilerAdapter::compileWS(const std:
482485
mainGraphHandle = graphHandle;
483486
}
484487
}
488+
auto compile_model_mem_end = get_peak_memory_usage();
489+
490+
std::cout << "Start of compilation memory usage: Peak " << compile_model_mem_start << " KB" << std::endl;
491+
std::cout << "End of compilation memory usage: Peak " << compile_model_mem_end << " KB" << std::endl;
492+
std::cout << "Compilation memory usage: Peak " << compile_model_mem_end - compile_model_mem_start << " KB"
493+
<< std::endl;
485494

486495
std::vector<std::shared_ptr<IGraph>> driverGraphs;
487496
for (size_t handleIndex = 0; handleIndex < initGraphHandles.size(); ++handleIndex) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
3+
#include "mem_usage.hpp"
4+
5+
namespace intel_npu {
6+
7+
#if defined _WIN32
8+
9+
#include <windows.h>
10+
#include <psapi.h>
11+
12+
int64_t get_peak_memory_usage() {
13+
PROCESS_MEMORY_COUNTERS mem_counters;
14+
if (!GetProcessMemoryInfo(GetCurrentProcess(), &mem_counters, sizeof(mem_counters))) {
15+
throw std::runtime_error("Can't get system memory values");
16+
}
17+
18+
// Linux tracks memory usage in pages and then converts them to kB.
19+
// Thus, there is always some room for inaccuracy as pages are not guaranteed to be fully used.
20+
// In Windows, the situation is different: the system returns the memory usage in bytes, not in pages.
21+
// To align the output between the two operating systems as closely as possible, we have two options:
22+
// 1. Use rounding to the nearest integer.
23+
// 2. Try to estimate the number of pages used in Windows. However,
24+
// this approach is likely to be inaccurate as well, so option 1 was chosen.
25+
static constexpr double bytes_in_kilobyte = 1024.0;
26+
27+
// please note then we calculate difference
28+
// to get peak memory increment value, so we return int64, not size_t
29+
return static_cast<int64_t>(std::round(mem_counters.PeakWorkingSetSize / bytes_in_kilobyte));
30+
}
31+
32+
#else
33+
34+
#include <fstream>
35+
#include <regex>
36+
#include <sstream>
37+
38+
int64_t get_peak_memory_usage() {
39+
size_t peak_mem_usage_kB = 0;
40+
41+
std::ifstream status_file("/proc/self/status");
42+
std::string line;
43+
std::regex vm_peak_regex("VmPeak:");
44+
std::smatch vm_match;
45+
bool mem_values_found = false;
46+
while (std::getline(status_file, line)) {
47+
if (std::regex_search(line, vm_match, vm_peak_regex)) {
48+
std::istringstream iss(vm_match.suffix());
49+
iss >> peak_mem_usage_kB;
50+
mem_values_found = true;
51+
}
52+
}
53+
54+
if (!mem_values_found) {
55+
throw std::runtime_error("Can't get system memory values");
56+
}
57+
58+
// please note then we calculate difference
59+
// to get peak memory increment value, so we return int64, not size_t
60+
return static_cast<int64_t>(peak_mem_usage_kB);
61+
}
62+
63+
#endif
64+
}

0 commit comments

Comments
 (0)