Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 1 | /* |
Giorgio Arena | 6e9d0e0 | 2020-01-03 15:02:04 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2020 ARM Limited. |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 3 | * |
| 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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_GRAPH_LAYERS_H |
| 25 | #define ARM_COMPUTE_GRAPH_LAYERS_H |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 26 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 27 | #include "arm_compute/graph/GraphBuilder.h" |
| 28 | #include "arm_compute/graph/Types.h" |
| 29 | #include "arm_compute/graph/frontend/ILayer.h" |
| 30 | #include "arm_compute/graph/frontend/IStream.h" |
| 31 | #include "arm_compute/graph/frontend/SubStream.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 32 | |
| 33 | #include "arm_compute/core/utils/misc/Utility.h" |
| 34 | |
| 35 | #include <memory> |
| 36 | #include <string> |
| 37 | |
| 38 | namespace arm_compute |
| 39 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 40 | namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 41 | { |
| 42 | namespace frontend |
| 43 | { |
| 44 | /** Input Layer */ |
| 45 | class InputLayer final : public ILayer |
| 46 | { |
| 47 | public: |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 48 | /** Construct an input layer. |
| 49 | * |
| 50 | * @param[in] desc Description of input tensor. |
| 51 | * @param[in] accessor Accessor to get input tensor data from. |
| 52 | */ |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 53 | InputLayer(TensorDescriptor desc, ITensorAccessorUPtr accessor) |
| 54 | : _desc(desc), _accessor(std::move(accessor)) |
| 55 | { |
| 56 | } |
| 57 | |
| 58 | NodeID create_layer(IStream &s) override |
| 59 | { |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 60 | NodeParams common_params = { name(), s.hints().target_hint }; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 61 | return GraphBuilder::add_input_node(s.graph(), common_params, _desc, std::move(_accessor)); |
| 62 | } |
| 63 | |
| 64 | private: |
| 65 | TensorDescriptor _desc; |
| 66 | ITensorAccessorUPtr _accessor; |
| 67 | }; |
| 68 | |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 69 | /** Constant Layer */ |
| 70 | class ConstantLayer final : public ILayer |
| 71 | { |
| 72 | public: |
| 73 | /** Construct a constant layer. |
| 74 | * |
| 75 | * @param[in] desc Description of input tensor. |
| 76 | * @param[in] accessor Accessor to get input tensor data from. |
| 77 | */ |
| 78 | ConstantLayer(TensorDescriptor desc, ITensorAccessorUPtr accessor) |
| 79 | : _desc(desc), _accessor(std::move(accessor)) |
| 80 | { |
| 81 | } |
| 82 | |
| 83 | NodeID create_layer(IStream &s) override |
| 84 | { |
| 85 | NodeParams common_params = { name(), s.hints().target_hint }; |
| 86 | return GraphBuilder::add_const_node(s.graph(), common_params, _desc, std::move(_accessor)); |
| 87 | } |
| 88 | |
| 89 | private: |
| 90 | TensorDescriptor _desc; |
| 91 | ITensorAccessorUPtr _accessor; |
| 92 | }; |
| 93 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 94 | /** Output Layer */ |
| 95 | class OutputLayer final : public ILayer |
| 96 | { |
| 97 | public: |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 98 | /** Construct an output layer. |
| 99 | * |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 100 | * @param[in] accessor Accessor to give output tensor data to. |
| 101 | * @param[in] connection_idx (Optional) Input connection index |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 102 | */ |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 103 | OutputLayer(ITensorAccessorUPtr accessor, unsigned int connection_idx = 0) |
| 104 | : _accessor(std::move(accessor)), _connection_idx(connection_idx) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 105 | { |
| 106 | } |
| 107 | |
| 108 | NodeID create_layer(IStream &s) override |
| 109 | { |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 110 | NodeParams common_params = { name(), s.hints().target_hint }; |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 111 | NodeIdxPair input = { s.tail_node(), _connection_idx }; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 112 | return GraphBuilder::add_output_node(s.graph(), common_params, input, std::move(_accessor)); |
| 113 | } |
| 114 | |
| 115 | private: |
| 116 | ITensorAccessorUPtr _accessor; |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 117 | unsigned int _connection_idx; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | /** Activation Layer */ |
| 121 | class ActivationLayer final : public ILayer |
| 122 | { |
| 123 | public: |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 124 | /** Construct an activation layer. |
| 125 | * |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 126 | * @param[in] act_info Activation information |
| 127 | * @param[in] out_quant_info (Optional) Output quantization info |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 128 | */ |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 129 | ActivationLayer(ActivationLayerInfo act_info, |
| 130 | const QuantizationInfo out_quant_info = QuantizationInfo()) |
| 131 | : _act_info(act_info), |
| 132 | _out_quant_info(std::move(out_quant_info)) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 133 | { |
| 134 | } |
| 135 | |
| 136 | NodeID create_layer(IStream &s) override |
| 137 | { |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 138 | NodeParams common_params = { name(), s.hints().target_hint }; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 139 | NodeIdxPair input = { s.tail_node(), 0 }; |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 140 | return GraphBuilder::add_activation_node(s.graph(), common_params, input, _act_info, std::move(_out_quant_info)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | private: |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 144 | ActivationLayerInfo _act_info; |
| 145 | const QuantizationInfo _out_quant_info; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | /** Batchnormalization Layer */ |
| 149 | class BatchNormalizationLayer final : public ILayer |
| 150 | { |
| 151 | public: |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 152 | /** Construct a batch normalization layer. |
| 153 | * |
| 154 | * @param[in] mean Accessor to get mean tensor data from. |
| 155 | * @param[in] var Accessor to get var tensor data from. |
| 156 | * @param[in] gamma (Optional) Accessor to get gamma tensor data from. Default: nullptr. |
| 157 | * @param[in] beta (Optional) Accessor to get beta tensor data from. Default: nullptr. |
| 158 | * @param[in] epsilon (Optional) Epsilon value. Default: 0.001. |
| 159 | */ |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 160 | BatchNormalizationLayer(ITensorAccessorUPtr mean, |
| 161 | ITensorAccessorUPtr var, |
| 162 | ITensorAccessorUPtr gamma = nullptr, |
| 163 | ITensorAccessorUPtr beta = nullptr, |
| 164 | float epsilon = 0.001f) |
| 165 | : _mean(std::move(mean)), _var(std::move(var)), _gamma(std::move(gamma)), _beta(std::move(beta)), _epsilon(epsilon) |
| 166 | { |
| 167 | } |
| 168 | |
| 169 | NodeID create_layer(IStream &s) override |
| 170 | { |
| 171 | ARM_COMPUTE_ERROR_ON(_mean == nullptr); |
| 172 | ARM_COMPUTE_ERROR_ON(_var == nullptr); |
| 173 | |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 174 | NodeParams common_params = { name(), s.hints().target_hint }; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 175 | NodeIdxPair input = { s.tail_node(), 0 }; |
| 176 | return GraphBuilder::add_batch_normalization_node(s.graph(), common_params, input, _epsilon, |
| 177 | std::move(_mean), std::move(_var), std::move(_beta), std::move(_gamma)); |
| 178 | } |
| 179 | |
| 180 | private: |
| 181 | ITensorAccessorUPtr _mean; |
| 182 | ITensorAccessorUPtr _var; |
| 183 | ITensorAccessorUPtr _gamma; |
| 184 | ITensorAccessorUPtr _beta; |
| 185 | float _epsilon; |
| 186 | }; |
| 187 | |
Manuel Bottini | d2048ce | 2018-10-23 17:00:42 +0100 | [diff] [blame] | 188 | /** Bounding Box Transform Layer */ |
| 189 | class BoundingBoxTransformLayer final : public ILayer |
| 190 | { |
| 191 | public: |
| 192 | /** Construct a bounding box transform layer. |
| 193 | * |
| 194 | * @param[in] sub_stream_input Graph sub-stream for the input |
| 195 | * @param[in] sub_stream_deltas Graph sub-stream for the deltas |
| 196 | * @param[in] info Contains BoundingBox operation information described in @ref BoundingBoxTransformInfo. |
| 197 | */ |
| 198 | BoundingBoxTransformLayer(SubStream &&sub_stream_input, SubStream &&sub_stream_deltas, BoundingBoxTransformInfo info) |
| 199 | : _ss_input(sub_stream_input), _ss_deltas(sub_stream_deltas), _bbox_info(info) |
| 200 | { |
| 201 | } |
| 202 | |
| 203 | /** Create layer and add to the given stream. |
| 204 | * |
| 205 | * @param[in] s Stream to add layer to. |
| 206 | * |
| 207 | * @return ID of the created node. |
| 208 | */ |
| 209 | NodeID create_layer(IStream &s) override |
| 210 | { |
| 211 | NodeParams common_params = { name(), s.hints().target_hint }; |
| 212 | NodeIdxPair input = { _ss_input.tail_node(), 0 }; |
| 213 | NodeIdxPair deltas = { _ss_deltas.tail_node(), 0 }; |
| 214 | return GraphBuilder::add_bounding_box_transform_node(s.graph(), common_params, input, deltas, _bbox_info); |
| 215 | } |
| 216 | |
| 217 | private: |
| 218 | SubStream _ss_input; |
| 219 | SubStream _ss_deltas; |
| 220 | BoundingBoxTransformInfo _bbox_info; |
| 221 | }; |
| 222 | |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 223 | /** Channel Shuffle Layer */ |
| 224 | class ChannelShuffleLayer final : public ILayer |
| 225 | { |
| 226 | public: |
| 227 | /** Construct a Channel Shuffle layer. |
| 228 | * |
| 229 | * @param[in] num_groups Number of groups |
| 230 | */ |
| 231 | ChannelShuffleLayer(unsigned int num_groups) |
| 232 | : _num_groups(num_groups) |
| 233 | { |
| 234 | } |
| 235 | |
| 236 | NodeID create_layer(IStream &s) override |
| 237 | { |
| 238 | NodeParams common_params = { name(), s.hints().target_hint }; |
| 239 | NodeIdxPair input = { s.tail_node(), 0 }; |
| 240 | return GraphBuilder::add_channel_shuffle_node(s.graph(), common_params, input, _num_groups); |
| 241 | } |
| 242 | |
| 243 | private: |
| 244 | unsigned int _num_groups; |
| 245 | }; |
| 246 | |
Georgios Pinitas | 427bbbf | 2018-08-28 13:32:02 +0100 | [diff] [blame] | 247 | /** Concat Layer */ |
| 248 | class ConcatLayer final : public ILayer |
| 249 | { |
| 250 | public: |
| 251 | /** Construct a concatenation layer |
| 252 | * |
| 253 | * @param[in] sub_stream1 First graph branch |
| 254 | * @param[in] sub_stream2 Second graph branch |
| 255 | * @param[in] rest_sub_streams Rest sub-graph branches |
| 256 | */ |
| 257 | template <typename... Ts> |
| 258 | ConcatLayer(SubStream &&sub_stream1, SubStream &&sub_stream2, Ts &&... rest_sub_streams) |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 259 | : _sub_streams(), _concat_descriptor(DataLayoutDimension::CHANNEL) |
Pablo Tello | 3252143 | 2018-11-15 14:43:10 +0000 | [diff] [blame] | 260 | { |
| 261 | _sub_streams.push_back(arm_compute::support::cpp14::make_unique<SubStream>(std::move(sub_stream1))); |
| 262 | _sub_streams.push_back(arm_compute::support::cpp14::make_unique<SubStream>(std::move(sub_stream2))); |
| 263 | |
| 264 | utility::for_each([&](SubStream && sub_stream) |
| 265 | { |
| 266 | _sub_streams.push_back(arm_compute::support::cpp14::make_unique<SubStream>(std::move(sub_stream))); |
| 267 | }, |
| 268 | std::move(rest_sub_streams)...); |
| 269 | } |
| 270 | /** Construct a concatenation layer |
| 271 | * |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 272 | * @param[in] concat_descriptor Concat layer descriptor |
| 273 | * @param[in] sub_stream1 First graph branch |
| 274 | * @param[in] sub_stream2 Second graph branch |
| 275 | * @param[in] rest_sub_streams Rest sub-graph branches |
Pablo Tello | 3252143 | 2018-11-15 14:43:10 +0000 | [diff] [blame] | 276 | */ |
| 277 | template <typename... Ts> |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 278 | ConcatLayer(descriptors::ConcatLayerDescriptor concat_descriptor, SubStream &&sub_stream1, SubStream &&sub_stream2, Ts &&... rest_sub_streams) |
| 279 | : _sub_streams(), _concat_descriptor(concat_descriptor) |
Georgios Pinitas | 427bbbf | 2018-08-28 13:32:02 +0100 | [diff] [blame] | 280 | { |
| 281 | _sub_streams.push_back(arm_compute::support::cpp14::make_unique<SubStream>(std::move(sub_stream1))); |
| 282 | _sub_streams.push_back(arm_compute::support::cpp14::make_unique<SubStream>(std::move(sub_stream2))); |
| 283 | |
| 284 | utility::for_each([&](SubStream && sub_stream) |
| 285 | { |
| 286 | _sub_streams.push_back(arm_compute::support::cpp14::make_unique<SubStream>(std::move(sub_stream))); |
| 287 | }, |
| 288 | std::move(rest_sub_streams)...); |
| 289 | } |
| 290 | /** Construct a concat layer |
| 291 | * |
| 292 | * @param[in] sub_stream Sub-stream |
| 293 | */ |
| 294 | template <typename... Ts> |
| 295 | ConcatLayer(SubStream &&sub_stream) |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 296 | : _sub_streams(), _concat_descriptor(DataLayoutDimension::CHANNEL) |
Georgios Pinitas | 427bbbf | 2018-08-28 13:32:02 +0100 | [diff] [blame] | 297 | { |
| 298 | _sub_streams.push_back(arm_compute::support::cpp14::make_unique<SubStream>(std::move(sub_stream))); |
| 299 | } |
| 300 | NodeID create_layer(IStream &s) override |
| 301 | { |
| 302 | NodeID nid = EmptyNodeID; |
| 303 | NodeParams common_params = { name(), s.hints().target_hint }; |
| 304 | if(_sub_streams.size() == 1 && _sub_streams.at(0) != nullptr) |
| 305 | { |
| 306 | nid = _sub_streams[0]->tail_node(); |
| 307 | } |
| 308 | else |
| 309 | { |
| 310 | // Collect tail nodes and concatenate |
| 311 | std::vector<NodeIdxPair> nodes; |
| 312 | for(auto &ss : _sub_streams) |
| 313 | { |
| 314 | if(ss && (ss->tail_node() != EmptyNodeID)) |
| 315 | { |
| 316 | const auto tail_node = s.graph().node(ss->tail_node()); |
| 317 | if(tail_node != nullptr && tail_node->type() != NodeType::Output) |
| 318 | { |
| 319 | nodes.push_back({ ss->tail_node(), 0 }); |
| 320 | } |
| 321 | } |
| 322 | } |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 323 | nid = GraphBuilder::add_concatenate_node(s.graph(), common_params, nodes, _concat_descriptor); |
Georgios Pinitas | 427bbbf | 2018-08-28 13:32:02 +0100 | [diff] [blame] | 324 | } |
| 325 | return nid; |
| 326 | } |
| 327 | |
| 328 | private: |
| 329 | std::vector<std::unique_ptr<SubStream>> _sub_streams; |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 330 | descriptors::ConcatLayerDescriptor _concat_descriptor; |
Georgios Pinitas | 427bbbf | 2018-08-28 13:32:02 +0100 | [diff] [blame] | 331 | }; |
| 332 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 333 | /** Convolution Layer */ |
| 334 | class ConvolutionLayer final : public ILayer |
| 335 | { |
| 336 | public: |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 337 | /** Construct a convolution layer. |
| 338 | * |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 339 | * @param[in] conv_width Convolution width. |
| 340 | * @param[in] conv_height Convolution height. |
| 341 | * @param[in] ofm Output feature map. |
| 342 | * @param[in] weights Accessor to get kernel weights from. |
| 343 | * @param[in] bias Accessor to get kernel bias from. |
| 344 | * @param[in] conv_info Padding and stride information. |
| 345 | * @param[in] num_groups (Optional) Number of groups. Default: 1. |
| 346 | * @param[in] weights_quant_info (Optional) Weights quantization information |
| 347 | * @param[in] out_quant_info (Optional) Output quantization info |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 348 | */ |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 349 | ConvolutionLayer(unsigned int conv_width, |
| 350 | unsigned int conv_height, |
| 351 | unsigned int ofm, |
| 352 | ITensorAccessorUPtr weights, |
| 353 | ITensorAccessorUPtr bias, |
| 354 | PadStrideInfo conv_info, |
| 355 | unsigned int num_groups = 1, |
| 356 | const QuantizationInfo weights_quant_info = QuantizationInfo(), |
| 357 | const QuantizationInfo out_quant_info = QuantizationInfo()) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 358 | : _conv_width(conv_width), |
| 359 | _conv_height(conv_height), |
| 360 | _ofm(ofm), |
| 361 | _conv_info(std::move(conv_info)), |
| 362 | _num_groups(num_groups), |
| 363 | _weights(std::move(weights)), |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 364 | _bias(std::move(bias)), |
| 365 | _weights_quant_info(std::move(weights_quant_info)), |
| 366 | _out_quant_info(std::move(out_quant_info)) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 367 | { |
| 368 | } |
| 369 | |
| 370 | NodeID create_layer(IStream &s) override |
| 371 | { |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 372 | NodeIdxPair input = { s.tail_node(), 0 }; |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 373 | NodeParams common_params = { name(), s.hints().target_hint }; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 374 | return GraphBuilder::add_convolution_node(s.graph(), common_params, input, |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 375 | Size2D(_conv_width, _conv_height), _ofm, _conv_info, _num_groups, |
Giorgio Arena | 59631a1 | 2018-05-02 13:59:04 +0100 | [diff] [blame] | 376 | s.hints().convolution_method_hint, s.hints().fast_math_hint, |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 377 | std::move(_weights), std::move(_bias), std::move(_weights_quant_info), std::move(_out_quant_info)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | private: |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 381 | unsigned int _conv_width; |
| 382 | unsigned int _conv_height; |
| 383 | unsigned int _ofm; |
| 384 | const PadStrideInfo _conv_info; |
| 385 | unsigned int _num_groups; |
| 386 | ITensorAccessorUPtr _weights; |
| 387 | ITensorAccessorUPtr _bias; |
| 388 | const QuantizationInfo _weights_quant_info; |
| 389 | const QuantizationInfo _out_quant_info; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 390 | }; |
| 391 | |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 392 | /** Deconvolution Layer */ |
| 393 | class DeconvolutionLayer final : public ILayer |
| 394 | { |
| 395 | public: |
| 396 | /** Construct a convolution layer. |
| 397 | * |
Manuel Bottini | c1b76fa | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 398 | * @param[in] conv_width Convolution width. |
| 399 | * @param[in] conv_height Convolution height. |
| 400 | * @param[in] ofm Output feature map. |
| 401 | * @param[in] weights Accessor to get kernel weights from. |
| 402 | * @param[in] bias Accessor to get kernel bias from. |
| 403 | * @param[in] deconv_info Padding and stride information. |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 404 | */ |
| 405 | DeconvolutionLayer(unsigned int conv_width, |
| 406 | unsigned int conv_height, |
| 407 | unsigned int ofm, |
| 408 | ITensorAccessorUPtr weights, |
| 409 | ITensorAccessorUPtr bias, |
Manuel Bottini | c1b76fa | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 410 | PadStrideInfo deconv_info) |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 411 | : _conv_width(conv_width), |
| 412 | _conv_height(conv_height), |
| 413 | _ofm(ofm), |
| 414 | _deconv_info(std::move(deconv_info)), |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 415 | _weights(std::move(weights)), |
| 416 | _bias(std::move(bias)) |
| 417 | { |
| 418 | } |
| 419 | |
| 420 | NodeID create_layer(IStream &s) override |
| 421 | { |
| 422 | NodeIdxPair input = { s.tail_node(), 0 }; |
| 423 | NodeParams common_params = { name(), s.hints().target_hint }; |
| 424 | return GraphBuilder::add_deconvolution_node(s.graph(), common_params, input, |
Manuel Bottini | c1b76fa | 2019-06-17 12:04:40 +0100 | [diff] [blame] | 425 | Size2D(_conv_width, _conv_height), _ofm, _deconv_info, |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 426 | std::move(_weights), std::move(_bias)); |
| 427 | } |
| 428 | |
| 429 | private: |
| 430 | unsigned int _conv_width; |
| 431 | unsigned int _conv_height; |
| 432 | unsigned int _ofm; |
| 433 | const PadStrideInfo _deconv_info; |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 434 | ITensorAccessorUPtr _weights; |
| 435 | ITensorAccessorUPtr _bias; |
| 436 | }; |
| 437 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 438 | /** Depthwise Convolution Layer */ |
| 439 | class DepthwiseConvolutionLayer final : public ILayer |
| 440 | { |
| 441 | public: |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 442 | /** Construct a depthwise convolution layer. |
| 443 | * |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 444 | * @param[in] conv_width Convolution width. |
| 445 | * @param[in] conv_height Convolution height. |
| 446 | * @param[in] weights Accessor to get kernel weights from. |
| 447 | * @param[in] bias Accessor to get kernel bias from. |
| 448 | * @param[in] conv_info Padding and stride information. |
| 449 | * @param[in] depth_multiplier (Optional) Depth multiplier parameter. |
| 450 | * @param[in] weights_quant_info (Optional) Quantization info used for weights |
| 451 | * @param[in] out_quant_info (Optional) Output quantization info |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 452 | */ |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 453 | DepthwiseConvolutionLayer(unsigned int conv_width, |
| 454 | unsigned int conv_height, |
| 455 | ITensorAccessorUPtr weights, |
| 456 | ITensorAccessorUPtr bias, |
| 457 | PadStrideInfo conv_info, |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 458 | int depth_multiplier = 1, |
| 459 | const QuantizationInfo weights_quant_info = QuantizationInfo(), |
| 460 | const QuantizationInfo out_quant_info = QuantizationInfo()) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 461 | : _conv_width(conv_width), |
| 462 | _conv_height(conv_height), |
| 463 | _conv_info(std::move(conv_info)), |
| 464 | _weights(std::move(weights)), |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 465 | _bias(std::move(bias)), |
Georgios Pinitas | 05045c1 | 2018-12-07 18:31:47 +0000 | [diff] [blame] | 466 | _depth_multiplier(depth_multiplier), |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 467 | _weights_quant_info(std::move(weights_quant_info)), |
| 468 | _out_quant_info(std::move(out_quant_info)) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 469 | { |
| 470 | } |
| 471 | |
| 472 | NodeID create_layer(IStream &s) override |
| 473 | { |
| 474 | NodeIdxPair input = { s.tail_node(), 0 }; |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 475 | NodeParams common_params = { name(), s.hints().target_hint }; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 476 | return GraphBuilder::add_depthwise_convolution_node(s.graph(), common_params, |
Georgios Pinitas | 05045c1 | 2018-12-07 18:31:47 +0000 | [diff] [blame] | 477 | input, Size2D(_conv_width, _conv_height), _conv_info, _depth_multiplier, |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 478 | s.hints().depthwise_convolution_method_hint, |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 479 | std::move(_weights), std::move(_bias), std::move(_weights_quant_info), std::move(_out_quant_info)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | private: |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 483 | unsigned int _conv_width; |
| 484 | unsigned int _conv_height; |
| 485 | const PadStrideInfo _conv_info; |
| 486 | ITensorAccessorUPtr _weights; |
| 487 | ITensorAccessorUPtr _bias; |
Georgios Pinitas | 05045c1 | 2018-12-07 18:31:47 +0000 | [diff] [blame] | 488 | int _depth_multiplier; |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 489 | const QuantizationInfo _weights_quant_info; |
| 490 | const QuantizationInfo _out_quant_info; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 491 | }; |
Isabella Gottardi | cd4e9ab | 2019-11-05 17:50:27 +0000 | [diff] [blame] | 492 | /** Dequantization Layer */ |
| 493 | class DequantizationLayer final : public ILayer |
| 494 | { |
| 495 | public: |
| 496 | /** Construct a dequantization layer. |
| 497 | * |
| 498 | */ |
| 499 | DequantizationLayer() |
| 500 | { |
| 501 | } |
| 502 | |
| 503 | NodeID create_layer(IStream &s) override |
| 504 | { |
| 505 | NodeParams common_params = { name(), s.hints().target_hint }; |
| 506 | NodeIdxPair input = { s.tail_node(), 0 }; |
| 507 | return GraphBuilder::add_dequantization_node(s.graph(), common_params, input); |
| 508 | } |
| 509 | }; |
| 510 | |
Isabella Gottardi | 7234ed8 | 2018-11-27 08:51:10 +0000 | [diff] [blame] | 511 | /** DetectionOutput Layer */ |
| 512 | class DetectionOutputLayer final : public ILayer |
| 513 | { |
| 514 | public: |
| 515 | /** Construct a detection output layer. |
| 516 | * |
| 517 | * @param[in] sub_stream_conf Confidence graph sub-stream. |
| 518 | * @param[in] sub_stream_prior PriorBox graph sub-stream. |
| 519 | * @param[in] detect_info DetectionOutput parameters. |
| 520 | */ |
Georgios Pinitas | f52cd78 | 2019-03-25 14:06:14 +0000 | [diff] [blame] | 521 | DetectionOutputLayer(SubStream &&sub_stream_conf, SubStream &&sub_stream_prior, const DetectionOutputLayerInfo &detect_info) |
Isabella Gottardi | 7234ed8 | 2018-11-27 08:51:10 +0000 | [diff] [blame] | 522 | : _ss_conf(std::move(sub_stream_conf)), _ss_prior(std::move(sub_stream_prior)), _detect_info(detect_info) |
| 523 | { |
| 524 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 525 | |
Isabella Gottardi | 7234ed8 | 2018-11-27 08:51:10 +0000 | [diff] [blame] | 526 | NodeID create_layer(IStream &s) override |
| 527 | { |
| 528 | NodeParams common_params = { name(), s.hints().target_hint }; |
| 529 | NodeIdxPair input_loc = { s.tail_node(), 0 }; |
| 530 | NodeIdxPair input_conf = { _ss_conf.tail_node(), 0 }; |
| 531 | NodeIdxPair input_priorbox = { _ss_prior.tail_node(), 0 }; |
| 532 | return GraphBuilder::add_detection_output_node(s.graph(), common_params, input_loc, input_conf, input_priorbox, _detect_info); |
| 533 | } |
| 534 | |
| 535 | private: |
| 536 | SubStream _ss_conf; |
| 537 | SubStream _ss_prior; |
| 538 | DetectionOutputLayerInfo _detect_info; |
| 539 | }; |
Isabella Gottardi | a7acb3c | 2019-01-08 13:48:44 +0000 | [diff] [blame] | 540 | /** DetectionOutputPostProcess Layer */ |
| 541 | class DetectionPostProcessLayer final : public ILayer |
| 542 | { |
| 543 | public: |
| 544 | /** Construct a detection output layer. |
| 545 | * |
| 546 | * @param[in] sub_stream_class_prediction Class prediction graph sub-stream. |
| 547 | * @param[in] detect_info DetectionOutput parameters. |
| 548 | * @param[in] anchors Accessor to get anchors tensor data from. |
| 549 | * @param[in] out_quant_info (Optional) Output quantization info |
| 550 | */ |
| 551 | DetectionPostProcessLayer(SubStream &&sub_stream_class_prediction, DetectionPostProcessLayerInfo detect_info, ITensorAccessorUPtr anchors, |
| 552 | const QuantizationInfo out_quant_info = QuantizationInfo()) |
| 553 | : _sub_stream_class_prediction(std::move(sub_stream_class_prediction)), _detect_info(detect_info), _anchors(std::move(anchors)), _out_quant_info(std::move(out_quant_info)) |
| 554 | { |
| 555 | } |
| 556 | |
| 557 | NodeID create_layer(IStream &s) override |
| 558 | { |
| 559 | ARM_COMPUTE_ERROR_ON(_anchors == nullptr); |
| 560 | |
| 561 | NodeParams common_params = { name(), s.hints().target_hint }; |
| 562 | NodeIdxPair input_box_encoding = { s.tail_node(), 0 }; |
| 563 | NodeIdxPair input_class_prediction = { _sub_stream_class_prediction.tail_node(), 0 }; |
| 564 | return GraphBuilder::add_detection_post_process_node(s.graph(), common_params, input_box_encoding, input_class_prediction, _detect_info, std::move(_anchors), std::move(_out_quant_info)); |
| 565 | } |
| 566 | |
| 567 | private: |
| 568 | SubStream _sub_stream_class_prediction; |
| 569 | DetectionPostProcessLayerInfo _detect_info; |
| 570 | ITensorAccessorUPtr _anchors; |
| 571 | const QuantizationInfo _out_quant_info; |
| 572 | }; |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 573 | /** Dummy Layer */ |
| 574 | class DummyLayer final : public ILayer |
| 575 | { |
| 576 | public: |
Isabella Gottardi | cd4e9ab | 2019-11-05 17:50:27 +0000 | [diff] [blame] | 577 | /** Construct a dummy layer. |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 578 | * |
| 579 | * @param[in] shape Output shape |
| 580 | */ |
| 581 | DummyLayer(TensorShape shape) |
| 582 | : _shape(shape) |
| 583 | { |
| 584 | } |
| 585 | |
| 586 | NodeID create_layer(IStream &s) override |
| 587 | { |
| 588 | NodeParams common_params = { name(), s.hints().target_hint }; |
| 589 | NodeIdxPair input = { s.tail_node(), 0 }; |
| 590 | return GraphBuilder::add_dummy_node(s.graph(), common_params, input, _shape); |
| 591 | } |
| 592 | |
| 593 | private: |
| 594 | TensorShape _shape; |
| 595 | }; |
| 596 | |
Georgios Pinitas | 427bbbf | 2018-08-28 13:32:02 +0100 | [diff] [blame] | 597 | class EltwiseLayer final : public ILayer |
| 598 | { |
| 599 | public: |
| 600 | /** Construct an element-wise operation layer |
| 601 | * |
| 602 | * @param[in] sub_stream0 First graph sub-stream |
| 603 | * @param[in] sub_stream1 First graph sub-stream |
| 604 | * @param[in] op Element-wise operation to perform |
| 605 | */ |
| 606 | EltwiseLayer(SubStream &&sub_stream0, SubStream &&sub_stream1, EltwiseOperation op) |
| 607 | : _ss0(std::move(sub_stream0)), _ss1(std::move(sub_stream1)), _op(op) |
| 608 | { |
| 609 | } |
| 610 | |
| 611 | NodeID create_layer(IStream &s) override |
| 612 | { |
| 613 | NodeParams common_params = { name(), s.hints().target_hint }; |
| 614 | NodeIdxPair input0 = { _ss0.tail_node(), 0 }; |
| 615 | NodeIdxPair input1 = { _ss1.tail_node(), 0 }; |
| 616 | |
| 617 | return GraphBuilder::add_elementwise_node(s.graph(), common_params, input0, input1, _op); |
| 618 | } |
| 619 | |
| 620 | private: |
| 621 | SubStream _ss0; |
| 622 | SubStream _ss1; |
| 623 | EltwiseOperation _op; |
| 624 | }; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 625 | /** Flatten Layer */ |
| 626 | class FlattenLayer final : public ILayer |
| 627 | { |
| 628 | public: |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 629 | /** Construct a flatten layer. */ |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 630 | FlattenLayer() |
| 631 | { |
| 632 | } |
| 633 | |
| 634 | NodeID create_layer(IStream &s) override |
| 635 | { |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 636 | NodeParams common_params = { name(), s.hints().target_hint }; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 637 | NodeIdxPair input = { s.tail_node(), 0 }; |
| 638 | return GraphBuilder::add_flatten_node(s.graph(), common_params, input); |
| 639 | } |
| 640 | }; |
| 641 | |
| 642 | /** Fully Connected Layer */ |
| 643 | class FullyConnectedLayer final : public ILayer |
| 644 | { |
| 645 | public: |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 646 | /** Construct a fully connected layer. |
| 647 | * |
Georgios Pinitas | 2f1366a | 2018-07-31 16:33:06 +0100 | [diff] [blame] | 648 | * @param[in] num_outputs Number of outputs. |
| 649 | * @param[in] weights Accessor to get weights from. |
| 650 | * @param[in] bias Accessor to get bias from. |
Georgios Pinitas | c55cef1 | 2018-08-01 15:24:18 +0100 | [diff] [blame] | 651 | * @param[in] fc_info (Optional) Fully connected layer metadata |
Georgios Pinitas | 2f1366a | 2018-07-31 16:33:06 +0100 | [diff] [blame] | 652 | * @param[in] weights_quant_info (Optional) Weights quantization information |
| 653 | * @param[in] out_quant_info (Optional) Output quantization info |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 654 | */ |
Georgios Pinitas | c55cef1 | 2018-08-01 15:24:18 +0100 | [diff] [blame] | 655 | FullyConnectedLayer(unsigned int num_outputs, |
| 656 | ITensorAccessorUPtr weights, |
| 657 | ITensorAccessorUPtr bias, |
| 658 | const FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo(), |
| 659 | const QuantizationInfo weights_quant_info = QuantizationInfo(), |
| 660 | const QuantizationInfo out_quant_info = QuantizationInfo()) |
Georgios Pinitas | 2f1366a | 2018-07-31 16:33:06 +0100 | [diff] [blame] | 661 | : _num_outputs(num_outputs), |
| 662 | _weights(std::move(weights)), |
| 663 | _bias(std::move(bias)), |
Michele Di Giorgio | a42f55f | 2019-03-08 14:52:17 +0000 | [diff] [blame] | 664 | _weights_ss(nullptr), |
| 665 | _bias_ss(nullptr), |
| 666 | _fc_info(fc_info), |
| 667 | _weights_quant_info(std::move(weights_quant_info)), |
| 668 | _out_quant_info(std::move(out_quant_info)) |
| 669 | { |
| 670 | } |
| 671 | |
| 672 | /** Construct a fully connected layer. |
| 673 | * |
| 674 | * @param[in] num_outputs Number of outputs. |
| 675 | * @param[in] sub_stream_weights Graph sub-stream for the weights. |
| 676 | * @param[in] sub_stream_bias Graph sub-stream for the bias. |
| 677 | * @param[in] fc_info (Optional) Fully connected layer metadata |
| 678 | * @param[in] weights_quant_info (Optional) Weights quantization information |
| 679 | * @param[in] out_quant_info (Optional) Output quantization info |
| 680 | */ |
| 681 | FullyConnectedLayer(unsigned int num_outputs, |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 682 | SubStream sub_stream_weights, |
| 683 | SubStream sub_stream_bias, |
Michele Di Giorgio | a42f55f | 2019-03-08 14:52:17 +0000 | [diff] [blame] | 684 | const FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo(), |
| 685 | const QuantizationInfo weights_quant_info = QuantizationInfo(), |
| 686 | const QuantizationInfo out_quant_info = QuantizationInfo()) |
| 687 | : _num_outputs(num_outputs), |
| 688 | _weights(nullptr), |
| 689 | _bias(nullptr), |
| 690 | _weights_ss(arm_compute::support::cpp14::make_unique<SubStream>(std::move(sub_stream_weights))), |
| 691 | _bias_ss(arm_compute::support::cpp14::make_unique<SubStream>(std::move(sub_stream_bias))), |
Georgios Pinitas | c55cef1 | 2018-08-01 15:24:18 +0100 | [diff] [blame] | 692 | _fc_info(fc_info), |
Georgios Pinitas | 2f1366a | 2018-07-31 16:33:06 +0100 | [diff] [blame] | 693 | _weights_quant_info(std::move(weights_quant_info)), |
| 694 | _out_quant_info(std::move(out_quant_info)) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 695 | { |
| 696 | } |
| 697 | |
Michele Di Giorgio | 47e6fed | 2018-11-13 12:04:25 +0000 | [diff] [blame] | 698 | /** Create layer and add to the given stream. |
| 699 | * |
| 700 | * @param[in] s Stream to add layer to. |
| 701 | * |
| 702 | * @return ID of the created node. |
| 703 | */ |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 704 | NodeID create_layer(IStream &s) override |
| 705 | { |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 706 | NodeParams common_params = { name(), s.hints().target_hint }; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 707 | NodeIdxPair input = { s.tail_node(), 0 }; |
Michele Di Giorgio | a42f55f | 2019-03-08 14:52:17 +0000 | [diff] [blame] | 708 | if(_weights != nullptr) |
| 709 | { |
| 710 | return GraphBuilder::add_fully_connected_layer(s.graph(), common_params, input, _num_outputs, |
| 711 | std::move(_weights), std::move(_bias), _fc_info, |
| 712 | std::move(_weights_quant_info), std::move(_out_quant_info)); |
| 713 | } |
| 714 | else |
| 715 | { |
| 716 | ARM_COMPUTE_ERROR_ON(_weights_ss == nullptr); |
| 717 | |
| 718 | NodeID bias_nid = (_bias_ss == nullptr) ? EmptyNodeID : _bias_ss->tail_node(); |
| 719 | return GraphBuilder::add_fully_connected_layer(s.graph(), common_params, input, _num_outputs, |
| 720 | _weights_ss->tail_node(), bias_nid, _fc_info, |
| 721 | std::move(_out_quant_info)); |
| 722 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | private: |
Georgios Pinitas | c55cef1 | 2018-08-01 15:24:18 +0100 | [diff] [blame] | 726 | unsigned int _num_outputs; |
| 727 | ITensorAccessorUPtr _weights; |
| 728 | ITensorAccessorUPtr _bias; |
Michele Di Giorgio | a42f55f | 2019-03-08 14:52:17 +0000 | [diff] [blame] | 729 | std::unique_ptr<SubStream> _weights_ss; |
| 730 | std::unique_ptr<SubStream> _bias_ss; |
Georgios Pinitas | c55cef1 | 2018-08-01 15:24:18 +0100 | [diff] [blame] | 731 | const FullyConnectedLayerInfo _fc_info; |
| 732 | const QuantizationInfo _weights_quant_info; |
| 733 | const QuantizationInfo _out_quant_info; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 734 | }; |
| 735 | |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 736 | /** Generate Proposals Layer */ |
| 737 | class GenerateProposalsLayer final : public ILayer |
| 738 | { |
| 739 | public: |
| 740 | /** Construct a generate proposals layer. |
| 741 | * |
| 742 | * @param[in] ss_scores Graph sub-stream for the scores. |
| 743 | * @param[in] ss_deltas Graph sub-stream for the deltas. |
| 744 | * @param[in] ss_anchors Graph sub-stream for the anchors. |
| 745 | * @param[in] info Generate Proposals operation information. |
| 746 | */ |
| 747 | GenerateProposalsLayer(SubStream &&ss_scores, SubStream &&ss_deltas, SubStream &&ss_anchors, GenerateProposalsInfo info) |
| 748 | : _ss_scores(std::move(ss_scores)), _ss_deltas(std::move(ss_deltas)), _ss_anchors(std::move(ss_anchors)), _info(info) |
| 749 | { |
| 750 | } |
| 751 | |
| 752 | /** Create layer and add to the given stream. |
| 753 | * |
| 754 | * @param[in] s Stream to add layer to. |
| 755 | * |
| 756 | * @return ID of the created node. |
| 757 | */ |
| 758 | NodeID create_layer(IStream &s) override |
| 759 | { |
| 760 | NodeParams common_params = { name(), s.hints().target_hint }; |
| 761 | NodeIdxPair scores = { _ss_scores.tail_node(), 0 }; |
| 762 | NodeIdxPair deltas = { _ss_deltas.tail_node(), 0 }; |
| 763 | NodeIdxPair anchors = { _ss_anchors.tail_node(), 0 }; |
| 764 | return GraphBuilder::add_generate_proposals_node(s.graph(), common_params, scores, deltas, anchors, _info); |
| 765 | } |
| 766 | |
| 767 | private: |
| 768 | SubStream _ss_scores; |
| 769 | SubStream _ss_deltas; |
| 770 | SubStream _ss_anchors; |
| 771 | GenerateProposalsInfo _info; |
| 772 | }; |
| 773 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 774 | /** Normalization Layer */ |
| 775 | class NormalizationLayer final : public ILayer |
| 776 | { |
| 777 | public: |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 778 | /** Construct a normalization layer. |
| 779 | * |
| 780 | * @param[in] norm_info Normalization information. |
| 781 | */ |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 782 | NormalizationLayer(NormalizationLayerInfo norm_info) |
| 783 | : _norm_info(norm_info) |
| 784 | { |
| 785 | } |
| 786 | |
| 787 | NodeID create_layer(IStream &s) override |
| 788 | { |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 789 | NodeParams common_params = { name(), s.hints().target_hint }; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 790 | NodeIdxPair input = { s.tail_node(), 0 }; |
| 791 | return GraphBuilder::add_normalization_node(s.graph(), common_params, input, _norm_info); |
| 792 | } |
| 793 | |
| 794 | private: |
| 795 | NormalizationLayerInfo _norm_info; |
| 796 | }; |
| 797 | |
Michele Di Giorgio | 555d110 | 2018-09-12 13:51:59 +0100 | [diff] [blame] | 798 | /** Normalize planar YUV Layer */ |
| 799 | class NormalizePlanarYUVLayer final : public ILayer |
| 800 | { |
| 801 | public: |
| 802 | /** Construct a normalize planar YUV layer. |
| 803 | * |
| 804 | * @param[in] mean Accessor to get mean tensor data from. |
| 805 | * @param[in] std Accessor to get std tensor data from. |
| 806 | */ |
| 807 | NormalizePlanarYUVLayer(ITensorAccessorUPtr mean, |
| 808 | ITensorAccessorUPtr std) |
| 809 | : _mean(std::move(mean)), _std(std::move(std)) |
| 810 | { |
| 811 | } |
| 812 | |
| 813 | NodeID create_layer(IStream &s) override |
| 814 | { |
| 815 | ARM_COMPUTE_ERROR_ON(_mean == nullptr); |
| 816 | ARM_COMPUTE_ERROR_ON(_std == nullptr); |
| 817 | |
| 818 | NodeParams common_params = { name(), s.hints().target_hint }; |
| 819 | NodeIdxPair input = { s.tail_node(), 0 }; |
| 820 | return GraphBuilder::add_normalize_planar_yuv_node(s.graph(), common_params, input, |
| 821 | std::move(_mean), std::move(_std)); |
| 822 | } |
| 823 | |
| 824 | private: |
| 825 | ITensorAccessorUPtr _mean; |
| 826 | ITensorAccessorUPtr _std; |
| 827 | }; |
| 828 | |
Michele Di Giorgio | 4bb1733 | 2018-09-26 13:56:51 +0100 | [diff] [blame] | 829 | /** Pad Layer */ |
| 830 | class PadLayer final : public ILayer |
| 831 | { |
| 832 | public: |
| 833 | /** Construct a pad layer. |
| 834 | * |
| 835 | * @param[in] padding The padding for each spatial dimension of the input tensor. The pair padding[i] |
| 836 | * specifies the front and the end padding in the i-th dimension. |
| 837 | */ |
| 838 | PadLayer(PaddingList padding) |
| 839 | : _padding(padding) |
| 840 | { |
| 841 | } |
| 842 | |
| 843 | NodeID create_layer(IStream &s) override |
| 844 | { |
| 845 | NodeParams common_params = { name(), s.hints().target_hint }; |
| 846 | NodeIdxPair input = { s.tail_node(), 0 }; |
| 847 | return GraphBuilder::add_pad_node(s.graph(), common_params, input, _padding); |
| 848 | } |
| 849 | |
| 850 | private: |
| 851 | PaddingList _padding; |
| 852 | }; |
| 853 | |
Georgios Pinitas | 57c4824 | 2018-08-02 13:41:49 +0100 | [diff] [blame] | 854 | /** Permute Layer */ |
| 855 | class PermuteLayer final : public ILayer |
| 856 | { |
| 857 | public: |
| 858 | /** Construct a permute layer. |
| 859 | * |
| 860 | * @param[in] perm Permutation vector. |
| 861 | * @param[in] layout (Optional) Data layout to assign to permuted tensor. |
| 862 | * If UNKNOWN then the input's layout will be used. |
| 863 | */ |
| 864 | PermuteLayer(PermutationVector perm, DataLayout layout = DataLayout::UNKNOWN) |
| 865 | : _perm(perm), _layout(layout) |
| 866 | { |
| 867 | } |
| 868 | |
| 869 | NodeID create_layer(IStream &s) override |
| 870 | { |
| 871 | NodeParams common_params = { name(), s.hints().target_hint }; |
| 872 | NodeIdxPair input = { s.tail_node(), 0 }; |
| 873 | return GraphBuilder::add_permute_node(s.graph(), common_params, input, _perm, _layout); |
| 874 | } |
| 875 | |
| 876 | private: |
| 877 | PermutationVector _perm; |
| 878 | DataLayout _layout; |
| 879 | }; |
| 880 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 881 | /** Pooling Layer */ |
| 882 | class PoolingLayer final : public ILayer |
| 883 | { |
| 884 | public: |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 885 | /** Construct a pooling layer. |
| 886 | * |
| 887 | * @param[in] pool_info Pooling information. |
| 888 | */ |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 889 | PoolingLayer(PoolingLayerInfo pool_info) |
| 890 | : _pool_info(pool_info) |
| 891 | { |
| 892 | } |
| 893 | |
| 894 | NodeID create_layer(IStream &s) override |
| 895 | { |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 896 | NodeParams common_params = { name(), s.hints().target_hint }; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 897 | NodeIdxPair input = { s.tail_node(), 0 }; |
| 898 | return GraphBuilder::add_pooling_node(s.graph(), common_params, input, _pool_info); |
| 899 | } |
| 900 | |
| 901 | private: |
| 902 | PoolingLayerInfo _pool_info; |
| 903 | }; |
| 904 | |
Georgios Pinitas | f8c4749 | 2020-02-04 17:39:59 +0000 | [diff] [blame^] | 905 | /** PRelu Layer */ |
| 906 | class PReluLayer final : public ILayer |
| 907 | { |
| 908 | public: |
| 909 | /** Construct an PRelu operation layer |
| 910 | * |
| 911 | * @param[in] sub_stream0 First graph sub-stream |
| 912 | * @param[in] sub_stream1 First graph sub-stream |
| 913 | */ |
| 914 | PReluLayer(SubStream &&sub_stream0, SubStream &&sub_stream1) |
| 915 | : _ss0(std::move(sub_stream0)), _ss1(std::move(sub_stream1)) |
| 916 | { |
| 917 | } |
| 918 | |
| 919 | NodeID create_layer(IStream &s) override |
| 920 | { |
| 921 | NodeParams common_params = { name(), s.hints().target_hint }; |
| 922 | NodeIdxPair input = { _ss0.tail_node(), 0 }; |
| 923 | NodeIdxPair alpha = { _ss1.tail_node(), 0 }; |
| 924 | |
| 925 | return GraphBuilder::add_prelu_node(s.graph(), common_params, input, alpha); |
| 926 | } |
| 927 | |
| 928 | private: |
| 929 | SubStream _ss0; |
| 930 | SubStream _ss1; |
| 931 | }; |
| 932 | |
Giorgio Arena | 6e9d0e0 | 2020-01-03 15:02:04 +0000 | [diff] [blame] | 933 | /** Print Layer */ |
| 934 | class PrintLayer final : public ILayer |
| 935 | { |
| 936 | public: |
| 937 | /** Construct a print layer. |
| 938 | * |
| 939 | * Example usage to locally dequantize and print a tensor: |
| 940 | * |
| 941 | * Tensor *output = new Tensor(); |
| 942 | * const auto transform = [output](ITensor *input) |
| 943 | * { |
| 944 | * output->allocator()->init(*input->info()); |
| 945 | * output->info()->set_data_type(DataType::F32); |
| 946 | * output->allocator()->allocate(); |
| 947 | * |
| 948 | * Window win; |
| 949 | * win.use_tensor_dimensions(input->info()->tensor_shape()); |
| 950 | * Iterator in(input, win); |
| 951 | * Iterator out(output, win); |
| 952 | * execute_window_loop(win, [&](const Coordinates &) |
| 953 | * { |
| 954 | * *(reinterpret_cast<float *>(out.ptr())) = dequantize_qasymm8(*in.ptr(), input->info()->quantization_info().uniform()); |
| 955 | * }, in, out); |
| 956 | * |
| 957 | * return output; |
| 958 | * }; |
| 959 | * |
| 960 | * graph << InputLayer(input_descriptor.set_quantization_info(in_quant_info), get_input_accessor(common_params, nullptr, false)) |
| 961 | * << ... |
| 962 | * << \\ CNN Layers |
| 963 | * << ... |
| 964 | * << PrintLayer(std::cout, IOFormatInfo(), transform) |
| 965 | * << ... |
| 966 | * << OutputLayer(get_output_accessor(common_params, 5)); |
| 967 | * |
| 968 | * @param[in] stream Output stream. |
| 969 | * @param[in] format_info (Optional) Format info. |
| 970 | * @param[in] transform (Optional) Input transform function. |
| 971 | */ |
| 972 | PrintLayer(std::ostream &stream, const IOFormatInfo &format_info = IOFormatInfo(), const std::function<ITensor *(ITensor *)> transform = nullptr) |
| 973 | : _stream(stream), _format_info(format_info), _transform(transform) |
| 974 | { |
| 975 | } |
| 976 | |
| 977 | NodeID create_layer(IStream &s) override |
| 978 | { |
| 979 | NodeParams common_params = { name(), s.hints().target_hint }; |
| 980 | NodeIdxPair input = { s.tail_node(), 0 }; |
| 981 | return GraphBuilder::add_print_node(s.graph(), common_params, input, _stream, _format_info, _transform); |
| 982 | } |
| 983 | |
| 984 | private: |
| 985 | std::ostream &_stream; |
| 986 | const IOFormatInfo &_format_info; |
| 987 | const std::function<ITensor *(ITensor *)> _transform; |
| 988 | }; |
| 989 | |
Pablo Tello | 3252143 | 2018-11-15 14:43:10 +0000 | [diff] [blame] | 990 | /** PriorBox Layer */ |
| 991 | class PriorBoxLayer final : public ILayer |
| 992 | { |
| 993 | public: |
| 994 | /** Construct a priorbox layer. |
| 995 | * |
| 996 | * @param[in] sub_stream First graph sub-stream |
| 997 | * @param[in] prior_info PriorBox parameters. |
| 998 | */ |
Georgios Pinitas | f52cd78 | 2019-03-25 14:06:14 +0000 | [diff] [blame] | 999 | PriorBoxLayer(SubStream &&sub_stream, const PriorBoxLayerInfo &prior_info) |
Pablo Tello | 3252143 | 2018-11-15 14:43:10 +0000 | [diff] [blame] | 1000 | : _ss(std::move(sub_stream)), _prior_info(prior_info) |
| 1001 | { |
| 1002 | } |
| 1003 | |
| 1004 | NodeID create_layer(IStream &s) override |
| 1005 | { |
| 1006 | NodeParams common_params = { name(), s.hints().target_hint }; |
| 1007 | NodeIdxPair input0 = { s.tail_node(), 0 }; |
| 1008 | NodeIdxPair input1 = { _ss.tail_node(), 0 }; |
| 1009 | return GraphBuilder::add_priorbox_node(s.graph(), common_params, input0, input1, _prior_info); |
| 1010 | } |
| 1011 | |
| 1012 | private: |
| 1013 | SubStream _ss; |
| 1014 | PriorBoxLayerInfo _prior_info; |
| 1015 | }; |
| 1016 | |
Isabella Gottardi | 3db1ba9 | 2019-05-17 12:35:20 +0100 | [diff] [blame] | 1017 | /** Quantization Layer */ |
| 1018 | class QuantizationLayer final : public ILayer |
| 1019 | { |
| 1020 | public: |
| 1021 | /** Construct a quantization layer. |
| 1022 | * |
| 1023 | * @param[in] out_quant_info Output tensor quantization info |
| 1024 | */ |
| 1025 | QuantizationLayer(QuantizationInfo out_quant_info) |
| 1026 | : _out_quant_info(out_quant_info) |
| 1027 | { |
| 1028 | } |
| 1029 | |
| 1030 | NodeID create_layer(IStream &s) override |
| 1031 | { |
| 1032 | NodeParams common_params = { name(), s.hints().target_hint }; |
| 1033 | NodeIdxPair input = { s.tail_node(), 0 }; |
| 1034 | return GraphBuilder::add_quantization_node(s.graph(), common_params, input, _out_quant_info); |
| 1035 | } |
| 1036 | |
| 1037 | private: |
| 1038 | QuantizationInfo _out_quant_info; |
| 1039 | }; |
| 1040 | |
Gian Marco Iodice | 23e2479 | 2018-09-07 15:32:14 +0100 | [diff] [blame] | 1041 | /** Reorg Layer */ |
| 1042 | class ReorgLayer final : public ILayer |
| 1043 | { |
| 1044 | public: |
| 1045 | /** Construct a reorg layer. |
| 1046 | * |
| 1047 | * @param[in] stride Stride value to use for reorganizing the values in the output tensor. |
| 1048 | * It defines the spatial distance between 2 consecutive pixels in the x and y direction |
| 1049 | */ |
| 1050 | ReorgLayer(int stride) |
| 1051 | : _stride(stride) |
| 1052 | { |
| 1053 | } |
| 1054 | |
| 1055 | NodeID create_layer(IStream &s) override |
| 1056 | { |
| 1057 | NodeParams common_params = { name(), s.hints().target_hint }; |
| 1058 | NodeIdxPair input = { s.tail_node(), 0 }; |
| 1059 | return GraphBuilder::add_reorg_node(s.graph(), common_params, input, _stride); |
| 1060 | } |
| 1061 | |
| 1062 | private: |
| 1063 | int _stride; |
| 1064 | }; |
| 1065 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 1066 | /** Reshape Layer */ |
| 1067 | class ReshapeLayer final : public ILayer |
| 1068 | { |
| 1069 | public: |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 1070 | /** Construct a reshape layer. |
| 1071 | * |
| 1072 | * @param[in] shape Target shape. |
| 1073 | */ |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 1074 | ReshapeLayer(TensorShape shape) |
| 1075 | : _shape(shape) |
| 1076 | { |
| 1077 | } |
| 1078 | |
| 1079 | NodeID create_layer(IStream &s) override |
| 1080 | { |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 1081 | NodeParams common_params = { name(), s.hints().target_hint }; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 1082 | NodeIdxPair input = { s.tail_node(), 0 }; |
| 1083 | return GraphBuilder::add_reshape_node(s.graph(), common_params, input, _shape); |
| 1084 | } |
| 1085 | |
| 1086 | private: |
| 1087 | TensorShape _shape; |
| 1088 | }; |
| 1089 | |
Georgios Pinitas | 087eaf6 | 2018-05-16 15:52:35 +0100 | [diff] [blame] | 1090 | /** Resize Layer */ |
| 1091 | class ResizeLayer final : public ILayer |
| 1092 | { |
| 1093 | public: |
| 1094 | ResizeLayer(InterpolationPolicy policy, float width_scale, float height_scale) |
| 1095 | : _policy(policy), _width_scale(width_scale), _height_scale(height_scale) |
| 1096 | { |
| 1097 | } |
| 1098 | |
| 1099 | NodeID create_layer(IStream &s) override |
| 1100 | { |
| 1101 | NodeParams common_params = { name(), s.hints().target_hint }; |
| 1102 | NodeIdxPair input = { s.tail_node(), 0 }; |
| 1103 | return GraphBuilder::add_resize_node(s.graph(), common_params, input, _policy, _width_scale, _height_scale); |
| 1104 | } |
| 1105 | |
| 1106 | private: |
| 1107 | InterpolationPolicy _policy; |
| 1108 | float _width_scale; |
| 1109 | float _height_scale; |
| 1110 | }; |
| 1111 | |
Manuel Bottini | 3f9d4d7 | 2018-10-19 14:04:42 +0100 | [diff] [blame] | 1112 | /** ROIAlign Layer */ |
| 1113 | class ROIAlignLayer final : public ILayer |
| 1114 | { |
| 1115 | public: |
| 1116 | /** Construct a RoiAlign layer. |
| 1117 | * |
| 1118 | * @param[in] sub_stream_input Graph sub-stream for the input |
| 1119 | * @param[in] sub_stream_rois Graph sub-stream for the rois |
| 1120 | * @param[in] pool_info Pooling information. |
| 1121 | */ |
| 1122 | ROIAlignLayer(SubStream &&sub_stream_input, SubStream &&sub_stream_rois, ROIPoolingLayerInfo pool_info) |
| 1123 | : _ss_input(sub_stream_input), _ss_rois(sub_stream_rois), _pool_info(pool_info) |
| 1124 | { |
| 1125 | } |
| 1126 | |
| 1127 | /** Prevent instances of this class from being copy constructed */ |
| 1128 | ROIAlignLayer(const ROIAlignLayer &) = delete; |
| 1129 | /** Prevent instances of this class from being copied */ |
| 1130 | ROIAlignLayer &operator=(const ROIAlignLayer &) = delete; |
| 1131 | |
| 1132 | NodeID create_layer(IStream &s) override |
| 1133 | { |
| 1134 | NodeParams common_params = { name(), s.hints().target_hint }; |
| 1135 | NodeIdxPair input = { _ss_input.tail_node(), 0 }; |
| 1136 | NodeIdxPair rois = { _ss_rois.tail_node(), 0 }; |
| 1137 | return GraphBuilder::add_roi_align_node(s.graph(), common_params, input, rois, _pool_info); |
| 1138 | } |
| 1139 | |
| 1140 | private: |
| 1141 | SubStream _ss_input; |
| 1142 | SubStream _ss_rois; |
| 1143 | ROIPoolingLayerInfo _pool_info; |
| 1144 | }; |
| 1145 | |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 1146 | /** Scale Layer */ |
| 1147 | class ScaleLayer final : public ILayer |
| 1148 | { |
| 1149 | public: |
| 1150 | /** Construct a scale layer. |
| 1151 | * |
| 1152 | * @param[in] mul_w Accessor to get mul weight from. |
| 1153 | * @param[in] add_w Accessor to get add weight from. |
| 1154 | */ |
| 1155 | ScaleLayer(ITensorAccessorUPtr mul_w, |
| 1156 | ITensorAccessorUPtr add_w) |
| 1157 | : _mul_w(std::move(mul_w)), _add_w(std::move(add_w)) |
| 1158 | { |
| 1159 | } |
| 1160 | |
| 1161 | NodeID create_layer(IStream &s) override |
| 1162 | { |
| 1163 | NodeParams common_params = { name(), s.hints().target_hint }; |
| 1164 | NodeIdxPair input = { s.tail_node(), 0 }; |
| 1165 | return GraphBuilder::add_scale_layer(s.graph(), common_params, input, std::move(_mul_w), std::move(_add_w)); |
| 1166 | } |
| 1167 | |
| 1168 | private: |
| 1169 | ITensorAccessorUPtr _mul_w; |
| 1170 | ITensorAccessorUPtr _add_w; |
| 1171 | }; |
| 1172 | |
Michele Di Giorgio | c30b668 | 2018-09-12 17:44:08 +0100 | [diff] [blame] | 1173 | /** Slice Layer */ |
| 1174 | class SliceLayer final : public ILayer |
| 1175 | { |
| 1176 | public: |
| 1177 | /** Construct a slice layer. |
| 1178 | * |
| 1179 | * @param[in] starts The starts of the dimensions of the input tensor to be sliced. The length must be of rank(input). |
| 1180 | * @param[in] ends The ends of the dimensions of the input tensor to be sliced. The length must be of rank(input). |
| 1181 | */ |
| 1182 | SliceLayer(Coordinates &starts, Coordinates &ends) |
| 1183 | : _starts(starts), _ends(ends) |
| 1184 | { |
| 1185 | } |
| 1186 | |
| 1187 | NodeID create_layer(IStream &s) override |
| 1188 | { |
| 1189 | NodeParams common_params = { name(), s.hints().target_hint }; |
| 1190 | NodeIdxPair input = { s.tail_node(), 0 }; |
| 1191 | return GraphBuilder::add_slice_node(s.graph(), common_params, input, _starts, _ends); |
| 1192 | } |
| 1193 | |
| 1194 | private: |
| 1195 | Coordinates _starts; |
| 1196 | Coordinates _ends; |
| 1197 | }; |
| 1198 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 1199 | /** Softmax Layer */ |
| 1200 | class SoftmaxLayer final : public ILayer |
| 1201 | { |
| 1202 | public: |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 1203 | /** Construct a softmax layer. |
| 1204 | * |
| 1205 | * @param[in] beta (Optional) Beta value. Default 1.0. |
| 1206 | */ |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 1207 | SoftmaxLayer(float beta = 1.0f) |
| 1208 | : _beta(beta) |
| 1209 | { |
| 1210 | } |
| 1211 | |
| 1212 | NodeID create_layer(IStream &s) override |
| 1213 | { |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 1214 | NodeParams common_params = { name(), s.hints().target_hint }; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 1215 | NodeIdxPair input = { s.tail_node(), 0 }; |
| 1216 | return GraphBuilder::add_softmax_node(s.graph(), common_params, input, _beta); |
| 1217 | } |
| 1218 | |
| 1219 | private: |
| 1220 | float _beta; |
| 1221 | }; |
Michalis Spyrou | 96f6769 | 2018-09-13 11:39:28 +0100 | [diff] [blame] | 1222 | |
Michele Di Giorgio | ec69975 | 2019-03-22 15:25:32 +0000 | [diff] [blame] | 1223 | /** Stack Layer */ |
| 1224 | class StackLayer final : public ILayer |
| 1225 | { |
| 1226 | public: |
| 1227 | /** Construct a concatenation layer |
| 1228 | * |
| 1229 | * @param[in] sub_stream1 First graph branch |
| 1230 | * @param[in] sub_stream2 Second graph branch |
| 1231 | * @param[in] rest_sub_streams Rest sub-graph branches |
| 1232 | */ |
| 1233 | template <typename... Ts> |
| 1234 | StackLayer(SubStream &&sub_stream1, SubStream &&sub_stream2, Ts &&... rest_sub_streams) |
| 1235 | : _sub_streams(), _axis(0) |
| 1236 | { |
| 1237 | _sub_streams.push_back(arm_compute::support::cpp14::make_unique<SubStream>(std::move(sub_stream1))); |
| 1238 | _sub_streams.push_back(arm_compute::support::cpp14::make_unique<SubStream>(std::move(sub_stream2))); |
| 1239 | |
| 1240 | utility::for_each([&](SubStream && sub_stream) |
| 1241 | { |
| 1242 | _sub_streams.push_back(arm_compute::support::cpp14::make_unique<SubStream>(std::move(sub_stream))); |
| 1243 | }, |
| 1244 | std::move(rest_sub_streams)...); |
| 1245 | } |
| 1246 | /** Construct a concatenation layer |
| 1247 | * |
| 1248 | * @param[in] axis Stack layer axis along which to stack the inputs |
| 1249 | * @param[in] sub_stream1 First graph branch |
| 1250 | * @param[in] sub_stream2 Second graph branch |
| 1251 | * @param[in] rest_sub_streams Rest sub-graph branches |
| 1252 | */ |
| 1253 | template <typename... Ts> |
| 1254 | StackLayer(int axis, SubStream &&sub_stream1, SubStream &&sub_stream2, Ts &&... rest_sub_streams) |
| 1255 | : _sub_streams(), _axis(axis) |
| 1256 | { |
| 1257 | _sub_streams.push_back(arm_compute::support::cpp14::make_unique<SubStream>(std::move(sub_stream1))); |
| 1258 | _sub_streams.push_back(arm_compute::support::cpp14::make_unique<SubStream>(std::move(sub_stream2))); |
| 1259 | |
| 1260 | utility::for_each([&](SubStream && sub_stream) |
| 1261 | { |
| 1262 | _sub_streams.push_back(arm_compute::support::cpp14::make_unique<SubStream>(std::move(sub_stream))); |
| 1263 | }, |
| 1264 | std::move(rest_sub_streams)...); |
| 1265 | } |
| 1266 | /** Construct a concat layer |
| 1267 | * |
| 1268 | * @param[in] sub_stream Sub-stream |
| 1269 | */ |
| 1270 | template <typename... Ts> |
| 1271 | StackLayer(SubStream &&sub_stream) |
| 1272 | : _sub_streams(), _axis(0) |
| 1273 | { |
| 1274 | _sub_streams.push_back(arm_compute::support::cpp14::make_unique<SubStream>(std::move(sub_stream))); |
| 1275 | } |
| 1276 | NodeID create_layer(IStream &s) override |
| 1277 | { |
| 1278 | NodeID nid = EmptyNodeID; |
| 1279 | NodeParams common_params = { name(), s.hints().target_hint }; |
| 1280 | if(_sub_streams.size() == 1 && _sub_streams.at(0) != nullptr) |
| 1281 | { |
| 1282 | nid = _sub_streams[0]->tail_node(); |
| 1283 | } |
| 1284 | else |
| 1285 | { |
| 1286 | // Collect tail nodes and stack |
| 1287 | std::vector<NodeIdxPair> nodes; |
| 1288 | for(auto &ss : _sub_streams) |
| 1289 | { |
| 1290 | if(ss && (ss->tail_node() != EmptyNodeID)) |
| 1291 | { |
| 1292 | const auto tail_node = s.graph().node(ss->tail_node()); |
| 1293 | if(tail_node != nullptr && tail_node->type() != NodeType::Output) |
| 1294 | { |
| 1295 | nodes.push_back({ ss->tail_node(), 0 }); |
| 1296 | } |
| 1297 | } |
| 1298 | } |
| 1299 | nid = GraphBuilder::add_stack_node(s.graph(), common_params, nodes, _axis); |
| 1300 | } |
| 1301 | return nid; |
| 1302 | } |
| 1303 | |
| 1304 | private: |
| 1305 | std::vector<std::unique_ptr<SubStream>> _sub_streams; |
| 1306 | int _axis; |
| 1307 | }; |
| 1308 | |
Michalis Spyrou | 4e1c3f3 | 2018-09-20 17:14:03 +0100 | [diff] [blame] | 1309 | /** Upsample Layer */ |
| 1310 | class UpsampleLayer final : public ILayer |
| 1311 | { |
| 1312 | public: |
| 1313 | /** Construct a Upsample layer. |
| 1314 | * |
| 1315 | * @param[in] info Stride info |
| 1316 | * @param[in] upsampling_policy Upsampling policy |
| 1317 | */ |
| 1318 | UpsampleLayer(Size2D info, InterpolationPolicy upsampling_policy) |
| 1319 | : _info(info), _upsampling_policy(upsampling_policy) |
| 1320 | { |
| 1321 | } |
| 1322 | |
| 1323 | NodeID create_layer(IStream &s) override |
| 1324 | { |
| 1325 | NodeParams common_params = { name(), s.hints().target_hint }; |
| 1326 | NodeIdxPair input = { s.tail_node(), 0 }; |
| 1327 | return GraphBuilder::add_upsample_node(s.graph(), common_params, input, _info, _upsampling_policy); |
| 1328 | } |
| 1329 | |
| 1330 | private: |
| 1331 | Size2D _info; |
| 1332 | InterpolationPolicy _upsampling_policy; |
| 1333 | }; |
| 1334 | |
Michalis Spyrou | 96f6769 | 2018-09-13 11:39:28 +0100 | [diff] [blame] | 1335 | /** YOLO Layer */ |
| 1336 | class YOLOLayer final : public ILayer |
| 1337 | { |
| 1338 | public: |
| 1339 | /** Construct a YOLO layer. |
| 1340 | * |
| 1341 | * @param[in] act_info Activation info |
| 1342 | * @param[in] num_classes Number of classes to activate |
| 1343 | */ |
| 1344 | YOLOLayer(ActivationLayerInfo act_info, int32_t num_classes) |
| 1345 | : _act_info(act_info), _num_classes(num_classes) |
| 1346 | { |
| 1347 | } |
| 1348 | |
| 1349 | NodeID create_layer(IStream &s) override |
| 1350 | { |
| 1351 | NodeParams common_params = { name(), s.hints().target_hint }; |
| 1352 | NodeIdxPair input = { s.tail_node(), 0 }; |
| 1353 | return GraphBuilder::add_yolo_node(s.graph(), common_params, input, _act_info, _num_classes); |
| 1354 | } |
| 1355 | |
| 1356 | private: |
| 1357 | ActivationLayerInfo _act_info; |
| 1358 | int32_t _num_classes; |
| 1359 | }; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 1360 | } // namespace frontend |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 1361 | } // namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 1362 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 1363 | #endif /* ARM_COMPUTE_GRAPH_LAYERS_H */ |