// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #pragma once #include #include #include #include #include #include "core/framework/execution_provider.h" #include "core/graph/graph_viewer.h" #include "core/common/logging/logging.h" #include "core/platform/telemetry_strings.h" #include "core/platform/telemetry_redaction.h" #ifdef _WIN32 #include #include #include #include "core/platform/tracing.h" #include "core/platform/windows/telemetry.h" #endif namespace onnxruntime { /** Class for managing lookup of the execution providers in a session. Add() must not run concurrently with accessors or iteration, except for GetProviderOptionsSnapshot(), which is explicitly synchronized for ETW capture-state callbacks. */ class ExecutionProviders { public: ExecutionProviders() { #ifdef _WIN32 // Register callback for ETW capture state (rundown) etw_callback_ = onnxruntime::WindowsTelemetry::EtwInternalCallback( [this]( LPCGUID SourceId, ULONG IsEnabled, UCHAR Level, ULONGLONG MatchAnyKeyword, ULONGLONG MatchAllKeyword, PEVENT_FILTER_DESCRIPTOR FilterData, PVOID CallbackContext) { (void)SourceId; (void)Level; (void)MatchAnyKeyword; (void)MatchAllKeyword; (void)FilterData; (void)CallbackContext; // Check if this callback is for capturing state if ((IsEnabled == EVENT_CONTROL_CODE_CAPTURE_STATE) && ((MatchAnyKeyword & static_cast(onnxruntime::logging::ORTTraceLoggingKeyword::Session)) != 0)) { for (const auto& [provider_id, options] : GetProviderOptionsSnapshot()) { LogProviderOptions(provider_id, options, true); } } }); WindowsTelemetry::RegisterInternalCallback(etw_callback_); #endif } ~ExecutionProviders() { #ifdef _WIN32 WindowsTelemetry ::UnregisterInternalCallback(etw_callback_); #endif } common::Status Add(const std::string& provider_id, const std::shared_ptr& p_exec_provider) { // A null provider would crash later when we dereference it (e.g. GetProviderOptions()). // Fail with a clear error instead so the caller can diagnose the missing provider. if (p_exec_provider == nullptr) { auto status = ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "Provider ", provider_id, " is null and cannot be registered."); LOGS_DEFAULT(ERROR) << status.ErrorMessage(); return status; } const auto check_provider_not_registered = [&]() -> common::Status { if (provider_idx_map_.find(provider_id) != provider_idx_map_.end()) { return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "Provider ", provider_id, " has already been registered."); } return Status::OK(); }; common::Status duplicate_status; { std::lock_guard lock(exec_providers_mutex_); duplicate_status = check_provider_not_registered(); } if (!duplicate_status.IsOK()) { LOGS_DEFAULT(ERROR) << duplicate_status.ErrorMessage(); return duplicate_status; } ProviderOptions providerOptions = p_exec_provider->GetProviderOptions(); { std::lock_guard lock(exec_providers_mutex_); // make sure there are no issues before we change any internal data structures duplicate_status = check_provider_not_registered(); if (duplicate_status.IsOK()) { // index that provider will have after insertion auto new_provider_idx = exec_providers_.size(); ORT_IGNORE_RETURN_VALUE(provider_idx_map_.insert({provider_id, new_provider_idx})); // update execution provider options exec_provider_options_[provider_id] = providerOptions; exec_provider_ids_.push_back(provider_id); exec_providers_.push_back(p_exec_provider); } } if (!duplicate_status.IsOK()) { LOGS_DEFAULT(ERROR) << duplicate_status.ErrorMessage(); return duplicate_status; } #ifdef _WIN32 LogProviderOptions(provider_id, providerOptions, false); #endif return Status::OK(); } const IExecutionProvider* Get(const onnxruntime::Node& node) const { return Get(node.GetExecutionProviderType()); } const IExecutionProvider* Get(onnxruntime::ProviderType provider_id) const { auto it = provider_idx_map_.find(provider_id); if (it == provider_idx_map_.end()) { return nullptr; } return exec_providers_[it->second].get(); } IExecutionProvider* Get(onnxruntime::ProviderType provider_id) { auto it = provider_idx_map_.find(provider_id); if (it == provider_idx_map_.end()) { return nullptr; } return exec_providers_[it->second].get(); } bool Empty() const { return exec_providers_.empty(); } size_t NumProviders() const { return exec_providers_.size(); } using const_iterator = typename std::vector>::const_iterator; const_iterator begin() const noexcept { return exec_providers_.cbegin(); } const_iterator end() const noexcept { return exec_providers_.cend(); } const std::vector& GetIds() const { return exec_provider_ids_; } const ProviderOptionsMap& GetAllProviderOptions() const { return exec_provider_options_; } using ProviderOptionsSnapshot = std::vector>; ProviderOptionsSnapshot GetProviderOptionsSnapshot() const { std::lock_guard lock(exec_providers_mutex_); ProviderOptionsSnapshot provider_options_snapshot; provider_options_snapshot.reserve(exec_provider_ids_.size()); for (const auto& provider_id : exec_provider_ids_) { auto it = exec_provider_options_.find(provider_id); if (it != exec_provider_options_.end()) { provider_options_snapshot.emplace_back(provider_id, it->second); } } return provider_options_snapshot; } bool GetCpuProviderWasImplicitlyAdded() const { return cpu_execution_provider_was_implicitly_added_; } void SetCpuProviderWasImplicitlyAdded(bool cpu_execution_provider_was_implicitly_added) { cpu_execution_provider_was_implicitly_added_ = cpu_execution_provider_was_implicitly_added; } private: // Some compilers emit incomprehensive output if this is allowed // with a container that has unique_ptr or something move-only. ORT_DISALLOW_COPY_AND_ASSIGNMENT(ExecutionProviders); // Synchronizes provider registration with ETW capture-state snapshots. mutable std::mutex exec_providers_mutex_; void LogProviderOptions(const std::string& provider_id, const ProviderOptions& options, bool capture_state) { const Env& env = Env::Default(); // Convert ProviderOptions to string for telemetry logging bool truncated = false; const auto provider_options_str = telemetry_detail::FormatTelemetryMap(options, ",", ":", &truncated); env.GetTelemetryProvider().LogProviderOptions( provider_id, ScrubStringForTelemetry(provider_options_str, truncated), capture_state); } std::vector> exec_providers_; std::vector exec_provider_ids_; ProviderOptionsMap exec_provider_options_; // maps for fast lookup of an index into exec_providers_ std::unordered_map provider_idx_map_; // Whether the CPU provider was implicitly added to a session for fallback (true), // or whether it was explicitly added by the caller. bool cpu_execution_provider_was_implicitly_added_ = false; #ifdef _WIN32 WindowsTelemetry::EtwInternalCallback etw_callback_; #endif }; } // namespace onnxruntime