Skip to content

Architecture

Sudhanshu Shukla edited this page Nov 23, 2026 · 3 revisions

Architecture: Native Mixtral-Style Sparse MoE

The project implements a decoder-only causal Transformer in mixtral_lab/model.py. It is architecturally inspired by Mixtral; it is not a weights-compatible reimplementation of the released pretrained Mixtral models.

Decoder block

Token IDs → Token embedding → N × TransformerBlock → RMSNorm → LM head
                                  │
                                  ├─ RMSNorm → GQA causal attention + RoPE → Residual
                                  └─ RMSNorm → Top-k sparse SwiGLU experts → Residual

Attention

  • Grouped-query attention (GQA): query heads may outnumber KV heads. K/V states are repeated across query groups in this reference implementation.
  • Rotary position embeddings (RoPE): applied to projected queries and keys based on explicit position offsets.
  • Causal attention: uses PyTorch scaled dot-product attention, with configurable global or bounded sliding mode.
  • KV cache: supported for autoregressive generation; context and attention semantics depend on the selected mode.

Sparse MoE feed-forward layer

The router scores each token against num_experts, selects the highest-probability top_k experts, normalizes selected weights, and combines expert outputs. Experts use SwiGLU MLPs. The loss contains next-token cross-entropy plus a configurable auxiliary load-balancing term.

Because routing and expert computation are implemented as a research reference path, the code does not implement a dedicated fused expert kernel or expert-parallel dispatch.

Training objective

For Stage 1, the model optimizes a causal language-model objective with an auxiliary routing regularizer:

L_total = L_next_token_CE + aux_loss_coef × L_router_balance

For Stage 2, prompt tokens are masked from the causal-LM labels, and only the completion portion is supervised, while the model still computes its routing auxiliary loss.

Where to inspect implementation

  • model.py — model, attention, router, experts and losses.
  • config.py — architecture parameters and validation.
  • train_utils.py — model creation, initialization, optimization and checkpoint utilities.

See Model Configuration for reproducible parameter selection.

Detailed Mermaid diagrams

Clone this wiki locally