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.

test_effect_random.py 14 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ==============================================================================
  15. import pytest
  16. import numpy as np
  17. import mindspore.nn as nn
  18. import mindspore.ops.operations as P
  19. import mindspore.nn.probability.distribution as msd
  20. from mindspore import context, Tensor
  21. from mindspore.ops import composite as C
  22. from mindspore.common import dtype as mstype
  23. from mindspore import dtype
  24. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  25. class Sampling(nn.Cell):
  26. """
  27. Test class: sample of Normal distribution.
  28. """
  29. def __init__(self, shape, seed=0):
  30. super(Sampling, self).__init__()
  31. self.n1 = msd.Normal(0, 1, seed=seed, dtype=dtype.float32)
  32. self.shape = shape
  33. def construct(self, mean=None, sd=None):
  34. s1 = self.n1.sample(self.shape, mean, sd)
  35. s2 = self.n1.sample(self.shape, mean, sd)
  36. s3 = self.n1.sample(self.shape, mean, sd)
  37. return s1, s2, s3
  38. @pytest.mark.level0
  39. @pytest.mark.platform_arm_ascend_training
  40. @pytest.mark.platform_x86_ascend_training
  41. @pytest.mark.env_onecard
  42. def test_sample_graph():
  43. shape = (2, 3)
  44. seed = 0
  45. samp = Sampling(shape, seed=seed)
  46. sample1, sample2, sample3 = samp()
  47. assert ((sample1 != sample2).any() and (sample1 != sample3).any() and (sample2 != sample3).any()), \
  48. "The results should be different!"
  49. class CompositeNormalNet(nn.Cell):
  50. def __init__(self, shape=None, seed=0):
  51. super(CompositeNormalNet, self).__init__()
  52. self.shape = shape
  53. self.seed = seed
  54. def construct(self, mean, stddev):
  55. s1 = C.normal(self.shape, mean, stddev, self.seed)
  56. s2 = C.normal(self.shape, mean, stddev, self.seed)
  57. s3 = C.normal(self.shape, mean, stddev, self.seed)
  58. return s1, s2, s3
  59. @pytest.mark.level0
  60. @pytest.mark.platform_arm_ascend_training
  61. @pytest.mark.platform_x86_ascend_training
  62. @pytest.mark.env_onecard
  63. def test_composite_normal():
  64. shape = (3, 2, 4)
  65. mean = Tensor(0.0, mstype.float32)
  66. stddev = Tensor(1.0, mstype.float32)
  67. net = CompositeNormalNet(shape)
  68. s1, s2, s3 = net(mean, stddev)
  69. assert ((s1 != s2).any() and (s1 != s3).any() and (s2 != s3).any()), \
  70. "The results should be different!"
  71. class CompositeLaplaceNet(nn.Cell):
  72. def __init__(self, shape=None, seed=0):
  73. super(CompositeLaplaceNet, self).__init__()
  74. self.shape = shape
  75. self.seed = seed
  76. def construct(self, mean, lambda_param):
  77. s1 = C.laplace(self.shape, mean, lambda_param, self.seed)
  78. s2 = C.laplace(self.shape, mean, lambda_param, self.seed)
  79. s3 = C.laplace(self.shape, mean, lambda_param, self.seed)
  80. return s1, s2, s3
  81. @pytest.mark.level0
  82. @pytest.mark.platform_arm_ascend_training
  83. @pytest.mark.platform_x86_ascend_training
  84. @pytest.mark.env_onecard
  85. def test_composite_laplace():
  86. shape = (3, 2, 4)
  87. mean = Tensor(1.0, mstype.float32)
  88. lambda_param = Tensor(1.0, mstype.float32)
  89. net = CompositeLaplaceNet(shape)
  90. s1, s2, s3 = net(mean, lambda_param)
  91. assert ((s1 != s2).any() and (s1 != s3).any() and (s2 != s3).any()), \
  92. "The results should be different!"
  93. class CompositeGammaNet(nn.Cell):
  94. def __init__(self, shape=None, seed=0):
  95. super(CompositeGammaNet, self).__init__()
  96. self.shape = shape
  97. self.seed = seed
  98. def construct(self, alpha, beta):
  99. s1 = C.gamma(self.shape, alpha, beta, self.seed)
  100. s2 = C.gamma(self.shape, alpha, beta, self.seed)
  101. s3 = C.gamma(self.shape, alpha, beta, self.seed)
  102. return s1, s2, s3
  103. @pytest.mark.level0
  104. @pytest.mark.platform_arm_ascend_training
  105. @pytest.mark.platform_x86_ascend_training
  106. @pytest.mark.env_onecard
  107. def test_composite_gamma():
  108. shape = (3, 2, 4)
  109. alpha = Tensor(1.0, mstype.float32)
  110. beta = Tensor(1.0, mstype.float32)
  111. net = CompositeGammaNet(shape)
  112. s1, s2, s3 = net(alpha, beta)
  113. assert ((s1 != s2).any() and (s1 != s3).any() and (s2 != s3).any()), \
  114. "The results should be different!"
  115. class CompositePoissonNet(nn.Cell):
  116. def __init__(self, shape=None, seed=0):
  117. super(CompositePoissonNet, self).__init__()
  118. self.shape = shape
  119. self.seed = seed
  120. def construct(self, mean):
  121. s1 = C.poisson(self.shape, mean, self.seed)
  122. s2 = C.poisson(self.shape, mean, self.seed)
  123. s3 = C.poisson(self.shape, mean, self.seed)
  124. return s1, s2, s3
  125. @pytest.mark.level0
  126. @pytest.mark.platform_arm_ascend_training
  127. @pytest.mark.platform_x86_ascend_training
  128. @pytest.mark.env_onecard
  129. def test_composite_poisson():
  130. shape = (3, 2, 4)
  131. mean = Tensor(2.0, mstype.float32)
  132. net = CompositePoissonNet(shape)
  133. s1, s2, s3 = net(mean)
  134. assert ((s1 != s2).any() and (s1 != s3).any() and (s2 != s3).any()), \
  135. "The results should be different!"
  136. class CompositeUniformNet(nn.Cell):
  137. def __init__(self, shape=None, seed=0):
  138. super(CompositeUniformNet, self).__init__()
  139. self.shape = shape
  140. self.seed = seed
  141. def construct(self, a, b):
  142. s1 = C.uniform(self.shape, a, b, self.seed)
  143. s2 = C.uniform(self.shape, a, b, self.seed)
  144. s3 = C.uniform(self.shape, a, b, self.seed)
  145. return s1, s2, s3
  146. @pytest.mark.level0
  147. @pytest.mark.platform_arm_ascend_training
  148. @pytest.mark.platform_x86_ascend_training
  149. @pytest.mark.env_onecard
  150. def test_composite_uniform():
  151. shape = (3, 2, 4)
  152. a = Tensor(0.0, mstype.float32)
  153. b = Tensor(1.0, mstype.float32)
  154. net = CompositeUniformNet(shape)
  155. s1, s2, s3 = net(a, b)
  156. assert ((s1 != s2).any() and (s1 != s3).any() and (s2 != s3).any()), \
  157. "The results should be different!"
  158. class StandardNormalNet(nn.Cell):
  159. def __init__(self, shape, seed=0, seed2=0):
  160. super(StandardNormalNet, self).__init__()
  161. self.shape = shape
  162. self.seed = seed
  163. self.seed2 = seed2
  164. self.standard_normal = P.StandardNormal(seed, seed2)
  165. def construct(self):
  166. s1 = self.standard_normal(self.shape)
  167. s2 = self.standard_normal(self.shape)
  168. s3 = self.standard_normal(self.shape)
  169. return s1, s2, s3
  170. @pytest.mark.level0
  171. @pytest.mark.platform_arm_ascend_training
  172. @pytest.mark.platform_x86_ascend_training
  173. @pytest.mark.env_onecard
  174. def test_standard_normal():
  175. shape = (4, 16)
  176. net = StandardNormalNet(shape)
  177. s1, s2, s3 = net()
  178. assert ((s1 != s2).any() and (s1 != s3).any() and (s2 != s3).any()), \
  179. "The results should be different!"
  180. class StandardLaplaceNet(nn.Cell):
  181. def __init__(self, shape, seed=0, seed2=0):
  182. super(StandardLaplaceNet, self).__init__()
  183. self.shape = shape
  184. self.seed = seed
  185. self.seed2 = seed2
  186. self.standard_laplace = P.StandardLaplace(seed, seed2)
  187. def construct(self):
  188. s1 = self.standard_laplace(self.shape)
  189. s2 = self.standard_laplace(self.shape)
  190. s3 = self.standard_laplace(self.shape)
  191. return s1, s2, s3
  192. @pytest.mark.level0
  193. @pytest.mark.platform_arm_ascend_training
  194. @pytest.mark.platform_x86_ascend_training
  195. @pytest.mark.env_onecard
  196. def test_standard_laplace():
  197. shape = (4, 16)
  198. net = StandardLaplaceNet(shape)
  199. s1, s2, s3 = net()
  200. assert ((s1 != s2).any() and (s1 != s3).any() and (s2 != s3).any()), \
  201. "The results should be different!"
  202. class GammaNet(nn.Cell):
  203. def __init__(self, shape, alpha, beta, seed=0, seed2=0):
  204. super(GammaNet, self).__init__()
  205. self.shape = shape
  206. self.alpha = alpha
  207. self.beta = beta
  208. self.seed = seed
  209. self.seed2 = seed2
  210. self.gamma = P.Gamma(seed, seed2)
  211. def construct(self):
  212. s1 = self.gamma(self.shape, self.alpha, self.beta)
  213. s2 = self.gamma(self.shape, self.alpha, self.beta)
  214. s3 = self.gamma(self.shape, self.alpha, self.beta)
  215. return s1, s2, s3
  216. @pytest.mark.level0
  217. @pytest.mark.platform_arm_ascend_training
  218. @pytest.mark.platform_x86_ascend_training
  219. @pytest.mark.env_onecard
  220. def test_gamma():
  221. shape = (4, 16)
  222. alpha = Tensor(1.0, mstype.float32)
  223. beta = Tensor(1.0, mstype.float32)
  224. net = GammaNet(shape, alpha, beta)
  225. s1, s2, s3 = net()
  226. assert ((s1 != s2).any() and (s1 != s3).any() and (s2 != s3).any()), \
  227. "The results should be different!"
  228. class PoissonNet(nn.Cell):
  229. def __init__(self, shape, seed=0, seed2=0):
  230. super(PoissonNet, self).__init__()
  231. self.shape = shape
  232. self.seed = seed
  233. self.seed2 = seed2
  234. self.poisson = P.Poisson(seed, seed2)
  235. def construct(self, mean):
  236. s1 = self.poisson(self.shape, mean)
  237. s2 = self.poisson(self.shape, mean)
  238. s3 = self.poisson(self.shape, mean)
  239. return s1, s2, s3
  240. @pytest.mark.level0
  241. @pytest.mark.platform_arm_ascend_training
  242. @pytest.mark.platform_x86_ascend_training
  243. @pytest.mark.env_onecard
  244. def test_poisson():
  245. shape = (4, 16)
  246. mean = Tensor(5.0, mstype.float32)
  247. net = PoissonNet(shape=shape)
  248. s1, s2, s3 = net(mean)
  249. assert ((s1 != s2).any() and (s1 != s3).any() and (s2 != s3).any()), \
  250. "The results should be different!"
  251. class UniformIntNet(nn.Cell):
  252. def __init__(self, shape, seed=0, seed2=0):
  253. super(UniformIntNet, self).__init__()
  254. self.shape = shape
  255. self.seed = seed
  256. self.seed2 = seed2
  257. self.uniform_int = P.UniformInt(seed, seed2)
  258. def construct(self, minval, maxval):
  259. s1 = self.uniform_int(self.shape, minval, maxval)
  260. s2 = self.uniform_int(self.shape, minval, maxval)
  261. s3 = self.uniform_int(self.shape, minval, maxval)
  262. return s1, s2, s3
  263. @pytest.mark.level0
  264. @pytest.mark.platform_arm_ascend_training
  265. @pytest.mark.platform_x86_ascend_training
  266. @pytest.mark.env_onecard
  267. def test_uniform_int():
  268. shape = (4, 16)
  269. minval = Tensor(1, mstype.int32)
  270. maxval = Tensor(5, mstype.int32)
  271. net = UniformIntNet(shape)
  272. s1, s2, s3 = net(minval, maxval)
  273. assert ((s1 != s2).any() and (s1 != s3).any() and (s2 != s3).any()), \
  274. "The results should be different!"
  275. class UniformRealNet(nn.Cell):
  276. def __init__(self, shape, seed=0, seed2=0):
  277. super(UniformRealNet, self).__init__()
  278. self.shape = shape
  279. self.seed = seed
  280. self.seed2 = seed2
  281. self.uniform_real = P.UniformReal(seed, seed2)
  282. def construct(self):
  283. s1 = self.uniform_real(self.shape)
  284. s2 = self.uniform_real(self.shape)
  285. s3 = self.uniform_real(self.shape)
  286. return s1, s2, s3
  287. @pytest.mark.level0
  288. @pytest.mark.platform_arm_ascend_training
  289. @pytest.mark.platform_x86_ascend_training
  290. @pytest.mark.env_onecard
  291. def test_uniform_real():
  292. shape = (4, 16)
  293. net = UniformRealNet(shape)
  294. s1, s2, s3 = net()
  295. assert ((s1 != s2).any() and (s1 != s3).any() and (s2 != s3).any()), \
  296. "The results should be different!"
  297. class DropoutGenMaskNet(nn.Cell):
  298. def __init__(self, shape):
  299. super(DropoutGenMaskNet, self).__init__()
  300. self.shape = shape
  301. self.dropout_gen_mask = P.DropoutGenMask(Seed0=0, Seed1=0)
  302. def construct(self, keep_prob):
  303. s1 = self.dropout_gen_mask(self.shape, keep_prob)
  304. s2 = self.dropout_gen_mask(self.shape, keep_prob)
  305. s3 = self.dropout_gen_mask(self.shape, keep_prob)
  306. return s1, s2, s3
  307. @pytest.mark.level0
  308. @pytest.mark.platform_arm_ascend_training
  309. @pytest.mark.platform_x86_ascend_training
  310. @pytest.mark.env_onecard
  311. def test_dropout_gen_mask():
  312. shape = (2, 4, 5)
  313. keep_prob = Tensor(0.5, mstype.float32)
  314. net = DropoutGenMaskNet(shape)
  315. s1, s2, s3 = net(keep_prob)
  316. assert ((s1 != s2).any() and (s1 != s3).any() and (s2 != s3).any()), \
  317. "The results should be different!"
  318. class RandomChoiceWithMaskNet(nn.Cell):
  319. def __init__(self):
  320. super(RandomChoiceWithMaskNet, self).__init__()
  321. self.rnd_choice_mask = P.RandomChoiceWithMask(count=4, seed=0)
  322. def construct(self, x):
  323. index1, _ = self.rnd_choice_mask(x)
  324. index2, _ = self.rnd_choice_mask(x)
  325. index3, _ = self.rnd_choice_mask(x)
  326. return index1, index2, index3
  327. @pytest.mark.level0
  328. @pytest.mark.platform_arm_ascend_training
  329. @pytest.mark.platform_x86_ascend_training
  330. @pytest.mark.env_onecard
  331. def test_random_choice_with_mask():
  332. net = RandomChoiceWithMaskNet()
  333. x = Tensor(np.array([[1, 0, 1, 0], [0, 0, 0, 1], [1, 1, 1, 1], [0, 0, 0, 1]]).astype(np.bool))
  334. index1, index2, index3 = net(x)
  335. assert ((index1 != index2).any() and (index1 != index3).any() and (index2 != index3).any()), \
  336. "The results should be different!"
  337. class RandomCategoricalNet(nn.Cell):
  338. def __init__(self, num_sample):
  339. super(RandomCategoricalNet, self).__init__()
  340. self.random_categorical = P.RandomCategorical(mstype.int64)
  341. self.num_sample = num_sample
  342. def construct(self, logits, seed=0):
  343. s1 = self.random_categorical(logits, self.num_sample, seed)
  344. s2 = self.random_categorical(logits, self.num_sample, seed)
  345. s3 = self.random_categorical(logits, self.num_sample, seed)
  346. return s1, s2, s3
  347. @pytest.mark.level0
  348. @pytest.mark.platform_arm_ascend_training
  349. @pytest.mark.platform_x86_ascend_training
  350. @pytest.mark.env_onecard
  351. def test_random_categorical():
  352. num_sample = 8
  353. net = RandomCategoricalNet(num_sample)
  354. x = Tensor(np.random.random((10, 5)).astype(np.float32))
  355. # Outputs may be the same, only basic functions are verified here.
  356. net(x)