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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. net.set_train()
  52. exe.compile(net, x, auto_parallel_mode=True, phase='train')
  53. hccl.rank_id = rank_save
  54. return net.parameters_dict()['w1'].data.asnumpy()
  55. slice0 = get_slice(0)
  56. slice1 = get_slice(1)
  57. slice4 = get_slice(4)
  58. slice_shape = slice0.shape
  59. slice0 = slice0.flatten()
  60. slice1 = slice1.flatten()
  61. slice4 = slice4.flatten()
  62. expect_slice_shape = (16, 32)
  63. assert expect_slice_shape == slice_shape
  64. assert all(slice0 == slice4)
  65. if init_name not in ["One", "Zero"]:
  66. assert any(slice0 != slice1)
  67. initializers = ["Uniform", "Normal", "TruncatedNormal", "HeUniform", "HeNormal", "XavierUniform", "One", "Zero"]
  68. def test_initializer_weight_slice():
  69. for init_name in initializers:
  70. check_initializer_weight_slice(init_name)
  71. def test_wrong_order_set_parallel_mode_with_initializer():
  72. weight = initializer("Normal", [64, 32], ms.float32)
  73. strategy1 = ((2, 1), (4, 1))
  74. strategy2 = ((2, 4),)
  75. net = Net(strategy1, strategy2, weight)
  76. exe = me._executor
  77. x = Tensor(np.ones([32, 32]), dtype=ms.float32)
  78. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  79. net.set_auto_parallel()
  80. with pytest.raises(RuntimeError):
  81. exe.compile(net, x, auto_parallel_mode=True, phase='train')
  82. def test_wrong_order_set_same_parallel_mode_with_initializer():
  83. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  84. weight = initializer("Normal", [64, 32], ms.float32)
  85. strategy1 = ((2, 1), (4, 1))
  86. strategy2 = ((2, 4),)
  87. net = Net(strategy1, strategy2, weight)
  88. exe = me._executor
  89. x = Tensor(np.ones([32, 32]), dtype=ms.float32)
  90. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=8, global_rank=0)
  91. net.set_auto_parallel()
  92. exe.compile(net, x, auto_parallel_mode=True, phase='train')
  93. def test_wrong_order_set_parallel_mode_without_initializer():
  94. weight = Tensor(np.ones([64, 32]), ms.float32)
  95. strategy1 = ((2, 1), (4, 1))
  96. strategy2 = ((2, 4),)
  97. net = Net(strategy1, strategy2, weight)
  98. exe = me._executor
  99. x = Tensor(np.ones([32, 32]), dtype=ms.float32)
  100. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  101. net.set_auto_parallel()
  102. exe.compile(net, x, auto_parallel_mode=True, phase='train')
  103. def test_check_initializer_weight_slice_seed(init_name="Uniform"):
  104. def get_slice(rank):
  105. set_seed(1)
  106. hccl = Hccl()
  107. rank_save = hccl.rank_id
  108. hccl.rank_id = rank
  109. context.reset_auto_parallel_context()
  110. context.set_auto_parallel_context(device_num=8, global_rank=0)
  111. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  112. strategy1 = ((2, 1), (4, 1))
  113. strategy2 = ((2, 4),)
  114. context.set_context(mode=context.GRAPH_MODE)
  115. exe = me._executor
  116. x = Tensor(np.ones([32, 32]), dtype=ms.float32)
  117. weight = initializer(init_name, [64, 32], ms.float32)
  118. net = Net(strategy1, strategy2, weight)
  119. net.set_auto_parallel()
  120. net.set_train()
  121. exe.compile(net, x, auto_parallel_mode=True, phase='train')
  122. hccl.rank_id = rank_save
  123. return net.parameters_dict()['w1'].data.asnumpy()
  124. slice0 = get_slice(0)
  125. slice1 = get_slice(1)
  126. slice4 = get_slice(4)
  127. slice_shape = slice0.shape
  128. slice0 = slice0.flatten()
  129. slice1 = slice1.flatten()
  130. slice4 = slice4.flatten()
  131. expect_slice_shape = (16, 32)
  132. assert expect_slice_shape == slice_shape
  133. assert all(slice0 == slice4)
  134. assert all(slice0 == slice1)
  135. if __name__ == '__main__':
  136. test_initializer_weight_slice()