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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. 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. _executor.compile(net, x, y, b)
  63. def test_onehot_model_parallel():
  64. context.set_auto_parallel_context(device_num=16, global_rank=0)
  65. strategy1 = ((2, 4), (4, 2))
  66. strategy2 = ((2, 8), )
  67. strategy3 = ((1, 16), (), ())
  68. strategy4 = ((16, 1), (16, 1))
  69. compile_graph(strategy1, strategy2, strategy3, strategy4)
  70. def test_onehot_batch_parallel():
  71. context.set_auto_parallel_context(device_num=16, global_rank=0)
  72. strategy1 = ((2, 4), (4, 2))
  73. strategy2 = ((2, 8), )
  74. strategy3 = ((16, 1), (), ())
  75. strategy4 = ((16, 1), (16, 1))
  76. compile_graph(strategy1, strategy2, strategy3, strategy4)
  77. def test_onehot_batch_parallel_invalid_strategy():
  78. context.set_auto_parallel_context(device_num=16, global_rank=0)
  79. strategy1 = ((2, 4), (4, 2))
  80. strategy2 = ((2, 8), )
  81. strategy3 = ((16, ), (), ())
  82. strategy4 = ((16, 1), (16, 1))
  83. try:
  84. compile_graph(strategy1, strategy2, strategy3, strategy4)
  85. except:
  86. pass
  87. def test_onehot_repeated_calculation():
  88. context.set_auto_parallel_context(device_num=16, global_rank=0)
  89. strategy1 = ((2, 4), (4, 2))
  90. strategy2 = ((2, 8), )
  91. strategy3 = ((4, 1), (), ())
  92. strategy4 = ((16, 1), (16, 1))
  93. compile_graph(strategy1, strategy2, strategy3, strategy4)
  94. def test_onehot_auto():
  95. context.set_auto_parallel_context(device_num=16, global_rank=0)
  96. strategy1 = None
  97. strategy2 = None
  98. strategy3 = None
  99. strategy4 = None
  100. compile_graph(strategy1, strategy2, strategy3, strategy4, auto=True)
  101. def test_onehot_model_parallel():
  102. context.set_auto_parallel_context(device_num=16, global_rank=0)
  103. strategy1 = ((2, 4), (4, 2))
  104. strategy2 = ((2, 8), )
  105. strategy3 = ((1, 16), (), ())
  106. strategy4 = ((16, 1), (16, 1))
  107. compile_graph(strategy1, strategy2, strategy3, strategy4)
  108. def test_onehot_batch_parallel_axis0():
  109. context.set_auto_parallel_context(device_num=16, global_rank=0)
  110. strategy1 = ((2, 4), (4, 2))
  111. strategy2 = ((2, 8), )
  112. strategy3 = ((16, 1), (), ())
  113. strategy4 = ((16, 1), (16, 1))
  114. compile_graph(strategy1, strategy2, strategy3, strategy4, onthot_axis=0)
  115. # auto parallel for onehot axis equal to 0 has not been supported yet
  116. def test_onehot_batch_parallel_invalid_strategy_axis0():
  117. context.set_auto_parallel_context(device_num=16, global_rank=0)
  118. strategy1 = ((2, 4), (4, 2))
  119. strategy2 = ((2, 8), )
  120. strategy3 = None
  121. strategy4 = ((16, 1), (16, 1))
  122. try:
  123. compile_graph(strategy1, strategy2, strategy3, strategy4, onthot_axis=0)
  124. except:
  125. pass
  126. def test_onehot_repeated_calculation_axis0():
  127. context.set_auto_parallel_context(device_num=16, global_rank=0)
  128. strategy1 = ((2, 4), (4, 2))
  129. strategy2 = ((2, 8), )
  130. strategy3 = ((4, 1), (), ())
  131. strategy4 = ((16, 1), (16, 1))
  132. compile_graph(strategy1, strategy2, strategy3, strategy4, onthot_axis=0)
  133. def test_onehot_auto_axis0():
  134. context.set_auto_parallel_context(device_num=16, global_rank=14)
  135. strategy1 = None
  136. strategy2 = None
  137. strategy3 = None
  138. strategy4 = None
  139. compile_graph(strategy1, strategy2, strategy3, strategy4, auto=True, onthot_axis=0)