code: def test_resize_token_embeddings(self):
      (
          config,
          inputs_dict,
      ) = self.model_tester.prepare_config_and_inputs_for_common()
  
      def _get_word_embedding_weight(model, embedding_layer):
          if hasattr(embedding_layer, "weight"):
              return embedding_layer.weight
          else:
              # Here we build the word embeddings weights if not exists.
              # And then we retry to get the attribute once built.
              model(model.dummy_inputs)
              if hasattr(embedding_layer, "weight"):
                  return embedding_layer.weight
              else:
                  return None
  
      for model_class in self.all_model_classes:
          for size in [config.vocab_size - 10, config.vocab_size + 10]:
              # build the embeddings
              model = model_class(config=config)
              old_input_embeddings = _get_word_embedding_weight(
                  model, model.get_input_embeddings()
              )
              old_output_embeddings = _get_word_embedding_weight(
                  model, model.get_output_embeddings()
              )
  
              # reshape the embeddings
              model.resize_token_embeddings(size)
              new_input_embeddings = _get_word_embedding_weight(
                  model, model.get_input_embeddings()
              )
              new_output_embeddings = _get_word_embedding_weight(
                  model, model.get_output_embeddings()
              )
  
              # check that the resized embeddings size matches the desired size.
              assert_size = (
                  size if size is not None else config.vocab_size
              )
  
              self.assertEqual(
                  new_input_embeddings.shape[0], assert_size
              )
  
              # check that weights remain the same after resizing
              models_equal = True
              for p1, p2 in zip(
                  old_input_embeddings.value(),
                  new_input_embeddings.value(),
              ):
                  if tf.math.reduce_sum(tf.math.abs(p1 - p2)) > 0:
                      models_equal = False
              self.assertTrue(models_equal)
  
              if (
                  old_output_embeddings is not None
                  and new_output_embeddings is not None
              ):
                  self.assertEqual(
                      new_output_embeddings.shape[0], assert_size
                  )
  
                  models_equal = True
                  for p1, p2 in zip(
                      old_output_embeddings.value(),
                      new_output_embeddings.value(),
                  ):
                      if tf.math.reduce_sum(tf.math.abs(p1 - p2)) > 0:
                          models_equal = False
                  self.assertTrue(models_equal)
  
imports: import unittest
  import numpy
  from transformers import OPTConfig, is_tf_available
  from transformers.testing_utils import require_sentencepiece, require_tf, slow, tooslow
  from ...test_configuration_common import ConfigTester
  from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor
label_desc: Use image node_id 5 for calling the TFOPTModelTest obj's underlying member method code with example usage: obj.test_resize_token_embeddings() and returns: embedding_layer, embedding_layer, None