ModernBertBackbone classkeras_hub.models.ModernBertBackbone(
vocabulary_size=50368,
hidden_dim=768,
intermediate_dim=1152,
num_layers=22,
num_heads=12,
local_attention_window=128,
global_attn_every_n_layers=3,
rotary_max_wavelength=160000,
local_rotary_max_wavelength=10000,
layer_norm_epsilon=1e-05,
dtype=None,
**kwargs
)
ModernBERT backbone model.
ModernBERT features Rotary Positional Embeddings (RoPE), GeGLU activations, Layer Normalization, and alternating local and global attention layers.
The backbone accepts a dictionary of token IDs and padding masks, and outputs the raw hidden sequence representations.
Arguments
128.3.160000.None, uses rotary_max_wavelength. Defaults to
10000.1e-5.keras.DTypePolicy. The dtype of the layers.
Defaults to None.Examples
import keras_hub
import numpy as np
# Instantiate a ModernBERT backbone
backbone = keras_hub.models.ModernBertBackbone(
vocabulary_size=50368,
hidden_dim=768,
intermediate_dim=1152,
num_layers=22,
num_heads=12,
)
# Prepare dummy input data
input_data = {
"token_ids": np.random.randint(0, 50368, size=(2, 512), dtype="int32"),
"padding_mask": np.ones((2, 512), dtype="int32"),
}
# Extract hidden states
outputs = backbone(input_data)
from_preset methodModernBertBackbone.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:
'bert_base_en''kaggle://user/bert/keras/bert_base_en''hf://user/bert_base_en''modelscope://user/bert_base_en''./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
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 |
|---|---|---|
| modern_bert_base_en | 149.01M | 22-layer ModernBERT Base encoder model pretrained on English for masked language modeling. Uses Rotary Position Embeddings (RoPE), alternating local and global attention, and GeGLU feedforward layers. |
| modern_bert_embed_base | 149.01M | 22-layer ModernBERT Base sentence embedding model from Nomic AI (nomic-ai/modernbert-embed-base). Produces 768-dimensional, L2-normalized, mean-pooled embeddings for inputs of up to 8192 tokens. Prefix queries with 'search_query: ' and documents with 'search_document: '. |
| gte_modern_bert_base | 149.01M | 22-layer ModernBERT Base sentence embedding model from Alibaba-NLP (Alibaba-NLP/gte-modernbert-base). Produces 768-dimensional [CLS]-pooled embeddings that are not normalized, so compare them with cosine similarity. The model supports inputs of up to 8192 tokens. |
| modern_bert_large_en | 394.78M | 28-layer ModernBERT Large encoder model pretrained on English for masked language modeling. Uses Rotary Position Embeddings (RoPE), alternating local and global attention, and GeGLU feedforward layers. |
token_embedding propertykeras_hub.models.ModernBertBackbone.token_embedding
A keras.layers.Embedding instance for embedding token ids.
This layer embeds integer token ids to the hidden dim of the model.