Fine-tuning LLama3 Models on Bagel
This tutorial will guide you through the steps to fine-tune LLaMA3 models on Bagel. We will start by creating a raw asset, downloading a dataset, purchasing a LLaMA3 model from the Bakery marketplace, and finally, fine-tuning the model.
Prerequisites
Create Bakery Account from here
Get user_id & api_key from bakery.
Google Colab (Chrome Browser).
Installation
To install the Bagel Python client, run the following commands in your terminal:
Step 1: Bakery Setup
This initializes the Bagel client, which will be used to interact with the Bagel server throughout the tutorial.
Here, we'll prompt the user to enter their API key securely (without displaying it on the screen) and then sets it as an environment variable named BAGEL_API_KEY
.
Step 2: Create Asset on Bagel
This code defines a function create_asset that creates a new dataset /asset using the bagel client and returns the resulting asset ID after creation.
You can also retrieve asset info by adding the following
Step 3: Upload asset file in google colab
This code uploads files from your local machine to the Google Colab environment . Please enusre you're using Google Chrome browser.
You can upload any text, CSV, JSON or Parquet format file. Generally, parquet format is widely used for LLama3 model fine-tuning. You can generate your own parquet format data from here
For printing the name and size in bytes of each file, please use the code below:
Step 4: Upload Dataset in Bakery
This code uploads the dataset of your choice to your created asset using the bagel client, and then prints the response from the upload operation.
Step 5: Purchase a LLaMA3 Model from Bakery
To fine-tune a Llama3 model, you first need to purchase it from the Bakery marketplace:
Log in to your Bagel account and navigate to the Model tab on your Bakery homepage.
Browse the available models and purchase the Llama3-8b model for this example.
It is a large language model designed for advanced text generation and dialogue applications. It is part of the Meta Llama 3 family, which includes both an 8 billion parameter and a 70 billion parameter version. This model utilizes an optimized transformer architecture and is fine-tuned with supervised learning and reinforcement learning with human feedback to enhance its helpfulness and safety.
After purchasing, the model will be available under the My Models section.
You can also use your CLI to purchase assets by running this code:
Buy LLama3 Model From Bakery Marketplace
Step 6: Fine-tune the LLama3 Model on Bakery
Now that you have your dataset and model, you can fine-tune the LLaMA3 model using the following code:
This will initiate the finetuning process and print asset id of the finetuned model.
Step 7: Check Job Status
This will print the job id of the finetuned model
Status of the Job can also be shown by using
Step 8: Download Fine-tuned Model from Bakery
Step 9: Load Model using transformers
This code loads the pre-trained language model with an adapter using a configuration that minimizes GPU memory usage by quantizing the model to 8-bit precision. It first loads the adapter configuration to determine the base model, then initializes the model with the specified quantization settings. The tokenizer is loaded separately, and the model is only loaded when needed, ensuring efficient resource management.
Step 10: ChatBot
Now lets use a generate_response
function to generate a relevant and concise reply based on the finetuned model. We'll be using specific generation parameters to ensure a relevant and concise reply.
Lets use a chatbot
function to initiate a simple text-based chatbot that interacts with the user in a loop, continuously prompting for user input and generating responses based on the input. It prints a welcome message, processes user input to generate a response using the generate_response function, and displays the chatbot's reply. The loop terminates when the user types "quit," ending the conversation and printing a farewell message.
Step 11: Clean GPU Memory
Step 12: Delete the Base Model & Fine-Tuned Model
Reference
LLama3 bot tutorial - Please refer to the google colab tutorial for interactive development
Need extra help or just want to connect with other developers? Join our community on Discord for additional support 👾
Last updated