Design, Train, and Deploy LLMs with PyTorch
Dilyan Grigorov

#LLM
#NTK
#AI
#PyTorch
#MoE
#SwiGLU
#RoPE
#GPU
#RLHF
#CUDA
🧠 ساخت Large Language Model از صفر
📘 این کتاب یک راهنمای کامل و دستبهکار برای طراحی، آموزش و دیپلوی **Large Language Model (LLM)**های خودته؛ از پایههای Tokenization گرفته تا مرحلههای پیشرفته Fine-Tuning و یادگیری تقویتی. کتاب برای دولوپرها، دانشمندهای داده و متخصصهای AI نوشته شده و بین اصلهای بنیادی و تکنیکهای State-of-the-Art پل میزنه؛ با یک نگاه شفاف و کمیاب به اینکه Transformerهای مدرن واقعاً زیر سطح چطور کار میکنن.
🛠️ از مبانی شروع میکنی و یاد میگیری چطور محیط توسعهات رو با Python و PyTorch راهاندازی کنی، دیتاستها رو مدیریت کنی، و پایههای حیاتی مثل Tensorها، Embeddingها و Gradient Descent رو پیادهسازی کنی. بعد وارد قلب معماری مدلهای مدرن میشی و موضوعهایی مثل RMS Normalization، Rotary Positional Embeddings (RoPE)، Scaled Dot-Product Attention، Grouped Query Attention (GQA)، Mixture of Experts (MoE) و Activationهای SwiGLU رو پوشش میدی؛ هرکدوم عمیق بررسی میشن و قدمبهقدم با کد ساخته میشن.
⚡ همینطور که جلو میری، کتاب یکپارچهسازی Custom CUDA Kernelها رو معرفی میکنه و بهت یاد میده چطور کامپوننتهای کلیدی رو در سطح GPU برای سرعت و مصرف حافظه بهتر بهینهسازی کنی؛ مهارتی ضروری برای اسکیل کردن LLMهای واقعی. همینطور روی فازهای آموزشیای مسلط میشی که مدلهای پیشرو امروز رو شکل میدن:
🧱 Pretraining — ساخت فهم عمومی زبانی و معنایی
🎯 Midtraining — گسترش قابلیتهای دامنهمحور و سازگاریپذیری
🧑🏫 Supervised Fine-Tuning (SFT) — همراستا کردن رفتار مدل با دادههای گزینششده و تسکمحور
🏆 Reinforcement Learning from Human Feedback (RLHF) — پالایش پاسخها از طریق بهینهسازی مبتنی بر پاداش برای همراستایی با انسان
📊 فصلهای پایانی تو رو در مسیر آمادهسازی دیتاست، فیلتر کردن، Deduplication و بهینهسازی آموزش جلو میبرن و در نهایت به Evaluation مدل و Prompting دنیای واقعی با یک TokenGenerator سفارشی برای تولید متن و Inference میرسن.
🚀 تا پایان این کتاب، دانش و اعتمادبهنفس لازم رو داری تا مدلهای Transformer-Based خودت رو معماری، آموزش و دیپلوی کنی؛ با ترکیبی از عمق تئوری و مهارت عملی که برای نوآوری در دنیای سریعاً در حال تحول AI لازم داری.
🎯 چیزهایی که یاد میگیری
⚙️ چطور محیط توسعه خودت رو با استفاده از PyTorch پیکربندی و بهینهسازی کنی
🧩 مکانیزمهای Tokenization، Embeddingها، Normalization و سازوکارهای Attention رو میفهمی
🧠 کامپوننتهای Transformer مثل RMSNorm، RoPE، GQA، MoE و SwiGLU رو از صفر پیادهسازی میکنی
🚀 Custom CUDA Kernelها رو یکپارچه میکنی تا محاسبات Transformer سریعتر بشن
🏗️ کل پایپلاین آموزش LLM رو یاد میگیری: Pretraining، Midtraining، Supervised Fine-Tuning و RLHF
🧹 تکنیکهای آمادهسازی دیتاست، Deduplication، دیباگ مدل و مدیریت حافظه GPU رو به کار میگیری
🤖 یاد میگیری چطور یک معماری کامل شبیه GPT رو برای تسکهای واقعی آموزش بدی، ارزیابی کنی و دیپلوی کنی
👤 این کتاب برای چه کسانیه؟
👨💻 این کتاب برای دولوپرهای نرمافزار، دانشمندهای داده، مهندسهای یادگیری ماشین و علاقهمندهای AI نوشته شده که میخوان مدلهای خودشون رو از صفر بسازن.
📖 فهرست مطالب
فصل ۱. Large Language Model چیست؟ شروع کار با کتابخونهها و راهاندازی محیط برای ساخت یک LLM از صفر
فصل ۲. کانسپتهای بنیادی در توسعه LLM
فصل ۳. ساخت یک Tokenizer برای مدل معماری Transformer
فصل ۴. RMS Normalization و پیکربندی مدل
فصل ۵. Rotary Positional Embeddings: یکپارچهسازی NTK و YaRN Scaling
فصل ۶. هسته Scaled Dot-Product Attention، Sliding Window و Grouped Query Attention؛ هسته پشت همه مدلهای Transformer
فصل ۷. AttentionBlock با Rotary Embedding، GQA، Sliding Window و Sink Tokenها
فصل ۸. بلوک Multilayer Perceptron با Mixture of Experts (MoE) و SwiGLU
فصل ۹. Transformer Block و مدل کامل Transformer؛ وقت کنار هم گذاشتن قطعههای پازله
فصل ۱۰. آمادهسازی دیتاست، آموزش مدل، Token Generator برای Inference و Prompting؛ لحظه بزرگ
فصل ۱۱. آموزش پیشرفته و CUDA Kernelها
👤 درباره نویسنده
👨💻 دیلیان گریگوروف یک دولوپر نرمافزاره که به توسعه نرمافزار با Python، Generative Deep Learning و یادگیری ماشین، ساختارهای داده و الگوریتمها علاقه زیادی داره. او طرفدار Open Source و خود زبان Python است. دیلیان ۱۶ سال تجربه صنعتی در برنامهنویسی با Python داره و ۵ سال از این مدت رو صرف پژوهش و تست راهکارهای Generative AI کرده.
🔎 علاقه او به این حوزه از پیشزمینهاش بهعنوان متخصص SEO میاد؛ جایی که هر روز با الگوریتمهای موتورهای جستوجو سروکار داشته. او از تعامل با کامیونیتی نرمافزار لذت میبره و معمولاً در Meetupهای محلی و کنفرانسهای بزرگتر Talk ارائه میده. در وقت آزادش کتاب میخونه، در کوهستان پیادهروی میکنه، قدمهای طولانی میزنه، با پسرش بازی میکنه و پیانو مینوازه.
This book is a complete, hands-on guide to designing, training, and deploying your own Large Language Models (LLMs)—from the foundations of tokenization to the advanced stages of fine-tuning and reinforcement learning. Written for developers, data scientists, and AI practitioners, it bridges core principles and state-of-the-art techniques, offering a rare, transparent look at how modern transformers truly work beneath the surface.
Starting from the essentials, you’ll learn how to set up your environment with Python and PyTorch, manage datasets, and implement critical fundamentals such as tensors, embeddings, and gradient descent. You’ll then progress through the architectural heart of modern models, covering RMS normalization, rotary positional embeddings (RoPE), scaled dot-product attention, Grouped Query Attention (GQA), Mixture of Experts (MoE), and SwiGLU activations, each explored in depth and built step by step in code. As you advance, the book introduces custom CUDA kernel integration, teaching you how to optimize key components for speed and memory efficiency at the GPU level—an essential skill for scaling real-world LLMs. You’ll also gain mastery over the phases of training that define today’s leading models:
The final chapters guide you through dataset preparation, filtering, deduplication, and training optimization, culminating in model evaluation and real-world prompting with a custom TokenGenerator for text generation and inference.
By the end of this book, you’ll have the knowledge and confidence to architect, train, and deploy your own transformer-based models, equipped with both the theoretical depth and practical expertise to innovate in the rapidly evolving world of AI.
What You’ll Learn
Who this book is for:
Software developers, data scientists, machine learning engineers and AI enthusiasts looking to build their models from scratch.
Table of Contents
Chapter 1. What Is a Large Language Model? Getting Started with Libraries and Environment Setup for Building an LLM from Scratch
Chapter 2. Foundational Concepts in LLM Development
Chapter 3. Building a Tokenizer for the Transformers Architecture Model
Chapter 4. RMS Normalization and Model Configuration
Chapter 5. Rotary Positional Embeddings: Integrating NTK and YaRN Scaling
Chapter 6. Scaled Dot-Product Attention Core—Sliding Window and Grouped Query Attention—The Core Behind All Transformer Models
Chapter 7. AttentionBlock with Rotary Embedding, GQA, Sliding Window, and Sink Tokens
Chapter 8. Multilayer Perceptron Block with Mixture of Experts (MoE) and SwiGLU
Chapter 9. Transformer Block and Full Transformer Model—It's Time to Put the Puzzle Together
Chapter 10. Dataset Preparation, Model Training, Token Generator for Inference and Prompting—The BIG Moment
Chapter 11. Advanced Training and CUDA Kernels
About the Author
Dilyan Grigorov is a software developer with a passion for Python software development, generative deep learning & machine learning, data structures, and algorithms. He is an advocate for open source and the Python language itself. He has 16 years of industry experience programming in Python and has spent 5 of those years researching and testing Generative AI solutions. His passion for them stems from his background as an SEO specialist dealing with search engine algorithms daily. He enjoys engaging with the software community, often giving talks at local meetups and larger conferences. In his spare time, he enjoys reading books, hiking in the mountains, taking long walks, playing with his son, and playing the piano.









