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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202
  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 mindspore.ops.operations import _inner_ops as inner
  27. from ..ut_filter import non_graph_engine
  28. from ....mindspore_test_framework.mindspore_test import mindspore_test
  29. from ....mindspore_test_framework.pipeline.forward.compile_forward \
  30. import (pipeline_for_compile_forward_ge_graph_for_case_by_case_config,
  31. pipeline_for_compile_forward_ge_graph_for_case_by_case_config_exception)
  32. from ....mindspore_test_framework.pipeline.gradient.compile_gradient \
  33. import pipeline_for_compile_grad_ge_graph_for_case_by_case_config
  34. class InputBackward(nn.Cell):
  35. def __init__(self, network):
  36. super(InputBackward, self).__init__()
  37. self.network = network
  38. self.network.set_train()
  39. self.grad = C.grad_all_with_sens
  40. def construct(self, x1, x2, x3, sens):
  41. return self.grad(self.network)(x1, x2, x3, sens)
  42. class NetForTupleInput(nn.Cell):
  43. def __init__(self, op):
  44. super(NetForTupleInput, self).__init__()
  45. self.op = op
  46. def construct(self, x1, x2):
  47. return self.op((x1, x2))
  48. class StridedSlicessdNet(nn.Cell):
  49. def __init__(self):
  50. super(StridedSlicessdNet, self).__init__()
  51. self.rank = P.Rank()
  52. def construct(self, x1):
  53. return P.StridedSlice(1, 1, 0, self.rank(x1), 0)(x1, (0, 0), (0, 0), (1, 1))
  54. class NetForConcat(nn.Cell):
  55. def __init__(self):
  56. super(NetForConcat, self).__init__()
  57. self.concat = P.Concat()
  58. def construct(self, x1):
  59. return self.concat((x1, x1))
  60. class NetForConcat1(nn.Cell):
  61. def __init__(self):
  62. super(NetForConcat1, self).__init__()
  63. self.concat = P.Concat()
  64. def construct(self, x1, x2):
  65. return self.concat((x1, x2))
  66. class NetForPackInput(nn.Cell):
  67. def __init__(self, op):
  68. super(NetForPackInput, self).__init__()
  69. self.op = op
  70. self.mul = P.Mul()
  71. def construct(self, *args):
  72. t = ()
  73. for element in args:
  74. t = t + (self.mul(element, element),)
  75. return self.op(t)
  76. class NetForUnpackInput(nn.Cell):
  77. def __init__(self, op):
  78. super(NetForUnpackInput, self).__init__()
  79. self.op = op
  80. self.mul = P.Mul()
  81. def construct(self, x1):
  82. return self.op((self.mul(x1, x1)))
  83. class NetForFlatten(nn.Cell):
  84. def __init__(self):
  85. super(NetForFlatten, self).__init__()
  86. self.flatten = P.Flatten()
  87. def construct(self, x, y):
  88. return self.flatten(x) + y
  89. class NetForFlatten0D(nn.Cell):
  90. def __init__(self):
  91. super(NetForFlatten0D, self).__init__()
  92. self.flatten = P.Flatten()
  93. def construct(self, x):
  94. return self.flatten(x)
  95. class NetForFlattenComposed(nn.Cell):
  96. # make flatten op together with other ops for testing flatten grad
  97. def __init__(self):
  98. super(NetForFlattenComposed, self).__init__()
  99. self.flatten = P.Flatten()
  100. def construct(self, x, y):
  101. return self.flatten(x + x) + y
  102. class ArgmaxNet(nn.Cell):
  103. def __init__(self):
  104. super(ArgmaxNet, self).__init__()
  105. self.argmax = P.Argmax(axis=1)
  106. def construct(self, input_):
  107. return self.argmax(input_)
  108. class ArgminNet(nn.Cell):
  109. def __init__(self):
  110. super(ArgminNet, self).__init__()
  111. self.argmin = P.Argmin(axis=1)
  112. def construct(self, input_):
  113. return self.argmin(input_)
  114. class CumSumNet(nn.Cell):
  115. def __init__(self):
  116. super(CumSumNet, self).__init__()
  117. self.cumsum = P.CumSum()
  118. self.axis = 1
  119. def construct(self, input_):
  120. return self.cumsum(input_, self.axis)
  121. class SummaryNet(nn.Cell):
  122. def __init__(self):
  123. super(SummaryNet, self).__init__()
  124. self.s = P.ScalarSummary()
  125. self.add = P.TensorAdd()
  126. def construct(self, x, y):
  127. self.s("x1", x)
  128. return self.add(x, y)
  129. class HistogramSummaryNet(nn.Cell):
  130. def __init__(self):
  131. super(HistogramSummaryNet, self).__init__()
  132. self.summary = P.HistogramSummary()
  133. self.add = P.TensorAdd()
  134. def construct(self, x, y):
  135. out = self.add(x, y)
  136. string_in = "out"
  137. self.summary(string_in, out)
  138. return out
  139. class ScatterUpdate(nn.Cell):
  140. """ScatterUpdate net definition"""
  141. def __init__(self, ref_shape, dtype=np.float32, use_locking=False):
  142. super(ScatterUpdate, self).__init__()
  143. self.scatter_update = P.ScatterUpdate(use_locking)
  144. self.ref = Parameter(Tensor(np.ones(ref_shape, dtype)), name="ref")
  145. def construct(self, indices, updates):
  146. out = self.scatter_update(self.ref, indices, updates)
  147. return out
  148. class ScatterMax(nn.Cell):
  149. """ScatterMax net definition"""
  150. def __init__(self, dtype=np.float32, use_locking=False):
  151. super(ScatterMax, self).__init__()
  152. self.scatter_max = P.ScatterMax(use_locking)
  153. self.ref = Parameter(Tensor(np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], dtype)), name="ref")
  154. def construct(self, indices, updates):
  155. out = self.scatter_max(self.ref, indices, updates)
  156. return out
  157. class ScatterMin(nn.Cell):
  158. """ScatterMin net definition"""
  159. def __init__(self, dtype=np.float32, use_locking=False):
  160. super(ScatterMin, self).__init__()
  161. self.scatter_min = P.ScatterMin(use_locking)
  162. self.ref = Parameter(Tensor(np.array([[-1.0, 2.0, 3.0], [-4.0, 1.0, 6.0]], dtype)), name="ref")
  163. def construct(self, indices, updates):
  164. out = self.scatter_min(self.ref, indices, updates)
  165. return out
  166. class ScatterAdd(nn.Cell):
  167. """ScatterAdd net definition"""
  168. def __init__(self, ref_shape, dtype=np.float32, use_locking=False):
  169. super(ScatterAdd, self).__init__()
  170. self.scatter_add = P.ScatterAdd(use_locking)
  171. self.ref = Parameter(Tensor(np.ones(ref_shape, dtype)), name="ref")
  172. def construct(self, indices, updates):
  173. out = self.scatter_add(self.ref, indices, updates)
  174. return out
  175. class ScatterSub(nn.Cell):
  176. """ScatterSub net definition"""
  177. def __init__(self, ref_shape, dtype=np.float32, use_locking=False):
  178. super(ScatterSub, self).__init__()
  179. self.scatter_sub = P.ScatterSub(use_locking)
  180. self.ref = Parameter(Tensor(np.ones(ref_shape, dtype)), name="ref")
  181. def construct(self, indices, updates):
  182. out = self.scatter_sub(self.ref, indices, updates)
  183. return out
  184. class ScatterMul(nn.Cell):
  185. """ScatterMul net definition"""
  186. def __init__(self, ref_shape, dtype=np.float32, use_locking=False):
  187. super(ScatterMul, self).__init__()
  188. self.scatter_mul = P.ScatterMul(use_locking)
  189. self.ref = Parameter(Tensor(np.ones(ref_shape, dtype)), name="ref")
  190. def construct(self, indices, updates):
  191. out = self.scatter_mul(self.ref, indices, updates)
  192. return out
  193. class ScatterDiv(nn.Cell):
  194. """ScatterDiv net definition"""
  195. def __init__(self, ref_shape, dtype=np.float32, use_locking=False):
  196. super(ScatterDiv, self).__init__()
  197. self.scatter_div = P.ScatterDiv(use_locking)
  198. self.ref = Parameter(Tensor(np.ones(ref_shape, dtype)*10), name="ref")
  199. def construct(self, indices, updates):
  200. out = self.scatter_div(self.ref, indices, updates)
  201. return out
  202. class ApplyFtrlNet(nn.Cell):
  203. def __init__(self):
  204. super(ApplyFtrlNet, self).__init__()
  205. self.apply_ftrl = P.ApplyFtrl()
  206. self.lr = 0.001
  207. self.l1 = 0.0
  208. self.l2 = 0.0
  209. self.lr_power = -0.5
  210. self.var = Parameter(Tensor(np.random.rand(3, 3).astype(np.float32)), name="var")
  211. self.accum = Parameter(Tensor(np.random.rand(3, 3).astype(np.float32)), name="accum")
  212. self.linear = Parameter(Tensor(np.random.rand(3, 3).astype(np.float32)), name="linear")
  213. def construct(self, grad):
  214. out = self.apply_ftrl(self.var, self.accum, self.linear, grad, self.lr, self.l1, self.l2, self.lr_power)
  215. return out
  216. class SparseApplyFtrlNet(nn.Cell):
  217. def __init__(self):
  218. super(SparseApplyFtrlNet, self).__init__()
  219. self.sparse_apply_ftrl = P.SparseApplyFtrl(lr=0.001, l1=0.0, l2=0.0, lr_power=-0.5)
  220. self.var = Parameter(Tensor(np.random.rand(3, 3).astype(np.float32)), name="var")
  221. self.accum = Parameter(Tensor(np.random.rand(3, 3).astype(np.float32)), name="accum")
  222. self.linear = Parameter(Tensor(np.random.rand(3, 3).astype(np.float32)), name="linear")
  223. def construct(self, grad, indices):
  224. out = self.sparse_apply_ftrl(self.var, self.accum, self.linear, grad, indices)
  225. return out
  226. class SparseApplyProximalAdagradNet(nn.Cell):
  227. def __init__(self):
  228. super(SparseApplyProximalAdagradNet, self).__init__()
  229. self.sparse_apply_proximal_adagrad = P.SparseApplyProximalAdagrad()
  230. self.var = Parameter(Tensor(np.random.rand(3, 3).astype(np.float32)), name="var")
  231. self.accum = Parameter(Tensor(np.random.rand(3, 3).astype(np.float32)), name="accum")
  232. self.lr = 0.01
  233. self.l1 = 0.0
  234. self.l2 = 0.0
  235. def construct(self, grad, indices):
  236. out = self.sparse_apply_proximal_adagrad(self.var, self.accum, self.lr, self.l1, self.l2, grad, indices)
  237. return out
  238. class ApplyProximalAdagradNet(nn.Cell):
  239. def __init__(self):
  240. super(ApplyProximalAdagradNet, self).__init__()
  241. self.apply_proximal_adagrad = P.ApplyProximalAdagrad()
  242. self.var = Parameter(Tensor(np.random.rand(3, 3).astype(np.float32)), name="var")
  243. self.accum = Parameter(Tensor(np.random.rand(3, 3).astype(np.float32)), name="accum")
  244. self.lr = 0.01
  245. self.l1 = 0.0
  246. self.l2 = 0.0
  247. def construct(self, grad):
  248. out = self.apply_proximal_adagrad(self.var, self.accum, self.lr, self.l1, self.l2, grad)
  249. return out
  250. class ApplyAdaMaxNet(nn.Cell):
  251. def __init__(self):
  252. super(ApplyAdaMaxNet, self).__init__()
  253. self.apply_ada_max = P.ApplyAdaMax()
  254. self.beta1_power = 0.9
  255. self.lr = 0.001
  256. self.beta1 = 0.9
  257. self.beta2 = 0.99
  258. self.epsilon = 1e-10
  259. self.var = Parameter(Tensor(np.random.rand(3, 3).astype(np.float32)), name="var")
  260. self.m = Parameter(Tensor(np.random.rand(3, 3).astype(np.float32)), name="m")
  261. self.v = Parameter(Tensor(np.random.rand(3, 3).astype(np.float32)), name="v")
  262. def construct(self, grad):
  263. out = self.apply_ada_max(self.var, self.m, self.v, self.beta1_power, self.lr,
  264. self.beta1, self.beta2, self.epsilon, grad)
  265. return out
  266. class ApplyAdadeltaNet(nn.Cell):
  267. def __init__(self):
  268. super(ApplyAdadeltaNet, self).__init__()
  269. self.apply_adadelta = P.ApplyAdadelta()
  270. self.lr = 0.001
  271. self.rho = 0.0
  272. self.epsilon = 1e-6
  273. self.var = Parameter(Tensor(np.random.rand(3, 3).astype(np.float32)), name="var")
  274. self.accum = Parameter(Tensor(np.random.rand(3, 3).astype(np.float32)), name="accum")
  275. self.accum_update = Parameter(Tensor(np.random.rand(3, 3).astype(np.float32)), name="accum_update")
  276. def construct(self, grad):
  277. out = self.apply_adadelta(self.var, self.accum, self.accum_update, self.lr, self.rho, self.epsilon, grad)
  278. return out
  279. class ApplyAdagradNet(nn.Cell):
  280. def __init__(self):
  281. super(ApplyAdagradNet, self).__init__()
  282. self.apply_adagrad = P.ApplyAdagrad()
  283. self.lr = 0.001
  284. self.var = Parameter(Tensor(np.random.rand(3, 3).astype(np.float32)), name="var")
  285. self.accum = Parameter(Tensor(np.random.rand(3, 3).astype(np.float32)), name="accum")
  286. def construct(self, grad):
  287. out = self.apply_adagrad(self.var, self.accum, self.lr, grad)
  288. return out
  289. class ApplyAdagradV2Net(nn.Cell):
  290. def __init__(self):
  291. super(ApplyAdagradV2Net, self).__init__()
  292. self.apply_adagrad_v2 = P.ApplyAdagradV2(epsilon=1e-6)
  293. self.lr = 0.001
  294. self.var = Parameter(Tensor(np.random.rand(3, 3).astype(np.float32)), name="var")
  295. self.accum = Parameter(Tensor(np.random.rand(3, 3).astype(np.float32)), name="accum")
  296. def construct(self, grad):
  297. out = self.apply_adagrad_v2(self.var, self.accum, self.lr, grad)
  298. return out
  299. class ApplyAddSignNet(nn.Cell):
  300. def __init__(self):
  301. super(ApplyAddSignNet, self).__init__()
  302. self.apply_add_sign = P.ApplyAddSign()
  303. self.lr = 0.001
  304. self.alpha = 1.0
  305. self.sign_decay = 0.99
  306. self.beta = 0.99
  307. self.var = Parameter(Tensor(np.random.rand(3, 3).astype(np.float32)), name="var")
  308. self.m = Parameter(Tensor(np.random.rand(3, 3).astype(np.float32)), name="m")
  309. def construct(self, grad):
  310. out = self.apply_add_sign(self.var, self.m, self.lr, self.alpha, self.sign_decay, self.beta, grad)
  311. return out
  312. class ApplyPowerSignNet(nn.Cell):
  313. def __init__(self):
  314. super(ApplyPowerSignNet, self).__init__()
  315. self.apply_power_sign = P.ApplyPowerSign()
  316. self.lr = 0.001
  317. self.logbase = np.e
  318. self.sign_decay = 0.99
  319. self.beta = 0.99
  320. self.var = Parameter(Tensor(np.random.rand(3, 3).astype(np.float32)), name="var")
  321. self.m = Parameter(Tensor(np.random.rand(3, 3).astype(np.float32)), name="m")
  322. def construct(self, grad):
  323. out = self.apply_power_sign(self.var, self.m, self.lr, self.logbase, self.sign_decay, self.beta, grad)
  324. return out
  325. class ApplyGradientDescentNet(nn.Cell):
  326. def __init__(self):
  327. super(ApplyGradientDescentNet, self).__init__()
  328. self.apply_gradient_descent = P.ApplyGradientDescent()
  329. self.alpha = 0.001
  330. self.var = Parameter(Tensor(np.random.rand(3, 3).astype(np.float32)), name="var")
  331. def construct(self, delta):
  332. out = self.apply_gradient_descent(self.var, self.alpha, delta)
  333. return out
  334. class ApplyProximalGradientDescentNet(nn.Cell):
  335. def __init__(self):
  336. super(ApplyProximalGradientDescentNet, self).__init__()
  337. self.apply_proximal_gradient_descent = P.ApplyProximalGradientDescent()
  338. self.alpha = 0.001
  339. self.l1 = 0.0
  340. self.l2 = 0.0
  341. self.var = Parameter(Tensor(np.random.rand(3, 3).astype(np.float32)), name="var")
  342. def construct(self, delta):
  343. out = self.apply_proximal_gradient_descent(self.var, self.alpha, self.l1, self.l2, delta)
  344. return out
  345. class SparseApplyAdagradNet(nn.Cell):
  346. def __init__(self):
  347. super(SparseApplyAdagradNet, self).__init__()
  348. self.sparse_apply_adagrad = P.SparseApplyAdagrad(lr=0.01)
  349. self.var = Parameter(Tensor(np.random.rand(3, 3).astype(np.float32)), name="var")
  350. self.accum = Parameter(Tensor(np.random.rand(3, 3).astype(np.float32)), name="accum")
  351. def construct(self, grad, indices):
  352. out = self.sparse_apply_adagrad(self.var, self.accum, grad, indices)
  353. return out
  354. class ApplyRMSNet(nn.Cell):
  355. def __init__(self):
  356. super(ApplyRMSNet, self).__init__()
  357. self.apply_rms = P.ApplyRMSProp()
  358. self.lr = 0.001
  359. self.rho = 0.0
  360. self.momentum = 0.0
  361. self.epsilon = 1e-10
  362. self.var = Parameter(Tensor(np.random.rand(3, 3).astype(np.float32)), name="var")
  363. self.ms = Parameter(Tensor(np.random.rand(3, 3).astype(np.float32)), name="ms")
  364. self.moment = Parameter(Tensor(np.random.rand(3, 3).astype(np.float32)), name="moment")
  365. def construct(self, grad):
  366. out = self.apply_rms(self.var, self.ms, self.moment, self.lr, grad, self.rho, self.momentum, self.epsilon)
  367. return out
  368. class InplaceAddNet(nn.Cell):
  369. def __init__(self):
  370. super(InplaceAddNet, self).__init__()
  371. self.inplace_add = P.InplaceAdd(indices=(0, 1))
  372. def construct(self, x, v):
  373. out = self.inplace_add(x, v)
  374. return out
  375. class InplaceSubNet(nn.Cell):
  376. def __init__(self):
  377. super(InplaceSubNet, self).__init__()
  378. self.inplace_sub = P.InplaceSub(indices=(0, 1))
  379. def construct(self, x, v):
  380. out = self.inplace_sub(x, v)
  381. return out
  382. class NormalNet(nn.Cell):
  383. def __init__(self, shape=None, mean=0.0, stddev=1.0, seed=0):
  384. super(NormalNet, self).__init__()
  385. self.normal = P.Normal(seed=seed)
  386. self.shape = shape
  387. self.mean = Tensor(mean, mstype.float32)
  388. self.stddev = Tensor(stddev, mstype.float32)
  389. def construct(self):
  390. out = self.normal(self.shape, self.mean, self.stddev)
  391. return out
  392. class StridedSliceNet(nn.Cell):
  393. def __init__(self):
  394. super(StridedSliceNet, self).__init__()
  395. self.begins = (1, 2, 3, 2, 1)
  396. self.ends = (5, 6, 7, 8, 9)
  397. self.strides = (1, 2, 3, 2, 1)
  398. self.strided_slice_0 = P.StridedSlice(begin_mask=3, end_mask=5, ellipsis_mask=4,
  399. shrink_axis_mask=2, new_axis_mask=8)
  400. self.strided_slice_1 = P.StridedSlice(begin_mask=5, end_mask=2, ellipsis_mask=2,
  401. shrink_axis_mask=6, new_axis_mask=10)
  402. self.strided_slice_2 = P.StridedSlice(begin_mask=3, end_mask=3, ellipsis_mask=4,
  403. shrink_axis_mask=5, new_axis_mask=13)
  404. self.strided_slice_3 = P.StridedSlice(begin_mask=0, end_mask=0, ellipsis_mask=4,
  405. shrink_axis_mask=12, new_axis_mask=15)
  406. self.const_0 = Tensor(np.ones([6, 8, 9, 1, 8], np.float32))
  407. self.const_1 = Tensor(np.ones([5, 7, 8, 1, 8], np.float32))
  408. self.const_2 = Tensor(np.ones([1, 3, 7, 8, 9, 1, 8], np.float32))
  409. self.const_3 = Tensor(np.ones([1, 1, 6, 7, 8, 9, 1, 8], np.float32))
  410. def construct(self, x):
  411. out_0 = self.strided_slice_0(x, self.begins, self.ends, self.strides) + self.const_0
  412. out_1 = self.strided_slice_1(x, self.begins, self.ends, self.strides) + self.const_1
  413. out_2 = self.strided_slice_2(x, self.begins, self.ends, self.strides) + self.const_2
  414. out_3 = self.strided_slice_3(x, self.begins, self.ends, self.strides) + self.const_3
  415. return out_0, out_1, out_2, out_3
  416. def test_strided_slice_const():
  417. class StridedSLiceConstNet(nn.Cell):
  418. """StridedSLiceConstNet net definition"""
  419. def __init__(self):
  420. super(StridedSLiceConstNet, self).__init__()
  421. self.begins = (0, 2, -5, 2, 1)
  422. self.ends = (0, 6, 9, 8, 9)
  423. self.strides = (1, 2, 1, 2, 1)
  424. self.strided_slice = P.StridedSlice(begin_mask=2,
  425. end_mask=6,
  426. ellipsis_mask=4,
  427. shrink_axis_mask=6,
  428. new_axis_mask=18)
  429. def construct(self, x):
  430. out = self.strided_slice(x, self.begins, self.ends, self.strides)
  431. return out
  432. net = StridedSLiceConstNet()
  433. context.set_context(mode=context.GRAPH_MODE, save_graphs=True)
  434. x = Tensor(np.ones([6, 7, 8, 9, 10]), mstype.float32)
  435. ret = net(x)
  436. assert ret.shape == (0, 1, 7, 8, 9, 3, 1)
  437. assert (ret.asnumpy() == np.array([], np.float32).reshape([0, 1, 7, 8, 9, 3, 1])).all()
  438. test_case_math_ops = [
  439. ('BitwiseAnd', {
  440. 'block': P.BitwiseAnd(),
  441. 'desc_inputs': [Tensor(np.array([0, 0, 1, -1, 1, 1, 1]), mstype.int16),
  442. Tensor(np.array([0, 1, 1, -1, -1, 2, 3]), mstype.int16)],
  443. 'skip': ['backward']}),
  444. ('BitwiseAnd_1', {
  445. 'block': P.BitwiseAnd(),
  446. 'desc_inputs': [Tensor(np.array([[1, 2, 3], [-1, -2, -3]]), mstype.int16),
  447. Tensor(np.array([1, 1, 1]), mstype.int16)],
  448. 'skip': ['backward']}),
  449. ('BitwiseOr', {
  450. 'block': P.BitwiseOr(),
  451. 'desc_inputs': [Tensor(np.array([0, 0, 1, -1, 1, 1, 1]), mstype.int16),
  452. Tensor(np.array([0, 1, 1, -1, -1, 2, 3]), mstype.int16)],
  453. 'skip': ['backward']}),
  454. ('BitwiseOr_1', {
  455. 'block': P.BitwiseOr(),
  456. 'desc_inputs': [Tensor(np.array([[1, 2, 3], [-1, -2, -3]]), mstype.int16),
  457. Tensor(np.array([1, 1, 1]), mstype.int16)],
  458. 'skip': ['backward']}),
  459. ('BitwiseXor', {
  460. 'block': P.BitwiseXor(),
  461. 'desc_inputs': [Tensor(np.array([0, 0, 1, -1, 1, 1, 1]), mstype.int16),
  462. Tensor(np.array([0, 1, 1, -1, -1, 2, 3]), mstype.int16)],
  463. 'skip': ['backward']}),
  464. ('BitwiseXor_1', {
  465. 'block': P.BitwiseXor(),
  466. 'desc_inputs': [Tensor(np.array([[1, 2, 3], [-1, -2, -3]]), mstype.int16),
  467. Tensor(np.array([1, 1, 1]), mstype.int16)],
  468. 'skip': ['backward']}),
  469. ('Neg', {
  470. 'block': P.Neg(),
  471. 'desc_inputs': [[1, 3, 4, 4]],
  472. 'desc_bprop': [[1, 3, 4, 4]]}),
  473. ('Sub', {
  474. 'block': P.Sub(),
  475. 'desc_inputs': [[3, 5], [2, 3, 3, 5]],
  476. 'desc_bprop': [[2, 3, 3, 5]]}),
  477. ('TensorAdd', {
  478. 'block': P.TensorAdd(),
  479. 'desc_inputs': [[3, 5], [2, 3, 3, 5]],
  480. 'desc_bprop': [[2, 3, 3, 5]]}),
  481. ('Mul0', {
  482. 'block': P.Mul(),
  483. 'desc_inputs': [[2, 3, 3, 5], [2, 3, 3, 5]],
  484. 'desc_bprop': [[2, 3, 3, 5]]}),
  485. ('Mul1', {
  486. 'block': P.Mul(),
  487. 'desc_inputs': [[2, 3, 1, 1], [2, 3, 3, 5]],
  488. 'desc_bprop': [[2, 3, 3, 5]]}),
  489. ('Mul2', {
  490. 'block': P.Mul(),
  491. 'desc_inputs': [[2, 3, 3, 5], [2, 3, 1, 1]],
  492. 'desc_bprop': [[2, 3, 3, 5]],
  493. 'skip': ['backward']}),
  494. ('Mul3', {
  495. 'block': P.Mul(),
  496. 'desc_inputs': [[3, 5], [2, 3, 3, 5]],
  497. 'desc_bprop': [[2, 3, 3, 5]],
  498. 'skip': ['backward']}),
  499. ('Mul4', {
  500. 'block': P.Mul(),
  501. 'desc_inputs': [[2, 3, 3, 5], [3, 5]],
  502. 'desc_bprop': [[2, 3, 3, 5]],
  503. 'skip': ['backward']}),
  504. ('Add0', {
  505. 'block': P.TensorAdd(),
  506. 'desc_inputs': [[2, 3, 3, 5], [2, 3, 3, 5]],
  507. 'desc_bprop': [[2, 3, 3, 5]]}),
  508. ('Add1', {
  509. 'block': P.TensorAdd(),
  510. 'desc_inputs': [[3, 5], [2, 3, 3, 5]],
  511. 'desc_bprop': [[2, 3, 3, 5]],
  512. 'skip': ['backward']}),
  513. ('Add2', {
  514. 'block': P.TensorAdd(),
  515. 'desc_inputs': [[2, 3, 3, 5], [3, 5]],
  516. 'desc_bprop': [[2, 3, 3, 5]],
  517. 'skip': ['backward']}),
  518. ('Add3', {
  519. 'block': P.TensorAdd(),
  520. 'desc_inputs': [[2, 3, 1, 1], [2, 3, 3, 5]],
  521. 'desc_bprop': [[2, 3, 3, 5]],
  522. 'skip': ['backward']}),
  523. ('Add4', {
  524. 'block': P.TensorAdd(),
  525. 'desc_inputs': [[2, 3, 3, 5], [2, 3, 1, 1]],
  526. 'desc_bprop': [[2, 3, 3, 5]],
  527. 'skip': ['backward']}),
  528. ('Minimum', {
  529. 'block': P.Minimum(),
  530. 'desc_inputs': [[2, 3, 3, 5], [2, 3, 3, 5]],
  531. 'desc_bprop': [[2, 3, 3, 5]]}),
  532. ('Pow_0', {
  533. 'block': P.Pow(),
  534. 'desc_const': [2.0],
  535. 'desc_inputs': [[2, 3, 3, 5]],
  536. 'desc_bprop': [[2, 3, 3, 5]]}),
  537. ('Pow_1', {
  538. 'block': P.Pow(),
  539. 'desc_inputs': [[3, 5], [2, 3, 3, 5]],
  540. 'desc_bprop': [[2, 3, 3, 5]]}),
  541. ('Exp', {
  542. 'block': P.Exp(),
  543. 'desc_inputs': [[2, 3]],
  544. 'desc_bprop': [[2, 3]]}),
  545. ('Expm1', {
  546. 'block': P.Expm1(),
  547. 'desc_inputs': [[2, 3]],
  548. 'desc_bprop': [[2, 3]]}),
  549. ('Erf', {
  550. 'block': P.Erf(),
  551. 'desc_inputs': [Tensor(np.array([-2, -1, 0, 1, 2]).astype(np.float16))],
  552. 'desc_bprop': [Tensor(np.array([-2, -1, 0, 1, 2]).astype(np.float16))]}),
  553. ('Floor', {
  554. 'block': P.Floor(),
  555. 'desc_inputs': [[2, 512, 56, 56]],
  556. 'desc_bprop': [[2, 512, 56, 56]],
  557. 'skip': ['backward']}),
  558. ('Ceil', {
  559. 'block': P.Ceil(),
  560. 'desc_inputs': [[2, 512, 56, 56]],
  561. 'desc_bprop': [[2, 512, 56, 56]],
  562. 'skip': ['backward']}),
  563. ('InplaceAdd', {
  564. 'block': InplaceAddNet(),
  565. 'desc_inputs': [Tensor(np.array([[1, 2], [3, 4], [5, 6]]).astype(np.float32)),
  566. Tensor(np.array([[0.5, 1], [1, 1.5]]).astype(np.float32))],
  567. 'skip': ['backward']}),
  568. ('InplaceSub', {
  569. 'block': InplaceSubNet(),
  570. 'desc_inputs': [Tensor(np.array([[1, 2], [3, 4], [5, 6]]).astype(np.float32)),
  571. Tensor(np.array([[0.5, 1], [1, 1.5]]).astype(np.float32))],
  572. 'skip': ['backward']}),
  573. ('ACos', {
  574. 'block': P.ACos(),
  575. 'desc_inputs': [Tensor(np.array([2., 3.]).astype(np.float32))],
  576. 'desc_bprop': [Tensor(np.array([2., 3.]).astype(np.float32))]}),
  577. ('ACosGrad', {
  578. 'block': G.ACosGrad(),
  579. 'desc_inputs': [[2, 3], [2, 3]],
  580. 'skip': ['backward']}),
  581. ('Acosh', {
  582. 'block': P.Acosh(),
  583. 'desc_inputs': [Tensor(np.array([2., 3.]).astype(np.float32))],
  584. 'desc_bprop': [Tensor(np.array([2., 3.]).astype(np.float32))]}),
  585. ('AcoshGrad', {
  586. 'block': G.AcoshGrad(),
  587. 'desc_inputs': [[2, 3], [2, 3]],
  588. 'skip': ['backward']}),
  589. ('Sin', {
  590. 'block': P.Sin(),
  591. 'desc_inputs': [[2, 3]],
  592. 'desc_bprop': [[2, 3]]}),
  593. ('Asin', {
  594. 'block': P.Asin(),
  595. 'desc_inputs': [[2, 3]],
  596. 'desc_bprop': [[2, 3]]}),
  597. ('Asinh', {
  598. 'block': P.Asinh(),
  599. 'desc_inputs': [[3, 4, 5]],
  600. 'desc_bprop': [[3, 4, 5]]}),
  601. ('Reciprocal', {
  602. 'block': P.Reciprocal(),
  603. 'desc_inputs': [[2, 3, 3, 5]],
  604. 'desc_bprop': [[2, 3, 3, 5]]}),
  605. ('Minimum_0', {
  606. 'block': P.Minimum(),
  607. 'desc_inputs': [[2, 3, 3, 5], [3, 3, 5]],
  608. 'desc_bprop': [[2, 3, 3, 5]]}),
  609. ('Maximum', {
  610. 'block': P.Maximum(),
  611. 'desc_inputs': [[2, 3, 3, 5], [2, 3, 3, 5]],
  612. 'desc_bprop': [[2, 3, 3, 5]]}),
  613. ('Maximum_0', {
  614. 'block': P.Maximum(),
  615. 'desc_inputs': [[3, 5], [2, 3, 3, 5]],
  616. 'desc_bprop': [[2, 3, 3, 5]]}),
  617. ('MaximumGrad', {
  618. 'block': G.MaximumGrad(),
  619. 'desc_inputs': [[2, 3, 3, 5], [2, 3, 3, 5], [2, 3, 3, 5]],
  620. 'skip': ['backward']}),
  621. ('MinimumGrad', {
  622. 'block': G.MinimumGrad(),
  623. 'desc_inputs': [[2, 3, 3, 5], [2, 3, 3, 5], [2, 3, 3, 5]],
  624. 'skip': ['backward']}),
  625. ('StridedSlice', {
  626. 'block': P.StridedSlice(),
  627. 'desc_const': [(0, 1, 2, 1),
  628. (2, 3, 3, 4),
  629. (1, 1, 1, 1)],
  630. 'desc_inputs': [[2, 3, 3, 5]],
  631. 'desc_bprop': [[2, 2, 1, 3]]}),
  632. ('Slice_1', {
  633. 'block': P.Slice(),
  634. 'desc_const': [(0, 1, 2, 1),
  635. (1, 1, 1, 2)],
  636. 'desc_inputs': [[2, 3, 3, 5]],
  637. 'desc_bprop': [[1, 1, 1, 2]]}),
  638. ('StridedSliceGrad', {
  639. 'block': G.StridedSliceGrad(),
  640. 'desc_const': [(64, 1, 1024),
  641. (0, 1, 0),
  642. (64, 2, 1024),
  643. (1, 1, 1)],
  644. 'desc_inputs': [[64, 128, 1024]],
  645. 'skip': ['backward']}),
  646. ('RandomChoiceWithMask', {
  647. 'block': P.RandomChoiceWithMask(256),
  648. 'desc_inputs': [Tensor(np.random.rand(24000, 4).astype(np.bool_))],
  649. 'desc_bprop': [[256, 4], [256, 4]],
  650. 'skip': ['backward']}),
  651. ('LessEqual', {
  652. 'block': P.LessEqual(),
  653. 'desc_inputs': [Tensor(np.random.rand(4).astype(np.float16)),
  654. Tensor(np.random.rand(4).astype(np.float16))],
  655. 'skip': ['backward']}),
  656. ('Less', {
  657. 'block': P.Less(),
  658. 'desc_inputs': [[2, 1, 4, 5], [2, 1, 4, 5]],
  659. 'desc_bprop': [Tensor(np.zeros((2, 1, 4, 5), np.bool_))],
  660. 'skip': ['backward']}),
  661. ('RealDiv_0', {
  662. 'block': P.RealDiv(),
  663. 'desc_const': [Tensor(2048.0), Tensor(0.0)],
  664. 'desc_inputs': [],
  665. 'skip': ['backward']}),
  666. ('RealDiv', {
  667. 'block': P.RealDiv(),
  668. 'desc_inputs': [[4], Tensor(np.ones(4).astype(np.float32))],
  669. 'desc_bprop': [[4]]}),
  670. ('RealDiv_1', {
  671. 'block': P.RealDiv(),
  672. 'desc_inputs': [[512, 1024], [512, 1024]],
  673. 'desc_bprop': [[512, 1024]]}),
  674. ('FloorDiv', {
  675. 'block': P.FloorDiv(),
  676. 'desc_inputs': [Tensor(np.random.rand(4).astype(np.float16)),
  677. Tensor(np.random.rand(4).astype(np.float16))],
  678. 'skip': ['backward']}),
  679. ('FloorMod', {
  680. 'block': P.FloorMod(),
  681. 'desc_inputs': [[3, 4, 5], [2, 3, 4, 5]],
  682. 'desc_bprop': [[2, 3, 4, 5]]}),
  683. ('identity', {
  684. 'block': ops.functional.identity,
  685. 'desc_inputs': [[2, 2]],
  686. 'skip': ['backward']}),
  687. ('MatMul_1', {
  688. 'block': P.MatMul(transpose_a=False, transpose_b=False),
  689. 'desc_inputs': [[1024, 160], [160, 1024]],
  690. 'desc_bprop': [[1024, 1024]]}),
  691. ('MatMul_2', {
  692. 'block': P.MatMul(transpose_a=True, transpose_b=True),
  693. 'desc_inputs': [[160, 1024], [1024, 160]],
  694. 'desc_bprop': [[1024, 1024]]}),
  695. ('Sub', {
  696. 'block': P.Sub(),
  697. 'desc_inputs': [[3], [3]],
  698. 'desc_bprop': [[3]]}),
  699. ('TruncatedNormal', {
  700. 'block': P.TruncatedNormal(),
  701. 'desc_const': [(1, 2, 3)],
  702. 'desc_inputs': [],
  703. 'skip': ['backward'],
  704. 'add_fake_input': True}),
  705. ('Select', {
  706. 'block': P.Select(),
  707. 'desc_inputs': [Tensor(np.array([[True, False, False], [False, True, True]])),
  708. [2, 3], [2, 3]],
  709. 'desc_bprop': [[2, 3]]}),
  710. ('Rank', {
  711. 'block': P.Rank(),
  712. 'desc_inputs': [[2, 3]],
  713. 'skip': ['backward']}),
  714. ('InvertPermutation', {
  715. 'block': P.InvertPermutation(),
  716. 'desc_const': [(0, 3, 1, 2)],
  717. 'desc_inputs': [],
  718. 'skip': ['backward']}),
  719. ('Square', {
  720. 'block': P.Square(),
  721. 'desc_inputs': [[4]],
  722. 'desc_bprop': [[4]]}),
  723. ('Rsqrt', {
  724. 'block': P.Rsqrt(),
  725. 'desc_inputs': [[4]],
  726. 'desc_bprop': [[4]]}),
  727. ('Sqrt', {
  728. 'block': P.Sqrt(),
  729. 'desc_inputs': [[4]],
  730. 'desc_bprop': [[4]]}),
  731. ('RealDiv', {
  732. 'block': P.RealDiv(),
  733. 'desc_inputs': [[4, 5], [2, 3, 4, 5]],
  734. 'desc_bprop': [[2, 3, 4, 5]]}),
  735. ('Div', {
  736. 'block': P.Div(),
  737. 'desc_inputs': [[4, 5], [2, 3, 4, 5]],
  738. 'desc_bprop': [[2, 3, 4, 5]]}),
  739. ('Equal', {
  740. 'block': P.Equal(),
  741. 'desc_inputs': [[3, 4, 5], [4, 5]],
  742. 'desc_bprop': [Tensor(np.zeros((3, 4, 5), np.bool_))]}),
  743. ('NotEqual', {
  744. 'block': P.NotEqual(),
  745. 'desc_inputs': [[4, 1], [2, 3, 4, 5]],
  746. 'desc_bprop': [Tensor(np.ones((2, 3, 4, 5), np.bool_))]}),
  747. ('NotEqual_0', {
  748. 'block': P.NotEqual(),
  749. 'desc_inputs': [1, [2, 3, 4, 5]],
  750. 'desc_bprop': [Tensor(np.ones((2, 3, 4, 5), np.bool_))],
  751. 'skip': ['backward']}),
  752. ('ApproximateEqual', {
  753. 'block': P.ApproximateEqual(),
  754. 'desc_inputs': [[3, 4, 5], [3, 4, 5]],
  755. 'desc_bprop': [Tensor(np.zeros((3, 4, 5), np.bool_))]}),
  756. ('Greater', {
  757. 'block': P.Greater(),
  758. 'desc_inputs': [[2, 3, 4, 1], [4, 5]],
  759. 'desc_bprop': [Tensor(np.ones((2, 3, 4, 5), np.bool_))]}),
  760. ('GreaterEqual', {
  761. 'block': P.GreaterEqual(),
  762. 'desc_inputs': [[2, 3, 4, 1], [4, 5]],
  763. 'desc_bprop': [Tensor(np.ones((2, 3, 4, 5), np.bool_))]}),
  764. ('LogicalNot', {
  765. 'block': P.LogicalNot(),
  766. 'desc_inputs': [Tensor(np.zeros((3, 4, 5), np.bool_))],
  767. 'desc_bprop': [Tensor(np.ones((3, 4, 5), np.bool_))]}),
  768. ('LogicalAnd', {
  769. 'block': P.LogicalAnd(),
  770. 'desc_inputs': [Tensor(np.zeros((2, 3, 4), np.bool_)), Tensor(np.ones((1), np.bool_))],
  771. 'desc_bprop': [Tensor(np.zeros((2, 3, 4), np.bool_))]}),
  772. ('LogicalOr', {
  773. 'block': P.LogicalOr(),
  774. 'desc_inputs': [Tensor(np.zeros((3, 4, 5), np.bool_)), Tensor(np.ones((3, 1, 1), np.bool_))],
  775. 'desc_bprop': [Tensor(np.zeros((3, 4, 5), np.bool_))]}),
  776. ('NpuAllocFloatStatus', {
  777. 'block': P.NPUAllocFloatStatus(),
  778. 'desc_inputs': [],
  779. 'add_fack_input': True,
  780. 'fack_input_type': np.float32,
  781. 'desc_bprop': [Tensor(np.zeros([8]).astype(np.float32))],
  782. 'skip': ['backward']}),
  783. ('NpuGetFloatStatus', {
  784. 'block': P.NPUGetFloatStatus(),
  785. 'desc_inputs': [Tensor(np.zeros([8]).astype(np.float32))],
  786. 'desc_bprop': [Tensor(np.zeros([8]).astype(np.float32))],
  787. 'skip': ['backward']}),
  788. ('NpuClearFloatStatus', {
  789. 'block': P.NPUClearFloatStatus(),
  790. 'desc_inputs': [Tensor(np.zeros([8]).astype(np.float32))],
  791. 'desc_bprop': [Tensor(np.zeros([8]).astype(np.float32))],
  792. 'skip': ['backward']}),
  793. ('CheckValid', {
  794. 'block': P.CheckValid(),
  795. 'desc_inputs': [[20000, 4], [3]],
  796. 'desc_bprop': [[20000]],
  797. 'skip': ['backward']}),
  798. ('NMSWithMask', {
  799. 'block': P.NMSWithMask(0.5),
  800. 'desc_inputs': [[128, 5]],
  801. 'desc_bprop': [[128, 5], [128], [128]],
  802. 'skip': ['backward']}),
  803. ('Abs', {
  804. 'block': P.Abs(),
  805. 'desc_inputs': [[4]],
  806. 'desc_bprop': [[4]]}),
  807. ('CumSum', {
  808. 'block': CumSumNet(),
  809. 'desc_inputs': [Tensor(np.array([[3, 4, 6, 10], [1, 6, 7, 9], [4, 3, 8, 7], [1, 3, 7, 9]]).astype(np.float32))],
  810. 'desc_bprop': [Tensor(np.array([[3, 4, 6, 10], [1, 6, 7, 9], [4, 3, 8, 7],
  811. [1, 3, 7, 9]]).astype(np.float32))]}),
  812. ('ReduceSum_3', {
  813. 'block': P.ReduceSum(),
  814. 'desc_const': [0],
  815. 'desc_inputs': [[3, 2]],
  816. 'desc_bprop': [[2]]}),
  817. ('ReduceSum_4', {
  818. 'block': P.ReduceSum(keep_dims=True),
  819. 'desc_const': [0],
  820. 'desc_inputs': [[3, 2]],
  821. 'desc_bprop': [[1, 2]]}),
  822. ('ReduceSum_5', {
  823. 'block': P.ReduceSum(keep_dims=True),
  824. 'desc_inputs': [[2, 3, 4]],
  825. 'desc_bprop': [[1, 1, 1]]}),
  826. ('ReduceSum_6', {
  827. 'block': P.ReduceSum(),
  828. 'desc_inputs': [[2, 3, 4]],
  829. 'desc_bprop': [[1]]}),
  830. ('Sum_0', {
  831. 'block': P.ReduceSum(),
  832. 'desc_const': [(1,)],
  833. 'desc_inputs': [[3, 2]],
  834. 'desc_bprop': [[3]]}),
  835. ('Sum_1', {
  836. 'block': P.ReduceSum(keep_dims=True),
  837. 'desc_const': [(1,)],
  838. 'desc_inputs': [[3, 2]],
  839. 'desc_bprop': [[3, 1]]}),
  840. ('Sum_2', {
  841. 'block': P.ReduceSum(),
  842. 'desc_const': [(0, 1)],
  843. 'desc_inputs': [[3, 2]],
  844. 'desc_bprop': [[1]]}),
  845. ('Sum_3', {
  846. 'block': P.ReduceSum(),
  847. 'desc_const': [0],
  848. 'desc_inputs': [[3, 2]],
  849. 'desc_bprop': [[2]]}),
  850. ('Sum_4', {
  851. 'block': P.ReduceSum(keep_dims=True),
  852. 'desc_const': [0],
  853. 'desc_inputs': [[3, 2]],
  854. 'desc_bprop': [[1, 2]]}),
  855. ('Sum_5', {
  856. 'block': P.ReduceSum(keep_dims=True),
  857. 'desc_const': [()],
  858. 'desc_inputs': [[2, 3, 4]],
  859. 'desc_bprop': [[1, 1, 1]]}),
  860. ('Sum_6', {
  861. 'block': P.ReduceSum(),
  862. 'desc_const': [()],
  863. 'desc_inputs': [[2, 3, 4]],
  864. 'desc_bprop': [[1]]}),
  865. ('Sign', {
  866. 'block': P.Sign(),
  867. 'desc_inputs': [[3]],
  868. 'desc_bprop': [[3]]}),
  869. ('Round', {
  870. 'block': P.Round(),
  871. 'desc_inputs': [[3]],
  872. 'desc_bprop': [[3]]}),
  873. ('Atan2', {
  874. 'block': P.Atan2(),
  875. 'desc_inputs': [Tensor(np.array([0, 1]).astype(np.float32)),
  876. Tensor(np.array([1, 1]).astype(np.float32))],
  877. 'desc_bprop': [[2]]}),
  878. ('SquareSumAll', {
  879. 'block': P.SquareSumAll(),
  880. 'desc_inputs': [Tensor(np.array([0, 1, 4, 5]).astype(np.float32)),
  881. Tensor(np.array([1, 1, 3, 7]).astype(np.float32))],
  882. 'skip': ['backward']}),
  883. ('Cos', {
  884. 'block': P.Cos(),
  885. 'desc_inputs': [[2, 3]],
  886. 'desc_bprop': [[2, 3]]}),
  887. ('ReduceAll', {
  888. 'block': P.ReduceAll(),
  889. 'desc_const': [1],
  890. 'desc_inputs': [Tensor(np.array([[True, False], [True, True]]))],
  891. 'desc_bprop': []}),
  892. ('BesselI0e', {
  893. 'block': P.BesselI0e(),
  894. 'desc_inputs': [[2, 3]],
  895. 'desc_bprop': [[2, 3]]}),
  896. ('BesselI1e', {
  897. 'block': P.BesselI1e(),
  898. 'desc_inputs': [[2, 3]],
  899. 'desc_bprop': [[2, 3]]}),
  900. ('Atan', {
  901. 'block': P.Atan(),
  902. 'desc_inputs': [[2, 3]],
  903. 'desc_bprop': [[2, 3]]}),
  904. ('AtanGrad', {
  905. 'block': G.AtanGrad(),
  906. 'desc_inputs': [[2, 3], [2, 3]],
  907. 'skip': ['backward']}),
  908. ('Atanh', {
  909. 'block': P.Atanh(),
  910. 'desc_inputs': [[2, 3]],
  911. 'desc_bprop': [[2, 3]]}),
  912. ('Cosh', {
  913. 'block': P.Cosh(),
  914. 'desc_inputs': [[3, 4, 5]],
  915. 'desc_bprop': [[3, 4, 5]]}),
  916. ('Sinh', {
  917. 'block': P.Sinh(),
  918. 'desc_inputs': [[3, 4, 5]],
  919. 'desc_bprop': [[3, 4, 5]]}),
  920. ('Inv', {
  921. 'block': P.Inv(),
  922. 'desc_inputs': [[21, 9, 12, 5]],
  923. 'desc_bprop': [[21, 9, 12, 5]]}),
  924. ('Invert', {
  925. 'block': P.Invert(),
  926. 'desc_inputs': [Tensor(np.array([[24, 4, 13, 9], [1, 5, 10, 8]]).astype(np.int16))],
  927. 'desc_bprop': [],
  928. 'skip': ['backward']}),
  929. ('HistogramFixedWidth', {
  930. 'block': P.HistogramFixedWidth(5),
  931. 'desc_inputs': [Tensor([-1.0, 0.0, 1.5, 2.0, 5.0, 15], mstype.float16), Tensor([0.0, 5.0], mstype.float16)],
  932. 'desc_bprop': [],
  933. 'skip': ['backward']}),
  934. ('Normal', {
  935. 'block': NormalNet((3, 2, 4), 0.0, 1.0, 0),
  936. 'desc_inputs': [],
  937. 'skip': ['backward']}),
  938. ('Mod', {
  939. 'block': P.Mod(),
  940. 'desc_inputs': [[3, 4, 5], [2, 3, 4, 5]],
  941. 'desc_bprop': [[2, 3, 4, 5]]}),
  942. ]
  943. test_case_nn_ops = [
  944. ('BiasAdd', {
  945. 'block': P.BiasAdd(),
  946. 'desc_inputs': [[1, 3, 3, 3], [3]],
  947. 'desc_bprop': [[1, 3, 3, 3]]}),
  948. ('BiasAddGrad', {
  949. 'block': G.BiasAddGrad(),
  950. 'desc_inputs': [[1, 3, 3, 3]],
  951. 'skip': ['backward']}),
  952. ('Gelu', {
  953. 'block': P.Gelu(),
  954. 'desc_inputs': [[1, 3, 4, 4]],
  955. 'desc_bprop': [[1, 3, 4, 4]]}),
  956. ('GeluGrad', {
  957. 'block': G.GeluGrad(),
  958. 'desc_inputs': [[2, 2], [2, 2], [2, 2]],
  959. 'desc_bprop': [[2, 2]],
  960. 'skip': ['backward']}),
  961. ('Tanh', {
  962. 'block': P.Tanh(),
  963. 'desc_inputs': [[1, 3, 4, 4]],
  964. 'desc_bprop': [[1, 3, 4, 4]]}),
  965. ('TanhGrad', {
  966. 'block': G.TanhGrad(),
  967. 'desc_inputs': [[1, 3, 4, 4], [1, 3, 4, 4]],
  968. 'desc_bprop': [[1, 3, 4, 4]],
  969. 'skip': ['backward']}),
  970. ('ReLU', {
  971. 'block': P.ReLU(),
  972. 'desc_inputs': [[1, 3, 4, 4]],
  973. 'desc_bprop': [[1, 3, 4, 4]]}),
  974. ('ReLU6', {
  975. 'block': P.ReLU6(),
  976. 'desc_inputs': [[1, 3, 4, 4]],
  977. 'desc_bprop': [[1, 3, 4, 4]]}),
  978. ('ReLUV2', {
  979. 'block': P.ReLUV2(),
  980. 'desc_inputs': [[1, 3, 4, 4]],
  981. 'desc_bprop': [[1, 3, 4, 4], ([1, 1, 4, 4, 2], {'dtype': np.uint8})]}),
  982. ('ReLUGrad', {
  983. 'block': G.ReluGrad(),
  984. 'desc_inputs': [[1, 3, 4, 4], [1, 3, 4, 4]],
  985. 'skip': ['backward']}),
  986. ('Softplus', {
  987. 'block': P.Softplus(),
  988. 'desc_inputs': [[1, 3, 4, 4]],
  989. 'desc_bprop': [[1, 3, 4, 4]]}),
  990. ('SoftplusGrad', {
  991. 'block': G.SoftplusGrad(),
  992. 'desc_inputs': [[1, 3, 4, 4], [1, 3, 4, 4]],
  993. 'skip': ['backward']}),
  994. ('Elu', {
  995. 'block': P.Elu(),
  996. 'desc_inputs': [[2, 3, 4]],
  997. 'desc_bprop': [[2, 3, 4]]}),
  998. ('EluGrad', {
  999. 'block': G.EluGrad(),
  1000. 'desc_inputs': [[2, 3, 4], [2, 3, 4]],
  1001. 'desc_bprop': [[2, 3, 4]],
  1002. 'skip': ['backward']}),
  1003. ('Sigmoid', {
  1004. 'block': P.Sigmoid(),
  1005. 'desc_inputs': [[1, 3, 4, 4]],
  1006. 'desc_bprop': [[1, 3, 4, 4]]}),
  1007. ('MaxPool', {
  1008. 'block': P.MaxPool(ksize=(2, 2), strides=(2, 2), padding="VALID"),
  1009. 'desc_inputs': [[100, 3, 28, 28]],
  1010. 'desc_bprop': [[100, 3, 14, 14]]}),
  1011. ('MaxPoolGrad', {
  1012. 'block': G.MaxPoolGrad(ksize=(2, 2), strides=(2, 2), padding="VALID"),
  1013. 'desc_inputs': [[3, 4, 6, 6], [3, 4, 3, 3], [3, 4, 3, 3]],
  1014. 'desc_bprop': [[3, 4, 6, 6]],
  1015. 'skip': ['backward']}),
  1016. ('AvgPool', {
  1017. 'block': P.AvgPool(ksize=(2, 2), strides=(2, 2), padding="VALID"),
  1018. 'desc_inputs': [[100, 3, 28, 28]],
  1019. 'desc_bprop': [[100, 3, 14, 14]]}),
  1020. ('AvgPoolGrad', {
  1021. 'block': G.AvgPoolGrad(ksize=(2, 2), strides=(2, 2), padding="VALID"),
  1022. 'desc_const': [(3, 4, 6, 6)],
  1023. 'const_first': True,
  1024. 'desc_inputs': [[3, 4, 6, 6]],
  1025. 'desc_bprop': [[3, 4, 6, 6]],
  1026. 'skip': ['backward']}),
  1027. ('MaxPoolWithArgmax', {
  1028. 'block': P.MaxPoolWithArgmax(ksize=2, strides=2),
  1029. 'desc_inputs': [[128, 32, 32, 64]],
  1030. 'desc_bprop': [[128, 32, 16, 32], ([128, 32, 4, 33], {'dtype': np.uint16})]}),
  1031. ('SoftmaxCrossEntropyWithLogits', {
  1032. 'block': P.SoftmaxCrossEntropyWithLogits(),
  1033. 'desc_inputs': [[1, 10], [1, 10]],
  1034. 'desc_bprop': [[1], [1, 10]],
  1035. 'skip': ['backward_exec']}),
  1036. ('Flatten', {
  1037. 'block': P.Flatten(),
  1038. 'desc_inputs': [[128, 32, 32, 64]],
  1039. 'desc_bprop': [[128, 65536]]}),
  1040. ('LogSoftmax', {
  1041. 'block': P.LogSoftmax(),
  1042. 'desc_inputs': [[64, 2]],
  1043. 'desc_bprop': [[64, 2]]}),
  1044. ('LogSoftmaxGrad', {
  1045. 'block': G.LogSoftmaxGrad(),
  1046. 'desc_inputs': [[16, 1234], [16, 1234]],
  1047. 'desc_bprop': [[64, 2]],
  1048. 'skip': ['backward']}),
  1049. ('L2Normalize', {
  1050. 'block': P.L2Normalize(),
  1051. 'desc_inputs': [[2, 2]],
  1052. 'desc_bprop': [[2, 2]]}),
  1053. ('L2NormalizeGrad', {
  1054. 'block': G.L2NormalizeGrad(),
  1055. 'desc_inputs': [[2, 2], [2, 2], [2, 2]],
  1056. 'desc_bprop': [[2, 2]],
  1057. 'skip': ['backward']}),
  1058. ('LayerNorm', {
  1059. 'block': P.LayerNorm(),
  1060. 'desc_inputs': [[2, 16], [16], [16]],
  1061. 'desc_bprop': [[2, 16], [2, 1], [2, 1]]}),
  1062. ('LayerNormGrad', {
  1063. 'block': G.LayerNormGrad(),
  1064. 'desc_inputs': [[2, 16], [2, 16], [2, 16], [2, 16], [16]],
  1065. 'desc_bprop': [[2, 16], [16], [16]],
  1066. 'skip': ['backward']}),
  1067. ('FusedBatchNorm', {
  1068. 'block': P.FusedBatchNorm(),
  1069. 'desc_inputs': [[128, 64, 32, 64], [64], [64], [64], [64]],
  1070. 'desc_bprop': [[128, 64, 32, 64], [64], [64], [64], [64]],
  1071. 'skip': []}),
  1072. ('FusedBatchNormGrad', {
  1073. 'block': G.FusedBatchNormGrad(),
  1074. 'desc_inputs': [[128, 64, 32, 64], [128, 64, 32, 64], [64], [64], [64]],
  1075. 'desc_bprop': [[128, 64, 32, 64], [64], [64], [64], [64]],
  1076. 'skip': ['backward']}),
  1077. ('BatchNorm', {
  1078. 'block': P.BatchNorm(),
  1079. 'desc_inputs': [[128, 64, 32, 32], [64], [64], [64], [64]],
  1080. 'desc_bprop': [[128, 64, 32, 32], [64], [64], [64], [64]],
  1081. 'skip': []}),
  1082. ('BatchNormGrad', {
  1083. 'block': G.BatchNormGrad(),
  1084. 'desc_inputs': [[128, 64, 32, 32], [128, 64, 32, 32], [64], [64], [64]],
  1085. 'desc_bprop': [[128, 64, 32, 32], [64], [64], [64], [64]],
  1086. 'skip': ['backward']}),
  1087. ('BasicLSTMCell', {
  1088. 'block': P.BasicLSTMCell(keep_prob=1.0, forget_bias=1.0, state_is_tuple=True, activation='tanh'),
  1089. 'desc_inputs': [[128, 128], [128, 128], [128, 128], [512, 256, 1, 1], [512, 1, 1, 1]],
  1090. 'desc_bprop': [[128, 128], [128, 128], [128, 128], [128, 128], [128, 128], [128, 128], [128, 128]],
  1091. 'skip': []}),
  1092. ('TopK', {
  1093. 'block': P.TopK(),
  1094. 'desc_const': [5],
  1095. 'desc_inputs': [[20, 20, 10]],
  1096. 'desc_bprop': [[20, 20, 5]],
  1097. 'skip': ['backward']}),
  1098. ('GatherV2_0', {
  1099. 'block': P.GatherV2(),
  1100. 'desc_const': [0],
  1101. 'desc_inputs': [[3, 1, 2], Tensor(np.array([0, 1]).astype(np.int32))],
  1102. 'desc_bprop': [[2, 1, 2]]}),
  1103. ('GatherV2_1', {
  1104. 'block': P.GatherV2(),
  1105. 'desc_const': [2],
  1106. 'desc_inputs': [[3, 1, 3], Tensor(np.array([0, 1]).astype(np.int32))],
  1107. 'desc_bprop': [[3, 1, 2]]}),
  1108. ('GatherV2_2', {
  1109. 'block': P.GatherV2(),
  1110. 'desc_const': [0],
  1111. 'desc_inputs': [[3, 1, 3], Tensor(np.array([[0, 1], [0, 1], [0, 1]]).astype(np.int32))],
  1112. 'desc_bprop': [[3, 2, 1, 3]]}),
  1113. ('GatherV2_3', {
  1114. 'block': P.GatherV2(),
  1115. 'desc_const': [2],
  1116. 'desc_inputs': [[3, 1, 3], Tensor(np.array([[0, 1], [0, 1], [0, 1]]).astype(np.int32))],
  1117. 'desc_bprop': [[3, 1, 3, 2]]}),
  1118. ('GatherV2_4', {
  1119. 'block': P.GatherV2(),
  1120. 'desc_const': [1],
  1121. 'desc_inputs': [[32, 5, 1024], Tensor(np.array([3]).astype(np.int32))],
  1122. 'desc_bprop': [[32, 1, 1024]]}),
  1123. ('GatherV2_5', {
  1124. 'block': P.GatherV2(),
  1125. 'desc_const': [-1],
  1126. 'desc_inputs': [[3, 1, 3], Tensor(np.array([0, 1]).astype(np.int32))],
  1127. 'desc_bprop': [[3, 1, 2]]}),
  1128. ('GatherV2_6', {
  1129. 'block': P.GatherV2(),
  1130. 'desc_const': [0],
  1131. 'desc_inputs': [[1152], Tensor(np.array(10).astype(np.int32))],
  1132. 'desc_bprop': [Tensor(np.array(10).astype(np.float32))]}),
  1133. ('SparseGatherV2_0', {
  1134. 'block': P.SparseGatherV2(),
  1135. 'desc_const': [0],
  1136. 'desc_inputs': [[3, 1, 2], Tensor(np.array([0, 1]).astype(np.int32))],
  1137. 'desc_bprop': [[2, 1, 2]]}),
  1138. ('Range', {
  1139. 'block': inner.Range(1.0, 5.0),
  1140. 'desc_inputs': [Tensor(np.ones([10]).astype(np.float32))],
  1141. 'desc_bprop': [[10]]}),
  1142. ('UnsortedSegmentSum', {
  1143. 'block': P.UnsortedSegmentSum(),
  1144. 'desc_const': [1280],
  1145. 'desc_inputs': [[1280, 1024], Tensor(np.ones(1280).astype(np.int32))],
  1146. 'desc_bprop': [[8192, 1024]],
  1147. 'skip': ['backward']}),
  1148. ('UnsortedSegmentSum_1', {
  1149. 'block': P.UnsortedSegmentSum(),
  1150. 'desc_const': [4],
  1151. 'desc_inputs': [[3, 2, 1, 3], Tensor(np.array([[0, 1], [0, 1], [0, 1]]).astype(np.int32))],
  1152. 'desc_bprop': [[4, 1, 3]],
  1153. 'skip': ['backward']}),
  1154. ('UnsortedSegmentMin', {
  1155. 'block': P.UnsortedSegmentMin(),
  1156. 'desc_const': [4],
  1157. 'desc_inputs': [[3, 2, 1, 3], Tensor(np.array([1, 2, 3]).astype(np.int32))],
  1158. 'desc_bprop': [[4, 2, 1, 3]]}),
  1159. ('DropoutGenMask', {
  1160. 'block': P.DropoutGenMask(),
  1161. 'desc_const': [(2, 2), Tensor(0.5, mstype.float32)],
  1162. 'desc_inputs': [],
  1163. 'desc_bprop': [Tensor(np.ones(1).astype(np.int8))],
  1164. 'skip': ['backward']}),
  1165. ('DropoutDoMask', {
  1166. 'block': P.DropoutDoMask(),
  1167. 'desc_const': [Tensor(0.5)],
  1168. 'desc_inputs': [[64, 12, 128, 128], Tensor(np.ones(1572864).astype(np.uint8))],
  1169. 'desc_bprop': [[64, 12, 128, 128]]}),
  1170. ('Dropout', {
  1171. 'block': nn.Dropout(0.5),
  1172. 'desc_inputs': [[64, 12, 128, 128]],
  1173. 'desc_bprop': [[64, 12, 128, 128]]}),
  1174. ('ReduceMean0', {
  1175. 'block': P.ReduceMean(),
  1176. 'desc_const': [(2,)],
  1177. 'desc_inputs': [[3, 2, 2]],
  1178. 'desc_bprop': [[3, 2]]}),
  1179. ('ReduceMean1', {
  1180. 'block': P.ReduceMean(),
  1181. 'desc_const': [2],
  1182. 'desc_inputs': [[3, 2, 2]],
  1183. 'desc_bprop': [[3, 2]]}),
  1184. ('All', {
  1185. 'block': P.ReduceAll(),
  1186. 'desc_const': [(1,)],
  1187. 'desc_inputs': [Tensor(np.ones([3, 2]).astype(np.bool_))],
  1188. 'desc_bprop': [[3]],
  1189. 'skip': ['backward']}),
  1190. ('DescConst', {
  1191. 'block': Tensor(np.array([2], np.float32)),
  1192. 'desc_inputs': [],
  1193. 'desc_bprop': [[1]],
  1194. 'skip': ['backward'],
  1195. 'add_fake_input': True}),
  1196. ('Fill', {
  1197. 'block': P.Fill(),
  1198. 'desc_const': [mstype.float32, (2, 3), 1.0],
  1199. 'desc_inputs': [],
  1200. 'desc_bprop': [[2, 3]],
  1201. 'skip': ['backward'],
  1202. 'add_fake_input': True}),
  1203. ('OnesLike', {
  1204. 'block': P.OnesLike(),
  1205. 'desc_inputs': [Tensor(np.array([[0, 1], [2, 1]]).astype(np.int32))],
  1206. 'desc_bprop': [Tensor(np.array([[1, 1], [1, 1]]).astype(np.int32))]
  1207. }),
  1208. ('ZerosLike', {
  1209. 'block': P.ZerosLike(),
  1210. 'desc_inputs': [Tensor(np.array([[0, 1], [2, 1]]).astype(np.int32))],
  1211. 'desc_bprop': [Tensor(np.array([[1, 1], [1, 1]]).astype(np.int32))]
  1212. }),
  1213. ('Softmax', {
  1214. 'block': P.Softmax(),
  1215. 'desc_inputs': [[5, 5]],
  1216. 'desc_bprop': [[5, 5]]}),
  1217. ('DepthwiseConv2dNative_1', {
  1218. 'block': P.DepthwiseConv2dNative(3, (3, 3), pad_mode="pad", pad=1, stride=2),
  1219. 'desc_inputs': [[10, 32, 32, 32], [1, 32, 3, 3]],
  1220. 'desc_bprop': [[10, 32, 16, 16]]}),
  1221. ('DepthwiseConv2dNative_2', {
  1222. 'block': P.DepthwiseConv2dNative(1, (3, 3), pad_mode="same", pad=0, stride=1),
  1223. 'desc_inputs': [[2592, 2048, 4, 4], [1, 2048, 3, 3]],
  1224. 'desc_bprop': [[2592, 2048, 4, 4]]}),
  1225. ('SigmoidCrossEntropyWithLogits', {
  1226. 'block': P.SigmoidCrossEntropyWithLogits(),
  1227. 'desc_inputs': [[128, 10], [128, 10]],
  1228. 'desc_bprop': [[128, 10]]}),
  1229. ('Pad', {
  1230. 'block': P.Pad(((1, 2), (2, 3))),
  1231. 'desc_inputs': [[7, 7]],
  1232. 'desc_bprop': [[10, 12]]}),
  1233. ('BinaryCrossEntropy', {
  1234. 'block': P.BinaryCrossEntropy(),
  1235. 'desc_inputs': [[1, 2, 3], [1, 2, 3], [1, 2, 3]],
  1236. 'desc_bprop': []}),
  1237. ('SparseApplyAdagrad', {
  1238. 'block': SparseApplyAdagradNet(),
  1239. 'desc_inputs': [[3, 3], Tensor(np.ones((3,), np.int32))],
  1240. 'desc_bprop': [[3, 3], [3, 3]],
  1241. 'skip': ['backward']}),
  1242. ('SparseApplyFtrl', {
  1243. 'block': SparseApplyFtrlNet(),
  1244. 'desc_inputs': [[3, 3], Tensor(np.ones((3,), np.int32))],
  1245. 'skip': ['backward']}),
  1246. ('ApplyProximalAdagrad', {
  1247. 'block': ApplyProximalAdagradNet(),
  1248. 'desc_inputs': [[3, 3]],
  1249. 'skip': ['backward']}),
  1250. ('SparseApplyProximalAdagrad', {
  1251. 'block': SparseApplyProximalAdagradNet(),
  1252. 'desc_inputs': [[3, 3], Tensor(np.ones((3,), np.int32))],
  1253. 'skip': ['backward']}),
  1254. ('ApplyAdaMax', {
  1255. 'block': ApplyAdaMaxNet(),
  1256. 'desc_inputs': [[3, 3]],
  1257. 'skip': ['backward']}),
  1258. ('ApplyAdadelta', {
  1259. 'block': ApplyAdadeltaNet(),
  1260. 'desc_inputs': [[3, 3]],
  1261. 'skip': ['backward']}),
  1262. ('ApplyAdagrad', {
  1263. 'block': ApplyAdagradNet(),
  1264. 'desc_inputs': [[3, 3]],
  1265. 'skip': ['backward']}),
  1266. ('ApplyAdagradV2', {
  1267. 'block': ApplyAdagradV2Net(),
  1268. 'desc_inputs': [[3, 3]],
  1269. 'skip': ['backward']}),
  1270. ('ApplyAddSign', {
  1271. 'block': ApplyAddSignNet(),
  1272. 'desc_inputs': [[3, 3]],
  1273. 'skip': ['backward']}),
  1274. ('ApplyPowerSign', {
  1275. 'block': ApplyPowerSignNet(),
  1276. 'desc_inputs': [[3, 3]],
  1277. 'skip': ['backward']}),
  1278. ('ApplyGradientDescent', {
  1279. 'block': ApplyGradientDescentNet(),
  1280. 'desc_inputs': [[3, 3]],
  1281. 'skip': ['backward']}),
  1282. ('ApplyProximalGradientDescent', {
  1283. 'block': ApplyProximalGradientDescentNet(),
  1284. 'desc_inputs': [[3, 3]],
  1285. 'skip': ['backward']}),
  1286. ('Flatten_1', {
  1287. 'block': NetForFlatten(),
  1288. 'desc_inputs': [Tensor(np.ones([2, 3, 4]).astype(np.int32)), Tensor(np.ones([2, 12]).astype(np.int32))],
  1289. 'desc_bprop': [Tensor(np.ones([2, 12]).astype(np.int32))],
  1290. 'skip': ['backward']}),
  1291. ('Flatten_2', {
  1292. 'block': NetForFlatten(),
  1293. 'desc_inputs': [Tensor(np.ones([8]).astype(np.int32)), Tensor(np.ones([8, 3]).astype(np.int32))],
  1294. 'desc_bprop': [Tensor(np.ones([8, 3]).astype(np.int32))],
  1295. 'skip': ['backward']}),
  1296. ('Flatten_3', {
  1297. 'block': NetForFlattenComposed(),
  1298. 'desc_inputs': [Tensor(np.ones([2, 3, 4]).astype(np.int32)), Tensor(np.ones([2, 12]).astype(np.int32))],
  1299. 'desc_bprop': [Tensor(np.ones([2, 12]).astype(np.int32))],
  1300. 'skip': []}),
  1301. ('ArgmaxNet', {
  1302. 'block': ArgmaxNet(),
  1303. 'desc_inputs': [Tensor(np.array([[128, 32, 32, 64], [128, 32, 32, 64]]).astype(np.float16))],
  1304. 'desc_bprop': [Tensor(np.array([[128, 32, 32, 64], [128, 32, 32, 64]]).astype(np.float16))],
  1305. 'skip': ['backward']}),
  1306. ('ArgminNet', {
  1307. 'block': ArgminNet(),
  1308. 'desc_inputs': [Tensor(np.array([[128, 32, 32, 64], [128, 32, 32, 64]]).astype(np.float16))],
  1309. 'desc_bprop': [Tensor(np.array([[128, 32, 32, 64], [128, 32, 32, 64]]).astype(np.float16))],
  1310. 'skip': ['backward']}),
  1311. ('StridedSliceNet', {
  1312. 'block': StridedSliceNet(),
  1313. 'desc_inputs': [[6, 7, 8, 9, 10]],
  1314. 'skip': ['backward']}),
  1315. ('OneHot', {
  1316. 'block': P.OneHot(),
  1317. 'desc_const': [3, Tensor(1.0, mstype.float32), Tensor(0.0, mstype.float32)],
  1318. 'desc_inputs': [Tensor(np.array([64]).astype(np.int32))],
  1319. 'desc_bprop': [[1, 3]]}),
  1320. ('ReduceProd_0', {
  1321. 'block': P.ReduceProd(),
  1322. 'desc_const': [0],
  1323. 'desc_inputs': [[3, 2]],
  1324. 'desc_bprop': [[2]]}),
  1325. ('ReduceProd_1', {
  1326. 'block': P.ReduceProd(keep_dims=True),
  1327. 'desc_const': [0],
  1328. 'desc_inputs': [[3, 2]],
  1329. 'desc_bprop': [[1, 2]]}),
  1330. ('CumProd', {
  1331. 'block': P.CumProd(),
  1332. 'desc_const': [0],
  1333. 'desc_inputs': [[3, 2]],
  1334. 'desc_bprop': [[3, 2]]}),
  1335. ('ApplyFtrl', {
  1336. 'block': ApplyFtrlNet(),
  1337. 'desc_inputs': [[3, 3]],
  1338. 'desc_bprop': [3, 3],
  1339. 'skip': ['backward']}),
  1340. ('ApplyRMSProp', {
  1341. 'block': ApplyRMSNet(),
  1342. 'desc_inputs': [[3, 3]],
  1343. 'desc_bprop': [3, 3],
  1344. 'skip': ['backward']}),
  1345. ('ApplyCenteredRMSProp', {
  1346. 'block': P.ApplyCenteredRMSProp(),
  1347. 'desc_const': [0.9, 0.0, 1e-10, 0.001],
  1348. 'desc_inputs': [Tensor(1., mstype.float32), Tensor(2., mstype.float32), Tensor(1., mstype.float32),
  1349. Tensor(2., mstype.float32), Tensor(1., mstype.float32)],
  1350. 'desc_bprop': [1],
  1351. 'skip': ['backward']}),
  1352. ('CTCLoss', {
  1353. 'block': P.CTCLoss(),
  1354. 'desc_inputs': [Tensor(np.ones([6, 4, 6]).astype(np.float32)),
  1355. Tensor(np.array([[0, 1], [1, 0], [2, 3], [3, 2]]).astype(np.int64)),
  1356. Tensor(np.array([1, 2, 3, 4]).astype(np.int32)),
  1357. Tensor(np.array([6, 6, 6, 6]).astype(np.int32))],
  1358. 'desc_bprop': [[4], [6, 4, 6]]}),
  1359. ('L2Loss_1', {
  1360. 'block': P.L2Loss(),
  1361. 'desc_inputs': [Tensor(np.array([1, 2, 3, 4]), mstype.float32)],
  1362. 'desc_bprop': []}),
  1363. ('L2Loss_2', {
  1364. 'block': P.L2Loss(),
  1365. 'desc_inputs': [Tensor(np.array([[1, 1], [2, 2], [3, 3], [4, 4]]), mstype.float16)],
  1366. 'desc_bprop': []}),
  1367. ('ResizeBilinear', {
  1368. 'block': P.ResizeBilinear((5, 5)),
  1369. 'desc_inputs': [Tensor([[[[1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]]], mstype.float16)],
  1370. 'desc_bprop': [Tensor([[[[1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]]], mstype.float16)]}),
  1371. ('ResizeBilinearGrad', {
  1372. 'block': G.ResizeBilinearGrad(),
  1373. 'desc_inputs': [Tensor([[[[1, 2, 3, 4, 5]]]], mstype.float32), Tensor([[[[1, 2, 3, 4, 5]]]], mstype.float32)],
  1374. 'desc_bprop': [Tensor([[[[1, 2, 3, 4, 5]]]], mstype.float32)],
  1375. 'skip': ['backward']}),
  1376. ('ROIAlign', {
  1377. 'block': P.ROIAlign(7, 7, 0.03125, 2),
  1378. 'desc_inputs': [[2, 256, 192, 320], [1024, 5]],
  1379. 'desc_bprop': [[7, 7]]}),
  1380. ('ROIAlignGrad', {
  1381. 'block': G.ROIAlignGrad((1, 1, 1, 1), 2, 2, 0.5, 2),
  1382. 'desc_inputs': [[1, 1, 2, 2], [1, 5]],
  1383. 'desc_bprop': [[1, 1, 2, 2]],
  1384. 'skip': ['backward']}),
  1385. ('LARSUpdate', {
  1386. 'block': P.LARSUpdate(1e-05, 0.001, False),
  1387. 'desc_const': [0.0, 0.001],
  1388. 'desc_inputs': [[3, 3], [3, 3], [3, 3], [3, 3]],
  1389. 'desc_bprop': [3, 3],
  1390. 'skip': ['backward']}),
  1391. ('SGD', {
  1392. 'block': P.SGD(0.0, 0.0, False),
  1393. 'desc_inputs': [[3, 3], [3, 3], Tensor(0.001, mstype.float32), [3, 3], Tensor(0.1, mstype.float32), [3, 3]],
  1394. 'desc_bprop': [3, 3],
  1395. 'skip': ['backward']}),
  1396. ('BinaryCrossEntropy', {
  1397. 'block': P.BinaryCrossEntropy(),
  1398. 'desc_inputs': [Tensor([[0.3, 0.8], [0.4, 0.3]], mstype.float16),
  1399. Tensor([[0.4, 1.2], [-0.4, -0.9]], mstype.float16),
  1400. Tensor([[-1.4, -0.7], [0.9, 0.7]], mstype.float16)],
  1401. 'desc_bprop': []}),
  1402. ('BinaryCrossEntropyGrad', {
  1403. 'block': G.BinaryCrossEntropyGrad(),
  1404. 'desc_inputs': [Tensor([[0.3, 0.8], [0.4, 0.3]], mstype.float16),
  1405. Tensor([[0.4, 1.2], [-0.4, -0.9]], mstype.float16), Tensor(0.85, mstype.float16),
  1406. Tensor([[-1.4, -0.7], [0.9, 0.7]], mstype.float16)],
  1407. 'desc_bprop': [],
  1408. 'skip': ['backward']}),
  1409. ('DataFormatDimMap', {
  1410. 'block': P.DataFormatDimMap(),
  1411. 'desc_inputs': [Tensor([0, 1, 2, 3], mstype.int32)],
  1412. 'desc_bprop': [],
  1413. 'skip': ['backward']}),
  1414. ]
  1415. test_case_array_ops = [
  1416. ('SpaceToDepth', {
  1417. 'block': P.SpaceToDepth(2),
  1418. 'desc_inputs': [[1, 3, 2, 2]],
  1419. 'desc_bprop': [[1, 12, 1, 1]]}),
  1420. ('DepthToSpace', {
  1421. 'block': P.DepthToSpace(2),
  1422. 'desc_inputs': [[1, 12, 1, 1]],
  1423. 'desc_bprop': [[1, 3, 2, 2]]}),
  1424. ('Split', {
  1425. 'block': P.Split(1, 2),
  1426. 'desc_inputs': [Tensor(np.array([[1, 1, 1, 1], [2, 2, 2, 2]]))],
  1427. 'skip': ['backward']}),
  1428. ('Argmax', {
  1429. 'block': P.Argmax(),
  1430. 'desc_inputs': [[128, 32, 32, 64]],
  1431. 'desc_bprop': [0],
  1432. 'skip': ['backward']}),
  1433. ('Argmin', {
  1434. 'block': P.Argmin(),
  1435. 'desc_inputs': [[128, 32, 32, 64]],
  1436. 'desc_bprop': [1],
  1437. 'skip': ['backward']}),
  1438. ('ArgMaxWithValue', {
  1439. 'block': P.ArgMaxWithValue(),
  1440. 'desc_inputs': [[128, 32, 32, 64]],
  1441. 'desc_bprop': [[1], [1]],
  1442. 'skip': ['backward']}),
  1443. ('ArgMinWithValue', {
  1444. 'block': P.ArgMinWithValue(),
  1445. 'desc_inputs': [[128, 32, 32, 64]],
  1446. 'desc_bprop': [[1], [1]],
  1447. 'skip': ['backward']}),
  1448. ('Transpose_dim3', {
  1449. 'block': P.Transpose(),
  1450. 'desc_const': [(0, 2, 1)],
  1451. 'desc_inputs': [[1, 2, 3]],
  1452. 'desc_bprop': [[1, 3, 2]]}),
  1453. ('Transpose_dim4', {
  1454. 'block': P.Transpose(),
  1455. 'desc_const': [(0, 1, 2, 3)],
  1456. 'desc_inputs': [[1, 2, 3, 4]],
  1457. 'desc_bprop': [[1, 2, 4, 3]]}),
  1458. ('AddN', {
  1459. 'block': NetForTupleInput(P.AddN()),
  1460. 'desc_inputs': [[2, 3, 3, 5], [2, 3, 3, 5]],
  1461. 'desc_bprop': [[2, 3, 3, 5]],
  1462. 'skip': ['backward']}),
  1463. ('AccumulateNV2', {
  1464. 'block': NetForTupleInput(P.AccumulateNV2()),
  1465. 'desc_inputs': [[2, 3, 3, 5], [2, 3, 3, 5]],
  1466. 'desc_bprop': [[2, 3, 3, 5]],
  1467. 'skip': ['backward']}),
  1468. ('Shape', {
  1469. 'block': P.Shape(),
  1470. 'desc_inputs': [[3, 3, 2, 2]],
  1471. 'skip': ['backward']}),
  1472. ('Reshape', {
  1473. 'block': P.Reshape(),
  1474. 'desc_const': [(64,)],
  1475. 'desc_inputs': [[64, 1]],
  1476. 'desc_bprop': [[64]]}),
  1477. ('Cast', {
  1478. 'block': P.Cast(),
  1479. 'desc_const': [mstype.int32],
  1480. 'desc_inputs': [[2, 3, 4, 5]],
  1481. 'desc_bprop': [Tensor(np.ones((2, 3, 4, 5)).astype(np.int32))]}),
  1482. ('ExpandDims', {
  1483. 'block': P.ExpandDims(),
  1484. 'desc_const': [0],
  1485. 'desc_inputs': [[2, 2]],
  1486. 'desc_bprop': [[1, 2, 2]]}),
  1487. ('ExpandDims_1', {
  1488. 'block': P.ExpandDims(),
  1489. 'desc_const': [-1],
  1490. 'desc_inputs': [[2, 2]],
  1491. 'desc_bprop': [[2, 2, 1]]}),
  1492. ('Squeeze', {
  1493. 'block': P.Squeeze(2),
  1494. 'desc_inputs': [[3, 2, 1]],
  1495. 'desc_bprop': [[3, 2]]}),
  1496. ('Squeeze_0', {
  1497. 'block': P.Squeeze(),
  1498. 'desc_inputs': [[3, 1, 2, 1]],
  1499. 'desc_bprop': [[3, 2]]}),
  1500. ('Squeeze_1', {
  1501. 'block': P.Squeeze(),
  1502. 'desc_inputs': [[1, 1, 1, 1]],
  1503. 'desc_bprop': [1.0],
  1504. 'skip': ['backward']}),
  1505. ('Squeeze_2', {
  1506. 'block': P.Squeeze((2, 3)),
  1507. 'desc_inputs': [[3, 2, 1, 1]],
  1508. 'desc_bprop': [[3, 2]]}),
  1509. ('Size', {
  1510. 'block': P.Size(),
  1511. 'desc_inputs': [[2, 3, 5]],
  1512. 'skip': ['backward']}),
  1513. ('Tile_0', {
  1514. 'block': P.Tile(),
  1515. 'desc_const': [(1, 2)],
  1516. 'desc_inputs': [[64, 1]],
  1517. 'desc_bprop': [[64, 2]]}),
  1518. ('Tile_1', {
  1519. 'block': P.Tile(),
  1520. 'desc_const': [(1, 1)],
  1521. 'desc_inputs': [[64, 1]],
  1522. 'desc_bprop': [[64, 1]]}),
  1523. ('Tile_2', {
  1524. 'block': P.Tile(),
  1525. 'desc_const': [(2, 1, 1, 2)],
  1526. 'desc_inputs': [[2, 2, 2]],
  1527. 'desc_bprop': [[2, 2, 2, 4]]}),
  1528. ('ConcatV2_0', {
  1529. 'block': P.Concat(),
  1530. 'desc_inputs': [
  1531. (Tensor(np.array([[0, 1], [2, 1]]).astype(np.int32)),
  1532. Tensor(np.array([[0, 1], [2, 1]]).astype(np.int32)))],
  1533. 'desc_bprop': [([4, 2], {'dtype': np.int32})]}),
  1534. ('ConcatV2_1', {
  1535. 'block': P.Concat(axis=2),
  1536. 'desc_inputs': [(Tensor(np.array([[[0, 1, 2]], [[2, 1, 2]]]).astype(np.int32)),
  1537. Tensor(np.array([[[0, 1]], [[2, 1]]]).astype(np.int32)))],
  1538. 'desc_bprop': [([2, 1, 5], {'dtype': np.int32})]}),
  1539. ('ConcatV2_2', {
  1540. 'block': NetForConcat(),
  1541. 'desc_inputs': [[2, 2]],
  1542. 'desc_bprop': [[4, 2]]}),
  1543. ('ConcatV2_3', {
  1544. 'block': NetForConcat1(),
  1545. 'desc_inputs': [[2, 2], [2, 2]],
  1546. 'desc_bprop': [[4, 2]]}),
  1547. ('ConcatV2_4', {
  1548. 'block': P.Concat(axis=0),
  1549. 'desc_inputs': [
  1550. (Tensor(np.ones((3, 2, 3), np.float32)),
  1551. Tensor(np.ones((5, 2, 3), np.float32)),
  1552. Tensor(np.ones((6, 2, 3), np.float32)))],
  1553. 'desc_bprop': [[14, 2, 3]]}),
  1554. ('ConcatV2_5', {
  1555. 'block': P.Concat(axis=-1),
  1556. 'desc_inputs': [(Tensor(np.array([1], np.float32)),
  1557. Tensor(np.array([1], np.float32)),
  1558. Tensor(np.array([1], np.float32)))],
  1559. 'desc_bprop': [[3, ]]}),
  1560. ('Pack_0', {
  1561. 'block': NetForPackInput(P.Pack()),
  1562. 'desc_inputs': [[2, 2], [2, 2], [2, 2]],
  1563. 'desc_bprop': [[3, 2, 2]],
  1564. }),
  1565. ('Pack_1', {
  1566. 'block': NetForPackInput(P.Pack(axis=-2)),
  1567. 'desc_inputs': [[3, 2, 3], [3, 2, 3], [3, 2, 3]],
  1568. 'desc_bprop': [[3, 2, 3, 3]],
  1569. }),
  1570. ('Pack_2', {
  1571. 'block': NetForPackInput(P.Pack()),
  1572. 'desc_inputs': [[128, 128], [128, 128]],
  1573. 'desc_bprop': [[2, 128, 128]],
  1574. }),
  1575. ('Unpack_0', {
  1576. 'block': NetForUnpackInput(P.Unpack(axis=0)),
  1577. 'desc_inputs': [[2, 4]],
  1578. 'desc_bprop': [[4], [4]],
  1579. }),
  1580. ('Unpack_1', {
  1581. 'block': NetForUnpackInput(P.Unpack(axis=-1)),
  1582. 'desc_inputs': [Tensor(np.array([[1, 1, 1]], np.float32))],
  1583. 'desc_bprop': [[1], [1], [1]],
  1584. }),
  1585. ('Diag_1', {
  1586. 'block': P.Diag(),
  1587. 'desc_inputs': [[4]],
  1588. 'desc_bprop': [[4, 4]],
  1589. }),
  1590. ('Diag_2', {
  1591. 'block': P.Diag(),
  1592. 'desc_inputs': [[4, 4]],
  1593. 'desc_bprop': [[4, 4, 4, 4]],
  1594. }),
  1595. ('DiagPart_1', {
  1596. 'block': P.DiagPart(),
  1597. 'desc_inputs': [[4, 4]],
  1598. 'desc_bprop': [[4]],
  1599. }),
  1600. ('DiagPart_2', {
  1601. 'block': P.DiagPart(),
  1602. 'desc_inputs': [[4, 4, 4, 4]],
  1603. 'desc_bprop': [[4, 4]],
  1604. }),
  1605. ('SpaceToBatch_1', {
  1606. 'block': P.SpaceToBatch(2, [[0, 0], [0, 0]]),
  1607. 'desc_inputs': [[1, 3, 2, 2]],
  1608. 'desc_bprop': [[4, 3, 1, 1]],
  1609. }),
  1610. ('SpaceToBatch_2', {
  1611. 'block': P.SpaceToBatch(2, [[1, 1], [0, 4]]),
  1612. 'desc_inputs': [[1, 3, 2, 2]],
  1613. 'desc_bprop': [[4, 3, 2, 3]],
  1614. }),
  1615. ('BatchToSpace_1', {
  1616. 'block': P.BatchToSpace(2, [[0, 0], [0, 0]]),
  1617. 'desc_inputs': [[4, 3, 1, 1]],
  1618. 'desc_bprop': [[1, 3, 2, 2]],
  1619. }),
  1620. ('BatchToSpace_2', {
  1621. 'block': P.BatchToSpace(2, [[0, 0], [0, 1]]),
  1622. 'desc_inputs': [[4, 3, 1, 1]],
  1623. 'desc_bprop': [[1, 3, 2, 1]],
  1624. }),
  1625. ('UnsortedSegmentMin_1', {
  1626. 'block': P.UnsortedSegmentMin(),
  1627. 'desc_const': [2],
  1628. 'desc_inputs': [Tensor(np.array([[1, 2, 3], [4, 5, 6], [4, 2, 1]]).astype(np.float32)),
  1629. Tensor(np.array([0, 1, 1]).astype(np.int32))],
  1630. 'desc_bprop': [Tensor(np.array([[1, 2, 3], [4, 2, 1]]).astype(np.float32))]}),
  1631. ('BroadcastTo', {
  1632. 'block': P.BroadcastTo((2, 3)),
  1633. 'desc_inputs': [Tensor(np.array([1, 2, 3]).astype(np.float32))],
  1634. 'desc_bprop': [Tensor(np.array([[1, 2, 3], [1, 2, 3]]).astype(np.float32))]}),
  1635. ('InTopK', {
  1636. 'block': P.InTopK(2),
  1637. 'desc_inputs': [Tensor(np.array([[1, 2, 3], [2, 3, 6], [4, 2, 1]]).astype(np.float32)),
  1638. Tensor(np.array([2, 1, 2]).astype(np.int32))],
  1639. 'skip': ['backward'],
  1640. }),
  1641. ('InplaceUpdate', {
  1642. 'block': P.InplaceUpdate((0, 2)),
  1643. 'desc_inputs': [Tensor(np.arange(24).reshape(3, 4, 2).astype(np.float32)),
  1644. Tensor(np.arange(16).reshape(2, 4, 2).astype(np.float32))],
  1645. 'skip': ['backward'],
  1646. }),
  1647. ('ReverseSequence', {
  1648. 'block': P.ReverseSequence(1, 0),
  1649. 'desc_inputs': [Tensor(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]).astype(np.float32)),
  1650. Tensor(np.array([1, 2, 3]).astype(np.int32))],
  1651. 'desc_bprop': [[3, 3]]}),
  1652. ('LinSpace', {
  1653. 'block': inner.LinSpace(),
  1654. 'desc_inputs': [Tensor([5, 5.5], mstype.float32),
  1655. Tensor(1, mstype.float32),
  1656. Tensor(10, mstype.float32),
  1657. Tensor(5, mstype.int32)],
  1658. 'skip': ['backward'],
  1659. }),
  1660. ('MatrixDiag', {
  1661. 'block': inner.MatrixDiag(),
  1662. 'desc_inputs': [Tensor(np.array([1, -1]), mstype.float32),
  1663. Tensor(np.arange(-12, 0).reshape(3, 2, 2), mstype.float32)],
  1664. 'skip': ['backward'],
  1665. }),
  1666. ('MatrixDiagPart', {
  1667. 'block': inner.MatrixDiagPart(),
  1668. 'desc_inputs': [Tensor(np.arange(12).reshape(3, 2, 2), mstype.float32),
  1669. Tensor(np.arange(-12, 0).reshape(3, 2, 2), mstype.float32)],
  1670. 'skip': ['backward'],
  1671. }),
  1672. ('MatrixSetDiag', {
  1673. 'block': inner.MatrixSetDiag(),
  1674. 'desc_inputs': [Tensor(np.arange(12).reshape(3, 2, 2), mstype.float32),
  1675. Tensor(np.arange(6).reshape(3, 2), mstype.float32),
  1676. Tensor(np.arange(-12, 0).reshape(3, 2, 2), mstype.float32)],
  1677. 'skip': ['backward'],
  1678. }),
  1679. ]
  1680. test_case_other_ops = [
  1681. ('ScalarLog', {
  1682. 'block': F.scalar_log,
  1683. 'desc_const': [0.0],
  1684. 'desc_inputs': [],
  1685. 'desc_bprop': [1],
  1686. 'skip': ['backward']}),
  1687. ('BoundingBoxEncode', {
  1688. 'block': P.BoundingBoxEncode(means=(0.0, 0.0, 0.0, 0.0), stds=(1.0, 1.0, 1.0, 1.0)),
  1689. 'desc_inputs': [[256, 4], [256, 4]],
  1690. 'desc_bprop': [[256, 4]],
  1691. 'skip': ['backward']}),
  1692. ('BoundingBoxDecode', {
  1693. '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)),
  1694. 'desc_inputs': [[256, 4], [256, 4]],
  1695. 'desc_bprop': [[256, 4]],
  1696. 'skip': ['backward']}),
  1697. ('GatherNd', {
  1698. 'block': P.GatherNd(),
  1699. 'desc_inputs': (Tensor(np.ones((1, 3, 6, 6), np.float32)),
  1700. Tensor(np.ones((2, 4), np.int32))),
  1701. 'desc_bprop': [[2]]}),
  1702. ('ScatterNd', {
  1703. 'block': P.ScatterNd(),
  1704. 'desc_const': [(3, 3)],
  1705. 'desc_inputs': (Tensor(np.ones((2, 2), np.int32)),
  1706. Tensor(np.ones((2,), np.int32))),
  1707. 'desc_bprop': [([3, 3], {'dtype': np.int32})]}),
  1708. ('TensorScatterUpdate', {
  1709. 'block': P.TensorScatterUpdate(),
  1710. 'desc_inputs': (Tensor(np.arange(3 * 4 * 5).reshape((3, 4, 5)), mstype.float32),
  1711. Tensor(np.array([[0, 1], [1, 2]], np.int32)),
  1712. Tensor(np.ones([2, 5], np.float32) * 99)),
  1713. 'desc_bprop': [([3, 4, 5], {'dtype': np.float32})]}),
  1714. ('ScatterMaxUseLocking', {
  1715. 'block': ScatterMax(use_locking=True),
  1716. 'desc_inputs': (Tensor(np.array([1, 0], np.int32)),
  1717. Tensor(np.array([[5.0, 5.0, 5.0], [4.0, 4.0, 4.0]], np.float32))),
  1718. 'skip': ['backward']}),
  1719. ('ScatterMax1d', {
  1720. 'block': ScatterMax(),
  1721. 'desc_inputs': (Tensor(np.array([1, 0], np.int32)),
  1722. Tensor(np.array([[5.0, 5.0, 5.0], [4.0, 4.0, 4.0]], np.float32))),
  1723. 'skip': ['backward']}),
  1724. ('ScatterMaxF32', {
  1725. 'block': ScatterMax(),
  1726. 'desc_inputs': (Tensor(np.array([[0, 0], [1, 1]], np.int32)),
  1727. Tensor(np.ones([2, 2, 3], np.float32) * 99)),
  1728. 'skip': ['backward']}),
  1729. ('ScatterMaxF16', {
  1730. 'block': ScatterMax(np.float16),
  1731. 'desc_inputs': (Tensor(np.array([[0, 0], [1, 1]], np.int32)),
  1732. Tensor(np.ones([2, 2, 3], np.float16) * 99)),
  1733. 'skip': ['backward']}),
  1734. ('ScatterMaxI32', {
  1735. 'block': ScatterMax(np.int32),
  1736. 'desc_inputs': (Tensor(np.array([[0, 0], [1, 1]], np.int32)),
  1737. Tensor(np.ones([2, 2, 3], np.int32) * 99)),
  1738. 'skip': ['backward']}),
  1739. ('ScatterMinUseLocking', {
  1740. 'block': ScatterMin(use_locking=True),
  1741. 'desc_inputs': (Tensor(np.array([1, 0], np.int32)),
  1742. Tensor(np.ones([2, 3], np.float32))),
  1743. 'skip': ['backward']}),
  1744. ('ScatterMin1d', {
  1745. 'block': ScatterMin(),
  1746. 'desc_inputs': (Tensor(np.array([1, 0], np.int32)),
  1747. Tensor(np.ones([2, 3], np.float32))),
  1748. 'skip': ['backward']}),
  1749. ('ScatterMinF32', {
  1750. 'block': ScatterMin(),
  1751. 'desc_inputs': (Tensor(np.array([[0, 0], [1, 1]], np.int32)),
  1752. Tensor(np.ones([2, 2, 3], np.float32))),
  1753. 'skip': ['backward']}),
  1754. ('ScatterMinF16', {
  1755. 'block': ScatterMin(np.float16),
  1756. 'desc_inputs': (Tensor(np.array([[0, 0], [1, 1]], np.int32)),
  1757. Tensor(np.ones([2, 2, 3], np.float16))),
  1758. 'skip': ['backward']}),
  1759. ('ScatterMinI32', {
  1760. 'block': ScatterMin(np.int32),
  1761. 'desc_inputs': (Tensor(np.array([[0, 0], [1, 1]], np.int32)),
  1762. Tensor(np.ones([2, 2, 3], np.int32))),
  1763. 'skip': ['backward']}),
  1764. ('ScatterUpdate', {
  1765. 'block': ScatterUpdate((6,)),
  1766. 'desc_inputs': (Tensor(np.array([2, 0, 5], np.int32)),
  1767. Tensor(np.array([2.0, 3.0, 4.0], np.float32))),
  1768. 'skip': ['backward']}),
  1769. ('ScatterAddUseLocking', {
  1770. 'block': ScatterAdd((6,), use_locking=True),
  1771. 'desc_inputs': (Tensor(np.array([2, 0, 5], np.int32)),
  1772. Tensor(np.array([2.0, 3.0, 4.0], np.float32))),
  1773. 'skip': ['backward']}),
  1774. ('ScatterAdd', {
  1775. 'block': ScatterAdd((6,)),
  1776. 'desc_inputs': (Tensor(np.array([2, 0, 5], np.int32)),
  1777. Tensor(np.array([2.0, 3.0, 4.0], np.float32))),
  1778. 'skip': ['backward']}),
  1779. ('ScatterAddScalar', {
  1780. 'block': ScatterAdd((6,)),
  1781. 'desc_inputs': (Tensor(np.array([2], np.int32)),
  1782. Tensor(np.array([2.0], np.float32))),
  1783. 'skip': ['backward']}),
  1784. ('ScatterAdd2d', {
  1785. 'block': ScatterAdd((3, 4)),
  1786. 'desc_inputs': (Tensor(np.array([[0, 1], [1, 2]], np.int32)),
  1787. Tensor(np.array([[[1, 1, 1, 1], [2, 2, 2, 2]],
  1788. [[3, 3, 3, 3], [4, 4, 4, 4]]], np.float32))),
  1789. 'skip': ['backward']}),
  1790. ('ScatterAddF16', {
  1791. 'block': ScatterAdd((6,), np.float16),
  1792. 'desc_inputs': (Tensor(np.array([2, 0, 5], np.int32)),
  1793. Tensor(np.array([2.0, 3.0, 4.0], np.float16))),
  1794. 'skip': ['backward']}),
  1795. ('ScatterAddI8', {
  1796. 'block': ScatterAdd((6,), np.int8),
  1797. 'desc_inputs': (Tensor(np.array([2, 0, 5], np.int32)),
  1798. Tensor(np.array([2, 3, 4], np.int8))),
  1799. 'skip': ['backward']}),
  1800. ('ScatterAddI32', {
  1801. 'block': ScatterAdd((6,), np.int32),
  1802. 'desc_inputs': (Tensor(np.array([2, 0, 5], np.int32)),
  1803. Tensor(np.array([2, 3, 4], np.int32))),
  1804. 'skip': ['backward']}),
  1805. ('ScatterAddU8', {
  1806. 'block': ScatterAdd((6,), np.uint8),
  1807. 'desc_inputs': (Tensor(np.array([2, 0, 5], np.int32)),
  1808. Tensor(np.array([2, 3, 4], np.uint8))),
  1809. 'skip': ['backward']}),
  1810. ('ScatterMulUseLocking', {
  1811. 'block': ScatterMul((6,), use_locking=True),
  1812. 'desc_inputs': (Tensor(np.array([2], np.int32)),
  1813. Tensor(np.array([2.0], np.float32))),
  1814. 'skip': ['backward']}),
  1815. ('ScatterMulScalar', {
  1816. 'block': ScatterMul((6,)),
  1817. 'desc_inputs': (Tensor(np.array([2], np.int32)),
  1818. Tensor(np.array([2.0], np.float32))),
  1819. 'skip': ['backward']}),
  1820. ('ScatterMul2d', {
  1821. 'block': ScatterMul((3, 4)),
  1822. 'desc_inputs': (Tensor(np.array([[0, 1], [1, 2]], np.int32)),
  1823. Tensor(np.array([[[1, 1, 1, 1], [2, 2, 2, 2]],
  1824. [[3, 3, 3, 3], [4, 4, 4, 4]]], np.float32))),
  1825. 'skip': ['backward']}),
  1826. ('ScatterMulF16', {
  1827. 'block': ScatterMul((6,), np.float16),
  1828. 'desc_inputs': (Tensor(np.array([2, 0, 5], np.int32)),
  1829. Tensor(np.array([2.0, 3.0, 4.0], np.float16))),
  1830. 'skip': ['backward']}),
  1831. ('ScatterMulI8', {
  1832. 'block': ScatterMul((6,), np.int8),
  1833. 'desc_inputs': (Tensor(np.array([2, 0, 5], np.int32)),
  1834. Tensor(np.array([2, 3, 4], np.int8))),
  1835. 'skip': ['backward']}),
  1836. ('ScatterMulI32', {
  1837. 'block': ScatterMul((6,), np.int32),
  1838. 'desc_inputs': (Tensor(np.array([2, 0, 5], np.int32)),
  1839. Tensor(np.array([2, 3, 4], np.int32))),
  1840. 'skip': ['backward']}),
  1841. ('ScatterMulU8', {
  1842. 'block': ScatterMul((6,), np.uint8),
  1843. 'desc_inputs': (Tensor(np.array([2, 0, 5], np.int32)),
  1844. Tensor(np.array([2, 3, 4], np.uint8))),
  1845. 'skip': ['backward']}),
  1846. ('ScatterDivUseLocking', {
  1847. 'block': ScatterDiv((6,), use_locking=True),
  1848. 'desc_inputs': (Tensor(np.array([2], np.int32)),
  1849. Tensor(np.array([2.0], np.float32))),
  1850. 'skip': ['backward']}),
  1851. ('ScatterDivScalar', {
  1852. 'block': ScatterDiv((6,)),
  1853. 'desc_inputs': (Tensor(np.array([2], np.int32)),
  1854. Tensor(np.array([2.0], np.float32))),
  1855. 'skip': ['backward']}),
  1856. ('ScatterDiv2d', {
  1857. 'block': ScatterDiv((3, 4)),
  1858. 'desc_inputs': (Tensor(np.array([[0, 1], [1, 2]], np.int32)),
  1859. Tensor(np.array([[[1, 1, 1, 1], [2, 2, 2, 2]],
  1860. [[3, 3, 3, 3], [4, 4, 4, 4]]], np.float32))),
  1861. 'skip': ['backward']}),
  1862. ('ScatterDivF16', {
  1863. 'block': ScatterDiv((6,), np.float16),
  1864. 'desc_inputs': (Tensor(np.array([2, 0, 5], np.int32)),
  1865. Tensor(np.array([2.0, 3.0, 4.0], np.float16))),
  1866. 'skip': ['backward']}),
  1867. ('ScatterDivI8', {
  1868. 'block': ScatterDiv((6,), np.int8),
  1869. 'desc_inputs': (Tensor(np.array([2, 0, 5], np.int32)),
  1870. Tensor(np.array([2, 3, 4], np.int8))),
  1871. 'skip': ['backward']}),
  1872. ('ScatterDivU8', {
  1873. 'block': ScatterDiv((6,), np.uint8),
  1874. 'desc_inputs': (Tensor(np.array([2, 0, 5], np.int32)),
  1875. Tensor(np.array([2, 3, 4], np.uint8))),
  1876. 'skip': ['backward']}),
  1877. ('ScatterSubUseLocking', {
  1878. 'block': ScatterSub((6,), use_locking=True),
  1879. 'desc_inputs': (Tensor(np.array([2], np.int32)),
  1880. Tensor(np.array([2.0], np.float32))),
  1881. 'skip': ['backward']}),
  1882. ('ScatterSubScalar', {
  1883. 'block': ScatterSub((6,)),
  1884. 'desc_inputs': (Tensor(np.array([2], np.int32)),
  1885. Tensor(np.array([2.0], np.float32))),
  1886. 'skip': ['backward']}),
  1887. ('ScatterSub2d', {
  1888. 'block': ScatterSub((3, 4)),
  1889. 'desc_inputs': (Tensor(np.array([[0, 1], [1, 2]], np.int32)),
  1890. Tensor(np.array([[[1, 1, 1, 1], [2, 2, 2, 2]],
  1891. [[3, 3, 3, 3], [4, 4, 4, 4]]], np.float32))),
  1892. 'skip': ['backward']}),
  1893. ('ScatterSubF16', {
  1894. 'block': ScatterSub((6,), np.float16),
  1895. 'desc_inputs': (Tensor(np.array([2, 0, 5], np.int32)),
  1896. Tensor(np.array([2.0, 3.0, 4.0], np.float16))),
  1897. 'skip': ['backward']}),
  1898. ('ScatterSubI32', {
  1899. 'block': ScatterSub((6,), np.int32),
  1900. 'desc_inputs': (Tensor(np.array([2, 0, 5], np.int32)),
  1901. Tensor(np.array([2, 3, 4], np.int32))),
  1902. 'skip': ['backward']}),
  1903. ('ScatterSubI8', {
  1904. 'block': ScatterSub((6,), np.int8),
  1905. 'desc_inputs': (Tensor(np.array([2, 0, 5], np.int32)),
  1906. Tensor(np.array([2, 3, 4], np.int8))),
  1907. 'skip': ['backward']}),
  1908. ('ScatterSubU8', {
  1909. 'block': ScatterSub((6,), np.uint8),
  1910. 'desc_inputs': (Tensor(np.array([2, 0, 5], np.int32)),
  1911. Tensor(np.array([1, 1, 0], np.uint8))),
  1912. 'skip': ['backward']}),
  1913. ('SmoothL1Loss', {
  1914. 'block': P.SmoothL1Loss(),
  1915. 'desc_inputs': [[256, 4], [256, 4]],
  1916. 'desc_bprop': [[256, 4]]}),
  1917. ('IOU', {
  1918. 'block': P.IOU(),
  1919. 'desc_inputs': [Tensor(np.ones((256, 4), np.float16)), Tensor(np.ones((128, 4), np.float16))],
  1920. 'desc_bprop': [[128, 256]]}),
  1921. ('Summary', {
  1922. 'block': SummaryNet(),
  1923. 'desc_inputs': [Tensor(np.array([1.1]).astype(np.float32)),
  1924. Tensor(np.array([1.2]).astype(np.float32))],
  1925. 'skip': ['backward']}),
  1926. ('ConfusionMulGrad_1', {
  1927. 'block': P.ConfusionMulGrad(axis=[0], keep_dims=False),
  1928. 'desc_inputs': [[3, 2], [3, 2], [3, 2]],
  1929. 'desc_bprop': [[3, 2], [2]],
  1930. 'skip': ['backward']}),
  1931. ('ConfusionMulGrad_2', {
  1932. 'block': P.ConfusionMulGrad(axis=[0], keep_dims=True),
  1933. 'desc_inputs': [[3, 2], [3, 2], [3, 2]],
  1934. 'desc_bprop': [[3, 2], [1, 2]],
  1935. 'skip': ['backward']}),
  1936. ('ConfusionMulGrad_3', {
  1937. 'block': P.ConfusionMulGrad(axis=(), keep_dims=True),
  1938. 'desc_inputs': [[2, 3, 4], [2, 3, 4], [2, 3, 4]],
  1939. 'desc_bprop': [[2, 3, 4], [1, 1, 1]],
  1940. 'skip': ['backward']}),
  1941. ('HistogramSummary', {
  1942. 'block': HistogramSummaryNet(),
  1943. 'desc_inputs': [Tensor(np.array([1.1]).astype(np.float32)),
  1944. Tensor(np.array([1.2]).astype(np.float32))],
  1945. 'skip': ['backward']}),
  1946. ]
  1947. test_case_quant_ops = [
  1948. ('AscendQuant_1', {
  1949. 'block': inner.AscendQuant(0.5, 0.0, False, "Round"),
  1950. 'desc_inputs': [Tensor(np.random.rand(1, 2, 4, 4), mstype.float32)],
  1951. 'skip': ['backward']}),
  1952. ('AscendQuant_2', {
  1953. 'block': inner.AscendQuant(80.0, 10.0, True, "Round"),
  1954. 'desc_inputs': [Tensor([100.0, 200.0], mstype.float32)],
  1955. 'skip': ['backward']}),
  1956. ('AscendQuant_3', {
  1957. 'block': inner.AscendQuant(80.0, 0.0, False, "Floor"),
  1958. 'desc_inputs': [Tensor([100.0, 200.0], mstype.float32)],
  1959. 'skip': ['backward']}),
  1960. ('AscendQuant_4', {
  1961. 'block': inner.AscendQuant(80.0, 0.0, False, "Ceil"),
  1962. 'desc_inputs': [Tensor([100.0, 200.0], mstype.float32)],
  1963. 'skip': ['backward']}),
  1964. ('AscendQuant_5', {
  1965. 'block': inner.AscendQuant(80.0, 0.0, False, "Trunc"),
  1966. 'desc_inputs': [Tensor([100.0, 200.0], mstype.float32)],
  1967. 'skip': ['backward']}),
  1968. ('AscendQuant_6', {
  1969. 'block': inner.AscendQuant(-80.0, 10.0, False, "Round"),
  1970. 'desc_inputs': [Tensor([100.0, 200.0], mstype.float32)],
  1971. 'skip': ['backward']}),
  1972. ('AscendQuant_7', {
  1973. 'block': inner.AscendQuant(80.0, -10.0, False, "Round"),
  1974. 'desc_inputs': [Tensor([100.0, 200.0], mstype.float32)],
  1975. 'skip': ['backward']}),
  1976. ('AscendQuant_8', {
  1977. 'block': inner.AscendQuant(80.0, 10.0, False, "Round"),
  1978. 'desc_inputs': [Tensor([100.0, 200.0], mstype.float16)],
  1979. 'skip': ['backward']}),
  1980. ]
  1981. test_case_lists = [test_case_nn_ops, test_case_math_ops, test_case_array_ops, test_case_other_ops, test_case_quant_ops]
  1982. test_case = functools.reduce(lambda x, y: x + y, test_case_lists)
  1983. # use -k to select certain testcast
  1984. # pytest tests/python/ops/test_ops.py::test_backward -k LayerNorm
  1985. test_exec_case = test_case
  1986. test_backward_exec_case = filter(lambda x: 'skip' not in x[1] or 'backward' not in x[1]['skip'], test_case)
  1987. @non_graph_engine
  1988. @mindspore_test(pipeline_for_compile_forward_ge_graph_for_case_by_case_config)
  1989. def test_exec():
  1990. context.set_context(mode=context.GRAPH_MODE)
  1991. return test_exec_case
  1992. @mindspore_test(pipeline_for_compile_grad_ge_graph_for_case_by_case_config)
  1993. def test_backward_exec():
  1994. context.set_context(mode=context.GRAPH_MODE)
  1995. return test_backward_exec_case
  1996. raise_set = [
  1997. ('Cast_Error', {
  1998. 'block': (P.Cast(), {'exception': TypeError}),
  1999. 'desc_const': [mstype.int32],
  2000. 'desc_inputs': ['wrong input'],
  2001. 'desc_bprop': [Tensor(np.ones((2, 3, 3, 5)).astype(np.int32))]}),
  2002. ('Maximum_Error', {
  2003. 'block': (P.Maximum(), {'exception': TypeError}),
  2004. 'desc_const': [(1, 2, 3)],
  2005. 'desc_inputs': [[2, 3, 3, 5]],
  2006. 'desc_bprop': [[2, 3, 3, 5]]}),
  2007. ('Shape_error', {
  2008. 'block': (P.Shape(), {'exception': TypeError}),
  2009. 'desc_inputs': [(64, 1)],
  2010. 'desc_bprop': [[64]]}),
  2011. ('Flatten_Error', {
  2012. 'block': (NetForFlatten0D(), {'exception': ValueError}),
  2013. 'desc_inputs': [Tensor(np.array(0).astype(np.int32))],
  2014. 'desc_bprop': [Tensor(np.array(0).astype(np.int32))]}),
  2015. ('ScatterNdUpdate', {
  2016. 'block': (P.ScatterNdUpdate(), {'exception': TypeError}),
  2017. 'desc_inputs': (Tensor(np.ones((2, 3), np.float32)),
  2018. Tensor(np.ones((2, 2), np.float32)),
  2019. Tensor(np.ones((2,), np.float32))),
  2020. 'desc_bprop': [[2, 3]]}),
  2021. ('Pack', {
  2022. 'block': (NetForPackInput(P.Pack()), {'exception': ValueError}),
  2023. 'desc_inputs': [[2, 2]],
  2024. 'desc_bprop': [[1, 2, 2]]}),
  2025. ('PReLU', {
  2026. 'block': (P.PReLU(), {'exception': ValueError}),
  2027. 'desc_inputs': [[2], [1]],
  2028. 'desc_bprop': [[1]]}),
  2029. ('SSIM', {
  2030. 'block': (nn.SSIM(), {'exception': ValueError}),
  2031. 'desc_inputs': [Tensor(np.ones((1, 3, 8, 8)), mstype.float32),
  2032. Tensor(np.ones((1, 3, 8, 8)), mstype.float32)]}),
  2033. ('StridedSlice_0', {
  2034. 'block': (P.StridedSlice(), {'exception': ValueError}),
  2035. 'desc_const': [(1, 2.2, 3), (3, 4, 5), (1, 1, 1)],
  2036. 'desc_inputs': [[4, 5, 6, 7]]}),
  2037. ('StridedSlice_1', {
  2038. 'block': (P.StridedSlice(), {'exception': ValueError}),
  2039. 'desc_const': [(1, 2, 3), (3, 4, 5), (1, 1)],
  2040. 'desc_inputs': [[4, 5, 6, 7]]}),
  2041. ('StridedSlice_2', {
  2042. 'block': (P.StridedSlice(), {'exception': ValueError}),
  2043. 'desc_const': [(1, 2, 3), (3, 4, 5), (1, 1, 0)],
  2044. 'desc_inputs': [[4, 5, 6, 7]]}),
  2045. ]
  2046. @mindspore_test(pipeline_for_compile_forward_ge_graph_for_case_by_case_config_exception)
  2047. def test_check_exception():
  2048. return raise_set