I Tried Logging Which Experts the MoE Router Selects on the NVIDIA DGX Spark

I Tried Logging Which Experts the MoE Router Selects on the NVIDIA DGX Spark

We examined how MoE (Mixture of Experts) routers select experts, using Japanese, English and code texts. Comparing several models, we explain with measured data how language and topic affect expert selection, and how load-balancing loss calculation changes specialization.
2026.12.05

This page has been translated by machine translation. View original

Introduction

Hello, this is Shimada from the Manufacturing Business Technology Department at Classmethod.

This time, I investigate the router in Mixture of Experts (MoE).
In an MoE model, the router chooses experts for each token.
So what does the router rely on when it chooses?
Whether the chosen experts change between Japanese and English, or with the topic of a sentence, cannot be determined just by reading an explanation of the mechanism.

Prior studies disagree on whether experts split by topic.
The Mixtral paper reports that no clear per-domain tendency was observed, and that the selection seems to follow syntax more than domain[1].
On the other hand, the OLMoE paper reports that experts clearly split by domain, such as arXiv and GitHub, and shows this in a figure alongside the Mixtral results[2].

So I fed Japanese, English, and code text into MoE models and recorded, token by token, which experts the router chose.
I used a single NVIDIA DGX Spark.
I also measured memory usage and generation speed for MoE and dense models, which I cover in the latter half of the article.

What the MoE router does

In a dense model, every token passes through the same FFN in each layer.
An MoE model replaces the FFN with multiple FFNs and uses only some of them for each token.
Each of the replaced FFNs is called an expert.
The router decides which experts to use.
The router computes a score for each expert from the token's hidden state and picks the top k.
The main models used this time pick 8 out of 128 experts.

Because of this mechanism, MoE distinguishes total parameters from active parameters.
All the weights must be kept in memory, but within the FFN part, only the selected experts are used to compute a single token.
The attention and embedding layers are used every time for every token, so the active parameters are this shared part plus the 8 selected experts.

During training, to prevent tokens from concentrating on particular experts, a load balancing loss is added to the original loss as an auxiliary loss.
Depending on the scope over which this loss is computed, there are two options: micro-batch and global batch.
A micro-batch refers to what one GPU processes in one forward pass.
A global batch refers to the entire set of sequences used before the weights are updated once.
The Qwen team reports that computing this loss over the whole global batch, rather than per micro-batch, makes experts more likely to split by domain[3].
According to the paper, only a few sequences fit in a micro-batch, so a per-micro-batch loss is almost the same as requiring balance sequence by sequence.
As a result, even for a sequence from a single domain such as code, the model is required to spread tokens across all experts, which makes it harder for experts to split by domain.

Models used

Model Type Total parameters Active Layers Experts
llm-jp-4-32b-a3b-base MoE 32.1B 3.8B 32 8 of 128
llm-jp-4-33b-base dense 33.2B 33.2B 64 None
Qwen3-30B-A3B-Base MoE 30.5B 3.3B 48 8 of 128
OLMoE-1B-7B-0924 MoE 6.9B 1.3B 16 8 of 64

The main models I used are the two LLM-jp-4 models released by LLM-jp at the National Institute of Informatics.
The technical report says the 32B-A3B MoE and the 8B dense model were trained with the same corpus, the same mixture, and the same tokenizer[4].
The 33B dense model used here has no results in the report, but according to its model card, its training token count and corpus are the same as 32B-A3B.
However, the 33B dense model is a Llama-style model with 64 layers, so the number of layers also differs from the MoE's 32 layers.

Qwen3-30B-A3B was used for comparison with the LLM-jp-4 MoE.
The LLM-jp-4 MoE has the same architecture as the Qwen3 MoE, and the number of experts is also the same.
It is also loaded with the same class in Hugging Face transformers.
The main differences are size, such as the number of layers, the training data, the tokenizer, and how the load balancing loss is computed.
The Qwen3 technical report says it used a global-batch load balancing loss[5].
The LLM-jp-4 technical report only says the loss coefficient was set to 0.01, the same as OLMoE, and does not say which method was used.
So I looked at the training configuration published by LLM-jp.
In the 32B-A3B configuration, --moe-router-load-balancing-type aux_loss is specified at every training phase, and the micro-batch is one or two sequences[6].
The sequence length is 4,096, 16,384, or 65,536 tokens depending on the phase.
The aux_loss in Megatron-LM, which LLM-jp uses, counts the number of tokens per expert within the micro-batch.
In other words, LLM-jp-4 is trained with a per-micro-batch load balancing loss.

