46 bool output_edges_are_separate_tensors(Graph &g,
const Edge *input_edge)
48 const auto parent_node = input_edge->producer();
49 const auto input_tensor = input_edge->tensor();
50 const auto input_edge_id = input_edge->id();
52 if (parent_node ==
nullptr)
57 const auto output_edges = parent_node->output_edges();
61 if (output_edges.size() == 1)
66 return std::all_of(output_edges.begin(), output_edges.end(),
70 if (edge_id == input_edge_id)
75 auto edge = g.edge(edge_id);
76 return edge->tensor() != input_tensor;
81 void set_new_output_and_inherit_accessor(std::unique_ptr<INode> &node, Tensor *orig_output, Tensor *new_output)
84 << node->id() <<
" and name : " << node->name() << std::endl);
86 new_output->set_accessor(orig_output->extract_accessor());
88 node->set_output_tensor(new_output->id(), 0);
92 void try_in_place_depthwiseconv(std::unique_ptr<INode> &node)
95 Edge *input_edge = node->input_edge(0);
96 Edge *weight_edge = node->input_edge(1);
99 auto input_tensor = input_edge->tensor();
100 auto weight_tensor = weight_edge->tensor();
103 const auto input_shape = input_tensor->desc().shape;
104 const auto qinfo_input = input_tensor->desc().quant_info;
106 const auto weight_shape = weight_tensor->desc().shape;
107 const auto weight_layout = weight_tensor->desc().layout;
111 unsigned int depth_multiplier{};
112 if (node->type() == NodeType::FusedDepthwiseConvolutionBatchNormalizationLayer)
115 polymorphic_downcast<FusedDepthwiseConvolutionBatchNormalizationNode *>(node.get())->convolution_info();
117 polymorphic_downcast<FusedDepthwiseConvolutionBatchNormalizationNode *>(node.get())->depth_multiplier();
119 else if (node->type() == NodeType::DepthwiseConvolutionLayer)
121 conv_info = polymorphic_downcast<DepthwiseConvolutionLayerNode *>(node.get())->convolution_info();
122 depth_multiplier = polymorphic_downcast<DepthwiseConvolutionLayerNode *>(node.get())->depth_multiplier();
126 auto current_output_tensor = node->output(0);
128 const auto out_shape = current_output_tensor->desc().shape;
129 const auto qinfo_out = current_output_tensor->desc().quant_info;
132 (qinfo_input == qinfo_out) && (input_tensor->accessor() ==
nullptr);
135 input_can_in_place &= weight_layout == input_tensor->desc().layout && weight_layout ==
DataLayout::NHWC;
139 const bool is_1x1 = weight_shape[weights_width_idx] == 1
U && weight_shape[weights_height_idx] == 1
U;
140 input_can_in_place &= is_1x1;
142 input_can_in_place &= depth_multiplier == 1;
143 input_can_in_place &=
conv_info.stride() == std::make_pair(1
U, 1
U);
144 input_can_in_place &= !
conv_info.has_padding();
147 if (input_can_in_place)
149 set_new_output_and_inherit_accessor(node, current_output_tensor, input_tensor);
154 "or the quantization info are different.\n");
159 void try_in_place_elementwise(std::unique_ptr<INode> &node)
162 Edge *input0_edge = node->input_edge(0);
163 Edge *input1_edge = node->input_edge(1);
166 auto input0_tensor = input0_edge->tensor();
167 auto input1_tensor = input1_edge->tensor();
170 const auto shape0 = input0_tensor->desc().shape;
171 const auto shape1 = input1_tensor->desc().shape;
172 const auto qinfo0 = input0_tensor->desc().quant_info;
173 const auto qinfo1 = input1_tensor->desc().quant_info;
177 if (out_shape.total_size() == 0)
183 auto current_output_tensor = node->output(0);
185 const auto qinfo_out = current_output_tensor->desc().quant_info;
189 (qinfo0 == qinfo_out) &&
190 (input0_tensor->desc().data_type == current_output_tensor->desc().data_type) &&
191 (input0_tensor->accessor() ==
nullptr);
193 (qinfo1 == qinfo_out) &&
194 (input1_tensor->desc().data_type == current_output_tensor->desc().data_type) &&
195 (input1_tensor->accessor() ==
nullptr);
197 if (input0_can_in_place)
199 set_new_output_and_inherit_accessor(node, current_output_tensor, input0_tensor);
201 else if (input1_can_in_place)
203 set_new_output_and_inherit_accessor(node, current_output_tensor, input1_tensor);
208 "or the quantization info are different.\n");
215 return "InPlaceOperationMutator";
220 return IGraphMutator::MutationType::Backend;
223 void InPlaceOperationMutator::mutate(
Graph &g)
225 std::set<NodeType> in_place_nodes = {NodeType::ActivationLayer,
226 NodeType::BatchNormalizationLayer,
227 NodeType::EltwiseLayer,
228 NodeType::UnaryEltwiseLayer,
229 NodeType::DepthwiseConvolutionLayer,
230 NodeType::FusedDepthwiseConvolutionBatchNormalizationLayer,
231 NodeType::PrintLayer};
234 for (
auto &node : g.
nodes())
236 if (node && in_place_nodes.find(node->type()) !=
std::end(in_place_nodes))
239 Edge *input_edge = node->input_edge(0);
242 if ((input_edge !=
nullptr) && output_edges_are_separate_tensors(g, input_edge))
244 if (node->type() == NodeType::EltwiseLayer)
246 try_in_place_elementwise(node);
248 else if (node->type() == NodeType::FusedDepthwiseConvolutionBatchNormalizationLayer ||
249 node->type() == NodeType::DepthwiseConvolutionLayer)
251 try_in_place_depthwiseconv(node);
256 auto current_output_tensor = node->output(0);
257 auto new_output_tensor = input_edge->
tensor();
262 if (new_output_tensor->accessor() !=
nullptr ||
263 current_output_tensor->desc().quant_info != new_output_tensor->desc().quant_info)
266 "the input tensor or the quantization info are different.\n");
270 set_new_output_and_inherit_accessor(node, current_output_tensor, new_output_tensor);