blob: a4755782ed848556e5a8e2673b7d24f1e29e7dea [file] [log] [blame]
Sheri Zhang7e20e292021-02-02 11:49:34 +00001/*
2 * Copyright (c) 2018-2021 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 */
Georgios Pinitas7891a732021-08-20 21:39:25 +010024#include "src/gpu/cl/kernels/ClPermuteKernel.h"
Sheri Zhang7e20e292021-02-02 11:49:34 +000025
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/Helpers.h"
30#include "arm_compute/core/TensorInfo.h"
31#include "arm_compute/core/Utils.h"
Sheri Zhang7e20e292021-02-02 11:49:34 +000032#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010033#include "arm_compute/core/Validate.h"
34
Sheri Zhang7e20e292021-02-02 11:49:34 +000035#include "src/core/CL/CLValidate.h"
36#include "src/core/helpers/AutoConfiguration.h"
37#include "src/core/helpers/WindowHelpers.h"
38#include "support/Cast.h"
39#include "support/StringSupport.h"
40
41namespace arm_compute
42{
43namespace opencl
44{
45namespace kernels
46{
47namespace
48{
49TensorShape get_dst_shape(const ITensorInfo *src, const PermutationVector &perm)
50{
51 TensorShape dst_shape = src->tensor_shape();
52 permute(dst_shape, perm);
53 return dst_shape;
54}
55
56Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst, const PermutationVector &perm)
57{
58 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
59 ARM_COMPUTE_RETURN_ERROR_ON(src->data_type() == DataType::UNKNOWN);
60 ARM_COMPUTE_RETURN_ERROR_ON_MSG(src->num_dimensions() < 1 || src->num_dimensions() > 4,
Teresa Charlin27886092021-02-25 20:15:01 +000061 "Permutation up to 4-D src tensor is supported");
Sheri Zhang7e20e292021-02-02 11:49:34 +000062 ARM_COMPUTE_RETURN_ERROR_ON_MSG(perm.num_dimensions() < 1 || perm.num_dimensions() > 4,
63 "Permutation vector size should be less than or equal to 4");
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010064 for (const auto &p : perm)
Sheri Zhang7e20e292021-02-02 11:49:34 +000065 {
66 ARM_COMPUTE_RETURN_ERROR_ON_MSG(p >= perm.num_dimensions(), "Permutation vector has invalid values");
67 }
68
69 // Validate configured dst
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010070 if (dst->total_size() != 0)
Sheri Zhang7e20e292021-02-02 11:49:34 +000071 {
72 const TensorShape dst_shape = misc::shape_calculator::compute_permutation_output_shape(*src, perm);
73 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(dst->tensor_shape(), dst_shape);
74 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(src, dst);
75 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
76 }
77 return Status{};
78}
79} // namespace
80
Giorgio Arena4a95bba2021-06-28 11:00:27 +010081ClPermuteKernel::ClPermuteKernel()
82{
83 _type = CLKernelType::ELEMENTWISE;
84}
85
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010086void ClPermuteKernel::configure(const CLCompileContext &compile_context,
87 const ITensorInfo *src,
88 ITensorInfo *dst,
89 const PermutationVector &perm)
Sheri Zhang7e20e292021-02-02 11:49:34 +000090{
91 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010092 auto padding_info = get_padding_info({src, dst});
Sheri Zhang7e20e292021-02-02 11:49:34 +000093 const TensorShape dst_shape = get_dst_shape(src, perm);
Teresa Charlin27886092021-02-25 20:15:01 +000094 // Output auto initialization if not yet initialized
Sheri Zhang7e20e292021-02-02 11:49:34 +000095 auto_init_if_empty(*dst, src->clone()->set_tensor_shape(dst_shape));
96
97 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, perm));
98
99 _perm = perm;
100
101 // Create kernel
102 CLBuildOptions build_opts;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100103 build_opts.add_option("-DDATA_TYPE=" +
104 get_cl_unsigned_type_from_element_size(data_size_from_type(src->data_type())));
Sheri Zhang7e20e292021-02-02 11:49:34 +0000105 build_opts.add_option("-DDEPTH_IN=" + support::cpp11::to_string(src->dimension(2)));
106 // New positions of width(W), height(H), channel(C) and batch(D) based on permutation vector
107 build_opts.add_option("-DP1=" + support::cpp11::to_string((_perm.num_dimensions() >= 1) ? perm[0] : 0));
108 build_opts.add_option("-DP2=" + support::cpp11::to_string((_perm.num_dimensions() >= 2) ? perm[1] : 1));
109 build_opts.add_option("-DP3=" + support::cpp11::to_string((_perm.num_dimensions() >= 3) ? perm[2] : 2));
110 build_opts.add_option("-DP4=" + support::cpp11::to_string((_perm.num_dimensions() >= 4) ? perm[3] : 3));
111
112 _kernel = create_kernel(compile_context, "permute", build_opts.options());
113
114 // Configure kernel window
115 Window win = calculate_max_window(*src, Steps());
116
Sheri Zhang7e20e292021-02-02 11:49:34 +0000117 ICLKernel::configure_internal(win);
118 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
119}
120
121Status ClPermuteKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const PermutationVector &perm)
122{
123 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
124 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, perm));
125
126 return Status{};
127}
128
129void ClPermuteKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
130{
131 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
132 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window);
133
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100134 const auto src =
135 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
136 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
Sheri Zhang7e20e292021-02-02 11:49:34 +0000137
138 Window slice_in = window.first_slice_window_4D().collapse(ICLKernel::window(), 2, 4);
139
140 // Setup dst slice
141 Window slice_out(slice_in);
142 slice_out.set(Window::DimX, Window::Dimension(0, 0, 0));
143 slice_out.set(Window::DimY, Window::Dimension(0, 0, 0));
144 slice_out.set(Window::DimZ, Window::Dimension(0, 0, 0));
145 slice_out.set(3, Window::Dimension(0, 0, 0));
146
147 do
148 {
149 unsigned int idx = 0;
150 add_4D_tensor_argument(idx, src, slice_in);
151 add_4D_tensor_argument(idx, dst, slice_out);
152 enqueue(queue, *this, slice_in, lws_hint());
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100153 } while (window.slide_window_slice_4D(slice_in) && window.slide_window_slice_4D(slice_out));
Sheri Zhang7e20e292021-02-02 11:49:34 +0000154}
155} // namespace kernels
156} // namespace opencl
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100157} // namespace arm_compute