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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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, Parameter
  19. import mindspore as ms
  20. import mindspore.common.api as me
  21. def test_get_parameter_layout():
  22. class Net(nn.Cell):
  23. def __init__(self, strategy1, strategy2, weight):
  24. super().__init__()
  25. self.weight = Parameter(weight, "w1")
  26. self.matmul = P.MatMul(transpose_a=False, transpose_b=True).set_strategy(strategy1)
  27. self.relu = P.ReLU().set_strategy(strategy2)
  28. def construct(self, x):
  29. out = self.matmul(x, self.weight)
  30. out = self.relu(out)
  31. return out
  32. context.reset_auto_parallel_context()
  33. context.set_auto_parallel_context(device_num=8, global_rank=0)
  34. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  35. strategy1 = ((2, 1), (4, 1))
  36. strategy2 = ((2, 4), )
  37. context.set_context(mode=context.GRAPH_MODE)
  38. x = Tensor(np.ones([32, 32]), dtype=ms.float32)
  39. weight = Tensor(np.ones([64, 32]), dtype=ms.float32)
  40. net = Net(strategy1, strategy2, weight)
  41. exe = me._executor
  42. exe.compile(net, x)
  43. x_layout = ([2, 4], [1, -1]) # device_arrangement = [2, 4], tensor_map = [1, -1]
  44. weight_layout = ([2, 4], [0, -1]) # device_arrangement = [2, 4], tensor_map = [0, -1]
  45. expect_dict = {'x': x_layout, 'w1': weight_layout}
  46. # to be resovled: static local variable count_p is used in step_parallel.cc, it needs to be reset between each ut
  47. assert (net._parameter_layout_dict == expect_dict)
  48. if __name__ == '__main__':
  49. test_get_parameter_layout()