docstr: Inputs:
      labels (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*, *training*):
          Labels for computing the masked language modeling loss. 
          Indices should either be in `[0, 1, 2]` (3 basic actions) or -1 (see `input_ids` docstring). 
          labels with <0 values are ignored (masked).
      lr_pair_flag (`torch.LongTensor` of shape `(batch_size, sequence_length, number_of_l_brackets)`, *optional*, *training*):
          A flag tensor. Indicating whether a pair of tokens are paired *left-right* brackts. 1 for paired, 0 for not.
      rr_pair_flag (`torch.LongTensor` of shape `(batch_size, sequence_length, number_of_r_brackets)`, *optional*, *training*):
          A flag tensor. Indicating whether a pair of tokens are paired *right-right* brackts. 1 for paired, 0 for not.
      full_decoder_input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*, *inference*):
          Decoded token ids until now. Keeping track of how many tokens have been copied from
          the input, so we know which token to copy now.
      full_hidden_states (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*, *inference*):
          Hidden states of the decoded tokens until now. 
      decoder_pairing (`List[torch.LongTensor]` each element is shape `(batch_size, 1)`, *optional*, *inference*):
          A list of tensors. Each tensor is a tensor of shape `(batch_size, 1)`, decoded pairing actions until now.
      decoder_linking (`List[torch.LongTensor]` each element is shape `(batch_size, number_of_r)`, *optional*, *inference*):
          A list of tensors. Each tensor is a tensor of shape `(batch_size, number_of_r)`, decoded linking actions until now.
      decoder_typing (`List[torch.LongTensor]` each element is shape `(batch_size, 1)`, *optional*, *inference*):
          A list of tensors. Each tensor is a tensor of shape `(batch_size, 1)`, decoded typing actions until now.
  Returns:
      output (`MySeq2SeqLMOutput` ):
          loss (`torch.FloatTensor` of shape `(1)`, *optional*, returned when `labels` is provided):
          pairing (`torch.LongTensor` of shape `(batch_size, 1)`, pairing actions of this step, *inferece* only)
          linking (`torch.LongTensor` of shape `(batch_size, number_of_r)`, linking actions of this step, *inferece* only)
          typing (`torch.LongTensor` of shape `(batch_size, 1)`, typing actions of this step, *inferece* only)
