Skip to content

Commit 00c20c8

Browse files
authored
Add telemetry event for auto ep selection (microsoft#25243)
This pull request introduces telemetry enhancements for logging execution provider (EP) auto-selection events in ONNX Runtime. The changes include adding new methods to log EP selection data, updating classes to support session ID retrieval, and integrating telemetry logging into the session provider policy context. ### Telemetry Enhancements: * **Added `LogAutoEpSelection` Method**: Introduced a new virtual method in `Telemetry` and its override in `WindowsTelemetry` to log session-specific EP auto-selection data, including requested and available EP IDs and selection policies. ### Session ID Support: * **Added `GetCurrentSessionId` Method**: Added a method to `InferenceSession` to retrieve the current session ID, enabling session-specific telemetry logging. ### Integration with Provider Policy Context: * **Telemetry Logging in EP Selection**: Integrated telemetry logging into `ProviderPolicyContext::SelectEpsForSession`, capturing requested and available EP IDs, along with the selection policy type, and invoking the telemetry provider's `LogAutoEpSelection` method.
1 parent 8e7ed3a commit 00c20c8

File tree

6 files changed

+124
-0
lines changed

6 files changed

+124
-0
lines changed

onnxruntime/core/platform/telemetry.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,13 @@ void Telemetry::LogDriverInfoEvent(const std::string_view device_class,
107107
ORT_UNUSED_PARAMETER(driver_versions);
108108
}
109109

110+
void Telemetry::LogAutoEpSelection(uint32_t session_id, const std::string& selection_policy,
111+
const std::vector<std::string>& requested_execution_provider_ids,
112+
const std::vector<std::string>& available_execution_provider_ids) const {
113+
ORT_UNUSED_PARAMETER(session_id);
114+
ORT_UNUSED_PARAMETER(selection_policy);
115+
ORT_UNUSED_PARAMETER(requested_execution_provider_ids);
116+
ORT_UNUSED_PARAMETER(available_execution_provider_ids);
117+
}
118+
110119
} // namespace onnxruntime

onnxruntime/core/platform/telemetry.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ class Telemetry {
7878
const std::wstring_view& driver_names,
7979
const std::wstring_view& driver_versions) const;
8080

81+
virtual void LogAutoEpSelection(uint32_t session_id, const std::string& selection_policy,
82+
const std::vector<std::string>& requested_execution_provider_ids,
83+
const std::vector<std::string>& available_execution_provider_ids) const;
84+
8185
private:
8286
ORT_DISALLOW_COPY_ASSIGNMENT_AND_MOVE(Telemetry);
8387
};

onnxruntime/core/platform/windows/telemetry.cc

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,4 +433,49 @@ void WindowsTelemetry::LogDriverInfoEvent(const std::string_view device_class, c
433433
TraceLoggingWideString(driver_versions.data(), "driverVersions"));
434434
}
435435

436+
void WindowsTelemetry::LogAutoEpSelection(uint32_t session_id, const std::string& selection_policy,
437+
const std::vector<std::string>& requested_execution_provider_ids,
438+
const std::vector<std::string>& available_execution_provider_ids) const {
439+
if (global_register_count_ == 0 || enabled_ == false)
440+
return;
441+
442+
// Build requested execution provider string
443+
std::string requested_execution_provider_string;
444+
bool first = true;
445+
for (const auto& ep_id : requested_execution_provider_ids) {
446+
if (first) {
447+
first = false;
448+
} else {
449+
requested_execution_provider_string += ',';
450+
}
451+
requested_execution_provider_string += ep_id;
452+
}
453+
454+
// Build available execution provider string
455+
std::string available_execution_provider_string;
456+
first = true;
457+
for (const auto& ep_id : available_execution_provider_ids) {
458+
if (first) {
459+
first = false;
460+
} else {
461+
available_execution_provider_string += ',';
462+
}
463+
available_execution_provider_string += ep_id;
464+
}
465+
466+
TraceLoggingWrite(telemetry_provider_handle,
467+
"EpAutoSelection",
468+
TraceLoggingBool(true, "UTCReplace_AppSessionGuid"),
469+
TelemetryPrivacyDataTag(PDT_ProductAndServiceUsage),
470+
TraceLoggingKeyword(MICROSOFT_KEYWORD_MEASURES),
471+
TraceLoggingKeyword(static_cast<uint64_t>(onnxruntime::logging::ORTTraceLoggingKeyword::Session)),
472+
TraceLoggingLevel(WINEVENT_LEVEL_INFO),
473+
// Telemetry info
474+
TraceLoggingUInt8(0, "schemaVersion"),
475+
TraceLoggingUInt32(session_id, "sessionId"),
476+
TraceLoggingString(selection_policy.c_str(), "selectionPolicy"),
477+
TraceLoggingString(requested_execution_provider_string.c_str(), "requestedExecutionProviderIds"),
478+
TraceLoggingString(available_execution_provider_string.c_str(), "availableExecutionProviderIds"));
479+
}
480+
436481
} // namespace onnxruntime

