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 16 17import numpy as np 18import pytest 19 20from mindspore import Tensor 21import mindspore.nn as nn 22from mindspore.ops import operations as P 23import mindspore.context as context 24from tests.security_utils import security_off_wrap 25 26 27class PrintNetOneInput(nn.Cell): 28 def __init__(self): 29 super(PrintNetOneInput, self).__init__() 30 self.op = P.Print() 31 32 def construct(self, x): 33 self.op(x) 34 return x 35 36 37class PrintNetTwoInputs(nn.Cell): 38 def __init__(self): 39 super(PrintNetTwoInputs, self).__init__() 40 self.op = P.Print() 41 42 def construct(self, x, y): 43 self.op(x, y) 44 return x 45 46 47class PrintNetIndex(nn.Cell): 48 def __init__(self): 49 super(PrintNetIndex, self).__init__() 50 self.op = P.Print() 51 52 def construct(self, x): 53 self.op(x[0][0][6][3]) 54 return x 55 56 57 58@security_off_wrap 59def print_testcase(nptype): 60 # large shape 61 x = np.arange(20808).reshape(6, 3, 34, 34).astype(nptype) 62 # a value that can be stored as int8_t 63 x[0][0][6][3] = 125 64 # small shape 65 y = np.arange(9).reshape(3, 3).astype(nptype) 66 x = Tensor(x) 67 y = Tensor(y) 68 # graph mode 69 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 70 net_1 = PrintNetOneInput() 71 net_2 = PrintNetTwoInputs() 72 net_3 = PrintNetIndex() 73 net_1(x) 74 net_2(x, y) 75 net_3(x) 76 77 78class PrintNetString(nn.Cell): 79 def __init__(self): 80 super(PrintNetString, self).__init__() 81 self.op = P.Print() 82 83 def construct(self, x, y): 84 self.op("The first Tensor is", x) 85 self.op("The second Tensor is", y) 86 self.op("This line only prints string", "Another line") 87 self.op("The first Tensor is", x, y, "is the second Tensor") 88 return x 89 90 91@security_off_wrap 92def print_testcase_string(nptype): 93 x = np.ones(18).astype(nptype) 94 y = np.arange(9).reshape(3, 3).astype(nptype) 95 x = Tensor(x) 96 y = Tensor(y) 97 # graph mode 98 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 99 net = PrintNetString() 100 net(x, y) 101 102 103class PrintTypes(nn.Cell): 104 def __init__(self): 105 super(PrintTypes, self).__init__() 106 self.op = P.Print() 107 108 def construct(self, x, y, z): 109 self.op("This is a scalar:", 34, "This is int:", x, "This is float64:", y, "This is int64:", z) 110 return x 111 112 113@security_off_wrap 114@pytest.mark.level0 115@pytest.mark.platform_x86_gpu_training 116@pytest.mark.env_onecard 117def test_print_multiple_types(): 118 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 119 x = Tensor(np.array([[1], [3], [4], [6], [3]], dtype=np.int32)) 120 y = Tensor(np.array([[1], [3], [4], [6], [3]]).astype(np.float64)) 121 z = Tensor(np.arange(9).reshape(3, 3).astype(np.int64)) 122 net = PrintTypes() 123 net(x, y, z) 124 125 126@security_off_wrap 127@pytest.mark.level1 128@pytest.mark.platform_x86_gpu_training 129@pytest.mark.env_onecard 130def test_print_bool(): 131 print_testcase(np.bool) 132 133 134@security_off_wrap 135@pytest.mark.level1 136@pytest.mark.platform_x86_gpu_training 137@pytest.mark.env_onecard 138def test_print_int8(): 139 print_testcase(np.int8) 140 141 142@security_off_wrap 143@pytest.mark.level1 144@pytest.mark.platform_x86_gpu_training 145@pytest.mark.env_onecard 146def test_print_int16(): 147 print_testcase(np.int16) 148 149 150@security_off_wrap 151@pytest.mark.level1 152@pytest.mark.platform_x86_gpu_training 153@pytest.mark.env_onecard 154def test_print_int32(): 155 print_testcase(np.int32) 156 157 158@security_off_wrap 159@pytest.mark.level1 160@pytest.mark.platform_x86_gpu_training 161@pytest.mark.env_onecard 162def test_print_int64(): 163 print_testcase(np.int64) 164 165 166@security_off_wrap 167@pytest.mark.level1 168@pytest.mark.platform_x86_gpu_training 169@pytest.mark.env_onecard 170def test_print_uint8(): 171 print_testcase(np.uint8) 172 173 174@security_off_wrap 175@pytest.mark.level1 176@pytest.mark.platform_x86_gpu_training 177@pytest.mark.env_onecard 178def test_print_uint16(): 179 print_testcase(np.uint16) 180 181 182@security_off_wrap 183@pytest.mark.level1 184@pytest.mark.platform_x86_gpu_training 185@pytest.mark.env_onecard 186def test_print_uint32(): 187 print_testcase(np.uint32) 188 189 190@security_off_wrap 191@pytest.mark.level1 192@pytest.mark.platform_x86_gpu_training 193@pytest.mark.env_onecard 194def test_print_uint64(): 195 print_testcase(np.uint64) 196 197 198@security_off_wrap 199@pytest.mark.level1 200@pytest.mark.platform_x86_gpu_training 201@pytest.mark.env_onecard 202def test_print_float16(): 203 print_testcase(np.float16) 204 205 206@security_off_wrap 207@pytest.mark.level1 208@pytest.mark.platform_x86_gpu_training 209@pytest.mark.env_onecard 210def test_print_float32(): 211 print_testcase(np.float32) 212 213 214@security_off_wrap 215@pytest.mark.level1 216@pytest.mark.platform_x86_gpu_training 217@pytest.mark.env_onecard 218def test_print_string(): 219 print_testcase_string(np.float32) 220