Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions crates/multimodal/src/registry/kimi_k25.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ impl ModelProcessorSpec for KimiK25VisionSpec {
}

fn matches(&self, metadata: &ModelMetadata) -> bool {
// Kimi-K3 reuses K2.5's MoonViT vision stack and `<|media_pad|>`
// placeholder (media_placeholder_token_id 163605), so it shares this
// spec.
let id = metadata.model_id.to_ascii_lowercase();
id.contains("kimi") && id.contains("k2")
id.contains("kimi") && (id.contains("k2") || id.contains("k3"))
|| metadata
.config_model_type()
.is_some_and(|mt| mt == "kimi_k25")
.is_some_and(|mt| mt == "kimi_k25" || mt == "kimi_k3")
Comment on lines 34 to +38

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve authored media order for K3 prompts

For a K3 request whose content interleaves text and images, routing it through the K2.5 spec also inherits ModelProcessorSpec's MediaFirst ordering. The gRPC preparation path consequently collapses the content array into all <|media_pad|> anchors followed by all text before the K3 renderer sees it, even though push_content in the new XTML renderer processes parts positionally; prompts such as text-image-text therefore change meaning. K3 needs an authored-order override, likely through a distinct spec rather than sharing this one unchanged.

Useful? React with 👍 / 👎.

}

fn placeholder_token(&self, _metadata: &ModelMetadata) -> RegistryResult<String> {
Expand Down Expand Up @@ -120,6 +123,34 @@ mod tests {
assert_eq!(spec.name(), "kimi_k25");
}

#[test]
fn kimi_k3_matches_model_id_and_model_type() {
let tokenizer = TestTokenizer::new(&[("<|media_pad|>", 163605)]);
// Match by model_id containing kimi + k3.
let config = json!({
"model_type": "kimi_k3",
"media_placeholder_token_id": 163605
});
let metadata = ModelMetadata {
model_id: "moonshotai/Kimi-K3",
tokenizer: &tokenizer,
config: &config,
};
let registry = ModelRegistry::new();
let spec = registry
.lookup(&metadata)
.expect("kimi_k3 -> kimi_k25 spec");
assert_eq!(spec.name(), "kimi_k25");

// Also match by model_type alone (id without a k3 hint).
let metadata_by_type = ModelMetadata {
model_id: "internal/checkpoint-final",
tokenizer: &tokenizer,
config: &config,
};
assert!(registry.lookup(&metadata_by_type).is_some());
}

#[test]
fn kimi_k25_prompt_replacements() {
let tokenizer = TestTokenizer::new(&[("<|media_pad|>", 163605)]);
Expand Down
10 changes: 9 additions & 1 deletion crates/multimodal/src/vision/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ impl VisionProcessorRegistry {
Box::new(super::processors::Llama4VisionProcessor::new()),
);

// Register Kimi-K2.5 Vision
// Register Kimi-K2.5 Vision (also used by Kimi-K3, same MoonViT stack)
registry.register(
"kimi-k2",
Box::new(super::processors::KimiK25Processor::new()),
Expand All @@ -334,6 +334,14 @@ impl VisionProcessorRegistry {
"kimi_k2",
Box::new(super::processors::KimiK25Processor::new()),
);
registry.register(
"kimi-k3",
Box::new(super::processors::KimiK25Processor::new()),
);
registry.register(
"kimi_k3",
Box::new(super::processors::KimiK25Processor::new()),
);

registry
}
Expand Down
1 change: 1 addition & 0 deletions crates/reasoning_parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ categories = ["parsing", "text-processing"]

[dependencies]
parking_lot = { workspace = true }
regex = "1.12"
thiserror.workspace = true
tokio = { workspace = true, features = ["sync"] }

Expand Down
20 changes: 19 additions & 1 deletion crates/reasoning_parser/src/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use parking_lot::RwLock;
use crate::{
parsers::{
BaseReasoningParser, CohereCmdParser, DeepSeekR1Parser, Glm45Parser, InklingParser,
KimiParser, MiniMaxParser, NanoV3Parser, PassthroughParser, Qwen3Parser,
KimiK3Parser, KimiParser, MiniMaxParser, NanoV3Parser, PassthroughParser, Qwen3Parser,
QwenThinkingParser, Step3Parser,
},
traits::{ParserConfig, ReasoningParser, DEFAULT_MAX_BUFFER_SIZE},
Expand Down Expand Up @@ -184,6 +184,9 @@ impl ParserFactory {
Box::new(BaseReasoningParser::new(config).with_model_type("kimi_thinking".to_string()))
});

// Kimi K3 XTML think channel (structural <|open|>/<|close|>/<|sep|> tokens).
registry.register_parser("kimi_k3", || Box::new(KimiK3Parser::new()));

registry.register_pattern("deepseek-r1", "deepseek_r1");
registry.register_pattern("deepseek-v3.1", "deepseek_v31");
registry.register_pattern("deepseek-v3-1", "deepseek_v31");
Expand All @@ -196,6 +199,10 @@ impl ParserFactory {
registry.register_pattern("glm-5", "glm45"); // GLM-5.x reuse glm45 reasoning format
registry.register_pattern("kimi-k2-thinking", "kimi_thinking");
registry.register_pattern("kimi-k2.5", "kimi_k25");
// K3 patterns must precede the generic "kimi" pattern below, since
// "kimi-k3".contains("kimi") and first-substring-match wins.
registry.register_pattern("kimi-k3", "kimi_k3");
registry.register_pattern("kimi_k3", "kimi_k3");
registry.register_pattern("kimi", "kimi"); // legacy: Kimi-K2-Instruct with unicode tokens
registry.register_pattern("step3", "step3");
registry.register_pattern("minimax", "minimax");
Expand Down Expand Up @@ -280,6 +287,17 @@ mod tests {
assert_eq!(parser.model_type(), "kimi");
}

#[test]
fn test_factory_creates_kimi_k3() {
let factory = ParserFactory::new();
// Kimi-K3 ids must resolve to the dedicated kimi_k3 parser, not the
// legacy "kimi" parser (whose pattern is a substring of "kimi-k3").
assert_eq!(factory.create("moonshotai/Kimi-K3").model_type(), "kimi_k3");
assert_eq!(factory.create("Kimi_K3").model_type(), "kimi_k3");
// The legacy kimi pattern still resolves plain Kimi-K2 ids.
assert_eq!(factory.create("kimi-chat").model_type(), "kimi");
}

#[test]
fn test_factory_creates_inkling() {
let factory = ParserFactory::new();
Expand Down
5 changes: 3 additions & 2 deletions crates/reasoning_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ pub mod traits;

pub use factory::{ParserFactory, ParserRegistry};
pub use parsers::{
BaseReasoningParser, CohereCmdParser, DeepSeekR1Parser, Glm45Parser, InklingParser, KimiParser,
MiniMaxParser, NanoV3Parser, PassthroughParser, Qwen3Parser, QwenThinkingParser, Step3Parser,
BaseReasoningParser, CohereCmdParser, DeepSeekR1Parser, Glm45Parser, InklingParser,
KimiK3Parser, KimiParser, MiniMaxParser, NanoV3Parser, PassthroughParser, Qwen3Parser,
QwenThinkingParser, Step3Parser,
};
pub use traits::{
ParseError, ParserConfig, ParserResult, ReasoningParser, DEFAULT_MAX_BUFFER_SIZE,
Expand Down
Loading
Loading