Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 1 | /* |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [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 | */ |
| 24 | #ifndef __ARM_COMPUTE_GRAPH_CONVOLUTION_LAYER_H__ |
| 25 | #define __ARM_COMPUTE_GRAPH_CONVOLUTION_LAYER_H__ |
| 26 | |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 27 | #include "arm_compute/graph/GraphContext.h" |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 28 | #include "arm_compute/graph/INode.h" |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 29 | #include "arm_compute/graph/ITensorObject.h" |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 30 | #include "arm_compute/graph/SubTensor.h" |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 31 | #include "arm_compute/graph/Tensor.h" |
| 32 | #include "arm_compute/graph/Types.h" |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 33 | #include "arm_compute/runtime/IFunction.h" |
| 34 | |
| 35 | #include <memory> |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 36 | |
| 37 | namespace arm_compute |
| 38 | { |
| 39 | namespace graph |
| 40 | { |
| 41 | /** Convolution layer node */ |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 42 | class ConvolutionLayer final : public INode |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 43 | { |
| 44 | public: |
| 45 | /** Default Constructor |
| 46 | * |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 47 | * @param[in] conv_width Convolution width |
| 48 | * @param[in] conv_height Convolution height |
| 49 | * @param[in] ofm Output feature map |
| 50 | * @param[in] weights Weights of the convolution layer |
| 51 | * @param[in] biases Bias of the convolution layer |
| 52 | * @param[in] conv_info Convolution information |
| 53 | * @param[in] num_groups (Optional) Number of groups, default = 1 |
| 54 | * @param[in] weights_info (Optional) Weights information |
| 55 | * @param[in] weights_quant_info (Optional) Weights quantization information |
| 56 | * @param[in] out_quant_info (Optional) Output quantization info |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 57 | */ |
| 58 | template <typename AccessorTypeWeights, typename AccessorTypeBiases> |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 59 | ConvolutionLayer(unsigned int conv_width, |
| 60 | unsigned int conv_height, |
| 61 | unsigned int ofm, |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 62 | AccessorTypeWeights &&weights, |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 63 | AccessorTypeBiases &&biases, |
| 64 | const PadStrideInfo conv_info, |
| 65 | unsigned int num_groups = 1, |
| 66 | const WeightsInfo weights_info = WeightsInfo(), |
| 67 | const QuantizationInfo weights_quant_info = QuantizationInfo(), |
| 68 | const QuantizationInfo out_quant_info = QuantizationInfo()) |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 69 | : _conv_width(conv_width), |
| 70 | _conv_height(conv_height), |
| 71 | _ofm(ofm), |
| 72 | _weights(std::move(weights)), |
| 73 | _biases(std::move(biases)), |
| 74 | _conv_info(std::move(conv_info)), |
| 75 | _num_groups(num_groups), |
| 76 | _weights_info(std::move(weights_info)), |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 77 | _weights_quant_info(std::move(weights_quant_info)), |
| 78 | _out_quant_info(std::move(out_quant_info)), |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 79 | _is(nullptr), |
| 80 | _os(nullptr), |
| 81 | _ws(nullptr), |
| 82 | _bs(nullptr) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 83 | { |
| 84 | } |
| 85 | |
| 86 | // Inherited methods overriden: |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 87 | std::unique_ptr<arm_compute::IFunction> instantiate_node(GraphContext &ctx, ITensorObject *input, ITensorObject *output) override; |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 88 | |
| 89 | private: |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 90 | /** Instantiates a non-grouped convolution |
| 91 | * |
Michalis Spyrou | e472082 | 2017-10-02 17:44:52 +0100 | [diff] [blame] | 92 | * @param[in] input Input tensor |
| 93 | * @param[in] output Output tensor |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 94 | * @param[in] conv_method_hint Hint that specifies which convolution layer method to use |
| 95 | * |
| 96 | * @return Convolution function |
| 97 | */ |
Michalis Spyrou | e472082 | 2017-10-02 17:44:52 +0100 | [diff] [blame] | 98 | std::unique_ptr<arm_compute::IFunction> instantiate_convolution(ITensor *input, ITensor *output, ConvolutionMethodHint conv_method_hint); |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 99 | /** Instantiates a grouped convolution |
| 100 | * |
Michalis Spyrou | e472082 | 2017-10-02 17:44:52 +0100 | [diff] [blame] | 101 | * @param[in] input Input tensor |
| 102 | * @param[in] output Output tensor |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 103 | * @param[in] conv_method_hint Hint that specifies which convolution layer method to use |
| 104 | * |
| 105 | * @return Grouped Convolution function |
| 106 | */ |
Michalis Spyrou | e472082 | 2017-10-02 17:44:52 +0100 | [diff] [blame] | 107 | std::unique_ptr<arm_compute::IFunction> instantiate_grouped_convolution(ITensor *input, ITensor *output, ConvolutionMethodHint conv_method_hint); |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 108 | |
| 109 | private: |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 110 | unsigned int _conv_width; /**< Convolution width */ |
| 111 | unsigned int _conv_height; /**< Convolution height */ |
| 112 | unsigned int _ofm; /**< Output feature maps */ |
| 113 | Tensor _weights; /**< Weights tensor */ |
| 114 | Tensor _biases; /**< Biases tensor */ |
| 115 | const PadStrideInfo _conv_info; /**< Convolution layer information */ |
| 116 | unsigned int _num_groups; /**< Number of groups */ |
| 117 | const WeightsInfo _weights_info; /**< Convolution layer weights information */ |
| 118 | const QuantizationInfo _weights_quant_info; /**< Output quantization information */ |
| 119 | const QuantizationInfo _out_quant_info; /**< Output quantization information */ |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 120 | |
| 121 | std::unique_ptr<SubTensor[]> _is; /**< Input tensor sub-tensors used for grouped convolution */ |
| 122 | std::unique_ptr<SubTensor[]> _os; /**< Output tensor sub-tensors used for grouped convolution */ |
| 123 | std::unique_ptr<SubTensor[]> _ws; /**< Weights tensor sub-tensors used for grouped convolution */ |
| 124 | std::unique_ptr<SubTensor[]> _bs; /**< Biases tensor sub-tensors used for grouped convolution */ |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 125 | }; |
| 126 | } // namespace graph |
| 127 | } // namespace arm_compute |
| 128 | #endif /* __ARM_COMPUTE_GRAPH_CONVOLUTION_LAYER_H__ */ |