ONE - On-device Neural Engine
Loading...
Searching...
No Matches
ExpandDims.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022 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 "Builders.h"
18#include "kernels/Utils.h"
19
20namespace luci_interpreter
21{
22
23void configure_kernel_CircleExpandDims(const circle::Operator *cur_op,
24 BaseRuntimeGraph *runtime_graph)
25{
26 const auto input_index = cur_op->inputs()->operator[](0);
27 const auto axis_index = cur_op->inputs()->operator[](1);
28 const auto output_index = cur_op->outputs()->operator[](0);
29
30 assert(input_index != -1);
31 assert(axis_index != -1);
32 assert(output_index != -1);
33
34 const auto input = runtime_graph->getCircleTensorByIndex(input_index);
35 const auto axis = runtime_graph->getCircleTensorByIndex(axis_index);
36 auto output = runtime_graph->getCircleTensorByIndex(output_index);
37
38 assert(input != nullptr);
39 assert(axis != nullptr);
40 assert(output != nullptr);
41
42 auto axis_data = runtime_graph->getConstDataByTensor(axis);
43 assert(axis_data != nullptr);
44
45 int32_t axis_value;
46
47 switch (Tensor::element_type(axis))
48 {
49 case DataType::S32:
50 axis_value = *reinterpret_cast<int32_t *>(axis_data);
51 break;
52 case DataType::S64:
53 axis_value = static_cast<int32_t>(*reinterpret_cast<int64_t *>(axis_data));
54 break;
55 default:
56 assert(false && "Unsupported type.");
57 }
58
59 if (axis_value < 0)
60 {
61 axis_value += Tensor::num_dims(input) + 1;
62 }
63
64 LUCI_INTERPRETER_CHECK(axis_value <= Tensor::num_dims(input) and axis_value >= 0);
65}
66
67void execute_kernel_CircleExpandDims(const circle::Operator *cur_op,
68 BaseRuntimeGraph *runtime_graph)
69{
70 const auto input_index = cur_op->inputs()->operator[](0);
71 const auto output_index = cur_op->outputs()->operator[](0);
72
73 assert(input_index != -1);
74 assert(output_index != -1);
75
76 const auto input = runtime_graph->getCircleTensorByIndex(input_index);
77 const auto output = runtime_graph->getCircleTensorByIndex(output_index);
78
79 bool is_inplace = runtime_graph->is_inplace_op(cur_op);
80
81 if (is_inplace)
82 {
83 runtime_graph->makeInplaceOperation(input, output);
84 return;
85 }
86
87 // Just copy input to output
88 const auto input_data = runtime_graph->getDataByTensor(input);
89 auto output_data = runtime_graph->getDataByTensor(output);
90
91 assert(input_data != nullptr);
92 assert(output_data != nullptr);
93
94 const size_t element_size = getDataTypeSize(Tensor::element_type(input));
95 const int32_t num_elements = Tensor::num_elements(input);
96 std::memcpy(output_data, input_data, num_elements * element_size);
97}
98
99} // namespace luci_interpreter
void makeInplaceOperation(const circle::Tensor *src_tensor, const circle::Tensor *dst_tensor)
uint8_t * getConstDataByTensor(const circle::Tensor *raw_tensor)
const circle::Tensor * getCircleTensorByIndex(int32_t index)
bool is_inplace_op(const circle::Operator *op)
uint8_t * getDataByTensor(const circle::Tensor *raw_tensor)
#define LUCI_INTERPRETER_CHECK(cond)
Definition Utils.h:36
void execute_kernel_CircleExpandDims(const circle::Operator *cur_op, BaseRuntimeGraph *runtime_graph)
size_t getDataTypeSize(DataType data_type)
Definition DataType.h:33
void configure_kernel_CircleExpandDims(const circle::Operator *cur_op, BaseRuntimeGraph *runtime_graph)