diff --git a/vllm/sampling_params.py b/vllm/sampling_params.py index ee50afb18a..e4e203b103 100644 --- a/vllm/sampling_params.py +++ b/vllm/sampling_params.py @@ -789,8 +789,9 @@ class SamplingParams( self._validate_logprobs(model_config) self._validate_logit_bias(model_config) self._validate_trace_replay(model_config, speculative_config) + self._validate_stop_token_ids(model_config) self._validate_logits_processors(model_config) - self._validate_allowed_token_ids(tokenizer) + self._validate_allowed_token_ids(model_config) self._validate_spec_decode(speculative_config) self._validate_diffusion(model_config) self._validate_structured_outputs( @@ -859,6 +860,31 @@ class SamplingParams( value=num_prompt_logprobs, ) + def _validate_stop_token_ids(self, model_config: ModelConfig) -> None: + """Validate stop_token_ids are within vocabulary range.""" + if not self.stop_token_ids: + return + + # stop_token_ids are used as column indices into the logits tensor, + # whose width is the model's vocab size (LogitsProcessor is built from + # config.vocab_size, InputBatch.vocab_size comes from + # model_config.get_vocab_size()), so use the same bound here — like + # _validate_logit_bias, which indexes the same tensor. + vocab_size = model_config.get_vocab_size() + invalid_token_ids = [ + token_id + for token_id in self.stop_token_ids + if token_id < 0 or token_id >= vocab_size + ] + + if invalid_token_ids: + raise VLLMValidationError( + f"token_id(s) {invalid_token_ids} in stop_token_ids contain " + f"out-of-vocab token ids. Vocabulary size: {vocab_size}", + parameter="stop_token_ids", + value=invalid_token_ids, + ) + def _validate_logit_bias(self, model_config: ModelConfig) -> None: """Validate logit_bias token IDs are within vocabulary range.""" if not self.logit_bias: @@ -941,7 +967,7 @@ class SamplingParams( validate_logits_processors_parameters(model_config.logits_processors, self) - def _validate_allowed_token_ids(self, tokenizer: TokenizerLike | None) -> None: + def _validate_allowed_token_ids(self, model_config: ModelConfig) -> None: allowed_token_ids = self.allowed_token_ids if allowed_token_ids is None: return @@ -953,19 +979,24 @@ class SamplingParams( value=allowed_token_ids, ) - if tokenizer is not None: - vocab_size = len(tokenizer) - invalid_token_ids = [ - token_id - for token_id in allowed_token_ids - if token_id < 0 or token_id >= vocab_size - ] - if invalid_token_ids: - raise VLLMValidationError( - "allowed_token_ids contains out-of-vocab token id!", - parameter="allowed_token_ids", - value=invalid_token_ids, - ) + # allowed_token_ids are client-supplied ids used as column indices + # into the logits tensor (the mask in InputBatch is sized by + # model_config.get_vocab_size(), and the ids are written as + # mask[req_index][allowed_token_ids]), so use the same bound here — + # like _validate_stop_token_ids and _validate_logit_bias, which + # index the same tensor. + vocab_size = model_config.get_vocab_size() + invalid_token_ids = [ + token_id + for token_id in allowed_token_ids + if token_id < 0 or token_id >= vocab_size + ] + if invalid_token_ids: + raise VLLMValidationError( + "allowed_token_ids contains out-of-vocab token id!", + parameter="allowed_token_ids", + value=invalid_token_ids, + ) def _validate_spec_decode( self,