T5Gemma2Backbone model

[source]

T5Gemma2Backbone class

keras_hub.models.T5Gemma2Backbone(
    vocabulary_size,
    encoder_hidden_dim,
    encoder_intermediate_dim,
    encoder_num_layers,
    encoder_num_attention_heads,
    encoder_num_key_value_heads,
    encoder_head_dim,
    encoder_layer_types,
    decoder_hidden_dim,
    decoder_intermediate_dim,
    decoder_num_layers,
    decoder_num_attention_heads,
    decoder_num_key_value_heads,
    decoder_head_dim,
    decoder_layer_types,
    dropout_rate=0.0,
    rms_norm_eps=1e-06,
    query_pre_attn_scalar=1.0,
    attention_bias=False,
    hidden_activation="gelu_approximate",
    tie_word_embeddings=True,
    initializer_range=0.02,
    attention_dropout=0.0,
    sliding_window=None,
    cross_attention_hidden_size=None,
    attn_logit_softcapping=None,
    final_logit_softcapping=None,
    rope_max_wavelength=10000.0,
    global_rope_scaling_factor=1.0,
    encoder_rope_max_wavelength=None,
    encoder_global_rope_scaling_factor=None,
    use_query_key_norm=True,
    vision_encoder=None,
    eoi_token_index=256000,
    dtype=None,
    **kwargs
)

T5Gemma2 backbone model.

This class implements the encoder-decoder backbone of the T5Gemma2 model. T5Gemma2 is based on Gemma3 and features merged self+cross attention in the decoder (unlike T5Gemma which used separate attention sublayers), Gemma3-style Q/K normalization, and per-layer-type sliding window attention patterns.

When a vision_encoder is provided, the model also accepts image inputs. Images are processed by the vision encoder and the resulting embeddings are interleaved into the encoder text embeddings at positions marked by image placeholder tokens.

Arguments

  • vocabulary_size: int, The size of the vocabulary.
  • encoder_hidden_dim: int, Encoder hidden dimensionality.
  • encoder_intermediate_dim: int, Encoder FFN intermediate size.
  • encoder_num_layers: int, Number of encoder layers.
  • encoder_num_attention_heads: int, Encoder attention heads.
  • encoder_num_key_value_heads: int, Encoder KV heads for GQA.
  • encoder_head_dim: int, Encoder head dimensionality.
  • encoder_layer_types: list of str, Attention layer types for each encoder layer ("full_attention" or "sliding_attention").
  • decoder_hidden_dim: int, Decoder hidden dimensionality.
  • decoder_intermediate_dim: int, Decoder FFN intermediate size.
  • decoder_num_layers: int, Number of decoder layers.
  • decoder_num_attention_heads: int, Decoder attention heads.
  • decoder_num_key_value_heads: int, Decoder KV heads for GQA.
  • decoder_head_dim: int, Decoder head dimensionality.
  • decoder_layer_types: list of str, Attention layer types for each decoder layer.
  • dropout_rate: float, Dropout rate. Defaults to 0.0.
  • rms_norm_eps: float, RMS normalization epsilon. Defaults to 1e-6.
  • query_pre_attn_scalar: float, Query scalar. Defaults to 1.0.
  • attention_bias: bool, Attention bias. Defaults to False.
  • hidden_activation: str, FFN activation. Defaults to "gelu_approximate".
  • tie_word_embeddings: bool, Tie input/output embeddings. Defaults to True.
  • initializer_range: float, Initializer range. Defaults to 0.02.
  • attention_dropout: float, Attention dropout. Defaults to 0.0.
  • sliding_window: int, optional, Sliding window size.
  • cross_attention_hidden_size: int, optional, Cross-attention hidden size. Defaults to encoder_hidden_dim.
  • attn_logit_softcapping: float, optional, Attention softcapping.
  • final_logit_softcapping: float, optional, Final logit softcapping.
  • rope_max_wavelength: float, RoPE maximum wavelength. Defaults to 10000.0.
  • global_rope_scaling_factor: float, RoPE scaling factor for full attention layers. Defaults to 1.0.
  • use_query_key_norm: bool, Whether to use Gemma3-style Q/K normalization. Defaults to True.
  • vision_encoder: optional, A Gemma3VisionEncoder instance for multimodal inputs. When None, the model is text-only.
  • eoi_token_index: int, Token index for the end-of-image token. Defaults to 256000.
  • dtype: dtype for computations. Defaults to None.
  • **kwargs: Additional keyword arguments.

