blob: f79c6a12351e06e2897911329f5324e83d595afb [file] [log] [blame]
Georgios Pinitasc1a72452018-08-24 11:25:32 +01001/*
ramelg016d891572021-09-29 10:05:09 +01002 * Copyright (c) 2018-2021 Arm Limited.
Georgios Pinitasc1a72452018-08-24 11:25:32 +01003 *
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/CLSlice.h"
25
26#include "arm_compute/core/CL/ICLTensor.h"
Georgios Pinitasc1a72452018-08-24 11:25:32 +010027#include "arm_compute/core/Types.h"
28#include "arm_compute/core/utils/helpers/tensor_transform.h"
Georgios Pinitasc1a72452018-08-24 11:25:32 +010029
ramelg016d891572021-09-29 10:05:09 +010030#include "src/common/utils/Log.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010031#include "src/core/CL/kernels/CLStridedSliceKernel.h"
ramelg016d891572021-09-29 10:05:09 +010032
Georgios Pinitasc1a72452018-08-24 11:25:32 +010033namespace arm_compute
34{
Michalis Spyrouf20d6d62020-07-16 17:46:51 +010035namespace experimental
Georgios Pinitasc1a72452018-08-24 11:25:32 +010036{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010037void CLSlice::configure(const CLCompileContext &compile_context,
38 const ITensorInfo *input,
39 ITensorInfo *output,
40 const Coordinates &starts,
41 const Coordinates &ends)
Manuel Bottini2b84be52020-04-08 10:15:51 +010042{
Georgios Pinitasc1a72452018-08-24 11:25:32 +010043 ARM_COMPUTE_ERROR_ON_NULLPTR(input);
ramelg016d891572021-09-29 10:05:09 +010044 ARM_COMPUTE_LOG_PARAMS(input, output, starts, ends);
Georgios Pinitasc1a72452018-08-24 11:25:32 +010045
46 // Get absolute end coordinates
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000047 const int32_t slice_end_mask = arm_compute::helpers::tensor_transform::construct_slice_end_mask(ends);
Georgios Pinitasc1a72452018-08-24 11:25:32 +010048
Georgios Pinitas40f51a62020-11-21 03:04:18 +000049 auto k = std::make_unique<CLStridedSliceKernel>();
Manuel Bottini2b84be52020-04-08 10:15:51 +010050 k->configure(compile_context, input, output, starts, ends, BiStrides(), 0, slice_end_mask, 0);
Georgios Pinitasc1a72452018-08-24 11:25:32 +010051 _kernel = std::move(k);
52}
53
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010054Status CLSlice::validate(const ITensorInfo *input,
55 const ITensorInfo *output,
56 const Coordinates &starts,
57 const Coordinates &ends)
Georgios Pinitasc1a72452018-08-24 11:25:32 +010058{
59 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input);
60
61 // Check start dimensions for being non-negative
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010062 ARM_COMPUTE_RETURN_ERROR_ON(
63 std::any_of(starts.cbegin(), starts.cbegin() + starts.num_dimensions(), [](int i) { return i < 0; }));
Georgios Pinitasc1a72452018-08-24 11:25:32 +010064
65 // Get absolute end coordinates
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000066 const int32_t slice_end_mask = arm_compute::helpers::tensor_transform::construct_slice_end_mask(ends);
Georgios Pinitasc1a72452018-08-24 11:25:32 +010067
Georgios Pinitasb4af2c62018-12-10 18:45:35 +000068 return CLStridedSliceKernel::validate(input, output, starts, ends, BiStrides(), 0, slice_end_mask, 0);
Georgios Pinitasc1a72452018-08-24 11:25:32 +010069}
Michalis Spyrouf20d6d62020-07-16 17:46:51 +010070} // namespace experimental
71
72struct CLSlice::Impl
73{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010074 const ICLTensor *src{nullptr};
75 ICLTensor *dst{nullptr};
76 std::unique_ptr<experimental::CLSlice> op{nullptr};
Michalis Spyrouf20d6d62020-07-16 17:46:51 +010077};
78
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010079CLSlice::CLSlice() : _impl(std::make_unique<Impl>())
Michalis Spyrouf20d6d62020-07-16 17:46:51 +010080{
81}
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010082CLSlice::CLSlice(CLSlice &&) = default;
Michalis Spyrouf20d6d62020-07-16 17:46:51 +010083CLSlice &CLSlice::operator=(CLSlice &&) = default;
84CLSlice::~CLSlice() = default;
85
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010086Status CLSlice::validate(const ITensorInfo *input,
87 const ITensorInfo *output,
88 const Coordinates &starts,
89 const Coordinates &ends)
Michalis Spyrouf20d6d62020-07-16 17:46:51 +010090{
91 return experimental::CLSlice::validate(input, output, starts, ends);
92}
93
94void CLSlice::configure(const ICLTensor *input, ICLTensor *output, const Coordinates &starts, const Coordinates &ends)
95{
96 configure(CLKernelLibrary::get().get_compile_context(), input, output, starts, ends);
97}
98
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010099void CLSlice::configure(const CLCompileContext &compile_context,
100 const ICLTensor *input,
101 ICLTensor *output,
102 const Coordinates &starts,
103 const Coordinates &ends)
Michalis Spyrouf20d6d62020-07-16 17:46:51 +0100104{
105 _impl->src = input;
106 _impl->dst = output;
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000107 _impl->op = std::make_unique<experimental::CLSlice>();
Michalis Spyrouf20d6d62020-07-16 17:46:51 +0100108 _impl->op->configure(compile_context, input->info(), output->info(), starts, ends);
109}
110
111void CLSlice::run()
112{
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100113 ITensorPack pack;
114 pack.add_tensor(TensorType::ACL_SRC, _impl->src);
115 pack.add_tensor(TensorType::ACL_DST, _impl->dst);
116 _impl->op->run(pack);
Michalis Spyrouf20d6d62020-07-16 17:46:51 +0100117}
Georgios Pinitasc1a72452018-08-24 11:25:32 +0100118} // namespace arm_compute