Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +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 "arm_compute/core/TensorShape.h" |
| 25 | #include "arm_compute/core/Types.h" |
| 26 | #include "tests/AssetsLibrary.h" |
| 27 | #include "tests/Globals.h" |
| 28 | #include "tests/IAccessor.h" |
| 29 | #include "tests/framework/Asserts.h" |
| 30 | #include "tests/framework/Fixture.h" |
| 31 | #include "tests/validation/CPP/DeconvolutionLayer.h" |
| 32 | #include "tests/validation/Helpers.h" |
| 33 | |
| 34 | #include <random> |
| 35 | |
| 36 | namespace arm_compute |
| 37 | { |
| 38 | namespace test |
| 39 | { |
| 40 | namespace validation |
| 41 | { |
| 42 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 43 | class DeconvolutionLayerFixtureBase : public framework::Fixture |
| 44 | { |
| 45 | public: |
| 46 | /* |
| 47 | * |
| 48 | * @param[in] a The number of zeros added to right and bottom edges of the input. |
| 49 | * @param[in] u How much to scale the X and Y axis. |
| 50 | */ |
| 51 | template <typename...> |
| 52 | void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info, |
| 53 | const std::pair<unsigned int, unsigned int> &a, const std::pair<unsigned int, unsigned int> &u, DataType data_type, int fractional_bits) |
| 54 | { |
| 55 | _fractional_bits = fractional_bits; |
| 56 | _data_type = data_type; |
| 57 | |
| 58 | _target = compute_target(input_shape, weights_shape, bias_shape, output_shape, info, a, u, data_type, fractional_bits); |
| 59 | _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, info, a, data_type, fractional_bits); |
| 60 | } |
| 61 | |
| 62 | protected: |
| 63 | template <typename U> |
| 64 | void fill(U &&tensor, int i) |
| 65 | { |
| 66 | switch(tensor.data_type()) |
| 67 | { |
| 68 | case DataType::F32: |
| 69 | { |
| 70 | std::uniform_real_distribution<> distribution(-1.0f, 1.0f); |
| 71 | library->fill(tensor, distribution, i); |
| 72 | break; |
| 73 | } |
| 74 | default: |
| 75 | library->fill_tensor_uniform(tensor, i); |
| 76 | } |
| 77 | } |
| 78 | /* |
| 79 | * |
| 80 | * @param[in] a The number of zeros added to right and bottom edges of the input. |
| 81 | * @param[in] u How much to scale the X and Y axis. |
| 82 | */ |
| 83 | TensorType compute_target(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, |
| 84 | const PadStrideInfo &info, const std::pair<unsigned int, unsigned int> &a, const std::pair<float, float> &u, DataType data_type, int fixed_point_position) |
| 85 | { |
| 86 | // Create tensors |
| 87 | TensorType src = create_tensor<TensorType>(input_shape, data_type, 1, fixed_point_position); |
| 88 | TensorType weights = create_tensor<TensorType>(weights_shape, data_type, 1, fixed_point_position); |
| 89 | TensorType bias = create_tensor<TensorType>(bias_shape, data_type, 1, fixed_point_position); |
| 90 | TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1, fixed_point_position); |
| 91 | |
| 92 | // Create and configure function |
| 93 | FunctionType conv; |
| 94 | conv.configure(&src, &weights, &bias, &dst, info, a.first, a.second, u.first, u.second); |
| 95 | |
| 96 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 97 | ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 98 | ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 99 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 100 | |
| 101 | // Allocate tensors |
| 102 | src.allocator()->allocate(); |
| 103 | weights.allocator()->allocate(); |
| 104 | bias.allocator()->allocate(); |
| 105 | dst.allocator()->allocate(); |
| 106 | |
| 107 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 108 | ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 109 | ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 110 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 111 | |
| 112 | // Fill tensors |
| 113 | fill(AccessorType(src), 0); |
| 114 | fill(AccessorType(weights), 1); |
| 115 | fill(AccessorType(bias), 2); |
| 116 | |
| 117 | // Compute NEConvolutionLayer function |
| 118 | conv.run(); |
| 119 | |
| 120 | return dst; |
| 121 | } |
| 122 | |
| 123 | SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, |
| 124 | const PadStrideInfo &info, const std::pair<unsigned int, unsigned int> a, DataType data_type, int fixed_point_position) |
| 125 | { |
| 126 | // Create reference |
| 127 | SimpleTensor<T> src{ input_shape, data_type, 1, fixed_point_position }; |
| 128 | SimpleTensor<T> weights{ weights_shape, data_type, 1, fixed_point_position }; |
| 129 | SimpleTensor<T> bias{ bias_shape, data_type, 1, fixed_point_position }; |
| 130 | |
| 131 | // Fill reference |
| 132 | fill(src, 0); |
| 133 | fill(weights, 1); |
| 134 | fill(bias, 2); |
| 135 | |
| 136 | return reference::deconvolution_layer<T>(src, weights, bias, output_shape, info, a); |
| 137 | } |
| 138 | |
| 139 | TensorType _target{}; |
| 140 | SimpleTensor<T> _reference{}; |
| 141 | int _fractional_bits{}; |
| 142 | DataType _data_type{}; |
| 143 | }; |
| 144 | |
| 145 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T, unsigned int kernel_size_x, unsigned int kernel_size_y> |
| 146 | class DeconvolutionValidationFixture : public DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T> |
| 147 | { |
| 148 | public: |
| 149 | template <typename...> |
| 150 | void setup(TensorShape input_shape, unsigned int sx, unsigned int sy, unsigned int padx, unsigned int pady, |
| 151 | unsigned int ax, unsigned int ay, unsigned int ux, unsigned int uy, unsigned int num_kernels, DataType data_type) |
| 152 | { |
| 153 | ARM_COMPUTE_ERROR_ON_MSG(kernel_size_x != kernel_size_y, "Only square kernels supported"); |
| 154 | const TensorShape weights_shape(kernel_size_x, kernel_size_y, input_shape.z(), num_kernels); |
| 155 | const TensorShape bias_shape(num_kernels); |
| 156 | const PadStrideInfo info(sx, sy, padx, pady, DimensionRoundingType::CEIL); |
| 157 | const std::pair<unsigned int, unsigned int> a(ax, ay); |
| 158 | const std::pair<float, float> u(ux, uy); |
| 159 | auto out_dim = deconvolution_output_dimensions(input_shape.x(), input_shape.y(), kernel_size_x, kernel_size_y, padx, pady, a.first, a.second, u.first, u.second, |
| 160 | DimensionRoundingType::CEIL); |
| 161 | TensorShape output_shape = deconvolution_output_shape(out_dim, input_shape, weights_shape); |
| 162 | DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, a, u, data_type, 0); |
| 163 | } |
| 164 | }; |
| 165 | |
| 166 | } // namespace validation |
| 167 | } // namespace test |
| 168 | } // namespace arm_compute |