class_docstr: A class containing all functions for auto-regressive text generation, to be used as a mixin in [`PreTrainedModel`].
  
  The class exposes [`~generation.GenerationMixin.generate`], which can be used for:
      - *greedy decoding* by calling [`~generation.GenerationMixin.greedy_search`] if `num_beams=1` and
        `do_sample=False`
      - *contrastive search* by calling [`~generation.GenerationMixin.contrastive_search`] if `penalty_alpha>0` and
        `top_k>1`
      - *multinomial sampling* by calling [`~generation.GenerationMixin.sample`] if `num_beams=1` and
        `do_sample=True`
      - *beam-search decoding* by calling [`~generation.GenerationMixin.beam_search`] if `num_beams>1` and
        `do_sample=False`
      - *beam-search multinomial sampling* by calling [`~generation.GenerationMixin.beam_sample`] if `num_beams>1`
        and `do_sample=True`
      - *diverse beam-search decoding* by calling [`~generation.GenerationMixin.group_beam_search`], if `num_beams>1`
        and `num_beam_groups>1`
      - *constrained beam-search decoding* by calling [`~generation.GenerationMixin.constrained_beam_search`], if
        `constraints!=None` or `force_words_ids!=None`
  
  You do not need to call any of the above methods directly. Pass custom parameter values to 'generate' instead. To
  learn more about decoding strategies refer to the [text generation strategies guide](../generation_strategies).
class_docstr_tok: ["A","class","containing","all","functions","for","auto-regressive","text","generation",",","to","be","used","as","a","mixin","in","[","`","PreTrainedModel","`","]",".","The","class","exposes","[","`","~generation.GenerationMixin.generate","`","]",",","which","can","be","used","for",":","-","*","greedy","decoding","*","by","calling","[","`","~generation.GenerationMixin.greedy_search","`","]","if","`","num_beams=1","`","and","`","do_sample=False","`","-","*","contrastive","search","*","by","calling","[","`","~generation.GenerationMixin.contrastive_search","`","]","if","`","penalty_alpha",">","0","`","and","`","top_k",">","1","`","-","*","multinomial","sampling","*","by","calling","[","`","~generation.GenerationMixin.sample","`","]","if","`","num_beams=1","`","and","`","do_sample=True","`","-","*","beam-search","decoding","*","by","calling","[","`","~generation.GenerationMixin.beam_search","`","]","if","`","num_beams",">","1","`","and","`","do_sample=False","`","-","*","beam-search","multinomial","sampling","*","by","calling","[","`","~generation.GenerationMixin.beam_sample","`","]","if","`","num_beams",">","1","`","and","`","do_sample=True","`","-","*","diverse","beam-search","decoding","*","by","calling","[","`","~generation.GenerationMixin.group_beam_search","`","]",",","if","`","num_beams",">","1","`","and","`","num_beam_groups",">","1","`","-","*","constrained","beam-search","decoding","*","by","calling","[","`","~generation.GenerationMixin.constrained_beam_search","`","]",",","if","`","constraints","!","=None","`","or","`","force_words_ids","!","=None","`","You","do","not","need","to","call","any","of","the","above","methods","directly",".","Pass","custom","parameter","values","to","'generate","'","instead",".","To","learn","more","about","decoding","strategies","refer","to","the","[","text","generation","strategies","guide","]","(","..","\/generation_strategies",")","."]
docstr: Prepares `decoder_input_ids` for generation with encoder-decoder models
docstr_tok: ["Prepares","`","decoder_input_ids","`","for","generation","with","encoder-decoder","models"]
code: def _prepare_decoder_input_ids_for_generation(
      self,
      batch_size: int,
      model_input_name: str,
      model_kwargs: Dict[str, torch.Tensor],
      decoder_start_token_id: int = None,
      bos_token_id: int = None,
      device: torch.device = None,
  ) -> Tuple[torch.LongTensor, Dict[str, torch.Tensor]]:
      """Prepares `decoder_input_ids` for generation with encoder-decoder models"""
      # 1. Check whether the user has defined `decoder_input_ids` manually. To facilitate in terms of input naming,
      # we also allow the user to pass it under `input_ids`, if the encoder does not use it as the main input.
      if (
          model_kwargs is not None
          and "decoder_input_ids" in model_kwargs
      ):
          decoder_input_ids = model_kwargs.pop("decoder_input_ids")
      elif (
          "input_ids" in model_kwargs
          and model_input_name != "input_ids"
      ):
          decoder_input_ids = model_kwargs.pop("input_ids")
      else:
          decoder_input_ids = None
  
      # 2. Encoder-decoder models expect the `decoder_input_ids` to start with a special token. Let's ensure that.
      decoder_start_token_id = self._get_decoder_start_token_id(
          decoder_start_token_id, bos_token_id
      )
      if device is None:
          device = self.device
      decoder_input_ids_start = (
          torch.ones((batch_size, 1), dtype=torch.long, device=device)
          * decoder_start_token_id
      )
  
      # no user input -> use decoder_start_token_id as decoder_input_ids
      if decoder_input_ids is None:
          decoder_input_ids = decoder_input_ids_start
      # exception: Donut checkpoints have task-specific decoder starts and don't expect a BOS token
      elif (
          self.config.model_type == "vision-encoder-decoder"
          and "donut" in self.name_or_path.lower()
      ):
          pass
      # user input but doesn't start with decoder_start_token_id -> prepend decoder_start_token_id (and adjust
      # decoder_attention_mask if provided)
      elif (
          (decoder_input_ids[:, 0] != decoder_start_token_id)
          .all()
          .item()
      ):
          decoder_input_ids = torch.cat(
              [decoder_input_ids_start, decoder_input_ids], dim=-1
          )
          if "decoder_attention_mask" in model_kwargs:
              decoder_attention_mask = model_kwargs[
                  "decoder_attention_mask"
              ]
              decoder_attention_mask = torch.cat(
                  (
                      torch.ones_like(decoder_attention_mask)[:, :1],
                      decoder_attention_mask,
                  ),
                  dim=-1,
              )
              model_kwargs[
                  "decoder_attention_mask"
              ] = decoder_attention_mask
  
      return decoder_input_ids, model_kwargs
  
