[Bugfix][Rust Frontend] Reject min_tokens above max_tokens (#46733)
Co-authored-by: Bugen Zhao <i@bugenzhao.com> Signed-off-by: reidliu41 <reid201711@gmail.com> Signed-off-by: Bugen Zhao <i@bugenzhao.com>
This commit is contained in:
@@ -20,6 +20,11 @@ pub enum Error {
|
||||
Logprobs(#[from] LogprobsError),
|
||||
#[error(transparent)]
|
||||
TokenIds(#[from] TokenIdsError),
|
||||
#[error(
|
||||
"`min_tokens` must be less than or equal to `max_tokens`, \
|
||||
got min_tokens={min_tokens}, max_tokens={max_tokens}"
|
||||
)]
|
||||
MinTokensExceedsMaxTokens { min_tokens: u32, max_tokens: u32 },
|
||||
#[error("`thinking_token_budget` must be a non-negative integer or -1 for unlimited.")]
|
||||
InvalidThinkingTokenBudget,
|
||||
#[error("text request stream `{request_id}` closed before terminal output")]
|
||||
@@ -32,6 +37,24 @@ pub enum Error {
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
impl Error {
|
||||
/// Whether this error represents invalid user request parameters.
|
||||
pub fn is_request_validation_error(&self) -> bool {
|
||||
match self {
|
||||
Self::PromptTooLong { .. }
|
||||
| Self::EmptyPromptTokenIds { .. }
|
||||
| Self::Logprobs(_)
|
||||
| Self::TokenIds(_)
|
||||
| Self::MinTokensExceedsMaxTokens { .. }
|
||||
| Self::InvalidThinkingTokenBudget
|
||||
// An empty tokenized prompt detected later, at request prepare
|
||||
// time, surfaces through the transparent Llm wrapper.
|
||||
| Self::Llm(LlmError::EmptyPromptTokenIds { .. }) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<vllm_tokenizer::TokenizerError> for Error {
|
||||
fn from(error: vllm_tokenizer::TokenizerError) -> Self {
|
||||
Self::Tokenizer(error.0)
|
||||
|
||||
@@ -128,6 +128,12 @@ pub fn lower_sampling_params(
|
||||
prompt_len,
|
||||
)?;
|
||||
let min_tokens = min_tokens.unwrap_or(0);
|
||||
if min_tokens > max_tokens {
|
||||
return Err(Error::MinTokensExceedsMaxTokens {
|
||||
min_tokens,
|
||||
max_tokens,
|
||||
});
|
||||
}
|
||||
let thinking_token_budget = normalize_thinking_token_budget(thinking_token_budget)?;
|
||||
let frequency_penalty = frequency_penalty.unwrap_or(0.0);
|
||||
let presence_penalty = presence_penalty.unwrap_or(0.0);
|
||||
@@ -414,6 +420,27 @@ mod tests {
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lower_sampling_params_rejects_min_tokens_above_resolved_max_tokens() {
|
||||
let error = lower_sampling_params_with_limits(
|
||||
SamplingParams {
|
||||
max_tokens: Some(4),
|
||||
min_tokens: Some(5),
|
||||
..SamplingParams::default()
|
||||
},
|
||||
sample_sampling_limits(),
|
||||
)
|
||||
.unwrap_err();
|
||||
|
||||
assert!(matches!(
|
||||
error,
|
||||
Error::MinTokensExceedsMaxTokens {
|
||||
min_tokens: 5,
|
||||
max_tokens: 4,
|
||||
}
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lower_text_request_applies_python_style_eos_hints() {
|
||||
let prepared = lower_text_request(
|
||||
|
||||
Reference in New Issue
Block a user