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.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # Copyright 2019 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 pytest
  16. from mindspore import Tensor
  17. from mindspore.ops import operations as P
  18. import mindspore.nn as nn
  19. import numpy as np
  20. import mindspore.context as context
  21. class AssignAdd(nn.Cell):
  22. def __init__( self):
  23. super(AssignAdd, self).__init__()
  24. self.add = P.AssignAdd()
  25. def construct(self, x, y):
  26. res = self.add(x, y)
  27. return res
  28. @pytest.mark.level0
  29. @pytest.mark.platform_x86_gpu_training
  30. @pytest.mark.env_onecard
  31. def test_assign_add():
  32. expect1 = np.array([[[[ 0, 2, 4.],
  33. [ 6, 8, 10.],
  34. [12, 14, 16.]],
  35. [[18, 20, 22.],
  36. [24, 26, 28.],
  37. [30, 32, 34.]],
  38. [[36, 38, 40.],
  39. [42, 44, 46.],
  40. [48, 50, 52.]]]])
  41. expect2 = np.array([[[[ 0, 3, 6],
  42. [ 9, 12, 15],
  43. [18, 21, 24]],
  44. [[27, 30, 33],
  45. [36, 39, 42],
  46. [45, 48, 51]],
  47. [[54, 57, 60],
  48. [63, 66, 69],
  49. [72, 75, 78]]]])
  50. x = Tensor(np.arange(1 * 3 * 3 * 3).reshape(1, 3, 3, 3).astype(np.float32))
  51. y = Tensor(np.arange(1 * 3 * 3 * 3).reshape(1, 3, 3, 3).astype(np.float32))
  52. context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU')
  53. add = AssignAdd()
  54. output1 = add(x, y)
  55. assert (output1.asnumpy() == expect1).all()
  56. output2 = add(output1, y)
  57. assert (output2.asnumpy() == expect2).all()
  58. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  59. add = AssignAdd()
  60. output1 = add(x, y)
  61. assert (output1.asnumpy() == expect1).all()
  62. output2 = add(output1, y)
  63. assert (output2.asnumpy() == expect2).all()