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_ops_check.py 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. """test checking for some ops"""
  16. import functools
  17. import logging
  18. import numpy as np
  19. import pytest
  20. from mindspore import nn
  21. from mindspore import Tensor
  22. from mindspore.ops import operations as P
  23. from mindspore.common.api import _executor
  24. from ..ut_filter import non_graph_engine
  25. from ....mindspore_test_framework.mindspore_test import mindspore_test
  26. from ....mindspore_test_framework.pipeline.forward.compile_forward \
  27. import pipeline_for_compile_forward_ge_graph_for_case_by_case_config
  28. from ....mindspore_test_framework.pipeline.forward.verify_exception \
  29. import pipeline_for_verify_exception_for_case_by_case_config
  30. logging.basicConfig(level=logging.WARNING)
  31. class NetMissConstruct(nn.Cell):
  32. """ NetMissConstruct definition """
  33. def __init__(self):
  34. super(NetMissConstruct, self).__init__()
  35. self.conv1 = nn.Conv2d(1, 6, 5, pad_mode='valid')
  36. self.conv2 = nn.Conv2d(6, 16, 5, pad_mode='valid')
  37. self.fc1 = nn.Dense(16 * 5 * 5, 120)
  38. self.fc2 = nn.Dense(120, 84)
  39. self.fc3 = nn.Dense(84, 10)
  40. self.relu = nn.ReLU()
  41. self.max_pool2d = nn.MaxPool2d(kernel_size=2)
  42. self.flatten = P.Flatten()
  43. # pylint: disable=abstract-method
  44. # TestCase: Mis-spelled 'construct' to 'construtc'
  45. def construtc(self, x):
  46. x = self.max_pool2d(self.relu(self.conv1(x)))
  47. x = self.max_pool2d(self.relu(self.conv2(x)))
  48. x = self.flatten(x)
  49. x = self.relu(self.fc1(x))
  50. x = self.relu(self.fc2(x))
  51. x = self.fc3(x)
  52. return x
  53. def test_net_without_construct():
  54. """ test_net_without_construct """
  55. net = NetMissConstruct()
  56. inp = Tensor(np.ones([1, 1, 32, 32]).astype(np.float32))
  57. try:
  58. _executor.compile(net, inp)
  59. except RuntimeError as err:
  60. if str(err).find("Unsupported syntax 'Raise' at ") >= 0:
  61. print(str(err))
  62. else:
  63. raise err
  64. class NetWithRaise(nn.Cell):
  65. """ NetWithRaise definition """
  66. def __init__(self):
  67. super(NetWithRaise, self).__init__()
  68. self.conv1 = nn.Conv2d(1, 6, 5, pad_mode='valid')
  69. # raise exception in method 'construct'
  70. def construct(self, x):
  71. raise 'exception in construct'
  72. def test_net_with_raise():
  73. """ test_net_with_raise """
  74. net = NetWithRaise()
  75. inp = Tensor(np.ones([1, 1, 32, 32]).astype(np.float32))
  76. try:
  77. _executor.compile(net, inp)
  78. except RuntimeError as err:
  79. if str(err).find("Unsupported syntax 'Raise' at ") >= 0:
  80. print(str(err))
  81. else:
  82. raise err
  83. class NetAddN(nn.Cell):
  84. """net for test AddN"""
  85. def __init__(self):
  86. super(NetAddN, self).__init__()
  87. self.net = P.AddN()
  88. def construct(self, x):
  89. return self.net(x)
  90. class NetSplit(nn.Cell):
  91. "net for test Split"
  92. def __init__(self):
  93. super(NetSplit, self).__init__()
  94. self.net = P.Split(1, 2)
  95. def construct(self, x):
  96. return self.net(x)
  97. class NetBatchMatMul(nn.Cell):
  98. """net for test BatchMatMul"""
  99. def __init__(self):
  100. super(NetBatchMatMul, self).__init__()
  101. self.op = P.BatchMatMul()
  102. def construct(self, x, y):
  103. return self.op(x, y)
  104. test_case_check_ops = [
  105. ('Conv_Padding_1', {
  106. 'block': nn.Conv2d(1, 6, 5, pad_mode='same', padding=0),
  107. 'desc_inputs': [Tensor(np.ones(shape=[1, 1, 6, 5]).astype(np.float32))]}),
  108. ('Conv_Padding_2', {
  109. 'block': nn.Conv2d(1, 6, 5, pad_mode='valid', padding=0),
  110. 'desc_inputs': [Tensor(np.ones(shape=[1, 1, 6, 5]).astype(np.float32))]}),
  111. ('Conv_Padding_3', {
  112. 'block': nn.Conv2d(1, 6, 5, pad_mode='pad', padding=0),
  113. 'desc_inputs': [Tensor(np.ones(shape=[1, 1, 6, 5]).astype(np.float32))]}),
  114. ('Conv_Padding_4', {
  115. 'block': nn.Conv2d(1, 6, 5, pad_mode='pad', padding=7),
  116. 'desc_inputs': [Tensor(np.ones(shape=[1, 1, 6, 5]).astype(np.float32))]}),
  117. ('Conv_Bias_1', {
  118. 'block': nn.Conv2d(1, 6, 5, has_bias=True, bias_init=Tensor(np.ones([6]).astype(np.float32))),
  119. 'desc_inputs': [Tensor(np.ones(shape=[1, 1, 6, 5]).astype(np.float32))]}),
  120. ('Conv_Bias_2', {
  121. 'block': nn.Conv2d(1, 6, 5, has_bias=True, bias_init='zeros'),
  122. 'desc_inputs': [Tensor(np.ones(shape=[1, 1, 6, 5]).astype(np.float32))]}),
  123. ('Conv_Bias_3', {
  124. 'block': nn.Conv2d(1, 6, 5, has_bias=False, bias_init='zeros'),
  125. 'desc_inputs': [Tensor(np.ones(shape=[1, 1, 6, 5]).astype(np.float32))]}),
  126. ('Conv_Bias_4', {
  127. 'block': nn.Conv2d(1, 6, 5, has_bias=False, bias_init=Tensor(np.ones([6]).astype(np.float32))),
  128. 'desc_inputs': [Tensor(np.ones(shape=[1, 1, 6, 5]).astype(np.float32))]}),
  129. ('Dense_Bias_1', {
  130. 'block': nn.Dense(1, 6, has_bias=True, bias_init=Tensor(np.ones([6]).astype(np.float32))),
  131. 'desc_inputs': [Tensor(np.ones(shape=[6, 1]).astype(np.float32))]}),
  132. ('Dense_Bias_2', {
  133. 'block': nn.Dense(1, 6, has_bias=True, bias_init='zeros'),
  134. 'desc_inputs': [Tensor(np.ones(shape=[6, 1]).astype(np.float32))]}),
  135. ('Dense_Bias_3', {
  136. 'block': nn.Dense(1, 6, has_bias=False, bias_init='zeros'),
  137. 'desc_inputs': [Tensor(np.ones(shape=[6, 1]).astype(np.float32))]}),
  138. ('Dense_Bias_4', {
  139. 'block': nn.Dense(1, 6, has_bias=False, bias_init=Tensor(np.ones([6]).astype(np.float32))),
  140. 'desc_inputs': [Tensor(np.ones(shape=[6, 1]).astype(np.float32))]}),
  141. ('MaxPool2d_1', {
  142. 'block': nn.MaxPool2d(5, pad_mode='same'),
  143. 'desc_inputs': [Tensor(np.ones(shape=[5, 5, 8, 8]).astype(np.float32))]}),
  144. ('MaxPool2d_2', {
  145. 'block': nn.MaxPool2d(5, pad_mode='valid'),
  146. 'desc_inputs': [Tensor(np.ones(shape=[5, 5, 8, 8]).astype(np.float32))]}),
  147. ('AvgPool2d_1', {
  148. 'block': nn.AvgPool2d(5, pad_mode='same'),
  149. 'desc_inputs': [Tensor(np.ones(shape=[5, 5, 8, 8]).astype(np.float32))]}),
  150. ('AvgPool2d_2', {
  151. 'block': nn.AvgPool2d(5, pad_mode='valid'),
  152. 'desc_inputs': [Tensor(np.ones(shape=[5, 5, 8, 8]).astype(np.float32))]}),
  153. ('Conv2D_1', {
  154. 'block': P.Conv2D(1, 6, pad_mode='same', pad=0),
  155. 'desc_inputs': [Tensor(np.ones(shape=[5, 5, 8, 8]).astype(np.float32)),
  156. Tensor(np.ones(shape=[1, 5, 6, 6]).astype(np.float32))]}),
  157. ('Conv2D_2', {
  158. 'block': P.Conv2D(1, 6, pad_mode='valid', pad=0),
  159. 'desc_inputs': [Tensor(np.ones(shape=[5, 5, 8, 8]).astype(np.float32)),
  160. Tensor(np.ones(shape=[1, 5, 6, 6]).astype(np.float32))]}),
  161. ('Conv2D_3', {
  162. 'block': P.Conv2D(1, 6, pad_mode='pad', pad=0),
  163. 'desc_inputs': [Tensor(np.ones(shape=[5, 5, 8, 8]).astype(np.float32)),
  164. Tensor(np.ones(shape=[1, 5, 6, 6]).astype(np.float32))]}),
  165. ('Conv2D_4', {
  166. 'block': P.Conv2D(1, 6, pad_mode='pad', pad=7),
  167. 'desc_inputs': [Tensor(np.ones(shape=[5, 5, 8, 8]).astype(np.float32)),
  168. Tensor(np.ones(shape=[1, 5, 6, 6]).astype(np.float32))]}),
  169. ('MatMul_1', {
  170. 'block': P.MatMul(),
  171. 'desc_inputs': [Tensor(np.ones(shape=[1, 3])), Tensor(np.ones(shape=[3, 4]))]}),
  172. ('MatMul_2', {
  173. 'block': P.BatchMatMul(),
  174. 'desc_inputs': [Tensor(np.ones(shape=[5, 1, 5])), Tensor(np.ones(shape=[5, 5, 4]))]}),
  175. ('MatMul_Transpose_1', {
  176. 'block': P.MatMul(transpose_a=True),
  177. 'desc_inputs': [Tensor(np.ones(shape=[3, 1])), Tensor(np.ones(shape=[3, 4]))]}),
  178. ('MatMul_Transpose_2', {
  179. 'block': P.MatMul(transpose_b=True),
  180. 'desc_inputs': [Tensor(np.ones(shape=[3, 2])), Tensor(np.ones(shape=[5, 2]))]}),
  181. ('MatMul_Transpose_3', {
  182. 'block': P.MatMul(transpose_a=True, transpose_b=True),
  183. 'desc_inputs': [Tensor(np.ones(shape=[3, 2])), Tensor(np.ones(shape=[5, 3]))]}),
  184. ('BatchMatMul', {
  185. 'block': NetBatchMatMul(),
  186. 'desc_inputs': [Tensor(np.ones(shape=[3, 1, 5])), Tensor(np.ones(shape=[3, 5, 4]))]}),
  187. ]
  188. test_case_lists = [test_case_check_ops]
  189. test_exec_case = functools.reduce(lambda x, y: x + y, test_case_lists)
  190. # use -k to select certain testcast
  191. # pytest tests/python/ops/test_ops.py::test_backward -k LayerNorm
  192. import mindspore.context as context
  193. @non_graph_engine
  194. @mindspore_test(pipeline_for_compile_forward_ge_graph_for_case_by_case_config)
  195. def test_exec():
  196. context.set_context(mode=context.GRAPH_MODE)
  197. return test_exec_case
  198. raise_set = [
  199. ('Conv_Padding_1_Error', {
  200. 'block': (lambda x: nn.Conv2d(1, 6, 5, pad_mode='same', padding=7), {'exception': ValueError}),
  201. 'desc_inputs': [Tensor(np.ones(shape=[1, 1, 6, 5]).astype(np.float32))]}),
  202. ('Conv_Padding_2_Error', {
  203. 'block': (lambda x: nn.Conv2d(1, 6, 5, pad_mode='same', padding=7), {'exception': ValueError}),
  204. 'desc_inputs': [Tensor(np.ones(shape=[1, 1, 6, 5]).astype(np.float32))]}),
  205. ('Conv2D_1_Error', {
  206. 'block': (lambda x, y: P.Conv2D(1, 6, pad_mode='same', pad=7), {'exception': ValueError}),
  207. 'desc_inputs': [Tensor(np.ones(shape=[5, 5, 8, 8]).astype(np.float32)),
  208. Tensor(np.ones(shape=[1, 5, 6, 6]).astype(np.float32))]}),
  209. ('Conv2D_2_Error', {
  210. 'block': (lambda x, y: P.Conv2D(1, 6, pad_mode='valid', pad=7), {'exception': ValueError}),
  211. 'desc_inputs': [Tensor(np.ones(shape=[5, 5, 8, 8]).astype(np.float32)),
  212. Tensor(np.ones(shape=[1, 5, 6, 6]).astype(np.float32))]}),
  213. ('NetAddN_Error', {
  214. 'block': (NetAddN(), {'exception': TypeError}),
  215. 'desc_inputs': [(np.random.randn(1, 2, 3, 4).astype(np.float32),
  216. np.random.randn(1, 2, 3, 4).astype(np.float32))]}),
  217. ('AddN_Error', {
  218. 'block': (P.AddN(), {'exception': TypeError}),
  219. 'desc_inputs': [(np.random.randn(1, 2, 3, 4).astype(np.float32),
  220. np.random.randn(1, 2, 3, 4).astype(np.float32))]}),
  221. ('Splite_Error', {
  222. 'block': (NetSplit(), {'exception': TypeError}),
  223. 'desc_inputs': [None]}),
  224. ('MatMul_1_Error', {
  225. 'block': (P.MatMul(), {'exception': ValueError}),
  226. 'desc_inputs': [Tensor(np.ones(shape=[5])), Tensor(np.ones(shape=[4]))]}),
  227. ('MatMul_2_Error', {
  228. 'block': (P.MatMul(), {'exception': ValueError}),
  229. 'desc_inputs': [Tensor(np.ones(shape=[1, 5])), Tensor(np.ones(shape=[3, 4]))]}),
  230. ('MatMul_3_Error', {
  231. 'block': (P.MatMul(), {'exception': ValueError}),
  232. 'desc_inputs': [Tensor(np.ones(shape=[1, 5])), Tensor(np.ones(shape=[5, 5, 4]))]}),
  233. ('MatMul_Transpose_1_Error', {
  234. 'block': (P.MatMul(transpose_a=True), {'exception': ValueError}),
  235. 'desc_inputs': [Tensor(np.ones(shape=[1, 3])), Tensor(np.ones(shape=[3, 4]))]}),
  236. ('MatMul_Transpose_2_Error', {
  237. 'block': (P.MatMul(transpose_b=True), {'exception': ValueError}),
  238. 'desc_inputs': [Tensor(np.ones(shape=[3, 2])), Tensor(np.ones(shape=[2, 5]))]}),
  239. ('MatMul_Transpose_3_Error', {
  240. 'block': (P.MatMul(transpose_a=True, transpose_b=True), {'exception': ValueError}),
  241. 'desc_inputs': [Tensor(np.ones(shape=[3, 2])), Tensor(np.ones(shape=[3, 5]))]}),
  242. ('BatchMatMul_1_Error', {
  243. 'block': (P.BatchMatMul(), {'exception': ValueError}),
  244. 'desc_inputs': [Tensor(np.ones(shape=[5])), Tensor(np.ones(shape=[4]))]}),
  245. ('BatchMatMul_2_Error', {
  246. 'block': (P.BatchMatMul(), {'exception': ValueError}),
  247. 'desc_inputs': [Tensor(np.ones(shape=[1, 5])), Tensor(np.ones(shape=[3, 4]))]}),
  248. ('BatchMatMul_3_Error', {
  249. 'block': (P.BatchMatMul(), {'exception': ValueError}),
  250. 'desc_inputs': [Tensor(np.ones(shape=[3, 1, 5])), Tensor(np.ones(shape=[3, 3, 4]))]}),
  251. ('BatchMatMul_4_Error', {
  252. 'block': (P.BatchMatMul(), {'exception': ValueError}),
  253. 'desc_inputs': [Tensor(np.ones(shape=[3, 1, 5])), Tensor(np.ones(shape=[1, 3, 5, 4]))]}),
  254. ('BatchMatMul_5_Error', {
  255. 'block': (P.BatchMatMul(), {'exception': ValueError}),
  256. 'desc_inputs': [Tensor(np.ones(shape=[3, 1, 5])), Tensor(np.ones(shape=[2, 5, 4]))]}),
  257. ]
  258. @mindspore_test(pipeline_for_verify_exception_for_case_by_case_config)
  259. def test_check_exception():
  260. return raise_set