Spaces:
Runtime error
Runtime error
Update fine_tune.py
Browse files- fine_tune.py +94 -94
fine_tune.py
CHANGED
|
@@ -1,95 +1,95 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
from
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
print("
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
#
|
| 30 |
-
model
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
tokenizer.
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"],
|
| 54 |
-
)
|
| 55 |
-
print("LoRA configured.")
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
#
|
| 59 |
-
training_arguments = TrainingArguments(
|
| 60 |
-
output_dir=output_dir,
|
| 61 |
-
num_train_epochs=1,
|
| 62 |
-
per_device_train_batch_size=2,
|
| 63 |
-
gradient_accumulation_steps=2,
|
| 64 |
-
optim="paged_adamw_32bit",
|
| 65 |
-
logging_steps=10,
|
| 66 |
-
learning_rate=2e-4,
|
| 67 |
-
weight_decay=0.01,
|
| 68 |
-
fp16=True,
|
| 69 |
-
max_grad_norm=0.3,
|
| 70 |
-
max_steps=-1,
|
| 71 |
-
warmup_ratio=0.03,
|
| 72 |
-
group_by_length=True,
|
| 73 |
-
lr_scheduler_type="linear",
|
| 74 |
-
)
|
| 75 |
-
print("Training arguments set.")
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
#
|
| 79 |
-
trainer = SFTTrainer(
|
| 80 |
-
model=model,
|
| 81 |
-
train_dataset=dataset,
|
| 82 |
-
peft_config=peft_config,
|
| 83 |
-
dataset_text_field="text",
|
| 84 |
-
max_seq_length=2048,
|
| 85 |
-
tokenizer=tokenizer,
|
| 86 |
-
args=training_arguments,
|
| 87 |
-
)
|
| 88 |
-
print("Trainer initialized. Starting the fine-tuning process...")
|
| 89 |
-
trainer.train()
|
| 90 |
-
print("Training complete.")
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
#
|
| 94 |
-
trainer.model.save_pretrained(output_dir)
|
| 95 |
print(f"Fine-tuned model adapter saved to {output_dir}")
|
|
|
|
| 1 |
+
|
| 2 |
+
# fine_tuning/fine_tune.py
|
| 3 |
+
|
| 4 |
+
import os
|
| 5 |
+
import torch
|
| 6 |
+
from datasets import load_dataset
|
| 7 |
+
from transformers import (
|
| 8 |
+
AutoModelForCausalLM,
|
| 9 |
+
AutoTokenizer,
|
| 10 |
+
TrainingArguments,
|
| 11 |
+
BitsAndBytesConfig
|
| 12 |
+
)
|
| 13 |
+
from peft import LoraConfig
|
| 14 |
+
from trl import SFTTrainer
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
base_model_name = "unsloth/llama-3-8b-Instruct-bnb-4bit"
|
| 18 |
+
output_dir = "fine_tuning/results/llama-3-8b-instruct-direct-ed"
|
| 19 |
+
|
| 20 |
+
dataset_path = "dataset.jsonl"
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
# Load the Dataset
|
| 24 |
+
print("Loading dataset...")
|
| 25 |
+
dataset = load_dataset("json", data_files=dataset_path, split="train")
|
| 26 |
+
print("Dataset loaded successfully.")
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
# Load the Base Model & Tokenizer
|
| 30 |
+
print(f"Loading base model: {base_model_name}...")
|
| 31 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 32 |
+
base_model_name,
|
| 33 |
+
device_map="auto",
|
| 34 |
+
trust_remote_code=True,
|
| 35 |
+
)
|
| 36 |
+
model.config.use_cache = False
|
| 37 |
+
print("Base model loaded successfully.")
|
| 38 |
+
|
| 39 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model_name, trust_remote_code=True)
|
| 40 |
+
if tokenizer.pad_token is None:
|
| 41 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 42 |
+
tokenizer.padding_side = "right"
|
| 43 |
+
print("Tokenizer loaded and configured.")
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
# Configure PEFT (LoRA)
|
| 47 |
+
peft_config = LoraConfig(
|
| 48 |
+
lora_alpha=16,
|
| 49 |
+
lora_dropout=0.1,
|
| 50 |
+
r=16,
|
| 51 |
+
bias="none",
|
| 52 |
+
task_type="CAUSAL_LM",
|
| 53 |
+
target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"],
|
| 54 |
+
)
|
| 55 |
+
print("LoRA configured.")
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
# Define Training Arguments
|
| 59 |
+
training_arguments = TrainingArguments(
|
| 60 |
+
output_dir=output_dir,
|
| 61 |
+
num_train_epochs=1,
|
| 62 |
+
per_device_train_batch_size=2,
|
| 63 |
+
gradient_accumulation_steps=2,
|
| 64 |
+
optim="paged_adamw_32bit",
|
| 65 |
+
logging_steps=10,
|
| 66 |
+
learning_rate=2e-4,
|
| 67 |
+
weight_decay=0.01,
|
| 68 |
+
fp16=True,
|
| 69 |
+
max_grad_norm=0.3,
|
| 70 |
+
max_steps=-1,
|
| 71 |
+
warmup_ratio=0.03,
|
| 72 |
+
group_by_length=True,
|
| 73 |
+
lr_scheduler_type="linear",
|
| 74 |
+
)
|
| 75 |
+
print("Training arguments set.")
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
# Initialize and Start Training
|
| 79 |
+
trainer = SFTTrainer(
|
| 80 |
+
model=model,
|
| 81 |
+
train_dataset=dataset,
|
| 82 |
+
peft_config=peft_config,
|
| 83 |
+
dataset_text_field="text",
|
| 84 |
+
max_seq_length=2048,
|
| 85 |
+
tokenizer=tokenizer,
|
| 86 |
+
args=training_arguments,
|
| 87 |
+
)
|
| 88 |
+
print("Trainer initialized. Starting the fine-tuning process...")
|
| 89 |
+
trainer.train()
|
| 90 |
+
print("Training complete.")
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
# Save the Final Model
|
| 94 |
+
trainer.model.save_pretrained(output_dir)
|
| 95 |
print(f"Fine-tuned model adapter saved to {output_dir}")
|