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