Learn How I Fine-Tuned LLaMA-7B With Dolly V2 Dataset In A No-Code Method
Published: 2023-06-13 15:31:49

Fine-tuning pre-trained models is a crucial practice in NLP to optimize their performance for specific tasks. As a developer, I understand the importance of fine-tuning models to achieve better results in targeted applications.


![Fine Tuning](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8765vllrtl49yxug9tdl.png)

However, this process comes with its fair share of challenges. I have personally faced various difficulties that impeded the fine-tuning process. One significant obstacle was the complex setup required for fine-tuning LLMs, including managing memory limitations and dealing with expensive GPUs. Additionally, the lack of established conventions made navigating this process even more daunting.

LLaMA 7B, an impressive language model developed by Meta AI, holds immense potential across various applications. Yet, fine-tuning this model can be a daunting task, characterized by intricate setups, memory limitations, high GPU costs, and a lack of standardized practices.

In this blog, I will share my experience of overcoming these obstacles and showcase how Monster API enabled me to fine-tune LLaMA 7B with Dolly v2 Dataset with ease and at a fraction of the cost.

**But first, what is LLaMA?**

LLaMA (Large Language Model Meta AI) is a foundational Large Language Model (LLM) developed by Facebook AI Research (FAIR) for machine translation tasks. LLaMA is based on the Transformer architecture, which is a neural network architecture designed to handle sequential data such as text. 

LLaMA is available in several sizes (7B, 13B, 33B, and 65B parameters). LLaMA 7B is the smallest of them all with 7 Billion Parameters. 

The model is trained on a large corpus of multilingual data, allowing it to translate between many different language pairs. One of the key advantages of LLaMA is its flexibility - it can be trained on any type of conversational data and can be integrated with a variety of messaging platforms.

**Furthermore, What is Databricks Dolly V2 dataset?**

The Databricks Dolly V2 dataset, specifically the "data bricks-dolly-15k" corpus, is a collection of over 15,000 records created by Databricks employees. The purpose of this dataset is to enable LLMs, to demonstrate interactive and engaging conversational abilities like ChatGPT.

I'll be using this diverse and rich Databricks Dolly V2 dataset to finetune the LLaMA 7B model.

**What is Fine-Tuning an LLM and why is it so important?**

Language models such as LLaMA are trained on vast amounts of general language data to learn patterns, grammar, and contextual information. However, they may not perform optimally when applied directly to specific tasks or domains.

By fine-tuning, users can enhance the model's performance, making it - 

**1. More accurate**
**2. Context-aware**
**3. Aligned with the target application.**

Fine-tuning enables us to tailor the pre-trained models to specific tasks, effectively transferring their general language knowledge to the specialized task.

**To explain fine-tuning better, consider this example-** 

Imagine you're a travel app developer creating a chatbot to assist users in finding the best vacation destinations. 

![Travel App](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yu06f1117f16i8ynssl0.png)

To build an intelligent chatbot, you need a language model that not only comprehends natural language but also possesses knowledge about popular travel destinations, tourist attractions, and hotel recommendations.

Instead of training an LLM from scratch, which requires massive amounts of data and computational resources, you can start with a pre-trained model like LLaMA. However, since LLaMA hadn't been specifically trained for travel-related tasks, its responses might not accurately address travel queries.

_This is where fine-tuning comes into play!_

By fine-tuning LLaMA 7B on a dataset specifically curated for travel-related information, you can adapt the model to better understand and generate responses related to vacation planning. The fine-tuning process teaches the model the nuances of travel-related language and aligns its knowledge with the requirements of the travel app.

**Only if life was that easy!**

![Surprised](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qeszakhrhi2waxwg9dwq.png)

Fine-tuning comes with its fair share of challenges. Let me delve into these challenges and share my experiences.

1. **Complex Setups:** Firstly, complex setups can be a real headache. Fine-tuning requires configuring the right libraries, dependencies, and frameworks to ensure compatibility between the pre-trained model, the dataset, and the desired task. It can be time-consuming and frustrating, especially when dealing with different versions and compatibility issues.

