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