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_nccl_broadcast_op.py 2.6 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 mindspore.context as context
  17. import mindspore.nn as nn
  18. from mindspore import Tensor
  19. from mindspore.common.initializer import initializer
  20. from mindspore.common.parameter import Parameter
  21. from mindspore.communication.management import init, get_rank, get_group_size
  22. from mindspore.ops import operations as P
  23. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  24. init()
  25. rank = get_rank()
  26. size = get_group_size()
  27. x = np.ones([3, 1, 3, 3]).astype(np.float32) * 0.01 * (rank + 1)
  28. class Net(nn.Cell):
  29. def __init__(self):
  30. super(Net, self).__init__()
  31. self.x1 = Parameter(initializer(Tensor(x), x.shape), name='x1')
  32. self.x2 = Parameter(initializer(Tensor(x), x.shape), name='x2')
  33. self.x3 = Parameter(initializer(Tensor(x), x.shape), name='x3')
  34. self.broadcast1 = P.Broadcast(0)
  35. self.broadcast2 = P.Broadcast(1)
  36. self.broadcast3 = P.Broadcast(2)
  37. def construct(self):
  38. return (self.broadcast1((self.x1,)),
  39. self.broadcast2((self.x2,)),
  40. self.broadcast3((self.x3,)))
  41. def test_Broadcast():
  42. broadcast = Net()
  43. output = broadcast()
  44. expect0 = np.ones([3, 1, 3, 3]).astype(np.float32) * 1
  45. expect1 = np.ones([3, 1, 3, 3]).astype(np.float32) * 2
  46. expect2 = np.ones([3, 1, 3, 3]).astype(np.float32) * 3
  47. diff0 = output[0][0].asnumpy() - expect0
  48. error0 = np.ones(shape=expect0.shape) * 1.0e-5
  49. assert np.all(diff0 < error0)
  50. assert output[0][0].shape == expect0.shape
  51. diff1 = output[1][0].asnumpy() - expect1
  52. error1 = np.ones(shape=expect1.shape) * 1.0e-5
  53. assert np.all(diff1 < error1)
  54. assert output[1][0].shape == expect1.shape
  55. diff2 = output[2][0].asnumpy() - expect2
  56. error2 = np.ones(shape=expect2.shape) * 1.0e-5
  57. assert np.all(diff2 < error2)
  58. assert output[2][0].shape == expect2.shape