Downloads · 30 days
0
HadeelHegazi/lora_model_num_train_epochs1max_steps120
lora_model_num_train_epochs1max_steps120 is a machine learning model from HadeelHegazi. Use it for the machine learning task on the model card, and read the license before you ship it in a product.
This repository contains code for fine-tuning a language model using Unsloth and Xformers. The code is designed to facilitate efficient model training and inference on large datasets, with a focus on optimizing memory…
Downloads · 30 days
0
Access
Public
Updated Sep 1, 2024
Repo size
168 MB
Likes
0
Public
Click a slice to open those files.
.safetensors168 MB · 95%
From the Hugging Face model README
This repository contains code for fine-tuning a language model using Unsloth and Xformers. The code is designed to facilitate efficient model training and inference on large datasets, with a focus on optimizing memory usage and training speed.
This script demonstrates the process of installing necessary packages, loading a pre-trained model from the Unsloth repository, and fine-tuning the model using a specific dataset. The model used in this example is "Meta-Llama-3.1-8B," which has been quantized to 4-bit precision to reduce memory usage and improve training efficiency.
The following packages are required for the fine-tuning process:
unsloth: A library for fast language model fine-tuning.xformers: A library that provides efficient attention mechanisms, including Flash Attention.trl, peft, accelerate, bitsandbytes: Libraries for training large models.To install these packages, use the following commands:
!pip install "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
!pip install --no-deps "xformers<0.0.27" "trl<0.9.0" peft accelerate bitsandbytes
!pip uninstall xformers
!pip install xformers
!python -m xformers.info
!pip install triton
The model configuration includes setting parameters for maximum sequence length, data type detection, and 4-bit quantization. The model is loaded from the Unsloth repository and configured with LoRA (Low-Rank Adaptation) for fine-tuning.
from unsloth import FastLanguageModel
import torch
max_seq_length = 2048
dtype = None # Auto detection
load_in_4bit = True # Use 4bit quantization
model, tokenizer = FastLanguageModel.from_pretrained(
model_name="unsloth/Meta-Llama-3.1-8B",
max_seq_length=max_seq_length,
dtype=dtype,
load_in_4bit=load_in_4bit,
)
The dataset used for fine-tuning is the Alpaca dataset, which is formatted to include instructions, inputs, and expected responses. The dataset is processed to fit the model’s input requirements.
from datasets import load_dataset
alpaca_prompt = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
### Instruction:
{}
### Input:
{}
### Response:
{}"""
dataset = load_dataset("yahma/alpaca-cleaned", split="train")
The model is fine-tuned using the SFTTrainer from the trl library. Key training parameters include batch size, learning rate, and the number of training steps.
from trl import SFTTrainer
from transformers import TrainingArguments
trainer = SFTTrainer(
model=model,
tokenizer=tokenizer,
train_dataset=dataset,
args=TrainingArguments(
per_device_train_batch_size=2,
gradient_accumulation_steps=4,
max_steps=120,
learning_rate=2e-4,
output_dir="outputs",
),
)
trainer.train()
Before and after training, the model is tested using predefined prompts to compare its performance. The outputs demonstrate the model's ability to generate responses based on the given instructions and inputs.
# Sample prompt for testing before training
inputs = tokenizer(
[
alpaca_prompt.format(
"Based on the information provided, rewrite the sentence by changing its tense from past to future.",
"She played the piano beautifully for hours and then stopped as it was midnight.",
"",
)
], return_tensors="pt"
).to("cuda")
outputs = model.generate(**inputs, max_new_tokens=128)
# Sample prompt for testing after training
outputs = model.generate(**inputs, max_new_tokens=128)
The fine-tuned model can be saved locally or pushed to the Hugging Face Hub for public access.
model.push_to_hub("HadeelHegazi/lora_model_num_train_epochs1max_steps120", token="hf_your_token")
tokenizer.push_to_hub("HadeelHegazi/lora_model_num_train_epochs1max_steps120", token="hf_your_token")
torch.cuda.empty_cache() to free up memory.PYTORCH_CUDA_ALLOC_CONF: Set the environment variable PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True to manage memory fragmentation. This can be done in your script or in your environment setup.torch.cuda.amp) to reduce memory usage.pip uninstall xformers
pip install xformers --no-cache-dir
python -m xformers.info
This script provides a comprehensive guide to fine-tuning a large language model using Unsloth and Xformers. The included code can be easily adapted to different datasets and models for various NLP tasks.
base_model: unsloth/meta-llama-3.1-8b-bnb-4bit language:
This llama model was trained 2x faster with Unsloth and Huggingface's TRL library.