@@ -646,25 +646,20 @@ fn image_tokens(
646
646
const GLOBAL_IMG : & str = "<global-img>" ;
647
647
648
648
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 ( ) ;
649
650
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 )
668
663
} ;
669
664
670
665
let image_seq_len = config. get_number_of_features ( ) ;
0 commit comments