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_scatter_update.py 3.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # Copyright 2021 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 scatter update """
  16. import numpy as np
  17. import pytest
  18. import mindspore.nn as nn
  19. from mindspore import Tensor, Model, Parameter
  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, strategy1=None, strategy2=None):
  25. super(Net, self).__init__()
  26. self.inputs = Parameter(Tensor(np.ones([32, 64, 128]).astype(np.float32)), "input")
  27. self.indices = Tensor(np.ones([4, 8]).astype(np.int32))
  28. self.updates = Tensor(np.ones([4, 8, 64, 128]).astype(np.float32))
  29. self.scatter_update = P.ScatterUpdate().shard(strategy1)
  30. self.add = P.TensorAdd().shard(strategy2)
  31. self.relu = P.ReLU()
  32. def construct(self, x):
  33. out = self.scatter_update(self.inputs, self.indices, self.updates)
  34. out = self.add(x, out)
  35. out = self.relu(out)
  36. return out
  37. def test_distribute_predict():
  38. context.set_context(mode=context.GRAPH_MODE, save_graphs=True)
  39. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, full_batch=True)
  40. inputs = Tensor(np.ones([32, 64, 128]).astype(np.float32))
  41. strategy1 = ((1, 2, 4), (1, 1), (1, 1, 2, 4))
  42. strategy2 = ((1, 2, 4), (1, 2, 4))
  43. net = Net(strategy1, strategy2)
  44. model = Model(net)
  45. predict_map = model.infer_predict_layout(inputs)
  46. output = model.predict(inputs)
  47. context.reset_auto_parallel_context()
  48. return predict_map, output
  49. def test_scatter_update_wrong_strategy():
  50. context.set_context(mode=context.GRAPH_MODE, save_graphs=True)
  51. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, full_batch=True)
  52. inputs = Tensor(np.ones([32, 64, 128]).astype(np.float32))
  53. strategy1 = ((1, 2, 4), (1, 1), (1, 1, 4, 2))
  54. strategy2 = ((1, 2, 4), (1, 2, 4))
  55. net = Net(strategy1, strategy2)
  56. model = Model(net)
  57. with pytest.raises(RuntimeError):
  58. model.predict(inputs)
  59. context.reset_auto_parallel_context()
  60. def test_distribute_predict_auto_parallel():
  61. context.set_context(mode=context.GRAPH_MODE, save_graphs=True)
  62. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=8, full_batch=True)
  63. inputs = Tensor(np.ones([32, 64, 128]).astype(np.float32))
  64. net = Net()
  65. model = Model(net)
  66. predict_map = model.infer_predict_layout(inputs)
  67. output = model.predict(inputs)
  68. context.reset_auto_parallel_context()
  69. return predict_map, output