Unit 1 of 12 · about 3 hours · explain, measure

Prompt tokenization and preprocessing as request work

Tokenization runs on the host before the first device kernel, and detokenization runs between streamed tokens. A slow tokenizer looks exactly like slow prefill or ragged decode in a naive trace.

You'll be able to

  • Explain where prompt tokenization and detokenization sit in a serving request and why their cost lands in TTFT and streaming gaps.
  • Measure tokenizer and detokenizer CPU time with a sampling flame graph and name the hottest functions.
  • State the prompt lengths and conditions under which tokenization can be ignored in a baseline.

Covers

Prompt tokenization and request preprocessingTokenizer and detokenizer CPU flame graphs
1

Read

In this order, only the parts listed

0 of 3 done

  1. A CPU-bound tokenizer as the bottleneckAI Systems Performance Engineering - Chapter 16, profiling and debugging inference

    Profiling, Debugging, and Tuning Inference Performance (pp. 959 to 964): the host-CPU-at-100-percent tokenization example and moving it to a GPU tokenizer or custom kernel

    Read only the host-CPU tokenization example. Leave cache-hit tuning and tiered-cache sizing for the KV sections.

  2. Where tokenization enters TTFTvLLM Metrics Design and Production Metrics

    Frontend Stats Collection: arrival_time starts when tokenization begins, and TTFT is measured relative to it

  3. CPU flame graphs with py-spypy-spy - sampling profiler for Python

    README Usage: py-spy record -o profile.svg -- python myprogram.py for a flame graph, and py-spy top for the live view

2

Focus on

  • Tokenization and detokenization are host work inside the request path. Tokenization adds to TTFT and scales with prompt length; detokenization adds to gaps between streamed tokens and scales with output length.
  • vLLM starts the TTFT clock when tokenization begins, so tokenizer time is inside TTFT by definition, not beside it.
  • A flame graph attributes host time to functions. Look for tokenizer encode and decode calls with regex or Python-loop frames, not device kernels.
  • Ignoring tokenization is a stated assumption, not a default. It fails for long prompts, slow tokenizers and per-token detokenization in streaming.
Common mistakes · 3
  • Reading TTFT as prefill time. TTFT includes queueing, tokenization, scheduling and host overhead around the prefill kernels.
  • Profiling the tokenizer once with a cold import. Import and cache warmup dominate the first call, so profile steady-state calls on your prompt distribution.
  • Detokenizing each token independently in analysis code. Streamed text uses incremental detokenization state, so naive per-token decoding can disagree with what the user saw.
3

Try it yourself

Closed-book, before checking anything

Closed-book, draw the request path from arrival through tokenization, queue, prefill, first-token sampling and detokenization, and mark which spans vLLM counts in TTFT. Then profile a local tokenize and detokenize loop with py-spy record and name the two hottest frames.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.Where does vLLM start the TTFT clock?Answer

    When tokenization begins on the frontend. Tokenizer time is inside TTFT by construction.

  2. 2.A flame graph shows most request-wall time in a Python regex function under prefill-heavy load. Is the prefill kernel slow?Answer

    There is no evidence for that. The host tokenizer or preprocessing is the bottleneck, and the device may be idle waiting for work.

  3. 3.When is it safe to ignore tokenization in a baseline?Answer

    Only when you measured it small against TTFT on your prompts, with the tokenizer, cache state and lengths recorded. Long prompts or per-token detokenization break the assumption.

  4. 4.Why launch the process under py-spy record instead of attaching to a running server with a process id?Answer

    Attaching to a process you did not start usually needs elevated permissions. Launching under py-spy record avoids that restriction.

You're done when

You can point to the tokenizer span inside a TTFT trace and to the hottest tokenizer frame in your own flame graph, and state the prompt lengths where ignoring it stops being safe.

Only when you can do this without notes.

Leads to

Notes

Resources · 3 for this unit, 9 more in the section
  • AI Systems Performance Engineering - Chapter 16, profiling and debugging inferenceChris Fregly (O'Reilly) · required

    Read the first part of Chapter 16: monitoring system metrics, profiling with Nsight Systems and Nsight Compute, the troubleshooting recipes and debugging correctness. Treat every quoted speedup as a hypothesis for your own hardware.

  • vLLM Metrics Design and Production MetricsvLLM Project · required

    Map scheduler and cache events to exported request, queue, TTFT, inter-token and generation metrics. The link is pinned to v0.31.0; check the definitions against the version you install.

  • py-spy - sampling profiler for PythonBen Frederick · support

    Use py-spy record to take CPU flame graphs of tokenizer and detokenizer work, and py-spy top to watch the hot functions live. Launch the profiled process under py-spy.

Also in this section

  • Efficiently Scaling Transformer InferenceMLSys · primary

    Read the prefill and decode cost, memory, batching and parallelism sections. Use its models to predict phase bottlenecks before measurement and record which assumptions fail on the selected model, engine and GPU.

  • AI Systems Performance Engineering companion code - Chapter 16Chris Fregly · required

    Begin with inference_profiling.py, inference_server_load_test.py, monitoring_bundle.py and runtime scheduler instrumentation. Run only hardware-compatible targets, retain raw artifacts and re-measure all bundled expectations on your own device.

  • Hands-On LLM Serving and Optimization - Chapter 5 and the Chapter 4 metrics sectionChi Wang and Peiheng Hu (O'Reilly) · required

    Read Chapter 5 on GPU specifications, model loading and arithmetic intensity in the prefill and decode phases, and "Measuring Performance in LLM Serving" in Chapter 4. Turn each claim into a prediction for one short-prompt and one long-prompt workload.

  • vLLM Benchmark CLI (v0.31.0)vLLM Project · required

    Use vllm bench latency, vllm bench throughput and vllm bench serve for phase and endpoint baselines. Fix the dataset, lengths, request rate, warmup and sampling, and keep the raw per-request results.

  • TensorRT-LLM Benchmarking GuideNVIDIA · support

    Use Before Benchmarking and Online Serving Benchmarking as a reproducibility checklist. Borrow environment capture and startup metrics even if TensorRT-LLM is not the selected engine.

  • Time to First Token - Measurement and Load-Testing SessionsPatchy631 · support

    Use only Week 3 and Week 5 as a practical checklist for Prometheus, Grafana and request-rate sweeps. Follow the competency gate rather than its calendar, and replace fixed concurrency goals with your hardware-safe saturation range.

  • LLM Inference Systems Laboratory - InstrumentationRoadmap-Final · support

    Complete Milestone B: correlate request, iteration, KV, scheduler, kernel and resource events, quantify observer overhead, and retain raw arrival and completion records.

  • Taming Throughput-Latency Tradeoff in LLM Inference with Sarathi-ServeAgrawal et al. · support

    Read arXiv v3, Chunked-prefills and Stall-free batching for the chunked-prefill vocabulary and the prefill-decode interference argument, and Overhead of chunked-prefills for the cost of splitting. Treat the reported capacity gains as the paper's result on its hardware.

  • vLLM SamplingParams API (v0.31.0)vLLM Project · reference

    Look up temperature, seed, max_tokens, ignore_eos, stop and stop_token_ids defaults when you fix the output contract. The link is pinned to v0.31.0; check the fields against the version you install.