TF: final bias as a layer in seq2seq models (replicate TFMarian fix) (#18903)
This commit is contained in:
@@ -2806,6 +2806,24 @@ class TF{{cookiecutter.camelcase_modelname}}Model(TF{{cookiecutter.camelcase_mod
|
||||
)
|
||||
|
||||
|
||||
# Copied from transformers.models.bart.modeling_tf_bart.BiasLayer
|
||||
class BiasLayer(tf.keras.layers.Layer):
|
||||
"""
|
||||
Bias as a layer. It is used for serialization purposes: `tf.keras.Model.save_weights` stores on a per-layer basis,
|
||||
so all weights have to be registered in a layer.
|
||||
"""
|
||||
|
||||
def __init__(self, shape, initializer, trainable, name, **kwargs):
|
||||
super().__init__(name=name, **kwargs)
|
||||
# Note: the name of this variable will NOT be scoped when serialized, i.e. it will not be in the format of
|
||||
# "outer_layer/inner_layer/.../name:0". Instead, it will be "name:0". For further details, see:
|
||||
# https://github.com/huggingface/transformers/pull/18833#issuecomment-1233090214
|
||||
self.bias = self.add_weight(name=name, shape=shape, initializer=initializer, trainable=trainable)
|
||||
|
||||
def call(self, x):
|
||||
return x + self.bias
|
||||
|
||||
|
||||
@add_start_docstrings(
|
||||
"The {{cookiecutter.uppercase_modelname}} Model with a language modeling head. Can be used for summarization.",
|
||||
{{cookiecutter.uppercase_modelname}}_START_DOCSTRING,
|
||||
@@ -2822,9 +2840,10 @@ class TF{{cookiecutter.camelcase_modelname}}ForConditionalGeneration(TF{{cookiec
|
||||
self.model._set_save_spec(inputs=self.serving.input_signature)
|
||||
self.use_cache = config.use_cache
|
||||
# final_bias_logits is registered as a buffer in pytorch, so not trainable for the sake of consistency.
|
||||
self.final_logits_bias = self.add_weight(
|
||||
self.bias_layer = BiasLayer(
|
||||
name="final_logits_bias", shape=[1, config.vocab_size], initializer="zeros", trainable=False
|
||||
)
|
||||
self.final_logits_bias = self.bias_layer.bias # alias to keep the same interface with PT
|
||||
|
||||
def get_decoder(self):
|
||||
return self.model.decoder
|
||||
@@ -2911,7 +2930,7 @@ class TF{{cookiecutter.camelcase_modelname}}ForConditionalGeneration(TF{{cookiec
|
||||
training=training
|
||||
)
|
||||
lm_logits = self.model.shared(outputs[0], mode="linear")
|
||||
lm_logits = lm_logits + self.final_logits_bias
|
||||
lm_logits = self.bias_layer(lm_logits)
|
||||
masked_lm_loss = None if labels is None else self.hf_compute_loss(labels, lm_logits)
|
||||
|
||||
if not return_dict:
|
||||
|
||||
Reference in New Issue
Block a user