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.

unaryop.cpp 5.0 kB

Fix warnings on Visual Studio (#1456) * Fix warning C4244 in src/layer/convolution.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/convolution_sgemm_int8.h C4244: 'initializing': conversion from 'double' to 'int', possible loss of data * Fix warning C4244 in src/layer/deconvolution.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/elu.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4267 in src/layer/embed.cpp C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data * Fix warning C4244 in src/layer/exp.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/innerproduct.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/log.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data C4244: 'initializing': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/lrn.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/mvn.cp C4244: 'initializing': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/power.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warnings C4244 and C4267 in src/layer/proposal.cpp C4244: 'initializing': conversion from 'double' to 'float', possible loss of data C4244: 'initializing': conversion from 'double' to 'int', possible loss of data C4267: 'argument': conversion from 'size_t' to 'int', possible loss of data C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data * Fix warning C4244 in src/layer/reduction.cpp C4244: 'return': conversion from 'double' to 'T', possible loss of data * Fix warning C4244 in src/layer/tanh.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/binaryop.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warnings C4244 and C4267 in src/layer/unaryop.cpp C4244: 'return': conversion from 'double' to 'T', possible loss of data C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data * Fix warning C4244 in src/layer/x86/convolutiondepthwise_3x3_int8.h C4244: 'initializing': conversion from 'double' to 'int', possible loss of data
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
6 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 "unaryop.h"
  15. #include <math.h>
  16. namespace ncnn {
  17. UnaryOp::UnaryOp()
  18. {
  19. one_blob_only = true;
  20. support_inplace = true;
  21. }
  22. int UnaryOp::load_param(const ParamDict& pd)
  23. {
  24. op_type = pd.get(0, 0);
  25. return 0;
  26. }
  27. template<typename Op>
  28. static int unary_op_inplace(Mat& a, const Option& opt)
  29. {
  30. Op op;
  31. int size = static_cast<int>(a.total());
  32. #pragma omp parallel for num_threads(opt.num_threads)
  33. for (int i = 0; i < size; i++)
  34. {
  35. a[i] = op(a[i]);
  36. }
  37. return 0;
  38. }
  39. struct unary_op_abs
  40. {
  41. float operator()(const float& x) const
  42. {
  43. return (float)fabs(x);
  44. }
  45. };
  46. struct unary_op_neg
  47. {
  48. float operator()(const float& x) const
  49. {
  50. return -x;
  51. }
  52. };
  53. struct unary_op_floor
  54. {
  55. float operator()(const float& x) const
  56. {
  57. return (float)floor(x);
  58. }
  59. };
  60. struct unary_op_ceil
  61. {
  62. float operator()(const float& x) const
  63. {
  64. return (float)ceil(x);
  65. }
  66. };
  67. struct unary_op_square
  68. {
  69. float operator()(const float& x) const
  70. {
  71. return x * x;
  72. }
  73. };
  74. struct unary_op_sqrt
  75. {
  76. float operator()(const float& x) const
  77. {
  78. return (float)sqrt(x);
  79. }
  80. };
  81. struct unary_op_rsqrt
  82. {
  83. float operator()(const float& x) const
  84. {
  85. return (float)(1.f / sqrt(x));
  86. }
  87. };
  88. struct unary_op_exp
  89. {
  90. float operator()(const float& x) const
  91. {
  92. return (float)exp(x);
  93. }
  94. };
  95. struct unary_op_log
  96. {
  97. float operator()(const float& x) const
  98. {
  99. return (float)log(x);
  100. }
  101. };
  102. struct unary_op_sin
  103. {
  104. float operator()(const float& x) const
  105. {
  106. return (float)sin(x);
  107. }
  108. };
  109. struct unary_op_cos
  110. {
  111. float operator()(const float& x) const
  112. {
  113. return (float)cos(x);
  114. }
  115. };
  116. struct unary_op_tan
  117. {
  118. float operator()(const float& x) const
  119. {
  120. return (float)tan(x);
  121. }
  122. };
  123. struct unary_op_asin
  124. {
  125. float operator()(const float& x) const
  126. {
  127. return (float)asin(x);
  128. }
  129. };
  130. struct unary_op_acos
  131. {
  132. float operator()(const float& x) const
  133. {
  134. return (float)acos(x);
  135. }
  136. };
  137. struct unary_op_atan
  138. {
  139. float operator()(const float& x) const
  140. {
  141. return (float)atan(x);
  142. }
  143. };
  144. struct unary_op_reciprocal
  145. {
  146. float operator()(const float& x) const
  147. {
  148. return 1.f / x;
  149. }
  150. };
  151. struct unary_op_tanh
  152. {
  153. float operator()(const float& x) const
  154. {
  155. return (float)tanh(x);
  156. }
  157. };
  158. int UnaryOp::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
  159. {
  160. if (op_type == Operation_ABS)
  161. return unary_op_inplace<unary_op_abs>(bottom_top_blob, opt);
  162. if (op_type == Operation_NEG)
  163. return unary_op_inplace<unary_op_neg>(bottom_top_blob, opt);
  164. if (op_type == Operation_FLOOR)
  165. return unary_op_inplace<unary_op_floor>(bottom_top_blob, opt);
  166. if (op_type == Operation_CEIL)
  167. return unary_op_inplace<unary_op_ceil>(bottom_top_blob, opt);
  168. if (op_type == Operation_SQUARE)
  169. return unary_op_inplace<unary_op_square>(bottom_top_blob, opt);
  170. if (op_type == Operation_SQRT)
  171. return unary_op_inplace<unary_op_sqrt>(bottom_top_blob, opt);
  172. if (op_type == Operation_RSQRT)
  173. return unary_op_inplace<unary_op_rsqrt>(bottom_top_blob, opt);
  174. if (op_type == Operation_EXP)
  175. return unary_op_inplace<unary_op_exp>(bottom_top_blob, opt);
  176. if (op_type == Operation_LOG)
  177. return unary_op_inplace<unary_op_log>(bottom_top_blob, opt);
  178. if (op_type == Operation_SIN)
  179. return unary_op_inplace<unary_op_sin>(bottom_top_blob, opt);
  180. if (op_type == Operation_COS)
  181. return unary_op_inplace<unary_op_cos>(bottom_top_blob, opt);
  182. if (op_type == Operation_TAN)
  183. return unary_op_inplace<unary_op_tan>(bottom_top_blob, opt);
  184. if (op_type == Operation_ASIN)
  185. return unary_op_inplace<unary_op_asin>(bottom_top_blob, opt);
  186. if (op_type == Operation_ACOS)
  187. return unary_op_inplace<unary_op_acos>(bottom_top_blob, opt);
  188. if (op_type == Operation_ATAN)
  189. return unary_op_inplace<unary_op_atan>(bottom_top_blob, opt);
  190. if (op_type == Operation_RECIPROCAL)
  191. return unary_op_inplace<unary_op_reciprocal>(bottom_top_blob, opt);
  192. if (op_type == Operation_TANH)
  193. return unary_op_inplace<unary_op_tanh>(bottom_top_blob, opt);
  194. return 0;
  195. }
  196. } // namespace ncnn