Skip to content

[Engine] poly: GPU cache-bias routing (gate=4) for Qwen4 and Qwen3.5 - #513

Open
NightRain233 wants to merge 1 commit into
simplexai-labs:mainfrom
NightRain233:feat/gpu-cache-bias-routing
Open

NightRain233 wants to merge 1 commit into
simplexai-labs:mainfrom
NightRain233:feat/gpu-cache-bias-routing

Conversation

@NightRain233

@NightRain233 NightRain233 commented Dec 6, 2026 •

Copy link
Copy Markdown
Contributor

Problem & Result

--cache-bias (cache-conditioned routing: select Top-K with +bias for VRAM-resident experts, then compute final softmax weights over ONLY the chosen n_used unbiased logits, matching CPU Engine::route_cached()) previously ran only on the host CPU and forced picks_like_alone() (and CUDA/HIP/Metal doorbell routing) off whenever opt_.cache_bias > 0.f.

As a result, enabling --cache-bias 0.5 on a partially offloaded Qwen4 model forced a full CPU router fallback on every layer — actually slowing down Metal decode when combined with #474 (27.39 -> 26.46 tok/s, router wait 23.9 ms/tok, despite raising VRAM hit rate to 97.9%).

This PR moves --cache-bias routing onto the GPU via a new kGateSoftmaxLogitAdd = 4 mode in route_rows() (Metal and CUDA/HIP) and wires slots_moved() + route_card() + card_cache_bias() for Qwen4, Qwen35, and Inkling2 (standalone on main; pairs naturally with #474 / #512 on Metal and immediately enables bell_on() / picks_like_alone() on CUDA/HIP).

Measured Performance (16 GB Apple M4, Qwen4-35B-A3B UD-IQ2_XS, 192 tok, --ple-direct)

(End-to-end Metal numbers measured with Metal Doorbell #474 active on the Round 4 integration branch before splitting; gate=4 kernel & slots_moved() parity verified directly on this standalone PR branch.)

Measured Quality Trade-Offs (Apple M5 Ultra, Qwen4-35B-A3B UD-IQ2_XS, simulated 4,000-expert partial VRAM, --defer-rank -1 isolated from deferral)

Config (--defer-rank -1 unless noted) 128-Step Top-1 128-Step Top-5 Gold Mean Prob VRAM Hit (TF / tasks) Math Trap Logic Puzzle Python LRUCache Python edit_distance Zh QA 16K NIAH Score
Full-VRAM lossless baseline 100.00% 100.00% 0.7326 100.0% PASS PASS PASS PASS PASS PASS 6/6
Partial-VRAM lossless (--cache-bias 0) 99.22% 100.00% 0.7299 58.7% (36–76%) PASS PASS PASS PASS PASS PASS 6/6
GPU --cache-bias 0.3 (no defer) 90.62% 100.00% 0.7158 69.4% (66–86%) FAIL FAIL PASS PASS PASS PASS 4/6
GPU --cache-bias 0.5 (no defer) 87.50% 100.00% 0.6894 78.9% (79–94%) PASS PASS PASS PASS PASS PASS 6/6
GPU --cache-bias 1.0 (no defer) 78.12% 99.22% 0.6424 93.4% (84–99%) PASS FAIL PASS PASS PASS PASS 5/6
GPU --cache-bias 0.5 + --defer-rank 0 78.12% 100.00% 0.5975 78.6% (79–94%) FAIL FAIL PASS PASS PASS PASS 4/6

(Key honest takeaway: --cache-bias is an opt-in approximate acceleration knob; 0.5 without deferral keeps 100% Top-5 / 87.5% Top-1 and passes all 6 tasks including 32K/16K NIAH, while stacking --cache-bias with --defer-rank 0 or pushing --cache-bias >= 1.0 visibly lowers strict math/logic accuracy on cold/mismatched profiles.)

Approach

  1. New kGateSoftmaxLogitAdd = 4 in route_rows() (engine/include/strata/poly/kernels.hpp, engine/metal/src/poly/basic.{cpp,metal}, engine/src/poly/kernels.cu):
    • Selects Top-n_used on logits * in_scale + bias (lower expert index wins exact ties, matching Engine::route_cached()), then runs softmax strictly over the chosen n_used unbiased scaled logits exp(raw[id_k] - mx_chosen) / sum, followed by standard norm / escale / w_scale.
    • Protects degenerate rows (all-NaN, all--inf, or sparse finite logits) in both Metal and CUDA/HIP without polluting the chosen-set softmax with -inf/NaN slots.
  2. Architecture Capability Hook Engine::card_cache_bias() (engine/src/poly/engine.{hpp,cpp}):
    • Adds virtual bool card_cache_bias() const { return false; } (bumping kPolyEngineAbiRev 2 -> 3 per engine.hpp:267).
    • Relaxes Engine::bell_on() (non-Metal branch; Metal stays gated until [Engine] metal: add MTLSharedEvent doorbell with defer-rank and watchdog-safe CB chunking #474) and Engine::picks_like_alone() from opt_.cache_bias <= 0.f to (opt_.cache_bias <= 0.f || card_cache_bias()).
    • Adds R < kStageRows to like_alone in Engine::step() and calls (void) card_moe(1) when !card && like_alone && opt_.cache_bias > 0.f in run() / step() so slots_moved() refreshes the GPU bias table before host-routed-like-alone batches.
  3. Per-Architecture GPU Bias Table (Qwen4, Qwen35, Inkling2):
    • Inkling2 already maintains its own card bias table (card_cache_bias() -> true).
    • Qwen4 and Qwen35 implement slots_moved() (updating a persistent in-place GPU cache_b_ buffer so captured CUDA/Metal graphs keep valid pointers across dynamic expert swaps, allocating nothing when cache_bias == 0) and dispatch kGateSoftmaxLogitAdd in route_card() when opt_.cache_bias > 0.f && cache_b_ && !all_cached() && n < kStageRows (plus !block_pass_ on Qwen35).
  4. Unit & Parity Tests (engine/metal/poly_tests/basic_parity.cpp, engine/metal/poly_tests/memory_growth.cpp):
    • test_route_gate4 and CacheBiasRouteEngine covering full parity vs Engine::route_cached(), cache_bias == 0 non-allocation, prompt-batch / all_cached() fallbacks, and Metal graph capture/replay across dynamic expert swaps.

Validation

Passed (on this exact PR branch d38abe8, Apple M4 macOS)

  • cmake --build engine/build-metal --target strata-poly poly_basic_parity poly_route_metal_test poly_memory_growth poly_qwen4_parity -j8 — PASS
  • ./engine/build-metal/poly_basic_parity — PASS (poly basic CPU-reference mismatches: 0, including all test_route_gate4 cases)
  • ./engine/build-metal/poly_route_metal_test — PASS
  • ./engine/build-metal/poly_memory_growth — PASS (CacheBiasRouteEngine + graph capture/replay + ABI/capability checks: OK)
  • ./engine/build-metal/poly_qwen4_parity — PASS
  • git diff --check and python3 tools/test_dropped_paths.py — PASS

Failed

  • None on Metal / Linux / macOS CI (Windows / Python tests/ hit unrelated Windows terminal timing flakes in test_update_with_model and test_bench; this PR touches zero Python files).

Not Run

  • CUDA / HIP compilation and hardware execution of engine/src/poly/kernels.cu (not available on macOS; statically verified line-by-line against gate == 0 warp primitives and degenerate-row handling — maintainer run-backends CI check needed before merge).
  • Real-weight end-to-end run on Qwen3.5 GGUF (covered by synthetic poly_tiny_qwen35moe_protocol and CacheBiasRouteEngine tests; real-weight verification was on Qwen4-35B-A3B).

Limits & Default-Path Preservation

  • Strictly opt-in via --cache-bias > 0. When --cache-bias 0 (the default), slots_moved() returns immediately without allocating cache_b_, route_card() dispatches the existing gate == 0 path byte-identically, and bell_on() / picks_like_alone() behavior is unchanged.

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