LlamaCausalLM model
- 원본 링크 : https://keras.io/api/keras_hub/models/llama/llama_causal_lm/
- 최종 확인 : 2024-11-26
LlamaCausalLM class
keras_hub.models.LlamaCausalLM(backbone, preprocessor=None, **kwargs)An end-to-end Llama model for causal language modeling.
A causal language model (LM) predicts the next token based on previous
tokens. This task setup can be used to train the model unsupervised on
plain text input, or to autoregressively generate plain text similar to
the data used for training. This task can be used for pre-training or
fine-tuning a LLaMA model, simply by calling fit().
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(). You can recompile the model with
different keras_hub.samplers objects to control the generation. By
default, "top_k" sampling will be used.
Arguments
- backbone: A
keras_hub.models.LlamaBackboneinstance. - preprocessor: A
keras_hub.models.LlamaCausalLMPreprocessororNone. IfNone, this model will not apply preprocessing, and inputs should be preprocessed before calling the model.
from_preset method
LlamaCausalLM.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:
- a built-in preset identifier like
'bert_base_en' - a Kaggle Models handle like
'kaggle://user/bert/keras/bert_base_en' - a Hugging Face handle like
'hf://user/bert_base_en' - a path to a local preset directory like
'./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
- 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, saved weights will be loaded into the model architecture. IfFalse, 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 name | Parameters | Description |
|---|---|---|
| llama2_7b_en | 6.74B | 7 billion parameter, 32-layer, base LLaMA 2 model. |
| llama2_7b_en_int8 | 6.74B | 7 billion parameter, 32-layer, base LLaMA 2 model with activation and weights quantized to int8. |
| llama2_instruct_7b_en | 6.74B | 7 billion parameter, 32-layer, instruction tuned LLaMA 2 model. |
| llama2_instruct_7b_en_int8 | 6.74B | 7 billion parameter, 32-layer, instruction tuned LLaMA 2 model with activation and weights quantized to int8. |
| vicuna_1.5_7b_en | 6.74B | 7 billion parameter, 32-layer, instruction tuned Vicuna v1.5 model. |
| llama3_8b_en | 8.03B | 8 billion parameter, 32-layer, base LLaMA 3 model. |
| llama3_8b_en_int8 | 8.03B | 8 billion parameter, 32-layer, base LLaMA 3 model with activation and weights quantized to int8. |
| llama3_instruct_8b_en | 8.03B | 8 billion parameter, 32-layer, instruction tuned LLaMA 3 model. |
| llama3_instruct_8b_en_int8 | 8.03B | 8 billion parameter, 32-layer, instruction tuned LLaMA 3 model with activation and weights quantized to int8. |
generate method
LlamaCausalLM.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
- inputs: python data, tensor data, or a
tf.data.Dataset. If apreprocessoris attached to the model,inputsshould match the structure expected by thepreprocessorlayer. If apreprocessoris not attached,inputsshould match the structure expected thebackbonemodel. - max_length: Optional. int. The max length of the generated sequence.
Will default to the max configured
sequence_lengthof thepreprocessor. IfpreprocessorisNone,inputsshould be should be padded to the desired maximum length and this argument will be ignored. - stop_token_ids: Optional.
None, “auto”, or tuple of token ids. Defaults to “auto” which uses thepreprocessor.tokenizer.end_token_id. Not specifying a processor will produce an error. None stops generation after generatingmax_lengthtokens. 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. - strip_prompt: Optional. By default, generate() returns the full prompt followed by its completion generated by the model. If this option is set to True, only the newly generated text is returned.
backbone property
keras_hub.models.LlamaCausalLM.backboneA keras_hub.models.Backbone model with the core architecture.
preprocessor property
keras_hub.models.LlamaCausalLM.preprocessorA keras_hub.models.Preprocessor layer used to preprocess input.