<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>Dinesh - Neural Networks</title><link href="https://www.mdinesh.com/" rel="alternate"/><link href="https://www.mdinesh.com/feeds/neural-networks.atom.xml" rel="self"/><id>https://www.mdinesh.com/</id><updated>2026-11-01T10:00:00+05:30</updated><entry><title>Mixture of Experts</title><link href="https://www.mdinesh.com/mixture-of-experts.html" rel="alternate"/><published>2026-11-01T10:00:00+05:30</published><updated>2026-11-01T10:00:00+05:30</updated><author><name>Dinesh</name></author><id>tag:www.mdinesh.com,2026-11-01:/mixture-of-experts.html</id><summary type="html">&lt;p&gt;A deep dive into Mixture of Experts.&lt;/p&gt;</summary><content type="html">&lt;p&gt;In this post, I will do a deep dive into Mixture of Experts, a sparse neural network which replaces a transformer's feed-forward block with multiple parallel "expert" sub-networks, which helps in saving compute when we process tokens.&lt;/p&gt;
&lt;h3&gt;Why does it matter?&lt;/h3&gt;

&lt;p&gt;Large-scale AI models often contain billions to trillions of parameters. Parameters are basically the weights and biases that the model learns during the training phase. When we have to pass a single token through the network, we don't want to pass it through the entire network, which can be computationally expensive and time-consuming, which is basically what a dense network does.&lt;/p&gt;
&lt;p&gt;Mixture of Experts, on the other hand, activates experts, usually sub-networks of the whole network, depending on the input token, through what is called a gate, which I will discuss later in the article.&lt;/p&gt;
&lt;h3&gt;Dense vs. Sparse Networks&lt;/h3&gt;

