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

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