Downloads · 30 days
0
Mayank022/Llama2_Finetuned_on_raw_text
Llama2_Finetuned_on_raw_text is a machine learning model from Mayank022. Use it for the machine learning task on the model card, and read the license before you ship it in a product. It is set up for transformers.
This project demonstrates how to fine-tune the LLaMA 2 7B model on a custom raw text dataset using Unsloth, LoRA, and 4-bit Quantization (QLoRA) techniques. We use a modular approach that can be adapted to any other d…
Downloads · 30 days
0
Access
Public
Updated Mar 30, 2025
Repo size
80.5 MB
Likes
0
Public
Click a slice to open those files.
.safetensors80 MB · 99%
From the Hugging Face model README
This project demonstrates how to fine-tune the LLaMA 2 7B model on a custom raw text dataset using Unsloth, LoRA, and 4-bit Quantization (QLoRA) techniques. We use a modular approach that can be adapted to any other dataset.

We use two datasets in this tutorial:
Install the required libraries:
pip install peft accelerate bitsandbytes transformers datasets GPUtil
Key libraries:
transformers (HuggingFace)datasets (HuggingFace)bitsandbytes (for quantization)peft (Parameter-Efficient Fine-Tuning)GPUtil (to monitor GPU usage)We check GPU availability and set CUDA configurations:
import torch, GPUtil, os
GPUtil.showUtilization()
if torch.cuda.is_available():
print("✅ GPU Available")
else:
print("❌ Using CPU")
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_compute_dtype=torch.float16,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4",
)
model = AutoModelForCausalLM.from_pretrained(
"unsloth/llama-2-7b",
quantization_config=bnb_config,
device_map="auto"
)
tokenizer = AutoTokenizer.from_pretrained("unsloth/llama-2-7b")
from peft import prepare_model_for_kbit_training, LoraConfig, get_peft_model
model = prepare_model_for_kbit_training(model)
lora_config = LoraConfig(
r=8,
lora_alpha=16,
target_modules=["q_proj", "v_proj", "k_proj", "o_proj"],
lora_dropout=0.05,
bias="none",
task_type="CAUSAL_LM",
)
model = get_peft_model(model, lora_config)
Here we show how to use a simple text file (hawaiian_wildfire.txt) as your dataset.
from datasets import Dataset
with open("hawaiian_wildfire.txt", "r") as f:
data = f.read()
dataset = Dataset.from_dict({"text": [data]})
def tokenize(batch):
return tokenizer(batch["text"], truncation=True, padding="max_length", max_length=512)
tokenized_dataset = dataset.map(tokenize)
from transformers import TrainingArguments, Trainer
training_args = TrainingArguments(
output_dir="llama-custom-lora",
per_device_train_batch_size=1,
num_train_epochs=2,
logging_steps=10,
save_steps=100,
save_total_limit=2,
fp16=True,
optim="paged_adamw_8bit",
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=tokenized_dataset,
tokenizer=tokenizer,
)
trainer.train()
model.eval()
input_text = "The wildfire in Hawaii caused"
inputs = tokenizer(input_text, return_tensors="pt").to("cuda")
outputs = model.generate(**inputs, max_new_tokens=100)
print(tokenizer.decode(outputs[0]))
Instead of manual setup, you can use: