KerasHub:预训练模型 / API 文档 / 模型架构 / Llama3 / Llama3CausalLMPreprocessor 层

Llama3CausalLMPreprocessor 层

[来源]

Llama3CausalLMPreprocessor

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

Llama 3 因果 LM 预处理器。

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

为了与生成一起使用,该层还公开了两种方法 generate_preprocess()generate_postprocess()。当此预处理器附加到 keras_hub.models.Llama3CausalLM 实例时,这些方法将在 generate() 中隐式调用。它们也可以独立调用(例如,在单独的进程中预计算生成的预处理输入)。

参数

  • tokenizer:一个 keras_hub.models.Llama3Tokenizer 实例。
  • sequence_length:打包输入的长度。
  • add_start_token:如果为 True,预处理器将在每个输入序列前添加 tokenizer 的起始 token。默认为 False
  • add_end_token:如果为 True,预处理器将在每个输入序列后添加 tokenizer 的结束 token。默认为 False

调用参数

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

示例

# Load the preprocessor from a preset.
preprocessor = keras_hub.models.Llama3CausalLMPreprocessor.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 方法

Llama3CausalLMPreprocessor.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",
)
预设 参数 描述
llama3_8b_en 8.03B 80.3 亿参数,32 层,基础 LLaMA 3 模型。
llama3_instruct_8b_en 8.03B 80.3 亿参数,32 层,指令调优的 LLaMA 3 模型。
llama3_8b_en_int8 8.03B 80.3 亿参数,32 层,基础 LLaMA 3 模型,激活和权重量化为 int8。
llama3_instruct_8b_en_int8 8.03B 80.3 亿参数,32 层,指令调优的 LLaMA 3 模型,激活和权重量化为 int8。

tokenizer 属性

keras_hub.models.Llama3CausalLMPreprocessor.tokenizer

用于 token 化字符串的分词器。