ONE - On-device Neural Engine
Loading...
Searching...
No Matches
PALL2Normalize.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 LUCI_INTERPRETER_PAL_L2_NORMALIZE_COMMON_H
19#define LUCI_INTERPRETER_PAL_L2_NORMALIZE_COMMON_H
20
21#include "PALUtils.h"
22#include <cmath>
23
25{
26
27inline void L2Normalization(const luci_interpreter::RuntimeShape &input_shape,
28 const float *input_data,
29 const luci_interpreter::RuntimeShape &output_shape, float *output_data,
30 float epsilon = 1e-6)
31{
32 const int trailing_dim = input_shape.dimensionsCount() - 1;
33 const int outer_size =
34 flatSizeSkipDim(input_shape.dimsData(), trailing_dim, input_shape.dimensionsCount());
35 const int depth = MatchingDim(input_shape, trailing_dim, output_shape, trailing_dim);
36 for (int i = 0; i < outer_size; ++i)
37 {
38 float squared_l2_norm = 0;
39 for (int c = 0; c < depth; ++c)
40 {
41 const float val = input_data[depth * i + c];
42 squared_l2_norm += val * val;
43 }
44 float l2_norm = std::sqrt(squared_l2_norm);
45 l2_norm = std::fmax(l2_norm, epsilon);
46 for (int c = 0; c < depth; ++c)
47 {
48 output_data[depth * i + c] = input_data[depth * i + c] / l2_norm;
49 }
50 }
51}
52
53} // namespace luci_interpreter_pal
54
55#endif // LUCI_INTERPRETER_PAL_L2_NORMALIZE_COMMON_H
int32_t dimensionsCount() const
Definition Tensor.h:106
const luci_interpreter::RuntimeShape output_shape
int flatSizeSkipDim(const int32_t *dims_data, int skip_dim, int num_dims)
Definition PALUtils.h:183
int MatchingDim(const luci_interpreter::RuntimeShape &shape1, int index1, const luci_interpreter::RuntimeShape &shape2, int index2)
Definition PALUtils.h:173
void L2Normalization(const luci_interpreter::RuntimeShape &input_shape, const float *input_data, const luci_interpreter::RuntimeShape &output_shape, float *output_data, float epsilon=1e-6)