{"record":{"id":"a9d216642a078fbd","repo":"sgl-project/sglang","slug":"top-p-values-must-be-in-0-1","errorCode":null,"errorMessage":"top_p values must be in (0, 1]","messagePattern":"top_p values must be in \\(0, 1\\]","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/kernels/ops/sampling/renorm_triton.py","lineNumber":117,"sourceCode":"    the bandwidth-heavy masking, partial reduction, and normalization.\n    \"\"\"\n    probs_fp32 = _prepare_probs(probs)\n    batch_size, vocab_size = probs_fp32.shape\n    if batch_size == 0 or vocab_size == 0:\n        return probs_fp32\n\n    if isinstance(top_p, torch.Tensor):\n        top_ps = top_p.to(device=probs.device, dtype=torch.float32).reshape(-1)\n        if top_ps.numel() == 1:\n            top_ps = top_ps.expand(batch_size)\n        elif top_ps.numel() != batch_size:\n            raise ValueError(\n                f\"top_p must be scalar or have one value per row, got \"\n                f\"{top_ps.numel()} values for {batch_size} rows\"\n            )\n    else:\n        if not 0.0 < float(top_p) <= 1.0:\n            raise ValueError(\"top_p values must be in (0, 1]\")\n        top_ps = torch.full(\n            (batch_size,), float(top_p), device=probs.device, dtype=torch.float32\n        )\n\n    # Match FlashInfer's threshold semantics: sort ascending, discard the prefix\n    # whose cumulative mass is below 1 - p, and retain all ties at the pivot.\n    sorted_probs = torch.sort(probs_fp32, dim=-1).values\n    cdf = torch.cumsum(sorted_probs, dim=-1)\n    cutoff = torch.searchsorted(cdf, (1.0 - top_ps).unsqueeze(1), right=False).squeeze(\n        1\n    )\n    cutoff.clamp_(max=vocab_size - 1)\n    pivots = sorted_probs.gather(1, cutoff.unsqueeze(1)).squeeze(1).contiguous()\n\n    return _renorm_from_pivots(probs_fp32, pivots)\n\n\ndef top_k_renorm_probs_triton(","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/kernels/ops/sampling/renorm_triton.py#L99-L135","documentation":"When top_p is given as a Python scalar (not a tensor), the kernel validates the range (0, 1]: p must be positive (p=0 would keep nothing) and at most 1 (p>1 is not a probability mass fraction). Out-of-range scalars are rejected before building the threshold tensor.","triggerScenarios":"Calling top_p_renorm_probs_triton with top_p=0.0, a negative value, or something > 1.0; also float('nan') fails the comparison chain.","commonSituations":"Config typos (top_p=0 meaning 'disabled' in some frameworks but invalid here), misparsed CLI floats, or default-sentinel values like 0 passed through from a sampling-params object.","solutions":["Use a top_p strictly in (0, 1], e.g. 1.0 to disable top-p filtering semantics","Treat top_p<=0 in your config as 'no filtering' and substitute 1.0 before calling"],"exampleFix":"# before\nout = top_p_renorm_probs_triton(probs, 0.0)\n# after\ntop_p = 1.0 if top_p is None or top_p <= 0 else top_p\nout = top_p_renorm_probs_triton(probs, top_p)","handlingStrategy":"validation","validationCode":"if not isinstance(top_p, torch.Tensor):\n    assert 0.0 < float(top_p) <= 1.0, top_p\n# map 'disabled' sentinels to 1.0\ntop_p = 1.0 if not top_p or top_p <= 0 else top_p","typeGuard":"def valid_scalar_top_p(p): return isinstance(p, torch.Tensor) or 0.0 < float(p) <= 1.0","tryCatchPattern":null,"preventionTips":["Normalize top_p=0/None sentinels to 1.0 at the config layer"],"tags":["sampling","top-p","value-range","validation"],"backgroundTag":"parameter-out-of-range","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}