[misc] handle flash attention 3.0 with recent transformers version (>= 4.56.0) - #7033
Conversation
flash_attn.bert_padding is unavailable in FA3-only environments. The transformers fallback for _index_first_axis/_pad_input/_unpad_input requires transformers>=4.56.0 (_pad_input/_unpad_input only became importable at module level in that release; _index_first_axis has been available since 4.53.0). Raise a clear error instead of a bare ImportError when neither dependency is new enough.
|
ardalan.mehrani seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
There was a problem hiding this comment.
Code Review
This pull request introduces a fallback mechanism in verl/utils/attention_utils.py to support environments where flash_attn is not installed (such as FlashAttention-3 only environments) by importing equivalent functions from transformers (>=4.56.0) and einops. The reviewer noted that repeatedly raising and catching ImportError exceptions on every call to _get_attention_functions() in the training hot path introduces significant performance overhead, and suggested caching the resolved imports to avoid this.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| if is_torch_npu_available(check_device=False): | ||
| from verl.utils.npu_flash_attn_utils import index_first_axis, pad_input, rearrange, unpad_input | ||
| else: | ||
| from flash_attn.bert_padding import index_first_axis, pad_input, rearrange, unpad_input | ||
| try: | ||
| from flash_attn.bert_padding import index_first_axis, pad_input, rearrange, unpad_input | ||
| except ImportError as e: | ||
| # FlashAttention-2 (`flash_attn`) is not installed, e.g. FA3-only environments. | ||
| # transformers ships equivalent implementations with matching signatures/returns, but | ||
| # `_pad_input`/`_unpad_input` are only importable at module level since transformers==4.56.0 | ||
| # (https://github.com/huggingface/transformers/pull/40002); `_index_first_axis` has been | ||
| # available since transformers==4.53.0 (https://github.com/huggingface/transformers/pull/38972). | ||
| # `rearrange` has no transformers equivalent - flash_attn.bert_padding.rearrange is itself just | ||
| # a re-export of einops.rearrange, so we import it directly from einops (a transformers dep). | ||
| from einops import rearrange | ||
|
|
||
| try: | ||
| from transformers.modeling_flash_attention_utils import ( | ||
| _index_first_axis as index_first_axis, | ||
| _pad_input as pad_input, | ||
| _unpad_input as unpad_input, | ||
| ) | ||
| except ImportError: | ||
| raise ImportError( | ||
| "Neither `flash_attn` nor a compatible `transformers` (>=4.56.0) providing " | ||
| "`_index_first_axis`/`_pad_input`/`_unpad_input` was found. Install `flash_attn` " | ||
| "or upgrade `transformers` to >=4.56.0." | ||
| ) from e | ||
|
|
||
| _index_first_axis, _pad_input, _rearrange, _unpad_input = index_first_axis, pad_input, rearrange, unpad_input |
There was a problem hiding this comment.
In FA3-only environments (where flash_attn is not installed), every call to pad_input, unpad_input, etc., will invoke _get_attention_functions(), which attempts to import flash_attn.bert_padding, raises an ImportError, catches it, and then imports from transformers. Raising and catching exceptions on every single step in the training hot path introduces significant performance overhead. Caching the imported functions after the first successful resolution avoids this overhead entirely.
if _index_first_axis is not None:
return _index_first_axis, _pad_input, _rearrange, _unpad_input
if is_torch_npu_available(check_device=False):
from verl.utils.npu_flash_attn_utils import index_first_axis, pad_input, rearrange, unpad_input
else:
try:
from flash_attn.bert_padding import index_first_axis, pad_input, rearrange, unpad_input
except ImportError as e:
# FlashAttention-2 (flash_attn) is not installed, e.g. FA3-only environments.
# transformers ships equivalent implementations with matching signatures/returns, but
# _pad_input/_unpad_input are only importable at module level since transformers==4.56.0
# (https://github.com/huggingface/transformers/pull/40002); _index_first_axis has been
# available since transformers==4.53.0 (https://github.com/huggingface/transformers/pull/38972).
# rearrange has no transformers equivalent - flash_attn.bert_padding.rearrange is itself just
# a re-export of einops.rearrange, so we import it directly from einops (a transformers dep).
from einops import rearrange
try:
from transformers.modeling_flash_attention_utils import (
_index_first_axis as index_first_axis,
_pad_input as pad_input,
_unpad_input as unpad_input,
)
except ImportError:
raise ImportError(
"Neither flash_attn nor a compatible transformers (>=4.56.0) providing "
"_index_first_axis/_pad_input/_unpad_input was found. Install flash_attn "
"or upgrade transformers to >=4.56.0."
) from e
_index_first_axis, _pad_input, _rearrange, _unpad_input = index_first_axis, pad_input, rearrange, unpad_inputdb120ab to
698285e
Compare
|
@ArdalanM Please fix pre-commit fail: https://github.com/verl-project/verl/blob/main/CONTRIBUTING.md#code-linting-and-formatting |
Summary
verl/utils/attention_utils.pyfalls back totransformers.modeling_flash_attention_utilswhenflash_attnisn't installed, which is how we support running with FlashAttention-3 (flash_attn_interface) instead of FA2. That fallback needstransformers>=4.56.0(_pad_input/_unpad_inputweren't importable before that). On older transformers it failed with a confusing bareImportErrorinstead of a clear message.This PR catches that and raises a clear error telling the user to install
flash_attnor upgradetransformers.Test plan
Tested with FlashAttention-3 and transformers 5.12 via
examples/cispo_trainer/run_qwen3_8b_fsdp.sh: