- rasbt/LLMs-from-scratch एक repository है जिसमें GPT जैसे LLM को develop, pretrain और fine-tune करने का code है, और यह Manning की किताब Build a Large Language Model (From Scratch) की official code repository है
- सीखने का तरीका educational उद्देश्य से छोटा लेकिन काम करने वाला model शुरुआत से बनाने की प्रक्रिया पर आधारित है, और यह ChatGPT के पीछे मौजूद बड़े foundation model बनाने की approach जैसी flow को follow करता है
- इसमें text data processing, attention mechanism, GPT implementation, unlabeled data pretraining, text classification fine-tuning, और instruction-following fine-tuning तक chapter-wise code और notebooks दिए गए हैं
- मुख्य chapters का code सामान्य notebooks पर reasonable समय में चलने के लिए design किया गया है, उपलब्ध होने पर GPU का automatic उपयोग करता है, और external LLM libraries के बिना PyTorch में implement किया गया है
- appendices और bonus materials LoRA, KV Cache, MoE, Llama/Qwen/Gemma परिवारों के implementations, evaluation, DPO, और UI examples तक विस्तार करते हैं, जिससे LLM learning process को practice-focused तरीके से और व्यापक बनाया जा सकता है
repository का उद्देश्य और किताब से संबंध
- rasbt/LLMs-from-scratch GPT जैसे LLM को शुरुआत से implement करने की code repository है
- इसे Manning की किताब Build a Large Language Model (From Scratch) की official code repository के रूप में उपलब्ध कराया गया है
- किताब की संरचना ऐसी है कि step-by-step coding के जरिए समझा जा सके कि LLM अंदर से कैसे काम करते हैं
- explanation में text, diagrams और examples शामिल हैं
- educational उद्देश्य से छोटा लेकिन काम करने वाला model खुद develop और train किया जाता है
- repository में बड़े pretrained model weights load करके fine-tune करने वाला code भी शामिल है
- किताब की जानकारी:
installation और code usage
- repository को ZIP download या
git clone से प्राप्त किया जा सकता है
git clone --depth 1 https://github.com/rasbt/LLMs-from-scratch.git
- अगर Manning website से code bundle लिया गया है, तो latest updates GitHub की official repository में check करने की सलाह दी गई है
- Python और package installation, code environment setup को setup/README.md में cover किया गया है
- problem solving document Troubleshooting Guide के रूप में उपलब्ध है
chapter-wise learning flow
- किताब और repository LLM implementation को step-by-step curriculum में बांटते हैं
- मुख्य chapters की संरचना:
- Ch 1: LLM को समझना, कोई code नहीं
- Ch 2: text data handling
- Ch 3: attention mechanism implement करना
- Ch 4: GPT model को शुरुआत से implement करना
- Ch 5: unlabeled data से pretraining
- Ch 6: text classification के लिए fine-tuning
- Ch 7: instruction-following fine-tuning
- appendices में PyTorch introduction, references, exercise solutions, training loop improvements, और LoRA-based parameter-efficient fine-tuning शामिल हैं
prerequisite knowledge और execution environment
- सबसे महत्वपूर्ण prerequisite knowledge Python programming की basics है
- deep learning neural networks का experience हो तो कुछ concepts अधिक familiar लग सकते हैं
- code external LLM libraries के बिना PyTorch में शुरुआत से implement किया गया है
- PyTorch में proficiency जरूरी नहीं है
- PyTorch की basic knowledge मददगार है
- Appendix A PyTorch का छोटा introduction देता है
- मुख्य chapters का code सामान्य notebook पर reasonable समय में चलने के लिए design किया गया है
- special hardware की जरूरत नहीं है, और GPU हो तो यह automatically इस्तेमाल करता है
video lecture और follow-up book
- Manning में किताब की structure follow करने वाला 17 घंटे 15 मिनट का companion video lecture है
- यह किताब के हर chapter और section structure को reflect करता है
- इसे independent alternative या coding along के लिए supplementary material के रूप में इस्तेमाल किया जा सकता है
- follow-up nature वाली किताब Build A Reasoning Model (From Scratch) भी introduce की गई है
- यह independent किताब है, लेकिन Build A Large Language Model (From Scratch) की follow-up मानी जा सकती है
- pretrained model से शुरू करके reasoning ability improve करने की approaches implement करती है
- शामिल approaches: inference-time scaling, reinforcement learning, distillation
- related repository: rasbt/reasoning-from-scratch
exercises और bonus materials
- हर chapter में कई exercises शामिल हैं
- solutions Appendix C में summarized हैं, और संबंधित code notebooks हर chapter folder में हैं
- Manning website से free 170-page PDF Test Yourself On Build a Large Language Model (From Scratch) प्राप्त की जा सकती है
- इसमें हर chapter के लगभग 30 quizzes और solutions शामिल हैं
-
मुख्य bonus topics
- Setup:
- Python setup tips
- packages और libraries installation
- Docker environment setup
- Ch 2:
- BPE tokenizer को शुरुआत से implement करना
- कई BPE implementations की comparison
- embedding layer और linear layer का difference
- simple numbers के जरिए dataloader intuition
- Ch 3:
- efficient multi-head attention implementations की comparison
- PyTorch buffers को समझना
- Ch 4:
- FLOPs analysis
- KV Cache
- Grouped-Query Attention, Multi-Head Latent Attention, Sliding Window Attention
- Gated DeltaNet, DeepSeek Sparse Attention, Cross-Layer KV Sharing
- Mixture-of-Experts
- Ch 5:
- alternative weight loading methods
- Project Gutenberg dataset pretraining
- training loop improvements
- hyperparameter optimization
- pretrained LLM से interact करने वाला UI
- GPT को Llama में convert करना
- memory-efficient model weight loading
- Tiktoken BPE tokenizer extension
- faster LLM training के लिए PyTorch performance tips
- Llama 3.2, Qwen3, Gemma 3, Olmo 3, Tiny Aya, Qwen3.5, Gemma 4 implementations
- Ch 6:
- other layers और larger models को fine-tune करने के additional experiments
- 50k IMDb movie review dataset classification fine-tuning
- GPT-based spam classifier UI
- Ch 7:
- near-duplicate detection और passive-voice items generate करने के लिए dataset utilities
- OpenAI API और Ollama से instruction response evaluation
- instruction fine-tuning dataset generation और improvement
- Llama 3.1 70B और Ollama से preference dataset generation
- DPO से LLM alignment implement करना
- instruction-fine-tuned GPT model UI
contribution और citation
- feedback और questions Manning Forum या GitHub Discussions पर लिए जाते हैं
- यह printed book से corresponding code repository है, इसलिए फिलहाल main chapter code की contents को expand करने वाली contributions स्वीकार नहीं की जा सकतीं
- यह restriction physical book और code के बीच difference बनने से रोकने के लिए है
- अगर research में किताब या code उपयोगी हो, तो citation की सलाह दी जाती है
- Chicago-style citation और BibTeX entries उपलब्ध हैं
1 टिप्पणियां
Hacker News टिप्पणियाँ
अब तक finetuning guide सबसे अच्छा resource लग रहा है
https://ravinkumar.com/GenAiGuidebook/language_models/finetu...
LLM एक अहम विषय है, लेकिन इस पर सतही videos और articles बहुत हैं। मुझे लगता है कि नीचे से LLM code करने पर कई concepts साफ हो जाते हैं
secondary goal यह भी है कि जिन लोगों को जरूरत हो, वे अपना LLM बना सकें। किताब में pretraining और finetuning समेत पूरी pipeline code की गई है, लेकिन आर्थिक रूप से LLM pretrain करना व्यावहारिक नहीं लगता, इसलिए pretrained weights load करना भी दिखाया जाएगा
GPT-2 जैसे LLM का इस्तेमाल करके सब कुछ शुरुआत से implement किया जाएगा, और laptop पर चलने वाले 124M model से लेकर छोटे GPU पर चलने वाले 1558M model तक के weights load किए जा सकेंगे। असल में लोग HF transformers या axolotl जैसे framework इस्तेमाल करेंगे, लेकिन उम्मीद है कि यह खुद implement करने वाला तरीका प्रक्रिया को कम black box जैसा दिखाएगा
import torchहै, तो यह पूरी तरह from scratch implementation जैसा नहीं लगता :-)इस क्षेत्र में काम करने वालों में कोई भी ऐसे models के derivatives हाथ से calculate नहीं करता। differentiable programming के नजरिए से सोचना basic premise है, और इस मामले में इसे पर्याप्त रूप से “from scratch” माना जा सकता है
ऐसे comments देखते समय हर बार शक होता है कि commenter को अंदर क्या होता है या modern machine learning कैसे काम करती है, इसकी अच्छी समझ नहीं है
[1] https://karpathy.ai/zero-to-hero.html
सामान्य developers के लिए यह कहीं ज्यादा accessible है, और math background मानकर नहीं चलता। यह अच्छा starting point है, जिसके बाद दूसरे मिलते-जुलते resources बेहतर समझ में आने लगते हैं
educational material बनाने में अविश्वसनीय रूप से बहुत काम लगता है, और यह किताब कितनी भी सफल हो, rasbt अगर लगाए गए समय के मुकाबले income calculate करें तो hourly rate के हिसाब से बात नहीं बनेगी
इस topic को समझने वाले लोग बहुत हैं, लेकिन उन्होंने उस knowledge का क्या किया? उसे अपने पास रखा और OpenAI जाकर knowledge को private रखते हुए कहीं ज्यादा पैसा कमाया
अगर हम ऐसी दुनिया में रहना चाहते हैं जहाँ यह knowledge खुली हो, तो ठीक-ठाक dinner जितनी कीमत वाली किताब के बारे में publicly शिकायत करने से तो बचना ही चाहिए
लक्ष्य यह है कि कोई चीज lunar lander की तरह landing सीख सके। simple रूप में 100 feet ऊँचाई से शुरू करके एक direction में thrust देना, और crater कम बनने तक बार-बार कोशिश करना
फिर horizontal movement जैसे variables जोड़ना, horizontal thrusters लगाना, और बाद में horizontal thrusters हटाकर lander को rotate करने देना—इस तरह expand करना चाहता हूँ
बिलकुल नहीं पता कहाँ से शुरू करूँ, और यह किताब “mainstream” machine learning जैसी लगती है, इसलिए सोच रहा हूँ कि क्या इससे मदद मिलेगी
Python की gymnasium[1] library में lunar lander environment है, उसे देखना अच्छा रहेगा। सीखते समय मैंने सबसे ज्यादा इसी environment पर ध्यान दिया था और इसे कुछ तरीकों से solve किया था
कुछ समय पहले PyTorch में Soft Actor Critic implement करते समय इस्तेमाल की गई मेरी notebook2 भी देख सकते हैं। यह सिखाने के लिए बहुत अच्छा resource नहीं है, लेकिन शायद कुछ मिल जाए
[0]: https://www.manning.com/books/grokking-deep-reinforcement-le...
[1]: https://gymnasium.farama.org/environments/box2d/
AlphaGo जैसी चीज़ों को भी आखिरकार क्लासिक reinforcement learning techniques के input के रूप में deep neural network इस्तेमाल करने वाला काम माना जा सकता है
Sutton और Barto की Reinforcement Learning: An Introduction को इस विषय की definitive introductory book माना जाता है
कुछ दूसरी general machine learning/deep learning books में reinforcement learning introduction chapters काफी लंबे लिखे हुए हैं (https://github.com/rasbt/machine-learning-book/tree/main/ch1...). फिर भी इस case में, जैसा दूसरों ने कहा है, dedicated reinforcement learning book ज़्यादा सही रहेगी
https://www.ida.liu.se/~TDDC17/info/labs/rl.en.shtml
[0] https://www.youtube.com/watch?v=kCc8FmEb1nY
character-level LLM के बजाय actual word-level LLM implement करती है, pretraining के बाद pretrained weights load करना दिखाती है, और उस LLM की instruction fine-tuning करती है
साथ ही instruction fine-tuned LLM का alignment process code करती है, और classification task के लिए fine-tuning भी दिखाती है। पूरी book में बहुत illustrations हैं, और सिर्फ chapter 3 में ही 26 figures हैं :)
Video भी अच्छा लगता है। 2 घंटे का है, इसलिए solid introductory supplementary material के रूप में अच्छा हो सकता है। Book पढ़ने में शायद उसका करीब 10 गुना समय लगेगा
मैंने भी ज़्यादातर चीज़ों को अच्छी तरह समझने के लिए इसे कई बार देखा
जाहिर है, PyTorch भी बहुत अच्छी तरह आना चाहिए, और matrix multiplication, backpropagation वगैरह भी पता होना चाहिए। बोलने की speed भी बहुत तेज़ है
उदाहरण के लिए, मुझे पता है कि attention कई तरह के models में इस्तेमाल होता है, और Transformer भी language models के अलावा दूसरी जगहों पर इस्तेमाल होता है
जानना चाहता हूँ कि क्या इस book को देखने से attention और Transformer को language model के बाहर भी इस्तेमाल करने लायक अच्छी समझ मिल पाएगी
फर्क यह है कि LLM में text को tokens में बदला जाता है, और उन tokens को LLM में जाने वाली vector embeddings में convert किया जाता है। Vision Transformer में image को token मानने के बजाय image patches को tokens के रूप में इस्तेमाल किया जाता है, और उन्हें vector embeddings में बदला जाता है
Text हो या vision, attention mechanism वही है, और दोनों cases में input के रूप में vector embeddings लेते हैं
(*chapter 3 मैंने पिछले हफ्ते ही submit किया था और जल्द ही MEAP पर आ जाएगा। तब तक code को notes के साथ यहाँ देखा जा सकता है: https://github.com/rasbt/LLMs-from-scratch/blob/main/ch03/01...)