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.common.tensor import Tensor 21from mindspore.nn import Cell 22from mindspore.ops import operations as P 23from mindspore.ops.operations import _inner_ops as inner 24 25 26class NetEqual(Cell): 27 def __init__(self): 28 super(NetEqual, self).__init__() 29 self.Equal = P.Equal() 30 31 def construct(self, x, y): 32 return self.Equal(x, y) 33 34 35class NetEqualDynamic(Cell): 36 def __init__(self): 37 super(NetEqualDynamic, self).__init__() 38 self.conv = inner.GpuConvertToDynamicShape() 39 self.Equal = P.Equal() 40 41 def construct(self, x, y): 42 x_conv = self.conv(x) 43 y_conv = self.conv(y) 44 return self.Equal(x_conv, y_conv) 45 46 47class NetNotEqual(Cell): 48 def __init__(self): 49 super(NetNotEqual, self).__init__() 50 self.NotEqual = P.NotEqual() 51 52 def construct(self, x, y): 53 return self.NotEqual(x, y) 54 55 56class NetGreaterEqual(Cell): 57 def __init__(self): 58 super(NetGreaterEqual, self).__init__() 59 self.GreaterEqual = P.GreaterEqual() 60 61 def construct(self, x, y): 62 return self.GreaterEqual(x, y) 63 64@pytest.mark.level0 65@pytest.mark.platform_x86_gpu_training 66@pytest.mark.env_onecard 67def test_equal(): 68 x0_np = np.arange(24).reshape((4, 3, 2)).astype(np.float32) 69 x0 = Tensor(x0_np) 70 y0_np = np.arange(24).reshape((4, 3, 2)).astype(np.float32) 71 y0 = Tensor(y0_np) 72 expect0 = np.equal(x0_np, y0_np) 73 x1_np = np.array([0, 1, 3]).astype(np.float32) 74 x1 = Tensor(x1_np) 75 y1_np = np.array([0]).astype(np.float32) 76 y1 = Tensor(y1_np) 77 expect1 = np.equal(x1_np, y1_np) 78 x2_np = np.array([0, 1, 3]).astype(np.int32) 79 x2 = Tensor(x2_np) 80 y2_np = np.array([0]).astype(np.int32) 81 y2 = Tensor(y2_np) 82 expect2 = np.equal(x2_np, y2_np) 83 x3_np = np.array([0, 1, 3]).astype(np.int16) 84 x3 = Tensor(x3_np) 85 y3_np = np.array([0, 1, -3]).astype(np.int16) 86 y3 = Tensor(y3_np) 87 expect3 = np.equal(x3_np, y3_np) 88 x4_np = np.array([0, 1, 4]).astype(np.uint8) 89 x4 = Tensor(x4_np) 90 y4_np = np.array([0, 1, 3]).astype(np.uint8) 91 y4 = Tensor(y4_np) 92 expect4 = np.equal(x4_np, y4_np) 93 x5_np = np.array([True, False, True]).astype(bool) 94 x5 = Tensor(x5_np) 95 y5_np = np.array([True, False, False]).astype(bool) 96 y5 = Tensor(y5_np) 97 expect5 = np.equal(x5_np, y5_np) 98 x6_np = np.array([0, 1, 4]).astype(np.int8) 99 x6 = Tensor(x6_np) 100 y6_np = np.array([0, 1, 3]).astype(np.int8) 101 y6 = Tensor(y6_np) 102 expect6 = np.equal(x6_np, y6_np) 103 x7_np = np.array([0, 1, 4]).astype(np.int64) 104 x7 = Tensor(x7_np) 105 y7_np = np.array([0, 1, 3]).astype(np.int64) 106 y7 = Tensor(y7_np) 107 expect7 = np.equal(x7_np, y7_np) 108 x8_np = np.array([0, 1, 4]).astype(np.float16) 109 x8 = Tensor(x8_np) 110 y8_np = np.array([0, 1, 3]).astype(np.float16) 111 y8 = Tensor(y8_np) 112 expect8 = np.equal(x8_np, y8_np) 113 x9_np = np.array([0, 1, 4]).astype(np.float64) 114 x9 = Tensor(x9_np) 115 y9_np = np.array([0, 1, 3]).astype(np.float64) 116 y9 = Tensor(y9_np) 117 expect9 = np.equal(x9_np, y9_np) 118 119 x = [x0, x1, x2, x3, x4, x5, x6, x7, x8, x9] 120 y = [y0, y1, y2, y3, y4, y5, y6, y7, y8, y9] 121 expect = [expect0, expect1, expect2, expect3, expect4, expect5, expect6, expect7, expect8, expect9] 122 123 context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU") 124 equal = NetEqual() 125 for i, xi in enumerate(x): 126 output = equal(xi, y[i]) 127 assert np.all(output.asnumpy() == expect[i]) 128 assert output.shape == expect[i].shape 129 print('test [%d/%d] passed!' % (i, len(x))) 130 131 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 132 equal = NetEqual() 133 for i, xi in enumerate(x): 134 output = equal(xi, y[i]) 135 assert np.all(output.asnumpy() == expect[i]) 136 assert output.shape == expect[i].shape 137 print('test [%d/%d] passed!' % (i, len(x))) 138 139@pytest.mark.level0 140@pytest.mark.platform_x86_gpu_training 141@pytest.mark.env_onecard 142def test_notequal(): 143 x0 = Tensor(np.array([[1.2, 1], [1, 0]]).astype(np.float32)) 144 y0 = Tensor(np.array([[1, 2]]).astype(np.float32)) 145 expect0 = np.array([[True, True], [False, True]]) 146 x1 = Tensor(np.array([[2, 1], [1, 1]]).astype(np.int16)) 147 y1 = Tensor(np.array([[1, 2]]).astype(np.int16)) 148 expect1 = np.array([[True, True], [False, True]]) 149 x2 = Tensor(np.array([[2, 1], [1, 2]]).astype(np.uint8)) 150 y2 = Tensor(np.array([[1, 2]]).astype(np.uint8)) 151 expect2 = np.array([[True, True], [False, False]]) 152 x3 = Tensor(np.array([[False, True], [True, False]]).astype(bool)) 153 y3 = Tensor(np.array([[True, False]]).astype(bool)) 154 expect3 = np.array([[True, True], [False, False]]) 155 x4 = Tensor(np.array([[1.2, 1], [1, 0]]).astype(np.float16)) 156 y4 = Tensor(np.array([[1, 2]]).astype(np.float16)) 157 expect4 = np.array([[True, True], [False, True]]) 158 x5 = Tensor(np.array([[2, 1], [1, 0]]).astype(np.int64)) 159 y5 = Tensor(np.array([[1, 2]]).astype(np.int64)) 160 expect5 = np.array([[True, True], [False, True]]) 161 x6 = Tensor(np.array([[2, 1], [1, 0]]).astype(np.int32)) 162 y6 = Tensor(np.array([[1, 2], [1, 2]]).astype(np.int32)) 163 expect6 = np.array([[True, True], [False, True]]) 164 165 x = [x0, x1, x2, x3, x4, x5, x6] 166 y = [y0, y1, y2, y3, y4, y5, y6] 167 expect = [expect0, expect1, expect2, expect3, expect4, expect5, expect6] 168 169 context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU") 170 notequal = NetNotEqual() 171 for i, xi in enumerate(x): 172 output = notequal(xi, y[i]) 173 assert np.all(output.asnumpy() == expect[i]) 174 assert output.shape == expect[i].shape 175 print('test [%d/%d] passed!' % (i, len(x))) 176 177 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 178 notequal = NetNotEqual() 179 for i, xi in enumerate(x): 180 output = notequal(xi, y[i]) 181 assert np.all(output.asnumpy() == expect[i]) 182 assert output.shape == expect[i].shape 183 print('test [%d/%d] passed!' % (i, len(x))) 184 185@pytest.mark.level0 186@pytest.mark.platform_x86_gpu_training 187@pytest.mark.env_onecard 188def test_greaterqual(): 189 x0 = Tensor(np.array([[1.2, 1], [1, 0]]).astype(np.float32)) 190 y0 = Tensor(np.array([[1, 2], [1, 2]]).astype(np.float32)) 191 expect0 = np.array([[True, False], [True, False]]) 192 x1 = Tensor(np.array([[2, 1], [1, 1]]).astype(np.int16)) 193 y1 = Tensor(np.array([[1, 2]]).astype(np.int16)) 194 expect1 = np.array([[True, False], [True, False]]) 195 x2 = Tensor(np.array([[2, 1], [1, 2]]).astype(np.uint8)) 196 y2 = Tensor(np.array([[1, 2]]).astype(np.uint8)) 197 expect2 = np.array([[True, False], [True, True]]) 198 199 x3 = Tensor(np.array([[2, 1], [1, 2]]).astype(np.float64)) 200 y3 = Tensor(np.array([[1, 2]]).astype(np.float64)) 201 expect3 = np.array([[True, False], [True, True]]) 202 x4 = Tensor(np.array([[2, 1], [1, 2]]).astype(np.float16)) 203 y4 = Tensor(np.array([[1, 2]]).astype(np.float16)) 204 expect4 = np.array([[True, False], [True, True]]) 205 x5 = Tensor(np.array([[2, 1], [1, 1]]).astype(np.int64)) 206 y5 = Tensor(np.array([[1, 2]]).astype(np.int64)) 207 expect5 = np.array([[True, False], [True, False]]) 208 x6 = Tensor(np.array([[2, 1], [1, 1]]).astype(np.int32)) 209 y6 = Tensor(np.array([[1, 2]]).astype(np.int32)) 210 expect6 = np.array([[True, False], [True, False]]) 211 x7 = Tensor(np.array([[2, 1], [1, 1]]).astype(np.int8)) 212 y7 = Tensor(np.array([[1, 2]]).astype(np.int8)) 213 expect7 = np.array([[True, False], [True, False]]) 214 215 x = [x0, x1, x2, x3, x4, x5, x6, x7] 216 y = [y0, y1, y2, y3, y4, y5, y6, y7] 217 expect = [expect0, expect1, expect2, expect3, expect4, expect5, expect6, expect7] 218 219 context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU") 220 gequal = NetGreaterEqual() 221 for i, xi in enumerate(x): 222 output = gequal(xi, y[i]) 223 assert np.all(output.asnumpy() == expect[i]) 224 assert output.shape == expect[i].shape 225 print('test [%d/%d] passed!' % (i, len(x))) 226 227 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 228 gequal = NetGreaterEqual() 229 for i, xi in enumerate(x): 230 output = gequal(xi, y[i]) 231 assert np.all(output.asnumpy() == expect[i]) 232 assert output.shape == expect[i].shape 233 print('test [%d/%d] passed!' % (i, len(x))) 234 235 236@pytest.mark.level0 237@pytest.mark.platform_x86_gpu_training 238@pytest.mark.env_onecard 239def test_equal_dynamic_shape(): 240 x0_np = np.arange(24).reshape((4, 3, 2)).astype(np.float32) 241 x0 = Tensor(x0_np) 242 y0_np = np.arange(24).reshape((4, 3, 2)).astype(np.float32) 243 y0 = Tensor(y0_np) 244 expect0 = np.equal(x0_np, y0_np) 245 246 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 247 equal = NetEqualDynamic() 248 output0 = equal(x0, y0) 249 assert np.all(output0.asnumpy() == expect0) 250 assert output0.shape == expect0.shape 251