Layer

SwiGLU MLP

×

Two learned feature branches combine to form a position-wise update to the residual stream.

h=a⊙uh = a \odot u
Δmlp=hWdown\Delta_{\mathrm{mlp}} = hW_{\mathrm{down}}
All 5 steps
g=xWgateg = xW_{\mathrm{gate}}
u=xWupu = xW_{\mathrm{up}}
a=g σ(g)a=g\,\sigma(g)
h=a⊙uh = a \odot u
Δmlp=hWdown\Delta_{\mathrm{mlp}} = hW_{\mathrm{down}}
in
xmlpx_{\mathrm{mlp}} [B batch, T positions, 4096 d]
out
Δmlp\Delta_{\mathrm{mlp}} [B batch, T positions, 4096 d]
Parameters176,160,768 × 32 layers

176,160,768 parameters per instance, across its learned components

ComponentCalculationParameters
Gate projection4,096 × 14,33658,720,256
Up projection4,096 × 14,33658,720,256
Down projection14,336 × 4,09658,720,256
Per-instance total176,160,768

MLP width, derived from the configuration

  1. 4d=4×4,096=16,3844d=4\times 4{,}096=16{,}384model.py 193–234sku_list.py 19–248
  2. ⌊23×16,384⌋=10,922\left\lfloor \tfrac{2}{3}\times 16{,}384\right\rfloor=10{,}922model.py 193–234
  3. ⌊1.3×10,922⌋=14,198\left\lfloor 1.3\times 10{,}922\right\rfloor=14{,}198sku_list.py 19–248model.py 193–234
  4. 1,024×⌈14,198/1,024⌉=14,336=dff1{,}024\times\left\lceil 14{,}198/1{,}024\right\rceil=14{,}336=d_{ff}sku_list.py 19–248model.py 193–234

One in each of the 32 layers, each with its own weights.

Not checked against a checkpoint.

Weights · one MLP

Llama 3.1 8B · 16-bit336 MiB
GELU, same width · 16-bit224 MiB

3 × 4,096 × 14,336 parameters × 2 bytes.

A GELU MLP of the same width: two matrices, 117,440,512; at 4d4d, 134,217,728.

model.py 208–216, 193–234sku_list.py 19–248generation.py 105–108
Sources

In codeself.feed_forward(self.ffn_norm(h))

Open pinned code ↗

Direct evidence

Meta's Llama 3.1 8B base catalogue entry supplies dimensions, head counts, the vocabulary (LLAMA3_VOCAB_SIZE, line 19), MLP rounding, epsilon, rope_theta and use_scaled_rope: True.

sku_list.py 19–248 ↗ · meta-llama/llama-models @ 0e0b8c519242

Excerpt
LLAMA3_VOCAB_SIZE = 128256

… lines 20–230 omitted …

        Model(
            core_model_id=CoreModelId.llama3_1_8b,
            description="Llama 3.1 8b model",
            huggingface_repo="meta-llama/Llama-3.1-8B",
            arch_args={
                "dim": 4096,
                "n_layers": 32,
                "n_heads": 32,
                "n_kv_heads": 8,
                "vocab_size": LLAMA3_VOCAB_SIZE,
                "ffn_dim_multiplier": 1.3,
                "multiple_of": 1024,
                "norm_eps": 1e-05,
                "rope_theta": 500000.0,
                "use_scaled_rope": True,
            },
            pth_file_count=1,
        ),

The layer passes four times model width; FeedForward truncates, applies the configured multiplier and rounds to the configured multiple.

model.py 193–234 ↗ · meta-llama/llama3 @ a0940f9cf706

Excerpt
class FeedForward(nn.Module):
    def __init__(
        self,
        dim: int,
        hidden_dim: int,
        multiple_of: int,
        ffn_dim_multiplier: Optional[float],
    ):
        super().__init__()
        hidden_dim = int(2 * hidden_dim / 3)
        # custom dim factor multiplier
        if ffn_dim_multiplier is not None:
            hidden_dim = int(ffn_dim_multiplier * hidden_dim)
        hidden_dim = multiple_of * ((hidden_dim + multiple_of - 1) // multiple_of)

… lines 207–228 omitted …

        self.feed_forward = FeedForward(
            dim=args.dim,
            hidden_dim=4 * args.dim,
            multiple_of=args.multiple_of,
            ffn_dim_multiplier=args.ffn_dim_multiplier,
        )

The MLP owns three bias-free projections. Its forward expression applies SiLU to w1(x), multiplies w3(x) element-wise and projects through w2.

model.py 208–219 ↗ · meta-llama/llama3 @ a0940f9cf706

Excerpt
        self.w1 = ColumnParallelLinear(
            dim, hidden_dim, bias=False, gather_output=False, init_method=lambda x: x
        )
        self.w2 = RowParallelLinear(
            hidden_dim, dim, bias=False, input_is_parallel=True, init_method=lambda x: x
        )
        self.w3 = ColumnParallelLinear(
            dim, hidden_dim, bias=False, gather_output=False, init_method=lambda x: x
        )

    def forward(self, x):
        return self.w2(F.silu(self.w1(x)) * self.w3(x))

Attention receives attention_norm(x) and its update is added to x; the MLP receives ffn_norm(h) and its update is added to h.

model.py 236–248 ↗ · meta-llama/llama3 @ a0940f9cf706

Excerpt
        self.attention_norm = RMSNorm(args.dim, eps=args.norm_eps)
        self.ffn_norm = RMSNorm(args.dim, eps=args.norm_eps)

    def forward(
        self,
        x: torch.Tensor,
        start_pos: int,
        freqs_cis: torch.Tensor,
        mask: Optional[torch.Tensor],
    ):
        h = x + self.attention(self.attention_norm(x), start_pos, freqs_cis, mask)
        out = h + self.feed_forward(self.ffn_norm(h))
        return out