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.h 5.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**
  2. * \file dnn/test/common/rng.h
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2020 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. #pragma once
  12. #include "megdnn/dtype.h"
  13. #include "test/common/utils.h"
  14. #include <random>
  15. #include <set>
  16. namespace megdnn {
  17. namespace test {
  18. class RNG {
  19. protected:
  20. class RNGxorshf;
  21. public:
  22. virtual void gen(const TensorND& tensor) = 0;
  23. virtual ~RNG() = default;
  24. };
  25. class Float16PeriodicalRNG : public RNG {
  26. public:
  27. Float16PeriodicalRNG();
  28. Float16PeriodicalRNG(size_t range);
  29. void gen(const TensorND& tensor) override;
  30. dt_float16 get_single_val();
  31. private:
  32. void gen_all_valid_float16();
  33. size_t m_offset;
  34. std::vector<dt_float16> m_sequence;
  35. };
  36. class IIDRNG : public RNG {
  37. public:
  38. void gen(const TensorND& tensor) override;
  39. virtual dt_float32 gen_single_val() = 0;
  40. virtual bool output_is_float() { return true; }
  41. protected:
  42. virtual bool has_fast_float32();
  43. virtual void fill_fast_float32(dt_float32* dest, size_t size);
  44. };
  45. class NormalRNG final : public IIDRNG {
  46. public:
  47. NormalRNG(dt_float32 mean = 0.0f, dt_float32 stddev = 1.0f)
  48. : m_dist(mean, stddev) {}
  49. void fill_fast_float32(dt_float32* dest, size_t size) override;
  50. protected:
  51. dt_float32 gen_single_val() override;
  52. private:
  53. std::normal_distribution<dt_float32> m_dist;
  54. bool has_fast_float32() override;
  55. };
  56. class ConstValue final : public IIDRNG {
  57. public:
  58. ConstValue(dt_float32 value = 0.0f) : value_(value) {}
  59. void fill_fast_float32(dt_float32* dest, size_t size) override;
  60. protected:
  61. dt_float32 gen_single_val() override { return value_; }
  62. private:
  63. dt_float32 value_;
  64. bool has_fast_float32() override { return true; }
  65. };
  66. class UniformIntRNG : public IIDRNG {
  67. public:
  68. UniformIntRNG(dt_int32 a, dt_int32 b) : m_dist(a, b) {}
  69. dt_float32 gen_single_val() override;
  70. bool output_is_float() override { return false; }
  71. protected:
  72. std::uniform_int_distribution<dt_int32> m_dist;
  73. };
  74. //! range must be positive; each value would be negated with prob 0.5
  75. class UniformIntNonZeroRNG : public UniformIntRNG {
  76. std::uniform_int_distribution<dt_int32> m_dist_flip{0, 1};
  77. public:
  78. UniformIntNonZeroRNG(int a, int b) : UniformIntRNG(a, b) {
  79. megdnn_assert(a > 0 && b > a);
  80. }
  81. dt_float32 gen_single_val() override;
  82. };
  83. class UniformFloatRNG : public IIDRNG {
  84. public:
  85. UniformFloatRNG(dt_float32 a, dt_float32 b) : m_dist(a, b) {}
  86. dt_float32 gen_single_val() override;
  87. protected:
  88. std::uniform_real_distribution<dt_float32> m_dist;
  89. bool has_fast_float32() override;
  90. void fill_fast_float32(dt_float32* dest, size_t size) override;
  91. };
  92. //! range must be positive; each value would be negated with prob 0.5
  93. class UniformFloatNonZeroRNG : public UniformFloatRNG {
  94. std::uniform_int_distribution<dt_int32> m_dist_flip{0, 1};
  95. public:
  96. UniformFloatNonZeroRNG(float a, float b) : UniformFloatRNG(a, b) {
  97. megdnn_assert(a > 0 && b > a);
  98. }
  99. dt_float32 gen_single_val() override;
  100. void fill_fast_float32(dt_float32* dest, size_t size) override;
  101. };
  102. class UniformFloatWithZeroRNG final : public UniformFloatRNG {
  103. public:
  104. UniformFloatWithZeroRNG(dt_float32 a, dt_float32 b,
  105. float zero_val_proportion)
  106. : UniformFloatRNG(a, b) {
  107. if (zero_val_proportion < 0.f)
  108. zero_val_proportion_ = 0.f;
  109. else if (zero_val_proportion > 1.f)
  110. zero_val_proportion_ = 1.f;
  111. else
  112. zero_val_proportion_ = zero_val_proportion;
  113. }
  114. private:
  115. float zero_val_proportion_;
  116. void fill_fast_float32(dt_float32* dest, size_t size) override;
  117. };
  118. class BernoulliRNG final : public IIDRNG {
  119. public:
  120. BernoulliRNG(dt_float32 probability_);
  121. dt_float32 gen_single_val() override;
  122. private:
  123. dt_float32 m_probability;
  124. std::uniform_real_distribution<dt_float32> m_dist;
  125. };
  126. /**
  127. * \brief RNG without replacement, so that no two values in the tensor are
  128. * equal.
  129. *
  130. * Each value is generated repeatedly by IIDRNG, until the newly-generated value
  131. * differs from any previous value.
  132. */
  133. class NoReplacementRNG final : public RNG {
  134. private:
  135. IIDRNG* m_iid_rng;
  136. public:
  137. NoReplacementRNG(IIDRNG* iid_rng) : m_iid_rng(iid_rng) {}
  138. void gen(const TensorND& tensor) override;
  139. };
  140. //! generate a batch of matrices that are likely to have a small condition num
  141. class InvertibleMatrixRNG final : public RNG {
  142. std::unique_ptr<RNGxorshf> m_rng;
  143. public:
  144. InvertibleMatrixRNG();
  145. ~InvertibleMatrixRNG() noexcept;
  146. void gen(const TensorND& tensor) override;
  147. private:
  148. template <typename ctype>
  149. void do_gen(ctype* ptr, size_t batch, size_t n);
  150. };
  151. //! generate a continuous number of delta, start from value
  152. class ConsecutiveRNG final : public IIDRNG {
  153. public:
  154. ConsecutiveRNG(dt_float32 value = 0.0f, dt_float32 delta = 1.0f)
  155. : value_(value), delta_(delta) {}
  156. void fill_fast_float32(dt_float32* dest, size_t size) override;
  157. protected:
  158. dt_float32 gen_single_val() override {
  159. auto res = value_;
  160. value_ += delta_;
  161. return res;
  162. }
  163. private:
  164. dt_float32 value_, delta_;
  165. bool has_fast_float32() override { return true; }
  166. };
  167. } // namespace test
  168. } // namespace megdnn
  169. // vim: syntax=cpp.doxygen

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

Contributors (1)