class_docstr: [`EncoderDecoderModel`] is a generic model class that will be instantiated as a transformer architecture with one
  of the base model classes of the library as encoder and another one as decoder when created with the
  :meth*~transformers.AutoModel.from_pretrained* class method for the encoder and
  :meth*~transformers.AutoModelForCausalLM.from_pretrained* class method for the decoder.
class_docstr_tok: ["[","`","EncoderDecoderModel","`","]","is","a","generic","model","class","that","will","be","instantiated","as","a","transformer","architecture","with","one","of","the","base","model","classes","of","the","library","as","encoder","and","another","one","as","decoder","when","created","with","the",":","meth","*","~transformers.AutoModel.from_pretrained","*","class","method","for","the","encoder","and",":","meth","*","~transformers.AutoModelForCausalLM.from_pretrained","*","class","method","for","the","decoder","."]
docstr: Returns:
  
  Examples:
  
  ```python
  >>> from transformers import EncoderDecoderModel, BertTokenizer
  >>> import torch
  
  >>> tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
  >>> model = EncoderDecoderModel.from_encoder_decoder_pretrained(
  ...     "bert-base-uncased", "bert-base-uncased"
  ... )  # initialize Bert2Bert from pre-trained checkpoints
  
  >>> # training
  >>> model.config.decoder_start_token_id = tokenizer.cls_token_id
  >>> model.config.pad_token_id = tokenizer.pad_token_id
  >>> model.config.vocab_size = model.config.decoder.vocab_size
  
  >>> input_ids = tokenizer("This is a really long text", return_tensors="pt").input_ids
  >>> labels = tokenizer("This is the corresponding summary", return_tensors="pt").input_ids
  >>> outputs = model(input_ids=input_ids, labels=input_ids)
  >>> loss, logits = outputs.loss, outputs.logits
  
  >>> # save and load from pretrained
  >>> model.save_pretrained("bert2bert")
  >>> model = EncoderDecoderModel.from_pretrained("bert2bert")
  
  >>> # generation
  >>> generated = model.generate(input_ids)
  ```
