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, "expected_seq_len", None)
  
      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.attentions
          if (
              model_class.__name__
              == "ViltForImagesAndTextClassification"
          ):
              # attentions are a list of length num_images
              # each element contains the attentions of a particular image index
              self.assertEqual(
                  len(attentions), self.model_tester.num_images
              )
              self.assertEqual(
                  len(attentions[0]),
                  self.model_tester.num_hidden_layers,
              )
          else:
              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.attentions
          if (
              model_class.__name__
              == "ViltForImagesAndTextClassification"
          ):
              # attentions are a list of length num_images
              # each element contains the attentions of a particular image index
              self.assertEqual(
                  len(attentions), self.model_tester.num_images
              )
              self.assertEqual(
                  len(attentions[0]),
                  self.model_tester.num_hidden_layers,
              )
          else:
              self.assertEqual(
                  len(attentions), self.model_tester.num_hidden_layers
              )
  
          if (
              model_class.__name__
              == "ViltForImagesAndTextClassification"
          ):
              self.assertListEqual(
                  list(attentions[0][0].shape[-3:]),
                  [
                      self.model_tester.num_attention_heads,
                      seq_len,
                      seq_len,
                  ],
              )
          else:
              self.assertListEqual(
                  list(attentions[0].shape[-3:]),
                  [
                      self.model_tester.num_attention_heads,
                      seq_len,
                      seq_len,
                  ],
              )
          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)
              )
  
          self.assertEqual(out_len + 1, len(outputs))
  
          self_attentions = (
              outputs.encoder_attentions
              if config.is_encoder_decoder
              else outputs.attentions
          )
  
          if (
              model_class.__name__
              == "ViltForImagesAndTextClassification"
          ):
              self.assertEqual(
                  len(self_attentions), self.model_tester.num_images
              )
              self.assertEqual(
                  len(self_attentions[0]),
                  self.model_tester.num_hidden_layers,
              )
              self.assertListEqual(
                  list(self_attentions[0][0].shape[-3:]),
                  [
                      self.model_tester.num_attention_heads,
                      seq_len,
                      seq_len,
                  ],
              )
          else:
              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,
                      seq_len,
                      seq_len,
                  ],
              )
  
imports: import unittest
  from datasets import load_dataset
  from packaging import version
  from transformers import ViltConfig, is_torch_available, is_vision_available
  from transformers.models.auto import get_values
  from transformers.testing_utils import require_torch, require_vision, slow, torch_device
  from transformers.utils import cached_property
  from ...test_configuration_common import ConfigTester
  from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
label_desc: Use image node_id 11 for calling the ViltModelTest obj's underlying member method code with example usage: obj.test_attention_outputs() without return types