1*c217d954SCole Faust /* 2*c217d954SCole Faust * Copyright (c) 2021 Arm Limited. 3*c217d954SCole Faust * 4*c217d954SCole Faust * SPDX-License-Identifier: MIT 5*c217d954SCole Faust * 6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy 7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to 8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the 9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is 11*c217d954SCole Faust * furnished to do so, subject to the following conditions: 12*c217d954SCole Faust * 13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all 14*c217d954SCole Faust * copies or substantial portions of the Software. 15*c217d954SCole Faust * 16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22*c217d954SCole Faust * SOFTWARE. 23*c217d954SCole Faust */ 24*c217d954SCole Faust #include "arm_compute/core/utils/misc/ShapeCalculator.h" 25*c217d954SCole Faust #include "tests/framework/Fixture.h" 26*c217d954SCole Faust #include "tests/validation/reference/ActivationLayer.h" 27*c217d954SCole Faust #include "tests/validation/reference/Conv3D.h" 28*c217d954SCole Faust 29*c217d954SCole Faust #include <random> 30*c217d954SCole Faust 31*c217d954SCole Faust namespace arm_compute 32*c217d954SCole Faust { 33*c217d954SCole Faust namespace test 34*c217d954SCole Faust { 35*c217d954SCole Faust namespace validation 36*c217d954SCole Faust { 37*c217d954SCole Faust using namespace arm_compute::misc::shape_calculator; 38*c217d954SCole Faust 39*c217d954SCole Faust template <typename TensorType, typename AccessorType, typename FunctionType, typename T> 40*c217d954SCole Faust class DirectConvolution3DValidationGenericFixture : public framework::Fixture 41*c217d954SCole Faust { 42*c217d954SCole Faust public: 43*c217d954SCole Faust using TBias = typename std::conditional < std::is_same<T, uint8_t>::value || std::is_same<T, int8_t>::value, int32_t, T >::type; 44*c217d954SCole Faust 45*c217d954SCole Faust template <typename...> 46*c217d954SCole Faust 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*c217d954SCole Faust unsigned int num_kernels, bool has_bias, const ActivationLayerInfo &act_info, const DataType &data_type, const DataLayout &data_layout, 48*c217d954SCole Faust const QuantizationInfo &src_qinfo = QuantizationInfo(), const QuantizationInfo &weights_qinfo = QuantizationInfo(), const QuantizationInfo &dst_qinfo = QuantizationInfo()) 49*c217d954SCole Faust { 50*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(data_layout != DataLayout::NDHWC); 51*c217d954SCole Faust 52*c217d954SCole Faust const TensorShape weights_shape(num_kernels, input_shape[0], kernel_width, kernel_height, kernel_depth); 53*c217d954SCole Faust const TensorShape bias_shape(num_kernels); 54*c217d954SCole Faust const DataType bias_data_type = is_data_type_quantized(data_type) ? DataType::S32 : data_type; 55*c217d954SCole Faust 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*c217d954SCole Faust const TensorShape output_shape = compute_conv3d_shape(input_shape, weights_shape, conv3d_info); 57*c217d954SCole Faust 58*c217d954SCole Faust _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*c217d954SCole Faust _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*c217d954SCole Faust } 61*c217d954SCole Faust 62*c217d954SCole Faust protected: 63*c217d954SCole Faust template <typename U> fill(U && tensor,int i)64*c217d954SCole Faust void fill(U &&tensor, int i) 65*c217d954SCole Faust { 66*c217d954SCole Faust switch(tensor.data_type()) 67*c217d954SCole Faust { 68*c217d954SCole Faust case DataType::F16: 69*c217d954SCole Faust { 70*c217d954SCole Faust arm_compute::utils::uniform_real_distribution_16bit<half> distribution{ -1.0f, 1.0f }; 71*c217d954SCole Faust library->fill(tensor, distribution, i); 72*c217d954SCole Faust break; 73*c217d954SCole Faust } 74*c217d954SCole Faust case DataType::F32: 75*c217d954SCole Faust { 76*c217d954SCole Faust std::uniform_real_distribution<float> distribution(-1.0f, 1.0f); 77*c217d954SCole Faust library->fill(tensor, distribution, i); 78*c217d954SCole Faust break; 79*c217d954SCole Faust } 80*c217d954SCole Faust default: 81*c217d954SCole Faust library->fill_tensor_uniform(tensor, i); 82*c217d954SCole Faust } 83*c217d954SCole Faust } 84*c217d954SCole Faust 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*c217d954SCole Faust TensorType compute_target(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, const Conv3dInfo &conv3d_info, 86*c217d954SCole Faust bool has_bias, const DataType &data_type, const DataType &bias_data_type, const DataLayout &data_layout, const QuantizationInfo &src_qinfo, 87*c217d954SCole Faust const QuantizationInfo &weights_qinfo, const QuantizationInfo &dst_qinfo) 88*c217d954SCole Faust { 89*c217d954SCole Faust // Create tensors 90*c217d954SCole Faust TensorType src = create_tensor<TensorType>(input_shape, data_type, 1, src_qinfo, data_layout); 91*c217d954SCole Faust TensorType weights = create_tensor<TensorType>(weights_shape, data_type, 1, weights_qinfo, data_layout); 92*c217d954SCole Faust TensorType bias = has_bias ? create_tensor<TensorType>(bias_shape, bias_data_type, 1, QuantizationInfo()) : TensorType(); 93*c217d954SCole Faust TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1, dst_qinfo, data_layout); 94*c217d954SCole Faust 95*c217d954SCole Faust // Create and configure function 96*c217d954SCole Faust FunctionType conv{}; 97*c217d954SCole Faust conv.configure(&src, &weights, has_bias ? &bias : nullptr, &dst, conv3d_info); 98*c217d954SCole Faust 99*c217d954SCole Faust ARM_COMPUTE_ASSERT(src.info()->is_resizable()); 100*c217d954SCole Faust ARM_COMPUTE_ASSERT(weights.info()->is_resizable()); 101*c217d954SCole Faust ARM_COMPUTE_ASSERT(dst.info()->is_resizable()); 102*c217d954SCole Faust 103*c217d954SCole Faust // Allocate tensors 104*c217d954SCole Faust src.allocator()->allocate(); 105*c217d954SCole Faust weights.allocator()->allocate(); 106*c217d954SCole Faust dst.allocator()->allocate(); 107*c217d954SCole Faust 108*c217d954SCole Faust ARM_COMPUTE_ASSERT(!src.info()->is_resizable()); 109*c217d954SCole Faust ARM_COMPUTE_ASSERT(!weights.info()->is_resizable()); 110*c217d954SCole Faust ARM_COMPUTE_ASSERT(!dst.info()->is_resizable()); 111*c217d954SCole Faust 112*c217d954SCole Faust // Fill tensors 113*c217d954SCole Faust fill(AccessorType(src), 0); 114*c217d954SCole Faust fill(AccessorType(weights), 1); 115*c217d954SCole Faust 116*c217d954SCole Faust if(has_bias) 117*c217d954SCole Faust { 118*c217d954SCole Faust ARM_COMPUTE_ASSERT(bias.info()->is_resizable()); 119*c217d954SCole Faust bias.allocator()->allocate(); 120*c217d954SCole Faust ARM_COMPUTE_ASSERT(!bias.info()->is_resizable()); 121*c217d954SCole Faust fill(AccessorType(bias), 2); 122*c217d954SCole Faust } 123*c217d954SCole Faust 124*c217d954SCole Faust // Compute Direct Convolution 3D function 125*c217d954SCole Faust conv.run(); 126*c217d954SCole Faust 127*c217d954SCole Faust return dst; 128*c217d954SCole Faust } 129*c217d954SCole Faust 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*c217d954SCole Faust SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, 131*c217d954SCole Faust const Conv3dInfo &conv3d_info, bool has_bias, const DataType &data_type, const DataType &bias_data_type, const QuantizationInfo &src_qinfo, 132*c217d954SCole Faust const QuantizationInfo &weights_qinfo, const QuantizationInfo &dst_qinfo) 133*c217d954SCole Faust { 134*c217d954SCole Faust // Create reference 135*c217d954SCole Faust SimpleTensor<T> src{ input_shape, data_type, 1, src_qinfo }; 136*c217d954SCole Faust SimpleTensor<T> weights{ weights_shape, data_type, 1, weights_qinfo }; 137*c217d954SCole Faust SimpleTensor<TBias> bias{ bias_shape, bias_data_type }; 138*c217d954SCole Faust SimpleTensor<T> dst{ output_shape, data_type, 1, dst_qinfo }; 139*c217d954SCole Faust 140*c217d954SCole Faust // Fill reference 141*c217d954SCole Faust fill(src, 0); 142*c217d954SCole Faust fill(weights, 1); 143*c217d954SCole Faust 144*c217d954SCole Faust if(has_bias) 145*c217d954SCole Faust { 146*c217d954SCole Faust fill(bias, 2); 147*c217d954SCole Faust } 148*c217d954SCole Faust 149*c217d954SCole Faust return reference::activation_layer(reference::conv3d<T, TBias>(src, weights, bias, dst, conv3d_info), conv3d_info.act_info); 150*c217d954SCole Faust } 151*c217d954SCole Faust 152*c217d954SCole Faust TensorType _target{}; 153*c217d954SCole Faust SimpleTensor<T> _reference{}; 154*c217d954SCole Faust }; 155*c217d954SCole Faust 156*c217d954SCole Faust template <typename TensorType, typename AccessorType, typename FunctionType, typename T> 157*c217d954SCole Faust class DirectConvolution3DValidationFixture : public DirectConvolution3DValidationGenericFixture<TensorType, AccessorType, FunctionType, T> 158*c217d954SCole Faust { 159*c217d954SCole Faust public: 160*c217d954SCole Faust 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*c217d954SCole Faust 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*c217d954SCole Faust unsigned int num_kernels, bool has_bias, ActivationLayerInfo act_info, DataType data_type, DataLayout data_layout) 163*c217d954SCole Faust { 164*c217d954SCole Faust 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*c217d954SCole Faust kernel_depth, num_kernels, has_bias, act_info, data_type, data_layout); 166*c217d954SCole Faust } 167*c217d954SCole Faust }; 168*c217d954SCole Faust 169*c217d954SCole Faust template <typename TensorType, typename AccessorType, typename FunctionType, typename T> 170*c217d954SCole Faust class DirectConvolution3DValidationQuantizedFixture : public DirectConvolution3DValidationGenericFixture<TensorType, AccessorType, FunctionType, T> 171*c217d954SCole Faust { 172*c217d954SCole Faust public: 173*c217d954SCole Faust 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*c217d954SCole Faust 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*c217d954SCole Faust unsigned int num_kernels, bool has_bias, ActivationLayerInfo act_info, DataType data_type, DataLayout data_layout, QuantizationInfo src_qinfo, QuantizationInfo weights_qinfo, 176*c217d954SCole Faust QuantizationInfo dst_qinfo) 177*c217d954SCole Faust { 178*c217d954SCole Faust 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*c217d954SCole Faust kernel_depth, num_kernels, has_bias, act_info, data_type, data_layout, src_qinfo, 180*c217d954SCole Faust weights_qinfo, dst_qinfo); 181*c217d954SCole Faust } 182*c217d954SCole Faust }; 183*c217d954SCole Faust } // namespace validation 184*c217d954SCole Faust } // namespace test 185*c217d954SCole Faust } // namespace arm_compute 186