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.

_cell_wrapper.py 2.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. """Cell of auto parallel"""
  16. from mindspore.nn.cell import Cell
  17. from mindspore.ops.operations.comm_ops import AllGather
  18. _allgather_cell = None
  19. class AllGatherCell(Cell):
  20. """
  21. Allgather cell, used in model parallel scenario.
  22. To allgather the selected parameter slice from each device.
  23. """
  24. def __init__(self):
  25. super(AllGatherCell, self).__init__(auto_prefix=False)
  26. self.allgather = AllGather()
  27. def construct(self, x):
  28. x = self.allgather(x)
  29. return x
  30. class SaveOptShardCkptCell(Cell):
  31. """
  32. Allgather cell, used in optimizer parallel scenario.
  33. Firstly gather the tensor to original layout in the specified device group.
  34. Then gather the whole parameter slices from all devices.
  35. Note:
  36. This could be optimized later with less communication consumption.
  37. """
  38. def __init__(self, group):
  39. super(SaveOptShardCkptCell, self).__init__(auto_prefix=False)
  40. self.allgather1 = AllGather(group)
  41. self.allgather2 = AllGather()
  42. def construct(self, x):
  43. x = self.allgather1(x)
  44. x = self.allgather2(x)
  45. return x
  46. def get_allgather_cell(group):
  47. """Get AllGatherCell object."""
  48. global _allgather_cell
  49. if group:
  50. _allgather_cell = SaveOptShardCkptCell(group)
  51. else:
  52. _allgather_cell = AllGatherCell()
  53. return _allgather_cell
  54. def destroy_allgather_cell():
  55. """Destroy AllGatherCell object."""
  56. global _allgather_cell
  57. if _allgather_cell:
  58. _allgather_cell = None