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_broadcast_op.py 3.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 pytest
  16. from mindspore.ops import operations as P
  17. from mindspore.nn import Cell
  18. from mindspore.common.tensor import Tensor
  19. import mindspore.common.dtype as mstype
  20. import mindspore.context as context
  21. import numpy as np
  22. @pytest.mark.level0
  23. @pytest.mark.platform_x86_gpu_training
  24. @pytest.mark.env_onecard
  25. def test_nobroadcast():
  26. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  27. x1_np = np.random.rand(10, 20).astype(np.float32)
  28. x2_np = np.random.rand(10, 20).astype(np.float32)
  29. output_ms = P.Minimum()(Tensor(x1_np), Tensor(x2_np))
  30. output_np = np.minimum(x1_np, x2_np)
  31. assert np.allclose(output_ms.asnumpy(), output_np)
  32. output_ms = P.Maximum()(Tensor(x1_np), Tensor(x2_np))
  33. output_np = np.maximum(x1_np, x2_np)
  34. assert np.allclose(output_ms.asnumpy(), output_np)
  35. output_ms = P.Greater()(Tensor(x1_np), Tensor(x2_np))
  36. output_np = x1_np > x2_np
  37. assert np.allclose(output_ms.asnumpy(), output_np)
  38. output_ms = P.Less()(Tensor(x1_np), Tensor(x2_np))
  39. output_np = x1_np < x2_np
  40. assert np.allclose(output_ms.asnumpy(), output_np)
  41. output_ms = P.Pow()(Tensor(x1_np), Tensor(x2_np))
  42. output_np = np.power(x1_np, x2_np)
  43. assert np.allclose(output_ms.asnumpy(), output_np)
  44. @pytest.mark.level0
  45. @pytest.mark.platform_x86_gpu_training
  46. @pytest.mark.env_onecard
  47. def test_broadcast():
  48. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  49. x1_np = np.random.rand(3, 1, 5, 1).astype(np.float32)
  50. x2_np = np.random.rand(1, 4, 1, 6).astype(np.float32)
  51. output_ms = P.Minimum()(Tensor(x1_np), Tensor(x2_np))
  52. output_np = np.minimum(x1_np, x2_np)
  53. assert np.allclose(output_ms.asnumpy(), output_np)
  54. output_ms = P.Maximum()(Tensor(x1_np), Tensor(x2_np))
  55. output_np = np.maximum(x1_np, x2_np)
  56. assert np.allclose(output_ms.asnumpy(), output_np)
  57. output_ms = P.Greater()(Tensor(x1_np), Tensor(x2_np))
  58. output_np = x1_np > x2_np
  59. assert np.allclose(output_ms.asnumpy(), output_np)
  60. output_ms = P.Less()(Tensor(x1_np), Tensor(x2_np))
  61. output_np = x1_np < x2_np
  62. assert np.allclose(output_ms.asnumpy(), output_np)
  63. output_ms = P.Pow()(Tensor(x1_np), Tensor(x2_np))
  64. output_np = np.power(x1_np, x2_np)
  65. assert np.allclose(output_ms.asnumpy(), output_np)