Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Enhance PartitionGraph #14277

Merged
merged 17 commits into from
Mar 26, 2019
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
17 changes: 9 additions & 8 deletions src/c_api/c_api_symbolic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -722,14 +722,15 @@ int MXGenBackendSubgraph(SymbolHandle sym_handle, const char *backend,
API_BEGIN();
nnvm::Symbol *sym = static_cast<nnvm::Symbol *>(sym_handle);
*s = sym->Copy();
nnvm::Graph g = Symbol2Graph(*s);
mxnet::op::SubgraphPropertyPtr property =
mxnet::op::SubgraphPropertyRegistry::Get()->CreateSubgraphProperty(
backend);
g.attrs["subgraph_property"] =
std::make_shared<nnvm::any>(std::move(property));
g = ApplyPass(std::move(g), "PartitionGraph");
s->outputs = g.outputs;
std::vector<mxnet::op::SubgraphPropertyPtr> properties =
mxnet::op::SubgraphPropertyRegistry::Get()->CreateSubgraphProperty(backend);
for (auto property : properties) {
nnvm::Graph g = Symbol2Graph(*s);
property->SetAttr("graph", g);
g.attrs["subgraph_property"] = std::make_shared<nnvm::any>(std::move(property));
g = nnvm::ApplyPass(std::move(g), "PartitionGraph");
s->outputs = g.outputs;
}
*ret_sym_handle = s;
API_END_HANDLE_ERROR(delete s);
}
19 changes: 11 additions & 8 deletions src/c_api/c_api_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,19 @@ int MXPartitionGraphByOpNames(SymbolHandle sym_handle,
}
nnvm::Symbol* sym = static_cast<nnvm::Symbol*>(sym_handle);
*s = sym->Copy();
nnvm::Graph g;
g.outputs = s->outputs;
if (!op_name_set.empty()) {
mxnet::op::SubgraphPropertyPtr property
= mxnet::op::SubgraphPropertyRegistry::Get()->CreateSubgraphProperty(prop_name);
property->SetAttr("op_names", op_name_set);
g.attrs["subgraph_property"] = std::make_shared<nnvm::any>(std::move(property));
std::vector<mxnet::op::SubgraphPropertyPtr> properties =
mxnet::op::SubgraphPropertyRegistry::Get()->CreateSubgraphProperty(prop_name);
for (auto property : properties) {
nnvm::Graph g;
g.outputs = s->outputs;
property->SetAttr("graph", g);
property->SetAttr("op_names", op_name_set);
g.attrs["subgraph_property"] = std::make_shared<nnvm::any>(std::move(property));
g = nnvm::ApplyPass(std::move(g), "PartitionGraph");
s->outputs = g.outputs;
}
}
g = nnvm::ApplyPass(std::move(g), "PartitionGraph");
s->outputs = g.outputs;
*ret_sym_handle = s;
API_END_HANDLE_ERROR(delete s);
}
Expand Down
303 changes: 215 additions & 88 deletions src/executor/graph_executor.cc

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
* under the License.
*/

#ifndef MXNET_OPERATOR_SUBGRAPH_MKLDNN_MKLDNN_CONV_PROPERTY_H_
#define MXNET_OPERATOR_SUBGRAPH_MKLDNN_MKLDNN_CONV_PROPERTY_H_
#if MXNET_USE_MKLDNN == 1

#include <string>
#include <vector>
#include "../common.h"
#include "../subgraph_property.h"
#include "../../nn/activation-inl.h"
Expand Down Expand Up @@ -149,7 +153,10 @@ class SgMKLDNNConvProperty : public SubgraphProperty {
}
}
static SubgraphPropertyPtr Create() {
return std::make_shared<SgMKLDNNConvProperty>();
auto property = std::make_shared<SgMKLDNNConvProperty>();
property->SetAttr<std::string>("prop_name", "MKLDNN Convolution optimization pass");
property->SetAttr<bool>("inference_only", true);
return property;
}
nnvm::NodePtr CreateSubgraphNode(const nnvm::Symbol &sym,
const int subgraph_id = 0) const override {
Expand Down Expand Up @@ -241,9 +248,8 @@ class SgMKLDNNConvProperty : public SubgraphProperty {
int disable_conv_sum;
};

MXNET_REGISTER_SUBGRAPH_PROPERTY(MKLDNN, SgMKLDNNConvProperty);

} // namespace op
} // namespace mxnet

#endif // if MXNET_USE_MKLDNN == 1
#endif // MXNET_OPERATOR_SUBGRAPH_MKLDNN_MKLDNN_CONV_PROPERTY_H_
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
* specific language governing permissions and limitations
* under the License.
*/

#ifndef MXNET_OPERATOR_SUBGRAPH_MKLDNN_MKLDNN_POST_QUANTIZE_CONV_PROPERTY_H_
#define MXNET_OPERATOR_SUBGRAPH_MKLDNN_MKLDNN_POST_QUANTIZE_CONV_PROPERTY_H_
#if MXNET_USE_MKLDNN == 1

