N Neurarch Architectures Checks Docs Open the app

Architectures / NLP/LLM

φ Phi-3 Mini Block

Phi-3 Mini 3.8B decoder block — full MHA (32H, 3072D), SwiGLU FFN (8192), RMSNorm, RoPE

Layers
12
Parameters
310.29M
Input
1 × 2048
Output
1 × 2048 × 32064
Verifier
Clean

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

Open Phi-3 Mini Block on the canvas Free, no account needed

When to pick it

Pick for compact LLMs (≤4B params) when you want modern ingredients (RoPE, SwiGLU) without the full LLaMA-3 footprint.

Structure

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

LayerTypeParametersOutput shape
1tokensInputshape=[1, 2048]1 × 2048
2embedEmbedding1 × 2048 × 3072
3ropeRoPE
4norm_attnRMSNormnormalizedShape=30721 × 2048 × 3072
5attnGrouped Query AttnembedDim=3072, numHeads=32, numKVHeads=321 × 2048 × 3072
6residual_1Add1 × 2048 × 3072
7norm_ffnRMSNormnormalizedShape=30721 × 2048 × 3072
8ffnSwiGLUembedDim=3072, intermediateSize=81921 × 2048 × 3072
9residual_2Add1 × 2048 × 3072
10norm_outRMSNormnormalizedShape=30721 × 2048 × 3072
11lm_headLinearoutFeatures=320641 × 2048 × 32064
12outputOutput1 × 2048 × 32064

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

        self.embedding_1 = nn.Embedding(32064, 3072)
        self.rmsNorm_1 = nn.RMSNorm(3072)
        self.groupedQueryAttention_1 = nn.ModuleDict({
            'q_proj': nn.Linear(3072, 3072,        bias=False),   # 32 heads × 96
            'k_proj': nn.Linear(3072, 3072, bias=False),   # 32 KV heads × 96
            'v_proj': nn.Linear(3072, 3072, bias=False),
            'o_proj': nn.Linear(3072, 3072,        bias=False),
        })  # GQA: 32Q / 32KV heads (requires F.scaled_dot_product_attention)
        self.rmsNorm_2 = nn.RMSNorm(3072)
        self.swiglu_1 = nn.ModuleDict({
            'gate_proj': nn.Linear(3072, 8192, bias=False),
            'up_proj':   nn.Linear(3072, 8192, bias=False),
            'down_proj': nn.Linear(8192, 3072, bias=False),
        })  # SwiGLU FFN (LLaMA-style)
        self.rmsNorm_3 = nn.RMSNorm(3072)
        self.linear_1 = nn.Linear(6291456, 32064)

    def forward(self, x):
        # tokens shape: [1,2048]
        # rope: RoPE applied inside attention (no separate layer needed)
        embedding_embed = self.embedding_1(x)
        rms_norm_m_attn = self.rmsNorm_1(embedding_embed)
        grouped_query_attention_attn = self.groupedQueryAttention_1['o_proj'](F.scaled_dot_product_attention(
            self.groupedQueryAttention_1['q_proj'](rms_norm_m_attn).view(rms_norm_m_attn.size(0),-1,32,96).transpose(1,2),
            self.groupedQueryAttention_1['k_proj'](rms_norm_m_attn).view(rms_norm_m_attn.size(0),-1,32,96).transpose(1,2).repeat_interleave(1,dim=1),
            self.groupedQueryAttention_1['v_proj'](rms_norm_m_attn).view(rms_norm_m_attn.size(0),-1,32,96).transpose(1,2).repeat_interleave(1,dim=1),
            is_causal=True,
        ).transpose(1,2).reshape(rms_norm_m_attn.size(0),-1,3072))
        add_idual1 = grouped_query_attention_attn + embedding_embed
        rms_norm_rm_ffn = self.rmsNorm_2(add_idual1)
        swiglu_ffn = self.swiglu_1['down_proj'](F.silu(self.swiglu_1['gate_proj'](rms_norm_rm_ffn)) * self.swiglu_1['up_proj'](rms_norm_rm_ffn))
        add_idual2 = swiglu_ffn + add_idual1
        rms_norm_rm_out = self.rmsNorm_3(add_idual2)

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
🧠 GPT-2
GPT-2 Small — causal transformer block
12 layers · 84.33M
🦙 LLaMA-3 Block
LLaMA-3 decoder block — GQA
10 layers · 702.55M