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_skip_redistribution.py 2.2 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 numpy as np
  15. import mindspore as ms
  16. from mindspore import context, Tensor, Parameter
  17. from mindspore.common.api import _executor
  18. from mindspore.nn import Cell, TrainOneStepCell, Momentum
  19. from mindspore.ops import operations as P
  20. class Net(Cell):
  21. def __init__(self, matmul_weight, strategy1=None):
  22. super().__init__()
  23. self.gatherv2 = P.Gather().shard(strategy1)
  24. self.reshape = P.Reshape().add_prim_attr("skip_redistribution", True)
  25. self.matmul = P.MatMul(transpose_b=False)
  26. self.index = Tensor(np.ones([64, 64]), dtype=ms.int32)
  27. self.matmul_weight = Parameter(matmul_weight, "w1")
  28. self.axis = 0
  29. def construct(self, x, b):
  30. out = self.gatherv2(x, self.index, self.axis)
  31. out = self.reshape(out, (64, -1))
  32. out = self.matmul(out, self.matmul_weight)
  33. return out
  34. _w1 = Tensor(np.ones([4096, 32]), dtype=ms.float32)
  35. _x = Tensor(np.ones([64, 64]), dtype=ms.float32)
  36. _b = Tensor(np.ones([128, 64, 32]), dtype=ms.float32)
  37. def compile_net(net):
  38. context.set_context(save_graphs=True)
  39. optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
  40. train_net = TrainOneStepCell(net, optimizer)
  41. train_net.set_auto_parallel()
  42. train_net.set_train()
  43. _executor.compile(train_net, _x, _b)
  44. context.reset_auto_parallel_context()
  45. def test_reshape_skip_redistribution():
  46. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  47. strategy1 = ((1, 8), (1, 1))
  48. net = Net(_w1, strategy1)
  49. compile_net(net)