2. **Memory Requirements:** Out-of-memory problems are another common obstacle. Fine-tuning large models, such as LLaMA, demands significant memory resources. Unfortunately, not all developers have access to high-end GPUs with ample memory capacity. This limitation often leads to crashing or freezing during the training process, forcing us to optimize code and experiment with batch sizes.
 
3. **GPU Costs:** Let's not forget about the cost of GPUs. GPUs are essential for accelerating deep learning tasks, but they are expensive. Fine-tuning models, especially over extended periods, can quickly rack up costs, making it a luxury that not all developers can afford. It's a constant balancing act between optimizing performance and managing the budget.

4. **Standardized Practices:** Lastly, the absence of standardized practices can be frustrating. Fine-tuning techniques, tools, and best practices are continuously evolving, making it challenging to find a consistent and reliable approach. As developers, we often have to navigate through a maze of documentation, forums, and trial and error to figure out the most effective fine-tuning strategies.

**In conclusion, fine-tuning is far from a walk in the park**.Complex setups, out-of-memory woes, GPU costs, and the absence of standardized practices make it a bumpy ride.

**The Silver Lining**

**How I used Monster API to help solve the challenges?**

[Monster API](https://monsterapi.ai/?utm_source=devto-llm-finetuning&utm_medium=referral) has simplified and effectively made the often intricate fine-tuning process straightforward and quick, reducing the complex setup to a simple, easy-to-follow UI native approach. I fine-tuned LLaMA 7B using Databricks Dolly 15k for 3 epochs using LoRA.**The best part? It cost me less than $20**. 

With just five easy steps, I was able to set up my fine-tuning task and achieve impressive results. Let's dive in!

1.**Select an LLM:** The first step was to choose an LLM from the available options, including popular models like Llama 7B, GPT-J 6B, or StableLM 7B. 

![Specify Job Name and Select an LLM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5d7dj1m7l9w8aoihkxxh.png)

2.**Select or Create a Task:** Next, I needed to define the task for fine-tuning the LLM. Monster API offered a range of pre-defined tasks such as "Instruction Fine-Tuning" or "Text Classification." However, I also had the flexibility to choose the "Other" option for custom tasks. This versatility allowed me to fine-tune the LLM according to my specific needs.

![Select or Create a Task](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ooja7bhuss5dgtdy4r0y.png)

3.**Select a HuggingFace Dataset:** To train the LLM effectively, I needed a high-quality dataset. Monster API seamlessly integrated with HuggingFace datasets, providing a vast selection to choose from. The platform even suggested applicable datasets based on my selected task. With just a few clicks, the chosen dataset was automatically formatted and ready for use. 

![Select a HuggingFace Dataset](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zrw8rv73kats6b28vq8i.png)

4.**Specify Hyper-parameters:** Monster API took care of most of the hyper-parameters by pre-filling them based on my selected LLM. However, I had the freedom to modify these parameters, such as epochs, learning rate, cutoff length, warmup steps, and more, to suit my specific requirements. This level of customization allowed me to fine-tune the LLM exactly as needed.

![Specify Hyper-parameters](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/75mpnosfsxor4qiyxolr.png)

5.**Review and Submit Job:** After setting up all the parameters, I clicked on "Next" and reached the summary page. This step was crucial as it allowed me to review all the settings to ensure everything was accurate. Once I confirmed the details, I submitted the job, and Monster API took care of the rest.

**That’s it! In 5 easy steps, my fine-tuning setup was complete.** 

I was impressed by how straightforward the process was, allowing me to focus more on my task rather than grappling with complex configurations. Monster API took care of optimizing the model and ensuring that it fits in the constraints of available GPU memory. Abstracting those complex environments and GPU setup away from me while allowing me to focus on fine-tuning aspects.

After successfully setting up my fine-tuning job using Monster API, I was able to view logs of how my job is performing. Thus giving me detailed insights.

**The Moment of Truth**

At the end of the fine-tuning process, I obtained a LoRA adapter file that served as a bridge between the fine-tuned LLM and the inference stage. 

_**Let's explore the results!**_

**The entire fine-tuning process, from start to finish, took approximately 8 hours, or precisely 505 minutes.** Considering the complexity of fine-tuning large language models, this was an impressive turnaround time.

To provide you with a visual representation of the results, I have attached relevant graphs from WandB Metrics to give you a better understanding. These graphs offer a comprehensive understanding of the performance and progress of the fine-tuning process.

1.**Eval Loss:** The evaluation loss curve graph illustrates the evaluation progress of the fine-tuned LLM over time. It showcases the decrease in loss values as the model learns and adapts to the dataset.

![Eval Curve](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jdympx837k7ri5avu01w.jpg)

2.**Learning Rate:** The learning rate graph provides information about the rate at which the model adjusts its parameters during training. It reveals the optimization process by demonstrating how the learning rate changes over time.

![Learning Rate](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3a20f0m5l5t9gbk0fjfn.jpg)

3.**GPU Power Usage:**  The GPU power usage graph depicts the utilization of the GPU's computing power throughout the fine-tuning process.

![GPU Power](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vdb0vqexmiy3nbnxtkho.jpg)

4.**GPU Time Spent Accessing Memory:** Memory access is a critical aspect that can affect performance. Monitoring and optimizing memory access aids in reducing bottlenecks and maximizing GPU efficiency. 

![GPU Time Spent](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j4or2a3f07wnjlorcvcq.jpg)

5. **GPU Temperature (°C):** The GPU temperature graph provides a visual representation of the GPU's thermal conditions during the fine-tuning process.

![GPU Temp](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/isuj79jkbh3y61974n0d.jpg)

These WandB Metrics graphs offer valuable insights into the fine-tuning process, allowing for a detailed analysis of various aspects such as loss, learning rate, GPU power usage, GPU memory access, GPU temperature, etc.

**Final Remarks**

As a developer who embarked on the journey of fine-tuning LLMs using Monster API, I can confidently say that the experience has been transformative.

With Monster API, the process of fine-tuning LLMs was streamlined and accessible. The platform offered an easy-to-use interface, allowing me to select an LLM, define my task, choose a dataset, and specify hyper-parameters. The step-by-step approach eliminated the guesswork and confusion, providing me with a clear path to follow.

One of the standout features of Monster API that I recently learned about was its decentralized GPU network. This capability greatly reduced the cost and complexity associated with accessing powerful computational resources.

In my own experience, I was able to fine-tune the LLaMA 7B model using the Databricks Dolly V2 dataset for three epochs, and **the entire process cost me less than $20**.

This level of affordability and accessibility opens up new possibilities for developers who previously faced barriers due to limited resources. 

**Conclusion**

My fine-tuned LLM exhibited enhanced performance, as it captured contextual understanding, maintained coherence, and generated more accurate responses.

In conclusion, my journey with Monster API in fine-tuning LLMs has been a rewarding experience. It has not only addressed challenges but also helped me as a developer to explore the full potential of these language models. 

By sharing this account, I hope to inspire other developers and provide insights into the possibilities and solutions available when working with [Monster API](https://monsterapi.ai/?utm_source=devto-llm-finetuning&utm_medium=referral).

**Resources:**

Monster API - [Monster API](https://monsterapi.ai/?utm_source=devto-llm-finetuning&utm_medium=referral)

Fine Tuning Documentation - [GitBooks](https://docs.monsterapi.ai/fine-tune-a-large-language-model-llm/launch-a-fine-tuning-job)

Databricks Dolly Dataset - [HuggingFace](https://huggingface.co/datasets/databricks/databricks-dolly-15k)

LLaMA Model - [HuggingFace](https://huggingface.co/decapoda-research/llama-7b-hf)

LLaMA GitHub Page - [GitHub](https://github.com/facebookresearch/llama)