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_get_next.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
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 mindspore as ms
  15. import mindspore.nn as nn
  16. from mindspore import Tensor
  17. from mindspore import context
  18. from mindspore.common.api import _executor
  19. from mindspore.common.initializer import initializer
  20. from mindspore.common.parameter import Parameter, ParameterTuple
  21. from mindspore.ops import composite as C
  22. from mindspore.ops import operations as P
  23. context.set_context(mode=context.GRAPH_MODE)
  24. grad_by_list = C.GradOperation(get_by_list=True)
  25. class NetWithLoss(nn.Cell):
  26. def __init__(self, network, types, shapes, output_num, strategy3=None, strategy4=None, axis=-1):
  27. super(NetWithLoss, self).__init__()
  28. self.get_next = P.GetNext(types, shapes, output_num, "")
  29. self.one_hot = P.OneHot(axis=axis).shard(strategy3)
  30. self.on_value = Tensor(1.0, ms.float32)
  31. self.off_value = Tensor(0.0, ms.float32)
  32. self.loss = P.SoftmaxCrossEntropyWithLogits().shard(strategy4)
  33. self.network = network
  34. def construct(self):
  35. data, label = self.get_next()
  36. predict = self.network(data)
  37. label = self.one_hot(label, 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. self.weights = ParameterTuple(network.trainable_params())
  44. def construct(self):
  45. return grad_by_list(self.network, self.weights)()
  46. def compile_net(net):
  47. net.set_auto_parallel()
  48. _executor.compile(net)
  49. def test_get_next_single():
  50. class Net(nn.Cell):
  51. def __init__(self, channel=1, w=0.25):
  52. super().__init__()
  53. self.norm = P.L2Normalize(axis=1)
  54. self.prelu = P.PReLU()
  55. self.w = Parameter(initializer(w, [channel,]), name='w')
  56. def construct(self, data):
  57. x = self.norm(data)
  58. x = self.prelu(x, self.w)
  59. return x
  60. net = GradWrap(NetWithLoss(Net(), [ms.float32, ms.int32], [[32, 64], [32]], 2))
  61. _executor.compile(net)
  62. def test_get_next_semi_auto_parallel():
  63. class Net(nn.Cell):
  64. def __init__(self, channel=1, w=0.25, strategy1=None, strategy2=None):
  65. super().__init__()
  66. self.norm = P.L2Normalize().shard(strategy1)
  67. self.prelu = P.PReLU().shard(strategy2)
  68. self.w = Parameter(initializer(w, [channel,]), name='w')
  69. def construct(self, data):
  70. x = self.norm(data)
  71. x = self.prelu(x, self.w)
  72. return x
  73. context.set_auto_parallel_context(device_num=4, global_rank=0)
  74. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  75. network = Net(strategy1=((1, 4),), strategy2=((4, 1), (1,)))
  76. strategy3 = ((4, 1), (), ())
  77. strategy4 = ((4, 1), (4, 1))
  78. net_with_loss = NetWithLoss(network, [ms.float32, ms.int32], [[32, 64], [32]], 2, strategy3=strategy3,
  79. strategy4=strategy4)
  80. net = GradWrap(net_with_loss)
  81. compile_net(net)
  82. def test_get_next_semi_auto_parallel1():
  83. class Net(nn.Cell):
  84. def __init__(self, channel=1, w=0.25, strategy1=None, strategy2=None):
  85. super().__init__()
  86. self.norm = P.L2Normalize().shard(strategy1)
  87. self.prelu = P.PReLU().shard(strategy2)
  88. self.w = Parameter(initializer(w, [channel,]), name='w')
  89. def construct(self, data):
  90. x = self.norm(data)
  91. x = self.prelu(x, self.w)
  92. return x
  93. context.set_auto_parallel_context(device_num=4, global_rank=0)
  94. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  95. network = Net(strategy1=((1, 4),), strategy2=((4, 1), (1,)))
  96. strategy3 = ((1, 4), (), ())
  97. strategy4 = ((4, 1), (4, 1))
  98. net_with_loss = NetWithLoss(network, [ms.float32, ms.int32], [[32, 64], [32]], 2, strategy3=strategy3,
  99. strategy4=strategy4)
  100. net = GradWrap(net_with_loss)
  101. compile_net(net)
  102. def test_get_next_auto_parallel():
  103. class Net(nn.Cell):
  104. def __init__(self, channel=1, w=0.25, strategy1=None, strategy2=None):
  105. super().__init__()
  106. self.norm = P.L2Normalize().shard(strategy1)
  107. self.prelu = P.PReLU().shard(strategy2)
  108. self.w = Parameter(initializer(w, [channel,]), name='w')
  109. def construct(self, data):
  110. x = self.norm(data)
  111. x = self.prelu(x, self.w)
  112. return x
  113. context.set_auto_parallel_context(device_num=4, global_rank=0)
  114. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  115. network = Net()
  116. net_with_loss = NetWithLoss(network, [ms.float32, ms.int32], [[32, 64], [32]], 2)
  117. net = GradWrap(net_with_loss)
  118. compile_net(net)
  119. def test_only_one_get_next():
  120. class Net(nn.Cell):
  121. def __init__(self):
  122. super().__init__()
  123. self.get_next = P.GetNext([ms.float32, ms.int32], [[32, 64], [32]], 2, "")
  124. def construct(self):
  125. return self.get_next()
  126. context.set_auto_parallel_context(device_num=4, global_rank=0)
  127. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  128. net = Net()
  129. compile_net(net)