blob: ffc13060a88ae280c68044ef3c63fe608fc67ddd [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 */
24#include "src/core/gpu/cl/kernels/ClPermuteKernel.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/Helpers.h"
30#include "arm_compute/core/TensorInfo.h"
31#include "arm_compute/core/Utils.h"
32#include "arm_compute/core/Validate.h"
33#include "arm_compute/core/utils/misc/ShapeCalculator.h"
34#include "src/core/CL/CLValidate.h"
35#include "src/core/helpers/AutoConfiguration.h"
36#include "src/core/helpers/WindowHelpers.h"
37#include "support/Cast.h"
38#include "support/StringSupport.h"
39
40namespace arm_compute
41{
42namespace opencl
43{
44namespace kernels
45{
46namespace
47{
48TensorShape get_dst_shape(const ITensorInfo *src, const PermutationVector &perm)
49{
50 TensorShape dst_shape = src->tensor_shape();
51 permute(dst_shape, perm);
52 return dst_shape;
53}
54
55Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst, const PermutationVector &perm)
56{
57 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
58 ARM_COMPUTE_RETURN_ERROR_ON(src->data_type() == DataType::UNKNOWN);
59 ARM_COMPUTE_RETURN_ERROR_ON_MSG(src->num_dimensions() < 1 || src->num_dimensions() > 4,
Teresa Charlin27886092021-02-25 20:15:01 +000060 "Permutation up to 4-D src tensor is supported");
Sheri Zhang7e20e292021-02-02 11:49:34 +000061 ARM_COMPUTE_RETURN_ERROR_ON_MSG(perm.num_dimensions() < 1 || perm.num_dimensions() > 4,
62 "Permutation vector size should be less than or equal to 4");
63 for(const auto &p : perm)
64 {
65 ARM_COMPUTE_RETURN_ERROR_ON_MSG(p >= perm.num_dimensions(), "Permutation vector has invalid values");
66 }
67
68 // Validate configured dst
69 if(dst->total_size() != 0)
70 {
71 const TensorShape dst_shape = misc::shape_calculator::compute_permutation_output_shape(*src, perm);
72 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(dst->tensor_shape(), dst_shape);
73 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(src, dst);
74 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
75 }
76 return Status{};
77}
78} // namespace
79
Sheri Zhang7e20e292021-02-02 11:49:34 +000080void ClPermuteKernel::configure(const CLCompileContext &compile_context, const ITensorInfo *src, ITensorInfo *dst, const PermutationVector &perm)
81{
82 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
83 auto padding_info = get_padding_info({ src, dst });
84 const TensorShape dst_shape = get_dst_shape(src, perm);
Teresa Charlin27886092021-02-25 20:15:01 +000085 // Output auto initialization if not yet initialized
Sheri Zhang7e20e292021-02-02 11:49:34 +000086 auto_init_if_empty(*dst, src->clone()->set_tensor_shape(dst_shape));
87
88 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, perm));
89
90 _perm = perm;
91
92 // Create kernel
93 CLBuildOptions build_opts;
94 build_opts.add_option("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(data_size_from_type(src->data_type())));
95 build_opts.add_option("-DDEPTH_IN=" + support::cpp11::to_string(src->dimension(2)));
96 // New positions of width(W), height(H), channel(C) and batch(D) based on permutation vector
97 build_opts.add_option("-DP1=" + support::cpp11::to_string((_perm.num_dimensions() >= 1) ? perm[0] : 0));
98 build_opts.add_option("-DP2=" + support::cpp11::to_string((_perm.num_dimensions() >= 2) ? perm[1] : 1));
99 build_opts.add_option("-DP3=" + support::cpp11::to_string((_perm.num_dimensions() >= 3) ? perm[2] : 2));
100 build_opts.add_option("-DP4=" + support::cpp11::to_string((_perm.num_dimensions() >= 4) ? perm[3] : 3));
101
102 _kernel = create_kernel(compile_context, "permute", build_opts.options());
103
104 // Configure kernel window
105 Window win = calculate_max_window(*src, Steps());
106
Sheri Zhang7e20e292021-02-02 11:49:34 +0000107 ICLKernel::configure_internal(win);
108 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
109}
110
111Status ClPermuteKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const PermutationVector &perm)
112{
113 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
114 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, perm));
115
116 return Status{};
117}
118
119void ClPermuteKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
120{
121 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
122 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window);
123
124 const auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
125 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
126
127 Window slice_in = window.first_slice_window_4D().collapse(ICLKernel::window(), 2, 4);
128
129 // Setup dst slice
130 Window slice_out(slice_in);
131 slice_out.set(Window::DimX, Window::Dimension(0, 0, 0));
132 slice_out.set(Window::DimY, Window::Dimension(0, 0, 0));
133 slice_out.set(Window::DimZ, Window::Dimension(0, 0, 0));
134 slice_out.set(3, Window::Dimension(0, 0, 0));
135
136 do
137 {
138 unsigned int idx = 0;
139 add_4D_tensor_argument(idx, src, slice_in);
140 add_4D_tensor_argument(idx, dst, slice_out);
141 enqueue(queue, *this, slice_in, lws_hint());
142 }
143 while(window.slide_window_slice_4D(slice_in) && window.slide_window_slice_4D(slice_out));
144}
145} // namespace kernels
146} // namespace opencl
147} // namespace arm_compute