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_optimize_assign.py 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. from mindspore import Tensor
  19. from mindspore.nn import Cell
  20. import mindspore.ops.operations as P
  21. from mindspore.ops import functional as F
  22. from mindspore.common.parameter import Parameter
  23. class TestOptAssignNet_1(Cell):
  24. def __init__(self):
  25. super(TestOptAssignNet_1, self).__init__()
  26. self.add = P.Add()
  27. self.reduce_max = P.ReduceMax()
  28. self.param = Parameter(
  29. Tensor(np.zeros([2, 2, 2]).astype(np.float32)), name='param')
  30. def construct(self, x, y):
  31. add_res = self.add(x, y)
  32. F.depend(add_res, F.assign(self.param, add_res))
  33. return self.reduce_max(add_res)
  34. class TestOptAssignNet_2(Cell):
  35. def __init__(self):
  36. super(TestOptAssignNet_2, self).__init__()
  37. self.add = P.Add()
  38. self.param = Parameter(
  39. Tensor(np.zeros([2, 2, 2]).astype(np.float32)), name='param')
  40. def construct(self, x, y):
  41. add_res = self.add(x, y)
  42. F.depend(add_res, F.assign(self.param, add_res))
  43. return add_res
  44. def test_opt_assign_output_1():
  45. np.random.seed(0)
  46. input_x = np.random.normal(0, 1, [2, 2, 2]).astype(np.float32)
  47. input_y = np.random.normal(0, 1, [2, 2, 2]).astype(np.float32)
  48. context.set_context(mode=context.GRAPH_MODE,
  49. enable_graph_kernel=True, device_target="GPU")
  50. net = TestOptAssignNet_1()
  51. result_open_gk = net(Tensor(input_x), Tensor(input_y))
  52. context.set_context(mode=context.GRAPH_MODE,
  53. enable_graph_kernel=False, device_target="GPU")
  54. net_beta = TestOptAssignNet_1()
  55. result_close_gk = net_beta(Tensor(input_x), Tensor(input_y))
  56. res = np.allclose(result_open_gk.asnumpy(), result_close_gk.asnumpy(), rtol=1.e-4, atol=1.e-7, equal_nan=True)
  57. assert res
  58. def test_opt_assign_output_2():
  59. np.random.seed(0)
  60. input_x = np.random.normal(0, 1, [2, 2, 2]).astype(np.float32)
  61. input_y = np.random.normal(0, 1, [2, 2, 2]).astype(np.float32)
  62. context.set_context(mode=context.GRAPH_MODE,
  63. enable_graph_kernel=True, device_target="GPU")
  64. net = TestOptAssignNet_2()
  65. result_open_gk = net(Tensor(input_x), Tensor(input_y))
  66. context.set_context(mode=context.GRAPH_MODE,
  67. enable_graph_kernel=False, device_target="GPU")
  68. net_beta = TestOptAssignNet_2()
  69. result_close_gk = net_beta(Tensor(input_x), Tensor(input_y))
  70. res = np.allclose(result_open_gk.asnumpy(), result_close_gk.asnumpy(), rtol=1.e-4, atol=1.e-7, equal_nan=True)
  71. assert res
  72. @pytest.mark.level0
  73. @pytest.mark.platform_x86_gpu_training
  74. @pytest.mark.env_onecard
  75. def test_opt_assign_gpu_1():
  76. test_opt_assign_output_1()
  77. @pytest.mark.level0
  78. @pytest.mark.platform_x86_gpu_training
  79. @pytest.mark.env_onecard
  80. def test_opt_assign_gpu_2():
  81. test_opt_assign_output_2()