Add README with usage and example output
Browse files
README.md
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Tokenizer Inspector
|
| 2 |
+
|
| 3 |
+
A small CLI tool for SLM builders: inspect and compare Hugging Face tokenizers before you commit one to a training run.
|
| 4 |
+
|
| 5 |
+
## What it tells you
|
| 6 |
+
|
| 7 |
+
- **Vocab size** and token type (byte-level BPE, SentencePiece Unigram, WordPiece, etc.)
|
| 8 |
+
- **Special tokens** — what's added on top of the learned vocab
|
| 9 |
+
- **Chars/token** on your test corpus — higher means more compact (fewer tokens to train on)
|
| 10 |
+
- **Vocab utilization** — what fraction of the vocab your data actually touches
|
| 11 |
+
- **Roundtrip check** — does `decode(encode(text))` give you back the same text?
|
| 12 |
+
- **Top tokens** — what the tokenizer is actually producing for your data
|
| 13 |
+
- **Side-by-side comparison** of two or more tokenizers on the same corpus
|
| 14 |
+
|
| 15 |
+
## Install
|
| 16 |
+
|
| 17 |
+
```bash
|
| 18 |
+
pip install tokenizers huggingface_hub
|
| 19 |
+
```
|
| 20 |
+
|
| 21 |
+
## Usage
|
| 22 |
+
|
| 23 |
+
```bash
|
| 24 |
+
# Inspect a single tokenizer by repo ID
|
| 25 |
+
python3 tokenizer_inspector.py gpt2
|
| 26 |
+
|
| 27 |
+
# Compare two tokenizers side by side
|
| 28 |
+
python3 tokenizer_inspector.py gpt2 HuggingFaceTB/SmolLM2-135M --compare
|
| 29 |
+
|
| 30 |
+
# Tokenize your own text
|
| 31 |
+
python3 tokenizer_inspector.py gpt2 --text "Hello, world! This is a test."
|
| 32 |
+
|
| 33 |
+
# Show the first 30 tokens produced
|
| 34 |
+
python3 tokenizer_inspector.py gpt2 --show-tokens
|
| 35 |
+
|
| 36 |
+
# Use a local tokenizer.json file
|
| 37 |
+
python3 tokenizer_inspector.py ./my_tokenizer.json
|
| 38 |
+
|
| 39 |
+
# JSON output (for scripting)
|
| 40 |
+
python3 tokenizer_inspector.py gpt2 --json
|
| 41 |
+
```
|
| 42 |
+
|
| 43 |
+
## Example output
|
| 44 |
+
|
| 45 |
+
```
|
| 46 |
+
============================================================
|
| 47 |
+
COMPARISON (2 tokenizers)
|
| 48 |
+
============================================================
|
| 49 |
+
Metric gpt2 HuggingFaceTB/Smol
|
| 50 |
+
-------------------- -------------------- --------------------
|
| 51 |
+
Vocab size 50,257 49,152
|
| 52 |
+
Token type byte-level BPE byte-level BPE
|
| 53 |
+
Special tokens 1 17
|
| 54 |
+
Chars/token 2.6 2.72
|
| 55 |
+
Corpus tokens 116 111
|
| 56 |
+
Vocab util 0.13% 0.12%
|
| 57 |
+
Longest tok 128 81
|
| 58 |
+
Roundtrip ok ok
|
| 59 |
+
```
|
| 60 |
+
|
| 61 |
+
## When to use this
|
| 62 |
+
|
| 63 |
+
- **Before choosing a tokenizer for training**: compare chars/token on a sample of your actual data, not just English prose
|
| 64 |
+
- **Debugging a bad tokenizer**: if roundtrip fails, your tokenizer is lossy
|
| 65 |
+
- **Vocab sizing**: if vocab utilization is very low (<1%), your vocab is oversized for your data
|
| 66 |
+
- **Special token audit**: see exactly what control tokens are in the vocab
|
| 67 |
+
|
| 68 |
+
## Notes
|
| 69 |
+
|
| 70 |
+
- The default corpus is mixed English + Python code. Use `--corpus` or `--text` to test on your own data.
|
| 71 |
+
- Chars/token is corpus-dependent. A tokenizer that's great for code may be worse for Turkish, and vice versa.
|
| 72 |
+
- This tool reads `tokenizer.json` (the fast-rs format). For SentencePiece `.model` files, it falls back to that format.
|