Backbone class

keras_nlp.models.Backbone(*args, dtype=None, **kwargs)

Base class for all Backbone models.

A Backbone is the basic architecture for a given NLP model. Unlike a keras_nlp.models.Task, a Backbone is not tailored to any specific loss function and training setup. A Backbone generally outputs the last hidden states of an architecture before any output predictions.

A Backbone can be used in one of two ways:

  1. Through a Task class, which will wrap and extend a Backbone so it can be used with high level Keras functions like fit(), predict() or evaluate(). Task classes are built with a particular training objective in mind (e.g. classification or language modeling).
  2. Directly, by extending underlying functional model with additional outputs and training setup. This is the most flexible approach, and can allow for any outputs, loss, or custom training loop.

All backbones include a from_preset() constructor which can be used to load a pre-trained config and weights.


# Load a BERT backbone with pre-trained weights.
backbone = keras_nlp.models.Backbone.from_preset(
# Load a GPT2 backbone with pre-trained weights at bfloat16 precision.
backbone = keras_nlp.models.Backbone.from_preset(


from_preset method

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

Instantiate a keras_nlp.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 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_nlp.models.Backbone.from_preset(), or from a model class like keras_nlp.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.


  • 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.


# Load a Gemma backbone with pre-trained weights.
model = keras_nlp.models.Backbone.from_preset(

# Load a Bert backbone with a pre-trained config and random weights.
model = keras_nlp.models.Backbone.from_preset(

token_embedding property


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

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


enable_lora method


Enable Lora on the backbone.

Calling this method will freeze all weights on the backbone, while enabling Lora on the query & value EinsumDense layers of the attention layers.


save_lora_weights method



load_lora_weights method



save_to_preset method


Save backbone to a preset directory.


  • preset_dir: The path to the local model preset directory.