Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +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_BENCHMARK_FULLYCONNECTED_LAYER_H__ |
| 25 | #define __ARM_COMPUTE_TEST_BENCHMARK_FULLYCONNECTED_LAYER_H__ |
| 26 | |
| 27 | #include "TensorLibrary.h" |
| 28 | #include "Utils.h" |
| 29 | #include "dataset/ConvolutionLayerDataset.h" |
| 30 | |
| 31 | #include <memory> |
| 32 | #include <string> |
| 33 | |
| 34 | using namespace arm_compute; |
| 35 | using namespace arm_compute::test; |
| 36 | using namespace arm_compute::test::benchmark; |
| 37 | |
| 38 | namespace arm_compute |
| 39 | { |
| 40 | namespace test |
| 41 | { |
| 42 | namespace benchmark |
| 43 | { |
| 44 | template <typename DataSet, typename TensorType, typename Accessor, typename Function, DataType dt = DataType::F32> |
| 45 | class FullyConnectedLayer : public ::benchmark::Fixture |
| 46 | { |
| 47 | public: |
| 48 | void SetUp(::benchmark::State &state) override |
| 49 | { |
| 50 | profiler.add(std::make_shared<WallClockTimer>()); |
| 51 | |
| 52 | const FullyConnectedLayerDataObject fc_obj = *(DataSet().begin() + state.range(0)); |
| 53 | |
| 54 | // Set batched in source and destination shapes |
| 55 | const unsigned int batches = state.range(1); |
| 56 | const unsigned int fixed_point_position = 4; |
| 57 | TensorShape src_shape = fc_obj.src_shape; |
| 58 | TensorShape dst_shape = fc_obj.dst_shape; |
| 59 | src_shape.set(src_shape.num_dimensions(), batches); |
| 60 | dst_shape.set(dst_shape.num_dimensions(), batches); |
| 61 | |
| 62 | // Create tensors |
Moritz Pflanzer | 94450f1 | 2017-06-30 12:48:43 +0100 | [diff] [blame] | 63 | src = create_tensor<TensorType>(src_shape, dt, 1, fixed_point_position); |
| 64 | weights = create_tensor<TensorType>(fc_obj.weights_shape, dt, 1, fixed_point_position); |
| 65 | bias = create_tensor<TensorType>(fc_obj.bias_shape, dt, 1, fixed_point_position); |
| 66 | dst = create_tensor<TensorType>(dst_shape, dt, 1, fixed_point_position); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 67 | |
| 68 | // Create and configure function |
| 69 | fc_layer = std::unique_ptr<Function>(new Function()); |
| 70 | fc_layer->configure(&src, &weights, &bias, &dst); |
| 71 | |
| 72 | // Allocate tensors |
| 73 | src.allocator()->allocate(); |
| 74 | weights.allocator()->allocate(); |
| 75 | bias.allocator()->allocate(); |
| 76 | dst.allocator()->allocate(); |
| 77 | |
| 78 | // Fill tensors |
| 79 | library->fill_tensor_uniform(Accessor(src), 0); |
| 80 | library->fill_tensor_uniform(Accessor(weights), 1); |
| 81 | library->fill_tensor_uniform(Accessor(bias), 2); |
| 82 | } |
| 83 | |
| 84 | void TearDown(::benchmark::State &state) override |
| 85 | { |
| 86 | fc_layer.reset(); |
| 87 | |
| 88 | src.allocator()->free(); |
| 89 | weights.allocator()->free(); |
| 90 | bias.allocator()->free(); |
| 91 | dst.allocator()->free(); |
| 92 | |
| 93 | profiler.submit(state); |
| 94 | } |
| 95 | |
| 96 | std::unique_ptr<Function> fc_layer{ nullptr }; |
| 97 | Profiler profiler{}; |
| 98 | |
| 99 | private: |
| 100 | TensorType src{}; |
| 101 | TensorType weights{}; |
| 102 | TensorType bias{}; |
| 103 | TensorType dst{}; |
| 104 | }; |
| 105 | } // namespace benchmark |
| 106 | } // namespace test |
| 107 | } // namespace arm_compute |
| 108 | #endif //__ARM_COMPUTE_TEST_BENCHMARK_FULLYCONNECTED_LAYER_H__ |