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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. import mindspore as ms
  15. import mindspore.nn as nn
  16. from mindspore.ops import operations as P
  17. from mindspore.ops import composite as C
  18. from mindspore import Tensor
  19. from mindspore import context
  20. from mindspore.common.api import _executor
  21. from tests.ut.python.ops.test_math_ops import VirtualLoss
  22. import numpy as np
  23. class NetWithLoss(nn.Cell):
  24. def __init__(self, network):
  25. super(NetWithLoss, self).__init__()
  26. self.loss = VirtualLoss()
  27. self.network = network
  28. def construct(self, x, y):
  29. predict = self.network(x, y)
  30. return self.loss(predict)
  31. class GradWrap(nn.Cell):
  32. def __init__(self, network):
  33. super(GradWrap, self).__init__()
  34. self.network = network
  35. def construct(self, x, y):
  36. return C.grad_all(self.network)(x, y)
  37. class Net(nn.Cell):
  38. def __init__(self, strategy):
  39. super().__init__()
  40. self.reshape = P.Reshape()
  41. self.mul = P.Mul().set_strategy(strategy)
  42. self.relu = P.ReLU()
  43. def construct(self, x, y):
  44. out = self.reshape(x, (10000, 36, 1))
  45. out = self.mul(out, y)
  46. out = self.relu(out)
  47. return out
  48. def compile(net, x, y):
  49. net.set_auto_parallel()
  50. _executor.compile(net, x, y)
  51. def test_reshape_parameter_data_parallel():
  52. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  53. strategy = ((8, 1, 1), (8, 1, 1))
  54. net = GradWrap(NetWithLoss(Net(strategy)))
  55. x = Tensor(np.ones([10000, 36]), dtype=ms.float32)
  56. y = Tensor(np.ones([10000, 36, 1]), dtype=ms.float32)
  57. compile(net, x, y)
  58. def test_reshape_parameter_model_parallel():
  59. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  60. strategy = ((4, 2, 1), (4, 2, 1))
  61. net = GradWrap(NetWithLoss(Net(strategy)))
  62. x = Tensor(np.ones([10000, 36]), dtype=ms.float32)
  63. y = Tensor(np.ones([10000, 36, 1]), dtype=ms.float32)
  64. compile(net, x, y)