Skip to content

Add Gemma 3 conversion script #2358

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

Merged
merged 10 commits into from
Aug 13, 2025

Conversation

abheesht17
Copy link
Collaborator

@abheesht17 abheesht17 commented Aug 13, 2025

Verified for gemma3_instruct_1b, gemma3_instruct_4b

@github-actions github-actions bot added the Gemma Gemma model specific issues label Aug 13, 2025
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.

Summary of Changes

Hello @abheesht17, 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 introduces a new conversion script for Gemma 3 models, enabling the transformation of Flax checkpoints into the Keras format. This significantly expands the utility of Gemma 3 models by making them compatible with the Keras ecosystem, supporting both text-only and multimodal variants across various sizes. A new utility function for downloading files from Google Cloud Storage was also added to facilitate the process.

Highlights

  • Gemma 3 Checkpoint Conversion: A new Python script (convert_gemma3_checkpoints.py) has been added to convert Gemma 3 Flax checkpoints to the Keras format.
  • Multimodal and Text-Only Support: The conversion script supports various Gemma 3 model sizes (1B, 4B, 12B, 27B) and types, including both text-only and vision-and-text models.
  • Google Cloud Storage Download Utility: A new helper function download_gcs_file was introduced in checkpoint_conversion_utils.py to enable direct downloading of files from Google Cloud Storage, which is used to fetch tokenizer models.
  • Comprehensive Weight Mapping: The script includes detailed logic for mapping and assigning weights from Flax model parameters to their corresponding layers in the Keras Gemma3Backbone and Gemma3VisionEncoder models.
  • Conversion Validation: A validation step is implemented to compare the output of the converted Keras model with the original Flax model, ensuring the accuracy and integrity of the conversion process.
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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

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 issue 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 adds a conversion script for Gemma 3 checkpoints. The changes include adding a utility to download files from GCS and the main conversion script.

My review focuses on improving the robustness, readability, and maintainability of the new code. Key suggestions include:

  • Using library-provided methods for parsing GCS URIs instead of manual string splitting.
  • Providing more informative error messages for missing dependencies.
  • Refactoring long, repetitive functions in the conversion script to reduce code duplication.
  • Avoiding hardcoded values and magic strings.
  • Using temporary files for downloaded artifacts to avoid cluttering the file system.

Overall, the script is a great addition, and these changes will make it more robust and easier to maintain.

Copy link
Collaborator

@JyotinderSingh JyotinderSingh left a comment

Choose a reason for hiding this comment

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

Thanks for this PR!

@abheesht17 abheesht17 merged commit 46ae9ee into keras-team:master Aug 13, 2025
7 checks passed
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