Hugging Face LLM Course: Practical Transformers · Урок 2
Tokenization: BPE, WordPiece, SentencePiece
A tokenizer splits text into subwords and converts them to numbers the model understands. For chat models it also holds the dialogue format (chat template).
ENМатеріали курсів поки англійською. Інтерфейс, ШІ-наставник і перевірка завдань — українською.
Algorithms
- BPE (GPT-2, RoBERTa) — iteratively merges the most frequent symbol pairs. Its byte-level variant works on bytes, so any text, including Cyrillic and emoji, splits without unknown tokens. Most current LLMs use it: Llama 3 and Llama 4, Qwen, GPT models.
- WordPiece (BERT) — merges that maximise corpus likelihood.
- SentencePiece (T5, Llama 1 and 2) — trains BPE or Unigram directly on raw text and encodes the space as an ordinary symbol
▁, so it is whitespace-agnostic.
The examples target transformers 5. Version 5 has one tokenizer per model: the split into slow (Python) and fast (Rust) implementations is gone, and AutoTokenizer picks the backend for you.
from transformers import AutoTokenizer
tok = AutoTokenizer.from_pretrained("google-bert/bert-base-uncased")
print(tok.tokenize("Hugging Face!")) # ['hugging', 'face', '!']
enc = tok("Hugging Face!", return_tensors="pt")
print(enc.input_ids) # tensor([[101, ..., 102]])
batch_enc = tok(["Short.", "Much longer."], padding=True, truncation=True,
max_length=16, return_tensors="pt")
print(batch_enc.attention_mask) # 1=real, 0=pad
print(tok.decode([101, 7592, 2088, 102], skip_special_tokens=True)) # hello world
print(tok.decode(batch_enc.input_ids.tolist(), skip_special_tokens=True)) # list of strings
enc = tok("Hello world", return_offsets_mapping=True)
print(enc.offset_mapping) # character span of each token
Далі урок відкривається після безкоштовної реєстрації
Реєстрація через email або Google відкриває перші два уроки кожного курсу назавжди і 7 днів повного доступу до всього каталогу. Картка не потрібна.


