[Performance Improvement] Update batched_count_greater_than to handle batch size 1 without recompile (#38933)

Signed-off-by: Lucas Kabela <lucaskabela@meta.com>
Co-authored-by: Luka Govedič <ProExpertProg@users.noreply.github.com>
This commit is contained in:
Lucas Kabela
2026-04-09 23:51:31 +08:00
committed by GitHub
co-authored by Luka Govedič
parent 3b1d9c3156
commit a8c6ee9b78
3 changed files with 100 additions and 1 deletions
+6
View File
@@ -324,6 +324,12 @@ class Sampler(nn.Module):
token_logprobs = logprobs.gather(-1, token_ids)
# Compute the ranks of the actual token.
# Avoid 0/1 specialization recompile on the batch dimension
# of the compiled batched_count_greater_than. mark_unbacked makes
# the size fully symbolic so dynamo doesn't specialize when
# batch_size transitions from 1 to >=2.
torch._dynamo.decorators.mark_unbacked(logprobs, 0)
torch._dynamo.decorators.mark_unbacked(token_logprobs, 0)
token_ranks = batched_count_greater_than(logprobs, token_logprobs)
# Concatenate together with the topk.