class_docstr: Here we also overwrite some of the tests of test_modeling_common.py, as SAM's vision encoder does not use input_ids, inputs_embeds,
  attention_mask and seq_length.
class_docstr_tok: ["Here","we","also","overwrite","some","of","the","tests","of","test_modeling_common.py",",","as","SAM","'s","vision","encoder","does","not","use","input_ids",",","inputs_embeds",",","attention_mask","and","seq_length","."]
code: def test_attention_outputs(self):
      (
          config,
          inputs_dict,
      ) = self.model_tester.prepare_config_and_inputs_for_common()
      config.return_dict = True
  
      expected_vision_attention_shape = (
          self.model_tester.batch_size
          * self.model_tester.num_attention_heads,
          196,
          196,
      )
      expected_mask_decoder_attention_shape = (
          self.model_tester.batch_size,
          1,
          144,
          32,
      )
  
      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)
              )
  
          vision_attentions = outputs.vision_attentions
          self.assertEqual(
              len(vision_attentions),
              self.model_tester.num_hidden_layers,
          )
  
          mask_decoder_attentions = outputs.mask_decoder_attentions
          self.assertEqual(
              len(mask_decoder_attentions),
              self.model_tester.mask_decoder_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)
              )
          vision_attentions = outputs.vision_attentions
          self.assertEqual(
              len(vision_attentions),
              self.model_tester.num_hidden_layers,
          )
  
          mask_decoder_attentions = outputs.mask_decoder_attentions
          self.assertEqual(
              len(mask_decoder_attentions),
              self.model_tester.mask_decoder_tester.num_hidden_layers,
          )
  
          self.assertListEqual(
              list(vision_attentions[0].shape[-4:]),
              list(expected_vision_attention_shape),
          )
  
          self.assertListEqual(
              list(mask_decoder_attentions[0].shape[-4:]),
              list(expected_mask_decoder_attention_shape),
          )
  
imports: import gc
  import unittest
  import requests
  from transformers import SamConfig, SamMaskDecoderConfig, SamPromptEncoderConfig, SamVisionConfig, pipeline
  from transformers.testing_utils import backend_empty_cache, require_torch, 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
  from ...test_pipeline_mixin import PipelineTesterMixin
label_desc: Use image node_id 9 for calling the SamModelTest obj's underlying member method code with example usage: obj.test_attention_outputs() without return types