code: def token_generation(self, text_q, vis_embed, long_video=False):
      ctx_embed = self.get_model().vlm_att_key_projector(vis_embed)
      # Key part 1: calculate context-related embedding
      ctx_embed = text_q @ ctx_embed.transpose(-1, -2)
      ctx_embed = ctx_embed / (vis_embed.shape[-1] ** 0.5)
      if not long_video:
          ctx_embed = (ctx_embed.softmax(-1) @ vis_embed).mean(1)
      else:
          block_size = 64
          outputs = []
          ctx_score = ctx_embed.softmax(-1)
          for L in range(0, len(ctx_score), block_size):
              R = L + block_size
              sub_embed = (ctx_score[L:R] @ vis_embed[L:R]).mean(1)
              outputs.append(sub_embed)
          ctx_embed = torch.cat(outputs)
          torch.cuda.empty_cache()
      ctx_embed = self.get_model().vlm_att_val_projector(
          ctx_embed[:, None]
      )
  
      # Key part 2: calculate visual embedding
      if self.config.compress_type is not None:
          if "grid" in self.config.compress_type:
              grid_size = int(
                  self.config.compress_type.split("grid:")[-1]
              )
              cur_shape = int(vis_embed.shape[1] ** 0.5)
              assert (
                  grid_size > 1
              ), f"Grid size should be larger than 1, but got {grid_size}"
              vis_embed = vis_embed.reshape(
                  vis_embed.shape[0], cur_shape, cur_shape, -1
              )
              grid_stride = cur_shape // grid_size
              vis_embed = F.avg_pool2d(
                  vis_embed.permute(0, 3, 1, 2),
                  padding=0,
                  kernel_size=grid_stride,
                  stride=grid_stride,
              )
  
              vis_embed = vis_embed.permute(0, 2, 3, 1).flatten(1, 2)
          elif "mean" in self.config.compress_type:
              vis_embed = vis_embed.mean(dim=1, keepdim=True)
  
      # concat token in shape (B, n+1, C)
      vis_embed = self.get_model().mm_projector(vis_embed)
      final_token = torch.cat([ctx_embed, vis_embed], dim=1)
      return final_token
  
imports: from abc import ABC, abstractmethod
  import os
  import json
  import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  from transformers import BertTokenizer
  from transformers.models.bert.modeling_bert import BertLMHeadModel
  from .qformer import BertConfig
  from .qformer import BertLMHeadModel
  from .multimodal_encoder.builder import build_vision_tower
  from .multimodal_projector.builder import build_vision_projector
  from llamavid.constants import IGNORE_INDEX, IMAGE_TOKEN_INDEX, DEFAULT_IMAGE_PATCH_TOKEN, DEFAULT_IM_START_TOKEN, DEFAULT_IM_END_TOKEN
label_desc: Use image node_id 5 for calling the LLaMAVIDMetaForCausalLM obj's underlying member method code with example usage: obj.token_generation(text_q, vis_embed, long_video) and returns: final_token