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_distribute_predict.py 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. # ============================================================================
  15. """ test distribute predict """
  16. import numpy as np
  17. import pytest
  18. import mindspore.nn as nn
  19. from mindspore import Tensor, Model
  20. from mindspore.ops import operations as P
  21. from mindspore import context
  22. from mindspore.parallel._utils import _infer_rank_list
  23. class Net(nn.Cell):
  24. """Net definition"""
  25. def __init__(self):
  26. super(Net, self).__init__()
  27. self.fc1 = nn.Dense(128, 768, activation='relu')
  28. self.fc2 = nn.Dense(128, 768, activation='relu')
  29. self.fc3 = nn.Dense(128, 768, activation='relu')
  30. self.fc4 = nn.Dense(768, 768, activation='relu')
  31. self.relu4 = nn.ReLU()
  32. self.relu5 = nn.ReLU()
  33. self.transpose = P.Transpose()
  34. self.matmul1 = P.MatMul()
  35. self.matmul2 = P.MatMul()
  36. def construct(self, x):
  37. q = self.fc1(x)
  38. k = self.fc2(x)
  39. v = self.fc3(x)
  40. k = self.transpose(k, (1, 0))
  41. c = self.relu4(self.matmul1(q, k))
  42. s = self.relu5(self.matmul2(c, v))
  43. s = self.fc4(s)
  44. return s
  45. def test_distribute_predict():
  46. context.set_context(mode=context.GRAPH_MODE)
  47. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, full_batch=True)
  48. inputs = Tensor(np.ones([32, 128]).astype(np.float32))
  49. net = Net()
  50. model = Model(net)
  51. predict_map = model.infer_predict_layout(inputs)
  52. output = model.predict(inputs)
  53. context.reset_auto_parallel_context()
  54. return predict_map, output
  55. def test_edge_case():
  56. context.set_context(mode=context.GRAPH_MODE)
  57. inputs = Tensor(np.ones([32, 48]).astype(np.float32))
  58. net = Net()
  59. model = Model(net)
  60. with pytest.raises(RuntimeError):
  61. model.infer_predict_layout(inputs)
  62. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  63. with pytest.raises(RuntimeError):
  64. model.infer_predict_layout(inputs)
  65. context.set_auto_parallel_context(full_batch=True, enable_parallel_optimizer=True)
  66. with pytest.raises(RuntimeError):
  67. model.predict(inputs)
  68. # standalone predict
  69. def test_infer_rank_list1():
  70. train_map = {'weight': [[4, 8], [-1, 0]]}
  71. predict_map = None
  72. rank_list = _infer_rank_list(train_map, predict_map)["weight"]
  73. assert list(rank_list[0]) == [0, 1, 2, 3, 4, 5, 6, 7]
  74. assert rank_list[1] is False
  75. # similar layout: gpt3 prediction mode
  76. def test_infer_rank_list2():
  77. train_map = {'weight': [[4, 8], [-1, 0]]}
  78. predict_map = {'weight': [[8], [-1, 0]]}
  79. rank_list = _infer_rank_list(train_map, predict_map)
  80. expect_map = {'weight': ([0], True)}
  81. assert rank_list == expect_map
  82. # same layout
  83. def test_infer_rank_list3():
  84. train_map = {'weight': [[4, 8], [-1, 0]]}
  85. predict_map = {'weight': [[4, 8], [-1, 0]]}
  86. rank_list = _infer_rank_list(train_map, predict_map)
  87. expect_map = {'weight': ([0], True)}
  88. assert rank_list == expect_map
  89. # totally different layout
  90. def test_infer_rank_list4():
  91. train_map = {'weight': [[4, 8], [-1, 0]]}
  92. predict_map = {'weight': [[2, 2], [1, 0]]}
  93. rank_list = _infer_rank_list(train_map, predict_map)["weight"]
  94. assert list(rank_list[0]) == [0, 1, 2, 3, 4, 5, 6, 7]
  95. assert rank_list[1] is False
  96. # full shape ckpt
  97. def test_infer_rank_list5():
  98. train_map = {'weight': [[8], [-1, -1]]}
  99. predict_map = {'weight': [[2, 2], [1, 0]]}
  100. rank_list = _infer_rank_list(train_map, predict_map)
  101. expect_map = {'weight': ([0], False)}
  102. assert rank_list == expect_map