Tuning Large Language Models for Real-World ApplicationsChapter 56
Step 6: Configure Training
Section 6 of 13-~ 2 min read-Synced from Cuantum content
from transformers import TrainingArgumentsfrom trl import SFTTrainer training_args = TrainingArguments( output_dir="outputs/ch2_domain_mistral", num_train_epochs=3, per_device_train_batch_size=2, gradient_accumulation_steps=8, learning_rate=2e-4, warmup_ratio=0.03, logging_steps=20, save_strategy="epoch", fp16=True, report_to="none") trainer = SFTTrainer( model=model, train_dataset=dataset, dataset_text_field="text", tokenizer=tokenizer, max_seq_length=1024, packing=True, args=training_args)Code Breakdown
- What happens in this step
- You define the training settings (
TrainingArguments). - You create an
SFTTrainerthat knows how to fine-tune your LoRA-wrapped model on your dataset. - Training does not start until Step 7 (
trainer.train()). - TrainingArguments: the key knobs
per_device_train_batch_size=2sets the batch size per GPU.gradient_accumulation_steps=8simulates a larger batch by accumulating gradients. Effective batch size is about2 × 8 = 16.learning_rate=2e-4is a common starting point for LoRA/QLoRA adapter training.warmup_ratio=0.03warms up the learning rate at the beginning for stability.fp16=Trueenables mixed precision to reduce memory and speed up training.save_strategy="epoch"saves a checkpoint after each epoch.output_dir=...is where outputs and checkpoints go.report_to="none"keeps the run simple (no external tracking).- SFTTrainer: connecting model + data
model=modelshould be the model after Step 4 (LoRA adapters attached).train_dataset=datasetis what you loaded in Step 5.dataset_text_field="text"tells the trainer which column contains the prompt string.max_seq_length=1024truncates or pads sequences to this max length.packing=Truepacks multiple short examples into one sequence for better GPU utilization.
At this point, everything is configured. Next, Step 7 runs the actual training loop.