Hugo speaks with Sebastian Raschka, a machine learning & AI researcher, programmer, and author. As Staff Research Engineer at Lightning AI, he focuses on the intersection of AI research, software development, and large language models (LLMs).
How do you build LLMs? How can you use them, both in prototype and production settings? What are the building blocks you need to know about?
In this episode, we’ll tell you everything you need to know about LLMs, but were too afraid to ask: from covering the entire LLM lifecycle, what type of skills you need to work with them, what type of resources and hardware, prompt engineering vs fine-tuning vs RAG, how to build an LLM from scratch, and much more.
The idea here is not that you’ll need to use an LLM you’ve built from scratch, but that we’ll learn a lot about LLMs and how to use them in the process.
Near the end we also did some live coding to fine-tune GPT-2 in order to create a spam classifier!
Get full access to Vanishing Gradients at hugobowne.substack.com/subscribe
Audio and video play straight from the show's own hosting. PodSubs stores and serves no media.
Transcript
From the show. Tap a time to play from there.
SPEAKER_00Hugo Bowne-Anderson here, your friendly neighborhood podcast host. Today, I'm speaking with Sebastian Rajka, a machine learning and AI researcher, programmer, and author. As staff research engineer at Lightning AI, Sebastian focuses on the intersection of AI research, software development, and large language models, or LLMs. So how do you build LLMs? How can you use them both in prototype and production settings? What are the building blocks you need to know about? In this episode,
we'll tell you everything you need to know about LLMs but were too afraid to ask,
SPEAKER_00from covering the entire LLM lifecycle, what type of skills you need to work with them, what type of resources and hardware you need also, prompt engineering versus fine-tuning versus rag, how to build an LLM from scratch, and much more. The idea here is not that you'll need to use an LLM you've built from scratch ever, but that we'll learn a lot about LLMs and how to use them in the process. A bit of bookkeeping before we jump in. I'd honestly love to hear from you about what resonates with you, what doesn't, and anybody you'd like to hear me speak with, along with topics you'd like to hear more about.
SPEAKER_00The best way to let me know currently is on Twitter. At Vanishing Data is the podcast handle, and I'm at Hugo Bowne. It would be great if you could subscribe to the show on your app of choice, and if you'd like it, do write us a review on iTunes and or anywhere else. This episode was recorded as a YouTube live stream. So when we occasionally refer to people commenting in the chat, that's what we're on about. We plan to have more such live streams and you can subscribe to our channel to keep up to date. The links in the show notes. Near the end, we also did some live coding to fine tune GPT-2 to create a
spam classifier. Although some of it relies on visuals, much of it can be grokked through the audio.
SPEAKER_00So we left it in. If you want to watch it though, do check out the video in the show notes. I'm your host, Hugo Bowne-Anderson, and welcome to Vanishing Gradients. Oh, also, we have a live stream coming up called How to Build Terrible AI Systems. This will be with Jason Liu, who is an independent consultant using his expertise in recommendation systems, among other things, to help fast-growing startups build out their LLM and RAG applications. Jason was previously at Meta and Stitch Fix...
And we'll talk about how to build terrible AI applications in an inverted thinking approach. So envisaging all the failure modes that would result in building terrible AI
SPEAKER_00systems and then figure out how to avoid such pitfalls. This will be a lot of fun. So I've included a link to registration in the show notes.
SPEAKER_01How are you, Sebastian? I'm good. Yeah. Thanks for the nice introduction. And yeah, I'm excited to be here. It's nice to see you again and chat a bit about things we are both excited about. That's AI, LLMs, machine learning. So I'm looking forward to this.
SPEAKER_00I'm really excited about it as well. And so we actually many years ago, maybe six years ago, did a podcast as well for the DataFrame podcast. And that was more on machine learning and your work in biology as well. I introduced you with respect to... your research, your being an author, your work at Lightning AI. Can't wait to talk about what you're doing at Lightning and all the fantastic stuff there and how it can help people get started with building LLMs themselves as well. But I could have introduced you by talking about your work on
Psychic Learn or Biopandas or... ML Extend or mention the several books you're working on currently or your
SPEAKER_00newsletter, which I know a lot of people get so much value from. So I thought we could just start by a brief introduction to who you are and what you do and why you love it. And just quickly, we've got people saying hi from Switzerland, from Houston, Texas, and from Angola already. So that's pretty cool. And just quickly to set the stage, you're in the United States of America. In Wisconsin?
SPEAKER_01Not too long ago before I decided to join the startup world, because I do like doing things myself, like coding and these types of things rather than, I mean, I like my time there a lot. In academia, you have a lot of roles from managing research, managing students, teaching. And I do like teaching a lot, but I'm also like a coder and like my focus time. And that brings me also back to why I'm still in Wisconsin. I really do like traveling occasionally to conferences, both coasts, the East Coast, the West Coast, meeting people and stuff.
SPEAKER_01But for me, I also need my focus time. And Wisconsin is nice and quiet. I wouldn't say isolated, but there are also people around here, of course. I do. I'm like a computer person. I am. I like nature. I go outside, but most of the day, you can find me in front of the computer coding and that stuff. Originally, I started as, I would say, computational biologist who got really interested in statistics. That was around 2010-ish, 12-ish. And yeah, I took a class in statistical pattern classification. And from there, I got into machine learning. And then eventually,
back then, AI machine learning, that was almost synonymous, or it still is.
SPEAKER_01And now we call it AI because it's like the flashier term. But yeah, it's been my passion for more than a decade. No, it's 12 years. It's an exciting topic. And you mentioned scikit-learn and this almost feels like a lifetime ago. Yeah, we used scikit-learn a lot back then, contributed a bit there. And I think this was maybe my first tutorial I taught with Andreas Müller back then. I think that's how we met, if I recall correctly. I met you also at SciPython back then.
SPEAKER_00I recall, and you mentioned going to conferences. So yeah, we met, I don't know if you remember, we met in the Schultz beer garden, actually.
SPEAKER_01Yep, yep, yep. It was a hot summer in Austin. It was, I love the SciPy conference though. I'm actually going also this year. It's in July, the first time in a different location. It's a bit bittersweet because, yeah, so I'm excited for Tacoma, but then on the other hand, So many good memories in Austin too. So it will be, I will be looking forward to it. We'll see how things are in Tacoma. Yeah. And it's going to be exciting. I'm also teaching a tutorial there if someone is interested related to LLMs.
SPEAKER_00So what's the tutorial? And congratulations on having a tutorial accepted as well.
SPEAKER_01Yeah, so this tutorial will be on coding LLMs from scratch, but also like showing how to use LLMs for real world use cases. So it's going to be like a journey from understanding LLMs from the bottom up to coding LLMs to then using, let's say, cutting edge efficient LLM libraries, essentially like this journey, the path of Because I like to start usually with understanding things before you treat things as a black box. It's really, let's see how these things work before we actually use them in
practice.
SPEAKER_01So you know about the pitfalls, but also to get a better handle on it, what the things can do and what they can't do. And I think one of the best ways, instead of like hand-wavy explanations, I think one of the best ways to understand the topic is really doing it, really coding it, because then it's really... You can really see, okay, this is the data I feed in, this is what comes out, and this is happening inside of the model. And I think this is a great and fun way of understanding how things work.
SPEAKER_00Amazing. And that couldn't segue more nicely into what we're here to talk about, actually. So we're here to talk about the LLM lifecycle, implementing LLMs from scratch, developing LLM libraries, training LLMs on the cloud, latest models, lots of fun stuff. But I thought we could start a bit more personally. And I'd just love to know, why are you so interested in LLMs? You're spending a lot of time working on them, writing about them, doing podcasts like this. So what is it about LLMs and you that gives you so much energy
and passion about this subject?
SPEAKER_01Yeah, that's a good question. You mentioned like writing about them. I would also say I would add writing with them. So I think that's maybe why I find them also so fascinating is because it's maybe the first kind of LLM model that I use or concept that I use on a daily basis also as a user. Back then I did research in biology, like drug discovery, using machine learning to do drug discovery, finding like molecules that have some certain effect in a human body or in general. And it's something that you do it for research and you build a
model that can
SPEAKER_01predict something. And that's basically a very isolated project in terms of it's not something you would do in your living room in the evening where you, oh, I need my model to classify something now. But with LLMs, it's something where you use it on a daily basis. You write emails. So maybe this is something where you can say, oh, can you proofread this? Or I don't know, I'm like brainstorming something. Can you give me some ideas on that topic that I maybe haven't covered yet or something? Or translate this language from here to another language or some rephrasing of paragraphs.
SPEAKER_01For example, I'm writing a lot. And sometimes when you write a lot, it was a long day. You can get stuck sometimes, writer's block or something. It's like, where, oh, can you rephrase the sentence? Because I don't like the sentence structure. And you feel like you want to just see some options there or something like that. And also for coding, sometimes you have this code, you work on a problem, and then you can't just find out why it's not working. It doesn't always work, but sometimes it does. You just use your code, put it in an element and say, hey, am I missing something here or is there something that doesn't
look right?
SPEAKER_01And sometimes it really gives you a pointer of, oh, you defined this variable twice or something, or you are overwriting something here. So it's like actually quite useful for everyday tasks. And I think this is also like a motivation for like, hey, let's develop better models and let's, yeah, let's push the envelope there and see how we can make them better. But then also LLMs are relatively large. I wouldn't say they're necessarily complicated, but it's still fun to look under the hood how they work. And because that's also inspiring to maybe new architecture or tweaks.
Like it's understanding how these things work and then seeing the limitations,
SPEAKER_01what it can do, what it can't do. And it's also satisfying. I must say when you have an LLM that starts out generating gibberish and then you train it and you can literally see It's starting to put together coherent sentences. It's also fun, of course, to train a classifier, but there's something about seeing a model generating text that is very satisfying and a lot of fun compared to just watching numbers. So it's like a fun project, yeah. And it applies to so many areas. So yeah, I would say that's maybe why I'm so excited about those.
SPEAKER_00And I'm glad you mentioned writing. I find it wonderful for ideation when thinking through things. Yeah. I think the hallucination challenge is something which can be quite challenging there. But for code, as a coding assistant, I find it fantastic. And particularly if my questions or my challenges are something that is somewhat represented in the training set, I do find that they can generally get stuck in some sort of local minima when... but may not be that representative and give you nonsense for some time.
And you need to be smart enough to be able to step out of that and start again, I think.
SPEAKER_00But I think that's a very good use for all of these things. I also just wanted to add, got a lot of people in the chat just saying how much they love your books, including the ones that are currently in development, LLMs from scratch. So that's super cool as well. We've touched on this in your answer to that question, but I'm wondering if there's anything else you want to say about what some of your favorite use cases for LLMs are.
SPEAKER_01Yeah. Since you mentioned code, I mentioned like fixing my code, but now I remember there's also another one I use a lot. So back when I was a student, I still do, I do use Anki, which is like a flashcard, basically system on a computer where you ask, you read something, you learn something and you put yourself, you write a question for yourself and then you kind of try to memorize this. And It's just, I do this a lot for also coding concepts. So back as a student, all the bash stuff, like bash scripting stuff, I usually don't remember.
SPEAKER_01Python I'm really good at, but like anything terminal or shell terminal related, it's something, if I don't use it in a while, I forget it. So I have a lot of these cheat sheets and try to memorize these things. But ChatGPT, for example, or any LLM, basically, it's really taking over that niche where it makes me a bit lazy, but also it frees up so much time and makes me do things faster where a concrete example would be I have, let's say, image files in a folder and I want to change the prefix of the file.
SPEAKER_01So I don't know the file ending. I want to convert them from PNG to JPEG. And instead of just trying to memorize how to do that on the command line or searching the internet, you can just tell basically your LM, for example, ChatGPT, like, hey, I have these files on a macOS terminal, rename them, sorry, rename them or convert them from JPEG to PNG. What is the code? And it gives you like a code snippet, like a for loop in Bash script using a macOS built-in tool. And you just copy and paste that. Of course, double checking that it doesn't do anything weird before you
paste it.
SPEAKER_01You know, usually it is very good at these tasks where, yeah, you can just ask it, okay, can you give me the code for doing X, Y, Z, let's say, like quick things that you usually don't do on a daily basis, but that are very useful. Or even like sometimes I have LaTeX code or something. There are other tools for that, but you can just take a screenshot and say, sorry, if you have an equation and you want the LaTeX code, you can say, what is the LaTeX code shown in this equation here? And then you have the LaTeX code, basically. So it's useful for these little things that I would say various
different unrelated tasks where you don't have to memorize those and you don't have to keep all your
SPEAKER_01cheat sheets. Now, the only downside is what if the LRM goes down one day, right? So what do you do then? So if ChatGPT has connection problems, so in that case, I feel also it's useful to have your own local LLMs or just systems that you can use locally. But in general, I would say these little tasks where you try to usually memorize things, that is a perfect example for LLMs most of the time. They're not perfect, but they get you most of the way there.
SPEAKER_00They're not perfect. They may never be perfect, but we're still in very early days of the technology and... I suppose something I'm hearing in there, there has been a worldview, which I don't think is really sticking around that much, that there'll be one big model to solve all the issues or a set of a handful of massive models. You talked about having your own local LLMs. We can think about having LLMs to solve specific tasks, such as... There are some wonderful ones in biology coming out as well, actually, like gene form and these types of stuff that are used for
bioinformatics and
SPEAKER_00proteomics and that type of stuff. So I suppose my provocation is... Do you see a world or would you like to see a world in which there are a few big models or is a world in which there are lots of different smaller models solving different types of problems, different people interacting with them and that type of stuff? Something you'd like to see more of.
SPEAKER_01I would say both. I think there is utility in having one big model that can do a lot of things. Like similar to a search engine, like Google can find a lot of things on the internet. It's not like specific to finding, let's say flights or finding technical articles or something. It's very general. And I think there's a use for that because you don't have to memorize all the different places you have to go and search. So you can go to this one search engine, just type it in and hope it gives you the same or the correct results. But maybe it's,
SPEAKER_01for example, not the perfect search engine if you really are interested in getting your flights or booking your flights for that. You would then go to the airline's website or other search platforms that specialize in that. In the same way, I can see LLMs, for example, being useful as a general... thing where you don't have to memorize specialized tools. It can do a lot of things, but it's also almost like a jack of all trades. It's maybe really good at a lot of things, but it's maybe not optimized for a certain use case.
SPEAKER_01So if you have resources to optimize for a certain use case, I do think you can get better results. Can you use ChatGPT to specify spam messages? It can probably do that really well. It is probably very accurate. But I think if you have a large data set and you would further fine tune it to classify spam messages, I think it would be even better than that. So it's like there is utility in specializing those. But then from a user's perspective, I do think, yeah, it adds more overhead because now you have to keep multiple copies of models that are relatively big and then also memorizing where you have them,
SPEAKER_01how to run them and stuff like that. But also going back to your biology example, that is definitely something I would like to see more of. I remember, I think, AlphaFold... two, the second alpha fold version. I think they also use the transformer architecture. So it would be exciting to see more stuff like that, where it's not necessarily generating human language. It's generating, in this case, amino acid sequences or in gene form, or like you said, that would be DNA sequences, where it's something where you, where there's definitely a connection, where it's not human language or like written language. It's some other form of code.
SPEAKER_01And I do think there are exciting applications of that. I can also see, I don't know, music or other things like where you have a sequence of things and yeah, you can basically see if it can help you tackling these types of tasks. And so I think biology is a natural one because there are lots of important problems to be solved in biology. There's a huge amount of complexity that we can't necessarily model ourselves as well.
SPEAKER_00I'm excited to really dive into LLMs now, and I've pasted some links to your book, the Manning link, and then to the GitHub repository, which has a bunch of notebooks, so people can check those out. Before that, there are a couple of things I just want to triangulate around. I do think we talked about using LLMs as coding assistants. One other great thing that they're able to do is they can help me get started with a programming language. So I know how to program in Python and R and a couple of other things. But if I wanted to try something in Rust,
SPEAKER_00which people are excited about at the moment, it can help me get absolutely started there. I also just remembered that you used to work in R a lot and you even have a book. In R. And another thing you mentioned, which is the flashcard, we'll be talking about stuff related to your book, Building LLMs from Scratch, but you've got a lot of other books. And if I recall correctly, your love of flashcards essentially resulted in you writing a book called Machine Learning Q&A. I.
SPEAKER_01Yep, that's true. It's actually, as you may know, I'm also very active on social media. I have sometimes posts where I describe concepts in machine learning and AI. And at some point, I thought it would be fun to try out a new book format where I explain concepts a bit longer than you can possibly do in a LinkedIn or Twitter post. So in this book, MLQ and AI, I'm describing like 30 more, I would say, advanced topics that I usually didn't cover in my other textbooks. Basically,
SPEAKER_01like from anything from like self-supposed learning, what are embeddings, how to evaluate machine learning models, the difference between encoder and decoder transformers, how to construct confidence intervals for machine learning, like all these topics that are important, useful, but they don't quite fit into other textbooks, I would say. into introductory textbooks, I should say, where it's slightly out of scope. They are important and interesting to explain, but then when you're teaching something, it's like, oh, I'm going off on a tangent here.
SPEAKER_01I need to keep focused on the introductory parts. And this is a book where I outsourced all these slightly more advanced concepts into a book of 30 self-contained questions. I think it's also a perfect book if you are on a trip or something and You have 10 minutes here and there and you just get out the book and read one chapter. It's usually three, four pages and you learn something new. It's like the perfect book for that or for your lunch break or something like that. It's basically 30 chunks of knowledge where you can just read one chapter that is
SPEAKER_01only like five to six pages, let's say, and you don't have to keep all the rest in memory where... Usually with other textbooks, the LLM from scratch textbook, that's a coding textbook that I want. That way I would recommend that one, yeah, to take some time, maybe an hour and really work through a chapter doing coding where the MLQ and AI is more like, it gives you an infusion, a quick infusion of knowledge, I hope. So where you can learn about a certain topic. And so people who read it, they really liked it. It's a new format that I tried out. I have not seen any other book like that. So I was thinking, okay, it's like an experiment.
SPEAKER_01either people love or hate it. And luckily so far, the feedback has been really positive. So maybe there will be a second volume one day with more questions and answers. But yeah.
SPEAKER_00Amazing. I'll put a link to that in the show notes as well. I'm excited to jump in and really get into the nitty gritty of LLMs now. So I want to talk about at a high level, what the full LLM life cycle is, but I suppose first, maybe you could tell us why it's important to know about these things.
SPEAKER_01Yes. One thing is, of course, at Lightning AI, we work a lot with other companies also and building solutions. And I can imagine also for individuals where you are trying to really solve a problem, it is really useful to know all the steps involved. Knowing the LLM lifecycle helps you to understand basically where, so if it's like a journey from implementing things from scratch to having a fine-tuned model, where in that process you should,
You can always start at the very beginning, but not every project requires that.
SPEAKER_01So it's in a sense, it's useful to know what are the steps and where can I, let's say, build on existing LLMs, for example, and where should I jump in, basically. And the LLM lifecycle, it is like a big term, but you can think of it as the process from... coding an architecture could be GPT, Lama, Phi, all these different types of architectures, which are all like descendants of GPT, but they're all very similar. But then coding that architecture, defining some of the unique properties or like the vocabulary size,
SPEAKER_01the embedding size, the number of transformer blocks, what activation functions to use. These are almost, they seem like arbitrary choices and they are to some extent. It makes sense to get a big picture overview because it also determines the size of the LLM, which then also determines how much data you need and what compute resources you need for training, but then also later for deployment. And yeah, so that would be one aspect, like coding the architecture, pre-training the architecture. That would be like a second step where I've seen projects where most of them that
SPEAKER_01we see nowadays in papers, they are very general LLMs like LAMA, PHY, and so forth. There are models that have been trained on a general data set. But there are also projects where people trained it on more specialized data. For example, there was Bloomberg GPT where they trained the model on finance data because they were interested in like more finance specific LLMs. And so basically it's a trade-off. Is it worth doing that? Because knowing about that, so then you understand basically if you've done that or at least try to do that, how expensive it is.
So is it really worthwhile training your own LLM because it can't cost hundreds of
SPEAKER_01thousands to millions of dollars? Or is it fine to use a pre-trained LLM? And so it's important to understand, I feel like, the steps. So you can choose based on your budget, based on your requirements, timelines, and so forth. And the next step would then be fine-tuning an LLM. And there are also different ways to fine-tune. Most of the LLMs we see when we talk about fine-tuning, they are instruction fine-tuned, which means basically they are fine-tuned again almost on a very general corpus where you make the LLM more,
SPEAKER_01let's say, specific in terms of Not specific. So it keeps the same knowledge, but you make it more able to follow instructions. If you say, summarize this text or translate this text or fix my spelling or give me a function in code that does XYZ. So it's becoming better at fulfilling these requests, like following the instructions. But then usually that's a research topic still, but usually it's not as good as pre-training for instilling new knowledge. So if you want to change the language,
SPEAKER_01like if you have an English LLM and you want to, let's say, make it better at Spanish, I think instruction fine-tuning might work if there has been enough Spanish data in the pre-training stage. But it's not able to teach the LLM a new language, for example. For that, you would go back to the pre-training stage if your language was not already contained in there. So in that case, it makes sense to understand a bit about pre-training and fine-tuning. So you can choose based on your task what is the one that is
worthwhile here.
SPEAKER_01But then sometimes you just want a classifier. You want to just, let's say, classify spam messages and For that one, you probably don't need to instruction fine-tune. You can also just classification fine-tune the model. Or if you have a task where you don't want the LLM to generate necessarily the whole response, you want it more like finding information and document, then that would be where you use a pre-trained LLM or instruction fine-tuned LLM. and build a retrieval augmenter generation system on top of it. So these are all like different,
SPEAKER_01I would say, steps in the pipeline from implementation to pre-training, fine-tuning the RAC system on top of it. And then the last step would probably be deploying the LLM. So that would be also one step. And then, of course, updating the LLM, like monitoring it and updating LLMs. It's a whole process where, yeah, you don't, for every problem, you don't need to do all the steps. For example, some applications, if you, like we talked before, want just your personal assistant, you maybe don't need to necessarily fine tune it.
SPEAKER_01You just download Llama 3 or something, run it on your computer or deploy it in a simple UI, and that should be maybe sufficient. So in that case, knowing about the steps is quite useful about LM lifecycle steps, yeah.
SPEAKER_00So just to recap, and I want to dive into a few of them, but first we have is coding the architecture, then pre-training on some relatively large corpus, then fine tuning, perhaps instruction tuning, although there are other ways to do it, to not introduce necessarily more content per se, but more method. We can say more structure around the types of responses you want and this type of stuff. Then there are design choices around if you want to use rag or something along those lines. We'll get to, later in this conversation, when to think about using RAG or prompt engineering or fine-tuning,
SPEAKER_00then deploying, and then, of course, maintaining and monitoring and these types of things. I have a few kind of questions I want to get into here. We know that... So that's a really nice kind of set of five steps. And I think that's a very nice number to use. There are other things that people do. So I'm wondering, we know that OpenAI did a lot of RLHF, which is reinforcement learning. Whether it's actually reinforcement learning or not, we can talk about it at a different point. But it's reinforcement learning... esque stuff with human feedback and so if people use chat gbt
sometimes it'll ask for a thumb like you can give an answer a thumb up or a thumb down essentially that's you labeling whether it's good or not and then they'll do some sort of reinforcement learning around whether those things work side note everyone may have
SPEAKER_00seen like ChatGBT overuses the word Delve. We saw that recently. And one of the more attractive theories why that's the case is from the reinforcement learning via human feedback. I'll try to find an article, but you can probably Google it, which the idea was that OpenAI contracts a lot of developing nations or a number of developing nations. And the word Delve may be overrepresented in their use of English so that when they saw it, they'd give it a thumb up. And so it wasn't necessarily, wasn't overused in the training data, but it was the RLHF side. That meant it's overrepresented by chat GPT.
But these types of things like RLHF, we see more maybe reinforcement learning via AI as well.
SPEAKER_00And like human computer interactions, HCI stuff. How do these types of things fit into your LLM life cycle?
SPEAKER_01Yeah, good question. So I would say I implicitly only mentioned it, but this type of RLHF would be like an extension or one step in the instruction fine tuning case. RLHF, usually it's like a two-step procedure where you do supervised instruction fine tuning. So that is basically more next token prediction thing. The loss is slightly different from the pre-training. But what you do is basically you have an instruction, the text, the one that you want the LLM to generate. So, for example,
SPEAKER_01if you want to fine-tune or instruction, fine-tuning one task would be summarizing text. So you give it the instruction, summarize this text plus the input text. And then the desired response would be the summarized text that some, let's say, human has written. And then you train the LLM basic, given the input and the instruction, generate exactly that text, basically. So that's the loss function, basically. And after that, so you do that, let's say, on 50,000 examples. After that, you have ideally a similar data set or the same data set with
two different
SPEAKER_01summaries. Now, instead of just having one summary, you have two summaries that are just slight variations of each other. And like you said, one is better than the other. And then you have the thumbs up and thumbs down basically. And then you further refine the instruction fine tuning by saying basically which of the two responses is better given two responses. And actually with a DELF, that is an interesting hypothesis that it was not the training data necessarily, but like the thumbs up and thumbs down responses, like in terms of one, let's say summary or one explanation had a DELF in it and one didn't.
SPEAKER_01So it would be interesting. to test that, to see like a non-instruction fine-tuned LLM, how often does it use the word Delph when it generates text? And then if it generates, if it's fine-tuned after that, comparing proportionally, how often does it generate the word Delph afterwards? It's interesting. For my book, the LMs from Scratch, I used the word, I think, one time. Yeah. And I immediately, on the Manning Forum, there was someone like, hey, you use the word Delft in your text. Did you use ChatGPT to write your book or something?
SPEAKER_01It's funny. It can happen because it's actually a word I was curious. Then I went back to my older books, the ones published before 2018. And I also used the word Delft. There was one book I used, I think, two or three times. So in that case, it's a chicken-egg problem, right? Do the books or in general texts contain Delph because you use ChatGPT? Or did ChatGPT also write Delph because it learned from the books? Or it's like, which way is it, basically? It's interesting. I think it's a legit word. It's just, like you said, it's a bit overused when you use ChatGPT.
SPEAKER_01So nowadays, yeah, it's like you're oversensitive. If you see Delve in an email or something, oh, this guy just probably used ChatGPT.
SPEAKER_00I actually love the word Delve. I wouldn't mind it being used more. I like that word. It's like the sixth finger of the language world, though.
SPEAKER_01So I do use it sparingly. Sometimes I tend to forget. But yeah, when I write emails, I usually avoid this term now. Yeah.
SPEAKER_00Absolutely. I also did want to say there are lots of interesting conversations around different parts of the life cycle. I'm just going to link to... It's an episode of Latent Space, the AI engineering podcast that had Jeremy Howard... The reason I'm linking to this is, so you may recall, dear listeners, and Sebastian, of course, Jeremy, many moons ago, wrote a paper on universal language model fine-tuning, ULMFIT, which suggested,
SPEAKER_00as a precursor to a lot of the techniques we see in GPTs these days, in which... He talked about pre-training, fine-tuning, and he's now in this interview and elsewhere now talking about replacing fine-tuning with what he calls continued pre-training. So I'm just going to read something. I'd love your thoughts on it.
SPEAKER_01So yeah, interesting. Yeah, I have not seen or read the proposal you just mentioned, but Yeah, I kind of didn't really explicitly mention continued pre-training in my brief top-of-my-head lifecycle explanation of LLMs. But yeah, now that you bring that up, it's a good point. It's what I mentioned before is when you want to instill new knowledge into an LLM. So that would be, you still don't need, let's say, to completely pre-train from scratch. You can do continued pre-training.
SPEAKER_01One concrete example on top of my head would be CodeLama. Mm-hmm. So what the researchers at Meta did is they trained a Lama model, the Lama 2 model in this case, for example, to generate generic text, like training it on normal language, spoken language, or written language, let's say. And after that project was done, it was a whole project. It was a paper and stuff like that. And people can use the model, but then they wanted to make a code-specific LLM, like a version of Lama called CodeLama that is specific for writing code.
And in that case, what they did is they started off with the Lama 2 model.
SPEAKER_01And then they continued pre-training it on code, basically. And then also you can, again, follow up with instruction fine-tuning so that it can specifically answer questions about your code and follow instructions. But yeah, so I think this is a very good, important concept for... Yeah, instilling new knowledge into an LLM. You can do that to some extent with instruction fine tuning too. So if you have a lot of questions, for example, if you have an LLM that doesn't know about a concept, but I don't know,
SPEAKER_01let's say history in 2024. So some historic facts from 2024 that have not been part of the pre-training data set. So if, let's say from any date in the future, so it has not been done or used for pre-training, you can create questions and answers of this data and then instruction fine-tune that LLM on it. And to some extent, the LLM will also learn these historic facts that have not been, or these facts from the data that has not been included in the original pre-training set.
SPEAKER_01Now, it's still an open research question whether you should do still continued pre-training on it, or if that is sufficient, if you just use another LLM to generate a Q&A data set from that data. So the loss function is slightly different. Also for pre-training, you really have a next token prediction, like you predict one token at a time. So if you have a text with five words, you have n minus one or four prediction tasks. You give it the first word, predict the second, then you give it the first and second and predict the third. So in that case, for a given sentence,
SPEAKER_01you have, you can think of it as four gradient descent updates almost, or you have four different predictions that you factor into the loss function. where with the Q&A prediction, it's really like one example. So you don't chop up the example, if that makes sense. So it's really like the whole sentence that you feed in. So in that case, one argument can be made that for continued pre-training, it's making maybe better use of your data. Because it's yeah it's having n minus one tasks instead of just one task per
example it's a very
SPEAKER_01subtle distinction but i think yeah this is a valid point where continued pre-training might be more making more effective use of your data but then you can also say yeah why don't i do more epochs over my q and a data set then so it's it's still an open research question i've been starting to look into that too i think both are useful and you can always do both too so it's it doesn't have to be one or the other you can train or continue pre-training on your data. And then you can convert that data into a Q&A data set for instruction fine-tuning.
SPEAKER_00Fantastic. And I love that you mentioned codeloma. That's actually the example that they give in the post I linked to as well. Oh, okay. So we have a lot of people who want to work with LLMs and can to varying degrees. But I'm wondering, I've been writing Python code for ages and I've got a background in... maths and statistics and multivariate calculus and applied maths and that type of stuff. But it's not clear to me that I can always do all the LLM stuff that I'd like to. So my very general question is,
what skills do you need to work with LLMs?
SPEAKER_00And work with LLMs could mean whatever you interpret it to mean. But I'd really be interested in your thoughts on that as an LLM educator, researcher.
SPEAKER_01Yeah. So I would say, yeah, it's almost, you can think of an LLM as a PyTorch model, really, if you are familiar with deep learning. I don't think there is much new in there where you, so if you have done deep learning before, you will recognize basically everything. The whole training loop, you use the same cross entropy loss, you use the same learning rate scheduler, gradient descent, backpropagation, like all these concepts from deep learning. They are the same when you train LLMs in that case. So I think if you have a background in like deep learning,
SPEAKER_01it's pretty easy to get started with LLM training. It's like the same concepts, hyper parameter fine tuning, looking at loss curves and stuff. The only challenge is that LLMs can be bigger. So in that case, so you have to think a bit more, at least for me, it was like that. Before I trained things on a single GPU, for example, when I trained convolutional neural networks, maybe on multiple GPUs sometimes, but It was cheaper to do that. Like you could fit everything into one GPU if you wanted to. You used maybe distributed data parallelism to make it train faster.
But technically, usually most of the time you could fit the whole model into a single GPU.
SPEAKER_01And with LLMs, depending on what size you use, this could be a bit more challenging, which is where you maybe have to pick up some new concepts. So it's still a deep learning. Fundamentally, it's deep learning under the hood. But in addition to that, I think it's computationally adding a few more restrictions, but challenges. So for example, multi GPU training is almost a requirement for fine. It's not required, but I think it helps if you are familiar with multi GPU training, but also like mixed precision training,
SPEAKER_01lower precision formats, like B float 16, which is like a special 16 bit precision format that to lower the memory requirements instead of training models in 32 bits and these types of things. But also maybe saving checkpoints and continuing the print reading later because usually things might crash or you don't have time to finish the training. You want to make checkpoints to maybe if something goes wrong to rewind and stuff like that. So it adds more like these engineering challenges. So for me, that was something where I had to pick up a few new skills.
SPEAKER_01Also, when I joined Lightning AI, coincidentally, we are developing PyTorch Lightning and a library called Fabric, which makes that relatively easy on top of PyTorch. So in PyTorch, all these things exist. Multi-GPU training with fully sharded data parallelism. tensor parallelism since the last PyTorch release too. And also checkpoint, as we can save checkpoints, load them and stuff, but it's very, I would say verbose. So it's, if you use, let's say Seaborn for plotting, I'm not sure if you're familiar. It's a wrapper around, so it's like a wrapper around Matplotlib.
You can do all the things that Seaborn does in Matplotlib, but it would be a lot of code.
SPEAKER_01So in that case, Seaborn makes your life easier that you can focus more on the concepts and what type of plot you create. Rather than, let's say, the nitty-gritty managing of all the colors and all the layouts and stuff. And in the same way, PyTorch Lightning and Fabric help you manage your PyTorch code that you don't have to micromanage all the different GPUs, what they are doing and how to distribute things and stuff like that. So it's really, you can do that once you have the code in the format that PyTorch Lightning expects or Fabric. You can change one line of code and change it from single to
multi-GPU training.
SPEAKER_01You don't have to change anything else or mixed precision training. So in that sense, there are tools that exist to make it easier, but yeah, still there are things to think about when getting into LLMs. Yeah. So I would say mostly from a deep learning perspective, everything should be familiar, but yeah, you have these additional, I would say challenges for scaling up the computing for these larger models, essentially. Yeah.
SPEAKER_00Absolutely. I am interested in, as we both know, a lot of people who work in data science, machine learning may not be that knowledgeable about deep learning. People who don't know that much about deep learning, it seems to me like it is the type of thing you can learn on the fly when trying to work with these types of things, but having some form of knowledge of linear algebra and backprop and this type of stuff is eminently useful, I think, which building things from scratch actually helps you with,
SPEAKER_00right? Mm-hmm.
SPEAKER_01Yeah, that's a good point. I think backpropagation underlies the whole training in a sense, but yeah, also there you can cheat a little bit and you don't have to, let's say, implement gradient descent from scratch. You can do that, but you can also in that way just implement everything in C code. But there is also PyTorch, which makes that easier. For example, instead of calculating the gradients and applying the chain rule, by yourself, there's a method called .backward, which does it for you.
It's still tremendously useful to understand what it is doing under the hood,
SPEAKER_01but you don't explicitly need to write that. What you could technically do is you can write a training loop in PyTorch, and then you get a feeling of, oh, it's calculating gradients, it's computing the derivative of the loss with respect to the weights, basically, and then applying these gradients and stuff like that. But you don't have to immediately, let's say, know all the details or be able to do it by hand. This could be something you can do as a second step, maybe optionally, maybe for a simpler model. To be honest, I wouldn't be able, top of my head,
SPEAKER_01to do that for an LLM because LLMs are very large, have so many layers. And I can do that for a multi-layer perceptron, like a module in the LLM. Doing it for the whole model, that would be a month project, I think, where you sit down a month and write it out because the models are so large and getting it right, it's not trivial. So I think understanding the fundamentals is good, but then you can outsource the hard work to PyTorch, PyTorch Lightning, and approximately what's happening under the hood, which is important, but you don't have to really do it manually.
SPEAKER_01Absolutely.
SPEAKER_00And it is about... I want to say the right abstraction layer. I mean, right. I want to say have error bars or whole distribution of uncertainty around, but that's something I very much appreciate about PyTorch Lightning in terms of what it abstracts away and the code that I need to write in order to get my deep learning working. And, I would say very similar things about scikit-learn. I would say very similar things about Keras. And as you know, I'm in Berlin currently. I've been hanging out with the Spacey team. Say very similar things about Spacey as well. And as you know, listeners may not.
SPEAKER_00I work on Metaflow in my day job. So it's something we think a lot about. I mean, it's a tool that cuts across the infrastructure stack. But we want to make sure that... You can use an at Kubernetes decorator, for example, as a data scientist or machine learning engineer, and not have to worry about kubectl and configuring your cluster and all of these types of things. So I think these types of developer tools and abstraction layers are incredibly important in order to get us working as well as possible with the machines. We've talked about what skills you need to work with
SPEAKER_00LLMs. And I love your take there. It's very different to scikit-learn or Pandas or Seaborn or back in the day, we used to like open notebooks and just Pandas read CSV. Wow. In an iPython notebook pre-Jupyter. But it isn't the same with LLMs now. My laptop may not be enough depending on what I have. So I'm just wondering if you can help us get our head around what type of like hardware and resources you need to work with LLMs. Yeah.
SPEAKER_01Yeah, you bring up an interesting fact, the notebooks. So I do still use notebooks, by the way, a lot. Even Pandas. Pandas is an old hat by now because it's been around for such a long time, but it is still relevant. I was just using it the other day to format my data set for the LLM, the classification fine-tuning data set. But... Yeah, like you said, there are limitations now because Jupyter Notebooks or back then IPython Notebooks, but let's say Jupyter Notebooks, you can't really even train, I would say, most LLMs. Simple ones, yes, the small ones, but the problem is,
SPEAKER_01for example, you can't really use the more advanced multi-GPU processing paradigms. You can do some... data parallelism there, but because the way the forking and multiprocessing works, you can't really use like tensor parallelism, fully sharded data parallelism in notebooks on multiple GPUs. So that's already ruling out notebooks as a platform. It's more like it's useful for prototyping, but then for the real training, that would be just a paradigm shift where you go more into the engineering away from like the toy examples almost.
SPEAKER_01Yeah. But yeah, so there are multiple platforms because, yeah, one limitation is usually how much RAM you have, GPU RAM, or how many GPUs you have. That's usually nowadays the bottleneck. And that is also why the meme or the saying on Twitter and LinkedIn is like the GPU poor thing where people don't, where everything is bound by how many GPUs you have and this type of stuff. And multiple platforms, of course, there's still a Google collab you could use for prototyping using a single GPU and
SPEAKER_01trying things out. The problem though is with that, yeah, I think it's limited to one GPU. So you can't really do multi-GPU training. And I think it depends on your tier, but... After some time, the computation quits, like the runtime is exceeded or something. So you can like infinitely run something. And then, yeah, usually LM training takes sometimes days or weeks. That would not be a good platform because then also, yeah, that would just not work. AWS and GCP and Azure, these are usually the platforms where you can rent your
instances to train things.
SPEAKER_01But I did that also at university and stuff. The problem there is you have to set up your environment every time. And it's expensive because you rent your GPU or your machine with a GPU, you set it up, you spend a lot of time configuring things and you pay for that basically, right? The GPU costs while you just do trivial things. And One option, a little plug here, but at Lightning AI, we have also a platform where you can use GPUs. But the nice thing is you can basically run Visual Studio Code, Jupyter Notebooks, and you can flexibly switch between CPUs and GPUs.
So I can do my coding like I would do on my normal computer in Visual Studio Code
SPEAKER_01or Jupyter Notebooks on a CPU, which is free if you use the first CPU. And then unless I'm running code, I don't waste my money there. And then once I'm ready to train my LLM, I can just switch to multi-GPU machine or multi-GPU option. Basically, you just add on your GPUs and you don't have to install anything again. It's persistent because in Google call app, it's usually even when I prototype, I upload my files. I do some debugging and then, oh, now I want to use my GPU. And then it deletes your whole environment. You have to install the requirements again, and it's a hassle.
So we developed also the Lightning AI studio at work for ourselves as researchers
SPEAKER_01so that we can actually effectively solve problems. But yeah, also a lot of people find them useful. So that is another option. If people are interested in fine tuning or training LLMs where that might be something they find useful. But yeah, there are several options. I also still use my personal computer sometimes for small problems. So there is, I would say, no right or wrong solution, but there are tools where you can do things more effectively if you need GPUs, which is usually the bottleneck for LM training.
SPEAKER_00Amazing. And I've actually put a link. I'll put it in the show notes and I put it in the chat too. I shared the GitHub for your LLMs from scratch book. I've actually shared the link to the setup instructions where you go through a lot of these ways to do it as well. Speaking of running things locally also, hopefully we'll get to this, but on the weekend you trained or maybe just before the weekend trained fine-tuned a spam classifier on a gpt model on your laptop so we hopefully we'll get to that example i i can show that yeah so this is something
where i trained a small gpt model like for educational purposes and but it does perform pretty well it can classify spam with i was like 96 accuracy on a balanced data set and so yeah
SPEAKER_01you can train that model on i did it on my macbook air and So it worked in, I think it was like six minutes. But yeah, on a GPU, if you had a GPU for that, it would only take 30 seconds. So it is still like a magnitude of 20-fold, where 20-fold is faster to do it on a GPU compared to a laptop. But it is possible. So it's actually maybe a great intro to LLMs to fine tune LMs to try it out first on a simple LLM. And what's nice about that, what I like about GPT-2 is you can then load the pre-trained
weights from OpenAI
SPEAKER_01into it because they shared them. Of course, there are also weights for other LLMs available. But what's nice is it's small. It fits onto a single GPU and you can do all kinds of things with it. They also have a 1.5 billion version where you can scale it up where it would only run on a GPU then. The other thing is also all the other LLMs are derived from that architecture. So I will probably make some materials sometime to convert GPT to LAMA and Mistral and PHY. Actually, we have a project, open source project called LitGPT,
SPEAKER_01where it's exactly that, where we have one library. that implements all kinds of neural networks, Mistral, Phi, Lama 3, Lama 2, I think we have 30 LLMs implemented there. And they all share the base architecture. There are only slight variations. And this was also actually for me or for my colleagues, a great learning experience to basically understand more about architecture choices because you can change a few things and then you Actually, it's great to play with LLMs that have been pre-trained.
SPEAKER_01And the reason is it's like a self-check. Because when you implement the architecture yourself, you have to get it exactly right. Otherwise, you won't be able to load the weights because the tensor dimensions won't match, basically. So it will complain, oh, your tensor is too big or too small or something is not compatible. So it's like a great way to learn to code LLMs by... using an architecture that exists and then coding it yourself and then loading existing weights into it. But even then,
SPEAKER_01if you get the architecture and you load the, let's say you think you get it, you load the weights into it and you then run the LLM, you can quickly see whether you did everything correctly or not. Because if you make a tiny mistake, then immediately you will see the LLM generates like gibberish. So you have to get it exactly right. I remember when we were implementing, that was the Google Gemma architecture. It was quite funny because they had a lot of different implementations out there. I think there was like a JAX, Keras, and a PyTorch one and stuff like that. So we tried to make it so load the weights into our litgbt framework.
SPEAKER_01And then it loaded fine, but somehow the model generated gibberish. And then we did find out why. I think it was like an offset of the RMS norm had a plus one offset. But like a small thing, like adding plus one makes a whole difference. And this is, it sounds trivial, but this is actually a great learning exercise because it's helping you to understand what they did because you really have to get it right. And these are things that are usually not mentioned in the papers. So by coding those things, you might even learn more than you would just learn from reading the papers. Basically, it's really actually a great and fun exercise.
SPEAKER_01Fun if you at some point find the solution. Otherwise it's frustrating, but it is good. I think fun, fun thing to do. Yeah.
SPEAKER_00Yeah. Awesome. So just as a teaser, at the end of this live stream, we'll show some of the code to fine tune GPT-2 that Sebastian did on his laptop. Also, thank you for mentioning Lit as well. And I'll include these in the show notes, but definitely check out PyTorch Lightning if you haven't. Fabric and Lit, which are the three open source projects you work on. And then there's Lightning AI Studio, which Sebastian runs a lot of experiments on and that type of stuff. So I'll include links to all those things. I now want to jump in and think about
SPEAKER_00you've got your book coming out called Building LLMs from Scratch. So how do you even get started and actually implement an LLM from Scratch? And why would you want to do this?
SPEAKER_01For that, it's like I'm lying on the... So the book is interesting where this is like the first book. So the MLQ&AI book was maybe the first... So let's go back. The machine learning with Python and Psychic Learn book, PyTorch and Psychic Learn book I have, it's explaining different steps in the machine learning... world, basically data processing, the different classifiers, how PyTorch works, training convolutional networks, and so forth. So the chapters are semi-connected. In the MLQ and AI book, the chapters are very independent.
SPEAKER_01And now in this LLM from scratch book, I basically, it's like the journey, really, the LLM lifecycle journey, where it's really one chapter really builds on each other. And how you get started with that is really, I would say, starting at the bottom, basically, really. In terms of bottom, if you look at the LLM diagram, the bottom is where the data comes in. It's funny because in LLM research or in neural network research in general, all the diagrams are different. flipped in a way because it's something the editor complained because usually when you read diagrams in English,
SPEAKER_01you read them from top to bottom or left to right. And if you look at neural network diagrams, they're usually from bottom to the top where the input layer is at the bottom and the output is at the top. But anyways, a little segue here. But yeah, you start at the bottom where you really think about how do you feed in your data? Because I think It's really helping a lot to code the data input before coding the LLM because it really helps you understand what does the data look like that
the LLM gets and what
SPEAKER_01does the LLM output. So basically you start with a data for pre-training where you basically, it's the next token prediction task. You have a sentence and the target is basically the sentence shifted by one position. So thinking about this, how to implement that, how to chunk up the text and stuff like that. And so it's really the book takes people on that journey from the very beginning, like getting our data pipeline implemented to understanding the LLM under the hood, basically, like what is the attention mechanism?
SPEAKER_01How does self-attention work? What is this causal mask? How do we mask the attention mechanism? And then what is multi-head attention and stuff like that? It's like... It's also why you would do that. It's think of it maybe as like a lot of people like working on cars, right? So they are in their garage, maybe. Personally, I'm not very talented, so I wouldn't be able to do that. But a lot of people like to work on older cars or even maybe building model planes and these types of things. And it's a lot of fun where you learn a lot about this.
SPEAKER_01But if you, let's say, build a car yourself, you wouldn't build necessarily if you start a Ferrari. You would build maybe something simpler, maybe like a... An older car that is simpler to understand, like maybe an old Mustang or something like that. I'd probably start with a tricycle or something like that, which isn't technically a car. Yeah. But maybe if you are building model planes. So it helps you kind of understand how airplanes work in a way if you build a model plane. That doesn't mean this is the airplane that can fly passengers.
SPEAKER_01It's really a model plane, I think. And in this case, you can think of this book as building a small, large language model that actually works. It's a GPT model, so it does work. But it's not like the latest, greatest chat GPT model because that would take millions of dollars to build. Maybe hundreds of millions, actually.
SPEAKER_00Yeah.
SPEAKER_01Hundreds maybe, yeah, hundreds of millions. So in that case, it's like a fun project where you learn, let's say, how something works. Because if you build a car, let's say the old Ford Model T, the first car or one of the first cars, if you build that, you get a good understanding of how cars work and how the motor works, right? The motor is like similar. It's, of course, much more advanced now, but it's like similar principles, right? And if you build that one step at a time, I think this really helps the way you can think of the
attention mechanism as
SPEAKER_01building the motor. And then in chapter four, when you build the architecture, you build the chassis around the motor, like connected to the wheels and stuff like that. And then chapter five, the pre-training is like turning on the motor and taking it for a ride. And then the fine tuning is basically tuning up your car, making it go faster and stuff like that. But yeah, once you understand that, you could technically take out that motor, put in a more powerful motor. You don't have to necessarily build this powerful motor yourself. You can maybe purchase, let's say, a pre-built motor and put it into a car.
SPEAKER_01But it would follow the same principles. You can still connect it to the wheels. It will make your car go faster. So in that case, I think it's a great exercise to understand the whole thing. And you can always later swap it out by more efficient components, let's say. But yeah, in that book, I'm trying to keep it as simple as possible, which is why you mentioned I can train this model on a laptop computer. So it doesn't require you to spend hundreds, thousands or hundreds of thousands of dollars to train the model. But if you want to, you can make it larger and you can train it on GPUs. So I have the same model.
SPEAKER_01as bonus material, a bit bigger and pre-training it on about 70,000 public domain books from Project Gutenberg, where this would take about two weeks on A100 GPU. So you can train this longer, but yeah, I try to keep it simple so you can really see results in 10 minutes if you want to. Yeah.
SPEAKER_00Fantastic. So are those chapters live in your book yet by any chance? Yeah. So chapters one to five are on the Manning website available. So definitely check out the book and everything happening there. I did then want to talk a bit about, we mentioned briefly what type of resources and hardware you need. I wonder if there's anything you just want to add about how people can just get started on the cloud in terms of training and fine tuning and that type of stuff.
SPEAKER_01So personally, I use Lightning Studios. So in a sense, I can really show a demo where it's really simple. You can just drag and drop your files into the Lightning Studio. It's a Visual Studio code or JupyterLab interface, whatever your heart desires. And yeah, all the code I have in that GitHub repository runs on both CPU and GPU. So there's nothing you have to do on your end. It's really just making sure the machine you're using has a GPU and then it would run on the GPU. It's as simple as that. Thanks to PyTorch, basically.
SPEAKER_00And we actually have a question in the chat. And so I just want to remind people that if you have any questions, please drop them in the chat and we'll try to get to them. But Atalaba Miguel, great question. Would you advise adopting Lightning AI to effectively prototype and build LLM products without the hassle of environments and this type of stuff? So maybe you can say a bit about that.
SPEAKER_01Without the hassle of end-to-end environments?
SPEAKER_00Oh, no, sorry. They wrote envs, so I translated to environment.
SPEAKER_01Oh, I see, I see. Yeah, so with the nice thing about... So the question was about lightning or... I'm sorry. Yeah, yeah. So Lightning is probably then referring to the Lightning AI Studios. Basically, I was just making sure because at Lightning, we do many things from open source to the Lightning AI Studios. So with Lightning AI Studios, it's essentially you can think of it as an environment. So basically, when you start a new studio, you can create a new studio and this would be a fresh environment that has nothing installed yet, basically. But you can then,
SPEAKER_01if you have a requirements, the text file, you can pip install the requirements and then they will stay forever in that studio. You can put the studio, let's say you can say sleep. If you are done working with it and next day you wake up, I want to continue working on it. You restart the studio and everything is still there. So you never have to reinstall something like you have on Google Colab. It's persistent, basically. And then if you want a GPU, you can just switch it. You don't need to move your files anywhere. It's all still there. It's all persistent. So in that case, yeah, it makes the development very easy because once you have the
code developed and you
SPEAKER_01have it working, you can always switch the GPU, CPU machines and stuff like that. And yeah, you never have to worry about the environment. And I think that is where it makes it easy, basically, where you really don't have to worry much about installing things. And it's helping with the whole lifecycle of the LRM because you can have everything in one notebook, sorry, in one studio. And we have actually studio templates where we have pre-trained models. If some people are interested, we have my colleague, Adrian. He has continued pre-training and pre-training studios where he's showing how to
SPEAKER_01pre-train a small LAMA model.
SPEAKER_02Yeah.
SPEAKER_01And we also, in our litGPT library, we have the best practices implemented for parallelism, where it's also, yeah, doing parallel training, quantization, mixed precision training, all the good stuff, basically. Yeah. And it plays nicely then with cloud infrastructure like Lightning Studios, because you can run it on a single GPU or CPU, but then to make it faster, you can just switch it and you don't have to change anything in your code. It will automatically use all the GPUs available, for example. Yeah. Awesome.
SPEAKER_00Thank you for spelling that out in such wonderful detail. I'm also, I mentioned that I work on Metaflow before and as head of DevRel at Outer Bounds, my day job, I'd have to fire myself if I didn't mention, we're also thinking a lot about training challenges and fine tuning. We've actually just announced a collaboration with AWS Trainium for using Metaflow to do this type of training and fine tuning, which is super fun. And if you don't know, AWS Trainium is a chip that AWS built for deep learning to train 100 people. billion plus parameter models and that type of stuff.
SPEAKER_00So it's a very exciting and rich space at the moment. We've got a couple of questions in the chat that I want to get to when we get to more thinking about cutting edge techniques and that type of stuff. But before that, Sebastian, I'm very interested in... There are so many conversations around like when to use RAG or fine tuning or do pre-training from scratch or quote unquote prompt engineering. So... I'm just wondering what type of issues would make you think about doing prompt engineering, RAG,
SPEAKER_00fine tuning, pre-training from scratch, and how do you choose between all these techniques?
SPEAKER_01Yeah, I would say it depends on, of course, the application. But if you use something like ChatGPT, then your only option is essentially prompt engineering, right? Or if you're interested in an LLM that can respond specifically, I don't know, you have the PyTorch Lightning documentation and it's too long to read or to complicate it and you want the LLM to digest it for you and you have a question like, In PyTorch Lightning, how do I X, Y, Z? You could see if it can answer. You don't need to do anything else. And because actually I tried it a couple of times,
it is pretty good at that. But now let's say you have a more specific software library or in-house product
SPEAKER_01that has not been on the internet. So the LLM has not been pre-trained on that. So it doesn't know anything about that, let's say software, like maybe some proprietary data. So what you could do then is you could see if you can fit the document that you're asking questions about into the context. So you can maybe upload it. There you have to be a bit careful. I would probably not do that on ChatGPT if it's a proprietary document because, yeah, it... Maybe you can't leave your computer. You don't know what's happening with the data if it leaves the computer. So in that case, you might also consider a local LLM where you run your local
LLM. Try to fit the data you are questioning or you have questions about into the context.
SPEAKER_01If it fits and returns a good response, yeah, maybe that's sufficient. You don't have to do anything else. But then maybe it can't answer the question. So in that case, it might make sense to apply a RAC system where you chunk up the text and then you build, can do that usually in memory. Have a database, indexed database that you can, or the LM can refer to, find similar chunks and then synthesize the response based on that. Where it's getting you around the context input limitation, the size. But then, yeah, if you do that a lot, if you do that every day and you have really high demand on a
really good LLM that
SPEAKER_01answers questions for that type of thing, you might think about at some point about pre-training the LLM. Maybe that helps additionally or even fine-tuning it for certain instructions that you often tend to ask. In addition to the rack, maybe that has additional benefits. So I think it comes down to also a low effort, prompting, to medium effort building that rack system because that is a few extra steps. It's not much, but it's a few extra steps. And then most effort would be pre-training, basically, like this kind of... It's almost like a cost scale or effort you have to put into it scale.
SPEAKER_01But then also... Yeah, it needs to be worth it. I think the pre-training, it's something I would not do routinely. It's a lot of work and not as cheap as prompting.
SPEAKER_00Fantastic. I like that rank ordering of doing things when you need to at a certain point, but trying the easier and less computationally intensive things first. So we've talked a lot about kind of what are now, I think, canonical foundational concepts in LLMs and building LLMs and using LLMs. We haven't yet dove into like really cutting edge stuff. And I know that you're very interested in techniques such as Laura, QLaura, now maybe even Dora.
I know there are a whole bunch of cutting edge things that you work on constantly. I'm wondering what a current cutting edge research techniques that you're interested in and why?
SPEAKER_01Mm-hmm. I think you mentioned Laura and QLaura. And these are, I think, still my favorites, which is funny because they've been around. Laura at least has been around maybe for two or three years. But I do think it's still... I used it just the other day on the weekend. It is really my go-to for fine-tuning because it is getting me good results much more efficiently. I think it's maybe not cutting edge in terms of it's not... Super new, but it is one of my favorite research techniques, I must say. So Allura is basically where you approximate the weight update
by two smaller,
SPEAKER_01low rank matrices, which is like a neat, nice idea. But other than that, I think just thinking about it, I looked a lot at architectures recently, LLM architectures. What I like, I find interesting is, no, and the word slipped my mind, the mixture of experts. where you have like a routing mechanism where you don't use all the layers, all the fully connected layers. You selectively choose some of them. That is an interesting idea.
SPEAKER_00I think it was a mixed rule model where I first saw.
SPEAKER_01Yeah, I think there's just a new one that came out like a few weeks ago. Yeah, I think we have one of them. We have them and we have it in LitGPT too. But yeah, I have to double check. But yeah, so this is an interesting idea where I feel like we are taking, it's still the same LLM architecture, but we are like taking it to the next step. Another one I just saw the other day, I was skimming a paper where they did multi-token prediction, which was quite interesting, where, yeah, we talked about pre-training predicting the next token, so always the next token. But then in that paper,
what they did is they had one large language model head predicting the next token,
SPEAKER_01another one predicting the second next token, and another one, the third next token, which is interesting because during inference, for example, but even during training, But mostly during inference, you can predict four tokens at a time if you have four of these language heads. So it's like almost like a hack because it's not like new math or new, but it's like cool because it's like with the tools we have, how can we put them together slightly differently? to do cool things. And in this case, if you think about four large language model heads that you predict plus one,
SPEAKER_01the other one predicts plus two, the other one plus three, and the other one plus four, and you run them in parallel, you get four times speedups on inference because you can generate four tokens at a time, basically, which is interesting. It's kind of like... Maybe not as quite as good as the other one, but it's like one of these neat little ideas where you can, you don't have to reinvent the wheel. It's like a neat add-on almost. But also, yeah, coming back to the RLHF you mentioned, that is also something I'm really interested in at the moment. For example, direct preference optimization is also has been around for a year or so,
SPEAKER_01but it's also a neat idea where you make the problem a bit simpler. You remove the reward model. In that RLHF process, because when you do that, you have to usually train two LLMs. You have the one LLM that you instruction fine tune with supervised learning. Then you have to train a reward model that learns based on your thumbs up and thumbs down responses. And then you have to train, update the first model with that reward model to get that fine tuned model. And that is a lot of LLMs where you have to keep two LLMs basically in memory.
SPEAKER_01And if you can make that a bit simpler, that would be nice. So in direct preference optimization, you skip that reward model. You don't have one. You just use basically the reward of the model for both. You have the logits, the outputs. Yeah. You use actually softmax or, yeah, a score basically for the one model that you want to optimize. And then you have the reference model and you take the ratio basically, and you say, is this response better than the other? And based on that, you can skip the reward model. You can use the model implicitly to compute its reward that you
are using to update the model.
SPEAKER_01And that is also a neat idea. It's been around for a year and people, I think most of the models you find on leaderboards nowadays are using direct preference optimization, but I think it might be also because it's simpler. I just saw the other two weeks ago, a paper where they did a more rigorous analysis and RLHF with PPO is still better, like in terms of absolute performance, it's of course more work, but I find DPO is a nice, I would say alternative. And I'm really looking forward to the LAMA3 paper because I saw in the blog post that they mentioned that they used both.
SPEAKER_01So they used RLHF PPO plus DPO. And I'm really curious and I hope they have included some ablation studies to see like comparing basically combination of both methods to only one or the other. That would be something I'm personally very excited about. So I hope we will see a LAMA3 paper. Yeah, and these are mainly the techniques. These are very, I would say, fundamental techniques, but they also almost stood the test of time in a one-year frame. So I think they will be around for a bit longer.
SPEAKER_01And I think they are, like you mentioned, Dora. It's an add-on of LoRa. And there are like all these cool techniques on top of it. But I think, yeah, these fundamental techniques, the vanilla techniques, they are still very fascinating. Yeah.
SPEAKER_00Very cool. And with the DPO stuff, is that what, once again, users of ChatGPT, the app or the web interface, may recall that sometimes instead of asking for a thumb up or thumb down, ChatGPT will give you two options and ask you to choose one. So is that, are they using the results of that for DPO, do you think, or... I think so.
SPEAKER_01So I don't think ChatGPT uses DPO. I think they might, we don't know, they might be still using PPO because they invented it. But yeah, that's the same concept. I think if you choose one or the other, it's basically the thumbs up, thumbs down kind of thing where it's like one is preferred over the other. Yeah. There's another method. I think Tversky-Kahneman optimization. Yeah. And there, I think they allow you to, you don't need a reference. Sorry,
there you have the thumbs up and thumbs down where you don't need the reference.
SPEAKER_01The RLHF PPO and DPO, they both have a preference. It's one better than the other, basically. It's like a ranking. So also for listeners before, when we said thumbs up, thumbs down, we both meant, I think, the comparison of two responses and the absolute thumbs up, thumbs down. If you don't have a reference, that would be the KTO basically.
SPEAKER_00Yeah, that makes sense. So we actually have a question in the chat that it's interesting for several reasons to me. The question's from Eric. What's Sebastian's opinion on sparse attention mechanisms to increase context size of LLMs? How well do they capture interactions between words far apart compared to a standard? Attention mechanism. Now, I'd love to hear your answer to that. But we've talked about a lot of things, but we've barely got into even what context sizes and attention mechanisms and that type of thing. So how important are these things to know bits and pieces about?
I would say the context length and stuff that is interesting and important because
SPEAKER_01it helps you understand like limitations. So it's like with the original GPT model, they use an embedding layer that had absolute values. And so you can't really trivially extend the context length. So you are stuck with that, but it's like a design choice you have to make at the beginning. When you make your GPT, what is my context length? I think they use 1024 there. So you can't really do more than that. But with other LLMs, you can extend it. I'm not, I must say, not an expert in these techniques. There are techniques like long LoRa in these types of things or
many other
SPEAKER_01techniques on where you can extend it. Regarding sparse attention, it's also for lowering your memory requirements because otherwise you have a quadratic memory cost for matching each token with each other token. That's why it's called self-attention because each token in the sequence itself is referencing each other token. And it's like a quadratic thing. You can think of it as a n-squared matrix attention scores. And making the sparse can help with efficiency. I must say I personally haven't really worked on that much.
SPEAKER_01I just use the regular one because... I don't know. I never, hmm. I usually tweak other things when I have to make things more efficient. But this was something, the reason is maybe because all the major LLMs don't do it. So in that case, I was like, there's a reason why most LLMs don't use it. These are neat techniques, but I do think they're approximations. So I must say my knowledge is more limited there. I remember there was one LLM. I don't think it was Mistral. It was one of the, it could have been Mistral, Falcon...
SPEAKER_01There was one of the LLMs that did something like that where they... So they have like a sliding window attention mechanism, basically, which is like that, where you only have the attention for, let's say, N tokens. So let's say if your token context is N, it would be like, I don't know, N minus... Let's say five tokens ahead for one token. And then you slide the window over for the next token. Again, five tokens ahead. So you can do it like that, which is... an approximation, but based on the paper, I remember they got really good results,
basically. So I don't think they did an ablation study, but they got really good results.
SPEAKER_01It was, at the time, the best LLM, open source LLM. I forgot just which LLM it was. But yeah, so this is something that works. It's a good alternative. But that is also as much as my knowledge goes, because I have not implemented these things myself, the sparse attention mechanisms.
SPEAKER_00Knowing you, in a week you will have... Speaking of that, I think it's time to jump into a demo. Show us how, like, the code of what you're doing to fine-tune GPT-2 locally. All right, okay. So this is the segment where we do some live coding. Although some of it relies on visuals, much of it can be grok through the audio, so we left it in. If you want to watch it, though, do check out the video in the show notes. It takes around 20 minutes here, so also feel free to skip forward.
SPEAKER_01Yeah, I actually have prepared the code. I have the code on my computer, but let me just share my screen here. And I can also maybe share it afterwards as a link and other people can run it. Absolutely. I have the code on my computer, but yeah, I'm just dragging the code in here. And this is basically a classification example. So where I was training, pre-training an LLM, a GPT model in previous projects, and then I was loading the pre-trained weights from OpenAI. And then I was just adopting it for text classification,
SPEAKER_01like an introduction to classification, because it's something that I find interesting because most people don't know that you can adopt an LLM. It could be a BERT model, a GPT model, a Lama model to do simple classification. Like you are familiar with usually from classic machine learning, like scikit-learn when you train classifiers. So in this notebook here, yeah, it's the whole thing. Of course, it's very verbose because we are also loading and retraining the data.
SPEAKER_00And could you also just increase your text size?
SPEAKER_01Oh, yeah. Sorry. Oh, no, that's cool. So this is actually a studio interface. That's usually what I use for my coding. So it's because I can do all that stuff on a CPU. And then when I need my GPU, I can switch to the GPU here. And so basically in this notebook, I'm going from preparing the data set to loading the weights from OpenAI and then modifying the architecture and then basically fine tuning it.
SPEAKER_01So it might take a few minutes because a lot of people are using the GPU. So let me use this one that I ran actually before. But let me clear all the outputs and restart. And then we can go through it step by step. So there are some packages I use. Actually, there are not too many. There are some dependencies. So there is a tick token. It's for tokenizing the text, which is quite fast. So you can use the Hugging Face tokenizers, for example, but I did some benchmarks. The tick token one is really nice. It's based on Rust. So it's pretty fast. TensorFlow, because most people probably don't know,
SPEAKER_01but the OpenAI weights for GPT were in TensorFlow back then. And see Pandas for the data loading, because it's still relevant. We talked about Pandas earlier in the good old days, but it's still relevant, I think, in the new days, although a lot of people like Polaris too.
SPEAKER_00Yeah, yeah. I also just wanted to mention, if you ask ChatGPT to generate a list of something for you and then put it in a spreadsheet, or there are lots of things, if you are showing me the code, most of the time we'll be using Pandas as well, right? Yeah.
SPEAKER_01Yeah, yeah. And so here I have a data set. Basically, I use a very simple data set. It's text messages, not email messages, although email messages also exist because I wanted to keep it fast so that you can run it. We can run it here in 30 seconds on a GPU or at home, like in 10 minutes on a computer or like laptop. But yeah, it's a simple data set where we have ham, which means not spam and spam messages. So we want to train a model that can classify those. And basically, we want to repurpose an LLM to classify those. In this case, it's not a balanced data set.
SPEAKER_01It's also something to check. But to make the problem simpler, I balance the data set myself so that we have the same number of spam and hams. So the accuracy we compute is more meaningful. Otherwise, we would have to do F1 scores, precision and recall. And this would be another topic almost in valid state. Then let's convert these hams to or spams to integer labels. do a random split. So I'm just browsing over that because that's not the interesting part. It's just the necessary data preparation part where we are creating a data set in
SPEAKER_01PyTorch and preparing the data loaders here. So these are just regular PyTorch data loaders. Really, I'm skipping over it because that's not the LLM part. And I guess the audience is more interested in the LLM part. But the takeaway here is that we have a training set. a validation set and a test set, and 130 training batches, 19 validation batches, and 30 test batches. It's almost like a classic problem you would have in scikit-learn when you train a classifier, like a logistic regression classifier,
SPEAKER_01or a naive base, or a random forest, or a support vector machine. Now comes the interesting part, the model, basically. So here we are loading the smallest model, but you can change that. You can change it to the larger models, the 500 million or 1,000, 1,500 million size model. And here are some settings, the vocabulary size, context length that we just talked about. If you want to use dropout and if you want to use the bias units in the QKV matrices.
SPEAKER_01So these are the query key and value matrices in the attention mechanism. An interesting side note is that back then the GPT had the bias units. So we are matching it because we want to load the pre-trained weights. But if you look at newer architectures, Bias units are not really necessary. I think like all the new models, they don't really use bias units anymore. But yeah, if you're interested, you can actually also take the larger model here and put it in here and train the larger model. So you can see they are actually all the same, except they repeat the number of layers different number of times.
And layers here refers to the transformer blocks and then the attention heads.
SPEAKER_01So these are just slight variations, but ultimately the architecture is the same for all these models. So this code, the notebook should work with all the models sizes here listed here. And I'm not showing you the model itself. I'm just importing it here because it's quite verbose. And this would be another, I would say, podcast or tutorial, but it's all contained in
SPEAKER_02Yeah,
SPEAKER_01in this big file where I have the architecture implementation, this would be somewhere down here in the GPT model, which in turn has transformer blocks and the feedforward module, which in turn looks like this. So it's rabbits, how do you say the rabbits all the way down or the thing, but it's like very recursive almost. You have a lot of things going on here. Okay, here we are loading the GPT model weights. And for that, I also have abstracted this away in this one here.
SPEAKER_01So the original weights were in TensorFlow. So I have a written utility function that converts it to PyTorch, basically. It's not very interesting. It's really like loading the parameters from a TensorFlow checkpoint and returning them as tensors. And then that's what's feeding into the model. So basically, I have a function that downloads the weights. And then initializes the model that is contained here and loading the weights into that model. And did I execute this? Yeah, I did.
SPEAKER_01You can see the check mark here. And then now let's, before we actually do anything with this model, let's actually make sure it works before we fine tune anything. I think this is what we talked about before. It's like the sanity check. Can it generate coherent text? So when I execute this, I hope that it can generate coherent text. And you can see, so the input text is every effort moves you. And then the output is forward. So it's the first word is it's completing the sentence because it's a model has been pre-trained.
So it's like what it does, it's trying to complete the input, the next token prediction.
SPEAKER_01And then you can see it generates one more sentence. I kept it here. You can, you can increase this and it will generate more text, but yeah. But see, it's also repetitive because the thing is with this model, it's a very small model. It's not the best model yet, right? you would instruction fine-tune it later. And you can see also the larger models. We can change it maybe later if we have time, but they would perform a bit better. But the point is really it can generate coherent text. If you make a mistake in your implementation, you would notice it's just generating gibberish.
It wouldn't be able to make any sentence that has a grammatically correct meaning, basically.
SPEAKER_01Now comes the interesting part. Also, we can actually look at the architecture here. So what you can see here is the GPT model architecture. So it consists of the token embedding. So we are embedding the input text and we have a vocabulary size of 50,000 words approximately. It's embedding each token into a 768 dimensional vector. And we have also position embeddings that are the same dimensions. They are just added to each other. Optional dropout, we don't use dropout here, so it's zero.
SPEAKER_01And then the transformer blocks, which consists of multi-head attention, which in turn consists of the query, key, and value matrices. Projection layer, the feedforward module, which is really like a multi-layer perceptron. Mm-hmm. layer norms. So here it looks like they are applied next to each other, but that's just how it's sorted here alphabetically. But they are actually not applied after each other. They are in different positions if you look here in this file. And then really the GPT architecture is just really repeating
this transform block.
SPEAKER_01So we have the first instance, the second instance, and the third instance, and then yeah, it's dotted out here. I think you can click here and then it would show you all the ones up to 12. So Python uses zero indexing. So it would be the 12th transformer block. And that's essentially it. And then you have one output layer that takes each token and projects it back into the vocabulary dimension. So you can then convert using a tokenizer in an inverted way. You can convert the token IDs back into the text.
SPEAKER_01And so this is the architecture. But if we are interested in just classifying text, let's say spam and hem, we don't need the whole vocabulary as output. So we just need an answer. Is it spam or not spam? So what we could do is we could actually replace on this layer here. by a layer that only has two output units. Actually, you can go even further and have one output unit, like in binary classification. But to keep it more general, we set it equal to the number of classes, which is two in this case.
SPEAKER_01So what I'm doing here is first, I'm freezing the whole model that all the parameters are not trainable. And then I chop off this last head here, the output head. So here in this case, it would map to the vocabulary. And I replace it by my own layer. So this is a new linear layer initialized from random weights. And it has two classes for spam and not spam. So in this case, I'm replacing the last layer. And in addition, you don't have to do it, but usually it performs better.
SPEAKER_01I make the last transformer block and layer norm also trainable. So basically what I'm saying is I'm freezing the whole model except for this final part here and then the final layer norm. So this one. So now after this modification, this one is trainable. And this whole transformer block here that includes the feedforward would be trainable. But all the other ones, they are just frozen. I don't need to worry about them because they're actually quite good at extracting language or working with language.
SPEAKER_01And let's then train such a model. So first, what we could also do is actually, we can try it on some text. This is just showing you after the modification, what the output looks like. So if I have text one, two, three, four, five, six tokens, so the dot is a separate token. Now, before what the model was doing, it was outputting six output tokens, but each token had 50,000 dimensions like the vocabulary. But here's just a sanity check. After our modification, each token has two output dimensions.
SPEAKER_01This is similar to deep learning when you train, let's say, a convolutional network to predict two classes, like whether something is a cat or a dog, where the first column would stand for, let's say, cat, and the second one would stand for dog. And here it would be not spam and spam. So you have two columns as output. And for that, we would maybe need to know a bit more about how attention mechanisms work. But they are mapping... from one token to the other, the attention scores. And then the output are these vectors. So the first output vector has information about the first token.
And the second vector has information about the first and the second token because
SPEAKER_01we have this causal attention mask. And the third output vector has information about the first, second, and third token. And then the last vector here, it has information about all the previous tokens. So in this simple example, this would be these six tokens. All the information about these tokens multiplied by the attention rates would be contained in this output embedding. So what we are doing now is we are only focusing on the last one because it has information about all the words. If we focused on this one, it would only know about the first word.
SPEAKER_01And so this one is really the most, I would say, information-rich. So what we do now is we just focus... on this last output token and that is the one the last output token that we fine-tune so what we can do then is a small utility function to compute the loss but the loss is only computed about for the last output token here and this is yeah just a regular cross entropy loss we would use in pytorch for training classifiers and so forth but only focusing on the last output token and then we have
SPEAKER_01So this is doing it for one batch. And then we do the same thing for the whole data loader. So earlier we said, if I scroll up again, I hope it's not too much scrolling and people don't get nauseous here. But when we go up, we had 130 training batches. So this loader function will just compute the loss over these 130 training batches, basically.
SPEAKER_02Okay.
SPEAKER_01We already reached the bottom. So I think the most complicated stuff is already past us. So right now it's the fun part where we get to fine tune. So this is a loss loader. And then we have the same thing for, so sorry, we apply it here. And that's one thing that always gets me in PyTorch. I always forget when I just want to check something. I forget to use the no grad context or inference mode. If you don't do that, it will compute or construct the computation graph for all your
iterations because
SPEAKER_01it's thinking or training. And this one disables that so we don't blow up the memory here. Okay, let's just execute that. So this just gives us the initial loss before we start training. So it's around three. Loss is usually not super interpretable. So loss is, yeah, almost like a not super meaningful value. So we do the same thing for the accuracy. So we have an accuracy loader and we can apply that to the data set here and we can see just as a sanity check. that our model before we start training only gets roughly 46,
let's say 50% accuracy.
SPEAKER_01So it's really not very good at predicting spam and ham. It's just like to make sure this is our baseline and let's see how good we can get when we fine tune it. So here I have a very simple function. It's really just what you would use for training any convolutional network classifier or even logistic regression in PyTorch. So here it's iterating over the epochs, putting the model into training mode. Usually best practice to zero out your gradients from the previous iteration because there are tricks like gradient accumulation,
SPEAKER_01which is why this exists, but we don't need that here. Calculate the loss. And then here's our backpropagation that we talked about earlier. So we just use .backward to calculate the gradients. update the model parameters, and the rest is really just here. This is just tracking things, printing things to the command line. But really, the fun part really happens here in these three lines of code, basically. Calculating the loss, backpropagating, and then applying the updates, basically. Yeah, let's execute that.
SPEAKER_01And then, oh, one more thing. I have an evaluation function. I think I'm using it somewhere here. Maybe not the evaluate model function. I think I was using it somewhere here, which is why I defined it. Oh yeah, here we go. So I'm using this evaluate model function, which will just, it's just a wrapper function to compute the loss for all the plotters here in one line. So it's really just computing the loss. And then let's actually execute that. So this is essentially just a time function, initializing the optimizer. five epochs and then feeding the model and the data loaders,
SPEAKER_01optimizer, and so forth into this train function that we just defined. And let's see what happens. Oh, we have not defined that one. Yep. Let's do that. Yeah, and then it should only take 20 seconds because we are running this on a GPU here. So you can see it starts out after one epoch with 55% or 75%. We'll worry about the hiccup here. Yeah, a little hiccup here, but it's improving again. 75% in 27 minutes. Also, I should say this is only approximated.
So here, what we did is we only computed it over 10 batches just to make the evaluation faster.
SPEAKER_01So we can also then evaluate it on the full case.
SPEAKER_02So here, let's see. Yep.
SPEAKER_01So after one epoch, it exaggerated here already. So it doesn't really do much. Yeah. It's not great, but it could be improved. But yeah, it is definitely better than the baseline here. It's overfitting a little bit. Yeah. And let's compute the training, validation, and test accuracies here. It's 80%. I was actually, maybe I have the wrong file or the wrong hyperparameters. I was actually getting 95% earlier this weekend when you check out that GitHub notebook. Maybe let's run this from the top.
SPEAKER_01It could be that I ran something twice.
SPEAKER_00Yeah. And while you're running that, there is a question from Komal. Komal asks, do we feed forward the data set until the last attention head and train the params on last attention head and linear layer? It goes through all the attention heads and then they get combined usually.
SPEAKER_01So it's a good question. So if we look at the code here, we have the transformer block, which is here and uses the attention mechanism and let's scroll up. So this is the attention mechanism here. And we have the reshaping here where we are reshaping it to the number of heads times the head dimension. So we are basically collapsing the dimension over the multiple heads.
SPEAKER_01So it's all the heads concatenated to each other, basically. Right. If that makes sense. So in that sense, it's not necessarily the last head. It's all the heads. But if the question was about which multi-head attention module, then we are going through the whole M up to the last one. So where we had, let me see. Oh, see, now we got the better accuracy. I must have run something twice before where, so now we get the really good one where we have 95% test accuracy. Yeah. But the question going back here.
SPEAKER_01Yeah, here we are. So it's really going through the whole model, through all the 12 attention blocks or multi-head attention blocks in the transform blocks up to the last one. And then it's really getting the last token from here, from that layer. But everything else, it uses the full thing. So it's not just the last one, it's using the full thing. And then from the output layer, we replace that. We replace that with the one here that has two output classes. But it still outputs. So the model, the attention mechanism still outputs.
SPEAKER_01If you have six input tokens, it always also outputs six output tokens. So we are only focusing on the last output token.
SPEAKER_00Thank you for that wonderful, enlightening demo. I did have another question, but you can... My question was, when talking about the full LLM lifecycle, we did talk about deploying and monitoring. We didn't talk about evaluation at all. And of course, this is a classification problem where it's very easy to define accuracy and these types of things. And I feel a bit cheating tagging a question like this on near the end of an episode because I think evaluation needs a whole series of podcasts itself. But I'm just wondering if there's any advice you could give
people thinking through
SPEAKER_00how to evaluate LLMs and this type of stuff.
SPEAKER_01Yeah, I think the challenge nowadays, the biggest challenge with evaluating LLMs is making sure we use an independent test data set or like questions for evaluation. So to evaluate an instruction fine-tuned LLM, there are multiple benchmarks. Usually a lot of them are Q&A based. So basically you have an instruction like answer. So multiple choice, pick one of these answers or answer in a single word. What is the historic date when so-and-so happened? And then if it outputs the correct date, then it's correct.
And the problem though with that is most of the benchmark data has probably been
SPEAKER_01used for pre-training the LLM. So if you evaluate an LLM really for your own application, I think I personally would make sure to include some data that has not been used for pre-training. Also interesting is, for example, if you develop your own system, we just want to make sure that LLM learns well when you do continued pre-training or instruction fine-tuning. Personally, it's just like a trick, but what you can do is you can change the facts in your data. So, for example, you have your pre-training data and...
SPEAKER_01If you feel like, okay, this is something general that could have been used in the pre-training, you can just change the fact in that. So you can change, if you have a Wikipedia article with some historic dates, you can just change the historic dates in that article, for example. names of people involved in that article and see what the model outputs. Is it learning from the document you fed it or is it like recalling it maybe from memory that has, which gives you an indicator that it has already seen this article before if it recalls the original memory,
SPEAKER_01basically the original date, like a little trick. Another big challenge is this is only checking factual correctness. It's not always sufficient, I would say. A lot of people, when they're building chatbots, they care about conversational tasks. And this is more tricky. I think most people, what they do really is looking at an LMSYS chatbot arena where they are comparing two models side by side and then just saying, okay, this model is better than the other, like thumbs up types of responses. And then you can compute a ratio of how many people preferred
one LLM over the
SPEAKER_01other, more like a relative comparison. But you can also use, I've seen that a lot in papers recently because it's cheaper to use a GPT-4 to judge two answers, basically. It's like more to make sure that's the LLM output coherent text, grammatically correct text. But it is an open problem still. So yeah, not an easy one. So just briefly, like we downloaded the bigger model now. And if you want to use it here, it should work now. Let's see if it also... Yeah, let's see if it generates more,
if it repeats itself less.
SPEAKER_01Yeah, see, this is already better than the small model. It is generated. It's not repeating itself so much here. It starts repeating itself. But yeah, the only one, only difference you have to still be aware of now, the output dimensions is 2028. I think before it was smaller. So you do have to, when you add your layer here...
SPEAKER_02You have to match this number. But other than that, yeah, you can now train a larger model basically on the same data set.
SPEAKER_01Yeah. And also if people are interested, we do have this litGPT open source library where we have implemented 20 or so LLMs that are not GPT. So this is the one that is really with best practices, multi-GPU training where... My implementation here is really a GPT model, GPT focused. But here, this is like a more general one with Gemma, Lama 3 and so forth, the GPT. Yeah. So if people are interested in from scratch training, I recommend checking out this, of course. But then after that,
I think the GPT would be a good one because it's still...
SPEAKER_01relatively readable, all open source. And yeah, people can look at also how we implement LoRa and stuff like that. So yeah, it's a bit more code because it's made for efficiency and production purposes. But yeah, I hope people found that useful. And I like that stuff. It's a lot of fun for me. I realized maybe not fun for everyone, but I hope everyone enjoyed the quick demo here. I found it incredibly enlightening and a lot of people in the chat seem to as well.
SPEAKER_00A lot of people have said thank you. So I think it's time to wrap up. I do want you just to let people know where they can follow you and receive your newsletter or that type of stuff. RasBT is R-A-S-B-T is your handle on Twitter, Sebastian Rajka. People can find you on Twitter. LinkedIn under the same name and your website, if they Google you, and I'll put those links in the show notes, but is there anywhere else you'd encourage people to? I think if you go to my website, it has links to all the other things as well. I do also have a course there if people are interested in that,
my books and
SPEAKER_01Yeah, I think everything is pretty much linked on the website. That would be like the hub and my social channels. I'm pretty active on both Twitter and LinkedIn because I think this is also like where, yeah, you get to exchange ideas with other AI people and it's fun to bounce off ideas and share things and discuss things.
SPEAKER_00Yeah. And I've said this to you before, but I'm still, I don't know how you do everything you do. And...
SPEAKER_01Wisconsin.
SPEAKER_00I've joked that you're prolific, but also incredibly high quality. Because most prolific people, there's an amount, quality trade-off. And you managed to balance that incredibly well. I presume there's some structured... approach you have, which allows you to just get things.
SPEAKER_01I think the secret sauce is being excited about those things. So I have also a lot of commitments. I'm married, family, and so forth. But it's also important to make time for those to recharge. But if you're generally excited about something, yeah, you try to find as much time as you can in the day to do the things you are excited about, right? I think, yeah, being excited about things helps a lot. And also it's sad, but saying no to a lot of other good opportunities or things, because like you said, yeah,
SPEAKER_01you can't do all of the things, right? In that sense, just picking your handful of projects, but then trying to do a good job. That's what I usually try to work on things that I find exciting. But there are also lots of things I unfortunately have to say no to. So for example... I almost know nothing about CUDA, for example. I wish I would, but that is one of my weaker spots where I wish I had more time to learn it. But yeah, you can't do it all. You have to pick your battles in that sense. You specialize in certain things. So I think sometimes also when you go on social media,
SPEAKER_01you see a lot of interesting and exciting things. And my advice is almost like always, you have to realize these are from different people. If someone writes about CUDA, there's another one that writes about math. But it's usually not the same person who does both things at the same level, let's say in terms of generating content. And, but if you see it, you feel like, oh, I have to use use no and do all of these things. But yeah, realistically, that's not possible. Yeah. So for me, it's also, I only do a small subsection. So thank you for saying I'm prolific,
SPEAKER_01but I would say also, yeah, I, there are lots of other things I wish I had time for. And realistically, you can only do so many things, but I'm glad that people like these things and find them useful.
SPEAKER_00Absolutely. And I don't know if I could read as much as you write. I'm only half joking there as well. But we've already got people commenting in the chat. Daniel says, yes, very prolific, incredibly high quality. Eric says, thank you for the podcast. Sebastian is great. Leandro says, Sebastian is incredibly generous by sharing his knowledge to us mortals. So there we go. I think I want to ask you in any final words of advice. I do love you said, I paraphrase, but pursue things that you're interested in and excited about and this type of stuff. I think that's a beautiful takeaway.
SPEAKER_00I do wonder, anyone else getting started or in the weeds working with LLMs and generative AI, any advice you'd give? I would say playing with these things helps a lot because I think nowadays,
SPEAKER_01yeah, the research papers are still there, but they are usually not super detailed. So if you read a paper and you feel like you don't understand what's happening or you don't understand the details, it sometimes also, it doesn't mean you don't understand. It means more like maybe the information is just not there. So in that case, I wouldn't be feeling so like... pressure to understand everything. Everyone is specialized these days. So if you pick your niche and you are excited about that, I think going from there is not a bad thing. I would say one step at a time.
SPEAKER_01Don't worry so much about everything all at once. For us, even when we built LitGPT, it was one thing at a time. So it's like a year, almost two-year project. And also, I must say, I'm doing only a small fraction in LitGPT. Most of the hard work is done by my colleagues, Carlos and Adrian. So if you think I know LMs, they know LMs times two, basically. So in that sense, it's always a team effort too. So one person also can't do it all. So for example, Adrian is very good at multi-device,
multi-node training, for example.
SPEAKER_01And I'm more like keeping things simple. And Carlos is really emphasizing high quality code. And I learn things myself every day by just working with other people. So in that sense, I think my advice is also, yeah, if you can partner with other people also, and you don't have to do everything alone. You learn things even faster if you work with other people, in my opinion. Yeah.
SPEAKER_00It takes a village to train and to raise an LLM, let's say. That's super cool. And it's great to know that you work with wonderful people who you learn a lot from as well. And I think people always forget, like for people in the public eye, I think there can be some spotlight effect or something like that. And they forget how much teamwork goes into everything people like you and I come and present as well. It's a function of huge communities of hard work, right? Yeah.
SPEAKER_01Yeah, it is. Also, I mentioned colleagues I work very closely with, but it's also the broader community. If you think about social media, it's for me, I learned so much through just sharing things where I shared something. And there's always someone who knows a little bit more in a certain area and then gives me pointers to, hey, have you seen this paper? Or like things I would have never found myself. It's really like sharing is in that sense, double win where people, you share something people find useful. So it's, in that sense, really nice to contribute. But then also you get back a lot of feedback,
which is also useful helping you grow, basically.
SPEAKER_01So it's really a win, in my opinion. Yeah.
SPEAKER_00Okay. There's one last question then. I'm sorry. I know we'd planned an hour and a half. We've already gone for two hours. But there is, I think, something... Very interesting happening. You may have seen yesterday that OpenAI... No, let me flip it around. Stack Overflow announced a collaboration with OpenAI yesterday. One... I think the provocative version of my question would be something along the lines of... One concern with LLMs is that they may reduce community interactions. I think Stack Overflow is a wonderful example of this, actually.
SPEAKER_00If I'm using more co-piloty type things, there may be a less chance of me asking a question on Stack Overflow and then having a conversation based around it. So I think similarly with Wikipedia, if I'm asking chat GPT all these questions, there's less chance of me contributing or noticing gaps in Wikipedia. I think one thing is... The general question, is there a challenge of what LLMs will do to Web 2.0 in terms of having big communities of people discussing, making knowledge together and where your and my conversation is
weirdly guided through like retraining of LLMs and human computer interaction and these types of
SPEAKER_00things?
SPEAKER_01One of the concerns is basically, I think also if everyone uses LLMs and no one uses Stack Overflow anymore, then there would be no Stack Overflow, like no new content. And then there would be no new LLM. And it's, yeah, this is a very important and good point. I think the LLMs are only there and they're only good because of all the people who contributed to the knowledge base. Without the internet, you still would have some books and stuff. But I don't think without the internet, I don't think LLMs would be nearly as useful and powerful as they are right now. And I do think,
SPEAKER_01yeah, that is a thing where if people stop using or contributing to Stack Overflow, well, I don't think it will, of course, then also limit what we can do with LLMs in the future. So I think it's still important to keep these things, discussions going. And I think also LLMs. They are useful for often very trivial tasks. But think about this. If you ask a question to the LLM and it can't answer the question, right? You would still use Stack Overflow, right?
SPEAKER_01Yeah, I think we would still contribute tricky and high-quality conversations to Stack Overflow, in my opinion, even though you can answer the trivial question with LLMs. And maybe this is even a good thing because... This way, for example, things like Stack Overflow, we can grow it more with, instead of duplicate content, with more unique, tricky content. To focus on that, because the low-hanging fruit, the simple questions can be answered with GPT. And in that sense,
you avoid people asking duplicate questions on Stack Overflow.
SPEAKER_01So you really... Go on Stack Overflow more for the quality over quantity now that LLMs are there. So maybe it's not so much of a big problem, but then of course the traffic might change and that might impact Stack Overflow.
SPEAKER_00And their ability to coordinate in that sense. The jury's out, but important conversations to have and keep an eye on. I always love chatting with you, Sebastian. It's always fun to have conversations publicly as well. So I'd love to thank you once again for coming on the podcast and thank everyone for all the wonderful questions in the chat as well. And go to Vanishing Gradients. It's on Spotify, all of those things. And this episode will be out in the next week or so. I've also put a link to Sebastian's website in the chat and in the show notes also. So thank you once again, Sebastian.
SPEAKER_01Yeah, thanks so much for the invitation. And thanks everyone for joining and all the questions. It was, yeah, I would say pretty much improvised, but really fun podcast episode. And I hope people, yeah, enjoyed that. It was really, yeah, I enjoy talking about LLMs. I just had a lot of fun for two hours now. So I hope you had fun too. And yeah, maybe see you at PyCon next week or SciPy in July.
SPEAKER_00Fantastic. All right. Thanks once again, everyone. Ciao. And seen. Thanks for tuning in, everybody. And thanks for sticking around to the end of the episode. I would honestly love to hear from you about what resonates with you in the show, what doesn't, and anybody you'd like to hear me speak with, along with topics you'd like to hear more about. The best way to let me know currently is on Twitter. At Vanishing Data is the podcast handle, and I'm at Hugo Bowne. See you in the next episode.
Hilary Mason, CEO of Hidden Door, works with agents across software development and creative projects. Drawing on Richard Hamming’s distinction between science and engineering, she describes agents as “bringing the...
Agents can write the code, run the analysis, and produce a convincing explanation. How do you check that they’ve answered the right question, used the data appropriately, and reached a conclusion you can act on? I...
“It’s an LLM, a loop, and enough tokens.” - Nicolay Gerold, Amp Code So why does a coding agent forget your instructions, keep reading tiny pieces of a file, or struggle with an edit another model handles easily?...
“If you feel that a product is hard to eval, or you feel like, ‘I don’t even know how to eval this,’ it’s a strong smell that your product isn’t good.” - Hamel Husain, on AI product design An AI data agent tells you...
“It would be sad if local models were not an option and there were only proprietary models. It’s good to have alternatives. Competition is good for business.” - Sebastian Raschka, on open-weight AI Kimi K3’s weights...