code: def test_attention_outputs(self):
      (
          config,
          inputs_dict,
      ) = self.model_tester.prepare_config_and_inputs_for_common()
      config.return_dict = True
  
      seq_len = getattr(self.model_tester, "seq_length", None)
      decoder_seq_length = getattr(
          self.model_tester, "decoder_seq_length", seq_len
      )
      encoder_seq_length = getattr(
          self.model_tester, "encoder_seq_length", seq_len
      )
      decoder_key_length = getattr(
          self.model_tester, "decoder_key_length", decoder_seq_length
      )
      encoder_key_length = getattr(
          self.model_tester, "key_length", encoder_seq_length
      )
  
      for model_class in self.all_model_classes:
          inputs_dict["output_attentions"] = True
          inputs_dict["output_hidden_states"] = False
          config.return_dict = True
          model = model_class(config)
          model.to(torch_device)
          model.eval()
  
          subsampled_encoder_seq_length = (
              model._get_feat_extract_output_lengths(encoder_seq_length)
          )
          subsampled_encoder_key_length = (
              model._get_feat_extract_output_lengths(encoder_key_length)
          )
  
          with torch.no_grad():
              outputs = model(
                  **self._prepare_for_class(inputs_dict, model_class)
              )
          attentions = (
              outputs.encoder_attentions
              if config.is_encoder_decoder
              else outputs.attentions
          )
          self.assertEqual(
              len(attentions), self.model_tester.num_hidden_layers
          )
  
          # check that output_attentions also work using config
          del inputs_dict["output_attentions"]
          config.output_attentions = True
          model = model_class(config)
          model.to(torch_device)
          model.eval()
          with torch.no_grad():
              outputs = model(
                  **self._prepare_for_class(inputs_dict, model_class)
              )
          attentions = (
              outputs.encoder_attentions
              if config.is_encoder_decoder
              else outputs.attentions
          )
          self.assertEqual(
              len(attentions), self.model_tester.num_hidden_layers
          )
  
          self.assertListEqual(
              list(attentions[0].shape[-3:]),
              [
                  self.model_tester.num_attention_heads,
                  subsampled_encoder_seq_length,
                  subsampled_encoder_key_length,
              ],
          )
          out_len = len(outputs)
  
          correct_outlen = 5
  
          # loss is at first position
          if "labels" in inputs_dict:
              correct_outlen += 1  # loss is added to beginning
          if "past_key_values" in outputs:
              correct_outlen += 1  # past_key_values have been returned
  
          self.assertEqual(out_len, correct_outlen)
  
          # decoder attentions
          decoder_attentions = outputs.decoder_attentions
          self.assertIsInstance(decoder_attentions, (list, tuple))
          self.assertEqual(
              len(decoder_attentions),
              self.model_tester.num_hidden_layers,
          )
          self.assertListEqual(
              list(decoder_attentions[0].shape[-3:]),
              [
                  self.model_tester.num_attention_heads,
                  decoder_seq_length,
                  decoder_key_length,
              ],
          )
  
          # cross attentions
          cross_attentions = outputs.cross_attentions
          self.assertIsInstance(cross_attentions, (list, tuple))
          self.assertEqual(
              len(cross_attentions), self.model_tester.num_hidden_layers
          )
          self.assertListEqual(
              list(cross_attentions[0].shape[-3:]),
              [
                  self.model_tester.num_attention_heads,
                  decoder_seq_length,
                  subsampled_encoder_key_length,
              ],
          )
  
          # Check attention is always last and order is fine
          inputs_dict["output_attentions"] = True
          inputs_dict["output_hidden_states"] = True
          model = model_class(config)
          model.to(torch_device)
          model.eval()
          with torch.no_grad():
              outputs = model(
                  **self._prepare_for_class(inputs_dict, model_class)
              )
  
          added_hidden_states = 2
          self.assertEqual(out_len + added_hidden_states, len(outputs))
  
          self_attentions = (
              outputs.encoder_attentions
              if config.is_encoder_decoder
              else outputs.attentions
          )
  
          self.assertEqual(
              len(self_attentions), self.model_tester.num_hidden_layers
          )
          self.assertListEqual(
              list(self_attentions[0].shape[-3:]),
              [
                  self.model_tester.num_attention_heads,
                  subsampled_encoder_seq_length,
                  subsampled_encoder_key_length,
              ],
          )
  
imports: import copy
  import inspect
  import os
  import tempfile
  import unittest
  from transformers import Speech2TextConfig
  from transformers.testing_utils import is_torch_available, require_sentencepiece, require_tokenizers, require_torch, require_torch_fp16, require_torchaudio, slow, torch_device
  from transformers.utils import cached_property
  from ...generation.test_utils import GenerationTesterMixin
  from ...test_configuration_common import ConfigTester
  from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
  from ...test_pipeline_mixin import PipelineTesterMixin
label_desc: Use image node_id 15 for calling the Speech2TextModelTest obj's underlying member method code with example usage: obj.test_attention_outputs() without return types