[PR #3072] [Engine] Windows low-RAM mode: STRATA_PROMPT_DIRECT=1 for a mapped experts.bin without a RAM budget (#3065) #4436

Open
opened 2026-11-07 15:35:42 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/3072
Author: @As7656431
Created: 10/18/2026
Status: 🔄 Open

Base: main ← Head: windows-prompt-direct


📝 Commits (3)

  • 2c62621 prompt path: STRATA_PROMPT_DIRECT=1 on Windows, also in the low-RAM mode without a RAM budget (#3065)
  • 857b57b prompt path: copy blobs already in the working set, read the rest through the file cache (STRATA_PREFILL_DIRECT_RAM, #3065)
  • 237d6cb tests: STRATA_PROMPT_DIRECT=1 on a mapped experts.bin - copy_blobs hands out the file's bytes, read unbuffered, copied from the working set or read through the file cache in merged runs (#3065)

📊 Changes

5 files changed (+245 additions, -6 deletions)

View changed files

📝 include/strata/core/expert_source.hpp (+11 -1)
📝 src/core/expert_source.cpp (+140 -2)
📝 src/prefill/prefill.cpp (+9 -3)
📝 src/program/generate.cpp (+12 -0)
📝 tests/core/file_expert_source_test.cpp (+73 -0)

📄 Description

Problem and result

On a Windows PC whose RAM cannot hold the experts, the low-RAM mode (--mmap-experts, no --resident-budget-gib) reads each prompt chunk's experts by copying them out of the mapped experts.bin. Those copies are page faults. On this PC (RTX 5070 12 GB, 31.8 GiB RAM, a 35.5 GB IQ2_XS experts.bin on a drive of about 1.5 GB/s) the drive read only 0.2-0.5 GB/s during a long prompt, and the faulted pages push the decode's cached experts out of RAM. STRATA_PROMPT_DIRECT=1 already reads the prompt path's blobs unbuffered, but only on Linux and only with a RAM budget.

This PR, opt-in and Windows only:

  • STRATA_PROMPT_DIRECT=1 also works on Windows and in the low-RAM mode without a budget: the stager's blobs are read with unbuffered, overlapped reads from a second handle on experts.bin, while the decode keeps the mapping.
  • STRATA_PREFILL_DIRECT_RAM (on that path only): a blob whose pages are already in the working set is copied from the mapping; =2 reads the rest through a cached handle (what the standby list still holds), =3 as 2 with neighbouring blobs of a layer merged into one read.

With both unset nothing changes. On 0.2.3.1, same PC and settings, tools/strata_bench.py, medians of 3:

main (7ddc17a3) this PR, STRATA_PROMPT_DIRECT=1 STRATA_PREFILL_DIRECT_RAM=3
4K prompt 87.0 tok/s, first token 46.3 s 414.6 tok/s, first token 9.7 s
16K prompt 101.3 tok/s, first token 158.7 s 846.6 tok/s, first token 19.0 s
agent follow-up turn, first token 62.8 s 7.2 s
agent decode 5.7 tok/s 48.7 tok/s
short-chat decode 55.8 tok/s 55.9 tok/s

#3065 has the problem, the measurements and the reproduction steps. There is no maintainer reply on scope yet; both switches are opt-in and no default changes. This PR was closed in the 0.2.0-beta reset; it is now rebased on main (7ddc17a3, v0.2.3.1) and re-tested there. It is independent of #3086; the two merge cleanly together.

Approach

  • open_prompt_direct gets a Windows branch: each expert file is opened again with FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED (experts.bin only, and not when the file tier is already unbuffered). With STRATA_PREFILL_DIRECT_RAM >= 2 it also opens a cached overlapped handle.
  • read_direct uses these handles when the tier itself is not unbuffered. copy_blobs already reads through pdirect_, as it does on Linux.
  • ExpertSource::prompt_reads() (default false; true on Windows for experts.bin with those handles open): the stager in prefill.cpp then hands the unpinned blobs to copy_blobs, 8 at a time. transient() is not touched, so the decode and the prompt path's CPU share behave as before; only where the bytes come from changes.
  • copy_blobs with STRATA_PREFILL_DIRECT_RAM (Windows, prompt_reads() only) asks QueryWorkingSetEx which blobs are already in RAM.
  • generate.cpp reads the switch in the mmap mode without a budget on Windows only, so Linux keeps its behaviour also with the switch set.
  • An earlier approach to the same faults, #1520, releases the copied pages after the copy; this PR avoids the faults themselves.
  • New test test_prompt_direct_mapped in tests/core/file_expert_source_test.cpp: a synthetic experts.bin, open_prompt_direct, then copy_blobs of 8 blobs, read unbuffered, copied from the working set, and read through the file cache in merged runs, compared byte for byte with the file.

Validation

Passed:

  • Build, Windows 10 22H2, MSVC 2022, CUDA 13.0, -DCMAKE_CUDA_ARCHITECTURES=120 -DSTRATA_PORTABLE=ON: cmake --build build --target strata on 857b57b3 (this branch before the test commit; that commit touches only tests/), and cmake --build build --target file_expert_source_test with -DSTRATA_BUILD_TESTS=ON on 237d6cb6 (the head). No new kind of compiler warning; a few more C4996 (getenv), as in the code around it.
  • file_expert_source_test on 237d6cb6: file_expert_source_test: PASS. The new test prints prompt direct (unbuffered): 7 of 8 blobs read from the file: bytes PASS and prompt direct (file cache, merged): 7 of 8 blobs read from the file: bytes PASS (the eighth blob was copied from the working set). The test also passes on main 7ddc17a3, and on this branch merged with #3086.
  • Same computation. STRATA_DUMP_FIRST_LOGITS after an 8K document and a 300-token prompt, two runs per build and setting, in one round with main: for each prompt there were two outputs in all, the first choice was the same in every run, and main itself gave both outputs for the 300-token prompt (KL 1.9e-4 between them). This branch gave each output once with the switches unset and once with them set, for both prompts. On the 8K document main's two runs gave the first output; the second differs from it by KL 1.24e-3, which is what the 0.1.45 release showed between two of its own runs of this document.
  • Speed, the table above: tools/strata_bench.py of 7ddc17a3 as the client, against serve/server.py of the same commit started separately (--url, and PYTHONUTF8=1 for #2792), on the same day with the same settings; only the engine and the two variables differ. The main run is the report in #3063.
  • With the switches set the start-up log says the prompt path's expert reads go unbuffered (STRATA_PROMPT_DIRECT=1, mmap mode).

Failed: none observed.

Not run:

  • HIP, SYCL and Metal builds: there is no toolchain for them on this PC. The new code is host-side Windows file I/O (#if defined(_WIN32)) in files that every backend compiles, so the HIP build on Windows compiles it too. I would be glad if someone with a HIP or SYCL setup could build this branch; until then I am leaving it as a draft.
  • Linux: the Linux code path is unchanged, and the new test also runs there (with O_DIRECT, skipping without it), but I have not built or run it on Linux.
  • A fast NVMe drive (#2791 suggests the default path is much faster there), a RAM budget together with STRATA_PROMPT_DIRECT=1 on Windows (that combination now opens the handles; before, it was refused as Linux only), the GGUF read in place, other models, contexts above 32K.

Limits

  • The gain needs Windows, experts that do not fit in RAM, the low-RAM mode and a drive that is not very fast. Elsewhere the switches change little or nothing.
  • The tool's prompts are generated word lists. With a real 20K-token English document the gain was smaller (#3065, on 0.1.44.1 with all switches: 162.5 s down to 33.1 s).
  • NTFS serves unbuffered reads of a mapped file one at a time (the note in read_direct); on this drive they still run near its speed.

🔄 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/3072 **Author:** [@As7656431](https://github.com/As7656431) **Created:** 10/18/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `windows-prompt-direct` --- ### 📝 Commits (3) - [`2c62621`](https://github.com/Niko1221/Strata/commit/2c62621457943d848f3f74e91515f25ec1eea14d) prompt path: STRATA_PROMPT_DIRECT=1 on Windows, also in the low-RAM mode without a RAM budget (#3065) - [`857b57b`](https://github.com/Niko1221/Strata/commit/857b57b3f9b3f63575e1d3734c3c8306ed7be237) prompt path: copy blobs already in the working set, read the rest through the file cache (STRATA_PREFILL_DIRECT_RAM, #3065) - [`237d6cb`](https://github.com/Niko1221/Strata/commit/237d6cb6e5c19f5061f4680c9bfadee39271543b) tests: STRATA_PROMPT_DIRECT=1 on a mapped experts.bin - copy_blobs hands out the file's bytes, read unbuffered, copied from the working set or read through the file cache in merged runs (#3065) ### 📊 Changes **5 files changed** (+245 additions, -6 deletions) <details> <summary>View changed files</summary> 📝 `include/strata/core/expert_source.hpp` (+11 -1) 📝 `src/core/expert_source.cpp` (+140 -2) 📝 `src/prefill/prefill.cpp` (+9 -3) 📝 `src/program/generate.cpp` (+12 -0) 📝 `tests/core/file_expert_source_test.cpp` (+73 -0) </details> ### 📄 Description ## Problem and result On a Windows PC whose RAM cannot hold the experts, the low-RAM mode (`--mmap-experts`, no `--resident-budget-gib`) reads each prompt chunk's experts by copying them out of the mapped `experts.bin`. Those copies are page faults. On this PC (RTX 5070 12 GB, 31.8 GiB RAM, a 35.5 GB IQ2_XS `experts.bin` on a drive of about 1.5 GB/s) the drive read only 0.2-0.5 GB/s during a long prompt, and the faulted pages push the decode's cached experts out of RAM. `STRATA_PROMPT_DIRECT=1` already reads the prompt path's blobs unbuffered, but only on Linux and only with a RAM budget. This PR, opt-in and Windows only: - `STRATA_PROMPT_DIRECT=1` also works on Windows and in the low-RAM mode without a budget: the stager's blobs are read with unbuffered, overlapped reads from a second handle on `experts.bin`, while the decode keeps the mapping. - `STRATA_PREFILL_DIRECT_RAM` (on that path only): a blob whose pages are already in the working set is copied from the mapping; `=2` reads the rest through a cached handle (what the standby list still holds), `=3` as 2 with neighbouring blobs of a layer merged into one read. With both unset nothing changes. On 0.2.3.1, same PC and settings, `tools/strata_bench.py`, medians of 3: | | main (7ddc17a3) | this PR, `STRATA_PROMPT_DIRECT=1 STRATA_PREFILL_DIRECT_RAM=3` | |---|---|---| | 4K prompt | 87.0 tok/s, first token 46.3 s | 414.6 tok/s, first token 9.7 s | | 16K prompt | 101.3 tok/s, first token 158.7 s | 846.6 tok/s, first token 19.0 s | | agent follow-up turn, first token | 62.8 s | 7.2 s | | agent decode | 5.7 tok/s | 48.7 tok/s | | short-chat decode | 55.8 tok/s | 55.9 tok/s | ## Related discussion #3065 has the problem, the measurements and the reproduction steps. There is no maintainer reply on scope yet; both switches are opt-in and no default changes. This PR was closed in the 0.2.0-beta reset; it is now rebased on `main` (7ddc17a3, v0.2.3.1) and re-tested there. It is independent of #3086; the two merge cleanly together. ## Approach - `open_prompt_direct` gets a Windows branch: each expert file is opened again with `FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED` (`experts.bin` only, and not when the file tier is already unbuffered). With `STRATA_PREFILL_DIRECT_RAM >= 2` it also opens a cached overlapped handle. - `read_direct` uses these handles when the tier itself is not unbuffered. `copy_blobs` already reads through `pdirect_`, as it does on Linux. - `ExpertSource::prompt_reads()` (default false; true on Windows for `experts.bin` with those handles open): the stager in `prefill.cpp` then hands the unpinned blobs to `copy_blobs`, 8 at a time. `transient()` is not touched, so the decode and the prompt path's CPU share behave as before; only where the bytes come from changes. - `copy_blobs` with `STRATA_PREFILL_DIRECT_RAM` (Windows, `prompt_reads()` only) asks `QueryWorkingSetEx` which blobs are already in RAM. - `generate.cpp` reads the switch in the mmap mode without a budget on Windows only, so Linux keeps its behaviour also with the switch set. - An earlier approach to the same faults, #1520, releases the copied pages after the copy; this PR avoids the faults themselves. - New test `test_prompt_direct_mapped` in `tests/core/file_expert_source_test.cpp`: a synthetic `experts.bin`, `open_prompt_direct`, then `copy_blobs` of 8 blobs, read unbuffered, copied from the working set, and read through the file cache in merged runs, compared byte for byte with the file. ## Validation **Passed:** - Build, Windows 10 22H2, MSVC 2022, CUDA 13.0, `-DCMAKE_CUDA_ARCHITECTURES=120 -DSTRATA_PORTABLE=ON`: `cmake --build build --target strata` on 857b57b3 (this branch before the test commit; that commit touches only `tests/`), and `cmake --build build --target file_expert_source_test` with `-DSTRATA_BUILD_TESTS=ON` on 237d6cb6 (the head). No new kind of compiler warning; a few more C4996 (`getenv`), as in the code around it. - `file_expert_source_test` on 237d6cb6: `file_expert_source_test: PASS`. The new test prints `prompt direct (unbuffered): 7 of 8 blobs read from the file: bytes PASS` and `prompt direct (file cache, merged): 7 of 8 blobs read from the file: bytes PASS` (the eighth blob was copied from the working set). The test also passes on main 7ddc17a3, and on this branch merged with #3086. - Same computation. `STRATA_DUMP_FIRST_LOGITS` after an 8K document and a 300-token prompt, two runs per build and setting, in one round with main: for each prompt there were two outputs in all, the first choice was the same in every run, and main itself gave both outputs for the 300-token prompt (KL 1.9e-4 between them). This branch gave each output once with the switches unset and once with them set, for both prompts. On the 8K document main's two runs gave the first output; the second differs from it by KL 1.24e-3, which is what the 0.1.45 release showed between two of its own runs of this document. - Speed, the table above: `tools/strata_bench.py` of 7ddc17a3 as the client, against `serve/server.py` of the same commit started separately (`--url`, and `PYTHONUTF8=1` for #2792), on the same day with the same settings; only the engine and the two variables differ. The main run is the report in #3063. - With the switches set the start-up log says `the prompt path's expert reads go unbuffered (STRATA_PROMPT_DIRECT=1, mmap mode)`. **Failed:** none observed. **Not run:** - HIP, SYCL and Metal builds: there is no toolchain for them on this PC. The new code is host-side Windows file I/O (`#if defined(_WIN32)`) in files that every backend compiles, so the HIP build on Windows compiles it too. I would be glad if someone with a HIP or SYCL setup could build this branch; until then I am leaving it as a draft. - Linux: the Linux code path is unchanged, and the new test also runs there (with O_DIRECT, skipping without it), but I have not built or run it on Linux. - A fast NVMe drive (#2791 suggests the default path is much faster there), a RAM budget together with `STRATA_PROMPT_DIRECT=1` on Windows (that combination now opens the handles; before, it was refused as Linux only), the GGUF read in place, other models, contexts above 32K. ## Limits - The gain needs Windows, experts that do not fit in RAM, the low-RAM mode and a drive that is not very fast. Elsewhere the switches change little or nothing. - The tool's prompts are generated word lists. With a real 20K-token English document the gain was smaller (#3065, on 0.1.44.1 with all switches: 162.5 s down to 33.1 s). - NTFS serves unbuffered reads of a mapped file one at a time (the note in `read_direct`); on this drive they still run near its speed. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
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#4436
No description provided.