You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

batchnorm_arm.cpp 5.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2017 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. #include "batchnorm_arm.h"
  15. #if __ARM_NEON
  16. #include <arm_neon.h>
  17. #endif // __ARM_NEON
  18. namespace ncnn {
  19. DEFINE_LAYER_CREATOR(BatchNorm_arm)
  20. int BatchNorm_arm::forward(const Mat& bottom_blob, Mat& top_blob) const
  21. {
  22. // a = bias - slope * mean / sqrt(var)
  23. // b = slope / sqrt(var)
  24. // value = b * value + a
  25. int w = bottom_blob.w;
  26. int h = bottom_blob.h;
  27. int size = w * h;
  28. top_blob.create(w, h, channels);
  29. if (top_blob.empty())
  30. return -100;
  31. const float* a_data_ptr = a_data;
  32. const float* b_data_ptr = b_data;
  33. #pragma omp parallel for
  34. for (int q=0; q<channels; q++)
  35. {
  36. const float* ptr = bottom_blob.channel(q);
  37. float* outptr = top_blob.channel(q);
  38. float a = a_data_ptr[q];
  39. float b = b_data_ptr[q];
  40. #if __ARM_NEON
  41. int nn = size >> 2;
  42. int remain = size - (nn << 2);
  43. #else
  44. int remain = size;
  45. #endif // __ARM_NEON
  46. #if __ARM_NEON
  47. #if __aarch64__
  48. float32x4_t _a = vdupq_n_f32(a);
  49. float32x4_t _b = vdupq_n_f32(b);
  50. for (; nn>0; nn--)
  51. {
  52. float32x4_t _p = vld1q_f32(ptr);
  53. float32x4_t _outp = _a;
  54. _outp = vfmaq_f32(_outp, _p, _b);
  55. vst1q_f32(outptr, _outp);
  56. ptr += 4;
  57. outptr += 4;
  58. }
  59. #else
  60. if (nn > 0)
  61. {
  62. asm volatile(
  63. "vdup.f32 q1, %6 \n"
  64. "vdup.f32 q2, %7 \n"
  65. "0: \n"
  66. "pld [%1, #128] \n"
  67. "vld1.f32 {d0-d1}, [%1 :128]! \n"
  68. "vorr.32 q3, q1, q1 \n"
  69. "vmla.f32 q3, q0, q2 \n"
  70. "subs %0, #1 \n"
  71. "vst1.f32 {d6-d7}, [%2 :128]! \n"
  72. "bne 0b \n"
  73. : "=r"(nn), // %0
  74. "=r"(ptr), // %1
  75. "=r"(outptr) // %2
  76. : "0"(nn),
  77. "1"(ptr),
  78. "2"(outptr),
  79. "r"(a), // %6
  80. "r"(b) // %7
  81. : "cc", "memory", "q0", "q1", "q2", "q3"
  82. );
  83. }
  84. #endif // __aarch64__
  85. #endif // __ARM_NEON
  86. for (; remain>0; remain--)
  87. {
  88. *outptr = b * *ptr + a;
  89. ptr++;
  90. outptr++;
  91. }
  92. }
  93. return 0;
  94. }
  95. int BatchNorm_arm::forward_inplace(Mat& bottom_top_blob) const
  96. {
  97. // a = bias - slope * mean / sqrt(var)
  98. // b = slope / sqrt(var)
  99. // value = b * value + a
  100. int w = bottom_top_blob.w;
  101. int h = bottom_top_blob.h;
  102. int size = w * h;
  103. const float* a_data_ptr = a_data;
  104. const float* b_data_ptr = b_data;
  105. #pragma omp parallel for
  106. for (int q=0; q<channels; q++)
  107. {
  108. float* ptr = bottom_top_blob.channel(q);
  109. float a = a_data_ptr[q];
  110. float b = b_data_ptr[q];
  111. #if __ARM_NEON
  112. int nn = size >> 2;
  113. int remain = size - (nn << 2);
  114. #else
  115. int remain = size;
  116. #endif // __ARM_NEON
  117. #if __ARM_NEON
  118. #if __aarch64__
  119. float32x4_t _a = vdupq_n_f32(a);
  120. float32x4_t _b = vdupq_n_f32(b);
  121. for (; nn>0; nn--)
  122. {
  123. float32x4_t _p = vld1q_f32(ptr);
  124. float32x4_t _outp = _a;
  125. _outp = vfmaq_f32(_outp, _p, _b);
  126. vst1q_f32(ptr, _outp);
  127. ptr += 4;
  128. }
  129. #else
  130. if (nn > 0)
  131. {
  132. asm volatile(
  133. "vdup.f32 q1, %4 \n"
  134. "vdup.f32 q2, %5 \n"
  135. "0: \n"
  136. "pld [%1, #128] \n"
  137. "vld1.f32 {d0-d1}, [%1 :128] \n"
  138. "vorr.32 q3, q1, q1 \n"
  139. "vmla.f32 q3, q0, q2 \n"
  140. "subs %0, #1 \n"
  141. "vst1.f32 {d6-d7}, [%1 :128]! \n"
  142. "bne 0b \n"
  143. : "=r"(nn), // %0
  144. "=r"(ptr) // %1
  145. : "0"(nn),
  146. "1"(ptr),
  147. "r"(a), // %4
  148. "r"(b) // %5
  149. : "cc", "memory", "q0", "q1", "q2", "q3"
  150. );
  151. }
  152. #endif // __aarch64__
  153. #endif // __ARM_NEON
  154. for (; remain>0; remain--)
  155. {
  156. *ptr = b * *ptr + a;
  157. ptr++;
  158. }
  159. }
  160. return 0;
  161. }
  162. } // namespace ncnn

No Description

Contributors (1)