| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2020 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 "layernorm.h" |
| 16 | |
| 17 | namespace ncnn { |
| 18 | |
| 19 | LayerNorm::LayerNorm() |
| 20 | { |
| 21 | one_blob_only = true; |
| 22 | support_inplace = true; |
| 23 | } |
| 24 | |
| 25 | int LayerNorm::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 LayerNorm::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 | beta_data = mb.load(affine_size, 1); |
| 44 | if (beta_data.empty()) |
| 45 | return -100; |
| 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | int LayerNorm::forward_inplace(Mat& bottom_top_blob, const Option& opt) const |
| 51 | { |
| 52 | // x = (x - mean) / sqrt(var + eps) * gamma + beta |
| 53 | |
| 54 | int dims = bottom_top_blob.dims; |
| 55 | |
| 56 | if (dims == 1) |
| 57 | { |
| 58 | int w = bottom_top_blob.w; |
| 59 | // assert affine_size == w |
| 60 | |
| 61 | float* ptr = bottom_top_blob; |
| 62 | |
| 63 | // mean and var |
| 64 | float sum = 0.f; |
| 65 | float sqsum = 0.f; |
| 66 | for (int i = 0; i < w; i++) |
| 67 | { |
| 68 | sum += ptr[i]; |
| 69 | //sqsum += ptr[i] * ptr[i]; |
| 70 | } |
| 71 | float mean = sum / w; |
| 72 | float tmp = 0.f; |
| 73 | for (int i = 0; i < w; i++) |
| 74 | { |
| 75 | tmp = ptr[i] - mean; |
| 76 | sqsum += tmp * tmp; |
| 77 | } |
| 78 | float var = sqsum / w; |
| 79 | // the var maybe minus due to accuracy |
| 80 | //float var = sqsum / w - mean * mean; |
| 81 | |
| 82 | float a = 1.f / (sqrtf(var + eps)); |
| 83 | float b = -mean * a; |
| 84 | |
| 85 | if (affine) |
| 86 | { |
| 87 | for (int i = 0; i < w; i++) |
| 88 | { |
| 89 | ptr[i] = (ptr[i] * a + b) * gamma_data[i] + beta_data[i]; |
| 90 | } |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | for (int i = 0; i < w; i++) |
| 95 | { |
| 96 | ptr[i] = ptr[i] * a + b; |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | if (dims == 2) |
| 102 | { |
| 103 | int w = bottom_top_blob.w; |
| 104 | int h = bottom_top_blob.h; |
| 105 | // assert affine_size == w |
| 106 | |
| 107 | #pragma omp parallel for num_threads(opt.num_threads) |
| 108 | for (int i = 0; i < h; i++) |
| 109 | { |
| 110 | float* ptr = bottom_top_blob.row(i); |
| 111 | |
| 112 | // mean and var |
| 113 | float sum = 0.f; |
| 114 | float sqsum = 0.f; |
| 115 | for (int j = 0; j < w; j++) |
| 116 | { |
| 117 | sum += ptr[j]; |
| 118 | //sqsum += ptr[j] * ptr[j]; |
| 119 | } |
| 120 | float mean = sum / w; |
| 121 | float tmp = 0.f; |
| 122 | for (int j = 0; j < w; j++) |
| 123 | { |
| 124 | tmp = ptr[j] - mean; |
| 125 | sqsum += tmp * tmp; |
| 126 | } |
| 127 | float var = sqsum / w; |
| 128 | // the var maybe minus due to accuracy |
| 129 | //float var = sqsum / w - mean * mean; |
| 130 | |
| 131 | float a = 1.f / (sqrtf(var + eps)); |
| 132 | float b = -mean * a; |
| 133 | |
| 134 | if (affine) |
| 135 | { |
| 136 | for (int j = 0; j < w; j++) |
| 137 | { |
| 138 | ptr[j] = (ptr[j] * a + b) * gamma_data[j] + beta_data[j]; |
| 139 | } |
| 140 | } |
| 141 | else |
| 142 | { |
| 143 | for (int j = 0; j < w; j++) |
| 144 | { |
| 145 | ptr[j] = ptr[j] * a + b; |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | if (dims == 3) |
| 152 | { |
| 153 | int w = bottom_top_blob.w; |
| 154 | int h = bottom_top_blob.h; |
| 155 | int channels = bottom_top_blob.c; |
| 156 | int size = w * h; |
| 157 | |
| 158 | if (affine_size == w) |
| 159 | { |
| 160 | #pragma omp parallel for num_threads(opt.num_threads) |
| 161 | for (int q = 0; q < channels; q++) |
| 162 | { |
| 163 | for (int i = 0; i < h; i++) |
| 164 | { |
| 165 | float* ptr = bottom_top_blob.channel(q).row(i); |
| 166 | |
| 167 | // mean and var |
| 168 | float sum = 0.f; |
| 169 | float sqsum = 0.f; |
| 170 | for (int j = 0; j < w; j++) |
| 171 | { |
| 172 | sum += ptr[j]; |
| 173 | //sqsum += ptr[j] * ptr[j]; |
| 174 | } |
| 175 | float mean = sum / w; |
| 176 | float tmp = 0.f; |
| 177 | for (int j = 0; j < w; j++) |
| 178 | { |
| 179 | tmp = ptr[j] - mean; |
| 180 | sqsum += tmp * tmp; |
| 181 | } |
| 182 | float var = sqsum / w; |
| 183 | // the var maybe minus due to accuracy |
| 184 | //float var = sqsum / w - mean * mean; |
| 185 | |
| 186 | float a = 1.f / (sqrtf(var + eps)); |
| 187 | float b = -mean * a; |
| 188 | |
| 189 | if (affine) |
| 190 | { |
| 191 | for (int j = 0; j < w; j++) |
| 192 | { |
| 193 | ptr[j] = (ptr[j] * a + b) * gamma_data[j] + beta_data[j]; |
| 194 | } |
| 195 | } |
| 196 | else |
| 197 | { |
| 198 | for (int j = 0; j < w; j++) |
| 199 | { |
| 200 | ptr[j] = ptr[j] * a + b; |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | else // if (affine_size == size) |
| 207 | { |
| 208 | #pragma omp parallel for num_threads(opt.num_threads) |
| 209 | for (int q = 0; q < channels; q++) |
| 210 | { |
| 211 | float* ptr = bottom_top_blob.channel(q); |
| 212 | |
| 213 | // mean and var |
| 214 | float sum = 0.f; |
| 215 | float sqsum = 0.f; |
| 216 | for (int i = 0; i < size; i++) |
| 217 | { |
| 218 | sum += ptr[i]; |
| 219 | //sqsum += ptr[i] * ptr[i]; |
| 220 | } |
| 221 | float mean = sum / size; |
| 222 | float tmp = 0.f; |
| 223 | for (int i = 0; i < size; i++) |
| 224 | { |
| 225 | tmp = ptr[i] - mean; |
| 226 | sqsum += tmp * tmp; |
| 227 | } |
| 228 | float var = sqsum / size; |
| 229 | // the var maybe minus due to accuracy |
| 230 | //float var = sqsum / size - mean * mean; |
| 231 | |
| 232 | float a = 1.f / (sqrtf(var + eps)); |
| 233 | float b = -mean * a; |
| 234 | |
| 235 | if (affine) |
| 236 | { |
| 237 | for (int i = 0; i < size; i++) |
| 238 | { |
| 239 | ptr[i] = (ptr[i] * a + b) * gamma_data[i] + beta_data[i]; |
| 240 | } |
| 241 | } |
| 242 | else |
| 243 | { |
| 244 | for (int i = 0; i < size; i++) |
| 245 | { |
| 246 | ptr[i] = ptr[i] * a + b; |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | } // namespace ncnn |