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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 implied.
  10. */
  11. #include "megdnn.h"
  12. #include "test/naive/fixture.h"
  13. #include "test/naive/rng.h"
  14. #include "test/common/tensor.h"
  15. namespace megdnn {
  16. namespace test {
  17. template<typename ctype>
  18. void assert_uniform_correct(const ctype *src, size_t size) {
  19. for (size_t i = 0; i < size; ++ i) {
  20. ASSERT_GT(src[i], ctype(0));
  21. ASSERT_LE(src[i], ctype(1));
  22. }
  23. auto stat = get_mean_var(src, size, ctype(0.5));
  24. ASSERT_LE(std::abs(stat.first - 0.5), 1e-3);
  25. ASSERT_LE(std::abs(stat.second - 1.0 / 12), 1e-3);
  26. }
  27. namespace {
  28. template<typename dtype>
  29. void run_uniform(Handle *handle) {
  30. auto opr = handle->create_operator<UniformRNG>();
  31. opr->param().dtype = DTypeTrait<dtype>::enumv;
  32. Tensor<typename DTypeTrait<dtype>::ctype> t(
  33. 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 = std::min(200000,
  134. 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 ) not_same++;
  146. res[i] = ptr[i];
  147. }
  148. ASSERT_GT(not_same, 5000);
  149. std::sort(res.begin(),res.end());
  150. for(size_t i = 0; i < size; ++i){
  151. ASSERT_LE(std::abs(res[i] - ctype(i)), 1e-8);
  152. }
  153. }
  154. }
  155. TEST_F(NAIVE, UNIFORM_RNG_F32) {
  156. run_uniform<dtype::Float32>(handle());
  157. }
  158. TEST_F(NAIVE, UNIFORM_RNG_F16) {
  159. DNN_INC_FLOAT16(run_uniform<dtype::Float16>(handle()));
  160. }
  161. TEST_F(NAIVE, GAUSSIAN_RNG_F32) {
  162. run_gaussian<dtype::Float32>(handle());
  163. }
  164. TEST_F(NAIVE, GAUSSIAN_RNG_F16) {
  165. DNN_INC_FLOAT16(run_gaussian<dtype::Float16>(handle()));
  166. }
  167. TEST_F(NAIVE, GAMMA_RNG_F32) {
  168. run_gamma<dtype::Float32>(handle());
  169. }
  170. TEST_F(NAIVE, GAMMA_RNG_F16) {
  171. DNN_INC_FLOAT16(run_gamma<dtype::Float16>(handle()));
  172. }
  173. TEST_F(NAIVE, POISSON_RNG_F32) {
  174. run_poisson<dtype::Float32>(handle());
  175. }
  176. TEST_F(NAIVE, POISSON_RNG_F16) {
  177. DNN_INC_FLOAT16(run_poisson<dtype::Float16>(handle()));
  178. }
  179. TEST_F(NAIVE, BETA_RNG_F32) {
  180. run_beta<dtype::Float32>(handle());
  181. }
  182. TEST_F(NAIVE, BETA_RNG_F16) {
  183. DNN_INC_FLOAT16(run_beta<dtype::Float16>(handle()));
  184. }
  185. TEST_F(NAIVE, PERMUTATION_RNG_F32) {
  186. run_permutation<dtype::Float32>(handle());
  187. }
  188. TEST_F(NAIVE, PERMUTATION_RNG_INT32) {
  189. run_permutation<dtype::Int32>(handle());
  190. }
  191. TEST_F(NAIVE, PERMUTATION_RNG_INT16) {
  192. run_permutation<dtype::Int16>(handle());
  193. }
  194. } // namespace test
  195. } // namespace megdnn
  196. // vim: syntax=cpp.doxygen

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