blob: 6d09eba87c066f6119aefc0fe2bf1af4eb50d468 [file] [log] [blame]
Frank Leib59eb0b2017-12-27 12:32:14 +08001/*
Michalis Spyrou9d0b5f82019-05-01 13:03:59 +01002 * Copyright (c) 2017-2019 ARM Limited.
Frank Leib59eb0b2017-12-27 12:32:14 +08003 *
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
33using namespace arm_compute;
34using namespace utils;
35
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000036class GCDCExample : public Example
Frank Leib59eb0b2017-12-27 12:32:14 +080037{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000038public:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010039 bool do_setup(int argc, char **argv) override
Frank Leib59eb0b2017-12-27 12:32:14 +080040 {
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000041 ARM_COMPUTE_UNUSED(argc);
42 ARM_COMPUTE_UNUSED(argv);
Frank Leib59eb0b2017-12-27 12:32:14 +080043
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000044 // init instance
45 GCScheduler::get().default_init();
Frank Leib59eb0b2017-12-27 12:32:14 +080046
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000047 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 Spyrou9d0b5f82019-05-01 13:03:59 +010084 execute_window_loop(window, [&](const Coordinates &)
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000085 {
86 *reinterpret_cast<half_float::half *>(it.ptr()) = half_float::half(1.f);
87 });
88 src.unmap();
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010089
90 return true;
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000091 }
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
105private:
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 Leib59eb0b2017-12-27 12:32:14 +0800123
124/** Main program for directconvolution test
125 *
126 * @param[in] argc Number of arguments
127 * @param[in] argv Arguments
128 */
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000129int main(int argc, char **argv)
Frank Leib59eb0b2017-12-27 12:32:14 +0800130{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000131 return utils::run_example<GCDCExample>(argc, argv);
Frank Leib59eb0b2017-12-27 12:32:14 +0800132}