Implementing LLM Decoding Sampling Strategies
A tutorial presents a configurable PyTorch logits-processing pipeline for temperature, top-k, top-p, min-p and repetition penalty. It explains how each control changes the probability distribution and its failure modes, and argues that temperature scaling should precede top-p truncation because scaling changes the distribution shape. It also recommends versioning and recording sampling settings so that model evaluations are not distorted by differences in decoding parameters.
The tutorial provides developers with a concrete PyTorch implementation and a unified way to understand the distinct roles, ordering and limitations of common decoding controls rather than treating them as interchangeable settings.
采样策略是推理系统里参数最少、影响最直接的组件:同一模型,换一套采样参数,输出从严谨到癫狂只在一行代码之间。这篇文章把主流策略全部实现并指出各自的失效模式——它们不是并列的选项,而是一组有明确语义分工的旋钮。
一、共享骨架:logits 处理管线
所有采样策略都是对 logits 的一连串变换。工程实现把它们组成一条可配置的管线,顺序有讲究: