Skip to content

Conversation

kharshith-k
Copy link

No description provided.

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

Summary of Changes

Hello @kharshith-k, 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 checkpoint conversion tool by introducing the capability to export Keras models into the Hugging Face safetensors format. This feature facilitates seamless integration of Gemma3 models with the Hugging Face ecosystem, providing a standardized and efficient way to share and utilize these models across different platforms. The changes include a new command-line option, comprehensive weight conversion logic, and a verification step to confirm the integrity of the exported models.

Highlights

  • Hugging Face Safetensors Export: Added functionality to convert Gemma3 Keras checkpoints to Hugging Face safetensors format, enabling broader interoperability.
  • New CLI Flag: Introduced the --export_safetensors command-line flag to activate the Hugging Face conversion process.
  • Comprehensive Weight Mapping: Implemented detailed logic for mapping and transforming Keras model weights (including token embeddings, attention layers, and MLP layers) to their corresponding Hugging Face safetensors structure, with support for bfloat16 data types.
  • Conversion Verification: Included a post-conversion step to load the exported Hugging Face model and perform a sample inference, ensuring the correctness and integrity of the conversion.
  • Configuration Conversion: Added a utility function to convert Keras Gemma model configurations to the Hugging Face Gemma3TextConfig format.
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
Contributor

@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 adds a valuable feature to convert Keras Gemma3 models to the Hugging Face Safetensors format, enhancing interoperability. The implementation is comprehensive, covering configuration conversion, weight porting, and a validation step. I've provided a few suggestions to improve code clarity, maintainability, and adherence to the repository's style guide, primarily by improving docstrings, refactoring duplicated code, and ensuring deterministic validation.

Comment on lines +46 to +47
def convert_to_hf_config(keras_config):
"""Convert Keras Gemma config to Hugging Face GemmaConfig."""
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The docstring for this function is missing Args and Returns sections, which is inconsistent with the repository's style guide. Providing detailed docstrings improves code clarity and maintainability.1

def convert_to_hf_config(keras_config):
    """Convert Keras Gemma config to Hugging Face GemmaConfig.

    Args:
        keras_config: A Keras Gemma3 config object from the backbone.

    Returns:
        A `transformers.Gemma3TextConfig` instance.
    """

Style Guide References

Footnotes

  1. The style guide requires all public functions to have Google-style docstrings, including comprehensive documentation for all parameters and return values.

Comment on lines +61 to +62
def export_to_hf(backbone, keras_tokenizer, path):
"""Convert a Keras Gemma model to Hugging Face format and save to path."""
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The docstring for this function is missing Args and Returns sections, which is inconsistent with the repository's style guide. Providing detailed docstrings improves code clarity and maintainability.1

def export_to_hf(backbone, keras_tokenizer, path):
    """Convert a Keras Gemma model to Hugging Face format and save to path.

    Args:
        backbone: A `keras_hub.models.Gemma3Backbone` instance.
        keras_tokenizer: A `keras_hub.models.Gemma3Tokenizer` instance.
        path: str. The path to save the Hugging Face model to.
    """

Style Guide References

Footnotes

  1. The style guide requires all public functions to have Google-style docstrings, including comprehensive documentation for all parameters and return values.

Comment on lines +68 to +72
def to_torch(weight):
# Convert bfloat16 to float32 first, then to torch, then to bfloat16
if hasattr(weight.dtype, "name") and "bfloat16" in str(weight.dtype):
weight = np.array(weight, dtype=np.float32)
return torch.from_numpy(weight).to(torch.bfloat16)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This helper function can be simplified to robustly handle various array types (like JAX arrays) and then used consistently throughout export_to_hf to reduce code duplication.

Currently, the conversion logic torch.from_numpy(np.array(weight, dtype=np.float32)).to(torch.bfloat16) is repeated for many weights. You can simplify to_torch to encapsulate this logic and improve maintainability.

With the suggested change, you can then refactor the rest of the function, for example:

q_kernel = block.attention.query_dense.get_weights()[0]
weights_dict[f"model.layers.{i}.self_attn.q_proj.weight"] = (
    to_torch(q_kernel)
    .permute(1, 0, 2)
    .reshape(backbone.hidden_dim, -1)
    .T
)
    def to_torch(weight):
        # Convert array-like weights (e.g., from JAX) to a float32 NumPy
        # array before creating a bfloat16 torch tensor for compatibility.
        np_weight = np.array(weight, dtype=np.float32)
        return torch.from_numpy(np_weight).to(torch.bfloat16)

top_k=50,
top_p=1.0,
):
# Tokenize inpu
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

There is a typo in this comment.

    # Tokenize input

Comment on lines +215 to +222
outputs = model.generate(
**inputs,
max_new_tokens=max_new_tokens,
temperature=temperature,
top_k=top_k,
top_p=top_p,
do_sample=True,
)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The infer function is used for a sanity check after converting the model. Currently, it uses sampling (do_sample=True), which makes the output non-deterministic. For a validation or sanity check step in a conversion script, it's better to use a deterministic generation strategy like greedy search to ensure consistent and predictable outputs.

        outputs = model.generate(
            **inputs,
            max_new_tokens=max_new_tokens,
            temperature=temperature,
            top_k=top_k,
            top_p=top_p,
            do_sample=False,
        )

Comment on lines +327 to +328
"Export model to Safetensors format (HuggingFace-compatible). \
Only for text-only models.",
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The help string for the export_safetensors flag contains a backslash and extra whitespace, which will be literally included in the help message displayed to the user. It's better to use implicit string concatenation for multiline help messages to ensure clean formatting.

    "Export model to Safetensors format (HuggingFace-compatible). "
    "Only for text-only models.",

@sachinprasadhs
Copy link
Collaborator

Thanks for the PR, the export to safetensors should be made available here https://github.com/keras-team/keras-hub/tree/master/keras_hub/src/utils/transformers/export.

  • Create a new file for Gemma3
  • Add Gemma3 details here
    MODEL_CONFIGS = {
    "GemmaBackbone": get_gemma_config,
    # Add for future models, e.g., "MistralBackbone": get_mistral_config
    }
    MODEL_EXPORTERS = {
    "GemmaBackbone": get_gemma_weights_map,
    # Add for future models, e.g., "MistralBackbone": get_mistral_weights_map
    }
    MODEL_TOKENIZER_CONFIGS = {
    "GemmaTokenizer": get_gemma_tokenizer_config,
    # Add for future models, e.g., "MistralTokenizer":
    # get_mistral_tokenizer_config
    }
  • Add a test file for Gemma3 export.

@hertschuh hertschuh added the kokoro:force-run Runs Tests on GPU label Oct 13, 2025
@kokoro-team kokoro-team removed the kokoro:force-run Runs Tests on GPU label Oct 13, 2025
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.

4 participants