ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Elementwise.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2018 The TensorFlow Authors. All Rights Reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef __NNFW_CKER_ELEMENTWISE_H__
19#define __NNFW_CKER_ELEMENTWISE_H__
20
21#include "cker/eigen/Utils.h"
22#include "cker/Shape.h"
23#include "cker/Types.h"
24#include <Eigen/Core>
25
26namespace nnfw
27{
28namespace cker
29{
30
31inline void Sin(const Shape &input_shape, const float *input_data, const Shape &output_shape,
32 float *output_data)
33{
34 const int size = MatchingFlatSize(input_shape, output_shape);
35 for (int i = 0; i < size; i++)
36 {
37 output_data[i] = std::sin(input_data[i]);
38 }
39}
40
41inline void Cos(const Shape &input_shape, const float *input_data, const Shape &output_shape,
42 float *output_data)
43{
44 const int size = MatchingFlatSize(input_shape, output_shape);
45 for (int i = 0; i < size; i++)
46 {
47 output_data[i] = std::cos(input_data[i]);
48 }
49}
50
51inline void Abs(const Shape &input_shape, const float *input_data, const Shape &output_shape,
52 float *output_data)
53{
54 auto input_map = MapAsVector(input_data, input_shape);
55 auto output_map = MapAsVector(output_data, output_shape);
56 output_map.array() = input_map.array().abs();
57}
58
59inline void Rsqrt(const Shape &input_shape, const float *input_data, const Shape &output_shape,
60 float *output_data)
61{
62 const int size = MatchingFlatSize(input_shape, output_shape);
63 for (int i = 0; i < size; i++)
64 {
65 output_data[i] = 1.f / std::sqrt(input_data[i]);
66 }
67}
68
69template <typename T>
70inline void Neg(const Shape &input_shape, const T *input_data, const Shape &output_shape,
71 T *output_data)
72{
73 const int size = MatchingFlatSize(input_shape, output_shape);
74 for (int i = 0; i < size; i++)
75 {
76 output_data[i] = -input_data[i];
77 }
78}
79
80inline void Log(const Shape &input_shape, const float *input_data, const Shape &output_shape,
81 float *output_data)
82{
83 const int size = MatchingFlatSize(input_shape, output_shape);
84 for (int i = 0; i < size; i++)
85 {
86 output_data[i] = std::log(input_data[i]);
87 }
88}
89
90inline void Floor(const Shape &input_shape, const float *input_data, const Shape &output_shape,
91 float *output_data)
92{
93 const int flat_size = MatchingFlatSize(input_shape, output_shape);
94
95 for (int i = 0; i < flat_size; i++)
96 {
97 output_data[i] = std::floor(input_data[i]);
98 }
99}
100
101inline void Sqrt(const Shape &input_shape, const float *input_data, const Shape &output_shape,
102 float *output_data)
103{
104 const int flat_size = MatchingFlatSize(input_shape, output_shape);
105
106 for (int i = 0; i < flat_size; i++)
107 {
108 output_data[i] = std::sqrt(input_data[i]);
109 }
110}
111
112inline void Square(const Shape &input_shape, const float *input_data, const Shape &output_shape,
113 float *output_data)
114{
115 const int flat_size = MatchingFlatSize(input_shape, output_shape);
116
117 for (int i = 0; i < flat_size; i++)
118 {
119 output_data[i] = input_data[i] * input_data[i];
120 }
121}
122
123} // namespace cker
124} // namespace nnfw
125
126#endif // __NNFW_CKER_ELEMENTWISE_H__
const luci_interpreter::RuntimeShape output_shape
void Log(const Shape &input_shape, const float *input_data, const Shape &output_shape, float *output_data)
Definition Elementwise.h:80
void Square(const Shape &input_shape, const float *input_data, const Shape &output_shape, float *output_data)
void Cos(const Shape &input_shape, const float *input_data, const Shape &output_shape, float *output_data)
Definition Elementwise.h:41
void Neg(const Shape &input_shape, const T *input_data, const Shape &output_shape, T *output_data)
Definition Elementwise.h:70
void Sin(const Shape &input_shape, const float *input_data, const Shape &output_shape, float *output_data)
Definition Elementwise.h:31
void Abs(const Shape &input_shape, const float *input_data, const Shape &output_shape, float *output_data)
Definition Elementwise.h:51
void Rsqrt(const Shape &input_shape, const float *input_data, const Shape &output_shape, float *output_data)
Definition Elementwise.h:59
void Sqrt(const Shape &input_shape, const float *input_data, const Shape &output_shape, float *output_data)
int MatchingFlatSize(const Shape &shape, Ts... check_shapes)
Definition Shape.h:383
void Floor(const Shape &input_shape, const float *input_data, const Shape &output_shape, float *output_data)
Definition Elementwise.h:90
VectorMap< Scalar > MapAsVector(Scalar *data, const Shape &shape)
Definition Utils.h:43
Definition topk_v2.h:30
int32_t size[5]
Definition Slice.cpp:35