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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. def setup_module():
  24. context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
  25. @pytest.mark.level0
  26. @pytest.mark.platform_arm_ascend_training
  27. @pytest.mark.platform_x86_ascend_training
  28. @pytest.mark.env_onecard
  29. def test_parser_construct():
  30. class ParentNet(Cell):
  31. def __init__(self):
  32. super().__init__()
  33. self.relu = P.ReLU()
  34. def construct(self, x):
  35. return self.relu(x)
  36. class UncleNet(Cell):
  37. def __init__(self):
  38. super(UncleNet, self).__init__()
  39. self.sigmoid = P.Sigmoid()
  40. def construct(self, x):
  41. return self.sigmoid(x)
  42. class Net(UncleNet, ParentNet):
  43. def __init__(self):
  44. super().__init__()
  45. super(UncleNet, self).__init__()
  46. def construct(self, x):
  47. return super(UncleNet, self).construct(x)
  48. input_np_x = np.ones([2, 3, 4, 5]).astype(np.float32)
  49. out_np = np.ones([2, 3, 4, 5]).astype(np.float32)
  50. input_me = Tensor(input_np_x)
  51. output_grad_me = Tensor(out_np)
  52. net = Net()
  53. out_me = net(input_me)
  54. net1 = Net()
  55. grad = GradOperation(sens_param=True)
  56. grad_op = grad(net1)
  57. grad_me = grad_op(input_me, output_grad_me)
  58. assert np.allclose(input_np_x, out_me.asnumpy(), 0.001, 0.001)
  59. assert np.allclose(input_np_x, grad_me.asnumpy(), 0.001, 0.001)