diff --git a/helios/src/server/request.rs b/helios/src/server/request.rs index bde28ab42..3f2cf606a 100644 --- a/helios/src/server/request.rs +++ b/helios/src/server/request.rs @@ -608,7 +608,23 @@ fn output_limit( Some(value) => value .as_u64() .ok_or("output limit must be a nonnegative integer")?, - None => (configured as usize).min(remaining) as u64, + None => { + if remaining == 0 { + return Err(Error::admission( + AdmissionError::Context, + if prompt > context { + format!( + "prompt of {prompt} tokens exceeds this server's context of {context} tokens" + ) + } else { + format!( + "prompt of {prompt} tokens fills this server's context of {context} tokens, leaving no room for output tokens" + ) + }, + )); + } + (configured as usize).min(remaining) as u64 + } }; let max_new_tokens = u32::try_from(limit).map_err(|error| { Error::Admission(AdmissionError::Invalid(Failure::caused_by( @@ -1037,8 +1053,36 @@ mod tests { ] { assert!(limit(body, 262144, 144).is_err()); } - assert!(limit(serde_json::json!({}), 262144, 262144).is_err()); - assert!(limit(serde_json::json!({}), 262144, 262145).is_err()); + for (body, prompt) in [ + (serde_json::json!({"max_tokens":0}), 144), + (serde_json::json!({"max_completion_tokens":0}), 262144), + ] { + let error = limit(body, 262144, prompt).unwrap_err(); + assert!(!matches!(error, Error::Admission(AdmissionError::Context(_))), "{error:?}"); + assert!( + error.to_string().contains("output limit must be between 1 and 2147483647 tokens"), + "{error}" + ); + } + for body in [serde_json::json!({}), serde_json::json!({"max_tokens":null})] { + let filled = limit(body.clone(), 262144, 262144).unwrap_err(); + assert!(matches!(filled, Error::Admission(AdmissionError::Context(_))), "{filled:?}"); + assert!( + filled.to_string().contains("fills this server's context of 262144 tokens, leaving no room for output tokens"), + "{filled}" + ); + let exceeded = limit(body, 262144, 262145).unwrap_err(); + assert!(matches!(exceeded, Error::Admission(AdmissionError::Context(_))), "{exceeded:?}"); + assert!( + exceeded.to_string().contains("prompt of 262145 tokens exceeds this server's context of 262144 tokens"), + "{exceeded}" + ); + } + let explicit_overflow = limit(serde_json::json!({"max_tokens":1}), 262144, 262144).unwrap_err(); + assert!( + matches!(explicit_overflow, Error::Admission(AdmissionError::Context(_))), + "{explicit_overflow:?}" + ); } #[test] fn absent_sampling_ranges_fix_distribution_parameters_and_preserve_seed() {