KerasHub: 预训练模型 / API 文档 / 模型架构 / Llama / LlamaCausalLMPreprocessor 层

LlamaCausalLMPreprocessor 层

[源]

LlamaCausalLMPreprocessor

keras_hub.models.LlamaCausalLMPreprocessor(
    tokenizer, sequence_length=1024, add_start_token=True, add_end_token=True, **kwargs
)

Llama Causal LM 预处理器。

这个预处理层旨在与 keras_hub.models.LlamaCausalLM 一起使用。默认情况下,它将接收批量的字符串输入,并以 (x, y, sample_weight) 格式返回输出,其中 y 标签是 x 序列中的下一个标记 ID。

为了用于生成任务,该层还提供了两个方法 generate_preprocess()generate_postprocess()。当此预处理器附加到 keras_hub.models.LlamaCausalLM 实例时,这些方法会在 generate() 中被隐式调用。它们也可以独立调用(例如,在单独的进程中为生成预计算预处理输入)。

参数

  • tokenizer: 一个 keras_hub.models.LlamaTokenizer 实例。
  • sequence_length: 打包输入的长度。
  • add_start_token: 如果为 True,预处理器会将分词器的开始标记添加到每个输入序列的前面。默认值为 True
  • add_end_token: 如果为 True,预处理器会将分词器的结束标记添加到每个输入序列的后面。默认值为 False

调用参数

  • x: 一个字符串、tf.Tensor 或 Python 字符串列表。
  • y: 标签数据。应始终为 None,因为该层会生成标签。
  • sample_weight: 标签权重。应始终为 None,因为该层会生成标签权重。
  • sequence_length: 传递此参数以覆盖层已配置的 sequence_length

示例

# Load the preprocessor from a preset.
preprocessor = keras_hub.models.LlamaCausalLMPreprocessor.from_preset(
    "llama_base_en"
)

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

# Tokenize a batch of sentences.
sentences = tf.constant(["Taco tuesday", "Fish taco please!"])
preprocessor(sentences)
# 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 = tf.data.Dataset.from_tensor_slices((features, labels))
ds = ds.map(preprocessor, num_parallel_calls=tf.data.AUTOTUNE)

# Map a dataset to preprocess unlabled sentences.
ds = tf.data.Dataset.from_tensor_slices(features)
ds = ds.map(preprocessor, num_parallel_calls=tf.data.AUTOTUNE)

[源]

from_preset 方法

LlamaCausalLMPreprocessor.from_preset(
    preset, config_file="preprocessor.json", **kwargs
)

从模型预设实例化一个 keras_hub.models.Preprocessor

预设是一组配置文件、权重和其他文件资产的目录,用于保存和加载预训练模型。preset 可以是以下之一:

  1. 内置预设标识符,例如 'bert_base_en'
  2. Kaggle Models 句柄,例如 'kaggle://user/bert/keras/bert_base_en'
  3. Hugging Face 句柄,例如 'hf://user/bert_base_en'
  4. 本地预设目录的路径,例如 './bert_base_en'

对于任何 Preprocessor 子类,您都可以运行 cls.presets.keys() 来列出该类所有可用的内置预设。

由于给定模型通常有多个预处理类,此方法应在特定的子类上调用,例如 keras_hub.models.BertTextClassifierPreprocessor.from_preset()

参数

  • preset: 字符串。一个内置预设标识符、一个 Kaggle Models 句柄、一个 Hugging Face 句柄或本地目录的路径。

示例

# Load a preprocessor for Gemma generation.
preprocessor = keras_hub.models.CausalLMPreprocessor.from_preset(
    "gemma_2b_en",
)

# Load a preprocessor for Bert classification.
preprocessor = keras_hub.models.TextClassifierPreprocessor.from_preset(
    "bert_base_en",
)
预设 参数 描述
llama2_7b_en 6.74B 70亿参数,32层,基础 LLaMA 2 模型。
llama2_instruct_7b_en 6.74B 70亿参数,32层,指令微调 LLaMA 2 模型。
vicuna_1.5_7b_en 6.74B 70亿参数,32层,指令微调 Vicuna v1.5 模型。
llama2_7b_en_int8 6.74B 70亿参数,32层,基础 LLaMA 2 模型,激活和权重量化到 int8。
llama2_instruct_7b_en_int8 6.74B 70亿参数,32层,指令微调 LLaMA 2 模型,激活和权重量化到 int8。
llama3_8b_en 8.03B 80亿参数,32层,基础 LLaMA 3 模型。
llama3_instruct_8b_en 8.03B 80亿参数,32层,指令微调 LLaMA 3 模型。
llama3_8b_en_int8 8.03B 80亿参数,32层,基础 LLaMA 3 模型,激活和权重量化到 int8。
llama3_instruct_8b_en_int8 8.03B 80亿参数,32层,指令微调 LLaMA 3 模型,激活和权重量化到 int8。

tokenizer 属性

keras_hub.models.LlamaCausalLMPreprocessor.tokenizer

用于对字符串进行分词的分词器。