readCtrl_lambda / code /finetune /qwen3-14B.py
mshahidul
Initial commit of readCtrl code without large models
030876e
import json
import os
import sys
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
from unsloth import FastLanguageModel
import torch
model, tokenizer = FastLanguageModel.from_pretrained(
model_name = "unsloth/Qwen3-4B",
max_seq_length = 8192, # Context length - can be longer, but uses more memory
load_in_4bit = False, # 4bit uses much less memory
load_in_8bit = False, # A bit more accurate, uses 2x memory
full_finetuning = False, # We have full finetuning now!
# token = "hf_...", # use one if using gated models
)
model = FastLanguageModel.get_peft_model(
model,
r = 32, # Choose any number > 0! Suggested 8, 16, 32, 64, 128
target_modules = ["q_proj", "k_proj", "v_proj", "o_proj",
"gate_proj", "up_proj", "down_proj",],
lora_alpha = 32, # Best to choose alpha = rank or rank*2
lora_dropout = 0, # Supports any, but = 0 is optimized
bias = "none", # Supports any, but = "none" is optimized
# [NEW] "unsloth" uses 30% less VRAM, fits 2x larger batch sizes!
use_gradient_checkpointing = "unsloth", # True or "unsloth" for very long context
random_state = 3407,
use_rslora = False, # We support rank stabilized LoRA
loftq_config = None, # And LoftQ
)
with open(f"/home/mshahidul/readctrl/data/finetuning_data/dataset_for_sft_support_check_list.json") as f:
data = json.load(f)
from datasets import Dataset
dataset = Dataset.from_list(data)
from unsloth.chat_templates import standardize_sharegpt
dataset = standardize_sharegpt(dataset)
def formatting_prompts_func(examples):
convos = examples["conversations"]
texts = [tokenizer.apply_chat_template(convo, tokenize = False, add_generation_prompt = False) for convo in convos]
return { "text" : texts, }
dataset = dataset.map(formatting_prompts_func, batched = True)
split_dataset = dataset.train_test_split(test_size = 0.1, seed = 3407, shuffle = True)
train_dataset = split_dataset["train"]
eval_dataset = split_dataset["test"]
from trl import SFTTrainer, SFTConfig
trainer = SFTTrainer(
model = model,
tokenizer = tokenizer,
train_dataset = train_dataset,
eval_dataset = eval_dataset,
args = SFTConfig(
dataset_text_field = "text",
per_device_train_batch_size = 8,
gradient_accumulation_steps = 2, # Use GA to mimic batch size!
warmup_steps = 5,
num_train_epochs = 3, # Set this for 1 full training run.
# max_steps = 30,
learning_rate = 2e-4, # Reduce to 2e-5 for long training runs
logging_steps = 1,
per_device_eval_batch_size = 8,
bf16 = True,
tf32 = True,
optim = "adamw_8bit",
weight_decay = 0.01,
lr_scheduler_type = "linear",
seed = 3407,
report_to = "none", # Use this for WandB etc
),
)
trainer_stats = trainer.train()
save_dir = "/home/mshahidul/readctrl_model/support_checking_vllm/qwen3-4b"
os.makedirs(save_dir, exist_ok=True)
# Export merged model weights in FP16 format.
model.save_pretrained_merged(
save_dir,
tokenizer,
save_method = "merged_16bit",
)
tokenizer.save_pretrained(save_dir)
eval_metrics = trainer.evaluate()
print(f"Eval metrics: {eval_metrics}")
# model.push_to_hub(f"Translation_Evaluator_Qwen3_14B_v1", )
# tokenizer.push_to_hub(f"Translation_Evaluator_Qwen3_14B_v1")
# print(f"Model pushed to Hugging Face Hub")