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 9.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. } // namespace
  206. TEST_F(NAIVE, UNIFORM_RNG_F32) {
  207. run_uniform<dtype::Float32>(handle());
  208. }
  209. TEST_F(NAIVE, UNIFORM_RNG_F16) {
  210. DNN_INC_FLOAT16(run_uniform<dtype::Float16>(handle()));
  211. }
  212. TEST_F(NAIVE, GAUSSIAN_RNG_F32) {
  213. run_gaussian<dtype::Float32>(handle());
  214. }
  215. TEST_F(NAIVE, GAUSSIAN_RNG_F16) {
  216. DNN_INC_FLOAT16(run_gaussian<dtype::Float16>(handle()));
  217. }
  218. TEST_F(NAIVE, GAMMA_RNG_F32) {
  219. run_gamma<dtype::Float32>(handle());
  220. }
  221. TEST_F(NAIVE, GAMMA_RNG_F16) {
  222. DNN_INC_FLOAT16(run_gamma<dtype::Float16>(handle()));
  223. }
  224. TEST_F(NAIVE, POISSON_RNG_F32) {
  225. run_poisson<dtype::Float32>(handle());
  226. }
  227. TEST_F(NAIVE, POISSON_RNG_F16) {
  228. DNN_INC_FLOAT16(run_poisson<dtype::Float16>(handle()));
  229. }
  230. TEST_F(NAIVE, BETA_RNG_F32) {
  231. run_beta<dtype::Float32>(handle());
  232. }
  233. TEST_F(NAIVE, BETA_RNG_F16) {
  234. DNN_INC_FLOAT16(run_beta<dtype::Float16>(handle()));
  235. }
  236. TEST_F(NAIVE, PERMUTATION_RNG_F32) {
  237. run_permutation<dtype::Float32>(handle());
  238. }
  239. TEST_F(NAIVE, PERMUTATION_RNG_INT32) {
  240. run_permutation<dtype::Int32>(handle());
  241. }
  242. TEST_F(NAIVE, PERMUTATION_RNG_INT16) {
  243. run_permutation<dtype::Int16>(handle());
  244. }
  245. TEST_F(NAIVE, SHUFFLE_RNG_FWD_F32) {
  246. run_shuffle<dtype::Float32>(handle(), false);
  247. }
  248. TEST_F(NAIVE, SHUFFLE_RNG_FWD_INT32) {
  249. run_shuffle<dtype::Int32>(handle(), false);
  250. }
  251. TEST_F(NAIVE, SHUFFLE_RNG_FWD_F16) {
  252. run_shuffle<dtype::Float16>(handle(), false);
  253. }
  254. TEST_F(NAIVE, SHUFFLE_RNG_BWD_F32) {
  255. run_shuffle<dtype::Float32>(handle(), true);
  256. }
  257. TEST_F(NAIVE, SHUFFLE_RNG_BWD_INT32) {
  258. run_shuffle<dtype::Int32>(handle(), true);
  259. }
  260. TEST_F(NAIVE, SHUFFLE_RNG_BWD_F16) {
  261. run_shuffle<dtype::Float16>(handle(), true);
  262. }
  263. } // namespace test
  264. } // namespace megdnn
  265. // vim: syntax=cpp.doxygen

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台