Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +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_DEPTHWISE_SEPARABLE_CONVOLUTION_LAYER_FIXTURE |
| 25 | #define ARM_COMPUTE_TEST_DEPTHWISE_SEPARABLE_CONVOLUTION_LAYER_FIXTURE |
| 26 | |
| 27 | #include "arm_compute/core/TensorShape.h" |
| 28 | #include "arm_compute/core/Types.h" |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 29 | #include "tests/AssetsLibrary.h" |
| 30 | #include "tests/Globals.h" |
| 31 | #include "tests/IAccessor.h" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 32 | #include "tests/framework/Asserts.h" |
| 33 | #include "tests/framework/Fixture.h" |
| 34 | #include "tests/validation/CPP/DepthwiseSeparableConvolutionLayer.h" |
| 35 | #include "tests/validation/Helpers.h" |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 36 | |
| 37 | #include <random> |
| 38 | |
| 39 | namespace arm_compute |
| 40 | { |
| 41 | namespace test |
| 42 | { |
| 43 | namespace validation |
| 44 | { |
| 45 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 46 | class DepthwiseSeparableConvolutionValidationFixture : public framework::Fixture |
| 47 | { |
| 48 | public: |
| 49 | template <typename...> |
| 50 | void setup(TensorShape in_shape, TensorShape depthwise_weights_shape, TensorShape depthwise_out_shape, TensorShape pointwise_weights_shape, TensorShape biases_shape, TensorShape output_shape, |
| 51 | PadStrideInfo pad_stride_depthwise_info, PadStrideInfo pad_stride_pointwise_info) |
| 52 | { |
| 53 | _target = compute_target(in_shape, depthwise_weights_shape, depthwise_out_shape, pointwise_weights_shape, biases_shape, output_shape, pad_stride_depthwise_info, pad_stride_pointwise_info); |
| 54 | _reference = compute_reference(in_shape, depthwise_weights_shape, depthwise_out_shape, pointwise_weights_shape, biases_shape, output_shape, pad_stride_depthwise_info, pad_stride_pointwise_info); |
| 55 | } |
| 56 | |
| 57 | protected: |
| 58 | template <typename U> |
| 59 | void fill(U &&tensor, int i) |
| 60 | { |
| 61 | switch(tensor.data_type()) |
| 62 | { |
| 63 | case DataType::F32: |
| 64 | { |
| 65 | std::uniform_real_distribution<> distribution(-1.0f, 1.0f); |
| 66 | library->fill(tensor, distribution, i); |
| 67 | break; |
| 68 | } |
| 69 | default: |
| 70 | library->fill_tensor_uniform(tensor, i); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | TensorType compute_target(const TensorShape &input_shape, const TensorShape &depthwise_weights_shape, const TensorShape &depthwise_out_shape, const TensorShape &pointwise_weights_shape, |
| 75 | const TensorShape &biases_shape, |
| 76 | const TensorShape &output_shape, const PadStrideInfo &pad_stride_depthwise_info, const PadStrideInfo &pad_stride_pointwise_info) |
| 77 | { |
| 78 | // Create tensors |
| 79 | TensorType src = create_tensor<TensorType>(input_shape, DataType::F32); |
| 80 | TensorType depthwise_weights = create_tensor<TensorType>(depthwise_weights_shape, DataType::F32); |
| 81 | TensorType depthwise_out = create_tensor<TensorType>(depthwise_out_shape, DataType::F32); |
| 82 | TensorType pointwise_weights = create_tensor<TensorType>(pointwise_weights_shape, DataType::F32); |
| 83 | TensorType biases = create_tensor<TensorType>(biases_shape, DataType::F32); |
| 84 | TensorType dst = create_tensor<TensorType>(output_shape, DataType::F32); |
| 85 | |
| 86 | // Create Depthwise Separable Convolution Layer configure function |
| 87 | CLDepthwiseSeparableConvolutionLayer depthwise_separable_convolution_layer; |
| 88 | depthwise_separable_convolution_layer.configure(&src, &depthwise_weights, &depthwise_out, &pointwise_weights, &biases, &dst, pad_stride_depthwise_info, pad_stride_pointwise_info); |
| 89 | |
| 90 | // Allocate tensors |
| 91 | src.allocator()->allocate(); |
| 92 | depthwise_weights.allocator()->allocate(); |
| 93 | depthwise_out.allocator()->allocate(); |
| 94 | pointwise_weights.allocator()->allocate(); |
| 95 | biases.allocator()->allocate(); |
| 96 | dst.allocator()->allocate(); |
| 97 | |
| 98 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 99 | ARM_COMPUTE_EXPECT(!depthwise_weights.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 100 | ARM_COMPUTE_EXPECT(!depthwise_out.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 101 | ARM_COMPUTE_EXPECT(!pointwise_weights.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 102 | ARM_COMPUTE_EXPECT(!biases.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 103 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 104 | |
| 105 | // Fill tensors |
| 106 | fill(AccessorType(src), 0); |
| 107 | fill(AccessorType(depthwise_weights), 1); |
| 108 | fill(AccessorType(pointwise_weights), 2); |
| 109 | fill(AccessorType(biases), 3); |
| 110 | |
| 111 | // Compute function |
| 112 | depthwise_separable_convolution_layer.run(); |
| 113 | |
| 114 | return dst; |
| 115 | } |
| 116 | |
| 117 | SimpleTensor<T> compute_reference(const TensorShape &in_shape, const TensorShape &depthwise_weights_shape, const TensorShape &depthwise_out_shape, const TensorShape &pointwise_weights_shape, |
| 118 | const TensorShape &biases_shape, const TensorShape &dst_shape, const PadStrideInfo &pad_stride_depthwise_info, const PadStrideInfo &pad_stride_pointwise_info) |
| 119 | { |
| 120 | SimpleTensor<T> src(in_shape, DataType::F32); |
| 121 | SimpleTensor<T> depthwise_weights(depthwise_weights_shape, DataType::F32); |
| 122 | SimpleTensor<T> pointwise_weights(pointwise_weights_shape, DataType::F32); |
| 123 | SimpleTensor<T> biases(biases_shape, DataType::F32); |
| 124 | |
| 125 | fill(src, 0); |
| 126 | fill(depthwise_weights, 1); |
| 127 | fill(pointwise_weights, 2); |
| 128 | fill(biases, 3); |
| 129 | |
| 130 | return reference::depthwise_separable_convolution_layer(src, depthwise_weights, depthwise_out_shape, pointwise_weights, biases, dst_shape, pad_stride_depthwise_info, pad_stride_pointwise_info); |
| 131 | } |
| 132 | |
| 133 | TensorType _target{}; |
| 134 | SimpleTensor<T> _reference{}; |
| 135 | }; |
| 136 | } // namespace validation |
| 137 | } // namespace test |
| 138 | } // namespace arm_compute |
| 139 | #endif /* ARM_COMPUTE_TEST_DEPTHWISE_SEPARABLE_CONVOLUTION_LAYER_FIXTURE */ |