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_prelu.py 6.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. from mindspore import context
  16. import mindspore.nn as nn
  17. from mindspore.ops import operations as P
  18. from mindspore import Tensor
  19. from tests.ut.python.ops.test_math_ops import VirtualLoss
  20. import mindspore as ms
  21. from mindspore.common.api import _executor
  22. from mindspore.ops import composite as C
  23. class NetWithLoss(nn.Cell):
  24. def __init__(self, network):
  25. super(NetWithLoss, self).__init__()
  26. self.loss = VirtualLoss()
  27. self.network = network
  28. def construct(self, x, y):
  29. predict = self.network(x, y)
  30. return self.loss(predict)
  31. class GradWrap(nn.Cell):
  32. def __init__(self, network):
  33. super(GradWrap, self).__init__()
  34. self.network = network
  35. def construct(self, x, y):
  36. return C.grad_all(self.network)(x, y)
  37. def compile(net, x, y):
  38. net.set_auto_parallel()
  39. _executor.compile(net, x, y)
  40. def test_prelu_single_success1():
  41. class Net(nn.Cell):
  42. def __init__(self):
  43. super().__init__()
  44. self.prelu = P.PReLU()
  45. def construct(self, x, y):
  46. out = self.prelu(x, y)
  47. return out
  48. context.reset_auto_parallel_context()
  49. net = GradWrap(NetWithLoss(Net()))
  50. x = Tensor(np.random.rand(1, 33, 4, 4), ms.float32)
  51. w = Tensor(np.random.rand(33), ms.float32)
  52. compile(net, x, w)
  53. def test_prelu_single_success2():
  54. class Net(nn.Cell):
  55. def __init__(self):
  56. super().__init__()
  57. self.prelu = P.PReLU()
  58. def construct(self, x, y):
  59. out = self.prelu(x, y)
  60. return out
  61. context.reset_auto_parallel_context()
  62. net = GradWrap(NetWithLoss(Net()))
  63. x = Tensor(np.random.rand(1, 33, 4, 4), ms.float32)
  64. w = Tensor([0.1], ms.float32)
  65. compile(net, x, w)
  66. def test_prelu_parallel_success1():
  67. class Net(nn.Cell):
  68. def __init__(self, strategy):
  69. super().__init__()
  70. self.prelu = P.PReLU().set_strategy(strategy)
  71. def construct(self, x, y):
  72. out = self.prelu(x, y)
  73. return out
  74. context.reset_auto_parallel_context()
  75. context.set_auto_parallel_context(device_num=8, global_rank=0)
  76. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  77. strategy = ((1, 1, 1, 1), (1, ))
  78. x = Tensor(np.random.rand(4, 4, 32, 64),dtype=ms.float32)
  79. w = Tensor(np.random.rand(4),dtype=ms.float32)
  80. net = GradWrap(NetWithLoss(Net(strategy)))
  81. compile(net, x, w)
  82. def test_prelu_parallel_success2():
  83. class Net(nn.Cell):
  84. def __init__(self, strategy):
  85. super().__init__()
  86. self.prelu = P.PReLU().set_strategy(strategy)
  87. def construct(self, x, y):
  88. out = self.prelu(x, y)
  89. return out
  90. context.reset_auto_parallel_context()
  91. context.set_auto_parallel_context(device_num=64, global_rank=0)
  92. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  93. strategy = ((2, 1, 4, 8), (1, ))
  94. x = Tensor(np.random.rand(4, 4, 32, 64),dtype=ms.float32)
  95. w = Tensor(np.random.rand(4),dtype=ms.float32)
  96. net = GradWrap(NetWithLoss(Net(strategy)))
  97. compile(net, x, w)
  98. def test_prelu_parallel_success3():
  99. class NetWithLoss(nn.Cell):
  100. def __init__(self, network):
  101. super(NetWithLoss, self).__init__()
  102. self.loss = VirtualLoss()
  103. self.network = network
  104. def construct(self, x, y, w):
  105. predict = self.network(x, y, w)
  106. return self.loss(predict)
  107. class GradWrap(nn.Cell):
  108. def __init__(self, network):
  109. super(GradWrap, self).__init__()
  110. self.network = network
  111. def construct(self, x, y, w):
  112. return C.grad_all(self.network)(x, y, w)
  113. class Net(nn.Cell):
  114. def __init__(self, strategy1, strategy2):
  115. super().__init__()
  116. self.matmul = P.MatMul().set_strategy(strategy1)
  117. self.prelu = P.PReLU().set_strategy(strategy2)
  118. def construct(self, x, y, w):
  119. out = self.matmul(x, y)
  120. out = self.prelu(out, w)
  121. return out
  122. context.reset_auto_parallel_context()
  123. context.set_auto_parallel_context(device_num=64, global_rank=0)
  124. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  125. strategy1 = ((2, 4), (4, 2))
  126. strategy2 = ((32, 1), (1, ))
  127. x = Tensor(np.random.rand(128, 64),dtype=ms.float32)
  128. y = Tensor(np.random.rand(64, 16),dtype=ms.float32)
  129. w = Tensor(np.random.rand(16),dtype=ms.float32)
  130. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  131. net.set_auto_parallel()
  132. _executor.compile(net, x, y, w)
  133. def test_prelu_parallel_success4():
  134. class Net(nn.Cell):
  135. def __init__(self, strategy):
  136. super().__init__()
  137. self.prelu = P.PReLU().set_strategy(strategy)
  138. def construct(self, x, y):
  139. out = self.prelu(x, y)
  140. return out
  141. context.reset_auto_parallel_context()
  142. context.set_auto_parallel_context(device_num=64, global_rank=0)
  143. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  144. strategy = ((2, 4, 4, 2), (4, ))
  145. x = Tensor(np.random.rand(4, 16, 32, 64),dtype=ms.float32)
  146. w = Tensor(np.random.rand(16),dtype=ms.float32)
  147. net = GradWrap(NetWithLoss(Net(strategy)))
  148. compile(net, x, w)
  149. def test_prelu_parallel_success5():
  150. class Net(nn.Cell):
  151. def __init__(self, strategy):
  152. super().__init__()
  153. self.prelu = P.PReLU().set_strategy(strategy)
  154. def construct(self, x, y):
  155. out = self.prelu(x, y)
  156. return out
  157. context.reset_auto_parallel_context()
  158. context.set_auto_parallel_context(device_num=64, global_rank=0)
  159. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  160. strategy = ((2, 4, 4, 2), (1, ))
  161. x = Tensor(np.random.rand(4, 16, 32, 64),dtype=ms.float32)
  162. w = Tensor(np.random.rand(1),dtype=ms.float32)
  163. net = GradWrap(NetWithLoss(Net(strategy)))
  164. compile(net, x, w)