Frank Lei | b59eb0b | 2017-12-27 12:32:14 +0800 | [diff] [blame] | 1 | /* |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Frank Lei | b59eb0b | 2017-12-27 12:32:14 +0800 | [diff] [blame] | 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_GC |
| 25 | #error "This example needs to be built with -DARM_COMPUTE_GC" |
| 26 | #endif /* ARM_COMPUTE_GC */ |
| 27 | |
| 28 | #include "arm_compute/runtime/GLES_COMPUTE/GCFunctions.h" |
| 29 | #include "arm_compute/runtime/GLES_COMPUTE/GCScheduler.h" |
| 30 | #include "half/half.hpp" |
| 31 | #include "utils/Utils.h" |
| 32 | |
| 33 | using namespace arm_compute; |
| 34 | using namespace utils; |
| 35 | |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 36 | class GCDCExample : public Example |
Frank Lei | b59eb0b | 2017-12-27 12:32:14 +0800 | [diff] [blame] | 37 | { |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 38 | public: |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 39 | bool do_setup(int argc, char **argv) override |
Frank Lei | b59eb0b | 2017-12-27 12:32:14 +0800 | [diff] [blame] | 40 | { |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 41 | ARM_COMPUTE_UNUSED(argc); |
| 42 | ARM_COMPUTE_UNUSED(argv); |
Frank Lei | b59eb0b | 2017-12-27 12:32:14 +0800 | [diff] [blame] | 43 | |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 44 | // init instance |
| 45 | GCScheduler::get().default_init(); |
Frank Lei | b59eb0b | 2017-12-27 12:32:14 +0800 | [diff] [blame] | 46 | |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 47 | const TensorShape src_shape = TensorShape{ 11U /* W */, 13U /* H */, 4U /* C */, 3U /* N */ }; |
| 48 | const unsigned int kernel_size = 3; |
| 49 | const int stride_x = 1; |
| 50 | const int stride_y = 1; |
| 51 | const int pad_x = 0; |
| 52 | const int pad_y = 0; |
| 53 | const unsigned int num_kernels = 256; |
| 54 | const DataType data_type = DataType::F16; |
| 55 | |
| 56 | // generate shape |
| 57 | const TensorShape weights_shape(kernel_size, kernel_size, src_shape.z(), num_kernels); |
| 58 | const TensorShape bias_shape(num_kernels); |
| 59 | const PadStrideInfo pad_info(stride_x, stride_y, pad_x, pad_y, DimensionRoundingType::FLOOR); |
| 60 | |
| 61 | // output shape should be 9*11*256*3 (W*H*C*N) |
| 62 | const TensorShape dst_shape = get_output_shape(src_shape, weights_shape, pad_info); |
| 63 | |
| 64 | // create tensors |
| 65 | src.allocator()->init(TensorInfo(src_shape, 1, data_type)); |
| 66 | weights.allocator()->init(TensorInfo(weights_shape, 1, data_type)); |
| 67 | bias.allocator()->init(TensorInfo(bias_shape, 1, data_type)); |
| 68 | dst.allocator()->init(TensorInfo(dst_shape, 1, data_type)); |
| 69 | |
| 70 | // configure layer |
| 71 | conv.configure(&src, &weights, &bias, &dst, pad_info); |
| 72 | |
| 73 | // allocate tensors |
| 74 | src.allocator()->allocate(); |
| 75 | weights.allocator()->allocate(); |
| 76 | bias.allocator()->allocate(); |
| 77 | dst.allocator()->allocate(); |
| 78 | |
| 79 | // To demonstrate how to fill tensor with some values... |
| 80 | src.map(); |
| 81 | Window window; |
| 82 | window.use_tensor_dimensions(src_shape); |
| 83 | Iterator it(&src, window); |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 84 | execute_window_loop(window, [&](const Coordinates &) |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 85 | { |
| 86 | *reinterpret_cast<half_float::half *>(it.ptr()) = half_float::half(1.f); |
| 87 | }); |
| 88 | src.unmap(); |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 89 | |
| 90 | return true; |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 91 | } |
| 92 | void do_run() override |
| 93 | { |
| 94 | // run the layer |
| 95 | conv.run(); |
| 96 | } |
| 97 | void do_teardown() override |
| 98 | { |
| 99 | // check result |
| 100 | dst.map(); |
| 101 | // do something |
| 102 | dst.unmap(); |
| 103 | } |
| 104 | |
| 105 | private: |
| 106 | GCTensor src{}, weights{}, bias{}, dst{}; |
| 107 | GCDirectConvolutionLayer conv{}; |
| 108 | |
| 109 | TensorShape get_output_shape(TensorShape in_shape, TensorShape kernel_shape, const PadStrideInfo &info) |
| 110 | { |
| 111 | TensorShape out_shape(in_shape); |
| 112 | const std::pair<unsigned int, unsigned int> scaled_dims = scaled_dimensions(in_shape.x(), |
| 113 | in_shape.y(), |
| 114 | kernel_shape.x(), |
| 115 | kernel_shape.y(), |
| 116 | info); |
| 117 | out_shape.set(0, scaled_dims.first); |
| 118 | out_shape.set(1, scaled_dims.second); |
| 119 | out_shape.set(2, kernel_shape[3]); |
| 120 | return out_shape; |
| 121 | } |
| 122 | }; |
Frank Lei | b59eb0b | 2017-12-27 12:32:14 +0800 | [diff] [blame] | 123 | |
| 124 | /** Main program for directconvolution test |
| 125 | * |
| 126 | * @param[in] argc Number of arguments |
| 127 | * @param[in] argv Arguments |
| 128 | */ |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 129 | int main(int argc, char **argv) |
Frank Lei | b59eb0b | 2017-12-27 12:32:14 +0800 | [diff] [blame] | 130 | { |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 131 | return utils::run_example<GCDCExample>(argc, argv); |
Frank Lei | b59eb0b | 2017-12-27 12:32:14 +0800 | [diff] [blame] | 132 | } |