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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 mindspore.nn as nn
  18. from mindspore import Tensor, Model, Parameter
  19. from mindspore.ops import operations as P
  20. from mindspore import context
  21. class Net(nn.Cell):
  22. """Net definition"""
  23. def __init__(self):
  24. super(Net, self).__init__()
  25. self.inputs = Parameter(Tensor(np.ones([32, 128]).astype(np.float32)), "input")
  26. self.indices = Tensor(np.ones([4]).astype(np.int32))
  27. self.updates = Tensor(np.ones([4, 128]).astype(np.float32))
  28. self.scatter_update = P.ScatterUpdate().shard(((1, 8), (1,), (1, 8)))
  29. self.add = P.TensorAdd().shard(((8, 1), (8, 1)))
  30. self.relu = P.ReLU()
  31. def construct(self, x):
  32. out = self.scatter_update(self.inputs, self.indices, self.updates)
  33. out = self.add(x, out)
  34. out = self.relu(out)
  35. return out
  36. def test_distribute_predict():
  37. context.set_context(mode=context.GRAPH_MODE, save_graphs=True)
  38. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, full_batch=True)
  39. inputs = Tensor(np.ones([32, 128]).astype(np.float32))
  40. net = Net()
  41. model = Model(net)
  42. predict_map = model.infer_predict_layout(inputs)
  43. output = model.predict(inputs)
  44. context.reset_auto_parallel_context()
  45. return predict_map, output