Skip to content

Commit 43b39ff

Browse files
skottmckayankitm3k
authored andcommitted
Reduce default logger usage (microsoft#23030)
### Description <!-- Describe your changes. --> We have use cases where multiple sessions are created concurrently. Minimizing the usage of the default logger is important for these scenarios. Wire through the session logger to as many places as possible. The EP logger can also be used once the session is created (can't be used during EP construction/kernel registration but can be used in GetCapability and Compile). ### Motivation and Context <!-- - Why is this change required? What problem does it solve? - If it fixes an open issue, please link to the issue here. --> Improve logging when there are concurrent sessions.
1 parent 71bdd0f commit 43b39ff

File tree

62 files changed

+389
-243
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+389
-243
lines changed

include/onnxruntime/core/framework/kernel_registry.h

+10-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
#include "core/framework/op_kernel.h"
99

1010
namespace onnxruntime {
11+
namespace logging {
12+
class Logger;
13+
}
1114

1215
using KernelCreateMap = std::multimap<std::string, KernelCreateInfo>;
1316
using KernelDefHashes = std::vector<std::pair<std::string, HashValue>>;
@@ -33,6 +36,7 @@ class KernelRegistry {
3336
// Kernel matching uses the types from the node and the kernel_type_str_resolver.
3437
Status TryFindKernel(const Node& node, ProviderType exec_provider,
3538
const IKernelTypeStrResolver& kernel_type_str_resolver,
39+
const logging::Logger& logger,
3640
const KernelCreateInfo** out) const;
3741

3842
// map of type constraint name to required type
@@ -42,6 +46,7 @@ class KernelRegistry {
4246
// Kernel matching uses the explicit type constraint name to required type map in type_constraints.
4347
Status TryFindKernel(const Node& node, ProviderType exec_provider,
4448
const TypeConstraintMap& type_constraints,
49+
const logging::Logger& logger,
4550
const KernelCreateInfo** out) const;
4651

4752
/**
@@ -61,13 +66,15 @@ class KernelRegistry {
6166
std::string_view domain,
6267
int version,
6368
const KernelRegistry::TypeConstraintMap& type_constraints,
69+
const logging::Logger& logger,
6470
const KernelCreateInfo** out) const;
6571

6672
static bool HasImplementationOf(const KernelRegistry& r, const Node& node,
6773
ProviderType exec_provider,
68-
const IKernelTypeStrResolver& kernel_type_str_resolver) {
74+
const IKernelTypeStrResolver& kernel_type_str_resolver,
75+
const logging::Logger& logger) {
6976
const KernelCreateInfo* info;
70-
Status st = r.TryFindKernel(node, exec_provider, kernel_type_str_resolver, &info);
77+
Status st = r.TryFindKernel(node, exec_provider, kernel_type_str_resolver, logger, &info);
7178
return st.IsOK();
7279
}
7380

@@ -83,6 +90,7 @@ class KernelRegistry {
8390
Status TryFindKernelImpl(const Node& node, ProviderType exec_provider,
8491
const IKernelTypeStrResolver* kernel_type_str_resolver,
8592
const TypeConstraintMap* type_constraints,
93+
const logging::Logger& logger,
8694
const KernelCreateInfo** out) const;
8795

8896
// Check whether the types of inputs/outputs of the given node match the extra

include/onnxruntime/core/optimizer/graph_transformer_utils.h

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ InlinedVector<std::unique_ptr<GraphTransformer>> GenerateTransformers(
5353
TransformerLevel level,
5454
const SessionOptions& session_options,
5555
const IExecutionProvider& execution_provider /*required by constant folding*/,
56+
const logging::Logger& logger,
5657
const InlinedHashSet<std::string>& rules_and_transformers_to_disable = {},
5758
concurrency::ThreadPool* intra_op_thread_pool = nullptr,
5859
std::unordered_map<std::string, std::unique_ptr<Tensor>>* p_buffered_tensors = nullptr);
@@ -84,6 +85,7 @@ InlinedVector<std::unique_ptr<GraphTransformer>> GenerateTransformersForMinimalB
8485
const SessionOptions& session_options,
8586
const SatApplyContextVariant& apply_context,
8687
const IExecutionProvider& cpu_execution_provider,
88+
const logging::Logger& logger,
8789
const InlinedHashSet<std::string>& rules_and_transformers_to_disable = {},
8890
concurrency::ThreadPool* intra_op_thread_pool = nullptr,
8991
std::unordered_map<std::string, std::unique_ptr<Tensor>>* p_buffered_tensors = nullptr);

onnxruntime/core/framework/allocation_planner.cc

+43-36
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ class PlannerImpl {
138138
const SubgraphsKernelCreateInfoMaps& subgraphs_kernel_create_info_maps,
139139
const InlinedHashMap<OrtValueName, OrtDevice>& outer_scope_node_arg_to_location_map,
140140
const OrtValueNameIdxMap& ort_value_name_idx_map,
141-
const ISequentialPlannerContext& context, SequentialExecutionPlan& plan)
141+
const ISequentialPlannerContext& context, SequentialExecutionPlan& plan,
142+
const logging::Logger& logger)
142143
: context_(&context),
143144
plan_(plan),
144145
parent_node_(parent_node),
@@ -148,14 +149,15 @@ class PlannerImpl {
148149
kernel_create_info_map_(kernel_create_info_map),
149150
subgraphs_kernel_create_info_maps_(subgraphs_kernel_create_info_maps),
150151
outer_scope_node_arg_to_location_map_(outer_scope_node_arg_to_location_map),
151-
ort_value_name_idx_map_(ort_value_name_idx_map) {}
152+
ort_value_name_idx_map_(ort_value_name_idx_map),
153+
logger_(logger) {
154+
}
152155

153156
Status CreatePlan(
154157
#ifdef ORT_ENABLE_STREAM
155158
const IStreamCommandHandleRegistry& stream_handle_registry,
156159
#endif
157-
const PathString& partition_config_file,
158-
const logging::Logger& logger);
160+
const PathString& partition_config_file);
159161

160162
private:
161163
gsl::not_null<const ISequentialPlannerContext*> context_;
@@ -183,6 +185,12 @@ class PlannerImpl {
183185
InlinedHashMap<onnxruntime::NodeIndex, InlinedHashSet<onnxruntime::NodeIndex>> dependence_graph_;
184186
InlinedHashMap<onnxruntime::OrtValueIndex, onnxruntime::NodeIndex> value_node_map_;
185187

188+
// logger_ is not currently used in a minimal build
189+
#if defined(ORT_MINIMAL_BUILD) && !defined(ORT_EXTENDED_MINIMAL_BUILD)
190+
[[maybe_unused]]
191+
#endif
192+
const logging::Logger& logger_;
193+
186194
// OrtValueInfo: Auxiliary information about an OrtValue used only during plan-generation:
187195
struct OrtValueInfo {
188196
const onnxruntime::NodeArg* p_def_site; // the (unique) NodeArg corresponding to the MLValue
@@ -213,6 +221,7 @@ class PlannerImpl {
213221
FreeBufferInfo(OrtValueIndex ort_value, size_t dealloc_point)
214222
: ml_value(ort_value), deallocate_point(dealloc_point) {}
215223
};
224+
216225
// freelist_ : a list of ml-values whose buffers are free to be reused, sorted by when
217226
// they became free (more recently freed earlier in the list).
218227
std::list<FreeBufferInfo> freelist_;
@@ -225,7 +234,8 @@ class PlannerImpl {
225234
}
226235

227236
int& UseCount(OrtValueIndex n) {
228-
ORT_ENFORCE(n >= 0 && static_cast<size_t>(n) < ort_value_info_.size(), "invalid value index: ", n, " against size ", ort_value_info_.size());
237+
ORT_ENFORCE(n >= 0 && static_cast<size_t>(n) < ort_value_info_.size(),
238+
"invalid value index: ", n, " against size ", ort_value_info_.size());
229239
return ort_value_info_[n].usecount;
230240
}
231241
int& UseCount(const OrtValueName& name) { return UseCount(Index(name)); }
@@ -335,9 +345,9 @@ class PlannerImpl {
335345
// we cannot.
336346
const Node* producer_node = graph.GetProducerNode(p_input_arg->Name());
337347
if (producer_node && HasExternalOutputs(*producer_node)) {
338-
LOGS_DEFAULT(VERBOSE) << "Be noted Node " << node.Name() << " is reusing input buffer of node "
339-
<< producer_node->Name() << " which has external outputs. "
340-
<< "Be cautious the reuse MUST be a read-only usage.";
348+
LOGS(logger_, VERBOSE) << "Be noted Node " << node.Name() << " is reusing input buffer of node "
349+
<< producer_node->Name() << " which has external outputs. "
350+
<< "Be cautious the reuse MUST be a read-only usage.";
341351
}
342352
#endif
343353
*reusable_input = Index(p_input_arg->Name());
@@ -361,9 +371,9 @@ class PlannerImpl {
361371
// we cannot.
362372
const Node* producer_node = graph.GetProducerNode(p_input_arg->Name());
363373
if (producer_node && HasExternalOutputs(*producer_node)) {
364-
LOGS_DEFAULT(VERBOSE) << "Be noted Node " << node.Name() << " is reusing input buffer of node "
365-
<< producer_node->Name() << " which has external outputs. "
366-
<< "Be cautious the reuse MUST be a read-only usage.";
374+
LOGS(logger_, VERBOSE) << "Be noted Node " << node.Name() << " is reusing input buffer of node "
375+
<< producer_node->Name() << " which has external outputs. "
376+
<< "Be cautious the reuse MUST be a read-only usage.";
367377
}
368378
#endif
369379
*reusable_input = Index(p_input_arg->Name());
@@ -397,8 +407,8 @@ class PlannerImpl {
397407
}
398408
} else {
399409
#if !defined(ORT_MINIMAL_BUILD) || defined(ORT_EXTENDED_MINIMAL_BUILD)
400-
LOGS_DEFAULT(VERBOSE) << "Node " << node.Name() << " cannot reuse input buffer for node "
401-
<< producer_node->Name() << " as it has external outputs";
410+
LOGS(logger_, VERBOSE) << "Node " << node.Name() << " cannot reuse input buffer for node "
411+
<< producer_node->Name() << " as it has external outputs";
402412
#endif
403413
}
404414
}
@@ -448,8 +458,8 @@ class PlannerImpl {
448458
return true;
449459
} else {
450460
#if !defined(ORT_MINIMAL_BUILD) || defined(ORT_EXTENDED_MINIMAL_BUILD)
451-
LOGS_DEFAULT(VERBOSE) << "Node " << node.Name() << " cannot reuse strided output buffer for node "
452-
<< producer_node->Name() << " as it has external outputs.";
461+
LOGS(logger_, VERBOSE) << "Node " << node.Name() << " cannot reuse strided output buffer for node "
462+
<< producer_node->Name() << " as it has external outputs.";
453463
#endif
454464
}
455465
}
@@ -1198,9 +1208,9 @@ class PlannerImpl {
11981208
// Otherwise, we cannot reuse the buffer.
11991209
const Node* producer_node = graph_viewer.GetProducerNode(p_input_arg->Name());
12001210
if (producer_node && HasExternalOutputs(*producer_node)) {
1201-
LOGS_DEFAULT(VERBOSE) << "Be noted input buffer " << p_output_arg->Name() << " of node "
1202-
<< producer_node->Name() << " which has external outputs is reused. "
1203-
<< "Be cautious the reuse MUST be a read-only usage.";
1211+
LOGS(logger_, VERBOSE) << "Be noted input buffer " << p_output_arg->Name() << " of node "
1212+
<< producer_node->Name() << " which has external outputs is reused. "
1213+
<< "Be cautious the reuse MUST be a read-only usage.";
12041214
}
12051215
#endif
12061216

@@ -1241,9 +1251,9 @@ class PlannerImpl {
12411251
// Otherwise, we cannot reuse the buffer.
12421252
const Node* producer_node = graph_viewer.GetProducerNode(p_input_arg->Name());
12431253
if (producer_node && HasExternalOutputs(*producer_node)) {
1244-
LOGS_DEFAULT(VERBOSE) << "Be noted input buffer " << p_output_arg->Name() << " of node "
1245-
<< producer_node->Name() << " which has external outputs is reused. "
1246-
<< "Be cautious the reuse MUST be a read-only usage.";
1254+
LOGS(logger_, VERBOSE) << "Be noted input buffer " << p_output_arg->Name() << " of node "
1255+
<< producer_node->Name() << " which has external outputs is reused. "
1256+
<< "Be cautious the reuse MUST be a read-only usage.";
12471257
}
12481258
#endif
12491259

@@ -1290,8 +1300,8 @@ class PlannerImpl {
12901300
}
12911301
} else {
12921302
#if !defined(ORT_MINIMAL_BUILD) || defined(ORT_EXTENDED_MINIMAL_BUILD)
1293-
LOGS_DEFAULT(VERBOSE) << "Node " << node->Name() << " cannot reuse input buffer for node "
1294-
<< producer_node->Name() << " as it has external outputs";
1303+
LOGS(logger_, VERBOSE) << "Node " << node->Name() << " cannot reuse input buffer for node "
1304+
<< producer_node->Name() << " as it has external outputs";
12951305
#endif
12961306
}
12971307
}
@@ -1869,8 +1879,7 @@ class PlannerImpl {
18691879
}
18701880

18711881
#ifndef ORT_ENABLE_STREAM
1872-
void PartitionIntoStreams(const logging::Logger& /*logger*/,
1873-
const ExecutionProviders& /*execution_providers*/,
1882+
void PartitionIntoStreams(const ExecutionProviders& /*execution_providers*/,
18741883
const PathString& /*partition_config_file*/) {
18751884
if (graph_viewer_.NumberOfNodes() > 0) {
18761885
stream_nodes_.push_back({});
@@ -1915,11 +1924,11 @@ class PlannerImpl {
19151924

19161925
#else
19171926

1918-
void
1919-
PartitionIntoStreams(const logging::Logger& logger, const ExecutionProviders& execution_providers,
1920-
const PathString& partition_config_file) {
1921-
auto partitioner = IGraphPartitioner::CreateGraphPartitioner(logger, partition_config_file);
1922-
auto status = partitioner->PartitionGraph(graph_viewer_, execution_providers, stream_nodes_, context_->GetExecutionOrder());
1927+
void PartitionIntoStreams(const ExecutionProviders& execution_providers,
1928+
const PathString& partition_config_file) {
1929+
auto partitioner = IGraphPartitioner::CreateGraphPartitioner(logger_, partition_config_file);
1930+
auto status = partitioner->PartitionGraph(graph_viewer_, execution_providers, stream_nodes_,
1931+
context_->GetExecutionOrder());
19231932
ORT_ENFORCE(status.IsOK(), status.ErrorMessage());
19241933
plan_.node_stream_map_.resize(SafeInt<size_t>(graph_viewer_.MaxNodeIndex()) + 1);
19251934
for (size_t i = 0; i < stream_nodes_.size(); ++i) {
@@ -2282,10 +2291,9 @@ Status PlannerImpl::CreatePlan(
22822291
#ifdef ORT_ENABLE_STREAM
22832292
const IStreamCommandHandleRegistry& stream_handle_registry,
22842293
#endif
2285-
const PathString& partition_config_file,
2286-
const logging::Logger& logger) {
2294+
const PathString& partition_config_file) {
22872295
// 1. partition graph into streams
2288-
PartitionIntoStreams(logger, execution_providers_, this->parent_node_ ? PathString{} : partition_config_file);
2296+
PartitionIntoStreams(execution_providers_, parent_node_ ? PathString{} : partition_config_file);
22892297

22902298
// 2. initialize the plan based on stream partition result
22912299
int num_ml_values = ort_value_name_idx_map_.MaxIdx() + 1;
@@ -2354,14 +2362,13 @@ Status SequentialPlanner::CreatePlan(
23542362
PlannerImpl planner(parent_node, graph_viewer, outer_scope_node_args, providers,
23552363
kernel_create_info_map, subgraphs_kernel_create_info_maps,
23562364
outer_scope_node_arg_to_location_map,
2357-
ort_value_name_idx_map, context, *plan);
2365+
ort_value_name_idx_map, context, *plan, logger);
23582366

23592367
return planner.CreatePlan(
23602368
#ifdef ORT_ENABLE_STREAM
23612369
stream_handle_registry,
23622370
#endif
2363-
partition_config_file,
2364-
logger);
2371+
partition_config_file);
23652372
}
23662373

23672374
#ifdef ORT_ENABLE_STREAM

onnxruntime/core/framework/fallback_cpu_capability.cc

+6-5
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ static bool IsSmallInitializer(const onnxruntime::GraphViewer& graph, const Node
4141

4242
std::unordered_set<NodeIndex> GetCpuPreferredNodes(const onnxruntime::GraphViewer& graph,
4343
const IExecutionProvider::IKernelLookup& kernel_lookup,
44-
gsl::span<const NodeIndex> tentative_nodes) {
44+
gsl::span<const NodeIndex> tentative_nodes,
45+
const logging::Logger& logger) {
4546
// automatic conversion from const std::vector&
4647
const auto& ordered_nodes = graph.GetNodesInTopologicalOrder();
4748
InlinedVector<size_t> node_id_to_order_map(graph.MaxNodeIndex());
@@ -83,7 +84,7 @@ std::unordered_set<NodeIndex> GetCpuPreferredNodes(const onnxruntime::GraphViewe
8384
auto consumer_nodes = graph.GetConsumerNodes(node_arg.Name());
8485
for (auto& consumer_node : consumer_nodes) {
8586
candidates.push(consumer_node->Index());
86-
LOGS_DEFAULT(INFO) << "Candidate for fallback CPU execution: " << consumer_node->Name();
87+
LOGS(logger, INFO) << "Candidate for fallback CPU execution: " << consumer_node->Name();
8788
}
8889
}
8990
return Status::OK();
@@ -159,9 +160,9 @@ std::unordered_set<NodeIndex> GetCpuPreferredNodes(const onnxruntime::GraphViewe
159160

160161
if (place_in_cpu) {
161162
cpu_nodes.insert(cur);
162-
LOGS_DEFAULT(INFO) << "ORT optimization- Force fallback to CPU execution for node: " << node->Name()
163-
<< " because the CPU execution path is deemed faster than overhead involved with execution on other EPs "
164-
<< " capable of executing this node";
163+
LOGS(logger, INFO) << "ORT optimization- Force fallback to CPU execution for node: " << node->Name()
164+
<< " because the CPU execution path is deemed faster than overhead involved with execution "
165+
"on other EPs capable of executing this node";
165166
for (auto* output : node->OutputDefs()) {
166167
cpu_output_args.insert(output);
167168
}

onnxruntime/core/framework/fallback_cpu_capability.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
#include "core/graph/graph_viewer.h"
1010

1111
namespace onnxruntime {
12+
namespace logging {
13+
class Logger;
14+
}
1215

1316
/**
1417
Returns a list of nodes that are preferred on CPU.
@@ -19,6 +22,7 @@ namespace onnxruntime {
1922
*/
2023
std::unordered_set<NodeIndex> GetCpuPreferredNodes(const GraphViewer& graph,
2124
const IExecutionProvider::IKernelLookup& kernel_lookup,
22-
gsl::span<const NodeIndex> tentative_nodes);
25+
gsl::span<const NodeIndex> tentative_nodes,
26+
const logging::Logger& logger);
2327

2428
} // namespace onnxruntime

0 commit comments

Comments
 (0)