🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)
🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)

🔧 Programmierung 🕛 kürzlich 3 Min Lesezeit
0

Building an LLM Inference Engine from Scratch: Tokenization Pipeline Notes

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

The development of an LLM inference engine starts from understanding and implementing the tokenizer. Before the model can perform any computation, raw text input must be converted into numerical token IDs that correspond to entries in the model vocabulary.






Loading the Tokenizer Configuration



The first step was to load the tokenizer files downloaded from Hugging Face. The tokenizer configuration contains several important components:




  • Vocabulary (vocab.json): Maps token strings to integer token IDs.

  • Merge rules (merges.txt or equivalent JSON structure): Defines the Byte Pair Encoding (BPE) merge priority.

  • Regex pattern (tokenizer.json): Defines how raw text is initially split into smaller components.

  • Special tokens: Defines reserved tokens such as beginning-of-sequence, end-of-sequence, padding, etc.



Using the nlohmann::json library in C++, the vocabulary and merge dictionaries were parsed into native C++ data structures for efficient lookup.



Example structures:




CODE
std::unordered_map<std::string, int> vocabulary;

std::unordered_map<
std::pair<std::string, std::string>,
int,
PairHash
> merge_rank;






The tokenizer class was designed to manage the complete encoding pipeline:




CODE
Raw Text
|
v
Normalization
|
v
Regex Pre-tokenization
|
v
Byte / Unicode Conversion
|
v
BPE Merge Algorithm
|
v
Vocabulary Lookup
|
v
Token IDs









Text Normalization and Pre-tokenization



Modern LLM tokenizers do not directly map words to vocabulary entries. Instead, they apply a multi-stage transformation.



The regex pattern stored in tokenizer.json is used to perform pre-tokenization.



For example, the pattern separates:




  • Words

  • Numbers

  • Punctuation

  • Whitespace

  • Contractions



A simplified example:

Input:




CODE
Hello world!






Regex output:




CODE
["Hello", " world", "!"]






Each segment is then processed independently by the BPE algorithm.






Byte-Level Encoding



Modern LLM tokenizers, such as GPT-2, Qwen, and many Hugging Face models, operate on bytes rather than directly on Unicode characters.



The original text:




CODE
你好






is first converted into UTF-8 bytes:




CODE
E4 BD A0
E5 A5 BD






Each byte is mapped into a special Unicode representation through a byte-to-unicode mapping.



The purpose of this mapping is to allow every possible byte value (0-255) to be represented as a valid Unicode token candidate.



Example:




CODE
Byte:
0xF0

Mapped Unicode:
ð






This creates an intermediate representation used by BPE.






Byte Pair Encoding (BPE) Merge Algorithm



The key discovery during implementation was that tokenization is not simply a vocabulary lookup.

The tokenizer does not immediately search:




CODE
"hello"






inside the vocabulary.

Instead, it performs iterative merging based on the merge rules.

Each pre-tokenized segment is first broken into individual byte/unicode units:

Example:




CODE
hello






becomes:




CODE
h e l l o






The tokenizer then checks adjacent pairs:




CODE
(h,e)
(e,l)
(l,l)
(l,o)






Each pair is searched in the merge dictionary.

The merge dictionary contains the priority ranking:




CODE
("h","e") -> 10
("he","l") -> 5
("hel","l") -> 3






A lower rank means a higher merge priority.



The algorithm repeatedly:




  1. Finds all possible adjacent pairs.

  2. Checks whether each pair exists in the merge dictionary.

  3. Selects the pair with the lowest merge rank.

  4. Combines the pair into a single token.

  5. Repeats until no valid merges remain.






Vocabulary Lookup



After BPE merging is complete, the resulting token strings are searched in the vocabulary dictionary.



Example:

After merging:




CODE
["hello", "Ġworld"]






Vocabulary lookup:




CODE
hello     -> 15339
Ġworld -> 1917






The final tokenizer output becomes:




CODE
[
15339,
1917
]






These integer IDs are then used as input embeddings for the transformer model.



Repo: https://github.com/NgKaiWen7/InferenceEngine/tree/tokenization

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
3 Quellen
GPT-6 Astra Release Today? OpenAI’s Next Major AI Model Is Almost Here
1 Quelle
Apple accuses OpenAI of destroying evidence as trade-secrets fight intensifies
1 Quelle
Major AI platforms go down in unprecedented simultaneous outage