[Rust Frontend] Add HY3 unified parser and local XGrammar structural-tag builder (#53054)

Signed-off-by: Bugen Zhao <i@bugenzhao.com>
This commit is contained in:
Bugen Zhao
2026-08-20 19:13:54 -07:00
committed by GitHub
parent c6e19b3be2
commit df3b3422b4
17 changed files with 849 additions and 86 deletions
+38 -1
View File
@@ -111,12 +111,32 @@ pub(super) struct GenerationConfig {
pub eos_token_id: Option<OneOrManyTokenIds>,
pub temperature: Option<f32>,
pub top_p: Option<f32>,
#[serde(deserialize_with = "deserialize_top_k")]
pub top_k: Option<u32>,
pub min_p: Option<f32>,
pub repetition_penalty: Option<f32>,
pub max_new_tokens: Option<u32>,
}
/// Deserialize vLLM-compatible `top_k` values from generation configs.
///
/// Both `-1` and `0` disable top-k sampling and become `None`; positive values
/// are preserved.
fn deserialize_top_k<'de, D>(deserializer: D) -> std::result::Result<Option<u32>, D::Error>
where
D: serde::Deserializer<'de>,
{
match Option::<i64>::deserialize(deserializer)? {
None | Some(-1) | Some(0) => Ok(None),
Some(value) if value > 0 => {
u32::try_from(value).map(Some).map_err(serde::de::Error::custom)
}
Some(value) => Err(serde::de::Error::custom(format!(
"top_k must be -1, 0, or a positive integer, got {value}"
))),
}
}
/// HF generation configs allow either one EOS id or a list of EOS ids.
#[derive(Debug, Clone, Deserialize)]
#[serde(untagged)]
@@ -294,7 +314,24 @@ where
#[cfg(test)]
mod tests {
use super::ModelConfig;
use super::{GenerationConfig, ModelConfig};
#[test]
fn generation_config_normalizes_vllm_top_k_values() {
for (input, expected) in [
(r#"{"top_k":null}"#, None),
(r#"{"top_k":-1}"#, None),
(r#"{"top_k":0}"#, None),
(r#"{"top_k":20}"#, Some(20)),
] {
let config: GenerationConfig = serde_json::from_str(input).unwrap();
assert_eq!(config.top_k, expected, "input={input}");
}
for input in [r#"{"top_k":-2}"#, r#"{"top_k":4294967296}"#] {
assert!(serde_json::from_str::<GenerationConfig>(input).is_err());
}
}
#[test]
fn model_config_detects_moe_from_named_expert_fields() {