Llama 4 From Scratch

Build Llama 4's MoE feed-forward block: a router, a sigmoid gate on the expert's input, a SwiGLU expert, and the shared expert every token uses.

A large model can store many feed-forward networks without running all of them for every token. Llama 4 uses this idea: a router selects a network, called an expert, for the token being processed.

There is also a shared expert that processes every token. That gives us a small piece of the architecture we can build and inspect: one shared path plus one selected path.

Follow one token through an expert block

Suppose a token is represented by the vector x = [1, 0]. A learned router produces a score for each available expert. We will use two experts and scores [0, −1], so expert 0 wins.

Llama 4 applies sigmoid to the winning score. Sigmoid maps a number into the range 0 to 1; sigmoid(0) is 0.5. The selected expert receives 0.5x, while the shared expert receives x.

BranchInput to expertContribution to the result
Sharedx = [1, 0]Shared expert's output
Routed0.5x = [0.5, 0]Expert 0's output on the scaled input
CombinationBoth branch outputsAdd the two vectors

Writing S for the shared network and E for the selected network:

$$\begin{aligned} s&=\operatorname{sigmoid}(r_j)\\ y&=S(x)+E_j(sx) \end{aligned}$$

Here r_j is the winning router score. This y is the feed-forward branch's output; a full decoder block also has normalization, attention and residual additions.

It is tempting to write the second term as sE_j(x). Would that give the same answer? For a linear expert, yes. For this SwiGLU expert, generally no: the position of that multiplication matters.

Build the nonlinear part

A Llama 4 expert uses SwiGLU: make two projections of the input, apply SiLU to one, multiply their entries, then project back. In PyTorch notation that is down(silu(gate(x)) * up(x)).

SiLU(z) = z × sigmoid(z), applied separately to each coordinate.

For a transparent example, make the first two projections identity matrices. The expert becomes scale * silu(x) * x, with scale representing a simple output projection. For an input coordinate of 1 and output scale 1, E(0.5) = SiLU(0.5) × 0.5 ≈ 0.1556. But 0.5E(1) = 0.5 × SiLU(1) × 1 ≈ 0.3655. The shared path adds the same value to both, so it cannot remove the difference. These hand-picked weights let us examine routing; they have not learned language.

The program also adds a second token, [0, 1], so we can inspect both experts. Its printed diagonal entries are the active coordinate from each token.

The tokens choose experts [0, 1], both with gate 0.5. The active output coordinates are approximately [0.8867, 1.0423]. Moving the gate after the expert changes them to [1.0966, 1.4621]. The final check is False.

Now add 2 to every router score before selecting the winner. Predict which expert IDs change and whether the outputs change.

Put the block in the model

The released Scout model has 16 routed experts; Maverick has 128. Each MoE block selects one routed expert and also uses a shared expert. Their layer layouts differ: Scout uses MoE feed-forward sublayers throughout its decoder, while Maverick alternates dense and MoE feed-forward sublayers. A dense sublayer uses its one feed-forward network for every token. The Meta model builders make this distinction explicit.

Llama 4 also accepts images. A vision encoder turns image patches into vectors, which are projected and placed alongside text embeddings before the language decoder processes the sequence. This early combination is called early fusion. Native multimodality still involves a vision encoder; our expert program implements neither that encoder nor attention.

The model card describes the released models and their training context. Implementing this block reproduces a mechanism, not their trained capabilities or benchmark scores.

Inspect the implementation: open Meta's MoE.forward. Locate top-k selection, sigmoid, multiplication of the routed input, and addition of the shared expert. Then find batched_swiglu and identify the two projections. That tracing exercise is a useful check before translating a model diagram into code.

  • A Llama 4 MoE branch adds a shared expert to one selected routed expert.
  • The selected sigmoid gate scales the expert input: S(x) + E(sx). Moving it after SwiGLU generally changes the result.
  • SwiGLU multiplies a SiLU-activated projection by a second projection, then projects back.
  • Scout uses MoE feed-forward sublayers throughout; Maverick alternates dense and MoE sublayers.
  • Image features join text embeddings before the decoder. Our tiny block does not reproduce trained model capabilities.