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# ============================================================================ 15import numpy as np 16import pytest 17 18import mindspore 19import mindspore.context as context 20import mindspore.nn as nn 21import mindspore.ops as ops 22from mindspore import Tensor 23 24context.set_context(mode=context.GRAPH_MODE, device_target='GPU') 25 26 27class TestTimeDistributed(nn.Cell): 28 def __init__(self, cell, time_axis, reshape_with_axis=None): 29 super(TestTimeDistributed, self).__init__() 30 self.time_distributed = nn.TimeDistributed(cell, time_axis, reshape_with_axis) 31 32 def construct(self, inputs): 33 return self.time_distributed(inputs) 34 35 36@pytest.mark.level0 37@pytest.mark.platform_x86_gpu_training 38@pytest.mark.env_onecard 39def test_time_distributed_conv2d(): 40 inputs = np.random.randint(0, 10, [32, 12, 10, 10]) 41 conv2d = nn.Conv2d(12, 24, 4, has_bias=False, weight_init='normal') 42 output_expect = conv2d(Tensor(inputs, mindspore.float32)).asnumpy() 43 inputs = inputs.reshape([32, 1, 12, 10, 10]).repeat(6, axis=1) 44 time_distributed = TestTimeDistributed(conv2d, time_axis=1, reshape_with_axis=0) 45 output = time_distributed(Tensor(inputs, mindspore.float32)).asnumpy() 46 for i in range(output.shape[1]): 47 assert np.all(np.abs(output[:, i, :] - output_expect) < 1e-5) 48 print("Conv2D layer wrapped successful") 49 50 51@pytest.mark.level0 52@pytest.mark.platform_x86_gpu_training 53@pytest.mark.env_onecard 54def test_time_distributed_maxpool2d(): 55 inputs = np.random.randint(0, 10, [32, 12, 10, 10]) 56 pool = nn.MaxPool2d(kernel_size=3, stride=1) 57 output_expect = pool(Tensor(inputs, mindspore.float32)).asnumpy() 58 inputs = inputs.reshape([32, 1, 12, 10, 10]).repeat(6, axis=1) 59 time_distributed = TestTimeDistributed(pool, time_axis=1, reshape_with_axis=0) 60 output = time_distributed(Tensor(inputs, mindspore.float32)).asnumpy() 61 for i in range(output.shape[1]): 62 assert np.all(output[:, i, :] == output_expect) 63 print("MaxPooling2D layer wrapped successful") 64 65 66@pytest.mark.level0 67@pytest.mark.platform_x86_gpu_training 68@pytest.mark.env_onecard 69def test_time_distributed_dense(): 70 inputs = np.random.randint(0, 10, [32, 10]) 71 dense = nn.Dense(10, 6) 72 output_expect = dense(Tensor(inputs, mindspore.float32)).asnumpy() 73 inputs = inputs.reshape([32, 1, 10]).repeat(6, axis=1) 74 time_distributed = TestTimeDistributed(dense, time_axis=1, reshape_with_axis=0) 75 output = time_distributed(Tensor(inputs, mindspore.float32)).asnumpy() 76 for i in range(output.shape[1]): 77 assert np.all(output[:, i, :] == output_expect) 78 print("Dense layer wrapped successful") 79 80 81@pytest.mark.level0 82@pytest.mark.platform_x86_gpu_training 83@pytest.mark.env_onecard 84def test_time_distributed_dense_with_reshape_axis_not_first(): 85 inputs = np.random.randint(0, 10, [32, 10]) 86 dense = nn.Dense(10, 6) 87 output_expect = dense(Tensor(inputs, mindspore.float32)).asnumpy() 88 inputs = inputs.reshape([1, 32, 10]).repeat(6, axis=0) 89 time_distributed = TestTimeDistributed(dense, time_axis=0, reshape_with_axis=1) 90 output = time_distributed(Tensor(inputs, mindspore.float32)).asnumpy() 91 for i in range(output.shape[0]): 92 assert np.all(output[i, :] == output_expect) 93 print("Dense layer wrapped successful") 94 95 96@pytest.mark.level0 97@pytest.mark.platform_x86_gpu_training 98@pytest.mark.env_onecard 99def test_time_distributed_argmax(): 100 inputs = np.random.randint(0, 10, [3, 4]) 101 argmax = ops.Argmax(output_type=mindspore.int32, axis=1) 102 output_expect = argmax(Tensor(inputs, mindspore.float32)).asnumpy() 103 inputs = inputs.reshape([3, 1, 4]).repeat(6, axis=1) 104 time_distributed = TestTimeDistributed(argmax, time_axis=1, reshape_with_axis=0) 105 output = time_distributed(Tensor(inputs, mindspore.float32)).asnumpy() 106 for i in range(output.shape[1]): 107 assert np.all(output[:, i] == output_expect) 108 print("Argmax op wrapped successful") 109 110 111@pytest.mark.level0 112@pytest.mark.platform_x86_gpu_training 113@pytest.mark.env_onecard 114def test_time_distributed_flatten(): 115 inputs = np.random.randint(0, 10, [3, 4, 5]) 116 flatten = nn.Flatten() 117 output_expect = flatten(Tensor(inputs, mindspore.float32)).asnumpy() 118 inputs = inputs.reshape([3, 1, 4, 5]).repeat(6, axis=1) 119 time_distributed = TestTimeDistributed(flatten, time_axis=1, reshape_with_axis=0) 120 output = time_distributed(Tensor(inputs, mindspore.float32)).asnumpy() 121 for i in range(output.shape[1]): 122 assert np.all(output[:, i, :] == output_expect) 123 print("Flatten op wrapped successful") 124 125 126@pytest.mark.level0 127@pytest.mark.platform_x86_gpu_training 128@pytest.mark.env_onecard 129def test_time_distributed_conv2d_no_reshape_axis(): 130 inputs = np.random.randint(0, 10, [32, 12, 10, 10]) 131 conv2d = nn.Conv2d(12, 24, 4, has_bias=False, weight_init='normal') 132 output_expect = conv2d(Tensor(inputs, mindspore.float32)).asnumpy() 133 inputs = inputs.reshape([32, 1, 12, 10, 10]).repeat(6, axis=1) 134 time_distributed = TestTimeDistributed(conv2d, time_axis=1) 135 output = time_distributed(Tensor(inputs, mindspore.float32)).asnumpy() 136 for i in range(output.shape[1]): 137 assert np.all(output[:, i, :] == output_expect) 138 print("Conv2D layer with no reshape axis wrapped successful") 139 140 141@pytest.mark.level0 142@pytest.mark.platform_x86_gpu_training 143@pytest.mark.env_onecard 144def test_time_distributed_maxpool2d_no_reshape_axis(): 145 inputs = np.random.randint(0, 10, [32, 12, 10, 10]) 146 pool = nn.MaxPool2d(kernel_size=3, stride=1) 147 output_expect = pool(Tensor(inputs, mindspore.float32)).asnumpy() 148 inputs = inputs.reshape([32, 1, 12, 10, 10]).repeat(6, axis=1) 149 time_distributed = TestTimeDistributed(pool, time_axis=1) 150 output = time_distributed(Tensor(inputs, mindspore.float32)).asnumpy() 151 for i in range(output.shape[1]): 152 assert np.all(output[:, i, :] == output_expect) 153 print("MaxPooling2D layer with no reshape axis wrapped successful") 154 155 156@pytest.mark.level0 157@pytest.mark.platform_x86_gpu_training 158@pytest.mark.env_onecard 159def test_time_distributed_dense_no_reshape_axis(): 160 inputs = np.random.randint(0, 10, [32, 10]) 161 dense = nn.Dense(10, 6) 162 output_expect = dense(Tensor(inputs, mindspore.float32)).asnumpy() 163 inputs = inputs.reshape([32, 1, 10]).repeat(6, axis=1) 164 time_distributed = TestTimeDistributed(dense, time_axis=1) 165 output = time_distributed(Tensor(inputs, mindspore.float32)).asnumpy() 166 for i in range(output.shape[1]): 167 assert np.all(output[:, i, :] == output_expect) 168 print("Dense layer with no reshape axis wrapped successful") 169 170 171@pytest.mark.level0 172@pytest.mark.platform_x86_gpu_training 173@pytest.mark.env_onecard 174def test_time_distributed_argmax_no_reshape_axis(): 175 inputs = np.random.randint(0, 10, [3, 4]) 176 argmax = ops.Argmax(output_type=mindspore.int32, axis=1) 177 output_expect = argmax(Tensor(inputs, mindspore.float32)).asnumpy() 178 inputs = inputs.reshape([3, 1, 4]).repeat(6, axis=1) 179 time_distributed = TestTimeDistributed(argmax, time_axis=1) 180 output = time_distributed(Tensor(inputs, mindspore.float32)).asnumpy() 181 for i in range(output.shape[1]): 182 assert np.all(output[:, i] == output_expect) 183 print("Argmax op with no reshape axis wrapped successful") 184 185 186@pytest.mark.level0 187@pytest.mark.platform_x86_gpu_training 188@pytest.mark.env_onecard 189def test_time_distributed_flatten_no_reshape_axis(): 190 inputs = np.random.randint(0, 10, [3, 4, 5]) 191 flatten = nn.Flatten() 192 output_expect = flatten(Tensor(inputs, mindspore.float32)).asnumpy() 193 inputs = inputs.reshape([3, 1, 4, 5]).repeat(6, axis=1) 194 time_distributed = TestTimeDistributed(flatten, time_axis=1) 195 output = time_distributed(Tensor(inputs, mindspore.float32)).asnumpy() 196 for i in range(output.shape[1]): 197 assert np.all(output[:, i, :] == output_expect) 198 print("Flatten op with no reshape axis wrapped successful") 199