Imported Upstream version 1.21.0
[platform/core/ml/nnfw.git] / compiler / luci-interpreter / src / kernels / Shape.cpp
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/Shape.h"
18 #include "kernels/Utils.h"
19
20 namespace luci_interpreter
21 {
22 namespace kernels
23 {
24
25 ShapeKernel::ShapeKernel(const Tensor *input, Tensor *output, const ShapeParams &params)
26   : KernelWithParams<ShapeParams>({input}, {output}, params)
27 {
28 }
29
30 void ShapeKernel::configure()
31 {
32   LUCI_INTERPRETER_CHECK(output()->element_type() == DataType::S32 or
33                          output()->element_type() == DataType::S64);
34   const auto input_shape = input()->shape();
35
36   Shape output_shape(1);
37   output_shape.dim(0) = input_shape.num_dims();
38
39   output()->resize(output_shape);
40 }
41
42 void ShapeKernel::execute() const
43 {
44   switch (params().out_type)
45   {
46     case DataType::S32:
47       evalInt<int32_t>();
48       break;
49     case DataType::S64:
50       evalInt<int64_t>();
51       break;
52     default:
53       throw std::runtime_error("Unsupported type.");
54   }
55 }
56
57 template <typename T> void ShapeKernel::evalInt() const
58 {
59   const auto input_shape = input()->shape();
60
61   auto output_data = getTensorData<T>(output());
62
63   for (int i = 0; i < input_shape.num_dims(); ++i)
64   {
65     output_data[i] = input_shape.dim(i);
66   }
67 }
68
69 } // namespace kernels
70 } // namespace luci_interpreter