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 5.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 pytest
  16. from mindspore import context
  17. import mindspore.nn as nn
  18. from mindspore.ops import operations as P
  19. from mindspore import Tensor, Parameter
  20. import mindspore as ms
  21. import mindspore.common.api as me
  22. from mindspore.common.initializer import initializer
  23. from mindspore.common import set_seed
  24. from hccl_test.manage.api import Hccl
  25. class Net(nn.Cell):
  26. def __init__(self, strategy1, strategy2, weight):
  27. super().__init__()
  28. self.weight = Parameter(weight, "w1")
  29. self.matmul = P.MatMul(transpose_a=False, transpose_b=True).shard(strategy1)
  30. self.relu = P.ReLU().shard(strategy2)
  31. def construct(self, x):
  32. out = self.matmul(x, self.weight)
  33. out = self.relu(out)
  34. return out
  35. def check_initializer_weight_slice(init_name="Uniform"):
  36. def get_slice(rank):
  37. hccl = Hccl()
  38. rank_save = hccl.rank_id
  39. hccl.rank_id = rank
  40. context.reset_auto_parallel_context()
  41. context.set_auto_parallel_context(device_num=8, global_rank=0)
  42. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  43. strategy1 = ((2, 1), (4, 1))
  44. strategy2 = ((2, 4),)
  45. context.set_context(mode=context.GRAPH_MODE)
  46. exe = me._executor
  47. x = Tensor(np.ones([32, 32]), dtype=ms.float32)
  48. weight = initializer(init_name, [64, 32], ms.float32)
  49. net = Net(strategy1, strategy2, weight)
  50. net.set_auto_parallel()
  51. exe.compile(net, x, auto_parallel_mode=True, phase='train')
  52. hccl.rank_id = rank_save
  53. return net.parameters_dict()['w1'].data.asnumpy()
  54. slice0 = get_slice(0)
  55. slice1 = get_slice(1)
  56. slice4 = get_slice(4)
  57. slice_shape = slice0.shape
  58. slice0 = slice0.flatten()
  59. slice1 = slice1.flatten()
  60. slice4 = slice4.flatten()
  61. expect_slice_shape = (16, 32)
  62. assert expect_slice_shape == slice_shape
  63. assert all(slice0 == slice4)
  64. if init_name not in ["One", "Zero"]:
  65. assert any(slice0 != slice1)
  66. initializers = ["Uniform", "Normal", "TruncatedNormal", "HeUniform", "HeNormal", "XavierUniform", "One", "Zero"]
  67. def test_initializer_weight_slice():
  68. for init_name in initializers:
  69. check_initializer_weight_slice(init_name)
  70. def test_wrong_order_set_parallel_mode_with_initializer():
  71. weight = initializer("Normal", [64, 32], ms.float32)
  72. strategy1 = ((2, 1), (4, 1))
  73. strategy2 = ((2, 4),)
  74. net = Net(strategy1, strategy2, weight)
  75. exe = me._executor
  76. x = Tensor(np.ones([32, 32]), dtype=ms.float32)
  77. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  78. net.set_auto_parallel()
  79. with pytest.raises(RuntimeError):
  80. exe.compile(net, x, auto_parallel_mode=True, phase='train')
  81. def test_wrong_order_set_same_parallel_mode_with_initializer():
  82. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  83. weight = initializer("Normal", [64, 32], ms.float32)
  84. strategy1 = ((2, 1), (4, 1))
  85. strategy2 = ((2, 4),)
  86. net = Net(strategy1, strategy2, weight)
  87. exe = me._executor
  88. x = Tensor(np.ones([32, 32]), dtype=ms.float32)
  89. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=8, global_rank=0)
  90. net.set_auto_parallel()
  91. exe.compile(net, x, auto_parallel_mode=True, phase='train')
  92. def test_wrong_order_set_parallel_mode_without_initializer():
  93. weight = Tensor(np.ones([64, 32]), ms.float32)
  94. strategy1 = ((2, 1), (4, 1))
  95. strategy2 = ((2, 4),)
  96. net = Net(strategy1, strategy2, weight)
  97. exe = me._executor
  98. x = Tensor(np.ones([32, 32]), dtype=ms.float32)
  99. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  100. net.set_auto_parallel()
  101. exe.compile(net, x, auto_parallel_mode=True, phase='train')
  102. def test_check_initializer_weight_slice_seed(init_name="Uniform"):
  103. def get_slice(rank):
  104. set_seed(1)
  105. hccl = Hccl()
  106. rank_save = hccl.rank_id
  107. hccl.rank_id = rank
  108. context.reset_auto_parallel_context()
  109. context.set_auto_parallel_context(device_num=8, global_rank=0)
  110. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  111. strategy1 = ((2, 1), (4, 1))
  112. strategy2 = ((2, 4),)
  113. context.set_context(mode=context.GRAPH_MODE)
  114. exe = me._executor
  115. x = Tensor(np.ones([32, 32]), dtype=ms.float32)
  116. weight = initializer(init_name, [64, 32], ms.float32)
  117. net = Net(strategy1, strategy2, weight)
  118. net.set_auto_parallel()
  119. exe.compile(net, x, auto_parallel_mode=True, phase='train')
  120. hccl.rank_id = rank_save
  121. return net.parameters_dict()['w1'].data.asnumpy()
  122. slice0 = get_slice(0)
  123. slice1 = get_slice(1)
  124. slice4 = get_slice(4)
  125. slice_shape = slice0.shape
  126. slice0 = slice0.flatten()
  127. slice1 = slice1.flatten()
  128. slice4 = slice4.flatten()
  129. expect_slice_shape = (16, 32)
  130. assert expect_slice_shape == slice_shape
  131. assert all(slice0 == slice4)
  132. assert all(slice0 == slice1)
  133. if __name__ == '__main__':
  134. test_initializer_weight_slice()