#include <string>
#include <vector>
#include "../common.h"
#include "../subgraph_property.h"
#include "../../nn/mkldnn/mkldnn_convolution-inl.h"
Expand Down Expand Up @@ -107,7 +110,11 @@ class SgMKLDNNConvPostQuantizeProperty : public SubgraphProperty {
}
}
static SubgraphPropertyPtr Create() {
return std::make_shared<SgMKLDNNConvPostQuantizeProperty>();
auto property = std::make_shared<SgMKLDNNConvPostQuantizeProperty>();
property->SetAttr<std::string>("prop_name",
"MKLDNN Convolution post-quantization optimization pass");
property->SetAttr<bool>("inference_only", true);
return property;
}
nnvm::NodePtr CreateSubgraphNode(const nnvm::Symbol &sym,
const int subgraph_id = 0) const override {
Expand Down Expand Up @@ -155,9 +162,8 @@ class SgMKLDNNConvPostQuantizeProperty : public SubgraphProperty {
int disable_all;
};

MXNET_REGISTER_SUBGRAPH_PROPERTY(MKLDNN_POST_QUANTIZE, SgMKLDNNConvPostQuantizeProperty);

} // namespace op
} // namespace mxnet

#endif // if MXNET_USE_MKLDNN == 1
#endif // MXNET_OPERATOR_SUBGRAPH_MKLDNN_MKLDNN_POST_QUANTIZE_CONV_PROPERTY_H_
34 changes: 34 additions & 0 deletions src/operator/subgraph/mkldnn/mkldnn_subgraph_property.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#if MXNET_USE_MKLDNN == 1

#include "mkldnn_conv_property.h"
#include "mkldnn_post_quantize_conv_property.h"

namespace mxnet {
namespace op {

MXNET_REGISTER_SUBGRAPH_PROPERTY(MKLDNN, SgMKLDNNConvProperty);
MXNET_REGISTER_SUBGRAPH_PROPERTY(MKLDNN_POST_QUANTIZE, SgMKLDNNConvPostQuantizeProperty);
ZhennanQin marked this conversation as resolved.
Show resolved Hide resolved

} // namespace op
} // namespace mxnet

#endif // MXNET_USE_MKLDNN == 1
35 changes: 16 additions & 19 deletions src/operator/subgraph/subgraph_property.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,13 @@ class SubgraphProperty {
CHECK(it != attrs_.end()) << "Cannot find attribute " << name << " in SubgraphProperty";
return nnvm::get<T>(*it->second);
}

/*!
* \brief Check if the attr exists.
*/
bool HasAttr(const std::string& name) const {
auto it = attrs_.find(name);
return it != attrs_.end();
}
protected:
std::unordered_map<std::string, std::shared_ptr<nnvm::any>> attrs_;
};
Expand All @@ -160,35 +166,26 @@ class SubgraphPropertyRegistry {
return &inst;
}

SubgraphPropertyPtr CreateSubgraphProperty(const std::string& name) {
std::vector<SubgraphPropertyPtr> CreateSubgraphProperty(const std::string& name) {
auto it = prop_fn_map_.find(name);
CHECK(it != prop_fn_map_.end()) << "SubgraphProperty " << name
<< " is not found in SubgraphPropertyRegistry";
return it->second();
}

SubgraphPropertyCreateFn __REGISTER_OR_GET__(const std::string& name,
SubgraphPropertyCreateFn fn) {
if (prop_fn_map_.count(name) == 0U) {
return __REGISTER__(name, fn);
} else {
return prop_fn_map_.at(name);
}
std::vector<SubgraphPropertyPtr> ret;
ret.reserve(it->second.size());
for (auto i : it->second) ret.emplace_back(i());
return ret;
}

private:
SubgraphPropertyCreateFn __REGISTER__(const std::string& name, SubgraphPropertyCreateFn fn) {
CHECK_EQ(prop_fn_map_.count(name), 0U) << "Subgraph property " << name
<< " has been registered";
prop_fn_map_[name] = fn;
return prop_fn_map_[name];
prop_fn_map_[name].push_back(fn);
return fn;
}

SubgraphPropertyRegistry() = default;
SubgraphPropertyRegistry(const SubgraphPropertyRegistry&) = delete;
SubgraphPropertyRegistry(SubgraphPropertyRegistry&&) = delete;
SubgraphPropertyRegistry& operator=(const SubgraphPropertyRegistry&) = delete;
std::unordered_map<std::string, SubgraphPropertyCreateFn> prop_fn_map_;
std::unordered_map<std::string, std::vector<SubgraphPropertyCreateFn>> prop_fn_map_;
};

// This op name set is for setting the names of operators that should be grouped into
Expand All @@ -200,7 +197,7 @@ typedef dmlc::ThreadLocalStore<std::unordered_map<std::string, std::unordered_se

#define MXNET_REGISTER_SUBGRAPH_PROPERTY(Name, SubgraphPropertyType) \
static DMLC_ATTRIBUTE_UNUSED auto __make_ ## SubgraphPropertyType ## _ ## Name ## __ = \
SubgraphPropertyRegistry::Get()->__REGISTER_OR_GET__(#Name, &SubgraphPropertyType::Create)
SubgraphPropertyRegistry::Get()->__REGISTER__(#Name, &SubgraphPropertyType::Create)
ZhennanQin marked this conversation as resolved.
Show resolved Hide resolved

} // namespace op
} // namespace mxnet
Expand Down