Skip to content
IJK

Padding-Free Packing With Flash Attention 2 in Transformers

By IJK ·

What a Hugging Face post says is now compatible with Flash Attention 2, what DataCollatorWithFlattening does and how TRL users enable it.

What is now compatible

Training with packed instruction tuning examples (without padding) is now compatible with Flash Attention 2 in Hugging Face, thanks to a recent PR and the new DataCollatorWithFlattening [1]

What the collator does

By selecting DataCollatorWithFlattening, Hugging Face Trainer users can now seamlessly concatenate sequences into a single tensor while accounting for sequence boundaries during Flash Attention 2 computations. [2]

provided byalso viaPacked examples with Flash Attention 2now compatible, with a new data collatorDataCollatorWithFlatteningconcatenates sequences into one tensor and accoun...padding_free=True in TRLsame feature for SFTTrainer users
How is packing enabled?

The TRL flag

The same feature is available to Hugging Face SFTTrainer users in the TRL library by setting a new flag, padding_free=True, when calling the data collator DataCollatorForCompletionOnlyLM. [3]

Sources

  1. The Hugging Face source says: Training with packed instruction tuning examples (without padding) is now compatible with Flash Attention 2 in Hugging Face, thanks to a recent PR and the new DataCollatorWithFlattening Packing with Flash Attention 2
  2. The Hugging Face source says: By selecting DataCollatorWithFlattening, Hugging Face Trainer users can now seamlessly concatenate sequences into a single tensor while accounting for sequence boundaries during Flash Attention 2 computations. Packing with Flash Attention 2
  3. The Hugging Face source says: The same feature is available to Hugging Face SFTTrainer users in the TRL library by setting a new flag, padding_free=True, when calling the data collator DataCollatorForCompletionOnlyLM. Packing with Flash Attention 2

Editorial (AI-assisted; automatically checked, not human-reviewed). Each statement is linked to its source; check the source before relying on a detail.

  • training
  • flash attention
  • trl
  • hugging face