Repository navigation
[Engine] poly: GPU cache-bias routing (gate=4) for Qwen4 and Qwen3.5 - #513
Open
NightRain233 wants to merge 1 commit into
Open
NightRain233 wants to merge 1 commit into
NightRain233 wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem & Result
--cache-bias(cache-conditioned routing: select Top-K with+biasfor VRAM-resident experts, then compute final softmax weights over ONLY the chosenn_usedunbiased logits, matching CPUEngine::route_cached()) previously ran only on the host CPU and forcedpicks_like_alone()(and CUDA/HIP/Metal doorbell routing) off wheneveropt_.cache_bias > 0.f.As a result, enabling
--cache-bias 0.5on 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 wait23.9 ms/tok, despite raising VRAM hit rate to97.9%).This PR moves
--cache-biasrouting onto the GPU via a newkGateSoftmaxLogitAdd = 4mode inroute_rows()(Metal and CUDA/HIP) and wiresslots_moved()+route_card()+card_cache_bias()forQwen4,Qwen35, andInkling2(standalone onmain; pairs naturally with #474 / #512 on Metal and immediately enablesbell_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=4kernel &slots_moved()parity verified directly on this standalone PR branch.)STRATA_GPU_EXPERTS=4000,STRATA_FREEZE_EXPERTS=1,--window 2 --defer-rank 0):--cache-bias 0.5(before):26.46 tok/s(97.9%VRAM hit,router wait 23.9 ms/tok)--cache-bias 0,--defer-rank 0):27.39–29.24 tok/s(~92%VRAM hit)--cache-bias 0.3/0.5/1.0:31.35 tok/s(98.0%hit) /31.90 tok/s(98.7%hit,CPU 3.6 ms/tok) /31.20 tok/s(99.4%hit)--cache-bias 0.5):33.52 tok/s(98.0%hit,+26.7%vs CPU--cache-bias 0.5);--window 4:31.28 tok/s(97.5%hit)--metal-memory balanced, 4,207 experts auto-loaded):--cache-bias 0:30.16 tok/s-> GPU--cache-bias 0.5:32.69 tok/s(97.4%hit)Measured Quality Trade-Offs (Apple M5 Ultra, Qwen4-35B-A3B
UD-IQ2_XS, simulated 4,000-expert partial VRAM,--defer-rank -1isolated from deferral)--defer-rank -1unless noted)LRUCacheedit_distance--cache-bias 0)--cache-bias 0.3(no defer)--cache-bias 0.5(no defer)--cache-bias 1.0(no defer)--cache-bias 0.5+--defer-rank 0(Key honest takeaway:
--cache-biasis an opt-in approximate acceleration knob;0.5without deferral keeps 100% Top-5 / 87.5% Top-1 and passes all 6 tasks including 32K/16K NIAH, while stacking--cache-biaswith--defer-rank 0or pushing--cache-bias >= 1.0visibly lowers strict math/logic accuracy on cold/mismatched profiles.)Approach
kGateSoftmaxLogitAdd = 4inroute_rows()(engine/include/strata/poly/kernels.hpp,engine/metal/src/poly/basic.{cpp,metal},engine/src/poly/kernels.cu):n_usedonlogits * in_scale + bias(lower expert index wins exact ties, matchingEngine::route_cached()), then runs softmax strictly over the chosenn_usedunbiased scaled logitsexp(raw[id_k] - mx_chosen) / sum, followed by standardnorm/escale/w_scale.-inf, or sparse finite logits) in both Metal and CUDA/HIP without polluting the chosen-set softmax with-inf/NaN slots.Engine::card_cache_bias()(engine/src/poly/engine.{hpp,cpp}):virtual bool card_cache_bias() const { return false; }(bumpingkPolyEngineAbiRev2 -> 3perengine.hpp:267).Engine::bell_on()(non-Metal branch; Metal stays gated until [Engine] metal: add MTLSharedEvent doorbell with defer-rank and watchdog-safe CB chunking #474) andEngine::picks_like_alone()fromopt_.cache_bias <= 0.fto(opt_.cache_bias <= 0.f || card_cache_bias()).R < kStageRowstolike_aloneinEngine::step()and calls(void) card_moe(1)when!card && like_alone && opt_.cache_bias > 0.finrun()/step()soslots_moved()refreshes the GPU bias table before host-routed-like-alone batches.Qwen4,Qwen35,Inkling2):Inkling2already maintains its own card bias table (card_cache_bias() -> true).Qwen4andQwen35implementslots_moved()(updating a persistent in-place GPUcache_b_buffer so captured CUDA/Metal graphs keep valid pointers across dynamic expert swaps, allocating nothing whencache_bias == 0) and dispatchkGateSoftmaxLogitAddinroute_card()whenopt_.cache_bias > 0.f && cache_b_ && !all_cached() && n < kStageRows(plus!block_pass_onQwen35).engine/metal/poly_tests/basic_parity.cpp,engine/metal/poly_tests/memory_growth.cpp):test_route_gate4andCacheBiasRouteEnginecovering full parity vsEngine::route_cached(),cache_bias == 0non-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 alltest_route_gate4cases)./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— PASSgit diff --checkandpython3 tools/test_dropped_paths.py— PASSFailed
Windows / Python tests/hit unrelated Windows terminal timing flakes intest_update_with_modelandtest_bench; this PR touches zero Python files).Not Run
engine/src/poly/kernels.cu(not available on macOS; statically verified line-by-line againstgate == 0warp primitives and degenerate-row handling — maintainerrun-backendsCI check needed before merge).Qwen3.5GGUF (covered by syntheticpoly_tiny_qwen35moe_protocolandCacheBiasRouteEnginetests; real-weight verification was onQwen4-35B-A3B).Limits & Default-Path Preservation
--cache-bias > 0. When--cache-bias 0(the default),slots_moved()returns immediately without allocatingcache_b_,route_card()dispatches the existinggate == 0path byte-identically, andbell_on()/picks_like_alone()behavior is unchanged.