|
| 1 | +// Ported from: vllm/v1/sample/metadata.py @ e24d1b24 |
| 2 | +// |
| 3 | +// SamplingMetadata — the per-slot sampling state the V1 Sampler (M1.7) consumes, |
| 4 | +// built once per step by InputBatch::make_sampling_metadata() (the port of |
| 5 | +// gpu_input_batch.py::_make_sampling_metadata). Field NAMES mirror upstream 1:1. |
| 6 | +// |
| 7 | +// ─── T0 field subset ──────────────────────────────────────────────────────── |
| 8 | +// The scalar/vector fields are the num_reqs-dense slices of the InputBatch |
| 9 | +// per-slot arrays. Upstream keeps them as device tensors sliced `[:num_reqs]`; |
| 10 | +// here each is a host std::vector already truncated to num_reqs. The |
| 11 | +// None-when-not-needed optionals (temperature/top_p/top_k/prompt_token_ids/ |
| 12 | +// allowed_token_ids_mask) preserve upstream's "skip the copy" semantics so the |
| 13 | +// sampler's branching ports unchanged. |
| 14 | +// |
| 15 | +// ─── logitsprocs → flat fields (recorded deviation) ───────────────────────── |
| 16 | +// Upstream carries a `logitsprocs: LogitsProcessors` plugin object graph plus |
| 17 | +// `logprob_token_ids`, `spec_token_ids`, `thinking_budget_state_holder`. We do |
| 18 | +// NOT port the plugin interface; instead the three T0 builtins |
| 19 | +// (vllm/v1/sample/logits_processor/builtin.py) are represented as flat inputs — |
| 20 | +// `min_tokens`, `logit_bias`, `min_p` — since Task 3 ports the three builtins |
| 21 | +// directly as functions rather than as a plugin dispatch. The remaining plugin |
| 22 | +// members are marked stubs below (defaulted empty/None) with their upstream cite. |
| 23 | +#ifndef VLLM_V1_SAMPLE_METADATA_H_ |
| 24 | +#define VLLM_V1_SAMPLE_METADATA_H_ |
| 25 | + |
| 26 | +#include <cstdint> |
| 27 | +#include <map> |
| 28 | +#include <optional> |
| 29 | +#include <set> |
| 30 | +#include <vector> |
| 31 | + |
| 32 | +namespace vllm::v1 { |
| 33 | + |
| 34 | +// Per-request min-tokens state (the flattened MinTokensLogitsProcessor input, |
| 35 | +// vllm/v1/sample/logits_processor/builtin.py::MinTokensLogitsProcessor). While |
| 36 | +// `output_len < min_tokens`, the sampler masks every id in `stop_token_ids` |
| 37 | +// (eos + stop_token_ids, i.e. upstream `params.all_stop_token_ids`) to -inf. |
| 38 | +struct MinTokensState { |
| 39 | + int min_tokens = 0; |
| 40 | + std::set<int32_t> stop_token_ids; |
| 41 | +}; |
| 42 | + |
| 43 | +// SamplingMetadata (vllm/v1/sample/metadata.py::SamplingMetadata) — T0 subset. |
| 44 | +struct SamplingMetadata { |
| 45 | + // None when all_greedy (upstream skips the temperature copy). Else [num_reqs]. |
| 46 | + std::optional<std::vector<float>> temperature; |
| 47 | + bool all_greedy = true; |
| 48 | + bool all_random = false; |
| 49 | + |
| 50 | + // None when no_top_p / no_top_k. Else [num_reqs]. |
| 51 | + std::optional<std::vector<float>> top_p; |
| 52 | + std::optional<std::vector<int32_t>> top_k; |
| 53 | + |
| 54 | + // req_index -> per-request RNG seed. Upstream is `dict[int, torch.Generator]`; |
| 55 | + // we don't have torch.Generator, so we carry the seed (the actual seeded RNG |
| 56 | + // lands in Task 2's random_sample). Requests without their own seed are absent |
| 57 | + // from the map (upstream NOTE at gpu_input_batch.py:251-252). |
| 58 | + std::map<int, uint64_t> generators; |
| 59 | + |
| 60 | + // None => no logprobs; 0 => sampled-token logprob only; k => top-k; -1 => all. |
| 61 | + std::optional<int> max_num_logprobs; |
| 62 | + |
| 63 | + bool no_penalties = true; |
| 64 | + // None unless penalties (or a token-id-consuming proc) need it. Ragged per-req |
| 65 | + // prompt token ids (upstream: a padded [num_reqs, max_prompt_len] i32 tensor). |
| 66 | + std::optional<std::vector<std::vector<int32_t>>> prompt_token_ids; |
| 67 | + // [num_reqs] each (dense slices of the InputBatch penalty arrays). |
| 68 | + std::vector<float> frequency_penalties; |
| 69 | + std::vector<float> presence_penalties; |
| 70 | + std::vector<float> repetition_penalties; |
| 71 | + |
| 72 | + // Per-request generated tokens so far (empty when no proc needs them, matching |
| 73 | + // upstream's needs_output_token_ids gate). |
| 74 | + std::vector<std::vector<int32_t>> output_token_ids; |
| 75 | + |
| 76 | + // None unless a request restricts allowed ids. Upstream is a 2D bool tensor |
| 77 | + // [num_reqs, vocab]; represented here as row-major bool rows [num_reqs][vocab]. |
| 78 | + std::optional<std::vector<std::vector<uint8_t>>> allowed_token_ids_mask; |
| 79 | + |
| 80 | + // req_index -> list of bad-words token-id n-grams |
| 81 | + // (vllm/v1/sample/ops/bad_words.py::apply_bad_words input). |
| 82 | + std::map<int, std::vector<std::vector<int32_t>>> bad_words_token_ids; |
| 83 | + |
| 84 | + // ─── T0 builtin logits-processor inputs (flat; see header deviation) ─────── |
| 85 | + // req_index -> min-tokens state (MinTokensLogitsProcessor). |
| 86 | + std::map<int, MinTokensState> min_tokens; |
| 87 | + // req_index -> (token_id -> additive bias) (LogitBiasLogitsProcessor). |
| 88 | + std::map<int, std::map<int32_t, float>> logit_bias; |
| 89 | + // [num_reqs] min-p thresholds (MinPLogitsProcessor); 0 disables per row. |
| 90 | + std::vector<float> min_p; |
| 91 | + |
| 92 | + // ─── STUBS (marked; defaulted empty/None at T0) ──────────────────────────── |
| 93 | + // Upstream `logitsprocs: LogitsProcessors` plugin graph — NOT ported (the |
| 94 | + // three T0 builtins are the flat fields above). No field. |
| 95 | + // |
| 96 | + // Upstream `logprob_token_ids: dict[int, list[int]] | None` (generative- |
| 97 | + // scoring: gather logprobs for specific ids). Deferred behind this stub. |
| 98 | + std::optional<std::map<int, std::vector<int32_t>>> logprob_token_ids; |
| 99 | + // Upstream `spec_token_ids: list[list[int]] | None` (speculative decode). |
| 100 | + // Always empty lists at T0 (kept so the sampler's spec branch ports). |
| 101 | + std::optional<std::vector<std::vector<int32_t>>> spec_token_ids; |
| 102 | + // Upstream `thinking_budget_state_holder` — deferred; no field/flag ported. |
| 103 | +}; |
| 104 | + |
| 105 | +} // namespace vllm::v1 |
| 106 | + |
| 107 | +#endif // VLLM_V1_SAMPLE_METADATA_H_ |
0 commit comments