The EASIEST way to finetune LLAMA-v2 on local machine!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and welcome to my YouTube channel in today's video I'm going to show you the easiest way to fine tune a large language model such as Lama V2 you can also apply the same for any other llm such as falcon or llama V1 or any other llm available out there today we are going to use a custom data set which is in CSV format and we it has three different columns so this is like an instruction data set exactly an instruction data set but you can use any other data set you want if you if you just have text you can use that what we are going to do today is we are doing the instruction fine tuning but you can also do simple fine tuning if you like to do that so it has three columns First Column is instruction second column is input and third column is output so instruction is give three tips for staying healthy okay so this uh okay so the best way to look at this data that is to just load it and then then we then I think it's pretty easier so let's open an eye python import pandas and so this will be data.csv so here is my data set okay um okay so there is instruction uh input and some kind of output so you see like here there is no instruction hopefully we can find one with the input sorry there is no input yeah so like here is one instruction identifies our odd one out and you have three different inputs or input text and one output so the idea is to convert this to a format which can be used uh by Auto Train that that's what we will be using so we can do that quickly by creating a convert dot pi and you also need to add the instructions right so my data frame is uh PD dot read CSV and here we have data.csv so the technique that I'm showing you you can custom fine tune on any data set you want okay so we got the data set now we need another column so I can call a text column so I can just write for okay and we go through all the columns oh sorry all the rows and we have a new text column let's say this is empty at first so we need some kind of prompt and prompt can be anything you are about you answer question based on the instruction or it can be something like this okay below is an instruction that describes a task paired with an input that provides further context Write a response that appropriately completes the request so this this can be one of the prompts you can design different kinds of prompts and then we have uh instruction and input and a response so instruction row instruction input this row input query because input is a Bar Reservoir so input and what was the let's let's print DF dot add okay so it was instruction input and output that's what I wanted to see instruction input and response is row output the output coming from the bot so now my text will be prompt so sometimes we don't have the input query so I can just say F Len input query Dot strip is zero and it's also probably a good idea to convert just everything to string if not already otherwise it might throw an error event so there is some number or something and I'm going to do one more thing I'm going to say dfsdf dot Bill n a with nothing so here now my length will be zero so so it's going to be prompt Plus and then prompt and switch choose new lines so here I can write um Hash Hash instruction so this is all totally up to you instruction then Plus instruction uh Plus then again a new line response and you have the response okay so your text is your prompt plus instruction learner space then the instruction uh then you have the response you can also like instead add a slash n here and an instruction and then slash n response slash n okay let's use this format this is the alpaca format um response okay and otherwise you just you also need to add the context so I'm just going to copy paste this thing and paste it here so you have the instruction and also the input so you can just say like okay input in the Twitter LinkedIn case it was also like input so slash n input and then you have Plus input query okay so now you got prompt that has after that you have two new lines then instruction yeah the instruction then you have the input input query here you can also add a slash n then you have slash n response okay great so now we can do so this is like a recruit way of doing things text column Dot append text and we can say DF Dot and we have we have just added the text column okay now we can just save it so DF Dot to CSV and let's just save it like train.csv that makes our job easy without the index column okay if you also have um validation sets you can also save it as valid.csv and it will be saved in the same folder where everything is where the script is so I can now just run python convert dot pi yeah so pretty fast so we have already converted our file to crane.csb which has a text column which is with the instructions and everything so now what we can do is that that's all actually if you have the valid column you can also if you have a validation data set you can save it as a valid.csv otherwise just leave it like that the next step is to install Auto Train and you can install Auto Train using pip install Auto Train hyphen advanced so once you have installed Auto Train this is going to take a few seconds you can also run Auto Train setup so since everything is changing so fast in the world of language models Auto Train setup is going to upgrade all your python packages especially Transformers and TRL and all these kind of packages to the latest from the main branch if you don't want to do this don't do this it's a it's a little bit risky too so do it in your own environment uh you can also do Auto Train setup update torch now this is very useful because collab doesn't have torch 2.2 so this is going to install uh torch also uh yeah you can also run the whole thing whatever I'm showing you today in collab so now comes the one and only command which is auto drain llm command and you can see all the parameters by typing Auto Train llm hyphen hyphen help and now it's going to show you like all the parameters that you can tune so we we are not going to look at all all the different kinds of parameters so like let's let's say you have the train split so here in case you have the CSV file the file name is your split name okay you can also use any data set from The Hub so if I wanted here here I could just convert this to a hugging phase data set format push it to HUB and then just use the data set name okay so let's train the model so we do Auto Train llm minus minus train the train command and now we have the project name so project name it can be anything my llm and model name so you have to specify the model from The Hub so here my model is meta Lama slash Lama 2 7B HF so you need to have access to this model so if you don't have access to the model you won't be able to use it and if you don't have access if you have access then to use Auto Train you should be you should be logging in from hugging face CLI I will show you in the end minus minus data path so my data is stored in the same in this folder so I'll just do Dot and now I can do use pepft so we do have to use left and use four bit otherwise I cannot train locally um and we can also adjust the learning rate to let's say 2E minus four uh train batch size since I have two gpus I can do it 12 and num train a box two three and for today we will be using the sft trainer so you can also use the generic trainer so it just provides you the parameter minus minus trainer sft and let's press enter unrecognized arguments project name use theft Okay so use underscore 4-bit and we are missing a missing two hyphens here okay so you'll have the command uh in in like uh in the description box so use it's not use 4-bit so I made this thing and I'm also confused use input so now hopefully it should work so yeah it tells you okay loading this data set from CSV so nothing is stored on the Hub and it's loading the shards and the training should start so the training has started and you can see it's showing me like one hour to go but it's not going to take one hour it's going to probably take like 30 minutes uh since we are using packing with sfd trainer but these are the parameters that you don't need to care about all you need to care about is pushing data in proper format and running one single command now uh this is going to take a while if you want to use this model later on you can also just load it from my llm and you can see like you can also run tensorboard here so when the model is done it's going to appear here inside my llm folder that's your project name so now if you want to if you want to push it to HUB you can also use push to Hub command so let me just clear this for you so if you want to push to HUB you can just add push to Hub and with that you also need the repo ID so with your user and my llama let's say so one once the training is done it's automatically going to push your model here then you can use API inference or you can use inference and points to just deploy the model it's fully production ready or you can remove these commands and you can just play with it locally so your data is local your models are local and every everything is local so I had two gpus so it was using a batch size of 12 but I can also just use one GPU and reduce the batch size to let's say two or four and it works fine on a single GPU and I also tried to use the same command on uh Google collab even the free version it will work but for the free version you have to make sure that you create a copy of this data set and then Shard it into multiple smaller chunks so these are only two chunks here so otherwise it's going to eat up all your RAM so I've already created a sharded version of multiple sharder version of this data set and I can also put that in the description box and you can use it directly in the free version of Google collab and you can just run this command so I think that's is the easiest way to train or fine-tune llm these days I hope you give it a try if you have any questions or comments or suggestions uh please write to me in the comment section do like the video do subscribe and share the video with your friends and see you next time goodbye
Info
Channel: Abhishek Thakur
Views: 167,544
Rating: undefined out of 5
Keywords: machine learning, deep learning, artificial intelligence, kaggle, abhishek thakur, llama finetuning, llama v2 finetuning, how to finetune llama, llama finetuning on local machine, llama v2 instruction finetuning, finetune llama on custom dataset, llama google colab, finetune llama on google colab, how to finetune llm, falcon finetuning, llama fine tuning, llm finetuning, how to train llm, llm training custom dataset, autotrain llm training, autotrain llm, autotrain advanced
Id: 3fsn19OI_C8
Channel Id: undefined
Length: 17min 26sec (1046 seconds)
Published: Thu Jul 20 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.