Skip to content

[Engine] qwen4: add memory-aware PLE Direct reader (F_NOCACHE) and macOS warmer - #473

Draft
NightRain233 wants to merge 1 commit into
simplexai-labs:mainfrom
NightRain233:feat/qwen4-metal-ple-direct
Draft

NightRain233 wants to merge 1 commit into
simplexai-labs:mainfrom
NightRain233:feat/qwen4-metal-ple-direct

Conversation

@NightRain233

@NightRain233 NightRain233 commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Problem and result

On 16 GB Apple Silicon Macs running Qwen4-35B-A3B (UD-IQ2_XS), the 10.73 GiB per-layer embedding (PLE) table (per_layer_token_embd.weight) cannot stay resident in RAM alongside VRAM-cached weights and routed experts. Reading PLE rows via mmap pollutes the unified-memory page cache, pushes the OS into heavy swap thrashing, and caps decode throughput on 16 GB hosts.

This PR adds a memory-aware Direct I/O PLE reader (PleReader + DirectFile with F_NOCACHE on macOS, --ple-direct / --ple-mmap / STRATA_PLE_DIRECT / STRATA_PLE_CACHE_ROWS), asynchronous prompt/draft PLE prefetching with Metal submit() ordering so layer 0 executes while SSD reads complete, and macOS 16 KiB MADV_WILLNEED warmer support gated cleanly behind origin/main's should_warm_ple_table() in Engine::on_loaded().

Related discussion

Tracking issue: #472. Rebased onto origin/main (7cef952, v0.2.20-poly.11), preserving d72af68's should_warm_ple_table() and Engine::on_loaded() deferral.

Approach

  • Auto-selects the Direct I/O PleReader (bypassing unified-memory page-cache bloat via F_NOCACHE on macOS) with a bounded row cache (1,048,576 rows / 76 MiB default) when host RAM is too tight to hold the full PLE table safely alongside routed experts, while keeping explicit --ple-direct / --ple-mmap / STRATA_PLE_DIRECT overrides.
  • Submits layer 0 on Metal (gpu::metal::submit()) before waiting on PleReader::collect() so GPU work overlaps SSD reads; drains pending PLE tickets cleanly on ~Qwen4(), reset_call(), and on_reset().
  • Preserves should_warm_ple_table() on origin/main and extends start_ple_warmer() to support macOS (getpagesize() stride + MADV_WILLNEED) when mmap mode has sufficient headroom.
  • Keeps non-Apple / CUDA / HIP / SYCL paths guarded cleanly; registers STRATA_PLE_DIRECT and STRATA_PLE_CACHE_ROWS in terminal/data/settings-register.json and updates docs/SETTINGS.md.

Validation

  • Passed (Apple M4 16 GB, macOS, commit cb2ddec on origin/main 7cef952):
    • Metal Release configure + build (strata-poly, poly_qwen4_parity, poly_memory_test, poly_memory_growth, poly_runtime_safety, poly_tiny_model_fixture): 0 errors, 0 warnings.
    • ctest --test-dir engine/build-metal -R '^(poly_qwen4_parity|poly_memory_test|poly_memory_growth|poly_metal_defaults|poly_runtime_safety)$' --output-on-failure: 11/11 passed.
    • Standalone engine/src/poly/cold_budget_test.cpp: passed (poly_cold_budget_test: ok).
    • python3 tools/check --only docs: passed (6/6).
    • python3 -m unittest tools/test_settings_register.py: passed (8/8).
    • Real hardware 16 GB Apple M4 (Qwen4-35B-A3B-UD-IQ2_XS, integrated stack on bfd5d1b): PLE direct reader (76.0 MiB cache, F_NOCACHE) kept peak memory footprint at 7.4–8.8 GiB across 8K–32K contexts (13.9–15.5 MiB SSD reads per 192-token decode; 612 MiB SSD reads across 32,768-token prompt with 58.1% cache hits and 100% NIAH recall).
  • Failed: None observed.
  • Not run: CUDA, HIP, SYCL, Linux, and Windows builds were not run locally on this Apple Silicon host. Kept as Draft pending cross-backend CI (backends.yml / check.yml).

Limits

  • Direct I/O bypasses OS page caching by design; hosts with abundant free RAM (>= 32–64 GiB) continue to use mmap + should_warm_ple_table() warmer automatically.
  • Cross-backend compilation on CUDA/HIP/SYCL/MSVC must be confirmed via CI before marking Ready for Review.

@NightRain233
NightRain233 force-pushed the feat/qwen4-metal-ple-direct branch from 3cee661 to cb2ddec Compare December 6, 2026 05:38
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