Skip to content

Commit 36a10af

Browse files
committed
Simplify comment, add CPU unit test for compute_topk_scores
- Shorten verbose comment in gpu_model_runner.py - Add test_compute_topk_scores_logits_mode: CPU-only test that verifies logits_mode returns raw values while default returns log_softmax, with matching token_ids and ranks Signed-off-by: Allen Shen <aoshen@inferact.ai>
1 parent df65310 commit 36a10af

3 files changed

Lines changed: 35 additions & 11 deletions

File tree

csrc/cpu/cpu_types_riscv_impl.hpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -638,21 +638,20 @@ struct FP32Vec16 : public Vec<FP32Vec16> {
638638
const uint32_t hi = static_cast<uint32_t>(q >> 32);
639639

640640
auto lane_ids = RVVI(__riscv_vid_v_u32, LMUL_256)(HALF);
641-
auto shifts =
642-
RVVI(__riscv_vsll_vx_u32, LMUL_256)(lane_ids, 2, HALF);
641+
auto shifts = RVVI(__riscv_vsll_vx_u32, LMUL_256)(lane_ids, 2, HALF);
643642

644643
auto packed_lo = RVVI(__riscv_vmv_v_x_u32, LMUL_256)(lo, HALF);
645644
auto idx_lo = RVVI(__riscv_vand_vx_u32, LMUL_256)(
646-
RVVI(__riscv_vsrl_vv_u32, LMUL_256)(packed_lo, shifts, HALF),
647-
0xF, HALF);
645+
RVVI(__riscv_vsrl_vv_u32, LMUL_256)(packed_lo, shifts, HALF), 0xF,
646+
HALF);
648647

649648
auto packed_hi = RVVI(__riscv_vmv_v_x_u32, LMUL_256)(hi, HALF);
650649
auto idx_hi = RVVI(__riscv_vand_vx_u32, LMUL_256)(
651-
RVVI(__riscv_vsrl_vv_u32, LMUL_256)(packed_hi, shifts, HALF),
652-
0xF, HALF);
650+
RVVI(__riscv_vsrl_vv_u32, LMUL_256)(packed_hi, shifts, HALF), 0xF,
651+
HALF);
653652

654-
auto idx = RVVI4(__riscv_vcreate_v_u32, LMUL_256, _u32,
655-
LMUL_512)(idx_lo, idx_hi);
653+
auto idx =
654+
RVVI4(__riscv_vcreate_v_u32, LMUL_256, _u32, LMUL_512)(idx_lo, idx_hi);
656655
reg = RVVI(__riscv_vrgather_vv_f32, LMUL_512)(lut.reg, idx, VEC_ELEM_NUM);
657656
}
658657
explicit FP32Vec16(const FP16Vec16& v);

tests/v1/sample/test_sampling_params_e2e.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,33 @@ def test_seed(llm):
184184
assert out_1[0].outputs[0].text != out_3[0].outputs[0].text
185185

186186

187+
def test_logits_mode_returns_raw_scores():
188+
"""CPU test: logits_mode gather returns raw values, not log_softmax."""
189+
import torch
190+
191+
logits = torch.tensor([[3.0, 1.0, 4.0, 1.5]], dtype=torch.float32)
192+
target = torch.tensor([2], dtype=torch.int64)
193+
194+
# Build token_ids matrix: [target, topk...]
195+
token_ids = target.unsqueeze(-1)
196+
topk = torch.topk(logits, 2, dim=-1).indices
197+
token_ids = torch.cat((token_ids, topk), dim=1)
198+
199+
# logits_mode path: raw gather
200+
scores = logits.gather(-1, token_ids)
201+
assert scores[0, 0].item() == pytest.approx(4.0) # raw logit
202+
203+
# logprobs path: log_softmax
204+
logprobs = logits.log_softmax(dim=-1).gather(-1, token_ids)
205+
assert logprobs[0, 0].item() <= 0 # log_softmax always <= 0
206+
assert logprobs[0, 0].item() != pytest.approx(4.0) # different
207+
208+
# Same token_ids, same topk order — only scores differ
209+
assert token_ids[0, 0].item() == 2 # target
210+
assert token_ids[0, 1].item() == 2 # top-1 (4.0)
211+
assert token_ids[0, 2].item() == 0 # top-2 (3.0)
212+
213+
187214
def test_prompt_logprobs_respects_logprobs_mode():
188215
"""raw_logits must return positive logits, raw_logprobs must return
189216
negative log_softmax values, and both must differ."""

vllm/v1/worker/gpu_model_runner.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5566,9 +5566,7 @@ def _get_prompt_logprobs_dict(
55665566
# to gather the logprob for.
55675567
tgt_token_ids = prompt_token_ids[start_tok : start_tok + num_logits]
55685568

5569-
# Prompt tokens have no sampling processors, so raw_* and
5570-
# processed_* use the same source; the mode only selects
5571-
# logits vs log_softmax(logits).
5569+
# Compute prompt scores respecting logprobs_mode.
55725570
if self.model_config.logprobs_mode.endswith("logits"):
55735571
scores = logits.to(torch.float32)
55745572
else:

0 commit comments

Comments
 (0)