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.
This commit is contained in:
Weixing Zhang 2018-12-06 11:24:37 -08:00 committed by Ke Zhang
parent 3c7c1068e7
commit 194d0a98d1
5 changed files with 48 additions and 6 deletions

View file

@ -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<onnxruntime::NodeIndex> 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;

View file

@ -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<onnxruntime::NodeIndex> 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;

View file

@ -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<onnxruntime::NodeIndex> 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;

View file

@ -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

View file

@ -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);
}
}