OLMoE-1B-7B is the model from the paper that reported that experts split by domain.
To check with this article's measurement method whether experts split by domain as the paper reports, I only recorded the router.
The OLMoE paper only says the load balancing loss is computed "for one batch" and does not say whether it is a micro-batch or a global batch[7].

All were BF16 base models used without quantization.
I chose base models rather than chat models because I only feed fixed text and look at the router's choices, with no need to generate text.

Recording the router's choices

I captured the router's choices by loading the models with transformers and attaching a forward hook to the router of each layer.
In the Qwen3 MoE in transformers 5.15.0, the router's forward returns the scores, the weights, and the indices of the selected experts.
The hook saves only the third one, the indices of the selected experts.
For OLMoE as well, I attached hooks to the router of each layer in the same way.

record_router.py
layers = model.model.layers
captured = {}

def make_hook(layer_idx):
    def hook(_module, _inputs, output):
        router_logits, router_scores, router_indices = output
        captured[layer_idx] = router_indices.cpu().numpy()
    return hook

for i, layer in enumerate(layers):
    layer.mlp.gate.register_forward_hook(make_hook(i))

ids = tok(text, add_special_tokens=False, return_tensors="pt").input_ids[:, :2048]
with torch.no_grad():
    model(ids.to("cuda"))
# captured[i] is an array of (number of tokens, 8), holding the indices of the experts each token chose at layer i

I prepared four kinds of text.

  • Japanese: 11 articles from Japanese Wikipedia
  • English: 11 articles from English Wikipedia on the same topics as the 11 Japanese articles
  • Code: 6 files from the CPython 3.12 standard library
  • Code without comments: the 6 files above with docstrings and # comments removed

The Wikipedia topics are Transformer, Mount Fuji, photosynthesis, quantum mechanics, economics, the history of Japan, soccer, sushi, Tokyo, Beethoven, and the heart.
I paired the Japanese and English articles on the same topics.
This lets me separate the effect of language from the effect of topic.
The code files are json/decoder.py, argparse.py, http/client.py, csv.py, textwrap.py, and logging/__init__.py.
Standard library files contain many docstrings and comments written in English.
To check whether the code results were influenced by that English text, I measured both the original files and the files with docstrings and comments removed.

Every text was cut to its first 2048 tokens and fed in a single forward pass without a chat template.
The only ones that did not reach 2048 tokens were two of the comment-free code files under Qwen3, at 2015 and 1864 tokens.
The tokenizers differ by model, so the amount of text that fits in 2048 tokens varies by model and language.
English articles fit about 9,300 characters with every model, while Japanese articles fit about 4,200 characters with llm-jp-4, about 2,900 with Qwen3, and about 1,900 with OLMoE.

Comparing how experts are chosen

Method of comparison

For each layer, I counted the 8 experts chosen by each token of a text once each, without using the router weights, and turned the counts into a distribution of expert usage.
I measure the difference between the distributions of two texts with the Jensen-Shannon divergence (hereafter JS).
Because I use a base-2 logarithm, JS is 0 for identical distributions and 1 for completely non-overlapping distributions.

The fewer tokens counted, the larger JS appears due to chance fluctuation.
So for every pair, I compared texts cut to half length.
Most pairs are 1024 tokens against 1024 tokens.
I compared the first half of text 1 with the second half of text 2, and the second half of text 1 with the first half of text 2, and averaged them.
This is because the beginning of an article and the rest contain different content, so comparing with only one combination would bias the value.
I took the JS between the first and second halves of the same text as a yardstick combining chance fluctuation and the difference in content within an article.

Results

The following table shows the JS averaged over the middle range when the layers are divided into three, and the value for the last layer.
The middle range, in terms of the horizontal-axis numbers in the figures, is layers 10 to 20 for llm-jp-4, 16 to 31 for Qwen3, and 5 to 9 for OLMoE.
Looking at the per-layer figures, in Qwen3 and OLMoE the difference between Japanese and English grows sharply in the last two or three layers.
In llm-jp-4, the difference between topics becomes small only in the last layer, falling below the difference between Japanese and English.
So I show the last layer separately.

