blob: 28d122b3cfbedfd8423322e2d97b3ec5b1eff004 [file] [log] [blame]
Pablo Tello54303692018-11-22 16:14:36 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 Arm Limited.
Pablo Tello54303692018-11-22 16:14:36 +00003 *
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 "arm_compute/runtime/CL/functions/CLUnstack.h"
25
26#include "arm_compute/core/CL/ICLTensor.h"
27#include "arm_compute/core/Error.h"
28#include "arm_compute/core/TensorInfo.h"
29#include "arm_compute/core/Types.h"
30#include "arm_compute/core/utils/misc/ShapeCalculator.h"
31
32namespace arm_compute
33{
34namespace
35{
36inline unsigned int wrap_axis(int axis, const ITensorInfo *const tensor)
37{
38 return wrap_around(axis, static_cast<int>(tensor->num_dimensions()));
39}
40
41inline void setup_slice_coordinates_and_mask(Coordinates &slice_start, int32_t &slice_end_mask, const unsigned int input_num_dimensions)
42{
43 // Setups up coordinates to slice the input tensor: start coordinates to all 0s and the unstacking axis of both Start/End to slice just one 2d tensor at a time.
44 Coordinates slice_end;
45 slice_start.set_num_dimensions(input_num_dimensions);
46 slice_end.set_num_dimensions(input_num_dimensions);
47 for(size_t k = 0; k < input_num_dimensions; ++k)
48 {
49 slice_start.set(k, 0);
50 slice_end.set(k, -1);
51 }
52 slice_end_mask = arm_compute::helpers::tensor_transform::construct_slice_end_mask(slice_end);
53}
Pablo Tello54303692018-11-22 16:14:36 +000054} // namespace
55
56CLUnstack::CLUnstack() // NOLINT
57 : _num_slices(0),
58 _strided_slice_vector()
59{
60}
61
62void CLUnstack::configure(const ICLTensor *input, const std::vector<ICLTensor *> &output_vector, int axis)
63{
Manuel Bottini2b84be52020-04-08 10:15:51 +010064 configure(CLKernelLibrary::get().get_compile_context(), input, output_vector, axis);
65}
66
67void CLUnstack::configure(const CLCompileContext &compile_context, const ICLTensor *input, const std::vector<ICLTensor *> &output_vector, int axis)
68{
Georgios Pinitas985b5d52018-12-24 11:24:40 +000069 std::vector<ITensorInfo *> outputs_vector_info(output_vector.size());
70 std::transform(output_vector.begin(), output_vector.end(), outputs_vector_info.begin(), [](ICLTensor * t)
71 {
72 ARM_COMPUTE_ERROR_ON_NULLPTR(t);
73 return t->info();
74 });
75
Pablo Tello54303692018-11-22 16:14:36 +000076 ARM_COMPUTE_ERROR_ON_NULLPTR(input);
Georgios Pinitas985b5d52018-12-24 11:24:40 +000077 ARM_COMPUTE_ERROR_THROW_ON(CLUnstack::validate(input->info(), outputs_vector_info, axis));
78
Pablo Tello54303692018-11-22 16:14:36 +000079 // Wrap around negative values
80 const unsigned int axis_u = wrap_axis(axis, input->info());
Georgios Pinitas985b5d52018-12-24 11:24:40 +000081 _num_slices = std::min(outputs_vector_info.size(), input->info()->dimension(axis_u));
Michalis Spyroubcfd09a2019-05-01 13:03:59 +010082 _strided_slice_vector.resize(_num_slices);
Georgios Pinitas985b5d52018-12-24 11:24:40 +000083
Pablo Tello54303692018-11-22 16:14:36 +000084 Coordinates slice_start;
85 int32_t slice_end_mask;
86 setup_slice_coordinates_and_mask(slice_start, slice_end_mask, input->info()->tensor_shape().num_dimensions());
87 for(unsigned int slice = 0; slice < _num_slices; ++slice)
88 {
89 // Adjusts start and end coordinates to take a 2D slice at a time
90 slice_start.set(axis_u, slice);
Manuel Bottini2b84be52020-04-08 10:15:51 +010091 _strided_slice_vector[slice].configure(compile_context, input, output_vector[slice], slice_start, Coordinates(), BiStrides(), 0, slice_end_mask, (1 << axis_u));
Pablo Tello54303692018-11-22 16:14:36 +000092 }
93}
94
95Status CLUnstack::validate(const ITensorInfo *input, const std::vector<ITensorInfo *> &output_vector, int axis)
96{
97 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input);
98 ARM_COMPUTE_RETURN_ERROR_ON(output_vector.empty());
99 ARM_COMPUTE_RETURN_ERROR_ON(axis < (-static_cast<int>(input->tensor_shape().num_dimensions())));
100 ARM_COMPUTE_RETURN_ERROR_ON(axis >= static_cast<int>(input->tensor_shape().num_dimensions()));
101 const unsigned int num_slices = std::min(output_vector.size(), input->dimension(wrap_axis(axis, input)));
102 ARM_COMPUTE_RETURN_ERROR_ON(num_slices > input->dimension(wrap_axis(axis, input)));
103 ARM_COMPUTE_RETURN_ERROR_ON(num_slices > output_vector.size());
104 Coordinates slice_start;
105 int32_t slice_end_mask;
106 for(size_t k = 0; k < num_slices; ++k)
107 {
108 slice_start.set(wrap_axis(axis, input), k);
109 setup_slice_coordinates_and_mask(slice_start, slice_end_mask, input->tensor_shape().num_dimensions());
110 ARM_COMPUTE_RETURN_ON_ERROR(CLStridedSlice::validate(input, output_vector[k], slice_start, Coordinates(), BiStrides(), 0, slice_end_mask, (1 << wrap_axis(axis, input))));
111 }
112 return Status{};
113}
114
115void CLUnstack::run()
116{
117 for(unsigned i = 0; i < _num_slices; ++i)
118 {
119 _strided_slice_vector[i].run();
120 }
121}
122
123} // namespace arm_compute