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
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 | |
_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
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 | |
_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
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 | |