onnxruntime/core/platform/windows/telemetry.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ class WindowsTelemetry : public Telemetry {
7171
const std::wstring_view& driver_names,
7272
const std::wstring_view& driver_versions) const override;
7373

74+
void LogAutoEpSelection(uint32_t session_id, const std::string& selection_policy,
75+
const std::vector<std::string>& requested_execution_provider_ids,
76+
const std::vector<std::string>& available_execution_provider_ids) const override;
77+
7478
using EtwInternalCallback = std::function<void(LPCGUID SourceId, ULONG IsEnabled, UCHAR Level,
7579
ULONGLONG MatchAnyKeyword, ULONGLONG MatchAllKeyword,
7680
PEVENT_FILTER_DESCRIPTOR FilterData, PVOID CallbackContext)>;

onnxruntime/core/session/inference_session.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,10 @@ class InferenceSession {
630630
return weight_hash_;
631631
}
632632

633+
uint32_t GetCurrentSessionId() const {
634+
return session_id_;
635+
}
636+
633637
protected:
634638
#if !defined(ORT_MINIMAL_BUILD)
635639

onnxruntime/core/session/provider_policy_context.cc

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,64 @@ Status ProviderPolicyContext::SelectEpsForSession(const Environment& env, const
214214
"No execution providers selected. Please check the device policy and available devices.");
215215
}
216216

217+
// Log telemetry for auto EP selection
218+
{
219+
std::vector<std::string> requested_ep_ids;
220+
requested_ep_ids.reserve(devices_selected.size());
221+
222+
for (const auto* device : devices_selected) {
223+
if (device != nullptr) {
224+
requested_ep_ids.push_back(device->ep_name);
225+
}
226+
}
227+
228+
// Extract available execution provider IDs
229+
std::vector<std::string> available_ep_ids;
230+
available_ep_ids.reserve(execution_devices.size());
231+
for (const auto* device : execution_devices) {
232+
available_ep_ids.push_back(device->ep_name);
233+
}
234+
235+
std::string policy_type;
236+
if (options.value.ep_selection_policy.delegate) {
237+
policy_type = "custom_delegate";
238+
} else {
239+
switch (options.value.ep_selection_policy.policy) {
240+
case OrtExecutionProviderDevicePolicy_DEFAULT:
241+
policy_type = "DEFAULT";
242+
break;
243+
case OrtExecutionProviderDevicePolicy_PREFER_CPU:
244+
policy_type = "PREFER_CPU";
245+
break;
246+
case OrtExecutionProviderDevicePolicy_PREFER_NPU:
247+
policy_type = "PREFER_NPU";
248+
break;
249+
case OrtExecutionProviderDevicePolicy_PREFER_GPU:
250+
policy_type = "PREFER_GPU";
251+
break;
252+
case OrtExecutionProviderDevicePolicy_MAX_PERFORMANCE:
253+
policy_type = "MAX_PERFORMANCE";
254+
break;
255+
case OrtExecutionProviderDevicePolicy_MAX_EFFICIENCY:
256+
policy_type = "MAX_EFFICIENCY";
257+
break;
258+
case OrtExecutionProviderDevicePolicy_MIN_OVERALL_POWER:
259+
policy_type = "MIN_OVERALL_POWER";
260+
break;
261+
default:
262+
policy_type = "UNKNOWN";
263+
break;
264+
}
265+
}
266+
267+
const Env& os_env = Env::Default();
268+
os_env.GetTelemetryProvider().LogAutoEpSelection(
269+
sess.GetCurrentSessionId(),
270+
policy_type,
271+
requested_ep_ids,
272+
available_ep_ids);
273+
}
274+
217275
// Configure the session options for the devices. This updates the SessionOptions in the InferenceSession with any
218276
// EP options that have not been overridden by the user.
219277
ORT_RETURN_IF_ERROR(AddEpDefaultOptionsToSession(sess, devices_selected));

0 commit comments

Comments
 (0)