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.8 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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.ops.operations.comm_ops import _VirtualDataset
  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.virtual_dataset = _VirtualDataset()
  29. self.one_hot = P.OneHot(axis=axis).shard(strategy3)
  30. self.on_value = Tensor(2.0, ms.float32)
  31. self.off_value = Tensor(1.0, ms.float32)
  32. self.loss = P.SoftmaxCrossEntropyWithLogits().shard(strategy4)
  33. self.network = network
  34. def construct(self, x, y, b):
  35. b_virtual = self.virtual_dataset(b)
  36. predict = self.network(x, y)
  37. label = self.one_hot(b_virtual, 64, self.on_value, self.off_value)
  38. return self.loss(predict, label)[0]
  39. class GradWrap(nn.Cell):
  40. def __init__(self, network):
  41. super(GradWrap, self).__init__()
  42. self.network = network
  43. def construct(self, x, y, b):
  44. return grad_all(self.network)(x, y, b)
  45. class Net(nn.Cell):
  46. def __init__(self, strategy1, strategy2):
  47. super().__init__()
  48. self.matmul = P.MatMul().shard(strategy1)
  49. self.gelu = P.Gelu().shard(strategy2)
  50. def construct(self, x, y):
  51. out = self.matmul(x, y)
  52. out = self.gelu(out)
  53. return out
  54. def compile_graph(strategy1, strategy2, strategy3, strategy4, auto=False, onthot_axis=-1):
  55. net = GradWrap(NetWithLoss(Net(strategy1, strategy2), strategy3, strategy4, axis=onthot_axis))
  56. net.set_auto_parallel()
  57. if auto:
  58. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  59. else:
  60. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  61. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  62. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  63. b = Tensor(np.ones([64]), dtype=ms.int32)
  64. net.set_train()
  65. _executor.compile(net, x, y, b)
  66. def test_onehot_model_parallel():
  67. context.set_auto_parallel_context(device_num=16, global_rank=0)
  68. strategy1 = ((2, 4), (4, 2))
  69. strategy2 = ((2, 8),)
  70. strategy3 = ((1, 16), (), ())
  71. strategy4 = ((16, 1), (16, 1))
  72. compile_graph(strategy1, strategy2, strategy3, strategy4)
  73. def test_onehot_batch_parallel():
  74. context.set_auto_parallel_context(device_num=16, global_rank=0)
  75. strategy1 = ((2, 4), (4, 2))
  76. strategy2 = ((2, 8),)
  77. strategy3 = ((16, 1), (), ())
  78. strategy4 = ((16, 1), (16, 1))
  79. compile_graph(strategy1, strategy2, strategy3, strategy4)
  80. def test_onehot_batch_parallel_invalid_strategy():
  81. context.set_auto_parallel_context(device_num=16, global_rank=0)
  82. strategy1 = ((2, 4), (4, 2))
  83. strategy2 = ((2, 8),)
  84. strategy3 = ((16,), (), ())
  85. strategy4 = ((16, 1), (16, 1))
  86. try:
  87. compile_graph(strategy1, strategy2, strategy3, strategy4)
  88. except ValueError:
  89. pass
  90. except TypeError:
  91. pass
  92. except RuntimeError:
  93. pass
  94. def test_onehot_repeated_calculation():
  95. context.set_auto_parallel_context(device_num=16, global_rank=0)
  96. strategy1 = ((2, 4), (4, 2))
  97. strategy2 = ((2, 8),)
  98. strategy3 = ((4, 1), (), ())
  99. strategy4 = ((16, 1), (16, 1))
  100. compile_graph(strategy1, strategy2, strategy3, strategy4)
  101. def test_onehot_auto():
  102. context.set_auto_parallel_context(device_num=16, global_rank=0)
  103. strategy1 = None
  104. strategy2 = None
  105. strategy3 = None
  106. strategy4 = None
  107. compile_graph(strategy1, strategy2, strategy3, strategy4, auto=True)
  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 ValueError:
  125. pass
  126. except TypeError:
  127. pass
  128. except RuntimeError:
  129. pass
  130. def test_onehot_repeated_calculation_axis0():
  131. context.set_auto_parallel_context(device_num=16, global_rank=0)
  132. strategy1 = ((2, 4), (4, 2))
  133. strategy2 = ((2, 8),)
  134. strategy3 = ((4, 1), (), ())
  135. strategy4 = ((16, 1), (16, 1))
  136. compile_graph(strategy1, strategy2, strategy3, strategy4, onthot_axis=0)
  137. def test_onehot_auto_axis0():
  138. context.set_auto_parallel_context(device_num=16, global_rank=14)
  139. strategy1 = None
  140. strategy2 = None
  141. strategy3 = None
  142. strategy4 = None
  143. compile_graph(strategy1, strategy2, strategy3, strategy4, auto=True, onthot_axis=0)