-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Uncontaminated Sample Packing #3525
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
Follow up: DRY up attention code. We re-implement a big |
c07d6bd to
c23f676
Compare
|
I added support for passing position IDs to RoPE (needed for correctness, just like attention), and a (fused QK) triton kernel for the RoPE embedding (similar to what exists currently for the non-packing case). Benchmarks show we're competitive to the triton kernel for the non-packing case while numerical ~match and significantly beat the torch slow path: RoPE kernel benchmark sweep (microseconds per call)
|
|
I added helpers for attention backend selection / running that each of |
|
Tested and pushed a fix for xformers attention, this PR should be good to go now. One open question: should we make sample packing the default for pretrain / SFT workloads? It should always work and provides better throughput than without. It's a bit of a shift though; it reshapes samples to One option is just to reshape so samples have shape Another option is to strongly recommend using sample packing in a logged message on the command line (if not already enabled). We can also explore this in a follow up PR if we don't want to make a decision now. |
|
I added some utils and updated the CLI to work OOTB with DDP. Just use These utils should be reusable in our notebooks / scripts too! PS: DDP working relies on removing the |
Yes the goal is to allow the padding free collator then it auto gets a perf boost :) We can do this for the next PR if that helps I also fixed the torch.compile issue for CE (verifying now) |
|
disabled the |
This PR adds sample packing support. It uses TRL's SFTConfig
packing=Trueandpadding_free=Trueargs to pack the sequences, and we computepacked_seq_lengthsmetadata and thread it through the model forward pass. This metadata is used to create block causal masks for SDPA and xformers attention, and is passed to the flash attention varlen API which handles the block causal masking itself under the hood (we need to do this ourselves because of our custom forward pass, whereas TRL handles the sequence length metadata internally in their trainer).I added a few unit tests. I also wrote a quick bash script for smoke testing some common model architectures: gist, which runs.
Below is a comparison of short
unsloth/qwen2.5-0.5btraining runs. The losses don't match because we're seeing more / different samples on each step. But the scale and trend match, which is the important bit.Commands:
No sample packing:
Sample packing:
Note that we use
--per_device_train_batch_size 1in the latter case since we are packing multiple examples into a single[1, max_seq_length]tensor.The benefit of this approach is that we're able to discard a lot of zero padding, and therefore get higher token/s training throughput. The below plot shows that we're able to get through our dataset ~20% faster. These gains depend on the dataset and configured
--max_seq_length; if we increase this we generally get better packing efficiency => higher throughput.I manually tested on SDPA and flash attention, but I still need to test xformers attention since I couldn't get it to build for blackwell.
TODO