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)
      visual_seq_len = getattr(
          self.model_tester, "visual_seq_length", None
      )
  
      encoder_seq_length = (seq_len if seq_len is not None else 0) + (
          visual_seq_len if visual_seq_len is not None else 0
      )
      encoder_key_length = getattr(
          self.model_tester, "key_length", encoder_seq_length
      )
      chunk_length = getattr(self.model_tester, "chunk_length", None)
      if chunk_length is not None and hasattr(
          self.model_tester, "num_hashes"
      ):
          encoder_seq_length = (
              encoder_seq_length * self.model_tester.num_hashes
          )
  
      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()
          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
          )
  
          if chunk_length is not None:
              self.assertListEqual(
                  list(attentions[0].shape[-4:]),
                  [
                      self.model_tester.num_attention_heads,
                      encoder_seq_length,
                      chunk_length,
                      encoder_key_length,
                  ],
              )
          else:
              self.assertListEqual(
                  list(attentions[0].shape[-3:]),
                  [
                      self.model_tester.num_attention_heads,
                      encoder_seq_length,
                      encoder_key_length,
                  ],
              )
          out_len = len(outputs)
  
          # 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)
              )
  
          if hasattr(self.model_tester, "num_hidden_states_types"):
              added_hidden_states = (
                  self.model_tester.num_hidden_states_types
              )
          elif self.is_encoder_decoder:
              added_hidden_states = 2
          else:
              added_hidden_states = 1
          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
          )
          if chunk_length is not None:
              self.assertListEqual(
                  list(self_attentions[0].shape[-4:]),
                  [
                      self.model_tester.num_attention_heads,
                      encoder_seq_length,
                      chunk_length,
                      encoder_key_length,
                  ],
              )
          else:
              self.assertListEqual(
                  list(self_attentions[0].shape[-3:]),
                  [
                      self.model_tester.num_attention_heads,
                      encoder_seq_length,
                      encoder_key_length,
                  ],
              )
  
imports: import copy
  import unittest
  from transformers import VisualBertConfig, is_torch_available
  from transformers.testing_utils import require_torch, slow, torch_device
  from ...test_configuration_common import ConfigTester
  from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
  from ...test_pipeline_mixin import PipelineTesterMixin
label_desc: Use image node_id 3 for calling the VisualBertModelTest obj's underlying member method code with example usage: obj.test_attention_outputs() without return types