Skip to content

Commit

Permalink
TF: GPT2 with native embedding layers (huggingface#23436)
Browse files Browse the repository at this point in the history
  • Loading branch information
gante authored and novice03 committed Jun 23, 2023
1 parent feebafb commit 0446ded
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
3 changes: 0 additions & 3 deletions docs/source/en/internal/modeling_utils.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ Most of those are only useful if you are studying the code of the models in the

[[autodoc]] modeling_tf_utils.TFConv1D

[[autodoc]] modeling_tf_utils.TFSharedEmbeddings
- call

[[autodoc]] modeling_tf_utils.TFSequenceSummary

## TensorFlow loss functions
Expand Down
4 changes: 4 additions & 0 deletions src/transformers/modeling_tf_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3132,6 +3132,10 @@ def __init__(self, vocab_size: int, hidden_size: int, initializer_range: Optiona
self.vocab_size = vocab_size
self.hidden_size = hidden_size
self.initializer_range = hidden_size**-0.5 if initializer_range is None else initializer_range
warnings.warn(
"`TFSharedEmbeddings` is scheduled for deletion in v4.32, use `tf.keras.layers.Embedding` instead.",
DeprecationWarning,
)

def build(self, input_shape):
"""
Expand Down
39 changes: 18 additions & 21 deletions src/transformers/models/gpt2/modeling_tf_gpt2.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
TFPreTrainedModel,
TFSequenceClassificationLoss,
TFSequenceSummary,
TFSharedEmbeddings,
get_initializer,
keras_serializable,
unpack_inputs,
Expand Down Expand Up @@ -315,29 +314,27 @@ def __init__(self, config, *inputs, **kwargs):
self.n_positions = config.n_positions
self.initializer_range = config.initializer_range

self.wte = TFSharedEmbeddings(
config.vocab_size, config.hidden_size, initializer_range=config.initializer_range, name="wte"
self.wte = tf.keras.layers.Embedding(
input_dim=config.vocab_size,
output_dim=config.hidden_size,
embeddings_initializer=get_initializer(config.initializer_range),
name="wte",
)
self.wpe = tf.keras.layers.Embedding(
input_dim=config.n_positions,
output_dim=config.n_embd,
embeddings_initializer=get_initializer(config.initializer_range),
name="wpe",
)
self.drop = tf.keras.layers.Dropout(config.embd_pdrop)
self.h = [TFBlock(config, scale=True, name=f"h_._{i}") for i in range(config.n_layer)]
self.ln_f = tf.keras.layers.LayerNormalization(epsilon=config.layer_norm_epsilon, name="ln_f")

def build(self, input_shape):
with tf.name_scope("wpe"):
self.wpe = self.add_weight(
name="embeddings",
shape=[self.n_positions, self.n_embd],
initializer=get_initializer(self.initializer_range),
)

super().build(input_shape)

def get_input_embeddings(self):
return self.wte

def set_input_embeddings(self, value):
self.wte.weight = value
self.wte.vocab_size = shape_list(value)[0]
def set_input_embeddings(self, new_embeddings):
self.wte = new_embeddings

def _prune_heads(self, heads_to_prune):
"""
Expand Down Expand Up @@ -438,13 +435,13 @@ def call(

if inputs_embeds is None:
check_embeddings_within_bounds(input_ids, self.config.vocab_size)
inputs_embeds = self.wte(input_ids, mode="embedding")
inputs_embeds = self.wte(input_ids)

position_embeds = tf.gather(self.wpe, position_ids)
position_embeds = self.wpe(position_ids)

if token_type_ids is not None:
token_type_ids = tf.reshape(token_type_ids, [-1, shape_list(token_type_ids)[-1]])
token_type_embeds = self.wte(token_type_ids, mode="embedding")
token_type_embeds = self.wte(token_type_ids)
else:
token_type_embeds = tf.constant(0.0)

Expand Down Expand Up @@ -904,7 +901,7 @@ def call(
training=training,
)
hidden_states = transformer_outputs[0]
logits = self.transformer.wte(hidden_states, mode="linear")
logits = tf.matmul(hidden_states, self.transformer.wte.weights, transpose_b=True)

loss = None
if labels is not None:
Expand Down Expand Up @@ -1048,7 +1045,7 @@ def call(
all_hidden_states = transformer_outputs.hidden_states[:-1] + (hidden_states,)
else:
all_hidden_states = None
lm_logits = self.transformer.wte(hidden_states, mode="linear")
lm_logits = tf.matmul(hidden_states, self.transformer.wte.weights, transpose_b=True)
mc_logits = self.multiple_choice_head(hidden_states, mc_token_ids, training=training)
mc_logits = tf.squeeze(mc_logits, axis=-1)

Expand Down

0 comments on commit 0446ded

Please sign in to comment.