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_to_ops.py 3.1 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.ops import operations as P
  20. @pytest.mark.level0
  21. @pytest.mark.platform_x86_gpu_training
  22. @pytest.mark.env_onecard
  23. def test_broadcast():
  24. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  25. shape = (3, 4, 5, 6)
  26. x_np = np.random.rand(3, 1, 5, 1).astype(np.float32)
  27. output = P.BroadcastTo(shape)(Tensor(x_np))
  28. expect = np.broadcast_to(x_np, shape)
  29. assert np.allclose(output.asnumpy(), expect)
  30. x1_np = np.random.rand(3, 1, 5, 1).astype(np.float16)
  31. output = P.BroadcastTo(shape)(Tensor(x1_np))
  32. expect = np.broadcast_to(x1_np, shape)
  33. assert np.allclose(output.asnumpy(), expect)
  34. shape = (2, 3, 4, 5)
  35. x1_np = np.random.rand(4, 5).astype(np.float32)
  36. output = P.BroadcastTo(shape)(Tensor(x1_np))
  37. expect = np.broadcast_to(x1_np, shape)
  38. assert np.allclose(output.asnumpy(), expect)
  39. @pytest.mark.level0
  40. @pytest.mark.platform_x86_gpu_training
  41. @pytest.mark.env_onecard
  42. def test_broadcast_dyn_init():
  43. """
  44. Test running the op with -1's in the init shape to support varied inputs.
  45. """
  46. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  47. ms_shape = (-1, 4, 5, 6)
  48. np_shape = (3, 4, 5, 6)
  49. x_np = np.random.rand(3, 1, 5, 1).astype(np.float32)
  50. output = P.BroadcastTo(ms_shape)(Tensor(x_np))
  51. expect = np.broadcast_to(x_np, np_shape)
  52. assert np.allclose(output.asnumpy(), expect)
  53. x1_np = np.random.rand(3, 1, 5, 1).astype(np.float16)
  54. output = P.BroadcastTo(ms_shape)(Tensor(x1_np))
  55. expect = np.broadcast_to(x1_np, np_shape)
  56. assert np.allclose(output.asnumpy(), expect)
  57. ms_shape = (2, 3, -1, 5)
  58. np_shape = (2, 3, 4, 5)
  59. x1_np = np.random.rand(4, 5).astype(np.float32)
  60. output = P.BroadcastTo(ms_shape)(Tensor(x1_np))
  61. expect = np.broadcast_to(x1_np, np_shape)
  62. assert np.allclose(output.asnumpy(), expect)
  63. @pytest.mark.level0
  64. @pytest.mark.platform_x86_gpu_training
  65. @pytest.mark.env_onecard
  66. def test_broadcast_dyn_invalid_init():
  67. """
  68. Test running the op with -1's in the init shape in incorrect positions.
  69. Expected to fail.
  70. """
  71. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  72. ms_shape = (2, -1, 4, 5)
  73. x_np = np.random.rand(4, 5).astype(np.float32)
  74. with pytest.raises(ValueError):
  75. P.BroadcastTo(ms_shape)(Tensor(x_np))