feat: Add Multi-Token Prediction (MTP) support to SFTTrainer #4290
+2,796
−13
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📝 Description
This PR adds Multi-Token Prediction (MTP) support to the TRL library, based on Meta AI's paper Better & Faster Large Language Models via Multi-token Prediction and other baseline works.
MTP extends the standard next-token prediction by training additional heads to forecast multiple future tokens in parallel, providing richer training signals and improving both training efficiency and inference speed.
🔬 Connection to Reinforcement Learning
MTP has an interesting duality with world models in reinforcement learning. The additional prediction heads essentially learn to forecast future observations, similar to how world models predict future states. This conceptual similarity suggests that MTP could potentially serve as a key component in imagination-based RL algorithms, such as Dreamer, which learns behaviors by imagining trajectories in a learned latent space.
Just as world models enable agents to "dream" about possible futures before taking actions, MTP heads learn to predict multiple future tokens, effectively building an implicit model of the sequential dependencies in language. This parallel opens up exciting possibilities:
🎯 Key Features
📦 Main Components
trl/models/modeling_mtp_extension.py- Core MTP implementation with 5 head typestrl/trainer/mtp_data_collator.py- Data collator for multi-token targetstrl/trainer/sft_config.py- MTP configuration parameterstrl/trainer/sft_trainer.py- SFTTrainer integrationdocs/source/mtp_trainer.md- Complete documentation with examplesexamples/scripts/sft_with_mtp.py- Comprehensive example scripttests/test_mtp_functionality.py- Full test suite💡 Usage Example