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_batch_parallel.py 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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, w1, w2):
  29. predict = self.network(x, w1, w2)
  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, w1, w2):
  36. return C.grad_all(self.network)(x, w1, w2)
  37. class NetConv(nn.Cell):
  38. def __init__(self,
  39. cin,
  40. cout,
  41. kernel_size,
  42. stride=1,
  43. pad_mode='pad',
  44. padding=0,
  45. dilation=1,
  46. group=1,
  47. has_bias=False,
  48. weight_init='normal',
  49. bias_init='zeros',
  50. strategy=None):
  51. super(NetConv, self).__init__()
  52. self.conv = nn.Conv2d(cin,
  53. cout,
  54. kernel_size,
  55. stride,
  56. pad_mode,
  57. padding,
  58. dilation,
  59. group,
  60. has_bias,
  61. weight_init,
  62. bias_init)
  63. self.conv.conv2d.set_strategy(strategy)
  64. def construct(self, input_x):
  65. return self.conv(input_x)
  66. def test_batch():
  67. class Net(nn.Cell):
  68. def __init__(self, strategy1, strategy2, strategy3):
  69. super().__init__()
  70. self.conv1 = NetConv(16, 8, (3, 3), bias_init='zeros', strategy=strategy1)
  71. self.mul1 = P.Mul().set_strategy(strategy2)
  72. self.conv2 = NetConv(8, 64, (9, 9), bias_init='zeros', strategy=strategy1)
  73. self.mul2 = P.Mul().set_strategy(strategy3)
  74. def construct(self, x, w1, w2):
  75. out1 = self.conv1(x)
  76. out2 = self.mul1(out1, w1)
  77. out3 = self.conv2(out2)
  78. out4 = self.mul2(out3, w2)
  79. return out4
  80. context.set_auto_parallel_context(device_num=8, global_rank=0)
  81. strategy1 = ((8, 1, 1, 1), (1, 1, 1, 1))
  82. strategy2 = ((1, 1, 1, 8), (1, 1, 1, 8))
  83. strategy3 = ((4, 1, 1, 2), (4, 1, 1, 2))
  84. net = GradWrap(NetWithLoss(Net(strategy1, strategy2, strategy3)))
  85. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  86. net.set_auto_parallel()
  87. x = Tensor(np.ones([128, 16, 34, 34]), dtype=ms.float32)
  88. w1 = Tensor(np.ones([128, 8, 32, 32]), dtype=ms.float32)
  89. w2 = Tensor(np.ones([128, 64, 24, 24]), dtype=ms.float32)
  90. _executor.compile(net, x, w1, w2)
  91. if __name__ == '__main__':
  92. test_batch()