1 /* 2 * Copyright (c) 2018-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 #ifndef ARM_COMPUTE_TEST_CAST_FIXTURE 25 #define ARM_COMPUTE_TEST_CAST_FIXTURE 26 27 #include "tests/validation/fixtures/DepthConvertLayerFixture.h" 28 29 namespace arm_compute 30 { 31 namespace test 32 { 33 namespace validation 34 { 35 template <typename TensorType, typename AccessorType, typename FunctionType, typename T1, typename T2> 36 class CastValidationFixture : public framework::Fixture 37 { 38 public: 39 template <typename...> setup(TensorShape shape,DataType dt_in,DataType dt_out,ConvertPolicy policy)40 void setup(TensorShape shape, DataType dt_in, DataType dt_out, ConvertPolicy policy) 41 { 42 _target = compute_target(shape, dt_in, dt_out, policy); 43 _reference = compute_reference(shape, dt_in, dt_out, policy); 44 } 45 46 protected: 47 template <typename U> fill(U && tensor,int i,DataType dt_in,DataType dt_out)48 void fill(U &&tensor, int i, DataType dt_in, DataType dt_out) 49 { 50 // Restricting range to avoid inf values 51 if(dt_out == DataType::F16) 52 { 53 const int signed_min = -32000; 54 const int signed_max = 32000; 55 const int unsigned_min = 0; 56 const int unsigned_max = 65000; 57 58 switch(dt_in) 59 { 60 case DataType::U8: 61 case DataType::QASYMM8: 62 case DataType::QASYMM8_SIGNED: 63 case DataType::S8: 64 case DataType::F32: 65 { 66 library->fill_tensor_uniform(tensor, i); 67 break; 68 } 69 case DataType::U16: 70 { 71 library->fill_tensor_uniform(tensor, i, static_cast<uint16_t>(unsigned_min), static_cast<uint16_t>(unsigned_max)); 72 break; 73 } 74 case DataType::S16: 75 { 76 library->fill_tensor_uniform(tensor, i, static_cast<int16_t>(signed_min), static_cast<int16_t>(signed_max)); 77 break; 78 } 79 case DataType::U32: 80 { 81 library->fill_tensor_uniform(tensor, i, static_cast<uint32_t>(unsigned_min), static_cast<uint32_t>(unsigned_max)); 82 break; 83 } 84 case DataType::S32: 85 { 86 library->fill_tensor_uniform(tensor, i, static_cast<int32_t>(signed_min), static_cast<int32_t>(signed_max)); 87 break; 88 } 89 default: 90 ARM_COMPUTE_ERROR("NOT SUPPORTED!"); 91 } 92 } 93 else 94 { 95 library->fill_tensor_uniform(tensor, i); 96 } 97 } 98 compute_target(const TensorShape & shape,DataType dt_in,DataType dt_out,ConvertPolicy policy)99 TensorType compute_target(const TensorShape &shape, DataType dt_in, DataType dt_out, ConvertPolicy policy) 100 { 101 // Create tensors 102 TensorType src = create_tensor<TensorType>(shape, dt_in, 1); 103 TensorType dst = create_tensor<TensorType>(shape, dt_out, 1); 104 105 // Create and configure function 106 FunctionType cast; 107 cast.configure(&src, &dst, policy); 108 109 ARM_COMPUTE_ASSERT(src.info()->is_resizable()); 110 ARM_COMPUTE_ASSERT(dst.info()->is_resizable()); 111 112 // Allocate tensors 113 src.allocator()->allocate(); 114 dst.allocator()->allocate(); 115 116 ARM_COMPUTE_ASSERT(!src.info()->is_resizable()); 117 ARM_COMPUTE_ASSERT(!dst.info()->is_resizable()); 118 119 // Fill tensors 120 fill(AccessorType(src), 0, dt_in, dt_out); 121 122 // Compute function 123 cast.run(); 124 125 return dst; 126 } 127 compute_reference(const TensorShape & shape,DataType dt_in,DataType dt_out,ConvertPolicy policy)128 SimpleTensor<T2> compute_reference(const TensorShape &shape, DataType dt_in, DataType dt_out, ConvertPolicy policy) 129 { 130 // Create reference 131 SimpleTensor<T1> src{ shape, dt_in, 1 }; 132 133 // Fill reference 134 fill(src, 0, dt_in, dt_out); 135 136 return reference::depth_convert<T1, T2>(src, dt_out, policy, 0); 137 } 138 139 TensorType _target{}; 140 SimpleTensor<T2> _reference{}; 141 }; 142 } // namespace validation 143 } // namespace test 144 } // namespace arm_compute 145 #endif /* ARM_COMPUTE_TEST_CAST_FIXTURE */ 146