RisetSumber Terbuka 🇷🇺 27.07.2026 20:04

Writing a Decoder-Only Transformer for LLM from Scratch in Python

Google/DeepMindGoogle/DeepMind OpenAIOpenAI MetaMeta
The author of a series of articles describes in detail the implementation of a transformer block for a small decoder-only LLM using the PyTorch framework. Components covered include: Multi-Head Attention with masking, Feed Forward Network with GELU, normalization layer, and residual connections. The article explains the difference from the original architecture: pre-normalization (Pre-LN) is used instead of post-normalization for training stability.
Penulis, Vladimir, dengan menggunakan contoh tugas watermarking teks berlisensi, mengimplementasikan blok transformer untuk decoder-only LLM (Large Language Model). Ia menjelaskan bahwa transformer adalah arsitektur dari makalah Google "Attention Is All You Need", di mana suatu urutan diproses secara paralel menggunakan mekanisme attention. Inti dari Multi-Head Attention terletak pada tiga matriks proyeksi Q, K, V. Pada tahap pertama dihitung kemiripan antar-token (matriks attention), sedangkan pada tahap kedua konten token dicampur berdasarkan bobot yang telah ditemukan. Demi efisiensi, seluruh head attention diimplementasikan melalui satu proyeksi linear besar. Setelah proses attention, diterapkan Feed Forward Network dengan aktivasi GELU (Gaussian Error Linear Unit) dan dropout. Penyusunan transformer mencakup normalisasi (Pre-LN, atau Pre-Layer Normalization) serta koneksi residual. Selain itu, ditambahkan pula causal mask (mask masa depan) untuk decoder. Secara keseluruhan, blok tersebut sudah siap digunakan dalam penyusunan LLM, pelatihan model, maupun proses generasi teks.
Sumber: Habr — хаб NLP — asli
Postingan kami sebelumnya tentang topik ini ↓
Berita terbaru