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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. class Net(nn.Cell):
  23. """Net definition"""
  24. def __init__(self):
  25. super(Net, self).__init__()
  26. self.fc1 = nn.Dense(128, 768, activation='relu')
  27. self.fc2 = nn.Dense(128, 768, activation='relu')
  28. self.fc3 = nn.Dense(128, 768, activation='relu')
  29. self.fc4 = nn.Dense(768, 768, activation='relu')
  30. self.relu4 = nn.ReLU()
  31. self.relu5 = nn.ReLU()
  32. self.transpose = P.Transpose()
  33. self.matmul1 = P.MatMul()
  34. self.matmul2 = P.MatMul()
  35. def construct(self, x):
  36. q = self.fc1(x)
  37. k = self.fc2(x)
  38. v = self.fc3(x)
  39. k = self.transpose(k, (1, 0))
  40. c = self.relu4(self.matmul1(q, k))
  41. s = self.relu5(self.matmul2(c, v))
  42. s = self.fc4(s)
  43. return s
  44. def test_distribute_predict():
  45. context.set_context(mode=context.GRAPH_MODE)
  46. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, full_batch=True)
  47. inputs = Tensor(np.ones([32, 128]).astype(np.float32))
  48. net = Net()
  49. model = Model(net)
  50. predict_map = model.infer_predict_layout(inputs)
  51. output = model.predict(inputs)
  52. context.reset_auto_parallel_context()
  53. return predict_map, output
  54. def test_edge_case():
  55. context.set_context(mode=context.GRAPH_MODE)
  56. inputs = Tensor(np.ones([32, 48]).astype(np.float32))
  57. net = Net()
  58. model = Model(net)
  59. with pytest.raises(RuntimeError):
  60. model.infer_predict_layout(inputs)
  61. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  62. with pytest.raises(RuntimeError):
  63. model.infer_predict_layout(inputs)
  64. context.set_auto_parallel_context(full_batch=True, enable_parallel_optimizer=True)
  65. with pytest.raises(RuntimeError):
  66. model.predict(inputs)