def encode_vision(self, image, test=False, return_key_values=False):
        """encode image / videos as features.

        Args:
            image (torch.Tensor): The input images.
            test (bool): Whether testing.

        Returns: tuple.
            - vision_embeds (torch.Tensor): The features of all patches. Shape: [B,T,L,C].
            - pooled_vision_embeds (torch.Tensor): The pooled features. Shape: [B,T,C].
            - vision_past_key_values (torch.Tensor): The past key values of vision transformer.

        """
        T = image.shape[1]
        use_image = True if T == 1 else False
        image = image.permute(0, 2, 1, 3, 4)  # [B, T, C, H, W] -> [B, C, T, H, W]
        vision_embeds = self.vision_encoder(image, use_image=use_image)  # [B, T, L, C]
        B, T, L, C = vision_embeds.shape
        vision_embeds = vision_embeds + self.vision_temp_embed.to(vision_embeds.dtype)
        vision_embeds = vision_embeds.reshape(B, -1, C) # [B, T*L, C]
        vision_embeds = self.vision_layernorm(vision_embeds)  # [B, T*L, C]

        vision_atts = torch.ones(
            vision_embeds.shape[:-1], dtype=torch.long, device=vision_embeds.device
        )

        query_tokens = self.query_tokens.expand(image.size(0), -1, -1)

        query_output = self.qformer.bert(
            query_embeds=query_tokens,
            encoder_hidden_states=vision_embeds,
            encoder_attention_mask=vision_atts,
            use_cache=True,
            return_dict=True,
        )

        vision_query_embeds = query_output.last_hidden_state
        vision_past_key_values = query_output.past_key_values

        if return_key_values:  # This is used in this model cap loss
            return (
                vision_embeds,
                vision_query_embeds,
                vision_past_key_values,
            )
        else:  # This is to match retrieval.py #19
            return vision_embeds, vision_query_embeds
encode image / videos as features.

        Args:
            image (torch.Tensor): The input images.
            test (bool): Whether testing.

        Returns: tuple.
            - vision_embeds (torch.Tensor): The features of all patches. Shape: [B,T,L,C].
            - pooled_vision_embeds (torch.Tensor): The pooled features. Shape: [B,T,C].
            - vision_past_key_values (torch.Tensor): The past key values of vision transformer.

        
video_chat2/models/videochat2_qformer.py