File size: 6,007 Bytes
a9bd396
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<!--Copyright 2020 The HuggingFace Team. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.

⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
rendered properly in your Markdown viewer.

-->

# ν† ν¬λ‚˜μ΄μ €[[tokenizer]]

ν† ν¬λ‚˜μ΄μ €λŠ” λͺ¨λΈμ˜ μž…λ ₯을 μ€€λΉ„ν•˜λŠ” 역할을 λ‹΄λ‹Ήν•©λ‹ˆλ‹€. 이 λΌμ΄λΈŒλŸ¬λ¦¬μ—λŠ” λͺ¨λ“  λͺ¨λΈμ„ μœ„ν•œ ν† ν¬λ‚˜μ΄μ €κ°€ ν¬ν•¨λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€. λŒ€λΆ€λΆ„μ˜ ν† ν¬λ‚˜μ΄μ €λŠ” 두 κ°€μ§€ λ²„μ „μœΌλ‘œ μ œκ³΅λ©λ‹ˆλ‹€. μ™„μ „ν•œ 파이썬 κ΅¬ν˜„κ³Ό Rust 라이브러리 [πŸ€— Tokenizers](https://github.com/huggingface/tokenizers)에 κΈ°λ°˜ν•œ "Fast" κ΅¬ν˜„μž…λ‹ˆλ‹€. "Fast" κ΅¬ν˜„μ€ λ‹€μŒμ„ κ°€λŠ₯ν•˜κ²Œ ν•©λ‹ˆλ‹€:

1. 특히 배치 토큰화λ₯Ό μˆ˜ν–‰ν•  λ•Œ 속도가 크게 ν–₯μƒλ©λ‹ˆλ‹€.
2. 원본 λ¬Έμžμ—΄(문자 및 단어)κ³Ό 토큰 곡간 사이λ₯Ό λ§€ν•‘ν•˜λŠ” 좔가적인 λ©”μ†Œλ“œλ₯Ό μ œκ³΅ν•©λ‹ˆλ‹€. (예: νŠΉμ • 문자λ₯Ό ν¬ν•¨ν•˜λŠ” ν† ν°μ˜ 인덱슀λ₯Ό μ–»κ±°λ‚˜, νŠΉμ • 토큰에 ν•΄λ‹Ήν•˜λŠ” 문자 λ²”μœ„λ₯Ό κ°€μ Έμ˜€λŠ” λ“±).

κΈ°λ³Έ 클래슀인 [`PreTrainedTokenizer`]와 [`PreTrainedTokenizerFast`]λŠ” λ¬Έμžμ—΄ μž…λ ₯을 μΈμ½”λ”©ν•˜λŠ” λ©”μ†Œλ“œλ₯Ό κ΅¬ν˜„ν•˜λ©°(μ•„λž˜ μ°Έμ‘°), 둜컬 νŒŒμΌμ΄λ‚˜ 디렉토리, λ˜λŠ” λΌμ΄λΈŒλŸ¬λ¦¬μ—μ„œ μ œκ³΅ν•˜λŠ” 사전 ν›ˆλ ¨λœ ν† ν¬λ‚˜μ΄μ €(HuggingFace의 AWS S3 μ €μž₯μ†Œμ—μ„œ λ‹€μš΄λ‘œλ“œλœ)λ‘œλΆ€ν„° 파이썬 및 "Fast" ν† ν¬λ‚˜μ΄μ €λ₯Ό μΈμŠ€ν„΄μŠ€ν™”ν•˜κ±°λ‚˜ μ €μž₯ν•˜λŠ” κΈ°λŠ₯을 μ œκ³΅ν•©λ‹ˆλ‹€. 이 두 ν΄λž˜μŠ€λŠ” 곡톡 λ©”μ†Œλ“œλ₯Ό ν¬ν•¨ν•˜λŠ” [`~tokenization_utils_base.PreTrainedTokenizerBase`]에 μ˜μ‘΄ν•©λ‹ˆλ‹€.

[`PreTrainedTokenizer`]와 [`PreTrainedTokenizerFast`]λŠ” λͺ¨λ“  ν† ν¬λ‚˜μ΄μ €μ—μ„œ μ‚¬μš©λ˜λŠ” μ£Όμš” λ©”μ†Œλ“œλ“€μ„ κ΅¬ν˜„ν•©λ‹ˆλ‹€:

- 토큰화(λ¬Έμžμ—΄μ„ ν•˜μœ„ 단어 토큰 λ¬Έμžμ—΄λ‘œ λΆ„ν• ), 토큰 λ¬Έμžμ—΄μ„ ID둜 λ³€ν™˜ 및 κ·Έ λ°˜λŒ€ κ³Όμ •, 그리고 인코딩/λ””μ½”λ”©(즉, 토큰화 및 μ •μˆ˜λ‘œ λ³€ν™˜)을 μˆ˜ν–‰ν•©λ‹ˆλ‹€.
- ꡬ쑰(BPE, SentencePiece λ“±)에 ꡬ애받지 μ•Šκ³  μ–΄νœ˜μ— μƒˆλ‘œμš΄ 토큰을 μΆ”κ°€ν•©λ‹ˆλ‹€.
- 특수 토큰(마슀크, λ¬Έμž₯ μ‹œμž‘ λ“±) 관리: 토큰을 μΆ”κ°€ν•˜κ³ , μ‰½κ²Œ μ ‘κ·Όν•  수 μžˆλ„λ‘ ν† ν¬λ‚˜μ΄μ €μ˜ 속성에 ν• λ‹Ήν•˜λ©°, 토큰화 κ³Όμ •μ—μ„œ λΆ„λ¦¬λ˜μ§€ μ•Šλ„λ‘ 보μž₯ν•©λ‹ˆλ‹€.

[`BatchEncoding`]은 [`~tokenization_utils_base.PreTrainedTokenizerBase`]의 인코딩 λ©”μ†Œλ“œ(`__call__`, `encode_plus`, `batch_encode_plus`)의 좜λ ₯을 λ‹΄κ³  있으며, 파이썬 λ”•μ…”λ„ˆλ¦¬λ₯Ό μƒμ†λ°›μŠ΅λ‹ˆλ‹€. ν† ν¬λ‚˜μ΄μ €κ°€ 순수 파이썬 ν† ν¬λ‚˜μ΄μ €μΈ 경우 이 ν΄λž˜μŠ€λŠ” ν‘œμ€€ 파이썬 λ”•μ…”λ„ˆλ¦¬μ²˜λŸΌ λ™μž‘ν•˜λ©°, μ΄λŸ¬ν•œ λ©”μ†Œλ“œλ“€λ‘œ κ³„μ‚°λœ λ‹€μ–‘ν•œ λͺ¨λΈ μž…λ ₯(`input_ids`, `attention_mask` λ“±)을 κ°–μŠ΅λ‹ˆλ‹€. ν† ν¬λ‚˜μ΄μ €κ°€ "Fast" ν† ν¬λ‚˜μ΄μ €μΌ 경우(즉, HuggingFace [tokenizers 라이브러리](https://github.com/huggingface/tokenizers) 기반일 경우), 이 ν΄λž˜μŠ€λŠ” μΆ”κ°€μ μœΌλ‘œ 원본 λ¬Έμžμ—΄(문자 및 단어)κ³Ό 토큰 곡간 사이λ₯Ό λ§€ν•‘ν•˜λŠ” 데 μ‚¬μš©ν•  수 μžˆλŠ” μ—¬λŸ¬ κ³ κΈ‰ μ •λ ¬ λ©”μ†Œλ“œλ₯Ό μ œκ³΅ν•©λ‹ˆλ‹€ (예: νŠΉμ • 문자λ₯Ό ν¬ν•¨ν•˜λŠ” ν† ν°μ˜ 인덱슀λ₯Ό μ–»κ±°λ‚˜, νŠΉμ • 토큰에 ν•΄λ‹Ήν•˜λŠ” 문자 λ²”μœ„λ₯Ό μ–»λŠ” λ“±).


# λ©€ν‹°λͺ¨λ‹¬ ν† ν¬λ‚˜μ΄μ €[[multimodal-tokenizer]]

κ·Έ 외에도 각 ν† ν¬λ‚˜μ΄μ €λŠ” "λ©€ν‹°λͺ¨λ‹¬" ν† ν¬λ‚˜μ΄μ €κ°€ 될 수 있으며, μ΄λŠ” ν† ν¬λ‚˜μ΄μ €κ°€ λͺ¨λ“  κ΄€λ ¨ 특수 토큰을 ν† ν¬λ‚˜μ΄μ € μ†μ„±μ˜ μΌλΆ€λ‘œ μ €μž₯ν•˜μ—¬ 더 μ‰½κ²Œ μ ‘κ·Όν•  수 μžˆλ„λ‘ ν•œλ‹€λŠ” 것을 μ˜λ―Έν•©λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄, LLaVA와 같은 λΉ„μ „-μ–Έμ–΄ λͺ¨λΈμ—μ„œ ν† ν¬λ‚˜μ΄μ €λ₯Ό κ°€μ Έμ˜€λ©΄, `tokenizer.image_token_id`에 μ ‘κ·Όν•˜μ—¬ ν”Œλ ˆμ΄μŠ€ν™€λ”λ‘œ μ‚¬μš©λ˜λŠ” 특수 이미지 토큰을 얻을 수 μžˆμŠ΅λ‹ˆλ‹€.

λͺ¨λ“  μœ ν˜•μ˜ ν† ν¬λ‚˜μ΄μ €μ— μΆ”κ°€ 특수 토큰을 ν™œμ„±ν™”ν•˜λ €λ©΄, λ‹€μŒ μ½”λ“œλ₯Ό μΆ”κ°€ν•˜κ³  ν† ν¬λ‚˜μ΄μ €λ₯Ό μ €μž₯ν•΄μ•Ό ν•©λ‹ˆλ‹€. μΆ”κ°€ 특수 토큰은 λ°˜λ“œμ‹œ νŠΉμ • λͺ¨λ‹¬λ¦¬ν‹°μ™€ 관련될 ν•„μš”λŠ” μ—†μœΌλ©°, λͺ¨λΈμ΄ 자주 μ ‘κ·Όν•΄μ•Ό ν•˜λŠ” μ–΄λ–€ 것이든 될 수 μžˆμŠ΅λ‹ˆλ‹€. μ•„λž˜ μ½”λ“œμ—μ„œ `output_dir`에 μ €μž₯된 ν† ν¬λ‚˜μ΄μ €λŠ” μ„Έ 개의 μΆ”κ°€ 특수 토큰에 직접 μ ‘κ·Όν•  수 있게 λ©λ‹ˆλ‹€.

```python
vision_tokenizer = AutoTokenizer.from_pretrained(
    "llava-hf/llava-1.5-7b-hf",
    extra_special_tokens={"image_token": "<image>", "boi_token": "<image_start>", "eoi_token": "<image_end>"}
)
print(vision_tokenizer.image_token, vision_tokenizer.image_token_id)
("<image>", 32000)
```

## PreTrainedTokenizer[[transformers.PreTrainedTokenizer]]

[[autodoc]] PreTrainedTokenizer
    - __call__
    - add_tokens
    - add_special_tokens
    - apply_chat_template
    - batch_decode
    - decode
    - encode
    - push_to_hub
    - all


## PreTrainedTokenizerFast[[transformers.PreTrainedTokenizerFast]]

[`PreTrainedTokenizerFast`]λŠ” [tokenizers](https://huggingface.co/docs/tokenizers) λΌμ΄λΈŒλŸ¬λ¦¬μ— μ˜μ‘΄ν•©λ‹ˆλ‹€. πŸ€— tokenizers λΌμ΄λΈŒλŸ¬λ¦¬μ—μ„œ 얻은 ν† ν¬λ‚˜μ΄μ €λŠ”
πŸ€— transformers둜 맀우 κ°„λ‹¨ν•˜κ²Œ κ°€μ Έμ˜¬ 수 μžˆμŠ΅λ‹ˆλ‹€. μ–΄λ–»κ²Œ ν•˜λŠ”μ§€ μ•Œμ•„λ³΄λ €λ©΄ [Using tokenizers from πŸ€— tokenizers](../fast_tokenizers) νŽ˜μ΄μ§€λ₯Ό μ°Έκ³ ν•˜μ„Έμš”.

[[autodoc]] PreTrainedTokenizerFast
    - __call__
    - add_tokens
    - add_special_tokens
    - apply_chat_template
    - batch_decode
    - decode
    - encode
    - push_to_hub
    - all

## BatchEncoding[[transformers.BatchEncoding]]

[[autodoc]] BatchEncoding