code: def test_retain_grad_hidden_states_attentions(self):
      (
          config,
          inputs_dict,
      ) = self.model_tester.prepare_config_and_inputs_for_common()
      config.output_hidden_states = True
      config.output_attentions = self.has_attentions
  
      # no need to test all models as different heads yield the same functionality
      model_class = self.all_model_classes[0]
      model = model_class(config)
      model.to(torch_device)
  
      inputs = self._prepare_for_class(inputs_dict, model_class)
  
      outputs = model(**inputs)
  
      output = outputs[0]
  
      # Encoder-/Decoder-only models
      hidden_states = outputs.hidden_states[0][0]
      hidden_states.retain_grad()
  
      if self.has_attentions:
          attentions = outputs.attentions[0][0]
          attentions.retain_grad()
  
      output.flatten()[0].backward(retain_graph=True)
  
      self.assertIsNotNone(hidden_states.grad)
  
      if self.has_attentions:
          self.assertIsNotNone(attentions.grad)
  
imports: import tempfile
  import unittest
  import numpy
  from transformers import BridgeTowerConfig, BridgeTowerTextConfig, BridgeTowerVisionConfig, is_torch_available, is_vision_available
  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, _config_zero_init, floats_tensor, ids_tensor, random_attention_mask
  from ...test_pipeline_mixin import PipelineTesterMixin
label_desc: Use image node_id 14 for calling the BridgeTowerModelTest obj's underlying member method code with example usage: obj.test_retain_grad_hidden_states_attentions() without return types