Skip to content

examples : add moe-trace to log MoE expert routing decisions - #28544

Open
aryan0078 wants to merge 1 commit into
ggml-org:masterfrom
aryan0078:add-moe-trace-example
Open

aryan0078 wants to merge 1 commit into
ggml-org:masterfrom
aryan0078:add-moe-trace-example

Conversation

@aryan0078

Copy link
Copy Markdown

Overview

Adds examples/moe-trace: a small diagnostic that logs which routed experts a
MoE model selects — one layer,expert CSV row per routed activation — for a
prompt evaluation plus a short greedy continuation.

It hooks the scheduler eval callback and downloads only the ffn_moe_topk id
tensors (one tiny i32 tensor per MoE layer per ubatch), so tracing adds
negligible overhead to the evaluation itself.

Motivation: expert-routing statistics decide whether hot-expert strategies
(caching, pinning, offload placement) can work for a given model, and that is
worth measuring on a real workload before implementing anything. Concretely,
tracing Qwen3.8-Flash-Next (512 experts, top-10) showed 98.6% of all 24,576
(layer, expert) slots active within ~1.3k tokens and a hottest-slot share of
only ~2.5x uniform — i.e. no exploitable hot set, which ruled out a planned
cache implementation in an hour instead of after days of building it.

Usage and aggregation one-liners are in the example's README.

Additional information

Output goes to MOE_TRACE_OUT (default moe_trace.csv). The tool only reads
tensors named ffn_moe_topk*, which build_moe_ffn names uniformly across
MoE architectures.

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES — the code and this description were prepared with
    an AI assistant (Claude Code). I reviewed the diff and ran the tool against
    real models on my machine (RX 6700, Vulkan; i7-12700).

🤖 Generated with Claude Code

Captures the ffn_moe_topk id tensors via the eval callback and writes one
layer,expert CSV row per routed activation, for a prompt evaluation plus a
short greedy continuation. Measures expert-routing skew on a real workload,
which decides whether hot-expert caching/pinning/placement strategies can
work for a given model before implementing them.

Co-Authored-By: Claude Opus 5 <[email protected]>
@ggml-gh-bot

ggml-gh-bot Bot commented Sep 7, 2026

Copy link
Copy Markdown

Hi @aryan0078, thanks for your contribution!

Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:

  • Multiple open PRs from a new contributor: We limit new contributors (those without a previously merged PR) to 1 open PR at a time. You currently have 3 open PRs.

  • AI-generated content: While code is allowed to be generated by AI, please write the PR description and commit messages on your own without the help of AI.


Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation examples

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant