📱

Get Our Mobile App

Take your business learning on the go!

Download on the App StoreGet it on Google Play

How LLMs Works? - Overview

Piyush Garg1:00:57

Transcription

Hey everyone, welcome back! Welcome to another exciting video, and in this video, we are going to understand how LLMs work. So, in this video, we will take a bit of a deep dive into how our popular LLM AI tools like ChatGPT, Google Gemini, Claude, and all others work under the hood, and on what basic principles they operate. And we will also do a little bit of coding to better understand what happens internally when you write a prompt on ChatGPT and how its output is generated. So, it's a whole fun video. Sit back comfortably. Grab your tea or coffee, and binge-watch this video. So, with that, let's start with the video. So, how do LLMs work? LLM stands for Large Language Model. Now, you don't need to know its full form, which I will explain, no problem. Let's take an example of an LLM model, let's say ChatGPT. Why is ChatGPT an example here? Because ChatGPT is a model that everyone has used, right? If we ask it, "Hey, how are you?" and I press enter, you can see that I get a result: "Hey, I am doing great." Correct. So, what happened here is that by me writing "Hey, how are you?", I got an output, right? And this feels magical, right? I understand, this whole thing feels magical that by me writing this, such a prompt, such an output came that feels like a human typed it, which is on point. Whatever I ask it. If I ask it, "What is 2 + 2?", you can see I get a precise, a great answer along with an emoji. So, what is happening here? Okay? Let's understand how this GPT model is working. So, if we come back to our whiteboard, and I understand, okay, I am doing a dark mode. So, here is a dark mode. GPT. Okay? The entire magic is hidden within the full form of GPT. If we take the full form of GPT, that is Generative Pre-trained, okay, Pre-trained Transformer. This is your entire magical layer through which this auto-generation, or this ChatGPT, works. At first, it seems like this is a completely magical thing, that whatever we type, we get an output. But in reality, everything is nothing but just science and math. Just science, math, and by converting it into coding, you can also build your own GPT-like model if you have enough resources. Okay? There is an asterisk here: "if you have enough resources." Okay? So, let's do one thing. Let's start breaking things down. So, what is this? First, let's talk about the word "Generative." Okay? So, what is this? Generative. Generative means to generate something. Okay? If we talk about traditional applications or traditional search engines like Google, Bing, Yahoo, when you search for something, they don't generate content, right? What do they have? They have indexed data. They have crawled the entire web, and they have some indexing of the data, and they find and bring you relevant results. That is why they are known as search engines. But they never generate content themselves. Whereas, when we talk about LLMs like Gemini or ChatGPT, they are able to generate things. That means, based on your input, on the spot, next sequences for you. Here, I am using a word: sequences. Next sentences for you, next sequences for you are generated on the spot. Okay? So, its nature is what is generative in nature. It generates. What does it generate? The next set of sequences. And on what basis does it generate this generative nature? Right? How will it generate? It generates based on the pre-trained data. Okay? So, it's not like there is some, you know, artificial intelligence within it, or some innate intelligence. No. To generate this, it needs some pre-training. Pre-training means internet data, books, conversations, historical data. So, when all that data is pre-trained within it, based on that, it is able to generate the next sequence. If I say, "How are you?" it says, "I am good." So, the "I am good" that was generated, on what basis was it generated? Based on the pre-trained data. So, this part, I am sure, everyone already knows. There is nothing new in this. The main thing comes, the last thing, that is Transformer. So, this Transformer architecture is what this entire process works on. So, GPT means Generative, which is the nature of this particular model of this Transformer. It is a Transformer, which is generative in nature, and it works on pre-trained data. Understood? Got it? Cool. Now, let's go a bit internal. Okay? This was just an overview. Now, what is this Transformer? Right? Transformers basically is a white paper from Google, that is "Attention Is All You Need." If you Google this white paper, you will find it. So, this particular paper was created by Google, and Google created a Transformer architecture. I will show you its diagram. This is the diagram. All your AI applications, AI LLM models, work on this particular Transformer. So, we can say that this is kind of the heart for every LLM out there. All your elements work on this Transformer architecture. What was there before this? How do they work? We will understand all that. See, what happens is when you write something, let's say you wrote "Hello." Okay? Let's take a simple example. "Hello, how are you?" So, first of all, what happens? First of all, when you feed this into your Transformer. Okay? Inside the Transformer. What is a Transformer? Let's say I have created a Transformer layer. Okay? Let's keep it a black box for now. We will deep dive into it slowly. That this is my Transformer layer. For a Transformer, let's rewrite this. For a Transformer, what can a Transformer do? It transforms anything, any input, into a particular output. For example, let's say what can it do? If I give it "Hi" as an input, it can transform this "Hi" into "Hello." Okay? It can, it's a Transformer. It can transform a text. It can even transform a text input into an image, right? Based on something. Okay? Something magical is happening here. We don't know what that magic is yet. But based on some magical thing, it can become an image. It can even take input as a voice. Okay? Because a voice can also be an input, and its output can be a voice again. It can be a text output. Uh, speech synthesizer applications. So, its output can be anything. So, what does a Transformer do? A Transformer basically takes some input sequences. It takes some input text. And based on the Transformer neural network, its output comes out. The Transformer, which I just showed you the white paper. This Transformer was developed by Google in 2017, and its primary Transformer was in Google Translate. Okay, Google Translate. So, Google Translate is kind of a Transformer itself, which can take, let's say, English language. You gave it some input in English, and I got the output in Hindi. Let's say you said "Hi," and you got "Namaste" as output. So, what happened here? They designed a Transformer that can take an English language input and output it in a selected output language, let's say, in this case, if my output language was Hindi, it could output it. So, this is the whole idea behind Transformer. That we need to build a Transformer. In the case of GPT, like these LLM applications, how does this Transformer work? If this is your Transformer layer, it takes some input. Let's say I told it that "Hey, I am P.I." Let's say this was my input, okay? I will only give this much input. So, what this Transformer does is, it basically predicts the next word. The GPT Transformer, or your LLM Transformers, their entire outcome is: "Tell me only and only the next word." So, for this particular input, what output will we get? For this particular input, we will get the output "u." Okay? Based on the previous data it has. After that, what can we do? I got "u." That means the next word, the Transformer gave me next is "u." Now, what can we do? We can append "u" to our previous, whatever our initial input was, and put it back into the Transformer. So, now what will we get? We will get "s." Okay? We will get "s." Now, what will we do? Now, we will pick up this entire word again. See, I picked up the entire word again, and whatever our Transformer gave me, when we put it back into the Transformer, what will I get? I will get "h." Okay? Very good. Then what will we do? We will take this entire sequence again. We will append it. Now, this is our input. We will re-run it. I might get "k" for "kh." Let's call it "kh" here. So, as soon as it finishes, that means what output did I get? "Hi, I am Piyush." So, what did we input? We input this particular string, and we got this as output. How? Because we had a Transformer, somehow, let's understand for now, I wrote some magical code that will correctly predict the next word. It will predict the next single letter. And if you repeat this process until my Transformer says "end." It's basically like an auto-complete. If you think about it, if you run it very optimally, that is what your GPT is doing. So, when you input anything into GPT, what is it doing? It predicts the next word within a Transformer and repeats the entire process until when? Until it gets an end. That is basically your Transformer. Understood? Okay? So, we got a high-level design, a high-level understanding of what it is. You have an input sequence. You get the next word prediction. You append it back, and you repeat this process. Very good. Now, let's look at this Transformer. That how does this Transformer work? Okay? How does this entire architecture work? See, when we say some word, let's say if we told it, "Hey there." Okay? Computers don't understand English, Spanish, Hindi, Marathi, Gujarati, these things. Right? These words, these letters in front of you, these English letters, our computers don't understand them. For computers, math is more efficient. Okay? Numbers. So, computers prefer everything in the format of numbers. So, the first step that comes, okay? Let's do one thing here. Let's type in the first step. Step number one. This is our step one, which we call the encoding phase. Okay? Step one, encoding phase. What are we talking about now? We are talking about the Transformer architecture. And this is not made up. You can see that some input came here. So, we are talking about input embeddings. That what is happening in case of input. Okay? Let's look at the encoding step. When the user gives you an input, the very first step is to tokenize it. We will split it into tokens. What is the meaning of splitting out? What is a simple token algorithm? Let's do one thing. Let's consider "Hey" as one token, and "there" as another token. Okay? So, what are we doing? We are splitting it, and we will assign it to an equivalent number. Let's say I have a dictionary here. I have a master data here where I know that the value of "Hey" is, let's say I decided the value of "Hey" is 10, and I decided that the value of "there" is 20. Uh, let's say 36, randomly. Okay? I have taken such random numbers. So, what will this particular step do? It will convert it into tokens. It will tokenize it. So, for "Hey," I will get 10. Okay? For "Hey," I got 10, and for "there," I got 36. So, in this particular step, what are you doing? Whatever input the user is giving you, you are tokenizing it. Okay? So, let me just type in "tokenize." So, what does tokenize do? It maps inputs to a particular number. How could you do this in a simpler way? Let's say I will map A to one. B I will map to two. C I will always map to three. And if we have this dictionary ready, then what will we do? H E Y means we will keep converting them into numbers. And our tokenization will look something like this: H is a token, E is a token, Y is a token, then space is also a token, then T is also a token, then H is also a token, then E is also a token. So, based on this, what will you do? You will generate numbers. So, if we tokenize Piyush, so P will have a number, I will have a number, Y U S H. So, that means we will have six tokens. This is tokenization. Now, in reality, this tokenization varies from model to model. There is no fixed algorithm for it. So, this dictionary varies from model to model. So, I will tell you a website. If you want to visualize tokens, there is an app by Vercel called "Tokenizer." So, this Tokenizer will help you understand how these tokens are converted. So, if we take the GPT-4 model here. Okay? And the user told me, "Hey there." Okay? So, you can see that in reality, how many tokens were created? Nine tokens were created. How? In the case of GPT, there is this is your start token, from where it is known where the sentence starts. I am start, and its equivalent token is 20064. Okay? Then what was the user's token? 1428. And then you can see this is how the tokens are being built. So, what does this step do? It converts user input into numbers. This is called tokenization. It's very simple. Every model has its own tokenization. For example, if we choose Google, and if I type in "Hey there, comma, my name is Piyush Garg." So, you can see that if we choose the Google Gemma model, then this is how the tokens are built. Okay? There are 11 tokens. "Hey there," see, this is one token. Okay? This is a token. "My name is Piyush." See, this is one token. This is one token. This is one token. And then "Garg" is one token. And these are the equivalent numbers. So, what does tokenization do? It maps these particular things to a particular number. Why? Because it is easier to visualize numbers. Okay? So, you can also create your own tokenization. It's very easy. So, what you need to do is, here you have, like I have a Google Colab here, and it is GPU enabled. But okay, for tokenization, you don't need any GPU or anything. So, if I do here, `pip install hf hf-transformers upgrade transformers`. So, Transformers is basically a Python library by Hugging Face, which has many such tools, like, you know, if you want to tokenize something, do something, then all those things. So, for a second, let's install it. So, you can see that Transformers has been installed. And if we create another code block here. So, we will see `from transformers` I am importing an `AutoTokenizer` here. And then I can make my own tokenizer. Okay? Okay, so let's create a tokenizer. I can just say, "Hey, AutoTokenizer, I want to create a tokenizer." Okay? How do you create a tokenizer? From a pre-trained model, from `_pretrained` is a function here. And here you need to give the name of your model. So, let's say in this particular scenario, we will use Google's Gemma model. Gemma model. Okay? So, I need to create this tokenizer. And then what I will do is, here we will say, "Hey, tokenize." Okay? Let's do one thing. Let's try to tokenize. So, I will call the tokenizer here and let's try to tokenize. "Hey there." So, let's say if we run this. Run this particular application. Let's see what we get. Okay? So, let's let Transformers import it. So, we created a tokenizer for this particular model. Okay? It varies from model to model. For this particular model, and in the tokenizer, we gave this our input string. Uh, and yeah, you have to basically, you need to give your Hugging Face token here. So, that is fine. Okay? Let's run this again. So, now it will work. So, it did what? It loaded the tokenizer for this particular model. And if we wait a bit here, and please do not use my Hugging Face token, because after this video, I will revoke it. Okay? So, this is what we got, the tokenizer. See, its "Hey there" became these tokens. Okay? So, that means if we tokenize "Hey there," our tokens became: 2 177531 2085 23688. Nice. If we say, "Hey there, my name is Piyush Garg," and we run this. So, what you will notice is that we will get some input tokens here. So, from the tokenizer, what am I able to do? I am able to tokenize these particular characters for this particular model. Okay? So, how is this happening? Because there is some vocabulary going on. So, now these tokens, okay? These tokens, they will have a pre-built dictionary. Meaning, there is some static thing about how to tokenize. So, that thing is called vocabulary. Okay? Vocabulary. And every model has its own vocabulary size. If you have a larger vocabulary size, it means you can do more complex tokenization within it. For example, if the vocabulary size is large, that means it knows more characters. Larger vocab size means "Hey" and "there" can be one token. Smaller vocab size means H has its own token, E has its own token, Y has its own token. So, every model has its own vocabulary size. Based on that, this tokenization happens. Is the tokenization process clear to us? Very good. So, now after tokenization, we do something known as vector embeddings. We create vector embeddings of these tokens. Now, what are these vector embeddings?

