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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 ReciprocalNet(nn.Cell):
  35. def __init__(self):
  36. super(ReciprocalNet, self).__init__()
  37. self.reciprocal = P.Reciprocal()
  38. def construct(self, x):
  39. return self.reciprocal(x)
  40. @pytest.mark.level0
  41. @pytest.mark.platform_x86_cpu
  42. @pytest.mark.env_onecard
  43. def test_square():
  44. x = np.array([1, 2, 3]).astype(np.int16)
  45. net = SquareNet()
  46. output = net(Tensor(x))
  47. expect_output = np.array([1, 4, 9]).astype(np.int16)
  48. print(output)
  49. assert np.all(output.asnumpy() == expect_output)
  50. x = np.array([1, 2, 3]).astype(np.int32)
  51. net = SquareNet()
  52. output = net(Tensor(x))
  53. expect_output = np.array([1, 4, 9]).astype(np.int32)
  54. print(output)
  55. assert np.all(output.asnumpy() == expect_output)
  56. x = np.array([1, 2, 3]).astype(np.int64)
  57. net = SquareNet()
  58. output = net(Tensor(x))
  59. expect_output = np.array([1, 4, 9]).astype(np.int64)
  60. print(output)
  61. assert np.all(output.asnumpy() == expect_output)
  62. x = np.array([1, 2, 3]).astype(np.float16)
  63. net = SquareNet()
  64. output = net(Tensor(x))
  65. expect_output = np.array([1, 4, 9]).astype(np.float16)
  66. print(output)
  67. assert np.all(output.asnumpy() == expect_output)
  68. x = np.array([1, 2, 3]).astype(np.float32)
  69. net = SquareNet()
  70. output = net(Tensor(x))
  71. expect_output = np.array([1, 4, 9]).astype(np.float32)
  72. print(output)
  73. assert np.all(output.asnumpy() == expect_output)
  74. x = np.array([1, 2, 3]).astype(np.float64)
  75. net = SquareNet()
  76. output = net(Tensor(x))
  77. expect_output = np.array([1, 4, 9]).astype(np.float64)
  78. print(output)
  79. assert np.all(output.asnumpy() == expect_output)
  80. @pytest.mark.level0
  81. @pytest.mark.platform_x86_cpu
  82. @pytest.mark.env_onecard
  83. def test_floor():
  84. net = FloorNet()
  85. x = np.random.randn(3, 4).astype(np.float16)
  86. x = x * 100
  87. output = net(Tensor(x))
  88. expect_output = np.floor(x).astype(np.float16)
  89. print(output.asnumpy())
  90. assert np.all(output.asnumpy() == expect_output)
  91. x = np.random.randn(4, 3).astype(np.float32)
  92. x = x * 100
  93. output = net(Tensor(x))
  94. expect_output = np.floor(x)
  95. print(output.asnumpy())
  96. assert np.all(output.asnumpy() == expect_output)
  97. @pytest.mark.level0
  98. @pytest.mark.platform_x86_cpu
  99. @pytest.mark.env_onecard
  100. def test_reciprocal():
  101. net = ReciprocalNet()
  102. prop = 100 if np.random.random() > 0.5 else -100
  103. x = np.random.randn(3, 4, 5, 6).astype(np.float16) * prop
  104. output = net(Tensor(x))
  105. expect_output = (1. / x).astype(np.float16)
  106. diff = output.asnumpy() - expect_output
  107. error = np.ones(shape=expect_output.shape) * 1.0e-5
  108. assert np.all(np.abs(diff) < error)
  109. x = np.random.randn(3, 4, 5, 6).astype(np.float32) * prop
  110. output = net(Tensor(x))
  111. expect_output = (1. / x).astype(np.float32)
  112. diff = output.asnumpy() - expect_output
  113. error = np.ones(shape=expect_output.shape) * 1.0e-5
  114. assert np.all(np.abs(diff) < error)
  115. test_square()
  116. test_floor()
  117. test_reciprocal()