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_001_single_while.py 2.2 kB

4 years ago
4 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 pytest
  16. from mindspore.common import dtype as mstype
  17. from mindspore import nn
  18. from mindspore import Tensor
  19. from mindspore.ops import composite as C
  20. from mindspore import context
  21. context.set_context(mode=context.GRAPH_MODE)
  22. class ForwardNet(nn.Cell):
  23. def construct(self, x, y):
  24. y = y + 10
  25. while x < y:
  26. x = (x + 2) * (y - 9)
  27. y = y + 2
  28. x = x + 5
  29. return x
  30. class BackwardNet(nn.Cell):
  31. def __init__(self, forward_net):
  32. super(BackwardNet, self).__init__()
  33. self.forward_net = forward_net
  34. self.grad = C.GradOperation()
  35. def construct(self, *inputs):
  36. grads = self.grad(self.forward_net)(*inputs)
  37. return grads
  38. @pytest.mark.level0
  39. @pytest.mark.platform_x86_gpu_training
  40. @pytest.mark.platform_arm_ascend_training
  41. @pytest.mark.platform_x86_ascend_training
  42. @pytest.mark.env_onecard
  43. def test_forward():
  44. c1 = Tensor([0], mstype.int32)
  45. c2 = Tensor([0], mstype.int32)
  46. expect = Tensor([75], mstype.int32)
  47. forward_net = ForwardNet()
  48. output = forward_net(c1, c2)
  49. assert expect == output
  50. @pytest.mark.level0
  51. @pytest.mark.platform_x86_gpu_training
  52. @pytest.mark.platform_arm_ascend_training
  53. @pytest.mark.platform_x86_ascend_training
  54. @pytest.mark.env_onecard
  55. def test_backward():
  56. c1 = Tensor([0], mstype.int32)
  57. c2 = Tensor([0], mstype.int32)
  58. expect = Tensor([75], mstype.int32)
  59. forward_net = ForwardNet()
  60. output = forward_net(c1, c2)
  61. assert expect == output