class_docstr: Self Attention and FFN Unit
class_docstr_tok: ["Self","Attention","and","FFN","Unit"]
docstr: GPTSAN transformer block.
  
  Args:
      hidden_states  (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
          Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention
          if the model is configured as a decoder.
      past_key_values (`tuple(tuple(torch.FloatTensor))` of length `config.n_layers` with each tuple having 4 tensors of shape `(batch_size, num_heads, sequence_length - 1, embed_size_per_head)`):
          Contains precomputed key and value hidden states of the attention blocks. Can be used to speed up
          decoding. If `past_key_values` are used, the user can optionally input only the last
          `decoder_input_ids` (those that don't have their past key value states given to this model) of shape
          `(batch_size, 1)` instead of all `decoder_input_ids` of shape `(batch_size, sequence_length)`.
      attention_mask (`torch.FloatTensor` of shape `(batch_size, sequence_length)`, *optional*):
          Mask to avoid performing attention on the padding token indices of the encoder input. This mask is used
          in the cross-attention if the model is configured as a decoder. Mask values selected in `[0, 1]`:
  
          - 1 for tokens that are **not masked**,
          - 0 for tokens that are **masked**.
  
      head_mask (`numpy.ndarray` of shape `({0})`, `optional):
          Mask to nullify selected heads of the attention modules. Mask values selected in `[0, 1]`:
  
          - 1 indicates the head is **not masked**,
          - 0 indicates the head is **masked**.
  
      use_cache (`bool`, *optional*):
          If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding
          (see `past_key_values`).
      output_attentions (`bool`) :
          output attention probabirities.
      output_router_tuple:
          output experts router logits and expert id.
  Returns:
      Tuple[torch.Tensor[num_groups, tokens_per_group, hidden_dim],...]
docstr_tok: ["GPTSAN","transformer","block",".","Args",":","hidden_states","(","`","torch.FloatTensor","`","of","shape","`","(","batch_size",",","sequence_length",",","hidden_size",")","`",",","*","optional","*",")",":","Sequence","of","hidden-states","at","the","output","of","the","last","layer","of","the","encoder",".","Used","in","the","cross-attention","if","the","model","is","configured","as","a","decoder",".","past_key_values","(","`","tuple","(","tuple","(","torch.FloatTensor",")",")","`","of","length","`","config.n_layers","`","with","each","tuple","having","4","tensors","of","shape","`","(","batch_size",",","num_heads",",","sequence_length","-","1",",","embed_size_per_head",")","`",")",":","Contains","precomputed","key","and","value","hidden","states","of","the","attention","blocks",".","Can","be","used","to","speed","up","decoding",".","If","`","past_key_values","`","are","used",",","the","user","can","optionally","input","only","the","last","`","decoder_input_ids","`","(","those","that","do","n't","have","their","past","key","value","states","given","to","this","model",")","of","shape","`","(","batch_size",",","1",")","`","instead","of","all","`","decoder_input_ids","`","of","shape","`","(","batch_size",",","sequence_length",")","`",".","attention_mask","(","`","torch.FloatTensor","`","of","shape","`","(","batch_size",",","sequence_length",")","`",",","*","optional","*",")",":","Mask","to","avoid","performing","attention","on","the","padding","token","indices","of","the","encoder","input",".","This","mask","is","used","in","the","cross-attention","if","the","model","is","configured","as","a","decoder",".","Mask","values","selected","in","`","[","0",",","1","]","`",":","-","1","for","tokens","that","are","*","*","not","masked","*","*",",","-","0","for","tokens","that","are","*","*","masked","*","*",".","head_mask","(","`","numpy.ndarray","`","of","shape","`","(","{","0","}",")","`",",","`","optional",")",":","Mask","to","nullify","selected","heads","of","the","attention","modules",".","Mask","values","selected","in","`","[","0",",","1","]","`",":","-","1","indicates","the","head","is","*","*","not","masked","*","*",",","-","0","indicates","the","head","is","*","*","masked","*","*",".","use_cache","(","`","bool","`",",","*","optional","*",")",":","If","set","to","`","True","`",",","`","past_key_values","`","key","value","states","are","returned","and","can","be","used","to","speed","up","decoding","(","see","`","past_key_values","`",")",".","output_attentions","(","`","bool","`",")",":","output","attention","probabirities",".","output_router_tuple",":","output","experts","router","logits","and","expert","id",".","Returns",":","Tuple","[","torch.Tensor","[","num_groups",",","tokens_per_group",",","hidden_dim","]",",","...","]"]
code: def forward(
      self,
      hidden_states: Optional[Tuple[torch.FloatTensor]],
      past_key_value: Optional[Tuple[torch.Tensor]] = None,
      attention_mask: Optional[torch.FloatTensor] = None,
      head_mask: Optional[torch.FloatTensor] = None,
      use_cache: Optional[bool] = False,
      output_attentions: Optional[bool] = False,
      output_router_tuple: Optional[bool] = False,
  ) -> Tuple[Union[torch.Tensor, Tuple[torch.Tensor]], ...]:
      r"""
      GPTSAN transformer block.
  
      Args:
          hidden_states  (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
              Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention
              if the model is configured as a decoder.
          past_key_values (`tuple(tuple(torch.FloatTensor))` of length `config.n_layers` with each tuple having 4 tensors of shape `(batch_size, num_heads, sequence_length - 1, embed_size_per_head)`):
              Contains precomputed key and value hidden states of the attention blocks. Can be used to speed up
              decoding. If `past_key_values` are used, the user can optionally input only the last
              `decoder_input_ids` (those that don't have their past key value states given to this model) of shape
              `(batch_size, 1)` instead of all `decoder_input_ids` of shape `(batch_size, sequence_length)`.
          attention_mask (`torch.FloatTensor` of shape `(batch_size, sequence_length)`, *optional*):
              Mask to avoid performing attention on the padding token indices of the encoder input. This mask is used
              in the cross-attention if the model is configured as a decoder. Mask values selected in `[0, 1]`:
  
              - 1 for tokens that are **not masked**,
              - 0 for tokens that are **masked**.
  
          head_mask (`numpy.ndarray` of shape `({0})`, `optional):
              Mask to nullify selected heads of the attention modules. Mask values selected in `[0, 1]`:
  
              - 1 indicates the head is **not masked**,
              - 0 indicates the head is **masked**.
  
          use_cache (`bool`, *optional*):
              If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding
              (see `past_key_values`).
          output_attentions (`bool`) :
              output attention probabirities.
          output_router_tuple:
              output experts router logits and expert id.
      Returns:
          Tuple[torch.Tensor[num_groups, tokens_per_group, hidden_dim],...]
      """
      atten_out = self.self_attn(
          hidden_states=hidden_states,
          past_key_value=past_key_value,
          attention_mask=attention_mask,
          head_mask=head_mask,
          use_cache=use_cache,
          output_attentions=output_attentions,
      )
      attention_output = atten_out[0]
  
      if isinstance(self.feed_forward, GPTSanJapaneseLayerSparseFF):
          sparse_out = self.feed_forward(
              attention_output, output_router_tuple
          )
          if output_router_tuple:
              hidden, router_tuple = sparse_out
          else:
              hidden = sparse_out
      else:
          hidden = self.feed_forward(attention_output)
  
      outputs = (hidden,) + atten_out[1:]
  
      if (
          isinstance(self.feed_forward, GPTSanJapaneseLayerSparseFF)
          and output_router_tuple
      ):
          outputs += (router_tuple,)
  
      return outputs
  
imports: import copy
  from typing import List, Optional, Tuple, Union
  import torch
  import torch.nn
  from ...activations import ACT2FN
  from ...modeling_outputs import MoECausalLMOutputWithPast, MoEModelOutputWithPastAndCrossAttentions
  from ...modeling_utils import PreTrainedModel
  from ...utils import DUMMY_INPUTS, DUMMY_MASK, add_start_docstrings, add_start_docstrings_to_model_forward, is_torch_fx_proxy, logging
  from .configuration_gptsan_japanese import GPTSanJapaneseConfig
label_desc: Use image node_id 2 for calling the GPTSanJapaneseBlock obj's underlying member method code with example usage: obj.forward(hidden_states, past_key_value, attention_mask, head_mask, use_cache, output_attentions, output_router_tuple) and returns: outputs