2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "kernels/Reverse.h"
18 #include "kernels/Utils.h"
19 #include <tensorflow/lite/kernels/internal/reference/reference_ops.h>
21 namespace luci_interpreter
27 Reverse::Reverse(const Tensor *input, const Tensor *axes, Tensor *output)
28 : Kernel({input, axes}, {output})
32 void Reverse::configure()
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)
40 throw std::runtime_error("Unsupported input type.");
42 if (axes()->element_type() != DataType::S32)
44 throw std::runtime_error("Unsupported axes type.");
46 if (axes()->shape().num_elements() > 1)
48 throw std::runtime_error("Current implementation does not support more than 1 axis.");
50 int axis_value = getTensorData<int32_t>(axes())[0];
51 if (axis_value < 0 || axis_value >= input()->shape().num_dims())
53 throw std::runtime_error("Invalid axes value");
55 assert(input()->element_type() == output()->element_type());
57 output()->resize(input()->shape());
60 void Reverse::execute() const
62 int axis_value = getTensorData<int32_t>(axes())[0];
63 switch (output()->element_type())
65 case DataType::FLOAT32:
66 tflite::reference_ops::Reverse<float>(axis_value, getTensorShape(input()),
67 getTensorData<float>(input()), getTensorShape(output()),
68 getTensorData<float>(output()));
71 tflite::reference_ops::Reverse<uint8_t>(
72 axis_value, getTensorShape(input()), getTensorData<uint8_t>(input()),
73 getTensorShape(output()), getTensorData<uint8_t>(output()));
76 throw std::runtime_error("Unsupported output type");
80 } // namespace kernels
81 } // namespace luci_interpreter