code: def test_resize_tokens_embeddings(self):
      (
          original_config,
          inputs_dict,
      ) = self.model_tester.prepare_config_and_inputs_for_common()
      if not self.test_resize_embeddings:
          return
  
      for model_class in self.all_model_classes:
          config = copy.deepcopy(original_config)
          model = model_class(config)
          model.to(torch_device)
  
          if self.model_tester.is_training is False:
              model.eval()
  
          model_vocab_size = config.vocab_size
          # Retrieve the embeddings and clone theme
          model_embed = model.resize_token_embeddings(model_vocab_size)
          cloned_embeddings = [
              emb.weight.clone() for emb in model_embed.emb_layers
          ]
          # Retrieve the cutoffs and copy them
          copied_cutoffs = copy.copy(model_embed.cutoffs)
  
          test_layers = [x for x in range(config.div_val)]
          for layer in test_layers:
              # Check that resizing the token embeddings with a larger vocab size increases the model's vocab size
              model_embed = model.resize_token_embeddings(
                  model_vocab_size + 10, layer
              )
              self.assertEqual(
                  model.config.vocab_size, model_vocab_size + 10
              )
              # Check that it actually resizes the embeddings matrix
              self.assertEqual(
                  model_embed.emb_layers[layer].weight.shape[0],
                  cloned_embeddings[layer].shape[0] + 10,
              )
              # Check that the cutoffs were modified accordingly
              self.check_cutoffs_and_n_token(
                  copied_cutoffs,
                  layer,
                  model_embed,
                  model,
                  model_class,
                  10,
                  model_vocab_size,
              )
  
              # Check that the model can still do a forward pass successfully (every parameter should be resized)
              model(**inputs_dict)
  
              # Check that resizing the token embeddings with a smaller vocab size decreases the model's vocab size
              model_embed = model.resize_token_embeddings(
                  model_vocab_size - 5, layer
              )
              self.assertEqual(
                  model.config.vocab_size, model_vocab_size - 5
              )
              # Check that it actually resizes the embeddings matrix
              self.assertEqual(
                  model_embed.emb_layers[layer].weight.shape[0],
                  cloned_embeddings[layer].shape[0] - 5,
              )
              # Check that the cutoffs were modified accordingly
              self.check_cutoffs_and_n_token(
                  copied_cutoffs,
                  layer,
                  model_embed,
                  model,
                  model_class,
                  -5,
                  model_vocab_size,
              )
  
              # Check that the model can still do a forward pass successfully (every parameter should be resized)
              # Input ids should be clamped to the maximum size of the vocabulary
              inputs_dict["input_ids"].clamp_(
                  max=model_vocab_size - 5 - 1
              )
              model(**inputs_dict)
  
              # Check that adding and removing tokens has not modified the first part of the embedding matrix.
              models_equal = True
              for p1, p2 in zip(
                  cloned_embeddings[layer],
                  model_embed.emb_layers[layer].weight,
              ):
                  if p1.data.ne(p2.data).sum() > 0:
                      models_equal = False
  
              self.assertTrue(models_equal)
  
              # Reset model embeddings to original size
              model.resize_token_embeddings(model_vocab_size, layer)
              self.assertEqual(
                  model_vocab_size, model.config.vocab_size
              )
              self.assertEqual(
                  model_embed.emb_layers[layer].weight.shape[0],
                  cloned_embeddings[layer].shape[0],
              )
  
imports: import copy
  import random
  import unittest
  from transformers import TransfoXLConfig, is_torch_available
  from transformers.testing_utils import require_torch, require_torch_multi_gpu, slow, torch_device
  from ...generation.test_generation_utils import GenerationTesterMixin
  from ...test_configuration_common import ConfigTester
  from ...test_modeling_common import ModelTesterMixin, ids_tensor
label_desc: Use image node_id 10 for calling the TransfoXLModelTest obj's underlying member method code with example usage: obj.test_resize_tokens_embeddings() without return types