Giorgio Arena | 16def8d | 2021-10-07 11:03:12 +0100 | [diff] [blame] | 1 | /* |
Matthew Bentham | 945b8da | 2023-07-12 11:54:59 +0000 | [diff] [blame] | 2 | * Copyright (c) 2021, 2023 Arm Limited. |
Giorgio Arena | 16def8d | 2021-10-07 11:03:12 +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 | */ |
Jakub Sujak | 8f4b3df | 2023-10-30 16:04:51 +0000 | [diff] [blame] | 24 | |
| 25 | #ifndef ACL_TESTS_VALIDATION_FIXTURES_DIRECTCONVOLUTION3DFIXTURE_H |
| 26 | #define ACL_TESTS_VALIDATION_FIXTURES_DIRECTCONVOLUTION3DFIXTURE_H |
| 27 | |
Giorgio Arena | 16def8d | 2021-10-07 11:03:12 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Jakub Sujak | 8f4b3df | 2023-10-30 16:04:51 +0000 | [diff] [blame] | 29 | #include "tests/framework/Asserts.h" // Required for ARM_COMPUTE_ASSERT |
Giorgio Arena | 16def8d | 2021-10-07 11:03:12 +0100 | [diff] [blame] | 30 | #include "tests/framework/Fixture.h" |
| 31 | #include "tests/validation/reference/ActivationLayer.h" |
| 32 | #include "tests/validation/reference/Conv3D.h" |
| 33 | |
| 34 | #include <random> |
| 35 | |
| 36 | namespace arm_compute |
| 37 | { |
| 38 | namespace test |
| 39 | { |
| 40 | namespace validation |
| 41 | { |
| 42 | using namespace arm_compute::misc::shape_calculator; |
| 43 | |
| 44 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 45 | class DirectConvolution3DValidationGenericFixture : public framework::Fixture |
| 46 | { |
| 47 | public: |
Giorgio Arena | 51847d5 | 2021-10-19 15:45:57 +0100 | [diff] [blame] | 48 | using TBias = typename std::conditional < std::is_same<T, uint8_t>::value || std::is_same<T, int8_t>::value, int32_t, T >::type; |
| 49 | |
Giorgio Arena | 945ae9e | 2021-10-13 11:13:04 +0100 | [diff] [blame] | 50 | void setup(const TensorShape &input_shape, int stride_x, int stride_y, int stride_z, int pad_x, int pad_y, int pad_z, unsigned int kernel_width, int kernel_height, int kernel_depth, |
Giorgio Arena | 51847d5 | 2021-10-19 15:45:57 +0100 | [diff] [blame] | 51 | unsigned int num_kernels, bool has_bias, const ActivationLayerInfo &act_info, const DataType &data_type, const DataLayout &data_layout, |
| 52 | const QuantizationInfo &src_qinfo = QuantizationInfo(), const QuantizationInfo &weights_qinfo = QuantizationInfo(), const QuantizationInfo &dst_qinfo = QuantizationInfo()) |
Giorgio Arena | 16def8d | 2021-10-07 11:03:12 +0100 | [diff] [blame] | 53 | { |
| 54 | ARM_COMPUTE_ERROR_ON(data_layout != DataLayout::NDHWC); |
| 55 | |
Giorgio Arena | 945ae9e | 2021-10-13 11:13:04 +0100 | [diff] [blame] | 56 | const TensorShape weights_shape(num_kernels, input_shape[0], kernel_width, kernel_height, kernel_depth); |
Giorgio Arena | 16def8d | 2021-10-07 11:03:12 +0100 | [diff] [blame] | 57 | const TensorShape bias_shape(num_kernels); |
Giorgio Arena | 51847d5 | 2021-10-19 15:45:57 +0100 | [diff] [blame] | 58 | const DataType bias_data_type = is_data_type_quantized(data_type) ? DataType::S32 : data_type; |
Giorgio Arena | 5c002ec | 2021-10-12 16:00:40 +0100 | [diff] [blame] | 59 | const Conv3dInfo conv3d_info(Size3D(stride_x, stride_y, stride_z), Padding3D(pad_x, pad_y, pad_z), act_info, Size3D(1U, 1U, 1U), DimensionRoundingType::FLOOR, false); |
Giorgio Arena | 16def8d | 2021-10-07 11:03:12 +0100 | [diff] [blame] | 60 | const TensorShape output_shape = compute_conv3d_shape(input_shape, weights_shape, conv3d_info); |
| 61 | |
Giorgio Arena | 51847d5 | 2021-10-19 15:45:57 +0100 | [diff] [blame] | 62 | _target = compute_target(input_shape, weights_shape, bias_shape, output_shape, conv3d_info, has_bias, data_type, bias_data_type, data_layout, src_qinfo, weights_qinfo, dst_qinfo); |
| 63 | _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, conv3d_info, has_bias, data_type, bias_data_type, src_qinfo, weights_qinfo, dst_qinfo); |
Giorgio Arena | 16def8d | 2021-10-07 11:03:12 +0100 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | protected: |
| 67 | template <typename U> |
| 68 | void fill(U &&tensor, int i) |
| 69 | { |
| 70 | switch(tensor.data_type()) |
| 71 | { |
| 72 | case DataType::F16: |
| 73 | { |
| 74 | arm_compute::utils::uniform_real_distribution_16bit<half> distribution{ -1.0f, 1.0f }; |
| 75 | library->fill(tensor, distribution, i); |
| 76 | break; |
| 77 | } |
| 78 | case DataType::F32: |
| 79 | { |
| 80 | std::uniform_real_distribution<float> distribution(-1.0f, 1.0f); |
| 81 | library->fill(tensor, distribution, i); |
| 82 | break; |
| 83 | } |
| 84 | default: |
| 85 | library->fill_tensor_uniform(tensor, i); |
| 86 | } |
| 87 | } |
| 88 | |
Giorgio Arena | 945ae9e | 2021-10-13 11:13:04 +0100 | [diff] [blame] | 89 | TensorType compute_target(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, const Conv3dInfo &conv3d_info, |
Giorgio Arena | 51847d5 | 2021-10-19 15:45:57 +0100 | [diff] [blame] | 90 | bool has_bias, const DataType &data_type, const DataType &bias_data_type, const DataLayout &data_layout, const QuantizationInfo &src_qinfo, |
| 91 | const QuantizationInfo &weights_qinfo, const QuantizationInfo &dst_qinfo) |
Giorgio Arena | 16def8d | 2021-10-07 11:03:12 +0100 | [diff] [blame] | 92 | { |
| 93 | // Create tensors |
Giorgio Arena | 51847d5 | 2021-10-19 15:45:57 +0100 | [diff] [blame] | 94 | TensorType src = create_tensor<TensorType>(input_shape, data_type, 1, src_qinfo, data_layout); |
| 95 | TensorType weights = create_tensor<TensorType>(weights_shape, data_type, 1, weights_qinfo, data_layout); |
| 96 | TensorType bias = has_bias ? create_tensor<TensorType>(bias_shape, bias_data_type, 1, QuantizationInfo()) : TensorType(); |
| 97 | TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1, dst_qinfo, data_layout); |
Giorgio Arena | 16def8d | 2021-10-07 11:03:12 +0100 | [diff] [blame] | 98 | |
Giorgio Arena | 16def8d | 2021-10-07 11:03:12 +0100 | [diff] [blame] | 99 | // Create and configure function |
| 100 | FunctionType conv{}; |
| 101 | conv.configure(&src, &weights, has_bias ? &bias : nullptr, &dst, conv3d_info); |
| 102 | |
| 103 | ARM_COMPUTE_ASSERT(src.info()->is_resizable()); |
| 104 | ARM_COMPUTE_ASSERT(weights.info()->is_resizable()); |
| 105 | ARM_COMPUTE_ASSERT(dst.info()->is_resizable()); |
| 106 | |
| 107 | // Allocate tensors |
| 108 | src.allocator()->allocate(); |
| 109 | weights.allocator()->allocate(); |
| 110 | dst.allocator()->allocate(); |
| 111 | |
| 112 | ARM_COMPUTE_ASSERT(!src.info()->is_resizable()); |
| 113 | ARM_COMPUTE_ASSERT(!weights.info()->is_resizable()); |
| 114 | ARM_COMPUTE_ASSERT(!dst.info()->is_resizable()); |
| 115 | |
| 116 | // Fill tensors |
| 117 | fill(AccessorType(src), 0); |
| 118 | fill(AccessorType(weights), 1); |
| 119 | |
| 120 | if(has_bias) |
| 121 | { |
| 122 | ARM_COMPUTE_ASSERT(bias.info()->is_resizable()); |
| 123 | bias.allocator()->allocate(); |
| 124 | ARM_COMPUTE_ASSERT(!bias.info()->is_resizable()); |
| 125 | fill(AccessorType(bias), 2); |
| 126 | } |
| 127 | |
| 128 | // Compute Direct Convolution 3D function |
| 129 | conv.run(); |
| 130 | |
| 131 | return dst; |
| 132 | } |
| 133 | |
Giorgio Arena | 51847d5 | 2021-10-19 15:45:57 +0100 | [diff] [blame] | 134 | SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, |
| 135 | const Conv3dInfo &conv3d_info, bool has_bias, const DataType &data_type, const DataType &bias_data_type, const QuantizationInfo &src_qinfo, |
| 136 | const QuantizationInfo &weights_qinfo, const QuantizationInfo &dst_qinfo) |
Giorgio Arena | 16def8d | 2021-10-07 11:03:12 +0100 | [diff] [blame] | 137 | { |
| 138 | // Create reference |
Giorgio Arena | 51847d5 | 2021-10-19 15:45:57 +0100 | [diff] [blame] | 139 | SimpleTensor<T> src{ input_shape, data_type, 1, src_qinfo }; |
| 140 | SimpleTensor<T> weights{ weights_shape, data_type, 1, weights_qinfo }; |
| 141 | SimpleTensor<TBias> bias{ bias_shape, bias_data_type }; |
| 142 | SimpleTensor<T> dst{ output_shape, data_type, 1, dst_qinfo }; |
Giorgio Arena | 16def8d | 2021-10-07 11:03:12 +0100 | [diff] [blame] | 143 | |
| 144 | // Fill reference |
| 145 | fill(src, 0); |
| 146 | fill(weights, 1); |
| 147 | |
| 148 | if(has_bias) |
| 149 | { |
| 150 | fill(bias, 2); |
| 151 | } |
| 152 | |
Giorgio Arena | 51847d5 | 2021-10-19 15:45:57 +0100 | [diff] [blame] | 153 | return reference::activation_layer(reference::conv3d<T, TBias>(src, weights, bias, dst, conv3d_info), conv3d_info.act_info); |
Giorgio Arena | 16def8d | 2021-10-07 11:03:12 +0100 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | TensorType _target{}; |
| 157 | SimpleTensor<T> _reference{}; |
| 158 | }; |
| 159 | |
| 160 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 161 | class DirectConvolution3DValidationFixture : public DirectConvolution3DValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 162 | { |
| 163 | public: |
Giorgio Arena | 16def8d | 2021-10-07 11:03:12 +0100 | [diff] [blame] | 164 | void setup(TensorShape input_shape, int stride_x, int stride_y, int stride_z, int pad_x, int pad_y, int pad_z, unsigned int kernel_width, int kernel_height, int kernel_depth, |
| 165 | unsigned int num_kernels, bool has_bias, ActivationLayerInfo act_info, DataType data_type, DataLayout data_layout) |
| 166 | { |
| 167 | DirectConvolution3DValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, stride_x, stride_y, stride_z, pad_x, pad_y, pad_z, kernel_width, kernel_height, |
| 168 | kernel_depth, num_kernels, has_bias, act_info, data_type, data_layout); |
| 169 | } |
| 170 | }; |
Giorgio Arena | 51847d5 | 2021-10-19 15:45:57 +0100 | [diff] [blame] | 171 | |
| 172 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 173 | class DirectConvolution3DValidationQuantizedFixture : public DirectConvolution3DValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 174 | { |
| 175 | public: |
Giorgio Arena | 51847d5 | 2021-10-19 15:45:57 +0100 | [diff] [blame] | 176 | void setup(TensorShape input_shape, int stride_x, int stride_y, int stride_z, int pad_x, int pad_y, int pad_z, unsigned int kernel_width, int kernel_height, int kernel_depth, |
| 177 | unsigned int num_kernels, bool has_bias, ActivationLayerInfo act_info, DataType data_type, DataLayout data_layout, QuantizationInfo src_qinfo, QuantizationInfo weights_qinfo, |
| 178 | QuantizationInfo dst_qinfo) |
| 179 | { |
| 180 | DirectConvolution3DValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, stride_x, stride_y, stride_z, pad_x, pad_y, pad_z, kernel_width, kernel_height, |
| 181 | kernel_depth, num_kernels, has_bias, act_info, data_type, data_layout, src_qinfo, |
| 182 | weights_qinfo, dst_qinfo); |
| 183 | } |
| 184 | }; |
Giorgio Arena | 16def8d | 2021-10-07 11:03:12 +0100 | [diff] [blame] | 185 | } // namespace validation |
| 186 | } // namespace test |
| 187 | } // namespace arm_compute |
Jakub Sujak | 8f4b3df | 2023-10-30 16:04:51 +0000 | [diff] [blame] | 188 | |
| 189 | #endif // ACL_TESTS_VALIDATION_FIXTURES_DIRECTCONVOLUTION3DFIXTURE_H |