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_lin_space.py 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # Copyright 2022 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 pytest
  15. import mindspore.common.dtype as mstype
  16. from mindspore import Tensor, context
  17. from mindspore.nn import Cell
  18. from mindspore.ops import operations as P
  19. from parallel.utils.utils import ParallelValidator, compile_net
  20. class Net(Cell):
  21. def __init__(self, strategy=None):
  22. super(Net, self).__init__()
  23. self.lin_space = P.LinSpace().shard(strategy)
  24. def construct(self, start, end, x):
  25. return self.lin_space(start, end, x)
  26. def test_lin_space_loss_auto_parallel():
  27. """
  28. Feature: test LinSpace auto parallel
  29. Description: auto parallel
  30. Expectation: compile success
  31. """
  32. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=8, global_rank=0)
  33. start = Tensor(1, mstype.float32)
  34. end = Tensor(10, mstype.float32)
  35. x = 8
  36. net = Net()
  37. compile_net(net, start, end, x)
  38. def test_lin_space_parallel_with_repeated_cal():
  39. """
  40. Feature: test LinSpace parallel
  41. Description: parallel with repeated_cal
  42. Expectation: compile success
  43. """
  44. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  45. start = Tensor(1, mstype.float32)
  46. end = Tensor(10, mstype.float32)
  47. x = 8
  48. strategy = ((4,),)
  49. net = Net(strategy)
  50. phase = compile_net(net, start, end, x)
  51. validator = ParallelValidator(net, phase)
  52. assert validator.check_node_inputs('LinSpace-0', ['Add-0', 'Add-1', 2])
  53. def test_lin_space_parallel_with_x_2():
  54. """
  55. Feature: test LinSpace parallel
  56. Description: parallel with input x is 2
  57. Expectation: compile success
  58. """
  59. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  60. start = Tensor(1, mstype.float32)
  61. end = Tensor(10, mstype.float32)
  62. x = 2
  63. strategy = ((2,),)
  64. net = Net(strategy)
  65. phase = compile_net(net, start, end, x)
  66. validator = ParallelValidator(net, phase)
  67. assert validator.check_node_inputs('LinSpace-0', ['Add-0', 'Add-1', 1])
  68. def test_lin_space_data_parallel():
  69. """
  70. Feature: test LinSpace data parallel
  71. Description: data parallel
  72. Expectation: compile success
  73. """
  74. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  75. start = Tensor(1, mstype.float32)
  76. end = Tensor(10, mstype.float32)
  77. x = 8
  78. net = Net()
  79. phase = compile_net(net, start, end, x)
  80. validator = ParallelValidator(net, phase)
  81. assert validator.check_node_inputs('LinSpace-0', ['Add-0', 'Add-1', 1])
  82. def test_lin_space_parallel_strategy_wrong():
  83. """
  84. Feature: test LinSpace parallel with invalid strategy
  85. Description: invalid strategy
  86. Expectation: compile success
  87. """
  88. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  89. start = Tensor(1, mstype.float32)
  90. end = Tensor(10, mstype.float32)
  91. x = 6
  92. strategy = ((4,),)
  93. net = Net(strategy)
  94. with pytest.raises(RuntimeError):
  95. compile_net(net, start, end, x)