Skip to content

Conversation

buildwithsuhana
Copy link
Contributor

This PR introduces the new Gemma 3 backbone for embedding gemma model. It includes:

  • MeanPooling Layer: This new layer correctly computes the average of token embeddings while ignoring padded tokens using a mask.

  • The Gemma3EmbeddingModel: This model integrates a Gemma3Backbone, your new MeanPooling layer, and a final Dense projection layer to produce fixed-size sequence embeddings. It is also fully serializable.

@github-actions github-actions bot added the Gemma Gemma model specific issues label Oct 6, 2025
Copy link

Summary of Changes

Hello @buildwithsuhana, 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 significantly enhances the Gemma3 backbone by introducing a dedicated embedding model. The core purpose is to provide a robust and accurate method for generating fixed-size sequence embeddings from the Gemma3 model's output, specifically by handling variable-length sequences through a new mean pooling mechanism that correctly accounts for padding. This change improves the model's utility for downstream tasks requiring dense vector representations of input sequences.

Highlights

  • New MeanPooling Layer: A new MeanPooling layer has been introduced to accurately compute the average of token embeddings by correctly ignoring padded tokens using a mask.
  • Gemma3EmbeddingModel Integration: The Gemma3EmbeddingModel has been added, which integrates the Gemma3Backbone, the new MeanPooling layer, and a final Dense projection layer to generate fixed-size sequence embeddings. This model is also fully serializable.
  • Comprehensive Testing: New test cases have been added for the Gemma3EmbeddingModel to ensure its basic functionality, architectural correctness (parameter and layer counts), serialization capabilities, and the ability to build from preset backbones.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a Gemma3EmbeddingModel and a MeanPooling layer to support embedding generation with Gemma 3 models. The changes are well-structured and include corresponding tests. My review focuses on ensuring adherence to the repository's style guide, particularly regarding documentation and serialization, to improve code quality and maintainability. I've suggested adding proper docstrings and get_config methods, and removing type hints from function signatures as per the contribution guidelines.

@abheesht17 abheesht17 self-requested a review October 6, 2025 05:46
Copy link
Collaborator

@abheesht17 abheesht17 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, nice work! Let's do the following:

  • Let's move the layers to the backbone itself.
  • Add a conversion script.

return super().from_config(config)


class MeanPooling(layers.Layer):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move this to gemma3_mean_pooling.py?

super().__init__(**kwargs)
self.supports_masking = True

def call(self, inputs):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of a tuple, can we just pass these arguments to call(...) separately instead of passing them as a tuple?



@keras_hub_export("keras_hub.models.Gemma3Embedding")
class Gemma3EmbeddingModel(keras.Model):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should just stick the layers on top in Gemma3Backbone instead of creating a separate class for this.

Can probably add an argument, named is_embedding_model, maybe. If is_embedding_model is True, add the dense layers + mean pooling layer, and return a dictionary, which returns the hidden states and the pooled output.

What do you think?

)
self.embedding_dim = embedding_dim

def call(self, inputs):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't generally have call(...) for KerasHub models because these models are Functional models.

Comment on lines +527 to +528
self.projection_layer = layers.Dense(
embedding_dim, dtype=backbone.dtype, name="embedding_projection"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there just one dense layer for Embedding Gemma? I thought there were several.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Gemma Gemma model specific issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants