aboutsummaryrefslogtreecommitdiff
path: root/src/gpu/cl/kernels/ClScatterKernel.cpp
blob: c95e1566795c2f6be463bac1d0ac257abb04e2b4 (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
/*
 * Copyright (c) 2024 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/gpu/cl/kernels/ClScatterKernel.h"

#include "arm_compute/core/CL/ICLTensor.h"
#include "arm_compute/core/ITensorPack.h"
#include "arm_compute/core/TensorInfo.h"
#include "arm_compute/core/Utils.h"

#include "src/common/utils/Log.h"
#include "src/core/helpers/WindowHelpers.h"
#include "support/Cast.h"

namespace arm_compute
{
namespace opencl
{
namespace kernels
{
ClScatterKernel::ClScatterKernel()
{
}

Status ClScatterKernel::validate(const ITensorInfo *updates,
                                 const ITensorInfo *indices,
                                 const ITensorInfo *dst,
                                 const ScatterInfo &info)
{
    ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(updates, dst);
    ARM_COMPUTE_ERROR_ON_DATA_TYPE_NOT_IN(indices, DataType::S32);
    ARM_COMPUTE_ERROR_ON_DATA_TYPE_NOT_IN(dst, DataType::F32);
    ARM_COMPUTE_RETURN_ERROR_ON_MSG(dst->num_dimensions() > 1, "Only 1D output tensors are currently supported.");
    ARM_COMPUTE_RETURN_ERROR_ON_MSG(indices->num_dimensions() > 2, "Only 2D indices tensors are currently supported.");
    ARM_COMPUTE_RETURN_ERROR_ON_MSG(updates->num_dimensions() > 1, "Only 1D update tensors are currently supported.");
    ARM_COMPUTE_RETURN_ERROR_ON_MSG(
        indices->tensor_shape().y() != updates->tensor_shape()[updates->num_dimensions() - 1],
        "Height of indices tensor should match size of highest dimension in updates tensor.");
    ARM_COMPUTE_RETURN_ERROR_ON_MSG(updates->num_dimensions() > dst->num_dimensions(),
                                    "Update tensor cannot have more dims than output tensor.");
    ARM_COMPUTE_UNUSED(info);

    return Status{};
}
void ClScatterKernel::configure(const ClCompileContext &compile_context,
                                const ITensorInfo      *updates,
                                const ITensorInfo      *indices,
                                ITensorInfo            *dst,
                                const ScatterInfo      &info)
{
    ARM_COMPUTE_ERROR_ON_NULLPTR(updates, dst, indices);
    ARM_COMPUTE_LOG_PARAMS(updates, indices, dst, info);

    // Configure kernel window
    const auto indices_shape = indices->tensor_shape();
    Window     win           = calculate_max_window(
                      *indices, Steps(indices_shape.x(), indices_shape.y())); // Ensures single thread for deterministic output.

    // Set build options
    CLBuildOptions build_opts;
    build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(dst->data_type()));
    build_opts.add_option("-DINDICES_DIMS=" + support::cpp11::to_string(indices->num_dimensions()));
    build_opts.add_option("-DINDICES_SHAPE_Y=" + support::cpp11::to_string(indices_shape.y()));
    build_opts.add_option("-DOUT_SHAPE_X=" + support::cpp11::to_string(dst->tensor_shape().x()));

    switch (info.func)
    {
        case ScatterFunction::Update:
            build_opts.add_option("-DSCATTER_FUNCTION=UPDATE_OP");
            break;
        case ScatterFunction::Add:
            build_opts.add_option("-DSCATTER_FUNCTION=ADD_OP");
            break;
        case ScatterFunction::Sub:
            build_opts.add_option("-DSCATTER_FUNCTION=SUB_OP");
            break;
        case ScatterFunction::Max:
            build_opts.add_option("-DSCATTER_FUNCTION=MAX_OP");
            break;
        case ScatterFunction::Min:
            build_opts.add_option("-DSCATTER_FUNCTION=MIN_OP");
            break;
        default:
            ARM_COMPUTE_ERROR("Not implemented");
    }

    // Create kernel
    std::string kernel_name("scatter1D");
    ICLKernel::configure_internal(win);
    _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
    // Set config_id for enabling LWS tuning
    _config_id = kernel_name;
    _config_id += "_";
    _config_id += lower_string(string_from_data_type(updates->data_type()));
    _config_id += "_";
    _config_id += support::cpp11::to_string(dst->dimension(1));
    _config_id += "_";
    _config_id += support::cpp11::to_string(dst->dimension(0));
    _config_id += "_";
    _config_id += support::cpp11::to_string(dst->dimension(2));
    _config_id += "_";
}

void ClScatterKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
{
    unsigned int idx = 0;

    Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);

    const auto updates =
        utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
    const auto indices =
        utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
    auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
    add_4D_tensor_argument(idx, updates, window_collapsed);
    add_4D_tensor_argument(idx, indices, window_collapsed);
    add_4D_tensor_argument(idx, dst, window_collapsed);

    enqueue(queue, *this, window, lws_hint());
}

} // namespace kernels
} // namespace opencl
} // namespace arm_compute