研究オープンソース 🇷🇺 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.
著者ウラジーミルは、ライセンステキストの透かし入れタスクの例を用いて、decoder-only LLM用のトランスフォーマーブロックを実装しています。彼は、トランスフォーマーがGoogleの論文「Attention Is All You Need」に基づくアーキテクチャであり、系列がアテンション機構によって並列に処理されることを説明しています。Multi-Head Attentionの基礎には、三つの射影行列Q、K、Vがあります。第一段階ではトークン間の類似度(アテンション行列)が計算され、第二段階では得られた重みによってトークンの内容が混合されます。効率化のため、全てのアテンションヘッドは一つの大きな線形射影として実装されています。アテンションの後には、GELU活性化関数とドロップアウトを備えたFeed Forward Networkが適用されます。トランスフォーマーの組み立てには、正規化(Pre-LN)と残差接続が含まれます。また、デコーダー用に未来マスク(causal mask)も追加されます。全体として、このブロックはLLMの構築、学習、テキスト生成に使用できる状態にあります。
出典: Habr — хаб NLP — 原文
関連記事 ↓
新着ニュース