ONE - On-device Neural Engine
Loading...
Searching...
No Matches
RmsNorm.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 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/RmsNorm.h"
18
19#include "kernels/Utils.h"
20
21#include <tensorflow/lite/kernels/internal/common.h>
22#include <cmath>
23
24namespace luci_interpreter
25{
26namespace kernels
27{
28
29RmsNorm::RmsNorm(const Tensor *input, const Tensor *gamma, Tensor *output,
30 const RmsNormParams &params)
31 : KernelWithParams<RmsNormParams>({input, gamma}, {output}, params)
32{
33}
34
36{
37 auto num_dims = input()->shape().num_dims();
38 LUCI_INTERPRETER_CHECK(num_dims == 3 || num_dims == 4);
39 LUCI_INTERPRETER_CHECK(input()->element_type() == output()->element_type());
40 LUCI_INTERPRETER_CHECK(gamma()->element_type() == input()->element_type());
41 LUCI_INTERPRETER_CHECK(gamma()->shape().num_dims() == 1);
42 LUCI_INTERPRETER_CHECK((gamma()->shape().dim(0) == input()->shape().dim(num_dims - 1)) ||
43 (gamma()->shape().dim(0) == 1));
44
45 output()->resize(input()->shape());
46}
47
48void RmsNorm::execute() const
49{
50 switch (input()->element_type())
51 {
52 case DataType::FLOAT32:
53 evalFloat();
54 break;
55 default:
56 throw std::runtime_error("luci-intp RmsNorm Unsupported type.");
57 }
58}
59
60void RmsNorm::evalFloat() const
61{
62 tflite::RuntimeShape input_shape = getTensorShape(input());
64
65 const float *input_data = getTensorData<float>(input());
66 const float *gamma_data = getTensorData<float>(gamma());
67 auto gamma_shape = getTensorShape(gamma());
68 bool single_gamma = gamma_shape.DimensionsCount() == 1 && gamma_shape.Dims(0) == 1;
69 float *output_data = getTensorData<float>(output());
70
71 if (input_shape.DimensionsCount() == 4)
72 {
73 // Dimensions for image case are (N x H x W x C)
74 const int32_t batches = tflite::MatchingDim(input_shape, 0, output_shape, 0);
75 const int32_t heights = tflite::MatchingDim(input_shape, 1, output_shape, 1);
76 const int32_t widths = tflite::MatchingDim(input_shape, 2, output_shape, 2);
77 const int32_t channels = tflite::MatchingDim(input_shape, 3, output_shape, 3);
78 for (int32_t batch = 0; batch < batches; batch++)
79 {
80 for (int32_t height = 0; height < heights; height++)
81 {
82 for (int32_t width = 0; width < widths; width++)
83 {
84 double square_sum = 0.0f;
85 for (int32_t channel = 0; channel < channels; channel++)
86 {
87 double input_val =
88 input_data[tflite::Offset(input_shape, batch, height, width, channel)];
89 square_sum += (input_val * input_val);
90 }
91 double rms = std::sqrt((square_sum / channels) + params().epsilon);
92 for (int32_t channel = 0; channel < channels; channel++)
93 {
94 double gamma = single_gamma ? gamma_data[0] : gamma_data[channel];
95 output_data[tflite::Offset(output_shape, batch, height, width, channel)] =
96 gamma *
97 (input_data[tflite::Offset(input_shape, batch, height, width, channel)] / rms);
98 }
99 }
100 }
101 }
102 }
103 else if (input_shape.DimensionsCount() == 3)
104 {
105 // Dimensions for non image case are (N x C x D1 x D2 … Dn)
106 const int32_t batches = tflite::MatchingDim(input_shape, 0, output_shape, 0);
107 const int32_t channels = tflite::MatchingDim(input_shape, 1, output_shape, 1);
108 const int32_t size = tflite::MatchingDim(input_shape, 2, output_shape, 2);
109 for (int32_t batch = 0; batch < batches; batch++)
110 {
111 for (int32_t channel = 0; channel < channels; channel++)
112 {
113 double square_sum = 0.0f;
114 size_t offset =
115 static_cast<size_t>(batch * channels * size) + static_cast<size_t>(channel * size);
116 for (int32_t i = 0; i < size; i++)
117 {
118 double input_val = input_data[offset + i];
119 square_sum += (input_val * input_val);
120 }
121 double rms = std::sqrt((square_sum / size) + params().epsilon);
122 for (int32_t i = 0; i < size; i++)
123 {
124 double gamma = single_gamma ? gamma_data[0] : gamma_data[i];
125 output_data[offset + i] = gamma * (input_data[offset + i] / rms);
126 }
127 }
128 }
129 }
130 else
131 throw std::runtime_error("luci-intp RmsNorm unsupported rank.");
132}
133
134} // namespace kernels
135} // namespace luci_interpreter
const RmsNormParams & params() const
Definition Kernel.h:67
int num_dims() const
Definition Tensor.h:39
void resize(const Shape &new_shape)
Definition Tensor.cpp:56
const Shape & shape() const
Definition Tensor.h:107
RmsNorm(const Tensor *input, const Tensor *gamma, Tensor *output, const RmsNormParams &params)
Definition RmsNorm.cpp:29
const Tensor * gamma() const
Definition RmsNorm.h:34
const Tensor * input() const
Definition RmsNorm.h:33
void execute() const override
Definition RmsNorm.cpp:48
#define LUCI_INTERPRETER_CHECK(cond)
Definition Utils.h:36
__global uchar * offset(const Image *img, int x, int y)
Definition helpers.h:540
const luci_interpreter::RuntimeShape output_shape
list input_data
Definition infer.py:29
tflite::RuntimeShape getTensorShape(const Tensor *tensor)
Definition Utils.h:194
int32_t size[5]
Definition Slice.cpp:35