(
LOGITS,
BUFFER,
PERCENTILE_TO_STD_TABLE,
NORMAL_CDF_TO_SIGMA_TABLE,
K,
P,
BATCH_SIZE,
VOCAB_SIZE: tl.constexpr,
MASK_VALUE: tl.constexpr,
BLOCK_SIZE: tl.constexpr,
BLOCK_SIZE_TRUNC: tl.constexpr,
TOPK_ENABLED: tl.constexpr,
TOPP_ENABLED: tl.constexpr,
)
| 83 | |
| 84 | @triton.jit |
| 85 | def _topk_topp_kernel( |
| 86 | LOGITS, |
| 87 | BUFFER, |
| 88 | PERCENTILE_TO_STD_TABLE, |
| 89 | NORMAL_CDF_TO_SIGMA_TABLE, |
| 90 | K, |
| 91 | P, |
| 92 | BATCH_SIZE, |
| 93 | VOCAB_SIZE: tl.constexpr, |
| 94 | MASK_VALUE: tl.constexpr, |
| 95 | BLOCK_SIZE: tl.constexpr, |
| 96 | BLOCK_SIZE_TRUNC: tl.constexpr, |
| 97 | TOPK_ENABLED: tl.constexpr, |
| 98 | TOPP_ENABLED: tl.constexpr, |
| 99 | ): |
| 100 | NUM_TILES: tl.constexpr = (VOCAB_SIZE + BLOCK_SIZE - 1) // BLOCK_SIZE |
| 101 | pid = tl.program_id(0) |
| 102 | num_programs = tl.num_programs(0) |
| 103 | for row_id in tl.range(pid, BATCH_SIZE, num_programs): |
| 104 | LOGITS_ROW = LOGITS + row_id * VOCAB_SIZE |
| 105 | BUFFER_ROW = BUFFER + pid * VOCAB_SIZE |
| 106 | |
| 107 | final_pivot = -float("inf") |
| 108 | duplicate_logit = float("inf") |
| 109 | num_duplicate_logit = tl.zeros((), dtype=tl.uint32) |
| 110 | num_keep = tl.zeros((), dtype=tl.uint32) |
| 111 | num_kept = tl.zeros((), dtype=tl.uint32) |
| 112 | |
| 113 | max_logit = -float("inf") |
| 114 | min_logit = float("inf") |
| 115 | |
| 116 | if TOPK_ENABLED: |
| 117 | k = tl.load(K + row_id) |
| 118 | if k < VOCAB_SIZE: |
| 119 | # Zeroth pass: Compute avg and std from a sample block |
| 120 | offs = tl.arange(0, BLOCK_SIZE) |
| 121 | mask_n = offs < VOCAB_SIZE |
| 122 | logits_blk0 = tl.load(LOGITS_ROW + offs, mask=mask_n, other=-float("inf")) |
| 123 | # Exclude -inf values (e.g. from grammar bitmasks) from |
| 124 | # statistics to avoid NaN in pivot computation. |
| 125 | finite_mask = (logits_blk0 > -float("inf")) & mask_n |
| 126 | num_finite = tl.sum(finite_mask) |
| 127 | finite_logits = tl.where(finite_mask, logits_blk0, 0.0) |
| 128 | avg_logit = tl.where(num_finite > 0, tl.sum(finite_logits) / num_finite, 0.0) |
| 129 | sq_avg_logit = tl.where( |
| 130 | num_finite > 0, |
| 131 | tl.sum(finite_logits * finite_logits) / num_finite, |
| 132 | 0.0, |
| 133 | ) |
| 134 | std_logit = tl.sqrt(tl.maximum(sq_avg_logit - avg_logit * avg_logit, 0.0)) |
| 135 | |
| 136 | # Calculate outlier pivot t for Gaussian sigma-truncation |
| 137 | percentile = tl.cast(k / VOCAB_SIZE * 200, tl.uint32) |
| 138 | percentile = tl.minimum(percentile, 199) |
| 139 | sigma = tl.load(PERCENTILE_TO_STD_TABLE + percentile) |
| 140 | sigma = sigma + tl.abs(sigma) * -0.15 |
| 141 | outlier_pivot = avg_logit + std_logit * sigma |
| 142 | num_outliers = tl.zeros((), dtype=tl.uint32) |
nothing calls this directly
no outgoing calls
no test coverage detected