aboutsummaryrefslogtreecommitdiff
path: root/src/dynamic_fusion/sketch/gpu/ckw_driver/components/GpuCkwPool2d.cpp
blob: 8ab3ec3a55611f70978b61ed065e2ef255e1c3d5 (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
 * Copyright (c) 2023 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 "src/dynamic_fusion/sketch/gpu/ckw_driver/components/GpuCkwPool2d.h"

#include "arm_compute/core/Error.h"
#include "arm_compute/core/utils/helpers/AdjustVecSize.h"
#include "arm_compute/core/Validate.h"
#include "ckw/TensorTileSampler.h"

#include "src/core/helpers/WindowHelpers.h"
#include "src/dynamic_fusion/sketch/gpu/ckw_driver/components/utils/type_converter/Common.h"
#include "src/dynamic_fusion/sketch/gpu/ckw_driver/components/utils/WriterHelper.h"
#include "src/dynamic_fusion/sketch/gpu/ckw_driver/GpuCkwKernelWriter.h"
#include "src/dynamic_fusion/sketch/gpu/ckw_driver/GpuCkwScopedKernelWriter.h"
#include "src/dynamic_fusion/sketch/gpu/ckw_driver/GpuCkwVariableTable.h"
#include "src/dynamic_fusion/sketch/gpu/GpuKernelArgument.h"
#include "src/dynamic_fusion/sketch/gpu/GpuKernelComponentGroup.h"

using namespace ckw;

namespace arm_compute
{
namespace experimental
{
namespace dynamic_fusion
{
GpuCkwPool2d::GpuCkwPool2d(ComponentId                      id,
                           const ArgumentPack<ITensorInfo> &tensors,
                           const Attributes                &attributes,
                           const Settings                  &settings)
    : IGpuCkwComponentDriver{id, tensors}, _src{}, _dst{}, _attributes{attributes}, _settings{settings}

{
    _src = this->tensors().get_const_tensor(TensorType::ACL_SRC_0);
    _dst = this->tensors().get_const_tensor(TensorType::ACL_DST_0);
    ARM_COMPUTE_ERROR_ON_NULLPTR(_src, _dst);
}

void GpuCkwPool2d::write_component_code(const ComponentGroup    &comp_group,
                                        GpuCkwVariableTable     &vtable,
                                        GpuCkwScopedKernelWriter writer) const
{
    const auto         root_window = comp_group.get_root_component()->ckw_component_driver()->get_window();
    const unsigned int n0          = root_window.x().step();
    const unsigned int m0          = root_window.y().step();

    GpuCkwComponentArgument *src =
        vtable.declare_variable(comp_group, writer, _src, TensorStorageType::ClBufferUint8Ptr, "src");
    GpuCkwComponentArgument *dst =
        vtable.declare_variable(comp_group, writer, _dst, TensorStorageType::ClBufferUint8Ptr, "dst");

    TileOperand &gid_0 = writer->declare_tile("gid_0", ckw::DataType::Int32);
    TileOperand &gid_1 = writer->declare_tile("gid_1", ckw::DataType::Int32);
    TileOperand &gid_2 = writer->declare_tile("gid_2", ckw::DataType::Int32);

    writer->op_get_global_id(gid_0, 0);
    writer->op_get_global_id(gid_1, 1);
    writer->op_get_global_id(gid_2, 2);

    // Data Layout is NHWC
    constexpr int width_idx  = 1;
    constexpr int height_idx = 2;

    const int32_t pool_size_x   = static_cast<int32_t>(_attributes.pool_size().x());
    const int32_t pool_size_y   = static_cast<int32_t>(_attributes.pool_size().y());
    const int32_t pad_x         = static_cast<int32_t>(_attributes.pad().left);
    const int32_t pad_y         = static_cast<int32_t>(_attributes.pad().top);
    const int32_t src_width     = static_cast<int32_t>(_src->dimension(width_idx));
    const int32_t src_height    = static_cast<int32_t>(_src->dimension(height_idx));
    const auto    src_data_type = _src->data_type();

    // Check if this is global pooling path
    const bool is_global_pooling =
        (pool_size_x == src_width) && (pool_size_y == src_height) && (pad_x == 0) && (pad_y == 0);
    // Check if this a case of FP_MIXED_PRECISION
    const bool use_fp_mixed_precision =
        (src_data_type == DataType::F16) && _settings.mixed_precision() && _attributes.pool_type() != PoolingType::MAX;
    const auto acc_data_type = (use_fp_mixed_precision) ? (DataType::F32) : (src_data_type);

    TileOperand       &const_0            = writer->declare_tile("0", 0);
    const TileOperand &const_1            = writer->declare_tile("1", 1);
    const TileOperand &const_lowest_value = writer->declare_tile("LOWEST_VALUE", std::numeric_limits<float>::lowest());
    const TileOperand &pool_size_x_tile   = writer->declare_tile("POOL_SIZE_X", pool_size_x);
    const TileOperand &pool_size_y_tile   = writer->declare_tile("POOL_SIZE_Y", pool_size_y);
    const TileOperand &stride_x_tile = writer->declare_tile("STRIDE_X", static_cast<int32_t>(_attributes.stride().x()));
    const TileOperand &stride_y_tile = writer->declare_tile("STRIDE_Y", static_cast<int32_t>(_attributes.stride().y()));
    const TileOperand &pad_x_tile    = writer->declare_tile("PAD_X", pad_x);
    const TileOperand &pad_y_tile    = writer->declare_tile("PAD_Y", pad_y);
    const TileOperand &dst_height_tile =
        writer->declare_tile("DST_HEIGHT", static_cast<int32_t>(_dst->dimension(height_idx)));
    const TileOperand &src_height_tile = writer->declare_tile("SRC_HEIGHT", src_height);
    const TileOperand &src_width_tile  = writer->declare_tile("SRC_WIDTH", src_width);

    TileOperand &idx_out_n = writer->declare_tile("idx_out_n", ckw::DataType::Int32);
    TileOperand &idx_out_h = writer->declare_tile("idx_out_h", ckw::DataType::Int32);
    TileOperand &idx_out_w = writer->declare_tile("idx_out_w", ckw::DataType::Int32);
    TileOperand &idx_out_c = writer->declare_tile("idx_out_c", ckw::DataType::Int32);

    const int32_t dst_partial_n0_v = _dst->tensor_shape()[0] % n0;

    get_coord(writer, idx_out_c, gid_0, n0, dst_partial_n0_v, "dst_x_", const_0);
    get_coord(writer, idx_out_w, gid_1, 1, 0, "dst_y_", const_0);

    writer->op_binary_expression(idx_out_h, gid_2, BinaryOp::Mod, dst_height_tile); // gid_2 % h
    writer->op_binary_expression(idx_out_n, gid_2, BinaryOp::Div, dst_height_tile); // gid_2 / h

    TensorTileSampler src_sampler;
    src_sampler.width(n0);
    src_sampler.height(m0);
    src_sampler.format(TensorSamplerFormat::C_W_H);
    src_sampler.address_mode_x(TensorSamplerAddressModeX::None);
    src_sampler.address_mode_y(TensorSamplerAddressModeY::None);
    src_sampler.address_mode_z(TensorSamplerAddressModeZ::None);
    src_sampler.x(idx_out_c);
    src_sampler.b(idx_out_n);

    TensorTileSampler dst_sampler;
    dst_sampler.width(n0);
    dst_sampler.height(m0);
    dst_sampler.format(TensorSamplerFormat::C_W_H);
    dst_sampler.address_mode_x(TensorSamplerAddressModeX::OverlappingMin);
    dst_sampler.address_mode_y(TensorSamplerAddressModeY::None);
    dst_sampler.address_mode_z(TensorSamplerAddressModeZ::None);
    dst_sampler.x(idx_out_c);
    dst_sampler.y(idx_out_w);
    dst_sampler.z(idx_out_h);
    dst_sampler.b(idx_out_n);

    // Prepare dst tensor and tile
    TileInfo dst_tile_info = TileInfo(to_ckw(src_data_type), m0, n0);
    if (!dst->has_tile())
    {
        TileOperand &dst_tile = writer->declare_tile("dst_tile", dst_tile_info);
        dst->init_virtual_tensor(dst_tile, dst_sampler);
    }
    const TileOperand &dst_tile = dst->tile();

    // A tile used to temporarily store results or as an accumulator in case of AVG and L2 pooling.
    const TileOperand &res_tile = writer->declare_tile("res_tile", TileInfo(to_ckw(acc_data_type), m0, n0));

    // Initialise result tile with appropriate value
    if (_attributes.pool_type() == PoolingType::MAX)
    {
        if (_settings.use_inf_as_limit())
        {
            TileContainer            minus_inf_tile_container;
            std::vector<std::string> value = std::vector<std::string>(n0, "(-INFINITY)");
            minus_inf_tile_container.push_back({value});
            const TileOperand &minus_inf =
                writer->declare_tile("minus_inf_const", minus_inf_tile_container, to_ckw(acc_data_type));
            writer->op_assign(res_tile, minus_inf);
        }
        else
        {
            writer->op_assign(res_tile, const_lowest_value);
        }
    }
    else
    {
        writer->op_assign(res_tile, const_0);
    }

    // idx_in_w = idx_out_w * STRIDE_X - PAD_X
    TileOperand &idx_in_w = writer->declare_tile("idx_in_w", ckw::DataType::Int32);
    writer->op_binary_expression(idx_in_w, idx_out_w, BinaryOp::Mul, stride_x_tile);
    writer->op_binary_expression(idx_in_w, idx_in_w, BinaryOp::Sub, pad_x_tile);

    // idx_in_h = idx_out_h * STRIDE_Y - PAD_Y
    TileOperand &idx_in_h = writer->declare_tile("idx_in_h", ckw::DataType::Int32);
    writer->op_binary_expression(idx_in_h, idx_out_h, BinaryOp::Mul, stride_y_tile);
    writer->op_binary_expression(idx_in_h, idx_in_h, BinaryOp::Sub, pad_y_tile);

    TileOperand &minus_idx_in_w = writer->declare_tile("minus_idx_in_w", ckw::DataType::Int32);
    TileOperand &minus_idx_in_h = writer->declare_tile("minus_idx_in_h", ckw::DataType::Int32);

    writer->op_unary_expression(minus_idx_in_w, UnaryOp::Negate, idx_in_w);
    writer->op_unary_expression(minus_idx_in_h, UnaryOp::Negate, idx_in_h);

    // Pooling starting/ending offsets for X dim
    TileOperand &pool_x_s = writer->declare_tile("pool_x_s", ckw::DataType::Int32);
    TileOperand &pool_x_e = writer->declare_tile("pool_x_e", ckw::DataType::Int32);

    writer->op_binary_elementwise_function(pool_x_s, BinaryFunction::Max, const_0, minus_idx_in_w);
    writer->op_binary_expression(pool_x_e, src_width_tile, BinaryOp::Add, minus_idx_in_w);
    writer->op_binary_elementwise_function(pool_x_e, BinaryFunction::Min, pool_size_x_tile, pool_x_e);

    // Pooling starting/ending offsets for Y dim
    TileOperand &pool_y_s = writer->declare_tile("pool_y_s", ckw::DataType::Int32);
    TileOperand &pool_y_e = writer->declare_tile("pool_y_e", ckw::DataType::Int32);

    writer->op_binary_elementwise_function(pool_y_s, BinaryFunction::Max, const_0, minus_idx_in_h);
    writer->op_binary_expression(pool_y_e, src_height_tile, BinaryOp::Add, minus_idx_in_h);
    writer->op_binary_elementwise_function(pool_y_e, BinaryFunction::Min, pool_size_y_tile, pool_y_e);

    const TileOperand &filter_size = writer->declare_tile("filter_size", ckw::DataType::Int32);
    if (_attributes.exclude_padding())
    {
        const TileOperand &y_diff = writer->declare_tile("y_diff", ckw::DataType::Int32);
        const TileOperand &x_diff = writer->declare_tile("x_diff", ckw::DataType::Int32);

        writer->op_binary_expression(y_diff, pool_y_e, BinaryOp::Sub, pool_y_s);
        writer->op_binary_expression(x_diff, pool_x_e, BinaryOp::Sub, pool_x_s);

        writer->op_binary_expression(filter_size, y_diff, BinaryOp::Mul, x_diff);
    }
    else
    {
        writer->op_binary_expression(filter_size, pool_size_x_tile, BinaryOp::Mul, pool_size_y_tile);
    }

    const TileOperand &x = writer->declare_tile("x", ckw::DataType::Int32);
    const TileOperand &y = writer->declare_tile("y", ckw::DataType::Int32);

    if (is_global_pooling)
    {
        writer->op_assign(x, const_0);
        writer->op_assign(y, const_0);

        writer->op_assign(pool_y_e, pool_size_y_tile);
        writer->op_assign(pool_x_e, pool_size_x_tile);
    }
    else
    {
        writer->op_assign(x, pool_x_s);
        writer->op_assign(y, pool_y_s);
    }

    // Y dim for-loop
    writer->op_for_loop(
        y, BinaryOp::Less, pool_y_e, y, AssignmentOp::Increment, const_1,
        [&]()
        {
            // Reset the iterator for the inner loop
            if (is_global_pooling)
            {
                writer->op_assign(x, const_0);
            }
            else
            {
                writer->op_assign(x, pool_x_s);
            }

            TileOperand &a_y = writer->declare_tile("a_y", ckw::DataType::Int32);
            writer->op_binary_expression(a_y, idx_in_h, BinaryOp::Add, y);

            // X dim for-loop
            writer->op_for_loop(
                x, BinaryOp::Less, pool_x_e, x, AssignmentOp::Increment, const_1,
                [&]()
                {
                    TileOperand &a_x = writer->declare_tile("a_x", ckw::DataType::Int32);
                    writer->op_binary_expression(a_x, idx_in_w, BinaryOp::Add, x);

                    TileOperand &src_tile = writer->declare_tile("src_tile", TileInfo(to_ckw(acc_data_type), m0, n0));

                    src_sampler.y(a_x);
                    src_sampler.z(a_y);

                    // Load src tile
                    if (use_fp_mixed_precision)
                    {
                        TileOperand &src_uncasted_tile = writer->declare_tile("uncasted_src_tile", dst_tile_info);
                        writer->op_load(src_uncasted_tile, src->tensor(), src_sampler);
                        writer->op_cast_expression(src_tile, src_uncasted_tile, ckw::ConvertPolicy::None);
                    }
                    else
                    {
                        writer->op_load(src_tile, src->tensor(), src_sampler);
                    }

                    // Take the square of the input, for L2 Pooling
                    if (_attributes.pool_type() == PoolingType::L2)
                    {
                        writer->op_binary_expression(src_tile, src_tile, BinaryOp::Mul, src_tile);
                    }

                    // Perfom Pooling op
                    if (_attributes.pool_type() == PoolingType::MAX)
                    {
                        writer->op_binary_elementwise_function(res_tile, BinaryFunction::Max, res_tile, src_tile);
                    }
                    else
                    {
                        writer->op_binary_expression(res_tile, res_tile, BinaryOp::Add, src_tile);
                    }
                });
        });

    if ((_attributes.pool_type() == PoolingType::AVG) || (_attributes.pool_type() == PoolingType::L2))
    {
        // filter_size is automatically broadcasted in the operation
        writer->op_binary_expression(res_tile, res_tile, BinaryOp::Div, filter_size);
    }

    // Take square root of the result in L2 pooling
    if (_attributes.pool_type() == PoolingType::L2)
    {
        writer->op_unary_elementwise_function(res_tile, UnaryFunction::Sqrt, res_tile);
    }

    // Store the results and do casting if FP_MIXED_PRECISION
    if (use_fp_mixed_precision)
    {
        writer->op_cast_expression(dst_tile, res_tile, ckw::ConvertPolicy::None);
    }
    else
    {
        writer->op_assign(dst_tile, res_tile);
    }
}

Window GpuCkwPool2d::get_window() const
{
    ARM_COMPUTE_ERROR_ON_MSG(_dst->tensor_shape().total_size() == 0U, "Destination tensor is not initialized");

    TensorShape        output_shape = _dst->tensor_shape();
    const unsigned int vec_size = adjust_vec_size(((_dst->data_type() == DataType::F32) ? 2 : 4), _dst->dimension(0));
    // Create and configure kernel window
    auto win = calculate_max_window(output_shape, Steps(vec_size));
    win      = win.collapse_if_possible(win, Window::DimZ); // collapse window on batch size.
    return win;
}

std::string GpuCkwPool2d::get_name(const ComponentGroup &comp_group) const
{
    ARM_COMPUTE_UNUSED(comp_group);

    return "pool2dMxN";
}

} // namespace dynamic_fusion
} // namespace experimental
} // namespace arm_compute