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.

onehot_model_parallel.py 5.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. # ============================================================================
  15. import os
  16. import pytest
  17. import numpy as np
  18. import mindspore as ms
  19. from mindspore.nn import Cell
  20. from mindspore.ops import operations as P
  21. from mindspore.common.tensor import Tensor
  22. import mindspore.context as context
  23. import mindspore.communication.management as distributedTool
  24. device_num = 2
  25. device_id = int(os.getenv('DEVICE_ID'))
  26. rank_id = 0
  27. def setup_module():
  28. global device_num
  29. global rank_id
  30. np.random.seed(0)
  31. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  32. context.set_context(enable_task_sink=True,
  33. device_id=device_id)
  34. context.set_context(enable_ir_fusion=True)
  35. context.set_context(enable_loop_sink=False)
  36. distributedTool.init()
  37. device_num = distributedTool.get_group_size()
  38. rank_id = distributedTool.get_rank()
  39. context.set_auto_parallel_context(device_num=device_num,
  40. global_rank=rank_id)
  41. def teardown_module():
  42. distributedTool.release()
  43. class Onehot(Cell):
  44. def __init__(self, axis=-1, depth=1, on_value=1.0, off_value=0.0, strategy=None):
  45. super(Onehot, self).__init__()
  46. trans_stra = None
  47. if strategy:
  48. trans_stra = (strategy[0],)
  49. self.onehot = P.OneHot().set_strategy(strategy=strategy)
  50. self.depth = depth
  51. self.on_value = Tensor(on_value, ms.float32)
  52. self.off_value = Tensor(off_value, ms.float32)
  53. self.transpose = P.Transpose().set_strategy(strategy=trans_stra)
  54. self.sub = P.Sub().set_strategy(strategy=((1, 1), (1, 1)))
  55. def construct(self, input, indices):
  56. x = self.onehot(indices, self.depth, self.on_value, self.off_value)
  57. x = self.transpose(x, (1, 0))
  58. x = self.sub(input, x)
  59. return x
  60. class DataGenerator():
  61. def get_parallel_blocks(self, input_, strategy):
  62. blocks = [input_]
  63. i = 0
  64. for stra in strategy:
  65. temp = []
  66. while len(blocks) > 0:
  67. block = blocks.pop(0)
  68. temp.extend(np.split(block, stra, axis=i))
  69. blocks.extend(temp)
  70. i += 1
  71. return blocks
  72. def generate_data(self, shape):
  73. data = np.random.rand(*shape)
  74. return data
  75. def input_data(self, shape):
  76. data = (self.generate_data(shape)*2).astype(np.float32)
  77. stra = [1]*len(shape)
  78. stra[0] = device_num
  79. datas = self.get_parallel_blocks(data, stra)
  80. return Tensor(data), Tensor(datas[rank_id])
  81. def label_data(self, shape, classes):
  82. data = (self.generate_data(shape)*(classes-1)).astype(np.int32)
  83. stra = [1]*len(shape)
  84. stra[0] = device_num
  85. datas = self.get_parallel_blocks(data, stra)
  86. return Tensor(data), Tensor(datas[rank_id])
  87. class OneHotFactory:
  88. def __init__(self, batch_size, classes, on_value=1.0, off_value=0.0, axis=None, strategy=None):
  89. dataGen = DataGenerator()
  90. self.input_full, self.input_part = dataGen.input_data((classes, batch_size))
  91. self.label_full, self.label_part = dataGen.label_data((batch_size,), classes)
  92. self.depth = classes
  93. self.on_value = on_value
  94. self.off_value = off_value
  95. self.axis = axis
  96. self.strategy = strategy
  97. def forward_mindspore_single_impl(self):
  98. net = Onehot(axis=self.axis,
  99. depth=self.depth,
  100. on_value=self.on_value,
  101. off_value=self.off_value)
  102. out = net(self.input_full, self.label_full)
  103. return out
  104. def forward_mindspore_parallel_impl(self):
  105. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  106. net = Onehot(axis=self.axis,
  107. depth=self.depth,
  108. on_value=self.on_value,
  109. off_value=self.off_value, strategy=self.strategy)
  110. out = net.compile_and_run(self.input_full, self.label_full)
  111. return out
  112. def forward_cmp(self):
  113. out_mindspore_single = self.forward_mindspore_single_impl().asnumpy()
  114. context.reset_auto_parallel_context()
  115. out_mindspore_parallel = self.forward_mindspore_parallel_impl().asnumpy()
  116. context.reset_auto_parallel_context()
  117. assert np.allclose(out_mindspore_single, out_mindspore_parallel, 0.0001, 0.0001)
  118. def test_reid_onehot_forward_int32_128_depth1024_model_parallel():
  119. fact = OneHotFactory(batch_size=128,
  120. classes=1024,
  121. on_value=1.000000,
  122. off_value=0.000000,
  123. axis=-1,
  124. strategy=((1, device_num), (), ()))
  125. fact.forward_cmp()
  126. def test_reid_onehot_forward_int32_1024_depth128_model_parallel():
  127. fact = OneHotFactory(batch_size=1024,
  128. classes=128,
  129. on_value=1.000000,
  130. off_value=0.000000,
  131. axis=-1,
  132. strategy=((1, device_num), (), ()))
  133. fact.forward_cmp()