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 "kernels/ExpandDims.h"
18#include "kernels/Utils.h"
19
20namespace luci_interpreter
21{
22namespace kernels
23{
24
25ExpandDims::ExpandDims(const Tensor *input, const Tensor *axis, Tensor *output)
26 : Kernel({input, axis}, {output})
27{
28}
29
31{
32 int32_t axis_value;
33
34 switch (axis()->element_type())
35 {
36 case loco::DataType::S32:
37 axis_value = *getTensorData<int32_t>(axis());
38 break;
39 case loco::DataType::S64:
40 axis_value = static_cast<int32_t>(*getTensorData<int64_t>(axis()));
41 break;
42 default:
43 throw std::runtime_error("luci-intp ExpandDims Unsupported type.");
44 }
45
46 const auto input_shape = input()->shape();
47
48 if (axis_value < 0)
49 {
50 axis_value += input_shape.num_dims() + 1;
51 }
52
53 LUCI_INTERPRETER_CHECK(axis_value <= input_shape.num_dims() and axis_value >= 0);
54
55 Shape output_shape(input_shape.num_dims() + 1);
56 for (int32_t i = 0; i < output_shape.num_dims(); ++i)
57 {
58 if (i < axis_value)
59 {
60 output_shape.dim(i) = input_shape.dim(i);
61 }
62 else if (i == axis_value)
63 {
64 output_shape.dim(i) = 1;
65 }
66 else
67 {
69 output_shape.dim(i) = input_shape.dim(i - 1);
70 }
71 }
72
74}
75
77{
78 // Just copy input to output
79 const auto *input_data = input()->data<void>();
80 auto *output_data = output()->data<void>();
81
82 const size_t element_size = getDataTypeSize(input()->element_type());
83 const int32_t num_elements = input()->shape().num_elements();
84 std::memcpy(output_data, input_data, num_elements * element_size);
85}
86
87} // namespace kernels
88} // namespace luci_interpreter
int32_t num_elements() const
Definition Tensor.h:53
int num_dims() const
Definition Tensor.h:39
void resize(const Shape &new_shape)
Definition Tensor.cpp:56
const Shape & shape() const
Definition Tensor.h:107
const T * data() const
Definition Tensor.h:127
ExpandDims(const Tensor *input, const Tensor *axis, Tensor *output)
#define LUCI_INTERPRETER_CHECK(cond)
Definition Utils.h:36
const luci_interpreter::RuntimeShape output_shape
size_t getDataTypeSize(DataType data_type)
Definition DataType.h:33