[PR #4910] [CLOSED] [Engine] avoid per-call unordered_map allocation in PleReader::issue #5246

Closed
opened 2026-11-07 16:33:45 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/4910
Author: @martifaj
Created: 11/1/2026
Status: ❌ Closed

Base: main ← Head: engine/ple-reader-issue-dedup


📝 Commits (1)

  • 10e5214 [Engine] avoid per-call unordered_map allocation in PleReader::issue

📊 Changes

1 file changed (+41 additions, -16 deletions)

View changed files

📝 src/ngram/ple_reader.cpp (+41 -16)

📄 Description

Problem and result

Problem:
Every call to PleReader::issue() (invoked per token/layer during direct-SSD PLE streaming) constructed a temporary std::unordered_map<uint64_t, size_t> by_page and std::vector<Job> jobs on the heap to deduplicate cache-missed rows that land on the same 4 KiB page, and then sorted jobs by page offset anyway (std::sort(jobs.begin(), jobs.end(), ...)). For decode and prefetch batches (n = 1 or n = 16), heap-allocating and hashing into std::unordered_map on every call dominates the CPU time spent inside issue().

Result:
PleReader::issue() now collects cache misses into a stack array (Miss stack_misses[64], falling back to a reusable Impl::miss_scratch vector when n > 64), sorts them by (first, order), and coalesces adjacent equal-page misses in a single linear pass directly into m.queue. The queued Jobs (their page offsets, read lengths, Use ordering, and stats.dedup_rows) are identical to origin/main, reducing CPU time in issue() by 2.6x–3.2x across sparse/random batches (n = 1..256) and reducing caller-thread issue time on a real 28.8 GB Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS PLE shard from 2.9–3.2 µs/token to 1.7–2.1 µs/token on Intel Core i9-10900F + Crucial P3 1TB NVMe (CT1000P3SSD8).

Approach

  • Add a plain Miss struct holding {first, length, row, in_page, order, dst}.
  • For n <= 64 (which covers single-row reads n = 1, decode/prefetch PLE_N_HEADS = 16, and 4-token chunks), store misses on the stack (Miss stack_misses[64]); for n > 64, reuse Impl::miss_scratch so steady-state calls do not reallocate.
  • Sort misses[0..n_miss) by (first, order) and merge contiguous runs with the same first page offset directly into m.queue, reserving end - k entries in j.uses.
  • Preserves exact behavior and output bytes: same page offsets, same length (PAGE or 2 * PAGE), same Use order within each job, and identical stats.dedup_rows counts.

Validation

  • Passed:
    • Environment:
      • Commit: 14fc8a0a030c85504f850546a3df8d14c276f104 (vs. origin/main 46c152a1)
      • OS / Kernel: Linux 6.17.0-41-generic #41-Ubuntu SMP PREEMPT_DYNAMIC x86_64
      • CPU: Intel(R) Core(TM) i9-10900F CPU @ 2.80GHz (10 cores / 20 threads)
      • RAM: 60 GiB (58 GiB available)
      • Storage / SSD: Crucial P3 1TB NVMe SSD (CT1000P3SSD8, firmware P9CR313, PCIe Gen3 x4 8.0 GT/s, ext4 on /dev/nvme0n1p5 for the GGUF PLE shard; /tmp on tmpfs for synthetic stress-test tables)
      • GPU: NVIDIA GeForce RTX 2080 Ti (11,264 MiB VRAM, driver 580.159.03)
      • Model artifact: Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf (28.8 GB PLE shard)
      • Compiler: g++ (Ubuntu 15.2.0-4ubuntu4) 15.2.0 (-O2 -std=c++20 -Wall -Wextra -Wpedantic -Werror -pthread)
    • Real GGUF PLE shard parity & direct-I/O test (ple_reader_test --gguf):
      • Parity vs. mmap (20,000 rows / 1,250 tokens):
        g++ -O2 -std=c++20 -Wall -Wextra -Wpedantic -Werror -pthread -Iinclude src/ngram/ple_reader_test.cpp src/kernels/ngram.cpp src/ngram/ple_reader.cpp src/platform/direct_file.cpp src/platform/memory.cpp -o /tmp/ple_reader_test
        /tmp/ple_reader_test --gguf Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf --rows 20000
        
        Output:
        tokens 1250: mmap 3454.4 us/token, direct 333.2 us/token (issue on this thread 2.6 us)
        ple io: 20000 rows, 0.0% row-cache hits, 20000 SSD reads (83.7 MB), read p50 215 us p99 366 us, blocked 410.726 ms total (submit 8.813 ms), cache 0/0 rows
        ple_reader real-table check: OK (bit-identical)
        
      • 3-run before/after --direct-only comparison on Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf (--rows 20000, 1,250 tokens × 16 heads, cache_rows = 0):
        Run Arm direct (µs/token) Caller-thread issue (µs/token) SSD read p50 / p99 (µs) Result
        Run 1 Before (origin/main) 591.2 2.9 170 / 1092 OK (bit-identical)
        Run 1 After (HEAD) 247.6 1.7 122 / 279 OK (bit-identical)
        Run 2 Before (origin/main) 247.6 3.2 121 / 275 OK (bit-identical)
        Run 2 After (HEAD) 247.4 2.1 122 / 275 OK (bit-identical)
        Run 3 Before (origin/main) 248.0 3.1 122 / 275 OK (bit-identical)
        Run 3 After (HEAD) 246.7 1.9 121 / 276 OK (bit-identical)
        (Caller-thread issue time in ple_reader_test includes mutex lock, PleReader::issue() job construction, cv_work.notify_one(), and the eventfd file.wake() syscall, dropping from 2.9–3.2 µs/token to 1.7–2.1 µs/token.)
    • Unit & format selftest (ple_reader_test --selftest):
      • Command:
        /tmp/ple_reader_test --selftest
        
      • Result:
        ple prefetched-key reuse (reordered, duplicated, subset), Direct vs Mmap bytes: OK
        ple table shared by a prompt thread, a decode window and a single gather, 9 reader configurations, vs the mapped reader: OK
        ple formats, both readers vs their dequantizers: IQ4_NL Q5_0 F8_E4M3 Q5_1 Q8_0 Q4_0 BF16 NVFP4: OK
        ple_reader selftest (row_bytes=90): OK
        ple_reader selftest (row_bytes=110): OK
        
    • Stress & correctness suite (ple_reader_stress_test), including ASan/UBSan and TSan:
      • Commands:
        g++ -O2 -std=c++20 -Wall -Wextra -Wpedantic -Werror -pthread -Iinclude src/ngram/ple_reader_stress_test.cpp src/ngram/ple_reader.cpp src/platform/direct_file.cpp -o /tmp/ple_reader_stress_test
        /tmp/ple_reader_stress_test --seconds 0.15 --dir /tmp
        
        g++ -O2 -std=c++20 -Wall -Wextra -Wpedantic -Werror -pthread -fsanitize=address,undefined -Iinclude src/ngram/ple_reader_stress_test.cpp src/ngram/ple_reader.cpp src/platform/direct_file.cpp -o /tmp/ple_reader_stress_asan
        /tmp/ple_reader_stress_asan --seconds 0.05 --dir /tmp
        
        g++ -O2 -std=c++20 -Wall -Wextra -Wpedantic -Werror -pthread -fsanitize=thread -Iinclude src/ngram/ple_reader_stress_test.cpp src/ngram/ple_reader.cpp src/platform/direct_file.cpp -o /tmp/ple_reader_stress_tsan
        /tmp/ple_reader_stress_tsan --seconds 0.05 --dir /tmp
        
      • Result: ple_reader stress: 7 tables x 15 configurations ... error path: a failed read fails the next tickets too, none hangs ... ple_reader stress: OK (all 105 configurations + error-path check passed bit-identical against ground truth, with zero compiler warnings and zero ASan/UBSan/TSan findings).
    • Job-equivalence & CPU microbenchmark (Intel Core i9-10900F, 3 runs of 150,000 iterations per batch size, rb = 90, table_offset = 192):
      • Verified across 1,000 randomized batches per configuration that issue() produces identical queue jobs (offset, length, ticket, and uses vector elements) and dedup_rows counts before and after the change.
      • Measured CPU time per issue() call (miss-collection + deduplication + sort + queue push, all 3 runs shown):
        Workload Batch size (n) Before (origin/main) — Runs 1 / 2 / 3 After (engine/ple-reader-issue-dedup) — Runs 1 / 2 / 3 Speedup (Runs 1 / 2 / 3)
        Sparse / random rows (0..1,000,000) 1 70.1 / 71.9 / 71.2 ns/call 23.4 / 22.5 / 22.7 ns/call 3.00x / 3.20x / 3.14x
        Sparse / random rows (0..1,000,000) 16 (PLE_N_HEADS) 1245.0 / 1245.5 / 1221.6 ns/call 452.7 / 460.9 / 452.2 ns/call 2.75x / 2.70x / 2.70x
        Sparse / random rows (0..1,000,000) 64 7737.6 / 7907.1 / 7690.9 ns/call 2429.4 / 2495.8 / 2487.4 ns/call 3.18x / 3.17x / 3.09x
        Sparse / random rows (0..1,000,000) 256 31162.5 / 32309.2 / 31237.2 ns/call 11920.7 / 12263.9 / 11999.1 ns/call 2.61x / 2.63x / 2.60x
        High page-collision rows (0..512, ~11 pages) 1 68.2 / 69.5 / 69.2 ns/call 21.2 / 21.8 / 22.0 ns/call 3.21x / 3.18x / 3.14x
        High page-collision rows (0..512, ~11 pages) 16 (PLE_N_HEADS) 775.3 / 805.2 / 770.8 ns/call 323.2 / 336.0 / 325.6 ns/call 2.40x / 2.40x / 2.37x
        High page-collision rows (0..512, ~11 pages) 64 1869.3 / 1916.5 / 1862.5 ns/call 1966.9 / 2023.6 / 1959.5 ns/call 0.95x / 0.95x / 0.95x
  • Failed:
    • None.
  • Not run:
    • Full CUDA/HIP/SYCL GPU target builds (strata-engine): not built on this host because no CUDA (nvcc), ROCm/HIP, or SYCL compiler toolkit is installed in the build environment (PleReader is pure host C++ and tested end-to-end against the real GGUF PLE shard via ple_reader_test --gguf).

Limits

  • Scoped strictly to PleReader::issue() page-miss deduplication and sorting; each Job still owns its std::vector<Use> uses allocation and DirectFile is unchanged.
  • In synthetic extreme page-collision cases at larger batch sizes (e.g. n = 64 rows all falling into ~11 pages), sorting all 64 Miss structs before coalescing is ~5% (~100 ns) slower than hashing into an 11-entry unordered_map and sorting 11 Jobs; on real PLE n-gram tables (320M rows across 16 heads), page collisions within a call are rare (Sparse / random rows above, 3.1x faster at n = 64).

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/4910 **Author:** [@martifaj](https://github.com/martifaj) **Created:** 11/1/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `engine/ple-reader-issue-dedup` --- ### 📝 Commits (1) - [`10e5214`](https://github.com/Niko1221/Strata/commit/10e5214842a9fcf20e14b75afe46511cae807d1f) [Engine] avoid per-call unordered_map allocation in PleReader::issue ### 📊 Changes **1 file changed** (+41 additions, -16 deletions) <details> <summary>View changed files</summary> 📝 `src/ngram/ple_reader.cpp` (+41 -16) </details> ### 📄 Description ### Problem and result **Problem:** Every call to `PleReader::issue()` (invoked per token/layer during direct-SSD PLE streaming) constructed a temporary `std::unordered_map<uint64_t, size_t> by_page` and `std::vector<Job> jobs` on the heap to deduplicate cache-missed rows that land on the same 4 KiB page, and then sorted `jobs` by page offset anyway (`std::sort(jobs.begin(), jobs.end(), ...)`). For decode and prefetch batches (`n = 1` or `n = 16`), heap-allocating and hashing into `std::unordered_map` on every call dominates the CPU time spent inside `issue()`. **Result:** `PleReader::issue()` now collects cache misses into a stack array (`Miss stack_misses[64]`, falling back to a reusable `Impl::miss_scratch` vector when `n > 64`), sorts them by `(first, order)`, and coalesces adjacent equal-page misses in a single linear pass directly into `m.queue`. The queued `Job`s (their page offsets, read lengths, `Use` ordering, and `stats.dedup_rows`) are identical to `origin/main`, reducing CPU time in `issue()` by **2.6x–3.2x** across sparse/random batches (`n = 1..256`) and reducing caller-thread `issue` time on a real 28.8 GB `Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS` PLE shard from **2.9–3.2 µs/token** to **1.7–2.1 µs/token** on Intel Core i9-10900F + Crucial P3 1TB NVMe (`CT1000P3SSD8`). ### Approach - Add a plain `Miss` struct holding `{first, length, row, in_page, order, dst}`. - For `n <= 64` (which covers single-row reads `n = 1`, decode/prefetch `PLE_N_HEADS = 16`, and 4-token chunks), store misses on the stack (`Miss stack_misses[64]`); for `n > 64`, reuse `Impl::miss_scratch` so steady-state calls do not reallocate. - Sort `misses[0..n_miss)` by `(first, order)` and merge contiguous runs with the same `first` page offset directly into `m.queue`, reserving `end - k` entries in `j.uses`. - Preserves exact behavior and output bytes: same page offsets, same `length` (`PAGE` or `2 * PAGE`), same `Use` order within each job, and identical `stats.dedup_rows` counts. ### Validation - **Passed:** - **Environment:** - Commit: `14fc8a0a030c85504f850546a3df8d14c276f104` (vs. `origin/main` `46c152a1`) - OS / Kernel: `Linux 6.17.0-41-generic #41-Ubuntu SMP PREEMPT_DYNAMIC x86_64` - CPU: `Intel(R) Core(TM) i9-10900F CPU @ 2.80GHz` (10 cores / 20 threads) - RAM: `60 GiB` (`58 GiB` available) - Storage / SSD: `Crucial P3 1TB NVMe SSD` (`CT1000P3SSD8`, firmware `P9CR313`, PCIe Gen3 x4 `8.0 GT/s`, `ext4` on `/dev/nvme0n1p5` for the GGUF PLE shard; `/tmp` on `tmpfs` for synthetic stress-test tables) - GPU: `NVIDIA GeForce RTX 2080 Ti` (`11,264 MiB` VRAM, driver `580.159.03`) - Model artifact: `Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf` (28.8 GB PLE shard) - Compiler: `g++ (Ubuntu 15.2.0-4ubuntu4) 15.2.0` (`-O2 -std=c++20 -Wall -Wextra -Wpedantic -Werror -pthread`) - **Real GGUF PLE shard parity & direct-I/O test (`ple_reader_test --gguf`):** - Parity vs. `mmap` (`20,000` rows / `1,250` tokens): ```sh g++ -O2 -std=c++20 -Wall -Wextra -Wpedantic -Werror -pthread -Iinclude src/ngram/ple_reader_test.cpp src/kernels/ngram.cpp src/ngram/ple_reader.cpp src/platform/direct_file.cpp src/platform/memory.cpp -o /tmp/ple_reader_test /tmp/ple_reader_test --gguf Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf --rows 20000 ``` Output: ```text tokens 1250: mmap 3454.4 us/token, direct 333.2 us/token (issue on this thread 2.6 us) ple io: 20000 rows, 0.0% row-cache hits, 20000 SSD reads (83.7 MB), read p50 215 us p99 366 us, blocked 410.726 ms total (submit 8.813 ms), cache 0/0 rows ple_reader real-table check: OK (bit-identical) ``` - 3-run before/after `--direct-only` comparison on `Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf` (`--rows 20000`, `1,250` tokens × `16` heads, `cache_rows = 0`): | Run | Arm | `direct` (µs/token) | Caller-thread `issue` (µs/token) | SSD read p50 / p99 (µs) | Result | | :--- | :--- | :--- | :--- | :--- | :--- | | Run 1 | Before (`origin/main`) | `591.2` | `2.9` | `170 / 1092` | `OK (bit-identical)` | | Run 1 | After (`HEAD`) | `247.6` | **`1.7`** | `122 / 279` | `OK (bit-identical)` | | Run 2 | Before (`origin/main`) | `247.6` | `3.2` | `121 / 275` | `OK (bit-identical)` | | Run 2 | After (`HEAD`) | `247.4` | **`2.1`** | `122 / 275` | `OK (bit-identical)` | | Run 3 | Before (`origin/main`) | `248.0` | `3.1` | `122 / 275` | `OK (bit-identical)` | | Run 3 | After (`HEAD`) | `246.7` | **`1.9`** | `121 / 276` | `OK (bit-identical)` | *(Caller-thread `issue` time in `ple_reader_test` includes mutex lock, `PleReader::issue()` job construction, `cv_work.notify_one()`, and the `eventfd` `file.wake()` syscall, dropping from `2.9–3.2 µs/token` to `1.7–2.1 µs/token`.)* - **Unit & format selftest (`ple_reader_test --selftest`):** - Command: ```sh /tmp/ple_reader_test --selftest ``` - Result: ```text ple prefetched-key reuse (reordered, duplicated, subset), Direct vs Mmap bytes: OK ple table shared by a prompt thread, a decode window and a single gather, 9 reader configurations, vs the mapped reader: OK ple formats, both readers vs their dequantizers: IQ4_NL Q5_0 F8_E4M3 Q5_1 Q8_0 Q4_0 BF16 NVFP4: OK ple_reader selftest (row_bytes=90): OK ple_reader selftest (row_bytes=110): OK ``` - **Stress & correctness suite (`ple_reader_stress_test`), including ASan/UBSan and TSan:** - Commands: ```sh g++ -O2 -std=c++20 -Wall -Wextra -Wpedantic -Werror -pthread -Iinclude src/ngram/ple_reader_stress_test.cpp src/ngram/ple_reader.cpp src/platform/direct_file.cpp -o /tmp/ple_reader_stress_test /tmp/ple_reader_stress_test --seconds 0.15 --dir /tmp g++ -O2 -std=c++20 -Wall -Wextra -Wpedantic -Werror -pthread -fsanitize=address,undefined -Iinclude src/ngram/ple_reader_stress_test.cpp src/ngram/ple_reader.cpp src/platform/direct_file.cpp -o /tmp/ple_reader_stress_asan /tmp/ple_reader_stress_asan --seconds 0.05 --dir /tmp g++ -O2 -std=c++20 -Wall -Wextra -Wpedantic -Werror -pthread -fsanitize=thread -Iinclude src/ngram/ple_reader_stress_test.cpp src/ngram/ple_reader.cpp src/platform/direct_file.cpp -o /tmp/ple_reader_stress_tsan /tmp/ple_reader_stress_tsan --seconds 0.05 --dir /tmp ``` - Result: `ple_reader stress: 7 tables x 15 configurations ... error path: a failed read fails the next tickets too, none hangs ... ple_reader stress: OK` (all 105 configurations + error-path check passed bit-identical against ground truth, with zero compiler warnings and zero ASan/UBSan/TSan findings). - **Job-equivalence & CPU microbenchmark (Intel Core i9-10900F, 3 runs of 150,000 iterations per batch size, `rb = 90`, `table_offset = 192`):** - Verified across 1,000 randomized batches per configuration that `issue()` produces identical `queue` jobs (`offset`, `length`, `ticket`, and `uses` vector elements) and `dedup_rows` counts before and after the change. - Measured CPU time per `issue()` call (miss-collection + deduplication + sort + `queue` push, all 3 runs shown): | Workload | Batch size (`n`) | Before (`origin/main`) — Runs 1 / 2 / 3 | After (`engine/ple-reader-issue-dedup`) — Runs 1 / 2 / 3 | Speedup (Runs 1 / 2 / 3) | | :--- | :--- | :--- | :--- | :--- | | Sparse / random rows (`0..1,000,000`) | `1` | `70.1 / 71.9 / 71.2 ns/call` | `23.4 / 22.5 / 22.7 ns/call` | **3.00x / 3.20x / 3.14x** | | Sparse / random rows (`0..1,000,000`) | `16` (`PLE_N_HEADS`) | `1245.0 / 1245.5 / 1221.6 ns/call` | `452.7 / 460.9 / 452.2 ns/call` | **2.75x / 2.70x / 2.70x** | | Sparse / random rows (`0..1,000,000`) | `64` | `7737.6 / 7907.1 / 7690.9 ns/call` | `2429.4 / 2495.8 / 2487.4 ns/call` | **3.18x / 3.17x / 3.09x** | | Sparse / random rows (`0..1,000,000`) | `256` | `31162.5 / 32309.2 / 31237.2 ns/call` | `11920.7 / 12263.9 / 11999.1 ns/call` | **2.61x / 2.63x / 2.60x** | | High page-collision rows (`0..512`, ~11 pages) | `1` | `68.2 / 69.5 / 69.2 ns/call` | `21.2 / 21.8 / 22.0 ns/call` | **3.21x / 3.18x / 3.14x** | | High page-collision rows (`0..512`, ~11 pages) | `16` (`PLE_N_HEADS`) | `775.3 / 805.2 / 770.8 ns/call` | `323.2 / 336.0 / 325.6 ns/call` | **2.40x / 2.40x / 2.37x** | | High page-collision rows (`0..512`, ~11 pages) | `64` | `1869.3 / 1916.5 / 1862.5 ns/call` | `1966.9 / 2023.6 / 1959.5 ns/call` | **0.95x / 0.95x / 0.95x** | - **Failed:** - None. - **Not run:** - Full CUDA/HIP/SYCL GPU target builds (`strata-engine`): not built on this host because no CUDA (`nvcc`), ROCm/HIP, or SYCL compiler toolkit is installed in the build environment (`PleReader` is pure host C++ and tested end-to-end against the real GGUF PLE shard via `ple_reader_test --gguf`). ### Limits - Scoped strictly to `PleReader::issue()` page-miss deduplication and sorting; each `Job` still owns its `std::vector<Use> uses` allocation and `DirectFile` is unchanged. - In synthetic extreme page-collision cases at larger batch sizes (e.g. `n = 64` rows all falling into ~11 pages), sorting all 64 `Miss` structs before coalescing is ~5% (`~100 ns`) slower than hashing into an 11-entry `unordered_map` and sorting 11 `Job`s; on real PLE n-gram tables (320M rows across 16 heads), page collisions within a call are rare (`Sparse / random` rows above, **3.1x** faster at `n = 64`). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-07 16:33:45 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#5246
No description provided.