"git@developer.sourcefind.cn:chenpangpang/transformers.git" did not exist on "6a7b9da2aece53f40512c5c1b43c71303a970de8"
Unverified Commit cf9e7cb0 authored by Joao Gante's avatar Joao Gante Committed by GitHub
Browse files

TF: embeddings out of bounds check factored into function (#23427)

parent 45e3d649
...@@ -33,7 +33,7 @@ from ...modeling_tf_utils import ( ...@@ -33,7 +33,7 @@ from ...modeling_tf_utils import (
keras_serializable, keras_serializable,
unpack_inputs, unpack_inputs,
) )
from ...tf_utils import shape_list, stable_softmax from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax
from ...utils import ( from ...utils import (
ModelOutput, ModelOutput,
add_start_docstrings, add_start_docstrings,
...@@ -572,16 +572,7 @@ class TFGroupViTTextEmbeddings(tf.keras.layers.Layer): ...@@ -572,16 +572,7 @@ class TFGroupViTTextEmbeddings(tf.keras.layers.Layer):
raise ValueError("You have to specify either input_ids or inputs_embeds") raise ValueError("You have to specify either input_ids or inputs_embeds")
if inputs_embeds is None: if inputs_embeds is None:
# Note: tf.gather, on which the embedding layer is based, won't check positive out of bound check_embeddings_within_bounds(input_ids, self.config.vocab_size)
# indices on GPU, returning zeros instead. This is a dangerous silent behavior.
tf.debugging.assert_less(
input_ids,
tf.cast(self.config.vocab_size, dtype=input_ids.dtype),
message=(
"input_ids must be smaller than the embedding layer's input dimension (got"
f" {tf.math.reduce_max(input_ids)} >= {self.config.vocab_size})"
),
)
inputs_embeds = tf.gather(params=self.weight, indices=input_ids) inputs_embeds = tf.gather(params=self.weight, indices=input_ids)
input_shape = shape_list(inputs_embeds)[:-1] input_shape = shape_list(inputs_embeds)[:-1]
......
...@@ -41,7 +41,7 @@ from ...modeling_tf_utils import ( ...@@ -41,7 +41,7 @@ from ...modeling_tf_utils import (
keras_serializable, keras_serializable,
unpack_inputs, unpack_inputs,
) )
from ...tf_utils import shape_list, stable_softmax from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax
from ...utils import add_start_docstrings, add_start_docstrings_to_model_forward, logging, replace_return_docstrings from ...utils import add_start_docstrings, add_start_docstrings_to_model_forward, logging, replace_return_docstrings
from .configuration_layoutlm import LayoutLMConfig from .configuration_layoutlm import LayoutLMConfig
...@@ -140,16 +140,7 @@ class TFLayoutLMEmbeddings(tf.keras.layers.Layer): ...@@ -140,16 +140,7 @@ class TFLayoutLMEmbeddings(tf.keras.layers.Layer):
assert not (input_ids is None and inputs_embeds is None) assert not (input_ids is None and inputs_embeds is None)
if input_ids is not None: if input_ids is not None:
# Note: tf.gather, on which the embedding layer is based, won't check positive out of bound check_embeddings_within_bounds(input_ids, self.config.vocab_size)
# indices on GPU, returning zeros instead. This is a dangerous silent behavior.
tf.debugging.assert_less(
input_ids,
tf.cast(self.config.vocab_size, dtype=input_ids.dtype),
message=(
"input_ids must be smaller than the embedding layer's input dimension (got"
f" {tf.math.reduce_max(input_ids)} >= {self.config.vocab_size})"
),
)
inputs_embeds = tf.gather(params=self.weight, indices=input_ids) inputs_embeds = tf.gather(params=self.weight, indices=input_ids)
input_shape = shape_list(inputs_embeds)[:-1] input_shape = shape_list(inputs_embeds)[:-1]
......
...@@ -36,6 +36,7 @@ from ...modeling_tf_utils import ( ...@@ -36,6 +36,7 @@ from ...modeling_tf_utils import (
keras_serializable, keras_serializable,
unpack_inputs, unpack_inputs,
) )
from ...tf_utils import check_embeddings_within_bounds
from ...utils import add_start_docstrings, add_start_docstrings_to_model_forward, replace_return_docstrings from ...utils import add_start_docstrings, add_start_docstrings_to_model_forward, replace_return_docstrings
from .configuration_layoutlmv3 import LayoutLMv3Config from .configuration_layoutlmv3 import LayoutLMv3Config
...@@ -240,16 +241,7 @@ class TFLayoutLMv3TextEmbeddings(tf.keras.layers.Layer): ...@@ -240,16 +241,7 @@ class TFLayoutLMv3TextEmbeddings(tf.keras.layers.Layer):
token_type_ids = tf.zeros(input_shape, dtype=position_ids.dtype) token_type_ids = tf.zeros(input_shape, dtype=position_ids.dtype)
if inputs_embeds is None: if inputs_embeds is None:
# Note: tf.gather, on which the embedding layer is based, won't check positive out of bound check_embeddings_within_bounds(input_ids, self.word_embeddings.input_dim)
# indices on GPU, returning zeros instead. This is a dangerous silent behavior.
tf.debugging.assert_less(
input_ids,
tf.cast(self.word_embeddings.input_dim, dtype=input_ids.dtype),
message=(
"input_ids must be smaller than the embedding layer's input dimension (got"
f" {tf.math.reduce_max(input_ids)} >= {self.word_embeddings.input_dim})"
),
)
inputs_embeds = self.word_embeddings(input_ids) inputs_embeds = self.word_embeddings(input_ids)
token_type_embeddings = self.token_type_embeddings(token_type_ids) token_type_embeddings = self.token_type_embeddings(token_type_ids)
......
...@@ -33,7 +33,7 @@ from ...modeling_tf_utils import ( ...@@ -33,7 +33,7 @@ from ...modeling_tf_utils import (
keras_serializable, keras_serializable,
unpack_inputs, unpack_inputs,
) )
from ...tf_utils import shape_list, stable_softmax from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax
from ...utils import ( from ...utils import (
ContextManagers, ContextManagers,
ModelOutput, ModelOutput,
...@@ -1746,16 +1746,7 @@ class TFLEDEncoder(tf.keras.layers.Layer): ...@@ -1746,16 +1746,7 @@ class TFLEDEncoder(tf.keras.layers.Layer):
if hasattr(self.embed_tokens, "load_weight_prefix"): if hasattr(self.embed_tokens, "load_weight_prefix"):
context.append(tf.name_scope(self.embed_tokens.load_weight_prefix + "/")) context.append(tf.name_scope(self.embed_tokens.load_weight_prefix + "/"))
with ContextManagers(context): with ContextManagers(context):
# Note: tf.gather, on which the embedding layer is based, won't check positive out of bound check_embeddings_within_bounds(input_ids, self.embed_tokens.input_dim)
# indices on GPU, returning zeros instead. This is a dangerous silent behavior.
tf.debugging.assert_less(
input_ids,
tf.cast(self.embed_tokens.input_dim, dtype=input_ids.dtype),
message=(
"input_ids must be smaller than the embedding layer's input dimension (got"
f" {tf.math.reduce_max(input_ids)} >= {self.embed_tokens.input_dim})"
),
)
inputs_embeds = self.embed_tokens(input_ids) inputs_embeds = self.embed_tokens(input_ids)
elif inputs_embeds is not None: elif inputs_embeds is not None:
input_shape = shape_list(inputs_embeds)[:-1] input_shape = shape_list(inputs_embeds)[:-1]
...@@ -2038,16 +2029,7 @@ class TFLEDDecoder(tf.keras.layers.Layer): ...@@ -2038,16 +2029,7 @@ class TFLEDDecoder(tf.keras.layers.Layer):
if hasattr(self.embed_tokens, "load_weight_prefix"): if hasattr(self.embed_tokens, "load_weight_prefix"):
context.append(tf.name_scope(self.embed_tokens.load_weight_prefix + "/")) context.append(tf.name_scope(self.embed_tokens.load_weight_prefix + "/"))
with ContextManagers(context): with ContextManagers(context):
# Note: tf.gather, on which the embedding layer is based, won't check positive out of bound check_embeddings_within_bounds(input_ids, self.embed_tokens.input_dim)
# indices on GPU, returning zeros instead. This is a dangerous silent behavior.
tf.debugging.assert_less(
input_ids,
tf.cast(self.embed_tokens.input_dim, dtype=input_ids.dtype),
message=(
"input_ids must be smaller than the embedding layer's input dimension (got"
f" {tf.math.reduce_max(input_ids)} >= {self.embed_tokens.input_dim})"
),
)
inputs_embeds = self.embed_tokens(input_ids) inputs_embeds = self.embed_tokens(input_ids)
hidden_states = inputs_embeds hidden_states = inputs_embeds
......
...@@ -34,7 +34,7 @@ from ...modeling_tf_utils import ( ...@@ -34,7 +34,7 @@ from ...modeling_tf_utils import (
keras_serializable, keras_serializable,
unpack_inputs, unpack_inputs,
) )
from ...tf_utils import shape_list, stable_softmax from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax
from ...utils import ( from ...utils import (
MULTIPLE_CHOICE_DUMMY_INPUTS, MULTIPLE_CHOICE_DUMMY_INPUTS,
ModelOutput, ModelOutput,
...@@ -538,16 +538,7 @@ class TFLongformerEmbeddings(tf.keras.layers.Layer): ...@@ -538,16 +538,7 @@ class TFLongformerEmbeddings(tf.keras.layers.Layer):
assert not (input_ids is None and inputs_embeds is None) assert not (input_ids is None and inputs_embeds is None)
if input_ids is not None: if input_ids is not None:
# Note: tf.gather, on which the embedding layer is based, won't check positive out of bound check_embeddings_within_bounds(input_ids, self.config.vocab_size)
# indices on GPU, returning zeros instead. This is a dangerous silent behavior.
tf.debugging.assert_less(
input_ids,
tf.cast(self.config.vocab_size, dtype=input_ids.dtype),
message=(
"input_ids must be smaller than the embedding layer's input dimension (got"
f" {tf.math.reduce_max(input_ids)} >= {self.config.vocab_size})"
),
)
inputs_embeds = tf.gather(params=self.weight, indices=input_ids) inputs_embeds = tf.gather(params=self.weight, indices=input_ids)
input_shape = shape_list(inputs_embeds)[:-1] input_shape = shape_list(inputs_embeds)[:-1]
......
...@@ -32,7 +32,7 @@ from ...modeling_tf_utils import ( ...@@ -32,7 +32,7 @@ from ...modeling_tf_utils import (
shape_list, shape_list,
unpack_inputs, unpack_inputs,
) )
from ...tf_utils import stable_softmax from ...tf_utils import check_embeddings_within_bounds, stable_softmax
from ...utils import ( from ...utils import (
ModelOutput, ModelOutput,
add_code_sample_docstrings, add_code_sample_docstrings,
...@@ -232,16 +232,7 @@ class TFLxmertEmbeddings(tf.keras.layers.Layer): ...@@ -232,16 +232,7 @@ class TFLxmertEmbeddings(tf.keras.layers.Layer):
assert not (input_ids is None and inputs_embeds is None) assert not (input_ids is None and inputs_embeds is None)
if input_ids is not None: if input_ids is not None:
# Note: tf.gather, on which the embedding layer is based, won't check positive out of bound check_embeddings_within_bounds(input_ids, self.config.vocab_size)
# indices on GPU, returning zeros instead. This is a dangerous silent behavior.
tf.debugging.assert_less(
input_ids,
tf.cast(self.config.vocab_size, dtype=input_ids.dtype),
message=(
"input_ids must be smaller than the embedding layer's input dimension (got"
f" {tf.math.reduce_max(input_ids)} >= {self.config.vocab_size})"
),
)
inputs_embeds = tf.gather(params=self.weight, indices=input_ids) inputs_embeds = tf.gather(params=self.weight, indices=input_ids)
input_shape = shape_list(inputs_embeds)[:-1] input_shape = shape_list(inputs_embeds)[:-1]
......
...@@ -37,7 +37,7 @@ from ...modeling_tf_utils import ( ...@@ -37,7 +37,7 @@ from ...modeling_tf_utils import (
keras_serializable, keras_serializable,
unpack_inputs, unpack_inputs,
) )
from ...tf_utils import shape_list, stable_softmax from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax
from ...utils import ( from ...utils import (
ContextManagers, ContextManagers,
add_code_sample_docstrings, add_code_sample_docstrings,
...@@ -778,16 +778,7 @@ class TFMarianEncoder(tf.keras.layers.Layer): ...@@ -778,16 +778,7 @@ class TFMarianEncoder(tf.keras.layers.Layer):
if hasattr(self.embed_tokens, "load_weight_prefix"): if hasattr(self.embed_tokens, "load_weight_prefix"):
context.append(tf.name_scope(self.embed_tokens.load_weight_prefix + "/")) context.append(tf.name_scope(self.embed_tokens.load_weight_prefix + "/"))
with ContextManagers(context): with ContextManagers(context):
# Note: tf.gather, on which the embedding layer is based, won't check positive out of bound check_embeddings_within_bounds(input_ids, self.embed_tokens.input_dim)
# indices on GPU, returning zeros instead. This is a dangerous silent behavior.
tf.debugging.assert_less(
input_ids,
tf.cast(self.embed_tokens.input_dim, dtype=input_ids.dtype),
message=(
"input_ids must be smaller than the embedding layer's input dimension (got"
f" {tf.math.reduce_max(input_ids)} >= {self.embed_tokens.input_dim})"
),
)
inputs_embeds = self.embed_tokens(input_ids) * self.embed_scale inputs_embeds = self.embed_tokens(input_ids) * self.embed_scale
embed_pos = self.embed_positions(input_shape) embed_pos = self.embed_positions(input_shape)
...@@ -990,16 +981,7 @@ class TFMarianDecoder(tf.keras.layers.Layer): ...@@ -990,16 +981,7 @@ class TFMarianDecoder(tf.keras.layers.Layer):
if hasattr(self.embed_tokens, "load_weight_prefix"): if hasattr(self.embed_tokens, "load_weight_prefix"):
context.append(tf.name_scope(self.embed_tokens.load_weight_prefix + "/")) context.append(tf.name_scope(self.embed_tokens.load_weight_prefix + "/"))
with ContextManagers(context): with ContextManagers(context):
# Note: tf.gather, on which the embedding layer is based, won't check positive out of bound check_embeddings_within_bounds(input_ids, self.embed_tokens.input_dim)
# indices on GPU, returning zeros instead. This is a dangerous silent behavior.
tf.debugging.assert_less(
input_ids,
tf.cast(self.embed_tokens.input_dim, dtype=input_ids.dtype),
message=(
"input_ids must be smaller than the embedding layer's input dimension (got"
f" {tf.math.reduce_max(input_ids)} >= {self.embed_tokens.input_dim})"
),
)
inputs_embeds = self.embed_tokens(input_ids) * self.embed_scale inputs_embeds = self.embed_tokens(input_ids) * self.embed_scale
hidden_states = inputs_embeds hidden_states = inputs_embeds
......
...@@ -37,7 +37,7 @@ from ...modeling_tf_utils import ( ...@@ -37,7 +37,7 @@ from ...modeling_tf_utils import (
keras_serializable, keras_serializable,
unpack_inputs, unpack_inputs,
) )
from ...tf_utils import shape_list, stable_softmax from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax
from ...utils import ( from ...utils import (
ContextManagers, ContextManagers,
add_code_sample_docstrings, add_code_sample_docstrings,
...@@ -770,16 +770,7 @@ class TFMBartEncoder(tf.keras.layers.Layer): ...@@ -770,16 +770,7 @@ class TFMBartEncoder(tf.keras.layers.Layer):
if hasattr(self.embed_tokens, "load_weight_prefix"): if hasattr(self.embed_tokens, "load_weight_prefix"):
context.append(tf.name_scope(self.embed_tokens.load_weight_prefix + "/")) context.append(tf.name_scope(self.embed_tokens.load_weight_prefix + "/"))
with ContextManagers(context): with ContextManagers(context):
# Note: tf.gather, on which the embedding layer is based, won't check positive out of bound check_embeddings_within_bounds(input_ids, self.embed_tokens.input_dim)
# indices on GPU, returning zeros instead. This is a dangerous silent behavior.
tf.debugging.assert_less(
input_ids,
tf.cast(self.embed_tokens.input_dim, dtype=input_ids.dtype),
message=(
"input_ids must be smaller than the embedding layer's input dimension (got"
f" {tf.math.reduce_max(input_ids)} >= {self.embed_tokens.input_dim})"
),
)
inputs_embeds = self.embed_tokens(input_ids) * self.embed_scale inputs_embeds = self.embed_tokens(input_ids) * self.embed_scale
embed_pos = self.embed_positions(input_shape) embed_pos = self.embed_positions(input_shape)
...@@ -989,16 +980,7 @@ class TFMBartDecoder(tf.keras.layers.Layer): ...@@ -989,16 +980,7 @@ class TFMBartDecoder(tf.keras.layers.Layer):
if hasattr(self.embed_tokens, "load_weight_prefix"): if hasattr(self.embed_tokens, "load_weight_prefix"):
context.append(tf.name_scope(self.embed_tokens.load_weight_prefix + "/")) context.append(tf.name_scope(self.embed_tokens.load_weight_prefix + "/"))
with ContextManagers(context): with ContextManagers(context):
# Note: tf.gather, on which the embedding layer is based, won't check positive out of bound check_embeddings_within_bounds(input_ids, self.embed_tokens.input_dim)
# indices on GPU, returning zeros instead. This is a dangerous silent behavior.
tf.debugging.assert_less(
input_ids,
tf.cast(self.embed_tokens.input_dim, dtype=input_ids.dtype),
message=(
"input_ids must be smaller than the embedding layer's input dimension (got"
f" {tf.math.reduce_max(input_ids)} >= {self.embed_tokens.input_dim})"
),
)
inputs_embeds = self.embed_tokens(input_ids) * self.embed_scale inputs_embeds = self.embed_tokens(input_ids) * self.embed_scale
hidden_states = inputs_embeds hidden_states = inputs_embeds
......
...@@ -46,7 +46,7 @@ from ...modeling_tf_utils import ( ...@@ -46,7 +46,7 @@ from ...modeling_tf_utils import (
keras_serializable, keras_serializable,
unpack_inputs, unpack_inputs,
) )
from ...tf_utils import shape_list, stable_softmax from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax
from ...utils import ( from ...utils import (
MULTIPLE_CHOICE_DUMMY_INPUTS, MULTIPLE_CHOICE_DUMMY_INPUTS,
ModelOutput, ModelOutput,
...@@ -212,16 +212,7 @@ class TFMobileBertEmbeddings(tf.keras.layers.Layer): ...@@ -212,16 +212,7 @@ class TFMobileBertEmbeddings(tf.keras.layers.Layer):
assert not (input_ids is None and inputs_embeds is None) assert not (input_ids is None and inputs_embeds is None)
if input_ids is not None: if input_ids is not None:
# Note: tf.gather, on which the embedding layer is based, won't check positive out of bound check_embeddings_within_bounds(input_ids, self.config.vocab_size)
# indices on GPU, returning zeros instead. This is a dangerous silent behavior.
tf.debugging.assert_less(
input_ids,
tf.cast(self.config.vocab_size, dtype=input_ids.dtype),
message=(
"input_ids must be smaller than the embedding layer's input dimension (got"
f" {tf.math.reduce_max(input_ids)} >= {self.config.vocab_size})"
),
)
inputs_embeds = tf.gather(params=self.weight, indices=input_ids) inputs_embeds = tf.gather(params=self.weight, indices=input_ids)
input_shape = shape_list(inputs_embeds)[:-1] input_shape = shape_list(inputs_embeds)[:-1]
......
...@@ -45,7 +45,7 @@ from ...modeling_tf_utils import ( ...@@ -45,7 +45,7 @@ from ...modeling_tf_utils import (
keras_serializable, keras_serializable,
unpack_inputs, unpack_inputs,
) )
from ...tf_utils import shape_list, stable_softmax from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax
from ...utils import ( from ...utils import (
MULTIPLE_CHOICE_DUMMY_INPUTS, MULTIPLE_CHOICE_DUMMY_INPUTS,
add_code_sample_docstrings, add_code_sample_docstrings,
...@@ -144,16 +144,7 @@ class TFMPNetEmbeddings(tf.keras.layers.Layer): ...@@ -144,16 +144,7 @@ class TFMPNetEmbeddings(tf.keras.layers.Layer):
assert not (input_ids is None and inputs_embeds is None) assert not (input_ids is None and inputs_embeds is None)
if input_ids is not None: if input_ids is not None:
# Note: tf.gather, on which the embedding layer is based, won't check positive out of bound check_embeddings_within_bounds(input_ids, self.config.vocab_size)
# indices on GPU, returning zeros instead. This is a dangerous silent behavior.
tf.debugging.assert_less(
input_ids,
tf.cast(self.config.vocab_size, dtype=input_ids.dtype),
message=(
"input_ids must be smaller than the embedding layer's input dimension (got"
f" {tf.math.reduce_max(input_ids)} >= {self.config.vocab_size})"
),
)
inputs_embeds = tf.gather(params=self.weight, indices=input_ids) inputs_embeds = tf.gather(params=self.weight, indices=input_ids)
input_shape = shape_list(inputs_embeds)[:-1] input_shape = shape_list(inputs_embeds)[:-1]
......
...@@ -35,7 +35,7 @@ from ...modeling_tf_utils import ( ...@@ -35,7 +35,7 @@ from ...modeling_tf_utils import (
keras_serializable, keras_serializable,
unpack_inputs, unpack_inputs,
) )
from ...tf_utils import shape_list, stable_softmax from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax
from ...utils import ( from ...utils import (
ModelOutput, ModelOutput,
add_code_sample_docstrings, add_code_sample_docstrings,
...@@ -295,30 +295,12 @@ class TFOpenAIGPTMainLayer(tf.keras.layers.Layer): ...@@ -295,30 +295,12 @@ class TFOpenAIGPTMainLayer(tf.keras.layers.Layer):
position_ids = tf.reshape(position_ids, [-1, shape_list(position_ids)[-1]]) position_ids = tf.reshape(position_ids, [-1, shape_list(position_ids)[-1]])
if inputs_embeds is None: if inputs_embeds is None:
# Note: tf.gather, on which the embedding layer is based, won't check positive out of bound check_embeddings_within_bounds(input_ids, self.config.vocab_size)
# indices on GPU, returning zeros instead. This is a dangerous silent behavior.
tf.debugging.assert_less(
input_ids,
tf.cast(self.config.vocab_size, dtype=input_ids.dtype),
message=(
"input_ids must be smaller than the embedding layer's input dimension (got"
f" {tf.math.reduce_max(input_ids)} >= {self.config.vocab_size})"
),
)
inputs_embeds = self.tokens_embed(input_ids, mode="embedding") inputs_embeds = self.tokens_embed(input_ids, mode="embedding")
position_embeds = tf.gather(self.positions_embed, position_ids) position_embeds = tf.gather(self.positions_embed, position_ids)
if token_type_ids is not None: if token_type_ids is not None:
token_type_ids = tf.reshape(token_type_ids, [-1, shape_list(token_type_ids)[-1]]) token_type_ids = tf.reshape(token_type_ids, [-1, shape_list(token_type_ids)[-1]])
# Note: tf.gather, on which the embedding layer is based, won't check positive out of bound check_embeddings_within_bounds(token_type_ids, self.config.vocab_size, "token_type_ids")
# indices on GPU, returning zeros instead. This is a dangerous silent behavior.
tf.debugging.assert_less(
token_type_ids,
tf.cast(self.config.vocab_size, dtype=token_type_ids.dtype),
message=(
"token_type_ids must be smaller than the embedding layer's input dimension (got"
f" {tf.math.reduce_max(token_type_ids)} >= {self.config.vocab_size})"
),
)
token_type_embeds = self.tokens_embed(token_type_ids, mode="embedding") token_type_embeds = self.tokens_embed(token_type_ids, mode="embedding")
else: else:
token_type_embeds = 0 token_type_embeds = 0
......
...@@ -33,7 +33,7 @@ from ...modeling_tf_utils import ( ...@@ -33,7 +33,7 @@ from ...modeling_tf_utils import (
keras_serializable, keras_serializable,
unpack_inputs, unpack_inputs,
) )
from ...tf_utils import shape_list, stable_softmax from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax
from ...utils import ( from ...utils import (
add_code_sample_docstrings, add_code_sample_docstrings,
add_start_docstrings, add_start_docstrings,
...@@ -631,16 +631,7 @@ class TFOPTDecoder(tf.keras.layers.Layer): ...@@ -631,16 +631,7 @@ class TFOPTDecoder(tf.keras.layers.Layer):
past_key_values_length = shape_list(past_key_values[0][0])[2] if past_key_values is not None else 0 past_key_values_length = shape_list(past_key_values[0][0])[2] if past_key_values is not None else 0
if inputs_embeds is None: if inputs_embeds is None:
# Note: tf.gather, on which the embedding layer is based, won't check positive out of bound check_embeddings_within_bounds(input_ids, self.embed_tokens.vocab_size)
# indices on GPU, returning zeros instead. This is a dangerous silent behavior.
tf.debugging.assert_less(
input_ids,
tf.cast(self.embed_tokens.vocab_size, dtype=input_ids.dtype),
message=(
"input_ids must be smaller than the embedding layer's input dimension (got"
f" {tf.math.reduce_max(input_ids)} >= {self.embed_tokens.vocab_size})"
),
)
inputs_embeds = self.embed_tokens(input_ids) inputs_embeds = self.embed_tokens(input_ids)
if attention_mask is None: if attention_mask is None:
......
...@@ -38,7 +38,7 @@ from ...modeling_tf_utils import ( ...@@ -38,7 +38,7 @@ from ...modeling_tf_utils import (
keras_serializable, keras_serializable,
unpack_inputs, unpack_inputs,
) )
from ...tf_utils import shape_list, stable_softmax from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax
from ...utils import ( from ...utils import (
ContextManagers, ContextManagers,
add_code_sample_docstrings, add_code_sample_docstrings,
...@@ -782,16 +782,7 @@ class TFPegasusEncoder(tf.keras.layers.Layer): ...@@ -782,16 +782,7 @@ class TFPegasusEncoder(tf.keras.layers.Layer):
if hasattr(self.embed_tokens, "load_weight_prefix"): if hasattr(self.embed_tokens, "load_weight_prefix"):
context.append(tf.name_scope(self.embed_tokens.load_weight_prefix + "/")) context.append(tf.name_scope(self.embed_tokens.load_weight_prefix + "/"))
with ContextManagers(context): with ContextManagers(context):
# Note: tf.gather, on which the embedding layer is based, won't check positive out of bound check_embeddings_within_bounds(input_ids, self.embed_tokens.input_dim)
# indices on GPU, returning zeros instead. This is a dangerous silent behavior.
tf.debugging.assert_less(
input_ids,
tf.cast(self.embed_tokens.input_dim, dtype=input_ids.dtype),
message=(
"input_ids must be smaller than the embedding layer's input dimension (got"
f" {tf.math.reduce_max(input_ids)} >= {self.embed_tokens.input_dim})"
),
)
inputs_embeds = self.embed_tokens(input_ids) * self.embed_scale inputs_embeds = self.embed_tokens(input_ids) * self.embed_scale
embed_pos = self.embed_positions(input_shape) embed_pos = self.embed_positions(input_shape)
...@@ -997,16 +988,7 @@ class TFPegasusDecoder(tf.keras.layers.Layer): ...@@ -997,16 +988,7 @@ class TFPegasusDecoder(tf.keras.layers.Layer):
if hasattr(self.embed_tokens, "load_weight_prefix"): if hasattr(self.embed_tokens, "load_weight_prefix"):
context.append(tf.name_scope(self.embed_tokens.load_weight_prefix + "/")) context.append(tf.name_scope(self.embed_tokens.load_weight_prefix + "/"))
with ContextManagers(context): with ContextManagers(context):
# Note: tf.gather, on which the embedding layer is based, won't check positive out of bound check_embeddings_within_bounds(input_ids, self.embed_tokens.input_dim)
# indices on GPU, returning zeros instead. This is a dangerous silent behavior.
tf.debugging.assert_less(
input_ids,
tf.cast(self.embed_tokens.input_dim, dtype=input_ids.dtype),
message=(
"input_ids must be smaller than the embedding layer's input dimension (got"
f" {tf.math.reduce_max(input_ids)} >= {self.embed_tokens.input_dim})"
),
)
inputs_embeds = self.embed_tokens(input_ids) * self.embed_scale inputs_embeds = self.embed_tokens(input_ids) * self.embed_scale
hidden_states = inputs_embeds hidden_states = inputs_embeds
......
...@@ -45,7 +45,7 @@ from ...modeling_tf_utils import ( ...@@ -45,7 +45,7 @@ from ...modeling_tf_utils import (
keras_serializable, keras_serializable,
unpack_inputs, unpack_inputs,
) )
from ...tf_utils import shape_list, stable_softmax from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax
from ...utils import ( from ...utils import (
DUMMY_INPUTS, DUMMY_INPUTS,
MULTIPLE_CHOICE_DUMMY_INPUTS, MULTIPLE_CHOICE_DUMMY_INPUTS,
...@@ -122,16 +122,7 @@ class TFRemBertEmbeddings(tf.keras.layers.Layer): ...@@ -122,16 +122,7 @@ class TFRemBertEmbeddings(tf.keras.layers.Layer):
assert not (input_ids is None and inputs_embeds is None) assert not (input_ids is None and inputs_embeds is None)
if input_ids is not None: if input_ids is not None:
# Note: tf.gather, on which the embedding layer is based, won't check positive out of bound check_embeddings_within_bounds(input_ids, self.config.vocab_size)
# indices on GPU, returning zeros instead. This is a dangerous silent behavior.
tf.debugging.assert_less(
input_ids,
tf.cast(self.config.vocab_size, dtype=input_ids.dtype),
message=(
"input_ids must be smaller than the embedding layer's input dimension (got"
f" {tf.math.reduce_max(input_ids)} >= {self.config.vocab_size})"
),
)
inputs_embeds = tf.gather(params=self.weight, indices=input_ids) inputs_embeds = tf.gather(params=self.weight, indices=input_ids)
input_shape = shape_list(inputs_embeds)[:-1] input_shape = shape_list(inputs_embeds)[:-1]
......
...@@ -46,7 +46,7 @@ from ...modeling_tf_utils import ( ...@@ -46,7 +46,7 @@ from ...modeling_tf_utils import (
keras_serializable, keras_serializable,
unpack_inputs, unpack_inputs,
) )
from ...tf_utils import shape_list, stable_softmax from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax
from ...utils import ( from ...utils import (
DUMMY_INPUTS, DUMMY_INPUTS,
MULTIPLE_CHOICE_DUMMY_INPUTS, MULTIPLE_CHOICE_DUMMY_INPUTS,
...@@ -144,16 +144,7 @@ class TFRobertaEmbeddings(tf.keras.layers.Layer): ...@@ -144,16 +144,7 @@ class TFRobertaEmbeddings(tf.keras.layers.Layer):
assert not (input_ids is None and inputs_embeds is None) assert not (input_ids is None and inputs_embeds is None)
if input_ids is not None: if input_ids is not None:
# Note: tf.gather, on which the embedding layer is based, won't check positive out of bound check_embeddings_within_bounds(input_ids, self.config.vocab_size)
# indices on GPU, returning zeros instead. This is a dangerous silent behavior.
tf.debugging.assert_less(
input_ids,
tf.cast(self.config.vocab_size, dtype=input_ids.dtype),
message=(
"input_ids must be smaller than the embedding layer's input dimension (got"
f" {tf.math.reduce_max(input_ids)} >= {self.config.vocab_size})"
),
)
inputs_embeds = tf.gather(params=self.weight, indices=input_ids) inputs_embeds = tf.gather(params=self.weight, indices=input_ids)
input_shape = shape_list(inputs_embeds)[:-1] input_shape = shape_list(inputs_embeds)[:-1]
......
...@@ -46,7 +46,7 @@ from ...modeling_tf_utils import ( ...@@ -46,7 +46,7 @@ from ...modeling_tf_utils import (
keras_serializable, keras_serializable,
unpack_inputs, unpack_inputs,
) )
from ...tf_utils import shape_list, stable_softmax from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax
from ...utils import ( from ...utils import (
DUMMY_INPUTS, DUMMY_INPUTS,
MULTIPLE_CHOICE_DUMMY_INPUTS, MULTIPLE_CHOICE_DUMMY_INPUTS,
...@@ -149,16 +149,7 @@ class TFRobertaPreLayerNormEmbeddings(tf.keras.layers.Layer): ...@@ -149,16 +149,7 @@ class TFRobertaPreLayerNormEmbeddings(tf.keras.layers.Layer):
assert not (input_ids is None and inputs_embeds is None) assert not (input_ids is None and inputs_embeds is None)
if input_ids is not None: if input_ids is not None:
# Note: tf.gather, on which the embedding layer is based, won't check positive out of bound check_embeddings_within_bounds(input_ids, self.config.vocab_size)
# indices on GPU, returning zeros instead. This is a dangerous silent behavior.
tf.debugging.assert_less(
input_ids,
tf.cast(self.config.vocab_size, dtype=input_ids.dtype),
message=(
"input_ids must be smaller than the embedding layer's input dimension (got"
f" {tf.math.reduce_max(input_ids)} >= {self.config.vocab_size})"
),
)
inputs_embeds = tf.gather(params=self.weight, indices=input_ids) inputs_embeds = tf.gather(params=self.weight, indices=input_ids)
input_shape = shape_list(inputs_embeds)[:-1] input_shape = shape_list(inputs_embeds)[:-1]
......
...@@ -46,7 +46,7 @@ from ...modeling_tf_utils import ( ...@@ -46,7 +46,7 @@ from ...modeling_tf_utils import (
keras_serializable, keras_serializable,
unpack_inputs, unpack_inputs,
) )
from ...tf_utils import shape_list, stable_softmax from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax
from ...utils import ( from ...utils import (
MULTIPLE_CHOICE_DUMMY_INPUTS, MULTIPLE_CHOICE_DUMMY_INPUTS,
add_code_sample_docstrings, add_code_sample_docstrings,
...@@ -175,16 +175,7 @@ class TFRoFormerEmbeddings(tf.keras.layers.Layer): ...@@ -175,16 +175,7 @@ class TFRoFormerEmbeddings(tf.keras.layers.Layer):
assert not (input_ids is None and inputs_embeds is None) assert not (input_ids is None and inputs_embeds is None)
if input_ids is not None: if input_ids is not None:
# Note: tf.gather, on which the embedding layer is based, won't check positive out of bound check_embeddings_within_bounds(input_ids, self.config.vocab_size)
# indices on GPU, returning zeros instead. This is a dangerous silent behavior.
tf.debugging.assert_less(
input_ids,
tf.cast(self.config.vocab_size, dtype=input_ids.dtype),
message=(
"input_ids must be smaller than the embedding layer's input dimension (got"
f" {tf.math.reduce_max(input_ids)} >= {self.config.vocab_size})"
),
)
inputs_embeds = tf.gather(params=self.weight, indices=input_ids) inputs_embeds = tf.gather(params=self.weight, indices=input_ids)
input_shape = shape_list(inputs_embeds)[:-1] input_shape = shape_list(inputs_embeds)[:-1]
......
...@@ -36,7 +36,7 @@ from ...modeling_tf_utils import ( ...@@ -36,7 +36,7 @@ from ...modeling_tf_utils import (
keras_serializable, keras_serializable,
unpack_inputs, unpack_inputs,
) )
from ...tf_utils import shape_list, stable_softmax from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax
from ...utils import ( from ...utils import (
add_code_sample_docstrings, add_code_sample_docstrings,
add_start_docstrings, add_start_docstrings,
...@@ -1030,16 +1030,7 @@ class TFSpeech2TextDecoder(tf.keras.layers.Layer): ...@@ -1030,16 +1030,7 @@ class TFSpeech2TextDecoder(tf.keras.layers.Layer):
past_key_values_length = shape_list(past_key_values[0][0])[2] if past_key_values is not None else 0 past_key_values_length = shape_list(past_key_values[0][0])[2] if past_key_values is not None else 0
if inputs_embeds is None: if inputs_embeds is None:
# Note: tf.gather, on which the embedding layer is based, won't check positive out of bound check_embeddings_within_bounds(input_ids, self.embed_tokens.vocab_size)
# indices on GPU, returning zeros instead. This is a dangerous silent behavior.
tf.debugging.assert_less(
input_ids,
tf.cast(self.embed_tokens.vocab_size, dtype=input_ids.dtype),
message=(
"input_ids must be smaller than the embedding layer's input dimension (got"
f" {tf.math.reduce_max(input_ids)} >= {self.embed_tokens.vocab_size})"
),
)
inputs_embeds = self.embed_tokens(input_ids) * self.embed_scale inputs_embeds = self.embed_tokens(input_ids) * self.embed_scale
else: else:
inputs_embeds = inputs_embeds inputs_embeds = inputs_embeds
......
...@@ -40,7 +40,7 @@ from ...modeling_tf_utils import ( ...@@ -40,7 +40,7 @@ from ...modeling_tf_utils import (
keras_serializable, keras_serializable,
unpack_inputs, unpack_inputs,
) )
from ...tf_utils import shape_list, stable_softmax from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax
from ...utils import ( from ...utils import (
DUMMY_INPUTS, DUMMY_INPUTS,
DUMMY_MASK, DUMMY_MASK,
...@@ -686,16 +686,7 @@ class TFT5MainLayer(tf.keras.layers.Layer): ...@@ -686,16 +686,7 @@ class TFT5MainLayer(tf.keras.layers.Layer):
if hasattr(self.embed_tokens, "load_weight_prefix"): if hasattr(self.embed_tokens, "load_weight_prefix"):
context.append(tf.name_scope(self.embed_tokens.load_weight_prefix + "/")) context.append(tf.name_scope(self.embed_tokens.load_weight_prefix + "/"))
with ContextManagers(context): with ContextManagers(context):
# Note: tf.gather, on which the embedding layer is based, won't check positive out of bound check_embeddings_within_bounds(input_ids, self.embed_tokens.input_dim)
# indices on GPU, returning zeros instead. This is a dangerous silent behavior.
tf.debugging.assert_less(
input_ids,
tf.cast(self.embed_tokens.input_dim, dtype=input_ids.dtype),
message=(
"input_ids must be smaller than the embedding layer's input dimension (got"
f" {tf.math.reduce_max(input_ids)} >= {self.embed_tokens.input_dim})"
),
)
inputs_embeds = self.embed_tokens(input_ids) inputs_embeds = self.embed_tokens(input_ids)
batch_size, seq_length = input_shape batch_size, seq_length = input_shape
......
...@@ -38,7 +38,7 @@ from ...modeling_tf_utils import ( ...@@ -38,7 +38,7 @@ from ...modeling_tf_utils import (
keras_serializable, keras_serializable,
unpack_inputs, unpack_inputs,
) )
from ...tf_utils import shape_list, stable_softmax from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax
from ...utils import ( from ...utils import (
ModelOutput, ModelOutput,
add_start_docstrings, add_start_docstrings,
...@@ -231,16 +231,7 @@ class TFTapasEmbeddings(tf.keras.layers.Layer): ...@@ -231,16 +231,7 @@ class TFTapasEmbeddings(tf.keras.layers.Layer):
position_ids = tf.math.minimum(self.max_position_embeddings - 1, position - first_position) position_ids = tf.math.minimum(self.max_position_embeddings - 1, position - first_position)
if input_ids is not None: if input_ids is not None:
# Note: tf.gather, on which the embedding layer is based, won't check positive out of bound check_embeddings_within_bounds(input_ids, self.config.vocab_size)
# indices on GPU, returning zeros instead. This is a dangerous silent behavior.
tf.debugging.assert_less(
input_ids,
tf.cast(self.config.vocab_size, dtype=input_ids.dtype),
message=(
"input_ids must be smaller than the embedding layer's input dimension (got"
f" {tf.math.reduce_max(input_ids)} >= {self.config.vocab_size})"
),
)
inputs_embeds = tf.gather(params=self.weight, indices=input_ids) inputs_embeds = tf.gather(params=self.weight, indices=input_ids)
position_embeddings = tf.gather(self.position_embeddings, indices=position_ids) position_embeddings = tf.gather(self.position_embeddings, indices=position_ids)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment