-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix the inference of transformer-based models trained with masked language modeling #909
Changes from 3 commits
ef64992
6a5e582
a135fe2
2cd8fd2
4e50acf
d2286f6
9b38f04
2983433
8607a9b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,10 +37,58 @@ class LastHiddenState(Layer): | |
The output class returned by the HuggingFace transformer layer | ||
""" | ||
|
||
def __init__(self, **kwargs): | ||
super().__init__(**kwargs) | ||
self.supports_masking = True | ||
|
||
def call(self, inputs: TFBaseModelOutputWithPoolingAndCrossAttentions): | ||
return inputs.last_hidden_state | ||
|
||
|
||
@Block.registry.register("inference_hidden_state") | ||
@tf.keras.utils.register_keras_serializable(package="merlin.models") | ||
class InferenceHiddenState(Layer): | ||
sararb marked this conversation as resolved.
Show resolved
Hide resolved
sararb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""A post-processing layer to select the hidden state | ||
of the next-item position, during inference. | ||
""" | ||
|
||
def __init__(self, **kwargs): | ||
super().__init__(**kwargs) | ||
self.supports_masking = True | ||
|
||
def call( | ||
self, | ||
inputs: tf.Tensor, | ||
training: bool = False, | ||
testing: bool = False, | ||
): | ||
"""Select the hidden state of the target position, during inference. | ||
sararb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
During training or testing, the inputs are returned | ||
without any processing. | ||
|
||
Parameters: | ||
---------- | ||
inputs: tf.Tensor | ||
The 3-D output tensor returned by the transformer block | ||
training : bool, optional | ||
Flag that indicates whether in training mode, by default True | ||
testing : bool, optional | ||
Flag that indicates whether in evaluation mode, by default True | ||
|
||
Returns | ||
------- | ||
tf.Tensor | ||
If inference, returns a 2-D tensor with the hidden states of | ||
the target position | ||
""" | ||
if not training and not testing: | ||
if getattr(inputs, "_keras_mask", None) is not None: | ||
inputs = tf.reshape( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can add an assert here to check if the resulting inputs after |
||
tf.boolean_mask(inputs, inputs._keras_mask), (-1, inputs.shape[-1]) | ||
) | ||
return inputs | ||
|
||
|
||
@Block.registry.register("pooler_output") | ||
@tf.keras.utils.register_keras_serializable(package="merlin.models") | ||
class PoolerOutput(Layer): | ||
|
@@ -113,10 +161,21 @@ def call(self, inputs: TFBaseModelOutputWithPoolingAndCrossAttentions): | |
class PrepareTransformerInputs(tf.keras.layers.Layer): | ||
"""Prepare the dictionary of inputs expected by the transformer layer""" | ||
|
||
def __init__(self, **kwargs): | ||
super().__init__(**kwargs) | ||
self.supports_masking = True | ||
|
||
def call(self, inputs: tf.Tensor) -> Dict[str, tf.Tensor]: | ||
mask = None | ||
if getattr(inputs, "_keras_mask", None) is not None and isinstance( | ||
inputs._keras_mask, tf.RaggedTensor | ||
): | ||
mask = inputs._keras_mask.to_tensor() | ||
if isinstance(inputs, tf.RaggedTensor): | ||
# convert to a dense tensor as HF transformers do not support ragged tensors | ||
inputs = inputs.to_tensor() | ||
if mask is not None: | ||
inputs._keras_mask = mask | ||
return {"inputs_embeds": inputs} | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
set
supports_masking=True
so that the transformer layer can forward themask
tensor created bySequenceMaskLastInference