Logits are the model’s unnormalized scores for the next token. Softmax converts them into probabilities. Temperature changes how concentrated those probabilities are, while top-p removes the low-probability tail until a chosen cumulative mass is retained. Because top-p depends on those probabilities, its position in the sequence of operations matters.

Top-p is a decision about probability mass. Temperature changes that mass before we decide which tokens belong to the nucleus. Divide logits by temperature, normalize with softmax, then take the smallest ranked set whose cumulative probability reaches p. If we choose the set first and apply temperature only inside that set, a token excluded at the first step can never return. Those two pipelines define different sampling distributions. Hugging Face's generation configuration documents temperature and top-p as separate controls. The order is part of our sampler implementation, not something the seed can repair.

Take three logits 0, -1, -2. At temperature 1, their probabilities are about 0.665, 0.245, 0.090. With top-p at 0.8, we need the first two tokens. At temperature 0.5, the probabilities become about 0.867, 0.117, 0.016. The first token alone crosses 0.8. The rank order did not change because positive temperature preserves rank, but the membership of the nucleus did. That is why top-k and top-p react differently to temperature. Top-k membership stays the same in this simple case, while top-p's need not.

For a production refactor I would compare the full next-token probability distribution on small crafted logits, then use a seeded sampler as a secondary regression check. Include allowed-token masks, repetition penalties, minimum-token rules, padding tokens in a sharded vocabulary, ties and very low temperature. Write down the transform order once so CPU reference and GPU path implement the same distribution. A changed string under the same seed could be a harmless difference in random-number consumption, but a changed nucleus is an actual algorithm change. If the new order is intentional, treat it as a new API behavior. Users may have tuned temperature and top-p together, so compare quality and communicate the change.

To make the difference explicit, apply top-p first at temperature 1. It keeps the first two tokens. Scaling their original scores by 0.5 then renormalizing gives approximately 0.881 and 0.119 within that set. Applying temperature 0.5 before top-p instead retains only the first token, giving final probabilities 1 and 0. The same uniform random draw can therefore select different outputs. Reusing a seed controls a source of randomness. It does not make two different probability distributions equal.

A useful reference test computes the retained set and final normalized probabilities without drawing a sample. For these crafted logits it can assert the two expected sets directly. Then test threshold boundaries, including whether the token that crosses the cumulative threshold is retained, and any minimum-token rule. Positive finite temperature preserves score ordering in this simple example, but ties, numerical precision and other score processors still need a defined policy. Temperature zero is usually handled as a greedy mode rather than literal division by zero.

If both implementations produce the same probabilities but different sampled strings, inspect their random-number consumption and mapping from draws to tokens. If the retained probabilities differ, investigate the transform contract first. This separation makes the regression actionable instead of turning every mismatch into a request to try more seeds.