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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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(
  34. handle, {TensorShape{200000}, dtype()});
  35. opr->exec(t.tensornd(), {});
  36. assert_uniform_correct(t.ptr(), t.layout().total_nr_elems());
  37. }
  38. template<typename dtype>
  39. void run_gaussian(Handle *handle) {
  40. using ctype = typename DTypeTrait<dtype>::ctype;
  41. auto opr = handle->create_operator<GaussianRNG>();
  42. opr->param().mean = 0.8;
  43. opr->param().std = 2.3;
  44. opr->param().dtype = DTypeTrait<dtype>::enumv;
  45. Tensor<ctype> t(handle, {TensorShape{200001}, dtype()});
  46. opr->exec(t.tensornd(), {});
  47. auto ptr = t.ptr();
  48. auto size = t.layout().total_nr_elems();
  49. for (size_t i = 0; i < size; ++ i) {
  50. ASSERT_LE(std::abs(ptr[i] - 0.8), ctype(15));
  51. }
  52. auto stat = get_mean_var(ptr, size, ctype(0.8));
  53. ASSERT_LE(std::abs(stat.first - 0.8), 5e-3);
  54. ASSERT_LE(std::abs(stat.second - 2.3 * 2.3), 5e-2);
  55. }
  56. template<typename dtype>
  57. void run_gamma(Handle* handle){
  58. using ctype = typename DTypeTrait<dtype>::ctype;
  59. auto opr = handle->create_operator<GammaRNG>();
  60. TensorLayout ly{TensorShape{2000000*5}, dtype()};
  61. Tensor<ctype> out(handle, ly);
  62. Tensor<ctype> shape(handle, ly);
  63. Tensor<ctype> scale(handle, ly);
  64. auto shape_ptr = shape.ptr();
  65. auto scale_ptr = scale.ptr();
  66. for (int i = 0; i < 5; ++i) {
  67. for (int j = 0; j < 2000000; ++j) {
  68. shape_ptr[i * 2000000 + j] = 2 * 0.3 * i + 0.5;
  69. scale_ptr[i * 2000000 + j] = i * 0.2 + 0.1;
  70. }
  71. }
  72. opr->exec(shape.tensornd(), scale.tensornd(), out.tensornd(), {});
  73. auto ptr = out.ptr();
  74. for(int i = 0; i < 5 ; ++i){
  75. float a = 2 * 0.3 * i + 0.5, b = i * 0.2 + 0.1;
  76. float mean = a * b;
  77. float std = a * (b * b) ;
  78. auto stat = get_mean_var(ptr + i * 2000000, 2000000, ctype(mean));
  79. ASSERT_LE(std::abs(stat.first - mean), 0.01);
  80. ASSERT_LE(std::abs(stat.second - std), 0.01);
  81. }
  82. }
  83. template<typename dtype>
  84. void run_poisson(Handle* handle){
  85. using ctype = typename DTypeTrait<dtype>::ctype;
  86. auto opr = handle->create_operator<PoissonRNG>();
  87. TensorLayout ly{TensorShape{200000*5}, dtype()};
  88. Tensor<ctype> out(handle, ly);
  89. Tensor<ctype> lam(handle, ly);
  90. auto lam_ptr = lam.ptr();
  91. for(int i = 0; i < 5; ++i){
  92. for(int j = 0; j <200000; ++j){
  93. lam_ptr[i*200000 + j] = ctype(i + 1);
  94. }
  95. }
  96. opr->exec(lam.tensornd(), out.tensornd(), {});
  97. auto ptr = out.ptr();
  98. for(int i = 0; i < 5 ; ++i){
  99. auto stat = get_mean_var(ptr + i*200000, 200000, ctype(i + 1));
  100. ASSERT_LE(std::abs(stat.first - ctype(i + 1)), 0.01);
  101. ASSERT_LE(std::abs(stat.second - ctype(i + 1)), 0.01);
  102. }
  103. }
  104. template<typename dtype>
  105. void run_beta(Handle* handle){
  106. using ctype = typename DTypeTrait<dtype>::ctype;
  107. auto opr = handle->create_operator<BetaRNG>();
  108. TensorLayout ly{TensorShape{200000*5}, dtype()};
  109. Tensor<ctype> out(handle, ly);
  110. Tensor<ctype> alpha(handle, ly);
  111. Tensor<ctype> beta(handle, ly);
  112. auto alpha_ptr = alpha.ptr();
  113. auto beta_ptr = beta.ptr();
  114. for (int i = 0; i < 5; ++i) {
  115. for (int j = 0; j < 200000; ++j) {
  116. alpha_ptr[i * 200000 + j] = 0.3 * i + 0.1;
  117. beta_ptr[i * 200000 + j] = 2 * i * 0.3 + 0.1;
  118. }
  119. }
  120. opr->exec(alpha.tensornd(),beta.tensornd(), out.tensornd(), {});
  121. auto ptr = out.ptr();
  122. for(int i = 0; i < 5 ; ++i){
  123. float a = 0.3 * i + 0.1, b = 2 * i * 0.3 + 0.1;
  124. float mean = a / (a + b);
  125. float std = a * b / ((a + b) * (a + b) * (a + b + 1));
  126. auto stat = get_mean_var(ptr + i * 200000, 200000, ctype(mean));
  127. ASSERT_LE(std::abs(stat.first - mean), 0.01);
  128. ASSERT_LE(std::abs(stat.second - std), 0.01);
  129. }
  130. }
  131. template<typename dtype>
  132. void run_permutation(Handle* handle){
  133. using ctype = typename DTypeTrait<dtype>::ctype;
  134. size_t sample_num = std::min(200000,
  135. static_cast<int>(DTypeTrait<dtype>::max()) - 10);
  136. auto opr = handle->create_operator<PermutationRNG>();
  137. opr->param().dtype = DTypeTrait<dtype>::enumv;
  138. TensorLayout ly{TensorShape{sample_num}, dtype()};
  139. Tensor<ctype> t(handle, ly);
  140. opr->exec(t.tensornd(), {});
  141. auto ptr = t.ptr();
  142. auto size = t.layout().total_nr_elems();
  143. std::vector<ctype> res(size);
  144. int not_same = 0;
  145. for(size_t i = 0; i < size; ++i){
  146. if ((ptr[i] - ctype(i)) >= 1 ) 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, {TensorShape{opr->get_workspace_in_bytes(srclay, dstlay,
  165. 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(src.tensornd(), dst.tensornd(), index.tensornd(),
  176. {workspace.ptr(), workspace.layout().total_nr_elems()});
  177. auto dptr = dst.ptr();
  178. auto iptr = index.ptr();
  179. size_t len = index.layout().total_nr_elems();
  180. size_t step = size / len;
  181. for (size_t i = 0; i < len; ++i) {
  182. for (size_t j = 0; j < step; ++j) {
  183. ASSERT_EQ(dptr[i * step + j], sptr[iptr[i] * step + j]);
  184. }
  185. }
  186. if (bwd_flag) {
  187. for (size_t j = 0; j < size; ++j) {
  188. sptr[j] = 0;
  189. }
  190. auto oprbwd = handle->create_operator<ShuffleRNGBackward>();
  191. oprbwd->exec(
  192. dst.tensornd(), index.tensornd(), src.tensornd(),
  193. {workspace.ptr(), workspace.layout().total_nr_elems()});
  194. for (size_t i = 0; i < len; ++i) {
  195. for (size_t j = 0; j < step; ++j) {
  196. ASSERT_EQ(dptr[i * step + j], sptr[iptr[i] * step + j]);
  197. }
  198. }
  199. }
  200. };
  201. run({10});
  202. run({6, 3});
  203. }
  204. } // namespace
  205. TEST_F(NAIVE, UNIFORM_RNG_F32) {
  206. run_uniform<dtype::Float32>(handle());
  207. }
  208. TEST_F(NAIVE, UNIFORM_RNG_F16) {
  209. DNN_INC_FLOAT16(run_uniform<dtype::Float16>(handle()));
  210. }
  211. TEST_F(NAIVE, GAUSSIAN_RNG_F32) {
  212. run_gaussian<dtype::Float32>(handle());
  213. }
  214. TEST_F(NAIVE, GAUSSIAN_RNG_F16) {
  215. DNN_INC_FLOAT16(run_gaussian<dtype::Float16>(handle()));
  216. }
  217. TEST_F(NAIVE, GAMMA_RNG_F32) {
  218. run_gamma<dtype::Float32>(handle());
  219. }
  220. TEST_F(NAIVE, GAMMA_RNG_F16) {
  221. DNN_INC_FLOAT16(run_gamma<dtype::Float16>(handle()));
  222. }
  223. TEST_F(NAIVE, POISSON_RNG_F32) {
  224. run_poisson<dtype::Float32>(handle());
  225. }
  226. TEST_F(NAIVE, POISSON_RNG_F16) {
  227. DNN_INC_FLOAT16(run_poisson<dtype::Float16>(handle()));
  228. }
  229. TEST_F(NAIVE, BETA_RNG_F32) {
  230. run_beta<dtype::Float32>(handle());
  231. }
  232. TEST_F(NAIVE, BETA_RNG_F16) {
  233. DNN_INC_FLOAT16(run_beta<dtype::Float16>(handle()));
  234. }
  235. TEST_F(NAIVE, PERMUTATION_RNG_F32) {
  236. run_permutation<dtype::Float32>(handle());
  237. }
  238. TEST_F(NAIVE, PERMUTATION_RNG_INT32) {
  239. run_permutation<dtype::Int32>(handle());
  240. }
  241. TEST_F(NAIVE, PERMUTATION_RNG_INT16) {
  242. run_permutation<dtype::Int16>(handle());
  243. }
  244. TEST_F(NAIVE, SHUFFLE_RNG_FWD_F32) {
  245. run_shuffle<dtype::Float32>(handle(), false);
  246. }
  247. TEST_F(NAIVE, SHUFFLE_RNG_FWD_INT32) {
  248. run_shuffle<dtype::Int32>(handle(), false);
  249. }
  250. TEST_F(NAIVE, SHUFFLE_RNG_FWD_F16) {
  251. run_shuffle<dtype::Float16>(handle(), false);
  252. }
  253. TEST_F(NAIVE, SHUFFLE_RNG_BWD_F32) {
  254. run_shuffle<dtype::Float32>(handle(), true);
  255. }
  256. TEST_F(NAIVE, SHUFFLE_RNG_BWD_INT32) {
  257. run_shuffle<dtype::Int32>(handle(), true);
  258. }
  259. TEST_F(NAIVE, SHUFFLE_RNG_BWD_F16) {
  260. run_shuffle<dtype::Float16>(handle(), true);
  261. }
  262. } // namespace test
  263. } // namespace megdnn
  264. // vim: syntax=cpp.doxygen

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