Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 1 | /* |
ramelg01 | 6d89157 | 2021-09-29 10:05:09 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2021 Arm Limited. |
Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 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 "arm_compute/runtime/CL/functions/CLSlice.h" |
| 25 | |
| 26 | #include "arm_compute/core/CL/ICLTensor.h" |
Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Types.h" |
| 28 | #include "arm_compute/core/utils/helpers/tensor_transform.h" |
Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 29 | |
ramelg01 | 6d89157 | 2021-09-29 10:05:09 +0100 | [diff] [blame] | 30 | #include "src/common/utils/Log.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 31 | #include "src/core/CL/kernels/CLStridedSliceKernel.h" |
ramelg01 | 6d89157 | 2021-09-29 10:05:09 +0100 | [diff] [blame] | 32 | |
Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 33 | namespace arm_compute |
| 34 | { |
Michalis Spyrou | f20d6d6 | 2020-07-16 17:46:51 +0100 | [diff] [blame] | 35 | namespace experimental |
Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 36 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 37 | void CLSlice::configure(const CLCompileContext &compile_context, |
| 38 | const ITensorInfo *input, |
| 39 | ITensorInfo *output, |
| 40 | const Coordinates &starts, |
| 41 | const Coordinates &ends) |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 42 | { |
Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 43 | ARM_COMPUTE_ERROR_ON_NULLPTR(input); |
ramelg01 | 6d89157 | 2021-09-29 10:05:09 +0100 | [diff] [blame] | 44 | ARM_COMPUTE_LOG_PARAMS(input, output, starts, ends); |
Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 45 | |
| 46 | // Get absolute end coordinates |
Georgios Pinitas | b4af2c6 | 2018-12-10 18:45:35 +0000 | [diff] [blame] | 47 | const int32_t slice_end_mask = arm_compute::helpers::tensor_transform::construct_slice_end_mask(ends); |
Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 48 | |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 49 | auto k = std::make_unique<CLStridedSliceKernel>(); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 50 | k->configure(compile_context, input, output, starts, ends, BiStrides(), 0, slice_end_mask, 0); |
Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 51 | _kernel = std::move(k); |
| 52 | } |
| 53 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 54 | Status CLSlice::validate(const ITensorInfo *input, |
| 55 | const ITensorInfo *output, |
| 56 | const Coordinates &starts, |
| 57 | const Coordinates &ends) |
Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 58 | { |
| 59 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input); |
| 60 | |
| 61 | // Check start dimensions for being non-negative |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 62 | ARM_COMPUTE_RETURN_ERROR_ON( |
| 63 | std::any_of(starts.cbegin(), starts.cbegin() + starts.num_dimensions(), [](int i) { return i < 0; })); |
Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 64 | |
| 65 | // Get absolute end coordinates |
Georgios Pinitas | b4af2c6 | 2018-12-10 18:45:35 +0000 | [diff] [blame] | 66 | const int32_t slice_end_mask = arm_compute::helpers::tensor_transform::construct_slice_end_mask(ends); |
Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 67 | |
Georgios Pinitas | b4af2c6 | 2018-12-10 18:45:35 +0000 | [diff] [blame] | 68 | return CLStridedSliceKernel::validate(input, output, starts, ends, BiStrides(), 0, slice_end_mask, 0); |
Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 69 | } |
Michalis Spyrou | f20d6d6 | 2020-07-16 17:46:51 +0100 | [diff] [blame] | 70 | } // namespace experimental |
| 71 | |
| 72 | struct CLSlice::Impl |
| 73 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 74 | const ICLTensor *src{nullptr}; |
| 75 | ICLTensor *dst{nullptr}; |
| 76 | std::unique_ptr<experimental::CLSlice> op{nullptr}; |
Michalis Spyrou | f20d6d6 | 2020-07-16 17:46:51 +0100 | [diff] [blame] | 77 | }; |
| 78 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 79 | CLSlice::CLSlice() : _impl(std::make_unique<Impl>()) |
Michalis Spyrou | f20d6d6 | 2020-07-16 17:46:51 +0100 | [diff] [blame] | 80 | { |
| 81 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 82 | CLSlice::CLSlice(CLSlice &&) = default; |
Michalis Spyrou | f20d6d6 | 2020-07-16 17:46:51 +0100 | [diff] [blame] | 83 | CLSlice &CLSlice::operator=(CLSlice &&) = default; |
| 84 | CLSlice::~CLSlice() = default; |
| 85 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 86 | Status CLSlice::validate(const ITensorInfo *input, |
| 87 | const ITensorInfo *output, |
| 88 | const Coordinates &starts, |
| 89 | const Coordinates &ends) |
Michalis Spyrou | f20d6d6 | 2020-07-16 17:46:51 +0100 | [diff] [blame] | 90 | { |
| 91 | return experimental::CLSlice::validate(input, output, starts, ends); |
| 92 | } |
| 93 | |
| 94 | void 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 99 | void CLSlice::configure(const CLCompileContext &compile_context, |
| 100 | const ICLTensor *input, |
| 101 | ICLTensor *output, |
| 102 | const Coordinates &starts, |
| 103 | const Coordinates &ends) |
Michalis Spyrou | f20d6d6 | 2020-07-16 17:46:51 +0100 | [diff] [blame] | 104 | { |
| 105 | _impl->src = input; |
| 106 | _impl->dst = output; |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 107 | _impl->op = std::make_unique<experimental::CLSlice>(); |
Michalis Spyrou | f20d6d6 | 2020-07-16 17:46:51 +0100 | [diff] [blame] | 108 | _impl->op->configure(compile_context, input->info(), output->info(), starts, ends); |
| 109 | } |
| 110 | |
| 111 | void CLSlice::run() |
| 112 | { |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 113 | 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 Spyrou | f20d6d6 | 2020-07-16 17:46:51 +0100 | [diff] [blame] | 117 | } |
Georgios Pinitas | c1a7245 | 2018-08-24 11:25:32 +0100 | [diff] [blame] | 118 | } // namespace arm_compute |