Spaces:
Running
Running
Delete finetuned_model.py
#1
by AnilNiraula - opened
- finetuned_model.py +0 -65
finetuned_model.py
DELETED
|
@@ -1,65 +0,0 @@
|
|
| 1 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments, BitsAndBytesConfig
|
| 2 |
-
from peft import LoraConfig, get_peft_model
|
| 3 |
-
from trl import SFTTrainer
|
| 4 |
-
from datasets import load_dataset
|
| 5 |
-
import torch
|
| 6 |
-
|
| 7 |
-
# Load SmolLM-135M-Instruct model with 4-bit quantization
|
| 8 |
-
model_name = "HuggingFaceTB/SmolLM-135M-Instruct"
|
| 9 |
-
quantization_config = BitsAndBytesConfig(
|
| 10 |
-
load_in_4bit=True,
|
| 11 |
-
bnb_4bit_quant_type="nf4",
|
| 12 |
-
bnb_4bit_compute_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 13 |
-
)
|
| 14 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 15 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 16 |
-
model_name,
|
| 17 |
-
quantization_config=quantization_config,
|
| 18 |
-
device_map="auto",
|
| 19 |
-
)
|
| 20 |
-
|
| 21 |
-
# Prepare PEFT config for efficient fine-tuning
|
| 22 |
-
peft_config = LoraConfig(
|
| 23 |
-
r=16,
|
| 24 |
-
lora_alpha=32,
|
| 25 |
-
target_modules=["q_proj", "v_proj", "k_proj", "o_proj"],
|
| 26 |
-
lora_dropout=0.05,
|
| 27 |
-
bias="none",
|
| 28 |
-
task_type="CAUSAL_LM"
|
| 29 |
-
)
|
| 30 |
-
model = get_peft_model(model, peft_config)
|
| 31 |
-
|
| 32 |
-
# Load dataset (example: assume 'financial_data.jsonl' with {'text': 'query ||| response'} format; incorporate system prompt in data prep if needed)
|
| 33 |
-
dataset = load_dataset("json", data_files="financial_data.jsonl", split="train")
|
| 34 |
-
|
| 35 |
-
# Training arguments (adjusted for smaller model: larger batch size for speed)
|
| 36 |
-
training_args = TrainingArguments(
|
| 37 |
-
output_dir="./finetuned_smollm135m",
|
| 38 |
-
num_train_epochs=3,
|
| 39 |
-
per_device_train_batch_size=8, # Increased for smaller model
|
| 40 |
-
gradient_accumulation_steps=2,
|
| 41 |
-
learning_rate=2e-4,
|
| 42 |
-
fp16=True if torch.cuda.is_available() else False,
|
| 43 |
-
save_steps=500,
|
| 44 |
-
logging_steps=100,
|
| 45 |
-
optim="paged_adamw_8bit",
|
| 46 |
-
weight_decay=0.01,
|
| 47 |
-
warmup_steps=100,
|
| 48 |
-
)
|
| 49 |
-
|
| 50 |
-
# Trainer
|
| 51 |
-
trainer = SFTTrainer(
|
| 52 |
-
model=model,
|
| 53 |
-
args=training_args,
|
| 54 |
-
train_dataset=dataset,
|
| 55 |
-
peft_config=peft_config,
|
| 56 |
-
dataset_text_field="text", # Adjust based on your dataset
|
| 57 |
-
tokenizer=tokenizer,
|
| 58 |
-
max_seq_length=512,
|
| 59 |
-
)
|
| 60 |
-
|
| 61 |
-
trainer.train()
|
| 62 |
-
|
| 63 |
-
# Save fine-tuned model
|
| 64 |
-
trainer.model.save_pretrained("./finetuned_smollm135m")
|
| 65 |
-
tokenizer.save_pretrained("./finetuned_smollm135m")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|