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# ============================================================================ 15import pytest 16from mindspore.ops import composite as C 17import mindspore.common.dtype as mstype 18import mindspore.nn as nn 19import mindspore.context as context 20from mindspore.common.tensor import Tensor 21 22class Net(nn.Cell): 23 def construct(self, x, y): 24 while x < y: 25 x = x * x + 1 26 return x 27 28 29class GradNet(nn.Cell): 30 def __init__(self, net): 31 super().__init__() 32 self.net = net 33 self.grad_op = C.GradOperation(get_all=True) 34 35 def construct(self, x, y): 36 gradient_function = self.grad_op(self.net) 37 return gradient_function(x, y) 38 39 40@pytest.mark.level1 41@pytest.mark.platform_arm_ascend_training 42@pytest.mark.platform_x86_ascend_training 43@pytest.mark.env_onecard 44def test_while_grad(): 45 context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") 46 x = Tensor([2.0], dtype=mstype.float32) 47 y = Tensor([2.0], dtype=mstype.float32) 48 GradNet(Net())(x, y) 49