Pair compared llm-jp-4 middle Last layer Qwen3 middle Last layer OLMoE middle Last layer
First and second halves of the same text 0.05 0.03 0.07 0.04 0.03 0.02
Japanese and English, same topic 0.08 0.17 0.23 0.84 0.17 0.56
Same language, different topic 0.13 0.07 0.34 0.10 0.11 0.12
English and code 0.14 0.09 0.65 0.46 0.34 0.39
Japanese and code 0.15 0.17 0.72 0.82 0.33 0.25
Code and code without comments 0.06 0.04 0.13 0.13 0.04 0.06

JS divergence per layer for llm-jp-4-32b-a3b
JS divergence per layer for llm-jp-4-32b-a3b

JS divergence per layer for Qwen3-30B-A3B
JS divergence per layer for Qwen3-30B-A3B

In the middle layers, experts split by topic rather than language

In the middle layers, for both llm-jp-4 and Qwen3, the difference between topics showed up larger than the difference between Japanese and English.
The pair of Japanese "富士山" and English "Mount Fuji" chooses more similar experts than the pair of Japanese "富士山" and Japanese "光合成".

I also checked by topic.
For each Japanese article, I averaged the JS against the 11 English articles over the middle layers and counted how often the English article with the smallest JS was the one on the same topic.
By chance, about 1 of the 11 topics would match.
In the middle layers, llm-jp-4 had the same-topic English article as the closest for all 11 topics, and Qwen3 for 9 topics.

This suggests that the router in the middle layers chooses experts based on information closer to what a sentence is about than to its language.
However, Japanese and English articles on the same topic tend to share the same tokens, such as Latin-letter spellings of proper nouns, mathematical formulas, and years, and it remains possible that these make the JS smaller.

In the last layer, experts split by language

In the last layer, the difference between languages became larger than the difference between topics.
In Qwen3, the JS between Japanese and English on the same topic rose to 0.84, far above 0.10 for different topics in the same language.
In llm-jp-4 as well, Japanese and English on the same topic is 0.17, and different topics in the same language is 0.07.

In the last layer of Qwen3, there was one expert chosen by 98.4% of Japanese tokens.
The same expert was chosen by 6.3% of tokens for both English and code.
If 8 of 128 experts were chosen uniformly, the share for one expert would be 6.25%, so this expert is not treated specially for English and code.

Share of times each expert was chosen by text type in the first, middle, and last layers of Qwen3-30B-A3B
Share of times each expert was chosen by text type in the first, middle, and last layers of Qwen3-30B-A3B. Uniform selection is taken as 1, and values of 8 or more share the same color. The experts on the horizontal axis are sorted in each row in descending order of bias. codestrip is code without comments

The last layer is the layer just before the next token is output.
It is where the language of the output token is determined, so it felt natural to me that experts split by language there.

Qwen3 split more clearly than llm-jp-4

For pairs that differ in language, topic, or text type, the JS of Qwen3 in the middle layers was about 2 to 5 times that of llm-jp-4.
The JS between the first and second halves of the same text in the middle layers is 0.05 for llm-jp-4 and 0.07 for Qwen3.
llm-jp-4 also splits more than chance fluctuation, but not as much as Qwen3.
The most biased expert in llm-jp-4 was one chosen by 28.2% of Japanese tokens in the last layer.
That is about 4.5 times the uniform 6.25%, but far lower than Qwen3's 98.4%.

Share of times each expert was chosen by text type in the first, middle, and last layers of llm-jp-4-32b-a3b
Share of times each expert was chosen by text type in the first, middle, and last layers of llm-jp-4-32b-a3b. Uniform selection is taken as 1, and values of 8 or more share the same color. The experts on the horizontal axis are sorted in each row in descending order of bias. codestrip is code without comments

Effect of code comments

In Qwen3, the JS between code and code without comments was 0.13.
This is larger than 0.04 to 0.07 for the first and second halves of the same text, so the presence of English docstrings and comments changes how experts are chosen.
Even so, the JS between English and code is 0.65 in the middle layers, much larger than the difference due to the presence of comments.
The JS between English and code without comments is 0.77, so removing comments widens the gap from English further.
In llm-jp-4 and OLMoE as well, removing comments increases the JS against English.
In every model, the fact that code chose experts different from English is not due to the English in the comments.
If anything, the comments brought the way code chooses experts closer to English.

Compared with the reports of prior studies

