code: def test_attention_outputs(self):
      seq_len = getattr(self.model_tester, "num_latents", None)
  
      for model_class in self.all_model_classes:
          (
              config,
              inputs_dict,
          ) = self.model_tester.prepare_config_and_inputs_for_model_class(
              model_class
          )
          config.return_dict = True
  
          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)
              )
          self_attentions = outputs.attentions
          cross_attentions = outputs.cross_attentions
  
          # check expected number of attentions depending on model class
          expected_num_self_attentions = (
              self.model_tester.num_blocks
              * self.model_tester.num_self_attends_per_block
          )
          if model.__class__.__name__ == "PerceiverModel":
              # we expect to have 2 cross-attentions, namely one in the PerceiverEncoder, and one in PerceiverBasicDecoder
              expected_num_cross_attentions = 1
          else:
              # we expect to have 2 cross-attentions, namely one in the PerceiverEncoder, and one in PerceiverBasicDecoder
              expected_num_cross_attentions = 2
          self.assertEqual(
              len(self_attentions), expected_num_self_attentions
          )
          self.assertEqual(
              len(cross_attentions), expected_num_cross_attentions
          )
  
          # 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)
              )
          self_attentions = outputs.attentions
          cross_attentions = outputs.cross_attentions
          self.assertEqual(
              len(self_attentions), expected_num_self_attentions
          )
          self.assertEqual(
              len(cross_attentions), expected_num_cross_attentions
          )
  
          self.assertListEqual(
              list(self_attentions[0].shape[-3:]),
              [
                  self.model_tester.num_self_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.attentions
  
          self.assertEqual(
              len(self_attentions), expected_num_self_attentions
          )
          self.assertListEqual(
              list(self_attentions[0].shape[-3:]),
              [
                  self.model_tester.num_self_attention_heads,
                  seq_len,
                  seq_len,
              ],
          )
  
imports: import copy
  import inspect
  import math
  import tempfile
  import unittest
  import warnings
  from typing import Dict, List, Tuple
  import numpy
  from datasets import load_dataset
  from transformers import PerceiverConfig
  from transformers.models.auto import get_values
  from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
  from transformers.utils import is_torch_available, is_vision_available
  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 13 for calling the PerceiverModelTest obj's underlying member method code with example usage: obj.test_attention_outputs() without return types