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_nn_ops.py 23 kB

5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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 nn ops """
  16. import numpy as np
  17. import mindspore
  18. import mindspore.context as context
  19. import mindspore.nn as nn
  20. from mindspore import Tensor, Parameter
  21. from mindspore.common.initializer import initializer
  22. from mindspore.ops import Primitive
  23. from mindspore.ops import composite as C
  24. from mindspore.ops import operations as P
  25. from mindspore.ops.operations import _grad_ops as G
  26. from mindspore.ops import prim_attr_register, PrimitiveWithInfer
  27. from ..ut_filter import non_graph_engine
  28. from ....mindspore_test_framework.mindspore_test import mindspore_test
  29. from ....mindspore_test_framework.pipeline.forward.compile_forward \
  30. import pipeline_for_compile_forward_ge_graph_for_case_by_case_config
  31. from ....mindspore_test_framework.pipeline.forward.verify_exception \
  32. import pipeline_for_verify_exception_for_case_by_case_config
  33. def conv3x3(in_channels, out_channels, stride=1, padding=1):
  34. """3x3 convolution """
  35. return nn.Conv2d(in_channels, out_channels,
  36. kernel_size=3, stride=stride, padding=padding)
  37. def conv1x1(in_channels, out_channels, stride=1, padding=0):
  38. """1x1 convolution"""
  39. return nn.Conv2d(in_channels, out_channels,
  40. kernel_size=1, stride=stride, padding=padding)
  41. class ResidualBlock(nn.Cell):
  42. """
  43. residual Block
  44. """
  45. expansion = 4
  46. def __init__(self,
  47. in_channels,
  48. out_channels,
  49. stride=1,
  50. down_sample=False):
  51. super(ResidualBlock, self).__init__()
  52. out_chls = out_channels // self.expansion
  53. self.conv1 = conv1x1(in_channels, out_chls, stride=1, padding=0)
  54. self.bn1 = nn.BatchNorm2d(out_chls)
  55. self.conv2 = conv3x3(out_chls, out_chls, stride=stride, padding=0)
  56. self.bn2 = nn.BatchNorm2d(out_chls)
  57. self.conv3 = conv1x1(out_chls, out_channels, stride=1, padding=0)
  58. self.bn3 = nn.BatchNorm2d(out_channels)
  59. self.relu = nn.ReLU()
  60. self.downsample = down_sample
  61. self.conv_down_sample = conv1x1(in_channels, out_channels,
  62. stride=stride, padding=0)
  63. self.bn_down_sample = nn.BatchNorm2d(out_channels)
  64. self.add = P.TensorAdd()
  65. def construct(self, x):
  66. """
  67. :param x:
  68. :return:
  69. """
  70. identity = x
  71. out = self.conv1(x)
  72. out = self.bn1(out)
  73. out = self.relu(out)
  74. out = self.conv2(out)
  75. out = self.bn2(out)
  76. out = self.relu(out)
  77. out = self.conv3(out)
  78. out = self.bn3(out)
  79. if self.downsample:
  80. identity = self.conv_down_sample(identity)
  81. identity = self.bn_down_sample(identity)
  82. out = self.add(out, identity)
  83. out = self.relu(out)
  84. return out
  85. class VirtualLossGrad(PrimitiveWithInfer):
  86. """ VirtualLossGrad definition """
  87. @prim_attr_register
  88. def __init__(self):
  89. """init VirtualLossGrad"""
  90. def __call__(self, x, out, dout):
  91. raise NotImplementedError
  92. def infer_shape(self, x_shape, out_shape, dout_shape):
  93. return x_shape
  94. def infer_dtype(self, x_dtype, out_dtype, dout_dtype):
  95. return x_dtype
  96. class VirtualLoss(PrimitiveWithInfer):
  97. """ VirtualLoss definition """
  98. @prim_attr_register
  99. def __init__(self):
  100. """init VirtualLoss"""
  101. def __call__(self, x):
  102. raise NotImplementedError
  103. def get_bprop(self):
  104. loss_grad = VirtualLossGrad()
  105. def bprop(x, out, dout):
  106. # pylint: disable=unused-argument
  107. dx = loss_grad(x, out, dout)
  108. return (dx,)
  109. return bprop
  110. def infer_shape(self, x_shape):
  111. return []
  112. def infer_dtype(self, x_dtype):
  113. return x_dtype
  114. class VirtualNetWithLoss(nn.Cell):
  115. """ VirtualNetWithLoss definition """
  116. def __init__(self, network):
  117. super(VirtualNetWithLoss, self).__init__()
  118. self.loss = VirtualLoss()
  119. self.network = network
  120. def construct(self, x):
  121. predict = self.network(x)
  122. return self.loss(predict)
  123. class SoftMaxGrad(nn.Cell):
  124. """ SoftMaxGrad definition """
  125. def __init__(self, network):
  126. super(SoftMaxGrad, self).__init__()
  127. self.network = network
  128. def construct(self, x):
  129. return C.grad(self.network)(x)
  130. class DropoutGrad(nn.Cell):
  131. """ DropoutGrad definition """
  132. def __init__(self, network):
  133. super(DropoutGrad, self).__init__()
  134. self.network = network
  135. def construct(self, x):
  136. return C.grad(self.network)(x)
  137. class ScalarSummaryNet(nn.Cell):
  138. """ ScalarSummaryNet definition """
  139. def __init__(self):
  140. super(ScalarSummaryNet, self).__init__()
  141. self.summary = P.ScalarSummary()
  142. def construct(self, scalar):
  143. string_in = "bias_value"
  144. out = self.summary(string_in, scalar)
  145. return out
  146. class L2NormalizeNet(nn.Cell):
  147. """ L2NormalizeNet definition """
  148. def __init__(self):
  149. super(L2NormalizeNet, self).__init__()
  150. self.l2_normalize = P.L2Normalize()
  151. def construct(self, x):
  152. out = self.l2_normalize(x)
  153. return out
  154. class HistogramSummaryNet(nn.Cell):
  155. """HistogramSummaryNet definition"""
  156. def __init__(self):
  157. super(HistogramSummaryNet, self).__init__()
  158. self.summary = P.HistogramSummary()
  159. def construct(self, tensor):
  160. string_in = "wight_value"
  161. out = self.summary(string_in, tensor)
  162. return out
  163. class FusedBatchNormGrad(nn.Cell):
  164. """ FusedBatchNormGrad definition """
  165. def __init__(self, network):
  166. super(FusedBatchNormGrad, self).__init__()
  167. self.grad = C.GradOperation(name="get_all", get_all=True, sens_param=True)
  168. self.network = network
  169. def construct(self, inp, output_grad):
  170. return self.grad(self.network)(inp, output_grad)
  171. class NetWithLoss(nn.Cell):
  172. """ NetWithLoss definition """
  173. def __init__(self, network):
  174. super(NetWithLoss, self).__init__()
  175. self.loss = P.SmoothL1Loss()
  176. self.network = network
  177. def construct(self, x, label):
  178. predict = self.network(x)
  179. return self.loss(predict, label)
  180. class Grad(nn.Cell):
  181. """ GradWrap definition """
  182. def __init__(self, network):
  183. super(Grad, self).__init__()
  184. self.network = network
  185. self.network.set_train()
  186. def construct(self, x, label):
  187. return C.grad(self.network)(x, label)
  188. class BatchnormNet(nn.Cell):
  189. """ BatchnormNet definition """
  190. def __init__(self):
  191. super(BatchnormNet, self).__init__()
  192. self.conv1 = nn.Conv2d(3, 4, kernel_size=8, stride=2, pad_mode="pad", padding=3)
  193. self.bn1 = nn.BatchNorm2d(4)
  194. self.flatten = P.Flatten()
  195. self.weight = Parameter(Tensor(np.ones([64, 10], np.float32)), name="weight")
  196. self.bias = Parameter(Tensor(np.ones([10], np.float32)), name="bias")
  197. self.fc = P.MatMul()
  198. self.biasAdd = P.BiasAdd()
  199. def construct(self, x):
  200. x = self.conv1(x)
  201. x = self.bn1(x)
  202. x = self.flatten(x)
  203. x = self.biasAdd(self.fc(x, self.weight), self.bias)
  204. return x
  205. class NetWithLossClass(nn.Cell):
  206. """ NetWithLossClass definition """
  207. def __init__(self, network):
  208. super(NetWithLossClass, self).__init__(auto_prefix=False)
  209. self.loss = nn.SoftmaxCrossEntropyWithLogits()
  210. self.network = network
  211. def construct(self, x, label):
  212. predict = self.network(x)
  213. return self.loss(predict, label)
  214. class BlockNet(nn.Cell):
  215. """ BlockNet definition """
  216. def __init__(self):
  217. super(BlockNet, self).__init__()
  218. self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, pad_mode="pad", padding=3)
  219. self.bn1 = nn.BatchNorm2d(64)
  220. self.relu = nn.ReLU()
  221. self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2)
  222. self.block_down_sample = ResidualBlock(
  223. 64, 256, stride=1, down_sample=True
  224. )
  225. self.flatten = P.Flatten()
  226. self.weight = Parameter(Tensor(np.ones([1024, 10]).astype(np.float32)), name="weight")
  227. self.bias = Parameter(Tensor(np.ones([10]).astype((np.float32))), name="bias")
  228. self.fc = P.MatMul()
  229. self.biasAdd = P.BiasAdd()
  230. def construct(self, x):
  231. x = self.conv1(x)
  232. return x
  233. class Conv2dWithBiasNet(nn.Cell):
  234. """ Conv2dWithBiasNet definition """
  235. def __init__(self):
  236. super(Conv2dWithBiasNet, self).__init__()
  237. self.conv = nn.Conv2d(3, 10, 1, bias_init='zeros')
  238. self.flatten = P.Flatten()
  239. def construct(self, input_x):
  240. return self.flatten(self.conv(input_x))
  241. class Conv2dNativeNet(nn.Cell):
  242. """ Conv2dNativeNet definition """
  243. def __init__(self):
  244. super(Conv2dNativeNet, self).__init__()
  245. self.conv = P.DepthwiseConv2dNative(channel_multiplier=3, kernel_size=(3, 3))
  246. self.flatten = P.Flatten()
  247. channel_multipliers = 1
  248. in_channels = 3
  249. kernel_size = (3, 3)
  250. self.weight = Parameter(initializer(
  251. Tensor(np.ones([channel_multipliers, in_channels, *kernel_size], dtype=np.float32)),
  252. [channel_multipliers, in_channels, *kernel_size]), name='weight')
  253. def construct(self, input_x):
  254. return self.flatten(self.conv(input_x, self.weight))
  255. class MakeRefKeyNet(nn.Cell):
  256. """ MakeRefKeyNet definition """
  257. def __init__(self):
  258. super(MakeRefKeyNet, self).__init__()
  259. self.y = Parameter(Tensor([1.0], mindspore.float32), name="y")
  260. def construct(self, x):
  261. key = P.MakeRefKey("y")()
  262. P.Assign()(key, x)
  263. return x
  264. class StateNet(nn.Cell):
  265. """ StateTestTensor definition """
  266. def __init__(self):
  267. super(StateNet, self).__init__()
  268. weight = Tensor(np.ones([2, 1, 2, 2], np.float32))
  269. self.s1 = Parameter(weight, name="s1")
  270. self.s2 = Parameter(weight, name="s2")
  271. self.sub = P.Sub()
  272. self.loss = nn.SoftmaxCrossEntropyWithLogits()
  273. self.assign = P.Assign()
  274. def construct(self, x):
  275. x = Primitive('depend')(x, self.assign(self.s1, x + self.s1))
  276. self.s1 = self.sub(self.s1, x)
  277. self.s2 = self.sub(self.s2, x)
  278. return x
  279. class ComparisonNet(nn.Cell):
  280. def __init__(self):
  281. """ ComparisonNet definition """
  282. super(ComparisonNet, self).__init__()
  283. def construct(self, x, y):
  284. ret = x <= y
  285. return ret
  286. def test_max_pool_with_arg_max():
  287. class NetMaxPoolWithArgMax(nn.Cell):
  288. def __init__(self):
  289. """ ComparisonNet definition """
  290. super(NetMaxPoolWithArgMax, self).__init__()
  291. self.max_pool_with_arg_max = P.MaxPoolWithArgmax(padding="valid", ksize=2, strides=1)
  292. def construct(self, x):
  293. ret = self.max_pool_with_arg_max(x)
  294. return ret
  295. x = Tensor(np.ones([1, 1, 3, 3], np.float32))
  296. net = NetMaxPoolWithArgMax()
  297. context.set_context(mode=context.GRAPH_MODE, save_graphs=True)
  298. ret = net(x)
  299. print(ret)
  300. class GradWrapUnfold(nn.Cell):
  301. """ GradWrapUnfold definition """
  302. def __init__(self, network):
  303. super(GradWrapUnfold, self).__init__()
  304. self.network = network
  305. self.sens = Tensor(np.ones([1, 4, 2, 2], np.float32))
  306. def construct(self, x):
  307. return C.grad_all_with_sens(self.network)(x, self.sens)
  308. class UnfoldNetValid(nn.Cell):
  309. """ UnfoldNetValid definition """
  310. def __init__(self):
  311. super(UnfoldNetValid, self).__init__()
  312. self.unfold = nn.Unfold(ksizes=[1, 2, 2, 1],
  313. strides=[1, 1, 1, 1],
  314. rates=[1, 1, 1, 1],
  315. padding='VALID')
  316. def construct(self, x):
  317. return self.unfold(x)
  318. class UnfoldNetSame(nn.Cell):
  319. """ UnfoldNetSame definition """
  320. def __init__(self):
  321. super(UnfoldNetSame, self).__init__()
  322. self.unfold = nn.Unfold(ksizes=[1, 2, 2, 1],
  323. strides=[1, 1, 1, 1],
  324. rates=[1, 1, 1, 1],
  325. padding='SAME')
  326. def construct(self, x):
  327. return self.unfold(x)
  328. class FlattenNet(nn.Cell):
  329. """ FlattenNet definition """
  330. def __init__(self):
  331. super(FlattenNet, self).__init__()
  332. self.flatten = P.Flatten()
  333. def construct(self, x):
  334. return self.flatten(x)
  335. class PReLUNet(nn.Cell):
  336. """ PReLUNet definition """
  337. def __init__(self):
  338. super(PReLUNet, self).__init__()
  339. self.prelu = P.PReLU()
  340. self.w = Tensor(np.ones(3, np.float32))
  341. def construct(self, x):
  342. return self.prelu(x, self.w)
  343. class PReLUGradNet(nn.Cell):
  344. """ PReLUGradNet definition """
  345. def __init__(self):
  346. super(PReLUGradNet, self).__init__()
  347. self.prelu_grad = G.PReLUGrad()
  348. def construct(self, dout, x, w):
  349. return self.prelu_grad(dout, x, w)
  350. test_cases = [
  351. ('SoftMaxGrad', {
  352. 'block': SoftMaxGrad(VirtualNetWithLoss(P.Softmax())),
  353. 'desc_inputs': [[128, 32, 32, 64]],
  354. 'desc_bprop': [[128, 32, 32, 64]],
  355. }),
  356. ('DropoutGrad', {
  357. 'block': DropoutGrad(VirtualNetWithLoss(nn.Dropout())),
  358. 'desc_inputs': [[128, 32, 32, 64]],
  359. 'desc_bprop': [[128, 32, 32, 64]],
  360. }),
  361. ('ScalarSummary', {
  362. 'block': ScalarSummaryNet(),
  363. 'desc_inputs': [Tensor(2.2)],
  364. }),
  365. ('L2Normalize', {
  366. 'block': L2NormalizeNet(),
  367. 'desc_inputs': [Tensor(np.array([[1.0, 2, 3], [4.0, 5, 6], [7.0, 8, 9]]), mindspore.float32)],
  368. }),
  369. ('HistogramSummary', {
  370. 'block': HistogramSummaryNet(),
  371. 'desc_inputs': [[1, 2, 3]],
  372. }),
  373. ('FusedBatchNormGrad', {
  374. 'block': FusedBatchNormGrad(nn.BatchNorm2d(num_features=512, eps=1e-5, momentum=0.1)),
  375. 'desc_inputs': [[64, 512, 7, 7], [64, 512, 7, 7]],
  376. 'desc_bprop': [[64, 512, 7, 7]],
  377. }),
  378. ('BatchnormGrad', {
  379. 'block': Grad(NetWithLoss(BatchnormNet())),
  380. 'desc_inputs': [Tensor(np.ones([1, 3, 8, 8], np.float32)), Tensor(np.zeros([1, 10], np.float32))],
  381. }),
  382. ('BlockGrad', {
  383. 'block': Grad(NetWithLossClass(BlockNet())),
  384. 'desc_inputs': [Tensor(np.ones([1, 3, 8, 8], np.float32)), Tensor(np.zeros([1, 64, 4, 4], np.float32))],
  385. }),
  386. ('Conv2dWithBiasGrad', {
  387. 'block': Grad(NetWithLossClass(Conv2dWithBiasNet())),
  388. 'desc_inputs': [Tensor(np.ones([1, 3, 16, 16], np.float32)), Tensor(np.zeros([1, 2560], np.float32))],
  389. }),
  390. ('Conv2dNativeGrad', {
  391. 'block': Grad(NetWithLossClass(Conv2dNativeNet())),
  392. 'desc_inputs': [Tensor(np.ones([1, 3, 16, 16], np.float32)), Tensor(np.zeros([1, 1764], np.float32))],
  393. }),
  394. ('MakeRefKey', {
  395. 'block': MakeRefKeyNet(),
  396. 'desc_inputs': [Tensor([2.0], mindspore.float32)],
  397. }),
  398. ('StateTest', {
  399. 'block': StateNet(),
  400. 'desc_inputs': [Tensor(np.ones([2, 1, 2, 2]).astype(np.float32))],
  401. }),
  402. ('StateGrad', {
  403. 'block': Grad(NetWithLossClass(StateNet())),
  404. 'desc_inputs': [Tensor(np.ones([2, 1, 2, 2], np.float32)), Tensor(np.ones([2, 1, 2, 2], np.float32))],
  405. }),
  406. ('ComparisonTest', {
  407. 'block': ComparisonNet(),
  408. 'desc_inputs': [Tensor(np.ones([6, 9, 10], np.int32)), Tensor(np.ones([6, 9, 10], np.int32))],
  409. }),
  410. ('UnfoldValid', {
  411. 'block': UnfoldNetValid(),
  412. 'desc_inputs': [Tensor(np.ones([1, 1, 3, 3], np.float32))],
  413. 'desc_bprop': [Tensor(np.ones([1, 4, 2, 2], np.float32))],
  414. 'skip': ['backward']}),
  415. ('UnfoldSame', {
  416. 'block': UnfoldNetSame(),
  417. 'desc_inputs': [Tensor(np.ones([1, 1, 3, 3], np.float32))],
  418. 'desc_bprop': [Tensor(np.ones([1, 4, 3, 3], np.float32))],
  419. 'skip': ['backward']}),
  420. ('UnfoldGrad', {
  421. 'block': GradWrapUnfold(UnfoldNetValid()),
  422. 'desc_inputs': [Tensor(np.ones([1, 1, 3, 3], np.float32))],
  423. 'desc_bprop': [Tensor(np.ones([1, 4, 2, 2], np.float32))],
  424. 'skip': ['backward']}),
  425. ('LogSigmoid', {
  426. 'block': nn.LogSigmoid(),
  427. 'desc_inputs': [Tensor(np.array([1, 2, 3, 4]).astype(np.float32))],
  428. 'desc_bprop': [Tensor(np.array([1, 2, 3, 4]).astype(np.float32))],
  429. 'skip': ['backward']}),
  430. ('ReduceLogSumExp', {
  431. 'block': nn.ReduceLogSumExp((0,), False),
  432. 'desc_inputs': [Tensor(np.array([3, 4, 5, 6]).astype(np.float32))],
  433. 'desc_bprop': [Tensor(np.array([1, 2, 3, 4]).astype(np.float32))],
  434. 'skip': ['backward']}),
  435. ('FlattenNet', {
  436. 'block': FlattenNet(),
  437. 'desc_inputs': [Tensor(np.ones([1, 2, 3, 4], np.float32))],
  438. }),
  439. ('PReLUNet', {
  440. 'block': PReLUNet(),
  441. 'desc_inputs': [Tensor(np.ones([1, 3, 4, 4], np.float32))],
  442. }),
  443. ('PReLUGradNet', {
  444. 'block': PReLUGradNet(),
  445. 'desc_inputs': [Tensor(np.ones([1, 3, 4, 4], np.float32)),
  446. Tensor(np.ones([1, 3, 4, 4], np.float32)),
  447. Tensor(np.ones(3, np.float32))],
  448. }),
  449. ]
  450. test_cases_for_verify_exception = [
  451. ('ApplyMomentum_Error', {
  452. 'block': (P.ApplyMomentum(), {'exception': TypeError}),
  453. 'desc_inputs': [[2], [128, 32, 32, 64], [128, 32, 32, 64], [128, 32, 32, 64], [128, 32, 32, 64]],
  454. 'desc_bprop': [[128, 32, 32, 64]],
  455. 'skip': ['backward']
  456. }),
  457. ('Conv2d_ValueError_1', {
  458. 'block': (lambda _: P.Conv2D(3, 4, mode=-2.0), {'exception': TypeError}),
  459. 'desc_inputs': [0],
  460. }),
  461. ('Conv2d_ValueError_2', {
  462. 'block': (lambda _: P.Conv2D(3, 4, mode=-2), {'exception': ValueError}),
  463. 'desc_inputs': [0],
  464. }),
  465. ('MaxPoolWithArgmax_ValueError_1', {
  466. 'block': (lambda _: P.MaxPoolWithArgmax(padding='sane'), {'exception': ValueError}),
  467. 'desc_inputs': [0],
  468. }),
  469. ('MaxPoolWithArgmax_ValueError_2', {
  470. 'block': (lambda _: P.MaxPoolWithArgmax(ksize='1'), {'exception': TypeError}),
  471. 'desc_inputs': [0],
  472. }),
  473. ('MaxPoolWithArgmax_ValueError_3', {
  474. 'block': (lambda _: P.MaxPoolWithArgmax(ksize=-2), {'exception': ValueError}),
  475. 'desc_inputs': [0],
  476. }),
  477. ('MaxPoolWithArgmax_ValueError_4', {
  478. 'block': (lambda _: P.MaxPoolWithArgmax(strides=-1), {'exception': ValueError}),
  479. 'desc_inputs': [0],
  480. }),
  481. ('FusedBatchNorm_ValueError_1', {
  482. 'block': (lambda _: P.FusedBatchNorm(mode="1", epsilon=1e-5, momentum=0.1), {'exception': TypeError}),
  483. 'desc_inputs': [0],
  484. }),
  485. ('FusedBatchNorm_ValueError_2', {
  486. 'block': (lambda _: P.FusedBatchNorm(mode=2, epsilon=1e-5, momentum=0.1), {'exception': ValueError}),
  487. 'desc_inputs': [0],
  488. }),
  489. ('FusedBatchNorm_ValueError_3', {
  490. 'block': (lambda _: P.FusedBatchNorm(mode=0, epsilon=-1e-5, momentum=0.1), {'exception': ValueError}),
  491. 'desc_inputs': [0],
  492. }),
  493. ('FusedBatchNorm_ValueError_4', {
  494. 'block': (lambda _: P.FusedBatchNorm(mode=0, epsilon=1e-5, momentum=-0.1), {'exception': ValueError}),
  495. 'desc_inputs': [0],
  496. }),
  497. ('FusedBatchNorm_ValueError_5', {
  498. 'block': (lambda _: P.FusedBatchNorm(mode=1, epsilon=-0.001, momentum=0.0), {'exception': ValueError}),
  499. 'desc_inputs': [0],
  500. }),
  501. ('Softmax_ValueError_1', {
  502. 'block': (lambda _: P.Softmax("1"), {'exception': TypeError}),
  503. 'desc_inputs': [0],
  504. }),
  505. ('Softmax_ValueError_2', {
  506. 'block': (lambda _: P.Softmax(1.1), {'exception': TypeError}),
  507. 'desc_inputs': [0],
  508. }),
  509. ('Softmax_ValueError_3', {
  510. 'block': (lambda _: P.Softmax(axis="1"), {'exception': TypeError}),
  511. 'desc_inputs': [0],
  512. }),
  513. ('DropoutGenMask_ValueError_1', {
  514. 'block': (lambda _: P.DropoutGenMask(Seed0="seed0"), {'exception': TypeError}),
  515. 'desc_inputs': [0],
  516. }),
  517. ('DropoutGenMask_ValueError_2', {
  518. 'block': (lambda _: P.DropoutGenMask(Seed0=1.0), {'exception': TypeError}),
  519. 'desc_inputs': [0],
  520. }),
  521. ('DropoutGenMask_ValueError_3', {
  522. 'block': (lambda _: P.DropoutGenMask(Seed1="seed1"), {'exception': TypeError}),
  523. 'desc_inputs': [0],
  524. }),
  525. ('DropoutGenMask_ValueError_4', {
  526. 'block': (lambda _: P.DropoutGenMask(Seed1=2.0), {'exception': TypeError}),
  527. 'desc_inputs': [0],
  528. }),
  529. ('MaxPool2d_ValueError_1', {
  530. 'block': (nn.MaxPool2d(kernel_size=120, stride=1, pad_mode="valid"), {'exception': ValueError}),
  531. 'desc_inputs': [Tensor(np.random.randn(32, 3, 112, 112).astype(np.float32).transpose(0, 3, 1, 2))],
  532. }),
  533. ('MaxPool2d_ValueError_2', {
  534. 'block': (
  535. lambda _: nn.MaxPool2d(kernel_size=120, stride=True, pad_mode="valid"),
  536. {'exception': TypeError},
  537. ),
  538. 'desc_inputs': [Tensor(np.random.randn(32, 3, 112, 112).astype(np.float32).transpose(0, 3, 1, 2))],
  539. }),
  540. ('MaxPool2d_ValueError_3', {
  541. 'block': (
  542. lambda _: nn.MaxPool2d(kernel_size=3, stride=True, pad_mode="valid"),
  543. {'exception': TypeError},
  544. ),
  545. 'desc_inputs': [Tensor(np.random.randn(32, 3, 112, 112).astype(np.float32).transpose(0, 3, 1, 2))],
  546. }),
  547. ('ReduceLogsumexp_TypeError_1', {
  548. 'block': (
  549. lambda _: nn.ReduceLogSumExp(axis=(0,), keep_dims=2),
  550. {'exception': TypeError},
  551. ),
  552. 'desc_inputs': [Tensor(np.array([3, 4, 5, 6]).astype(np.float32))],
  553. }),
  554. ('ReduceLogsumexp_TypeError_2', {
  555. 'block': (
  556. lambda _: nn.ReduceLogSumExp(axis=1.2, keep_dims=True),
  557. {'exception': TypeError},
  558. ),
  559. 'desc_inputs': [Tensor(np.array([3, 4, 5, 6]).astype(np.float32))],
  560. }),
  561. ]
  562. @non_graph_engine
  563. @mindspore_test(pipeline_for_compile_forward_ge_graph_for_case_by_case_config)
  564. def test_compile():
  565. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  566. return test_cases
  567. @mindspore_test(pipeline_for_verify_exception_for_case_by_case_config)
  568. def test_check_exception():
  569. return test_cases_for_verify_exception