class_docstr: A class containing all functions for auto-regressive text generation, to be used as a mixin in [`PreTrainedModel`].
  
  The class exposes [`~generation_utils.GenerationMixin.generate`], which can be used for:
      - *greedy decoding* by calling [`~generation_utils.GenerationMixin.greedy_search`] if `num_beams=1` and
        `do_sample=False`.
      - *contrastive search* by calling [`~generation_utils.GenerationMixin.contrastive_search`] if `penalty_alpha>0`
        and `top_k>1`
      - *multinomial sampling* by calling [`~generation_utils.GenerationMixin.sample`] if `num_beams=1` and
        `do_sample=True`.
      - *beam-search decoding* by calling [`~generation_utils.GenerationMixin.beam_search`] if `num_beams>1` and
        `do_sample=False`.
      - *beam-search multinomial sampling* by calling [`~generation_utils.GenerationMixin.beam_sample`] if
        `num_beams>1` and `do_sample=True`.
      - *diverse beam-search decoding* by calling [`~generation_utils.GenerationMixin.group_beam_search`], if
        `num_beams>1` and `num_beam_groups>1`.
      - *constrained beam-search decoding* by calling [`~generation_utils.GenerationMixin.constrained_beam_search`],
        if `constraints!=None` or `force_words_ids!=None`.
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_utils.GenerationMixin.generate","`","]",",","which","can","be","used","for",":","-","*","greedy","decoding","*","by","calling","[","`","~generation_utils.GenerationMixin.greedy_search","`","]","if","`","num_beams=1","`","and","`","do_sample=False","`",".","-","*","contrastive","search","*","by","calling","[","`","~generation_utils.GenerationMixin.contrastive_search","`","]","if","`","penalty_alpha",">","0","`","and","`","top_k",">","1","`","-","*","multinomial","sampling","*","by","calling","[","`","~generation_utils.GenerationMixin.sample","`","]","if","`","num_beams=1","`","and","`","do_sample=True","`",".","-","*","beam-search","decoding","*","by","calling","[","`","~generation_utils.GenerationMixin.beam_search","`","]","if","`","num_beams",">","1","`","and","`","do_sample=False","`",".","-","*","beam-search","multinomial","sampling","*","by","calling","[","`","~generation_utils.GenerationMixin.beam_sample","`","]","if","`","num_beams",">","1","`","and","`","do_sample=True","`",".","-","*","diverse","beam-search","decoding","*","by","calling","[","`","~generation_utils.GenerationMixin.group_beam_search","`","]",",","if","`","num_beams",">","1","`","and","`","num_beam_groups",">","1","`",".","-","*","constrained","beam-search","decoding","*","by","calling","[","`","~generation_utils.GenerationMixin.constrained_beam_search","`","]",",","if","`","constraints","!","=None","`","or","`","force_words_ids","!","=None","`","."]
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 "
              f"{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. models with `input_ids` can also make use of `inputs_embeds`
      if self._can_retrieve_inputs_from_name(
          inputs, "inputs_embeds", model_kwargs
      ):
          inputs, input_name = (
              model_kwargs["inputs_embeds"],
              "inputs_embeds",
          )
  
      # 4. Only encoder-decoder models can have non `input_ids` input format
      if (
          not self.config.is_encoder_decoder
          and input_name != "input_ids"
      ):
          raise ValueError(
              f"If {input_name} is passed as model-specific keyword "
              "input then model has to be an encoder-decoder and not a "
              f"{self.__class__.__name__}."
          )
  
      # 5. if `inputs` is still None, try to create `input_ids` from BOS token
      if inputs is None:
          inputs = self._prepare_input_ids_for_generation(
              bos_token_id, model_kwargs.get("encoder_outputs")
          )
  
      return inputs, input_name, model_kwargs
  
imports: import inspect
  import warnings
  from dataclasses import dataclass
  from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Union
  import torch
  import torch.distributed
  from torch import nn
  from flexgen.timer import timers
  from .generation_beam_constraints import Constraint, DisjunctiveConstraint, PhrasalConstraint
  from .generation_beam_search import BeamScorer, BeamSearchScorer, ConstrainedBeamSearchScorer
  from .generation_logits_process import EncoderNoRepeatNGramLogitsProcessor, ExponentialDecayLengthPenalty, ForcedBOSTokenLogitsProcessor, ForcedEOSTokenLogitsProcessor, ForceTokensLogitsProcessor, HammingDiversityLogitsProcessor, InfNanRemoveLogitsProcessor, LogitNormalization, LogitsProcessorList, MinLengthLogitsProcessor, NoBadWordsLogitsProcessor, NoRepeatNGramLogitsProcessor, PrefixConstrainedLogitsProcessor, RepetitionPenaltyLogitsProcessor, SuppressTokensAtBeginLogitsProcessor, SuppressTokensLogitsProcessor, TemperatureLogitsWarper, TopKLogitsWarper, TopPLogitsWarper, TypicalLogitsWarper
  from .generation_stopping_criteria import MaxLengthCriteria, MaxTimeCriteria, StoppingCriteria, StoppingCriteriaList, validate_stopping_criteria
  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 .pytorch_utils import torch_int_div
  from .utils import ModelOutput, logging
label_desc: Use image node_id 1 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