OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
Update file_load_bench (#36741)
Oleg Pipikina9a431dbec2e
diff --git a/src/core/tests/file_load_benchmark.cpp b/src/core/tests/file_load_benchmark.cppindex 3c983276d79..d14bd1bf9bf 100644--- a/src/core/tests/file_load_benchmark.cpp+++ b/src/core/tests/file_load_benchmark.cpp@@ -38,6 +38,13 @@#include "common_test_utils/common_utils.hpp"#include "common_test_utils/file_utils.hpp"+// These benchmarks measure wall-clock timing and are meaningless (and extremely slow for+// multi-GB files) in a Debug (-O0) build.+#ifndef NDEBUG+# error \+ "file_load_benchmark.cpp must be built in Release mode: rebuild with -DCMAKE_BUILD_TYPE=Release, or delete this #error to build in Debug anyway."+#endif+namespace ov::test {namespace {@@ -211,38 +218,37 @@ void loop_touch_mem_lock(const std::filesystem::path& path, size_t /*file_size*/ensure_memory_resident(mapped); // should be near no-op and just lock/unlock resident pages}-void parallel_loop_sync_then_memcpy(const std::filesystem::path& path, size_t file_size) {- auto mapped = load_mmap_object(path);- util::vm_prefetch(mapped->data(), mapped->size(), std::thread::hardware_concurrency());- constexpr size_t chunk_size = 128 * util::one_mib;- std::vector<char> buffer(std::min(chunk_size, file_size));- volatile char sink = 0;+// --- "compute" scenario -----------------------------------------------------------------+// Instead of a raw memcpy or mlock(), run a std::transform pass over the mapped bytes (e.g.+// mimicking a dequantization/dtype-conversion pass over model weights).+void compute_over_mapped(const std::shared_ptr<ov::MappedMemory>& mapped) {+ constexpr size_t chunk_size = 128 * util::one_mib; // 128 MiB chunks+ const size_t file_size = mapped->size();+ std::vector<uint64_t> out(std::min(chunk_size, file_size) / sizeof(uint64_t));+ uint64_t acc = 0;for (size_t offset = 0; offset < file_size; offset += chunk_size) {- const size_t copy_size = std::min(chunk_size, file_size - offset);- std::memcpy(buffer.data(), mapped->data() + offset, copy_size);- sink += buffer[0] + buffer[copy_size / 2] + buffer[copy_size - 1]; // prevents optimization+ const size_t n = std::min(chunk_size, file_size - offset);+ const size_t n_words = n / sizeof(uint64_t);+ const auto* first = reinterpret_cast<const uint64_t*>(mapped->data() + offset);+ std::transform(first, first + n_words, out.begin(), [](uint64_t v) {+ return v * 3u + 7u;+ });+ if (n_words > 0)+ acc += out[0] + out[n_words / 2] + out[n_words - 1]; // prevents optimization}+ volatile uint64_t sink = acc;+ (void)sink;}-void mmap_then_memcpy(const std::filesystem::path& path, size_t file_size) {+void mmap_then_compute(const std::filesystem::path& path, size_t /*file_size*/) {auto mapped = load_mmap_object(path);- constexpr size_t chunk_size = 128 * util::one_mib;- std::vector<char> buffer(std::min(chunk_size, file_size));- volatile char sink = 0;- for (size_t offset = 0; offset < file_size; offset += chunk_size) {- const size_t copy_size = std::min(chunk_size, file_size - offset);- std::memcpy(buffer.data(), mapped->data() + offset, copy_size);- sink += buffer[0] + buffer[copy_size / 2] + buffer[copy_size - 1]; // prevents optimization- }+ compute_over_mapped(mapped);}-// Strategy: ifstream into a single pre-allocated buffer — one kernel→user copy-void ifstream_read(const std::filesystem::path& path, size_t file_size) {- std::vector<char> read_buffer(file_size);- std::ifstream f(path, std::ios::binary);- f.read(read_buffer.data(), static_cast<std::streamsize>(file_size));- volatile char sink = read_buffer[0] + read_buffer[file_size / 2] + read_buffer[file_size - 1];- (void)sink;+void mmap_prefetch_then_compute(const std::filesystem::path& path, size_t /*file_size*/) {+ auto mapped = load_mmap_object(path);+ mapped->hint_prefetch();+ compute_over_mapped(mapped);}void mmap_prefetch_then_memcpy_partial(const std::filesystem::path& path,@@ -271,7 +277,7 @@ void mmap_prefetch_then_memcpy_partial(const std::filesystem::path& path,class FileLoadBenchmark : public ::testing::Test {};-TEST_F(FileLoadBenchmark, strategies_read_memcpy) {+TEST_F(FileLoadBenchmark, read_into_mmap_and_compute) {const std::vector<size_t> sizes_mib = {10, 100, 500, 1000};constexpr int warmup = 0;constexpr int runs = 3;@@ -285,37 +291,28 @@ TEST_F(FileLoadBenchmark, strategies_read_memcpy) {files.push_back(tf);}- // Collect results: [file_idx] -> {mmap_prefetch_memcpy, mmap_memcpy, ifstream}+ // Collect results: [file_idx] -> {no hint, sync prefetch}struct Row {size_t mib;- long long t_hint_prefetch;- long long t_no_prefault;- long long t_ifstream;+ long long t_no_hint;+ long long t_sync_prefetch;};std::vector<Row> results;for (const auto& tf : files) {Row r{};r.mib = tf.size_mib;- r.t_ifstream = bench(- [&]() {- strategy::ifstream_read(tf.path, tf.size_bytes());- },- tf.path,- tf.size_bytes(),- warmup,- runs);- r.t_no_prefault = bench(+ r.t_no_hint = bench([&]() {- strategy::mmap_then_memcpy(tf.path, tf.size_bytes());+ strategy::mmap_then_compute(tf.path, tf.size_bytes());},tf.path,tf.size_bytes(),warmup,runs);- r.t_hint_prefetch = bench(+ r.t_sync_prefetch = bench([&]() {- strategy::parallel_loop_sync_then_memcpy(tf.path, tf.size_bytes());+ strategy::mmap_prefetch_then_compute(tf.path, tf.size_bytes());},tf.path,tf.size_bytes(),@@ -325,10 +322,20 @@ TEST_F(FileLoadBenchmark, strategies_read_memcpy) {}printf("\n--- Latency (ms, mean of %d runs, cold cache) ---\n", runs);- printf("%-10s | %18s | %13s | %13s\n", "Size (MiB)", "parallel loop sync", "default mmap", "ifstream");- printf("%-10s-|-%18s-|-%13s-|-%13s\n", "----------", "------------------", "-------------", "-------------");+ printf("%-10s | %17s | %13s\n", "Size (MiB)", "sync prefetch", "mmap+compute");+ printf("%-10s-|-%17s-|-%13s\n", "----------", "-----------------", "-------------");+ for (const auto& r : results) {+ printf("%-10zu | %14lld ms | %10lld ms\n", r.mib, r.t_sync_prefetch, r.t_no_hint);+ }++ printf("\n--- Throughput (MiB/s) ---\n");+ printf("%-10s | %17s | %13s\n", "Size (MiB)", "sync prefetch", "mmap+compute");+ printf("%-10s-|-%17s-|-%13s\n", "----------", "-----------------", "-------------");for (const auto& r : results) {- printf("%-10zu | %15lld ms | %10lld ms | %10lld ms\n", r.mib, r.t_hint_prefetch, r.t_no_prefault, r.t_ifstream);+ printf("%-10zu | %12.0f MiB/s | %8.0f MiB/s\n",+ r.mib,+ throughput_mibs(r.mib, r.t_sync_prefetch),+ throughput_mibs(r.mib, r.t_no_hint));}}diff --git a/src/core/tests/file_load_benchmark_guide.md b/src/core/tests/file_load_benchmark_guide.mdindex b407019ad37..d681cb99ab8 100644--- a/src/core/tests/file_load_benchmark_guide.md+++ b/src/core/tests/file_load_benchmark_guide.md@@ -34,6 +34,7 @@ is printed once at the start of the run.| Test | Description ||------|-------------|-| `strategies_read_memcpy` | Compares cold-cache load-and-copy performance across three strategies — `ifstream` read into a preallocated buffer, `mmap` + `memcpy`, and `mmap` + `hint_prefetch` + `memcpy`. Reports latency and throughput. || `strategies_mlock` | Measures the cost of making an entire file resident in memory without an additional user copy. |-| `hint_prefetch_with_offset_table` | Stresses partial-region `hint_prefetch` on a single 1200 MB file across a matrix of starting offsets and region sizes. Highlights alignment and offset effects on prefetch latency. |\ No newline at end of file+| `read_into_mmap_and_compute` | **compute scenario.** Compares a `std::transform` pass over the mapped bytes (mimicking a dequantization/dtype-conversion pass) with and without a preceding synchronous `hint_prefetch`, instead of `mlock()` or `memcpy()`. Files up to 10 GB. |+| `hint_prefetch_with_offset_table` | Stresses partial-region `hint_prefetch` on a single 1200 MB file across a matrix of starting offsets and region sizes. Highlights alignment and offset effects on prefetch latency. |+