<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[LLM-From-Scratch-Study-Notes]]></title><description><![CDATA[LLM-From-Scratch-Study-Notes]]></description><link>https://llm-from-scratch.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Wed, 09 Sep 2026 06:47:52 GMT</lastBuildDate><atom:link href="https://llm-from-scratch.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Understanding Mixture-of-Experts (MoE) in Simple Terms]]></title><description><![CDATA[Why MoE Can Have Many FFNs Yet Use Less Memory & Compute
Large Language Models (LLMs) like GPT-OSS, Mixtral, and DeepSeek-V3/R1 use Mixture-of-Experts (MoE) layers to massively expand model capacity without increasing inference cost. But the mechanis...]]></description><link>https://llm-from-scratch.hashnode.dev/understanding-mixture-of-experts-moe-in-simple-terms</link><guid isPermaLink="true">https://llm-from-scratch.hashnode.dev/understanding-mixture-of-experts-moe-in-simple-terms</guid><dc:creator><![CDATA[Gasym A. Valiyev]]></dc:creator><pubDate>Mon, 24 Nov 2025 07:01:51 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1763967619940/a5179181-eeea-4c34-8e4a-0dd1a20b470a.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-why-moe-can-have-many-ffns-yet-use-less-memory-amp-compute"><em>Why MoE Can Have Many FFNs Yet Use Less Memory &amp; Compute</em></h3>
<p>Large Language Models (LLMs) like GPT-OSS, Mixtral, and DeepSeek-V3/R1 use <strong>Mixture-of-Experts (MoE)</strong> layers to massively expand model capacity <em>without increasing inference cost</em>. But the mechanism behind this is often confusing — especially when comparing dense vs. MoE feed-forward networks (FFNs).</p>
<p>This article explains the concept <strong>simply, concretely, and concisely</strong>, using an example where both dense and MoE versions have equal total parameters — and then explains why real MoE models have <em>far more</em> parameters overall.</p>
<hr />
<h1 id="heading-1-background-ffns-are-the-largest-part-of-a-transformer-block">1. Background: FFNs Are the Largest Part of a Transformer Block</h1>
<p>In a typical transformer block:</p>
<ul>
<li><p>The multi-head attention (MHA) is relatively small.</p>
</li>
<li><p>The <strong>feed-forward network (FFN)</strong> contains the <em>majority</em> of the model’s parameters.</p>
</li>
<li><p>This block is repeated many times (e.g., DeepSeek-V3 uses 61 layers).</p>
</li>
</ul>
<p>So modifying the FFN architecture has a huge impact on total model size and compute.</p>
<hr />
<h1 id="heading-2-dense-architecture-baseline">2. Dense Architecture (Baseline)</h1>
<p>In a dense transformer:</p>
<ul>
<li><p>There is <strong>one FFN per block</strong>.</p>
</li>
<li><p>Every token goes through <strong>all</strong> the FFN parameters every time.</p>
</li>
<li><p>Compute and memory scale with the <strong>full hidden dimension</strong>, every token, every layer.</p>
</li>
</ul>
<p>Example:<br />A dense FFN has <strong>308M parameters</strong> → <strong>all 308M</strong> are active per forward pass.</p>
<hr />
<h1 id="heading-3-moe-architecture-sparse">3. MoE Architecture (Sparse)</h1>
<p>Instead of one big FFN, MoE replaces it with <strong>many experts</strong>, each of which is its own FFN.</p>
<p>Example:</p>
<ul>
<li><p>8 experts</p>
</li>
<li><p>Each expert has <strong>38.5M parameters</strong></p>
</li>
<li><p>Total ≈ <strong>308M parameters</strong> (same as dense)</p>
</li>
</ul>
<p>But MoE adds a <strong>router</strong> that chooses only <em>top_k</em> experts per token.</p>
<p>For example:</p>
<p><code>num_experts = 8   top_k = 2</code></p>
<p>Only <strong>2 out of 8 experts</strong> are activated for each token.</p>
<p>That means:</p>
<ul>
<li><p>Total parameters: ~308M</p>
</li>
<li><p><strong>Active</strong> parameters per token: ~77M</p>
</li>
<li><p>The rest stay idle!</p>
</li>
</ul>
<p>This sparsity is why MoE is called <em>sparse</em>.</p>
<hr />
<h1 id="heading-4-why-moe-saves-memory-and-compute">4. Why MoE Saves Memory and Compute</h1>
<p>Even if total parameters equal the dense case, MoE saves compute because only a small fraction is used.</p>
<h3 id="heading-in-dense">In Dense:</h3>
<ul>
<li><p>100% of 308M active per token</p>
</li>
<li><p>Full activations must be stored</p>
</li>
<li><p>Full FLOPs needed</p>
</li>
</ul>
<h3 id="heading-in-moe">In MoE:</h3>
<ul>
<li><p>Only 2 experts × 38.5M = <strong>77M active parameters</strong></p>
</li>
<li><p>Activations only stored for active experts</p>
</li>
<li><p>FLOPs reduced by <strong>top_k / num_experts</strong> ratio<br />  (in this example, 2/8 = 25% of dense compute)</p>
</li>
</ul>
<p>This gives <strong>significantly lower memory and compute cost</strong> per token.</p>
<hr />
<h1 id="heading-5-then-why-do-people-say-moe-increases-total-parameters">5. Then Why Do People Say “MoE Increases Total Parameters”?</h1>
<p>This is the key point that confuses many readers.</p>
<p>👉 In theoretical demos or scripts, MoE is configured to have <strong>the same total parameters</strong> as the dense FFN.<br />This makes comparisons fair when measuring memory savings.</p>
<p>But…</p>
<hr />
<h1 id="heading-6-in-real-moe-models-capacity-is-greatly-increased">6. In Real MoE Models, Capacity Is <em>Greatly</em> Increased</h1>
<p>Real MoE models use many more experts, each larger, because <strong>we can afford it</strong> — we only activate a small subset.</p>
<p>Instead of 8 experts, a real MoE could have:</p>
<ul>
<li><p>64 experts</p>
</li>
<li><p>128 experts</p>
</li>
<li><p>256 experts (as in DeepSeek-V3)</p>
</li>
<li><p>1024 experts</p>
</li>
</ul>
<p>This explodes the total parameter count <strong>without exploding inference cost</strong>.</p>
<p>Example (realistic):</p>
<ul>
<li><p>Dense FFN: <strong>300M parameters</strong></p>
</li>
<li><p>MoE FFN with 256 experts:</p>
<ul>
<li>Total capacity = 300M × 256 = <strong>76.8B parameters</strong></li>
</ul>
</li>
<li><p>top_k = 8 → only 8 experts active</p>
<ul>
<li>Active parameters = 8 × 300M = <strong>2.4B per token</strong></li>
</ul>
</li>
</ul>
<p>Thus:</p>
<h3 id="heading-massive-total-capacity">🟢 Massive total capacity</h3>
<h3 id="heading-small-active-compute">🔴 Small active compute</h3>
<p>This is the magic of MoE.</p>
<hr />
<h1 id="heading-7-why-this-works-one-sentence-summary">7. Why This Works (One-Sentence Summary)</h1>
<blockquote>
<p><strong>MoE increases <em>total capacity</em> (many experts) but reduces <em>active compute</em> because each token uses only a few experts selected by the router.</strong></p>
</blockquote>
<hr />
<h1 id="heading-8-visual-analogy-very-simple">8. Visual Analogy (Very Simple)</h1>
<p>Think of a workshop:</p>
<h3 id="heading-dense-model-one-giant-machine">Dense model = one giant machine</h3>
<p>Every job must use the same giant machine, even if unnecessary.</p>
<h3 id="heading-moe-model-256-specialized-machines">MoE model = 256 specialized machines</h3>
<p>For each job:</p>
<ul>
<li><p>A router picks the best 2 machines.</p>
</li>
<li><p>The rest stay powered off.</p>
</li>
</ul>
<p>Total machine count = huge<br />Power used per job = very small</p>
<hr />
<h1 id="heading-9-concrete-example-deepseek-v3">9. Concrete Example (DeepSeek-V3)</h1>
<ul>
<li><p>Total model parameters: <strong>671B</strong></p>
</li>
<li><p>Experts per MoE layer: <strong>256</strong></p>
</li>
<li><p>Active experts per token: <strong>1 shared + 8 routed = 9</strong></p>
</li>
<li><p>Each expert: <strong>~4.1B</strong> parameters.</p>
</li>
<li><p>Active params per token: <strong>~37B</strong></p>
</li>
<li><p>Total params: <strong>671B</strong></p>
</li>
</ul>
<p>Thus DeepSeek-V3 has extremely high capacity, but inference cost like a much smaller dense model.</p>
<hr />
<h1 id="heading-10-summary-for-retrieval-practice">10. Summary for Retrieval Practice</h1>
<p>Memorize these short bullets:</p>
<ul>
<li><p>MoE replaces <strong>1 FFN</strong> with <strong>many FFNs (experts)</strong>.</p>
</li>
<li><p>Router picks <strong>top_k</strong> experts per token.</p>
</li>
<li><p>Only those experts are active → low compute.</p>
</li>
<li><p>Total parameters can be massive → high capacity.</p>
</li>
<li><p>Real MoE models have far more parameters than dense models.</p>
</li>
<li><p>But <strong>active</strong> parameters are small → efficient inference.</p>
</li>
<li><p>Memory savings come from reduced activations and fewer FFN computations.</p>
</li>
</ul>
<hr />
]]></content:encoded></item></channel></rss>