Gian Marco Iodice | 761c8d0 | 2019-06-10 14:46:49 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019 ARM Limited. |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #include "FuseBatchNormalization.h" |
| 25 | |
| 26 | namespace arm_compute |
| 27 | { |
| 28 | namespace test |
| 29 | { |
| 30 | namespace validation |
| 31 | { |
| 32 | namespace reference |
| 33 | { |
| 34 | template <typename T> |
| 35 | void fuse_batch_normalization_dwc_layer(const SimpleTensor<T> &w, const SimpleTensor<T> &mean, const SimpleTensor<T> &var, SimpleTensor<T> &w_fused, SimpleTensor<T> &b_fused, const SimpleTensor<T> &b, |
| 36 | const SimpleTensor<T> &beta, const SimpleTensor<T> &gamma, float epsilon) |
| 37 | { |
| 38 | const auto *w_data = w.data(); |
| 39 | const auto *b_data = b.data(); |
| 40 | |
| 41 | auto *w_fused_data = w_fused.data(); |
| 42 | auto *b_fused_data = b_fused.data(); |
| 43 | |
| 44 | const unsigned int width = w.shape()[0]; |
| 45 | const unsigned int height = w.shape()[1]; |
| 46 | const unsigned int dim2 = w.shape()[2]; |
| 47 | |
| 48 | for(unsigned int b = 0; b < dim2; ++b) |
| 49 | { |
| 50 | const auto mean_val = mean.data()[b]; |
| 51 | const auto var_val = var.data()[b]; |
| 52 | const auto beta_val = beta.data()[b]; |
| 53 | const auto gamma_val = gamma.data()[b]; |
| 54 | |
| 55 | for(unsigned int i = 0; i < width * height; ++i) |
| 56 | { |
| 57 | unsigned int index = i + b * width * height; |
| 58 | |
| 59 | w_fused_data[index] = (gamma_val * (w_data[index])) / sqrt(var_val + epsilon); |
| 60 | } |
| 61 | |
| 62 | b_fused_data[b] = (b_data[b] - mean_val) / sqrt(var_val + epsilon) * gamma_val + beta_val; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | template <typename T> |
| 67 | void fuse_batch_normalization_conv_layer(const SimpleTensor<T> &w, const SimpleTensor<T> &mean, const SimpleTensor<T> &var, SimpleTensor<T> &w_fused, SimpleTensor<T> &b_fused, |
| 68 | const SimpleTensor<T> &b, |
| 69 | const SimpleTensor<T> &beta, const SimpleTensor<T> &gamma, float epsilon) |
| 70 | { |
| 71 | const auto *w_data = w.data(); |
| 72 | const auto *b_data = b.data(); |
| 73 | |
| 74 | auto *w_fused_data = w_fused.data(); |
| 75 | auto *b_fused_data = b_fused.data(); |
| 76 | |
| 77 | const unsigned int width = w.shape()[0]; |
| 78 | const unsigned int height = w.shape()[1]; |
| 79 | const unsigned int dim2 = w.shape()[2]; |
| 80 | const unsigned int dim3 = w.shape()[3]; |
| 81 | |
| 82 | for(unsigned int b = 0; b < dim3; ++b) |
| 83 | { |
| 84 | const auto mean_val = mean.data()[b]; |
| 85 | const auto var_val = var.data()[b]; |
| 86 | const auto beta_val = beta.data()[b]; |
| 87 | const auto gamma_val = gamma.data()[b]; |
| 88 | |
| 89 | for(unsigned int i = 0; i < width * height * dim2; ++i) |
| 90 | { |
| 91 | unsigned int index = i + b * width * height * dim2; |
| 92 | |
| 93 | w_fused_data[index] = (gamma_val * (w_data[index])) / sqrt(var_val + epsilon); |
| 94 | } |
| 95 | |
| 96 | b_fused_data[b] = (b_data[b] - mean_val) / sqrt(var_val + epsilon) * gamma_val + beta_val; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | template void fuse_batch_normalization_dwc_layer(const SimpleTensor<float> &w, const SimpleTensor<float> &mean, const SimpleTensor<float> &var, SimpleTensor<float> &w_fused, |
| 101 | SimpleTensor<float> &b_fused, const SimpleTensor<float> &b, const SimpleTensor<float> &beta, const SimpleTensor<float> &gamma, float epsilon); |
| 102 | template void fuse_batch_normalization_dwc_layer(const SimpleTensor<half> &w, const SimpleTensor<half> &mean, const SimpleTensor<half> &var, SimpleTensor<half> &w_fused, SimpleTensor<half> &b_fused, |
| 103 | const SimpleTensor<half> &b, const SimpleTensor<half> &beta, const SimpleTensor<half> &gamma, float epsilon); |
| 104 | template void fuse_batch_normalization_conv_layer(const SimpleTensor<float> &w, const SimpleTensor<float> &mean, const SimpleTensor<float> &var, SimpleTensor<float> &w_fused, |
| 105 | SimpleTensor<float> &b_fused, const SimpleTensor<float> &b, const SimpleTensor<float> &beta, const SimpleTensor<float> &gamma, float epsilon); |
| 106 | template void fuse_batch_normalization_conv_layer(const SimpleTensor<half> &w, const SimpleTensor<half> &mean, const SimpleTensor<half> &var, SimpleTensor<half> &w_fused, SimpleTensor<half> &b_fused, |
| 107 | const SimpleTensor<half> &b, const SimpleTensor<half> &beta, const SimpleTensor<half> &gamma, float epsilon); |
| 108 | } // namespace reference |
| 109 | } // namespace validation |
| 110 | } // namespace test |
| 111 | } // namespace arm_compute |