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_pynative_hccl.py 2.9 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. """test bert thor performance with 8p on mlperf dataset"""
  16. import os
  17. from multiprocessing import Process
  18. import pytest
  19. import numpy as np
  20. import mindspore.nn as nn
  21. from mindspore import Tensor
  22. import mindspore.dataset as dataset
  23. from mindspore import dtype as mstype
  24. from mindspore.ops import operations as P
  25. import mindspore.communication.management as D
  26. from mindspore import context
  27. from mindspore.context import ParallelMode
  28. MINDSPORE_HCCL_CONFIG_PATH = "/home/workspace/mindspore_config/hccl/rank_table_8p.json"
  29. np.random.seed(1)
  30. dataset.config.set_seed(1)
  31. os.environ['GLOG_v'] = str(2)
  32. class AllReduceNet(nn.Cell):
  33. def __init__(self):
  34. super(AllReduceNet, self).__init__()
  35. self.all_reduce = P.AllReduce()
  36. def construct(self, x):
  37. return self.all_reduce(x)
  38. def train_allreduce_8p(device_id, device_num):
  39. os.system("mkdir " + str(device_id))
  40. os.chdir(str(device_id))
  41. context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend", device_id=device_id)
  42. os.environ['MINDSPORE_HCCL_CONFIG_PATH'] = MINDSPORE_HCCL_CONFIG_PATH
  43. os.environ['RANK_ID'] = str(device_id)
  44. os.environ['RANK_SIZE'] = str(device_num)
  45. D.init()
  46. context.reset_auto_parallel_context()
  47. context.set_auto_parallel_context(parallel_mode=ParallelMode.DATA_PARALLEL, gradients_mean=True,
  48. device_num=device_num)
  49. net = AllReduceNet()
  50. input_x = np.ones([32, 255, 255, 3]).astype(np.float32)
  51. except_output = input_x * 8
  52. output = net(Tensor(input_x, mstype.float32))
  53. assert np.allclose(output.asnumpy(), except_output)
  54. @pytest.mark.level0
  55. @pytest.mark.platform_arm_ascend_training
  56. @pytest.mark.platform_x86_ascend_training
  57. @pytest.mark.env_single
  58. def test_pynative_hccl_8p():
  59. device_num = 8
  60. process = []
  61. for i in range(device_num):
  62. device_id = i
  63. process.append(Process(target=train_allreduce_8p, args=(device_id, device_num)))
  64. for i in range(device_num):
  65. process[i].start()
  66. print("Waiting for all subprocesses done...")
  67. for i in range(device_num):
  68. process[i].join()
  69. for i in range(device_num):
  70. os.system("rm -rf " + str(i))
  71. print("End training...")