Here are the translated paragraphs:

? See, every word has a meaning. Okay? Every word has a meaning. When I write a word here. Let's say cat. Then an image formed in your mind. Right? Very good. The cat's favorite food, let us say we wrote milk here. Right? Cat's favorite food is milk. On the other hand, if we write Pedigree here. Okay? If I wrote Pedigree here, okay, then what word should come to your mind? Your brain will say that dog should come here. Right? What should come here? Dog should come. So these particular words, cat, dog, Pedigree, milk. How did you basically predict that dog is more relevant with Pedigree? Because in the real world, we see these things. We observe these things. They have a semantic meaning. So if we search here, what are vector embeddings? Vector embeddings are a numerical representation. Of what? Of data that basically captures semantic relationships and meanings. That means that when these words, which we have just tokenized. When we create their vector embeddings, a semantic meaning will emerge from them. So let's understand vector embeddings. What happens inside vector embeddings? Let us say we made this graph. Right? This is a 3D graph. We are understanding it in 2D for now. But in reality, it's a 3D sphere. Okay? Let us say we took cat and dog here. Correct? Assume for a second that the tokenization of cat, the token, was 20. And the dog, the token that came out for us, let us say 80. So what will happen is, if we plot it here, when we create its vector embeddings, and try to plot it on this graph, it will be something like, let us say, we took cat at this particular point, and at this particular point, let us say we took dog. Right? So let us say the vector embeddings that came out for cat. Right? So let's make this a bit smaller. This came out as cat, and this has come out as dog here. Now what happens is, when the vector embedding for milk was created. Right? Let us say we created the vector embeddings for milk, then the vector embeddings for milk turned out to be something like this. Right? It formed somewhere around this particular area. So let's take milk here. So what will happen is, if we, if I show you the magical thing here. Right? Let's change the color. If I calculate the distance between these two. Right? The direction of cat and dog, first look at the direction, it's downwards. And if I calculate their distance, and in the same direction, I go towards milk. Right? And then if I go downwards in the same direction. Right? Like, then here, there is a high probability, a high possibility that I will find Pedigree here. Right? So that means, that means what is happening is, here I was able to capture semantic meanings. You are getting my point? Now which word is related to which word? What is its meaning in the real world? That is called vector embeddings. Nice. Correct? Similarly, it might be that cat. Correct? Cat is a domestic animal. Let's say. Right? So if we take a word, domestic. Correct? So, let's say this is an animal. So what will happen is, if we take a word. Let's say man. So man is a human. Correct? Man, let's say it's plotted somewhere here. These are just examples. Right? I am not drawing any real arrows. So let us say we took a man here, and man is plotted somewhere here. So what will happen is, if we calculate the distance between man and dog. Right? And let us say somewhere in this entire, you know, vector embedding space, the word animal is written here. Okay? Let's say animal was somewhere here. So what will happen is, if we take the pen again here. So if we go from dog in the direction of animal. See what the direction is. Right? So that means what will happen is, if we go in the same direction from man, then here, there is a possibility that I will get a human. So I am able to link that if dog is an animal, then man is a human. So, what is our process doing? If we visualize its directions a bit. Right? In one direction, it is placing breeds, like cat, dog. In one direction, it is placing what entities are possible, what is their nature, we have placed that in this direction. Right? And what they consume, we have placed that in this direction. So that means, if we repeat this pattern somewhere. So if I take the distance between dog and Pedigree. Right? And I go the same distance from man. And then go in the downward direction, as much as its direction was. Then there is a possibility I can get something like sandwich. Because man likes sandwich. So basically, here, if you understand vector embeddings a little, what is happening? We are understanding semantic meanings. Related words. What is the relation of one word with another word? What is its meaning in the real world? That is what we capture in vector embeddings. And how do vector embeddings come? Basically, it's a matrix. Okay? So, let us say the dimension size of your vector embeddings is 512. That means, there are possible 512 dimensions. So, what happens? The vector embedding of this 'the' will be created like this. So, let us say, uh, basically a matrix is formed. Right? Let's create a matrix. I'll have to work with a pen. So let's say a matrix was created, and I wrote some numbers here. Like I wrote 0.3 here. Here I wrote some negative number. Here some number, here some number. So there are some numbers like this. And then we created another, you know, an array. Then there are some numbers here. So basically, our vector embeddings are formed in this way. So, see, we first tokenized. We converted the word into a number, which is always fixed. And then, to find its semantic meaning, we converted it into vector embeddings. How do we convert into vector embeddings? Using some vector embedding model. Like OpenAI's vector embedding models are available. So if we search here, what are OpenAI vector embeddings, vector embeddings. So if we search here, the documentation for vector embeddings is very simple. Okay? So how does it work? See, very simple code. What you did is, client.embeddings.create. You took your input text, dog, cat, cat, loves, milk, whatever, and you decided the model, and you will get embeddings in return. And this is what your embeddings look like. So can you see that these are our embeddings. Okay? And if you look at the dimension here, or so, by default, the embedding vector is 1536 for this particular model, and 3072 for this particular model. To reduce the embedding dimension without losing its conceptual representation properties, you can also tweak the dimension parameters. So, see what will happen? If, let's say, I downscaled the dimensions. If I reduced the 512 dimensions, then the meaning between words will start to be lost. And on the other hand, if I keep a larger dimension size, then I have more diverse meanings. So this is called vector embeddings. The code for it is very simple. It's just this much. You have to call an embedding model, and you have to give an input, and that's it. Okay? Just in case you want me to run it, no problem, I will run it. So if we go back to our Google Colab. I will just do a pip install openai. So let's install the openai package. And you will need an OpenAI key. That is, quite obviously, right? So what I will do is, you can see, this is installed. So let's create another code block here. And copy-paste the code. But before that, before that, you will have to set an OpenAI key. Right? So for that, I will do import os here. So import os, and then we will say os.environ, and here the name of our key, I will tell you in a minute, that is OPENAI_API_KEY. So let's copy this, equals to whatever your OpenAI key is. So I will copy this. And again, if you use my key, it will not work because after this video, I have to revoke these. Right? So we have set our OpenAI API key. Client.embeddings.create. So let's give an input, let's say cat loves milk, anything, an input. Used a small model, and let's run it. So let's see what happens. Okay? So let's look at the response. What response do I get? A client equals to openai, uh, what happened? Okay? Uh, my bad. Let's do this below. Right? First, we will set the API key, and then we will do this, obviously. So why did the error occur? Because we were setting the API key later. So set the OpenAI key, do this. And you can see that these are our embeddings. So can you see that this is just a bunch of numbers? If you look at it, these are our embeddings for cat loves milk. Okay? So, basically, what is this doing? If we are using this model, the small model, then by default, what was the token size? Uh, what was the dimension size? So the dimension size was mentioned here as 1536. So, 1536 embeddings will come to us, meaning what is its meaning in 1536 dimensions? What is the semantic meaning? This is a purely magical thing, I understand. Okay? So let's do one thing. Let's also check the length of these embeddings that came out. So I will just copy. Right? And let's paste this. And I want to see the length of its embeddings. So length of these embeddings, and run. So let's see what happens. Right? Uh, so what, let's comment this out for a second. So you can see 1536 is the one. Because we are using a small model here. So as they said, the small model's length is 1536. So we have a vector embedding of 1536 ready with semantic meaning. So up to here, your white paper, your input embeddings, this is complete. So very good, you have completed step one of your transformer. Then let's go to step two. Okay? Then comes step two, which is called positional encoding. Now this is a very important thing. Positional encoding. See, what happens is, when you create these embeddings, you are creating token-by-token vector embeddings, correct? But you might want to store what is the position of this particular word in the sentence. What it means. See, if I take an example here, that if I take an example here, "The dog chased", okay, "the dog", "chased cat", and I take another sentence, "The cat chased dog". If you look at these two, the tokens are exactly the same. Every word in both is exactly the same. If you look, see here you have 'the'. Right? Uh, you have 'the' here. You have 'dog' here, which is basically 'dog' here also. The word 'chased' is there. The word 'cat' is there. So that means I have used exactly the same words here. If you, you know, what you are doing is, if you are writing your own tokenizer, you converted them into tokens, you created their vector embeddings. Both can have the same vector embeddings. Correct? So here, we have lost the position of dog and the position of cat. Because of which, the meaning of this entire sentence remained the same. Which is not. In English, if you read these sentences, you clearly understand that the meaning of both is completely different. Right? So, what does positional encoding do? It basically adds some data about the positions of the tokens. Now we will call them tokens. Right? You can call them words. You can call them sequences. But tokens is the right word. So it tells the positions of the tokens. So how does this particular step work? This particular step does what? It adds something to your vector embeddings. So let's put an add symbol here. So what it will do is, we will create another, you know, a similar, uh, a positional encoding matrix. And let's give it a different color, let's say red. So what this will do is, let's say it has some parameters, like the first number, we added something to it. To this number, we subtracted something. To this number, we did something. So what will happen is, its result, its result will be kind of a different matrix. But, but what will happen is, because of this positional encoding, the matrix that comes out here will be different from the result of this positional matrix. Right? So if I go here for a second. So what you will understand is, I will show you some examples. Uh, positional encoding, positional encoding, since our model contains no recurrence or no convolution, in order for the model to make use of the order of the sequence, we must inject some information about the relative or absolute positions of the tokens in the sequence. Okay? To this end, we add positional encoding to the input embeddings at the bottom of the encoder and decoder stacks. The positional encodings have the same dimensions as the model, as the embeddings. So that they can be summed. There are many choices of positional encodings, learned or fixed. So here, this is the cosine and sine formula used to create this positional encoding. So what is the complete meaning of positional encoding? That, hey, if this dog is the second word here, and here the dog is the last word, then to maintain the positions, I need some kind of a delta. We won't call it delta. Let's call it a magical matrix. Right? Let's call it a magical matrix for a second. That when I add it to the original matrix, to the vector embeddings, then the new matrix that comes out, for every token, will have those positions encoded. So this is the step for positional encoding. I hope I make sense. Okay? Don't worry, if you want me to explain it in a little more detail, then let me know in the comments. I have already studied all these things in detail. The only thing is that perhaps we don't need to go into that much detail. That's why I am skipping it. But I hope you got the context behind it. You got the agenda behind this positional encoding. Okay? Got it. Nice. Okay? Now comes the very important step. Right? If we go back to our diagram, so there is something known as this multi-head attention. This particular attention model, you will hear about it a lot in this particular LLM field. See, let's understand one thing before that. What is self-attention? Right? What does a self-attention model do? Before the transformer architecture, that is, before the transformer architecture was built, before 2017, we had an RNN neural network. Okay? In this, what happens is, in a very simple neural network, what happens is, let's say you have some tokens. So this is your token. This is your token. This is your token. And this is also your token. What you used to do was, one by one, one by one, you would encode it. You would encode it. You would encode it. And you would encode it. Right? Now, if you see this particular problem, you can study RNN and CNN in depth. The problem with this is, number one, you are processing one sequence at a time, which is very time-consuming. Secondly, you have lost context. You have lost context. Now, what does losing context mean? If I write "The river bank", "The ICICI bank". Right? And this video is not sponsored by ICICI, by the way. So what does this mean? If you look, if we train this in an RNN, right? Then see what you did. You picked up one token, 'the'. You picked up 'the'. You tokenized it, did vector embedding, very good. Then you picked up 'river'. Right? 'River'. You picked up 'river'. You tokenized it. You converted it into vector embedding. Very good. Then you picked up 'bank'. So what you did was, you tokenized it. You created its vector embeddings. You also did positional encoding. Finished. Similarly, this is your sentence one, finished. Right? Then again, when you come to this sentence, you picked up 'the'. Very good. Same. Right? ICICI will have its own vector embedding. 'Bank' will again be the same as this 'bank'. But my friend, tell me one thing, are this 'bank' and this 'bank' the same? The word is the same, 'bank'. Correct? But if you really think about it, this 'bank' refers to a real bank, the one we talk about in the banking system, the financial banking system. But the meaning of this 'bank' is completely different. Because it is coming in the context of a river. Correct? It is coming in the context of this. So, what is happening is, this is a problematic thing. This is a problematic thing. So, in the case of RNNs, in the older neural networks before the transformer architecture, this was a problem that you lost context. But then, the self-attention model, the self-attention model. What does the self-attention model do? It allows these vector embeddings that were created. Right? If I carry forward this example. So, let us say, we take "The river bank". Let us say some vector embeddings were created for them. Right? So first, there must have been tokenization. So let's say its token was created. Right? Its token was created. Its token was created. And then you created its vector embeddings. Right? You created its vector embeddings. So you created these three vector embeddings. And you also did positional encoding. Now, what happens in the self-attention mechanism? These tokens are allowed to talk to each other. Right? They can talk to each other and change their vector embeddings. That means, if 'bank' is coming in the context of a river, then its vector embeddings, if we change them, it changes the semantic meaning. If we go back to that diagram of vector embeddings, if the first step by default placed 'bank' here. Let's say. Right?

