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_add_relu_redistribution.py 2.6 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # Copyright 2020 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 AddRelu(nn.Cell):
  24. def __init__(self, strategy0=None, strategy1=None):
  25. super(AddRelu, self).__init__()
  26. self.add = P.TensorAdd().set_strategy(strategy=strategy0)
  27. self.relu = P.ReLU().set_strategy(strategy=strategy1)
  28. def construct(self, x, z):
  29. out = self.add(x, z)
  30. return self.relu(out)
  31. class NetWithLoss(nn.Cell):
  32. def __init__(self, network):
  33. super(NetWithLoss, self).__init__()
  34. self.loss = VirtualLoss()
  35. self.network = network
  36. def construct(self, x, z):
  37. predict = self.network(x, z)
  38. return self.loss(predict)
  39. class Grad(nn.Cell):
  40. def __init__(self, network):
  41. super(Grad, self).__init__()
  42. self.network = network
  43. def construct(self, x, y):
  44. return C.grad_all(self.network)(x, y)
  45. def test_add_relu_stride_slice():
  46. context.set_auto_parallel_context(device_num=8, global_rank=7)
  47. strategy0 = ((1, 1), (1, 1))
  48. strategy1 = ((8, 1), )
  49. net = Grad(NetWithLoss(AddRelu(strategy0, strategy1)))
  50. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  51. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  52. y = Tensor(np.ones([128, 32]), dtype=ms.float32)
  53. _executor.compile(net, x, y)
  54. def test_add_relu_all_gather():
  55. context.set_auto_parallel_context(device_num=8, global_rank=7)
  56. strategy0 = ((8, 1), (8, 1))
  57. strategy1 = ((1, 1), )
  58. net = Grad(NetWithLoss(AddRelu(strategy0, strategy1)))
  59. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  60. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  61. y = Tensor(np.ones([128, 32]), dtype=ms.float32)
  62. _executor.compile(net, x, y)