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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /**
  2. * \file dnn/test/cuda/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 implied.
  10. */
  11. #include "test/naive/rng.h"
  12. #include "megdnn/oprs.h"
  13. #include "test/common/tensor.h"
  14. #include "test/cuda/fixture.h"
  15. namespace megdnn {
  16. namespace test {
  17. namespace {
  18. template <typename T>
  19. void run_gamma(Handle* handle) {
  20. using ctype = typename DTypeTrait<T>::ctype;
  21. auto opr = handle->create_operator<GammaRNG>();
  22. TensorLayout ly{TensorShape{2000000 * 5}, T()};
  23. SyncedTensor<ctype> out(handle, ly);
  24. SyncedTensor<ctype> shape(handle, ly);
  25. SyncedTensor<ctype> scale(handle, ly);
  26. auto shape_ptr = shape.ptr_mutable_host();
  27. auto scale_ptr = scale.ptr_mutable_host();
  28. for (int i = 0; i < 5; ++i) {
  29. for (int j = 0; j < 2000000; ++j) {
  30. shape_ptr[i * 2000000 + j] = 2 * 0.3 * i + 0.3;
  31. scale_ptr[i * 2000000 + j] = i * 0.2 + 0.1;
  32. }
  33. }
  34. opr->exec(shape.tensornd_dev(), scale.tensornd_dev(), out.tensornd_dev(), {});
  35. auto ptr = out.ptr_mutable_host();
  36. for (int i = 0; i < 5; ++i) {
  37. float a = 2 * 0.3 * i + 0.3, b = i * 0.2 + 0.1;
  38. float mean = a * b;
  39. float std = a * (b * b);
  40. auto stat = get_mean_var(ptr + i * 2000000, 2000000, ctype(mean));
  41. ASSERT_LE(std::abs(stat.first - mean), 0.01);
  42. ASSERT_LE(std::abs(stat.second - std), 0.01);
  43. }
  44. }
  45. template <typename T>
  46. void run_poisson(Handle* handle) {
  47. using ctype = typename DTypeTrait<T>::ctype;
  48. auto opr = handle->create_operator<PoissonRNG>();
  49. TensorLayout ly{TensorShape{200000 * 5}, T()};
  50. SyncedTensor<ctype> out(handle, ly);
  51. SyncedTensor<ctype> lam(handle, ly);
  52. auto lam_ptr = lam.ptr_mutable_host();
  53. for (int i = 0; i < 5; ++i) {
  54. for (int j = 0; j < 200000; ++j) {
  55. lam_ptr[i * 200000 + j] = ctype(i + 1);
  56. }
  57. }
  58. opr->exec(lam.tensornd_dev(), out.tensornd_dev(), {});
  59. auto ptr = out.ptr_mutable_host();
  60. for (int i = 0; i < 5; ++i) {
  61. auto stat = get_mean_var(ptr + i * 200000, 200000, ctype(i + 1));
  62. ASSERT_LE(std::abs(stat.first - ctype(i + 1)), 0.01);
  63. ASSERT_LE(std::abs(stat.second - ctype(i + 1)), 0.01);
  64. }
  65. }
  66. template <typename T>
  67. void run_beta(Handle* handle) {
  68. using ctype = typename DTypeTrait<T>::ctype;
  69. auto opr = handle->create_operator<BetaRNG>();
  70. TensorLayout ly{TensorShape{200000 * 5}, T()};
  71. SyncedTensor<ctype> out(handle, ly);
  72. SyncedTensor<ctype> alpha(handle, ly);
  73. SyncedTensor<ctype> beta(handle, ly);
  74. auto alpha_ptr = alpha.ptr_mutable_host();
  75. auto beta_ptr = beta.ptr_mutable_host();
  76. for (int i = 0; i < 5; ++i) {
  77. for (int j = 0; j < 200000; ++j) {
  78. alpha_ptr[i * 200000 + j] = 0.3 * i + 0.1;
  79. beta_ptr[i * 200000 + j] = 2 * i * 0.3 + 0.1;
  80. }
  81. }
  82. opr->exec(alpha.tensornd_dev(), beta.tensornd_dev(), out.tensornd_dev(), {});
  83. auto ptr = out.ptr_mutable_host();
  84. for (int i = 0; i < 5; ++i) {
  85. float a = 0.3 * i + 0.1, b = 2 * i * 0.3 + 0.1;
  86. float mean = a / (a + b);
  87. float std = a * b / ((a + b) * (a + b) * (a + b + 1));
  88. auto stat = get_mean_var(ptr + i * 200000, 200000, ctype(mean));
  89. ASSERT_LE(std::abs(stat.first - mean), 0.01);
  90. ASSERT_LE(std::abs(stat.second - std), 0.01);
  91. }
  92. }
  93. template <typename T>
  94. void run_permutation(Handle* handle) {
  95. using ctype = typename DTypeTrait<T>::ctype;
  96. size_t sample_num = std::min(200000, static_cast<int>(DTypeTrait<T>::max()) - 10);
  97. auto opr = handle->create_operator<PermutationRNG>();
  98. opr->param().dtype = DTypeTrait<T>::enumv;
  99. TensorLayout ly{TensorShape{sample_num}, T()};
  100. Tensor<dt_byte> workspace(
  101. handle, {TensorShape{opr->get_workspace_in_bytes(ly)}, dtype::Byte()});
  102. SyncedTensor<ctype> t(handle, ly);
  103. opr->exec(t.tensornd_dev(), {workspace.ptr(), workspace.layout().total_nr_elems()});
  104. auto ptr = t.ptr_mutable_host();
  105. auto size = t.layout().total_nr_elems();
  106. std::vector<ctype> res(size);
  107. int not_same = 0;
  108. for (size_t i = 0; i < size; ++i) {
  109. if ((ptr[i] - ctype(i)) >= ctype(1))
  110. not_same++;
  111. res[i] = ptr[i];
  112. }
  113. ASSERT_GT(not_same, 5000);
  114. std::sort(res.begin(), res.end());
  115. for (size_t i = 0; i < size; ++i) {
  116. ASSERT_LE(std::abs(res[i] - ctype(i)), 1e-8);
  117. }
  118. }
  119. template <typename T>
  120. void run_shuffle(Handle* handle, bool bwd_flag) {
  121. using ctype = typename DTypeTrait<T>::ctype;
  122. auto run = [&](TensorShape shape) {
  123. auto opr = handle->create_operator<ShuffleRNGForward>();
  124. TensorLayout srclay{shape, T()};
  125. TensorLayout dstlay{shape, T()};
  126. TensorLayout indexlay{TensorShape{shape[0]}, dtype::Int32()};
  127. Tensor<dt_byte> workspace(
  128. handle,
  129. {TensorShape{opr->get_workspace_in_bytes(srclay, dstlay, indexlay)},
  130. dtype::Byte()});
  131. SyncedTensor<ctype> src(handle, srclay);
  132. SyncedTensor<ctype> dst(handle, dstlay);
  133. SyncedTensor<DTypeTrait<dt_int32>::ctype> index(handle, indexlay);
  134. auto sptr = src.ptr_mutable_host();
  135. size_t size = src.layout().total_nr_elems();
  136. for (size_t j = 0; j < size; ++j) {
  137. sptr[j] = j;
  138. }
  139. opr->exec(
  140. src.tensornd_dev(), dst.tensornd_dev(), index.tensornd_dev(),
  141. {workspace.ptr(), workspace.layout().total_nr_elems()});
  142. auto dptr = dst.ptr_mutable_host();
  143. auto iptr = index.ptr_mutable_host();
  144. size_t len = index.layout().total_nr_elems();
  145. size_t step = size / len;
  146. for (size_t i = 0; i < len; ++i) {
  147. for (size_t j = 0; j < step; ++j) {
  148. ASSERT_EQ(dptr[i * step + j], sptr[iptr[i] * step + j]);
  149. }
  150. }
  151. if (bwd_flag) {
  152. for (size_t j = 0; j < size; ++j) {
  153. sptr[j] = 0;
  154. }
  155. auto oprbwd = handle->create_operator<ShuffleRNGBackward>();
  156. oprbwd->exec(
  157. dst.tensornd_dev(), index.tensornd_dev(), src.tensornd_dev(),
  158. {workspace.ptr(), workspace.layout().total_nr_elems()});
  159. auto sptr_bwd = src.ptr_mutable_host();
  160. for (size_t i = 0; i < len; ++i) {
  161. for (size_t j = 0; j < step; ++j) {
  162. ASSERT_EQ(dptr[i * step + j], sptr_bwd[iptr[i] * step + j]);
  163. }
  164. }
  165. }
  166. };
  167. run({10});
  168. run({6, 3});
  169. }
  170. } // anonymous namespace
  171. TEST_F(CUDA, UNIFORM_RNG_F32) {
  172. auto opr = handle_cuda()->create_operator<UniformRNG>();
  173. opr->param().dtype = DTypeTrait<dtype::Float32>::enumv;
  174. SyncedTensor<> t(handle_cuda(), {TensorShape{200000}, dtype::Float32()});
  175. opr->exec(t.tensornd_dev(), {});
  176. assert_uniform_correct(t.ptr_mutable_host(), t.layout().total_nr_elems());
  177. }
  178. TEST_F(CUDA, GAUSSIAN_RNG_F32) {
  179. auto opr = handle_cuda()->create_operator<GaussianRNG>();
  180. opr->param().mean = 0.8;
  181. opr->param().std = 2.3;
  182. opr->param().dtype = DTypeTrait<dtype::Float32>::enumv;
  183. for (size_t size : {1, 200000, 200001}) {
  184. TensorLayout ly{{size}, dtype::Float32()};
  185. Tensor<dt_byte> workspace(
  186. handle_cuda(),
  187. {TensorShape{opr->get_workspace_in_bytes(ly)}, dtype::Byte()});
  188. SyncedTensor<> t(handle_cuda(), ly);
  189. opr->exec(
  190. t.tensornd_dev(),
  191. {workspace.ptr(), workspace.layout().total_nr_elems()});
  192. auto ptr = t.ptr_mutable_host();
  193. ASSERT_LE(std::abs(ptr[0] - 0.8), 2.3);
  194. if (size >= 1000) {
  195. auto stat = get_mean_var(ptr, size, 0.8f);
  196. ASSERT_LE(std::abs(stat.first - 0.8), 5e-3);
  197. ASSERT_LE(std::abs(stat.second - 2.3 * 2.3), 5e-2);
  198. }
  199. }
  200. }
  201. TEST_F(CUDA, GAMMA_RNG_F32) {
  202. run_gamma<dtype::Float32>(handle_cuda());
  203. }
  204. TEST_F(CUDA, GAMMA_RNG_F16) {
  205. run_gamma<dtype::Float16>(handle_cuda());
  206. }
  207. TEST_F(CUDA, POISSON_RNG_F32) {
  208. run_poisson<dtype::Float32>(handle_cuda());
  209. }
  210. TEST_F(CUDA, POISSON_RNG_F16) {
  211. run_poisson<dtype::Float16>(handle_cuda());
  212. }
  213. TEST_F(CUDA, BETA_RNG_F32) {
  214. run_beta<dtype::Float32>(handle_cuda());
  215. }
  216. TEST_F(CUDA, BETA_RNG_F16) {
  217. run_beta<dtype::Float16>(handle_cuda());
  218. }
  219. TEST_F(CUDA, PERMUTATION_RNG_F32) {
  220. run_permutation<dtype::Float32>(handle_cuda());
  221. }
  222. TEST_F(CUDA, PERMUTATION_RNG_INT32) {
  223. run_permutation<dtype::Int32>(handle_cuda());
  224. }
  225. TEST_F(CUDA, PERMUTATION_RNG_INT16) {
  226. run_permutation<dtype::Int16>(handle_cuda());
  227. }
  228. TEST_F(CUDA, SHUFFLE_RNG_F32) {
  229. run_shuffle<dtype::Float32>(handle_cuda(), false);
  230. }
  231. TEST_F(CUDA, SHUFFLE_RNG_INT32) {
  232. run_shuffle<dtype::Int32>(handle_cuda(), false);
  233. }
  234. TEST_F(CUDA, SHUFFLE_RNG_F16) {
  235. run_shuffle<dtype::Float16>(handle_cuda(), false);
  236. }
  237. TEST_F(CUDA, SHUFFLE_RNG_BWD_F32) {
  238. run_shuffle<dtype::Float32>(handle_cuda(), true);
  239. }
  240. TEST_F(CUDA, SHUFFLE_RNG_BWD_INT32) {
  241. run_shuffle<dtype::Int32>(handle_cuda(), true);
  242. }
  243. TEST_F(CUDA, SHUFFLE_RNG_BWD_F16) {
  244. run_shuffle<dtype::Float16>(handle_cuda(), true);
  245. }
  246. } // namespace test
  247. } // namespace megdnn
  248. // vim: syntax=cpp.doxygen

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