@@ -522,30 +522,30 @@ def _assemble_patch_grid(
522522 """Assemble projected patches into a 2-D tile grid with newline columns.
523523
524524 Args:
525- patches: Projected patch features ``[num_patches, hw, n_embed ]``
525+ patches: Projected patch features ``[num_patches, hw, dim ]``
526526 where ``hw = patch_side * patch_side`` (typically 100).
527527 crop_shape: ``[width_tiles, height_tiles]`` tile layout for
528528 this image.
529529
530530 Returns:
531531 Flattened tile grid with one newline column per grid row:
532- ``[(Ht * ps) * (Wt * ps + 1), n_embed ]``.
532+ ``[(Ht * ps) * (Wt * ps + 1), dim ]``.
533533 """
534- n_embed = patches .shape [ - 1 ]
535- patch_side = int (patches . shape [ 1 ] ** 0.5 )
534+ _ , hw , dim = patches .shape
535+ patch_side = int (hw ** 0.5 )
536536 width_tiles = int (crop_shape [0 ].item ())
537537 height_tiles = int (crop_shape [1 ].item ())
538538
539- grid = patches .reshape (
540- height_tiles , width_tiles , patch_side , patch_side , n_embed
541- )
542- grid = grid .permute (0 , 2 , 1 , 3 , 4 ).reshape (
543- height_tiles * patch_side , width_tiles * patch_side , n_embed
539+ features = (
540+ patches .view (height_tiles , width_tiles , patch_side , patch_side , dim )
541+ .permute (0 , 2 , 1 , 3 , 4 )
542+ .reshape (height_tiles * patch_side , width_tiles * patch_side , dim )
544543 )
545544 newline = self .image_newline [None , None , :].expand (
546- height_tiles * patch_side , 1 , n_embed
545+ height_tiles * patch_side , 1 , dim
547546 )
548- return torch .cat ([grid , newline ], dim = 1 ).reshape (- 1 , n_embed )
547+ features = torch .cat ([features , newline ], dim = 1 )
548+ return features .reshape (- 1 , dim )
549549
550550 def _pixel_values_to_embedding (
551551 self ,
@@ -646,6 +646,9 @@ def _get_num_input_output_tokens(
646646 self ,
647647 image_spatial_crop : torch .Tensor | None = None ,
648648 ) -> tuple [int , int ]:
649+ """
650+ Init fixed spatial constants, which will be used later.
651+ """
649652 base_size = BASE_SIZE # 1024
650653 image_size = IMAGE_SIZE # 640
651654 patch_size = 16
@@ -681,9 +684,7 @@ def _get_num_input_output_tokens(
681684 return num_input_tokens , num_output_tokens
682685
683686 def get_encoder_cudagraph_config (self ):
684- # Init fixed spatial constants, which will be used later.
685687 self ._get_num_input_output_tokens ()
686-
687688 return EncoderCudaGraphConfig (
688689 modalities = ["image" ],
689690 buffer_keys = ["pixel_values" ],
@@ -700,7 +701,7 @@ def get_encoder_cudagraph_budget_range(
700701 self ,
701702 vllm_config ,
702703 ) -> tuple [int , int ]:
703- # Min budget to hold at least one global image with newline tokens.
704+ # Min budget: at least one global image with newline tokens (without patches) .
704705 min_budget = self .global_image_output_token
705706 max_budget = min (
706707 vllm_config .scheduler_config .max_num_batched_tokens ,
@@ -813,23 +814,25 @@ def encoder_cudagraph_forward(
813814 """
814815 pixel_values = values ["pixel_values" ]
815816
816- global_feat_1 = self .sam_model (pixel_values )
817- global_feat_2 = self .vision_model (pixel_values , global_feat_1 )
818- global_feat = torch .cat (
817+ global_features_1 = self .sam_model (pixel_values )
818+ global_features_2 = self .vision_model (pixel_values , global_features_1 )
819+ features = torch .cat (
819820 (
820- global_feat_2 [:, 1 :],
821- global_feat_1 .flatten (2 ).permute (0 , 2 , 1 ),
821+ global_features_2 [:, 1 :],
822+ global_features_1 .flatten (2 ).permute (0 , 2 , 1 ),
822823 ),
823824 dim = - 1 ,
824825 )
825- global_proj = self .projector (global_feat )
826+ features = self .projector (features )
826827
827- B = pixel_values .shape [0 ]
828- n_embed = global_proj .shape [- 1 ]
828+ bsz = pixel_values .shape [0 ]
829829 side = self .image_side
830- global_2d = global_proj .reshape (B , side , side , n_embed )
831- newline = self .image_newline .view (1 , 1 , 1 , n_embed ).expand (B , side , 1 , n_embed )
832- return torch .cat ([global_2d , newline ], dim = 2 ).reshape (- 1 , n_embed )
830+ dim = features .shape [- 1 ]
831+
832+ features = features .view (bsz , side , side , dim )
833+ newline = self .image_newline .view (1 , 1 , 1 , dim ).expand (bsz , side , 1 , dim )
834+ features = torch .cat ([features , newline ], dim = 2 )
835+ return features .view (- 1 , dim )
833836
834837 def encoder_eager_forward (
835838 self ,
@@ -873,36 +876,35 @@ def postprocess_encoder_output(
873876 grid via ``crop_shape``, adds ONE newline per grid row.
874877 4. Merges: local_tiled + global + ``view_seperator``.
875878 """
876- images_spatial_crop = batch_mm_kwargs ["images_spatial_crop" ]
877- n_embed = output .shape [- 1 ]
878879 bsz = len (indices )
880+ n_embed = output .shape [- 1 ]
879881
882+ images_spatial_crop = batch_mm_kwargs ["images_spatial_crop" ]
880883 is_tiled = (images_spatial_crop [:, 0 ] > 1 ) | (images_spatial_crop [:, 1 ] > 1 )
881884 num_patches = [
882885 int (np ) for np in torch .where (is_tiled , images_spatial_crop .prod (dim = - 1 ), 0 )
883886 ]
884887 total_patches = sum (num_patches )
885888
886- global_per_image = self .image_side * (self .image_side + 1 )
887- global_part = output [: bsz * global_per_image ].reshape (
888- bsz , global_per_image , n_embed
889+ global_part = output [: bsz * self .global_image_output_token ].reshape (
890+ bsz , self .global_image_output_token , n_embed
889891 )
890892
891893 # Eagerly encode all local patches in one batched call.
892894 local_flat = None
893895 if total_patches > 0 :
894896 images_crop = batch_mm_kwargs ["images_crop" ]
895- local_feat_1 = self .sam_model (images_crop )
896- local_feat_2 = self .vision_model (images_crop , local_feat_1 )
897- local_feat = torch .cat (
897+ local_features_1 = self .sam_model (images_crop )
898+ local_features_2 = self .vision_model (images_crop , local_features_1 )
899+ features = torch .cat (
898900 (
899- local_feat_2 [:, 1 :],
900- local_feat_1 .flatten (2 ).permute (0 , 2 , 1 ),
901+ local_features_2 [:, 1 :],
902+ local_features_1 .flatten (2 ).permute (0 , 2 , 1 ),
901903 ),
902904 dim = - 1 ,
903905 )
904- local_proj = self .projector (local_feat )
905- local_flat = local_proj .reshape (
906+ features = self .projector (features )
907+ local_flat = features .reshape (
906908 total_patches , self .single_patch_output_token , n_embed
907909 )
908910
@@ -912,7 +914,7 @@ def postprocess_encoder_output(
912914 single_image_output : list [torch .Tensor ] = []
913915
914916 # 1. Process local patches: assemble tile grid, add 1 newline per row.
915- if num_patch > 0 and local_flat is not None :
917+ if num_patch > 0 : # and local_flat is not None
916918 patches = local_flat [cur_patch : cur_patch + num_patch ]
917919 cur_patch += num_patch
918920 single_image_output .append (
0 commit comments