Avoid hashing the operator type in the GraphViewer priority node check unless the string has a chance of matching. (#7972)

* Avoid hashing the operator type in the GraphViewer priority node check unless the string has a chance of matching.

Below are perf numbers from a test that loads 16 models multiple times. I was checking that some unrelated changes didn't have unexpected perf cost and found the PriorityNodeCompare overwhelmed any contribution the other changes were making.

*Before*

CPU Time:74.678s

CPU Time for relevant Top Hotspots
std::_Hash_array_representation<char> 20.834s
onnxruntime::PriorityNodeCompare::IsHighPri 7.589s
onnxruntime::Graph::KahnsTopologicalSort 4.487s

*After*

CPU Time:47.103s

CPU Time for relevant Top Hotspots
onnxruntime::Graph::KahnsTopologicalSort 4.465s
onnxruntime::PriorityNodeCompare::IsHighPri 2.873s
This commit is contained in:
Scott McKay 2021-06-12 14:11:33 +10:00 committed by GitHub
parent 6e134c2cc3
commit 96ead2be91
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -18,15 +18,19 @@ bool NodeCompare::operator()(const Node* n1, const Node* n2) const {
#if !defined(ORT_MINIMAL_BUILD)
struct PriorityNodeCompare {
inline bool IsHighPri(const Node* n) const {
static const std::unordered_set<std::string> high_pri_ops = {"Shape", "Size"};
return high_pri_ops.find(n->OpType()) != high_pri_ops.end();
// local statics so we can compare std::strings in the checks
static const std::string shape_op("Shape");
static const std::string size_op("Size");
const auto& op_type = n->OpType();
return op_type == shape_op || op_type == size_op;
}
// Used for std::priority_queue
// If return false, n1 will be output first
// If return true, n2 will be output first
bool operator()(const Node* n1, const Node* n2) const {
// nodes in global high priorty list will be output first
// nodes in global high priority list will be output first
if (IsHighPri(n1) != IsHighPri(n2)) {
return IsHighPri(n2);
}