[PR #588] [CLOSED] DeepSeek: Windows drops the copied source pages and bounds the in-place experts' working set #611

Closed
opened 2026-10-29 21:27:27 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/mw00/project-maya/pull/588
Author: @handmade0octopus
Created: 10/28/2026
Status: ❌ Closed

Base: main ← Head: pr/windows-inplace-ws-trim


📝 Commits (2)

  • 0e62c78 IQ1_M: MSVC keeps ggml's delta rounding (35 of 11,220 subnormal rows differed without it)
  • 30ac28a DeepSeek: Windows drops the experts' source pages it has copied and bounds the in-place ones' working set

📊 Changes

7 files changed (+337 additions, -24 deletions)

View changed files

📝 CMakeLists.txt (+8 -1)
📝 docs/SETTINGS.md (+19 -5)
📝 src/ds4/ds4_model.cu (+105 -3)
📝 src/ds4/ds4_os.hpp (+94 -8)
📝 src/kernels/cpu/iq_avx2.cpp (+9 -1)
📝 tests/ds4_copy_cache.cpp (+6 -6)
➕ tests/ds4_copy_cache_windows_test.cpp (+96 -0)

📄 Description

The bug

On Windows, a long DeepSeek request on a warm-capped pack (some CPU-lane experts copied into RAM, the rest read in
place from the GGUFs) drains the machine's free memory until nothing is left. Measured on one box (one GPU, 128 GB
RAM, Maya-S IQ2_XXS with an 82 GB warm cap, fresh 32,768-token prompt + a 2,048-token decode, a 1 Hz client-side
guard at 6,144 MiB of available RAM):

  • the same engine and config that passed with 6,170 MiB free on one prompt failed at 6,103 MiB on the next
    (run cancelled mid-decode) - the drain is prompt-dependent, so a cap validated once is not safe;
  • a 2 Hz attribution sample over the request showed the engine's working set +18.6 GB while its private bytes
    grew +0.5 GB and the system cache shrank 20 GB (the OS reclaiming under pressure): file-backed pages of the
    in-place experts faulting into this process's working set and never leaving it;
  • the drain scales with the number of uncopied in-place experts: packs whose experts are all copied (IQ1_M, IQ1_S)
    are flat (22-36 GiB free throughout), 6,177 uncopied experts left 6.0 GiB.

Linux never sees this: the same pages sit in the page cache, which counts as available. On Windows a mapped file
page resident in a working set counts as in use until demoted, and nothing demoted it:

  • os::advise(fd, off, n, /*need=*/false) - the release hint at every copy/promotion/swap site - was a no-op on
    Windows;
  • os::pread_direct/open_direct are Windows stubs, so the unbuffered hybrid in IoQueue never engages and every
    runtime expert read is buffered (that pool self-trims, so it is the secondary consumer);
  • the CPU lane's computes and the lookahead's PrefetchVirtualMemory deliberately fault the experts in.

The fix (Windows-scoped; Linux code paths unchanged)

  1. os::unlock_range(p, n): demotes a mapped range's whole interior pages from the process working set to the
    standby list with VirtualUnlock (the working-set documentation: unlocked pages are removed even though the
    call reports ERROR_NOT_LOCKED). A clean file-backed page stays resident and soft-faults back unchanged - it
    only counts as available again. Interior pages only: an edge page may hold a neighbour's bytes.
  2. os::advise(!need) is real on Windows now: map_ro keeps an fd→view registry, advise demotes through it.
    This makes the ten existing release sites (expert copies, VRAM promotions, RAM swaps) effective there.
  3. release_copied_cache drops its Windows no-op: after an expert's bytes are copied, its whole interior source
    pages are demoted the same way (81-95 GB of double-held pages per load on the machine above).
  4. The in-place experts' working set gets a bound: the decode lanes note every in-place range they touch
    (lane_take / lane_take_win CPU-lane keep-lists, ahead_ask's prefetches) into a bounded ring,
    reference-counted per range - a range is demoted only when its oldest note's count reaches zero, i.e. no use
    since that note, so hot experts are never churned. A trimmer thread keeps the process working set under the
    load's own size plus MAYA_DS4_INPLACE_TRIM_GB (default 6 GB on Windows, 0 elsewhere;
    MAYA_DS4_INPLACE_TRIM_MS, default 250). An un-counted first build demoted hot pages through their stale notes
    and cost 13% decode; the counted one costs none.

