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_auto_parallel_cast.py 3.0 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 import dtype as mstype
  22. from mindspore.common.api import _executor
  23. from mindspore.ops import composite as C
  24. from mindspore.parallel._utils import _reset_op_id as reset_op_id
  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, z, w):
  31. predict = self.network(x, y, z, w)
  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, z, w):
  38. return C.grad_all(self.network)(x, y, z, w)
  39. # model_parallel test
  40. def test_double_star_graph():
  41. class Net(nn.Cell):
  42. def __init__(self):
  43. super().__init__()
  44. self.matmul1 = P.MatMul()
  45. self.matmul2 = P.MatMul()
  46. self.matmul3 = P.MatMul()
  47. self.cast1 = P.Cast()
  48. self.cast2 = P.Cast()
  49. def construct(self, x, y, z, w):
  50. m1_result = self.matmul1(x, y)
  51. m2_result = self.matmul2(z, w)
  52. m3_result = self.matmul3(self.cast1(m2_result, mstype.float16), self.cast2(m1_result, mstype.float16))
  53. return m3_result
  54. size = 8
  55. context.set_auto_parallel_context(device_num=size, global_rank=0)
  56. x = Tensor(np.ones([32, 8]), dtype=ms.float32)
  57. y = Tensor(np.ones([8, 16]), dtype=ms.float32)
  58. z = Tensor(np.ones([8, 16]), dtype=ms.float32)
  59. w = Tensor(np.ones([16, 32]), dtype=ms.float32)
  60. net = NetWithLoss(Net())
  61. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  62. reset_op_id()
  63. _executor.compile(net, x, y, z, w, phase='train')
  64. strategies = _executor._get_strategy(net)
  65. expected_strategies = {'Default/network-Net/Cast-op1': [[8, 1]],
  66. 'Default/network-Net/Cast-op3': [[1, 8]],
  67. 'Default/network-Net/MatMul-op2': [[8, 1], [1, 1]],
  68. 'Default/network-Net/MatMul-op4': [[1, 1], [1, 8]],
  69. 'Default/network-Net/MatMul-op0': [[1, 8], [8, 1]]}
  70. assert strategies == expected_strategies