Examples

import numpy as np
from keras_hub.models import T5Gemma2Backbone

input_data = {
    "encoder_token_ids": np.ones(shape=(1, 12), dtype="int32"),
    "encoder_padding_mask": np.array(
        [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0]], dtype="int32"
    ),
    "decoder_token_ids": np.ones(shape=(1, 8), dtype="int32"),
    "decoder_padding_mask": np.array(
        [[1, 1, 1, 1, 1, 1, 1, 1]], dtype="int32"
    ),
}

model = T5Gemma2Backbone(
    vocabulary_size=32000,
    encoder_hidden_dim=256,
    encoder_intermediate_dim=512,
    encoder_num_layers=4,
    encoder_num_attention_heads=4,
    encoder_num_key_value_heads=2,
    encoder_head_dim=64,
    encoder_layer_types=["full_attention"] * 4,
    decoder_hidden_dim=256,
    decoder_intermediate_dim=512,
    decoder_num_layers=4,
    decoder_num_attention_heads=4,
    decoder_num_key_value_heads=2,
    decoder_head_dim=64,
    decoder_layer_types=["full_attention"] * 4,
    dropout_rate=0.1,
    rms_norm_eps=1e-6,
    query_pre_attn_scalar=1.0,
    attention_bias=False,
    hidden_activation="gelu_approximate",
)
output = model(input_data)

[source]

from_preset method

T5Gemma2Backbone.from_preset(preset, load_weights=True, **kwargs)

Instantiate a keras_hub.models.Backbone from a model preset.

A preset is a directory of configs, weights and other file assets used to save and load a pre-trained model. The preset can be passed as a one of:

  1. a built-in preset identifier like 'bert_base_en'
  2. a Kaggle Models handle like 'kaggle://user/bert/keras/bert_base_en'
  3. a Hugging Face handle like 'hf://user/bert_base_en'
  4. a ModelScope handle like 'modelscope://user/bert_base_en'
  5. a path to a local preset directory like './bert_base_en'

This constructor can be called in one of two ways. Either from the base class like keras_hub.models.Backbone.from_preset(), or from a model class like keras_hub.models.GemmaBackbone.from_preset(). If calling from the base class, the subclass of the returning object will be inferred from the config in the preset directory.

For any Backbone subclass, you can run cls.presets.keys() to list all built-in presets available on the class.

Arguments

  • preset: string. A built-in preset identifier, a Kaggle Models handle, a Hugging Face handle, or a path to a local directory.
  • load_weights: bool. If True, the weights will be loaded into the model architecture. If False, the weights will be randomly initialized.

Examples

# Load a Gemma backbone with pre-trained weights.
model = keras_hub.models.Backbone.from_preset(
    "gemma_2b_en",
)

# Load a Bert backbone with a pre-trained config and random weights.
model = keras_hub.models.Backbone.from_preset(
    "bert_base_en",
    load_weights=False,
)
Preset Parameters Description
t5gemma2_270m_270m 953.80M Encoder–decoder (T5-style) based out of Gemma3 model with 270M encoder + 270M decoder parameters, supporting text generation, multilingual tasks and long-context inputs.
t5gemma2_1b_1b 2.42B Encoder–decoder (T5-style) based out of Gemma3 model with 1B encoder + 1B decoder parameters, supporting text generation, multilingual tasks and long-context inputs.
t5gemma2_4b_4b 8.18B Encoder–decoder (T5-style) based out of Gemma3 model with 4B encoder + 4B decoder parameters, supporting text generation, multilingual tasks and long-context inputs.

token_embedding property

keras_hub.models.T5Gemma2Backbone.token_embedding

A keras.layers.Embedding instance for embedding token ids.

This layer embeds integer token ids to the hidden dim of the model.