decompositions.py 178 KB

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