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_minimum_op.py 6.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. from mindspore.common.tensor import Tensor
  19. from mindspore.nn import Cell
  20. from mindspore.ops import operations as P
  21. class ConstScalarAndTensorMinimum(Cell):
  22. def __init__(self):
  23. super(ConstScalarAndTensorMinimum, self).__init__()
  24. self.min = P.Minimum()
  25. self.x = 20
  26. def construct(self, y):
  27. return self.min(self.x, y)
  28. class TwoTensorsMinimum(Cell):
  29. def __init__(self):
  30. super(TwoTensorsMinimum, self).__init__()
  31. self.min = P.Minimum()
  32. def construct(self, x, y):
  33. return self.min(x, y)
  34. @pytest.mark.level0
  35. @pytest.mark.platform_x86_cpu
  36. @pytest.mark.env_onecard
  37. def test_minimum_constScalar_tensor_int():
  38. x = Tensor(np.array([[2, 3, 4], [100, 200, 300]]).astype(np.int32))
  39. expect = [[2, 3, 4], [20, 20, 20]]
  40. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  41. min_op = ConstScalarAndTensorMinimum()
  42. output = min_op(x)
  43. assert np.all(output.asnumpy() == expect)
  44. @pytest.mark.level0
  45. @pytest.mark.platform_x86_cpu
  46. @pytest.mark.env_onecard
  47. def test_minimum_two_tensors_Not_Broadcast_int():
  48. prop = 100 if np.random.random() > 0.5 else -100
  49. x = np.random.randn(3, 4, 5).astype(np.int32) * prop
  50. y = np.random.randn(3, 4, 5).astype(np.int32) * prop
  51. expect = np.minimum(x, y).astype(np.int32)
  52. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  53. min_op = TwoTensorsMinimum()
  54. output = min_op(Tensor(x), Tensor(y))
  55. assert np.all(output.asnumpy() == expect)
  56. @pytest.mark.level0
  57. @pytest.mark.platform_x86_cpu
  58. @pytest.mark.env_onecard
  59. def test_minimum_two_tensors_Broadcast_int():
  60. prop = 100 if np.random.random() > 0.5 else -100
  61. x = np.random.randn(3, 4, 5).astype(np.int32) * prop
  62. y = np.random.randn(3, 1, 1).astype(np.int32) * prop
  63. expect = np.minimum(x, y).astype(np.int32)
  64. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  65. min_op = TwoTensorsMinimum()
  66. output = min_op(Tensor(x), Tensor(y))
  67. assert np.all(output.asnumpy() == expect)
  68. @pytest.mark.level0
  69. @pytest.mark.platform_x86_cpu
  70. @pytest.mark.env_onecard
  71. def test_minimum_two_tensors_Broadcast_oneDimension_int():
  72. prop = 100 if np.random.random() > 0.5 else -100
  73. x = np.random.randn(3).astype(np.int32) * prop
  74. y = np.random.randn(3).astype(np.int32) * prop
  75. expect = np.minimum(x, y).astype(np.int32)
  76. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  77. min_op = TwoTensorsMinimum()
  78. output = min_op(Tensor(x), Tensor(y))
  79. assert np.all(output.asnumpy() == expect)
  80. @pytest.mark.level0
  81. @pytest.mark.platform_x86_cpu
  82. @pytest.mark.env_onecard
  83. def test_minimum_two_tensors_notBroadcast_all_oneDimension_int():
  84. x = Tensor(np.array([[2]]).astype(np.int32))
  85. y = Tensor(np.array([[100]]).astype(np.int32))
  86. expect = [[2]]
  87. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  88. min_op = TwoTensorsMinimum()
  89. output = min_op(x, y)
  90. assert np.all(output.asnumpy() == expect)
  91. @pytest.mark.level0
  92. @pytest.mark.platform_x86_cpu
  93. @pytest.mark.env_onecard
  94. def test_minimum_two_tensors_notBroadcast_float32():
  95. prop = 100 if np.random.random() > 0.5 else -100
  96. x = np.random.randn(3, 4, 5).astype(np.float32) * prop
  97. y = np.random.randn(3, 4, 5).astype(np.float32) * prop
  98. expect = np.minimum(x, y).astype(np.float32)
  99. error = np.ones(shape=expect.shape) * 1.0e-5
  100. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  101. min_op = TwoTensorsMinimum()
  102. output = min_op(Tensor(x), Tensor(y))
  103. diff = output.asnumpy() - expect
  104. assert np.all(np.abs(diff) < error)
  105. assert output.shape == expect.shape
  106. @pytest.mark.level0
  107. @pytest.mark.platform_x86_cpu
  108. @pytest.mark.env_onecard
  109. def test_minimum_two_tensors_notBroadcast_float16():
  110. prop = 100 if np.random.random() > 0.5 else -100
  111. x = np.random.randn(3, 4, 5).astype(np.float16) * prop
  112. y = np.random.randn(3, 4, 5).astype(np.float16) * prop
  113. expect = np.minimum(x, y).astype(np.float16)
  114. error = np.ones(shape=expect.shape) * 1.0e-5
  115. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  116. min_op = TwoTensorsMinimum()
  117. output = min_op(Tensor(x), Tensor(y))
  118. diff = output.asnumpy() - expect
  119. assert np.all(np.abs(diff) < error)
  120. assert output.shape == expect.shape
  121. @pytest.mark.level0
  122. @pytest.mark.platform_x86_cpu
  123. @pytest.mark.env_onecard
  124. def test_minimum_two_tensors_Broadcast_float16():
  125. prop = 100 if np.random.random() > 0.5 else -100
  126. x = np.random.randn(3, 4, 5).astype(np.float16) * prop
  127. y = np.random.randn(3, 4, 1).astype(np.float16) * prop
  128. expect = np.minimum(x, y).astype(np.float16)
  129. error = np.ones(shape=expect.shape) * 1.0e-5
  130. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  131. min_op = TwoTensorsMinimum()
  132. output = min_op(Tensor(x), Tensor(y))
  133. diff = output.asnumpy() - expect
  134. assert np.all(np.abs(diff) < error)
  135. assert output.shape == expect.shape
  136. @pytest.mark.level0
  137. @pytest.mark.platform_x86_cpu
  138. @pytest.mark.env_onecard
  139. def test_minimum_two_tensors_notBroadcast_float64():
  140. prop = 100 if np.random.random() > 0.5 else -100
  141. x = np.random.randn(3, 4, 1).astype(np.float64) * prop
  142. y = np.random.randn(3, 4, 5).astype(np.float64) * prop
  143. expect = np.minimum(x, y).astype(np.float64)
  144. error = np.ones(shape=expect.shape) * 1.0e-5
  145. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  146. min_op = TwoTensorsMinimum()
  147. output = min_op(Tensor(x), Tensor(y))
  148. diff = output.asnumpy() - expect
  149. assert np.all(np.abs(diff) < error)
  150. assert output.shape == expect.shape