Results on the machine above (fresh prompts, 6,144 MiB floor, visible servers, live telemetry)

arm min free RAM prefill decode
warm-82, before 6,170.5 / 6,103.5 MiB 672.9 12.1 t/s pass-by-26-MiB / cancelled
warm-82, after 21,375.7 MiB 678.0 12.4 t/s working set ends below its start
warm-96, before ~6,009 MiB (twice) - - cancelled both times
warm-96, after 6,676.4 MiB 691.5 11.5 t/s valid - a cap that was impossible

Packs whose experts are all copied and pinned (IQ1_M in production) are unaffected by construction: the ring stays
empty and the trimmer thread no-ops.

Tests

  • new ds4_copy_cache_windows_test (Windows CTest): copied source pages leave the working set with byte-identical
    re-fault; unlock_range/advise interior-only semantics; edge pages retained; sub-page, null, bad-fd and
    overflow ranges rejected; working_set_bytes sane; an independent expert copy stays intact;
  • native_rows_exact_test passes (its Windows arm needs the MSVC IQ1_M rounding fix in the first commit: without
    it 35 of 11,220 subnormal cases differ from ggml-cpu);
  • a standalone 256 MiB mapping probe: 65,536 resident pages → 2 (the two retained edge pages), contents identical
    after re-fault.