Is it? Which was the financial system where we use banks. When we come into this self-attention model, the river can talk to the bank and the bank can talk to the river, their bids. So it's possible that this bank shifts a little bit here, which changes the semantic meaning that, hey, this is not the bank we use financially. This is a bank for a river. Got it? So, what happens here in the self-attention mechanism? The vectors are allowed to talk to each other. Okay? So, to do this, what is done is basically there is an input, there is an original matrix, there is a transpose of it, you divide it by the square root of the dimensions of the model, and after that, you find out which word is in what relation with which word. So, the entire motive of self-attention is what is the motive? That if this word is with the word river, it means that we can also change the surrounding contextual information into vector embeddings. So that means here, the context of the word is maintained. Okay? This is very important. Right? Because if this doesn't happen, then your output from the LM will be very bad. Correct? So, here, in this transformer architecture, this is possible within self-attention. So, did you understand self-attention? What is the entire motive of self-attention? That the context of the word is maintained. That is basically a contextual thing. Okay? And it can also be said that it basically relates words, which we also call tokens, to each other. So, this is its entire motive, to each other. This is its motive. Nice. Okay? Now, there is no need to go into its formulas. If you are an application developer, meaning if you are a researcher. Okay? You can go into its formulas. But okay? As a developer, you should just know that what happens in this process? There is no need to go too deep. Now, what is this called? This is basically your single head. Okay? Single head self-attention. Okay? This was your single head self-attention. But if you go into the white paper and you see there is something known as a multi-head, multi-head attention. So, what does multi-head attention do? Multi-head attention, what this particular thing does, it improves. Okay? It improves the contextual understanding. Okay? So, in this particular process, you do something known as this process is done because to improve the contextual understanding. In this, what you can do is you can do some kind of parallel. Okay? So, which is called heads. That is what is known as multi-head. So, you can say that you have multiple heads. Let me give an example. Let us say you are looking at a train. Okay? A train is going. You are looking at a train. A train is going. On a train, you saw a dog. Okay? Let's say you like dogs, and you saw a dog, and that dog was doing something. So, what happens is when you saw that scenario, and later when you explain it, so what you will say is that you will say that, "Hey, the dog was in the train." Okay? Let's say one part of your brain understood this thing, that the dog was in the train. The second part of the brain understood that, you know, the dog was sleeping. Maybe it was sleeping. Okay? It was sleeping. So, the dog was sleeping. One part saw this. And the third part might have seen that the dog was, you know, brown in color. Let's say brown in color. So, what happened, see, you noticed three things. Okay? You noticed three different things. So, this is basically what your multi-head attention does. So, it takes the different sentences at the same time. Okay? Takes different sentences at the same time. And then combines them to understand everything better. Okay? So, by combining these things, you can get basically this output. So, this is your multi-head self-attention. So, in this, you have contextual information, and you are able to do some kind of parallelization. So, overall, what is its motive? It improves your overall contextual understanding. In LLMs, context is everything. You must have heard, there is a context window. Context is everything, what are the surrounding words? In what context is the conversation happening? Because that helps it to understand the next set of sequences. Okay? Okay? Great. So, so far, we have understood that when the user gives us some input, what are we doing? We are doing an encoding. We are tokenizing it. We are creating vector embeddings out of it. And we are adding its positional encoding matrix to it. So that we have the, we have that context, that which particular word is at which position. Right? Then what do we do? Then we basically go into a multi-head attention. Where we have the contextual understanding. We relate words to each other. We allow the embeddings to talk to each other. Right? That is the main thing. That we allow the vector embeddings to talk to each other. So that they can change their vector embeddings according to the surrounding context. Now, what will happen is there is a normalization layer. Okay? Let's not talk about this. This is like a deep research thing. So, add a normalization layer and a feed-forward layer. So, this particular process, this is repeated. This process is repeated. So, what happens is basically if you gave an input. Let me just change the color to white. You gave an input. So, what happens? See, encoding happened. Okay? Encoding happened once. Very good. In the entire encoding step, you have done vector embedding, position, encoding, everything. Now, what happens is attention. Okay? Self-attention. After that, feed-forward. Right? After that, back here, then back here. So, what happens is in this entire process, your output, your output tokens, they keep getting refined. Okay? So, they are basically repeated again and again and again. And then finally, what happens? You get a new matrix, a new sequence. So, let us say, if in this particular scenario, this was your input. Correct? This matrix, this was your input. So, what you can assume is that at the end, okay, at the end, you will basically have one more matrix. Okay, one more matrix. And you can assume that this matrix, this particular token, this is your next word, this is your next token. So, what will you do with this? You will decode it, right? Decode means, remember when you tokenized your sentences, you converted them into numbers? So, here, what do you do? Basically, you detokenize. Okay? Detokenize, meaning for a given token, give its human-readable format. So, let's say its output came, let's say its output came as U. And then what do you do? You basically repeat this process. Then what do you do? These three things, you again give them as input. Okay? You again give them as input. It goes through the process. It goes, goes, goes, goes, goes, and then what happens? Finally, the next set of tokens are predicted. Okay? Then what happens? It again goes back, and the next set of tokens are predicted. So, this is basically how an LLM works. Okay? It's not over yet. It's not over yet. So, let's understand this part a little bit, how the output is generated. Okay? So, let's say what we did? Let's take an example. Okay? Let's take an example. I said, "Hi, how are..." You know what will come next. Correct? So, what happens? We give this to our encoder. Okay? Let's talk about the input for now. We gave this to our encoder. Correct? Okay? This is our input. After encoding, what happened? The encoder gave us an output. What is this output? Basically, I'm talking about the vector embeddings. Okay? Vector embedding, self-attention. After that, the vectors that are formed, that is this output. Then this output goes to our decoder. Okay? Goes to the decoder. And we also give the decoder a decoder input. That is, how to start initially? So, let's say this was our input. Okay? "How are..." Let's say I told it, "How are you?" Let's say we told it, "How are you?" Now, our LLM has to reply. Okay? So, user input went, was encoded, output went, went to the decoder. Now, what will we give to the decoder here? We will tell it, "Start." Okay? So, let's say I told it, "Start." So, "start of string" is said like this. "Beginning of string" is given something like this. Okay? "Beginning of string" or "start of the sequence." So, let's say I gave it an initial input, "Start." So, what will happen is when this "Start" goes inside it. Okay? This "Start" is again, you know, encoded and everything. It will go into the decoder. So, now this decoder has two inputs. Number one, what the user initially gave. Plus, what we already have. Now, what happens? It basically goes to an output. Okay? What does the output do? The output is basically nothing. It's a prediction. It predicts the next word. Correct? So, what will it do? It will predict an output. Okay? It will predict an output. And in this output, there are multiple predictions. Okay? When you talk about an LLM, keep one thing in mind. You have two stages in an LLM. Number one is known as a training phase, and number second is known as an inference phase. Okay? Inference phase. So, what do they mean? Training phase, where you are actually training the model. And inference, just a second. Inference phase, where you are using it. Let's talk about the training phase first. Okay? What happens in the training phase? You gave "Hi, how are you?" and you have its expected output. Let's say the expected output is, "I am fine." Okay? We already have this as our expected output. Okay? So, what happens is you give this, you give this. Okay? And it should end here. Now, what will happen is when you give this to the output, the output will give you an output. So, technically, tell me one thing. If we are in the training phase, for this input, this must be the output. Okay? What should be its next sequence? I know this is a little bit tricky. If this is the input, and we have this output, this training phase is going on. We know both the input and output. So, its output should actually be "end of string." Yes or no? Technically, there should be nothing after this, right? The output should be "end of string." So, this is called the label. This is our expected output. Then what do we do? If we gave this input, and let's say whatever we got in the output, let's say whatever output we got, we calculate a loss for it, meaning the output the model gave us might be wrong, might be right. The model gave us some output like this. This was my expected output. Right? We will calculate a loss for these. That is known as cross-entropy loss. And then what do we do? Based on that loss, we do backpropagation. Okay? We do backpropagation. That means our neural network. Okay? These layers, remember? If we go back a little. Remember these layers where we did this again and again and again. So, this is basically a neural network. If you have seen some diagrams of neural networks, meaning I am not a machine learning student, but okay? If you have seen something like this, a neural network. Right? What happens inside it? You have layers. So, there are diagrams like this. Right? You have layers. These are connected to each other like this. So, this is connected to this. So, something like this happens. Correct? So, in this, what happens, meaning it's not multiple like this. Okay? So, what happens in this particular scenario is you go back. Okay? With the loss, meaning this was the expected output, and you keep on changing these weights. You change them. See, what happens? In the forward direction, you got an input. You did something, and you gave me an output. Now, the expected output might have been this. You gave me this output. The loss between these two, what do we do? We do backpropagation. And we keep on updating the weights so that next time, we get the expected output. It's basically like learning. Okay? When we talk about machine learning. See, if I asked you 2 + 2, and you told me five. I told you four. Now, what happens is you basically try to go back and remember that whenever 2 + 2 is asked, I have to give the answer four. That thing. Okay? So, in this, what will happen? Backpropagation happens with the loss. So, what happens with this? The model trains. What is happening with this? The model is training. So, predict the input, output, next token. Whatever next token it predicts, you match it with your label because you already have the output. You match it with your label. You calculate the loss, and you do backpropagation. So, this is your training phase. This is how your entire model is trained. When you are in inference mode. Okay? Inference mode. Inference, not interference. Inference mode. Okay? Ignore the spellings. What happens in inference mode? You are using the model. Technically, the same thing happens. If we copy this. Okay? This entire thing, we copy it like this. And we do a paste. Backpropagation does not happen in this. Right? You don't want to change the model's weights. So, you gave the input, encoder, output. You only gave "Start." You don't know the output, right? You just said "Start." So, what will it do? Based on its training data, it will tell you, let's say, "I." That is it. In the first step, what did it do? It only gave you "I." Okay? Then what happens? Right? You got "I" as output. Very good. Okay? Now, what will happen is take this. Okay? Take this and take this. Okay? Take all of this and start this process again. But but in step two, after you have already completed the first step, you do not repeat this process. Because technically, the output of this process remains the same. So, you don't have to repeat this process. So, what happens is "Start." One. It goes to the decoder. Outputs, let's say, "A." Okay? Then what happens is you take this. You append it here. You restart the process, and the next is given "space." You go back, you go ahead, and "I am fine." And you repeat this process until you get "end of string." So, until you get "end of string," you keep on repeating, and it is expected that if your training data was good, you will get an output, something like "I am fine." So, basically, this is how your whole model works. Okay? So, net net, if I want to tell you, if we understand the white paper a little bit. I haven't gone too deep. You have input embeddings. You have positional encoding. Right? You have multi-head attention and a feed-forward layer. This is where that loop runs. And if you look at the output, what is happening is an input is going from the input side. Okay? And from here, your output came. See, your output came from here. And this is where your linear and softmax are applied. What is linear? This output I was talking about, this is basically your linear. What does linear do? Linear gives you probabilities that what could be the next token? For example, if "I am" was going on. I know "f" is the next token here. So, linear will give you a probability distribution that, hey, see, "f" could be, "g" could be, "n" could be, "x" could be. Let's say it said, the probability of "f" being is 0.9. Okay? Let's say I'm taking any example. Its probability is 0.1, its some probability, its some probability. Okay? So, what does this do? This linear function, which was your here, I don't know where I wrote it. Yes. The linear function, your linear function, basically gives you these probabilities. Now, which one will you choose from this? Of course, the one with the highest probability, the one whose probability is higher, you will choose that. So, here, the next step is softmax. Okay? Let's write it here, softmax. So, what does softmax basically do? It's a function. Softmax is a function that chooses which one to choose. So, let's say if the value of this softmax is one. If the softmax value is one, then what it will choose is it will choose "F." But if you want to be a little more creative. If I increase the softmax value, let's say. Okay? If I tweet it, then maybe I can make it choose "G." So, basically, softmax is responsible for picking up which, you know, which linear to pick? Which probability distribution to pick? That is your softmax. If you have ever noticed. I will show you something. If we go to Gemini Studio. Okay? Gemini Studio, meaning it's also available in OpenAI, it works with all LLMs. But okay? Let's go to Gemini AI Studio. Because it's free. You can also try. So, what will I do here? You see temperature here. What is this temperature? This is basically your softmax. Let's do one thing. If I don't touch softmax. I just keep it one. Okay? And I give it a "Hi." And I send this particular message. So, what you will notice is I got, "Hi there, how can I help you?" Let's increase the softmax. Okay? And I just send a "Hi." Okay? You can see that it will do what? It will go a little more creative. Okay? Let's say "Hello." "Hello." "How can I help you?" So, what does this softmax, this temperature do? It's basically controls the creativity. Creativity allowed. So, see, if you increase softmax, then what will happen is it will tend to pick up this "g," "n," or "x." So, creativity increases. So, this is basically your temperature thing. This is basically your softmaxing. So, this is kind of your entire working, how your AI models work. So, just in case if you want to see this in a little bit of code, you must have understood embeddings. So, I will show you a simple code. We have already installed transformers. So, how can you do it? See, it's very simple. You have your own tokenizer.

