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.8 kB

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