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.context as context 19import mindspore.nn as nn 20from mindspore import Tensor 21from mindspore.ops import operations as P 22 23context.set_context(mode=context.GRAPH_MODE, device_target='CPU') 24 25 26class SquareNet(nn.Cell): 27 def __init__(self): 28 super(SquareNet, self).__init__() 29 self.square = P.Square() 30 31 def construct(self, x): 32 return self.square(x) 33 34 35class FloorNet(nn.Cell): 36 def __init__(self): 37 super(FloorNet, self).__init__() 38 self.floor = P.Floor() 39 40 def construct(self, x): 41 return self.floor(x) 42 43 44class RoundNet(nn.Cell): 45 def __init__(self): 46 super(RoundNet, self).__init__() 47 self.round = P.Round() 48 49 def construct(self, x): 50 return self.round(x) 51 52 53class ReciprocalNet(nn.Cell): 54 def __init__(self): 55 super(ReciprocalNet, self).__init__() 56 self.reciprocal = P.Reciprocal() 57 58 def construct(self, x): 59 return self.reciprocal(x) 60 61 62class RintNet(nn.Cell): 63 def __init__(self): 64 super(RintNet, self).__init__() 65 self.rint = P.Rint() 66 67 def construct(self, x): 68 return self.rint(x) 69 70 71class IdentityNet(nn.Cell): 72 def __init__(self): 73 super(IdentityNet, self).__init__() 74 self.identity = P.Identity() 75 76 def construct(self, x): 77 return self.identity(x) 78 79 80@pytest.mark.level0 81@pytest.mark.platform_x86_cpu 82@pytest.mark.env_onecard 83def test_square(): 84 x = np.array([1, 2, 3]).astype(np.int16) 85 net = SquareNet() 86 output = net(Tensor(x)) 87 expect_output = np.array([1, 4, 9]).astype(np.int16) 88 print(output) 89 assert np.all(output.asnumpy() == expect_output) 90 91 x = np.array([1, 2, 3]).astype(np.int32) 92 net = SquareNet() 93 output = net(Tensor(x)) 94 expect_output = np.array([1, 4, 9]).astype(np.int32) 95 print(output) 96 assert np.all(output.asnumpy() == expect_output) 97 98 x = np.array([1, 2, 3]).astype(np.int64) 99 net = SquareNet() 100 output = net(Tensor(x)) 101 expect_output = np.array([1, 4, 9]).astype(np.int64) 102 print(output) 103 assert np.all(output.asnumpy() == expect_output) 104 105 x = np.array([1, 2, 3]).astype(np.float16) 106 net = SquareNet() 107 output = net(Tensor(x)) 108 expect_output = np.array([1, 4, 9]).astype(np.float16) 109 print(output) 110 assert np.all(output.asnumpy() == expect_output) 111 112 x = np.array([1, 2, 3]).astype(np.float32) 113 net = SquareNet() 114 output = net(Tensor(x)) 115 expect_output = np.array([1, 4, 9]).astype(np.float32) 116 print(output) 117 assert np.all(output.asnumpy() == expect_output) 118 119 x = np.array([1, 2, 3]).astype(np.float64) 120 net = SquareNet() 121 output = net(Tensor(x)) 122 expect_output = np.array([1, 4, 9]).astype(np.float64) 123 print(output) 124 assert np.all(output.asnumpy() == expect_output) 125 126 127@pytest.mark.level0 128@pytest.mark.platform_x86_cpu 129@pytest.mark.env_onecard 130def test_floor(): 131 net = FloorNet() 132 133 x = np.random.randn(3, 4).astype(np.float16) 134 x = x * 100 135 output = net(Tensor(x)) 136 expect_output = np.floor(x).astype(np.float16) 137 print(output.asnumpy()) 138 assert np.all(output.asnumpy() == expect_output) 139 140 x = np.random.randn(4, 3).astype(np.float32) 141 x = x * 100 142 output = net(Tensor(x)) 143 expect_output = np.floor(x) 144 print(output.asnumpy()) 145 assert np.all(output.asnumpy() == expect_output) 146 147 148@pytest.mark.level0 149@pytest.mark.platform_x86_cpu 150@pytest.mark.env_onecard 151def test_rint(): 152 net = RintNet() 153 prop = 100 if np.random.random() > 0.5 else -100 154 x = np.random.randn(3, 4, 5, 6).astype(np.float16) * prop 155 output = net(Tensor(x)) 156 expect_output = np.rint(x).astype(np.float16) 157 np.testing.assert_almost_equal(output.asnumpy(), expect_output) 158 159 x = np.random.randn(3, 4, 5, 6).astype(np.float32) * prop 160 output = net(Tensor(x)) 161 expect_output = np.rint(x).astype(np.float32) 162 np.testing.assert_almost_equal(output.asnumpy(), expect_output) 163 164 165@pytest.mark.level0 166@pytest.mark.platform_x86_cpu 167@pytest.mark.env_onecard 168def test_round(): 169 net = RoundNet() 170 171 x = np.array([0.9920, -0.4077, 0.9734, -1.0362, 1.5, -2.5, 4.5]).astype(np.float16) 172 output = net(Tensor(x)) 173 expect_output = np.round(x).astype(np.float16) 174 np.testing.assert_almost_equal(output.asnumpy(), expect_output) 175 176 x = np.array([0.9920, -0.4077, 0.9734, -1.0362, 1.5, -2.5, 4.5]).astype(np.float32) 177 output = net(Tensor(x)) 178 expect_output = np.round(x).astype(np.float32) 179 np.testing.assert_almost_equal(output.asnumpy(), expect_output) 180 181 182@pytest.mark.level0 183@pytest.mark.platform_x86_cpu 184@pytest.mark.env_onecard 185def test_reciprocal(): 186 net = ReciprocalNet() 187 prop = 100 if np.random.random() > 0.5 else -100 188 x = np.random.randn(3, 4, 5, 6).astype(np.float16) * prop 189 output = net(Tensor(x)) 190 expect_output = (1. / x).astype(np.float16) 191 diff = output.asnumpy() - expect_output 192 error = np.ones(shape=expect_output.shape) * 1.0e-5 193 assert np.all(np.abs(diff) < error) 194 195 x = np.random.randn(3, 4, 5, 6).astype(np.float32) * prop 196 output = net(Tensor(x)) 197 expect_output = (1. / x).astype(np.float32) 198 diff = output.asnumpy() - expect_output 199 error = np.ones(shape=expect_output.shape) * 1.0e-5 200 assert np.all(np.abs(diff) < error) 201 202 203@pytest.mark.level0 204@pytest.mark.platform_x86_cpu 205@pytest.mark.env_onecard 206def test_identity_pynative(): 207 context.set_context(mode=context.PYNATIVE_MODE, device_target="CPU") 208 net = IdentityNet() 209 210 x = np.random.randn(3, 4, 5, 6).astype(np.float64) 211 input_tensor = Tensor(x) 212 output = net(input_tensor) 213 np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy()) 214 assert id(input_tensor) != id(output) 215 216 x = np.random.randn(3, 4, 5, 6).astype(np.float32) 217 input_tensor = Tensor(x) 218 output = net(input_tensor) 219 np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy()) 220 assert id(input_tensor) != id(output) 221 222 x = np.random.randn(3, 4, 5, 6).astype(np.float16) 223 input_tensor = Tensor(x) 224 output = net(input_tensor) 225 np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy()) 226 assert id(input_tensor) != id(output) 227 228 x = np.random.randn(3, 4, 5, 6).astype(np.uint64) 229 input_tensor = Tensor(x) 230 output = net(input_tensor) 231 np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy()) 232 assert id(input_tensor) != id(output) 233 234 x = np.random.randn(3, 4, 5, 6).astype(np.int64) 235 input_tensor = Tensor(x) 236 output = net(input_tensor) 237 np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy()) 238 assert id(input_tensor) != id(output) 239 240 x = np.random.randn(3, 4, 5, 6).astype(np.uint32) 241 input_tensor = Tensor(x) 242 output = net(input_tensor) 243 np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy()) 244 assert id(input_tensor) != id(output) 245 246 x = np.random.randn(3, 4, 5, 6).astype(np.int32) 247 input_tensor = Tensor(x) 248 output = net(input_tensor) 249 np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy()) 250 assert id(input_tensor) != id(output) 251 252 x = np.random.randn(3, 4, 5, 6).astype(np.uint16) 253 input_tensor = Tensor(x) 254 output = net(input_tensor) 255 np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy()) 256 assert id(input_tensor) != id(output) 257 258 x = np.random.randn(3, 4, 5, 6).astype(np.int16) 259 input_tensor = Tensor(x) 260 output = net(input_tensor) 261 np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy()) 262 assert id(input_tensor) != id(output) 263 264 x = np.random.randn(3, 4, 5, 6).astype(np.uint8) 265 input_tensor = Tensor(x) 266 output = net(input_tensor) 267 np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy()) 268 assert id(input_tensor) != id(output) 269 270 x = np.random.randn(3, 4, 5, 6).astype(np.int8) 271 input_tensor = Tensor(x) 272 output = net(input_tensor) 273 np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy()) 274 assert id(input_tensor) != id(output) 275 276 x = np.random.randn(3, 4, 5, 6).astype(np.bool) 277 input_tensor = Tensor(x) 278 output = net(input_tensor) 279 np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy()) 280 assert id(input_tensor) != id(output) 281 282 283@pytest.mark.level0 284@pytest.mark.platform_x86_cpu 285@pytest.mark.env_onecard 286def test_identity_graph(): 287 context.set_context(mode=context.GRAPH_MODE, device_target="CPU") 288 net = IdentityNet() 289 290 x = np.random.randn(3, 4, 5, 6).astype(np.float64) 291 input_tensor = Tensor(x) 292 output = net(input_tensor) 293 np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy()) 294 assert id(input_tensor) != id(output) 295 296 x = np.random.randn(3, 4, 5, 6).astype(np.float32) 297 input_tensor = Tensor(x) 298 output = net(input_tensor) 299 np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy()) 300 assert id(input_tensor) != id(output) 301 302 x = np.random.randn(3, 4, 5, 6).astype(np.float16) 303 input_tensor = Tensor(x) 304 output = net(input_tensor) 305 np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy()) 306 assert id(input_tensor) != id(output) 307 308 x = np.random.randn(3, 4, 5, 6).astype(np.uint64) 309 input_tensor = Tensor(x) 310 output = net(input_tensor) 311 np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy()) 312 assert id(input_tensor) != id(output) 313 314 x = np.random.randn(3, 4, 5, 6).astype(np.int64) 315 input_tensor = Tensor(x) 316 output = net(input_tensor) 317 np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy()) 318 assert id(input_tensor) != id(output) 319 320 x = np.random.randn(3, 4, 5, 6).astype(np.uint32) 321 input_tensor = Tensor(x) 322 output = net(input_tensor) 323 np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy()) 324 assert id(input_tensor) != id(output) 325 326 x = np.random.randn(3, 4, 5, 6).astype(np.int32) 327 input_tensor = Tensor(x) 328 output = net(input_tensor) 329 np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy()) 330 assert id(input_tensor) != id(output) 331 332 x = np.random.randn(3, 4, 5, 6).astype(np.uint16) 333 input_tensor = Tensor(x) 334 output = net(input_tensor) 335 np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy()) 336 assert id(input_tensor) != id(output) 337 338 x = np.random.randn(3, 4, 5, 6).astype(np.int16) 339 input_tensor = Tensor(x) 340 output = net(input_tensor) 341 np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy()) 342 assert id(input_tensor) != id(output) 343 344 x = np.random.randn(3, 4, 5, 6).astype(np.uint8) 345 input_tensor = Tensor(x) 346 output = net(input_tensor) 347 np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy()) 348 assert id(input_tensor) != id(output) 349 350 x = np.random.randn(3, 4, 5, 6).astype(np.int8) 351 input_tensor = Tensor(x) 352 output = net(input_tensor) 353 np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy()) 354 assert id(input_tensor) != id(output) 355 356 x = np.random.randn(3, 4, 5, 6).astype(np.bool) 357 input_tensor = Tensor(x) 358 output = net(input_tensor) 359 np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy()) 360 assert id(input_tensor) != id(output) 361