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_pipeline_parallel.py 3.2 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. # ============================================================================
  15. import numpy as np
  16. import mindspore as ms
  17. import mindspore.nn as nn
  18. from mindspore import Tensor
  19. from mindspore import context
  20. from mindspore.common.api import _executor
  21. from mindspore.ops import composite as C
  22. from mindspore.ops import operations as P
  23. from tests.ut.python.ops.test_math_ops import VirtualLoss
  24. grad_all = C.GradOperation(get_all=True)
  25. class NetWithLoss(nn.Cell):
  26. def __init__(self, network):
  27. super(NetWithLoss, self).__init__()
  28. self.loss = VirtualLoss()
  29. self.network = network
  30. def construct(self, x, y):
  31. predict = self.network(x, y)
  32. return self.loss(predict)
  33. class GradWrap(nn.Cell):
  34. def __init__(self, network):
  35. super(GradWrap, self).__init__()
  36. self.network = network
  37. def construct(self, x, y):
  38. return grad_all(self.network)(x, y)
  39. class Net(nn.Cell):
  40. def __init__(self, axis=0, stage1=0, stage2=0, strategy1=None, strategy2=None, shape=None, target=""):
  41. super().__init__()
  42. if shape is None:
  43. shape = [64, 64]
  44. self.gatherv2 = P.GatherV2().shard(strategy1).add_prim_attr("primitive_target", target)
  45. self.mul = P.Mul().shard(strategy2)
  46. self.index = Tensor(np.ones(shape), dtype=ms.int32)
  47. self.gatherv2.set_stage(stage1)
  48. self.mul.set_stage(stage2)
  49. self.axis = axis
  50. def construct(self, x, y):
  51. out = self.gatherv2(x, self.index, self.axis)
  52. out = self.mul(out, y)
  53. return out
  54. def test_gatherv2_semi_samestage1():
  55. context.set_auto_parallel_context(device_num=8, global_rank=0, \
  56. parallel_mode="semi_auto_parallel", pipeline_stages=2)
  57. strategy1 = ((1, 2), (1, 1))
  58. strategy2 = ((2, 1, 1), (2, 1, 1))
  59. net = GradWrap(NetWithLoss(Net(0, 0, 0, strategy1, strategy2)))
  60. net.set_auto_parallel()
  61. x = Tensor(np.ones([64, 64]), dtype=ms.float32)
  62. y = Tensor(np.ones([64, 64, 64]), dtype=ms.float32)
  63. net.set_train()
  64. _executor.compile(net, x, y)
  65. def test_gatherv2_semi_samestage2():
  66. context.set_auto_parallel_context(device_num=8, global_rank=5, \
  67. parallel_mode="semi_auto_parallel", pipeline_stages=2)
  68. strategy1 = ((1, 2), (1, 1))
  69. strategy2 = ((2, 1, 1), (2, 1, 1))
  70. net = GradWrap(NetWithLoss(Net(0, 1, 1, strategy1, strategy2)))
  71. net.set_auto_parallel()
  72. x = Tensor(np.ones([64, 64]), dtype=ms.float32)
  73. y = Tensor(np.ones([64, 64, 64]), dtype=ms.float32)
  74. net.set_train()
  75. _executor.compile(net, x, y)