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_iou.py 2.4 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 numpy as np
  15. import pytest
  16. import mindspore as ms
  17. from mindspore import context, Tensor
  18. from mindspore.common.api import _cell_graph_executor
  19. from mindspore.nn import Cell
  20. from mindspore.ops import operations as P
  21. _anchor_boxes = Tensor(np.ones([32, 4]), ms.float32)
  22. _gt_boxes = Tensor(np.ones([64, 4]), ms.float32)
  23. class Net(Cell):
  24. def __init__(self, strategy=None):
  25. super(Net, self).__init__()
  26. self.iou = P.IOU().shard(strategy)
  27. def construct(self, anchor_boxes, gt_boxes):
  28. x = self.iou(anchor_boxes, gt_boxes)
  29. return x
  30. def compile_net(net: Cell):
  31. net.set_train()
  32. net.set_auto_parallel()
  33. _cell_graph_executor.compile(net, _anchor_boxes, _gt_boxes)
  34. context.reset_auto_parallel_context()
  35. def test_auto_parallel_iou():
  36. """
  37. Feature: test IOU auto parallel
  38. Description: auto parallel
  39. Expectation: compile success
  40. """
  41. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=8, global_rank=0)
  42. net = Net()
  43. compile_net(net)
  44. def test_data_parallel_iou():
  45. """
  46. Feature: test IOU data parallel strategy
  47. Description: only shard the batch dimension
  48. Expectation: compile success
  49. """
  50. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  51. strategy = ((2, 1), (4, 1))
  52. net = Net(strategy)
  53. compile_net(net)
  54. def test_iou_strategy_error():
  55. """
  56. Feature: test IOU with illegal strategy
  57. Description: illegal strategy
  58. Expectation: raise RuntimeError
  59. """
  60. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  61. strategy = ((2, 2), (2, 1))
  62. net = Net(strategy)
  63. with pytest.raises(RuntimeError):
  64. compile_net(net)