ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Abs.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022 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/Abs.h"
18
19#include "kernels/Utils.h"
20
21#include <cmath> // abs for float
22
23namespace luci_interpreter
24{
25namespace kernels
26{
27
28Abs::Abs(const Tensor *input, Tensor *output) : Kernel({input}, {output}) {}
29
31{
32 LUCI_INTERPRETER_CHECK(input()->element_type() == output()->element_type());
33
34 output()->resize(input()->shape());
35}
36
37void Abs::execute() const
38{
39 switch (input()->element_type())
40 {
41 case DataType::FLOAT32:
42 eval<float>();
43 break;
44 default:
45 throw std::runtime_error("luci-intp Abs Unsupported type.");
46 }
47}
48
49template <typename T> void Abs::eval() const
50{
51 const auto *input_data = input()->data<T>();
52 auto *output_data = output()->data<T>();
53
54 const int size = tflite::MatchingFlatSize(getTensorShape(input()), getTensorShape(output()));
55
56 for (int i = 0; i < size; ++i)
57 {
58 output_data[i] = std::abs(input_data[i]);
59 }
60}
61
62} // namespace kernels
63} // namespace luci_interpreter
void resize(const Shape &new_shape)
Definition Tensor.cpp:56
const T * data() const
Definition Tensor.h:127
Abs(const Tensor *input, Tensor *output)
Definition Abs.cpp:28
void execute() const override
Definition Abs.cpp:37
void configure() override
Definition Abs.cpp:30
Tensor * output() const
Definition Abs.h:34
const Tensor * input() const
Definition Abs.h:33
#define LUCI_INTERPRETER_CHECK(cond)
Definition Utils.h:36
tflite::RuntimeShape getTensorShape(const Tensor *tensor)
Definition Utils.h:194
int32_t size[5]
Definition Slice.cpp:35