Repository navigation
Conversation
Add llama_moe_cache_slots(ctx, il, n_tokens, n_expert_used), which returns the host slot map of layer il (one int32 per expert, -1 if the expert is not on the device) when a batch of that shape reads the layer's experts from the MoE cache, and NULL otherwise, under the same conditions as the graph's own lookup (build_moe_cache_slots), including the device's support for it. An expert streamer that keeps the host copy of the experts uses it to leave out the experts the device already holds. Read between the layer's expert selection and its lookup, a selected expert that has a slot keeps it through that layer's uploads: moe_cache_slru::plan marks every selected expert as seen before it picks a victim, and victim() never returns a slot of the same layer holding a seen expert, so neither a hit nor an earlier fill of the batch is evicted, and the host weights of a hit are never read. LLAMA_HAS_MOE_CACHE_SLOTS lets a caller build against a llama.cpp without it.
Owner
Author
|
Closing with Helldez/BigMoeOnEdge#433 and ggml-org#434: the CPU pass ran after the device pass, not beside it, and the measured gain did not justify a second fork commit. |
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.
Experimental, for BigMoeOnEdge's expert streamer. One commit on top of the expert-ready hook.
What
llama_moe_cache_slots(ctx, il, n_tokens, n_expert_used)returns the MoE cache's host slot map for layeril(oneint32per expert,-1= not on the device), orNULLwhen a batch of that shape does not read the layer's experts from the cache. The conditions mirrorbuild_moe_cache_slots: the layer is cached, the batch is at most 32 tokens and fits the slots, and the device supports the lookup.LLAMA_HAS_MOE_CACHE_SLOTSlets a caller build against a llama.cpp without it.Why
The streamer keeps the host copy of the experts and has to make every routed expert resident before the copy callback uploads the misses. It cannot see what the device already holds, so its RAM cache duplicates the VRAM cache. With this accessor it leaves the device's experts out.
Correctness (the eviction question)
The streamer reads the map from the eval callback at the layer's
ffn_moe_topknode. That is after selection and before the lookup split whose copy callback runsmoe_cache_slru::plan. No other plan can run in between, because the lookup split of layerilis the next one to copy a slot map. Could the plan forilevict a hit that the streamer skipped?plansetsseen[e] = seen_genfor every selected expert before it does anything else.touched (moved to the protected segment, and possibly demoted back to probation by the 50/50 rule). Neither changeskey_ofor the slot map.victim(il).victimskips any slot whose key is in layeriland whose expert isseenin this generation. That covers every hit of the batch and every fill made earlier in the same plan. It aborts rather than return such a slot.So a selected expert that has a slot when the layer's lookup starts keeps it through that plan, and its host weights are never read for that batch. A plan for a later layer can evict it, so the map says nothing about later layers or the next token. The caller must read it per layer, at that point.