Keras 3 API documentation / KerasNLP / Pretrained Models / Llama / LlamaCausalLMPreprocessor layer

LlamaCausalLMPreprocessor layer


LlamaCausalLMPreprocessor class

    tokenizer, sequence_length=1024, add_start_token=True, add_end_token=False, **kwargs

Llama Causal LM preprocessor.

This preprocessing layer is meant for use with keras_nlp.models.LlamaCausalLM. By default, it will take in batches of strings, and return outputs in a (x, y, sample_weight) format, where the y label is the next token id in the x sequence.

For use with generation, the layer also exposes two methods generate_preprocess() and generate_postprocess(). When this preprocessor is attached to a keras_nlp.models.LlamaCausalLM instance, these methods will be called implicitly in generate(). They can also be called standalone (e.g. to precompute preprocessing inputs for generation in a separate process).


  • tokenizer: A keras_nlp.models.LlamaTokenizer instance.
  • sequence_length: The length of the packed inputs.
  • add_start_token: If True, the preprocessor will prepend the tokenizer start token to each input sequence. Default is True.
  • add_end_token: If True, the preprocessor will append the tokenizer end token to each input sequence. Default is False.

Call arguments

  • x: A string, tf.Tensor or list of python strings.
  • y: Label data. Should always be None as the layer generates labels.
  • sample_weight: Label weights. Should always be None as the layer generates label weights.
  • sequence_length: Pass to override the configured sequence_length of the layer.


# Load the preprocessor from a preset.
preprocessor = keras_nlp.models.LlamaCausalLMPreprocessor.from_preset(

# Tokenize and pack a single sentence.
sentence = tf.constant("League of legends")
# Same output.
preprocessor("League of legends")

# Tokenize a batch of sentences.
sentences = tf.constant(["Taco tuesday", "Fish taco please!"])
# Same output.
preprocessor(["Taco tuesday", "Fish taco please!"])

# Map a dataset to preprocess a single sentence.
features = tf.constant(
        "Avatar 2 is amazing!",
        "Well, I am not sure.",
labels = tf.constant([1, 0])
ds =, labels))
ds =,

# Map a dataset to preprocess unlabled sentences.
ds =
ds =,


from_preset method

LlamaCausalLMPreprocessor.from_preset(preset, **kwargs)

Instantiate a keras_nlp.models.Preprocessor 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'

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

As there are usually multiple preprocessing classes for a given model, this method should be called on a specific subclass like keras_nlp.models.BertPreprocessor.from_preset().


  • preset: string. A built in preset identifier, a Kaggle Models handle, a Hugging Face handle, or a path to a local directory.


# Load a preprocessor for Gemma generation.
preprocessor = keras_nlp.models.GemmaCausalLMPreprocessor.from_preset(

# Load a preprocessor for Bert classification.
preprocessor = keras_nlp.models.BertPreprocessor.from_preset(
Preset name Parameters Description
llama2_7b_en 6.74B LLaMA 2 7B Base model
llama2_instruct_7b_en 6.74B LLaMA 2 7B Chat model

tokenizer property


The tokenizer used to tokenize strings.