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 _from_pretrained(
      cls,
      model_id: Union[str, Path],
      config: "PretrainedConfig",
      use_auth_token: Optional[Union[bool, str]] = None,
      revision: Optional[str] = None,
      force_download: bool = False,
      cache_dir: Optional[str] = None,
      file_name: Optional[str] = None,
      subfolder: str = "",
      use_cache: bool = True,
      local_files_only: bool = False,
      use_merged: Optional[bool] = None,
      provider: str = "CPUExecutionProvider",
      session_options: Optional[onnxruntime.SessionOptions] = None,
      provider_options: Optional[Dict[str, Any]] = None,
      use_io_binding: Optional[bool] = None,
      model_save_dir: Optional[
          Union[str, Path, TemporaryDirectory]
      ] = None,
      **kwargs,
  ) -> "ORTModelForCausalLM":
      model_path = Path(model_id)
  
      # We do not implement the logic for use_cache=False, use_merged=True
      if use_cache is False:
          if use_merged is True:
              raise ValueError(
                  "The parameters combination use_cache=False, use_merged=True is not supported."
                  " To use a merged decoder, past key values must be used."
              )
          use_merged = False
  
      decoder_name = (
          "decoder_file_name"
          if use_cache
          else "decoder_with_past_file_name"
      )
      decoder_file_name = kwargs.pop(decoder_name, None)
  
      if decoder_file_name is not None:
          logger.warning(
              f"The `{decoder_name}` argument is deprecated, please use `file_name` instead."
          )
          file_name = file_name or decoder_file_name
  
      if file_name is None:
          decoder_path = None
          # We use `is not False` here to include two cases: use_merged = None (in which case we auto-detect it),
          # and use_merged = True (explicitely specified by the user)
          if use_merged is not False:
              try:
                  decoder_path = (
                      ORTModelForCausalLM.infer_onnx_filename(
                          model_id,
                          [DECODER_MERGED_ONNX_FILE_PATTERN],
                          argument_name=None,
                          subfolder=subfolder,
                          use_auth_token=use_auth_token,
                          revision=revision,
                      )
                  )
                  use_merged = True
                  file_name = decoder_path.name
              except FileNotFoundError as e:
                  if use_merged is True:
                      raise FileNotFoundError(
                          "The parameter `use_merged=True` was passed to ORTModelForCausalLM.from_pretrained()"
                          " but no ONNX file for a merged decoder could be found in"
                          f" {str(Path(model_id, subfolder))}, with the error: {e}"
                      )
                  use_merged = False
  
          if use_merged is False:
              pattern = (
                  DECODER_WITH_PAST_ONNX_FILE_PATTERN
                  if use_cache
                  else DECODER_ONNX_FILE_PATTERN
              )
              # exclude decoder file for first iteration
              decoder_path = ORTModelForCausalLM.infer_onnx_filename(
                  model_id,
                  [r"^((?!decoder).)*.onnx", pattern],
                  argument_name=None,
                  subfolder=subfolder,
                  use_auth_token=use_auth_token,
                  revision=revision,
              )
              file_name = decoder_path.name
  
          if (
              file_name == ONNX_DECODER_WITH_PAST_NAME
              and config.model_type in MODEL_TO_PATCH_FOR_PAST
          ):
              raise ValueError(
                  f"ONNX Runtime inference using {ONNX_DECODER_WITH_PAST_NAME} has been deprecated for {config.model_type} architecture. Please re-export your model with optimum>=1.14.0 or set use_cache=False. For details about the deprecation, please refer to https://github.com/huggingface/optimum/releases/tag/v1.14.0."
              )
  
          regular_file_names = []
          for name in [
              ONNX_WEIGHTS_NAME,
              ONNX_DECODER_WITH_PAST_NAME
              if use_cache
              else ONNX_DECODER_NAME,
          ]:
              regular_file_names += ORTModelForCausalLM._generate_regular_names_for_filename(
                  name
              )
  
          if file_name not in regular_file_names:
              logger.warning(
                  f"The ONNX file {file_name} is not a regular name used in optimum.onnxruntime that are {regular_file_names}, the "
                  f"{cls.__name__} might not behave as expected."
              )
  
      model_cache_path, preprocessors = cls._cached_file(
          model_path=model_path,
          use_auth_token=use_auth_token,
          revision=revision,
          force_download=force_download,
          cache_dir=cache_dir,
          file_name=file_name,
          subfolder=subfolder,
          local_files_only=local_files_only,
      )
      new_model_save_dir = model_cache_path.parent
  
      # model_save_dir can be provided in kwargs as a TemporaryDirectory instance, in which case we want to keep it
      # instead of the path only.
      if model_save_dir is None:
          model_save_dir = new_model_save_dir
  
      # Since v1.7.0 decoder with past models have fixed sequence length of 1
      # To keep these models compatible we set this dimension to dynamic
      onnx_model = onnx.load(
          str(model_cache_path), load_external_data=False
      )
      model_uses_external_data = check_model_uses_external_data(
          onnx_model
      )
  
      if model_uses_external_data:
          onnx_model = onnx.load(
              str(model_cache_path), load_external_data=True
          )
  
      input_dims = {
          node.name: [
              dim.dim_value or dim.dim_param
              for dim in node.type.tensor_type.shape.dim
          ]
          for node in onnx_model.graph.input
      }
      if input_dims["input_ids"][1] == 1:
          input_dims["input_ids"][1] = "sequence_length"
          output_dims = {
              node.name: [
                  dim.dim_value or dim.dim_param
                  for dim in node.type.tensor_type.shape.dim
              ]
              for node in onnx_model.graph.output
          }
          output_dims["logits"][1] = "sequence_length"
          onnx_model = update_model_dims.update_inputs_outputs_dims(
              onnx_model, input_dims, output_dims
          )
  
          onnx.save(
              onnx_model,
              str(model_cache_path),
              save_as_external_data=model_uses_external_data,
              all_tensors_to_one_file=True,
              location=model_cache_path.name + "_data",
              size_threshold=0,
          )
      del onnx_model
  
      model = ORTModel.load_model(
          model_cache_path,
          provider=provider,
          session_options=session_options,
          provider_options=provider_options,
      )
  
      if config.model_type == "bloom":
          init_cls = ORTBloomForCausalLM
      elif config.model_type == "falcon":
          init_cls = ORTFalconForCausalLM
      elif config.model_type == "mpt":
          init_cls = ORTMPTForCausalLM
      elif config.model_type == "opt":
          init_cls = ORTOPTForCausalLM
      elif config.model_type == "gpt_bigcode":
          init_cls = ORTGPTBigCodeForCausalLM
      else:
          init_cls = ORTModelForCausalLM
  
      return init_cls(
          model=model,
          config=config,
          use_io_binding=use_io_binding,
          model_save_dir=model_save_dir,
          preprocessors=preprocessors,
          use_cache=use_cache,
      )
  
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 4 for calling the ORTModelForCausalLM obj's underlying member method code with example usage: obj._from_pretrained(cls, model_id, config, use_auth_token, revision, force_download, cache_dir, file_name, subfolder, use_cache, local_files_only, use_merged, provider, session_options, provider_options, use_io_binding, model_save_dir) and returns: init_cls