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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. @pytest.mark.level0
  53. @pytest.mark.platform_x86_cpu
  54. @pytest.mark.env_onecard
  55. def test_square():
  56. x = np.array([1, 2, 3]).astype(np.int16)
  57. net = SquareNet()
  58. output = net(Tensor(x))
  59. expect_output = np.array([1, 4, 9]).astype(np.int16)
  60. print(output)
  61. assert np.all(output.asnumpy() == expect_output)
  62. x = np.array([1, 2, 3]).astype(np.int32)
  63. net = SquareNet()
  64. output = net(Tensor(x))
  65. expect_output = np.array([1, 4, 9]).astype(np.int32)
  66. print(output)
  67. assert np.all(output.asnumpy() == expect_output)
  68. x = np.array([1, 2, 3]).astype(np.int64)
  69. net = SquareNet()
  70. output = net(Tensor(x))
  71. expect_output = np.array([1, 4, 9]).astype(np.int64)
  72. print(output)
  73. assert np.all(output.asnumpy() == expect_output)
  74. x = np.array([1, 2, 3]).astype(np.float16)
  75. net = SquareNet()
  76. output = net(Tensor(x))
  77. expect_output = np.array([1, 4, 9]).astype(np.float16)
  78. print(output)
  79. assert np.all(output.asnumpy() == expect_output)
  80. x = np.array([1, 2, 3]).astype(np.float32)
  81. net = SquareNet()
  82. output = net(Tensor(x))
  83. expect_output = np.array([1, 4, 9]).astype(np.float32)
  84. print(output)
  85. assert np.all(output.asnumpy() == expect_output)
  86. x = np.array([1, 2, 3]).astype(np.float64)
  87. net = SquareNet()
  88. output = net(Tensor(x))
  89. expect_output = np.array([1, 4, 9]).astype(np.float64)
  90. print(output)
  91. assert np.all(output.asnumpy() == expect_output)
  92. @pytest.mark.level0
  93. @pytest.mark.platform_x86_cpu
  94. @pytest.mark.env_onecard
  95. def test_floor():
  96. net = FloorNet()
  97. x = np.random.randn(3, 4).astype(np.float16)
  98. x = x * 100
  99. output = net(Tensor(x))
  100. expect_output = np.floor(x).astype(np.float16)
  101. print(output.asnumpy())
  102. assert np.all(output.asnumpy() == expect_output)
  103. x = np.random.randn(4, 3).astype(np.float32)
  104. x = x * 100
  105. output = net(Tensor(x))
  106. expect_output = np.floor(x)
  107. print(output.asnumpy())
  108. assert np.all(output.asnumpy() == expect_output)
  109. @pytest.mark.level0
  110. @pytest.mark.platform_x86_cpu
  111. @pytest.mark.env_onecard
  112. def test_rint():
  113. net = RintNet()
  114. prop = 100 if np.random.random() > 0.5 else -100
  115. x = np.random.randn(3, 4, 5, 6).astype(np.float16) * prop
  116. output = net(Tensor(x))
  117. expect_output = np.rint(x).astype(np.float16)
  118. np.testing.assert_almost_equal(output.asnumpy(), expect_output)
  119. x = np.random.randn(3, 4, 5, 6).astype(np.float32) * prop
  120. output = net(Tensor(x))
  121. expect_output = np.rint(x).astype(np.float32)
  122. np.testing.assert_almost_equal(output.asnumpy(), expect_output)
  123. @pytest.mark.level0
  124. @pytest.mark.platform_x86_cpu
  125. @pytest.mark.env_onecard
  126. def test_round():
  127. net = RoundNet()
  128. x = np.array([0.9920, -0.4077, 0.9734, -1.0362, 1.5, -2.5, 4.5]).astype(np.float16)
  129. output = net(Tensor(x))
  130. expect_output = np.round(x).astype(np.float16)
  131. np.testing.assert_almost_equal(output.asnumpy(), expect_output)
  132. x = np.array([0.9920, -0.4077, 0.9734, -1.0362, 1.5, -2.5, 4.5]).astype(np.float32)
  133. output = net(Tensor(x))
  134. expect_output = np.round(x).astype(np.float32)
  135. np.testing.assert_almost_equal(output.asnumpy(), expect_output)
  136. @pytest.mark.level0
  137. @pytest.mark.platform_x86_cpu
  138. @pytest.mark.env_onecard
  139. def test_reciprocal():
  140. net = ReciprocalNet()
  141. prop = 100 if np.random.random() > 0.5 else -100
  142. x = np.random.randn(3, 4, 5, 6).astype(np.float16) * prop
  143. output = net(Tensor(x))
  144. expect_output = (1. / x).astype(np.float16)
  145. diff = output.asnumpy() - expect_output
  146. error = np.ones(shape=expect_output.shape) * 1.0e-5
  147. assert np.all(np.abs(diff) < error)
  148. x = np.random.randn(3, 4, 5, 6).astype(np.float32) * prop
  149. output = net(Tensor(x))
  150. expect_output = (1. / x).astype(np.float32)
  151. diff = output.asnumpy() - expect_output
  152. error = np.ones(shape=expect_output.shape) * 1.0e-5
  153. assert np.all(np.abs(diff) < error)