flashinfer.sampling.softmax

flashinfer.sampling.softmax(logits: Tensor, temperature: float | Tensor | None = None, enable_pdl: bool | None = None, *, batch_invariant: bool = False, out_dtype: dtype | None = None, out: Tensor | None = None) → Tensor

Fused GPU kernel for online safe softmax with temperature scaling.

Parameters:
  • logits (torch.Tensor) – Two-dimensional CUDA tensor of logits. With batch_invariant=True, leading batch dimensions are also supported, e.g. three-dimensional logits.

  • temperature (Optional[Union[torch.Tensor, float]]) – Either a scalar or a tensor of shape (batch_size,) for two-dimensional logits. With batch_invariant=True and additional leading dimensions, a per-row temperature tensor has shape (M,) with M = logits.numel() // logits.shape[-1], or logits.shape[:-1]; a length-one tensor applies to all rows. If a scalar, the same temperature is used for all requests. A per-row tensor gives each row its own temperature. The tensor must be on the same CUDA device as logits and have dtype float16, bfloat16, float32, or float64.

  • enable_pdl (Optional[bool]) – Whether to enable Programmatic Dependent Launch (PDL) for improved performance on supported hardware. If None (default), PDL will be automatically enabled on devices with compute capability >= 9.0. The custom Blackwell softmax routes apply PDL on SM103 and SM107; the SM100 fast path remains non-PDL, while fallback routes preserve their existing PDL behavior. On SM100/SM103/SM107, 32-byte aligned rows whose vocabulary is a multiple of 8 and at most 262144 take the row-caching cluster route, which reads and writes each row exactly once.

  • batch_invariant (bool) – True runs the batch-invariant class lse.row in place of the kernels above: a row’s bits depend on its values, the vocabulary size and the temperature alone, not on the batch it is computed in, its position, the configuration chosen or the SM count, and the result is deterministic (CUDA-graph replay included). The dataflow is a fixed pair of row trees (the maximum and the sum of ex2.approx weights), fingerprinted as flashinfer.batch_invariant.fingerprint("softmax", N=, dtype=, temperature=); the same trees serve log_probs(), so a token’s log-probability and the softmax of its row agree by construction. Supported: bf16 / f16 / fp32 logits of any vocabulary up to 2^21 (and beyond it where the row’s alignment admits a configuration), every temperature form above (a per-row tensor may also take the shape of the leading dimensions of 3-D logits; its values are not validated on the device, so a 0 yields a NaN row: substitute 1.0 for greedy rows), SM100 / SM103 / SM107. Cost: with 16-bit logits the class is faster than the operator’s default route (which casts to fp32 and writes fp32); with fp32 logits it is slower, about 0.4-0.7x of the default route on SM100 / SM107 (three passes over the row against the fused route’s one), while faster than torch’s softmax. Unsupported inputs raise flashinfer.batch_invariant.BatchInvariantUnsupportedError instead of falling back. See flashinfer/batch_invariant/README.md.

  • out_dtype (Optional[torch.dtype]) – Only with batch_invariant=True: None (default) returns the input dtype; torch.float32 returns the probabilities before the class’s output conversion, which for a 16-bit input are the fp32 input’s class bit for bit (the samplers’ softmax(dtype=torch.float32) without the copy).

  • out (Optional[torch.Tensor]) – Only with batch_invariant=True: a caller-owned output buffer of the output dtype and logits.shape (contiguous, 16-byte aligned), written in place and returned, so nothing is allocated (CUDA graphs on caller-owned buffers, a framework’s persistent structures). A buffer that does not fit is refused with ValueError, never copied.

Returns:

probs – Tensor of the same shape as input containing the softmax probabilities.

Return type:

torch.Tensor

Examples

>>> import torch
>>> import flashinfer
>>> torch.manual_seed(42)
>>> batch_size = 4
>>> vocab_size = 5
>>> logits = torch.rand(batch_size, vocab_size).to(0)
>>> logits
tensor([[0.8823, 0.9150, 0.3829, 0.9593, 0.3904],
        [0.6009, 0.2566, 0.7936, 0.9408, 0.1332],
        [0.9346, 0.5936, 0.8694, 0.5677, 0.7411],
        [0.4294, 0.8854, 0.5739, 0.2666, 0.6274]], device='cuda:0')
>>> probs = flashinfer.sampling.softmax(logits, temperature=1.0)
>>> probs
tensor([[0.2309, 0.2385, 0.1401, 0.2493, 0.1412],
        [0.2019, 0.1431, 0.2448, 0.2837, 0.1265],
        [0.2401, 0.1707, 0.2249, 0.1664, 0.1979],
        [0.1724, 0.2719, 0.1991, 0.1465, 0.2101]], device='cuda:0')