Transformers 文件
ByT5
並獲得增強的文件體驗
開始使用
ByT5
ByT5 是 T5 模型的無分詞器(tokenizer-free)版本,旨在直接處理原始 UTF-8 位元組。這意味著它可以處理任何語言,對拼寫錯誤等噪聲更具魯棒性,並且使用起來更簡單,因為它不需要預處理管道。
你可以在 Google 組織下找到所有原始的 ByT5 檢查點。
請參考 T5 文件,瞭解如何將 ByT5 應用於不同語言任務的更多示例。
下面的示例演示瞭如何使用 Pipeline、AutoModel 和命令列生成文字。
import torch
from transformers import pipeline
pipeline = pipeline(
task="text2text-generation",
model="google/byt5-small",
torch_dtype=torch.float16,
device=0
)
pipeline("translate English to French: The weather is nice today")
量化
量化透過以較低精度表示權重來減少大型模型的記憶體負擔。有關更多可用量化後端,請參閱量化概述。
以下示例使用torchao僅將權重量化為int4。
# pip install torchao
import torch
from transformers import TorchAoConfig, AutoModelForSeq2SeqLM, AutoTokenizer
quantization_config = TorchAoConfig("int4_weight_only", group_size=128)
model = AutoModelForSeq2SeqLM.from_pretrained(
"google/byt5-xl",
torch_dtype=torch.bfloat16,
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("cuda")
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") 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 = 125 additional_special_tokens = None **kwargs )
引數
- eos_token (
str
,可選,預設為"</s>"
) — 序列結束標記。當使用特殊標記構建序列時,這不是用於序列結束的標記。所使用的標記是 `sep_token`。
- unk_token (
str
,可選,預設為"<unk>"
) — 未知標記。不在詞彙表中的標記無法轉換為 ID,將被設定為此標記。 - pad_token (
str
,可選,預設為"<pad>"
) — 用於填充的標記,例如在對不同長度的序列進行批處理時使用。 - extra_ids (
int
,可選,預設為 125) — 在詞彙表末尾新增的額外 ID 的數量,用作哨兵標記。這些標記可以透過“”訪問,其中“{%d}”是介於 0 和 extra_ids-1 之間的數字。額外標記從詞彙表末尾向前索引(“ ”是詞彙表中的最後一個標記,類似於 ByT5 預處理,請參見此處)。 - additional_special_tokens (
list[str]
,可選) — 分詞器使用的附加特殊標記。
構建一個 ByT5 分詞器。ByT5 僅使用原始位元組的 utf-8 編碼。
該分詞器繼承自 PreTrainedTokenizer,其中包含了大部分主要方法。使用者應參考此超類以獲取有關這些方法的更多資訊。
build_inputs_with_special_tokens
< 源 >( token_ids_0: list token_ids_1: typing.Optional[list[int]] = None ) → list[int]
透過連線和新增特殊標記,從序列或序列對構建用於序列分類任務的模型輸入。序列格式如下:
- 單個序列:
X </s>
- 序列對:
A </s> B </s>
將標記序列(字串)轉換為單個字串。
create_token_type_ids_from_sequences
< 源 >( token_ids_0: list token_ids_1: typing.Optional[list[int]] = None ) → list[int]
根據傳入的兩個序列建立掩碼,用於序列對分類任務。ByT5 不使用標記型別 ID,因此返回一個全零列表。
get_special_tokens_mask
< 源 >( token_ids_0: list token_ids_1: typing.Optional[list[int]] = None already_has_special_tokens: bool = False ) → list[int]
從沒有新增特殊標記的標記列表中檢索序列ID。此方法在使用分詞器prepare_for_model
方法新增特殊標記時呼叫。