Skip to content
0:00 / 0:00

#gpt#transformer#pytorch

Let's build GPT: from scratch, in code, spelled out.

Andrej Karpathy

1.7M subscribers • 6 videos on Hallway

YouTube

Intermediate. Assumes working knowledge.

8.1M views3 years ago14 recommendations

AI and machine learningProgramming languagesTransformers and LLMsNeural networks and deep learningPython

Andrej Karpathy builds a Generative Pretrained Transformer from scratch using PyTorch, explaining the mechanics of attention and language models.

We build a Generatively Pretrained Transformer (GPT), following the paper "Attention is All You Need" and OpenAI's GPT-2 / GPT-3. We talk about connections to ChatGPT, which has taken the world by storm. We watch GitHub Copilot, itself a GPT, help us write a GPT (meta :D!) . I recommend people watch the earlier makemore videos to get comfortable with the autoregressive language modeling framework and basics of tensors and PyTorch nn, which we take for granted in this video. Links: - Google colab for the video: colab.research.google.com/drive/1JMLa53HDuA-i7ZBmqV7ZnA3c_fv… - GitHub repo for the video: github.com/karpathy/ng-video-lecture - Playlist of the whole Zero to Hero series so far: youtube.com/watch?v=VMj-3S1tku0&list=PLAqhIrjkxbuWI23v9cThsA… - nanoGPT repo: github.com/karpathy/nanoGPT - my website: karpathy.ai - my twitter: twitter.com/karpathy - our Discord channel: discord.gg/3zy8kqD9Cp Supplementary links: - Attention is All You Need paper: arxiv.org/abs/1706.03762 - OpenAI GPT-3 paper: arxiv.org/abs/2005.14165 - OpenAI ChatGPT blog post: openai.com/blog/chatgpt/ - The GPU I'm training the model on is from Lambda GPU Cloud, I think the best and easiest way to spin up an on-demand GPU instance in the cloud that you can ssh to: lambdalabs.com . If you prefer to work in notebooks, I think the easiest path today is Google Colab. Suggested exercises: - EX1: The n-dimensional tensor mastery challenge: Combine the `Head` and `MultiHeadAttention` into one class that processes all the heads in parallel, treating the heads as another batch dimension (answer is in nanoGPT). - EX2: Train the GPT on your own dataset of choice! What other data could be fun to blabber on about? (A fun advanced suggestion if you like: train a GPT to do addition of two numbers, i.e. a+b=c. You may find it helpful to predict the digits of c in reverse order, as the typical addition algorithm (that you're hoping it learns) would proceed right to left too. You may want to modify the data loader to simply serve random problems and skip the generation of train.bin, val.bin. You may want to mask out the loss at the input positions of a+b that just specify the problem using y=-1 in the targets (see CrossEntropyLoss ignore_index). Does your Transformer learn to add? Once you have this, swole doge project: build a calculator clone in GPT, for all of +-*/. Not an easy problem. You may need Chain of Thought traces.) - EX3: Find a dataset that is very large, so large that you can't see a gap between train and val loss. Pretrain the transformer on this data, then initialize with that model and finetune it on tiny shakespeare with a smaller number of steps and lower learning rate. Can you obtain a lower validation loss by the use of pretraining? - EX4: Read some transformer papers and implement one additional feature or change that people seem to use. Does it impro

Why it's here

14 people recommend this

What people say. Widely recommended as the single best, most intuitive video for understanding how transformers and LLMs work under the hood.

Moments people linked to

yonzon Hacker News • Recommends

On “The Transformer Family”

Great compilation, would be great to see Vision Transformer (ViT) included. Andrej Karpathy's GPT video is a must have companion for this link to this video I was going nuts trying to grok Key, Query,Position and Value until Andrej broke it down for me.

durdnon Hacker News • Recommends

On “GPT in 60 Lines of NumPy”

Very impressive. Recently I watched this really amazing lecture on building GPT from scratch from Karpathy, I was blown away: link to this video

wesammikhailon Hacker News • Recommends

On “Google DeepMind CEO Says Some Form of AGI Possible in a Few Years”

Andrej Karpathy, the director of AI at tesla, published an AMAZING video about how the current models are built which I think everyone in the tech space should watch. It is intuitive, easy to follow and quite frankly the best video I have seen on the topic. Here is the link: link to this video If you conclude, after watching this, that this is all intelligence is - namely cascading optimization and data structure problems chained in a row, then you and a fair number of people here wont ever find common ground. That is not to say that you're wrong, it just seems as if there is a missing component here that we haven't yet discovered.

villedespommeson Hacker News • Recommends

On “Implementing a ChatGPT-like LLM from scratch, step by step”

+1, Andrey is an amazing educator! I'd also recommend his link to this video and checking out his github repos. MinGPT, for example, implements a small gpt model that's compatible with HF API, whereas more modern nanoGPT shows how to use newer features such as flash attention. The quality of every video, every blog post is just so high.

amrbon Hacker News • Recommends

On “Deep Learning Tuning Playbook”

I recommend this video on the subject. "Let's build GPT: from scratch, in code, spelled out." link to this video

qazpoton Hacker News • Recommends

On “Flow-Based Programming, a way for AI and humans to develop together”

…training is done, LLMs can generate text given a prompt and the probabilities that it has learned. The analogy of LLMs being auto-complete on steroids is very apt. - Whether the text generated by LLMs is factual or not is purely coincidental. I would highly recommend watching and working though the code (if possible) of NanoGPT by Andrej Karpathy. link to this video A lot of things which seem like magic about LLMs will get demystified. Now one can argue that LLMs are showing human like reasoning/intelligence/sentience as an emergent behavior. This is hard to argue against because all these terms are extremely hard to define. IMO, the only emergent behavior that LLMs are showing is the output they gener…

bfg_damienon Hacker News • Recommends

On “Ask HN: How to Learn LLM Fundamentals”

Here are a few resources that might help get you started: 1. A thread explaining the internal working of transformers: link 2. Paper by DeepMind which provides pseudo-code for important algorithms for Transformer models: link 3. Another thread specifically on large language models: link Once again these are not courses per se, but do provide intuitive explanations for how transformers work. There is also the nanoGPT series of videos by Karpathy on youtube. First video here: link to this video

pankajdohareyon Hacker News • Recommends

On “What Are Transformer Models and How Do They Work?”

I agree that Karpathy's YouTube video is an excellent resource for understanding Transformers from scratch. It provides a hands-on experience that can be particularly helpful for those who want to implement the models themselves. Here's the link to the video titled "Let's build GPT: from scratch, in code, spelled out": link to this video Additionally, for more comprehensive resources on Transformers, you may find these resources useful: * The Illustrated Transformer by Jay Alammar: link * MIT 6.S191: Recurrent Neural Networks, Transformers, and Attention: link * Karpathy's course, Deep Learning and Generative Models (Lecture 6 covers Transformers): link ... These resources cover different aspects…

erwincoumanson Hacker News • Recommends

On “Transformers from Scratch”

Andrej Karpathy's 2 hour video and code is really good to understand the details of Transformers: "Let's build GPT: from scratch, in code, spelled out." link to this video

Alpion Hacker News • Recommends

On “Introducing "Implement DNS in a Weekend"”

I love all the series about writing your own X in 100 lines of code. It gives you the understanding of technology and removes a lot of unnecessary details. The great examples of this are 'A from-scratch tour of Bitcoin in Python' link and 'Let's build GPT: from scratch, in code, spelled out' link to this video from Andrej Karpathy I wonder if anybody tried to collect all such projects together and built his own 'Internet in just 100 lines of code'

FranklinMailloton Hacker News • Recommends

On “Ask HN: Can someone ELI5 Transformers and the “Attention is all we need” paper”

Those Computerphile videos0 by Rob Miles helped me understand transformers. He specifically references the "Attention is all you need" paper. And for a deeper dive, Andrej Kharpaty has this hands-on video1 where he builds a transformer from scratch. You can check-out his other videos on NLP as well they are all excellent. 0 link , link 1 link to this video

samspencon Hacker News • Recommends

On “Andrej Karpathy: Intro to Large Language Models [Video]”

I initially thought this was his older video, but I see this video was published just an hour ago (from the time of this comment). I was thinking of this other video he had published early this year: "Let's build GPT: from scratch, in code, spelled out" link to this video Karpathy is generally well-reputed as a good tutor, especially for complex topics in AI / ML.

solomatovon Hacker News • Recommends

On “Ask HN: Is it feasible to train my own LLM?”

Yes, it's doable. Your model won't be as large and as performant as a real large scale model, but you could train something. You could watch this for a start: link to this video

yregon Hacker News • Recommends

On “A ChatGPT clone, in 3000 bytes of C, backed by GPT-2 (2023)”

Attention is all you need and GPT-2 were well known at that point. Many might doubt whether this approach leads to "general" intelligence – depends on the definition. BTW, Karpathy has a nice video tutorial about building an LLM: link to this video

u/Suspicious_Role5912in r/dotnet • Mentions

NanoGptDotnet: An advanced Large Language Model written in C# that generates shakespeare like text. Designed by Andrej Karpathy and based off breakthrough "Attention Is All You Need" article. If anyone is interested in learning AI/LLM's and your more familiar with dotnet than python, take a look at my NanoGptDotnet project. It is a direct translation of a project by Andrej Karpathy, a leading expert in deep learning and LLM's. His project was written while recording the youtube video: Let's build GPT: from scratch, in code, spelled out. The design used in the better model, NanoGpt in my project, is based off the breakthrough Attention Is All You Need article. The project is meant to be a…

psycho brmon Data Science Stack Exchange • Mentions

On “ChatGPT's Architecture - Decoder Only? Or Encoder-Decoder?”

TLDR (simplified): encoder sees into future, decoder predicts transformer sees into future and then predicts, encodes, then decodes gpt doesnt see into future, it only predicts - thats why it's decoder-only you can check karpathy's explanation: link to this video (~2m short, at the linked time, or for full understanding watch the whole 2h video, or even whole ~10h series) basically, gpt decoder doesnt look into future, it is only predicting, so this is the decoding part.. and it is specific in that we dont know the full context of sentence.. however, in case of translating from one language to another, we know the full context from the original sentence, so basically before running the deco…
5View the answer