code: def generate_masks_with_special_tokens_and_transfer_map(
      tokenized, special_tokens_list
  ):
      """Generate attention mask between each pair of special tokens.
  
      Only token pairs in between two special tokens are attended to
      and thus the attention mask for these pairs is positive.
  
      Args:
          input_ids (torch.Tensor): input ids. Shape: [bs, num_token]
          special_tokens_mask (list): special tokens mask.
  
      Returns:
          Tuple(Tensor, Tensor):
          - attention_mask is the attention mask between each tokens.
            Only token pairs in between two special tokens are positive.
            Shape: [bs, num_token, num_token].
          - position_ids is the position id of tokens within each valid sentence.
            The id starts from 0 whenenver a special token is encountered.
            Shape: [bs, num_token]
      """
      input_ids = tokenized["input_ids"]
      bs, num_token = input_ids.shape
      # special_tokens_mask:
      # bs, num_token. 1 for special tokens. 0 for normal tokens
      special_tokens_mask = torch.zeros(
          (bs, num_token), device=input_ids.device
      ).bool()
  
      for special_token in special_tokens_list:
          special_tokens_mask |= input_ids == special_token
  
      # idxs: each row is a list of indices of special tokens
      idxs = torch.nonzero(special_tokens_mask)
  
      # generate attention mask and positional ids
      attention_mask = (
          torch.eye(num_token, device=input_ids.device)
          .bool()
          .unsqueeze(0)
          .repeat(bs, 1, 1)
      )
      position_ids = torch.zeros(
          (bs, num_token), device=input_ids.device
      )
      previous_col = 0
      for i in range(idxs.shape[0]):
          row, col = idxs[i]
          if (col == 0) or (col == num_token - 1):
              attention_mask[row, col, col] = True
              position_ids[row, col] = 0
          else:
              attention_mask[
                  row,
                  previous_col + 1 : col + 1,
                  previous_col + 1 : col + 1,
              ] = True
              position_ids[
                  row, previous_col + 1 : col + 1
              ] = torch.arange(
                  0, col - previous_col, device=input_ids.device
              )
          previous_col = col
  
      return attention_mask, position_ids.to(torch.long)
  
file_path: mmdetection/mmdet/models/language_models/bert.py
imports: from collections import OrderedDict
  from typing import Sequence
  import torch
  from mmengine.model import BaseModel
  from torch import nn
  from mmdet.registry import MODELS
label_desc: Use image node_id 1 for calling a global function with example usage: generate_masks_with_special_tokens_and_transfer_map(tokenized, special_tokens_list) and returns: attention_mask,  position_ids