Spaces:
Sleeping
Sleeping
Update finetuned_model.py
Browse files- finetuned_model.py +15 -10
finetuned_model.py
CHANGED
|
@@ -1,16 +1,21 @@
|
|
| 1 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments
|
| 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 model
|
| 8 |
-
model_name = "HuggingFaceTB/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
model_name,
|
|
|
|
| 12 |
device_map="auto",
|
| 13 |
-
torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,
|
| 14 |
)
|
| 15 |
|
| 16 |
# Prepare PEFT config for efficient fine-tuning
|
|
@@ -27,12 +32,12 @@ model = get_peft_model(model, peft_config)
|
|
| 27 |
# Load dataset (example: assume 'financial_data.jsonl' with {'text': 'query ||| response'} format)
|
| 28 |
dataset = load_dataset("json", data_files="financial_data.jsonl", split="train")
|
| 29 |
|
| 30 |
-
# Training arguments
|
| 31 |
training_args = TrainingArguments(
|
| 32 |
-
output_dir="./
|
| 33 |
num_train_epochs=3,
|
| 34 |
-
per_device_train_batch_size=
|
| 35 |
-
gradient_accumulation_steps=
|
| 36 |
learning_rate=2e-4,
|
| 37 |
fp16=True if torch.cuda.is_available() else False,
|
| 38 |
save_steps=500,
|
|
@@ -56,5 +61,5 @@ trainer = SFTTrainer(
|
|
| 56 |
trainer.train()
|
| 57 |
|
| 58 |
# Save fine-tuned model
|
| 59 |
-
trainer.model.save_pretrained("./
|
| 60 |
-
|
|
|
|
| 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
|
|
|
|
| 32 |
# Load dataset (example: assume 'financial_data.jsonl' with {'text': 'query ||| response'} format)
|
| 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,
|
|
|
|
| 61 |
trainer.train()
|
| 62 |
|
| 63 |
# Save fine-tuned model
|
| 64 |
+
trainer.model.save_pretrained("./finetuned_smollm135m")
|
| 65 |
+
tokenizer.save_pretrained("./finetuned_smollm135m")
|