class_docstr: ONNX model with a causal language modeling head for ONNX Runtime inference. This class officially supports bloom, codegen, falcon, gpt2, gpt_bigcode, gpt_neo, gpt_neox, gptj, llama.
class_docstr_tok: ["ONNX","model","with","a","causal","language","modeling","head","for","ONNX","Runtime","inference",".","This","class","officially","supports","bloom",",","codegen",",","falcon",",","gpt2",",","gpt_bigcode",",","gpt_neo",",","gpt_neox",",","gptj",",","llama","."]
code: def __init__(
      self,
      model: onnxruntime.InferenceSession,
      config: "PretrainedConfig",
      use_io_binding: Optional[bool] = None,
      model_save_dir: Optional[
          Union[str, Path, TemporaryDirectory]
      ] = None,
      preprocessors: Optional[List] = None,
      generation_config: Optional[GenerationConfig] = None,
      use_cache: Optional[bool] = None,
      **kwargs,
  ):
      if use_io_binding is None:
          use_io_binding = model.get_providers()[0] in [
              "CPUExecutionProvider",
              "CUDAExecutionProvider",
          ]
  
      super().__init__(
          model,
          config,
          use_io_binding,
          model_save_dir,
          preprocessors,
          **kwargs,
      )
  
      self.num_pkv = 2
      self.normalized_config = (
          NormalizedConfigManager.get_normalized_config_class(
              config.model_type
          )(config)
      )
      self.key_value_input_names = [
          key
          for key in self.inputs_names
          if (".key" in key) or (".value" in key)
      ]
      self.key_value_output_names = [
          key
          for key in self.output_names
          if (".key" in key) or (".value" in key)
      ]
      self.use_cache = len(self.key_value_input_names) > 0
  
      if generation_config is None:
          generation_config = GenerationConfig.from_model_config(config)
      self.generation_config = generation_config
      self.onnx_paths = [self.model_path]
      self.use_merged = "use_cache_branch" in self.inputs_names
      self.model_type = self.config.model_type
  
      self.use_fp16 = False
      for inp in model.get_inputs():
          if (
              inp.name == "past_key_values"
              and inp.type == "tensor(float16)"
          ):
              self.use_fp16 = True
              break
  
      # Reference: https://github.com/huggingface/optimum/pull/1381
      model_type = config.model_type.replace("_", "-")
      if (
          model_type in MODEL_TYPES_REQUIRING_POSITION_IDS
          and "position_ids" not in self.inputs_names
      ):
          logger.warning(
              f"ORTModelForCausalLM loaded a legacy ONNX model with no position_ids input, although this input is required for batched generation for the architecture {model_type}. "
              "We strongly encourage to re-export the model with optimum>=1.14 for position_ids and batched inference support."
          )
  
      if use_cache ^ self.use_cache:
          raise ValueError(
              f"`use_cache` was set to `{use_cache}` but the loaded model only supports `use_cache={self.use_cache}`. "
              f"Please load your current model with `use_cache={self.use_cache}` or export the original model "
              f"once again with `use_cache={use_cache}` when calling the `from_pretrained` method. "
              "To export your model, simply set `export=True`."
          )
  
      if use_io_binding and not use_cache:
          raise ValueError(
              "The parameters combination use_cache=False, use_io_binding=True is not supported. "
              "Please either pass use_cache=True, use_io_binding=True (default), or use_cache=False, use_io_binding=False."
          )
  
imports: import logging
  from pathlib import Path
  from tempfile import TemporaryDirectory
  from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union
  import numpy
  import onnx
  import torch
  from onnx.tools import update_model_dims
  from transformers import AutoModelForCausalLM, GenerationConfig
  from transformers.file_utils import add_end_docstrings, add_start_docstrings_to_model_forward
  from transformers.modeling_outputs import CausalLMOutputWithPast
  import onnxruntime
  from ..exporters.onnx import MODEL_TYPES_REQUIRING_POSITION_IDS, main_export
  from ..onnx.utils import check_model_uses_external_data
  from ..utils import NormalizedConfigManager, check_if_transformers_greater
  from ..utils.modeling_utils import MODEL_TO_PATCH_FOR_PAST
  from ..utils.save_utils import maybe_save_preprocessors
  from .constants import DECODER_MERGED_ONNX_FILE_PATTERN, DECODER_ONNX_FILE_PATTERN, DECODER_WITH_PAST_ONNX_FILE_PATTERN
  from .modeling_ort import ONNX_MODEL_END_DOCSTRING, ORTModel
  from .models.bloom import bloom_convert_to_bloom_cache, bloom_convert_to_standard_cache
  from .utils import ONNX_DECODER_NAME, ONNX_DECODER_WITH_PAST_NAME, ONNX_WEIGHTS_NAME
label_desc: Use image node_id 1 to create a new ORTModelForCausalLM object from inherited base classes: ORTModel, GenerationMixin with example: obj = ORTModelForCausalLM(model, config, use_io_binding, model_save_dir, preprocessors, generation_config, use_cache)