class_docstr: Transformer class.
  Args:
      d_model (int): model dimension
      nhead (int): number of heads
      num_glance_enc_layers (int): number of glance encoder layers
      num_gaze_s1_dec_layers (int): number of gaze s1 decoder layers
      num_gaze_s2_dec_layers (int): number of gaze s2 decoder layers
      dim_feedforward (int): dimension of feedforward
      dropout (int): drop out probability
      activation (str): activation
class_docstr_tok: ["Transformer","class",".","Args",":","d_model","(","int",")",":","model","dimension","nhead","(","int",")",":","number","of","heads","num_glance_enc_layers","(","int",")",":","number","of","glance","encoder","layers","num_gaze_s1_dec_layers","(","int",")",":","number","of","gaze","s1","decoder","layers","num_gaze_s2_dec_layers","(","int",")",":","number","of","gaze","s2","decoder","layers","dim_feedforward","(","int",")",":","dimension","of","feedforward","dropout","(","int",")",":","drop","out","probability","activation","(","str",")",":","activation"]
code: def forward(
      self,
      src,
      mask,
      il_token_embed,
      rl_token_embed,
      verb_token_embed,
      role_token_embed,
      pos_embed,
      vidx_ridx,
      targets=None,
      inference=False,
  ):
      device = il_token_embed.device
      # flatten NxCxHxW to HWxNxC
      bs, _, h, w = src.shape
      flattend_src = src.flatten(2).permute(2, 0, 1)
      pos_embed = pos_embed.flatten(2).permute(2, 0, 1)
      mask = mask.flatten(1)
  
      # Glance Transformer
      ## Encoder
      il_token = il_token_embed.unsqueeze(1).repeat(1, bs, 1)
      glance_enc_zero_mask = torch.zeros(
          (bs, 1), dtype=torch.bool, device=device
      )
      mem_mask = torch.cat([glance_enc_zero_mask, mask], dim=1)
      il_token_flattend_src = torch.cat([il_token, flattend_src], dim=0)
      glance_enc_memory = self.glance_enc(
          il_token_flattend_src,
          src_key_padding_mask=mem_mask,
          pos=pos_embed,
          num_zeros=1,
      )
      il_token_feature, aggregated_src = glance_enc_memory.split(
          [1, h * w], dim=0
      )
  
      # Gaze-Step1 Transformer
      ## Decoder
      all_role_tokens = role_token_embed.unsqueeze(1).repeat(1, bs, 1)
      role_tgt = torch.zeros_like(all_role_tokens)
      extracted_rhs = self.gaze_s1_dec(
          all_role_tokens,
          self.ln1(flattend_src),
          memory_key_padding_mask=mask,
          pos=pos_embed,
          query_pos=role_tgt,
      )
      extracted_rhs = extracted_rhs.transpose(1, 2)
      ## Encoder
      num_all_roles = 190
      rl_token = rl_token_embed.unsqueeze(1).repeat(1, bs, 1)
      gaze_s1_enc_zero_mask = torch.zeros(
          (bs, (1 + num_all_roles)), dtype=torch.bool, device=device
      )
      rl_token_extracted_rhs = torch.cat(
          [rl_token, extracted_rhs.view(num_all_roles, 1, -1)], dim=0
      )
      gaze_s1_enc_memory = self.gaze_s1_enc(
          rl_token_extracted_rhs,
          src_key_padding_mask=gaze_s1_enc_zero_mask,
          pos=None,
          num_zeros=(1 + num_all_roles),
      )
      rl_token_feature, aggregated_rhs = gaze_s1_enc_memory.split(
          [1, num_all_roles], dim=0
      )
  
      # Verb Prediction
      il_token_feature = il_token_feature.view(bs, -1)
      rl_token_feature = rl_token_feature.view(bs, -1)
      vhs = torch.cat([il_token_feature, rl_token_feature], dim=-1)
      vhs = self.ln2(vhs)
      verb_pred = self.verb_classifier(vhs).view(
          bs, self.num_verb_classes
      )
  
      # Gaze-Step2 Transformer
      ## Deocder
      ### At training time, we assume that the ground-truth verb is given.
      ##### Please see the evaluation details in [Grounded Situation Recognition] task.
      ##### There are three evaluation settings: Top-1 Predicted Verb, Top-5 Predicted Verbs and Ground-Truth Verb.
      ##### If top-1 predicted verb is incorrect, then grounded noun predictions in Top-1 Predicted Verb setting are considered incorrect.
      ##### If the ground-truth verb is not included in top-5 predicted verbs,
      # then grounded noun predictions in Top-5 Predicted Verbs setting are considered incorrect.
      ##### In Ground-Truth Verb setting, we only consider grounded noun predictions.
      ### At inference time, we use the predicted verb.
      #### For frame-role queries, we select the verb token embedding corresponding to the predicted verb.
      #### For frame-role queries, we select the role token embeddings corresponding to the roles associated with the predicted verb.
      if not inference:
          selected_verb_token = verb_token_embed[targets["verbs"]].view(
              1, -1
          )
          selected_roles = targets["roles"]
      else:
          top1_verb = torch.topk(verb_pred, k=1, dim=1)[1].item()
          selected_verb_token = verb_token_embed[top1_verb].view(1, -1)
          selected_roles = vidx_ridx[top1_verb]
      selected_role_tokens = role_token_embed[selected_roles]
      frame_role_queries = selected_role_tokens + selected_verb_token
      frame_role_queries = frame_role_queries.unsqueeze(1).repeat(
          1, bs, 1
      )
      role_tgt = torch.zeros_like(frame_role_queries)
      final_rhs = self.gaze_s2_dec(
          frame_role_queries,
          self.ln3(aggregated_src),
          memory_key_padding_mask=mask,
          pos=pos_embed,
          query_pos=role_tgt,
      )
      final_rhs = self.ln4(final_rhs)
      final_rhs = final_rhs.transpose(1, 2)
  
      return (
          verb_pred,
          extracted_rhs,
          aggregated_rhs,
          final_rhs,
          selected_roles,
      )
  
imports: import copy
  import torch
  import torch.nn.functional
  from typing import Optional
  from torch import nn, Tensor
label_desc: Use image node_id 3 for calling the Transformer obj's underlying member method code with example usage: obj.forward(src, mask, il_token_embed, rl_token_embed, verb_token_embed, role_token_embed, pos_embed, vidx_ridx, targets, inference) and returns: verb_pred,  extracted_rhs,  aggregated_rhs,  final_rhs,  selected_roles