@@ -669,11 +669,6 @@ def __init__(self, args, dictionary, embed_tokens, no_encoder_attn=False):
669669 factor = args .adaptive_softmax_factor ,
670670 tie_proj = args .tie_adaptive_proj ,
671671 )
672- elif not self .share_input_output_embed :
673- self .embed_out = nn .Parameter (
674- torch .Tensor (len (dictionary ), self .output_embed_dim )
675- )
676- nn .init .normal_ (self .embed_out , mean = 0 , std = self .output_embed_dim ** - 0.5 )
677672
678673 if args .decoder_normalize_before and not getattr (
679674 args , "no_decoder_final_norm" , False
@@ -686,6 +681,16 @@ def __init__(self, args, dictionary, embed_tokens, no_encoder_attn=False):
686681 else :
687682 self .layernorm_embedding = None
688683
684+ if self .share_input_output_embed :
685+ self .output_projection = nn .Linear (
686+ self .embed_tokens .weight .shape [1 ], self .embed_tokens .weight .shape [0 ], bias = False
687+ )
688+ else :
689+ self .output_projection = nn .Linear (
690+ self .output_embed_dim , len (dictionary ), bias = False
691+ )
692+ nn .init .normal_ (self .output_projection .weight , mean = 0 , std = self .output_embed_dim ** - 0.5 )
693+
689694 def build_decoder_layer (self , args , no_encoder_attn = False ):
690695 return TransformerDecoderLayer (args , no_encoder_attn )
691696
@@ -852,10 +857,7 @@ def output_layer(self, features):
852857 """Project features to the vocabulary size."""
853858 if self .adaptive_softmax is None :
854859 # project back to size of vocabulary
855- if self .share_input_output_embed :
856- return F .linear (features , self .embed_tokens .weight )
857- else :
858- return F .linear (features , self .embed_out )
860+ return self .output_projection (features )
859861 else :
860862 return features
861863
@@ -890,6 +892,18 @@ def upgrade_state_dict_named(self, state_dict, name):
890892 "{}.embed_positions._float_tensor" .format (name )
891893 ] = torch .FloatTensor (1 )
892894
895+ embed_tokens_weights_key = f"{ name } .embed_tokens.weights"
896+ embed_out_key = f"{ name } .embed_out"
897+ if embed_tokens_weights_key in state_dict :
898+ state_dict [f"{ name } .output_projection.weight" ] = state_dict [
899+ embed_tokens_weights_key
900+ ]
901+ if embed_out_key in state_dict :
902+ state_dict [f"{ name } .output_projection.weight" ] = state_dict [
903+ embed_out_key
904+ ]
905+ del state_dict [embed_out_key ]
906+
893907 for i in range (self .num_layers ):
894908 # update layer norms
895909 layer_norm_map = {
0 commit comments