Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Hugging Face 14 min read Deep dive mediumAdept

Tokenization in Transformers v5: Simpler, Clearer, and More Modular

Transformers v5 redesigns tokenizers so that tokenizer architecture (normalizer, pre-tokenizer, model, post-processor, decoder) is separated from the trained vocabulary, much as PyTorch separates model architecture from weights. The walkthrough covers the tokenization pipeline stages, the BPE, Unigram and WordPiece algorithms, and how the transformers wrapper adds chat templates, special tokens, truncation and padding on top of the Rust tokenizers library. It also lays out the class hierarchy: PreTrainedTokenizerBase as the common interface, with TokenizersBackend, PythonBackend and SentencePieceBackend beneath it. The v5 changes consolidate on a single fast backend and make tokenizers easier to inspect, customize and train from scratch.

Assumes: Pythonbasic LLM conceptsHugging Face Transformers usage

Related posts

  • tokenizers v1: encode, decode and scaling, measured

    Hugging Face mediumAdept

    tokenizers v1 encodes text 3 to 30 times faster than v0.23 single-threaded on an Apple M4 Max and scales at 76% of linear across eight cores, while producing identical token IDs and keeping the same API. The gains come from replacing regex pre-tokenization with SIMD bitstream operations (bitcannon) for common BPE patterns, a thread-local word cache for repeated pre-tokens, an allocation-free merge loop using an intrusive linked list in a scratch buffer with ranks packed into 64-bit integers, batched model calls, and per-thread sub-pools for parallel encoding. The benchmark methodology separates load time, verifies output by hashing IDs, pins to physical cores, and warns that 'warm' benchmarks on repeated versus distinct documents measure very different workloads.

  • Train and Fine-Tune Sentence Transformers Models

    Hugging Face mediumAdept

    Sentence Transformers models map text to fixed-size embeddings by running a pre-trained Transformer and pooling its token embeddings, and training them depends on matching the dataset format to the right loss. Four common formats map to specific losses: labeled pairs (ContrastiveLoss, SoftmaxLoss, CosineSimilarityLoss), unlabeled positive pairs (MultipleNegativesRankingLoss), class-labeled sentences (batch triplet losses), and unlabeled triplets (TripletLoss). A worked example builds a distilroberta-base model, trains it on QQP triplets with TripletLoss via the legacy `fit` API, and pushes it to the Hugging Face Hub. The guide is marked outdated: since v3.0, SentenceTransformerTrainer replaces this API.

  • Announcing pg_tiktoken: A Postgres Extension for Fast BPE Tokenization

    Neon mediumAdept

    pg_tiktoken is a Postgres extension on Neon that wraps OpenAI's tiktoken BPE tokenizer, exposing tiktoken_encode (returns token IDs) and tiktoken_count (returns token counts) for encodings such as cl100k_base, p50k_base, p50k_edit and r50k_base or their model names. A practical pattern stores chat message history with a precomputed n_tokens column, then uses a window-function cumulative sum ordered by recency to select the most recent messages that fit within a model's context budget (MAX_HISTORY_TOKENS = model max minus system and completion tokens) before calling gpt-3.5-turbo.

  • Native-speed vLLM transformers modeling backend

    Hugging Face hardMaster

    The transformers modeling backend in vLLM now matches or beats vLLM's hand-written native implementations in throughput. Benchmarks covered Qwen3-4B on one GPU, Qwen3-32B with tensor parallelism, and Qwen3-235B FP8 MoE with data and expert parallelism on 8×H100. The backend uses torch.fx static analysis to find known patterns, then rewrites the model source via AST to swap in fused vLLM kernels such as QKVParallelLinear, MergedColumnParallelLinear and expert-parallel MoE ops. It also infers TP/PP plans while staying compatible with torch.compile and CUDA Graphs. Any compliant transformers model gets this with `--model-impl transformers`, except linear-attention models and custom Hub-code models.

  • How Specializing the Vocabulary of SLMs Can Cut Inference Cost by 77% in Real Applications

    Hugging Face hardMaster

    VocabX rebuilds a fine-tuned small model's tokenizer by choosing tokens for the cost they save rather than how often they appear. Saved tokens are weighted by field, because a generated token cost 7.4x an input token in their setup. The method works at two layers under one criterion: sub-word merges plus multi-word "macro tokens" found by five span miners. It estimates gains for candidates that never appear in the sample rather than scoring them zero. On a Qwen3-4B Portuguese essay grader, responses shrank from 350 to 101 tokens and measured inference cost fell 77%, while bench score rose from 0.703 to 0.810. Fusing grades into JSON macro tokens hurt quality, so the decision tokens the model exists to make were kept separate.

The quest for software engineering knowledge.

Look:
Layout: