@@ -178,6 +178,44 @@ def _override_attn_implementation(config: PretrainedConfig, attn_implementation:
178178 pass
179179
180180
181+ def _set_paged_attention_safe_cuda_graphs (model ) -> None :
182+ """Disable CUDA graph capture for paged attention by default.
183+
184+ ``transformers`` continuous batching defaults ``use_cuda_graph=True`` for flash-attention
185+ paths, but ``flash_attn_with_kvcache`` is not currently CUDA-graph capture-safe. Setting
186+ the model's default ``continuous_batching_config`` keeps paged Flash Attention working for
187+ direct ``generate_batch`` / ``init_continuous_batching`` callers without requiring a monkey
188+ patch.
189+ """
190+ if not hasattr (model , "config" ) or not isinstance (model .config , PretrainedConfig ):
191+ return
192+
193+ attn = getattr (model .config , "_attn_implementation" , None ) or getattr (
194+ model .config , "attn_implementation" , None
195+ )
196+ if not isinstance (attn , str ) or "paged" not in [part .strip () for part in attn .split ("|" )]:
197+ return
198+
199+ try :
200+ from transformers import ContinuousBatchingConfig , GenerationConfig
201+ except Exception :
202+ return
203+
204+ gen_config = getattr (model , "generation_config" , None )
205+ if gen_config is None :
206+ try :
207+ gen_config = GenerationConfig .from_model_config (model .config )
208+ except Exception :
209+ return
210+ model .generation_config = gen_config
211+
212+ cb_config = getattr (gen_config , "continuous_batching_config" , None )
213+ if not isinstance (cb_config , ContinuousBatchingConfig ):
214+ gen_config .continuous_batching_config = ContinuousBatchingConfig (use_cuda_graph = (False , False ))
215+ elif getattr (cb_config , "use_cuda_graph" , None ) is None :
216+ cb_config .use_cuda_graph = (False , False )
217+
218+
181219def _is_accelerated_attention_device (device : object ) -> bool :
182220 """Return True when the selected device can run CUDA/ROCm flash attention."""
183221
@@ -663,6 +701,7 @@ def from_pretrained(
663701 trust_remote_code = trust_remote_code ,
664702 model_local_path = model_local_path ,
665703 )
704+ _set_paged_attention_safe_cuda_graphs (instance .model )
666705
667706 return instance
668707
@@ -825,6 +864,7 @@ def skip(*args, **kwargs):
825864 trust_remote_code = trust_remote_code ,
826865 model_local_path = model_local_path ,
827866 )
867+ _set_paged_attention_safe_cuda_graphs (instance .model )
828868
829869 timer = getattr (instance , "quant_region_timer" , None )
830870 if timer is not None :
@@ -1078,6 +1118,7 @@ def from_quantized(
10781118 model_local_path = model_local_path ,
10791119 )
10801120 instance ._runtime_generate = runtime_generate
1121+ _set_paged_attention_safe_cuda_graphs (instance .model )
10811122 return instance
10821123
10831124 if format_code == FORMAT .MARLIN :
@@ -1677,7 +1718,7 @@ def assign(mod, device_id):
16771718 cls .generate = lambda _ , ** kwargs : mlx_generate (model = model , tokenizer = tokenizer , ** kwargs )
16781719
16791720
1680- return cls (
1721+ instance = cls (
16811722 model ,
16821723 quantized = True ,
16831724 quantize_config = qcfg ,
@@ -1687,6 +1728,8 @@ def assign(mod, device_id):
16871728 trust_remote_code = trust_remote_code ,
16881729 model_local_path = model_local_path ,
16891730 )
1731+ _set_paged_attention_safe_cuda_graphs (instance .model )
1732+ return instance
16901733
16911734 cls .from_quantized = from_quantized
16921735
0 commit comments