Understanding Mixtral-8x7b

<p>
<em>This blog post is adapted from an <a href="https://x.com/vtabbott_/status/1741292811193065557?s=20" rel="nofollow">X thread</a> I posted. It's garnered significant interest, so I decided to post it here as well!</em></p>
<p><a href="https://mistral.ai/news/mixtral-of-experts/" rel="nofollow">Mixtral-8x7b</a> by MistralAI is an LLM that outperforms all but OpenAI and Anthropic's most powerful models. And, it is <a href="https://github.com/mistralai/mistral-src/tree/moe" rel="nofollow">open-source</a>. In this blog post, I will explain its architecture design using my <a href="https://openreview.net/forum?id=RyZB4qXEgt" rel="nofollow">Neural Circuit Diagrams</a>. Let's dive in and see how cutting-edge transformers work!  </p>
<p><a href="https://cdn-uploads.huggingface.co/production/uploads/65921b0435c41262d6b68b34/5yRsqriAZH2cp2fUseR7g.png" rel="nofollow"><img alt="image/png" src="https://cdn-uploads.huggingface.co/production/uploads/65921b0435c41262d6b68b34/5yRsqriAZH2cp2fUseR7g.png"/></a><em>From LMSys' <a href="https://huggingface.co/spaces/lmsys/chatbot-arena-leaderboard">Chatbot Arena</a>. Mixtral-8x7b is very, very good. You can try it in <a href="https://chat.lmsys.org/?arena" rel="nofollow">the arena</a> for yourself!</em></p>
<p>The overall structure of the architecture is shockingly simple. It is a decoder-only transformer. The model input is a series of tokens, which are embedded into vectors, and are then processed via decoder layers. The output is the probability of every location being occupied by some word, allowing for text infill and prediction.</p>
<p><a href="https://cdn-uploads.huggingface.co/production/uploads/65921b0435c41262d6b68b34/eDxgIVPpPYsLp7y7KQMmd.png" rel="nofollow"><img alt="image/png" src="https://cdn-uploads.huggingface.co/production/uploads/65921b0435c41262d6b68b34/eDxgIVPpPYsLp7y7KQMmd.png"/></a><em>The overall model converts tokens to vectors, processes them, and converts them back to word probabilities.</em></p>
<p>Every decoder layer has two key sections: an attention mechanism, which incorporates contextual information; and a multi-layer perceptron, which individually processes every word vector.</p>
<p>These are encapsulated in residual connections, which allows for <a href="https://twitter.com/vtabbott_/status/1733457509632070027?s=20" rel="nofollow">training at depth</a>. A combination of contextual and individual processing allows for sophisticated patterns to be discovered.</p>
<p><a href="https://cdn-uploads.huggingface.co/production/uploads/65921b0435c41262d6b68b34/ukuM-mNOoDAjPfoFGJ_yf.png" rel="nofollow"><img alt="image/png" src="https://cdn-uploads.huggingface.co/production/uploads/65921b0435c41262d6b68b34/ukuM-mNOoDAjPfoFGJ_yf.png"/></a><em>The decoder layers are akin to the original transformer's, but exclusively use self-attention.</em></p>
<p>The attention mechanism used is similar to the original transformer's, which I cover in detail in my paper and briefly in a <a href="https://www.youtube.com/watch?v=ghlIs8bVXU4" rel="nofollow">YouTube video</a>. I list additional key features in the diagram, also covered in the <a href="https://github.com/mistralai/mistral-src/tree/moe" rel="nofollow">original github</a> and <a href="https://huggingface.co/docs/transformers/model_doc/mixtral">Hugging Face docs</a>.</p>
<p>A key feature not explicitly shown in the below diagram is <a href="https://hazyresearch.stanford.edu/blog/2023-01-12-flashattention-long-sequences" rel="nofollow">FlashAttention</a> by <a href="https://hazyresearch.stanford.edu/" rel="nofollow">Hazy Research</a>, which accelerates algorithms by decomposing attention to fit on GPU kernels enabling high-speed memory access. I've been making progress using <em>Neural Circuit Diagrams</em> to derive such techniques. Explicitly stating variables in memory, linearity, and broadcasting is naturally displayed by them, offering the formal tools needed to accelerate algorithms.</p>
<p><a href="https://cdn-uploads.huggingface.co/production/uploads/65921b0435c41262d6b68b34/1UuvfoZqsw9nB569N9kRB.png" rel="nofollow"><img alt="image/png" src="https://cdn-uploads.huggingface.co/production/uploads/65921b0435c41262d6b68b34/1UuvfoZqsw9nB569N9kRB.png"/></a><em>Attention mechanisms have gradually evolved since popularized by 2017's <a href="https://arxiv.org/abs/1706.03762" rel="nofollow">Attention is All You Need</a>.</em></p>
<p>Finally, we get the key feature of Mixtral: <strong>Sparse Mixture of Experts</strong> (SMoE). MLP layers are immense consumers of computational resources. SMoEs have multiple layers ("experts") available. For every input, a weighted sum is taken over the outputs of the most relevant experts. SMoE layers can therefore learn sophisticated patterns while having relatively inexpensive compute cost.</p>
<p><a href="https://cdn-uploads.huggingface.co/production/uploads/65921b0435c41262d6b68b34/-scdQY3VUk19RsiULSo-7.png" rel="nofollow"><img alt="image/png" src="https://cdn-uploads.huggingface.co/production/uploads/65921b0435c41262d6b68b34/-scdQY3VUk19RsiULSo-7.png"/></a><em>A gating mechanism decides which layers to execute, leading to a computationally efficient algorithm. See also <a href="https://arxiv.org/pdf/2101.03961.pdf" rel="nofollow">Switch Transformers</a> and <a href="https://arxiv.org/pdf/2211.15841.pdf" rel="nofollow">MegaBlocks</a>.</em></p>
<p><strong>Conclusion.</strong> Mixtral is an immense achievement for the open-source AI community. The model is surprisingly simple. Compared to the <a href="https://arxiv.org/abs/1706.03762" rel="nofollow">original transformer</a> <a href="/blog/vtabbott/vtabbott.io/ncd-poster">architecture</a>, encoders have been removed. Attention mechanisms have incurred 7 years' gradual innovations. The biggest change is the presence of SMoEs instead of plain MLPs. Mixtral has proven that open-source designs and SMoEs are on the frontier of ML development, and I suspect both will attract far more attention as a result.</p>
<p><a href="https://cdn-uploads.huggingface.co/production/uploads/65921b0435c41262d6b68b34/OsCpW1VryCYHBBFUuqN2S.png" rel="nofollow"><img alt="image/png" src="https://cdn-uploads.huggingface.co/production/uploads/65921b0435c41262d6b68b34/OsCpW1VryCYHBBFUuqN2S.png"/></a><em>The overall attention architecture, expressed using Neural Circuit Diagrams.</em></p>
