1# Copyright 2019-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 20from mindspore import Tensor 21from mindspore.ops import operations as P 22 23def strided_slice(nptype): 24 context.set_context(mode=context.GRAPH_MODE, device_target='GPU') 25 26 x = Tensor(np.arange(0, 2*3*4*5).reshape(2, 3, 4, 5).astype(nptype)) 27 y = P.StridedSlice()(x, (1, 0, 0, 2), (2, 2, 2, 4), (1, 1, 1, 1)) 28 expect = np.array([[[[62, 63], 29 [67, 68]], 30 [[82, 83], 31 [87, 88]]]]).astype(nptype) 32 assert np.allclose(y.asnumpy(), expect) 33 34 y = P.StridedSlice()(x, (1, 0, 0, 5), (2, 2, 2, 1), (1, 1, 1, -2)) 35 expect = np.array([[[[64, 62], 36 [69, 67]], 37 [[84, 82], 38 [89, 87]]]]).astype(nptype) 39 assert np.allclose(y.asnumpy(), expect) 40 41 y = P.StridedSlice()(x, (1, 0, 0, -1), (2, 2, 2, 1), (1, 1, 1, -1)) 42 expect = np.array([[[[64, 63, 62], 43 [69, 68, 67]], 44 [[84, 83, 82], 45 [89, 88, 87]]]]).astype(nptype) 46 assert np.allclose(y.asnumpy(), expect) 47 48 y = P.StridedSlice()(x, (1, 0, -1, -2), (2, 2, 0, -5), (1, 1, -1, -2)) 49 expect = np.array([[[[78, 76], 50 [73, 71], 51 [68, 66]], 52 [[98, 96], 53 [93, 91], 54 [88, 86]]]]).astype(nptype) 55 assert np.allclose(y.asnumpy(), expect) 56 57 # ME Infer fault 58 # y = P.StridedSlice(begin_mask=0b1000, end_mask=0b0010)(x, (1, 0, 0, 2), (2, 2, 2, 4), (1, 1, 1, 1)) 59 # expect = np.array([[[[62, 63], 60 # [67, 68]], 61 # [[82, 83], 62 # [87, 88]], 63 # [[102, 103], 64 # [107, 108]]]]).astype(nptype) 65 # assert np.allclose(y.asnumpy(), expect) 66 67 op = P.StridedSlice(begin_mask=0b1000, end_mask=0b0010, ellipsis_mask=0b0100) 68 y = op(x, (1, 0, 0, 2), (2, 2, 2, 4), (1, 1, 1, 1)) 69 expect = np.array([[[[60, 61, 62, 63], 70 [65, 66, 67, 68], 71 [70, 71, 72, 73], 72 [75, 76, 77, 78]], 73 [[80, 81, 82, 83], 74 [85, 86, 87, 88], 75 [90, 91, 92, 93], 76 [95, 96, 97, 98]], 77 [[100, 101, 102, 103], 78 [105, 106, 107, 108], 79 [110, 111, 112, 113], 80 [115, 116, 117, 118]]]]).astype(nptype) 81 assert np.allclose(y.asnumpy(), expect) 82 83 x = Tensor(np.arange(0, 3*4*5).reshape(3, 4, 5).astype(nptype)) 84 y = P.StridedSlice()(x, (1, 0, 0), (2, -3, 3), (1, 1, 3)) 85 expect = np.array([[[20]]]).astype(nptype) 86 assert np.allclose(y.asnumpy(), expect) 87 88 x_np = np.arange(0, 4*5).reshape(4, 5).astype(nptype) 89 y = Tensor(x_np)[:, ::-1] 90 expect = x_np[:, ::-1] 91 assert np.allclose(y.asnumpy(), expect) 92 93 x = Tensor(np.arange(0, 2 * 3 * 4 * 5 * 4 * 3 * 2).reshape(2, 3, 4, 5, 4, 3, 2).astype(nptype)) 94 y = P.StridedSlice()(x, (1, 0, 0, 2, 1, 2, 0), (2, 2, 2, 4, 2, 3, 2), (1, 1, 1, 1, 1, 1, 2)) 95 expect = np.array([[[[[[[1498.]]], 96 [[[1522.]]]], 97 [[[[1618.]]], 98 [[[1642.]]]]], 99 [[[[[1978.]]], 100 [[[2002.]]]], 101 [[[[2098.]]], 102 [[[2122.]]]]]]]).astype(nptype) 103 assert np.allclose(y.asnumpy(), expect) 104 105@pytest.mark.level0 106@pytest.mark.platform_x86_gpu_training 107@pytest.mark.env_onecard 108def test_strided_slice_float64(): 109 strided_slice(np.float64) 110 111@pytest.mark.level0 112@pytest.mark.platform_x86_gpu_training 113@pytest.mark.env_onecard 114def test_strided_slice_float32(): 115 strided_slice(np.float32) 116 117@pytest.mark.level1 118@pytest.mark.platform_x86_gpu_training 119@pytest.mark.env_onecard 120def test_strided_slice_float16(): 121 strided_slice(np.float16) 122 123@pytest.mark.level1 124@pytest.mark.platform_x86_gpu_training 125@pytest.mark.env_onecard 126def test_strided_slice_int64(): 127 strided_slice(np.int64) 128 129@pytest.mark.level1 130@pytest.mark.platform_x86_gpu_training 131@pytest.mark.env_onecard 132def test_strided_slice_int32(): 133 strided_slice(np.int32) 134 135@pytest.mark.level1 136@pytest.mark.platform_x86_gpu_training 137@pytest.mark.env_onecard 138def test_strided_slice_int16(): 139 strided_slice(np.int16) 140 141@pytest.mark.level0 142@pytest.mark.platform_x86_gpu_training 143@pytest.mark.env_onecard 144def test_strided_slice_int8(): 145 strided_slice(np.int8) 146 147@pytest.mark.level1 148@pytest.mark.platform_x86_gpu_training 149@pytest.mark.env_onecard 150def test_strided_slice_uint64(): 151 strided_slice(np.uint64) 152 153@pytest.mark.level1 154@pytest.mark.platform_x86_gpu_training 155@pytest.mark.env_onecard 156def test_strided_slice_uint32(): 157 strided_slice(np.uint32) 158 159@pytest.mark.level1 160@pytest.mark.platform_x86_gpu_training 161@pytest.mark.env_onecard 162def test_strided_slice_uint16(): 163 strided_slice(np.uint16) 164 165@pytest.mark.level1 166@pytest.mark.platform_x86_gpu_training 167@pytest.mark.env_onecard 168def test_strided_slice_uint8(): 169 strided_slice(np.uint8) 170 171@pytest.mark.level1 172@pytest.mark.platform_x86_gpu_training 173@pytest.mark.env_onecard 174def test_strided_slice_bool(): 175 strided_slice(np.bool) 176 x = Tensor(np.arange(0, 4*4*4).reshape(4, 4, 4).astype(np.float32)) 177 y = x[-8:, :8] 178 expect = np.array([[[0., 1., 2., 3.], 179 [4., 5., 6., 7.], 180 [8., 9., 10., 11.], 181 [12., 13., 14., 15.]], 182 183 [[16., 17., 18., 19.], 184 [20., 21., 22., 23.], 185 [24., 25., 26., 27.], 186 [28., 29., 30., 31.]], 187 188 [[32., 33., 34., 35.], 189 [36., 37., 38., 39.], 190 [40., 41., 42., 43.], 191 [44., 45., 46., 47.]], 192 193 [[48., 49., 50., 51.], 194 [52., 53., 54., 55.], 195 [56., 57., 58., 59.], 196 [60., 61., 62., 63.]]]) 197 assert np.allclose(y.asnumpy(), expect) 198