Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge GatherToSplitFusion and #19218 to a General Fusion #19600

Merged
merged 4 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions onnxruntime/core/optimizer/gather_slice_fusion.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace onnxruntime {

bool GatherSliceToSplitFusion::IsSupportedGather(const Graph& graph, const Node& node, int64_t& index,
int64_t& axis, int64_t& indices_n_dims) const {
int64_t& axis) const {
if (!graph_utils::IsSupportedOptypeVersionAndDomain(node, "Gather", {1, 11, 13}) ||
!graph_utils::IsSupportedProvider(node, GetCompatibleExecutionProviders())) {
return false;
Expand All @@ -23,7 +23,7 @@ bool GatherSliceToSplitFusion::IsSupportedGather(const Graph& graph, const Node&

if (!indices_init) return false;

if (indices_init->data_type() != ONNX_NAMESPACE::TensorProto::INT64) return false;
if (indices_init->data_type() != ONNX_NAMESPACE::TensorProto::INT64 || indices_init->dims_size() != 1) return false;

// get the index value
Initializer init_const(*indices_init, graph.ModelPath());
Expand All @@ -36,8 +36,6 @@ bool GatherSliceToSplitFusion::IsSupportedGather(const Graph& graph, const Node&
auto& axis_attr = attrs.at("axis");
if (utils::HasInt(axis_attr)) axis = axis_attr.i();
}

indices_n_dims = indices_init->dims_size();
return true;
}

Expand Down Expand Up @@ -204,7 +202,6 @@ Status GatherSliceToSplitFusion::ApplyImpl(Graph& graph, bool& modified, int gra
bool can_fuse = true;
bool first_edge = true;
int64_t split_axis = 0;
int64_t indices_n_dims = -1;

// Fuse 2 Gathers and 1 slice to Split
// Get those outputs as Split outputs
Expand All @@ -215,10 +212,10 @@ Status GatherSliceToSplitFusion::ApplyImpl(Graph& graph, bool& modified, int gra

// find the nodes to be merged
for (auto consumer : consumers) {
int64_t index, axis, dims;
int64_t index, axis;
InlinedVector<int64_t> starts, ends, axes, steps;

bool IsSupportedGatherOps = IsSupportedGather(graph, *consumer, index, axis, dims);
bool IsSupportedGatherOps = IsSupportedGather(graph, *consumer, index, axis);
bool IsSupportedSliceOps = IsSupportedSlice(graph, *consumer, starts, ends, axes, steps);

if ((!consumer || consumer->InputDefs()[0] != node_arg) ||
Expand All @@ -227,14 +224,6 @@ Status GatherSliceToSplitFusion::ApplyImpl(Graph& graph, bool& modified, int gra
}

if (IsSupportedGatherOps) {
if (indices_n_dims == -1) {
indices_n_dims = dims;
} else if (indices_n_dims != dims) {
// Not the same number of dimensions (0 or 1) for all scalar indices.
can_fuse = false;
break;
}

if (axis < 0) axis += rank;

if (first_edge) {
Expand Down
3 changes: 1 addition & 2 deletions onnxruntime/core/optimizer/gather_slice_fusion.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ Fuse (2 Gather nodes + 1 Slice) to 1 split node.

class GatherSliceToSplitFusion : public GraphTransformer {
private:
bool IsSupportedGather(const Graph& graph, const Node& node, int64_t& index, int64_t& axis,
int64_t& indices_n_dims) const;
bool IsSupportedGather(const Graph& graph, const Node& node, int64_t& index, int64_t& axis) const;

bool IsSupportedSlice(const Graph& graph, const Node& node,
InlinedVector<int64_t>& starts,
Expand Down
6 changes: 3 additions & 3 deletions onnxruntime/test/optimizer/graph_transform_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7652,7 +7652,7 @@ TEST_F(GraphTransformationTests, GatherSliceToSplitFusion) {
builder.AddNode("Reshape", {data_arg, reshape_arg}, {reshape_out});

// Create Gather-1 Ops
auto* gather_index_1 = builder.MakeInitializer<int64_t>({}, {static_cast<int64_t>(-2)});
auto* gather_index_1 = builder.MakeInitializer<int64_t>({1}, {static_cast<int64_t>(-2)});
auto* gather_out_1 = builder.MakeIntermediate<float>({{2, 512, 1, 64}});
builder.AddNode("Gather", {reshape_out, gather_index_1}, {gather_out_1})
.AddAttribute("axis", static_cast<int64_t>(2));
Expand All @@ -7663,7 +7663,7 @@ TEST_F(GraphTransformationTests, GatherSliceToSplitFusion) {
.AddAttribute("perm", std::vector<int64_t>{0, 2, 1, 3});

// Create Gather-2 Ops
auto* gather_index_2 = builder.MakeInitializer<int64_t>({}, {static_cast<int64_t>(-1)});
auto* gather_index_2 = builder.MakeInitializer<int64_t>({1}, {static_cast<int64_t>(-1)});
auto* gather_out_2 = builder.MakeIntermediate<float>({{2, 512, 1, 64}});
builder.AddNode("Gather", {reshape_out, gather_index_2}, {gather_out_2})
.AddAttribute("axis", static_cast<int64_t>(2));
Expand Down Expand Up @@ -7730,7 +7730,7 @@ TEST_F(GraphTransformationTests, GatherSliceToSplitFusion_Invalid) {
builder.AddNode("Reshape", {data_arg, reshape_arg}, {reshape_out});

// Create Gather-1 Ops
auto* gather_index_1 = builder.MakeInitializer<int64_t>({}, {static_cast<int64_t>(-2)});
auto* gather_index_1 = builder.MakeInitializer<int64_t>({1}, {static_cast<int64_t>(-2)});
auto* gather_out_1 = builder.MakeIntermediate<float>({{2, 512, 1, 64}});
builder.AddNode("Gather", {reshape_out, gather_index_1}, {gather_out_1})
.AddAttribute("axis", static_cast<int64_t>(2));
Expand Down
Loading