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_ops.py 15 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 os
  16. import tempfile
  17. import pytest
  18. import scipy
  19. import numpy as np
  20. import mindspore.nn as nn
  21. import mindspore.ops.operations as P
  22. from mindspore import context, Tensor
  23. from mindspore.common import dtype as mstype
  24. from mindspore.common.parameter import Parameter
  25. from mindspore.train.summary.summary_record import SummaryRecord
  26. from tests.summary_utils import SummaryReader
  27. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  28. class AssignAddNet(nn.Cell):
  29. def __init__(self, para):
  30. super(AssignAddNet, self).__init__()
  31. self.para = Parameter(para, name="para")
  32. self.assign_add = P.AssignAdd()
  33. def construct(self, value):
  34. self.assign_add(self.para, value)
  35. return self.para
  36. @pytest.mark.level0
  37. @pytest.mark.platform_arm_ascend_training
  38. @pytest.mark.platform_x86_ascend_training
  39. @pytest.mark.env_onecard
  40. def test_assign_add():
  41. x = Tensor(1, dtype=mstype.int32)
  42. y = Tensor(2, dtype=mstype.int32)
  43. expect = Tensor(3, dtype=mstype.int32)
  44. net = AssignAddNet(x)
  45. out = net(y)
  46. np.testing.assert_array_equal(out.asnumpy(), expect.asnumpy())
  47. class AssignSubNet(nn.Cell):
  48. def __init__(self, para):
  49. super(AssignSubNet, self).__init__()
  50. self.para = Parameter(para, name="para")
  51. self.assign_sub = P.AssignSub()
  52. def construct(self, value):
  53. self.assign_sub(self.para, value)
  54. return self.para
  55. @pytest.mark.level0
  56. @pytest.mark.platform_arm_ascend_training
  57. @pytest.mark.platform_x86_ascend_training
  58. @pytest.mark.env_onecard
  59. def test_assign_sub():
  60. x = Tensor(3, dtype=mstype.int32)
  61. y = Tensor(2, dtype=mstype.int32)
  62. expect = Tensor(1, dtype=mstype.int32)
  63. net = AssignSubNet(x)
  64. out = net(y)
  65. np.testing.assert_array_equal(out.asnumpy(), expect.asnumpy())
  66. class ScatterAddNet(nn.Cell):
  67. def __init__(self, input_x):
  68. super(ScatterAddNet, self).__init__()
  69. self.input_x = Parameter(input_x, name="para")
  70. self.scatter_add = P.ScatterAdd()
  71. def construct(self, indices, updates):
  72. self.scatter_add(self.input_x, indices, updates)
  73. return self.input_x
  74. @pytest.mark.level0
  75. @pytest.mark.platform_arm_ascend_training
  76. @pytest.mark.platform_x86_ascend_training
  77. @pytest.mark.env_onecard
  78. def test_scatter_add():
  79. input_x = Tensor(np.array([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]), mstype.float32)
  80. indices = Tensor(np.array([[0, 1], [1, 1]]), mstype.int32)
  81. updates = Tensor(np.ones([2, 2, 3]), mstype.float32)
  82. expect = Tensor(np.array([[1.0, 1.0, 1.0], [3.0, 3.0, 3.0]]), mstype.float32)
  83. net = ScatterAddNet(input_x)
  84. out = net(indices, updates)
  85. np.testing.assert_almost_equal(out.asnumpy(), expect.asnumpy())
  86. class ScatterSubNet(nn.Cell):
  87. def __init__(self, input_x):
  88. super(ScatterSubNet, self).__init__()
  89. self.input_x = Parameter(input_x, name="para")
  90. self.scatter_sub = P.ScatterSub()
  91. def construct(self, indices, updates):
  92. self.scatter_sub(self.input_x, indices, updates)
  93. return self.input_x
  94. @pytest.mark.level0
  95. @pytest.mark.platform_arm_ascend_training
  96. @pytest.mark.platform_x86_ascend_training
  97. @pytest.mark.env_onecard
  98. def test_scatter_sub():
  99. input_x = Tensor(np.array([[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]]), mstype.float32)
  100. indices = Tensor(np.array([[0, 1]]), mstype.int32)
  101. updates = Tensor(np.array([[[1.0, 1.0, 1.0], [2.0, 2.0, 2.0]]]), mstype.float32)
  102. expect = Tensor(np.array([[-1.0, -1.0, -1.0], [-1.0, -1.0, -1.0]]), mstype.float32)
  103. net = ScatterSubNet(input_x)
  104. out = net(indices, updates)
  105. np.testing.assert_almost_equal(out.asnumpy(), expect.asnumpy())
  106. class ScatterMulNet(nn.Cell):
  107. def __init__(self, input_x):
  108. super(ScatterMulNet, self).__init__()
  109. self.input_x = Parameter(input_x, name="para")
  110. self.scatter_mul = P.ScatterMul()
  111. def construct(self, indices, updates):
  112. self.scatter_mul(self.input_x, indices, updates)
  113. return self.input_x
  114. @pytest.mark.level0
  115. @pytest.mark.platform_arm_ascend_training
  116. @pytest.mark.platform_x86_ascend_training
  117. @pytest.mark.env_onecard
  118. def test_scatter_mul():
  119. input_x = Tensor(np.array([[1.0, 1.0, 1.0], [2.0, 2.0, 2.0]]), mstype.float32)
  120. indices = Tensor(np.array([[0, 1]]), mstype.int32)
  121. updates = Tensor(np.array([[[2.0, 2.0, 2.0], [2.0, 2.0, 2.0]]]), mstype.float32)
  122. expect = Tensor(np.array([[2.0, 2.0, 2.0], [4.0, 4.0, 4.0]]), mstype.float32)
  123. net = ScatterMulNet(input_x)
  124. out = net(indices, updates)
  125. np.testing.assert_almost_equal(out.asnumpy(), expect.asnumpy())
  126. class ScatterDivNet(nn.Cell):
  127. def __init__(self, input_x):
  128. super(ScatterDivNet, self).__init__()
  129. self.input_x = Parameter(input_x, name="para")
  130. self.scatter_div = P.ScatterDiv()
  131. def construct(self, indices, updates):
  132. self.scatter_div(self.input_x, indices, updates)
  133. return self.input_x
  134. @pytest.mark.level0
  135. @pytest.mark.platform_arm_ascend_training
  136. @pytest.mark.platform_x86_ascend_training
  137. @pytest.mark.env_onecard
  138. def test_scatter_div():
  139. input_x = Tensor(np.array([[6.0, 6.0, 6.0], [2.0, 2.0, 2.0]]), mstype.float32)
  140. indices = Tensor(np.array([[0, 1]]), mstype.int32)
  141. updates = Tensor(np.array([[[2.0, 2.0, 2.0], [2.0, 2.0, 2.0]]]), mstype.float32)
  142. expect = Tensor(np.array([[3.0, 3.0, 3.0], [1.0, 1.0, 1.0]]), mstype.float32)
  143. net = ScatterDivNet(input_x)
  144. out = net(indices, updates)
  145. np.testing.assert_almost_equal(out.asnumpy(), expect.asnumpy())
  146. class ScatterMaxNet(nn.Cell):
  147. def __init__(self, input_x):
  148. super(ScatterMaxNet, self).__init__()
  149. self.input_x = Parameter(input_x, name="para")
  150. self.scatter_max = P.ScatterMax()
  151. def construct(self, indices, updates):
  152. self.scatter_max(self.input_x, indices, updates)
  153. return self.input_x
  154. @pytest.mark.level0
  155. @pytest.mark.platform_arm_ascend_training
  156. @pytest.mark.platform_x86_ascend_training
  157. @pytest.mark.env_onecard
  158. def test_scatter_max():
  159. input_x = Tensor(np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]), mstype.float32)
  160. indices = Tensor(np.array([[0, 0], [1, 1]]), mstype.int32)
  161. updates = Tensor(np.ones([2, 2, 3]) * 88, mstype.float32)
  162. expect = Tensor(np.array([[88.0, 88.0, 88.0], [88.0, 88.0, 88.0]]), mstype.float32)
  163. net = ScatterMaxNet(input_x)
  164. out = net(indices, updates)
  165. np.testing.assert_almost_equal(out.asnumpy(), expect.asnumpy())
  166. class ScatterMinNet(nn.Cell):
  167. def __init__(self, input_x):
  168. super(ScatterMinNet, self).__init__()
  169. self.input_x = Parameter(input_x, name="para")
  170. self.scatter_min = P.ScatterMin()
  171. def construct(self, indices, updates):
  172. self.scatter_min(self.input_x, indices, updates)
  173. return self.input_x
  174. @pytest.mark.level0
  175. @pytest.mark.platform_arm_ascend_training
  176. @pytest.mark.platform_x86_ascend_training
  177. @pytest.mark.env_onecard
  178. def test_scatter_min():
  179. input_x = Tensor(np.array([[0.0, 1.0, 2.0], [0.0, 0.0, 0.0]]), mstype.float32)
  180. indices = Tensor(np.array([[0, 0], [1, 1]]), mstype.int32)
  181. updates = Tensor(np.ones([2, 2, 3]), mstype.float32)
  182. expect = Tensor(np.array([[0.0, 1.0, 1.0], [0.0, 0.0, 0.0]]), mstype.float32)
  183. net = ScatterMinNet(input_x)
  184. out = net(indices, updates)
  185. np.testing.assert_almost_equal(out.asnumpy(), expect.asnumpy())
  186. class ScatterUpdateNet(nn.Cell):
  187. def __init__(self, input_x):
  188. super(ScatterUpdateNet, self).__init__()
  189. self.input_x = Parameter(input_x, name="para")
  190. self.scatter_update = P.ScatterUpdate()
  191. def construct(self, indices, updates):
  192. self.scatter_update(self.input_x, indices, updates)
  193. return self.input_x
  194. @pytest.mark.level0
  195. @pytest.mark.platform_arm_ascend_training
  196. @pytest.mark.platform_x86_ascend_training
  197. @pytest.mark.env_onecard
  198. def test_scatter_update():
  199. input_x = Tensor(np.array([[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]]), mstype.float32)
  200. indices = Tensor(np.array([[0, 0], [1, 1]]), mstype.int32)
  201. updates = Tensor(np.array([[[1.0, 2.2, 1.0], [2.0, 1.2, 1.0]], [[2.0, 2.2, 1.0], [3.0, 1.2, 1.0]]]), mstype.float32)
  202. expect = Tensor(np.array([[2.0, 1.2, 1.0], [3.0, 1.2, 1.0]]), mstype.float32)
  203. net = ScatterUpdateNet(input_x)
  204. out = net(indices, updates)
  205. np.testing.assert_almost_equal(out.asnumpy(), expect.asnumpy())
  206. class ScatterNdAddNet(nn.Cell):
  207. def __init__(self, input_x):
  208. super(ScatterNdAddNet, self).__init__()
  209. self.input_x = Parameter(input_x, name="para")
  210. self.scatter_nd_add = P.ScatterNdAdd()
  211. def construct(self, indices, updates):
  212. self.scatter_nd_add(self.input_x, indices, updates)
  213. return self.input_x
  214. @pytest.mark.level0
  215. @pytest.mark.platform_arm_ascend_training
  216. @pytest.mark.platform_x86_ascend_training
  217. @pytest.mark.env_onecard
  218. def test_scatter_nd_add():
  219. input_x = Tensor(np.array([1, 2, 3, 4, 5, 6, 7, 8]), mstype.float32)
  220. indices = Tensor(np.array([[2], [4], [1], [7]]), mstype.int32)
  221. updates = Tensor(np.array([6, 7, 8, 9]), mstype.float32)
  222. expect = Tensor(np.array([1, 10, 9, 4, 12, 6, 7, 17]), mstype.float32)
  223. net = ScatterNdAddNet(input_x)
  224. out = net(indices, updates)
  225. np.testing.assert_almost_equal(out.asnumpy(), expect.asnumpy())
  226. class ScatterNdSubNet(nn.Cell):
  227. def __init__(self, input_x):
  228. super(ScatterNdSubNet, self).__init__()
  229. self.input_x = Parameter(input_x, name="para")
  230. self.scatter_nd_sub = P.ScatterNdSub()
  231. def construct(self, indices, updates):
  232. self.scatter_nd_sub(self.input_x, indices, updates)
  233. return self.input_x
  234. @pytest.mark.level0
  235. @pytest.mark.platform_arm_ascend_training
  236. @pytest.mark.platform_x86_ascend_training
  237. @pytest.mark.env_onecard
  238. def test_scatter_nd_sub():
  239. input_x = Tensor(np.array([1, 2, 3, 4, 5, 6, 7, 8]), mstype.float32)
  240. indices = Tensor(np.array([[2], [4], [1], [7]]), mstype.int32)
  241. updates = Tensor(np.array([6, 7, 8, 9]), mstype.float32)
  242. expect = Tensor(np.array([1, -6, -3, 4, -2, 6, 7, -1]), mstype.float32)
  243. net = ScatterNdSubNet(input_x)
  244. out = net(indices, updates)
  245. np.testing.assert_almost_equal(out.asnumpy(), expect.asnumpy())
  246. class ScatterNdUpdateNet(nn.Cell):
  247. def __init__(self, input_x):
  248. super(ScatterNdUpdateNet, self).__init__()
  249. self.input_x = Parameter(input_x, name="para")
  250. self.scatter_nd_update = P.ScatterNdUpdate()
  251. def construct(self, indices, updates):
  252. self.scatter_nd_update(self.input_x, indices, updates)
  253. return self.input_x
  254. @pytest.mark.level0
  255. @pytest.mark.platform_arm_ascend_training
  256. @pytest.mark.platform_x86_ascend_training
  257. @pytest.mark.env_onecard
  258. def test_scatter_nd_update():
  259. input_x = Tensor(np.array([[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]]), mstype.float32)
  260. indices = Tensor(np.array([[0, 0], [1, 1]]), mstype.int32)
  261. updates = Tensor(np.array([1.0, 2.2]), mstype.float32)
  262. expect = Tensor(np.array([[1., 0.3, 3.6], [0.4, 2.2, -3.2]]), mstype.float32)
  263. net = ScatterNdUpdateNet(input_x)
  264. out = net(indices, updates)
  265. np.testing.assert_almost_equal(out.asnumpy(), expect.asnumpy())
  266. class ScatterNonAliasingAddNet(nn.Cell):
  267. def __init__(self, input_x):
  268. super(ScatterNonAliasingAddNet, self).__init__()
  269. self.input_x = Parameter(input_x, name="para")
  270. self.scatter_non_aliasing_add = P.ScatterNonAliasingAdd()
  271. def construct(self, indices, updates):
  272. out = self.scatter_non_aliasing_add(self.input_x, indices, updates)
  273. return out
  274. @pytest.mark.level0
  275. @pytest.mark.platform_arm_ascend_training
  276. @pytest.mark.platform_x86_ascend_training
  277. @pytest.mark.env_onecard
  278. def test_scatter_non_aliasing_add():
  279. input_x = Tensor(np.array([1, 2, 3, 4, 5, 6, 7, 8]), mstype.float32)
  280. indices = Tensor(np.array([[2], [4], [1], [7]]), mstype.int32)
  281. updates = Tensor(np.array([6, 7, 8, 9]), mstype.float32)
  282. expect = Tensor(np.array([1.0, 10.0, 9.0, 4.0, 12.0, 6.0, 7.0, 17.0]), mstype.float32)
  283. net = ScatterNonAliasingAddNet(input_x)
  284. out = net(indices, updates)
  285. np.testing.assert_almost_equal(out.asnumpy(), expect.asnumpy())
  286. class SummaryNet(nn.Cell):
  287. def __init__(self):
  288. super().__init__()
  289. self.scalar_summary = P.ScalarSummary()
  290. self.image_summary = P.ImageSummary()
  291. self.tensor_summary = P.TensorSummary()
  292. self.histogram_summary = P.HistogramSummary()
  293. def construct(self, image_tensor):
  294. self.image_summary("image", image_tensor)
  295. self.tensor_summary("tensor", image_tensor)
  296. self.histogram_summary("histogram", image_tensor)
  297. scalar = image_tensor[0][0][0][0]
  298. self.scalar_summary("scalar", scalar)
  299. return scalar
  300. def train_summary_record(test_writer, steps):
  301. """Train and record summary."""
  302. net = SummaryNet()
  303. out_me_dict = {}
  304. for i in range(0, steps):
  305. image_tensor = Tensor(np.array([[[[i]]]]).astype(np.float32))
  306. out_put = net(image_tensor)
  307. test_writer.record(i)
  308. out_me_dict[i] = out_put.asnumpy()
  309. return out_me_dict
  310. @pytest.mark.level0
  311. @pytest.mark.platform_arm_ascend_training
  312. @pytest.mark.platform_x86_ascend_training
  313. @pytest.mark.env_onecard
  314. def test_summary():
  315. with tempfile.TemporaryDirectory() as tmp_dir:
  316. steps = 2
  317. with SummaryRecord(tmp_dir) as test_writer:
  318. train_summary_record(test_writer, steps=steps)
  319. file_name = os.path.realpath(test_writer.full_file_name)
  320. with SummaryReader(file_name) as summary_writer:
  321. for _ in range(steps):
  322. event = summary_writer.read_event()
  323. tags = set(value.tag for value in event.summary.value)
  324. assert tags == {'tensor', 'histogram', 'scalar', 'image'}
  325. @pytest.mark.level0
  326. @pytest.mark.platform_arm_ascend_training
  327. @pytest.mark.platform_x86_ascend_training
  328. @pytest.mark.env_onecard
  329. def test_igamma():
  330. class IGammaTest(nn.Cell):
  331. def __init__(self):
  332. super().__init__()
  333. self.igamma = nn.IGamma()
  334. def construct(self, x, a):
  335. return self.igamma(a=a, x=x)
  336. x = 4.22
  337. a = 2.29
  338. net = IGammaTest()
  339. out = net(Tensor(x, mstype.float32), Tensor(a, mstype.float32))
  340. expect = scipy.special.gammainc(a, x)
  341. assert np.allclose(out.asnumpy(), expect, rtol=1e-5, atol=1e-5, equal_nan=True)