Fix a bug in ReluClip fusion (#9764)

This commit is contained in:
Hariharan Seshadri 2021-11-16 17:59:02 -08:00 committed by GitHub
parent b409cbe62c
commit 871d3fb839
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 1 deletions

View file

@ -96,9 +96,17 @@ Status FuseReluClip::Apply(Graph& graph, Node& node, RewriteRuleEffect& rule_eff
mutable_next_node->ClearAttribute("min");
mutable_next_node->AddAttribute("min", 0.f);
} else {
// Add the initialized tensor to the graph
graph.AddInitializedTensor(replacement_min);
// Create a corresponding NodeArg for the initialized tensor
ONNX_NAMESPACE::TypeProto t;
t.mutable_tensor_type()->set_elem_type(replacement_min.data_type());
NodeArg* replacement_min_nodearg = &graph.GetOrCreateNodeArg(replacement_min.name(), &t);
// Replace the input def at the appropriate index of the Clip node
auto& mutable_input_defs = mutable_next_node->MutableInputDefs();
NodeArg* replacement_min_nodearg = graph.GetNodeArg(replacement_min.name());
if (mutable_input_defs.size() == 1) { // Clip node only has the required 'input' so add optional 'min' input
mutable_input_defs.push_back(replacement_min_nodearg);
mutable_next_node->MutableInputArgsCount().push_back(1);

View file

@ -1941,6 +1941,30 @@ TEST_F(GraphTransformationTests, ReluClip11Fusion) {
}
}
TEST_F(GraphTransformationTests, ReluClip11FusionGHIssue9753) {
auto model_uri = MODEL_FOLDER "fusion/relu_clip_fusion_gh_issue_9753.onnx";
std::shared_ptr<Model> model;
ASSERT_STATUS_OK(Model::Load(model_uri, model, nullptr, *logger_));
Graph& graph = model->MainGraph();
std::map<std::string, int> op_to_count = CountOpsInGraph(graph);
// The model contains one Relu and one Clip
ASSERT_TRUE(op_to_count["Relu"] == 1);
ASSERT_TRUE(op_to_count["Clip"] == 1);
auto rule_transformer_L1 = std::make_unique<RuleBasedGraphTransformer>("RuleTransformer1");
ASSERT_STATUS_OK(rule_transformer_L1->Register(std::make_unique<FuseReluClip>()));
onnxruntime::GraphTransformerManager graph_transformation_mgr{5};
ASSERT_STATUS_OK(graph_transformation_mgr.Register(std::move(rule_transformer_L1), TransformerLevel::Level1));
ASSERT_STATUS_OK(graph_transformation_mgr.ApplyTransformers(graph, TransformerLevel::Level1, *logger_));
op_to_count = CountOpsInGraph(graph);
// After fusion, the model only contains Clip.
ASSERT_TRUE(op_to_count["Relu"] == 0);
ASSERT_TRUE(op_to_count["Clip"] == 1);
}
// Test Reshape Fusion with 2 constant initializers for Concat inputs.
TEST_F(GraphTransformationTests, ReshapeFusionTest) {
auto model_uri = MODEL_FOLDER "fusion/reshape.onnx";