From 0ac228305df963c5030d5886b03f913fd937552b Mon Sep 17 00:00:00 2001 From: YangKai0616 Date: Mon, 18 May 2026 09:03:18 +0000 Subject: [PATCH 1/4] Support FA2 flash_attn_with_kvcache for XPU continuous batching --- .../continuous_batching/initialization.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/transformers/generation/continuous_batching/initialization.py b/src/transformers/generation/continuous_batching/initialization.py index 30f1d082ce28..477fc813ca90 100644 --- a/src/transformers/generation/continuous_batching/initialization.py +++ b/src/transformers/generation/continuous_batching/initialization.py @@ -22,6 +22,7 @@ from ...configuration_utils import PretrainedConfig from ...generation.configuration_utils import CompileConfig, ContinuousBatchingConfig from ...modeling_flash_attention_utils import lazy_import_paged_flash_attention +from ...utils import is_torch_xpu_available from ...utils.generic import is_flash_attention_requested from .requests import logger from .utils import WorkloadHints @@ -103,7 +104,7 @@ def ensure_decode_fast_path_is_available( available, and no user-provided max blocks per request, set it to the fallback default.""" # Then, if the decode fast path is not turned off, check if it is available if cb_config.max_blocks_per_request != 0: - # NOTE: block table should be available with FA2 and FA3, but there seems to be an issue with FA2 atm + # NOTE: For CUDA, block table should be available with FA2 and FA3, but there seems to be an issue with FA2 atm if is_flash_attention_requested(config, version=3): flash_attn_with_kvcache = lazy_import_paged_flash_attention(config._attn_implementation)[1] conditions = [ @@ -118,6 +119,19 @@ def ensure_decode_fast_path_is_available( f"because at least one condition is not met: {conditions}." ) cb_config.max_blocks_per_request = 0 + elif torch.xpu.is_available() and is_flash_attention_requested(config, version=2): + flash_attn_with_kvcache = lazy_import_paged_flash_attention(config._attn_implementation)[1] + conditions = [ + is_torch_xpu_available(), # Block table is supported on XPU through kernels-community/flash-attn2 + flash_attn_with_kvcache is not None, + ] + if not all(conditions): + if user_requested: + logger.warning( + f"Although {cb_config.max_blocks_per_request = }, the decode fast path is not available " + f"because at least one condition is not met: {conditions}." + ) + cb_config.max_blocks_per_request = 0 # Specific warning for attn implementation other than FA3 else: if user_requested: From 1439bd24be4413de8bc0e55340d97541058153af Mon Sep 17 00:00:00 2001 From: YangKai0616 Date: Tue, 26 May 2026 06:50:26 +0000 Subject: [PATCH 2/4] Update according to the comments --- .../continuous_batching/initialization.py | 29 ++++++++----------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/src/transformers/generation/continuous_batching/initialization.py b/src/transformers/generation/continuous_batching/initialization.py index 477fc813ca90..d08f70dfa51f 100644 --- a/src/transformers/generation/continuous_batching/initialization.py +++ b/src/transformers/generation/continuous_batching/initialization.py @@ -105,10 +105,15 @@ def ensure_decode_fast_path_is_available( # Then, if the decode fast path is not turned off, check if it is available if cb_config.max_blocks_per_request != 0: # NOTE: For CUDA, block table should be available with FA2 and FA3, but there seems to be an issue with FA2 atm - if is_flash_attention_requested(config, version=3): + cuda_available = torch.cuda.is_available() + fa_cuda = is_flash_attention_requested(config, version=3) and cuda_available + # XPU support is given through its kernel variation `kernels-community/flash-attn2` + xpu_available = is_torch_xpu_available() + fa_xpu = is_flash_attention_requested(config, version=2) and xpu_available + if fa_cuda or fa_xpu: flash_attn_with_kvcache = lazy_import_paged_flash_attention(config._attn_implementation)[1] conditions = [ - torch.cuda.is_available(), # Block table is only supported on CUDA + fa_cuda or fa_xpu, # Block table is only supported on these flash_attn_with_kvcache is not None, # The `flash_attn_with_kvcache` fn is needed ] # Throw a warning only if the decode fast path was requested by the user @@ -119,25 +124,15 @@ def ensure_decode_fast_path_is_available( f"because at least one condition is not met: {conditions}." ) cb_config.max_blocks_per_request = 0 - elif torch.xpu.is_available() and is_flash_attention_requested(config, version=2): - flash_attn_with_kvcache = lazy_import_paged_flash_attention(config._attn_implementation)[1] - conditions = [ - is_torch_xpu_available(), # Block table is supported on XPU through kernels-community/flash-attn2 - flash_attn_with_kvcache is not None, - ] - if not all(conditions): - if user_requested: - logger.warning( - f"Although {cb_config.max_blocks_per_request = }, the decode fast path is not available " - f"because at least one condition is not met: {conditions}." - ) - cb_config.max_blocks_per_request = 0 - # Specific warning for attn implementation other than FA3 + # Specific warning for unsupported attention implementation/device combinations else: if user_requested: logger.warning( f"Although {cb_config.max_blocks_per_request = }, the decode fast path is not available " - f"because the attention implementation is not FA3. Got {config._attn_implementation = }." + "because the attention implementation and device combination is not supported. Supported " + "combinations are Flash Attention 3 on CUDA, or Flash Attention 2 on XPU through " + "`kernels-community/flash-attn2`. " + f"Got {config._attn_implementation = }, {cuda_available = }, {xpu_available = }." ) cb_config.max_blocks_per_request = 0 From 2c3cd058d505c7732a6753dddf7b67d8cb10360c Mon Sep 17 00:00:00 2001 From: YangKai0616 Date: Tue, 26 May 2026 07:03:49 +0000 Subject: [PATCH 3/4] simplify the code --- .../generation/continuous_batching/initialization.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/transformers/generation/continuous_batching/initialization.py b/src/transformers/generation/continuous_batching/initialization.py index d08f70dfa51f..9e05baf6d375 100644 --- a/src/transformers/generation/continuous_batching/initialization.py +++ b/src/transformers/generation/continuous_batching/initialization.py @@ -110,18 +110,14 @@ def ensure_decode_fast_path_is_available( # XPU support is given through its kernel variation `kernels-community/flash-attn2` xpu_available = is_torch_xpu_available() fa_xpu = is_flash_attention_requested(config, version=2) and xpu_available - if fa_cuda or fa_xpu: + if fa_cuda or fa_xpu: # Block table is only supported on these flash_attn_with_kvcache = lazy_import_paged_flash_attention(config._attn_implementation)[1] - conditions = [ - fa_cuda or fa_xpu, # Block table is only supported on these - flash_attn_with_kvcache is not None, # The `flash_attn_with_kvcache` fn is needed - ] # Throw a warning only if the decode fast path was requested by the user - if not all(conditions): + if flash_attn_with_kvcache is None: if user_requested: logger.warning( f"Although {cb_config.max_blocks_per_request = }, the decode fast path is not available " - f"because at least one condition is not met: {conditions}." + f"because `flash_attn_with_kvcache` is not available for {config._attn_implementation = }." ) cb_config.max_blocks_per_request = 0 # Specific warning for unsupported attention implementation/device combinations From 629e8a78ed921b312e73e9029d4109d503b22310 Mon Sep 17 00:00:00 2001 From: YangKai0616 Date: Tue, 26 May 2026 07:19:22 +0000 Subject: [PATCH 4/4] Code quality --- .../generation/continuous_batching/initialization.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transformers/generation/continuous_batching/initialization.py b/src/transformers/generation/continuous_batching/initialization.py index 9e05baf6d375..c515b428dd87 100644 --- a/src/transformers/generation/continuous_batching/initialization.py +++ b/src/transformers/generation/continuous_batching/initialization.py @@ -110,7 +110,7 @@ def ensure_decode_fast_path_is_available( # XPU support is given through its kernel variation `kernels-community/flash-attn2` xpu_available = is_torch_xpu_available() fa_xpu = is_flash_attention_requested(config, version=2) and xpu_available - if fa_cuda or fa_xpu: # Block table is only supported on these + if fa_cuda or fa_xpu: # Block table is only supported on these flash_attn_with_kvcache = lazy_import_paged_flash_attention(config._attn_implementation)[1] # Throw a warning only if the decode fast path was requested by the user if flash_attn_with_kvcache is None: