Preserve image-aware sparse QSA for Qwen vision

This commit is contained in:
Georg Bauer committed 2026-10-04 14:03:12 +02:00
1 parent a8cad88100
commit dccf81971f
26 files changed
+1215 -44

No files matched your search

+23 -3
View File
@@ -207,12 +207,27 @@ pub(super) struct Identity {
pub(super) start: usize,
pub(super) end: usize,
pub(super) digest: [u8; 32],
/// Missing on checkpoints written before image-aware sparse QSA.
#[serde(default)]
pub(super) policy: u32,
}
pub(super) fn cache_policy() -> u32 {
u32::from(std::env::var("MTPLX_QWEN4_VISION_QSA").as_deref() != Ok("0"))
}
pub(super) fn compatible(images: &[Identity]) -> bool {
images.iter().all(|image| image.policy == cache_policy())
}
pub(super) fn same_prefix(a: &[Identity], b: &[Identity], tokens: usize) -> bool {
a.iter()
.filter(|i| i.start < tokens)
.eq(b.iter().filter(|i| i.start < tokens))
// Image pads do not identify partial image embeddings. MTPLX only restores
// before an image or after its complete span, never into the middle.
!b.iter()
.any(|image| image.start < tokens && tokens < image.end)
&& a.iter()
.filter(|i| i.start < tokens)
.eq(b.iter().filter(|i| i.start < tokens))
}
#[test]
@@ -271,14 +286,19 @@ fn qwen_image_cache_keys_distinguish_pixels_and_preserve_prior_text() {
start: 10,
end: 14,
digest: [1; 32],
policy: cache_policy(),
}];
let changed = vec![Identity {
start: 10,
end: 14,
digest: [2; 32],
policy: cache_policy(),
}];
assert!(same_prefix(&old, &old, 20));
assert!(!same_prefix(&old, &changed, 20));
assert!(same_prefix(&old, &changed, 10));
assert!(!same_prefix(&old, &[], 20));
assert!(!same_prefix(&old, &old, 11));
assert!(!same_prefix(&old, &old, 13));
assert!(same_prefix(&old, &old, 14));
}