Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

update decoder_vocab_size when resizing embeds #16700

Merged
merged 1 commit into from
Apr 11, 2022
Merged
Changes from all 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
14 changes: 8 additions & 6 deletions src/transformers/models/marian/modeling_marian.py
Original file line number Diff line number Diff line change
Expand Up @@ -1280,11 +1280,9 @@ def __init__(self, config: MarianConfig):
super().__init__(config)
self.model = MarianModel(config)

self.target_vocab_size = (
config.vocab_size if config.share_encoder_decoder_embeddings else config.decoder_vocab_size
)
self.register_buffer("final_logits_bias", torch.zeros((1, self.target_vocab_size)))
self.lm_head = nn.Linear(config.d_model, self.target_vocab_size, bias=False)
target_vocab_size = config.vocab_size if config.share_encoder_decoder_embeddings else config.decoder_vocab_size
self.register_buffer("final_logits_bias", torch.zeros((1, target_vocab_size)))
self.lm_head = nn.Linear(config.d_model, target_vocab_size, bias=False)

# Initialize weights and apply final processing
self.post_init()
Expand All @@ -1306,6 +1304,10 @@ def _resize_token_embeddings(self, new_num_tokens: int) -> nn.Embedding:
new_embeddings = self._get_resized_embeddings(old_embeddings, new_num_tokens)
self.set_input_embeddings(new_embeddings)

# update config.decoder_vocab_size if embeddings are tied
if self.config.share_encoder_decoder_embeddings:
self.config.decoder_vocab_size = new_num_tokens

# if word embeddings are not tied, make sure that lm head is resized as well
if (
self.config.share_encoder_decoder_embeddings
Expand Down Expand Up @@ -1451,7 +1453,7 @@ def forward(
masked_lm_loss = None
if labels is not None:
loss_fct = CrossEntropyLoss()
masked_lm_loss = loss_fct(lm_logits.view(-1, self.target_vocab_size), labels.view(-1))
masked_lm_loss = loss_fct(lm_logits.view(-1, self.config.decoder_vocab_size), labels.view(-1))

if not return_dict:
output = (lm_logits,) + outputs[1:]
Expand Down