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.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. enable_parallel_optimizer=True)
  49. inputs = Tensor(np.ones([32, 128]).astype(np.float32))
  50. net = Net()
  51. model = Model(net)
  52. predict_map = model.infer_predict_layout(inputs)
  53. output = model.predict(inputs)
  54. context.reset_auto_parallel_context()
  55. return predict_map, output
  56. def test_edge_case():
  57. context.set_context(mode=context.GRAPH_MODE)
  58. inputs = Tensor(np.ones([32, 48]).astype(np.float32))
  59. net = Net()
  60. model = Model(net)
  61. with pytest.raises(RuntimeError):
  62. model.infer_predict_layout(inputs)
  63. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  64. with pytest.raises(RuntimeError):
  65. model.infer_predict_layout(inputs)
  66. # standalone predict
  67. def test_infer_rank_list1():
  68. train_map = {'weight': [[4, 8], [-1, 0]]}
  69. predict_map = None
  70. rank_list = _infer_rank_list(train_map, predict_map)["weight"]
  71. assert list(rank_list[0]) == [0, 1, 2, 3, 4, 5, 6, 7]
  72. assert rank_list[1] is False
  73. # similar layout: gpt3 prediction mode
  74. def test_infer_rank_list2():
  75. train_map = {'weight': [[4, 8], [-1, 0]]}
  76. predict_map = {'weight': [[8], [-1, 0]]}
  77. rank_list = _infer_rank_list(train_map, predict_map)
  78. expect_map = {'weight': ([0], True)}
  79. assert rank_list == expect_map
  80. # same layout
  81. def test_infer_rank_list3():
  82. train_map = {'weight': [[4, 8], [-1, 0]]}
  83. predict_map = {'weight': [[4, 8], [-1, 0]]}
  84. rank_list = _infer_rank_list(train_map, predict_map)
  85. expect_map = {'weight': ([0], True)}
  86. assert rank_list == expect_map
  87. # totally different layout
  88. def test_infer_rank_list4():
  89. train_map = {'weight': [[4, 8], [-1, 0]]}
  90. predict_map = {'weight': [[2, 2], [1, 0]]}
  91. rank_list = _infer_rank_list(train_map, predict_map)["weight"]
  92. assert list(rank_list[0]) == [0, 1, 2, 3, 4, 5, 6, 7]
  93. assert rank_list[1] is False
  94. # full shape ckpt
  95. def test_infer_rank_list5():
  96. train_map = {'weight': [[8], [-1, -1]]}
  97. predict_map = {'weight': [[2, 2], [1, 0]]}
  98. rank_list = _infer_rank_list(train_map, predict_map)
  99. expect_map = {'weight': ([0], False)}
  100. assert rank_list == expect_map