Sanghoon Lee | f47bfb9 | 2018-01-23 15:16:47 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017-2018 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_LOCALLY_CONNECTED_FIXTURE |
| 25 | #define ARM_COMPUTE_TEST_LOCALLY_CONNECTED_FIXTURE |
| 26 | |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/Error.h" |
Sanghoon Lee | f47bfb9 | 2018-01-23 15:16:47 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/TensorShape.h" |
| 29 | #include "arm_compute/core/Types.h" |
| 30 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
| 31 | #include "tests/AssetsLibrary.h" |
| 32 | #include "tests/Globals.h" |
| 33 | #include "tests/IAccessor.h" |
| 34 | #include "tests/framework/Asserts.h" |
| 35 | #include "tests/framework/Fixture.h" |
| 36 | #include "tests/validation/Helpers.h" |
| 37 | #include "tests/validation/reference/LocallyConnected.h" |
| 38 | #include "tests/validation/reference/Utils.h" |
| 39 | |
| 40 | #include <random> |
| 41 | |
| 42 | namespace arm_compute |
| 43 | { |
| 44 | class NELocallyConnected; |
| 45 | |
| 46 | namespace test |
| 47 | { |
| 48 | namespace validation |
| 49 | { |
| 50 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 51 | class LocallyConnectedValidationFixture : public framework::Fixture |
| 52 | { |
| 53 | public: |
| 54 | using TBias = typename std::conditional<std::is_same<typename std::decay<T>::type, uint8_t>::value, int32_t, T>::type; |
| 55 | |
| 56 | public: |
| 57 | template <typename...> |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 58 | void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info, Size2D dilation, DataType data_type) |
Sanghoon Lee | f47bfb9 | 2018-01-23 15:16:47 +0000 | [diff] [blame] | 59 | { |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 60 | ARM_COMPUTE_UNUSED(dilation); |
| 61 | |
Sanghoon Lee | f47bfb9 | 2018-01-23 15:16:47 +0000 | [diff] [blame] | 62 | _data_type = data_type; |
| 63 | _bias_data_type = data_type; |
| 64 | |
| 65 | _target = compute_target(input_shape, weights_shape, bias_shape, output_shape, info); |
| 66 | _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, info); |
| 67 | } |
| 68 | |
| 69 | protected: |
| 70 | template <typename U> |
| 71 | void fill(U &&tensor, int i) |
| 72 | { |
| 73 | std::uniform_real_distribution<> distribution(-1.0f, 1.0f); |
| 74 | library->fill(tensor, distribution, i); |
| 75 | } |
| 76 | |
| 77 | TensorType compute_target(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, const PadStrideInfo &info) |
| 78 | { |
| 79 | TensorShape reshaped_weights_shape(weights_shape); |
| 80 | |
| 81 | // Create tensors |
| 82 | TensorType src = create_tensor<TensorType>(input_shape, _data_type); |
| 83 | TensorType weights = create_tensor<TensorType>(reshaped_weights_shape, _data_type); |
| 84 | TensorType bias = create_tensor<TensorType>(bias_shape, _bias_data_type); |
| 85 | TensorType dst = create_tensor<TensorType>(output_shape, _data_type); |
| 86 | |
| 87 | // Create and configure function |
| 88 | FunctionType locally_connected; |
| 89 | locally_connected.configure(&src, &weights, &bias, &dst, info); |
| 90 | |
| 91 | // Allocate tensors |
| 92 | src.allocator()->allocate(); |
| 93 | weights.allocator()->allocate(); |
| 94 | bias.allocator()->allocate(); |
| 95 | dst.allocator()->allocate(); |
| 96 | |
| 97 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 98 | ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 99 | ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 100 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 101 | |
| 102 | // Fill tensors |
| 103 | fill(AccessorType(src), 0); |
| 104 | fill(AccessorType(weights), 1); |
| 105 | fill(AccessorType(bias), 2); |
| 106 | |
| 107 | locally_connected.run(); |
| 108 | |
| 109 | return dst; |
| 110 | } |
| 111 | |
| 112 | SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, const PadStrideInfo &info) |
| 113 | { |
| 114 | // Create reference |
| 115 | SimpleTensor<T> src(input_shape, _data_type); |
| 116 | SimpleTensor<T> weights(weights_shape, _data_type); |
| 117 | SimpleTensor<TBias> bias(bias_shape, _bias_data_type); |
| 118 | |
| 119 | // Fill reference |
| 120 | fill(src, 0); |
| 121 | fill(weights, 1); |
| 122 | fill(bias, 2); |
| 123 | |
| 124 | return reference::locally_connected<T>(src, weights, bias, output_shape, info); |
| 125 | } |
| 126 | |
| 127 | TensorType _target{}; |
| 128 | SimpleTensor<T> _reference{}; |
| 129 | DataType _data_type{}; |
| 130 | DataType _bias_data_type{}; |
| 131 | }; |
| 132 | |
| 133 | } // namespace validation |
| 134 | } // namespace test |
| 135 | } // namespace arm_compute |
| 136 | #endif /* ARM_COMPUTE_TEST_LOCALLY_CONNECTED_FIXTURE */ |