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