Skip to content

vllm.v1.worker.gpu.sample.logprob

Classes:

Functions:

LogprobTokenIdsState

Per-request override of which token ids' logprobs to return.

See SamplingParams.logprob_token_ids.

Source code in vllm/v1/worker/gpu/sample/logprob.py
class LogprobTokenIdsState:
    """Per-request override of which token ids' logprobs to return.

    See `SamplingParams.logprob_token_ids`.
    """

    def __init__(self, max_num_reqs: int, device: torch.device):
        self.max_num_reqs = max_num_reqs
        self.num_token_ids = UvaBackedTensor(max_num_reqs, dtype=torch.int32)
        self.token_ids = StagedWriteTensor(
            (max_num_reqs, MAX_LOGPROB_TOKEN_IDS),
            dtype=torch.int32,
            device=device,
        )

    def add_request(self, req_idx: int, sampling_params: SamplingParams) -> None:
        token_ids = sampling_params.logprob_token_ids
        if not token_ids:
            self.num_token_ids.np[req_idx] = 0
            return
        n = len(token_ids)
        if n > MAX_LOGPROB_TOKEN_IDS:
            raise ValueError(
                f"Too many logprob_token_ids: {n}. The max is {MAX_LOGPROB_TOKEN_IDS}."
            )
        self.num_token_ids.np[req_idx] = n
        self.token_ids.stage_write(req_idx, 0, token_ids)

    def apply_staged_writes(self) -> None:
        self.num_token_ids.copy_to_uva()
        self.token_ids.apply_write()

    def max_num_token_ids(self, idx_mapping_np: np.ndarray) -> int:
        return int(self.num_token_ids.np[idx_mapping_np].max(initial=0))

concat_logprob_stashes(parts, cu_num_generated_tokens)

Join the logprobs stashed for the requests committing this step.

Each stash is as wide as the widest logprobs request in the batch at the step that request converged, so stashes from different steps can differ in width. Pad the narrow ones the way compute_topk_scores pads a mixed batch: token id 0 at -inf, which the output processor never reports.

Source code in vllm/v1/worker/gpu/sample/logprob.py
def concat_logprob_stashes(
    parts: list[LogprobsTensors], cu_num_generated_tokens: list[int]
) -> LogprobsTensors:
    """Join the logprobs stashed for the requests committing this step.

    Each stash is as wide as the widest logprobs request in the batch at the
    step that request converged, so stashes from different steps can differ
    in width. Pad the narrow ones the way compute_topk_scores pads a mixed
    batch: token id 0 at -inf, which the output processor never reports.
    """
    width = max(p.logprob_token_ids.shape[1] for p in parts)

    def pad(t: torch.Tensor, value: float) -> torch.Tensor:
        return F.pad(t, (0, width - t.shape[1]), value=value)

    return LogprobsTensors(
        logprob_token_ids=torch.cat([pad(p.logprob_token_ids, 0) for p in parts]),
        logprobs=torch.cat([pad(p.logprobs, float("-inf")) for p in parts]),
        selected_token_ranks=torch.cat([p.selected_token_ranks for p in parts]),
        cu_num_generated_tokens=cu_num_generated_tokens,
    )

masked_stash_write(stash, slots, keep, new)

Write new ([n * W] rows) into the per-slot stash where keep.

The PyTorch counterpart of DiffusionGemma's stash_converged_logprobs.

Source code in vllm/v1/worker/gpu/sample/logprob.py
def masked_stash_write(
    stash: LogprobsTensors,
    slots: torch.Tensor,
    keep: torch.Tensor,
    new: LogprobsTensors,
) -> None:
    """Write `new` ([n * W] rows) into the per-slot stash where `keep`.

    The PyTorch counterpart of DiffusionGemma's `stash_converged_logprobs`.
    """
    n = slots.shape[0]
    pad = stash.logprobs.shape[-1] - new.logprobs.shape[1]
    ids = F.pad(new.logprob_token_ids.to(torch.int64), (0, pad))
    lps = F.pad(new.logprobs, (0, pad), value=float("-inf"))
    ranks = new.selected_token_ranks.to(torch.int64).view(n, -1)
    W = ranks.shape[1]
    for buf, rows in (
        (stash.logprob_token_ids, ids.view(n, W, -1)),
        (stash.logprobs, lps.view(n, W, -1)),
        (stash.selected_token_ranks, ranks),
    ):
        mask = keep.view(n, *[1] * (rows.dim() - 1))
        buf[slots, :W] = torch.where(mask, rows, buf[slots, :W])