Imported Upstream version 1.8.0
[platform/core/ml/nnfw.git] / compiler / luci-interpreter / src / kernels / Reverse.cpp
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 "kernels/Reverse.h"
18 #include "kernels/Utils.h"
19 #include <tensorflow/lite/kernels/internal/reference/reference_ops.h>
20
21 namespace luci_interpreter
22 {
23
24 namespace kernels
25 {
26
27 Reverse::Reverse(const Tensor *input, const Tensor *axes, Tensor *output)
28     : Kernel({input, axes}, {output})
29 {
30 }
31
32 void Reverse::configure()
33 {
34   assert(axes()->shape().num_dims() == 1);
35   assert(input()->shape().num_dims() >= axes()->shape().num_elements());
36   if (input()->element_type() != DataType::S32 && input()->element_type() != DataType::FLOAT32 &&
37       input()->element_type() != DataType::U8 && input()->element_type() != DataType::S16 &&
38       input()->element_type() != DataType::S64)
39   {
40     throw std::runtime_error("Unsupported input type.");
41   }
42   if (axes()->element_type() != DataType::S32)
43   {
44     throw std::runtime_error("Unsupported axes type.");
45   }
46   if (axes()->shape().num_elements() > 1)
47   {
48     throw std::runtime_error("Current implementation does not support more than 1 axis.");
49   }
50   int axis_value = getTensorData<int32_t>(axes())[0];
51   if (axis_value < 0 || axis_value >= input()->shape().num_dims())
52   {
53     throw std::runtime_error("Invalid axes value");
54   }
55   assert(input()->element_type() == output()->element_type());
56
57   output()->resize(input()->shape());
58 }
59
60 void Reverse::execute() const
61 {
62   int axis_value = getTensorData<int32_t>(axes())[0];
63   switch (output()->element_type())
64   {
65     case DataType::FLOAT32:
66       tflite::reference_ops::Reverse<float>(axis_value, getTensorShape(input()),
67                                             getTensorData<float>(input()), getTensorShape(output()),
68                                             getTensorData<float>(output()));
69       break;
70     case DataType::U8:
71       tflite::reference_ops::Reverse<uint8_t>(
72           axis_value, getTensorShape(input()), getTensorData<uint8_t>(input()),
73           getTensorShape(output()), getTensorData<uint8_t>(output()));
74       break;
75     default:
76       throw std::runtime_error("Unsupported output type");
77   }
78 }
79
80 } // namespace kernels
81 } // namespace luci_interpreter