Skip to content

Commit d618424

Browse files
authored
HuggingFaceM4/Idefics3-8B-Llama3 crash fix (#3267)
Signed-off-by: Wang, Yi A <[email protected]>
1 parent c5e6f9a commit d618424

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

router/src/config.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,10 @@ impl Idefics3 {
265265
pub fn get_max_longest_edge_for_image_resize(&self) -> usize {
266266
1456
267267
}
268+
269+
pub fn get_max_image_size(&self) -> usize {
270+
4096
271+
}
268272
}
269273

270274
#[derive(Clone, Debug, Serialize, Deserialize)]

router/src/validation.rs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -646,25 +646,20 @@ fn image_tokens(
646646
const GLOBAL_IMG: &str = "<global-img>";
647647

648648
let max_longest_edge_for_image_resize = config.get_max_longest_edge_for_image_resize();
649+
let max_image_size = config.get_max_image_size();
649650

650-
// resize image if it is larger than max_longest_edge_for_image_resize keeping aspect ratio
651-
let (height, width) = if height > max_longest_edge_for_image_resize
652-
|| width > max_longest_edge_for_image_resize
653-
{
654-
let aspect_ratio = height as f32 / width as f32;
655-
if height > width {
656-
(
657-
max_longest_edge_for_image_resize,
658-
(max_longest_edge_for_image_resize as f32 / aspect_ratio) as usize,
659-
)
660-
} else {
661-
(
662-
(max_longest_edge_for_image_resize as f32 * aspect_ratio) as usize,
663-
max_longest_edge_for_image_resize,
664-
)
665-
}
666-
} else {
667-
(height, width)
651+
let (height, width) = {
652+
let h = height as f32;
653+
let w = width as f32;
654+
655+
// First resize to max_longest_edge (always scale to this size)
656+
let scale1 = max_longest_edge_for_image_resize as f32 / h.max(w);
657+
let (h, w) = (h * scale1, w * scale1);
658+
659+
// Ensure we dont exceed max_size (only scale down)
660+
let scale2 = (max_image_size as f32 / h.max(w)).min(1.0);
661+
662+
((h * scale2) as usize, (w * scale2) as usize)
668663
};
669664

670665
let image_seq_len = config.get_number_of_features();

0 commit comments

Comments
 (0)