This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
Copy pathmkldnn_conv_property.h
295 lines (275 loc) · 10.4 KB
/
mkldnn_conv_property.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
* 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.
*/
#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 "../../nn/activation-inl.h"
#include "../../leaky_relu-inl.h"
#include "../../nn/convolution-inl.h"
#include "../../nn/mkldnn/mkldnn_ops-inl.h"
#include "../../tensor/matrix_op-inl.h"
#include "../common.h"
#include "mkldnn_subgraph_base-inl.h"
namespace mxnet {
namespace op {
class SgMKLDNNConvSelector : public SubgraphSelector {
public:
/*! \brief pattern match status_ */
enum SelectStatus {
kFail = 0,
kStart,
kBN,
kSum,
kSuccess,
};
private:
bool disable_all_;
bool disable_conv_bn_;
bool disable_conv_act_;
bool disable_conv_sum_;
bool quantize_;
SelectStatus status_;
std::vector<const nnvm::Node *> matched_list_;
public:
SgMKLDNNConvSelector(int dis_all, int dis_conv_bn, int dis_conv_act, int dis_conv_sum,
int quantize)
: disable_all_(dis_all),
disable_conv_bn_(dis_conv_bn),
disable_conv_act_(dis_conv_act),
disable_conv_sum_(dis_conv_sum),
quantize_(quantize) {}
bool Select(const nnvm::Node& n, const std::shared_ptr<NodeAttr>& node_attr) override {
if (n.op() && n.op()->name == "Convolution") {
const auto ¶m = nnvm::get<ConvolutionParam>(n.attrs.parsed);
if ((param.kernel.ndim() == 2 || param.kernel.ndim() == 3) &&
SupportMKLDNNAttr(node_attr)) {
status_ = disable_all_ ? kSuccess : kStart;
matched_list_.clear();
matched_list_.push_back(&n);
return true;
}
}
return false;
}
bool SelectInput(const nnvm::Node &n, const nnvm::Node &new_node) override {
return false;
}
bool SelectOutput(const nnvm::Node &n, const nnvm::Node &new_node) override {
// If n isn't the last matched node, then we encoutered a internal
// branch, we should pop out the node behind n and stop fusion.
if (matched_list_.back() != &n) {
if (std::find(matched_list_.begin(), matched_list_.end(), &n) !=
matched_list_.end()) {
while (matched_list_.back() != &n) {
matched_list_.pop_back();
}
}
status_ = kSuccess;
return false;
}
if (status_ == kFail || status_ == kSuccess || new_node.is_variable())
return false;
// Use status_ machine to do selection. The status_ change is
// kStart -> kBN -> kSum -> kSuccess
switch (status_) {
case kStart:
if ((!disable_conv_bn_) && new_node.op()->name == "BatchNorm") {
matched_list_.push_back(&new_node);
status_ = kBN;
return true;
}
case kBN:
if ((!disable_conv_sum_) && new_node.op()->name == "elemwise_add") {
matched_list_.push_back(&new_node);
status_ = kSum;
return true;
}
case kSum:
default:
if ((!disable_conv_act_) && new_node.op()->name == "Activation") {
const ActivationParam ¶m =
nnvm::get<ActivationParam>(new_node.attrs.parsed);
if ((quantize_ && SupportQuantizedMKLDNNAct(param)) ||
(!quantize_ && SupportMKLDNNAct(param))) {
matched_list_.push_back(&new_node);
// not support conv+relu+sum yet.
status_ = kSuccess;
return true;
}
} else if ((!disable_conv_act_) && new_node.op()->name == "LeakyReLU") {
const LeakyReLUParam ¶m =
nnvm::get<LeakyReLUParam>(new_node.attrs.parsed);
if (param.act_type == leakyrelu::kLeakyReLU ||
param.act_type == leakyrelu::kGELU) {
matched_list_.push_back(&new_node);
// not support conv+relu+sum yet.
status_ = kSuccess;
return true;
}
} else if ((!disable_conv_act_) && new_node.op()->name == "clip") {
if (!(quantize_ && (status_ == kSum))) {
// TODO(zhennan): doesn't support int8 conv+sum+relu6 at moment. To support this, we
// need to fuse conv+sum first, and calibrate with it. Then fuse int8 relu6 into fused
// conv.
const ClipParam ¶m = nnvm::get<ClipParam>(new_node.attrs.parsed);
if (param.a_min == 0.f) {
matched_list_.push_back(&new_node);
// not support conv+relu+sum yet.
status_ = kSuccess;
return true;
}
}
}
status_ = kSuccess;
return false;
}
}
std::vector<nnvm::Node *> Filter(
const std::vector<nnvm::Node *> &candidates) override {
if (status_ == kFail) {
return std::vector<nnvm::Node *>(0);
} else {
std::vector<nnvm::Node *> ret;
for (auto i : matched_list_) {
auto non_const_i = const_cast<nnvm::Node *>(i);
if (std::find(candidates.begin(), candidates.end(), non_const_i) !=
candidates.end()) {
ret.push_back(non_const_i);
}
}
return ret;
}
}
void Reset() override {
CHECK_GE(matched_list_.size(), 1);
auto new_selector = SgMKLDNNConvSelector(disable_all_, disable_conv_bn_, disable_conv_act_,
disable_conv_sum_, quantize_);
new_selector.Select(*matched_list_[0], nullptr);
*this = new_selector;
}
};
class SgMKLDNNConvProperty : public SubgraphProperty {
public:
SgMKLDNNConvProperty() {
disable_conv_bn_ = dmlc::GetEnv("MXNET_DISABLE_MKLDNN_FUSE_CONV_BN", 0);
disable_conv_act_ = dmlc::GetEnv("MXNET_DISABLE_MKLDNN_FUSE_CONV_RELU", 0);
disable_conv_sum_ = dmlc::GetEnv("MXNET_DISABLE_MKLDNN_FUSE_CONV_SUM", 0);
disable_all_ = disable_conv_bn_ && disable_conv_act_ && disable_conv_sum_;
}
static SubgraphPropertyPtr Create() {
static const std::string &name = "MKLDNN convolution optimization pass";
auto property = std::make_shared<SgMKLDNNConvProperty>();
property->SetAttr<std::string>("property_name", name);
property->SetAttr<bool>("inference_only", true);
if (dmlc::GetEnv("MXNET_DISABLE_MKLDNN_CONV_OPT", 0)) {
property->SetAttr<bool>("disable", true);
}
return property;
}
nnvm::ObjectPtr CreateSubgraphNode(const nnvm::Symbol &sym,
const int subgraph_id = 0) const override {
nnvm::ObjectPtr n = nnvm::Node::Create();
// This op has single output, remove duplicated.
auto last_node = sym.outputs[0].node;
nnvm::Symbol new_sym;
new_sym.outputs.emplace_back(last_node);
std::ostringstream node_name;
node_name << "sg_mkldnn_";
bool _with_sum = false;
DFSVisit(new_sym.outputs, [&](const nnvm::ObjectPtr &node) {
if (node->is_variable()) return;
auto &sub_name = node->op()->name;
if (sub_name == "Convolution") {
node_name << "conv_";
} else if (sub_name == "BatchNorm") {
node_name << "bn_";
n->attrs.dict["with_bn"] = "true";
} else if (sub_name == "elemwise_add") {
node_name << "add_";
n->attrs.dict["with_sum"] = "true";
_with_sum = true;
} else if (sub_name == "Activation" || sub_name == "LeakyReLU" || sub_name == "clip") {
node_name << "act_";
if (!_with_sum) {
n->attrs.dict["with_act"] = "true";
} else {
n->attrs.dict["with_postsum_act"] = "true";
}
}
});
node_name << std::to_string(subgraph_id);
n->attrs.name = node_name.str();
n->attrs.op = Op::Get("_sg_mkldnn_conv");
CHECK(n->attrs.op);
n->attrs.subgraphs.emplace_back(std::make_shared<nnvm::Symbol>(new_sym));
n->op()->attr_parser(&(n->attrs));
return n;
}
SubgraphSelectorPtr CreateSubgraphSelector() const override {
bool quantize = HasAttr("quantize") ? GetAttr<bool>("quantize") : false;
auto selector = std::make_shared<SgMKLDNNConvSelector>(
disable_all_, disable_conv_bn_, disable_conv_act_, disable_conv_sum_, quantize);
return selector;
}
void ConnectSubgraphOutputs(
const nnvm::ObjectPtr n,
std::vector<nnvm::NodeEntry *> *output_entries) const override {
// Connect all extern output entries to output[0]
for (size_t i = 0; i < output_entries->size(); ++i) {
*output_entries->at(i) = nnvm::NodeEntry{n, 0, 0};
}
}
void ConnectSubgraphInputs(
const nnvm::ObjectPtr n, std::vector<nnvm::NodeEntry *> *input_entries,
std::vector<nnvm::NodeEntry> *orig_input_entries) const override {
auto sym = n->attrs.subgraphs[0];
std::unordered_set<const nnvm::Node *> node_sets;
DFSVisit(sym->outputs, [&](const nnvm::ObjectPtr &node) {
if (node->is_variable()) return;
node_sets.insert(node.get());
if (node->op()->name == "elemwise_add") {
// Make sure n is the left operand of sum, if not,
// switch sum operands sequence to ensure that
// the extra sum operand stays in the last of inputs.
if (node_sets.count(node->inputs[1].node.get())) {
auto tmp = node->inputs[1];
node->inputs[1] = node->inputs[0];
node->inputs[0] = tmp;
std::rotate(input_entries->begin(), input_entries->begin() + 1,
input_entries->end());
std::rotate(orig_input_entries->begin(),
orig_input_entries->begin() + 1,
orig_input_entries->end());
}
}
});
n->inputs = *orig_input_entries;
}
private:
int disable_all_;
int disable_conv_bn_;
int disable_conv_act_;
int disable_conv_sum_;
};
} // namespace op
} // namespace mxnet
#endif // if MXNET_USE_MKLDNN == 1
#endif // MXNET_OPERATOR_SUBGRAPH_MKLDNN_MKLDNN_CONV_PROPERTY_H_