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.

rng.cpp 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /**
  2. * \file dnn/test/naive/rng.cpp
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or
  10. * implied.
  11. */
  12. #include "test/naive/rng.h"
  13. #include "megdnn.h"
  14. #include "test/common/tensor.h"
  15. #include "test/naive/fixture.h"
  16. namespace megdnn {
  17. namespace test {
  18. template <typename ctype>
  19. void assert_uniform_correct(const ctype* src, size_t size) {
  20. for (size_t i = 0; i < size; ++i) {
  21. ASSERT_GT(src[i], ctype(0));
  22. ASSERT_LE(src[i], ctype(1));
  23. }
  24. auto stat = get_mean_var(src, size, ctype(0.5));
  25. ASSERT_LE(std::abs(stat.first - 0.5), 1e-3);
  26. ASSERT_LE(std::abs(stat.second - 1.0 / 12), 1e-3);
  27. }
  28. namespace {
  29. template <typename dtype>
  30. void run_uniform(Handle* handle) {
  31. auto opr = handle->create_operator<UniformRNG>();
  32. opr->param().dtype = DTypeTrait<dtype>::enumv;
  33. Tensor<typename DTypeTrait<dtype>::ctype> t(handle, {TensorShape{200000}, dtype()});
  34. opr->exec(t.tensornd(), {});
  35. assert_uniform_correct(t.ptr(), t.layout().total_nr_elems());
  36. }
  37. template <typename dtype>
  38. void run_gaussian(Handle* handle) {
  39. using ctype = typename DTypeTrait<dtype>::ctype;
  40. auto opr = handle->create_operator<GaussianRNG>();
  41. opr->param().mean = 0.8;
  42. opr->param().std = 2.3;
  43. opr->param().dtype = DTypeTrait<dtype>::enumv;
  44. Tensor<ctype> t(handle, {TensorShape{200001}, dtype()});
  45. opr->exec(t.tensornd(), {});
  46. auto ptr = t.ptr();
  47. auto size = t.layout().total_nr_elems();
  48. for (size_t i = 0; i < size; ++i) {
  49. ASSERT_LE(std::abs(ptr[i] - 0.8), ctype(15));
  50. }
  51. auto stat = get_mean_var(ptr, size, ctype(0.8));
  52. ASSERT_LE(std::abs(stat.first - 0.8), 5e-3);
  53. ASSERT_LE(std::abs(stat.second - 2.3 * 2.3), 5e-2);
  54. }
  55. template <typename dtype>
  56. void run_gamma(Handle* handle) {
  57. using ctype = typename DTypeTrait<dtype>::ctype;
  58. auto opr = handle->create_operator<GammaRNG>();
  59. TensorLayout ly{TensorShape{2000000 * 5}, dtype()};
  60. Tensor<ctype> out(handle, ly);
  61. Tensor<ctype> shape(handle, ly);
  62. Tensor<ctype> scale(handle, ly);
  63. auto shape_ptr = shape.ptr();
  64. auto scale_ptr = scale.ptr();
  65. for (int i = 0; i < 5; ++i) {
  66. for (int j = 0; j < 2000000; ++j) {
  67. shape_ptr[i * 2000000 + j] = 2 * 0.3 * i + 0.5;
  68. scale_ptr[i * 2000000 + j] = i * 0.2 + 0.1;
  69. }
  70. }
  71. opr->exec(shape.tensornd(), scale.tensornd(), out.tensornd(), {});
  72. auto ptr = out.ptr();
  73. for (int i = 0; i < 5; ++i) {
  74. float a = 2 * 0.3 * i + 0.5, b = i * 0.2 + 0.1;
  75. float mean = a * b;
  76. float std = a * (b * b);
  77. auto stat = get_mean_var(ptr + i * 2000000, 2000000, ctype(mean));
  78. ASSERT_LE(std::abs(stat.first - mean), 0.01);
  79. ASSERT_LE(std::abs(stat.second - std), 0.01);
  80. }
  81. }
  82. template <typename dtype>
  83. void run_poisson(Handle* handle) {
  84. using ctype = typename DTypeTrait<dtype>::ctype;
  85. auto opr = handle->create_operator<PoissonRNG>();
  86. TensorLayout ly{TensorShape{200000 * 5}, dtype()};
  87. Tensor<ctype> out(handle, ly);
  88. Tensor<ctype> lam(handle, ly);
  89. auto lam_ptr = lam.ptr();
  90. for (int i = 0; i < 5; ++i) {
  91. for (int j = 0; j < 200000; ++j) {
  92. lam_ptr[i * 200000 + j] = ctype(i + 1);
  93. }
  94. }
  95. opr->exec(lam.tensornd(), out.tensornd(), {});
  96. auto ptr = out.ptr();
  97. for (int i = 0; i < 5; ++i) {
  98. auto stat = get_mean_var(ptr + i * 200000, 200000, ctype(i + 1));
  99. ASSERT_LE(std::abs(stat.first - ctype(i + 1)), 0.01);
  100. ASSERT_LE(std::abs(stat.second - ctype(i + 1)), 0.01);
  101. }
  102. }
  103. template <typename dtype>
  104. void run_beta(Handle* handle) {
  105. using ctype = typename DTypeTrait<dtype>::ctype;
  106. auto opr = handle->create_operator<BetaRNG>();
  107. TensorLayout ly{TensorShape{200000 * 5}, dtype()};
  108. Tensor<ctype> out(handle, ly);
  109. Tensor<ctype> alpha(handle, ly);
  110. Tensor<ctype> beta(handle, ly);
  111. auto alpha_ptr = alpha.ptr();
  112. auto beta_ptr = beta.ptr();
  113. for (int i = 0; i < 5; ++i) {
  114. for (int j = 0; j < 200000; ++j) {
  115. alpha_ptr[i * 200000 + j] = 0.3 * i + 0.1;
  116. beta_ptr[i * 200000 + j] = 2 * i * 0.3 + 0.1;
  117. }
  118. }
  119. opr->exec(alpha.tensornd(), beta.tensornd(), out.tensornd(), {});
  120. auto ptr = out.ptr();
  121. for (int i = 0; i < 5; ++i) {
  122. float a = 0.3 * i + 0.1, b = 2 * i * 0.3 + 0.1;
  123. float mean = a / (a + b);
  124. float std = a * b / ((a + b) * (a + b) * (a + b + 1));
  125. auto stat = get_mean_var(ptr + i * 200000, 200000, ctype(mean));
  126. ASSERT_LE(std::abs(stat.first - mean), 0.01);
  127. ASSERT_LE(std::abs(stat.second - std), 0.01);
  128. }
  129. }
  130. template <typename dtype>
  131. void run_permutation(Handle* handle) {
  132. using ctype = typename DTypeTrait<dtype>::ctype;
  133. size_t sample_num =
  134. std::min(200000, static_cast<int>(DTypeTrait<dtype>::max()) - 10);
  135. auto opr = handle->create_operator<PermutationRNG>();
  136. opr->param().dtype = DTypeTrait<dtype>::enumv;
  137. TensorLayout ly{TensorShape{sample_num}, dtype()};
  138. Tensor<ctype> t(handle, ly);
  139. opr->exec(t.tensornd(), {});
  140. auto ptr = t.ptr();
  141. auto size = t.layout().total_nr_elems();
  142. std::vector<ctype> res(size);
  143. int not_same = 0;
  144. for (size_t i = 0; i < size; ++i) {
  145. if ((ptr[i] - ctype(i)) >= 1)
  146. not_same++;
  147. res[i] = ptr[i];
  148. }
  149. ASSERT_GT(not_same, 5000);
  150. std::sort(res.begin(), res.end());
  151. for (size_t i = 0; i < size; ++i) {
  152. ASSERT_LE(std::abs(res[i] - ctype(i)), 1e-8);
  153. }
  154. }
  155. template <typename T>
  156. void run_shuffle(Handle* handle, bool bwd_flag) {
  157. using ctype = typename DTypeTrait<T>::ctype;
  158. auto run = [&](TensorShape shape) {
  159. auto opr = handle->create_operator<ShuffleRNGForward>();
  160. TensorLayout srclay{shape, T()};
  161. TensorLayout dstlay{shape, T()};
  162. TensorLayout indexlay{TensorShape{shape[0]}, dtype::Int32()};
  163. Tensor<dt_byte> workspace(
  164. handle,
  165. {TensorShape{opr->get_workspace_in_bytes(srclay, dstlay, indexlay)},
  166. dtype::Byte()});
  167. Tensor<ctype> src(handle, srclay);
  168. Tensor<ctype> dst(handle, dstlay);
  169. Tensor<DTypeTrait<dt_int32>::ctype> index(handle, indexlay);
  170. auto sptr = src.ptr();
  171. size_t size = src.layout().total_nr_elems();
  172. for (size_t j = 0; j < size; ++j) {
  173. sptr[j] = j;
  174. }
  175. opr->exec(
  176. src.tensornd(), dst.tensornd(), index.tensornd(),
  177. {workspace.ptr(), workspace.layout().total_nr_elems()});
  178. auto dptr = dst.ptr();
  179. auto iptr = index.ptr();
  180. size_t len = index.layout().total_nr_elems();
  181. size_t step = size / len;
  182. for (size_t i = 0; i < len; ++i) {
  183. for (size_t j = 0; j < step; ++j) {
  184. ASSERT_EQ(dptr[i * step + j], sptr[iptr[i] * step + j]);
  185. }
  186. }
  187. if (bwd_flag) {
  188. for (size_t j = 0; j < size; ++j) {
  189. sptr[j] = 0;
  190. }
  191. auto oprbwd = handle->create_operator<ShuffleRNGBackward>();
  192. oprbwd->exec(
  193. dst.tensornd(), index.tensornd(), src.tensornd(),
  194. {workspace.ptr(), workspace.layout().total_nr_elems()});
  195. for (size_t i = 0; i < len; ++i) {
  196. for (size_t j = 0; j < step; ++j) {
  197. ASSERT_EQ(dptr[i * step + j], sptr[iptr[i] * step + j]);
  198. }
  199. }
  200. }
  201. };
  202. run({10});
  203. run({6, 3});
  204. }
  205. template <typename T>
  206. void run_dropout(Handle* handle) {
  207. using ctype = typename DTypeTrait<T>::ctype;
  208. auto run = [&](TensorShape shape, float drop_prob) {
  209. auto fwd = handle->create_operator<DropoutForward>();
  210. auto bwd = handle->create_operator<DropoutBackward>();
  211. fwd->param().drop_prob = drop_prob;
  212. bwd->param().drop_prob = drop_prob;
  213. double scale = 1.0 / (1.0 - drop_prob);
  214. TensorLayout inp_lay{shape, T()};
  215. TensorLayout oup_lay{shape, T()};
  216. TensorLayout mask_lay{{fwd->get_mask_size_in_bytes(inp_lay)}, dtype::Byte()};
  217. TensorLayout doup_lay{shape, T()};
  218. TensorLayout dinp_lay{shape, T()};
  219. TensorLayout fwd_ws_lay{
  220. {fwd->get_workspace_in_bytes(inp_lay, oup_lay, mask_lay)},
  221. dtype::Byte()};
  222. TensorLayout bwd_ws_lay{
  223. {bwd->get_workspace_in_bytes(doup_lay, mask_lay, dinp_lay)},
  224. dtype::Byte()};
  225. Tensor<ctype> inp(handle, inp_lay);
  226. Tensor<ctype> oup(handle, oup_lay);
  227. Tensor<DTypeTrait<dt_byte>::ctype> mask(handle, mask_lay);
  228. Tensor<ctype> doup(handle, doup_lay);
  229. Tensor<ctype> dinp(handle, dinp_lay);
  230. Tensor<DTypeTrait<dt_byte>::ctype> fwd_ws(handle, fwd_ws_lay);
  231. Tensor<DTypeTrait<dt_byte>::ctype> bwd_ws(handle, bwd_ws_lay);
  232. for (size_t i = 0; i < inp.layout().total_nr_elems(); ++i) {
  233. inp.ptr()[i] = 1;
  234. doup.ptr()[i] = 1;
  235. }
  236. fwd->exec(
  237. inp.tensornd(), oup.tensornd(), mask.tensornd(),
  238. {fwd_ws.ptr(), fwd_ws.layout().total_nr_elems()});
  239. size_t droped_cnt = 0;
  240. for (size_t i = 0; i < inp.layout().total_nr_elems(); ++i) {
  241. ASSERT_TRUE(oup.ptr()[i] == 0 || oup.ptr()[i] == static_cast<ctype>(scale));
  242. if (oup.ptr()[i] == 0) {
  243. droped_cnt++;
  244. }
  245. }
  246. float real_drop = droped_cnt * 1.0 / inp.layout().total_nr_elems();
  247. ASSERT_LT(abs(drop_prob - real_drop), 1e-2);
  248. bwd->exec(
  249. doup.tensornd(), mask.tensornd(), dinp.tensornd(),
  250. {bwd_ws.ptr(), bwd_ws.layout().total_nr_elems()});
  251. for (size_t i = 0; i < inp.layout().total_nr_elems(); ++i) {
  252. ASSERT_TRUE(oup.ptr()[i] == dinp.ptr()[i]);
  253. }
  254. };
  255. run({32, 32, 32, 32}, 0.2);
  256. run({100000}, 0.3);
  257. }
  258. } // namespace
  259. TEST_F(NAIVE, UNIFORM_RNG_F32) {
  260. run_uniform<dtype::Float32>(handle());
  261. }
  262. TEST_F(NAIVE, UNIFORM_RNG_F16) {
  263. DNN_INC_FLOAT16(run_uniform<dtype::Float16>(handle()));
  264. }
  265. TEST_F(NAIVE, GAUSSIAN_RNG_F32) {
  266. run_gaussian<dtype::Float32>(handle());
  267. }
  268. TEST_F(NAIVE, GAUSSIAN_RNG_F16) {
  269. DNN_INC_FLOAT16(run_gaussian<dtype::Float16>(handle()));
  270. }
  271. TEST_F(NAIVE, GAMMA_RNG_F32) {
  272. run_gamma<dtype::Float32>(handle());
  273. }
  274. TEST_F(NAIVE, GAMMA_RNG_F16) {
  275. DNN_INC_FLOAT16(run_gamma<dtype::Float16>(handle()));
  276. }
  277. TEST_F(NAIVE, POISSON_RNG_F32) {
  278. run_poisson<dtype::Float32>(handle());
  279. }
  280. TEST_F(NAIVE, POISSON_RNG_F16) {
  281. DNN_INC_FLOAT16(run_poisson<dtype::Float16>(handle()));
  282. }
  283. TEST_F(NAIVE, BETA_RNG_F32) {
  284. run_beta<dtype::Float32>(handle());
  285. }
  286. TEST_F(NAIVE, BETA_RNG_F16) {
  287. DNN_INC_FLOAT16(run_beta<dtype::Float16>(handle()));
  288. }
  289. TEST_F(NAIVE, PERMUTATION_RNG_F32) {
  290. run_permutation<dtype::Float32>(handle());
  291. }
  292. TEST_F(NAIVE, PERMUTATION_RNG_INT32) {
  293. run_permutation<dtype::Int32>(handle());
  294. }
  295. TEST_F(NAIVE, PERMUTATION_RNG_INT16) {
  296. run_permutation<dtype::Int16>(handle());
  297. }
  298. TEST_F(NAIVE, SHUFFLE_RNG_FWD_F32) {
  299. run_shuffle<dtype::Float32>(handle(), false);
  300. }
  301. TEST_F(NAIVE, SHUFFLE_RNG_FWD_INT32) {
  302. run_shuffle<dtype::Int32>(handle(), false);
  303. }
  304. TEST_F(NAIVE, SHUFFLE_RNG_FWD_F16) {
  305. run_shuffle<dtype::Float16>(handle(), false);
  306. }
  307. TEST_F(NAIVE, SHUFFLE_RNG_BWD_F32) {
  308. run_shuffle<dtype::Float32>(handle(), true);
  309. }
  310. TEST_F(NAIVE, SHUFFLE_RNG_BWD_INT32) {
  311. run_shuffle<dtype::Int32>(handle(), true);
  312. }
  313. TEST_F(NAIVE, SHUFFLE_RNG_BWD_F16) {
  314. run_shuffle<dtype::Float16>(handle(), true);
  315. }
  316. TEST_F(NAIVE, DROPOUT_F32) {
  317. run_dropout<dtype::Float32>(handle());
  318. }
  319. TEST_F(NAIVE, DROPOUT_F16) {
  320. run_dropout<dtype::Float16>(handle());
  321. }
  322. } // namespace test
  323. } // namespace megdnn
  324. // vim: syntax=cpp.doxygen