liveBook · Manning

Tokenizer

As explained in Server-Side WebAssembly

A tokenizer is a text-processing component that converts text into discrete tokens and can reconstruct text from token sequences.

A tokenizer is the bridge between readable text and an ML model’s numeric input. It splits a prompt into tokens, exposes their numeric IDs for inference, and decodes generated IDs back into text. In story generation, it preprocesses the prompt and postprocesses model output so the result can be returned as a coherent string. section 5.4.1 section 5.4.2

Load a tokenizer from a tokenizer.json fixture before running inference. Encode the prompt with special-token handling enabled, copy its input IDs into a mutable sequence, and pass that sequence to the model as input. Each sampled next-token ID extends the sequence. section 5.4.2 section 5.4.2

let tokenizer = Tokenizer::from_file("fixture/tokenizer.json").unwrap();

This initialization loads the tokenizer artifact used by the component before it encodes the story prompt. section 5.4.2

During generation, tokenization supports the control loop:

  • Look up the <EOS> token ID to recognize a model-defined stopping marker; the fallback ID of 0 avoids an immediate lookup failure but may not be a valid stopping point. section 5.4.2
  • Decode the growing ID sequence after sampling each token, making the generated text available for output and for a double-newline stopping check. section 5.4.2
  • Return the final decoded sequence after trimming surrounding whitespace. section 5.4.2

Generating from “Once upon a time”

Encode “Once upon a time” into input IDs, run inference, sample a next token from the output probabilities, append its ID, and decode the sequence. Repeat for up to 100 iterations, stopping at <EOS> or when the decoded text ends with two newlines. section 5.4.2

How Server-Side WebAssembly covers it

How deep it goes
Works through loading, encoding IDs, EOS lookup, iterative decoding, and stopping checks without detailing tokenizer training or vocabulary construction.
Examples
Uses a tokenizer.json fixture and the prompt “Once upon a time” to generate a story token by token.
What it assumes
Assumes a tokenizer.json artifact, an ONNX model, and a Rust component configured with tokenizers and wasi:nn.
What you can do after
Apply it via the tokenizers library to encode prompts and decode sampled IDs in a Wasm story generator.
How it connects
Connects tokenization with ONNX inference, wasi:nn, input tensors, softmax, and random sampling.

Also explained in

Compare how 16 books explain tokenizer →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Server-Side WebAssembly. The section links point to the parts of the book it draws on.