fix: jax & NNX TraceContextError in T5Backbone#2602
fix: jax & NNX TraceContextError in T5Backbone#2602maitry63 wants to merge 8 commits intokeras-team:masterfrom
Conversation
Summary of ChangesHello @maitry63, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses and resolves a Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request aims to resolve a TraceContextError in T5Backbone when using JAX with NNX enabled by switching to a subclassed model implementation instead of the Keras Functional API.
While the approach is correct, the implementation is incomplete. The functional model definition is conditionally removed, but the required call method for the subclassed model path is missing, which will make the model non-functional when NNX is enabled. I've added a critical comment with a suggested implementation for the call method.
I also found a minor issue with a redundant import statement. Please see my comments for details.
| else: | ||
| # NNX-safe subclassed model path | ||
| super().__init__(dtype=dtype, **kwargs) |
There was a problem hiding this comment.
This change is intended to fix an issue with JAX and NNX by avoiding the Functional API. However, by only calling super().__init__() for the NNX path, the model becomes a subclassed model without a call method. This will cause a NotImplementedError when the model is called.
To complete the fix, you need to implement the call method to define the forward pass. The logic for this method is the same as what's currently inside the if not nnx_enabled: block.
Here is a suggested implementation for the call method to be added to the T5Backbone class:
def call(self, inputs, training=False):
encoder_token_ids = inputs["encoder_token_ids"]
encoder_padding_mask = inputs["encoder_padding_mask"]
decoder_token_ids = inputs["decoder_token_ids"]
decoder_padding_mask = inputs["decoder_padding_mask"]
# Encoder.
x = self.token_embedding(encoder_token_ids)
x = self.encoder_embedding_dropout(x, training=training)
encoder_attention_mask = encoder_padding_mask[:, None, :]
position_bias = None
for transformer_layer in self.encoder_transformer_layers:
output = transformer_layer(
x,
attention_mask=encoder_attention_mask,
position_bias=position_bias,
use_causal_mask=False,
training=training,
)
if isinstance(output, tuple):
x, position_bias = output
x = self.encoder_layer_norm(x)
x = self.encoder_dropout(x, training=training)
encoder_output = x
# Decoder.
x = self.token_embedding(decoder_token_ids)
x = self.decoder_embedding_dropout(x, training=training)
decoder_attention_mask = decoder_padding_mask[:, None, :]
position_bias = None
for transformer_layer in self.decoder_transformer_layers:
output = transformer_layer(
x,
attention_mask=decoder_attention_mask,
position_bias=position_bias,
encoder_hidden_states=encoder_output,
encoder_attention_mask=encoder_attention_mask,
use_causal_mask=True,
training=training,
)
if isinstance(output, tuple):
x, position_bias = output
x = self.decoder_layer_norm(x)
x = self.decoder_dropout(x, training=training)
decoder_output = x
return {
"encoder_sequence_output": encoder_output,
"decoder_sequence_output": decoder_output,
}| import keras | ||
|
|
|
@gemini-code-assist review |
There was a problem hiding this comment.
Code Review
The pull request addresses a TraceContextError in the T5 Backbone Model by refactoring the functional model definition into a _forward method and conditionally initializing the functional model based on whether NNX is enabled. This change correctly separates the functional model construction from the __init__ method, which is a common pattern to avoid tracing issues with JAX/NNX. The added call method then dispatches to this _forward method. The changes are well-implemented and directly resolve the reported issue.
| attention_mask=encoder_attention_mask, | ||
| position_bias=position_bias, | ||
| use_causal_mask=False, | ||
| def _forward(inputs, training=None): |
| decoder_padding_mask = inputs["decoder_padding_mask"] | ||
| # Encoder | ||
| x = self.token_embedding(encoder_token_ids) | ||
| x = self.encoder_embedding_dropout(x, training=training) |
There was a problem hiding this comment.
The training argument should be passed to the token_embedding layer call as well, to ensure consistent behavior, especially if the embedding layer has any dropout or batch normalization components that behave differently during training and inference. While ReversibleEmbedding might not directly use training, it's good practice for consistency.
# Encoder
x = self.token_embedding(encoder_token_ids, training=training)
x = self.encoder_embedding_dropout(x, training=training)| x = self.encoder_dropout(x, training=training) | ||
| encoder_output = x | ||
| # Decoder | ||
| x = self.token_embedding(decoder_token_ids) |
There was a problem hiding this comment.
This PR fixes the JAX & NNX TraceContextError with T5 Backbone Model.
Fixes: 2591