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_parser_construct.py 2.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. """ test_parser_construct """
  16. import pytest
  17. import numpy as np
  18. from mindspore import context
  19. from mindspore.nn import Cell
  20. from mindspore.common.tensor import Tensor
  21. from mindspore.ops import operations as P
  22. from mindspore.ops.composite import GradOperation
  23. from mindspore.common.parameter import Parameter
  24. def setup_module():
  25. context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
  26. @pytest.mark.level0
  27. @pytest.mark.platform_arm_ascend_training
  28. @pytest.mark.platform_x86_ascend_training
  29. @pytest.mark.env_onecard
  30. def test_parser_construct():
  31. class ParentNet(Cell):
  32. def __init__(self):
  33. super().__init__()
  34. self.relu = P.ReLU()
  35. def construct(self, x):
  36. return self.relu(x)
  37. class UncleNet(Cell):
  38. def __init__(self):
  39. super(UncleNet, self).__init__()
  40. self.sigmoid = P.Sigmoid()
  41. def construct(self, x):
  42. return self.sigmoid(x)
  43. class Net(UncleNet, ParentNet):
  44. def __init__(self):
  45. super().__init__()
  46. super(UncleNet, self).__init__()
  47. def construct(self, x):
  48. return super(UncleNet, self).construct(x)
  49. input_np_x = np.ones([2, 3, 4, 5]).astype(np.float32)
  50. out_np = np.ones([2, 3, 4, 5]).astype(np.float32)
  51. input_me = Tensor(input_np_x)
  52. output_grad_me = Tensor(out_np)
  53. net = Net()
  54. out_me = net(input_me)
  55. net1 = Net()
  56. grad = GradOperation(sens_param=True)
  57. grad_op = grad(net1)
  58. grad_me = grad_op(input_me, output_grad_me)
  59. assert np.allclose(input_np_x, out_me.asnumpy(), 0.001, 0.001)
  60. assert np.allclose(input_np_x, grad_me.asnumpy(), 0.001, 0.001)
  61. @pytest.mark.level0
  62. @pytest.mark.platform_arm_ascend_training
  63. @pytest.mark.platform_x86_ascend_training
  64. @pytest.mark.env_onecard
  65. def test_sit_parser_input_parameter():
  66. def tensor_add(x, y):
  67. add = P.TensorAdd()
  68. z = add(x, y)
  69. return z
  70. x = Tensor(np.ones([2, 2]).astype(np.float32))
  71. x = Parameter(x, name="x")
  72. y = Tensor(np.ones([2, 2]).astype(np.float32))
  73. y = Parameter(y, name="y")
  74. grad = GradOperation(get_all=True, get_by_list=False, sens_param=False)
  75. with pytest.raises(TypeError):
  76. grad(tensor_add)(x, y)