• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020 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
19from mindspore import Tensor
20from mindspore.ops import operations as P
21from mindspore.ops.operations import _inner_ops as inner
22import mindspore.nn as nn
23import mindspore.context as context
24
25class DynamicShapeNet(nn.Cell):
26    def __init__(self):
27        super(DynamicShapeNet, self).__init__()
28        self.convert_to_dynamic_shape_op = inner.GpuConvertToDynamicShape()
29        self.dynamic_shape_op = P.DynamicShape()
30
31    def construct(self, x):
32        x_dynamic_shape = self.convert_to_dynamic_shape_op(x)
33        return self.dynamic_shape_op(x_dynamic_shape)
34
35
36def dynamic_shape(np_type):
37    context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
38
39    dynamic_shape_net = DynamicShapeNet()
40
41    shape = (1,)
42    x = Tensor(np.zeros(shape).astype(np_type))
43    ms_out = dynamic_shape_net(x).asnumpy()
44    expected = np.array(shape)
45    np.testing.assert_array_equal(ms_out, expected)
46
47    shape = (7,)
48    x = Tensor(np.zeros(shape).astype(np_type))
49    ms_out = dynamic_shape_net(x).asnumpy()
50    expected = np.array(shape)
51    np.testing.assert_array_equal(ms_out, expected)
52
53    shape = (1, 1)
54    x = Tensor(np.zeros(shape).astype(np_type))
55    ms_out = dynamic_shape_net(x).asnumpy()
56    expected = np.array(shape)
57    np.testing.assert_array_equal(ms_out, expected)
58
59    shape = (1, 7)
60    x = Tensor(np.zeros(shape).astype(np_type))
61    ms_out = dynamic_shape_net(x).asnumpy()
62    expected = np.array(shape)
63    np.testing.assert_array_equal(ms_out, expected)
64
65    shape = (3, 1)
66    x = Tensor(np.zeros(shape).astype(np_type))
67    ms_out = dynamic_shape_net(x).asnumpy()
68    expected = np.array(shape)
69    np.testing.assert_array_equal(ms_out, expected)
70
71    shape = (2, 4)
72    x = Tensor(np.zeros(shape).astype(np_type))
73    ms_out = dynamic_shape_net(x).asnumpy()
74    expected = np.array(shape)
75    np.testing.assert_array_equal(ms_out, expected)
76
77    shape = (1, 1, 1)
78    x = Tensor(np.zeros(shape).astype(np_type))
79    ms_out = dynamic_shape_net(x).asnumpy()
80    expected = np.array(shape)
81    np.testing.assert_array_equal(ms_out, expected)
82
83    shape = (1, 5, 3)
84    x = Tensor(np.zeros(shape).astype(np_type))
85    ms_out = dynamic_shape_net(x).asnumpy()
86    expected = np.array(shape)
87    np.testing.assert_array_equal(ms_out, expected)
88
89    shape = (2, 3, 1, 3, 1)
90    x = Tensor(np.zeros(shape).astype(np_type))
91    ms_out = dynamic_shape_net(x).asnumpy()
92    expected = np.array(shape)
93    np.testing.assert_array_equal(ms_out, expected)
94
95@pytest.mark.level1
96@pytest.mark.platform_x86_gpu_training
97@pytest.mark.env_onecard
98def test_dynamic_shape_int32():
99    dynamic_shape(np.int32)
100
101@pytest.mark.level0
102@pytest.mark.platform_x86_gpu_training
103@pytest.mark.env_onecard
104def test_dynamic_shape_float16():
105    dynamic_shape(np.float16)
106
107@pytest.mark.level0
108@pytest.mark.platform_x86_gpu_training
109@pytest.mark.env_onecard
110def test_dynamic_shape_float32():
111    dynamic_shape(np.float32)
112
113@pytest.mark.level1
114@pytest.mark.platform_x86_gpu_training
115@pytest.mark.env_onecard
116def test_dynamic_shape_bool():
117    dynamic_shape(np.bool)
118