Compute Library
 21.02
gc_dc.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2019 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 
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  bool do_setup(int argc, char **argv) override
40  {
41  ARM_COMPUTE_UNUSED(argc);
42  ARM_COMPUTE_UNUSED(argv);
43 
44  // init instance
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;
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 &)
85  {
86  *reinterpret_cast<half_float::half *>(it.ptr()) = half_float::half(1.f);
87  });
88  src.unmap();
89 
90  return true;
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{};
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 };
123 
124 /** Main program for directconvolution test
125  *
126  * @param[in] argc Number of arguments
127  * @param[in] argv Arguments
128  */
129 int main(int argc, char **argv)
130 {
131  return utils::run_example<GCDCExample>(argc, argv);
132 }
Shape of a tensor.
Definition: TensorShape.h:39
half_float::half half
16-bit floating point type
Definition: Types.h:46
Basic function to execute direct convolution function.
Interface for OpenGL ES tensor.
Definition: GCTensor.h:38
void use_tensor_dimensions(const TensorShape &shape, size_t first_dimension=Window::DimX)
Use the tensor&#39;s dimensions to fill the window dimensions.
Definition: Window.inl:276
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2021 Arm Limited.
1 channel, 1 F16 per channel
std::pair< unsigned int, unsigned int > scaled_dimensions(int width, int height, int kernel_width, int kernel_height, const PadStrideInfo &pad_stride_info, const Size2D &dilation=Size2D(1U, 1U))
Returns expected width and height of output scaled tensor depending on dimensions rounding mode...
Definition: Utils.cpp:419
T x() const
Alias to access the size of the first dimension.
Definition: Dimensions.h:87
static GCScheduler & get()
Access the scheduler singleton.
Definition: GCScheduler.cpp:70
const DataType data_type
Definition: Im2Col.cpp:150
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
T z() const
Alias to access the size of the third dimension.
Definition: Dimensions.h:97
Coordinates of an item.
Definition: Coordinates.h:37
Abstract Example class.
Definition: Utils.h:78
Padding and stride information class.
Definition: Types.h:722
int main(int argc, char **argv)
Main program for directconvolution test.
Definition: gc_dc.cpp:129
Includes all the OpenGLES functions at once.
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
Interface to enqueue GLES kernels and get/set the GLES CommandQueue.
Store the tensor&#39;s metadata.
Definition: TensorInfo.h:45
void execute_window_loop(const Window &w, L &&lambda_function, Ts &&... iterators)
Iterate through the passed window, automatically adjusting the iterators and calling the lambda_funct...
Definition: Helpers.inl:77
T y() const
Alias to access the size of the second dimension.
Definition: Dimensions.h:92
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
DataType
Available data types.
Definition: Types.h:77
Describe a multidimensional execution window.
Definition: Window.h:39
void default_init()
Initialises the context and command queue used by the scheduler to default values and sets a default ...
Definition: GCScheduler.cpp:48