docstr_tok: ["Inputs",":","labels","(","`","torch.LongTensor","`","of","shape","`","(","batch_size",",","target_sequence_length",")","`",",","*","optional","*",",","*","training","*",")",":","Labels","for","computing","the","masked","language","modeling","loss",".","Indices","should","either","be","in","`","[","0",",","1",",","2","]","`","(","3","basic","actions",")","or","-1","(","see","`","input_ids","`","docstring",")",".","labels","with","<","0","values","are","ignored","(","masked",")",".","lr_pair_flag","(","`","torch.LongTensor","`","of","shape","`","(","batch_size",",","sequence_length",",","number_of_l_brackets",")","`",",","*","optional","*",",","*","training","*",")",":","A","flag","tensor",".","Indicating","whether","a","pair","of","tokens","are","paired","*","left-right","*","brackts",".","1","for","paired",",","0","for","not",".","rr_pair_flag","(","`","torch.LongTensor","`","of","shape","`","(","batch_size",",","sequence_length",",","number_of_r_brackets",")","`",",","*","optional","*",",","*","training","*",")",":","A","flag","tensor",".","Indicating","whether","a","pair","of","tokens","are","paired","*","right-right","*","brackts",".","1","for","paired",",","0","for","not",".","full_decoder_input_ids","(","`","torch.LongTensor","`","of","shape","`","(","batch_size",",","sequence_length",")","`",",","*","optional","*",",","*","inference","*",")",":","Decoded","token","ids","until","now",".","Keeping","track","of","how","many","tokens","have","been","copied","from","the","input",",","so","we","know","which","token","to","copy","now",".","full_hidden_states","(","`","torch.FloatTensor","`","of","shape","`","(","batch_size",",","sequence_length",",","hidden_size",")","`",",","*","optional","*",",","*","inference","*",")",":","Hidden","states","of","the","decoded","tokens","until","now",".","decoder_pairing","(","`","List","[","torch.LongTensor","]","`","each","element","is","shape","`","(","batch_size",",","1",")","`",",","*","optional","*",",","*","inference","*",")",":","A","list","of","tensors",".","Each","tensor","is","a","tensor","of","shape","`","(","batch_size",",","1",")","`",",","decoded","pairing","actions","until","now",".","decoder_linking","(","`","List","[","torch.LongTensor","]","`","each","element","is","shape","`","(","batch_size",",","number_of_r",")","`",",","*","optional","*",",","*","inference","*",")",":","A","list","of","tensors",".","Each","tensor","is","a","tensor","of","shape","`","(","batch_size",",","number_of_r",")","`",",","decoded","linking","actions","until","now",".","decoder_typing","(","`","List","[","torch.LongTensor","]","`","each","element","is","shape","`","(","batch_size",",","1",")","`",",","*","optional","*",",","*","inference","*",")",":","A","list","of","tensors",".","Each","tensor","is","a","tensor","of","shape","`","(","batch_size",",","1",")","`",",","decoded","typing","actions","until","now",".","Returns",":","output","(","`","MySeq2SeqLMOutput","`",")",":","loss","(","`","torch.FloatTensor","`","of","shape","`","(","1",")","`",",","*","optional","*",",","returned","when","`","labels","`","is","provided",")",":","pairing","(","`","torch.LongTensor","`","of","shape","`","(","batch_size",",","1",")","`",",","pairing","actions","of","this","step",",","*","inferece","*","only",")","linking","(","`","torch.LongTensor","`","of","shape","`","(","batch_size",",","number_of_r",")","`",",","linking","actions","of","this","step",",","*","inferece","*","only",")","typing","(","`","torch.LongTensor","`","of","shape","`","(","batch_size",",","1",")","`",",","typing","actions","of","this","step",",","*","inferece","*","only",")"]
code: def forward(
      self,
      input_ids=None,
      attention_mask=None,
      decoder_input_ids=None,
      decoder_attention_mask=None,
      head_mask=None,
      decoder_head_mask=None,
      cross_attn_head_mask=None,
      encoder_outputs=None,
      past_key_values=None,
      inputs_embeds=None,
      decoder_inputs_embeds=None,
      labels=None,
      sentence_map=None,
      use_cache=None,
      output_attentions=None,
      output_hidden_states=None,
      return_dict=None,
      encoder_input_ids=None,
      full_decoder_input_ids=None,
      full_hidden_states=None,
      lr_pair_flag=None,
      rr_pair_flag=None,
      sentence_idx=None,
      target_sentence_idx=None,
      same_sentence_flag=None,
      decoder_pairing=None,
      decoder_linking=None,
      decoder_typing=None,
  ):
      r"""
      Inputs:
          labels (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*, *training*):
              Labels for computing the masked language modeling loss.
              Indices should either be in `[0, 1, 2]` (3 basic actions) or -1 (see `input_ids` docstring).
              labels with <0 values are ignored (masked).
          lr_pair_flag (`torch.LongTensor` of shape `(batch_size, sequence_length, number_of_l_brackets)`, *optional*, *training*):
              A flag tensor. Indicating whether a pair of tokens are paired *left-right* brackts. 1 for paired, 0 for not.
          rr_pair_flag (`torch.LongTensor` of shape `(batch_size, sequence_length, number_of_r_brackets)`, *optional*, *training*):
              A flag tensor. Indicating whether a pair of tokens are paired *right-right* brackts. 1 for paired, 0 for not.
          full_decoder_input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*, *inference*):
              Decoded token ids until now. Keeping track of how many tokens have been copied from
              the input, so we know which token to copy now.
          full_hidden_states (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*, *inference*):
              Hidden states of the decoded tokens until now.
          decoder_pairing (`List[torch.LongTensor]` each element is shape `(batch_size, 1)`, *optional*, *inference*):
              A list of tensors. Each tensor is a tensor of shape `(batch_size, 1)`, decoded pairing actions until now.
          decoder_linking (`List[torch.LongTensor]` each element is shape `(batch_size, number_of_r)`, *optional*, *inference*):
              A list of tensors. Each tensor is a tensor of shape `(batch_size, number_of_r)`, decoded linking actions until now.
          decoder_typing (`List[torch.LongTensor]` each element is shape `(batch_size, 1)`, *optional*, *inference*):
              A list of tensors. Each tensor is a tensor of shape `(batch_size, 1)`, decoded typing actions until now.
      Returns:
          output (`MySeq2SeqLMOutput` ):
              loss (`torch.FloatTensor` of shape `(1)`, *optional*, returned when `labels` is provided):
              pairing (`torch.LongTensor` of shape `(batch_size, 1)`, pairing actions of this step, *inferece* only)
              linking (`torch.LongTensor` of shape `(batch_size, number_of_r)`, linking actions of this step, *inferece* only)
              typing (`torch.LongTensor` of shape `(batch_size, 1)`, typing actions of this step, *inferece* only)
      """
      return_dict = (
          return_dict
          if return_dict is not None
          else self.config.use_return_dict
      )
      target_ids = decoder_input_ids
      if labels is not None:  # training
          # decoder_input_ids starts with <pad> and has the same length as target_ids
          decoder_input_ids = self._shift_right(target_ids)
      outputs = self.t5(
          input_ids,
          attention_mask=attention_mask,
          decoder_input_ids=decoder_input_ids,
          encoder_outputs=encoder_outputs,
          decoder_attention_mask=decoder_attention_mask,
          head_mask=head_mask,
          decoder_head_mask=decoder_head_mask,
          cross_attn_head_mask=cross_attn_head_mask,
          past_key_values=past_key_values,
          inputs_embeds=inputs_embeds,
          decoder_inputs_embeds=decoder_inputs_embeds,
          use_cache=use_cache,
          output_attentions=output_attentions,
          output_hidden_states=output_hidden_states,
          return_dict=return_dict,
      )
  
      loss, lm_logits = None, None
      if labels is not None:  # Training: We compute loss
          outputs.last_hidden_state = outputs.last_hidden_state.to(
              self.device
          )
          # shape: (batch_size, seq_len, 1)
          action_logits = self.action_head(outputs.last_hidden_state)
          # a new loss that is compatible with inference
          (numer, denom, rr_loss) = self.get_logits_training(
              outputs.last_hidden_state,  # current_hidden_state
              target_ids=target_ids,
              lr_pair_flag=lr_pair_flag,
              rr_pair_flag=rr_pair_flag,
              labels=labels,
              target_sentence_idx=target_sentence_idx,
              same_sentence_flag=same_sentence_flag,
          )
          # keeping <copy> score 0.
          action_logits = torch.cat(
              [torch.zeros_like(action_logits), action_logits], dim=-1
          )
  
          denom = logsumexp(
              torch.cat([action_logits, denom], dim=-1), dim=-1
          )
          numer = logsumexp(
              torch.cat(
                  [
                      action_logits
                      + torch.where(
                          one_hot_ignore_negative(
                              labels, num_classes=3
                          ),
                          0.0,
                          float("-inf"),
                      )[..., :2],
                      numer,
                  ],
                  dim=-1,
              ),
              dim=-1,
          )
  
          loss = (denom - numer)[
              decoder_attention_mask.bool()
          ].sum() + rr_loss.sum()
          loss = loss / target_ids.size(0)
  
          if same_sentence_flag is not None:
              loss = loss / target_sentence_idx.max().item()
          l_choice, r_choice, typing_choice = None, None, None
  
      else:  # inference, step-by-step classifications
          # (batch_size, 1, dim)
          outputs.last_hidden_state = outputs.last_hidden_state.to(
              self.device
          )
          # denom: (batch_size, 1, 1)
          # l_choice / typing_choice: (batch_size, 1)
          # r_choice: (batch_size, number_of_r)
          (
              denom,
              l_choice,
              typing_choice,
              r_choice,
          ) = self.get_logits_inference(
              outputs.last_hidden_state,  # current_hidden_state
              full_decoder_input_ids,  # previous decoded ids
              full_hidden_states=full_hidden_states,
          )
          # (batch_size, 1, 1)
          action_logits = self.action_head(outputs.last_hidden_state)
          # (batch_size, 1, 3)
          action_logits = torch.cat(
              [torch.zeros_like(action_logits), action_logits, denom],
              dim=-1,
          )
          # Restore lm_logits from action_logits
          lm_logits = self.decoder_input_ids_to_vocab_mask(
              action_logits, full_decoder_input_ids, encoder_input_ids
          )
  
      return MySeq2SeqLMOutput(
          loss=loss,
          logits=lm_logits,
          past_key_values=outputs.past_key_values,
          decoder_hidden_states=outputs.last_hidden_state,
          decoder_attentions=outputs.decoder_attentions,
          cross_attentions=outputs.cross_attentions,
          encoder_last_hidden_state=outputs.encoder_last_hidden_state,
          encoder_hidden_states=outputs.encoder_hidden_states,
          encoder_attentions=outputs.encoder_attentions,
          pairing=l_choice,
          linking=r_choice,
          typing=typing_choice,
      )
  
