vllm.v1.sample.ops.topk_topp_triton
¶
Combined Top-K and Top-P Triton kernels.
Based on the paper "Qrita: High-performance Top-k and Top-p Algorithm for GPUs using Pivot-based Truncation and Selection" By Park et al. (https://arxiv.org/abs/2602.01518)
Functions:
-
apply_top_k_top_p_triton–Apply combined top-k and top-p masking using Triton.
_apply_topp_split(logits, k, p, mask_value, num_sm)
¶
Split-row top-p pipeline for small batches; masks logits in place.
Source code in vllm/v1/sample/ops/topk_topp_triton.py
_topp_sb_combine(PART_BASE, Z, p, lo, hi, best, best_mass, best_minl, best_nmin, S, F, IS_LAST)
¶
Combine one round's partials and advance the search state.
Returns (done, nomask, pivot, dup, numdup, numkeep, lo, hi, best,
best_mass, best_minl, best_nmin, sel_fidx, best_sel). best* tracks the
tightest fallback: the largest evaluated pivot whose above-mass is >= p,
which guarantees the top-p mass invariant if the search fails to
converge. Pivots are recomputed from (lo, hi) via the same ladder the
sweep used, so partials line up with pivots without storing them.
sel_fidx / best_sel report which ladder slot produced dup /
updated best this round (-1 if none), so callers can recover the
per-slice (minl, nmin) partials behind the boundary value for
deterministic duplicate handling.
Source code in vllm/v1/sample/ops/topk_topp_triton.py
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 | |
_topp_sb_ladder(lo, hi, F)
¶
Geometric ladder of F pivots inside (lo, hi).
_topp_sb_row_stats(STATS, row, S)
¶
Combine per-slice partials into row (max, sum_exp, min, n_finite).
Source code in vllm/v1/sample/ops/topk_topp_triton.py
_update_min_larger_stats(data, above_mask, min_larger, num_min_larger, sentinel)
¶
Update running (min, count) of values above a pivot across tiles.
Tracks the smallest value strictly above a pivot and how many times
it occurs. Called once per tile per pivot; the running state is
carried across tiles via min_larger / num_min_larger.
Merge rule
- tile min < running min → replace both
- tile min == running min → accumulate count
- tile min > running min → keep running values
Source code in vllm/v1/sample/ops/topk_topp_triton.py
apply_top_k_top_p_triton(logits, k, p, mask_value=float('-inf'))
¶
Apply combined top-k and top-p masking using Triton.
Top-k is applied first (by logit value), then top-p is applied to the remaining k values (by probability).
Parameters:
-
(logits¶Tensor) –[batch_size, vocab_size] float32 tensor. The returned tensor may alias this input or be a new contiguous tensor for unsupported layouts.
-
(k¶Tensor | None) –[batch_size] int32 tensor of top-k values per row, or None to disable top-k
-
(p¶Tensor | None) –[batch_size] float32 tensor of top-p values per row (0 to 1), or None to disable top-p
-
(mask_value¶float, default:float('-inf')) –Value for masked positions (default: -inf)
Returns:
-
Tensor–The masked logits tensor. It may or may not be modified in-place.
Source code in vllm/v1/sample/ops/topk_topp_triton.py
1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 | |