ONE - On-device Neural Engine
Loading...
Searching...
No Matches
FloorMod.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2020 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_FLOOR_MOD_H__
19#define __NNFW_CKER_FLOOR_MOD_H__
20
21#include "cker/Shape.h"
22#include "cker/Utils.h"
23
24#include <functional>
25#include <stdexcept>
26#include <string>
27
28namespace nnfw
29{
30namespace cker
31{
32
33template <typename T>
34inline void FloorModBroadcast(const Shape &unextended_input1_shape, const T *input1_data,
35 const Shape &unextended_input2_shape, const T *input2_data,
36 const Shape &unextended_output_shape, T *output_data)
37{
38 struct FloatMod
39 {
40 float operator()(const float lhs, const float rhs) const { return std::fmod(lhs, rhs); }
41 };
42
43 using ModFunc =
44 typename std::conditional<std::is_integral<T>::value, std::modulus<T>, FloatMod>::type;
45
46 if (unextended_output_shape.DimensionsCount() > 4)
47 throw std::runtime_error(std::string("cker::FloorModBroadcast: Unsupported rank size : ") +
48 std::to_string(unextended_output_shape.DimensionsCount()));
49 const Shape output_shape = Shape::ExtendedShape(4, unextended_output_shape);
50
53 NdArrayDescsForElementwiseBroadcast(unextended_input1_shape, unextended_input2_shape, &desc1,
54 &desc2);
55
56 for (int b = 0; b < output_shape.Dims(0); ++b)
57 {
58 for (int y = 0; y < output_shape.Dims(1); ++y)
59 {
60 for (int x = 0; x < output_shape.Dims(2); ++x)
61 {
62 for (int c = 0; c < output_shape.Dims(3); ++c)
63 {
64 auto out_idx = Offset(output_shape, b, y, x, c);
65 auto in1_idx = SubscriptToIndex(desc1, b, y, x, c);
66 auto in2_idx = SubscriptToIndex(desc2, b, y, x, c);
67 auto in1_val = input1_data[in1_idx];
68 auto in2_val = input2_data[in2_idx];
69
70 ModFunc mod_func;
71 T trunc_mod = mod_func(in1_val, in2_val);
72 output_data[out_idx] = (trunc_mod != 0) && ((in2_val < 0) != (trunc_mod < 0))
73 ? (trunc_mod + in2_val)
74 : trunc_mod;
75 }
76 }
77 }
78 }
79}
80
81template <typename T>
82inline void FloorModElementwise(const Shape &shape, const T *input1_data, const T *input2_data,
83 T *output_data)
84{
85 struct FloatMod
86 {
87 float operator()(const float lhs, const float rhs) const { return std::fmod(lhs, rhs); }
88 };
89
90 using ModFunc =
91 typename std::conditional<std::is_integral<T>::value, std::modulus<T>, FloatMod>::type;
92
93 int num_elements = shape.FlatSize();
94 for (int t = 0; t < num_elements; t++)
95 {
96 ModFunc mod_func;
97 auto in1_val = input1_data[t];
98 auto in2_val = input2_data[t];
99 T trunc_mod = mod_func(in1_val, in2_val);
100 output_data[t] =
101 (trunc_mod != 0) && ((in2_val < 0) != (trunc_mod < 0)) ? (trunc_mod + in2_val) : trunc_mod;
102 }
103}
104
105} // namespace cker
106} // namespace nnfw
107
108#endif // __NNFW_CKER_FLOOR_MOD_H__
int32_t DimensionsCount() const
Definition Shape.h:91
int FlatSize() const
Definition Shape.h:181
NdArrayDesc< 4 > desc1
const luci_interpreter::RuntimeShape output_shape
NdArrayDesc< 4 > desc2
int Offset(const Shape &shape, int i0, int i1, int i2, int i3)
Definition Shape.h:237
void NdArrayDescsForElementwiseBroadcast(const Shape &input0_shape, const Shape &input1_shape, NdArrayDesc< N > *desc0_out, NdArrayDesc< N > *desc1_out)
Definition Utils.h:290
void FloorModBroadcast(const Shape &unextended_input1_shape, const T *input1_data, const Shape &unextended_input2_shape, const T *input2_data, const Shape &unextended_output_shape, T *output_data)
Definition FloorMod.h:34
void FloorModElementwise(const Shape &shape, const T *input1_data, const T *input2_data, T *output_data)
Definition FloorMod.h:82
int SubscriptToIndex(const NdArrayDesc< 4 > &desc, int i0, int i1, int i2, int i3)
Definition Utils.h:255
Definition topk_v2.h:30