code: def test_hidden_states_output(self):
      def check_hidden_states_output(inputs_dict, config, model_class):
          model = model_class(config)
          model.to(torch_device)
          model.eval()
  
          with torch.no_grad():
              outputs = model(
                  **self._prepare_for_class(inputs_dict, model_class)
              )
  
          hidden_states = outputs.hidden_states
  
          expected_num_layers = getattr(
              self.model_tester,
              "expected_num_hidden_layers",
              self.model_tester.num_hidden_layers + 1,
          )
          self.assertEqual(len(hidden_states), expected_num_layers)
  
          # LayoutLMv2 has a different expected sequence length
          expected_seq_len = (
              self.model_tester.seq_length
              + self.model_tester.image_feature_pool_shape[0]
              * self.model_tester.image_feature_pool_shape[1]
          )
  
          self.assertListEqual(
              list(hidden_states[0].shape[-2:]),
              [expected_seq_len, self.model_tester.hidden_size],
          )
  
      (
          config,
          inputs_dict,
      ) = self.model_tester.prepare_config_and_inputs_for_common()
  
      for model_class in self.all_model_classes:
          inputs_dict["output_hidden_states"] = True
          check_hidden_states_output(inputs_dict, config, model_class)
  
          # check that output_hidden_states also work using config
          del inputs_dict["output_hidden_states"]
          config.output_hidden_states = True
  
          check_hidden_states_output(inputs_dict, config, model_class)
  
imports: import os
  import random
  import tempfile
  import unittest
  from transformers.testing_utils import require_detectron2, require_torch, require_torch_multi_gpu, slow, torch_device
  from transformers.utils import is_detectron2_available, is_torch_available
  from ...test_configuration_common import ConfigTester
  from ...test_modeling_common import ModelTesterMixin, _config_zero_init, ids_tensor, random_attention_mask
label_desc: Use image node_id 11 for calling the LayoutLMv2ModelTest obj's underlying member method code with example usage: obj.test_hidden_states_output() without return types