Skip to content

[Engine] Cap WDDM expert cache shrink step to 256 MiB to avoid VRAM over-reduction - #6089

Closed
cobra91 wants to merge 0 commit into
Niko1221:mainfrom
cobra91:wddm-shrink-cap
Closed

cobra91 wants to merge 0 commit into
Niko1221:mainfrom
cobra91:wddm-shrink-cap

Conversation

@cobra91

@cobra91 cobra91 commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Under Windows WDDM, when the engine sizes the primary expert cache and queries device_free_bytes() right after initial allocation, WDDM's memory reporting can lag, occasionally returning free_b < 16 MiB.

In Vanilla upstream (src/program/generate.cpp:9022), when free_b is near 0, give is computed as:

int64_t give = want - (int64_t) free_b + (64ll << 20);
if (free_b < ((size_t) 16 << 20))
    give = std::max<int64_t>(give, ++zero_reads <= 2 ? 1ll << 30 : xcache.bytes() / 4);

Because want includes --vram-reserve-mib plus pipe_first, give drops 1.0 to 1.4 GiB in a single iteration. If WDDM driver reporting remains low on the second check, it chops another 1.4 GiB (losing up to 2.8 GiB of VRAM).

In multi-GPU configurations (--layer-split auto), this over-reduction tricks the layer-split cost model into believing GPU0 lacks capacity, causing it to push excessive layers to secondary cards (e.g., dropping K from 22 to 17 on an RTX 5080 + 4060 Ti rig). In single-GPU or dual-GPU setups, it leaves hundreds of MiB (or even GiB) of dead VRAM space above the requested reserve, unnecessarily spilling expert slots to host RAM or disk.

Solution

  1. Bound the shrink step to 256 MiB:
    • When free_b < 16 MiB, step down by 256 MiB per read for up to 4 iterations (++zero_reads <= 4 ? 256ll << 20 : xcache.bytes() / 4).
    • When free_b >= 16 MiB, cap the shrink to std::min(give, 256ll << 20).
  2. Improve log clarity by printing the exact amount of MiB being reduced: shrinking the expert cache by %lld MiB.

Validation

Tested on a physical dual NVIDIA rig:

  • Hardware: RTX 5080 (16 GB, sm_120) + RTX 4060 Ti (16 GB, sm_89, PCIe x2), 32 GB DDR5, Windows 11
  • Base: upstream/main (c77572806)
  • Model: Swift-Qwen3.8-Flash-Next (IQ2_XS), 64K context, q4_0 KV, --layer-split auto --pipeline-windows 2

A/B Observations during startup & load:

Metric Vanilla Upstream (v0.2.15) With 256 MiB Bounded Shrink Cap
GPU0 Shrink behavior Panic chopped ~1.4 GiB twice (-2.8 GiB) Bounded 256 MiB steps, exact target hit
GPU0 Post-Load Free VRAM 3,823 MiB (2,747 MiB wasted dead space above 1,076 MiB reserve) 1,061 MiB (matching target 1,076 MiB reserve within 15 MiB)
GPU0 Expert Slots 6,215 slots (8.09 GiB) 7,976 slots (10.43 GiB, +1,761 slots retained)
GPU1 Expert Slots 7,746 slots (10.55 GiB) 7,949 slots (10.89 GiB, +203 slots retained)
Total Expert Slots 13,961 slots 15,925 slots (+1,964 slots retained on GPUs)
Layer Split Placement Degraded to K=17 (17 layers GPU0 / 31 layers GPU1) Optimal K=22 (22 layers GPU0 / 26 layers GPU1)

Sustained Generation Benchmark (10,000 Continuous Tokens, Cold + Warm, 40,000 Tokens Total):

Tested under continuous heavy dual-GPU load (~260W total across both cards):

Metric Arm A (Vanilla c775728) Cold Arm B (PR #6089) Cold Arm A (Vanilla) Warm Arm B (PR #6089) Warm Gain / Impact PR #6089
Tokens generated 10,000 tok 10,000 tok 10,000 tok 10,000 tok Full sustained load
Total time 68.06 s (~1m08s) 63.32 s (~1m03s) 71.08 s (~1m11s) 68.96 s (~1m09s) -4.74 s (Cold) / -2.12 s (Warm)
Decode speed 146.9 tok/s 157.9 tok/s 140.7 tok/s 145.0 tok/s +7.5% (Cold) / +3.1% (Warm)
Expert Cache Hit Rate 99.3% (6.1M lookups) 99.6% (5.9M lookups) 99.2% (6.4M lookups) 99.3% (6.2M lookups) +0.1% to +0.3%
Draft Accept Rate 80.2% 84.2% 77.9% 81.3% +3.4% to +4.0%
SSD Disk Reads 0 io 0 io 0 io 0 io No swapping, resident in VRAM+RAM
WDDM Stability / Crash 0 crash 0 crash 0 crash 0 crash Zero hangs, rock-solid memory stability

@cobra91

cobra91 commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

Verified and benchmarked in isolation on Strata v0.2.17 (3142b66b9).

In generate.cpp:9278, Windows WDDM commit latency can still report 0 MiB free, triggering the 1 GiB (1ll << 30) panic drop and shedding hundreds of expert slots unnecessarily.

  • With the 256 MiB bounded step, expert cache retention on the primary card remains optimal.
  • Benchmark on v0.2.17 shows cold decode improving from 41.0 to 45.1 tok/s (+10.0%) and synthetic median improving from 63.6 to 68.7 tok/s (+8.0%).

Clean, fully mergeable, and confirms stability on v0.2.17.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant