Man

Development & AI | Alper Akgun

Fine tuning Llama2 7b LLM model using Hugging face autotrain

October, 2023

Fine-tuning for large language models (LLMs) involves training a pre-trained model on a specific task or domain to adapt it to perform better on that particular task. Here's a concise explanation:

Pre-trained Model: Start with a pre-trained LLM like LLAMA-2 or Mistral, which has learned a lot from a vast amount of text data.

Task-Specific Data: Gather a dataset related to your specific task, such as translation, sentiment analysis, or text generation.

Fine-tuning: Train the LLM on your dataset while keeping most of its pre-learned knowledge. This process fine-tunes the model's weights to better perform on your task.

Task-Specific Performance: The fine-tuned model is now specialized for your task and typically achieves better performance compared to training from scratch.

Application: Use the fine-tuned model for your specific task, whether it's generating content, making predictions, or other natural language processing tasks.

Here we fine tune a Llama2 7B model using Huggingface's Autotrain in a Google colab. Starting with installation steps.

!pip install pandas
!pip install autotrain-advanced
!autotrain setup --update-torch

You must logging to Hugging face and create a token with "write" access.

1. Navigate to this URL: https://huggingface.co/settings/tokens

2. Create a write token and copy it to your clipboard

3. Run the code below and enter your token

from huggingface_hub import notebook_login
notebook_login() 

Create a a folder and a train.csv file with filled with some task specific data. Below is the content for the train.csv file.

Concept,Funny Description Prompt,text
A giraffe at a dance party,"A girafe, wearing disco lights and surrounded by dance floor tiles, grooving to funky beats, with a sign that reads 'Hug me if you dare!'.","###Human:
Generate a midjourney prompt for A girafe at a dance party

###Assistant:
A girafe, wearing disco lights and surrounded by dance floor tiles, grooving to funky beats, with a sign that reads 'Hug me if you dare!'."
A robot on a first date,"A robot, with a bouquet of USB cables, nervously adjusting its antennas, at a romantic restaurant that serves electricity shots.","###Human:
Generate a midjourney prompt for A robot on a first date
            

Now in a new folder put your csv file and start the training. The autotrain command will download the mentioned model, do finetuning and upload the result back to a new project on huggingface.

mkdir finetune1 && cd finetune1
nano train.csv
!autotrain llm --train --project_name MYPROJECT --model abhishek/llama-2-7b-hf-small-shards --data_path . --use_peft --use_int4 --learning_rate 2e-4 --train_batch_size 12 --num_train_epochs 3 --trainer sft --push_to_hub --repo_id /