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 47 kB

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