It was made. Okay? Look at this, you have created a tokenizer. So, what does a tokenizer do? It tokenizes user input. So, let's do one thing. Here we say input tokens. In the tokenizer, I put that a right a Python code for let us say anything you can give Python code for adding two numbers, okay? So, what did I do? I basically tokenized it and I want to see its tokens, what input tokens it created. Okay? If we run this, then you can see input tokens have been created. Now, what will we do? Okay? Our tokens have been created. Now, let's do one thing. Let's load the model. So, here I will just say import torch. Okay? We imported torch. So, let's do one thing, let's load the model. So, here I have to import auto a model. So, from the documentation, I am just copying. Okay? So, here we do from transformers import AutoModelForCausalLM. So, here we will say that do one thing, load a model for me. Dot from a pre-trained. Okay? Okay, from pre-trained and here you have to give the model's name. So, what was our model's name? We set it somewhere above, uh, say, here is the model name. Okay? So, copy this model name and here we have to give the torch type. So, torch dtype b16. So, as soon as we run this, it will download and load my model. This might take some time. If you have loaded it once, it will not take time. But you can see here, it will take some time. I think it is around a 2GB model. Yes, it is a 2GB model. So, you can see a 2GB model is getting downloaded. Okay? Now, let's wait here for a bit. Okay? You can see my model has, I think, been loaded. Now, what will we do? Let's create another code block here. So, what I will say is, I will just say output equals, I took the model. Okay? Model, and inside this, what will we do? We will give input IDs. Okay? Input_ids. So, how will I get that? You might remember, we are getting these input IDs. So, do one thing, take the input tokens and from them, I will just take the input IDs. So, what will this give me? It will give output. So, let's print the output here. That means for this input token, this should be the output token. And what error did we get here? Okay, my bad. Here, when you tokenize, just do a return. Okay? Return tensor, a tensor = PT. Okay? So, we need tensor output. Return tensors. Yes. So, you can see I am getting a tensor. Nice. Now, let's run the output again. So, now you will notice that what will be our output here? We will get predictions. Okay? So, look what we got here. We got a lot of predictions here. What did we get? We got a lot of predictions. Now, what can I do? See, what will we do? If you look here, I can generate from this. What is this? These are our predictions. These are all linear. These are all basically allowed values. Now, let's do one thing. Let's try to do a generation. Okay? I will do a generation from the model here. For that, I will just say model.generate, generate. Okay? And the same thing, I will just do input IDs. When you use the generate function, it basically gives you the one with the highest probability. Here, I got everything. Here, you will get the one with the highest probability. Okay? So, let's generate. And after that, we will put it in the gen_out variable. And here we will do gen_out. Okay? So, let's print and see what I am getting as a generative output. Okay? So, let's see the generative output, what am I getting. It will take some time because it is generating, right? All that you saw just now, that model, the whole thing is running. So, it will basically predict the next one. And this is what I got. Okay? This gen_out I got. Now, is this gen_out readable? Is this gen_out readable? No. Why? Because these are tokens. So, what do we need to do? We need to detokenize the tokens. We need to detokenize the tokens. So, do one thing, take your tokenizer that we created above. Inside the tokenizer, there is a function known as batch_decode. And inside this, what can I do? We run whatever this generative output is. So, right. So, look at this, beginning of string, write a Python code for add two numbers, Python def. Look, now what did my LLM do? It wrote a code, add number x and y, add two numbers together. And there is no further output. Why? Why? Because our max tokens were low. So, what can we do? We can increase the max tokens here. So, let's make max tokens, say, 100. Okay? Now, let's generate the output, max tokens. Max new tokens, sorry, max new tokens 100. So, now let's generate. Okay? So, you can see it is running and it will take a little bit more time because this time our max new tokens is set to 100. Okay? So, you can see that I got this output. And now we decode it again. So, you can see that now our token, this is a simple Python code to do, this is, you can see that here we have a large code in Python, def, where is it starting from? Uh, this is a simple code to write two tokens. Okay? So, what did it do? It printed the sum of number one and two and called a sum function. It also gave an explanation. So, you can see that basically it is able to generate the next sequences. Okay? Next set of outputs based on how it was trained. So, this is how the whole LLM works and this was a very broad overview. Okay? It was an overview of how an LLM works. So, the steps are basically simple: you have an encoding. You have positional encoding. Then self-attention, how does the model work? How are the vector embeddings changed? So that it has context of what the surrounding words are. And vector embeddings are allowed to talk to each other. And then how in the output embeddings, you have some initial string. You have the input and output tokens are used. Okay? So, all these things are used. And there are two modes to use, two modes. One is the training phase, one is the inference phase. In the training phase, you even have a backpropagation layer. Where you train the model again. So that it keeps getting fine-tuned. Its parameters, its weights are updated. So that it can make good predictions. And in inference, you don't do backpropagation. Inference means that you are using that model. So, next time whenever you go to ChatGPT, okay? And where is ChatGPT? My ChatGPT is lost. Anyway, or you go to any LLM and you say hello to that particular thing, just imagine what all things are going on behind. Okay? How these tokens are generated. So, yes, I understand that this is completely amazing. You can go as deep as you want to study this. But a recommendation as an application developer. Okay? As a user of AI tools, AI applications, and a builder of these. You don't need to go into the depth of these things. An understanding is enough that how these things work internally. Unless and unless you are a research engineer. If you want to become an AI research engineer, then definitely you will have to go deeper. You will have to understand all those functions, the mathematical calculations behind them, their matrix multiplications. But, but honestly, as an application developer, you don't have to go into this. It's basically like, hey, look, we understand Node internally, right? How Node.js works internally. But we don't really care about the real code. We don't really care what the code is there reality. How which algorithm is used. We just know that how it works. Right? So, similarly here, now you know how it works. It's not necessary that you go into its full depth. Okay? So, that is the thing. So, it's completely on you if this is something that interests you, you can go into its depths. There is a lot of content available. It's all machine learning content. But personally, that thing doesn't excite me as much as building applications on top of it excites me. Okay? How can we deploy these? How can we use these things on a large scale? For a business use case. Very important thing for a business use case. So, that was all about this particular video. I hope you liked the video. Okay? It was a very high-level design. So, let me know what are your thoughts on this particular video. If you liked the video, do like and subscribe. We will meet you in the next video. Until then, bye-bye and take care.