You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_print_op.py 5.4 kB

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