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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 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, strategy4=strategy4)
  77. net = GradWrap(net_with_loss)
  78. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  79. _executor.compile(net)
  80. def test_get_next_semi_auto_parallel1():
  81. class Net(nn.Cell):
  82. def __init__(self, channel=1, w=0.25, strategy1=None, strategy2=None):
  83. super().__init__()
  84. self.norm = P.L2Normalize().set_strategy(strategy1)
  85. self.prelu = P.PReLU().set_strategy(strategy2)
  86. self.w = Parameter(initializer(w, [channel,]), name='w')
  87. def construct(self, data):
  88. x = self.norm(data)
  89. x = self.prelu(x, self.w)
  90. return x
  91. context.set_auto_parallel_context(device_num=4, global_rank=0)
  92. network = Net(strategy1=((1,4), ), strategy2=((4,1),(1, )))
  93. strategy3 = ((1, 4),(),())
  94. strategy4=((4,1), (4,1))
  95. net_with_loss = NetWithLoss(network, [ms.float32, ms.int32],[[32,64], [32]], 2, strategy3=strategy3, strategy4=strategy4)
  96. net = GradWrap(net_with_loss)
  97. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  98. _executor.compile(net)
  99. def test_get_next_auto_parallel():
  100. class Net(nn.Cell):
  101. def __init__(self, channel=1, w=0.25, strategy1=None, strategy2=None):
  102. super().__init__()
  103. self.norm = P.L2Normalize().set_strategy(strategy1)
  104. self.prelu = P.PReLU().set_strategy(strategy2)
  105. self.w = Parameter(initializer(w, [channel,]), name='w')
  106. def construct(self, data):
  107. x = self.norm(data)
  108. x = self.prelu(x, self.w)
  109. return x
  110. context.set_auto_parallel_context(device_num=4, global_rank=0)
  111. network = Net()
  112. net_with_loss = NetWithLoss(network, [ms.float32, ms.int32],[[32,64], [32]], 2)
  113. net = GradWrap(net_with_loss)
  114. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  115. _executor.compile(net)
  116. def test_only_one_get_next():
  117. class Net(nn.Cell):
  118. def __init__(self):
  119. super().__init__()
  120. self.get_next = P.GetNext([ms.float32, ms.int32],[[32,64], [32]], 2, "")
  121. def construct(self):
  122. return self.get_next()
  123. context.set_auto_parallel_context(device_num=4, global_rank=0)
  124. net = Net()
  125. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  126. _executor.compile(net)