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 5.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 test_prelu_single_success1():
  38. class Net(nn.Cell):
  39. def __init__(self):
  40. super().__init__()
  41. self.prelu = P.PReLU()
  42. def construct(self, x, y):
  43. out = self.prelu(x, y)
  44. return out
  45. context.reset_auto_parallel_context()
  46. net = GradWrap(NetWithLoss(Net()))
  47. x = Tensor(np.random.rand(1, 33, 4, 4), ms.float32)
  48. w = Tensor(np.random.rand(33), ms.float32)
  49. _executor.compile(net, x, w)
  50. def test_prelu_single_success2():
  51. class Net(nn.Cell):
  52. def __init__(self):
  53. super().__init__()
  54. self.prelu = P.PReLU()
  55. def construct(self, x, y):
  56. out = self.prelu(x, y)
  57. return out
  58. context.reset_auto_parallel_context()
  59. net = GradWrap(NetWithLoss(Net()))
  60. x = Tensor(np.random.rand(1, 33, 4, 4), ms.float32)
  61. w = Tensor([0.1], ms.float32)
  62. _executor.compile(net, x, w)
  63. def test_prelu_parallel_success1():
  64. class Net(nn.Cell):
  65. def __init__(self, strategy):
  66. super().__init__()
  67. self.prelu = P.PReLU().set_strategy(strategy)
  68. def construct(self, x, y):
  69. out = self.prelu(x, y)
  70. return out
  71. context.reset_auto_parallel_context()
  72. context.set_auto_parallel_context(device_num=8, global_rank=0)
  73. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  74. strategy = ((1, 1, 1, 1), (1, ))
  75. x = Tensor(np.random.rand(4, 4, 32, 64),dtype=ms.float32)
  76. w = Tensor(np.random.rand(4),dtype=ms.float32)
  77. net = GradWrap(NetWithLoss(Net(strategy)))
  78. _executor.compile(net, x, w)
  79. def test_prelu_parallel_success2():
  80. class Net(nn.Cell):
  81. def __init__(self, strategy):
  82. super().__init__()
  83. self.prelu = P.PReLU().set_strategy(strategy)
  84. def construct(self, x, y):
  85. out = self.prelu(x, y)
  86. return out
  87. context.reset_auto_parallel_context()
  88. context.set_auto_parallel_context(device_num=64, global_rank=0)
  89. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  90. strategy = ((2, 1, 4, 8), (1, ))
  91. x = Tensor(np.random.rand(4, 4, 32, 64),dtype=ms.float32)
  92. w = Tensor(np.random.rand(4),dtype=ms.float32)
  93. net = GradWrap(NetWithLoss(Net(strategy)))
  94. _executor.compile(net, x, w)
  95. def test_prelu_parallel_success3():
  96. class NetWithLoss(nn.Cell):
  97. def __init__(self, network):
  98. super(NetWithLoss, self).__init__()
  99. self.loss = VirtualLoss()
  100. self.network = network
  101. def construct(self, x, y, w):
  102. predict = self.network(x, y, w)
  103. return self.loss(predict)
  104. class GradWrap(nn.Cell):
  105. def __init__(self, network):
  106. super(GradWrap, self).__init__()
  107. self.network = network
  108. def construct(self, x, y, w):
  109. return C.grad_all(self.network)(x, y, w)
  110. class Net(nn.Cell):
  111. def __init__(self, strategy1, strategy2):
  112. super().__init__()
  113. self.matmul = P.MatMul().set_strategy(strategy1)
  114. self.prelu = P.PReLU().set_strategy(strategy2)
  115. def construct(self, x, y, w):
  116. out = self.matmul(x, y)
  117. out = self.prelu(out, w)
  118. return out
  119. context.reset_auto_parallel_context()
  120. context.set_auto_parallel_context(device_num=64, global_rank=0)
  121. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  122. strategy1 = ((2, 4), (4, 2))
  123. strategy2 = ((32, 1), (1, ))
  124. x = Tensor(np.random.rand(128, 64),dtype=ms.float32)
  125. y = Tensor(np.random.rand(64, 16),dtype=ms.float32)
  126. w = Tensor(np.random.rand(16),dtype=ms.float32)
  127. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  128. _executor.compile(net, x, y, w)