imports: import copy
  import inspect
  import warnings
  from dataclasses import dataclass
  from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Tuple, Union
  import torch
  import torch.distributed
  from torch import nn
  from ..cache_utils import Cache, DynamicCache
  from ..integrations.deepspeed import is_deepspeed_zero3_enabled
  from ..modeling_outputs import CausalLMOutputWithPast, Seq2SeqLMOutput
  from ..models.auto import MODEL_FOR_CAUSAL_IMAGE_MODELING_MAPPING, MODEL_FOR_CAUSAL_LM_MAPPING, MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING, MODEL_FOR_VISION_2_SEQ_MAPPING
  from ..utils import ExplicitEnum, ModelOutput, is_accelerate_available, logging
  from .beam_constraints import DisjunctiveConstraint, PhrasalConstraint
  from .beam_search import BeamScorer, BeamSearchScorer, ConstrainedBeamSearchScorer
  from .candidate_generator import AssistedCandidateGenerator, CandidateGenerator, PromptLookupCandidateGenerator, _crop_past_key_values, _prepare_attention_mask, _prepare_token_type_ids
  from .configuration_utils import GenerationConfig
  from .logits_process import EncoderNoRepeatNGramLogitsProcessor, EncoderRepetitionPenaltyLogitsProcessor, EpsilonLogitsWarper, EtaLogitsWarper, ExponentialDecayLengthPenalty, ForcedBOSTokenLogitsProcessor, ForcedEOSTokenLogitsProcessor, ForceTokensLogitsProcessor, HammingDiversityLogitsProcessor, InfNanRemoveLogitsProcessor, LogitNormalization, LogitsProcessorList, MinLengthLogitsProcessor, MinNewTokensLengthLogitsProcessor, NoBadWordsLogitsProcessor, NoRepeatNGramLogitsProcessor, PrefixConstrainedLogitsProcessor, RepetitionPenaltyLogitsProcessor, SequenceBiasLogitsProcessor, SuppressTokensAtBeginLogitsProcessor, SuppressTokensLogitsProcessor, TemperatureLogitsWarper, TopKLogitsWarper, TopPLogitsWarper, TypicalLogitsWarper, UnbatchedClassifierFreeGuidanceLogitsProcessor
  from .stopping_criteria import MaxLengthCriteria, MaxTimeCriteria, StoppingCriteria, StoppingCriteriaList, validate_stopping_criteria
label_desc: Use image node_id 6 for calling the GenerationMixin obj's underlying member method code with example usage: obj._prepare_decoder_input_ids_for_generation(batch_size, model_input_name, model_kwargs, decoder_start_token_id, bos_token_id, device) and returns: decoder_input_ids,  model_kwargs