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

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