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_dict.py 2.4 kB

5 years ago
5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # Copyright 2019 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. import numpy as np
  15. import mindspore.context as context
  16. import mindspore.nn as nn
  17. from mindspore import Tensor, Parameter
  18. from mindspore.communication.management import init
  19. from mindspore.ops import operations as P
  20. class DataParallelNet(nn.Cell):
  21. def __init__(self):
  22. super(DataParallelNet, self).__init__()
  23. weight_init = np.random.rand(512, 64).astype(np.float32)
  24. self.weight = Parameter(Tensor(weight_init), name="weight", layerwise_parallel=False)
  25. self.fc = P.MatMul()
  26. def construct(self, x):
  27. x = self.fc(x, self.weight)
  28. return x
  29. class ModelParallelNet(nn.Cell):
  30. def __init__(self):
  31. super(ModelParallelNet, self).__init__()
  32. weight_init = np.random.rand(512, 64).astype(np.float32)
  33. self.weight = Parameter(Tensor(weight_init), name="weight", layerwise_parallel=True)
  34. self.fc = P.MatMul()
  35. def construct(self, x):
  36. x = self.fc(x, self.weight)
  37. return x
  38. def test_param_broadcast():
  39. context.set_context(mode=context.GRAPH_MODE)
  40. context.reset_auto_parallel_context()
  41. context.set_auto_parallel_context(parallel_mode="data_parallel", parameter_broadcast=True)
  42. init()
  43. network = DataParallelNet()
  44. network.set_train()
  45. predict = Tensor(np.ones([64, 512]).astype(np.float32) * 0.01)
  46. _ = network(predict)
  47. context.reset_auto_parallel_context()
  48. def test_param_not_broadcast():
  49. context.set_context(mode=context.GRAPH_MODE)
  50. context.reset_auto_parallel_context()
  51. context.set_auto_parallel_context(parallel_mode="data_parallel", parameter_broadcast=False)
  52. init()
  53. network = ModelParallelNet()
  54. network.set_train()
  55. predict = Tensor(np.ones([64, 512]).astype(np.float32) * 0.01)
  56. _ = network(predict)
  57. context.reset_auto_parallel_context()