blob: 04b5b98453cc49bdfdb1220457cde4fc79996d20 [file] [log] [blame]
Georgios Pinitas77589b52018-08-21 14:41:35 +01001/*
2 * Copyright (c) 2018 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 Pinitasc1a72452018-08-24 11:25:32 +010024#include "SliceOperations.h"
Georgios Pinitas77589b52018-08-21 14:41:35 +010025
26#include "arm_compute/core/utils/helpers/tensor_transform.h"
27
28namespace arm_compute
29{
30namespace test
31{
32namespace validation
33{
34namespace reference
35{
36template <typename T>
Georgios Pinitasc1a72452018-08-24 11:25:32 +010037SimpleTensor<T> slice(const SimpleTensor<T> &src, Coordinates starts, Coordinates ends)
38{
39 using namespace arm_compute::helpers::tensor_transform;
40
41 // Validation checks
42 ARM_COMPUTE_ERROR_ON(src.shape().num_dimensions() > 4);
43 ARM_COMPUTE_ERROR_ON(starts.num_dimensions() > src.shape().num_dimensions());
44 ARM_COMPUTE_ERROR_ON(std::any_of(starts.cbegin(), starts.cbegin() + starts.num_dimensions(), [](int i)
45 {
46 return i < 0;
47 }));
48 ARM_COMPUTE_ERROR_ON(ends.num_dimensions() > src.shape().num_dimensions());
49
50 // Get source shape
51 const TensorShape &src_shape = src.shape();
52
53 // Get actual end
54 Coordinates ends_abs = slice_absolute_end_coords(src_shape, ends);
55
56 // Get destination shape
57 TensorShape dst_shape = compute_slice_output_shape(src_shape, starts, ends_abs);
58
59 // Create destination tensor
60 SimpleTensor<T> dst{ dst_shape, src.data_type(), 1 };
61
62 // Perform slice
63 Window win;
64 win.use_tensor_dimensions(dst_shape);
65 execute_window_loop(win, [&](const Coordinates & id)
66 {
67 Coordinates offset;
68 for(unsigned int i = 0; i < id.num_dimensions(); ++i)
69 {
70 offset.set(i, starts[i] + id[i]);
71 }
72 *reinterpret_cast<T *>(dst(id)) = *reinterpret_cast<const T *>(src(offset));
73 });
74
75 return dst;
76}
77
78template SimpleTensor<float> slice(const SimpleTensor<float> &src, Coordinates starts, Coordinates ends);
79template SimpleTensor<half_float::half> slice(const SimpleTensor<half_float::half> &src, Coordinates starts, Coordinates ends);
80
81template <typename T>
Georgios Pinitas77589b52018-08-21 14:41:35 +010082SimpleTensor<T> strided_slice(const SimpleTensor<T> &src,
83 Coordinates starts, Coordinates ends, BiStrides strides,
84 int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask)
85{
86 using namespace arm_compute::helpers::tensor_transform;
87
88 // Validation checks
89 ARM_COMPUTE_ERROR_ON(src.shape().num_dimensions() > 4);
90 ARM_COMPUTE_ERROR_ON(starts.num_dimensions() > src.shape().num_dimensions());
91 ARM_COMPUTE_ERROR_ON(ends.num_dimensions() > src.shape().num_dimensions());
92 ARM_COMPUTE_ERROR_ON(strides.num_dimensions() > src.shape().num_dimensions());
Georgios Pinitasc1a72452018-08-24 11:25:32 +010093 ARM_COMPUTE_ERROR_ON(std::any_of(strides.cbegin(), strides.cbegin() + strides.num_dimensions(), [](int i)
Georgios Pinitas77589b52018-08-21 14:41:35 +010094 {
Georgios Pinitasc1a72452018-08-24 11:25:32 +010095 return i == 0;
96 }));
Georgios Pinitas77589b52018-08-21 14:41:35 +010097
98 // Get source shape
99 const TensorShape &src_shape = src.shape();
100
101 // Get actual start, end coordinates and strides
102 const Coordinates final_strides = strided_slice_strides(src_shape, strides);
103 const Coordinates starts_abs = strided_slice_absolute_start_coords(src_shape, starts, final_strides, begin_mask);
104 const Coordinates ends_abs = strided_slice_absolute_end_coords(src_shape, starts_abs, ends, final_strides, end_mask, shrink_axis_mask);
105
106 // Get destination shape
107 const TensorShape dst_shape = compute_strided_slice_output_shape(src_shape, starts_abs, ends_abs, final_strides);
108
109 // Create destination tensor
110 SimpleTensor<T> dst{ dst_shape, src.data_type(), 1 };
111
112 // Perform strided slice
113 Window win;
114 win.use_tensor_dimensions(dst_shape);
115 execute_window_loop(win, [&](const Coordinates & id)
116 {
117 Coordinates offset;
118 for(unsigned int i = 0; i < id.num_dimensions(); ++i)
119 {
120 offset.set(i, starts_abs[i] + id[i] * final_strides[i]);
121 }
122 *reinterpret_cast<T *>(dst(id)) = *reinterpret_cast<const T *>(src(offset));
123 });
124
125 return dst;
126}
127
128template SimpleTensor<float> strided_slice(const SimpleTensor<float> &src,
129 Coordinates starts, Coordinates ends, BiStrides strides,
130 int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask);
131template SimpleTensor<half_float::half> strided_slice(const SimpleTensor<half_float::half> &src,
132 Coordinates starts, Coordinates ends, BiStrides strides,
133 int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask);
134} // namespace reference
135} // namespace validation
136} // namespace test
137} // namespace arm_compute