// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #pragma once #include "core/graph/basic_types.h" #include "core/framework/allocator.h" #include "core/framework/data_types.h" #include "core/framework/framework_common.h" #include "core/framework/iexecutor.h" #include "core/framework/session_state.h" #include "core/framework/session_options.h" namespace onnxruntime { class ExecutionProviders; struct FeedsFetchesInfo; class FeedsFetchesManager; struct MLValueCopyInfo; class Graph; class KernelDef; class KernelRegistryManager; class IExecutionProvider; class Node; #if !defined(ORT_MINIMAL_BUILD) #endif class Tensor; struct KernelCreateInfo; #ifdef ENABLE_TRAINING struct PartialGraphExecutionState; typedef InlinedHashMap OrtValueCache; typedef std::shared_ptr OrtValueCachePtr; #endif namespace logging { class Logger; } namespace utils { /// // Do the placement new for strings on pre-allocated buffer // `elements` times. /// /// /// void ConstructStrings(void* p_data, int64_t elements); /// /// Destroy std::string objects in the contiquous chunk of memory /// by explicitly invoking ~string(); /// /// /// void DestroyStrings(void* p_data, int64_t elements); const std::string& GetNodeInputProviderType(const SessionState::NodeInfo& info); // return true if the execution provider is CPU based (meaning no copies to device are required) bool ProviderIsCpuBased(const IExecutionProvider& provider); bool IsMemcpyNode(const Node& node); // Returns true if src memory can satisfy tgt's requirements without a data copy. // HOST_ACCESSIBLE -> DEFAULT is valid (device can access HOST_ACCESSIBLE memory directly). // DEFAULT -> HOST_ACCESSIBLE is NOT valid (CPU cannot read device-only memory). bool CanSourceSatisfyTarget(const OrtDevice& src, const OrtDevice& tgt); common::Status CopyOneInputAcrossDevices(const SessionState& session_state, const std::string& input_name, const OrtValue& orig_mlvalue, OrtValue& new_mlvalue); // Searches the allocation plan from the session_state to find the OrtMemoryInfo for the value 'name'. const OrtDevice& FindDeviceForValue(const SessionState& session_state, std::string_view name); // Initialize the feed and fetch copy info using session_state. // Determines the device that each graph input that will be fed will be consumed on, // and the device that each graph output that will be fetched will be created on. common::Status InitializeFeedFetchCopyInfo(const SessionState& session_state, FeedsFetchesManager& feeds_fetches_manager); // Finalize the feed and fetch copy info using session_state and the device and location information from the feeds // and fetches that will be used in graph execution. void FinalizeFeedFetchCopyInfo(FeedsFetchesManager& feeds_fetches_manager, gsl::span feed_locations, gsl::span fetch_alloc_info); // Execute the main graph. The feed_fetches_manager will be finalized based on the provided feeds and fetches. common::Status ExecuteGraph(const SessionState& session_state, FeedsFetchesManager& feeds_fetches_manager, gsl::span feeds, std::vector& fetches, ExecutionMode execution_mode, const bool& terminate_flag, const logging::Logger& logger, #ifdef ORT_ENABLE_STREAM DeviceStreamCollectionHolder& device_stream_collection_holder, #endif bool only_execute_path_to_fetches = false, Stream* parent_stream = nullptr, profiling::Profiler* run_profiler = nullptr); common::Status ExecuteGraph(const SessionState& session_state, FeedsFetchesManager& feeds_fetches_manager, gsl::span feeds, std::vector& fetches, ExecutionMode execution_mode, const RunOptions& run_options, #ifdef ORT_ENABLE_STREAM DeviceStreamCollectionHolder& device_stream_collection_holder, #endif const logging::Logger& logger, profiling::Profiler* run_profiler = nullptr); #ifdef ENABLE_TRAINING common::Status ExecutePartialGraph(const SessionState& session_state, FeedsFetchesManager& feeds_fetches_manager, std::vector& feeds, std::vector& fetches, const logging::Logger& logger, PartialGraphExecutionState& state, const OrtValueCachePtr& cache, const bool& terminate_flag, int32_t partial_graph_index, Stream* parent_stream); #endif // Execute a subgraph. The feeds_fetches_manager should have been finalized prior to calling this function. // See IControlFlowNode::SetupSubgraphExecutionInfo usage in the control flow kernels. common::Status ExecuteSubgraph(const SessionState& session_state, const FeedsFetchesManager& feeds_fetches_manager, gsl::span feeds, std::vector& fetches, const std::unordered_map& fetch_allocators, ExecutionMode execution_mode, const bool& terminate_flag, const logging::Logger& logger, Stream* parent_stream, /*when this is enabled, we will sync the parent stream to make sure the subgraph fetches is complete. this is mainly used when the parent kernel depends on the CPU value of the subgraph fetches, i.e. the loop condition*/ bool sync_subgraph_fetches = false, profiling::Profiler* run_profiler = nullptr); bool IsInputOnCpu(const Node& node, const KernelCreateInfo* p_kci, size_t index); bool IsOutputOnCpu(const Node& node, const KernelCreateInfo* p_kci, size_t index); template constexpr ONNXTensorElementDataType GetONNXTensorElementDataType() { return ONNX_TENSOR_ELEMENT_DATA_TYPE_UNDEFINED; } template <> constexpr ONNXTensorElementDataType GetONNXTensorElementDataType() { return ONNX_TENSOR_ELEMENT_DATA_TYPE_BOOL; } template <> constexpr ONNXTensorElementDataType GetONNXTensorElementDataType() { return ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING; } template <> constexpr ONNXTensorElementDataType GetONNXTensorElementDataType() { return ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT; } template <> constexpr ONNXTensorElementDataType GetONNXTensorElementDataType() { return ONNX_TENSOR_ELEMENT_DATA_TYPE_DOUBLE; } template <> constexpr ONNXTensorElementDataType GetONNXTensorElementDataType() { return ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT16; } template <> constexpr ONNXTensorElementDataType GetONNXTensorElementDataType() { return ONNX_TENSOR_ELEMENT_DATA_TYPE_BFLOAT16; } template <> constexpr ONNXTensorElementDataType GetONNXTensorElementDataType() { return ONNX_TENSOR_ELEMENT_DATA_TYPE_INT8; } template <> constexpr ONNXTensorElementDataType GetONNXTensorElementDataType() { return ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT8; } template <> constexpr ONNXTensorElementDataType GetONNXTensorElementDataType() { return ONNX_TENSOR_ELEMENT_DATA_TYPE_INT16; } template <> constexpr ONNXTensorElementDataType GetONNXTensorElementDataType() { return ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT16; } template <> constexpr ONNXTensorElementDataType GetONNXTensorElementDataType() { return ONNX_TENSOR_ELEMENT_DATA_TYPE_INT32; } template <> constexpr ONNXTensorElementDataType GetONNXTensorElementDataType() { return ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT32; } template <> constexpr ONNXTensorElementDataType GetONNXTensorElementDataType() { return ONNX_TENSOR_ELEMENT_DATA_TYPE_INT64; } template <> constexpr ONNXTensorElementDataType GetONNXTensorElementDataType() { return ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT64; } #if !defined(DISABLE_FLOAT8_TYPES) template <> constexpr ONNXTensorElementDataType GetONNXTensorElementDataType() { return ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT8E4M3FN; } template <> constexpr ONNXTensorElementDataType GetONNXTensorElementDataType() { return ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT8E4M3FNUZ; } template <> constexpr ONNXTensorElementDataType GetONNXTensorElementDataType() { return ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT8E5M2; } template <> constexpr ONNXTensorElementDataType GetONNXTensorElementDataType() { return ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT8E5M2FNUZ; } template <> constexpr ONNXTensorElementDataType GetONNXTensorElementDataType() { return ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT8E8M0; } #endif template <> constexpr ONNXTensorElementDataType GetONNXTensorElementDataType() { return ONNX_TENSOR_ELEMENT_DATA_TYPE_INT4; } template <> constexpr ONNXTensorElementDataType GetONNXTensorElementDataType() { return ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT4; } template <> constexpr ONNXTensorElementDataType GetONNXTensorElementDataType() { return ONNX_TENSOR_ELEMENT_DATA_TYPE_INT2; } template <> constexpr ONNXTensorElementDataType GetONNXTensorElementDataType() { return ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT2; } #if !defined(DISABLE_FLOAT4_TYPES) template <> constexpr ONNXTensorElementDataType GetONNXTensorElementDataType() { return ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT4E2M1; } #endif int32_t ONNXTensorElementDataTypeToProtoTensorType(ONNXTensorElementDataType); #ifdef ENABLE_TRAINING common::Status VerifyInputTensorsAllocatedContiguously(OpKernelContext* context); #endif } // namespace utils } // namespace onnxruntime