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_op.py 8.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. import mindspore
  20. from mindspore import Tensor
  21. from mindspore.ops import operations as P
  22. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  23. class SubNet(nn.Cell):
  24. def __init__(self):
  25. super(SubNet, self).__init__()
  26. self.sub = P.Sub()
  27. def construct(self, x, y):
  28. return self.sub(x, y)
  29. class DivNet(nn.Cell):
  30. def __init__(self):
  31. super(DivNet, self).__init__()
  32. self.div = P.Div()
  33. def construct(self, x, y):
  34. return self.div(x, y)
  35. class ModNet(nn.Cell):
  36. def __init__(self):
  37. super(ModNet, self).__init__()
  38. self.mod = P.Mod()
  39. def construct(self, x, y):
  40. return self.mod(x, y)
  41. @pytest.mark.level0
  42. @pytest.mark.platform_x86_cpu
  43. @pytest.mark.env_onecard
  44. def test_sub():
  45. x = np.random.rand(2, 3, 4, 4).astype(np.float32)
  46. y = np.random.rand(4, 1).astype(np.float32)
  47. net = SubNet()
  48. output = net(Tensor(x), Tensor(y, mindspore.float32))
  49. expect_output = x - y
  50. assert np.all(output.asnumpy() == expect_output)
  51. @pytest.mark.level0
  52. @pytest.mark.platform_x86_cpu_training
  53. @pytest.mark.env_onecard
  54. def test_div():
  55. prop = 1 if np.random.random() < 0.5 else -1
  56. x0_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
  57. y0_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
  58. x1_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
  59. y1_np = np.random.randint(1, 100, (2, 1, 4, 4)).astype(np.float32) * prop
  60. x2_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.float16) * prop
  61. y2_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float16) * prop
  62. x3_np = np.random.randint(1, 100, 1).astype(np.float32) * prop
  63. y3_np = np.random.randint(1, 100, 1).astype(np.float32) * prop
  64. x4_np = np.array(768).astype(np.float32) * prop
  65. y4_np = np.array(3072.5).astype(np.float32) * prop
  66. x5_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.int32) * prop
  67. y5_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int32) * prop
  68. x6_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int32) * prop
  69. y6_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
  70. x7_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.int64) * prop
  71. y7_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int64) * prop
  72. x0 = Tensor(x0_np)
  73. y0 = Tensor(y0_np)
  74. x1 = Tensor(x1_np)
  75. y1 = Tensor(y1_np)
  76. x2 = Tensor(x2_np)
  77. y2 = Tensor(y2_np)
  78. x3 = Tensor(x3_np)
  79. y3 = Tensor(y3_np)
  80. x4 = Tensor(x4_np)
  81. y4 = Tensor(y4_np)
  82. x5 = Tensor(x5_np)
  83. y5 = Tensor(y5_np)
  84. x6 = Tensor(x6_np)
  85. y6 = Tensor(y6_np)
  86. x7 = Tensor(x7_np)
  87. y7 = Tensor(y7_np)
  88. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  89. div = DivNet()
  90. output0 = div(x0, y0)
  91. expect0 = np.divide(x0_np, y0_np)
  92. diff0 = output0.asnumpy() - expect0
  93. error0 = np.ones(shape=expect0.shape) * 1.0e-5
  94. assert np.all(diff0 < error0)
  95. assert output0.shape == expect0.shape
  96. output1 = div(x1, y1)
  97. expect1 = np.divide(x1_np, y1_np)
  98. diff1 = output1.asnumpy() - expect1
  99. error1 = np.ones(shape=expect1.shape) * 1.0e-5
  100. assert np.all(diff1 < error1)
  101. assert output1.shape == expect1.shape
  102. output2 = div(x2, y2)
  103. expect2 = np.divide(x2_np, y2_np).astype(np.float16)
  104. diff2 = output2.asnumpy() - expect2
  105. error2 = np.ones(shape=expect2.shape) * 1.0e-5
  106. assert np.all(diff2 < error2)
  107. assert output2.shape == expect2.shape
  108. output3 = div(x3, y3)
  109. expect3 = np.divide(x3_np, y3_np)
  110. diff3 = output3.asnumpy() - expect3
  111. error3 = np.ones(shape=expect3.shape) * 1.0e-5
  112. assert np.all(diff3 < error3)
  113. assert output3.shape == expect3.shape
  114. output4 = div(x4, y4)
  115. expect4 = np.divide(x4_np, y4_np)
  116. diff4 = output4.asnumpy() - expect4
  117. error4 = np.ones(shape=expect4.shape) * 1.0e-5
  118. assert np.all(diff4 < error4)
  119. assert output4.shape == expect4.shape
  120. output5 = div(x5, y5)
  121. expect5 = x5_np // y5_np
  122. assert np.all(output5.asnumpy() == expect5)
  123. output6 = div(x6, y6)
  124. expect6 = np.divide(x6_np, y6_np)
  125. diff6 = output6.asnumpy() - expect6
  126. error6 = np.ones(shape=expect6.shape) * 1.0e-5
  127. assert np.all(diff6 < error6)
  128. assert output6.shape == expect6.shape
  129. output7 = div(x7, y7)
  130. expect7 = np.divide(x7_np, y7_np).astype(np.int64)
  131. assert np.all(output7.asnumpy() == expect7)
  132. assert output7.shape == expect7.shape
  133. @pytest.mark.level0
  134. @pytest.mark.platform_x86_cpu_training
  135. @pytest.mark.env_onecard
  136. def test_mod():
  137. prop = 1 if np.random.random() < 0.5 else -1
  138. x0_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
  139. y0_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
  140. x1_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
  141. y1_np = np.random.randint(1, 100, (2, 1, 4, 4)).astype(np.float32) * prop
  142. x2_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.float16) * prop
  143. y2_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float16) * prop
  144. x3_np = np.random.randint(1, 100, 1).astype(np.float32) * prop
  145. y3_np = np.random.randint(1, 100, 1).astype(np.float32) * prop
  146. x4_np = np.array(768).astype(np.float32) * prop
  147. y4_np = np.array(3072.5).astype(np.float32) * prop
  148. x5_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.int32) * prop
  149. y5_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int32) * prop
  150. x6_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int32) * prop
  151. y6_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
  152. x7_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.int64) * prop
  153. y7_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int64) * prop
  154. x0 = Tensor(x0_np)
  155. y0 = Tensor(y0_np)
  156. x1 = Tensor(x1_np)
  157. y1 = Tensor(y1_np)
  158. x2 = Tensor(x2_np)
  159. y2 = Tensor(y2_np)
  160. x3 = Tensor(x3_np)
  161. y3 = Tensor(y3_np)
  162. x4 = Tensor(x4_np)
  163. y4 = Tensor(y4_np)
  164. x5 = Tensor(x5_np)
  165. y5 = Tensor(y5_np)
  166. x6 = Tensor(x6_np)
  167. y6 = Tensor(y6_np)
  168. x7 = Tensor(x7_np)
  169. y7 = Tensor(y7_np)
  170. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  171. mod = ModNet()
  172. output0 = mod(x0, y0)
  173. expect0 = np.mod(x0_np, y0_np)
  174. diff0 = output0.asnumpy() - expect0
  175. error0 = np.ones(shape=expect0.shape) * 1.0e-5
  176. assert np.all(diff0 < error0)
  177. assert output0.shape == expect0.shape
  178. output1 = mod(x1, y1)
  179. expect1 = np.mod(x1_np, y1_np)
  180. diff1 = output1.asnumpy() - expect1
  181. error1 = np.ones(shape=expect1.shape) * 1.0e-5
  182. assert np.all(diff1 < error1)
  183. assert output1.shape == expect1.shape
  184. output2 = mod(x2, y2)
  185. expect2 = np.mod(x2_np, y2_np).astype(np.float16)
  186. diff2 = output2.asnumpy() - expect2
  187. error2 = np.ones(shape=expect2.shape) * 1.0e-5
  188. assert np.all(diff2 < error2)
  189. assert output2.shape == expect2.shape
  190. output3 = mod(x3, y3)
  191. expect3 = np.mod(x3_np, y3_np)
  192. diff3 = output3.asnumpy() - expect3
  193. error3 = np.ones(shape=expect3.shape) * 1.0e-5
  194. assert np.all(diff3 < error3)
  195. assert output3.shape == expect3.shape
  196. output4 = mod(x4, y4)
  197. expect4 = np.mod(x4_np, y4_np)
  198. diff4 = output4.asnumpy() - expect4
  199. error4 = np.ones(shape=expect4.shape) * 1.0e-5
  200. assert np.all(diff4 < error4)
  201. assert output4.shape == expect4.shape
  202. output5 = mod(x5, y5)
  203. expect5 = np.mod(x5_np, y5_np)
  204. assert np.all(output5.asnumpy() == expect5)
  205. assert output5.shape == expect5.shape
  206. output6 = mod(x6, y6)
  207. expect6 = np.mod(x6_np, y6_np)
  208. diff6 = output6.asnumpy() - expect6
  209. error6 = np.ones(shape=expect6.shape) * 1.0e-5
  210. assert np.all(diff6 < error6)
  211. assert output6.shape == expect6.shape
  212. output7 = mod(x7, y7)
  213. expect7 = np.mod(x7_np, y7_np).astype(np.int64)
  214. assert np.all(output7.asnumpy() == expect7)
  215. assert output6.shape == expect6.shape
  216. test_sub()
  217. test_div()
  218. test_mod()