decompositions.py 175 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301
  1. # mypy: allow-untyped-decorators
  2. # mypy: allow-untyped-defs
  3. import functools
  4. import itertools
  5. import numbers
  6. import operator
  7. import sys
  8. from collections.abc import Iterable
  9. from enum import Enum
  10. from functools import partial, reduce
  11. from itertools import chain, product
  12. from typing import Any, Callable, cast, Optional, Union
  13. import torch
  14. import torch._meta_registrations
  15. import torch._prims as prims
  16. import torch._prims_common as utils
  17. import torch.nn.functional as F
  18. from torch import sym_float, sym_int, Tensor
  19. from torch._decomp import register_decomposition
  20. from torch._higher_order_ops.out_dtype import out_dtype
  21. from torch._prims_common import (
  22. IntLike,
  23. NumberType,
  24. suggest_memory_format,
  25. TensorLike,
  26. TensorSequenceType,
  27. )
  28. from torch._prims_common.wrappers import (
  29. _maybe_convert_to_dtype,
  30. _maybe_resize_out,
  31. _safe_copy_out,
  32. out_wrapper,
  33. )
  34. from torch.utils import _pytree as pytree
  35. from torch.utils._pytree import tree_map
  36. DispatchKey = torch._C.DispatchKey # type: ignore[attr-defined]
  37. # None of these functions are publicly accessible; get at them
  38. # from torch._decomps
  39. __all__: list[str] = []
  40. aten = torch._ops.ops.aten
  41. class Reduction(Enum):
  42. NONE = 0
  43. MEAN = 1
  44. SUM = 2
  45. # This wraps a decomposition and performs various type promotion logic within it, depending on the strategy provided
  46. # We're currently reusing ELEMENTWISE_TYPE_PROMOTION_KIND, although some of the usages are on non-elementwise ops
  47. # Will need to validate the non-elementwise uses
  48. def type_casts(
  49. f: Callable,
  50. type_promotion: utils.ELEMENTWISE_TYPE_PROMOTION_KIND,
  51. compute_dtype_only: bool = False,
  52. include_non_tensor_args: bool = False,
  53. ):
  54. @functools.wraps(f)
  55. def inner(*args, **kwargs):
  56. allowed_types = (
  57. (Tensor, torch.types._Number) if include_non_tensor_args else (Tensor,)
  58. ) # type: ignore[arg-type]
  59. flat_args = [
  60. x
  61. for x in pytree.arg_tree_leaves(*args, **kwargs)
  62. if isinstance(x, allowed_types)
  63. ]
  64. computation_dtype, result_dtype = utils.elementwise_dtypes(
  65. *flat_args, type_promotion_kind=type_promotion
  66. )
  67. # TODO: pretty sure this is not quite right
  68. def increase_prec(x):
  69. if isinstance(x, Tensor):
  70. return x.to(computation_dtype)
  71. else:
  72. return x
  73. def decrease_prec(x):
  74. if isinstance(x, Tensor):
  75. return x.to(result_dtype)
  76. else:
  77. return x
  78. r = f(*tree_map(increase_prec, args), **tree_map(increase_prec, kwargs))
  79. if compute_dtype_only:
  80. return r
  81. else:
  82. return tree_map(decrease_prec, r)
  83. return inner
  84. compute_only_pw_cast_for_opmath = partial(
  85. type_casts,
  86. type_promotion=utils.ELEMENTWISE_TYPE_PROMOTION_KIND.DEFAULT,
  87. compute_dtype_only=True,
  88. )
  89. pw_cast_for_opmath = partial(
  90. type_casts, type_promotion=utils.ELEMENTWISE_TYPE_PROMOTION_KIND.DEFAULT
  91. )
  92. pw_cast_for_opmath_non_tensor_args = partial(
  93. type_casts,
  94. type_promotion=utils.ELEMENTWISE_TYPE_PROMOTION_KIND.DEFAULT,
  95. include_non_tensor_args=True,
  96. )
  97. pw_cast_for_int_to_real = partial(
  98. type_casts, type_promotion=utils.ELEMENTWISE_TYPE_PROMOTION_KIND.INT_TO_FLOAT
  99. )
  100. # This expands x until x.dim() == dim. Might be useful as an operator
  101. def _unsqueeze_to_dim(x: Tensor, dim: int) -> Tensor:
  102. for _ in range(dim - x.dim()):
  103. x = x.unsqueeze(-1)
  104. return x
  105. @register_decomposition(aten.tanh_backward)
  106. @out_wrapper("grad_input")
  107. @pw_cast_for_opmath
  108. def tanh_backward(out_grad: Tensor, y: Tensor):
  109. return out_grad * (1 - y * y).conj_physical()
  110. @register_decomposition(aten.sigmoid_backward)
  111. @out_wrapper("grad_input")
  112. @pw_cast_for_opmath
  113. def sigmoid_backward(out_grad: Tensor, y: Tensor):
  114. return out_grad * (y * (1 - y)).conj_physical()
  115. @register_decomposition(aten.softplus_backward)
  116. @out_wrapper("grad_input")
  117. @pw_cast_for_opmath
  118. def softplus_backward(out_grad: Tensor, x: Tensor, beta: float, threshold: float):
  119. z = (x * beta).exp()
  120. return torch.where((x * beta) > threshold, out_grad, out_grad * z / (z + 1.0))
  121. @register_decomposition(aten.elu_backward)
  122. @out_wrapper("grad_input")
  123. @pw_cast_for_opmath
  124. def elu_backward(
  125. grad_output: Tensor,
  126. alpha: float,
  127. scale: float,
  128. input_scale: float,
  129. is_result: bool,
  130. self_or_result: Tensor,
  131. ):
  132. negcoef = alpha * scale
  133. poscoef = scale
  134. negiptcoef = input_scale
  135. if is_result:
  136. return torch.where(
  137. self_or_result <= 0,
  138. grad_output * negiptcoef * (self_or_result + negcoef),
  139. grad_output * poscoef,
  140. )
  141. else:
  142. return torch.where(
  143. self_or_result <= 0,
  144. grad_output * negiptcoef * negcoef * torch.exp(self_or_result * negiptcoef),
  145. grad_output * poscoef,
  146. )
  147. @register_decomposition([aten.fill.Scalar])
  148. def fill_scalar(self, value):
  149. return torch.full_like(self, value)
  150. @register_decomposition([aten.fill.Tensor])
  151. def fill_tensor(self, value: Tensor):
  152. torch._check(
  153. value.dim() == 0,
  154. lambda: f"fill only supports 0-dimension value tensor but got tensor with {value.dim()} dimensions",
  155. )
  156. return aten.copy(self, value)
  157. @register_decomposition(aten.hardsigmoid)
  158. @out_wrapper()
  159. @pw_cast_for_opmath
  160. def hardsigmoid(self: Tensor) -> Tensor:
  161. return torch.clamp(torch.clamp(self + 3, min=0), max=6) / 6
  162. @register_decomposition(aten.hardsigmoid_backward)
  163. @out_wrapper("grad_input")
  164. @pw_cast_for_opmath
  165. def hardsigmoid_backward(grad_output: Tensor, self: Tensor):
  166. return torch.where(
  167. (self > -3.0) & (self < 3.0),
  168. grad_output * (1.0 / 6.0),
  169. 0.0,
  170. )
  171. @register_decomposition(aten.hardtanh_backward)
  172. @out_wrapper("grad_input")
  173. def hardtanh_backward(
  174. grad_output: Tensor, self: Tensor, min_val: float, max_val: float
  175. ):
  176. return torch.where((self <= min_val) | (self >= max_val), 0.0, grad_output)
  177. @register_decomposition(aten.hardswish)
  178. @out_wrapper()
  179. @pw_cast_for_opmath
  180. def hardswish(self: Tensor) -> Tensor:
  181. return self * torch.clamp(torch.clamp(self + 3, min=0), max=6) / 6
  182. @register_decomposition(aten.hardswish_backward)
  183. @out_wrapper()
  184. @pw_cast_for_opmath
  185. def hardswish_backward(grad_output: Tensor, self: Tensor) -> Tensor:
  186. return torch.where(
  187. self <= -3,
  188. 0.0,
  189. torch.where(self < 3, grad_output * ((self / 3) + 0.5), grad_output),
  190. )
  191. @register_decomposition(aten.threshold_backward)
  192. @out_wrapper("grad_input")
  193. def threshold_backward(grad_output: Tensor, self: Tensor, threshold: float):
  194. return torch.where(self <= threshold, 0, grad_output)
  195. @register_decomposition(aten.leaky_relu_backward)
  196. @out_wrapper("grad_input")
  197. @pw_cast_for_opmath
  198. def leaky_relu_backward(
  199. grad_output: Tensor, self: Tensor, negative_slope: float, self_is_result: bool
  200. ):
  201. return torch.where(self > 0, grad_output, grad_output * negative_slope)
  202. @register_decomposition(aten.gelu_backward)
  203. @out_wrapper("grad_input")
  204. @pw_cast_for_opmath
  205. def gelu_backward(grad: Tensor, self: Tensor, approximate: str = "none"):
  206. M_SQRT2 = 1.41421356237309504880
  207. M_SQRT1_2 = 0.70710678118654752440
  208. M_2_SQRTPI = 1.12837916709551257390
  209. if approximate == "tanh":
  210. kBeta = M_SQRT2 * M_2_SQRTPI * 0.5
  211. kKappa = 0.044715
  212. x_sq = self * self
  213. x_cube = x_sq * self
  214. inner = kBeta * (self + kKappa * x_cube)
  215. tanh_inner = torch.tanh(inner)
  216. left = 0.5 * self
  217. right = 1 + tanh_inner
  218. left_derivative = 0.5 * right
  219. tanh_derivative = 1 - tanh_inner * tanh_inner
  220. inner_derivative = kBeta * (1 + 3 * kKappa * x_sq)
  221. right_derivative = left * tanh_derivative * inner_derivative
  222. return grad * (left_derivative + right_derivative)
  223. else:
  224. kAlpha = M_SQRT1_2
  225. kBeta = M_2_SQRTPI * M_SQRT1_2 * 0.5
  226. cdf = 0.5 * (1 + torch.erf(self * kAlpha))
  227. pdf = kBeta * torch.exp(self * self * -0.5)
  228. return grad * (cdf + self * pdf)
  229. @register_decomposition(aten.mish_backward)
  230. @pw_cast_for_opmath
  231. def mish_backward(grad_output: Tensor, input: Tensor):
  232. input_tanh_softplus = torch.tanh(F.softplus(input))
  233. input_sigmoid = torch.sigmoid(input)
  234. out = input * input_sigmoid * (1 - input_tanh_softplus * input_tanh_softplus)
  235. return grad_output * (input_tanh_softplus + out)
  236. @register_decomposition(aten.silu)
  237. @out_wrapper()
  238. @pw_cast_for_opmath
  239. def silu(self: Tensor) -> Tensor:
  240. return self * torch.sigmoid(self)
  241. @register_decomposition(aten.silu_backward)
  242. @out_wrapper("grad_input")
  243. @pw_cast_for_opmath
  244. def silu_backward(grad_output: Tensor, self: Tensor) -> Tensor:
  245. sigmoid = 1 / (1 + torch.exp(-self))
  246. return grad_output * sigmoid * (1 + self * (1 - sigmoid))
  247. @register_decomposition(aten._prelu_kernel)
  248. def _prelu_kernel(self: Tensor, weight: Tensor) -> Tensor:
  249. return torch.where(self > 0, self, weight * self)
  250. @register_decomposition(aten._prelu_kernel_backward)
  251. def _prelu_kernel_backward(
  252. grad_output: Tensor,
  253. self: Tensor,
  254. weight: Tensor,
  255. ) -> tuple[Tensor, Tensor]:
  256. input_grad = torch.where(self > 0, grad_output, weight * grad_output)
  257. weight_grad = torch.where(self > 0, 0.0, self * grad_output)
  258. return (input_grad, weight_grad)
  259. @register_decomposition(aten.rrelu_with_noise_backward)
  260. @out_wrapper()
  261. @pw_cast_for_opmath
  262. def rrelu_with_noise_backward(
  263. grad_output: Tensor,
  264. self: Tensor,
  265. noise: Tensor,
  266. lower: float,
  267. upper: float,
  268. training: bool,
  269. self_is_result: bool,
  270. ) -> Tensor:
  271. if training and upper - lower > 1e-6:
  272. return grad_output.mul(noise)
  273. else:
  274. negative_slope = (lower + upper) / 2
  275. return aten.leaky_relu_backward(
  276. grad_output, self, negative_slope, self_is_result
  277. )
  278. @register_decomposition(aten.log_sigmoid_backward)
  279. @out_wrapper("grad_input")
  280. @pw_cast_for_opmath
  281. def log_sigmoid_backward(grad_output: Tensor, self: Tensor, buffer: Tensor) -> Tensor:
  282. in_negative = self < 0
  283. max_deriv = torch.where(in_negative, 1, 0)
  284. sign = torch.where(in_negative, 1, -1)
  285. z = torch.exp(-torch.abs(self))
  286. return grad_output * (max_deriv - sign * (z / (1 + z)))
  287. # CPU has a special formula that uses buffer, but disabled for convenience sake
  288. # return (max_deriv - sign * (buffer / (1 + buffer))) * grad_output
  289. def apply_loss_reduction(loss: Tensor, reduction: int):
  290. if reduction == Reduction.MEAN.value:
  291. return torch.mean(loss)
  292. elif reduction == Reduction.SUM.value:
  293. return torch.sum(loss)
  294. else:
  295. return loss
  296. def to_real_dtype(dtype: torch.dtype):
  297. if dtype == torch.complex32:
  298. return torch.float16
  299. elif dtype == torch.complex64:
  300. return torch.float32
  301. elif dtype == torch.complex128:
  302. return torch.float64
  303. # TODO: None of these loss castings are quite correct, see
  304. # https://github.com/pytorch/pytorch/issues/76870. Also, the ATen kernels
  305. # perform the pointwise portion in opmath, but don't maintain it between the
  306. # pointwise portion and the reduction
  307. @register_decomposition(aten.mse_loss)
  308. @out_wrapper()
  309. @pw_cast_for_opmath
  310. def mse_loss(
  311. self: Tensor, target: Tensor, reduction: int = Reduction.MEAN.value
  312. ) -> Tensor:
  313. loss = (self - target) ** 2
  314. return apply_loss_reduction(loss, reduction)
  315. @register_decomposition(aten.mse_loss_backward)
  316. @out_wrapper("grad_input")
  317. @pw_cast_for_opmath
  318. def mse_loss_backward(
  319. grad_output: Tensor, input: Tensor, target: Tensor, reduction: int
  320. ):
  321. norm = 2.0 / input.numel() if reduction == Reduction.MEAN.value else 2.0
  322. return norm * (input - target) * grad_output
  323. @register_decomposition(aten._safe_softmax)
  324. def safe_softmax(self, dim, dtype=None):
  325. out = torch.softmax(self, dim=dim, dtype=dtype)
  326. masked = self.eq(float("-inf"))
  327. masked_rows = torch.all(masked, dim=dim, keepdim=True)
  328. zeros = torch.zeros_like(out)
  329. return torch.where(masked_rows, zeros, out)
  330. @register_decomposition(aten.smooth_l1_loss)
  331. @out_wrapper()
  332. @pw_cast_for_opmath
  333. def smooth_l1_loss(
  334. self: Tensor,
  335. target: Tensor,
  336. reduction: int = Reduction.MEAN.value,
  337. beta: float = 1.0,
  338. ):
  339. loss = (self - target).abs()
  340. loss = torch.where(loss < beta, 0.5 * loss**2 / beta, loss - 0.5 * beta)
  341. return apply_loss_reduction(loss, reduction)
  342. @register_decomposition(aten.smooth_l1_loss_backward.default)
  343. @pw_cast_for_opmath
  344. def smooth_l1_loss_backward(
  345. grad_output: Tensor, self: Tensor, target: Tensor, reduction: int, beta: float
  346. ):
  347. norm = 1.0 / self.numel() if reduction == Reduction.MEAN.value else 1.0
  348. x = self - target
  349. abs_x = torch.abs(x)
  350. norm_grad = norm * grad_output
  351. return torch.where(
  352. abs_x < beta,
  353. norm_grad * x / beta,
  354. norm_grad * torch.sign(x),
  355. )
  356. @register_decomposition(aten.smooth_l1_loss_backward.grad_input)
  357. @pw_cast_for_opmath
  358. def smooth_l1_loss_backward_out(
  359. grad_output: Tensor,
  360. self: Tensor,
  361. target: Tensor,
  362. reduction: int,
  363. beta: float,
  364. grad_input: Tensor,
  365. ):
  366. result = smooth_l1_loss_backward(grad_output, self, target, reduction, beta)
  367. _maybe_resize_out(grad_input, result.shape)
  368. return _safe_copy_out(copy_from=result, copy_to=grad_input, exact_dtype=True)
  369. @register_decomposition(aten.huber_loss_backward.default)
  370. @pw_cast_for_opmath
  371. def huber_loss_backward(
  372. grad_output: Tensor, self: Tensor, target: Tensor, reduction: int, delta: float
  373. ):
  374. norm = 1.0 / self.numel() if reduction == Reduction.MEAN.value else 1.0
  375. x = self - target
  376. return torch.where(
  377. x < -delta,
  378. -norm * grad_output * delta,
  379. torch.where(x > delta, norm * grad_output * delta, norm * x * grad_output),
  380. )
  381. # We cannot use @out_wrapper() here, because the output tensor is not named 'out', it's 'grad_input'
  382. @register_decomposition(aten.huber_loss_backward.out)
  383. @pw_cast_for_opmath
  384. def huber_loss_backward_out(
  385. grad_output: Tensor,
  386. self: Tensor,
  387. target: Tensor,
  388. reduction: int,
  389. delta: float,
  390. grad_input: Tensor,
  391. ):
  392. result = huber_loss_backward(grad_output, self, target, reduction, delta)
  393. _maybe_resize_out(grad_input, result.shape)
  394. return _safe_copy_out(copy_from=result, copy_to=grad_input, exact_dtype=True)
  395. def _nll_loss_backward(
  396. grad_output: Tensor,
  397. self: Tensor,
  398. target: Tensor,
  399. weight: Optional[Tensor],
  400. reduction: int,
  401. ignore_index: int,
  402. total_weight: Tensor,
  403. ) -> Tensor:
  404. channel_dim = 0 if self.dim() < 2 else 1
  405. if reduction == Reduction.MEAN.value:
  406. grad_output = grad_output / total_weight
  407. target = target.unsqueeze(channel_dim)
  408. safe_target = torch.where(target != ignore_index, target, 0)
  409. grad_input = torch.zeros_like(self)
  410. grad_input = torch.scatter(grad_input, channel_dim, safe_target, -1.0)
  411. if grad_input.dim() > grad_output.dim() > 0:
  412. grad_output = grad_output.unsqueeze(channel_dim)
  413. if weight is not None:
  414. new_shape = [1 for _ in range(self.dim())]
  415. new_shape[channel_dim] = weight.shape[0]
  416. weight = weight.reshape(new_shape)
  417. grad_output = grad_output * weight
  418. grad_output = torch.where(target != ignore_index, grad_output, 0)
  419. return grad_input * grad_output
  420. @register_decomposition(aten.glu_backward)
  421. @out_wrapper("grad_input")
  422. @pw_cast_for_opmath
  423. def glu_backward(grad_output: Tensor, self: Tensor, dim: int) -> Tensor:
  424. assert self.dim() > 0, "glu does not support 0-dimensional tensors"
  425. wrap_dim = utils.canonicalize_dim(self.dim(), dim)
  426. nIn = self.size(wrap_dim)
  427. assert nIn % 2 == 0, (
  428. f"Halving dimension must be even, but dimension {wrap_dim} is size {nIn}"
  429. )
  430. inputSize = nIn // 2
  431. firstHalf = self.narrow(wrap_dim, 0, inputSize)
  432. secondHalf = self.narrow(wrap_dim, inputSize, inputSize)
  433. gradInputFirstHalf = torch.sigmoid(secondHalf)
  434. gradInputSecondHalf = (
  435. (1.0 - gradInputFirstHalf) * gradInputFirstHalf * firstHalf * grad_output
  436. )
  437. gradInputFirstHalf = gradInputFirstHalf * grad_output
  438. return torch.cat([gradInputFirstHalf, gradInputSecondHalf], dim=wrap_dim)
  439. @register_decomposition(aten.nll_loss_backward)
  440. @out_wrapper("grad_input")
  441. def nll_loss_backward(
  442. grad_output: Tensor,
  443. self: Tensor,
  444. target: Tensor,
  445. weight: Optional[Tensor],
  446. reduction: int,
  447. ignore_index: int,
  448. total_weight: Tensor,
  449. ) -> Tensor:
  450. assert 0 <= self.dim() <= 2, "input tensor should be 1D or 2D"
  451. assert target.dim() <= 1, (
  452. "0D or 1D target tensor expected, multi-target not supported"
  453. )
  454. no_batch_dim = self.dim() == 1 and target.dim() == 0
  455. assert no_batch_dim or (self.shape[0] == target.shape[0]), (
  456. f"size mismatch (got input: {self.shape}, target: {target.shape})"
  457. )
  458. assert total_weight.numel() == 1, (
  459. "expected total_weight to be a single element tensor, got: ",
  460. f"{total_weight.shape} ({total_weight.numel()} elements)",
  461. )
  462. assert weight is None or weight.numel() == self.shape[-1], (
  463. "weight tensor should be defined either for all or no classes"
  464. )
  465. if reduction == Reduction.NONE.value and self.dim() == 2:
  466. assert grad_output.dim() == 1 and grad_output.shape[0] == self.shape[0], (
  467. f"Expected a tensor of dimension 1 and tensor.size[0] == {self.shape[0]} but "
  468. f"got: dimension {grad_output.dim()} and tensor.size[0] == {grad_output.shape[0]}"
  469. )
  470. else:
  471. assert grad_output.dim() <= 1 and grad_output.numel() == 1, (
  472. f"Expected a single element grad_output tensor, but got: {grad_output.shape}"
  473. )
  474. return _nll_loss_backward(
  475. grad_output, self, target, weight, reduction, ignore_index, total_weight
  476. )
  477. @register_decomposition(aten.nll_loss2d_backward)
  478. @out_wrapper("grad_input")
  479. def nll_loss2d_backward(
  480. grad_output: Tensor,
  481. self: Tensor,
  482. target: Tensor,
  483. weight: Optional[Tensor],
  484. reduction: int,
  485. ignore_index: int,
  486. total_weight: Tensor,
  487. ) -> Tensor:
  488. assert self.dim() == 4, (
  489. f"only batches of spatial inputs supported (4D tensors), but got input of dimension: {self.dim()}"
  490. )
  491. assert target.dim() == 3, (
  492. f"only batches of spatial targets supported (3D tensors) but got targets of dimension: {target.dim()}"
  493. )
  494. assert (
  495. self.shape[0] == target.shape[0]
  496. and self.shape[2] == target.shape[1]
  497. and self.shape[3] == target.shape[2]
  498. ), f"size mismatch (got input: {self.shape}, target: {target.shape}"
  499. assert total_weight.numel() == 1, (
  500. "expected total_weight to be a single element tensor, "
  501. f"got: {total_weight.shape} ( {total_weight.numel()}, elements)"
  502. )
  503. return _nll_loss_backward(
  504. grad_output, self, target, weight, reduction, ignore_index, total_weight
  505. )
  506. @register_decomposition(aten.binary_cross_entropy)
  507. @out_wrapper()
  508. @pw_cast_for_opmath
  509. def binary_cross_entropy(
  510. self: Tensor,
  511. target: Tensor,
  512. weight: Optional[Tensor] = None,
  513. reduction: int = Reduction.MEAN.value,
  514. ) -> Tensor:
  515. # We cannot currently model this without introducing data-dependent control flow
  516. # TORCH_CHECK(
  517. # (input_val >= 0) && (input_val <= 1),
  518. # "all elements of input should be between 0 and 1"
  519. # )
  520. loss = (target - 1) * torch.maximum(
  521. torch.log1p(-self), self.new_full((), -100)
  522. ) - target * torch.maximum(torch.log(self), self.new_full((), -100))
  523. if weight is not None:
  524. loss = loss * weight
  525. return apply_loss_reduction(loss, reduction)
  526. @register_decomposition(aten.binary_cross_entropy_backward)
  527. @out_wrapper("grad_input")
  528. @pw_cast_for_opmath
  529. def binary_cross_entropy_backward(
  530. grad_output: Tensor,
  531. self: Tensor,
  532. target: Tensor,
  533. weight: Optional[Tensor] = None,
  534. reduction: int = Reduction.MEAN.value,
  535. ) -> Tensor:
  536. EPSILON = 1e-12
  537. result = grad_output * (self - target) / torch.clamp(self * (1 - self), min=EPSILON)
  538. if weight is not None:
  539. result = result * weight
  540. if reduction == Reduction.MEAN.value:
  541. result = result / self.numel()
  542. return result
  543. @register_decomposition(aten.soft_margin_loss)
  544. @out_wrapper()
  545. @pw_cast_for_opmath
  546. def soft_margin_loss(
  547. input: Tensor,
  548. target: Tensor,
  549. reduction: int = Reduction.MEAN.value,
  550. ) -> Tensor:
  551. loss = torch.log1p(torch.exp(-input * target))
  552. return apply_loss_reduction(loss, reduction)
  553. @register_decomposition(aten.soft_margin_loss_backward)
  554. @out_wrapper("grad_input")
  555. @pw_cast_for_opmath
  556. def soft_margin_loss_backward(
  557. grad_output: Tensor,
  558. self: Tensor,
  559. target: Tensor,
  560. reduction: int = Reduction.MEAN.value,
  561. ) -> Tensor:
  562. grad_input = target * grad_output * (torch.sigmoid(target * self) - 1)
  563. if reduction == Reduction.MEAN.value:
  564. grad_input = grad_input / self.numel()
  565. return grad_input
  566. @register_decomposition(aten.dist)
  567. @out_wrapper()
  568. def dist(input: Tensor, other: Tensor, p: float = 2):
  569. return aten.norm(input - other, p=p)
  570. @register_decomposition(aten._euclidean_dist)
  571. @out_wrapper()
  572. def _euclidean_dist(x1: Tensor, x2: Tensor) -> Tensor:
  573. x1_norm = x1.pow(2).sum(-1, True)
  574. x1_pad = torch.ones_like(x1_norm, memory_format=torch.contiguous_format)
  575. x2_norm = x2.pow(2).sum(-1, True)
  576. x2_pad = torch.ones_like(x2_norm, memory_format=torch.contiguous_format)
  577. x1_ = torch.cat([x1.mul(-2), x1_norm, x1_pad], -1)
  578. x2_ = torch.cat([x2, x2_pad, x2_norm], -1)
  579. result = x1_.matmul(x2_.mT)
  580. return result.clamp_min(0).sqrt()
  581. @register_decomposition(aten.slice_backward)
  582. @out_wrapper()
  583. def slice_backward(
  584. grad_output: Tensor,
  585. input_sizes: list[int],
  586. dim: int,
  587. start: int,
  588. end: int,
  589. step: int,
  590. ):
  591. grad_input = grad_output.new_zeros(input_sizes)
  592. return torch.slice_scatter(grad_input, grad_output, dim, start, end, step)
  593. @register_decomposition(aten.slice.Tensor)
  594. def slice_forward(
  595. # Tensor(a) self, int dim=0, SymInt? start=None, SymInt? end=None, SymInt step=1
  596. self: Tensor,
  597. dim: int = 0,
  598. start: Optional[int] = None,
  599. end: Optional[int] = None,
  600. step: int = 1,
  601. ):
  602. from torch.fx.experimental.symbolic_shapes import (
  603. guard_size_oblivious,
  604. statically_known_true,
  605. )
  606. ndim = self.dim()
  607. if ndim == 0:
  608. raise RuntimeError("slice() cannot be applied to a 0-dim tensor.")
  609. dim = utils.canonicalize_dim(self.dim(), dim)
  610. sizes = list(self.size())
  611. strides = list(self.stride())
  612. if step <= 0:
  613. raise RuntimeError("slice step must be positive")
  614. start_val = start if start is not None else 0
  615. end_val = end if end is not None else sys.maxsize # 2^63 - 1
  616. if guard_size_oblivious(start_val < 0):
  617. start_val += sizes[dim]
  618. if guard_size_oblivious(end_val < 0):
  619. end_val += sizes[dim]
  620. if guard_size_oblivious(start_val < 0):
  621. start_val = 0
  622. elif guard_size_oblivious(start_val > sizes[dim]):
  623. start_val = sizes[dim]
  624. if statically_known_true(end_val == sys.maxsize):
  625. end_val = sizes[dim]
  626. elif guard_size_oblivious(end_val < start_val):
  627. end_val = start_val
  628. elif guard_size_oblivious(end_val > sizes[dim]):
  629. end_val = sizes[dim]
  630. storage_offset = self.storage_offset() + start_val * strides[dim]
  631. len = end_val - start_val
  632. sizes[dim] = (len + step - 1) // step
  633. strides[dim] *= step
  634. if self.is_quantized:
  635. raise NotImplementedError(
  636. "Slice decomposition for quantized tensors aren't implemented"
  637. )
  638. else:
  639. return self.as_strided(sizes, strides, storage_offset)
  640. def _normalize_start_end(
  641. x: Tensor, dim: int, start: Optional[int], end: Optional[int]
  642. ) -> tuple[int, int]:
  643. """
  644. Normalize start and end such that both are in the range
  645. [0, x.get_size()[dim]] and start <= end.
  646. """
  647. dim_size = x.shape[dim]
  648. def clamp_wrap(val, lower, upper, default) -> int:
  649. if val is None:
  650. return default
  651. if val < 0:
  652. val = val + dim_size
  653. return min(max(val, lower), upper)
  654. start = clamp_wrap(start, 0, dim_size, 0)
  655. end = clamp_wrap(end, start, dim_size, dim_size)
  656. return start, end
  657. # This is not in torch._refs because aten.index used by
  658. # aten._unsafe_masked_index does not have a decomposition.
  659. @register_decomposition(aten.slice_scatter)
  660. @out_wrapper()
  661. def slice_scatter(
  662. input: Tensor,
  663. src: Tensor,
  664. dim: int = 0,
  665. start: Optional[int] = None,
  666. end: Optional[int] = None,
  667. step: int = 1,
  668. ):
  669. dim = utils.canonicalize_dim(input.ndim, dim)
  670. dim_size = input.shape[dim]
  671. start, end = _normalize_start_end(input, dim, start, end)
  672. src_size = list(input.shape)
  673. src_size[dim] = (end - start + (step - 1)) // step
  674. src = src.expand(src_size)
  675. if start == 0 and end == dim_size and step == 1:
  676. return src.clone()
  677. indices: list[Optional[Tensor]] = [None] * input.dim()
  678. idx = torch.arange(dim_size, device=input.device)
  679. indices[dim] = (idx - start) // step
  680. mask = torch.ones(dim_size, device=input.device, dtype=torch.bool)
  681. if start != 0:
  682. mask = torch.logical_and(mask, idx >= start)
  683. if end != dim_size:
  684. mask = torch.logical_and(mask, idx < end)
  685. if step != 1:
  686. mask = torch.logical_and(mask, (idx - start) % step == 0)
  687. mask_shape = [1] * input.dim()
  688. mask_shape[dim] = -1
  689. mask = mask.view(mask_shape)
  690. return aten.where(mask, aten._unsafe_masked_index(src, mask, indices, 0), input)
  691. @register_decomposition(aten.select_backward)
  692. @out_wrapper()
  693. def select_backward(grad_output: Tensor, input_sizes: list[int], dim: int, index: int):
  694. grad_input = grad_output.new_zeros(input_sizes)
  695. return torch.select_scatter(grad_input, grad_output, dim, index)
  696. @register_decomposition(aten.diagonal_backward)
  697. @out_wrapper()
  698. def diagonal_backward(
  699. grad_output: Tensor, input_sizes: list[int], offset: int, dim1: int, dim2: int
  700. ):
  701. grad_input = grad_output.new_zeros(input_sizes)
  702. return torch.diagonal_scatter(grad_input, grad_output, offset, dim1, dim2)
  703. def _cast_grad_to_input_dtype(
  704. grad_output: Tensor, grad_input: Tensor, input_dtype: torch.dtype
  705. ):
  706. if grad_output.dtype != input_dtype:
  707. grad_input = grad_input.to(input_dtype)
  708. return grad_input
  709. @register_decomposition(aten._softmax_backward_data)
  710. @out_wrapper("grad_input")
  711. @compute_only_pw_cast_for_opmath
  712. def _softmax_backward_data(
  713. grad_output: Tensor, output: Tensor, dim: int, input_dtype: torch.dtype
  714. ):
  715. new_grad_output = grad_output * output
  716. grad_input = new_grad_output - output * torch.sum(
  717. new_grad_output, dim=dim, keepdim=True
  718. )
  719. # CPU kernel doesn't respect input_dtype, but following check doesn't work for meta tensor
  720. # if grad_output.device == torch.device("cpu"):
  721. # return grad_input.contiguous()
  722. return _cast_grad_to_input_dtype(grad_output, grad_input, input_dtype).contiguous()
  723. @register_decomposition(aten._log_softmax_backward_data)
  724. @out_wrapper()
  725. @compute_only_pw_cast_for_opmath
  726. def _log_softmax_backward_data(
  727. grad_output: Tensor, output: Tensor, dim: int, input_dtype: torch.dtype
  728. ):
  729. grad_input = grad_output - torch.exp(output) * torch.sum(
  730. grad_output, dim=dim, keepdim=True
  731. )
  732. return _cast_grad_to_input_dtype(grad_output, grad_input, input_dtype)
  733. def _im2col_col2im_indices_along_dim(
  734. input_d, kernel_d, dilation_d, padding_d, stride_d, device
  735. ):
  736. """Utility function to implement im2col and col2im"""
  737. blocks_d = input_d + padding_d * 2 - dilation_d * (kernel_d - 1)
  738. arange_kw = partial(torch.arange, dtype=torch.int64, device=device)
  739. # Stride kernel over input and find starting indices along dim d
  740. blocks_d_indices = arange_kw(0, blocks_d, stride_d).unsqueeze(0)
  741. # Apply dilation on kernel and find its indices along dim d
  742. kernel_grid = arange_kw(0, kernel_d * dilation_d, dilation_d).unsqueeze(-1)
  743. # Broadcast and add kernel starting positions (indices) with
  744. # kernel_grid along dim d, to get block indices along dim d
  745. return blocks_d_indices + kernel_grid
  746. @register_decomposition(aten.im2col)
  747. @out_wrapper()
  748. def im2col(
  749. input: Tensor,
  750. kernel_size: list[int],
  751. dilation: list[int],
  752. padding: list[int],
  753. stride: list[int],
  754. ) -> Tensor:
  755. torch._check(len(kernel_size) == 2, lambda: "im2col(): only 2D kernel supported")
  756. torch._check(len(dilation) == 2, lambda: "im2col(): only 2D dilation supported")
  757. torch._check(len(padding) == 2, lambda: "im2col(): only 2D padding supported")
  758. torch._check(len(stride) == 2, lambda: "im2col(): only 2D stride supported")
  759. def check_positive(param, param_name, strict=True):
  760. cond = all(p > 0 for p in param) if strict else all(p >= 0 for p in param)
  761. torch._check(
  762. cond, lambda: "{param_name} should be greater {'than' zero, but got {param}"
  763. )
  764. check_positive(kernel_size, "kernel_size")
  765. check_positive(dilation, "dilation")
  766. check_positive(dilation, "padding", strict=False)
  767. check_positive(stride, "stride")
  768. shape = input.shape
  769. ndim = len(shape)
  770. torch._check(
  771. ndim in (3, 4) and all(d != 0 for d in shape[-3:]),
  772. lambda: "Expected 3D or 4D (batch mode) tensor for input with possible 0 batch size "
  773. f"and non-zero dimensions, but got: {tuple(shape)}",
  774. )
  775. output_size = tuple(
  776. 1 + (out + 2 * pad - dil * (ker - 1) - 1) // st
  777. for out, pad, dil, ker, st in zip(
  778. shape[-2:], padding, dilation, kernel_size, stride
  779. )
  780. )
  781. torch._check(
  782. all(c > 0 for c in output_size),
  783. lambda: f"Given an input with spatial size {tuple(shape[-2:])}, "
  784. f"kernel_size={kernel_size}, dilation={dilation}, "
  785. f"padding={padding}, stride={stride}, "
  786. "the calculated shape of the array of sliding blocks "
  787. f"is {output_size}, but its components must be at least one.",
  788. )
  789. batched_input = ndim == 4
  790. if not batched_input:
  791. input = input.unsqueeze(0)
  792. batch_dim, channel_dim, input_h, input_w = input.shape
  793. stride_h, stride_w = stride
  794. padding_h, padding_w = padding
  795. dilation_h, dilation_w = dilation
  796. kernel_h, kernel_w = kernel_size
  797. blocks_row_indices = _im2col_col2im_indices_along_dim(
  798. input_h, kernel_h, dilation_h, padding_h, stride_h, input.device
  799. )
  800. blocks_col_indices = _im2col_col2im_indices_along_dim(
  801. input_w, kernel_w, dilation_w, padding_w, stride_w, input.device
  802. )
  803. # Note that F.pad takes (padding_left, padding_right, padding_top, padding_bottom)
  804. # ugh
  805. padded_input = F.pad(input, (padding_w, padding_w, padding_h, padding_h))
  806. blocks_row_indices = blocks_row_indices.unsqueeze(-1).unsqueeze(-1)
  807. output = padded_input[:, :, blocks_row_indices, blocks_col_indices]
  808. output = output.permute(0, 1, 2, 4, 3, 5)
  809. num_blocks_row = blocks_row_indices.size(1)
  810. num_blocks_col = blocks_col_indices.size(1)
  811. output = output.reshape(
  812. batch_dim, channel_dim * kernel_h * kernel_w, num_blocks_row * num_blocks_col
  813. )
  814. if not batched_input:
  815. output = output.squeeze(0)
  816. return output
  817. @register_decomposition(aten.col2im)
  818. @out_wrapper()
  819. @pw_cast_for_opmath
  820. def col2im(
  821. input: Tensor,
  822. output_size: list[int],
  823. kernel_size: list[int],
  824. dilation: list[int],
  825. padding: list[int],
  826. stride: list[int],
  827. ) -> Tensor:
  828. torch._check(len(output_size) == 2, lambda: "only 2D output_size supported")
  829. torch._check(len(kernel_size) == 2, lambda: "only 2D kernel supported")
  830. torch._check(len(dilation) == 2, lambda: "only 2D dilation supported")
  831. torch._check(len(padding) == 2, lambda: "only 2D padding supported")
  832. torch._check(len(stride) == 2, lambda: "only 2D stride supported")
  833. def check_positive(param, param_name, strict=True):
  834. cond = all(p > 0 for p in param) if strict else all(p >= 0 for p in param)
  835. torch._check(
  836. cond, lambda: "{param_name} should be greater than zero, but got {param}"
  837. )
  838. check_positive(kernel_size, "kernel_size")
  839. check_positive(dilation, "dilation")
  840. check_positive(padding, "padding", strict=False)
  841. check_positive(stride, "stride")
  842. check_positive(output_size, "output_size")
  843. shape = input.shape
  844. ndim = len(shape)
  845. torch._check(
  846. ndim in (2, 3) and all(d != 0 for d in shape[-2:]),
  847. lambda: "Expected 2D or 3D (batch mode) tensor for input with possible 0 batch size "
  848. f"and non-zero dimensions, but got: {tuple(shape)}",
  849. )
  850. prod_kernel_size = kernel_size[0] * kernel_size[1]
  851. torch._check(
  852. shape[-2] % prod_kernel_size == 0,
  853. lambda: "Expected size of input's first non-batch dimension to be divisible by the "
  854. f"product of kernel_size, but got input.shape[-2] = {shape[-2]} and "
  855. f"kernel_size={kernel_size}",
  856. )
  857. col = [
  858. 1 + (out + 2 * pad - dil * (ker - 1) - 1) // st
  859. for out, pad, dil, ker, st in zip(
  860. output_size, padding, dilation, kernel_size, stride
  861. )
  862. ]
  863. L = col[0] * col[1]
  864. torch._check(
  865. shape[-1] == L,
  866. lambda: f"Given output_size={output_size}, kernel_size={kernel_size}, "
  867. f"dilation={dilation}, padding={padding}, stride={stride}, "
  868. f"expected input.size(-1) to be {L} but got {shape[-1]}.",
  869. )
  870. torch._check(
  871. L > 0,
  872. lambda: f"Given output_size={output_size}, kernel_size={kernel_size}, "
  873. f"dilation={dilation}, padding={padding}, stride={stride}, "
  874. f"expected input.size(-1) to be {L} but got {shape[-1]}.",
  875. )
  876. batched_input = ndim == 3
  877. if not batched_input:
  878. input = input.unsqueeze(0)
  879. shape = input.shape
  880. out_h, out_w = output_size
  881. stride_h, stride_w = stride
  882. padding_h, padding_w = padding
  883. dilation_h, dilation_w = dilation
  884. kernel_h, kernel_w = kernel_size
  885. # col2im is defined as the backwards of im2col, so we differentiate its decomposition by hand
  886. input = input.reshape([shape[0], shape[1] // prod_kernel_size] + kernel_size + col)
  887. input = input.permute(0, 1, 2, 4, 3, 5)
  888. indices_row = _im2col_col2im_indices_along_dim(
  889. out_h, kernel_h, dilation_h, padding_h, stride_h, input.device
  890. )
  891. indices_row = _unsqueeze_to_dim(indices_row, 4)
  892. indices_col = _im2col_col2im_indices_along_dim(
  893. out_w, kernel_w, dilation_w, padding_w, stride_w, input.device
  894. )
  895. output_padded_size = [o + 2 * p for o, p in zip(output_size, padding)]
  896. output = input.new_zeros(
  897. [shape[0], shape[1] // prod(kernel_size)] + output_padded_size
  898. )
  899. idx = (None, None, indices_row, indices_col)
  900. output = aten._unsafe_index_put(output, idx, input, accumulate=True)
  901. output = F.pad(output, (-padding_w, -padding_w, -padding_h, -padding_h))
  902. if not batched_input:
  903. output = output.squeeze(0)
  904. return output
  905. @register_decomposition(aten.native_dropout_backward)
  906. @out_wrapper()
  907. def native_dropout_backward(grad_output: Tensor, mask: Tensor, scale: float):
  908. # According to the CUDA kernel implementation we should have this test;
  909. # but it seems to fail tests!
  910. # torch._check(mask.dtype == torch.bool, lambda: f"Mask should be Bool Scalar Type {mask.dtype}")
  911. # Mimicking CUDA kernel's behavior for output stride: output follow input's memory format
  912. # This different from TensorIterator's behavior
  913. r = (grad_output * (mask.type_as(grad_output) * scale)).clone(
  914. memory_format=utils.suggest_memory_format(grad_output)
  915. )
  916. return r
  917. @register_decomposition(aten.unfold_backward)
  918. @out_wrapper()
  919. def unfold_backward(
  920. grad: Tensor, input_size: list[int], dimension: int, size: int, step: int
  921. ) -> Tensor:
  922. if len(input_size) == 0:
  923. return torch.squeeze_copy(grad, 0)
  924. dim = utils.canonicalize_dim(len(input_size), dimension)
  925. idx = torch.arange(input_size[dim], device=grad.device, dtype=torch.int32)
  926. idx = idx.unfold(0, size, step).flatten()
  927. grad = grad.movedim(-1, dim + 1).flatten(dim, dim + 1)
  928. # nb. At the moment this generates two kernels in triton
  929. # It could potentially be fused into one call to scatter_reduce,
  930. # in the case step <= size provided scatter_reduce generates 1 kernel
  931. grad_input = grad.new_zeros(input_size)
  932. index = (None,) * dim + (idx,)
  933. return aten._unsafe_index_put(grad_input, index, grad, accumulate=True).contiguous()
  934. @register_decomposition(aten.logit_backward.default)
  935. @pw_cast_for_opmath
  936. def logit_backward(
  937. grad_output: Tensor, self: Tensor, eps: Optional[float] = None
  938. ) -> Tensor:
  939. if eps is not None:
  940. lo = eps
  941. hi = 1.0 - lo
  942. return torch.where(
  943. torch.logical_and(self >= lo, self <= hi),
  944. grad_output / (self * (1.0 - self)),
  945. 0.0,
  946. )
  947. else:
  948. return torch.where(
  949. torch.logical_and(self >= 0.0, self <= 1.0),
  950. grad_output / (self * (1.0 - self)),
  951. self.new_full((), float("nan")),
  952. )
  953. @register_decomposition(aten.dropout)
  954. @aten.dropout.default.py_impl(DispatchKey.CompositeImplicitAutograd)
  955. @aten.dropout.default.py_impl(DispatchKey.Autograd)
  956. def dropout(input: Tensor, p: float, train: Optional[bool]):
  957. if train and p != 0:
  958. return aten.native_dropout(input, p, train)[0]
  959. else:
  960. return input.clone()
  961. @register_decomposition(aten.native_dropout)
  962. @out_wrapper("out0", "out1")
  963. def native_dropout(input: Tensor, p: float, train: Optional[bool]):
  964. if train and p != 0:
  965. if p == 1:
  966. return (torch.zeros_like(input), torch.zeros_like(input, dtype=torch.bool))
  967. if not input.dtype.is_floating_point:
  968. raise RuntimeError(
  969. "result type Float can't be cast to the desired output type Long"
  970. )
  971. bool_mask = torch.rand_like(input) > p
  972. res = bool_mask * input * float(1.0 / (1.0 - p))
  973. return (res, bool_mask)
  974. else:
  975. return (input, torch.ones_like(input, dtype=torch.bool))
  976. @register_decomposition(aten._softmax)
  977. @out_wrapper()
  978. def _softmax(x: Tensor, dim: int, half_to_float: bool):
  979. # eager softmax returns a contiguous tensor. Ensure that decomp also returns
  980. # a contiguous tensor.
  981. x = x.contiguous()
  982. if half_to_float:
  983. assert x.dtype == torch.half
  984. computation_dtype, result_dtype = utils.elementwise_dtypes(
  985. x, type_promotion_kind=utils.ELEMENTWISE_TYPE_PROMOTION_KIND.DEFAULT
  986. )
  987. x = x.to(computation_dtype)
  988. if x.numel() == 0:
  989. unnormalized = torch.exp(x)
  990. else:
  991. x_max = torch.amax(x, dim, keepdim=True)
  992. unnormalized = torch.exp(x - x_max)
  993. result = unnormalized / torch.sum(unnormalized, dim, keepdim=True)
  994. if not half_to_float:
  995. result = result.to(result_dtype)
  996. return result
  997. @register_decomposition(aten._log_softmax)
  998. @out_wrapper(exact_dtype=True)
  999. def _log_softmax(x: Tensor, dim: int, half_to_float: bool):
  1000. # eager log_softmax returns a contiguous tensor. Ensure that decomp also
  1001. # returns a contiguous tensor.
  1002. x = x.contiguous()
  1003. if half_to_float:
  1004. assert x.dtype == torch.half
  1005. computation_dtype, result_dtype = utils.elementwise_dtypes(
  1006. x, type_promotion_kind=utils.ELEMENTWISE_TYPE_PROMOTION_KIND.DEFAULT
  1007. )
  1008. x = x.to(computation_dtype)
  1009. if x.numel() == 0:
  1010. shifted = x
  1011. else:
  1012. x_max = torch.amax(x, dim, keepdim=True)
  1013. shifted = x - x_max
  1014. shifted_logsumexp = torch.log(torch.sum(torch.exp(shifted), dim, keepdim=True))
  1015. result = shifted - shifted_logsumexp
  1016. if not half_to_float:
  1017. result = result.to(result_dtype)
  1018. return result
  1019. @register_decomposition(aten.embedding)
  1020. @out_wrapper()
  1021. def embedding(
  1022. weight: Tensor,
  1023. indices: Tensor,
  1024. padding_idx: int = -1,
  1025. scale_grad_by_freq: bool = False,
  1026. sparse: bool = False,
  1027. ) -> Tensor:
  1028. assert weight.dim() == 2, "'weight' must be 2-D"
  1029. # Nb. scale_grad_by_freq is not used in the forward
  1030. if indices.ndim <= 1:
  1031. # We need this one as weight[indices] calls item() in these cases
  1032. out = weight.index_select(0, indices)
  1033. if indices.ndim == 0:
  1034. out = out.squeeze(0)
  1035. return out
  1036. else:
  1037. return weight[indices]
  1038. @register_decomposition(aten.embedding_dense_backward)
  1039. @out_wrapper()
  1040. def embedding_dense_backward(
  1041. grad_output: Tensor,
  1042. indices: Tensor,
  1043. num_weights: int,
  1044. padding_idx: int,
  1045. scale_grad_by_freq: bool,
  1046. ):
  1047. computation_dtype, result_dtype = utils.elementwise_dtypes(
  1048. grad_output, type_promotion_kind=utils.ELEMENTWISE_TYPE_PROMOTION_KIND.DEFAULT
  1049. )
  1050. grad_output = grad_output.to(computation_dtype)
  1051. indices = _maybe_convert_to_dtype(indices, torch.long) # type: ignore[assignment]
  1052. if scale_grad_by_freq:
  1053. counts = indices.new_zeros((num_weights,))
  1054. ones = torch.ones_like(indices)
  1055. counts = aten._unsafe_index_put(counts, [indices], ones, accumulate=True)
  1056. grad_weights_scale = counts[indices]
  1057. grad_output = grad_output / grad_weights_scale.unsqueeze(-1)
  1058. mask = _unsqueeze_to_dim(indices == padding_idx, grad_output.ndim)
  1059. grad = grad_output.masked_fill(mask, 0)
  1060. grad_weight = grad_output.new_zeros(
  1061. (num_weights,) + grad_output.shape[indices.ndim :]
  1062. )
  1063. return aten._unsafe_index_put(grad_weight, [indices], grad, accumulate=True).to(
  1064. result_dtype
  1065. )
  1066. def prod(x: list[int]):
  1067. r = 1
  1068. for i in x:
  1069. r *= i
  1070. return r
  1071. def _pad_chunk(
  1072. tensors: list[Tensor],
  1073. dim: int,
  1074. num_chunks: int,
  1075. ) -> list[Tensor]:
  1076. padded_tensors = []
  1077. for tensor in tensors:
  1078. tensor_size = tensor.size()
  1079. pad_along_dim = (tensor_size[dim] + num_chunks - 1) // num_chunks * num_chunks
  1080. if pad_along_dim != tensor_size[dim]:
  1081. # Use aten.constant_pad_nd instead of copy_ for functionalization
  1082. pad = [0] * 2 * (tensor.ndim - dim - 1) + [
  1083. 0,
  1084. pad_along_dim - tensor_size[dim],
  1085. ]
  1086. tensor = aten.constant_pad_nd(tensor, pad, 0)
  1087. view_size = tensor_size[:dim] + torch.Size([num_chunks, -1])
  1088. padded_tensors.append(tensor.reshape(view_size))
  1089. return padded_tensors
  1090. def have_same_ndims(tensors: list[Tensor]):
  1091. ndim = tensors[0].ndim
  1092. for tensor in tensors:
  1093. if tensor.ndim != ndim:
  1094. return False
  1095. return True
  1096. def leading_dimension_matches(tensors: list[Tensor], dim: int):
  1097. leading_dim_sizes = tensors[0].size()[:dim]
  1098. for tensor in tensors:
  1099. torch._check(
  1100. tensor.size()[:dim] == leading_dim_sizes,
  1101. lambda: "_chunk_cat expects same sizes of 0,...,dim-1 dimensions for all tensors",
  1102. )
  1103. def _preprocess_chunk_cat_inputs(
  1104. tensors: list[Tensor],
  1105. dim: int,
  1106. num_chunks: int,
  1107. ):
  1108. torch._check(num_chunks >= 1, lambda: "_chunk_cat expects positive num_chunks")
  1109. torch._check(
  1110. len(tensors) > 0, lambda: "_chunk_cat expects a non-empty input tensor list"
  1111. )
  1112. expected_dtype = tensors[0].dtype
  1113. expected_device = tensors[0].device
  1114. for tensor in tensors:
  1115. torch._check(tensor.numel() > 0, lambda: "_chunk_cat expects non-empty tensor")
  1116. torch._check(
  1117. tensor.dtype == expected_dtype,
  1118. lambda: "_chunk_cat expects all input tensors with the same dtype",
  1119. )
  1120. torch._check(
  1121. tensor.device == expected_device,
  1122. lambda: "_chunk_cat expects all inputs tensors on the same device",
  1123. )
  1124. if have_same_ndims(tensors):
  1125. dim = utils.canonicalize_dim(tensors[0].dim(), dim)
  1126. else:
  1127. torch._check(
  1128. dim >= 0,
  1129. lambda: "_chunk_cat expects non-negative dim when input tensors have different ndims",
  1130. )
  1131. for tensor in tensors:
  1132. torch._check(
  1133. dim < tensor.ndim,
  1134. lambda: "_chunk_cat expects dim < ndim for all input tensors",
  1135. )
  1136. leading_dimension_matches(tensors, dim)
  1137. return dim
  1138. @register_decomposition([aten._chunk_cat.default, aten._chunk_cat.out])
  1139. def _chunk_cat(
  1140. tensors: list[Tensor],
  1141. dim: int,
  1142. num_chunks: int,
  1143. out: Optional[Tensor] = None,
  1144. ) -> Tensor:
  1145. dim = _preprocess_chunk_cat_inputs(tensors, dim, num_chunks)
  1146. padded_tensors = _pad_chunk(tensors, dim, num_chunks)
  1147. if out is None:
  1148. return torch.cat(padded_tensors, dim + 1)
  1149. else:
  1150. torch.cat(padded_tensors, dim + 1, out=out)
  1151. return out
  1152. # out_wrapper currently does not allow optional outputs
  1153. @register_decomposition(
  1154. [aten.split_with_sizes_copy.default, aten.split_with_sizes_copy.out]
  1155. )
  1156. def split_with_sizes_copy(
  1157. self: Tensor,
  1158. split_sizes: list[int],
  1159. dim: int = 0,
  1160. out: Optional[list[Tensor]] = None,
  1161. ) -> Optional[list[Tensor]]:
  1162. splits = aten.split_with_sizes(self, split_sizes, dim=dim)
  1163. if out is None:
  1164. return [s.clone(memory_format=torch.contiguous_format) for s in splits]
  1165. else:
  1166. for output, split in zip(out, splits):
  1167. _maybe_resize_out(output, split.shape)
  1168. _safe_copy_out(copy_from=split, copy_to=output, exact_dtype=True)
  1169. return None
  1170. @register_decomposition(aten.unsafe_split.Tensor)
  1171. def unsafe_split(input: Tensor, split_size: int, dim: int = 0) -> tuple[Tensor, ...]:
  1172. return aten.split.Tensor(input, split_size, dim)
  1173. @register_decomposition(aten.unsafe_split_with_sizes.default)
  1174. def unsafe_split_with_sizes(
  1175. input: Tensor, split_sizes: list[int], dim: int = 0
  1176. ) -> tuple[Tensor, ...]:
  1177. return aten.split_with_sizes.default(input, split_sizes, dim)
  1178. @register_decomposition(aten.split.Tensor)
  1179. def split(self: Tensor, split_size: int, dim: int = 0) -> tuple[Tensor, ...]:
  1180. input_sizes = self.shape
  1181. dim_size = input_sizes[dim]
  1182. if split_size == 0:
  1183. assert dim_size == 0
  1184. return (self.detach(),)
  1185. chunks = (dim_size + split_size - 1) // split_size
  1186. # Avoid importing sympy at a module level
  1187. from torch.fx.experimental.symbolic_shapes import guard_int
  1188. chunks = guard_int(chunks)
  1189. split_sizes = [split_size for i in range(chunks)]
  1190. split_sizes[-1] = split_size - (split_size * chunks - dim_size)
  1191. return torch.split(self, split_sizes, dim)
  1192. @aten.tensor_split.tensor_indices_or_sections.py_impl(
  1193. DispatchKey.CompositeImplicitAutograd
  1194. )
  1195. def tensor_split_tensor_indices_or_sections_py_impl(
  1196. self: Tensor,
  1197. tensor_indices_or_sections: Tensor,
  1198. dim: int = 0,
  1199. ) -> tuple[Tensor, ...]:
  1200. assert tensor_indices_or_sections.device.type == "cpu"
  1201. assert tensor_indices_or_sections.dtype == torch.int64
  1202. split_dim = tensor_indices_or_sections.dim()
  1203. torch._check(
  1204. split_dim == 1 or split_dim == 0,
  1205. lambda: "tensor_split expected tensor_indices_or_sections to be a zero-dimensional "
  1206. f"or one-dimensional tensor, but got a tensor with {split_dim} dims",
  1207. )
  1208. if split_dim == 0:
  1209. sections = tensor_indices_or_sections.item()
  1210. assert isinstance(sections, IntLike)
  1211. return self.tensor_split(sections, dim)
  1212. else:
  1213. indices = [i.item() for i in tensor_indices_or_sections]
  1214. # WARNING: Tempted to torch._check_is_size on the indices here? You
  1215. # can't: tensor_split works with negative values in indices:
  1216. #
  1217. # >>> torch.tensor_split(torch.randn(10), torch.tensor([-5, 5]))
  1218. # (tensor([ 0.3540, 2.1074, -0.8507, 1.1639, 0.3055]), tensor([]),
  1219. # tensor([-0.4285, 1.0692, -0.1776, 0.9362, 1.6143]))
  1220. #
  1221. # Sorry, I don't make the rules. Explicitly do the item call in user
  1222. # code if you KNOW that they are non-negative.
  1223. return self.tensor_split(indices, dim)
  1224. # TODO: this doesn't appear to have enough precision in bfloat16
  1225. @register_decomposition(aten.addmm)
  1226. @out_wrapper(exact_dtype=True)
  1227. @pw_cast_for_opmath
  1228. def addmm(self: Tensor, mat1: Tensor, mat2: Tensor, beta: int = 1, alpha: int = 1):
  1229. if not self.is_floating_point() and not self.is_complex():
  1230. beta = int(beta)
  1231. alpha = int(alpha)
  1232. out = alpha * torch.mm(mat1, mat2)
  1233. if beta == 0:
  1234. return out
  1235. # The output of aten.addmm is contiguous, we need to match this behavior in the decomposition.
  1236. # The original implementation 'beta * self + out' would return a strided tensor if `self` is strided.
  1237. # We thus use `out`, the output of torch.mm, which is always contiguous, as the first argument for addition.
  1238. # This is relying on TensorIterator's behavior that it takes higher precedence on the stride of first input.
  1239. # Alternative, we can write `(beta * self + out).contiguous()`, but it introduces another copy in some cases.
  1240. # This implementation is not ideal, and we should revisit this when we have a better solution.
  1241. return out + beta * self
  1242. @register_decomposition(aten._addmm_activation)
  1243. @out_wrapper()
  1244. @pw_cast_for_opmath
  1245. def _addmm_activation(
  1246. self: Tensor,
  1247. mat1: Tensor,
  1248. mat2: Tensor,
  1249. beta: int = 1,
  1250. alpha: int = 1,
  1251. use_gelu: bool = False,
  1252. ):
  1253. out = addmm(self, mat1, mat2, beta, alpha)
  1254. if use_gelu:
  1255. if self.is_cuda:
  1256. return aten.gelu(out, approximate="tanh")
  1257. else:
  1258. return aten.gelu(out)
  1259. return aten.relu(out)
  1260. @register_decomposition(aten.addmv)
  1261. @out_wrapper(exact_dtype=True)
  1262. @pw_cast_for_opmath
  1263. def addmv(self: Tensor, mat1: Tensor, vec: Tensor, beta: int = 1, alpha: int = 1):
  1264. if not self.is_floating_point() and not self.is_complex():
  1265. beta = int(beta)
  1266. alpha = int(alpha)
  1267. out = alpha * torch.mv(mat1, vec)
  1268. if beta == 0:
  1269. return out
  1270. if out.numel() == 0: # handle empty matrix
  1271. return beta * self
  1272. return out + beta * self
  1273. @register_decomposition(aten.native_group_norm_backward.default)
  1274. @pw_cast_for_opmath
  1275. def native_group_norm_backward(
  1276. grad_output: Tensor,
  1277. input: Tensor,
  1278. mean: Tensor,
  1279. rstd: Tensor,
  1280. gamma: Optional[Tensor],
  1281. N: int,
  1282. C: int,
  1283. HxW: int,
  1284. group: int,
  1285. output_mask: list[bool],
  1286. ) -> tuple[Optional[Tensor], Optional[Tensor], Optional[Tensor]]:
  1287. utils.check_same_device(
  1288. grad_output, input, mean, rstd, allow_cpu_scalar_tensors=False
  1289. )
  1290. utils.check_same_shape(input, grad_output, allow_cpu_scalar_tensors=False)
  1291. utils.check_same_shape(mean, rstd, allow_cpu_scalar_tensors=False)
  1292. torch._check(
  1293. input.numel() == N * C * HxW,
  1294. lambda: f"Expect input to have {N * C * HxW} elements",
  1295. )
  1296. torch._check(
  1297. mean.shape == (N, group),
  1298. lambda: f"Expect mean to have shape ({N}, {group}, but got {mean.shape}",
  1299. )
  1300. torch._check(
  1301. gamma is None or gamma.numel() == C,
  1302. lambda: f"Expect gamma to have {C} elements but got {gamma.numel() if gamma is not None else -1}",
  1303. )
  1304. cpg, _rem = divmod(C, group)
  1305. torch._check(
  1306. _rem == 0,
  1307. lambda: f"Expect number of channels {C} to be evenly-divisible by number of groups {group}",
  1308. )
  1309. # Compute Internal gradients
  1310. ds = torch.mul(grad_output, input).view(N, C, HxW).sum(dim=[2])
  1311. db = grad_output.view(N, C, HxW).sum(dim=[2])
  1312. d_input: Optional[Tensor] = None
  1313. d_gamma: Optional[Tensor] = None
  1314. d_bias: Optional[Tensor] = None
  1315. if output_mask[0]:
  1316. s = 1.0 / (HxW * cpg)
  1317. if gamma is not None:
  1318. ds_val = torch.mul(ds, gamma.unsqueeze(0)).reshape(N, group, cpg).sum(2)
  1319. db_val = torch.mul(db, gamma.unsqueeze(0)).reshape(N, group, cpg).sum(2)
  1320. c1 = torch.mul(
  1321. rstd.unsqueeze(-1),
  1322. gamma.reshape(1, group, cpg),
  1323. )
  1324. else:
  1325. ds_val = ds.reshape(N, group, cpg).sum(2)
  1326. db_val = db.reshape(N, group, cpg).sum(2)
  1327. c1 = torch.mul(
  1328. rstd.unsqueeze(-1),
  1329. torch.ones((1, group, cpg), device=rstd.device),
  1330. )
  1331. c2 = (db_val * mean - ds_val) * rstd * rstd * rstd * s
  1332. c3 = -c2 * mean - db_val * rstd * s
  1333. c1 = c1.unsqueeze(-1)
  1334. c2 = _unsqueeze_to_dim(c2, 4)
  1335. c3 = _unsqueeze_to_dim(c3, 4)
  1336. d_input = (
  1337. torch.mul(grad_output.reshape(N, group, cpg, HxW), c1)
  1338. + torch.mul(input.reshape(N, group, cpg, HxW), c2)
  1339. + c3
  1340. )
  1341. d_input = d_input.reshape(input.shape).to(input.dtype)
  1342. if output_mask[1]:
  1343. d_gamma = (
  1344. (
  1345. (ds.view(N, group, cpg) - db.view(N, group, cpg) * mean.unsqueeze(-1))
  1346. * rstd.unsqueeze(-1)
  1347. )
  1348. .sum(dim=[0])
  1349. .reshape(C)
  1350. )
  1351. if output_mask[2]:
  1352. d_bias = db.sum(dim=[0])
  1353. return (d_input, d_gamma, d_bias)
  1354. # out_wrapper currently does not allow optional outputs
  1355. @register_decomposition(aten.native_group_norm_backward.out)
  1356. def native_group_norm_backward_out(
  1357. grad_output: Tensor,
  1358. input: Tensor,
  1359. mean: Tensor,
  1360. rstd: Tensor,
  1361. gamma: Optional[Tensor],
  1362. N: int,
  1363. C: int,
  1364. HxW: int,
  1365. group: int,
  1366. output_mask: list[bool],
  1367. *,
  1368. out0: torch.Tensor,
  1369. out1: torch.Tensor,
  1370. out2: torch.Tensor,
  1371. ) -> tuple[Optional[Tensor], Optional[Tensor], Optional[Tensor]]:
  1372. result = native_group_norm_backward(
  1373. grad_output, input, mean, rstd, gamma, N, C, HxW, group, output_mask
  1374. )
  1375. grad_input = (out0, out1, out2)
  1376. for i, r in enumerate(result):
  1377. if r is not None:
  1378. _maybe_resize_out(grad_input[i], r.shape)
  1379. _safe_copy_out(copy_from=r, copy_to=grad_input[i], exact_dtype=True)
  1380. return grad_input
  1381. def _maybe_cast(x: Optional[Tensor], dtype) -> Optional[Tensor]:
  1382. if x is not None:
  1383. return x.to(dtype)
  1384. return x
  1385. # TODO: Take a closer look at the type promotion semantics
  1386. @register_decomposition(aten.native_layer_norm_backward.default)
  1387. def native_layer_norm_backward(
  1388. grad_out: Tensor,
  1389. input: Tensor,
  1390. normalized_shape: list[int],
  1391. mean: Tensor,
  1392. rstd: Tensor,
  1393. weight: Optional[Tensor],
  1394. bias: Optional[Tensor],
  1395. output_mask: list[bool],
  1396. ) -> tuple[Optional[Tensor], Optional[Tensor], Optional[Tensor]]:
  1397. input_shape = input.shape
  1398. input_ndim = input.dim()
  1399. computation_dtype = utils.get_computation_dtype(input.dtype)
  1400. grad_out_cast, input_cast, weight_cast, bias_cast = (
  1401. x.to(computation_dtype, memory_format=torch.contiguous_format)
  1402. if x is not None
  1403. else x
  1404. for x in (grad_out, input, weight, bias)
  1405. )
  1406. assert grad_out_cast is not None
  1407. axis = input_ndim - len(normalized_shape)
  1408. inner_dims = input_shape[axis:]
  1409. outer_dims = input_shape[:axis]
  1410. inner_dim_indices: list[int] = []
  1411. outer_dim_indices: list[int] = []
  1412. for i in range(input_ndim):
  1413. if i >= axis:
  1414. inner_dim_indices.append(i)
  1415. else:
  1416. outer_dim_indices.append(i)
  1417. N = prod(inner_dims) # type: ignore[arg-type]
  1418. M = prod(outer_dims) # type: ignore[arg-type]
  1419. from torch.fx.experimental.symbolic_shapes import statically_known_true
  1420. if statically_known_true(M == 0) or statically_known_true(N == 0):
  1421. return (
  1422. input.new_zeros(input_shape) if output_mask[0] else None,
  1423. input.new_zeros(input_shape[axis:]) if output_mask[1] else None,
  1424. input.new_zeros(input_shape[axis:]) if output_mask[2] else None,
  1425. )
  1426. mean = _unsqueeze_to_dim(mean, input_cast.dim()) # type: ignore[union-attr]
  1427. rstd = _unsqueeze_to_dim(rstd, input_cast.dim()) # type: ignore[union-attr]
  1428. assert input_cast is not None
  1429. x_hat = (input_cast - mean) * rstd
  1430. if weight_cast is not None:
  1431. grad_x_hat = grad_out_cast * weight_cast
  1432. else:
  1433. grad_x_hat = grad_out_cast
  1434. a = grad_x_hat * N
  1435. b = torch.sum(grad_x_hat, inner_dim_indices, True)
  1436. c1 = torch.mul(grad_x_hat, x_hat)
  1437. c2 = torch.sum(c1, inner_dim_indices, True)
  1438. c3 = torch.mul(x_hat, c2)
  1439. inner = a - b - c3
  1440. d_input: Optional[Tensor] = None
  1441. d_weight: Optional[Tensor] = None
  1442. d_bias: Optional[Tensor] = None
  1443. if output_mask[0]:
  1444. d_input = (rstd / N) * inner
  1445. if output_mask[1] and weight_cast is not None:
  1446. if len(outer_dim_indices) > 0:
  1447. d_weight = torch.sum(grad_out_cast * x_hat, outer_dim_indices, False)
  1448. else:
  1449. d_weight = grad_out_cast * x_hat
  1450. if output_mask[2] and bias_cast is not None:
  1451. if len(outer_dim_indices) > 0:
  1452. d_bias = torch.sum(grad_out_cast, outer_dim_indices, False)
  1453. else:
  1454. d_bias = grad_out_cast.clone()
  1455. return (
  1456. _maybe_cast(d_input, input.dtype),
  1457. _maybe_cast(d_weight, input.dtype),
  1458. _maybe_cast(d_bias, input.dtype),
  1459. )
  1460. # out_wrapper currently does not allow optional outputs
  1461. @register_decomposition(aten.native_layer_norm_backward.out)
  1462. def native_layer_norm_backward_out(
  1463. grad_out: Tensor,
  1464. input: Tensor,
  1465. normalized_shape: list[int],
  1466. mean: Tensor,
  1467. rstd: Tensor,
  1468. weight: Optional[Tensor],
  1469. bias: Optional[Tensor],
  1470. output_mask: list[bool],
  1471. *,
  1472. out0: torch.Tensor,
  1473. out1: torch.Tensor,
  1474. out2: torch.Tensor,
  1475. ) -> tuple[Optional[Tensor], Optional[Tensor], Optional[Tensor]]:
  1476. result = native_layer_norm_backward(
  1477. grad_out, input, normalized_shape, mean, rstd, weight, bias, output_mask
  1478. )
  1479. grad_input = (out0, out1, out2)
  1480. for i, r in enumerate(result):
  1481. if r is not None:
  1482. _maybe_resize_out(grad_input[i], r.shape)
  1483. _safe_copy_out(copy_from=r, copy_to=grad_input[i], exact_dtype=True)
  1484. return grad_input
  1485. @register_decomposition(aten._fused_rms_norm_backward.default)
  1486. def _fused_rms_norm_backward(
  1487. grad_out: Tensor,
  1488. input: Tensor,
  1489. normalized_shape: list[int],
  1490. rstd: Tensor,
  1491. weight: Optional[Tensor],
  1492. output_mask: list[bool],
  1493. ) -> tuple[Optional[Tensor], Optional[Tensor]]:
  1494. input_shape = input.shape
  1495. input_ndim = input.dim()
  1496. computation_dtype = utils.get_computation_dtype(input.dtype)
  1497. grad_out_cast = grad_out.to(
  1498. computation_dtype, memory_format=torch.contiguous_format
  1499. )
  1500. input_cast = input.to(computation_dtype, memory_format=torch.contiguous_format)
  1501. weight_cast = (
  1502. weight.to(computation_dtype, memory_format=torch.contiguous_format)
  1503. if weight is not None
  1504. else None
  1505. )
  1506. assert grad_out_cast is not None
  1507. axis = input_ndim - len(normalized_shape)
  1508. inner_dims = input_shape[axis:]
  1509. outer_dims = input_shape[:axis]
  1510. inner_dim_indices: list[int] = []
  1511. outer_dim_indices: list[int] = []
  1512. for i in range(input_ndim):
  1513. if i >= axis:
  1514. inner_dim_indices.append(i)
  1515. else:
  1516. outer_dim_indices.append(i)
  1517. N = prod(inner_dims) # type: ignore[arg-type]
  1518. M = prod(outer_dims) # type: ignore[arg-type]
  1519. from torch.fx.experimental.symbolic_shapes import guard_or_false
  1520. if guard_or_false(M == 0) or guard_or_false(N == 0):
  1521. return (
  1522. input.new_zeros(input_shape) if output_mask[0] else None,
  1523. input.new_zeros(input_shape[axis:]) if output_mask[1] else None,
  1524. )
  1525. rstd = _unsqueeze_to_dim(rstd, input_cast.dim()) # type: ignore[union-attr]
  1526. if weight_cast is not None:
  1527. grad_x_hat = grad_out_cast * weight_cast
  1528. else:
  1529. grad_x_hat = grad_out_cast
  1530. d_input: Optional[Tensor] = None
  1531. d_weight: Optional[Tensor] = None
  1532. x_hat = input_cast * rstd
  1533. if output_mask[0]:
  1534. sum_val = torch.sum(x_hat * grad_x_hat, dim=inner_dim_indices, keepdim=True)
  1535. d_input = (grad_x_hat - (x_hat / N) * sum_val) * rstd
  1536. if output_mask[1] and weight_cast is not None:
  1537. d_weight_full_shape = grad_out_cast * x_hat
  1538. if len(outer_dim_indices) > 0:
  1539. d_weight = torch.sum(
  1540. d_weight_full_shape, dim=outer_dim_indices, keepdim=False
  1541. )
  1542. else:
  1543. d_weight = d_weight_full_shape
  1544. return (
  1545. _maybe_cast(d_input, input.dtype),
  1546. _maybe_cast(d_weight, input.dtype),
  1547. )
  1548. def native_batch_norm_helper(
  1549. input: Tensor,
  1550. weight: Optional[Tensor],
  1551. bias: Optional[Tensor],
  1552. running_mean: Optional[Tensor],
  1553. running_var: Optional[Tensor],
  1554. training: bool,
  1555. momentum: float,
  1556. eps: float,
  1557. functional: bool,
  1558. ) -> tuple[Tensor, Tensor, Tensor, Optional[Tensor], Optional[Tensor]]:
  1559. reduction_dims = [0] + list(range(2, input.dim()))
  1560. computation_dtype = utils.get_computation_dtype(input.dtype)
  1561. new_running_mean = running_mean
  1562. new_running_var = running_var
  1563. if training:
  1564. computation_dtype = utils.get_computation_dtype(input.dtype)
  1565. input_acc = input.to(dtype=computation_dtype)
  1566. biased_var, mean = torch.var_mean(
  1567. input_acc, dim=reduction_dims, correction=0, keepdim=True
  1568. )
  1569. rstd = torch.rsqrt(biased_var + eps)
  1570. output = (input - mean) * rstd
  1571. save_mean = torch.squeeze(mean, reduction_dims)
  1572. save_rstd = torch.squeeze(rstd, reduction_dims)
  1573. if running_mean is not None:
  1574. new_running_mean = momentum * save_mean + (1 - momentum) * running_mean
  1575. if not functional:
  1576. running_mean.copy_(new_running_mean)
  1577. if running_var is not None:
  1578. n = input.numel() / input.shape[1]
  1579. # This doesn't strictly match eager's numerics, which accumulates var sum and then directly applies the correction
  1580. # But... that would require re-implementing var here, for negligible numerics gain on a tensor whose
  1581. # numerics probably don't matter.
  1582. squeezed_var = torch.squeeze(biased_var, reduction_dims)
  1583. unbiased_var = squeezed_var * (n / (n - 1))
  1584. new_running_var = momentum * unbiased_var + (1 - momentum) * running_var
  1585. if not functional:
  1586. running_var.copy_(new_running_var)
  1587. else:
  1588. assert running_mean is not None and running_var is not None
  1589. running_mean = running_mean.to(dtype=computation_dtype, copy=True)
  1590. new_running_mean = running_mean
  1591. running_var = running_var.to(dtype=computation_dtype, copy=True)
  1592. new_running_var = running_var
  1593. mean = running_mean
  1594. invstd = 1 / (torch.sqrt(running_var + eps))
  1595. # Very annoying inconsistency where CPU and CUDA give different shapes
  1596. if input.device.type != "cpu":
  1597. save_mean = running_mean
  1598. save_rstd = invstd
  1599. else:
  1600. save_mean = input.new_zeros((0,))
  1601. save_rstd = input.new_zeros((0,))
  1602. mean = _unsqueeze_to_dim(mean, input.dim() - 1)
  1603. invstd = _unsqueeze_to_dim(invstd, input.dim() - 1)
  1604. output = (input - mean) * invstd
  1605. if weight is not None:
  1606. weight = weight.flatten()
  1607. weight = _unsqueeze_to_dim(weight, input.dim() - 1)
  1608. output = output * weight
  1609. if bias is not None:
  1610. bias = bias.flatten()
  1611. bias = _unsqueeze_to_dim(bias, input.dim() - 1)
  1612. output = output + bias
  1613. if input.device.type == "cpu":
  1614. save_mean = save_mean.to(dtype=input.dtype)
  1615. save_rstd = save_rstd.to(dtype=input.dtype)
  1616. return (
  1617. output.to(dtype=input.dtype),
  1618. save_mean,
  1619. save_rstd,
  1620. new_running_mean,
  1621. new_running_var,
  1622. )
  1623. @register_decomposition(aten.native_batch_norm)
  1624. @out_wrapper("out", "save_mean", "save_invstd")
  1625. def native_batch_norm(
  1626. input: Tensor,
  1627. weight: Optional[Tensor],
  1628. bias: Optional[Tensor],
  1629. running_mean: Optional[Tensor],
  1630. running_var: Optional[Tensor],
  1631. training: bool,
  1632. momentum: float,
  1633. eps: float,
  1634. ) -> tuple[Tensor, Tensor, Tensor]:
  1635. output, save_mean, save_rstd, _, _ = native_batch_norm_helper(
  1636. input, weight, bias, running_mean, running_var, training, momentum, eps, False
  1637. )
  1638. return output, save_mean, save_rstd
  1639. # TODO: this decomposition is NOT here to stay. We would much prefer replacing native_batch_norm
  1640. # with our new correctly schema'd _native_batch_norm_legit and its variants, but
  1641. # we cannot do that immediately in the C++ because it would be forwards incompatible
  1642. # with some mobile use cases.
  1643. #
  1644. # Since this change is most impactful for aot autograd/functionalization, we simply
  1645. # register this decomposition on the Autograd key for the python dispatcher (which is
  1646. # currently only used by aot autograd/functionalization and no one else, really).
  1647. # In two weeks or so, we should remove this decomposition and phase out the current native_batch_norm
  1648. # to be _native_batch_norm_legit and have the right schema (stating that there are input mutations).
  1649. @aten.native_batch_norm.default.py_impl(DispatchKey.Autograd)
  1650. @aten.native_batch_norm.default.py_impl(DispatchKey.CompositeImplicitAutograd)
  1651. def native_batch_norm_decomposition(
  1652. input: Tensor,
  1653. weight: Optional[Tensor],
  1654. bias: Optional[Tensor],
  1655. running_mean: Optional[Tensor],
  1656. running_var: Optional[Tensor],
  1657. training: bool,
  1658. momentum: float,
  1659. eps: float,
  1660. ) -> tuple[Tensor, Tensor, Tensor]:
  1661. if running_mean is None and running_var is None:
  1662. return aten._native_batch_norm_legit(
  1663. input, weight, bias, training, momentum, eps
  1664. )
  1665. if running_mean is None:
  1666. raise RuntimeError(
  1667. "running_mean is None, but running_var is provided. "
  1668. "They should both be None or both be provided."
  1669. )
  1670. if running_var is None:
  1671. raise RuntimeError(
  1672. "running_var is None, but running_mean is provided. "
  1673. "They should both be None or both be provided."
  1674. )
  1675. if training:
  1676. # HACK: batch norm consolidation should clean this up so this op doesn't take in a training arg.
  1677. return aten._native_batch_norm_legit(
  1678. input, weight, bias, running_mean, running_var, training, momentum, eps
  1679. )
  1680. else:
  1681. return aten._native_batch_norm_legit_no_training(
  1682. input, weight, bias, running_mean, running_var, momentum, eps
  1683. )
  1684. @aten.unsafe_chunk.default.py_impl(DispatchKey.CompositeImplicitAutograd)
  1685. def unsafe_chunk_py_impl(tensor, chunks, dim=0) -> list[Tensor]:
  1686. dim_size = tensor.size(dim)
  1687. split_size = (dim_size + chunks - 1) // chunks
  1688. if split_size == 0 and dim_size == 0:
  1689. split_sizes = [split_size for _ in chunks]
  1690. split_sizes[chunks - 1] = split_size - (split_size * chunks - dim_size)
  1691. return torch.ops.aten.unsafe_split_with_sizes.default(tensor, split_sizes, dim)
  1692. return torch.ops.aten.unsafe_split.Tensor(tensor, split_size, dim)
  1693. @register_decomposition(aten._native_batch_norm_legit_no_training.default)
  1694. def _native_batch_norm_legit_no_training(
  1695. input: Tensor,
  1696. weight: Optional[Tensor],
  1697. bias: Optional[Tensor],
  1698. running_mean: Tensor,
  1699. running_var: Tensor,
  1700. momentum: float,
  1701. eps: float,
  1702. ) -> tuple[Tensor, Tensor, Tensor]:
  1703. return aten._native_batch_norm_legit.default(
  1704. input,
  1705. weight,
  1706. bias,
  1707. running_mean,
  1708. running_var,
  1709. False, # training
  1710. momentum,
  1711. eps,
  1712. )
  1713. @register_decomposition(aten._native_batch_norm_legit.default)
  1714. def _native_batch_norm_legit(
  1715. input: Tensor,
  1716. weight: Optional[Tensor],
  1717. bias: Optional[Tensor],
  1718. running_mean: Tensor,
  1719. running_var: Tensor,
  1720. training: bool,
  1721. momentum: float,
  1722. eps: float,
  1723. ) -> tuple[Tensor, Tensor, Tensor]:
  1724. output, save_mean, save_rstd, _, _ = native_batch_norm_helper(
  1725. input, weight, bias, running_mean, running_var, training, momentum, eps, False
  1726. )
  1727. return output, save_mean, save_rstd
  1728. @register_decomposition(aten._native_batch_norm_legit.no_stats)
  1729. def _native_batch_norm_legit_no_stats(
  1730. input: Tensor,
  1731. weight: Optional[Tensor],
  1732. bias: Optional[Tensor],
  1733. training: bool,
  1734. momentum: float,
  1735. eps: float,
  1736. ) -> tuple[Tensor, Tensor, Tensor]:
  1737. output, save_mean, save_rstd, _, _ = native_batch_norm_helper(
  1738. input, weight, bias, None, None, training, momentum, eps, False
  1739. )
  1740. return output, save_mean, save_rstd
  1741. @register_decomposition(aten._native_batch_norm_legit_functional.default)
  1742. def _native_batch_norm_legit_functional(
  1743. input: Tensor,
  1744. weight: Optional[Tensor],
  1745. bias: Optional[Tensor],
  1746. running_mean: Tensor,
  1747. running_var: Tensor,
  1748. training: bool,
  1749. momentum: float,
  1750. eps: float,
  1751. ) -> tuple[Tensor, Tensor, Tensor, Tensor, Tensor]:
  1752. (
  1753. output,
  1754. save_mean,
  1755. save_rstd,
  1756. new_running_mean,
  1757. new_running_var,
  1758. ) = native_batch_norm_helper(
  1759. input, weight, bias, running_mean, running_var, training, momentum, eps, True
  1760. )
  1761. assert new_running_mean is not None, "new_running_mean should not be None"
  1762. assert new_running_var is not None, "new_running_var should not be None"
  1763. return output, save_mean, save_rstd, new_running_mean, new_running_var
  1764. def _get_batch_norm_reserve_tensor(
  1765. input: Tensor,
  1766. weight: Optional[Tensor],
  1767. bias: Optional[Tensor],
  1768. running_mean: Tensor,
  1769. running_var: Tensor,
  1770. eps: float,
  1771. training: bool,
  1772. ) -> Tensor:
  1773. """
  1774. Return a reserve tensor for batch norm, used only by cudnn to pass forward state to the
  1775. backward pass. This is needed for `_batch_norm_with_update` and `_batch_norm_no_update`,
  1776. which support a variety of backends including cudnn. We create this tensor here to get
  1777. the correct shape in the traced graph if we detect that will call the cudnn kernel,
  1778. and rely on DCE to avoid materializing this tensor.
  1779. """
  1780. backend = torch._C._select_batch_norm_backend( # type: ignore[attr-defined]
  1781. input, weight, bias, running_mean, running_var, True, eps
  1782. )
  1783. reserve_size = 0
  1784. if backend == torch._C._BatchNormBackend.Cudnn: # type: ignore[attr-defined]
  1785. reserve_size = torch._C._get_cudnn_batch_norm_reserve_space_size( # type: ignore[attr-defined]
  1786. input, training
  1787. )
  1788. return torch.empty(
  1789. reserve_size, dtype=torch.uint8, layout=input.layout, device=input.device
  1790. )
  1791. @register_decomposition(aten._batch_norm_with_update.default)
  1792. def _batch_norm_with_update(
  1793. input: Tensor,
  1794. weight: Optional[Tensor],
  1795. bias: Optional[Tensor],
  1796. running_mean: Tensor,
  1797. running_var: Tensor,
  1798. momentum: float,
  1799. eps: float,
  1800. ) -> tuple[Tensor, Tensor, Tensor, Tensor]:
  1801. output, save_mean, save_rstd, _, _ = native_batch_norm_helper(
  1802. input,
  1803. weight,
  1804. bias,
  1805. running_mean,
  1806. running_var,
  1807. True, # training
  1808. momentum,
  1809. eps,
  1810. False, # functional
  1811. )
  1812. reserve = _get_batch_norm_reserve_tensor(
  1813. input, weight, bias, running_mean, running_var, eps, training=True
  1814. )
  1815. return output, save_mean, save_rstd, reserve
  1816. @register_decomposition(aten._batch_norm_with_update_functional.default)
  1817. def _batch_norm_with_update_functional(
  1818. input: Tensor,
  1819. weight: Optional[Tensor],
  1820. bias: Optional[Tensor],
  1821. running_mean: Tensor,
  1822. running_var: Tensor,
  1823. momentum: float,
  1824. eps: float,
  1825. ) -> tuple[Tensor, Tensor, Tensor, Tensor, Tensor, Tensor]:
  1826. (
  1827. output,
  1828. save_mean,
  1829. save_rstd,
  1830. new_rm,
  1831. new_rv,
  1832. ) = native_batch_norm_helper(
  1833. input, weight, bias, running_mean, running_var, True, momentum, eps, True
  1834. )
  1835. reserve = _get_batch_norm_reserve_tensor(
  1836. input, weight, bias, running_mean, running_var, eps, training=True
  1837. )
  1838. assert new_rm is not None, "new_running_mean should not be None"
  1839. assert new_rv is not None, "new_running_var should not be None"
  1840. return (output, save_mean, save_rstd, reserve, new_rm, new_rv)
  1841. @register_decomposition(aten._batch_norm_no_update.default)
  1842. def _batch_norm_no_update(
  1843. input: Tensor,
  1844. weight: Optional[Tensor],
  1845. bias: Optional[Tensor],
  1846. running_mean: Tensor,
  1847. running_var: Tensor,
  1848. momentum: float,
  1849. eps: float,
  1850. ) -> tuple[Tensor, Tensor, Tensor, Tensor]:
  1851. output, save_mean, save_rstd, _, _ = native_batch_norm_helper(
  1852. input,
  1853. weight,
  1854. bias,
  1855. running_mean,
  1856. running_var,
  1857. False, # training
  1858. momentum,
  1859. eps,
  1860. False, # functional
  1861. )
  1862. reserve = _get_batch_norm_reserve_tensor(
  1863. input, weight, bias, running_mean, running_var, eps, training=False
  1864. )
  1865. return output, save_mean, save_rstd, reserve
  1866. @register_decomposition(aten._fused_dropout)
  1867. @out_wrapper("out0", "out1")
  1868. @pw_cast_for_opmath
  1869. def _fused_dropout_decomposition(input, p, generator=None):
  1870. assert generator is None
  1871. mask = (torch.rand_like(input) < p).to(dtype=torch.uint8)
  1872. res = mask.type_as(input) * input * (1.0 / p)
  1873. return (res, mask)
  1874. @register_decomposition(aten._to_copy)
  1875. @out_wrapper()
  1876. def _to_copy(
  1877. x: Union[Tensor, NumberType],
  1878. *,
  1879. dtype: Optional[torch.dtype] = None,
  1880. layout=None,
  1881. device: Optional[torch.device] = None,
  1882. pin_memory: bool = False,
  1883. non_blocking: bool = False,
  1884. memory_format: Optional[torch.memory_format] = None,
  1885. ):
  1886. assert not layout or layout == torch.strided, "TODO"
  1887. assert not pin_memory, "TODO"
  1888. assert isinstance(x, (torch.Tensor, int, float, bool, complex))
  1889. if device is None and dtype is None and memory_format is None:
  1890. if isinstance(x, torch.Tensor):
  1891. return x.clone()
  1892. else:
  1893. return x
  1894. dtype_converted = False
  1895. if isinstance(x, torch.Tensor):
  1896. x_tensor = x
  1897. else:
  1898. x_tensor = torch.scalar_tensor(x)
  1899. if device is not None and device != x_tensor.device:
  1900. # avoid conversions on cpu
  1901. if dtype is not None and device.type == "cpu":
  1902. x_tensor = torch._prims.convert_element_type(x_tensor, dtype)
  1903. dtype_converted = True
  1904. x_tensor = torch._prims.device_put(x_tensor, device, non_blocking)
  1905. if dtype is not None and not dtype_converted:
  1906. x_tensor = torch._prims.convert_element_type(x_tensor, dtype)
  1907. dtype_converted = True
  1908. if memory_format is not None: # no ref/prim for memory format
  1909. return torch.clone(x_tensor, memory_format=memory_format)
  1910. return x_tensor
  1911. # Questionable decompositions
  1912. # This is only valid if we're running the graph without autograd, such as if the backward pass has been traced.
  1913. # Note that this decomposition causes issues with in-place ops
  1914. @register_decomposition([aten.detach, aten.lift, aten.lift_fresh])
  1915. @out_wrapper()
  1916. def nop_decomposition(x):
  1917. return aten.alias(x)
  1918. # Also register to the Autograd dispatch key, so this decomp can run above autograd.
  1919. # native_batch_norm needs to decompose into other ops before autograd.
  1920. @aten.cudnn_batch_norm.default.py_impl(DispatchKey.Autograd)
  1921. @register_decomposition(aten.cudnn_batch_norm)
  1922. @out_wrapper("out0", "out1", "out2", "out3")
  1923. def cudnn_batch_norm(
  1924. input: Tensor,
  1925. weight: Tensor,
  1926. bias: Optional[Tensor],
  1927. running_mean: Optional[Tensor],
  1928. running_var: Optional[Tensor],
  1929. training: bool,
  1930. exponential_average_factor: float,
  1931. epsilon: float,
  1932. ):
  1933. a, b, c = aten.native_batch_norm(
  1934. input,
  1935. weight,
  1936. bias,
  1937. running_mean,
  1938. running_var,
  1939. training,
  1940. exponential_average_factor,
  1941. epsilon,
  1942. )
  1943. # Cudnn return running mean and variance when training is True
  1944. if training:
  1945. return (a, b, c, input.new_zeros((0,), dtype=torch.uint8))
  1946. return (
  1947. a,
  1948. weight.new_zeros((0,)),
  1949. weight.new_zeros((0,)),
  1950. input.new_zeros((0,), dtype=torch.uint8),
  1951. )
  1952. def _broadcast_batch_norm_backward(x, broadcast_mask):
  1953. for axis, mask in enumerate(broadcast_mask):
  1954. if mask == 1 and not (axis < x.ndim and x.shape[axis] == mask):
  1955. x = x.unsqueeze(axis)
  1956. return x
  1957. @register_decomposition(aten.batch_norm_backward.default)
  1958. def batch_norm_backward(
  1959. grad_out: Tensor,
  1960. input: Tensor,
  1961. weight: Optional[Tensor],
  1962. running_mean: Optional[Tensor],
  1963. running_var: Optional[Tensor],
  1964. save_mean: Optional[Tensor],
  1965. save_invstd: Optional[Tensor],
  1966. train: bool,
  1967. eps: float,
  1968. output_mask: list[bool],
  1969. reserve: Tensor,
  1970. ) -> tuple[Tensor, Optional[Tensor], Optional[Tensor]]:
  1971. return native_batch_norm_backward(
  1972. grad_out,
  1973. input,
  1974. weight,
  1975. running_mean,
  1976. running_var,
  1977. save_mean,
  1978. save_invstd,
  1979. train,
  1980. eps,
  1981. output_mask,
  1982. )
  1983. @register_decomposition(aten.native_batch_norm_backward.default)
  1984. def native_batch_norm_backward(
  1985. grad_out: Tensor,
  1986. input: Tensor,
  1987. weight: Optional[Tensor],
  1988. running_mean: Optional[Tensor],
  1989. running_var: Optional[Tensor],
  1990. save_mean: Optional[Tensor],
  1991. save_invstd: Optional[Tensor],
  1992. train: bool,
  1993. eps: float,
  1994. output_mask: list[bool],
  1995. ) -> tuple[Tensor, Optional[Tensor], Optional[Tensor]]:
  1996. input_dtype = input.dtype
  1997. if weight is not None:
  1998. weight_dtype = weight.dtype
  1999. else:
  2000. weight_dtype = input_dtype
  2001. computation_dtype = utils.get_computation_dtype(input.dtype)
  2002. (
  2003. grad_out_cast,
  2004. input_cast,
  2005. weight_cast,
  2006. running_mean_cast,
  2007. running_var_cast,
  2008. save_mean_cast,
  2009. save_invstd_cast,
  2010. ) = (
  2011. x.to(computation_dtype) if x is not None else x
  2012. for x in (
  2013. grad_out,
  2014. input,
  2015. weight,
  2016. running_mean,
  2017. running_var,
  2018. save_mean,
  2019. save_invstd,
  2020. )
  2021. )
  2022. input_shape = input.shape
  2023. input_rank = input.dim()
  2024. assert input_rank >= 2, "rank of the input must be at least 2"
  2025. axis = 1
  2026. num_features = prod(list(input_shape)) / input_shape[axis]
  2027. mean = save_mean_cast
  2028. invstd = save_invstd_cast
  2029. if train:
  2030. assert mean is not None and invstd is not None
  2031. else:
  2032. assert running_mean_cast is not None and running_var_cast is not None
  2033. mean = running_mean_cast
  2034. invstd = torch.rsqrt(running_var_cast + eps)
  2035. broadcast_mask: list[int] = [1] * input_rank
  2036. broadcast_mask[axis] = input_shape[axis]
  2037. reduction_axes: list[int] = []
  2038. for i in range(input_rank):
  2039. if i != axis:
  2040. reduction_axes.append(i)
  2041. mean = _broadcast_batch_norm_backward(mean, broadcast_mask) # type: ignore[arg-type]
  2042. norm = 1.0 / num_features
  2043. grad_output_sum = torch.sum(grad_out_cast, reduction_axes) # type: ignore[arg-type]
  2044. dot_p = torch.sum(grad_out_cast * (input_cast - mean), reduction_axes) # type: ignore[operator]
  2045. grad_mean = _broadcast_batch_norm_backward(grad_output_sum * norm, broadcast_mask)
  2046. proj_scale = _broadcast_batch_norm_backward(
  2047. torch.mul(dot_p * norm, invstd * invstd), # type: ignore[operator]
  2048. broadcast_mask,
  2049. )
  2050. if weight_cast is None:
  2051. grad_scale = _broadcast_batch_norm_backward(invstd, broadcast_mask) * 1.0 # type: ignore[arg-type]
  2052. else:
  2053. grad_scale = _broadcast_batch_norm_backward(
  2054. invstd * weight_cast, broadcast_mask
  2055. )
  2056. if train:
  2057. proj = (input_cast - mean) * proj_scale # type: ignore[operator]
  2058. grad_input = ((grad_out_cast - proj) - grad_mean) * grad_scale
  2059. else:
  2060. grad_input = grad_out_cast * grad_scale
  2061. if output_mask[1]:
  2062. grad_weight = dot_p * invstd
  2063. else:
  2064. grad_weight = None # "None" doesn't work with vjp, should use zeros for vjp
  2065. if output_mask[2]:
  2066. grad_bias = grad_output_sum
  2067. else:
  2068. grad_bias = None # "None" doesn't work with vjp, should use zeros for vjp
  2069. return (
  2070. grad_input.to(input_dtype),
  2071. _maybe_cast(grad_weight, weight_dtype),
  2072. _maybe_cast(grad_bias, weight_dtype),
  2073. )
  2074. # out_wrapper currently does not allow optional outputs
  2075. @register_decomposition(aten.native_batch_norm_backward.out)
  2076. def native_batch_norm_backward_out(
  2077. grad_out: Tensor,
  2078. input: Tensor,
  2079. weight: Optional[Tensor],
  2080. running_mean: Optional[Tensor],
  2081. running_var: Optional[Tensor],
  2082. save_mean: Optional[Tensor],
  2083. save_invstd: Optional[Tensor],
  2084. train: bool,
  2085. eps: float,
  2086. output_mask: list[bool],
  2087. *,
  2088. out0: torch.Tensor,
  2089. out1: torch.Tensor,
  2090. out2: torch.Tensor,
  2091. ) -> tuple[Tensor, Optional[Tensor], Optional[Tensor]]:
  2092. result = native_batch_norm_backward(
  2093. grad_out,
  2094. input,
  2095. weight,
  2096. running_mean,
  2097. running_var,
  2098. save_mean,
  2099. save_invstd,
  2100. train,
  2101. eps,
  2102. output_mask,
  2103. )
  2104. grad_input = (out0, out1, out2)
  2105. for i, r in enumerate(result):
  2106. if r is not None:
  2107. _maybe_resize_out(grad_input[i], r.shape)
  2108. _safe_copy_out(copy_from=r, copy_to=grad_input[i], exact_dtype=True)
  2109. return grad_input
  2110. @register_decomposition(aten.miopen_batch_norm_backward)
  2111. @out_wrapper("out0", "out1", "out2")
  2112. def miopen_batch_norm_backward(
  2113. input: Tensor,
  2114. grad_output: Tensor,
  2115. weight: Tensor,
  2116. running_mean: Optional[Tensor],
  2117. running_var: Optional[Tensor],
  2118. save_mean: Optional[Tensor],
  2119. save_var: Optional[Tensor],
  2120. epsilon: float,
  2121. ):
  2122. return aten.native_batch_norm_backward(
  2123. grad_output,
  2124. input,
  2125. weight,
  2126. running_mean,
  2127. running_var,
  2128. save_mean,
  2129. save_var,
  2130. True,
  2131. epsilon,
  2132. [True, True, True],
  2133. )
  2134. @register_decomposition(aten.cudnn_batch_norm_backward)
  2135. @out_wrapper("out0", "out1", "out2")
  2136. def cudnn_batch_norm_backward(
  2137. input: Tensor,
  2138. grad_output: Tensor,
  2139. weight: Tensor,
  2140. running_mean: Optional[Tensor],
  2141. running_var: Optional[Tensor],
  2142. save_mean: Optional[Tensor],
  2143. save_var: Optional[Tensor],
  2144. epsilon: float,
  2145. reserveSpace: Tensor,
  2146. ):
  2147. return aten.native_batch_norm_backward(
  2148. grad_output,
  2149. input,
  2150. weight,
  2151. running_mean,
  2152. running_var,
  2153. save_mean,
  2154. save_var,
  2155. True,
  2156. epsilon,
  2157. [True, True, True],
  2158. )
  2159. @register_decomposition(aten._adaptive_avg_pool2d)
  2160. @out_wrapper()
  2161. @pw_cast_for_opmath
  2162. def adaptive_avg_pool2d(input: Tensor, output_size: tuple[int, int]):
  2163. # Preconditions
  2164. device = input.device
  2165. shape = input.shape
  2166. ndim = len(shape)
  2167. torch._check(
  2168. ndim in (3, 4),
  2169. lambda: f"adaptive_avg_pool2d(): Expected 3D or 4D tensor, but got {ndim}",
  2170. )
  2171. for d in input.shape[-2:]:
  2172. torch._check(
  2173. d != 0,
  2174. lambda: "adaptive_avg_pool2d(): Expected input to have non-zero size for "
  2175. f"non-batch dimensions, but input has shape {tuple(shape)}.",
  2176. )
  2177. # Optimisation (we should also do this in the kernel implementation)
  2178. if shape[-2] % output_size[-2] == 0 and shape[-1] % output_size[-1] == 0:
  2179. stride = tuple(i // o for i, o in zip(shape[-2:], output_size))
  2180. kernel = tuple(
  2181. i - (o - 1) * s for i, o, s in zip(shape[-2:], output_size, stride)
  2182. )
  2183. return torch.nn.functional.avg_pool2d(input, kernel, stride)
  2184. def start_index(a, b, c):
  2185. return torch.div(a * c, b, rounding_mode="trunc")
  2186. def end_index(a, b, c):
  2187. return torch.div((a + 1) * c + b - 1, b, rounding_mode="trunc")
  2188. def compute_idx(in_size, out_size):
  2189. orange = torch.arange(out_size, device=device, dtype=torch.int64)
  2190. i0 = start_index(orange, out_size, in_size)
  2191. # Let length = end_index - start_index, i.e. the length of the pooling kernels
  2192. # length.max() can be computed analytically as follows:
  2193. maxlength = in_size // out_size + 1
  2194. in_size_mod = in_size % out_size
  2195. # adaptive = True iff there are kernels with different lengths
  2196. adaptive = not (in_size_mod == 0 or out_size % in_size_mod == 0)
  2197. if adaptive:
  2198. maxlength += 1
  2199. elif in_size_mod == 0:
  2200. maxlength -= 1
  2201. range_max = torch.arange(maxlength, device=device, dtype=torch.int64)
  2202. idx = i0.unsqueeze(-1) + range_max
  2203. if adaptive:
  2204. # Need to clamp to avoid accessing out-of-bounds memory
  2205. # TODO make minimum accept scalars
  2206. maxval = torch.scalar_tensor(
  2207. in_size - 1, dtype=idx.dtype, device=idx.device
  2208. )
  2209. idx = torch.minimum(idx, maxval)
  2210. # Compute the length
  2211. i1 = end_index(orange, out_size, in_size)
  2212. length = i1 - i0
  2213. else:
  2214. length = maxlength
  2215. return idx, length, range_max, adaptive
  2216. # length is not None if it's constant, otherwise we'll need to compute it
  2217. idxh, length_h, range_max_h, adaptive_h = compute_idx(shape[-2], output_size[-2])
  2218. idxw, length_w, range_max_w, adaptive_w = compute_idx(shape[-1], output_size[-1])
  2219. vals = input[..., _unsqueeze_to_dim(idxh, 4), idxw]
  2220. # Shortcut for the simpler case
  2221. if not adaptive_h and not adaptive_w:
  2222. return torch.mean(vals, dim=(-3, -1))
  2223. def maybe_mask(vals, length, range_max, adaptive, dim):
  2224. if isinstance(length, IntLike):
  2225. return vals, length
  2226. else:
  2227. # zero-out the things we didn't really want to select
  2228. assert dim < 0
  2229. # hack
  2230. mask = range_max >= length.unsqueeze(-1)
  2231. if dim == -2:
  2232. mask = _unsqueeze_to_dim(mask, 4)
  2233. vals = torch.masked_fill(vals, mask, 0.0)
  2234. # Compute the length of each window
  2235. length = _unsqueeze_to_dim(length, -dim)
  2236. return vals, length
  2237. vals, length_h = maybe_mask(
  2238. vals, length_h, range_max_h, adaptive=adaptive_h, dim=-2
  2239. )
  2240. vals, length_w = maybe_mask(
  2241. vals, length_w, range_max_w, adaptive=adaptive_w, dim=-1
  2242. )
  2243. # We unroll the sum as we assume that the kernels are going to be small
  2244. ret = None
  2245. for i, j in product(range(vals.shape[-3]), range(vals.shape[-1])):
  2246. if ret is None:
  2247. ret = vals[..., i, :, j]
  2248. else:
  2249. ret = ret + vals[..., i, :, j]
  2250. return ret / (length_h * length_w)
  2251. def _max_unpoolnd(
  2252. self: TensorLike, indices: TensorLike, output_size: list[int], dim: int
  2253. ):
  2254. # If the input tensors self and indices came from max_pool call as
  2255. # required by the documentation, this operation is deterministic
  2256. # because that ensures that if there are two entries in `indices`
  2257. # tensor that are equal, the corresponding values in `self` are also
  2258. # equal. If this condition is not satisfied, the operation is
  2259. # non-deterministic as one of the different values in `self` 'wins'.
  2260. utils.alert_not_deterministic(f"max_unpooling{dim}d_forward_out")
  2261. nc = reduce(operator.mul, self.shape[:-dim])
  2262. hw = reduce(operator.mul, output_size)
  2263. indices_nc_shape = [1] * self.ndim
  2264. indices_nc_shape[:-dim] = self.shape[:-dim]
  2265. indices_flat = (
  2266. indices + aten.arange(nc, device=self.device).view(indices_nc_shape) * hw
  2267. ).reshape(-1)
  2268. output = self.new_zeros(list(self.shape[:-dim]) + list(output_size))
  2269. return aten._unsafe_index_put(
  2270. output.reshape(-1), [indices_flat], self.reshape(-1), accumulate=False
  2271. ).view(output.shape)
  2272. @register_decomposition(aten.max_unpool2d)
  2273. @out_wrapper()
  2274. def max_unpool2d(
  2275. self: TensorLike,
  2276. indices: TensorLike,
  2277. output_size: list[int],
  2278. ):
  2279. torch._check(
  2280. indices.dtype == torch.int64,
  2281. lambda: f"elements in indices should be type int64 but got: {indices.dtype}",
  2282. )
  2283. torch._check(
  2284. len(output_size) == 2,
  2285. lambda: (
  2286. f"There should be exactly two elements (height, width) in output_size, "
  2287. f"but got {len(output_size)} elements."
  2288. ),
  2289. )
  2290. torch._check(
  2291. self.ndim in (3, 4),
  2292. lambda: (
  2293. f"Input to max_unpooling2d should be a 3d or 4d Tensor, "
  2294. f"but got a tensor with {self.ndim} dimensions."
  2295. ),
  2296. )
  2297. torch._check(
  2298. self.shape == indices.shape,
  2299. lambda: (
  2300. f"Expected shape of indices to be same as that of the input tensor ({self.shape}) "
  2301. f"but got indices tensor with shape: {indices.shape}"
  2302. ),
  2303. )
  2304. for i in range(1, self.ndim):
  2305. torch._check(
  2306. self.size(i) > 0,
  2307. lambda: (
  2308. f"max_unpooling2d(): "
  2309. f"Expected input to have non-zero size for non-batch dimensions, "
  2310. f"but got {self.shape} with dimension {i} being empty."
  2311. ),
  2312. )
  2313. return _max_unpoolnd(self, indices, output_size, 2)
  2314. @register_decomposition(aten.max_unpool3d)
  2315. @out_wrapper()
  2316. def max_unpool3d(
  2317. input: TensorLike,
  2318. indices: TensorLike,
  2319. output_size: list[int],
  2320. stride: list[int],
  2321. padding: list[int],
  2322. ):
  2323. torch._check(
  2324. indices.dtype == torch.int64, lambda: "elements in indices should be type int64"
  2325. )
  2326. torch._check(
  2327. input.ndim in (4, 5),
  2328. lambda: f"Input to max_unpooling3d should be a 4d or 5d Tensor, but got a tensor with {input.ndim} dimensions.",
  2329. )
  2330. torch._check(
  2331. len(output_size) == 3,
  2332. lambda: (
  2333. f"There should be exactly three elements (depth, height, width) in output_size, "
  2334. f"but got {len(output_size)} elements."
  2335. ),
  2336. )
  2337. torch._check(
  2338. len(stride) == 3,
  2339. lambda: f"There should be exactly three elements (depth, height, width) in stride, but got: {len(stride)} elements.",
  2340. )
  2341. torch._check(
  2342. len(padding) == 3,
  2343. lambda: f"There should be exactly three elements (depth, height, width) in padding, but got: {len(padding)} elements.",
  2344. )
  2345. torch._check(
  2346. input.shape == indices.shape,
  2347. lambda: (
  2348. f"Expected shape of indices to be same as that of the input tensor ({input.shape}) "
  2349. f"but got indices tensor with shape: {indices.shape}"
  2350. ),
  2351. )
  2352. for i in range(1, input.ndim):
  2353. torch._check(
  2354. input.size(i) > 0,
  2355. lambda: (
  2356. f"max_unpooling3d(): "
  2357. f"Expected input to have non-zero size for non-batch dimensions, "
  2358. f"but got {input.shape} with dimension {i} being empty."
  2359. ),
  2360. )
  2361. torch._check(
  2362. stride[0] > 0 and stride[1] > 0 and stride[2] > 0,
  2363. lambda: f"strides should be greater than zero, but got stride: {stride}",
  2364. )
  2365. return _max_unpoolnd(input, indices, output_size, 3)
  2366. @register_decomposition(aten.index_add_)
  2367. def index_add_(
  2368. x: TensorLike,
  2369. dim: int,
  2370. index: TensorLike,
  2371. tensor: TensorLike,
  2372. *,
  2373. alpha: NumberType = 1,
  2374. ):
  2375. return _index_add(x, dim, index, tensor, inplace=True, alpha=alpha)
  2376. @register_decomposition(aten.index_add)
  2377. @out_wrapper()
  2378. def index_add(
  2379. x: TensorLike,
  2380. dim: int,
  2381. index: TensorLike,
  2382. tensor: TensorLike,
  2383. *,
  2384. alpha: NumberType = 1,
  2385. ):
  2386. return _index_add(x, dim, index, tensor, inplace=False, alpha=alpha)
  2387. def _index_add(
  2388. x: TensorLike,
  2389. dim: int,
  2390. index: TensorLike,
  2391. tensor: TensorLike,
  2392. *,
  2393. inplace: bool,
  2394. alpha: NumberType = 1,
  2395. ):
  2396. dim = utils.canonicalize_dims(x.ndim, dim)
  2397. torch._check(
  2398. index.ndim <= 1,
  2399. lambda: f"Index should have dimension 1 or 0 (got {index.ndim})",
  2400. )
  2401. index_size = index.size(0) if index.ndim == 1 else 1
  2402. tensor_size = tensor.size(dim) if tensor.ndim > 0 else 1
  2403. torch._check(
  2404. tensor_size == index_size,
  2405. lambda: f"Number of indices ({index_size}) should be equal to tensor.size(dim) ({tensor_size}), for {dim=}",
  2406. )
  2407. if alpha != 1:
  2408. python_type = utils.dtype_to_type(x.dtype)
  2409. torch._check(
  2410. python_type == bool
  2411. or utils.is_weakly_lesser_type(type(alpha), python_type),
  2412. lambda: f"alpha argument of type {type(alpha)} cannot be safely cast to type {python_type}!",
  2413. )
  2414. tensor = tensor * alpha
  2415. # Treat scalars as elements of \R^1
  2416. zero_dim = x.ndim == 0
  2417. x1 = x.unsqueeze(0) if zero_dim else x
  2418. idx = (None,) * dim + (index,)
  2419. index_put = aten.index_put_ if inplace else aten.index_put
  2420. out = index_put(x1, idx, tensor, accumulate=True)
  2421. if inplace:
  2422. return x
  2423. else:
  2424. return out.squeeze(0) if zero_dim else out.contiguous()
  2425. @register_decomposition(aten.pad_sequence.default)
  2426. @aten.pad_sequence.default.py_impl(DispatchKey.CompositeImplicitAutograd)
  2427. def pad_sequence(sequences, batch_first=False, padding_value=0.0):
  2428. torch._check(len(sequences) > 0, lambda: "received an empty list of sequences")
  2429. sequences_size = len(sequences)
  2430. max_size = sequences[0].size()
  2431. trailing_dims = max_size[1:]
  2432. max_len = max(x.size(0) for x in sequences)
  2433. if batch_first:
  2434. out_dims = (sequences_size, max_len)
  2435. else:
  2436. out_dims = (max_len, sequences_size)
  2437. out_dims = out_dims + trailing_dims
  2438. out = sequences[0].new_full(out_dims, padding_value)
  2439. dim_paddings = (0, 0) * len(trailing_dims)
  2440. for i in range(sequences_size):
  2441. currseq = sequences[i]
  2442. row = aten.constant_pad_nd(
  2443. currseq, dim_paddings + (0, max_len - currseq.size(0)), padding_value
  2444. )
  2445. if batch_first:
  2446. out = aten.select_scatter(out, row, dim=0, index=i)
  2447. else:
  2448. out = aten.select_scatter(out, row, dim=1, index=i)
  2449. return out
  2450. @register_decomposition(aten.index_copy_)
  2451. def index_copy_(x: TensorLike, dim: int, index: TensorLike, tensor: TensorLike):
  2452. return _index_copy(x, dim, index, tensor, inplace=True)
  2453. @register_decomposition(aten.index_copy)
  2454. @out_wrapper()
  2455. def index_copy(x: TensorLike, dim: int, index: TensorLike, tensor: TensorLike):
  2456. return _index_copy(x, dim, index, tensor, inplace=False)
  2457. def _index_copy(
  2458. x: TensorLike, dim: int, index: TensorLike, tensor: TensorLike, *, inplace: bool
  2459. ):
  2460. dim = utils.canonicalize_dims(x.ndim, dim)
  2461. torch._check(
  2462. index.ndim <= 1,
  2463. lambda: f"Index should have dimension 1 or 0 (got {index.ndim})",
  2464. )
  2465. # Treat scalars as elements of \R^1
  2466. zero_dim = x.ndim == 0
  2467. x1 = x.unsqueeze(0) if zero_dim else x
  2468. index = index.unsqueeze(0) if index.ndim == 0 else index
  2469. idx = (None,) * dim + (index,)
  2470. index_put = aten.index_put_ if inplace else aten.index_put
  2471. out = index_put(x1, idx, tensor)
  2472. if inplace:
  2473. return x
  2474. else:
  2475. return out.squeeze(0) if zero_dim else out.contiguous()
  2476. # nb: Should use acc_t, not op_math
  2477. @register_decomposition(aten.log_sigmoid_forward)
  2478. @out_wrapper("output", "buffer")
  2479. @pw_cast_for_opmath
  2480. def log_sigmoid_forward(self: Tensor) -> tuple[Tensor, Tensor]:
  2481. min = torch.minimum(self.new_zeros(()), self)
  2482. z = torch.exp(-torch.abs(self))
  2483. if self.is_cuda or self.is_xpu:
  2484. buffer = self.new_zeros((0,))
  2485. else:
  2486. buffer = z
  2487. return min - torch.log1p(z), buffer
  2488. @register_decomposition(aten.uniform)
  2489. @out_wrapper()
  2490. def uniform(
  2491. x: Tensor,
  2492. low: Union[bool, int, float] = 0.0,
  2493. high: Union[bool, int, float] = 1.0,
  2494. generator: Optional[torch.Generator] = None,
  2495. ):
  2496. return prims._uniform_helper(
  2497. x.shape,
  2498. low=sym_float(low),
  2499. high=sym_float(high),
  2500. dtype=x.dtype,
  2501. device=x.device,
  2502. generator=generator,
  2503. )
  2504. @register_decomposition(aten.uniform_)
  2505. def uniform_(self, low=0, high=1, generator=None):
  2506. return self.copy_(uniform(self, low, high, generator))
  2507. # aten/src/ATen/native/UpSample.cpp compute_output_size
  2508. def upsample_compute_output_size(input_size, output_size, scale_factors):
  2509. spatial_dimensions = len(input_size) - 2
  2510. if output_size is not None:
  2511. torch._check(
  2512. scale_factors is None,
  2513. lambda: "Must specify exactly one of output_size and scale_factors",
  2514. )
  2515. torch._check(len(output_size) == spatial_dimensions, lambda: "")
  2516. return output_size
  2517. if scale_factors is not None:
  2518. # NB: this isn't necessary lol
  2519. torch._check(
  2520. output_size is None,
  2521. lambda: "Must specify exactly one of output_size and scale_factors",
  2522. )
  2523. torch._check(len(scale_factors) == spatial_dimensions, lambda: "")
  2524. output_size = []
  2525. for i, s in enumerate(scale_factors):
  2526. if int(s) == s:
  2527. output_size.append(input_size[i + 2] * int(s))
  2528. else:
  2529. output_size.append(sym_int(input_size[i + 2] * s))
  2530. return output_size
  2531. torch._check(
  2532. False, lambda: "Must specify exactly one of output_size and scale_factors"
  2533. )
  2534. def get_scale_value(scales, idx):
  2535. if scales is None:
  2536. return None
  2537. return scales[idx]
  2538. @register_decomposition(aten.upsample_nearest1d.vec)
  2539. @register_decomposition(aten.upsample_nearest2d.vec)
  2540. @register_decomposition(aten.upsample_nearest3d.vec)
  2541. @aten.upsample_nearest1d.vec.py_impl(DispatchKey.CompositeImplicitAutograd)
  2542. @aten.upsample_nearest1d.vec.py_impl(DispatchKey.Autograd)
  2543. @aten.upsample_nearest2d.vec.py_impl(DispatchKey.CompositeImplicitAutograd)
  2544. @aten.upsample_nearest2d.vec.py_impl(DispatchKey.Autograd)
  2545. @aten.upsample_nearest3d.vec.py_impl(DispatchKey.CompositeImplicitAutograd)
  2546. @aten.upsample_nearest3d.vec.py_impl(DispatchKey.Autograd)
  2547. def _upsample_nearest_vec(
  2548. input: Tensor,
  2549. output_size: Optional[list[int]],
  2550. scale_factors: Optional[list[float]],
  2551. ) -> Tensor:
  2552. osize = upsample_compute_output_size(input.size(), output_size, scale_factors)
  2553. scales = (
  2554. scale_factors if scale_factors else [None] * len(osize) # type: ignore[list-item]
  2555. )
  2556. return _upsample_nearest(input, osize, scales)
  2557. @register_decomposition(aten._upsample_nearest_exact1d.vec)
  2558. @register_decomposition(aten._upsample_nearest_exact2d.vec)
  2559. @register_decomposition(aten._upsample_nearest_exact3d.vec)
  2560. @aten._upsample_nearest_exact1d.vec.py_impl(DispatchKey.CompositeImplicitAutograd)
  2561. @aten._upsample_nearest_exact1d.vec.py_impl(DispatchKey.Autograd)
  2562. @aten._upsample_nearest_exact2d.vec.py_impl(DispatchKey.CompositeImplicitAutograd)
  2563. @aten._upsample_nearest_exact2d.vec.py_impl(DispatchKey.Autograd)
  2564. @aten._upsample_nearest_exact3d.vec.py_impl(DispatchKey.CompositeImplicitAutograd)
  2565. @aten._upsample_nearest_exact3d.vec.py_impl(DispatchKey.Autograd)
  2566. def _upsample_nearest_exact_vec(
  2567. input: Tensor,
  2568. output_size: Optional[list[int]],
  2569. scale_factors: Optional[list[float]],
  2570. ) -> Tensor:
  2571. osize = upsample_compute_output_size(input.size(), output_size, scale_factors)
  2572. scales = (
  2573. scale_factors if scale_factors else [None] * len(osize) # type: ignore[list-item]
  2574. )
  2575. return _upsample_nearest(input, osize, scales, exact=True)
  2576. def _compute_upsample_nearest_indices(input, output_size, scales, exact=False):
  2577. # For each dim in output_size, compute the set of input indices used
  2578. # to produce the upsampled output.
  2579. indices = []
  2580. num_spatial_dims = len(output_size)
  2581. offset = 0.5 if exact else 0.0
  2582. for d in range(num_spatial_dims):
  2583. # Math matches aten/src/ATen/native/cpu/UpSampleKernel.cpp
  2584. #
  2585. # Indices are computed as following:
  2586. # scale = isize / osize
  2587. # Case: exact=False
  2588. # input_index = floor(output_index * scale)
  2589. # Same as OpenCV INTER_NEAREST
  2590. #
  2591. # Case: exact=False
  2592. # index_f32 = (output_index + 0.5) * scale - 0.5
  2593. # input_index = round(index_f32)
  2594. # Same as Pillow and Scikit-Image/Scipy ndi.zoom
  2595. osize = output_size[d]
  2596. isize = input.shape[-num_spatial_dims + d]
  2597. scale = isize / (isize * scales[d]) if scales[d] is not None else isize / osize
  2598. output_indices = torch.arange(osize, dtype=torch.float32, device=input.device)
  2599. input_indices = ((output_indices + offset) * scale).to(torch.int64)
  2600. for _ in range(num_spatial_dims - 1 - d):
  2601. input_indices = input_indices.unsqueeze(-1)
  2602. indices.append(input_indices)
  2603. return indices
  2604. @register_decomposition([aten.upsample_nearest1d.default, aten.upsample_nearest1d.out])
  2605. @aten.upsample_nearest1d.default.py_impl(DispatchKey.CompositeImplicitAutograd)
  2606. @aten.upsample_nearest1d.default.py_impl(DispatchKey.Autograd)
  2607. @out_wrapper(preserve_memory_format=True, exact_dtype=True)
  2608. def upsample_nearest1d(
  2609. input: Tensor,
  2610. output_size: list[int],
  2611. scales: Optional[float] = None,
  2612. ) -> Tensor:
  2613. return _upsample_nearest(input, output_size, [scales])
  2614. @register_decomposition(
  2615. [aten._upsample_nearest_exact1d.default, aten._upsample_nearest_exact1d.out]
  2616. )
  2617. @aten._upsample_nearest_exact1d.default.py_impl(DispatchKey.CompositeImplicitAutograd)
  2618. @aten._upsample_nearest_exact1d.default.py_impl(DispatchKey.Autograd)
  2619. @out_wrapper(preserve_memory_format=True, exact_dtype=True)
  2620. def upsample_nearest_exact1d(
  2621. input: Tensor,
  2622. output_size: list[int],
  2623. scales: Optional[float] = None,
  2624. ) -> Tensor:
  2625. return _upsample_nearest(input, output_size, [scales], exact=True)
  2626. @register_decomposition([aten.upsample_nearest2d.default, aten.upsample_nearest2d.out])
  2627. @aten.upsample_nearest2d.default.py_impl(DispatchKey.CompositeImplicitAutograd)
  2628. @aten.upsample_nearest2d.default.py_impl(DispatchKey.Autograd)
  2629. @out_wrapper(preserve_memory_format=True, exact_dtype=True)
  2630. def upsample_nearest2d(
  2631. input: Tensor,
  2632. output_size: list[int],
  2633. scales_h: Optional[float] = None,
  2634. scales_w: Optional[float] = None,
  2635. ) -> Tensor:
  2636. return _upsample_nearest(input, output_size, [scales_h, scales_w])
  2637. @register_decomposition(
  2638. [aten._upsample_nearest_exact2d.default, aten._upsample_nearest_exact2d.out]
  2639. )
  2640. @aten._upsample_nearest_exact2d.default.py_impl(DispatchKey.CompositeImplicitAutograd)
  2641. @aten._upsample_nearest_exact2d.default.py_impl(DispatchKey.Autograd)
  2642. @out_wrapper(preserve_memory_format=True, exact_dtype=True)
  2643. def _upsample_nearest_exact2d(
  2644. input: Tensor,
  2645. output_size: list[int],
  2646. scales_h: Optional[float] = None,
  2647. scales_w: Optional[float] = None,
  2648. ) -> Tensor:
  2649. return _upsample_nearest(input, output_size, [scales_h, scales_w], exact=True)
  2650. @register_decomposition([aten.upsample_nearest3d.default, aten.upsample_nearest3d.out])
  2651. @aten.upsample_nearest3d.default.py_impl(DispatchKey.CompositeImplicitAutograd)
  2652. @aten.upsample_nearest3d.default.py_impl(DispatchKey.Autograd)
  2653. @out_wrapper(preserve_memory_format=True, exact_dtype=True)
  2654. def upsample_nearest3d(
  2655. input: Tensor,
  2656. output_size: list[int],
  2657. scales_d: Optional[float] = None,
  2658. scales_h: Optional[float] = None,
  2659. scales_w: Optional[float] = None,
  2660. ) -> Tensor:
  2661. return _upsample_nearest(input, output_size, [scales_d, scales_h, scales_w])
  2662. @register_decomposition(
  2663. [aten._upsample_nearest_exact3d.default, aten._upsample_nearest_exact3d.out]
  2664. )
  2665. @aten._upsample_nearest_exact3d.default.py_impl(DispatchKey.CompositeImplicitAutograd)
  2666. @aten._upsample_nearest_exact3d.default.py_impl(DispatchKey.Autograd)
  2667. @out_wrapper(preserve_memory_format=True, exact_dtype=True)
  2668. def _upsample_nearest_exact3d(
  2669. input: Tensor,
  2670. output_size: list[int],
  2671. scales_d: Optional[float] = None,
  2672. scales_h: Optional[float] = None,
  2673. scales_w: Optional[float] = None,
  2674. ) -> Tensor:
  2675. return _upsample_nearest(
  2676. input, output_size, [scales_d, scales_h, scales_w], exact=True
  2677. )
  2678. @pw_cast_for_opmath
  2679. def _upsample_nearest(
  2680. input: Tensor,
  2681. output_size: list[int],
  2682. scales: list[Optional[float]],
  2683. exact: bool = False,
  2684. ) -> Tensor:
  2685. spatial_indices = _compute_upsample_nearest_indices(
  2686. input, output_size, scales, exact=exact
  2687. )
  2688. indices = [None, None] + spatial_indices
  2689. result = aten._unsafe_index(input, indices)
  2690. if result.ndim == 4:
  2691. # convert output to correct memory format, if necessary
  2692. memory_format = utils.suggest_memory_format(input)
  2693. # following "heuristic: only use channels_last path when it's faster than the contiguous path"
  2694. n_channels = input.shape[1]
  2695. if input.device.type == "cuda" and n_channels < 4:
  2696. memory_format = torch.contiguous_format
  2697. result = result.contiguous(memory_format=memory_format)
  2698. return result
  2699. def gather_params(params, has_biases, has_projections):
  2700. if has_biases and has_projections:
  2701. group_size = 5
  2702. elif has_biases:
  2703. group_size = 4
  2704. elif has_projections:
  2705. group_size = 3
  2706. else:
  2707. group_size = 2
  2708. assert len(params) % group_size == 0, len(params)
  2709. return [
  2710. tuple(params[i : i + group_size]) for i in range(0, len(params), group_size)
  2711. ]
  2712. def params_hiddens(params, hiddens, i, bidirectional):
  2713. if bidirectional:
  2714. cur_params, cur_hidden = params[2 * i], hiddens[2 * i]
  2715. bidir_params, bidir_hidden = params[2 * i + 1], hiddens[2 * i + 1]
  2716. else:
  2717. cur_params, cur_hidden = params[i], hiddens[i]
  2718. bidir_params, bidir_hidden = None, None
  2719. return cur_params, cur_hidden, bidir_params, bidir_hidden
  2720. def update_hidden_for_packed(cur_hidden, last_batch_size, batch_size, hiddens):
  2721. assert last_batch_size > batch_size
  2722. hiddens.append(cur_hidden.narrow(0, batch_size, last_batch_size - batch_size))
  2723. return cur_hidden.narrow(0, 0, batch_size)
  2724. def update_hidden_for_packed_reverse(
  2725. cur_hidden, last_batch_size, batch_size, inp_hidden
  2726. ):
  2727. if last_batch_size == batch_size:
  2728. return cur_hidden
  2729. assert last_batch_size < batch_size
  2730. return torch.concat(
  2731. (
  2732. cur_hidden,
  2733. inp_hidden.narrow(0, last_batch_size, batch_size - last_batch_size),
  2734. )
  2735. )
  2736. def one_layer_rnn_data(
  2737. inp, hidden, params, has_biases, hidden_fn, batch_sizes, reverse=False
  2738. ):
  2739. ih_weight = params[0]
  2740. hh_weight = params[1]
  2741. ih_bias = params[2] if has_biases else None
  2742. hh_bias = params[3] if has_biases else None
  2743. step_output = []
  2744. hiddens: list[torch.Tensor] = []
  2745. last_batch_size = batch_sizes[-1] if reverse else batch_sizes[0]
  2746. cur_hidden = hidden.narrow(0, 0, last_batch_size)
  2747. split_inp = torch.split(inp, list(batch_sizes))
  2748. if reverse:
  2749. split_inp = split_inp[::-1]
  2750. for inp in split_inp:
  2751. i = inp.shape[0]
  2752. if last_batch_size == i:
  2753. pass # don't update cur_hidden
  2754. # this will only happen when reverse=False, since batch sizes are sorted largest -> smallest
  2755. elif reverse:
  2756. cur_hidden = update_hidden_for_packed_reverse(
  2757. cur_hidden, last_batch_size, i, hidden
  2758. )
  2759. else:
  2760. cur_hidden = update_hidden_for_packed(
  2761. cur_hidden, last_batch_size, i, hiddens
  2762. )
  2763. cur_hidden = hidden_fn(inp, cur_hidden, ih_weight, ih_bias, hh_weight, hh_bias)
  2764. last_batch_size = i
  2765. step_output.append(cur_hidden)
  2766. if reverse:
  2767. step_output.reverse()
  2768. else:
  2769. hiddens.append(cur_hidden)
  2770. hiddens.reverse()
  2771. out = torch.cat(step_output, 0)
  2772. hidden_out = torch.cat(hiddens, 0) if not reverse else cur_hidden
  2773. return out, hidden_out
  2774. def rnn_cell(nonlinearity):
  2775. def inner(i, cur_hidden, ih_weight, ih_bias, hh_weight, hh_bias):
  2776. return nonlinearity(F.linear(cur_hidden, hh_weight, hh_bias) + i)
  2777. return inner
  2778. def rnn_cell_data(nonlinearity):
  2779. def inner(i, cur_hidden, ih_weight, ih_bias, hh_weight, hh_bias):
  2780. i = F.linear(i, ih_weight, ih_bias)
  2781. return nonlinearity(F.linear(cur_hidden, hh_weight, hh_bias) + i)
  2782. return inner
  2783. def one_layer_rnn(inp, hidden, params, has_biases, hidden_fn, reverse=False):
  2784. ih_weight = params[0]
  2785. hh_weight = params[1]
  2786. ih_bias = params[2] if has_biases else None
  2787. hh_bias = params[3] if has_biases else None
  2788. precomputed_input = F.linear(inp, ih_weight, ih_bias)
  2789. precomputed_input = precomputed_input.flip(0) if reverse else precomputed_input
  2790. cur_hidden = hidden.unsqueeze(0)
  2791. step_output = []
  2792. for i in precomputed_input:
  2793. cur_hidden = hidden_fn(i, cur_hidden, ih_weight, ih_bias, hh_weight, hh_bias)
  2794. step_output.append(cur_hidden)
  2795. if reverse:
  2796. step_output.reverse()
  2797. out = torch.cat(step_output, 0)
  2798. return out, cur_hidden.squeeze(0)
  2799. def mkldnn_one_layer_lstm(inp, hidden, params, has_biases, reverse=False):
  2800. w0 = params[0]
  2801. w1 = params[1]
  2802. if has_biases:
  2803. w2 = params[2]
  2804. w3 = params[3]
  2805. else:
  2806. w2 = torch.zeros(w0.size())
  2807. w3 = torch.zeros(w1.size())
  2808. hx = hidden[0].unsqueeze(0)
  2809. cx = hidden[1].unsqueeze(0)
  2810. batch_sizes: list[int] = []
  2811. mode = 2 # third_party/ideep/include/ideep/abstract_types.hpp: ideep::rnn_kind::LSTM = 2
  2812. hidden_size = hx.size(2)
  2813. num_layers = 1
  2814. # _rnn_helper already handles bidirectional and batch_first so we hard-code them to False here
  2815. bidirectional = False
  2816. batch_first = False
  2817. train = False
  2818. # If batch_first, inp has been permuted in _rnn_helper. Convert to contiguous here.
  2819. # Same as aten/src/ATen/native/mkldnn/RNN.cpp: mkldnn_rnn: input = input.contiguous();
  2820. inp = inp.contiguous()
  2821. hx = hx.contiguous()
  2822. cx = cx.contiguous()
  2823. outputs = torch.ops.aten.mkldnn_rnn_layer.default(
  2824. inp,
  2825. w0,
  2826. w1,
  2827. w2,
  2828. w3,
  2829. hx,
  2830. cx,
  2831. reverse,
  2832. batch_sizes,
  2833. mode,
  2834. hidden_size,
  2835. num_layers,
  2836. has_biases,
  2837. bidirectional,
  2838. batch_first,
  2839. train,
  2840. )
  2841. y, hy, cy = outputs[0], outputs[1], outputs[2]
  2842. return y, (hy.squeeze(0), cy.squeeze(0))
  2843. def _rnn_helper(
  2844. input,
  2845. hidden,
  2846. params,
  2847. has_biases,
  2848. num_layers,
  2849. dropout,
  2850. train,
  2851. bidirectional,
  2852. batch_first,
  2853. layer_fn,
  2854. ):
  2855. input = input.transpose(0, 1) if batch_first else input
  2856. final_hiddens = []
  2857. for i in range(num_layers):
  2858. cur_params, cur_hidden, bidir_params, bidir_hidden = params_hiddens(
  2859. params, hidden, i, bidirectional
  2860. )
  2861. dropout = dropout if (train and num_layers < i - 1) else 0.0
  2862. fwd_inp, fwd_hidden = layer_fn(input, cur_hidden, cur_params, has_biases)
  2863. final_hiddens.append(fwd_hidden)
  2864. if bidirectional:
  2865. bwd_inp, bwd_hidden = layer_fn(
  2866. input, bidir_hidden, bidir_params, has_biases, reverse=True
  2867. )
  2868. final_hiddens.append(bwd_hidden)
  2869. if bidirectional:
  2870. input = torch.cat([fwd_inp, bwd_inp], fwd_inp.dim() - 1) # type: ignore[possibly-undefined]
  2871. else:
  2872. input = fwd_inp
  2873. if dropout != 0 and train and i < num_layers - 1:
  2874. input = torch.dropout(input, dropout, train=True)
  2875. input = input.transpose(0, 1) if batch_first else input
  2876. return input, final_hiddens
  2877. @register_decomposition(aten.rnn_tanh.input)
  2878. @aten.rnn_tanh.input.py_impl(DispatchKey.CompositeImplicitAutograd)
  2879. @aten.rnn_tanh.input.py_impl(DispatchKey.Autograd)
  2880. def rnn_tanh_input(
  2881. input,
  2882. hx,
  2883. params,
  2884. has_biases,
  2885. num_layers,
  2886. dropout,
  2887. train,
  2888. bidirectional,
  2889. batch_first,
  2890. ):
  2891. hidden = hx.unbind(0)
  2892. params = gather_params(params, has_biases, False)
  2893. out, final_hiddens = _rnn_helper(
  2894. input,
  2895. hidden,
  2896. params,
  2897. has_biases,
  2898. num_layers,
  2899. dropout,
  2900. train,
  2901. bidirectional,
  2902. batch_first,
  2903. partial(one_layer_rnn, hidden_fn=rnn_cell(torch.tanh)),
  2904. )
  2905. return out, torch.stack(final_hiddens, 0)
  2906. @register_decomposition(aten.rnn_relu.input)
  2907. @aten.rnn_relu.input.py_impl(DispatchKey.CompositeImplicitAutograd)
  2908. @aten.rnn_relu.input.py_impl(DispatchKey.Autograd)
  2909. def rnn_relu_input(
  2910. input,
  2911. hx,
  2912. params,
  2913. has_biases,
  2914. num_layers,
  2915. dropout,
  2916. train,
  2917. bidirectional,
  2918. batch_first,
  2919. ):
  2920. hidden = hx.unbind(0)
  2921. params = gather_params(params, has_biases, False)
  2922. out, final_hiddens = _rnn_helper(
  2923. input,
  2924. hidden,
  2925. params,
  2926. has_biases,
  2927. num_layers,
  2928. dropout,
  2929. train,
  2930. bidirectional,
  2931. batch_first,
  2932. partial(one_layer_rnn, hidden_fn=rnn_cell(torch.relu)),
  2933. )
  2934. return out, torch.stack(final_hiddens, 0)
  2935. @register_decomposition(aten.rnn_relu.data)
  2936. @aten.rnn_relu.data.py_impl(DispatchKey.CompositeImplicitAutograd)
  2937. @aten.rnn_relu.data.py_impl(DispatchKey.Autograd)
  2938. def rnn_relu_data(
  2939. data,
  2940. batch_sizes,
  2941. hx,
  2942. params,
  2943. has_biases,
  2944. num_layers,
  2945. dropout,
  2946. train,
  2947. bidirectional,
  2948. ):
  2949. hidden = hx.unbind(0)
  2950. params = gather_params(params, has_biases, False)
  2951. out, final_hiddens = _rnn_helper(
  2952. data,
  2953. hidden,
  2954. params,
  2955. has_biases,
  2956. num_layers,
  2957. dropout,
  2958. train,
  2959. bidirectional,
  2960. False,
  2961. partial(
  2962. one_layer_rnn_data,
  2963. batch_sizes=batch_sizes,
  2964. hidden_fn=rnn_cell_data(torch.relu),
  2965. ),
  2966. )
  2967. return out, torch.stack(final_hiddens, 0)
  2968. @register_decomposition(aten.rnn_tanh.data)
  2969. @aten.rnn_tanh.data.py_impl(DispatchKey.CompositeImplicitAutograd)
  2970. @aten.rnn_tanh.data.py_impl(DispatchKey.Autograd)
  2971. def rnn_tanh_data(
  2972. data,
  2973. batch_sizes,
  2974. hx,
  2975. params,
  2976. has_biases,
  2977. num_layers,
  2978. dropout,
  2979. train,
  2980. bidirectional,
  2981. ):
  2982. hidden = hx.unbind(0)
  2983. params = gather_params(params, has_biases, False)
  2984. out, final_hiddens = _rnn_helper(
  2985. data,
  2986. hidden,
  2987. params,
  2988. has_biases,
  2989. num_layers,
  2990. dropout,
  2991. train,
  2992. bidirectional,
  2993. False,
  2994. partial(
  2995. one_layer_rnn_data,
  2996. batch_sizes=batch_sizes,
  2997. hidden_fn=rnn_cell_data(torch.tanh),
  2998. ),
  2999. )
  3000. return out, torch.stack(final_hiddens, 0)
  3001. def lstm_cell(inp, hx, cx, hh_weight, hh_bias, hr_weight, chunk_dim):
  3002. gates = F.linear(hx, hh_weight, hh_bias) + inp
  3003. chunked_gates = gates.chunk(4, chunk_dim)
  3004. in_gate = chunked_gates[0].sigmoid()
  3005. forget_gate = chunked_gates[1].sigmoid()
  3006. cell_gate = chunked_gates[2].tanh()
  3007. out_gate = chunked_gates[3].sigmoid()
  3008. cy = forget_gate * cx + (in_gate * cell_gate)
  3009. hy = out_gate * cy.tanh()
  3010. hy = hy if hr_weight is None else F.linear(hy, hr_weight, None)
  3011. return hy, cy
  3012. def one_layer_lstm(inp, hidden, params, has_biases, reverse=False):
  3013. ih_weight = params[0]
  3014. hh_weight = params[1]
  3015. ih_bias = params[2] if has_biases else None
  3016. hh_bias = params[3] if has_biases else None
  3017. hr_weight = (
  3018. params[4] if len(params) == 5 else params[2] if len(params) == 3 else None
  3019. )
  3020. hx = hidden[0].unsqueeze(0)
  3021. cx = hidden[1].unsqueeze(0)
  3022. precomputed_input = F.linear(inp, ih_weight, ih_bias)
  3023. precomputed_input = precomputed_input.flip(0) if reverse else precomputed_input
  3024. step_output = []
  3025. for inp in precomputed_input:
  3026. hx, cx = lstm_cell(inp, hx, cx, hh_weight, hh_bias, hr_weight, chunk_dim=2)
  3027. step_output.append(hx)
  3028. if reverse:
  3029. step_output.reverse()
  3030. out = torch.cat(step_output, 0)
  3031. return out, (hx.squeeze(1), cx.squeeze(1))
  3032. def one_layer_lstm_data(inp, hidden, params, has_biases, batch_sizes, reverse=False):
  3033. ih_weight = params[0]
  3034. hh_weight = params[1]
  3035. ih_bias = params[2] if has_biases else None
  3036. hh_bias = params[3] if has_biases else None
  3037. hr_weight = (
  3038. params[4] if len(params) == 5 else params[2] if len(params) == 3 else None
  3039. )
  3040. step_output = []
  3041. hiddens = []
  3042. last_batch_size = batch_sizes[-1] if reverse else batch_sizes[0]
  3043. split_inp = torch.split(inp, list(batch_sizes))
  3044. if reverse:
  3045. split_inp = split_inp[::-1]
  3046. orig_hx = hidden[0]
  3047. orig_cx = hidden[1]
  3048. hx, cx = (
  3049. orig_hx.narrow(0, 0, last_batch_size),
  3050. orig_cx.narrow(0, 0, last_batch_size),
  3051. )
  3052. for inp in split_inp:
  3053. i = inp.shape[0]
  3054. inp = F.linear(inp, ih_weight, ih_bias)
  3055. # this will only happen when reverse=False, since batch sizes are sorted largest -> smallest
  3056. if i < last_batch_size:
  3057. hiddens.append(
  3058. (
  3059. hx.narrow(0, i, last_batch_size - i),
  3060. cx.narrow(0, i, last_batch_size - i),
  3061. )
  3062. )
  3063. hx, cx = hx.narrow(0, 0, i), cx.narrow(0, 0, i)
  3064. # this will only happen when reverse=True
  3065. if i > last_batch_size:
  3066. hx = torch.concat(
  3067. (hx, orig_hx.narrow(0, last_batch_size, i - last_batch_size)), 0
  3068. )
  3069. cx = torch.concat(
  3070. (cx, orig_cx.narrow(0, last_batch_size, i - last_batch_size)), 0
  3071. )
  3072. hx, cx = lstm_cell(inp, hx, cx, hh_weight, hh_bias, hr_weight, chunk_dim=1)
  3073. last_batch_size = i
  3074. step_output.append(hx)
  3075. if reverse:
  3076. step_output.reverse()
  3077. hidden_out = (hx, cx)
  3078. else:
  3079. hiddens.append((hx, cx))
  3080. hiddens.reverse()
  3081. hidden0, hidden1 = zip(*hiddens)
  3082. hidden_out = torch.cat(hidden0, 0), torch.cat(hidden1, 0)
  3083. out = torch.cat(step_output, 0)
  3084. return out, hidden_out
  3085. def select_one_layer_lstm_function(input, hx, params):
  3086. r"""Check whether we could use decompose lstm with mkldnn_rnn_layer.
  3087. All the below conditions need to be met:
  3088. * ``torch._C._get_mkldnn_enabled()`` returns ``True``.
  3089. * All the input args are on CPU.
  3090. * The dtypes of args are either torch.float or torch.bfloat16.
  3091. * Inference.
  3092. * ``has_projections`` returns ``False``.
  3093. Args:
  3094. * input: the input sequence to LSTM
  3095. * hx: a tuple of the input hidden state and cell state ``(h_0, c_0)`` to LSTM
  3096. * params: the weight and bias tensors of LSTM
  3097. """
  3098. def use_mkldnn(input, hx, params):
  3099. if not torch._C._get_mkldnn_enabled():
  3100. return False
  3101. tensors = [input] + list(hx) + list(chain.from_iterable(params))
  3102. devices = {t.device for t in tensors}
  3103. if len(devices) != 1:
  3104. return False
  3105. device = devices.pop()
  3106. if device != torch.device("cpu"):
  3107. return False
  3108. # With autocast, possible to have mixed dtype here
  3109. dtypes = {t.dtype for t in tensors}
  3110. for dtype in dtypes:
  3111. if dtype not in [torch.float, torch.bfloat16]:
  3112. return False
  3113. if input.requires_grad:
  3114. return False
  3115. has_projections = hx[0].size(2) != hx[1].size(2)
  3116. if has_projections:
  3117. return False
  3118. return True
  3119. # mkldnn_one_layer_lstm does not depend on seq_len while one_layer_lstm
  3120. # will expand over the seq_len dim
  3121. if use_mkldnn(input, hx, params):
  3122. return mkldnn_one_layer_lstm
  3123. else:
  3124. return one_layer_lstm
  3125. @register_decomposition(aten.lstm.input)
  3126. @aten.lstm.input.py_impl(DispatchKey.CompositeImplicitAutograd)
  3127. @aten.lstm.input.py_impl(DispatchKey.Autograd)
  3128. def lstm_impl(
  3129. input,
  3130. hx,
  3131. params,
  3132. has_biases,
  3133. num_layers,
  3134. dropout,
  3135. train,
  3136. bidirectional,
  3137. batch_first,
  3138. ):
  3139. assert len(hx) == 2, "lstm expects two hidden states"
  3140. params = gather_params(params, has_biases, hx[0].size(2) != hx[1].size(2))
  3141. hidden = list(zip(hx[0], hx[1]))
  3142. layer_fn = select_one_layer_lstm_function(input, hx, params)
  3143. out, final_hiddens = _rnn_helper(
  3144. input,
  3145. hidden,
  3146. params,
  3147. has_biases,
  3148. num_layers,
  3149. dropout,
  3150. train,
  3151. bidirectional,
  3152. batch_first,
  3153. layer_fn,
  3154. )
  3155. final_hiddens = list(zip(*final_hiddens))
  3156. return out, torch.stack(final_hiddens[0], 0), torch.stack(final_hiddens[1], 0)
  3157. @register_decomposition(aten.lstm.data)
  3158. @aten.lstm.data.py_impl(DispatchKey.CompositeImplicitAutograd)
  3159. @aten.lstm.data.py_impl(DispatchKey.Autograd)
  3160. def lstm_data_impl(
  3161. data,
  3162. batch_sizes,
  3163. hx,
  3164. params,
  3165. has_biases,
  3166. num_layers,
  3167. dropout,
  3168. train,
  3169. bidirectional,
  3170. ):
  3171. assert len(hx) == 2, "lstm expects two hidden states"
  3172. params = gather_params(params, has_biases, hx[0].size(2) != hx[1].size(2))
  3173. hidden = list(zip(hx[0], hx[1]))
  3174. out, final_hiddens = _rnn_helper(
  3175. data,
  3176. hidden,
  3177. params,
  3178. has_biases,
  3179. num_layers,
  3180. dropout,
  3181. train,
  3182. bidirectional,
  3183. False,
  3184. partial(one_layer_lstm_data, batch_sizes=batch_sizes),
  3185. )
  3186. final_hiddens = list(zip(*final_hiddens))
  3187. return out, torch.stack(final_hiddens[0], 0), torch.stack(final_hiddens[1], 0)
  3188. def gru_cell(inp, cur_hidden, ih_weight, ih_bias, hh_weight, hh_bias):
  3189. chunked_igates = inp.chunk(3, 1)
  3190. chunked_hgates = F.linear(cur_hidden, hh_weight, hh_bias).chunk(3, 2)
  3191. reset_gate = (chunked_hgates[0] + chunked_igates[0]).sigmoid()
  3192. input_gate = (chunked_hgates[1] + chunked_igates[1]).sigmoid()
  3193. new_gate = (chunked_igates[2] + (chunked_hgates[2] * reset_gate)).tanh()
  3194. return (cur_hidden - new_gate) * input_gate + new_gate
  3195. def gru_cell_data(inp, cur_hidden, ih_weight, ih_bias, hh_weight, hh_bias):
  3196. chunked_igates = F.linear(inp, ih_weight, ih_bias).chunk(3, 1)
  3197. chunked_hgates = F.linear(cur_hidden, hh_weight, hh_bias).chunk(3, 1)
  3198. reset_gate = (chunked_hgates[0] + chunked_igates[0]).sigmoid()
  3199. input_gate = (chunked_hgates[1] + chunked_igates[1]).sigmoid()
  3200. new_gate = (chunked_igates[2] + (chunked_hgates[2] * reset_gate)).tanh()
  3201. return (cur_hidden - new_gate) * input_gate + new_gate
  3202. @register_decomposition(aten.gru.data)
  3203. @aten.gru.data.py_impl(DispatchKey.CompositeImplicitAutograd)
  3204. @aten.gru.data.py_impl(DispatchKey.Autograd)
  3205. def gru_impl_data(
  3206. data,
  3207. batch_sizes,
  3208. hx,
  3209. params,
  3210. has_biases,
  3211. num_layers,
  3212. dropout,
  3213. train,
  3214. bidirectional,
  3215. ):
  3216. params = gather_params(params, has_biases, False)
  3217. out, final_hiddens = _rnn_helper(
  3218. data,
  3219. hx.unbind(0),
  3220. params,
  3221. has_biases,
  3222. num_layers,
  3223. dropout,
  3224. train,
  3225. bidirectional,
  3226. False,
  3227. partial(one_layer_rnn_data, batch_sizes=batch_sizes, hidden_fn=gru_cell_data),
  3228. )
  3229. return out, torch.stack(final_hiddens, 0)
  3230. @register_decomposition(aten.gru.input)
  3231. @aten.gru.input.py_impl(DispatchKey.CompositeImplicitAutograd)
  3232. @aten.gru.input.py_impl(DispatchKey.Autograd)
  3233. def gru_impl(
  3234. input,
  3235. hx,
  3236. params,
  3237. has_biases,
  3238. num_layers,
  3239. dropout,
  3240. train,
  3241. bidirectional,
  3242. batch_first,
  3243. ):
  3244. params = gather_params(params, has_biases, False)
  3245. out, final_hiddens = _rnn_helper(
  3246. input,
  3247. hx.unbind(0),
  3248. params,
  3249. has_biases,
  3250. num_layers,
  3251. dropout,
  3252. train,
  3253. bidirectional,
  3254. batch_first,
  3255. partial(one_layer_rnn, hidden_fn=gru_cell),
  3256. )
  3257. return out, torch.stack(final_hiddens, 0)
  3258. @register_decomposition(aten._upsample_bilinear2d_aa.vec)
  3259. @aten._upsample_bilinear2d_aa.vec.py_impl(DispatchKey.CompositeImplicitAutograd)
  3260. @aten._upsample_bilinear2d_aa.vec.py_impl(DispatchKey.Autograd)
  3261. def upsample_bilinear2d_aa_vec(input, output_size, align_corners, scale_factors):
  3262. osize = upsample_compute_output_size(input.size(), output_size, scale_factors)
  3263. scale_h = get_scale_value(scale_factors, 0)
  3264. scale_w = get_scale_value(scale_factors, 1)
  3265. return torch.ops.aten._upsample_bilinear2d_aa(
  3266. input, osize, align_corners, scale_h, scale_w
  3267. )
  3268. @register_decomposition(aten._upsample_bicubic2d_aa.vec)
  3269. @aten._upsample_bicubic2d_aa.vec.py_impl(DispatchKey.CompositeImplicitAutograd)
  3270. @aten._upsample_bicubic2d_aa.vec.py_impl(DispatchKey.Autograd)
  3271. def upsample_bicubic2d_aa_vec(input, output_size, align_corners, scale_factors):
  3272. osize = upsample_compute_output_size(input.size(), output_size, scale_factors)
  3273. scale_h = get_scale_value(scale_factors, 0)
  3274. scale_w = get_scale_value(scale_factors, 1)
  3275. return torch.ops.aten._upsample_bicubic2d_aa(
  3276. input, osize, align_corners, scale_h, scale_w
  3277. )
  3278. @register_decomposition(aten.upsample_bilinear2d.vec)
  3279. @register_decomposition(aten.upsample_trilinear3d.vec)
  3280. @aten.upsample_linear1d.vec.py_impl(DispatchKey.CompositeImplicitAutograd)
  3281. @aten.upsample_linear1d.vec.py_impl(DispatchKey.Autograd)
  3282. @aten.upsample_bilinear2d.vec.py_impl(DispatchKey.CompositeImplicitAutograd)
  3283. @aten.upsample_bilinear2d.vec.py_impl(DispatchKey.Autograd)
  3284. @aten.upsample_trilinear3d.vec.py_impl(DispatchKey.CompositeImplicitAutograd)
  3285. @aten.upsample_trilinear3d.vec.py_impl(DispatchKey.Autograd)
  3286. def _upsample_linear_vec(input, output_size, align_corners, scale_factors):
  3287. osize = upsample_compute_output_size(input.size(), output_size, scale_factors)
  3288. scales = scale_factors if scale_factors else [None] * len(osize)
  3289. return _upsample_linear(input, osize, align_corners, scales)
  3290. @register_decomposition([aten.upsample_linear1d.default, aten.upsample_linear1d.out])
  3291. @out_wrapper()
  3292. def upsample_linear1d(
  3293. input: Tensor,
  3294. output_size: list[int],
  3295. align_corners: bool,
  3296. scales_w: Optional[float] = None,
  3297. ) -> Tensor:
  3298. return _upsample_linear(input, output_size, align_corners, [scales_w])
  3299. @register_decomposition(
  3300. [aten.upsample_bilinear2d.default, aten.upsample_bilinear2d.out]
  3301. )
  3302. @aten.upsample_bilinear2d.default.py_impl(DispatchKey.Autograd)
  3303. @out_wrapper()
  3304. def upsample_bilinear2d(
  3305. input: Tensor,
  3306. output_size: list[int],
  3307. align_corners: bool,
  3308. scales_h: Optional[float] = None,
  3309. scales_w: Optional[float] = None,
  3310. ) -> Tensor:
  3311. return _upsample_linear(input, output_size, align_corners, [scales_h, scales_w])
  3312. @register_decomposition(
  3313. [aten.upsample_trilinear3d.default, aten.upsample_trilinear3d.out]
  3314. )
  3315. @out_wrapper()
  3316. def upsample_trilinear3d(
  3317. input: Tensor,
  3318. output_size: list[int],
  3319. align_corners: bool,
  3320. scales_d: Optional[float] = None,
  3321. scales_h: Optional[float] = None,
  3322. scales_w: Optional[float] = None,
  3323. ) -> Tensor:
  3324. return _upsample_linear(
  3325. input, output_size, align_corners, [scales_d, scales_h, scales_w]
  3326. )
  3327. def _compute_scale(in_size, out_size, align_corners, scale=None):
  3328. if align_corners:
  3329. return (in_size - 1.0) / (out_size - 1.0) if out_size > 1 else 0
  3330. else:
  3331. return 1.0 / scale if scale is not None and scale > 0 else in_size / out_size
  3332. def _compute_source_index(scale, dst_index, align_corners):
  3333. if align_corners:
  3334. return scale * dst_index
  3335. else:
  3336. return scale * (dst_index + 0.5) - 0.5
  3337. def _sum_tensors_uint8(
  3338. src: Iterable[Tensor], weights: Iterable[Tensor], weights_precision: Tensor
  3339. ) -> Tensor:
  3340. output = _sum_tensors(
  3341. s.to(torch.int32) * c.to(torch.int32) for s, c in zip(src, weights)
  3342. ) + (1 << (weights_precision - 1))
  3343. output = output >> weights_precision
  3344. return torch.clamp(output, 0, 255).to(torch.uint8)
  3345. def _compute_weight_precision(weights: TensorSequenceType) -> Tensor:
  3346. max_weight = torch.stack(weights).max()
  3347. max_weight_precision = 22
  3348. precisions = torch.arange(max_weight_precision, device=max_weight.device)
  3349. values = 0.5 + max_weight * (1 << (precisions + 1))
  3350. mask = values >= (1 << 15)
  3351. return max_weight_precision - mask.sum()
  3352. @pw_cast_for_opmath
  3353. def _upsample_linear(
  3354. input: Tensor,
  3355. output_size: list[int],
  3356. align_corners: bool,
  3357. scales: list[Optional[float]],
  3358. ) -> Tensor:
  3359. # get dimensions of original image
  3360. n_channels = input.shape[1]
  3361. inp_sizes = input.shape[2:]
  3362. n_dims = len(inp_sizes)
  3363. _, dtype = utils.elementwise_dtypes(
  3364. input,
  3365. type_promotion_kind=utils.ELEMENTWISE_TYPE_PROMOTION_KIND.INT_TO_FLOAT,
  3366. )
  3367. def get_values(inp_size, out_size, scales, nsqueeze):
  3368. # First Calculate scaling factor
  3369. scale_factor = _compute_scale(inp_size, out_size, align_corners, scales)
  3370. # We have to create arange with int64 dtype and use .to in order to avoid
  3371. # additional kernels creation in inductor and get a perf slowdown
  3372. i = torch.arange(out_size, device=input.device).to(dtype=dtype)
  3373. x_f32 = _compute_source_index(scale_factor, i, align_corners).clamp(min=0.0)
  3374. x_f32 = x_f32.reshape(x_f32.shape[0], *[1] * (nsqueeze))
  3375. x = x_f32.to(torch.int64)
  3376. xp1 = (x + 1).clamp(max=inp_size - 1)
  3377. return x_f32, x, xp1
  3378. values = [
  3379. get_values(inp_size, out_size, scales, n_dims - 1 - i)
  3380. for i, (inp_size, out_size, scales) in enumerate(
  3381. zip(inp_sizes, output_size, scales)
  3382. )
  3383. ]
  3384. xs_f32, xs, xp1s = list(zip(*values))
  3385. vs = []
  3386. for a in product(*[[0, 1]] * n_dims):
  3387. idx = [None, None] + [xs[k] if a[k] == 0 else xp1s[k] for k in range(n_dims)]
  3388. v = aten._unsafe_index(input, idx)
  3389. v = _maybe_convert_to_dtype(v, dtype)
  3390. vs.append(v)
  3391. for i in reversed(range(n_dims)):
  3392. xscale = (xs_f32[i] - xs[i]).clamp(0.0, 1.0).to(dtype)
  3393. vs = [
  3394. # x1 * (1 - alpha) + x2 * alpha == x1 + (x2 - x1) * alpha
  3395. v1 + torch.mul(v2 - v1, xscale)
  3396. for v1, v2 in zip(vs[::2], vs[1::2])
  3397. ]
  3398. assert len(vs) == 1
  3399. result = vs[0]
  3400. # convert output to correct memory format, if necessary
  3401. memory_format = utils.suggest_memory_format(input)
  3402. # following "heuristic: only use channels_last path when it's faster than the contiguous path"
  3403. if input.device.type == "cuda" and n_channels < 16:
  3404. memory_format = torch.contiguous_format
  3405. assert isinstance(result, torch.Tensor)
  3406. result = result.contiguous(memory_format=memory_format)
  3407. if not input.is_floating_point():
  3408. result = result.round()
  3409. return result
  3410. # We should be applying decompositions after all transformations
  3411. @register_decomposition(aten.is_same_size.default)
  3412. def is_same_size(a: Tensor, b: Tensor) -> bool:
  3413. return a.shape == b.shape
  3414. @register_decomposition([aten._reshape_alias, aten._unsafe_view])
  3415. @out_wrapper()
  3416. def _reshape_alias(x, shape, *args):
  3417. return aten.view(x, shape)
  3418. @register_decomposition([aten._unsafe_index])
  3419. def _unsafe_index(x, indices):
  3420. return aten.index(x, indices)
  3421. @register_decomposition([aten._unsafe_index_put])
  3422. def _unsafe_index_put(x, indices, value, accumulate=False):
  3423. return aten.index_put(x, indices, value, accumulate)
  3424. @register_decomposition([aten._unsafe_masked_index])
  3425. def _unsafe_masked_index(x, mask, indices, fill):
  3426. for index in indices:
  3427. if index is not None:
  3428. torch._check(
  3429. index.dtype in [torch.long, torch.int],
  3430. lambda: "tensors used as indices must be long or int tensors",
  3431. )
  3432. torch._check(
  3433. mask.dtype == torch.bool,
  3434. lambda: "tensors used as masks must be bool tensors",
  3435. )
  3436. from torch.fx.experimental.symbolic_shapes import guard_or_false
  3437. if guard_or_false(x.numel() == 0):
  3438. meta_result = torch._meta_registrations.meta_index_Tensor(x, indices)
  3439. return x.new_full(meta_result.shape, fill)
  3440. for i in range(len(indices)):
  3441. index = indices[i]
  3442. if index is not None:
  3443. indices[i] = index.clamp(min=0, max=x.size(i) - 1)
  3444. return aten._unsafe_index(x, indices).masked_fill(~mask, fill)
  3445. @register_decomposition([aten._unsafe_masked_index_put_accumulate])
  3446. def _unsafe_masked_index_put_accumulate(x, mask, indices, values):
  3447. for index in indices:
  3448. if index is not None:
  3449. torch._check(
  3450. index.dtype in [torch.long, torch.int],
  3451. lambda: "tensors used as indices must be long or int tensors",
  3452. )
  3453. torch._check(
  3454. mask.dtype == torch.bool,
  3455. lambda: "tensors used as masks must be bool tensors",
  3456. )
  3457. if x.numel() == 0:
  3458. return x.clone()
  3459. for i in range(len(indices)):
  3460. index = indices[i]
  3461. if index is not None:
  3462. indices[i] = index.clamp(min=-x.size(i), max=x.size(i) - 1)
  3463. masked_value = values.masked_fill(~mask, 0)
  3464. return aten._unsafe_index_put(x, indices, masked_value, accumulate=True)
  3465. def _nll_loss_forward(
  3466. self: Tensor,
  3467. target: Tensor,
  3468. weight: Optional[Tensor],
  3469. reduction: int,
  3470. ignore_index: int,
  3471. ) -> tuple[Tensor, Tensor]:
  3472. # self can be [N, C] or [C]
  3473. # target can be [N] or []
  3474. n_dims = self.dim()
  3475. channel_dim = 1
  3476. if n_dims < 2:
  3477. channel_dim = 0
  3478. if weight is not None:
  3479. if n_dims > 1:
  3480. shape = [
  3481. 1,
  3482. ] * n_dims
  3483. shape[channel_dim] = weight.shape[0]
  3484. w = weight.view(shape)
  3485. else:
  3486. w = weight
  3487. self = self * w
  3488. safe_target = torch.where(target != ignore_index, target, 0)
  3489. safe_target_ = safe_target.unsqueeze(channel_dim)
  3490. # target can be [N, 1] or [1]
  3491. result = -torch.gather(self, channel_dim, safe_target_).squeeze(channel_dim)
  3492. result = torch.where(target != ignore_index, result, 0)
  3493. if reduction == Reduction.NONE.value and n_dims > 1:
  3494. total_weight = self.new_full((), 0.0)
  3495. return result, total_weight
  3496. if weight is not None:
  3497. w = w.expand(self.shape)
  3498. wsum = torch.gather(w, channel_dim, safe_target_).squeeze(channel_dim)
  3499. wsum = torch.where(target != ignore_index, wsum, 0)
  3500. total_weight = wsum.sum()
  3501. else:
  3502. total_weight = (target != ignore_index).sum().to(self)
  3503. if reduction == Reduction.SUM.value:
  3504. result = result.sum()
  3505. elif reduction == Reduction.MEAN.value:
  3506. result = result.sum() / total_weight
  3507. return result, total_weight
  3508. @register_decomposition(aten.nll_loss_forward)
  3509. @out_wrapper("output", "total_weight")
  3510. def nll_loss_forward(
  3511. self: Tensor,
  3512. target: Tensor,
  3513. weight: Optional[Tensor],
  3514. reduction: int,
  3515. ignore_index: int,
  3516. ) -> tuple[Tensor, Tensor]:
  3517. assert self.dim() > 0 and self.dim() <= 2, "input tensor should be 1D or 2D"
  3518. assert target.dim() <= 1, (
  3519. "0D or 1D target tensor expected, multi-target not supported"
  3520. )
  3521. no_batch_dim = self.dim() == 1 and target.dim() == 0
  3522. assert no_batch_dim or (self.shape[0] == target.shape[0]), (
  3523. f"size mismatch (got input: {self.shape}, target: {target.shape})"
  3524. )
  3525. n_classes = self.shape[-1]
  3526. assert weight is None or (weight.dim() == 1 and weight.numel() == n_classes), (
  3527. f"weight tensor should be defined either for all {n_classes} classes or no classes "
  3528. f"but got weight tensor of shape: {weight.shape}"
  3529. )
  3530. return _nll_loss_forward(self, target, weight, reduction, ignore_index)
  3531. @register_decomposition(aten.nll_loss2d_forward)
  3532. @out_wrapper("output", "total_weight")
  3533. def nll_loss2d_forward(
  3534. self: Tensor,
  3535. target: Tensor,
  3536. weight: Optional[Tensor],
  3537. reduction: int,
  3538. ignore_index: int,
  3539. ) -> tuple[Tensor, Tensor]:
  3540. return _nll_loss_forward(self, target, weight, reduction, ignore_index)
  3541. # These are adapted from aten/src/ATen/native/UpSample.h, which is based on
  3542. # https://en.wikipedia.org/wiki/Bicubic_interpolation#Bicubic_convolution_algorithm
  3543. def _upsample_cubic_convolution1(x: Tensor, A: float) -> Tensor:
  3544. return ((A + 2) * x - (A + 3)) * x * x + 1
  3545. def _upsample_cubic_convolution2(x: Tensor, A: float) -> Tensor:
  3546. return ((A * x - 5 * A) * x + 8 * A) * x - 4 * A
  3547. def _upsample_get_cubic_coefficients(t: Tensor) -> TensorSequenceType:
  3548. A = -0.75
  3549. if t.device == torch.device("cpu"):
  3550. tt1 = torch.stack([t, 1.0 - t], dim=0)
  3551. tt2 = torch.stack([t + 1.0, 2.0 - t], dim=0)
  3552. w03 = _upsample_cubic_convolution2(tt2, A)
  3553. w12 = _upsample_cubic_convolution1(tt1, A)
  3554. w0, w3 = torch.unbind(w03, dim=0)
  3555. w1, w2 = torch.unbind(w12, dim=0)
  3556. return w0, w1, w2, w3
  3557. else:
  3558. return (
  3559. _upsample_cubic_convolution2(t + 1.0, A),
  3560. _upsample_cubic_convolution1(t, A),
  3561. _upsample_cubic_convolution1(1.0 - t, A),
  3562. _upsample_cubic_convolution2(2.0 - t, A),
  3563. )
  3564. def _upsample_cubic_interp1d(coeffs: TensorSequenceType, ts: Tensor) -> Tensor:
  3565. coeffs2 = _upsample_get_cubic_coefficients(ts)
  3566. return _sum_tensors(c1 * c2 for (c1, c2) in zip(coeffs, coeffs2))
  3567. # Need this instead of just sum() to keep mypy happy
  3568. def _sum_tensors(ts: Iterable[Tensor]) -> Tensor:
  3569. return reduce(torch.add, ts)
  3570. def _linspace_from_neg_one(
  3571. num_steps: int, align_corners: bool, dtype: torch.dtype, device: torch.device
  3572. ):
  3573. if num_steps <= 1:
  3574. return torch.tensor(0, device=device, dtype=dtype)
  3575. a = ((num_steps - 1) / num_steps) if not align_corners else 1
  3576. return torch.linspace(-a, a, steps=num_steps, device=device, dtype=dtype)
  3577. def _make_base_grid_4d(theta: Tensor, h: int, w: int, align_corners: bool):
  3578. dtype = theta.dtype
  3579. device = theta.device
  3580. # Using padding and summation generates a single kernel vs using torch.stack where 3 kernels generated
  3581. # corresponding to each individual tensor: grid_x, grid_y, grid_one
  3582. grid_x = _linspace_from_neg_one(w, align_corners, dtype, device).view(1, w, 1)
  3583. grid_y = _linspace_from_neg_one(h, align_corners, dtype, device).view(h, 1, 1)
  3584. grid_one = torch.ones((1, 1, 1), dtype=dtype, device=device)
  3585. # this is just a temporary hack and we should use torch.stack here once #104480 is merged
  3586. grid_x = torch.nn.functional.pad(grid_x, pad=(0, 2), mode="constant", value=0)
  3587. grid_y = torch.nn.functional.pad(grid_y, pad=(1, 1), mode="constant", value=0)
  3588. grid_one = torch.nn.functional.pad(grid_one, pad=(2, 0), mode="constant", value=0)
  3589. return grid_x + grid_y + grid_one
  3590. def _make_base_grid_5d(theta: Tensor, d: int, h: int, w: int, align_corners: bool):
  3591. dtype = theta.dtype
  3592. device = theta.device
  3593. grid_x = _linspace_from_neg_one(w, align_corners, dtype, device).view(1, 1, w, 1)
  3594. grid_y = _linspace_from_neg_one(h, align_corners, dtype, device).view(1, h, 1, 1)
  3595. grid_z = _linspace_from_neg_one(d, align_corners, dtype, device).view(d, 1, 1, 1)
  3596. grid_one = torch.ones((1, 1, 1, 1), dtype=dtype, device=device)
  3597. # this is just a temporary hack and we should use torch.stack here once #104480 is merged
  3598. grid_x = torch.nn.functional.pad(grid_x, pad=(0, 3), mode="constant", value=0)
  3599. grid_y = torch.nn.functional.pad(grid_y, pad=(1, 2), mode="constant", value=0)
  3600. grid_z = torch.nn.functional.pad(grid_z, pad=(2, 1), mode="constant", value=0)
  3601. grid_one = torch.nn.functional.pad(grid_one, pad=(3, 0), mode="constant", value=0)
  3602. return grid_x + grid_y + grid_z + grid_one
  3603. def _affine_grid_generator_4d(theta: Tensor, size: list[int], align_corners: bool):
  3604. n, _, h, w = size
  3605. base_grid = _make_base_grid_4d(theta, h, w, align_corners=align_corners)
  3606. # base_grid shape is (h, w, 3) and theta shape is (n, 2, 3)
  3607. # We do manually a matrix multiplication which is faster than mm()
  3608. # (h * w, 3, 1) * (n, 1, 3, 2) -> (n, h * w, 2)
  3609. grid = (base_grid.view(-1, 3, 1) * theta.mT.unsqueeze(1)).sum(-2)
  3610. return grid.view(n, h, w, 2)
  3611. def _affine_grid_generator_5d(theta: Tensor, size: list[int], align_corners: bool):
  3612. n, _, d, h, w = size
  3613. base_grid = _make_base_grid_5d(theta, d, h, w, align_corners=align_corners)
  3614. # base_grid shape is (d, h, w, 4) and theta shape is (n, 3, 4)
  3615. # We do manually a matrix multiplication which is faster than mm()
  3616. # (d * h * w, 4, 1) * (n, 1, 4, 3) -> (n, h * w, 3)
  3617. grid = (base_grid.view(-1, 4, 1) * theta.mT.unsqueeze(1)).sum(-2)
  3618. return grid.view(n, d, h, w, 3)
  3619. @register_decomposition(aten.affine_grid_generator)
  3620. @out_wrapper()
  3621. @pw_cast_for_opmath
  3622. def affine_grid_generator(theta: Tensor, size: list[int], align_corners: bool):
  3623. torch._check(
  3624. len(size) in (4, 5),
  3625. lambda: "affine_grid_generator needs 4d (spatial) or 5d (volumetric) inputs.",
  3626. )
  3627. if len(size) == 4:
  3628. return _affine_grid_generator_4d(theta, size, align_corners=align_corners)
  3629. else:
  3630. return _affine_grid_generator_5d(theta, size, align_corners=align_corners)
  3631. def _grid_sampler_2d(
  3632. a: Tensor,
  3633. grid: Tensor,
  3634. interpolation_mode: int = 0,
  3635. padding_mode: int = 0,
  3636. align_corners: bool = False,
  3637. _expand_grid: bool = True,
  3638. ) -> Tensor:
  3639. # This method is a copy of grid_sampler_2d implementation and introduced with additional arg _expand_grid to
  3640. # optionally expand the input grid for performance reasons.
  3641. # Experimenting locally it was found that compiled CUDA code is accelerated by ~5x
  3642. # and CPU code by ~2x on bicubic mode, if we expand the grid from (N, H, W, 2) into (N, C, H, W, 2)
  3643. # However, this leads to a slowdown around ~0.8x on CPU bilinear mode, channels first.
  3644. # Thus we apply this hack to not expand the grid for this case.
  3645. torch._check(
  3646. interpolation_mode in (0, 1, 2),
  3647. lambda: f"Invalid interpolation mode {interpolation_mode}",
  3648. )
  3649. torch._check(
  3650. padding_mode in (0, 1, 2), lambda: f"Invalid padding mode {padding_mode}"
  3651. )
  3652. def unnormalize(coords: Tensor, size: int) -> Tensor:
  3653. # Rescale coordinates from [-1, 1] to:
  3654. # [0, size - 1] if align_corners is True
  3655. # [-.5, size -.5] if align_corners is False
  3656. mul = (size * 0.5 - 0.5) if align_corners else (size * 0.5)
  3657. ofs = size * 0.5 - 0.5
  3658. return coords * mul + ofs
  3659. # Reflects coordinates until they fall between low and high (inclusive).
  3660. # The bounds are passed as twice their value so that half-integer values
  3661. # can be represented as ints.
  3662. def reflect_coordinates(coords: Tensor, twice_low: int, twice_high: int) -> Tensor:
  3663. if twice_low == twice_high:
  3664. return torch.zeros_like(coords)
  3665. coords_min = twice_low / 2
  3666. coords_span = (twice_high - twice_low) / 2
  3667. coords2 = (coords - coords_min).abs()
  3668. extra = torch.fmod(coords2, coords_span)
  3669. flips = (coords2 / coords_span).floor().to(dtype=torch.int8)
  3670. return torch.where(
  3671. flips & 1 == 0, extra + coords_min, coords_span + coords_min - extra
  3672. )
  3673. def compute_coordinates(coords: Tensor, size: int) -> Tensor:
  3674. if padding_mode == 0: # Zero
  3675. return coords
  3676. elif padding_mode == 1: # Borders
  3677. return torch.clamp(coords, 0, size - 1)
  3678. else: # padding_mode == 2, Reflection
  3679. if align_corners:
  3680. coords_reflected = reflect_coordinates(coords, 0, 2 * (size - 1))
  3681. else:
  3682. coords_reflected = reflect_coordinates(coords, -1, 2 * size - 1)
  3683. return torch.clamp(coords_reflected, 0, size - 1)
  3684. def compute_source_index(coords: Tensor, size: int) -> Tensor:
  3685. coords_un = unnormalize(coords, size)
  3686. return compute_coordinates(coords_un, size)
  3687. N, C, iH, iW = a.shape
  3688. _, oH, oW, two = grid.shape
  3689. assert two == 2
  3690. if _expand_grid:
  3691. # Let's expand grid to [N, C, oH, oW, 2]
  3692. # This allows to generate a single triton cuda kernel instead of two kernels.
  3693. # Two kernels are due source indices, weights have shape (N, 1, oH, oW), xnumel=N*oH*oW
  3694. # and output has shape (N, C, oH, oW), xnumel=N*C*oH*oW
  3695. # Expanding grid to (N, C, oH, oW, two) unifies xnumel to N*C*oH*oW
  3696. grid = grid.view(N, 1, oH, oW, two).expand(N, C, oH, oW, 2)
  3697. def in_bounds_cond(xs: Tensor, ys: Tensor) -> Tensor:
  3698. return torch.logical_and(
  3699. 0 <= xs, torch.logical_and(xs < iW, torch.logical_and(0 <= ys, ys < iH))
  3700. )
  3701. N_idx = torch.arange(N, device=a.device).view(N, 1, 1, 1)
  3702. C_idx = torch.arange(C, device=a.device).view(1, C, 1, 1)
  3703. def clip(xs: Tensor, ys: Tensor, ws: Tensor) -> TensorSequenceType:
  3704. cond = in_bounds_cond(xs, ys)
  3705. # To clip to inside valid coordinates, we map the coordinates
  3706. # to (x, y) = (0, 0) and also set the weight to 0
  3707. # We also change the shape of the tensor to the appropriate one for
  3708. # broadcasting with N_idx, C_idx for the purposes of advanced indexing
  3709. c = C if _expand_grid else 1
  3710. return tuple(
  3711. torch.where(cond, t, 0).view(N, c, oH, oW)
  3712. for t in (xs.to(dtype=torch.int64), ys.to(dtype=torch.int64), ws)
  3713. )
  3714. def get_summand(ix: Tensor, iy: Tensor, w) -> Tensor:
  3715. # Perform clipping, index into input tensor and multiply by weight
  3716. idx_x, idx_y, w_ = clip(ix, iy, w)
  3717. return a[N_idx, C_idx, idx_y, idx_x] * w_
  3718. x = grid[..., 0]
  3719. y = grid[..., 1]
  3720. if interpolation_mode == 0: # Bilinear
  3721. ix = compute_source_index(x, iW)
  3722. iy = compute_source_index(y, iH)
  3723. ix_nw, iy_nw = ix.floor(), iy.floor()
  3724. ix_ne, iy_ne = ix_nw + 1, iy_nw
  3725. ix_sw, iy_sw = ix_nw, iy_nw + 1
  3726. ix_se, iy_se = ix_ne, iy_sw
  3727. w_nw = (ix_se - ix) * (iy_se - iy)
  3728. w_ne = (ix - ix_sw) * (iy_sw - iy)
  3729. w_sw = (ix_ne - ix) * (iy - iy_ne)
  3730. w_se = (ix - ix_nw) * (iy - iy_nw)
  3731. return _sum_tensors(
  3732. get_summand(ix, iy, w)
  3733. for (ix, iy, w) in (
  3734. (ix_nw, iy_nw, w_nw),
  3735. (ix_ne, iy_ne, w_ne),
  3736. (ix_sw, iy_sw, w_sw),
  3737. (ix_se, iy_se, w_se),
  3738. )
  3739. )
  3740. elif interpolation_mode == 1: # Nearest
  3741. ix = compute_source_index(x, iW)
  3742. iy = compute_source_index(y, iH)
  3743. ix_nearest = ix.round()
  3744. iy_nearest = iy.round()
  3745. return get_summand(ix_nearest, iy_nearest, 1)
  3746. else: # interpolation_mode == 2, Bicubic
  3747. ix = unnormalize(x, iW)
  3748. iy = unnormalize(y, iH)
  3749. ix_nw = ix.floor()
  3750. iy_nw = iy.floor()
  3751. tx = ix - ix_nw
  3752. ty = iy - iy_nw
  3753. if not _expand_grid:
  3754. tx = tx.unsqueeze(1)
  3755. ty = ty.unsqueeze(1)
  3756. def get_value_bounded(ix: Tensor, iy: Tensor) -> Tensor:
  3757. x = compute_coordinates(ix, iW)
  3758. y = compute_coordinates(iy, iH)
  3759. return get_summand(x, y, 1)
  3760. def get_coeff(ofs: int) -> Tensor:
  3761. iy_ofs = iy_nw + (ofs - 1)
  3762. cs = (
  3763. get_value_bounded(ix_nw - 1, iy_ofs),
  3764. get_value_bounded(ix_nw, iy_ofs),
  3765. get_value_bounded(ix_nw + 1, iy_ofs),
  3766. get_value_bounded(ix_nw + 2, iy_ofs),
  3767. )
  3768. return _upsample_cubic_interp1d(cs, tx)
  3769. coeffs = tuple(get_coeff(ofs) for ofs in range(4))
  3770. return _upsample_cubic_interp1d(coeffs, ty)
  3771. @register_decomposition(aten.grid_sampler_2d)
  3772. @out_wrapper()
  3773. @pw_cast_for_opmath
  3774. def grid_sampler_2d(
  3775. a: Tensor,
  3776. grid: Tensor,
  3777. interpolation_mode: int = 0,
  3778. padding_mode: int = 0,
  3779. align_corners: bool = False,
  3780. ) -> Tensor:
  3781. return _grid_sampler_2d(
  3782. a,
  3783. grid=grid,
  3784. interpolation_mode=interpolation_mode,
  3785. padding_mode=padding_mode,
  3786. align_corners=align_corners,
  3787. )
  3788. @register_decomposition(aten.mv)
  3789. @out_wrapper(exact_dtype=True)
  3790. @pw_cast_for_opmath
  3791. def mv(self, vec):
  3792. torch._check(
  3793. self.dim() == 2 and vec.dim() == 1,
  3794. lambda: f"matrix @ vector expected, got {self.dim()}, {vec.dim()}",
  3795. )
  3796. torch._check(
  3797. self.size(1) == vec.size(0),
  3798. lambda: f"size mismatch, got input ({self.size(0)}x{self.size(1)}), vec ({vec.size(0)})",
  3799. )
  3800. return (self * vec).sum(dim=1)
  3801. @register_decomposition(aten.binary_cross_entropy_with_logits)
  3802. @out_wrapper()
  3803. def binary_cross_entropy_with_logits(
  3804. self, target, weight=None, pos_weight=None, reduction=Reduction.MEAN.value
  3805. ):
  3806. if pos_weight is not None:
  3807. log_weight = (pos_weight - 1) * target + 1
  3808. loss = (1 - target) * self - (log_weight * F.logsigmoid(self))
  3809. else:
  3810. loss = (1 - target) * self - F.logsigmoid(self)
  3811. if weight is not None:
  3812. loss = loss * weight
  3813. return apply_loss_reduction(loss, reduction)
  3814. def should_fold(tensor1: torch.Tensor, tensor2: torch.Tensor, is_out: bool) -> bool:
  3815. # For comments of the logic of this function see eager in /native/LinearAlgebra.cpp
  3816. t1, t2 = (tensor1, tensor2) if tensor1.ndim >= tensor2.ndim else (tensor2, tensor1)
  3817. from torch.fx.experimental.symbolic_shapes import guard_or_false
  3818. if not (t1.ndim >= 3 and t2.ndim <= 2):
  3819. return False
  3820. if t2.requires_grad and not is_out:
  3821. return True
  3822. if tensor1.ndim == 2:
  3823. return False
  3824. if guard_or_false(t1.numel() == 0):
  3825. return True
  3826. t1_shape = t1.shape
  3827. t1_stride = t1.stride()
  3828. # Check the contiguous, we can skip the dim with size of 1
  3829. # as aten: https://github.com/pytorch/pytorch/blob/e201460f8aa1510b4c4686627d57b69756c4b916/aten/src/ATen/TensorGeometry.cpp#L17
  3830. expected_stride = [1]
  3831. for size in reversed(t1_shape[1:]):
  3832. expected_stride.append(size * expected_stride[-1])
  3833. return all(
  3834. guard_or_false(size == 1) or guard_or_false(left == right)
  3835. for left, right, size in zip(
  3836. t1_stride, list(reversed(expected_stride)), t1_shape
  3837. )
  3838. )
  3839. @aten.matmul.default.py_impl(DispatchKey.CompositeImplicitAutograd)
  3840. @aten.matmul.out.py_impl(DispatchKey.CompositeImplicitAutograd)
  3841. @out_wrapper(pass_is_out=True)
  3842. def matmul(tensor1, tensor2, *, is_out=False):
  3843. dim_tensor1 = tensor1.dim()
  3844. dim_tensor2 = tensor2.dim()
  3845. assert dim_tensor1 != 0 and dim_tensor2 != 0
  3846. if dim_tensor1 == 1 and dim_tensor2 == 1:
  3847. return torch.dot(tensor1, tensor2)
  3848. elif dim_tensor1 == 2 and dim_tensor2 == 1:
  3849. return torch.mv(tensor1, tensor2)
  3850. elif dim_tensor1 == 1 and dim_tensor2 == 2:
  3851. return torch.squeeze(torch.mm(torch.unsqueeze(tensor1, 0), tensor2), 0)
  3852. elif dim_tensor1 == 2 and dim_tensor2 == 2:
  3853. return torch.mm(tensor1, tensor2)
  3854. elif should_fold(tensor1, tensor2, is_out):
  3855. # dim_tensor1 >=3 && (dim_tensor2 == 1 || dim_tensor2 == 2) ||
  3856. # dim_tensor2 >=3 && (dim_tensor1 == 1 || dim_tensor1 == 2)
  3857. # and some condition on the strides is fulfilled
  3858. # optimization: use mm instead of bmm by folding the batch of the larger tensor
  3859. # into its leading matrix dimension
  3860. transpose = dim_tensor2 > dim_tensor1
  3861. t1 = tensor2.mT if transpose else tensor1
  3862. t2 = (
  3863. tensor2 if not transpose else (tensor1.t() if dim_tensor1 == 2 else tensor1)
  3864. )
  3865. # Invariant: t1.dim() >= 3 && (t2.dim() == 1 || t2.dim() == 2)
  3866. # and t1 and t2 are matmul-compatible
  3867. # Why not t1.view(-1, sizes_1[-1])?
  3868. # If the last dim is 0, then view(-1, 0) won't work because the -1 becomes ambiguous.
  3869. # This can happen in e.g. [3, 5, 0] @ [0, 0].
  3870. sizes_1 = t1.shape
  3871. output_shape = list(sizes_1[:-1])
  3872. folded_dim1 = reduce(operator.mul, output_shape)
  3873. # Readjust output_shape if we are multiplying by a matrix
  3874. t2_is_matrix = t2.dim() == 2
  3875. if t2_is_matrix:
  3876. output_shape.append(t2.shape[1])
  3877. # This will almost always be a view.
  3878. # It may not be a view if t2->requires_grad(). See should_fold in aten/ for an explanation
  3879. t1_folded = t1.reshape(folded_dim1, sizes_1[-1])
  3880. if t2_is_matrix:
  3881. # This copies if we perform a 2D @ 3D and the first tensor requires_grad
  3882. # See should_fold native/LinearAlgebra.cpp for why.
  3883. output = torch.ops.aten._unsafe_view(t1_folded.mm(t2), output_shape)
  3884. return output.mT.contiguous() if transpose else output
  3885. else:
  3886. return torch.ops.aten._unsafe_view(t1_folded.mv(t2), output_shape)
  3887. elif dim_tensor1 >= 1 and dim_tensor2 >= 1:
  3888. # We are multiplying b1 x n x m1 by x2 x m2 x p (where b1 can be a list);
  3889. # we track m1 vs m2 separately even though they must match for nicer error messages
  3890. n = tensor1.size(-2) if dim_tensor1 > 1 else 1
  3891. m1 = tensor1.size(-1)
  3892. batch_tensor1 = tensor1.shape[:-2]
  3893. m2 = tensor2.size(-2) if dim_tensor2 > 1 else tensor2.size(-1)
  3894. p = tensor2.size(-1) if dim_tensor2 > 1 else 1
  3895. batch_tensor2: list[int] = []
  3896. # TODO: handling of slice
  3897. for i in range(dim_tensor2 - 2):
  3898. batch_tensor2.append(tensor2.size(i))
  3899. # Same optimization for the gradients as that in should_fold
  3900. # If we're going to broadcast, we force it to go through the should_fold branch
  3901. if (
  3902. dim_tensor1 == 3
  3903. and dim_tensor2 == 3
  3904. and batch_tensor1[0] != batch_tensor2[0]
  3905. ):
  3906. if batch_tensor1[0] == 1 and tensor1.requires_grad:
  3907. return matmul(tensor1.squeeze(0), tensor2)
  3908. if batch_tensor2[0] == 1 and tensor2.requires_grad:
  3909. return matmul(tensor1, tensor2.squeeze(0))
  3910. # expand the batch portion (i.e. cut off matrix dimensions and expand rest)
  3911. expand_batch_portion = list(
  3912. torch.broadcast_shapes(batch_tensor1, batch_tensor2)
  3913. )
  3914. tensor1_expand_size = expand_batch_portion + [n, m1]
  3915. expand_batch_product = prod(expand_batch_portion)
  3916. # HACK: We need reshape with symint support
  3917. tensor1_expanded = tensor1.expand(tensor1_expand_size).reshape(
  3918. expand_batch_product, n, m1
  3919. )
  3920. vector_rhs = dim_tensor2 == 1
  3921. if vector_rhs:
  3922. tensor2_expand_size = expand_batch_portion + [m2]
  3923. tensor2_expanded = (
  3924. tensor2.expand(tensor2_expand_size)
  3925. .reshape(expand_batch_product, m2)
  3926. .unsqueeze(2)
  3927. )
  3928. else:
  3929. tensor2_expand_size = expand_batch_portion + [m2, p]
  3930. tensor2_expanded = tensor2.expand(tensor2_expand_size).reshape(
  3931. expand_batch_product, m2, p
  3932. )
  3933. output_shape = expand_batch_portion
  3934. if dim_tensor1 > 1:
  3935. output_shape.append(n)
  3936. if dim_tensor2 > 1:
  3937. output_shape.append(p)
  3938. if vector_rhs:
  3939. return tensor1_expanded.bmm(tensor2_expanded).squeeze(-1).view(output_shape)
  3940. else:
  3941. return tensor1_expanded.bmm(tensor2_expanded).view(output_shape)
  3942. else:
  3943. torch._check(False, lambda: "both arguments to matmul need to be at least 1D")
  3944. @register_decomposition([aten.upsample_bicubic2d.default, aten.upsample_bicubic2d.out])
  3945. @aten.upsample_bicubic2d.default.py_impl(DispatchKey.Autograd)
  3946. @out_wrapper()
  3947. @pw_cast_for_opmath
  3948. def upsample_bicubic2d_default(
  3949. input: Tensor,
  3950. output_size: tuple[int, int],
  3951. align_corners: bool,
  3952. scale_h: Optional[float] = None,
  3953. scale_w: Optional[float] = None,
  3954. ) -> Tensor:
  3955. # get dimensions of original image
  3956. _, _, in_h, in_w = input.shape
  3957. # Calculate horizontal and vertical scaling factor
  3958. h_scale_factor = _compute_scale(in_h, output_size[0], align_corners, scale_h)
  3959. w_scale_factor = _compute_scale(in_w, output_size[1], align_corners, scale_w)
  3960. _, dtype = utils.elementwise_dtypes(
  3961. input, type_promotion_kind=utils.ELEMENTWISE_TYPE_PROMOTION_KIND.INT_TO_FLOAT
  3962. )
  3963. # We have to create arange with int64 dtype and use .to in order to avoid
  3964. # additional kernels creation in inductor and get a perf slowdown
  3965. i = torch.arange(output_size[0], device=input.device).to(dtype=dtype)
  3966. j = torch.arange(output_size[1], device=input.device).to(dtype=dtype)
  3967. x_float = _compute_source_index(w_scale_factor, j, align_corners)
  3968. y_float = _compute_source_index(h_scale_factor, i, align_corners)
  3969. y_float = y_float.unsqueeze(-1)
  3970. x = x_float.floor()
  3971. y = y_float.floor()
  3972. # We should also clamp xscale/yscale
  3973. # See guard_index_and_lambda in UpSample.h
  3974. yscale = (y_float - y).clamp(0.0, 1.0)
  3975. xscale = (x_float - x).clamp(0.0, 1.0)
  3976. x = x.to(torch.int64)
  3977. y = y.to(torch.int64)
  3978. iys_ofs = (y - 1, y, y + 1, y + 2)
  3979. ixs_ofs = (x - 1, x, x + 1, x + 2)
  3980. weights_x = _upsample_get_cubic_coefficients(xscale)
  3981. weights_y = _upsample_get_cubic_coefficients(yscale)
  3982. weights_precision_x, weights_precision_y = None, None
  3983. if input.dtype == torch.uint8:
  3984. weights_precision_x = _compute_weight_precision(weights_x)
  3985. weights_precision_y = _compute_weight_precision(weights_y)
  3986. weights_x = [
  3987. (w * (1 << weights_precision_x) + torch.sign(w) * 0.5).to(torch.int16)
  3988. for w in weights_x
  3989. ]
  3990. weights_y = [
  3991. (w * (1 << weights_precision_y) + torch.sign(w) * 0.5).to(torch.int16)
  3992. for w in weights_y
  3993. ]
  3994. def load_bounded(ys, xs):
  3995. y_idx = torch.clamp(ys, 0, in_h - 1)
  3996. x_idx = torch.clamp(xs, 0, in_w - 1)
  3997. v = aten._unsafe_index(input, [None, None, y_idx, x_idx])
  3998. return v
  3999. def get_x_interp(y):
  4000. src_x = tuple(load_bounded(y, x_ofs) for x_ofs in ixs_ofs)
  4001. if input.dtype == torch.uint8:
  4002. assert weights_precision_x is not None
  4003. return _sum_tensors_uint8(src_x, weights_x, weights_precision_x)
  4004. return _sum_tensors(c1 * c2 for (c1, c2) in zip(src_x, weights_x))
  4005. src_y = tuple(get_x_interp(y_ofs) for y_ofs in iys_ofs)
  4006. if input.dtype == torch.uint8:
  4007. assert weights_precision_y is not None
  4008. result = _sum_tensors_uint8(src_y, weights_y, weights_precision_y)
  4009. else:
  4010. result = _sum_tensors(c1 * c2 for (c1, c2) in zip(src_y, weights_y))
  4011. # convert output to correct memory format, if necessary
  4012. memory_format = utils.suggest_memory_format(input)
  4013. result = result.contiguous(memory_format=memory_format)
  4014. return result
  4015. @register_decomposition(aten.upsample_bicubic2d.vec)
  4016. @aten.upsample_bicubic2d.vec.py_impl(DispatchKey.CompositeImplicitAutograd)
  4017. @aten.upsample_bicubic2d.vec.py_impl(DispatchKey.Autograd)
  4018. @out_wrapper()
  4019. @pw_cast_for_opmath
  4020. def upsample_bicubic2d_vec(
  4021. a: Tensor,
  4022. output_size: Optional[tuple[int, int]],
  4023. align_corners: bool,
  4024. scale_factors: Optional[tuple[float, float]] = None,
  4025. ) -> Tensor:
  4026. torch._check(
  4027. bool(output_size) + bool(scale_factors) == 1,
  4028. lambda: "Must specify exactly one of output_size and scale_factors.",
  4029. )
  4030. if output_size is None:
  4031. assert scale_factors is not None
  4032. output_size = cast(
  4033. tuple[int, int],
  4034. tuple(
  4035. sym_int(sym_float(w) * scale)
  4036. for w, scale in zip(a.shape[2:], scale_factors)
  4037. ),
  4038. )
  4039. scale_h, scale_w = scale_factors if scale_factors else (None, None)
  4040. return upsample_bicubic2d_default(a, output_size, align_corners, scale_h, scale_w)
  4041. @register_decomposition(aten.reflection_pad1d)
  4042. @register_decomposition(aten.reflection_pad2d)
  4043. @register_decomposition(aten.reflection_pad3d)
  4044. @pw_cast_for_opmath
  4045. @out_wrapper()
  4046. def _reflection_pad(a: Tensor, padding: tuple[int, ...]) -> Tensor:
  4047. def idx(left, middle, right):
  4048. dim_idx = torch.arange(-left, middle + right, device=a.device)
  4049. return middle - 1 - (middle - 1 - dim_idx.abs()).abs()
  4050. return _reflection_or_replication_pad(
  4051. a,
  4052. padding,
  4053. idx,
  4054. )
  4055. @register_decomposition(aten.replication_pad1d)
  4056. @register_decomposition(aten.replication_pad2d)
  4057. @register_decomposition(aten.replication_pad3d)
  4058. @pw_cast_for_opmath
  4059. @out_wrapper()
  4060. def _replication_pad(a: Tensor, padding: tuple[int, ...]) -> Tensor:
  4061. def idx(left, middle, right):
  4062. dim_idx = torch.arange(-left, middle + right, device=a.device)
  4063. return torch.clamp(dim_idx, 0, middle - 1)
  4064. return _reflection_or_replication_pad(
  4065. a,
  4066. padding,
  4067. idx,
  4068. )
  4069. def _reflection_or_replication_pad(
  4070. a: Tensor,
  4071. padding: tuple[int, ...],
  4072. idx_fn: Callable[[int, int, int], Tensor],
  4073. ) -> Tensor:
  4074. dim = len(padding) // 2
  4075. torch._check(
  4076. a.dim() in (dim + 1, dim + 2),
  4077. lambda: f"reflection_pad{dim}d requires {dim + 1}D or {dim + 2}D input",
  4078. )
  4079. inp_shape = a.shape[-dim:]
  4080. nc_dim = a.dim() - dim
  4081. padding_left = [padding[2 * (dim - 1 - i)] for i in range(dim)]
  4082. padding_right = [padding[2 * (dim - 1 - i) + 1] for i in range(dim)]
  4083. result = a
  4084. for i in range(dim):
  4085. idx: list[Any] = [None] * result.dim()
  4086. idx[i + nc_dim] = idx_fn(padding_left[i], inp_shape[i], padding_right[i])
  4087. result = aten._unsafe_index(result, idx)
  4088. # convert output to correct memory format, if necessary
  4089. memory_format = utils.suggest_memory_format(result)
  4090. result = result.contiguous(memory_format=memory_format)
  4091. return result
  4092. @register_decomposition(aten.reflection_pad1d_backward)
  4093. @register_decomposition(aten.reflection_pad2d_backward)
  4094. @register_decomposition(aten.reflection_pad3d_backward)
  4095. @out_wrapper("grad_input")
  4096. def _reflection_pad_backward(grad_output, x, padding):
  4097. dim = len(padding) // 2
  4098. dhw = [h - 1 for h in x.shape[-dim:]]
  4099. padding_left = [padding[2 * (dim - 1 - i)] for i in range(dim)]
  4100. padding_right = [padding[2 * (dim - 1 - i) + 1] for i in range(dim)]
  4101. indices = []
  4102. for i in range(x.ndim):
  4103. view_shape = [1] * x.ndim
  4104. view_shape[i] = -1
  4105. indices.append(torch.arange(x.shape[i], device=x.device).view(view_shape))
  4106. b = indices[:-dim]
  4107. xyz = indices[-dim:]
  4108. def index_range_condition(index_range):
  4109. i, lb, ub = index_range
  4110. return torch.logical_and(i >= lb, i <= ub)
  4111. # Areas after reflection:
  4112. #
  4113. # top-left | top | top-right
  4114. # -----------------------------------------
  4115. # left | center | right
  4116. # -----------------------------------------
  4117. # bottom-left | bottom | bottom-right
  4118. #
  4119. # The center area is the original matrix. Other areas are reflections.
  4120. center = [xyz[i] + padding_left[i] for i in range(dim)]
  4121. left_reflect = [padding_left[i] - xyz[i] for i in range(dim)]
  4122. right_reflect = [2 * dhw[i] + padding_left[i] - xyz[i] for i in range(dim)]
  4123. # Accumulate gradients from different areas
  4124. # If some of the padding is negative, center load is not always valid
  4125. range_c = [
  4126. (center[i], 0, dhw[i] + padding_left[i] + padding_right[i]) for i in range(dim)
  4127. ]
  4128. cond = functools.reduce(
  4129. aten.logical_and, [index_range_condition(range_c[i]) for i in range(dim)]
  4130. )
  4131. grad = aten._unsafe_masked_index(grad_output, cond, b + center, 0.0)
  4132. def accumulate(grad, out, index_ranges):
  4133. # If the upper bound is less than the lower bound, we can get rid of one accumulation.
  4134. # This happens when the padding size is zero.
  4135. for i in range(dim):
  4136. upper_less_than_lower = index_ranges[i][2] < index_ranges[i][1]
  4137. if isinstance(upper_less_than_lower, bool) and upper_less_than_lower:
  4138. return grad
  4139. cond = functools.reduce(
  4140. aten.logical_and,
  4141. [index_range_condition(index_range) for index_range in index_ranges],
  4142. )
  4143. g = aten._unsafe_masked_index(grad_output, cond, b + out, 0.0)
  4144. return grad + g
  4145. for area in itertools.product(*[[-1, 0, 1] for _ in range(dim)]):
  4146. if area == tuple([0] * dim):
  4147. # center, this is already done.
  4148. continue
  4149. outs = []
  4150. index_ranges = []
  4151. for i in range(dim):
  4152. if area[i] == 0:
  4153. out = center[i]
  4154. index_range = range_c[i]
  4155. elif area[i] == -1:
  4156. out = left_reflect[i]
  4157. index_range = (xyz[i], 1, padding_left[i])
  4158. elif area[i] == 1:
  4159. out = right_reflect[i]
  4160. index_range = (xyz[i], dhw[i] - padding_right[i], dhw[i] - 1)
  4161. outs.append(out) # type: ignore[possibly-undefined]
  4162. index_ranges.append(index_range) # type: ignore[possibly-undefined]
  4163. grad = accumulate(grad, outs, index_ranges)
  4164. return grad
  4165. @register_decomposition(aten.aminmax)
  4166. @out_wrapper("min", "max")
  4167. def aminmax(self, *, dim=None, keepdim=False):
  4168. amin = torch.amin(self, dim=dim, keepdim=keepdim)
  4169. amax = torch.amax(self, dim=dim, keepdim=keepdim)
  4170. return amin, amax
  4171. @register_decomposition(aten.nansum)
  4172. @out_wrapper()
  4173. def nansum(self, dim=None, keepdim=False, *, dtype=None):
  4174. return aten.sum(torch.where(torch.isnan(self), 0, self), dim, keepdim, dtype=dtype)
  4175. @register_decomposition([aten.arange.default, aten.arange.out])
  4176. @out_wrapper()
  4177. def arange_default(
  4178. end: NumberType,
  4179. *,
  4180. dtype: Optional[torch.dtype] = None,
  4181. layout: torch.layout = torch.strided,
  4182. device: Optional[torch.device] = None,
  4183. pin_memory: bool = False,
  4184. ):
  4185. return aten.arange.start_step(
  4186. 0, end, 1, dtype=dtype, layout=layout, device=device, pin_memory=pin_memory
  4187. )
  4188. @register_decomposition([aten.arange.start])
  4189. def arange_start(
  4190. start: NumberType,
  4191. end: NumberType,
  4192. *,
  4193. dtype: Optional[torch.dtype] = None,
  4194. layout: torch.layout = torch.strided,
  4195. device: Optional[torch.device] = None,
  4196. pin_memory: bool = False,
  4197. ):
  4198. return aten.arange.start_step(
  4199. start, end, 1, dtype=dtype, layout=layout, device=device, pin_memory=pin_memory
  4200. )
  4201. @register_decomposition(out_dtype)
  4202. def out_dtype_decomp(*args, **kwargs):
  4203. from torch._higher_order_ops.out_dtype import out_dtype_dense
  4204. return out_dtype_dense(*args, **kwargs)
  4205. @register_decomposition(aten.multi_margin_loss)
  4206. @aten.multi_margin_loss.default.py_impl(DispatchKey.Autograd)
  4207. @out_wrapper()
  4208. def multi_margin_loss(
  4209. input: Tensor,
  4210. target: Tensor,
  4211. p: NumberType = 1,
  4212. margin: NumberType = 1,
  4213. weight: Optional[Tensor] = None,
  4214. reduction: int = Reduction.MEAN.value,
  4215. ) -> Tensor:
  4216. input = torch.atleast_2d(input)
  4217. target = torch.atleast_1d(target)
  4218. nframe = input.shape[0]
  4219. dim = input.shape[1]
  4220. torch._check(p == 1 or p == 2, lambda: "only p == 1 and p == 2 supported")
  4221. torch._check(
  4222. input.ndim == 2 and dim != 0,
  4223. lambda: f"Expected non-empty vector or matrix with optional 0-dim batch size, but got: {input.shape}",
  4224. )
  4225. torch._check(
  4226. target.ndim == 1 and target.numel() == nframe,
  4227. lambda: f"inconsistent target size, expected {nframe} but got {target.shape}",
  4228. )
  4229. if weight is not None:
  4230. weight = torch.atleast_1d(weight)
  4231. torch._check(
  4232. weight.ndim == 1 and weight.numel() == dim, # type: ignore[union-attr]
  4233. lambda: f"inconsistent weight size, expected {dim} but got {weight.shape}", # type: ignore[union-attr]
  4234. )
  4235. target = target.unsqueeze(1)
  4236. u = torch.gather(input, dim=1, index=target)
  4237. z = margin - u + input
  4238. z = z.clamp_min(0)
  4239. z = z if p == 1 else z * z
  4240. if weight is not None:
  4241. z = z * weight[target]
  4242. idx = torch.arange(dim, device=input.device)
  4243. z = torch.where(idx != target, z, 0)
  4244. if reduction == Reduction.MEAN.value:
  4245. return z.mean()
  4246. elif reduction == Reduction.SUM.value:
  4247. return z.sum() / z.shape[1]
  4248. else:
  4249. return z.mean(dim=1)
  4250. @register_decomposition(aten.multilabel_margin_loss_forward)
  4251. @aten.multilabel_margin_loss_forward.default.py_impl(DispatchKey.Autograd)
  4252. @out_wrapper("output", "is_target")
  4253. def multilabel_margin_loss_forward(
  4254. input: Tensor,
  4255. target: Tensor,
  4256. reduction: int,
  4257. ) -> tuple[Tensor, Tensor]:
  4258. orig_input_shape = input.shape
  4259. orig_target_shape = target.shape
  4260. input = torch.atleast_2d(input)
  4261. target = torch.atleast_2d(target)
  4262. dim = input.shape[1]
  4263. torch._check(
  4264. len(orig_input_shape) <= 2 and dim != 0,
  4265. lambda: f"Expected non-empty vector or matrix with optional 0-dim batch size, but got: {orig_input_shape}",
  4266. )
  4267. torch._check(
  4268. len(orig_target_shape) <= 2 and orig_target_shape == orig_input_shape,
  4269. lambda: f"inconsistent target size: {orig_target_shape} for input of size: {orig_input_shape}",
  4270. )
  4271. # ignores labels after the first -1, detects when -1 is not present
  4272. idx = torch.arange(dim, device=target.device)
  4273. is_end = target == -1
  4274. end_idx = torch.amin(torch.where(is_end, idx, dim), dim=-1, keepdim=True)
  4275. # target indices
  4276. target_mask = idx < end_idx
  4277. # masks target to be able to use gather, which doesn't allow -1
  4278. tidx0 = torch.where(target_mask, target, 0)
  4279. u = torch.gather(input, dim=-1, index=tidx0)
  4280. # is_target
  4281. tidx1 = torch.where(target_mask, target, -1)
  4282. is_target = torch.any(idx == tidx1.unsqueeze(dim=-1), dim=1)
  4283. # loss
  4284. z = 1.0 - u.T.unsqueeze(dim=-1) + input
  4285. z = z.clamp_min(0)
  4286. z = z / dim
  4287. # masks loss
  4288. z = torch.where(is_target, 0, z)
  4289. # reduction
  4290. if reduction == Reduction.MEAN.value:
  4291. z = z.sum(dim=(0, -1)).mean()
  4292. elif reduction == Reduction.SUM.value:
  4293. z = z.sum()
  4294. else:
  4295. z = z.sum(dim=(0, -1))
  4296. # result
  4297. is_target = is_target.to(input.dtype).reshape(orig_target_shape)
  4298. return z, is_target
  4299. # scaled_dot_product_attention used to be decomposed in pre-autograd, given that
  4300. # it calls _scaled_dot_product_attention_math and
  4301. # _scaled_dot_product_attention_math only has a CompositeImplicitAutograd
  4302. # kernel. As a result it's decomposed into ops with finer granularity.
  4303. # However recent PRs (#103826 #105131 #115913) added new logic in
  4304. # scaled_dot_product_attention and now it calls
  4305. # _scaled_dot_product_flash_attention_for_cpu in export path. This results
  4306. # in _scaled_dot_product_flash_attention_for_cpu showing up in export result.
  4307. # This decomposition ensures scaled_dot_product_attention is still decomposed
  4308. # the same way as before, i.e., going through
  4309. # _scaled_dot_product_attention_math. Notice that this decomp rule should be
  4310. # excluded by inductor.
  4311. @register_decomposition(aten._scaled_dot_product_flash_attention_for_cpu.default)
  4312. def scaled_dot_product_flash_attention_for_cpu(
  4313. query: Tensor,
  4314. key: Tensor,
  4315. value: Tensor,
  4316. dropout_p: float = 0.0,
  4317. is_causal: bool = False,
  4318. *,
  4319. attn_mask: Optional[Tensor] = None,
  4320. scale: Optional[float] = None,
  4321. ) -> tuple[Tensor, Tensor]:
  4322. torch._check(
  4323. torch.is_floating_point(query),
  4324. lambda: f"query must be FP32, FP64, BF16, FP16 but got {query.dtype}",
  4325. )
  4326. torch._check(
  4327. query.dim() == 4 and key.dim() == 4 and value.dim() == 4,
  4328. lambda: f"q, k, v must be a 4 dimensional tensor, got {query.dim()}, {key.dim()}, {value.dim()}",
  4329. )
  4330. torch._check(
  4331. dropout_p == 0.0, lambda: f"dropout probability must be zero, got {dropout_p}"
  4332. )
  4333. torch._check(
  4334. query.shape[3] == value.shape[3] and key.shape[3] == value.shape[3],
  4335. lambda: "q, k, v should have the same head size",
  4336. )
  4337. output, attn = aten._scaled_dot_product_attention_math.default(
  4338. query,
  4339. key,
  4340. value,
  4341. attn_mask=attn_mask,
  4342. dropout_p=dropout_p,
  4343. is_causal=is_causal,
  4344. dropout_mask=None,
  4345. scale=scale,
  4346. enable_gqa=query.size(1) != key.size(1),
  4347. )
  4348. # Why this change?
  4349. # In pre-dispatch export scaled_dot_product_attention is executed via
  4350. # * flash_attention.
  4351. # flash_attention allocates output tensor as (N, H, L, E) (see PR #134656)
  4352. # assume x: [N, H, L, E] is the output sdpa
  4353. # In MHA code, this output is then permuted via (2, 0, 1, 3) to get
  4354. # (L, N, H, E) dim tensor
  4355. # x = x.permute(2, 0, 1, 3).contiguous() and the viewed via
  4356. # x = x.view(L * N, H * E)
  4357. # During pre autograd dispatch call to contiguous is not traced because
  4358. # flash_attention output after the x.permute is already contiguous
  4359. # on which the view is valid
  4360. # However, during 2nd stage export, post-dispatch, we run _match variant
  4361. # instead of flash* to get the decomposition. _match variant returns
  4362. # x: [N, H, L, E] applying x.permute(2, 0, 1, 3) returns
  4363. # x: [L, N, H, E] and without converting this to contiguous tensor
  4364. # subsequent view is not valid and the export fails
  4365. # solution is to maintain the return tensor view from the decomp to be
  4366. # exactly same as *flash* variant.
  4367. # Really the invariant you want to maintain is:
  4368. # pre-dispatch op-output and its decomposed representation must
  4369. # return tensor with same view and dims
  4370. output = (
  4371. output.permute(2, 0, 1, 3)
  4372. .contiguous(memory_format=torch.contiguous_format)
  4373. .permute(1, 2, 0, 3)
  4374. )
  4375. return output, attn
  4376. def register_inplace(aten_op, outplace_op):
  4377. @register_decomposition(aten_op)
  4378. def inplace_op(*args, **kwargs):
  4379. out = outplace_op(*args, **kwargs)
  4380. return args[0].copy_(out)
  4381. return inplace_op
  4382. @register_decomposition([aten.baddbmm])
  4383. @out_wrapper(exact_dtype=True)
  4384. @pw_cast_for_opmath
  4385. def baddbmm(self, batch1, batch2, beta=1, alpha=1):
  4386. if not self.is_floating_point() and not self.is_complex():
  4387. beta = int(beta)
  4388. alpha = int(alpha)
  4389. result = torch.bmm(batch1, batch2)
  4390. if not isinstance(alpha, numbers.Number) or alpha != 1:
  4391. result = result * alpha
  4392. if beta == 0:
  4393. return result
  4394. if not isinstance(beta, numbers.Number) or beta != 1:
  4395. self = self * beta
  4396. return self + result
  4397. @register_decomposition(aten.floor_divide)
  4398. @out_wrapper()
  4399. def floor_divide(self, other):
  4400. return torch.div(self, other, rounding_mode="floor")
  4401. @register_decomposition(aten.sym_numel)
  4402. def sym_numel(t):
  4403. return functools.reduce(operator.mul, t.shape, 1)
  4404. @register_decomposition([aten.sum.default, aten.sum.out])
  4405. def sum_default(
  4406. self: Tensor,
  4407. *,
  4408. dtype: Optional[torch.dtype] = None,
  4409. out: Optional[Tensor] = None,
  4410. ) -> Tensor:
  4411. if out is None:
  4412. return aten.sum.dim_IntList(self, [], dtype=dtype)
  4413. else:
  4414. return aten.sum.IntList_out(self, [], dtype=dtype, out=out)
  4415. @register_decomposition([aten.squeeze.default, aten.squeeze.dim])
  4416. def squeeze_default(self: Tensor, dim: Optional[int] = None):
  4417. # handle a scalar directly
  4418. if not isinstance(self, torch.Tensor):
  4419. return self
  4420. # perform squeeze
  4421. if dim is None:
  4422. return aten.squeeze.dims(self, list(range(self.dim())))
  4423. else:
  4424. return aten.squeeze.dims(self, [dim])
  4425. @register_decomposition(torch.ops.aten._weight_norm_interface)
  4426. def _weight_norm_interface(v, g, dim=0):
  4427. # https://github.com/pytorch/pytorch/blob/852f8526c52190125446adc9a6ecbcc28fb66182/aten/src/ATen/native/WeightNorm.cpp#L58
  4428. keep_dim = tuple(i for i in range(len(v.shape)) if i != dim)
  4429. # align with cuda behavior, keep norm in 'float' when g is 'bfloat16'
  4430. norm_dtype = torch.float if g.dtype == torch.bfloat16 else None
  4431. norm = v.norm(2, keep_dim, keepdim=True, dtype=norm_dtype)
  4432. return v * (g / norm.to(g.dtype)), norm
  4433. @register_decomposition(aten.isin)
  4434. @out_wrapper()
  4435. def isin(elements, test_elements, *, assume_unique=False, invert=False):
  4436. # handle when either elements or test_elements are Scalars (they can't both be)
  4437. if not isinstance(elements, torch.Tensor):
  4438. elements = torch.tensor(elements, device=test_elements.device)
  4439. if not isinstance(test_elements, torch.Tensor):
  4440. if invert:
  4441. return torch.ne(elements, test_elements)
  4442. else:
  4443. return torch.eq(elements, test_elements)
  4444. if test_elements.numel() < 10.0 * pow(elements.numel(), 0.145):
  4445. return isin_default(elements, test_elements, invert=invert)
  4446. else:
  4447. return isin_sorting(
  4448. elements, test_elements, assume_unique=assume_unique, invert=invert
  4449. )
  4450. @register_decomposition(aten.bernoulli.default)
  4451. def bernoulli(
  4452. self: torch.Tensor,
  4453. *,
  4454. generator: Optional[torch.Generator] = None,
  4455. ) -> torch.Tensor:
  4456. if generator is None:
  4457. raw_p = torch.rand(self.size(), dtype=torch.float32, device=self.device)
  4458. else:
  4459. raw_p = torch.rand(
  4460. self.size(),
  4461. generator=generator,
  4462. dtype=torch.float32,
  4463. device=self.device,
  4464. )
  4465. p = (raw_p < self).to(self.dtype)
  4466. return p
  4467. def isin_default(elements, test_elements, *, invert=False):
  4468. if elements.numel() == 0:
  4469. return torch.empty_like(elements, dtype=torch.bool)
  4470. expanded_elem_shape = elements.shape + (1,) * test_elements.ndim
  4471. x = elements.view(expanded_elem_shape)
  4472. dim = tuple(range(-1, -test_elements.ndim - 1, -1))
  4473. res = (x == test_elements).any(dim=dim)
  4474. return ~res if invert else res
  4475. def isin_sorting(elements, test_elements, *, assume_unique=False, invert=False):
  4476. elements_flat = elements.flatten()
  4477. test_elements_flat = test_elements.flatten()
  4478. if assume_unique:
  4479. # This is the same as the aten implementation. For
  4480. # assume_unique=False, we cannot use unique() here, so we use a
  4481. # version with searchsorted instead.
  4482. all_elements = torch.cat([elements_flat, test_elements_flat])
  4483. sorted_elements, sorted_order = torch.sort(all_elements, stable=True)
  4484. duplicate_mask = sorted_elements[1:] == sorted_elements[:-1]
  4485. duplicate_mask = torch.constant_pad_nd(duplicate_mask, [0, 1], False)
  4486. if invert:
  4487. duplicate_mask = duplicate_mask.logical_not()
  4488. mask = torch.empty_like(duplicate_mask)
  4489. mask = mask.index_copy(0, sorted_order, duplicate_mask)
  4490. return mask[0 : elements.numel()]
  4491. else:
  4492. sorted_test_elements, _ = torch.sort(test_elements_flat)
  4493. idx = torch.searchsorted(sorted_test_elements, elements_flat)
  4494. test_idx = torch.where(idx < sorted_test_elements.numel(), idx, 0)
  4495. cmp = sorted_test_elements[test_idx] == elements_flat
  4496. cmp = cmp.logical_not() if invert else cmp
  4497. return cmp.reshape(elements.shape)
  4498. @register_decomposition(aten.take)
  4499. @out_wrapper()
  4500. def take(self, index):
  4501. flattened = self.reshape(-1)
  4502. return flattened[index]
  4503. @register_decomposition(aten.resize_as)
  4504. def resize_as(self, other, memory_format=None):
  4505. if memory_format is None:
  4506. memory_format = torch.contiguous_format
  4507. if memory_format == torch.preserve_format:
  4508. memory_format = suggest_memory_format(other)
  4509. return aten.resize(self, other.shape, memory_format=memory_format)
  4510. register_inplace(aten.addbmm_, aten.addbmm)
  4511. register_inplace(aten.addmm_, aten.addmm)
  4512. register_inplace(aten.addmv_, aten.addmv)
  4513. register_inplace(aten.baddbmm_, aten.baddbmm)
  4514. register_inplace(aten.fill_, aten.fill)
  4515. register_inplace(aten.gelu_, aten.gelu)
  4516. register_inplace(aten.hardswish_, aten.hardswish)
  4517. register_inplace(aten.hardtanh_, aten.hardtanh)
  4518. register_inplace(aten.hardsigmoid_, aten.hardsigmoid)
  4519. register_inplace(aten.__iand__, aten.__and__)
  4520. register_inplace(aten.__ilshift__, aten.__lshift__)
  4521. register_inplace(aten.index_put_, aten.index_put)
  4522. register_inplace(aten.index_reduce_, aten.index_reduce)
  4523. register_inplace(aten.__ior__, aten.__or__)
  4524. register_inplace(aten.__irshift__, aten.__rshift__)
  4525. register_inplace(aten.__ixor__, aten.__xor__)
  4526. register_inplace(aten.leaky_relu_, aten.leaky_relu)
  4527. register_inplace(aten.logit_, aten.logit)
  4528. register_inplace(aten.relu_, aten.relu)
  4529. register_inplace(aten.renorm_, aten.renorm)
  4530. register_inplace(aten.round_, aten.round)
  4531. register_inplace(aten.scatter_, aten.scatter)
  4532. register_inplace(aten.scatter_add_, aten.scatter_add)
  4533. register_inplace(aten.scatter_reduce_, aten.scatter_reduce)
  4534. register_inplace(aten.silu_, aten.silu)