Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / runtime / onert / backend / cpu / ops / ShapeLayer.cc
1 /*
2  * Copyright (c) 2020 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 "ShapeLayer.h"
18
19 #include "OperationUtils.h"
20
21 namespace onert
22 {
23 namespace backend
24 {
25 namespace cpu
26 {
27 namespace ops
28 {
29
30 ShapeLayer::ShapeLayer() : _input(nullptr), _output(nullptr)
31 {
32   // DO NOTHING
33 }
34
35 template <typename T> void GetRawShape(const IPortableTensor *input, T *output_data)
36 {
37   auto shape = input->getShape();
38   for (int i = 0; i < shape.rank(); ++i)
39   {
40     output_data[i] = static_cast<T>(shape.dim(i));
41   }
42 }
43
44 void ShapeLayer::configure(const IPortableTensor *input, IPortableTensor *output)
45 {
46   _input = input;
47   _output = output;
48 }
49
50 void ShapeLayer::run()
51 {
52   if (_output->data_type() == OperandType::UINT32)
53   {
54     GetRawShape(_input, getBuffer<uint32_t>(_output));
55   }
56   else if (_output->data_type() == OperandType::INT32)
57   {
58     GetRawShape(_input, getBuffer<int32_t>(_output));
59   }
60   else if (_output->data_type() == OperandType::INT64)
61   {
62     GetRawShape(_input, getBuffer<int64_t>(_output));
63   }
64   else
65   {
66     throw std::runtime_error{"NYI : not supported output type for ShapeLayer"};
67   }
68 }
69
70 } // namespace ops
71 } // namespace cpu
72 } // namespace backend
73 } // namespace onert