N Neurarch Architectures Checks Docs Open the app

Architectures / NLP/LLM

๐Ÿง  GPT-2

GPT-2 Small โ€” causal transformer block (768D, 12 heads, 4ร— FFN)

Layers
12
Parameters
84.33M
Input
1 ร— 1024
Output
1 ร— 1024 ร— 50257
Verifier
Clean

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

Open GPT-2 on the canvas Free, no account needed

When to pick it

Pick for single-GPU language modeling experiments and as a teaching reference for the canonical decoder-only stack. Modern LLMs prefer LLaMA-3 / Phi-3 blocks.

Structure

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

LayerTypeParametersOutput shape
1tokensInputshape=[1, 1024]1 ร— 1024
2token_embedEmbedding1 ร— 1024 ร— 768
3pos_embedPositional EncodingembedDim=768, maxLen=10241 ร— 1024 ร— 768
4ln_1LayerNormnormalizedShape=7681 ร— 1024 ร— 768
5attnCausal AttentionembedDim=768, numHeads=121 ร— 1024 ร— 768
6residual_1Add1 ร— 1024 ร— 768
7ln_2LayerNormnormalizedShape=7681 ร— 1024 ร— 768
8mlpFeed ForwardffDim=30721 ร— 1024 ร— 768
9residual_2Add1 ร— 1024 ร— 768
10ln_fLayerNormnormalizedShape=7681 ร— 1024 ร— 768
11lm_headLinearoutFeatures=502571 ร— 1024 ร— 50257
12logitsOutput1 ร— 1024 ร— 50257

What the verifier says

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

No finding. Shapes propagate end to end, every divisibility condition holds, and no advisory rule fires. See the checks.

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 GPT_2Block(nn.Module):
    def __init__(self):
        super().__init__()

        self.embedding_1 = nn.Embedding(50257, 768)
        self.layerNorm_1 = nn.LayerNorm(768)
        self.causalAttention_1 = nn.MultiheadAttention(embed_dim=768, num_heads=12, batch_first=True)
        self.layerNorm_2 = nn.LayerNorm(768)
        self.feedForward_1 = nn.Sequential(
            nn.Linear(768, 3072),
            nn.ReLU(),
            nn.Linear(3072, 768)
        )
        self.layerNorm_3 = nn.LayerNorm(768)
        self.linear_1 = nn.Linear(768, 50257)

    def forward(self, x):
        # tokens shape: [1,1024]
        embedding_ding_1 = self.embedding_1(x)
        # positionalEncoding: add positional encoding externally (e.g. sinusoidal or learned PE)
        layer_norm_Norm_1 = self.layerNorm_1(embedding_ding_1)
        causal_attention_tion_1 = self.causalAttention_1(layer_norm_Norm_1, layer_norm_Norm_1, layer_norm_Norm_1)[0]
        add_add_1 = causal_attention_tion_1 + embedding_ding_1
        layer_norm_Norm_2 = self.layerNorm_2(add_add_1)
        feed_forward_ward_1 = self.feedForward_1(layer_norm_Norm_2)
        add_add_2 = feed_forward_ward_1 + add_add_1
        layer_norm_Norm_3 = self.layerNorm_3(add_add_2)
        linear_near_1 = self.linear_1(layer_norm_Norm_3)
        # Output
        return linear_near_1


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

    x = torch.randint(0, 50000, (1, 1024))  # (batch, features)

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

๐Ÿค– Transformer Block
Transformer encoder block
8 layers ยท 7.09M
๐Ÿ“– BERT Base
BERT-Base encoder โ€” bidirectional MHA
11 layers ยท 31.12M
๐Ÿฆ™ 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