Skip to content

Commit f384e0f

Browse files
committed
HuggingFaceM4/Idefics3-8B-Llama3 crash fix
Signed-off-by: Wang, Yi A <[email protected]>
1 parent 3752143 commit f384e0f

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
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: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -646,11 +646,10 @@ 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-
{
651+
// resize image to max_longest_edge_for_image_resize and keep aspect ratio
652+
let (height, width) = {
654653
let aspect_ratio = height as f32 / width as f32;
655654
if height > width {
656655
(
@@ -663,8 +662,23 @@ fn image_tokens(
663662
max_longest_edge_for_image_resize,
664663
)
665664
}
666-
} else {
667-
(height, width)
665+
};
666+
667+
let (height, width) = {
668+
let aspect_ratio = height as f32 / width as f32;
669+
if height >= width && height > max_image_size {
670+
(
671+
max_image_size,
672+
(max_image_size as f32 / aspect_ratio) as usize,
673+
)
674+
} else if width > height && width > max_image_size {
675+
(
676+
(max_image_size as f32 * aspect_ratio) as usize,
677+
max_image_size,
678+
)
679+
} else {
680+
(height, width)
681+
}
668682
};
669683

670684
let image_seq_len = config.get_number_of_features();

0 commit comments

Comments
 (0)