Transformers documentation

ByT5

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

该模型于 2021-05-28 在 HF papers 上发布,并于 2021-06-01 贡献至 Hugging Face Transformers。

ByT5

ByT5 是 T5 模型的无分词器版本,旨在直接处理原始 UTF-8 字节。这意味着它可以处理任何语言,对拼写错误等噪声更具鲁棒性,并且由于无需预处理流水线,使用起来更加简单。

您可以在 Google 组织下找到所有原始的 ByT5 检查点。

有关如何将 ByT5 应用于不同语言任务的更多示例,请参阅 T5 文档。

下面的示例演示了如何使用 Pipeline、AutoModel 以及命令行来生成文本。

AutoModel
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer


tokenizer = AutoTokenizer.from_pretrained(
    "google/byt5-small"
)
model = AutoModelForSeq2SeqLM.from_pretrained(
    "google/byt5-small",
    device_map="auto"
)

input_ids = tokenizer("summarize: Photosynthesis is the process by which plants, algae, and some bacteria convert light energy into chemical energy.", return_tensors="pt").to(model.device)

output = model.generate(**input_ids)
print(tokenizer.decode(output[0], skip_special_tokens=True))

量化

量化通过以较低精度表示权重来降低大模型的内存负担。有关更多可用的量化后端,请参阅 量化 概述。

以下示例使用 torchao 仅将权重量化为 int4。

# pip install torchao
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, TorchAoConfig


quantization_config = TorchAoConfig("int4_weight_only", group_size=128)

model = AutoModelForSeq2SeqLM.from_pretrained(
    "google/byt5-xl",
    device_map="auto",
    quantization_config=quantization_config
)

tokenizer = AutoTokenizer.from_pretrained("google/byt5-xl")
input_ids = tokenizer("translate English to French: The weather is nice today.", return_tensors="pt").to(model.device)

output = model.generate(**input_ids)
print(tokenizer.decode(output[0], skip_special_tokens=True))

备注

  • 建议使用分词器进行批量推理和训练。

  • 以下示例展示了如何在没有分词器的情况下使用该模型。

    import torch
    from transformers import AutoModelForSeq2SeqLM
    
    model = AutoModelForSeq2SeqLM.from_pretrained("google/byt5-small", device_map="auto")
    
    num_special_tokens = 3
    
    input_ids = torch.tensor([list("Life is like a box of chocolates.".encode("utf-8"))]) + num_special_tokens
    labels = torch.tensor([list("La vie est comme une boîte de chocolat.".encode("utf-8"))]) + num_special_tokens
    loss = model(input_ids, labels=labels).loss
    loss.item()
  • ByT5 使用最高字节值(258、257 等)进行掩码,而不是像 {extra_id_0} 这样的哨兵标记。

    # Example: character-level denoising with mask tokens
    input_ids = tokenizer("The dog chases a ball in the park.").input_ids
    masked_input = torch.tensor([input_ids[:8] + [258] + input_ids[14:21] + [257] + input_ids[28:]])
    output = model.generate(masked_input, max_length=100)

ByT5Tokenizer

class transformers.ByT5Tokenizer

< >

( eos_token = '</s>'unk_token = '<unk>'pad_token = '<pad>'extra_ids = 125additional_special_tokens = None**kwargs )

参数

  • eos_token (str, 可选, 默认为 "</s>") — 序列结束标记。在构建包含特殊标记的序列时,序列结束使用的并非此标记,而是 sep_token。

    在构建包含特殊标记的序列时,序列末尾使用的并不是这个标记。 实际使用的是 sep_token。

  • unk_token (str,可选,默认为 "<unk>") — 未知标记。不在词汇表中的标记无法转换为 ID,因此会被设置为该标记。
  • pad_token (str,可选,默认为 "<pad>") — 用于填充的标记,例如在对长度不同的序列进行批处理时。
  • extra_ids (int, 可选,默认为 125) — 在词汇表末尾添加一定数量的额外 id,用作哨兵。这些令牌可通过“<extraid{%d}>”访问,其中“{%d}”是 0 到 extra_ids-1 之间的数字。额外令牌从词汇表的末尾索引到开头(“<extra_id_0>”是词汇表中的最后一个令牌,类似于 ByT5 预处理,参见 此处)。
  • additional_special_tokens (list[str],可选) — 分词器使用的额外特殊标记。

构建 ByT5 分词器。ByT5 直接使用原始字节的 utf-8 编码。

该分词器继承自 PreTrainedTokenizer,其中包含了大部分主要方法。用户应参考该父类以获取有关这些方法的更多信息。

build_inputs_with_special_tokens

< >

( token_ids_0: list[int]token_ids_1: list[int] | None = None ) → list[int]

参数

  • token_ids_0 (list[int]) — 将添加特殊标记的 ID 列表。
  • token_ids_1 (list[int], 可选) — 用于序列对的可选第二组 ID 列表。

返回值

list[int]

包含相应特殊标记的输入 ID列表。

通过拼接并添加特殊标记,从单个序列或序列对构建用于序列分类任务的模型输入。序列的格式如下:

  • 单条序列:X </s>
  • 序列对:A </s> B </s>

convert_tokens_to_string

< >

( tokens )

将单个字符串中的标记序列(字符串)进行转换。

create_token_type_ids_from_sequences

< >

( token_ids_0: list[int]token_ids_1: list[int] | None = None ) → list[int]

参数

  • token_ids_0 (list[int]) — ID 列表。
  • token_ids_1 (list[int], 可选) — 用于序列对的可选第二组 ID 列表。

返回值

list[int]

零列表。

根据传入的两个序列创建掩码,用于序列对分类任务。ByT5 不使用 token type ids,因此返回一个全零列表。

get_special_tokens_mask

< >

( token_ids_0: list[int]token_ids_1: list[int] | None = Nonealready_has_special_tokens: bool = False ) → list[int]

参数

  • token_ids_0 (list[int]) — ID 列表。
  • token_ids_1 (list[int], 可选) — 用于序列对的可选第二组 ID 列表。
  • already_has_special_tokens (bool,可选,默认为 False) — 指示 token 列表是否已针对该模型格式化为包含特殊 token 的形式。

返回值

list[int]

一个整数列表,取值范围为 [0, 1]:1 表示特殊标记,0 表示序列标记。

从未添加特殊标记的标记列表中获取序列 ID。当使用分词器的 prepare_for_model 方法添加特殊标记时,会调用此方法。

Update on GitHub