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 numpy as np 16import pytest 17import mindspore.context as context 18import mindspore.nn as nn 19from mindspore import Tensor 20from mindspore.common import dtype as mstype 21from mindspore.ops import operations as P 22 23context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend") 24 25class Net(nn.Cell): 26 def __init__(self): 27 super(Net, self).__init__() 28 self.get_next = P.GetNext([mstype.float32], [(1, 1)], 1, "test") 29 30 def construct(self, x1,): 31 x = self.get_next() 32 x = x + x1 33 return x 34 35def test_pynative_synchronize_true(): 36 context.set_context(pynative_synchronize=True) 37 with pytest.raises(RuntimeError) as execinfo: 38 x1 = np.random.randn(1, 1).astype(np.float32) 39 net = Net() 40 output = net(Tensor(x1)) 41 print(output.asnumpy()) 42 assert "GetNext" in str(execinfo.value) 43 44def test_pynative_synchronize_false(): 45 context.set_context(pynative_synchronize=False) 46 with pytest.raises(RuntimeError) as execinfo: 47 x1 = np.random.randn(1, 1).astype(np.float32) 48 net = Net() 49 output = net(Tensor(x1)) 50 print(output.asnumpy()) 51 assert "Sync stream error" in str(execinfo.value) 52