Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +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_TEST_REFERENCE_REFERENCE_CPP_H__ |
| 25 | #define __ARM_COMPUTE_TEST_REFERENCE_REFERENCE_CPP_H__ |
| 26 | |
Isabella Gottardi | b797fa2 | 2017-06-23 15:02:11 +0100 | [diff] [blame] | 27 | #include "RawTensor.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | #include "Reference.h" |
| 29 | |
Isabella Gottardi | b797fa2 | 2017-06-23 15:02:11 +0100 | [diff] [blame] | 30 | #include <map> |
Georgios Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 31 | #include <memory> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 32 | #include <ostream> |
Georgios Pinitas | d4f8c27 | 2017-06-30 16:16:19 +0100 | [diff] [blame] | 33 | #include <vector> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | |
| 35 | namespace arm_compute |
| 36 | { |
| 37 | class Tensor; |
| 38 | |
| 39 | namespace test |
| 40 | { |
| 41 | namespace validation |
| 42 | { |
| 43 | /** C++ reference implementation. */ |
| 44 | class ReferenceCPP final : public Reference |
| 45 | { |
| 46 | public: |
Giorgio Arena | 50f9fd7 | 2017-06-19 17:05:30 +0100 | [diff] [blame] | 47 | /** Function to compute reference sobel 3x3. |
| 48 | * |
| 49 | * @param[in] src Input tensor. |
| 50 | * @param[in] dst_x Result tensor along x axis |
| 51 | * @param[in] dst_y Result tensor along y axis |
| 52 | * @param[in] border_mode Border mode to use for input tensor |
| 53 | * @param[in] constant_border_value Constant value to use if @p border_mode is constant |
| 54 | * |
| 55 | */ |
| 56 | static void sobel_3x3(RawTensor &src, RawTensor &dst_x, RawTensor &dst_y, BorderMode border_mode, uint8_t constant_border_value); |
| 57 | /** Function to compute reference sobel 5x5. |
| 58 | * |
| 59 | * @param[in] src Input tensor. |
| 60 | * @param[in] dst_x Result tensor along x axis |
| 61 | * @param[in] dst_y Result tensor along y axis |
| 62 | * @param[in] border_mode Border mode to use for input tensor |
| 63 | * @param[in] constant_border_value Constant value to use if @p border_mode is constant |
| 64 | * |
| 65 | */ |
| 66 | static void sobel_5x5(RawTensor &src, RawTensor &dst_x, RawTensor &dst_y, BorderMode border_mode, uint8_t constant_border_value); |
Giorgio Arena | 2ca209e | 2017-06-13 15:49:37 +0100 | [diff] [blame] | 67 | /** Function to compute the min max values and their location in a tensor. |
| 68 | * |
| 69 | * @param[in] src Input tensor. |
| 70 | * @param[out] min Minimum value of the tensor. |
| 71 | * @param[out] max Maximum value of the tensor |
| 72 | * @param[out] min_loc Array with locations of minimum values |
| 73 | * @param[out] max_loc Array with locations of maximum values |
| 74 | * @param[out] min_count Number of minimum values found |
| 75 | * @param[out] max_count Number of maximum values found |
| 76 | */ |
Giorgio Arena | 935deee | 2017-06-14 13:40:36 +0100 | [diff] [blame] | 77 | static void min_max_location(const RawTensor &src, int32_t &min, int32_t &max, IArray<Coordinates2D> &min_loc, IArray<Coordinates2D> &max_loc, uint32_t &min_count, uint32_t &max_count); |
Giorgio Arena | f795986 | 2017-06-13 15:19:51 +0100 | [diff] [blame] | 78 | /** Function to compute the mean and standard deviation of a tensor. |
| 79 | * |
| 80 | * @param[in] src Input tensor. |
| 81 | * @param[out] mean Mean of the tensor. |
| 82 | * @param[out] std_dev Standard deviation of the tensor |
| 83 | */ |
| 84 | static void mean_and_standard_deviation(const RawTensor &src, float &mean, float &std_dev); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 85 | /** Function to compute the integral image of a tensor. |
| 86 | * |
| 87 | * @param[in] src Input tensor. |
| 88 | * @param[out] dst Result tensor. |
| 89 | */ |
| 90 | static void integral_image(const RawTensor &src, RawTensor &dst); |
| 91 | /** Function to compute the absolute difference between two tensors. |
| 92 | * |
| 93 | * @param[in] src1 First tensor. |
| 94 | * @param[in] src2 Second tensor. |
| 95 | * @param[out] dst Result tensor. |
| 96 | */ |
| 97 | static void absolute_difference(const RawTensor &src1, const RawTensor &src2, RawTensor &dst); |
| 98 | /** Function to accumulate an input tensor into an output tensor. |
| 99 | * |
| 100 | * @param[in] src Input tensor. |
| 101 | * @param[in, out] dst Result tensor. |
| 102 | */ |
| 103 | static void accumulate(const RawTensor &src, RawTensor &dst); |
| 104 | /** Function to accumulate a squared value from an input tensor to an output tensor. |
| 105 | * |
| 106 | * @param[in] src Input tensor. |
| 107 | * @param[in, out] dst Result tensor. |
| 108 | * @param[in] shift A uint32_t value within the range of [0, 15] |
| 109 | */ |
| 110 | static void accumulate_squared(const RawTensor &src, RawTensor &dst, uint32_t shift); |
| 111 | /** Function to accumulate a weighted value from an input tensor to an output tensor. |
| 112 | * |
| 113 | * @param[in] src Input tensor. |
| 114 | * @param[in, out] dst Result tensor. |
| 115 | * @param[in] alpha A float value within the range of [0, 1] |
| 116 | */ |
| 117 | static void accumulate_weighted(const RawTensor &src, RawTensor &dst, float alpha); |
| 118 | /** Arithmetic addition of @p src1 and @p src2 |
| 119 | * |
| 120 | * @param[in] src1 First tensor. |
| 121 | * @param[in] src2 Second tensor. |
| 122 | * @param[out] dst Result tensor. |
| 123 | * @param[in] convert_policy Overflow policy. |
| 124 | */ |
| 125 | static void arithmetic_addition(const RawTensor &src1, const RawTensor &src2, RawTensor &dst, ConvertPolicy convert_policy); |
| 126 | /** Arithmetic subtraction of @p src2 from @p src1 |
| 127 | * |
| 128 | * @param[in] src1 First tensor. |
| 129 | * @param[in] src2 Second tensor. |
| 130 | * @param[out] dst Result tensor. |
| 131 | * @param[in] convert_policy Overflow policy. |
| 132 | */ |
| 133 | static void arithmetic_subtraction(const RawTensor &src1, const RawTensor &src2, RawTensor &dst, ConvertPolicy convert_policy); |
| 134 | /** Function to compute the bitwise and between two tensors. |
| 135 | * |
| 136 | * @param[in] src1 First tensor. |
| 137 | * @param[in] src2 Second tensor. |
| 138 | * @param[out] dst Result tensor. |
| 139 | */ |
| 140 | static void bitwise_and(const RawTensor &src1, const RawTensor &src2, RawTensor &dst); |
| 141 | /** Function to compute the bitwise or between two tensors. |
| 142 | * |
| 143 | * @param[in] src1 First tensor. |
| 144 | * @param[in] src2 Second tensor. |
| 145 | * @param[out] dst Result tensor. |
| 146 | */ |
| 147 | static void bitwise_or(const RawTensor &src1, const RawTensor &src2, RawTensor &dst); |
| 148 | /** Function to compute the bitwise xor between two tensors. |
| 149 | * |
| 150 | * @param[in] src1 First tensor. |
| 151 | * @param[in] src2 Second tensor. |
| 152 | * @param[out] dst Result tensor. |
| 153 | */ |
| 154 | static void bitwise_xor(const RawTensor &src1, const RawTensor &src2, RawTensor &dst); |
| 155 | /** Function to compute the bitwise not of a tensor. |
| 156 | * |
| 157 | * @param[in] src Input tensor. |
| 158 | * @param[out] dst Result tensor. |
| 159 | */ |
| 160 | static void bitwise_not(const RawTensor &src, RawTensor &dst); |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 161 | /** Function to compute box3x3 filtered result tensor. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 162 | * |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 163 | * @param[in] src Input tensor. |
| 164 | * @param[out] dst Result tensor. |
| 165 | * @param[in] border_mode Border mode. |
| 166 | * @param[in] constant_border_value Constant border value if @p border_mode is BorderMode::CONSTANT. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 167 | */ |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 168 | static void box3x3(const RawTensor &src, RawTensor &dst, BorderMode border_mode, uint8_t constant_border_value); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 169 | /** Depth conversion from @p src to @p dst |
| 170 | * |
| 171 | * @param[in] src First tensor. |
| 172 | * @param[out] dst Result tensor. |
| 173 | * @param[in] policy Overflow policy. |
| 174 | * @param[in] shift Value for down/up conversions. |
| 175 | */ |
| 176 | static void depth_convert(const RawTensor &src, RawTensor &dst, ConvertPolicy policy, uint32_t shift); |
SiCong Li | 5a53664 | 2017-06-19 14:47:05 +0100 | [diff] [blame] | 177 | /** Function to compute gaussian3x3 filtered result tensor. |
| 178 | * |
| 179 | * @param[in] src Input tensor. |
| 180 | * @param[out] dst Result tensor. |
| 181 | * @param[in] border_mode Border mode |
| 182 | * @param[in] constant_border_value Constant border value if @p border_mode is BorderMode::CONSTANT |
| 183 | */ |
| 184 | static void gaussian3x3(const RawTensor &src, RawTensor &dst, BorderMode border_mode, uint8_t constant_border_value); |
SiCong Li | 3eb263e | 2017-06-19 15:31:43 +0100 | [diff] [blame] | 185 | /** Function to compute gaussian5x5 filtered result tensor. |
| 186 | * |
| 187 | * @param[in] src Input tensor. |
| 188 | * @param[out] dst Result tensor. |
| 189 | * @param[in] border_mode Border mode |
| 190 | * @param[in] constant_border_value Constant border value if @p border_mode is BorderMode::CONSTANT |
| 191 | */ |
| 192 | static void gaussian5x5(const RawTensor &src, RawTensor &dst, BorderMode border_mode, uint8_t constant_border_value); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 193 | /** Compute GEMM function. |
| 194 | * |
| 195 | * @param[in] src1 First input tensor |
| 196 | * @param[in] src2 Second input tensor |
| 197 | * @param[in] src3 Third input tensor |
| 198 | * @param[out] dst Output tensr |
| 199 | * @param[in] alpha Weight of the matrix product |
| 200 | * @param[in] beta Weight of the third matrix |
| 201 | */ |
| 202 | static void gemm(const RawTensor &src1, const RawTensor &src2, const RawTensor &src3, |
| 203 | RawTensor &dst, float alpha, float beta); |
Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 204 | /** Compute non linear filter function. |
| 205 | * |
| 206 | * @param[in] src First input tensor |
| 207 | * @param[out] dst Output tensor |
| 208 | * @param[in] function Non linear function to perform |
| 209 | * @param[in] mask_size Mask size. Supported sizes: 3, 5 |
| 210 | * @param[in] pattern Matrix pattern |
| 211 | * @param[in] mask The given mask. |
| 212 | * @param[in] border_mode Strategy to use for borders. |
| 213 | * @param[in] constant_border_value (Optional) Constant value to use for borders if border_mode is set to CONSTANT. |
| 214 | */ |
| 215 | static void non_linear_filter(const RawTensor &src, RawTensor &dst, NonLinearFilterFunction function, unsigned int mask_size, |
| 216 | MatrixPattern pattern, const uint8_t *mask, BorderMode border_mode, uint8_t constant_border_value = 0); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 217 | /** Element-wise multiplication of @p src1, @p src2 and @p scale |
| 218 | * |
| 219 | * @param[in] src1 First tensor. |
| 220 | * @param[in] src2 Second tensor. |
| 221 | * @param[out] dst Result tensor. |
| 222 | * @param[in] scale A non-negative float multiplied to each product. |
| 223 | * @param[in] convert_policy Overflow policy. |
| 224 | * @param[in] rounding_policy Rounding policy. |
| 225 | */ |
| 226 | static void pixel_wise_multiplication(const RawTensor &src1, const RawTensor &src2, RawTensor &dst, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy); |
| 227 | /** Fixed-point Pixel-wise multiplication of @p src1 by @p src2 |
| 228 | * |
| 229 | * @param[in] src1 First tensor. |
| 230 | * @param[in] src2 Second tensor. |
| 231 | * @param[out] dst Result tensor. |
| 232 | * @param[in] scale A non-negative float multiplied to each product. |
| 233 | * @param[in] convert_policy Overflow policy. |
| 234 | * @param[in] rounding_policy Rounding policy. |
| 235 | */ |
| 236 | static void fixed_point_pixel_wise_multiplication(const RawTensor &src1, const RawTensor &src2, RawTensor &dst, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy); |
Isabella Gottardi | b797fa2 | 2017-06-23 15:02:11 +0100 | [diff] [blame] | 237 | /** Table Lookup f@p src to @p dst |
| 238 | * |
| 239 | * @param[in] src Input tensor. |
| 240 | * @param[out] dst Result tensor. |
| 241 | * @param[in] lut Input lookup table. |
| 242 | */ |
| 243 | template <typename T> |
| 244 | static void table_lookup(const RawTensor &src, RawTensor &dst, std::map<T, T> &lut); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 245 | /** Threshold of@p src to @p dst |
| 246 | * |
Isabella Gottardi | b797fa2 | 2017-06-23 15:02:11 +0100 | [diff] [blame] | 247 | * @param[in] src Input tensor. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 248 | * @param[out] dst Result tensor. |
| 249 | * @param[in] threshold Threshold. When the threhold type is RANGE, this is used as the lower threshold. |
| 250 | * @param[in] false_value value to set when the condition is not respected. |
| 251 | * @param[in] true_value value to set when the condition is respected. |
| 252 | * @param[in] type Thresholding type. Either RANGE or BINARY. |
| 253 | * @param[in] upper Upper threshold. Only used when the thresholding type is RANGE. |
| 254 | */ |
| 255 | static void threshold(const RawTensor &src, RawTensor &dst, uint8_t threshold, uint8_t false_value, uint8_t true_value, ThresholdType type, uint8_t upper); |
| 256 | /** Activation layer of @p src base on information from @p act_info. |
| 257 | * |
| 258 | * @param[in] input Input tensor. |
| 259 | * @param[in] output Second tensor. |
| 260 | * @param[out] act_info Activation layer information. |
| 261 | */ |
| 262 | static void activation_layer(const RawTensor &input, RawTensor &output, ActivationLayerInfo act_info); |
| 263 | /** Batch Normalization of @p src based on the information from @p norm_info. |
| 264 | * |
| 265 | * @param[in] src Input tensor. |
| 266 | * @param[out] dst Result tensor. |
| 267 | * @param[out] mean Mean vector tensor. |
| 268 | * @param[out] var Var vector tensor. |
| 269 | * @param[out] beta Beta vector tensor. |
| 270 | * @param[out] gamma Gamma vector tensor. |
| 271 | * @param[in] epsilon Small value to avoid division with zero. |
| 272 | * @param[in] fixed_point_position Fixed point position. |
| 273 | */ |
| 274 | static void batch_normalization_layer(const RawTensor &src, RawTensor &dst, const RawTensor &mean, const RawTensor &var, const RawTensor &beta, const RawTensor &gamma, float epsilon, |
| 275 | int fixed_point_position = 0); |
| 276 | /** Convolution layer function |
| 277 | * |
| 278 | * @param[in] src Input tensor. |
| 279 | * @param[in] weights Weights tensor. |
| 280 | * @param[in] bias Bias tensor. |
| 281 | * @param[out] dst Result tensor. |
| 282 | * @param[in] conv_info Pads and strides information for the convolution layer. |
| 283 | */ |
| 284 | static void convolution_layer(const RawTensor &src, const RawTensor &weights, const RawTensor &bias, RawTensor &dst, const PadStrideInfo &conv_info); |
Georgios Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 285 | /** Depth concatenate layer from @p srcs to @p dst |
| 286 | * |
| 287 | * @param[in] srcs Input tensors. |
| 288 | * @param[out] dst Result tensor. |
| 289 | */ |
| 290 | static void depth_concatenate_layer(const std::vector<std::unique_ptr<RawTensor>> &srcs, RawTensor &dst); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 291 | /** Fully connected layer function |
| 292 | * |
| 293 | * @param[in] src Input tensor |
| 294 | * @param[in] weights Weights tensor. |
| 295 | * @param[in] bias Bias tensor. |
| 296 | * @param[out] dst Result tensor. |
| 297 | */ |
| 298 | static void fully_connected_layer(const RawTensor &src, const RawTensor &weights, const RawTensor &bias, RawTensor &dst); |
| 299 | /** Normalization of @p src based on the information from @p norm_info. |
| 300 | * |
| 301 | * @param[in] src Input tensor. |
| 302 | * @param[out] dst Result tensor. |
| 303 | * @param[in] norm_info Normalization Layer information. |
| 304 | */ |
| 305 | static void normalization_layer(const RawTensor &src, RawTensor &dst, NormalizationLayerInfo norm_info); |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 306 | /** Pooling layer of @p src based on the information from @p pool_info. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 307 | * |
| 308 | * @param[in] src Input tensor. |
| 309 | * @param[out] dst Result tensor. |
| 310 | * @param[in] pool_info Pooling Layer information. |
| 311 | * @param[in] fixed_point_position Fixed point position. (Optional) |
| 312 | */ |
| 313 | static void pooling_layer(const RawTensor &src, RawTensor &dst, PoolingLayerInfo pool_info, int fixed_point_position = 0); |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 314 | /** ROI Pooling layer of @p src based on the information from @p pool_info and @p rois. |
| 315 | * |
| 316 | * @param[in] src Input tensor. |
| 317 | * @param[out] dst Result tensor. |
| 318 | * @param[in] rois Region of Interest points. |
| 319 | * @param[in] pool_info ROI Pooling Layer information. |
| 320 | */ |
| 321 | static void roi_pooling_layer(const RawTensor &src, RawTensor &dst, const std::vector<ROI> &rois, const ROIPoolingLayerInfo &pool_info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 322 | /** Fixed point operations of @p src |
| 323 | * |
| 324 | * @param[in] src Input tensor. |
| 325 | * @param[out] dst Result tensor. |
| 326 | * @param[in] op Fixed point operation to perform. |
| 327 | */ |
| 328 | static void fixed_point_operation(const RawTensor &src, RawTensor &dst, FixedPointOp op); |
| 329 | |
| 330 | private: |
| 331 | ReferenceCPP() = delete; |
| 332 | ~ReferenceCPP() = delete; |
| 333 | }; |
| 334 | } // namespace validation |
| 335 | } // namespace test |
| 336 | } // namespace arm_compute |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 337 | #endif /* __ARM_COMPUTE_TEST_REFERENCE_REFERENCE_CPP_H__ */ |