imports: import sys
  import os
  from ctypes import sizeof
  import math
  from typing import Optional, Tuple, Any, Dict, Iterable, List
  import logging
  import torch
  import torch.utils.checkpoint
  from torch import nn
  import torch.nn.init
  import torch.nn.functional
  from transformers.models.t5.modeling_t5 import T5PreTrainedModel, T5Model, T5_INPUTS_DOCSTRING, _CONFIG_FOR_DOC
  from transformers.file_utils import add_start_docstrings_to_model_forward, replace_return_docstrings
  from .modeling_outputs import MySeq2SeqLMOutput
  import util
  from util import logsumexp, dummy_padding, prepare_pair_embeddings, one_hot_ignore_negative, batched_masked_select
label_desc: Use image node_id 2 for calling the T5ERE obj's underlying member method code with example usage: obj.forward(input_ids, attention_mask, decoder_input_ids, decoder_attention_mask, head_mask, decoder_head_mask, cross_attn_head_mask, encoder_outputs, past_key_values, inputs_embeds, decoder_inputs_embeds, labels, sentence_map, use_cache, output_attentions, output_hidden_states, return_dict, encoder_input_ids, full_decoder_input_ids, full_hidden_states, lr_pair_flag, rr_pair_flag, sentence_idx, target_sentence_idx, same_sentence_flag, decoder_pairing, decoder_linking, decoder_typing) and returns: MySeq2SeqLMOutput