-
Notifications
You must be signed in to change notification settings - Fork 652
Add initial T5Gemma S/S support #947
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
Draft
maasir554
wants to merge
6
commits into
google:dev
Choose a base branch
from
maasir554:t5gemma-support
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3022b77
Fix Python converter Bazel targets
maasir554 2d84021
Add T5Gemma S/S config and tensor metadata
maasir554 98ff239
Add T5Gemma safetensors conversion
maasir554 0c64e7c
Add T5Gemma encoder-decoder runtime
maasir554 7378108
Document T5Gemma conversion and runtime
maasir554 65263cf
Sync config bindings with upstream Gemma4
maasir554 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -136,7 +136,7 @@ enum class PostQKType { | |
|
|
||
| static inline bool EnumValid(PostQKType type) { | ||
| return static_cast<size_t>(type) < | ||
| static_cast<size_t>(PostNormType::kSentinel); | ||
| static_cast<size_t>(PostQKType::kSentinel); | ||
| } | ||
|
|
||
| // FFW activation function. | ||
|
|
@@ -221,6 +221,8 @@ enum class Model { | |
| GEMMA3_12B_LM, | ||
| GEMMA3_27B_LM, | ||
| GEMMA4_26B_MOE, | ||
| // T5Gemma family - starting with S/S. | ||
| T5GEMMA_S_S, | ||
| kSentinel, | ||
| }; | ||
|
|
||
|
|
@@ -438,6 +440,14 @@ struct ModelConfig : public IFields { | |
| visitor(use_global_timescale); | ||
| visitor(partial_rotary_factor); | ||
|
|
||
| visitor(is_encoder_decoder); | ||
| visitor(encoder_num_layers); | ||
|
Member
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. We can make |
||
| visitor(encoder_layer_configs); | ||
| visitor(encoder_attention_window_sizes); | ||
| visitor(decoder_num_layers); | ||
| visitor(decoder_layer_configs); | ||
| visitor(decoder_attention_window_sizes); | ||
|
|
||
| // Append new fields here, then update `python/configs.cc`. | ||
| } | ||
|
|
||
|
|
@@ -465,6 +475,16 @@ struct ModelConfig : public IFields { | |
| attention_window_sizes[i] = new_max_seq_len; | ||
| } | ||
| } | ||
| for (size_t i = 0; i < encoder_attention_window_sizes.size(); ++i) { | ||
| if (encoder_attention_window_sizes[i] == max_seq_len) { | ||
| encoder_attention_window_sizes[i] = new_max_seq_len; | ||
| } | ||
| } | ||
| for (size_t i = 0; i < decoder_attention_window_sizes.size(); ++i) { | ||
| if (decoder_attention_window_sizes[i] == max_seq_len) { | ||
| decoder_attention_window_sizes[i] = new_max_seq_len; | ||
| } | ||
| } | ||
| max_seq_len = new_max_seq_len; | ||
| } | ||
|
|
||
|
|
@@ -494,10 +514,23 @@ struct ModelConfig : public IFields { | |
| for (const auto& layer_config : layer_configs) { | ||
| num_heads = HWY_MAX(num_heads, layer_config.heads); | ||
| } | ||
| for (const auto& layer_config : encoder_layer_configs) { | ||
| num_heads = HWY_MAX(num_heads, layer_config.heads); | ||
| } | ||
| for (const auto& layer_config : decoder_layer_configs) { | ||
| num_heads = HWY_MAX(num_heads, layer_config.heads); | ||
| } | ||
| return num_heads; | ||
| } | ||
|
|
||
| size_t KVCacheCols() const { | ||
| if (is_encoder_decoder) { | ||
| size_t cols = 0; | ||
| for (const auto& lc : decoder_layer_configs) { | ||
| cols += lc.CacheLayerSize(); | ||
| } | ||
| return cols; | ||
| } | ||
| size_t cols = 0; | ||
| for (const auto& lc : layer_configs) { | ||
| cols += lc.CacheLayerSize(); | ||
|
|
@@ -553,6 +586,17 @@ struct ModelConfig : public IFields { | |
| InternalModelConfig internal; | ||
| bool use_global_timescale = false; // for Gemma 3 | ||
| float partial_rotary_factor = 1.0f; // Fraction of dims with RoPE (0.25 for Gemma4 MoE). | ||
|
|
||
| // Text encoder-decoder models such as T5Gemma have separate encoder and | ||
| // decoder stacks. Existing decoder-only models leave these fields at their | ||
| // defaults and continue to use `layer_configs`. | ||
| bool is_encoder_decoder = false; | ||
| uint32_t encoder_num_layers = 0; | ||
| std::vector<LayerConfig> encoder_layer_configs; | ||
| std::vector<uint32_t> encoder_attention_window_sizes; | ||
| uint32_t decoder_num_layers = 0; | ||
| std::vector<LayerConfig> decoder_layer_configs; | ||
| std::vector<uint32_t> decoder_attention_window_sizes; | ||
| }; | ||
|
|
||
| // Returns the sub-config for the ViT model of the PaliGemma model. | ||
|
|
@@ -562,6 +606,7 @@ enum DeducedLayerTypes { | |
| kDeducedViT = 2, | ||
| kDeduced448 = 4, // For ViT, 448x448 resolution instead of 224x224. | ||
| kDeducedKqNorm = 8, | ||
| kDeducedT5Gemma = 16, | ||
| }; | ||
|
|
||
| // layer_types is one or more of `DeducedLayerTypes`. | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Let's add a member variable initialized to
config.is_encoder_decoder ? config.decoder_num_layers : config.num_layers, then we can use that everywhere.