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_cdist.py 4.5 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. from mindspore import Tensor, context
  18. from mindspore.nn import Cell
  19. import mindspore.ops as ops
  20. from parallel.utils.utils import compile_net
  21. B = 8
  22. P = 8
  23. R = 8
  24. M = 2
  25. input_x_2d_ = Tensor(np.random.normal(size=[P, M]).astype(np.float32))
  26. input_y_2d_ = Tensor(np.random.normal(size=[R, M]).astype(np.float32))
  27. input_x_3d_ = Tensor(np.random.normal(size=[B, P, M]).astype(np.float32))
  28. input_y_3d_ = Tensor(np.random.normal(size=[B, R, M]).astype(np.float32))
  29. class Net(Cell):
  30. def __init__(self, strategy=None):
  31. super(Net, self).__init__()
  32. self.cdist = ops.Cdist().shard(strategy)
  33. def construct(self, *inputs):
  34. output = self.cdist(*inputs)
  35. return output
  36. def test_cdist_2d_auto_parallel():
  37. """
  38. Feature: test Cdist-2d in parallel
  39. Description: auto parallel with 2d inputs
  40. Expectation: compile success
  41. """
  42. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=8, global_rank=0)
  43. net = Net()
  44. compile_net(net, input_x_2d_, input_y_2d_)
  45. def test_cdist_2d_data_parallel():
  46. """
  47. Feature: test Cdist-2d in parallel
  48. Description: data parallel with 2d inputs
  49. Expectation: compile success
  50. """
  51. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  52. strategy = ((4, 1), (2, 1))
  53. net = Net(strategy)
  54. compile_net(net, input_x_2d_, input_y_2d_)
  55. def test_cdist_2d_data_parallel_with_repeated_cal():
  56. """
  57. Feature: test Cdist-2d in parallel with repeated calculation
  58. Description: data parallel with 2d inputs
  59. Expectation: compile success
  60. """
  61. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  62. strategy = ((2, 1), (2, 1))
  63. net = Net(strategy)
  64. compile_net(net, input_x_2d_, input_y_2d_)
  65. def test_cdist_2d_strategy_error():
  66. """
  67. Feature: test invalid strategy for Cdist 2d
  68. Description: illegal strategy with 2d inputs
  69. Expectation: raise RuntimeError
  70. """
  71. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  72. strategy = ((2, 2), (2, 1))
  73. net = Net(strategy)
  74. with pytest.raises(RuntimeError):
  75. compile_net(net, input_x_2d_, input_y_2d_)
  76. def test_cdist_3d_auto_parallel():
  77. """
  78. Feature: test Cdist-3d in parallel
  79. Description: auto parallel with 3d inputs
  80. Expectation: compile success
  81. """
  82. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=8, global_rank=0)
  83. net = Net()
  84. compile_net(net, input_x_3d_, input_y_3d_)
  85. def test_cdist_3d_data_parallel():
  86. """
  87. Feature: test Cdist-3d in parallel
  88. Description: data parallel with 3d inputs
  89. Expectation: compile success
  90. """
  91. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  92. strategy = ((2, 2, 1), (2, 2, 1))
  93. net = Net(strategy)
  94. compile_net(net, input_x_3d_, input_y_3d_)
  95. def test_cdist_3d_data_parallel_with_repeated_cal():
  96. """
  97. Feature: test Cdist-3d in parallel with repeated calculation
  98. Description: data parallel with 3d inputs
  99. Expectation: compile success
  100. """
  101. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  102. strategy = ((2, 2, 1), (2, 1, 1))
  103. net = Net(strategy)
  104. compile_net(net, input_x_3d_, input_y_3d_)
  105. def test_cdist_3d_strategy_error():
  106. """
  107. Feature: test invalid strategy for Cdist 3d
  108. Description: illegal strategy with 3d inputs
  109. Expectation: raise RuntimeError
  110. """
  111. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  112. strategy = ((2, 2, 1), (1, 2, 1))
  113. net = Net(strategy)
  114. with pytest.raises(RuntimeError):
  115. compile_net(net, input_x_3d_, input_y_3d_)