forked from microsoft/onnxruntime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Some Shape Related Fusions (microsoft#19832)
This PR adds below shape related fusions, which is helpful for some transformer models: - ShapeInputMerge is to merge all Shape nodes' input NodeArg to a single one (the 1st one on topo order) if they have the same shape value. This helps CSE fusion to merge more nodes. - CSE fusion to support scalar tensor as attribute value. This is mainly to support ConstantOfShape node.
- Loading branch information
Showing
7 changed files
with
291 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#include "core/optimizer/shape_input_merge.h" | ||
|
||
#include "core/graph/graph_utils.h" | ||
|
||
namespace onnxruntime { | ||
|
||
namespace { | ||
std::string GetShapeString(const NodeArg* input_arg) { | ||
auto shape = input_arg->Shape(); | ||
if (!shape) return ""; | ||
std::stringstream ss; | ||
ss << "["; | ||
for (int i = 0; i < shape->dim_size(); ++i) { | ||
if (i != 0) ss << ","; | ||
auto dim = shape->dim(i); | ||
if (dim.has_dim_value()) { | ||
ss << std::to_string(dim.dim_value()); | ||
} else if (dim.has_dim_param()) { | ||
ss << "'" << dim.dim_param() << "'"; | ||
} else { | ||
return ""; | ||
} | ||
} | ||
ss << "]"; | ||
return ss.str(); | ||
} | ||
|
||
} // namespace | ||
|
||
Status ShapeInputMerge::ApplyImpl(Graph& graph, bool& modified, int graph_level, const logging::Logger& logger) const { | ||
GraphViewer graph_viewer(graph); | ||
const auto& node_topology_list = graph_viewer.GetNodesInTopologicalOrder(); | ||
InlinedHashMap<std::string, InlinedVector<Node*>> input_hash_to_nodes; | ||
for (auto node_index : node_topology_list) { | ||
auto* p_node = graph.GetNode(node_index); | ||
if (!p_node) continue; // we removed the node as part of an earlier fusion | ||
ORT_RETURN_IF_ERROR(Recurse(*p_node, modified, graph_level, logger)); | ||
if (!graph_utils::IsSupportedOptypeVersionAndDomain(*p_node, "Shape", {1, 13, 15, 19, 21}) || | ||
!graph_utils::IsSupportedProvider(*p_node, GetCompatibleExecutionProviders())) { | ||
continue; | ||
} | ||
std::string shape_str = GetShapeString(p_node->InputDefs()[0]); | ||
if (shape_str.empty()) continue; | ||
if (input_hash_to_nodes.find(shape_str) == input_hash_to_nodes.end()) { | ||
input_hash_to_nodes[shape_str] = InlinedVector<Node*>(); | ||
} | ||
input_hash_to_nodes[shape_str].emplace_back(p_node); | ||
} | ||
|
||
// All Shape nodes are processed in topological order, so we can safely merge the inputs to the first node's input. | ||
for (auto& kv : input_hash_to_nodes) { | ||
if (kv.second.size() < 2) continue; | ||
NodeArg* first_input_arg = kv.second[0]->MutableInputDefs()[0]; | ||
bool is_first_input_arg_graph_input = graph.IsInputsIncludingInitializers(first_input_arg); | ||
for (size_t i = 1; i < kv.second.size(); ++i) { | ||
Node* p_node = kv.second[i]; | ||
const NodeArg* input_arg = p_node->InputDefs()[0]; | ||
if (p_node->InputDefs()[0]->Name() == first_input_arg->Name()) continue; | ||
if (!graph.IsInputsIncludingInitializers(input_arg)) { | ||
const Node::EdgeEnd& input_edge = *p_node->InputEdgesBegin(); | ||
graph.RemoveEdge(input_edge.GetNode().Index(), p_node->Index(), input_edge.GetSrcArgIndex(), 0); | ||
} | ||
graph_utils::ReplaceNodeInput(*p_node, 0, *first_input_arg); | ||
if (!is_first_input_arg_graph_input) { | ||
const Node::EdgeEnd& first_input_edge = *kv.second[0]->InputEdgesBegin(); | ||
graph.AddEdge(first_input_edge.GetNode().Index(), p_node->Index(), first_input_edge.GetSrcArgIndex(), 0); | ||
} | ||
modified = true; | ||
} | ||
} | ||
|
||
return Status::OK(); | ||
} | ||
|
||
} // namespace onnxruntime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#pragma once | ||
|
||
#include "core/optimizer/graph_transformer.h" | ||
|
||
namespace onnxruntime { | ||
|
||
/** | ||
@Class ShapeInputMerge | ||
Merge all shape inputs having same shape value to a single shape input. | ||
This change will not affect the performance, but it open chances for CSE fusion to merge nodes. | ||
*/ | ||
class ShapeInputMerge : public GraphTransformer { | ||
public: | ||
ShapeInputMerge(const InlinedHashSet<std::string_view>& compatible_execution_providers = {}) noexcept | ||
: GraphTransformer("ShapeInputMerge", compatible_execution_providers) {} | ||
|
||
Status ApplyImpl(Graph& graph, bool& modified, int graph_level, const logging::Logger& logger) const override; | ||
}; | ||
|
||
} // namespace onnxruntime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.