Skip to main content

SamplingParams

The SamplingParams class controls how text is generated from language models. It configures sampling strategies, stopping conditions, penalties, and output options.

Constructor

Parameters

Token Generation

int
default:"32"
Maximum number of tokens to generate per output sequence.
int
default:"None"
Minimum number of tokens to generate. Values < 1 have no effect. Prevents early stopping.

Sampling Strategy

float
default:"None"
Temperature for sampling (≥ 0). Controls randomness:
  • 0.0: Greedy decoding (deterministic)
  • < 1.0: More focused, deterministic outputs
  • = 1.0: Standard sampling
  • > 1.0: More random, creative outputs
If None and neither top_p nor top_k are specified, defaults to greedy decoding.
float
default:"None"
Nucleus sampling threshold (0 to 1). Only tokens with cumulative probability ≥ top_p are considered.
  • 1.0: Consider all tokens (standard sampling)
  • < 1.0: Consider only most probable tokens
If None and neither temperature nor top_k are specified, defaults to greedy decoding.
int
default:"None"
Sample from the top K most likely tokens.
  • 0: Consider all tokens
  • 1: Greedy decoding
  • > 1: Limit sampling to top K tokens
If None and neither temperature nor top_p are specified, defaults to greedy decoding.
float
default:"None"
Lower bound for top-P decay algorithm. Defaults to 1e-6.
float
default:"None"
Decay factor for top-P algorithm. Defaults to 1.0.
int
default:"None"
Token ID where top-P decay resets. Defaults to 1.
float
default:"None"
Minimum token probability threshold. Scales the most likely token to determine minimum probability. Defaults to 0.0.
int
default:"None"
Random seed for reproducible sampling. Defaults to 0.
Enable beam search instead of sampling. When True, best_of becomes the beam width.
int
default:"1"
Number of output sequences to return per prompt.
int
default:"None"
Number of sequences to generate for selection:
  • Sampling mode: Generate best_of sequences, return top n by cumulative log probability
  • Beam search mode: Use beam width of best_of, return top n
Must satisfy best_of >= n. Defaults to n.
float
default:"None"
Diversity penalty for beam search. Values > 1.0 encourage diverse beams. Defaults to 1.0.
List[int]
default:"None"
Array of beam widths for variable-beam-width search.
float
default:"None"
Exponential penalty for sequence length in beam search. Defaults to 0.0.
int
default:"None"
Stop beam search when beam_width complete sequences are generated. Defaults to 1.

Stopping Conditions

int
default:"None"
End-of-sequence token ID. Generation stops when this token is generated. Defaults to tokenizer’s EOS token.
int
default:"None"
Padding token ID. Defaults to end_id.
str | List[str]
default:"None"
Stop string(s). Generation stops when any of these strings are generated.
List[int]
default:"None"
Stop token IDs. Generation stops when any of these tokens are generated.
bool
default:"False"
Include stop string in the output text. When False, stop strings are removed.
bool
default:"False"
Continue generation after EOS token is generated.

Bad Words / Tokens

str | List[str]
default:"None"
Bad string(s) to avoid. When these would be generated, they are redirected to alternative tokens.
List[int]
default:"None"
Token IDs to avoid during generation.

Repetition Control

float
default:"None"
Penalty for repeating tokens:
  • < 1.0: Encourage repetition
  • = 1.0: No penalty (default)
  • > 1.0: Discourage repetition
float
default:"None"
Penalty for tokens that have already appeared (independent of frequency):
  • < 0.0: Encourage repetition
  • = 0.0: No penalty (default)
  • > 0.0: Discourage repetition
float
default:"None"
Penalty based on token frequency in generated text:
  • < 0.0: Encourage repetition
  • = 0.0: No penalty (default)
  • > 0.0: Discourage repetition (stronger for more frequent tokens)
int
default:"None"
Number of prompt tokens to ignore for presence/frequency penalties. Defaults to 0.
int
default:"None"
Prevent repetition of n-grams of this size. Defaults to very large value (no limit).

Output Control

int
default:"None"
Number of log probabilities to return per output token:
  • None: No log probabilities
  • 0: Only the sampled token’s log probability
  • K > 0: Top-K log probabilities plus sampled token (if not in top-K)
int
default:"None"
Number of log probabilities to return per prompt token. Same format as logprobs.
LogprobMode
default:"LogprobMode.RAW"
Log probability calculation mode:
  • LogprobMode.RAW: Raw log probabilities from model output
  • LogprobMode.PROCESSED: After applying sampling parameters (temperature, top-k, top-p)
bool
default:"False"
Return full logits tensor for prompt tokens.
bool
default:"False"
Return full logits tensor for generated tokens.
bool
default:"True"
Exclude input tokens from output token IDs.
bool
default:"False"
Return encoder hidden states for encoder-decoder models.
bool
default:"False"
Include performance metrics in output (TTFT, latency, throughput, etc.).
List[str]
default:"None"
Additional model outputs to gather (model-specific).

Tokenization

bool
default:"True"
Convert output token IDs to text.
bool
default:"True"
Add special tokens (BOS, EOS) when encoding the prompt.
bool
default:"True"
Skip special tokens when decoding output text.
bool
default:"True"
Add spaces between special tokens in decoded output.
int
default:"None"
Truncate prompt to last K tokens (left truncation). Must be ≥ 1.

Advanced

torch.Tensor
default:"None"
Embedding bias tensor of shape [vocab_size] with dtype float32.
LogitsProcessor | List[LogitsProcessor]
default:"None"
Custom logits processor callback(s) to modify logits before sampling. Can be a single processor or list.
bool
default:"False"
Apply batched logits processor. Processor must be provided when initializing LLM.
LookaheadDecodingConfig
default:"None"
Configuration for lookahead decoding optimization.
GuidedDecodingParams
default:"None"
Guided decoding parameters for structured output (JSON, regex, grammar).

Usage Examples

Basic Sampling

Greedy Decoding

Beam Search

Stop Strings

Multiple Outputs

Log Probabilities

Repetition Control

Structured Output with Guided Decoding

Performance Monitoring

GuidedDecodingParams

Parameters for structured output generation:

LogprobMode

Enum for log probability modes: