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 equals num_hidden_layers of the deep encoder + 1, + 2 for the first shallow encoder, + 2
          # for the final shallow encoder
          expected_num_layers = (
              self.model_tester.num_hidden_layers + 1 + 2 + 2
          )
          self.assertEqual(len(hidden_states), expected_num_layers)
  
          seq_length = self.model_tester.seq_length
          for i in range(expected_num_layers):
              if (i < 2) or ((expected_num_layers - i) < 3):
                  # the expected length of the hidden_states of the first and final shallow encoders
                  # is equal to the seq_length
                  self.assertListEqual(
                      list(hidden_states[i].shape[-2:]),
                      [seq_length, self.model_tester.hidden_size],
                  )
              else:
                  # the expected length of the hidden_states of the deep encoder need to be updated
                  # for CANINE since the seq length is downsampled
                  self.assertListEqual(
                      list(hidden_states[i].shape[-2:]),
                      [
                          seq_length // config.downsampling_rate,
                          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 unittest
  from typing import List, Tuple
  from transformers import CanineConfig, is_torch_available
  from transformers.testing_utils import require_torch, slow, torch_device
  from ...test_configuration_common import ConfigTester
  from ...test_modeling_common import ModelTesterMixin, _config_zero_init, global_rng, ids_tensor, random_attention_mask
  from ...test_pipeline_mixin import PipelineTesterMixin
label_desc: Use image node_id 8 for calling the CanineModelTest obj's underlying member method code with example usage: obj.test_hidden_states_output() without return types