John Richardson | 63e5041 | 2017-10-13 20:51:42 +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 | #ifndef ARM_COMPUTE_TEST_PHASE_FIXTURE |
| 25 | #define ARM_COMPUTE_TEST_PHASE_FIXTURE |
| 26 | |
| 27 | #include "arm_compute/core/TensorShape.h" |
| 28 | #include "arm_compute/core/Types.h" |
| 29 | #include "tests/Globals.h" |
| 30 | #include "tests/IAccessor.h" |
| 31 | #include "tests/framework/Asserts.h" |
| 32 | #include "tests/framework/Fixture.h" |
Georgios Pinitas | 5a7e776 | 2017-12-01 16:27:29 +0000 | [diff] [blame] | 33 | #include "tests/validation/reference/Phase.h" |
John Richardson | 63e5041 | 2017-10-13 20:51:42 +0100 | [diff] [blame] | 34 | |
| 35 | namespace arm_compute |
| 36 | { |
| 37 | namespace test |
| 38 | { |
| 39 | namespace validation |
| 40 | { |
| 41 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 42 | class PhaseValidationFixture : public framework::Fixture |
| 43 | { |
| 44 | public: |
| 45 | template <typename...> |
| 46 | void setup(TensorShape shape, Format format, PhaseType phase_type) |
| 47 | { |
| 48 | _target = compute_target(shape, format, phase_type); |
| 49 | _reference = compute_reference(shape, format, phase_type); |
| 50 | } |
| 51 | |
| 52 | protected: |
| 53 | template <typename U> |
| 54 | void fill(U &&tensor, std::random_device::result_type seed_offset) |
| 55 | { |
| 56 | library->fill_tensor_uniform(tensor, seed_offset); |
| 57 | } |
| 58 | |
| 59 | TensorType compute_target(const TensorShape &shape, Format format, PhaseType phase_type) |
| 60 | { |
| 61 | DataType data_type = data_type_from_format(format); |
| 62 | |
| 63 | // Create tensors |
| 64 | TensorType src1 = create_tensor<TensorType>(shape, data_type); |
| 65 | src1.info()->set_format(format); |
| 66 | |
| 67 | TensorType src2 = create_tensor<TensorType>(shape, data_type); |
| 68 | src2.info()->set_format(format); |
| 69 | |
| 70 | TensorType dst = create_tensor<TensorType>(shape, DataType::U8); |
| 71 | dst.info()->set_format(Format::U8); |
| 72 | |
| 73 | // Create and configure function |
| 74 | FunctionType phase; |
| 75 | |
| 76 | phase.configure(&src1, &src2, &dst, phase_type); |
| 77 | |
| 78 | ARM_COMPUTE_EXPECT(src1.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 79 | ARM_COMPUTE_EXPECT(src2.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 80 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 81 | |
| 82 | // Allocate tensors |
| 83 | src1.allocator()->allocate(); |
| 84 | src2.allocator()->allocate(); |
| 85 | dst.allocator()->allocate(); |
| 86 | |
| 87 | ARM_COMPUTE_EXPECT(!src1.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 88 | ARM_COMPUTE_EXPECT(!src2.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 89 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 90 | |
| 91 | // Fill tensors |
| 92 | fill(AccessorType(src1), 0); |
| 93 | fill(AccessorType(src2), 1); |
| 94 | |
| 95 | // Compute function |
| 96 | phase.run(); |
| 97 | |
| 98 | return dst; |
| 99 | } |
| 100 | |
| 101 | SimpleTensor<uint8_t> compute_reference(const TensorShape &shape, Format format, PhaseType phase_type) |
| 102 | { |
| 103 | DataType data_type = data_type_from_format(format); |
| 104 | |
| 105 | // Create reference |
| 106 | SimpleTensor<T> src1{ shape, data_type }; |
| 107 | SimpleTensor<T> src2{ shape, data_type }; |
| 108 | |
| 109 | // Fill reference |
| 110 | fill(src1, 0); |
| 111 | fill(src2, 1); |
| 112 | |
| 113 | return reference::phase<T>(src1, src2, phase_type); |
| 114 | } |
| 115 | |
| 116 | TensorType _target{}; |
| 117 | SimpleTensor<uint8_t> _reference{}; |
| 118 | }; |
| 119 | } // namespace validation |
| 120 | } // namespace test |
| 121 | } // namespace arm_compute |
| 122 | #endif /* ARM_COMPUTE_TEST_PHASE_FIXTURE */ |