| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | // |
| 5 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | // in compliance with the License. You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://opensource.org/licenses/BSD-3-Clause |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software distributed |
| 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | // specific language governing permissions and limitations under the License. |
| 14 | |
| 15 | #include "rmsnorm.h" |
| 16 | |
| 17 | namespace ncnn { |
| 18 | |
| 19 | RMSNorm::RMSNorm() |
| 20 | { |
| 21 | one_blob_only = true; |
| 22 | support_inplace = true; |
| 23 | } |
| 24 | |
| 25 | int RMSNorm::load_param(const ParamDict& pd) |
| 26 | { |
| 27 | affine_size = pd.get(0, 0); |
| 28 | eps = pd.get(1, 0.001f); |
| 29 | affine = pd.get(2, 1); |
| 30 | |
| 31 | return 0; |
| 32 | } |
| 33 | |
| 34 | int RMSNorm::load_model(const ModelBin& mb) |
| 35 | { |
| 36 | if (affine == 0) |
| 37 | return 0; |
| 38 | |
| 39 | gamma_data = mb.load(affine_size, 1); |
| 40 | if (gamma_data.empty()) |
| 41 | return -100; |
| 42 | |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | int RMSNorm::forward_inplace(Mat& bottom_top_blob, const Option& opt) const |
| 47 | { |
| 48 | // x = x / sqrt(rms + eps) * gamma |
| 49 | |
| 50 | int dims = bottom_top_blob.dims; |
| 51 | |
| 52 | if (dims == 1) |
| 53 | { |
| 54 | int w = bottom_top_blob.w; |
| 55 | // assert affine_size == w |
| 56 | |
| 57 | float* ptr = bottom_top_blob; |
| 58 | |
| 59 | float sqsum = 0.f; |
| 60 | for (int i = 0; i < w; i++) |
| 61 | { |
| 62 | sqsum += ptr[i] * ptr[i]; |
| 63 | } |
| 64 | float rms = sqrtf(sqsum / w + eps); |
| 65 | |
| 66 | float a = 1.f / rms; |
| 67 | |
| 68 | if (affine) |
| 69 | { |
| 70 | for (int i = 0; i < w; i++) |
| 71 | { |
| 72 | ptr[i] = (ptr[i] * a) * gamma_data[i]; |
| 73 | } |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | for (int i = 0; i < w; i++) |
| 78 | { |
| 79 | ptr[i] = ptr[i] * a; |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | if (dims == 2) |
| 85 | { |
| 86 | int w = bottom_top_blob.w; |
| 87 | int h = bottom_top_blob.h; |
| 88 | // assert affine_size == w |
| 89 | |
| 90 | #pragma omp parallel for num_threads(opt.num_threads) |
| 91 | for (int i = 0; i < h; i++) |
| 92 | { |
| 93 | float* ptr = bottom_top_blob.row(i); |
| 94 | |
| 95 | float sqsum = 0.f; |
| 96 | for (int j = 0; j < w; j++) |
| 97 | { |
| 98 | sqsum += ptr[j] * ptr[j]; |
| 99 | } |
| 100 | float rms = sqrtf(sqsum / w + eps); |
| 101 | |
| 102 | float a = 1.f / rms; |
| 103 | |
| 104 | if (affine) |
| 105 | { |
| 106 | for (int j = 0; j < w; j++) |
| 107 | { |
| 108 | ptr[j] = (ptr[j] * a) * gamma_data[j]; |
| 109 | } |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | for (int j = 0; j < w; j++) |
| 114 | { |
| 115 | ptr[j] = ptr[j] * a; |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | if (dims == 3) |
| 122 | { |
| 123 | int w = bottom_top_blob.w; |
| 124 | int h = bottom_top_blob.h; |
| 125 | int channels = bottom_top_blob.c; |
| 126 | int size = w * h; |
| 127 | |
| 128 | if (affine_size == w) |
| 129 | { |
| 130 | #pragma omp parallel for num_threads(opt.num_threads) |
| 131 | for (int q = 0; q < channels; q++) |
| 132 | { |
| 133 | for (int i = 0; i < h; i++) |
| 134 | { |
| 135 | float* ptr = bottom_top_blob.channel(q).row(i); |
| 136 | |
| 137 | float sqsum = 0.f; |
| 138 | for (int j = 0; j < w; j++) |
| 139 | { |
| 140 | sqsum += ptr[j] * ptr[j]; |
| 141 | } |
| 142 | float rms = sqrtf(sqsum / w + eps); |
| 143 | |
| 144 | float a = 1.f / rms; |
| 145 | |
| 146 | if (affine) |
| 147 | { |
| 148 | for (int j = 0; j < w; j++) |
| 149 | { |
| 150 | ptr[j] = (ptr[j] * a) * gamma_data[j]; |
| 151 | } |
| 152 | } |
| 153 | else |
| 154 | { |
| 155 | for (int j = 0; j < w; j++) |
| 156 | { |
| 157 | ptr[j] = ptr[j] * a; |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | else // if (affine_size == size) |
| 164 | { |
| 165 | #pragma omp parallel for num_threads(opt.num_threads) |
| 166 | for (int q = 0; q < channels; q++) |
| 167 | { |
| 168 | float* ptr = bottom_top_blob.channel(q); |
| 169 | |
| 170 | float sqsum = 0.f; |
| 171 | for (int i = 0; i < size; i++) |
| 172 | { |
| 173 | sqsum += ptr[i] * ptr[i]; |
| 174 | } |
| 175 | float rms = sqrtf(sqsum / size + eps); |
| 176 | |
| 177 | float a = 1.f / rms; |
| 178 | |
| 179 | if (affine) |
| 180 | { |
| 181 | for (int i = 0; i < size; i++) |
| 182 | { |
| 183 | ptr[i] = (ptr[i] * a) * gamma_data[i]; |
| 184 | } |
| 185 | } |
| 186 | else |
| 187 | { |
| 188 | for (int i = 0; i < size; i++) |
| 189 | { |
| 190 | ptr[i] = ptr[i] * a; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | } // namespace ncnn |