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: This function extracts the model-specific `inputs` for generation.
docstr_tok: ["This","function","extracts","the","model-specific","`","inputs","`","for","generation","."]
code: def _prepare_model_inputs(
      self,
      inputs: Optional[torch.Tensor] = None,
      bos_token_id: Optional[int] = None,
      model_kwargs: Optional[Dict[str, torch.Tensor]] = None,
  ) -> Tuple[torch.Tensor, Optional[str], Dict[str, torch.Tensor]]:
      """
      This function extracts the model-specific `inputs` for generation.
      """
      # 1. retrieve all kwargs that are non-None or non-model input related.
      # some encoder-decoder models have different names for model and encoder
      if (
          self.config.is_encoder_decoder
          and hasattr(self, "encoder")
          and self.encoder.main_input_name != self.main_input_name
      ):
          input_name = self.encoder.main_input_name
      else:
          input_name = self.main_input_name
  
      model_kwargs = {
          k: v
          for k, v in model_kwargs.items()
          if v is not None or k != input_name
      }
  
      # 2. check whether model_input_name is passed as kwarg
      # if yes and `inputs` is None use kwarg inputs
      inputs_kwarg = model_kwargs.pop(input_name, None)
      if inputs_kwarg is not None and inputs is not None:
          raise ValueError(
              f"`inputs`: {inputs}` were passed alongside {input_name} which is not allowed. "
              f"Make sure to either pass {inputs} or {input_name}=..."
          )
      elif inputs_kwarg is not None:
          inputs = inputs_kwarg
  
      # 3. In the presence of `inputs_embeds` for text models:
      # - decoder-only models should complain if the user attempts to pass `inputs_embeds`, but the model
      # doesn't have its forwarding implemented. `inputs_embeds` is kept in `model_kwargs` and can coexist with
      # input_ids (`inputs_embeds` will be used in the 1st generation step, as opposed to `input_ids`)
      # - encoder-decoder models should complain if the user attempts to pass `inputs_embeds` and `input_ids`, and
      # pull the former to inputs. It will be used in place of `input_ids` to get the encoder hidden states.
      if input_name == "input_ids" and "inputs_embeds" in model_kwargs:
          if not self.config.is_encoder_decoder:
              has_inputs_embeds_forwarding = "inputs_embeds" in set(
                  inspect.signature(
                      self.prepare_inputs_for_generation
                  ).parameters.keys()
              )
              if not has_inputs_embeds_forwarding:
                  raise ValueError(
                      f"You passed `inputs_embeds` to `.generate()`, but the model class {self.__class__.__name__} "
                      "doesn't have its forwarding implemented. See the GPT2 implementation for an example "
                      "(https://github.com/huggingface/transformers/pull/21405), and feel free to open a PR with it!"
                  )
              # In this case, `input_ids` is moved to the `model_kwargs`, so a few automations (like the creation of
              # the attention mask) can rely on the actual model input.
              model_kwargs[
                  "input_ids"
              ] = self._maybe_initialize_input_ids_for_generation(
                  inputs, bos_token_id, model_kwargs=model_kwargs
              )
          else:
              if inputs is not None:
                  raise ValueError(
                      "You passed `inputs_embeds` and `input_ids` to `.generate()`. Please pick one."
                  )
          inputs, input_name = (
              model_kwargs["inputs_embeds"],
              "inputs_embeds",
          )
  
      # 4. if `inputs` is still None, try to create `input_ids` from BOS token
      inputs = self._maybe_initialize_input_ids_for_generation(
          inputs, bos_token_id, model_kwargs
      )
      return inputs, input_name, 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 2 for calling the GenerationMixin obj's underlying member method code with example usage: obj._prepare_model_inputs(inputs, bos_token_id, model_kwargs) and returns: inputs,  input_name,  model_kwargs