Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / runtime / onert / backend / cpu / ops / GatherLayer.cc
1 /*
2  * Copyright (c) 2019 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 "GatherLayer.h"
18
19 #include "OperationUtils.h"
20
21 #include <cker/operation/Gather.h>
22
23 namespace onert
24 {
25 namespace backend
26 {
27 namespace cpu
28 {
29 namespace ops
30 {
31
32 void GatherLayer::configure(const IPortableTensor *input, const IPortableTensor *indices,
33                             IPortableTensor *output, int32_t axis)
34 {
35   _input = input;
36   _indices = indices;
37   _axis = axis;
38   _output = output;
39 }
40
41 template <typename InputType> void GatherLayer::runByInputType()
42 {
43   using OutputType = InputType;
44   nnfw::cker::GatherParams op_params;
45   op_params.axis = _axis;
46
47   switch (_indices->data_type())
48   {
49     case OperandType::INT32:
50     {
51       using IndicesType = int32_t;
52
53       nnfw::cker::Gather<InputType, IndicesType>(
54         op_params, getShape(_input), getBuffer<InputType>(_input), getShape(_indices),
55         getBuffer<IndicesType>(_indices), getShape(_output), getBuffer<OutputType>(_output));
56       break;
57     }
58     case OperandType::INT64:
59     {
60       using IndicesType = int64_t;
61
62       nnfw::cker::Gather<InputType, IndicesType>(
63         op_params, getShape(_input), getBuffer<InputType>(_input), getShape(_indices),
64         getBuffer<IndicesType>(_indices), getShape(_output), getBuffer<OutputType>(_output));
65       break;
66     }
67     default:
68       throw std::runtime_error("Gather: unsupported indices data type");
69   }
70 }
71
72 void GatherLayer::run()
73 {
74   switch (_input->data_type())
75   {
76     case OperandType::FLOAT32:
77       runByInputType<float>();
78       break;
79     case OperandType::QUANT_UINT8_ASYMM:
80       runByInputType<uint8_t>();
81       break;
82     case OperandType::INT32:
83       runByInputType<int32_t>();
84       break;
85     default:
86       throw std::runtime_error("Gather: unsupported input data type");
87   }
88 }
89
90 } // namespace ops
91 } // namespace cpu
92 } // namespace backend
93 } // namespace onert