Spaces:
Runtime error
Runtime error
Create train.py
Browse files
train.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import torch
|
| 3 |
+
from datasets import load_dataset
|
| 4 |
+
from transformers import (
|
| 5 |
+
AutoModelForCausalLM,
|
| 6 |
+
AutoTokenizer,
|
| 7 |
+
BitsAndBytesConfig,
|
| 8 |
+
TrainingArguments,
|
| 9 |
+
pipeline,
|
| 10 |
+
)
|
| 11 |
+
from peft import LoraConfig, PeftModel
|
| 12 |
+
from trl import SFTTrainer
|
| 13 |
+
|
| 14 |
+
# Model to fine-tune - you can change this to any of the models you want to train
|
| 15 |
+
# 'meta-llama/Meta-Llama-3-70B-Instruct'
|
| 16 |
+
# 'meta-llama/Llama-3.3-70B-Instruct'
|
| 17 |
+
# 'meta-llama/Meta-Llama-3-8B-Instruct'
|
| 18 |
+
base_model = "meta-llama/Meta-Llama-3-8B-Instruct"
|
| 19 |
+
new_model = "llama-3-8b-custom" # A name for your fine-tuned model
|
| 20 |
+
|
| 21 |
+
# Load the datasets
|
| 22 |
+
# Make sure your CSVs are in the same directory as this script
|
| 23 |
+
dataset = load_dataset('csv', data_files=['data_training.csv', 'data_training_1.csv'], split="train")
|
| 24 |
+
|
| 25 |
+
# 4-bit quantization configuration
|
| 26 |
+
compute_dtype = getattr(torch, "float16")
|
| 27 |
+
quant_config = BitsAndBytesConfig(
|
| 28 |
+
load_in_4bit=True,
|
| 29 |
+
bnb_4bit_quant_type="nf4",
|
| 30 |
+
bnb_4bit_compute_dtype=compute_dtype,
|
| 31 |
+
bnb_4bit_use_double_quant=False,
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# Load the base model
|
| 35 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 36 |
+
base_model,
|
| 37 |
+
quantization_config=quant_config,
|
| 38 |
+
device_map={"": 0},
|
| 39 |
+
token=os.environ.get("HF_TOKEN") # Get token from secrets
|
| 40 |
+
)
|
| 41 |
+
model.config.use_cache = False
|
| 42 |
+
model.config.pretraining_tp = 1
|
| 43 |
+
|
| 44 |
+
# Load tokenizer
|
| 45 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model, trust_remote_code=True, token=os.environ.get("HF_TOKEN"))
|
| 46 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 47 |
+
tokenizer.padding_side = "right"
|
| 48 |
+
|
| 49 |
+
# PEFT configuration for LoRA
|
| 50 |
+
peft_params = LoraConfig(
|
| 51 |
+
lora_alpha=16,
|
| 52 |
+
lora_dropout=0.1,
|
| 53 |
+
r=64,
|
| 54 |
+
bias="none",
|
| 55 |
+
task_type="CAUSAL_LM",
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
# Training parameters
|
| 59 |
+
training_params = TrainingArguments(
|
| 60 |
+
output_dir="./results",
|
| 61 |
+
num_train_epochs=1,
|
| 62 |
+
per_device_train_batch_size=4,
|
| 63 |
+
gradient_accumulation_steps=1,
|
| 64 |
+
optim="paged_adamw_32bit",
|
| 65 |
+
save_steps=25,
|
| 66 |
+
logging_steps=25,
|
| 67 |
+
learning_rate=2e-4,
|
| 68 |
+
weight_decay=0.001,
|
| 69 |
+
fp16=False,
|
| 70 |
+
bf16=False,
|
| 71 |
+
max_grad_norm=0.3,
|
| 72 |
+
max_steps=-1,
|
| 73 |
+
warmup_ratio=0.03,
|
| 74 |
+
group_by_length=True,
|
| 75 |
+
lr_scheduler_type="constant",
|
| 76 |
+
report_to="tensorboard"
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
# Create the trainer
|
| 80 |
+
trainer = SFTTrainer(
|
| 81 |
+
model=model,
|
| 82 |
+
train_dataset=dataset,
|
| 83 |
+
peft_config=peft_params,
|
| 84 |
+
dataset_text_field="text", # IMPORTANT: Change "text" to the name of the column in your CSV that contains the training data
|
| 85 |
+
max_seq_length=None,
|
| 86 |
+
tokenizer=tokenizer,
|
| 87 |
+
args=training_params,
|
| 88 |
+
packing=False,
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
# Train the model
|
| 92 |
+
trainer.train()
|
| 93 |
+
|
| 94 |
+
# Save the fine-tuned model
|
| 95 |
+
trainer.model.save_pretrained(new_model)
|