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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  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], [64]],
  628. 'desc_bprop': [[128, 64, 32, 32], [64], [64], [64], [64]],
  629. 'skip': ['backward']}),
  630. ('ApplyMomentum', {
  631. 'block': P.ApplyMomentum(),
  632. 'desc_inputs': [[128, 32, 32, 64], [128, 32, 32, 64],
  633. [32, 32, 64], [32, 32, 64], [32, 32, 64]],
  634. 'desc_bprop': [[128, 32, 32, 64]],
  635. 'skip': ['backward']}),
  636. ('TopK', {
  637. 'block': P.TopK(),
  638. 'desc_const': [5],
  639. 'desc_inputs': [[20, 20, 10]],
  640. 'desc_bprop': [[20, 20, 5]],
  641. 'skip': ['backward']}),
  642. ('GatherV2_0', {
  643. 'block': P.GatherV2(),
  644. 'desc_const': [0],
  645. 'desc_inputs': [[3, 1, 2], Tensor(np.array([0, 1]).astype(np.int32))],
  646. 'desc_bprop': [[2, 1, 2]]}),
  647. ('GatherV2_1', {
  648. 'block': P.GatherV2(),
  649. 'desc_const': [2],
  650. 'desc_inputs': [[3, 1, 3], Tensor(np.array([0, 1]).astype(np.int32))],
  651. 'desc_bprop': [[3, 1, 2]]}),
  652. ('GatherV2_2', {
  653. 'block': P.GatherV2(),
  654. 'desc_const': [0],
  655. 'desc_inputs': [[3, 1, 3], Tensor(np.array([[0, 1], [0, 1], [0, 1]]).astype(np.int32))],
  656. 'desc_bprop': [[3, 2, 1, 3]]}),
  657. ('GatherV2_3', {
  658. 'block': P.GatherV2(),
  659. 'desc_const': [2],
  660. 'desc_inputs': [[3, 1, 3], Tensor(np.array([[0, 1], [0, 1], [0, 1]]).astype(np.int32))],
  661. 'desc_bprop': [[3, 1, 3, 2]]}),
  662. ('GatherV2_4', {
  663. 'block': P.GatherV2(),
  664. 'desc_const': [1],
  665. 'desc_inputs': [[32, 5, 1024], Tensor(np.array([3]).astype(np.int32))],
  666. 'desc_bprop': [[32, 1, 1024]]}),
  667. ('GatherV2_5', {
  668. 'block': P.GatherV2(),
  669. 'desc_const': [-1],
  670. 'desc_inputs': [[3, 1, 3], Tensor(np.array([0, 1]).astype(np.int32))],
  671. 'desc_bprop': [[3, 1, 2]]}),
  672. ('GatherV2_6', {
  673. 'block': P.GatherV2(),
  674. 'desc_const': [0],
  675. 'desc_inputs': [[1152], Tensor(np.array(10).astype(np.int32))],
  676. 'desc_bprop': [Tensor(np.array(10).astype(np.float32))]}),
  677. ('UnsortedSegmentSum', {
  678. 'block': P.UnsortedSegmentSum(),
  679. 'desc_const': [1280],
  680. 'desc_inputs': [[1280,1024], Tensor(np.ones(1280).astype(np.int32))],
  681. 'desc_bprop': [[8192,1024]],
  682. 'skip': ['backward']}),
  683. ('UnsortedSegmentSum_1', {
  684. 'block': P.UnsortedSegmentSum(),
  685. 'desc_const': [4],
  686. 'desc_inputs': [[3, 2, 1, 3], Tensor(np.array([[0, 1], [0, 1], [0, 1]]).astype(np.int32))],
  687. 'desc_bprop': [[4, 1, 3]],
  688. 'skip': ['backward']}),
  689. ('DropoutGenMask', {
  690. 'block': P.DropoutGenMask(),
  691. 'desc_const': [(2, 2), Tensor(0.5, mstype.float32)],
  692. 'desc_inputs': [],
  693. 'desc_bprop': [Tensor(np.ones(1).astype(np.int8))],
  694. 'skip': ['backward']}),
  695. ('DropoutDoMask', {
  696. 'block': P.DropoutDoMask(),
  697. 'desc_const': [Tensor(0.5)],
  698. 'desc_inputs': [[64, 12, 128, 128], Tensor(np.ones(1572864).astype(np.uint8))],
  699. 'desc_bprop': [[64, 12, 128, 128]]}),
  700. ('Dropout', {
  701. 'block': nn.Dropout(0.5),
  702. 'desc_inputs': [[64, 12, 128, 128]],
  703. 'desc_bprop': [[64, 12, 128, 128]]}),
  704. ('ReduceMean0', {
  705. 'block': P.ReduceMean(),
  706. 'desc_const': [(2,)],
  707. 'desc_inputs': [[3, 2, 2]],
  708. 'desc_bprop': [[3, 2]]}),
  709. ('ReduceMean1', {
  710. 'block': P.ReduceMean(),
  711. 'desc_const': [2],
  712. 'desc_inputs': [[3, 2, 2]],
  713. 'desc_bprop': [[3, 2]]}),
  714. ('All', {
  715. 'block': P.ReduceAll(),
  716. 'desc_const': [(1,)],
  717. 'desc_inputs': [Tensor(np.ones([3, 2]).astype(np.bool_))],
  718. 'desc_bprop': [[3]],
  719. 'skip': ['backward']}),
  720. ('DescConst', {
  721. 'block': Tensor(np.array([2], np.float32)),
  722. 'desc_inputs': [],
  723. 'desc_bprop': [[1]],
  724. 'skip': ['backward'],
  725. 'add_fake_input': True}),
  726. ('Fill', {
  727. 'block': P.Fill(),
  728. 'desc_const': [mstype.float32, (2, 3), 1.0],
  729. 'desc_inputs': [],
  730. 'desc_bprop': [[2, 3]],
  731. 'skip': ['backward'],
  732. 'add_fake_input': True}),
  733. ('OnesLike', {
  734. 'block': P.OnesLike(),
  735. 'desc_inputs': [Tensor(np.array([[0, 1], [2, 1]]).astype(np.int32))],
  736. 'desc_bprop': [Tensor(np.array([[1, 1], [1, 1]]).astype(np.int32))]
  737. }),
  738. ('ZerosLike', {
  739. 'block': P.ZerosLike(),
  740. 'desc_inputs': [Tensor(np.array([[0, 1], [2, 1]]).astype(np.int32))],
  741. 'desc_bprop': [Tensor(np.array([[1, 1], [1, 1]]).astype(np.int32))]
  742. }),
  743. ('Softmax', {
  744. 'block': P.Softmax(),
  745. 'desc_inputs': [[5, 5]],
  746. 'desc_bprop': [[5, 5]]}),
  747. ('DepthwiseConv2dNative_1', {
  748. 'block': P.DepthwiseConv2dNative(3, (3, 3), pad_mode="pad", pad=1, stride=2),
  749. 'desc_inputs': [[10, 32, 32, 32], [1, 32, 3, 3]],
  750. 'desc_bprop': [[10, 32, 16, 16]]}),
  751. ('DepthwiseConv2dNative_2', {
  752. 'block': P.DepthwiseConv2dNative(1, (3, 3), pad_mode="same", pad=0, stride=1),
  753. 'desc_inputs': [[2592, 2048, 4, 4], [1, 2048, 3, 3]],
  754. 'desc_bprop': [[2592, 2048, 4, 4]]}),
  755. ('SigmoidCrossEntropyWithLogits', {
  756. 'block': P.SigmoidCrossEntropyWithLogits(),
  757. 'desc_inputs': [[128, 10], [128, 10]],
  758. 'desc_bprop': [[128, 10]]}),
  759. ('Pad', {
  760. 'block': P.Pad(((1, 2), (2, 3))),
  761. 'desc_inputs': [[7, 7]],
  762. 'desc_bprop': [[10, 12]]}),
  763. ('BinaryCrossEntropy', {
  764. 'block': P.BinaryCrossEntropy(),
  765. 'desc_inputs': [[1, 2, 3], [1, 2, 3], [1, 2, 3]],
  766. 'desc_bprop': []}),
  767. ('SparseApplyAdagrad', {
  768. 'block': P.SparseApplyAdagrad(0.5),
  769. 'desc_inputs': [[3, 3], [3, 3], [3, 3], Tensor(np.ones((3,), np.int32))],
  770. 'desc_bprop': [3, 3],
  771. 'skip': ['backward']}),
  772. ('Flatten_1', {
  773. 'block': NetForFlatten(),
  774. 'desc_inputs': [Tensor(np.ones([2, 3, 4]).astype(np.int32)), Tensor(np.ones([2, 12]).astype(np.int32))],
  775. 'desc_bprop': [Tensor(np.ones([2, 12]).astype(np.int32))],
  776. 'skip': ['backward']}),
  777. ('Flatten_2', {
  778. 'block': NetForFlatten(),
  779. 'desc_inputs': [Tensor(np.ones([8]).astype(np.int32)), Tensor(np.ones([8, 3]).astype(np.int32))],
  780. 'desc_bprop': [Tensor(np.ones([8, 3]).astype(np.int32))],
  781. 'skip': ['backward']}),
  782. ('ArgmaxNet', {
  783. 'block': ArgmaxNet(),
  784. 'desc_inputs': [Tensor(np.array([[128, 32, 32, 64],[128, 32, 32, 64]]).astype(np.float16))],
  785. 'desc_bprop': [Tensor(np.array([[128, 32, 32, 64],[128, 32, 32, 64]]).astype(np.float16))],
  786. 'skip': ['backward']}),
  787. ('ArgminNet', {
  788. 'block': ArgminNet(),
  789. 'desc_inputs': [Tensor(np.array([[128, 32, 32, 64],[128, 32, 32, 64]]).astype(np.float16))],
  790. 'desc_bprop': [Tensor(np.array([[128, 32, 32, 64],[128, 32, 32, 64]]).astype(np.float16))],
  791. 'skip': ['backward']}),
  792. ('CumSumNet', {
  793. 'block': CumSumNet(),
  794. 'desc_const': [0],
  795. 'desc_inputs': [Tensor(np.array([[3, 4, 6, 10],[1, 6, 7, 9],[4, 3, 8, 7],[1, 3, 7, 9]]).astype(np.float16))],
  796. 'desc_bprop': [Tensor(np.array([[3, 4, 6, 10],[1, 6, 7, 9],[4, 3, 8, 7],[1, 3, 7, 9]]).astype(np.float16))]}),
  797. ('OneHot', {
  798. 'block': P.OneHot(),
  799. 'desc_const': [3, Tensor(1.0, mstype.float32), Tensor(0.0, mstype.float32)],
  800. 'desc_inputs': [Tensor(np.array([64]).astype(np.int32))],
  801. 'desc_bprop': [[1, 3]]}),
  802. ('ReduceProd_0', {
  803. 'block': P.ReduceProd(),
  804. 'desc_const': [0],
  805. 'desc_inputs': [[3, 2]],
  806. 'desc_bprop': [[2]]}),
  807. ('ReduceProd_1', {
  808. 'block': P.ReduceProd(keep_dims=True),
  809. 'desc_const': [0],
  810. 'desc_inputs': [[3, 2]],
  811. 'desc_bprop': [[1, 2]]}),
  812. ('CumProd', {
  813. 'block': P.CumProd(),
  814. 'desc_const': [0],
  815. 'desc_inputs': [[3, 2]],
  816. 'desc_bprop': [[3, 2]]}),
  817. ('ApplyFtrl', {
  818. 'block': P.ApplyFtrl(),
  819. 'desc_const': [0.001, 0.0, 0.0, -0.5],
  820. 'desc_inputs': [[3, 3], [3, 3], [3, 3], [3, 3]],
  821. 'desc_bprop': [3, 3],
  822. 'skip': ['backward']}),
  823. ('ApplyRMSProp', {
  824. 'block': P.ApplyRMSProp(),
  825. 'desc_const': [0.9, 0.0, 1e-10, 0.001],
  826. 'desc_inputs': [[3, 3], [3, 3], [3, 3], [3, 3]],
  827. 'desc_bprop': [3, 3],
  828. 'skip': ['backward']}),
  829. ('ApplyCenteredRMSProp', {
  830. 'block': P.ApplyCenteredRMSProp(),
  831. 'desc_const': [0.9, 0.0, 1e-10, 0.001],
  832. 'desc_inputs': [[3, 3], [3, 3], [3, 3], [3, 3], [3, 3]],
  833. 'desc_bprop': [3, 3],
  834. 'skip': ['backward']}),
  835. ('L2Loss_1', {
  836. 'block': P.L2Loss(),
  837. 'desc_inputs': [Tensor(np.array([1, 2, 3, 4]), mstype.float16)],
  838. 'desc_bprop': []}),
  839. ('L2Loss_2', {
  840. 'block': P.L2Loss(),
  841. 'desc_inputs': [Tensor(np.array([[1, 1], [2, 2], [3, 3], [4, 4]]), mstype.float16)],
  842. 'desc_bprop': []}),
  843. ]
  844. test_case_array_ops = [
  845. ('SpaceToDepth', {
  846. 'block': P.SpaceToDepth(2),
  847. 'desc_inputs': [[1, 3, 2, 2]],
  848. 'desc_bprop': [[1, 12, 1, 1]]}),
  849. ('DepthToSpace', {
  850. 'block': P.DepthToSpace(2),
  851. 'desc_inputs': [[1, 12, 1, 1]],
  852. 'desc_bprop': [[1, 3, 2, 2]]}),
  853. ('Split', {
  854. 'block': P.Split(1, 2),
  855. 'desc_inputs': [Tensor(np.array([[1, 1, 1, 1], [2, 2, 2, 2]]))],
  856. 'skip': ['backward']}),
  857. ('Argmax', {
  858. 'block': P.Argmax(),
  859. 'desc_inputs': [[128, 32, 32, 64]],
  860. 'desc_bprop': [0],
  861. 'skip': ['backward']}),
  862. ('Argmin', {
  863. 'block': P.Argmin(),
  864. 'desc_inputs': [[128, 32, 32, 64]],
  865. 'desc_bprop': [1],
  866. 'skip': ['backward']}),
  867. ('ArgMaxWithValue', {
  868. 'block': P.ArgMaxWithValue(),
  869. 'desc_inputs': [[128, 32, 32, 64]],
  870. 'desc_bprop': [[1], [1]],
  871. 'skip': ['backward']}),
  872. ('ArgMinWithValue', {
  873. 'block': P.ArgMinWithValue(),
  874. 'desc_inputs': [[128, 32, 32, 64]],
  875. 'desc_bprop': [[1], [1]],
  876. 'skip': ['backward']}),
  877. ('Transpose_dim3', {
  878. 'block': P.Transpose(),
  879. 'desc_const': [(0, 2, 1)],
  880. 'desc_inputs': [[1, 2, 3]],
  881. 'desc_bprop': [[1, 3, 2]]}),
  882. ('Transpose_dim4', {
  883. 'block': P.Transpose(),
  884. 'desc_const': [(0, 1, 2, 3)],
  885. 'desc_inputs': [[1, 2, 3, 4]],
  886. 'desc_bprop': [[1, 2, 4, 3]]}),
  887. ('AddN', {
  888. 'block': NetForTupleInput(P.AddN()),
  889. 'desc_inputs': [[2, 3, 3, 5], [2, 3, 3, 5]],
  890. 'desc_bprop': [[2, 3, 3, 5]],
  891. 'skip': ['backward']}),
  892. ('Shape', {
  893. 'block': P.Shape(),
  894. 'desc_inputs': [[3, 3, 2, 2]],
  895. 'skip': ['backward']}),
  896. ('Reshape', {
  897. 'block': P.Reshape(),
  898. 'desc_const': [(64,)],
  899. 'desc_inputs': [[64, 1]],
  900. 'desc_bprop': [[64]]}),
  901. ('Cast', {
  902. 'block': P.Cast(),
  903. 'desc_const': [mstype.int32],
  904. 'desc_inputs': [[2, 3, 4, 5]],
  905. 'desc_bprop': [Tensor(np.ones((2, 3, 4, 5)).astype(np.int32))]}),
  906. ('ExpandDims', {
  907. 'block': P.ExpandDims(),
  908. 'desc_const': [0],
  909. 'desc_inputs': [[2, 2]],
  910. 'desc_bprop': [[1, 2, 2]]}),
  911. ('ExpandDims_1', {
  912. 'block': P.ExpandDims(),
  913. 'desc_const': [-1],
  914. 'desc_inputs': [[2, 2]],
  915. 'desc_bprop': [[2, 2, 1]]}),
  916. ('Squeeze', {
  917. 'block': P.Squeeze(2),
  918. 'desc_inputs': [[3, 2, 1]],
  919. 'desc_bprop': [[3, 2]]}),
  920. ('Squeeze_0', {
  921. 'block': P.Squeeze(),
  922. 'desc_inputs': [[3, 1, 2, 1]],
  923. 'desc_bprop': [[3, 2]]}),
  924. ('Squeeze_1', {
  925. 'block': P.Squeeze(),
  926. 'desc_inputs': [[1, 1, 1, 1]],
  927. 'desc_bprop': [1.0],
  928. 'skip': ['backward']}),
  929. ('Squeeze_2', {
  930. 'block': P.Squeeze((2, 3)),
  931. 'desc_inputs': [[3, 2, 1, 1]],
  932. 'desc_bprop': [[3, 2]]}),
  933. ('Size', {
  934. 'block': P.Size(),
  935. 'desc_inputs': [[2, 3, 5]],
  936. 'skip': ['backward']}),
  937. ('Tile_0', {
  938. 'block': P.Tile(),
  939. 'desc_const': [(1, 2)],
  940. 'desc_inputs': [[64, 1]],
  941. 'desc_bprop': [[64, 2]]}),
  942. ('Tile_1', {
  943. 'block': P.Tile(),
  944. 'desc_const': [(1, 1)],
  945. 'desc_inputs': [[64, 1]],
  946. 'desc_bprop': [[64, 1]]}),
  947. ('Tile_2', {
  948. 'block': P.Tile(),
  949. 'desc_const': [(2, 1, 1, 2)],
  950. 'desc_inputs': [[2, 2, 2]],
  951. 'desc_bprop': [[2, 2, 2, 4]]}),
  952. ('ConcatV2_0', {
  953. 'block': P.Concat(),
  954. 'desc_inputs': [
  955. (Tensor(np.array([[0, 1], [2, 1]]).astype(np.int32)),
  956. Tensor(np.array([[0, 1], [2, 1]]).astype(np.int32)))],
  957. 'desc_bprop': [([4, 2], {'dtype': np.int32})]}),
  958. ('ConcatV2_1', {
  959. 'block': P.Concat(axis=2),
  960. 'desc_inputs': [(Tensor(np.array([[[0, 1, 2]], [[2, 1, 2]]]).astype(np.int32)),
  961. Tensor(np.array([[[0, 1]], [[2, 1]]]).astype(np.int32)))],
  962. 'desc_bprop': [([2, 1, 5], {'dtype': np.int32})]}),
  963. ('ConcatV2_2', {
  964. 'block': NetForConcat(),
  965. 'desc_inputs': [[2, 2]],
  966. 'desc_bprop': [[4, 2]]}),
  967. ('ConcatV2_3', {
  968. 'block': NetForConcat1(),
  969. 'desc_inputs': [[2, 2], [2, 2]],
  970. 'desc_bprop': [[4, 2]]}),
  971. ('ConcatV2_4', {
  972. 'block': P.Concat(axis=0),
  973. 'desc_inputs': [
  974. (Tensor(np.ones((3, 2, 3), np.float32)),
  975. Tensor(np.ones((5, 2, 3), np.float32)),
  976. Tensor(np.ones((6, 2, 3), np.float32)))],
  977. 'desc_bprop': [[14, 2, 3]]}),
  978. ('ConcatV2_5', {
  979. 'block': P.Concat(axis=-1),
  980. 'desc_inputs': [(Tensor(np.array([1], np.float32)),
  981. Tensor(np.array([1], np.float32)),
  982. Tensor(np.array([1], np.float32)))],
  983. 'desc_bprop': [[3,]]}),
  984. ('Pack_0', {
  985. 'block': NetForPackInput(P.Pack()),
  986. 'desc_inputs':[[2, 2], [2, 2], [2, 2]],
  987. 'desc_bprop':[[3, 2, 2]],
  988. }),
  989. ('Pack_1', {
  990. 'block': NetForPackInput(P.Pack(axis=-2)),
  991. 'desc_inputs':[[3, 2, 3], [3, 2, 3], [3, 2, 3]],
  992. 'desc_bprop':[[3, 2, 3, 3]],
  993. }),
  994. ('Pack_2', {
  995. 'block': NetForPackInput(P.Pack()),
  996. 'desc_inputs':[[2, 2]],
  997. 'desc_bprop':[[1, 2, 2]],
  998. }),
  999. ('Pack_3', {
  1000. 'block': NetForPackInput(P.Pack()),
  1001. 'desc_inputs':[[128, 128], [128, 128]],
  1002. 'desc_bprop':[[2, 128, 128]],
  1003. }),
  1004. ('Unpack_0', {
  1005. 'block': NetForUnpackInput(P.Unpack(axis=0)),
  1006. 'desc_inputs':[[2, 4]],
  1007. 'desc_bprop':[[4], [4]],
  1008. }),
  1009. ('Unpack_1', {
  1010. 'block': NetForUnpackInput(P.Unpack(axis=-1)),
  1011. 'desc_inputs':[Tensor(np.array([[1, 1, 1]], np.float32))],
  1012. 'desc_bprop':[[1], [1], [1]],
  1013. }),
  1014. ('Diag', {
  1015. 'block': P.Diag(),
  1016. 'desc_inputs': [[4]],
  1017. 'desc_bprop': [[4, 4]],
  1018. }),
  1019. ('DiagPart', {
  1020. 'block': P.DiagPart(),
  1021. 'desc_inputs': [[4, 4]],
  1022. 'desc_bprop': [[4]],
  1023. }),
  1024. ('SpaceToBatch_1', {
  1025. 'block': P.SpaceToBatch(2, [[0, 0], [0, 0]]),
  1026. 'desc_inputs': [[1, 3, 2, 2]],
  1027. 'desc_bprop': [[4, 3, 1, 1]],
  1028. }),
  1029. ('SpaceToBatch_2', {
  1030. 'block': P.SpaceToBatch(2, [[1, 1], [0, 4]]),
  1031. 'desc_inputs': [[1, 3, 2, 2]],
  1032. 'desc_bprop': [[4, 3, 2, 3]],
  1033. }),
  1034. ('BatchToSpace_1', {
  1035. 'block': P.BatchToSpace(2, [[0, 0], [0, 0]]),
  1036. 'desc_inputs': [[4, 3, 1, 1]],
  1037. 'desc_bprop': [[1, 3, 2, 2]],
  1038. }),
  1039. ('BatchToSpace_2', {
  1040. 'block': P.BatchToSpace(2, [[0, 0], [0, 1]]),
  1041. 'desc_inputs': [[4, 3, 1, 1]],
  1042. 'desc_bprop': [[1, 3, 2, 1]],
  1043. }),
  1044. ]
  1045. test_case_other_ops = [
  1046. ('ScalarLog', {
  1047. 'block': F.scalar_log,
  1048. 'desc_const': [0.0],
  1049. 'desc_inputs': [],
  1050. 'desc_bprop': [1],
  1051. 'skip': ['backward']}),
  1052. ('BoundingBoxEncode', {
  1053. 'block': P.BoundingBoxEncode(means=(0.0, 0.0, 0.0, 0.0), stds=(1.0, 1.0, 1.0, 1.0)),
  1054. 'desc_inputs': [[256, 4], [256, 4]],
  1055. 'desc_bprop': [[256, 4]],
  1056. 'skip': ['backward']}),
  1057. ('BoundingBoxDecode', {
  1058. '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)),
  1059. 'desc_inputs': [[256, 4], [256, 4]],
  1060. 'desc_bprop': [[256, 4]],
  1061. 'skip': ['backward']}),
  1062. ('GatherNd', {
  1063. 'block': P.GatherNd(),
  1064. 'desc_inputs': (Tensor(np.ones((1, 3, 6, 6), np.float32)),
  1065. Tensor(np.ones((2, 4), np.int32))),
  1066. 'desc_bprop': [[2]]}),
  1067. ('ScatterNdUpdate', {
  1068. 'block': P.ScatterNdUpdate(),
  1069. 'desc_inputs': (Tensor(np.ones((2, 3), np.float32)),
  1070. Tensor(np.ones((2, 2), np.int32)),
  1071. Tensor(np.ones((2,), np.float32))),
  1072. 'desc_bprop': [[2, 3]]}),
  1073. ('ScatterNd', {
  1074. 'block': P.ScatterNd(),
  1075. 'desc_const': [(3, 3)],
  1076. 'desc_inputs': (Tensor(np.ones((2, 2), np.int32)),
  1077. Tensor(np.ones((2,), np.int32))),
  1078. 'desc_bprop': [([3, 3], {'dtype': np.int32})]}),
  1079. ('SmoothL1Loss', {
  1080. 'block': P.SmoothL1Loss(),
  1081. 'desc_inputs': [[256, 4], [256, 4]],
  1082. 'desc_bprop': [[256, 4]]}),
  1083. ('IOU', {
  1084. 'block': P.IOU(),
  1085. 'desc_inputs': [Tensor(np.ones((256, 4), np.float16)), Tensor(np.ones((128, 4), np.float16))],
  1086. 'desc_bprop': [[128, 256]]}),
  1087. ('Summary', {
  1088. 'block': SummaryNet(),
  1089. 'desc_inputs': [Tensor(np.array([1.1]).astype(np.float32)),
  1090. Tensor(np.array([1.2]).astype(np.float32))],
  1091. 'skip': ['backward']}),
  1092. ('ConfusionMulGrad_1', {
  1093. 'block': P.ConfusionMulGrad(axis = [0], keep_dims = False),
  1094. 'desc_inputs': [[3, 2], [3, 2], [3, 2]],
  1095. 'desc_bprop': [[3, 2], [2]],
  1096. 'skip': ['backward']}),
  1097. ('ConfusionMulGrad_2', {
  1098. 'block': P.ConfusionMulGrad(axis = [0], keep_dims = True),
  1099. 'desc_inputs': [[3, 2], [3, 2], [3, 2]],
  1100. 'desc_bprop': [[3, 2], [1, 2]],
  1101. 'skip': ['backward']}),
  1102. ('ConfusionMulGrad_3', {
  1103. 'block': P.ConfusionMulGrad(axis = (), keep_dims = True),
  1104. 'desc_inputs': [[2, 3, 4], [2, 3, 4], [2, 3, 4]],
  1105. 'desc_bprop': [[2, 3, 4], [1, 1, 1]],
  1106. 'skip': ['backward']}),
  1107. ('HistogramSummary', {
  1108. 'block': HistogramSummaryNet(),
  1109. 'desc_inputs': [Tensor(np.array([1.1]).astype(np.float32)),
  1110. Tensor(np.array([1.2]).astype(np.float32))],
  1111. 'skip': ['backward']}),
  1112. ]
  1113. test_case_lists = [test_case_nn_ops, test_case_math_ops, test_case_array_ops, test_case_other_ops]
  1114. test_case = functools.reduce(lambda x, y: x + y, test_case_lists)
  1115. # use -k to select certain testcast
  1116. # pytest tests/python/ops/test_ops.py::test_backward -k LayerNorm
  1117. test_exec_case = test_case
  1118. test_backward_exec_case = filter(lambda x: 'skip' not in x[1] or
  1119. 'backward' not in x[1]['skip'], test_case)
  1120. import mindspore.context as context
  1121. @non_graph_engine
  1122. @mindspore_test(pipeline_for_compile_forward_ge_graph_for_case_by_case_config)
  1123. def test_exec():
  1124. context.set_context(mode=context.GRAPH_MODE)
  1125. return test_exec_case
  1126. @mindspore_test(pipeline_for_compile_grad_ge_graph_for_case_by_case_config)
  1127. def test_backward_exec():
  1128. context.set_context(mode=context.GRAPH_MODE)
  1129. return test_backward_exec_case
  1130. raise_set = [
  1131. ('Cast_Error', {
  1132. 'block': (P.Cast(), {'exception': TypeError}),
  1133. 'desc_const': [mstype.int32],
  1134. 'desc_inputs': ['wrong input'],
  1135. 'desc_bprop': [Tensor(np.ones((2, 3, 3, 5)).astype(np.int32))]}),
  1136. ('Maximum_Error', {
  1137. 'block': (P.Maximum(), {'exception': TypeError}),
  1138. 'desc_const': [(1, 2, 3)],
  1139. 'desc_inputs': [[2, 3, 3, 5]],
  1140. 'desc_bprop': [[2, 3, 3, 5]]}),
  1141. ('Shape_error', {
  1142. 'block': (P.Shape(), {'exception': TypeError}),
  1143. 'desc_inputs': [(64, 1)],
  1144. 'desc_bprop': [[64]]}),
  1145. ('Flatten_Error', {
  1146. 'block': (NetForFlatten0D(), {'exception': ValueError}),
  1147. 'desc_inputs': [Tensor(np.array(0).astype(np.int32))],
  1148. 'desc_bprop': [Tensor(np.array(0).astype(np.int32))]}),
  1149. ]
  1150. @mindspore_test(pipeline_for_compile_forward_ge_graph_for_case_by_case_config_exception)
  1151. def test_check_exception():
  1152. return raise_set