code: def get_logits_training(
      self,
      decoder_output,  # (batch_size, seq_len, hidden_dim)
      target_ids,  # (batch_size, seq_len, )
      lr_pair_flag=None,
      rr_pair_flag=None,
      labels=None,
      target_sentence_idx=None,
      same_sentence_flag=None,
  ):
      decoder_output = self.dropout(
          decoder_output
      )  # one dropout for all
  
      batch_size, seq_len = decoder_output.size(0), decoder_output.size(
          1
      )
      target_mask = target_ids != self.config.pad_token_id
  
      # (batch_size, seq_len)
      linearized_indices = (labels >= 0).cumsum(dim=-1) - 1
      # (batch_size, seq_len)
      is_r = target_ids == self.mention_end_id
      is_l = target_ids == self.mention_start_id
  
      if is_r.sum() == 0:
          numer = torch.full_like(decoder_output[..., :1], NEGINF)
          denom = torch.full_like(decoder_output[..., :1], NEGINF)
          rr_loss = torch.zeros_like(denom)
  
          return numer, denom, rr_loss
  
      # (batch_size, num_r / num_l)
      (r_pos, r_pos_mask) = batched_masked_select(
          linearized_indices, is_r
      )
      (l_pos, l_pos_mask) = batched_masked_select(
          linearized_indices, is_l
      )
  
      # (batch_size, num_r / num_l, hidden_dim)
      r_emb, _ = batched_masked_select(decoder_output, is_r)
      l_emb, _ = batched_masked_select(decoder_output, is_l)
  
      # (batch_size, seq_len, num_r / num_l)
      distance_to_previous_r = linearized_indices.unsqueeze(
          2
      ) - r_pos.unsqueeze(1)
      distance_to_previous_l = linearized_indices.unsqueeze(
          2
      ) - l_pos.unsqueeze(1)
  
      # (batch_size, seq_len, num_r / num_l)
      is_after_r = distance_to_previous_r > 0
      is_after_l = distance_to_previous_l > 0
      is_after_r = (
          is_after_r
          & target_mask.unsqueeze(2)
          & r_pos_mask.unsqueeze(1)
      )
      is_after_l = (
          is_after_l
          & target_mask.unsqueeze(2)
          & l_pos_mask.unsqueeze(1)
      )
  
      # check correctness for batch
      kept_l = min(self.max_nest_depth, l_emb.size(1))
      # (batch_size, seq_len, kept_l)
      _, prev_l_indices = (
          -distance_to_previous_l + (is_after_l * 10000)
      ).topk(kept_l, dim=2)
  
      # (batch_size, seq_len, kept_l, hidden_dim)
      kept_l_emb = util.batch_select(l_emb, prev_l_indices)
      # (batch_size, seq_len, kept_l)
      distance_to_previous_l = util._batched_index_select(
          distance_to_previous_l, prev_l_indices, dim=2
      )
      expanded_decoder_output = decoder_output.unsqueeze(2).expand(
          -1, -1, kept_l, -1
      )
      # (batch_size, seq_len, kept_l, 2*hidden_dim)
      lr_pair_emb = torch.cat(
          [kept_l_emb, expanded_decoder_output], dim=-1
      )
  
      # shape(batch_size, seq_len, kept_l, num_typing_classes)
      kept_is_after_l = is_after_l.gather(
          dim=2, index=prev_l_indices
      ).unsqueeze(-1)
  
      lr_score = (
          self.lr_scorer(lr_pair_emb) + (~kept_is_after_l) * NEGINF
      )
  
      # (batch_size, seq_len, 1)
      lr_denom = logsumexp(
          lr_score, dim=(2, 3), keepdim=False
      ).unsqueeze(-1) * is_after_l.any(dim=2, keepdim=True)
      # (batch_size, seq_len, num_l, num_typing_classes) ->
      #     (batch_size, seq_len, kept_l, num_typing_classes)
      kept_lr_pair_flag = util._batched_index_select(
          lr_pair_flag, prev_l_indices, dim=2
      )
      # (batch_size, seq_len, 1)
      lr_numer = logsumexp(
          lr_score + (~kept_lr_pair_flag) * NEGINF,
          dim=(2, 3),
          keepdim=False,
      ).unsqueeze(-1) * is_after_l.any(dim=2, keepdim=True)
  
      # (batch_size, seq_len, kept_r, 1+num_linking_classes)
      rr_score = self.get_rr_relation_score(
          r_emb, decoder_output, distance_to_previous_r
      )
      # (batch_size, seq_len, kept_r)
      rr_denom = logsumexp(rr_score, dim=3, keepdim=False)
      # (batch_size, seq_len, kept_r)
      rr_numer = logsumexp(
          rr_score + (~rr_pair_flag) * (NEGINF), dim=3, keepdim=False
      )
  
      if same_sentence_flag is not None:
          # (batch_size, seq_len, 1)
          rr_denom = (rr_denom * same_sentence_flag * is_after_r).sum(
              2, keepdim=True
          )
          # (batch_size, seq_len, 1)
          rr_numer = (rr_numer * same_sentence_flag * is_after_r).sum(
              2, keepdim=True
          )
      else:
          # (batch_size, seq_len, 1)
          rr_denom = (rr_denom * is_after_r).sum(2, keepdim=True)
          # (batch_size, seq_len, 1)
          rr_numer = (rr_numer * is_after_r).sum(2, keepdim=True)
  
      numer, denom = lr_numer, lr_denom
      rr_loss = (rr_denom - rr_numer) * is_r.unsqueeze(-1)
  
      return (numer, denom, rr_loss)
  
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 4 for calling the T5ERE obj's underlying member method code with example usage: obj.get_logits_training(decoder_output, target_ids, lr_pair_flag, rr_pair_flag, labels, target_sentence_idx, same_sentence_flag) and returns: numer,  denom,  rr_loss, numer,  denom,  rr_loss