blob: 21bafa61e1c6afb92160b80b7dd10bbaa0da03ec [file] [log] [blame]
Georgios Pinitasf4261ad2019-12-02 11:58:19 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2019-2020 Arm Limited.
Georgios Pinitasf4261ad2019-12-02 11:58:19 +00003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "arm_compute/graph/mutators/SyntheticDataTypeMutator.h"
25
26#include "arm_compute/graph/GraphBuilder.h"
27#include "arm_compute/graph/ITensorAccessor.h"
28#include "arm_compute/graph/Logger.h"
29#include "arm_compute/graph/Utils.h"
30#include "arm_compute/graph/nodes/Nodes.h"
31
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010032#include "support/Cast.h"
Georgios Pinitasf4261ad2019-12-02 11:58:19 +000033
34#include <set>
35
36namespace arm_compute
37{
38namespace graph
39{
40namespace
41{
42/** Empty accessor class */
43class EmptyAccessor final : public graph::ITensorAccessor
44{
45public:
46 /** Default Constructor */
47 EmptyAccessor() = default;
48
49 // Inherited methods overriden:
50 bool access_tensor(ITensor &tensor) override
51 {
52 ARM_COMPUTE_UNUSED(tensor);
53 return true;
54 }
55};
56
57/** Check if the mutation pass can be applied
58 *
59 * @param[in] g Graph the mutation pass need to be applied on
60 *
61 * @return True if the pass can be applied else false
62 */
63bool is_mutation_supported(Graph &g)
64{
65 const std::set<NodeType> unsupported_node_types = { NodeType::DetectionOutputLayer,
66 NodeType::NormalizationLayer,
67 NodeType::PriorBoxLayer
68 };
69
70 for(const auto &utype : unsupported_node_types)
71 {
72 if(!g.nodes(utype).empty())
73 {
74 return false;
75 }
76 }
77 return true;
78}
79
80/** Remove nodes that get optimized out during conversion
81 *
82 * @param[in, out] g Graph to remove the nodes from.
83 */
84void remove_optimized_nodes(Graph &g)
85{
86 const std::set<NodeType> optimized_node_types = { NodeType::BatchNormalizationLayer };
87
88 for(const auto &opt_type : optimized_node_types)
89 {
90 const std::vector<NodeID> opt_nodes_ids = g.nodes(opt_type);
91 for(const auto &node_id : opt_nodes_ids)
92 {
93 INode *node = g.node(node_id);
94
95 // Get input edge
96 Edge *input_edge = node->input_edge(0);
97 ARM_COMPUTE_ERROR_ON(input_edge == nullptr);
98
99 // Get producer node
100 INode *producer = input_edge->producer();
101 const EdgeID producer_edge_id = input_edge->producer_idx();
102 ARM_COMPUTE_ERROR_ON(producer == nullptr);
103
104 // Get driving nodes
105 std::vector<NodeIdxPair> driving_nodes = get_driving_nodes(*node);
106
107 // Remove node
108 g.remove_node(node->id());
109
110 // Update connections
111 for(auto &driving_node : driving_nodes)
112 {
113 g.add_connection(producer->id(), producer_edge_id, driving_node.node_id, driving_node.index);
114 }
115 }
116 }
117}
118
119/** Convert tensor meta-data
120 *
121 * @param[in,out] g Graph to convert tensors of.
122 */
123void convert_tensors(Graph &g)
124{
125 auto &tensors = g.tensors();
126 for(auto &tensor : tensors)
127 {
128 if(tensor != nullptr)
129 {
130 tensor->desc().data_type = DataType::QASYMM8;
131 tensor->desc().quant_info = QuantizationInfo(0.125f, -10);
132 }
133 }
134}
135
136/** Convert special node
137 *
138 * @param[in,out] g Graph to convert tensors of.
139 * @param[in] fnc Conversion function.
140 * @param[in] optional_arguments Conversion function arguments.
141 */
142template <typename NT>
143void convert_special_node(Graph &g, std::function<bool(INode *, Tensor *)> const &f)
144{
145 const std::vector<NodeID> nodes_ids = g.nodes(NT::node_type);
146 for(const auto &nodes_id : nodes_ids)
147 {
148 INode *node = arm_compute::utils::cast::polymorphic_downcast<NT *>(g.node(nodes_id));
149 ARM_COMPUTE_ERROR_ON(node == nullptr);
150
151 Tensor *output_tensor = node->output(0);
152 ARM_COMPUTE_ERROR_ON(output_tensor == nullptr);
153
154 f(node, output_tensor);
155 }
156}
157
158/** Converts special tensors
159 *
160 * @param[in,out] g Graph to convert tensors of.
161 */
162void convert_special_tensors(Graph &g)
163{
164 auto softmax_func = [](INode * node, Tensor * tensor)
165 {
166 ARM_COMPUTE_UNUSED(node);
167 tensor->desc().quant_info = QuantizationInfo(1.f / 256.f, 0);
168 return true;
169 };
170
171 auto act_func = [](INode * node, Tensor * tensor)
172 {
173 auto *act_node = arm_compute::utils::cast::polymorphic_downcast<ActivationLayerNode *>(node);
174 if(act_node->activation_info().activation() == ActivationLayerInfo::ActivationFunction::TANH)
175 {
176 tensor->desc().quant_info = QuantizationInfo(1.f / 128.f, 128);
177 }
SiCongLi2e5fd632020-03-02 15:39:15 +0000178 else if(act_node->activation_info().activation() == ActivationLayerInfo::ActivationFunction::LOGISTIC)
179 {
180 tensor->desc().quant_info = QuantizationInfo(1.f / 256.f, 0);
181 }
Georgios Pinitasf4261ad2019-12-02 11:58:19 +0000182 return true;
183 };
184
185 convert_special_node<ActivationLayerNode>(g, act_func);
186 convert_special_node<SoftmaxLayerNode>(g, softmax_func);
187}
188
189/** Handle nodes with bias
190 *
191 * @note Special tensors are for now biases that the data type differ
192 *
193 * @param[in,out] g Graph to convert tensors of.
194 */
195void handle_nodes_with_bias(Graph &g)
196{
197 const std::set<NodeType> special_node_types = { NodeType::ConvolutionLayer,
198 NodeType::DeconvolutionLayer,
199 NodeType::DepthwiseConvolutionLayer,
200 NodeType::FullyConnectedLayer
201 };
202
203 for(const auto &spc_type : special_node_types)
204 {
205 const std::vector<NodeID> scp_nodes_ids = g.nodes(spc_type);
206 for(const auto &node_id : scp_nodes_ids)
207 {
208 INode *node = g.node(node_id);
209 if(node != nullptr)
210 {
211 Tensor *tensor = node->input(2);
212 if(tensor != nullptr)
213 {
214 tensor->desc().data_type = DataType::S32;
215 }
216 else
217 {
218 auto params = node->common_node_params();
219 params.name = params.name.empty() ? "" : params.name + "Bias";
220
221 TensorDescriptor b_desc = node->input(1)->desc();
222 auto depth = b_desc.shape[get_dimension_idx(b_desc.layout, DataLayoutDimension::BATCHES)];
223 b_desc.shape = TensorShape(depth);
224
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000225 auto accessor = std::make_unique<EmptyAccessor>();
Georgios Pinitasf4261ad2019-12-02 11:58:19 +0000226 auto b_nid = GraphBuilder::add_const_node(g, params, b_desc, std::move(accessor));
227 g.add_connection(b_nid, 0, node_id, 2);
228 }
229 }
230 }
231 }
232}
233} // namespace
234
235const char *SyntheticDataTypeMutator::name()
236{
237 return "SyntheticDataTypeMutator";
238}
239
240IGraphMutator::MutationType SyntheticDataTypeMutator::type() const
241{
242 return IGraphMutator::MutationType::IR;
243}
244
245void SyntheticDataTypeMutator::mutate(Graph &g)
246{
247 if(is_mutation_supported(g))
248 {
249 // Remove nodes that get optimized out (e.g. BatchNorm)
250 remove_optimized_nodes(g);
251
252 // Convert tensor
253 convert_tensors(g);
254 convert_special_tensors(g);
255
256 // Handle special nodes
257 handle_nodes_with_bias(g);
258 }
259 else
260 {
261 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Synthetic data type mutator couldn't be applied" << std::endl);
262 }
263}
264} // namespace graph
265} // namespace arm_compute