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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.nn as nn
  16. from mindspore.ops import operations as P
  17. from mindspore import Tensor
  18. from tests.ut.python.ops.test_math_ops import VirtualLoss
  19. import mindspore as ms
  20. from mindspore.common.api import _executor
  21. from mindspore.ops import composite as C
  22. from mindspore.common.parameter import Parameter,ParameterTuple
  23. from mindspore.ops.operations.comm_ops import _VirtualDataset
  24. from mindspore import context
  25. from mindspore.common.initializer import initializer
  26. context.set_context(mode=context.GRAPH_MODE)
  27. class NetWithLoss(nn.Cell):
  28. def __init__(self, network, types, shapes, output_num, strategy3=None, strategy4=None, axis=-1):
  29. super(NetWithLoss, self).__init__()
  30. self.get_next = P.GetNext(types, shapes, output_num, "")
  31. self.one_hot = P.OneHot(axis=axis).set_strategy(strategy3)
  32. self.on_value = Tensor(1.0, ms.float32)
  33. self.off_value = Tensor(0.0, ms.float32)
  34. self.loss = P.SoftmaxCrossEntropyWithLogits().set_strategy(strategy4)
  35. self.network = network
  36. def construct(self):
  37. data, label = self.get_next()
  38. predict = self.network(data)
  39. label = self.one_hot(label, 64, self.on_value, self.off_value)
  40. return self.loss(predict, label)[0]
  41. class GradWrap(nn.Cell):
  42. def __init__(self, network):
  43. super(GradWrap, self).__init__()
  44. self.network = network
  45. self.weights = ParameterTuple(network.trainable_params())
  46. def construct(self):
  47. return C.grad_by_list(self.network, self.weights)()
  48. def compile(net):
  49. net.set_auto_parallel()
  50. _executor.compile(net)
  51. def test_get_next_single():
  52. class Net(nn.Cell):
  53. def __init__(self, channel=1, w=0.25):
  54. super().__init__()
  55. self.norm = P.L2Normalize(axis=1)
  56. self.prelu = P.PReLU()
  57. self.w = Parameter(initializer(w, [channel,]), name='w')
  58. def construct(self, data):
  59. x = self.norm(data)
  60. x = self.prelu(x, self.w)
  61. return x
  62. net = GradWrap(NetWithLoss(Net(), [ms.float32, ms.int32],[[32,64], [32]], 2))
  63. _executor.compile(net)
  64. def test_get_next_semi_auto_parallel():
  65. class Net(nn.Cell):
  66. def __init__(self, channel=1, w=0.25, strategy1=None, strategy2=None):
  67. super().__init__()
  68. self.norm = P.L2Normalize().set_strategy(strategy1)
  69. self.prelu = P.PReLU().set_strategy(strategy2)
  70. self.w = Parameter(initializer(w, [channel,]), name='w')
  71. def construct(self, data):
  72. x = self.norm(data)
  73. x = self.prelu(x, self.w)
  74. return x
  75. context.set_auto_parallel_context(device_num=4, global_rank=0)
  76. network = Net(strategy1=((1,4), ), strategy2=((4,1),(1, )))
  77. strategy3 = ((4, 1),(),())
  78. strategy4=((4,1), (4,1))
  79. net_with_loss = NetWithLoss(network, [ms.float32, ms.int32],[[32,64], [32]], 2, strategy3=strategy3, strategy4=strategy4)
  80. net = GradWrap(net_with_loss)
  81. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  82. compile(net)
  83. def test_get_next_semi_auto_parallel1():
  84. class Net(nn.Cell):
  85. def __init__(self, channel=1, w=0.25, strategy1=None, strategy2=None):
  86. super().__init__()
  87. self.norm = P.L2Normalize().set_strategy(strategy1)
  88. self.prelu = P.PReLU().set_strategy(strategy2)
  89. self.w = Parameter(initializer(w, [channel,]), name='w')
  90. def construct(self, data):
  91. x = self.norm(data)
  92. x = self.prelu(x, self.w)
  93. return x
  94. context.set_auto_parallel_context(device_num=4, global_rank=0)
  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, strategy4=strategy4)
  99. net = GradWrap(net_with_loss)
  100. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  101. compile(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().set_strategy(strategy1)
  107. self.prelu = P.PReLU().set_strategy(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. network = Net()
  115. net_with_loss = NetWithLoss(network, [ms.float32, ms.int32],[[32,64], [32]], 2)
  116. net = GradWrap(net_with_loss)
  117. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  118. compile(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. net = Net()
  128. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  129. compile(net)