• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2019 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
24context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
25
26class NetGatherD(nn.Cell):
27    def __init__(self, dim=1):
28        super(NetGatherD, self).__init__()
29        self.gatherd = P.GatherD()
30        self.dim = int(dim)
31
32    def construct(self, x, index):
33        return self.gatherd(x, self.dim, index)
34
35
36@pytest.mark.level0
37@pytest.mark.platform_x86_cpu
38@pytest.mark.env_onecard
39def test_gatherd_fp32():
40    prop = 100 if np.random.random() > 0.5 else -100
41    x = np.random.randn(5, 5, 5).astype(np.float32) * prop
42    index = np.random.randint(0, 5, (5, 3, 5)).astype(np.int32)
43    dim = 1
44
45    gatherd = NetGatherD(dim)
46    output = gatherd(Tensor(x), Tensor(index))
47
48    expect = np.zeros(index.shape).astype(np.float32)
49    for i in range(index.shape[0]):
50        for j in range(index.shape[1]):
51            for k in range(index.shape[2]):
52                expect[i, j, k] = x[i, index[i, j, k], k]
53    error = np.ones(shape=expect.shape) * 1.0e-6
54    assert np.all(np.abs(output.asnumpy() - expect) < error)
55
56
57@pytest.mark.level0
58@pytest.mark.platform_x86_cpu
59@pytest.mark.env_onecard
60def test_gatherd_fp16():
61    prop = 100 if np.random.random() > 0.5 else -100
62    x = np.random.randn(5, 5, 5).astype(np.float16) * prop
63    index = np.random.randint(0, 5, (3, 5, 5)).astype(np.int64)
64    dim = 0
65
66    gatherd = NetGatherD(dim)
67    output = gatherd(Tensor(x), Tensor(index))
68
69    expect = np.zeros(index.shape).astype(np.float16)
70    for i in range(index.shape[0]):
71        for j in range(index.shape[1]):
72            for k in range(index.shape[2]):
73                expect[i, j, k] = x[index[i, j, k], j, k]
74    error = np.ones(shape=expect.shape) * 1.0e-6
75    assert np.all(np.abs(output.asnumpy() - expect) < error)
76
77
78
79@pytest.mark.level0
80@pytest.mark.platform_x86_cpu
81@pytest.mark.env_onecard
82def test_gatherd_int32():
83    prop = 100 if np.random.random() > 0.5 else -100
84    x = np.random.randn(5, 5, 5).astype(np.int32) * prop
85    index = np.random.randint(0, 5, (5, 5, 8)).astype(np.int32)
86    dim = -1
87
88    gatherd = NetGatherD(dim)
89    output = gatherd(Tensor(x), Tensor(index))
90
91    expect = np.zeros(index.shape).astype(np.int32)
92    for i in range(index.shape[0]):
93        for j in range(index.shape[1]):
94            for k in range(index.shape[2]):
95                expect[i, j, k] = x[i, j, index[i, j, k]]
96    assert np.all(output.asnumpy() == expect)
97
98
99@pytest.mark.level0
100@pytest.mark.platform_x86_cpu
101@pytest.mark.env_onecard
102def test_gatherd_bool():
103    prop = 100 if np.random.random() > 0.5 else -100
104    x = np.random.randn(5, 5, 5).astype(np.int32) * prop
105    x = (x >= 0).astype(np.bool)
106    index = np.random.randint(0, 5, (5, 5, 8)).astype(np.int32)
107    dim = -1
108
109    gatherd = NetGatherD(dim)
110    output = gatherd(Tensor(x), Tensor(index))
111
112    expect = np.zeros(index.shape).astype(np.bool)
113    for i in range(index.shape[0]):
114        for j in range(index.shape[1]):
115            for k in range(index.shape[2]):
116                expect[i, j, k] = x[i, j, index[i, j, k]]
117    assert np.all(output.asnumpy() == expect)
118