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_arithmetic_self_op.py 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 numpy as np
  16. import pytest
  17. import mindspore.context as context
  18. import mindspore.nn as nn
  19. from mindspore import Tensor
  20. from mindspore.ops import operations as P
  21. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  22. class SquareNet(nn.Cell):
  23. def __init__(self):
  24. super(SquareNet, self).__init__()
  25. self.square = P.Square()
  26. def construct(self, x):
  27. return self.square(x)
  28. class FloorNet(nn.Cell):
  29. def __init__(self):
  30. super(FloorNet, self).__init__()
  31. self.floor = P.Floor()
  32. def construct(self, x):
  33. return self.floor(x)
  34. class RoundNet(nn.Cell):
  35. def __init__(self):
  36. super(RoundNet, self).__init__()
  37. self.round = P.Round()
  38. def construct(self, x):
  39. return self.round(x)
  40. class ReciprocalNet(nn.Cell):
  41. def __init__(self):
  42. super(ReciprocalNet, self).__init__()
  43. self.reciprocal = P.Reciprocal()
  44. def construct(self, x):
  45. return self.reciprocal(x)
  46. class RintNet(nn.Cell):
  47. def __init__(self):
  48. super(RintNet, self).__init__()
  49. self.rint = P.Rint()
  50. def construct(self, x):
  51. return self.rint(x)
  52. class IdentityNet(nn.Cell):
  53. def __init__(self):
  54. super(IdentityNet, self).__init__()
  55. self.identity = P.Identity()
  56. def construct(self, x):
  57. return self.identity(x)
  58. @pytest.mark.level0
  59. @pytest.mark.platform_x86_cpu
  60. @pytest.mark.env_onecard
  61. def test_square():
  62. x = np.array([1, 2, 3]).astype(np.int16)
  63. net = SquareNet()
  64. output = net(Tensor(x))
  65. expect_output = np.array([1, 4, 9]).astype(np.int16)
  66. print(output)
  67. assert np.all(output.asnumpy() == expect_output)
  68. x = np.array([1, 2, 3]).astype(np.int32)
  69. net = SquareNet()
  70. output = net(Tensor(x))
  71. expect_output = np.array([1, 4, 9]).astype(np.int32)
  72. print(output)
  73. assert np.all(output.asnumpy() == expect_output)
  74. x = np.array([1, 2, 3]).astype(np.int64)
  75. net = SquareNet()
  76. output = net(Tensor(x))
  77. expect_output = np.array([1, 4, 9]).astype(np.int64)
  78. print(output)
  79. assert np.all(output.asnumpy() == expect_output)
  80. x = np.array([1, 2, 3]).astype(np.float16)
  81. net = SquareNet()
  82. output = net(Tensor(x))
  83. expect_output = np.array([1, 4, 9]).astype(np.float16)
  84. print(output)
  85. assert np.all(output.asnumpy() == expect_output)
  86. x = np.array([1, 2, 3]).astype(np.float32)
  87. net = SquareNet()
  88. output = net(Tensor(x))
  89. expect_output = np.array([1, 4, 9]).astype(np.float32)
  90. print(output)
  91. assert np.all(output.asnumpy() == expect_output)
  92. x = np.array([1, 2, 3]).astype(np.float64)
  93. net = SquareNet()
  94. output = net(Tensor(x))
  95. expect_output = np.array([1, 4, 9]).astype(np.float64)
  96. print(output)
  97. assert np.all(output.asnumpy() == expect_output)
  98. @pytest.mark.level0
  99. @pytest.mark.platform_x86_cpu
  100. @pytest.mark.env_onecard
  101. def test_floor():
  102. net = FloorNet()
  103. x = np.random.randn(3, 4).astype(np.float16)
  104. x = x * 100
  105. output = net(Tensor(x))
  106. expect_output = np.floor(x).astype(np.float16)
  107. print(output.asnumpy())
  108. assert np.all(output.asnumpy() == expect_output)
  109. x = np.random.randn(4, 3).astype(np.float32)
  110. x = x * 100
  111. output = net(Tensor(x))
  112. expect_output = np.floor(x)
  113. print(output.asnumpy())
  114. assert np.all(output.asnumpy() == expect_output)
  115. x = np.random.randn(4, 3).astype(np.float64)
  116. x = x * 100
  117. output = net(Tensor(x))
  118. expect_output = np.floor(x)
  119. print(output.asnumpy())
  120. assert np.all(output.asnumpy() == expect_output)
  121. @pytest.mark.level0
  122. @pytest.mark.platform_x86_cpu
  123. @pytest.mark.env_onecard
  124. def test_rint():
  125. net = RintNet()
  126. prop = 100 if np.random.random() > 0.5 else -100
  127. x = np.random.randn(3, 4, 5, 6).astype(np.float16) * prop
  128. output = net(Tensor(x))
  129. expect_output = np.rint(x).astype(np.float16)
  130. np.testing.assert_almost_equal(output.asnumpy(), expect_output)
  131. x = np.random.randn(3, 4, 5, 6).astype(np.float32) * prop
  132. output = net(Tensor(x))
  133. expect_output = np.rint(x).astype(np.float32)
  134. np.testing.assert_almost_equal(output.asnumpy(), expect_output)
  135. x = np.random.randn(3, 4, 5, 6).astype(np.float64) * prop
  136. output = net(Tensor(x))
  137. expect_output = np.rint(x).astype(np.float64)
  138. np.testing.assert_almost_equal(output.asnumpy(), expect_output)
  139. @pytest.mark.level0
  140. @pytest.mark.platform_x86_cpu
  141. @pytest.mark.env_onecard
  142. def test_round():
  143. net = RoundNet()
  144. x = np.array([0.9920, -0.4077, 0.9734, -1.0362, 1.5, -2.5, 4.5]).astype(np.float16)
  145. output = net(Tensor(x))
  146. expect_output = np.round(x).astype(np.float16)
  147. np.testing.assert_almost_equal(output.asnumpy(), expect_output)
  148. x = np.array([0.9920, -0.4077, 0.9734, -1.0362, 1.5, -2.5, 4.5]).astype(np.float32)
  149. output = net(Tensor(x))
  150. expect_output = np.round(x).astype(np.float32)
  151. np.testing.assert_almost_equal(output.asnumpy(), expect_output)
  152. x = np.array([0.9920, -0.4077, 0.9734, -1.0362, 1.5, -2.5, 4.5]).astype(np.float64)
  153. output = net(Tensor(x))
  154. expect_output = np.round(x).astype(np.float64)
  155. np.testing.assert_almost_equal(output.asnumpy(), expect_output)
  156. @pytest.mark.level0
  157. @pytest.mark.platform_x86_cpu
  158. @pytest.mark.env_onecard
  159. def test_reciprocal():
  160. net = ReciprocalNet()
  161. prop = 100 if np.random.random() > 0.5 else -100
  162. x = np.random.randn(3, 4, 5, 6).astype(np.float16) * prop
  163. output = net(Tensor(x))
  164. expect_output = (1. / x).astype(np.float16)
  165. diff = output.asnumpy() - expect_output
  166. error = np.ones(shape=expect_output.shape) * 1.0e-5
  167. assert np.all(np.abs(diff) < error)
  168. x = np.random.randn(3, 4, 5, 6).astype(np.float32) * prop
  169. output = net(Tensor(x))
  170. expect_output = (1. / x).astype(np.float32)
  171. diff = output.asnumpy() - expect_output
  172. error = np.ones(shape=expect_output.shape) * 1.0e-5
  173. assert np.all(np.abs(diff) < error)
  174. x = np.random.randn(3, 4, 5, 6).astype(np.float64) * prop
  175. output = net(Tensor(x))
  176. expect_output = (1. / x).astype(np.float64)
  177. diff = output.asnumpy() - expect_output
  178. error = np.ones(shape=expect_output.shape) * 1.0e-7
  179. assert np.all(np.abs(diff) < error)
  180. @pytest.mark.level0
  181. @pytest.mark.platform_x86_cpu
  182. @pytest.mark.env_onecard
  183. def test_identity_pynative():
  184. context.set_context(mode=context.PYNATIVE_MODE, device_target="CPU")
  185. net = IdentityNet()
  186. x = np.random.randn(3, 4, 5, 6).astype(np.float64)
  187. input_tensor = Tensor(x)
  188. output = net(input_tensor)
  189. np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy())
  190. assert id(input_tensor) != id(output)
  191. x = np.random.randn(3, 4, 5, 6).astype(np.float32)
  192. input_tensor = Tensor(x)
  193. output = net(input_tensor)
  194. np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy())
  195. assert id(input_tensor) != id(output)
  196. x = np.random.randn(3, 4, 5, 6).astype(np.float16)
  197. input_tensor = Tensor(x)
  198. output = net(input_tensor)
  199. np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy())
  200. assert id(input_tensor) != id(output)
  201. x = np.random.randn(3, 4, 5, 6).astype(np.uint64)
  202. input_tensor = Tensor(x)
  203. output = net(input_tensor)
  204. np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy())
  205. assert id(input_tensor) != id(output)
  206. x = np.random.randn(3, 4, 5, 6).astype(np.int64)
  207. input_tensor = Tensor(x)
  208. output = net(input_tensor)
  209. np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy())
  210. assert id(input_tensor) != id(output)
  211. x = np.random.randn(3, 4, 5, 6).astype(np.uint32)
  212. input_tensor = Tensor(x)
  213. output = net(input_tensor)
  214. np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy())
  215. assert id(input_tensor) != id(output)
  216. x = np.random.randn(3, 4, 5, 6).astype(np.int32)
  217. input_tensor = Tensor(x)
  218. output = net(input_tensor)
  219. np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy())
  220. assert id(input_tensor) != id(output)
  221. x = np.random.randn(3, 4, 5, 6).astype(np.uint16)
  222. input_tensor = Tensor(x)
  223. output = net(input_tensor)
  224. np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy())
  225. assert id(input_tensor) != id(output)
  226. x = np.random.randn(3, 4, 5, 6).astype(np.int16)
  227. input_tensor = Tensor(x)
  228. output = net(input_tensor)
  229. np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy())
  230. assert id(input_tensor) != id(output)
  231. x = np.random.randn(3, 4, 5, 6).astype(np.uint8)
  232. input_tensor = Tensor(x)
  233. output = net(input_tensor)
  234. np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy())
  235. assert id(input_tensor) != id(output)
  236. x = np.random.randn(3, 4, 5, 6).astype(np.int8)
  237. input_tensor = Tensor(x)
  238. output = net(input_tensor)
  239. np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy())
  240. assert id(input_tensor) != id(output)
  241. x = np.random.randn(3, 4, 5, 6).astype(np.bool)
  242. input_tensor = Tensor(x)
  243. output = net(input_tensor)
  244. np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy())
  245. assert id(input_tensor) != id(output)
  246. @pytest.mark.level0
  247. @pytest.mark.platform_x86_cpu
  248. @pytest.mark.env_onecard
  249. def test_identity_graph():
  250. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  251. net = IdentityNet()
  252. x = np.random.randn(3, 4, 5, 6).astype(np.float64)
  253. input_tensor = Tensor(x)
  254. output = net(input_tensor)
  255. np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy())
  256. assert id(input_tensor) != id(output)
  257. x = np.random.randn(3, 4, 5, 6).astype(np.float32)
  258. input_tensor = Tensor(x)
  259. output = net(input_tensor)
  260. np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy())
  261. assert id(input_tensor) != id(output)
  262. x = np.random.randn(3, 4, 5, 6).astype(np.float16)
  263. input_tensor = Tensor(x)
  264. output = net(input_tensor)
  265. np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy())
  266. assert id(input_tensor) != id(output)
  267. x = np.random.randn(3, 4, 5, 6).astype(np.uint64)
  268. input_tensor = Tensor(x)
  269. output = net(input_tensor)
  270. np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy())
  271. assert id(input_tensor) != id(output)
  272. x = np.random.randn(3, 4, 5, 6).astype(np.int64)
  273. input_tensor = Tensor(x)
  274. output = net(input_tensor)
  275. np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy())
  276. assert id(input_tensor) != id(output)
  277. x = np.random.randn(3, 4, 5, 6).astype(np.uint32)
  278. input_tensor = Tensor(x)
  279. output = net(input_tensor)
  280. np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy())
  281. assert id(input_tensor) != id(output)
  282. x = np.random.randn(3, 4, 5, 6).astype(np.int32)
  283. input_tensor = Tensor(x)
  284. output = net(input_tensor)
  285. np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy())
  286. assert id(input_tensor) != id(output)
  287. x = np.random.randn(3, 4, 5, 6).astype(np.uint16)
  288. input_tensor = Tensor(x)
  289. output = net(input_tensor)
  290. np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy())
  291. assert id(input_tensor) != id(output)
  292. x = np.random.randn(3, 4, 5, 6).astype(np.int16)
  293. input_tensor = Tensor(x)
  294. output = net(input_tensor)
  295. np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy())
  296. assert id(input_tensor) != id(output)
  297. x = np.random.randn(3, 4, 5, 6).astype(np.uint8)
  298. input_tensor = Tensor(x)
  299. output = net(input_tensor)
  300. np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy())
  301. assert id(input_tensor) != id(output)
  302. x = np.random.randn(3, 4, 5, 6).astype(np.int8)
  303. input_tensor = Tensor(x)
  304. output = net(input_tensor)
  305. np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy())
  306. assert id(input_tensor) != id(output)
  307. x = np.random.randn(3, 4, 5, 6).astype(np.bool)
  308. input_tensor = Tensor(x)
  309. output = net(input_tensor)
  310. np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy())
  311. assert id(input_tensor) != id(output)