LLaMA Components › Implementation: A Complete LLaMA Block

Michael BrenndoerferUpdated August 2, 20252 min read
Language AI HandbookAll chapters
Part 29 of 67Modern Decoder Models
Chapter 208LLaMA Components › Implementation: A Complete LLaMA Block
Reading section 6 of 12 · Implementation: A Complete LLaMA Block · 2 minSection 6 of 12 · 2 min

Book contents

Section 6 of 12 · 2 min read

LLaMA Components › Implementation: A Complete LLaMA BlockOne section · Sections in this chapter6 / 12 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Let's assemble all components into a complete LLaMA transformer block:

In[17]:
Code
class LLaMABlock:
    """
    Complete LLaMA transformer block.

    Combines RMSNorm, RoPE attention, and SwiGLU FFN with
    pre-norm configuration and residual connections.
    """

    def __init__(self, d_model, n_heads, d_ff, rope_base=10000, eps=1e-6):
        """
        Args:
            d_model: Model dimension
            n_heads: Number of attention heads
            d_ff: FFN hidden dimension (typically ~2.67 * d_model for SwiGLU)
            rope_base: Base for RoPE frequency computation
            eps: Epsilon for RMSNorm
        """
        self.d_model = d_model
        self.n_heads = n_heads

        # Normalization layers
        self.norm1 = RMSNorm(d_model, eps)
        self.norm2 = RMSNorm(d_model, eps)

        # Attention with RoPE
        self.attention = RoPEAttention(d_model, n_heads, rope_base)

        # SwiGLU FFN
        self.ffn = SwiGLU(d_model, d_ff)

    def __call__(self, x, mask=None):
        """
        Forward pass through the block.

        Args:
            x: Input tensor of shape (seq_len, d_model)
            mask: Optional causal mask of shape (seq_len, seq_len)

        Returns:
            Output tensor of shape (seq_len, d_model)
        """
        # Pre-norm attention with residual
        h = x + self.attention(self.norm1(x), mask)

        # Pre-norm FFN with residual
        y = h + self.ffn(self.norm2(h))

        return y

Let's test our implementation with a small example:

In[18]:
Code
# Configuration matching LLaMA 7B proportions (scaled down)
d_model = 128
n_heads = 4
d_ff = int(d_model * 2.67)  # SwiGLU hidden dimension
seq_len = 32

# Create a block
block = LLaMABlock(d_model=d_model, n_heads=n_heads, d_ff=d_ff)

# Create sample input
x = np.random.randn(seq_len, d_model).astype(np.float32)

# Create causal mask (lower triangular)
mask = np.tril(np.ones((seq_len, seq_len), dtype=bool))

# Forward pass
y = block(x, mask=mask)
Out[19]:
Console
LLaMA Block Configuration:
  Model dimension: 128
  Attention heads: 4
  Head dimension:  32
  FFN dimension:   341
  Sequence length: 32

Input shape:  (32, 128)
Output shape: (32, 128)

Input stats:  mean=-0.0094, std=0.9868
Output stats: mean=-0.0106, std=1.1425

The output maintains the same shape as the input, as expected for a transformer block. The mean and standard deviation of the output remain in a reasonable range. This shows that RMSNorm successfully stabilizes activations. Without normalization, activations could grow or shrink exponentially through many layers. This makes training unstable. The fact that output statistics stay close to the input statistics is a good sign that the block is well-behaved.