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