1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2018-2020 Arm Limited.
3*c217d954SCole Faust *
4*c217d954SCole Faust * SPDX-License-Identifier: MIT
5*c217d954SCole Faust *
6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust *
13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust * copies or substantial portions of the Software.
15*c217d954SCole Faust *
16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust * SOFTWARE.
23*c217d954SCole Faust */
24*c217d954SCole Faust #include "SliceOperations.h"
25*c217d954SCole Faust
26*c217d954SCole Faust #include "arm_compute/core/utils/helpers/tensor_transform.h"
27*c217d954SCole Faust #include "arm_compute/core/utils/misc/ShapeCalculator.h"
28*c217d954SCole Faust
29*c217d954SCole Faust namespace arm_compute
30*c217d954SCole Faust {
31*c217d954SCole Faust namespace test
32*c217d954SCole Faust {
33*c217d954SCole Faust namespace validation
34*c217d954SCole Faust {
35*c217d954SCole Faust namespace reference
36*c217d954SCole Faust {
37*c217d954SCole Faust template <typename T>
slice(const SimpleTensor<T> & src,Coordinates starts,Coordinates ends)38*c217d954SCole Faust SimpleTensor<T> slice(const SimpleTensor<T> &src, Coordinates starts, Coordinates ends)
39*c217d954SCole Faust {
40*c217d954SCole Faust using namespace arm_compute::helpers::tensor_transform;
41*c217d954SCole Faust
42*c217d954SCole Faust // Validation checks
43*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(src.shape().num_dimensions() > 4);
44*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(starts.num_dimensions() > src.shape().num_dimensions());
45*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(std::any_of(starts.cbegin(), starts.cbegin() + starts.num_dimensions(), [](int i)
46*c217d954SCole Faust {
47*c217d954SCole Faust return i < 0;
48*c217d954SCole Faust }));
49*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(ends.num_dimensions() > src.shape().num_dimensions());
50*c217d954SCole Faust
51*c217d954SCole Faust // Get source shape
52*c217d954SCole Faust const TensorShape &src_shape = src.shape();
53*c217d954SCole Faust
54*c217d954SCole Faust // Get destination shape
55*c217d954SCole Faust TensorShape dst_shape = arm_compute::misc::shape_calculator::compute_slice_shape(src_shape, starts, ends);
56*c217d954SCole Faust
57*c217d954SCole Faust // Create destination tensor
58*c217d954SCole Faust SimpleTensor<T> dst{ dst_shape, src.data_type(), 1 };
59*c217d954SCole Faust
60*c217d954SCole Faust // Perform slice
61*c217d954SCole Faust Window win;
62*c217d954SCole Faust win.use_tensor_dimensions(dst_shape);
63*c217d954SCole Faust execute_window_loop(win, [&](const Coordinates & id)
64*c217d954SCole Faust {
65*c217d954SCole Faust Coordinates offset;
66*c217d954SCole Faust for(unsigned int i = 0; i < id.num_dimensions(); ++i)
67*c217d954SCole Faust {
68*c217d954SCole Faust offset.set(i, starts[i] + id[i]);
69*c217d954SCole Faust }
70*c217d954SCole Faust *reinterpret_cast<T *>(dst(id)) = *reinterpret_cast<const T *>(src(offset));
71*c217d954SCole Faust });
72*c217d954SCole Faust
73*c217d954SCole Faust return dst;
74*c217d954SCole Faust }
75*c217d954SCole Faust
76*c217d954SCole Faust template SimpleTensor<float> slice(const SimpleTensor<float> &src, Coordinates starts, Coordinates ends);
77*c217d954SCole Faust template SimpleTensor<half_float::half> slice(const SimpleTensor<half_float::half> &src, Coordinates starts, Coordinates ends);
78*c217d954SCole Faust
79*c217d954SCole Faust template <typename T>
strided_slice(const SimpleTensor<T> & src,Coordinates starts,Coordinates ends,BiStrides strides,int32_t begin_mask,int32_t end_mask,int32_t shrink_axis_mask)80*c217d954SCole Faust SimpleTensor<T> strided_slice(const SimpleTensor<T> &src,
81*c217d954SCole Faust Coordinates starts, Coordinates ends, BiStrides strides,
82*c217d954SCole Faust int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask)
83*c217d954SCole Faust {
84*c217d954SCole Faust using namespace arm_compute::helpers::tensor_transform;
85*c217d954SCole Faust
86*c217d954SCole Faust // Validation checks
87*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(src.shape().num_dimensions() > 4);
88*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(starts.num_dimensions() > src.shape().num_dimensions());
89*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(ends.num_dimensions() > src.shape().num_dimensions());
90*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(strides.num_dimensions() > src.shape().num_dimensions());
91*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(std::any_of(strides.cbegin(), strides.cbegin() + strides.num_dimensions(), [](int i)
92*c217d954SCole Faust {
93*c217d954SCole Faust return i == 0;
94*c217d954SCole Faust }));
95*c217d954SCole Faust
96*c217d954SCole Faust // Get source shape
97*c217d954SCole Faust const TensorShape &src_shape = src.shape();
98*c217d954SCole Faust
99*c217d954SCole Faust // Get destination shape
100*c217d954SCole Faust const TensorShape dst_shape = compute_strided_slice_output_shape(src_shape, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask);
101*c217d954SCole Faust
102*c217d954SCole Faust // Create destination tensor
103*c217d954SCole Faust SimpleTensor<T> dst{ dst_shape, src.data_type(), 1 };
104*c217d954SCole Faust
105*c217d954SCole Faust // Get coordinates
106*c217d954SCole Faust Coordinates starts_abs{};
107*c217d954SCole Faust Coordinates ends_abs{};
108*c217d954SCole Faust Coordinates final_strides{};
109*c217d954SCole Faust std::tie(starts_abs, ends_abs, final_strides) = calculate_strided_slice_coords(src_shape,
110*c217d954SCole Faust starts, ends, strides,
111*c217d954SCole Faust begin_mask, end_mask, shrink_axis_mask);
112*c217d954SCole Faust
113*c217d954SCole Faust // Perform strided slice
114*c217d954SCole Faust unsigned int idx = 0;
115*c217d954SCole Faust Window win;
116*c217d954SCole Faust win.use_tensor_dimensions(compute_strided_slice_output_shape(src_shape,
117*c217d954SCole Faust starts, ends, strides,
118*c217d954SCole Faust begin_mask, end_mask, shrink_axis_mask, true));
119*c217d954SCole Faust execute_window_loop(win, [&](const Coordinates & id)
120*c217d954SCole Faust {
121*c217d954SCole Faust Coordinates offset;
122*c217d954SCole Faust for(unsigned int i = 0; i < id.num_dimensions(); ++i)
123*c217d954SCole Faust {
124*c217d954SCole Faust offset.set(i, starts_abs[i] + id[i] * final_strides[i]);
125*c217d954SCole Faust }
126*c217d954SCole Faust dst.data()[idx++] = *reinterpret_cast<const T *>(src(offset));
127*c217d954SCole Faust });
128*c217d954SCole Faust
129*c217d954SCole Faust return dst;
130*c217d954SCole Faust }
131*c217d954SCole Faust
132*c217d954SCole Faust template SimpleTensor<float> strided_slice(const SimpleTensor<float> &src,
133*c217d954SCole Faust Coordinates starts, Coordinates ends, BiStrides strides,
134*c217d954SCole Faust int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask);
135*c217d954SCole Faust template SimpleTensor<half_float::half> strided_slice(const SimpleTensor<half_float::half> &src,
136*c217d954SCole Faust Coordinates starts, Coordinates ends, BiStrides strides,
137*c217d954SCole Faust int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask);
138*c217d954SCole Faust } // namespace reference
139*c217d954SCole Faust } // namespace validation
140*c217d954SCole Faust } // namespace test
141*c217d954SCole Faust } // namespace arm_compute
142