To integrate a cosine learning rate scheduler into SFTConfig, you need to set the lr_scheduler_type parameter to "cosine". This works similarly to how you would set it in TRL's SFTConfig as it inherits from Hugging Face's TrainingArguments[5][6].
Here's how to implement it:
from trl import SFTConfig, SFTTrainer
# Create SFTConfig with cosine scheduler
training_args = SFTConfig(
output_dir="output_directory",
learning_rate=1e-4, # Your specified learning rate
lr_scheduler_type="cosine", # Set cosine scheduler here
warmup_ratio=0.1, # Optional: 10% of steps for warmup
# Other training arguments
per_device_train_batch_size=4,
gradient_accumulation_steps=1,
num_train_epochs=3,
# ... other parameters as needed
)
# Create the trainer with this config
trainer = SFTTrainer(
model="your-model",
train_dataset=your_dataset,
args=training_args,
# ... other trainer parameters
)
trainer.train()
If you want to use cosine with restarts instead, you can set:
training_args = SFTConfig(
# ... other parameters
lr_scheduler_type="cosine_with_restarts",
# ... other parameters
)
Key parameters for controlling the scheduler:
warmup_ratio: Percentage of training steps to use for warmup (alternative to warmup_steps)
warmup_steps: Absolute number of steps for warmup
num_train_epochs or max_steps: Determines the total length of training (and thus the decay period)
The SFTConfig class inherits all scheduler functionality from Hugging Face's TrainingArguments, so all the standard scheduler options are available[3][5].