Aller au contenu principal

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ètreObjectifValeur typique
max_seq_lengthLongueur maximale de la séquence512-2048
packingRegrouper plusieurs exemplesFalse (plus simple)
dataset_num_procTravailleurs de prétraitement4-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",
...
)

Continuer vers le Chapitre 3 →