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 20import mindspore.nn as nn 21from mindspore import Tensor 22from mindspore.ops import operations as P 23from mindspore.ops.operations import _inner_ops as inner 24 25class NetZerosLike(nn.Cell): 26 def __init__(self): 27 super(NetZerosLike, self).__init__() 28 self.zeros_like = P.ZerosLike() 29 30 def construct(self, x): 31 return self.zeros_like(x) 32 33 34@pytest.mark.level0 35@pytest.mark.platform_x86_gpu_training 36@pytest.mark.env_onecard 37def test_ZerosLike(): 38 x0_np = np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(np.float32) 39 x1_np = np.random.uniform(-2, 2, 1).astype(np.float32) 40 41 x0 = Tensor(x0_np) 42 x1 = Tensor(x1_np) 43 44 context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU") 45 zeros_like = NetZerosLike() 46 output0 = zeros_like(x0) 47 expect0 = np.zeros_like(x0_np) 48 diff0 = output0.asnumpy() - expect0 49 error0 = np.ones(shape=expect0.shape) * 1.0e-5 50 assert np.all(diff0 < error0) 51 assert output0.shape == expect0.shape 52 53 output1 = zeros_like(x1) 54 expect1 = np.zeros_like(x1_np) 55 diff1 = output1.asnumpy() - expect1 56 error1 = np.ones(shape=expect1.shape) * 1.0e-5 57 assert np.all(diff1 < error1) 58 assert output1.shape == expect1.shape 59 60 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 61 zeros_like = NetZerosLike() 62 output0 = zeros_like(x0) 63 expect0 = np.zeros_like(x0_np) 64 diff0 = output0.asnumpy() - expect0 65 error0 = np.ones(shape=expect0.shape) * 1.0e-5 66 assert np.all(diff0 < error0) 67 assert output0.shape == expect0.shape 68 69 output1 = zeros_like(x1) 70 expect1 = np.zeros_like(x1_np) 71 diff1 = output1.asnumpy() - expect1 72 error1 = np.ones(shape=expect1.shape) * 1.0e-5 73 assert np.all(diff1 < error1) 74 assert output1.shape == expect1.shape 75 76 77class ZerosLikeDynamicNet(nn.Cell): 78 def __init__(self): 79 super(ZerosLikeDynamicNet, self).__init__() 80 self.gpu_convert_to_dynamic_shape = inner.GpuConvertToDynamicShape() 81 self.zeros_like = P.ZerosLike() 82 83 def construct(self, x): 84 converted_to_dynamic = self.gpu_convert_to_dynamic_shape(x) 85 return self.zeros_like(converted_to_dynamic) 86 87 88def zeros_like_dynamic(x): 89 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 90 net = ZerosLikeDynamicNet() 91 return net(x) 92 93@pytest.mark.level1 94@pytest.mark.platform_x86_gpu_training 95@pytest.mark.env_onecard 96def test_zeros_like_dynamic_bool(): 97 x = Tensor(np.arange(120).reshape(3, 4, 1, 2, 5).astype(np.bool)) 98 output = zeros_like_dynamic(x) 99 expected = np.zeros([3, 4, 1, 2, 5]) 100 np.testing.assert_array_equal(output.asnumpy(), expected) 101 102@pytest.mark.level1 103@pytest.mark.platform_x86_gpu_training 104@pytest.mark.env_onecard 105def test_zeros_like_dynamic_int8(): 106 x = Tensor(np.arange(24).reshape(1, 4, 1, 6).astype(np.int8)) 107 output = zeros_like_dynamic(x) 108 expected = np.zeros([1, 4, 1, 6]) 109 np.testing.assert_array_equal(output.asnumpy(), expected) 110 111@pytest.mark.level1 112@pytest.mark.platform_x86_gpu_training 113@pytest.mark.env_onecard 114def test_zeros_like_dynamic_uint8(): 115 x = Tensor(np.arange(30).reshape(3, 2, 5).astype(np.uint8)) 116 output = zeros_like_dynamic(x) 117 expected = np.zeros([3, 2, 5]) 118 np.testing.assert_array_equal(output.asnumpy(), expected) 119 120@pytest.mark.level1 121@pytest.mark.platform_x86_gpu_training 122@pytest.mark.env_onecard 123def test_zeros_like_dynamic_int32(): 124 x = Tensor(np.arange(16).reshape(2, 2, 2, 2).astype(np.int32)) 125 output = zeros_like_dynamic(x) 126 expected = np.zeros([2, 2, 2, 2]) 127 np.testing.assert_array_equal(output.asnumpy(), expected) 128 129@pytest.mark.level1 130@pytest.mark.platform_x86_gpu_training 131@pytest.mark.env_onecard 132def test_zeros_like_dynamic_float16(): 133 x = Tensor(np.arange(120).reshape(3, 4, 1, 2, 5).astype(np.float16)) 134 output = zeros_like_dynamic(x) 135 expected = np.zeros([3, 4, 1, 2, 5]) 136 np.testing.assert_array_almost_equal(output.asnumpy(), expected) 137 138@pytest.mark.level0 139@pytest.mark.platform_x86_gpu_training 140@pytest.mark.env_onecard 141def test_zeros_like_dynamic_float32(): 142 x = Tensor(np.arange(63).reshape(3, 7, 3).astype(np.float32)) 143 output = zeros_like_dynamic(x) 144 expected = np.zeros([3, 7, 3]) 145 np.testing.assert_array_almost_equal(output.asnumpy(), expected) 146 147@pytest.mark.level0 148@pytest.mark.platform_x86_gpu_training 149@pytest.mark.env_onecard 150def test_zeros_like_dynamic_float64(): 151 x = Tensor(np.arange(2).reshape(2, 1, 1).astype(np.float64)) 152 output = zeros_like_dynamic(x) 153 expected = np.zeros([2, 1, 1]) 154 np.testing.assert_array_almost_equal(output.asnumpy(), expected) 155 156@pytest.mark.level0 157@pytest.mark.platform_x86_gpu_training 158@pytest.mark.env_onecard 159def test_zeros_like_dynamic_multiple_inputs(): 160 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 161 net = ZerosLikeDynamicNet() 162 163 x = Tensor(np.arange(4).reshape(4).astype(np.float32)) 164 output = net(x) 165 expected = np.zeros([4]) 166 np.testing.assert_array_almost_equal(output.asnumpy(), expected) 167 168 x = Tensor(np.arange(8).reshape(2, 1, 2, 2).astype(np.uint8)) 169 output = net(x) 170 expected = np.zeros([2, 1, 2, 2]) 171 np.testing.assert_array_equal(output.asnumpy(), expected) 172 173 x = Tensor(np.arange(1).reshape(1).astype(np.float16)) 174 output = net(x) 175 expected = np.zeros([1]) 176 np.testing.assert_array_almost_equal(output.asnumpy(), expected) 177