• 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# ============================================================================
15import numpy as np
16import pytest
17
18from mindspore import Tensor
19from mindspore.ops import composite as C
20from mindspore.ops.operations import _inner_ops as inner
21import mindspore.nn as nn
22import mindspore.context as context
23
24def sequence_mask(x, maxlen):
25    return C.sequence_mask(Tensor(x.astype(np.int32)), maxlen)
26
27@pytest.mark.level0
28@pytest.mark.platform_x86_gpu_training
29@pytest.mark.env_onecard
30def test_sequence_mask_1d():
31    context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
32    a = np.array([2, 3, 1])
33    maxlen = 4
34    ms_out = sequence_mask(a, maxlen)
35    expected_out = Tensor(np.array([[True, True, False, False],
36                                    [True, True, True, False],
37                                    [True, False, False, False]]))
38    np.testing.assert_array_equal(expected_out.asnumpy(), ms_out.asnumpy())
39
40@pytest.mark.level0
41@pytest.mark.platform_x86_gpu_training
42@pytest.mark.env_onecard
43def test_sequence_mask_2d():
44    context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
45    a = np.array([[0, 1, 3, 2], [1, 4, 4, 2]])
46    maxlen = 6
47    ms_out = sequence_mask(a, maxlen)
48    expected_out = Tensor(np.array([[[False, False, False, False, False, False],
49                                     [True, False, False, False, False, False],
50                                     [True, True, True, False, False, False],
51                                     [True, True, False, False, False, False]],
52                                    [[True, False, False, False, False, False],
53                                     [True, True, True, True, False, False],
54                                     [True, True, True, True, False, False],
55                                     [True, True, False, False, False, False]]]))
56    np.testing.assert_array_equal(expected_out.asnumpy(), ms_out.asnumpy())
57
58@pytest.mark.level0
59@pytest.mark.platform_x86_gpu_training
60@pytest.mark.env_onecard
61def test_sequence_mask_3d():
62    context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
63    a = np.array([[[2, 2], [1, 1]],
64                  [[2, 0], [2, 1]],
65                  [[0, 0], [0, 0]]])
66    maxlen = 2
67    ms_out = sequence_mask(a, maxlen)
68    expected_out = Tensor(np.array([[[[True, True], [True, True]], [[True, False], [True, False]]],
69                                    [[[True, True], [False, False]], [[True, True], [True, False]]],
70                                    [[[False, False], [False, False]], [[False, False], [False, False]]]]))
71
72    np.testing.assert_array_equal(expected_out.asnumpy(), ms_out.asnumpy())
73
74@pytest.mark.level0
75@pytest.mark.platform_x86_gpu_training
76@pytest.mark.env_onecard
77def test_sequence_mask_maxlen_1():
78    context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
79    a = np.array([[[0, 1], [1, 1]],
80                  [[1, 0], [1, 1]],
81                  [[0, 1], [0, 1]]])
82    maxlen = 1
83    ms_out = sequence_mask(a, maxlen)
84    expected_out = Tensor(np.array([[[[False], [True]], [[True], [True,]]],
85                                    [[[True], [False]], [[True], [True]]],
86                                    [[[False], [True]], [[False], [True]]]]))
87
88    np.testing.assert_array_equal(expected_out.asnumpy(), ms_out.asnumpy())
89
90@pytest.mark.level0
91@pytest.mark.platform_x86_gpu_training
92@pytest.mark.env_onecard
93def test_sequence_mask_dynamic():
94    class SequenceMaskDynamicNet1(nn.Cell):
95        def __init__(self, maxlen):
96            super(SequenceMaskDynamicNet1, self).__init__()
97            self.maxlen = maxlen
98            self.convert_to_dynamic_shape = inner.GpuConvertToDynamicShape()
99
100        def construct(self, x):
101            converted_to_dynamic_shape = self.convert_to_dynamic_shape(x)
102            return C.sequence_mask(converted_to_dynamic_shape, self.maxlen)
103
104    class SequenceMaskDynamicNet2(nn.Cell):
105        def __init__(self):
106            super(SequenceMaskDynamicNet2, self).__init__()
107            self.convert_to_dynamic_shape = inner.GpuConvertToDynamicShape()
108
109        def construct(self, x):
110            converted_to_dynamic_shape = self.convert_to_dynamic_shape(x)
111            return C.sequence_mask(converted_to_dynamic_shape)
112
113    context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
114
115    sequence_mask_net = SequenceMaskDynamicNet1(4)
116
117    a = Tensor(np.array([0, 1, 0, 2, 0, 5]))
118    ms_out = sequence_mask_net(a)
119    expected_out = Tensor(np.array([[False, False, False, False],
120                                    [True, False, False, False],
121                                    [False, False, False, False],
122                                    [True, True, False, False],
123                                    [False, False, False, False],
124                                    [True, True, True, True]]))
125    np.testing.assert_array_equal(expected_out.asnumpy(), ms_out.asnumpy())
126
127    a = Tensor(np.array([[4, 3, 0], [0, 1, 3]]))
128    ms_out = sequence_mask_net(a)
129    expected_out = Tensor(np.array([[[True, True, True, True],
130                                     [True, True, True, False],
131                                     [False, False, False, False]],
132                                    [[False, False, False, False],
133                                     [True, False, False, False],
134                                     [True, True, True, False]]]))
135    np.testing.assert_array_equal(expected_out.asnumpy(), ms_out.asnumpy())
136
137    net_without_maxlen = SequenceMaskDynamicNet2()
138
139    context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
140    a = np.array([2, 3, 1])
141    ms_out = net_without_maxlen(Tensor(a))
142    expected_out = Tensor(np.array([[True, True, False],
143                                    [True, True, True],
144                                    [True, False, False]]))
145    np.testing.assert_array_equal(expected_out.asnumpy(), ms_out.asnumpy())
146
147
148def sequence_mask_optional(x):
149    return C.sequence_mask(Tensor(x.astype(np.int32)))
150
151
152@pytest.mark.level0
153@pytest.mark.platform_x86_gpu_training
154@pytest.mark.env_onecard
155def test_sequence_mask_optional_maxlen():
156    context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
157    a = np.array([2, 3, 1])
158    ms_out = sequence_mask_optional(a)
159    expected_out = Tensor(np.array([[True, True, False],
160                                    [True, True, True],
161                                    [True, False, False]]))
162    np.testing.assert_array_equal(expected_out.asnumpy(), ms_out.asnumpy())
163
164    context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
165    a = np.array([2, 3, 1])
166    ms_out = sequence_mask_optional(a)
167    expected_out = Tensor(np.array([[True, True, False],
168                                    [True, True, True],
169                                    [True, False, False]]))
170    np.testing.assert_array_equal(expected_out.asnumpy(), ms_out.asnumpy())
171