[Rust Frontend] Migrate gemma4 to unified parser (#46602)
Signed-off-by: Bugen Zhao <i@bugenzhao.com>
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use vllm_parser::tool::{
|
||||
Result, StructuralTagModel, Tool, ToolParser, ToolParserError, ToolParserOutput,
|
||||
};
|
||||
use vllm_parser::unified::{
|
||||
UnifiedParser, UnifiedParserError, UnifiedParserEvent, UnifiedParserOutput,
|
||||
};
|
||||
use vllm_tokenizer::Tokenizer;
|
||||
|
||||
/// Tokenizer stub used by unified-parser benchmarks.
|
||||
struct BenchTokenizer;
|
||||
|
||||
impl Tokenizer for BenchTokenizer {
|
||||
fn encode(&self, text: &str, _add_special_tokens: bool) -> vllm_tokenizer::Result<Vec<u32>> {
|
||||
Ok(text.chars().map(|_| u32::MAX).collect())
|
||||
}
|
||||
|
||||
fn decode(
|
||||
&self,
|
||||
token_ids: &[u32],
|
||||
_skip_special_tokens: bool,
|
||||
) -> vllm_tokenizer::Result<String> {
|
||||
Ok("\u{FFFD}".repeat(token_ids.len()))
|
||||
}
|
||||
|
||||
fn token_to_id(&self, _token: &str) -> Option<u32> {
|
||||
Some(u32::MAX)
|
||||
}
|
||||
}
|
||||
|
||||
/// Bench-only adapter that exposes a unified parser through the tool-parser
|
||||
/// benchmark harness.
|
||||
///
|
||||
/// Returns error if the unified parser produces reasoning events.
|
||||
pub struct UnifiedToolParserAdapter<T> {
|
||||
inner: Box<dyn UnifiedParser>,
|
||||
_marker: std::marker::PhantomData<T>,
|
||||
}
|
||||
|
||||
fn map_unified_error(error: UnifiedParserError) -> ToolParserError {
|
||||
ToolParserError::ParsingFailed {
|
||||
message: format!("unified parser failed: {error}"),
|
||||
}
|
||||
}
|
||||
|
||||
fn append_unified_output(
|
||||
output: UnifiedParserOutput,
|
||||
tool_output: &mut ToolParserOutput,
|
||||
) -> Result<()> {
|
||||
for event in output.events {
|
||||
match event {
|
||||
UnifiedParserEvent::Text(text) => tool_output.push_text(text),
|
||||
UnifiedParserEvent::ToolCall(call) => tool_output.push_call(call),
|
||||
UnifiedParserEvent::Reasoning(_) => {
|
||||
return Err(ToolParserError::ParsingFailed {
|
||||
message: "unified parser emitted reasoning in tool-parser adapter".to_string(),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
impl<T: UnifiedParser> ToolParser for UnifiedToolParserAdapter<T> {
|
||||
fn create(tools: &[Tool]) -> Result<Box<dyn ToolParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
let inner = T::create(tools, Arc::new(BenchTokenizer)).map_err(map_unified_error)?;
|
||||
Ok(Box::new(Self {
|
||||
inner,
|
||||
_marker: std::marker::PhantomData,
|
||||
}))
|
||||
}
|
||||
|
||||
fn preserve_special_tokens(&self) -> bool {
|
||||
self.inner.preserve_special_tokens()
|
||||
}
|
||||
|
||||
fn structural_tag_model(&self) -> Option<StructuralTagModel> {
|
||||
self.inner.structural_tag_model()
|
||||
}
|
||||
|
||||
fn tool_call_id(&self, tool_index: usize) -> Option<&str> {
|
||||
self.inner.tool_call_id(tool_index)
|
||||
}
|
||||
|
||||
fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> {
|
||||
let mut unified_output = UnifiedParserOutput::default();
|
||||
let result = self.inner.parse_into(chunk, &mut unified_output).map_err(map_unified_error);
|
||||
append_unified_output(unified_output, output)?;
|
||||
result
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> Result<ToolParserOutput> {
|
||||
let unified_output = self.inner.finish().map_err(map_unified_error)?;
|
||||
let mut output = ToolParserOutput::default();
|
||||
append_unified_output(unified_output, &mut output)?;
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
fn reset(&mut self) -> String {
|
||||
self.inner.reset()
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,9 @@
|
||||
// This module is shared by multiple benchmark targets.
|
||||
// There could be false positives for unused code or imports, and fixing them would lead to some other benchmarks failing to compile.
|
||||
#![allow(dead_code)]
|
||||
#![allow(unused_imports)]
|
||||
|
||||
mod adapter;
|
||||
|
||||
use futures::FutureExt as _;
|
||||
use openai_protocol::common::{Function as OpenAiFunction, Tool as OpenAiTool};
|
||||
@@ -6,6 +11,8 @@ use tool_parser::traits::ToolParser as ExternalToolParser;
|
||||
use vllm_parser::tool::test_utils::collect_stream;
|
||||
use vllm_parser::tool::{Tool, ToolParser};
|
||||
|
||||
pub(super) use adapter::UnifiedToolParserAdapter;
|
||||
|
||||
pub(super) fn openai_tools(tools: &[Tool]) -> Vec<OpenAiTool> {
|
||||
tools
|
||||
.iter()
|
||||
|
||||
Reference in New Issue
Block a user