Georgios Pinitas | ce09314 | 2017-06-19 16:11:53 +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 "CL/CLAccessor.h" |
| 25 | #include "CL/Helper.h" |
| 26 | #include "TypePrinter.h" |
| 27 | #include "arm_compute/runtime/CL/functions/CLPoolingLayer.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 <random> |
| 34 | |
| 35 | using namespace arm_compute; |
| 36 | using namespace arm_compute::test; |
| 37 | using namespace arm_compute::test::cl; |
| 38 | using namespace arm_compute::test::validation; |
| 39 | |
| 40 | namespace |
| 41 | { |
| 42 | const float tolerance_f = 1e-05; /**< Tolerance value for comparing reference's output against implementation's output for float input */ |
| 43 | |
| 44 | /** Compute CL pooling layer function. |
| 45 | * |
| 46 | * @param[in] shape Shape of the input and output tensors. |
| 47 | * @param[in] dt Data type of input and output tensors. |
| 48 | * @param[in] pool_info Pooling Layer information. |
| 49 | * |
| 50 | * @return Computed output tensor. |
| 51 | */ |
| 52 | CLTensor compute_pooling_layer(const TensorShape &shape_in, const TensorShape &shape_out, DataType dt, PoolingLayerInfo pool_info) |
| 53 | { |
| 54 | // Create tensors |
| 55 | CLTensor src = create_tensor(shape_in, dt); |
| 56 | CLTensor dst = create_tensor(shape_out, dt); |
| 57 | |
| 58 | // Create and configure function |
| 59 | CLPoolingLayer pool; |
| 60 | pool.configure(&src, &dst, pool_info); |
| 61 | |
| 62 | // Allocate tensors |
| 63 | src.allocator()->allocate(); |
| 64 | dst.allocator()->allocate(); |
| 65 | |
| 66 | BOOST_TEST(!src.info()->is_resizable()); |
| 67 | BOOST_TEST(!dst.info()->is_resizable()); |
| 68 | |
| 69 | // Fill tensors |
| 70 | std::uniform_real_distribution<> distribution(-1, 1); |
| 71 | library->fill(CLAccessor(src), distribution, 0); |
| 72 | |
| 73 | // Compute function |
| 74 | pool.run(); |
| 75 | |
| 76 | return dst; |
| 77 | } |
| 78 | |
| 79 | TensorShape get_output_shape(TensorShape in_shape, const PoolingLayerInfo &pool_info) |
| 80 | { |
| 81 | TensorShape out_shape(in_shape); |
| 82 | const std::pair<unsigned int, unsigned int> scaled_dims = arm_compute::scaled_dimensions(in_shape.x(), |
| 83 | in_shape.y(), |
| 84 | pool_info.pool_size(), |
| 85 | pool_info.pad_stride_info().stride().first, pool_info.pad_stride_info().stride().second, |
| 86 | pool_info.pad_stride_info().pad().first, pool_info.pad_stride_info().pad().second, |
| 87 | pool_info.pad_stride_info().round()); |
| 88 | out_shape.set(0, scaled_dims.first); |
| 89 | out_shape.set(1, scaled_dims.second); |
| 90 | return out_shape; |
| 91 | } |
| 92 | } // namespace |
| 93 | |
| 94 | #ifndef DOXYGEN_SKIP_THIS |
| 95 | BOOST_AUTO_TEST_SUITE(CL) |
| 96 | BOOST_AUTO_TEST_SUITE(PoolingLayer) |
| 97 | |
| 98 | BOOST_AUTO_TEST_SUITE(Float) |
| 99 | BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit")) |
| 100 | BOOST_DATA_TEST_CASE(RunSmall, 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 }), |
| 101 | src_shape, dt, pool_type, pool_size, pool_stride, pool_pad) |
| 102 | { |
| 103 | PoolingLayerInfo pool_info(pool_type, pool_size, PadStrideInfo(pool_stride, pool_stride, pool_pad, pool_pad, DimensionRoundingType::CEIL)); |
| 104 | TensorShape dst_shape = get_output_shape(src_shape, pool_info); |
| 105 | |
| 106 | // Compute function |
| 107 | CLTensor dst = compute_pooling_layer(src_shape, dst_shape, dt, pool_info); |
| 108 | |
| 109 | // Compute reference |
| 110 | RawTensor ref_dst = Reference::compute_reference_pooling_layer(src_shape, dst_shape, dt, pool_info); |
| 111 | |
| 112 | // Validate output |
| 113 | validate(CLAccessor(dst), ref_dst, tolerance_f); |
| 114 | } |
| 115 | BOOST_AUTO_TEST_SUITE_END() |
| 116 | |
| 117 | BOOST_AUTO_TEST_SUITE_END() |
| 118 | BOOST_AUTO_TEST_SUITE_END() |
| 119 | #endif |