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.

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