Transformer block

Reference code meta-llama/llama3 @ a0940f9, configuration meta-llama/llama-models @ 0e0b8c5 · checkpoint, tokenizer and tensors not verified · How to read

32 independent transformer blocks

learnedself.feed_forward(self.ffn_norm(h))

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]
parameters
176,160,768 · × 32 blocks = 5,637,144,576
Parameters

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

Exact per-instance accounting
ComponentCalculationParameters
Gate projection4,096×14,3364{,}096 \times 14{,}33658,720,256
Up projection4,096×14,3364{,}096 \times 14{,}33658,720,256
Down projection14,336×4,09614{,}336 \times 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{,}384mlp-widthconfig
  2. ⌊23×16,384⌋=10,922\left\lfloor \tfrac{2}{3}\times 16{,}384\right\rfloor=10{,}922mlp-width
  3. ⌊1.3×10,922⌋=14,198\left\lfloor 1.3\times 10{,}922\right\rfloor=14{,}198configmlp-width
  4. 1,024×⌈14,198/1,024⌉=14,336=dff1{,}024\times\left\lceil 14{,}198/1{,}024\right\rceil=14{,}336=d_{ff}configmlp-width

Across 32 independent blocks: 176,160,768×32=5,637,144,576176{,}160{,}768 \times 32 = 5{,}637{,}144{,}576 parameters.

Derived from the pinned catalogue dimensions and implementation. Checkpoint tensors have not been independently counted.

Codemodel.py 193–248 · sku_list.py 19, 188–207
model.py 193–206
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)
model.py 229–234
        self.feed_forward = FeedForward(
            dim=args.dim,
            hidden_dim=4 * args.dim,
            multiple_of=args.multiple_of,
            ffn_dim_multiplier=args.ffn_dim_multiplier,
        )
model.py 208–219
        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))
model.py 236–248
        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
sku_list.py 19
LLAMA3_VOCAB_SIZE = 128256
sku_list.py 188–207
def llama3_base_models() -> list[Model]:
    return [
        Model(
            core_model_id=CoreModelId.llama3_8b,
            description="Llama 3 8b model",
            huggingface_repo="meta-llama/Llama-3-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": False,
            },
            pth_file_count=1,
        ),
Sources4

Direct evidence

Meta’s original Llama 3 8B base catalogue entry supplies dimensions, head counts, vocabulary, MLP rounding settings, epsilon and unscaled RoPE settings.

config · meta-llama/llama-models / models/sku_list.py
Pinned revision 0e0b8c519242 ↗ · lines 19–207

Read code excerpt
LLAMA3_VOCAB_SIZE = 128256

… lines 20–187 omitted …

def llama3_base_models() -> list[Model]:
    return [
        Model(
            core_model_id=CoreModelId.llama3_8b,
            description="Llama 3 8b model",
            huggingface_repo="meta-llama/Llama-3-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": False,
            },
            pth_file_count=1,
        ),

Direct evidence

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

mlp-width · meta-llama/llama3 / llama/model.py
Pinned revision a0940f9cf706 ↗ · lines 193–234

Read code 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,
        )

Direct evidence

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

mlp · meta-llama/llama3 / llama/model.py
Pinned revision a0940f9cf706 ↗ · lines 208–219

Read code 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))

Direct evidence

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.

handoff · meta-llama/llama3 / llama/model.py
Pinned revision a0940f9cf706 ↗ · lines 236–248

Read code 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