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 18import mindspore.context as context 19import mindspore.nn as nn 20from mindspore import Tensor 21from mindspore.ops import operations as P 22 23 24class SquaredDifference(nn.Cell): 25 def __init__(self): 26 super(SquaredDifference, self).__init__() 27 self.squaredDiff = P.SquaredDifference() 28 29 def construct(self, x, y): 30 return self.squaredDiff(x, y) 31 32 33@pytest.mark.level0 34@pytest.mark.platform_x86_gpu_training 35@pytest.mark.env_onecard 36def test_nobroadcast_f16(): 37 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 38 np.random.seed(42) 39 net = SquaredDifference() 40 input_x = np.random.uniform(10, 20, (3, 4, 5, 2)).astype(np.float16) 41 input_y = np.random.uniform(40, 50, (3, 4, 5, 2)).astype(np.float16) 42 output = net(Tensor(input_x), Tensor(input_y)).asnumpy() 43 diff = input_x-input_y 44 expect = diff*diff 45 assert np.all(output == expect) 46 47 48@pytest.mark.level0 49@pytest.mark.platform_x86_gpu_training 50@pytest.mark.env_onecard 51def test_nobroadcast_f32(): 52 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 53 np.random.seed(42) 54 net = SquaredDifference() 55 input_x = np.random.rand(3, 4, 5, 2).astype(np.float32) 56 input_y = np.random.rand(3, 4, 5, 2).astype(np.float32) 57 output = net(Tensor(input_x), Tensor(input_y)).asnumpy() 58 diff = input_x-input_y 59 expect = diff*diff 60 assert np.all(output == expect) 61 62 63@pytest.mark.level0 64@pytest.mark.platform_x86_gpu_training 65@pytest.mark.env_onecard 66def test_nobroadcast_int32(): 67 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 68 np.random.seed(42) 69 net = SquaredDifference() 70 input_x = np.random.rand(3, 4, 5, 2).astype(np.int32) 71 input_y = np.random.rand(3, 4, 5, 2).astype(np.int32) 72 output = net(Tensor(input_x), Tensor(input_y)).asnumpy() 73 diff = input_x-input_y 74 expect = diff*diff 75 assert np.all(output == expect) 76 77 78@pytest.mark.level0 79@pytest.mark.platform_x86_gpu_training 80@pytest.mark.env_onecard 81def test_broadcast_int32(): 82 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 83 np.random.seed(42) 84 net = SquaredDifference() 85 input_x = np.random.rand(1, 4, 1, 2).astype(np.int32) 86 input_y = np.random.rand(3, 1, 5, 1).astype(np.int32) 87 output = net(Tensor(input_x), Tensor(input_y)).asnumpy() 88 diff = input_x-input_y 89 expect = diff*diff 90 assert np.all(output == expect) 91 92 93@pytest.mark.level0 94@pytest.mark.platform_x86_gpu_training 95@pytest.mark.env_onecard 96def test_broadcast_f32(): 97 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 98 np.random.seed(42) 99 net = SquaredDifference() 100 input_x = np.random.rand(1, 4, 1, 2).astype(np.float32) 101 input_y = np.random.rand(3, 1, 5, 1).astype(np.float32) 102 output = net(Tensor(input_x), Tensor(input_y)).asnumpy() 103 diff = input_x-input_y 104 expect = diff*diff 105 assert np.all(output == expect) 106 107 108@pytest.mark.level0 109@pytest.mark.platform_x86_gpu_training 110@pytest.mark.env_onecard 111def test_broadcast_f16(): 112 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 113 np.random.seed(42) 114 net = SquaredDifference() 115 input_x = np.random.rand(1, 4, 1, 2).astype(np.float16) 116 input_y = np.random.rand(3, 1, 5, 1).astype(np.float16) 117 output = net(Tensor(input_x), Tensor(input_y)).asnumpy() 118 diff = input_x-input_y 119 expect = diff*diff 120 assert np.all(output == expect) 121 122 123@pytest.mark.level0 124@pytest.mark.platform_x86_gpu_training 125@pytest.mark.env_onecard 126def test_broadcast_bool(): 127 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 128 np.random.seed(42) 129 net = SquaredDifference() 130 input_x = np.random.rand(1, 4, 1, 2).astype(np.bool) 131 input_y = np.random.uniform(10, 20, (3, 1, 5, 1)).astype(np.float32) 132 output = net(Tensor(input_x), Tensor(input_y)).asnumpy() 133 diff = input_x-input_y 134 expect = diff*diff 135 error = np.ones(shape=np.array(output.shape, dtype=int))*1.0e-6 136 double_check = np.abs(output-expect)/expect 137 assert np.all(double_check < error) 138 139 140@pytest.mark.level0 141@pytest.mark.platform_x86_gpu_training 142@pytest.mark.env_onecard 143def test_nobroadcast_bool(): 144 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 145 np.random.seed(42) 146 net = SquaredDifference() 147 input_x = np.random.rand(3, 4, 5, 2).astype(np.bool) 148 input_y = np.random.rand(3, 4, 5, 2).astype(np.float32) 149 output = net(Tensor(input_x), Tensor(input_y)).asnumpy() 150 diff = input_x-input_y 151 expect = diff*diff 152 error = np.ones(shape=np.array(output.shape, dtype=int))*1.0e-6 153 double_check = np.abs(output-expect)/expect 154 assert np.all(double_check < error) 155 156 157@pytest.mark.level0 158@pytest.mark.platform_x86_gpu_training 159@pytest.mark.env_onecard 160def test_broadcast_int32_f16(): 161 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 162 np.random.seed(42) 163 net = SquaredDifference() 164 input_x = np.random.rand(1, 4, 1, 2).astype(np.int32) 165 input_y = np.random.uniform(10, 20, (3, 1, 5, 1)).astype(np.float16) 166 output = net(Tensor(input_x), Tensor(input_y)).asnumpy() 167 diff = input_x-input_y 168 expect = diff*diff 169 error = np.ones(shape=np.array(output.shape, dtype=int))*1.0e-3 170 double_check = np.abs(output-expect)/expect 171 assert np.all(double_check < error) 172 173 174@pytest.mark.level0 175@pytest.mark.platform_x86_gpu_training 176@pytest.mark.env_onecard 177def test_broadcast_int32_f32(): 178 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 179 np.random.seed(42) 180 net = SquaredDifference() 181 input_x = np.random.rand(1, 4, 1, 2).astype(np.int32) 182 input_y = np.random.uniform(10, 20, (3, 1, 5, 1)).astype(np.float32) 183 output = net(Tensor(input_x), Tensor(input_y)).asnumpy() 184 diff = input_x-input_y 185 expect = diff*diff 186 error = np.ones(shape=np.array(output.shape, dtype=int))*1.0e-6 187 double_check = np.abs(output-expect)/expect 188 assert np.all(double_check < error) 189 190 191@pytest.mark.level0 192@pytest.mark.platform_x86_gpu_training 193@pytest.mark.env_onecard 194def test_nobroadcast_int32_f16(): 195 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 196 np.random.seed(42) 197 net = SquaredDifference() 198 input_x = np.random.rand(2, 4, 3, 2).astype(np.int32) 199 input_y = np.random.uniform(10, 20, (2, 4, 3, 2)).astype(np.float16) 200 output = net(Tensor(input_x), Tensor(input_y)).asnumpy() 201 diff = input_x-input_y 202 expect = diff*diff 203 error = np.ones(shape=np.array(output.shape, dtype=int))*1.0e-3 204 double_check = np.abs(output-expect)/expect 205 assert np.all(double_check < error) 206 207 208@pytest.mark.level0 209@pytest.mark.platform_x86_gpu_training 210@pytest.mark.env_onecard 211def test_nobroadcast_int32_f32(): 212 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 213 np.random.seed(42) 214 net = SquaredDifference() 215 input_x = np.random.rand(2, 4, 3, 2).astype(np.int32) 216 input_y = np.random.uniform(10, 20, (2, 4, 3, 2)).astype(np.float32) 217 output = net(Tensor(input_x), Tensor(input_y)).asnumpy() 218 diff = input_x-input_y 219 expect = diff*diff 220 error = np.ones(shape=np.array(output.shape, dtype=int))*1.0e-6 221 double_check = np.abs(output-expect)/expect 222 assert np.all(double_check < error) 223 224 225@pytest.mark.level0 226@pytest.mark.platform_x86_gpu_training 227@pytest.mark.env_onecard 228def test_broadcast_f32_scalar_tensor(): 229 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 230 np.random.seed(42) 231 net = SquaredDifference() 232 input_x = np.random.rand(2).astype(np.float32) 233 input_y = np.random.rand(3, 1, 5, 1).astype(np.float32) 234 output = net(Tensor(input_x), Tensor(input_y)).asnumpy() 235 diff = input_x-input_y 236 expect = diff*diff 237 assert np.all(output == expect) 238 239 240@pytest.mark.level0 241@pytest.mark.platform_x86_gpu_training 242@pytest.mark.env_onecard 243def test_broadcast_f32_tensor_tensor(): 244 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 245 np.random.seed(42) 246 net = SquaredDifference() 247 input_x = np.random.rand(1, 2).astype(np.float32) 248 input_y = np.random.rand(3, 1, 5, 1).astype(np.float32) 249 output = net(Tensor(input_x), Tensor(input_y)).asnumpy() 250 diff = input_x-input_y 251 expect = diff*diff 252 assert np.all(output == expect) 253 254 255@pytest.mark.level0 256@pytest.mark.platform_x86_gpu_training 257@pytest.mark.env_onecard 258def test_broadcast_f32_tensor_tensor_dim_over_7(): 259 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 260 np.random.seed(42) 261 net = SquaredDifference() 262 input_x = np.random.rand(1, 2).astype(np.float32) 263 input_y = np.random.rand(3, 1, 5, 1, 3, 4, 2, 1).astype(np.float32) 264 try: 265 net(Tensor(input_x), Tensor(input_y)) 266 except RuntimeError: 267 assert True 268 269 270@pytest.mark.level0 271@pytest.mark.platform_x86_gpu_training 272@pytest.mark.env_onecard 273def test_broadcast_f32_tensor_tensor_cannot_brocast(): 274 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 275 np.random.seed(42) 276 net = SquaredDifference() 277 input_x = np.random.rand(5, 3).astype(np.float32) 278 input_y = np.random.rand(3, 1, 5, 1, 3, 4, 2).astype(np.float32) 279 try: 280 net(Tensor(input_x), Tensor(input_y)) 281 except ValueError: 282 assert True 283 284 285@pytest.mark.level0 286@pytest.mark.platform_x86_gpu_training 287@pytest.mark.env_onecard 288def test_broadcast_int_f32_precision(): 289 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 290 np.random.seed(42) 291 net = SquaredDifference() 292 input_x = np.random.randint(20, 30, (1, 2)).astype(np.int32) 293 input_y = np.random.rand(3, 1, 5, 1).astype(np.float32) 294 output = net(Tensor(input_x), Tensor(input_y)).asnumpy() 295 diff = input_x-input_y 296 expect = diff*diff 297 error = np.ones(shape=np.array(output.shape, dtype=int))*1.0e-3 298 double_thousand = np.abs(output-expect)/expect 299 assert np.all(double_thousand < error) 300 301 302@pytest.mark.level0 303@pytest.mark.platform_x86_gpu_training 304@pytest.mark.env_onecard 305def test_broadcast_type_error(): 306 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 307 np.random.seed(42) 308 net = SquaredDifference() 309 input_x = np.random.randint(20, 30, (1, 2)).astype(np.bool) 310 input_y = np.random.rand(3, 1, 5, 1).astype(np.bool) 311 try: 312 net(Tensor(input_x), Tensor(input_y)) 313 except TypeError: 314 assert True 315