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

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