---
title: "Improving Hugging Face Training Efficiency Through Packing with Flash Attention 2"
description: "Hugging Face Transformers now supports padding-free packing of instruction-tuning examples with Flash Attention 2 via the new DataCollatorWithFlattening, which concatenates sequences and passes position_ids so flash_attn_varlen_func respects example boundaries and avoids cross-example attention."
url: "https://swe.quest/articles/improving-hugging-face-training-efficiency-through-packing-with-flash-attention-0e7b5058677254df"
canonical: "https://huggingface.co/blog/packing-with-FA2"
source: "Hugging Face"
published: "2024-08-21"
difficulty: "hard"
kind: "How-to"
reading_minutes: 5
tags: ["model-training","llms","performance","python"]
---

# Improving Hugging Face Training Efficiency Through Packing with Flash Attention 2

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2024-08-21, 5 min read, How-to. Original: [https://huggingface.co/blog/packing-with-FA2](https://huggingface.co/blog/packing-with-FA2)

## Summary

Hugging Face Transformers now supports padding-free packing of instruction-tuning examples with Flash Attention 2 via the new DataCollatorWithFlattening, which concatenates sequences and passes position\_ids so flash\_attn\_varlen\_func respects example boundaries and avoids cross-example attention. On 8 A100s, throughput doubled on FLAN (short, highly variable lengths) and rose 1.4x on OrcaMath for Llama2-7B, Mistral-7B and Granite-8B-code, with peak memory down 20% and 6% respectively. Validation loss matched padded training because the number of optimization steps is unchanged. Usage requires loading the model with attn\_implementation="flash\_attention\_2" and using the new collator, or setting padding\_free=True on TRL's DataCollatorForCompletionOnlyLM. It works only for models that expose position\_ids.

Written by swe.quest, not by the author.

**Difficulty:** Hard: written for specialists in the field

## Topics

- [Model training](https://swe.quest/tags/model-training.md) (AI & ML)
- [LLMs](https://swe.quest/tags/llms.md) (AI & ML)
- [Performance engineering](https://swe.quest/tags/performance.md) (Reliability & operations)
- [Python](https://swe.quest/tags/python.md) (Languages)

## Prerequisites

- LLM fine-tuning ([easy posts on LLMs](https://swe.quest/tags/llms.md?difficulty=easy))
- attention mechanism
- Hugging Face Trainer

## Keywords

Flash Attention 2, Hugging Face Transformers, DataCollatorWithFlattening, TRL, SFTTrainer, flash\_attn\_varlen\_func

## Related posts

- [Optimizing your LLM in production](https://swe.quest/articles/optimizing-your-llm-in-production-aa5bbb901928fa57.md): Hugging Face · 2023-09-15 · Medium · 31 min read · Inference & serving, LLMs, Performance engineering, Python
  Serving billion-parameter LLMs efficiently comes down to three levers: lower precision, Flash Attention, and inference-friendly architectures such as ALiBi, rotary embeddings, Multi-Query Attention and Grouped-Query Attention.
- [Fine-tuning Llama 2 70B using PyTorch FSDP](https://swe.quest/articles/fine-tuning-llama-2-70b-using-pytorch-fsdp-0e8dde34b4ad3fd0.md): Hugging Face · 2023-09-13 · Hard · 10 min read · Model training, LLMs, Hardware, Python
  Full fine-tuning of Llama 2 70B across two nodes of 8×A100 80GB GPUs with PyTorch FSDP, Hugging Face Accelerate, Transformers and TRL, addressing three obstacles.
- [Making FlashAttention-4 faster for inference](https://swe.quest/articles/making-flashattention-4-faster-for-inference-dc917e70c20e0f22.md): Modal · 2026-06-11 · Hard · 14 min read · Inference & serving, Hardware, Performance engineering, LLMs
  Modal engineers extended the FlashAttention-4 kernel for decode-heavy LLM inference on Blackwell GPUs through two kinds of change: new parallelism strategies, such as split-KV parallelism across the key/value dimension, and support for irregular memory access via cp.async in place of TMA bulk loads.
- [Parameter-Efficient Fine-Tuning using 🤗 PEFT](https://swe.quest/articles/parameter-efficient-fine-tuning-using-peft-5c28b9ec4e65b0a1.md): Hugging Face · 2023-02-10 · Medium · 5 min read · Model training, LLMs, Python
  Hugging Face's PEFT library brings parameter-efficient fine-tuning methods (LoRA, Prefix Tuning, Prompt Tuning, P-Tuning) to Transformers and Accelerate.
- [Make LLM Fine-tuning 2x faster with Unsloth and 🤗 TRL](https://swe.quest/articles/make-llm-fine-tuning-2x-faster-with-unsloth-and-trl-5db8c0b6a40cf74e.md): Hugging Face · 2024-01-10 · Medium · 5 min read · Model training, LLMs, Performance engineering, Python
  Unsloth speeds up QLoRA fine-tuning of Llama and Mistral-family models by rewriting PyTorch modules as Triton kernels with manually derived backpropagation.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
