• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 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 #include "src/core/CL/kernels/CLMaxUnpoolingLayerKernel.h"
25 
26 #include "arm_compute/core/CL/CLHelpers.h"
27 #include "arm_compute/core/CL/CLKernelLibrary.h"
28 #include "arm_compute/core/CL/ICLTensor.h"
29 #include "arm_compute/core/CL/OpenCL.h"
30 #include "arm_compute/core/Helpers.h"
31 #include "arm_compute/core/TensorInfo.h"
32 #include "arm_compute/core/Utils.h"
33 #include "arm_compute/core/utils/misc/ShapeCalculator.h"
34 #include "src/core/AccessWindowStatic.h"
35 #include "src/core/CL/CLValidate.h"
36 #include "src/core/helpers/AutoConfiguration.h"
37 #include "src/core/helpers/WindowHelpers.h"
38 #include "support/StringSupport.h"
39 
40 namespace arm_compute
41 {
42 using namespace misc::shape_calculator;
43 
44 namespace
45 {
validate_arguments(const ITensorInfo * input,const ITensorInfo * output,const PoolingLayerInfo & pool_info,const ITensorInfo * indices)46 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const PoolingLayerInfo &pool_info, const ITensorInfo *indices)
47 {
48     ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output, indices);
49     ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
50     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
51     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(indices, 1, DataType::U32);
52     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, indices);
53 
54     int                 pool_stride_x   = 0;
55     int                 pool_stride_y   = 0;
56     PoolingType         pool_type       = pool_info.pool_type;
57     const PadStrideInfo pad_stride_info = pool_info.pad_stride_info;
58     std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride();
59     const int    pool_size_x = pool_info.pool_size.width;
60     const int    pool_size_y = pool_info.pool_size.height;
61     const Size2D pool_size(pool_size_x, pool_size_y);
62 
63     ARM_COMPUTE_RETURN_ERROR_ON_MSG(pool_type != PoolingType::MAX, "Pooling indices only supported for MAX pooling method");
64     ARM_COMPUTE_RETURN_ERROR_ON_MSG((pool_size != Size2D(2, 2)), "Pooling indices only supported for pool size 2x2");
65     if(output->total_size() != 0)
66     {
67         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
68         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
69     }
70 
71     return Status{};
72 }
73 } // namespace
74 
CLMaxUnpoolingLayerKernel()75 CLMaxUnpoolingLayerKernel::CLMaxUnpoolingLayerKernel()
76     : _input(nullptr), _output(nullptr), _indices(nullptr)
77 {
78 }
79 
configure(const CLCompileContext & compile_context,const ICLTensor * input,const ICLTensor * indices,ICLTensor * output,const PoolingLayerInfo & pool_info)80 void CLMaxUnpoolingLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *indices, ICLTensor *output, const PoolingLayerInfo &pool_info)
81 {
82     ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
83     ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), pool_info, indices->info()));
84 
85     _input   = input;
86     _output  = output;
87     _indices = indices;
88 
89     // Create build options
90     CLBuildOptions build_opts;
91     build_opts.add_option("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(input->info()->element_size()));
92     build_opts.add_option("-DWIDTH_DST=" + support::cpp11::to_string(output->info()->dimension(0)));
93     build_opts.add_option("-DHEIGHT_DST=" + support::cpp11::to_string(output->info()->dimension(1)));
94     build_opts.add_option("-DDEPTH_DST=" + support::cpp11::to_string(output->info()->dimension(2)));
95 
96     const std::string kernel_name("max_unpooling_layer_2");
97 
98     // Create kernel
99     _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
100 
101     const TensorShape output_shape = compute_unpool_shape(*input->info(), pool_info);
102     auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
103 
104     auto window = calculate_max_window(*input->info(), Steps());
105     ICLKernel::configure_internal(window);
106 
107     // Set config_id for enabling LWS tuning
108     _config_id = kernel_name;
109     _config_id += lower_string(string_from_data_type(input->info()->data_type()));
110     _config_id += "_";
111     _config_id += support::cpp11::to_string(output->info()->dimension(0));
112     _config_id += "_";
113     _config_id += support::cpp11::to_string(output->info()->dimension(1));
114     _config_id += "_";
115     _config_id += support::cpp11::to_string(output->info()->dimension(2));
116     _config_id += "_";
117     _config_id += support::cpp11::to_string(output->info()->dimension(3));
118 }
119 
validate(const ITensorInfo * input,const ITensorInfo * indices,const ITensorInfo * output,const PoolingLayerInfo & pool_info)120 Status CLMaxUnpoolingLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *indices, const ITensorInfo *output, const PoolingLayerInfo &pool_info)
121 {
122     ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, indices, output);
123     ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, pool_info, indices));
124     return Status{};
125 }
126 
run(const Window & window,cl::CommandQueue & queue)127 void CLMaxUnpoolingLayerKernel::run(const Window &window, cl::CommandQueue &queue)
128 {
129     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
130     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
131 
132     Window slice = window.first_slice_window_3D();
133 
134     do
135     {
136         unsigned int idx = 0;
137         add_3D_tensor_argument(idx, _input, slice);
138         add_3D_tensor_argument(idx, _output, slice);
139         add_3D_tensor_argument(idx, _indices, slice);
140         enqueue(queue, *this, slice, lws_hint());
141     }
142     while(window.slide_window_slice_3D(slice));
143 }
144 } // namespace arm_compute
145