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_forward_graph.py 2.9 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. # ============================================================================
  15. import numpy as np
  16. import mindspore as ms
  17. from mindspore import context, Tensor, Parameter
  18. from mindspore.common.api import _executor
  19. from mindspore.nn import Cell
  20. from mindspore.ops import operations as P
  21. class Net(Cell):
  22. def __init__(self, mul_weight, strategy1=None, strategy2=None):
  23. super().__init__()
  24. self.mul = P.Mul().set_strategy(strategy1)
  25. self.neg = P.Neg().set_strategy(strategy2)
  26. self.mul_weight = Parameter(mul_weight, "w1")
  27. def construct(self, x, b):
  28. out = self.mul(x, self.mul_weight)
  29. out = self.neg(out)
  30. return out, b
  31. _x = Tensor(np.ones([128, 64, 32]), dtype=ms.float32)
  32. _w1 = Tensor(np.ones([128, 64, 32]), dtype=ms.float32)
  33. _b = Tensor(np.ones([128, 64, 32]), dtype=ms.float32)
  34. def compile_net(net):
  35. net.set_auto_parallel()
  36. _executor.compile(net, _x, _b)
  37. context.reset_auto_parallel_context()
  38. def test_forward_graph_data_parallel():
  39. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  40. strategy1 = ((16, 1, 1), (16, 1, 1))
  41. strategy2 = ((16, 1, 1),)
  42. net = Net(_w1, strategy1, strategy2)
  43. compile_net(net)
  44. def test_forward_graph_model_parallel():
  45. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  46. strategy1 = ((1, 1, 16), (1, 1, 16))
  47. strategy2 = ((1, 1, 16),)
  48. net = Net(_w1, strategy1, strategy2)
  49. compile_net(net)
  50. def test_forward_graph_hybrid_parallel():
  51. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  52. strategy1 = ((2, 2, 4), (2, 2, 4))
  53. strategy2 = ((2, 2, 4),)
  54. net = Net(_w1, strategy1, strategy2)
  55. compile_net(net)
  56. def test_forward_graph_auto_parallel():
  57. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=16, global_rank=0)
  58. net = Net(_w1)
  59. compile_net(net)
  60. def test_forward_graph_repeat_calc():
  61. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  62. strategy1 = ((2, 2, 4), (2, 2, 4))
  63. strategy2 = ((1, 2, 2),)
  64. net = Net(_w1, strategy1, strategy2)
  65. compile_net(net)