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.py 43 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  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 ops """
  16. import functools
  17. import numpy as np
  18. from mindspore import ops
  19. from mindspore.ops import functional as F
  20. from mindspore.ops import operations as P
  21. from mindspore.ops.operations import _grad_ops as G
  22. import mindspore.ops.composite as C
  23. import mindspore.nn as nn
  24. from mindspore import Tensor
  25. from mindspore.common import dtype as mstype
  26. from ..ut_filter import non_graph_engine
  27. from ....mindspore_test_framework.mindspore_test import mindspore_test
  28. from ....mindspore_test_framework.pipeline.forward.compile_forward\
  29. import (pipeline_for_compile_forward_ge_graph_for_case_by_case_config,
  30. pipeline_for_compile_forward_ge_graph_for_case_by_case_config_exception)
  31. from ....mindspore_test_framework.pipeline.gradient.compile_gradient\
  32. import pipeline_for_compile_grad_ge_graph_for_case_by_case_config
  33. class InputBackward(nn.Cell):
  34. def __init__(self, network):
  35. super(InputBackward, self).__init__()
  36. self.network = network
  37. self.network.set_train()
  38. self.grad = C.grad_all_with_sens
  39. def construct(self, x1, x2, x3, sens):
  40. return self.grad(self.network)(x1, x2, x3, sens)
  41. class NetForTupleInput(nn.Cell):
  42. def __init__(self, op):
  43. super(NetForTupleInput, self).__init__()
  44. self.op = op
  45. def construct(self, x1, x2):
  46. return self.op((x1, x2))
  47. class StridedSlicessdNet(nn.Cell):
  48. def __init__(self):
  49. super(StridedSlicessdNet, self).__init__()
  50. self.rank = P.Rank()
  51. def construct(self, x1):
  52. return P.StridedSlice(1, 1, 0, self.rank(x1), 0)(x1, (0, 0), (0, 0), (1, 1))
  53. class NetForConcat(nn.Cell):
  54. def __init__(self):
  55. super(NetForConcat, self).__init__()
  56. self.concat = P.Concat()
  57. def construct(self, x1):
  58. return self.concat((x1, x1))
  59. class NetForConcat1(nn.Cell):
  60. def __init__(self):
  61. super(NetForConcat1, self).__init__()
  62. self.concat = P.Concat()
  63. def construct(self, x1, x2):
  64. return self.concat((x1, x2))
  65. class NetForPackInput(nn.Cell):
  66. def __init__(self, op):
  67. super(NetForPackInput, self).__init__()
  68. self.op = op
  69. self.mul = P.Mul()
  70. def construct(self, *args):
  71. t = ()
  72. for i in range(len(args)):
  73. t = t + (self.mul(args[i], args[i]),)
  74. return self.op(t)
  75. class NetForUnpackInput(nn.Cell):
  76. def __init__(self, op):
  77. super(NetForUnpackInput, self).__init__()
  78. self.op = op
  79. self.mul = P.Mul()
  80. def construct(self, x1):
  81. return self.op((self.mul(x1, x1)))
  82. class NetForFlatten(nn.Cell):
  83. def __init__(self):
  84. super(NetForFlatten, self).__init__()
  85. self.flatten = P.Flatten()
  86. def construct(self, x, y):
  87. return self.flatten(x) + y
  88. class NetForFlatten0D(nn.Cell):
  89. def __init__(self):
  90. super(NetForFlatten0D, self).__init__()
  91. self.flatten = P.Flatten()
  92. def construct(self, x):
  93. return self.flatten(x)
  94. class ArgmaxNet(nn.Cell):
  95. def __init__(self):
  96. super(ArgmaxNet, self).__init__()
  97. self.argmax = P.Argmax(axis=1)
  98. def construct(self, input):
  99. return self.argmax(input)
  100. class ArgminNet(nn.Cell):
  101. def __init__(self):
  102. super(ArgminNet, self).__init__()
  103. self.argmin = P.Argmin(axis=1)
  104. def construct(self, input):
  105. return self.argmin(input)
  106. class CumSumNet(nn.Cell):
  107. def __init__(self):
  108. super(CumSumNet, self).__init__()
  109. self.cumsum = P.CumSum()
  110. self.axis = 1
  111. def construct(self, input):
  112. return self.cumsum(input, self.axis)
  113. class SummaryNet(nn.Cell):
  114. def __init__(self,):
  115. super(SummaryNet, self).__init__()
  116. self.s = P.ScalarSummary()
  117. self.add = P.TensorAdd()
  118. def construct(self, x, y):
  119. self.s("x1", x)
  120. return self.add(x, y)
  121. class HistogramSummaryNet(nn.Cell):
  122. def __init__(self,):
  123. super(HistogramSummaryNet, self).__init__()
  124. self.summary = P.HistogramSummary()
  125. self.add = P.TensorAdd()
  126. def construct(self, x, y):
  127. out = self.add(x, y)
  128. string_in = "out"
  129. self.summary(string_in, out)
  130. return out
  131. test_case_math_ops = [
  132. ('Neg', {
  133. 'block': P.Neg(),
  134. 'desc_inputs': [[1, 3, 4, 4]],
  135. 'desc_bprop': [[1, 3, 4, 4]]}),
  136. ('Sub', {
  137. 'block': P.Sub(),
  138. 'desc_inputs': [[3, 5], [2, 3, 3, 5]],
  139. 'desc_bprop': [[2, 3, 3, 5]]}),
  140. ('TensorAdd', {
  141. 'block': P.TensorAdd(),
  142. 'desc_inputs': [[3, 5], [2, 3, 3, 5]],
  143. 'desc_bprop': [[2, 3, 3, 5]]}),
  144. ('Mul0', {
  145. 'block': P.Mul(),
  146. 'desc_inputs': [[2, 3, 3, 5], [2, 3, 3, 5]],
  147. 'desc_bprop': [[2, 3, 3, 5]]}),
  148. ('Mul1', {
  149. 'block': P.Mul(),
  150. 'desc_inputs': [[2, 3, 1, 1], [2, 3, 3, 5]],
  151. 'desc_bprop': [[2, 3, 3, 5]]}),
  152. ('Mul2', {
  153. 'block': P.Mul(),
  154. 'desc_inputs': [[2, 3, 3, 5], [2, 3, 1, 1]],
  155. 'desc_bprop': [[2, 3, 3, 5]],
  156. 'skip': ['backward']}),
  157. ('Mul3', {
  158. 'block': P.Mul(),
  159. 'desc_inputs': [[3, 5], [2, 3, 3, 5]],
  160. 'desc_bprop': [[2, 3, 3, 5]],
  161. 'skip': ['backward']}),
  162. ('Mul4', {
  163. 'block': P.Mul(),
  164. 'desc_inputs': [[2, 3, 3, 5], [3, 5]],
  165. 'desc_bprop': [[2, 3, 3, 5]],
  166. 'skip': ['backward']}),
  167. ('Add0', {
  168. 'block': P.TensorAdd(),
  169. 'desc_inputs': [[2, 3, 3, 5], [2, 3, 3, 5]],
  170. 'desc_bprop': [[2, 3, 3, 5]]}),
  171. ('Add1', {
  172. 'block': P.TensorAdd(),
  173. 'desc_inputs': [[3, 5], [2, 3, 3, 5]],
  174. 'desc_bprop': [[2, 3, 3, 5]],
  175. 'skip': ['backward']}),
  176. ('Add2', {
  177. 'block': P.TensorAdd(),
  178. 'desc_inputs': [[2, 3, 3, 5], [3, 5]],
  179. 'desc_bprop': [[2, 3, 3, 5]],
  180. 'skip': ['backward']}),
  181. ('Add3', {
  182. 'block': P.TensorAdd(),
  183. 'desc_inputs': [[2, 3, 1, 1], [2, 3, 3, 5]],
  184. 'desc_bprop': [[2, 3, 3, 5]],
  185. 'skip': ['backward']}),
  186. ('Add4', {
  187. 'block': P.TensorAdd(),
  188. 'desc_inputs': [[2, 3, 3, 5], [2, 3, 1, 1]],
  189. 'desc_bprop': [[2, 3, 3, 5]],
  190. 'skip': ['backward']}),
  191. ('Minimum', {
  192. 'block': P.Minimum(),
  193. 'desc_inputs': [[2, 3, 3, 5], [2, 3, 3, 5]],
  194. 'desc_bprop': [[2, 3, 3, 5]]}),
  195. ('Pow_0', {
  196. 'block': P.Pow(),
  197. 'desc_const': [2.0],
  198. 'desc_inputs': [[2, 3, 3, 5]],
  199. 'desc_bprop': [[2, 3, 3, 5]]}),
  200. ('Pow_1', {
  201. 'block': P.Pow(),
  202. 'desc_inputs': [[3, 5], [2, 3, 3, 5]],
  203. 'desc_bprop': [[2, 3, 3, 5]]}),
  204. ('Exp', {
  205. 'block': P.Exp(),
  206. 'desc_inputs': [[2, 3]],
  207. 'desc_bprop': [[2, 3]]}),
  208. ('Erf', {
  209. 'block': P.Erf(),
  210. 'desc_inputs': [Tensor(np.array([-2, -1, 0, 1, 2]).astype(np.float16))],
  211. 'desc_bprop': [Tensor(np.array([-2, -1, 0, 1, 2]).astype(np.float16))]}),
  212. ('Floor', {
  213. 'block': P.Floor(),
  214. 'desc_inputs': [[2, 512, 56, 56]],
  215. 'desc_bprop': [[2, 512, 56, 56]],
  216. 'skip': ['backward']}),
  217. ('ACos', {
  218. 'block': P.ACos(),
  219. 'desc_inputs': [[2, 3]],
  220. 'desc_bprop': [[2, 3]]}),
  221. ('Acosh', {
  222. 'block': P.Acosh(),
  223. 'desc_inputs': [[3, 4, 5]],
  224. 'desc_bprop': [[3, 4, 5]]}),
  225. ('Sin', {
  226. 'block': P.Sin(),
  227. 'desc_inputs': [[2, 3]],
  228. 'desc_bprop': [[2, 3]]}),
  229. ('Reciprocal', {
  230. 'block': P.Reciprocal(),
  231. 'desc_inputs': [[2, 3, 3, 5]],
  232. 'desc_bprop': [[2, 3, 3, 5]]}),
  233. ('Minimum_0', {
  234. 'block': P.Minimum(),
  235. 'desc_inputs': [[2, 3, 3, 5], [3, 3, 5]],
  236. 'desc_bprop': [[2, 3, 3, 5]]}),
  237. ('Maximum', {
  238. 'block': P.Maximum(),
  239. 'desc_inputs': [[2, 3, 3, 5], [2, 3, 3, 5]],
  240. 'desc_bprop': [[2, 3, 3, 5]]}),
  241. ('Maximum_0', {
  242. 'block': P.Maximum(),
  243. 'desc_inputs': [[3, 5], [2, 3, 3, 5]],
  244. 'desc_bprop': [[2, 3, 3, 5]]}),
  245. ('MaximumGrad', {
  246. 'block': G.MaximumGrad(),
  247. 'desc_inputs': [[2, 3, 3, 5], [2, 3, 3, 5], [2, 3, 3, 5]],
  248. 'skip': ['backward']}),
  249. ('MinimumGrad', {
  250. 'block': G.MinimumGrad(),
  251. 'desc_inputs': [[2, 3, 3, 5], [2, 3, 3, 5], [2, 3, 3, 5]],
  252. 'skip': ['backward']}),
  253. ('StridedSlice', {
  254. 'block': P.StridedSlice(),
  255. 'desc_const': [(0, 1, 2, 1),
  256. (2, 3, 3, 4),
  257. (1, 1, 1, 1)],
  258. 'desc_inputs': [[2, 3, 3, 5]],
  259. 'desc_bprop': [[2, 2, 1, 3]]}),
  260. ('Slice_1', {
  261. 'block': P.Slice(),
  262. 'desc_const': [(0, 1, 2, 1),
  263. (1, 1, 1, 2)],
  264. 'desc_inputs': [[2, 3, 3, 5]],
  265. 'desc_bprop': [[1, 1, 1, 2]]}),
  266. ('StridedSliceGrad', {
  267. 'block': G.StridedSliceGrad(),
  268. 'desc_const': [(64, 1, 1024),
  269. (0, 1, 0),
  270. (64, 2, 1024),
  271. (1, 1, 1)],
  272. 'desc_inputs': [[64, 128, 1024]],
  273. 'skip': ['backward']}),
  274. ('RandomChoiceWithMask', {
  275. 'block': P.RandomChoiceWithMask(256),
  276. 'desc_inputs': [Tensor(np.random.rand(24000, 4).astype(np.bool_))],
  277. 'desc_bprop': [[256,4], [256,4]],
  278. 'skip': ['backward']}),
  279. ('LessEqual', {
  280. 'block': P.LessEqual(),
  281. 'desc_inputs': [Tensor(np.random.rand(4).astype(np.float16)),
  282. Tensor(np.random.rand(4).astype(np.float16))],
  283. 'skip': ['backward']}),
  284. ('Less', {
  285. 'block': P.Less(),
  286. 'desc_inputs': [[2, 1, 4, 5], [2, 1, 4, 5]],
  287. 'desc_bprop': [Tensor(np.zeros((2, 1, 4, 5), np.bool_))],
  288. 'skip': ['backward']}),
  289. ('RealDiv_0', {
  290. 'block': P.RealDiv(),
  291. 'desc_const': [Tensor(2048.0), Tensor(0.0)],
  292. 'desc_inputs': [],
  293. 'skip': ['backward']}),
  294. ('RealDiv', {
  295. 'block': P.RealDiv(),
  296. 'desc_inputs': [[4], Tensor(np.ones(4).astype(np.float32))],
  297. 'desc_bprop': [[4]]}),
  298. ('RealDiv_1', {
  299. 'block': P.RealDiv(),
  300. 'desc_inputs': [[512, 1024], [512, 1024]],
  301. 'desc_bprop': [[512, 1024]]}),
  302. ('FloorDiv', {
  303. 'block': P.FloorDiv(),
  304. 'desc_inputs': [Tensor(np.random.rand(4).astype(np.float16)),
  305. Tensor(np.random.rand(4).astype(np.float16))],
  306. 'skip': ['backward']}),
  307. ('FloorMod', {
  308. 'block': P.FloorMod(),
  309. 'desc_inputs': [[3, 4, 5], [2, 3, 4, 5]],
  310. 'desc_bprop': [[2, 3, 4, 5]]}),
  311. ('identity', {
  312. 'block': ops.functional.identity,
  313. 'desc_inputs': [[2, 2]],
  314. 'skip': ['backward']}),
  315. ('MatMul_1', {
  316. 'block': P.MatMul(transpose_a=False, transpose_b=False),
  317. 'desc_inputs': [[1024, 160], [160, 1024]],
  318. 'desc_bprop': [[1024, 1024]]}),
  319. ('MatMul_2', {
  320. 'block': P.MatMul(transpose_a=True, transpose_b=True),
  321. 'desc_inputs': [[160, 1024], [1024, 160]],
  322. 'desc_bprop': [[1024, 1024]]}),
  323. ('Sub', {
  324. 'block': P.Sub(),
  325. 'desc_inputs': [[3], [3]],
  326. 'desc_bprop': [[3]]}),
  327. ('TruncatedNormal', {
  328. 'block': P.TruncatedNormal(),
  329. 'desc_const': [(1, 2, 3)],
  330. 'desc_inputs': [],
  331. 'skip': ['backward'],
  332. 'add_fake_input': True}),
  333. ('Select', {
  334. 'block': P.Select(),
  335. 'desc_inputs': [Tensor(np.array([[True, False, False], [False, True, True]])),
  336. [2, 3], [2, 3]],
  337. 'desc_bprop': [[2, 3]]}),
  338. ('Rank', {
  339. 'block': P.Rank(),
  340. 'desc_inputs': [[2, 3]],
  341. 'skip': ['backward']}),
  342. ('InvertPermutation', {
  343. 'block': P.InvertPermutation(),
  344. 'desc_const': [(0, 3, 1, 2)],
  345. 'desc_inputs': [],
  346. 'skip': ['backward']}),
  347. ('Square', {
  348. 'block': P.Square(),
  349. 'desc_inputs': [[4]],
  350. 'desc_bprop': [[4]]}),
  351. ('Rsqrt', {
  352. 'block': P.Rsqrt(),
  353. 'desc_inputs': [[4]],
  354. 'desc_bprop': [[4]]}),
  355. ('Sqrt', {
  356. 'block': P.Sqrt(),
  357. 'desc_inputs': [[4]],
  358. 'desc_bprop': [[4]]}),
  359. ('RealDiv', {
  360. 'block': P.RealDiv(),
  361. 'desc_inputs': [[4, 5], [2, 3, 4, 5]],
  362. 'desc_bprop': [[2, 3, 4, 5]]}),
  363. ('Div', {
  364. 'block': P.Div(),
  365. 'desc_inputs': [[4, 5], [2, 3, 4, 5]],
  366. 'desc_bprop': [[2, 3, 4, 5]]}),
  367. ('Equal', {
  368. 'block': P.Equal(),
  369. 'desc_inputs': [[3, 4, 5], [4, 5]],
  370. 'desc_bprop': [Tensor(np.zeros((3, 4, 5), np.bool_))]}),
  371. ('NotEqual', {
  372. 'block': P.NotEqual(),
  373. 'desc_inputs': [[4, 1], [2, 3, 4, 5]],
  374. 'desc_bprop': [Tensor(np.ones((2, 3, 4, 5), np.bool_))]}),
  375. ('NotEqual_0', {
  376. 'block': P.NotEqual(),
  377. 'desc_inputs': [ 1, [2, 3, 4, 5]],
  378. 'desc_bprop': [Tensor(np.ones((2, 3, 4, 5), np.bool_))],
  379. 'skip': ['backward']}),
  380. ('Greater', {
  381. 'block': P.Greater(),
  382. 'desc_inputs': [[2, 3, 4, 1], [4, 5]],
  383. 'desc_bprop': [Tensor(np.ones((2, 3, 4, 5), np.bool_))]}),
  384. ('GreaterEqual', {
  385. 'block': P.GreaterEqual(),
  386. 'desc_inputs': [[2, 3, 4, 1], [4, 5]],
  387. 'desc_bprop': [Tensor(np.ones((2, 3, 4, 5), np.bool_))]}),
  388. ('LogicalNot', {
  389. 'block': P.LogicalNot(),
  390. 'desc_inputs': [Tensor(np.zeros((3, 4, 5), np.bool_))],
  391. 'desc_bprop': [Tensor(np.ones((3, 4, 5), np.bool_))]}),
  392. ('LogicalAnd', {
  393. 'block': P.LogicalAnd(),
  394. 'desc_inputs': [Tensor(np.zeros((2, 3, 4), np.bool_)), Tensor(np.ones((1), np.bool_))],
  395. 'desc_bprop': [Tensor(np.zeros((2, 3, 4), np.bool_))]}),
  396. ('LogicalOr', {
  397. 'block': P.LogicalOr(),
  398. 'desc_inputs': [Tensor(np.zeros((3, 4, 5), np.bool_)), Tensor(np.ones((3, 1, 1), np.bool_))],
  399. 'desc_bprop': [Tensor(np.zeros((3, 4, 5), np.bool_))]}),
  400. ('NpuAllocFloatStatus', {
  401. 'block': P.NPUAllocFloatStatus(),
  402. 'desc_inputs': [],
  403. 'add_fack_input': True,
  404. 'fack_input_type': np.float32,
  405. 'desc_bprop': [Tensor(np.zeros([8]).astype(np.float32))],
  406. 'skip': ['backward']}),
  407. ('NpuGetFloatStatus', {
  408. 'block': P.NPUGetFloatStatus(),
  409. 'desc_inputs': [Tensor(np.zeros([8]).astype(np.float32))],
  410. 'desc_bprop': [Tensor(np.zeros([8]).astype(np.float32))],
  411. 'skip': ['backward']}),
  412. ('NpuClearFloatStatus', {
  413. 'block': P.NPUClearFloatStatus(),
  414. 'desc_inputs': [Tensor(np.zeros([8]).astype(np.float32))],
  415. 'desc_bprop': [Tensor(np.zeros([8]).astype(np.float32))],
  416. 'skip': ['backward']}),
  417. ('CheckValid', {
  418. 'block': P.CheckValid(),
  419. 'desc_inputs': [[20000, 4], [3]],
  420. 'desc_bprop': [[20000]],
  421. 'skip': ['backward']}),
  422. ('NMSWithMask', {
  423. 'block': P.NMSWithMask(0.5),
  424. 'desc_inputs': [[128, 5]],
  425. 'desc_bprop': [[128, 5], [128], [128]],
  426. 'skip': ['backward']}),
  427. ('Abs', {
  428. 'block': P.Abs(),
  429. 'desc_inputs': [[4]],
  430. 'desc_bprop': [[4]]}),
  431. ('CumSum', {
  432. 'block': P.CumSum(),
  433. 'desc_const': [0],
  434. 'desc_inputs': [Tensor(np.array([[3, 4],[1, 6]]).astype(np.float16))],
  435. 'desc_bprop': [Tensor(np.array([[3, 4],[4, 10]]).astype(np.float16))]}),
  436. ('ReduceSum_3', {
  437. 'block': P.ReduceSum(),
  438. 'desc_const': [0],
  439. 'desc_inputs': [[3, 2]],
  440. 'desc_bprop': [[2]]}),
  441. ('ReduceSum_4', {
  442. 'block': P.ReduceSum(keep_dims=True),
  443. 'desc_const': [0],
  444. 'desc_inputs': [[3, 2]],
  445. 'desc_bprop': [[1, 2]]}),
  446. ('ReduceSum_5', {
  447. 'block': P.ReduceSum(keep_dims=True),
  448. 'desc_inputs': [[2, 3, 4]],
  449. 'desc_bprop': [[1, 1, 1]]}),
  450. ('ReduceSum_6', {
  451. 'block': P.ReduceSum(),
  452. 'desc_inputs': [[2, 3, 4]],
  453. 'desc_bprop': [[1]]}),
  454. ('Sum_0', {
  455. 'block': P.ReduceSum(),
  456. 'desc_const': [(1,)],
  457. 'desc_inputs': [[3, 2]],
  458. 'desc_bprop': [[3]]}),
  459. ('Sum_1', {
  460. 'block': P.ReduceSum(keep_dims=True),
  461. 'desc_const': [(1,)],
  462. 'desc_inputs': [[3, 2]],
  463. 'desc_bprop': [[3, 1]]}),
  464. ('Sum_2', {
  465. 'block': P.ReduceSum(),
  466. 'desc_const': [(0, 1)],
  467. 'desc_inputs': [[3, 2]],
  468. 'desc_bprop': [[1]]}),
  469. ('Sum_3', {
  470. 'block': P.ReduceSum(),
  471. 'desc_const': [0],
  472. 'desc_inputs': [[3, 2]],
  473. 'desc_bprop': [[2]]}),
  474. ('Sum_4', {
  475. 'block': P.ReduceSum(keep_dims=True),
  476. 'desc_const': [0],
  477. 'desc_inputs': [[3, 2]],
  478. 'desc_bprop': [[1, 2]]}),
  479. ('Sum_5', {
  480. 'block': P.ReduceSum(keep_dims=True),
  481. 'desc_const': [()],
  482. 'desc_inputs': [[2, 3, 4]],
  483. 'desc_bprop': [[1, 1, 1]]}),
  484. ('Sum_6', {
  485. 'block': P.ReduceSum(),
  486. 'desc_const': [()],
  487. 'desc_inputs': [[2, 3, 4]],
  488. 'desc_bprop': [[1]]}),
  489. ('Sign', {
  490. 'block': P.Sign(),
  491. 'desc_inputs': [[3]],
  492. 'desc_bprop': [[3]]}),
  493. ('Round', {
  494. 'block': P.Round(),
  495. 'desc_inputs': [[3]],
  496. 'desc_bprop': [[3]]}),
  497. ('Atan2', {
  498. 'block': P.Atan2(),
  499. 'desc_inputs': [Tensor(np.array([0, 1]).astype(np.float32)),
  500. Tensor(np.array([1, 1]).astype(np.float32))],
  501. 'desc_bprop': [[2]]})
  502. ]
  503. test_case_nn_ops = [
  504. ('BiasAdd', {
  505. 'block': P.BiasAdd(),
  506. 'desc_inputs': [[1, 3, 3, 3], [3]],
  507. 'desc_bprop': [[1, 3, 3, 3]]}),
  508. ('BiasAddGrad', {
  509. 'block': G.BiasAddGrad(),
  510. 'desc_inputs': [[1, 3, 3, 3]],
  511. 'skip': ['backward']}),
  512. ('Gelu', {
  513. 'block': P.Gelu(),
  514. 'desc_inputs': [[1, 3, 4, 4]],
  515. 'desc_bprop': [[1, 3, 4, 4]]}),
  516. ('GeluGrad', {
  517. 'block': G.GeluGrad(),
  518. 'desc_inputs': [[2, 2], [2, 2], [2, 2]],
  519. 'desc_bprop': [[2, 2]],
  520. 'skip': ['backward']}),
  521. ('Tanh', {
  522. 'block': P.Tanh(),
  523. 'desc_inputs': [[1, 3, 4, 4]],
  524. 'desc_bprop': [[1, 3, 4, 4]]}),
  525. ('TanhGrad', {
  526. 'block': G.TanhGrad(),
  527. 'desc_inputs': [[1, 3, 4, 4], [1, 3, 4, 4]],
  528. 'desc_bprop': [[1, 3, 4, 4]],
  529. 'skip': ['backward']}),
  530. ('ReLU', {
  531. 'block': P.ReLU(),
  532. 'desc_inputs': [[1, 3, 4, 4]],
  533. 'desc_bprop': [[1, 3, 4, 4]]}),
  534. ('ReLU6', {
  535. 'block': P.ReLU6(),
  536. 'desc_inputs': [[1, 3, 4, 4]],
  537. 'desc_bprop': [[1, 3, 4, 4]]}),
  538. ('ReLUV2', {
  539. 'block': P.ReLUV2(),
  540. 'desc_inputs': [[1, 3, 4, 4]],
  541. 'desc_bprop': [[1, 3, 4, 4], ([1, 1, 4, 4, 2], {'dtype': np.uint8})]}),
  542. ('ReLUGrad', {
  543. 'block': G.ReluGrad(),
  544. 'desc_inputs': [[1, 3, 4, 4], [1, 3, 4, 4]],
  545. 'skip': ['backward']}),
  546. ('Elu', {
  547. 'block': P.Elu(),
  548. 'desc_inputs': [[2, 3, 4]],
  549. 'desc_bprop': [[2, 3, 4]]}),
  550. ('EluGrad', {
  551. 'block': G.EluGrad(),
  552. 'desc_inputs': [[2, 3, 4], [2, 3, 4]],
  553. 'desc_bprop': [[2, 3, 4]],
  554. 'skip': ['backward']}),
  555. ('Sigmoid', {
  556. 'block': P.Sigmoid(),
  557. 'desc_inputs': [[1, 3, 4, 4]],
  558. 'desc_bprop': [[1, 3, 4, 4]]}),
  559. ('MaxPool', {
  560. 'block': P.MaxPool(ksize=(2, 2), strides=(2, 2), padding="VALID"),
  561. 'desc_inputs': [[100, 3, 28, 28]],
  562. 'desc_bprop': [[100, 3, 14, 14]]}),
  563. ('MaxPoolGrad', {
  564. 'block': G.MaxPoolGrad(ksize=(2, 2), strides=(2, 2), padding="VALID"),
  565. 'desc_inputs': [[3, 4, 6, 6], [3, 4, 3, 3], [3, 4, 3, 3]],
  566. 'desc_bprop': [[3, 4, 6, 6]],
  567. 'skip': ['backward']}),
  568. ('AvgPool', {
  569. 'block': P.AvgPool(ksize=(2, 2), strides=(2, 2), padding="VALID"),
  570. 'desc_inputs': [[100, 3, 28, 28]],
  571. 'desc_bprop': [[100, 3, 14, 14]]}),
  572. ('AvgPoolGrad', {
  573. 'block': G.AvgPoolGrad(ksize=(2, 2), strides=(2, 2), padding="VALID"),
  574. 'desc_const': [(3, 4, 6, 6)],
  575. 'const_first': True,
  576. 'desc_inputs': [[3, 4, 6, 6]],
  577. 'desc_bprop': [[3, 4, 6, 6]],
  578. 'skip': ['backward']}),
  579. ('MaxPoolWithArgmax', {
  580. 'block': P.MaxPoolWithArgmax(ksize=2, strides=2),
  581. 'desc_inputs': [[128, 32, 32, 64]],
  582. 'desc_bprop': [[128, 32, 16, 32], ([128, 32, 4, 33], {'dtype': np.uint16})]}),
  583. ('SoftmaxCrossEntropyWithLogits', {
  584. 'block': P.SoftmaxCrossEntropyWithLogits(),
  585. 'desc_inputs': [[1, 10], [1, 10]],
  586. 'desc_bprop': [[1], [1, 10]],
  587. 'skip': ['backward_exec']}),
  588. ('Flatten', {
  589. 'block': P.Flatten(),
  590. 'desc_inputs': [[128, 32, 32, 64]],
  591. 'desc_bprop': [[128 * 32 * 8 * 16]]}),
  592. ('LogSoftmax', {
  593. 'block': P.LogSoftmax(),
  594. 'desc_inputs': [[64, 2]],
  595. 'desc_bprop': [[64, 2]]}),
  596. ('LogSoftmaxGrad', {
  597. 'block': G.LogSoftmaxGrad(),
  598. 'desc_inputs': [[16, 1234], [16, 1234]],
  599. 'desc_bprop': [[64, 2]],
  600. 'skip': ['backward']}),
  601. ('LayerNorm', {
  602. 'block': P.LayerNorm(),
  603. 'desc_inputs': [[2, 16], [16], [16]],
  604. 'desc_bprop': [[2, 16], [2, 1], [2, 1]]}),
  605. ('LayerNormGrad', {
  606. 'block': G.LayerNormGrad(),
  607. 'desc_inputs': [[2, 16], [2, 16], [2, 16], [2, 16], [16]],
  608. 'desc_bprop': [[2, 16], [16], [16]],
  609. 'skip': ['backward']}),
  610. ('FusedBatchNorm', {
  611. 'block': P.FusedBatchNorm(),
  612. 'desc_inputs': [[128, 64, 32, 64], [64], [64], [64], [64]],
  613. 'desc_bprop': [[128, 64, 32, 64], [64], [64], [64], [64]],
  614. 'skip': []}),
  615. ('FusedBatchNormGrad', {
  616. 'block': G.FusedBatchNormGrad(),
  617. 'desc_inputs': [[128, 64, 32, 64], [128, 64, 32, 64], [64], [64], [64]],
  618. 'desc_bprop': [[128, 64, 32, 64], [64], [64], [64], [64]],
  619. 'skip': ['backward']}),
  620. ('BatchNorm', {
  621. 'block': P.BatchNorm(),
  622. 'desc_inputs': [[128, 64, 32, 32], [64], [64], [64], [64]],
  623. 'desc_bprop': [[128, 64, 32, 32], [64], [64], [64], [64]],
  624. 'skip': []}),
  625. ('BatchNormGrad', {
  626. 'block': G.BatchNormGrad(),
  627. 'desc_inputs': [[128, 64, 32, 32], [128, 64, 32, 32], [64], [64], [64]],
  628. 'desc_bprop': [[128, 64, 32, 32], [64], [64], [64], [64]],
  629. 'skip': ['backward']}),
  630. ('TopK', {
  631. 'block': P.TopK(),
  632. 'desc_const': [5],
  633. 'desc_inputs': [[20, 20, 10]],
  634. 'desc_bprop': [[20, 20, 5]],
  635. 'skip': ['backward']}),
  636. ('GatherV2_0', {
  637. 'block': P.GatherV2(),
  638. 'desc_const': [0],
  639. 'desc_inputs': [[3, 1, 2], Tensor(np.array([0, 1]).astype(np.int32))],
  640. 'desc_bprop': [[2, 1, 2]]}),
  641. ('GatherV2_1', {
  642. 'block': P.GatherV2(),
  643. 'desc_const': [2],
  644. 'desc_inputs': [[3, 1, 3], Tensor(np.array([0, 1]).astype(np.int32))],
  645. 'desc_bprop': [[3, 1, 2]]}),
  646. ('GatherV2_2', {
  647. 'block': P.GatherV2(),
  648. 'desc_const': [0],
  649. 'desc_inputs': [[3, 1, 3], Tensor(np.array([[0, 1], [0, 1], [0, 1]]).astype(np.int32))],
  650. 'desc_bprop': [[3, 2, 1, 3]]}),
  651. ('GatherV2_3', {
  652. 'block': P.GatherV2(),
  653. 'desc_const': [2],
  654. 'desc_inputs': [[3, 1, 3], Tensor(np.array([[0, 1], [0, 1], [0, 1]]).astype(np.int32))],
  655. 'desc_bprop': [[3, 1, 3, 2]]}),
  656. ('GatherV2_4', {
  657. 'block': P.GatherV2(),
  658. 'desc_const': [1],
  659. 'desc_inputs': [[32, 5, 1024], Tensor(np.array([3]).astype(np.int32))],
  660. 'desc_bprop': [[32, 1, 1024]]}),
  661. ('GatherV2_5', {
  662. 'block': P.GatherV2(),
  663. 'desc_const': [-1],
  664. 'desc_inputs': [[3, 1, 3], Tensor(np.array([0, 1]).astype(np.int32))],
  665. 'desc_bprop': [[3, 1, 2]]}),
  666. ('GatherV2_6', {
  667. 'block': P.GatherV2(),
  668. 'desc_const': [0],
  669. 'desc_inputs': [[1152], Tensor(np.array(10).astype(np.int32))],
  670. 'desc_bprop': [Tensor(np.array(10).astype(np.float32))]}),
  671. ('UnsortedSegmentSum', {
  672. 'block': P.UnsortedSegmentSum(),
  673. 'desc_const': [1280],
  674. 'desc_inputs': [[1280,1024], Tensor(np.ones(1280).astype(np.int32))],
  675. 'desc_bprop': [[8192,1024]],
  676. 'skip': ['backward']}),
  677. ('UnsortedSegmentSum_1', {
  678. 'block': P.UnsortedSegmentSum(),
  679. 'desc_const': [4],
  680. 'desc_inputs': [[3, 2, 1, 3], Tensor(np.array([[0, 1], [0, 1], [0, 1]]).astype(np.int32))],
  681. 'desc_bprop': [[4, 1, 3]],
  682. 'skip': ['backward']}),
  683. ('DropoutGenMask', {
  684. 'block': P.DropoutGenMask(),
  685. 'desc_const': [(2, 2), Tensor(0.5, mstype.float32)],
  686. 'desc_inputs': [],
  687. 'desc_bprop': [Tensor(np.ones(1).astype(np.int8))],
  688. 'skip': ['backward']}),
  689. ('DropoutDoMask', {
  690. 'block': P.DropoutDoMask(),
  691. 'desc_const': [Tensor(0.5)],
  692. 'desc_inputs': [[64, 12, 128, 128], Tensor(np.ones(1572864).astype(np.uint8))],
  693. 'desc_bprop': [[64, 12, 128, 128]]}),
  694. ('Dropout', {
  695. 'block': nn.Dropout(0.5),
  696. 'desc_inputs': [[64, 12, 128, 128]],
  697. 'desc_bprop': [[64, 12, 128, 128]]}),
  698. ('ReduceMean0', {
  699. 'block': P.ReduceMean(),
  700. 'desc_const': [(2,)],
  701. 'desc_inputs': [[3, 2, 2]],
  702. 'desc_bprop': [[3, 2]]}),
  703. ('ReduceMean1', {
  704. 'block': P.ReduceMean(),
  705. 'desc_const': [2],
  706. 'desc_inputs': [[3, 2, 2]],
  707. 'desc_bprop': [[3, 2]]}),
  708. ('All', {
  709. 'block': P.ReduceAll(),
  710. 'desc_const': [(1,)],
  711. 'desc_inputs': [Tensor(np.ones([3, 2]).astype(np.bool_))],
  712. 'desc_bprop': [[3]],
  713. 'skip': ['backward']}),
  714. ('DescConst', {
  715. 'block': Tensor(np.array([2], np.float32)),
  716. 'desc_inputs': [],
  717. 'desc_bprop': [[1]],
  718. 'skip': ['backward'],
  719. 'add_fake_input': True}),
  720. ('Fill', {
  721. 'block': P.Fill(),
  722. 'desc_const': [mstype.float32, (2, 3), 1.0],
  723. 'desc_inputs': [],
  724. 'desc_bprop': [[2, 3]],
  725. 'skip': ['backward'],
  726. 'add_fake_input': True}),
  727. ('OnesLike', {
  728. 'block': P.OnesLike(),
  729. 'desc_inputs': [Tensor(np.array([[0, 1], [2, 1]]).astype(np.int32))],
  730. 'desc_bprop': [Tensor(np.array([[1, 1], [1, 1]]).astype(np.int32))]
  731. }),
  732. ('ZerosLike', {
  733. 'block': P.ZerosLike(),
  734. 'desc_inputs': [Tensor(np.array([[0, 1], [2, 1]]).astype(np.int32))],
  735. 'desc_bprop': [Tensor(np.array([[1, 1], [1, 1]]).astype(np.int32))]
  736. }),
  737. ('Softmax', {
  738. 'block': P.Softmax(),
  739. 'desc_inputs': [[5, 5]],
  740. 'desc_bprop': [[5, 5]]}),
  741. ('DepthwiseConv2dNative_1', {
  742. 'block': P.DepthwiseConv2dNative(3, (3, 3), pad_mode="pad", pad=1, stride=2),
  743. 'desc_inputs': [[10, 32, 32, 32], [1, 32, 3, 3]],
  744. 'desc_bprop': [[10, 32, 16, 16]]}),
  745. ('DepthwiseConv2dNative_2', {
  746. 'block': P.DepthwiseConv2dNative(1, (3, 3), pad_mode="same", pad=0, stride=1),
  747. 'desc_inputs': [[2592, 2048, 4, 4], [1, 2048, 3, 3]],
  748. 'desc_bprop': [[2592, 2048, 4, 4]]}),
  749. ('SigmoidCrossEntropyWithLogits', {
  750. 'block': P.SigmoidCrossEntropyWithLogits(),
  751. 'desc_inputs': [[128, 10], [128, 10]],
  752. 'desc_bprop': [[128, 10]]}),
  753. ('Pad', {
  754. 'block': P.Pad(((1, 2), (2, 3))),
  755. 'desc_inputs': [[7, 7]],
  756. 'desc_bprop': [[10, 12]]}),
  757. ('BinaryCrossEntropy', {
  758. 'block': P.BinaryCrossEntropy(),
  759. 'desc_inputs': [[1, 2, 3], [1, 2, 3], [1, 2, 3]],
  760. 'desc_bprop': []}),
  761. ('SparseApplyAdagrad', {
  762. 'block': P.SparseApplyAdagrad(0.5),
  763. 'desc_inputs': [[3, 3], [3, 3], [3, 3], Tensor(np.ones((3,), np.int32))],
  764. 'desc_bprop': [[3, 3], [3, 3]],
  765. 'skip': ['backward']}),
  766. ('Flatten_1', {
  767. 'block': NetForFlatten(),
  768. 'desc_inputs': [Tensor(np.ones([2, 3, 4]).astype(np.int32)), Tensor(np.ones([2, 12]).astype(np.int32))],
  769. 'desc_bprop': [Tensor(np.ones([2, 12]).astype(np.int32))],
  770. 'skip': ['backward']}),
  771. ('Flatten_2', {
  772. 'block': NetForFlatten(),
  773. 'desc_inputs': [Tensor(np.ones([8]).astype(np.int32)), Tensor(np.ones([8, 3]).astype(np.int32))],
  774. 'desc_bprop': [Tensor(np.ones([8, 3]).astype(np.int32))],
  775. 'skip': ['backward']}),
  776. ('ArgmaxNet', {
  777. 'block': ArgmaxNet(),
  778. 'desc_inputs': [Tensor(np.array([[128, 32, 32, 64],[128, 32, 32, 64]]).astype(np.float16))],
  779. 'desc_bprop': [Tensor(np.array([[128, 32, 32, 64],[128, 32, 32, 64]]).astype(np.float16))],
  780. 'skip': ['backward']}),
  781. ('ArgminNet', {
  782. 'block': ArgminNet(),
  783. 'desc_inputs': [Tensor(np.array([[128, 32, 32, 64],[128, 32, 32, 64]]).astype(np.float16))],
  784. 'desc_bprop': [Tensor(np.array([[128, 32, 32, 64],[128, 32, 32, 64]]).astype(np.float16))],
  785. 'skip': ['backward']}),
  786. ('CumSumNet', {
  787. 'block': CumSumNet(),
  788. 'desc_const': [0],
  789. 'desc_inputs': [Tensor(np.array([[3, 4, 6, 10],[1, 6, 7, 9],[4, 3, 8, 7],[1, 3, 7, 9]]).astype(np.float16))],
  790. 'desc_bprop': [Tensor(np.array([[3, 4, 6, 10],[1, 6, 7, 9],[4, 3, 8, 7],[1, 3, 7, 9]]).astype(np.float16))]}),
  791. ('OneHot', {
  792. 'block': P.OneHot(),
  793. 'desc_const': [3, Tensor(1.0, mstype.float32), Tensor(0.0, mstype.float32)],
  794. 'desc_inputs': [Tensor(np.array([64]).astype(np.int32))],
  795. 'desc_bprop': [[1, 3]]}),
  796. ('ReduceProd_0', {
  797. 'block': P.ReduceProd(),
  798. 'desc_const': [0],
  799. 'desc_inputs': [[3, 2]],
  800. 'desc_bprop': [[2]]}),
  801. ('ReduceProd_1', {
  802. 'block': P.ReduceProd(keep_dims=True),
  803. 'desc_const': [0],
  804. 'desc_inputs': [[3, 2]],
  805. 'desc_bprop': [[1, 2]]}),
  806. ('CumProd', {
  807. 'block': P.CumProd(),
  808. 'desc_const': [0],
  809. 'desc_inputs': [[3, 2]],
  810. 'desc_bprop': [[3, 2]]}),
  811. ('ApplyFtrl', {
  812. 'block': P.ApplyFtrl(),
  813. 'desc_const': [0.001, 0.0, 0.0, -0.5],
  814. 'desc_inputs': [[3, 3], [3, 3], [3, 3], [3, 3]],
  815. 'desc_bprop': [3, 3],
  816. 'skip': ['backward']}),
  817. ('ApplyRMSProp', {
  818. 'block': P.ApplyRMSProp(),
  819. 'desc_const': [0.9, 0.0, 1e-10, 0.001],
  820. 'desc_inputs': [[3, 3], [3, 3], [3, 3], [3, 3]],
  821. 'desc_bprop': [3, 3],
  822. 'skip': ['backward']}),
  823. ('ApplyCenteredRMSProp', {
  824. 'block': P.ApplyCenteredRMSProp(),
  825. 'desc_const': [0.9, 0.0, 1e-10, 0.001],
  826. 'desc_inputs': [[3, 3], [3, 3], [3, 3], [3, 3], [3, 3]],
  827. 'desc_bprop': [3, 3],
  828. 'skip': ['backward']}),
  829. ('L2Loss_1', {
  830. 'block': P.L2Loss(),
  831. 'desc_inputs': [Tensor(np.array([1, 2, 3, 4]), mstype.float16)],
  832. 'desc_bprop': []}),
  833. ('L2Loss_2', {
  834. 'block': P.L2Loss(),
  835. 'desc_inputs': [Tensor(np.array([[1, 1], [2, 2], [3, 3], [4, 4]]), mstype.float16)],
  836. 'desc_bprop': []}),
  837. ]
  838. test_case_array_ops = [
  839. ('SpaceToDepth', {
  840. 'block': P.SpaceToDepth(2),
  841. 'desc_inputs': [[1, 3, 2, 2]],
  842. 'desc_bprop': [[1, 12, 1, 1]]}),
  843. ('DepthToSpace', {
  844. 'block': P.DepthToSpace(2),
  845. 'desc_inputs': [[1, 12, 1, 1]],
  846. 'desc_bprop': [[1, 3, 2, 2]]}),
  847. ('Split', {
  848. 'block': P.Split(1, 2),
  849. 'desc_inputs': [Tensor(np.array([[1, 1, 1, 1], [2, 2, 2, 2]]))],
  850. 'skip': ['backward']}),
  851. ('Argmax', {
  852. 'block': P.Argmax(),
  853. 'desc_inputs': [[128, 32, 32, 64]],
  854. 'desc_bprop': [0],
  855. 'skip': ['backward']}),
  856. ('Argmin', {
  857. 'block': P.Argmin(),
  858. 'desc_inputs': [[128, 32, 32, 64]],
  859. 'desc_bprop': [1],
  860. 'skip': ['backward']}),
  861. ('ArgMaxWithValue', {
  862. 'block': P.ArgMaxWithValue(),
  863. 'desc_inputs': [[128, 32, 32, 64]],
  864. 'desc_bprop': [[1], [1]],
  865. 'skip': ['backward']}),
  866. ('ArgMinWithValue', {
  867. 'block': P.ArgMinWithValue(),
  868. 'desc_inputs': [[128, 32, 32, 64]],
  869. 'desc_bprop': [[1], [1]],
  870. 'skip': ['backward']}),
  871. ('Transpose_dim3', {
  872. 'block': P.Transpose(),
  873. 'desc_const': [(0, 2, 1)],
  874. 'desc_inputs': [[1, 2, 3]],
  875. 'desc_bprop': [[1, 3, 2]]}),
  876. ('Transpose_dim4', {
  877. 'block': P.Transpose(),
  878. 'desc_const': [(0, 1, 2, 3)],
  879. 'desc_inputs': [[1, 2, 3, 4]],
  880. 'desc_bprop': [[1, 2, 4, 3]]}),
  881. ('AddN', {
  882. 'block': NetForTupleInput(P.AddN()),
  883. 'desc_inputs': [[2, 3, 3, 5], [2, 3, 3, 5]],
  884. 'desc_bprop': [[2, 3, 3, 5]],
  885. 'skip': ['backward']}),
  886. ('Shape', {
  887. 'block': P.Shape(),
  888. 'desc_inputs': [[3, 3, 2, 2]],
  889. 'skip': ['backward']}),
  890. ('Reshape', {
  891. 'block': P.Reshape(),
  892. 'desc_const': [(64,)],
  893. 'desc_inputs': [[64, 1]],
  894. 'desc_bprop': [[64]]}),
  895. ('Cast', {
  896. 'block': P.Cast(),
  897. 'desc_const': [mstype.int32],
  898. 'desc_inputs': [[2, 3, 4, 5]],
  899. 'desc_bprop': [Tensor(np.ones((2, 3, 4, 5)).astype(np.int32))]}),
  900. ('ExpandDims', {
  901. 'block': P.ExpandDims(),
  902. 'desc_const': [0],
  903. 'desc_inputs': [[2, 2]],
  904. 'desc_bprop': [[1, 2, 2]]}),
  905. ('ExpandDims_1', {
  906. 'block': P.ExpandDims(),
  907. 'desc_const': [-1],
  908. 'desc_inputs': [[2, 2]],
  909. 'desc_bprop': [[2, 2, 1]]}),
  910. ('Squeeze', {
  911. 'block': P.Squeeze(2),
  912. 'desc_inputs': [[3, 2, 1]],
  913. 'desc_bprop': [[3, 2]]}),
  914. ('Squeeze_0', {
  915. 'block': P.Squeeze(),
  916. 'desc_inputs': [[3, 1, 2, 1]],
  917. 'desc_bprop': [[3, 2]]}),
  918. ('Squeeze_1', {
  919. 'block': P.Squeeze(),
  920. 'desc_inputs': [[1, 1, 1, 1]],
  921. 'desc_bprop': [1.0],
  922. 'skip': ['backward']}),
  923. ('Squeeze_2', {
  924. 'block': P.Squeeze((2, 3)),
  925. 'desc_inputs': [[3, 2, 1, 1]],
  926. 'desc_bprop': [[3, 2]]}),
  927. ('Size', {
  928. 'block': P.Size(),
  929. 'desc_inputs': [[2, 3, 5]],
  930. 'skip': ['backward']}),
  931. ('Tile_0', {
  932. 'block': P.Tile(),
  933. 'desc_const': [(1, 2)],
  934. 'desc_inputs': [[64, 1]],
  935. 'desc_bprop': [[64, 2]]}),
  936. ('Tile_1', {
  937. 'block': P.Tile(),
  938. 'desc_const': [(1, 1)],
  939. 'desc_inputs': [[64, 1]],
  940. 'desc_bprop': [[64, 1]]}),
  941. ('Tile_2', {
  942. 'block': P.Tile(),
  943. 'desc_const': [(2, 1, 1, 2)],
  944. 'desc_inputs': [[2, 2, 2]],
  945. 'desc_bprop': [[2, 2, 2, 4]]}),
  946. ('ConcatV2_0', {
  947. 'block': P.Concat(),
  948. 'desc_inputs': [
  949. (Tensor(np.array([[0, 1], [2, 1]]).astype(np.int32)),
  950. Tensor(np.array([[0, 1], [2, 1]]).astype(np.int32)))],
  951. 'desc_bprop': [([4, 2], {'dtype': np.int32})]}),
  952. ('ConcatV2_1', {
  953. 'block': P.Concat(axis=2),
  954. 'desc_inputs': [(Tensor(np.array([[[0, 1, 2]], [[2, 1, 2]]]).astype(np.int32)),
  955. Tensor(np.array([[[0, 1]], [[2, 1]]]).astype(np.int32)))],
  956. 'desc_bprop': [([2, 1, 5], {'dtype': np.int32})]}),
  957. ('ConcatV2_2', {
  958. 'block': NetForConcat(),
  959. 'desc_inputs': [[2, 2]],
  960. 'desc_bprop': [[4, 2]]}),
  961. ('ConcatV2_3', {
  962. 'block': NetForConcat1(),
  963. 'desc_inputs': [[2, 2], [2, 2]],
  964. 'desc_bprop': [[4, 2]]}),
  965. ('ConcatV2_4', {
  966. 'block': P.Concat(axis=0),
  967. 'desc_inputs': [
  968. (Tensor(np.ones((3, 2, 3), np.float32)),
  969. Tensor(np.ones((5, 2, 3), np.float32)),
  970. Tensor(np.ones((6, 2, 3), np.float32)))],
  971. 'desc_bprop': [[14, 2, 3]]}),
  972. ('ConcatV2_5', {
  973. 'block': P.Concat(axis=-1),
  974. 'desc_inputs': [(Tensor(np.array([1], np.float32)),
  975. Tensor(np.array([1], np.float32)),
  976. Tensor(np.array([1], np.float32)))],
  977. 'desc_bprop': [[3,]]}),
  978. ('Pack_0', {
  979. 'block': NetForPackInput(P.Pack()),
  980. 'desc_inputs':[[2, 2], [2, 2], [2, 2]],
  981. 'desc_bprop':[[3, 2, 2]],
  982. }),
  983. ('Pack_1', {
  984. 'block': NetForPackInput(P.Pack(axis=-2)),
  985. 'desc_inputs':[[3, 2, 3], [3, 2, 3], [3, 2, 3]],
  986. 'desc_bprop':[[3, 2, 3, 3]],
  987. }),
  988. ('Pack_2', {
  989. 'block': NetForPackInput(P.Pack()),
  990. 'desc_inputs':[[128, 128], [128, 128]],
  991. 'desc_bprop':[[2, 128, 128]],
  992. }),
  993. ('Unpack_0', {
  994. 'block': NetForUnpackInput(P.Unpack(axis=0)),
  995. 'desc_inputs':[[2, 4]],
  996. 'desc_bprop':[[4], [4]],
  997. }),
  998. ('Unpack_1', {
  999. 'block': NetForUnpackInput(P.Unpack(axis=-1)),
  1000. 'desc_inputs':[Tensor(np.array([[1, 1, 1]], np.float32))],
  1001. 'desc_bprop':[[1], [1], [1]],
  1002. }),
  1003. ('Diag_1', {
  1004. 'block': P.Diag(),
  1005. 'desc_inputs': [[4]],
  1006. 'desc_bprop': [[4, 4]],
  1007. }),
  1008. ('Diag_2', {
  1009. 'block': P.Diag(),
  1010. 'desc_inputs': [[4, 4]],
  1011. 'desc_bprop': [[4, 4, 4, 4]],
  1012. }),
  1013. ('DiagPart_1', {
  1014. 'block': P.DiagPart(),
  1015. 'desc_inputs': [[4, 4]],
  1016. 'desc_bprop': [[4]],
  1017. }),
  1018. ('DiagPart_2', {
  1019. 'block': P.DiagPart(),
  1020. 'desc_inputs': [[4, 4, 4, 4]],
  1021. 'desc_bprop': [[4, 4]],
  1022. }),
  1023. ('SpaceToBatch_1', {
  1024. 'block': P.SpaceToBatch(2, [[0, 0], [0, 0]]),
  1025. 'desc_inputs': [[1, 3, 2, 2]],
  1026. 'desc_bprop': [[4, 3, 1, 1]],
  1027. }),
  1028. ('SpaceToBatch_2', {
  1029. 'block': P.SpaceToBatch(2, [[1, 1], [0, 4]]),
  1030. 'desc_inputs': [[1, 3, 2, 2]],
  1031. 'desc_bprop': [[4, 3, 2, 3]],
  1032. }),
  1033. ('BatchToSpace_1', {
  1034. 'block': P.BatchToSpace(2, [[0, 0], [0, 0]]),
  1035. 'desc_inputs': [[4, 3, 1, 1]],
  1036. 'desc_bprop': [[1, 3, 2, 2]],
  1037. }),
  1038. ('BatchToSpace_2', {
  1039. 'block': P.BatchToSpace(2, [[0, 0], [0, 1]]),
  1040. 'desc_inputs': [[4, 3, 1, 1]],
  1041. 'desc_bprop': [[1, 3, 2, 1]],
  1042. }),
  1043. ]
  1044. test_case_other_ops = [
  1045. ('ScalarLog', {
  1046. 'block': F.scalar_log,
  1047. 'desc_const': [0.0],
  1048. 'desc_inputs': [],
  1049. 'desc_bprop': [1],
  1050. 'skip': ['backward']}),
  1051. ('BoundingBoxEncode', {
  1052. 'block': P.BoundingBoxEncode(means=(0.0, 0.0, 0.0, 0.0), stds=(1.0, 1.0, 1.0, 1.0)),
  1053. 'desc_inputs': [[256, 4], [256, 4]],
  1054. 'desc_bprop': [[256, 4]],
  1055. 'skip': ['backward']}),
  1056. ('BoundingBoxDecode', {
  1057. 'block': P.BoundingBoxDecode(means=(0.0, 0.0, 0.0, 0.0), stds=(1.0, 1.0, 1.0, 1.0), max_shape=(768, 1280)),
  1058. 'desc_inputs': [[256, 4], [256, 4]],
  1059. 'desc_bprop': [[256, 4]],
  1060. 'skip': ['backward']}),
  1061. ('GatherNd', {
  1062. 'block': P.GatherNd(),
  1063. 'desc_inputs': (Tensor(np.ones((1, 3, 6, 6), np.float32)),
  1064. Tensor(np.ones((2, 4), np.int32))),
  1065. 'desc_bprop': [[2]]}),
  1066. ('ScatterNd', {
  1067. 'block': P.ScatterNd(),
  1068. 'desc_const': [(3, 3)],
  1069. 'desc_inputs': (Tensor(np.ones((2, 2), np.int32)),
  1070. Tensor(np.ones((2,), np.int32))),
  1071. 'desc_bprop': [([3, 3], {'dtype': np.int32})]}),
  1072. ('SmoothL1Loss', {
  1073. 'block': P.SmoothL1Loss(),
  1074. 'desc_inputs': [[256, 4], [256, 4]],
  1075. 'desc_bprop': [[256, 4]]}),
  1076. ('IOU', {
  1077. 'block': P.IOU(),
  1078. 'desc_inputs': [Tensor(np.ones((256, 4), np.float16)), Tensor(np.ones((128, 4), np.float16))],
  1079. 'desc_bprop': [[128, 256]]}),
  1080. ('Summary', {
  1081. 'block': SummaryNet(),
  1082. 'desc_inputs': [Tensor(np.array([1.1]).astype(np.float32)),
  1083. Tensor(np.array([1.2]).astype(np.float32))],
  1084. 'skip': ['backward']}),
  1085. ('ConfusionMulGrad_1', {
  1086. 'block': P.ConfusionMulGrad(axis = [0], keep_dims = False),
  1087. 'desc_inputs': [[3, 2], [3, 2], [3, 2]],
  1088. 'desc_bprop': [[3, 2], [2]],
  1089. 'skip': ['backward']}),
  1090. ('ConfusionMulGrad_2', {
  1091. 'block': P.ConfusionMulGrad(axis = [0], keep_dims = True),
  1092. 'desc_inputs': [[3, 2], [3, 2], [3, 2]],
  1093. 'desc_bprop': [[3, 2], [1, 2]],
  1094. 'skip': ['backward']}),
  1095. ('ConfusionMulGrad_3', {
  1096. 'block': P.ConfusionMulGrad(axis = (), keep_dims = True),
  1097. 'desc_inputs': [[2, 3, 4], [2, 3, 4], [2, 3, 4]],
  1098. 'desc_bprop': [[2, 3, 4], [1, 1, 1]],
  1099. 'skip': ['backward']}),
  1100. ('HistogramSummary', {
  1101. 'block': HistogramSummaryNet(),
  1102. 'desc_inputs': [Tensor(np.array([1.1]).astype(np.float32)),
  1103. Tensor(np.array([1.2]).astype(np.float32))],
  1104. 'skip': ['backward']}),
  1105. ]
  1106. test_case_lists = [test_case_nn_ops, test_case_math_ops, test_case_array_ops, test_case_other_ops]
  1107. test_case = functools.reduce(lambda x, y: x + y, test_case_lists)
  1108. # use -k to select certain testcast
  1109. # pytest tests/python/ops/test_ops.py::test_backward -k LayerNorm
  1110. test_exec_case = test_case
  1111. test_backward_exec_case = filter(lambda x: 'skip' not in x[1] or
  1112. 'backward' not in x[1]['skip'], test_case)
  1113. import mindspore.context as context
  1114. @non_graph_engine
  1115. @mindspore_test(pipeline_for_compile_forward_ge_graph_for_case_by_case_config)
  1116. def test_exec():
  1117. context.set_context(mode=context.GRAPH_MODE, save_graphs=True)
  1118. return test_exec_case
  1119. @mindspore_test(pipeline_for_compile_grad_ge_graph_for_case_by_case_config)
  1120. def test_backward_exec():
  1121. context.set_context(mode=context.GRAPH_MODE)
  1122. return test_backward_exec_case
  1123. raise_set = [
  1124. ('Cast_Error', {
  1125. 'block': (P.Cast(), {'exception': TypeError}),
  1126. 'desc_const': [mstype.int32],
  1127. 'desc_inputs': ['wrong input'],
  1128. 'desc_bprop': [Tensor(np.ones((2, 3, 3, 5)).astype(np.int32))]}),
  1129. ('Maximum_Error', {
  1130. 'block': (P.Maximum(), {'exception': TypeError}),
  1131. 'desc_const': [(1, 2, 3)],
  1132. 'desc_inputs': [[2, 3, 3, 5]],
  1133. 'desc_bprop': [[2, 3, 3, 5]]}),
  1134. ('Shape_error', {
  1135. 'block': (P.Shape(), {'exception': TypeError}),
  1136. 'desc_inputs': [(64, 1)],
  1137. 'desc_bprop': [[64]]}),
  1138. ('Flatten_Error', {
  1139. 'block': (NetForFlatten0D(), {'exception': ValueError}),
  1140. 'desc_inputs': [Tensor(np.array(0).astype(np.int32))],
  1141. 'desc_bprop': [Tensor(np.array(0).astype(np.int32))]}),
  1142. ('ScatterNdUpdate', {
  1143. 'block': (P.ScatterNdUpdate(), {'exception': TypeError}),
  1144. 'desc_inputs': (Tensor(np.ones((2, 3), np.float32)),
  1145. Tensor(np.ones((2, 2), np.int32)),
  1146. Tensor(np.ones((2,), np.float32))),
  1147. 'desc_bprop': [[2, 3]]}),
  1148. ('Pack', {
  1149. 'block': (NetForPackInput(P.Pack()), {'exception': ValueError}),
  1150. 'desc_inputs':[[2, 2]],
  1151. 'desc_bprop':[[1, 2, 2]]}),
  1152. ('PReLU', {
  1153. 'block': (P.PReLU(), {'exception': ValueError}),
  1154. 'desc_inputs':[[2], [1]],
  1155. 'desc_bprop':[[1]]}),
  1156. ]
  1157. @mindspore_test(pipeline_for_compile_forward_ge_graph_for_case_by_case_config_exception)
  1158. def test_check_exception():
  1159. return raise_set