How an Inference Engine Decodes Policies

How an Inference Engine Decodes Policies
3 min read

The choice of a decoding strategy doesn’t just affect the quality of a language model’s responses: it also impacts the workload demanded of the inference engine. According to a tutorial published on Machine Learning Mastery, which uses the vLLM library, certain settings add computation after each model call, create multiple active sequences for a single request, or keep the blocks of the KV cache—the memory storing intermediate generation states—alive longer. These effects persist even when the model weights and the prompt remain identical.

In vLLM, model execution and token selection are separate. The LLM object loads the model from the Hugging Face Hub, while SamplingParams describes how to process the logits—the scores assigned to each vocabulary entry—and then choose the tokens, the text units manipulated by the model. The tutorial’s examples use HuggingFaceTB/SmolLM2-1.7B-Instruct and a single prompt.

Sampling Filters and Per-Token Computation

Decoding settings do not avoid the model’s full computation. Even with top-k or top-p, which restrict candidates, the model produces a logit for each vocabulary element. These filters then act on these scores. The tutorial distinguishes greedy selection, which retains the highest score, from sampling, which requires recalibrating scores, constructing a probability distribution, and drawing a token. Repetition penalties, on the other hand, take into account tokens that have already appeared.

These operations execute at each decoding step. They are generally less costly than the model’s forward pass, but their weight increases with vocabulary size, batch size, or the number of combined processes. However, the tutorial notes that response length can weigh more heavily: a strategy that generates more tokens calls the model more often and lengthens the KV cache.

Multiple Outputs and Beam Search

The parameter n=4 requests four sampled continuations for the same prompt. These sequences are independent: they do not compete and are not ranked against each other. If all generate G tokens, their private cache represents roughly four times that of a single continuation. This approach can nonetheless remain more efficient than four separate requests, as vLLM can share part of the prompt computation.

Beam search works differently. Its hypotheses are compared using cumulative scores: at each step, they are extended, ranked together, and only the best are kept. In vLLM, it uses a separate interface, BeamSearchParams. With a beam width of four, the engine must manage up to four hypotheses, their scores, and their cache references. Once beams diverge, their KV state becomes private, reducing the capacity available for other requests.

Stops, Structured Outputs, and Batch Cost

The cost of a request depends heavily on the end of generation. The parameter max_tokens sets an upper bound, and the request can end earlier after an end-of-sequence (EOS) token or a stop string. Conversely, min_tokens prevents stopping before the chosen threshold. When requests in the same batch finish at different times, vLLM removes completed sequences from subsequent iterations, but cache blocks remain occupied as long as a request is active.

Structured outputs also impose an extra cost. vLLM, via StructuredOutputsParams, constrains generation to respect a choice, a regular expression, a grammar, or a JSON schema. At each token, the engine updates the constraint state and masks invalid logits. The schema guarantees the structure and allowed values of the response, but, the tutorial specifies, it does not guarantee that the classification is correct.

Finally, vLLM uses continuous batching to combine active requests at each iteration. Requests can have different prompts, lengths, and temperatures. Nevertheless, the number of active sequences, their length, and their individual processing determine the work the engine must perform.

S’abonner
Notification pour
guest

0 Commentaires
Le plus ancien
Le plus récent Le plus populaire
0
Nous aimerions avoir votre avis, veuillez laisser un commentaire.x