def get_ort_inference_session(
    model_path: Union[Path, str],
    device: Union["Device", str],
    inference_settings: dict[str, Any],
    use_ort_extensions: bool = False,
    device_id: Optional[int] = None,
    external_initializers: Optional[dict[str, "NDArray"]] = None,
):
    """Get an ONNXRuntime inference session.

    :param model_path: Path to the ONNX model file.
    :param inference_settings: Inference settings for the session.
        session_options: dict, optional. Session options for the session.
        execution_provider: list. List of execution providers to use. Can be a list of provider names or a list of
            (provider name, provider options) tuples.
        provider_options: list, optional. List of provider options for the execution providers.
    :param use_ort_extensions: Whether to use onnxruntime-extensions. Default is False.
    :param device_id: Optional device id to use for CUDA or DML execution providers.
    :param external_initializers: Optional external initializers for the session. A dictionary of external initializer
        names and numpy arrays.
    """
    import onnxruntime as ort

    sess_options = ort.SessionOptions()
    if use_ort_extensions:
        # register custom ops for onnxruntime_extensions
        from onnxruntime_extensions import get_library_path

        sess_options.register_custom_ops_library(get_library_path())
    if external_initializers:
        from onnxruntime import OrtValue

        # convert external initializers to OrtValue
        initializer_names = []
        initializer_values = []
        for name, value in external_initializers.items():
            initializer_names.append(name)
            initializer_values.append(OrtValue.ortvalue_from_numpy(value))

        # add external initializers to the session
        sess_options.add_external_initializers(initializer_names, initializer_values)

    logger.debug("inference_settings: %s", inference_settings)

    # session options
    session_options = inference_settings.get("session_options", {})
    inter_op_num_threads = session_options.get("inter_op_num_threads")
    intra_op_num_threads = session_options.get("intra_op_num_threads")
    enable_profiling = session_options.get("enable_profiling", False)
    execution_mode = session_options.get("execution_mode")
    graph_optimization_level = session_options.get("graph_optimization_level")
    extra_session_config = session_options.get("extra_session_config")
    log_severity_level = session_options.get("log_severity_level")
    if enable_profiling:
        sess_options.enable_profiling = True
    if inter_op_num_threads:
        sess_options.inter_op_num_threads = inter_op_num_threads
    if intra_op_num_threads:
        sess_options.intra_op_num_threads = intra_op_num_threads
    if execution_mode is not None:
        if execution_mode == 0:
            sess_options.execution_mode = ort.ExecutionMode.ORT_SEQUENTIAL
        elif execution_mode == 1:
            sess_options.execution_mode = ort.ExecutionMode.ORT_PARALLEL
    if graph_optimization_level is not None:
        # level can be 0, 1, 2, 3
        sess_options.graph_optimization_level = ort.GraphOptimizationLevel(graph_optimization_level)
    if extra_session_config:
        for key, value in extra_session_config.items():
            sess_options.add_session_config_entry(key, value)
    if log_severity_level is not None:
        sess_options.log_severity_level = log_severity_level

    # execution providers and provider options
    providers, provider_options = check_and_normalize_provider_args(
        inference_settings.get("execution_provider"),
        inference_settings.get("provider_options"),
        ort.get_available_providers(),
    )
    for idx, provider in enumerate(providers):
        if provider in ["CUDAExecutionProvider", "DmlExecutionProvider"] and device_id is not None:
            provider_options[idx]["device_id"] = str(device_id)
        elif provider == "QNNExecutionProvider" and "backend_path" not in provider_options[idx]:
            # add backend_path for QNNExecutionProvider
            provider_options[idx]["backend_path"] = "QnnHtp.dll"
    logger.debug("Normalized providers: %s, provider_options: %s", providers, provider_options)

    # dml specific settings
    if len(providers) >= 1 and providers[0] == "DmlExecutionProvider":
        sess_options.enable_mem_pattern = False

    sess_kwargs = {}
    if ort_supports_ep_devices():
        initialize_inference_session_options(
            sess_options, device, providers, provider_options, inference_settings.get("provider_selection_policy")
        )
    else:
        sess_kwargs.update({"providers": providers, "provider_options": provider_options})

    # create session
    session = ort.InferenceSession(str(model_path), sess_options=sess_options, **sess_kwargs)
    check_ort_fallback(session, providers)

    # set tuning results for tunable operators (currently only for ROCM EP)
    tuning_op_result = inference_settings.get("tuning_op_result")
    if tuning_op_result:
        assert isinstance(tuning_op_result, list)
        session.set_tuning_results(tuning_op_result)
    return session
Get an ONNXRuntime inference session.

    :param model_path: Path to the ONNX model file.
    :param inference_settings: Inference settings for the session.
        session_options: dict, optional. Session options for the session.
        execution_provider: list. List of execution providers to use. Can be a list of provider names or a list of
            (provider name, provider options) tuples.
        provider_options: list, optional. List of provider options for the execution providers.
    :param use_ort_extensions: Whether to use onnxruntime-extensions. Default is False.
    :param device_id: Optional device id to use for CUDA or DML execution providers.
    :param external_initializers: Optional external initializers for the session. A dictionary of external initializer
        names and numpy arrays.
    
olive/common/ort_inference.py