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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. 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. from mindspore.common.initializer import initializer
  22. from hccl_test.manage.api import Hccl
  23. def test_initializer_weight_slice():
  24. class Net(nn.Cell):
  25. def __init__(self, strategy1, strategy2, weight):
  26. super().__init__()
  27. self.weight = Parameter(weight, "w1")
  28. self.matmul = P.MatMul(transpose_a=False, transpose_b=True).set_strategy(strategy1)
  29. self.relu = P.ReLU().set_strategy(strategy2)
  30. def construct(self, x):
  31. out = self.matmul(x, self.weight)
  32. out = self.relu(out)
  33. return out
  34. def get_slice(rank):
  35. hccl = Hccl()
  36. rank_save = hccl.rank_id
  37. hccl.rank_id = rank
  38. context.reset_auto_parallel_context()
  39. context.set_auto_parallel_context(device_num=8, global_rank=0)
  40. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  41. strategy1 = ((2, 1), (4, 1))
  42. strategy2 = ((2, 4),)
  43. context.set_context(mode=context.GRAPH_MODE)
  44. exe = me._executor
  45. x = Tensor(np.ones([32, 32]), dtype=ms.float32)
  46. weight = initializer("Uniform", [64, 32], ms.float32)
  47. net = Net(strategy1, strategy2, weight)
  48. net.set_auto_parallel()
  49. exe.compile(net, x, auto_parallel_mode=True, phase='train')
  50. hccl.rank_id = rank_save
  51. return net.parameters_dict()['w1'].data.asnumpy()
  52. slice0 = get_slice(0)
  53. slice1 = get_slice(1)
  54. slice4 = get_slice(4)
  55. slice_shape = slice0.shape
  56. slice0 = slice0.flatten()
  57. slice1 = slice1.flatten()
  58. slice4 = slice4.flatten()
  59. expect_slice_shape = (16, 32)
  60. assert expect_slice_shape == slice_shape
  61. assert all(slice0 == slice4)
  62. assert any(slice0 != slice1)
  63. if __name__ == '__main__':
  64. test_initializer_weight_slice()