Repository navigation
perf(moe): promote recurring hybrid misses into empty cache slots - #674
Open
taking-lying-flat wants to merge 1 commit into
Open
taking-lying-flat wants to merge 1 commit into
taking-lying-flat wants to merge 1 commit into
Conversation
KarrAcaRn
pushed a commit
to KarrAcaRn/FreeToken-ByAI
that referenced
this pull request
Oct 21, 2026
FlashML-org#676, FlashML-org#674, FlashML-org#672, FlashML-org#667 deferred (drafts), FlashML-org#616 superseded by next's shared gate. Assisted-by: Claude Opus 5.5
taking-lying-flat
marked this pull request as ready for review
October 24, 2026 00:56
This branch has not been deployed
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.
With hybrid auto-fetch, a single miss at
fetch_fraction=0.4rounds to zero fetches. A repeatedly used expert can therefore keep running on the CPU while GPU cache slots remain empty. Recency prioritization cannot help when the fetch count stays zero.Allow the recency policy to promote one previously used missing expert into an empty slot when the automatic split selects zero. This lets a stable working set finish warming the cache. The CPU reference and Triton kernel use the same rule; explicit fixed caps,
lowest_id, and full-cache eviction decisions retain their existing behavior.Only
python/freetoken/moe/offload_kernels.pyis changed. No test files are included.Validation
Compared against
mainat9c635a157798da4aa43e6d051e92620521ed1858on an NVIDIA RTX A1000 Laptop GPU (4 GiB, driver 595.91.07), Intel Core i7-12800HX, Linux x86_64, Python 3.12.13, PyTorch 2.12.1+cu130, Triton 3.7.1.Executed the upstream CPU reference and the actual CUDA Triton cache-management kernel with metadata fixtures matching the cache tensor layouts. The unrelated
flashlibimport was omitted from the standalone harness; the hybrid function bodies were executed unchanged. No model checkpoint, expert-weight transfer, GEMM, CPU expert executor, or end-to-end serving benchmark was involved.At fraction 0.4, 8 layers, 16 experts per layer, 64 slots, top-8, and 256 decode steps per active layer:
The cold-cache scenario becomes fully resident after the fifth step on each layer. CPU/CUDA state and routing agree. Additional checks passed for fixed caps 0/1,
lowest_id, a full cache, recurring-expert selection among first-time misses, and CUDA Graph replay using live cache state.Metadata-only A/B timings (microseconds per input-id copy plus cache-management kernel, CUDA Graph, median of 9 alternating trials, each with 20 replays of 128 calls):
The additional empty-slot check has a measurable cost. These are management-overhead measurements, not an end-to-end speedup claim; model-level tokens/s remains unmeasured.
Existing test expectation to update
test_hybrid_fraction_gpu_matches_cpu_referencecurrently requires the fetched count to equal the stateless_balanced_fetch(...)result on every step. That assertion encodes the zero-fetch behavior this PR changes. Replaying its seed-0 routing sequence with the production CPU reference gives five policy-expectation differences (zero-based steps 16, 17, 18, 21, 34): one miss is promoted instead of the old expected zero. CPU/GPU parity remains checked separately.This production-code-only PR leaves that test file unchanged. The policy assertion needs to account for the empty-slot recurring-miss exception before the existing test can pass under the new behavior. The repository's requested model-level end-to-end A/B is also outstanding.
Standalone reproduction command (PyTorch and Triton required)
Run from the repository root on both the base revision and this branch:
Expected on main:
fetches=0, cpu_routes=256, resident=Falseon both devices.Expected on this branch:
fetches=1, cpu_routes=1, resident=Trueon both devices.AI-assisted implementation and validation.