Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
OpenDAS
vllm_cscc
Commits
8d0a01a5
Unverified
Commit
8d0a01a5
authored
Jul 21, 2025
by
Lu Fang
Committed by
GitHub
Jul 21, 2025
Browse files
[v1][sampler] Inplace logprobs comparison to get the token rank (#21283)
Signed-off-by:
Lu Fang
<
lufang@fb.com
>
parent
0ec82edd
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
1 deletion
+26
-1
vllm/v1/sample/ops/logprobs.py
vllm/v1/sample/ops/logprobs.py
+24
-0
vllm/v1/sample/sampler.py
vllm/v1/sample/sampler.py
+2
-1
No files found.
vllm/v1/sample/ops/logprobs.py
0 → 100644
View file @
8d0a01a5
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""Some utilities for logprobs, including logits."""
import
torch
@
torch
.
compile
(
dynamic
=
True
)
def
batched_count_greater_than
(
x
:
torch
.
Tensor
,
values
:
torch
.
Tensor
)
->
torch
.
Tensor
:
"""
Counts elements in each row of x that are greater than the corresponding
value in values. Use torch.compile to generate an optimized kernel for
this function. otherwise, it will create additional copies of the input
tensors and cause memory issues.
Args:
x (torch.Tensor): A 2D tensor of shape (batch_size, n_elements).
values (torch.Tensor): A 2D tensor of shape (batch_size, 1).
Returns:
torch.Tensor: A 1D tensor of shape (batch_size,) with the counts.
"""
return
(
x
>=
values
).
sum
(
-
1
)
vllm/v1/sample/sampler.py
View file @
8d0a01a5
...
@@ -9,6 +9,7 @@ from vllm.utils import is_pin_memory_available
...
@@ -9,6 +9,7 @@ from vllm.utils import is_pin_memory_available
from
vllm.v1.outputs
import
LogprobsTensors
,
SamplerOutput
from
vllm.v1.outputs
import
LogprobsTensors
,
SamplerOutput
from
vllm.v1.sample.metadata
import
SamplingMetadata
from
vllm.v1.sample.metadata
import
SamplingMetadata
from
vllm.v1.sample.ops.bad_words
import
apply_bad_words
from
vllm.v1.sample.ops.bad_words
import
apply_bad_words
from
vllm.v1.sample.ops.logprobs
import
batched_count_greater_than
from
vllm.v1.sample.ops.penalties
import
apply_all_penalties
from
vllm.v1.sample.ops.penalties
import
apply_all_penalties
from
vllm.v1.sample.ops.topk_topp_sampler
import
TopKTopPSampler
from
vllm.v1.sample.ops.topk_topp_sampler
import
TopKTopPSampler
...
@@ -174,7 +175,7 @@ class Sampler(nn.Module):
...
@@ -174,7 +175,7 @@ class Sampler(nn.Module):
token_logprobs
=
logprobs
.
gather
(
-
1
,
token_ids
)
token_logprobs
=
logprobs
.
gather
(
-
1
,
token_ids
)
# Compute the ranks of the actual token.
# Compute the ranks of the actual token.
token_ranks
=
(
logprobs
>=
token_logprobs
)
.
sum
(
-
1
)
token_ranks
=
batched_count_greater_than
(
logprobs
,
token_logprobs
)
# Concatenate together with the topk.
# Concatenate together with the topk.
indices
=
torch
.
cat
((
token_ids
,
topk_indices
),
dim
=
1
)
indices
=
torch
.
cat
((
token_ids
,
topk_indices
),
dim
=
1
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment