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 | #include "NEON/Helper.h" |
| 25 | #include "NEON/NEAccessor.h" |
| 26 | #include "TypePrinter.h" |
| 27 | #include "arm_compute/runtime/NEON/functions/NEPoolingLayer.h" |
| 28 | #include "tests/dataset/PoolingLayerDataset.h" |
| 29 | #include "validation/Datasets.h" |
| 30 | #include "validation/Reference.h" |
| 31 | #include "validation/Validation.h" |
| 32 | |
| 33 | #include <iostream> |
| 34 | #include <random> |
| 35 | |
| 36 | using namespace arm_compute; |
| 37 | using namespace arm_compute::test; |
| 38 | using namespace arm_compute::test::neon; |
| 39 | using namespace arm_compute::test::validation; |
| 40 | |
| 41 | namespace |
| 42 | { |
| 43 | const float tolerance_q = 0; /**< Tolerance value for comparing reference's output against implementation's output for quantized input */ |
| 44 | const float tolerance_f = 1e-05; /**< Tolerance value for comparing reference's output against implementation's output for float input */ |
| 45 | |
| 46 | /** Compute Neon pooling layer function. |
| 47 | * |
| 48 | * @param[in] shape Shape of the input and output tensors. |
| 49 | * @param[in] dt Data type of input and output tensors. |
| 50 | * @param[in] pool_info Pooling Layer information. |
| 51 | * |
| 52 | * @return Computed output tensor. |
| 53 | */ |
| 54 | Tensor compute_pooling_layer(const TensorShape &shape_in, const TensorShape &shape_out, DataType dt, PoolingLayerInfo pool_info, int fixed_point_position = 0) |
| 55 | { |
| 56 | // Create tensors |
| 57 | Tensor src = create_tensor(shape_in, dt, 1, fixed_point_position); |
| 58 | Tensor dst = create_tensor(shape_out, dt, 1, fixed_point_position); |
| 59 | |
| 60 | // Create and configure function |
| 61 | NEPoolingLayer pool; |
| 62 | pool.configure(&src, &dst, pool_info); |
| 63 | |
| 64 | // Allocate tensors |
| 65 | src.allocator()->allocate(); |
| 66 | dst.allocator()->allocate(); |
| 67 | |
| 68 | BOOST_TEST(!src.info()->is_resizable()); |
| 69 | BOOST_TEST(!dst.info()->is_resizable()); |
| 70 | |
| 71 | // Fill tensors |
| 72 | int min = 0; |
| 73 | int max = 0; |
| 74 | switch(dt) |
| 75 | { |
| 76 | case DataType::F32: |
| 77 | min = -1; |
| 78 | max = 1; |
| 79 | break; |
| 80 | case DataType::QS8: |
| 81 | min = -(1 << fixed_point_position); |
| 82 | max = (1 << fixed_point_position); |
| 83 | break; |
| 84 | default: |
| 85 | ARM_COMPUTE_ERROR("DataType not supported."); |
| 86 | } |
| 87 | std::uniform_real_distribution<> distribution(min, max); |
| 88 | library->fill(NEAccessor(src), distribution, 0); |
| 89 | |
| 90 | // Compute function |
| 91 | pool.run(); |
| 92 | |
| 93 | return dst; |
| 94 | } |
Michele Di Giorgio | 8af2dd6 | 2017-06-19 15:19:29 +0100 | [diff] [blame] | 95 | |
| 96 | TensorShape get_output_shape(TensorShape in_shape, const PoolingLayerInfo &pool_info) |
| 97 | { |
| 98 | TensorShape out_shape(in_shape); |
| 99 | const std::pair<unsigned int, unsigned int> scaled_dims = arm_compute::scaled_dimensions(in_shape.x(), |
| 100 | in_shape.y(), |
| 101 | pool_info.pool_size(), |
Gian Marco Iodice | 4e28869 | 2017-06-27 11:41:59 +0100 | [diff] [blame] | 102 | pool_info.pool_size(), |
| 103 | pool_info.pad_stride_info()); |
Michele Di Giorgio | 8af2dd6 | 2017-06-19 15:19:29 +0100 | [diff] [blame] | 104 | out_shape.set(0, scaled_dims.first); |
| 105 | out_shape.set(1, scaled_dims.second); |
| 106 | return out_shape; |
| 107 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 108 | } // namespace |
| 109 | |
| 110 | #ifndef DOXYGEN_SKIP_THIS |
| 111 | BOOST_AUTO_TEST_SUITE(NEON) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 112 | BOOST_AUTO_TEST_SUITE(PoolingLayer) |
| 113 | |
| 114 | BOOST_AUTO_TEST_SUITE(Float) |
| 115 | BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit")) |
| 116 | BOOST_DATA_TEST_CASE(RandomDataset, |
| 117 | RandomPoolingLayerDataset() * boost::unit_test::data::make(DataType::F32), |
| 118 | obj, dt) |
| 119 | { |
| 120 | // Compute function |
| 121 | Tensor dst = compute_pooling_layer(obj.src_shape, obj.dst_shape, dt, obj.info); |
| 122 | |
| 123 | // Compute reference |
| 124 | RawTensor ref_dst = Reference::compute_reference_pooling_layer(obj.src_shape, obj.dst_shape, dt, obj.info); |
| 125 | |
| 126 | // Validate output |
| 127 | validate(NEAccessor(dst), ref_dst, tolerance_f, 0); |
| 128 | } |
Michele Di Giorgio | 8af2dd6 | 2017-06-19 15:19:29 +0100 | [diff] [blame] | 129 | |
| 130 | BOOST_DATA_TEST_CASE(RunSmall7x7, |
| 131 | SmallShapes() * CNNFloatDataTypes() * PoolingTypes() * boost::unit_test::data::make({ 2, 3, 7 }) * boost::unit_test::data::make({ 1, 2 }) * boost::unit_test::data::make({ 0, 1 }), |
| 132 | src_shape, dt, pool_type, pool_size, pool_stride, pool_pad) |
| 133 | { |
| 134 | PoolingLayerInfo pool_info(pool_type, pool_size, PadStrideInfo(pool_stride, pool_stride, pool_pad, pool_pad, DimensionRoundingType::CEIL)); |
| 135 | TensorShape dst_shape = get_output_shape(src_shape, pool_info); |
| 136 | |
| 137 | // Compute function |
| 138 | Tensor dst = compute_pooling_layer(src_shape, dst_shape, dt, pool_info); |
| 139 | |
| 140 | // Compute reference |
| 141 | RawTensor ref_dst = Reference::compute_reference_pooling_layer(src_shape, dst_shape, dt, pool_info); |
| 142 | |
| 143 | // Validate output |
| 144 | validate(NEAccessor(dst), ref_dst, tolerance_f, 0); |
| 145 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 146 | BOOST_AUTO_TEST_SUITE_END() |
| 147 | |
| 148 | BOOST_AUTO_TEST_SUITE(Quantized) |
| 149 | BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit")) |
| 150 | BOOST_DATA_TEST_CASE(RandomDataset, |
| 151 | RandomPoolingLayerDataset() * boost::unit_test::data::make(DataType::QS8) * boost::unit_test::data::xrange(1, 5), |
| 152 | obj, dt, fixed_point_position) |
| 153 | { |
| 154 | // Compute function |
| 155 | Tensor dst = compute_pooling_layer(obj.src_shape, obj.dst_shape, dt, obj.info, fixed_point_position); |
| 156 | |
| 157 | // Compute reference |
| 158 | RawTensor ref_dst = Reference::compute_reference_pooling_layer(obj.src_shape, obj.dst_shape, dt, obj.info, fixed_point_position); |
| 159 | |
| 160 | // Validate output |
| 161 | validate(NEAccessor(dst), ref_dst, tolerance_q, 0); |
| 162 | } |
| 163 | BOOST_AUTO_TEST_SUITE_END() |
| 164 | |
| 165 | BOOST_AUTO_TEST_SUITE_END() |
| 166 | BOOST_AUTO_TEST_SUITE_END() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 167 | #endif |