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_auto_parallel_reshape.py 2.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. from mindspore.common.parameter import Parameter
  24. class NetWithLoss(nn.Cell):
  25. def __init__(self, network):
  26. super(NetWithLoss, self).__init__()
  27. self.loss = VirtualLoss()
  28. self.network = network
  29. def construct(self, x):
  30. predict = self.network(x)
  31. return self.loss(predict)
  32. class GradWrap(nn.Cell):
  33. def __init__(self, network):
  34. super(GradWrap, self).__init__()
  35. self.network = network
  36. def construct(self, x):
  37. return C.grad_all(self.network)(x)
  38. # core dump, step_auto_parallel should SetInputs for transpose axis
  39. def test_reshape_matmul():
  40. class Net(nn.Cell):
  41. def __init__(self):
  42. super().__init__()
  43. self.reshape = P.Reshape()
  44. self.matmul = P.MatMul()
  45. self.matmul_weight = Parameter(Tensor(np.ones([28, 64]), dtype=ms.float32), name="weight")
  46. def construct(self, x):
  47. out = self.reshape(x, (64, 28))
  48. out = self.matmul(out, self.matmul_weight)
  49. return out
  50. size = 8
  51. context.set_auto_parallel_context(device_num=size, global_rank=0)
  52. x = Tensor(np.ones([8*size, 28, 1, 1]), dtype=ms.float32)
  53. net = GradWrap(NetWithLoss(Net()))
  54. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  55. _executor.compile(net, x)
  56. if __name__ == '__main__':
  57. test_reshape_matmul()