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

5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  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, Parameter, context
  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. 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': P.CumSum(),
  456. 'desc_const': [0],
  457. 'desc_inputs': [Tensor(np.array([[3, 4], [1, 6]]).astype(np.float16))],
  458. 'desc_bprop': [Tensor(np.array([[3, 4], [4, 10]]).astype(np.float16))]}),
  459. ('ReduceSum_3', {
  460. 'block': P.ReduceSum(),
  461. 'desc_const': [0],
  462. 'desc_inputs': [[3, 2]],
  463. 'desc_bprop': [[2]]}),
  464. ('ReduceSum_4', {
  465. 'block': P.ReduceSum(keep_dims=True),
  466. 'desc_const': [0],
  467. 'desc_inputs': [[3, 2]],
  468. 'desc_bprop': [[1, 2]]}),
  469. ('ReduceSum_5', {
  470. 'block': P.ReduceSum(keep_dims=True),
  471. 'desc_inputs': [[2, 3, 4]],
  472. 'desc_bprop': [[1, 1, 1]]}),
  473. ('ReduceSum_6', {
  474. 'block': P.ReduceSum(),
  475. 'desc_inputs': [[2, 3, 4]],
  476. 'desc_bprop': [[1]]}),
  477. ('Sum_0', {
  478. 'block': P.ReduceSum(),
  479. 'desc_const': [(1,)],
  480. 'desc_inputs': [[3, 2]],
  481. 'desc_bprop': [[3]]}),
  482. ('Sum_1', {
  483. 'block': P.ReduceSum(keep_dims=True),
  484. 'desc_const': [(1,)],
  485. 'desc_inputs': [[3, 2]],
  486. 'desc_bprop': [[3, 1]]}),
  487. ('Sum_2', {
  488. 'block': P.ReduceSum(),
  489. 'desc_const': [(0, 1)],
  490. 'desc_inputs': [[3, 2]],
  491. 'desc_bprop': [[1]]}),
  492. ('Sum_3', {
  493. 'block': P.ReduceSum(),
  494. 'desc_const': [0],
  495. 'desc_inputs': [[3, 2]],
  496. 'desc_bprop': [[2]]}),
  497. ('Sum_4', {
  498. 'block': P.ReduceSum(keep_dims=True),
  499. 'desc_const': [0],
  500. 'desc_inputs': [[3, 2]],
  501. 'desc_bprop': [[1, 2]]}),
  502. ('Sum_5', {
  503. 'block': P.ReduceSum(keep_dims=True),
  504. 'desc_const': [()],
  505. 'desc_inputs': [[2, 3, 4]],
  506. 'desc_bprop': [[1, 1, 1]]}),
  507. ('Sum_6', {
  508. 'block': P.ReduceSum(),
  509. 'desc_const': [()],
  510. 'desc_inputs': [[2, 3, 4]],
  511. 'desc_bprop': [[1]]}),
  512. ('Sign', {
  513. 'block': P.Sign(),
  514. 'desc_inputs': [[3]],
  515. 'desc_bprop': [[3]]}),
  516. ('Round', {
  517. 'block': P.Round(),
  518. 'desc_inputs': [[3]],
  519. 'desc_bprop': [[3]]}),
  520. ('Atan2', {
  521. 'block': P.Atan2(),
  522. 'desc_inputs': [Tensor(np.array([0, 1]).astype(np.float32)),
  523. Tensor(np.array([1, 1]).astype(np.float32))],
  524. 'desc_bprop': [[2]]})
  525. ]
  526. test_case_nn_ops = [
  527. ('BiasAdd', {
  528. 'block': P.BiasAdd(),
  529. 'desc_inputs': [[1, 3, 3, 3], [3]],
  530. 'desc_bprop': [[1, 3, 3, 3]]}),
  531. ('BiasAddGrad', {
  532. 'block': G.BiasAddGrad(),
  533. 'desc_inputs': [[1, 3, 3, 3]],
  534. 'skip': ['backward']}),
  535. ('Gelu', {
  536. 'block': P.Gelu(),
  537. 'desc_inputs': [[1, 3, 4, 4]],
  538. 'desc_bprop': [[1, 3, 4, 4]]}),
  539. ('GeluGrad', {
  540. 'block': G.GeluGrad(),
  541. 'desc_inputs': [[2, 2], [2, 2], [2, 2]],
  542. 'desc_bprop': [[2, 2]],
  543. 'skip': ['backward']}),
  544. ('Tanh', {
  545. 'block': P.Tanh(),
  546. 'desc_inputs': [[1, 3, 4, 4]],
  547. 'desc_bprop': [[1, 3, 4, 4]]}),
  548. ('TanhGrad', {
  549. 'block': G.TanhGrad(),
  550. 'desc_inputs': [[1, 3, 4, 4], [1, 3, 4, 4]],
  551. 'desc_bprop': [[1, 3, 4, 4]],
  552. 'skip': ['backward']}),
  553. ('ReLU', {
  554. 'block': P.ReLU(),
  555. 'desc_inputs': [[1, 3, 4, 4]],
  556. 'desc_bprop': [[1, 3, 4, 4]]}),
  557. ('ReLU6', {
  558. 'block': P.ReLU6(),
  559. 'desc_inputs': [[1, 3, 4, 4]],
  560. 'desc_bprop': [[1, 3, 4, 4]]}),
  561. ('ReLUV2', {
  562. 'block': P.ReLUV2(),
  563. 'desc_inputs': [[1, 3, 4, 4]],
  564. 'desc_bprop': [[1, 3, 4, 4], ([1, 1, 4, 4, 2], {'dtype': np.uint8})]}),
  565. ('ReLUGrad', {
  566. 'block': G.ReluGrad(),
  567. 'desc_inputs': [[1, 3, 4, 4], [1, 3, 4, 4]],
  568. 'skip': ['backward']}),
  569. ('Softplus', {
  570. 'block': P.Softplus(),
  571. 'desc_inputs': [[1, 3, 4, 4]],
  572. 'desc_bprop': [[1, 3, 4, 4]]}),
  573. ('SoftplusGrad', {
  574. 'block': G.SoftplusGrad(),
  575. 'desc_inputs': [[1, 3, 4, 4], [1, 3, 4, 4]],
  576. 'skip': ['backward']}),
  577. ('Elu', {
  578. 'block': P.Elu(),
  579. 'desc_inputs': [[2, 3, 4]],
  580. 'desc_bprop': [[2, 3, 4]]}),
  581. ('EluGrad', {
  582. 'block': G.EluGrad(),
  583. 'desc_inputs': [[2, 3, 4], [2, 3, 4]],
  584. 'desc_bprop': [[2, 3, 4]],
  585. 'skip': ['backward']}),
  586. ('Sigmoid', {
  587. 'block': P.Sigmoid(),
  588. 'desc_inputs': [[1, 3, 4, 4]],
  589. 'desc_bprop': [[1, 3, 4, 4]]}),
  590. ('MaxPool', {
  591. 'block': P.MaxPool(ksize=(2, 2), strides=(2, 2), padding="VALID"),
  592. 'desc_inputs': [[100, 3, 28, 28]],
  593. 'desc_bprop': [[100, 3, 14, 14]]}),
  594. ('MaxPoolGrad', {
  595. 'block': G.MaxPoolGrad(ksize=(2, 2), strides=(2, 2), padding="VALID"),
  596. 'desc_inputs': [[3, 4, 6, 6], [3, 4, 3, 3], [3, 4, 3, 3]],
  597. 'desc_bprop': [[3, 4, 6, 6]],
  598. 'skip': ['backward']}),
  599. ('AvgPool', {
  600. 'block': P.AvgPool(ksize=(2, 2), strides=(2, 2), padding="VALID"),
  601. 'desc_inputs': [[100, 3, 28, 28]],
  602. 'desc_bprop': [[100, 3, 14, 14]]}),
  603. ('AvgPoolGrad', {
  604. 'block': G.AvgPoolGrad(ksize=(2, 2), strides=(2, 2), padding="VALID"),
  605. 'desc_const': [(3, 4, 6, 6)],
  606. 'const_first': True,
  607. 'desc_inputs': [[3, 4, 6, 6]],
  608. 'desc_bprop': [[3, 4, 6, 6]],
  609. 'skip': ['backward']}),
  610. ('MaxPoolWithArgmax', {
  611. 'block': P.MaxPoolWithArgmax(ksize=2, strides=2),
  612. 'desc_inputs': [[128, 32, 32, 64]],
  613. 'desc_bprop': [[128, 32, 16, 32], ([128, 32, 4, 33], {'dtype': np.uint16})]}),
  614. ('SoftmaxCrossEntropyWithLogits', {
  615. 'block': P.SoftmaxCrossEntropyWithLogits(),
  616. 'desc_inputs': [[1, 10], [1, 10]],
  617. 'desc_bprop': [[1], [1, 10]],
  618. 'skip': ['backward_exec']}),
  619. ('Flatten', {
  620. 'block': P.Flatten(),
  621. 'desc_inputs': [[128, 32, 32, 64]],
  622. 'desc_bprop': [[128 * 32 * 8 * 16]]}),
  623. ('LogSoftmax', {
  624. 'block': P.LogSoftmax(),
  625. 'desc_inputs': [[64, 2]],
  626. 'desc_bprop': [[64, 2]]}),
  627. ('LogSoftmaxGrad', {
  628. 'block': G.LogSoftmaxGrad(),
  629. 'desc_inputs': [[16, 1234], [16, 1234]],
  630. 'desc_bprop': [[64, 2]],
  631. 'skip': ['backward']}),
  632. ('L2Normalize', {
  633. 'block': P.L2Normalize(),
  634. 'desc_inputs': [[2, 2]],
  635. 'desc_bprop': [[2, 2]]}),
  636. ('L2NormalizeGrad', {
  637. 'block': G.L2NormalizeGrad(),
  638. 'desc_inputs': [[2, 2], [2, 2], [2, 2]],
  639. 'desc_bprop': [[2, 2]],
  640. 'skip': ['backward']}),
  641. ('LayerNorm', {
  642. 'block': P.LayerNorm(),
  643. 'desc_inputs': [[2, 16], [16], [16]],
  644. 'desc_bprop': [[2, 16], [2, 1], [2, 1]]}),
  645. ('LayerNormGrad', {
  646. 'block': G.LayerNormGrad(),
  647. 'desc_inputs': [[2, 16], [2, 16], [2, 16], [2, 16], [16]],
  648. 'desc_bprop': [[2, 16], [16], [16]],
  649. 'skip': ['backward']}),
  650. ('FusedBatchNorm', {
  651. 'block': P.FusedBatchNorm(),
  652. 'desc_inputs': [[128, 64, 32, 64], [64], [64], [64], [64]],
  653. 'desc_bprop': [[128, 64, 32, 64], [64], [64], [64], [64]],
  654. 'skip': []}),
  655. ('FusedBatchNormGrad', {
  656. 'block': G.FusedBatchNormGrad(),
  657. 'desc_inputs': [[128, 64, 32, 64], [128, 64, 32, 64], [64], [64], [64]],
  658. 'desc_bprop': [[128, 64, 32, 64], [64], [64], [64], [64]],
  659. 'skip': ['backward']}),
  660. ('BatchNorm', {
  661. 'block': P.BatchNorm(),
  662. 'desc_inputs': [[128, 64, 32, 32], [64], [64], [64], [64]],
  663. 'desc_bprop': [[128, 64, 32, 32], [64], [64], [64], [64]],
  664. 'skip': []}),
  665. ('BatchNormGrad', {
  666. 'block': G.BatchNormGrad(),
  667. 'desc_inputs': [[128, 64, 32, 32], [128, 64, 32, 32], [64], [64], [64]],
  668. 'desc_bprop': [[128, 64, 32, 32], [64], [64], [64], [64]],
  669. 'skip': ['backward']}),
  670. ('TopK', {
  671. 'block': P.TopK(),
  672. 'desc_const': [5],
  673. 'desc_inputs': [[20, 20, 10]],
  674. 'desc_bprop': [[20, 20, 5]],
  675. 'skip': ['backward']}),
  676. ('GatherV2_0', {
  677. 'block': P.GatherV2(),
  678. 'desc_const': [0],
  679. 'desc_inputs': [[3, 1, 2], Tensor(np.array([0, 1]).astype(np.int32))],
  680. 'desc_bprop': [[2, 1, 2]]}),
  681. ('GatherV2_1', {
  682. 'block': P.GatherV2(),
  683. 'desc_const': [2],
  684. 'desc_inputs': [[3, 1, 3], Tensor(np.array([0, 1]).astype(np.int32))],
  685. 'desc_bprop': [[3, 1, 2]]}),
  686. ('GatherV2_2', {
  687. 'block': P.GatherV2(),
  688. 'desc_const': [0],
  689. 'desc_inputs': [[3, 1, 3], Tensor(np.array([[0, 1], [0, 1], [0, 1]]).astype(np.int32))],
  690. 'desc_bprop': [[3, 2, 1, 3]]}),
  691. ('GatherV2_3', {
  692. 'block': P.GatherV2(),
  693. 'desc_const': [2],
  694. 'desc_inputs': [[3, 1, 3], Tensor(np.array([[0, 1], [0, 1], [0, 1]]).astype(np.int32))],
  695. 'desc_bprop': [[3, 1, 3, 2]]}),
  696. ('GatherV2_4', {
  697. 'block': P.GatherV2(),
  698. 'desc_const': [1],
  699. 'desc_inputs': [[32, 5, 1024], Tensor(np.array([3]).astype(np.int32))],
  700. 'desc_bprop': [[32, 1, 1024]]}),
  701. ('GatherV2_5', {
  702. 'block': P.GatherV2(),
  703. 'desc_const': [-1],
  704. 'desc_inputs': [[3, 1, 3], Tensor(np.array([0, 1]).astype(np.int32))],
  705. 'desc_bprop': [[3, 1, 2]]}),
  706. ('GatherV2_6', {
  707. 'block': P.GatherV2(),
  708. 'desc_const': [0],
  709. 'desc_inputs': [[1152], Tensor(np.array(10).astype(np.int32))],
  710. 'desc_bprop': [Tensor(np.array(10).astype(np.float32))]}),
  711. ('UnsortedSegmentSum', {
  712. 'block': P.UnsortedSegmentSum(),
  713. 'desc_const': [1280],
  714. 'desc_inputs': [[1280, 1024], Tensor(np.ones(1280).astype(np.int32))],
  715. 'desc_bprop': [[8192, 1024]],
  716. 'skip': ['backward']}),
  717. ('UnsortedSegmentSum_1', {
  718. 'block': P.UnsortedSegmentSum(),
  719. 'desc_const': [4],
  720. 'desc_inputs': [[3, 2, 1, 3], Tensor(np.array([[0, 1], [0, 1], [0, 1]]).astype(np.int32))],
  721. 'desc_bprop': [[4, 1, 3]],
  722. 'skip': ['backward']}),
  723. ('DropoutGenMask', {
  724. 'block': P.DropoutGenMask(),
  725. 'desc_const': [(2, 2), Tensor(0.5, mstype.float32)],
  726. 'desc_inputs': [],
  727. 'desc_bprop': [Tensor(np.ones(1).astype(np.int8))],
  728. 'skip': ['backward']}),
  729. ('DropoutDoMask', {
  730. 'block': P.DropoutDoMask(),
  731. 'desc_const': [Tensor(0.5)],
  732. 'desc_inputs': [[64, 12, 128, 128], Tensor(np.ones(1572864).astype(np.uint8))],
  733. 'desc_bprop': [[64, 12, 128, 128]]}),
  734. ('Dropout', {
  735. 'block': nn.Dropout(0.5),
  736. 'desc_inputs': [[64, 12, 128, 128]],
  737. 'desc_bprop': [[64, 12, 128, 128]]}),
  738. ('ReduceMean0', {
  739. 'block': P.ReduceMean(),
  740. 'desc_const': [(2,)],
  741. 'desc_inputs': [[3, 2, 2]],
  742. 'desc_bprop': [[3, 2]]}),
  743. ('ReduceMean1', {
  744. 'block': P.ReduceMean(),
  745. 'desc_const': [2],
  746. 'desc_inputs': [[3, 2, 2]],
  747. 'desc_bprop': [[3, 2]]}),
  748. ('All', {
  749. 'block': P.ReduceAll(),
  750. 'desc_const': [(1,)],
  751. 'desc_inputs': [Tensor(np.ones([3, 2]).astype(np.bool_))],
  752. 'desc_bprop': [[3]],
  753. 'skip': ['backward']}),
  754. ('DescConst', {
  755. 'block': Tensor(np.array([2], np.float32)),
  756. 'desc_inputs': [],
  757. 'desc_bprop': [[1]],
  758. 'skip': ['backward'],
  759. 'add_fake_input': True}),
  760. ('Fill', {
  761. 'block': P.Fill(),
  762. 'desc_const': [mstype.float32, (2, 3), 1.0],
  763. 'desc_inputs': [],
  764. 'desc_bprop': [[2, 3]],
  765. 'skip': ['backward'],
  766. 'add_fake_input': True}),
  767. ('OnesLike', {
  768. 'block': P.OnesLike(),
  769. 'desc_inputs': [Tensor(np.array([[0, 1], [2, 1]]).astype(np.int32))],
  770. 'desc_bprop': [Tensor(np.array([[1, 1], [1, 1]]).astype(np.int32))]
  771. }),
  772. ('ZerosLike', {
  773. 'block': P.ZerosLike(),
  774. 'desc_inputs': [Tensor(np.array([[0, 1], [2, 1]]).astype(np.int32))],
  775. 'desc_bprop': [Tensor(np.array([[1, 1], [1, 1]]).astype(np.int32))]
  776. }),
  777. ('Softmax', {
  778. 'block': P.Softmax(),
  779. 'desc_inputs': [[5, 5]],
  780. 'desc_bprop': [[5, 5]]}),
  781. ('DepthwiseConv2dNative_1', {
  782. 'block': P.DepthwiseConv2dNative(3, (3, 3), pad_mode="pad", pad=1, stride=2),
  783. 'desc_inputs': [[10, 32, 32, 32], [1, 32, 3, 3]],
  784. 'desc_bprop': [[10, 32, 16, 16]]}),
  785. ('DepthwiseConv2dNative_2', {
  786. 'block': P.DepthwiseConv2dNative(1, (3, 3), pad_mode="same", pad=0, stride=1),
  787. 'desc_inputs': [[2592, 2048, 4, 4], [1, 2048, 3, 3]],
  788. 'desc_bprop': [[2592, 2048, 4, 4]]}),
  789. ('SigmoidCrossEntropyWithLogits', {
  790. 'block': P.SigmoidCrossEntropyWithLogits(),
  791. 'desc_inputs': [[128, 10], [128, 10]],
  792. 'desc_bprop': [[128, 10]]}),
  793. ('Pad', {
  794. 'block': P.Pad(((1, 2), (2, 3))),
  795. 'desc_inputs': [[7, 7]],
  796. 'desc_bprop': [[10, 12]]}),
  797. ('BinaryCrossEntropy', {
  798. 'block': P.BinaryCrossEntropy(),
  799. 'desc_inputs': [[1, 2, 3], [1, 2, 3], [1, 2, 3]],
  800. 'desc_bprop': []}),
  801. ('SparseApplyAdagrad', {
  802. 'block': P.SparseApplyAdagrad(0.5),
  803. 'desc_inputs': [[3, 3], [3, 3], [3, 3], Tensor(np.ones((3,), np.int32))],
  804. 'skip': ['backward']}),
  805. ('Flatten_1', {
  806. 'block': NetForFlatten(),
  807. 'desc_inputs': [Tensor(np.ones([2, 3, 4]).astype(np.int32)), Tensor(np.ones([2, 12]).astype(np.int32))],
  808. 'desc_bprop': [Tensor(np.ones([2, 12]).astype(np.int32))],
  809. 'skip': ['backward']}),
  810. ('Flatten_2', {
  811. 'block': NetForFlatten(),
  812. 'desc_inputs': [Tensor(np.ones([8]).astype(np.int32)), Tensor(np.ones([8, 3]).astype(np.int32))],
  813. 'desc_bprop': [Tensor(np.ones([8, 3]).astype(np.int32))],
  814. 'skip': ['backward']}),
  815. ('ArgmaxNet', {
  816. 'block': ArgmaxNet(),
  817. 'desc_inputs': [Tensor(np.array([[128, 32, 32, 64], [128, 32, 32, 64]]).astype(np.float16))],
  818. 'desc_bprop': [Tensor(np.array([[128, 32, 32, 64], [128, 32, 32, 64]]).astype(np.float16))],
  819. 'skip': ['backward']}),
  820. ('ArgminNet', {
  821. 'block': ArgminNet(),
  822. 'desc_inputs': [Tensor(np.array([[128, 32, 32, 64], [128, 32, 32, 64]]).astype(np.float16))],
  823. 'desc_bprop': [Tensor(np.array([[128, 32, 32, 64], [128, 32, 32, 64]]).astype(np.float16))],
  824. 'skip': ['backward']}),
  825. ('CumSumNet', {
  826. 'block': CumSumNet(),
  827. 'desc_const': [0],
  828. 'desc_inputs': [Tensor(np.array([[3, 4, 6, 10], [1, 6, 7, 9], [4, 3, 8, 7], [1, 3, 7, 9]]).astype(np.float16))],
  829. 'desc_bprop': [
  830. Tensor(np.array([[3, 4, 6, 10], [1, 6, 7, 9], [4, 3, 8, 7], [1, 3, 7, 9]]).astype(np.float16))]}),
  831. ('OneHot', {
  832. 'block': P.OneHot(),
  833. 'desc_const': [3, Tensor(1.0, mstype.float32), Tensor(0.0, mstype.float32)],
  834. 'desc_inputs': [Tensor(np.array([64]).astype(np.int32))],
  835. 'desc_bprop': [[1, 3]]}),
  836. ('ReduceProd_0', {
  837. 'block': P.ReduceProd(),
  838. 'desc_const': [0],
  839. 'desc_inputs': [[3, 2]],
  840. 'desc_bprop': [[2]]}),
  841. ('ReduceProd_1', {
  842. 'block': P.ReduceProd(keep_dims=True),
  843. 'desc_const': [0],
  844. 'desc_inputs': [[3, 2]],
  845. 'desc_bprop': [[1, 2]]}),
  846. ('CumProd', {
  847. 'block': P.CumProd(),
  848. 'desc_const': [0],
  849. 'desc_inputs': [[3, 2]],
  850. 'desc_bprop': [[3, 2]]}),
  851. ('ApplyFtrl', {
  852. 'block': ApplyFtrlNet(),
  853. 'desc_inputs': [[3, 3]],
  854. 'desc_bprop': [3, 3],
  855. 'skip': ['backward']}),
  856. ('ApplyRMSProp', {
  857. 'block': P.ApplyRMSProp(),
  858. 'desc_const': [0.9, 0.0, 1e-10, 0.001],
  859. 'desc_inputs': [[3, 3], [3, 3], [3, 3], [3, 3]],
  860. 'desc_bprop': [3, 3],
  861. 'skip': ['backward']}),
  862. ('ApplyCenteredRMSProp', {
  863. 'block': P.ApplyCenteredRMSProp(),
  864. 'desc_const': [0.9, 0.0, 1e-10, 0.001],
  865. 'desc_inputs': [[3, 3], [3, 3], [3, 3], [3, 3], [3, 3]],
  866. 'desc_bprop': [3, 3],
  867. 'skip': ['backward']}),
  868. ('CTCLoss', {
  869. 'block': P.CTCLoss(),
  870. 'desc_inputs': [Tensor(np.ones([6, 4, 6]).astype(np.float32)),
  871. Tensor(np.array([[0, 1], [1, 0], [2, 3], [3, 2]]).astype(np.int64)),
  872. Tensor(np.array([1, 2, 3, 4]).astype(np.int32)),
  873. Tensor(np.array([6, 6, 6, 6]).astype(np.int32))],
  874. 'desc_bprop': [[4], [6, 4, 6]]}),
  875. ('L2Loss_1', {
  876. 'block': P.L2Loss(),
  877. 'desc_inputs': [Tensor(np.array([1, 2, 3, 4]), mstype.float32)],
  878. 'desc_bprop': []}),
  879. ('L2Loss_2', {
  880. 'block': P.L2Loss(),
  881. 'desc_inputs': [Tensor(np.array([[1, 1], [2, 2], [3, 3], [4, 4]]), mstype.float16)],
  882. 'desc_bprop': []}),
  883. ('ResizeBilinear', {
  884. 'block': P.ResizeBilinear((5, 5)),
  885. 'desc_inputs': [Tensor([[[[1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]]], mstype.float16)],
  886. 'desc_bprop': [Tensor([[[[1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]]], mstype.float16)]}),
  887. ('ResizeBilinearGrad', {
  888. 'block': G.ResizeBilinearGrad(),
  889. 'desc_inputs': [Tensor([[[[1, 2, 3, 4, 5]]]], mstype.float32), Tensor([[[[1, 2, 3, 4, 5]]]], mstype.float32)],
  890. 'desc_bprop': [Tensor([[[[1, 2, 3, 4, 5]]]], mstype.float32)],
  891. 'skip': ['backward']}),
  892. ('ROIAlign', {
  893. 'block': P.ROIAlign(7, 7, 0.03125, 2),
  894. 'desc_inputs': [[2, 256, 192, 320], [1024, 5]],
  895. 'desc_bprop': [[7,7]]}),
  896. ('ROIAlignGrad', {
  897. 'block': G.ROIAlignGrad((1, 1, 1, 1), 2, 2, 0.5, 2),
  898. 'desc_inputs': [[1, 1, 2, 2], [1, 5]],
  899. 'desc_bprop': [[1, 1, 2, 2]],
  900. 'skip': ['backward']}),
  901. ]
  902. test_case_array_ops = [
  903. ('SpaceToDepth', {
  904. 'block': P.SpaceToDepth(2),
  905. 'desc_inputs': [[1, 3, 2, 2]],
  906. 'desc_bprop': [[1, 12, 1, 1]]}),
  907. ('DepthToSpace', {
  908. 'block': P.DepthToSpace(2),
  909. 'desc_inputs': [[1, 12, 1, 1]],
  910. 'desc_bprop': [[1, 3, 2, 2]]}),
  911. ('Split', {
  912. 'block': P.Split(1, 2),
  913. 'desc_inputs': [Tensor(np.array([[1, 1, 1, 1], [2, 2, 2, 2]]))],
  914. 'skip': ['backward']}),
  915. ('Argmax', {
  916. 'block': P.Argmax(),
  917. 'desc_inputs': [[128, 32, 32, 64]],
  918. 'desc_bprop': [0],
  919. 'skip': ['backward']}),
  920. ('Argmin', {
  921. 'block': P.Argmin(),
  922. 'desc_inputs': [[128, 32, 32, 64]],
  923. 'desc_bprop': [1],
  924. 'skip': ['backward']}),
  925. ('ArgMaxWithValue', {
  926. 'block': P.ArgMaxWithValue(),
  927. 'desc_inputs': [[128, 32, 32, 64]],
  928. 'desc_bprop': [[1], [1]],
  929. 'skip': ['backward']}),
  930. ('ArgMinWithValue', {
  931. 'block': P.ArgMinWithValue(),
  932. 'desc_inputs': [[128, 32, 32, 64]],
  933. 'desc_bprop': [[1], [1]],
  934. 'skip': ['backward']}),
  935. ('Transpose_dim3', {
  936. 'block': P.Transpose(),
  937. 'desc_const': [(0, 2, 1)],
  938. 'desc_inputs': [[1, 2, 3]],
  939. 'desc_bprop': [[1, 3, 2]]}),
  940. ('Transpose_dim4', {
  941. 'block': P.Transpose(),
  942. 'desc_const': [(0, 1, 2, 3)],
  943. 'desc_inputs': [[1, 2, 3, 4]],
  944. 'desc_bprop': [[1, 2, 4, 3]]}),
  945. ('AddN', {
  946. 'block': NetForTupleInput(P.AddN()),
  947. 'desc_inputs': [[2, 3, 3, 5], [2, 3, 3, 5]],
  948. 'desc_bprop': [[2, 3, 3, 5]],
  949. 'skip': ['backward']}),
  950. ('Shape', {
  951. 'block': P.Shape(),
  952. 'desc_inputs': [[3, 3, 2, 2]],
  953. 'skip': ['backward']}),
  954. ('Reshape', {
  955. 'block': P.Reshape(),
  956. 'desc_const': [(64,)],
  957. 'desc_inputs': [[64, 1]],
  958. 'desc_bprop': [[64]]}),
  959. ('Cast', {
  960. 'block': P.Cast(),
  961. 'desc_const': [mstype.int32],
  962. 'desc_inputs': [[2, 3, 4, 5]],
  963. 'desc_bprop': [Tensor(np.ones((2, 3, 4, 5)).astype(np.int32))]}),
  964. ('ExpandDims', {
  965. 'block': P.ExpandDims(),
  966. 'desc_const': [0],
  967. 'desc_inputs': [[2, 2]],
  968. 'desc_bprop': [[1, 2, 2]]}),
  969. ('ExpandDims_1', {
  970. 'block': P.ExpandDims(),
  971. 'desc_const': [-1],
  972. 'desc_inputs': [[2, 2]],
  973. 'desc_bprop': [[2, 2, 1]]}),
  974. ('Squeeze', {
  975. 'block': P.Squeeze(2),
  976. 'desc_inputs': [[3, 2, 1]],
  977. 'desc_bprop': [[3, 2]]}),
  978. ('Squeeze_0', {
  979. 'block': P.Squeeze(),
  980. 'desc_inputs': [[3, 1, 2, 1]],
  981. 'desc_bprop': [[3, 2]]}),
  982. ('Squeeze_1', {
  983. 'block': P.Squeeze(),
  984. 'desc_inputs': [[1, 1, 1, 1]],
  985. 'desc_bprop': [1.0],
  986. 'skip': ['backward']}),
  987. ('Squeeze_2', {
  988. 'block': P.Squeeze((2, 3)),
  989. 'desc_inputs': [[3, 2, 1, 1]],
  990. 'desc_bprop': [[3, 2]]}),
  991. ('Size', {
  992. 'block': P.Size(),
  993. 'desc_inputs': [[2, 3, 5]],
  994. 'skip': ['backward']}),
  995. ('Tile_0', {
  996. 'block': P.Tile(),
  997. 'desc_const': [(1, 2)],
  998. 'desc_inputs': [[64, 1]],
  999. 'desc_bprop': [[64, 2]]}),
  1000. ('Tile_1', {
  1001. 'block': P.Tile(),
  1002. 'desc_const': [(1, 1)],
  1003. 'desc_inputs': [[64, 1]],
  1004. 'desc_bprop': [[64, 1]]}),
  1005. ('Tile_2', {
  1006. 'block': P.Tile(),
  1007. 'desc_const': [(2, 1, 1, 2)],
  1008. 'desc_inputs': [[2, 2, 2]],
  1009. 'desc_bprop': [[2, 2, 2, 4]]}),
  1010. ('ConcatV2_0', {
  1011. 'block': P.Concat(),
  1012. 'desc_inputs': [
  1013. (Tensor(np.array([[0, 1], [2, 1]]).astype(np.int32)),
  1014. Tensor(np.array([[0, 1], [2, 1]]).astype(np.int32)))],
  1015. 'desc_bprop': [([4, 2], {'dtype': np.int32})]}),
  1016. ('ConcatV2_1', {
  1017. 'block': P.Concat(axis=2),
  1018. 'desc_inputs': [(Tensor(np.array([[[0, 1, 2]], [[2, 1, 2]]]).astype(np.int32)),
  1019. Tensor(np.array([[[0, 1]], [[2, 1]]]).astype(np.int32)))],
  1020. 'desc_bprop': [([2, 1, 5], {'dtype': np.int32})]}),
  1021. ('ConcatV2_2', {
  1022. 'block': NetForConcat(),
  1023. 'desc_inputs': [[2, 2]],
  1024. 'desc_bprop': [[4, 2]]}),
  1025. ('ConcatV2_3', {
  1026. 'block': NetForConcat1(),
  1027. 'desc_inputs': [[2, 2], [2, 2]],
  1028. 'desc_bprop': [[4, 2]]}),
  1029. ('ConcatV2_4', {
  1030. 'block': P.Concat(axis=0),
  1031. 'desc_inputs': [
  1032. (Tensor(np.ones((3, 2, 3), np.float32)),
  1033. Tensor(np.ones((5, 2, 3), np.float32)),
  1034. Tensor(np.ones((6, 2, 3), np.float32)))],
  1035. 'desc_bprop': [[14, 2, 3]]}),
  1036. ('ConcatV2_5', {
  1037. 'block': P.Concat(axis=-1),
  1038. 'desc_inputs': [(Tensor(np.array([1], np.float32)),
  1039. Tensor(np.array([1], np.float32)),
  1040. Tensor(np.array([1], np.float32)))],
  1041. 'desc_bprop': [[3, ]]}),
  1042. ('Pack_0', {
  1043. 'block': NetForPackInput(P.Pack()),
  1044. 'desc_inputs': [[2, 2], [2, 2], [2, 2]],
  1045. 'desc_bprop': [[3, 2, 2]],
  1046. }),
  1047. ('Pack_1', {
  1048. 'block': NetForPackInput(P.Pack(axis=-2)),
  1049. 'desc_inputs': [[3, 2, 3], [3, 2, 3], [3, 2, 3]],
  1050. 'desc_bprop': [[3, 2, 3, 3]],
  1051. }),
  1052. ('Pack_2', {
  1053. 'block': NetForPackInput(P.Pack()),
  1054. 'desc_inputs': [[128, 128], [128, 128]],
  1055. 'desc_bprop': [[2, 128, 128]],
  1056. }),
  1057. ('Unpack_0', {
  1058. 'block': NetForUnpackInput(P.Unpack(axis=0)),
  1059. 'desc_inputs': [[2, 4]],
  1060. 'desc_bprop': [[4], [4]],
  1061. }),
  1062. ('Unpack_1', {
  1063. 'block': NetForUnpackInput(P.Unpack(axis=-1)),
  1064. 'desc_inputs': [Tensor(np.array([[1, 1, 1]], np.float32))],
  1065. 'desc_bprop': [[1], [1], [1]],
  1066. }),
  1067. ('Diag_1', {
  1068. 'block': P.Diag(),
  1069. 'desc_inputs': [[4]],
  1070. 'desc_bprop': [[4, 4]],
  1071. }),
  1072. ('Diag_2', {
  1073. 'block': P.Diag(),
  1074. 'desc_inputs': [[4, 4]],
  1075. 'desc_bprop': [[4, 4, 4, 4]],
  1076. }),
  1077. ('DiagPart_1', {
  1078. 'block': P.DiagPart(),
  1079. 'desc_inputs': [[4, 4]],
  1080. 'desc_bprop': [[4]],
  1081. }),
  1082. ('DiagPart_2', {
  1083. 'block': P.DiagPart(),
  1084. 'desc_inputs': [[4, 4, 4, 4]],
  1085. 'desc_bprop': [[4, 4]],
  1086. }),
  1087. ('SpaceToBatch_1', {
  1088. 'block': P.SpaceToBatch(2, [[0, 0], [0, 0]]),
  1089. 'desc_inputs': [[1, 3, 2, 2]],
  1090. 'desc_bprop': [[4, 3, 1, 1]],
  1091. }),
  1092. ('SpaceToBatch_2', {
  1093. 'block': P.SpaceToBatch(2, [[1, 1], [0, 4]]),
  1094. 'desc_inputs': [[1, 3, 2, 2]],
  1095. 'desc_bprop': [[4, 3, 2, 3]],
  1096. }),
  1097. ('BatchToSpace_1', {
  1098. 'block': P.BatchToSpace(2, [[0, 0], [0, 0]]),
  1099. 'desc_inputs': [[4, 3, 1, 1]],
  1100. 'desc_bprop': [[1, 3, 2, 2]],
  1101. }),
  1102. ('BatchToSpace_2', {
  1103. 'block': P.BatchToSpace(2, [[0, 0], [0, 1]]),
  1104. 'desc_inputs': [[4, 3, 1, 1]],
  1105. 'desc_bprop': [[1, 3, 2, 1]],
  1106. }),
  1107. ]
  1108. test_case_other_ops = [
  1109. ('ScalarLog', {
  1110. 'block': F.scalar_log,
  1111. 'desc_const': [0.0],
  1112. 'desc_inputs': [],
  1113. 'desc_bprop': [1],
  1114. 'skip': ['backward']}),
  1115. ('BoundingBoxEncode', {
  1116. 'block': P.BoundingBoxEncode(means=(0.0, 0.0, 0.0, 0.0), stds=(1.0, 1.0, 1.0, 1.0)),
  1117. 'desc_inputs': [[256, 4], [256, 4]],
  1118. 'desc_bprop': [[256, 4]],
  1119. 'skip': ['backward']}),
  1120. ('BoundingBoxDecode', {
  1121. '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)),
  1122. 'desc_inputs': [[256, 4], [256, 4]],
  1123. 'desc_bprop': [[256, 4]],
  1124. 'skip': ['backward']}),
  1125. ('GatherNd', {
  1126. 'block': P.GatherNd(),
  1127. 'desc_inputs': (Tensor(np.ones((1, 3, 6, 6), np.float32)),
  1128. Tensor(np.ones((2, 4), np.int32))),
  1129. 'desc_bprop': [[2]]}),
  1130. ('ScatterNd', {
  1131. 'block': P.ScatterNd(),
  1132. 'desc_const': [(3, 3)],
  1133. 'desc_inputs': (Tensor(np.ones((2, 2), np.int32)),
  1134. Tensor(np.ones((2,), np.int32))),
  1135. 'desc_bprop': [([3, 3], {'dtype': np.int32})]}),
  1136. ('ScatterMax', {
  1137. 'block': ScatterMax(),
  1138. 'desc_inputs': (Tensor(np.array([[0, 0], [1, 1]], np.int32)),
  1139. Tensor(np.ones([2, 2, 3], np.float32) * 99)),
  1140. 'skip': ['backward']}),
  1141. ('SmoothL1Loss', {
  1142. 'block': P.SmoothL1Loss(),
  1143. 'desc_inputs': [[256, 4], [256, 4]],
  1144. 'desc_bprop': [[256, 4]]}),
  1145. ('IOU', {
  1146. 'block': P.IOU(),
  1147. 'desc_inputs': [Tensor(np.ones((256, 4), np.float16)), Tensor(np.ones((128, 4), np.float16))],
  1148. 'desc_bprop': [[128, 256]]}),
  1149. ('Summary', {
  1150. 'block': SummaryNet(),
  1151. 'desc_inputs': [Tensor(np.array([1.1]).astype(np.float32)),
  1152. Tensor(np.array([1.2]).astype(np.float32))],
  1153. 'skip': ['backward']}),
  1154. ('ConfusionMulGrad_1', {
  1155. 'block': P.ConfusionMulGrad(axis=[0], keep_dims=False),
  1156. 'desc_inputs': [[3, 2], [3, 2], [3, 2]],
  1157. 'desc_bprop': [[3, 2], [2]],
  1158. 'skip': ['backward']}),
  1159. ('ConfusionMulGrad_2', {
  1160. 'block': P.ConfusionMulGrad(axis=[0], keep_dims=True),
  1161. 'desc_inputs': [[3, 2], [3, 2], [3, 2]],
  1162. 'desc_bprop': [[3, 2], [1, 2]],
  1163. 'skip': ['backward']}),
  1164. ('ConfusionMulGrad_3', {
  1165. 'block': P.ConfusionMulGrad(axis=(), keep_dims=True),
  1166. 'desc_inputs': [[2, 3, 4], [2, 3, 4], [2, 3, 4]],
  1167. 'desc_bprop': [[2, 3, 4], [1, 1, 1]],
  1168. 'skip': ['backward']}),
  1169. ('HistogramSummary', {
  1170. 'block': HistogramSummaryNet(),
  1171. 'desc_inputs': [Tensor(np.array([1.1]).astype(np.float32)),
  1172. Tensor(np.array([1.2]).astype(np.float32))],
  1173. 'skip': ['backward']}),
  1174. ]
  1175. test_case_lists = [test_case_nn_ops, test_case_math_ops, test_case_array_ops, test_case_other_ops]
  1176. test_case = functools.reduce(lambda x, y: x + y, test_case_lists)
  1177. # use -k to select certain testcast
  1178. # pytest tests/python/ops/test_ops.py::test_backward -k LayerNorm
  1179. test_exec_case = test_case
  1180. test_backward_exec_case = filter(lambda x: 'skip' not in x[1] or
  1181. 'backward' not in x[1]['skip'], test_case)
  1182. @non_graph_engine
  1183. @mindspore_test(pipeline_for_compile_forward_ge_graph_for_case_by_case_config)
  1184. def test_exec():
  1185. context.set_context(mode=context.GRAPH_MODE)
  1186. return test_exec_case
  1187. @mindspore_test(pipeline_for_compile_grad_ge_graph_for_case_by_case_config)
  1188. def test_backward_exec():
  1189. context.set_context(mode=context.GRAPH_MODE)
  1190. return test_backward_exec_case
  1191. raise_set = [
  1192. ('Cast_Error', {
  1193. 'block': (P.Cast(), {'exception': TypeError}),
  1194. 'desc_const': [mstype.int32],
  1195. 'desc_inputs': ['wrong input'],
  1196. 'desc_bprop': [Tensor(np.ones((2, 3, 3, 5)).astype(np.int32))]}),
  1197. ('Maximum_Error', {
  1198. 'block': (P.Maximum(), {'exception': TypeError}),
  1199. 'desc_const': [(1, 2, 3)],
  1200. 'desc_inputs': [[2, 3, 3, 5]],
  1201. 'desc_bprop': [[2, 3, 3, 5]]}),
  1202. ('Shape_error', {
  1203. 'block': (P.Shape(), {'exception': TypeError}),
  1204. 'desc_inputs': [(64, 1)],
  1205. 'desc_bprop': [[64]]}),
  1206. ('Flatten_Error', {
  1207. 'block': (NetForFlatten0D(), {'exception': ValueError}),
  1208. 'desc_inputs': [Tensor(np.array(0).astype(np.int32))],
  1209. 'desc_bprop': [Tensor(np.array(0).astype(np.int32))]}),
  1210. ('ScatterNdUpdate', {
  1211. 'block': (P.ScatterNdUpdate(), {'exception': TypeError}),
  1212. 'desc_inputs': (Tensor(np.ones((2, 3), np.float32)),
  1213. Tensor(np.ones((2, 2), np.int32)),
  1214. Tensor(np.ones((2,), np.float32))),
  1215. 'desc_bprop': [[2, 3]]}),
  1216. ('Pack', {
  1217. 'block': (NetForPackInput(P.Pack()), {'exception': ValueError}),
  1218. 'desc_inputs': [[2, 2]],
  1219. 'desc_bprop': [[1, 2, 2]]}),
  1220. ('PReLU', {
  1221. 'block': (P.PReLU(), {'exception': ValueError}),
  1222. 'desc_inputs': [[2], [1]],
  1223. 'desc_bprop': [[1]]}),
  1224. ]
  1225. @mindspore_test(pipeline_for_compile_forward_ge_graph_for_case_by_case_config_exception)
  1226. def test_check_exception():
  1227. return raise_set