blob: 992c2a89d3c3fb15e711bc9a776c12326ae38bd6 [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,
60 "Permutation upto 4-D src tensor is supported");
61 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
80void ClPermuteKernel::configure(const ITensorInfo *src, ITensorInfo *dst, const PermutationVector &perm)
81{
82 configure(CLKernelLibrary::get().get_compile_context(), src, dst, perm);
83}
84
85void ClPermuteKernel::configure(const CLCompileContext &compile_context, const ITensorInfo *src, ITensorInfo *dst, const PermutationVector &perm)
86{
87 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
88 auto padding_info = get_padding_info({ src, dst });
89 const TensorShape dst_shape = get_dst_shape(src, perm);
90 // Output auto inizialitation if not yet initialized
91 auto_init_if_empty(*dst, src->clone()->set_tensor_shape(dst_shape));
92
93 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, perm));
94
95 _perm = perm;
96
97 // Create kernel
98 CLBuildOptions build_opts;
99 build_opts.add_option("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(data_size_from_type(src->data_type())));
100 build_opts.add_option("-DDEPTH_IN=" + support::cpp11::to_string(src->dimension(2)));
101 // New positions of width(W), height(H), channel(C) and batch(D) based on permutation vector
102 build_opts.add_option("-DP1=" + support::cpp11::to_string((_perm.num_dimensions() >= 1) ? perm[0] : 0));
103 build_opts.add_option("-DP2=" + support::cpp11::to_string((_perm.num_dimensions() >= 2) ? perm[1] : 1));
104 build_opts.add_option("-DP3=" + support::cpp11::to_string((_perm.num_dimensions() >= 3) ? perm[2] : 2));
105 build_opts.add_option("-DP4=" + support::cpp11::to_string((_perm.num_dimensions() >= 4) ? perm[3] : 3));
106
107 _kernel = create_kernel(compile_context, "permute", build_opts.options());
108
109 // Configure kernel window
110 Window win = calculate_max_window(*src, Steps());
111
112 // The CLPermute doesn't need padding so update_window_and_padding() can be skipped
113 Coordinates coord;
114 coord.set_num_dimensions(dst->num_dimensions());
115 dst->set_valid_region(ValidRegion(coord, dst->tensor_shape()));
116
117 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
134 const auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
135 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
136
137 Window slice_in = window.first_slice_window_4D().collapse(ICLKernel::window(), 2, 4);
138
139 // Setup dst slice
140 Window slice_out(slice_in);
141 slice_out.set(Window::DimX, Window::Dimension(0, 0, 0));
142 slice_out.set(Window::DimY, Window::Dimension(0, 0, 0));
143 slice_out.set(Window::DimZ, Window::Dimension(0, 0, 0));
144 slice_out.set(3, Window::Dimension(0, 0, 0));
145
146 do
147 {
148 unsigned int idx = 0;
149 add_4D_tensor_argument(idx, src, slice_in);
150 add_4D_tensor_argument(idx, dst, slice_out);
151 enqueue(queue, *this, slice_in, lws_hint());
152 }
153 while(window.slide_window_slice_4D(slice_in) && window.slide_window_slice_4D(slice_out));
154}
155} // namespace kernels
156} // namespace opencl
157} // namespace arm_compute