> ## Documentation Index
> Fetch the complete documentation index at: https://docs.baseten.co/llms.txt
> Use this file to discover all available pages before exploring further.

# Min-p sampling

> Sample at higher temperatures by dropping tokens much less likely than the top token, and pass the same cut-off to RL losses.

Min-p sampling drops every token whose probability is less than `min_p` times the probability of the most likely token.
To sample with it, set `min_p` in [`SamplingParams`](https://docs.baseten.co/reference/sdk/loops/types).
When the samples feed an RL loss, also pass the cut-off to the trainer, as described in [Match the cut-off in RL losses](#match-the-cut-off-in-rl-losses).

## Sample with min-p

To sample with a min-p cut-off, set `min_p` in `SamplingParams`:

```python theme={"system"}
from baseten.loops import SamplingParams

params = SamplingParams(temperature=1.0, min_p=0.1, max_tokens=512)
response = await sampling_client.sample_async(
    prompt=prompt, num_samples=8, sampling_params=params,
)
```

**min\_p** `number` (default: 0)

Relative probability threshold, in `[0, 1]`. With `min_p=0.1`, a token stays only if its probability is at least 10% of the top token's probability. `0` turns off the cut-off.

The sampler applies the cut-off to the temperature-scaled distribution and renormalizes over the tokens that remain.
Each logprob in the response is the token's log-probability under this renormalized distribution, which the rest of this page calls the cut-off distribution.

## Why use min-p

The min-p threshold scales with the probability of the top token, so the cut-off adapts to the model's confidence at each step:

| Top token probability | Threshold at `min_p=0.1` | Effect |
| - | - | - |
| 0.9 | 0.09 | A confident step keeps only a few plausible tokens. |
| 0.05 | 0.005 | An uncertain step keeps many tokens. |

A higher temperature flattens the distribution and moves probability mass into the long tail of unlikely tokens.
Top-p (`top_p`) keeps the most likely tokens until their combined probability reaches `top_p`.
On a flat distribution, reaching that total takes many tokens, so top-p keeps much of the tail.
Min-p instead compares each token with the top token.
A tail token stays only if its probability reaches `min_p` times the top token's, however many other tail tokens there are.
To get more diverse samples with fewer incoherent tokens, raise `temperature` and set `min_p` between `0.05` and `0.1`.

## Match the cut-off in RL losses

The RL losses `importance_sampling`, `ppo`, `cispo`, `dppo`, and `dro` compare the trainer's logprobs with the sampler's logprobs.
The sampler's logprobs come from the cut-off distribution.
If the trainer scores the full vocabulary instead, each importance ratio `exp(new_logprobs - old_logprobs)` equals 1 minus the probability mass that the cut-off removed, even when the weights match.

To score each target token over the same tokens the sampler kept, add the cut-off to `loss_fn_inputs` with one value per position:

```python theme={"system"}
from baseten.loops import Datum, ModelInput, TensorData

seq = response.sequences[0]
L = len(tokens)
# Positions whose target is a sampled token get the sampler's min_p.
# Prompt positions get 0, which keeps every token.
n_sampled = len(seq.tokens)
min_p = [0.0] * (L - n_sampled) + [0.1] * n_sampled

datum = Datum(
    model_input=ModelInput.from_ints(tokens),
    loss_fn_inputs={
        "target_tokens": TensorData(data=targets, dtype="int64", shape=[L]),
        "logprobs": TensorData(data=behavior_logprobs, dtype="float32", shape=[L]),
        "advantages": TensorData(data=advantages, dtype="float32", shape=[L]),
        "min_p": TensorData(data=min_p, dtype="float32", shape=[L]),
    },
)

fb = training_client.forward_backward(
    data=[datum],
    loss_fn="ppo",
).result(timeout=600.0)
```

For each cut-off that you set in `SamplingParams`, pass the matching input below.
Each input has shape `[L]`, aligned with `target_tokens`.

**min\_p** `TensorData`

`float32` values in `[0, 1]`. `0` keeps every token at that position.

**top\_k** `TensorData`

Integer values. `0` or a negative value keeps every token at that position.

**top\_p** `TensorData`

`float32` values in `(0, 1]`. `1` keeps every token at that position.

* Set each cut-off input on every `Datum` in the batch or on none of them. The trainer rejects a batch that sets an input on only some datums.
* If you sample at a temperature other than 1, also pass a `temperatures` input with the sampling temperature at each position. The trainer applies the cut-off to the temperature-scaled distribution, as the sampler does.
* Don't sample with `top_p` when the samples feed an RL loss. Use `min_p` alone or with `top_k`. The sampler's top-p implementation can keep less than `top_p` of the probability mass. The trainer's top-p then keeps tokens the sampler couldn't draw, which pushes the importance ratios below 1.
* `cross_entropy`, `dpo`, and `forward_backward_custom()` reject cut-off inputs.

### Check the cut-off match

With cut-off inputs, the RL losses report `mismatch_cutoff_forced_rate` in `fb.metrics`.
It's the share of sampled tokens that the trainer's cut-off would have removed.
The trainer keeps those tokens, because the sampler drew them.
The rate stays well under 1% when the inputs match `SamplingParams`.
A higher rate means the inputs differ from the settings that produced the samples.

## Next steps

* [Sampling settings](https://docs.baseten.co/loops/hyperparameters.md#sampling-settings): Set the other `SamplingParams` fields.
* [Loss functions](https://docs.baseten.co/loops/loss-functions.md): Look up the inputs and configuration for each RL loss.
* [Check whether an RL run is learning](https://docs.baseten.co/loops/rl-run-health.md#sampler-and-trainer-kl-divergence): Monitor the KL divergence between the sampler and trainer.
