_torch_docs.py 425 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383
  1. # mypy: allow-untyped-defs
  2. """Adds docstrings to functions defined in the torch._C module."""
  3. import re
  4. import torch._C
  5. from torch._C import _add_docstr as add_docstr
  6. def parse_kwargs(desc):
  7. r"""Map a description of args to a dictionary of {argname: description}.
  8. Input:
  9. (' weight (Tensor): a weight tensor\n' +
  10. ' Some optional description')
  11. Output: {
  12. 'weight': \
  13. 'weight (Tensor): a weight tensor\n Some optional description'
  14. }
  15. """
  16. # Split on exactly 4 spaces after a newline
  17. regx = re.compile(r"\n\s{4}(?!\s)")
  18. kwargs = [section.strip() for section in regx.split(desc)]
  19. kwargs = [section for section in kwargs if len(section) > 0]
  20. return {desc.split(" ")[0]: desc for desc in kwargs}
  21. def merge_dicts(*dicts):
  22. """Merge dictionaries into a single dictionary."""
  23. return {x: d[x] for d in dicts for x in d}
  24. common_args = parse_kwargs(
  25. """
  26. input (Tensor): the input tensor.
  27. generator (:class:`torch.Generator`, optional): a pseudorandom number generator for sampling
  28. out (Tensor, optional): the output tensor.
  29. memory_format (:class:`torch.memory_format`, optional): the desired memory format of
  30. returned tensor. Default: ``torch.preserve_format``.
  31. """
  32. )
  33. reduceops_common_args = merge_dicts(
  34. common_args,
  35. parse_kwargs(
  36. """
  37. dtype (:class:`torch.dtype`, optional): the desired data type of returned tensor.
  38. If specified, the input tensor is casted to :attr:`dtype` before the operation
  39. is performed. This is useful for preventing data type overflows. Default: None.
  40. keepdim (bool): whether the output tensor has :attr:`dim` retained or not.
  41. """
  42. ),
  43. {
  44. "opt_keepdim": """
  45. keepdim (bool, optional): whether the output tensor has :attr:`dim` retained or not. Default: ``False``.
  46. """
  47. },
  48. )
  49. multi_dim_common = merge_dicts(
  50. reduceops_common_args,
  51. parse_kwargs(
  52. """
  53. dim (int or tuple of ints): the dimension or dimensions to reduce.
  54. """
  55. ),
  56. {
  57. "keepdim_details": """
  58. If :attr:`keepdim` is ``True``, the output tensor is of the same size
  59. as :attr:`input` except in the dimension(s) :attr:`dim` where it is of size 1.
  60. Otherwise, :attr:`dim` is squeezed (see :func:`torch.squeeze`), resulting in the
  61. output tensor having 1 (or ``len(dim)``) fewer dimension(s).
  62. """
  63. },
  64. {
  65. "opt_dim": """
  66. dim (int or tuple of ints, optional): the dimension or dimensions to reduce.
  67. """
  68. },
  69. {
  70. "opt_dim_all_reduce": """
  71. dim (int or tuple of ints, optional): the dimension or dimensions to reduce.
  72. If ``None``, all dimensions are reduced.
  73. """
  74. },
  75. )
  76. single_dim_common = merge_dicts(
  77. reduceops_common_args,
  78. parse_kwargs(
  79. """
  80. dim (int): the dimension to reduce.
  81. """
  82. ),
  83. {
  84. "opt_dim": """
  85. dim (int, optional): the dimension to reduce.
  86. """
  87. },
  88. {
  89. "opt_dim_all_reduce": """
  90. dim (int, optional): the dimension to reduce.
  91. If ``None``, all dimensions are reduced.
  92. """
  93. },
  94. {
  95. "opt_dim_without_none": """
  96. dim (int, optional): the dimension to reduce. If omitted, all dimensions are reduced. Explicit ``None`` is not supported.
  97. """
  98. },
  99. {
  100. "keepdim_details": """If :attr:`keepdim` is ``True``, the output tensor is of the same size
  101. as :attr:`input` except in the dimension :attr:`dim` where it is of size 1.
  102. Otherwise, :attr:`dim` is squeezed (see :func:`torch.squeeze`), resulting in
  103. the output tensor having 1 fewer dimension than :attr:`input`."""
  104. },
  105. )
  106. factory_common_args = merge_dicts(
  107. common_args,
  108. parse_kwargs(
  109. """
  110. dtype (:class:`torch.dtype`, optional): the desired data type of returned tensor.
  111. Default: if ``None``, uses a global default (see :func:`torch.set_default_dtype`).
  112. layout (:class:`torch.layout`, optional): the desired layout of returned Tensor.
  113. Default: ``torch.strided``.
  114. device (:class:`torch.device`, optional): the desired device of returned tensor.
  115. Default: if ``None``, uses the current device for the default tensor type
  116. (see :func:`torch.set_default_device`). :attr:`device` will be the CPU
  117. for CPU tensor types and the current CUDA device for CUDA tensor types.
  118. requires_grad (bool, optional): If autograd should record operations on the
  119. returned tensor. Default: ``False``.
  120. pin_memory (bool, optional): If set, returned tensor would be allocated in
  121. the pinned memory. Works only for CPU tensors. Default: ``False``.
  122. memory_format (:class:`torch.memory_format`, optional): the desired memory format of
  123. returned Tensor. Default: ``torch.contiguous_format``.
  124. check_invariants (bool, optional): If sparse tensor invariants are checked.
  125. Default: as returned by :func:`torch.sparse.check_sparse_tensor_invariants.is_enabled`,
  126. initially False.
  127. """
  128. ),
  129. {
  130. "sparse_factory_device_note": """\
  131. .. note::
  132. If the ``device`` argument is not specified the device of the given
  133. :attr:`values` and indices tensor(s) must match. If, however, the
  134. argument is specified the input Tensors will be converted to the
  135. given device and in turn determine the device of the constructed
  136. sparse tensor."""
  137. },
  138. )
  139. factory_like_common_args = parse_kwargs(
  140. """
  141. input (Tensor): the size of :attr:`input` will determine size of the output tensor.
  142. layout (:class:`torch.layout`, optional): the desired layout of returned tensor.
  143. Default: if ``None``, defaults to the layout of :attr:`input`.
  144. dtype (:class:`torch.dtype`, optional): the desired data type of returned Tensor.
  145. Default: if ``None``, defaults to the dtype of :attr:`input`.
  146. device (:class:`torch.device`, optional): the desired device of returned tensor.
  147. Default: if ``None``, defaults to the device of :attr:`input`.
  148. requires_grad (bool, optional): If autograd should record operations on the
  149. returned tensor. Default: ``False``.
  150. pin_memory (bool, optional): If set, returned tensor would be allocated in
  151. the pinned memory. Works only for CPU tensors. Default: ``False``.
  152. memory_format (:class:`torch.memory_format`, optional): the desired memory format of
  153. returned Tensor. Default: ``torch.preserve_format``.
  154. """
  155. )
  156. factory_data_common_args = parse_kwargs(
  157. """
  158. data (array_like): Initial data for the tensor. Can be a list, tuple,
  159. NumPy ``ndarray``, scalar, and other types.
  160. dtype (:class:`torch.dtype`, optional): the desired data type of returned tensor.
  161. Default: if ``None``, infers data type from :attr:`data`.
  162. device (:class:`torch.device`, optional): the desired device of returned tensor.
  163. Default: if ``None``, uses the current device for the default tensor type
  164. (see :func:`torch.set_default_device`). :attr:`device` will be the CPU
  165. for CPU tensor types and the current CUDA device for CUDA tensor types.
  166. requires_grad (bool, optional): If autograd should record operations on the
  167. returned tensor. Default: ``False``.
  168. pin_memory (bool, optional): If set, returned tensor would be allocated in
  169. the pinned memory. Works only for CPU tensors. Default: ``False``.
  170. """
  171. )
  172. tf32_notes = {
  173. "tf32_note": """This operator supports :ref:`TensorFloat32<tf32_on_ampere>`."""
  174. }
  175. rocm_fp16_notes = {
  176. "rocm_fp16_note": """On certain ROCm devices, when using float16 inputs this module will use \
  177. :ref:`different precision<fp16_on_mi200>` for backward."""
  178. }
  179. reproducibility_notes: dict[str, str] = {
  180. "forward_reproducibility_note": """This operation may behave nondeterministically when given tensors on \
  181. a CUDA device. See :doc:`/notes/randomness` for more information.""",
  182. "backward_reproducibility_note": """This operation may produce nondeterministic gradients when given tensors on \
  183. a CUDA device. See :doc:`/notes/randomness` for more information.""",
  184. "cudnn_reproducibility_note": """In some circumstances when given tensors on a CUDA device \
  185. and using CuDNN, this operator may select a nondeterministic algorithm to increase performance. If this is \
  186. undesirable, you can try to make the operation deterministic (potentially at \
  187. a performance cost) by setting ``torch.backends.cudnn.deterministic = True``. \
  188. See :doc:`/notes/randomness` for more information.""",
  189. }
  190. sparse_support_notes = {
  191. "sparse_beta_warning": """
  192. .. warning::
  193. Sparse support is a beta feature and some layout(s)/dtype/device combinations may not be supported,
  194. or may not have autograd support. If you notice missing functionality please
  195. open a feature request.""",
  196. }
  197. add_docstr(
  198. torch.abs,
  199. r"""
  200. abs(input: Tensor, *, out: Optional[Tensor]) -> Tensor
  201. Computes the absolute value of each element in :attr:`input`.
  202. .. math::
  203. \text{out}_{i} = |\text{input}_{i}|
  204. """
  205. + r"""
  206. Args:
  207. {input}
  208. Keyword args:
  209. {out}
  210. Example::
  211. >>> torch.abs(torch.tensor([-1, -2, 3]))
  212. tensor([ 1, 2, 3])
  213. """.format(**common_args),
  214. )
  215. add_docstr(
  216. torch.absolute,
  217. r"""
  218. absolute(input: Tensor, *, out: Optional[Tensor]) -> Tensor
  219. Alias for :func:`torch.abs`
  220. """,
  221. )
  222. add_docstr(
  223. torch.acos,
  224. r"""
  225. acos(input: Tensor, *, out: Optional[Tensor]) -> Tensor
  226. Computes the inverse cosine of each element in :attr:`input`.
  227. .. math::
  228. \text{out}_{i} = \cos^{-1}(\text{input}_{i})
  229. """
  230. + r"""
  231. Args:
  232. {input}
  233. Keyword args:
  234. {out}
  235. Example::
  236. >>> a = torch.randn(4)
  237. >>> a
  238. tensor([ 0.3348, -0.5889, 0.2005, -0.1584])
  239. >>> torch.acos(a)
  240. tensor([ 1.2294, 2.2004, 1.3690, 1.7298])
  241. """.format(**common_args),
  242. )
  243. add_docstr(
  244. torch.arccos,
  245. r"""
  246. arccos(input: Tensor, *, out: Optional[Tensor]) -> Tensor
  247. Alias for :func:`torch.acos`.
  248. """,
  249. )
  250. add_docstr(
  251. torch.acosh,
  252. r"""
  253. acosh(input: Tensor, *, out: Optional[Tensor]) -> Tensor
  254. Returns a new tensor with the inverse hyperbolic cosine of the elements of :attr:`input`.
  255. .. math::
  256. \text{out}_{i} = \cosh^{-1}(\text{input}_{i})
  257. Note:
  258. The domain of the inverse hyperbolic cosine is `[1, inf)` and values outside this range
  259. will be mapped to ``NaN``, except for `+ INF` for which the output is mapped to `+ INF`.
  260. """
  261. + r"""
  262. Args:
  263. {input}
  264. Keyword arguments:
  265. {out}
  266. Example::
  267. >>> a = torch.randn(4).uniform_(1, 2)
  268. >>> a
  269. tensor([ 1.3192, 1.9915, 1.9674, 1.7151 ])
  270. >>> torch.acosh(a)
  271. tensor([ 0.7791, 1.3120, 1.2979, 1.1341 ])
  272. """.format(**common_args),
  273. )
  274. add_docstr(
  275. torch.arccosh,
  276. r"""
  277. arccosh(input: Tensor, *, out: Optional[Tensor]) -> Tensor
  278. Alias for :func:`torch.acosh`.
  279. """,
  280. )
  281. add_docstr(
  282. torch.index_add,
  283. r"""
  284. index_add(input: Tensor, dim: int, index: Tensor, source: Tensor, *, alpha: Union[Number, _complex] = 1, out: Optional[Tensor]) -> Tensor # noqa: B950
  285. See :meth:`~Tensor.index_add_` for function description.
  286. """,
  287. )
  288. add_docstr(
  289. torch.index_copy,
  290. r"""
  291. index_copy(input: Tensor, dim: int, index: Tensor, source: Tensor, *, out: Optional[Tensor]) -> Tensor
  292. See :meth:`~Tensor.index_add_` for function description.
  293. """,
  294. )
  295. add_docstr(
  296. torch.index_reduce,
  297. r"""
  298. index_reduce(input: Tensor, dim: int, index: Tensor, source: Tensor, reduce: str, *, include_self: bool = True, out: Optional[Tensor]) -> Tensor # noqa: B950
  299. See :meth:`~Tensor.index_reduce_` for function description.
  300. """,
  301. )
  302. add_docstr(
  303. torch.add,
  304. r"""
  305. add(input, other, *, alpha=1, out=None) -> Tensor
  306. Adds :attr:`other`, scaled by :attr:`alpha`, to :attr:`input`.
  307. .. math::
  308. \text{{out}}_i = \text{{input}}_i + \text{{alpha}} \times \text{{other}}_i
  309. """
  310. + r"""
  311. Supports :ref:`broadcasting to a common shape <broadcasting-semantics>`,
  312. :ref:`type promotion <type-promotion-doc>`, and integer, float, and complex inputs.
  313. Args:
  314. {input}
  315. other (Tensor or Number): the tensor or number to add to :attr:`input`.
  316. Keyword arguments:
  317. alpha (Number): the multiplier for :attr:`other`.
  318. {out}
  319. Examples::
  320. >>> a = torch.randn(4)
  321. >>> a
  322. tensor([ 0.0202, 1.0985, 1.3506, -0.6056])
  323. >>> torch.add(a, 20)
  324. tensor([ 20.0202, 21.0985, 21.3506, 19.3944])
  325. >>> b = torch.randn(4)
  326. >>> b
  327. tensor([-0.9732, -0.3497, 0.6245, 0.4022])
  328. >>> c = torch.randn(4, 1)
  329. >>> c
  330. tensor([[ 0.3743],
  331. [-1.7724],
  332. [-0.5811],
  333. [-0.8017]])
  334. >>> torch.add(b, c, alpha=10)
  335. tensor([[ 2.7695, 3.3930, 4.3672, 4.1450],
  336. [-18.6971, -18.0736, -17.0994, -17.3216],
  337. [ -6.7845, -6.1610, -5.1868, -5.4090],
  338. [ -8.9902, -8.3667, -7.3925, -7.6147]])
  339. """.format(**common_args),
  340. )
  341. add_docstr(
  342. torch.addbmm,
  343. r"""
  344. addbmm(input, batch1, batch2, *, beta=1, alpha=1, out=None) -> Tensor
  345. Performs a batch matrix-matrix product of matrices stored
  346. in :attr:`batch1` and :attr:`batch2`,
  347. with a reduced add step (all matrix multiplications get accumulated
  348. along the first dimension).
  349. :attr:`input` is added to the final result.
  350. :attr:`batch1` and :attr:`batch2` must be 3-D tensors each containing the
  351. same number of matrices.
  352. If :attr:`batch1` is a :math:`(b \times n \times m)` tensor, :attr:`batch2` is a
  353. :math:`(b \times m \times p)` tensor, :attr:`input` must be
  354. :ref:`broadcastable <broadcasting-semantics>` with a :math:`(n \times p)` tensor
  355. and :attr:`out` will be a :math:`(n \times p)` tensor.
  356. .. math::
  357. out = \beta\ \text{input} + \alpha\ (\sum_{i=0}^{b-1} \text{batch1}_i \mathbin{@} \text{batch2}_i)
  358. If :attr:`beta` is 0, then the content of :attr:`input` will be ignored, and `nan` and `inf` in
  359. it will not be propagated.
  360. """
  361. + r"""
  362. For inputs of type `FloatTensor` or `DoubleTensor`, arguments :attr:`beta` and :attr:`alpha`
  363. must be real numbers, otherwise they should be integers.
  364. {tf32_note}
  365. {rocm_fp16_note}
  366. Args:
  367. input (Tensor): matrix to be added
  368. batch1 (Tensor): the first batch of matrices to be multiplied
  369. batch2 (Tensor): the second batch of matrices to be multiplied
  370. Keyword args:
  371. beta (Number, optional): multiplier for :attr:`input` (:math:`\beta`)
  372. alpha (Number, optional): multiplier for `batch1 @ batch2` (:math:`\alpha`)
  373. {out}
  374. Example::
  375. >>> M = torch.randn(3, 5)
  376. >>> batch1 = torch.randn(10, 3, 4)
  377. >>> batch2 = torch.randn(10, 4, 5)
  378. >>> torch.addbmm(M, batch1, batch2)
  379. tensor([[ 6.6311, 0.0503, 6.9768, -12.0362, -2.1653],
  380. [ -4.8185, -1.4255, -6.6760, 8.9453, 2.5743],
  381. [ -3.8202, 4.3691, 1.0943, -1.1109, 5.4730]])
  382. """.format(**common_args, **tf32_notes, **rocm_fp16_notes),
  383. )
  384. add_docstr(
  385. torch.addcdiv,
  386. r"""
  387. addcdiv(input, tensor1, tensor2, *, value=1, out=None) -> Tensor
  388. Performs the element-wise division of :attr:`tensor1` by :attr:`tensor2`,
  389. multiplies the result by the scalar :attr:`value` and adds it to :attr:`input`.
  390. .. warning::
  391. Integer division with addcdiv is no longer supported, and in a future
  392. release addcdiv will perform a true division of tensor1 and tensor2.
  393. The historic addcdiv behavior can be implemented as
  394. (input + value * torch.trunc(tensor1 / tensor2)).to(input.dtype)
  395. for integer inputs and as (input + value * tensor1 / tensor2) for float inputs.
  396. The future addcdiv behavior is just the latter implementation:
  397. (input + value * tensor1 / tensor2), for all dtypes.
  398. .. math::
  399. \text{out}_i = \text{input}_i + \text{value} \times \frac{\text{tensor1}_i}{\text{tensor2}_i}
  400. """
  401. + r"""
  402. The shapes of :attr:`input`, :attr:`tensor1`, and :attr:`tensor2` must be
  403. :ref:`broadcastable <broadcasting-semantics>`.
  404. For inputs of type `FloatTensor` or `DoubleTensor`, :attr:`value` must be
  405. a real number, otherwise an integer.
  406. Args:
  407. input (Tensor): the tensor to be added
  408. tensor1 (Tensor): the numerator tensor
  409. tensor2 (Tensor): the denominator tensor
  410. Keyword args:
  411. value (Number, optional): multiplier for :math:`\text{{tensor1}} / \text{{tensor2}}`
  412. {out}
  413. Example::
  414. >>> t = torch.randn(1, 3)
  415. >>> t1 = torch.randn(3, 1)
  416. >>> t2 = torch.randn(1, 3)
  417. >>> torch.addcdiv(t, t1, t2, value=0.1)
  418. tensor([[-0.2312, -3.6496, 0.1312],
  419. [-1.0428, 3.4292, -0.1030],
  420. [-0.5369, -0.9829, 0.0430]])
  421. """.format(**common_args),
  422. )
  423. add_docstr(
  424. torch.addcmul,
  425. r"""
  426. addcmul(input, tensor1, tensor2, *, value=1, out=None) -> Tensor
  427. Performs the element-wise multiplication of :attr:`tensor1`
  428. by :attr:`tensor2`, multiplies the result by the scalar :attr:`value`
  429. and adds it to :attr:`input`.
  430. .. math::
  431. \text{out}_i = \text{input}_i + \text{value} \times \text{tensor1}_i \times \text{tensor2}_i
  432. """
  433. + r"""
  434. The shapes of :attr:`tensor`, :attr:`tensor1`, and :attr:`tensor2` must be
  435. :ref:`broadcastable <broadcasting-semantics>`.
  436. For inputs of type `FloatTensor` or `DoubleTensor`, :attr:`value` must be
  437. a real number, otherwise an integer.
  438. Args:
  439. input (Tensor): the tensor to be added
  440. tensor1 (Tensor): the tensor to be multiplied
  441. tensor2 (Tensor): the tensor to be multiplied
  442. Keyword args:
  443. value (Number, optional): multiplier for :math:`tensor1 .* tensor2`
  444. {out}
  445. Example::
  446. >>> t = torch.randn(1, 3)
  447. >>> t1 = torch.randn(3, 1)
  448. >>> t2 = torch.randn(1, 3)
  449. >>> torch.addcmul(t, t1, t2, value=0.1)
  450. tensor([[-0.8635, -0.6391, 1.6174],
  451. [-0.7617, -0.5879, 1.7388],
  452. [-0.8353, -0.6249, 1.6511]])
  453. """.format(**common_args),
  454. )
  455. add_docstr(
  456. torch.addmm,
  457. r"""
  458. addmm(input, mat1, mat2, out_dtype=None, *, beta=1, alpha=1, out=None) -> Tensor
  459. Performs a matrix multiplication of the matrices :attr:`mat1` and :attr:`mat2`.
  460. The matrix :attr:`input` is added to the final result.
  461. If :attr:`mat1` is a :math:`(n \times m)` tensor, :attr:`mat2` is a
  462. :math:`(m \times p)` tensor, then :attr:`input` must be
  463. :ref:`broadcastable <broadcasting-semantics>` with a :math:`(n \times p)` tensor
  464. and :attr:`out` will be a :math:`(n \times p)` tensor.
  465. :attr:`alpha` and :attr:`beta` are scaling factors on matrix-vector product between
  466. :attr:`mat1` and :attr:`mat2` and the added matrix :attr:`input` respectively.
  467. .. math::
  468. \text{out} = \beta\ \text{input} + \alpha\ (\text{mat1}_i \mathbin{@} \text{mat2}_i)
  469. If :attr:`beta` is 0, then the content of :attr:`input` will be ignored, and `nan` and `inf` in
  470. it will not be propagated.
  471. """
  472. + r"""
  473. For inputs of type `FloatTensor` or `DoubleTensor`, arguments :attr:`beta` and
  474. :attr:`alpha` must be real numbers, otherwise they should be integers.
  475. This operation has support for arguments with :ref:`sparse layouts<sparse-docs>`. If
  476. :attr:`input` is sparse the result will have the same layout and if :attr:`out`
  477. is provided it must have the same layout as :attr:`input`.
  478. {sparse_beta_warning}
  479. {tf32_note}
  480. {rocm_fp16_note}
  481. Args:
  482. input (Tensor): matrix to be added
  483. mat1 (Tensor): the first matrix to be matrix multiplied
  484. mat2 (Tensor): the second matrix to be matrix multiplied
  485. out_dtype (dtype, optional): the dtype of the output tensor,
  486. Supported only on CUDA and for torch.float32 given
  487. torch.float16/torch.bfloat16 input dtypes
  488. Keyword args:
  489. beta (Number, optional): multiplier for :attr:`input` (:math:`\beta`)
  490. alpha (Number, optional): multiplier for :math:`mat1 @ mat2` (:math:`\alpha`)
  491. {out}
  492. Example::
  493. >>> M = torch.randn(2, 3)
  494. >>> mat1 = torch.randn(2, 3)
  495. >>> mat2 = torch.randn(3, 3)
  496. >>> torch.addmm(M, mat1, mat2)
  497. tensor([[-4.8716, 1.4671, -1.3746],
  498. [ 0.7573, -3.9555, -2.8681]])
  499. """.format(**common_args, **tf32_notes, **rocm_fp16_notes, **sparse_support_notes),
  500. )
  501. add_docstr(
  502. torch.adjoint,
  503. r"""
  504. adjoint(input: Tensor) -> Tensor
  505. Returns a view of the tensor conjugated and with the last two dimensions transposed.
  506. ``x.adjoint()`` is equivalent to ``x.transpose(-2, -1).conj()`` for complex tensors and
  507. to ``x.transpose(-2, -1)`` for real tensors.
  508. Args:
  509. {input}
  510. Example::
  511. >>> x = torch.arange(4, dtype=torch.float)
  512. >>> A = torch.complex(x, x).reshape(2, 2)
  513. >>> A
  514. tensor([[0.+0.j, 1.+1.j],
  515. [2.+2.j, 3.+3.j]])
  516. >>> A.adjoint()
  517. tensor([[0.-0.j, 2.-2.j],
  518. [1.-1.j, 3.-3.j]])
  519. >>> (A.adjoint() == A.mH).all()
  520. tensor(True)
  521. """,
  522. )
  523. add_docstr(
  524. torch.sspaddmm,
  525. r"""
  526. sspaddmm(input, mat1, mat2, *, beta=1, alpha=1, out=None) -> Tensor
  527. Matrix multiplies a sparse tensor :attr:`mat1` with a dense tensor
  528. :attr:`mat2`, then adds the sparse tensor :attr:`input` to the result.
  529. Note: This function is equivalent to :func:`torch.addmm`, except
  530. :attr:`input` and :attr:`mat1` are sparse.
  531. Args:
  532. input (Tensor): a sparse matrix to be added
  533. mat1 (Tensor): a sparse matrix to be matrix multiplied
  534. mat2 (Tensor): a dense matrix to be matrix multiplied
  535. Keyword args:
  536. beta (Number, optional): multiplier for :attr:`mat` (:math:`\beta`)
  537. alpha (Number, optional): multiplier for :math:`mat1 @ mat2` (:math:`\alpha`)
  538. {out}
  539. """.format(**common_args),
  540. )
  541. add_docstr(
  542. torch.smm,
  543. r"""
  544. smm(input, mat) -> Tensor
  545. Performs a matrix multiplication of the sparse matrix :attr:`input`
  546. with the dense matrix :attr:`mat`.
  547. Args:
  548. input (Tensor): a sparse matrix to be matrix multiplied
  549. mat (Tensor): a dense matrix to be matrix multiplied
  550. """,
  551. )
  552. add_docstr(
  553. torch.addmv,
  554. r"""
  555. addmv(input, mat, vec, *, beta=1, alpha=1, out=None) -> Tensor
  556. Performs a matrix-vector product of the matrix :attr:`mat` and
  557. the vector :attr:`vec`.
  558. The vector :attr:`input` is added to the final result.
  559. If :attr:`mat` is a :math:`(n \times m)` tensor, :attr:`vec` is a 1-D tensor of
  560. size `m`, then :attr:`input` must be
  561. :ref:`broadcastable <broadcasting-semantics>` with a 1-D tensor of size `n` and
  562. :attr:`out` will be 1-D tensor of size `n`.
  563. :attr:`alpha` and :attr:`beta` are scaling factors on matrix-vector product between
  564. :attr:`mat` and :attr:`vec` and the added tensor :attr:`input` respectively.
  565. .. math::
  566. \text{out} = \beta\ \text{input} + \alpha\ (\text{mat} \mathbin{@} \text{vec})
  567. If :attr:`beta` is 0, then the content of :attr:`input` will be ignored, and `nan` and `inf` in
  568. it will not be propagated.
  569. """
  570. + r"""
  571. For inputs of type `FloatTensor` or `DoubleTensor`, arguments :attr:`beta` and
  572. :attr:`alpha` must be real numbers, otherwise they should be integers.
  573. Args:
  574. input (Tensor): vector to be added
  575. mat (Tensor): matrix to be matrix multiplied
  576. vec (Tensor): vector to be matrix multiplied
  577. Keyword args:
  578. beta (Number, optional): multiplier for :attr:`input` (:math:`\beta`)
  579. alpha (Number, optional): multiplier for :math:`mat @ vec` (:math:`\alpha`)
  580. {out}
  581. Example::
  582. >>> M = torch.randn(2)
  583. >>> mat = torch.randn(2, 3)
  584. >>> vec = torch.randn(3)
  585. >>> torch.addmv(M, mat, vec)
  586. tensor([-0.3768, -5.5565])
  587. """.format(**common_args),
  588. )
  589. add_docstr(
  590. torch.addr,
  591. r"""
  592. addr(input, vec1, vec2, *, beta=1, alpha=1, out=None) -> Tensor
  593. Performs the outer-product of vectors :attr:`vec1` and :attr:`vec2`
  594. and adds it to the matrix :attr:`input`.
  595. Optional values :attr:`beta` and :attr:`alpha` are scaling factors on the
  596. outer product between :attr:`vec1` and :attr:`vec2` and the added matrix
  597. :attr:`input` respectively.
  598. .. math::
  599. \text{out} = \beta\ \text{input} + \alpha\ (\text{vec1} \otimes \text{vec2})
  600. If :attr:`beta` is 0, then the content of :attr:`input` will be ignored, and `nan` and `inf` in
  601. it will not be propagated.
  602. """
  603. + r"""
  604. If :attr:`vec1` is a vector of size `n` and :attr:`vec2` is a vector
  605. of size `m`, then :attr:`input` must be
  606. :ref:`broadcastable <broadcasting-semantics>` with a matrix of size
  607. :math:`(n \times m)` and :attr:`out` will be a matrix of size
  608. :math:`(n \times m)`.
  609. Args:
  610. input (Tensor): matrix to be added
  611. vec1 (Tensor): the first vector of the outer product
  612. vec2 (Tensor): the second vector of the outer product
  613. Keyword args:
  614. beta (Number, optional): multiplier for :attr:`input` (:math:`\beta`)
  615. alpha (Number, optional): multiplier for :math:`\text{{vec1}} \otimes \text{{vec2}}` (:math:`\alpha`)
  616. {out}
  617. Example::
  618. >>> vec1 = torch.arange(1., 4.)
  619. >>> vec2 = torch.arange(1., 3.)
  620. >>> M = torch.zeros(3, 2)
  621. >>> torch.addr(M, vec1, vec2)
  622. tensor([[ 1., 2.],
  623. [ 2., 4.],
  624. [ 3., 6.]])
  625. """.format(**common_args),
  626. )
  627. add_docstr(
  628. torch.allclose,
  629. r"""
  630. allclose(input: Tensor, other: Tensor, rtol: float = 1e-05, atol: float = 1e-08, equal_nan: bool = False) -> bool
  631. This function checks if :attr:`input` and :attr:`other` satisfy the condition:
  632. .. math::
  633. \lvert \text{input}_i - \text{other}_i \rvert \leq \texttt{atol} + \texttt{rtol} \times \lvert \text{other}_i \rvert
  634. """
  635. + r"""
  636. elementwise, for all elements of :attr:`input` and :attr:`other`. The behaviour of this function is analogous to
  637. `numpy.allclose <https://numpy.org/doc/stable/reference/generated/numpy.allclose.html>`_
  638. Args:
  639. input (Tensor): first tensor to compare
  640. other (Tensor): second tensor to compare
  641. atol (float, optional): absolute tolerance. Default: 1e-08
  642. rtol (float, optional): relative tolerance. Default: 1e-05
  643. equal_nan (bool, optional): if ``True``, then two ``NaN`` s will be considered equal. Default: ``False``
  644. Example::
  645. >>> torch.allclose(torch.tensor([10000., 1e-07]), torch.tensor([10000.1, 1e-08]))
  646. False
  647. >>> torch.allclose(torch.tensor([10000., 1e-08]), torch.tensor([10000.1, 1e-09]))
  648. True
  649. >>> torch.allclose(torch.tensor([1.0, float('nan')]), torch.tensor([1.0, float('nan')]))
  650. False
  651. >>> torch.allclose(torch.tensor([1.0, float('nan')]), torch.tensor([1.0, float('nan')]), equal_nan=True)
  652. True
  653. """,
  654. )
  655. add_docstr(
  656. torch.all,
  657. r"""
  658. all(input: Tensor, *, out=None) -> Tensor
  659. Tests if all elements in :attr:`input` evaluate to `True`.
  660. .. note:: This function matches the behaviour of NumPy in returning
  661. output of dtype `bool` for all supported dtypes except `uint8`.
  662. For `uint8` the dtype of output is `uint8` itself.
  663. Args:
  664. {input}
  665. Keyword args:
  666. {out}
  667. Example::
  668. >>> a = torch.rand(1, 2).bool()
  669. >>> a
  670. tensor([[False, True]], dtype=torch.bool)
  671. >>> torch.all(a)
  672. tensor(False, dtype=torch.bool)
  673. >>> a = torch.arange(0, 3)
  674. >>> a
  675. tensor([0, 1, 2])
  676. >>> torch.all(a)
  677. tensor(False)
  678. .. function:: all(input, dim, keepdim=False, *, out=None) -> Tensor
  679. :noindex:
  680. For each row of :attr:`input` in the given dimension :attr:`dim`,
  681. returns `True` if all elements in the row evaluate to `True` and `False` otherwise.
  682. {keepdim_details}
  683. Args:
  684. {input}
  685. {opt_dim_all_reduce}
  686. {opt_keepdim}
  687. Keyword args:
  688. {out}
  689. Example::
  690. >>> a = torch.rand(4, 2).bool()
  691. >>> a
  692. tensor([[True, True],
  693. [True, False],
  694. [True, True],
  695. [True, True]], dtype=torch.bool)
  696. >>> torch.all(a, dim=1)
  697. tensor([ True, False, True, True], dtype=torch.bool)
  698. >>> torch.all(a, dim=0)
  699. tensor([ True, False], dtype=torch.bool)
  700. """.format(**multi_dim_common),
  701. )
  702. add_docstr(
  703. torch.any,
  704. r"""
  705. any(input: Tensor, *, out: Optional[Tensor]) -> Tensor
  706. Tests if any element in :attr:`input` evaluates to `True`.
  707. .. note:: This function matches the behaviour of NumPy in returning
  708. output of dtype `bool` for all supported dtypes except `uint8`.
  709. For `uint8` the dtype of output is `uint8` itself.
  710. Args:
  711. {input}
  712. Keyword args:
  713. {out}
  714. Example::
  715. >>> a = torch.rand(1, 2).bool()
  716. >>> a
  717. tensor([[False, True]], dtype=torch.bool)
  718. >>> torch.any(a)
  719. tensor(True, dtype=torch.bool)
  720. >>> a = torch.arange(0, 3)
  721. >>> a
  722. tensor([0, 1, 2])
  723. >>> torch.any(a)
  724. tensor(True)
  725. .. function:: any(input, dim, keepdim=False, *, out=None) -> Tensor
  726. :noindex:
  727. For each row of :attr:`input` in the given dimension :attr:`dim`,
  728. returns `True` if any element in the row evaluate to `True` and `False` otherwise.
  729. {keepdim_details}
  730. Args:
  731. {input}
  732. {opt_dim_all_reduce}
  733. {opt_keepdim}
  734. Keyword args:
  735. {out}
  736. Example::
  737. >>> a = torch.randn(4, 2) < 0
  738. >>> a
  739. tensor([[ True, True],
  740. [False, True],
  741. [ True, True],
  742. [False, False]])
  743. >>> torch.any(a, 1)
  744. tensor([ True, True, True, False])
  745. >>> torch.any(a, 0)
  746. tensor([True, True])
  747. """.format(**multi_dim_common),
  748. )
  749. add_docstr(
  750. torch.angle,
  751. r"""
  752. angle(input: Tensor, *, out: Optional[Tensor]) -> Tensor
  753. Computes the element-wise angle (in radians) of the given :attr:`input` tensor.
  754. .. math::
  755. \text{out}_{i} = angle(\text{input}_{i})
  756. """
  757. + r"""
  758. Args:
  759. {input}
  760. Keyword args:
  761. {out}
  762. .. note:: Starting in PyTorch 1.8, angle returns pi for negative real numbers,
  763. zero for non-negative real numbers, and propagates NaNs. Previously
  764. the function would return zero for all real numbers and not propagate
  765. floating-point NaNs.
  766. Example::
  767. >>> torch.angle(torch.tensor([-1 + 1j, -2 + 2j, 3 - 3j]))*180/3.14159
  768. tensor([ 135., 135, -45])
  769. """.format(**common_args),
  770. )
  771. add_docstr(
  772. torch.as_strided,
  773. r"""
  774. as_strided(input, size, stride, storage_offset=None) -> Tensor
  775. Create a view of an existing `torch.Tensor` :attr:`input` with specified
  776. :attr:`size`, :attr:`stride` and :attr:`storage_offset`.
  777. .. warning::
  778. Prefer using other view functions, like :meth:`torch.Tensor.view` or
  779. :meth:`torch.Tensor.expand`, to setting a view's strides manually with
  780. `as_strided`, as this function will throw an error on non-standard Pytorch
  781. backends (that do not have a concept of stride) and the result will depend
  782. on the current layout in memory. The constructed view must only refer to
  783. elements within the Tensor's storage or a runtime error will be thrown.
  784. If the generated view is "overlapped" (with multiple indices referring to
  785. the same element in memory), the behavior of inplace operations on this view
  786. is undefined (and might not throw runtime errors).
  787. Args:
  788. {input}
  789. size (tuple or ints): the shape of the output tensor
  790. stride (tuple or ints): the stride of the output tensor
  791. storage_offset (int, optional): the offset in the underlying storage of the output tensor.
  792. If ``None``, the storage_offset of the output tensor will match the input tensor.
  793. Example::
  794. >>> x = torch.randn(3, 3)
  795. >>> x
  796. tensor([[ 0.9039, 0.6291, 1.0795],
  797. [ 0.1586, 2.1939, -0.4900],
  798. [-0.1909, -0.7503, 1.9355]])
  799. >>> t = torch.as_strided(x, (2, 2), (1, 2))
  800. >>> t
  801. tensor([[0.9039, 1.0795],
  802. [0.6291, 0.1586]])
  803. >>> t = torch.as_strided(x, (2, 2), (1, 2), 1)
  804. tensor([[0.6291, 0.1586],
  805. [1.0795, 2.1939]])
  806. """.format(**common_args),
  807. )
  808. add_docstr(
  809. torch.as_tensor,
  810. r"""
  811. as_tensor(data: Any, dtype: Optional[dtype] = None, device: Optional[DeviceLikeType]) -> Tensor
  812. Converts :attr:`data` into a tensor, sharing data and preserving autograd
  813. history if possible.
  814. If :attr:`data` is already a tensor with the requested dtype and device
  815. then :attr:`data` itself is returned, but if :attr:`data` is a
  816. tensor with a different dtype or device then it's copied as if using
  817. `data.to(dtype=dtype, device=device)`.
  818. If :attr:`data` is a NumPy array (an ndarray) with the same dtype and device then a
  819. tensor is constructed using :func:`torch.from_numpy`.
  820. If :attr:`data` is a CuPy array, the returned tensor will be located on the same device as the CuPy array unless
  821. specifically overwritten by :attr:`device` or a default device. The device of the CuPy array is inferred from the
  822. pointer of the array using `cudaPointerGetAttributes` unless :attr:`device` is provided with an explicit device index.
  823. .. seealso::
  824. :func:`torch.tensor` never shares its data and creates a new "leaf tensor" (see :doc:`/notes/autograd`).
  825. Args:
  826. {data}
  827. {dtype}
  828. device (:class:`torch.device`, optional): the device of the constructed tensor. If None and data is a tensor
  829. then the device of data is used. If None and data is not a tensor then
  830. the result tensor is constructed on the current device.
  831. Example::
  832. >>> a = numpy.array([1, 2, 3])
  833. >>> t = torch.as_tensor(a)
  834. >>> t
  835. tensor([ 1, 2, 3])
  836. >>> t[0] = -1
  837. >>> a
  838. array([-1, 2, 3])
  839. >>> a = numpy.array([1, 2, 3])
  840. >>> t = torch.as_tensor(a, device=torch.device('cuda'))
  841. >>> t
  842. tensor([ 1, 2, 3])
  843. >>> t[0] = -1
  844. >>> a
  845. array([1, 2, 3])
  846. """.format(**factory_data_common_args),
  847. )
  848. add_docstr(
  849. torch.asin,
  850. r"""
  851. asin(input: Tensor, *, out: Optional[Tensor]) -> Tensor
  852. Returns a new tensor with the arcsine of the elements of :attr:`input`.
  853. .. math::
  854. \text{out}_{i} = \sin^{-1}(\text{input}_{i})
  855. """
  856. + r"""
  857. Args:
  858. {input}
  859. Keyword args:
  860. {out}
  861. Example::
  862. >>> a = torch.randn(4)
  863. >>> a
  864. tensor([-0.5962, 1.4985, -0.4396, 1.4525])
  865. >>> torch.asin(a)
  866. tensor([-0.6387, nan, -0.4552, nan])
  867. """.format(**common_args),
  868. )
  869. add_docstr(
  870. torch.arcsin,
  871. r"""
  872. arcsin(input: Tensor, *, out: Optional[Tensor]) -> Tensor
  873. Alias for :func:`torch.asin`.
  874. """,
  875. )
  876. add_docstr(
  877. torch.asinh,
  878. r"""
  879. asinh(input: Tensor, *, out: Optional[Tensor]) -> Tensor
  880. Returns a new tensor with the inverse hyperbolic sine of the elements of :attr:`input`.
  881. .. math::
  882. \text{out}_{i} = \sinh^{-1}(\text{input}_{i})
  883. """
  884. + r"""
  885. Args:
  886. {input}
  887. Keyword arguments:
  888. {out}
  889. Example::
  890. >>> a = torch.randn(4)
  891. >>> a
  892. tensor([ 0.1606, -1.4267, -1.0899, -1.0250 ])
  893. >>> torch.asinh(a)
  894. tensor([ 0.1599, -1.1534, -0.9435, -0.8990 ])
  895. """.format(**common_args),
  896. )
  897. add_docstr(
  898. torch.arcsinh,
  899. r"""
  900. arcsinh(input: Tensor, *, out: Optional[Tensor]) -> Tensor
  901. Alias for :func:`torch.asinh`.
  902. """,
  903. )
  904. add_docstr(
  905. torch.atan,
  906. r"""
  907. atan(input: Tensor, *, out: Optional[Tensor]) -> Tensor
  908. Returns a new tensor with the arctangent of the elements of :attr:`input`.
  909. .. math::
  910. \text{out}_{i} = \tan^{-1}(\text{input}_{i})
  911. """
  912. + r"""
  913. Args:
  914. {input}
  915. Keyword args:
  916. {out}
  917. Example::
  918. >>> a = torch.randn(4)
  919. >>> a
  920. tensor([ 0.2341, 0.2539, -0.6256, -0.6448])
  921. >>> torch.atan(a)
  922. tensor([ 0.2299, 0.2487, -0.5591, -0.5727])
  923. """.format(**common_args),
  924. )
  925. add_docstr(
  926. torch.arctan,
  927. r"""
  928. arctan(input: Tensor, *, out: Optional[Tensor]) -> Tensor
  929. Alias for :func:`torch.atan`.
  930. """,
  931. )
  932. add_docstr(
  933. torch.atan2,
  934. r"""
  935. atan2(input: Tensor, other: Tensor, *, out: Optional[Tensor]) -> Tensor
  936. Element-wise arctangent of :math:`\text{{input}}_{{i}} / \text{{other}}_{{i}}`
  937. with consideration of the quadrant. Returns a new tensor with the signed angles
  938. in radians between vector :math:`(\text{{other}}_{{i}}, \text{{input}}_{{i}})`
  939. and vector :math:`(1, 0)`. (Note that :math:`\text{{other}}_{{i}}`, the second
  940. parameter, is the x-coordinate, while :math:`\text{{input}}_{{i}}`, the first
  941. parameter, is the y-coordinate.)
  942. The shapes of ``input`` and ``other`` must be
  943. :ref:`broadcastable <broadcasting-semantics>`.
  944. Args:
  945. input (Tensor): the first input tensor
  946. other (Tensor): the second input tensor
  947. Keyword args:
  948. {out}
  949. Example::
  950. >>> a = torch.randn(4)
  951. >>> a
  952. tensor([ 0.9041, 0.0196, -0.3108, -2.4423])
  953. >>> torch.atan2(a, torch.randn(4))
  954. tensor([ 0.9833, 0.0811, -1.9743, -1.4151])
  955. """.format(**common_args),
  956. )
  957. add_docstr(
  958. torch.arctan2,
  959. r"""
  960. arctan2(input: Tensor, other: Tensor, *, out: Optional[Tensor]) -> Tensor
  961. Alias for :func:`torch.atan2`.
  962. """,
  963. )
  964. add_docstr(
  965. torch.atanh,
  966. r"""
  967. atanh(input: Tensor, *, out: Optional[Tensor]) -> Tensor
  968. Returns a new tensor with the inverse hyperbolic tangent of the elements of :attr:`input`.
  969. Note:
  970. The domain of the inverse hyperbolic tangent is `(-1, 1)` and values outside this range
  971. will be mapped to ``NaN``, except for the values `1` and `-1` for which the output is
  972. mapped to `+/-INF` respectively.
  973. .. math::
  974. \text{out}_{i} = \tanh^{-1}(\text{input}_{i})
  975. """
  976. + r"""
  977. Args:
  978. {input}
  979. Keyword arguments:
  980. {out}
  981. Example::
  982. >>> a = torch.randn(4).uniform_(-1, 1)
  983. >>> a
  984. tensor([ -0.9385, 0.2968, -0.8591, -0.1871 ])
  985. >>> torch.atanh(a)
  986. tensor([ -1.7253, 0.3060, -1.2899, -0.1893 ])
  987. """.format(**common_args),
  988. )
  989. add_docstr(
  990. torch.arctanh,
  991. r"""
  992. arctanh(input: Tensor, *, out: Optional[Tensor]) -> Tensor
  993. Alias for :func:`torch.atanh`.
  994. """,
  995. )
  996. add_docstr(
  997. torch.asarray,
  998. r"""
  999. asarray(obj: Any, *, dtype: Optional[dtype], device: Optional[DeviceLikeType], copy: Optional[bool] = None, requires_grad: bool = False) -> Tensor # noqa: B950
  1000. Converts :attr:`obj` to a tensor.
  1001. :attr:`obj` can be one of:
  1002. 1. a tensor
  1003. 2. a NumPy array or a NumPy scalar
  1004. 3. a DLPack capsule
  1005. 4. an object that implements Python's buffer protocol
  1006. 5. a scalar
  1007. 6. a sequence of scalars
  1008. When :attr:`obj` is a tensor, NumPy array, or DLPack capsule the returned tensor will,
  1009. by default, not require a gradient, have the same datatype as :attr:`obj`, be on the
  1010. same device, and share memory with it. These properties can be controlled with the
  1011. :attr:`dtype`, :attr:`device`, :attr:`copy`, and :attr:`requires_grad` keyword arguments.
  1012. If the returned tensor is of a different datatype, on a different device, or a copy is
  1013. requested then it will not share its memory with :attr:`obj`. If :attr:`requires_grad`
  1014. is ``True`` then the returned tensor will require a gradient, and if :attr:`obj` is
  1015. also a tensor with an autograd history then the returned tensor will have the same history.
  1016. When :attr:`obj` is not a tensor, NumPy array, or DLPack capsule but implements Python's
  1017. buffer protocol then the buffer is interpreted as an array of bytes grouped according to
  1018. the size of the datatype passed to the :attr:`dtype` keyword argument. (If no datatype is
  1019. passed then the default floating point datatype is used, instead.) The returned tensor
  1020. will have the specified datatype (or default floating point datatype if none is specified)
  1021. and, by default, be on the CPU device and share memory with the buffer.
  1022. When :attr:`obj` is a NumPy scalar, the returned tensor will be a 0-dimensional tensor on
  1023. the CPU and that doesn't share its memory (i.e. ``copy=True``). By default datatype will
  1024. be the PyTorch datatype corresponding to the NumPy's scalar's datatype.
  1025. When :attr:`obj` is none of the above but a scalar, or a sequence of scalars then the
  1026. returned tensor will, by default, infer its datatype from the scalar values, be on the
  1027. current default device, and not share its memory.
  1028. .. seealso::
  1029. :func:`torch.tensor` creates a tensor that always copies the data from the input object.
  1030. :func:`torch.from_numpy` creates a tensor that always shares memory from NumPy arrays.
  1031. :func:`torch.frombuffer` creates a tensor that always shares memory from objects that
  1032. implement the buffer protocol.
  1033. :func:`torch.from_dlpack` creates a tensor that always shares memory from
  1034. DLPack capsules.
  1035. Args:
  1036. obj (object): a tensor, NumPy array, DLPack Capsule, object that implements Python's
  1037. buffer protocol, scalar, or sequence of scalars.
  1038. Keyword args:
  1039. dtype (:class:`torch.dtype`, optional): the datatype of the returned tensor.
  1040. Default: ``None``, which causes the datatype of the returned tensor to be
  1041. inferred from :attr:`obj`.
  1042. copy (bool, optional): controls whether the returned tensor shares memory with :attr:`obj`.
  1043. Default: ``None``, which causes the returned tensor to share memory with :attr:`obj`
  1044. whenever possible. If ``True`` then the returned tensor does not share its memory.
  1045. If ``False`` then the returned tensor shares its memory with :attr:`obj` and an
  1046. error is thrown if it cannot.
  1047. device (:class:`torch.device`, optional): the device of the returned tensor.
  1048. Default: ``None``, which causes the device of :attr:`obj` to be used. Or, if
  1049. :attr:`obj` is a Python sequence, the current default device will be used.
  1050. requires_grad (bool, optional): whether the returned tensor requires grad.
  1051. Default: ``False``, which causes the returned tensor not to require a gradient.
  1052. If ``True``, then the returned tensor will require a gradient, and if :attr:`obj`
  1053. is also a tensor with an autograd history then the returned tensor will have
  1054. the same history.
  1055. Example::
  1056. >>> a = torch.tensor([1, 2, 3])
  1057. >>> # Shares memory with tensor 'a'
  1058. >>> b = torch.asarray(a)
  1059. >>> a.data_ptr() == b.data_ptr()
  1060. True
  1061. >>> # Forces memory copy
  1062. >>> c = torch.asarray(a, copy=True)
  1063. >>> a.data_ptr() == c.data_ptr()
  1064. False
  1065. >>> a = torch.tensor([1., 2., 3.], requires_grad=True)
  1066. >>> b = a + 2
  1067. >>> b
  1068. tensor([3., 4., 5.], grad_fn=<AddBackward0>)
  1069. >>> # Shares memory with tensor 'b', with no grad
  1070. >>> c = torch.asarray(b)
  1071. >>> c
  1072. tensor([3., 4., 5.])
  1073. >>> # Shares memory with tensor 'b', retaining autograd history
  1074. >>> d = torch.asarray(b, requires_grad=True)
  1075. >>> d
  1076. tensor([3., 4., 5.], grad_fn=<AddBackward0>)
  1077. >>> array = numpy.array([1, 2, 3])
  1078. >>> # Shares memory with array 'array'
  1079. >>> t1 = torch.asarray(array)
  1080. >>> array.__array_interface__['data'][0] == t1.data_ptr()
  1081. True
  1082. >>> # Copies memory due to dtype mismatch
  1083. >>> t2 = torch.asarray(array, dtype=torch.float32)
  1084. >>> array.__array_interface__['data'][0] == t2.data_ptr()
  1085. False
  1086. >>> scalar = numpy.float64(0.5)
  1087. >>> torch.asarray(scalar)
  1088. tensor(0.5000, dtype=torch.float64)
  1089. """,
  1090. )
  1091. add_docstr(
  1092. torch.baddbmm,
  1093. r"""
  1094. baddbmm(input, batch1, batch2, out_dtype=None, *, beta=1, alpha=1, out=None) -> Tensor
  1095. Performs a batch matrix-matrix product of matrices in :attr:`batch1`
  1096. and :attr:`batch2`.
  1097. :attr:`input` is added to the final result.
  1098. :attr:`batch1` and :attr:`batch2` must be 3-D tensors each containing the same
  1099. number of matrices.
  1100. If :attr:`batch1` is a :math:`(b \times n \times m)` tensor, :attr:`batch2` is a
  1101. :math:`(b \times m \times p)` tensor, then :attr:`input` must be
  1102. :ref:`broadcastable <broadcasting-semantics>` with a
  1103. :math:`(b \times n \times p)` tensor and :attr:`out` will be a
  1104. :math:`(b \times n \times p)` tensor. Both :attr:`alpha` and :attr:`beta` mean the
  1105. same as the scaling factors used in :meth:`torch.addbmm`.
  1106. .. math::
  1107. \text{out}_i = \beta\ \text{input}_i + \alpha\ (\text{batch1}_i \mathbin{@} \text{batch2}_i)
  1108. If :attr:`beta` is 0, then the content of :attr:`input` will be ignored, and `nan` and `inf` in
  1109. it will not be propagated.
  1110. """
  1111. + r"""
  1112. For inputs of type `FloatTensor` or `DoubleTensor`, arguments :attr:`beta` and
  1113. :attr:`alpha` must be real numbers, otherwise they should be integers.
  1114. {tf32_note}
  1115. {rocm_fp16_note}
  1116. Args:
  1117. input (Tensor): the tensor to be added
  1118. batch1 (Tensor): the first batch of matrices to be multiplied
  1119. batch2 (Tensor): the second batch of matrices to be multiplied
  1120. out_dtype (dtype, optional): the dtype of the output tensor,
  1121. Supported only on CUDA and for torch.float32 given
  1122. torch.float16/torch.bfloat16 input dtypes
  1123. Keyword args:
  1124. beta (Number, optional): multiplier for :attr:`input` (:math:`\beta`)
  1125. alpha (Number, optional): multiplier for :math:`\text{{batch1}} \mathbin{{@}} \text{{batch2}}` (:math:`\alpha`)
  1126. {out}
  1127. Example::
  1128. >>> M = torch.randn(10, 3, 5)
  1129. >>> batch1 = torch.randn(10, 3, 4)
  1130. >>> batch2 = torch.randn(10, 4, 5)
  1131. >>> torch.baddbmm(M, batch1, batch2).size()
  1132. torch.Size([10, 3, 5])
  1133. """.format(**common_args, **tf32_notes, **rocm_fp16_notes),
  1134. )
  1135. add_docstr(
  1136. torch.bernoulli,
  1137. r"""
  1138. bernoulli(input: Tensor, *, generator: Optional[Generator], out: Optional[Tensor]) -> Tensor
  1139. Draws binary random numbers (0 or 1) from a Bernoulli distribution.
  1140. The :attr:`input` tensor should be a tensor containing probabilities
  1141. to be used for drawing the binary random number.
  1142. Hence, all values in :attr:`input` have to be in the range:
  1143. :math:`0 \leq \text{input}_i \leq 1`.
  1144. The :math:`\text{i}^{th}` element of the output tensor will draw a
  1145. value :math:`1` according to the :math:`\text{i}^{th}` probability value given
  1146. in :attr:`input`.
  1147. .. math::
  1148. \text{out}_{i} \sim \mathrm{Bernoulli}(p = \text{input}_{i})
  1149. """
  1150. + r"""
  1151. The returned :attr:`out` tensor only has values 0 or 1 and is of the same
  1152. shape as :attr:`input`.
  1153. :attr:`out` can have integral ``dtype``, but :attr:`input` must have floating
  1154. point ``dtype``.
  1155. Args:
  1156. input (Tensor): the input tensor of probability values for the Bernoulli distribution
  1157. Keyword args:
  1158. {generator}
  1159. {out}
  1160. Example::
  1161. >>> a = torch.empty(3, 3).uniform_(0, 1) # generate a uniform random matrix with range [0, 1]
  1162. >>> a
  1163. tensor([[ 0.1737, 0.0950, 0.3609],
  1164. [ 0.7148, 0.0289, 0.2676],
  1165. [ 0.9456, 0.8937, 0.7202]])
  1166. >>> torch.bernoulli(a)
  1167. tensor([[ 1., 0., 0.],
  1168. [ 0., 0., 0.],
  1169. [ 1., 1., 1.]])
  1170. >>> a = torch.ones(3, 3) # probability of drawing "1" is 1
  1171. >>> torch.bernoulli(a)
  1172. tensor([[ 1., 1., 1.],
  1173. [ 1., 1., 1.],
  1174. [ 1., 1., 1.]])
  1175. >>> a = torch.zeros(3, 3) # probability of drawing "1" is 0
  1176. >>> torch.bernoulli(a)
  1177. tensor([[ 0., 0., 0.],
  1178. [ 0., 0., 0.],
  1179. [ 0., 0., 0.]])
  1180. """.format(**common_args),
  1181. )
  1182. add_docstr(
  1183. torch.bincount,
  1184. r"""
  1185. bincount(input, weights=None, minlength=0) -> Tensor
  1186. Count the frequency of each value in an array of non-negative ints.
  1187. The number of bins (size 1) is one larger than the largest value in
  1188. :attr:`input` unless :attr:`input` is empty, in which case the result is a
  1189. tensor of size 0. If :attr:`minlength` is specified, the number of bins is at least
  1190. :attr:`minlength` and if :attr:`input` is empty, then the result is tensor of size
  1191. :attr:`minlength` filled with zeros. If ``n`` is the value at position ``i``,
  1192. ``out[n] += weights[i]`` if :attr:`weights` is specified else
  1193. ``out[n] += 1``.
  1194. Note:
  1195. {backward_reproducibility_note}
  1196. Arguments:
  1197. input (Tensor): 1-d int tensor
  1198. weights (Tensor): optional, weight for each value in the input tensor.
  1199. Should be of same size as input tensor.
  1200. minlength (int): optional, minimum number of bins. Should be non-negative.
  1201. Returns:
  1202. output (Tensor): a tensor of shape ``Size([max(input) + 1])`` if
  1203. :attr:`input` is non-empty, else ``Size(0)``
  1204. Example::
  1205. >>> input = torch.randint(0, 8, (5,), dtype=torch.int64)
  1206. >>> weights = torch.linspace(0, 1, steps=5)
  1207. >>> input, weights
  1208. (tensor([4, 3, 6, 3, 4]),
  1209. tensor([ 0.0000, 0.2500, 0.5000, 0.7500, 1.0000])
  1210. >>> torch.bincount(input)
  1211. tensor([0, 0, 0, 2, 2, 0, 1])
  1212. >>> input.bincount(weights)
  1213. tensor([0.0000, 0.0000, 0.0000, 1.0000, 1.0000, 0.0000, 0.5000])
  1214. """.format(**reproducibility_notes),
  1215. )
  1216. add_docstr(
  1217. torch.bitwise_not,
  1218. r"""
  1219. bitwise_not(input, *, out=None) -> Tensor
  1220. Computes the bitwise NOT of the given input tensor. The input tensor must be of
  1221. integral or Boolean types. For bool tensors, it computes the logical NOT.
  1222. Args:
  1223. {input}
  1224. Keyword args:
  1225. {out}
  1226. Example::
  1227. >>> torch.bitwise_not(torch.tensor([-1, -2, 3], dtype=torch.int8))
  1228. tensor([ 0, 1, -4], dtype=torch.int8)
  1229. """.format(**common_args),
  1230. )
  1231. add_docstr(
  1232. torch.bmm,
  1233. r"""
  1234. bmm(input, mat2, out_dtype=None, *, out=None) -> Tensor
  1235. Performs a batch matrix-matrix product of matrices stored in :attr:`input`
  1236. and :attr:`mat2`.
  1237. :attr:`input` and :attr:`mat2` must be 3-D tensors each containing
  1238. the same number of matrices.
  1239. If :attr:`input` is a :math:`(b \times n \times m)` tensor, :attr:`mat2` is a
  1240. :math:`(b \times m \times p)` tensor, :attr:`out` will be a
  1241. :math:`(b \times n \times p)` tensor.
  1242. .. math::
  1243. \text{out}_i = \text{input}_i \mathbin{@} \text{mat2}_i
  1244. """
  1245. + r"""
  1246. {tf32_note}
  1247. {rocm_fp16_note}
  1248. .. note:: This function does not :ref:`broadcast <broadcasting-semantics>`.
  1249. For broadcasting matrix products, see :func:`torch.matmul`.
  1250. Args:
  1251. input (Tensor): the first batch of matrices to be multiplied
  1252. mat2 (Tensor): the second batch of matrices to be multiplied
  1253. out_dtype (dtype, optional): the dtype of the output tensor,
  1254. Supported only on CUDA and for torch.float32 given
  1255. torch.float16/torch.bfloat16 input dtypes
  1256. Keyword Args:
  1257. {out}
  1258. Example::
  1259. >>> input = torch.randn(10, 3, 4)
  1260. >>> mat2 = torch.randn(10, 4, 5)
  1261. >>> res = torch.bmm(input, mat2)
  1262. >>> res.size()
  1263. torch.Size([10, 3, 5])
  1264. """.format(**common_args, **tf32_notes, **rocm_fp16_notes),
  1265. )
  1266. add_docstr(
  1267. torch.bitwise_and,
  1268. r"""
  1269. bitwise_and(input, other, *, out=None) -> Tensor
  1270. Computes the bitwise AND of :attr:`input` and :attr:`other`. The input tensor must be of
  1271. integral or Boolean types. For bool tensors, it computes the logical AND.
  1272. Args:
  1273. input: the first input tensor
  1274. other: the second input tensor
  1275. Keyword args:
  1276. {out}
  1277. Example::
  1278. >>> torch.bitwise_and(torch.tensor([-1, -2, 3], dtype=torch.int8), torch.tensor([1, 0, 3], dtype=torch.int8))
  1279. tensor([1, 0, 3], dtype=torch.int8)
  1280. >>> torch.bitwise_and(torch.tensor([True, True, False]), torch.tensor([False, True, False]))
  1281. tensor([ False, True, False])
  1282. """.format(**common_args),
  1283. )
  1284. add_docstr(
  1285. torch.bitwise_or,
  1286. r"""
  1287. bitwise_or(input: Tensor, other: Tensor, *, out: Optional[Tensor]) -> Tensor
  1288. Computes the bitwise OR of :attr:`input` and :attr:`other`. The input tensor must be of
  1289. integral or Boolean types. For bool tensors, it computes the logical OR.
  1290. Args:
  1291. input: the first input tensor
  1292. other: the second input tensor
  1293. Keyword args:
  1294. {out}
  1295. Example::
  1296. >>> torch.bitwise_or(torch.tensor([-1, -2, 3], dtype=torch.int8), torch.tensor([1, 0, 3], dtype=torch.int8))
  1297. tensor([-1, -2, 3], dtype=torch.int8)
  1298. >>> torch.bitwise_or(torch.tensor([True, True, False]), torch.tensor([False, True, False]))
  1299. tensor([ True, True, False])
  1300. """.format(**common_args),
  1301. )
  1302. add_docstr(
  1303. torch.bitwise_xor,
  1304. r"""
  1305. bitwise_xor(input, other, *, out=None) -> Tensor
  1306. Computes the bitwise XOR of :attr:`input` and :attr:`other`. The input tensor must be of
  1307. integral or Boolean types. For bool tensors, it computes the logical XOR.
  1308. Args:
  1309. input: the first input tensor
  1310. other: the second input tensor
  1311. Keyword args:
  1312. {out}
  1313. Example::
  1314. >>> torch.bitwise_xor(torch.tensor([-1, -2, 3], dtype=torch.int8), torch.tensor([1, 0, 3], dtype=torch.int8))
  1315. tensor([-2, -2, 0], dtype=torch.int8)
  1316. >>> torch.bitwise_xor(torch.tensor([True, True, False]), torch.tensor([False, True, False]))
  1317. tensor([ True, False, False])
  1318. """.format(**common_args),
  1319. )
  1320. add_docstr(
  1321. torch.bitwise_left_shift,
  1322. r"""
  1323. bitwise_left_shift(input, other, *, out=None) -> Tensor
  1324. Computes the left arithmetic shift of :attr:`input` by :attr:`other` bits.
  1325. The input tensor must be of integral type. This operator supports
  1326. :ref:`broadcasting to a common shape <broadcasting-semantics>` and
  1327. :ref:`type promotion <type-promotion-doc>`.
  1328. The operation applied is:
  1329. .. math::
  1330. \text{{out}}_i = \text{{input}}_i << \text{{other}}_i
  1331. Args:
  1332. input (Tensor or Scalar): the first input tensor
  1333. other (Tensor or Scalar): the second input tensor
  1334. Keyword args:
  1335. {out}
  1336. Example::
  1337. >>> torch.bitwise_left_shift(torch.tensor([-1, -2, 3], dtype=torch.int8), torch.tensor([1, 0, 3], dtype=torch.int8))
  1338. tensor([-2, -2, 24], dtype=torch.int8)
  1339. """.format(**common_args),
  1340. )
  1341. add_docstr(
  1342. torch.bitwise_right_shift,
  1343. r"""
  1344. bitwise_right_shift(input, other, *, out=None) -> Tensor
  1345. Computes the right arithmetic shift of :attr:`input` by :attr:`other` bits.
  1346. The input tensor must be of integral type. This operator supports
  1347. :ref:`broadcasting to a common shape <broadcasting-semantics>` and
  1348. :ref:`type promotion <type-promotion-doc>`.
  1349. In any case, if the value of the right operand is negative or is greater
  1350. or equal to the number of bits in the promoted left operand, the behavior is undefined.
  1351. The operation applied is:
  1352. .. math::
  1353. \text{{out}}_i = \text{{input}}_i >> \text{{other}}_i
  1354. Args:
  1355. input (Tensor or Scalar): the first input tensor
  1356. other (Tensor or Scalar): the second input tensor
  1357. Keyword args:
  1358. {out}
  1359. Example::
  1360. >>> torch.bitwise_right_shift(torch.tensor([-2, -7, 31], dtype=torch.int8), torch.tensor([1, 0, 3], dtype=torch.int8))
  1361. tensor([-1, -7, 3], dtype=torch.int8)
  1362. """.format(**common_args),
  1363. )
  1364. add_docstr(
  1365. torch.broadcast_to,
  1366. r"""
  1367. broadcast_to(input, shape) -> Tensor
  1368. Broadcasts :attr:`input` to the shape :attr:`\shape`.
  1369. Equivalent to calling ``input.expand(shape)``. See :meth:`~Tensor.expand` for details.
  1370. Args:
  1371. {input}
  1372. shape (list, tuple, or :class:`torch.Size`): the new shape.
  1373. Example::
  1374. >>> x = torch.tensor([1, 2, 3])
  1375. >>> torch.broadcast_to(x, (3, 3))
  1376. tensor([[1, 2, 3],
  1377. [1, 2, 3],
  1378. [1, 2, 3]])
  1379. """.format(**common_args),
  1380. )
  1381. add_docstr(
  1382. torch.stack,
  1383. r"""
  1384. stack(tensors, dim=0, *, out=None) -> Tensor
  1385. Concatenates a sequence of tensors along a new dimension.
  1386. All tensors need to be of the same size.
  1387. .. seealso::
  1388. :func:`torch.cat` concatenates the given sequence along an existing dimension.
  1389. Arguments:
  1390. tensors (sequence of Tensors): sequence of tensors to concatenate
  1391. dim (int, optional): dimension to insert. Has to be between 0 and the number
  1392. of dimensions of concatenated tensors (inclusive). Default: 0
  1393. Keyword args:
  1394. {out}
  1395. Example::
  1396. >>> x = torch.randn(2, 3)
  1397. >>> x
  1398. tensor([[ 0.3367, 0.1288, 0.2345],
  1399. [ 0.2303, -1.1229, -0.1863]])
  1400. >>> torch.stack((x, x)) # same as torch.stack((x, x), dim=0)
  1401. tensor([[[ 0.3367, 0.1288, 0.2345],
  1402. [ 0.2303, -1.1229, -0.1863]],
  1403. [[ 0.3367, 0.1288, 0.2345],
  1404. [ 0.2303, -1.1229, -0.1863]]])
  1405. >>> torch.stack((x, x)).size()
  1406. torch.Size([2, 2, 3])
  1407. >>> torch.stack((x, x), dim=1)
  1408. tensor([[[ 0.3367, 0.1288, 0.2345],
  1409. [ 0.3367, 0.1288, 0.2345]],
  1410. [[ 0.2303, -1.1229, -0.1863],
  1411. [ 0.2303, -1.1229, -0.1863]]])
  1412. >>> torch.stack((x, x), dim=2)
  1413. tensor([[[ 0.3367, 0.3367],
  1414. [ 0.1288, 0.1288],
  1415. [ 0.2345, 0.2345]],
  1416. [[ 0.2303, 0.2303],
  1417. [-1.1229, -1.1229],
  1418. [-0.1863, -0.1863]]])
  1419. >>> torch.stack((x, x), dim=-1)
  1420. tensor([[[ 0.3367, 0.3367],
  1421. [ 0.1288, 0.1288],
  1422. [ 0.2345, 0.2345]],
  1423. [[ 0.2303, 0.2303],
  1424. [-1.1229, -1.1229],
  1425. [-0.1863, -0.1863]]])
  1426. """.format(**common_args),
  1427. )
  1428. add_docstr(
  1429. torch.hstack,
  1430. r"""
  1431. hstack(tensors, *, out=None) -> Tensor
  1432. Stack tensors in sequence horizontally (column wise).
  1433. This is equivalent to concatenation along the first axis for 1-D tensors, and along the second axis for all other tensors.
  1434. Args:
  1435. tensors (sequence of Tensors): sequence of tensors to concatenate
  1436. Keyword args:
  1437. {out}
  1438. Example::
  1439. >>> a = torch.tensor([1, 2, 3])
  1440. >>> b = torch.tensor([4, 5, 6])
  1441. >>> torch.hstack((a,b))
  1442. tensor([1, 2, 3, 4, 5, 6])
  1443. >>> a = torch.tensor([[1],[2],[3]])
  1444. >>> b = torch.tensor([[4],[5],[6]])
  1445. >>> torch.hstack((a,b))
  1446. tensor([[1, 4],
  1447. [2, 5],
  1448. [3, 6]])
  1449. """.format(**common_args),
  1450. )
  1451. add_docstr(
  1452. torch.vstack,
  1453. r"""
  1454. vstack(tensors, *, out=None) -> Tensor
  1455. Stack tensors in sequence vertically (row wise).
  1456. This is equivalent to concatenation along the first axis after all 1-D tensors have been reshaped by :func:`torch.atleast_2d`.
  1457. Args:
  1458. tensors (sequence of Tensors): sequence of tensors to concatenate
  1459. Keyword args:
  1460. {out}
  1461. Example::
  1462. >>> a = torch.tensor([1, 2, 3])
  1463. >>> b = torch.tensor([4, 5, 6])
  1464. >>> torch.vstack((a,b))
  1465. tensor([[1, 2, 3],
  1466. [4, 5, 6]])
  1467. >>> a = torch.tensor([[1],[2],[3]])
  1468. >>> b = torch.tensor([[4],[5],[6]])
  1469. >>> torch.vstack((a,b))
  1470. tensor([[1],
  1471. [2],
  1472. [3],
  1473. [4],
  1474. [5],
  1475. [6]])
  1476. """.format(**common_args),
  1477. )
  1478. add_docstr(
  1479. torch.dstack,
  1480. r"""
  1481. dstack(tensors, *, out=None) -> Tensor
  1482. Stack tensors in sequence depthwise (along third axis).
  1483. This is equivalent to concatenation along the third axis after 1-D and 2-D tensors have been reshaped by :func:`torch.atleast_3d`.
  1484. Args:
  1485. tensors (sequence of Tensors): sequence of tensors to concatenate
  1486. Keyword args:
  1487. {out}
  1488. Example::
  1489. >>> a = torch.tensor([1, 2, 3])
  1490. >>> b = torch.tensor([4, 5, 6])
  1491. >>> torch.dstack((a,b))
  1492. tensor([[[1, 4],
  1493. [2, 5],
  1494. [3, 6]]])
  1495. >>> a = torch.tensor([[1],[2],[3]])
  1496. >>> b = torch.tensor([[4],[5],[6]])
  1497. >>> torch.dstack((a,b))
  1498. tensor([[[1, 4]],
  1499. [[2, 5]],
  1500. [[3, 6]]])
  1501. """.format(**common_args),
  1502. )
  1503. add_docstr(
  1504. torch.tensor_split,
  1505. r"""
  1506. tensor_split(input, indices_or_sections, dim=0) -> List of Tensors
  1507. Splits a tensor into multiple sub-tensors, all of which are views of :attr:`input`,
  1508. along dimension :attr:`dim` according to the indices or number of sections specified
  1509. by :attr:`indices_or_sections`. This function is based on NumPy's
  1510. :func:`numpy.array_split`.
  1511. Args:
  1512. input (Tensor): the tensor to split
  1513. indices_or_sections (Tensor, int or list or tuple of ints):
  1514. If :attr:`indices_or_sections` is an integer ``n`` or a zero dimensional long tensor
  1515. with value ``n``, :attr:`input` is split into ``n`` sections along dimension :attr:`dim`.
  1516. If :attr:`input` is divisible by ``n`` along dimension :attr:`dim`, each
  1517. section will be of equal size, :code:`input.size(dim) / n`. If :attr:`input`
  1518. is not divisible by ``n``, the sizes of the first :code:`int(input.size(dim) % n)`
  1519. sections will have size :code:`int(input.size(dim) / n) + 1`, and the rest will
  1520. have size :code:`int(input.size(dim) / n)`.
  1521. If :attr:`indices_or_sections` is a list or tuple of ints, or a one-dimensional long
  1522. tensor, then :attr:`input` is split along dimension :attr:`dim` at each of the indices
  1523. in the list, tuple or tensor. For instance, :code:`indices_or_sections=[2, 3]` and :code:`dim=0`
  1524. would result in the tensors :code:`input[:2]`, :code:`input[2:3]`, and :code:`input[3:]`.
  1525. If :attr:`indices_or_sections` is a tensor, it must be a zero-dimensional or one-dimensional
  1526. long tensor on the CPU.
  1527. dim (int, optional): dimension along which to split the tensor. Default: ``0``
  1528. Example::
  1529. >>> x = torch.arange(8)
  1530. >>> torch.tensor_split(x, 3)
  1531. (tensor([0, 1, 2]), tensor([3, 4, 5]), tensor([6, 7]))
  1532. >>> x = torch.arange(7)
  1533. >>> torch.tensor_split(x, 3)
  1534. (tensor([0, 1, 2]), tensor([3, 4]), tensor([5, 6]))
  1535. >>> torch.tensor_split(x, (1, 6))
  1536. (tensor([0]), tensor([1, 2, 3, 4, 5]), tensor([6]))
  1537. >>> x = torch.arange(14).reshape(2, 7)
  1538. >>> x
  1539. tensor([[ 0, 1, 2, 3, 4, 5, 6],
  1540. [ 7, 8, 9, 10, 11, 12, 13]])
  1541. >>> torch.tensor_split(x, 3, dim=1)
  1542. (tensor([[0, 1, 2],
  1543. [7, 8, 9]]),
  1544. tensor([[ 3, 4],
  1545. [10, 11]]),
  1546. tensor([[ 5, 6],
  1547. [12, 13]]))
  1548. >>> torch.tensor_split(x, (1, 6), dim=1)
  1549. (tensor([[0],
  1550. [7]]),
  1551. tensor([[ 1, 2, 3, 4, 5],
  1552. [ 8, 9, 10, 11, 12]]),
  1553. tensor([[ 6],
  1554. [13]]))
  1555. """,
  1556. )
  1557. add_docstr(
  1558. torch.chunk,
  1559. r"""
  1560. chunk(input: Tensor, chunks: int, dim: int = 0) -> Tuple[Tensor, ...]
  1561. Attempts to split a tensor into the specified number of chunks. Each chunk is a view of
  1562. the input tensor.
  1563. .. note::
  1564. This function may return fewer than the specified number of chunks!
  1565. .. seealso::
  1566. :func:`torch.tensor_split` a function that always returns exactly the specified number of chunks
  1567. If the tensor size along the given dimension :attr:`dim` is divisible by :attr:`chunks`,
  1568. all returned chunks will be the same size.
  1569. If the tensor size along the given dimension :attr:`dim` is not divisible by :attr:`chunks`,
  1570. all returned chunks will be the same size, except the last one.
  1571. If such division is not possible, this function may return fewer
  1572. than the specified number of chunks.
  1573. Arguments:
  1574. input (Tensor): the tensor to split
  1575. chunks (int): number of chunks to return
  1576. dim (int): dimension along which to split the tensor
  1577. Example:
  1578. >>> torch.arange(11).chunk(6)
  1579. (tensor([0, 1]),
  1580. tensor([2, 3]),
  1581. tensor([4, 5]),
  1582. tensor([6, 7]),
  1583. tensor([8, 9]),
  1584. tensor([10]))
  1585. >>> torch.arange(12).chunk(6)
  1586. (tensor([0, 1]),
  1587. tensor([2, 3]),
  1588. tensor([4, 5]),
  1589. tensor([6, 7]),
  1590. tensor([8, 9]),
  1591. tensor([10, 11]))
  1592. >>> torch.arange(13).chunk(6)
  1593. (tensor([0, 1, 2]),
  1594. tensor([3, 4, 5]),
  1595. tensor([6, 7, 8]),
  1596. tensor([ 9, 10, 11]),
  1597. tensor([12]))
  1598. """,
  1599. )
  1600. add_docstr(
  1601. torch.unsafe_chunk,
  1602. r"""
  1603. unsafe_chunk(input, chunks, dim=0) -> List of Tensors
  1604. Works like :func:`torch.chunk` but without enforcing the autograd restrictions
  1605. on inplace modification of the outputs.
  1606. .. warning::
  1607. This function is safe to use as long as only the input, or only the outputs
  1608. are modified inplace after calling this function. It is user's
  1609. responsibility to ensure that is the case. If both the input and one or more
  1610. of the outputs are modified inplace, gradients computed by autograd will be
  1611. silently incorrect.
  1612. """,
  1613. )
  1614. add_docstr(
  1615. torch.unsafe_split,
  1616. r"""
  1617. unsafe_split(tensor, split_size_or_sections, dim=0) -> List of Tensors
  1618. Works like :func:`torch.split` but without enforcing the autograd restrictions
  1619. on inplace modification of the outputs.
  1620. .. warning::
  1621. This function is safe to use as long as only the input, or only the outputs
  1622. are modified inplace after calling this function. It is user's
  1623. responsibility to ensure that is the case. If both the input and one or more
  1624. of the outputs are modified inplace, gradients computed by autograd will be
  1625. silently incorrect.
  1626. """,
  1627. )
  1628. add_docstr(
  1629. torch.hsplit,
  1630. r"""
  1631. hsplit(input, indices_or_sections) -> List of Tensors
  1632. Splits :attr:`input`, a tensor with one or more dimensions, into multiple tensors
  1633. horizontally according to :attr:`indices_or_sections`. Each split is a view of
  1634. :attr:`input`.
  1635. If :attr:`input` is one dimensional this is equivalent to calling
  1636. torch.tensor_split(input, indices_or_sections, dim=0) (the split dimension is
  1637. zero), and if :attr:`input` has two or more dimensions it's equivalent to calling
  1638. torch.tensor_split(input, indices_or_sections, dim=1) (the split dimension is 1),
  1639. except that if :attr:`indices_or_sections` is an integer it must evenly divide
  1640. the split dimension or a runtime error will be thrown.
  1641. This function is based on NumPy's :func:`numpy.hsplit`.
  1642. Args:
  1643. input (Tensor): tensor to split.
  1644. indices_or_sections (int or list or tuple of ints): See argument in :func:`torch.tensor_split`.
  1645. Example::
  1646. >>> t = torch.arange(16.0).reshape(4,4)
  1647. >>> t
  1648. tensor([[ 0., 1., 2., 3.],
  1649. [ 4., 5., 6., 7.],
  1650. [ 8., 9., 10., 11.],
  1651. [12., 13., 14., 15.]])
  1652. >>> torch.hsplit(t, 2)
  1653. (tensor([[ 0., 1.],
  1654. [ 4., 5.],
  1655. [ 8., 9.],
  1656. [12., 13.]]),
  1657. tensor([[ 2., 3.],
  1658. [ 6., 7.],
  1659. [10., 11.],
  1660. [14., 15.]]))
  1661. >>> torch.hsplit(t, [3, 6])
  1662. (tensor([[ 0., 1., 2.],
  1663. [ 4., 5., 6.],
  1664. [ 8., 9., 10.],
  1665. [12., 13., 14.]]),
  1666. tensor([[ 3.],
  1667. [ 7.],
  1668. [11.],
  1669. [15.]]),
  1670. tensor([], size=(4, 0)))
  1671. """,
  1672. )
  1673. add_docstr(
  1674. torch.vsplit,
  1675. r"""
  1676. vsplit(input, indices_or_sections) -> List of Tensors
  1677. Splits :attr:`input`, a tensor with two or more dimensions, into multiple tensors
  1678. vertically according to :attr:`indices_or_sections`. Each split is a view of
  1679. :attr:`input`.
  1680. This is equivalent to calling torch.tensor_split(input, indices_or_sections, dim=0)
  1681. (the split dimension is 0), except that if :attr:`indices_or_sections` is an integer
  1682. it must evenly divide the split dimension or a runtime error will be thrown.
  1683. This function is based on NumPy's :func:`numpy.vsplit`.
  1684. Args:
  1685. input (Tensor): tensor to split.
  1686. indices_or_sections (int or list or tuple of ints): See argument in :func:`torch.tensor_split`.
  1687. Example::
  1688. >>> t = torch.arange(16.0).reshape(4,4)
  1689. >>> t
  1690. tensor([[ 0., 1., 2., 3.],
  1691. [ 4., 5., 6., 7.],
  1692. [ 8., 9., 10., 11.],
  1693. [12., 13., 14., 15.]])
  1694. >>> torch.vsplit(t, 2)
  1695. (tensor([[0., 1., 2., 3.],
  1696. [4., 5., 6., 7.]]),
  1697. tensor([[ 8., 9., 10., 11.],
  1698. [12., 13., 14., 15.]]))
  1699. >>> torch.vsplit(t, [3, 6])
  1700. (tensor([[ 0., 1., 2., 3.],
  1701. [ 4., 5., 6., 7.],
  1702. [ 8., 9., 10., 11.]]),
  1703. tensor([[12., 13., 14., 15.]]),
  1704. tensor([], size=(0, 4)))
  1705. """,
  1706. )
  1707. add_docstr(
  1708. torch.dsplit,
  1709. r"""
  1710. dsplit(input, indices_or_sections) -> List of Tensors
  1711. Splits :attr:`input`, a tensor with three or more dimensions, into multiple tensors
  1712. depthwise according to :attr:`indices_or_sections`. Each split is a view of
  1713. :attr:`input`.
  1714. This is equivalent to calling torch.tensor_split(input, indices_or_sections, dim=2)
  1715. (the split dimension is 2), except that if :attr:`indices_or_sections` is an integer
  1716. it must evenly divide the split dimension or a runtime error will be thrown.
  1717. This function is based on NumPy's :func:`numpy.dsplit`.
  1718. Args:
  1719. input (Tensor): tensor to split.
  1720. indices_or_sections (int or list or tuple of ints): See argument in :func:`torch.tensor_split`.
  1721. Example::
  1722. >>> t = torch.arange(16.0).reshape(2, 2, 4)
  1723. >>> t
  1724. tensor([[[ 0., 1., 2., 3.],
  1725. [ 4., 5., 6., 7.]],
  1726. [[ 8., 9., 10., 11.],
  1727. [12., 13., 14., 15.]]])
  1728. >>> torch.dsplit(t, 2)
  1729. (tensor([[[ 0., 1.],
  1730. [ 4., 5.]],
  1731. [[ 8., 9.],
  1732. [12., 13.]]]),
  1733. tensor([[[ 2., 3.],
  1734. [ 6., 7.]],
  1735. [[10., 11.],
  1736. [14., 15.]]]))
  1737. >>> torch.dsplit(t, [3, 6])
  1738. (tensor([[[ 0., 1., 2.],
  1739. [ 4., 5., 6.]],
  1740. [[ 8., 9., 10.],
  1741. [12., 13., 14.]]]),
  1742. tensor([[[ 3.],
  1743. [ 7.]],
  1744. [[11.],
  1745. [15.]]]),
  1746. tensor([], size=(2, 2, 0)))
  1747. """,
  1748. )
  1749. add_docstr(
  1750. torch.can_cast,
  1751. r"""
  1752. can_cast(from_, to) -> bool
  1753. Determines if a type conversion is allowed under PyTorch casting rules
  1754. described in the type promotion :ref:`documentation <type-promotion-doc>`.
  1755. Args:
  1756. from\_ (dtype): The original :class:`torch.dtype`.
  1757. to (dtype): The target :class:`torch.dtype`.
  1758. Example::
  1759. >>> torch.can_cast(torch.double, torch.float)
  1760. True
  1761. >>> torch.can_cast(torch.float, torch.int)
  1762. False
  1763. """,
  1764. )
  1765. add_docstr(
  1766. torch.corrcoef,
  1767. r"""
  1768. corrcoef(input) -> Tensor
  1769. Estimates the Pearson product-moment correlation coefficient matrix of the variables given by the :attr:`input` matrix,
  1770. where rows are the variables and columns are the observations.
  1771. .. note::
  1772. The correlation coefficient matrix R is computed using the covariance matrix C as given by
  1773. :math:`R_{ij} = \frac{ C_{ij} } { \sqrt{ C_{ii} * C_{jj} } }`
  1774. .. note::
  1775. Due to floating point rounding, the resulting array may not be Hermitian and its diagonal elements may not be 1.
  1776. The real and imaginary values are clipped to the interval [-1, 1] in an attempt to improve this situation.
  1777. Args:
  1778. input (Tensor): A 2D matrix containing multiple variables and observations, or a
  1779. Scalar or 1D vector representing a single variable.
  1780. Returns:
  1781. (Tensor) The correlation coefficient matrix of the variables.
  1782. .. seealso::
  1783. :func:`torch.cov` covariance matrix.
  1784. Example::
  1785. >>> x = torch.tensor([[0, 1, 2], [2, 1, 0]])
  1786. >>> torch.corrcoef(x)
  1787. tensor([[ 1., -1.],
  1788. [-1., 1.]])
  1789. >>> x = torch.randn(2, 4)
  1790. >>> x
  1791. tensor([[-0.2678, -0.0908, -0.3766, 0.2780],
  1792. [-0.5812, 0.1535, 0.2387, 0.2350]])
  1793. >>> torch.corrcoef(x)
  1794. tensor([[1.0000, 0.3582],
  1795. [0.3582, 1.0000]])
  1796. >>> torch.corrcoef(x[0])
  1797. tensor(1.)
  1798. """,
  1799. )
  1800. add_docstr(
  1801. torch.cov,
  1802. r"""
  1803. cov(input, *, correction=1, fweights=None, aweights=None) -> Tensor
  1804. Estimates the covariance matrix of the variables given by the :attr:`input` matrix, where rows are
  1805. the variables and columns are the observations.
  1806. A covariance matrix is a square matrix giving the covariance of each pair of variables. The diagonal contains
  1807. the variance of each variable (covariance of a variable with itself). By definition, if :attr:`input` represents
  1808. a single variable (Scalar or 1D) then its variance is returned.
  1809. The sample covariance of the variables :math:`x` and :math:`y` is given by:
  1810. .. math::
  1811. \text{cov}(x,y) = \frac{\sum^{N}_{i = 1}(x_{i} - \bar{x})(y_{i} - \bar{y})}{\max(0,~N~-~\delta N)}
  1812. where :math:`\bar{x}` and :math:`\bar{y}` are the simple means of the :math:`x` and :math:`y` respectively, and
  1813. :math:`\delta N` is the :attr:`correction`.
  1814. If :attr:`fweights` and/or :attr:`aweights` are provided, the weighted covariance
  1815. is calculated, which is given by:
  1816. .. math::
  1817. \text{cov}_w(x,y) = \frac{\sum^{N}_{i = 1}w_i(x_{i} - \mu_x^*)(y_{i} - \mu_y^*)}
  1818. {\max(0,~\sum^{N}_{i = 1}w_i~-~\frac{\sum^{N}_{i = 1}w_ia_i}{\sum^{N}_{i = 1}w_i}~\delta N)}
  1819. where :math:`w` denotes :attr:`fweights` or :attr:`aweights` (``f`` and ``a`` for brevity) based on whichever is
  1820. provided, or :math:`w = f \times a` if both are provided, and
  1821. :math:`\mu_x^* = \frac{\sum^{N}_{i = 1}w_ix_{i} }{\sum^{N}_{i = 1}w_i}` is the weighted mean of the variable. If not
  1822. provided, ``f`` and/or ``a`` can be seen as a :math:`\mathbb{1}` vector of appropriate size.
  1823. Args:
  1824. input (Tensor): A 2D matrix containing multiple variables and observations, or a
  1825. Scalar or 1D vector representing a single variable.
  1826. Keyword Args:
  1827. correction (int, optional): difference between the sample size and sample degrees of freedom.
  1828. Defaults to Bessel's correction, ``correction = 1`` which returns the unbiased estimate,
  1829. even if both :attr:`fweights` and :attr:`aweights` are specified. ``correction = 0``
  1830. will return the simple average. Defaults to ``1``.
  1831. fweights (tensor, optional): A Scalar or 1D tensor of observation vector frequencies representing the number of
  1832. times each observation should be repeated. Its numel must equal the number of columns of :attr:`input`.
  1833. Must have integral dtype. Ignored if ``None``. Defaults to ``None``.
  1834. aweights (tensor, optional): A Scalar or 1D array of observation vector weights.
  1835. These relative weights are typically large for observations considered "important" and smaller for
  1836. observations considered less "important". Its numel must equal the number of columns of :attr:`input`.
  1837. Must have floating point dtype. Ignored if ``None``. Defaults to ``None``.
  1838. Returns:
  1839. (Tensor) The covariance matrix of the variables.
  1840. .. seealso::
  1841. :func:`torch.corrcoef` normalized covariance matrix.
  1842. Example::
  1843. >>> x = torch.tensor([[0, 2], [1, 1], [2, 0]]).T
  1844. >>> x
  1845. tensor([[0, 1, 2],
  1846. [2, 1, 0]])
  1847. >>> torch.cov(x)
  1848. tensor([[ 1., -1.],
  1849. [-1., 1.]])
  1850. >>> torch.cov(x, correction=0)
  1851. tensor([[ 0.6667, -0.6667],
  1852. [-0.6667, 0.6667]])
  1853. >>> fw = torch.randint(1, 10, (3,))
  1854. >>> fw
  1855. tensor([1, 6, 9])
  1856. >>> aw = torch.rand(3)
  1857. >>> aw
  1858. tensor([0.4282, 0.0255, 0.4144])
  1859. >>> torch.cov(x, fweights=fw, aweights=aw)
  1860. tensor([[ 0.4169, -0.4169],
  1861. [-0.4169, 0.4169]])
  1862. """,
  1863. )
  1864. add_docstr(
  1865. torch.cat,
  1866. r"""
  1867. cat(tensors, dim=0, *, out=None) -> Tensor
  1868. Concatenates the given sequence of tensors in :attr:`tensors` in the given dimension.
  1869. All tensors must either have the same shape (except in the concatenating
  1870. dimension) or be a 1-D empty tensor with size ``(0,)``.
  1871. :func:`torch.cat` can be seen as an inverse operation for :func:`torch.split`
  1872. and :func:`torch.chunk`.
  1873. :func:`torch.cat` can be best understood via examples.
  1874. .. seealso::
  1875. :func:`torch.stack` concatenates the given sequence along a new dimension.
  1876. Args:
  1877. tensors (sequence of Tensors): Non-empty tensors provided must have the same shape,
  1878. except in the cat dimension.
  1879. dim (int, optional): the dimension over which the tensors are concatenated
  1880. Keyword args:
  1881. {out}
  1882. Example::
  1883. >>> x = torch.randn(2, 3)
  1884. >>> x
  1885. tensor([[ 0.6580, -1.0969, -0.4614],
  1886. [-0.1034, -0.5790, 0.1497]])
  1887. >>> torch.cat((x, x, x), 0)
  1888. tensor([[ 0.6580, -1.0969, -0.4614],
  1889. [-0.1034, -0.5790, 0.1497],
  1890. [ 0.6580, -1.0969, -0.4614],
  1891. [-0.1034, -0.5790, 0.1497],
  1892. [ 0.6580, -1.0969, -0.4614],
  1893. [-0.1034, -0.5790, 0.1497]])
  1894. >>> torch.cat((x, x, x), 1)
  1895. tensor([[ 0.6580, -1.0969, -0.4614, 0.6580, -1.0969, -0.4614, 0.6580,
  1896. -1.0969, -0.4614],
  1897. [-0.1034, -0.5790, 0.1497, -0.1034, -0.5790, 0.1497, -0.1034,
  1898. -0.5790, 0.1497]])
  1899. """.format(**common_args),
  1900. )
  1901. add_docstr(
  1902. torch.concat,
  1903. r"""
  1904. concat(tensors, dim=0, *, out=None) -> Tensor
  1905. Alias of :func:`torch.cat`.
  1906. """,
  1907. )
  1908. add_docstr(
  1909. torch.concatenate,
  1910. r"""
  1911. concatenate(tensors, axis=0, out=None) -> Tensor
  1912. Alias of :func:`torch.cat`.
  1913. """,
  1914. )
  1915. add_docstr(
  1916. torch.ceil,
  1917. r"""
  1918. ceil(input, *, out=None) -> Tensor
  1919. Returns a new tensor with the ceil of the elements of :attr:`input`,
  1920. the smallest integer greater than or equal to each element.
  1921. For integer inputs, follows the array-api convention of returning a
  1922. copy of the input tensor.
  1923. .. math::
  1924. \text{out}_{i} = \left\lceil \text{input}_{i} \right\rceil
  1925. """
  1926. + r"""
  1927. Args:
  1928. {input}
  1929. Keyword args:
  1930. {out}
  1931. Example::
  1932. >>> a = torch.randn(4)
  1933. >>> a
  1934. tensor([-0.6341, -1.4208, -1.0900, 0.5826])
  1935. >>> torch.ceil(a)
  1936. tensor([-0., -1., -1., 1.])
  1937. """.format(**common_args),
  1938. )
  1939. add_docstr(
  1940. torch.real,
  1941. r"""
  1942. real(input) -> Tensor
  1943. Returns a new tensor containing real values of the :attr:`self` tensor.
  1944. The returned tensor and :attr:`self` share the same underlying storage.
  1945. Args:
  1946. {input}
  1947. Example::
  1948. >>> x=torch.randn(4, dtype=torch.cfloat)
  1949. >>> x
  1950. tensor([(0.3100+0.3553j), (-0.5445-0.7896j), (-1.6492-0.0633j), (-0.0638-0.8119j)])
  1951. >>> x.real
  1952. tensor([ 0.3100, -0.5445, -1.6492, -0.0638])
  1953. """.format(**common_args),
  1954. )
  1955. add_docstr(
  1956. torch.imag,
  1957. r"""
  1958. imag(input) -> Tensor
  1959. Returns a new tensor containing imaginary values of the :attr:`self` tensor.
  1960. The returned tensor and :attr:`self` share the same underlying storage.
  1961. .. warning::
  1962. :func:`imag` is only supported for tensors with complex dtypes.
  1963. Args:
  1964. {input}
  1965. Example::
  1966. >>> x=torch.randn(4, dtype=torch.cfloat)
  1967. >>> x
  1968. tensor([(0.3100+0.3553j), (-0.5445-0.7896j), (-1.6492-0.0633j), (-0.0638-0.8119j)])
  1969. >>> x.imag
  1970. tensor([ 0.3553, -0.7896, -0.0633, -0.8119])
  1971. """.format(**common_args),
  1972. )
  1973. add_docstr(
  1974. torch.view_as_real,
  1975. r"""
  1976. view_as_real(input) -> Tensor
  1977. Returns a view of :attr:`input` as a real tensor. For an input complex tensor of
  1978. :attr:`size` :math:`m1, m2, \dots, mi`, this function returns a new
  1979. real tensor of size :math:`m1, m2, \dots, mi, 2`, where the last dimension of size 2
  1980. represents the real and imaginary components of complex numbers.
  1981. .. warning::
  1982. :func:`view_as_real` is only supported for tensors with ``complex dtypes``.
  1983. Args:
  1984. {input}
  1985. Example::
  1986. >>> x=torch.randn(4, dtype=torch.cfloat)
  1987. >>> x
  1988. tensor([(0.4737-0.3839j), (-0.2098-0.6699j), (0.3470-0.9451j), (-0.5174-1.3136j)])
  1989. >>> torch.view_as_real(x)
  1990. tensor([[ 0.4737, -0.3839],
  1991. [-0.2098, -0.6699],
  1992. [ 0.3470, -0.9451],
  1993. [-0.5174, -1.3136]])
  1994. """.format(**common_args),
  1995. )
  1996. add_docstr(
  1997. torch.view_as_complex,
  1998. r"""
  1999. view_as_complex(input) -> Tensor
  2000. Returns a view of :attr:`input` as a complex tensor. For an input complex
  2001. tensor of :attr:`size` :math:`m1, m2, \dots, mi, 2`, this function returns a
  2002. new complex tensor of :attr:`size` :math:`m1, m2, \dots, mi` where the last
  2003. dimension of the input tensor is expected to represent the real and imaginary
  2004. components of complex numbers.
  2005. .. warning::
  2006. :func:`view_as_complex` is only supported for tensors with
  2007. :class:`torch.dtype` ``torch.float64`` and ``torch.float32``. The input is
  2008. expected to have the last dimension of :attr:`size` 2. In addition, the
  2009. tensor must have a `stride` of 1 for its last dimension. The strides of all
  2010. other dimensions must be even numbers.
  2011. Args:
  2012. {input}
  2013. Example::
  2014. >>> x=torch.randn(4, 2)
  2015. >>> x
  2016. tensor([[ 1.6116, -0.5772],
  2017. [-1.4606, -0.9120],
  2018. [ 0.0786, -1.7497],
  2019. [-0.6561, -1.6623]])
  2020. >>> torch.view_as_complex(x)
  2021. tensor([(1.6116-0.5772j), (-1.4606-0.9120j), (0.0786-1.7497j), (-0.6561-1.6623j)])
  2022. """.format(**common_args),
  2023. )
  2024. add_docstr(
  2025. torch.reciprocal,
  2026. r"""
  2027. reciprocal(input, *, out=None) -> Tensor
  2028. Returns a new tensor with the reciprocal of the elements of :attr:`input`
  2029. .. math::
  2030. \text{out}_{i} = \frac{1}{\text{input}_{i}}
  2031. .. note::
  2032. Unlike NumPy's reciprocal, torch.reciprocal supports integral inputs. Integral
  2033. inputs to reciprocal are automatically :ref:`promoted <type-promotion-doc>` to
  2034. the default scalar type.
  2035. """
  2036. + r"""
  2037. Args:
  2038. {input}
  2039. Keyword args:
  2040. {out}
  2041. Example::
  2042. >>> a = torch.randn(4)
  2043. >>> a
  2044. tensor([-0.4595, -2.1219, -1.4314, 0.7298])
  2045. >>> torch.reciprocal(a)
  2046. tensor([-2.1763, -0.4713, -0.6986, 1.3702])
  2047. """.format(**common_args),
  2048. )
  2049. add_docstr(
  2050. torch.cholesky,
  2051. r"""
  2052. cholesky(input, upper=False, *, out=None) -> Tensor
  2053. Computes the Cholesky decomposition of a symmetric positive-definite
  2054. matrix :math:`A` or for batches of symmetric positive-definite matrices.
  2055. If :attr:`upper` is ``True``, the returned matrix ``U`` is upper-triangular, and
  2056. the decomposition has the form:
  2057. .. math::
  2058. A = U^TU
  2059. If :attr:`upper` is ``False``, the returned matrix ``L`` is lower-triangular, and
  2060. the decomposition has the form:
  2061. .. math::
  2062. A = LL^T
  2063. If :attr:`upper` is ``True``, and :math:`A` is a batch of symmetric positive-definite
  2064. matrices, then the returned tensor will be composed of upper-triangular Cholesky factors
  2065. of each of the individual matrices. Similarly, when :attr:`upper` is ``False``, the returned
  2066. tensor will be composed of lower-triangular Cholesky factors of each of the individual
  2067. matrices.
  2068. .. warning::
  2069. :func:`torch.cholesky` is deprecated in favor of :func:`torch.linalg.cholesky`
  2070. and will be removed in a future PyTorch release.
  2071. ``L = torch.cholesky(A)`` should be replaced with
  2072. .. code:: python
  2073. L = torch.linalg.cholesky(A)
  2074. ``U = torch.cholesky(A, upper=True)`` should be replaced with
  2075. .. code:: python
  2076. U = torch.linalg.cholesky(A).mH
  2077. This transform will produce equivalent results for all valid (symmetric positive definite) inputs.
  2078. Args:
  2079. input (Tensor): the input tensor :math:`A` of size :math:`(*, n, n)` where `*` is zero or more
  2080. batch dimensions consisting of symmetric positive-definite matrices.
  2081. upper (bool, optional): flag that indicates whether to return a
  2082. upper or lower triangular matrix. Default: ``False``
  2083. Keyword args:
  2084. out (Tensor, optional): the output matrix
  2085. Example::
  2086. >>> a = torch.randn(3, 3)
  2087. >>> a = a @ a.mT + 1e-3 # make symmetric positive-definite
  2088. >>> l = torch.cholesky(a)
  2089. >>> a
  2090. tensor([[ 2.4112, -0.7486, 1.4551],
  2091. [-0.7486, 1.3544, 0.1294],
  2092. [ 1.4551, 0.1294, 1.6724]])
  2093. >>> l
  2094. tensor([[ 1.5528, 0.0000, 0.0000],
  2095. [-0.4821, 1.0592, 0.0000],
  2096. [ 0.9371, 0.5487, 0.7023]])
  2097. >>> l @ l.mT
  2098. tensor([[ 2.4112, -0.7486, 1.4551],
  2099. [-0.7486, 1.3544, 0.1294],
  2100. [ 1.4551, 0.1294, 1.6724]])
  2101. >>> a = torch.randn(3, 2, 2) # Example for batched input
  2102. >>> a = a @ a.mT + 1e-03 # make symmetric positive-definite
  2103. >>> l = torch.cholesky(a)
  2104. >>> z = l @ l.mT
  2105. >>> torch.dist(z, a)
  2106. tensor(2.3842e-07)
  2107. """,
  2108. )
  2109. add_docstr(
  2110. torch.cholesky_solve,
  2111. r"""
  2112. cholesky_solve(B, L, upper=False, *, out=None) -> Tensor
  2113. Computes the solution of a system of linear equations with complex Hermitian
  2114. or real symmetric positive-definite lhs given its Cholesky decomposition.
  2115. Let :math:`A` be a complex Hermitian or real symmetric positive-definite matrix,
  2116. and :math:`L` its Cholesky decomposition such that:
  2117. .. math::
  2118. A = LL^{\text{H}}
  2119. where :math:`L^{\text{H}}` is the conjugate transpose when :math:`L` is complex,
  2120. and the transpose when :math:`L` is real-valued.
  2121. Returns the solution :math:`X` of the following linear system:
  2122. .. math::
  2123. AX = B
  2124. Supports inputs of float, double, cfloat and cdouble dtypes.
  2125. Also supports batches of matrices, and if :math:`A` or :math:`B` is a batch of matrices
  2126. then the output has the same batch dimensions.
  2127. Args:
  2128. B (Tensor): right-hand side tensor of shape `(*, n, k)`
  2129. where :math:`*` is zero or more batch dimensions
  2130. L (Tensor): tensor of shape `(*, n, n)` where `*` is zero or more batch dimensions
  2131. consisting of lower or upper triangular Cholesky decompositions of
  2132. symmetric or Hermitian positive-definite matrices.
  2133. upper (bool, optional): flag that indicates whether :math:`L` is lower triangular
  2134. or upper triangular. Default: ``False``.
  2135. Keyword args:
  2136. out (Tensor, optional): output tensor. Ignored if `None`. Default: `None`.
  2137. Example::
  2138. >>> A = torch.randn(3, 3)
  2139. >>> A = A @ A.T + torch.eye(3) * 1e-3 # Creates a symmetric positive-definite matrix
  2140. >>> L = torch.linalg.cholesky(A) # Extract Cholesky decomposition
  2141. >>> B = torch.randn(3, 2)
  2142. >>> torch.cholesky_solve(B, L)
  2143. tensor([[ -8.1625, 19.6097],
  2144. [ -5.8398, 14.2387],
  2145. [ -4.3771, 10.4173]])
  2146. >>> A.inverse() @ B
  2147. tensor([[ -8.1626, 19.6097],
  2148. [ -5.8398, 14.2387],
  2149. [ -4.3771, 10.4173]])
  2150. >>> A = torch.randn(3, 2, 2, dtype=torch.complex64)
  2151. >>> A = A @ A.mH + torch.eye(2) * 1e-3 # Batch of Hermitian positive-definite matrices
  2152. >>> L = torch.linalg.cholesky(A)
  2153. >>> B = torch.randn(2, 1, dtype=torch.complex64)
  2154. >>> X = torch.cholesky_solve(B, L)
  2155. >>> torch.dist(X, A.inverse() @ B)
  2156. tensor(1.6881e-5)
  2157. """,
  2158. )
  2159. add_docstr(
  2160. torch.cholesky_inverse,
  2161. r"""
  2162. cholesky_inverse(L, upper=False, *, out=None) -> Tensor
  2163. Computes the inverse of a complex Hermitian or real symmetric
  2164. positive-definite matrix given its Cholesky decomposition.
  2165. Let :math:`A` be a complex Hermitian or real symmetric positive-definite matrix,
  2166. and :math:`L` its Cholesky decomposition such that:
  2167. .. math::
  2168. A = LL^{\text{H}}
  2169. where :math:`L^{\text{H}}` is the conjugate transpose when :math:`L` is complex,
  2170. and the transpose when :math:`L` is real-valued.
  2171. Computes the inverse matrix :math:`A^{-1}`.
  2172. Supports input of float, double, cfloat and cdouble dtypes.
  2173. Also supports batches of matrices, and if :math:`A` is a batch of matrices
  2174. then the output has the same batch dimensions.
  2175. Args:
  2176. L (Tensor): tensor of shape `(*, n, n)` where `*` is zero or more batch dimensions
  2177. consisting of lower or upper triangular Cholesky decompositions of
  2178. symmetric or Hermitian positive-definite matrices.
  2179. upper (bool, optional): flag that indicates whether :math:`L` is lower triangular
  2180. or upper triangular. Default: ``False``
  2181. Keyword args:
  2182. out (Tensor, optional): output tensor. Ignored if `None`. Default: `None`.
  2183. Example::
  2184. >>> A = torch.randn(3, 3)
  2185. >>> A = A @ A.T + torch.eye(3) * 1e-3 # Creates a symmetric positive-definite matrix
  2186. >>> L = torch.linalg.cholesky(A) # Extract Cholesky decomposition
  2187. >>> torch.cholesky_inverse(L)
  2188. tensor([[ 1.9314, 1.2251, -0.0889],
  2189. [ 1.2251, 2.4439, 0.2122],
  2190. [-0.0889, 0.2122, 0.1412]])
  2191. >>> A.inverse()
  2192. tensor([[ 1.9314, 1.2251, -0.0889],
  2193. [ 1.2251, 2.4439, 0.2122],
  2194. [-0.0889, 0.2122, 0.1412]])
  2195. >>> A = torch.randn(3, 2, 2, dtype=torch.complex64)
  2196. >>> A = A @ A.mH + torch.eye(2) * 1e-3 # Batch of Hermitian positive-definite matrices
  2197. >>> L = torch.linalg.cholesky(A)
  2198. >>> torch.dist(torch.inverse(A), torch.cholesky_inverse(L))
  2199. tensor(5.6358e-7)
  2200. """,
  2201. )
  2202. add_docstr(
  2203. torch.clone,
  2204. r"""
  2205. clone(input, *, memory_format=torch.preserve_format) -> Tensor
  2206. Returns a copy of :attr:`input`.
  2207. .. note::
  2208. This function is differentiable, so gradients will flow back from the
  2209. result of this operation to :attr:`input`. To create a tensor without an
  2210. autograd relationship to :attr:`input` see :meth:`~Tensor.detach`.
  2211. In addition, when ``torch.preserve_format`` is used:
  2212. If the input tensor is dense (i.e., non-overlapping strided),
  2213. its memory format (including strides) is retained.
  2214. Otherwise (e.g., a non-dense view like a stepped slice),
  2215. the output is converted to the dense (contiguous) format.
  2216. Args:
  2217. {input}
  2218. Keyword args:
  2219. {memory_format}
  2220. """.format(**common_args),
  2221. )
  2222. add_docstr(
  2223. torch.clamp,
  2224. r"""
  2225. clamp(input, min=None, max=None, *, out=None) -> Tensor
  2226. Clamps all elements in :attr:`input` into the range `[` :attr:`min`, :attr:`max` `]`.
  2227. Letting min_value and max_value be :attr:`min` and :attr:`max`, respectively, this returns:
  2228. .. math::
  2229. y_i = \min(\max(x_i, \text{min\_value}_i), \text{max\_value}_i)
  2230. If :attr:`min` is ``None``, there is no lower bound.
  2231. Or, if :attr:`max` is ``None`` there is no upper bound.
  2232. """
  2233. + r"""
  2234. .. note::
  2235. If :attr:`min` is greater than :attr:`max` :func:`torch.clamp(..., min, max) <torch.clamp>`
  2236. sets all elements in :attr:`input` to the value of :attr:`max`.
  2237. Args:
  2238. {input}
  2239. min (Number or Tensor, optional): lower-bound of the range to be clamped to
  2240. max (Number or Tensor, optional): upper-bound of the range to be clamped to
  2241. Keyword args:
  2242. {out}
  2243. Example::
  2244. >>> a = torch.randn(4)
  2245. >>> a
  2246. tensor([-1.7120, 0.1734, -0.0478, -0.0922])
  2247. >>> torch.clamp(a, min=-0.5, max=0.5)
  2248. tensor([-0.5000, 0.1734, -0.0478, -0.0922])
  2249. >>> min = torch.linspace(-1, 1, steps=4)
  2250. >>> torch.clamp(a, min=min)
  2251. tensor([-1.0000, 0.1734, 0.3333, 1.0000])
  2252. """.format(**common_args),
  2253. )
  2254. add_docstr(
  2255. torch.clip,
  2256. r"""
  2257. clip(input, min=None, max=None, *, out=None) -> Tensor
  2258. Alias for :func:`torch.clamp`.
  2259. """,
  2260. )
  2261. add_docstr(
  2262. torch.column_stack,
  2263. r"""
  2264. column_stack(tensors, *, out=None) -> Tensor
  2265. Creates a new tensor by horizontally stacking the tensors in :attr:`tensors`.
  2266. Equivalent to ``torch.hstack(tensors)``, except each zero or one dimensional tensor ``t``
  2267. in :attr:`tensors` is first reshaped into a ``(t.numel(), 1)`` column before being stacked horizontally.
  2268. Args:
  2269. tensors (sequence of Tensors): sequence of tensors to concatenate
  2270. Keyword args:
  2271. {out}
  2272. Example::
  2273. >>> a = torch.tensor([1, 2, 3])
  2274. >>> b = torch.tensor([4, 5, 6])
  2275. >>> torch.column_stack((a, b))
  2276. tensor([[1, 4],
  2277. [2, 5],
  2278. [3, 6]])
  2279. >>> a = torch.arange(5)
  2280. >>> b = torch.arange(10).reshape(5, 2)
  2281. >>> torch.column_stack((a, b, b))
  2282. tensor([[0, 0, 1, 0, 1],
  2283. [1, 2, 3, 2, 3],
  2284. [2, 4, 5, 4, 5],
  2285. [3, 6, 7, 6, 7],
  2286. [4, 8, 9, 8, 9]])
  2287. """.format(**common_args),
  2288. )
  2289. add_docstr(
  2290. torch.complex,
  2291. r"""
  2292. complex(real, imag, *, out=None) -> Tensor
  2293. Constructs a complex tensor with its real part equal to :attr:`real` and its
  2294. imaginary part equal to :attr:`imag`.
  2295. Args:
  2296. real (Tensor): The real part of the complex tensor. Must be half, float or double.
  2297. imag (Tensor): The imaginary part of the complex tensor. Must be same dtype
  2298. as :attr:`real`.
  2299. Keyword args:
  2300. out (Tensor): If the inputs are ``torch.float32``, must be
  2301. ``torch.complex64``. If the inputs are ``torch.float64``, must be
  2302. ``torch.complex128``.
  2303. Example::
  2304. >>> real = torch.tensor([1, 2], dtype=torch.float32)
  2305. >>> imag = torch.tensor([3, 4], dtype=torch.float32)
  2306. >>> z = torch.complex(real, imag)
  2307. >>> z
  2308. tensor([(1.+3.j), (2.+4.j)])
  2309. >>> z.dtype
  2310. torch.complex64
  2311. """,
  2312. )
  2313. add_docstr(
  2314. torch.polar,
  2315. r"""
  2316. polar(abs, angle, *, out=None) -> Tensor
  2317. Constructs a complex tensor whose elements are Cartesian coordinates
  2318. corresponding to the polar coordinates with absolute value :attr:`abs` and angle
  2319. :attr:`angle`.
  2320. .. math::
  2321. \text{out} = \text{abs} \cdot \cos(\text{angle}) + \text{abs} \cdot \sin(\text{angle}) \cdot j
  2322. .. note::
  2323. `torch.polar` is similar to
  2324. `std::polar <https://en.cppreference.com/w/cpp/numeric/complex/polar>`_
  2325. and does not compute the polar decomposition
  2326. of a complex tensor like Python's `cmath.polar` and SciPy's `linalg.polar` do.
  2327. The behavior of this function is undefined if `abs` is negative or NaN, or if `angle` is
  2328. infinite.
  2329. """
  2330. + r"""
  2331. Args:
  2332. abs (Tensor): The absolute value the complex tensor. Must be float or double.
  2333. angle (Tensor): The angle of the complex tensor. Must be same dtype as
  2334. :attr:`abs`.
  2335. Keyword args:
  2336. out (Tensor): If the inputs are ``torch.float32``, must be
  2337. ``torch.complex64``. If the inputs are ``torch.float64``, must be
  2338. ``torch.complex128``.
  2339. Example::
  2340. >>> import numpy as np
  2341. >>> abs = torch.tensor([1, 2], dtype=torch.float64)
  2342. >>> angle = torch.tensor([np.pi / 2, 5 * np.pi / 4], dtype=torch.float64)
  2343. >>> z = torch.polar(abs, angle)
  2344. >>> z
  2345. tensor([(0.0000+1.0000j), (-1.4142-1.4142j)], dtype=torch.complex128)
  2346. """,
  2347. )
  2348. add_docstr(
  2349. torch.conj_physical,
  2350. r"""
  2351. conj_physical(input, *, out=None) -> Tensor
  2352. Computes the element-wise conjugate of the given :attr:`input` tensor.
  2353. If :attr:`input` has a non-complex dtype, this function just returns :attr:`input`.
  2354. .. note::
  2355. This performs the conjugate operation regardless of the fact conjugate bit is set or not.
  2356. .. warning:: In the future, :func:`torch.conj_physical` may return a non-writeable view for an :attr:`input` of
  2357. non-complex dtype. It's recommended that programs not modify the tensor returned by :func:`torch.conj_physical`
  2358. when :attr:`input` is of non-complex dtype to be compatible with this change.
  2359. .. math::
  2360. \text{out}_{i} = conj(\text{input}_{i})
  2361. """
  2362. + r"""
  2363. Args:
  2364. {input}
  2365. Keyword args:
  2366. {out}
  2367. Example::
  2368. >>> torch.conj_physical(torch.tensor([-1 + 1j, -2 + 2j, 3 - 3j]))
  2369. tensor([-1 - 1j, -2 - 2j, 3 + 3j])
  2370. """.format(**common_args),
  2371. )
  2372. add_docstr(
  2373. torch.conj,
  2374. r"""
  2375. conj(input) -> Tensor
  2376. Returns a view of :attr:`input` with a flipped conjugate bit. If :attr:`input` has a non-complex dtype,
  2377. this function just returns :attr:`input`.
  2378. .. note::
  2379. :func:`torch.conj` performs a lazy conjugation, but the actual conjugated tensor can be materialized
  2380. at any time using :func:`torch.resolve_conj`.
  2381. .. warning:: In the future, :func:`torch.conj` may return a non-writeable view for an :attr:`input` of
  2382. non-complex dtype. It's recommended that programs not modify the tensor returned by :func:`torch.conj_physical`
  2383. when :attr:`input` is of non-complex dtype to be compatible with this change.
  2384. Args:
  2385. {input}
  2386. Example::
  2387. >>> x = torch.tensor([-1 + 1j, -2 + 2j, 3 - 3j])
  2388. >>> x.is_conj()
  2389. False
  2390. >>> y = torch.conj(x)
  2391. >>> y.is_conj()
  2392. True
  2393. """.format(**common_args),
  2394. )
  2395. add_docstr(
  2396. torch.resolve_conj,
  2397. r"""
  2398. resolve_conj(input) -> Tensor
  2399. Returns a new tensor with materialized conjugation if :attr:`input`'s conjugate bit is set to `True`,
  2400. else returns :attr:`input`. The output tensor will always have its conjugate bit set to `False`.
  2401. Args:
  2402. {input}
  2403. Example::
  2404. >>> x = torch.tensor([-1 + 1j, -2 + 2j, 3 - 3j])
  2405. >>> y = x.conj()
  2406. >>> y.is_conj()
  2407. True
  2408. >>> z = y.resolve_conj()
  2409. >>> z
  2410. tensor([-1 - 1j, -2 - 2j, 3 + 3j])
  2411. >>> z.is_conj()
  2412. False
  2413. """.format(**common_args),
  2414. )
  2415. add_docstr(
  2416. torch.resolve_neg,
  2417. r"""
  2418. resolve_neg(input) -> Tensor
  2419. Returns a new tensor with materialized negation if :attr:`input`'s negative bit is set to `True`,
  2420. else returns :attr:`input`. The output tensor will always have its negative bit set to `False`.
  2421. Args:
  2422. {input}
  2423. Example::
  2424. >>> x = torch.tensor([-1 + 1j, -2 + 2j, 3 - 3j])
  2425. >>> y = x.conj()
  2426. >>> z = y.imag
  2427. >>> z.is_neg()
  2428. True
  2429. >>> out = z.resolve_neg()
  2430. >>> out
  2431. tensor([-1., -2., 3.])
  2432. >>> out.is_neg()
  2433. False
  2434. """.format(**common_args),
  2435. )
  2436. add_docstr(
  2437. torch.copysign,
  2438. r"""
  2439. copysign(input, other, *, out=None) -> Tensor
  2440. Create a new floating-point tensor with the magnitude of :attr:`input` and the sign of :attr:`other`, elementwise.
  2441. .. math::
  2442. \text{out}_{i} = \begin{cases}
  2443. -|\text{input}_{i}| & \text{if } \text{other}_{i} \leq -0.0 \\
  2444. |\text{input}_{i}| & \text{if } \text{other}_{i} \geq 0.0 \\
  2445. \end{cases}
  2446. """
  2447. + r"""
  2448. Supports :ref:`broadcasting to a common shape <broadcasting-semantics>`,
  2449. and integer and float inputs.
  2450. Args:
  2451. input (Tensor): magnitudes.
  2452. other (Tensor or Number): contains value(s) whose signbit(s) are
  2453. applied to the magnitudes in :attr:`input`.
  2454. Keyword args:
  2455. {out}
  2456. Example::
  2457. >>> a = torch.randn(5)
  2458. >>> a
  2459. tensor([-1.2557, -0.0026, -0.5387, 0.4740, -0.9244])
  2460. >>> torch.copysign(a, 1)
  2461. tensor([1.2557, 0.0026, 0.5387, 0.4740, 0.9244])
  2462. >>> a = torch.randn(4, 4)
  2463. >>> a
  2464. tensor([[ 0.7079, 0.2778, -1.0249, 0.5719],
  2465. [-0.0059, -0.2600, -0.4475, -1.3948],
  2466. [ 0.3667, -0.9567, -2.5757, -0.1751],
  2467. [ 0.2046, -0.0742, 0.2998, -0.1054]])
  2468. >>> b = torch.randn(4)
  2469. tensor([ 0.2373, 0.3120, 0.3190, -1.1128])
  2470. >>> torch.copysign(a, b)
  2471. tensor([[ 0.7079, 0.2778, 1.0249, -0.5719],
  2472. [ 0.0059, 0.2600, 0.4475, -1.3948],
  2473. [ 0.3667, 0.9567, 2.5757, -0.1751],
  2474. [ 0.2046, 0.0742, 0.2998, -0.1054]])
  2475. >>> a = torch.tensor([1.])
  2476. >>> b = torch.tensor([-0.])
  2477. >>> torch.copysign(a, b)
  2478. tensor([-1.])
  2479. .. note::
  2480. copysign handles signed zeros. If the other argument has a negative zero (-0),
  2481. the corresponding output value will be negative.
  2482. """.format(**common_args),
  2483. )
  2484. add_docstr(
  2485. torch.cos,
  2486. r"""
  2487. cos(input, *, out=None) -> Tensor
  2488. Returns a new tensor with the cosine of the elements of :attr:`input`.
  2489. .. math::
  2490. \text{out}_{i} = \cos(\text{input}_{i})
  2491. """
  2492. + r"""
  2493. Args:
  2494. {input}
  2495. Keyword args:
  2496. {out}
  2497. Example::
  2498. >>> a = torch.randn(4)
  2499. >>> a
  2500. tensor([ 1.4309, 1.2706, -0.8562, 0.9796])
  2501. >>> torch.cos(a)
  2502. tensor([ 0.1395, 0.2957, 0.6553, 0.5574])
  2503. """.format(**common_args),
  2504. )
  2505. add_docstr(
  2506. torch.cosh,
  2507. r"""
  2508. cosh(input, *, out=None) -> Tensor
  2509. Returns a new tensor with the hyperbolic cosine of the elements of
  2510. :attr:`input`.
  2511. .. math::
  2512. \text{out}_{i} = \cosh(\text{input}_{i})
  2513. """
  2514. + r"""
  2515. Args:
  2516. {input}
  2517. Keyword args:
  2518. {out}
  2519. Example::
  2520. >>> a = torch.randn(4)
  2521. >>> a
  2522. tensor([ 0.1632, 1.1835, -0.6979, -0.7325])
  2523. >>> torch.cosh(a)
  2524. tensor([ 1.0133, 1.7860, 1.2536, 1.2805])
  2525. .. note::
  2526. When :attr:`input` is on the CPU, the implementation of torch.cosh may use
  2527. the Sleef library, which rounds very large results to infinity or negative
  2528. infinity. See `here <https://sleef.org/purec.xhtml>`_ for details.
  2529. """.format(**common_args),
  2530. )
  2531. add_docstr(
  2532. torch.cross,
  2533. r"""
  2534. cross(input, other, dim=None, *, out=None) -> Tensor
  2535. Returns the cross product of vectors in dimension :attr:`dim` of :attr:`input`
  2536. and :attr:`other`.
  2537. Supports input of float, double, cfloat and cdouble dtypes. Also supports batches
  2538. of vectors, for which it computes the product along the dimension :attr:`dim`.
  2539. In this case, the output has the same batch dimensions as the inputs.
  2540. .. warning::
  2541. If :attr:`dim` is not given, it defaults to the first dimension found
  2542. with the size 3. Note that this might be unexpected.
  2543. This behavior is deprecated and will be changed to match that of :func:`torch.linalg.cross`
  2544. in a future release.
  2545. .. seealso::
  2546. :func:`torch.linalg.cross` which has dim=-1 as default.
  2547. Args:
  2548. {input}
  2549. other (Tensor): the second input tensor
  2550. dim (int, optional): the dimension to take the cross-product in.
  2551. Keyword args:
  2552. {out}
  2553. Example::
  2554. >>> a = torch.randn(4, 3)
  2555. >>> a
  2556. tensor([[-0.3956, 1.1455, 1.6895],
  2557. [-0.5849, 1.3672, 0.3599],
  2558. [-1.1626, 0.7180, -0.0521],
  2559. [-0.1339, 0.9902, -2.0225]])
  2560. >>> b = torch.randn(4, 3)
  2561. >>> b
  2562. tensor([[-0.0257, -1.4725, -1.2251],
  2563. [-1.1479, -0.7005, -1.9757],
  2564. [-1.3904, 0.3726, -1.1836],
  2565. [-0.9688, -0.7153, 0.2159]])
  2566. >>> torch.cross(a, b, dim=1)
  2567. tensor([[ 1.0844, -0.5281, 0.6120],
  2568. [-2.4490, -1.5687, 1.9792],
  2569. [-0.8304, -1.3037, 0.5650],
  2570. [-1.2329, 1.9883, 1.0551]])
  2571. >>> torch.cross(a, b)
  2572. tensor([[ 1.0844, -0.5281, 0.6120],
  2573. [-2.4490, -1.5687, 1.9792],
  2574. [-0.8304, -1.3037, 0.5650],
  2575. [-1.2329, 1.9883, 1.0551]])
  2576. """.format(**common_args),
  2577. )
  2578. add_docstr(
  2579. torch.logcumsumexp,
  2580. r"""
  2581. logcumsumexp(input, dim, *, out=None) -> Tensor
  2582. Returns the logarithm of the cumulative summation of the exponentiation of
  2583. elements of :attr:`input` in the dimension :attr:`dim`.
  2584. For summation index :math:`j` given by `dim` and other indices :math:`i`, the result is
  2585. .. math::
  2586. \text{{logcumsumexp}}(x)_{{ij}} = \log \sum\limits_{{k=0}}^{{j}} \exp(x_{{ik}})
  2587. Args:
  2588. {input}
  2589. dim (int): the dimension to do the operation over
  2590. Keyword args:
  2591. {out}
  2592. Example::
  2593. >>> a = torch.randn(10)
  2594. >>> torch.logcumsumexp(a, dim=0)
  2595. tensor([-0.42296738, -0.04462666, 0.86278635, 0.94622083, 1.05277811,
  2596. 1.39202815, 1.83525007, 1.84492621, 2.06084887, 2.06844475]))
  2597. """.format(**reduceops_common_args),
  2598. )
  2599. add_docstr(
  2600. torch.cummax,
  2601. r"""
  2602. cummax(input, dim, *, out=None) -> (Tensor, LongTensor)
  2603. Returns a namedtuple ``(values, indices)`` where ``values`` is the cumulative maximum of
  2604. elements of :attr:`input` in the dimension :attr:`dim`. And ``indices`` is the index
  2605. location of each maximum value found in the dimension :attr:`dim`.
  2606. .. math::
  2607. y_i = max(x_1, x_2, x_3, \dots, x_i)
  2608. Args:
  2609. {input}
  2610. dim (int): the dimension to do the operation over
  2611. Keyword args:
  2612. out (tuple, optional): the result tuple of two output tensors (values, indices)
  2613. Example::
  2614. >>> a = torch.randn(10)
  2615. >>> a
  2616. tensor([-0.3449, -1.5447, 0.0685, -1.5104, -1.1706, 0.2259, 1.4696, -1.3284,
  2617. 1.9946, -0.8209])
  2618. >>> torch.cummax(a, dim=0)
  2619. torch.return_types.cummax(
  2620. values=tensor([-0.3449, -0.3449, 0.0685, 0.0685, 0.0685, 0.2259, 1.4696, 1.4696,
  2621. 1.9946, 1.9946]),
  2622. indices=tensor([0, 0, 2, 2, 2, 5, 6, 6, 8, 8]))
  2623. """.format(**reduceops_common_args),
  2624. )
  2625. add_docstr(
  2626. torch.cummin,
  2627. r"""
  2628. cummin(input, dim, *, out=None) -> (Tensor, LongTensor)
  2629. Returns a namedtuple ``(values, indices)`` where ``values`` is the cumulative minimum of
  2630. elements of :attr:`input` in the dimension :attr:`dim`. And ``indices`` is the index
  2631. location of each maximum value found in the dimension :attr:`dim`.
  2632. .. math::
  2633. y_i = min(x_1, x_2, x_3, \dots, x_i)
  2634. Args:
  2635. {input}
  2636. dim (int): the dimension to do the operation over
  2637. Keyword args:
  2638. out (tuple, optional): the result tuple of two output tensors (values, indices)
  2639. Example::
  2640. >>> a = torch.randn(10)
  2641. >>> a
  2642. tensor([-0.2284, -0.6628, 0.0975, 0.2680, -1.3298, -0.4220, -0.3885, 1.1762,
  2643. 0.9165, 1.6684])
  2644. >>> torch.cummin(a, dim=0)
  2645. torch.return_types.cummin(
  2646. values=tensor([-0.2284, -0.6628, -0.6628, -0.6628, -1.3298, -1.3298, -1.3298, -1.3298,
  2647. -1.3298, -1.3298]),
  2648. indices=tensor([0, 1, 1, 1, 4, 4, 4, 4, 4, 4]))
  2649. """.format(**reduceops_common_args),
  2650. )
  2651. add_docstr(
  2652. torch.cumprod,
  2653. r"""
  2654. cumprod(input, dim, *, dtype=None, out=None) -> Tensor
  2655. Returns the cumulative product of elements of :attr:`input` in the dimension
  2656. :attr:`dim`.
  2657. For example, if :attr:`input` is a vector of size N, the result will also be
  2658. a vector of size N, with elements.
  2659. .. math::
  2660. y_i = x_1 \times x_2\times x_3\times \dots \times x_i
  2661. Args:
  2662. {input}
  2663. dim (int): the dimension to do the operation over
  2664. Keyword args:
  2665. {dtype}
  2666. {out}
  2667. Example::
  2668. >>> a = torch.randn(10)
  2669. >>> a
  2670. tensor([ 0.6001, 0.2069, -0.1919, 0.9792, 0.6727, 1.0062, 0.4126,
  2671. -0.2129, -0.4206, 0.1968])
  2672. >>> torch.cumprod(a, dim=0)
  2673. tensor([ 0.6001, 0.1241, -0.0238, -0.0233, -0.0157, -0.0158, -0.0065,
  2674. 0.0014, -0.0006, -0.0001])
  2675. >>> a[5] = 0.0
  2676. >>> torch.cumprod(a, dim=0)
  2677. tensor([ 0.6001, 0.1241, -0.0238, -0.0233, -0.0157, -0.0000, -0.0000,
  2678. 0.0000, -0.0000, -0.0000])
  2679. """.format(**reduceops_common_args),
  2680. )
  2681. add_docstr(
  2682. torch.cumsum,
  2683. r"""
  2684. cumsum(input, dim, *, dtype=None, out=None) -> Tensor
  2685. Returns the cumulative sum of elements of :attr:`input` in the dimension
  2686. :attr:`dim`.
  2687. For example, if :attr:`input` is a vector of size N, the result will also be
  2688. a vector of size N, with elements.
  2689. .. math::
  2690. y_i = x_1 + x_2 + x_3 + \dots + x_i
  2691. Args:
  2692. {input}
  2693. dim (int): the dimension to do the operation over
  2694. Keyword args:
  2695. {dtype}
  2696. {out}
  2697. Example::
  2698. >>> a = torch.randint(1, 20, (10,))
  2699. >>> a
  2700. tensor([13, 7, 3, 10, 13, 3, 15, 10, 9, 10])
  2701. >>> torch.cumsum(a, dim=0)
  2702. tensor([13, 20, 23, 33, 46, 49, 64, 74, 83, 93])
  2703. """.format(**reduceops_common_args),
  2704. )
  2705. add_docstr(
  2706. torch.count_nonzero,
  2707. r"""
  2708. count_nonzero(input, dim=None) -> Tensor
  2709. Counts the number of non-zero values in the tensor :attr:`input` along the given :attr:`dim`.
  2710. If no dim is specified then all non-zeros in the tensor are counted.
  2711. Args:
  2712. {input}
  2713. dim (int or tuple of ints, optional): Dim or tuple of dims along which to count non-zeros.
  2714. Example::
  2715. >>> x = torch.zeros(3,3)
  2716. >>> x[torch.randn(3,3) > 0.5] = 1
  2717. >>> x
  2718. tensor([[0., 1., 1.],
  2719. [0., 0., 0.],
  2720. [0., 0., 1.]])
  2721. >>> torch.count_nonzero(x)
  2722. tensor(3)
  2723. >>> torch.count_nonzero(x, dim=0)
  2724. tensor([0, 1, 2])
  2725. """.format(**reduceops_common_args),
  2726. )
  2727. add_docstr(
  2728. torch.dequantize,
  2729. r"""
  2730. dequantize(tensor) -> Tensor
  2731. Returns an fp32 Tensor by dequantizing a quantized Tensor
  2732. Args:
  2733. tensor (Tensor): A quantized Tensor
  2734. .. function:: dequantize(tensors) -> sequence of Tensors
  2735. :noindex:
  2736. Given a list of quantized Tensors, dequantize them and return a list of fp32 Tensors
  2737. Args:
  2738. tensors (sequence of Tensors): A list of quantized Tensors
  2739. """,
  2740. )
  2741. add_docstr(
  2742. torch.diag,
  2743. r"""
  2744. diag(input, diagonal=0, *, out=None) -> Tensor
  2745. - If :attr:`input` is a vector (1-D tensor), then returns a 2-D square tensor
  2746. with the elements of :attr:`input` as the diagonal.
  2747. - If :attr:`input` is a matrix (2-D tensor), then returns a 1-D tensor with
  2748. the diagonal elements of :attr:`input`.
  2749. The argument :attr:`diagonal` controls which diagonal to consider:
  2750. - If :attr:`diagonal` = 0, it is the main diagonal.
  2751. - If :attr:`diagonal` > 0, it is above the main diagonal.
  2752. - If :attr:`diagonal` < 0, it is below the main diagonal.
  2753. Args:
  2754. {input}
  2755. diagonal (int, optional): the diagonal to consider
  2756. Keyword args:
  2757. {out}
  2758. .. seealso::
  2759. :func:`torch.diagonal` always returns the diagonal of its input.
  2760. :func:`torch.diagflat` always constructs a tensor with diagonal elements
  2761. specified by the input.
  2762. Examples:
  2763. Get the square matrix where the input vector is the diagonal::
  2764. >>> a = torch.randn(3)
  2765. >>> a
  2766. tensor([ 0.5950,-0.0872, 2.3298])
  2767. >>> torch.diag(a)
  2768. tensor([[ 0.5950, 0.0000, 0.0000],
  2769. [ 0.0000,-0.0872, 0.0000],
  2770. [ 0.0000, 0.0000, 2.3298]])
  2771. >>> torch.diag(a, 1)
  2772. tensor([[ 0.0000, 0.5950, 0.0000, 0.0000],
  2773. [ 0.0000, 0.0000,-0.0872, 0.0000],
  2774. [ 0.0000, 0.0000, 0.0000, 2.3298],
  2775. [ 0.0000, 0.0000, 0.0000, 0.0000]])
  2776. Get the k-th diagonal of a given matrix::
  2777. >>> a = torch.randn(3, 3)
  2778. >>> a
  2779. tensor([[-0.4264, 0.0255,-0.1064],
  2780. [ 0.8795,-0.2429, 0.1374],
  2781. [ 0.1029,-0.6482,-1.6300]])
  2782. >>> torch.diag(a, 0)
  2783. tensor([-0.4264,-0.2429,-1.6300])
  2784. >>> torch.diag(a, 1)
  2785. tensor([ 0.0255, 0.1374])
  2786. """.format(**common_args),
  2787. )
  2788. add_docstr(
  2789. torch.diag_embed,
  2790. r"""
  2791. diag_embed(input, offset=0, dim1=-2, dim2=-1) -> Tensor
  2792. Creates a tensor whose diagonals of certain 2D planes (specified by
  2793. :attr:`dim1` and :attr:`dim2`) are filled by :attr:`input`.
  2794. To facilitate creating batched diagonal matrices, the 2D planes formed by
  2795. the last two dimensions of the returned tensor are chosen by default.
  2796. The argument :attr:`offset` controls which diagonal to consider:
  2797. - If :attr:`offset` = 0, it is the main diagonal.
  2798. - If :attr:`offset` > 0, it is above the main diagonal.
  2799. - If :attr:`offset` < 0, it is below the main diagonal.
  2800. The size of the new matrix will be calculated to make the specified diagonal
  2801. of the size of the last input dimension.
  2802. Note that for :attr:`offset` other than :math:`0`, the order of :attr:`dim1`
  2803. and :attr:`dim2` matters. Exchanging them is equivalent to changing the
  2804. sign of :attr:`offset`.
  2805. Applying :meth:`torch.diagonal` to the output of this function with
  2806. the same arguments yields a matrix identical to input. However,
  2807. :meth:`torch.diagonal` has different default dimensions, so those
  2808. need to be explicitly specified.
  2809. Args:
  2810. {input} Must be at least 1-dimensional.
  2811. offset (int, optional): which diagonal to consider. Default: 0
  2812. (main diagonal).
  2813. dim1 (int, optional): first dimension with respect to which to
  2814. take diagonal. Default: -2.
  2815. dim2 (int, optional): second dimension with respect to which to
  2816. take diagonal. Default: -1.
  2817. Example::
  2818. >>> a = torch.randn(2, 3)
  2819. >>> torch.diag_embed(a)
  2820. tensor([[[ 1.5410, 0.0000, 0.0000],
  2821. [ 0.0000, -0.2934, 0.0000],
  2822. [ 0.0000, 0.0000, -2.1788]],
  2823. [[ 0.5684, 0.0000, 0.0000],
  2824. [ 0.0000, -1.0845, 0.0000],
  2825. [ 0.0000, 0.0000, -1.3986]]])
  2826. >>> torch.diag_embed(a, offset=1, dim1=0, dim2=2)
  2827. tensor([[[ 0.0000, 1.5410, 0.0000, 0.0000],
  2828. [ 0.0000, 0.5684, 0.0000, 0.0000]],
  2829. [[ 0.0000, 0.0000, -0.2934, 0.0000],
  2830. [ 0.0000, 0.0000, -1.0845, 0.0000]],
  2831. [[ 0.0000, 0.0000, 0.0000, -2.1788],
  2832. [ 0.0000, 0.0000, 0.0000, -1.3986]],
  2833. [[ 0.0000, 0.0000, 0.0000, 0.0000],
  2834. [ 0.0000, 0.0000, 0.0000, 0.0000]]])
  2835. """.format(**common_args),
  2836. )
  2837. add_docstr(
  2838. torch.diagflat,
  2839. r"""
  2840. diagflat(input, offset=0) -> Tensor
  2841. - If :attr:`input` is a vector (1-D tensor), then returns a 2-D square tensor
  2842. with the elements of :attr:`input` as the diagonal.
  2843. - If :attr:`input` is a tensor with more than one dimension, then returns a
  2844. 2-D tensor with diagonal elements equal to a flattened :attr:`input`.
  2845. The argument :attr:`offset` controls which diagonal to consider:
  2846. - If :attr:`offset` = 0, it is the main diagonal.
  2847. - If :attr:`offset` > 0, it is above the main diagonal.
  2848. - If :attr:`offset` < 0, it is below the main diagonal.
  2849. Args:
  2850. {input}
  2851. offset (int, optional): the diagonal to consider. Default: 0 (main
  2852. diagonal).
  2853. Examples::
  2854. >>> a = torch.randn(3)
  2855. >>> a
  2856. tensor([-0.2956, -0.9068, 0.1695])
  2857. >>> torch.diagflat(a)
  2858. tensor([[-0.2956, 0.0000, 0.0000],
  2859. [ 0.0000, -0.9068, 0.0000],
  2860. [ 0.0000, 0.0000, 0.1695]])
  2861. >>> torch.diagflat(a, 1)
  2862. tensor([[ 0.0000, -0.2956, 0.0000, 0.0000],
  2863. [ 0.0000, 0.0000, -0.9068, 0.0000],
  2864. [ 0.0000, 0.0000, 0.0000, 0.1695],
  2865. [ 0.0000, 0.0000, 0.0000, 0.0000]])
  2866. >>> a = torch.randn(2, 2)
  2867. >>> a
  2868. tensor([[ 0.2094, -0.3018],
  2869. [-0.1516, 1.9342]])
  2870. >>> torch.diagflat(a)
  2871. tensor([[ 0.2094, 0.0000, 0.0000, 0.0000],
  2872. [ 0.0000, -0.3018, 0.0000, 0.0000],
  2873. [ 0.0000, 0.0000, -0.1516, 0.0000],
  2874. [ 0.0000, 0.0000, 0.0000, 1.9342]])
  2875. """.format(**common_args),
  2876. )
  2877. add_docstr(
  2878. torch.diagonal,
  2879. r"""
  2880. diagonal(input, offset=0, dim1=0, dim2=1) -> Tensor
  2881. Returns a partial view of :attr:`input` with the its diagonal elements
  2882. with respect to :attr:`dim1` and :attr:`dim2` appended as a dimension
  2883. at the end of the shape.
  2884. The argument :attr:`offset` controls which diagonal to consider:
  2885. - If :attr:`offset` = 0, it is the main diagonal.
  2886. - If :attr:`offset` > 0, it is above the main diagonal.
  2887. - If :attr:`offset` < 0, it is below the main diagonal.
  2888. Applying :meth:`torch.diag_embed` to the output of this function with
  2889. the same arguments yields a diagonal matrix with the diagonal entries
  2890. of the input. However, :meth:`torch.diag_embed` has different default
  2891. dimensions, so those need to be explicitly specified.
  2892. Args:
  2893. {input} Must be at least 2-dimensional.
  2894. offset (int, optional): which diagonal to consider. Default: 0
  2895. (main diagonal).
  2896. dim1 (int, optional): first dimension with respect to which to
  2897. take diagonal. Default: 0.
  2898. dim2 (int, optional): second dimension with respect to which to
  2899. take diagonal. Default: 1.
  2900. .. note:: To take a batch diagonal, pass in dim1=-2, dim2=-1.
  2901. Examples::
  2902. >>> a = torch.randn(3, 3)
  2903. >>> a
  2904. tensor([[-1.0854, 1.1431, -0.1752],
  2905. [ 0.8536, -0.0905, 0.0360],
  2906. [ 0.6927, -0.3735, -0.4945]])
  2907. >>> torch.diagonal(a)
  2908. tensor([-1.0854, -0.0905, -0.4945])
  2909. >>> torch.diagonal(a, 1)
  2910. tensor([ 1.1431, 0.0360])
  2911. >>> b = torch.randn(2, 5)
  2912. >>> b
  2913. tensor([[-1.7948, -1.2731, -0.3181, 2.0200, -1.6745],
  2914. [ 1.8262, -1.5049, 0.4114, 1.0704, -1.2607]])
  2915. >>> torch.diagonal(b, 1, 1, 0)
  2916. tensor([1.8262])
  2917. >>> x = torch.randn(2, 5, 4, 2)
  2918. >>> torch.diagonal(x, offset=-1, dim1=1, dim2=2)
  2919. tensor([[[-1.2631, 0.3755, -1.5977, -1.8172],
  2920. [-1.1065, 1.0401, -0.2235, -0.7938]],
  2921. [[-1.7325, -0.3081, 0.6166, 0.2335],
  2922. [ 1.0500, 0.7336, -0.3836, -1.1015]]])
  2923. """.format(**common_args),
  2924. )
  2925. add_docstr(
  2926. torch.diagonal_scatter,
  2927. r"""
  2928. diagonal_scatter(input, src, offset=0, dim1=0, dim2=1) -> Tensor
  2929. Embeds the values of the :attr:`src` tensor into :attr:`input` along
  2930. the diagonal elements of :attr:`input`, with respect to :attr:`dim1`
  2931. and :attr:`dim2`.
  2932. This function returns a tensor with fresh storage; it does not
  2933. return a view.
  2934. The argument :attr:`offset` controls which diagonal to consider:
  2935. - If :attr:`offset` = 0, it is the main diagonal.
  2936. - If :attr:`offset` > 0, it is above the main diagonal.
  2937. - If :attr:`offset` < 0, it is below the main diagonal.
  2938. Args:
  2939. {input} Must be at least 2-dimensional.
  2940. src (Tensor): the tensor to embed into :attr:`input`.
  2941. offset (int, optional): which diagonal to consider. Default: 0
  2942. (main diagonal).
  2943. dim1 (int, optional): first dimension with respect to which to
  2944. take diagonal. Default: 0.
  2945. dim2 (int, optional): second dimension with respect to which to
  2946. take diagonal. Default: 1.
  2947. .. note::
  2948. :attr:`src` must be of the proper size in order to be embedded
  2949. into :attr:`input`. Specifically, it should have the same shape as
  2950. ``torch.diagonal(input, offset, dim1, dim2)``
  2951. Examples::
  2952. >>> a = torch.zeros(3, 3)
  2953. >>> a
  2954. tensor([[0., 0., 0.],
  2955. [0., 0., 0.],
  2956. [0., 0., 0.]])
  2957. >>> torch.diagonal_scatter(a, torch.ones(3), 0)
  2958. tensor([[1., 0., 0.],
  2959. [0., 1., 0.],
  2960. [0., 0., 1.]])
  2961. >>> torch.diagonal_scatter(a, torch.ones(2), 1)
  2962. tensor([[0., 1., 0.],
  2963. [0., 0., 1.],
  2964. [0., 0., 0.]])
  2965. """.format(**common_args),
  2966. )
  2967. add_docstr(
  2968. torch.as_strided_scatter,
  2969. r"""
  2970. as_strided_scatter(input, src, size, stride, storage_offset=None) -> Tensor
  2971. Embeds the values of the :attr:`src` tensor into :attr:`input` along
  2972. the elements corresponding to the result of calling
  2973. input.as_strided(size, stride, storage_offset).
  2974. This function returns a tensor with fresh storage; it does not
  2975. return a view.
  2976. Args:
  2977. {input}
  2978. size (tuple or ints): the shape of the output tensor
  2979. stride (tuple or ints): the stride of the output tensor
  2980. storage_offset (int, optional): the offset in the underlying storage of the output tensor
  2981. .. note::
  2982. :attr:`src` must be of the proper size in order to be embedded
  2983. into :attr:`input`. Specifically, it should have the same shape as
  2984. `torch.as_strided(input, size, stride, storage_offset)`
  2985. Example::
  2986. >>> a = torch.arange(4).reshape(2, 2) + 1
  2987. >>> a
  2988. tensor([[1, 2],
  2989. [3, 4]])
  2990. >>> b = torch.zeros(3, 3)
  2991. >>> b
  2992. tensor([[0., 0., 0.],
  2993. [0., 0., 0.],
  2994. [0., 0., 0.]])
  2995. >>> torch.as_strided_scatter(b, a, (2, 2), (1, 2))
  2996. tensor([[1., 3., 2.],
  2997. [4., 0., 0.],
  2998. [0., 0., 0.]])
  2999. """.format(**common_args),
  3000. )
  3001. add_docstr(
  3002. torch.diff,
  3003. r"""
  3004. diff(input, n=1, dim=-1, prepend=None, append=None) -> Tensor
  3005. Computes the n-th forward difference along the given dimension.
  3006. The first-order differences are given by `out[i] = input[i + 1] - input[i]`. Higher-order
  3007. differences are calculated by using :func:`torch.diff` recursively.
  3008. Args:
  3009. input (Tensor): the tensor to compute the differences on
  3010. n (int, optional): the number of times to recursively compute the difference
  3011. dim (int, optional): the dimension to compute the difference along.
  3012. Default is the last dimension.
  3013. prepend, append (Tensor, optional): values to prepend or append to
  3014. :attr:`input` along :attr:`dim` before computing the difference.
  3015. Their dimensions must be equivalent to that of input, and their shapes
  3016. must match input's shape except on :attr:`dim`.
  3017. Keyword args:
  3018. {out}
  3019. Example::
  3020. >>> a = torch.tensor([1, 3, 2])
  3021. >>> torch.diff(a)
  3022. tensor([ 2, -1])
  3023. >>> b = torch.tensor([4, 5])
  3024. >>> torch.diff(a, append=b)
  3025. tensor([ 2, -1, 2, 1])
  3026. >>> c = torch.tensor([[1, 2, 3], [3, 4, 5]])
  3027. >>> torch.diff(c, dim=0)
  3028. tensor([[2, 2, 2]])
  3029. >>> torch.diff(c, dim=1)
  3030. tensor([[1, 1],
  3031. [1, 1]])
  3032. """.format(**common_args),
  3033. )
  3034. add_docstr(
  3035. torch.digamma,
  3036. r"""
  3037. digamma(input, *, out=None) -> Tensor
  3038. Alias for :func:`torch.special.digamma`.
  3039. """,
  3040. )
  3041. add_docstr(
  3042. torch.dist,
  3043. r"""
  3044. dist(input, other, p=2) -> Tensor
  3045. Returns the p-norm of (:attr:`input` - :attr:`other`)
  3046. The shapes of :attr:`input` and :attr:`other` must be
  3047. :ref:`broadcastable <broadcasting-semantics>`.
  3048. Args:
  3049. {input}
  3050. other (Tensor): the Right-hand-side input tensor
  3051. p (float, optional): the norm to be computed
  3052. Example::
  3053. >>> x = torch.randn(4)
  3054. >>> x
  3055. tensor([-1.5393, -0.8675, 0.5916, 1.6321])
  3056. >>> y = torch.randn(4)
  3057. >>> y
  3058. tensor([ 0.0967, -1.0511, 0.6295, 0.8360])
  3059. >>> torch.dist(x, y, 3.5)
  3060. tensor(1.6727)
  3061. >>> torch.dist(x, y, 3)
  3062. tensor(1.6973)
  3063. >>> torch.dist(x, y, 0)
  3064. tensor(4.)
  3065. >>> torch.dist(x, y, 1)
  3066. tensor(2.6537)
  3067. """.format(**common_args),
  3068. )
  3069. add_docstr(
  3070. torch.div,
  3071. r"""
  3072. div(input, other, *, rounding_mode=None, out=None) -> Tensor
  3073. Divides each element of the input ``input`` by the corresponding element of
  3074. :attr:`other`.
  3075. .. math::
  3076. \text{{out}}_i = \frac{{\text{{input}}_i}}{{\text{{other}}_i}}
  3077. .. note::
  3078. By default, this performs a "true" division like Python 3.
  3079. See the :attr:`rounding_mode` argument for floor division.
  3080. Supports :ref:`broadcasting to a common shape <broadcasting-semantics>`,
  3081. :ref:`type promotion <type-promotion-doc>`, and integer, float, and complex inputs.
  3082. Always promotes integer types to the default scalar type.
  3083. Args:
  3084. input (Tensor): the dividend
  3085. other (Tensor or Number): the divisor
  3086. Keyword args:
  3087. rounding_mode (str, optional): Type of rounding applied to the result:
  3088. * None - default behavior. Performs no rounding and, if both :attr:`input` and
  3089. :attr:`other` are integer types, promotes the inputs to the default scalar type.
  3090. Equivalent to true division in Python (the ``/`` operator) and NumPy's ``np.true_divide``.
  3091. * ``"trunc"`` - rounds the results of the division towards zero.
  3092. Equivalent to C-style integer division.
  3093. * ``"floor"`` - rounds the results of the division down.
  3094. Equivalent to floor division in Python (the ``//`` operator) and NumPy's ``np.floor_divide``.
  3095. {out}
  3096. Examples::
  3097. >>> x = torch.tensor([ 0.3810, 1.2774, -0.2972, -0.3719, 0.4637])
  3098. >>> torch.div(x, 0.5)
  3099. tensor([ 0.7620, 2.5548, -0.5944, -0.7438, 0.9274])
  3100. >>> a = torch.tensor([[-0.3711, -1.9353, -0.4605, -0.2917],
  3101. ... [ 0.1815, -1.0111, 0.9805, -1.5923],
  3102. ... [ 0.1062, 1.4581, 0.7759, -1.2344],
  3103. ... [-0.1830, -0.0313, 1.1908, -1.4757]])
  3104. >>> b = torch.tensor([ 0.8032, 0.2930, -0.8113, -0.2308])
  3105. >>> torch.div(a, b)
  3106. tensor([[-0.4620, -6.6051, 0.5676, 1.2639],
  3107. [ 0.2260, -3.4509, -1.2086, 6.8990],
  3108. [ 0.1322, 4.9764, -0.9564, 5.3484],
  3109. [-0.2278, -0.1068, -1.4678, 6.3938]])
  3110. >>> torch.div(a, b, rounding_mode='trunc')
  3111. tensor([[-0., -6., 0., 1.],
  3112. [ 0., -3., -1., 6.],
  3113. [ 0., 4., -0., 5.],
  3114. [-0., -0., -1., 6.]])
  3115. >>> torch.div(a, b, rounding_mode='floor')
  3116. tensor([[-1., -7., 0., 1.],
  3117. [ 0., -4., -2., 6.],
  3118. [ 0., 4., -1., 5.],
  3119. [-1., -1., -2., 6.]])
  3120. """.format(**common_args),
  3121. )
  3122. add_docstr(
  3123. torch.divide,
  3124. r"""
  3125. divide(input, other, *, rounding_mode=None, out=None) -> Tensor
  3126. Alias for :func:`torch.div`.
  3127. """,
  3128. )
  3129. add_docstr(
  3130. torch.dot,
  3131. r"""
  3132. dot(input, tensor, *, out=None) -> Tensor
  3133. Computes the dot product of two 1D tensors.
  3134. .. note::
  3135. Unlike NumPy's dot, torch.dot intentionally only supports computing the dot product
  3136. of two 1D tensors with the same number of elements.
  3137. Args:
  3138. input (Tensor): first tensor in the dot product, must be 1D.
  3139. tensor (Tensor): second tensor in the dot product, must be 1D.
  3140. Keyword args:
  3141. {out}
  3142. Example::
  3143. >>> torch.dot(torch.tensor([2, 3]), torch.tensor([2, 1]))
  3144. tensor(7)
  3145. >>> t1, t2 = torch.tensor([0, 1]), torch.tensor([2, 3])
  3146. >>> torch.dot(t1, t2)
  3147. tensor(3)
  3148. """.format(**common_args),
  3149. )
  3150. add_docstr(
  3151. torch.vdot,
  3152. r"""
  3153. vdot(input, other, *, out=None) -> Tensor
  3154. Computes the dot product of two 1D vectors along a dimension.
  3155. In symbols, this function computes
  3156. .. math::
  3157. \sum_{i=1}^n \overline{x_i}y_i.
  3158. where :math:`\overline{x_i}` denotes the conjugate for complex
  3159. vectors, and it is the identity for real vectors.
  3160. .. note::
  3161. Unlike NumPy's vdot, torch.vdot intentionally only supports computing the dot product
  3162. of two 1D tensors with the same number of elements.
  3163. .. seealso::
  3164. :func:`torch.linalg.vecdot` computes the dot product of two batches of vectors along a dimension.
  3165. Args:
  3166. input (Tensor): first tensor in the dot product, must be 1D. Its conjugate is used if it's complex.
  3167. other (Tensor): second tensor in the dot product, must be 1D.
  3168. Keyword args:
  3169. """
  3170. + rf"""
  3171. .. note:: {common_args["out"]}
  3172. """
  3173. + r"""
  3174. Example::
  3175. >>> torch.vdot(torch.tensor([2, 3]), torch.tensor([2, 1]))
  3176. tensor(7)
  3177. >>> a = torch.tensor((1 +2j, 3 - 1j))
  3178. >>> b = torch.tensor((2 +1j, 4 - 0j))
  3179. >>> torch.vdot(a, b)
  3180. tensor([16.+1.j])
  3181. >>> torch.vdot(b, a)
  3182. tensor([16.-1.j])
  3183. """,
  3184. )
  3185. add_docstr(
  3186. torch.eq,
  3187. r"""
  3188. eq(input, other, *, out=None) -> Tensor
  3189. Computes element-wise equality
  3190. The second argument can be a number or a tensor whose shape is
  3191. :ref:`broadcastable <broadcasting-semantics>` with the first argument.
  3192. Args:
  3193. input (Tensor): the tensor to compare
  3194. other (Tensor or float): the tensor or value to compare
  3195. Keyword args:
  3196. {out}
  3197. Returns:
  3198. A boolean tensor that is True where :attr:`input` is equal to :attr:`other` and False elsewhere
  3199. Example::
  3200. >>> torch.eq(torch.tensor([[1, 2], [3, 4]]), torch.tensor([[1, 1], [4, 4]]))
  3201. tensor([[ True, False],
  3202. [False, True]])
  3203. """.format(**common_args),
  3204. )
  3205. add_docstr(
  3206. torch.equal,
  3207. r"""
  3208. equal(input, other) -> bool
  3209. ``True`` if two tensors have the same size and elements, ``False`` otherwise.
  3210. .. note::
  3211. Tensors containing NaNs are never equal to each other. Additionally, this function does not
  3212. differentiate between the data types of the tensors during comparison. For more thorough tensor checks,
  3213. use :meth:`torch.testing.assert_close`.
  3214. Example::
  3215. >>> torch.equal(torch.tensor([1, 2]), torch.tensor([1, 2]))
  3216. True
  3217. >>> torch.equal(torch.tensor([3, torch.nan]), torch.tensor([3, torch.nan]))
  3218. False
  3219. >>> torch.equal(torch.tensor([1, 2, 3], dtype=torch.int32), torch.tensor([1, 2, 3], dtype=torch.float32))
  3220. True
  3221. """,
  3222. )
  3223. add_docstr(
  3224. torch.erf,
  3225. r"""
  3226. erf(input, *, out=None) -> Tensor
  3227. Alias for :func:`torch.special.erf`.
  3228. """,
  3229. )
  3230. add_docstr(
  3231. torch.erfc,
  3232. r"""
  3233. erfc(input, *, out=None) -> Tensor
  3234. Alias for :func:`torch.special.erfc`.
  3235. """,
  3236. )
  3237. add_docstr(
  3238. torch.erfinv,
  3239. r"""
  3240. erfinv(input, *, out=None) -> Tensor
  3241. Alias for :func:`torch.special.erfinv`.
  3242. """,
  3243. )
  3244. add_docstr(
  3245. torch.exp,
  3246. r"""
  3247. exp(input, *, out=None) -> Tensor
  3248. Returns a new tensor with the exponential of the elements
  3249. of the input tensor :attr:`input`.
  3250. .. math::
  3251. y_{i} = e^{x_{i}}
  3252. """
  3253. + r"""
  3254. Args:
  3255. {input}
  3256. Keyword args:
  3257. {out}
  3258. Example::
  3259. >>> torch.exp(torch.tensor([0, math.log(2.)]))
  3260. tensor([ 1., 2.])
  3261. """.format(**common_args),
  3262. )
  3263. add_docstr(
  3264. torch.exp2,
  3265. r"""
  3266. exp2(input, *, out=None) -> Tensor
  3267. Alias for :func:`torch.special.exp2`.
  3268. """,
  3269. )
  3270. add_docstr(
  3271. torch.expm1,
  3272. r"""
  3273. expm1(input, *, out=None) -> Tensor
  3274. Alias for :func:`torch.special.expm1`.
  3275. """,
  3276. )
  3277. add_docstr(
  3278. torch.eye,
  3279. r"""
  3280. eye(n, m=None, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) -> Tensor
  3281. Returns a 2-D tensor with ones on the diagonal and zeros elsewhere.
  3282. Args:
  3283. n (int): the number of rows
  3284. m (int, optional): the number of columns with default being :attr:`n`
  3285. Keyword arguments:
  3286. {out}
  3287. {dtype}
  3288. {layout}
  3289. {device}
  3290. {requires_grad}
  3291. Returns:
  3292. Tensor: A 2-D tensor with ones on the diagonal and zeros elsewhere
  3293. Example::
  3294. >>> torch.eye(3)
  3295. tensor([[ 1., 0., 0.],
  3296. [ 0., 1., 0.],
  3297. [ 0., 0., 1.]])
  3298. """.format(**factory_common_args),
  3299. )
  3300. add_docstr(
  3301. torch.floor,
  3302. r"""
  3303. floor(input, *, out=None) -> Tensor
  3304. Returns a new tensor with the floor of the elements of :attr:`input`,
  3305. the largest integer less than or equal to each element.
  3306. For integer inputs, follows the array-api convention of returning a
  3307. copy of the input tensor.
  3308. .. math::
  3309. \text{out}_{i} = \left\lfloor \text{input}_{i} \right\rfloor
  3310. """
  3311. + r"""
  3312. Args:
  3313. {input}
  3314. Keyword args:
  3315. {out}
  3316. Example::
  3317. >>> a = torch.randn(4)
  3318. >>> a
  3319. tensor([-0.8166, 1.5308, -0.2530, -0.2091])
  3320. >>> torch.floor(a)
  3321. tensor([-1., 1., -1., -1.])
  3322. """.format(**common_args),
  3323. )
  3324. add_docstr(
  3325. torch.floor_divide,
  3326. r"""
  3327. floor_divide(input, other, *, out=None) -> Tensor
  3328. .. note::
  3329. Before PyTorch 1.13 :func:`torch.floor_divide` incorrectly performed
  3330. truncation division. To restore the previous behavior use
  3331. :func:`torch.div` with ``rounding_mode='trunc'``.
  3332. Computes :attr:`input` divided by :attr:`other`, elementwise, and floors
  3333. the result.
  3334. .. math::
  3335. \text{{out}}_i = \text{floor} \left( \frac{{\text{{input}}_i}}{{\text{{other}}_i}} \right)
  3336. """
  3337. + r"""
  3338. Supports broadcasting to a common shape, type promotion, and integer and float inputs.
  3339. Args:
  3340. input (Tensor or Number): the dividend
  3341. other (Tensor or Number): the divisor
  3342. Keyword args:
  3343. {out}
  3344. Example::
  3345. >>> a = torch.tensor([4.0, 3.0])
  3346. >>> b = torch.tensor([2.0, 2.0])
  3347. >>> torch.floor_divide(a, b)
  3348. tensor([2.0, 1.0])
  3349. >>> torch.floor_divide(a, 1.4)
  3350. tensor([2.0, 2.0])
  3351. """.format(**common_args),
  3352. )
  3353. add_docstr(
  3354. torch.fmod,
  3355. r"""
  3356. fmod(input, other, *, out=None) -> Tensor
  3357. Applies C++'s `std::fmod <https://en.cppreference.com/w/cpp/numeric/math/fmod>`_ entrywise.
  3358. The result has the same sign as the dividend :attr:`input` and its absolute value
  3359. is less than that of :attr:`other`.
  3360. This function may be defined in terms of :func:`torch.div` as
  3361. .. code:: python
  3362. torch.fmod(a, b) == a - a.div(b, rounding_mode="trunc") * b
  3363. Supports :ref:`broadcasting to a common shape <broadcasting-semantics>`,
  3364. :ref:`type promotion <type-promotion-doc>`, and integer and float inputs.
  3365. .. note::
  3366. When the divisor is zero, returns ``NaN`` for floating point dtypes
  3367. on both CPU and GPU; raises ``RuntimeError`` for integer division by
  3368. zero on CPU; Integer division by zero on GPU may return any value.
  3369. .. note::
  3370. Complex inputs are not supported. In some cases, it is not mathematically
  3371. possible to satisfy the definition of a modulo operation with complex numbers.
  3372. .. seealso::
  3373. :func:`torch.remainder` which implements Python's modulus operator.
  3374. This one is defined using division rounding down the result.
  3375. Args:
  3376. input (Tensor): the dividend
  3377. other (Tensor or Scalar): the divisor
  3378. Keyword args:
  3379. {out}
  3380. Example::
  3381. >>> torch.fmod(torch.tensor([-3., -2, -1, 1, 2, 3]), 2)
  3382. tensor([-1., -0., -1., 1., 0., 1.])
  3383. >>> torch.fmod(torch.tensor([1, 2, 3, 4, 5]), -1.5)
  3384. tensor([1.0000, 0.5000, 0.0000, 1.0000, 0.5000])
  3385. """.format(**common_args),
  3386. )
  3387. add_docstr(
  3388. torch.frac,
  3389. r"""
  3390. frac(input, *, out=None) -> Tensor
  3391. Computes the fractional portion of each element in :attr:`input`.
  3392. .. math::
  3393. \text{out}_{i} = \text{input}_{i} - \left\lfloor |\text{input}_{i}| \right\rfloor * \operatorname{sgn}(\text{input}_{i})
  3394. Example::
  3395. >>> torch.frac(torch.tensor([1, 2.5, -3.2]))
  3396. tensor([ 0.0000, 0.5000, -0.2000])
  3397. """,
  3398. )
  3399. add_docstr(
  3400. torch.frexp,
  3401. r"""
  3402. frexp(input, *, out=None) -> (Tensor mantissa, Tensor exponent)
  3403. Decomposes :attr:`input` into mantissa and exponent tensors
  3404. such that :math:`\text{input} = \text{mantissa} \times 2^{\text{exponent}}`.
  3405. The range of mantissa is the open interval (-1, 1).
  3406. Supports float inputs.
  3407. Args:
  3408. input (Tensor): the input tensor
  3409. Keyword args:
  3410. out (tuple, optional): the output tensors
  3411. Example::
  3412. >>> x = torch.arange(9.)
  3413. >>> mantissa, exponent = torch.frexp(x)
  3414. >>> mantissa
  3415. tensor([0.0000, 0.5000, 0.5000, 0.7500, 0.5000, 0.6250, 0.7500, 0.8750, 0.5000])
  3416. >>> exponent
  3417. tensor([0, 1, 2, 2, 3, 3, 3, 3, 4], dtype=torch.int32)
  3418. >>> torch.ldexp(mantissa, exponent)
  3419. tensor([0., 1., 2., 3., 4., 5., 6., 7., 8.])
  3420. """,
  3421. )
  3422. add_docstr(
  3423. torch.from_numpy,
  3424. r"""
  3425. from_numpy(ndarray) -> Tensor
  3426. Creates a :class:`Tensor` from a :class:`numpy.ndarray`.
  3427. The returned tensor and :attr:`ndarray` share the same memory. Modifications to
  3428. the tensor will be reflected in the :attr:`ndarray` and vice versa. The returned
  3429. tensor is not resizable.
  3430. It currently accepts :attr:`ndarray` with dtypes of ``numpy.float64``,
  3431. ``numpy.float32``, ``numpy.float16``, ``numpy.complex64``, ``numpy.complex128``,
  3432. ``numpy.int64``, ``numpy.int32``, ``numpy.int16``, ``numpy.int8``, ``numpy.uint8``,
  3433. and ``bool``.
  3434. .. warning::
  3435. Writing to a tensor created from a read-only NumPy array is not supported and will result in undefined behavior.
  3436. Example::
  3437. >>> a = numpy.array([1, 2, 3])
  3438. >>> t = torch.from_numpy(a)
  3439. >>> t
  3440. tensor([ 1, 2, 3])
  3441. >>> t[0] = -1
  3442. >>> a
  3443. array([-1, 2, 3])
  3444. """,
  3445. )
  3446. add_docstr(
  3447. torch.frombuffer,
  3448. r"""
  3449. frombuffer(buffer, *, dtype, count=-1, offset=0, requires_grad=False) -> Tensor
  3450. Creates a 1-dimensional :class:`Tensor` from an object that implements
  3451. the Python buffer protocol.
  3452. Skips the first :attr:`offset` bytes in the buffer, and interprets the rest of
  3453. the raw bytes as a 1-dimensional tensor of type :attr:`dtype` with :attr:`count`
  3454. elements.
  3455. Note that either of the following must be true:
  3456. 1. :attr:`count` is a positive non-zero number, and the total number of bytes
  3457. in the buffer is more than :attr:`offset` plus :attr:`count` times the size
  3458. (in bytes) of :attr:`dtype`.
  3459. 2. :attr:`count` is negative, and the length (number of bytes) of the buffer
  3460. subtracted by the :attr:`offset` is a multiple of the size (in bytes) of
  3461. :attr:`dtype`.
  3462. The returned tensor and buffer share the same memory. Modifications to
  3463. the tensor will be reflected in the buffer and vice versa. The returned
  3464. tensor is not resizable.
  3465. .. note::
  3466. This function increments the reference count for the object that
  3467. owns the shared memory. Therefore, such memory will not be deallocated
  3468. before the returned tensor goes out of scope.
  3469. .. warning::
  3470. This function's behavior is undefined when passed an object implementing
  3471. the buffer protocol whose data is not on the CPU. Doing so is likely to
  3472. cause a segmentation fault.
  3473. .. warning::
  3474. This function does not try to infer the :attr:`dtype` (hence, it is not
  3475. optional). Passing a different :attr:`dtype` than its source may result
  3476. in unexpected behavior.
  3477. Args:
  3478. buffer (object): a Python object that exposes the buffer interface.
  3479. Keyword args:
  3480. dtype (:class:`torch.dtype`): the desired data type of returned tensor.
  3481. count (int, optional): the number of desired elements to be read.
  3482. If negative, all the elements (until the end of the buffer) will be
  3483. read. Default: -1.
  3484. offset (int, optional): the number of bytes to skip at the start of
  3485. the buffer. Default: 0.
  3486. {requires_grad}
  3487. Example::
  3488. >>> import array
  3489. >>> a = array.array('i', [1, 2, 3])
  3490. >>> t = torch.frombuffer(a, dtype=torch.int32)
  3491. >>> t
  3492. tensor([ 1, 2, 3])
  3493. >>> t[0] = -1
  3494. >>> a
  3495. array([-1, 2, 3])
  3496. >>> # Interprets the signed char bytes as 32-bit integers.
  3497. >>> # Each 4 signed char elements will be interpreted as
  3498. >>> # 1 signed 32-bit integer.
  3499. >>> import array
  3500. >>> a = array.array('b', [-1, 0, 0, 0])
  3501. >>> torch.frombuffer(a, dtype=torch.int32)
  3502. tensor([255], dtype=torch.int32)
  3503. """.format(**factory_common_args),
  3504. )
  3505. add_docstr(
  3506. torch.from_file,
  3507. r"""
  3508. from_file(filename, shared=None, size=0, *, dtype=None, layout=None, device=None, pin_memory=False)
  3509. Creates a CPU tensor with a storage backed by a memory-mapped file.
  3510. If ``shared`` is True, then memory is shared between processes. All changes are written to the file.
  3511. If ``shared`` is False, then changes to the tensor do not affect the file.
  3512. ``size`` is the number of elements in the Tensor. If ``shared`` is ``False``, then the file must contain
  3513. at least ``size * sizeof(dtype)`` bytes. If ``shared`` is ``True`` the file will be created if needed.
  3514. .. note::
  3515. Only CPU tensors can be mapped to files.
  3516. .. note::
  3517. For now, tensors with storages backed by a memory-mapped file cannot be created in pinned memory.
  3518. Args:
  3519. filename (str): file name to map
  3520. shared (bool): whether to share memory (whether ``MAP_SHARED`` or ``MAP_PRIVATE`` is passed to the
  3521. underlying `mmap(2) call <https://man7.org/linux/man-pages/man2/mmap.2.html>`_)
  3522. size (int): number of elements in the tensor
  3523. Keyword args:
  3524. {dtype}
  3525. {layout}
  3526. {device}
  3527. {pin_memory}
  3528. Example::
  3529. >>> t = torch.randn(2, 5, dtype=torch.float64)
  3530. >>> t.numpy().tofile('storage.pt')
  3531. >>> t_mapped = torch.from_file('storage.pt', shared=False, size=10, dtype=torch.float64)
  3532. """.format(**factory_common_args),
  3533. )
  3534. add_docstr(
  3535. torch.flatten,
  3536. r"""
  3537. flatten(input, start_dim=0, end_dim=-1) -> Tensor
  3538. Flattens :attr:`input` by reshaping it into a one-dimensional tensor. If :attr:`start_dim` or :attr:`end_dim`
  3539. are passed, only dimensions starting with :attr:`start_dim` and ending with :attr:`end_dim` are flattened.
  3540. The order of elements in :attr:`input` is unchanged.
  3541. Unlike NumPy's flatten, which always copies input's data, this function may return the original object, a view,
  3542. or copy. If no dimensions are flattened, then the original object :attr:`input` is returned. Otherwise, if input can
  3543. be viewed as the flattened shape, then that view is returned. Finally, only if the input cannot be viewed as the
  3544. flattened shape is input's data copied. See :meth:`torch.Tensor.view` for details on when a view will be returned.
  3545. .. note::
  3546. Flattening a zero-dimensional tensor will return a one-dimensional view.
  3547. Args:
  3548. {input}
  3549. start_dim (int): the first dim to flatten
  3550. end_dim (int): the last dim to flatten
  3551. Example::
  3552. >>> t = torch.tensor([[[1, 2],
  3553. ... [3, 4]],
  3554. ... [[5, 6],
  3555. ... [7, 8]]])
  3556. >>> torch.flatten(t)
  3557. tensor([1, 2, 3, 4, 5, 6, 7, 8])
  3558. >>> torch.flatten(t, start_dim=1)
  3559. tensor([[1, 2, 3, 4],
  3560. [5, 6, 7, 8]])
  3561. """.format(**common_args),
  3562. )
  3563. add_docstr(
  3564. torch.unflatten,
  3565. r"""
  3566. unflatten(input, dim, sizes) -> Tensor
  3567. Expands a dimension of the input tensor over multiple dimensions.
  3568. .. seealso::
  3569. :func:`torch.flatten` the inverse of this function. It coalesces several dimensions into one.
  3570. Args:
  3571. {input}
  3572. dim (int): Dimension to be unflattened, specified as an index into
  3573. ``input.shape``.
  3574. sizes (Tuple[int]): New shape of the unflattened dimension.
  3575. One of its elements can be `-1` in which case the corresponding output
  3576. dimension is inferred. Otherwise, the product of ``sizes`` *must*
  3577. equal ``input.shape[dim]``.
  3578. Returns:
  3579. A View of input with the specified dimension unflattened.
  3580. Examples::
  3581. >>> torch.unflatten(torch.randn(3, 4, 1), 1, (2, 2)).shape
  3582. torch.Size([3, 2, 2, 1])
  3583. >>> torch.unflatten(torch.randn(3, 4, 1), 1, (-1, 2)).shape
  3584. torch.Size([3, 2, 2, 1])
  3585. >>> torch.unflatten(torch.randn(5, 12, 3), -2, (2, 2, 3, 1, 1)).shape
  3586. torch.Size([5, 2, 2, 3, 1, 1, 3])
  3587. """.format(**common_args),
  3588. )
  3589. add_docstr(
  3590. torch.gather,
  3591. r"""
  3592. gather(input, dim, index, *, sparse_grad=False, out=None) -> Tensor
  3593. Gathers values along an axis specified by `dim`.
  3594. For a 3-D tensor the output is specified by::
  3595. out[i][j][k] = input[index[i][j][k]][j][k] # if dim == 0
  3596. out[i][j][k] = input[i][index[i][j][k]][k] # if dim == 1
  3597. out[i][j][k] = input[i][j][index[i][j][k]] # if dim == 2
  3598. :attr:`input` and :attr:`index` must have the same number of dimensions.
  3599. It is also required that ``index.size(d) <= input.size(d)`` for all
  3600. dimensions ``d != dim``. :attr:`out` will have the same shape as :attr:`index`.
  3601. Note that ``input`` and ``index`` do not broadcast against each other.
  3602. When :attr:`index` is empty, we always return an empty output with the same shape
  3603. without further error checking.
  3604. Args:
  3605. input (Tensor): the source tensor
  3606. dim (int): the axis along which to index
  3607. index (LongTensor): the indices of elements to gather
  3608. Keyword arguments:
  3609. sparse_grad (bool, optional): If ``True``, gradient w.r.t. :attr:`input` will be a sparse tensor.
  3610. out (Tensor, optional): the destination tensor
  3611. Example::
  3612. >>> t = torch.tensor([[1, 2], [3, 4]])
  3613. >>> torch.gather(t, 1, torch.tensor([[0, 0], [1, 0]]))
  3614. tensor([[ 1, 1],
  3615. [ 4, 3]])
  3616. """,
  3617. )
  3618. add_docstr(
  3619. torch.gcd,
  3620. r"""
  3621. gcd(input, other, *, out=None) -> Tensor
  3622. Computes the element-wise greatest common divisor (GCD) of :attr:`input` and :attr:`other`.
  3623. Both :attr:`input` and :attr:`other` must have integer types.
  3624. .. note::
  3625. This defines :math:`gcd(0, 0) = 0`.
  3626. Args:
  3627. {input}
  3628. other (Tensor): the second input tensor
  3629. Keyword arguments:
  3630. {out}
  3631. Example::
  3632. >>> a = torch.tensor([5, 10, 15])
  3633. >>> b = torch.tensor([3, 4, 5])
  3634. >>> torch.gcd(a, b)
  3635. tensor([1, 2, 5])
  3636. >>> c = torch.tensor([3])
  3637. >>> torch.gcd(a, c)
  3638. tensor([1, 1, 3])
  3639. """.format(**common_args),
  3640. )
  3641. add_docstr(
  3642. torch.ge,
  3643. r"""
  3644. ge(input, other, *, out=None) -> Tensor
  3645. Computes :math:`\text{input} \geq \text{other}` element-wise.
  3646. """
  3647. + r"""
  3648. The second argument can be a number or a tensor whose shape is
  3649. :ref:`broadcastable <broadcasting-semantics>` with the first argument.
  3650. Args:
  3651. input (Tensor): the tensor to compare
  3652. other (Tensor or float): the tensor or value to compare
  3653. Keyword args:
  3654. {out}
  3655. Returns:
  3656. A boolean tensor that is True where :attr:`input` is greater than or equal to :attr:`other` and False elsewhere
  3657. Example::
  3658. >>> torch.ge(torch.tensor([[1, 2], [3, 4]]), torch.tensor([[1, 1], [4, 4]]))
  3659. tensor([[True, True], [False, True]])
  3660. """.format(**common_args),
  3661. )
  3662. add_docstr(
  3663. torch.greater_equal,
  3664. r"""
  3665. greater_equal(input, other, *, out=None) -> Tensor
  3666. Alias for :func:`torch.ge`.
  3667. """,
  3668. )
  3669. add_docstr(
  3670. torch.gradient,
  3671. r"""
  3672. gradient(input, *, spacing=1, dim=None, edge_order=1) -> List of Tensors
  3673. Estimates the gradient of a function :math:`g : \mathbb{R}^n \rightarrow \mathbb{R}` in
  3674. one or more dimensions using the `second-order accurate central differences method
  3675. <https://www.ams.org/journals/mcom/1988-51-184/S0025-5718-1988-0935077-0/S0025-5718-1988-0935077-0.pdf>`_ and
  3676. either first or second order estimates at the boundaries.
  3677. The gradient of :math:`g` is estimated using samples. By default, when :attr:`spacing` is not
  3678. specified, the samples are entirely described by :attr:`input`, and the mapping of input coordinates
  3679. to an output is the same as the tensor's mapping of indices to values. For example, for a three-dimensional
  3680. :attr:`input` the function described is :math:`g : \mathbb{R}^3 \rightarrow \mathbb{R}`, and
  3681. :math:`g(1, 2, 3)\ == input[1, 2, 3]`.
  3682. When :attr:`spacing` is specified, it modifies the relationship between :attr:`input` and input coordinates.
  3683. This is detailed in the "Keyword Arguments" section below.
  3684. The gradient is estimated by estimating each partial derivative of :math:`g` independently. This estimation is
  3685. accurate if :math:`g` is in :math:`C^3` (it has at least 3 continuous derivatives), and the estimation can be
  3686. improved by providing closer samples. Mathematically, the value at each interior point of a partial derivative
  3687. is estimated using `Taylor's theorem with remainder <https://en.wikipedia.org/wiki/Taylor%27s_theorem>`_.
  3688. Letting :math:`x` be an interior point with :math:`x-h_l` and :math:`x+h_r` be points neighboring
  3689. it to the left and right respectively, :math:`f(x+h_r)` and :math:`f(x-h_l)` can be estimated using:
  3690. .. math::
  3691. \begin{aligned}
  3692. f(x+h_r) = f(x) + h_r f'(x) + {h_r}^2 \frac{f''(x)}{2} + {h_r}^3 \frac{f'''(\xi_1)}{6}, \xi_1 \in (x, x+h_r) \\
  3693. f(x-h_l) = f(x) - h_l f'(x) + {h_l}^2 \frac{f''(x)}{2} - {h_l}^3 \frac{f'''(\xi_2)}{6}, \xi_2 \in (x, x-h_l) \\
  3694. \end{aligned}
  3695. Using the fact that :math:`f \in C^3` and solving the linear system, we derive:
  3696. .. math::
  3697. f'(x) \approx \frac{ {h_l}^2 f(x+h_r) - {h_r}^2 f(x-h_l)
  3698. + ({h_r}^2-{h_l}^2 ) f(x) }{ {h_r} {h_l}^2 + {h_r}^2 {h_l} }
  3699. .. note::
  3700. We estimate the gradient of functions in complex domain
  3701. :math:`g : \mathbb{C}^n \rightarrow \mathbb{C}` in the same way.
  3702. The value of each partial derivative at the boundary points is computed differently. See edge_order below.
  3703. Args:
  3704. input (``Tensor``): the tensor that represents the values of the function
  3705. Keyword args:
  3706. spacing (``scalar``, ``list of scalar``, ``list of Tensor``, optional): :attr:`spacing` can be used to modify
  3707. how the :attr:`input` tensor's indices relate to sample coordinates. If :attr:`spacing` is a scalar then
  3708. the indices are multiplied by the scalar to produce the coordinates. For example, if :attr:`spacing=2` the
  3709. indices (1, 2, 3) become coordinates (2, 4, 6). If :attr:`spacing` is a list of scalars then the corresponding
  3710. indices are multiplied. For example, if :attr:`spacing=(2, -1, 3)` the indices (1, 2, 3) become coordinates (2, -2, 9).
  3711. Finally, if :attr:`spacing` is a list of one-dimensional tensors then each tensor specifies the coordinates for
  3712. the corresponding dimension. For example, if the indices are (1, 2, 3) and the tensors are (t0, t1, t2), then
  3713. the coordinates are (t0[1], t1[2], t2[3])
  3714. dim (``int``, ``list of int``, optional): the dimension or dimensions to approximate the gradient over. By default
  3715. the partial gradient in every dimension is computed. Note that when :attr:`dim` is specified the elements of
  3716. the :attr:`spacing` argument must correspond with the specified dims."
  3717. edge_order (``int``, optional): 1 or 2, for `first-order
  3718. <https://www.ams.org/journals/mcom/1988-51-184/S0025-5718-1988-0935077-0/S0025-5718-1988-0935077-0.pdf>`_ or
  3719. `second-order <https://www.ams.org/journals/mcom/1988-51-184/S0025-5718-1988-0935077-0/S0025-5718-1988-0935077-0.pdf>`_
  3720. estimation of the boundary ("edge") values, respectively. Note that when :attr:`edge_order` is specified, each
  3721. dimension size of :attr:`input` should be at least edge_order+1
  3722. Examples::
  3723. >>> # Estimates the gradient of f(x)=x^2 at points [-2, -1, 2, 4]
  3724. >>> coordinates = (torch.tensor([-2., -1., 1., 4.]),)
  3725. >>> values = torch.tensor([4., 1., 1., 16.], )
  3726. >>> torch.gradient(values, spacing = coordinates)
  3727. (tensor([-3., -2., 2., 5.]),)
  3728. >>> # Estimates the gradient of the R^2 -> R function whose samples are
  3729. >>> # described by the tensor t. Implicit coordinates are [0, 1] for the outermost
  3730. >>> # dimension and [0, 1, 2, 3] for the innermost dimension, and function estimates
  3731. >>> # partial derivative for both dimensions.
  3732. >>> t = torch.tensor([[1, 2, 4, 8], [10, 20, 40, 80]])
  3733. >>> torch.gradient(t)
  3734. (tensor([[ 9., 18., 36., 72.],
  3735. [ 9., 18., 36., 72.]]),
  3736. tensor([[ 1.0000, 1.5000, 3.0000, 4.0000],
  3737. [10.0000, 15.0000, 30.0000, 40.0000]]))
  3738. >>> # A scalar value for spacing modifies the relationship between tensor indices
  3739. >>> # and input coordinates by multiplying the indices to find the
  3740. >>> # coordinates. For example, below the indices of the innermost
  3741. >>> # 0, 1, 2, 3 translate to coordinates of [0, 2, 4, 6], and the indices of
  3742. >>> # the outermost dimension 0, 1 translate to coordinates of [0, 2].
  3743. >>> torch.gradient(t, spacing = 2.0) # dim = None (implicitly [0, 1])
  3744. (tensor([[ 4.5000, 9.0000, 18.0000, 36.0000],
  3745. [ 4.5000, 9.0000, 18.0000, 36.0000]]),
  3746. tensor([[ 0.5000, 0.7500, 1.5000, 2.0000],
  3747. [ 5.0000, 7.5000, 15.0000, 20.0000]]))
  3748. >>> # doubling the spacing between samples halves the estimated partial gradients.
  3749. >>>
  3750. >>> # Estimates only the partial derivative for dimension 1
  3751. >>> torch.gradient(t, dim = 1) # spacing = None (implicitly 1.)
  3752. (tensor([[ 1.0000, 1.5000, 3.0000, 4.0000],
  3753. [10.0000, 15.0000, 30.0000, 40.0000]]),)
  3754. >>> # When spacing is a list of scalars, the relationship between the tensor
  3755. >>> # indices and input coordinates changes based on dimension.
  3756. >>> # For example, below, the indices of the innermost dimension 0, 1, 2, 3 translate
  3757. >>> # to coordinates of [0, 3, 6, 9], and the indices of the outermost dimension
  3758. >>> # 0, 1 translate to coordinates of [0, 2].
  3759. >>> torch.gradient(t, spacing = [3., 2.])
  3760. (tensor([[ 4.5000, 9.0000, 18.0000, 36.0000],
  3761. [ 4.5000, 9.0000, 18.0000, 36.0000]]),
  3762. tensor([[ 0.3333, 0.5000, 1.0000, 1.3333],
  3763. [ 3.3333, 5.0000, 10.0000, 13.3333]]))
  3764. >>> # The following example is a replication of the previous one with explicit
  3765. >>> # coordinates.
  3766. >>> coords = (torch.tensor([0, 2]), torch.tensor([0, 3, 6, 9]))
  3767. >>> torch.gradient(t, spacing = coords)
  3768. (tensor([[ 4.5000, 9.0000, 18.0000, 36.0000],
  3769. [ 4.5000, 9.0000, 18.0000, 36.0000]]),
  3770. tensor([[ 0.3333, 0.5000, 1.0000, 1.3333],
  3771. [ 3.3333, 5.0000, 10.0000, 13.3333]]))
  3772. """,
  3773. )
  3774. add_docstr(
  3775. torch.geqrf,
  3776. r"""
  3777. geqrf(input, *, out=None) -> (Tensor, Tensor)
  3778. This is a low-level function for calling LAPACK's geqrf directly. This function
  3779. returns a namedtuple (a, tau) as defined in `LAPACK documentation for geqrf`_ .
  3780. Computes a QR decomposition of :attr:`input`.
  3781. Both `Q` and `R` matrices are stored in the same output tensor `a`.
  3782. The elements of `R` are stored on and above the diagonal.
  3783. Elementary reflectors (or Householder vectors) implicitly defining matrix `Q`
  3784. are stored below the diagonal.
  3785. The results of this function can be used together with :func:`torch.linalg.householder_product`
  3786. to obtain the `Q` matrix or
  3787. with :func:`torch.ormqr`, which uses an implicit representation of the `Q` matrix,
  3788. for an efficient matrix-matrix multiplication.
  3789. See `LAPACK documentation for geqrf`_ for further details.
  3790. .. note::
  3791. See also :func:`torch.linalg.qr`, which computes Q and R matrices, and :func:`torch.linalg.lstsq`
  3792. with the ``driver="gels"`` option for a function that can solve matrix equations using a QR decomposition.
  3793. Args:
  3794. input (Tensor): the input matrix
  3795. Keyword args:
  3796. out (tuple, optional): the output tuple of (Tensor, Tensor). Ignored if `None`. Default: `None`.
  3797. .. _LAPACK documentation for geqrf:
  3798. http://www.netlib.org/lapack/explore-html/df/dc5/group__variants_g_ecomputational_ga3766ea903391b5cf9008132f7440ec7b.html
  3799. """,
  3800. )
  3801. add_docstr(
  3802. torch.inner,
  3803. r"""
  3804. inner(input, other, *, out=None) -> Tensor
  3805. Computes the dot product for 1D tensors. For higher dimensions, sums the product
  3806. of elements from :attr:`input` and :attr:`other` along their last dimension.
  3807. .. note::
  3808. If either :attr:`input` or :attr:`other` is a scalar, the result is equivalent
  3809. to `torch.mul(input, other)`.
  3810. If both :attr:`input` and :attr:`other` are non-scalars, the size of their last
  3811. dimension must match and the result is equivalent to `torch.tensordot(input,
  3812. other, dims=([-1], [-1]))`
  3813. Args:
  3814. input (Tensor): First input tensor
  3815. other (Tensor): Second input tensor
  3816. Keyword args:
  3817. out (Tensor, optional): Optional output tensor to write result into. The output
  3818. shape is `input.shape[:-1] + other.shape[:-1]`.
  3819. Example::
  3820. # Dot product
  3821. >>> torch.inner(torch.tensor([1, 2, 3]), torch.tensor([0, 2, 1]))
  3822. tensor(7)
  3823. # Multidimensional input tensors
  3824. >>> a = torch.randn(2, 3)
  3825. >>> a
  3826. tensor([[0.8173, 1.0874, 1.1784],
  3827. [0.3279, 0.1234, 2.7894]])
  3828. >>> b = torch.randn(2, 4, 3)
  3829. >>> b
  3830. tensor([[[-0.4682, -0.7159, 0.1506],
  3831. [ 0.4034, -0.3657, 1.0387],
  3832. [ 0.9892, -0.6684, 0.1774],
  3833. [ 0.9482, 1.3261, 0.3917]],
  3834. [[ 0.4537, 0.7493, 1.1724],
  3835. [ 0.2291, 0.5749, -0.2267],
  3836. [-0.7920, 0.3607, -0.3701],
  3837. [ 1.3666, -0.5850, -1.7242]]])
  3838. >>> torch.inner(a, b)
  3839. tensor([[[-0.9837, 1.1560, 0.2907, 2.6785],
  3840. [ 2.5671, 0.5452, -0.6912, -1.5509]],
  3841. [[ 0.1782, 2.9843, 0.7366, 1.5672],
  3842. [ 3.5115, -0.4864, -1.2476, -4.4337]]])
  3843. # Scalar input
  3844. >>> torch.inner(a, torch.tensor(2))
  3845. tensor([[1.6347, 2.1748, 2.3567],
  3846. [0.6558, 0.2469, 5.5787]])
  3847. """,
  3848. )
  3849. add_docstr(
  3850. torch.outer,
  3851. r"""
  3852. outer(input, vec2, *, out=None) -> Tensor
  3853. Outer product of :attr:`input` and :attr:`vec2`.
  3854. If :attr:`input` is a vector of size :math:`n` and :attr:`vec2` is a vector of
  3855. size :math:`m`, then :attr:`out` must be a matrix of size :math:`(n \times m)`.
  3856. .. note:: This function does not :ref:`broadcast <broadcasting-semantics>`.
  3857. Args:
  3858. input (Tensor): 1-D input vector
  3859. vec2 (Tensor): 1-D input vector
  3860. Keyword args:
  3861. out (Tensor, optional): optional output matrix
  3862. Example::
  3863. >>> v1 = torch.arange(1., 5.)
  3864. >>> v2 = torch.arange(1., 4.)
  3865. >>> torch.outer(v1, v2)
  3866. tensor([[ 1., 2., 3.],
  3867. [ 2., 4., 6.],
  3868. [ 3., 6., 9.],
  3869. [ 4., 8., 12.]])
  3870. """,
  3871. )
  3872. add_docstr(
  3873. torch.ger,
  3874. r"""
  3875. ger(input, vec2, *, out=None) -> Tensor
  3876. Alias of :func:`torch.outer`.
  3877. .. warning::
  3878. This function is deprecated and will be removed in a future PyTorch release.
  3879. Use :func:`torch.outer` instead.
  3880. """,
  3881. )
  3882. add_docstr(
  3883. torch.get_default_dtype,
  3884. r"""
  3885. get_default_dtype() -> torch.dtype
  3886. Get the current default floating point :class:`torch.dtype`.
  3887. Example::
  3888. >>> torch.get_default_dtype() # initial default for floating point is torch.float32
  3889. torch.float32
  3890. >>> torch.set_default_dtype(torch.float64)
  3891. >>> torch.get_default_dtype() # default is now changed to torch.float64
  3892. torch.float64
  3893. """,
  3894. )
  3895. add_docstr(
  3896. torch.get_num_threads,
  3897. r"""
  3898. get_num_threads() -> int
  3899. Returns the number of threads used for parallelizing CPU operations
  3900. """,
  3901. )
  3902. add_docstr(
  3903. torch.get_num_interop_threads,
  3904. r"""
  3905. get_num_interop_threads() -> int
  3906. Returns the number of threads used for inter-op parallelism on CPU
  3907. (e.g. in JIT interpreter)
  3908. """,
  3909. )
  3910. add_docstr(
  3911. torch.gt,
  3912. r"""
  3913. gt(input, other, *, out=None) -> Tensor
  3914. Computes :math:`\text{input} > \text{other}` element-wise.
  3915. """
  3916. + r"""
  3917. The second argument can be a number or a tensor whose shape is
  3918. :ref:`broadcastable <broadcasting-semantics>` with the first argument.
  3919. Args:
  3920. input (Tensor): the tensor to compare
  3921. other (Tensor or float): the tensor or value to compare
  3922. Keyword args:
  3923. {out}
  3924. Returns:
  3925. A boolean tensor that is True where :attr:`input` is greater than :attr:`other` and False elsewhere
  3926. Example::
  3927. >>> torch.gt(torch.tensor([[1, 2], [3, 4]]), torch.tensor([[1, 1], [4, 4]]))
  3928. tensor([[False, True], [False, False]])
  3929. """.format(**common_args),
  3930. )
  3931. add_docstr(
  3932. torch.greater,
  3933. r"""
  3934. greater(input, other, *, out=None) -> Tensor
  3935. Alias for :func:`torch.gt`.
  3936. """,
  3937. )
  3938. add_docstr(
  3939. torch.hash_tensor,
  3940. r"""
  3941. hash_tensor(input, *, mode=0) -> Tensor
  3942. Returns a hash of all elements in the :attr:`input` tensor.
  3943. Currently only mode=0 (reduction via xor) is supported. The output will always
  3944. be of type ``torch.uint64``. The elements of ``input`` are upcasted to their
  3945. 64 bit float / integer equivalent and bitcasted to ``torch.uint64`` before
  3946. reduction via xor.
  3947. Args:
  3948. {input}
  3949. Keyword Args:
  3950. mode (int) : The hash to use. Default: 0 (xor_reduction)
  3951. Example::
  3952. >>> a = torch.randn(1, 3)
  3953. >>> a
  3954. tensor([[ 1.1918, -1.1813, 0.3373]])
  3955. >>> torch.hash_tensor(a)
  3956. tensor(13822780554648485888, dtype=torch.uint64)
  3957. .. function:: hash_tensor(input, dim, *, keepdim=False, mode=0) -> Tensor
  3958. :noindex:
  3959. Returns the hash of each row of the :attr:`input` tensor in the given
  3960. dimension :attr:`dim` given by mode. If :attr:`dim` is a list of dimensions,
  3961. reduce over all of them.
  3962. {keepdim_details}
  3963. Args:
  3964. {input}
  3965. {opt_dim_all_reduce}
  3966. {opt_keepdim}
  3967. Keyword Args:
  3968. mode (int) : The hash to use. Default: 0 (xor_reduction)
  3969. Example::
  3970. >>> a = torch.randn(2, 4)
  3971. >>> a
  3972. tensor([[ 0.1317, -0.5554, -1.4724, -1.1391],
  3973. [ 0.0778, -0.6070, 0.6375, 0.1798]])
  3974. >>> torch.hash_tensor(a, 1)
  3975. tensor([9233691267014066176, 9255993250844508160], dtype=torch.uint64)
  3976. """.format(**multi_dim_common),
  3977. )
  3978. add_docstr(
  3979. torch.histc,
  3980. r"""
  3981. histc(input, bins=100, min=0, max=0, *, out=None) -> Tensor
  3982. Computes the histogram of a tensor.
  3983. The elements are sorted into equal width bins between :attr:`min` and
  3984. :attr:`max`. If :attr:`min` and :attr:`max` are both zero, the minimum and
  3985. maximum values of the data are used.
  3986. Elements lower than min and higher than max and ``NaN`` elements are ignored.
  3987. Args:
  3988. {input}
  3989. bins (int): number of histogram bins
  3990. min (Scalar): lower end of the range (inclusive)
  3991. max (Scalar): upper end of the range (inclusive)
  3992. Keyword args:
  3993. {out}
  3994. Returns:
  3995. Tensor: Histogram represented as a tensor
  3996. Example::
  3997. >>> torch.histc(torch.tensor([1., 2, 1]), bins=4, min=0, max=3)
  3998. tensor([ 0., 2., 1., 0.])
  3999. """.format(**common_args),
  4000. )
  4001. add_docstr(
  4002. torch.histogram,
  4003. r"""
  4004. histogram(input, bins, *, range=None, weight=None, density=False, out=None) -> (Tensor, Tensor)
  4005. Computes a histogram of the values in a tensor.
  4006. :attr:`bins` can be an integer or a 1D tensor.
  4007. If :attr:`bins` is an int, it specifies the number of equal-width bins.
  4008. By default, the lower and upper range of the bins is determined by the
  4009. minimum and maximum elements of the input tensor. The :attr:`range`
  4010. argument can be provided to specify a range for the bins.
  4011. If :attr:`bins` is a 1D tensor, it specifies the sequence of bin edges
  4012. including the rightmost edge. It should contain at least 2 elements
  4013. and its elements should be increasing.
  4014. Args:
  4015. {input}
  4016. bins: int or 1D Tensor. If int, defines the number of equal-width bins. If tensor,
  4017. defines the sequence of bin edges including the rightmost edge.
  4018. Keyword args:
  4019. range (tuple of float): Defines the range of the bins.
  4020. weight (Tensor): If provided, weight should have the same shape as input. Each value in
  4021. input contributes its associated weight towards its bin's result.
  4022. density (bool): If False, the result will contain the count (or total weight) in each bin.
  4023. If True, the result is the value of the probability density function over the bins,
  4024. normalized such that the integral over the range of the bins is 1.
  4025. {out} (tuple, optional): The result tuple of two output tensors (hist, bin_edges).
  4026. Returns:
  4027. hist (Tensor): 1D Tensor containing the values of the histogram.
  4028. bin_edges(Tensor): 1D Tensor containing the edges of the histogram bins.
  4029. Example::
  4030. >>> torch.histogram(torch.tensor([1., 2, 1]), bins=4, range=(0., 3.), weight=torch.tensor([1., 2., 4.]))
  4031. (tensor([ 0., 5., 2., 0.]), tensor([0., 0.75, 1.5, 2.25, 3.]))
  4032. >>> torch.histogram(torch.tensor([1., 2, 1]), bins=4, range=(0., 3.), weight=torch.tensor([1., 2., 4.]), density=True)
  4033. (tensor([ 0., 0.9524, 0.3810, 0.]), tensor([0., 0.75, 1.5, 2.25, 3.]))
  4034. """.format(**common_args),
  4035. )
  4036. add_docstr(
  4037. torch.histogramdd,
  4038. r"""
  4039. histogramdd(input, bins, *, range=None, weight=None, density=False, out=None) -> (Tensor, Tensor[])
  4040. Computes a multi-dimensional histogram of the values in a tensor.
  4041. Interprets the elements of an input tensor whose innermost dimension has size N
  4042. as a collection of N-dimensional points. Maps each of the points into a set of
  4043. N-dimensional bins and returns the number of points (or total weight) in each bin.
  4044. :attr:`input` must be a tensor with at least 2 dimensions.
  4045. If input has shape (M, N), each of its M rows defines a point in N-dimensional space.
  4046. If input has three or more dimensions, all but the last dimension are flattened.
  4047. Each dimension is independently associated with its own strictly increasing sequence
  4048. of bin edges. Bin edges may be specified explicitly by passing a sequence of 1D
  4049. tensors. Alternatively, bin edges may be constructed automatically by passing a
  4050. sequence of integers specifying the number of equal-width bins in each dimension.
  4051. For each N-dimensional point in input:
  4052. - Each of its coordinates is binned independently among the bin edges
  4053. corresponding to its dimension
  4054. - Binning results are combined to identify the N-dimensional bin (if any)
  4055. into which the point falls
  4056. - If the point falls into a bin, the bin's count (or total weight) is incremented
  4057. - Points which do not fall into any bin do not contribute to the output
  4058. :attr:`bins` can be a sequence of N 1D tensors, a sequence of N ints, or a single int.
  4059. If :attr:`bins` is a sequence of N 1D tensors, it explicitly specifies the N sequences
  4060. of bin edges. Each 1D tensor should contain a strictly increasing sequence with at
  4061. least one element. A sequence of K bin edges defines K-1 bins, explicitly specifying
  4062. the left and right edges of all bins. Every bin is inclusive of its left edge. Only
  4063. the rightmost bin is inclusive of its right edge.
  4064. If :attr:`bins` is a sequence of N ints, it specifies the number of equal-width bins
  4065. in each dimension. By default, the leftmost and rightmost bin edges in each dimension
  4066. are determined by the minimum and maximum elements of the input tensor in the
  4067. corresponding dimension. The :attr:`range` argument can be provided to manually
  4068. specify the leftmost and rightmost bin edges in each dimension.
  4069. If :attr:`bins` is an int, it specifies the number of equal-width bins for all dimensions.
  4070. .. note::
  4071. See also :func:`torch.histogram`, which specifically computes 1D histograms.
  4072. While :func:`torch.histogramdd` infers the dimensionality of its bins and
  4073. binned values from the shape of :attr:`input`, :func:`torch.histogram`
  4074. accepts and flattens :attr:`input` of any shape.
  4075. Args:
  4076. {input}
  4077. bins: Tensor[], int[], or int.
  4078. If Tensor[], defines the sequences of bin edges.
  4079. If int[], defines the number of equal-width bins in each dimension.
  4080. If int, defines the number of equal-width bins for all dimensions.
  4081. Keyword args:
  4082. range (sequence of float): Defines the leftmost and rightmost bin edges
  4083. in each dimension.
  4084. weight (Tensor): By default, each value in the input has weight 1. If a weight
  4085. tensor is passed, each N-dimensional coordinate in input
  4086. contributes its associated weight towards its bin's result.
  4087. The weight tensor should have the same shape as the :attr:`input`
  4088. tensor excluding its innermost dimension N.
  4089. density (bool): If False (default), the result will contain the count (or total weight)
  4090. in each bin. If True, each count (weight) is divided by the total count
  4091. (total weight), then divided by the volume of its associated bin.
  4092. Returns:
  4093. hist (Tensor): N-dimensional Tensor containing the values of the histogram.
  4094. bin_edges(Tensor[]): sequence of N 1D Tensors containing the bin edges.
  4095. Example::
  4096. >>> torch.histogramdd(torch.tensor([[0., 1.], [1., 0.], [2., 0.], [2., 2.]]), bins=[3, 3],
  4097. ... weight=torch.tensor([1., 2., 4., 8.]))
  4098. torch.return_types.histogramdd(
  4099. hist=tensor([[0., 1., 0.],
  4100. [2., 0., 0.],
  4101. [4., 0., 8.]]),
  4102. bin_edges=(tensor([0.0000, 0.6667, 1.3333, 2.0000]),
  4103. tensor([0.0000, 0.6667, 1.3333, 2.0000])))
  4104. >>> torch.histogramdd(torch.tensor([[0., 0.], [1., 1.], [2., 2.]]), bins=[2, 2],
  4105. ... range=[0., 1., 0., 1.], density=True)
  4106. torch.return_types.histogramdd(
  4107. hist=tensor([[2., 0.],
  4108. [0., 2.]]),
  4109. bin_edges=(tensor([0.0000, 0.5000, 1.0000]),
  4110. tensor([0.0000, 0.5000, 1.0000])))
  4111. """.format(**common_args),
  4112. )
  4113. # TODO: Fix via https://github.com/pytorch/pytorch/issues/75798
  4114. torch.histogramdd.__module__ = "torch"
  4115. add_docstr(
  4116. torch.hypot,
  4117. r"""
  4118. hypot(input, other, *, out=None) -> Tensor
  4119. Given the legs of a right triangle, return its hypotenuse.
  4120. .. math::
  4121. \text{out}_{i} = \sqrt{\text{input}_{i}^{2} + \text{other}_{i}^{2}}
  4122. The shapes of ``input`` and ``other`` must be
  4123. :ref:`broadcastable <broadcasting-semantics>`.
  4124. """
  4125. + r"""
  4126. Args:
  4127. input (Tensor): the first input tensor
  4128. other (Tensor): the second input tensor
  4129. Keyword args:
  4130. {out}
  4131. Example::
  4132. >>> a = torch.hypot(torch.tensor([4.0]), torch.tensor([3.0, 4.0, 5.0]))
  4133. tensor([5.0000, 5.6569, 6.4031])
  4134. """.format(**common_args),
  4135. )
  4136. add_docstr(
  4137. torch.i0,
  4138. r"""
  4139. i0(input, *, out=None) -> Tensor
  4140. Alias for :func:`torch.special.i0`.
  4141. """,
  4142. )
  4143. add_docstr(
  4144. torch.igamma,
  4145. r"""
  4146. igamma(input, other, *, out=None) -> Tensor
  4147. Alias for :func:`torch.special.gammainc`.
  4148. """,
  4149. )
  4150. add_docstr(
  4151. torch.igammac,
  4152. r"""
  4153. igammac(input, other, *, out=None) -> Tensor
  4154. Alias for :func:`torch.special.gammaincc`.
  4155. """,
  4156. )
  4157. add_docstr(
  4158. torch.index_select,
  4159. r"""
  4160. index_select(input, dim, index, *, out=None) -> Tensor
  4161. Returns a new tensor which indexes the :attr:`input` tensor along dimension
  4162. :attr:`dim` using the entries in :attr:`index` which is a `LongTensor`.
  4163. The returned tensor has the same number of dimensions as the original tensor
  4164. (:attr:`input`). The :attr:`dim`\ th dimension has the same size as the length
  4165. of :attr:`index`; other dimensions have the same size as in the original tensor.
  4166. .. note:: The returned tensor does **not** use the same storage as the original
  4167. tensor. If :attr:`out` has a different shape than expected, we
  4168. silently change it to the correct shape, reallocating the underlying
  4169. storage if necessary.
  4170. Args:
  4171. {input}
  4172. dim (int): the dimension in which we index
  4173. index (IntTensor or LongTensor): the 1-D tensor containing the indices to index
  4174. Keyword args:
  4175. {out}
  4176. Example::
  4177. >>> x = torch.randn(3, 4)
  4178. >>> x
  4179. tensor([[ 0.1427, 0.0231, -0.5414, -1.0009],
  4180. [-0.4664, 0.2647, -0.1228, -1.1068],
  4181. [-1.1734, -0.6571, 0.7230, -0.6004]])
  4182. >>> indices = torch.tensor([0, 2])
  4183. >>> torch.index_select(x, 0, indices)
  4184. tensor([[ 0.1427, 0.0231, -0.5414, -1.0009],
  4185. [-1.1734, -0.6571, 0.7230, -0.6004]])
  4186. >>> torch.index_select(x, 1, indices)
  4187. tensor([[ 0.1427, -0.5414],
  4188. [-0.4664, -0.1228],
  4189. [-1.1734, 0.7230]])
  4190. """.format(**common_args),
  4191. )
  4192. add_docstr(
  4193. torch.inverse,
  4194. r"""
  4195. inverse(input, *, out=None) -> Tensor
  4196. Alias for :func:`torch.linalg.inv`
  4197. """,
  4198. )
  4199. add_docstr(
  4200. torch.isin,
  4201. r"""
  4202. isin(elements, test_elements, *, assume_unique=False, invert=False) -> Tensor
  4203. Tests if each element of :attr:`elements` is in :attr:`test_elements`. Returns
  4204. a boolean tensor of the same shape as :attr:`elements` that is True for elements
  4205. in :attr:`test_elements` and False otherwise.
  4206. .. note::
  4207. One of :attr:`elements` or :attr:`test_elements` can be a scalar, but not both.
  4208. Args:
  4209. elements (Tensor or Scalar): Input elements
  4210. test_elements (Tensor or Scalar): Values against which to test for each input element
  4211. assume_unique (bool, optional): If True, assumes both :attr:`elements` and
  4212. :attr:`test_elements` contain unique elements, which can speed up the
  4213. calculation. Default: False
  4214. invert (bool, optional): If True, inverts the boolean return tensor, resulting in True
  4215. values for elements *not* in :attr:`test_elements`. Default: False
  4216. Returns:
  4217. A boolean tensor of the same shape as :attr:`elements` that is True for elements in
  4218. :attr:`test_elements` and False otherwise
  4219. Example:
  4220. >>> torch.isin(torch.tensor([[1, 2], [3, 4]]), torch.tensor([2, 3]))
  4221. tensor([[False, True],
  4222. [ True, False]])
  4223. """,
  4224. )
  4225. add_docstr(
  4226. torch.isinf,
  4227. r"""
  4228. isinf(input) -> Tensor
  4229. Tests if each element of :attr:`input` is infinite
  4230. (positive or negative infinity) or not.
  4231. .. note::
  4232. Complex values are infinite when their real or imaginary part is
  4233. infinite.
  4234. Args:
  4235. {input}
  4236. Returns:
  4237. A boolean tensor that is True where :attr:`input` is infinite and False elsewhere
  4238. Example::
  4239. >>> torch.isinf(torch.tensor([1, float('inf'), 2, float('-inf'), float('nan')]))
  4240. tensor([False, True, False, True, False])
  4241. """.format(**common_args),
  4242. )
  4243. add_docstr(
  4244. torch.isposinf,
  4245. r"""
  4246. isposinf(input, *, out=None) -> Tensor
  4247. Tests if each element of :attr:`input` is positive infinity or not.
  4248. Args:
  4249. {input}
  4250. Keyword args:
  4251. {out}
  4252. Example::
  4253. >>> a = torch.tensor([-float('inf'), float('inf'), 1.2])
  4254. >>> torch.isposinf(a)
  4255. tensor([False, True, False])
  4256. """.format(**common_args),
  4257. )
  4258. add_docstr(
  4259. torch.isneginf,
  4260. r"""
  4261. isneginf(input, *, out=None) -> Tensor
  4262. Tests if each element of :attr:`input` is negative infinity or not.
  4263. Args:
  4264. {input}
  4265. Keyword args:
  4266. {out}
  4267. Example::
  4268. >>> a = torch.tensor([-float('inf'), float('inf'), 1.2])
  4269. >>> torch.isneginf(a)
  4270. tensor([ True, False, False])
  4271. """.format(**common_args),
  4272. )
  4273. add_docstr(
  4274. torch.isclose,
  4275. r"""
  4276. isclose(input, other, rtol=1e-05, atol=1e-08, equal_nan=False) -> Tensor
  4277. Returns a new tensor with boolean elements representing if each element of
  4278. :attr:`input` is "close" to the corresponding element of :attr:`other`.
  4279. Closeness is defined as:
  4280. .. math::
  4281. \lvert \text{input}_i - \text{other}_i \rvert \leq \texttt{rtol} \times \lvert \text{other}_i \rvert + \texttt{atol}
  4282. """
  4283. + r"""
  4284. where :attr:`input` and :attr:`other` are finite. Where :attr:`input`
  4285. and/or :attr:`other` are nonfinite they are close if and only if
  4286. they are equal, with NaNs being considered equal to each other when
  4287. :attr:`equal_nan` is True.
  4288. Args:
  4289. input (Tensor): first tensor to compare
  4290. other (Tensor): second tensor to compare
  4291. rtol (float, optional): relative tolerance. Default: 1e-05
  4292. atol (float, optional): absolute tolerance. Default: 1e-08
  4293. equal_nan (bool, optional): if ``True``, then two ``NaN`` s will be considered equal. Default: ``False``
  4294. Examples::
  4295. >>> torch.isclose(torch.tensor((1., 2, 3)), torch.tensor((1 + 1e-10, 3, 4)))
  4296. tensor([ True, False, False])
  4297. >>> torch.isclose(torch.tensor((float('inf'), 4)), torch.tensor((float('inf'), 6)), rtol=.5)
  4298. tensor([True, True])
  4299. """,
  4300. )
  4301. add_docstr(
  4302. torch.isfinite,
  4303. r"""
  4304. isfinite(input) -> Tensor
  4305. Returns a new tensor with boolean elements representing if each element is `finite` or not.
  4306. Real values are finite when they are not NaN, negative infinity, or infinity.
  4307. Complex values are finite when both their real and imaginary parts are finite.
  4308. Args:
  4309. {input}
  4310. Returns:
  4311. A boolean tensor that is True where :attr:`input` is finite and False elsewhere
  4312. Example::
  4313. >>> torch.isfinite(torch.tensor([1, float('inf'), 2, float('-inf'), float('nan')]))
  4314. tensor([True, False, True, False, False])
  4315. """.format(**common_args),
  4316. )
  4317. add_docstr(
  4318. torch.isnan,
  4319. r"""
  4320. isnan(input) -> Tensor
  4321. Returns a new tensor with boolean elements representing if each element of :attr:`input`
  4322. is NaN or not. Complex values are considered NaN when either their real
  4323. and/or imaginary part is NaN.
  4324. Arguments:
  4325. {input}
  4326. Returns:
  4327. A boolean tensor that is True where :attr:`input` is NaN and False elsewhere
  4328. Example::
  4329. >>> torch.isnan(torch.tensor([1, float('nan'), 2]))
  4330. tensor([False, True, False])
  4331. """.format(**common_args),
  4332. )
  4333. add_docstr(
  4334. torch.isreal,
  4335. r"""
  4336. isreal(input) -> Tensor
  4337. Returns a new tensor with boolean elements representing if each element of :attr:`input` is real-valued or not.
  4338. All real-valued types are considered real. Complex values are considered real when their imaginary part is 0.
  4339. Arguments:
  4340. {input}
  4341. Returns:
  4342. A boolean tensor that is True where :attr:`input` is real and False elsewhere
  4343. Example::
  4344. >>> torch.isreal(torch.tensor([1, 1+1j, 2+0j]))
  4345. tensor([True, False, True])
  4346. """.format(**common_args),
  4347. )
  4348. add_docstr(
  4349. torch.is_floating_point,
  4350. r"""
  4351. is_floating_point(input: Tensor) -> bool
  4352. Returns True if the data type of :attr:`input` is a floating point data type i.e.,
  4353. one of ``torch.float64``, ``torch.float32``, ``torch.float16``, and ``torch.bfloat16``.
  4354. Args:
  4355. {input}
  4356. Example::
  4357. >>> torch.is_floating_point(torch.tensor([1.0, 2.0, 3.0]))
  4358. True
  4359. >>> torch.is_floating_point(torch.tensor([1, 2, 3], dtype=torch.int32))
  4360. False
  4361. >>> torch.is_floating_point(torch.tensor([1.0, 2.0, 3.0], dtype=torch.float16))
  4362. True
  4363. >>> torch.is_floating_point(torch.tensor([1, 2, 3], dtype=torch.complex64))
  4364. False
  4365. """.format(**common_args),
  4366. )
  4367. add_docstr(
  4368. torch.is_complex,
  4369. r"""
  4370. is_complex(input: Tensor) -> bool
  4371. Returns True if the data type of :attr:`input` is a complex data type i.e.,
  4372. one of ``torch.complex64``, and ``torch.complex128``.
  4373. Args:
  4374. {input}
  4375. Example::
  4376. >>> torch.is_complex(torch.tensor([1, 2, 3], dtype=torch.complex64))
  4377. True
  4378. >>> torch.is_complex(torch.tensor([1, 2, 3], dtype=torch.complex128))
  4379. True
  4380. >>> torch.is_complex(torch.tensor([1, 2, 3], dtype=torch.int32))
  4381. False
  4382. >>> torch.is_complex(torch.tensor([1.0, 2.0, 3.0], dtype=torch.float16))
  4383. False
  4384. """.format(**common_args),
  4385. )
  4386. add_docstr(
  4387. torch.is_grad_enabled,
  4388. r"""
  4389. is_grad_enabled() -> (bool)
  4390. Returns True if grad mode is currently enabled.
  4391. """.format(**common_args),
  4392. )
  4393. add_docstr(
  4394. torch.is_inference_mode_enabled,
  4395. r"""
  4396. is_inference_mode_enabled() -> (bool)
  4397. Returns True if inference mode is currently enabled.
  4398. """.format(**common_args),
  4399. )
  4400. add_docstr(
  4401. torch.is_inference,
  4402. r"""
  4403. is_inference(input) -> (bool)
  4404. Returns True if :attr:`input` is an inference tensor.
  4405. A non-view tensor is an inference tensor if and only if it was
  4406. allocated during inference mode. A view tensor is an inference
  4407. tensor if and only if the tensor it is a view of is an inference tensor.
  4408. For details on inference mode please see
  4409. `Inference Mode <https://pytorch.org/cppdocs/notes/inference_mode.html>`_.
  4410. Args:
  4411. {input}
  4412. """.format(**common_args),
  4413. )
  4414. add_docstr(
  4415. torch.is_conj,
  4416. r"""
  4417. is_conj(input) -> (bool)
  4418. Returns True if the :attr:`input` is a conjugated tensor, i.e. its conjugate bit is set to `True`.
  4419. Args:
  4420. {input}
  4421. """.format(**common_args),
  4422. )
  4423. add_docstr(
  4424. torch.is_nonzero,
  4425. r"""
  4426. is_nonzero(input) -> (bool)
  4427. Returns True if the :attr:`input` is a single element tensor which is not equal to zero
  4428. after type conversions.
  4429. i.e. not equal to ``torch.tensor([0.])`` or ``torch.tensor([0])`` or
  4430. ``torch.tensor([False])``.
  4431. Throws a ``RuntimeError`` if ``torch.numel() != 1`` (even in case
  4432. of sparse tensors).
  4433. Args:
  4434. {input}
  4435. Examples::
  4436. >>> torch.is_nonzero(torch.tensor([0.]))
  4437. False
  4438. >>> torch.is_nonzero(torch.tensor([1.5]))
  4439. True
  4440. >>> torch.is_nonzero(torch.tensor([False]))
  4441. False
  4442. >>> torch.is_nonzero(torch.tensor([3]))
  4443. True
  4444. >>> torch.is_nonzero(torch.tensor([1, 3, 5]))
  4445. Traceback (most recent call last):
  4446. ...
  4447. RuntimeError: Boolean value of Tensor with more than one value is ambiguous
  4448. >>> torch.is_nonzero(torch.tensor([]))
  4449. Traceback (most recent call last):
  4450. ...
  4451. RuntimeError: Boolean value of Tensor with no values is ambiguous
  4452. """.format(**common_args),
  4453. )
  4454. add_docstr(
  4455. torch.kron,
  4456. r"""
  4457. kron(input, other, *, out=None) -> Tensor
  4458. Computes the Kronecker product, denoted by :math:`\otimes`, of :attr:`input` and :attr:`other`.
  4459. If :attr:`input` is a :math:`(a_0 \times a_1 \times \dots \times a_n)` tensor and :attr:`other` is a
  4460. :math:`(b_0 \times b_1 \times \dots \times b_n)` tensor, the result will be a
  4461. :math:`(a_0*b_0 \times a_1*b_1 \times \dots \times a_n*b_n)` tensor with the following entries:
  4462. .. math::
  4463. (\text{input} \otimes \text{other})_{k_0, k_1, \dots, k_n} =
  4464. \text{input}_{i_0, i_1, \dots, i_n} * \text{other}_{j_0, j_1, \dots, j_n},
  4465. where :math:`k_t = i_t * b_t + j_t` for :math:`0 \leq t \leq n`.
  4466. If one tensor has fewer dimensions than the other it is unsqueezed until it has the same number of dimensions.
  4467. Supports real-valued and complex-valued inputs.
  4468. .. note::
  4469. This function generalizes the typical definition of the Kronecker product for two matrices to two tensors,
  4470. as described above. When :attr:`input` is a :math:`(m \times n)` matrix and :attr:`other` is a
  4471. :math:`(p \times q)` matrix, the result will be a :math:`(p*m \times q*n)` block matrix:
  4472. .. math::
  4473. \mathbf{A} \otimes \mathbf{B}=\begin{bmatrix}
  4474. a_{11} \mathbf{B} & \cdots & a_{1 n} \mathbf{B} \\
  4475. \vdots & \ddots & \vdots \\
  4476. a_{m 1} \mathbf{B} & \cdots & a_{m n} \mathbf{B} \end{bmatrix}
  4477. where :attr:`input` is :math:`\mathbf{A}` and :attr:`other` is :math:`\mathbf{B}`.
  4478. Arguments:
  4479. input (Tensor)
  4480. other (Tensor)
  4481. Keyword args:
  4482. out (Tensor, optional): The output tensor. Ignored if ``None``. Default: ``None``
  4483. Examples::
  4484. >>> mat1 = torch.eye(2)
  4485. >>> mat2 = torch.ones(2, 2)
  4486. >>> torch.kron(mat1, mat2)
  4487. tensor([[1., 1., 0., 0.],
  4488. [1., 1., 0., 0.],
  4489. [0., 0., 1., 1.],
  4490. [0., 0., 1., 1.]])
  4491. >>> mat1 = torch.eye(2)
  4492. >>> mat2 = torch.arange(1, 5).reshape(2, 2)
  4493. >>> torch.kron(mat1, mat2)
  4494. tensor([[1., 2., 0., 0.],
  4495. [3., 4., 0., 0.],
  4496. [0., 0., 1., 2.],
  4497. [0., 0., 3., 4.]])
  4498. """,
  4499. )
  4500. add_docstr(
  4501. torch.kthvalue,
  4502. r"""
  4503. kthvalue(input, k, dim=None, keepdim=False, *, out=None) -> (Tensor, LongTensor)
  4504. Returns a namedtuple ``(values, indices)`` where ``values`` is the :attr:`k` th
  4505. smallest element of each row of the :attr:`input` tensor in the given dimension
  4506. :attr:`dim`. And ``indices`` is the index location of each element found.
  4507. If :attr:`dim` is not given, the last dimension of the `input` is chosen.
  4508. If :attr:`keepdim` is ``True``, both the :attr:`values` and :attr:`indices` tensors
  4509. are the same size as :attr:`input`, except in the dimension :attr:`dim` where
  4510. they are of size 1. Otherwise, :attr:`dim` is squeezed
  4511. (see :func:`torch.squeeze`), resulting in both the :attr:`values` and
  4512. :attr:`indices` tensors having 1 fewer dimension than the :attr:`input` tensor.
  4513. .. note::
  4514. When :attr:`input` is a CUDA tensor and there are multiple valid
  4515. :attr:`k` th values, this function may nondeterministically return
  4516. :attr:`indices` for any of them.
  4517. Args:
  4518. {input}
  4519. k (int): k for the k-th smallest element
  4520. dim (int, optional): the dimension to find the kth value along
  4521. {opt_keepdim}
  4522. Keyword args:
  4523. out (tuple, optional): the output tuple of (Tensor, LongTensor)
  4524. can be optionally given to be used as output buffers
  4525. Example::
  4526. >>> x = torch.arange(1., 6.)
  4527. >>> x
  4528. tensor([ 1., 2., 3., 4., 5.])
  4529. >>> torch.kthvalue(x, 4)
  4530. torch.return_types.kthvalue(values=tensor(4.), indices=tensor(3))
  4531. >>> x=torch.arange(1.,7.).resize_(2,3)
  4532. >>> x
  4533. tensor([[ 1., 2., 3.],
  4534. [ 4., 5., 6.]])
  4535. >>> torch.kthvalue(x, 2, 0, True)
  4536. torch.return_types.kthvalue(values=tensor([[4., 5., 6.]]), indices=tensor([[1, 1, 1]]))
  4537. """.format(**single_dim_common),
  4538. )
  4539. add_docstr(
  4540. torch.lcm,
  4541. r"""
  4542. lcm(input, other, *, out=None) -> Tensor
  4543. Computes the element-wise least common multiple (LCM) of :attr:`input` and :attr:`other`.
  4544. Both :attr:`input` and :attr:`other` must have integer types.
  4545. .. note::
  4546. This defines :math:`lcm(0, 0) = 0` and :math:`lcm(0, a) = 0`.
  4547. Args:
  4548. {input}
  4549. other (Tensor): the second input tensor
  4550. Keyword arguments:
  4551. {out}
  4552. Example::
  4553. >>> a = torch.tensor([5, 10, 15])
  4554. >>> b = torch.tensor([3, 4, 5])
  4555. >>> torch.lcm(a, b)
  4556. tensor([15, 20, 15])
  4557. >>> c = torch.tensor([3])
  4558. >>> torch.lcm(a, c)
  4559. tensor([15, 30, 15])
  4560. """.format(**common_args),
  4561. )
  4562. add_docstr(
  4563. torch.ldexp,
  4564. r"""
  4565. ldexp(input, other, *, out=None) -> Tensor
  4566. Multiplies :attr:`input` by 2 ** :attr:`other`.
  4567. .. math::
  4568. \text{{out}}_i = \text{{input}}_i * 2^\text{{other}}_i
  4569. """
  4570. + r"""
  4571. Typically this function is used to construct floating point numbers by multiplying
  4572. mantissas in :attr:`input` with integral powers of two created from the exponents
  4573. in :attr:`other`.
  4574. Args:
  4575. {input}
  4576. other (Tensor): a tensor of exponents, typically integers.
  4577. Keyword args:
  4578. {out}
  4579. Example::
  4580. >>> torch.ldexp(torch.tensor([1.]), torch.tensor([1]))
  4581. tensor([2.])
  4582. >>> torch.ldexp(torch.tensor([1.0]), torch.tensor([1, 2, 3, 4]))
  4583. tensor([ 2., 4., 8., 16.])
  4584. """.format(**common_args),
  4585. )
  4586. add_docstr(
  4587. torch.le,
  4588. r"""
  4589. le(input, other, *, out=None) -> Tensor
  4590. Computes :math:`\text{input} \leq \text{other}` element-wise.
  4591. """
  4592. + r"""
  4593. The second argument can be a number or a tensor whose shape is
  4594. :ref:`broadcastable <broadcasting-semantics>` with the first argument.
  4595. Args:
  4596. input (Tensor): the tensor to compare
  4597. other (Tensor or Scalar): the tensor or value to compare
  4598. Keyword args:
  4599. {out}
  4600. Returns:
  4601. A boolean tensor that is True where :attr:`input` is less than or equal to
  4602. :attr:`other` and False elsewhere
  4603. Example::
  4604. >>> torch.le(torch.tensor([[1, 2], [3, 4]]), torch.tensor([[1, 1], [4, 4]]))
  4605. tensor([[True, False], [True, True]])
  4606. """.format(**common_args),
  4607. )
  4608. add_docstr(
  4609. torch.less_equal,
  4610. r"""
  4611. less_equal(input, other, *, out=None) -> Tensor
  4612. Alias for :func:`torch.le`.
  4613. """,
  4614. )
  4615. add_docstr(
  4616. torch.lerp,
  4617. r"""
  4618. lerp(input, end, weight, *, out=None)
  4619. Does a linear interpolation of two tensors :attr:`start` (given by :attr:`input`) and :attr:`end` based
  4620. on a scalar or tensor :attr:`weight` and returns the resulting :attr:`out` tensor.
  4621. .. math::
  4622. \text{out}_i = \text{start}_i + \text{weight}_i \times (\text{end}_i - \text{start}_i)
  4623. """
  4624. + r"""
  4625. The shapes of :attr:`start` and :attr:`end` must be
  4626. :ref:`broadcastable <broadcasting-semantics>`. If :attr:`weight` is a tensor, then
  4627. the shapes of :attr:`weight`, :attr:`start`, and :attr:`end` must be :ref:`broadcastable <broadcasting-semantics>`.
  4628. Args:
  4629. input (Tensor): the tensor with the starting points
  4630. end (Tensor): the tensor with the ending points
  4631. weight (float or tensor): the weight for the interpolation formula
  4632. Keyword args:
  4633. {out}
  4634. Example::
  4635. >>> start = torch.arange(1., 5.)
  4636. >>> end = torch.empty(4).fill_(10)
  4637. >>> start
  4638. tensor([ 1., 2., 3., 4.])
  4639. >>> end
  4640. tensor([ 10., 10., 10., 10.])
  4641. >>> torch.lerp(start, end, 0.5)
  4642. tensor([ 5.5000, 6.0000, 6.5000, 7.0000])
  4643. >>> torch.lerp(start, end, torch.full_like(start, 0.5))
  4644. tensor([ 5.5000, 6.0000, 6.5000, 7.0000])
  4645. """.format(**common_args),
  4646. )
  4647. add_docstr(
  4648. torch.lgamma,
  4649. r"""
  4650. lgamma(input, *, out=None) -> Tensor
  4651. Computes the natural logarithm of the absolute value of the gamma function on :attr:`input`.
  4652. .. math::
  4653. \text{out}_{i} = \ln |\Gamma(\text{input}_{i})|
  4654. """
  4655. + """
  4656. Args:
  4657. {input}
  4658. Keyword args:
  4659. {out}
  4660. Example::
  4661. >>> a = torch.arange(0.5, 2, 0.5)
  4662. >>> torch.lgamma(a)
  4663. tensor([ 0.5724, 0.0000, -0.1208])
  4664. """.format(**common_args),
  4665. )
  4666. add_docstr(
  4667. torch.linspace,
  4668. r"""
  4669. linspace(start, end, steps, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) -> Tensor
  4670. Creates a one-dimensional tensor of size :attr:`steps` whose values are evenly
  4671. spaced from :attr:`start` to :attr:`end`, inclusive. That is, the value are:
  4672. .. math::
  4673. (\text{start},
  4674. \text{start} + \frac{\text{end} - \text{start}}{\text{steps} - 1},
  4675. \ldots,
  4676. \text{start} + (\text{steps} - 2) * \frac{\text{end} - \text{start}}{\text{steps} - 1},
  4677. \text{end})
  4678. """
  4679. + """
  4680. From PyTorch 1.11 linspace requires the steps argument. Use steps=100 to restore the previous behavior.
  4681. Args:
  4682. start (float or Tensor): the starting value for the set of points. If `Tensor`, it must be 0-dimensional
  4683. end (float or Tensor): the ending value for the set of points. If `Tensor`, it must be 0-dimensional
  4684. steps (int): size of the constructed tensor
  4685. Keyword arguments:
  4686. {out}
  4687. dtype (torch.dtype, optional): the data type to perform the computation in.
  4688. Default: if None, uses the global default dtype (see torch.get_default_dtype())
  4689. when both :attr:`start` and :attr:`end` are real,
  4690. and corresponding complex dtype when either is complex.
  4691. {layout}
  4692. {device}
  4693. {requires_grad}
  4694. Example::
  4695. >>> torch.linspace(3, 10, steps=5)
  4696. tensor([ 3.0000, 4.7500, 6.5000, 8.2500, 10.0000])
  4697. >>> torch.linspace(-10, 10, steps=5)
  4698. tensor([-10., -5., 0., 5., 10.])
  4699. >>> torch.linspace(start=-10, end=10, steps=5)
  4700. tensor([-10., -5., 0., 5., 10.])
  4701. >>> torch.linspace(start=-10, end=10, steps=1)
  4702. tensor([-10.])
  4703. """.format(**factory_common_args),
  4704. )
  4705. add_docstr(
  4706. torch.log,
  4707. r"""
  4708. log(input, *, out=None) -> Tensor
  4709. Returns a new tensor with the natural logarithm of the elements
  4710. of :attr:`input`.
  4711. .. math::
  4712. y_{i} = \log_{e} (x_{i})
  4713. """
  4714. + r"""
  4715. Args:
  4716. {input}
  4717. Keyword args:
  4718. {out}
  4719. Example::
  4720. >>> a = torch.rand(5) * 5
  4721. >>> a
  4722. tensor([4.7767, 4.3234, 1.2156, 0.2411, 4.5739])
  4723. >>> torch.log(a)
  4724. tensor([ 1.5637, 1.4640, 0.1952, -1.4226, 1.5204])
  4725. """.format(**common_args),
  4726. )
  4727. add_docstr(
  4728. torch.log10,
  4729. r"""
  4730. log10(input: Tensor, *, out: Optional[Tensor]) -> Tensor
  4731. Returns a new tensor with the logarithm to the base 10 of the elements
  4732. of :attr:`input`.
  4733. .. math::
  4734. y_{i} = \log_{10} (x_{i})
  4735. """
  4736. + r"""
  4737. Args:
  4738. {input}
  4739. Keyword args:
  4740. {out}
  4741. Example::
  4742. >>> a = torch.rand(5)
  4743. >>> a
  4744. tensor([ 0.5224, 0.9354, 0.7257, 0.1301, 0.2251])
  4745. >>> torch.log10(a)
  4746. tensor([-0.2820, -0.0290, -0.1392, -0.8857, -0.6476])
  4747. """.format(**common_args),
  4748. )
  4749. add_docstr(
  4750. torch.log1p,
  4751. r"""
  4752. log1p(input, *, out=None) -> Tensor
  4753. Returns a new tensor with the natural logarithm of (1 + :attr:`input`).
  4754. .. math::
  4755. y_i = \log_{e} (x_i + 1)
  4756. """
  4757. + r"""
  4758. .. note:: This function is more accurate than :func:`torch.log` for small
  4759. values of :attr:`input`
  4760. Args:
  4761. {input}
  4762. Keyword args:
  4763. {out}
  4764. Example::
  4765. >>> a = torch.randn(5)
  4766. >>> a
  4767. tensor([-1.0090, -0.9923, 1.0249, -0.5372, 0.2492])
  4768. >>> torch.log1p(a)
  4769. tensor([ nan, -4.8653, 0.7055, -0.7705, 0.2225])
  4770. """.format(**common_args),
  4771. )
  4772. add_docstr(
  4773. torch.log2,
  4774. r"""
  4775. log2(input: Tensor, *, out: Optional[Tensor]) -> Tensor
  4776. Returns a new tensor with the logarithm to the base 2 of the elements
  4777. of :attr:`input`.
  4778. .. math::
  4779. y_{i} = \log_{2} (x_{i})
  4780. """
  4781. + r"""
  4782. Args:
  4783. {input}
  4784. Keyword args:
  4785. {out}
  4786. Example::
  4787. >>> a = torch.rand(5)
  4788. >>> a
  4789. tensor([ 0.8419, 0.8003, 0.9971, 0.5287, 0.0490])
  4790. >>> torch.log2(a)
  4791. tensor([-0.2483, -0.3213, -0.0042, -0.9196, -4.3504])
  4792. """.format(**common_args),
  4793. )
  4794. add_docstr(
  4795. torch.logaddexp,
  4796. r"""
  4797. logaddexp(input, other, *, out=None) -> Tensor
  4798. Logarithm of the sum of exponentiations of the inputs.
  4799. Calculates pointwise :math:`\log\left(e^x + e^y\right)`. This function is useful
  4800. in statistics where the calculated probabilities of events may be so small as to
  4801. exceed the range of normal floating point numbers. In such cases the logarithm
  4802. of the calculated probability is stored. This function allows adding
  4803. probabilities stored in such a fashion.
  4804. This op should be disambiguated with :func:`torch.logsumexp` which performs a
  4805. reduction on a single tensor.
  4806. Args:
  4807. {input}
  4808. other (Tensor): the second input tensor
  4809. Keyword arguments:
  4810. {out}
  4811. Example::
  4812. >>> torch.logaddexp(torch.tensor([-1.0]), torch.tensor([-1.0, -2, -3]))
  4813. tensor([-0.3069, -0.6867, -0.8731])
  4814. >>> torch.logaddexp(torch.tensor([-100.0, -200, -300]), torch.tensor([-1.0, -2, -3]))
  4815. tensor([-1., -2., -3.])
  4816. >>> torch.logaddexp(torch.tensor([1.0, 2000, 30000]), torch.tensor([-1.0, -2, -3]))
  4817. tensor([1.1269e+00, 2.0000e+03, 3.0000e+04])
  4818. """.format(**common_args),
  4819. )
  4820. add_docstr(
  4821. torch.logaddexp2,
  4822. r"""
  4823. logaddexp2(input, other, *, out=None) -> Tensor
  4824. Logarithm of the sum of exponentiations of the inputs in base-2.
  4825. Calculates pointwise :math:`\log_2\left(2^x + 2^y\right)`. See
  4826. :func:`torch.logaddexp` for more details.
  4827. Args:
  4828. {input}
  4829. other (Tensor): the second input tensor
  4830. Keyword arguments:
  4831. {out}
  4832. """.format(**common_args),
  4833. )
  4834. add_docstr(
  4835. torch.xlogy,
  4836. r"""
  4837. xlogy(input, other, *, out=None) -> Tensor
  4838. Alias for :func:`torch.special.xlogy`.
  4839. """,
  4840. )
  4841. add_docstr(
  4842. torch.logical_and,
  4843. r"""
  4844. logical_and(input, other, *, out=None) -> Tensor
  4845. Computes the element-wise logical AND of the given input tensors. Zeros are treated as ``False`` and nonzeros are
  4846. treated as ``True``.
  4847. Args:
  4848. {input}
  4849. other (Tensor): the tensor to compute AND with
  4850. Keyword args:
  4851. {out}
  4852. Example::
  4853. >>> torch.logical_and(torch.tensor([True, False, True]), torch.tensor([True, False, False]))
  4854. tensor([ True, False, False])
  4855. >>> a = torch.tensor([0, 1, 10, 0], dtype=torch.int8)
  4856. >>> b = torch.tensor([4, 0, 1, 0], dtype=torch.int8)
  4857. >>> torch.logical_and(a, b)
  4858. tensor([False, False, True, False])
  4859. >>> torch.logical_and(a.double(), b.double())
  4860. tensor([False, False, True, False])
  4861. >>> torch.logical_and(a.double(), b)
  4862. tensor([False, False, True, False])
  4863. >>> torch.logical_and(a, b, out=torch.empty(4, dtype=torch.bool))
  4864. tensor([False, False, True, False])
  4865. """.format(**common_args),
  4866. )
  4867. add_docstr(
  4868. torch.logical_not,
  4869. r"""
  4870. logical_not(input, *, out=None) -> Tensor
  4871. Computes the element-wise logical NOT of the given input tensor. If not specified, the output tensor will have the bool
  4872. dtype. If the input tensor is not a bool tensor, zeros are treated as ``False`` and non-zeros are treated as ``True``.
  4873. Args:
  4874. {input}
  4875. Keyword args:
  4876. {out}
  4877. Example::
  4878. >>> torch.logical_not(torch.tensor([True, False]))
  4879. tensor([False, True])
  4880. >>> torch.logical_not(torch.tensor([0, 1, -10], dtype=torch.int8))
  4881. tensor([ True, False, False])
  4882. >>> torch.logical_not(torch.tensor([0., 1.5, -10.], dtype=torch.double))
  4883. tensor([ True, False, False])
  4884. >>> torch.logical_not(torch.tensor([0., 1., -10.], dtype=torch.double), out=torch.empty(3, dtype=torch.int16))
  4885. tensor([1, 0, 0], dtype=torch.int16)
  4886. """.format(**common_args),
  4887. )
  4888. add_docstr(
  4889. torch.logical_or,
  4890. r"""
  4891. logical_or(input, other, *, out=None) -> Tensor
  4892. Computes the element-wise logical OR of the given input tensors. Zeros are treated as ``False`` and nonzeros are
  4893. treated as ``True``.
  4894. Args:
  4895. {input}
  4896. other (Tensor): the tensor to compute OR with
  4897. Keyword args:
  4898. {out}
  4899. Example::
  4900. >>> torch.logical_or(torch.tensor([True, False, True]), torch.tensor([True, False, False]))
  4901. tensor([ True, False, True])
  4902. >>> a = torch.tensor([0, 1, 10, 0], dtype=torch.int8)
  4903. >>> b = torch.tensor([4, 0, 1, 0], dtype=torch.int8)
  4904. >>> torch.logical_or(a, b)
  4905. tensor([ True, True, True, False])
  4906. >>> torch.logical_or(a.double(), b.double())
  4907. tensor([ True, True, True, False])
  4908. >>> torch.logical_or(a.double(), b)
  4909. tensor([ True, True, True, False])
  4910. >>> torch.logical_or(a, b, out=torch.empty(4, dtype=torch.bool))
  4911. tensor([ True, True, True, False])
  4912. """.format(**common_args),
  4913. )
  4914. add_docstr(
  4915. torch.logical_xor,
  4916. r"""
  4917. logical_xor(input: Tensor, other: Tensor, *, out: Optional[Tensor]) -> Tensor
  4918. Computes the element-wise logical XOR of the given input tensors. Zeros are treated as ``False`` and nonzeros are
  4919. treated as ``True``.
  4920. Args:
  4921. {input}
  4922. other (Tensor): the tensor to compute XOR with
  4923. Keyword args:
  4924. {out}
  4925. Example::
  4926. >>> torch.logical_xor(torch.tensor([True, False, True]), torch.tensor([True, False, False]))
  4927. tensor([False, False, True])
  4928. >>> a = torch.tensor([0, 1, 10, 0], dtype=torch.int8)
  4929. >>> b = torch.tensor([4, 0, 1, 0], dtype=torch.int8)
  4930. >>> torch.logical_xor(a, b)
  4931. tensor([ True, True, False, False])
  4932. >>> torch.logical_xor(a.double(), b.double())
  4933. tensor([ True, True, False, False])
  4934. >>> torch.logical_xor(a.double(), b)
  4935. tensor([ True, True, False, False])
  4936. >>> torch.logical_xor(a, b, out=torch.empty(4, dtype=torch.bool))
  4937. tensor([ True, True, False, False])
  4938. """.format(**common_args),
  4939. )
  4940. add_docstr(
  4941. torch.logspace,
  4942. """
  4943. logspace(start, end, steps, base=10.0, *, \
  4944. out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) -> Tensor
  4945. """
  4946. + r"""
  4947. Creates a one-dimensional tensor of size :attr:`steps` whose values are evenly
  4948. spaced from :math:`{{\text{{base}}}}^{{\text{{start}}}}` to
  4949. :math:`{{\text{{base}}}}^{{\text{{end}}}}`, inclusive, on a logarithmic scale
  4950. with base :attr:`base`. That is, the values are:
  4951. .. math::
  4952. (\text{base}^{\text{start}},
  4953. \text{base}^{(\text{start} + \frac{\text{end} - \text{start}}{ \text{steps} - 1})},
  4954. \ldots,
  4955. \text{base}^{(\text{start} + (\text{steps} - 2) * \frac{\text{end} - \text{start}}{ \text{steps} - 1})},
  4956. \text{base}^{\text{end}})
  4957. """
  4958. + """
  4959. From PyTorch 1.11 logspace requires the steps argument. Use steps=100 to restore the previous behavior.
  4960. Args:
  4961. start (float or Tensor): the starting value for the set of points. If `Tensor`, it must be 0-dimensional
  4962. end (float or Tensor): the ending value for the set of points. If `Tensor`, it must be 0-dimensional
  4963. steps (int): size of the constructed tensor
  4964. base (float, optional): base of the logarithm function. Default: ``10.0``.
  4965. Keyword arguments:
  4966. {out}
  4967. dtype (torch.dtype, optional): the data type to perform the computation in.
  4968. Default: if None, uses the global default dtype (see torch.get_default_dtype())
  4969. when both :attr:`start` and :attr:`end` are real,
  4970. and corresponding complex dtype when either is complex.
  4971. {layout}
  4972. {device}
  4973. {requires_grad}
  4974. Example::
  4975. >>> torch.logspace(start=-10, end=10, steps=5)
  4976. tensor([ 1.0000e-10, 1.0000e-05, 1.0000e+00, 1.0000e+05, 1.0000e+10])
  4977. >>> torch.logspace(start=0.1, end=1.0, steps=5)
  4978. tensor([ 1.2589, 2.1135, 3.5481, 5.9566, 10.0000])
  4979. >>> torch.logspace(start=0.1, end=1.0, steps=1)
  4980. tensor([1.2589])
  4981. >>> torch.logspace(start=2, end=2, steps=1, base=2)
  4982. tensor([4.0])
  4983. """.format(**factory_common_args),
  4984. )
  4985. add_docstr(
  4986. torch.logsumexp,
  4987. r"""
  4988. logsumexp(input, dim, keepdim=False, *, out=None)
  4989. Returns the log of summed exponentials of each row of the :attr:`input`
  4990. tensor in the given dimension :attr:`dim`. The computation is numerically
  4991. stabilized.
  4992. For summation index :math:`j` given by `dim` and other indices :math:`i`, the result is
  4993. .. math::
  4994. \text{{logsumexp}}(x)_{{i}} = \log \sum_j \exp(x_{{ij}})
  4995. {keepdim_details}
  4996. Args:
  4997. {input}
  4998. {dim}
  4999. {opt_keepdim}
  5000. Keyword args:
  5001. {out}
  5002. Example::
  5003. >>> a = torch.randn(3, 3)
  5004. >>> torch.logsumexp(a, 1)
  5005. tensor([1.4907, 1.0593, 1.5696])
  5006. >>> torch.dist(torch.logsumexp(a, 1), torch.log(torch.sum(torch.exp(a), 1)))
  5007. tensor(1.6859e-07)
  5008. """.format(**multi_dim_common),
  5009. )
  5010. add_docstr(
  5011. torch.lt,
  5012. r"""
  5013. lt(input, other, *, out=None) -> Tensor
  5014. Computes :math:`\text{input} < \text{other}` element-wise.
  5015. """
  5016. + r"""
  5017. The second argument can be a number or a tensor whose shape is
  5018. :ref:`broadcastable <broadcasting-semantics>` with the first argument.
  5019. Args:
  5020. input (Tensor): the tensor to compare
  5021. other (Tensor or float): the tensor or value to compare
  5022. Keyword args:
  5023. {out}
  5024. Returns:
  5025. A boolean tensor that is True where :attr:`input` is less than :attr:`other` and False elsewhere
  5026. Example::
  5027. >>> torch.lt(torch.tensor([[1, 2], [3, 4]]), torch.tensor([[1, 1], [4, 4]]))
  5028. tensor([[False, False], [True, False]])
  5029. """.format(**common_args),
  5030. )
  5031. add_docstr(
  5032. torch.lu_unpack,
  5033. r"""
  5034. lu_unpack(LU_data, LU_pivots, unpack_data=True, unpack_pivots=True, *, out=None) -> (Tensor, Tensor, Tensor)
  5035. Unpacks the LU decomposition returned by :func:`~linalg.lu_factor` into the `P, L, U` matrices.
  5036. .. seealso::
  5037. :func:`~linalg.lu` returns the matrices from the LU decomposition. Its gradient formula is more efficient
  5038. than that of doing :func:`~linalg.lu_factor` followed by :func:`~linalg.lu_unpack`.
  5039. Args:
  5040. LU_data (Tensor): the packed LU factorization data
  5041. LU_pivots (Tensor): the packed LU factorization pivots
  5042. unpack_data (bool): flag indicating if the data should be unpacked.
  5043. If ``False``, then the returned ``L`` and ``U`` are empty tensors.
  5044. Default: ``True``
  5045. unpack_pivots (bool): flag indicating if the pivots should be unpacked into a permutation matrix ``P``.
  5046. If ``False``, then the returned ``P`` is an empty tensor.
  5047. Default: ``True``
  5048. Keyword args:
  5049. out (tuple, optional): output tuple of three tensors. Ignored if `None`.
  5050. Returns:
  5051. A namedtuple ``(P, L, U)``
  5052. Examples::
  5053. >>> A = torch.randn(2, 3, 3)
  5054. >>> LU, pivots = torch.linalg.lu_factor(A)
  5055. >>> P, L, U = torch.lu_unpack(LU, pivots)
  5056. >>> # We can recover A from the factorization
  5057. >>> A_ = P @ L @ U
  5058. >>> torch.allclose(A, A_)
  5059. True
  5060. >>> # LU factorization of a rectangular matrix:
  5061. >>> A = torch.randn(2, 3, 2)
  5062. >>> LU, pivots = torch.linalg.lu_factor(A)
  5063. >>> P, L, U = torch.lu_unpack(LU, pivots)
  5064. >>> # P, L, U are the same as returned by linalg.lu
  5065. >>> P_, L_, U_ = torch.linalg.lu(A)
  5066. >>> torch.allclose(P, P_) and torch.allclose(L, L_) and torch.allclose(U, U_)
  5067. True
  5068. """.format(**common_args),
  5069. )
  5070. add_docstr(
  5071. torch.less,
  5072. r"""
  5073. less(input, other, *, out=None) -> Tensor
  5074. Alias for :func:`torch.lt`.
  5075. """,
  5076. )
  5077. add_docstr(
  5078. torch.lu_solve,
  5079. r"""
  5080. lu_solve(b, LU_data, LU_pivots, *, out=None) -> Tensor
  5081. Returns the LU solve of the linear system :math:`Ax = b` using the partially pivoted
  5082. LU factorization of A from :func:`~linalg.lu_factor`.
  5083. This function supports ``float``, ``double``, ``cfloat`` and ``cdouble`` dtypes for :attr:`input`.
  5084. .. warning::
  5085. :func:`torch.lu_solve` is deprecated in favor of :func:`torch.linalg.lu_solve`.
  5086. :func:`torch.lu_solve` will be removed in a future PyTorch release.
  5087. ``X = torch.lu_solve(B, LU, pivots)`` should be replaced with
  5088. .. code:: python
  5089. X = linalg.lu_solve(LU, pivots, B)
  5090. Arguments:
  5091. b (Tensor): the RHS tensor of size :math:`(*, m, k)`, where :math:`*`
  5092. is zero or more batch dimensions.
  5093. LU_data (Tensor): the pivoted LU factorization of A from :meth:`~linalg.lu_factor` of size :math:`(*, m, m)`,
  5094. where :math:`*` is zero or more batch dimensions.
  5095. LU_pivots (IntTensor): the pivots of the LU factorization from :meth:`~linalg.lu_factor` of size :math:`(*, m)`,
  5096. where :math:`*` is zero or more batch dimensions.
  5097. The batch dimensions of :attr:`LU_pivots` must be equal to the batch dimensions of
  5098. :attr:`LU_data`.
  5099. Keyword args:
  5100. {out}
  5101. Example::
  5102. >>> A = torch.randn(2, 3, 3)
  5103. >>> b = torch.randn(2, 3, 1)
  5104. >>> LU, pivots = torch.linalg.lu_factor(A)
  5105. >>> x = torch.lu_solve(b, LU, pivots)
  5106. >>> torch.dist(A @ x, b)
  5107. tensor(1.00000e-07 *
  5108. 2.8312)
  5109. """.format(**common_args),
  5110. )
  5111. add_docstr(
  5112. torch.masked_select,
  5113. r"""
  5114. masked_select(input, mask, *, out=None) -> Tensor
  5115. Returns a new 1-D tensor which indexes the :attr:`input` tensor according to
  5116. the boolean mask :attr:`mask` which is a `BoolTensor`.
  5117. The shapes of the :attr:`mask` tensor and the :attr:`input` tensor don't need
  5118. to match, but they must be :ref:`broadcastable <broadcasting-semantics>`.
  5119. .. note:: The returned tensor does **not** use the same storage
  5120. as the original tensor
  5121. Args:
  5122. {input}
  5123. mask (BoolTensor): the tensor containing the binary mask to index with
  5124. Keyword args:
  5125. {out}
  5126. Example::
  5127. >>> x = torch.randn(3, 4)
  5128. >>> x
  5129. tensor([[ 0.3552, -2.3825, -0.8297, 0.3477],
  5130. [-1.2035, 1.2252, 0.5002, 0.6248],
  5131. [ 0.1307, -2.0608, 0.1244, 2.0139]])
  5132. >>> mask = x.ge(0.5)
  5133. >>> mask
  5134. tensor([[False, False, False, False],
  5135. [False, True, True, True],
  5136. [False, False, False, True]])
  5137. >>> torch.masked_select(x, mask)
  5138. tensor([ 1.2252, 0.5002, 0.6248, 2.0139])
  5139. """.format(**common_args),
  5140. )
  5141. add_docstr(
  5142. torch.matrix_power,
  5143. r"""
  5144. matrix_power(input, n, *, out=None) -> Tensor
  5145. Alias for :func:`torch.linalg.matrix_power`
  5146. """,
  5147. )
  5148. add_docstr(
  5149. torch.matrix_exp,
  5150. r"""
  5151. matrix_exp(A) -> Tensor
  5152. Alias for :func:`torch.linalg.matrix_exp`.
  5153. """,
  5154. )
  5155. add_docstr(
  5156. torch.max,
  5157. r"""
  5158. max(input, *, out=None) -> Tensor
  5159. Returns the maximum value of all elements in the ``input`` tensor.
  5160. .. note::
  5161. The difference between ``max``/``min`` and ``amax``/``amin`` is:
  5162. - ``amax``/``amin`` supports reducing on multiple dimensions,
  5163. - ``amax``/``amin`` does not return indices.
  5164. Both ``amax``/``amin`` evenly distribute gradients between equal values
  5165. when there are multiple input elements with the same minimum or maximum value.
  5166. For ``max``/``min``:
  5167. - If reduce over all dimensions(no dim specified), gradients evenly distribute between equally ``max``/``min`` values.
  5168. - If reduce over one specified axis, only propagate to the indexed element.
  5169. Args:
  5170. {input}
  5171. Keyword args:
  5172. {out}
  5173. Example::
  5174. >>> a = torch.randn(1, 3)
  5175. >>> a
  5176. tensor([[ 0.6763, 0.7445, -2.2369]])
  5177. >>> torch.max(a)
  5178. tensor(0.7445)
  5179. .. function:: max(input, dim, keepdim=False, *, out=None) -> (Tensor, LongTensor)
  5180. :noindex:
  5181. Returns a namedtuple ``(values, indices)`` where ``values`` is the maximum
  5182. value of each row of the :attr:`input` tensor in the given dimension
  5183. :attr:`dim`. And ``indices`` is the index location of each maximum value found
  5184. (argmax).
  5185. If ``keepdim`` is ``True``, the output tensors are of the same size
  5186. as ``input`` except in the dimension ``dim`` where they are of size 1.
  5187. Otherwise, ``dim`` is squeezed (see :func:`torch.squeeze`), resulting
  5188. in the output tensors having 1 fewer dimension than ``input``.
  5189. .. note:: If there are multiple maximal values in a reduced row then
  5190. the indices of the first maximal value are returned.
  5191. Args:
  5192. {input}
  5193. {opt_dim_without_none}
  5194. {opt_keepdim}
  5195. Keyword args:
  5196. out (tuple, optional): the result tuple of two output tensors (max, max_indices)
  5197. Example::
  5198. >>> a = torch.randn(4, 4)
  5199. >>> a
  5200. tensor([[-1.2360, -0.2942, -0.1222, 0.8475],
  5201. [ 1.1949, -1.1127, -2.2379, -0.6702],
  5202. [ 1.5717, -0.9207, 0.1297, -1.8768],
  5203. [-0.6172, 1.0036, -0.6060, -0.2432]])
  5204. >>> torch.max(a, 1)
  5205. torch.return_types.max(values=tensor([0.8475, 1.1949, 1.5717, 1.0036]), indices=tensor([3, 0, 0, 1]))
  5206. >>> a = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
  5207. >>> a.max(dim=1, keepdim=True)
  5208. torch.return_types.max(
  5209. values=tensor([[2.], [4.]]),
  5210. indices=tensor([[1], [1]]))
  5211. >>> a.max(dim=1, keepdim=False)
  5212. torch.return_types.max(
  5213. values=tensor([2., 4.]),
  5214. indices=tensor([1, 1]))
  5215. .. function:: max(input, other, *, out=None) -> Tensor
  5216. :noindex:
  5217. See :func:`torch.maximum`.
  5218. """.format(**single_dim_common),
  5219. )
  5220. add_docstr(
  5221. torch.maximum,
  5222. r"""
  5223. maximum(input, other, *, out=None) -> Tensor
  5224. Computes the element-wise maximum of :attr:`input` and :attr:`other`.
  5225. .. note::
  5226. If one of the elements being compared is a NaN, then that element is returned.
  5227. :func:`maximum` is not supported for tensors with complex dtypes.
  5228. Args:
  5229. {input}
  5230. other (Tensor): the second input tensor
  5231. Keyword args:
  5232. {out}
  5233. Example::
  5234. >>> a = torch.tensor((1, 2, -1))
  5235. >>> b = torch.tensor((3, 0, 4))
  5236. >>> torch.maximum(a, b)
  5237. tensor([3, 2, 4])
  5238. """.format(**common_args),
  5239. )
  5240. add_docstr(
  5241. torch.fmax,
  5242. r"""
  5243. fmax(input, other, *, out=None) -> Tensor
  5244. Computes the element-wise maximum of :attr:`input` and :attr:`other`.
  5245. This is like :func:`torch.maximum` except it handles NaNs differently:
  5246. if exactly one of the two elements being compared is a NaN then the non-NaN element is taken as the maximum.
  5247. Only if both elements are NaN is NaN propagated.
  5248. This function is a wrapper around C++'s ``std::fmax`` and is similar to NumPy's ``fmax`` function.
  5249. Supports :ref:`broadcasting to a common shape <broadcasting-semantics>`,
  5250. :ref:`type promotion <type-promotion-doc>`, and integer and floating-point inputs.
  5251. Args:
  5252. {input}
  5253. other (Tensor): the second input tensor
  5254. Keyword args:
  5255. {out}
  5256. Example::
  5257. >>> a = torch.tensor([9.7, float('nan'), 3.1, float('nan')])
  5258. >>> b = torch.tensor([-2.2, 0.5, float('nan'), float('nan')])
  5259. >>> torch.fmax(a, b)
  5260. tensor([9.7000, 0.5000, 3.1000, nan])
  5261. """.format(**common_args),
  5262. )
  5263. add_docstr(
  5264. torch.amax,
  5265. r"""
  5266. amax(input, dim, keepdim=False, *, out=None) -> Tensor
  5267. Returns the maximum value of each slice of the :attr:`input` tensor in the given
  5268. dimension(s) :attr:`dim`.
  5269. .. note::
  5270. The difference between ``max``/``min`` and ``amax``/``amin`` is:
  5271. - ``amax``/``amin`` supports reducing on multiple dimensions,
  5272. - ``amax``/``amin`` does not return indices.
  5273. Both ``amax``/``amin`` evenly distribute gradients between equal values
  5274. when there are multiple input elements with the same minimum or maximum value.
  5275. For ``max``/``min``:
  5276. - If reduce over all dimensions(no dim specified), gradients evenly distribute between equally ``max``/``min`` values.
  5277. - If reduce over one specified axis, only propagate to the indexed element.
  5278. {keepdim_details}
  5279. Args:
  5280. {input}
  5281. {opt_dim_all_reduce}
  5282. {opt_keepdim}
  5283. Keyword args:
  5284. {out}
  5285. Example::
  5286. >>> a = torch.randn(4, 4)
  5287. >>> a
  5288. tensor([[ 0.8177, 1.4878, -0.2491, 0.9130],
  5289. [-0.7158, 1.1775, 2.0992, 0.4817],
  5290. [-0.0053, 0.0164, -1.3738, -0.0507],
  5291. [ 1.9700, 1.1106, -1.0318, -1.0816]])
  5292. >>> torch.amax(a, 1)
  5293. tensor([1.4878, 2.0992, 0.0164, 1.9700])
  5294. """.format(**multi_dim_common),
  5295. )
  5296. add_docstr(
  5297. torch.argmax,
  5298. r"""
  5299. argmax(input) -> LongTensor
  5300. Returns the indices of the maximum value of all elements in the :attr:`input` tensor.
  5301. This is the second value returned by :meth:`torch.max`. See its
  5302. documentation for the exact semantics of this method.
  5303. .. note:: If there are multiple maximal values then the indices of the first maximal value are returned.
  5304. Args:
  5305. {input}
  5306. Example::
  5307. >>> a = torch.randn(4, 4)
  5308. >>> a
  5309. tensor([[ 1.3398, 0.2663, -0.2686, 0.2450],
  5310. [-0.7401, -0.8805, -0.3402, -1.1936],
  5311. [ 0.4907, -1.3948, -1.0691, -0.3132],
  5312. [-1.6092, 0.5419, -0.2993, 0.3195]])
  5313. >>> torch.argmax(a)
  5314. tensor(0)
  5315. .. function:: argmax(input, dim, keepdim=False) -> LongTensor
  5316. :noindex:
  5317. Returns the indices of the maximum values of a tensor across a dimension.
  5318. This is the second value returned by :meth:`torch.max`. See its
  5319. documentation for the exact semantics of this method.
  5320. Args:
  5321. {input}
  5322. {opt_dim} If ``None``, the argmax of the flattened input is returned.
  5323. {opt_keepdim}
  5324. Example::
  5325. >>> a = torch.randn(4, 4)
  5326. >>> a
  5327. tensor([[ 1.3398, 0.2663, -0.2686, 0.2450],
  5328. [-0.7401, -0.8805, -0.3402, -1.1936],
  5329. [ 0.4907, -1.3948, -1.0691, -0.3132],
  5330. [-1.6092, 0.5419, -0.2993, 0.3195]])
  5331. >>> torch.argmax(a, dim=1)
  5332. tensor([ 0, 2, 0, 1])
  5333. """.format(**single_dim_common),
  5334. )
  5335. add_docstr(
  5336. torch.argwhere,
  5337. r"""
  5338. argwhere(input) -> Tensor
  5339. Returns a tensor containing the indices of all non-zero elements of
  5340. :attr:`input`. Each row in the result contains the indices of a non-zero
  5341. element in :attr:`input`. The result is sorted lexicographically, with
  5342. the last index changing the fastest (C-style).
  5343. If :attr:`input` has :math:`n` dimensions, then the resulting indices tensor
  5344. :attr:`out` is of size :math:`(z \times n)`, where :math:`z` is the total number of
  5345. non-zero elements in the :attr:`input` tensor.
  5346. .. note::
  5347. This function is similar to NumPy's `argwhere`.
  5348. When :attr:`input` is on CUDA, this function causes host-device synchronization.
  5349. Args:
  5350. {input}
  5351. Example::
  5352. >>> t = torch.tensor([1, 0, 1])
  5353. >>> torch.argwhere(t)
  5354. tensor([[0],
  5355. [2]])
  5356. >>> t = torch.tensor([[1, 0, 1], [0, 1, 1]])
  5357. >>> torch.argwhere(t)
  5358. tensor([[0, 0],
  5359. [0, 2],
  5360. [1, 1],
  5361. [1, 2]])
  5362. """,
  5363. )
  5364. add_docstr(
  5365. torch.mean,
  5366. r"""
  5367. mean(input, *, dtype=None) -> Tensor
  5368. .. note::
  5369. If the `input` tensor is empty, ``torch.mean()`` returns ``nan``.
  5370. This behavior is consistent with NumPy and follows the definition
  5371. that the mean over an empty set is undefined.
  5372. Returns the mean value of all elements in the :attr:`input` tensor. Input must be floating point or complex.
  5373. Args:
  5374. input (Tensor):
  5375. the input tensor, either of floating point or complex dtype
  5376. Keyword args:
  5377. {dtype}
  5378. Example::
  5379. >>> a = torch.randn(1, 3)
  5380. >>> a
  5381. tensor([[ 0.2294, -0.5481, 1.3288]])
  5382. >>> torch.mean(a)
  5383. tensor(0.3367)
  5384. .. function:: mean(input, dim, keepdim=False, *, dtype=None, out=None) -> Tensor
  5385. :noindex:
  5386. Returns the mean value of each row of the :attr:`input` tensor in the given
  5387. dimension :attr:`dim`. If :attr:`dim` is a list of dimensions,
  5388. reduce over all of them.
  5389. {keepdim_details}
  5390. Args:
  5391. {input}
  5392. {opt_dim_all_reduce}
  5393. {opt_keepdim}
  5394. Keyword args:
  5395. {dtype}
  5396. {out}
  5397. .. seealso::
  5398. :func:`torch.nanmean` computes the mean value of `non-NaN` elements.
  5399. Example::
  5400. >>> a = torch.randn(4, 4)
  5401. >>> a
  5402. tensor([[-0.3841, 0.6320, 0.4254, -0.7384],
  5403. [-0.9644, 1.0131, -0.6549, -1.4279],
  5404. [-0.2951, -1.3350, -0.7694, 0.5600],
  5405. [ 1.0842, -0.9580, 0.3623, 0.2343]])
  5406. >>> torch.mean(a, 1)
  5407. tensor([-0.0163, -0.5085, -0.4599, 0.1807])
  5408. >>> torch.mean(a, 1, True)
  5409. tensor([[-0.0163],
  5410. [-0.5085],
  5411. [-0.4599],
  5412. [ 0.1807]])
  5413. """.format(**multi_dim_common),
  5414. )
  5415. add_docstr(
  5416. torch.nanmean,
  5417. r"""
  5418. nanmean(input, dim=None, keepdim=False, *, dtype=None, out=None) -> Tensor
  5419. Computes the mean of all `non-NaN` elements along the specified dimensions.
  5420. Input must be floating point or complex.
  5421. This function is identical to :func:`torch.mean` when there are no `NaN` values
  5422. in the :attr:`input` tensor. In the presence of `NaN`, :func:`torch.mean` will
  5423. propagate the `NaN` to the output whereas :func:`torch.nanmean` will ignore the
  5424. `NaN` values (`torch.nanmean(a)` is equivalent to `torch.mean(a[~a.isnan()])`).
  5425. {keepdim_details}
  5426. Args:
  5427. input (Tensor): the input tensor, either of floating point or complex dtype
  5428. {opt_dim_all_reduce}
  5429. {opt_keepdim}
  5430. Keyword args:
  5431. {dtype}
  5432. {out}
  5433. .. seealso::
  5434. :func:`torch.mean` computes the mean value, propagating `NaN`.
  5435. Example::
  5436. >>> x = torch.tensor([[torch.nan, 1, 2], [1, 2, 3]])
  5437. >>> x.mean()
  5438. tensor(nan)
  5439. >>> x.nanmean()
  5440. tensor(1.8000)
  5441. >>> x.mean(dim=0)
  5442. tensor([ nan, 1.5000, 2.5000])
  5443. >>> x.nanmean(dim=0)
  5444. tensor([1.0000, 1.5000, 2.5000])
  5445. # If all elements in the reduced dimensions are NaN then the result is NaN
  5446. >>> torch.tensor([torch.nan]).nanmean()
  5447. tensor(nan)
  5448. """.format(**multi_dim_common),
  5449. )
  5450. add_docstr(
  5451. torch.median,
  5452. r"""
  5453. median(input) -> Tensor
  5454. Returns the median of the values in :attr:`input`.
  5455. .. note::
  5456. The median is not unique for :attr:`input` tensors with an even number
  5457. of elements. In this case the lower of the two medians is returned. To
  5458. compute the mean of both medians, use :func:`torch.quantile` with ``q=0.5`` instead.
  5459. .. warning::
  5460. This function produces deterministic (sub)gradients unlike ``median(dim=0)``
  5461. Args:
  5462. {input}
  5463. Example::
  5464. >>> a = torch.randn(1, 3)
  5465. >>> a
  5466. tensor([[ 1.5219, -1.5212, 0.2202]])
  5467. >>> torch.median(a)
  5468. tensor(0.2202)
  5469. .. function:: median(input, dim=-1, keepdim=False, *, out=None) -> (Tensor, LongTensor)
  5470. :noindex:
  5471. Returns a namedtuple ``(values, indices)`` where ``values`` contains the median of each row of :attr:`input`
  5472. in the dimension :attr:`dim`, and ``indices`` contains the index of the median values found in the dimension :attr:`dim`.
  5473. By default, :attr:`dim` is the last dimension of the :attr:`input` tensor.
  5474. If :attr:`keepdim` is ``True``, the output tensors are of the same size
  5475. as :attr:`input` except in the dimension :attr:`dim` where they are of size 1.
  5476. Otherwise, :attr:`dim` is squeezed (see :func:`torch.squeeze`), resulting in
  5477. the outputs tensor having 1 fewer dimension than :attr:`input`.
  5478. .. note::
  5479. The median is not unique for :attr:`input` tensors with an even number
  5480. of elements in the dimension :attr:`dim`. In this case the lower of the
  5481. two medians is returned. To compute the mean of both medians in
  5482. :attr:`input`, use :func:`torch.quantile` with ``q=0.5`` instead.
  5483. .. warning::
  5484. ``indices`` does not necessarily contain the first occurrence of each
  5485. median value found, unless it is unique.
  5486. The exact implementation details are device-specific.
  5487. Do not expect the same result when run on CPU and GPU in general.
  5488. For the same reason do not expect the gradients to be deterministic.
  5489. Args:
  5490. {input}
  5491. {opt_dim_all_reduce}
  5492. {opt_keepdim}
  5493. Keyword args:
  5494. out ((Tensor, Tensor), optional): The first tensor will be populated with the median values and the second
  5495. tensor, which must have dtype long, with their indices in the dimension
  5496. :attr:`dim` of :attr:`input`.
  5497. Example::
  5498. >>> a = torch.randn(4, 5)
  5499. >>> a
  5500. tensor([[ 0.2505, -0.3982, -0.9948, 0.3518, -1.3131],
  5501. [ 0.3180, -0.6993, 1.0436, 0.0438, 0.2270],
  5502. [-0.2751, 0.7303, 0.2192, 0.3321, 0.2488],
  5503. [ 1.0778, -1.9510, 0.7048, 0.4742, -0.7125]])
  5504. >>> torch.median(a, 1)
  5505. torch.return_types.median(values=tensor([-0.3982, 0.2270, 0.2488, 0.4742]), indices=tensor([1, 4, 4, 3]))
  5506. """.format(**single_dim_common),
  5507. )
  5508. add_docstr(
  5509. torch.nanmedian,
  5510. r"""
  5511. nanmedian(input) -> Tensor
  5512. Returns the median of the values in :attr:`input`, ignoring ``NaN`` values.
  5513. This function is identical to :func:`torch.median` when there are no ``NaN`` values in :attr:`input`.
  5514. When :attr:`input` has one or more ``NaN`` values, :func:`torch.median` will always return ``NaN``,
  5515. while this function will return the median of the non-``NaN`` elements in :attr:`input`.
  5516. If all the elements in :attr:`input` are ``NaN`` it will also return ``NaN``.
  5517. Args:
  5518. {input}
  5519. Example::
  5520. >>> a = torch.tensor([1, float('nan'), 3, 2])
  5521. >>> a.median()
  5522. tensor(nan)
  5523. >>> a.nanmedian()
  5524. tensor(2.)
  5525. .. function:: nanmedian(input, dim=-1, keepdim=False, *, out=None) -> (Tensor, LongTensor)
  5526. :noindex:
  5527. Returns a namedtuple ``(values, indices)`` where ``values`` contains the median of each row of :attr:`input`
  5528. in the dimension :attr:`dim`, ignoring ``NaN`` values, and ``indices`` contains the index of the median values
  5529. found in the dimension :attr:`dim`.
  5530. This function is identical to :func:`torch.median` when there are no ``NaN`` values in a reduced row. When a reduced row has
  5531. one or more ``NaN`` values, :func:`torch.median` will always reduce it to ``NaN``, while this function will reduce it to the
  5532. median of the non-``NaN`` elements. If all the elements in a reduced row are ``NaN`` then it will be reduced to ``NaN``, too.
  5533. Args:
  5534. {input}
  5535. {opt_dim_all_reduce}
  5536. {opt_keepdim}
  5537. Keyword args:
  5538. out ((Tensor, Tensor), optional): The first tensor will be populated with the median values and the second
  5539. tensor, which must have dtype long, with their indices in the dimension
  5540. :attr:`dim` of :attr:`input`.
  5541. Example::
  5542. >>> a = torch.tensor([[2, 3, 1], [float('nan'), 1, float('nan')]])
  5543. >>> a
  5544. tensor([[2., 3., 1.],
  5545. [nan, 1., nan]])
  5546. >>> a.median(0)
  5547. torch.return_types.median(values=tensor([nan, 1., nan]), indices=tensor([1, 1, 1]))
  5548. >>> a.nanmedian(0)
  5549. torch.return_types.nanmedian(values=tensor([2., 1., 1.]), indices=tensor([0, 1, 0]))
  5550. """.format(**single_dim_common),
  5551. )
  5552. add_docstr(
  5553. torch.quantile,
  5554. r"""
  5555. quantile(input, q, dim=None, keepdim=False, *, interpolation='linear', out=None) -> Tensor
  5556. Computes the q-th quantiles of each row of the :attr:`input` tensor along the dimension :attr:`dim`.
  5557. To compute the quantile, we map q in [0, 1] to the range of indices [0, n] to find the location
  5558. of the quantile in the sorted input. If the quantile lies between two data points ``a < b`` with
  5559. indices ``i`` and ``j`` in the sorted order, result is computed according to the given
  5560. :attr:`interpolation` method as follows:
  5561. - ``linear``: ``a + (b - a) * fraction``, where ``fraction`` is the fractional part of the computed quantile index.
  5562. - ``lower``: ``a``.
  5563. - ``higher``: ``b``.
  5564. - ``nearest``: ``a`` or ``b``, whichever's index is closer to the computed quantile index (rounding down for .5 fractions).
  5565. - ``midpoint``: ``(a + b) / 2``.
  5566. If :attr:`q` is a 1D tensor, the first dimension of the output represents the quantiles and has size
  5567. equal to the size of :attr:`q`, the remaining dimensions are what remains from the reduction.
  5568. .. note::
  5569. By default :attr:`dim` is ``None`` resulting in the :attr:`input` tensor being flattened before computation.
  5570. Args:
  5571. {input}
  5572. q (float or Tensor): a scalar or 1D tensor of values in the range [0, 1].
  5573. {opt_dim}
  5574. {opt_keepdim}
  5575. Keyword arguments:
  5576. interpolation (str, optional): interpolation method to use when the desired quantile lies between two data points.
  5577. Can be ``linear``, ``lower``, ``higher``, ``midpoint`` and ``nearest``.
  5578. Default is ``linear``.
  5579. {out}
  5580. Example::
  5581. >>> a = torch.randn(2, 3)
  5582. >>> a
  5583. tensor([[ 0.0795, -1.2117, 0.9765],
  5584. [ 1.1707, 0.6706, 0.4884]])
  5585. >>> q = torch.tensor([0.25, 0.5, 0.75])
  5586. >>> torch.quantile(a, q, dim=1, keepdim=True)
  5587. tensor([[[-0.5661],
  5588. [ 0.5795]],
  5589. [[ 0.0795],
  5590. [ 0.6706]],
  5591. [[ 0.5280],
  5592. [ 0.9206]]])
  5593. >>> torch.quantile(a, q, dim=1, keepdim=True).shape
  5594. torch.Size([3, 2, 1])
  5595. >>> a = torch.arange(4.)
  5596. >>> a
  5597. tensor([0., 1., 2., 3.])
  5598. >>> torch.quantile(a, 0.6, interpolation='linear')
  5599. tensor(1.8000)
  5600. >>> torch.quantile(a, 0.6, interpolation='lower')
  5601. tensor(1.)
  5602. >>> torch.quantile(a, 0.6, interpolation='higher')
  5603. tensor(2.)
  5604. >>> torch.quantile(a, 0.6, interpolation='midpoint')
  5605. tensor(1.5000)
  5606. >>> torch.quantile(a, 0.6, interpolation='nearest')
  5607. tensor(2.)
  5608. >>> torch.quantile(a, 0.4, interpolation='nearest')
  5609. tensor(1.)
  5610. """.format(**single_dim_common),
  5611. )
  5612. add_docstr(
  5613. torch.nanquantile,
  5614. r"""
  5615. nanquantile(input, q, dim=None, keepdim=False, *, interpolation='linear', out=None) -> Tensor
  5616. This is a variant of :func:`torch.quantile` that "ignores" ``NaN`` values,
  5617. computing the quantiles :attr:`q` as if ``NaN`` values in :attr:`input` did
  5618. not exist. If all values in a reduced row are ``NaN`` then the quantiles for
  5619. that reduction will be ``NaN``. See the documentation for :func:`torch.quantile`.
  5620. Args:
  5621. {input}
  5622. q (float or Tensor): a scalar or 1D tensor of quantile values in the range [0, 1]
  5623. {opt_dim_all_reduce}
  5624. {opt_keepdim}
  5625. Keyword arguments:
  5626. interpolation (str): interpolation method to use when the desired quantile lies between two data points.
  5627. Can be ``linear``, ``lower``, ``higher``, ``midpoint`` and ``nearest``.
  5628. Default is ``linear``.
  5629. {out}
  5630. Example::
  5631. >>> t = torch.tensor([float('nan'), 1, 2])
  5632. >>> t.quantile(0.5)
  5633. tensor(nan)
  5634. >>> t.nanquantile(0.5)
  5635. tensor(1.5000)
  5636. >>> t = torch.tensor([[float('nan'), float('nan')], [1, 2]])
  5637. >>> t
  5638. tensor([[nan, nan],
  5639. [1., 2.]])
  5640. >>> t.nanquantile(0.5, dim=0)
  5641. tensor([1., 2.])
  5642. >>> t.nanquantile(0.5, dim=1)
  5643. tensor([ nan, 1.5000])
  5644. """.format(**single_dim_common),
  5645. )
  5646. add_docstr(
  5647. torch.min,
  5648. r"""
  5649. min(input, *, out=None) -> Tensor
  5650. Returns the minimum value of all elements in the :attr:`input` tensor.
  5651. .. note::
  5652. The difference between ``max``/``min`` and ``amax``/``amin`` is:
  5653. - ``amax``/``amin`` supports reducing on multiple dimensions,
  5654. - ``amax``/``amin`` does not return indices.
  5655. Both ``amax``/``amin`` evenly distribute gradients between equal values
  5656. when there are multiple input elements with the same minimum or maximum value.
  5657. For ``max``/``min``:
  5658. - If reduce over all dimensions(no dim specified), gradients evenly distribute between equally ``max``/``min`` values.
  5659. - If reduce over one specified axis, only propagate to the indexed element.
  5660. Args:
  5661. {input}
  5662. Keyword args:
  5663. {out}
  5664. Example::
  5665. >>> a = torch.randn(1, 3)
  5666. >>> a
  5667. tensor([[ 0.6750, 1.0857, 1.7197]])
  5668. >>> torch.min(a)
  5669. tensor(0.6750)
  5670. .. function:: min(input, dim, keepdim=False, *, out=None) -> (Tensor, LongTensor)
  5671. :noindex:
  5672. Returns a namedtuple ``(values, indices)`` where ``values`` is the minimum
  5673. value of each row of the :attr:`input` tensor in the given dimension
  5674. :attr:`dim`. And ``indices`` is the index location of each minimum value found
  5675. (argmin).
  5676. If :attr:`keepdim` is ``True``, the output tensors are of the same size as
  5677. :attr:`input` except in the dimension :attr:`dim` where they are of size 1.
  5678. Otherwise, :attr:`dim` is squeezed (see :func:`torch.squeeze`), resulting in
  5679. the output tensors having 1 fewer dimension than :attr:`input`.
  5680. .. note:: If there are multiple minimal values in a reduced row then
  5681. the indices of the first minimal value are returned.
  5682. Args:
  5683. {input}
  5684. {opt_dim_without_none}
  5685. {opt_keepdim}
  5686. Keyword args:
  5687. out (tuple, optional): the tuple of two output tensors (min, min_indices)
  5688. Example::
  5689. >>> a = torch.randn(4, 4)
  5690. >>> a
  5691. tensor([[-0.6248, 1.1334, -1.1899, -0.2803],
  5692. [-1.4644, -0.2635, -0.3651, 0.6134],
  5693. [ 0.2457, 0.0384, 1.0128, 0.7015],
  5694. [-0.1153, 2.9849, 2.1458, 0.5788]])
  5695. >>> torch.min(a, 1)
  5696. torch.return_types.min(values=tensor([-1.1899, -1.4644, 0.0384, -0.1153]), indices=tensor([2, 0, 1, 0]))
  5697. .. function:: min(input, other, *, out=None) -> Tensor
  5698. :noindex:
  5699. See :func:`torch.minimum`.
  5700. """.format(**single_dim_common),
  5701. )
  5702. add_docstr(
  5703. torch.minimum,
  5704. r"""
  5705. minimum(input, other, *, out=None) -> Tensor
  5706. Computes the element-wise minimum of :attr:`input` and :attr:`other`.
  5707. .. note::
  5708. If one of the elements being compared is a NaN, then that element is returned.
  5709. :func:`minimum` is not supported for tensors with complex dtypes.
  5710. Args:
  5711. {input}
  5712. other (Tensor): the second input tensor
  5713. Keyword args:
  5714. {out}
  5715. Example::
  5716. >>> a = torch.tensor((1, 2, -1))
  5717. >>> b = torch.tensor((3, 0, 4))
  5718. >>> torch.minimum(a, b)
  5719. tensor([1, 0, -1])
  5720. """.format(**common_args),
  5721. )
  5722. add_docstr(
  5723. torch.fmin,
  5724. r"""
  5725. fmin(input, other, *, out=None) -> Tensor
  5726. Computes the element-wise minimum of :attr:`input` and :attr:`other`.
  5727. This is like :func:`torch.minimum` except it handles NaNs differently:
  5728. if exactly one of the two elements being compared is a NaN then the non-NaN element is taken as the minimum.
  5729. Only if both elements are NaN is NaN propagated.
  5730. This function is a wrapper around C++'s ``std::fmin`` and is similar to NumPy's ``fmin`` function.
  5731. Supports :ref:`broadcasting to a common shape <broadcasting-semantics>`,
  5732. :ref:`type promotion <type-promotion-doc>`, and integer and floating-point inputs.
  5733. Args:
  5734. {input}
  5735. other (Tensor): the second input tensor
  5736. Keyword args:
  5737. {out}
  5738. Example::
  5739. >>> a = torch.tensor([2.2, float('nan'), 2.1, float('nan')])
  5740. >>> b = torch.tensor([-9.3, 0.1, float('nan'), float('nan')])
  5741. >>> torch.fmin(a, b)
  5742. tensor([-9.3000, 0.1000, 2.1000, nan])
  5743. """.format(**common_args),
  5744. )
  5745. add_docstr(
  5746. torch.amin,
  5747. r"""
  5748. amin(input, dim, keepdim=False, *, out=None) -> Tensor
  5749. Returns the minimum value of each slice of the :attr:`input` tensor in the given
  5750. dimension(s) :attr:`dim`.
  5751. .. note::
  5752. The difference between ``max``/``min`` and ``amax``/``amin`` is:
  5753. - ``amax``/``amin`` supports reducing on multiple dimensions,
  5754. - ``amax``/``amin`` does not return indices.
  5755. Both ``amax``/``amin`` evenly distribute gradients between equal values
  5756. when there are multiple input elements with the same minimum or maximum value.
  5757. For ``max``/``min``:
  5758. - If reduce over all dimensions(no dim specified), gradients evenly distribute between equally ``max``/``min`` values.
  5759. - If reduce over one specified axis, only propagate to the indexed element.
  5760. {keepdim_details}
  5761. Args:
  5762. {input}
  5763. {opt_dim_all_reduce}
  5764. {opt_keepdim}
  5765. Keyword args:
  5766. {out}
  5767. Example::
  5768. >>> a = torch.randn(4, 4)
  5769. >>> a
  5770. tensor([[ 0.6451, -0.4866, 0.2987, -1.3312],
  5771. [-0.5744, 1.2980, 1.8397, -0.2713],
  5772. [ 0.9128, 0.9214, -1.7268, -0.2995],
  5773. [ 0.9023, 0.4853, 0.9075, -1.6165]])
  5774. >>> torch.amin(a, 1)
  5775. tensor([-1.3312, -0.5744, -1.7268, -1.6165])
  5776. """.format(**multi_dim_common),
  5777. )
  5778. add_docstr(
  5779. torch.aminmax,
  5780. r"""
  5781. aminmax(input, *, dim=None, keepdim=False, out=None) -> (Tensor min, Tensor max)
  5782. Computes the minimum and maximum values of the :attr:`input` tensor.
  5783. Args:
  5784. input (Tensor):
  5785. The input tensor
  5786. Keyword Args:
  5787. dim (Optional[int]):
  5788. The dimension along which to compute the values. If `None`,
  5789. computes the values over the entire :attr:`input` tensor.
  5790. Default is `None`.
  5791. keepdim (bool):
  5792. If `True`, the reduced dimensions will be kept in the output
  5793. tensor as dimensions with size 1 for broadcasting, otherwise
  5794. they will be removed, as if calling (:func:`torch.squeeze`).
  5795. Default is `False`.
  5796. out (Optional[Tuple[Tensor, Tensor]]):
  5797. Optional tensors on which to write the result. Must have the same
  5798. shape and dtype as the expected output.
  5799. Default is `None`.
  5800. Returns:
  5801. A named tuple `(min, max)` containing the minimum and maximum values.
  5802. Raises:
  5803. RuntimeError
  5804. If any of the dimensions to compute the values over has size 0.
  5805. .. note::
  5806. NaN values are propagated to the output if at least one value is NaN.
  5807. .. seealso::
  5808. :func:`torch.amin` computes just the minimum value
  5809. :func:`torch.amax` computes just the maximum value
  5810. Example::
  5811. >>> torch.aminmax(torch.tensor([1, -3, 5]))
  5812. torch.return_types.aminmax(
  5813. min=tensor(-3),
  5814. max=tensor(5))
  5815. >>> # aminmax propagates NaNs
  5816. >>> torch.aminmax(torch.tensor([1, -3, 5, torch.nan]))
  5817. torch.return_types.aminmax(
  5818. min=tensor(nan),
  5819. max=tensor(nan))
  5820. >>> t = torch.arange(10).view(2, 5)
  5821. >>> t
  5822. tensor([[0, 1, 2, 3, 4],
  5823. [5, 6, 7, 8, 9]])
  5824. >>> t.aminmax(dim=0, keepdim=True)
  5825. torch.return_types.aminmax(
  5826. min=tensor([[0, 1, 2, 3, 4]]),
  5827. max=tensor([[5, 6, 7, 8, 9]]))
  5828. """,
  5829. )
  5830. add_docstr(
  5831. torch.argmin,
  5832. r"""
  5833. argmin(input, dim=None, keepdim=False) -> LongTensor
  5834. Returns the indices of the minimum value(s) of the flattened tensor or along a dimension
  5835. This is the second value returned by :meth:`torch.min`. See its
  5836. documentation for the exact semantics of this method.
  5837. .. note:: If there are multiple minimal values then the indices of the first minimal value are returned.
  5838. Args:
  5839. {input}
  5840. {opt_dim} If ``None``, the argmin of the flattened input is returned.
  5841. {opt_keepdim}
  5842. Example::
  5843. >>> a = torch.randn(4, 4)
  5844. >>> a
  5845. tensor([[ 0.1139, 0.2254, -0.1381, 0.3687],
  5846. [ 1.0100, -1.1975, -0.0102, -0.4732],
  5847. [-0.9240, 0.1207, -0.7506, -1.0213],
  5848. [ 1.7809, -1.2960, 0.9384, 0.1438]])
  5849. >>> torch.argmin(a)
  5850. tensor(13)
  5851. >>> torch.argmin(a, dim=1)
  5852. tensor([ 2, 1, 3, 1])
  5853. >>> torch.argmin(a, dim=1, keepdim=True)
  5854. tensor([[2],
  5855. [1],
  5856. [3],
  5857. [1]])
  5858. """.format(**single_dim_common),
  5859. )
  5860. add_docstr(
  5861. torch.mm,
  5862. r"""
  5863. mm(input, mat2, out_dtype=None, *, out=None) -> Tensor
  5864. Performs a matrix multiplication of the matrices :attr:`input` and :attr:`mat2`.
  5865. If :attr:`input` is a :math:`(n \times m)` tensor, :attr:`mat2` is a
  5866. :math:`(m \times p)` tensor, :attr:`out` will be a :math:`(n \times p)` tensor.
  5867. .. note:: This function does not :ref:`broadcast <broadcasting-semantics>`.
  5868. For broadcasting matrix products, see :func:`torch.matmul`.
  5869. Supports strided and sparse 2-D tensors as inputs, autograd with
  5870. respect to strided inputs.
  5871. This operation has support for arguments with :ref:`sparse layouts<sparse-docs>`.
  5872. If :attr:`out` is provided its layout will be used. Otherwise, the result
  5873. layout will be deduced from that of :attr:`input`.
  5874. {sparse_beta_warning}
  5875. {tf32_note}
  5876. {rocm_fp16_note}
  5877. Args:
  5878. input (Tensor): the first matrix to be matrix multiplied
  5879. mat2 (Tensor): the second matrix to be matrix multiplied
  5880. out_dtype (dtype, optional): the dtype of the output tensor,
  5881. Supported only on CUDA and for torch.float32 given
  5882. torch.float16/torch.bfloat16 input dtypes
  5883. Keyword args:
  5884. {out}
  5885. Example::
  5886. >>> mat1 = torch.randn(2, 3)
  5887. >>> mat2 = torch.randn(3, 3)
  5888. >>> torch.mm(mat1, mat2)
  5889. tensor([[ 0.4851, 0.5037, -0.3633],
  5890. [-0.0760, -3.6705, 2.4784]])
  5891. """.format(**common_args, **tf32_notes, **rocm_fp16_notes, **sparse_support_notes),
  5892. )
  5893. add_docstr(
  5894. torch.hspmm,
  5895. r"""
  5896. hspmm(mat1, mat2, *, out=None) -> Tensor
  5897. Performs a matrix multiplication of a :ref:`sparse COO matrix
  5898. <sparse-coo-docs>` :attr:`mat1` and a strided matrix :attr:`mat2`. The
  5899. result is a (1 + 1)-dimensional :ref:`hybrid COO matrix
  5900. <sparse-hybrid-coo-docs>`.
  5901. Args:
  5902. mat1 (Tensor): the first sparse matrix to be matrix multiplied
  5903. mat2 (Tensor): the second strided matrix to be matrix multiplied
  5904. Keyword args:
  5905. {out}
  5906. """.format(**common_args),
  5907. )
  5908. add_docstr(
  5909. torch.matmul,
  5910. r"""
  5911. matmul(input, other, *, out=None) -> Tensor
  5912. Matrix product of two tensors.
  5913. The behavior depends on the dimensionality of the tensors as follows:
  5914. - If both tensors are 1-dimensional, the dot product (scalar) is returned.
  5915. - If both arguments are 2-dimensional, the matrix-matrix product is returned.
  5916. - If the first argument is 1-dimensional and the second argument is 2-dimensional,
  5917. a 1 is prepended to its dimension for the purpose of the matrix multiply.
  5918. After the matrix multiply, the prepended dimension is removed.
  5919. - If the first argument is 2-dimensional and the second argument is 1-dimensional,
  5920. the matrix-vector product is returned.
  5921. - If both arguments are at least 1-dimensional and at least one argument is
  5922. N-dimensional (where N > 2), then a batched matrix multiply is returned. If the first
  5923. argument is 1-dimensional, a 1 is prepended to its dimension for the purpose of the
  5924. batched matrix multiply and removed after. If the second argument is 1-dimensional, a
  5925. 1 is appended to its dimension for the purpose of the batched matrix multiply and removed after.
  5926. The first N-2 dimensions of each argument, the batch dimensions, are
  5927. :ref:`broadcast <broadcasting-semantics>` (and thus must be broadcastable).
  5928. The last 2, the matrix dimensions, are handled as in the matrix-matrix product.
  5929. For example, if :attr:`input` is a
  5930. :math:`(j \times 1 \times n \times m)` tensor and :attr:`other` is a :math:`(k \times m \times p)`
  5931. tensor, the batch dimensions are :math:`(j \times 1)` and :math:`(k)`,
  5932. and the matrix dimensions are :math:`(n \times m)` and :math:`(m \times p)`.
  5933. :attr:`out` will be a :math:`(j \times k \times n \times p)` tensor.
  5934. This operation has support for arguments with :ref:`sparse layouts<sparse-docs>`. In particular the
  5935. matrix-matrix (both arguments 2-dimensional) supports sparse arguments with the same restrictions
  5936. as :func:`torch.mm`
  5937. {sparse_beta_warning}
  5938. {tf32_note}
  5939. {rocm_fp16_note}
  5940. .. note::
  5941. The 1-dimensional dot product version of this function does not support an :attr:`out` parameter.
  5942. Arguments:
  5943. input (Tensor): the first tensor to be multiplied
  5944. other (Tensor): the second tensor to be multiplied
  5945. Keyword args:
  5946. {out}
  5947. Example::
  5948. >>> # vector x vector
  5949. >>> tensor1 = torch.randn(3)
  5950. >>> tensor2 = torch.randn(3)
  5951. >>> torch.matmul(tensor1, tensor2).size()
  5952. torch.Size([])
  5953. >>> # matrix x vector
  5954. >>> tensor1 = torch.randn(3, 4)
  5955. >>> tensor2 = torch.randn(4)
  5956. >>> torch.matmul(tensor1, tensor2).size()
  5957. torch.Size([3])
  5958. >>> # batched matrix x broadcasted vector
  5959. >>> tensor1 = torch.randn(10, 3, 4)
  5960. >>> tensor2 = torch.randn(4)
  5961. >>> torch.matmul(tensor1, tensor2).size()
  5962. torch.Size([10, 3])
  5963. >>> # batched matrix x batched matrix
  5964. >>> tensor1 = torch.randn(10, 3, 4)
  5965. >>> tensor2 = torch.randn(10, 4, 5)
  5966. >>> torch.matmul(tensor1, tensor2).size()
  5967. torch.Size([10, 3, 5])
  5968. >>> # batched matrix x broadcasted matrix
  5969. >>> tensor1 = torch.randn(10, 3, 4)
  5970. >>> tensor2 = torch.randn(4, 5)
  5971. >>> torch.matmul(tensor1, tensor2).size()
  5972. torch.Size([10, 3, 5])
  5973. """.format(**common_args, **tf32_notes, **rocm_fp16_notes, **sparse_support_notes),
  5974. )
  5975. add_docstr(
  5976. torch.mode,
  5977. r"""
  5978. mode(input, dim=-1, keepdim=False, *, out=None) -> (Tensor, LongTensor)
  5979. Returns a namedtuple ``(values, indices)`` where ``values`` is the mode
  5980. value of each row of the :attr:`input` tensor in the given dimension
  5981. :attr:`dim`, i.e. a value which appears most often
  5982. in that row, and ``indices`` is the index location of each mode value found.
  5983. By default, :attr:`dim` is the last dimension of the :attr:`input` tensor.
  5984. If :attr:`keepdim` is ``True``, the output tensors are of the same size as
  5985. :attr:`input` except in the dimension :attr:`dim` where they are of size 1.
  5986. Otherwise, :attr:`dim` is squeezed (see :func:`torch.squeeze`), resulting
  5987. in the output tensors having 1 fewer dimension than :attr:`input`.
  5988. .. note:: This function is not defined for ``torch.cuda.Tensor`` yet.
  5989. Args:
  5990. {input}
  5991. {opt_dim}
  5992. {opt_keepdim}
  5993. Keyword args:
  5994. out (tuple, optional): the result tuple of two output tensors (values, indices)
  5995. Example::
  5996. >>> b = torch.tensor([[0, 0, 0, 2, 0, 0, 2],
  5997. ... [0, 3, 0, 0, 2, 0, 1],
  5998. ... [2, 2, 2, 0, 0, 0, 3],
  5999. ... [2, 2, 3, 0, 1, 1, 0],
  6000. ... [1, 1, 0, 0, 2, 0, 2]])
  6001. >>> torch.mode(b, 0)
  6002. torch.return_types.mode(
  6003. values=tensor([0, 2, 0, 0, 0, 0, 2]),
  6004. indices=tensor([1, 3, 4, 4, 2, 4, 4]))
  6005. """.format(**single_dim_common),
  6006. )
  6007. add_docstr(
  6008. torch.mul,
  6009. r"""
  6010. mul(input, other, *, out=None) -> Tensor
  6011. Multiplies :attr:`input` by :attr:`other`.
  6012. .. math::
  6013. \text{out}_i = \text{input}_i \times \text{other}_i
  6014. """
  6015. + r"""
  6016. Supports :ref:`broadcasting to a common shape <broadcasting-semantics>`,
  6017. :ref:`type promotion <type-promotion-doc>`, and integer, float, and complex inputs.
  6018. Args:
  6019. {input}
  6020. other (Tensor or Number): the tensor or number to multiply input by.
  6021. Keyword args:
  6022. {out}
  6023. Examples::
  6024. >>> a = torch.randn(3)
  6025. >>> a
  6026. tensor([ 0.2015, -0.4255, 2.6087])
  6027. >>> torch.mul(a, 100)
  6028. tensor([ 20.1494, -42.5491, 260.8663])
  6029. >>> b = torch.randn(4, 1)
  6030. >>> b
  6031. tensor([[ 1.1207],
  6032. [-0.3137],
  6033. [ 0.0700],
  6034. [ 0.8378]])
  6035. >>> c = torch.randn(1, 4)
  6036. >>> c
  6037. tensor([[ 0.5146, 0.1216, -0.5244, 2.2382]])
  6038. >>> torch.mul(b, c)
  6039. tensor([[ 0.5767, 0.1363, -0.5877, 2.5083],
  6040. [-0.1614, -0.0382, 0.1645, -0.7021],
  6041. [ 0.0360, 0.0085, -0.0367, 0.1567],
  6042. [ 0.4312, 0.1019, -0.4394, 1.8753]])
  6043. """.format(**common_args),
  6044. )
  6045. add_docstr(
  6046. torch.multiply,
  6047. r"""
  6048. multiply(input, other, *, out=None)
  6049. Alias for :func:`torch.mul`.
  6050. """,
  6051. )
  6052. add_docstr(
  6053. torch.multinomial,
  6054. r"""
  6055. multinomial(input, num_samples, replacement=False, *, generator=None, out=None) -> LongTensor
  6056. Returns a tensor where each row contains :attr:`num_samples` indices sampled
  6057. from the multinomial (a stricter definition would be multivariate,
  6058. refer to :class:`torch.distributions.multinomial.Multinomial` for more details)
  6059. probability distribution located in the corresponding row
  6060. of tensor :attr:`input`.
  6061. .. note::
  6062. The rows of :attr:`input` do not need to sum to one (in which case we use
  6063. the values as weights), but must be non-negative, finite and have
  6064. a non-zero sum.
  6065. Indices are ordered from left to right according to when each was sampled
  6066. (first samples are placed in first column).
  6067. If :attr:`input` is a vector, :attr:`out` is a vector of size :attr:`num_samples`.
  6068. If :attr:`input` is a matrix with `m` rows, :attr:`out` is an matrix of shape
  6069. :math:`(m \times \text{{num\_samples}})`.
  6070. If replacement is ``True``, samples are drawn with replacement.
  6071. If not, they are drawn without replacement, which means that when a
  6072. sample index is drawn for a row, it cannot be drawn again for that row.
  6073. .. note::
  6074. When drawn without replacement, :attr:`num_samples` must be lower than
  6075. number of non-zero elements in :attr:`input` (or the min number of non-zero
  6076. elements in each row of :attr:`input` if it is a matrix).
  6077. Args:
  6078. input (Tensor): the input tensor containing probabilities
  6079. num_samples (int): number of samples to draw
  6080. replacement (bool, optional): whether to draw with replacement or not
  6081. Keyword args:
  6082. {generator}
  6083. {out}
  6084. Example::
  6085. >>> weights = torch.tensor([0, 10, 3, 0], dtype=torch.float) # create a tensor of weights
  6086. >>> torch.multinomial(weights, 2)
  6087. tensor([1, 2])
  6088. >>> torch.multinomial(weights, 5) # ERROR!
  6089. RuntimeError: cannot sample n_sample > prob_dist.size(-1) samples without replacement
  6090. >>> torch.multinomial(weights, 4, replacement=True)
  6091. tensor([ 2, 1, 1, 1])
  6092. """.format(**common_args),
  6093. )
  6094. add_docstr(
  6095. torch.mv,
  6096. r"""
  6097. mv(input, vec, *, out=None) -> Tensor
  6098. Performs a matrix-vector product of the matrix :attr:`input` and the vector
  6099. :attr:`vec`.
  6100. If :attr:`input` is a :math:`(n \times m)` tensor, :attr:`vec` is a 1-D tensor of
  6101. size :math:`m`, :attr:`out` will be 1-D of size :math:`n`.
  6102. .. note:: This function does not :ref:`broadcast <broadcasting-semantics>`.
  6103. Args:
  6104. input (Tensor): matrix to be multiplied
  6105. vec (Tensor): vector to be multiplied
  6106. Keyword args:
  6107. {out}
  6108. Example::
  6109. >>> mat = torch.randn(2, 3)
  6110. >>> vec = torch.randn(3)
  6111. >>> torch.mv(mat, vec)
  6112. tensor([ 1.0404, -0.6361])
  6113. """.format(**common_args),
  6114. )
  6115. add_docstr(
  6116. torch.mvlgamma,
  6117. r"""
  6118. mvlgamma(input, p, *, out=None) -> Tensor
  6119. Alias for :func:`torch.special.multigammaln`.
  6120. """,
  6121. )
  6122. add_docstr(
  6123. torch.movedim,
  6124. r"""
  6125. movedim(input, source, destination) -> Tensor
  6126. Moves the dimension(s) of :attr:`input` at the position(s) in :attr:`source`
  6127. to the position(s) in :attr:`destination`.
  6128. Other dimensions of :attr:`input` that are not explicitly moved remain in
  6129. their original order and appear at the positions not specified in :attr:`destination`.
  6130. Args:
  6131. {input}
  6132. source (int or tuple of ints): Original positions of the dims to move. These must be unique.
  6133. destination (int or tuple of ints): Destination positions for each of the original dims. These must also be unique.
  6134. Examples::
  6135. >>> t = torch.randn(3,2,1)
  6136. >>> t
  6137. tensor([[[-0.3362],
  6138. [-0.8437]],
  6139. [[-0.9627],
  6140. [ 0.1727]],
  6141. [[ 0.5173],
  6142. [-0.1398]]])
  6143. >>> torch.movedim(t, 1, 0).shape
  6144. torch.Size([2, 3, 1])
  6145. >>> torch.movedim(t, 1, 0)
  6146. tensor([[[-0.3362],
  6147. [-0.9627],
  6148. [ 0.5173]],
  6149. [[-0.8437],
  6150. [ 0.1727],
  6151. [-0.1398]]])
  6152. >>> torch.movedim(t, (1, 2), (0, 1)).shape
  6153. torch.Size([2, 1, 3])
  6154. >>> torch.movedim(t, (1, 2), (0, 1))
  6155. tensor([[[-0.3362, -0.9627, 0.5173]],
  6156. [[-0.8437, 0.1727, -0.1398]]])
  6157. """.format(**common_args),
  6158. )
  6159. add_docstr(
  6160. torch.moveaxis,
  6161. r"""
  6162. moveaxis(input, source, destination) -> Tensor
  6163. Alias for :func:`torch.movedim`.
  6164. This function is equivalent to NumPy's moveaxis function.
  6165. Examples::
  6166. >>> t = torch.randn(3,2,1)
  6167. >>> t
  6168. tensor([[[-0.3362],
  6169. [-0.8437]],
  6170. [[-0.9627],
  6171. [ 0.1727]],
  6172. [[ 0.5173],
  6173. [-0.1398]]])
  6174. >>> torch.moveaxis(t, 1, 0).shape
  6175. torch.Size([2, 3, 1])
  6176. >>> torch.moveaxis(t, 1, 0)
  6177. tensor([[[-0.3362],
  6178. [-0.9627],
  6179. [ 0.5173]],
  6180. [[-0.8437],
  6181. [ 0.1727],
  6182. [-0.1398]]])
  6183. >>> torch.moveaxis(t, (1, 2), (0, 1)).shape
  6184. torch.Size([2, 1, 3])
  6185. >>> torch.moveaxis(t, (1, 2), (0, 1))
  6186. tensor([[[-0.3362, -0.9627, 0.5173]],
  6187. [[-0.8437, 0.1727, -0.1398]]])
  6188. """.format(**common_args),
  6189. )
  6190. add_docstr(
  6191. torch.swapdims,
  6192. r"""
  6193. swapdims(input, dim0, dim1) -> Tensor
  6194. Alias for :func:`torch.transpose`.
  6195. This function is equivalent to NumPy's swapaxes function.
  6196. Examples::
  6197. >>> x = torch.tensor([[[0,1],[2,3]],[[4,5],[6,7]]])
  6198. >>> x
  6199. tensor([[[0, 1],
  6200. [2, 3]],
  6201. [[4, 5],
  6202. [6, 7]]])
  6203. >>> torch.swapdims(x, 0, 1)
  6204. tensor([[[0, 1],
  6205. [4, 5]],
  6206. [[2, 3],
  6207. [6, 7]]])
  6208. >>> torch.swapdims(x, 0, 2)
  6209. tensor([[[0, 4],
  6210. [2, 6]],
  6211. [[1, 5],
  6212. [3, 7]]])
  6213. """.format(**common_args),
  6214. )
  6215. add_docstr(
  6216. torch.swapaxes,
  6217. r"""
  6218. swapaxes(input, axis0, axis1) -> Tensor
  6219. Alias for :func:`torch.transpose`.
  6220. This function is equivalent to NumPy's swapaxes function.
  6221. Examples::
  6222. >>> x = torch.tensor([[[0,1],[2,3]],[[4,5],[6,7]]])
  6223. >>> x
  6224. tensor([[[0, 1],
  6225. [2, 3]],
  6226. [[4, 5],
  6227. [6, 7]]])
  6228. >>> torch.swapaxes(x, 0, 1)
  6229. tensor([[[0, 1],
  6230. [4, 5]],
  6231. [[2, 3],
  6232. [6, 7]]])
  6233. >>> torch.swapaxes(x, 0, 2)
  6234. tensor([[[0, 4],
  6235. [2, 6]],
  6236. [[1, 5],
  6237. [3, 7]]])
  6238. """.format(**common_args),
  6239. )
  6240. add_docstr(
  6241. torch.narrow,
  6242. r"""
  6243. narrow(input, dim, start, length) -> Tensor
  6244. Returns a new tensor that is a narrowed version of :attr:`input` tensor. The
  6245. dimension :attr:`dim` is input from :attr:`start` to ``start + length``. The
  6246. returned tensor and :attr:`input` tensor share the same underlying storage.
  6247. Args:
  6248. input (Tensor): the tensor to narrow
  6249. dim (int): the dimension along which to narrow
  6250. start (int or Tensor): index of the element to start the narrowed dimension
  6251. from. Can be negative, which means indexing from the end of `dim`. If
  6252. `Tensor`, it must be an 0-dim integral `Tensor` (bools not allowed)
  6253. length (int): length of the narrowed dimension, must be weakly positive
  6254. Example::
  6255. >>> x = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
  6256. >>> torch.narrow(x, 0, 0, 2)
  6257. tensor([[ 1, 2, 3],
  6258. [ 4, 5, 6]])
  6259. >>> torch.narrow(x, 1, 1, 2)
  6260. tensor([[ 2, 3],
  6261. [ 5, 6],
  6262. [ 8, 9]])
  6263. >>> torch.narrow(x, -1, torch.tensor(-1), 1)
  6264. tensor([[3],
  6265. [6],
  6266. [9]])
  6267. """,
  6268. )
  6269. add_docstr(
  6270. torch.narrow_copy,
  6271. r"""
  6272. narrow_copy(input, dim, start, length, *, out=None) -> Tensor
  6273. Same as :meth:`Tensor.narrow` except this returns a copy rather
  6274. than shared storage. This is primarily for sparse tensors, which
  6275. do not have a shared-storage narrow method.
  6276. Args:
  6277. input (Tensor): the tensor to narrow
  6278. dim (int): the dimension along which to narrow
  6279. start (int): index of the element to start the narrowed dimension from. Can
  6280. be negative, which means indexing from the end of `dim`
  6281. length (int): length of the narrowed dimension, must be weakly positive
  6282. Keyword args:
  6283. {out}
  6284. Example::
  6285. >>> x = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
  6286. >>> torch.narrow_copy(x, 0, 0, 2)
  6287. tensor([[ 1, 2, 3],
  6288. [ 4, 5, 6]])
  6289. >>> torch.narrow_copy(x, 1, 1, 2)
  6290. tensor([[ 2, 3],
  6291. [ 5, 6],
  6292. [ 8, 9]])
  6293. >>> s = torch.arange(16).reshape(2, 2, 2, 2).to_sparse(2)
  6294. >>> torch.narrow_copy(s, 0, 0, 1)
  6295. tensor(indices=tensor([[0, 0],
  6296. [0, 1]]),
  6297. values=tensor([[[0, 1],
  6298. [2, 3]],
  6299. [[4, 5],
  6300. [6, 7]]]),
  6301. size=(1, 2, 2, 2), nnz=2, layout=torch.sparse_coo)
  6302. .. seealso::
  6303. :func:`torch.narrow` for a non copy variant
  6304. """.format(**common_args),
  6305. )
  6306. add_docstr(
  6307. torch.nan_to_num,
  6308. r"""
  6309. nan_to_num(input, nan=0.0, posinf=None, neginf=None, *, out=None) -> Tensor
  6310. Replaces :literal:`NaN`, positive infinity, and negative infinity values in :attr:`input`
  6311. with the values specified by :attr:`nan`, :attr:`posinf`, and :attr:`neginf`, respectively.
  6312. By default, :literal:`NaN`\ s are replaced with zero, positive infinity is replaced with the
  6313. greatest finite value representable by :attr:`input`'s dtype, and negative infinity
  6314. is replaced with the least finite value representable by :attr:`input`'s dtype.
  6315. Args:
  6316. {input}
  6317. nan (Number, optional): the value to replace :literal:`NaN`\s with. Default is zero.
  6318. posinf (Number, optional): if a Number, the value to replace positive infinity values with.
  6319. If None, positive infinity values are replaced with the greatest finite value representable by :attr:`input`'s dtype.
  6320. Default is None.
  6321. neginf (Number, optional): if a Number, the value to replace negative infinity values with.
  6322. If None, negative infinity values are replaced with the lowest finite value representable by :attr:`input`'s dtype.
  6323. Default is None.
  6324. Keyword args:
  6325. {out}
  6326. Example::
  6327. >>> x = torch.tensor([float('nan'), float('inf'), -float('inf'), 3.14])
  6328. >>> torch.nan_to_num(x)
  6329. tensor([ 0.0000e+00, 3.4028e+38, -3.4028e+38, 3.1400e+00])
  6330. >>> torch.nan_to_num(x, nan=2.0)
  6331. tensor([ 2.0000e+00, 3.4028e+38, -3.4028e+38, 3.1400e+00])
  6332. >>> torch.nan_to_num(x, nan=2.0, posinf=1.0)
  6333. tensor([ 2.0000e+00, 1.0000e+00, -3.4028e+38, 3.1400e+00])
  6334. """.format(**common_args),
  6335. )
  6336. add_docstr(
  6337. torch.ne,
  6338. r"""
  6339. ne(input, other, *, out=None) -> Tensor
  6340. Computes :math:`\text{input} \neq \text{other}` element-wise.
  6341. """
  6342. + r"""
  6343. The second argument can be a number or a tensor whose shape is
  6344. :ref:`broadcastable <broadcasting-semantics>` with the first argument.
  6345. Args:
  6346. input (Tensor): the tensor to compare
  6347. other (Tensor or float): the tensor or value to compare
  6348. Keyword args:
  6349. {out}
  6350. Returns:
  6351. A boolean tensor that is True where :attr:`input` is not equal to :attr:`other` and False elsewhere
  6352. Example::
  6353. >>> torch.ne(torch.tensor([[1, 2], [3, 4]]), torch.tensor([[1, 1], [4, 4]]))
  6354. tensor([[False, True], [True, False]])
  6355. """.format(**common_args),
  6356. )
  6357. add_docstr(
  6358. torch.not_equal,
  6359. r"""
  6360. not_equal(input, other, *, out=None) -> Tensor
  6361. Alias for :func:`torch.ne`.
  6362. """,
  6363. )
  6364. add_docstr(
  6365. torch.neg,
  6366. r"""
  6367. neg(input, *, out=None) -> Tensor
  6368. Returns a new tensor with the negative of the elements of :attr:`input`.
  6369. .. math::
  6370. \text{out} = -1 \times \text{input}
  6371. """
  6372. + r"""
  6373. Args:
  6374. {input}
  6375. Keyword args:
  6376. {out}
  6377. Example::
  6378. >>> a = torch.randn(5)
  6379. >>> a
  6380. tensor([ 0.0090, -0.2262, -0.0682, -0.2866, 0.3940])
  6381. >>> torch.neg(a)
  6382. tensor([-0.0090, 0.2262, 0.0682, 0.2866, -0.3940])
  6383. """.format(**common_args),
  6384. )
  6385. add_docstr(
  6386. torch.negative,
  6387. r"""
  6388. negative(input, *, out=None) -> Tensor
  6389. Alias for :func:`torch.neg`
  6390. """,
  6391. )
  6392. add_docstr(
  6393. torch.nextafter,
  6394. r"""
  6395. nextafter(input, other, *, out=None) -> Tensor
  6396. Return the next floating-point value after :attr:`input` towards :attr:`other`, elementwise.
  6397. The shapes of ``input`` and ``other`` must be
  6398. :ref:`broadcastable <broadcasting-semantics>`.
  6399. Args:
  6400. input (Tensor): the first input tensor
  6401. other (Tensor): the second input tensor
  6402. Keyword args:
  6403. {out}
  6404. Example::
  6405. >>> eps = torch.finfo(torch.float32).eps
  6406. >>> torch.nextafter(torch.tensor([1.0, 2.0]), torch.tensor([2.0, 1.0])) == torch.tensor([eps + 1, 2 - eps])
  6407. tensor([True, True])
  6408. """.format(**common_args),
  6409. )
  6410. add_docstr(
  6411. torch.nonzero,
  6412. r"""
  6413. nonzero(input, *, out=None, as_tuple=False) -> LongTensor or tuple of LongTensors
  6414. .. note::
  6415. :func:`torch.nonzero(..., as_tuple=False) <torch.nonzero>` (default) returns a
  6416. 2-D tensor where each row is the index for a nonzero value.
  6417. :func:`torch.nonzero(..., as_tuple=True) <torch.nonzero>` returns a tuple of 1-D
  6418. index tensors, allowing for advanced indexing, so ``x[x.nonzero(as_tuple=True)]``
  6419. gives all nonzero values of tensor ``x``. Of the returned tuple, each index tensor
  6420. contains nonzero indices for a certain dimension.
  6421. See below for more details on the two behaviors.
  6422. When :attr:`input` is on CUDA, :func:`torch.nonzero() <torch.nonzero>` causes
  6423. host-device synchronization.
  6424. **When** :attr:`as_tuple` **is** ``False`` **(default)**:
  6425. Returns a tensor containing the indices of all non-zero elements of
  6426. :attr:`input`. Each row in the result contains the indices of a non-zero
  6427. element in :attr:`input`. The result is sorted lexicographically, with
  6428. the last index changing the fastest (C-style).
  6429. If :attr:`input` has :math:`n` dimensions, then the resulting indices tensor
  6430. :attr:`out` is of size :math:`(z \times n)`, where :math:`z` is the total number of
  6431. non-zero elements in the :attr:`input` tensor.
  6432. **When** :attr:`as_tuple` **is** ``True``:
  6433. Returns a tuple of 1-D tensors, one for each dimension in :attr:`input`,
  6434. each containing the indices (in that dimension) of all non-zero elements of
  6435. :attr:`input` .
  6436. If :attr:`input` has :math:`n` dimensions, then the resulting tuple contains :math:`n`
  6437. tensors of size :math:`z`, where :math:`z` is the total number of
  6438. non-zero elements in the :attr:`input` tensor.
  6439. As a special case, when :attr:`input` has zero dimensions and a nonzero scalar
  6440. value, it is treated as a one-dimensional tensor with one element.
  6441. Args:
  6442. {input}
  6443. Keyword args:
  6444. out (LongTensor, optional): the output tensor containing indices
  6445. Returns:
  6446. LongTensor or tuple of LongTensor: If :attr:`as_tuple` is ``False``, the output
  6447. tensor containing indices. If :attr:`as_tuple` is ``True``, one 1-D tensor for
  6448. each dimension, containing the indices of each nonzero element along that
  6449. dimension.
  6450. Example::
  6451. >>> torch.nonzero(torch.tensor([1, 1, 1, 0, 1]))
  6452. tensor([[ 0],
  6453. [ 1],
  6454. [ 2],
  6455. [ 4]])
  6456. >>> torch.nonzero(torch.tensor([[0.6, 0.0, 0.0, 0.0],
  6457. ... [0.0, 0.4, 0.0, 0.0],
  6458. ... [0.0, 0.0, 1.2, 0.0],
  6459. ... [0.0, 0.0, 0.0,-0.4]]))
  6460. tensor([[ 0, 0],
  6461. [ 1, 1],
  6462. [ 2, 2],
  6463. [ 3, 3]])
  6464. >>> torch.nonzero(torch.tensor([1, 1, 1, 0, 1]), as_tuple=True)
  6465. (tensor([0, 1, 2, 4]),)
  6466. >>> torch.nonzero(torch.tensor([[0.6, 0.0, 0.0, 0.0],
  6467. ... [0.0, 0.4, 0.0, 0.0],
  6468. ... [0.0, 0.0, 1.2, 0.0],
  6469. ... [0.0, 0.0, 0.0,-0.4]]), as_tuple=True)
  6470. (tensor([0, 1, 2, 3]), tensor([0, 1, 2, 3]))
  6471. >>> torch.nonzero(torch.tensor(5), as_tuple=True)
  6472. (tensor([0]),)
  6473. """.format(**common_args),
  6474. )
  6475. add_docstr(
  6476. torch.normal,
  6477. r"""
  6478. normal(mean, std, *, generator=None, out=None) -> Tensor
  6479. Returns a tensor of random numbers drawn from separate normal distributions
  6480. whose mean and standard deviation are given.
  6481. The :attr:`mean` is a tensor with the mean of
  6482. each output element's normal distribution
  6483. The :attr:`std` is a tensor with the standard deviation of
  6484. each output element's normal distribution
  6485. The shapes of :attr:`mean` and :attr:`std` don't need to match, but the
  6486. total number of elements in each tensor need to be the same.
  6487. .. note:: When the shapes do not match, the shape of :attr:`mean`
  6488. is used as the shape for the returned output tensor
  6489. .. note:: When :attr:`std` is a CUDA tensor, this function synchronizes
  6490. its device with the CPU.
  6491. Args:
  6492. mean (Tensor): the tensor of per-element means
  6493. std (Tensor): the tensor of per-element standard deviations
  6494. Keyword args:
  6495. {generator}
  6496. {out}
  6497. Example::
  6498. >>> torch.normal(mean=torch.arange(1., 11.), std=torch.arange(1, 0, -0.1))
  6499. tensor([ 1.0425, 3.5672, 2.7969, 4.2925, 4.7229, 6.2134,
  6500. 8.0505, 8.1408, 9.0563, 10.0566])
  6501. .. function:: normal(mean=0.0, std, *, out=None) -> Tensor
  6502. :noindex:
  6503. Similar to the function above, but the means are shared among all drawn
  6504. elements.
  6505. Args:
  6506. mean (float, optional): the mean for all distributions
  6507. std (Tensor): the tensor of per-element standard deviations
  6508. Keyword args:
  6509. {out}
  6510. Example::
  6511. >>> torch.normal(mean=0.5, std=torch.arange(1., 6.))
  6512. tensor([-1.2793, -1.0732, -2.0687, 5.1177, -1.2303])
  6513. .. function:: normal(mean, std=1.0, *, out=None) -> Tensor
  6514. :noindex:
  6515. Similar to the function above, but the standard deviations are shared among
  6516. all drawn elements.
  6517. Args:
  6518. mean (Tensor): the tensor of per-element means
  6519. std (float, optional): the standard deviation for all distributions
  6520. Keyword args:
  6521. out (Tensor, optional): the output tensor
  6522. Example::
  6523. >>> torch.normal(mean=torch.arange(1., 6.))
  6524. tensor([ 1.1552, 2.6148, 2.6535, 5.8318, 4.2361])
  6525. .. function:: normal(mean, std, size, *, out=None) -> Tensor
  6526. :noindex:
  6527. Similar to the function above, but the means and standard deviations are shared
  6528. among all drawn elements. The resulting tensor has size given by :attr:`size`.
  6529. Args:
  6530. mean (float): the mean for all distributions
  6531. std (float): the standard deviation for all distributions
  6532. size (int...): a sequence of integers defining the shape of the output tensor.
  6533. Keyword args:
  6534. {out}
  6535. Example::
  6536. >>> torch.normal(2, 3, size=(1, 4))
  6537. tensor([[-1.3987, -1.9544, 3.6048, 0.7909]])
  6538. """.format(**common_args),
  6539. )
  6540. add_docstr(
  6541. torch.numel,
  6542. r"""
  6543. numel(input: Tensor) -> int
  6544. Returns the total number of elements in the :attr:`input` tensor.
  6545. Args:
  6546. {input}
  6547. Example::
  6548. >>> a = torch.randn(1, 2, 3, 4, 5)
  6549. >>> torch.numel(a)
  6550. 120
  6551. >>> a = torch.zeros(4,4)
  6552. >>> torch.numel(a)
  6553. 16
  6554. """.format(**common_args),
  6555. )
  6556. add_docstr(
  6557. torch.ones,
  6558. r"""
  6559. ones(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) -> Tensor
  6560. Returns a tensor filled with the scalar value `1`, with the shape defined
  6561. by the variable argument :attr:`size`.
  6562. Args:
  6563. size (int...): a sequence of integers defining the shape of the output tensor.
  6564. Can be a variable number of arguments or a collection like a list or tuple.
  6565. Keyword arguments:
  6566. {out}
  6567. {dtype}
  6568. {layout}
  6569. {device}
  6570. {requires_grad}
  6571. Example::
  6572. >>> torch.ones(2, 3)
  6573. tensor([[ 1., 1., 1.],
  6574. [ 1., 1., 1.]])
  6575. >>> torch.ones(5)
  6576. tensor([ 1., 1., 1., 1., 1.])
  6577. """.format(**factory_common_args),
  6578. )
  6579. add_docstr(
  6580. torch.ones_like,
  6581. r"""
  6582. ones_like(input, *, dtype=None, layout=None, device=None, requires_grad=False, memory_format=torch.preserve_format) -> Tensor
  6583. Returns a tensor filled with the scalar value `1`, with the same size as
  6584. :attr:`input`. ``torch.ones_like(input)`` is equivalent to
  6585. ``torch.ones(input.size(), dtype=input.dtype, layout=input.layout, device=input.device)``.
  6586. .. warning::
  6587. As of 0.4, this function does not support an :attr:`out` keyword. As an alternative,
  6588. the old ``torch.ones_like(input, out=output)`` is equivalent to
  6589. ``torch.ones(input.size(), out=output)``.
  6590. Args:
  6591. {input}
  6592. Keyword arguments:
  6593. {dtype}
  6594. {layout}
  6595. {device}
  6596. {requires_grad}
  6597. {memory_format}
  6598. Example::
  6599. >>> input = torch.empty(2, 3)
  6600. >>> torch.ones_like(input)
  6601. tensor([[ 1., 1., 1.],
  6602. [ 1., 1., 1.]])
  6603. """.format(**factory_like_common_args),
  6604. )
  6605. add_docstr(
  6606. torch.orgqr,
  6607. r"""
  6608. orgqr(input, tau) -> Tensor
  6609. Alias for :func:`torch.linalg.householder_product`.
  6610. """,
  6611. )
  6612. add_docstr(
  6613. torch.ormqr,
  6614. r"""
  6615. ormqr(input, tau, other, left=True, transpose=False, *, out=None) -> Tensor
  6616. Computes the matrix-matrix multiplication of a product of Householder matrices with a general matrix.
  6617. Multiplies a :math:`m \times n` matrix `C` (given by :attr:`other`) with a matrix `Q`,
  6618. where `Q` is represented using Householder reflectors `(input, tau)`.
  6619. See `Representation of Orthogonal or Unitary Matrices`_ for further details.
  6620. If :attr:`left` is `True` then `op(Q)` times `C` is computed, otherwise the result is `C` times `op(Q)`.
  6621. When :attr:`left` is `True`, the implicit matrix `Q` has size :math:`m \times m`.
  6622. It has size :math:`n \times n` otherwise.
  6623. If :attr:`transpose` is `True` then `op` is the conjugate transpose operation, otherwise it's a no-op.
  6624. Supports inputs of float, double, cfloat and cdouble dtypes.
  6625. Also supports batched inputs, and, if the input is batched, the output is batched with the same dimensions.
  6626. .. seealso::
  6627. :func:`torch.geqrf` can be used to form the Householder representation `(input, tau)` of matrix `Q`
  6628. from the QR decomposition.
  6629. .. note::
  6630. This function supports backward but it is only fast when ``(input, tau)`` do not require gradients
  6631. and/or ``tau.size(-1)`` is very small.
  6632. ``
  6633. Args:
  6634. input (Tensor): tensor of shape `(*, mn, k)` where `*` is zero or more batch dimensions
  6635. and `mn` equals to `m` or `n` depending on the :attr:`left`.
  6636. tau (Tensor): tensor of shape `(*, min(mn, k))` where `*` is zero or more batch dimensions.
  6637. other (Tensor): tensor of shape `(*, m, n)` where `*` is zero or more batch dimensions.
  6638. left (bool): controls the order of multiplication.
  6639. transpose (bool): controls whether the matrix `Q` is conjugate transposed or not.
  6640. Keyword args:
  6641. out (Tensor, optional): the output Tensor. Ignored if `None`. Default: `None`.
  6642. .. _Representation of Orthogonal or Unitary Matrices:
  6643. https://www.netlib.org/lapack/lug/node128.html
  6644. """,
  6645. )
  6646. add_docstr(
  6647. torch.permute,
  6648. r"""
  6649. permute(input, dims) -> Tensor
  6650. Returns a view of the original tensor :attr:`input` with its dimensions permuted.
  6651. Args:
  6652. {input}
  6653. dims (tuple of int): The desired ordering of dimensions
  6654. Example:
  6655. >>> x = torch.randn(2, 3, 5)
  6656. >>> x.size()
  6657. torch.Size([2, 3, 5])
  6658. >>> torch.permute(x, (2, 0, 1)).size()
  6659. torch.Size([5, 2, 3])
  6660. """.format(**common_args),
  6661. )
  6662. add_docstr(
  6663. torch.poisson,
  6664. r"""
  6665. poisson(input, generator=None) -> Tensor
  6666. Returns a tensor of the same size as :attr:`input` with each element
  6667. sampled from a Poisson distribution with rate parameter given by the corresponding
  6668. element in :attr:`input` i.e.,
  6669. .. math::
  6670. \text{{out}}_i \sim \text{{Poisson}}(\text{{input}}_i)
  6671. :attr:`input` must be non-negative.
  6672. Args:
  6673. input (Tensor): the input tensor containing the rates of the Poisson distribution
  6674. Keyword args:
  6675. {generator}
  6676. Example::
  6677. >>> rates = torch.rand(4, 4) * 5 # rate parameter between 0 and 5
  6678. >>> torch.poisson(rates)
  6679. tensor([[9., 1., 3., 5.],
  6680. [8., 6., 6., 0.],
  6681. [0., 4., 5., 3.],
  6682. [2., 1., 4., 2.]])
  6683. """.format(**common_args),
  6684. )
  6685. add_docstr(
  6686. torch.polygamma,
  6687. r"""
  6688. polygamma(n, input, *, out=None) -> Tensor
  6689. Alias for :func:`torch.special.polygamma`.
  6690. """,
  6691. )
  6692. add_docstr(
  6693. torch.positive,
  6694. r"""
  6695. positive(input) -> Tensor
  6696. Returns :attr:`input`.
  6697. Throws a runtime error if :attr:`input` is a bool tensor.
  6698. """
  6699. + r"""
  6700. Args:
  6701. {input}
  6702. Example::
  6703. >>> t = torch.randn(5)
  6704. >>> t
  6705. tensor([ 0.0090, -0.2262, -0.0682, -0.2866, 0.3940])
  6706. >>> torch.positive(t)
  6707. tensor([ 0.0090, -0.2262, -0.0682, -0.2866, 0.3940])
  6708. """.format(**common_args),
  6709. )
  6710. add_docstr(
  6711. torch.pow,
  6712. r"""
  6713. pow(input, exponent, *, out=None) -> Tensor
  6714. Takes the power of each element in :attr:`input` with :attr:`exponent` and
  6715. returns a tensor with the result.
  6716. :attr:`exponent` can be either a single ``float`` number or a `Tensor`
  6717. with the same number of elements as :attr:`input`.
  6718. When :attr:`exponent` is a scalar value, the operation applied is:
  6719. .. math::
  6720. \text{out}_i = x_i ^ \text{exponent}
  6721. When :attr:`exponent` is a tensor, the operation applied is:
  6722. .. math::
  6723. \text{out}_i = x_i ^ {\text{exponent}_i}
  6724. """
  6725. + r"""
  6726. When :attr:`exponent` is a tensor, the shapes of :attr:`input`
  6727. and :attr:`exponent` must be :ref:`broadcastable <broadcasting-semantics>`.
  6728. Args:
  6729. {input}
  6730. exponent (float or tensor): the exponent value
  6731. Keyword args:
  6732. {out}
  6733. Example::
  6734. >>> a = torch.randn(4)
  6735. >>> a
  6736. tensor([ 0.4331, 1.2475, 0.6834, -0.2791])
  6737. >>> torch.pow(a, 2)
  6738. tensor([ 0.1875, 1.5561, 0.4670, 0.0779])
  6739. >>> exp = torch.arange(1., 5.)
  6740. >>> a = torch.arange(1., 5.)
  6741. >>> a
  6742. tensor([ 1., 2., 3., 4.])
  6743. >>> exp
  6744. tensor([ 1., 2., 3., 4.])
  6745. >>> torch.pow(a, exp)
  6746. tensor([ 1., 4., 27., 256.])
  6747. .. function:: pow(self, exponent, *, out=None) -> Tensor
  6748. :noindex:
  6749. :attr:`self` is a scalar ``float`` value, and :attr:`exponent` is a tensor.
  6750. The returned tensor :attr:`out` is of the same shape as :attr:`exponent`
  6751. The operation applied is:
  6752. .. math::
  6753. \text{{out}}_i = \text{{self}} ^ {{\text{{exponent}}_i}}
  6754. Args:
  6755. self (float): the scalar base value for the power operation
  6756. exponent (Tensor): the exponent tensor
  6757. Keyword args:
  6758. {out}
  6759. Example::
  6760. >>> exp = torch.arange(1., 5.)
  6761. >>> base = 2
  6762. >>> torch.pow(base, exp)
  6763. tensor([ 2., 4., 8., 16.])
  6764. """.format(**common_args),
  6765. )
  6766. add_docstr(
  6767. torch.float_power,
  6768. r"""
  6769. float_power(input, exponent, *, out=None) -> Tensor
  6770. Raises :attr:`input` to the power of :attr:`exponent`, elementwise, in double precision.
  6771. If neither input is complex returns a ``torch.float64`` tensor,
  6772. and if one or more inputs is complex returns a ``torch.complex128`` tensor.
  6773. .. note::
  6774. This function always computes in double precision, unlike :func:`torch.pow`,
  6775. which implements more typical :ref:`type promotion <type-promotion-doc>`.
  6776. This is useful when the computation needs to be performed in a wider or more precise dtype,
  6777. or the results of the computation may contain fractional values not representable in the input dtypes,
  6778. like when an integer base is raised to a negative integer exponent.
  6779. Args:
  6780. input (Tensor or Number): the base value(s)
  6781. exponent (Tensor or Number): the exponent value(s)
  6782. Keyword args:
  6783. {out}
  6784. Example::
  6785. >>> a = torch.randint(10, (4,))
  6786. >>> a
  6787. tensor([6, 4, 7, 1])
  6788. >>> torch.float_power(a, 2)
  6789. tensor([36., 16., 49., 1.], dtype=torch.float64)
  6790. >>> a = torch.arange(1, 5)
  6791. >>> a
  6792. tensor([ 1, 2, 3, 4])
  6793. >>> exp = torch.tensor([2, -3, 4, -5])
  6794. >>> exp
  6795. tensor([ 2, -3, 4, -5])
  6796. >>> torch.float_power(a, exp)
  6797. tensor([1.0000e+00, 1.2500e-01, 8.1000e+01, 9.7656e-04], dtype=torch.float64)
  6798. """.format(**common_args),
  6799. )
  6800. add_docstr(
  6801. torch.prod,
  6802. r"""
  6803. prod(input: Tensor, *, dtype: Optional[_dtype]) -> Tensor
  6804. Returns the product of all elements in the :attr:`input` tensor.
  6805. Args:
  6806. {input}
  6807. Keyword args:
  6808. {dtype}
  6809. Example::
  6810. >>> a = torch.randn(1, 3)
  6811. >>> a
  6812. tensor([[-0.8020, 0.5428, -1.5854]])
  6813. >>> torch.prod(a)
  6814. tensor(0.6902)
  6815. .. function:: prod(input, dim, keepdim=False, *, dtype=None) -> Tensor
  6816. :noindex:
  6817. Returns the product of each row of the :attr:`input` tensor in the given
  6818. dimension :attr:`dim`.
  6819. {keepdim_details}
  6820. Args:
  6821. {input}
  6822. {opt_dim_all_reduce}
  6823. {opt_keepdim}
  6824. Keyword args:
  6825. {dtype}
  6826. Example::
  6827. >>> a = torch.randn(4, 2)
  6828. >>> a
  6829. tensor([[ 0.5261, -0.3837],
  6830. [ 1.1857, -0.2498],
  6831. [-1.1646, 0.0705],
  6832. [ 1.1131, -1.0629]])
  6833. >>> torch.prod(a, 1)
  6834. tensor([-0.2018, -0.2962, -0.0821, -1.1831])
  6835. """.format(**single_dim_common),
  6836. )
  6837. add_docstr(
  6838. torch.promote_types,
  6839. r"""
  6840. promote_types(type1, type2) -> dtype
  6841. Returns the :class:`torch.dtype` with the smallest size and scalar kind that is
  6842. not smaller nor of lower kind than either `type1` or `type2`. See type promotion
  6843. :ref:`documentation <type-promotion-doc>` for more information on the type
  6844. promotion logic.
  6845. Args:
  6846. type1 (:class:`torch.dtype`)
  6847. type2 (:class:`torch.dtype`)
  6848. Example::
  6849. >>> torch.promote_types(torch.int32, torch.float32)
  6850. torch.float32
  6851. >>> torch.promote_types(torch.uint8, torch.long)
  6852. torch.long
  6853. """,
  6854. )
  6855. add_docstr(
  6856. torch.qr,
  6857. r"""
  6858. qr(input: Tensor, some: bool = True, *, out: Union[Tensor, Tuple[Tensor, ...], List[Tensor], None]) -> (Tensor, Tensor)
  6859. Computes the QR decomposition of a matrix or a batch of matrices :attr:`input`,
  6860. and returns a namedtuple (Q, R) of tensors such that :math:`\text{input} = Q R`
  6861. with :math:`Q` being an orthogonal matrix or batch of orthogonal matrices and
  6862. :math:`R` being an upper triangular matrix or batch of upper triangular matrices.
  6863. If :attr:`some` is ``True``, then this function returns the thin (reduced) QR factorization.
  6864. Otherwise, if :attr:`some` is ``False``, this function returns the complete QR factorization.
  6865. .. warning::
  6866. :func:`torch.qr` is deprecated in favor of :func:`torch.linalg.qr`
  6867. and will be removed in a future PyTorch release. The boolean parameter :attr:`some` has been
  6868. replaced with a string parameter :attr:`mode`.
  6869. ``Q, R = torch.qr(A)`` should be replaced with
  6870. .. code:: python
  6871. Q, R = torch.linalg.qr(A)
  6872. ``Q, R = torch.qr(A, some=False)`` should be replaced with
  6873. .. code:: python
  6874. Q, R = torch.linalg.qr(A, mode="complete")
  6875. .. warning::
  6876. If you plan to backpropagate through QR, note that the current backward implementation
  6877. is only well-defined when the first :math:`\min(input.size(-1), input.size(-2))`
  6878. columns of :attr:`input` are linearly independent.
  6879. This behavior will probably change once QR supports pivoting.
  6880. .. note:: This function uses LAPACK for CPU inputs and MAGMA for CUDA inputs,
  6881. and may produce different (valid) decompositions on different device types
  6882. or different platforms.
  6883. Args:
  6884. input (Tensor): the input tensor of size :math:`(*, m, n)` where `*` is zero or more
  6885. batch dimensions consisting of matrices of dimension :math:`m \times n`.
  6886. some (bool, optional): Set to ``True`` for reduced QR decomposition and ``False`` for
  6887. complete QR decomposition. If `k = min(m, n)` then:
  6888. * ``some=True`` : returns `(Q, R)` with dimensions (m, k), (k, n) (default)
  6889. * ``'some=False'``: returns `(Q, R)` with dimensions (m, m), (m, n)
  6890. Keyword args:
  6891. out (tuple, optional): tuple of `Q` and `R` tensors.
  6892. The dimensions of `Q` and `R` are detailed in the description of :attr:`some` above.
  6893. Example::
  6894. >>> a = torch.tensor([[12., -51, 4], [6, 167, -68], [-4, 24, -41]])
  6895. >>> q, r = torch.qr(a)
  6896. >>> q
  6897. tensor([[-0.8571, 0.3943, 0.3314],
  6898. [-0.4286, -0.9029, -0.0343],
  6899. [ 0.2857, -0.1714, 0.9429]])
  6900. >>> r
  6901. tensor([[ -14.0000, -21.0000, 14.0000],
  6902. [ 0.0000, -175.0000, 70.0000],
  6903. [ 0.0000, 0.0000, -35.0000]])
  6904. >>> torch.mm(q, r).round()
  6905. tensor([[ 12., -51., 4.],
  6906. [ 6., 167., -68.],
  6907. [ -4., 24., -41.]])
  6908. >>> torch.mm(q.t(), q).round()
  6909. tensor([[ 1., 0., 0.],
  6910. [ 0., 1., -0.],
  6911. [ 0., -0., 1.]])
  6912. >>> a = torch.randn(3, 4, 5)
  6913. >>> q, r = torch.qr(a, some=False)
  6914. >>> torch.allclose(torch.matmul(q, r), a)
  6915. True
  6916. >>> torch.allclose(torch.matmul(q.mT, q), torch.eye(5))
  6917. True
  6918. """,
  6919. )
  6920. add_docstr(
  6921. torch.rad2deg,
  6922. r"""
  6923. rad2deg(input: Tensor, *, out: Optional[Tensor]) -> Tensor
  6924. Returns a new tensor with each of the elements of :attr:`input`
  6925. converted from angles in radians to degrees.
  6926. Args:
  6927. {input}
  6928. Keyword arguments:
  6929. {out}
  6930. Example::
  6931. >>> a = torch.tensor([[3.142, -3.142], [6.283, -6.283], [1.570, -1.570]])
  6932. >>> torch.rad2deg(a)
  6933. tensor([[ 180.0233, -180.0233],
  6934. [ 359.9894, -359.9894],
  6935. [ 89.9544, -89.9544]])
  6936. """.format(**common_args),
  6937. )
  6938. add_docstr(
  6939. torch.deg2rad,
  6940. r"""
  6941. deg2rad(input, *, out=None) -> Tensor
  6942. Returns a new tensor with each of the elements of :attr:`input`
  6943. converted from angles in degrees to radians.
  6944. Args:
  6945. {input}
  6946. Keyword arguments:
  6947. {out}
  6948. Example::
  6949. >>> a = torch.tensor([[180.0, -180.0], [360.0, -360.0], [90.0, -90.0]])
  6950. >>> torch.deg2rad(a)
  6951. tensor([[ 3.1416, -3.1416],
  6952. [ 6.2832, -6.2832],
  6953. [ 1.5708, -1.5708]])
  6954. """.format(**common_args),
  6955. )
  6956. add_docstr(
  6957. torch.heaviside,
  6958. r"""
  6959. heaviside(input, values, *, out=None) -> Tensor
  6960. Computes the Heaviside step function for each element in :attr:`input`.
  6961. The Heaviside step function is defined as:
  6962. .. math::
  6963. \text{{heaviside}}(input, values) = \begin{cases}
  6964. 0, & \text{if input < 0}\\
  6965. values, & \text{if input == 0}\\
  6966. 1, & \text{if input > 0}
  6967. \end{cases}
  6968. """
  6969. + r"""
  6970. Args:
  6971. {input}
  6972. values (Tensor): The values to use where :attr:`input` is zero.
  6973. Keyword arguments:
  6974. {out}
  6975. Example::
  6976. >>> input = torch.tensor([-1.5, 0, 2.0])
  6977. >>> values = torch.tensor([0.5])
  6978. >>> torch.heaviside(input, values)
  6979. tensor([0.0000, 0.5000, 1.0000])
  6980. >>> values = torch.tensor([1.2, -2.0, 3.5])
  6981. >>> torch.heaviside(input, values)
  6982. tensor([0., -2., 1.])
  6983. """.format(**common_args),
  6984. )
  6985. add_docstr(
  6986. torch.rand,
  6987. """
  6988. rand(*size, *, generator=None, out=None, dtype=None, layout=torch.strided, device=None, \
  6989. requires_grad=False, pin_memory=False) -> Tensor
  6990. """
  6991. + r"""
  6992. Returns a tensor filled with random numbers from a uniform distribution
  6993. on the interval :math:`[0, 1)`
  6994. The shape of the tensor is defined by the variable argument :attr:`size`.
  6995. Args:
  6996. size (int...): a sequence of integers defining the shape of the output tensor.
  6997. Can be a variable number of arguments or a collection like a list or tuple.
  6998. Keyword args:
  6999. {generator}
  7000. {out}
  7001. {dtype}
  7002. {layout}
  7003. {device}
  7004. {requires_grad}
  7005. {pin_memory}
  7006. Example::
  7007. >>> torch.rand(4)
  7008. tensor([ 0.5204, 0.2503, 0.3525, 0.5673])
  7009. >>> torch.rand(2, 3)
  7010. tensor([[ 0.8237, 0.5781, 0.6879],
  7011. [ 0.3816, 0.7249, 0.0998]])
  7012. """.format(**factory_common_args),
  7013. )
  7014. add_docstr(
  7015. torch.rand_like,
  7016. r"""
  7017. rand_like(input, *, dtype=None, layout=None, device=None, requires_grad=False, memory_format=torch.preserve_format) -> Tensor
  7018. Returns a tensor with the same size as :attr:`input` that is filled with
  7019. random numbers from a uniform distribution on the interval :math:`[0, 1)`.
  7020. ``torch.rand_like(input)`` is equivalent to
  7021. ``torch.rand(input.size(), dtype=input.dtype, layout=input.layout, device=input.device)``.
  7022. Args:
  7023. {input}
  7024. Keyword args:
  7025. {dtype}
  7026. {layout}
  7027. {device}
  7028. {requires_grad}
  7029. {memory_format}
  7030. """.format(**factory_like_common_args),
  7031. )
  7032. add_docstr(
  7033. torch.randint,
  7034. """
  7035. randint(low=0, high, size, \\*, generator=None, out=None, \
  7036. dtype=None, layout=torch.strided, device=None, requires_grad=False) -> Tensor
  7037. Returns a tensor filled with random integers generated uniformly
  7038. between :attr:`low` (inclusive) and :attr:`high` (exclusive).
  7039. The shape of the tensor is defined by the variable argument :attr:`size`.
  7040. .. note::
  7041. With the global dtype default (``torch.float32``), this function returns
  7042. a tensor with dtype ``torch.int64``.
  7043. Args:
  7044. low (int, optional): Lowest integer to be drawn from the distribution. Default: 0.
  7045. high (int): One above the highest integer to be drawn from the distribution.
  7046. size (tuple): a tuple defining the shape of the output tensor.
  7047. Keyword args:
  7048. {generator}
  7049. {out}
  7050. dtype (torch.dtype, optional): the desired data type of returned tensor. Default: if ``None``,
  7051. this function returns a tensor with dtype ``torch.int64``.
  7052. {layout}
  7053. {device}
  7054. {requires_grad}
  7055. Example::
  7056. >>> torch.randint(3, 5, (3,))
  7057. tensor([4, 3, 4])
  7058. >>> torch.randint(10, (2, 2))
  7059. tensor([[0, 2],
  7060. [5, 5]])
  7061. >>> torch.randint(3, 10, (2, 2))
  7062. tensor([[4, 5],
  7063. [6, 7]])
  7064. """.format(**factory_common_args),
  7065. )
  7066. add_docstr(
  7067. torch.randint_like,
  7068. """
  7069. randint_like(input, low=0, high, \\*, dtype=None, layout=torch.strided, device=None, requires_grad=False, \
  7070. memory_format=torch.preserve_format) -> Tensor
  7071. Returns a tensor with the same shape as Tensor :attr:`input` filled with
  7072. random integers generated uniformly between :attr:`low` (inclusive) and
  7073. :attr:`high` (exclusive).
  7074. .. note:
  7075. With the global dtype default (``torch.float32``), this function returns
  7076. a tensor with dtype ``torch.int64``.
  7077. Args:
  7078. {input}
  7079. low (int, optional): Lowest integer to be drawn from the distribution. Default: 0.
  7080. high (int): One above the highest integer to be drawn from the distribution.
  7081. Keyword args:
  7082. {dtype}
  7083. {layout}
  7084. {device}
  7085. {requires_grad}
  7086. {memory_format}
  7087. """.format(**factory_like_common_args),
  7088. )
  7089. add_docstr(
  7090. torch.randn,
  7091. """
  7092. randn(*size, *, generator=None, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False, \
  7093. pin_memory=False) -> Tensor
  7094. """
  7095. + r"""
  7096. Returns a tensor filled with random numbers from a normal distribution
  7097. with mean `0` and variance `1` (also called the standard normal
  7098. distribution).
  7099. .. math::
  7100. \text{{out}}_{{i}} \sim \mathcal{{N}}(0, 1)
  7101. For complex dtypes, the tensor is i.i.d. sampled from a `complex normal distribution`_ with zero mean and
  7102. unit variance as
  7103. .. math::
  7104. \text{{out}}_{{i}} \sim \mathcal{{CN}}(0, 1)
  7105. This is equivalent to separately sampling the real :math:`(\operatorname{{Re}})` and imaginary
  7106. :math:`(\operatorname{{Im}})` part of :math:`\text{{out}}_i` as
  7107. .. math::
  7108. \operatorname{{Re}}(\text{{out}}_{{i}}) \sim \mathcal{{N}}(0, \frac{{1}}{{2}}),\quad
  7109. \operatorname{{Im}}(\text{{out}}_{{i}}) \sim \mathcal{{N}}(0, \frac{{1}}{{2}})
  7110. The shape of the tensor is defined by the variable argument :attr:`size`.
  7111. Args:
  7112. size (int...): a sequence of integers defining the shape of the output tensor.
  7113. Can be a variable number of arguments or a collection like a list or tuple.
  7114. Keyword args:
  7115. {generator}
  7116. {out}
  7117. {dtype}
  7118. {layout}
  7119. {device}
  7120. {requires_grad}
  7121. {pin_memory}
  7122. Example::
  7123. >>> torch.randn(4)
  7124. tensor([-2.1436, 0.9966, 2.3426, -0.6366])
  7125. >>> torch.randn(2, 3)
  7126. tensor([[ 1.5954, 2.8929, -1.0923],
  7127. [ 1.1719, -0.4709, -0.1996]])
  7128. .. _complex normal distribution: https://en.wikipedia.org/wiki/Complex_normal_distribution
  7129. """.format(**factory_common_args),
  7130. )
  7131. add_docstr(
  7132. torch.randn_like,
  7133. r"""
  7134. randn_like(input, *, dtype=None, layout=None, device=None, requires_grad=False, memory_format=torch.preserve_format) -> Tensor
  7135. Returns a tensor with the same size as :attr:`input` that is filled with
  7136. random numbers from a normal distribution with mean 0 and variance 1. Please refer to :func:`torch.randn` for the
  7137. sampling process of complex dtypes. ``torch.randn_like(input)`` is equivalent to
  7138. ``torch.randn(input.size(), dtype=input.dtype, layout=input.layout, device=input.device)``.
  7139. Args:
  7140. {input}
  7141. Keyword args:
  7142. {dtype}
  7143. {layout}
  7144. {device}
  7145. {requires_grad}
  7146. {memory_format}
  7147. """.format(**factory_like_common_args),
  7148. )
  7149. add_docstr(
  7150. torch.randperm,
  7151. """
  7152. randperm(n, *, generator=None, out=None, dtype=torch.int64,layout=torch.strided, \
  7153. device=None, requires_grad=False, pin_memory=False) -> Tensor
  7154. """
  7155. + r"""
  7156. Returns a random permutation of integers from ``0`` to ``n - 1``.
  7157. Args:
  7158. n (int): the upper bound (exclusive)
  7159. Keyword args:
  7160. {generator}
  7161. {out}
  7162. dtype (:class:`torch.dtype`, optional): the desired data type of returned tensor.
  7163. Default: ``torch.int64``.
  7164. {layout}
  7165. {device}
  7166. {requires_grad}
  7167. {pin_memory}
  7168. Example::
  7169. >>> torch.randperm(4)
  7170. tensor([2, 1, 0, 3])
  7171. """.format(**factory_common_args),
  7172. )
  7173. add_docstr(
  7174. torch.tensor,
  7175. r"""
  7176. tensor(data, *, dtype=None, device=None, requires_grad=False, pin_memory=False) -> Tensor
  7177. Constructs a tensor with no autograd history (also known as a "leaf tensor", see :doc:`/notes/autograd`) by copying :attr:`data`.
  7178. .. warning::
  7179. When working with tensors prefer using :func:`torch.Tensor.clone`,
  7180. :func:`torch.Tensor.detach`, and :func:`torch.Tensor.requires_grad_` for
  7181. readability. Letting `t` be a tensor, ``torch.tensor(t)`` is equivalent to
  7182. ``t.detach().clone()``, and ``torch.tensor(t, requires_grad=True)``
  7183. is equivalent to ``t.detach().clone().requires_grad_(True)``.
  7184. .. seealso::
  7185. :func:`torch.as_tensor` preserves autograd history and avoids copies where possible.
  7186. :func:`torch.from_numpy` creates a tensor that shares storage with a NumPy array.
  7187. Args:
  7188. {data}
  7189. Keyword args:
  7190. {dtype}
  7191. device (:class:`torch.device`, optional): the device of the constructed tensor. If None and data is a tensor
  7192. then the device of data is used. If None and data is not a tensor then
  7193. the result tensor is constructed on the current device.
  7194. {requires_grad}
  7195. {pin_memory}
  7196. Example::
  7197. >>> torch.tensor([[0.1, 1.2], [2.2, 3.1], [4.9, 5.2]])
  7198. tensor([[ 0.1000, 1.2000],
  7199. [ 2.2000, 3.1000],
  7200. [ 4.9000, 5.2000]])
  7201. >>> torch.tensor([0, 1]) # Type inference on data
  7202. tensor([ 0, 1])
  7203. >>> torch.tensor([[0.11111, 0.222222, 0.3333333]],
  7204. ... dtype=torch.float64,
  7205. ... device=torch.device('cuda:0')) # creates a double tensor on a CUDA device
  7206. tensor([[ 0.1111, 0.2222, 0.3333]], dtype=torch.float64, device='cuda:0')
  7207. >>> torch.tensor(3.14159) # Create a zero-dimensional (scalar) tensor
  7208. tensor(3.1416)
  7209. >>> torch.tensor([]) # Create an empty tensor (of size (0,))
  7210. tensor([])
  7211. """.format(**factory_data_common_args),
  7212. )
  7213. add_docstr(
  7214. torch.range,
  7215. r"""
  7216. range(start=0, end, step=1, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) -> Tensor
  7217. Returns a 1-D tensor of size :math:`\left\lfloor \frac{\text{end} - \text{start}}{\text{step}} \right\rfloor + 1`
  7218. with values from :attr:`start` to :attr:`end` with step :attr:`step`. Step is
  7219. the gap between two values in the tensor.
  7220. .. math::
  7221. \text{out}_{i+1} = \text{out}_i + \text{step}.
  7222. """
  7223. + r"""
  7224. .. warning::
  7225. This function is deprecated and will be removed in a future release because its behavior is inconsistent with
  7226. Python's range builtin. Instead, use :func:`torch.arange`, which produces values in [start, end).
  7227. Args:
  7228. start (float, optional): the starting value for the set of points. Default: ``0``.
  7229. end (float): the ending value for the set of points
  7230. step (float, optional): the gap between each pair of adjacent points. Default: ``1``.
  7231. Keyword args:
  7232. {out}
  7233. {dtype} If `dtype` is not given, infer the data type from the other input
  7234. arguments. If any of `start`, `end`, or `step` are floating-point, the
  7235. `dtype` is inferred to be the default dtype, see
  7236. :meth:`~torch.get_default_dtype`. Otherwise, the `dtype` is inferred to
  7237. be `torch.int64`.
  7238. {layout}
  7239. {device}
  7240. {requires_grad}
  7241. Example::
  7242. >>> torch.range(1, 4)
  7243. tensor([ 1., 2., 3., 4.])
  7244. >>> torch.range(1, 4, 0.5)
  7245. tensor([ 1.0000, 1.5000, 2.0000, 2.5000, 3.0000, 3.5000, 4.0000])
  7246. """.format(**factory_common_args),
  7247. )
  7248. add_docstr(
  7249. torch.arange,
  7250. r"""
  7251. arange(start=0, end, step=1, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) -> Tensor
  7252. Returns a 1-D tensor of size :math:`\left\lceil \frac{\text{end} - \text{start}}{\text{step}} \right\rceil`
  7253. with values from the interval ``[start, end)`` taken with common difference
  7254. :attr:`step` beginning from `start`.
  7255. Note: When using floating-point dtypes (especially reduced precision types like ``bfloat16``),
  7256. the results may be affected by floating-point rounding behavior. Some values in the sequence
  7257. might not be exactly representable in certain floating-point formats, which can lead to
  7258. repeated values or unexpected rounding. For precise sequences, it is recommended to use
  7259. integer dtypes instead of floating-point dtypes.
  7260. Note that non-integer :attr:`step` is subject to floating point rounding errors when
  7261. comparing against :attr:`end`; to avoid inconsistency, we advise subtracting a small epsilon from :attr:`end`
  7262. in such cases.
  7263. .. math::
  7264. \text{out}_{{i+1}} = \text{out}_{i} + \text{step}
  7265. """
  7266. + r"""
  7267. Args:
  7268. start (Number, optional): the starting value for the set of points. Default: ``0``.
  7269. end (Number): the ending value for the set of points
  7270. step (Number, optional): the gap between each pair of adjacent points. Default: ``1``.
  7271. Keyword args:
  7272. {out}
  7273. {dtype} If `dtype` is not given, infer the data type from the other input
  7274. arguments. If any of `start`, `end`, or `stop` are floating-point, the
  7275. `dtype` is inferred to be the default dtype, see
  7276. :meth:`~torch.get_default_dtype`. Otherwise, the `dtype` is inferred to
  7277. be `torch.int64`.
  7278. {layout}
  7279. {device}
  7280. {requires_grad}
  7281. Example::
  7282. >>> torch.arange(5)
  7283. tensor([ 0, 1, 2, 3, 4])
  7284. >>> torch.arange(1, 4)
  7285. tensor([ 1, 2, 3])
  7286. >>> torch.arange(1, 2.5, 0.5)
  7287. tensor([ 1.0000, 1.5000, 2.0000])
  7288. """.format(**factory_common_args),
  7289. )
  7290. add_docstr(
  7291. torch.ravel,
  7292. r"""
  7293. ravel(input) -> Tensor
  7294. Return a contiguous flattened tensor. A copy is made only if needed.
  7295. Args:
  7296. {input}
  7297. Example::
  7298. >>> t = torch.tensor([[[1, 2],
  7299. ... [3, 4]],
  7300. ... [[5, 6],
  7301. ... [7, 8]]])
  7302. >>> torch.ravel(t)
  7303. tensor([1, 2, 3, 4, 5, 6, 7, 8])
  7304. """.format(**common_args),
  7305. )
  7306. add_docstr(
  7307. torch.remainder,
  7308. r"""
  7309. remainder(input, other, *, out=None) -> Tensor
  7310. Computes
  7311. `Python's modulus operation <https://docs.python.org/3/reference/expressions.html#binary-arithmetic-operations>`_
  7312. entrywise. The result has the same sign as the divisor :attr:`other` and its absolute value
  7313. is less than that of :attr:`other`.
  7314. It may also be defined in terms of :func:`torch.div` as
  7315. .. code:: python
  7316. torch.remainder(a, b) == a - a.div(b, rounding_mode="floor") * b
  7317. Supports :ref:`broadcasting to a common shape <broadcasting-semantics>`,
  7318. :ref:`type promotion <type-promotion-doc>`, and integer and float inputs.
  7319. .. note::
  7320. Complex inputs are not supported. In some cases, it is not mathematically
  7321. possible to satisfy the definition of a modulo operation with complex numbers.
  7322. See :func:`torch.fmod` for how division by zero is handled.
  7323. .. seealso::
  7324. :func:`torch.fmod` which implements C++'s `std::fmod <https://en.cppreference.com/w/cpp/numeric/math/fmod>`_.
  7325. This one is defined in terms of division rounding towards zero.
  7326. Args:
  7327. input (Tensor or Scalar): the dividend
  7328. other (Tensor or Scalar): the divisor
  7329. Keyword args:
  7330. {out}
  7331. Example::
  7332. >>> torch.remainder(torch.tensor([-3., -2, -1, 1, 2, 3]), 2)
  7333. tensor([ 1., 0., 1., 1., 0., 1.])
  7334. >>> torch.remainder(torch.tensor([1, 2, 3, 4, 5]), -1.5)
  7335. tensor([ -0.5000, -1.0000, 0.0000, -0.5000, -1.0000 ])
  7336. """.format(**common_args),
  7337. )
  7338. add_docstr(
  7339. torch.renorm,
  7340. r"""
  7341. renorm(input, p, dim, maxnorm, *, out=None) -> Tensor
  7342. Returns a tensor where each sub-tensor of :attr:`input` along dimension
  7343. :attr:`dim` is normalized such that the `p`-norm of the sub-tensor is lower
  7344. than the value :attr:`maxnorm`
  7345. .. note:: If the norm of a row is lower than `maxnorm`, the row is unchanged
  7346. Args:
  7347. {input}
  7348. p (float): the power for the norm computation
  7349. dim (int): the dimension to slice over to get the sub-tensors
  7350. maxnorm (float): the maximum norm to keep each sub-tensor under
  7351. Keyword args:
  7352. {out}
  7353. Example::
  7354. >>> x = torch.ones(3, 3)
  7355. >>> x[1].fill_(2)
  7356. tensor([ 2., 2., 2.])
  7357. >>> x[2].fill_(3)
  7358. tensor([ 3., 3., 3.])
  7359. >>> x
  7360. tensor([[ 1., 1., 1.],
  7361. [ 2., 2., 2.],
  7362. [ 3., 3., 3.]])
  7363. >>> torch.renorm(x, 1, 0, 5)
  7364. tensor([[ 1.0000, 1.0000, 1.0000],
  7365. [ 1.6667, 1.6667, 1.6667],
  7366. [ 1.6667, 1.6667, 1.6667]])
  7367. """.format(**common_args),
  7368. )
  7369. add_docstr(
  7370. torch.reshape,
  7371. r"""
  7372. reshape(input, shape) -> Tensor
  7373. Returns a tensor with the same data and number of elements as :attr:`input`,
  7374. but with the specified shape. When possible, the returned tensor will be a view
  7375. of :attr:`input`. Otherwise, it will be a copy. Contiguous inputs and inputs
  7376. with compatible strides can be reshaped without copying, but you should not
  7377. depend on the copying vs. viewing behavior.
  7378. See :meth:`torch.Tensor.view` on when it is possible to return a view.
  7379. A single dimension may be -1, in which case it's inferred from the remaining
  7380. dimensions and the number of elements in :attr:`input`.
  7381. Args:
  7382. input (Tensor): the tensor to be reshaped
  7383. shape (tuple of int): the new shape
  7384. Example::
  7385. >>> a = torch.arange(4.)
  7386. >>> torch.reshape(a, (2, 2))
  7387. tensor([[ 0., 1.],
  7388. [ 2., 3.]])
  7389. >>> b = torch.tensor([[0, 1], [2, 3]])
  7390. >>> torch.reshape(b, (-1,))
  7391. tensor([ 0, 1, 2, 3])
  7392. """,
  7393. )
  7394. add_docstr(
  7395. torch.result_type,
  7396. r"""
  7397. result_type(tensor1, tensor2) -> dtype
  7398. Returns the :class:`torch.dtype` that would result from performing an arithmetic
  7399. operation on the provided input tensors. See type promotion :ref:`documentation <type-promotion-doc>`
  7400. for more information on the type promotion logic.
  7401. Args:
  7402. tensor1 (Tensor or Number): an input tensor or number
  7403. tensor2 (Tensor or Number): an input tensor or number
  7404. Example::
  7405. >>> torch.result_type(torch.tensor([1, 2], dtype=torch.int), 1.0)
  7406. torch.float32
  7407. >>> torch.result_type(torch.tensor([1, 2], dtype=torch.uint8), torch.tensor(1))
  7408. torch.uint8
  7409. """,
  7410. )
  7411. add_docstr(
  7412. torch.row_stack,
  7413. r"""
  7414. row_stack(tensors, *, out=None) -> Tensor
  7415. Alias of :func:`torch.vstack`.
  7416. """,
  7417. )
  7418. add_docstr(
  7419. torch.round,
  7420. r"""
  7421. round(input, *, decimals=0, out=None) -> Tensor
  7422. Rounds elements of :attr:`input` to the nearest integer.
  7423. For integer inputs, follows the array-api convention of returning a
  7424. copy of the input tensor.
  7425. The return type of output is same as that of input's dtype.
  7426. .. note::
  7427. This function implements the "round half to even" to
  7428. break ties when a number is equidistant from two
  7429. integers (e.g. `round(2.5)` is 2).
  7430. When the :attr:\`decimals\` argument is specified the
  7431. algorithm used is similar to NumPy's `around`. This
  7432. algorithm is fast but inexact and it can easily
  7433. overflow for low precision dtypes.
  7434. Eg. `round(tensor([10000], dtype=torch.float16), decimals=3)` is `inf`.
  7435. .. seealso::
  7436. :func:`torch.ceil`, which rounds up.
  7437. :func:`torch.floor`, which rounds down.
  7438. :func:`torch.trunc`, which rounds towards zero.
  7439. Args:
  7440. {input}
  7441. decimals (int): Number of decimal places to round to (default: 0).
  7442. If decimals is negative, it specifies the number of positions
  7443. to the left of the decimal point.
  7444. Keyword args:
  7445. {out}
  7446. Example::
  7447. >>> torch.round(torch.tensor((4.7, -2.3, 9.1, -7.7)))
  7448. tensor([ 5., -2., 9., -8.])
  7449. >>> # Values equidistant from two integers are rounded towards the
  7450. >>> # the nearest even value (zero is treated as even)
  7451. >>> torch.round(torch.tensor([-0.5, 0.5, 1.5, 2.5]))
  7452. tensor([-0., 0., 2., 2.])
  7453. >>> # A positive decimals argument rounds to the to that decimal place
  7454. >>> torch.round(torch.tensor([0.1234567]), decimals=3)
  7455. tensor([0.1230])
  7456. >>> # A negative decimals argument rounds to the left of the decimal
  7457. >>> torch.round(torch.tensor([1200.1234567]), decimals=-3)
  7458. tensor([1000.])
  7459. """.format(**common_args),
  7460. )
  7461. add_docstr(
  7462. torch.rsqrt,
  7463. r"""
  7464. rsqrt(input, *, out=None) -> Tensor
  7465. Returns a new tensor with the reciprocal of the square-root of each of
  7466. the elements of :attr:`input`.
  7467. .. math::
  7468. \text{out}_{i} = \frac{1}{\sqrt{\text{input}_{i}}}
  7469. """
  7470. + r"""
  7471. Args:
  7472. {input}
  7473. Keyword args:
  7474. {out}
  7475. Example::
  7476. >>> a = torch.randn(4)
  7477. >>> a
  7478. tensor([-0.0370, 0.2970, 1.5420, -0.9105])
  7479. >>> torch.rsqrt(a)
  7480. tensor([ nan, 1.8351, 0.8053, nan])
  7481. """.format(**common_args),
  7482. )
  7483. add_docstr(
  7484. torch.scatter,
  7485. r"""
  7486. scatter(input, dim, index, src) -> Tensor
  7487. Out-of-place version of :meth:`torch.Tensor.scatter_`
  7488. """,
  7489. )
  7490. add_docstr(
  7491. torch.scatter_add,
  7492. r"""
  7493. scatter_add(input, dim, index, src) -> Tensor
  7494. Out-of-place version of :meth:`torch.Tensor.scatter_add_`
  7495. """,
  7496. )
  7497. add_docstr(
  7498. torch.scatter_reduce,
  7499. r"""
  7500. scatter_reduce(input, dim, index, src, reduce, *, include_self=True) -> Tensor
  7501. Out-of-place version of :meth:`torch.Tensor.scatter_reduce_`
  7502. """,
  7503. )
  7504. add_docstr(
  7505. torch.segment_reduce,
  7506. r"""
  7507. segment_reduce(data: Tensor, reduce: str, *, lengths: Tensor | None = None, indices: Tensor | None = None, offsets: Tensor | None = None, axis: _int = 0, unsafe: _bool = False, initial: Number | _complex | None = None) -> Tensor # noqa: B950
  7508. Perform a segment reduction operation on the input tensor along the specified axis.
  7509. Args:
  7510. data (Tensor): The input tensor on which the segment reduction operation will be performed.
  7511. reduce (str): The type of reduction operation. Supported values are ``sum``, ``mean``, ``max``, ``min``, ``prod``.
  7512. Keyword args:
  7513. lengths (Tensor, optional): Length of each segment. Default: ``None``.
  7514. offsets (Tensor, optional): Offset of each segment. Default: ``None``.
  7515. axis (int, optional): The axis perform reduction. Default: ``0``.
  7516. unsafe (bool, optional): Skip validation If `True`. Default: ``False``.
  7517. initial (Number, optional): The initial value for the reduction operation. Default: ``None``.
  7518. Example::
  7519. >>> data = torch.tensor([[1, 2, 3, 4],[5, 6, 7, 8],[9, 10, 11, 12]], dtype=torch.float32, device='cuda')
  7520. >>> lengths = torch.tensor([2, 1], device='cuda')
  7521. >>> torch.segment_reduce(data, 'max', lengths=lengths)
  7522. tensor([[ 5., 6., 7., 8.],
  7523. [ 9., 10., 11., 12.]], device='cuda:0')
  7524. """,
  7525. )
  7526. add_docstr(
  7527. torch.select,
  7528. r"""
  7529. select(input, dim, index) -> Tensor
  7530. Slices the :attr:`input` tensor along the selected dimension at the given index.
  7531. This function returns a view of the original tensor with the given dimension removed.
  7532. .. note:: If :attr:`input` is a sparse tensor and returning a view of
  7533. the tensor is not possible, a RuntimeError exception is
  7534. raised. In this is the case, consider using
  7535. :func:`torch.select_copy` function.
  7536. Args:
  7537. {input}
  7538. dim (int): the dimension to slice
  7539. index (int): the index to select with
  7540. .. note::
  7541. :meth:`select` is equivalent to slicing. For example,
  7542. ``tensor.select(0, index)`` is equivalent to ``tensor[index]`` and
  7543. ``tensor.select(2, index)`` is equivalent to ``tensor[:,:,index]``.
  7544. """.format(**common_args),
  7545. )
  7546. add_docstr(
  7547. torch.select_scatter,
  7548. r"""
  7549. select_scatter(input, src, dim, index) -> Tensor
  7550. Embeds the values of the :attr:`src` tensor into :attr:`input` at the given index.
  7551. This function returns a tensor with fresh storage; it does not create a view.
  7552. Args:
  7553. {input}
  7554. src (Tensor): The tensor to embed into :attr:`input`
  7555. dim (int): the dimension to insert the slice into.
  7556. index (int): the index to select with
  7557. .. note::
  7558. :attr:`src` must be of the proper size in order to be embedded
  7559. into :attr:`input`. Specifically, it should have the same shape as
  7560. ``torch.select(input, dim, index)``
  7561. Example::
  7562. >>> a = torch.zeros(2, 2)
  7563. >>> b = torch.ones(2)
  7564. >>> a.select_scatter(b, 0, 0)
  7565. tensor([[1., 1.],
  7566. [0., 0.]])
  7567. """.format(**common_args),
  7568. )
  7569. add_docstr(
  7570. torch.slice_scatter,
  7571. r"""
  7572. slice_scatter(input, src, dim=0, start=None, end=None, step=1) -> Tensor
  7573. Embeds the values of the :attr:`src` tensor into :attr:`input` at the given
  7574. dimension.
  7575. This function returns a tensor with fresh storage; it does not create a view.
  7576. Args:
  7577. {input}
  7578. src (Tensor): The tensor to embed into :attr:`input`
  7579. dim (int): the dimension to insert the slice into
  7580. start (Optional[int]): the start index of where to insert the slice
  7581. end (Optional[int]): the end index of where to insert the slice
  7582. step (int): the how many elements to skip in
  7583. Example::
  7584. >>> a = torch.zeros(8, 8)
  7585. >>> b = torch.ones(2, 8)
  7586. >>> a.slice_scatter(b, start=6)
  7587. tensor([[0., 0., 0., 0., 0., 0., 0., 0.],
  7588. [0., 0., 0., 0., 0., 0., 0., 0.],
  7589. [0., 0., 0., 0., 0., 0., 0., 0.],
  7590. [0., 0., 0., 0., 0., 0., 0., 0.],
  7591. [0., 0., 0., 0., 0., 0., 0., 0.],
  7592. [0., 0., 0., 0., 0., 0., 0., 0.],
  7593. [1., 1., 1., 1., 1., 1., 1., 1.],
  7594. [1., 1., 1., 1., 1., 1., 1., 1.]])
  7595. >>> b = torch.ones(8, 2)
  7596. >>> a.slice_scatter(b, dim=1, start=2, end=6, step=2)
  7597. tensor([[0., 0., 1., 0., 1., 0., 0., 0.],
  7598. [0., 0., 1., 0., 1., 0., 0., 0.],
  7599. [0., 0., 1., 0., 1., 0., 0., 0.],
  7600. [0., 0., 1., 0., 1., 0., 0., 0.],
  7601. [0., 0., 1., 0., 1., 0., 0., 0.],
  7602. [0., 0., 1., 0., 1., 0., 0., 0.],
  7603. [0., 0., 1., 0., 1., 0., 0., 0.],
  7604. [0., 0., 1., 0., 1., 0., 0., 0.]])
  7605. """.format(**common_args),
  7606. )
  7607. add_docstr(
  7608. torch.set_flush_denormal,
  7609. r"""
  7610. set_flush_denormal(mode) -> bool
  7611. Disables denormal floating numbers on CPU.
  7612. Returns ``True`` if your system supports flushing denormal numbers and it
  7613. successfully configures flush denormal mode. :meth:`~torch.set_flush_denormal`
  7614. is supported on x86 architectures supporting SSE3 and AArch64 architecture.
  7615. Args:
  7616. mode (bool): Controls whether to enable flush denormal mode or not
  7617. Example::
  7618. >>> torch.set_flush_denormal(True)
  7619. True
  7620. >>> torch.tensor([1e-323], dtype=torch.float64)
  7621. tensor([ 0.], dtype=torch.float64)
  7622. >>> torch.set_flush_denormal(False)
  7623. True
  7624. >>> torch.tensor([1e-323], dtype=torch.float64)
  7625. tensor(9.88131e-324 *
  7626. [ 1.0000], dtype=torch.float64)
  7627. """,
  7628. )
  7629. add_docstr(
  7630. torch.set_num_threads,
  7631. r"""
  7632. set_num_threads(int)
  7633. Sets the number of threads used for intraop parallelism on CPU.
  7634. .. warning::
  7635. To ensure that the correct number of threads is used, set_num_threads
  7636. must be called before running eager, JIT or autograd code.
  7637. """,
  7638. )
  7639. add_docstr(
  7640. torch.set_num_interop_threads,
  7641. r"""
  7642. set_num_interop_threads(int)
  7643. Sets the number of threads used for interop parallelism
  7644. (e.g. in JIT interpreter) on CPU.
  7645. .. warning::
  7646. Can only be called once and before any inter-op parallel work
  7647. is started (e.g. JIT execution).
  7648. """,
  7649. )
  7650. add_docstr(
  7651. torch.sigmoid,
  7652. r"""
  7653. sigmoid(input, *, out=None) -> Tensor
  7654. Alias for :func:`torch.special.expit`.
  7655. """,
  7656. )
  7657. add_docstr(
  7658. torch.logit,
  7659. r"""
  7660. logit(input, eps=None, *, out=None) -> Tensor
  7661. Alias for :func:`torch.special.logit`.
  7662. """,
  7663. )
  7664. add_docstr(
  7665. torch.sign,
  7666. r"""
  7667. sign(input, *, out=None) -> Tensor
  7668. Returns a new tensor with the signs of the elements of :attr:`input`.
  7669. .. math::
  7670. \text{out}_{i} = \operatorname{sgn}(\text{input}_{i})
  7671. """
  7672. + r"""
  7673. Args:
  7674. {input}
  7675. Keyword args:
  7676. {out}
  7677. Example::
  7678. >>> a = torch.tensor([0.7, -1.2, 0., 2.3])
  7679. >>> a
  7680. tensor([ 0.7000, -1.2000, 0.0000, 2.3000])
  7681. >>> torch.sign(a)
  7682. tensor([ 1., -1., 0., 1.])
  7683. """.format(**common_args),
  7684. )
  7685. add_docstr(
  7686. torch.signbit,
  7687. r"""
  7688. signbit(input, *, out=None) -> Tensor
  7689. Tests if each element of :attr:`input` has its sign bit set or not.
  7690. Args:
  7691. {input}
  7692. Keyword args:
  7693. {out}
  7694. Example::
  7695. >>> a = torch.tensor([0.7, -1.2, 0., 2.3])
  7696. >>> torch.signbit(a)
  7697. tensor([ False, True, False, False])
  7698. >>> a = torch.tensor([-0.0, 0.0])
  7699. >>> torch.signbit(a)
  7700. tensor([ True, False])
  7701. .. note::
  7702. signbit handles signed zeros, so negative zero (-0) returns True.
  7703. """.format(**common_args),
  7704. )
  7705. add_docstr(
  7706. torch.sgn,
  7707. r"""
  7708. sgn(input, *, out=None) -> Tensor
  7709. This function is an extension of torch.sign() to complex tensors.
  7710. It computes a new tensor whose elements have
  7711. the same angles as the corresponding elements of :attr:`input` and
  7712. absolute values (i.e. magnitudes) of one for complex tensors and
  7713. is equivalent to torch.sign() for non-complex tensors.
  7714. .. math::
  7715. \text{out}_{i} = \begin{cases}
  7716. 0 & |\text{{input}}_i| == 0 \\
  7717. \frac{{\text{{input}}_i}}{|{\text{{input}}_i}|} & \text{otherwise}
  7718. \end{cases}
  7719. """
  7720. + r"""
  7721. Args:
  7722. {input}
  7723. Keyword args:
  7724. {out}
  7725. Example::
  7726. >>> t = torch.tensor([3+4j, 7-24j, 0, 1+2j])
  7727. >>> t.sgn()
  7728. tensor([0.6000+0.8000j, 0.2800-0.9600j, 0.0000+0.0000j, 0.4472+0.8944j])
  7729. """.format(**common_args),
  7730. )
  7731. add_docstr(
  7732. torch.sin,
  7733. r"""
  7734. sin(input, *, out=None) -> Tensor
  7735. Returns a new tensor with the sine of the elements of :attr:`input`.
  7736. .. math::
  7737. \text{out}_{i} = \sin(\text{input}_{i})
  7738. """
  7739. + r"""
  7740. Args:
  7741. {input}
  7742. Keyword args:
  7743. {out}
  7744. Example::
  7745. >>> a = torch.randn(4)
  7746. >>> a
  7747. tensor([-0.5461, 0.1347, -2.7266, -0.2746])
  7748. >>> torch.sin(a)
  7749. tensor([-0.5194, 0.1343, -0.4032, -0.2711])
  7750. """.format(**common_args),
  7751. )
  7752. add_docstr(
  7753. torch.sinc,
  7754. r"""
  7755. sinc(input, *, out=None) -> Tensor
  7756. Alias for :func:`torch.special.sinc`.
  7757. """,
  7758. )
  7759. add_docstr(
  7760. torch.sinh,
  7761. r"""
  7762. sinh(input, *, out=None) -> Tensor
  7763. Returns a new tensor with the hyperbolic sine of the elements of
  7764. :attr:`input`.
  7765. .. math::
  7766. \text{out}_{i} = \sinh(\text{input}_{i})
  7767. """
  7768. + r"""
  7769. Args:
  7770. {input}
  7771. Keyword args:
  7772. {out}
  7773. Example::
  7774. >>> a = torch.randn(4)
  7775. >>> a
  7776. tensor([ 0.5380, -0.8632, -0.1265, 0.9399])
  7777. >>> torch.sinh(a)
  7778. tensor([ 0.5644, -0.9744, -0.1268, 1.0845])
  7779. .. note::
  7780. When :attr:`input` is on the CPU, the implementation of torch.sinh may use
  7781. the Sleef library, which rounds very large results to infinity or negative
  7782. infinity. See `here <https://sleef.org/purec.xhtml>`_ for details.
  7783. """.format(**common_args),
  7784. )
  7785. add_docstr(
  7786. torch.sort,
  7787. r"""
  7788. sort(input, dim=-1, descending=False, *, stable=False, out=None) -> (Tensor, LongTensor)
  7789. Sorts the elements of the :attr:`input` tensor along a given dimension
  7790. in ascending order by value.
  7791. If :attr:`dim` is not given, the last dimension of the `input` is chosen.
  7792. If :attr:`descending` is ``True`` then the elements are sorted in descending
  7793. order by value.
  7794. If :attr:`stable` is ``True`` then the sorting routine becomes stable, preserving
  7795. the order of equivalent elements.
  7796. A namedtuple of (values, indices) is returned, where the `values` are the
  7797. sorted values and `indices` are the indices of the elements in the original
  7798. `input` tensor.
  7799. Args:
  7800. {input}
  7801. dim (int, optional): the dimension to sort along
  7802. descending (bool, optional): controls the sorting order (ascending or descending)
  7803. Keyword args:
  7804. stable (bool, optional): makes the sorting routine stable, which guarantees that the order
  7805. of equivalent elements is preserved.
  7806. out (tuple, optional): the output tuple of (`Tensor`, `LongTensor`) that can
  7807. be optionally given to be used as output buffers
  7808. Example::
  7809. >>> x = torch.randn(3, 4)
  7810. >>> sorted, indices = torch.sort(x)
  7811. >>> sorted
  7812. tensor([[-0.2162, 0.0608, 0.6719, 2.3332],
  7813. [-0.5793, 0.0061, 0.6058, 0.9497],
  7814. [-0.5071, 0.3343, 0.9553, 1.0960]])
  7815. >>> indices
  7816. tensor([[ 1, 0, 2, 3],
  7817. [ 3, 1, 0, 2],
  7818. [ 0, 3, 1, 2]])
  7819. >>> sorted, indices = torch.sort(x, 0)
  7820. >>> sorted
  7821. tensor([[-0.5071, -0.2162, 0.6719, -0.5793],
  7822. [ 0.0608, 0.0061, 0.9497, 0.3343],
  7823. [ 0.6058, 0.9553, 1.0960, 2.3332]])
  7824. >>> indices
  7825. tensor([[ 2, 0, 0, 1],
  7826. [ 0, 1, 1, 2],
  7827. [ 1, 2, 2, 0]])
  7828. >>> x = torch.tensor([0, 1] * 9)
  7829. >>> x.sort()
  7830. torch.return_types.sort(
  7831. values=tensor([0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1]),
  7832. indices=tensor([ 2, 16, 4, 6, 14, 8, 0, 10, 12, 9, 17, 15, 13, 11, 7, 5, 3, 1]))
  7833. >>> x.sort(stable=True)
  7834. torch.return_types.sort(
  7835. values=tensor([0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1]),
  7836. indices=tensor([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 1, 3, 5, 7, 9, 11, 13, 15, 17]))
  7837. """.format(**common_args),
  7838. )
  7839. add_docstr(
  7840. torch.argsort,
  7841. r"""
  7842. argsort(input, dim=-1, descending=False, *, stable=False) -> Tensor
  7843. Returns the indices that sort a tensor along a given dimension in ascending
  7844. order by value.
  7845. This is the second value returned by :meth:`torch.sort`. See its documentation
  7846. for the exact semantics of this method.
  7847. If :attr:`stable` is ``True`` then the sorting routine becomes stable, preserving
  7848. the order of equivalent elements. If ``False``, the relative order of values
  7849. which compare equal is not guaranteed. ``True`` is slower.
  7850. Args:
  7851. {input}
  7852. dim (int, optional): the dimension to sort along
  7853. descending (bool, optional): controls the sorting order (ascending or descending)
  7854. Keyword args:
  7855. stable (bool, optional): controls the relative order of equivalent elements
  7856. Example::
  7857. >>> a = torch.randn(4, 4)
  7858. >>> a
  7859. tensor([[ 0.0785, 1.5267, -0.8521, 0.4065],
  7860. [ 0.1598, 0.0788, -0.0745, -1.2700],
  7861. [ 1.2208, 1.0722, -0.7064, 1.2564],
  7862. [ 0.0669, -0.2318, -0.8229, -0.9280]])
  7863. >>> torch.argsort(a, dim=1)
  7864. tensor([[2, 0, 3, 1],
  7865. [3, 2, 1, 0],
  7866. [2, 1, 0, 3],
  7867. [3, 2, 1, 0]])
  7868. """.format(**common_args),
  7869. )
  7870. add_docstr(
  7871. torch.msort,
  7872. r"""
  7873. msort(input: Tensor, *, out: Optional[Tensor]) -> Tensor
  7874. Sorts the elements of the :attr:`input` tensor along its first dimension
  7875. in ascending order by value.
  7876. .. note:: `torch.msort(t)` is equivalent to `torch.sort(t, dim=0)[0]`.
  7877. See also :func:`torch.sort`.
  7878. Args:
  7879. {input}
  7880. Keyword args:
  7881. {out}
  7882. Example::
  7883. >>> t = torch.randn(3, 4)
  7884. >>> t
  7885. tensor([[-0.1321, 0.4370, -1.2631, -1.1289],
  7886. [-2.0527, -1.1250, 0.2275, 0.3077],
  7887. [-0.0881, -0.1259, -0.5495, 1.0284]])
  7888. >>> torch.msort(t)
  7889. tensor([[-2.0527, -1.1250, -1.2631, -1.1289],
  7890. [-0.1321, -0.1259, -0.5495, 0.3077],
  7891. [-0.0881, 0.4370, 0.2275, 1.0284]])
  7892. """.format(**common_args),
  7893. )
  7894. add_docstr(
  7895. torch.sparse_compressed_tensor,
  7896. r"""sparse_compressed_tensor(compressed_indices, plain_indices, values, size=None, """
  7897. r"""*, dtype=None, layout=None, device=None, pin_memory=False, requires_grad=False, check_invariants=None) -> Tensor
  7898. Constructs a :ref:`sparse tensor in Compressed Sparse format - CSR,
  7899. CSC, BSR, or BSC - <sparse-compressed-docs>` with specified values at
  7900. the given :attr:`compressed_indices` and :attr:`plain_indices`. Sparse
  7901. matrix multiplication operations in Compressed Sparse format are
  7902. typically faster than that for sparse tensors in COO format. Make you
  7903. have a look at :ref:`the note on the data type of the indices
  7904. <sparse-compressed-docs>`.
  7905. {sparse_factory_device_note}
  7906. Args:
  7907. compressed_indices (array_like): (B+1)-dimensional array of size
  7908. ``(*batchsize, compressed_dim_size + 1)``. The last element of
  7909. each batch is the number of non-zero elements or blocks. This
  7910. tensor encodes the index in ``values`` and ``plain_indices``
  7911. depending on where the given compressed dimension (row or
  7912. column) starts. Each successive number in the tensor
  7913. subtracted by the number before it denotes the number of
  7914. elements or blocks in a given compressed dimension.
  7915. plain_indices (array_like): Plain dimension (column or row)
  7916. coordinates of each element or block in values. (B+1)-dimensional
  7917. tensor with the same length as values.
  7918. values (array_list): Initial values for the tensor. Can be a list,
  7919. tuple, NumPy ``ndarray``, scalar, and other types. that
  7920. represents a (1+K)-dimensional (for CSR and CSC layouts) or
  7921. (1+2+K)-dimensional tensor (for BSR and BSC layouts) where
  7922. ``K`` is the number of dense dimensions.
  7923. size (list, tuple, :class:`torch.Size`, optional): Size of the
  7924. sparse tensor: ``(*batchsize, nrows * blocksize[0], ncols *
  7925. blocksize[1], *densesize)`` where ``blocksize[0] ==
  7926. blocksize[1] == 1`` for CSR and CSC formats. If not provided,
  7927. the size will be inferred as the minimum size big enough to
  7928. hold all non-zero elements or blocks.
  7929. Keyword args:
  7930. dtype (:class:`torch.dtype`, optional): the desired data type of
  7931. returned tensor. Default: if None, infers data type from
  7932. :attr:`values`.
  7933. layout (:class:`torch.layout`, required): the desired layout of
  7934. returned tensor: :attr:`torch.sparse_csr`,
  7935. :attr:`torch.sparse_csc`, :attr:`torch.sparse_bsr`, or
  7936. :attr:`torch.sparse_bsc`.
  7937. device (:class:`torch.device`, optional): the desired device of
  7938. returned tensor. Default: if None, uses the current device
  7939. for the default tensor type (see
  7940. :func:`torch.set_default_device`). :attr:`device` will be
  7941. the CPU for CPU tensor types and the current CUDA device for
  7942. CUDA tensor types.
  7943. {pin_memory}
  7944. {requires_grad}
  7945. {check_invariants}
  7946. Example::
  7947. >>> compressed_indices = [0, 2, 4]
  7948. >>> plain_indices = [0, 1, 0, 1]
  7949. >>> values = [1, 2, 3, 4]
  7950. >>> torch.sparse_compressed_tensor(torch.tensor(compressed_indices, dtype=torch.int64),
  7951. ... torch.tensor(plain_indices, dtype=torch.int64),
  7952. ... torch.tensor(values), dtype=torch.double, layout=torch.sparse_csr)
  7953. tensor(crow_indices=tensor([0, 2, 4]),
  7954. col_indices=tensor([0, 1, 0, 1]),
  7955. values=tensor([1., 2., 3., 4.]), size=(2, 2), nnz=4,
  7956. dtype=torch.float64, layout=torch.sparse_csr)
  7957. """.format(**factory_common_args),
  7958. )
  7959. add_docstr(
  7960. torch.sparse_csr_tensor,
  7961. r"""sparse_csr_tensor(crow_indices, col_indices, values, size=None, """
  7962. r"""*, dtype=None, device=None, pin_memory=False, requires_grad=False, check_invariants=None) -> Tensor
  7963. Constructs a :ref:`sparse tensor in CSR (Compressed Sparse Row) <sparse-csr-docs>` with specified
  7964. values at the given :attr:`crow_indices` and :attr:`col_indices`. Sparse matrix multiplication operations
  7965. in CSR format are typically faster than that for sparse tensors in COO format. Make you have a look
  7966. at :ref:`the note on the data type of the indices <sparse-csr-docs>`.
  7967. {sparse_factory_device_note}
  7968. Args:
  7969. crow_indices (array_like): (B+1)-dimensional array of size
  7970. ``(*batchsize, nrows + 1)``. The last element of each batch
  7971. is the number of non-zeros. This tensor encodes the index in
  7972. values and col_indices depending on where the given row
  7973. starts. Each successive number in the tensor subtracted by the
  7974. number before it denotes the number of elements in a given
  7975. row.
  7976. col_indices (array_like): Column coordinates of each element in
  7977. values. (B+1)-dimensional tensor with the same length
  7978. as values.
  7979. values (array_list): Initial values for the tensor. Can be a list,
  7980. tuple, NumPy ``ndarray``, scalar, and other types that
  7981. represents a (1+K)-dimensional tensor where ``K`` is the number
  7982. of dense dimensions.
  7983. size (list, tuple, :class:`torch.Size`, optional): Size of the
  7984. sparse tensor: ``(*batchsize, nrows, ncols, *densesize)``. If
  7985. not provided, the size will be inferred as the minimum size
  7986. big enough to hold all non-zero elements.
  7987. Keyword args:
  7988. dtype (:class:`torch.dtype`, optional): the desired data type of
  7989. returned tensor. Default: if None, infers data type from
  7990. :attr:`values`.
  7991. device (:class:`torch.device`, optional): the desired device of
  7992. returned tensor. Default: if None, uses the current device
  7993. for the default tensor type (see
  7994. :func:`torch.set_default_device`). :attr:`device` will be
  7995. the CPU for CPU tensor types and the current CUDA device for
  7996. CUDA tensor types.
  7997. {pin_memory}
  7998. {requires_grad}
  7999. {check_invariants}
  8000. Example::
  8001. >>> crow_indices = [0, 2, 4]
  8002. >>> col_indices = [0, 1, 0, 1]
  8003. >>> values = [1, 2, 3, 4]
  8004. >>> torch.sparse_csr_tensor(torch.tensor(crow_indices, dtype=torch.int64),
  8005. ... torch.tensor(col_indices, dtype=torch.int64),
  8006. ... torch.tensor(values), dtype=torch.double)
  8007. tensor(crow_indices=tensor([0, 2, 4]),
  8008. col_indices=tensor([0, 1, 0, 1]),
  8009. values=tensor([1., 2., 3., 4.]), size=(2, 2), nnz=4,
  8010. dtype=torch.float64, layout=torch.sparse_csr)
  8011. """.format(**factory_common_args),
  8012. )
  8013. add_docstr(
  8014. torch.sparse_csc_tensor,
  8015. r"""sparse_csc_tensor(ccol_indices, row_indices, values, size=None, """
  8016. r"""*, dtype=None, device=None, pin_memory=False, requires_grad=False, check_invariants=None) -> Tensor
  8017. Constructs a :ref:`sparse tensor in CSC (Compressed Sparse Column)
  8018. <sparse-csc-docs>` with specified values at the given
  8019. :attr:`ccol_indices` and :attr:`row_indices`. Sparse matrix
  8020. multiplication operations in CSC format are typically faster than that
  8021. for sparse tensors in COO format. Make you have a look at :ref:`the
  8022. note on the data type of the indices <sparse-csc-docs>`.
  8023. {sparse_factory_device_note}
  8024. Args:
  8025. ccol_indices (array_like): (B+1)-dimensional array of size
  8026. ``(*batchsize, ncols + 1)``. The last element of each batch
  8027. is the number of non-zeros. This tensor encodes the index in
  8028. values and row_indices depending on where the given column
  8029. starts. Each successive number in the tensor subtracted by the
  8030. number before it denotes the number of elements in a given
  8031. column.
  8032. row_indices (array_like): Row coordinates of each element in
  8033. values. (B+1)-dimensional tensor with the same length as
  8034. values.
  8035. values (array_list): Initial values for the tensor. Can be a list,
  8036. tuple, NumPy ``ndarray``, scalar, and other types that
  8037. represents a (1+K)-dimensional tensor where ``K`` is the number
  8038. of dense dimensions.
  8039. size (list, tuple, :class:`torch.Size`, optional): Size of the
  8040. sparse tensor: ``(*batchsize, nrows, ncols, *densesize)``. If
  8041. not provided, the size will be inferred as the minimum size
  8042. big enough to hold all non-zero elements.
  8043. Keyword args:
  8044. dtype (:class:`torch.dtype`, optional): the desired data type of
  8045. returned tensor. Default: if None, infers data type from
  8046. :attr:`values`.
  8047. device (:class:`torch.device`, optional): the desired device of
  8048. returned tensor. Default: if None, uses the current device
  8049. for the default tensor type (see
  8050. :func:`torch.set_default_device`). :attr:`device` will be
  8051. the CPU for CPU tensor types and the current CUDA device for
  8052. CUDA tensor types.
  8053. {pin_memory}
  8054. {requires_grad}
  8055. {check_invariants}
  8056. Example::
  8057. >>> ccol_indices = [0, 2, 4]
  8058. >>> row_indices = [0, 1, 0, 1]
  8059. >>> values = [1, 2, 3, 4]
  8060. >>> torch.sparse_csc_tensor(torch.tensor(ccol_indices, dtype=torch.int64),
  8061. ... torch.tensor(row_indices, dtype=torch.int64),
  8062. ... torch.tensor(values), dtype=torch.double)
  8063. tensor(ccol_indices=tensor([0, 2, 4]),
  8064. row_indices=tensor([0, 1, 0, 1]),
  8065. values=tensor([1., 2., 3., 4.]), size=(2, 2), nnz=4,
  8066. dtype=torch.float64, layout=torch.sparse_csc)
  8067. """.format(**factory_common_args),
  8068. )
  8069. add_docstr(
  8070. torch.sparse_bsr_tensor,
  8071. r"""sparse_bsr_tensor(crow_indices, col_indices, values, size=None, """
  8072. r"""*, dtype=None, device=None, pin_memory=False, requires_grad=False, check_invariants=None) -> Tensor
  8073. Constructs a :ref:`sparse tensor in BSR (Block Compressed Sparse Row))
  8074. <sparse-bsr-docs>` with specified 2-dimensional blocks at the given
  8075. :attr:`crow_indices` and :attr:`col_indices`. Sparse matrix
  8076. multiplication operations in BSR format are typically faster than that
  8077. for sparse tensors in COO format. Make you have a look at :ref:`the
  8078. note on the data type of the indices <sparse-bsr-docs>`.
  8079. {sparse_factory_device_note}
  8080. Args:
  8081. crow_indices (array_like): (B+1)-dimensional array of size
  8082. ``(*batchsize, nrowblocks + 1)``. The last element of each
  8083. batch is the number of non-zeros. This tensor encodes the
  8084. block index in values and col_indices depending on where the
  8085. given row block starts. Each successive number in the tensor
  8086. subtracted by the number before it denotes the number of
  8087. blocks in a given row.
  8088. col_indices (array_like): Column block coordinates of each block
  8089. in values. (B+1)-dimensional tensor with the same length as
  8090. values.
  8091. values (array_list): Initial values for the tensor. Can be a list,
  8092. tuple, NumPy ``ndarray``, scalar, and other types that
  8093. represents a (1 + 2 + K)-dimensional tensor where ``K`` is the
  8094. number of dense dimensions.
  8095. size (list, tuple, :class:`torch.Size`, optional): Size of the
  8096. sparse tensor: ``(*batchsize, nrows * blocksize[0], ncols *
  8097. blocksize[1], *densesize)`` where ``blocksize ==
  8098. values.shape[1:3]``. If not provided, the size will be
  8099. inferred as the minimum size big enough to hold all non-zero
  8100. blocks.
  8101. Keyword args:
  8102. dtype (:class:`torch.dtype`, optional): the desired data type of
  8103. returned tensor. Default: if None, infers data type from
  8104. :attr:`values`.
  8105. device (:class:`torch.device`, optional): the desired device of
  8106. returned tensor. Default: if None, uses the current device
  8107. for the default tensor type (see
  8108. :func:`torch.set_default_device`). :attr:`device` will be
  8109. the CPU for CPU tensor types and the current CUDA device for
  8110. CUDA tensor types.
  8111. {pin_memory}
  8112. {requires_grad}
  8113. {check_invariants}
  8114. Example::
  8115. >>> crow_indices = [0, 1, 2]
  8116. >>> col_indices = [0, 1]
  8117. >>> values = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
  8118. >>> torch.sparse_bsr_tensor(torch.tensor(crow_indices, dtype=torch.int64),
  8119. ... torch.tensor(col_indices, dtype=torch.int64),
  8120. ... torch.tensor(values), dtype=torch.double)
  8121. tensor(crow_indices=tensor([0, 1, 2]),
  8122. col_indices=tensor([0, 1]),
  8123. values=tensor([[[1., 2.],
  8124. [3., 4.]],
  8125. [[5., 6.],
  8126. [7., 8.]]]), size=(2, 2), nnz=2, dtype=torch.float64,
  8127. layout=torch.sparse_bsr)
  8128. """.format(**factory_common_args),
  8129. )
  8130. add_docstr(
  8131. torch.sparse_bsc_tensor,
  8132. r"""sparse_bsc_tensor(ccol_indices, row_indices, values, size=None, """
  8133. r"""*, dtype=None, device=None, pin_memory=False, requires_grad=False, check_invariants=None) -> Tensor
  8134. Constructs a :ref:`sparse tensor in BSC (Block Compressed Sparse
  8135. Column)) <sparse-bsc-docs>` with specified 2-dimensional blocks at the
  8136. given :attr:`ccol_indices` and :attr:`row_indices`. Sparse matrix
  8137. multiplication operations in BSC format are typically faster than that
  8138. for sparse tensors in COO format. Make you have a look at :ref:`the
  8139. note on the data type of the indices <sparse-bsc-docs>`.
  8140. {sparse_factory_device_note}
  8141. Args:
  8142. ccol_indices (array_like): (B+1)-dimensional array of size
  8143. ``(*batchsize, ncolblocks + 1)``. The last element of each
  8144. batch is the number of non-zeros. This tensor encodes the
  8145. index in values and row_indices depending on where the given
  8146. column starts. Each successive number in the tensor subtracted
  8147. by the number before it denotes the number of elements in a
  8148. given column.
  8149. row_indices (array_like): Row block coordinates of each block in
  8150. values. (B+1)-dimensional tensor with the same length
  8151. as values.
  8152. values (array_list): Initial blocks for the tensor. Can be a list,
  8153. tuple, NumPy ``ndarray``, and other types that
  8154. represents a (1 + 2 + K)-dimensional tensor where ``K`` is the
  8155. number of dense dimensions.
  8156. size (list, tuple, :class:`torch.Size`, optional): Size of the
  8157. sparse tensor: ``(*batchsize, nrows * blocksize[0], ncols *
  8158. blocksize[1], *densesize)`` If not provided, the size will be
  8159. inferred as the minimum size big enough to hold all non-zero
  8160. blocks.
  8161. Keyword args:
  8162. dtype (:class:`torch.dtype`, optional): the desired data type of
  8163. returned tensor. Default: if None, infers data type from
  8164. :attr:`values`.
  8165. device (:class:`torch.device`, optional): the desired device of
  8166. returned tensor. Default: if None, uses the current device
  8167. for the default tensor type (see
  8168. :func:`torch.set_default_device`). :attr:`device` will be
  8169. the CPU for CPU tensor types and the current CUDA device for
  8170. CUDA tensor types.
  8171. {pin_memory}
  8172. {requires_grad}
  8173. {check_invariants}
  8174. Example::
  8175. >>> ccol_indices = [0, 1, 2]
  8176. >>> row_indices = [0, 1]
  8177. >>> values = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
  8178. >>> torch.sparse_bsc_tensor(torch.tensor(ccol_indices, dtype=torch.int64),
  8179. ... torch.tensor(row_indices, dtype=torch.int64),
  8180. ... torch.tensor(values), dtype=torch.double)
  8181. tensor(ccol_indices=tensor([0, 1, 2]),
  8182. row_indices=tensor([0, 1]),
  8183. values=tensor([[[1., 2.],
  8184. [3., 4.]],
  8185. [[5., 6.],
  8186. [7., 8.]]]), size=(2, 2), nnz=2, dtype=torch.float64,
  8187. layout=torch.sparse_bsc)
  8188. """.format(**factory_common_args),
  8189. )
  8190. add_docstr(
  8191. torch.sparse_coo_tensor,
  8192. r"""sparse_coo_tensor(indices, values, size=None, """
  8193. r"""*, dtype=None, device=None, pin_memory=False, requires_grad=False, check_invariants=None, is_coalesced=None) -> Tensor
  8194. Constructs a :ref:`sparse tensor in COO(rdinate) format
  8195. <sparse-coo-docs>` with specified values at the given
  8196. :attr:`indices`.
  8197. .. note::
  8198. This function returns an :ref:`uncoalesced tensor
  8199. <sparse-uncoalesced-coo-docs>` when :attr:`is_coalesced` is
  8200. unspecified or ``None``.
  8201. {sparse_factory_device_note}
  8202. Args:
  8203. indices (array_like): Initial data for the tensor. Can be a list, tuple,
  8204. NumPy ``ndarray``, scalar, and other types. Will be cast to a :class:`torch.LongTensor`
  8205. internally. The indices are the coordinates of the non-zero values in the matrix, and thus
  8206. should be two-dimensional where the first dimension is the number of tensor dimensions and
  8207. the second dimension is the number of non-zero values.
  8208. values (array_like): Initial values for the tensor. Can be a list, tuple,
  8209. NumPy ``ndarray``, scalar, and other types.
  8210. size (list, tuple, or :class:`torch.Size`, optional): Size of the sparse tensor. If not
  8211. provided the size will be inferred as the minimum size big enough to hold all non-zero
  8212. elements.
  8213. Keyword args:
  8214. dtype (:class:`torch.dtype`, optional): the desired data type of returned tensor.
  8215. Default: if None, infers data type from :attr:`values`.
  8216. device (:class:`torch.device`, optional): the desired device of returned tensor.
  8217. Default: if None, uses the current device for the default tensor type
  8218. (see :func:`torch.set_default_device`). :attr:`device` will be the CPU
  8219. for CPU tensor types and the current CUDA device for CUDA tensor types.
  8220. {pin_memory}
  8221. {requires_grad}
  8222. {check_invariants}
  8223. is_coalesced (bool, optional): When``True``, the caller is
  8224. responsible for providing tensor indices that correspond to a
  8225. coalesced tensor. If the :attr:`check_invariants` flag is
  8226. False, no error will be raised if the prerequisites are not
  8227. met and this will lead to silently incorrect results. To force
  8228. coalescion please use :meth:`coalesce` on the resulting
  8229. Tensor.
  8230. Default: None: except for trivial cases (e.g. nnz < 2) the
  8231. resulting Tensor has is_coalesced set to ``False```.
  8232. Example::
  8233. >>> i = torch.tensor([[0, 1, 1],
  8234. ... [2, 0, 2]])
  8235. >>> v = torch.tensor([3, 4, 5], dtype=torch.float32)
  8236. >>> torch.sparse_coo_tensor(i, v, [2, 4])
  8237. tensor(indices=tensor([[0, 1, 1],
  8238. [2, 0, 2]]),
  8239. values=tensor([3., 4., 5.]),
  8240. size=(2, 4), nnz=3, layout=torch.sparse_coo)
  8241. >>> torch.sparse_coo_tensor(i, v) # Shape inference
  8242. tensor(indices=tensor([[0, 1, 1],
  8243. [2, 0, 2]]),
  8244. values=tensor([3., 4., 5.]),
  8245. size=(2, 3), nnz=3, layout=torch.sparse_coo)
  8246. >>> torch.sparse_coo_tensor(i, v, [2, 4],
  8247. ... dtype=torch.float64,
  8248. ... device=torch.device('cuda:0'))
  8249. tensor(indices=tensor([[0, 1, 1],
  8250. [2, 0, 2]]),
  8251. values=tensor([3., 4., 5.]),
  8252. device='cuda:0', size=(2, 4), nnz=3, dtype=torch.float64,
  8253. layout=torch.sparse_coo)
  8254. # Create an empty sparse tensor with the following invariants:
  8255. # 1. sparse_dim + dense_dim = len(SparseTensor.shape)
  8256. # 2. SparseTensor._indices().shape = (sparse_dim, nnz)
  8257. # 3. SparseTensor._values().shape = (nnz, SparseTensor.shape[sparse_dim:])
  8258. #
  8259. # For instance, to create an empty sparse tensor with nnz = 0, dense_dim = 0 and
  8260. # sparse_dim = 1 (hence indices is a 2D tensor of shape = (1, 0))
  8261. >>> S = torch.sparse_coo_tensor(torch.empty([1, 0]), [], [1])
  8262. tensor(indices=tensor([], size=(1, 0)),
  8263. values=tensor([], size=(0,)),
  8264. size=(1,), nnz=0, layout=torch.sparse_coo)
  8265. # and to create an empty sparse tensor with nnz = 0, dense_dim = 1 and
  8266. # sparse_dim = 1
  8267. >>> S = torch.sparse_coo_tensor(torch.empty([1, 0]), torch.empty([0, 2]), [1, 2])
  8268. tensor(indices=tensor([], size=(1, 0)),
  8269. values=tensor([], size=(0, 2)),
  8270. size=(1, 2), nnz=0, layout=torch.sparse_coo)
  8271. .. _torch.sparse: https://pytorch.org/docs/stable/sparse.html
  8272. """.format(**factory_common_args),
  8273. )
  8274. add_docstr(
  8275. torch.sqrt,
  8276. r"""
  8277. sqrt(input, *, out=None) -> Tensor
  8278. Returns a new tensor with the square-root of the elements of :attr:`input`.
  8279. .. math::
  8280. \text{out}_{i} = \sqrt{\text{input}_{i}}
  8281. """
  8282. + r"""
  8283. Args:
  8284. {input}
  8285. Keyword args:
  8286. {out}
  8287. Example::
  8288. >>> a = torch.randn(4)
  8289. >>> a
  8290. tensor([-2.0755, 1.0226, 0.0831, 0.4806])
  8291. >>> torch.sqrt(a)
  8292. tensor([ nan, 1.0112, 0.2883, 0.6933])
  8293. """.format(**common_args),
  8294. )
  8295. add_docstr(
  8296. torch.square,
  8297. r"""
  8298. square(input: Tensor, *, out: Optional[Tensor]) -> Tensor
  8299. Returns a new tensor with the square of the elements of :attr:`input`.
  8300. Args:
  8301. {input}
  8302. Keyword args:
  8303. {out}
  8304. Example::
  8305. >>> a = torch.randn(4)
  8306. >>> a
  8307. tensor([-2.0755, 1.0226, 0.0831, 0.4806])
  8308. >>> torch.square(a)
  8309. tensor([ 4.3077, 1.0457, 0.0069, 0.2310])
  8310. """.format(**common_args),
  8311. )
  8312. add_docstr(
  8313. torch.squeeze,
  8314. r"""
  8315. squeeze(input: Tensor, dim: Optional[Union[int, List[int]]]) -> Tensor
  8316. Returns a tensor with all specified dimensions of :attr:`input` of size `1` removed.
  8317. For example, if `input` is of shape:
  8318. :math:`(A \times 1 \times B \times C \times 1 \times D)` then the `input.squeeze()`
  8319. will be of shape: :math:`(A \times B \times C \times D)`.
  8320. When :attr:`dim` is given, a squeeze operation is done only in the given
  8321. dimension(s). If `input` is of shape: :math:`(A \times 1 \times B)`,
  8322. ``squeeze(input, 0)`` leaves the tensor unchanged, but ``squeeze(input, 1)``
  8323. will squeeze the tensor to the shape :math:`(A \times B)`.
  8324. .. note:: The returned tensor shares the storage with the input tensor,
  8325. so changing the contents of one will change the contents of the other.
  8326. .. warning:: If the tensor has a batch dimension of size 1, then `squeeze(input)`
  8327. will also remove the batch dimension, which can lead to unexpected
  8328. errors. Consider specifying only the dims you wish to be squeezed.
  8329. Args:
  8330. {input}
  8331. dim (int or tuple of ints, optional): if given, the input will be squeezed
  8332. only in the specified dimensions.
  8333. .. versionchanged:: 2.0
  8334. :attr:`dim` now accepts tuples of dimensions.
  8335. Example::
  8336. >>> x = torch.zeros(2, 1, 2, 1, 2)
  8337. >>> x.size()
  8338. torch.Size([2, 1, 2, 1, 2])
  8339. >>> y = torch.squeeze(x)
  8340. >>> y.size()
  8341. torch.Size([2, 2, 2])
  8342. >>> y = torch.squeeze(x, 0)
  8343. >>> y.size()
  8344. torch.Size([2, 1, 2, 1, 2])
  8345. >>> y = torch.squeeze(x, 1)
  8346. >>> y.size()
  8347. torch.Size([2, 2, 1, 2])
  8348. >>> y = torch.squeeze(x, (1, 2, 3))
  8349. torch.Size([2, 2, 2])
  8350. """.format(**common_args),
  8351. )
  8352. add_docstr(
  8353. torch.std,
  8354. r"""
  8355. std(input, dim=None, *, correction=1, keepdim=False, out=None) -> Tensor
  8356. Calculates the standard deviation over the dimensions specified by :attr:`dim`.
  8357. :attr:`dim` can be a single dimension, list of dimensions, or ``None`` to
  8358. reduce over all dimensions.
  8359. The standard deviation (:math:`\sigma`) is calculated as
  8360. .. math:: \sigma = \sqrt{\frac{1}{\max(0,~N - \delta N)}\sum_{i=0}^{N-1}(x_i-\bar{x})^2}
  8361. where :math:`x` is the sample set of elements, :math:`\bar{x}` is the
  8362. sample mean, :math:`N` is the number of samples and :math:`\delta N` is
  8363. the :attr:`correction`.
  8364. """
  8365. + r"""
  8366. {keepdim_details}
  8367. Args:
  8368. {input}
  8369. {opt_dim_all_reduce}
  8370. Keyword args:
  8371. correction (int): difference between the sample size and sample degrees of freedom.
  8372. Defaults to `Bessel's correction`_, ``correction=1``.
  8373. .. versionchanged:: 2.0
  8374. Previously this argument was called ``unbiased`` and was a boolean
  8375. with ``True`` corresponding to ``correction=1`` and ``False`` being
  8376. ``correction=0``.
  8377. {opt_keepdim}
  8378. {out}
  8379. Example:
  8380. >>> a = torch.tensor(
  8381. ... [[ 0.2035, 1.2959, 1.8101, -0.4644],
  8382. ... [ 1.5027, -0.3270, 0.5905, 0.6538],
  8383. ... [-1.5745, 1.3330, -0.5596, -0.6548],
  8384. ... [ 0.1264, -0.5080, 1.6420, 0.1992]]
  8385. ... ) # fmt: skip
  8386. >>> torch.std(a, dim=1, keepdim=True)
  8387. tensor([[1.0311],
  8388. [0.7477],
  8389. [1.2204],
  8390. [0.9087]])
  8391. .. _Bessel's correction: https://en.wikipedia.org/wiki/Bessel%27s_correction
  8392. """.format(**multi_dim_common),
  8393. )
  8394. add_docstr(
  8395. torch.std_mean,
  8396. r"""
  8397. std_mean(input, dim=None, *, correction=1, keepdim=False, out=None) -> (Tensor, Tensor)
  8398. Calculates the standard deviation and mean over the dimensions specified by
  8399. :attr:`dim`. :attr:`dim` can be a single dimension, list of dimensions, or
  8400. ``None`` to reduce over all dimensions.
  8401. The standard deviation (:math:`\sigma`) is calculated as
  8402. .. math:: \sigma = \sqrt{\frac{1}{\max(0,~N - \delta N)}\sum_{i=0}^{N-1}(x_i-\bar{x})^2}
  8403. where :math:`x` is the sample set of elements, :math:`\bar{x}` is the
  8404. sample mean, :math:`N` is the number of samples and :math:`\delta N` is
  8405. the :attr:`correction`.
  8406. """
  8407. + r"""
  8408. {keepdim_details}
  8409. Args:
  8410. {input}
  8411. {opt_dim_all_reduce}
  8412. Keyword args:
  8413. correction (int): difference between the sample size and sample degrees of freedom.
  8414. Defaults to `Bessel's correction`_, ``correction=1``.
  8415. .. versionchanged:: 2.0
  8416. Previously this argument was called ``unbiased`` and was a boolean
  8417. with ``True`` corresponding to ``correction=1`` and ``False`` being
  8418. ``correction=0``.
  8419. {opt_keepdim}
  8420. {out}
  8421. Returns:
  8422. A tuple (std, mean) containing the standard deviation and mean.
  8423. Example:
  8424. >>> a = torch.tensor(
  8425. ... [[ 0.2035, 1.2959, 1.8101, -0.4644],
  8426. ... [ 1.5027, -0.3270, 0.5905, 0.6538],
  8427. ... [-1.5745, 1.3330, -0.5596, -0.6548],
  8428. ... [ 0.1264, -0.5080, 1.6420, 0.1992]]
  8429. ... ) # fmt: skip
  8430. >>> torch.std_mean(a, dim=0, keepdim=True)
  8431. (tensor([[1.2620, 1.0028, 1.0957, 0.6038]]),
  8432. tensor([[ 0.0645, 0.4485, 0.8707, -0.0665]]))
  8433. .. _Bessel's correction: https://en.wikipedia.org/wiki/Bessel%27s_correction
  8434. """.format(**multi_dim_common),
  8435. )
  8436. add_docstr(
  8437. torch.sub,
  8438. r"""
  8439. sub(input, other, *, alpha=1, out=None) -> Tensor
  8440. Subtracts :attr:`other`, scaled by :attr:`alpha`, from :attr:`input`.
  8441. .. math::
  8442. \text{{out}}_i = \text{{input}}_i - \text{{alpha}} \times \text{{other}}_i
  8443. """
  8444. + r"""
  8445. Supports :ref:`broadcasting to a common shape <broadcasting-semantics>`,
  8446. :ref:`type promotion <type-promotion-doc>`, and integer, float, and complex inputs.
  8447. Args:
  8448. {input}
  8449. other (Tensor or Number): the tensor or number to subtract from :attr:`input`.
  8450. Keyword args:
  8451. alpha (Number): the multiplier for :attr:`other`.
  8452. {out}
  8453. Example::
  8454. >>> a = torch.tensor((1, 2))
  8455. >>> b = torch.tensor((0, 1))
  8456. >>> torch.sub(a, b, alpha=2)
  8457. tensor([1, 0])
  8458. """.format(**common_args),
  8459. )
  8460. add_docstr(
  8461. torch.subtract,
  8462. r"""
  8463. subtract(input, other, *, alpha=1, out=None) -> Tensor
  8464. Alias for :func:`torch.sub`.
  8465. """,
  8466. )
  8467. add_docstr(
  8468. torch.sum,
  8469. r"""
  8470. sum(input, *, dtype=None) -> Tensor
  8471. Returns the sum of all elements in the :attr:`input` tensor.
  8472. Args:
  8473. {input}
  8474. Keyword args:
  8475. {dtype}
  8476. .. note:: Use the `dtype` argument if you need the result in a specific tensor type.
  8477. Otherwise, the result type may be automatically promoted (e.g., from `torch.int32` to `torch.int64`).
  8478. Example::
  8479. >>> a = torch.randn(1, 3)
  8480. >>> a
  8481. tensor([[ 0.1133, -0.9567, 0.2958]])
  8482. >>> torch.sum(a)
  8483. tensor(-0.5475)
  8484. .. function:: sum(input, dim, keepdim=False, *, dtype=None) -> Tensor
  8485. :noindex:
  8486. Returns the sum of each row of the :attr:`input` tensor in the given
  8487. dimension :attr:`dim`. If :attr:`dim` is a list of dimensions,
  8488. reduce over all of them.
  8489. {keepdim_details}
  8490. Args:
  8491. {input}
  8492. {opt_dim_all_reduce}
  8493. {opt_keepdim}
  8494. Keyword args:
  8495. {dtype}
  8496. Example::
  8497. >>> a = torch.randn(4, 4)
  8498. >>> a
  8499. tensor([[ 0.0569, -0.2475, 0.0737, -0.3429],
  8500. [-0.2993, 0.9138, 0.9337, -1.6864],
  8501. [ 0.1132, 0.7892, -0.1003, 0.5688],
  8502. [ 0.3637, -0.9906, -0.4752, -1.5197]])
  8503. >>> torch.sum(a, 1)
  8504. tensor([-0.4598, -0.1381, 1.3708, -2.6217])
  8505. >>> b = torch.arange(4 * 5 * 6).view(4, 5, 6)
  8506. >>> torch.sum(b, (2, 1))
  8507. tensor([ 435., 1335., 2235., 3135.])
  8508. """.format(**multi_dim_common),
  8509. )
  8510. add_docstr(
  8511. torch.nansum,
  8512. r"""
  8513. nansum(input, *, dtype=None) -> Tensor
  8514. Returns the sum of all elements, treating Not a Numbers (NaNs) as zero.
  8515. Args:
  8516. {input}
  8517. Keyword args:
  8518. {dtype}
  8519. Example::
  8520. >>> a = torch.tensor([1., 2., float('nan'), 4.])
  8521. >>> torch.nansum(a)
  8522. tensor(7.)
  8523. .. function:: nansum(input, dim, keepdim=False, *, dtype=None) -> Tensor
  8524. :noindex:
  8525. Returns the sum of each row of the :attr:`input` tensor in the given
  8526. dimension :attr:`dim`, treating Not a Numbers (NaNs) as zero.
  8527. If :attr:`dim` is a list of dimensions, reduce over all of them.
  8528. {keepdim_details}
  8529. Args:
  8530. {input}
  8531. {opt_dim_all_reduce}
  8532. {opt_keepdim}
  8533. Keyword args:
  8534. {dtype}
  8535. Example::
  8536. >>> torch.nansum(torch.tensor([1., float("nan")]))
  8537. tensor(1.)
  8538. >>> a = torch.tensor([[1, 2], [3., float("nan")]])
  8539. >>> torch.nansum(a)
  8540. tensor(6.)
  8541. >>> torch.nansum(a, dim=0)
  8542. tensor([4., 2.])
  8543. >>> torch.nansum(a, dim=1)
  8544. tensor([3., 3.])
  8545. """.format(**multi_dim_common),
  8546. )
  8547. add_docstr(
  8548. torch.svd,
  8549. r"""
  8550. svd(input, some=True, compute_uv=True, *, out=None) -> (Tensor, Tensor, Tensor)
  8551. Computes the singular value decomposition of either a matrix or batch of
  8552. matrices :attr:`input`. The singular value decomposition is represented as a
  8553. namedtuple `(U, S, V)`, such that :attr:`input` :math:`= U \text{diag}(S) V^{\text{H}}`.
  8554. where :math:`V^{\text{H}}` is the transpose of `V` for real inputs,
  8555. and the conjugate transpose of `V` for complex inputs.
  8556. If :attr:`input` is a batch of matrices, then `U`, `S`, and `V` are also
  8557. batched with the same batch dimensions as :attr:`input`.
  8558. If :attr:`some` is `True` (default), the method returns the reduced singular
  8559. value decomposition. In this case, if the last two dimensions of :attr:`input` are
  8560. `m` and `n`, then the returned `U` and `V` matrices will contain only
  8561. `min(n, m)` orthonormal columns.
  8562. If :attr:`compute_uv` is `False`, the returned `U` and `V` will be
  8563. zero-filled matrices of shape `(m, m)` and `(n, n)`
  8564. respectively, and the same device as :attr:`input`. The argument :attr:`some`
  8565. has no effect when :attr:`compute_uv` is `False`.
  8566. Supports :attr:`input` of float, double, cfloat and cdouble data types.
  8567. The dtypes of `U` and `V` are the same as :attr:`input`'s. `S` will
  8568. always be real-valued, even if :attr:`input` is complex.
  8569. .. warning::
  8570. :func:`torch.svd` is deprecated in favor of :func:`torch.linalg.svd`
  8571. and will be removed in a future PyTorch release.
  8572. ``U, S, V = torch.svd(A, some=some, compute_uv=True)`` (default) should be replaced with
  8573. .. code:: python
  8574. U, S, Vh = torch.linalg.svd(A, full_matrices=not some)
  8575. V = Vh.mH
  8576. ``_, S, _ = torch.svd(A, some=some, compute_uv=False)`` should be replaced with
  8577. .. code:: python
  8578. S = torch.linalg.svdvals(A)
  8579. .. note:: Differences with :func:`torch.linalg.svd`:
  8580. * :attr:`some` is the opposite of
  8581. :func:`torch.linalg.svd`'s :attr:`full_matrices`. Note that
  8582. default value for both is `True`, so the default behavior is
  8583. effectively the opposite.
  8584. * :func:`torch.svd` returns `V`, whereas :func:`torch.linalg.svd` returns
  8585. `Vh`, that is, :math:`V^{\text{H}}`.
  8586. * If :attr:`compute_uv` is `False`, :func:`torch.svd` returns zero-filled
  8587. tensors for `U` and `Vh`, whereas :func:`torch.linalg.svd` returns
  8588. empty tensors.
  8589. .. note:: The singular values are returned in descending order. If :attr:`input` is a batch of matrices,
  8590. then the singular values of each matrix in the batch are returned in descending order.
  8591. .. note:: The `S` tensor can only be used to compute gradients if :attr:`compute_uv` is `True`.
  8592. .. note:: When :attr:`some` is `False`, the gradients on `U[..., :, min(m, n):]`
  8593. and `V[..., :, min(m, n):]` will be ignored in the backward pass, as those vectors
  8594. can be arbitrary bases of the corresponding subspaces.
  8595. .. note:: The implementation of :func:`torch.linalg.svd` on CPU uses LAPACK's routine `?gesdd`
  8596. (a divide-and-conquer algorithm) instead of `?gesvd` for speed. Analogously,
  8597. on GPU, it uses cuSOLVER's routines `gesvdj` and `gesvdjBatched` on CUDA 10.1.243
  8598. and later, and MAGMA's routine `gesdd` on earlier versions of CUDA.
  8599. .. note:: The returned `U` will not be contiguous. The matrix (or batch of matrices) will
  8600. be represented as a column-major matrix (i.e. Fortran-contiguous).
  8601. .. warning:: The gradients with respect to `U` and `V` will only be finite when the input does not
  8602. have zero nor repeated singular values.
  8603. .. warning:: If the distance between any two singular values is close to zero, the gradients with respect to
  8604. `U` and `V` will be numerically unstable, as they depends on
  8605. :math:`\frac{1}{\min_{i \neq j} \sigma_i^2 - \sigma_j^2}`. The same happens when the matrix
  8606. has small singular values, as these gradients also depend on `S^{-1}`.
  8607. .. warning:: For complex-valued :attr:`input` the singular value decomposition is not unique,
  8608. as `U` and `V` may be multiplied by an arbitrary phase factor :math:`e^{i \phi}` on every column.
  8609. The same happens when :attr:`input` has repeated singular values, where one may multiply
  8610. the columns of the spanning subspace in `U` and `V` by a rotation matrix
  8611. and `the resulting vectors will span the same subspace`_.
  8612. Different platforms, like NumPy, or inputs on different device types,
  8613. may produce different `U` and `V` tensors.
  8614. Args:
  8615. input (Tensor): the input tensor of size `(*, m, n)` where `*` is zero or more
  8616. batch dimensions consisting of `(m, n)` matrices.
  8617. some (bool, optional): controls whether to compute the reduced or full decomposition, and
  8618. consequently, the shape of returned `U` and `V`. Default: `True`.
  8619. compute_uv (bool, optional): controls whether to compute `U` and `V`. Default: `True`.
  8620. Keyword args:
  8621. out (tuple, optional): the output tuple of tensors
  8622. Example::
  8623. >>> a = torch.randn(5, 3)
  8624. >>> a
  8625. tensor([[ 0.2364, -0.7752, 0.6372],
  8626. [ 1.7201, 0.7394, -0.0504],
  8627. [-0.3371, -1.0584, 0.5296],
  8628. [ 0.3550, -0.4022, 1.5569],
  8629. [ 0.2445, -0.0158, 1.1414]])
  8630. >>> u, s, v = torch.svd(a)
  8631. >>> u
  8632. tensor([[ 0.4027, 0.0287, 0.5434],
  8633. [-0.1946, 0.8833, 0.3679],
  8634. [ 0.4296, -0.2890, 0.5261],
  8635. [ 0.6604, 0.2717, -0.2618],
  8636. [ 0.4234, 0.2481, -0.4733]])
  8637. >>> s
  8638. tensor([2.3289, 2.0315, 0.7806])
  8639. >>> v
  8640. tensor([[-0.0199, 0.8766, 0.4809],
  8641. [-0.5080, 0.4054, -0.7600],
  8642. [ 0.8611, 0.2594, -0.4373]])
  8643. >>> torch.dist(a, torch.mm(torch.mm(u, torch.diag(s)), v.t()))
  8644. tensor(8.6531e-07)
  8645. >>> a_big = torch.randn(7, 5, 3)
  8646. >>> u, s, v = torch.svd(a_big)
  8647. >>> torch.dist(a_big, torch.matmul(torch.matmul(u, torch.diag_embed(s)), v.mT))
  8648. tensor(2.6503e-06)
  8649. .. _the resulting vectors will span the same subspace:
  8650. (https://en.wikipedia.org/wiki/Singular_value_decomposition#Singular_values,_singular_vectors,_and_their_relation_to_the_SVD)
  8651. """,
  8652. )
  8653. add_docstr(
  8654. torch.t,
  8655. r"""
  8656. t(input) -> Tensor
  8657. Expects :attr:`input` to be <= 2-D tensor and transposes dimensions 0
  8658. and 1.
  8659. 0-D and 1-D tensors are returned as is. When input is a 2-D tensor this
  8660. is equivalent to ``transpose(input, 0, 1)``.
  8661. Args:
  8662. {input}
  8663. Example::
  8664. >>> x = torch.randn(())
  8665. >>> x
  8666. tensor(0.1995)
  8667. >>> torch.t(x)
  8668. tensor(0.1995)
  8669. >>> x = torch.randn(3)
  8670. >>> x
  8671. tensor([ 2.4320, -0.4608, 0.7702])
  8672. >>> torch.t(x)
  8673. tensor([ 2.4320, -0.4608, 0.7702])
  8674. >>> x = torch.randn(2, 3)
  8675. >>> x
  8676. tensor([[ 0.4875, 0.9158, -0.5872],
  8677. [ 0.3938, -0.6929, 0.6932]])
  8678. >>> torch.t(x)
  8679. tensor([[ 0.4875, 0.3938],
  8680. [ 0.9158, -0.6929],
  8681. [-0.5872, 0.6932]])
  8682. See also :func:`torch.transpose`.
  8683. """.format(**common_args),
  8684. )
  8685. add_docstr(
  8686. torch.flip,
  8687. r"""
  8688. flip(input, dims) -> Tensor
  8689. Reverse the order of an n-D tensor along given axis in dims.
  8690. .. note::
  8691. `torch.flip` makes a copy of :attr:`input`'s data. This is different from NumPy's `np.flip`,
  8692. which returns a view in constant time. Since copying a tensor's data is more work than viewing that data,
  8693. `torch.flip` is expected to be slower than `np.flip`.
  8694. Args:
  8695. {input}
  8696. dims (a list or tuple): axis to flip on
  8697. Example::
  8698. >>> x = torch.arange(8).view(2, 2, 2)
  8699. >>> x
  8700. tensor([[[ 0, 1],
  8701. [ 2, 3]],
  8702. [[ 4, 5],
  8703. [ 6, 7]]])
  8704. >>> torch.flip(x, [0, 1])
  8705. tensor([[[ 6, 7],
  8706. [ 4, 5]],
  8707. [[ 2, 3],
  8708. [ 0, 1]]])
  8709. """.format(**common_args),
  8710. )
  8711. add_docstr(
  8712. torch.fliplr,
  8713. r"""
  8714. fliplr(input) -> Tensor
  8715. Flip tensor in the left/right direction, returning a new tensor.
  8716. Flip the entries in each row in the left/right direction.
  8717. Columns are preserved, but appear in a different order than before.
  8718. Note:
  8719. Requires the tensor to be at least 2-D.
  8720. .. note::
  8721. `torch.fliplr` makes a copy of :attr:`input`'s data. This is different from NumPy's `np.fliplr`,
  8722. which returns a view in constant time. Since copying a tensor's data is more work than viewing that data,
  8723. `torch.fliplr` is expected to be slower than `np.fliplr`.
  8724. Args:
  8725. input (Tensor): Must be at least 2-dimensional.
  8726. Example::
  8727. >>> x = torch.arange(4).view(2, 2)
  8728. >>> x
  8729. tensor([[0, 1],
  8730. [2, 3]])
  8731. >>> torch.fliplr(x)
  8732. tensor([[1, 0],
  8733. [3, 2]])
  8734. """.format(**common_args),
  8735. )
  8736. add_docstr(
  8737. torch.flipud,
  8738. r"""
  8739. flipud(input) -> Tensor
  8740. Flip tensor in the up/down direction, returning a new tensor.
  8741. Flip the entries in each column in the up/down direction.
  8742. Rows are preserved, but appear in a different order than before.
  8743. Note:
  8744. Requires the tensor to be at least 1-D.
  8745. .. note::
  8746. `torch.flipud` makes a copy of :attr:`input`'s data. This is different from NumPy's `np.flipud`,
  8747. which returns a view in constant time. Since copying a tensor's data is more work than viewing that data,
  8748. `torch.flipud` is expected to be slower than `np.flipud`.
  8749. Args:
  8750. input (Tensor): Must be at least 1-dimensional.
  8751. Example::
  8752. >>> x = torch.arange(4).view(2, 2)
  8753. >>> x
  8754. tensor([[0, 1],
  8755. [2, 3]])
  8756. >>> torch.flipud(x)
  8757. tensor([[2, 3],
  8758. [0, 1]])
  8759. """.format(**common_args),
  8760. )
  8761. add_docstr(
  8762. torch.roll,
  8763. r"""
  8764. roll(input, shifts, dims=None) -> Tensor
  8765. Roll the tensor :attr:`input` along the given dimension(s). Elements that are
  8766. shifted beyond the last position are re-introduced at the first position. If
  8767. :attr:`dims` is `None`, the tensor will be flattened before rolling and then
  8768. restored to the original shape.
  8769. Args:
  8770. {input}
  8771. shifts (int or tuple of ints): The number of places by which the elements
  8772. of the tensor are shifted. If shifts is a tuple, dims must be a tuple of
  8773. the same size, and each dimension will be rolled by the corresponding
  8774. value
  8775. dims (int or tuple of ints): Axis along which to roll
  8776. Example::
  8777. >>> x = torch.tensor([1, 2, 3, 4, 5, 6, 7, 8]).view(4, 2)
  8778. >>> x
  8779. tensor([[1, 2],
  8780. [3, 4],
  8781. [5, 6],
  8782. [7, 8]])
  8783. >>> torch.roll(x, 1)
  8784. tensor([[8, 1],
  8785. [2, 3],
  8786. [4, 5],
  8787. [6, 7]])
  8788. >>> torch.roll(x, 1, 0)
  8789. tensor([[7, 8],
  8790. [1, 2],
  8791. [3, 4],
  8792. [5, 6]])
  8793. >>> torch.roll(x, -1, 0)
  8794. tensor([[3, 4],
  8795. [5, 6],
  8796. [7, 8],
  8797. [1, 2]])
  8798. >>> torch.roll(x, shifts=(2, 1), dims=(0, 1))
  8799. tensor([[6, 5],
  8800. [8, 7],
  8801. [2, 1],
  8802. [4, 3]])
  8803. """.format(**common_args),
  8804. )
  8805. add_docstr(
  8806. torch.rot90,
  8807. r"""
  8808. rot90(input, k=1, dims=(0, 1)) -> Tensor
  8809. Rotate an n-D tensor by 90 degrees in the plane specified by dims axis.
  8810. Rotation direction is from the first towards the second axis if k > 0, and from the second towards the first for k < 0.
  8811. Args:
  8812. {input}
  8813. k (int): number of times to rotate. Default value is 1
  8814. dims (a list or tuple): axis to rotate. Default value is [0, 1]
  8815. Example::
  8816. >>> x = torch.arange(4).view(2, 2)
  8817. >>> x
  8818. tensor([[0, 1],
  8819. [2, 3]])
  8820. >>> torch.rot90(x, 1, [0, 1])
  8821. tensor([[1, 3],
  8822. [0, 2]])
  8823. >>> x = torch.arange(8).view(2, 2, 2)
  8824. >>> x
  8825. tensor([[[0, 1],
  8826. [2, 3]],
  8827. [[4, 5],
  8828. [6, 7]]])
  8829. >>> torch.rot90(x, 1, [1, 2])
  8830. tensor([[[1, 3],
  8831. [0, 2]],
  8832. [[5, 7],
  8833. [4, 6]]])
  8834. """.format(**common_args),
  8835. )
  8836. add_docstr(
  8837. torch.take,
  8838. r"""
  8839. take(input, index) -> Tensor
  8840. Returns a new tensor with the elements of :attr:`input` at the given indices.
  8841. The input tensor is treated as if it were viewed as a 1-D tensor. The result
  8842. takes the same shape as the indices.
  8843. Args:
  8844. {input}
  8845. index (LongTensor): the indices into tensor
  8846. Example::
  8847. >>> src = torch.tensor([[4, 3, 5],
  8848. ... [6, 7, 8]])
  8849. >>> torch.take(src, torch.tensor([0, 2, 5]))
  8850. tensor([ 4, 5, 8])
  8851. """.format(**common_args),
  8852. )
  8853. add_docstr(
  8854. torch.take_along_dim,
  8855. r"""
  8856. take_along_dim(input, indices, dim=None, *, out=None) -> Tensor
  8857. Selects values from :attr:`input` at the 1-dimensional indices from :attr:`indices` along the given :attr:`dim`.
  8858. If :attr:`dim` is None, the input array is treated as if it has been flattened to 1d.
  8859. Functions that return indices along a dimension, like :func:`torch.argmax` and :func:`torch.argsort`,
  8860. are designed to work with this function. See the examples below.
  8861. .. note::
  8862. This function is similar to NumPy's `take_along_axis`.
  8863. See also :func:`torch.gather`.
  8864. Args:
  8865. {input}
  8866. indices (LongTensor): the indices into :attr:`input`. Must have long dtype.
  8867. dim (int, optional): dimension to select along. Default: 0
  8868. Keyword args:
  8869. {out}
  8870. Example::
  8871. >>> t = torch.tensor([[10, 30, 20], [60, 40, 50]])
  8872. >>> max_idx = torch.argmax(t)
  8873. >>> torch.take_along_dim(t, max_idx)
  8874. tensor([60])
  8875. >>> sorted_idx = torch.argsort(t, dim=1)
  8876. >>> torch.take_along_dim(t, sorted_idx, dim=1)
  8877. tensor([[10, 20, 30],
  8878. [40, 50, 60]])
  8879. """.format(**common_args),
  8880. )
  8881. add_docstr(
  8882. torch.tan,
  8883. r"""
  8884. tan(input, *, out=None) -> Tensor
  8885. Returns a new tensor with the tangent of the elements of :attr:`input`.
  8886. .. math::
  8887. \text{out}_{i} = \tan(\text{input}_{i})
  8888. """
  8889. + r"""
  8890. Args:
  8891. {input}
  8892. Keyword args:
  8893. {out}
  8894. Example::
  8895. >>> a = torch.randn(4)
  8896. >>> a
  8897. tensor([-1.2027, -1.7687, 0.4412, -1.3856])
  8898. >>> torch.tan(a)
  8899. tensor([-2.5930, 4.9859, 0.4722, -5.3366])
  8900. """.format(**common_args),
  8901. )
  8902. add_docstr(
  8903. torch.tanh,
  8904. r"""
  8905. tanh(input, *, out=None) -> Tensor
  8906. Returns a new tensor with the hyperbolic tangent of the elements
  8907. of :attr:`input`.
  8908. .. math::
  8909. \text{out}_{i} = \tanh(\text{input}_{i})
  8910. """
  8911. + r"""
  8912. Args:
  8913. {input}
  8914. Keyword args:
  8915. {out}
  8916. Example::
  8917. >>> a = torch.randn(4)
  8918. >>> a
  8919. tensor([ 0.8986, -0.7279, 1.1745, 0.2611])
  8920. >>> torch.tanh(a)
  8921. tensor([ 0.7156, -0.6218, 0.8257, 0.2553])
  8922. """.format(**common_args),
  8923. )
  8924. add_docstr(
  8925. # torch.softmax doc str. Point this to torch.nn.functional.softmax
  8926. torch.softmax,
  8927. r"""
  8928. softmax(input, dim, *, dtype=None) -> Tensor
  8929. Alias for :func:`torch.nn.functional.softmax`.
  8930. """,
  8931. )
  8932. add_docstr(
  8933. torch.topk,
  8934. r"""
  8935. topk(input, k, dim=None, largest=True, sorted=True, *, out=None) -> (Tensor, LongTensor)
  8936. Returns the :attr:`k` largest elements of the given :attr:`input` tensor along
  8937. a given dimension.
  8938. If :attr:`dim` is not given, the last dimension of the `input` is chosen.
  8939. If :attr:`largest` is ``False`` then the `k` smallest elements are returned.
  8940. A namedtuple of `(values, indices)` is returned with the `values` and
  8941. `indices` of the largest `k` elements of each row of the `input` tensor in the
  8942. given dimension `dim`.
  8943. The boolean option :attr:`sorted` if ``True``, will make sure that the returned
  8944. `k` elements are themselves sorted
  8945. .. note::
  8946. When using `torch.topk`, the indices of tied elements are not guaranteed to be stable
  8947. and may vary across different invocations.
  8948. Args:
  8949. {input}
  8950. k (int): the k in "top-k"
  8951. dim (int, optional): the dimension to sort along
  8952. largest (bool, optional): controls whether to return largest or
  8953. smallest elements
  8954. sorted (bool, optional): controls whether to return the elements
  8955. in sorted order
  8956. Keyword args:
  8957. out (tuple, optional): the output tuple of (Tensor, LongTensor) that can be
  8958. optionally given to be used as output buffers
  8959. Example::
  8960. >>> x = torch.arange(1., 6.)
  8961. >>> x
  8962. tensor([ 1., 2., 3., 4., 5.])
  8963. >>> torch.topk(x, 3)
  8964. torch.return_types.topk(values=tensor([5., 4., 3.]), indices=tensor([4, 3, 2]))
  8965. """.format(**common_args),
  8966. )
  8967. add_docstr(
  8968. torch.trace,
  8969. r"""
  8970. trace(input) -> Tensor
  8971. Returns the sum of the elements of the diagonal of the input 2-D matrix.
  8972. Example::
  8973. >>> x = torch.arange(1., 10.).view(3, 3)
  8974. >>> x
  8975. tensor([[ 1., 2., 3.],
  8976. [ 4., 5., 6.],
  8977. [ 7., 8., 9.]])
  8978. >>> torch.trace(x)
  8979. tensor(15.)
  8980. """,
  8981. )
  8982. add_docstr(
  8983. torch.transpose,
  8984. r"""
  8985. transpose(input, dim0, dim1) -> Tensor
  8986. Returns a tensor that is a transposed version of :attr:`input`.
  8987. The given dimensions :attr:`dim0` and :attr:`dim1` are swapped.
  8988. If :attr:`input` is a strided tensor then the resulting :attr:`out`
  8989. tensor shares its underlying storage with the :attr:`input` tensor, so
  8990. changing the content of one would change the content of the other.
  8991. If :attr:`input` is a :ref:`sparse tensor <sparse-docs>` then the
  8992. resulting :attr:`out` tensor *does not* share the underlying storage
  8993. with the :attr:`input` tensor.
  8994. If :attr:`input` is a :ref:`sparse tensor <sparse-docs>` with compressed
  8995. layout (SparseCSR, SparseBSR, SparseCSC or SparseBSC) the arguments
  8996. :attr:`dim0` and :attr:`dim1` must be both batch dimensions, or must
  8997. both be sparse dimensions. The batch dimensions of a sparse tensor are the
  8998. dimensions preceding the sparse dimensions.
  8999. .. note::
  9000. Transpositions which interchange the sparse dimensions of a `SparseCSR`
  9001. or `SparseCSC` layout tensor will result in the layout changing between
  9002. the two options. Transposition of the sparse dimensions of a ` SparseBSR`
  9003. or `SparseBSC` layout tensor will likewise generate a result with the
  9004. opposite layout.
  9005. Args:
  9006. {input}
  9007. dim0 (int): the first dimension to be transposed
  9008. dim1 (int): the second dimension to be transposed
  9009. Example::
  9010. >>> x = torch.randn(2, 3)
  9011. >>> x
  9012. tensor([[ 1.0028, -0.9893, 0.5809],
  9013. [-0.1669, 0.7299, 0.4942]])
  9014. >>> torch.transpose(x, 0, 1)
  9015. tensor([[ 1.0028, -0.1669],
  9016. [-0.9893, 0.7299],
  9017. [ 0.5809, 0.4942]])
  9018. See also :func:`torch.t`.
  9019. """.format(**common_args),
  9020. )
  9021. add_docstr(
  9022. torch.triangular_solve,
  9023. r"""
  9024. triangular_solve(b, A, upper=True, transpose=False, unitriangular=False, *, out=None) -> (Tensor, Tensor)
  9025. Solves a system of equations with a square upper or lower triangular invertible matrix :math:`A`
  9026. and multiple right-hand sides :math:`b`.
  9027. In symbols, it solves :math:`AX = b` and assumes :math:`A` is square upper-triangular
  9028. (or lower-triangular if :attr:`upper`\ `= False`) and does not have zeros on the diagonal.
  9029. `torch.triangular_solve(b, A)` can take in 2D inputs `b, A` or inputs that are
  9030. batches of 2D matrices. If the inputs are batches, then returns
  9031. batched outputs `X`
  9032. If the diagonal of :attr:`A` contains zeros or elements that are very close to zero and
  9033. :attr:`unitriangular`\ `= False` (default) or if the input matrix is badly conditioned,
  9034. the result may contain `NaN` s.
  9035. Supports input of float, double, cfloat and cdouble data types.
  9036. .. warning::
  9037. :func:`torch.triangular_solve` is deprecated in favor of :func:`torch.linalg.solve_triangular`
  9038. and will be removed in a future PyTorch release.
  9039. :func:`torch.linalg.solve_triangular` has its arguments reversed and does not return a
  9040. copy of one of the inputs.
  9041. ``X = torch.triangular_solve(B, A).solution`` should be replaced with
  9042. .. code:: python
  9043. X = torch.linalg.solve_triangular(A, B)
  9044. Args:
  9045. b (Tensor): multiple right-hand sides of size :math:`(*, m, k)` where
  9046. :math:`*` is zero of more batch dimensions
  9047. A (Tensor): the input triangular coefficient matrix of size :math:`(*, m, m)`
  9048. where :math:`*` is zero or more batch dimensions
  9049. upper (bool, optional): whether :math:`A` is upper or lower triangular. Default: ``True``.
  9050. transpose (bool, optional): solves `op(A)X = b` where `op(A) = A^T` if this flag is ``True``,
  9051. and `op(A) = A` if it is ``False``. Default: ``False``.
  9052. unitriangular (bool, optional): whether :math:`A` is unit triangular.
  9053. If True, the diagonal elements of :math:`A` are assumed to be
  9054. 1 and not referenced from :math:`A`. Default: ``False``.
  9055. Keyword args:
  9056. out ((Tensor, Tensor), optional): tuple of two tensors to write
  9057. the output to. Ignored if `None`. Default: `None`.
  9058. Returns:
  9059. A namedtuple `(solution, cloned_coefficient)` where `cloned_coefficient`
  9060. is a clone of :math:`A` and `solution` is the solution :math:`X` to :math:`AX = b`
  9061. (or whatever variant of the system of equations, depending on the keyword arguments.)
  9062. Examples::
  9063. >>> A = torch.randn(2, 2).triu()
  9064. >>> A
  9065. tensor([[ 1.1527, -1.0753],
  9066. [ 0.0000, 0.7986]])
  9067. >>> b = torch.randn(2, 3)
  9068. >>> b
  9069. tensor([[-0.0210, 2.3513, -1.5492],
  9070. [ 1.5429, 0.7403, -1.0243]])
  9071. >>> torch.triangular_solve(b, A)
  9072. torch.return_types.triangular_solve(
  9073. solution=tensor([[ 1.7841, 2.9046, -2.5405],
  9074. [ 1.9320, 0.9270, -1.2826]]),
  9075. cloned_coefficient=tensor([[ 1.1527, -1.0753],
  9076. [ 0.0000, 0.7986]]))
  9077. """,
  9078. )
  9079. add_docstr(
  9080. torch.tril,
  9081. r"""
  9082. tril(input, diagonal=0, *, out=None) -> Tensor
  9083. Returns the lower triangular part of the matrix (2-D tensor) or batch of matrices
  9084. :attr:`input`, the other elements of the result tensor :attr:`out` are set to 0.
  9085. The lower triangular part of the matrix is defined as the elements on and
  9086. below the diagonal.
  9087. The argument :attr:`diagonal` controls which diagonal to consider. If
  9088. :attr:`diagonal` = 0, all elements on and below the main diagonal are
  9089. retained. A positive value includes just as many diagonals above the main
  9090. diagonal, and similarly a negative value excludes just as many diagonals below
  9091. the main diagonal. The main diagonal are the set of indices
  9092. :math:`\lbrace (i, i) \rbrace` for :math:`i \in [0, \min\{d_{1}, d_{2}\} - 1]` where
  9093. :math:`d_{1}, d_{2}` are the dimensions of the matrix.
  9094. """
  9095. + r"""
  9096. Args:
  9097. {input}
  9098. diagonal (int, optional): the diagonal to consider
  9099. Keyword args:
  9100. {out}
  9101. Example::
  9102. >>> a = torch.randn(3, 3)
  9103. >>> a
  9104. tensor([[-1.0813, -0.8619, 0.7105],
  9105. [ 0.0935, 0.1380, 2.2112],
  9106. [-0.3409, -0.9828, 0.0289]])
  9107. >>> torch.tril(a)
  9108. tensor([[-1.0813, 0.0000, 0.0000],
  9109. [ 0.0935, 0.1380, 0.0000],
  9110. [-0.3409, -0.9828, 0.0289]])
  9111. >>> b = torch.randn(4, 6)
  9112. >>> b
  9113. tensor([[ 1.2219, 0.5653, -0.2521, -0.2345, 1.2544, 0.3461],
  9114. [ 0.4785, -0.4477, 0.6049, 0.6368, 0.8775, 0.7145],
  9115. [ 1.1502, 3.2716, -1.1243, -0.5413, 0.3615, 0.6864],
  9116. [-0.0614, -0.7344, -1.3164, -0.7648, -1.4024, 0.0978]])
  9117. >>> torch.tril(b, diagonal=1)
  9118. tensor([[ 1.2219, 0.5653, 0.0000, 0.0000, 0.0000, 0.0000],
  9119. [ 0.4785, -0.4477, 0.6049, 0.0000, 0.0000, 0.0000],
  9120. [ 1.1502, 3.2716, -1.1243, -0.5413, 0.0000, 0.0000],
  9121. [-0.0614, -0.7344, -1.3164, -0.7648, -1.4024, 0.0000]])
  9122. >>> torch.tril(b, diagonal=-1)
  9123. tensor([[ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
  9124. [ 0.4785, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
  9125. [ 1.1502, 3.2716, 0.0000, 0.0000, 0.0000, 0.0000],
  9126. [-0.0614, -0.7344, -1.3164, 0.0000, 0.0000, 0.0000]])
  9127. """.format(**common_args),
  9128. )
  9129. # docstr is split in two parts to avoid format mis-captureing :math: braces '{}'
  9130. # as common args.
  9131. add_docstr(
  9132. torch.tril_indices,
  9133. r"""
  9134. tril_indices(row, col, offset=0, *, dtype=torch.long, device='cpu', layout=torch.strided) -> Tensor
  9135. Returns the indices of the lower triangular part of a :attr:`row`-by-
  9136. :attr:`col` matrix in a 2-by-N Tensor, where the first row contains row
  9137. coordinates of all indices and the second row contains column coordinates.
  9138. Indices are ordered based on rows and then columns.
  9139. The lower triangular part of the matrix is defined as the elements on and
  9140. below the diagonal.
  9141. The argument :attr:`offset` controls which diagonal to consider. If
  9142. :attr:`offset` = 0, all elements on and below the main diagonal are
  9143. retained. A positive value includes just as many diagonals above the main
  9144. diagonal, and similarly a negative value excludes just as many diagonals below
  9145. the main diagonal. The main diagonal are the set of indices
  9146. :math:`\lbrace (i, i) \rbrace` for :math:`i \in [0, \min\{d_{1}, d_{2}\} - 1]`
  9147. where :math:`d_{1}, d_{2}` are the dimensions of the matrix.
  9148. .. note::
  9149. When running on CUDA, ``row * col`` must be less than :math:`2^{59}` to
  9150. prevent overflow during calculation.
  9151. """
  9152. + r"""
  9153. Args:
  9154. row (``int``): number of rows in the 2-D matrix.
  9155. col (``int``): number of columns in the 2-D matrix.
  9156. offset (``int``): diagonal offset from the main diagonal.
  9157. Default: if not provided, 0.
  9158. Keyword args:
  9159. dtype (:class:`torch.dtype`, optional): the desired data type of returned tensor,
  9160. only support ``torch.int``, ``torch.long``. Default: if ``None``, ``torch.long``.
  9161. {device}
  9162. layout (:class:`torch.layout`, optional): currently only support ``torch.strided``.
  9163. Example::
  9164. >>> a = torch.tril_indices(3, 3)
  9165. >>> a
  9166. tensor([[0, 1, 1, 2, 2, 2],
  9167. [0, 0, 1, 0, 1, 2]])
  9168. >>> a = torch.tril_indices(4, 3, -1)
  9169. >>> a
  9170. tensor([[1, 2, 2, 3, 3, 3],
  9171. [0, 0, 1, 0, 1, 2]])
  9172. >>> a = torch.tril_indices(4, 3, 1)
  9173. >>> a
  9174. tensor([[0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3],
  9175. [0, 1, 0, 1, 2, 0, 1, 2, 0, 1, 2]])
  9176. """.format(**factory_common_args),
  9177. )
  9178. add_docstr(
  9179. torch.triu,
  9180. r"""
  9181. triu(input, diagonal=0, *, out=None) -> Tensor
  9182. Returns the upper triangular part of a matrix (2-D tensor) or batch of matrices
  9183. :attr:`input`, the other elements of the result tensor :attr:`out` are set to 0.
  9184. The upper triangular part of the matrix is defined as the elements on and
  9185. above the diagonal.
  9186. The argument :attr:`diagonal` controls which diagonal to consider. If
  9187. :attr:`diagonal` = 0, all elements on and above the main diagonal are
  9188. retained. A positive value excludes just as many diagonals above the main
  9189. diagonal, and similarly a negative value includes just as many diagonals below
  9190. the main diagonal. The main diagonal are the set of indices
  9191. :math:`\lbrace (i, i) \rbrace` for :math:`i \in [0, \min\{d_{1}, d_{2}\} - 1]` where
  9192. :math:`d_{1}, d_{2}` are the dimensions of the matrix.
  9193. """
  9194. + r"""
  9195. Args:
  9196. {input}
  9197. diagonal (int, optional): the diagonal to consider
  9198. Keyword args:
  9199. {out}
  9200. Example::
  9201. >>> a = torch.randn(3, 3)
  9202. >>> a
  9203. tensor([[ 0.2309, 0.5207, 2.0049],
  9204. [ 0.2072, -1.0680, 0.6602],
  9205. [ 0.3480, -0.5211, -0.4573]])
  9206. >>> torch.triu(a)
  9207. tensor([[ 0.2309, 0.5207, 2.0049],
  9208. [ 0.0000, -1.0680, 0.6602],
  9209. [ 0.0000, 0.0000, -0.4573]])
  9210. >>> torch.triu(a, diagonal=1)
  9211. tensor([[ 0.0000, 0.5207, 2.0049],
  9212. [ 0.0000, 0.0000, 0.6602],
  9213. [ 0.0000, 0.0000, 0.0000]])
  9214. >>> torch.triu(a, diagonal=-1)
  9215. tensor([[ 0.2309, 0.5207, 2.0049],
  9216. [ 0.2072, -1.0680, 0.6602],
  9217. [ 0.0000, -0.5211, -0.4573]])
  9218. >>> b = torch.randn(4, 6)
  9219. >>> b
  9220. tensor([[ 0.5876, -0.0794, -1.8373, 0.6654, 0.2604, 1.5235],
  9221. [-0.2447, 0.9556, -1.2919, 1.3378, -0.1768, -1.0857],
  9222. [ 0.4333, 0.3146, 0.6576, -1.0432, 0.9348, -0.4410],
  9223. [-0.9888, 1.0679, -1.3337, -1.6556, 0.4798, 0.2830]])
  9224. >>> torch.triu(b, diagonal=1)
  9225. tensor([[ 0.0000, -0.0794, -1.8373, 0.6654, 0.2604, 1.5235],
  9226. [ 0.0000, 0.0000, -1.2919, 1.3378, -0.1768, -1.0857],
  9227. [ 0.0000, 0.0000, 0.0000, -1.0432, 0.9348, -0.4410],
  9228. [ 0.0000, 0.0000, 0.0000, 0.0000, 0.4798, 0.2830]])
  9229. >>> torch.triu(b, diagonal=-1)
  9230. tensor([[ 0.5876, -0.0794, -1.8373, 0.6654, 0.2604, 1.5235],
  9231. [-0.2447, 0.9556, -1.2919, 1.3378, -0.1768, -1.0857],
  9232. [ 0.0000, 0.3146, 0.6576, -1.0432, 0.9348, -0.4410],
  9233. [ 0.0000, 0.0000, -1.3337, -1.6556, 0.4798, 0.2830]])
  9234. """.format(**common_args),
  9235. )
  9236. # docstr is split in two parts to avoid format mis-capturing :math: braces '{}'
  9237. # as common args.
  9238. add_docstr(
  9239. torch.triu_indices,
  9240. r"""
  9241. triu_indices(row, col, offset=0, *, dtype=torch.long, device='cpu', layout=torch.strided) -> Tensor
  9242. Returns the indices of the upper triangular part of a :attr:`row` by
  9243. :attr:`col` matrix in a 2-by-N Tensor, where the first row contains row
  9244. coordinates of all indices and the second row contains column coordinates.
  9245. Indices are ordered based on rows and then columns.
  9246. The upper triangular part of the matrix is defined as the elements on and
  9247. above the diagonal.
  9248. The argument :attr:`offset` controls which diagonal to consider. If
  9249. :attr:`offset` = 0, all elements on and above the main diagonal are
  9250. retained. A positive value excludes just as many diagonals above the main
  9251. diagonal, and similarly a negative value includes just as many diagonals below
  9252. the main diagonal. The main diagonal are the set of indices
  9253. :math:`\lbrace (i, i) \rbrace` for :math:`i \in [0, \min\{d_{1}, d_{2}\} - 1]`
  9254. where :math:`d_{1}, d_{2}` are the dimensions of the matrix.
  9255. .. note::
  9256. When running on CUDA, ``row * col`` must be less than :math:`2^{59}` to
  9257. prevent overflow during calculation.
  9258. """
  9259. + r"""
  9260. Args:
  9261. row (``int``): number of rows in the 2-D matrix.
  9262. col (``int``): number of columns in the 2-D matrix.
  9263. offset (``int``): diagonal offset from the main diagonal.
  9264. Default: if not provided, 0.
  9265. Keyword args:
  9266. dtype (:class:`torch.dtype`, optional): the desired data type of returned tensor,
  9267. only support ``torch.int``, ``torch.long``. Default: if ``None``, ``torch.long``.
  9268. {device}
  9269. layout (:class:`torch.layout`, optional): currently only support ``torch.strided``.
  9270. Example::
  9271. >>> a = torch.triu_indices(3, 3)
  9272. >>> a
  9273. tensor([[0, 0, 0, 1, 1, 2],
  9274. [0, 1, 2, 1, 2, 2]])
  9275. >>> a = torch.triu_indices(4, 3, -1)
  9276. >>> a
  9277. tensor([[0, 0, 0, 1, 1, 1, 2, 2, 3],
  9278. [0, 1, 2, 0, 1, 2, 1, 2, 2]])
  9279. >>> a = torch.triu_indices(4, 3, 1)
  9280. >>> a
  9281. tensor([[0, 0, 1],
  9282. [1, 2, 2]])
  9283. """.format(**factory_common_args),
  9284. )
  9285. add_docstr(
  9286. torch.true_divide,
  9287. r"""
  9288. true_divide(dividend, divisor, *, out) -> Tensor
  9289. Alias for :func:`torch.div` with ``rounding_mode=None``.
  9290. """,
  9291. )
  9292. add_docstr(
  9293. torch.trunc,
  9294. r"""
  9295. trunc(input, *, out=None) -> Tensor
  9296. Returns a new tensor with the truncated integer values of
  9297. the elements of :attr:`input`.
  9298. For integer inputs, follows the array-api convention of returning a
  9299. copy of the input tensor.
  9300. Args:
  9301. {input}
  9302. Keyword args:
  9303. {out}
  9304. Example::
  9305. >>> a = torch.randn(4)
  9306. >>> a
  9307. tensor([ 3.4742, 0.5466, -0.8008, -0.9079])
  9308. >>> torch.trunc(a)
  9309. tensor([ 3., 0., -0., -0.])
  9310. """.format(**common_args),
  9311. )
  9312. add_docstr(
  9313. torch.fake_quantize_per_tensor_affine,
  9314. r"""
  9315. fake_quantize_per_tensor_affine(input, scale, zero_point, quant_min, quant_max) -> Tensor
  9316. Returns a new tensor with the data in :attr:`input` fake quantized using :attr:`scale`,
  9317. :attr:`zero_point`, :attr:`quant_min` and :attr:`quant_max`.
  9318. .. math::
  9319. \text{output} = (
  9320. min(
  9321. \text{quant\_max},
  9322. max(
  9323. \text{quant\_min},
  9324. \text{std::nearby\_int}(\text{input} / \text{scale}) + \text{zero\_point}
  9325. )
  9326. ) - \text{zero\_point}
  9327. ) \times \text{scale}
  9328. Args:
  9329. input (Tensor): the input value(s), ``torch.float32`` tensor
  9330. scale (double scalar or ``float32`` Tensor): quantization scale
  9331. zero_point (int64 scalar or ``int32`` Tensor): quantization zero_point
  9332. quant_min (int64): lower bound of the quantized domain
  9333. quant_max (int64): upper bound of the quantized domain
  9334. Returns:
  9335. Tensor: A newly fake_quantized ``torch.float32`` tensor
  9336. Example::
  9337. >>> x = torch.randn(4)
  9338. >>> x
  9339. tensor([ 0.0552, 0.9730, 0.3973, -1.0780])
  9340. >>> torch.fake_quantize_per_tensor_affine(x, 0.1, 0, 0, 255)
  9341. tensor([0.1000, 1.0000, 0.4000, 0.0000])
  9342. >>> torch.fake_quantize_per_tensor_affine(x, torch.tensor(0.1), torch.tensor(0), 0, 255)
  9343. tensor([0.1000, 1.0000, 0.4000, 0.0000])
  9344. """,
  9345. )
  9346. add_docstr(
  9347. torch.fake_quantize_per_channel_affine,
  9348. r"""
  9349. fake_quantize_per_channel_affine(input, scale, zero_point, axis, quant_min, quant_max) -> Tensor
  9350. Returns a new tensor with the data in :attr:`input` fake quantized per channel using :attr:`scale`,
  9351. :attr:`zero_point`, :attr:`quant_min` and :attr:`quant_max`, across the channel specified by :attr:`axis`.
  9352. .. math::
  9353. \text{output} = (
  9354. min(
  9355. \text{quant\_max},
  9356. max(
  9357. \text{quant\_min},
  9358. \text{std::nearby\_int}(\text{input} / \text{scale}) + \text{zero\_point}
  9359. )
  9360. ) - \text{zero\_point}
  9361. ) \times \text{scale}
  9362. Args:
  9363. input (Tensor): the input value(s), in ``torch.float32``
  9364. scale (Tensor): quantization scale, per channel in ``torch.float32``
  9365. zero_point (Tensor): quantization zero_point, per channel in ``torch.int32`` or ``torch.half`` or ``torch.float32``
  9366. axis (int32): channel axis
  9367. quant_min (int64): lower bound of the quantized domain
  9368. quant_max (int64): upper bound of the quantized domain
  9369. Returns:
  9370. Tensor: A newly fake_quantized per channel ``torch.float32`` tensor
  9371. Example::
  9372. >>> x = torch.randn(2, 2, 2)
  9373. >>> x
  9374. tensor([[[-0.2525, -0.0466],
  9375. [ 0.3491, -0.2168]],
  9376. [[-0.5906, 1.6258],
  9377. [ 0.6444, -0.0542]]])
  9378. >>> scales = (torch.randn(2) + 1) * 0.05
  9379. >>> scales
  9380. tensor([0.0475, 0.0486])
  9381. >>> zero_points = torch.zeros(2).to(torch.int32)
  9382. >>> zero_points
  9383. tensor([0, 0])
  9384. >>> torch.fake_quantize_per_channel_affine(x, scales, zero_points, 1, 0, 255)
  9385. tensor([[[0.0000, 0.0000],
  9386. [0.3405, 0.0000]],
  9387. [[0.0000, 1.6134],
  9388. [0.6323, 0.0000]]])
  9389. """,
  9390. )
  9391. add_docstr(
  9392. torch.fix,
  9393. r"""
  9394. fix(input, *, out=None) -> Tensor
  9395. Alias for :func:`torch.trunc`
  9396. """,
  9397. )
  9398. add_docstr(
  9399. torch.unsqueeze,
  9400. r"""
  9401. unsqueeze(input, dim) -> Tensor
  9402. Returns a new tensor with a dimension of size one inserted at the
  9403. specified position.
  9404. The returned tensor shares the same underlying data with this tensor.
  9405. A :attr:`dim` value within the range ``[-input.dim() - 1, input.dim() + 1)``
  9406. can be used. Negative :attr:`dim` will correspond to :meth:`unsqueeze`
  9407. applied at :attr:`dim` = ``dim + input.dim() + 1``.
  9408. Args:
  9409. {input}
  9410. dim (int): the index at which to insert the singleton dimension
  9411. Example::
  9412. >>> x = torch.tensor([1, 2, 3, 4])
  9413. >>> torch.unsqueeze(x, 0)
  9414. tensor([[ 1, 2, 3, 4]])
  9415. >>> torch.unsqueeze(x, 1)
  9416. tensor([[ 1],
  9417. [ 2],
  9418. [ 3],
  9419. [ 4]])
  9420. """.format(**common_args),
  9421. )
  9422. add_docstr(
  9423. torch.var,
  9424. r"""
  9425. var(input, dim=None, *, correction=1, keepdim=False, out=None) -> Tensor
  9426. Calculates the variance over the dimensions specified by :attr:`dim`. :attr:`dim`
  9427. can be a single dimension, list of dimensions, or ``None`` to reduce over all
  9428. dimensions.
  9429. The variance (:math:`\sigma^2`) is calculated as
  9430. .. math:: \sigma^2 = \frac{1}{\max(0,~N - \delta N)}\sum_{i=0}^{N-1}(x_i-\bar{x})^2
  9431. where :math:`x` is the sample set of elements, :math:`\bar{x}` is the
  9432. sample mean, :math:`N` is the number of samples and :math:`\delta N` is
  9433. the :attr:`correction`.
  9434. """
  9435. + r"""
  9436. {keepdim_details}
  9437. Args:
  9438. {input}
  9439. {opt_dim_all_reduce}
  9440. Keyword args:
  9441. correction (int): difference between the sample size and sample degrees of freedom.
  9442. Defaults to `Bessel's correction`_, ``correction=1``.
  9443. .. versionchanged:: 2.0
  9444. Previously this argument was called ``unbiased`` and was a boolean
  9445. with ``True`` corresponding to ``correction=1`` and ``False`` being
  9446. ``correction=0``.
  9447. {opt_keepdim}
  9448. {out}
  9449. Example:
  9450. >>> a = torch.tensor(
  9451. ... [[ 0.2035, 1.2959, 1.8101, -0.4644],
  9452. ... [ 1.5027, -0.3270, 0.5905, 0.6538],
  9453. ... [-1.5745, 1.3330, -0.5596, -0.6548],
  9454. ... [ 0.1264, -0.5080, 1.6420, 0.1992]]
  9455. ... ) # fmt: skip
  9456. >>> torch.var(a, dim=1, keepdim=True)
  9457. tensor([[1.0631],
  9458. [0.5590],
  9459. [1.4893],
  9460. [0.8258]])
  9461. .. _Bessel's correction: https://en.wikipedia.org/wiki/Bessel%27s_correction
  9462. """.format(**multi_dim_common),
  9463. )
  9464. add_docstr(
  9465. torch.var_mean,
  9466. r"""
  9467. var_mean(input, dim=None, *, correction=1, keepdim=False, out=None) -> (Tensor, Tensor)
  9468. Calculates the variance and mean over the dimensions specified by :attr:`dim`.
  9469. :attr:`dim` can be a single dimension, list of dimensions, or ``None`` to
  9470. reduce over all dimensions.
  9471. The variance (:math:`\sigma^2`) is calculated as
  9472. .. math:: \sigma^2 = \frac{1}{\max(0,~N - \delta N)}\sum_{i=0}^{N-1}(x_i-\bar{x})^2
  9473. where :math:`x` is the sample set of elements, :math:`\bar{x}` is the
  9474. sample mean, :math:`N` is the number of samples and :math:`\delta N` is
  9475. the :attr:`correction`.
  9476. """
  9477. + r"""
  9478. {keepdim_details}
  9479. Args:
  9480. {input}
  9481. {opt_dim_all_reduce}
  9482. Keyword args:
  9483. correction (int): difference between the sample size and sample degrees of freedom.
  9484. Defaults to `Bessel's correction`_, ``correction=1``.
  9485. .. versionchanged:: 2.0
  9486. Previously this argument was called ``unbiased`` and was a boolean
  9487. with ``True`` corresponding to ``correction=1`` and ``False`` being
  9488. ``correction=0``.
  9489. {opt_keepdim}
  9490. {out}
  9491. Returns:
  9492. A tuple (var, mean) containing the variance and mean.
  9493. Example:
  9494. >>> a = torch.tensor(
  9495. ... [[ 0.2035, 1.2959, 1.8101, -0.4644],
  9496. ... [ 1.5027, -0.3270, 0.5905, 0.6538],
  9497. ... [-1.5745, 1.3330, -0.5596, -0.6548],
  9498. ... [ 0.1264, -0.5080, 1.6420, 0.1992]]
  9499. ... ) # fmt: skip
  9500. >>> torch.var_mean(a, dim=0, keepdim=True)
  9501. (tensor([[1.5926, 1.0056, 1.2005, 0.3646]]),
  9502. tensor([[ 0.0645, 0.4485, 0.8707, -0.0665]]))
  9503. .. _Bessel's correction: https://en.wikipedia.org/wiki/Bessel%27s_correction
  9504. """.format(**multi_dim_common),
  9505. )
  9506. add_docstr(
  9507. torch.zeros,
  9508. r"""
  9509. zeros(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) -> Tensor
  9510. Returns a tensor filled with the scalar value `0`, with the shape defined
  9511. by the variable argument :attr:`size`.
  9512. Args:
  9513. size (int...): a sequence of integers defining the shape of the output tensor.
  9514. Can be a variable number of arguments or a collection like a list or tuple.
  9515. Keyword args:
  9516. {out}
  9517. {dtype}
  9518. {layout}
  9519. {device}
  9520. {requires_grad}
  9521. Example::
  9522. >>> torch.zeros(2, 3)
  9523. tensor([[ 0., 0., 0.],
  9524. [ 0., 0., 0.]])
  9525. >>> torch.zeros(5)
  9526. tensor([ 0., 0., 0., 0., 0.])
  9527. """.format(**factory_common_args),
  9528. )
  9529. add_docstr(
  9530. torch.zeros_like,
  9531. r"""
  9532. zeros_like(input, *, dtype=None, layout=None, device=None, requires_grad=False, memory_format=torch.preserve_format) -> Tensor
  9533. Returns a tensor filled with the scalar value `0`, with the same size as
  9534. :attr:`input`. ``torch.zeros_like(input)`` is equivalent to
  9535. ``torch.zeros(input.size(), dtype=input.dtype, layout=input.layout, device=input.device)``.
  9536. .. warning::
  9537. As of 0.4, this function does not support an :attr:`out` keyword. As an alternative,
  9538. the old ``torch.zeros_like(input, out=output)`` is equivalent to
  9539. ``torch.zeros(input.size(), out=output)``.
  9540. Args:
  9541. {input}
  9542. Keyword args:
  9543. {dtype}
  9544. {layout}
  9545. {device}
  9546. {requires_grad}
  9547. {memory_format}
  9548. Example::
  9549. >>> input = torch.empty(2, 3)
  9550. >>> torch.zeros_like(input)
  9551. tensor([[ 0., 0., 0.],
  9552. [ 0., 0., 0.]])
  9553. """.format(**factory_like_common_args),
  9554. )
  9555. add_docstr(
  9556. torch.empty,
  9557. """
  9558. empty(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False, pin_memory=False, \
  9559. memory_format=torch.contiguous_format) -> Tensor
  9560. Returns a tensor filled with uninitialized data. The shape of the tensor is
  9561. defined by the variable argument :attr:`size`.
  9562. .. note::
  9563. If :func:`torch.use_deterministic_algorithms()` and
  9564. :attr:`torch.utils.deterministic.fill_uninitialized_memory` are both set to
  9565. ``True``, the output tensor is initialized to prevent any possible
  9566. nondeterministic behavior from using the data as an input to an operation.
  9567. Floating point and complex tensors are filled with NaN, and integer tensors
  9568. are filled with the maximum value.
  9569. Args:
  9570. size (int...): a sequence of integers defining the shape of the output tensor.
  9571. Can be a variable number of arguments or a collection like a list or tuple.
  9572. Keyword args:
  9573. {out}
  9574. {dtype}
  9575. {layout}
  9576. {device}
  9577. {requires_grad}
  9578. {pin_memory}
  9579. {memory_format}
  9580. Example::
  9581. >>> torch.empty((2,3), dtype=torch.int64)
  9582. tensor([[ 9.4064e+13, 2.8000e+01, 9.3493e+13],
  9583. [ 7.5751e+18, 7.1428e+18, 7.5955e+18]])
  9584. """.format(**factory_common_args),
  9585. )
  9586. add_docstr(
  9587. torch.empty_like,
  9588. r"""
  9589. empty_like(input, *, dtype=None, layout=None, device=None, requires_grad=False, memory_format=torch.preserve_format) -> Tensor
  9590. Returns an uninitialized tensor with the same size as :attr:`input`.
  9591. ``torch.empty_like(input)`` is equivalent to
  9592. ``torch.empty(input.size(), dtype=input.dtype, layout=input.layout, device=input.device)``.
  9593. .. note::
  9594. If :func:`torch.use_deterministic_algorithms()` and
  9595. :attr:`torch.utils.deterministic.fill_uninitialized_memory` are both set to
  9596. ``True``, the output tensor is initialized to prevent any possible
  9597. nondeterministic behavior from using the data as an input to an operation.
  9598. Floating point and complex tensors are filled with NaN, and integer tensors
  9599. are filled with the maximum value.
  9600. When ``torch.preserve_format`` is used:
  9601. If the input tensor is dense (i.e., non-overlapping strided),
  9602. its memory format (including strides) is retained.
  9603. Otherwise (e.g., a non-dense view like a stepped slice),
  9604. the output is converted to the dense format.
  9605. Args:
  9606. {input}
  9607. Keyword args:
  9608. {dtype}
  9609. {layout}
  9610. {device}
  9611. {requires_grad}
  9612. {memory_format}
  9613. Example::
  9614. >>> a=torch.empty((2,3), dtype=torch.int32, device = 'cuda')
  9615. >>> torch.empty_like(a)
  9616. tensor([[0, 0, 0],
  9617. [0, 0, 0]], device='cuda:0', dtype=torch.int32)
  9618. """.format(**factory_like_common_args),
  9619. )
  9620. add_docstr(
  9621. torch.empty_strided,
  9622. r"""
  9623. empty_strided(size, stride, *, dtype=None, layout=None, device=None, requires_grad=False, pin_memory=False) -> Tensor
  9624. Creates a tensor with the specified :attr:`size` and :attr:`stride` and filled with undefined data.
  9625. .. warning::
  9626. If the constructed tensor is "overlapped" (with multiple indices referring to the same element
  9627. in memory) its behavior is undefined.
  9628. .. note::
  9629. If :func:`torch.use_deterministic_algorithms()` and
  9630. :attr:`torch.utils.deterministic.fill_uninitialized_memory` are both set to
  9631. ``True``, the output tensor is initialized to prevent any possible
  9632. nondeterministic behavior from using the data as an input to an operation.
  9633. Floating point and complex tensors are filled with NaN, and integer tensors
  9634. are filled with the maximum value.
  9635. Args:
  9636. size (tuple of int): the shape of the output tensor
  9637. stride (tuple of int): the strides of the output tensor
  9638. Keyword args:
  9639. {dtype}
  9640. {layout}
  9641. {device}
  9642. {requires_grad}
  9643. {pin_memory}
  9644. Example::
  9645. >>> a = torch.empty_strided((2, 3), (1, 2))
  9646. >>> a
  9647. tensor([[8.9683e-44, 4.4842e-44, 5.1239e+07],
  9648. [0.0000e+00, 0.0000e+00, 3.0705e-41]])
  9649. >>> a.stride()
  9650. (1, 2)
  9651. >>> a.size()
  9652. torch.Size([2, 3])
  9653. """.format(**factory_common_args),
  9654. )
  9655. add_docstr(
  9656. torch.empty_permuted,
  9657. r"""
  9658. empty_permuted(size, physical_layout, *, dtype=None, layout=None, device=None, requires_grad=False, pin_memory=False) -> Tensor
  9659. Creates an uninitialized, non-overlapping and dense tensor with the
  9660. specified :attr:`size`, with :attr:`physical_layout` specifying how the
  9661. dimensions are physically laid out in memory (each logical dimension is listed
  9662. from outermost to innermost). :attr:`physical_layout` is a generalization
  9663. of NCHW/NHWC notation: if each dimension is assigned a number according to
  9664. what order they occur in size (N=0, C=1, H=2, W=3), then NCHW is ``(0, 1, 2, 3)``
  9665. while NHWC is ``(0, 2, 3, 1)``. Equivalently, the strides of the output
  9666. tensor ``t`` are such that ``t.stride(physical_layout[i]) == contiguous_strides[i]``
  9667. (notably, this function is *not* equivalent to ``torch.empty(size).permute(physical_layout)``).
  9668. Unlike :func:`torch.empty_strided`, this is guaranteed to produce a dense
  9669. tensor with no overlaps. If possible, prefer using this function over
  9670. :func:`torch.empty_strided` or manual use of :func:`torch.as_strided`.
  9671. .. note::
  9672. If :func:`torch.use_deterministic_algorithms()` and
  9673. :attr:`torch.utils.deterministic.fill_uninitialized_memory` are both set to
  9674. ``True``, the output tensor is initialized to prevent any possible
  9675. nondeterministic behavior from using the data as an input to an operation.
  9676. Floating point and complex tensors are filled with NaN, and integer tensors
  9677. are filled with the maximum value.
  9678. Args:
  9679. size (tuple of int): the shape of the output tensor
  9680. physical_layout (tuple of int): the ordering of dimensions physically in memory
  9681. Keyword args:
  9682. {dtype}
  9683. {layout}
  9684. {device}
  9685. {requires_grad}
  9686. {pin_memory}
  9687. Examples:
  9688. >>> torch.empty((2, 3, 5, 7)).stride()
  9689. (105, 35, 7, 1)
  9690. >>> torch.empty_permuted((2, 3, 5, 7), (0, 1, 2, 3)).stride()
  9691. (105, 35, 7, 1)
  9692. >>> torch.empty((2, 3, 5, 7), memory_format=torch.channels_last).stride()
  9693. (105, 1, 21, 3)
  9694. >>> torch.empty_permuted((2, 3, 5, 7), (0, 2, 3, 1)).stride()
  9695. (105, 1, 21, 3)
  9696. >>> torch.empty_permuted((2, 3, 5, 7), (0, 2, 3, 1)).dim_order()
  9697. (0, 2, 3, 1)
  9698. """.format(**factory_common_args),
  9699. )
  9700. add_docstr(
  9701. torch.full,
  9702. r"""
  9703. full(size, fill_value, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) -> Tensor
  9704. Creates a tensor of size :attr:`size` filled with :attr:`fill_value`. The
  9705. tensor's dtype is inferred from :attr:`fill_value`.
  9706. Args:
  9707. size (int...): a list, tuple, or :class:`torch.Size` of integers defining the
  9708. shape of the output tensor.
  9709. fill_value (Scalar): the value to fill the output tensor with.
  9710. Keyword args:
  9711. {out}
  9712. {dtype}
  9713. {layout}
  9714. {device}
  9715. {requires_grad}
  9716. Example::
  9717. >>> torch.full((2, 3), 3.141592)
  9718. tensor([[ 3.1416, 3.1416, 3.1416],
  9719. [ 3.1416, 3.1416, 3.1416]])
  9720. """.format(**factory_common_args),
  9721. )
  9722. add_docstr(
  9723. torch.full_like,
  9724. """
  9725. full_like(input, fill_value, \\*, dtype=None, layout=torch.strided, device=None, requires_grad=False, \
  9726. memory_format=torch.preserve_format) -> Tensor
  9727. Returns a tensor with the same size as :attr:`input` filled with :attr:`fill_value`.
  9728. ``torch.full_like(input, fill_value)`` is equivalent to
  9729. ``torch.full(input.size(), fill_value, dtype=input.dtype, layout=input.layout, device=input.device)``.
  9730. Args:
  9731. {input}
  9732. fill_value: the number to fill the output tensor with.
  9733. Keyword args:
  9734. {dtype}
  9735. {layout}
  9736. {device}
  9737. {requires_grad}
  9738. {memory_format}
  9739. Example::
  9740. >>> x = torch.ones(2, 3)
  9741. >>> torch.full_like(x, 3.141592)
  9742. tensor([[ 3.1416, 3.1416, 3.1416],
  9743. [ 3.1416, 3.1416, 3.1416]])
  9744. >>> torch.full_like(x, 7)
  9745. tensor([[7., 7., 7.],
  9746. [7., 7., 7.]])
  9747. >>> torch.full_like(x, 0.5, dtype=torch.int32)
  9748. tensor([[0, 0, 0],
  9749. [0, 0, 0]], dtype=torch.int32)
  9750. >>> y = torch.randn(3, 4, dtype=torch.float64)
  9751. >>> torch.full_like(y, -1.0)
  9752. tensor([[-1., -1., -1., -1.],
  9753. [-1., -1., -1., -1.],
  9754. [-1., -1., -1., -1.]], dtype=torch.float64)
  9755. """.format(**factory_like_common_args),
  9756. )
  9757. add_docstr(
  9758. torch.det,
  9759. r"""
  9760. det(input) -> Tensor
  9761. Alias for :func:`torch.linalg.det`
  9762. """,
  9763. )
  9764. add_docstr(
  9765. torch.where,
  9766. r"""
  9767. where(condition, input, other, *, out=None) -> Tensor
  9768. Return a tensor of elements selected from either :attr:`input` or :attr:`other`, depending on :attr:`condition`.
  9769. The operation is defined as:
  9770. .. math::
  9771. \text{out}_i = \begin{cases}
  9772. \text{input}_i & \text{if } \text{condition}_i \\
  9773. \text{other}_i & \text{otherwise} \\
  9774. \end{cases}
  9775. """
  9776. + r"""
  9777. .. note::
  9778. The tensors :attr:`condition`, :attr:`input`, :attr:`other` must be :ref:`broadcastable <broadcasting-semantics>`.
  9779. Arguments:
  9780. condition (BoolTensor): When True (nonzero), yield input, otherwise yield other
  9781. input (Tensor or Scalar): value (if :attr:`input` is a scalar) or values selected at indices
  9782. where :attr:`condition` is ``True``
  9783. other (Tensor or Scalar): value (if :attr:`other` is a scalar) or values selected at indices
  9784. where :attr:`condition` is ``False``
  9785. Keyword args:
  9786. {out}
  9787. Returns:
  9788. Tensor: A tensor of shape equal to the broadcasted shape of :attr:`condition`, :attr:`input`, :attr:`other`
  9789. Example::
  9790. >>> x = torch.randn(3, 2)
  9791. >>> y = torch.ones(3, 2)
  9792. >>> x
  9793. tensor([[-0.4620, 0.3139],
  9794. [ 0.3898, -0.7197],
  9795. [ 0.0478, -0.1657]])
  9796. >>> torch.where(x > 0, 1.0, 0.0)
  9797. tensor([[0., 1.],
  9798. [1., 0.],
  9799. [1., 0.]])
  9800. >>> torch.where(x > 0, x, y)
  9801. tensor([[ 1.0000, 0.3139],
  9802. [ 0.3898, 1.0000],
  9803. [ 0.0478, 1.0000]])
  9804. >>> x = torch.randn(2, 2, dtype=torch.double)
  9805. >>> x
  9806. tensor([[ 1.0779, 0.0383],
  9807. [-0.8785, -1.1089]], dtype=torch.float64)
  9808. >>> torch.where(x > 0, x, 0.)
  9809. tensor([[1.0779, 0.0383],
  9810. [0.0000, 0.0000]], dtype=torch.float64)
  9811. .. function:: where(condition) -> tuple of LongTensor
  9812. :noindex:
  9813. ``torch.where(condition)`` is identical to
  9814. ``torch.nonzero(condition, as_tuple=True)``.
  9815. .. note::
  9816. See also :func:`torch.nonzero`.
  9817. """.format(**common_args),
  9818. )
  9819. add_docstr(
  9820. torch.logdet,
  9821. r"""
  9822. logdet(input) -> Tensor
  9823. Calculates log determinant of a square matrix or batches of square matrices.
  9824. It returns ``-inf`` if the input has a determinant of zero, and ``NaN`` if it has
  9825. a negative determinant.
  9826. .. note::
  9827. Backward through :meth:`logdet` internally uses SVD results when :attr:`input`
  9828. is not invertible. In this case, double backward through :meth:`logdet` will
  9829. be unstable in when :attr:`input` doesn't have distinct singular values. See
  9830. :func:`torch.linalg.svd` for details.
  9831. .. seealso::
  9832. :func:`torch.linalg.slogdet` computes the sign (resp. angle) and natural logarithm of the
  9833. absolute value of the determinant of real-valued (resp. complex) square matrices.
  9834. Arguments:
  9835. input (Tensor): the input tensor of size ``(*, n, n)`` where ``*`` is zero or more
  9836. batch dimensions.
  9837. Example::
  9838. >>> A = torch.randn(3, 3)
  9839. >>> torch.det(A)
  9840. tensor(0.2611)
  9841. >>> torch.logdet(A)
  9842. tensor(-1.3430)
  9843. >>> A
  9844. tensor([[[ 0.9254, -0.6213],
  9845. [-0.5787, 1.6843]],
  9846. [[ 0.3242, -0.9665],
  9847. [ 0.4539, -0.0887]],
  9848. [[ 1.1336, -0.4025],
  9849. [-0.7089, 0.9032]]])
  9850. >>> A.det()
  9851. tensor([1.1990, 0.4099, 0.7386])
  9852. >>> A.det().log()
  9853. tensor([ 0.1815, -0.8917, -0.3031])
  9854. """,
  9855. )
  9856. add_docstr(
  9857. torch.slogdet,
  9858. r"""
  9859. slogdet(input) -> (Tensor, Tensor)
  9860. Alias for :func:`torch.linalg.slogdet`
  9861. """,
  9862. )
  9863. add_docstr(
  9864. torch.pinverse,
  9865. r"""
  9866. pinverse(input, rcond=1e-15) -> Tensor
  9867. Alias for :func:`torch.linalg.pinv`
  9868. """,
  9869. )
  9870. add_docstr(
  9871. torch.hann_window,
  9872. """
  9873. hann_window(window_length, periodic=True, *, dtype=None, \
  9874. layout=torch.strided, device=None, requires_grad=False) -> Tensor
  9875. """
  9876. + r"""
  9877. Hann window function.
  9878. .. math::
  9879. w[n] = \frac{1}{2}\ \left[1 - \cos \left( \frac{2 \pi n}{N - 1} \right)\right] =
  9880. \sin^2 \left( \frac{\pi n}{N - 1} \right),
  9881. where :math:`N` is the full window size.
  9882. The input :attr:`window_length` is a positive integer controlling the
  9883. returned window size. :attr:`periodic` flag determines whether the returned
  9884. window trims off the last duplicate value from the symmetric window and is
  9885. ready to be used as a periodic window with functions like
  9886. :meth:`torch.stft`. Therefore, if :attr:`periodic` is true, the :math:`N` in
  9887. above formula is in fact :math:`\text{window\_length} + 1`. Also, we always have
  9888. ``torch.hann_window(L, periodic=True)`` equal to
  9889. ``torch.hann_window(L + 1, periodic=False)[:-1])``.
  9890. .. note::
  9891. If :attr:`window_length` :math:`=1`, the returned window contains a single value 1.
  9892. """
  9893. + r"""
  9894. Arguments:
  9895. window_length (int): the size of returned window
  9896. periodic (bool, optional): If True, returns a window to be used as periodic
  9897. function. If False, return a symmetric window.
  9898. Keyword args:
  9899. {dtype} Only floating point types are supported.
  9900. layout (:class:`torch.layout`, optional): the desired layout of returned window tensor. Only
  9901. ``torch.strided`` (dense layout) is supported.
  9902. {device}
  9903. {requires_grad}
  9904. Returns:
  9905. Tensor: A 1-D tensor of size :math:`(\text{{window\_length}},)` containing the window
  9906. """.format(**factory_common_args),
  9907. )
  9908. add_docstr(
  9909. torch.hamming_window,
  9910. """
  9911. hamming_window(window_length, *, dtype=None, layout=None, device=None, pin_memory=False, \
  9912. requires_grad=False) -> Tensor
  9913. """
  9914. + r"""
  9915. Hamming window function.
  9916. .. math::
  9917. w[n] = \alpha - \beta\ \cos \left( \frac{2 \pi n}{N - 1} \right),
  9918. where :math:`N` is the full window size.
  9919. The input :attr:`window_length` is a positive integer controlling the
  9920. returned window size. :attr:`periodic` flag determines whether the returned
  9921. window trims off the last duplicate value from the symmetric window and is
  9922. ready to be used as a periodic window with functions like
  9923. :meth:`torch.stft`. Therefore, if :attr:`periodic` is true, the :math:`N` in
  9924. above formula is in fact :math:`\text{window\_length} + 1`. Also, we always have
  9925. ``torch.hamming_window(L, periodic=True)`` equal to
  9926. ``torch.hamming_window(L + 1, periodic=False)[:-1])``.
  9927. .. note::
  9928. If :attr:`window_length` :math:`=1`, the returned window contains a single value 1.
  9929. .. note::
  9930. This is a generalized version of :meth:`torch.hann_window`.
  9931. """
  9932. + r"""
  9933. Arguments:
  9934. window_length (int): the size of returned window
  9935. Keyword args:
  9936. {dtype} Only floating point types are supported.
  9937. layout (:class:`torch.layout`, optional): the desired layout of returned window tensor. Only
  9938. ``torch.strided`` (dense layout) is supported.
  9939. {device}
  9940. {pin_memory}
  9941. {requires_grad}
  9942. Returns:
  9943. Tensor: A 1-D tensor of size :math:`(\text{{window\_length}},)` containing the window.
  9944. .. function:: hamming_window(window_length, periodic, *, dtype=None, layout=None, device=None, \
  9945. pin_memory=False, requires_grad=False) -> Tensor
  9946. :noindex:
  9947. Hamming window function with periodic specified.
  9948. Arguments:
  9949. window_length (int): the size of returned window
  9950. periodic (bool): If True, returns a window to be used as periodic
  9951. function. If False, return a symmetric window.
  9952. Keyword args:
  9953. {dtype} Only floating point types are supported.
  9954. layout (:class:`torch.layout`, optional): the desired layout of returned window tensor. Only
  9955. ``torch.strided`` (dense layout) is supported.
  9956. {device}
  9957. {pin_memory}
  9958. {requires_grad}
  9959. Returns:
  9960. Tensor: A 1-D tensor of size :math:`(\text{{window\_length}},)` containing the window.
  9961. .. function:: hamming_window(window_length, periodic, float alpha, *, dtype=None, layout=None, device=None, \
  9962. pin_memory=False, requires_grad=False) -> Tensor
  9963. :noindex:
  9964. Hamming window function with periodic and alpha specified.
  9965. Arguments:
  9966. window_length (int): the size of returned window
  9967. periodic (bool): If True, returns a window to be used as periodic
  9968. function. If False, return a symmetric window.
  9969. alpha (float): The coefficient :math:`\alpha` in the equation above
  9970. Keyword args:
  9971. {dtype} Only floating point types are supported.
  9972. layout (:class:`torch.layout`, optional): the desired layout of returned window tensor. Only
  9973. ``torch.strided`` (dense layout) is supported.
  9974. {device}
  9975. {pin_memory}
  9976. {requires_grad}
  9977. Returns:
  9978. Tensor: A 1-D tensor of size :math:`(\text{{window\_length}},)` containing the window.
  9979. .. function:: hamming_window(window_length, periodic, float alpha, float beta, *, dtype=None, layout=None, \
  9980. device=None, pin_memory=False, requires_grad=False) -> Tensor
  9981. :noindex:
  9982. Hamming window function with periodic, alpha and beta specified.
  9983. Arguments:
  9984. window_length (int): the size of returned window
  9985. periodic (bool): If True, returns a window to be used as periodic
  9986. function. If False, return a symmetric window.
  9987. alpha (float): The coefficient :math:`\alpha` in the equation above
  9988. beta (float): The coefficient :math:`\beta` in the equation above
  9989. Keyword args:
  9990. {dtype} Only floating point types are supported.
  9991. layout (:class:`torch.layout`, optional): the desired layout of returned window tensor. Only
  9992. ``torch.strided`` (dense layout) is supported.
  9993. {device}
  9994. {pin_memory}
  9995. {requires_grad}
  9996. Returns:
  9997. Tensor: A 1-D tensor of size :math:`(\text{{window\_length}},)` containing the window.
  9998. """.format(**factory_common_args),
  9999. )
  10000. add_docstr(
  10001. torch.bartlett_window,
  10002. """
  10003. bartlett_window(window_length, periodic=True, *, dtype=None, \
  10004. layout=torch.strided, device=None, requires_grad=False) -> Tensor
  10005. """
  10006. + r"""
  10007. Bartlett window function.
  10008. .. math::
  10009. w[n] = 1 - \left| \frac{2n}{N-1} - 1 \right| = \begin{cases}
  10010. \frac{2n}{N - 1} & \text{if } 0 \leq n \leq \frac{N - 1}{2} \\
  10011. 2 - \frac{2n}{N - 1} & \text{if } \frac{N - 1}{2} < n < N \\
  10012. \end{cases},
  10013. where :math:`N` is the full window size.
  10014. The input :attr:`window_length` is a positive integer controlling the
  10015. returned window size. :attr:`periodic` flag determines whether the returned
  10016. window trims off the last duplicate value from the symmetric window and is
  10017. ready to be used as a periodic window with functions like
  10018. :meth:`torch.stft`. Therefore, if :attr:`periodic` is true, the :math:`N` in
  10019. above formula is in fact :math:`\text{window\_length} + 1`. Also, we always have
  10020. ``torch.bartlett_window(L, periodic=True)`` equal to
  10021. ``torch.bartlett_window(L + 1, periodic=False)[:-1])``.
  10022. .. note::
  10023. If :attr:`window_length` :math:`=1`, the returned window contains a single value 1.
  10024. """
  10025. + r"""
  10026. Arguments:
  10027. window_length (int): the size of returned window
  10028. periodic (bool, optional): If True, returns a window to be used as periodic
  10029. function. If False, return a symmetric window.
  10030. Keyword args:
  10031. {dtype} Only floating point types are supported.
  10032. layout (:class:`torch.layout`, optional): the desired layout of returned window tensor. Only
  10033. ``torch.strided`` (dense layout) is supported.
  10034. {device}
  10035. {requires_grad}
  10036. Returns:
  10037. Tensor: A 1-D tensor of size :math:`(\text{{window\_length}},)` containing the window
  10038. """.format(**factory_common_args),
  10039. )
  10040. add_docstr(
  10041. torch.blackman_window,
  10042. """
  10043. blackman_window(window_length, periodic=True, *, dtype=None, \
  10044. layout=torch.strided, device=None, requires_grad=False) -> Tensor
  10045. """
  10046. + r"""
  10047. Blackman window function.
  10048. .. math::
  10049. w[n] = 0.42 - 0.5 \cos \left( \frac{2 \pi n}{N - 1} \right) + 0.08 \cos \left( \frac{4 \pi n}{N - 1} \right)
  10050. where :math:`N` is the full window size.
  10051. The input :attr:`window_length` is a positive integer controlling the
  10052. returned window size. :attr:`periodic` flag determines whether the returned
  10053. window trims off the last duplicate value from the symmetric window and is
  10054. ready to be used as a periodic window with functions like
  10055. :meth:`torch.stft`. Therefore, if :attr:`periodic` is true, the :math:`N` in
  10056. above formula is in fact :math:`\text{window\_length} + 1`. Also, we always have
  10057. ``torch.blackman_window(L, periodic=True)`` equal to
  10058. ``torch.blackman_window(L + 1, periodic=False)[:-1]``.
  10059. .. note::
  10060. If :attr:`window_length` :math:`=1`, the returned window contains a single value 1.
  10061. """
  10062. + r"""
  10063. Arguments:
  10064. window_length (int): the size of returned window
  10065. periodic (bool, optional): If True, returns a window to be used as periodic
  10066. function. If False, return a symmetric window.
  10067. Keyword args:
  10068. {dtype} Only floating point types are supported.
  10069. layout (:class:`torch.layout`, optional): the desired layout of returned window tensor. Only
  10070. ``torch.strided`` (dense layout) is supported.
  10071. {device}
  10072. {requires_grad}
  10073. Returns:
  10074. Tensor: A 1-D tensor of size :math:`(\text{{window\_length}},)` containing the window
  10075. """.format(**factory_common_args),
  10076. )
  10077. add_docstr(
  10078. torch.kaiser_window,
  10079. """
  10080. kaiser_window(window_length, periodic=True, beta=12.0, *, dtype=None, \
  10081. layout=torch.strided, device=None, requires_grad=False) -> Tensor
  10082. """
  10083. + r"""
  10084. Computes the Kaiser window with window length :attr:`window_length` and shape parameter :attr:`beta`.
  10085. Let I_0 be the zeroth order modified Bessel function of the first kind (see :func:`torch.i0`) and
  10086. ``N = L - 1`` if :attr:`periodic` is False and ``L`` if :attr:`periodic` is True,
  10087. where ``L`` is the :attr:`window_length`. This function computes:
  10088. .. math::
  10089. out_i = I_0 \left( \beta \sqrt{1 - \left( {\frac{i - N/2}{N/2}} \right) ^2 } \right) / I_0( \beta )
  10090. Calling ``torch.kaiser_window(L, B, periodic=True)`` is equivalent to calling
  10091. ``torch.kaiser_window(L + 1, B, periodic=False)[:-1])``.
  10092. The :attr:`periodic` argument is intended as a helpful shorthand
  10093. to produce a periodic window as input to functions like :func:`torch.stft`.
  10094. .. note::
  10095. If :attr:`window_length` is one, then the returned window is a single element tensor containing a one.
  10096. """
  10097. + r"""
  10098. Args:
  10099. window_length (int): length of the window.
  10100. periodic (bool, optional): If True, returns a periodic window suitable for use in spectral analysis.
  10101. If False, returns a symmetric window suitable for use in filter design.
  10102. beta (float, optional): shape parameter for the window.
  10103. Keyword args:
  10104. {dtype}
  10105. layout (:class:`torch.layout`, optional): the desired layout of returned window tensor. Only
  10106. ``torch.strided`` (dense layout) is supported.
  10107. {device}
  10108. {requires_grad}
  10109. """.format(**factory_common_args),
  10110. )
  10111. add_docstr(
  10112. torch.vander,
  10113. """
  10114. vander(x, N=None, increasing=False) -> Tensor
  10115. """
  10116. + r"""
  10117. Generates a Vandermonde matrix.
  10118. The columns of the output matrix are elementwise powers of the input vector :math:`x^{{(N-1)}}, x^{{(N-2)}}, ..., x^0`.
  10119. If increasing is True, the order of the columns is reversed :math:`x^0, x^1, ..., x^{{(N-1)}}`. Such a
  10120. matrix with a geometric progression in each row is named for Alexandre-Theophile Vandermonde.
  10121. Arguments:
  10122. x (Tensor): 1-D input tensor.
  10123. N (int, optional): Number of columns in the output. If N is not specified,
  10124. a square array is returned :math:`(N = len(x))`.
  10125. increasing (bool, optional): Order of the powers of the columns. If True,
  10126. the powers increase from left to right, if False (the default) they are reversed.
  10127. Returns:
  10128. Tensor: Vandermonde matrix. If increasing is False, the first column is :math:`x^{{(N-1)}}`,
  10129. the second :math:`x^{{(N-2)}}` and so forth. If increasing is True, the columns
  10130. are :math:`x^0, x^1, ..., x^{{(N-1)}}`.
  10131. Example::
  10132. >>> x = torch.tensor([1, 2, 3, 5])
  10133. >>> torch.vander(x)
  10134. tensor([[ 1, 1, 1, 1],
  10135. [ 8, 4, 2, 1],
  10136. [ 27, 9, 3, 1],
  10137. [125, 25, 5, 1]])
  10138. >>> torch.vander(x, N=3)
  10139. tensor([[ 1, 1, 1],
  10140. [ 4, 2, 1],
  10141. [ 9, 3, 1],
  10142. [25, 5, 1]])
  10143. >>> torch.vander(x, N=3, increasing=True)
  10144. tensor([[ 1, 1, 1],
  10145. [ 1, 2, 4],
  10146. [ 1, 3, 9],
  10147. [ 1, 5, 25]])
  10148. """.format(**factory_common_args),
  10149. )
  10150. add_docstr(
  10151. torch.unbind,
  10152. r"""
  10153. unbind(input, dim=0) -> seq
  10154. Removes a tensor dimension.
  10155. Returns a tuple of all slices along a given dimension, already without it.
  10156. Arguments:
  10157. input (Tensor): the tensor to unbind
  10158. dim (int): dimension to remove
  10159. Example::
  10160. >>> torch.unbind(torch.tensor([[1, 2, 3],
  10161. >>> [4, 5, 6],
  10162. >>> [7, 8, 9]]))
  10163. (tensor([1, 2, 3]), tensor([4, 5, 6]), tensor([7, 8, 9]))
  10164. """,
  10165. )
  10166. add_docstr(
  10167. torch.combinations,
  10168. r"""
  10169. combinations(input: Tensor, r: int = 2, with_replacement: bool = False) -> seq
  10170. Compute combinations of length :math:`r` of the given tensor. The behavior is similar to
  10171. python's `itertools.combinations` when `with_replacement` is set to `False`, and
  10172. `itertools.combinations_with_replacement` when `with_replacement` is set to `True`.
  10173. Arguments:
  10174. input (Tensor): 1D vector.
  10175. r (int, optional): number of elements to combine
  10176. with_replacement (bool, optional): whether to allow duplication in combination
  10177. Returns:
  10178. Tensor: A tensor equivalent to converting all the input tensors into lists, do
  10179. `itertools.combinations` or `itertools.combinations_with_replacement` on these
  10180. lists, and finally convert the resulting list into tensor.
  10181. Example::
  10182. >>> a = [1, 2, 3]
  10183. >>> list(itertools.combinations(a, r=2))
  10184. [(1, 2), (1, 3), (2, 3)]
  10185. >>> list(itertools.combinations(a, r=3))
  10186. [(1, 2, 3)]
  10187. >>> list(itertools.combinations_with_replacement(a, r=2))
  10188. [(1, 1), (1, 2), (1, 3), (2, 2), (2, 3), (3, 3)]
  10189. >>> tensor_a = torch.tensor(a)
  10190. >>> torch.combinations(tensor_a)
  10191. tensor([[1, 2],
  10192. [1, 3],
  10193. [2, 3]])
  10194. >>> torch.combinations(tensor_a, r=3)
  10195. tensor([[1, 2, 3]])
  10196. >>> torch.combinations(tensor_a, with_replacement=True)
  10197. tensor([[1, 1],
  10198. [1, 2],
  10199. [1, 3],
  10200. [2, 2],
  10201. [2, 3],
  10202. [3, 3]])
  10203. """,
  10204. )
  10205. add_docstr(
  10206. torch.trapezoid,
  10207. r"""
  10208. trapezoid(y, x=None, *, dx=None, dim=-1) -> Tensor
  10209. Computes the `trapezoidal rule <https://en.wikipedia.org/wiki/Trapezoidal_rule>`_ along
  10210. :attr:`dim`. By default the spacing between elements is assumed to be 1, but
  10211. :attr:`dx` can be used to specify a different constant spacing, and :attr:`x` can be
  10212. used to specify arbitrary spacing along :attr:`dim`. Only one of :attr:`x` or :attr:`dx` should be specified.
  10213. Assuming :attr:`y` is a one-dimensional tensor with elements :math:`{y_0, y_1, ..., y_n}`,
  10214. the default computation is
  10215. .. math::
  10216. \begin{aligned}
  10217. \sum_{i = 1}^{n} \frac{1}{2} (y_i + y_{i-1})
  10218. \end{aligned}
  10219. When :attr:`dx` is specified the computation becomes
  10220. .. math::
  10221. \begin{aligned}
  10222. \sum_{i = 1}^{n} \frac{\Delta x}{2} (y_i + y_{i-1})
  10223. \end{aligned}
  10224. effectively multiplying the result by :attr:`dx`. When :attr:`x` is specified,
  10225. assuming :attr:`x` is also a one-dimensional tensor with
  10226. elements :math:`{x_0, x_1, ..., x_n}`, the computation becomes
  10227. .. math::
  10228. \begin{aligned}
  10229. \sum_{i = 1}^{n} \frac{(x_i - x_{i-1})}{2} (y_i + y_{i-1})
  10230. \end{aligned}
  10231. When :attr:`x` and :attr:`y` have the same size, the computation is as described above and no broadcasting is needed.
  10232. The broadcasting behavior of this function is as follows when their sizes are different. For both :attr:`x`
  10233. and :attr:`y`, the function computes the difference between consecutive elements along
  10234. dimension :attr:`dim`. This effectively creates two tensors, `x_diff` and `y_diff`, that have
  10235. the same shape as the original tensors except their lengths along the dimension :attr:`dim` is reduced by 1.
  10236. After that, those two tensors are broadcast together to compute final output as part of the trapezoidal rule.
  10237. See the examples below for details.
  10238. .. note::
  10239. The trapezoidal rule is a technique for approximating the definite integral of a function
  10240. by averaging its left and right Riemann sums. The approximation becomes more accurate as
  10241. the resolution of the partition increases.
  10242. Arguments:
  10243. y (Tensor): Values to use when computing the trapezoidal rule.
  10244. x (Tensor): If specified, defines spacing between values as specified above.
  10245. Keyword arguments:
  10246. dx (float): constant spacing between values. If neither :attr:`x` or :attr:`dx`
  10247. are specified then this defaults to 1. Effectively multiplies the result by its value.
  10248. dim (int): The dimension along which to compute the trapezoidal rule.
  10249. The last (inner-most) dimension by default.
  10250. Examples::
  10251. >>> # Computes the trapezoidal rule in 1D, spacing is implicitly 1
  10252. >>> y = torch.tensor([1, 5, 10])
  10253. >>> torch.trapezoid(y)
  10254. tensor(10.5)
  10255. >>> # Computes the same trapezoidal rule directly to verify
  10256. >>> (1 + 10 + 10) / 2
  10257. 10.5
  10258. >>> # Computes the trapezoidal rule in 1D with constant spacing of 2
  10259. >>> # NOTE: the result is the same as before, but multiplied by 2
  10260. >>> torch.trapezoid(y, dx=2)
  10261. 21.0
  10262. >>> # Computes the trapezoidal rule in 1D with arbitrary spacing
  10263. >>> x = torch.tensor([1, 3, 6])
  10264. >>> torch.trapezoid(y, x)
  10265. 28.5
  10266. >>> # Computes the same trapezoidal rule directly to verify
  10267. >>> ((3 - 1) * (1 + 5) + (6 - 3) * (5 + 10)) / 2
  10268. 28.5
  10269. >>> # Computes the trapezoidal rule for each row of a 3x3 matrix
  10270. >>> y = torch.arange(9).reshape(3, 3)
  10271. tensor([[0, 1, 2],
  10272. [3, 4, 5],
  10273. [6, 7, 8]])
  10274. >>> torch.trapezoid(y)
  10275. tensor([ 2., 8., 14.])
  10276. >>> # Computes the trapezoidal rule for each column of the matrix
  10277. >>> torch.trapezoid(y, dim=0)
  10278. tensor([ 6., 8., 10.])
  10279. >>> # Computes the trapezoidal rule for each row of a 3x3 ones matrix
  10280. >>> # with the same arbitrary spacing
  10281. >>> y = torch.ones(3, 3)
  10282. >>> x = torch.tensor([1, 3, 6])
  10283. >>> torch.trapezoid(y, x)
  10284. array([5., 5., 5.])
  10285. >>> # Computes the trapezoidal rule for each row of a 3x3 ones matrix
  10286. >>> # with different arbitrary spacing per row
  10287. >>> y = torch.ones(3, 3)
  10288. >>> x = torch.tensor([[1, 2, 3], [1, 3, 5], [1, 4, 7]])
  10289. >>> torch.trapezoid(y, x)
  10290. array([2., 4., 6.])
  10291. """,
  10292. )
  10293. add_docstr(
  10294. torch.trapz,
  10295. r"""
  10296. trapz(y, x, *, dim=-1) -> Tensor
  10297. Alias for :func:`torch.trapezoid`.
  10298. """,
  10299. )
  10300. add_docstr(
  10301. torch.cumulative_trapezoid,
  10302. r"""
  10303. cumulative_trapezoid(y, x=None, *, dx=None, dim=-1) -> Tensor
  10304. Cumulatively computes the `trapezoidal rule <https://en.wikipedia.org/wiki/Trapezoidal_rule>`_
  10305. along :attr:`dim`. By default the spacing between elements is assumed to be 1, but
  10306. :attr:`dx` can be used to specify a different constant spacing, and :attr:`x` can be
  10307. used to specify arbitrary spacing along :attr:`dim`.
  10308. For more details, please read :func:`torch.trapezoid`. The difference between :func:`torch.trapezoid`
  10309. and this function is that, :func:`torch.trapezoid` returns a value for each integration,
  10310. where as this function returns a cumulative value for every spacing within the integration. This
  10311. is analogous to how `.sum` returns a value and `.cumsum` returns a cumulative sum.
  10312. Arguments:
  10313. y (Tensor): Values to use when computing the trapezoidal rule.
  10314. x (Tensor): If specified, defines spacing between values as specified above.
  10315. Keyword arguments:
  10316. dx (float): constant spacing between values. If neither :attr:`x` or :attr:`dx`
  10317. are specified then this defaults to 1. Effectively multiplies the result by its value.
  10318. dim (int): The dimension along which to compute the trapezoidal rule.
  10319. The last (inner-most) dimension by default.
  10320. Examples::
  10321. >>> # Cumulatively computes the trapezoidal rule in 1D, spacing is implicitly 1.
  10322. >>> y = torch.tensor([1, 5, 10])
  10323. >>> torch.cumulative_trapezoid(y)
  10324. tensor([3., 10.5])
  10325. >>> # Computes the same trapezoidal rule directly up to each element to verify
  10326. >>> (1 + 5) / 2
  10327. 3.0
  10328. >>> (1 + 10 + 10) / 2
  10329. 10.5
  10330. >>> # Cumulatively computes the trapezoidal rule in 1D with constant spacing of 2
  10331. >>> # NOTE: the result is the same as before, but multiplied by 2
  10332. >>> torch.cumulative_trapezoid(y, dx=2)
  10333. tensor([6., 21.])
  10334. >>> # Cumulatively computes the trapezoidal rule in 1D with arbitrary spacing
  10335. >>> x = torch.tensor([1, 3, 6])
  10336. >>> torch.cumulative_trapezoid(y, x)
  10337. tensor([6., 28.5])
  10338. >>> # Computes the same trapezoidal rule directly up to each element to verify
  10339. >>> ((3 - 1) * (1 + 5)) / 2
  10340. 6.0
  10341. >>> ((3 - 1) * (1 + 5) + (6 - 3) * (5 + 10)) / 2
  10342. 28.5
  10343. >>> # Cumulatively computes the trapezoidal rule for each row of a 3x3 matrix
  10344. >>> y = torch.arange(9).reshape(3, 3)
  10345. tensor([[0, 1, 2],
  10346. [3, 4, 5],
  10347. [6, 7, 8]])
  10348. >>> torch.cumulative_trapezoid(y)
  10349. tensor([[ 0.5, 2.],
  10350. [ 3.5, 8.],
  10351. [ 6.5, 14.]])
  10352. >>> # Cumulatively computes the trapezoidal rule for each column of the matrix
  10353. >>> torch.cumulative_trapezoid(y, dim=0)
  10354. tensor([[ 1.5, 2.5, 3.5],
  10355. [ 6.0, 8.0, 10.0]])
  10356. >>> # Cumulatively computes the trapezoidal rule for each row of a 3x3 ones matrix
  10357. >>> # with the same arbitrary spacing
  10358. >>> y = torch.ones(3, 3)
  10359. >>> x = torch.tensor([1, 3, 6])
  10360. >>> torch.cumulative_trapezoid(y, x)
  10361. tensor([[2., 5.],
  10362. [2., 5.],
  10363. [2., 5.]])
  10364. >>> # Cumulatively computes the trapezoidal rule for each row of a 3x3 ones matrix
  10365. >>> # with different arbitrary spacing per row
  10366. >>> y = torch.ones(3, 3)
  10367. >>> x = torch.tensor([[1, 2, 3], [1, 3, 5], [1, 4, 7]])
  10368. >>> torch.cumulative_trapezoid(y, x)
  10369. tensor([[1., 2.],
  10370. [2., 4.],
  10371. [3., 6.]])
  10372. """,
  10373. )
  10374. add_docstr(
  10375. torch.repeat_interleave,
  10376. r"""
  10377. repeat_interleave(input, repeats, dim=None, *, output_size=None) -> Tensor
  10378. Repeat elements of a tensor.
  10379. .. warning::
  10380. This is different from :meth:`torch.Tensor.repeat` but similar to ``numpy.repeat``.
  10381. Args:
  10382. {input}
  10383. repeats (Tensor or int): The number of repetitions for each element.
  10384. repeats is broadcasted to fit the shape of the given axis.
  10385. dim (int, optional): The dimension along which to repeat values.
  10386. By default, use the flattened input array, and return a flat output
  10387. array.
  10388. Keyword args:
  10389. output_size (int, optional): Total output size for the given axis
  10390. ( e.g. sum of repeats). If given, it will avoid stream synchronization
  10391. needed to calculate output shape of the tensor.
  10392. Returns:
  10393. Tensor: Repeated tensor which has the same shape as input, except along the given axis.
  10394. Example::
  10395. >>> x = torch.tensor([1, 2, 3])
  10396. >>> x.repeat_interleave(2)
  10397. tensor([1, 1, 2, 2, 3, 3])
  10398. >>> y = torch.tensor([[1, 2], [3, 4]])
  10399. >>> torch.repeat_interleave(y, 2)
  10400. tensor([1, 1, 2, 2, 3, 3, 4, 4])
  10401. >>> torch.repeat_interleave(y, 3, dim=1)
  10402. tensor([[1, 1, 1, 2, 2, 2],
  10403. [3, 3, 3, 4, 4, 4]])
  10404. >>> torch.repeat_interleave(y, torch.tensor([1, 2]), dim=0)
  10405. tensor([[1, 2],
  10406. [3, 4],
  10407. [3, 4]])
  10408. >>> torch.repeat_interleave(y, torch.tensor([1, 2]), dim=0, output_size=3)
  10409. tensor([[1, 2],
  10410. [3, 4],
  10411. [3, 4]])
  10412. If the `repeats` is `tensor([n1, n2, n3, ...])`, then the output will be
  10413. `tensor([0, 0, ..., 1, 1, ..., 2, 2, ..., ...])` where `0` appears `n1` times,
  10414. `1` appears `n2` times, `2` appears `n3` times, etc.
  10415. .. function:: repeat_interleave(repeats, *) -> Tensor
  10416. :noindex:
  10417. Repeats 0 repeats[0] times, 1 repeats[1] times, 2 repeats[2] times, etc.
  10418. Args:
  10419. repeats (Tensor): The number of repetitions for each element.
  10420. Returns:
  10421. Tensor: Repeated tensor of size `sum(repeats)`.
  10422. Example::
  10423. >>> torch.repeat_interleave(torch.tensor([1, 2, 3]))
  10424. tensor([0, 1, 1, 2, 2, 2])
  10425. """.format(**common_args),
  10426. )
  10427. add_docstr(
  10428. torch.tile,
  10429. r"""
  10430. tile(input, dims) -> Tensor
  10431. Constructs a tensor by repeating the elements of :attr:`input`.
  10432. The :attr:`dims` argument specifies the number of repetitions
  10433. in each dimension.
  10434. If :attr:`dims` specifies fewer dimensions than :attr:`input` has, then
  10435. ones are prepended to :attr:`dims` until all dimensions are specified.
  10436. For example, if :attr:`input` has shape (8, 6, 4, 2) and :attr:`dims`
  10437. is (2, 2), then :attr:`dims` is treated as (1, 1, 2, 2).
  10438. Analogously, if :attr:`input` has fewer dimensions than :attr:`dims`
  10439. specifies, then :attr:`input` is treated as if it were unsqueezed at
  10440. dimension zero until it has as many dimensions as :attr:`dims` specifies.
  10441. For example, if :attr:`input` has shape (4, 2) and :attr:`dims`
  10442. is (3, 3, 2, 2), then :attr:`input` is treated as if it had the
  10443. shape (1, 1, 4, 2).
  10444. .. note::
  10445. This function is similar to NumPy's tile function.
  10446. Args:
  10447. input (Tensor): the tensor whose elements to repeat.
  10448. dims (tuple): the number of repetitions per dimension.
  10449. Example::
  10450. >>> x = torch.tensor([1, 2, 3])
  10451. >>> x.tile((2,))
  10452. tensor([1, 2, 3, 1, 2, 3])
  10453. >>> y = torch.tensor([[1, 2], [3, 4]])
  10454. >>> torch.tile(y, (2, 2))
  10455. tensor([[1, 2, 1, 2],
  10456. [3, 4, 3, 4],
  10457. [1, 2, 1, 2],
  10458. [3, 4, 3, 4]])
  10459. """,
  10460. )
  10461. add_docstr(
  10462. torch.quantize_per_tensor,
  10463. r"""
  10464. quantize_per_tensor(input, scale, zero_point, dtype) -> Tensor
  10465. Converts a float tensor to a quantized tensor with given scale and zero point.
  10466. Arguments:
  10467. input (Tensor): float tensor or list of tensors to quantize
  10468. scale (float or Tensor): scale to apply in quantization formula
  10469. zero_point (int or Tensor): offset in integer value that maps to float zero
  10470. dtype (:class:`torch.dtype`): the desired data type of returned tensor.
  10471. Has to be one of the quantized dtypes: ``torch.quint8``, ``torch.qint8``, ``torch.qint32``
  10472. Returns:
  10473. Tensor: A newly quantized tensor or list of quantized tensors.
  10474. Example::
  10475. >>> torch.quantize_per_tensor(torch.tensor([-1.0, 0.0, 1.0, 2.0]), 0.1, 10, torch.quint8)
  10476. tensor([-1., 0., 1., 2.], size=(4,), dtype=torch.quint8,
  10477. quantization_scheme=torch.per_tensor_affine, scale=0.1, zero_point=10)
  10478. >>> torch.quantize_per_tensor(torch.tensor([-1.0, 0.0, 1.0, 2.0]), 0.1, 10, torch.quint8).int_repr()
  10479. tensor([ 0, 10, 20, 30], dtype=torch.uint8)
  10480. >>> torch.quantize_per_tensor([torch.tensor([-1.0, 0.0]), torch.tensor([-2.0, 2.0])],
  10481. >>> torch.tensor([0.1, 0.2]), torch.tensor([10, 20]), torch.quint8)
  10482. (tensor([-1., 0.], size=(2,), dtype=torch.quint8,
  10483. quantization_scheme=torch.per_tensor_affine, scale=0.1, zero_point=10),
  10484. tensor([-2., 2.], size=(2,), dtype=torch.quint8,
  10485. quantization_scheme=torch.per_tensor_affine, scale=0.2, zero_point=20))
  10486. >>> torch.quantize_per_tensor(torch.tensor([-1.0, 0.0, 1.0, 2.0]), torch.tensor(0.1), torch.tensor(10), torch.quint8)
  10487. tensor([-1., 0., 1., 2.], size=(4,), dtype=torch.quint8,
  10488. quantization_scheme=torch.per_tensor_affine, scale=0.10, zero_point=10)
  10489. """,
  10490. )
  10491. add_docstr(
  10492. torch.quantize_per_tensor_dynamic,
  10493. r"""
  10494. quantize_per_tensor_dynamic(input, dtype, reduce_range) -> Tensor
  10495. Converts a float tensor to a quantized tensor with scale and zero_point calculated
  10496. dynamically based on the input.
  10497. Arguments:
  10498. input (Tensor): float tensor or list of tensors to quantize
  10499. dtype (:class:`torch.dtype`): the desired data type of returned tensor.
  10500. Has to be one of the quantized dtypes: ``torch.quint8``, ``torch.qint8``
  10501. reduce_range (bool): a flag to indicate whether to reduce the range of quantized
  10502. data by 1 bit, it's required to avoid instruction overflow for some hardwares
  10503. Returns:
  10504. Tensor: A newly (dynamically) quantized tensor
  10505. Example::
  10506. >>> t = torch.quantize_per_tensor_dynamic(torch.tensor([-1.0, 0.0, 1.0, 2.0]), torch.quint8, False)
  10507. >>> print(t)
  10508. tensor([-1., 0., 1., 2.], size=(4,), dtype=torch.quint8,
  10509. quantization_scheme=torch.per_tensor_affine, scale=0.011764705882352941,
  10510. zero_point=85)
  10511. >>> t.int_repr()
  10512. tensor([ 0, 85, 170, 255], dtype=torch.uint8)
  10513. """,
  10514. )
  10515. add_docstr(
  10516. torch.quantize_per_channel,
  10517. r"""
  10518. quantize_per_channel(input, scales, zero_points, axis, dtype) -> Tensor
  10519. Converts a float tensor to a per-channel quantized tensor with given scales and zero points.
  10520. Arguments:
  10521. input (Tensor): float tensor to quantize
  10522. scales (Tensor): float 1D tensor of scales to use, size should match ``input.size(axis)``
  10523. zero_points (int): integer 1D tensor of offset to use, size should match ``input.size(axis)``
  10524. axis (int): dimension on which apply per-channel quantization
  10525. dtype (:class:`torch.dtype`): the desired data type of returned tensor.
  10526. Has to be one of the quantized dtypes: ``torch.quint8``, ``torch.qint8``, ``torch.qint32``
  10527. Returns:
  10528. Tensor: A newly quantized tensor
  10529. Example::
  10530. >>> x = torch.tensor([[-1.0, 0.0], [1.0, 2.0]])
  10531. >>> torch.quantize_per_channel(x, torch.tensor([0.1, 0.01]), torch.tensor([10, 0]), 0, torch.quint8)
  10532. tensor([[-1., 0.],
  10533. [ 1., 2.]], size=(2, 2), dtype=torch.quint8,
  10534. quantization_scheme=torch.per_channel_affine,
  10535. scale=tensor([0.1000, 0.0100], dtype=torch.float64),
  10536. zero_point=tensor([10, 0]), axis=0)
  10537. >>> torch.quantize_per_channel(x, torch.tensor([0.1, 0.01]), torch.tensor([10, 0]), 0, torch.quint8).int_repr()
  10538. tensor([[ 0, 10],
  10539. [100, 200]], dtype=torch.uint8)
  10540. """,
  10541. )
  10542. add_docstr(
  10543. torch.quantized_batch_norm,
  10544. r"""
  10545. quantized_batch_norm(input, weight=None, bias=None, mean, var, eps, output_scale, output_zero_point) -> Tensor
  10546. Applies batch normalization on a 4D (NCHW) quantized tensor.
  10547. .. math::
  10548. y = \frac{x - \mathrm{E}[x]}{\sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta
  10549. Arguments:
  10550. input (Tensor): quantized tensor
  10551. weight (Tensor): float tensor that corresponds to the gamma, size C
  10552. bias (Tensor): float tensor that corresponds to the beta, size C
  10553. mean (Tensor): float mean value in batch normalization, size C
  10554. var (Tensor): float tensor for variance, size C
  10555. eps (float): a value added to the denominator for numerical stability.
  10556. output_scale (float): output quantized tensor scale
  10557. output_zero_point (int): output quantized tensor zero_point
  10558. Returns:
  10559. Tensor: A quantized tensor with batch normalization applied.
  10560. Example::
  10561. >>> qx = torch.quantize_per_tensor(torch.rand(2, 2, 2, 2), 1.5, 3, torch.quint8)
  10562. >>> torch.quantized_batch_norm(qx, torch.ones(2), torch.zeros(2), torch.rand(2), torch.rand(2), 0.00001, 0.2, 2)
  10563. tensor([[[[-0.2000, -0.2000],
  10564. [ 1.6000, -0.2000]],
  10565. [[-0.4000, -0.4000],
  10566. [-0.4000, 0.6000]]],
  10567. [[[-0.2000, -0.2000],
  10568. [-0.2000, -0.2000]],
  10569. [[ 0.6000, -0.4000],
  10570. [ 0.6000, -0.4000]]]], size=(2, 2, 2, 2), dtype=torch.quint8,
  10571. quantization_scheme=torch.per_tensor_affine, scale=0.2, zero_point=2)
  10572. """,
  10573. )
  10574. add_docstr(
  10575. torch.quantized_max_pool1d,
  10576. r"""
  10577. quantized_max_pool1d(input, kernel_size, stride=[], padding=0, dilation=1, ceil_mode=False) -> Tensor
  10578. Applies a 1D max pooling over an input quantized tensor composed of several input planes.
  10579. Arguments:
  10580. input (Tensor): quantized tensor
  10581. kernel_size (list of int): the size of the sliding window
  10582. stride (``list of int``, optional): the stride of the sliding window
  10583. padding (``list of int``, optional): padding to be added on both sides, must be >= 0 and <= kernel_size / 2
  10584. dilation (``list of int``, optional): The stride between elements within a sliding window, must be > 0. Default 1
  10585. ceil_mode (bool, optional): If True, will use ceil instead of floor to compute the output shape.
  10586. Defaults to False.
  10587. Returns:
  10588. Tensor: A quantized tensor with max_pool1d applied.
  10589. Example::
  10590. >>> qx = torch.quantize_per_tensor(torch.rand(2, 2), 1.5, 3, torch.quint8)
  10591. >>> torch.quantized_max_pool1d(qx, [2])
  10592. tensor([[0.0000],
  10593. [1.5000]], size=(2, 1), dtype=torch.quint8,
  10594. quantization_scheme=torch.per_tensor_affine, scale=1.5, zero_point=3)
  10595. """,
  10596. )
  10597. add_docstr(
  10598. torch.quantized_max_pool2d,
  10599. r"""
  10600. quantized_max_pool2d(input, kernel_size, stride=[], padding=0, dilation=1, ceil_mode=False) -> Tensor
  10601. Applies a 2D max pooling over an input quantized tensor composed of several input planes.
  10602. Arguments:
  10603. input (Tensor): quantized tensor
  10604. kernel_size (``list of int``): the size of the sliding window
  10605. stride (``list of int``, optional): the stride of the sliding window
  10606. padding (``list of int``, optional): padding to be added on both sides, must be >= 0 and <= kernel_size / 2
  10607. dilation (``list of int``, optional): The stride between elements within a sliding window, must be > 0. Default 1
  10608. ceil_mode (bool, optional): If True, will use ceil instead of floor to compute the output shape.
  10609. Defaults to False.
  10610. Returns:
  10611. Tensor: A quantized tensor with max_pool2d applied.
  10612. Example::
  10613. >>> qx = torch.quantize_per_tensor(torch.rand(2, 2, 2, 2), 1.5, 3, torch.quint8)
  10614. >>> torch.quantized_max_pool2d(qx, [2,2])
  10615. tensor([[[[1.5000]],
  10616. [[1.5000]]],
  10617. [[[0.0000]],
  10618. [[0.0000]]]], size=(2, 2, 1, 1), dtype=torch.quint8,
  10619. quantization_scheme=torch.per_tensor_affine, scale=1.5, zero_point=3)
  10620. """,
  10621. )
  10622. add_docstr(
  10623. torch.Stream,
  10624. r"""
  10625. Stream(device, *, priority) -> Stream
  10626. An in-order queue of executing the respective tasks asynchronously in first in first out (FIFO) order.
  10627. It can control or synchronize the execution of other Stream or block the current host thread to ensure
  10628. the correct task sequencing. It supports with statement as a context manager to ensure the operators
  10629. within the with block are running on the corresponding stream.
  10630. See in-depth description of the CUDA behavior at :ref:`cuda-semantics` for details
  10631. on the exact semantic that applies to all devices.
  10632. Arguments:
  10633. device (:class:`torch.device`, optional): the desired device for the Stream.
  10634. If not given, the current :ref:`accelerator<accelerators>` type will be used.
  10635. priority (int, optional): priority of the stream, should be 0 or negative, where negative
  10636. numbers indicate higher priority. By default, streams have priority 0.
  10637. Returns:
  10638. Stream: An torch.Stream object.
  10639. Example::
  10640. >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA)
  10641. >>> with torch.Stream(device='cuda') as s_cuda:
  10642. >>> a = torch.randn(10, 5, device='cuda')
  10643. >>> b = torch.randn(5, 10, device='cuda')
  10644. >>> c = torch.mm(a, b)
  10645. """,
  10646. )
  10647. add_docstr(
  10648. torch.Stream.query,
  10649. r"""
  10650. Stream.query() -> bool
  10651. Check if all the work submitted has been completed.
  10652. Returns:
  10653. bool: A boolean indicating if all kernels in this stream are completed.
  10654. Example::
  10655. >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA)
  10656. >>> s_cuda = torch.Stream(device='cuda')
  10657. >>> s_cuda.query()
  10658. True
  10659. """,
  10660. )
  10661. add_docstr(
  10662. torch.Stream.record_event,
  10663. r"""
  10664. Stream.record_event(event) -> Event
  10665. Record an event. En-queuing it into the Stream to allow further synchronization from the current point in the FIFO queue.
  10666. Arguments:
  10667. event (:class:`torch.Event`, optional): event to record. If not given, a new one will be allocated.
  10668. Returns:
  10669. Event: Recorded event.
  10670. Example::
  10671. >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA)
  10672. >>> s_cuda = torch.Stream(device='cuda')
  10673. >>> e_cuda = s_cuda.record_event()
  10674. """,
  10675. )
  10676. add_docstr(
  10677. torch.Stream.synchronize,
  10678. r"""
  10679. Stream.synchronize() -> None
  10680. Wait for all the kernels in this stream to complete.
  10681. Example::
  10682. >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA)
  10683. >>> s_cuda = torch.Stream(device='cuda')
  10684. >>> s_cuda.synchronize()
  10685. """,
  10686. )
  10687. add_docstr(
  10688. torch.Stream.wait_event,
  10689. r"""
  10690. Stream.wait_event(event) -> None
  10691. Make all future work submitted to the stream wait for an event.
  10692. Arguments:
  10693. event (:class:`torch.Event`): an event to wait for.
  10694. Example::
  10695. >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA)
  10696. >>> s1_cuda = torch.Stream(device='cuda')
  10697. >>> s2_cuda = torch.Stream(device='cuda')
  10698. >>> e_cuda = s1_cuda.record_event()
  10699. >>> s2_cuda.wait_event(e_cuda)
  10700. """,
  10701. )
  10702. add_docstr(
  10703. torch.Stream.wait_stream,
  10704. r"""
  10705. Stream.wait_stream(stream) -> None
  10706. Synchronize with another stream. All future work submitted to this stream will wait until all kernels
  10707. already submitted to the given stream are completed.
  10708. Arguments:
  10709. stream (:class:`torch.Stream`): a stream to synchronize.
  10710. Example::
  10711. >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA)
  10712. >>> s1_cuda = torch.Stream(device='cuda')
  10713. >>> s2_cuda = torch.Stream(device='cuda')
  10714. >>> s2_cuda.wait_stream(s1_cuda)
  10715. """,
  10716. )
  10717. add_docstr(
  10718. torch.Event,
  10719. r"""
  10720. Event(device=None, *, enable_timing=False, blocking=False, interprocess=False)
  10721. Query and record Stream status to identify or control dependencies across Stream and measure timing.
  10722. Arguments:
  10723. device (:class:`torch.device`, optional): the desired device for the Event.
  10724. If not given, the current :ref:`accelerator<accelerators>` type will be used.
  10725. enable_timing (bool, optional): indicates if the event should measure time (default: ``False``)
  10726. blocking (bool, optional): if ``True``, :meth:`wait` will be blocking (default: ``False``)
  10727. interprocess (bool): if ``True``, the event can be shared between processes (default: ``False``)
  10728. .. warning::
  10729. Both blocking and interprocess are not supported right now and are noops.
  10730. Returns:
  10731. Event: An torch.Event object.
  10732. Example::
  10733. >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA)
  10734. >>> event = torch.Event()
  10735. >>> e_cuda = torch.Event(device='cuda')
  10736. """,
  10737. )
  10738. add_docstr(
  10739. torch.Event.elapsed_time,
  10740. r"""
  10741. Event.elapsed_time(end_event) -> float
  10742. Returns the elapsed time in milliseconds between when this event and the :attr:`end_event` are
  10743. each recorded via :func:`torch.Stream.record_event`.
  10744. Arguments:
  10745. end_event (:class:`torch.Event`): The ending event has been recorded.
  10746. Returns:
  10747. float: Time between starting and ending event in milliseconds.
  10748. Example::
  10749. >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA)
  10750. >>> s_cuda = torch.Stream(device='cuda')
  10751. >>> e1_cuda = s_cuda.record_event()
  10752. >>> e2_cuda = s_cuda.record_event()
  10753. >>> ms = e1_cuda.elapsed_time(e2_cuda)
  10754. """,
  10755. )
  10756. add_docstr(
  10757. torch.Event.query,
  10758. r"""
  10759. Event.query() -> bool
  10760. Check if the stream where this event was recorded already moved past the point where the event was recorded.
  10761. Always returns ``True`` if the Event was not recorded.
  10762. Returns:
  10763. bool: A boolean indicating if all work currently captured by event has completed.
  10764. Example::
  10765. >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA)
  10766. >>> s_cuda = torch.Stream(device='cuda')
  10767. >>> e_cuda = s_cuda.record_event()
  10768. >>> e_cuda.query()
  10769. True
  10770. """,
  10771. )
  10772. add_docstr(
  10773. torch.Event.record,
  10774. r"""
  10775. Event.record(stream=None) -> None
  10776. Record the event in a given stream. The stream's device must match the event's device.
  10777. This function is equivalent to ``stream.record_event(self)``.
  10778. Arguments:
  10779. stream (:class:`torch.Stream`, optional): A stream to be recorded.
  10780. If not given, the current stream will be used.
  10781. Example::
  10782. >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA)
  10783. >>> e_cuda = torch.Event(device='cuda')
  10784. >>> e_cuda.record()
  10785. """,
  10786. )
  10787. add_docstr(
  10788. torch.Event.synchronize,
  10789. r"""
  10790. Event.synchronize() -> None
  10791. Wait for the event to complete. This prevents the CPU thread from proceeding until the event completes.
  10792. Example::
  10793. >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA)
  10794. >>> s_cuda = torch.Stream(device='cuda')
  10795. >>> e_cuda = s_cuda.record_event()
  10796. >>> e_cuda.synchronize()
  10797. """,
  10798. )
  10799. add_docstr(
  10800. torch.Event.wait,
  10801. r"""
  10802. Event.wait(stream=None) -> None
  10803. Make all future work submitted to the given stream wait for this event.
  10804. Arguments:
  10805. stream (:class:`torch.Stream`, optional): A stream to synchronize.
  10806. If not given, the current stream will be used.
  10807. Example::
  10808. >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA)
  10809. >>> s1_cuda = torch.Stream(device='cuda')
  10810. >>> s2_cuda = torch.Stream(device='cuda')
  10811. >>> e_cuda = s1_cuda.record()
  10812. >>> e_cuda.wait(s2)
  10813. """,
  10814. )
  10815. add_docstr(
  10816. torch.Generator,
  10817. r"""
  10818. Generator(device='cpu') -> Generator
  10819. Creates and returns a generator object that manages the state of the algorithm which
  10820. produces pseudo random numbers. Used as a keyword argument in many :ref:`inplace-random-sampling`
  10821. functions.
  10822. Arguments:
  10823. device (:class:`torch.device`, optional): the desired device for the generator.
  10824. Returns:
  10825. Generator: An torch.Generator object.
  10826. Example::
  10827. >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA)
  10828. >>> g_cpu = torch.Generator()
  10829. >>> g_cuda = torch.Generator(device='cuda')
  10830. """,
  10831. )
  10832. add_docstr(
  10833. torch.Generator.set_state,
  10834. r"""
  10835. Generator.set_state(new_state) -> void
  10836. Sets the Generator state.
  10837. Arguments:
  10838. new_state (torch.ByteTensor): The desired state.
  10839. Example::
  10840. >>> g_cpu = torch.Generator()
  10841. >>> g_cpu_other = torch.Generator()
  10842. >>> g_cpu.set_state(g_cpu_other.get_state())
  10843. """,
  10844. )
  10845. add_docstr(
  10846. torch.Generator.get_state,
  10847. r"""
  10848. Generator.get_state() -> Tensor
  10849. Returns the Generator state as a ``torch.ByteTensor``.
  10850. Returns:
  10851. Tensor: A ``torch.ByteTensor`` which contains all the necessary bits
  10852. to restore a Generator to a specific point in time.
  10853. Example::
  10854. >>> g_cpu = torch.Generator()
  10855. >>> g_cpu.get_state()
  10856. """,
  10857. )
  10858. add_docstr(
  10859. torch.Generator.graphsafe_set_state,
  10860. r"""
  10861. Generator.graphsafe_set_state(state) -> None
  10862. Sets the state of the generator to the specified state in a manner that is safe for use in graph capture.
  10863. This method is crucial for ensuring that the generator's state can be captured in the CUDA graph.
  10864. Arguments:
  10865. state (torch.Generator): A Generator point to the new state for the generator, typically obtained from `graphsafe_get_state`.
  10866. Example:
  10867. >>> g_cuda = torch.Generator(device='cuda')
  10868. >>> g_cuda_other = torch.Generator(device='cuda')
  10869. >>> current_state = g_cuda_other.graphsafe_get_state()
  10870. >>> g_cuda.graphsafe_set_state(current_state)
  10871. """,
  10872. )
  10873. add_docstr(
  10874. torch.Generator.graphsafe_get_state,
  10875. r"""
  10876. Generator.graphsafe_get_state() -> torch.Generator
  10877. Retrieves the current state of the generator in a manner that is safe for graph capture.
  10878. This method is crucial for ensuring that the generator's state can be captured in the CUDA graph.
  10879. Returns:
  10880. torch.Generator: A Generator point to the current state of the generator
  10881. Example:
  10882. >>> g_cuda = torch.Generator(device='cuda')
  10883. >>> current_state = g_cuda.graphsafe_get_state()
  10884. """,
  10885. )
  10886. add_docstr(
  10887. torch.Generator.clone_state,
  10888. r"""
  10889. Generator.clone_state() -> torch.Generator
  10890. Clones the current state of the generator and returns a new generator pointing to this cloned state.
  10891. This method is beneficial for preserving a particular state of a generator to restore at a later point.
  10892. Returns:
  10893. torch.Generator: A Generator pointing to the newly cloned state.
  10894. Example:
  10895. >>> g_cuda = torch.Generator(device='cuda')
  10896. >>> cloned_state = g_cuda.clone_state()
  10897. """,
  10898. )
  10899. add_docstr(
  10900. torch.Generator.manual_seed,
  10901. r"""
  10902. Generator.manual_seed(seed) -> Generator
  10903. Sets the seed for generating random numbers. Returns a `torch.Generator` object. Any 32-bit integer is a valid seed.
  10904. Arguments:
  10905. seed (int): The desired seed. Value must be within the inclusive range
  10906. `[-0x8000_0000_0000_0000, 0xffff_ffff_ffff_ffff]`. Otherwise, a RuntimeError
  10907. is raised. Negative inputs are remapped to positive values with the formula
  10908. `0xffff_ffff_ffff_ffff + seed`.
  10909. Returns:
  10910. Generator: An torch.Generator object.
  10911. Example::
  10912. >>> g_cpu = torch.Generator()
  10913. >>> g_cpu.manual_seed(2147483647)
  10914. """,
  10915. )
  10916. add_docstr(
  10917. torch.Generator.initial_seed,
  10918. r"""
  10919. Generator.initial_seed() -> int
  10920. Returns the initial seed for generating random numbers.
  10921. Example::
  10922. >>> g_cpu = torch.Generator()
  10923. >>> g_cpu.initial_seed()
  10924. 2147483647
  10925. """,
  10926. )
  10927. add_docstr(
  10928. torch.Generator.seed,
  10929. r"""
  10930. Generator.seed() -> int
  10931. Gets a non-deterministic random number from std::random_device or the current
  10932. time and uses it to seed a Generator.
  10933. Example::
  10934. >>> g_cpu = torch.Generator()
  10935. >>> g_cpu.seed()
  10936. 1516516984916
  10937. """,
  10938. )
  10939. add_docstr(
  10940. torch.Generator.device,
  10941. r"""
  10942. Generator.device -> device
  10943. Gets the current device of the generator.
  10944. Example::
  10945. >>> g_cpu = torch.Generator()
  10946. >>> g_cpu.device
  10947. device(type='cpu')
  10948. """,
  10949. )
  10950. add_docstr(
  10951. torch._assert_async,
  10952. r"""
  10953. _assert_async(tensor) -> void
  10954. Asynchronously assert that the contents of tensor are nonzero. For CPU tensors,
  10955. this is equivalent to ``assert tensor`` or ``assert tensor.is_nonzero()``; for
  10956. CUDA tensors, we DO NOT synchronize and you may only find out the assertion
  10957. failed at a later CUDA kernel launch. Asynchronous assertion can be helpful for
  10958. testing invariants in CUDA tensors without giving up performance. This function
  10959. is NOT intended to be used for regular error checking, as it will trash your CUDA
  10960. context if the assert fails (forcing you to restart your PyTorch process.)
  10961. Args:
  10962. tensor (Tensor): a one element tensor to test to see if it is nonzero. Zero
  10963. elements (including False for boolean tensors) cause an assertion failure
  10964. to be raised.
  10965. """,
  10966. )
  10967. add_docstr(
  10968. torch.searchsorted,
  10969. r"""
  10970. searchsorted(sorted_sequence, values, *, out_int32=False, right=False, side=None, out=None, sorter=None) -> Tensor
  10971. Find the indices from the *innermost* dimension of :attr:`sorted_sequence` such that, if the
  10972. corresponding values in :attr:`values` were inserted before the indices, when sorted, the order
  10973. of the corresponding *innermost* dimension within :attr:`sorted_sequence` would be preserved.
  10974. Return a new tensor with the same size as :attr:`values`. More formally,
  10975. the returned index satisfies the following rules:
  10976. .. list-table::
  10977. :widths: 12 10 78
  10978. :header-rows: 1
  10979. * - :attr:`sorted_sequence`
  10980. - :attr:`right`
  10981. - *returned index satisfies*
  10982. * - 1-D
  10983. - False
  10984. - ``sorted_sequence[i-1] < values[m][n]...[l][x] <= sorted_sequence[i]``
  10985. * - 1-D
  10986. - True
  10987. - ``sorted_sequence[i-1] <= values[m][n]...[l][x] < sorted_sequence[i]``
  10988. * - N-D
  10989. - False
  10990. - ``sorted_sequence[m][n]...[l][i-1] < values[m][n]...[l][x] <= sorted_sequence[m][n]...[l][i]``
  10991. * - N-D
  10992. - True
  10993. - ``sorted_sequence[m][n]...[l][i-1] <= values[m][n]...[l][x] < sorted_sequence[m][n]...[l][i]``
  10994. Args:
  10995. sorted_sequence (Tensor): N-D or 1-D tensor, containing monotonically increasing sequence on the *innermost*
  10996. dimension unless :attr:`sorter` is provided, in which case the sequence does not
  10997. need to be sorted
  10998. values (Tensor or Scalar): N-D tensor or a Scalar containing the search value(s).
  10999. Keyword args:
  11000. out_int32 (bool, optional): indicate the output data type. torch.int32 if True, torch.int64 otherwise.
  11001. Default value is False, i.e. default output data type is torch.int64.
  11002. right (bool, optional): if False, return the first suitable location that is found. If True, return the
  11003. last such index. If no suitable index found, return 0 for non-numerical value
  11004. (eg. nan, inf) or the size of *innermost* dimension within :attr:`sorted_sequence`
  11005. (one pass the last index of the *innermost* dimension). In other words, if False,
  11006. gets the lower bound index for each value in :attr:`values` on the corresponding
  11007. *innermost* dimension of the :attr:`sorted_sequence`. If True, gets the upper
  11008. bound index instead. Default value is False. :attr:`side` does the same and is
  11009. preferred. It will error if :attr:`side` is set to "left" while this is True.
  11010. side (str, optional): the same as :attr:`right` but preferred. "left" corresponds to False for :attr:`right`
  11011. and "right" corresponds to True for :attr:`right`. It will error if this is set to
  11012. "left" while :attr:`right` is True. Default value is None.
  11013. out (Tensor, optional): the output tensor, must be the same size as :attr:`values` if provided.
  11014. sorter (LongTensor, optional): if provided, a tensor matching the shape of the unsorted
  11015. :attr:`sorted_sequence` containing a sequence of indices that sort it in the
  11016. ascending order on the innermost dimension
  11017. Example::
  11018. >>> sorted_sequence = torch.tensor([[1, 3, 5, 7, 9], [2, 4, 6, 8, 10]])
  11019. >>> sorted_sequence
  11020. tensor([[ 1, 3, 5, 7, 9],
  11021. [ 2, 4, 6, 8, 10]])
  11022. >>> values = torch.tensor([[3, 6, 9], [3, 6, 9]])
  11023. >>> values
  11024. tensor([[3, 6, 9],
  11025. [3, 6, 9]])
  11026. >>> torch.searchsorted(sorted_sequence, values)
  11027. tensor([[1, 3, 4],
  11028. [1, 2, 4]])
  11029. >>> torch.searchsorted(sorted_sequence, values, side='right')
  11030. tensor([[2, 3, 5],
  11031. [1, 3, 4]])
  11032. >>> sorted_sequence_1d = torch.tensor([1, 3, 5, 7, 9])
  11033. >>> sorted_sequence_1d
  11034. tensor([1, 3, 5, 7, 9])
  11035. >>> torch.searchsorted(sorted_sequence_1d, values)
  11036. tensor([[1, 3, 4],
  11037. [1, 3, 4]])
  11038. """,
  11039. )
  11040. add_docstr(
  11041. torch.bucketize,
  11042. r"""
  11043. bucketize(input, boundaries, *, out_int32=False, right=False, out=None) -> Tensor
  11044. Returns the indices of the buckets to which each value in the :attr:`input` belongs, where the
  11045. boundaries of the buckets are set by :attr:`boundaries`. Return a new tensor with the same size
  11046. as :attr:`input`. If :attr:`right` is False (default), then the left boundary is open. Note that
  11047. this behavior is opposite the behavior of
  11048. `numpy.digitize <https://numpy.org/doc/stable/reference/generated/numpy.digitize.html>`_.
  11049. More formally, the returned index satisfies the following rules:
  11050. .. list-table::
  11051. :widths: 15 85
  11052. :header-rows: 1
  11053. * - :attr:`right`
  11054. - *returned index satisfies*
  11055. * - False
  11056. - ``boundaries[i-1] < input[m][n]...[l][x] <= boundaries[i]``
  11057. * - True
  11058. - ``boundaries[i-1] <= input[m][n]...[l][x] < boundaries[i]``
  11059. Args:
  11060. input (Tensor or Scalar): N-D tensor or a Scalar containing the search value(s).
  11061. boundaries (Tensor): 1-D tensor, must contain a strictly increasing sequence, or the return value is undefined.
  11062. Keyword args:
  11063. out_int32 (bool, optional): indicate the output data type. torch.int32 if True, torch.int64 otherwise.
  11064. Default value is False, i.e. default output data type is torch.int64.
  11065. right (bool, optional): determines the behavior for values in :attr:`boundaries`. See the table above.
  11066. out (Tensor, optional): the output tensor, must be the same size as :attr:`input` if provided.
  11067. Example::
  11068. >>> boundaries = torch.tensor([1, 3, 5, 7, 9])
  11069. >>> boundaries
  11070. tensor([1, 3, 5, 7, 9])
  11071. >>> v = torch.tensor([[3, 6, 9], [3, 6, 9]])
  11072. >>> v
  11073. tensor([[3, 6, 9],
  11074. [3, 6, 9]])
  11075. >>> torch.bucketize(v, boundaries)
  11076. tensor([[1, 3, 4],
  11077. [1, 3, 4]])
  11078. >>> torch.bucketize(v, boundaries, right=True)
  11079. tensor([[2, 3, 5],
  11080. [2, 3, 5]])
  11081. """,
  11082. )
  11083. add_docstr(
  11084. torch.view_as_real_copy,
  11085. r"""
  11086. Performs the same operation as :func:`torch.view_as_real`, but all output tensors
  11087. are freshly created instead of aliasing the input.
  11088. """,
  11089. )
  11090. add_docstr(
  11091. torch.view_as_complex_copy,
  11092. r"""
  11093. Performs the same operation as :func:`torch.view_as_complex`, but all output tensors
  11094. are freshly created instead of aliasing the input.
  11095. """,
  11096. )
  11097. add_docstr(
  11098. torch.as_strided_copy,
  11099. r"""
  11100. Performs the same operation as :func:`torch.as_strided`, but all output tensors
  11101. are freshly created instead of aliasing the input.
  11102. """,
  11103. )
  11104. add_docstr(
  11105. torch.diagonal_copy,
  11106. r"""
  11107. Performs the same operation as :func:`torch.diagonal`, but all output tensors
  11108. are freshly created instead of aliasing the input.
  11109. """,
  11110. )
  11111. add_docstr(
  11112. torch.expand_copy,
  11113. r"""
  11114. Performs the same operation as :func:`torch.Tensor.expand`, but all output tensors
  11115. are freshly created instead of aliasing the input.
  11116. """,
  11117. )
  11118. add_docstr(
  11119. torch.permute_copy,
  11120. r"""
  11121. Performs the same operation as :func:`torch.permute`, but all output tensors
  11122. are freshly created instead of aliasing the input.
  11123. """,
  11124. )
  11125. add_docstr(
  11126. torch.select_copy,
  11127. r"""
  11128. Performs the same operation as :func:`torch.select`, but all output tensors
  11129. are freshly created instead of aliasing the input.
  11130. """,
  11131. )
  11132. add_docstr(
  11133. torch.detach_copy,
  11134. r"""
  11135. Performs the same operation as :func:`torch.detach`, but all output tensors
  11136. are freshly created instead of aliasing the input.
  11137. """,
  11138. )
  11139. add_docstr(
  11140. torch.slice_copy,
  11141. r"""
  11142. Performs the same operation as :func:`torch.slice`, but all output tensors
  11143. are freshly created instead of aliasing the input.
  11144. """,
  11145. )
  11146. add_docstr(
  11147. torch.split_copy,
  11148. r"""
  11149. Performs the same operation as :func:`torch.split`, but all output tensors
  11150. are freshly created instead of aliasing the input.
  11151. """,
  11152. )
  11153. add_docstr(
  11154. torch.split_with_sizes_copy,
  11155. r"""
  11156. Performs the same operation as :func:`torch.split_with_sizes`, but all output tensors
  11157. are freshly created instead of aliasing the input.
  11158. """,
  11159. )
  11160. add_docstr(
  11161. torch.squeeze_copy,
  11162. r"""
  11163. Performs the same operation as :func:`torch.squeeze`, but all output tensors
  11164. are freshly created instead of aliasing the input.
  11165. """,
  11166. )
  11167. add_docstr(
  11168. torch.t_copy,
  11169. r"""
  11170. Performs the same operation as :func:`torch.t`, but all output tensors
  11171. are freshly created instead of aliasing the input.
  11172. """,
  11173. )
  11174. add_docstr(
  11175. torch.transpose_copy,
  11176. r"""
  11177. Performs the same operation as :func:`torch.transpose`, but all output tensors
  11178. are freshly created instead of aliasing the input.
  11179. """,
  11180. )
  11181. add_docstr(
  11182. torch.unsqueeze_copy,
  11183. r"""
  11184. Performs the same operation as :func:`torch.unsqueeze`, but all output tensors
  11185. are freshly created instead of aliasing the input.
  11186. """,
  11187. )
  11188. add_docstr(
  11189. torch.indices_copy,
  11190. r"""
  11191. Performs the same operation as :func:`torch.indices`, but all output tensors
  11192. are freshly created instead of aliasing the input.
  11193. """,
  11194. )
  11195. add_docstr(
  11196. torch.values_copy,
  11197. r"""
  11198. Performs the same operation as :func:`torch.values`, but all output tensors
  11199. are freshly created instead of aliasing the input.
  11200. """,
  11201. )
  11202. add_docstr(
  11203. torch.crow_indices_copy,
  11204. r"""
  11205. Performs the same operation as :func:`torch.crow_indices`, but all output tensors
  11206. are freshly created instead of aliasing the input.
  11207. """,
  11208. )
  11209. add_docstr(
  11210. torch.col_indices_copy,
  11211. r"""
  11212. Performs the same operation as :func:`torch.col_indices`, but all output tensors
  11213. are freshly created instead of aliasing the input.
  11214. """,
  11215. )
  11216. add_docstr(
  11217. torch.unbind_copy,
  11218. r"""
  11219. Performs the same operation as :func:`torch.unbind`, but all output tensors
  11220. are freshly created instead of aliasing the input.
  11221. """,
  11222. )
  11223. add_docstr(
  11224. torch.view_copy,
  11225. r"""
  11226. Performs the same operation as :func:`torch.view`, but all output tensors
  11227. are freshly created instead of aliasing the input.
  11228. """,
  11229. )
  11230. add_docstr(
  11231. torch.unfold_copy,
  11232. r"""
  11233. Performs the same operation as :func:`torch.unfold`, but all output tensors
  11234. are freshly created instead of aliasing the input.
  11235. """,
  11236. )
  11237. add_docstr(
  11238. torch.alias_copy,
  11239. r"""
  11240. Performs the same operation as :func:`torch.alias`, but all output tensors
  11241. are freshly created instead of aliasing the input.
  11242. """,
  11243. )
  11244. for unary_base_func_name in (
  11245. "exp",
  11246. "sqrt",
  11247. "abs",
  11248. "acos",
  11249. "asin",
  11250. "atan",
  11251. "ceil",
  11252. "cos",
  11253. "cosh",
  11254. "erf",
  11255. "erfc",
  11256. "expm1",
  11257. "floor",
  11258. "log",
  11259. "log10",
  11260. "log1p",
  11261. "log2",
  11262. "neg",
  11263. "tan",
  11264. "tanh",
  11265. "sin",
  11266. "sinh",
  11267. "round",
  11268. "lgamma",
  11269. "frac",
  11270. "reciprocal",
  11271. "sigmoid",
  11272. "trunc",
  11273. "zero",
  11274. ):
  11275. unary_foreach_func_name = f"_foreach_{unary_base_func_name}"
  11276. if hasattr(torch, unary_foreach_func_name):
  11277. add_docstr(
  11278. getattr(torch, unary_foreach_func_name),
  11279. rf"""
  11280. {unary_foreach_func_name}(self: List[Tensor]) -> List[Tensor]
  11281. Apply :func:`torch.{unary_base_func_name}` to each Tensor of the input list.
  11282. """,
  11283. )
  11284. unary_inplace_foreach_func_name = f"{unary_foreach_func_name}_"
  11285. if hasattr(torch, unary_inplace_foreach_func_name):
  11286. add_docstr(
  11287. getattr(torch, unary_inplace_foreach_func_name),
  11288. rf"""
  11289. {unary_inplace_foreach_func_name}(self: List[Tensor]) -> None
  11290. Apply :func:`torch.{unary_base_func_name}` to each Tensor of the input list.
  11291. """,
  11292. )