N Neurarch Architectures Checks Docs Open the app

Architectures / NLP/LLM

๐Ÿค– Transformer Block

Transformer encoder block (simplified)

Layers
8
Parameters
7.09M
Input
512 ร— 768
Output
512 ร— 768
Verifier
2 advisories

Every number on this page is computed from the graph by the same functions the app runs, not written by hand.

Open Transformer Block on the canvas Free, no account needed

When to pick it

Pick as a generic encoder building block for sequence models when you don't need the modern LLM stack (RoPE/GQA/RMSNorm). Good teaching baseline.

Structure

8 layers. Output shapes are propagated from the input shape, batch dimension excluded.

LayerTypeParametersOutput shape
1InputInputshape=[512, 768]512 ร— 768
2MultiHeadAttentionMulti-Head AttentionnumHeads=8512 ร— 768
3Add_1Add512 ร— 768
4LayerNorm_1LayerNorm512 ร— 768
5FeedForwardFeed ForwardffDim=3072512 ร— 768
6Add_2Add512 ร— 768
7LayerNorm_2LayerNorm512 ร— 768
8OutputOutput512 ร— 768

What the verifier says

The same 41 structural checks that run on every edit in the app, on this graph.

warn"LayerNorm_2" (layerNorm) is the last layer before Output. Normalizing the raw logits constrains the output range and breaks standard loss functions. Fix: Move normalization before the final Linear/Conv layer. (LayerNorm_2)
bn-at-output
warn1 attention layer(s) present but no positional encoding found. Attention is permutation-invariant, without position information the model cannot distinguish token order. Fix: Add a PositionalEncoding (sinusoidal) or RoPE layer before the first attention layer. (MultiHeadAttention)
attention-no-pe

The PyTorch it exports

Generated from the graph above. First 46 lines; the app exports the whole file, plus the training loop, the data contract and a deploy bundle.

# Architecture designed with Neurarch: https://neurarch.com
# PyTorch: compatible with Python 3.8+ and torch>=1.12
# Colab: pip install torch torchvision  (usually pre-installed)

import torch
import torch.nn as nn
import torch.nn.functional as F
from typing import Tuple

class TransformerBlock(nn.Module):
    def __init__(self):
        super().__init__()

        self.multiHeadAttention_1 = nn.MultiheadAttention(embed_dim=768, num_heads=8, batch_first=True)
        self.layerNorm_1 = nn.LayerNorm(768)
        self.feedForward_1 = nn.Sequential(
            nn.Linear(768, 3072),
            nn.ReLU(),
            nn.Linear(3072, 768)
        )
        self.layerNorm_2 = nn.LayerNorm(768)

    def forward(self, x):
        # Input shape: [512,768]
        multi_head_attention_tion_1 = self.multiHeadAttention_1(x, x, x)[0]
        add_add_1 = multi_head_attention_tion_1
        layer_norm_Norm_1 = self.layerNorm_1(add_add_1)
        feed_forward_ward_1 = self.feedForward_1(layer_norm_Norm_1)
        add_add_2 = feed_forward_ward_1
        layer_norm_Norm_2 = self.layerNorm_2(add_add_2)
        # Output
        return layer_norm_Norm_2


if __name__ == '__main__':
    model = TransformerBlock()
    model.eval()

    x = torch.randn(1, 512, 768)  # (batch, channels, length)
    with torch.no_grad():
        output = model(x)

    print(f'Input  shape : {tuple(x.shape)}')
    print(f'Output shape : {tuple(output.shape)}')
    total = sum(p.numel() for p in model.parameters())
    trainable = sum(p.numel() for p in model.parameters() if p.requires_grad)

For agents

This architecture is machine-readable end to end. An agent can list the set, fetch this graph, edit it, and have the edit verified before any GPU time is spent.

Also in NLP/LLM

๐Ÿ“– BERT Base
BERT-Base encoder โ€” bidirectional MHA
11 layers ยท 31.12M
๐Ÿง  GPT-2
GPT-2 Small โ€” causal transformer block
12 layers ยท 84.33M
๐Ÿฆ™ LLaMA-3 Block
LLaMA-3 decoder block โ€” GQA
10 layers ยท 702.55M
๐Ÿ”€ Mixtral MoE Block
Mixtral decoder block โ€” GQA + Sparse MoE
9 layers ยท 1.45B