Skip to content

Enable Flash Attention in recompute and causal modes#21

Merged
5 commits merged into
HabanaAI:habana-mainfrom
wszczurekhabana:flash_attention_causal_recompute
Feb 8, 2024
Merged

Enable Flash Attention in recompute and causal modes#21
5 commits merged into
HabanaAI:habana-mainfrom
wszczurekhabana:flash_attention_causal_recompute

Conversation

@wszczurekhabana
Copy link
Copy Markdown

@wszczurekhabana wszczurekhabana commented Feb 2, 2024

This is a follow-up on: huggingface#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:

Ratio Max input tokens Max new tokens Batch size Throughput [tokens/s]
97% 31744 1042 12 85.54
75% 24576 8192 16 336.29
50% 16384 16384 24 521.39
25% 8192 24576 36 708.42

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_source

Results 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

@wszczurekhabana wszczurekhabana requested a review from a user February 2, 2024 11:49
action="store_true",
help="Whether to enable Habana Flash Attention, provided that the model supports it.",
)
parser.add_argument(
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems as a counter intuitive argument for inferencing.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is no performance penalty and memory is also saved then we can internally pass it as True for 1st token when flash attention is used.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed offline. Here is summary:
We should left it controllable, cause it may be reasonable to turn it off for example on finetuning.
Additionnaly this parameter may cause some slight overhead even on inference, so maybe it's reasonable to turn it off if we don't need it.

action="store_true",
help="Whether to enable Habana Flash Attention in recompute mode on first token generation. This gives an opportunity of splitting graph internally which helps reduce memory consumption.",
)
parser.add_argument(
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we forcefully set this to True when batch size is 1 and when use_flash_attention is passed. We can add it to help text that this will be taken care.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed offline. Here is summary:
The improvements in memory are to achieve higher batch size and thus increase throughput. Improvements from setting causal=True (and using triangular softmax underneath) will scale with batch size and seq-lenght. So causal on bigger batches is still usefull, but we need to be mindfull that without speciall handling of padding tokens it requires same lenght inputs in a batch.

Comment thread examples/text-generation/README.md
@mandy-li
Copy link
Copy Markdown

mandy-li commented Feb 2, 2024

@wszczurekhabana , please provide llama2-70b finetuning test results.

@wszczurekhabana
Copy link
Copy Markdown
Author

@mandy-li please see the finetuning results updated in the description of the PR.

@mandy-li
Copy link
Copy Markdown

mandy-li commented Feb 7, 2024

@mandy-li please see the finetuning results updated in the description of the PR.

Thanks @wszczurekhabana . Since FT perf with flash_attention_causal_mask is better, can you add this arg to the command example in README?

@wszczurekhabana
Copy link
Copy Markdown
Author

Hi @mandy-li , I've added the argument to README in latest commit.

@puneeshkhanna
Copy link
Copy Markdown

Changes look good to me. @dvarshney-habana - we can merge.

@ghost ghost merged commit bfe362b into HabanaAI:habana-main Feb 8, 2024
bhargaveede pushed a commit that referenced this pull request Feb 19, 2024
* 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
bhargaveede pushed a commit that referenced this pull request Feb 19, 2024
* 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
astachowiczhabana pushed a commit that referenced this pull request Apr 5, 2024
* 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
astachowiczhabana pushed a commit that referenced this pull request Apr 5, 2024
* 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
astachowiczhabana pushed a commit that referenced this pull request Apr 11, 2024
…ce#862)

Co-authored-by: regisss <15324346+regisss@users.noreply.github.com>
Co-authored-by: Libin Tang <litang@habana.ai>
kalyanjk pushed a commit to kalyanjk/optimum-habana-fork that referenced this pull request Apr 12, 2024
* 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
kalyanjk pushed a commit to kalyanjk/optimum-habana-fork that referenced this pull request Apr 15, 2024
* 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
astachowiczhabana pushed a commit that referenced this pull request Apr 19, 2024
* 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
astachowiczhabana pushed a commit that referenced this pull request Apr 22, 2024
* 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
astachowiczhabana pushed a commit that referenced this pull request Apr 24, 2024
* 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
astachowiczhabana pushed a commit that referenced this pull request Apr 24, 2024
* 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
@astachowiczhabana
Copy link
Copy Markdown

huggingface#862

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants