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_checkvalid.py 4.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. # ============================================================================
  15. import numpy as np
  16. import pytest
  17. import mindspore as ms
  18. from mindspore import context, Tensor, Parameter
  19. from mindspore.nn import Cell
  20. from mindspore.ops import operations as P
  21. from mindspore.train import Model
  22. class Net(Cell):
  23. def __init__(self, weight, strategy):
  24. super().__init__()
  25. self.check_valid = P.CheckValid().shard(strategy)
  26. self.mul = P.Mul()
  27. cast_strategy = None
  28. if strategy:
  29. cast_strategy = (strategy[0],)
  30. self.cast = P.Cast().shard(cast_strategy)
  31. self.relu = P.ReLU()
  32. self.weight = Parameter(weight, "w1")
  33. def construct(self, x, b):
  34. out = self.mul(x, self.weight)
  35. out = self.check_valid(out, b)
  36. out = self.cast(out, ms.float32)
  37. out = self.relu(out)
  38. return out
  39. _x = Tensor(np.ones([16, 4]), dtype=ms.float32)
  40. _w = Tensor(np.ones([16, 4]), dtype=ms.float32)
  41. _b = Tensor(np.ones([3]), dtype=ms.float32)
  42. def compile_net(net):
  43. model = Model(net)
  44. model.predict(_x, _b)
  45. context.reset_auto_parallel_context()
  46. def test_check_valid_data_parallel():
  47. """
  48. Feature: test check valid data parallel
  49. Description:
  50. Expectation: compile success
  51. """
  52. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, full_batch=True)
  53. strategy = ((8, 1), (1,))
  54. net = Net(_w, strategy)
  55. compile_net(net)
  56. def test_check_valid_repeated_calc():
  57. """
  58. Feature: test check valid repeated calculation
  59. Description:
  60. Expectation: compile success
  61. """
  62. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, full_batch=True)
  63. strategy = ((2, 1), (1,))
  64. net = Net(_w, strategy)
  65. compile_net(net)
  66. def test_check_valid_no_shard():
  67. """
  68. Feature: test check valid no shard
  69. Description:
  70. Expectation: compile success
  71. """
  72. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, full_batch=True)
  73. strategy = ((1, 1), (1,))
  74. net = Net(_w, strategy)
  75. compile_net(net)
  76. def test_check_valid_strategy_none():
  77. """
  78. Feature: test check valid strategy none
  79. Description: generator batch parallel strategy
  80. Expectation: compile success
  81. """
  82. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, full_batch=True)
  83. strategy = None
  84. net = Net(_w, strategy)
  85. compile_net(net)
  86. def test_check_valid_auto_parallel():
  87. """
  88. Feature: test check valid auto parallel
  89. Description:
  90. Expectation: compile success
  91. """
  92. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=8, full_batch=True)
  93. strategy = None
  94. net = Net(_w, strategy)
  95. compile_net(net)
  96. def test_check_valid_shard_img():
  97. """
  98. Feature: test check valid shard img
  99. Description:
  100. Expectation: compile failed
  101. """
  102. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, full_batch=True)
  103. strategy = ((2, 1), (4,))
  104. net = Net(_w, strategy)
  105. with pytest.raises(RuntimeError):
  106. compile_net(net)
  107. def test_check_valid_shard_bbox_second_dimension():
  108. """
  109. Feature: test check valid shard bbox second dimension
  110. Description:
  111. Expectation: compile failed
  112. """
  113. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, full_batch=True)
  114. strategy = ((2, 2), (1,))
  115. net = Net(_w, strategy)
  116. with pytest.raises(RuntimeError):
  117. compile_net(net)