• 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
20from mindspore import Tensor
21from mindspore.ops import operations as P
22
23def reshape(nptype):
24    context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
25
26    reshape_op = P.Reshape()
27    data = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]).astype(nptype)
28    input_tensor = Tensor(np.array(data))
29
30    new_shape = (2, 6)
31    output_tensor = reshape_op(input_tensor, new_shape)
32    assert new_shape == output_tensor.shape
33    np.testing.assert_array_equal(output_tensor.asnumpy().flatten(), data)
34
35    new_shape = (6, 2)
36    output_tensor = reshape_op(input_tensor, new_shape)
37    assert new_shape == output_tensor.shape
38    np.testing.assert_array_equal(output_tensor.asnumpy().flatten(), data)
39
40    new_shape = (3, 4)
41    output_tensor = reshape_op(input_tensor, new_shape)
42    assert new_shape == output_tensor.shape
43    np.testing.assert_array_equal(output_tensor.asnumpy().flatten(), data)
44
45    new_shape = (4, 3)
46    output_tensor = reshape_op(input_tensor, new_shape)
47    assert new_shape == output_tensor.shape
48    np.testing.assert_array_equal(output_tensor.asnumpy().flatten(), data)
49
50    new_shape = (1, 12)
51    output_tensor = reshape_op(input_tensor, new_shape)
52    assert new_shape == output_tensor.shape
53    np.testing.assert_array_equal(output_tensor.asnumpy().flatten(), data)
54
55    new_shape = (12, 1)
56    output_tensor = reshape_op(input_tensor, new_shape)
57    assert new_shape == output_tensor.shape
58    np.testing.assert_array_equal(output_tensor.asnumpy().flatten(), data)
59
60def reshape_bool():
61    context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
62
63    reshape_op = P.Reshape()
64    data = np.array([True, True, False, True, False, False, True, False, False, False, False, False])
65    input_tensor = Tensor(np.array(data))
66
67    new_shape = (2, 6)
68    output_tensor = reshape_op(input_tensor, new_shape)
69    assert new_shape == output_tensor.shape
70    np.testing.assert_array_equal(output_tensor.asnumpy().flatten(), data)
71
72    new_shape = (6, 2)
73    output_tensor = reshape_op(input_tensor, new_shape)
74    assert new_shape == output_tensor.shape
75    np.testing.assert_array_equal(output_tensor.asnumpy().flatten(), data)
76
77    new_shape = (3, 4)
78    output_tensor = reshape_op(input_tensor, new_shape)
79    assert new_shape == output_tensor.shape
80    np.testing.assert_array_equal(output_tensor.asnumpy().flatten(), data)
81
82    new_shape = (4, 3)
83    output_tensor = reshape_op(input_tensor, new_shape)
84    assert new_shape == output_tensor.shape
85    np.testing.assert_array_equal(output_tensor.asnumpy().flatten(), data)
86
87    new_shape = (1, 12)
88    output_tensor = reshape_op(input_tensor, new_shape)
89    assert new_shape == output_tensor.shape
90    np.testing.assert_array_equal(output_tensor.asnumpy().flatten(), data)
91
92    new_shape = (12, 1)
93    output_tensor = reshape_op(input_tensor, new_shape)
94    assert new_shape == output_tensor.shape
95    np.testing.assert_array_equal(output_tensor.asnumpy().flatten(), data)
96
97
98@pytest.mark.level0
99@pytest.mark.platform_x86_gpu_training
100@pytest.mark.env_onecard
101def test_reshape_float():
102    reshape(np.float32)
103
104@pytest.mark.level0
105@pytest.mark.platform_x86_gpu_training
106@pytest.mark.env_onecard
107def test_reshape_float16():
108    reshape(np.float16)
109
110@pytest.mark.level1
111@pytest.mark.platform_x86_gpu_training
112@pytest.mark.env_onecard
113def test_reshape_int32():
114    reshape(np.int32)
115
116@pytest.mark.level1
117@pytest.mark.platform_x86_gpu_training
118@pytest.mark.env_onecard
119def test_reshape_uint8():
120    reshape(np.uint8)
121
122@pytest.mark.level1
123@pytest.mark.platform_x86_gpu_training
124@pytest.mark.env_onecard
125def test_reshape_bool():
126    reshape_bool()
127