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_reluv2.py 2.6 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # Copyright 2020 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 mindspore as ms
  17. import mindspore.context as context
  18. from mindspore import Tensor, Parameter
  19. import mindspore.nn as nn
  20. from mindspore.common.api import _executor
  21. from mindspore.nn import TrainOneStepCell, Momentum
  22. from mindspore.ops import operations as P
  23. class Net(nn.Cell):
  24. def __init__(self, mul_weight, strategy=None):
  25. super(Net, self).__init__()
  26. self.reluv2 = P.ReLUV2().shard(strategy)
  27. self.mul = P.Mul()
  28. self.weight = Parameter(mul_weight, "w1")
  29. def construct(self, x):
  30. out = self.mul(x, self.weight)
  31. output, _ = self.reluv2(out)
  32. return output
  33. _w1 = Tensor(np.ones([32, 16, 48, 64]), dtype=ms.float32)
  34. _x = Tensor(np.ones([32, 16, 48, 64]), dtype=ms.float32)
  35. def compile_net(net):
  36. context.set_context(mode=context.GRAPH_MODE, save_graphs=True)
  37. optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
  38. train_net = TrainOneStepCell(net, optimizer)
  39. train_net.set_auto_parallel()
  40. train_net.set_train()
  41. _executor.compile(train_net, _x)
  42. context.reset_auto_parallel_context()
  43. def test_reluv2():
  44. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  45. strategy = ((2, 1, 2, 2),)
  46. net = Net(_w1, strategy)
  47. compile_net(net)
  48. def test_reluv2_no_full():
  49. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  50. strategy = ((2, 1, 2, 1),)
  51. net = Net(_w1, strategy)
  52. compile_net(net)
  53. def test_reluv2_no_strategy():
  54. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  55. strategy = None
  56. net = Net(_w1, strategy)
  57. compile_net(net)
  58. def test_reluv2_auto_parallel():
  59. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=8, global_rank=0)
  60. net = Net(_w1)
  61. compile_net(net)