In OLMoE, the JS between English and code is 0.34 in the middle layers, more than 10 times the 0.03 between the first and second halves of the same text.
The tendency reported in the paper, that experts split between GitHub and general text, was also confirmed with this article's measurement method.

JS divergence per layer for OLMoE-1B-7B
JS divergence per layer for OLMoE-1B-7B

However, in OLMoE, even in the middle layers the difference between Japanese and English showed up larger than the difference between topics.
The same-topic English article was the closest for only 3 of the 11 topics.
In the last layer, 99.6% of Japanese tokens choose a single expert.
If 8 of 64 experts were chosen uniformly, this share would be 12.5%.
About 95% of OLMoE's training data by token count is DCLM-Baseline[8].
DCLM-Baseline is published as an English corpus[9].
I think that because there is little Japanese training data, the model handles Japanese sentences together regardless of topic, but I have not verified this.

OLMoE has 64 experts, different from the 128 of the other two, so the JS values cannot be lined up directly.
So I divided the middle-layer JS between English and code by the JS between the first and second halves of the same text, and used that for a rough comparison.
Dividing the unrounded values, it is about 13 times for OLMoE, about 9 times for Qwen3, and about 3 times for llm-jp-4.
The denominators are small, 0.03 to 0.07, and a slight change moves the ratio a lot, so I cannot say the difference between OLMoE's about 13 times and Qwen3's about 9 times is meaningful.
What can be said is that OLMoE and Qwen3 far exceed the JS between the first and second halves of the same text, while llm-jp-4's gap is small.
Qwen3 split in a way close to OLMoE, which was reported to split by domain.
In terms of the degree of splitting, llm-jp-4 is as weak as the Mixtral report.
However, llm-jp-4 also had the same-topic English article as the closest for all 11 topics in the middle layers, so it is not that it does not use topic as a cue.
Between Qwen3 and llm-jp-4, which have the same number of experts, Qwen3, trained with the global-batch load balancing loss, split more clearly than llm-jp-4, trained with micro-batches, which is consistent with the Qwen team's report.
For OLMoE, the loss method is unknown, so it cannot be used in this comparison.

The three models differ in both training data and size.
I did not compare two models that differ only in the load balancing loss method, and the amount of text that fits in 2048 tokens also differs by model and language, so I would like to treat the router recording results here as a rough guide.

Memory and speed of MoE and dense

Separately from recording the router, I also measured how the difference between MoE and dense appears in memory and speed.
Using vllm bench latency of vLLM, I fed 512 tokens per request, generated 256 tokens, and took the average of 5 runs.
The elapsed time also includes the time to process the 512 input tokens.

vllm bench latency --model <モデルのディレクトリ> --trust-remote-code --dtype bfloat16 \
  --max-model-len 4096 --kv-cache-memory-bytes 4294967296 --gpu-memory-utilization 0.75 \
  --batch-size 1 --input-len 512 --output-len 256 \
  --num-iters-warmup 2 --num-iters 5
Model Weight memory Time for 512-token input and 256-token generation Generation speed Tokens that fit in a 4GiB KV cache
llm-jp-4-33b (dense) 61.9GiB 70.5 s 3.6 tok/s 16,384
llm-jp-4-32b-a3b (MoE) 59.9GiB 8.9 s 28.8 tok/s 65,536
Qwen3-30B-A3B (MoE) 56.9GiB 8.2 s 31.1 tok/s 43,680

The values in the table were taken from the vLLM logs.
The lines for weight memory, the number of tokens that fit in the KV cache, and the average time extracted from the logs are as follows.
The process number and timestamp at the beginning of each line are omitted.

== bench-llmjp4-dense
Model loading took 61.89 GiB and 323.096662 seconds
GPU KV cache size: 16,384 tokens
Avg latency: 70.48910436942242 seconds
== bench-llmjp4-moe
Model loading took 59.91 GiB memory and 361.688885 seconds
GPU KV cache size: 65,536 tokens
Avg latency: 8.884756461810321 seconds
== bench-qwen3-moe
Model loading took 56.88 GiB memory and 352.312430 seconds
GPU KV cache size: 43,680 tokens
Avg latency: 8.226492861751467 seconds

Weight memory was almost the same for MoE and dense.
Even so, MoE generated about 8 times faster than dense.

