• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020-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 numpy as np
17import pytest
18
19import mindspore.context as context
20import mindspore.nn as nn
21from mindspore import Tensor
22from mindspore.ops import operations as P
23
24class Net(nn.Cell):
25    def __init__(self):
26        super(Net, self).__init__()
27        self.select = P.Select()
28
29    def construct(self, cond_op, input_x, input_y):
30        return self.select(cond_op, input_x, input_y)
31
32
33@pytest.mark.level0
34@pytest.mark.platform_x86_gpu_training
35@pytest.mark.env_onecard
36def test_select():
37    context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
38    select = Net()
39    cond = np.array([[True, False], [True, False]]).astype(np.bool)
40    x = np.array([[1.2, 1], [1, 0]]).astype(np.float32)
41    y = np.array([[1, 2], [3, 4.0]]).astype(np.float32)
42    output = select(Tensor(cond), Tensor(x), Tensor(y))
43    expect = [[1.2, 2], [1, 4.0]]
44    error = np.ones(shape=[2, 2]) * 1.0e-6
45    diff = output.asnumpy() - expect
46    assert np.all(diff < error)
47    assert np.all(-diff < error)
48
49    context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
50    x = np.array([[1, 0], [1, 0]]).astype(np.bool)
51    y = np.array([[0, 0], [1, 1]]).astype(np.bool)
52    output = select(Tensor(cond), Tensor(x), Tensor(y))
53    expect = np.array([[1, 0], [1, 1]]).astype(np.bool)
54    assert np.all(output.asnumpy() == expect)
55
56    context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
57    x = np.array([[1, 0], [1, 0]]).astype(np.bool)
58    y = np.array([[0, 0], [1, 1]]).astype(np.bool)
59    output = select(Tensor(cond), Tensor(x), Tensor(y))
60    expect = np.array([[1, 0], [1, 1]]).astype(np.bool)
61    assert np.all(output.asnumpy() == expect)
62