MCPcopy Create free account
hub / github.com/SooLab/CGFormer / resize_token_embeddings

Method resize_token_embeddings

bert/modeling_utils.py:376–401  ·  view source on GitHub ↗

Resize input token embeddings matrix of the model if new_num_tokens != config.vocab_size. Take care of tying weights embeddings afterwards if the model class has a `tie_weights()` method. Arguments: new_num_tokens: (`optional`) int: New number of tokens

(self, new_num_tokens: Optional[int] = None)

Source from the content-addressed store, hash-verified

374 output_embeddings.out_features = input_embeddings.num_embeddings
375
376 def resize_token_embeddings(self, new_num_tokens: Optional[int] = None):
377 """ Resize input token embeddings matrix of the model if new_num_tokens != config.vocab_size.
378 Take care of tying weights embeddings afterwards if the model class has a `tie_weights()` method.
379
380 Arguments:
381
382 new_num_tokens: (`optional`) int:
383 New number of tokens in the embedding matrix. Increasing the size will add newly initialized vectors at the end. Reducing the size will remove vectors from the end.
384 If not provided or None: does nothing and just returns a pointer to the input tokens ``torch.nn.Embeddings`` Module of the model.
385
386 Return: ``torch.nn.Embeddings``
387 Pointer to the input tokens Embeddings Module of the model
388 """
389 base_model = getattr(self, self.base_model_prefix, self) # get the base model if needed
390 model_embeds = base_model._resize_token_embeddings(new_num_tokens)
391 if new_num_tokens is None:
392 return model_embeds
393
394 # Update base model and current model config
395 self.config.vocab_size = new_num_tokens
396 base_model.vocab_size = new_num_tokens
397
398 # Tie weights again if needed
399 self.tie_weights()
400
401 return model_embeds
402
403 def _resize_token_embeddings(self, new_num_tokens):
404 old_embeddings = self.get_input_embeddings()

Callers

nothing calls this directly

Calls 2

tie_weightsMethod · 0.95

Tested by

no test coverage detected