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

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