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_eval.py 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. from mindspore import context, Tensor, Parameter
  18. from mindspore.common.api import _cell_graph_executor
  19. from mindspore.nn import Cell
  20. from mindspore.ops import operations as P
  21. class Net(Cell):
  22. def __init__(self, mul_weight, strategy1=None, strategy2=None):
  23. super().__init__()
  24. self.mul = P.Mul().shard(strategy1)
  25. self.neg = P.Neg().shard(strategy2)
  26. self.mul_weight = Parameter(mul_weight, "w1")
  27. def construct(self, x, b):
  28. out = self.mul(x, self.mul_weight)
  29. out = self.neg(out)
  30. return out
  31. class EvalNet(Cell):
  32. def __init__(self, network, strategy2=None):
  33. super().__init__()
  34. self.network = network
  35. self.relu = P.ReLU().shard(strategy2)
  36. def construct(self, x, b):
  37. out = self.network(x, b)
  38. out1 = self.relu(out)
  39. return out, out1
  40. _x = Tensor(np.ones([64, 64]), dtype=ms.float32)
  41. _w1 = Tensor(np.ones([64, 64]), dtype=ms.float32)
  42. _b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  43. def compile_net(net, input_data, label, is_train=True):
  44. net.set_auto_parallel()
  45. net.set_train(mode=is_train)
  46. phase = "train" if is_train else "eval"
  47. _cell_graph_executor.compile(net, input_data, label, phase=phase, auto_parallel_mode=True)
  48. def test_train_and_eval():
  49. """
  50. Feature: test train and eval in semi auto parallel.
  51. Description: train and eval net in auto parallel.
  52. Expectation: compile done without error.
  53. """
  54. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16)
  55. strategy1 = ((4, 4), (4, 4))
  56. strategy2 = ((4, 4),)
  57. net = Net(_w1, strategy1, strategy2)
  58. eval_net = EvalNet(net, strategy2=strategy2)
  59. compile_net(net, _x, _b, is_train=True)
  60. compile_net(eval_net, _x, _b, is_train=False)
  61. context.reset_auto_parallel_context()
  62. def test_train_and_eval_auto():
  63. """
  64. Feature: test train and eval in semi auto parallel.
  65. Description: train and eval net in auto parallel.
  66. Expectation: compile done without error.
  67. """
  68. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=16)
  69. strategy1 = ((4, 4), (4, 4))
  70. strategy2 = ((4, 4),)
  71. net = Net(_w1, strategy1, strategy2)
  72. eval_net = EvalNet(net, strategy2=strategy2)
  73. compile_net(net, _x, _b, is_train=True)
  74. compile_net(eval_net, _x, _b, is_train=False)
  75. context.reset_auto_parallel_context()