• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.common.api import ms_function
21from mindspore.common.initializer import initializer
22from mindspore.common.parameter import Parameter
23from mindspore.common.tensor import Tensor
24from mindspore.nn import Cell
25from mindspore.ops.operations import Tile
26
27
28class TileNet(Cell):
29    def __init__(self, numpy_input):
30        super(TileNet, self).__init__()
31        self.Tile = Tile()
32
33        self.input_parameter = Parameter(initializer(Tensor(numpy_input), numpy_input.shape), name='x')
34
35    @ms_function
36    def construct(self, mul):
37        return self.Tile(self.input_parameter, mul)
38
39
40def ms_tile(nptype):
41    context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
42
43    input_0 = np.arange(2).reshape((2, 1, 1)).astype(nptype)
44    mul_0 = (8, 1, 1)
45    input_1 = np.arange(32).reshape((2, 4, 4)).astype(nptype)
46    mul_1 = (2, 2, 2)
47    input_2 = np.arange(1).reshape((1, 1, 1)).astype(nptype)
48    mul_2 = (1, 1, 1)
49
50    tile_net = TileNet(input_0)
51    np_expected = np.tile(input_0, mul_0)
52    ms_output = tile_net(mul_0).asnumpy()
53    np.testing.assert_array_equal(ms_output, np_expected)
54
55    tile_net = TileNet(input_1)
56    np_expected = np.tile(input_1, mul_1)
57    ms_output = tile_net(mul_1).asnumpy()
58    np.testing.assert_array_equal(ms_output, np_expected)
59
60    tile_net = TileNet(input_2)
61    np_expected = np.tile(input_2, mul_2)
62    ms_output = tile_net(mul_2).asnumpy()
63    np.testing.assert_array_equal(ms_output, np_expected)
64
65@pytest.mark.level1
66@pytest.mark.platform_x86_gpu_training
67@pytest.mark.env_onecard
68def test_tile_float16():
69    ms_tile(np.float16)
70
71@pytest.mark.level0
72@pytest.mark.platform_x86_gpu_training
73@pytest.mark.env_onecard
74def test_tile_float32():
75    ms_tile(np.float32)
76
77@pytest.mark.level0
78@pytest.mark.platform_x86_gpu_training
79@pytest.mark.env_onecard
80def test_tile_float64():
81    ms_tile(np.float64)
82
83@pytest.mark.level1
84@pytest.mark.platform_x86_gpu_training
85@pytest.mark.env_onecard
86def test_tile_int16():
87    ms_tile(np.int16)
88
89@pytest.mark.level1
90@pytest.mark.platform_x86_gpu_training
91@pytest.mark.env_onecard
92def test_tile_int32():
93    ms_tile(np.int32)
94
95@pytest.mark.level1
96@pytest.mark.platform_x86_gpu_training
97@pytest.mark.env_onecard
98def test_tile_int64():
99    ms_tile(np.int64)
100