🔄 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/mw00/project-maya/pull/588 **Author:** [@handmade0octopus](https://github.com/handmade0octopus) **Created:** 10/28/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `pr/windows-inplace-ws-trim` --- ### 📝 Commits (2) - [`0e62c78`](https://github.com/mw00/project-maya/commit/0e62c78830ea37cec4ed4060ec0a06de92b35a71) IQ1_M: MSVC keeps ggml's delta rounding (35 of 11,220 subnormal rows differed without it) - [`30ac28a`](https://github.com/mw00/project-maya/commit/30ac28a5beca6326e4266a4cd5684d91b461960b) DeepSeek: Windows drops the experts' source pages it has copied and bounds the in-place ones' working set ### 📊 Changes **7 files changed** (+337 additions, -24 deletions) <details> <summary>View changed files</summary> 📝 `CMakeLists.txt` (+8 -1) 📝 `docs/SETTINGS.md` (+19 -5) 📝 `src/ds4/ds4_model.cu` (+105 -3) 📝 `src/ds4/ds4_os.hpp` (+94 -8) 📝 `src/kernels/cpu/iq_avx2.cpp` (+9 -1) 📝 `tests/ds4_copy_cache.cpp` (+6 -6) ➕ `tests/ds4_copy_cache_windows_test.cpp` (+96 -0) </details> ### 📄 Description ## The bug On Windows, a long DeepSeek request on a warm-capped pack (some CPU-lane experts copied into RAM, the rest read in place from the GGUFs) drains the machine's free memory until nothing is left. Measured on one box (one GPU, 128 GB RAM, Maya-S IQ2_XXS with an 82 GB warm cap, fresh 32,768-token prompt + a 2,048-token decode, a 1 Hz client-side guard at 6,144 MiB of available RAM): - the same engine and config that passed with 6,170 MiB free on one prompt **failed at 6,103 MiB on the next** (run cancelled mid-decode) - the drain is prompt-dependent, so a cap validated once is not safe; - a 2 Hz attribution sample over the request showed the engine's **working set +18.6 GB** while its private bytes grew **+0.5 GB** and the system cache shrank 20 GB (the OS reclaiming under pressure): file-backed pages of the in-place experts faulting into *this process's working set* and never leaving it; - the drain scales with the number of uncopied in-place experts: packs whose experts are all copied (IQ1_M, IQ1_S) are flat (22-36 GiB free throughout), 6,177 uncopied experts left 6.0 GiB. Linux never sees this: the same pages sit in the page cache, which counts as available. On Windows a mapped file page resident in a working set counts as in use until demoted, and nothing demoted it: - `os::advise(fd, off, n, /*need=*/false)` - the release hint at every copy/promotion/swap site - was a no-op on Windows; - `os::pread_direct`/`open_direct` are Windows stubs, so the unbuffered hybrid in `IoQueue` never engages and every runtime expert read is buffered (that pool self-trims, so it is the secondary consumer); - the CPU lane's computes and the lookahead's `PrefetchVirtualMemory` deliberately fault the experts in. ## The fix (Windows-scoped; Linux code paths unchanged) 1. `os::unlock_range(p, n)`: demotes a mapped range's whole interior pages from the process working set to the standby list with `VirtualUnlock` (the working-set documentation: unlocked pages are removed even though the call reports `ERROR_NOT_LOCKED`). A clean file-backed page stays resident and soft-faults back unchanged - it only counts as available again. Interior pages only: an edge page may hold a neighbour's bytes. 2. `os::advise(!need)` is real on Windows now: `map_ro` keeps an fd→view registry, `advise` demotes through it. This makes the ten existing release sites (expert copies, VRAM promotions, RAM swaps) effective there. 3. `release_copied_cache` drops its Windows no-op: after an expert's bytes are copied, its whole interior source pages are demoted the same way (81-95 GB of double-held pages per load on the machine above). 4. The in-place experts' working set gets a bound: the decode lanes note every in-place range they touch (`lane_take` / `lane_take_win` CPU-lane keep-lists, `ahead_ask`'s prefetches) into a bounded ring, reference-counted per range - a range is demoted only when its oldest note's count reaches zero, i.e. no use since that note, so hot experts are never churned. A trimmer thread keeps the process working set under the load's own size plus `MAYA_DS4_INPLACE_TRIM_GB` (default 6 GB on Windows, 0 elsewhere; `MAYA_DS4_INPLACE_TRIM_MS`, default 250). An un-counted first build demoted hot pages through their stale notes and cost 13% decode; the counted one costs none. ## Results on the machine above (fresh prompts, 6,144 MiB floor, visible servers, live telemetry) | arm | min free RAM | prefill | decode | | |---|---:|---:|---:|---| | warm-82, before | 6,170.5 / **6,103.5 MiB** | 672.9 | 12.1 t/s | pass-by-26-MiB / **cancelled** | | warm-82, after | **21,375.7 MiB** | 678.0 | **12.4 t/s** | working set ends *below* its start | | warm-96, before | ~6,009 MiB (twice) | - | - | **cancelled both times** | | warm-96, after | 6,676.4 MiB | 691.5 | 11.5 t/s | valid - a cap that was impossible | Packs whose experts are all copied and pinned (IQ1_M in production) are unaffected by construction: the ring stays empty and the trimmer thread no-ops. ## Tests - new `ds4_copy_cache_windows_test` (Windows CTest): copied source pages leave the working set with byte-identical re-fault; `unlock_range`/`advise` interior-only semantics; edge pages retained; sub-page, null, bad-fd and overflow ranges rejected; `working_set_bytes` sane; an independent expert copy stays intact; - `native_rows_exact_test` passes (its Windows arm needs the MSVC IQ1_M rounding fix in the first commit: without it 35 of 11,220 subnormal cases differ from ggml-cpu); - a standalone 256 MiB mapping probe: 65,536 resident pages → 2 (the two retained edge pages), contents identical after re-fault. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-29 21:27:27 +07:00
Sign in to join this conversation.
No labels
pull-request
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
mw00/project-maya#611
No description provided.