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