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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. from mindspore import Tensor, context
  16. from mindspore.nn import Cell
  17. from mindspore.ops import operations as P
  18. from parallel.utils.utils import ParallelValidator, compile_net
  19. x_ = Tensor(np.random.normal(size=[32, 8, 8]).astype(np.float32))
  20. class Net(Cell):
  21. def __init__(self, strategy=None):
  22. super(Net, self).__init__()
  23. self.l2_loss = P.L2Loss().shard(strategy)
  24. def construct(self, x):
  25. return self.l2_loss(x)
  26. def test_l2_loss_auto_parallel():
  27. """
  28. Feature: test L2Loss 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. net = Net()
  34. compile_net(net, x_)
  35. def test_l2_loss_model_parallel():
  36. """
  37. Feature: test L2Loss model parallel
  38. Description: model parallel
  39. Expectation: compile success
  40. """
  41. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  42. strategy = ((2, 2, 2),)
  43. net = Net(strategy)
  44. phase = compile_net(net, x_)
  45. validator = ParallelValidator(net, phase)
  46. assert validator.check_node_inputs('AllReduce-0', ['L2Loss-0'])
  47. assert validator.check_node_attrs('AllReduce-0', {'op': 'sum'})
  48. def test_l2_loss_data_parallel():
  49. """
  50. Feature: test L2Loss data parallel
  51. Description: data parallel
  52. Expectation: compile success
  53. """
  54. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  55. net = Net()
  56. phase = compile_net(net, x_)
  57. validator = ParallelValidator(net, phase)
  58. assert validator.check_node_inputs('AllReduce-0', ['L2Loss-0'])
  59. assert validator.check_node_attrs('AllReduce-0', {'op': 'sum'})