huggingface/transformers · error · ValueError

`min_p` has to be a float in the [0, 1] interval, but is {mi

Error message

`min_p` has to be a float in the [0, 1] interval, but is {min_p}

What it means

Thrown by MinPLogitsProcessor.__init__ when min_p is outside [0, 1]. Min-p sampling keeps only tokens whose probability is at least min_p times the top token's probability, so min_p is a ratio and both endpoints 0 and 1 are legal (0 disables, 1 keeps only the argmax).

Source

Thrown at src/transformers/generation/logits_process.py:753

    >>> # With sampling, the output is unexpected -- sometimes too unexpected.
    >>> outputs = model.generate(**inputs, do_sample=True)
    >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
    A sequence: 1, 2, 3 | < 4 (left-hand pointer) ;
    <BLANKLINE>
    <BLANKLINE>

    >>> # With `min_p` sampling, the output gets restricted to high-probability tokens.
    >>> # Pro tip: In practice, LLMs use `min_p` in the 0.01-0.2 range.
    >>> outputs = model.generate(**inputs, do_sample=True, min_p=0.1)
    >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
    A sequence: 1, 2, 3, 4, 5, 6, 7, 8, 9
    ```
    """

    def __init__(self, min_p: float, filter_value: float = -float("Inf"), min_tokens_to_keep: int = 1):
        if not (0 <= min_p <= 1.0):
            raise ValueError(f"`min_p` has to be a float in the [0, 1] interval, but is {min_p}")
        if not isinstance(min_tokens_to_keep, int) or (min_tokens_to_keep < 1):
            raise ValueError(f"`min_tokens_to_keep` has to be a positive integer, but is {min_tokens_to_keep}")

        self.min_p = min_p
        self.filter_value = filter_value
        self.min_tokens_to_keep = min_tokens_to_keep

    def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
        # Convert logits to probabilities
        probs = torch.softmax(scores, dim=-1)
        # Get the probability of the top token for each sequence in the batch
        top_probs = probs.amax(dim=-1, keepdim=True)
        # Calculate the actual min_p threshold by scaling min_p with the top token's probability
        scaled_min_p = self.min_p * top_probs
        # Create a mask for tokens that have a probability less than the scaled min_p
        tokens_to_remove = probs < scaled_min_p

        # Keep at least min_tokens_to_keep tokens (clip k to vocab size if needed, avoids index out of range)

View on GitHub (pinned to a597f97485)

Solutions

  1. Use a ratio in [0, 1]: min_p=0.1 (docs suggest 0.01–0.2 in practice)
  2. Convert percentages: min_p = pct / 100.0, clamped to [0, 1]
  3. Validate 0 <= min_p <= 1 in config-loading code before generate()

Example fix

# before
out = model.generate(**inputs, do_sample=True, min_p=10)  # percent mistake -> ValueError

# after
out = model.generate(**inputs, do_sample=True, min_p=0.1)
Defensive patterns

Strategy: validation

Validate before calling

def valid_min_p(p):
    return isinstance(p, (int, float)) and 0.0 <= float(p) <= 1.0

Type guard

def is_valid_min_p(p) -> bool:
    return isinstance(p, (int, float)) and 0.0 <= p <= 1.0

Try / catch

try:
    proc = MinPLogitsProcessor(float(p))
except ValueError as e:
    raise ValueError(f'min_p={p!r} must be in [0, 1] (typical 0.01-0.2)') from e

Prevention

When it happens

Trigger: MinPLogitsProcessor(1.2); min_p=-0.05; model.generate(do_sample=True, min_p=15) from a mistyped config (practical range is 0.01–0.2).

Common situations: Min-p is a newer parameter; users port values from papers or other engines using different scales; percentage confusion (10 instead of 0.1); generation-config typos.

Related errors


AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14). Data as JSON: /api/errors/7eb7f42c42d3cba6. Report an issue: GitHub.