huggingface-tokenizers
Use when "tokenizers", "HuggingFace tokenizer", "BPE", "WordPiece", or asking about "train tokenizer", "custom vocabulary", "tokenization", "subword", "fast tokenizer", "encode text"
What this skill does
<!-- Adapted from: claude-scientific-skills/scientific-skills/huggingface-tokenizers -->
# HuggingFace Tokenizers
Fast, production-ready tokenization - Rust-powered, Python API.
## When to Use
- High-performance tokenization (<20s per GB)
- Train custom tokenizers from scratch
- Track token-to-text alignment
- Production NLP pipelines
- Need BPE, WordPiece, or Unigram tokenization
## Quick Start
```python
from tokenizers import Tokenizer
# Load pretrained
tokenizer = Tokenizer.from_pretrained("bert-base-uncased")
# Encode
output = tokenizer.encode("Hello, how are you?")
print(output.tokens) # ['hello', ',', 'how', 'are', 'you', '?']
print(output.ids) # [7592, 1010, 2129, 2024, 2017, 1029]
# Decode
text = tokenizer.decode(output.ids)
```
## Train Custom Tokenizer
### BPE (GPT-2 style)
```python
from tokenizers import Tokenizer
from tokenizers.models import BPE
from tokenizers.trainers import BpeTrainer
from tokenizers.pre_tokenizers import ByteLevel
# Initialize
tokenizer = Tokenizer(BPE(unk_token="<|endoftext|>"))
tokenizer.pre_tokenizer = ByteLevel()
# Configure trainer
trainer = BpeTrainer(
vocab_size=50000,
special_tokens=["<|endoftext|>", "<|pad|>"],
min_frequency=2
)
# Train
tokenizer.train(files=["data.txt"], trainer=trainer)
# Save
tokenizer.save("my-tokenizer.json")
```
### WordPiece (BERT style)
```python
from tokenizers import Tokenizer
from tokenizers.models import WordPiece
from tokenizers.trainers import WordPieceTrainer
from tokenizers.pre_tokenizers import Whitespace
tokenizer = Tokenizer(WordPiece(unk_token="[UNK]"))
tokenizer.pre_tokenizer = Whitespace()
trainer = WordPieceTrainer(
vocab_size=30000,
special_tokens=["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]"]
)
tokenizer.train(files=["data.txt"], trainer=trainer)
```
## Encoding Options
```python
# Single text
output = tokenizer.encode("Hello world")
# Batch encoding
outputs = tokenizer.encode_batch(["Hello", "World"])
# With padding
tokenizer.enable_padding(pad_id=0, pad_token="[PAD]")
outputs = tokenizer.encode_batch(texts)
# With truncation
tokenizer.enable_truncation(max_length=512)
output = tokenizer.encode(long_text)
```
## Access Encoding Data
```python
output = tokenizer.encode("Hello world")
output.ids # Token IDs
output.tokens # Token strings
output.attention_mask # Attention mask
output.offsets # Character offsets (alignment)
output.word_ids # Word indices
```
## Pre-tokenizers
```python
from tokenizers.pre_tokenizers import (
Whitespace, # Split on whitespace
ByteLevel, # Byte-level (GPT-2)
BertPreTokenizer, # BERT style
Punctuation, # Split on punctuation
Sequence, # Chain multiple
)
# Chain pre-tokenizers
from tokenizers.pre_tokenizers import Sequence, Whitespace, Punctuation
tokenizer.pre_tokenizer = Sequence([Whitespace(), Punctuation()])
```
## Post-processing
```python
from tokenizers.processors import TemplateProcessing
# BERT-style: [CLS] ... [SEP]
tokenizer.post_processor = TemplateProcessing(
single="[CLS] $A [SEP]",
pair="[CLS] $A [SEP] $B:1 [SEP]:1",
special_tokens=[
("[CLS]", tokenizer.token_to_id("[CLS]")),
("[SEP]", tokenizer.token_to_id("[SEP]")),
],
)
```
## Normalization
```python
from tokenizers.normalizers import (
NFD, NFKC, Lowercase, StripAccents, Sequence
)
# BERT normalization
tokenizer.normalizer = Sequence([NFD(), Lowercase(), StripAccents()])
```
## With Transformers
```python
from transformers import PreTrainedTokenizerFast
# Wrap for transformers compatibility
fast_tokenizer = PreTrainedTokenizerFast(tokenizer_object=tokenizer)
# Now works with transformers
encoded = fast_tokenizer("Hello world", return_tensors="pt")
```
## Save and Load
```python
# Save
tokenizer.save("tokenizer.json")
# Load
tokenizer = Tokenizer.from_file("tokenizer.json")
# From HuggingFace Hub
tokenizer = Tokenizer.from_pretrained("bert-base-uncased")
```
## Performance Tips
1. **Use batch encoding** for multiple texts
2. **Enable padding/truncation** once, not per-encode
3. **Pre-tokenizer choice** affects speed significantly
4. **Train on representative data** for better vocabulary
## vs Alternatives
| Tool | Best For |
|------|----------|
| **tokenizers** | Speed, custom training, production |
| SentencePiece | T5/ALBERT, language-independent |
| tiktoken | OpenAI models (GPT) |
## Resources
- Docs: <https://huggingface.co/docs/tokenizers/>
- GitHub: <https://github.com/huggingface/tokenizers>
Related in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.