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.

norm.cpp 8.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #include "test/common/norm.h"
  2. #include "megdnn/dtype.h"
  3. #include "megdnn/oprs.h"
  4. #include "test/common/benchmarker.h"
  5. #include "test/common/checker.h"
  6. #include "test/naive/fixture.h"
  7. namespace megdnn {
  8. namespace test {
  9. TEST_F(NAIVE, L2NORM_FP32_DIM0) {
  10. Checker<Norm> checker(handle(), false);
  11. Norm::Param param;
  12. param.p = 2;
  13. param.dim = 0;
  14. checker.set_param(param);
  15. checker.exect(
  16. Testcase{
  17. TensorValue(
  18. {1, 2, 3, 4}, dtype::Float32(),
  19. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
  20. 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}),
  21. {}},
  22. Testcase{
  23. {},
  24. TensorValue(
  25. {1, 2, 3, 4}, dtype::Float32(),
  26. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
  27. 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}),
  28. });
  29. }
  30. TEST_F(NAIVE, L2NORM_FP32_DIM1) {
  31. Checker<Norm> checker(handle());
  32. Norm::Param param;
  33. param.p = 2;
  34. param.dim = 1;
  35. checker.set_param(param);
  36. checker.exect(
  37. Testcase{
  38. TensorValue(
  39. {1, 2, 3, 4}, dtype::Float32(),
  40. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
  41. 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}),
  42. {}},
  43. Testcase{
  44. {},
  45. TensorValue(
  46. {1, 1, 3, 4}, dtype::Float32(),
  47. {12.000, 13.0384, 14.1421, 15.2971, 16.4924, 17.7200,
  48. 18.9737, 20.2485, 21.5407, 22.8473, 24.1661, 25.4951}),
  49. });
  50. }
  51. TEST_F(NAIVE, L2NORM_FP32_DIM3) {
  52. Checker<Norm> checker(handle());
  53. Norm::Param param;
  54. param.p = 2;
  55. param.dim = 3;
  56. checker.set_param(param).exect(
  57. Testcase{
  58. TensorValue(
  59. {1, 2, 3, 4}, dtype::Float32(),
  60. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
  61. 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}),
  62. {}},
  63. Testcase{
  64. {},
  65. TensorValue(
  66. {1, 2, 3, 1}, dtype::Float32(),
  67. {3.7417, 11.2250, 19.1311, 27.0924, 35.0714, 43.0581})});
  68. }
  69. // l2, fp16
  70. TEST_F(NAIVE, L2NORM_FP16_DIM3) {
  71. Checker<Norm> checker(handle());
  72. Norm::Param param;
  73. param.p = 2;
  74. param.dim = 3;
  75. checker.set_param(param).exect(
  76. Testcase{
  77. TensorValue(
  78. {1, 2, 3, 4}, dtype::Float16(),
  79. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
  80. 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}),
  81. {}},
  82. Testcase{
  83. {},
  84. TensorValue(
  85. {1, 2, 3, 1}, dtype::Float16(),
  86. {3.7422, 11.2266, 19.1250, 27.0938, 35.0625, 43.0625})});
  87. }
  88. // l1, fp32,fp16
  89. TEST_F(NAIVE, L1NORM_FP32_DIM3) {
  90. Checker<Norm> checker(handle());
  91. Norm::Param param;
  92. param.p = 1;
  93. param.dim = 3;
  94. checker.set_param(param).exect(
  95. Testcase{
  96. TensorValue(
  97. {1, 2, 3, 4}, dtype::Float32(),
  98. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
  99. 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}),
  100. {}},
  101. Testcase{
  102. {},
  103. TensorValue(
  104. {1, 2, 3, 1}, dtype::Float32(), {6, 22, 38, 54, 70, 86}),
  105. });
  106. }
  107. TEST_F(NAIVE, L1NORM_FP16_DIM3) {
  108. Checker<Norm> checker(handle());
  109. Norm::Param param;
  110. param.p = 1;
  111. param.dim = 3;
  112. checker.set_param(param).exect(
  113. Testcase{
  114. TensorValue(
  115. {1, 2, 3, 4}, dtype::Float16(),
  116. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
  117. 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}),
  118. {}},
  119. Testcase{
  120. {},
  121. TensorValue(
  122. {1, 2, 3, 1}, dtype::Float16(), {6, 22, 38, 54, 70, 86}),
  123. });
  124. }
  125. // l0, fp32,fp16
  126. TEST_F(NAIVE, L0NORM_FP32_DIM3) {
  127. Checker<Norm> checker(handle());
  128. Norm::Param param;
  129. param.p = 0;
  130. param.dim = 3;
  131. checker.set_param(param).exect(
  132. Testcase{
  133. TensorValue(
  134. {1, 2, 3, 4}, dtype::Float32(),
  135. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
  136. 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}),
  137. {}},
  138. Testcase{
  139. {},
  140. TensorValue({1, 2, 3, 1}, dtype::Float32(), {3, 4, 4, 4, 4, 4}),
  141. });
  142. }
  143. TEST_F(NAIVE, L0NORM_FP16_DIM3) {
  144. Checker<Norm> checker(handle());
  145. Norm::Param param;
  146. param.p = 0;
  147. param.dim = 3;
  148. checker.set_param(param).exect(
  149. Testcase{
  150. TensorValue(
  151. {1, 2, 3, 4}, dtype::Float16(),
  152. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
  153. 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}),
  154. {}},
  155. Testcase{
  156. {},
  157. TensorValue({1, 2, 3, 1}, dtype::Float16(), {3, 4, 4, 4, 4, 4}),
  158. });
  159. }
  160. // inf
  161. TEST_F(NAIVE, INF_NORM_FP32_DIM3) {
  162. Checker<Norm> checker(handle());
  163. Norm::Param param;
  164. using Mode = Norm::Param::Mode;
  165. param.dim = 3;
  166. param.mode = Mode::INF_NORM;
  167. checker.set_param(param).exect(
  168. Testcase{
  169. TensorValue(
  170. {1, 2, 3, 4}, dtype::Float32(),
  171. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
  172. 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}),
  173. {}},
  174. Testcase{
  175. {},
  176. TensorValue({1, 2, 3, 1}, dtype::Float32(), {3, 7, 11, 15, 19, 23}),
  177. });
  178. }
  179. TEST_F(NAIVE, INF_NORM_FP16_DIM3) {
  180. Checker<Norm> checker(handle());
  181. Norm::Param param;
  182. using Mode = Norm::Param::Mode;
  183. param.dim = 3;
  184. param.mode = Mode::INF_NORM;
  185. checker.set_param(param).exect(
  186. Testcase{
  187. TensorValue(
  188. {1, 2, 3, 4}, dtype::Float16(),
  189. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
  190. 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}),
  191. {}},
  192. Testcase{
  193. {},
  194. TensorValue({1, 2, 3, 1}, dtype::Float16(), {3, 7, 11, 15, 19, 23}),
  195. });
  196. }
  197. // -inf
  198. TEST_F(NAIVE, NEG_INF_NORM_FP32_DIM3) {
  199. Checker<Norm> checker(handle());
  200. Norm::Param param;
  201. param.mode = Norm::Param::Mode::NEG_INF_NORM;
  202. param.dim = 3;
  203. checker.set_param(param).exect(
  204. Testcase{
  205. TensorValue(
  206. {1, 2, 3, 4}, dtype::Float32(),
  207. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
  208. 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}),
  209. {}},
  210. Testcase{
  211. {},
  212. TensorValue({1, 2, 3, 1}, dtype::Float32(), {0, 4, 8, 12, 16, 20}),
  213. });
  214. }
  215. TEST_F(NAIVE, NEG_INF_NORM_FP16_DIM3) {
  216. Checker<Norm> checker(handle());
  217. Norm::Param param;
  218. param.mode = Norm::Param::Mode::NEG_INF_NORM;
  219. param.dim = 3;
  220. checker.set_param(param).exect(
  221. Testcase{
  222. TensorValue(
  223. {1, 2, 3, 4}, dtype::Float16(),
  224. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
  225. 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}),
  226. {}},
  227. Testcase{
  228. {},
  229. TensorValue({1, 2, 3, 1}, dtype::Float16(), {0, 4, 8, 12, 16, 20}),
  230. });
  231. }
  232. } // namespace test
  233. } // namespace megdnn