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_assign_add.py 2.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. import numpy as np
  16. import pytest
  17. import mindspore.context as context
  18. import mindspore.nn as nn
  19. from mindspore import Tensor, Parameter
  20. from mindspore.ops import operations as P
  21. class AssignAdd(nn.Cell):
  22. def __init__(self, value):
  23. super(AssignAdd, self).__init__()
  24. self.var = Parameter(value, name="var")
  25. self.add = P.AssignAdd()
  26. def construct(self, y):
  27. self.add(self.var, y)
  28. return self.var
  29. def get_output(x2, y2, enable_graph_kernel=False):
  30. context.set_context(enable_graph_kernel=enable_graph_kernel)
  31. add = AssignAdd(x2)
  32. result_gk_on_1 = add(y2)
  33. add_2 = AssignAdd(result_gk_on_1)
  34. result_gk_on_2 = add_2(y2)
  35. output = [result_gk_on_1, result_gk_on_2]
  36. return output
  37. def assign_add():
  38. x2 = Tensor(np.arange(1 * 3 * 3 * 3).reshape(1, 3, 3, 3).astype(np.float32))
  39. y2 = Tensor(np.arange(1 * 3 * 3 * 3).reshape(1, 3, 3, 3).astype(np.float32))
  40. expect = get_output(x2, y2, False)
  41. output = get_output(x2, y2, True)
  42. e1, e2 = list(expect)
  43. o1, o2 = list(output)
  44. assert np.allclose(o1.asnumpy(), e1.asnumpy())
  45. assert np.allclose(o2.asnumpy(), e2.asnumpy())
  46. @pytest.mark.level0
  47. @pytest.mark.platform_x86_gpu_training
  48. @pytest.mark.env_onecard
  49. def test_assign_add_gpu():
  50. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  51. assign_add()
  52. @pytest.mark.level0
  53. @pytest.mark.platform_arm_ascend_training
  54. @pytest.mark.platform_x86_ascend_training
  55. @pytest.mark.env_onecard
  56. def test_assign_add_ascend():
  57. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  58. assign_add()