class_docstr: Implementation of `Grounding DINO: Marrying DINO with Grounded Pre-
  Training for Open-Set Object Detection.
  
  <https://arxiv.org/abs/2303.05499>`_
  
  Code is modified from the `official github repo
  <https://github.com/IDEA-Research/GroundingDINO>`_.
class_docstr_tok: ["Implementation","of","`","Grounding","DINO",":","Marrying","DINO","with","Grounded","Pre-","Training","for","Open-Set","Object","Detection",".","<","https",":","\/\/arxiv.org\/abs\/2303.05499",">","`","_","Code","is","modified","from","the","`","official","github","repo","<","https",":","\/\/github.com\/IDEA-Research\/GroundingDINO",">","`","_","."]
docstr: Get the tokens positive and prompts for the caption.
  
  Args:
      original_caption (str): The original caption, e.g. 'bench . car .'
      custom_entities (bool, optional): Whether to use custom entities.
          If ``True``, the ``original_caption`` should be a list of
          strings, each of which is a word. Defaults to False.
  
  Returns:
      Tuple[dict, str, dict, str]: The dict is a mapping from each entity
      id, which is numbered from 1, to its positive token id.
      The str represents the prompts.
docstr_tok: ["Get","the","tokens","positive","and","prompts","for","the","caption",".","Args",":","original_caption","(","str",")",":","The","original","caption",",","e.g",".","'bench",".","car",".","'","custom_entities","(","bool",",","optional",")",":","Whether","to","use","custom","entities",".","If","``","True","``",",","the","``","original_caption","``","should","be","a","list","of","strings",",","each","of","which","is","a","word",".","Defaults","to","False",".","Returns",":","Tuple","[","dict",",","str",",","dict",",","str","]",":","The","dict","is","a","mapping","from","each","entity","id",",","which","is","numbered","from","1",",","to","its","positive","token","id",".","The","str","represents","the","prompts","."]
code: def get_tokens_positive_and_prompts(
      self,
      original_caption: Union[str, list, tuple],
      custom_entities: bool = False,
      enhanced_text_prompt: Optional[ConfigType] = None,
      tokens_positive: Optional[list] = None,
  ) -> Tuple[dict, str, Tensor, list]:
      """Get the tokens positive and prompts for the caption.
  
      Args:
          original_caption (str): The original caption, e.g. 'bench . car .'
          custom_entities (bool, optional): Whether to use custom entities.
              If ``True``, the ``original_caption`` should be a list of
              strings, each of which is a word. Defaults to False.
  
      Returns:
          Tuple[dict, str, dict, str]: The dict is a mapping from each entity
          id, which is numbered from 1, to its positive token id.
          The str represents the prompts.
      """
      if tokens_positive is not None:
          if tokens_positive == -1:
              if not original_caption.endswith("."):
                  original_caption = (
                      original_caption + self._special_tokens
                  )
              return None, original_caption, None, original_caption
          else:
              if not original_caption.endswith("."):
                  original_caption = (
                      original_caption + self._special_tokens
                  )
              tokenized = self.language_model.tokenizer(
                  [original_caption],
                  padding="max_length"
                  if self.language_model.pad_to_max
                  else "longest",
                  return_tensors="pt",
              )
              (
                  positive_map_label_to_token,
                  positive_map,
              ) = self.get_positive_map(tokenized, tokens_positive)
  
              entities = []
              for token_positive in tokens_positive:
                  instance_entities = []
                  for t in token_positive:
                      instance_entities.append(
                          original_caption[t[0] : t[1]]
                      )
                  entities.append(" / ".join(instance_entities))
              return (
                  positive_map_label_to_token,
                  original_caption,
                  positive_map,
                  entities,
              )
  
      chunked_size = self.test_cfg.get("chunked_size", -1)
      if not self.training and chunked_size > 0:
          assert (
              isinstance(original_caption, (list, tuple))
              or custom_entities is True
          )
          all_output = self.get_tokens_positive_and_prompts_chunked(
              original_caption, enhanced_text_prompt
          )
          (
              positive_map_label_to_token,
              caption_string,
              positive_map,
              entities,
          ) = all_output
      else:
          (
              tokenized,
              caption_string,
              tokens_positive,
              entities,
          ) = self.get_tokens_and_prompts(
              original_caption, custom_entities, enhanced_text_prompt
          )
          (
              positive_map_label_to_token,
              positive_map,
          ) = self.get_positive_map(tokenized, tokens_positive)
      return (
          positive_map_label_to_token,
          caption_string,
          positive_map,
          entities,
      )
  
imports: import copy
  import re
  import warnings
  from typing import Dict, Optional, Tuple, Union
  import torch
  import torch.nn
  from torch import Tensor
  from mmdet.registry import MODELS
  from mmdet.structures import OptSampleList, SampleList
  from mmdet.utils import ConfigType
  from ..layers import SinePositionalEncoding
  from ..layers.transformer.grounding_dino_layers import GroundingDinoTransformerDecoder, GroundingDinoTransformerEncoder
  from .dino import DINO
  from .glip import create_positive_map, create_positive_map_label_to_token, run_ner
label_desc: Use image node_id 8 for calling the GroundingDINO obj's underlying member method code with example usage: obj.get_tokens_positive_and_prompts(original_caption, custom_entities, enhanced_text_prompt, tokens_positive) and returns: positive_map_label_to_token,  caption_string,  positive_map,  entities, None,  original_caption,  None,  original_caption, positive_map_label_to_token,  original_caption,  positive_map,  entities