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_maximum_op.py 5.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 ConstScalarAndTensorMaximum(Cell):
  22. def __init__(self):
  23. super(ConstScalarAndTensorMaximum, self).__init__()
  24. self.max = P.Maximum()
  25. self.x = 20
  26. def construct(self, y):
  27. return self.max(self.x, y)
  28. class TwoTensorsMaximum(Cell):
  29. def __init__(self):
  30. super(TwoTensorsMaximum, self).__init__()
  31. self.max = P.Maximum()
  32. def construct(self, x, y):
  33. return self.max(x, y)
  34. @pytest.mark.level0
  35. @pytest.mark.platform_x86_cpu_training
  36. @pytest.mark.env_onecard
  37. def test_maximum_constScalar_tensor_int():
  38. x = Tensor(np.array([[2, 3, 4], [100, 200, 300]]).astype(np.int32))
  39. expect = [[20, 20, 20], [100, 200, 300]]
  40. error = np.ones(shape=[2, 3]) * 1.0e-5
  41. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  42. max_op = ConstScalarAndTensorMaximum()
  43. output = max_op(x)
  44. diff = output.asnumpy() - expect
  45. assert np.all(diff < error)
  46. assert np.all(-diff < error)
  47. @pytest.mark.level0
  48. @pytest.mark.platform_x86_cpu_training
  49. @pytest.mark.env_onecard
  50. def test_maximum_two_tensors_Not_Broadcast_int():
  51. x = Tensor(np.array([[2, 3, 4], [100, 200, 300]]).astype(np.int32))
  52. y = Tensor(np.array([[1, 2, 3], [100, 100, 200]]).astype(np.int32))
  53. expect = [[2, 3, 4], [100, 200, 300]]
  54. error = np.ones(shape=[2, 3]) * 1.0e-5
  55. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  56. max_op = TwoTensorsMaximum()
  57. output = max_op(x, y)
  58. diff = output.asnumpy() - expect
  59. assert np.all(diff < error)
  60. assert np.all(-diff < error)
  61. @pytest.mark.level0
  62. @pytest.mark.platform_x86_cpu_training
  63. @pytest.mark.env_onecard
  64. def test_maximum_two_tensors_Broadcast_int():
  65. x = Tensor(np.array([[2, 3, 4], [100, 200, 300]]).astype(np.int32))
  66. y = Tensor(np.array([[100, 100, 200]]).astype(np.int32))
  67. expect = [[100, 100, 200], [100, 200, 300]]
  68. error = np.ones(shape=[2, 3]) * 1.0e-5
  69. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  70. max_op = TwoTensorsMaximum()
  71. output = max_op(x, y)
  72. diff = output.asnumpy() - expect
  73. assert np.all(diff < error)
  74. assert np.all(-diff < error)
  75. @pytest.mark.level0
  76. @pytest.mark.platform_x86_cpu_training
  77. @pytest.mark.env_onecard
  78. def test_maximum_two_tensors_Broadcast_oneDimension_int():
  79. x = Tensor(np.array([[2, 3, 4], [100, 200, 300]]).astype(np.int32))
  80. y = Tensor(np.array([[100]]).astype(np.int32))
  81. expect = [[100, 100, 100], [100, 200, 300]]
  82. error = np.ones(shape=[2, 3]) * 1.0e-5
  83. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  84. max_op = TwoTensorsMaximum()
  85. output = max_op(x, y)
  86. diff = output.asnumpy() - expect
  87. assert np.all(diff < error)
  88. assert np.all(-diff < error)
  89. @pytest.mark.level0
  90. @pytest.mark.platform_x86_cpu_training
  91. @pytest.mark.env_onecard
  92. def test_maximum_two_tensors_notBroadcast_all_oneDimension_int():
  93. x = Tensor(np.array([[2]]).astype(np.int32))
  94. y = Tensor(np.array([[100]]).astype(np.int32))
  95. expect = [[100]]
  96. error = np.ones(shape=[1, 1]) * 1.0e-5
  97. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  98. max_op = TwoTensorsMaximum()
  99. output = max_op(x, y)
  100. diff = output.asnumpy() - expect
  101. assert np.all(diff < error)
  102. assert np.all(-diff < error)
  103. @pytest.mark.level0
  104. @pytest.mark.platform_x86_cpu_training
  105. @pytest.mark.env_onecard
  106. def test_maximum_two_tensors_notBroadcast_float32():
  107. x = Tensor(np.array([[2.0, 2.0], [-1, 100]]).astype(np.float32))
  108. y = Tensor(np.array([[1.0, 2.1], [-0.8, 100.5]]).astype(np.float32))
  109. expect = [[2.0, 2.1], [-0.8, 100.5]]
  110. error = np.ones(shape=[2, 2]) * 1.0e-5
  111. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  112. max_op = TwoTensorsMaximum()
  113. output = max_op(x, y)
  114. diff = output.asnumpy() - expect
  115. assert np.all(diff < error)
  116. assert np.all(-diff < error)
  117. @pytest.mark.level0
  118. @pytest.mark.platform_x86_cpu_training
  119. @pytest.mark.env_onecard
  120. def test_maximum_two_tensors_notBroadcast_float64():
  121. x = Tensor(np.array([[2.0, 2.0], [-1, 100]]).astype(np.float64))
  122. y = Tensor(np.array([[1.0, 2.1], [-0.8, 100.5]]).astype(np.float64))
  123. expect = [[2.0, 2.1], [-0.8, 100.5]]
  124. error = np.ones(shape=[2, 2]) * 1.0e-5
  125. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  126. max_op = TwoTensorsMaximum()
  127. output = max_op(x, y)
  128. diff = output.asnumpy() - expect
  129. assert np.all(diff < error)
  130. assert np.all(-diff < error)