Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / src / kernels / ReverseV2.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/ReverseV2.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 ReverseV2::ReverseV2(const Tensor *input, const Tensor *axes, Tensor *output)
28   : Kernel({input, axes}, {output})
29 {
30 }
31
32 void ReverseV2::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     assert(false && "Unsupported input type.");
41   }
42   if (axes()->element_type() != DataType::S32)
43   {
44     assert(false && "Unsupported axes type.");
45   }
46   if (axes()->shape().num_elements() > 1)
47   {
48     assert(false && "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     assert(false && "Invalid axes value");
54   }
55   assert(input()->element_type() == output()->element_type());
56
57   // TODO: enable it only if kernel with dynamic shapes
58   output()->resize(input()->shape());
59 }
60
61 void ReverseV2::execute() const
62 {
63   int axis_value = getTensorData<int32_t>(axes())[0];
64   switch (output()->element_type())
65   {
66     case DataType::FLOAT32:
67       tflite::reference_ops::Reverse<float>(axis_value, getTensorShape(input()),
68                                             getTensorData<float>(input()), getTensorShape(output()),
69                                             getTensorData<float>(output()));
70       break;
71     case DataType::U8:
72       tflite::reference_ops::Reverse<uint8_t>(
73         axis_value, getTensorShape(input()), getTensorData<uint8_t>(input()),
74         getTensorShape(output()), getTensorData<uint8_t>(output()));
75       break;
76     default:
77       assert(false && "Unsupported output type");
78   }
79 }
80
81 } // namespace kernels
82 } // namespace luci_interpreter