From f384e0f30871ce159bf8078f0a96ebc4ff0c26f9 Mon Sep 17 00:00:00 2001 From: "Wang, Yi A" Date: Fri, 13 Jun 2025 00:35:39 -0700 Subject: [PATCH 1/2] HuggingFaceM4/Idefics3-8B-Llama3 crash fix Signed-off-by: Wang, Yi A --- router/src/config.rs | 4 ++++ router/src/validation.rs | 26 ++++++++++++++++++++------ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/router/src/config.rs b/router/src/config.rs index 7c595e2d9f7..a1a3c7f6ff4 100644 --- a/router/src/config.rs +++ b/router/src/config.rs @@ -265,6 +265,10 @@ impl Idefics3 { pub fn get_max_longest_edge_for_image_resize(&self) -> usize { 1456 } + + pub fn get_max_image_size(&self) -> usize { + 4096 + } } #[derive(Clone, Debug, Serialize, Deserialize)] diff --git a/router/src/validation.rs b/router/src/validation.rs index 28c7f2f8c4f..42e438975fd 100644 --- a/router/src/validation.rs +++ b/router/src/validation.rs @@ -646,11 +646,10 @@ fn image_tokens( const GLOBAL_IMG: &str = ""; let max_longest_edge_for_image_resize = config.get_max_longest_edge_for_image_resize(); + let max_image_size = config.get_max_image_size(); - // resize image if it is larger than max_longest_edge_for_image_resize keeping aspect ratio - let (height, width) = if height > max_longest_edge_for_image_resize - || width > max_longest_edge_for_image_resize - { + // resize image to max_longest_edge_for_image_resize and keep aspect ratio + let (height, width) = { let aspect_ratio = height as f32 / width as f32; if height > width { ( @@ -663,8 +662,23 @@ fn image_tokens( max_longest_edge_for_image_resize, ) } - } else { - (height, width) + }; + + let (height, width) = { + let aspect_ratio = height as f32 / width as f32; + if height >= width && height > max_image_size { + ( + max_image_size, + (max_image_size as f32 / aspect_ratio) as usize, + ) + } else if width > height && width > max_image_size { + ( + (max_image_size as f32 * aspect_ratio) as usize, + max_image_size, + ) + } else { + (height, width) + } }; let image_seq_len = config.get_number_of_features(); From dc132b3a4de97227ab99794273afa1056e339f01 Mon Sep 17 00:00:00 2001 From: "Wang, Yi A" Date: Tue, 19 Aug 2025 19:52:19 -0700 Subject: [PATCH 2/2] refine code Signed-off-by: Wang, Yi A --- router/src/validation.rs | 39 ++++++++++----------------------------- 1 file changed, 10 insertions(+), 29 deletions(-) diff --git a/router/src/validation.rs b/router/src/validation.rs index 42e438975fd..7717f373e4f 100644 --- a/router/src/validation.rs +++ b/router/src/validation.rs @@ -648,37 +648,18 @@ fn image_tokens( let max_longest_edge_for_image_resize = config.get_max_longest_edge_for_image_resize(); let max_image_size = config.get_max_image_size(); - // resize image to max_longest_edge_for_image_resize and keep aspect ratio let (height, width) = { - let aspect_ratio = height as f32 / width as f32; - if height > width { - ( - max_longest_edge_for_image_resize, - (max_longest_edge_for_image_resize as f32 / aspect_ratio) as usize, - ) - } else { - ( - (max_longest_edge_for_image_resize as f32 * aspect_ratio) as usize, - max_longest_edge_for_image_resize, - ) - } - }; + let h = height as f32; + let w = width as f32; - let (height, width) = { - let aspect_ratio = height as f32 / width as f32; - if height >= width && height > max_image_size { - ( - max_image_size, - (max_image_size as f32 / aspect_ratio) as usize, - ) - } else if width > height && width > max_image_size { - ( - (max_image_size as f32 * aspect_ratio) as usize, - max_image_size, - ) - } else { - (height, width) - } + // First resize to max_longest_edge (always scale to this size) + let scale1 = max_longest_edge_for_image_resize as f32 / h.max(w); + let (h, w) = (h * scale1, w * scale1); + + // Ensure we dont exceed max_size (only scale down) + let scale2 = (max_image_size as f32 / h.max(w)).min(1.0); + + ((h * scale2) as usize, (w * scale2) as usize) }; let image_seq_len = config.get_number_of_features();