Bird
Raised Fist0

Tokenization - BPE, WordPiece, SentencePiece, Why It Matters for LLMs

THEORYintermediateInterview freq: high
Choose your preparation mode6 modes available
β–Ά
Execution Trace
Step 1/4
Input Text

The input text 'The quick brown fox' is ready for tokenization.

🎯 Understanding the starting point of tokenization is crucial for LLMs.
input_text = 'The quick brown fox'
BPE Tokenization

BPE splits 'quick' into 'qui' and 'ck', showing subword segmentation.

🎯 BPE helps in handling out-of-vocabulary words by breaking them into known subwords.
bpe_tokens = bpe_tokenizer.tokenize(input_text)
WordPiece Tokenization

WordPiece splits 'quick' into 'qu' and '##ick', indicating subword prefixes.

🎯 WordPiece's prefix notation helps in understanding word composition.
wordpiece_tokens = wordpiece_tokenizer.tokenize(input_text)
SentencePiece Tokenization

SentencePiece adds a space marker '▁' before each word, treating spaces as tokens.

🎯 SentencePiece's approach allows for language-agnostic tokenization.
sentencepiece_tokens = sentencepiece_tokenizer.tokenize(input_text)
πŸ“Š
Diagram
Diagram
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚       Tokenization Flow       β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Input Text   β”‚ 'The quick brown fox' β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ BPE          β”‚ ['The', 'qui', 'ck', 'brown', 'fox'] β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ WordPiece    β”‚ ['The', 'qu', '##ick', 'brown', 'fox'] β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ SentencePieceβ”‚ ['▁The', '▁quick', '▁brown', '▁fox'] β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
  • Input Text: The original sentence to be tokenized.
  • BPE: Byte Pair Encoding splits words into subword units.
  • WordPiece: Similar to BPE but uses a different algorithm for subword selection.
  • SentencePiece: Treats the input as a sequence of characters, including spaces.
πŸ”‘
Key Moments
β–²
Q: Why does BPE split 'quick' into 'qui' and 'ck'?
A: BPE uses frequency of subword pairs to iteratively merge them, resulting in 'qui' and 'ck' as frequent subword units.
Q: How does WordPiece's prefix notation aid in tokenization?
A: The prefix notation '##' indicates continuation of a word, helping models understand word boundaries and compositions.
Q: What advantage does SentencePiece offer over BPE and WordPiece?
A: SentencePiece treats spaces as tokens, allowing for consistent tokenization across languages without pre-segmented input.

This diagram and trace explain the tokenization process using BPE, WordPiece, and SentencePiece methods. Tokenization is crucial for LLMs as it determines how text is broken down into manageable units for processing. BPE and WordPiece focus on subword units, which help in handling out-of-vocabulary words, while SentencePiece treats spaces as tokens, offering a language-agnostic approach. Understanding these methods is essential for optimizing LLM performance and ensuring effective text representation.

Primary Focus

βœ“ Tokenization methods and their impact on LLMs

Practice

(1/5)
1. In a deep learning model, when is it more appropriate to use Layer Normalization instead of Batch Normalization?
easy
A. When the model is trained on a very large batch size.
B. When the model is trained on a very small batch size or online learning.
C. When the model has a very deep architecture.
D. When the model is trained on a balanced dataset.

Solution

  1. Step 1: Understand the context of normalization techniques.

    Batch Normalization relies on batch statistics, which can be unstable with small batch sizes. Layer Normalization uses layer statistics, making it more stable for small batches or online learning.
  2. Step 2: Eliminate wrong options.

    When the model is trained on a very large batch size. is incorrect because large batch sizes are suitable for Batch Normalization. When the model has a very deep architecture. is incorrect because depth doesn't directly affect the choice between these normalizations. When the model is trained on a balanced dataset. is irrelevant to normalization choice.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Layer Norm for small batches or online learning. [OK]
Hint: Layer Norm for small batches or online learning. [OK]
Common Mistakes:
2. Given the following Python code snippet using a pre-trained YOLO model, what will be the output shape of the detection results?
import torch
from torchvision.models.detection import yolo_v3

model = yolo_v3(pretrained=True)
model.eval()

input_tensor = torch.rand(1, 3, 416, 416)
output = model(input_tensor)
print(output[0]['boxes'].shape)
easy
A. (1, 4)
B. (N, 4)
C. (N, 5)
D. (1, 5)

Solution

  1. Step 1: Understand YOLO output

    YOLO outputs a list of detections, each with a bounding box of 4 coordinates.
  2. Step 2: Determine output shape

    The shape is (N, 4) where N is the number of detected objects.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    YOLO boxes = (N, 4) [OK]
Hint: YOLO boxes = (N, 4) [OK]
Common Mistakes:
3. Given the following PyTorch code snippet for an LSTM layer, what is the shape of the output h_n?
import torch
lstm = torch.nn.LSTM(input_size=10, hidden_size=20, num_layers=2)
input = torch.randn(5, 3, 10)
output, (h_n, c_n) = lstm(input)
easy
A. (5, 3, 20)
B. (2, 3, 20)
C. (3, 5, 20)
D. (2, 5, 20)

Solution

  1. Step 1: Understand LSTM output shapes.

    The h_n shape is (num_layers, batch_size, hidden_size).
  2. Step 2: Apply the given parameters.

    Here, num_layers=2, batch_size=3, hidden_size=20, so the shape is (2, 3, 20).
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    h_n shape: (layers, batch, hidden). [OK]
Hint: h_n shape: (layers, batch, hidden). [OK]
Common Mistakes:
4. In a scenario where computational resources are limited, which approach is more suitable for sequence-to-sequence tasks and why?
medium
A. Use RNNs because they require less memory than attention mechanisms.
B. Use RNNs because they are faster to train than attention mechanisms.
C. Use attention mechanisms because they allow for parallel processing.
D. Use attention mechanisms because they are more interpretable than RNNs.

Solution

  1. Step 1: Evaluate computational efficiency.

    Attention mechanisms allow for parallel processing, making them more efficient with limited resources.
  2. Step 2: Eliminate wrong options.

    Use RNNs because they require less memory than attention mechanisms. is incorrect as attention mechanisms can be more memory efficient due to parallelism. Use RNNs because they are faster to train than attention mechanisms. is incorrect because RNNs are slower due to sequential processing. Use attention mechanisms because they are more interpretable than RNNs. is incorrect as interpretability is not the main concern here.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Parallel processing saves resources. [OK]
Hint: Parallel processing saves resources. [OK]
Common Mistakes:
5. A neural network is not learning effectively, and the gradients are extremely small. What is the most likely cause?
medium
A. The learning rate is too high.
B. The network is too shallow.
C. The activation function is causing vanishing gradients.
D. The dataset is too small.

Solution

  1. Step 1: Identify symptoms of vanishing gradients

    Extremely small gradients often indicate the vanishing gradient problem, typically caused by certain activation functions like sigmoid or tanh.
  2. Step 2: Eliminate wrong options

    The learning rate is too high. is incorrect because a high learning rate would cause divergence. The network is too shallow. is incorrect because a shallow network is less prone to vanishing gradients. The dataset is too small. is unrelated to gradient size.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Small gradients often mean vanishing issue. [OK]
Hint: Small gradients often mean vanishing issue. [OK]
Common Mistakes:
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard