Fine tuning Llama 2 on Hugging Face can significantly improve its performance on specific tasks. This involves adapting the model to a particular domain or task, which requires careful consideration of several factors.
The first step is to choose the right tokenizer, as Llama 2 is highly dependent on it. The article highlights that the AutoTokenizer from Hugging Face is a popular choice for this purpose.
Selecting the right tokenizer can greatly impact the model's performance, as seen in the comparison between the AutoTokenizer and the Llama 2 default tokenizer. The AutoTokenizer was found to be more efficient in certain cases.
To fine tune Llama 2, you'll need to specify the number of epochs and the learning rate. The article suggests starting with a small number of epochs and adjusting as needed.
Take a look at this: Llama 2 Huggingface
What Is Llama 2?
LLaMA 2 is a pre-trained model that has developed a general understanding of language during its initial training phase, covering multiple topics and text styles.
This general understanding is built on a large corpus of text data, which allows LLaMA 2 to grasp various language patterns and nuances.
The model's pre-training phase is a crucial step in its development, as it provides a solid foundation for fine-tuning on specific tasks or domains.
Fine-tuning LLaMA 2 involves adjusting its parameters to perform better on a given dataset or task, allowing it to adapt its responses and predictions to the specific requirements of the target task or domain.
This process leverages the model's existing knowledge and enables it to generate content that is more relevant and accurate for a specific use case or industry.
For more insights, see: Llm Fine Tuning Huggingface
Fine-Tuning an LLM
Fine-tuning an LLM can be a game-changer for those who need more specific results from their language model. Fine-tuning involves training a pre-trained model on a specific dataset to tailor it to your needs, improving performance, reducing costs and latency, and enhancing privacy.
Check this out: Tuning Hyperparameters
The benefits of fine-tuning are numerous, including improved performance, lower cost and latency, and enhanced privacy. Fine-tuning can also reduce the number of tokens required to generate a response, resulting in lower costs and latency.
However, fine-tuning can be a time-consuming and resource-intensive process, requiring significant expertise in data handling, training, and inference techniques. It also requires high-quality data, particularly if you're doing tasks that require labels.
To fine-tune an LLM, you'll need to prepare your dataset, choose a model, and fine-tune it using a technique like supervised fine-tuning (SFT). SFT involves refining a pre-trained model on a specific dataset under human supervision, aiming to tailor the broad knowledge of the model to particular tasks or domains.
RLHF (Reinforcement Learning from Human Feedback) is another technique used to refine the responses of language models like LLaMA 2. It relies on human evaluators to interact with the model, providing inputs and assessing the outputs, and enables the model to align its outputs more closely with human expectations.
Here are some key parameters to consider when fine-tuning an LLM:
- Learning rate: Adjust the learning rate to prevent overfitting on the task-specific dataset.
- Batch size: Choose a suitable batch size to balance training efficiency and model performance.
- Number of training epochs: Determine the optimal number of training epochs to achieve the desired level of fine-tuning.
Training Methods
To fine-tune Llama 2, you'll need to use a method that's efficient and effective.
The training process involves setting up the training parameters, which include the optimizer, learning rate scheduler, and dataset_text_field option. The paged_adamw_32bit optimizer is a memory-efficient version of AdamW, and the cosine learning rate scheduler helps with training stability.
To group samples of roughly the same length together, you can use the group_by_length option. This can help with training stability.
The trainer class used is from the trl library, which is a wrapper around the transformers library Trainer class. You'll also need to pass in the peft_config and the dataset_text_field option to tell the trainer which field to use for the training prompt.
There are multiple techniques you can apply to make the training more efficient, including:
- Packing: This involves concatenating a lot of texts with an End-Of-Sentence (EOS) token in between and cutting chunks of the context size to fill the batch without any padding.
- Train on completion only: This involves training the model on the completion of the input, rather than the whole input.
You can perform supervised fine-tuning with these techniques using SFTTrainer, which is powered by 🤗accelerate. This allows you to easily adapt the training to your hardware setup in one line of code.
Parameter Efficient Methods
Parameter Efficient Fine-Tuning (PEFT) methods aim to reduce the number of trainable parameters of a model while maintaining its performance. This is achieved by fine-tuning a subset of existing parameters, introducing new parameters, or introducing trainable prompts.
One of the most adopted PEFT methods is Low-Rank Adaptation for Large Language Models (LoRA), which makes fine-tuning more efficient by drastically reducing the number of trainable parameters. LoRA decomposes a large weight matrix into two smaller, low-rank matrices, which can be trained to adapt to new data while keeping the overall number of changes low.
The LoRA method has several advantages, including making fine-tuning more efficient, allowing for multiple lightweight and portable LoRA models, and being orthogonal to many other parameter-efficient methods. LoRA can be applied to any subset of weight matrices in a neural network, but for simplicity and further parameter efficiency, it's typically applied to attention blocks only.
To use LoRA with the Hugging Face PEFT library, you'll need to install the necessary libraries, including transformers, datasets, and peft. You'll also need to load the pre-trained LLaMA 2 model and tokenizer, and configure the LoRA settings.
For another approach, see: How to Use Huggingface Models in Python
Here are the basic steps to follow:
- Ensure you have the necessary libraries installed: pip install transformers datasets peft
- Load and preprocess your dataset using the Hugging Face datasets library
- Configure the LoRA settings using the LoraConfig class from the peft library
- Set up the training arguments and start the training process using the SFTTrainer class from the peft library
By following these steps, you can efficiently fine-tune the LLaMA 2 model using the LoRA method with the Hugging Face PEFT library.
Model and Training
Fine-tuning the Llama 2 model using HuggingFace is a powerful way to adapt it to your specific needs. We'll use the base 7b version of the Llama 2 model, which is loaded using the bitsandbytes library to load it in 4 bits.
The model is then loaded using a helper function that loads the model and tokenizer. For the 4-bit quantization, we're using normalized float (nf) with 4 bits. We're using the use_safetensors option to enable safe tensors format loading.
The transformers library integrates nicely with different quantization libraries, allowing us to check the quantization configuration of the model. The final component is the QLora configuration, which sets the rank of the update matrices (r = 16) and the dropout (lora_dropout = 0.05).
See what others are reading: Fine Tune Llama Huggingface
The original summary is quite succinct, but we can see what the base model produces. Training the model is done using Tensorboard to monitor the training process.
We'll use the paged_adamw_32bit optimizer, which is a memory-efficient version of AdamW, along with a cosine learning rate scheduler. The group_by_length option is used to group samples of roughly the same length together, which can help with training stability.
The trainer class used is from the trl library, which is a wrapper around the transformers library Trainer class. Additional to the standard training class, we'll pass in the peft_config and the dataset_text_field option. This allows us to save the model, which will save only the QLoRA adapter weights and the model configuration.
Here are the key training parameters:
With these settings, the training process can be monitored using Tensorboard, which shows a nice decrease in validation and training loss.
Take a look at this: Distributed Training Huggingface
Fine-Tuning Techniques
Fine-tuning an LLM can be a lengthy process requiring significant resources and expertise.
Fine-tuning involves training a model on your specific data, which can be time-consuming and resource-intensive, often involving huge GPUs.
To fine-tune an LLM, you'll need a high-quality dataset, ideally with labels for tasks like summarization or text extraction.
Fine-tuning can be done in a supervised manner, similar to training a traditional deep learning model, where you prepare the data, choose a model, fine-tune it, and evaluate the results.
However, fine-tuning an instruction-tuned model requires a dataset of instructions, which adds an extra layer of complexity.
Fine-tuning is often the most effective and versatile option for addressing issues like high cost, high latency, and hallucinations associated with prompt-based approaches.
Fine-tuning can reduce the number of tokens required to generate a response, resulting in lower costs and latency, but it may require expertise in data handling, training, and inference techniques.
To determine whether fine-tuning is suitable for your needs, consider factors like compute power (GPUs), time and expertise, and high-quality data with labels.
Here are some key factors to consider when deciding whether to fine-tune an LLM:
Fine-tuning can be a powerful solution for achieving optimal results, but it's essential to carefully consider the resources and expertise required.
Evaluation and Cost
Fine-tuning Llama 2 with Hugging Face can significantly improve the model's performance.
The evaluation process involves generating prompts using the generate_prompt function, which helps to assess the model's ability to create concise and informative summaries.
A key benefit of fine-tuning is that it can produce summaries that are much shorter and more to the point, as seen in the example where the fine-tuned model produces a "pretty much perfect" summary.
Additional reading: Fine-tuning vs Transfer Learning
Evaluation
In evaluation, it's crucial to have a clear understanding of what you're trying to measure.
The generate_prompt function is a useful tool for generating prompts for the model.
A good summary should be concise and to the point.
The first example from the test set was a long and rambling summary.
A fine-tuned model can produce much better results than the original.
The third example was still a bit long, but the fine-tuned model made it even shorter.
A good summary should express the main idea of the conversation.
The fourth example was a great summary, short and to the point.
Recommended read: Is Huggingface Transformers Good
What Makes Our Llama Expensive?
Our Llama fine-tuning is expensive due to the memory it requires to store the weights and gradients of the model. With 2 bytes for the weight and 2 bytes for the gradient, that's 4 bytes right there.
The Adam optimizer states take up even more space, requiring 4 + 8 bytes. This adds up to a total of 16 bytes per trainable parameter.
To put this into perspective, a single fine-tuning process can use up to 112GB of memory, excluding the intermediate hidden states. This is a huge amount of memory, especially considering the largest GPU available today can only handle up to 80GB of GPU VRAM.
Explore further: Finetune a Model
Sources
- https://www.acorn.io/resources/learning-center/fine-tuning-llama-2
- https://www.mlexpert.io/blog/fine-tuning-llama-2-on-custom-dataset
- https://blog.futuresmart.ai/integrating-llama-2-with-hugging-face-and-langchain
- https://duarteocarmo.com/blog/fine-tune-llama-2-telegram
- https://pytorch.org/blog/finetune-llms/
Featured Images: pexels.com