Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/transformers/modeling_tf_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,11 @@ def resize_token_embeddings(self, new_num_tokens=None) -> tf.Variable:
return model_embeds

def _get_word_embedding_weight(model, embedding_layer):
# If the variable holds the weights themselves, return them
if isinstance(embedding_layer, tf.Tensor):
return embedding_layer
# Otherwise, try to get them from the layer's attributes

embeds = getattr(embedding_layer, "weight", None)
if embeds is not None:
return embeds
Expand Down
12 changes: 11 additions & 1 deletion tests/test_modeling_tf_mt5.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,17 @@
if is_tf_available():
import tensorflow as tf

from transformers import AutoTokenizer, TFAutoModelForSeq2SeqLM
from transformers import AutoTokenizer, T5Tokenizer, TFAutoModelForSeq2SeqLM, TFMT5ForConditionalGeneration


@require_tf
class TFMT5ModelTest(unittest.TestCase): # no mixin with common tests -> most cases are already covered in the TF T5
@slow
def test_resize_embeddings(self):
model = TFMT5ForConditionalGeneration.from_pretrained("google/mt5-small")
tokenizer = T5Tokenizer.from_pretrained("google/mt5-small")
tokenizer.add_special_tokens({"bos_token": "", "eos_token": ""})
model._resize_token_embeddings(len(tokenizer))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this test a bit more aggressive. There is no assert statement here. Could we check, e.g. that len(tokenizer) matches the model?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, there was no check with respect to whether the resizing was actually happening. Added more asserts that confirm that it is the case 👍



@require_tf
Expand Down
7 changes: 7 additions & 0 deletions tests/test_modeling_tf_t5.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,13 @@ def test_generate_with_headmasking(self):
# TODO: Fix head-masking according to PyTorch T5 model
pass

@slow
def test_resize_embeddings(self):
model = TFT5ForConditionalGeneration.from_pretrained("t5-small")
tokenizer = T5Tokenizer.from_pretrained("t5-small")
tokenizer.add_special_tokens({"bos_token": "", "eos_token": ""})
model._resize_token_embeddings(len(tokenizer))


class TFT5EncoderOnlyModelTester:
def __init__(
Expand Down