In generation one request at a time, the speed is determined by the time to read the weights from memory for each token.
The memory bandwidth of the DGX Spark is 273GB/s[10].
The dense 33B reads about 66GB per token, so the upper limit is about 4.1 tok/s.
The MoE reads only the active 3.8B worth, about 7.6GB, so the upper limit is about 35.9 tok/s.
The measured values were 88% of the upper limit for dense and 80% for MoE.
The measured time includes input processing, so the generation-only speed should be a bit closer to the upper limit than this.
MoE speed is determined by active parameters rather than total parameters, so in this comparison the MoE could generate much faster.

The number of tokens that fit in the KV cache also differed by a factor of 4.
The size of the KV cache is proportional to the number of layers and the number of KV heads.
The llm-jp-4 MoE has 32 layers and 4 KV heads, while the dense model has 64 layers and 8 KV heads.
In both, the dimension of one KV head is 128, and one BF16 value is 2 bytes.
Multiplying by 2 for the two kinds, key and value, the KV cache per token is 64KiB for the MoE and 256KiB for the dense model.
This is not because it is an MoE, but because the two models differ in the number of layers and KV heads.

Conclusion

When I recorded the MoE router's choices on Japanese, English, and code, in llm-jp-4 and Qwen3 the experts split by topic rather than language in the middle layers, and split by language in the last layer.
What I found interesting in this experiment is that what the router emphasizes varies from layer to layer.

How strongly experts split differs greatly by model, and Qwen3, trained with the global-batch load balancing loss, split far more clearly than llm-jp-4, trained with micro-batches.
The training data also differs, so not all of the difference can be explained by how the loss is computed, but I found it interesting that MoE models differ this much in how they split.

Next, I plan to take the same recording with Qwen4-35B-A3B, released on December 3, 2026.
In addition to choosing 8 of 256 experts, Qwen4 has one shared expert, and its attention combines linear attention and sparse attention.
I would like to see how the way experts split changes compared with the llm-jp-4 and Qwen3 results this time.

References

脚注
  1. Section 5 of Mixtral of Experts. It says "we do not observe obvious patterns in the assignment of experts based on the topic." https://arxiv.org/abs/2401.04088 ↩︎

  2. Section 5.3 and Figure 22 of OLMoE. https://arxiv.org/abs/2409.02060 ↩︎

  3. Demons in the Detail: On Implementing Load Balancing Loss for Training Specialized Mixture-of-Expert Models. It says "the global-batch LBL also greatly improves the domain specialization of MoE experts." https://arxiv.org/abs/2501.11873 ↩︎

  4. Section 4.2.1 of the LLM-jp-4 technical report. It says "Within each phase, both models used the same corpus mixture, sampling weights, tokenizer, and sequence length" https://arxiv.org/abs/2610.19331 ↩︎

  5. Section 2 of the Qwen3 Technical Report. It says "we adopt the global-batch load balancing loss (Qiu et al., 2025) to encourage expert specialization." https://arxiv.org/abs/2505.09388 ↩︎

  6. The configurations starting with v4-32b-a3.8b- in pretrain/scripts/ of llm-jp/scripts. The link goes to the configuration for phase 1. https://github.com/llm-jp/scripts/blob/f09c81a31cee0d08c2d6a9b767e7a312dfadc046/pretrain/scripts/v4-32b-a3.8b-phase1/base/params.sh ↩︎

  7. Section 4.1.6 of the OLMoE paper. https://arxiv.org/abs/2409.02060 ↩︎

  8. Table 2 of the OLMoE paper. Of the total 4,060B tokens, DCLM-Baseline accounts for 3,860B tokens. https://arxiv.org/abs/2409.02060 ↩︎

  9. The dataset card for mlfoundations/dclm-baseline-1.0 on Hugging Face says "Language(s) (NLP): English". https://huggingface.co/datasets/mlfoundations/dclm-baseline-1.0 ↩︎

  10. The NVIDIA DGX Spark hardware documentation says "273 GB/s bandwidth". https://docs.nvidia.com/dgx/dgx-spark/hardware.html ↩︎


Claudeならクラスメソッドにお任せください

クラスメソッドは、Anthropic社とリセラー契約を締結しています。各種製品ガイドから、業種別の活用法、フェーズごとのお悩み解決などサービス支援ページにまとめております。まずはご覧いただき、お気軽にご相談ください。

サービス詳細を見る

Share this article