T5Gemma2Seq2SeqLM classkeras_hub.models.T5Gemma2Seq2SeqLM(backbone, preprocessor=None, **kwargs)
An end-to-end T5Gemma2 model for seq2seq language modeling.
A seq2seq language model (LM) is an encoder-decoder model which is used for conditional text generation. The encoder is given a "context" text (fed to the encoder), and the decoder predicts the next token based on both the encoder inputs and the previous tokens.
T5Gemma2 extends T5Gemma by using Gemma3-based components, with merged self+cross attention in the decoder, Gemma3-style Q/K normalization, and per-layer-type RoPE.
This model has a generate() method, which generates text based on
a prompt. The generation strategy used is controlled by an additional
sampler argument on compile().
Arguments
keras_hub.models.T5Gemma2Backbone instance.keras_hub.models.T5Gemma2Seq2SeqLMPreprocessor
or None. Defaults to None.Examples
Use generate() to do text generation.
t5gemma2_lm = keras_hub.models.T5Gemma2Seq2SeqLM.from_preset(
"t5gemma2_270m_270m"
)
t5gemma2_lm.generate(
"The quick brown fox jumped.", max_length=30
)
Custom backbone and vocabulary.
tokenizer = keras_hub.models.T5Gemma2Tokenizer(
proto="proto.spm",
)
preprocessor = keras_hub.models.T5Gemma2Seq2SeqLMPreprocessor(
tokenizer=tokenizer,
encoder_sequence_length=128,
decoder_sequence_length=128,
)
backbone = keras_hub.models.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",
)
t5gemma2_lm = keras_hub.models.T5Gemma2Seq2SeqLM(
backbone=backbone,
preprocessor=preprocessor,
)
from_preset methodT5Gemma2Seq2SeqLM.from_preset(preset, load_weights=True, **kwargs)
Instantiate a keras_hub.models.Task 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
one of:
'bert_base_en''kaggle://user/bert/keras/bert_base_en''hf://user/bert_base_en''./bert_base_en'For any Task subclass, you can run cls.presets.keys() to list all
built-in presets available on the class.
This constructor can be called in one of two ways. Either from a task
specific base class like keras_hub.models.CausalLM.from_preset(), or
from a model class like
keras_hub.models.BertTextClassifier.from_preset().
If calling from the a base class, the subclass of the returning object
will be inferred from the config in the preset directory.
Arguments
True, saved weights will be loaded into
the model architecture. If False, all weights will be
randomly initialized.Examples
# Load a Gemma generative task.
causal_lm = keras_hub.models.CausalLM.from_preset(
"gemma_2b_en",
)
# Load a Bert classification task.
model = keras_hub.models.TextClassifier.from_preset(
"bert_base_en",
num_classes=2,
)
| 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. |
generate methodT5Gemma2Seq2SeqLM.generate(
inputs, max_length=None, stop_token_ids="auto", strip_prompt=False
)
Generate text given prompt inputs.
This method generates text based on given inputs. The sampling method
used for generation can be set via the compile() method.
If inputs are a tf.data.Dataset, outputs will be generated
"batch-by-batch" and concatenated. Otherwise, all inputs will be handled
as a single batch.
If a preprocessor is attached to the model, inputs will be
preprocessed inside the generate() function and should match the
structure expected by the preprocessor layer (usually raw strings).
If a preprocessor is not attached, inputs should match the structure
expected by the backbone. See the example usage above for a
demonstration of each.
Arguments
tf.data.Dataset. If a
preprocessor is attached to the model, inputs should match
the structure expected by the preprocessor layer. If a
preprocessor is not attached, inputs should match the
structure expected the backbone model.sequence_length of the
preprocessor. If preprocessor is None, inputs should be
should be padded to the desired maximum length and this argument
will be ignored.None, "auto", or tuple of token ids.
Defaults to "auto" which uses the
preprocessor.tokenizer.end_token_id. Not specifying a
processor will produce an error. None stops generation after
generating max_length tokens. You may also specify a list of
token id's the model should stop on. Note that sequences of
tokens will each be interpreted as a stop token, multi-token
stop sequences are not supported.backbone propertykeras_hub.models.T5Gemma2Seq2SeqLM.backbone
A keras_hub.models.Backbone model with the core architecture.
preprocessor propertykeras_hub.models.T5Gemma2Seq2SeqLM.preprocessor
A keras_hub.models.Preprocessor layer used to preprocess input.