Udemy
Turn what you know into an opportunity and reach millions around the world.
Learn More
Your cart is empty.
Keep shopping
Build and Fine-Tune a Small LLM from Scratch in PyTorch
New
1 students

Build and Fine-Tune a Small LLM from Scratch in PyTorch

Code a 50M Llama-style LLM from random weights, then fine-tune it with SFT and DPO, test it, shrink it and serve it
Created byAnil Kumar
Last updated 11/2026
English

What you'll learn

  • Explain how an LLM works inside: tokens, embeddings, attention, next-token prediction, loss and perplexity
  • Build a byte-level BPE tokenizer and a data pipeline that packs about a billion tokens into binary shards
  • Code a modern Llama-style transformer from scratch: RoPE, grouped-query attention, RMSNorm and SwiGLU
  • Pre-train it on one 8 GB GPU with bf16, gradient accumulation, AdamW, a cosine schedule and safe checkpoints
  • Fine-tune a base model into a chat assistant with SFT and an assistant-only loss mask
  • Align it with DPO: preference pairs, the DPO loss, the reference model, beta, and diagnosing a failed run
  • Test a fine-tuned model with perplexity, side-by-side answers and benchmarks, and fine-tune it on your own data
  • Shrink a model with INT8 and 4-bit quantization and serve it with a streaming FastAPI chat server

Course content

8 sections • 30 lectures • 6h 42m total length
  • What We Build: A Complete LLM (Lumen-50M) from Random Weights12:55
  • Why We Pre-Train First, Then Fine-Tune and Align12:14
  • Hardware Reality, Lab Setup, and Your Options Without a Big Nvidia GPU21:10
  • Codebase Tour, Presets, Real Logs, Smoke Test and a Chat with the Model10:54

Requirements

  • Basic Python: functions, classes, running scripts, installing packages.
  • Basic terminal use: moving between folders and running commands.
  • To train the model yourself: Linux (or Windows with WSL2), Python 3.10 or newer, and an Nvidia GPU with 8 GB of memory. Without one, you can follow every real run in the lectures, train the small setting on a normal computer, or use a cloud GPU

Description

This course contains the use of artificial intelligence

Everyone uses large language models. Very few people know what happens inside one, and even fewer have trained and

fine-tuned one themselves. Companies now fine-tune their own models on their own data, and they need engineers who

understand every step, not only how to call an API.

In this course you build a complete small language model from random numbers, in pure PyTorch, and then turn it into

a chat assistant. It is a real, modern design: a 10-block, Llama-style transformer with 53,507,200 parameters, trained

on about one billion tokens on a single 8 GB laptop GPU.

First you build the model. You start with the basics: tensors, loss, gradients and next-token prediction. Then you

build a byte-level BPE tokenizer with 16,384 tokens, a data pipeline that filters, mixes and packs text into binary

shards, and the transformer itself: token embeddings, RoPE, causal attention with grouped-query attention, RMSNorm and

SwiGLU. You train it with a real engine: gradient accumulation, bf16, AdamW, a learning-rate schedule, resumable

checkpoints and a GPU heat guard. You watch the loss fall from 9.82 to 2.93, and you make it write text with

temperature, top-k, top-p and a KV-cache.

Then the heart of the course: fine-tuning. A base model only continues text. You turn it into an assistant with

supervised fine-tuning on 20,000 real chat conversations, with a loss mask so the model learns only from the

assistant's words. Then you align it with Direct Preference Optimization on 5,000 pairs of good and bad answers: the

DPO loss in code, the reference model, beta, reward accuracy and margin. And when alignment goes wrong (it does: the

model starts repeating itself), you diagnose it honestly instead of hiding it.

Then you test, shrink and serve it. You measure the model with perplexity, a sliding window and bits per byte, you

compare base, SFT and DPO answers side by side, and you learn the honest limits of a small model. You repeat the whole

fine-tuning process on your own data. Finally you shrink the model to INT8 (204.6 MB down to 81.8 MB, with perplexity

27.73 to 27.81) and 4-bit, and serve it with a streaming FastAPI chat server at about 108 tokens per second.

Everything is real. Every number, loss value, answer and timing on screen comes from a real run of the real code.

The full project is on GitHub, so you can rebuild and retrain every part yourself. Every package we use is open

source, and every training dataset is openly licensed and credited.

How it is taught. Face to face and step by step: every new word is explained with an everyday picture first, every

idea is shown as a moving animation, and every code file is opened and explained block by block.

What you need. Basic Python. An Nvidia GPU with 8 GB helps to train the model yourself, but it is not required to

follow the course: the lectures show every real run, and the smaller setting runs on a normal computer.

By the end, you will be able to build, pre-train, fine-tune, align, test, shrink and serve a language model of your

own, and you will understand every line that does it.

Who this course is for:

  • Python developers who want to understand how LLMs really work, not only call them through an API.
  • Engineers who need to fine-tune language models on their own data and want to know every step.
  • Students of machine learning who want one complete, real project from raw text to a chat server.
  • Working professionals moving into AI who want hands-on experience with pre-training, SFT and DPO.