ONE - On-device Neural Engine
Loading...
Searching...
No Matches
ReverseV2.cpp
Go to the documentation of this file.
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
21namespace luci_interpreter
22{
23
24namespace kernels
25{
26
27ReverseV2::ReverseV2(const Tensor *input, const Tensor *axes, Tensor *output)
28 : Kernel({input, axes}, {output})
29{
30}
31
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 }
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
61{
63 std::array<int32_t, 8> axis_array = {axis_value};
64 switch (output()->element_type())
65 {
66 case DataType::FLOAT32:
67 tflite::reference_ops::Reverse<float>(axis_array, 1, getTensorShape(input()),
70 break;
71 case DataType::U8:
72 tflite::reference_ops::Reverse<uint8_t>(axis_array, 1, getTensorShape(input()),
75 break;
76 default:
77 throw std::runtime_error("Unsupported output type");
78 }
79}
80
81} // namespace kernels
82} // namespace luci_interpreter
void resize(const Shape &new_shape)
Definition Tensor.cpp:56
ReverseV2(const Tensor *input, const Tensor *axes, Tensor *output)
Definition ReverseV2.cpp:27
const Tensor * input() const
Definition ReverseV2.h:32
const Tensor * axes() const
Definition ReverseV2.h:33
tflite::RuntimeShape getTensorShape(const Tensor *tensor)
Definition Utils.h:194
T must_cast(loco::Node *node)