Tokenizer#

Open in Colab

本教程展示如何使用 Gemma 分词器。理解分词器对于正确地向模型输入内容非常重要。

想要了解更多关于分词器的信息,请观看 Andrej Karpathy 的精彩演讲。

!pip install -q gemma
# Common imports

# Gemma imports
from gemma import gm

Tokenizer basics#

Gemma 分词器可以直接使用

tokenizer = gm.text.Gemma3Tokenizer()

tokens 的总数可以通过 .vocab_size 获取

tokenizer.vocab_size
256000

Encoding#

你可以编码一个字符串

  • 使用 .encode 编码成 token ids

tokenizer.encode('Derinkuyu is an underground city.')
[8636, 979, 78904, 603, 671, 30073, 3413, 235265]
  • 使用 .split 编码成 token 字符串

tokenizer.split('Derinkuyu is an underground city.')
['Der', 'ink', 'uyu', ' is', ' an', ' underground', ' city', '.']

需要注意的是,空格 是 tokens 的一部分。例如,这意味着对于模型来说, hellohello 映射到 2 个不同的 token ids。

tokenizer.encode(' hello');
tokenizer.encode('hello');
[25612]
[17534]

如果进行下一个词预测,重要的是不要添加尾随空格,因为它会使模型超出分布范围。

# When encoding this sentence, the last token will be an empty whitespace,
# which is unusual for the model.
tokenizer.split('The capital of France is ')
['The', ' capital', ' of', ' France', ' is', ' ']

Decoding#

Tokens 可以使用 .decode 解码。你可以解码单个 id 或整个句子。

tokenizer.decode([8636, 979, 78904, 603, 671, 30073, 3413, 235265])
'Derinkuyu is an underground city.'
tokenizer.decode(4567)
'Med'

Controls tokens#

一些 tokens 具有特殊含义。忽略这些可能会严重影响模型质量。

特殊 token ids 可以通过 tokenizer.special_tokens 属性访问。

<bos> / <eos>#

在 Gemma 模型中,句子开始 token (<bos>) 应该只在输入的开头出现一次。你可以显式添加它,或者使用 add_eos=True

token_ids = tokenizer.encode('Hello world!')
token_ids = [tokenizer.special_tokens.BOS] + token_ids
token_ids
[<_Gemma2SpecialTokens.BOS: 2>, 4521, 2134, 235341]
tokenizer.encode('Hello world!', add_bos=True)
[<_Gemma2SpecialTokens.BOS: 2>, 4521, 2134, 235341]

类似地,模型可以输出一个 <eos> token 来指示预测完成。

当微调 Gemma 时,你可以训练模型来预测 <eos> tokens。

tokenizer.encode('Hello world!', add_eos=True)
[4521, 2134, 235341, <_Gemma2SpecialTokens.EOS: 1>]

<start_of_turn> / <end_of_turn>#

当使用指令微调版本的 Gemma 时,<start_of_turn> / <end_of_turn> tokens 允许指定是用户还是模型在说话。

<start_of_turn> 应该后跟

  • 用户

  • 模型

用户和模型对话示例

token_ids = tokenizer.encode("""<start_of_turn>user
Knock knock.<end_of_turn>
<start_of_turn>model
Who's there ?<end_of_turn>
<start_of_turn>user
Gemma.<end_of_turn>
<start_of_turn>model
Gemma who?<end_of_turn>""")
tokenizer.decode(token_ids[0])
'<start_of_turn>'

<start_of_image>#

在 Gemma 3 中,为了指示图像在文本中的位置,提示应该包含特殊的 <start_of_image> token。在内部,Gemma 模型将自动扩展 token 以插入软图像 tokens。

(注意:还有一个 <end_of_image> token,但由模型内部处理)

Custom tokens#

在所有 Gemma 版本中,一些 tokens (99 个) 是未使用的。这允许自定义应用程序为他们的应用程序定义和微调他们自己的自定义 tokens。这些 tokens 可以通过 tokenizer.special_tokens.CUSTOM + xx 获取,其中 xx 是介于 098 之间的数字

tokenizer.decode(tokenizer.special_tokens.CUSTOM + 17)
'<unused17>'

你可以在构建分词器时自定义自定义 tokens 对应的含义。

tokenizer = gm.text.Gemma3Tokenizer(
    custom_tokens={
        0: '<my_custom_tag>',
        17: '<my_other_tag>',
    },
)

tokenizer.encode('<my_other_tag>')
[24]

自定义 tokens 字符串被编码为匹配的 token id。

tokenizer.special_tokens.CUSTOM + 17
24
tokenizer.decode(tokenizer.special_tokens.CUSTOM + 17)
'<my_other_tag>'