|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +/*! |
| 21 | + * \file dnnl_identity_property.cc |
| 22 | + * \brief Graph property for removing identity operators |
| 23 | + */ |
| 24 | + |
| 25 | +#ifndef MXNET_OPERATOR_SUBGRAPH_DNNL_DNNL_IDENTITY_PROPERTY_H_ |
| 26 | +#define MXNET_OPERATOR_SUBGRAPH_DNNL_DNNL_IDENTITY_PROPERTY_H_ |
| 27 | +#if MXNET_USE_ONEDNN == 1 |
| 28 | + |
| 29 | +#include <map> |
| 30 | +#include <string> |
| 31 | +#include <vector> |
| 32 | + |
| 33 | +#include "../common.h" |
| 34 | +#include "../../nn/dropout-inl.h" |
| 35 | +#include "dnnl_subgraph_base-inl.h" |
| 36 | + |
| 37 | +namespace mxnet { |
| 38 | +namespace op { |
| 39 | + |
| 40 | +class SgDNNLIdentitySelector : public SubgraphSelectorV2 { |
| 41 | + private: |
| 42 | + std::vector<const BiDirectedNode*> matched_list_; |
| 43 | + |
| 44 | + public: |
| 45 | + bool Select(const BiDirectedNode& seed_node, |
| 46 | + const std::shared_ptr<NodeAttr>& node_attr) override { |
| 47 | + bool status = false; |
| 48 | + if (seed_node.node->op() == Op::Get("_npi_copy")) { |
| 49 | + status = true; |
| 50 | + } |
| 51 | + |
| 52 | + if (seed_node.node->op() == Op::Get("Dropout")) { |
| 53 | + auto const& dropout_param = nnvm::get<DropoutParam>(seed_node.node->attrs.parsed); |
| 54 | + if (dropout_param.mode == dropout::kTraining) { |
| 55 | + status = true; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + if (status) { |
| 60 | + matched_list_.clear(); |
| 61 | + matched_list_.emplace_back(&seed_node); |
| 62 | + return true; |
| 63 | + } |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + bool SelectInput(const BiDirectedNode& n, const BiDirectedNode& input_node) override { |
| 68 | + if (input_node.node->is_variable()) { |
| 69 | + return false; |
| 70 | + } else if (input_node.node->op()) { |
| 71 | + matched_list_.emplace_back(&input_node); |
| 72 | + return true; |
| 73 | + } |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + bool SelectOutput(const BiDirectedNode& n, const BiDirectedNode& output_node) override { |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + std::vector<BiDirectedNode*> Filter(const std::vector<BiDirectedNode*>& candidates) override { |
| 82 | + // candidates should contain only two nodes - custom node and identity node |
| 83 | + if (candidates.size() == 2 && candidates.size() == matched_list_.size()) { |
| 84 | + return candidates; |
| 85 | + } else { |
| 86 | + return std::vector<BiDirectedNode*>(0); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + void Reset() override { |
| 91 | + CHECK_GE(matched_list_.size(), 1); |
| 92 | + auto new_selector = SgDNNLIdentitySelector(); |
| 93 | + new_selector.Select(*matched_list_[0], nullptr); |
| 94 | + *this = new_selector; |
| 95 | + } |
| 96 | +}; |
| 97 | + |
| 98 | +inline bool IsIdentityNode(const nnvm::ObjectPtr node) { |
| 99 | + return node->op() && (node->op() == Op::Get("_npi_copy") || node->op() == Op::Get("Dropout")); |
| 100 | +} |
| 101 | + |
| 102 | +class SgDNNLIdentityProperty : public SubgraphProperty { |
| 103 | + public: |
| 104 | + SgDNNLIdentityProperty() {} |
| 105 | + |
| 106 | + static SubgraphPropertyPtr Create() { |
| 107 | + static const std::string& name = "DNNL Identity optimization pass"; |
| 108 | + auto property = std::make_shared<SgDNNLIdentityProperty>(); |
| 109 | + property->SetAttr<std::string>("property_name", name); |
| 110 | + property->SetAttr<bool>("inference_only", true); |
| 111 | + return property; |
| 112 | + } |
| 113 | + |
| 114 | + nnvm::ObjectPtr CreateSubgraphNode(const nnvm::Symbol& sym, |
| 115 | + const int subgraph_id = 0) const override { |
| 116 | + nnvm::NodeEntry identity_node_entry; |
| 117 | + for (auto entry : sym.outputs) { |
| 118 | + if (IsIdentityNode(entry.node)) { |
| 119 | + identity_node_entry = entry; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + auto last_node = identity_node_entry.node; |
| 124 | + nnvm::Symbol new_sym; |
| 125 | + new_sym.outputs.emplace_back(last_node); |
| 126 | + |
| 127 | + nnvm::ObjectPtr org_node; |
| 128 | + DFSVisit(new_sym.outputs, [&](const nnvm::ObjectPtr& node) { |
| 129 | + if (!IsIdentityNode(node)) { |
| 130 | + org_node = node; |
| 131 | + } |
| 132 | + }); |
| 133 | + |
| 134 | + // Create copy of original node |
| 135 | + nnvm::ObjectPtr n = nnvm::Node::Create(); |
| 136 | + n->attrs = org_node->attrs; |
| 137 | + CHECK(n->op()); |
| 138 | + n->op()->attr_parser(&(n->attrs)); |
| 139 | + return n; |
| 140 | + } |
| 141 | + |
| 142 | + void ConnectSubgraphOutputs(const nnvm::ObjectPtr n, |
| 143 | + std::vector<nnvm::NodeEntry*>* output_entries) const override { |
| 144 | + // output of identity must be connected as output of operator before identity |
| 145 | + // e.g. for: /--index 0--> custom_op |
| 146 | + // (n) slice |
| 147 | + // \--index 1--> Dropout --index 0--> OUT_NODE |
| 148 | + // for OUT_NODE index 0 must be changed to index 1 |
| 149 | + for (int i = 0; i < output_entries->size(); ++i) { |
| 150 | + auto out_node = output_entries->at(i)->node; |
| 151 | + if (IsIdentityNode(out_node)) { |
| 152 | + output_entries->at(i)->index = out_node->inputs[0].index; |
| 153 | + } |
| 154 | + output_entries->at(i)->node = n; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + SubgraphSelectorV2Ptr CreateSubgraphSelectorV2() const override { |
| 159 | + auto selector = std::make_shared<SgDNNLIdentitySelector>(); |
| 160 | + return selector; |
| 161 | + } |
| 162 | +}; |
| 163 | + |
| 164 | +} // namespace op |
| 165 | +} // namespace mxnet |
| 166 | + |
| 167 | +#endif // if MXNET_USE_ONEDNN == 1 |
| 168 | +#endif // MXNET_OPERATOR_SUBGRAPH_DNNL_DNNL_IDENTITY_PROPERTY_H_ |
0 commit comments