Chapitre 2 : TRL et SFTTrainer
Formation simplifiée pour les LLM
TRL (Transformer Reinforcement Learning) fournit des entraîneurs spécialisés pour le fine-tuning.
Notions de base de SFTTrainer
from trl import SFTTrainer
from transformers import TrainingArguments
training_args = TrainingArguments(
output_dir="./output",
num_train_epochs=3,
per_device_train_batch_size=4,
gradient_accumulation_steps=4,
learning_rate=2e-4,
bf16=True,
gradient_checkpointing=True,
logging_steps=10,
save_strategy="epoch",
)
trainer = SFTTrainer(
model=model,
args=training_args,
train_dataset=dataset,
tokenizer=tokenizer,
dataset_text_field="text", # or use formatting_func
max_seq_length=512,
packing=False,
)
trainer.train()
Formatage du jeu de données
Option 1 : Champ de texte
# Dataset has "text" column
trainer = SFTTrainer(
dataset_text_field="text",
...
)
Option 2 : Fonction de formatage
def formatting_func(example):
return f"### Instruction:\n{example['instruction']}\n\n### Response:\n{example['response']}"
trainer = SFTTrainer(
formatting_func=formatting_func,
...
)
Paramètres importants
| Paramètre | Objectif | Valeur typique |
|---|---|---|
max_seq_length | Longueur maximale de la séquence | 512-2048 |
packing | Regrouper plusieurs exemples | False (plus simple) |
dataset_num_proc | Travailleurs de prétraitement | 4-8 |
Rappels et journalisation
from transformers import EarlyStoppingCallback
trainer = SFTTrainer(
callbacks=[EarlyStoppingCallback(early_stopping_patience=3)],
...
)
# Pour la journalisation W&B
training_args = TrainingArguments(
report_to="wandb",
...
)