• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
16import numpy as np
17import pytest
18
19import mindspore.context as context
20from mindspore import Tensor, nn
21from mindspore.common import dtype as mstype
22
23
24class CaseNet(nn.Cell):
25    def __init__(self):
26        super(CaseNet, self).__init__()
27        self.conv = nn.Conv2d(1, 1, 3)
28        self.relu = nn.ReLU()
29        self.relu1 = nn.ReLU()
30        self.softmax = nn.Softmax()
31        self.layers1 = (self.relu, self.softmax)
32        self.layers2 = (self.conv, self.relu1)
33
34    def construct(self, x, index1, index2):
35        x = self.layers1[index1](x)
36        x = self.layers2[index2](x)
37        return x
38
39
40@pytest.mark.level1
41@pytest.mark.platform_arm_ascend_training
42@pytest.mark.platform_x86_ascend_training
43@pytest.mark.platform_x86_gpu_training
44@pytest.mark.env_onecard
45def test_switch_layer():
46    context.set_context(mode=context.GRAPH_MODE)
47    net = CaseNet()
48    data = Tensor(np.ones((1, 1, 224, 224)), mstype.float32)
49    idx = Tensor(0, mstype.int32)
50    idx2 = Tensor(-1, mstype.int32)
51    value = net(data, idx, idx2)
52    relu = nn.ReLU()
53    true_value = relu(data)
54    ret = np.allclose(value.asnumpy(), true_value.asnumpy())
55    assert ret
56