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 CodeAIM-608
Duration65+ hours
Modules13
Projects5
Phase5 — PyTorch & Multimodal AI
Skill LevelIntermediate to Advanced
FormatSelf-paced + live cohorts
Price₹7,999 (early bird ₹5,499)
Last UpdatedJuly 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. 1 You will be able to implement a byte-pair encoding tokeniser from scratch in Python
  2. 2 You will be able to write multi-head self-attention and cross-attention in raw PyTorch
  3. 3 You will be able to build a complete transformer decoder model from individual layers
  4. 4 You will be able to write a training loop with loss computation, backprop, and gradient clipping
  5. 5 You will be able to implement modern LLM components: RMSNorm, RoPE, SwiGLU, and GQA
  6. 6 You will be able to train a small language model from random initialization on a custom corpus
  7. 7 You will be able to run distributed training with PyTorch DDP and FSDP across multiple GPUs
  8. 8 You will be able to implement LoRA fine-tuning from scratch without the PEFT library
  9. 9 You will be able to optimise inference with KV caching, continuous batching, and speculative decoding
  10. 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
Tensors, Gradients, and Autograd
  • 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
nn.Module and Custom Layers
  • 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
Training Utilities: Optimizers and Schedulers
  • 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
Byte-Pair Encoding (BPE) Implementation
  • 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
Tokeniser Engineering Details
  • 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
Embedding Layers and Vocabulary
  • 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
Scaled Dot-Product Attention
  • 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
Multi-Head Attention
  • 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
Optimised Attention: Flash Attention Concepts
  • 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
Positional Encoding
  • 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
Feed-Forward Networks and Layer Normalisation
  • 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
Assembling the Full Transformer Block
  • 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
The Training Loop
  • 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
Data Loading for Language Models
  • 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
Training a Small GPT from Scratch
  • 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
RMSNorm and SwiGLU
  • 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
Rotary Position Embedding (RoPE)
  • 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
Grouped-Query Attention and Mixture of Experts
  • 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
DataParallel and DistributedDataParallel
  • 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
Fully Sharded Data Parallel (FSDP)
  • 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
Tensor Parallelism and Pipeline Parallelism
  • 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
LoRA from Scratch
  • 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
Quantization-Aware Fine-Tuning
  • 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
Preference Optimization in PyTorch
  • 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
KV Caching
  • 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
Continuous Batching and PagedAttention
  • 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
Speculative Decoding and Quantized Inference
  • 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
Custom Attention Variants
  • 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
Custom Positional and Activation Functions
  • 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
Extending Existing Architectures
  • 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
Perplexity and Loss Metrics
  • 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
Benchmarking Against Standard Tasks
  • 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
Profiling and Debugging
  • 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
Building a Custom Inference Server
  • 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
Model Export and Optimisation
  • 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
Production Monitoring
  • 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
Project 1: GPT from Scratch
  • 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
Project 2: Custom Attention Mechanism
  • 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
Project 3–5: Distributed, Inference & Deployment
  • 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.

Project 1

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.

Deliverable: A trained model checkpoint, generation samples, perplexity evaluation, and a documented architecture report.
Project 2

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.

Deliverable: A custom attention implementation, benchmark results, and a technical write-up on trade-offs.
Project 3

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.

Deliverable: A distributed training script, multi-GPU benchmark report, and scaling efficiency analysis.
Project 4

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.

Deliverable: An optimised inference module, benchmark comparisons, and a streaming generation demo.
Project 5

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.

Deliverable: A production-ready inference server, Docker deployment config, and monitoring dashboard.

Tools, Technologies & Models Covered

LLM Models Covered

GPT-2 SmallOpenAI (repro)Training from scratch
Llama 2 7BMetaFine-tuning, DDP, FSDP
Mistral 7BMistral AIArchitecture study, LoRA
TinyLlama 1.1BTinyLlama ProjectFast iteration training
Qwen 2.5 0.5BAlibabaSmall-scale experiments
Custom GPTBuilt in courseFrom-scratch implementation

Tools & Frameworks Covered

PyTorchCore tensor and autograd frameworkDeep
torch.distributedDDP and FSDP distributed trainingDeep
torch.compileGraph-level optimisationIntermediate
torch.profilerPerformance profilingIntermediate
FastAPIInference server deploymentIntermediate
ONNXCross-platform model exportIntermediate
TensorRTNVIDIA GPU optimisationIntermediate
bitsandbytesQuantization for fine-tuningIntermediate
Weights & BiasesExperiment trackingIntermediate

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

  1. Complete Modules 1–5 to build a transformer from scratch
  2. Take Module 6 for modern architecture components
  3. Choose Modules 8–9 for fine-tuning and inference skills
  4. Build the GPT-from-scratch and inference server projects

Researcher implementing novel architectures

  1. Focus on Modules 3–4 and Module 6 for attention and architecture
  2. Deep-dive into Module 10 for custom components
  3. Complete the custom attention mechanism project
  4. Use Module 7 for scaling experiments to multiple GPUs

Infrastructure engineer building inference systems

  1. Take Modules 1–3 for foundational PyTorch and attention
  2. Focus on Module 9 for inference optimisation techniques
  3. Complete Module 12 for production deployment
  4. Build the optimised inference engine and server projects

What Comes After This Course

  • [object Object]
  • [object Object]
  • [object Object]

Pricing & Enrollment

Choose the plan that fits your learning goals. All plans include a 7-day money-back guarantee.

Self-Paced₹7,999All 13 modules, 5 projects, community Discord, lifetime access
Early Bird₹5,499Same as Self-Paced — limited time before cohort launch
Cohort Live₹14,999Live 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 →

Multimodal AI: Master Course

Build models that process vision, audio, and text

Explore Course →

Inference Optimization

vLLM, quantization, and production serving

Explore Course →

Hugging Face Course

Master the Hugging Face ecosystem for models and datasets

Explore Course →

Explore All Training Programs

Browse all 12 courses across 6 phases of professional AI training at aimodels.in.

View All Courses →