class_docstr: [`SpeechEncoderDecoderModel`] 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: ["[","`","SpeechEncoderDecoderModel","`","]","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 SpeechEncoderDecoderModel, Wav2Vec2Processor
  >>> from datasets import load_dataset
  >>> import torch
  
  >>> processor = Wav2Vec2Processor.from_pretrained("facebook/wav2vec2-xls-r-300m-en-to-15")
  >>> model = SpeechEncoderDecoderModel.from_pretrained("facebook/wav2vec2-xls-r-300m-en-to-15")
  
  >>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
  
  >>> input_values = processor(ds[0]["audio"]["array"], return_tensors="pt").input_values
  >>> # Inference: Translate English speech to German
  >>> generated = model.generate(input_values)
  >>> decoded = processor.batch_decode(generated, skip_special_tokens=True)[0]
  >>> decoded
  'Mr. Quilter ist der Apostel der Mittelschicht und wir freuen uns, sein Evangelium willkommen heißen zu können.'
  
  >>> # Training: Train model on English transcription
  >>> labels = processor(text=ds[0]["text"], return_tensors="pt").input_ids
  
  >>> loss = model(input_values, labels=labels).loss
  >>> loss.backward()
  ```
docstr_tok: ["Returns",":","Examples",":","``","`","python",">",">",">","from","transformers","import","SpeechEncoderDecoderModel",",","Wav2Vec2Processor",">",">",">","from","datasets","import","load_dataset",">",">",">","import","torch",">",">",">","processor","=","Wav2Vec2Processor.from_pretrained","(","``","facebook\/wav2vec2-xls-r-300m-en-to-15","''",")",">",">",">","model","=","SpeechEncoderDecoderModel.from_pretrained","(","``","facebook\/wav2vec2-xls-r-300m-en-to-15","''",")",">",">",">","ds","=","load_dataset","(","``","hf-internal-testing\/librispeech_asr_dummy","''",",","``","clean","''",",","split=","''","validation","''",")",">",">",">","input_values","=","processor","(","ds","[","0","]","[","``","audio","''","]","[","``","array","''","]",",","return_tensors=","''","pt","''",")",".input_values",">",">",">","#","Inference",":","Translate","English","speech","to","German",">",">",">","generated","=","model.generate","(","input_values",")",">",">",">","decoded","=","processor.batch_decode","(","generated",",","skip_special_tokens=True",")","[","0","]",">",">",">","decoded","'Mr",".","Quilter","ist","der","Apostel","der","Mittelschicht","und","wir","freuen","uns",",","sein","Evangelium","willkommen","hei\u00dfen","zu","k\u00f6nnen",".","'",">",">",">","#","Training",":","Train","model","on","English","transcription",">",">",">","labels","=","processor","(","text=ds","[","0","]","[","``","text","''","]",",","return_tensors=","''","pt","''",")",".input_ids",">",">",">","loss","=","model","(","input_values",",","labels=labels",")",".loss",">",">",">","loss.backward","(",")","``","`"]
code: def forward(
      self,
      inputs=None,
      attention_mask=None,
      decoder_input_ids=None,
      decoder_attention_mask=None,
      encoder_outputs=None,
      past_key_values=None,
      decoder_inputs_embeds=None,
      labels=None,
      use_cache=None,
      output_attentions=None,
      output_hidden_states=None,
      input_values=None,
      input_features=None,
      return_dict=None,
      **kwargs,
  ):
      r"""
      Returns:
  
      Examples:
  
      ```python
      >>> from transformers import SpeechEncoderDecoderModel, Wav2Vec2Processor
      >>> from datasets import load_dataset
      >>> import torch
  
      >>> processor = Wav2Vec2Processor.from_pretrained("facebook/wav2vec2-xls-r-300m-en-to-15")
      >>> model = SpeechEncoderDecoderModel.from_pretrained("facebook/wav2vec2-xls-r-300m-en-to-15")
  
      >>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
  
      >>> input_values = processor(ds[0]["audio"]["array"], return_tensors="pt").input_values
      >>> # Inference: Translate English speech to German
      >>> generated = model.generate(input_values)
      >>> decoded = processor.batch_decode(generated, skip_special_tokens=True)[0]
      >>> decoded
      'Mr. Quilter ist der Apostel der Mittelschicht und wir freuen uns, sein Evangelium willkommen heißen zu können.'
  
      >>> # Training: Train model on English transcription
      >>> labels = processor(text=ds[0]["text"], return_tensors="pt").input_ids
  
      >>> loss = model(input_values, labels=labels).loss
      >>> loss.backward()
      ```"""
      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:
          if inputs is None:
              if (
                  input_values is not None
                  and input_features is not None
              ):
                  raise ValueError(
                      "You cannot specify both input_values and input_features at the same time"
                  )
              elif input_values is not None:
                  inputs = input_values
              elif input_features is not None:
                  inputs = input_features
              else:
                  raise ValueError(
                      "You have to specify either input_values or input_features"
                  )
  
          encoder_outputs = self.encoder(
              inputs,
              attention_mask=attention_mask,
              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_output_dim != 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
          )
  
      # compute correct encoder attention mask
      if attention_mask is not None:
          encoder_attention_mask = (
              self.encoder._get_feature_vector_attention_mask(
                  encoder_hidden_states.shape[1], attention_mask
              )
          )
      else:
          encoder_attention_mask = None
  
      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=encoder_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:
          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.reshape(-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_hidden_states,
          encoder_hidden_states=encoder_outputs.hidden_states,
          encoder_attentions=encoder_outputs.attentions,
      )
  
imports: from typing import Optional
  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_speech_encoder_decoder import SpeechEncoderDecoderConfig
label_desc: Use image node_id 10 for calling the SpeechEncoderDecoderModel obj's underlying member method code with example usage: obj.forward(inputs, attention_mask, decoder_input_ids, decoder_attention_mask, encoder_outputs, past_key_values, decoder_inputs_embeds, labels, use_cache, output_attentions, output_hidden_states, input_values, input_features, return_dict) and returns: Seq2SeqLMOutput, unknown, unknown