Enable Flash Attention in recompute and causal modes (#21)#862
Merged
regisss merged 13 commits intoApr 5, 2024
Conversation
Co-authored-by: regisss <15324346+regisss@users.noreply.github.com>
Co-authored-by: regisss <15324346+regisss@users.noreply.github.com>
* Enable Flash Attention in recompute and causal modes * Add flash_attention_causal_mask to generation utils * Propagate Flash Attention causal_mask to finetuning example * Modify README example and provide additional description * Add flash_attention_causal_mask to FT README
regisss
reviewed
Apr 3, 2024
Collaborator
regisss
left a comment
There was a problem hiding this comment.
Why adding use_fused_rope in the changes of this PR? You need it for recompute and causal mode?
regisss
reviewed
Apr 4, 2024
Collaborator
|
@wszczurekhabana I changed the target of this PR to main because we first need to merge this change there and then I'll cherry-pick it in the release branch. |
regisss
reviewed
Apr 5, 2024
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
regisss
approved these changes
Apr 5, 2024
regisss
added a commit
that referenced
this pull request
Apr 5, 2024
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Cherry-pick from: HabanaAI#21
Original description:
This is a follow-up on: #623
where main issue is that when running with Flash Attention in causal mode (required for performance and memory optimizations on 1st token) it will generate a triangular attention mask - same on each batch of the input data.
If we have more than one batch of sequences that have different lengths, tokenizer will include a padding for those sentences that have smaller sequence lengths than 'max input tokens'. In the case of Flash Attention in causal mode, where triangular attention mask is applied to all the inputs, this will effectively mean that padding tokens are also attended to, which will in turn result in junk output being generated
This PR propagates the modes of running to run_generation.py so that optimization can still be used in case of the same length inputs in a batch. This is controlled through: --flash_attention_recompute and --flash_attention_causal_mask.
Additionally this PR provides a way to pass real input data to the model from Project Gutenberg Books for easier testing of large sequence lengths.
Below are the throughput measurements for different ratios of prompt to max seq length:
example of the command tested:
python ../gaudi_spawn.py --use_deepspeed --world_size 8 run_generation.py \ --num_beams 1 --attn_softmax_bf16 --model_name_or_path meta-llama/Llama-2-70b-hf \ --warmup 2 --n_iterations 3 --use_hpu_graphs --use_kv_cache --max_input_tokens 31744 --max_new_tokens 1042 --bf16 --batch_size 12 --reuse_cache --trim_logits --limit_hpu_graphs --use_flash_attention --flash_attention_recompute --flash_attention_causal_mask --book_sourceResults on finetuning:
No Flash Attention:
'train_runtime': 2499.5658, 'train_samples_per_second': 2.626
Flash Attention:
'train_runtime': 2487.0323, 'train_samples_per_second': 2.636
Flash Attention Causal:
'train_runtime': 2449.3563, 'train_samples_per_second': 2.686