From 194d0a98d16527e98228864710fc2e323688cfe2 Mon Sep 17 00:00:00 2001 From: Weixing Zhang Date: Thu, 6 Dec 2018 11:24:37 -0800 Subject: [PATCH] Add domain and version check for ops fusion. (#108) * Add domain and version check for ops fusion. * Create a helper function for OpType, domain and version check. * Put domain as a parameter. --- onnxruntime/core/graph/conv_add_fusion.cc | 5 +++-- onnxruntime/core/graph/conv_bn_fusion.cc | 5 +++-- onnxruntime/core/graph/conv_mul_fusion.cc | 5 +++-- onnxruntime/core/graph/graph_utils.cc | 21 +++++++++++++++++++++ onnxruntime/core/graph/graph_utils.h | 18 ++++++++++++++++++ 5 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 onnxruntime/core/graph/graph_utils.cc create mode 100644 onnxruntime/core/graph/graph_utils.h diff --git a/onnxruntime/core/graph/conv_add_fusion.cc b/onnxruntime/core/graph/conv_add_fusion.cc index 095371c35e..17e9352f62 100644 --- a/onnxruntime/core/graph/conv_add_fusion.cc +++ b/onnxruntime/core/graph/conv_add_fusion.cc @@ -2,6 +2,7 @@ // Licensed under the MIT License. #include "core/graph/initializer.h" +#include "core/graph/graph_utils.h" #include "core/graph/conv_add_fusion.h" using namespace onnx; @@ -11,12 +12,12 @@ namespace onnxruntime { Status ConvAddFusion::Apply(onnxruntime::Graph& graph, bool& modified) const { std::vector removed_nodes; for (auto& node : graph.Nodes()) { - if (node.OpType() != "Conv" || node.GetOutputEdgesCount() != 1) { + if (!utils::IsSupportedOptypeVersionAndDomain(node, "Conv", 1) || node.GetOutputEdgesCount() != 1) { continue; } const Node& next_node = *node.OutputNodesBegin(); - if (next_node.OpType() != "Add" || + if (!utils::IsSupportedOptypeVersionAndDomain(next_node, "Add", 7) || next_node.GetInputEdgesCount() != 1 || graph.IsNodeOutputsInGraphOutputs(next_node)) { continue; diff --git a/onnxruntime/core/graph/conv_bn_fusion.cc b/onnxruntime/core/graph/conv_bn_fusion.cc index a6b9b4ce9a..25a3eb74b9 100644 --- a/onnxruntime/core/graph/conv_bn_fusion.cc +++ b/onnxruntime/core/graph/conv_bn_fusion.cc @@ -2,6 +2,7 @@ // Licensed under the MIT License. #include "core/graph/initializer.h" +#include "core/graph/graph_utils.h" #include "core/graph/conv_bn_fusion.h" using namespace onnx; @@ -11,12 +12,12 @@ namespace onnxruntime { Status ConvBNFusion::Apply(onnxruntime::Graph& graph, bool& modified) const { std::vector removed_nodes; for (auto& node : graph.Nodes()) { - if (node.OpType() != "Conv" || node.GetOutputEdgesCount() != 1) { + if (!utils::IsSupportedOptypeVersionAndDomain(node, "Conv", 1) || node.GetOutputEdgesCount() != 1) { continue; } const Node& next_node = *node.OutputNodesBegin(); - if (next_node.OpType() != "BatchNormalization" || + if (!utils::IsSupportedOptypeVersionAndDomain(next_node, "BatchNormalization", 7) || next_node.GetInputEdgesCount() != 1 || graph.IsNodeOutputsInGraphOutputs(next_node)) { continue; diff --git a/onnxruntime/core/graph/conv_mul_fusion.cc b/onnxruntime/core/graph/conv_mul_fusion.cc index 8fb6450487..8a7fc9b51a 100644 --- a/onnxruntime/core/graph/conv_mul_fusion.cc +++ b/onnxruntime/core/graph/conv_mul_fusion.cc @@ -2,6 +2,7 @@ // Licensed under the MIT License. #include "core/graph/initializer.h" +#include "core/graph/graph_utils.h" #include "core/graph/conv_mul_fusion.h" using namespace onnx; @@ -11,12 +12,12 @@ namespace onnxruntime { Status ConvMulFusion::Apply(onnxruntime::Graph& graph, bool& modified) const { std::vector removed_nodes; for (auto& node : graph.Nodes()) { - if (node.OpType() != "Conv" || node.GetOutputEdgesCount() != 1) { + if (!utils::IsSupportedOptypeVersionAndDomain(node, "Conv", 1) || node.GetOutputEdgesCount() != 1) { continue; } const Node& next_node = *node.OutputNodesBegin(); - if (next_node.OpType() != "Mul" || + if (!utils::IsSupportedOptypeVersionAndDomain(next_node, "Mul", 7) || next_node.GetInputEdgesCount() != 1 || graph.IsNodeOutputsInGraphOutputs(next_node)) { continue; diff --git a/onnxruntime/core/graph/graph_utils.cc b/onnxruntime/core/graph/graph_utils.cc new file mode 100644 index 0000000000..7daeba8388 --- /dev/null +++ b/onnxruntime/core/graph/graph_utils.cc @@ -0,0 +1,21 @@ + +#include "core/graph/graph_utils.h" + +namespace onnxruntime { + +namespace utils { + // fusion is only done for ONNX domain ops + bool IsSupportedOptypeVersionAndDomain(const Node& node, + const std::string& op_type, + ONNX_NAMESPACE::OperatorSetVersion version, + const std::string& domain) { + if (node.OpType() != op_type || + node.Op()->Deprecated() || node.Op()->SinceVersion() != version || + (!node.Domain().empty() && node.Domain() != domain)) { + return false; + } + return true; + } +} + +} // namespace onnxruntime \ No newline at end of file diff --git a/onnxruntime/core/graph/graph_utils.h b/onnxruntime/core/graph/graph_utils.h new file mode 100644 index 0000000000..7ff5e93d3e --- /dev/null +++ b/onnxruntime/core/graph/graph_utils.h @@ -0,0 +1,18 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +#pragma once + +#include "core/graph/onnx_protobuf.h" +#include "core/graph/graph.h" + +namespace onnxruntime { + +namespace utils { + bool IsSupportedOptypeVersionAndDomain(const Node& node, + const std::string& op_type, + ONNX_NAMESPACE::OperatorSetVersion version, + const std::string& domain = kOnnxDomainAlias); +} + +} \ No newline at end of file