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.

softmax_arm.cpp 4.2 kB

8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 "softmax_arm.h"
  15. #include <float.h>
  16. #include <math.h>
  17. #if __ARM_NEON
  18. #include <arm_neon.h>
  19. #include "neon_mathfun.h"
  20. #endif // __ARM_NEON
  21. namespace ncnn {
  22. DEFINE_LAYER_CREATOR(Softmax_arm)
  23. int Softmax_arm::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
  24. {
  25. int dims = bottom_top_blob.dims;
  26. if (dims != 3 || axis != 0)
  27. return Softmax::forward_inplace(bottom_top_blob, opt);
  28. // value = exp( value - global max value )
  29. // sum all value
  30. // value = value / sum
  31. int w = bottom_top_blob.w;
  32. int h = bottom_top_blob.h;
  33. int channels = bottom_top_blob.c;
  34. size_t elemsize = bottom_top_blob.elemsize;
  35. int size = w * h;
  36. Mat max;
  37. max.create(w, h, elemsize, opt.workspace_allocator);
  38. if (max.empty())
  39. return -100;
  40. max.fill(-FLT_MAX);
  41. for (int q=0; q<channels; q++)
  42. {
  43. float* ptr = bottom_top_blob.channel(q);
  44. float* maxptr = max;
  45. for (int i=0; i<size; i++)
  46. {
  47. maxptr[i] = std::max(maxptr[i], ptr[i]);
  48. }
  49. }
  50. #pragma omp parallel for num_threads(opt.num_threads)
  51. for (int q=0; q<channels; q++)
  52. {
  53. float* ptr = bottom_top_blob.channel(q);
  54. float* maxptr = max;
  55. #if __ARM_NEON
  56. int nn = size >> 2;
  57. int remain = size - (nn << 2);
  58. #else
  59. int remain = size;
  60. #endif // __ARM_NEON
  61. #if __ARM_NEON
  62. for (; nn>0; nn--)
  63. {
  64. float32x4_t _p = vld1q_f32(ptr);
  65. float32x4_t _max = vld1q_f32(maxptr);
  66. _p = exp_ps(vsubq_f32(_p, _max));
  67. vst1q_f32(ptr, _p);
  68. ptr += 4;
  69. maxptr += 4;
  70. }
  71. #endif // __ARM_NEON
  72. for (; remain>0; remain--)
  73. {
  74. *ptr = exp(*ptr - *maxptr);
  75. ptr++;
  76. maxptr++;
  77. }
  78. }
  79. Mat sum;
  80. sum.create(w, h, elemsize, opt.workspace_allocator);
  81. if (sum.empty())
  82. return -100;
  83. sum.fill(0.f);
  84. for (int q=0; q<channels; q++)
  85. {
  86. float* ptr = bottom_top_blob.channel(q);
  87. float* sumptr = sum;
  88. #if __ARM_NEON
  89. int nn = size >> 2;
  90. int remain = size - (nn << 2);
  91. #else
  92. int remain = size;
  93. #endif // __ARM_NEON
  94. #if __ARM_NEON
  95. for (; nn>0; nn--)
  96. {
  97. float32x4_t _p = vld1q_f32(ptr);
  98. float32x4_t _sum = vld1q_f32(sumptr);
  99. _sum = vaddq_f32(_sum, _p);
  100. vst1q_f32(sumptr, _sum);
  101. ptr += 4;
  102. sumptr += 4;
  103. }
  104. #endif // __ARM_NEON
  105. for (; remain>0; remain--)
  106. {
  107. *sumptr += *ptr;
  108. ptr++;
  109. sumptr++;
  110. }
  111. }
  112. #pragma omp parallel for num_threads(opt.num_threads)
  113. for (int q=0; q<channels; q++)
  114. {
  115. float* ptr = bottom_top_blob.channel(q);
  116. float* sumptr = sum;
  117. #if __ARM_NEON
  118. int nn = size >> 2;
  119. int remain = size - (nn << 2);
  120. #else
  121. int remain = size;
  122. #endif // __ARM_NEON
  123. #if __ARM_NEON
  124. for (; nn>0; nn--)
  125. {
  126. float32x4_t _p = vld1q_f32(ptr);
  127. float32x4_t _sum = vld1q_f32(sumptr);
  128. #if __aarch64__
  129. _p = vdivq_f32(_p, _sum);
  130. #else
  131. _p = div_ps(_p, _sum);
  132. #endif // __aarch64__
  133. vst1q_f32(ptr, _p);
  134. ptr += 4;
  135. sumptr += 4;
  136. }
  137. #endif // __ARM_NEON
  138. for (; remain>0; remain--)
  139. {
  140. *ptr /= *sumptr;
  141. ptr++;
  142. sumptr++;
  143. }
  144. }
  145. return 0;
  146. }
  147. } // namespace ncnn