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# ============================================================================ 15 16import pytest 17import mindspore.context as context 18import mindspore.nn as nn 19 20context.set_context(mode=context.GRAPH_MODE) 21recompute_prefix = 'recompute_' 22 23 24class Net(nn.Cell): 25 def __init__(self): 26 super(Net, self).__init__() 27 self.pool = nn.MaxPool2d(kernel_size=2, stride=2) 28 29 def construct(self, input_x): 30 output = self.pool(input_x) 31 return output 32 33 34def test_set_recompute_true(): 35 net = Net() 36 net.pool.recompute() 37 assert net.pool.get_scope() == recompute_prefix 38 39def test_set_recompute_true_with_mp_comm_recompute(): 40 net = Net() 41 net.pool.recompute(mp_comm_recompute=True) 42 assert net.pool.get_scope() == recompute_prefix 43 44def test_set_recompute_true_with_mp_comm_recompute_false(): 45 net = Net() 46 net.pool.recompute(mp_comm_recompute=False) 47 assert net.pool.get_scope() == recompute_prefix 48 49def test_set_recompute_true_twice(): 50 net = Net() 51 net.pool.recompute() 52 with pytest.raises(RuntimeError): 53 net.pool.recompute() 54