2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * Copyright (c) 2017-2019 ARM Limited.
20 * SPDX-License-Identifier: MIT
22 * Permission is hereby granted, free of charge, to any person obtaining a copy
23 * of this software and associated documentation files (the "Software"), to
24 * deal in the Software without restriction, including without limitation the
25 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
26 * sell copies of the Software, and to permit persons to whom the Software is
27 * furnished to do so, subject to the following conditions:
29 * The above copyright notice and this permission notice shall be included in all
30 * copies or substantial portions of the Software.
32 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
41 #ifndef __ARM_COMPUTE_CLFULLYCONNECTEDHYBRIDLAYER_H__
42 #define __ARM_COMPUTE_CLFULLYCONNECTEDHYBRIDLAYER_H__
44 #include "arm_compute/runtime/CL/ICLSimpleFunction.h"
46 #include "arm_compute/core/CL/kernels/CLGEMMMatrixAccumulateBiasesKernel.h"
47 #include "arm_compute/core/CL/kernels/CLMultiplyScaleFactorKernel.h"
48 #include "arm_compute/core/CL/kernels/CLQuantizationSymmetricKernel.h"
49 #include "arm_compute/core/CL/kernels/CLScaleFactorSymm8Kernel.h"
50 #include "arm_compute/core/CL/kernels/CLTransposeKernel.h"
51 #include "arm_compute/runtime/MemoryGroup.h"
52 #include "arm_compute/runtime/CL/CLTensor.h"
53 #include "arm_compute/runtime/CL/functions/CLGEMMLowpMatrixMultiplyCore.h"
57 /** Basic function to reshape the weights of Fully Connected layer with OpenCL. This function calls
58 * the following kernels:
60 * -# @ref CLTransposeKernel
62 * @note The fully connected layer accepts "weights" tensors only with 2 dimensions.
64 class CLFullyConnectedHybridLayerReshapeWeights : public ICLSimpleFunction
67 /** Set the input and output tensors.
69 * @param[in] input Weights tensor. The weights must be 2 dimensional. Data types supported:
71 * @param[out] output Destination tensor which stores the transposed input tensor. Data type
72 * supported: Same as @p input.
74 void configure(const ICLTensor *input, ICLTensor *output);
75 /** Static function to check if given info will lead to a valid configuration of @ref
76 * CLFullyConnectedHybridLayerReshapeWeights
78 * @param[in] input Weights tensor. The weights must be 2 dimensional. Data types supported:
80 * @param[in] output Destination tensor which stores the transposed input tensor. Data type
81 * supported: Same as @p input.
85 static Status validate(const ITensorInfo *input, const ITensorInfo *output);
88 /** Basic function to compute a Fully Connected layer on OpenCL. This function calls the following
91 * -# @ref CLIm2ColKernel (called when the input comes from a convolutional layer)
92 * -# @ref CLFullyConnectedHybridLayerReshapeWeights (if @p are_weights_reshaped is set to false
93 * and transpose_weights is set to true ) (called once)
94 * -# @ref CLGEMMLowpMatrixMultiplyCore (if quantized symmetric)
95 * -# @ref CLGEMMMatrixAccumulateBiasesKernel (if @p biases is not equal to nullptr)
97 * @note The fully connected layer accepts "weights" tensors only with 2 dimensions.
99 class CLFullyConnectedHybridLayer : public IFunction
103 CLFullyConnectedHybridLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
104 /** Prevent instances of this class from being copied (As this class contains pointers) */
105 CLFullyConnectedHybridLayer(const CLFullyConnectedHybridLayer &) = delete;
106 /** Default move constructor */
107 CLFullyConnectedHybridLayer(CLFullyConnectedHybridLayer &&) = default;
108 /** Prevent instances of this class from being copied (As this class contains pointers) */
109 CLFullyConnectedHybridLayer &operator=(const CLFullyConnectedHybridLayer &) = delete;
110 /** Default move assignment operator */
111 CLFullyConnectedHybridLayer &operator=(CLFullyConnectedHybridLayer &&) = default;
112 /** Set the input and output tensors.
114 * @param[in] input Source tensor. Data type supported: F16/F32.
115 * @param[in] weights Weights tensor. The weights must be 2 dimensional.
116 * If this function is called after a Convolution Layer, the (transposed)
117 * weights will have as many rows as the product of the first 3 input's dimensions.
118 * If it is called after another FullyConnected Layer, the (transposed)
119 * weights will have as many rows as the input's first dimension.
120 * Data type supported: S8.
121 * @param[in] biases Bias tensor. Can be nullptr. Data type supported:Same as @p input.
122 * @param[out] output Destination tensor. Its shape should be equal to the output of a matrix
123 * multiplication between:
124 * - The output of im2col on the input and the (transposed) 2D weights, if the
125 * function is called after a Convolution Layer
126 * - The input tensor and the (transposed) 2D weights, if the function is
127 * called after another FullyConnected Layer.
128 * Data type supported: Same as @p input.
129 * @param[in] fc_info (Optional) Fully connected layer additional info
131 void configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases,
132 ICLTensor *output, FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo());
133 /** Static function to check if given info will lead to a valid configuration of @ref
134 * CLFullyConnectedHybridLayer
136 * @param[in] input Source tensor info. Data type supported: F16/F32.
137 * @param[in] weights Weights tensor info. The weights must be 2 dimensional.
138 * If this function is called after a Convolution Layer, the (transposed)
139 * weights will have as many rows as the product of the first 3 input's dimensions.
140 * If it is called after another FullyConnected Layer, the (transposed)
141 * weights will have as many rows as the input's first dimension.
142 * Data type supported: S8.
143 * @param[in] biases Bias tensor info. Can be nullptr. Data type supported:Same as @p input.
144 * @param[out] output Destination tensor info. Its shape should be equal to the output of a
145 * matrix multiplication between:
146 * - The output of im2col on the input and the (transposed) 2D weights, if the
147 * function is called after a Convolution Layer
148 * - The input tensor and the (transposed) 2D weights, if the function is
149 * called after another FullyConnected Layer.
150 * Data type supported: Same as @p input.
151 * @param[in] fc_info (Optional) Fully connected layer additional info
155 static Status validate(const ITensorInfo *input, const ITensorInfo *weights,
156 const ITensorInfo *biases, const ITensorInfo *output,
157 FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo());
159 // Inherited methods override
161 void prepare() override;
164 void configure_mm(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output,
165 bool retain_internal_weights);
167 MemoryGroup _memory_group;
168 CLFullyConnectedHybridLayerReshapeWeights _reshape_weights_kernel;
169 CLScaleFactorSymm8Kernel _scale_factor_kernel;
170 CLQuantizationSymmetricKernel _quant_input_kernel;
171 CLGEMMLowpMatrixMultiplyCore _mm_gemmlowp;
172 CLMultiplyScaleFactorKernel _multiply_scale_kernel;
173 CLGEMMMatrixAccumulateBiasesKernel _accumulate_biases_kernel; // TODO(COMPMID-1889): Use CLGEMM to
175 // CLFullyConnectedHybridLayer
176 CLTensor _reshape_weights_output;
177 CLTensor _quantized_input;
178 CLTensor _scale_factor;
179 CLTensor _gemmlowp_output;
180 bool _are_weights_reshaped;
181 bool _accumulate_biases;
183 const ICLTensor *_original_weights;
186 #endif /* __ARM_COMPUTE_CLFULLYCONNECTEDHYBRIDLAYER_H__ */