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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # Copyright 2019 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 context
  16. import mindspore.nn as nn
  17. from mindspore.ops import operations as P
  18. from mindspore import Tensor
  19. from tests.ut.python.ops.test_math_ops import VirtualLoss
  20. import mindspore as ms
  21. from mindspore.common.api import _executor
  22. from mindspore.ops import composite as C
  23. class NetWithLoss(nn.Cell):
  24. def __init__(self, network, strategy3):
  25. super(NetWithLoss, self).__init__()
  26. self.loss = P.SoftmaxCrossEntropyWithLogits().set_strategy(strategy3)
  27. self.network = network
  28. def construct(self, x, y, bias, label):
  29. predict = self.network(x, y, bias)
  30. return self.loss(predict, label)[0]
  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, bias, label):
  36. return C.grad_all(self.network)(x, y, bias, label)
  37. def test_linear():
  38. class Net(nn.Cell):
  39. def __init__(self, strategy0, strategy1, strategy2):
  40. super().__init__()
  41. self.fc_nobias = P.MatMul(transpose_b=True).set_strategy(strategy0)
  42. self.add = P.TensorAdd().set_strategy(strategy1)
  43. self.gelu = P.Gelu().set_strategy(strategy2)
  44. def construct(self, x, y, bias):
  45. out = self.fc_nobias(x, y)
  46. out = self.add(out, bias)
  47. out = self.gelu(out)
  48. return out
  49. context.set_auto_parallel_context(device_num=16, global_rank=0)
  50. strategy0 = ((2, 4), (2, 4))
  51. strategy1 = ((2, 4), (4, ))
  52. strategy2 = ((2, 8), )
  53. strategy3 = ((16, 1), (16, 1))
  54. net = GradWrap(NetWithLoss(Net(strategy0, strategy1, strategy2), strategy3))
  55. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  56. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  57. y = Tensor(np.ones([64, 32]), dtype=ms.float32)
  58. bias = Tensor(np.ones([64]), dtype=ms.float32)
  59. label = Tensor(np.ones([64, 64]), dtype=ms.float32)
  60. _executor.compile(net, x, y, bias, label)