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_op.py 2.4 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. 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. res = self.add(self.var, y)
  28. return res
  29. @pytest.mark.level0
  30. @pytest.mark.platform_x86_cpu
  31. @pytest.mark.env_onecard
  32. def test_assign_add():
  33. expect1 = np.array([[[[0, 2, 4.],
  34. [6, 8, 10.],
  35. [12, 14, 16.]],
  36. [[18, 20, 22.],
  37. [24, 26, 28.],
  38. [30, 32, 34.]],
  39. [[36, 38, 40.],
  40. [42, 44, 46.],
  41. [48, 50, 52.]]]])
  42. expect2 = np.array([[[[0, 3, 6],
  43. [9, 12, 15],
  44. [18, 21, 24]],
  45. [[27, 30, 33],
  46. [36, 39, 42],
  47. [45, 48, 51]],
  48. [[54, 57, 60],
  49. [63, 66, 69],
  50. [72, 75, 78]]]])
  51. x2 = Tensor(np.arange(1 * 3 * 3 * 3).reshape(1, 3, 3, 3).astype(np.float32))
  52. y2 = Tensor(np.arange(1 * 3 * 3 * 3).reshape(1, 3, 3, 3).astype(np.float32))
  53. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  54. add = AssignAdd(x2)
  55. output1 = add(y2)
  56. assert (output1.asnumpy() == expect1).all()
  57. add = AssignAdd(output1)
  58. output2 = add(y2)
  59. assert (output2.asnumpy() == expect2).all()