From 8ef4c9eee915444e5124a7ddb191043bee9c4c4f Mon Sep 17 00:00:00 2001 From: Neo <23699296+darkSuperman@users.noreply.github.com> Date: Thu, 16 Apr 2026 02:30:10 +0000 Subject: [PATCH 1/3] fix(sam3): prevent unwrap panic caused by LRU cache eviction --- src/models/vlm/sam3_image/impl.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/models/vlm/sam3_image/impl.rs b/src/models/vlm/sam3_image/impl.rs index 6881a192..fe585d28 100644 --- a/src/models/vlm/sam3_image/impl.rs +++ b/src/models/vlm/sam3_image/impl.rs @@ -352,8 +352,21 @@ impl Sam3Image { uncached.push(p.text.as_str()); } } + + let unique_prompts_count = seen.len(); + if unique_prompts_count > self.text_cache.cap().get() { + // If the number of unique prompts in the current batch exceeds the cache capacity, force an expansion. + self.text_cache.resize(NonZeroUsize::new(unique_prompts_count).unwrap()); + } + if !uncached.is_empty() { - for (text, feat) in uncached.iter().zip(self.encode_texts(engines, &uncached)?) { + let encoded_feats = self.encode_texts(engines, &uncached)?; + + if encoded_feats.len() != uncached.len() { + bail!("Encoded features length mismatch: expected {}, got {}", uncached.len(), encoded_feats.len()); + } + + for (text, feat) in uncached.iter().zip(encoded_feats) { self.text_cache.put(text.to_string(), feat); } } From 2c66f153ecfb2a366f7fcb3e233783504fc25f4a Mon Sep 17 00:00:00 2001 From: Neo <23699296+darkSuperman@users.noreply.github.com> Date: Thu, 16 Apr 2026 11:04:08 +0800 Subject: [PATCH 2/3] fix: use get instead of contains to promote LRU items and prevent eviction --- src/models/vlm/sam3_image/impl.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/models/vlm/sam3_image/impl.rs b/src/models/vlm/sam3_image/impl.rs index fe585d28..a0398ddb 100644 --- a/src/models/vlm/sam3_image/impl.rs +++ b/src/models/vlm/sam3_image/impl.rs @@ -348,8 +348,10 @@ impl Sam3Image { let mut uncached = Vec::new(); let mut seen = HashSet::new(); for p in prompts { - if seen.insert(&p.text) && !self.text_cache.contains(&p.text) { - uncached.push(p.text.as_str()); + if seen.insert(&p.text) { + if self.text_cache.get(&p.text).is_none() { + uncached.push(p.text.as_str()); + } } } From 29319b521cc70ff789328e756ffd7f0c6ca60768 Mon Sep 17 00:00:00 2001 From: jamjamjon Date: Sat, 30 May 2026 11:15:51 +0800 Subject: [PATCH 3/3] fmt & clippy --- src/models/vlm/sam3_image/impl.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/models/vlm/sam3_image/impl.rs b/src/models/vlm/sam3_image/impl.rs index a0398ddb..5f42bd89 100644 --- a/src/models/vlm/sam3_image/impl.rs +++ b/src/models/vlm/sam3_image/impl.rs @@ -348,24 +348,27 @@ impl Sam3Image { let mut uncached = Vec::new(); let mut seen = HashSet::new(); for p in prompts { - if seen.insert(&p.text) { - if self.text_cache.get(&p.text).is_none() { - uncached.push(p.text.as_str()); - } + if seen.insert(&p.text) && self.text_cache.get(&p.text).is_none() { + uncached.push(p.text.as_str()); } } let unique_prompts_count = seen.len(); if unique_prompts_count > self.text_cache.cap().get() { // If the number of unique prompts in the current batch exceeds the cache capacity, force an expansion. - self.text_cache.resize(NonZeroUsize::new(unique_prompts_count).unwrap()); + self.text_cache + .resize(NonZeroUsize::new(unique_prompts_count).unwrap()); } if !uncached.is_empty() { let encoded_feats = self.encode_texts(engines, &uncached)?; if encoded_feats.len() != uncached.len() { - bail!("Encoded features length mismatch: expected {}, got {}", uncached.len(), encoded_feats.len()); + bail!( + "Encoded features length mismatch: expected {}, got {}", + uncached.len(), + encoded_feats.len() + ); } for (text, feat) in uncached.iter().zip(encoded_feats) {