3434 require_torch ,
3535 set_config_for_less_flaky_test ,
3636 set_model_for_less_flaky_test ,
37- set_model_tester_for_less_flaky_test ,
3837 torch_device ,
3938)
4039from transformers .utils import (
5857 import torch
5958
6059
61- def _test_eager_matches_sdpa_inference (
60+ def _test_encoder_eager_matches_sdpa_inference (
6261 self ,
63- name ,
6462 dtype ,
65- padding_side ,
66- use_attention_mask ,
6763 output_attentions ,
6864 enable_kernels ,
6965 atols = None ,
@@ -127,22 +123,6 @@ def _test_eager_matches_sdpa_inference(
127123 ("cuda" , True , torch .float16 ): 5e-3 ,
128124 }
129125
130- set_model_tester_for_less_flaky_test (self )
131-
132- def _can_output_attn (model ):
133- parameters = inspect .signature (model .forward ).parameters
134- if "output_attentions" in parameters :
135- return True
136-
137- kwargs_param = parameters .get ("kwargs" )
138- if kwargs_param is not None :
139- try :
140- annotation = kwargs_param .annotation .__args__
141- return "output_attentions" in annotation [0 ].__annotations__
142- except AttributeError :
143- return False
144- return False
145-
146126 for model_class in self .all_model_classes :
147127 config , inputs_dict = self .model_tester .prepare_config_and_inputs_for_common ()
148128 set_config_for_less_flaky_test (config )
@@ -172,10 +152,6 @@ def _can_output_attn(model):
172152 set_model_for_less_flaky_test (model_eager )
173153 set_model_for_less_flaky_test (model_sdpa )
174154
175- can_output_attn = _can_output_attn (model_sdpa )
176- if not (self .has_attentions and can_output_attn ) and output_attentions :
177- self .skipTest (reason = "Model does not support output_attentions" )
178-
179155 # TODO: if we can also check with `batch_size=1` without being flaky?
180156 for batch_size in [7 ]:
181157 input_data_batch_size = batch_size
@@ -218,43 +194,13 @@ def _can_output_attn(model):
218194 pixel_values = pixel_values [:target_len ]
219195 processed_inputs ["pixel_values" ] = pixel_values
220196
221- if not use_attention_mask :
222- dummy_attention_mask = None
223- else :
224- dummy_attention_mask = inputs_dict .get ("attention_mask" , None )
225- if dummy_attention_mask is None :
226- seqlen = processed_inputs [model .main_input_name ].shape [- 1 ]
227- dummy_attention_mask = torch .ones (batch_size , seqlen ).to (torch .int64 ).to (torch_device )
228-
229- # extend dummy_attention_mask to have at least `batch_size` elements
230- if dummy_attention_mask .shape [0 ] < batch_size :
231- size = (batch_size - dummy_attention_mask .shape [0 ], * dummy_attention_mask .shape [1 :])
232- extension = torch .ones (size = size , dtype = dummy_attention_mask .dtype , device = torch_device )
233- dummy_attention_mask = torch .cat ((dummy_attention_mask , extension ), dim = 0 )
234-
235- dummy_attention_mask = dummy_attention_mask [:batch_size ].to (torch_device )
236-
237- dummy_attention_mask [:] = 1
238- if padding_side == "left" :
239- dummy_attention_mask [- 1 , :2 ] = 0
240- dummy_attention_mask [- 1 , 2 :] = 1
241- elif padding_side == "right" :
242- dummy_attention_mask [- 1 , - 2 :] = 0
243- dummy_attention_mask [- 1 , :- 2 ] = 1
244-
245197 processed_inputs .update (
246198 {
247199 "output_hidden_states" : True ,
200+ "output_attentions" : output_attentions ,
248201 }
249202 )
250203
251- # Otherwise fails for e.g. WhisperEncoderModel
252- if "attention_mask" in inspect .signature (model_eager .forward ).parameters :
253- processed_inputs ["attention_mask" ] = dummy_attention_mask
254-
255- if self .has_attentions and _can_output_attn (model_sdpa ):
256- processed_inputs ["output_attentions" ] = output_attentions
257-
258204 # TODO: test gradients as well (& for FA2 as well!)
259205 with torch .no_grad ():
260206 with sdpa_kernel (
@@ -270,35 +216,11 @@ def _can_output_attn(model):
270216 outputs_eager = model_eager (** prepared_inputs )
271217 outputs_sdpa = model_sdpa (** prepared_inputs )
272218
273- if "logits_per_text" in outputs_eager :
274- key = "logits_per_text"
275- elif "vision_hidden_states" in outputs_eager :
276- key = "vision_hidden_states"
277- elif "audio_values" in outputs_eager :
278- key = "audio_values"
279- elif "decoder_hidden_states" in outputs_eager :
280- key = "decoder_hidden_states"
281- elif "logits" in outputs_eager and "Classification" in model_class .__name__ :
282- key = "logits"
283- elif "language_model_outputs" in outputs_eager and "blip" in model_class .__name__ .lower ():
284- outputs_eager = outputs_eager ["language_model_outputs" ]
285- outputs_sdpa = outputs_sdpa ["language_model_outputs" ]
286- key = "hidden_states" if "hidden_states" in outputs_eager else "decoder_hidden_states"
287- else :
288- key = "hidden_states"
219+ key = "hidden_states"
289220
290221 # TODO: rename logits -> hidden_states
291- logits_eager = outputs_eager [key ]
292- logits_sdpa = outputs_sdpa [key ]
293-
294- if key in ["vision_hidden_states" , "decoder_hidden_states" , "hidden_states" ]:
295- logits_eager = logits_eager [- 1 ]
296- logits_sdpa = logits_sdpa [- 1 ]
297-
298- if key == "logits_per_text" :
299- nan_mask = torch .isnan (logits_eager )
300- logits_eager [nan_mask ] = 0
301- logits_sdpa [nan_mask ] = 0
222+ logits_eager = outputs_eager [key ][- 1 ]
223+ logits_sdpa = outputs_sdpa [key ][- 1 ]
302224
303225 if torch_device in ["cpu" , "cuda" ]:
304226 atol = atols [torch_device , enable_kernels , dtype ]
@@ -316,25 +238,6 @@ def _can_output_attn(model):
316238 atol = 1e-7
317239 rtol = 1e-4
318240
319- # Masked tokens output slightly deviates - we don't mind that.
320- if use_attention_mask :
321- _logits_sdpa = torch .zeros_like (input = logits_sdpa )
322- _logits_eager = torch .zeros_like (input = logits_eager )
323-
324- _logits_sdpa [:- 1 ] = logits_sdpa [:- 1 ]
325- _logits_eager [:- 1 ] = logits_eager [:- 1 ]
326-
327- if padding_side == "left" :
328- _logits_sdpa [- 1 :, 2 :] = logits_sdpa [- 1 :, 2 :]
329- _logits_eager [- 1 :, 2 :] = logits_eager [- 1 :, 2 :]
330-
331- elif padding_side == "right" :
332- _logits_sdpa [- 1 :, 2 :] = logits_sdpa [- 1 :, :- 2 ]
333- _logits_eager [- 1 :, 2 :] = logits_eager [- 1 :, :- 2 ]
334-
335- logits_sdpa = _logits_sdpa
336- logits_eager = _logits_eager
337-
338241 # Avoid test flakiness with bf16!
339242 # bf16 is not good at precision when the magnitude is larger. We have some models like `SiglipVision` with
340243 # this test passing all the time for fp32/fp16 but flaky with bf16. Furthermore, `llama` and `clip` have
@@ -467,14 +370,11 @@ def test_model_get_set_embeddings(self):
467370 def test_eager_matches_sdpa_inference (
468371 self , name , dtype , padding_side , use_attention_mask , output_attentions , enable_kernels
469372 ):
470- _test_eager_matches_sdpa_inference (
471- self , name , dtype , padding_side , use_attention_mask , output_attentions , enable_kernels
472- )
373+ if use_attention_mask :
374+ self . skipTest ( reason = "VideoLlama3VisionModel does not use attention mask" )
375+ _test_encoder_eager_matches_sdpa_inference ( self , dtype , output_attentions , enable_kernels )
473376
474377 def test_attention_outputs (self ):
475- if not self .has_attentions :
476- self .skipTest (reason = "Model does not output attentions" )
477-
478378 config , inputs_dict = self .model_tester .prepare_config_and_inputs_for_common ()
479379 config .return_dict = True
480380 # force eager attention to support output attentions
@@ -494,7 +394,7 @@ def test_attention_outputs(self):
494394 model .eval ()
495395 with torch .no_grad ():
496396 outputs = model (** self ._prepare_for_class (inputs_dict , model_class ))
497- attentions = outputs .encoder_attentions if config . is_encoder_decoder else outputs . attentions
397+ attentions = outputs .attentions
498398 self .assertEqual (len (attentions ), self .model_tester .num_hidden_layers )
499399
500400 # check that output_attentions also work using config
@@ -508,7 +408,7 @@ def test_attention_outputs(self):
508408 model .eval ()
509409 with torch .no_grad ():
510410 outputs = model (** self ._prepare_for_class (inputs_dict , model_class ))
511- attentions = outputs .encoder_attentions if config . is_encoder_decoder else outputs . attentions
411+ attentions = outputs .attentions
512412 self .assertEqual (len (attentions ), self .model_tester .num_hidden_layers )
513413
514414 self .assertListEqual (
@@ -526,13 +426,7 @@ def test_attention_outputs(self):
526426 with torch .no_grad ():
527427 outputs = model (** self ._prepare_for_class (inputs_dict , model_class ))
528428
529- if hasattr (self .model_tester , "num_hidden_states_types" ):
530- added_hidden_states = self .model_tester .num_hidden_states_types
531- elif self .is_encoder_decoder :
532- added_hidden_states = 2
533- else :
534- added_hidden_states = 1
535- self .assertEqual (out_len + added_hidden_states , len (outputs ))
429+ self .assertEqual (out_len + 1 , len (outputs ))
536430
537431 self_attentions = outputs .encoder_attentions if config .is_encoder_decoder else outputs .attentions
538432
@@ -640,7 +534,7 @@ def __init__(
640534 "max_window_layers" : 3 ,
641535 "model_type" : "qwen2" ,
642536 "num_attention_heads" : 4 ,
643- "num_hidden_layers" : 4 ,
537+ "num_hidden_layers" : 2 ,
644538 "num_key_value_heads" : 2 ,
645539 "rms_norm_eps" : 1e-06 ,
646540 "rope_scaling" : None ,
0 commit comments