Skip to content

[Engine] qwen4: extend --ple-direct to Metal (F_NOCACHE), async overlap, 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 macOS into heavy swap thrashing, and caps decode throughput on 16 GB hosts.

Upstream #464 added an opt-in --ple-direct (DirectFile / PleReader) path for CUDA/HIP while explicitly keeping Metal excluded (!gpu::is_metal()). This PR builds directly on top of #464 (0794bc5) to extend Direct I/O PLE support to Apple Silicon Metal:

  • Enables PleReader with F_NOCACHE on macOS (--ple-direct / --ple-mmap / STRATA_PLE_DIRECT / STRATA_PLE_CACHE_ROWS), keeping CUDA/HIP opt-in default (0) unchanged and auto-selecting Direct I/O on tight-RAM Metal hosts (ple_direct = -1 auto mode).
  • Opens PleReader in load_geometry() before fill_expert_cache() so the PLE budget is accounted for during expert cache sizing, and submits Metal layer 0 (gpu::metal::submit()) before waiting on PleReader::collect() to overlap SSD reads with GPU work.
  • Extends start_ple_warmer() to support macOS (getpagesize() stride + MADV_WILLNEED) when mmap mode has sufficient headroom, gated behind origin/main's should_warm_ple_table().

Related discussion

Tracking issue: #472. Rebased cleanly onto origin/main (0794bc5, including merged #464).

Approach

  • Preserves [Engine] qwen4: opt-in --ple-direct: the PLE table's rows from disk, and the VRAM reserve does not count them #464's opt-in ple_direct = 0 default on CUDA/HIP/SYCL; adds ple_direct = -1 auto selection for tight-RAM Metal hosts (with explicit --ple-direct, --ple-mmap, and STRATA_PLE_DIRECT overrides).
  • Uses macOS F_NOCACHE in DirectFile (pread) with the bounded row cache (1,048,576 rows / 76 MiB default, tunable via STRATA_PLE_CACHE_ROWS) to prevent unified-memory page-cache bloat.
  • Adds Metal gpu::metal::submit() overlap before PleReader::collect() so layer 0 GPU execution hides SSD read latency; drains pending PLE tickets cleanly on ~Qwen4(), reset_call(), and on_reset().
  • Extends start_ple_warmer() for macOS (MADV_WILLNEED) behind should_warm_ple_table() on roomy Macs (>= 32–64 GiB).
  • Registers STRATA_PLE_DIRECT and STRATA_PLE_CACHE_ROWS in terminal/data/settings-register.json and updates docs/SETTINGS.md and docs/models/qwen4.md.

Validation

  • Passed (Apple M5 Ultra 256 GB, macOS, exact rebased commit 40478fe on origin/main 0794bc5):
    • Clean Metal Release build (strata-poly + all poly test targets): 0 errors.
    • Full ctest --test-dir build-pr473 -L poly --output-on-failure: 51/51 (100%) passed (including poly_qwen4_parity, poly_memory_test, poly_memory_growth, poly_metal_defaults, poly_runtime_safety).
    • python3 tools/test_dropped_paths.py, python3 tools/settings_register.py --check, python3 tools/check --only docs: all passed (docs 6/6).
  • Passed (Physical 16 GB Apple M4, Qwen4-35B-A3B-UD-IQ2_XS, integrated stack):
    • True 64K (65,536-token) BasinKV NIAH (--metal-memory balanced --ple-direct --max-context 131072 --basinkv --basinkv-index sphere --basinkv-kv-quant wht_int4): PLE Direct achieved 93.0% cache hit, only 246.30 MiB SSD reads (699.8 ms collect wait), peak memory footprint 9.56 GiB, 0.00 GiB exit gpu_inuse, 100% exact needle recall (COBALT-7319).
    • True ~120K (122,880-token) BasinKV NIAH (same flags): PLE Direct achieved 95.1% cache hit (1,870,192 / 1,966,464 rows), only 338.86 MiB SSD reads (1,061.0 ms collect wait), peak memory footprint 9.57 GiB (0 swaps), 0.00 GiB exit gpu_inuse, 100% exact needle recall (QUARTZ-6042).
    • 8K–32K contexts: peak footprint stayed 7.4–8.8 GiB (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).
  • Failed: None observed.
  • Not run: CUDA, HIP, SYCL, Linux, and Windows builds were not run locally on this Apple Silicon host. Kept as Draft per CONTRIBUTING.md pending maintainer review and run-backends cross-backend CI.

Limits

  • Direct I/O bypasses OS page caching by design; hosts with abundant free RAM continue to use mmap + should_warm_ple_table() warmer automatically.
  • Cross-backend compilation on CUDA/HIP/SYCL/MSVC must be confirmed via run-backends CI before merging.

@NightRain233
NightRain233 force-pushed the feat/qwen4-metal-ple-direct branch from cb2ddec to 40478fe Compare December 6, 2026 16:45
@NightRain233 NightRain233 changed the title [Engine] qwen4: add memory-aware PLE Direct reader (F_NOCACHE) and macOS warmer [Engine] qwen4: extend --ple-direct to Metal (F_NOCACHE), async overlap, and macOS warmer Dec 6, 2026
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