code: def forward(
      self,
      input_ids: Optional[torch.LongTensor] = None,
      past_key_values: Optional[
          Tuple[Tuple[torch.Tensor, torch.Tensor], ...]
      ] = None,
      attention_mask: Optional[torch.Tensor] = None,
      inputs_embeds: Optional[torch.LongTensor] = None,
      use_cache: Optional[bool] = None,
      output_attentions: Optional[bool] = None,
      output_hidden_states: Optional[bool] = None,
      return_dict: Optional[bool] = None,
  ) -> Union[
      Tuple[torch.Tensor, ...],
      BaseModelOutputWithPastAndCrossAttentions,
  ]:
      output_attentions = (
          output_attentions
          if output_attentions is not None
          else self.config.output_attentions
      )
      output_hidden_states = (
          output_hidden_states
          if output_hidden_states is not None
          else self.config.output_hidden_states
      )
      use_cache = (
          use_cache if use_cache is not None else self.config.use_cache
      )
      return_dict = (
          return_dict
          if return_dict is not None
          else self.config.use_return_dict
      )
  
      if input_ids is not None and inputs_embeds is not None:
          raise ValueError(
              "You cannot specify both input_ids and inputs_embeds at the same time"
          )
      elif input_ids is not None:
          batch_size, seq_length = input_ids.shape
      elif inputs_embeds is not None:
          batch_size, seq_length, _ = inputs_embeds.shape
      else:
          raise ValueError(
              "You have to specify either input_ids or inputs_embeds"
          )
  
      if past_key_values is None:
          past_key_values = tuple([None] * len(self.blocks))
  
      if inputs_embeds is None:
          inputs_embeds = self.wte(input_ids)
  
      hidden_states = inputs_embeds
  
      presents = () if use_cache else None
      all_self_attentions = () if output_attentions else None
      all_hidden_states = () if output_hidden_states else None
  
      if self.gradient_checkpointing and self.training:
          if use_cache:
              logger.warning_once(
                  "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..."
              )
              use_cache = False
  
      # Compute alibi tensor: check build_alibi_tensor documentation
      seq_length_with_past = seq_length
      past_key_values_length = 0
      if past_key_values[0] is not None:
          past_key_values_length = past_key_values[0][0].shape[2]
          seq_length_with_past = (
              seq_length_with_past + past_key_values_length
          )
      if attention_mask is None:
          attention_mask = torch.ones(
              (batch_size, seq_length_with_past),
              device=hidden_states.device,
          )
      else:
          attention_mask = attention_mask.to(hidden_states.device)
  
      alibi = self.build_mpt_alibi_tensor(
          self.num_heads,
          self.config.max_seq_len,
          device=hidden_states.device,
      )
  
      causal_mask = _prepare_4d_causal_attention_mask(
          attention_mask,
          (batch_size, seq_length),
          inputs_embeds,
          past_key_values_length,
      )
      causal_mask = causal_mask.bool()
  
      for block, layer_past in zip(self.blocks, past_key_values):
          if output_hidden_states:
              all_hidden_states = all_hidden_states + (hidden_states,)
  
          if self.gradient_checkpointing and self.training:
              outputs = self._gradient_checkpointing_func(
                  block.__call__,
                  hidden_states,
                  alibi,
                  causal_mask,
                  layer_past,
                  use_cache,
                  output_attentions,
              )
          else:
              outputs = block(
                  hidden_states,
                  layer_past=layer_past,
                  attention_mask=causal_mask,
                  use_cache=use_cache,
                  output_attentions=output_attentions,
                  position_bias=alibi,
              )
  
          hidden_states = outputs[0]
          if use_cache is True:
              presents = presents + (outputs[1],)
  
          if output_attentions:
              all_self_attentions = all_self_attentions + (
                  outputs[2 if use_cache else 1],
              )
  
      # Add last hidden state
      hidden_states = self.norm_f(hidden_states)
  
      if output_hidden_states:
          all_hidden_states = all_hidden_states + (hidden_states,)
  
      if not return_dict:
          return tuple(
              v
              for v in [
                  hidden_states,
                  presents,
                  all_hidden_states,
                  all_self_attentions,
              ]
              if v is not None
          )
  
      return BaseModelOutputWithPastAndCrossAttentions(
          last_hidden_state=hidden_states,
          past_key_values=presents,
          hidden_states=all_hidden_states,
          attentions=all_self_attentions,
      )
  
imports: import math
  from typing import Optional, Tuple, Union
  import torch
  import torch.utils.checkpoint
  from torch import nn
  from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, LayerNorm, MSELoss
  from torch.nn import functional
  from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward
  from ...modeling_attn_mask_utils import _prepare_4d_causal_attention_mask
  from ...modeling_outputs import BaseModelOutputWithPastAndCrossAttentions, CausalLMOutputWithCrossAttentions, QuestionAnsweringModelOutput, SequenceClassifierOutputWithPast, TokenClassifierOutput
  from ...modeling_utils import PreTrainedModel
  from ...utils import logging
  from .configuration_mpt import MptConfig
label_desc: Use image node_id 5 for calling the MptModel obj's underlying member method code with example usage: obj.forward(input_ids, past_key_values, attention_mask, inputs_embeds, use_cache, output_attentions, output_hidden_states, return_dict) and returns: BaseModelOutputWithPastAndCrossAttentions, tuple