| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import trackio |
| import torch |
| from datasets import load_dataset |
| from peft import LoraConfig, prepare_model_for_kbit_training, get_peft_model |
| from trl import SFTTrainer, SFTConfig |
| from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig |
|
|
| |
| model_id = "mistralai/Mistral-3-14B-Reasoning-2512" |
|
|
| |
| print("π¦ Loading dataset...") |
| dataset = load_dataset("sakharamg/AviationQA", split="train") |
|
|
| |
| |
| print("βοΈ Subsampling dataset to 10,000 examples for efficiency...") |
| dataset = dataset.shuffle(seed=42).select(range(10000)) |
|
|
| |
| print("π Mapping dataset...") |
| def to_messages(example): |
| return { |
| "messages": [ |
| {"role": "user", "content": example["Question"]}, |
| {"role": "assistant", "content": example["Answer"]} |
| ] |
| } |
| dataset = dataset.map(to_messages, remove_columns=dataset.column_names) |
|
|
| |
| print("π Creating train/eval split...") |
| dataset_split = dataset.train_test_split(test_size=0.1, seed=42) |
| train_dataset = dataset_split["train"] |
| eval_dataset = dataset_split["test"] |
|
|
| |
| bnb_config = BitsAndBytesConfig( |
| load_in_4bit=True, |
| bnb_4bit_quant_type="nf4", |
| bnb_4bit_compute_dtype=torch.bfloat16, |
| bnb_4bit_use_double_quant=True, |
| ) |
|
|
| |
| print(f"π€ Loading model {model_id}...") |
| model = AutoModelForCausalLM.from_pretrained( |
| model_id, |
| quantization_config=bnb_config, |
| device_map="auto", |
| torch_dtype=torch.bfloat16, |
| attn_implementation="eager" |
| ) |
| model = prepare_model_for_kbit_training(model) |
|
|
| |
| tokenizer = AutoTokenizer.from_pretrained(model_id) |
| tokenizer.pad_token = tokenizer.eos_token |
| |
| if tokenizer.chat_template is None: |
| tokenizer.chat_template = "{% for message in messages %}{{'<|im_start|>' + message['role'] + '\n' + message['content'] + '<|im_end|>' + '\n'}}{% endfor %}{% if add_generation_prompt %}{{ '<|im_start|>assistant\n' }}{% endif %}" |
|
|
| |
| peft_config = LoraConfig( |
| r=16, |
| lora_alpha=32, |
| lora_dropout=0.05, |
| bias="none", |
| task_type="CAUSAL_LM", |
| target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"], |
| ) |
|
|
| |
| config = SFTConfig( |
| output_dir="Mistral-3-14B-AviationQA-SFT", |
| push_to_hub=True, |
| hub_model_id="sunkencity/Mistral-3-14B-AviationQA-SFT", |
| hub_strategy="every_save", |
| num_train_epochs=1, |
| per_device_train_batch_size=4, |
| gradient_accumulation_steps=4, |
| learning_rate=2e-4, |
| fp16=False, |
| bf16=True, |
| logging_steps=10, |
| save_strategy="steps", |
| save_steps=100, |
| eval_strategy="steps", |
| eval_steps=100, |
| report_to="trackio", |
| project="aviation-qa-tuning", |
| run_name="mistral-14b-sft-v1", |
| max_seq_length=2048, |
| dataset_kwargs={"add_special_tokens": False} |
| ) |
|
|
| |
| trainer = SFTTrainer( |
| model=model, |
| train_dataset=train_dataset, |
| eval_dataset=eval_dataset, |
| args=config, |
| peft_config=peft_config, |
| tokenizer=tokenizer, |
| ) |
|
|
| print("π Starting training...") |
| trainer.train() |
|
|
| print("πΎ Pushing to Hub...") |
| trainer.push_to_hub() |
|
|