fromsafetune.hardenimportConstrainedSFTTrainer,ConstrainedSFTConfigconfig=ConstrainedSFTConfig(output_dir="csft_out",csft_beta=0.5,csft_decay_rate=0.1)trainer=ConstrainedSFTTrainer(model=model,args=config,train_dataset=task_ds,reference_model=ref_model,# frozen aligned model, before fine-tuning)trainer.train()
The runner wrapper safetune.runner.harden.ConstrainedSFTTrainer runs this with
csft_beta / csft_decay_rate at their config defaults and no reference model,
so it degrades to plain SFT. Use the safetune.harden API above to supply
reference_model and enable the KL constraint.
Best for: a lightweight KL-regularized SFT that penalizes first-token drift from the aligned model.
Trade-offs: Uses a KL-regularized SFT with a position-decaying first-token penalty rather than the paper's bounded-DPO Eq. 3 + step-function β schedule; trains cleanly but cite the implementation, not the paper name.
@article{constrainedsft2024,title={Safety Alignment Should Be Made More Than Just a Few Tokens Deep},author={Qi, et al.},year={2024},note={ICLR 2025, arXiv:2406.05946},}