LLAMA-2 🦙: EASIET WAY To FINE-TUNE ON YOUR DATA 🙌

This is a narrated YouTube tutorial (by LARRUZ) demonstrating how to fine-tune a Llama 2 family model on custom data using Hugging Face's Auto Train Advanced package, primarily via Google Colab. The video walks through installing the package, authenticating with a Hugging Face token, constructing the single-line autotrain command (flags like --model, --data_path, --usepeft, --trainer), dataset formatting (Alpaca and Open Assistant examples), and key training settings (batch size, epochs, learning rate, max length), then shows expected outputs and how to push the resulting model to the Hugging Face Hub.
Questions this video answers
Python > 3.8
Yes — an NVIDIA GPU is required to fine tune a model locally (Colab GPU recommended if you lack local GPU).
A CSV-formatted dataset (Auto Train expects dataset files in CSV; provide the folder path, not the filename).
~52,000 rows/examples.
~10,000 examples.
Chapters
This is the easiest way you can fine tune a Llama 2 model on your own data set. We will do it with a single line of code using Auto Train Library from Hugging Face. The best part is you can fine tune any model you want using the exact same line of code. In order to run this locally on your own machine, you will need to download the Auto Train Advanced package From hugging face GitHub, repo. I'll put a link in the description of the video. All you need to do is to run this command Pip. Install Auto Train Advanced. You will need a Python version greater than 3.8 for this to work. You will also need an Nvidia GPU in order to fine tune a model. Now, if you don't have any gpu, you can always try the free Google Colab. And that's exactly what we're going to be doing in this tutorial. Okay, so let me show you how to run this using Google Colab. First, you need to go to runtime. Make sure that you are running a GPU runtime. Okay? After that, simply run the first cell. So this will install the Auto Train Advanced package as well as the Hugging Face Health package. Sometimes it will ask you to restart the runtime after just following this. So you need to go ahead and restart your runtime. Now, if you're running this on Google Colab specifically, you probably want to run this Auto Train setup Update Torch. If you run it locally, you probably don't need to do this. Okay, after that we need to actually provide our Hugging Face token. For that, you need to go to your Hugging Face account, go to Settings and then click on Access Tokens. And here you can create a new token if you want. Or I already have a token, so I'm going to just copy that and then go back to my Google Colab, put that token in there and then hit login. Okay, so here you can see we have successfully logged into Hugging Face. Now, in order to fine tune the model, we are going to be using this single line of code. Well, don't worry if it looks complicated. I will walk you through it step by step. Okay, so let's break this down. So this is exactly the same line of code, but I broke it down into multiple parts so that it's easier to understand. Now, we start with Auto Train. That is the package that we're using. We have this exclamation mark since we are running it within the Google Colab environment. If you are not running it in Google Colab, you don't have to put that in terminal, just remove this. Next we have an LLM a flag in here. The auto train package is not limited to training or fine tuning large language models only. It's a package from Hugging Face which you can use to fine tune other types of models, including computer vision models or neural network models using the tabular data sets. Right. So we need to specifically tell it that we want to use or fine tune a large language model. Next we have this train flag. So it tells it that we want to train a model and after that we need to provide a project name. In this specific case I'm calling my project llama2open assistant. So I'm going to be using a data set from Open Assistant to fine tune my model. Next we need to define which model we want to fine tune or retrain and that is defined using the model flag. So in this specific case I'm using llama 2.7B and I'm using the sharded version because I don't have enough VRAM to load the whole model at once in my Google Colab notebook. That's why I'm going to be using the sharded version. Now another thing to keep in mind is that this method is not limited to four fine tuning Llama models only. You can literally pick any model available on Hugging Face and use the exact same code to fine tune that model. Now another thing to keep in mind that it's not the official version. Somebody was kind enough to create a sharded version from the original Llama models. Right? And there are a lot of people who have done those. So you can pick anyone you want. Okay, so the next thing that we need to take care of is what data set that we want to use to fine tune this model. So we'll have to provide the name of the data set or the path to actually the data set using the data underscore path flag. You can provide data sets using two different ways. The easiest one is to simply upload your data to Hugging Face and just use the repo ID for that data set. So if you look here, I'm using the Open Assistant Guanco data set. Or if you want to use your own data set which is present locally and you're running this locally, you can simply provide the path of the folder in which the data set is located. So it expects a data set in the form of a CBSV file. But the path to the data is going to be path of the folder. You don't want to include the file name when you're providing the path. Now, the only thing you need to be worried about is what is going to be the format of the data set. So I'm going to be looking at two different examples of two different data sets available on Hugging Face that you can use with this model. So the first one is Alpaca 4 GPT, and it has 52,000 rows or 52,000 examples. Now, if you look at this data set, it has four columns, instruction, input, output, and then a text column, which basically combines these three columns with a system message, right? So if you look here, there's this special token for instruction. So that is the instruction in the first column. And then since there is no input in this specific example, there is a special token for response. And here is the response that the model is supposed to generate, right? So you have input or instructions and what is going to be the output from the model. Now we can look at some of the examples as well. For example, here you have three different columns present. And then using the text column, you're simply combining everything together. So this is a typical alpaca data set format and the model actually expects things in the alpaca format, right? So what you want to do is you want to have a single column that actually puts everything together. So in this case we are simply calling it a text column. You can call it whatever you want, but by default it's looking to this text column. Now here is another example and this is the one that we're using. So the open assistant go on co data set. In this case, it has almost 10,000 different sample or examples. Now if you look at this, it's following a very similar format. So here's a special token, that's the human and then response from the assistant, right? So you can easily see throughout this data set, you are going to see this format. So you have your human and then there's going to be a corresponding assistant response, right? So you want to format your data set in exactly the same way. It doesn't matter what you call these, but you want to use these special tokens in order for the model to influence, like what is the input and what is output. Okay, one more thing that I want to highlight is the format that we're looking at is very different than the original prompt template from llama2. So if you look at the prompt template from llama2 chat models, here is the format that they use. But the format that these data sets are available in, or you will need to format your own data set is going to be very different and the reason is very simple. We are actually fine tuning the base model, not the chart model. And since the base model is not following this specific prompt format, you get to decide what your data is going to look like. So after that we are defining the text column to use. So by default it's going to be looking at column name text, but you can call it whatever you want and use that in here. After that we are using the usepeft flag. That means the model itself is going to be using the parameter Efficient fine tuning methods for fine tuning the models. This is a method proposed by Hugging Face and now we're using it in four bit. So we're not using the full precision after that. We are defining the learning rate. So learning rate basically controls the the speed of conversion during training process. You can set it to a lower value, so that will take longer, but it will converge better. The rest of the parameters you can set depending on the hardware that you have. So you can set the train batch size. So since I'm running this on Google Colab, I'm using a value of two for a smaller data set. I have found that using a value of 4 seems to work right, but it really depends on how many GPUs you have. What's the VRAM for each GPU? And you can select it based on that. After that we need to define the number of training epics. I'm using pre, but again, depending on the data set, you probably want to use a higher number here. Now the trainer that we're using is sft. So this is supervised fine tuning. Essentially we're providing the data set in input output format, right? And that's why we're using the SFT trainer. Next we are defining the model max length. Now, if you recall, the new Llama 2 models have a context window of 4096 tokens. I'm defining a much shorter context window in order to speed up the training process. None of the tokens that I'm using in this data set are actually more than 24 it. And that's why we're simply using a much shorter max than tokens. Right now, if you want to push this to Hugging Face hub and your own account so you can use this push to hub flag. For that you will need to provide the repo id. So in my account I'm going to be creating another repo or model that's going to be called llama2open assistant, right? And once the training process is complete, it will simply push that to this specific repo id. Now keep in mind that since it's fine tuning pretty large language model, it will take a long time for the model to appear in your own Hugging Face account. You can expect at least an hour or so depending on the Internet speed that you have for this to show up in your Hugging Face account. So be mindful of that. Then we are simply defining the block size and we also want to store all the training logs. So that's how you fine tune a large language model using your own training data set using a single line of code. So in this case, if you look, it's still going through the training process. So it's going through the tokenization of my current data set. Now, I selected a relatively large data set, so this is going to take a while. It seems like yeah, I'm going to be here for a while, right? So I'll probably not be able to include the final model. However, I want to show you this. So during the training process, here's what you will actually see. So it will create this project folder, so it's the same name as our project name, right? And then it will keep track of whatever is happening during the training process. Right now it's going through the tokenization process, but once the training is complete, you will see a config JSON file as well as a tokenizer and the model file in here in this folder. And then you can use the transformer library to load the tokenizer the model and do inference or prediction on top of those or since we enabled pushing it to Hugging Face so you will be able to see the model after a while in your Hugging face account. And again you can use the transformer package to download download the model and you start using it on your local machine. So that's how you fine tune a large language model on your own data set using this powerful auto train package. Only thing you need to actually do is to provide your own data set. So in an upcoming video I will show you a process of how you can create your own data sets rather than using those available on Hugging Face. And for that we are going to be probably using GPT4 along with the code interpreter. So keep an eye out for that video. Now, just to reiterate, you will actually need a powerful GPU for this to work because if you're running this on a free Google Colab, it probably will timeout on you unless you have a very small data set. If you run into any issues or you want to discuss different fine tuning methods, check out the Discord server in the description of the video. We have a very active community who are helping each other out. If you need help with fine tuning your own model or would like to discuss those with me, check out my kindly link in the description of the video. I hope this video was helpful. Consider subscribing to the channel and liking the video. Thanks for watching and as always, see you in the next one.