&lt;p&gt;Parameters are the &lt;u&gt;weights and biases&lt;/u&gt; that the network learned during training.&lt;/p&gt;
&lt;p&gt;First of all, let's see what a dense vs. sparse network looks like.&lt;/p&gt;
&lt;p&gt;&lt;img src="images/mixture_of_experts/dense_and_sparse_network.png" alt="Sparse Network" style="display: block; margin: 0 auto; width: 650px; height: 700px;"/&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Dense Network&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;In a dense network, every parameter of the network is used by the token. When the token reaches the feed-forward block, the token is multiplied through the full weight matrix of the network, which can be quite expensive. We need to run each token through the entire network every time. For example, if there is a 70 billion parameter dense model, then it has to do 70 billion parameters' worth of matrix multiplication and addition for each token.&lt;/p&gt;
&lt;p&gt;Every time we pass a token over each parameter, we need two operations — multiply and add. We are computing two floating-point operations, so for each token we need 2 FLOPs per parameter, and assuming a 70 billion parameter dense model, we need 140 billion FLOPs, which is too expensive computationally.&lt;/p&gt;
&lt;p&gt;&lt;img src="images/mixture_of_experts/dense_network_matrix_multiplication.png" alt="Sparse Network" style="display: block; margin: 0 auto; width: 350px; height: 400px;"/&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Sparse Network&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;In a sparse network, when we pass the token through the network, a few subgroups of the network called experts get activated. So we need not do the arithmetic operation on the entire network's parameters. We do the arithmetic operation using the parameters of the activated experts. In the above sparse network (Mixture of Experts) visual, you can see that Expert 1 and Expert 2 got activated by the router/gate based on the given input token X.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;MoE replaces FFN in the Transformer block&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src="images/mixture_of_experts/MoE_in_Transformer_Block.png" alt="MoE in Transformer" style="display: block; margin: 0 auto; width: 450px; height: 500px;"/&gt;&lt;/p&gt;
&lt;p&gt;Each transformer layer has two sub-layers — multi-head attention and a feed-forward network — along with a residual connection and a normalization step. Mixture of Experts replaces the feed-forward network of the transformer block.&lt;/p&gt;
&lt;p&gt;The feed-forward network holds the factual knowledge, for example:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;The&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;capital&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kr"&gt;of&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;India&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;is&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;New&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Delhi&lt;/span&gt;
&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;The&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;closest&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;star&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Earth&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;is&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Sun&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;These facts are learned by the model from training data and converted to weights at each neuron. So when we pass a token through the FFN, it acts as a lookup table.&lt;/p&gt;
&lt;p&gt;We replace this FFN block with Mixture of Experts because we need this factual knowledge, and we only activate parts of the network, called experts, based on the content of the input token. When we only activate a certain number (K) of experts, we are making the lookup cheaper than the FFN. So Mixture of Experts is like a trick that scales the factual knowledge store.&lt;/p&gt;
&lt;p&gt;SwiGLU:&lt;/p&gt;
&lt;p&gt;It is like a smooth version of ReLU&lt;/p&gt;
&lt;p&gt;&lt;img src="images/mixture_of_experts/Contents_of_each_Expert.png" alt="Content of Each Expert" style="display: block; margin: 0 auto; width: 450px; height: 500px;"/&gt;&lt;/p&gt;
&lt;p&gt;Each Mixture of Experts block contains a router and experts, and inside each expert we use the SwiGLU activation function. ReLU converts all values below 0 to 0, whereas SwiGLU creates two projections of the input, passes one projection through the Swish activation (Swish(x) = x * Sigmoid(x)), and multiplies the two projections element-wise before creating a final down-projection (W_down). Compared to ReLU, which converts all negative values to 0, SwiGLU is much smoother and lets a little negative signal pass through the network.&lt;/p&gt;
&lt;p&gt;&lt;img src="images/mixture_of_experts/ReLU_versus_SwiGLU_activation_function.png" alt="ReLU versus SwiGLU activation function" style="display: block; margin: 0 auto; width: 550px; height: 400px;"/&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Gate/Router weights:&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Gate weights are computed for every new token x, and they decide how much each selected expert contributes. Based on these weights, after the gate/router scores the experts and the top-K experts are chosen, we use softmax on these selected scores to produce weights (that sum to 1, because the sum of probabilities is 1). We can easily spot the confidence of the router; usually, experts that get a higher probability mean that the router is much more confident in these experts. For example, a router is confident in the expert which gets 0.9 probability and less confident in the expert that got 0.1 probability.&lt;/p&gt;
&lt;p&gt;&lt;img src="images/mixture_of_experts/gated_weights.png" alt="Gated Weights" style="display: block; margin: 0 auto; width: 550px; height: 400px;"/&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;How to choose the "K" number of experts&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;There is no automated way to choose K. It is a hyperparameter that needs to be experimented with.&lt;/p&gt;
&lt;p&gt;&lt;img src="images/mixture_of_experts/K_number_of_experts.png" alt="K number of experts" style="display: block; margin: 0 auto; width: 550px; height: 400px;"/&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Example of MoE applied to next-word prediction&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src="images/mixture_of_experts/Next_Word_Prediction_Example.png" alt="Example of MoE with Next Word Prediction" style="display: block; margin: 0 auto; width: 550px; height: 400px;"/&gt;&lt;/p&gt;
&lt;p&gt;As each token passes through the layer, the router sends this token to the selected experts. For example, as you can see in the above image, the token "capital" goes through two experts, E1 and E6, and the token "India" goes through two experts, E4 and E6. After the final layer, the last token's representation is converted into a probability distribution, and the word with the highest probability, which is New Delhi with 0.71, gets selected.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Comparison of a few large-scale AI models, if they were dense vs. sparse&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src="images/mixture_of_experts/Frontier Models - Dense vs Sparse Compute usage.png" alt="Frontier Models - Dense vs Sparse Compute usage" style="display: block; margin: 0 auto; width: 650px; height: 500px;"/&gt;&lt;/p&gt;
&lt;p&gt;We already discussed that the FFN will multiply and add the token through the entire FFN weights, whereas the MoE model routes the token through the weights of the activated experts only.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Model&lt;/th&gt;
&lt;th&gt;If Dense (100%)&lt;/th&gt;
&lt;th&gt;MoE = active/token&lt;/th&gt;
&lt;th&gt;Share&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Kimi K3&lt;/td&gt;
&lt;td&gt;2.9T&lt;/td&gt;
&lt;td&gt;104B&lt;/td&gt;
&lt;td&gt;3.7%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;DeepSeek V4 Pro&lt;/td&gt;
&lt;td&gt;1.6T&lt;/td&gt;
&lt;td&gt;49B&lt;/td&gt;
&lt;td&gt;3.1%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Qwen 3.6 - 35B A3B&lt;/td&gt;
&lt;td&gt;35B&lt;/td&gt;
&lt;td&gt;3B&lt;/td&gt;
&lt;td&gt;8.6%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Active/token — these are the neurons which get active when we pass a token through the network.&lt;/p&gt;
&lt;p&gt;MoE share is calculated based on the active/token neurons. (share = (active per token / total params) * 100%)&lt;/p&gt;
&lt;p&gt;&lt;b&gt;What about the Compute Saved?&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src="images/mixture_of_experts/Kimi K3 Compute Usage.png" alt="Kimi K3 Compute Usage" style="display: block; margin: 0 auto; width: 650px; height: 500px;"/&gt;&lt;/p&gt;
&lt;p&gt;One FLOP (floating-point operation) is one multiply and one addition.&lt;/p&gt;
&lt;p&gt;And 1 GFLOP = 1 billion FLOPs. (And even though it is out of scope, 1 TFLOP = 1 trillion FLOPs.)&lt;/p&gt;
&lt;p&gt;I already discussed earlier in the article what a FLOP is, and I will do so again with an example here.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="nv"&gt;y1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;w11&lt;span class="w"&gt; &lt;/span&gt;*&lt;span class="w"&gt; &lt;/span&gt;x1&lt;span class="w"&gt; &lt;/span&gt;+&lt;span class="w"&gt; &lt;/span&gt;w12&lt;span class="w"&gt; &lt;/span&gt;*&lt;span class="w"&gt; &lt;/span&gt;x2&lt;span class="w"&gt; &lt;/span&gt;+&lt;span class="w"&gt; &lt;/span&gt;w13&lt;span class="w"&gt; &lt;/span&gt;*&lt;span class="w"&gt; &lt;/span&gt;x3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In the above equation, we are computing 7 FLOPs — 4 multiplications and 3 additions — which is 0.000000007 GFLOPs. To convert to GFLOPs: 7 FLOPs / 1 billion = 0.000000007 GFLOPs.&lt;/p&gt;
&lt;p&gt;In the above image we can see that if Kimi K3 were a dense network, it would consume 5600 GFLOPs per token, whereas if it were a Mixture of Experts, it would consume 208 GFLOPs per token, which is a 27x savings in computation.&lt;/p&gt;
&lt;p&gt;Memory is still the same between dense vs. sparse; all the neurons still hold all the parameters. No change in that.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Load Balancing the Experts&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src="images/mixture_of_experts/Load Balancing Experts.png" alt="Load Balancing Experts" style="display: block; margin: 0 auto; width: 650px; height: 500px;"/&gt;&lt;/p&gt;
&lt;p&gt;During the early phase of training, a few experts may get slightly more traffic and improve faster because they see more tokens, and they end up getting more traffic, like a rich-get-richer situation.&lt;/p&gt;
&lt;p&gt;And some experts become dead because they get very little traffic.&lt;/p&gt;
&lt;p&gt;To overcome this, we add an auxiliary load-balancing loss which penalizes the imbalance by multiplying, for each expert, the fraction of tokens sent to it by the average routing probability, then summing over all experts.&lt;/p&gt;
&lt;div class="math"&gt;$$L_{\text{aux}} = \alpha \cdot N \sum_{i=1}^{N} f_i \cdot P_i$$&lt;/div&gt;
&lt;p&gt;Here,&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="math"&gt;\(f_i\)&lt;/span&gt; = fraction of tokens sent to the expert&lt;/li&gt;
&lt;li&gt;&lt;span class="math"&gt;\(P_i\)&lt;/span&gt; = Mean Router probability&lt;/li&gt;
&lt;li&gt;&lt;span class="math"&gt;\(N\)&lt;/span&gt; = Number of experts in the layer&lt;/li&gt;
&lt;li&gt;&lt;span class="math"&gt;\(i\)&lt;/span&gt; = Expert index, from 1 to N&lt;/li&gt;
&lt;li&gt;&lt;span class="math"&gt;\(\alpha\)&lt;/span&gt; = a small weight, which is a hyperparameter, that sets how strongly balancing is pushed.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src="images/mixture_of_experts/Load Balancing Update Weights.png" alt="Load Balancing Update Weights" style="display: block; margin: 0 auto; width: 650px; height: 500px;"/&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Fine-Grained Experts and Shared Experts&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Large LLM models use a combination of fine-grained and shared experts.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Model&lt;/th&gt;
&lt;th&gt;Routed Experts&lt;/th&gt;
&lt;th&gt;Shared Experts&lt;/th&gt;
&lt;th&gt;Active per Token&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Kimi K3&lt;/td&gt;
&lt;td&gt;894&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;18 (16 routed + 2 shared)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;DeepSeek V4 Pro&lt;/td&gt;
&lt;td&gt;256&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;9 (8 routed + 1 shared)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Qwen 3.6 - 35B A3B&lt;/td&gt;
&lt;td&gt;256&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;9 (8 routed + 1 shared)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;img src="images/mixture_of_experts/Fine Grained and Shared Expert.png" alt="Fine Grained and Shared Expert" style="display: block; margin: 0 auto; width: 600px; height: 500px;"/&gt;&lt;/p&gt;
&lt;p&gt;A shared expert is an always-on expert that every token passes through, without any routing. We need shared experts because some knowledge is common, like basic syntax, frequent patterns, or some general structure in the data. Without a shared expert, each routed expert has to independently handle and store this common knowledge, which might cause duplication and wasted capacity and memory.&lt;/p&gt;
&lt;p&gt;A fine-grained expert is an expert chosen by the router to learn specific knowledge. These often go together in a Mixture of Experts.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;How do we scale MoE across multiple GPUs?&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src="images/mixture_of_experts/Multi GPU.png" alt="Multi GPUs" style="display: block; margin: 0 auto; width: 600px; height: 500px;"/&gt;&lt;/p&gt;
&lt;p&gt;We cannot fit all the experts in one GPU, so we have to spread these experts across multiple GPUs. Each GPU keeps just a few of the MoE layer's experts. The router looks at every token and decides which expert each token has to go to, and then the corresponding GPU will send the token across the network to whichever GPU holds that chosen expert. Each GPU now has a pile of tokens, computes the arithmetic, and sends the computation back to the original GPU the token originated from.&lt;/p&gt;
&lt;script type="text/javascript"&gt;if (!document.getElementById('mathjaxscript_pelican_#%@#$@#')) {
    var align = "center",
        indent = "0em",
        linebreak = "false";

    if (false) {
        align = (screen.width &lt; 768) ? "left" : align;
        indent = (screen.width &lt; 768) ? "0em" : indent;
        linebreak = (screen.width &lt; 768) ? 'true' : linebreak;
    }

    var mathjaxscript = document.createElement('script');
    mathjaxscript.id = 'mathjaxscript_pelican_#%@#$@#';
    mathjaxscript.type = 'text/javascript';
    mathjaxscript.src = 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=TeX-AMS-MML_HTMLorMML';

    var configscript = document.createElement('script');
    configscript.type = 'text/x-mathjax-config';
    configscript[(window.opera ? "innerHTML" : "text")] =
        "MathJax.Hub.Config({" +
        "    config: ['MMLorHTML.js']," +
        "    TeX: { extensions: ['AMSmath.js','AMSsymbols.js','noErrors.js','noUndefined.js'], equationNumbers: { autoNumber: 'none' } }," +
        "    jax: ['input/TeX','input/MathML','output/HTML-CSS']," +
        "    extensions: ['tex2jax.js','mml2jax.js','MathMenu.js','MathZoom.js']," +
        "    displayAlign: '"+ align +"'," +
        "    displayIndent: '"+ indent +"'," +
        "    showMathMenu: true," +
        "    messageStyle: 'normal'," +
        "    tex2jax: { " +
        "        inlineMath: [ ['\\\\(','\\\\)'] ], " +
        "        displayMath: [ ['$$','$$'] ]," +
        "        processEscapes: true," +
        "        preview: 'TeX'," +
        "    }, " +
        "    'HTML-CSS': { " +
        "        availableFonts: ['STIX', 'TeX']," +
        "        preferredFont: 'STIX'," +
        "        styles: { '.MathJax_Display, .MathJax .mo, .MathJax .mi, .MathJax .mn': {color: 'inherit ! important'} }," +
        "        linebreaks: { automatic: "+ linebreak +", width: '90% container' }," +
        "    }, " +
        "}); " +
        "if ('default' !== 'default') {" +
            "MathJax.Hub.Register.StartupHook('HTML-CSS Jax Ready',function () {" +
                "var VARIANT = MathJax.OutputJax['HTML-CSS'].FONTDATA.VARIANT;" +
                "VARIANT['normal'].fonts.unshift('MathJax_default');" +
                "VARIANT['bold'].fonts.unshift('MathJax_default-bold');" +
                "VARIANT['italic'].fonts.unshift('MathJax_default-italic');" +
                "VARIANT['-tex-mathit'].fonts.unshift('MathJax_default-italic');" +
            "});" +
            "MathJax.Hub.Register.StartupHook('SVG Jax Ready',function () {" +
                "var VARIANT = MathJax.OutputJax.SVG.FONTDATA.VARIANT;" +
                "VARIANT['normal'].fonts.unshift('MathJax_default');" +
                "VARIANT['bold'].fonts.unshift('MathJax_default-bold');" +
                "VARIANT['italic'].fonts.unshift('MathJax_default-italic');" +
                "VARIANT['-tex-mathit'].fonts.unshift('MathJax_default-italic');" +
            "});" +
        "}";

    (document.body || document.getElementsByTagName('head')[0]).appendChild(configscript);
    (document.body || document.getElementsByTagName('head')[0]).appendChild(mathjaxscript);
}
&lt;/script&gt;</content><category term="Neural Networks"/><category term="Neural Networks"/><category term="Inference Engineering"/></entry></feed>