Architectures / NLP/LLM
φ Phi-3 Mini Block
Phi-3 Mini 3.8B decoder block — full MHA (32H, 3072D), SwiGLU FFN (8192), RMSNorm, RoPE
Every number on this page is computed from the graph by the same functions the app runs, not written by hand.
When to pick it
Structure
12 layers. Output shapes are propagated from the input shape, batch dimension excluded.
| Layer | Type | Parameters | Output shape | |
|---|---|---|---|---|
| 1 | tokens | Input | shape=[1, 2048] | 1 × 2048 |
| 2 | embed | Embedding | 1 × 2048 × 3072 | |
| 3 | rope | RoPE | ||
| 4 | norm_attn | RMSNorm | normalizedShape=3072 | 1 × 2048 × 3072 |
| 5 | attn | Grouped Query Attn | embedDim=3072, numHeads=32, numKVHeads=32 | 1 × 2048 × 3072 |
| 6 | residual_1 | Add | 1 × 2048 × 3072 | |
| 7 | norm_ffn | RMSNorm | normalizedShape=3072 | 1 × 2048 × 3072 |
| 8 | ffn | SwiGLU | embedDim=3072, intermediateSize=8192 | 1 × 2048 × 3072 |
| 9 | residual_2 | Add | 1 × 2048 × 3072 | |
| 10 | norm_out | RMSNorm | normalizedShape=3072 | 1 × 2048 × 3072 |
| 11 | lm_head | Linear | outFeatures=32064 | 1 × 2048 × 32064 |
| 12 | output | Output | 1 × 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.