aboutsummaryrefslogtreecommitdiff
path: root/src/core/CL/kernels/CLFillBorderKernel.cpp
blob: 67dac3280eb7b5eb7bda7450f51620115b5dcc69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * Copyright (c) 2016-2020 Arm Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#include "arm_compute/core/CL/kernels/CLFillBorderKernel.h"

#include "arm_compute/core/CL/CLHelpers.h"
#include "arm_compute/core/CL/CLKernelLibrary.h"
#include "arm_compute/core/CL/ICLTensor.h"
#include "arm_compute/core/CL/OpenCL.h"
#include "arm_compute/core/Error.h"
#include "arm_compute/core/TensorInfo.h"
#include "arm_compute/core/Types.h"
#include "arm_compute/core/Utils.h"
#include "arm_compute/core/Validate.h"
#include "arm_compute/core/Window.h"
#include "support/StringSupport.h"

namespace arm_compute
{
CLFillBorderKernel::CLFillBorderKernel()
    : ICLKernel(), _tensor(nullptr)
{
}

bool CLFillBorderKernel::is_parallelisable() const
{
    return false;
}

template <class T>
void CLFillBorderKernel::set_constant_border(unsigned int idx, const PixelValue &constant_border_value)
{
    T value;
    constant_border_value.get(value);
    ICLKernel::add_argument<T>(idx, static_cast<T>(value));
}

void CLFillBorderKernel::configure(ICLTensor *tensor, BorderSize border_size, BorderMode border_mode, const PixelValue &constant_border_value)
{
    configure(CLKernelLibrary::get().get_compile_context(), tensor, border_size, border_mode, constant_border_value);
}

void CLFillBorderKernel::configure(const CLCompileContext &compile_context, ICLTensor *tensor, BorderSize border_size, BorderMode border_mode, const PixelValue &constant_border_value)
{
    ARM_COMPUTE_ERROR_ON(tensor == nullptr);
    ARM_COMPUTE_ERROR_ON(tensor->info()->num_channels() != 1);

    border_size.limit(tensor->info()->padding());

    // If there is no border: early exit
    if(border_size.empty() || border_mode == BorderMode::UNDEFINED)
    {
        return;
    }

    // Select appropriate kernel
    std::string kernel_name = "fill_image_borders_" + lower_string(string_from_border_mode(border_mode));

    const DataType dt = tensor->info()->data_type();

    // Define build options
    CLBuildOptions build_opts;
    build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(dt));
    build_opts.add_option("-DBORDER_SIZE_TOP=" + support::cpp11::to_string(border_size.top));
    build_opts.add_option("-DBORDER_SIZE_BOTTOM=" + support::cpp11::to_string(border_size.bottom));
    build_opts.add_option("-DBORDER_SIZE_LEFT=" + support::cpp11::to_string(border_size.left));
    build_opts.add_option("-DBORDER_SIZE_RIGHT=" + support::cpp11::to_string(border_size.right));

    // Create kernel
    _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
    _tensor = tensor;

    // Create static kernel arguments
    const unsigned int valid_width  = tensor->info()->valid_region().shape[0];
    const unsigned int valid_height = tensor->info()->valid_region().shape[1];
    const cl_int2      valid_region_coords =
    {
        {
            static_cast<cl_int>(tensor->info()->valid_region().anchor[0]),
            static_cast<cl_int>(tensor->info()->valid_region().anchor[1]),
        }
    };
    const unsigned int total_valid_width = border_size.left + valid_width + border_size.right;

    // Set static kernel arguments
    unsigned int idx = num_arguments_per_3D_tensor(); //Skip the tensor parameters
    ICLKernel::add_argument<cl_uint>(idx, valid_width);
    ICLKernel::add_argument<cl_uint>(idx, valid_height);
    ICLKernel::add_argument<cl_int2>(idx, valid_region_coords);
    if(BorderMode::CONSTANT == border_mode)
    {
        switch(dt)
        {
            case DataType::U8:
            case DataType::QASYMM8:
                set_constant_border<uint8_t>(idx, constant_border_value);
                break;
            case DataType::S8:
            case DataType::QASYMM8_SIGNED:
                set_constant_border<int8_t>(idx, constant_border_value);
                break;
            case DataType::U16:
                set_constant_border<uint16_t>(idx, constant_border_value);
                break;
            case DataType::S16:
                set_constant_border<int16_t>(idx, constant_border_value);
                break;
            case DataType::U32:
                set_constant_border<uint32_t>(idx, constant_border_value);
                break;
            case DataType::S32:
                set_constant_border<int32_t>(idx, constant_border_value);
                break;
            case DataType::F32:
                static_assert(sizeof(float) == 4, "Float must be 32 bit");
                set_constant_border<float>(idx, constant_border_value);
                break;
            case DataType::F16:
                static_assert(sizeof(cl_half) == sizeof(half), "Half must be same size as cl_half");
                static_assert(sizeof(cl_half) == 2, "Half must be 16 bit");
                set_constant_border<half>(idx, constant_border_value);
                break;
            default:
                ARM_COMPUTE_ERROR("Not handled");
        }
    }

    // Configure kernel window
    Window win;
    win.set(Window::DimX, Window::Dimension(0, total_valid_width + valid_height));
    win.set(Window::DimY, Window::Dimension(0, 1, 1));
    win.use_tensor_dimensions(tensor->info()->tensor_shape(), Window::DimZ);
    ICLKernel::configure_internal(win);

    // Set config_id for enabling LWS tuning
    _config_id = kernel_name;
    _config_id += "_";
    _config_id += lower_string(string_from_data_type(dt));
    _config_id += "_";
    _config_id += support::cpp11::to_string(tensor->info()->dimension(0));
    _config_id += "_";
    _config_id += support::cpp11::to_string(tensor->info()->dimension(1));
    _config_id += "_";
    _config_id += lower_string(string_from_border_mode(border_mode));
}

void CLFillBorderKernel::run(const Window &window, cl::CommandQueue &queue)
{
    // Border mode undefined or border width == 0
    if(_kernel() == nullptr)
    {
        return;
    }

    ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
    ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window);

    Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
    Window slice     = collapsed.first_slice_window_3D();

    do
    {
        unsigned int idx = 0;
        add_3D_tensor_argument(idx, _tensor, slice);
        enqueue(queue, *this, slice, lws_hint());
    }
    while(collapsed.slide_window_slice_3D(slice));
}
} // namespace arm_compute