docstr_tok: ["Returns",":","Examples",":","``","`","python",">",">",">","from","transformers","import","EncoderDecoderModel",",","BertTokenizer",">",">",">","import","torch",">",">",">","tokenizer","=","BertTokenizer.from_pretrained","(","``","bert-base-uncased","''",")",">",">",">","model","=","EncoderDecoderModel.from_encoder_decoder_pretrained","(","...","``","bert-base-uncased","''",",","``","bert-base-uncased","''","...",")","#","initialize","Bert2Bert","from","pre-trained","checkpoints",">",">",">","#","training",">",">",">","model.config.decoder_start_token_id","=","tokenizer.cls_token_id",">",">",">","model.config.pad_token_id","=","tokenizer.pad_token_id",">",">",">","model.config.vocab_size","=","model.config.decoder.vocab_size",">",">",">","input_ids","=","tokenizer","(","``","This","is","a","really","long","text","''",",","return_tensors=","''","pt","''",")",".input_ids",">",">",">","labels","=","tokenizer","(","``","This","is","the","corresponding","summary","''",",","return_tensors=","''","pt","''",")",".input_ids",">",">",">","outputs","=","model","(","input_ids=input_ids",",","labels=input_ids",")",">",">",">","loss",",","logits","=","outputs.loss",",","outputs.logits",">",">",">","#","save","and","load","from","pretrained",">",">",">","model.save_pretrained","(","``","bert2bert","''",")",">",">",">","model","=","EncoderDecoderModel.from_pretrained","(","``","bert2bert","''",")",">",">",">","#","generation",">",">",">","generated","=","model.generate","(","input_ids",")","``","`"]
code: def forward(
      self,
      input_ids: Optional[torch.LongTensor] = None,
      attention_mask: Optional[torch.FloatTensor] = None,
      decoder_input_ids: Optional[torch.LongTensor] = None,
      decoder_attention_mask: Optional[torch.BoolTensor] = None,
      encoder_outputs: Optional[Tuple[torch.FloatTensor]] = None,
      past_key_values: Tuple[Tuple[torch.FloatTensor]] = None,
      inputs_embeds: Optional[torch.FloatTensor] = None,
      decoder_inputs_embeds: Optional[torch.FloatTensor] = None,
      labels: 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,
      **kwargs,
  ) -> Union[Tuple, Seq2SeqLMOutput]:
      r"""
      Returns:
  
      Examples:
  
      ```python
      >>> from transformers import EncoderDecoderModel, BertTokenizer
      >>> import torch
  
      >>> tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
      >>> model = EncoderDecoderModel.from_encoder_decoder_pretrained(
      ...     "bert-base-uncased", "bert-base-uncased"
      ... )  # initialize Bert2Bert from pre-trained checkpoints
  
      >>> # training
      >>> model.config.decoder_start_token_id = tokenizer.cls_token_id
      >>> model.config.pad_token_id = tokenizer.pad_token_id
      >>> model.config.vocab_size = model.config.decoder.vocab_size
  
      >>> input_ids = tokenizer("This is a really long text", return_tensors="pt").input_ids
      >>> labels = tokenizer("This is the corresponding summary", return_tensors="pt").input_ids
      >>> outputs = model(input_ids=input_ids, labels=input_ids)
      >>> loss, logits = outputs.loss, outputs.logits
  
      >>> # save and load from pretrained
      >>> model.save_pretrained("bert2bert")
      >>> model = EncoderDecoderModel.from_pretrained("bert2bert")
  
      >>> # generation
      >>> generated = model.generate(input_ids)
      ```"""
      return_dict = (
          return_dict
          if return_dict is not None
          else self.config.use_return_dict
      )
  
      kwargs_encoder = {
          argument: value
          for argument, value in kwargs.items()
          if not argument.startswith("decoder_")
      }
  
      kwargs_decoder = {
          argument[len("decoder_") :]: value
          for argument, value in kwargs.items()
          if argument.startswith("decoder_")
      }
  
      if encoder_outputs is None:
          encoder_outputs = self.encoder(
              input_ids=input_ids,
              attention_mask=attention_mask,
              inputs_embeds=inputs_embeds,
              output_attentions=output_attentions,
              output_hidden_states=output_hidden_states,
              return_dict=return_dict,
              **kwargs_encoder,
          )
      elif isinstance(encoder_outputs, tuple):
          encoder_outputs = BaseModelOutput(*encoder_outputs)
  
      encoder_hidden_states = encoder_outputs[0]
  
      # optionally project encoder_hidden_states
      if (
          self.encoder.config.hidden_size
          != self.decoder.config.hidden_size
          and self.decoder.config.cross_attention_hidden_size is None
      ):
          encoder_hidden_states = self.enc_to_dec_proj(
              encoder_hidden_states
          )
  
      if (labels is not None) and (
          decoder_input_ids is None and decoder_inputs_embeds is None
      ):
          decoder_input_ids = shift_tokens_right(
              labels,
              self.config.pad_token_id,
              self.config.decoder_start_token_id,
          )
  
      # Decode
      decoder_outputs = self.decoder(
          input_ids=decoder_input_ids,
          attention_mask=decoder_attention_mask,
          encoder_hidden_states=encoder_hidden_states,
          encoder_attention_mask=attention_mask,
          inputs_embeds=decoder_inputs_embeds,
          output_attentions=output_attentions,
          output_hidden_states=output_hidden_states,
          use_cache=use_cache,
          past_key_values=past_key_values,
          return_dict=return_dict,
          **kwargs_decoder,
      )
  
      # Compute loss independent from decoder (as some shift the logits inside them)
      loss = None
      if labels is not None:
          warnings.warn(DEPRECATION_WARNING, FutureWarning)
          logits = (
              decoder_outputs.logits
              if return_dict
              else decoder_outputs[0]
          )
          loss_fct = CrossEntropyLoss()
          loss = loss_fct(
              logits.reshape(-1, self.decoder.config.vocab_size),
              labels.view(-1),
          )
  
      if not return_dict:
          if loss is not None:
              return (loss,) + decoder_outputs + encoder_outputs
          else:
              return decoder_outputs + encoder_outputs
  
      return Seq2SeqLMOutput(
          loss=loss,
          logits=decoder_outputs.logits,
          past_key_values=decoder_outputs.past_key_values,
          decoder_hidden_states=decoder_outputs.hidden_states,
          decoder_attentions=decoder_outputs.attentions,
          cross_attentions=decoder_outputs.cross_attentions,
          encoder_last_hidden_state=encoder_outputs.last_hidden_state,
          encoder_hidden_states=encoder_outputs.hidden_states,
          encoder_attentions=encoder_outputs.attentions,
      )
  
imports: import gc
  import os
  import tempfile
  import warnings
  from typing import Optional, Tuple, Union
  import torch
  from torch import nn
  from torch.nn import CrossEntropyLoss
  from ...configuration_utils import PretrainedConfig
  from ...modeling_outputs import BaseModelOutput, Seq2SeqLMOutput
  from ...modeling_utils import PreTrainedModel
  from ...utils import add_start_docstrings, add_start_docstrings_to_model_forward, logging, replace_return_docstrings
  from ..auto.configuration_auto import AutoConfig
  from ..auto.modeling_auto import AutoModel, AutoModelForCausalLM
  from .configuration_encoder_decoder import EncoderDecoderConfig
label_desc: Use image node_id 11 for calling the EncoderDecoderModel obj's underlying member method code with example usage: obj.forward(input_ids, attention_mask, decoder_input_ids, decoder_attention_mask, encoder_outputs, past_key_values, inputs_embeds, decoder_inputs_embeds, labels, use_cache, output_attentions, output_hidden_states, return_dict) and returns: Seq2SeqLMOutput, unknown, unknown