arm_compute v18.05
[platform/upstream/armcl.git] / examples / gc_dc.cpp
1 /*
2  * Copyright (c) 2017, 2018 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_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
36 class GCDCExample : public Example
37 {
38 public:
39     void do_setup(int argc, char **argv) override
40     {
41         ARM_COMPUTE_UNUSED(argc);
42         ARM_COMPUTE_UNUSED(argv);
43
44         // init instance
45         GCScheduler::get().default_init();
46
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);
84         execute_window_loop(window, [&](const Coordinates & id)
85         {
86             *reinterpret_cast<half_float::half *>(it.ptr()) = half_float::half(1.f);
87         });
88         src.unmap();
89     }
90     void do_run() override
91     {
92         // run the layer
93         conv.run();
94     }
95     void do_teardown() override
96     {
97         // check result
98         dst.map();
99         // do something
100         dst.unmap();
101     }
102
103 private:
104     GCTensor                 src{}, weights{}, bias{}, dst{};
105     GCDirectConvolutionLayer conv{};
106
107     TensorShape get_output_shape(TensorShape in_shape, TensorShape kernel_shape, const PadStrideInfo &info)
108     {
109         TensorShape out_shape(in_shape);
110         const std::pair<unsigned int, unsigned int> scaled_dims = scaled_dimensions(in_shape.x(),
111                                                                                     in_shape.y(),
112                                                                                     kernel_shape.x(),
113                                                                                     kernel_shape.y(),
114                                                                                     info);
115         out_shape.set(0, scaled_dims.first);
116         out_shape.set(1, scaled_dims.second);
117         out_shape.set(2, kernel_shape[3]);
118         return out_shape;
119     }
120 };
121
122 /** Main program for directconvolution test
123  *
124  * @param[in] argc Number of arguments
125  * @param[in] argv Arguments
126  */
127 int main(int argc, char **argv)
128 {
129     return utils::run_example<GCDCExample>(argc, argv);
130 }