Skip to main content

Decoding strategies

Decoding converts next-token distributions into token sequences. Strategy choice changes creativity, factuality and latency.

Adapted for this playbook from the 🤗 Transformers documentation by Hugging Face. Official page: https://huggingface.co/docs/transformers/generation_strategies. Images © Hugging Face (documentation-images) unless noted. This is not a substitute for the upstream docs — verify against the current version.

Covers Hugging Face pages: generation_strategies, tasks/prompting, assisted_decoding

Common strategies​

StrategyFlags (typical)Use when
Greedydo_sample=FalseDeterministic, short answers
Beam searchnum_beams>1Higher likelihood sequences (translation/summarisation)
Temperature samplingdo_sample=True, temperatureCreative text
Top-k / top-ptop_k, top_pTruncate tail of distribution
Assisted decodingassistant modelSpeculative speed-ups
outputs = model.generate(
**inputs,
max_new_tokens=100,
do_sample=True,
temperature=0.7,
top_p=0.9,
)

Prompting​

Decoding cannot fix a bad prompt. See Hugging Face’s Prompt engineering task guide and this playbook’s Prompt & context engineering.

Assisted decoding​

Speculative / assisted decoding uses a smaller draft model to propose tokens verified by the target model — see Assisted decoding.

Discussion

Comments​

Share feedback or questions about this page. No account required.

Loading comments…