Enable sequence packing with FlashAttention-2 #41
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.
Currently, datasets are prepared for caching using
transformer_lens
'tokenize_and_concatenate()
. This is problematic because sequences are concatenated as is, and no special handling is done to avoid cross-sequence attention contamination. In addition, sequences are separated by EOS tokens, which is not ideal when training SAEs.An alternative can be to have one sequence per sample in each batch, but this requires padding which wastes GPU resources and is thus sub-optimal.
This PR brings the ability to "pack" sequences together, meaning that sequences in a batch are concatenated to form a single long sample containing all sequences and avoid padding. To avoid attention contamination, FlashAttension-2 is used with the ability to pass a
position_ids
argument to bypass the need to materialize attention masks in memory (which is impractical). Using FA2 also brings a speed boost which is always welcome.For additional details, see: https://huggingface.co/blog/packing-with-FA2
Currently, FA2 with
position_ids
is only implemented for some models intransformers
. I'm working on a patch to bring it to more models, specifically GPT-NeoX-based models (e.g., Pythia) and GPT-2. Until it's upstreamed, this PR uses my fork of the library.Things done
load_dataset()
load_tokenized_data()
transformers