ONE - On-device Neural Engine
Loading...
Searching...
No Matches
StridedSlice.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "../KernelGenerator.h"
18#include "../Validator.h"
19
20#include <AclKernelGen.h>
21
23{
24
25void Validator::visit(const ir::operation::StridedSlice &) { _supported = true; }
26
27void KernelGenerator::visit(const ir::operation::StridedSlice &node)
28{
29 const auto output_index{node.getOutputs().at(0)};
30 const auto input_index{node.getInputs().at(ir::operation::StridedSlice::Input::INPUT)};
31 const auto starts_index{node.getInputs().at(ir::operation::StridedSlice::Input::STARTS)};
32 const auto ends_index{node.getInputs().at(ir::operation::StridedSlice::Input::ENDS)};
33 const auto strides_index{node.getInputs().at(ir::operation::StridedSlice::Input::STRIDES)};
34
35 auto outputData_tensor = _tensor_reg->getAclTensor(output_index);
36 auto inputData_tensor = _tensor_reg->getAclTensor(input_index);
37
38 // Set initializers for indices data such as order of inputData
39 int input_rank = _ctx.at(input_index).shape().rank();
40 std::vector<int32_t> starts;
41 std::vector<int32_t> ends;
42 std::vector<int32_t> strides;
43 starts.resize(input_rank, 0);
44 ends.resize(input_rank, 0);
45 strides.resize(input_rank, 0);
46 {
47 auto startData_base = _ctx.at(starts_index).data()->base();
48 auto endData_base = _ctx.at(ends_index).data()->base();
49 auto stridesData_base = _ctx.at(strides_index).data()->base();
50 [[maybe_unused]] const int startData_size = _ctx.at(starts_index).shape().num_elements();
51 [[maybe_unused]] const int endData_size = _ctx.at(ends_index).shape().num_elements();
52 [[maybe_unused]] const int stridesData_size = _ctx.at(strides_index).shape().num_elements();
53
54 using ir::DataType;
55
56 assert(_ctx.at(starts_index).typeInfo().type() == DataType::INT32);
57 assert(_ctx.at(ends_index).typeInfo().type() == DataType::INT32);
58 assert(_ctx.at(strides_index).typeInfo().type() == DataType::INT32);
59 assert(startData_size == input_rank);
60 assert(endData_size == input_rank);
61 assert(stridesData_size == input_rank);
62
63 assert(startData_base != nullptr);
64 for (int n = 0; n < input_rank; ++n)
65 {
66 auto axis = ::onert::backend::acl_common::ToARMComputeAxis(input_rank, n).value();
67
68 int32_t start_value = *(reinterpret_cast<const int32_t *>(startData_base) + n);
69 starts[axis] = start_value;
70
71 int32_t end_value = *(reinterpret_cast<const int32_t *>(endData_base) + n);
72 ends[axis] = end_value;
73
74 int32_t strides_value = *(reinterpret_cast<const int32_t *>(stridesData_base) + n);
75 strides[axis] = strides_value;
76 }
77 }
78
79 // Set mask bits such as order of inputData
80 const auto begin_mask = acl_common::ReorderBits<int32_t>(node.param().begin_mask, input_rank);
81 const auto end_mask = acl_common::ReorderBits<int32_t>(node.param().end_mask, input_rank);
82 const auto shrink_axis_mask =
83 acl_common::ReorderBits<int32_t>(node.param().shrink_axis_mask, input_rank);
84
85 ::arm_compute::Coordinates starts_set;
86 ::arm_compute::Coordinates ends_set;
87 ::arm_compute::BiStrides strides_set;
88
89 for (size_t i = 0; i < starts.size(); ++i)
90 {
91 starts_set.set(i, starts[i]);
92 ends_set.set(i, ends[i]);
93 strides_set.set(i, strides[i]);
94 }
95
96 // Disable applied dim_correction
97 if (static_cast<size_t>(inputData_tensor->getShape().rank()) !=
98 inputData_tensor->info()->num_dimensions())
99 {
100 // This means that high dimension's value is 1 and input tensor is applied dim_correction
101 acl_common::disableDimCorrection(inputData_tensor);
102 }
103
104 auto fn = acl_common::generateLayer<arm_compute::NEStridedSlice>(
105 inputData_tensor->handle(), outputData_tensor->handle(), starts_set, ends_set, strides_set,
106 begin_mask, end_mask, shrink_axis_mask);
107
108 // Revert disabling applied dim_correction
109 if (inputData_tensor->getShape().dim(0) == 1)
110 {
111 acl_common::enableDimCorrection(inputData_tensor);
112 }
113
114 _return_fn = acl_common::asAclFunction(std::move(fn));
115}
116
117} // namespace onert::backend::acl_neon
std::unique_ptr< exec::IFunction > _return_fn
const Object & at(const Index &index) const
Get the object that is associated with the given index.
ARMComputeAxis ToARMComputeAxis(uint32_t rank, uint32_t axis)
Definition Swizzle.h:45
void enableDimCorrection(IACLTensor *tensor)
std::unique_ptr< AclFunction > asAclFunction(std::unique_ptr<::arm_compute::IFunction > &&layer)
Definition Convert.cc:246
void disableDimCorrection(IACLTensor *tensor)
OperandType type
Definition Operand.h:42