1# Copyright 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, ops 22 23 24class Net(nn.Cell): 25 def __init__(self): 26 super(Net, self).__init__() 27 self.identity = ops.Identity() 28 29 def construct(self, x): 30 return self.identity(x) 31 32 33def generate_testcases(nptype): 34 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 35 x = np.random.randn(3, 4, 5, 6).astype(nptype) 36 net = Net() 37 input_tensor = Tensor(x) 38 output = net(input_tensor) 39 np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy()) 40 assert id(input_tensor) != id(output) 41 42 context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU") 43 x = np.random.randn(3, 4, 5, 6).astype(nptype) 44 net = Net() 45 input_tensor = Tensor(x) 46 output = net(input_tensor) 47 np.testing.assert_almost_equal(output.asnumpy(), input_tensor.asnumpy()) 48 assert id(input_tensor) != id(output) 49 50 51@pytest.mark.level0 52@pytest.mark.platform_x86_gpu_training 53@pytest.mark.env_onecard 54def test_identity_float64(): 55 generate_testcases(np.float64) 56 57 58@pytest.mark.level0 59@pytest.mark.platform_x86_gpu_training 60@pytest.mark.env_onecard 61def test_identity_float32(): 62 generate_testcases(np.float32) 63 64 65@pytest.mark.level1 66@pytest.mark.platform_x86_gpu_training 67@pytest.mark.env_onecard 68def test_identity_float16(): 69 generate_testcases(np.float16) 70 71 72@pytest.mark.level1 73@pytest.mark.platform_x86_gpu_training 74@pytest.mark.env_onecard 75def test_identity_uint64(): 76 generate_testcases(np.uint64) 77 78 79@pytest.mark.level1 80@pytest.mark.platform_x86_gpu_training 81@pytest.mark.env_onecard 82def test_identity_int64(): 83 generate_testcases(np.int64) 84 85 86@pytest.mark.level1 87@pytest.mark.platform_x86_gpu_training 88@pytest.mark.env_onecard 89def test_identity_uint32(): 90 generate_testcases(np.uint32) 91 92 93@pytest.mark.level1 94@pytest.mark.platform_x86_gpu_training 95@pytest.mark.env_onecard 96def test_identity_int32(): 97 generate_testcases(np.int32) 98 99 100@pytest.mark.level1 101@pytest.mark.platform_x86_gpu_training 102@pytest.mark.env_onecard 103def test_identity_uint16(): 104 generate_testcases(np.uint16) 105 106 107@pytest.mark.level1 108@pytest.mark.platform_x86_gpu_training 109@pytest.mark.env_onecard 110def test_identity_int16(): 111 generate_testcases(np.int16) 112 113 114@pytest.mark.level1 115@pytest.mark.platform_x86_gpu_training 116@pytest.mark.env_onecard 117def test_identity_uint8(): 118 generate_testcases(np.uint8) 119 120 121@pytest.mark.level1 122@pytest.mark.platform_x86_gpu_training 123@pytest.mark.env_onecard 124def test_identity_int8(): 125 generate_testcases(np.int8) 126 127 128@pytest.mark.level1 129@pytest.mark.platform_x86_gpu_training 130@pytest.mark.env_onecard 131def test_identity_bool(): 132 generate_testcases(np.bool) 133