Add Flash Attention Support - #105
Open
bogdanminko wants to merge 4 commits into
Open
Conversation
Author
Collaborator
|
is this ready for merging @bogdanminko ? |
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.
Add Flash Attention 2 support to GLiNER 2
About
Adds Flash Attention 2 support to GLiNER 2.
Tested on ModernBERT backbone with hivetrace/gliner-guard-uniencoder on A100 SXM.
TL;DR
FA2 gives a meaningful speedup only on long inputs (from ~20k chars / ~3k tokens (approx.)). On short sequences the difference is within noise, sometimes slightly slower than SDPA. Break-even point: ~5k chars, stable speedup from 20k onward.
Deps & hardware
Installation
Important: torch / python / CUDA versions must match the pre-built wheel.
Otherwise you could install it from source : https://github.com/dao-ailab/flash-attention
Usage
FA2 is auto-detected on model load, or can be forced via an environment variable (same pattern as Flash DeBERTa).
You can also set the dtype via env var:
FLASH_ATTN=1 FLASH_ATTN_DTYPE=fp16 #you can use bf16When FA2 is picked up, the load log will show:
Benchmark
Pipeline
Benchmarking pipeline as a Colab notebook: https://colab.research.google.com/drive/1GLZP60Np3blvaY70YVxMlvCSpF_vQaxv?usp=sharing
Note: Colab's T4 does not support FA2, so benchmarks were run on A100.
Results
When to enable