1# Copyright 2022 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 mindspore.context as context 16from mindspore import Tensor, nn 17from mindspore.common import dtype as mstype 18from mindspore import Parameter 19 20 21class IfNet(nn.Cell): 22 def __init__(self): 23 super().__init__() 24 self.param_a = Parameter(Tensor(10, mstype.float32), name="a") 25 self.zero = Parameter(Tensor(0, mstype.float32), name="zero") 26 27 def construct(self, x): 28 out = self.zero 29 out1 = self.zero 30 if x > 0: 31 out = out + self.param_a 32 out1 = out1 * self.param_a 33 out1 += out 34 out1 *= out 35 return out, out1 36 37 38def test_if_ge(): 39 """ 40 Feature: Control flow(if) implement 41 Description: test if with ge backend. 42 Expectation: success. 43 """ 44 context.set_context(mode=context.GRAPH_MODE) 45 net = IfNet() 46 x = Tensor(3, mstype.int32) 47 out0, out1 = net(x) 48 assert out0 == Tensor(10, mstype.float32) 49 assert out1 == Tensor(100, mstype.float32) 50 51if __name__ == "__main__": 52 test_if_ge() 53