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_onehot.py 5.7 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 as ms
  16. import mindspore.nn as nn
  17. from mindspore import Tensor
  18. from mindspore import context
  19. from mindspore.common.api import _executor
  20. from mindspore.ops import composite as C
  21. from mindspore.ops import operations as P
  22. from mindspore.nn.wrap.cell_wrapper import _VirtualDatasetCell
  23. context.set_context(mode=context.GRAPH_MODE)
  24. grad_all = C.GradOperation(get_all=True)
  25. class NetWithLoss(nn.Cell):
  26. def __init__(self, network, strategy3, strategy4, axis):
  27. super(NetWithLoss, self).__init__()
  28. self.one_hot = P.OneHot(axis=axis).shard(strategy3)
  29. self.on_value = Tensor(2.0, ms.float32)
  30. self.off_value = Tensor(1.0, ms.float32)
  31. self.loss = P.SoftmaxCrossEntropyWithLogits().shard(strategy4)
  32. self.network = network
  33. def construct(self, x, y, b):
  34. predict = self.network(x, y)
  35. label = self.one_hot(b, 64, self.on_value, self.off_value)
  36. return self.loss(predict, label)[0]
  37. class GradWrap(nn.Cell):
  38. def __init__(self, network):
  39. super(GradWrap, self).__init__()
  40. self.network = network
  41. def construct(self, x, y, b):
  42. return grad_all(self.network)(x, y, b)
  43. class Net(nn.Cell):
  44. def __init__(self, strategy1, strategy2):
  45. super().__init__()
  46. self.matmul = P.MatMul().shard(strategy1)
  47. self.gelu = P.GeLU().shard(strategy2)
  48. def construct(self, x, y):
  49. out = self.matmul(x, y)
  50. out = self.gelu(out)
  51. return out
  52. def compile_graph(strategy1, strategy2, strategy3, strategy4, auto=False, onthot_axis=-1):
  53. net = GradWrap(_VirtualDatasetCell(NetWithLoss(Net(strategy1, strategy2), strategy3, strategy4, axis=onthot_axis)))
  54. net.set_auto_parallel()
  55. if auto:
  56. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  57. else:
  58. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  59. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  60. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  61. b = Tensor(np.ones([64]), dtype=ms.int32)
  62. net.set_train()
  63. _executor.compile(net, x, y, b)
  64. def test_onehot_model_parallel():
  65. context.set_auto_parallel_context(device_num=16, global_rank=0)
  66. strategy1 = ((2, 4), (4, 2))
  67. strategy2 = ((2, 8),)
  68. strategy3 = ((1, 16), (), ())
  69. strategy4 = ((16, 1), (16, 1))
  70. compile_graph(strategy1, strategy2, strategy3, strategy4)
  71. def test_onehot_batch_parallel():
  72. context.set_auto_parallel_context(device_num=16, global_rank=0)
  73. strategy1 = ((2, 4), (4, 2))
  74. strategy2 = ((2, 8),)
  75. strategy3 = ((16, 1), (), ())
  76. strategy4 = ((16, 1), (16, 1))
  77. compile_graph(strategy1, strategy2, strategy3, strategy4)
  78. def test_onehot_batch_parallel_invalid_strategy():
  79. context.set_auto_parallel_context(device_num=16, global_rank=0)
  80. strategy1 = ((2, 4), (4, 2))
  81. strategy2 = ((2, 8),)
  82. strategy3 = ((16,), (), ())
  83. strategy4 = ((16, 1), (16, 1))
  84. try:
  85. compile_graph(strategy1, strategy2, strategy3, strategy4)
  86. except ValueError:
  87. pass
  88. except TypeError:
  89. pass
  90. except RuntimeError:
  91. pass
  92. def test_onehot_repeated_calculation():
  93. context.set_auto_parallel_context(device_num=16, global_rank=0)
  94. strategy1 = ((2, 4), (4, 2))
  95. strategy2 = ((2, 8),)
  96. strategy3 = ((4, 1), (), ())
  97. strategy4 = ((16, 1), (16, 1))
  98. compile_graph(strategy1, strategy2, strategy3, strategy4)
  99. def test_onehot_auto():
  100. context.set_auto_parallel_context(device_num=16, global_rank=0)
  101. strategy1 = None
  102. strategy2 = None
  103. strategy3 = None
  104. strategy4 = None
  105. compile_graph(strategy1, strategy2, strategy3, strategy4, auto=True)
  106. def test_onehot_batch_parallel_axis0():
  107. context.set_auto_parallel_context(device_num=16, global_rank=0)
  108. strategy1 = ((2, 4), (4, 2))
  109. strategy2 = ((2, 8),)
  110. strategy3 = ((16, 1), (), ())
  111. strategy4 = ((16, 1), (16, 1))
  112. compile_graph(strategy1, strategy2, strategy3, strategy4, onthot_axis=0)
  113. # auto parallel for onehot axis equal to 0 has not been supported yet
  114. def test_onehot_batch_parallel_invalid_strategy_axis0():
  115. context.set_auto_parallel_context(device_num=16, global_rank=0)
  116. strategy1 = ((2, 4), (4, 2))
  117. strategy2 = ((2, 8),)
  118. strategy3 = None
  119. strategy4 = ((16, 1), (16, 1))
  120. try:
  121. compile_graph(strategy1, strategy2, strategy3, strategy4, onthot_axis=0)
  122. except ValueError:
  123. pass
  124. except TypeError:
  125. pass
  126. except RuntimeError:
  127. pass
  128. def test_onehot_repeated_calculation_axis0():
  129. context.set_auto_parallel_context(device_num=16, global_rank=0)
  130. strategy1 = ((2, 4), (4, 2))
  131. strategy2 = ((2, 8),)
  132. strategy3 = ((4, 1), (), ())
  133. strategy4 = ((16, 1), (16, 1))
  134. compile_graph(strategy1, strategy2, strategy3, strategy4, onthot_axis=0)
  135. def test_onehot_auto_axis0():
  136. context.set_auto_parallel_context(device_num=16, global_rank=14)
  137. strategy1 = None
  138. strategy2 = None
  139. strategy3 = None
  140. strategy4 = None
  141. compile_graph(strategy1, strategy2, strategy3, strategy4, auto=True, onthot_axis=0)