docstr: num_precontext (`torch.LongTensor` of shape `(batch_size,1)`):
      length of `hybrid` input tokens in the input. Tokens up to this length refer to both front and back like
      BERT, tokens after that refer only to front like GPT. see also:
      https://github.com/tanreinama/GPTSAN/blob/main/report/model.md
  
  Returns:
      `MoEModelOutputWithPastAndCrossAttentions` or `tuple` if `return_dict` returns
      MoEModelOutputWithPastAndCrossAttentions insted of tuple
docstr_tok: ["num_precontext","(","`","torch.LongTensor","`","of","shape","`","(","batch_size,1",")","`",")",":","length","of","`","hybrid","`","input","tokens","in","the","input",".","Tokens","up","to","this","length","refer","to","both","front","and","back","like","BERT",",","tokens","after","that","refer","only","to","front","like","GPT",".","see","also",":","https",":","\/\/github.com\/tanreinama\/GPTSAN\/blob\/main\/report\/model.md","Returns",":","`","MoEModelOutputWithPastAndCrossAttentions","`","or","`","tuple","`","if","`","return_dict","`","returns","MoEModelOutputWithPastAndCrossAttentions","insted","of","tuple"]
code: def forward(
      self,
      input_ids: Optional[torch.LongTensor] = None,
      attention_mask: Optional[torch.FloatTensor] = None,
      token_type_ids: Optional[torch.FloatTensor] = None,
      spout: Optional[torch.FloatTensor] = None,
      past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None,
      head_mask: Optional[torch.FloatTensor] = None,
      use_cache: Optional[bool] = False,
      inputs_embeds: Optional[torch.FloatTensor] = None,
      decoder_inputs_embeds: Optional[torch.FloatTensor] = None,
      output_attentions: Optional[bool] = None,
      output_hidden_states: Optional[bool] = None,
      return_dict: Optional[bool] = None,
      output_router_logits: Optional[bool] = None,
      num_precontext: Optional[torch.LongTensor] = None,
  ) -> Union[
      MoEModelOutputWithPastAndCrossAttentions, Tuple[torch.FloatTensor]
  ]:
      r"""
      num_precontext (`torch.LongTensor` of shape `(batch_size,1)`):
          length of `hybrid` input tokens in the input. Tokens up to this length refer to both front and back like
          BERT, tokens after that refer only to front like GPT. see also:
          https://github.com/tanreinama/GPTSAN/blob/main/report/model.md
  
      Returns:
          `MoEModelOutputWithPastAndCrossAttentions` or `tuple` if `return_dict` returns
          MoEModelOutputWithPastAndCrossAttentions insted of tuple
      """
      return_dict = (
          return_dict
          if return_dict is not None
          else self.config.use_return_dict
      )
      device = self.position_embeddings.weight.device
      if input_ids is None:
          input_ids = (
              torch.zeros([1, 1]).int().to(device)
          )  # dummy for input_ids was None
      num_pasts_contexts = 0
      num_batch = input_ids.shape[0]
      pasts_or_spout_value = None
      if past_key_values is not None:
          num_pasts_contexts = past_key_values[0][0].shape[2]
      elif self.config.d_spout and spout is not None:
          # `spout` is a special input vector specific to GPTSAN
          # This controls the output by projecting embedded information such as the class of sentences during learning.
          # It should passed instead of the first past_key_value.
          # See the original GPTSAN repository for details
          num_pasts_contexts += 1
  
      # If there is an attention_mask, increase first one for spout
      if (
          self.config.d_spout
          and spout is not None
          and attention_mask is not None
      ):
          attention_mask_with_spout = torch.ones(
              num_batch, attention_mask.shape[1] + 1, device=device
          )
          attention_mask_with_spout[:, 1:] -= (
              1 - attention_mask
          )  # 1st token should be spout
          attention_mask = (
              attention_mask_with_spout  # update attention_mask
          )
  
      if num_precontext is not None:
          # `num_precontext` is the number of tokens that refer to each other in prefix-lm
          # created per batch, so dimension of num_precontext should be [batch, 1]
          if not (
              len(num_precontext.shape) == 2
              and num_precontext.shape[1] == 1
          ):  # num_precontext Should be [batch,1]
              raise ValueError(
                  "num_precontext should be [batch, 1] size."
              )
          num_precontext = torch.reshape(num_precontext, [-1])
      else:
          num_precontext = torch.zeros([num_batch]).int().to(device)
  
      num_input_contexts = input_ids.shape[1]
      num_output_contexts = num_input_contexts + num_pasts_contexts
  
      hidden_states = self.embed_tokens(input_ids)
  
      if past_key_values is not None:
          pasts_or_spout_value = past_key_values
      elif self.config.d_spout and spout is not None:
          # Make vector from `spout` of GPTSAN to the same shape as past_key_values
          pasts_or_spout_value = self.spout(
              spout
          )  # projecting `spout` vector
          pasts_or_spout_value = torch.reshape(
              pasts_or_spout_value,
              [
                  num_batch,
                  self.config.num_layers,
                  2,
                  self.config.num_heads,
                  num_pasts_contexts,
                  self.config.d_model // self.config.num_heads,
              ],
          )
          pasts_or_spout_value = torch.split(
              pasts_or_spout_value, [1] * self.config.num_layers, dim=1
          )
          # make same shape as past_key_values
          pasts_or_spout_value = tuple(
              tuple(
                  [
                      b.squeeze(1)
                      for b in torch.split(a.squeeze(1), [1, 1], dim=1)
                  ]
              )
              for a in pasts_or_spout_value
          )
      else:
          pasts_or_spout_value = [None] * self.config.num_layers
  
      # Token position considering spout and pasts
      token_position = (
          torch.arange(num_input_contexts).to(device)
          + num_pasts_contexts
      )
  
      if attention_mask is None:
          attention_mask = torch.ones(
              num_batch, num_input_contexts, device=device
          )
  
      # positions for get position_embeddings
      gather_position = (
          (
              torch.zeros(
                  (num_batch, self.config.d_model, num_input_contexts)
              ).to(device)
              + token_position.unsqueeze(0)
          )
          .transpose(1, 2)
          .long()
      )
      # When padding with padding_side="left", zeros line up on the left side of attention_mask, so position_embeddings is shifted accordingly
      gather_position -= (
          (1 - attention_mask).argmin(dim=-1).unsqueeze(1).unsqueeze(2)
      )
      gather_position = torch.clip(
          gather_position,
          num_pasts_contexts,
          self.config.max_position_embeddings - 1,
      )
  
      # attention_mask is applied per batch
      for i in range(num_batch):
          hidden_states[i] += torch.gather(
              self.position_embeddings.weight,
              dim=0,
              index=gather_position[i],
          )
  
      # Create a mask to be used when making the prefix Input length of Prefix-LM variable
      causal_mask = (
          torch.tril(
              torch.ones(
                  (num_output_contexts, num_output_contexts),
                  dtype=torch.uint8,
              )
          )
          .view(1, 1, num_output_contexts, num_output_contexts)
          .to(device)
      )
      prefix_lm_mask = causal_mask[:, :, -num_input_contexts:, :]
      if token_type_ids is not None:
          token_type_ids = token_type_ids.unsqueeze(1).unsqueeze(2)
          prefix_lm_mask = (
              (prefix_lm_mask + token_type_ids) > 0
          ).float()
      # Marge prefix_lm_mask and attention_mask
      extended_attention_mask = (
          prefix_lm_mask * attention_mask.unsqueeze(1).unsqueeze(2)
      )
  
      # Prepare head mask if needed
      if head_mask is not None:
          head_mask = self.get_head_mask(
              head_mask,
              self.config.num_switch_layers
              + self.config.num_ext_layers,
          )  # n_layer x batch x n_heads x N x N
  
      # outputs
      present_key_value_states = (
          () if self.config.use_cache or use_cache else None
      )
      all_hidden_states = (
          ()
          if self.config.output_hidden_states or output_hidden_states
          else None
      )
      all_attentions = (
          ()
          if self.config.output_attentions or output_attentions
          else None
      )
      all_router_probs = (
          ()
          if self.config.output_router_logits or output_router_logits
          else None
      )
  
      for layer, past in enumerate(pasts_or_spout_value):
          if layer == self.config.num_switch_layers:
              if self.config.num_ext_layers > 0:
                  # extra_position_embeddings are extra position embeddings that are only created when extending the model with code from the original GPTSAN repository. Not used in the default model.
                  # However, it is created when you create an additional layer and partially train only that location.
                  # Therefore, convert_gptsan_tf_checkpoint_to_pytorch.py is used when converting and loading models created in the original GPTSAN repository.
                  for i in range(num_batch):
                      hidden_states[i] += torch.gather(
                          self.extra_position_embeddings.weight,
                          dim=0,
                          index=gather_position[i],
                      )
  
          output_router_tuple = (
              self.config.output_router_logits or output_router_logits
          ) and layer < self.config.num_switch_layers
          block_output = self.blocks[layer](
              hidden_states=hidden_states,
              past_key_value=past,
              attention_mask=extended_attention_mask,
              head_mask=head_mask,
              use_cache=self.config.use_cache or use_cache,
              output_attentions=self.config.output_attentions
              or output_attentions,
              output_router_tuple=output_router_tuple,
          )
  
          outpos = 0
          hidden_states = block_output[outpos]
          if self.config.output_hidden_states or output_hidden_states:
              all_hidden_states += (hidden_states,)
          if self.config.use_cache or use_cache:
              outpos += 1
              present = block_output[outpos]
              present_key_value_states += (present,)
          if self.config.output_attentions or output_attentions:
              outpos += 1
              attention_probs = block_output[outpos]
              all_attentions += (attention_probs,)
          if output_router_tuple:
              outpos += 1
              router_tuple = block_output[outpos]
              all_router_probs.append(router_tuple[0])
  
      hidden_states = self.last_project(hidden_states)
      hidden_states = self.act(hidden_states)
  
      if self.config.output_hidden_states or output_hidden_states:
          all_hidden_states = all_hidden_states + (hidden_states,)
  
      if not return_dict:
          return tuple(
              v
              for v in [
                  hidden_states,
                  present_key_value_states,
                  all_hidden_states,
                  all_attentions,
                  all_router_probs,
              ]
              if v is not None
          )
  
      return MoEModelOutputWithPastAndCrossAttentions(
          last_hidden_state=hidden_states,
          past_key_values=present_key_value_states,
          hidden_states=all_hidden_states,
          attentions=all_attentions,
          router_probs=all_router_probs,
      )
  
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 4 for calling the GPTSanJapaneseModel obj's underlying member method code with example usage: obj.forward(input_ids, attention_mask, token_type_ids, spout, past_key_values, head_mask, use_cache, inputs_embeds, decoder_inputs_embeds, output_attentions, output_hidden_states, return_dict, output_router_logits, num_precontext) and returns: MoEModelOutputWithPastAndCrossAttentions, tuple