Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
chenpangpang
transformers
Commits
1471857f
Unverified
Commit
1471857f
authored
Apr 11, 2022
by
Suraj Patil
Committed by
GitHub
Apr 11, 2022
Browse files
update decoder_vocab_size when resizing embeds (#16700)
parent
5e686757
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
8 additions
and
6 deletions
+8
-6
src/transformers/models/marian/modeling_marian.py
src/transformers/models/marian/modeling_marian.py
+8
-6
No files found.
src/transformers/models/marian/modeling_marian.py
View file @
1471857f
...
...
@@ -1280,11 +1280,9 @@ class MarianMTModel(MarianPreTrainedModel):
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
()
...
...
@@ -1306,6 +1304,10 @@ class MarianMTModel(MarianPreTrainedModel):
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
...
...
@@ -1451,7 +1453,7 @@ class MarianMTModel(MarianPreTrainedModel):
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
:]
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment