PyTorch for LLMs: Pro Course — Build Language Models from Scratch
Learn PyTorch by building LLMs from scratch: tokenisers, attention, transformers, training loops, distributed training, fine-tuning, and deployment. 13 modules, 65+ hours.
Course Snapshot
| Course Code | AIM-608 |
| Duration | 65+ hours |
| Modules | 13 |
| Projects | 5 |
| Phase | 5 — PyTorch & Multimodal AI |
| Skill Level | Intermediate to Advanced |
| Format | Self-paced + live cohorts |
| Price | ₹7,999 (early bird ₹5,499) |
| Last Updated | July 2026 |
Course Overview
If you have ever loaded a Hugging Face model and wondered what actually happens inside the forward pass, this course is your answer. We build every component of a modern language model from scratch in PyTorch — the tokeniser, the embedding layer, positional encoding, multi-head attention, the full transformer block, and the training loop that turns random weights into a language model. You will not just call ''nn.TransformerEncoder'; you will write the attention math yourself, understand why gradient clipping matters, and debug a training run that diverges. From there we move to modern LLM architectures — RMSNorm, RoPE, SwiGLU, grouped-query attention, and mixture-of-experts — implementing each one in raw PyTorch before comparing with production implementations. The course then covers distributed training with DDP and FSDP, fine-tuning with LoRA written from scratch, inference optimisation with KV caching and continuous batching, and deployment with custom serving code. Thirteen modules, five projects, and sixty-five hours of deep, hands-on PyTorch engineering. By the end, you will not just use LLMs — you will understand them at the tensor level and be able to build, train, and deploy your own.
Ready to Start Learning?
Join 1,000+ AI professionals advancing their careers with aimodels.in training programs.
Enroll Now — Limited Seats →Who This Course Is Built For
This course is designed for specific professional profiles. If you match any of these, you will get maximum value.
ML Engineers
Engineers who want to go beyond calling Hugging Face APIs and understand the internals of transformer models at the PyTorch tensor level.
AI Researchers
Researchers who need to implement and modify novel architectures, custom attention mechanisms, or training algorithms in raw PyTorch.
Backend & Systems Engineers
Engineers building inference infrastructure who need to understand KV caching, attention computation, and memory management at the code level.
CS Students & Self-Learners
Learners who want a rigorous, from-scratch understanding of how language models work rather than a black-box API course.
This Course Is NOT For You If:
- Those who have never written Python or used NumPy — start with a Python programming course first.
- Anyone looking for a prompt engineering or no-code course — this is deep PyTorch implementation work.
- Learners without access to a GPU — labs require at least a Colab T4 for training exercises.
- Those who only want to use pretrained models via API — this course is about building and training, not calling.
What You Will Learn
After completing this course, you will be able to:
- 1 You will be able to implement a byte-pair encoding tokeniser from scratch in Python
- 2 You will be able to write multi-head self-attention and cross-attention in raw PyTorch
- 3 You will be able to build a complete transformer decoder model from individual layers
- 4 You will be able to write a training loop with loss computation, backprop, and gradient clipping
- 5 You will be able to implement modern LLM components: RMSNorm, RoPE, SwiGLU, and GQA
- 6 You will be able to train a small language model from random initialization on a custom corpus
- 7 You will be able to run distributed training with PyTorch DDP and FSDP across multiple GPUs
- 8 You will be able to implement LoRA fine-tuning from scratch without the PEFT library
- 9 You will be able to optimise inference with KV caching, continuous batching, and speculative decoding
- 10 You will be able to deploy a custom-trained model with a hand-written inference server
Download Free Course Syllabus
Get the complete detailed syllabus with all modules, lessons, and project descriptions delivered to your inbox.
Download Free Syllabus →Complete Course Curriculum
13 modules with detailed lessons. Every lesson includes specific learning points.
MODULE 1 — PyTorch Foundations
5 hours- Understand PyTorch tensors as the fundamental data structure for all LLM operations
- Master autograd: how PyTorch builds a computation graph and computes gradients
- Use requires_grad, retain_grad, and torch.no_grad() to control gradient computation
- Debug gradient flow with hooks and gradient norm tracking
- Build custom layers by subclassing nn.Module with __init__ and forward methods
- Register parameters, buffers, and sub-modules correctly
- Use nn.Parameter to create trainable weights inside custom layers
- Chain modules with nn.Sequential and inspect model architecture with named_parameters
- Configure SGD, Adam, and AdamW optimizers with appropriate learning rates
- Use cosine annealing, linear warmup, and polynomial learning rate schedulers
- Implement gradient clipping with torch.nn.utils.clip_grad_norm_
- Save and load model checkpoints with state_dict and torch.save
MODULE 2 — Tokeniser from Scratch
5 hours- Implement the BPE algorithm: merge rules, vocabulary building, and encoding
- Train a BPE tokeniser on a text corpus with configurable vocabulary size
- Handle special tokens: BOS, EOS, PAD, and UNK
- Compare your BPE implementation with Hugging Face tokenizers and tiktoken
- Implement pre-tokenization with regex patterns for words, spaces, and punctuation
- Handle Unicode and multilingual text in the tokeniser pipeline
- Build encode and decode functions that are inverse-compatible
- Profile tokeniser throughput and optimize for batch encoding
- Create nn.Embedding layers with custom vocabulary sizes and dimensions
- Understand the relationship between token IDs and embedding vectors
- Implement tied embeddings vs separate input/output embeddings
- Visualise embedding space with t-SNE and PCA for trained models
MODULE 3 — Attention Implementation
6 hours- Implement the core attention formula: softmax(QK^T / sqrt(d_k))V in PyTorch
- Understand why we scale by sqrt(d_k) to prevent gradient vanishing in softmax
- Build causal masks for autoregressive (decoder-only) models
- Debug attention weight distributions and identify common issues
- Split the embedding dimension into multiple heads for parallel attention
- Implement head projection with linear layers for Q, K, and V
- Concatenate and project multi-head outputs back to the model dimension
- Profile memory usage of multi-head attention and identify bottlenecks
- Understand the tiling strategy that makes Flash Attention memory-efficient
- Implement a simplified online-softmax attention in PyTorch
- Use PyTorch's F.scaled_dot_product_attention with Flash Attention backend
- Benchmark standard attention vs Flash Attention in speed and memory
MODULE 4 — Transformer from Scratch
6 hours- Implement sinusoidal positional encoding from the original Attention is All You Need paper
- Build learned positional embeddings as an alternative
- Understand why decoder-only models use RoPE (Rotary Position Embedding)
- Implement RoPE from scratch and compare with sinusoidal encoding
- Build the standard two-layer MLP with GELU activation
- Implement LayerNorm and RMSNorm from scratch with learnable parameters
- Understand pre-norm vs post-norm architecture and their training stability
- Compare GELU, SwiGLU, and ReLU feed-forward variants
- Combine attention, feed-forward, residual connections, and normalisation into a block
- Stack N transformer blocks into a complete decoder-only model
- Implement the language modeling head for next-token prediction
- Initialize weights with Xavier/Glorot and understand initialization impact on training
MODULE 5 — Training a Language Model
6 hours- Write a complete training loop: forward pass, loss, backward, optimizer step
- Implement cross-entropy loss for next-token prediction with label shifting
- Add gradient accumulation to simulate larger batch sizes on limited GPU memory
- Log training metrics: loss, perplexity, learning rate, and gradient norm
- Build a custom Dataset and DataLoader for text corpora with proper batching
- Implement variable-length sequence handling with padding and attention masks
- Use packing to concatenate documents and maximize GPU utilization
- Create efficient data pipelines with prefetching and num_workers
- Train a 10M parameter GPT model on a small text corpus (Shakespeare or Wikipedia)
- Monitor training loss curves and identify convergence vs divergence
- Generate text samples during training to qualitatively assess model improvement
- Save checkpoints and resume training from a saved state
MODULE 6 — Modern LLM Architecture
5 hours- Implement RMSNorm as the modern replacement for LayerNorm in Llama and Mistral
- Build the SwiGLU feed-forward block: SiLU activation with gating mechanism
- Compare parameter counts and throughput of SwiGLU vs standard MLP
- Understand why RMSNorm + SwiGLU improves training stability and inference speed
- Implement RoPE with complex number rotation of query and key vectors
- Understand how RoPE encodes relative position without learned parameters
- Extend RoPE to long contexts with NTK-aware scaling and YaRN
- Compare RoPE with ALiBi and learned absolute position embeddings
- Implement Grouped-Query Attention (GQA) to share K and V across query heads
- Understand the memory and speed trade-offs of GQA vs MHA vs MQA
- Build a Mixture-of-Experts (MoE) layer with top-k routing and expert networks
- Implement load balancing loss for MoE training stability
MODULE 7 — Distributed Training
5 hours- Understand the difference between DataParallel (DP) and DistributedDataParallel (DDP)
- Set up DDP with torch.distributed and multiple process groups
- Launch multi-GPU training with torchrun and environment variables
- Profile DDP communication overhead and gradient synchronization
- Understand FSDP as the PyTorch equivalent of DeepSpeed ZeRO-3
- Shard model parameters, gradients, and optimizer states across GPUs
- Configure FSDP with mixed precision and activation checkpointing
- Benchmark FSDP vs DDP on memory usage and training throughput
- Implement tensor parallelism by splitting attention heads across GPUs
- Understand pipeline parallelism with micro-batching for very large models
- Combine FSDP with tensor parallelism for 3D parallelism
- Use torch.distributed.device_mesh for flexible parallelism strategies
MODULE 8 — Fine-Tuning Implementations
5 hours- Implement LoRA without the PEFT library: low-rank matrices A and B
- Inject LoRA adapters into specific linear layers of a transformer
- Train only LoRA parameters while freezing the base model
- Merge LoRA weights back into the base model for deployment
- Implement 8-bit and 4-bit quantization wrappers for linear layers
- Understand straight-through estimators for gradient flow through quantization
- Train with quantized weights using bitsandbytes integration
- Compare quality of quantized fine-tuning vs full-precision training
- Implement the DPO loss function from scratch with a reference model
- Build a simple PPO training loop with reward model and KL penalty
- Implement GRPO with group-relative advantage estimation
- Compare convergence and stability of DPO, PPO, and GRPO implementations
MODULE 9 — Inference Optimisation
5 hours- Implement KV cache for autoregressive generation to avoid recomputing attention
- Understand cache shape, cache updating, and cache management across batches
- Benchmark generation speed with and without KV caching
- Implement cache compression with H2O (Heavy-Hitter Oracle) attention
- Understand continuous batching for variable-length request handling
- Implement a simplified PagedAttention scheme with block-based KV cache
- Compare static batching vs continuous batching in throughput and latency
- Integrate continuous batching with a custom request queue
- Implement speculative decoding with a draft model and acceptance sampling
- Use dynamic quantization with torch.quantization for CPU inference
- Benchmark INT8 and INT4 inference with FP16 baseline
- Combine speculative decoding with KV caching for maximum speedup
MODULE 10 — Custom Model Components
4 hours- Implement sliding window attention for long-context models
- Build linear attention with kernel approximations for O(n) complexity
- Implement multi-query attention and compare with grouped-query attention
- Experiment with sparse attention patterns for document-level tasks
- Implement ALiBi (Attention with Linear Biases) as an alternative to RoPE
- Build custom activation functions: GeGLU, ReGLU, and gated variants
- Experiment with different normalisation schemes: DeepNorm and ScaleNorm
- Profile the impact of each component on training and inference speed
- Modify a Hugging Face model to add custom layers or attention variants
- Implement mixture-of-depths: dynamic per-token computation allocation
- Build a custom loss function for multi-task training
- Test architectural changes on a small scale before scaling up
MODULE 11 — Evaluation & Benchmarking
4 hours- Compute perplexity correctly for language models with proper masking
- Track training and validation perplexity curves for convergence analysis
- Implement bits-per-character (BPC) for cross-tokeniser comparison
- Detect overfitting and underfitting from perplexity curves
- Implement evaluation harness for MMLU, GSM8K, and HumanEval in PyTorch
- Build a few-shot evaluation pipeline with prompt formatting
- Run zero-shot and few-shot evaluation on custom-trained models
- Compare your model against published benchmarks for similar sizes
- Use torch.profiler to identify CPU and GPU bottlenecks in training
- Debug NaN losses with anomaly detection and gradient hooks
- Profile memory usage with torch.cuda.memory_allocated and memory_reserved
- Optimize data loading and forward pass with torch.compile
MODULE 12 — Deployment
4 hours- Write a FastAPI server that loads a PyTorch model and serves completions
- Implement streaming responses with Server-Sent Events for token-by-token output
- Add request batching and queueing for concurrent request handling
- Configure GPU memory management with torch.cuda.empty_cache and context managers
- Export PyTorch models to ONNX for cross-platform inference
- Use torch.compile and inductor for graph-level optimisations
- Convert models to TensorRT for NVIDIA GPU optimisation
- Benchmark exported models vs native PyTorch inference
- Implement latency and throughput monitoring with Prometheus metrics
- Track token-level latency and identify generation bottlenecks
- Set up health checks and graceful degradation for inference servers
- Log model outputs for quality monitoring and drift detection
MODULE 13 — Capstone Projects
5 hours- Build a complete GPT-style transformer in raw PyTorch
- Train on a text corpus and generate coherent text samples
- Evaluate perplexity and compare with a baseline
- Document the architecture and training configuration
- Design and implement a novel attention variant (sliding window, linear, or sparse)
- Integrate it into a transformer model and train
- Benchmark against standard multi-head attention
- Write a technical report on the trade-offs
- Scale training to multiple GPUs with DDP or FSDP
- Implement KV caching and speculative decoding for fast inference
- Deploy the model behind a custom FastAPI server with streaming
- Present all projects with code, benchmarks, and documentation
Real-World Projects & Portfolio Outcomes
5 portfolio-worthy projects that prove your skills to employers.
GPT from Scratch
Build a complete GPT-style transformer model entirely in raw PyTorch — tokeniser, embeddings, multi-head attention, transformer blocks, and training loop. Train on a text corpus and generate coherent samples.
Custom Attention Mechanism
Design, implement, and benchmark a novel attention variant — sliding window, linear, or sparse attention — integrated into a transformer model. Compare against standard multi-head attention in quality and speed.
Distributed Training at Scale
Scale a transformer training run across multiple GPUs using PyTorch DDP or FSDP. Configure distributed process groups, profile communication overhead, and benchmark against single-GPU training.
Optimised Inference Engine
Implement KV caching, continuous batching, and speculative decoding for a trained model. Benchmark generation throughput and latency against a naive autoregressive baseline.
Production Inference Server
Deploy a custom-trained model behind a FastAPI server with streaming responses, request batching, GPU memory management, and Prometheus monitoring. Include health checks and graceful degradation.
Tools, Technologies & Models Covered
LLM Models Covered
| GPT-2 Small | OpenAI (repro) | Training from scratch |
|---|---|---|
| Llama 2 7B | Meta | Fine-tuning, DDP, FSDP |
| Mistral 7B | Mistral AI | Architecture study, LoRA |
| TinyLlama 1.1B | TinyLlama Project | Fast iteration training |
| Qwen 2.5 0.5B | Alibaba | Small-scale experiments |
| Custom GPT | Built in course | From-scratch implementation |
Tools & Frameworks Covered
| PyTorch | Core tensor and autograd framework | Deep |
|---|---|---|
| torch.distributed | DDP and FSDP distributed training | Deep |
| torch.compile | Graph-level optimisation | Intermediate |
| torch.profiler | Performance profiling | Intermediate |
| FastAPI | Inference server deployment | Intermediate |
| ONNX | Cross-platform model export | Intermediate |
| TensorRT | NVIDIA GPU optimisation | Intermediate |
| bitsandbytes | Quantization for fine-tuning | Intermediate |
| Weights & Biases | Experiment tracking | Intermediate |
Book a Free Counselling Call
Not sure which course is right for you? Book a free 1:1 counselling call with our AI training advisors.
Book Free Counselling Call →Your Instructor
Dr. Priya Sharma
Principal ML Infrastructure Engineer
10+ years in deep learning systems and PyTorch
Built custom training frameworks used by 3 AI startups
2,800+ engineers trained
You do not understand a transformer until you have written the attention computation yourself, watched the loss curve, and debugged a NaN. This course makes you do exactly that.
Course Team & Curriculum Design
Our teaching team includes PyTorch contributors and ML infrastructure engineers who have built custom training frameworks from scratch. TAs provide code review and debugging support during live cohorts.
Learning Path & Prerequisites
Prerequisites
- Intermediate to advanced Python programming
- Basic PyTorch: tensors, nn.Module, training loops (or willingness to learn fast)
- Linear algebra: matrix multiplication, dot products, and softmax
- Understanding of neural network training: loss, gradients, and backpropagation
- Access to a GPU (Colab T4 minimum, local GPU recommended for later modules)
Recommended Learning Paths
ML Engineer wanting model internals
- Complete Modules 1–5 to build a transformer from scratch
- Take Module 6 for modern architecture components
- Choose Modules 8–9 for fine-tuning and inference skills
- Build the GPT-from-scratch and inference server projects
Researcher implementing novel architectures
- Focus on Modules 3–4 and Module 6 for attention and architecture
- Deep-dive into Module 10 for custom components
- Complete the custom attention mechanism project
- Use Module 7 for scaling experiments to multiple GPUs
Infrastructure engineer building inference systems
- Take Modules 1–3 for foundational PyTorch and attention
- Focus on Module 9 for inference optimisation techniques
- Complete Module 12 for production deployment
- Build the optimised inference engine and server projects
What Comes After This Course
Pricing & Enrollment
Choose the plan that fits your learning goals. All plans include a 7-day money-back guarantee.
| Self-Paced | ₹7,999 | All 13 modules, 5 projects, community Discord, lifetime access |
|---|---|---|
| Early Bird | ₹5,499 | Same as Self-Paced — limited time before cohort launch |
| Cohort Live | ₹14,999 | Live sessions, TA support, code review, and certificate |
What Is Included
- 13 modules with 50+ hands-on lab notebooks
- 5 capstone projects with code review
- Pre-configured Colab and multi-GPU training scripts
- Private Discord community with instructors and TAs
- Lifetime access including future updates
- Certificate of completion with project portfolio
30-day money-back guarantee. Complete the first 4 modules and if the course is not the right fit, get a full refund — no questions asked.
Frequently Asked Questions
Do I need prior PyTorch experience?
You need basic PyTorch familiarity — tensors, nn.Module, and a simple training loop. Module 1 provides a refresher, but this is not a beginner PyTorch course. If you are completely new, we recommend completing a basic PyTorch tutorial first.
Will I really build a transformer from scratch?
Yes. By Module 4 you will have written multi-head attention, positional encoding, feed-forward networks, layer normalisation, and a complete transformer block — all in raw PyTorch without using nn.TransformerEncoder. Module 5 has you train it on a real text corpus.
Do I need multiple GPUs for the distributed training module?
No. Module 7 includes Colab notebooks that simulate multi-GPU training on a single GPU. For true multi-GPU training, you can use Kaggle (2x T4) or cloud GPU rentals (RunPod, Lambda). We provide scripts for both single-GPU simulation and real multi-GPU setups.
How is this different from the Fine-Tuning LLMs course (AIM-607)?
AIM-607 teaches you to fine-tune existing models using libraries like PEFT, TRL, and Unsloth. This course (AIM-608) teaches you to build, train, and deploy models from scratch in raw PyTorch. You will understand the internals that those libraries abstract away.
What GPU do I need for the training exercises?
A Google Colab free T4 (16GB) is sufficient for Modules 1–6 and most projects. For distributed training (Module 7) and larger models, a local GPU with 24GB+ VRAM or a cloud rental is recommended. We provide size-appropriate models for every GPU tier.
Is this course useful if I only use Hugging Face models?
Absolutely. Understanding the internals helps you debug training issues, choose the right architecture, write custom loss functions, and optimise inference. Many students report that this course transformed how they use Hugging Face libraries.
Will I get a certificate?
Yes. Complete all 5 capstone projects and submit them for code review. Upon passing, you receive a certificate with links to your project portfolio, suitable for sharing on LinkedIn and with employers.
What to Learn Next
Continue your AI learning journey with these recommended courses.
Fine-Tuning LLMs: Advanced Batch
LoRA, QLoRA, DPO, and GRPO for training custom models
Explore Course →Explore All Training Programs
Browse all 12 courses across 6 phases of professional AI training at aimodels.in.
View All Courses →