python.js 372 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619
  1. // Experimental Python Execution
  2. const python = {};
  3. python.Parser = class {
  4. constructor(text, file, debug) {
  5. this._tokenizer = new python.Tokenizer(text, file);
  6. this._debug = debug;
  7. python.Parser._precedence = python.Parser._precedence || {
  8. 'or': 2, 'and': 3, 'not' : 4,
  9. 'in': 5, 'instanceof': 5, 'is': 5, '<': 5, '>': 5, '<=': 5, '>=': 5, '<>': 5, '==': 5, '!=': 5,
  10. '|': 6, '^' : 7, '&' : 8,
  11. '<<': 9, '>>': 9, '+': 10, '-': 10, '*': 11, '@': 11, '/': 11, '//': 11, '%': 11,
  12. // '+': 12, '-': 12,
  13. '~': 13, '**': 14
  14. };
  15. }
  16. parse() {
  17. const node = this._node('program');
  18. node.body = [];
  19. while (!this._tokenizer.match('eof')) {
  20. const statement = this._statement();
  21. if (statement) {
  22. node.body.push(statement);
  23. continue;
  24. }
  25. if (this._tokenizer.eat('\n') || this._tokenizer.eat(';') || this._tokenizer.peek().type == 'eof') {
  26. continue;
  27. }
  28. if (this._tokenizer.eat('indent') && this._tokenizer.peek().type == 'eof') {
  29. continue;
  30. }
  31. throw new python.Error(`Unsupported statement ${this._tokenizer.location()}`);
  32. }
  33. return node;
  34. }
  35. _suite() {
  36. const node = this._node('block');
  37. node.statements = [];
  38. let statement = null;
  39. if (this._tokenizer.eat('\n')) {
  40. if (this._tokenizer.eat('indent')) {
  41. while (!this._tokenizer.eat('eof') && !this._tokenizer.eat('dedent')) {
  42. if (this._tokenizer.eat(';')) {
  43. continue;
  44. }
  45. statement = this._statement();
  46. if (statement) {
  47. node.statements.push(statement);
  48. continue;
  49. }
  50. if (this._tokenizer.eat('\n')) {
  51. continue;
  52. }
  53. if (this._tokenizer.match('dedent') || this._tokenizer.match('eof')) {
  54. continue;
  55. }
  56. throw new python.Error(`Empty statement ${this._tokenizer.location()}`);
  57. }
  58. }
  59. } else if (!this._tokenizer.eat('eof')) {
  60. while (!this._tokenizer.match('\n') && !this._tokenizer.match('eof') && !this._tokenizer.match('dedent')) {
  61. if (this._tokenizer.eat(';')) {
  62. continue;
  63. }
  64. statement = this._statement();
  65. if (statement) {
  66. node.statements.push(statement);
  67. continue;
  68. }
  69. throw new python.Error(`Empty statement ${this._tokenizer.location()}`);
  70. }
  71. this._tokenizer.eat('\n');
  72. }
  73. return node;
  74. }
  75. _statement() {
  76. let node = this._eat('id', 'break');
  77. if (node) {
  78. return node;
  79. }
  80. node = this._eat('id', 'continue');
  81. if (node) {
  82. return node;
  83. }
  84. node = this._eat('id', 'return');
  85. if (node) {
  86. node.expression = this._expression(-1, [], true);
  87. return node;
  88. }
  89. node = this._eat('id', 'raise');
  90. if (node) {
  91. node.exception = this._expression(-1, [ 'from' ]);
  92. if (this._tokenizer.eat('id', 'from')) {
  93. node.from = this._expression();
  94. } else if (this._tokenizer.eat(',')) {
  95. node.exception = [ node.exception ];
  96. node.exception.push(this._expression());
  97. if (this._tokenizer.eat(',')) {
  98. node.exception.push(this._expression());
  99. }
  100. }
  101. return node;
  102. }
  103. node = this._eat('id', 'assert');
  104. if (node) {
  105. node.condition = this._expression(-1, [ ',' ]);
  106. if (this._tokenizer.eat(',')) {
  107. node.message = this._expression();
  108. }
  109. return node;
  110. }
  111. node = this._eat('id', 'exec');
  112. if (node) {
  113. node.variable = this._expression(-1, [ 'in' ]);
  114. if (this._tokenizer.eat('in')) {
  115. do {
  116. node.target = node.target || [];
  117. node.target.push(this._expression(-1, [ 'in' ], false));
  118. }
  119. while (this._tokenizer.eat(','));
  120. }
  121. return node;
  122. }
  123. node = this._eat('id', 'global');
  124. if (node) {
  125. node.names = [];
  126. do {
  127. node.names.push(this._name(true).value);
  128. }
  129. while (this._tokenizer.eat(','));
  130. return node;
  131. }
  132. node = this._eat('id', 'nonlocal');
  133. if (node) {
  134. node.names = [];
  135. do {
  136. node.names.push(this._name(true).value);
  137. }
  138. while (this._tokenizer.eat(','));
  139. return node;
  140. }
  141. node = this._eat('id', 'import');
  142. if (node) {
  143. node.names = [];
  144. do {
  145. const alias = this._node('alias');
  146. alias.name = this._dottedName();
  147. if (this._tokenizer.eat('id', 'as')) {
  148. alias.asname = this._name(true).value;
  149. }
  150. node.names.push(alias);
  151. }
  152. while (this._tokenizer.eat(','));
  153. return node;
  154. }
  155. node = this._eat('id', 'from');
  156. if (node) {
  157. node.type = 'import_from';
  158. node.level = 0;
  159. const dots = this._tokenizer.peek();
  160. if (dots && Array.from(dots.type).every((c) => c == '.')) {
  161. this._eat(dots.type);
  162. node.level = Array.from(dots.type).length;
  163. }
  164. node.module = this._dottedName();
  165. this._tokenizer.expect('id', 'import');
  166. node.names = [];
  167. const close = this._tokenizer.eat('(');
  168. do {
  169. const alias = this._node('alias');
  170. alias.name = this._name(true).value;
  171. if (this._tokenizer.eat('id', 'as')) {
  172. alias.asname = this._name(true).value;
  173. }
  174. node.names.push(alias);
  175. }
  176. while (this._tokenizer.eat(','));
  177. if (close) {
  178. this._tokenizer.expect(')');
  179. }
  180. return node;
  181. }
  182. let decorator_list = this._decorator();
  183. node = this._eat('id', 'class');
  184. if (node) {
  185. node.name = this._name(true).value;
  186. if (decorator_list) {
  187. node.decorator_list = Array.from(decorator_list);
  188. decorator_list = null;
  189. }
  190. node.bases = this._tokenizer.peek().type === '(' ? this._arguments() : [];
  191. this._tokenizer.expect(':');
  192. node.body = this._suite();
  193. return node;
  194. }
  195. const async = this._eat('id', 'async');
  196. if (async &&
  197. !this._tokenizer.match('id', 'def') &&
  198. !this._tokenizer.match('id', 'with') &&
  199. !this._tokenizer.match('id', 'for')) {
  200. throw new python.Error(`Expected 'def', 'with' or 'for' ${this._tokenizer.location()}`);
  201. }
  202. node = this._eat('id', 'def');
  203. if (node) {
  204. if (async) {
  205. node.async = async;
  206. }
  207. node.name = this._name(true).value;
  208. if (decorator_list) {
  209. node.decorator_list = Array.from(decorator_list);
  210. decorator_list = null;
  211. }
  212. this._tokenizer.expect('(');
  213. node.parameters = this._parameters(')');
  214. if (this._tokenizer.eat('->')) {
  215. node.returnType = this._type();
  216. }
  217. this._tokenizer.expect(':');
  218. node.body = this._suite();
  219. return node;
  220. }
  221. if (decorator_list && decorator_list.length > 0) {
  222. throw new python.Error('Unexpected decorator.');
  223. }
  224. node = this._eat('id', 'del');
  225. if (node) {
  226. node.expression = this._expression(-1, [], true);
  227. return node;
  228. }
  229. node = this._eat('id', 'print');
  230. if (node) {
  231. node.expression = this._expression(-1, [], true);
  232. return node;
  233. }
  234. node = this._eat('id', 'if');
  235. if (node) {
  236. node.condition = this._expression();
  237. this._tokenizer.expect(':');
  238. node.then = this._suite();
  239. let current = node;
  240. this._tokenizer.eat('\n');
  241. while (this._tokenizer.eat('id', 'elif')) {
  242. current.else = this._node('if');
  243. current = current.else;
  244. current.condition = this._expression();
  245. this._tokenizer.expect(':');
  246. current.then = this._suite();
  247. this._tokenizer.eat('\n');
  248. }
  249. if (this._tokenizer.eat('id', 'else')) {
  250. this._tokenizer.expect(':');
  251. current.else = this._suite();
  252. }
  253. return node;
  254. }
  255. node = this._eat('id', 'while');
  256. if (node) {
  257. node.condition = this._expression();
  258. this._tokenizer.expect(':');
  259. node.body = this._suite();
  260. if (this._tokenizer.eat('id', 'else')) {
  261. this._tokenizer.expect(':');
  262. node.else = this._suite();
  263. }
  264. return node;
  265. }
  266. node = this._eat('id', 'pass');
  267. if (node) {
  268. return node;
  269. }
  270. node = this._eat('id', 'for');
  271. if (node) {
  272. node.variable = [];
  273. node.variable.push(this._expression(-1, [ 'in' ]));
  274. while (this._tokenizer.eat(',')) {
  275. if (this._tokenizer.match('id', 'in')) {
  276. node.variable.push({});
  277. break;
  278. }
  279. node.variable.push(this._expression(-1, [ 'in' ]));
  280. }
  281. this._tokenizer.expect('id', 'in');
  282. node.target = [];
  283. node.target.push(this._expression());
  284. while (this._tokenizer.eat(',')) {
  285. if (this._tokenizer.match(':')) {
  286. node.target.push({});
  287. break;
  288. }
  289. node.target.push(this._expression(-1, [ 'in' ]));
  290. }
  291. this._tokenizer.expect(':');
  292. node.body = this._suite();
  293. if (this._tokenizer.eat('id', 'else')) {
  294. this._tokenizer.expect(':');
  295. node.else = this._suite();
  296. }
  297. return node;
  298. }
  299. node = this._eat('id', 'with');
  300. if (node) {
  301. if (async) {
  302. node.async = async;
  303. }
  304. node.item = [];
  305. do {
  306. const item = this._node();
  307. item.type = 'with_item';
  308. item.expression = this._expression();
  309. if (this._tokenizer.eat('id', 'as')) {
  310. item.variable = this._expression();
  311. }
  312. node.item.push(item);
  313. }
  314. while (this._tokenizer.eat(','));
  315. this._tokenizer.expect(':');
  316. node.body = this._suite();
  317. return node;
  318. }
  319. node = this._eat('id', 'try');
  320. if (node) {
  321. this._tokenizer.expect(':');
  322. node.body = this._suite();
  323. node.except = [];
  324. while (this._tokenizer.match('id', 'except')) {
  325. const except = this._node('except');
  326. this._tokenizer.expect('id', 'except');
  327. except.clause = [];
  328. except.clause.push(this._expression());
  329. while (this._tokenizer.eat(',')) {
  330. if (this._tokenizer.match(':') || this._tokenizer.match('as')) {
  331. except.clause.push({});
  332. break;
  333. }
  334. except.clause.push(this._expression());
  335. }
  336. if (this._tokenizer.eat('id', 'as')) {
  337. except.variable = this._expression();
  338. }
  339. this._tokenizer.expect(':');
  340. except.body = this._suite();
  341. node.except.push(except);
  342. }
  343. if (this._tokenizer.match('id', 'else')) {
  344. node.else = this._node('else');
  345. this._tokenizer.expect('id', 'else');
  346. this._tokenizer.expect(':');
  347. node.else.body = this._suite();
  348. }
  349. if (this._tokenizer.match('id', 'finally')) {
  350. node.finally = this._node('finally');
  351. this._tokenizer.expect('id', 'finally');
  352. this._tokenizer.expect(':');
  353. node.finally.body = this._suite();
  354. }
  355. return node;
  356. }
  357. const expression = this._expression(-1, [], true);
  358. if (expression) {
  359. if (expression.type == 'id' && this._tokenizer.eat(':')) {
  360. node = this._node('var');
  361. node.name = expression.value;
  362. node.location = expression.location;
  363. node.variableType = this._expression(-1, [ '=' ]);
  364. if (this._tokenizer.eat('=')) {
  365. node.initializer = this._expression();
  366. }
  367. return node;
  368. }
  369. switch (expression.type) {
  370. case '=':
  371. case ':=':
  372. case '==':
  373. case '!=':
  374. case '+=':
  375. case '-=':
  376. case '*=':
  377. case '@=':
  378. case '/=':
  379. case '//=':
  380. case '**=':
  381. case '&=':
  382. case '|=':
  383. case '%=':
  384. case '>>=':
  385. case '<<=':
  386. case '>>':
  387. case '<<':
  388. case '>=':
  389. case '<=':
  390. case '<':
  391. case '>':
  392. case '%':
  393. case '^=':
  394. case '...':
  395. case 'call':
  396. case 'assert':
  397. case 'raise':
  398. case 'string':
  399. case 'list':
  400. case 'var':
  401. case '.':
  402. case '[]':
  403. case 'yield':
  404. case '+':
  405. case '-':
  406. case '*':
  407. case '**':
  408. case '@':
  409. case '/':
  410. case '//':
  411. case '~':
  412. case '&':
  413. case '^':
  414. case '|':
  415. case 'not':
  416. case 'id':
  417. case 'number':
  418. case 'in':
  419. case 'and':
  420. case 'or':
  421. case 'if':
  422. case 'for':
  423. case 'tuple':
  424. case 'lambda':
  425. case 'await':
  426. return expression;
  427. default:
  428. throw new python.Error(`Unhandled expression ${this._tokenizer.location()}`);
  429. }
  430. }
  431. return null;
  432. }
  433. _expression(minPrecedence, terminal, tuple) {
  434. minPrecedence = minPrecedence || -1;
  435. const terminalSet = new Set(terminal);
  436. const stack = [];
  437. for (;;) {
  438. let node = this._node();
  439. const token = this._tokenizer.peek();
  440. if (stack.length == 1 && terminalSet.has(token.value)) {
  441. break;
  442. }
  443. const precedence = python.Parser._precedence[token.value];
  444. if (precedence) {
  445. if (precedence >= minPrecedence) {
  446. this._tokenizer.read();
  447. node.type = token.value;
  448. if (token.type == 'id' && (token.value === 'in' || token.value === 'not')) {
  449. if (token.value === 'in') {
  450. node.type = 'in';
  451. } else if (this._tokenizer.eat('id', 'in')) {
  452. node.type = 'not in';
  453. } else {
  454. node.type = 'not';
  455. node.expression = this._expression(precedence, terminal, tuple === false ? false : true);
  456. stack.push(node);
  457. continue;
  458. }
  459. } else if (token.value == '~') {
  460. node.type = '~';
  461. node.expression = this._expression(precedence, terminal, tuple === false ? false : true);
  462. stack.push(node);
  463. continue;
  464. } else if (token.type == 'id' && token.value == 'is') {
  465. if (this._tokenizer.eat('id', 'not')) {
  466. node.type = 'is not';
  467. }
  468. }
  469. if (stack.length > 0) {
  470. node.op = node.type;
  471. node.type = 'binary';
  472. node.left = stack.pop();
  473. node.right = this._expression(precedence, terminal, tuple === true ? true : false);
  474. } else {
  475. node.op = node.type;
  476. node.type = 'unary';
  477. node.operand = this._expression(precedence, terminal, tuple === true ? true : false);
  478. }
  479. stack.push(node);
  480. continue;
  481. }
  482. }
  483. if (this._tokenizer.eat(':=')) {
  484. node.type = ':=';
  485. node.target = stack.pop();
  486. node.expression = this._expression(-1, terminal, tuple === false ? false : true);
  487. stack.push(node);
  488. continue;
  489. }
  490. if (this._tokenizer.eat('=')) {
  491. node.type = '=';
  492. node.target = stack.pop();
  493. node.expression = this._expression(-1, terminal, tuple === false ? false : true);
  494. stack.push(node);
  495. continue;
  496. }
  497. switch (token.type) {
  498. case '-=':
  499. case '**=':
  500. case '*=':
  501. case '//=':
  502. case '/=':
  503. case '&=':
  504. case '%=':
  505. case '^=':
  506. case '+=':
  507. case '<<=':
  508. case '>>=':
  509. case '|=':
  510. case '@=':
  511. node = this._node(token.type);
  512. this._tokenizer.expect(token.type);
  513. node.target = stack.pop();
  514. node.expression = this._expression(-1, terminal, true);
  515. stack.push(node);
  516. continue;
  517. default:
  518. break;
  519. }
  520. node = this._eat('id', 'if');
  521. if (node) {
  522. node.then = stack.pop();
  523. node.condition = this._expression();
  524. this._tokenizer.expect('id', 'else');
  525. node.else = this._expression();
  526. stack.push(node);
  527. continue;
  528. }
  529. while (this._tokenizer.match('id', 'for') || this._tokenizer.match('id', 'async')) {
  530. const async = this._eat('id', 'async');
  531. if (async && !this._tokenizer.match('id', 'for')) {
  532. throw new python.Error(`Expected 'for' ${this._tokenizer.location()}`);
  533. }
  534. node = this._eat('id', 'for');
  535. if (node) {
  536. if (async) {
  537. node.async = async;
  538. }
  539. node.expression = stack.pop();
  540. node.variable = this._expression(-1, [ 'in' ], true);
  541. this._tokenizer.expect('id', 'in');
  542. node.target = this._expression(-1, [ 'for', 'if' ], true);
  543. while (this._tokenizer.eat('id', 'if')) {
  544. node.condition = node.condition || [];
  545. node.condition.push(this._expression(-1, [ 'for', 'if' ]));
  546. }
  547. stack.push(node);
  548. }
  549. }
  550. node = this._eat('id', 'lambda');
  551. if (node) {
  552. node.parameters = this._parameters(':');
  553. node.body = this._expression(-1, terminal, false);
  554. stack.push(node);
  555. continue;
  556. }
  557. node = this._eat('id', 'yield');
  558. if (node) {
  559. if (this._tokenizer.eat('id', 'from')) {
  560. node.from = this._expression(-1, [], true);
  561. } else {
  562. node.expression = [];
  563. do {
  564. node.expression.push(this._expression(-1, [], false));
  565. }
  566. while (this._tokenizer.eat(','));
  567. }
  568. stack.push(node);
  569. continue;
  570. }
  571. node = this._eat('id', 'await');
  572. if (node) {
  573. node.expression = this._expression(minPrecedence, terminal, tuple);
  574. stack.push(node);
  575. continue;
  576. }
  577. node = this._eat('.');
  578. if (node) {
  579. this._tokenizer.eat('\n');
  580. node.target = stack.pop();
  581. node.member = this._name();
  582. stack.push(node);
  583. continue;
  584. }
  585. if (this._tokenizer.peek().type === '(') {
  586. if (stack.length == 0) {
  587. node = this._node('tuple');
  588. const args = this._arguments();
  589. if (args.length == 1) {
  590. stack.push(args[0]);
  591. } else {
  592. node.value = args;
  593. stack.push(node);
  594. }
  595. } else {
  596. node = this._node('call');
  597. node.target = stack.pop();
  598. node.args = this._arguments();
  599. stack.push(node);
  600. }
  601. continue;
  602. }
  603. if (this._tokenizer.peek().type === '[') {
  604. if (stack.length == 0) {
  605. stack.push(this._expressions());
  606. } else {
  607. node = this._node('[]');
  608. node.target = stack.pop();
  609. node.arguments = this._slice();
  610. stack.push(node);
  611. }
  612. continue;
  613. }
  614. if (this._tokenizer.peek().type == '{') {
  615. stack.push(this._dictOrSetMaker());
  616. continue;
  617. }
  618. node = this._node();
  619. const literal = this._literal();
  620. if (literal) {
  621. if (stack.length > 0 && literal.type == 'number' &&
  622. (literal.value.startsWith('-') || literal.value.startsWith('+'))) {
  623. node.type = literal.value.substring(0, 1);
  624. literal.value = literal.value.substring(1);
  625. node.left = stack.pop();
  626. node.right = literal;
  627. stack.push(node);
  628. } else if (stack.length == 1 && literal.type == 'string' && stack[0].type == 'string') {
  629. stack[0].value += literal.value;
  630. } else {
  631. if (literal.type === 'number') {
  632. switch (literal.value) {
  633. case 'inf': literal.value = Infinity; break;
  634. case '-inf': literal.value = -Infinity; break;
  635. default: break;
  636. }
  637. }
  638. stack.push(literal);
  639. }
  640. continue;
  641. }
  642. if (this._tokenizer.peek().keyword) {
  643. break;
  644. }
  645. node = this._eat('...');
  646. if (node) {
  647. stack.push(node);
  648. continue;
  649. }
  650. const identifier = this._name();
  651. if (identifier) {
  652. stack.push(identifier);
  653. continue;
  654. }
  655. if (tuple === true && stack.length == 1 && this._tokenizer.eat(',')) {
  656. if (stack[0].type === 'tuple') {
  657. [node] = stack;
  658. } else {
  659. node = this._node('tuple');
  660. node.value = [ stack.pop() ];
  661. stack.push(node);
  662. }
  663. // for, bar, = <expr>
  664. if (this._tokenizer.peek().type === '=') {
  665. continue;
  666. }
  667. if (!this._tokenizer.match('=') && !terminalSet.has(this._tokenizer.peek().value)) {
  668. const nextTerminal = terminal.slice(0).concat([ ',', '=' ]);
  669. const expression = this._expression(minPrecedence, nextTerminal, tuple);
  670. if (expression) {
  671. node.value.push(expression);
  672. continue;
  673. }
  674. }
  675. break;
  676. }
  677. break;
  678. }
  679. if (stack.length == 1) {
  680. return stack.pop();
  681. }
  682. if (stack.length != 0) {
  683. throw new python.Error(`Unexpected expression ${this._tokenizer.location()}`);
  684. }
  685. return null;
  686. }
  687. _decorator() {
  688. let list = null;
  689. while (this._tokenizer.eat('@')) {
  690. const node = this._node('decorator');
  691. node.value = this._expression();
  692. if (!node.value || (node.value.type !== 'call' && node.value.type !== 'id' && node.value.type !== '.')) {
  693. throw new python.Error(`Invalid decorator ${this._tokenizer.location()}`);
  694. }
  695. this._tokenizer.eat('\n');
  696. list = list !== null ? list : [];
  697. list.push(node);
  698. }
  699. return list;
  700. }
  701. _dictOrSetMaker() {
  702. const list = [];
  703. this._tokenizer.expect('{');
  704. let dict = true;
  705. while (!this._tokenizer.eat('}')) {
  706. const item = this._expression(-1, [], false);
  707. if (item == null) {
  708. throw new python.Error(`Expected expression ${this._tokenizer.location()}`);
  709. }
  710. if (!this._tokenizer.eat(':')) {
  711. dict = false;
  712. }
  713. if (dict) {
  714. const value = this._expression(-1, [], false);
  715. if (value == null) {
  716. throw new python.Error(`Expected expression ${this._tokenizer.location()}`);
  717. }
  718. list.push({ type: 'pair', key: item, value: value });
  719. } else {
  720. list.push(item);
  721. }
  722. this._tokenizer.eat(',');
  723. this._tokenizer.eat('\n');
  724. if (this._tokenizer.eat('}')) {
  725. break;
  726. }
  727. }
  728. if (dict) {
  729. return { type: 'dict', value: list };
  730. }
  731. return { type: 'set', value: list };
  732. }
  733. _expressions() {
  734. const list = [];
  735. this._tokenizer.expect('[');
  736. while (!this._tokenizer.eat(']')) {
  737. const expression = this._expression();
  738. if (expression == null) {
  739. throw new python.Error(`Expected expression ${this._tokenizer.location()}`);
  740. }
  741. list.push(expression);
  742. this._tokenizer.eat(',');
  743. while (this._tokenizer.eat('\n')) {
  744. // continue
  745. }
  746. if (this._tokenizer.eat(']')) {
  747. break;
  748. }
  749. }
  750. return { type: 'list', value: list };
  751. }
  752. _slice() {
  753. let node = { type: '::' };
  754. let list = [];
  755. const group = [ 'start', 'stop', 'step' ];
  756. this._tokenizer.expect('[');
  757. while (!this._tokenizer.eat(']')) {
  758. if (this._tokenizer.eat(':')) {
  759. node[group.shift()] = { type: 'list', value: list };
  760. list = [];
  761. continue;
  762. }
  763. if (this._tokenizer.eat(',')) {
  764. // list.push({});
  765. continue;
  766. }
  767. if (this._tokenizer.peek().type != ']') {
  768. const expression = this._expression();
  769. if (expression == null) {
  770. throw new python.Error(`Expected expression ${this._tokenizer.location()}`);
  771. }
  772. list.push(expression);
  773. }
  774. }
  775. if (list.length > 0) {
  776. node[group.shift()] = { type: 'list', value: list };
  777. }
  778. if (node.start && !node.stop && !node.step) {
  779. node = node.start;
  780. }
  781. return node;
  782. }
  783. _name(required) {
  784. const token = this._tokenizer.peek();
  785. if (token.type == 'id' && !token.keyword) {
  786. this._tokenizer.read();
  787. return token;
  788. }
  789. if (required) {
  790. throw new python.Error(`Invalid syntax ${this._tokenizer.location()}`);
  791. }
  792. return null;
  793. }
  794. _dottedName() {
  795. const list = [];
  796. do {
  797. list.push(this._name(true).value);
  798. }
  799. while (this._tokenizer.eat('.'));
  800. return list.join('.');
  801. }
  802. _literal() {
  803. const token = this._tokenizer.peek();
  804. if (token.type == 'string' || token.type == 'number' || token.type == 'boolean') {
  805. this._tokenizer.read();
  806. return token;
  807. }
  808. return null;
  809. }
  810. _typeArguments() {
  811. const list = [];
  812. this._tokenizer.expect('[');
  813. while (!this._tokenizer.eat(']')) {
  814. const type = this._type();
  815. if (type == null) {
  816. throw new python.Error(`Expected type ${this._tokenizer.location()}`);
  817. }
  818. list.push(type);
  819. if (!this._tokenizer.eat(',')) {
  820. this._tokenizer.expect(']');
  821. break;
  822. }
  823. }
  824. return list;
  825. }
  826. _type() {
  827. const type = this._node();
  828. type.type = 'type';
  829. type.name = this._expression(-1, [ '[', '=' ]);
  830. if (type.name) {
  831. if (this._tokenizer.peek().value === '[') {
  832. type.arguments = this._typeArguments();
  833. }
  834. return type;
  835. }
  836. return null;
  837. }
  838. _parameter(terminal) {
  839. const node = this._node('parameter');
  840. if (this._tokenizer.eat('/')) {
  841. node.name = '/';
  842. return node;
  843. }
  844. if (this._tokenizer.eat('**')) {
  845. node.parameterType = '**';
  846. }
  847. if (this._tokenizer.eat('*')) {
  848. node.parameterType = '*';
  849. }
  850. const identifier = this._name();
  851. if (identifier !== null) {
  852. node.name = identifier.value;
  853. if (terminal !== ':' && this._tokenizer.eat(':')) {
  854. node.parameterType = this._type();
  855. }
  856. if (this._tokenizer.eat('=')) {
  857. node.initializer = this._expression();
  858. }
  859. return node;
  860. }
  861. return null;
  862. }
  863. _parameters(terminal) {
  864. const list = [];
  865. while (!this._tokenizer.eat(terminal)) {
  866. this._tokenizer.eat('\n');
  867. if (this._tokenizer.eat('(')) {
  868. list.push(this._parameters(')'));
  869. } else {
  870. list.push(this._parameter(terminal));
  871. }
  872. this._tokenizer.eat('\n');
  873. if (!this._tokenizer.eat(',')) {
  874. this._tokenizer.expect(terminal);
  875. break;
  876. }
  877. }
  878. return list;
  879. }
  880. _arguments() {
  881. const list = [];
  882. this._tokenizer.expect('(');
  883. while (!this._tokenizer.eat(')')) {
  884. if (this._tokenizer.eat('\n')) {
  885. continue;
  886. }
  887. const expression = this._expression(-1, [], false);
  888. if (expression == null) {
  889. throw new python.Error(`Expected expression ${this._tokenizer.location()}`);
  890. }
  891. list.push(expression);
  892. if (!this._tokenizer.eat(',')) {
  893. this._tokenizer.eat('\n');
  894. this._tokenizer.expect(')');
  895. break;
  896. }
  897. }
  898. return list;
  899. }
  900. _node(type) {
  901. const node = {};
  902. node.location = this._tokenizer.location();
  903. if (type) {
  904. node.type = type;
  905. }
  906. return node;
  907. }
  908. _eat(type, value) {
  909. if (this._tokenizer.match(type, value)) {
  910. const node = this._node(type === 'id' ? value : type);
  911. this._tokenizer.expect(type, value);
  912. return node;
  913. }
  914. return null;
  915. }
  916. };
  917. python.Tokenizer = class {
  918. constructor(text, file) {
  919. this._text = text;
  920. this._file = file;
  921. this._position = 0;
  922. this._lineStart = 0;
  923. this._line = 0;
  924. this._token = { type: '', value: '' };
  925. this._brackets = 0;
  926. this._indentation = [];
  927. this._outdent = 0;
  928. if (!python.Tokenizer._whitespace) {
  929. python.Tokenizer._whitespace = new RegExp('[\u1680\u180e\u2000-\u200a\u202f\u205f\u3000\ufeff]');
  930. const identifierStartChars = '\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376\u0377\u037a-\u037d\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u0527\u0531-\u0556\u0559\u0561-\u0587\u05d0-\u05ea\u05f0-\u05f2\u0620-\u064a\u066e\u066f\u0671-\u06d3\u06d5\u06e5\u06e6\u06ee\u06ef\u06fa-\u06fc\u06ff\u0710\u0712-\u072f\u074d-\u07a5\u07b1\u07ca-\u07ea\u07f4\u07f5\u07fa\u0800-\u0815\u081a\u0824\u0828\u0840-\u0858\u08a0\u08a2-\u08ac\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0977\u0979-\u097f\u0985-\u098c\u098f\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bd\u09ce\u09dc\u09dd\u09df-\u09e1\u09f0\u09f1\u0a05-\u0a0a\u0a0f\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32\u0a33\u0a35\u0a36\u0a38\u0a39\u0a59-\u0a5c\u0a5e\u0a72-\u0a74\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2\u0ab3\u0ab5-\u0ab9\u0abd\u0ad0\u0ae0\u0ae1\u0b05-\u0b0c\u0b0f\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32\u0b33\u0b35-\u0b39\u0b3d\u0b5c\u0b5d\u0b5f-\u0b61\u0b71\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99\u0b9a\u0b9c\u0b9e\u0b9f\u0ba3\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bd0\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c33\u0c35-\u0c39\u0c3d\u0c58\u0c59\u0c60\u0c61\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cde\u0ce0\u0ce1\u0cf1\u0cf2\u0d05-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d\u0d4e\u0d60\u0d61\u0d7a-\u0d7f\u0d85-\u0d96\u0d9a-\u0db1\u0db3-\u0dbb\u0dbd\u0dc0-\u0dc6\u0e01-\u0e30\u0e32\u0e33\u0e40-\u0e46\u0e81\u0e82\u0e84\u0e87\u0e88\u0e8a\u0e8d\u0e94-\u0e97\u0e99-\u0e9f\u0ea1-\u0ea3\u0ea5\u0ea7\u0eaa\u0eab\u0ead-\u0eb0\u0eb2\u0eb3\u0ebd\u0ec0-\u0ec4\u0ec6\u0edc-\u0edf\u0f00\u0f40-\u0f47\u0f49-\u0f6c\u0f88-\u0f8c\u1000-\u102a\u103f\u1050-\u1055\u105a-\u105d\u1061\u1065\u1066\u106e-\u1070\u1075-\u1081\u108e\u10a0-\u10c5\u10c7\u10cd\u10d0-\u10fa\u10fc-\u1248\u124a-\u124d\u1250-\u1256\u1258\u125a-\u125d\u1260-\u1288\u128a-\u128d\u1290-\u12b0\u12b2-\u12b5\u12b8-\u12be\u12c0\u12c2-\u12c5\u12c8-\u12d6\u12d8-\u1310\u1312-\u1315\u1318-\u135a\u1380-\u138f\u13a0-\u13f4\u1401-\u166c\u166f-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f0\u1700-\u170c\u170e-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1877\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191c\u1950-\u196d\u1970-\u1974\u1980-\u19ab\u19c1-\u19c7\u1a00-\u1a16\u1a20-\u1a54\u1aa7\u1b05-\u1b33\u1b45-\u1b4b\u1b83-\u1ba0\u1bae\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1ce9-\u1cec\u1cee-\u1cf1\u1cf5\u1cf6\u1d00-\u1dbf\u1e00-\u1f15\u1f18-\u1f1d\u1f20-\u1f45\u1f48-\u1f4d\u1f50-\u1f57\u1f59\u1f5b\u1f5d\u1f5f-\u1f7d\u1f80-\u1fb4\u1fb6-\u1fbc\u1fbe\u1fc2-\u1fc4\u1fc6-\u1fcc\u1fd0-\u1fd3\u1fd6-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ff4\u1ff6-\u1ffc\u2071\u207f\u2090-\u209c\u2102\u2107\u210a-\u2113\u2115\u2119-\u211d\u2124\u2126\u2128\u212a-\u212d\u212f-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2c2e\u2c30-\u2c5e\u2c60-\u2ce4\u2ceb-\u2cee\u2cf2\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d80-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u2e2f\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309d-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312d\u3131-\u318e\u31a0-\u31ba\u31f0-\u31ff\u3400-\u4db5\u4e00-\u9fcc\ua000-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua697\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua78e\ua790-\ua793\ua7a0-\ua7aa\ua7f8-\ua801\ua803-\ua805\ua807-\ua80a\ua80c-\ua822\ua840-\ua873\ua882-\ua8b3\ua8f2-\ua8f7\ua8fb\ua90a-\ua925\ua930-\ua946\ua960-\ua97c\ua984-\ua9b2\ua9cf\uaa00-\uaa28\uaa40-\uaa42\uaa44-\uaa4b\uaa60-\uaa76\uaa7a\uaa80-\uaaaf\uaab1\uaab5\uaab6\uaab9-\uaabd\uaac0\uaac2\uaadb-\uaadd\uaae0-\uaaea\uaaf2-\uaaf4\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\uabc0-\uabe2\uac00-\ud7a3\ud7b0-\ud7c6\ud7cb-\ud7fb\uf900-\ufa6d\ufa70-\ufad9\ufb00-\ufb06\ufb13-\ufb17\ufb1d\ufb1f-\ufb28\ufb2a-\ufb36\ufb38-\ufb3c\ufb3e\ufb40\ufb41\ufb43\ufb44\ufb46-\ufbb1\ufbd3-\ufd3d\ufd50-\ufd8f\ufd92-\ufdc7\ufdf0-\ufdfb\ufe70-\ufe74\ufe76-\ufefc\uff21-\uff3a\uff41-\uff5a\uff66-\uffbe\uffc2-\uffc7\uffca-\uffcf\uffd2-\uffd7\uffda-\uffdc';
  931. const identifierChars = '\u0300-\u036f\u0483-\u0487\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u0620-\u0649\u0672-\u06d3\u06e7-\u06e8\u06fb-\u06fc\u0730-\u074a\u0800-\u0814\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0840-\u0857\u08e4-\u08fe\u0900-\u0903\u093a-\u093c\u093e-\u094f\u0951-\u0957\u0962-\u0963\u0966-\u096f\u0981-\u0983\u09bc\u09be-\u09c4\u09c7\u09c8\u09d7\u09df-\u09e0\u0a01-\u0a03\u0a3c\u0a3e-\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a66-\u0a71\u0a75\u0a81-\u0a83\u0abc\u0abe-\u0ac5\u0ac7-\u0ac9\u0acb-\u0acd\u0ae2-\u0ae3\u0ae6-\u0aef\u0b01-\u0b03\u0b3c\u0b3e-\u0b44\u0b47\u0b48\u0b4b-\u0b4d\u0b56\u0b57\u0b5f-\u0b60\u0b66-\u0b6f\u0b82\u0bbe-\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcd\u0bd7\u0be6-\u0bef\u0c01-\u0c03\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62-\u0c63\u0c66-\u0c6f\u0c82\u0c83\u0cbc\u0cbe-\u0cc4\u0cc6-\u0cc8\u0cca-\u0ccd\u0cd5\u0cd6\u0ce2-\u0ce3\u0ce6-\u0cef\u0d02\u0d03\u0d46-\u0d48\u0d57\u0d62-\u0d63\u0d66-\u0d6f\u0d82\u0d83\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0df2\u0df3\u0e34-\u0e3a\u0e40-\u0e45\u0e50-\u0e59\u0eb4-\u0eb9\u0ec8-\u0ecd\u0ed0-\u0ed9\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f41-\u0f47\u0f71-\u0f84\u0f86-\u0f87\u0f8d-\u0f97\u0f99-\u0fbc\u0fc6\u1000-\u1029\u1040-\u1049\u1067-\u106d\u1071-\u1074\u1082-\u108d\u108f-\u109d\u135d-\u135f\u170e-\u1710\u1720-\u1730\u1740-\u1750\u1772\u1773\u1780-\u17b2\u17dd\u17e0-\u17e9\u180b-\u180d\u1810-\u1819\u1920-\u192b\u1930-\u193b\u1951-\u196d\u19b0-\u19c0\u19c8-\u19c9\u19d0-\u19d9\u1a00-\u1a15\u1a20-\u1a53\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1b46-\u1b4b\u1b50-\u1b59\u1b6b-\u1b73\u1bb0-\u1bb9\u1be6-\u1bf3\u1c00-\u1c22\u1c40-\u1c49\u1c5b-\u1c7d\u1cd0-\u1cd2\u1d00-\u1dbe\u1e01-\u1f15\u200c\u200d\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2d81-\u2d96\u2de0-\u2dff\u3021-\u3028\u3099\u309a\ua640-\ua66d\ua674-\ua67d\ua69f\ua6f0-\ua6f1\ua7f8-\ua800\ua806\ua80b\ua823-\ua827\ua880-\ua881\ua8b4-\ua8c4\ua8d0-\ua8d9\ua8f3-\ua8f7\ua900-\ua909\ua926-\ua92d\ua930-\ua945\ua980-\ua983\ua9b3-\ua9c0\uaa00-\uaa27\uaa40-\uaa41\uaa4c-\uaa4d\uaa50-\uaa59\uaa7b\uaae0-\uaae9\uaaf2-\uaaf3\uabc0-\uabe1\uabec\uabed\uabf0-\uabf9\ufb20-\ufb28\ufe00-\ufe0f\ufe20-\ufe26\ufe33\ufe34\ufe4d-\ufe4f\uff10-\uff19\uff3f';
  932. python.Tokenizer._identifierStart = new RegExp(`[${identifierStartChars}]`);
  933. /* eslint-disable no-misleading-character-class */
  934. python.Tokenizer._identifierChar = new RegExp(`[${identifierStartChars}${identifierChars}]`);
  935. /* eslint-enable no-misleading-character-class */
  936. }
  937. }
  938. peek() {
  939. if (!this._cache) {
  940. this._tokenize();
  941. this._cache = true;
  942. }
  943. return this._token;
  944. }
  945. read() {
  946. if (!this._cache) {
  947. this._tokenize();
  948. }
  949. const next = this._position + this._token.value.length;
  950. while (this._position < next) {
  951. if (python.Tokenizer._isNewline(this._get(this._position))) {
  952. this._position = this._newLine(this._position);
  953. this._lineStart = this._position;
  954. this._line++;
  955. } else {
  956. this._position++;
  957. }
  958. }
  959. this._cache = false;
  960. return this._token;
  961. }
  962. match(type, value) {
  963. const token = this.peek();
  964. if (token.type === type && (!value || token.value === value)) {
  965. return true;
  966. }
  967. return false;
  968. }
  969. eat(type, value) {
  970. const token = this.peek();
  971. if (token.type === type && (!value || token.value === value)) {
  972. this.read();
  973. return true;
  974. }
  975. return false;
  976. }
  977. expect(type, value) {
  978. const token = this.peek();
  979. if (token.type !== type) {
  980. throw new python.Error(`Unexpected '${token.value}' instead of '${type}' ${this.location()}`);
  981. }
  982. if (value && token.value !== value) {
  983. throw new python.Error(`Unexpected '${token.value}' instead of '${value}' ${this.location()}`);
  984. }
  985. this.read();
  986. }
  987. location() {
  988. const line = this._line + 1;
  989. const column = this._position - this._lineStart + 1;
  990. return `at ${this._file}:${line}:${column}.`;
  991. }
  992. static _isSpace(c) {
  993. switch (c) {
  994. case ' ':
  995. case '\t':
  996. case '\v': // 11
  997. case '\f': // 12
  998. case '\xA0': // 160
  999. return true;
  1000. default:
  1001. if (c.charCodeAt(0) >= 0x1680) {
  1002. return python.Tokenizer._whitespace.test(c);
  1003. }
  1004. return false;
  1005. }
  1006. }
  1007. static _isNewline(c) {
  1008. switch (c) {
  1009. case '\n':
  1010. case '\r':
  1011. case '\u2028': // 8232
  1012. case '\u2029': // 8233
  1013. return true;
  1014. default:
  1015. return false;
  1016. }
  1017. }
  1018. static _isIdentifierStartChar(c) {
  1019. if (c < 'A') {
  1020. return c === '$';
  1021. }
  1022. if (c <= 'Z') {
  1023. return true;
  1024. }
  1025. if (c < 'a') {
  1026. return c === '_';
  1027. }
  1028. if (c <= 'z') {
  1029. return true;
  1030. }
  1031. const code = c.charCodeAt(0);
  1032. if (code >= 0xAA) {
  1033. return python.Tokenizer._identifierStart.test(c);
  1034. }
  1035. return false;
  1036. }
  1037. static _isIdentifierChar(c) {
  1038. if (c < '0') {
  1039. return c === '$';
  1040. }
  1041. if (c <= '9') {
  1042. return true;
  1043. }
  1044. if (c < 'A') {
  1045. return false;
  1046. }
  1047. if (c <= 'Z') {
  1048. return true;
  1049. }
  1050. if (c < 'a') {
  1051. return c === '_';
  1052. }
  1053. if (c <= 'z') {
  1054. return true;
  1055. }
  1056. const code = c.charCodeAt(0);
  1057. if (code >= 0xAA) {
  1058. return python.Tokenizer._identifierChar.test(c);
  1059. }
  1060. return false;
  1061. }
  1062. _get(position) {
  1063. return position >= this._text.length ? '\0' : this._text[position];
  1064. }
  1065. _skipLine() {
  1066. while (this._position < this._text.length) {
  1067. if (python.Tokenizer._isNewline(this._get(this._position))) {
  1068. break;
  1069. }
  1070. this._position++;
  1071. }
  1072. }
  1073. _skipWhitespace() {
  1074. while (this._position < this._text.length) {
  1075. const c = this._text[this._position];
  1076. if (c == '#') {
  1077. this._skipLine();
  1078. } else if (python.Tokenizer._isSpace(c)) {
  1079. this._position++;
  1080. } else if (c == '\\') {
  1081. // Explicit Line Continuation
  1082. this._position++;
  1083. if (python.Tokenizer._isNewline(this._get(this._position))) {
  1084. this._position = this._newLine(this._position);
  1085. this._lineStart = this._position;
  1086. this._line++;
  1087. } else {
  1088. throw new python.Error(`Unexpected '${this._text[this._position]}' after line continuation ${this.location()}`);
  1089. }
  1090. } else if (this._brackets > 0 && python.Tokenizer._isNewline(c)) {
  1091. // Implicit Line Continuation
  1092. this._position = this._newLine(this._position);
  1093. this._lineStart = this._position;
  1094. this._line++;
  1095. } else {
  1096. break;
  1097. }
  1098. }
  1099. }
  1100. _newLine(position) {
  1101. if ((this._get(position) === '\n' && this._get(position + 1) === '\r') ||
  1102. (this._get(position) === '\r' && this._get(position + 1) === '\n')) {
  1103. return position + 2;
  1104. }
  1105. return position + 1;
  1106. }
  1107. _tokenize() {
  1108. if (this._token.type !== '\n') {
  1109. this._skipWhitespace();
  1110. }
  1111. if (this._token.type === 'dedent') {
  1112. this._indentation.pop();
  1113. this._outdent--;
  1114. if (this._outdent > 0) {
  1115. this._token = { type: 'dedent', value: '' };
  1116. return;
  1117. }
  1118. }
  1119. if (this._token.type == '\n') {
  1120. let indent = '';
  1121. let i = this._position;
  1122. while (i < this._text.length) {
  1123. const c = this._text[i];
  1124. if (python.Tokenizer._isSpace(c)) {
  1125. indent += c;
  1126. i++;
  1127. } else if (python.Tokenizer._isNewline(c)) {
  1128. indent = '';
  1129. i = this._newLine(i);
  1130. this._position = i;
  1131. this._lineStart = i;
  1132. this._line++;
  1133. } else if (c == '#') {
  1134. indent = '';
  1135. while (i < this._text.length && !python.Tokenizer._isNewline(this._text[i])) {
  1136. i++;
  1137. }
  1138. continue;
  1139. } else {
  1140. break;
  1141. }
  1142. }
  1143. let type = null;
  1144. if (indent.length > 0) {
  1145. const current = this._indentation.length > 0 ? this._indentation[this._indentation.length - 1] : '';
  1146. if (indent.length > current.length) {
  1147. type = 'indent';
  1148. this._indentation.push(indent);
  1149. } else if (indent.length > 0 && indent.length < current.length) {
  1150. type = 'dedent';
  1151. this._outdent = 0;
  1152. for (let j = this._indentation.length - 1; j >= 0 && indent.length < this._indentation[j].length; j--) {
  1153. this._outdent++;
  1154. }
  1155. } else {
  1156. this._position += indent.length;
  1157. }
  1158. } else if (i >= this._text.length) {
  1159. this._token = { type: 'eof', value: '' };
  1160. return;
  1161. } else if (this._indentation.length > 0) {
  1162. type = 'dedent';
  1163. this._outdent = this._indentation.length;
  1164. }
  1165. if (type === 'indent' || type === 'dedent') {
  1166. this._token = { type: type, value: indent };
  1167. return;
  1168. }
  1169. }
  1170. if (this._position >= this._text.length) {
  1171. this._token = { type: 'eof', value: '' };
  1172. return;
  1173. }
  1174. const c = this._get(this._position);
  1175. const string = this._string();
  1176. if (string) {
  1177. this._token = string;
  1178. return;
  1179. }
  1180. switch (c) {
  1181. case '(':
  1182. case '[':
  1183. case '{':
  1184. this._brackets++;
  1185. this._token = { type: c, value: c };
  1186. return;
  1187. case ')':
  1188. case ']':
  1189. case '}':
  1190. if (this._brackets === 0) {
  1191. throw new python.Error(`Unexpected '${c}' ${this.location}`);
  1192. }
  1193. this._brackets--;
  1194. this._token = { type: c, value: c };
  1195. return;
  1196. case ',':
  1197. case ';':
  1198. case '?':
  1199. this._token = { type: c, value: c };
  1200. return;
  1201. default: {
  1202. const number = this._number();
  1203. if (number) {
  1204. this._token = number;
  1205. return;
  1206. }
  1207. if (c === '.') {
  1208. let end = this._position + 1;
  1209. while (this._get(end) === '.') {
  1210. end++;
  1211. }
  1212. const text = this._text.substring(this._position, end);
  1213. this._token = { type: text, value: text };
  1214. return;
  1215. }
  1216. const identifier = this._identifier();
  1217. if (identifier) {
  1218. this._token = identifier;
  1219. return;
  1220. }
  1221. const operator = this._operator();
  1222. if (operator) {
  1223. this._token = operator;
  1224. return;
  1225. }
  1226. break;
  1227. }
  1228. }
  1229. if (c === '.') {
  1230. this._token = { type: c, value: c };
  1231. return;
  1232. }
  1233. if (c === '\\') {
  1234. this._token = { type: '\\', value: c };
  1235. return;
  1236. }
  1237. if (python.Tokenizer._isNewline(c)) {
  1238. this._token = { type: '\n', value: this._text.substring(this._position, this._newLine(this._position)) };
  1239. return;
  1240. }
  1241. throw new python.Error(`Unexpected token '${c}' ${this.location()}`);
  1242. }
  1243. _number() {
  1244. const octal = (c) => c >= '0' && c <= '7' || c === '_';
  1245. const binary = (c) => c === '0' || c === '1' || c === '_';
  1246. const decimal = (c) => c >= '0' && c <= '9' || c === '_';
  1247. const hex = (c) => decimal(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') || c === '_';
  1248. let c = this._get(this._position);
  1249. const sign = (c === '-' || c === '+') ? 1 : 0;
  1250. let i = this._position + sign;
  1251. c = this._get(i);
  1252. if (c === '0') {
  1253. let radix = 0;
  1254. const n = this._get(i + 1);
  1255. if ((n === 'x' || n === 'X') && hex(this._get(i + 2))) {
  1256. i += 2;
  1257. while (hex(this._get(i))) {
  1258. i += 1;
  1259. }
  1260. if (this._get(i) === 'l' || this._get(i) === 'L') {
  1261. i += 1;
  1262. }
  1263. radix = 16;
  1264. } else if ((n === 'b' || n === 'B') && binary(this._get(i + 2))) {
  1265. i += 2;
  1266. while (binary(this._get(i))) {
  1267. i++;
  1268. }
  1269. radix = 2;
  1270. } else if ((n === 'o' || n === 'O') && octal(this._get(i + 2))) {
  1271. i += 2;
  1272. while (octal(this._get(i))) {
  1273. i++;
  1274. }
  1275. radix = 8;
  1276. } else if (n >= '0' && n <= '7') {
  1277. i++;
  1278. while (octal(this._get(i))) {
  1279. i += 1;
  1280. }
  1281. if (this._get(i) === 'l' || this._get(i) === 'L') {
  1282. i += 1;
  1283. }
  1284. radix = 8;
  1285. }
  1286. if (radix > 0 && this._get(i) !== '.') {
  1287. const radixText = this._text.substring(this._position, i);
  1288. const radixParseText = radixText.indexOf('_') !== -1 ? radixText.split('_').join('') : radixText;
  1289. if (!isNaN(parseInt(radixParseText, radix))) {
  1290. return { type: 'number', value: radixText };
  1291. }
  1292. }
  1293. }
  1294. i = this._position + sign;
  1295. let isDecimal = false;
  1296. if (this._get(i) >= '1' && this._get(i) <= '9') {
  1297. while (decimal(this._get(i))) {
  1298. i++;
  1299. }
  1300. c = this._get(i).toLowerCase();
  1301. isDecimal = c !== '.' && c !== 'e';
  1302. }
  1303. if (this._get(i) === '0') {
  1304. i++;
  1305. c = this._get(i).toLowerCase();
  1306. isDecimal = !decimal(c) && c !== '.' && c !== 'e' && c !== 'j';
  1307. }
  1308. if (isDecimal) {
  1309. if (this._get(i) === 'j' || this._get(i) === 'J' || this._get(i) === 'l' || this._get(i) === 'L') {
  1310. return { 'type': 'number', value: this._text.substring(this._position, i + 1) };
  1311. }
  1312. const intText = this._text.substring(this._position, i);
  1313. if (!isNaN(parseInt(intText, 10))) {
  1314. return { type: 'number', value: intText };
  1315. }
  1316. }
  1317. i = this._position + sign;
  1318. if ((this._get(i) >= '0' && this._get(i) <= '9') ||
  1319. (this._get(i) === '.' && this._get(i + 1) >= '0' && this._get(i + 1) <= '9')) {
  1320. while (decimal(this._get(i))) {
  1321. i++;
  1322. }
  1323. if (this._get(i) === '.') {
  1324. i++;
  1325. }
  1326. while (decimal(this._get(i))) {
  1327. i++;
  1328. }
  1329. if (i > (this._position + sign)) {
  1330. if (this._get(i) === 'e' || this._get(i) === 'E') {
  1331. i++;
  1332. if (this._get(i) == '-' || this._get(i) == '+') {
  1333. i++;
  1334. }
  1335. if (!decimal(this._get(i))) {
  1336. i = this._position;
  1337. } else {
  1338. while (decimal(this._get(i))) {
  1339. i++;
  1340. }
  1341. }
  1342. } else {
  1343. while (decimal(this._get(i))) {
  1344. i++;
  1345. }
  1346. }
  1347. }
  1348. if (i > (this._position + sign)) {
  1349. if (this._get(i) === 'j' || this._get(i) === 'J') {
  1350. return { type: 'number', value: this._text.substring(this._position, i + 1) };
  1351. }
  1352. const floatText = this._text.substring(this._position, i);
  1353. const floatParseText = floatText.indexOf('_') != -1 ? floatText.split('_').join('') : floatText;
  1354. if (!isNaN(parseFloat(floatParseText))) {
  1355. return { type: 'number', value: floatText };
  1356. }
  1357. }
  1358. }
  1359. return null;
  1360. }
  1361. _identifier() {
  1362. let i = this._position;
  1363. if (python.Tokenizer._isIdentifierStartChar(this._get(i))) {
  1364. i++;
  1365. while (python.Tokenizer._isIdentifierChar(this._get(i))) {
  1366. i++;
  1367. }
  1368. }
  1369. if (i > this._position) {
  1370. const text = this._text.substring(this._position, i);
  1371. let keyword = false;
  1372. switch (text) {
  1373. case 'and':
  1374. case 'as':
  1375. case 'else':
  1376. case 'for':
  1377. case 'if':
  1378. case 'import':
  1379. case 'in':
  1380. case 'is':
  1381. case 'not':
  1382. case 'or':
  1383. keyword = true;
  1384. break;
  1385. default:
  1386. keyword = false;
  1387. break;
  1388. }
  1389. return { type: 'id', value: text, keyword: keyword };
  1390. }
  1391. return null;
  1392. }
  1393. _operator() {
  1394. let length = 0;
  1395. const c0 = this._get(this._position);
  1396. const c1 = this._get(this._position + 1);
  1397. const c2 = this._get(this._position + 2);
  1398. switch (c0) {
  1399. case '+':
  1400. case '&':
  1401. case '|':
  1402. case '^':
  1403. case '=':
  1404. case '!':
  1405. case '%':
  1406. case '~':
  1407. length = c1 === '=' ? 2 : 1;
  1408. break;
  1409. case '-':
  1410. length = c1 === '=' || c1 === '>' ? 2 : 1;
  1411. break;
  1412. case '*':
  1413. length = c1 === '*' ? (c2 === '=' ? 3 : 2) : (c1 === '=' ? 2 : 1);
  1414. break;
  1415. case '/':
  1416. length = c1 === '/' ? (c2 === '=' ? 3 : 2) : (c1 === '=' ? 2 : 1);
  1417. break;
  1418. case '<':
  1419. length = c1 === '>' ? 2 : (c1 === '<' ? (c2 === '=' ? 3 : 2) : (c1 === '=' ? 2 : 1));
  1420. break;
  1421. case '>':
  1422. length = c1 === '>' ? (c2 === '=' ? 3 : 2) : (c1 === '=' ? 2 : 1);
  1423. break;
  1424. case '@':
  1425. length = c1 === '=' ? 2 : 1;
  1426. break;
  1427. case ':':
  1428. length = c1 === '=' ? 2 : 1;
  1429. break;
  1430. default:
  1431. return null;
  1432. }
  1433. const text = this._text.substring(this._position, this._position + length);
  1434. return { type: text, value: text };
  1435. }
  1436. _string() {
  1437. let i = this._position;
  1438. let prefix = -1;
  1439. if (this._get(i) === "'" || this._get(i) === '"') {
  1440. prefix = '';
  1441. } else if (this._get(i + 1) === "'" || this._get(i + 1) === '"') {
  1442. const c = this._get(i);
  1443. switch (c.toLowerCase()) {
  1444. case 'b':
  1445. case 'f':
  1446. case 'r':
  1447. case 'u':
  1448. prefix = c;
  1449. break;
  1450. default:
  1451. break;
  1452. }
  1453. } else if (this._get(i + 2) === "'" || this._get(i + 2) === '"') {
  1454. const cc = this._text.substr(this._position, 2);
  1455. switch (cc.toLowerCase()) {
  1456. case 'br':
  1457. case 'fr':
  1458. case 'rb':
  1459. case 'rf':
  1460. case 'ur':
  1461. prefix = cc;
  1462. break;
  1463. default:
  1464. break;
  1465. }
  1466. }
  1467. if (prefix.length >= 0) {
  1468. i += prefix.length;
  1469. let quote = '';
  1470. let count = 0;
  1471. const q0 = this._get(i);
  1472. const q1 = this._get(i + 1);
  1473. const q2 = this._get(i + 2);
  1474. switch (q0) {
  1475. case "'":
  1476. quote = q0;
  1477. count = (q1 === "'" && q2 === "'") ? 3 : 1;
  1478. break;
  1479. case '"':
  1480. quote = q0;
  1481. count = (q1 === '"' && q2 === '"') ? 3 : 1;
  1482. break;
  1483. default:
  1484. throw new python.Error(`Unsupported string quote '${q0}'.`);
  1485. }
  1486. i += count;
  1487. if (count == 1) {
  1488. while (i < this._text.length) {
  1489. if (this._text[i] === quote) {
  1490. return { type: 'string', value: this._text.substring(this._position, i + 1) };
  1491. } else if (this._text[i] === '\\' &&
  1492. (this._get(i + 1) == quote || this._get(i + 1) == '\n' || this._get(i + 1) == '\\')) {
  1493. i += 2;
  1494. } else if (this._text[i] === '\r' || this._text[i] === '\n') {
  1495. break;
  1496. } else {
  1497. i++;
  1498. }
  1499. }
  1500. } else if (count == 3) {
  1501. while (i < this._text.length) {
  1502. if (this._get(i) === quote && this._get(i + 1) === quote && this._get(i + 2) === quote) {
  1503. return { type: 'string', value: this._text.substring(this._position, i + 3) };
  1504. } else if (this._get(i) === '\\' && this._get(i + 1) === quote) {
  1505. i += 2;
  1506. continue;
  1507. }
  1508. i++;
  1509. }
  1510. }
  1511. }
  1512. i = this._position;
  1513. if (this._get(i) === '`') {
  1514. i++;
  1515. while (i < this._text.length) {
  1516. if (this._text[i] === '`') {
  1517. return { type: 'string', value: this._text.substring(this._position, i + 1) };
  1518. }
  1519. i++;
  1520. }
  1521. }
  1522. return null;
  1523. }
  1524. };
  1525. python.Execution = class {
  1526. constructor(sources) {
  1527. const self = this;
  1528. const execution = self;
  1529. this._sources = sources || new Map();
  1530. this._events = new Map();
  1531. this._utf8Decoder = new TextDecoder('utf-8');
  1532. this._unresolved = new Map();
  1533. const dict = class extends Map {
  1534. constructor(items) {
  1535. super();
  1536. if (items) {
  1537. for (const [name, value] of items) {
  1538. this.__setitem__(name, value);
  1539. }
  1540. }
  1541. }
  1542. __contains__(key) {
  1543. return this.has(key);
  1544. }
  1545. __setitem__(key, value) {
  1546. this.set(key, value);
  1547. }
  1548. __getitem__(key) {
  1549. return this.get(key);
  1550. }
  1551. };
  1552. this._modules = new dict();
  1553. this._registry = new Map();
  1554. const module = class {
  1555. constructor(name) {
  1556. this.__name__ = name;
  1557. }
  1558. };
  1559. const builtins = this.register('builtins', new module('builtins'));
  1560. this._builtins = builtins;
  1561. this._registry.set('__builtin__', builtins);
  1562. this.registerType('builtins.type', class {}).__class__ = builtins.type;
  1563. this.registerType('builtins.module', module);
  1564. this.registerType('builtins.method', class {});
  1565. this.registerType('builtins.function', class {});
  1566. this.registerType('builtins.code', class {});
  1567. this.import('builtins');
  1568. this.registerType('builtins.builtin_function_or_method', class {});
  1569. const typing = this.register('typing');
  1570. this._typing = typing;
  1571. const operator = this.register('operator');
  1572. this.register('_codecs');
  1573. this.register('argparse');
  1574. this.register('collections');
  1575. this.register('copy_reg');
  1576. this.register('cuml');
  1577. this.register('gensim');
  1578. this.register('io');
  1579. this.register('joblib');
  1580. const functools = this.register('functools');
  1581. this.registerType('functools.partial', class {});
  1582. const keras = this.register('keras');
  1583. this.register('lightgbm');
  1584. this.register('nolearn');
  1585. const fastcore = this.register('fastcore');
  1586. const fastai = this.register('fastai');
  1587. const math = this.register('math');
  1588. math.inf = Infinity;
  1589. const numpy = this.register('numpy');
  1590. const pickle = this.register('pickle');
  1591. const sklearn = this.register('sklearn');
  1592. const torch = this.register('torch');
  1593. const torchvision = this.register('torchvision');
  1594. this.register('torch.storage');
  1595. this.register('torch.nn.parameter');
  1596. this.register('torch.ops');
  1597. this.register('torch._ops');
  1598. this.register('torch.ops.torchvision');
  1599. this.register('torch.ops.torchaudio');
  1600. this.register('torch.ops._caffe2');
  1601. this.register('torchvision');
  1602. this.register('__torch__');
  1603. this.register('sys').modules = this._modules;
  1604. this.register('xgboost');
  1605. this.registerType('builtins.dict', dict);
  1606. this.registerType('builtins.ellipsis', class {});
  1607. this.registerType('builtins.cell', class {});
  1608. this.registerType('builtins.list', class extends Array {});
  1609. this.registerType('builtins.number', class {});
  1610. this.registerFunction('builtins.__import__', function(name, globals, locals, fromlist, level) {
  1611. return execution.__import__(name, globals, locals, fromlist, level);
  1612. });
  1613. this.registerFunction('builtins.bool', function(value) {
  1614. if (value) {
  1615. if (value.__bool__) {
  1616. return value.__bool__();
  1617. }
  1618. if (value.__len__) {
  1619. return value.__len__() > 0;
  1620. }
  1621. }
  1622. return false;
  1623. });
  1624. this.registerFunction('builtins.int', function(value) {
  1625. if (value) {
  1626. if (value.__int__) {
  1627. return value.__int__();
  1628. }
  1629. if (Number.isInteger(value)) {
  1630. return value;
  1631. }
  1632. }
  1633. return NaN;
  1634. });
  1635. this.registerFunction('builtins.float', function(value) {
  1636. if (value) {
  1637. if (value.__float__) {
  1638. return value.__float__();
  1639. }
  1640. if (Number(value) === value) {
  1641. return value;
  1642. }
  1643. }
  1644. return NaN;
  1645. });
  1646. this.registerFunction('builtins.str', function(value) {
  1647. if (value && value.__str__) {
  1648. return value.__str__();
  1649. }
  1650. return JSON.stringify(value);
  1651. });
  1652. this.registerType('builtins.complex', class {
  1653. constructor(real, imaginary) {
  1654. this.real = real;
  1655. this.imag = imaginary;
  1656. }
  1657. });
  1658. this.registerType('builtins.NoneType', class {});
  1659. this.registerType('builtins.object', class {});
  1660. this.registerType('builtins.tuple', class extends Array {
  1661. constructor(items) {
  1662. super(items ? items.length : 0);
  1663. if (items) {
  1664. for (let i = 0; i < items.length; i++) {
  1665. this[i] = items[i];
  1666. }
  1667. }
  1668. }
  1669. });
  1670. this.registerType('builtins.staticmethod', class {});
  1671. this.registerFunction('builtins.long', this.builtins.int);
  1672. this.registerFunction('builtins.print', function() {});
  1673. this.registerFunction('builtins.unicode', function(/* value */) {
  1674. throw new python.Error("'builtins.unicode' not implemented.");
  1675. });
  1676. this.registerType('builtins.Warning', class {});
  1677. this.registerType('builtins.FutureWarning', class extends builtins.Warning {});
  1678. this.registerType('builtins.BaseException', class {});
  1679. this.registerType('builtins.Exception', class extends builtins.BaseException {});
  1680. this.registerType('builtins.SyntaxError', class extends builtins.Exception {});
  1681. this.registerType('typing._Final', class {});
  1682. this.registerType('typing._SpecialForm', class extends typing._Final {});
  1683. this.registerType('typing._BaseGenericAlias', class extends typing._Final {});
  1684. this.registerType('typing._GenericAlias', class extends typing._BaseGenericAlias {});
  1685. this.registerType('typing._SpecialGenericAlias', class extends typing._BaseGenericAlias {});
  1686. this.registerType('typing._TupleType', class extends typing._SpecialGenericAlias {});
  1687. this.registerType('typing._CallableType', class {});
  1688. this.registerFunction('typing.cast', function() {
  1689. throw new python.Error("'typing.cast' not implemented.");
  1690. });
  1691. typing.Any = Reflect.construct(typing._SpecialForm, []);
  1692. typing.Callable = Reflect.construct(typing._CallableType, []);
  1693. typing.Dict = Reflect.construct(typing._SpecialGenericAlias, []);
  1694. typing.List = Reflect.construct(typing._SpecialGenericAlias, []);
  1695. typing.Optional = Reflect.construct(typing._SpecialForm, []);
  1696. typing.OrderedDict = Reflect.construct(typing._SpecialGenericAlias, []);
  1697. typing.Sequence = Reflect.construct(typing._SpecialGenericAlias, []);
  1698. typing.Tuple = Reflect.construct(typing._TupleType, []);
  1699. typing.Union = Reflect.construct(typing._SpecialForm, []);
  1700. this.registerFunction('operator.add', function() {
  1701. throw new python.Error("'operator.add' not implemented.");
  1702. });
  1703. this.registerFunction('operator.eq', function() {
  1704. throw new python.Error("'operator.eq' not implemented.");
  1705. });
  1706. this.registerFunction('operator.ge', function() {
  1707. throw new python.Error("'operator.ge' not implemented.");
  1708. });
  1709. this.registerFunction('operator.gt', function() {
  1710. throw new python.Error("'operator.gt' not implemented.");
  1711. });
  1712. this.registerFunction('operator.mul', function() {
  1713. throw new python.Error("'operator.mul' not implemented.");
  1714. });
  1715. this.registerFunction('operator.mod', function() {
  1716. throw new python.Error("'operator.mod' not implemented.");
  1717. });
  1718. this.registerFunction('operator.le', function() {
  1719. throw new python.Error("'operator.le' not implemented.");
  1720. });
  1721. this.registerFunction('operator.lt', function() {
  1722. throw new python.Error("'operator.lt' not implemented.");
  1723. });
  1724. this.registerFunction('operator.ne', function() {
  1725. throw new python.Error("'operator.ne' not implemented.");
  1726. });
  1727. this.registerFunction('operator.floordiv', function() {
  1728. throw new python.Error("'operator.floordiv' not implemented.");
  1729. });
  1730. this.registerFunction('operator.sub', function() {
  1731. throw new python.Error("'operator.sub' not implemented.");
  1732. });
  1733. this.registerType('argparse.Namespace', class {
  1734. constructor(args) {
  1735. this.args = args;
  1736. }
  1737. });
  1738. this.registerType('collections.deque', class extends Array {
  1739. constructor(iterable) {
  1740. super();
  1741. if (Array.isArray(iterable)) {
  1742. for (const value of iterable) {
  1743. this.push(value);
  1744. }
  1745. }
  1746. }
  1747. });
  1748. this.registerType('collections.OrderedDict', class extends dict {});
  1749. this.registerType('cuml.common.array_descriptor.CumlArrayDescriptorMeta', class {});
  1750. this.registerType('cuml.ensemble.randomforestclassifier.RandomForestClassifier', class {});
  1751. this.registerType('cuml.raft.common.handle.Handle', class {
  1752. __setstate__(state) {
  1753. this._handle = state;
  1754. }
  1755. });
  1756. this.registerType('dnnlib.tflib.network.Network', class {});
  1757. this.registerType('dnnlib.util.EasyDict', class extends dict {});
  1758. this.registerType('haiku._src.data_structures.FlatMapping', class {
  1759. constructor(dict) {
  1760. Object.assign(this, dict);
  1761. }
  1762. });
  1763. this.registerType('haiku._src.data_structures.frozendict', class {
  1764. constructor(obj) {
  1765. Object.assign(this, obj);
  1766. }
  1767. });
  1768. this.registerType('hmmlearn.hmm.MultinomialHMM', class {
  1769. __setstate__(state) {
  1770. Object.assign(this, state);
  1771. }
  1772. });
  1773. this.registerType('hmmlearn.base.ConvergenceMonitor', class {
  1774. __setstate__(state) {
  1775. Object.assign(this, state);
  1776. }
  1777. });
  1778. this.registerType('io.BytesIO', class {
  1779. constructor(buf, mode) {
  1780. this.mode = mode || 'r';
  1781. this._buf = this.mode === 'w' ? null : buf;
  1782. this._point = 0;
  1783. }
  1784. seek(offset) {
  1785. this._point = offset;
  1786. }
  1787. read(size) {
  1788. const start = this._point;
  1789. this._point = size !== undefined ? start + size : this._buf.length;
  1790. return this._buf.subarray(start, this._point);
  1791. }
  1792. write(data) {
  1793. const src = this._buf || new Uint8Array();
  1794. this._point = src.length + data.length;
  1795. this._buf = new Uint8Array(this._point);
  1796. this._buf.set(src, 0);
  1797. this._buf.set(data, src.length);
  1798. }
  1799. });
  1800. this.registerType('numpy.dtype', class {
  1801. constructor(obj, align, copy) {
  1802. if (typeof obj === 'string' && (obj.startsWith('<') || obj.startsWith('>'))) {
  1803. this.byteorder = obj.substring(0, 1);
  1804. obj = obj.substring(1);
  1805. } else {
  1806. this.byteorder = '=';
  1807. }
  1808. switch (obj) {
  1809. case 'b1': case 'bool': this.itemsize = 1; this.kind = 'b'; break;
  1810. case 'i1': case 'int8': this.itemsize = 1; this.kind = 'i'; break;
  1811. case 'i2': case 'int16': this.itemsize = 2; this.kind = 'i'; break;
  1812. case 'i4': case 'int32': this.itemsize = 4; this.kind = 'i'; break;
  1813. case 'i8': case 'int64': case 'int': this.itemsize = 8; this.kind = 'i'; break;
  1814. case 'u1': case 'uint8': this.itemsize = 1; this.kind = 'u'; break;
  1815. case 'u2': case 'uint16': this.itemsize = 2; this.kind = 'u'; break;
  1816. case 'u4': case 'uint32': this.itemsize = 4; this.kind = 'u'; break;
  1817. case 'u8': case 'uint64': case 'uint': this.itemsize = 8; this.kind = 'u'; break;
  1818. case 'f2': case 'float16': this.itemsize = 2; this.kind = 'f'; break;
  1819. case 'f4': case 'float32': this.itemsize = 4; this.kind = 'f'; break;
  1820. case 'f8': case 'float64': case 'float': this.itemsize = 8; this.kind = 'f'; break;
  1821. case 'c8': case 'complex64': this.itemsize = 8; this.kind = 'c'; break;
  1822. case 'c16': case 'complex128': case 'complex': this.itemsize = 16; this.kind = 'c'; break;
  1823. case 'M8': case 'M': this.itemsize = 8; this.kind = 'M'; break;
  1824. default:
  1825. if (obj.startsWith('V')) {
  1826. this.itemsize = parseInt(obj.substring(1), 10);
  1827. this.kind = 'V';
  1828. } else if (obj.startsWith('O')) {
  1829. this.itemsize = obj === 'O' ? 8 : parseInt(obj.substring(1), 10);
  1830. this.kind = 'O';
  1831. } else if (obj.startsWith('S')) {
  1832. this.itemsize = parseInt(obj.substring(1), 10);
  1833. this.kind = 'S';
  1834. } else if (obj.startsWith('U')) { // Unicode string
  1835. this.kind = 'U';
  1836. this.itemsize = 4 * parseInt(obj.substring(1), 10);
  1837. } else {
  1838. throw new python.Error(`Unsupported dtype '${obj}'.`);
  1839. }
  1840. break;
  1841. }
  1842. if (align) {
  1843. this.align = align;
  1844. }
  1845. if (copy) {
  1846. this.copy = copy;
  1847. }
  1848. }
  1849. get str() {
  1850. return (this.byteorder === '=' ? '<' : this.byteorder) + this.kind + this.itemsize.toString();
  1851. }
  1852. get name() {
  1853. switch (this.kind) {
  1854. case 'V': return `void${this.itemsize === 0 ? '' : (this.itemsize * 8)}`;
  1855. case 'S': return `bytes${this.itemsize === 0 ? '' : (this.itemsize * 8)}`;
  1856. case 'U': return `str${this.itemsize === 0 ? '' : (this.itemsize * 8)}`;
  1857. case 'M': return 'datetime64';
  1858. case 'b': return 'bool';
  1859. default: return this.__name__;
  1860. }
  1861. }
  1862. __setstate__(state) {
  1863. switch (state.length) {
  1864. case 8:
  1865. [
  1866. this.version, this.byteorder, this.subarray, this.names,
  1867. this.fields, this.elsize, this.alignment, this.int_dtypeflags
  1868. ] = state;
  1869. break;
  1870. case 9:
  1871. [
  1872. this.version, this.byteorder, this.subarray, this.names,
  1873. this.fields, this.elsize, this.alignment, this.int_dtypeflags,
  1874. this.metadata
  1875. ] = state;
  1876. break;
  1877. default:
  1878. throw new python.Error(`Unsupported numpy.dtype setstate length '${state.length}'.`);
  1879. }
  1880. }
  1881. get __name__() {
  1882. switch (this.kind) {
  1883. case 'b':
  1884. switch (this.itemsize) {
  1885. case 1: return 'boolean';
  1886. default: throw new python.Error(`Unsupported boolean itemsize '${this.itemsize}'.`);
  1887. }
  1888. case 'i':
  1889. switch (this.itemsize) {
  1890. case 1: return 'int8';
  1891. case 2: return 'int16';
  1892. case 4: return 'int32';
  1893. case 8: return 'int64';
  1894. default: throw new python.Error(`Unsupported int itemsize '${this.itemsize}'.`);
  1895. }
  1896. case 'u':
  1897. switch (this.itemsize) {
  1898. case 1: return 'uint8';
  1899. case 2: return 'uint16';
  1900. case 4: return 'uint32';
  1901. case 8: return 'uint64';
  1902. default: throw new python.Error(`Unsupported uint itemsize '${this.itemsize}'.`);
  1903. }
  1904. case 'f':
  1905. switch (this.itemsize) {
  1906. case 2: return 'float16';
  1907. case 4: return 'float32';
  1908. case 8: return 'float64';
  1909. default: throw new python.Error(`Unsupported float itemsize '${this.itemsize}'.`);
  1910. }
  1911. case 'c':
  1912. switch (this.itemsize) {
  1913. case 8: return 'complex64';
  1914. case 16: return 'complex128';
  1915. default: throw new python.Error(`Unsupported complex itemsize '${this.itemsize}'.`);
  1916. }
  1917. case 'S':
  1918. case 'U':
  1919. return 'string';
  1920. case 'M':
  1921. return 'datetime';
  1922. case 'O':
  1923. return 'object';
  1924. case 'V':
  1925. return 'void';
  1926. default:
  1927. throw new python.Error(`Unsupported dtype kind '${this.kind}'.`);
  1928. }
  1929. }
  1930. });
  1931. this.registerType('numpy.generic', class {});
  1932. this.registerType('numpy.inexact', class {});
  1933. this.registerType('numpy.bool_', class extends numpy.generic {});
  1934. this.registerType('numpy.number', class extends numpy.generic {});
  1935. this.registerType('numpy.integer', class extends numpy.number {});
  1936. this.registerType('numpy.floating', class extends numpy.inexact {});
  1937. this.registerType('numpy.float16', class extends numpy.floating {});
  1938. this.registerType('numpy.float32', class extends numpy.floating {});
  1939. this.registerType('numpy.float64', class extends numpy.floating {});
  1940. this.registerType('numpy.signedinteger', class extends numpy.integer {});
  1941. this.registerType('numpy.int8', class extends numpy.signedinteger {});
  1942. this.registerType('numpy.int16', class extends numpy.signedinteger {});
  1943. this.registerType('numpy.int32', class extends numpy.signedinteger {});
  1944. this.registerType('numpy.int64', class extends numpy.signedinteger {});
  1945. this.registerType('numpy.unsignedinteger', class extends numpy.integer {});
  1946. this.registerType('numpy.uint8', class extends numpy.unsignedinteger {});
  1947. this.registerType('numpy.uint16', class extends numpy.unsignedinteger {});
  1948. this.registerType('numpy.uint32', class extends numpy.unsignedinteger {});
  1949. this.registerType('numpy.uint64', class extends numpy.unsignedinteger {});
  1950. this.registerType('numpy.datetime64', class extends numpy.generic {});
  1951. this.registerType('gensim.models.doc2vec.Doctag', class {});
  1952. this.registerType('gensim.models.doc2vec.Doc2Vec', class {});
  1953. this.registerType('gensim.models.doc2vec.Doc2VecTrainables', class {});
  1954. this.registerType('gensim.models.doc2vec.Doc2VecVocab', class {});
  1955. this.registerType('gensim.models.fasttext.FastText', class {});
  1956. this.registerType('gensim.models.fasttext.FastTextTrainables', class {});
  1957. this.registerType('gensim.models.fasttext.FastTextVocab', class {});
  1958. this.registerType('gensim.models.fasttext.FastTextKeyedVectors', class {});
  1959. this.registerType('gensim.models.keyedvectors.Doc2VecKeyedVectors', class {});
  1960. this.registerType('gensim.models.keyedvectors.FastTextKeyedVectors', class {});
  1961. this.registerType('gensim.models.keyedvectors.KeyedVectors', class {});
  1962. this.registerType('gensim.models.keyedvectors.Vocab', class {});
  1963. this.registerType('gensim.models.keyedvectors.Word2VecKeyedVectors', class {});
  1964. this.registerType('gensim.models.phrases.Phrases', class {});
  1965. this.registerType('gensim.models.tfidfmodel.TfidfModel', class {});
  1966. this.registerType('gensim.models.word2vec.Vocab', class {});
  1967. this.registerType('gensim.models.word2vec.Word2Vec', class {});
  1968. this.registerType('gensim.models.word2vec.Word2VecTrainables', class {});
  1969. this.registerType('gensim.models.word2vec.Word2VecVocab', class {});
  1970. this.registerFunction('gensim.utils.call_on_class_only', function() {
  1971. throw new python.Error('This method should be called on a class object.');
  1972. });
  1973. this.registerType('google3.learning.deepmind.research.nbr.pbl_jax.clean_jaxline.utils.optimizers.ScaleByLarsState', class {
  1974. constructor(obj) {
  1975. Object.assign(this, obj);
  1976. }
  1977. });
  1978. this.registerType('joblib.numpy_pickle.NumpyArrayWrapper', class {
  1979. constructor(/* subtype, shape, dtype */) {
  1980. }
  1981. __setstate__(state) {
  1982. this.subclass = state.subclass;
  1983. this.dtype = state.dtype;
  1984. this.shape = state.shape;
  1985. this.order = state.order;
  1986. this.allow_mmap = state.allow_mmap;
  1987. }
  1988. __read__(unpickler) {
  1989. if (this.dtype.__name__ == 'object') {
  1990. return unpickler.load();
  1991. }
  1992. const size = this.dtype.itemsize * this.shape.reduce((a, b) => a * b, 1);
  1993. this.data = unpickler.read(size);
  1994. return execution.invoke(this.subclass, [ this.shape, this.dtype, this.data ]);
  1995. }
  1996. });
  1997. this.registerType('keras.engine.sequential.Sequential', class {});
  1998. this.registerType('lasagne.layers.conv.Conv2DLayer', class {});
  1999. this.registerType('lasagne.layers.dense.DenseLayer', class {});
  2000. this.registerType('lasagne.layers.input.InputLayer', class {});
  2001. this.registerType('lasagne.layers.pool.MaxPool2DLayer', class {});
  2002. this.registerType('lightgbm.sklearn.LGBMRegressor', class {});
  2003. this.registerType('lightgbm.sklearn.LGBMClassifier', class {});
  2004. this.registerType('lightgbm.basic.Booster', class {
  2005. constructor() {
  2006. this.average_output = false;
  2007. this.models = [];
  2008. this.loaded_parameter = '';
  2009. }
  2010. __setstate__(state) {
  2011. if (typeof state.handle === 'string') {
  2012. this.LoadModelFromString(state.handle);
  2013. return;
  2014. }
  2015. Object.assign(this, state);
  2016. }
  2017. LoadModelFromString(model_str) {
  2018. const lines = model_str.split('\n');
  2019. const signature = lines.shift() || '?';
  2020. if (signature.trim() !== 'tree') {
  2021. throw new python.Error(`Invalid signature '${signature.trim()}'.`);
  2022. }
  2023. // GBDT::LoadModelFromString() in https://github.com/microsoft/LightGBM/blob/master/src/boosting/gbdt_model_text.cpp
  2024. const key_vals = new Map();
  2025. while (lines.length > 0 && !lines[0].startsWith('Tree=')) {
  2026. const cur_line = lines.shift().trim();
  2027. if (cur_line.length > 0) {
  2028. const strs = cur_line.split('=');
  2029. if (strs.length === 1) {
  2030. key_vals.set(strs[0], '');
  2031. } else if (strs.length === 2) {
  2032. key_vals.set(strs[0], strs[1]);
  2033. } else if (strs.length > 2) {
  2034. if (strs[0] === "feature_names") {
  2035. key_vals.set(strs[0], cur_line.substring("feature_names=".length));
  2036. } else if (strs[0] == 'monotone_constraints') {
  2037. key_vals.set(strs[0], cur_line.substring('monotone_constraints='.length));
  2038. } else {
  2039. throw new python.Error(`Wrong line: ${cur_line.substring(0, Math.min(128, cur_line.length))}`);
  2040. }
  2041. }
  2042. }
  2043. }
  2044. const atoi = (key, value) => {
  2045. if (key_vals.has(key)) {
  2046. return parseInt(key_vals.get(key), 10);
  2047. }
  2048. if (value !== undefined) {
  2049. return value;
  2050. }
  2051. throw new python.Error(`Model file does not specify ${key}.`);
  2052. };
  2053. const list = (key, size) => {
  2054. if (key_vals.has(key)) {
  2055. const value = key_vals.get(key).split(' ');
  2056. if (value.length !== size) {
  2057. throw new python.Error(`Wrong size of ${key}.`);
  2058. }
  2059. return value;
  2060. }
  2061. throw new python.Error(`Model file does not contain ${key}.`);
  2062. };
  2063. this.version = key_vals.get('version') || '';
  2064. this.num_class = atoi('num_class');
  2065. this.num_tree_per_iteration = atoi('num_tree_per_iteration', this.num_class);
  2066. this.label_index = atoi('label_index');
  2067. this.max_feature_idx = atoi('max_feature_idx');
  2068. if (key_vals.has('average_output')) {
  2069. this.average_output = true;
  2070. }
  2071. this.feature_names = list('feature_names', this.max_feature_idx + 1);
  2072. this.feature_infos = list('feature_infos', this.max_feature_idx + 1);
  2073. if (key_vals.has('monotone_constraints')) {
  2074. this.monotone_constraints = list('monotone_constraints', this.max_feature_idx + 1);
  2075. }
  2076. if (key_vals.has('objective')) {
  2077. this.objective = key_vals.get('objective');
  2078. }
  2079. let tree = null;
  2080. while (lines.length > 0) {
  2081. const text = lines.shift();
  2082. const line = text.trim();
  2083. if (line.length === 0) {
  2084. continue;
  2085. }
  2086. if (line.startsWith('Tree=')) {
  2087. tree = { index: parseInt(line.split('=').pop(), 10) };
  2088. this.models.push(tree);
  2089. continue;
  2090. }
  2091. if (line === 'end of trees') {
  2092. break;
  2093. }
  2094. const param = line.split('=');
  2095. if (param.length !== 2) {
  2096. throw new python.Error(`Invalid property '${line}'.`);
  2097. }
  2098. const name = param[0].trim();
  2099. const value = param[1].trim();
  2100. tree[name] = value;
  2101. }
  2102. const ss = [];
  2103. let is_inparameter = false;
  2104. while (lines.length > 0) {
  2105. const text = lines.shift();
  2106. const line = text.trim();
  2107. if (line === 'parameters:') {
  2108. is_inparameter = true;
  2109. continue;
  2110. } else if (line === 'end of parameters') {
  2111. break;
  2112. } else if (is_inparameter) {
  2113. ss.push(line);
  2114. }
  2115. }
  2116. if (ss.length > 0) {
  2117. this.loaded_parameter = ss.join('\n');
  2118. }
  2119. }
  2120. });
  2121. this.registerFunction('megengine.functional.elemwise.clip', function() {});
  2122. this.registerFunction('megengine.functional.elemwise.sqrt', function() {});
  2123. this.registerFunction('megengine.functional.nn.conv2d', function() {});
  2124. this.registerFunction('megengine.functional.nn.relu', function() {});
  2125. this.registerFunction('megengine.functional.nn.sigmoid', function() {});
  2126. this.registerFunction('megengine.functional.tensor.arange', function() {});
  2127. this.registerFunction('megengine.functional.tensor.broadcast_to', function() {});
  2128. this.registerFunction('megengine.functional.tensor.concat', function() {});
  2129. this.registerFunction('megengine.functional.tensor.expand_dims', function() {});
  2130. this.registerFunction('megengine.functional.tensor.flatten', function() {});
  2131. this.registerFunction('megengine.functional.tensor.full', function() {});
  2132. this.registerFunction('megengine.functional.tensor.reshape', function() {});
  2133. this.registerFunction('megengine.functional.tensor.split', function() {});
  2134. this.registerFunction('megengine.functional.tensor.stack', function() {});
  2135. this.registerFunction('megengine.functional.tensor.transpose', function() {});
  2136. this.registerFunction('megengine.functional.vision.interpolate', function() {});
  2137. this.registerFunction('megengine.module.qat.module.QATModule._apply_fakequant_with_observer', function() {});
  2138. this.registerType('megengine.core._imperative_rt.common.CompNode', class {});
  2139. this.registerType('megengine.core._imperative_rt.ops.ElemwiseMultiType', class {});
  2140. this.registerType('megengine.core._imperative_rt.ops.FakeQuant', class {});
  2141. this.registerType('megengine.core._imperative_rt.ops.GetVarShape', class {});
  2142. this.registerType('megengine.core._imperative_rt.ops.Resize', class {});
  2143. this.registerType('megengine.core.ops._internal.param_defs.ConvolutionV0.Mode', class {});
  2144. this.registerType('megengine.core.ops._internal.param_defs.Convolution.ComputeMode', class {});
  2145. this.registerType('megengine.distributed.group.Group', class {});
  2146. this.registerType('megengine.module.activation.ReLU', class {});
  2147. this.registerType('megengine.module.activation.Softmax', class {});
  2148. this.registerType('megengine.module.adaptive_pooling.AdaptiveAvgPool2d', class {});
  2149. this.registerType('megengine.module.batchnorm.BatchNorm1d', class {});
  2150. this.registerType('megengine.module.batchnorm.BatchNorm2d', class {});
  2151. this.registerType('megengine.module.conv.Conv2d', class {});
  2152. this.registerType('megengine.module.conv.ConvTranspose2d', class {});
  2153. this.registerType('megengine.module.conv_bn.ConvBn2d', class {});
  2154. this.registerType('megengine.module.dropout.Dropout', class {});
  2155. this.registerType('megengine.module.identity.Identity', class {});
  2156. this.registerType('megengine.module.linear.Linear', class {});
  2157. this.registerType('megengine.module.module.Module', class {});
  2158. this.registerType('megengine.module.normalization.InstanceNorm', class {});
  2159. this.registerType('megengine.module.normalization.GroupNorm', class {});
  2160. this.registerType('megengine.module.pooling.AvgPool2d', class {});
  2161. this.registerType('megengine.module.pooling.MaxPool2d', class {});
  2162. this.registerType('megengine.module.qat.concat.Concat', class {});
  2163. this.registerType('megengine.module.qat.elemwise.Elemwise', class {});
  2164. this.registerType('megengine.module.sequential.Sequential', class {});
  2165. this.registerType('megengine.quantization.fake_quant.FakeQuantize', class {});
  2166. this.registerType('megengine.quantization.utils.QParams', class {});
  2167. this.registerType('megengine.quantization.utils.QuantMode', class {});
  2168. this.registerType('megengine.quantization.observer.ExponentialMovingAverageObserver', class {});
  2169. this.registerType('megengine.quantization.observer.HistogramObserver', class {});
  2170. this.registerType('megengine.quantization.observer.MinMaxObserver', class {});
  2171. this.registerType('megengine.quantization.observer.PassiveObserver', class {});
  2172. this.registerType('megengine.quantization.observer.SyncExponentialMovingAverageObserver', class {});
  2173. this.registerType('megengine.quantization.observer.SyncMinMaxObserver', class {});
  2174. this.registerType('megengine.traced_module.expr.Apply', class {});
  2175. this.registerType('megengine.traced_module.expr.CallFunction', class {});
  2176. this.registerType('megengine.traced_module.expr.CallMethod', class {});
  2177. this.registerType('megengine.traced_module.expr.Constant', class {});
  2178. this.registerType('megengine.traced_module.expr.GetAttr', class {});
  2179. this.registerType('megengine.traced_module.expr.Input', class {});
  2180. this.registerType('megengine.traced_module.fake_quant.FakeQuantize', class {});
  2181. this.registerType('megengine.traced_module.node.ModuleNode', class {});
  2182. this.registerType('megengine.traced_module.node.NodeMixin', class {});
  2183. this.registerType('megengine.traced_module.node.TensorNode', class {});
  2184. this.registerType('megengine.traced_module.pytree.ArgsIndex', class {});
  2185. this.registerType('megengine.traced_module.serialization._ModuleState', class {});
  2186. this.registerType('megengine.traced_module.traced_module.InternalGraph', class {});
  2187. this.registerType('megengine.traced_module.traced_module.NameSpace', class {});
  2188. this.registerType('megengine.traced_module.traced_module.TracedModule', class {});
  2189. this.registerType('megengine.tensor.Parameter', class {
  2190. constructor(data, dtype, device) {
  2191. this.data = data;
  2192. this.dtype = dtype;
  2193. this.device = device;
  2194. }
  2195. });
  2196. this.registerType('megengine.traced_module.pytree.TreeDef', class {
  2197. toString() {
  2198. let content = '';
  2199. for (const child of this.children_defs) {
  2200. content += `${child},`;
  2201. }
  2202. if (typeof this.type === "string") {
  2203. return `${this.type.split(".").slice(-1)}(${content})`;
  2204. }
  2205. return `${this.type.__name__}(${content})`;
  2206. }
  2207. });
  2208. this.registerType('megengine.traced_module.pytree.LeafDef', class {
  2209. toString() {
  2210. let content = '';
  2211. if (this.const_val !== null) {
  2212. content += this.const_val;
  2213. } else {
  2214. content += '[';
  2215. }
  2216. for (const t of Object.values(this.type)) {
  2217. content += t.__name__;
  2218. }
  2219. content += ']';
  2220. return content;
  2221. }
  2222. });
  2223. this.registerType('megengine.tensor.Tensor', class {
  2224. constructor(data, dtype, device) {
  2225. this.data = data;
  2226. this.dtype = dtype;
  2227. this.device = device;
  2228. }
  2229. });
  2230. this.registerType('megengine.core.tensor.dtype.QuantDtypeMeta', class {
  2231. constructor(name, cname, np_dtype, qmin, qmax, is_signed) {
  2232. this.name = name;
  2233. this.cname = cname;
  2234. this.np_dtype = np_dtype;
  2235. this.qmin = qmin;
  2236. this.qmax = qmax;
  2237. this.is_signed = is_signed;
  2238. }
  2239. });
  2240. this.registerType('nolearn.lasagne.base.BatchIterator', class {});
  2241. this.registerType('nolearn.lasagne.base.Layers', class {});
  2242. this.registerType('nolearn.lasagne.base.NeuralNet', class {});
  2243. this.registerType('nolearn.lasagne.base.TrainSplit', class {});
  2244. this.registerType('nolearn.lasagne.handlers.PrintLayerInfo', class {});
  2245. this.registerType('nolearn.lasagne.handlers.PrintLog', class {});
  2246. this.registerType('numpy.ndarray', class {
  2247. constructor(shape, dtype, buffer, offset, strides, order) {
  2248. this.shape = shape;
  2249. this.dtype = dtype;
  2250. this.data = buffer !== undefined ? buffer : null;
  2251. this.offset = offset !== undefined ? offset : 0;
  2252. this._strides = strides !== undefined ? strides : null;
  2253. this.order = offset !== undefined ? order : null;
  2254. this.flags = {};
  2255. this._read();
  2256. }
  2257. __setstate__(state) {
  2258. [this.version, this.shape, this.dtype, this.flags.fn, this.data] = state;
  2259. this._read();
  2260. }
  2261. flatten() {
  2262. const size = this.shape.reduce((a, b) => a * b, 1);
  2263. const value = execution.invoke('numpy.ndarray', [
  2264. [ size ], this.dtype, this.data, this.offset, this.strides, this.order
  2265. ]);
  2266. value.flags = this.flags;
  2267. return value;
  2268. }
  2269. tobytes() {
  2270. return this.data;
  2271. }
  2272. tolist() {
  2273. if (this.shape.length < 0 || this.shape.length > 1) {
  2274. throw new python.Error(`Unsupported shape '${JSON.stringify(this.shape)}'.`);
  2275. }
  2276. const size = this.shape.reduce((a, b) => a * b, 1);
  2277. const list = new Array(size);
  2278. switch (this.dtype.kind) {
  2279. case 'U': {
  2280. const data = new Uint32Array(new Uint8Array(this.data).buffer);
  2281. const itemsize = this.dtype.itemsize >> 2;
  2282. let offset = 0;
  2283. for (let i = 0; i < size; i++) {
  2284. const buffer = data.subarray(offset, offset + itemsize);
  2285. const index = buffer.indexOf(0);
  2286. list[i] = Array.from(index >= 0 ? buffer.subarray(0, index) : buffer).map((c) => String.fromCodePoint(c)).join('');
  2287. offset += itemsize;
  2288. }
  2289. return list;
  2290. }
  2291. case 'S': {
  2292. const data = this.data;
  2293. const itemsize = this.dtype.itemsize;
  2294. const decoder = new TextDecoder('utf-8');
  2295. let offset = 0;
  2296. for (let i = 0; i < size; i++) {
  2297. const buffer = data.subarray(offset, offset + itemsize);
  2298. const index = buffer.indexOf(0);
  2299. list[i] = decoder.decode(index >= 0 ? buffer.subarray(0, index) : buffer);
  2300. offset += itemsize;
  2301. }
  2302. return list;
  2303. }
  2304. case 'O': {
  2305. return this.data;
  2306. }
  2307. default: {
  2308. throw new python.Error(`Type kind '${this.dtype.kind}' not implemented.`);
  2309. }
  2310. }
  2311. }
  2312. get itemsize() {
  2313. return this.dtype.itemsize;
  2314. }
  2315. get size() {
  2316. return (this.shape || []).reduce((a, b) => a * b, 1);
  2317. }
  2318. get strides() {
  2319. if (!this._strides) {
  2320. const shape = this.shape;
  2321. const strides = new Array(shape.length);
  2322. let stride = this.itemsize;
  2323. for (let i = shape.length - 1; i >= 0; i--) {
  2324. strides[i] = stride;
  2325. stride *= shape[i];
  2326. }
  2327. return strides;
  2328. }
  2329. return this._strides;
  2330. }
  2331. _read() {
  2332. if (this.data) {
  2333. const length = this.dtype.itemsize * this.size;
  2334. if (typeof this.data == 'string') {
  2335. this.data = this._unescape(this.data, length);
  2336. if (this.data.length != length) {
  2337. throw new python.Error('Invalid string array data size.');
  2338. }
  2339. } else if (this.data.length != length) {
  2340. // throw new python.Error('Invalid array data size.');
  2341. }
  2342. }
  2343. }
  2344. _unescape(token, size) {
  2345. const length = token.length;
  2346. const a = new Uint8Array(length);
  2347. if (size && size == length) {
  2348. for (let p = 0; p < size; p++) {
  2349. a[p] = token.charCodeAt(p);
  2350. }
  2351. return a;
  2352. }
  2353. let i = 0;
  2354. let o = 0;
  2355. while (i < length) {
  2356. let c = token.charCodeAt(i++);
  2357. if (c !== 0x5C || i >= length) {
  2358. a[o++] = c;
  2359. } else {
  2360. c = token.charCodeAt(i++);
  2361. switch (c) {
  2362. case 0x27: a[o++] = 0x27; break; // '
  2363. case 0x5C: a[o++] = 0x5C; break; // \\
  2364. case 0x22: a[o++] = 0x22; break; // "
  2365. case 0x72: a[o++] = 0x0D; break; // \r
  2366. case 0x6E: a[o++] = 0x0A; break; // \n
  2367. case 0x74: a[o++] = 0x09; break; // \t
  2368. case 0x62: a[o++] = 0x08; break; // \b
  2369. case 0x58: // x
  2370. case 0x78: { // X
  2371. const xsi = i - 1;
  2372. const xso = o;
  2373. for (let xi = 0; xi < 2; xi++) {
  2374. if (i >= length) {
  2375. i = xsi;
  2376. o = xso;
  2377. a[o] = 0x5c;
  2378. break;
  2379. }
  2380. let xd = token.charCodeAt(i++);
  2381. xd = xd >= 65 && xd <= 70 ? xd - 55 : xd >= 97 && xd <= 102 ? xd - 87 : xd >= 48 && xd <= 57 ? xd - 48 : -1;
  2382. if (xd === -1) {
  2383. i = xsi;
  2384. o = xso;
  2385. a[o] = 0x5c;
  2386. break;
  2387. }
  2388. a[o] = a[o] << 4 | xd;
  2389. }
  2390. o++;
  2391. break;
  2392. }
  2393. default:
  2394. if (c < 48 || c > 57) { // 0-9
  2395. a[o++] = 0x5c;
  2396. a[o++] = c;
  2397. } else {
  2398. i--;
  2399. const osi = i;
  2400. const oso = o;
  2401. for (let oi = 0; oi < 3; oi++) {
  2402. if (i >= length) {
  2403. i = osi;
  2404. o = oso;
  2405. a[o] = 0x5c;
  2406. break;
  2407. }
  2408. const od = token.charCodeAt(i++);
  2409. if (od < 48 || od > 57) {
  2410. i = osi;
  2411. o = oso;
  2412. a[o] = 0x5c;
  2413. break;
  2414. }
  2415. a[o] = a[o] << 3 | od - 48;
  2416. }
  2417. o++;
  2418. }
  2419. break;
  2420. }
  2421. }
  2422. }
  2423. return a.slice(0, o);
  2424. }
  2425. });
  2426. this.registerType('numpy.ma.core.MaskedArray', class extends numpy.ndarray {
  2427. constructor(data /*, mask, dtype, copy, subok, ndmin, fill_value, keep_mask, hard_mask, shrink, order */) {
  2428. super(data.shape, data.dtype, data.data);
  2429. }
  2430. });
  2431. this.registerType('numpy.core.memmap.memmap', class extends numpy.ndarray {
  2432. constructor(shape, dtype) {
  2433. super(shape, dtype);
  2434. }
  2435. });
  2436. this.registerType('pathlib.Path', class {});
  2437. this.registerType('pathlib.PosixPath', class {});
  2438. this.registerType('pathlib.WindowsPath', class {});
  2439. this.registerType('sklearn._loss.link.BaseLink', class {});
  2440. this.registerType('sklearn._loss.link.MultinomialLogit', class extends sklearn._loss.link.BaseLink {});
  2441. this.registerType('sklearn._loss.loss.BaseLoss', class {});
  2442. this.registerType('sklearn._loss.loss.HalfMultinomialLoss', class extends sklearn._loss.loss.BaseLoss {});
  2443. this.registerType('sklearn.base.BaseEstimator', class {});
  2444. this.registerType('sklearn.base.TransformerMixin', class {});
  2445. this.registerType('sklearn.calibration._CalibratedClassifier', class {});
  2446. this.registerType('sklearn.calibration._SigmoidCalibration', class {});
  2447. this.registerType('sklearn.calibration.CalibratedClassifierCV', class {});
  2448. this.registerType('sklearn.cluster._agglomerative.FeatureAgglomeration', class {});
  2449. this.registerType('sklearn.cluster._dbscan.DBSCAN', class {});
  2450. this.registerType('sklearn.cluster._kmeans.KMeans', class {});
  2451. this.registerType('sklearn.cluster.k_means_.MiniBatchKMeans', class {});
  2452. this.registerType('sklearn.compose._column_transformer.ColumnTransformer', class {});
  2453. this.registerType('sklearn.compose._target.TransformedTargetRegressor', class {});
  2454. this.registerType('sklearn.cross_decomposition._pls.PLSRegression', class {});
  2455. this.registerType('sklearn.decomposition._fastica.FastICA', class {});
  2456. this.registerType('sklearn.decomposition._pca.PCA', class {});
  2457. this.registerType('sklearn.decomposition._truncated_svd.TruncatedSVD', class {});
  2458. this.registerType('sklearn.decomposition.pca.PCA', class {});
  2459. this.registerType('sklearn.decomposition.PCA', class {});
  2460. this.registerType('sklearn.decomposition.truncated_svd.TruncatedSVD', class {});
  2461. this.registerType('sklearn.discriminant_analysis.LinearDiscriminantAnalysis', class {});
  2462. this.registerType('sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis', class {});
  2463. this.registerType('sklearn.dummy.DummyClassifier', class {});
  2464. this.registerType('sklearn.dummy.DummyRegressor', class {});
  2465. this.registerType('sklearn.externals.joblib.numpy_pickle.NumpyArrayWrapper', class {
  2466. constructor(/* subtype, shape, dtype */) {
  2467. }
  2468. __setstate__(state) {
  2469. this.subclass = state.subclass;
  2470. this.dtype = state.dtype;
  2471. this.shape = state.shape;
  2472. this.order = state.order;
  2473. this.allow_mmap = state.allow_mmap;
  2474. }
  2475. __read__(unpickler) {
  2476. if (this.dtype.__name__ == 'object') {
  2477. return unpickler.load();
  2478. }
  2479. const size = this.dtype.itemsize * this.shape.reduce((a, b) => a * b, 1);
  2480. this.data = unpickler.read(size);
  2481. return execution.invoke(this.subclass, [ this.shape, this.dtype, this.data ]);
  2482. }
  2483. });
  2484. this.registerType('sklearn.externals.joblib.numpy_pickle.NDArrayWrapper', class {
  2485. constructor(/* subtype, shape, dtype */) {
  2486. }
  2487. __setstate__(state) {
  2488. this.subclass = state.subclass;
  2489. this.filename = state.state;
  2490. this.allow_mmap = state.allow_mmap;
  2491. }
  2492. __read__(/* unpickler */) {
  2493. return this; // return execution.invoke(this.subclass, [ this.shape, this.dtype, this.data ]);
  2494. }
  2495. });
  2496. this.registerType('sklearn.ensemble._bagging.BaggingClassifier', class {});
  2497. this.registerType('sklearn.ensemble._bagging.BaggingRegressor', class {});
  2498. this.registerType('sklearn.ensemble._forest.RandomForestClassifier', class {});
  2499. this.registerType('sklearn.ensemble._forest.RandomForestRegressor', class {});
  2500. this.registerType('sklearn.ensemble._forest.ExtraTreesClassifier', class {});
  2501. this.registerType('sklearn.ensemble._forest.ExtraTreesRegressor', class {});
  2502. this.registerType('sklearn.ensemble._gb_losses.BinomialDeviance', class {});
  2503. this.registerType('sklearn.ensemble._gb_losses.LeastSquaresError', class {});
  2504. this.registerType('sklearn.ensemble._gb_losses.MultinomialDeviance', class {});
  2505. this.registerType('sklearn.ensemble._gb.GradientBoostingClassifier', class {});
  2506. this.registerType('sklearn.ensemble._gb.GradientBoostingRegressor', class {});
  2507. this.registerType('sklearn.ensemble._hist_gradient_boosting.binning._BinMapper', class {});
  2508. this.registerType('sklearn.ensemble._hist_gradient_boosting.gradient_boosting.HistGradientBoostingRegressor', class {});
  2509. this.registerType('sklearn.ensemble._hist_gradient_boosting.loss.LeastSquares', class {});
  2510. this.registerType('sklearn.ensemble._hist_gradient_boosting.predictor.TreePredictor', class {});
  2511. this.registerType('sklearn.ensemble._iforest.IsolationForest', class {});
  2512. this.registerType('sklearn.ensemble._stacking.StackingClassifier', class {});
  2513. this.registerType('sklearn.ensemble._voting.VotingClassifier', class {});
  2514. this.registerType('sklearn.ensemble._voting.VotingRegressor', class {});
  2515. this.registerType('sklearn.ensemble._weight_boosting.AdaBoostClassifier', class {});
  2516. this.registerType('sklearn.ensemble._weight_boosting.AdaBoostRegressor', class {});
  2517. this.registerType('sklearn.ensemble.forest.RandomForestClassifier', class {});
  2518. this.registerType('sklearn.ensemble.forest.RandomForestRegressor', class {});
  2519. this.registerType('sklearn.ensemble.forest.ExtraTreesClassifier', class {});
  2520. this.registerType('sklearn.ensemble.gradient_boosting.BinomialDeviance', class {});
  2521. this.registerType('sklearn.ensemble.gradient_boosting.GradientBoostingClassifier', class {});
  2522. this.registerType('sklearn.ensemble.gradient_boosting.LogOddsEstimator', class {});
  2523. this.registerType('sklearn.ensemble.gradient_boosting.MultinomialDeviance', class {});
  2524. this.registerType('sklearn.ensemble.gradient_boosting.PriorProbabilityEstimator', class {});
  2525. this.registerType('sklearn.ensemble.weight_boosting.AdaBoostClassifier', class {});
  2526. this.registerType('sklearn.feature_extraction._hashing.FeatureHasher', class {});
  2527. this.registerType('sklearn.feature_extraction.text.CountVectorizer', class {});
  2528. this.registerType('sklearn.feature_extraction.text.HashingVectorizer', class {});
  2529. this.registerType('sklearn.feature_extraction.text.TfidfTransformer', class {});
  2530. this.registerType('sklearn.feature_extraction.text.TfidfVectorizer', class {});
  2531. this.registerType('sklearn.feature_selection._from_model.SelectFromModel', class {});
  2532. this.registerFunction('sklearn.feature_selection._univariate_selection.chi2', function() {
  2533. throw new python.Error("'sklearn.feature_selection._univariate_selection.chi2' not implemented.");
  2534. });
  2535. this.registerType('sklearn.feature_selection._univariate_selection.GenericUnivariateSelect', class {});
  2536. this.registerType('sklearn.feature_selection._univariate_selection.SelectKBest', class {});
  2537. this.registerType('sklearn.feature_selection._univariate_selection.SelectPercentile', class {});
  2538. this.registerType('sklearn.feature_selection._variance_threshold.VarianceThreshold', class {});
  2539. this.registerType('sklearn.feature_selection.univariate_selection.SelectKBest', class {});
  2540. this.registerType('sklearn.feature_selection.variance_threshold.VarianceThreshold', class {});
  2541. this.registerType('sklearn.gaussian_process._gpr.GaussianProcessRegressor', class {});
  2542. this.registerType('sklearn.gaussian_process.gpc.GaussianProcessClassifier', class {});
  2543. this.registerType('sklearn.gaussian_process.kernels.ConstantKernel', class {});
  2544. this.registerType('sklearn.gaussian_process.kernels.DotProduct', class {});
  2545. this.registerType('sklearn.gaussian_process.kernels.Product', class {});
  2546. this.registerType('sklearn.gaussian_process.kernels.RBF', class {});
  2547. this.registerType('sklearn.gaussian_process.kernels.Sum', class {});
  2548. this.registerType('sklearn.gaussian_process.kernels.WhiteKernel', class {});
  2549. this.registerType('sklearn.grid_search._CVScoreTuple', class {});
  2550. this.registerType('sklearn.grid_search.GridSearchCV', class {});
  2551. this.registerType('sklearn.impute._base.SimpleImputer', class {});
  2552. this.registerType('sklearn.impute.SimpleImputer', class {});
  2553. this.registerType('sklearn.isotonic.IsotonicRegression', class {});
  2554. this.registerType('sklearn.linear_model._base.LinearRegression', class {});
  2555. this.registerType('sklearn.linear_model._bayes.BayesianRidge', class {});
  2556. this.registerType('sklearn.linear_model._coordinate_descent.ElasticNetCV', class {});
  2557. this.registerType('sklearn.linear_model._coordinate_descent.ElasticNet', class {});
  2558. this.registerType('sklearn.linear_model._coordinate_descent.Lasso', class {});
  2559. this.registerType('sklearn.linear_model._least_angle.LassoLarsCV', class {});
  2560. this.registerType('sklearn.linear_model._logistic.LogisticRegression', class {});
  2561. this.registerType('sklearn.linear_model._quantile.QuantileRegressor', class {});
  2562. this.registerType('sklearn.linear_model._ridge.Ridge', class {});
  2563. this.registerType('sklearn.linear_model._ridge.RidgeClassifier', class {});
  2564. this.registerType('sklearn.linear_model._sgd_fast.Hinge', class {});
  2565. this.registerType('sklearn.linear_model._sgd_fast.Log', class {});
  2566. this.registerType('sklearn.linear_model._sgd_fast.ModifiedHuber', class {});
  2567. this.registerType('sklearn.linear_model._sgd_fast.SquaredHinge', class {});
  2568. this.registerType('sklearn.linear_model._stochastic_gradient.SGDClassifier', class {});
  2569. this.registerType('sklearn.linear_model.base.LinearRegression', class {});
  2570. this.registerType('sklearn.linear_model.sgd_fast.Hinge', class {});
  2571. this.registerType('sklearn.linear_model.LogisticRegression', class {});
  2572. this.registerType('sklearn.linear_model.logistic.LogisticRegression', class {});
  2573. this.registerType('sklearn.linear_model.logistic.LogisticRegressionCV', class {});
  2574. this.registerType('sklearn.linear_model.LassoLars​', class {});
  2575. this.registerType('sklearn.linear_model.ridge.Ridge', class {});
  2576. this.registerType('sklearn.linear_model.sgd_fast.Log', class {});
  2577. this.registerType('sklearn.linear_model.stochastic_gradient.SGDClassifier', class {});
  2578. this.registerType('sklearn.manifold._t_sne.TSNE', class {});
  2579. this.registerType('sklearn.metrics._dist_metrics.EuclideanDistance', class {});
  2580. this.registerType('sklearn.metrics._dist_metrics.EuclideanDistance64', class {});
  2581. this.registerType('sklearn.metrics._scorer._PassthroughScorer', class {});
  2582. this.registerType('sklearn.metrics._scorer._PredictScorer', class {});
  2583. this.registerType('sklearn.metrics.scorer._PredictScorer', class {});
  2584. this.registerType('sklearn.metrics._scorer._ThresholdScorer', class {});
  2585. this.registerType('sklearn.mixture._bayesian_mixture.BayesianGaussianMixture', class {});
  2586. this.registerType('sklearn.mixture._gaussian_mixture.GaussianMixture', class {});
  2587. this.registerType('sklearn.model_selection._search.GridSearchCV', class {});
  2588. this.registerType('sklearn.model_selection._search.RandomizedSearchCV', class {});
  2589. this.registerType('sklearn.model_selection._split.KFold', class {});
  2590. this.registerType('sklearn.model_selection._split.StratifiedKFold', class {});
  2591. this.registerType('sklearn.multiclass.OneVsRestClassifier', class {});
  2592. this.registerType('sklearn.multioutput.MultiOutputClassifier', class {});
  2593. this.registerType('sklearn.multioutput.MultiOutputRegressor', class {});
  2594. this.registerType('sklearn.naive_bayes.BernoulliNB', class {});
  2595. this.registerType('sklearn.naive_bayes.ComplementNB', class {});
  2596. this.registerType('sklearn.naive_bayes.GaussianNB', class {});
  2597. this.registerType('sklearn.naive_bayes.MultinomialNB', class {});
  2598. this.registerType('sklearn.neighbors.ball_tree.BallTree', class {});
  2599. this.registerType('sklearn.neighbors._classification.KNeighborsClassifier', class {});
  2600. this.registerType('sklearn.neighbors._dist_metrics.newObj', class {});
  2601. this.registerType('sklearn.neighbors._dist_metrics.EuclideanDistance', class {});
  2602. this.registerType('sklearn.neighbors._kd_tree.KDTree', class {});
  2603. this.registerType('sklearn.neighbors._kd_tree.newObj', class {});
  2604. this.registerType('sklearn.neighbors._regression.KNeighborsRegressor', class {});
  2605. this.registerType('sklearn.neighbors._unsupervised.NearestNeighbors', class {});
  2606. this.registerType('sklearn.neighbors.classification.KNeighborsClassifier', class {});
  2607. this.registerType('sklearn.neighbors.dist_metrics.newObj', class {});
  2608. this.registerType('sklearn.neighbors.dist_metrics.EuclideanDistance', class {});
  2609. this.registerType('sklearn.neighbors.kd_tree.newObj', class {});
  2610. this.registerType('sklearn.neighbors.kd_tree.KDTree', class {});
  2611. this.registerType('sklearn.neighbors.KNeighborsClassifier', class {});
  2612. this.registerType('sklearn.neighbors.KNeighborsRegressor', class {});
  2613. this.registerType('sklearn.neighbors.regression.KNeighborsRegressor', class {});
  2614. this.registerType('sklearn.neighbors.unsupervised.NearestNeighbors', class {});
  2615. this.registerType('sklearn.neural_network._multilayer_perceptron.MLPClassifier', class {});
  2616. this.registerType('sklearn.neural_network._multilayer_perceptron.MLPRegressor', class {});
  2617. this.registerType('sklearn.neural_network._stochastic_optimizers.AdamOptimizer', class {});
  2618. this.registerType('sklearn.neural_network._stochastic_optimizers.SGDOptimizer', class {});
  2619. this.registerType('sklearn.neural_network.rbm.BernoulliRBM', class {});
  2620. this.registerType('sklearn.neural_network.multilayer_perceptron.MLPClassifier', class {});
  2621. this.registerType('sklearn.neural_network.multilayer_perceptron.MLPRegressor', class {});
  2622. this.registerType('sklearn.neural_network.stochastic_gradient.SGDClassifier', class {});
  2623. this.registerType('sklearn.pipeline.Pipeline', class {});
  2624. this.registerType('sklearn.pipeline.FeatureUnion', class {});
  2625. this.registerType('sklearn.preprocessing._data.MinMaxScaler', class {});
  2626. this.registerType('sklearn.preprocessing._data.MaxAbsScaler', class {});
  2627. this.registerType('sklearn.preprocessing._data.Normalizer', class {});
  2628. this.registerType('sklearn.preprocessing._data.PolynomialFeatures', class {});
  2629. this.registerType('sklearn.preprocessing._data.PowerTransformer', class {});
  2630. this.registerType('sklearn.preprocessing._data.QuantileTransformer', class {});
  2631. this.registerType('sklearn.preprocessing._data.RobustScaler', class {});
  2632. this.registerType('sklearn.preprocessing._data.StandardScaler', class {});
  2633. this.registerType('sklearn.preprocessing._discretization.KBinsDiscretizer', class {});
  2634. this.registerType('sklearn.preprocessing._encoders.OneHotEncoder', class {});
  2635. this.registerType('sklearn.preprocessing._encoders.OrdinalEncoder', class {});
  2636. this.registerType('sklearn.preprocessing._function_transformer.FunctionTransformer', class {});
  2637. this.registerType('sklearn.preprocessing._label.LabelBinarizer', class {});
  2638. this.registerType('sklearn.preprocessing._label.LabelEncoder', class {});
  2639. this.registerType('sklearn.preprocessing._label.MultiLabelBinarizer', class {});
  2640. this.registerType('sklearn.preprocessing._polynomial.PolynomialFeatures', class {});
  2641. this.registerType('sklearn.preprocessing.data.Binarizer', class {});
  2642. this.registerType('sklearn.preprocessing.data.MaxAbsScaler', class {});
  2643. this.registerType('sklearn.preprocessing.data.MinMaxScaler', class {});
  2644. this.registerType('sklearn.preprocessing.data.Normalizer', class {});
  2645. this.registerType('sklearn.preprocessing.data.OneHotEncoder', class {});
  2646. this.registerType('sklearn.preprocessing.data.PolynomialFeatures', class {});
  2647. this.registerType('sklearn.preprocessing.data.PowerTransformer', class {});
  2648. this.registerType('sklearn.preprocessing.data.RobustScaler', class {});
  2649. this.registerType('sklearn.preprocessing.data.QuantileTransformer', class {});
  2650. this.registerType('sklearn.preprocessing.data.StandardScaler', class {});
  2651. this.registerType('sklearn.preprocessing.imputation.Imputer', class {});
  2652. this.registerType('sklearn.preprocessing.label.LabelBinarizer', class {});
  2653. this.registerType('sklearn.preprocessing.label.LabelEncoder', class {});
  2654. this.registerType('sklearn.preprocessing.label.MultiLabelBinarizer', class {});
  2655. this.registerType('sklearn.svm._classes.LinearSVC', class {});
  2656. this.registerType('sklearn.svm._classes.NuSVC', class {});
  2657. this.registerType('sklearn.svm._classes.OneClassSVM', class {});
  2658. this.registerType('sklearn.svm._classes.SVC', class {});
  2659. this.registerType('sklearn.svm._classes.SVR', class {});
  2660. this.registerType('sklearn.svm.classes.LinearSVC', class {});
  2661. this.registerType('sklearn.svm.classes.OneClassSVM', class {});
  2662. this.registerType('sklearn.svm.classes.SVC', class {});
  2663. this.registerType('sklearn.svm.classes.SVR', class {});
  2664. this.registerType('sklearn.tree._classes.DecisionTreeClassifier', class {});
  2665. this.registerType('sklearn.tree._classes.DecisionTreeRegressor', class {});
  2666. this.registerType('sklearn.tree._classes.ExtraTreeClassifier', class {});
  2667. this.registerType('sklearn.tree._classes.ExtraTreeRegressor', class {});
  2668. this.registerType('sklearn.tree._tree.Tree', class {
  2669. constructor(n_features, n_classes, n_outputs) {
  2670. this.n_features = n_features;
  2671. this.n_classes = n_classes;
  2672. this.n_outputs = n_outputs;
  2673. }
  2674. __setstate__(state) {
  2675. this.max_depth = state.max_depth;
  2676. this.node_count = state.node_count;
  2677. this.nodes = state.nodes;
  2678. this.values = state.values;
  2679. }
  2680. });
  2681. this.registerType('sklearn.tree.tree.DecisionTreeClassifier', class {});
  2682. this.registerType('sklearn.tree.tree.DecisionTreeRegressor', class {});
  2683. this.registerType('sklearn.tree.tree.ExtraTreeClassifier', class {});
  2684. this.registerType('sklearn.utils._bunch.Bunch', class {});
  2685. this.registerType('sklearn.utils.deprecation.DeprecationDict', class {});
  2686. this.registerType('pickle.Unpickler', class {
  2687. constructor(data) {
  2688. this._reader = data instanceof Uint8Array ? new python.BinaryReader(data) : new python.StreamReader(data);
  2689. this.persistent_load = () => {
  2690. throw new python.Error('Unsupported persistent id.');
  2691. };
  2692. }
  2693. load() {
  2694. const reader = this._reader;
  2695. const marker = [];
  2696. let stack = [];
  2697. const memo = {};
  2698. let size = 0;
  2699. while (reader.position < reader.length) {
  2700. const opcode = reader.byte();
  2701. // console.log((reader.position - 1).toString() + ' ' + Object.entries(OpCode).find(([, value]) => value === opcode)[0]);
  2702. // https://svn.python.org/projects/python/trunk/Lib/pickletools.py
  2703. // https://github.com/python/cpython/blob/master/Lib/pickle.py
  2704. switch (opcode) {
  2705. case 128: { // PROTO
  2706. const version = reader.byte();
  2707. if (version > 5) {
  2708. throw new python.Error(`Unsupported protocol version '${version}'.`);
  2709. }
  2710. break;
  2711. }
  2712. case 99: { // GLOBAL 'c'
  2713. const module = reader.line();
  2714. const name = reader.line();
  2715. stack.push(this.find_class(module, name));
  2716. break;
  2717. }
  2718. case 147: { // STACK_GLOBAL '\x93' (Protocol 4)
  2719. const name = stack.pop();
  2720. const module = stack.pop();
  2721. stack.push(this.find_class(module, name));
  2722. break;
  2723. }
  2724. case 111: { // OBJ 'o'
  2725. const args = stack;
  2726. const cls = args.pop();
  2727. stack = marker.pop();
  2728. const obj = this._instantiate(cls, args);
  2729. stack.push(obj);
  2730. break;
  2731. }
  2732. case 112 : { // PUT 'p'
  2733. const index = parseInt(reader.line(), 10);
  2734. memo[index] = stack[stack.length - 1];
  2735. size++;
  2736. break;
  2737. }
  2738. case 103: { // GET 'g'
  2739. const index = parseInt(reader.line(), 10);
  2740. stack.push(memo[index]);
  2741. break;
  2742. }
  2743. case 48: // POP '0'
  2744. stack.pop();
  2745. break;
  2746. case 49: // POP_MARK '1'
  2747. stack = marker.pop();
  2748. break;
  2749. case 50: // DUP '2'
  2750. stack.push(stack[stack.length-1]);
  2751. break;
  2752. case 80: // PERSID 'P'
  2753. stack.push(this.persistent_load(reader.line()));
  2754. break;
  2755. case 81: // BINPERSID 'Q'
  2756. stack.push(this.persistent_load(stack.pop()));
  2757. break;
  2758. case 82: { // REDUCE 'R'
  2759. const args = stack.pop();
  2760. const func = stack.pop();
  2761. stack.push(this._reduce(func, args));
  2762. break;
  2763. }
  2764. case 129: { // NEWOBJ
  2765. const args = stack.pop();
  2766. const cls = stack.pop();
  2767. const obj = this._newobj(cls, args);
  2768. stack.push(obj);
  2769. break;
  2770. }
  2771. case 146: { // NEWOBJ_EX '\x92' (Protocol 4)
  2772. const kwargs = stack.pop();
  2773. const args = stack.pop();
  2774. const cls = stack.pop();
  2775. if (Object.entries(kwargs).length > 0) {
  2776. throw new python.Error("Unpickle 'NEWOBJ_EX' not implemented.");
  2777. }
  2778. const obj = this._newobj(cls, args);
  2779. stack.push(obj);
  2780. break;
  2781. }
  2782. case 104: // BINGET 'h'
  2783. stack.push(memo[reader.byte()]);
  2784. break;
  2785. case 105: { // INST 'i'
  2786. const module = reader.line();
  2787. const name = reader.line();
  2788. const args = stack;
  2789. const cls = `${module}.${name}`;
  2790. stack = marker.pop();
  2791. // TODO
  2792. // cls = this.find_class(module, name)
  2793. const obj = this._instantiate(cls, args);
  2794. stack.push(obj);
  2795. break;
  2796. }
  2797. case 106: // LONG_BINGET 'j'
  2798. stack.push(memo[reader.uint32()]);
  2799. break;
  2800. case 113: // BINPUT 'q'
  2801. memo[reader.byte()] = stack[stack.length - 1];
  2802. size++;
  2803. break;
  2804. case 114: // LONG_BINPUT 'r'
  2805. memo[reader.uint32()] = stack[stack.length - 1];
  2806. size++;
  2807. break;
  2808. case 74: // BININT 'J'
  2809. stack.push(reader.int32());
  2810. break;
  2811. case 75: // BININT1 'K'
  2812. stack.push(reader.byte());
  2813. break;
  2814. case 76: // LONG 'L'
  2815. stack.push(parseInt(reader.line(), 10));
  2816. break;
  2817. case 77: // BININT2 'M'
  2818. stack.push(reader.uint16());
  2819. break;
  2820. case 66: // BINBYTES 'B' (Protocol 3)
  2821. stack.push(reader.read(reader.int32()));
  2822. break;
  2823. case 67: // SHORT_BINBYTES 'C' (Protocol 3)
  2824. stack.push(reader.read(reader.byte()));
  2825. break;
  2826. case 142: // BINBYTES8 '\x8e' (Protocol 4)
  2827. stack.push(reader.read(reader.int64()));
  2828. break;
  2829. case 70: // FLOAT 'F'
  2830. stack.push(parseFloat(reader.line()));
  2831. break;
  2832. case 71: // BINFLOAT 'G'
  2833. stack.push(reader.float64());
  2834. break;
  2835. case 73: { // INT 'I'
  2836. const value = reader.line();
  2837. if (value == '01') {
  2838. stack.push(true);
  2839. } else if (value == '00') {
  2840. stack.push(false);
  2841. } else {
  2842. stack.push(parseInt(value, 10));
  2843. }
  2844. break;
  2845. }
  2846. case 93: // EMPTY_LIST ']'
  2847. stack.push(execution.invoke('builtins.list', []));
  2848. break;
  2849. case 41: // EMPTY_TUPLE ')'
  2850. stack.push([]);
  2851. break;
  2852. case 143: // EMPTY_SET '\x8f' (Protocol 4)
  2853. stack.push([]);
  2854. break;
  2855. case 144: { // ADDITEMS '\x90' (Protocol 4)
  2856. const items = stack;
  2857. stack = marker.pop();
  2858. const obj = stack[stack.length - 1];
  2859. for (let i = 0; i < items.length; i++) {
  2860. obj.push(items[i]);
  2861. }
  2862. break;
  2863. }
  2864. case 145: { // FROZENSET '\x91' (Protocol 4)
  2865. const items = stack;
  2866. stack = marker.pop();
  2867. stack.push(items);
  2868. break;
  2869. }
  2870. case 100: { // DICT 'd'
  2871. const items = stack;
  2872. stack = marker.pop();
  2873. const dict = {};
  2874. for (let i = 0; i < items.length; i += 2) {
  2875. dict[items[i]] = items[i + 1];
  2876. }
  2877. stack.push(dict);
  2878. break;
  2879. }
  2880. case 108: { // LIST 'l'
  2881. const items = stack;
  2882. stack = marker.pop();
  2883. stack.push(items);
  2884. break;
  2885. }
  2886. case 116: { // TUPLE 't'
  2887. const items = stack;
  2888. stack = marker.pop();
  2889. stack.push(items);
  2890. break;
  2891. }
  2892. case 133: { // TUPLE1 // '\x85'
  2893. stack.push([ stack.pop() ]);
  2894. break;
  2895. }
  2896. case 134: { // TUPLE2 '\x86'
  2897. const b = stack.pop();
  2898. const a = stack.pop();
  2899. stack.push([ a, b ]);
  2900. break;
  2901. }
  2902. case 135: { // TUPLE3 '\x87'
  2903. const c = stack.pop();
  2904. const b = stack.pop();
  2905. const a = stack.pop();
  2906. stack.push([ a, b, c ]);
  2907. break;
  2908. }
  2909. case 115: { // SETITEM 's'
  2910. const value = stack.pop();
  2911. const key = stack.pop();
  2912. const obj = stack[stack.length - 1];
  2913. if (obj.__setitem__) {
  2914. obj.__setitem__(key, value);
  2915. } else {
  2916. obj[key] = value;
  2917. }
  2918. break;
  2919. }
  2920. case 117: { // SETITEMS 'u'
  2921. const items = stack;
  2922. stack = marker.pop();
  2923. const obj = stack[stack.length - 1];
  2924. for (let i = 0; i < items.length; i += 2) {
  2925. if (obj.__setitem__) {
  2926. obj.__setitem__(items[i], items[i + 1]);
  2927. } else {
  2928. obj[items[i]] = items[i + 1];
  2929. }
  2930. }
  2931. break;
  2932. }
  2933. case 125: // EMPTY_DICT '}'
  2934. stack.push({});
  2935. break;
  2936. case 97: { // APPEND 'a'
  2937. const append = stack.pop();
  2938. stack[stack.length-1].push(append);
  2939. break;
  2940. }
  2941. case 101: { // APPENDS 'e'
  2942. const appends = stack;
  2943. stack = marker.pop();
  2944. const list = stack[stack.length - 1];
  2945. list.push.apply(list, appends);
  2946. break;
  2947. }
  2948. case 83: { // STRING 'S'
  2949. const str = reader.line();
  2950. stack.push(str.substr(1, str.length - 2));
  2951. break;
  2952. }
  2953. case 84: // BINSTRING 'T'
  2954. stack.push(reader.string(reader.uint32()));
  2955. break;
  2956. case 85 : // SHORT_BINSTRING 'U'
  2957. stack.push(reader.string(reader.byte()));
  2958. break;
  2959. case 86: // UNICODE 'V'
  2960. stack.push(reader.line());
  2961. break;
  2962. case 88: // BINUNICODE 'X
  2963. stack.push(reader.string(reader.uint32(), 'utf-8'));
  2964. break;
  2965. case 140: // SHORT_BINUNICODE '\x8c' (Protocol 4)
  2966. stack.push(reader.string(reader.byte(), 'utf-8'));
  2967. break;
  2968. case 98: { // BUILD 'b'
  2969. const state = stack.pop();
  2970. let obj = stack.pop();
  2971. if (obj.__setstate__) {
  2972. if (obj.__setstate__.__call__) {
  2973. obj.__setstate__.__call__([ obj, state ]);
  2974. } else {
  2975. obj.__setstate__(state);
  2976. }
  2977. } else if (ArrayBuffer.isView(state) || Object(state) !== state) {
  2978. obj.__state__ = state;
  2979. } else if (obj instanceof Map) {
  2980. for (const key in state) {
  2981. obj.set(key, state[key]);
  2982. }
  2983. } else {
  2984. Object.assign(obj, state);
  2985. }
  2986. if (obj.__read__) {
  2987. obj = obj.__read__(this);
  2988. }
  2989. stack.push(obj);
  2990. break;
  2991. }
  2992. case 40: // MARK '('
  2993. marker.push(stack);
  2994. stack = [];
  2995. break;
  2996. case 136: // NEWTRUE '\x88'
  2997. stack.push(true);
  2998. break;
  2999. case 137: // NEWFALSE '\x89'
  3000. stack.push(false);
  3001. break;
  3002. case 138: { // LONG1 '\x8a'
  3003. const data = reader.read(reader.byte());
  3004. let number = 0;
  3005. switch (data.length) {
  3006. case 0: number = 0; break;
  3007. /* eslint-disable prefer-destructuring */
  3008. case 1: number = data[0]; break;
  3009. /* eslint-enable prefer-destructuring */
  3010. case 2: number = data[1] << 8 | data[0]; break;
  3011. case 3: number = data[2] << 16 | data[1] << 8 | data[0]; break;
  3012. case 4: number = data[3] << 24 | data[2] << 16 | data[1] << 8 | data[0]; break;
  3013. case 5: number = data[4] * 0x100000000 + ((data[3] << 24 | data[2] << 16 | data[1] << 8 | data[0]) >>> 0); break;
  3014. default: number = Array.prototype.slice.call(data, 0); break;
  3015. }
  3016. stack.push(number);
  3017. break;
  3018. }
  3019. case 139: // LONG4 '\x8b'
  3020. // TODO decode LONG4
  3021. stack.push(reader.read(reader.uint32()));
  3022. break;
  3023. case 148: // MEMOIZE '\x94' (Protocol 4)
  3024. memo[size++] = stack[stack.length - 1];
  3025. break;
  3026. case 149: // FRAME '\x95' (Protocol 4)
  3027. reader.read(8);
  3028. break;
  3029. case 150: { // BYTEARRAY8 '\x96' (Protocol 5)
  3030. stack.push(reader.read(reader.int64()));
  3031. break;
  3032. }
  3033. case 78: // NONE 'N'
  3034. stack.push(null);
  3035. break;
  3036. case 46: // STOP '.'
  3037. return stack.pop();
  3038. case 141: // BINUNICODE8 '\x8d' (Protocol 4)
  3039. case 151: // NEXT_BUFFER '\x97' (Protocol 5)
  3040. case 152: // READONLY_BUFFER '\x98' (Protocol 5)
  3041. default:
  3042. throw new python.Error(`Unknown opcode ${opcode} at position ${(reader.position - 1)}.`);
  3043. }
  3044. }
  3045. throw new python.Error('Unexpected end of file.');
  3046. }
  3047. find_class(module, name) {
  3048. execution.__import__(module);
  3049. return execution.resolve(`${module}.${name}`);
  3050. }
  3051. _instantiate(cls, args) {
  3052. return execution.invoke(cls, args);
  3053. }
  3054. _newobj(cls, args) {
  3055. // cls.__new__(cls, args)
  3056. return execution.invoke(cls, args);
  3057. }
  3058. _reduce(func, args) {
  3059. return execution.invoke(func, args);
  3060. }
  3061. read(size) {
  3062. return this._reader.read(size);
  3063. }
  3064. stream(size) {
  3065. return this._reader.stream(size);
  3066. }
  3067. });
  3068. this.registerType('random.Random', class {});
  3069. this.registerType('re.Pattern', class {
  3070. constructor(pattern, flags) {
  3071. this.pattern = pattern;
  3072. this.flags = flags;
  3073. }
  3074. });
  3075. this.registerType('spacy._ml.PrecomputableAffine', class {
  3076. __setstate__(state) {
  3077. Object.assign(this, new pickle.Unpickler(state).load());
  3078. }
  3079. });
  3080. this.registerType('spacy.syntax._parser_model.ParserModel', class {
  3081. __setstate__(state) {
  3082. Object.assign(this, new pickle.Unpickler(state).load());
  3083. }
  3084. });
  3085. this.registerType('theano.compile.function_module._constructor_Function', class {});
  3086. this.registerType('theano.compile.function_module._constructor_FunctionMaker', class {});
  3087. this.registerType('theano.compile.function_module.Function', class {});
  3088. this.registerType('theano.compile.function_module.Supervisor', class {});
  3089. this.registerType('theano.compile.io.In', class {});
  3090. this.registerType('theano.compile.io.SymbolicOutput', class {});
  3091. this.registerType('theano.compile.mode.Mode', class {});
  3092. this.registerType('theano.compile.ops.OutputGuard', class {});
  3093. this.registerType('theano.compile.ops.Shape', class {});
  3094. this.registerType('theano.compile.ops.Shape_i', class {});
  3095. this.registerType('theano.gof.destroyhandler.DestroyHandler', class {});
  3096. this.registerType('theano.gof.fg.FunctionGraph', class {});
  3097. this.registerType('theano.gof.graph.Apply', class {});
  3098. this.registerType('theano.gof.link.Container', class {});
  3099. this.registerType('theano.gof.opt._metadict', class {});
  3100. this.registerType('theano.gof.opt.ChangeTracker', class {});
  3101. this.registerType('theano.gof.opt.MergeFeature', class {});
  3102. this.registerType('theano.gof.optdb.Query', class {});
  3103. this.registerType('theano.gof.toolbox.PreserveVariableAttributes', class {});
  3104. this.registerType('theano.gof.toolbox.ReplaceValidate', class {});
  3105. this.registerType('theano.gof.utils.scratchpad', class {});
  3106. this.registerType('theano.misc.ordered_set.Link', class {});
  3107. this.registerType('theano.misc.ordered_set.OrderedSet', class {});
  3108. this.registerType('theano.sandbox.cuda.basic_ops.HostFromGpu', class {});
  3109. this.registerType('theano.sandbox.cuda.type.CudaNdarray_unpickler', class {});
  3110. this.registerType('theano.sandbox.cuda.type.CudaNdarrayType', class {});
  3111. this.registerType('theano.sandbox.cuda.var.CudaNdarraySharedVariable', class {});
  3112. this.registerType('theano.scalar.basic.Abs', class {});
  3113. this.registerType('theano.scalar.basic.Add', class {});
  3114. this.registerType('theano.scalar.basic.Cast', class {});
  3115. this.registerType('theano.scalar.basic.Composite', class {});
  3116. this.registerType('theano.scalar.basic.EQ', class {});
  3117. this.registerType('theano.scalar.basic.GE', class {});
  3118. this.registerType('theano.scalar.basic.Identity', class {});
  3119. this.registerType('theano.scalar.basic.IntDiv', class {});
  3120. this.registerType('theano.scalar.basic.Inv', class {});
  3121. this.registerType('theano.scalar.basic.LE', class {});
  3122. this.registerType('theano.scalar.basic.LT', class {});
  3123. this.registerType('theano.scalar.basic.Mul', class {});
  3124. this.registerType('theano.scalar.basic.Neg', class {});
  3125. this.registerType('theano.scalar.basic.Scalar', class {});
  3126. this.registerType('theano.scalar.basic.ScalarConstant', class {});
  3127. this.registerType('theano.scalar.basic.ScalarVariable', class {});
  3128. this.registerType('theano.scalar.basic.Second', class {});
  3129. this.registerType('theano.scalar.basic.Sgn', class {});
  3130. this.registerType('theano.scalar.basic.specific_out', class {});
  3131. this.registerType('theano.scalar.basic.Sub', class {});
  3132. this.registerType('theano.scalar.basic.Switch', class {});
  3133. this.registerType('theano.scalar.basic.Tanh', class {});
  3134. this.registerType('theano.scalar.basic.transfer_type', class {});
  3135. this.registerType('theano.scalar.basic.TrueDiv', class {});
  3136. this.registerType('theano.tensor.basic.Alloc', class {});
  3137. this.registerType('theano.tensor.basic.Dot', class {});
  3138. this.registerType('theano.tensor.basic.MaxAndArgmax', class {});
  3139. this.registerType('theano.tensor.basic.Reshape', class {});
  3140. this.registerType('theano.tensor.basic.ScalarFromTensor', class {});
  3141. this.registerType('theano.tensor.blas.Dot22', class {});
  3142. this.registerType('theano.tensor.blas.Dot22Scalar', class {});
  3143. this.registerType('theano.tensor.blas.Gemm', class {});
  3144. this.registerType('theano.tensor.elemwise.DimShuffle', class {});
  3145. this.registerType('theano.tensor.elemwise.Elemwise', class {});
  3146. this.registerType('theano.tensor.elemwise.Sum', class {});
  3147. this.registerType('theano.tensor.nnet.abstract_conv.AbstractConv2d', class {});
  3148. this.registerType('theano.tensor.nnet.abstract_conv.AbstractConv2d_gradInputs', class {});
  3149. this.registerType('theano.tensor.nnet.abstract_conv.AbstractConv2d_gradWeights', class {});
  3150. this.registerType('theano.tensor.nnet.corr.CorrMM', class {});
  3151. this.registerType('theano.tensor.nnet.corr.CorrMM_gradInputs', class {});
  3152. this.registerType('theano.tensor.nnet.corr.CorrMM_gradWeights', class {});
  3153. this.registerType('theano.tensor.nnet.nnet.CrossentropyCategorical1Hot', class {});
  3154. this.registerType('theano.tensor.nnet.nnet.CrossentropyCategorical1HotGrad', class {});
  3155. this.registerType('theano.tensor.nnet.nnet.CrossentropySoftmax1HotWithBiasDx', class {});
  3156. this.registerType('theano.tensor.nnet.nnet.CrossentropySoftmaxArgmax1HotWithBias', class {});
  3157. this.registerType('theano.tensor.nnet.nnet.Softmax', class {});
  3158. this.registerType('theano.tensor.nnet.nnet.SoftmaxGrad', class {});
  3159. this.registerType('theano.tensor.nnet.nnet.SoftmaxWithBias', class {});
  3160. this.registerType('theano.tensor.opt.MakeVector', class {});
  3161. this.registerType('theano.tensor.opt.ShapeFeature', class {});
  3162. this.registerType('theano.tensor.sharedvar.TensorSharedVariable', class {});
  3163. this.registerType('theano.tensor.signal.pool.MaxPoolGrad', class {});
  3164. this.registerType('theano.tensor.signal.pool.Pool', class {});
  3165. this.registerType('theano.tensor.subtensor.Subtensor', class {});
  3166. this.registerType('theano.tensor.type.TensorType', class {});
  3167. this.registerType('theano.tensor.var.TensorConstant', class {});
  3168. this.registerType('theano.tensor.var.TensorConstantSignature', class {});
  3169. this.registerType('theano.tensor.var.TensorVariable', class {});
  3170. this.registerType('thinc.describe.Biases', class {
  3171. __setstate__(state) {
  3172. Object.assign(this, state);
  3173. }
  3174. });
  3175. this.registerType('thinc.describe.Dimension', class {
  3176. __setstate__(state) {
  3177. Object.assign(this, state);
  3178. }
  3179. });
  3180. this.registerType('thinc.describe.Gradient', class {
  3181. __setstate__(state) {
  3182. Object.assign(this, state);
  3183. }
  3184. });
  3185. this.registerType('thinc.describe.Weights', class {
  3186. __setstate__(state) {
  3187. Object.assign(this, state);
  3188. }
  3189. });
  3190. this.registerType('thinc.describe.Synapses', class {
  3191. __setstate__(state) {
  3192. Object.assign(this, state);
  3193. }
  3194. });
  3195. this.registerType('thinc.neural._classes.affine.Affine', class {
  3196. __setstate__(state) {
  3197. Object.assign(this, new pickle.Unpickler(state).load());
  3198. }
  3199. });
  3200. this.registerType('thinc.neural._classes.convolution.ExtractWindow', class {
  3201. __setstate__(state) {
  3202. Object.assign(this, new pickle.Unpickler(state).load());
  3203. }
  3204. });
  3205. this.registerType('thinc.neural._classes.feature_extracter.FeatureExtracter', class {
  3206. __setstate__(state) {
  3207. Object.assign(this, new pickle.Unpickler(state).load());
  3208. }
  3209. });
  3210. this.registerType('thinc.neural._classes.feed_forward.FeedForward', class {
  3211. __setstate__(state) {
  3212. Object.assign(this, new pickle.Unpickler(state).load());
  3213. }
  3214. });
  3215. this.registerType('thinc.neural._classes.function_layer.FunctionLayer', class {
  3216. __setstate__(state) {
  3217. Object.assign(this, new pickle.Unpickler(state).load());
  3218. }
  3219. });
  3220. this.registerType('thinc.neural._classes.hash_embed.HashEmbed', class {
  3221. __setstate__(state) {
  3222. Object.assign(this, new pickle.Unpickler(state).load());
  3223. }
  3224. });
  3225. this.registerType('thinc.neural._classes.layernorm.LayerNorm', class {
  3226. __setstate__(state) {
  3227. Object.assign(this, new pickle.Unpickler(state).load());
  3228. }
  3229. });
  3230. this.registerType('thinc.neural._classes.maxout.Maxout', class {
  3231. __setstate__(state) {
  3232. Object.assign(this, new pickle.Unpickler(state).load());
  3233. }
  3234. });
  3235. this.registerType('thinc.neural._classes.resnet.Residual', class {
  3236. __setstate__(state) {
  3237. Object.assign(this, new pickle.Unpickler(state).load());
  3238. }
  3239. });
  3240. this.registerType('thinc.neural._classes.softmax.Softmax', class {
  3241. __setstate__(state) {
  3242. Object.assign(this, new pickle.Unpickler(state).load());
  3243. }
  3244. });
  3245. this.registerType('thinc.neural.mem.Memory', class {
  3246. });
  3247. this.registerType('thinc.neural.ops.NumpyOps', class {
  3248. });
  3249. this.registerType('__main__.BYOLState', class {
  3250. constructor(dict) {
  3251. Object.assign(this, dict);
  3252. }
  3253. });
  3254. this.registerType('types.GenericAlias', class {});
  3255. this.registerType('types.SimpleNamespace', class {});
  3256. this.register('types').ObjectType = builtins.object;
  3257. this.register('types').ModuleType = builtins.module;
  3258. this.register('types').MethodType = builtins.method;
  3259. this.register('types').FunctionType = builtins.function;
  3260. this.register('types').TypeType = builtins.type;
  3261. this.register('types').CodeType = builtins.code;
  3262. this.registerType('xgboost.compat.XGBoostLabelEncoder', class {});
  3263. this.registerType('xgboost.core.Booster', class {});
  3264. this.registerType('xgboost.sklearn.XGBClassifier', class {});
  3265. this.registerType('xgboost.sklearn.XGBRegressor', class {});
  3266. this.registerFunction('_codecs.encode', function(obj, encoding) {
  3267. return execution.invoke('builtins.bytearray', [ obj, encoding ]);
  3268. });
  3269. this.registerType('builtins.bytearray', class extends Uint8Array {
  3270. constructor(source, encoding /*, errors */) {
  3271. source = builtins.bytes.__encode__(source, encoding);
  3272. super(Number.isInteger(source) ? source : source.length);
  3273. if (Array.isArray(source)) {
  3274. for (let i = 0; i < source.length; i++) {
  3275. this[i] = source;
  3276. }
  3277. } else if (source instanceof Uint8Array) {
  3278. this.set(source, 0);
  3279. } else if (typeof source === 'string') {
  3280. for (let i = 0; i < source.length; i++) {
  3281. this[i] = source.charCodeAt(i);
  3282. }
  3283. }
  3284. }
  3285. static __encode__(source, encoding) {
  3286. if (source === undefined) {
  3287. return 0;
  3288. }
  3289. if (Number.isInteger(source)) {
  3290. return source;
  3291. }
  3292. if (Array.isArray(source) || source instanceof Uint8Array) {
  3293. return source;
  3294. }
  3295. if (typeof source === 'string') {
  3296. switch (encoding) {
  3297. case 'latin1':
  3298. case 'latin-1':
  3299. return source;
  3300. case 'utf8':
  3301. case 'utf-8':
  3302. return new TextEncoder('utf-8').encode(source);
  3303. case undefined:
  3304. throw new python.Error('Unsupported string argument without an encoding.');
  3305. default:
  3306. throw new python.Error(`Unsupported encoding '${encoding}'.`);
  3307. }
  3308. }
  3309. throw new python.Error('Unsupported source.');
  3310. }
  3311. });
  3312. this.registerType('builtins.bytes', class extends Uint8Array {
  3313. constructor(source, encoding /*, errors */) {
  3314. source = builtins.bytes.__encode__(source, encoding);
  3315. super(Number.isInteger(source) ? source : source.length);
  3316. if (Array.isArray(source)) {
  3317. for (let i = 0; i < source.length; i++) {
  3318. this[i] = source;
  3319. }
  3320. } else if (source instanceof Uint8Array) {
  3321. this.set(source, 0);
  3322. } else if (typeof source === 'string') {
  3323. for (let i = 0; i < source.length; i++) {
  3324. this[i] = source.charCodeAt(i);
  3325. }
  3326. }
  3327. }
  3328. static __encode__(source, encoding) {
  3329. if (source === undefined) {
  3330. return 0;
  3331. }
  3332. if (Number.isInteger(source)) {
  3333. return source;
  3334. }
  3335. if (Array.isArray(source) || source instanceof Uint8Array) {
  3336. return source;
  3337. }
  3338. if (typeof source === 'string') {
  3339. switch (encoding) {
  3340. case 'latin1':
  3341. case 'latin-1':
  3342. return source;
  3343. case 'utf8':
  3344. case 'utf-8':
  3345. return new TextEncoder('utf-8').encode(source);
  3346. case undefined:
  3347. throw new python.Error('Unsupported string argument without an encoding.');
  3348. default:
  3349. throw new python.Error(`Unsupported encoding '${encoding}'.`);
  3350. }
  3351. }
  3352. throw new python.Error('Unsupported source.');
  3353. }
  3354. });
  3355. this.registerType('builtins.frozenset', class extends Set {
  3356. constructor(iterable) {
  3357. super();
  3358. if (iterable) {
  3359. for (const item of iterable) {
  3360. this.add(item);
  3361. }
  3362. }
  3363. }
  3364. });
  3365. this.registerFunction('builtins.issubclass', function(obj, type) {
  3366. const name = `${type.__module__}.${type.__name__}`;
  3367. if (obj.__module__ && obj.__name__) {
  3368. if (name === `${obj.__module__}.${obj.__name__}`) {
  3369. return true;
  3370. }
  3371. }
  3372. if (obj.__bases__) {
  3373. for (const base of obj.__bases__) {
  3374. if (builtins.issubclass(base, type)) {
  3375. return true;
  3376. }
  3377. }
  3378. }
  3379. return false;
  3380. });
  3381. this.registerFunction('builtins.isinstance', function(obj, type) {
  3382. return obj.__class__ ? builtins.issubclass(obj.__class__, type) : false;
  3383. });
  3384. this.registerFunction('builtins.hasattr', function(obj, name) {
  3385. return Object.prototype.hasOwnProperty.call(obj, name);
  3386. });
  3387. this.registerFunction('builtins.getattr', function(obj, name, defaultValue) {
  3388. if (Object.prototype.hasOwnProperty.call(obj, name)) {
  3389. return obj[name];
  3390. }
  3391. return defaultValue;
  3392. });
  3393. this.registerFunction('builtins.setattr', function(obj, name, value) {
  3394. obj[name] = value;
  3395. });
  3396. this.registerType('builtins.set', class extends Set {
  3397. constructor(iterable) {
  3398. super(iterable);
  3399. }
  3400. });
  3401. this.registerType('builtins.slice', class {
  3402. constructor(start, stop, step) {
  3403. this.start = start;
  3404. this.stop = stop;
  3405. this.step = step;
  3406. }
  3407. });
  3408. this.registerFunction('builtins.hash', function(/* obj */) {
  3409. throw new python.Error("'builtins.hash' not implemented.");
  3410. });
  3411. this.registerFunction('cloudpickle.cloudpickle._builtin_type', function(name) {
  3412. return name;
  3413. });
  3414. this.registerType('collections.Counter', class {});
  3415. this.registerFunction('collections.defaultdict', function(/* default_factory */) {
  3416. return {};
  3417. });
  3418. this.registerFunction('copy_reg._reconstructor', function(cls, base, state) {
  3419. // copyreg._reconstructor in Python 3
  3420. if (base === '__builtin__.object' || base === self._builtins.object) {
  3421. return self.invoke(cls, []);
  3422. } else if (base === '__builtin__.tuple' || base === self._builtins.tuple) {
  3423. const obj = self.invoke(cls, []);
  3424. for (let i = 0; i < state.length; i++) {
  3425. obj[i] = state[i];
  3426. }
  3427. return obj;
  3428. }
  3429. throw new python.Error(`Unsupported copy_reg._reconstructor base type '${base}'.`);
  3430. });
  3431. this.registerFunction('copy.deepcopy', function(/* x */) {
  3432. throw new python.Error('Unsupported copy.deepcopy().');
  3433. });
  3434. this.registerFunction('dill._dill._create_array', function(f, args, state, npdict) {
  3435. const array = f(...args);
  3436. if (array.__setstate__) {
  3437. array.__setstate__(state);
  3438. }
  3439. if (npdict) {
  3440. throw new python.Error("'dill._dill._create_array::npdict' not implemented.");
  3441. }
  3442. return array;
  3443. });
  3444. this.registerFunction('dill._dill._create_cell', function(/* args */) {
  3445. return function() {
  3446. // TODO
  3447. };
  3448. });
  3449. this.registerFunction('dill._dill._create_code', function(args) {
  3450. return self.invoke('types.CodeType', [ args ]);
  3451. });
  3452. this.registerFunction('dill._dill._create_function', function(/* fcode, fglobals, fname, fdefaults, fclosure, fdict, fkwdefaults */) {
  3453. return function() {
  3454. // TODO
  3455. };
  3456. });
  3457. this.registerFunction('dill._dill._create_namedtuple', function(name, fieldnames, modulename /*, defaults */) {
  3458. const obj = execution.invoke('dill._dill._import_module', [ `${modulename}.${name}` ]);
  3459. if (obj) {
  3460. return obj;
  3461. }
  3462. return undefined;
  3463. });
  3464. this.registerFunction('dill._dill._create_type', function(/* typeobj */) {
  3465. // return execution.invoke(typeobj, Array.from(arguments).slice(1));
  3466. throw new python.Error("'dill._dill._create_type' not implemented.");
  3467. });
  3468. this.registerFunction('dill._dill._eval_repr', function(/* repr_str */) {
  3469. throw new python.Error("'dill._dill._eval_repr' not implemented.");
  3470. });
  3471. this.registerFunction('dill._dill._get_attr', function(self, name) {
  3472. if (Object.prototype.hasOwnProperty.call(self, name)) {
  3473. return self[name];
  3474. }
  3475. return undefined;
  3476. });
  3477. this.registerFunction('dill._dill._import_module', function(import_name, safe) {
  3478. try {
  3479. if (import_name.startsWith('__runtime__.')) {
  3480. return execution.module(import_name);
  3481. } else if (import_name.indexOf('.') === -1) {
  3482. return execution.__import__(import_name);
  3483. }
  3484. return execution.resolve(import_name);
  3485. } catch (err) {
  3486. if (safe) {
  3487. return null;
  3488. }
  3489. throw err;
  3490. }
  3491. });
  3492. this.registerFunction('dill._dill._load_type', function(name) {
  3493. const _dill = self.register('dill._dill');
  3494. if (!_dill._reverse_typemap) {
  3495. _dill._reverse_typemap = new Map();
  3496. for (const name of [ '__builtin__', 'types' ]) {
  3497. const module = self.register(name);
  3498. for (const [name, obj] of Object.entries(module)) {
  3499. if (obj.__module__ === 'builtins' &&
  3500. obj.__class__ === builtins.type) {
  3501. _dill._reverse_typemap.set(name, obj);
  3502. }
  3503. }
  3504. }
  3505. _dill._reverse_typemap.set('PartialType', functools.partial);
  3506. _dill._reverse_typemap.set('CellType', builtins.cell);
  3507. }
  3508. if (!_dill._reverse_typemap.has(name)) {
  3509. throw new python.Error(`Unknown type name '${name}' in 'dill._dill._load_type'.`);
  3510. }
  3511. return _dill._reverse_typemap.get(name);
  3512. });
  3513. this.registerFunction('keras.saving.pickle_utils.deserialize_model_from_bytecode', function(/* serialized_model */) {
  3514. return null; // throw new python.Error("'keras.saving.pickle_utils.deserialize_model_from_bytecode' not implemented.");
  3515. });
  3516. this.registerFunction('keras.src.saving.pickle_utils.deserialize_model_from_bytecode', keras.saving.pickle_utils.deserialize_model_from_bytecode);
  3517. this.registerFunction('lasagne.nonlinearities.rectify', function() {
  3518. throw new python.Error("'lasagne.nonlinearities.rectify' not implemented.");
  3519. });
  3520. this.registerFunction('lasagne.nonlinearities.softmax', function() {
  3521. throw new python.Error("'lasagne.nonlinearities.softmax' not implemented.");
  3522. });
  3523. this.registerFunction('lasagne.objectives.categorical_crossentropy', function() {
  3524. throw new python.Error("'lasagne.objectives.categorical_crossentropy' not implemented.");
  3525. });
  3526. this.registerFunction('lasagne.updates.nesterov_momentum', function() {
  3527. throw new python.Error("'lasagne.updates.nesterov_momentum' not implemented.");
  3528. });
  3529. this.registerFunction('msgpack.unpackb', function(packed, ext_hook) {
  3530. const BinaryReader = class {
  3531. constructor(buffer, ext_hook) {
  3532. // https://github.com/msgpack/msgpack-javascript/blob/master/src/Decoder.ts
  3533. // https://github.com/msgpack/msgpack-python/blob/main/msgpack/_unpacker.pyx
  3534. this._buffer = buffer;
  3535. this._position = 0;
  3536. this._view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
  3537. this._ext_hook = ext_hook;
  3538. }
  3539. value() {
  3540. const c = this._view.getUint8(this.skip(1));
  3541. if (c >= 0xe0) {
  3542. return c - 0x100;
  3543. }
  3544. if (c < 0xC0) {
  3545. if (c < 0x80) {
  3546. return c;
  3547. }
  3548. if (c < 0x90) {
  3549. return this.map(c - 0x80);
  3550. }
  3551. if (c < 0xa0) {
  3552. return this.array(c - 0x90);
  3553. }
  3554. return this.string(c - 0xa0);
  3555. }
  3556. switch (c) {
  3557. case 0xC0: return null;
  3558. case 0xC2: return false;
  3559. case 0xC3: return true;
  3560. case 0xC4: return this.read(this._view.getUint8(this.skip(1)));
  3561. case 0xC5: return this.read(this._view.getUint16(this.skip(2)));
  3562. case 0xC6: return this.read(this._view.getUint32(this.skip(4)));
  3563. case 0xC7: return this.extension(this._view.getUint8(this.skip(1)));
  3564. case 0xC8: return this.extension(this._view.getUint16(this.skip(2)));
  3565. case 0xC9: return this.extension(this._view.getUint32(this.skip(4)));
  3566. case 0xCA: return this._view.getFloat32(this.skip(4));
  3567. case 0xCB: return this._view.getFloat64(this.skip(8));
  3568. case 0xCC: return this._view.getUint8(this.skip(1));
  3569. case 0xCD: return this._view.getUint16(this.skip(2));
  3570. case 0xCE: return this._view.getUint32(this.skip(4));
  3571. case 0xCF: return this._view.getUint64(this.skip(8));
  3572. case 0xD0: return this._view.getInt8(this.skip(1));
  3573. case 0xD1: return this._view.getInt16(this.skip(2));
  3574. case 0xD2: return this._view.getInt32(this.skip(4));
  3575. case 0xD3: return this._view.getInt64(this.skip(8));
  3576. case 0xD4: return this.extension(1);
  3577. case 0xD5: return this.extension(2);
  3578. case 0xD6: return this.extension(4);
  3579. case 0xD7: return this.extension(8);
  3580. case 0xD8: return this.extension(16);
  3581. case 0xD9: return this.string(this._view.getUint8(this.skip(1)));
  3582. case 0xDA: return this.string(this._view.getUint16(this.skip(2)));
  3583. case 0xDB: return this.string(this._view.getUint32(this.skip(4)));
  3584. case 0xDC: return this.array(this._view.getUint16(this.skip(2)));
  3585. case 0xDD: return this.array(this._view.getUint32(this.skip(4)));
  3586. case 0xDE: return this.map(this._view.getUint16(this.skip(2)));
  3587. case 0xDF: return this.map(this._view.getUint32(this.skip(4)));
  3588. default: throw new python.Error(`Invalid code '${c}'.`);
  3589. }
  3590. }
  3591. map(size) {
  3592. const map = {};
  3593. for (let i = 0; i < size; i++) {
  3594. const key = this.value();
  3595. const value = this.value();
  3596. map[key] = value;
  3597. }
  3598. return map;
  3599. }
  3600. array(size) {
  3601. const array = new Array(size);
  3602. for (let i = 0; i < size; i++) {
  3603. array[i] = this.value();
  3604. }
  3605. return array;
  3606. }
  3607. extension(size) {
  3608. const code = this._view.getUint8(this.skip(1));
  3609. const data = this.read(size);
  3610. return this._ext_hook(code, data);
  3611. }
  3612. skip(offset) {
  3613. const position = this._position;
  3614. this._position += offset;
  3615. if (this._position > this._buffer.length) {
  3616. throw new python.Error(`Expected ${this._position - this._buffer.length} more bytes. The file might be corrupted. Unexpected end of file.`);
  3617. }
  3618. return position;
  3619. }
  3620. read(size) {
  3621. const data = this._buffer.subarray(this._position, this._position + size);
  3622. this._position += size;
  3623. return data;
  3624. }
  3625. string(size) {
  3626. const buffer = this.read(size);
  3627. this._decoder = this._decoder || new TextDecoder('utf8');
  3628. return this._decoder.decode(buffer);
  3629. }
  3630. };
  3631. return new BinaryReader(packed, ext_hook).value();
  3632. });
  3633. this.registerFunction('nolearn.lasagne.base.objective', function() {
  3634. throw new python.Error("'nolearn.lasagne.base.objective' not implemented.");
  3635. });
  3636. this.registerFunction('numpy._reconstruct', function(subtype, shape, dtype) {
  3637. return self.invoke(subtype, [ shape, dtype ]);
  3638. });
  3639. this.registerFunction('numpy.core._DType_reconstruct', function(/* scalar_type */) {
  3640. throw new python.Error("'numpy.core._DType_reconstruct' not implemented.");
  3641. });
  3642. this.registerFunction('numpy.core._multiarray_umath._reconstruct', function(subtype, shape, dtype) {
  3643. return self.invoke(subtype, [ shape, dtype ]);
  3644. });
  3645. this.registerFunction('numpy.core.multiarray._reconstruct', function(subtype, shape, dtype) {
  3646. return self.invoke(subtype, [ shape, dtype ]);
  3647. });
  3648. this.registerFunction('numpy.core.multiarray.scalar', function(dtype, rawData) {
  3649. let data = rawData;
  3650. if (typeof rawData === 'string' || rawData instanceof String) {
  3651. data = new Uint8Array(rawData.length);
  3652. for (let i = 0; i < rawData.length; i++) {
  3653. data[i] = rawData.charCodeAt(i);
  3654. }
  3655. }
  3656. switch (dtype.kind) {
  3657. case 'b': {
  3658. const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
  3659. switch (dtype.itemsize) {
  3660. case 1: return view.getInt8(0) ? true : false;
  3661. default: throw new python.Error(`Unsupported scalar dtype boolean itemsize '${dtype.itemsize}'.`);
  3662. }
  3663. }
  3664. case 'f': {
  3665. const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
  3666. switch (dtype.itemsize) {
  3667. case 2: return view.getFloat16(0, dtype.byteorder === '<');
  3668. case 4: return view.getFloat32(0, dtype.byteorder === '<');
  3669. case 8: return view.getFloat64(0, dtype.byteorder === '<');
  3670. default: throw new python.Error(`Unsupported scalar dtype float itemsize '${dtype.itemsize}'.`);
  3671. }
  3672. }
  3673. case 'i': {
  3674. const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
  3675. switch (dtype.itemsize) {
  3676. case 1: return view.getInt8(0);
  3677. case 2: return view.getInt16(0, dtype.byteorder === '<');
  3678. case 4: return view.getInt32(0, dtype.byteorder === '<');
  3679. case 8: return view.getInt64(0, dtype.byteorder === '<');
  3680. default: throw new python.Error(`Unsupported scalar dtype int itemsize '${dtype.itemsize}'.`);
  3681. }
  3682. }
  3683. case 'u': {
  3684. const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
  3685. switch (dtype.itemsize) {
  3686. case 1: return view.getUint8(0);
  3687. case 2: return view.getUint16(0, dtype.byteorder === '<');
  3688. case 4: return view.getUint32(0, dtype.byteorder === '<');
  3689. case 8: return view.getUint64(0, dtype.byteorder === '<');
  3690. default: throw new python.Error(`Unsupported scalar dtype uint itemsize '${dtype.itemsize}'.`);
  3691. }
  3692. }
  3693. case 'U': {
  3694. const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
  3695. const list = [];
  3696. for (let i = 0; i < dtype.itemsize; i += 4) {
  3697. list.push(String.fromCodePoint(view.getUint32(i, true)));
  3698. }
  3699. return list.join('');
  3700. }
  3701. default: {
  3702. throw new python.Error(`Unsupported scalar dtype kind '${dtype.kind}'.`);
  3703. }
  3704. }
  3705. });
  3706. this.registerFunction('numpy.core._multiarray_umath.scalar', function(dtype, rawData) {
  3707. let data = rawData;
  3708. if (typeof rawData === 'string') {
  3709. data = new Uint8Array(rawData.length);
  3710. for (let i = 0; i < rawData.length; i++) {
  3711. data[i] = rawData.charCodeAt(i);
  3712. }
  3713. }
  3714. const dataView = new DataView(data.buffer, data.byteOffset, data.byteLength);
  3715. switch (dtype.__name__) {
  3716. case 'uint8':
  3717. return dataView.getUint8(0);
  3718. case 'float32':
  3719. return dataView.getFloat32(0, true);
  3720. case 'float64':
  3721. return dataView.getFloat64(0, true);
  3722. case 'int8':
  3723. return dataView.getInt8(0, true);
  3724. case 'int16':
  3725. return dataView.getInt16(0, true);
  3726. case 'int32':
  3727. return dataView.getInt32(0, true);
  3728. case 'int64':
  3729. return dataView.getInt64(0, true);
  3730. default:
  3731. throw new python.Error(`Unsupported scalar type '${dtype.__name__}'.`);
  3732. }
  3733. });
  3734. this.registerFunction('numpy.load', function(file) {
  3735. // https://github.com/numpy/numpy/blob/main/numpy/lib/format.py
  3736. const signature = [ 0x93, 0x4E, 0x55, 0x4D, 0x50, 0x59 ];
  3737. if (!file.read(6).every((v, i) => v == signature[i])) {
  3738. throw new python.Error('Invalid signature.');
  3739. }
  3740. const version = file.read(2);
  3741. const [major, minor] = version;
  3742. if (major > 3) {
  3743. throw new python.Error(`Invalid version '${[ major, minor ].join('.')}'.`);
  3744. }
  3745. const buffer = new Uint8Array([ 0, 0, 0, 0 ]);
  3746. buffer.set(file.read(major >= 2 ? 4 : 2), 0);
  3747. const header_length = buffer[3] << 24 | buffer[2] << 16 | buffer[1] << 8 | buffer[0];
  3748. let header = file.read(header_length);
  3749. const decoder = new TextDecoder(major >= 3 ? 'utf-8' : 'ascii');
  3750. header = decoder.decode(header);
  3751. header = JSON.parse(header.replace(/\(/,'[').replace(/\)/,']').replace('[,','[1,]').replace(',]',']').replace(/'/g, '"').replace(/:\s*False\s*,/,':false,').replace(/:\s*True\s*,/,':true,').replace(/,\s*\}/, ' }'));
  3752. if (!header.descr || header.descr.length < 2) {
  3753. throw new python.Error("Missing property 'descr'.");
  3754. }
  3755. if (!header.shape) {
  3756. throw new python.Error("Missing property 'shape'.");
  3757. }
  3758. const shape = header.shape;
  3759. const dtype = self.invoke('numpy.dtype', [ header.descr.substring(1) ]);
  3760. dtype.byteorder = header.descr.substring(0, 1);
  3761. let data = null;
  3762. switch (dtype.byteorder) {
  3763. case '|': {
  3764. data = file.read();
  3765. if (dtype.kind === 'O') {
  3766. const unpickler = execution.invoke('pickle.Unpickler', [ data ]);
  3767. return unpickler.load();
  3768. }
  3769. break;
  3770. }
  3771. case '>':
  3772. case '<': {
  3773. if (header.descr.length !== 3) {
  3774. throw new python.Error(`Unsupported data type '${header.descr}'.`);
  3775. }
  3776. const count = shape.length === 0 ? 1 : shape.reduce((a, b) => a * b, 1);
  3777. data = file.read(dtype.itemsize * count);
  3778. break;
  3779. }
  3780. default: {
  3781. throw new python.Error(`Unsupported data type '${header.descr}'.`);
  3782. }
  3783. }
  3784. if (header.fortran_order) {
  3785. data = null;
  3786. }
  3787. return self.invoke('numpy.ndarray', [ shape, dtype, data ]);
  3788. });
  3789. this.registerFunction('numpy.save', function(file, arr) {
  3790. const descr = arr.dtype.str;
  3791. if (descr[0] !== '<' && descr[0] !== '>') {
  3792. throw new python.Error(`Unsupported byte order '${descr}'.`);
  3793. }
  3794. if (descr.length !== 3 || (descr[1] !== 'f' && descr[1] !== 'i' && descr[1] !== 'u' && descr[1] !== 'c' && descr.substring(1) !== 'b1')) {
  3795. throw new python.Error(`Unsupported data type '${descr}'.`);
  3796. }
  3797. let shape = '';
  3798. switch (arr.shape.length) {
  3799. case 0: shape = '()'; break;
  3800. case 1: shape = `(${arr.shape[0]},)`; break;
  3801. default: shape = `(${arr.shape.map((dimension) => dimension.toString()).join(', ')})`; break;
  3802. }
  3803. const properties = [
  3804. `'descr': '${descr}'`,
  3805. "'fortran_order': False",
  3806. `'shape': ${shape}`
  3807. ];
  3808. let header = `{ ${properties.join(', ')} }`;
  3809. header += `${' '.repeat(64 - ((header.length + 2 + 8 + 1) & 0x3f))}\n`;
  3810. const encoder = new TextEncoder('ascii');
  3811. file.write([ 0x93, 0x4E, 0x55, 0x4D, 0x50, 0x59, 0x01, 0x00 ]); // '\\x93NUMPY' + version
  3812. file.write([ header.length & 0xff, (header.length >> 8) & 0xff ]);
  3813. file.write(encoder.encode(header));
  3814. file.write(arr.tobytes());
  3815. });
  3816. this.registerFunction('numpy.asarray', function(a, dtype) {
  3817. const encode = (context, data, dim) => {
  3818. const size = context.shape[dim];
  3819. const littleendian = context.littleendian;
  3820. if (dim == context.shape.length - 1) {
  3821. for (let i = 0; i < size; i++) {
  3822. switch (context.dtype) {
  3823. case 'f2':
  3824. context.view.setFloat16(context.position, data[i], littleendian);
  3825. break;
  3826. case 'f4':
  3827. context.view.setFloat32(context.position, data[i], littleendian);
  3828. break;
  3829. case 'f8':
  3830. context.view.setFloat64(context.position, data[i], littleendian);
  3831. break;
  3832. case 'i1':
  3833. context.view.setInt8(context.position, data[i], littleendian);
  3834. break;
  3835. case 'i2':
  3836. context.view.setInt16(context.position, data[i], littleendian);
  3837. break;
  3838. case 'i4':
  3839. context.view.setInt32(context.position, data[i], littleendian);
  3840. break;
  3841. case 'i8':
  3842. context.view.setInt64(context.position, data[i], littleendian);
  3843. break;
  3844. case 'u1':
  3845. context.view.setUint8(context.position, data[i], littleendian);
  3846. break;
  3847. case 'u2':
  3848. context.view.setUint16(context.position, data[i], littleendian);
  3849. break;
  3850. case 'u4':
  3851. context.view.setUint32(context.position, data[i], littleendian);
  3852. break;
  3853. case 'u8':
  3854. context.view.setUint64(context.position, data[i], littleendian);
  3855. break;
  3856. case 'c8':
  3857. context.view.setComplex64(context.position, data[i], littleendian);
  3858. break;
  3859. case 'c16':
  3860. context.view.setComplex128(context.position, data[i], littleendian);
  3861. break;
  3862. case 'b1':
  3863. context.view.setInt8(context.position, data[i] ? 1 : 0);
  3864. break;
  3865. default:
  3866. throw new python.Error(`Unsupported tensor data type '${context.dtype}'.`);
  3867. }
  3868. context.position += context.itemsize;
  3869. }
  3870. } else {
  3871. for (let j = 0; j < size; j++) {
  3872. encode(context, data[j], dim + 1);
  3873. }
  3874. }
  3875. };
  3876. const array_size = (value) => {
  3877. if (value.every((item) => Array.isArray(item))) {
  3878. const dims = value.map((item) => array_size(item));
  3879. const [dim] = dims;
  3880. for (let i = 1; i < dims.length; i++) {
  3881. if (dim.length === dims[i].length) {
  3882. if (!dims[i].every((value, i) => value === dim[i])) {
  3883. throw new python.Error('Invalid array shape.');
  3884. }
  3885. }
  3886. }
  3887. return [ value.length ].concat(dim);
  3888. }
  3889. return [ value.length ];
  3890. };
  3891. const shape = Array.isArray(a) ? array_size(a) : [];
  3892. const size = dtype.itemsize * shape.reduce((a, b) => a * b, 1);
  3893. const context = {
  3894. position: 0,
  3895. itemsize: dtype.itemsize,
  3896. dtype: dtype.str.substring(1),
  3897. littleendian: dtype.str[0],
  3898. shape: shape,
  3899. data: new Uint8Array(size)
  3900. };
  3901. context.view = new DataView(context.data.buffer, context.data.byteOffset, size);
  3902. encode(context, a, 0);
  3903. return self.invoke('numpy.ndarray', [ shape, dtype, context.data ]);
  3904. });
  3905. this.registerFunction('numpy.mean', function() {
  3906. throw new python.Error("'numpy.mean' not implemented.");
  3907. });
  3908. this.registerFunction('numpy.ma.core._mareconstruct', function(subtype, baseclass, baseshape, basetype) {
  3909. const data = self.invoke(baseclass, [ baseshape, basetype ]);
  3910. // = ndarray.__new__(ndarray, baseshape, make_mask_descr(basetype))
  3911. const mask = self.invoke('numpy.ndarray', [ baseshape, '' ]);
  3912. return self.invoke(subtype, [ data, mask, basetype ]);
  3913. });
  3914. this.registerFunction('numpy.random.__RandomState_ctor', function() {
  3915. return {};
  3916. });
  3917. this.registerFunction('numpy.random._pickle.__randomstate_ctor', function() {
  3918. return {};
  3919. });
  3920. this.registerFunction('numpy.random._pickle.__bit_generator_ctor', function() {
  3921. throw new python.Error("'numpy.random._pickle.__bit_generator_ctor' not implemented.");
  3922. });
  3923. this.registerFunction('numpy.random._pickle.__generator_ctor', function() {
  3924. throw new python.Error("'numpy.random._pickle.__generator_ctor' not implemented.");
  3925. });
  3926. this.registerFunction('numpy.reshape', function() {
  3927. throw new python.Error("'numpy.reshape' not implemented.");
  3928. });
  3929. this.registerFunction('numpy.core.numeric._frombuffer', function(/* buf, dtype, shape, order */) {
  3930. return {};
  3931. });
  3932. this.registerFunction('sklearn.feature_selection._univariate_selection.f_classif', function() {
  3933. throw new python.Error("'sklearn.feature_selection._univariate_selection.f_classif' not implemented.");
  3934. });
  3935. this.registerFunction('sklearn.metrics.scorer._passthrough_scorer', function() {
  3936. throw new python.Error("'sklearn.metrics.scorer._passthrough_scorer' not implemented.");
  3937. });
  3938. this.registerFunction('sklearn.metrics._classification.accuracy_score', function() {
  3939. throw new python.Error("'sklearn.metrics._classification.accuracy_score' not implemented.");
  3940. });
  3941. this.registerFunction('sklearn.metrics._classification.f1_score', function() {
  3942. throw new python.Error("'sklearn.metrics._classification.f1_score' not implemented.");
  3943. });
  3944. this.registerFunction('sklearn.metrics._classification.precision_score', function() {
  3945. throw new python.Error("'sklearn.metrics._classification.precision_score' not implemented.");
  3946. });
  3947. this.registerFunction('sklearn.metrics._classification.recall_score', function() {
  3948. throw new python.Error("'sklearn.metrics._classification.recall_score' not implemented.");
  3949. });
  3950. this.registerFunction('sklearn.metrics._dist_metrics.newObj', function() {
  3951. throw new python.Error("'sklearn.metrics._dist_metrics.newObj' not implemented.");
  3952. });
  3953. this.registerFunction('sklearn.metrics._regression.mean_absolute_error', function() {
  3954. throw new python.Error("'sklearn.metrics._regression.mean_absolute_error' not implemented.");
  3955. });
  3956. this.registerFunction('sklearn.metrics._regression.mean_squared_error', function() {
  3957. throw new python.Error("'sklearn.metrics._regression.mean_squared_error' not implemented.");
  3958. });
  3959. this.registerFunction('re._compile', function(pattern, flags) {
  3960. return self.invoke('re.Pattern', [ pattern, flags ]);
  3961. });
  3962. this.registerFunction('srsly.cloudpickle.cloudpickle._builtin_type', function(name) {
  3963. return function() {
  3964. return self.invoke(`types.${name}`, arguments);
  3965. };
  3966. });
  3967. this.registerFunction('theano.scalar.basic.same_out', function() {
  3968. throw new python.Error("'theano.scalar.basic.same_out' not implemented.");
  3969. });
  3970. this.registerFunction('theano.scalar.basic.same_out_nocomplex', function() {
  3971. throw new python.Error("'theano.scalar.basic.same_out_nocomplex' not implemented.");
  3972. });
  3973. this.registerFunction('theano.scalar.basic.upcast_out', function() {
  3974. throw new python.Error("'theano.scalar.basic.upcast_out' not implemented.");
  3975. });
  3976. this.registerFunction('theano.scalar.basic.upgrade_to_float', function() {
  3977. throw new python.Error("'theano.scalar.basic.upgrade_to_float' not implemented.");
  3978. });
  3979. this.registerFunction('theano.tensor.nnet.conv2d', function() {
  3980. throw new python.Error("'theano.tensor.nnet.conv2d' not implemented.");
  3981. });
  3982. this.registerFunction('theano.tensor.type.values_eq_approx_remove_inf_nan', function() {
  3983. throw new python.Error("'theano.tensor.type.values_eq_approx_remove_inf_nan' not implemented.");
  3984. });
  3985. this.registerFunction('theano.tensor.type.values_eq_approx_remove_nan', function() {
  3986. throw new python.Error("'theano.tensor.type.values_eq_approx_remove_nan' not implemented.");
  3987. });
  3988. this.registerType('torch.nn.modules.module.Module', class {
  3989. constructor() {
  3990. this._modules = execution.invoke('collections.OrderedDict', []);
  3991. this._parameters = execution.invoke('collections.OrderedDict', []);
  3992. this._buffers = execution.invoke('collections.OrderedDict', []);
  3993. }
  3994. __setattr__(name, value) {
  3995. if (value instanceof torch.nn.modules.module.Module) {
  3996. this._modules.set(name, value);
  3997. } else {
  3998. this[name] = value;
  3999. }
  4000. }
  4001. __getattr__(name) {
  4002. if (this._modules.has(name)) {
  4003. return this._modules.get(name);
  4004. }
  4005. return this[name];
  4006. }
  4007. });
  4008. torch.nn.Module = torch.nn.modules.module.Module;
  4009. torch.nn.modules.Module = torch.nn.modules.module.Module;
  4010. this.registerType('torch.ao.quantization.fake_quantize.FakeQuantize', class {});
  4011. this.registerType('torch.ao.quantization.fake_quantize.FusedMovingAvgObsFakeQuantize', class {});
  4012. this.registerType('torch.ao.quantization.observer._PartialWrapper', class {});
  4013. this.registerType('torch.ao.quantization.observer.HistogramObserver', class {});
  4014. this.registerType('torch.ao.quantization.observer.MovingAverageMinMaxObserver', class {});
  4015. this.registerType('torch.ao.quantization.observer.MinMaxObserver', class {});
  4016. this.registerType('torch.ao.quantization.observer.PerChannelMinMaxObserver', class {});
  4017. this.registerType('torch.ao.quantization.observer.PlaceholderObserver', class {});
  4018. this.registerType('torch.ao.quantization.qconfig.QConfig', class {});
  4019. this.registerType('torch.ao.quantization.stubs.DeQuantStub', class {});
  4020. this.registerType('torch.ao.quantization.stubs.QuantStub', class {});
  4021. this.registerType('torch.ao.quantization.stubs.QuantWrapper', class {});
  4022. this.registerFunction('torch.ao.quantization.fx.graph_module._save_packed_weight', function() {
  4023. throw new python.Error("'torch.ao.quantization.fx.graph_module._save_packed_weight' not implemented.");
  4024. });
  4025. this.registerFunction('torch.ao.quantization.fx._lower_to_native_backend._load_packed_weight', function() {
  4026. throw new python.Error("'torch.ao.quantization.fx._lower_to_native_backend._load_packed_weight' not implemented.");
  4027. });
  4028. this.registerFunction('torch.ao.quantization.fx._lower_to_native_backend._save_packed_weight', function() {
  4029. throw new python.Error("'torch.ao.quantization.fx._lower_to_native_backend._save_packed_weight' not implemented.");
  4030. });
  4031. this.registerFunction('torch.ao.quantization.quantization_mappings._get_special_act_post_process', function() {
  4032. throw new python.Error("'torch.ao.quantization.quantization_mappings._get_special_act_post_process' not implemented.");
  4033. });
  4034. this.registerFunction('torch.ao.quantization.quantization_mappings.get_default_dynamic_quant_module_mappings', function() {
  4035. throw new python.Error("'torch.ao.quantization.quantization_mappings.get_default_dynamic_quant_module_mappings' not implemented.");
  4036. });
  4037. this.registerFunction('torch.ao.quantization.quantization_mappings.get_default_qat_module_mappings', function() {
  4038. throw new python.Error("'torch.ao.quantization.quantization_mappings.get_default_qat_module_mappings' not implemented.");
  4039. });
  4040. this.registerFunction('torch.ao.quantization.quantization_mappings.get_default_qconfig_propagation_list', function() {
  4041. throw new python.Error("'torch.ao.quantization.quantization_mappings.get_default_qconfig_propagation_list' not implemented.");
  4042. });
  4043. this.registerFunction('torch.ao.quantization.quantization_mappings.get_default_static_quant_module_mappings', function() {
  4044. throw new python.Error("'torch.ao.quantization.quantization_mappings.get_default_static_quant_module_mappings' not implemented.");
  4045. });
  4046. this.registerFunction('torch.ao.quantization.quantization_mappings.get_default_static_quant_reference_module_mappings', function() {
  4047. throw new python.Error("'torch.ao.quantization.quantization_mappings.get_default_static_quant_reference_module_mappings' not implemented.");
  4048. });
  4049. this.registerFunction('torch.ao.quantization.quantization_mappings.no_observer_set', function() {
  4050. throw new python.Error("'torch.ao.quantization.quantization_mappings.no_observer_set' not implemented.");
  4051. });
  4052. this.registerFunction('torch.ao.quantization.quantization_mappings._has_special_act_post_process', function() {
  4053. throw new python.Error("'torch.ao.quantization.quantization_mappings._has_special_act_post_process' not implemented.");
  4054. });
  4055. this.registerFunction('torch.ao.quantization.utils.get_qparam_dict', function() {
  4056. throw new python.Error("'torch.ao.quantization.utils.get_qparam_dict' not implemented.");
  4057. });
  4058. this.registerFunction('torch.ao.quantization.utils.has_no_children_ignoring_parametrizations', function() {
  4059. throw new python.Error("'torch.ao.quantization.utils.has_no_children_ignoring_parametrizations' not implemented.");
  4060. });
  4061. this.registerType('torch.autograd.variable.Variable', class {});
  4062. this.registerType('torch.backends.cudnn.rnn.Unserializable', class {});
  4063. this.registerFunction('torch.distributed._shard.sharded_tensor.pre_load_state_dict_hook', function() {
  4064. throw new python.Error("'torch.distributed._shard.sharded_tensor.pre_load_state_dict_hook' not implemented.");
  4065. });
  4066. this.registerFunction('torch.distributed._shard.sharded_tensor.state_dict_hook', function() {
  4067. throw new python.Error("'torch.distributed._shard.sharded_tensor.state_dict_hook' not implemented.");
  4068. });
  4069. this.registerType('torch.distributed.algorithms.join._JoinConfig', class {});
  4070. this.registerType('torch.distributions.bernoulli.Bernoulli', class {});
  4071. this.registerType('torch.distributions.categorical.Categorical', class {});
  4072. this.registerType('torch.distributions.constraints._LowerCholesky', class {});
  4073. this.registerType('torch.distributions.constraints._Real', class {});
  4074. this.registerType('torch.distributions.multivariate_normal.MultivariateNormal', class {});
  4075. this.registerType('torch.distributions.normal.Normal', class {});
  4076. this.registerType('torch.distributions.transforms._InverseTransform', class {});
  4077. this.registerType('torch.distributions.transforms.AffineTransform', class {});
  4078. this.registerType('torch.distributions.transforms.ComposeTransform', class {});
  4079. this.registerType('torch.distributions.transforms.ExpTransform', class {});
  4080. this.registerType('torch.distributions.transforms.LowerCholeskyTransform', class {});
  4081. this.registerType('torch.distributions.uniform.Uniform', class {});
  4082. this.registerType('torch.nn.backends.thnn._get_thnn_function_backend', class {});
  4083. this.registerType('torch.nn.intrinsic.modules.fused.ConvBnReLU2d', class {});
  4084. this.registerType('torch.nn.intrinsic.modules.fused.ConvReLU2d', class {});
  4085. this.registerType('torch.nn.intrinsic.modules.fused.BNReLU2d', class {});
  4086. this.registerType('torch.nn.intrinsic.qat.modules.conv_fused.ConvBn2d', class {});
  4087. this.registerType('torch.nn.intrinsic.qat.modules.conv_fused.ConvBnReLU2d', class {});
  4088. this.registerType('torch.nn.intrinsic.qat.modules.conv_fused.ConvReLU2d', class {});
  4089. this.registerType('torch.nn.intrinsic.quantized.modules.conv_relu.ConvReLU2d', class {});
  4090. this.registerType('torch.nn.intrinsic.quantized.modules.linear_relu.LinearReLU', class {});
  4091. this.registerType('torch.nn.modules.activation.CELU', class extends torch.nn.modules.module.Module {});
  4092. this.registerType('torch.nn.modules.activation.ELU', class extends torch.nn.modules.module.Module {});
  4093. this.registerType('torch.nn.modules.activation.GELU', class extends torch.nn.modules.module.Module {});
  4094. this.registerType('torch.nn.modules.activation.GLU', class extends torch.nn.modules.module.Module {});
  4095. this.registerType('torch.nn.modules.activation.Hardtanh', class extends torch.nn.modules.module.Module {});
  4096. this.registerType('torch.nn.modules.activation.Hardshrink', class extends torch.nn.modules.module.Module {});
  4097. this.registerType('torch.nn.modules.activation.Hardsigmoid', class extends torch.nn.modules.module.Module {});
  4098. this.registerType('torch.nn.modules.activation.Hardswish', class extends torch.nn.modules.module.Module {});
  4099. this.registerType('torch.nn.modules.activation.LeakyReLU', class extends torch.nn.modules.module.Module {});
  4100. this.registerType('torch.nn.modules.activation.LogSigmoid', class extends torch.nn.modules.module.Module {});
  4101. this.registerType('torch.nn.modules.activation.LogSoftmax', class extends torch.nn.modules.module.Module {});
  4102. this.registerType('torch.nn.modules.activation.Mish', class extends torch.nn.modules.module.Module {});
  4103. this.registerType('torch.nn.modules.activation.MultiheadAttention', class extends torch.nn.modules.module.Module {});
  4104. this.registerType('torch.nn.modules.activation.ReLU', class extends torch.nn.modules.module.Module {});
  4105. this.registerType('torch.nn.modules.activation.ReLU6', class extends torch.nn.modules.module.Module {});
  4106. this.registerType('torch.nn.modules.activation.PReLU', class extends torch.nn.modules.module.Module {});
  4107. this.registerType('torch.nn.modules.activation.RReLU', class extends torch.nn.modules.module.Module {});
  4108. this.registerType('torch.nn.modules.activation.SELU', class extends torch.nn.modules.module.Module {});
  4109. this.registerType('torch.nn.modules.activation.Sigmoid', class extends torch.nn.modules.module.Module {});
  4110. this.registerType('torch.nn.modules.activation.SiLU', class extends torch.nn.modules.module.Module {});
  4111. this.registerType('torch.nn.modules.activation.Softmax', class extends torch.nn.modules.module.Module {});
  4112. this.registerType('torch.nn.modules.activation.Softmax2d', class extends torch.nn.modules.module.Module {});
  4113. this.registerType('torch.nn.modules.activation.Softmin', class extends torch.nn.modules.module.Module {});
  4114. this.registerType('torch.nn.modules.activation.Softplus', class extends torch.nn.modules.module.Module {});
  4115. this.registerType('torch.nn.modules.activation.Softshrink', class extends torch.nn.modules.module.Module {});
  4116. this.registerType('torch.nn.modules.activation.Softsign', class extends torch.nn.modules.module.Module {});
  4117. this.registerType('torch.nn.modules.activation.Tanh', class extends torch.nn.modules.module.Module {});
  4118. this.registerType('torch.nn.modules.activation.Tanhshrink', class extends torch.nn.modules.module.Module {});
  4119. this.registerType('torch.nn.modules.activation.Threshold', class extends torch.nn.modules.module.Module {});
  4120. this.registerType('torch.nn.modules.adaptive.AdaptiveLogSoftmaxWithLoss', class {});
  4121. this.registerType('torch.nn.modules.batchnorm._NormBase', class extends torch.nn.modules.module.Module {});
  4122. this.registerType('torch.nn.modules.batchnorm._BatchNorm', class extends torch.nn.modules.batchnorm._NormBase {});
  4123. this.registerType('torch.nn.modules.batchnorm.BatchNorm1d', class extends torch.nn.modules.batchnorm._BatchNorm {});
  4124. this.registerType('torch.nn.modules.batchnorm.BatchNorm2d', class extends torch.nn.modules.batchnorm._BatchNorm {});
  4125. this.registerType('torch.nn.modules.batchnorm.BatchNorm3d', class extends torch.nn.modules.batchnorm._BatchNorm {});
  4126. this.registerType('torch.nn.modules.batchnorm.LazyBatchNorm1d', class {});
  4127. this.registerType('torch.nn.modules.batchnorm.LazyBatchNorm2d', class {});
  4128. this.registerType('torch.nn.modules.batchnorm.LazyBatchNorm3d', class {});
  4129. this.registerType('torch.nn.modules.batchnorm.SyncBatchNorm', class {});
  4130. this.registerType('torch.nn.modules.channelshuffle.ChannelShuffle', class {});
  4131. this.registerType('torch.nn.modules.container.ModuleDict', class extends torch.nn.modules.module.Module {});
  4132. this.registerType('torch.nn.modules.container.ModuleList', class extends torch.nn.modules.module.Module {});
  4133. this.registerType('torch.nn.modules.container.ParameterDict', class extends torch.nn.modules.module.Module {});
  4134. this.registerType('torch.nn.modules.container.ParameterList', class extends torch.nn.modules.module.Module {});
  4135. this.registerType('torch.nn.modules.container.Sequential', class extends torch.nn.modules.module.Module {});
  4136. this.registerType('torch.nn.modules.conv._ConvNd', class extends torch.nn.modules.module.Module {});
  4137. this.registerType('torch.nn.modules.conv.Conv1d', class extends torch.nn.modules.conv._ConvNd {});
  4138. this.registerType('torch.nn.modules.conv.Conv2d', class extends torch.nn.modules.conv._ConvNd {});
  4139. this.registerType('torch.nn.modules.conv.Conv3d', class extends torch.nn.modules.conv._ConvNd {});
  4140. this.registerType('torch.nn.modules.conv._ConvTransposeNd', class extends torch.nn.modules.module.Module {});
  4141. this.registerType('torch.nn.modules.conv.ConvTranspose1d', class extends torch.nn.modules.conv._ConvTransposeNd {});
  4142. this.registerType('torch.nn.modules.conv.ConvTranspose2d', class extends torch.nn.modules.conv._ConvTransposeNd {});
  4143. this.registerType('torch.nn.modules.conv.ConvTranspose3d', class extends torch.nn.modules.conv._ConvTransposeNd {});
  4144. this.registerType('torch.nn.modules.conv.LazyConv1d', class {});
  4145. this.registerType('torch.nn.modules.conv.LazyConv2d', class {});
  4146. this.registerType('torch.nn.modules.conv.LazyConv3d', class {});
  4147. this.registerType('torch.nn.modules.conv.LazyConvTranspose2d', class {});
  4148. this.registerType('torch.nn.modules.distance.CosineSimilarity', class {});
  4149. this.registerType('torch.nn.modules.dropout._DropoutNd', class extends torch.nn.modules.module.Module {});
  4150. this.registerType('torch.nn.modules.dropout.AlphaDropout', class extends torch.nn.modules.dropout._DropoutNd {});
  4151. this.registerType('torch.nn.modules.dropout.Dropout', class extends torch.nn.modules.dropout._DropoutNd {});
  4152. this.registerType('torch.nn.modules.dropout.Dropout1d', class extends torch.nn.modules.dropout._DropoutNd {});
  4153. this.registerType('torch.nn.modules.dropout.Dropout2d', class extends torch.nn.modules.dropout._DropoutNd {});
  4154. this.registerType('torch.nn.modules.dropout.Dropout3d', class extends torch.nn.modules.dropout._DropoutNd {});
  4155. this.registerType('torch.nn.modules.dropout.FeatureAlphaDropout', class extends torch.nn.modules.dropout._DropoutNd {});
  4156. this.registerType('torch.nn.modules.fold.Fold', class {});
  4157. this.registerType('torch.nn.modules.fold.Unfold', class {});
  4158. this.registerType('torch.nn.modules.flatten.Flatten', class extends torch.nn.modules.module.Module {});
  4159. this.registerType('torch.nn.modules.flatten.Unflatten', class extends torch.nn.modules.module.Module {});
  4160. this.registerType('torch.nn.modules.instancenorm.InstanceNorm1d', class {});
  4161. this.registerType('torch.nn.modules.instancenorm.InstanceNorm2d', class {});
  4162. this.registerType('torch.nn.modules.instancenorm.InstanceNorm3d', class {});
  4163. this.registerType('torch.nn.modules.instancenorm.LazyInstanceNorm2d', class {});
  4164. this.registerType('torch.nn.modules.linear._LinearWithBias', class {});
  4165. this.registerType('torch.nn.modules.linear.Bilinear', class extends torch.nn.modules.module.Module {});
  4166. this.registerType('torch.nn.modules.linear.Identity', class extends torch.nn.modules.module.Module {});
  4167. this.registerType('torch.nn.modules.linear.LazyLinear', class extends torch.nn.modules.module.Module {});
  4168. this.registerType('torch.nn.modules.linear.Linear', class extends torch.nn.modules.module.Module {});
  4169. this.registerType('torch.nn.modules.linear.NonDynamicallyQuantizableLinear', class extends torch.nn.modules.linear.Linear {});
  4170. this.registerType('torch.nn.modules.loss._Loss', class extends torch.nn.modules.module.Module {});
  4171. this.registerType('torch.nn.modules.loss._WeightedLoss', class extends torch.nn.modules.loss._Loss {});
  4172. this.registerType('torch.nn.modules.loss.BCELoss', class extends torch.nn.modules.loss._WeightedLoss {});
  4173. this.registerType('torch.nn.modules.loss.BCEWithLogitsLoss', class extends torch.nn.modules.loss._Loss {});
  4174. this.registerType('torch.nn.modules.loss.CrossEntropyLoss', class extends torch.nn.modules.loss._WeightedLoss {});
  4175. this.registerType('torch.nn.modules.loss.CosineEmbeddingLoss', class extends torch.nn.modules.loss._Loss {});
  4176. this.registerType('torch.nn.modules.loss.CTCLoss', class extends torch.nn.modules.loss._Loss {});
  4177. this.registerType('torch.nn.modules.loss.GaussianNLLLoss', class extends torch.nn.modules.loss._Loss {});
  4178. this.registerType('torch.nn.modules.loss.HuberLoss', class extends torch.nn.modules.loss._Loss {});
  4179. this.registerType('torch.nn.modules.loss.HingeEmbeddingLoss', class extends torch.nn.modules.loss._Loss {});
  4180. this.registerType('torch.nn.modules.loss.KLDivLoss', class extends torch.nn.modules.loss._Loss {});
  4181. this.registerType('torch.nn.modules.loss.L1Loss', class extends torch.nn.modules.loss._Loss {});
  4182. this.registerType('torch.nn.modules.loss.MarginRankingLoss', class extends torch.nn.modules.loss._Loss {});
  4183. this.registerType('torch.nn.modules.loss.MultiLabelMarginLoss', class extends torch.nn.modules.loss._Loss {});
  4184. this.registerType('torch.nn.modules.loss.MultiLabelSoftMarginLoss', class extends torch.nn.modules.loss._Loss {});
  4185. this.registerType('torch.nn.modules.loss.MultiMarginLoss', class extends torch.nn.modules.loss._WeightedLoss {});
  4186. this.registerType('torch.nn.modules.loss.MSELoss', class extends torch.nn.modules.loss._Loss {});
  4187. this.registerType('torch.nn.modules.loss.NLLLoss', class extends torch.nn.modules.loss._WeightedLoss {});
  4188. this.registerType('torch.nn.modules.loss.NLLLoss2d', class extends torch.nn.modules.loss.NLLLoss {});
  4189. this.registerType('torch.nn.modules.loss.PoissonNLLLoss', class {});
  4190. this.registerType('torch.nn.modules.loss.SmoothL1Loss', class {});
  4191. this.registerType('torch.nn.modules.loss.SoftMarginLoss', class {});
  4192. this.registerType('torch.nn.modules.loss.TripletMarginLoss', class {});
  4193. this.registerType('torch.nn.modules.loss.TripletMarginWithDistanceLoss', class {});
  4194. this.registerType('torch.nn.modules.module._IncompatibleKeys', class {});
  4195. this.registerType('torch.nn.modules.module._WrappedHook', class {});
  4196. this.registerType('torch.nn.modules.module.PatchForward', class {});
  4197. this.registerType('torch.nn.modules.normalization.CrossMapLRN2d', class {});
  4198. this.registerType('torch.nn.modules.normalization.GroupNorm', class extends torch.nn.modules.module.Module {});
  4199. this.registerType('torch.nn.modules.normalization.LayerNorm', class extends torch.nn.modules.module.Module {});
  4200. this.registerType('torch.nn.modules.normalization.LocalResponseNorm', class {});
  4201. this.registerType('torch.nn.modules.padding._ReflectionPadNd', class extends torch.nn.modules.module.Module {});
  4202. this.registerType('torch.nn.modules.padding.ReflectionPad1d', class extends torch.nn.modules.padding._ReflectionPadNd {});
  4203. this.registerType('torch.nn.modules.padding.ReflectionPad2d', class extends torch.nn.modules.padding._ReflectionPadNd {});
  4204. this.registerType('torch.nn.modules.padding.ReflectionPad3d', class extends torch.nn.modules.padding._ReflectionPadNd {});
  4205. this.registerType('torch.nn.modules.padding._ReplicationPadNd', class extends torch.nn.modules.module.Module {});
  4206. this.registerType('torch.nn.modules.padding.ReplicationPad1d', class extends torch.nn.modules.padding._ReplicationPadNd {});
  4207. this.registerType('torch.nn.modules.padding.ReplicationPad2d', class extends torch.nn.modules.padding._ReplicationPadNd {});
  4208. this.registerType('torch.nn.modules.padding.ReplicationPad3d', class extends torch.nn.modules.padding._ReplicationPadNd {});
  4209. this.registerType('torch.nn.modules.padding.ZeroPad2d', class {});
  4210. this.registerType('torch.nn.modules.padding.ConstantPad1d', class {});
  4211. this.registerType('torch.nn.modules.padding.ConstantPad2d', class {});
  4212. this.registerType('torch.nn.modules.padding.ConstantPad3d', class {});
  4213. this.registerType('torch.nn.modules.pixelshuffle.PixelShuffle', class {});
  4214. this.registerType('torch.nn.modules.pixelshuffle.PixelUnshuffle', class {});
  4215. this.registerType('torch.nn.modules.pooling._AdaptiveAvgPoolNd', class extends torch.nn.modules.module.Module {});
  4216. this.registerType('torch.nn.modules.pooling.AdaptiveAvgPool1d', class extends torch.nn.modules.pooling._AdaptiveAvgPoolNd {});
  4217. this.registerType('torch.nn.modules.pooling.AdaptiveAvgPool2d', class extends torch.nn.modules.pooling._AdaptiveAvgPoolNd {});
  4218. this.registerType('torch.nn.modules.pooling.AdaptiveAvgPool3d', class extends torch.nn.modules.pooling._AdaptiveAvgPoolNd {});
  4219. this.registerType('torch.nn.modules.pooling.AdaptiveMaxPool1d', class {});
  4220. this.registerType('torch.nn.modules.pooling.AdaptiveMaxPool2d', class {});
  4221. this.registerType('torch.nn.modules.pooling.AdaptiveMaxPool3d', class {});
  4222. this.registerType('torch.nn.modules.pooling.AvgPool1d', class {});
  4223. this.registerType('torch.nn.modules.pooling.AvgPool2d', class {});
  4224. this.registerType('torch.nn.modules.pooling.AvgPool3d', class {});
  4225. this.registerType('torch.nn.modules.pooling.FractionalMaxPool2d', class {});
  4226. this.registerType('torch.nn.modules.pooling.LPPool2d', class {});
  4227. this.registerType('torch.nn.modules.pooling.MaxPool1d', class {});
  4228. this.registerType('torch.nn.modules.pooling.MaxPool2d', class {});
  4229. this.registerType('torch.nn.modules.pooling.MaxPool3d', class {});
  4230. this.registerType('torch.nn.modules.pooling.MaxUnpool1d', class {});
  4231. this.registerType('torch.nn.modules.pooling.MaxUnpool2d', class {});
  4232. this.registerType('torch.nn.modules.pooling.MaxUnpool3d', class {});
  4233. this.registerType('torch.nn.modules.rnn.GRU', class {});
  4234. this.registerType('torch.nn.modules.rnn.GRUCell', class {});
  4235. this.registerType('torch.nn.modules.rnn.LSTM', class {});
  4236. this.registerType('torch.nn.modules.rnn.LSTMCell', class {});
  4237. this.registerType('torch.nn.modules.rnn.RNNBase', class extends torch.nn.modules.module.Module {});
  4238. this.registerType('torch.nn.modules.rnn.RNN', class extends torch.nn.modules.rnn.RNNBase {});
  4239. this.registerType('torch.nn.modules.rnn.RNNCellBase', class extends torch.nn.modules.module.Module {});
  4240. this.registerType('torch.nn.modules.rnn.RNNCell', class extends torch.nn.modules.rnn.RNNCellBase {});
  4241. this.registerType('torch.nn.modules.sparse.Embedding', class {});
  4242. this.registerType('torch.nn.modules.sparse.EmbeddingBag', class {});
  4243. this.registerType('torch.nn.modules.transformer.Transformer', class {});
  4244. this.registerType('torch.nn.modules.transformer.TransformerDecoder', class {});
  4245. this.registerType('torch.nn.modules.transformer.TransformerDecoderLayer', class {});
  4246. this.registerType('torch.nn.modules.transformer.TransformerEncoder', class {});
  4247. this.registerType('torch.nn.modules.transformer.TransformerEncoderLayer', class {});
  4248. this.registerType('torch.nn.modules.upsampling.Upsample', class {});
  4249. this.registerType('torch.nn.modules.upsampling.UpsamplingBilinear2d', class {});
  4250. this.registerType('torch.nn.modules.upsampling.UpsamplingNearest2d', class {});
  4251. this.registerType('torch.nn.parallel.data_parallel.DataParallel', class {});
  4252. this.registerType('torch.nn.parallel.distributed._DDPUnevenInputsConfig', class {});
  4253. this.registerType('torch.nn.parallel.distributed.DistributedDataParallel', class {});
  4254. this.registerType('torch.nn.qat.modules.conv.Conv2d', class {});
  4255. this.registerType('torch.nn.qat.modules.linear.Linear', class {});
  4256. this.registerType('torch.nn.quantized.modules.activation.ReLU', class {});
  4257. this.registerType('torch.nn.quantized.modules.activation.LeakyReLU', class {});
  4258. this.registerType('torch.nn.quantized.dynamic.modules.linear.Linear', class {});
  4259. this.registerType('torch.nn.quantized.dynamic.modules.rnn.GRU', class {});
  4260. this.registerType('torch.nn.quantized.dynamic.modules.rnn.LSTM', class {});
  4261. this.registerType('torch.nn.quantized.dynamic.modules.rnn.LSTMCell', class {});
  4262. this.registerType('torch.nn.quantized.dynamic.modules.rnn.PackedParameter', class {});
  4263. this.registerType('torch.nn.quantized.modules.activation.ReLU6', class {});
  4264. this.registerType('torch.nn.quantized.modules.batchnorm.BatchNorm2d', class {});
  4265. this.registerType('torch.nn.quantized.modules.conv.Conv1d', class {});
  4266. this.registerType('torch.nn.quantized.modules.conv.Conv2d', class {});
  4267. this.registerType('torch.nn.quantized.modules.conv.ConvTranspose2d', class {});
  4268. this.registerType('torch.nn.quantized.modules.DeQuantize', class {});
  4269. this.registerType('torch.nn.quantized.modules.dropout.Dropout', class {});
  4270. this.registerType('torch.nn.quantized.modules.embedding_ops.Embedding', class {});
  4271. this.registerType('torch.nn.quantized.modules.embedding_ops.EmbeddingPackedParams', class {});
  4272. this.registerType('torch.nn.quantized.modules.functional_modules.FloatFunctional', class {});
  4273. this.registerType('torch.nn.quantized.modules.functional_modules.QFunctional', class {});
  4274. this.registerType('torch.nn.quantized.modules.linear.Linear', class {});
  4275. this.registerType('torch.nn.quantized.modules.linear.LinearPackedParams', class {});
  4276. this.registerType('torch.nn.quantized.modules.normalization.InstanceNorm2d', class {});
  4277. this.registerType('torch.nn.quantized.modules.normalization.GroupNorm', class extends torch.nn.modules.normalization.GroupNorm {});
  4278. this.registerType('torch.nn.quantized.modules.normalization.LayerNorm', class extends torch.nn.modules.normalization.LayerNorm {});
  4279. this.registerType('torch.nn.quantized.modules.Quantize', class {});
  4280. this.registerType('torch.ao.nn.quantizable.modules.activation.MultiheadAttention', class extends torch.nn.modules.activation.MultiheadAttention {});
  4281. this.registerType('torch.ao.nn.quantizable.modules.rnn.LSTM', class {});
  4282. this.registerType('torch.ao.nn.quantized.modules.activation.MultiheadAttention', class extends torch.ao.nn.quantizable.modules.activation.MultiheadAttention {});
  4283. this.registerType('torch.ao.nn.quantized.modules.activation.ReLU6', class extends torch.nn.modules.activation.ReLU {});
  4284. this.registerType('torch.ao.nn.quantized.modules.activation.LeakyReLU', class extends torch.nn.modules.activation.LeakyReLU {});
  4285. this.registerType('torch.ao.nn.quantized.modules.utils.WeightedQuantizedModule', class extends torch.nn.modules.module.Module {});
  4286. this.registerType('torch.ao.nn.quantized.modules.batchnorm._BatchNorm', class extends torch.nn.modules.batchnorm._BatchNorm {});
  4287. this.registerType('torch.ao.nn.quantized.modules.batchnorm.BatchNorm2d', class extends torch.ao.nn.quantized.modules.batchnorm._BatchNorm {});
  4288. this.registerType('torch.ao.nn.quantized.modules.conv.Conv1d', class extends torch.nn.modules.module.Module {});
  4289. this.registerType('torch.ao.nn.quantized.modules.conv.Conv2d', class extends torch.nn.modules.module.Module {});
  4290. this.registerType('torch.ao.nn.quantized.modules.conv._ConvNd', class extends torch.ao.nn.quantized.modules.utils.WeightedQuantizedModule {});
  4291. this.registerType('torch.ao.nn.quantized.modules.conv._ConvTransposeNd', class extends torch.ao.nn.quantized.modules.conv._ConvNd {});
  4292. this.registerType('torch.ao.nn.quantized.modules.conv.ConvTranspose2d', class extends torch.ao.nn.quantized.modules.conv._ConvTransposeNd {});
  4293. this.registerType('torch.ao.nn.quantized.modules.Quantize', class extends torch.nn.modules.module.Module {});
  4294. this.registerType('torch.ao.nn.quantized.modules.DeQuantize', class extends torch.nn.modules.module.Module {});
  4295. this.registerType('torch.ao.nn.quantized.modules.dropout.Dropout', class extends torch.nn.modules.dropout.Dropout {});
  4296. this.registerType('torch.ao.nn.quantized.modules.functional_modules.FloatFunctional', class extends torch.nn.modules.module.Module {});
  4297. this.registerType('torch.ao.nn.quantized.modules.functional_modules.QFunctional', class extends torch.nn.modules.module.Module {});
  4298. this.registerType('torch.ao.nn.quantized.modules.functional_modules.FXFloatFunctional', class extends torch.nn.modules.module.Module {});
  4299. this.registerType('torch.ao.nn.quantized.modules.linear.Linear', class extends torch.ao.nn.quantized.modules.utils.WeightedQuantizedModule {});
  4300. this.registerType('torch.ao.nn.quantized.modules.linear.LinearPackedParams', class extends torch.nn.modules.module.Module {});
  4301. this.registerType('torch.ao.nn.quantized.modules.normalization.LayerNorm', class extends torch.nn.modules.normalization.LayerNorm {});
  4302. this.registerType('torch.ao.nn.quantized.modules.rnn.LSTM', class {});
  4303. this.registerType('torch.ao.nn.quantized.dynamic.modules.linear.Linear', class extends torch.ao.nn.quantized.modules.linear.Linear {});
  4304. this.registerType('torch.ao.nn.quantized.dynamic.modules.rnn.PackedParameter', class extends torch.nn.modules.module.Module {});
  4305. this.registerType('torch.ao.nn.quantized.dynamic.modules.rnn.RNNBase', class extends torch.nn.modules.module.Module {});
  4306. this.registerType('torch.ao.nn.quantized.dynamic.modules.rnn.GRU', class extends torch.ao.nn.quantized.dynamic.modules.rnn.RNNBase {});
  4307. this.registerType('torch.ao.nn.quantized.reference.modules.conv.Conv2d', class {});
  4308. this.registerType('torch.ao.nn.quantized.reference.modules.linear.Linear', class {});
  4309. this.registerType('torch.ao.nn.qat.modules.conv.Conv2d', class {});
  4310. this.registerType('torch.ao.nn.intrinsic.quantized.modules.conv_relu.ConvReLU2d', class extends torch.ao.nn.quantized.modules.conv.Conv2d {});
  4311. this.registerType('torch.ao.nn.intrinsic.quantized.modules.linear_relu.LinearReLU', class extends torch.ao.nn.quantized.modules.linear.Linear {});
  4312. this.registerType('torch.ao.nn.intrinsic.modules.fused._FusedModule', class extends torch.nn.modules.container.Sequential {});
  4313. this.registerType('torch.ao.nn.intrinsic.modules.fused.ConvBn2d', class extends torch.ao.nn.intrinsic.modules.fused._FusedModule {});
  4314. this.registerType('torch.ao.nn.intrinsic.modules.fused.ConvReLU2d', class extends torch.ao.nn.intrinsic.modules.fused._FusedModule {});
  4315. this.registerType('torch.ao.nn.intrinsic.modules.fused.LinearReLU', class extends torch.ao.nn.intrinsic.modules.fused._FusedModule {});
  4316. this.registerType('torch.ao.nn.intrinsic.modules.fused.ConvBnReLU2d', class extends torch.ao.nn.intrinsic.modules.fused._FusedModule {});
  4317. this.registerType('torch.nn.utils.prune.L1Unstructured', class {});
  4318. this.registerType('torch.nn.utils.spectral_norm.SpectralNorm', class {});
  4319. this.registerType('torch.nn.utils.spectral_norm.SpectralNormStateDictHook', class {});
  4320. this.registerType('torch.nn.utils.spectral_norm.SpectralNormLoadStateDictPreHook', class {});
  4321. this.registerType('torch.nn.utils.weight_norm.WeightNorm', class {});
  4322. this.registerFunction('torch.nn.utils.parametrize.type_before_parametrizations', function() {
  4323. throw python.Error("'torch.nn.utils.parametrize.type_before_parametrizations' not implemented.");
  4324. });
  4325. this.registerType('torch.torch_version.TorchVersion', class extends String {});
  4326. this.registerType('torch.optim.optimizer.Optimizer', class {});
  4327. this.registerType('torch.optim.adam.Adam', class extends torch.optim.optimizer.Optimizer {});
  4328. this.registerType('torch.optim.adamw.AdamW', class {});
  4329. this.registerType('torch.optim.adagrad.Adagrad', class {});
  4330. this.registerType('torch.optim.adadelta.Adadelta', class {});
  4331. this.registerType('torch.optim.lbfgs.LBFGS', class {});
  4332. this.registerType('torch.optim.lr_scheduler.CosineAnnealingLR', class {});
  4333. this.registerType('torch.optim.lr_scheduler.CyclicLR', class {});
  4334. this.registerType('torch.optim.lr_scheduler.ExponentialLR', class {});
  4335. this.registerType('torch.optim.lr_scheduler.LambdaLR', class {});
  4336. this.registerType('torch.optim.lr_scheduler.LinearLR', class {});
  4337. this.registerType('torch.optim.lr_scheduler.MultiStepLR', class {});
  4338. this.registerType('torch.optim.lr_scheduler.OneCycleLR', class {});
  4339. this.registerType('torch.optim.lr_scheduler.ReduceLROnPlateau', class {});
  4340. this.registerType('torch.optim.lr_scheduler.StepLR', class {});
  4341. this.registerType('torch.optim.optimizer._RequiredParameter', class {});
  4342. this.registerType('torch.optim.radam.RAdam', class extends torch.optim.optimizer.Optimizer {});
  4343. this.registerType('torch.optim.rmsprop.RMSprop', class {});
  4344. this.registerType('torch.optim.sgd.SGD', class {});
  4345. this.registerType('torch.optim.sparse_adam.SparseAdam', class {});
  4346. this.registerType('torch.optim.swa_utils.SWALR', class {});
  4347. torch.optim.RAdam = torch.optim.radam.RAdam;
  4348. this.registerType('torch.quantization.fake_quantize.FakeQuantize', class {});
  4349. this.registerType('torch.quantization.observer._PartialWrapper', class {});
  4350. this.registerType('torch.quantization.observer.HistogramObserver', class {});
  4351. this.registerType('torch.quantization.observer.MinMaxObserver', class {});
  4352. this.registerType('torch.quantization.observer.MovingAverageMinMaxObserver', class {});
  4353. this.registerType('torch.quantization.observer.MovingAveragePerChannelMinMaxObserver', class {});
  4354. this.registerType('torch.quantization.qconfig.QConfig', class {});
  4355. this.registerType('torch.quantization.stubs.DeQuantStub', class {});
  4356. this.registerType('torch.quantization.stubs.QuantStub', class {});
  4357. this.registerType('torch.utils._pytree.LeafSpec', class {});
  4358. this.registerType('torch.utils._pytree.TreeSpec', class {});
  4359. this.registerFunction('torch.utils.data._utils.collate.default_collate', function() {
  4360. throw new python.Error("'torch.utils.data._utils.collate.default_collate' not implemented.");
  4361. });
  4362. this.registerType('torch.utils.data.dataloader._MultiProcessingDataLoaderIter', class {});
  4363. this.registerType('torch.utils.data.dataloader.DataLoader', class {});
  4364. this.registerType('torch.utils.data.dataset.Subset', class {});
  4365. this.registerType('torch.utils.data.dataset.ConcatDataset', class {});
  4366. this.registerType('torch.utils.data.dataset.TensorDataset', class {});
  4367. this.registerType('torch.utils.data.sampler.BatchSampler', class {});
  4368. this.registerType('torch.utils.data.sampler.RandomSampler', class {});
  4369. this.registerType('torch.utils.data.sampler.SequentialSampler', class {});
  4370. this.registerType('torchvision.datasets.folder.ImageFolder', class {});
  4371. this.registerType('torchvision.datasets.mnist.FashionMNIST', class {});
  4372. this.registerType('torchvision.datasets.mnist.MNIST', class {});
  4373. this.registerType('torchvision.datasets.video_utils.VideoClips', class {});
  4374. this.registerType('torchvision.datasets.vision.StandardTransform', class {});
  4375. this.registerType('torchvision.ops.deform_conv.DeformConv2d', class {});
  4376. this.registerType('torchvision.ops.feature_pyramid_network.FeaturePyramidNetwork', class {});
  4377. this.registerType('torchvision.ops.feature_pyramid_network.LastLevelMaxPool', class {});
  4378. this.registerType('torchvision.ops.feature_pyramid_network.LastLevelP6P7', class {});
  4379. this.registerType('torchvision.ops.misc.Conv2dNormActivation', class {});
  4380. this.registerType('torchvision.ops.misc.ConvNormActivation', class {});
  4381. this.registerType('torchvision.ops.misc.MLP', class extends torch.nn.modules.container.Sequential {});
  4382. this.registerType('torchvision.ops.misc.ConvTranspose2d', class {});
  4383. this.registerType('torchvision.ops.misc.FrozenBatchNorm2d', class {});
  4384. this.registerType('torchvision.ops.misc.Permute', class {});
  4385. this.registerType('torchvision.ops.misc.SqueezeExcitation', class {});
  4386. this.registerType('torchvision.ops.poolers.LevelMapper', class {});
  4387. this.registerType('torchvision.ops.poolers.MultiScaleRoIAlign', class {});
  4388. this.registerType('torchvision.ops.stochastic_depth.StochasticDepth', class {});
  4389. this.registerType('torchvision.models.alexnet.AlexNet', class {});
  4390. this.registerType('torchvision.models.convnext.ConvNeXt', class {});
  4391. this.registerType('torchvision.models.convnext.CNBlock', class {});
  4392. this.registerType('torchvision.models.convnext.LayerNorm2d', class {});
  4393. this.registerType('torchvision.models.densenet.DenseNet', class extends torch.nn.modules.module.Module {});
  4394. this.registerType('torchvision.models.densenet._DenseBlock', class extends torch.nn.modules.container.ModuleDict {});
  4395. this.registerType('torchvision.models.densenet._DenseLayer', class extends torch.nn.modules.module.Module {});
  4396. this.registerType('torchvision.models.densenet._Transition', class extends torch.nn.modules.container.Sequential {});
  4397. this.registerType('torchvision.models.detection._utils.BalancedPositiveNegativeSampler', class {});
  4398. this.registerType('torchvision.models.detection._utils.BoxCoder', class {});
  4399. this.registerType('torchvision.models.detection._utils.Matcher', class {});
  4400. this.registerType('torchvision.models.detection._utils.SSDMatcher', class {});
  4401. this.registerType('torchvision.models.detection.anchor_utils.AnchorGenerator', class {});
  4402. this.registerType('torchvision.models.detection.anchor_utils.DefaultBoxGenerator', class {});
  4403. this.registerType('torchvision.models.detection.backbone_utils.BackboneWithFPN', class {});
  4404. this.registerType('torchvision.models.detection.faster_rcnn.FasterRCNN', class {});
  4405. this.registerType('torchvision.models.detection.faster_rcnn.FastRCNNConvFCHead', class {});
  4406. this.registerType('torchvision.models.detection.faster_rcnn.FastRCNNPredictor', class {});
  4407. this.registerType('torchvision.models.detection.faster_rcnn.TwoMLPHead', class {});
  4408. this.registerType('torchvision.models.detection.fcos.FCOS', class {});
  4409. this.registerType('torchvision.models.detection.fcos.FCOSHead', class {});
  4410. this.registerType('torchvision.models.detection.fcos.FCOSClassificationHead', class {});
  4411. this.registerType('torchvision.models.detection.fcos.FCOSRegressionHead', class {});
  4412. this.registerType('torchvision.models.detection._utils.BoxLinearCoder', class {});
  4413. this.registerType('torchvision.models.detection.keypoint_rcnn.KeypointRCNN', class {});
  4414. this.registerType('torchvision.models.detection.keypoint_rcnn.KeypointRCNNHeads', class {});
  4415. this.registerType('torchvision.models.detection.keypoint_rcnn.KeypointRCNNPredictor', class {});
  4416. this.registerType('torchvision.models.detection.mask_rcnn.MaskRCNN', class {});
  4417. this.registerType('torchvision.models.detection.mask_rcnn.MaskRCNNHeads', class {});
  4418. this.registerType('torchvision.models.detection.mask_rcnn.MaskRCNNPredictor', class {});
  4419. this.registerType('torchvision.models.detection.retinanet.RetinaNet', class {});
  4420. this.registerType('torchvision.models.detection.retinanet.RetinaNetClassificationHead', class {});
  4421. this.registerType('torchvision.models.detection.retinanet.RetinaNetHead', class {});
  4422. this.registerType('torchvision.models.detection.retinanet.RetinaNetRegressionHead', class {});
  4423. this.registerType('torchvision.models.detection.roi_heads.RoIHeads', class {});
  4424. this.registerType('torchvision.models.detection.rpn.AnchorGenerator', class {});
  4425. this.registerType('torchvision.models.detection.rpn.RegionProposalNetwork', class {});
  4426. this.registerType('torchvision.models.detection.rpn.RPNHead', class {});
  4427. this.registerType('torchvision.models.detection.ssd.SSD', class {});
  4428. this.registerType('torchvision.models.detection.ssd.SSDFeatureExtractorVGG', class {});
  4429. this.registerType('torchvision.models.detection.ssdlite.SSDLiteClassificationHead', class {});
  4430. this.registerType('torchvision.models.detection.ssdlite.SSDLiteFeatureExtractorMobileNet', class {});
  4431. this.registerType('torchvision.models.detection.ssdlite.SSDLiteHead', class {});
  4432. this.registerType('torchvision.models.detection.ssdlite.SSDLiteRegressionHead', class {});
  4433. this.registerType('torchvision.models.detection.transform.GeneralizedRCNNTransform', class {});
  4434. this.registerType('torchvision.models.efficientnet.EfficientNet', class {});
  4435. this.registerType('torchvision.models.efficientnet.FusedMBConv', class {});
  4436. this.registerType('torchvision.models.efficientnet.MBConv', class {});
  4437. this.registerType('torchvision.models.googlenet.BasicConv2d', class {});
  4438. this.registerType('torchvision.models.googlenet.GoogLeNet', class {});
  4439. this.registerType('torchvision.models.googlenet.Inception', class {});
  4440. this.registerType('torchvision.models.googlenet.InceptionAux', class {});
  4441. this.registerType('torchvision.models.inception.BasicConv2d', class {});
  4442. this.registerType('torchvision.models.inception.Inception3', class {});
  4443. this.registerType('torchvision.models.inception.InceptionAux', class {});
  4444. this.registerType('torchvision.models.inception.InceptionA', class {});
  4445. this.registerType('torchvision.models.inception.InceptionB', class {});
  4446. this.registerType('torchvision.models.inception.InceptionC', class {});
  4447. this.registerType('torchvision.models.inception.InceptionD', class {});
  4448. this.registerType('torchvision.models.inception.InceptionE', class {});
  4449. this.registerType('torchvision.models.mnasnet._InvertedResidual', class {});
  4450. this.registerType('torchvision.models.mnasnet.MNASNet', class {});
  4451. this.registerType('torchvision.models.maxvit.MaxVit', class {});
  4452. this.registerType('torchvision.models.maxvit.MaxVitBlock', class {});
  4453. this.registerType('torchvision.models.maxvit.MaxVitLayer', class {});
  4454. this.registerType('torchvision.models.maxvit.MBConv', class {});
  4455. this.registerType('torchvision.models.maxvit.PartitionAttentionLayer', class {});
  4456. this.registerType('torchvision.models.maxvit.RelativePositionalMultiHeadAttention', class {});
  4457. this.registerType('torchvision.models.maxvit.SwapAxes', class {});
  4458. this.registerType('torchvision.models.maxvit.WindowDepartition', class {});
  4459. this.registerType('torchvision.models.mobilenet.ConvBNReLU', class {});
  4460. this.registerType('torchvision.models.mobilenet.MobileNetV2', class {});
  4461. this.registerType('torchvision.models.mobilenet.InvertedResidual', class {});
  4462. this.registerType('torchvision.models.mobilenetv2.ConvBNActivation', class {});
  4463. this.registerType('torchvision.models.mobilenetv2.InvertedResidual', class {});
  4464. this.registerType('torchvision.models.mobilenetv2.MobileNetV2', class {});
  4465. this.registerType('torchvision.models.mobilenetv3.InvertedResidual', class {});
  4466. this.registerType('torchvision.models.mobilenetv3.MobileNetV3', class {});
  4467. this.registerType('torchvision.models.mobilenetv3.SqueezeExcitation', class {});
  4468. this.registerType('torchvision.models.regnet.AnyStage', class extends torch.nn.modules.container.Sequential {});
  4469. this.registerType('torchvision.models.regnet.BottleneckTransform', class {});
  4470. this.registerType('torchvision.models.regnet.ResBottleneckBlock', class {});
  4471. this.registerType('torchvision.models.regnet.RegNet', class {});
  4472. this.registerType('torchvision.models.regnet.SimpleStemIN', class {});
  4473. this.registerType('torchvision.models.resnet.Bottleneck', class {});
  4474. this.registerType('torchvision.models.resnet.BasicBlock', class {});
  4475. this.registerType('torchvision.models.quantization.mobilenet.QuantizableInvertedResidual', class {});
  4476. this.registerType('torchvision.models.quantization.mobilenet.QuantizableMobileNetV2', class {});
  4477. this.registerType('torchvision.models.quantization.mobilenetv2.QuantizableInvertedResidual', class {});
  4478. this.registerType('torchvision.models.quantization.mobilenetv2.QuantizableMobileNetV2', class {});
  4479. this.registerType('torchvision.models.quantization.resnet.QuantizableBasicBlock', class {});
  4480. this.registerType('torchvision.models.quantization.resnet.QuantizableBottleneck', class {});
  4481. this.registerType('torchvision.models.quantization.resnet.QuantizableResNet', class {});
  4482. this.registerType('torchvision.models.segmentation.deeplabv3.ASPP', class {});
  4483. this.registerType('torchvision.models.segmentation.deeplabv3.ASPPConv', class {});
  4484. this.registerType('torchvision.models.segmentation.deeplabv3.ASPPPooling', class {});
  4485. this.registerType('torchvision.models.segmentation.deeplabv3.DeepLabHead', class {});
  4486. this.registerType('torchvision.models.segmentation.deeplabv3.DeepLabV3', class {});
  4487. this.registerType('torchvision.models.segmentation.fcn.FCN', class {});
  4488. this.registerType('torchvision.models.segmentation.fcn.FCNHead', class {});
  4489. this.registerType('torchvision.models.shufflenetv2.ShuffleNetV2', class {});
  4490. this.registerType('torchvision.models.shufflenetv2.InvertedResidual', class {});
  4491. this.registerType('torchvision.models.squeezenet.Fire', class {});
  4492. this.registerType('torchvision.models.squeezenet.SqueezeNet', class {});
  4493. this.registerType('torchvision.models.resnet.ResNet', class {});
  4494. this.registerType('torchvision.models.vgg.VGG', class {});
  4495. this.registerType('torchvision.models.video.resnet.BasicBlock', class {});
  4496. this.registerType('torchvision.models.video.resnet.BasicStem', class {});
  4497. this.registerType('torchvision.models.video.resnet.Conv2Plus1D', class {});
  4498. this.registerType('torchvision.models.video.resnet.Conv3DNoTemporal', class {});
  4499. this.registerType('torchvision.models.video.resnet.Conv3DSimple', class {});
  4500. this.registerType('torchvision.models.video.resnet.R2Plus1dStem', class {});
  4501. this.registerType('torchvision.models.video.resnet.VideoResNet', class {});
  4502. this.registerType('torchvision.models.vision_transformer.Encoder', class extends torch.nn.modules.module.Module {});
  4503. this.registerType('torchvision.models.vision_transformer.EncoderBlock', class extends torch.nn.modules.module.Module {});
  4504. this.registerType('torchvision.models.vision_transformer.MLPBlock', class extends torchvision.ops.misc.MLP {});
  4505. this.registerType('torchvision.models.vision_transformer.VisionTransformer', class extends torch.nn.modules.module.Module {});
  4506. this.registerType('torchvision.models._utils.IntermediateLayerGetter', class {});
  4507. this.registerType('torchvision.transforms.functional.InterpolationMode', class {});
  4508. this.registerType('torchvision.transforms.transforms.ColorJitter', class extends torch.nn.modules.module.Module {});
  4509. this.registerType('torchvision.transforms.transforms.Compose', class {});
  4510. this.registerType('torchvision.transforms.transforms.CenterCrop', class extends torch.nn.modules.module.Module {});
  4511. this.registerType('torchvision.transforms.transforms.Grayscale', class extends torch.nn.modules.module.Module {});
  4512. this.registerType('torchvision.transforms.transforms.Lambda', class {});
  4513. this.registerType('torchvision.transforms.transforms.Normalize', class extends torch.nn.modules.module.Module {});
  4514. this.registerType('torchvision.transforms.transforms.RandomAffine', class extends torch.nn.modules.module.Module {});
  4515. this.registerType('torchvision.transforms.transforms.RandomCrop', class extends torch.nn.modules.module.Module {});
  4516. this.registerType('torchvision.transforms.transforms.RandomHorizontalFlip', class extends torch.nn.modules.module.Module {});
  4517. this.registerType('torchvision.transforms.transforms.RandomVerticalFlip', class extends torch.nn.modules.module.Module {});
  4518. this.registerType('torchvision.transforms.transforms.RandomResizedCrop', class extends torch.nn.modules.module.Module {});
  4519. this.registerType('torchvision.transforms.transforms.RandomRotation', class extends torch.nn.modules.module.Module {});
  4520. this.registerType('torchvision.transforms.transforms.Resize', class extends torch.nn.modules.module.Module {});
  4521. this.registerType('torchvision.transforms.transforms.Scale', class extends torch.nn.modules.module.Module {});
  4522. this.registerType('torchvision.transforms.transforms.ToPILImage', class {});
  4523. this.registerType('torchvision.transforms.transforms.ToTensor', class {});
  4524. this.registerFunction('torchvision.models.resnet.resnet18', function() {});
  4525. this.registerFunction('torchvision.models.resnet.resnet34', function() {});
  4526. this.registerFunction('torchvision.models.resnet.resnet50', function() {});
  4527. this.registerFunction('torchvision.models.resnet.resnet101', function() {});
  4528. this.registerFunction('torchvision.models.resnet.resnet152', function() {});
  4529. this.registerFunction('torchvision.models.vision_transformer.vit_h_14', function() {});
  4530. this.registerFunction('torchvision.ops.boxes.box_iou', function (/* boxes1, boxes2 */) {
  4531. throw new python.Error("'torchvision.ops.boxes.box_iou' not implemented.");
  4532. });
  4533. this.registerFunction('builtins.annotate', function(type, value) {
  4534. if (type === self._builtins.int) {
  4535. return Number.isInteger(value) ? value : NaN;
  4536. }
  4537. if (type === self._builtins.float) {
  4538. return typeof value === 'number' ? value : NaN;
  4539. }
  4540. if (type === self._builtins.number) {
  4541. // if (pytorch.Utility.isTensor(value)) {
  4542. // value.resize_([]);
  4543. // }
  4544. }
  4545. return value;
  4546. });
  4547. this.registerFunction('builtins.unchecked_cast', function(type, value) {
  4548. return value;
  4549. });
  4550. this.registerFunction('builtins.uninitialized', function(/* type */) {
  4551. return undefined;
  4552. });
  4553. this.registerFunction('ops.prim.data', function(tensor) {
  4554. return tensor;
  4555. });
  4556. this.registerFunction('ops.prim.device', function(tensor) {
  4557. return tensor.device;
  4558. });
  4559. this.registerFunction('ops.prim.dtype', function(tensor) {
  4560. return tensor.dtype.scalar_type();
  4561. });
  4562. this.registerFunction('ops.prim.is_quantized', function(tensor) {
  4563. return tensor.is_quantized;
  4564. });
  4565. this.registerFunction('ops.prim.is_cuda', function(/* tensor */) {
  4566. return false;
  4567. });
  4568. this.registerFunction('ops.prim.is_nested', function(tensor) {
  4569. return tensor.is_nested;
  4570. });
  4571. this.registerFunction('ops.prim.is_sparse', function(tensor) {
  4572. return tensor.is_sparse;
  4573. });
  4574. this.registerFunction('ops.prim.unchecked_unwrap_optional', function(value) {
  4575. return value;
  4576. });
  4577. this.registerFunction('ops.prim.NumToTensor', function(value) {
  4578. const tensor = self.invoke('torch.Tensor', []);
  4579. tensor.value = value; // TODO
  4580. return tensor;
  4581. });
  4582. this.registerFunction('ops.prim.min', function(value) {
  4583. if (Array.isArray(value)) {
  4584. return Math.min.apply(null, value);
  4585. }
  4586. return Math.min.apply(null, arguments);
  4587. });
  4588. this.registerFunction('ops.prim.max', function(value) {
  4589. if (Array.isArray(value)) {
  4590. return Math.max.apply(null, value);
  4591. }
  4592. return Math.max.apply(null, arguments);
  4593. });
  4594. this.registerFunction('ops.prim.shape', function(tensor) {
  4595. return tensor && tensor.size ? tensor.size() : undefined;
  4596. });
  4597. this.registerFunction('ops.quantized.conv_prepack', function(weight, bias, stride, padding, dilation, groups) {
  4598. const params = self.invoke('__torch__.torch.classes.quantized.Conv2dPackedParamsBase', []);
  4599. params.weight = weight;
  4600. params.bias = bias;
  4601. params.stride = stride;
  4602. params.padding =padding;
  4603. params.dilation = dilation;
  4604. params.groups = groups;
  4605. return params;
  4606. });
  4607. this.registerFunction('ops.quantized.conv1d_prepack', function(weight, bias, stride, padding, dilation, groups) {
  4608. const params = self.invoke('__torch__.torch.classes.quantized.Conv2dPackedParamsBase', []);
  4609. params.weight = weight;
  4610. params.bias = bias;
  4611. params.stride = stride;
  4612. params.padding =padding;
  4613. params.dilation = dilation;
  4614. params.groups = groups;
  4615. return params;
  4616. });
  4617. this.registerFunction('ops.quantized.conv2d_prepack', function(weight, bias, stride, padding, dilation, groups) {
  4618. const params = self.invoke('__torch__.torch.classes.quantized.Conv2dPackedParamsBase', []);
  4619. params.weight = weight;
  4620. params.bias = bias;
  4621. params.stride = stride;
  4622. params.padding =padding;
  4623. params.dilation = dilation;
  4624. params.groups = groups;
  4625. return params;
  4626. });
  4627. this.registerFunction('ops.quantized.conv3d_prepack', function(weight, bias, stride, padding, dilation, groups) {
  4628. const params = self.invoke('__torch__.torch.classes.quantized.Conv3dPackedParamsBase', []);
  4629. params.weight = weight;
  4630. params.bias = bias;
  4631. params.stride = stride;
  4632. params.padding =padding;
  4633. params.dilation = dilation;
  4634. params.groups = groups;
  4635. return params;
  4636. });
  4637. this.registerFunction('ops.quantized.conv_transpose1d_prepack', function(weight, bias, stride, padding, output_padding, dilation, groups) {
  4638. const params = self.invoke('__torch__.torch.classes.quantized.Conv2dPackedParamsBase', []);
  4639. params.weight = weight;
  4640. params.bias = bias;
  4641. params.stride = stride;
  4642. params.padding =padding;
  4643. params.output_padding = output_padding;
  4644. params.dilation = dilation;
  4645. params.groups = groups;
  4646. return params;
  4647. });
  4648. this.registerFunction('ops.quantized.conv_transpose2d_prepack', function(weight, bias, stride, padding, output_padding, dilation, groups) {
  4649. const params = self.invoke('__torch__.torch.classes.quantized.Conv2dPackedParamsBase', []);
  4650. params.weight = weight;
  4651. params.bias = bias;
  4652. params.stride = stride;
  4653. params.padding =padding;
  4654. params.output_padding = output_padding;
  4655. params.dilation = dilation;
  4656. params.groups = groups;
  4657. return params;
  4658. });
  4659. this.registerFunction('ops.quantized.linear_prepack', function(weight, bias) {
  4660. const params = self.invoke('__torch__.torch.classes.quantized.LinearPackedParamsBase', []);
  4661. params.weight = weight;
  4662. params.bias = bias;
  4663. return params;
  4664. });
  4665. this.registerFunction('ops.prim.RaiseException', function(message) {
  4666. throw new python.Error(message);
  4667. });
  4668. this.registerFunction('builtins.range', function(start, stop, step) {
  4669. if (stop === undefined && step === undefined) {
  4670. if (Number.isInteger(start)) {
  4671. return Array(start).keys();
  4672. }
  4673. if (isNaN(start)) {
  4674. return [];
  4675. }
  4676. }
  4677. throw new python.Error(`Unsupported range(${JSON.stringify(start)}, ${JSON.stringify(stop)}, ${JSON.stringify(step)})`);
  4678. });
  4679. this.registerFunction('torch._C._nn.gelu', function() {
  4680. throw new python.Error("'torch._C._nn.gelu' not implemented.");
  4681. });
  4682. this.registerFunction('torch._utils._rebuild_sparse_tensor', function(layout, data) {
  4683. if (layout === torch.sparse_coo) {
  4684. return self.invoke('torch._sparse_coo_tensor_unsafe', data);
  4685. }
  4686. throw new python.Error(`Unsupported sparse tensor layout '${layout ? layout.__str__() : ''}'.`);
  4687. });
  4688. this.registerFunction('torch.from_numpy', function(obj) {
  4689. const dtypes = new Map([
  4690. [ '<f2', torch.float16 ],
  4691. [ '<f4', torch.float32 ],
  4692. [ '<f8', torch.float64 ],
  4693. [ '<i2', torch.int16 ],
  4694. [ '<i4', torch.int32 ],
  4695. [ '<i8', torch.int64 ],
  4696. ]);
  4697. if (!dtypes.has(obj.dtype.str)) {
  4698. throw new python.Error(`Unsupported numpy.ndarray type '${obj.dtype.str}'.`);
  4699. }
  4700. const dtype = dtypes.get(obj.dtype.str);
  4701. const strides = obj.strides.map((stride) => stride / obj.itemsize);
  4702. const storage = execution.invoke('torch.storage._TypedStorage', [ obj.size, dtype ]);
  4703. storage._set_cdata(obj.data);
  4704. const tensor = execution.invoke('torch.Tensor', []);
  4705. tensor.__setstate__([ storage, 0, obj.shape, strides ]);
  4706. return tensor;
  4707. });
  4708. this.registerFunction('torch._utils._rebuild_device_tensor_from_numpy', function(data, dtype, device, requires_grad) {
  4709. const tensor = execution.invoke('torch.from_numpy', [ data ]);
  4710. // tensor = tensor.to(dtype, device)
  4711. tensor.requires_grad = requires_grad;
  4712. return tensor;
  4713. });
  4714. this.registerFunction('torch._sparse_coo_tensor_unsafe', function(indices, values, size) {
  4715. const tensor = self.invoke('torch.Tensor', []);
  4716. tensor._layout = torch.sparse_coo;
  4717. tensor._indices = indices;
  4718. tensor._values = values;
  4719. tensor._shape = size;
  4720. return tensor;
  4721. });
  4722. this.registerFunction('torch._utils._rebuild_tensor', function (storage, storage_offset, size, stride) {
  4723. if (Array.isArray(storage) && storage.length === 5 && storage[0] === 'storage') {
  4724. const [, storage_type, , ,size] = storage;
  4725. storage = new storage_type(size);
  4726. }
  4727. const name = `${storage.__class__.__module__}.${storage.__class__.__name__.replace('Storage', 'Tensor')}`;
  4728. const tensor = self.invoke(name, []);
  4729. tensor.__setstate__([ storage, storage_offset, size, stride ]);
  4730. return tensor;
  4731. });
  4732. this.registerFunction('torch._utils._rebuild_tensor_v2', function (storage, storage_offset, size, stride, requires_grad, backward_hooks) {
  4733. const tensor = execution.invoke('torch._utils._rebuild_tensor', [ storage, storage_offset, size, stride ]);
  4734. tensor.requires_grad = requires_grad;
  4735. tensor.backward_hooks = backward_hooks;
  4736. return tensor;
  4737. });
  4738. this.registerFunction('torch._utils._rebuild_parameter', function(data, requires_grad, backward_hooks) {
  4739. const param = self.invoke('torch.nn.parameter.Parameter', [ data, requires_grad ]);
  4740. param.backward_hooks = backward_hooks;
  4741. return param;
  4742. });
  4743. this.registerFunction('torch._utils._rebuild_parameter_with_state', function(data, requires_grad, backward_hooks, state) {
  4744. const _set_obj_state = (obj, state) => {
  4745. const [dict_state, slots_state] = Array.isArray(state) ? state : [state, null];
  4746. if (dict_state) {
  4747. for (const [k, v] of Object.entries(dict_state)) {
  4748. self.invoke('builtins.setattr', [ obj, k, v ]);
  4749. }
  4750. }
  4751. if (slots_state) {
  4752. for (const [k, v] of Object.entries(slots_state)) {
  4753. self.invoke('builtins.setattr', [ obj, k, v ]);
  4754. }
  4755. }
  4756. };
  4757. const param = self.invoke('torch.nn.parameter.Parameter', [ data, requires_grad ]);
  4758. param._backward_hooks = backward_hooks;
  4759. _set_obj_state(param, state);
  4760. return param;
  4761. });
  4762. this.registerFunction('torch._utils._rebuild_qtensor', function(storage, storage_offset, size, stride, quantizer_params, requires_grad, backward_hooks) {
  4763. const tensor = execution.invoke('torch._utils._rebuild_tensor_v2', [ storage, storage_offset, size, stride, requires_grad, backward_hooks ]);
  4764. tensor.quantizer_params = quantizer_params;
  4765. return tensor;
  4766. });
  4767. this.registerFunction('torch._utils._set_obj_state', function(obj, state) {
  4768. let dict_state = state;
  4769. let slots_state = null;
  4770. if (state instanceof self.builtins.tuple) {
  4771. if (state.length != 2) {
  4772. throw new python.Error(`Invalid serialized state: '${state}'.`);
  4773. }
  4774. [dict_state, slots_state] = state;
  4775. }
  4776. if (dict_state) {
  4777. for (const [name, value] of Object.entries(dict_state)) {
  4778. execution.invoke('builtins.setattr', [ obj, name, value ]);
  4779. }
  4780. }
  4781. if (slots_state) {
  4782. for (const [name, value] of Object.entries(slots_state)) {
  4783. execution.invoke('builtins.setattr', [ obj, name, value ]);
  4784. }
  4785. }
  4786. return obj;
  4787. });
  4788. this.registerFunction('torch._set_item', function(dict, key, value) {
  4789. dict[key] = value;
  4790. });
  4791. this.registerFunction('torch._tensor._rebuild_from_type_v2', function(func, new_type, args, state) {
  4792. let ret = func.apply(null, args);
  4793. if (ret.__class__ !== new_type) {
  4794. // ret = ret.as_subclass(new_type);
  4795. }
  4796. const setstate = execution.invoke('builtins.getattr', [ ret.__class__, '__setstate__', torch.Tensor.__setstate__ ]);
  4797. if (setstate !== torch.Tensor.__setstate__) {
  4798. ret.__setstate__(state);
  4799. } else {
  4800. ret = execution.invoke('torch._utils._set_obj_state', [ ret, state ]);
  4801. }
  4802. return ret;
  4803. });
  4804. this.registerFunction('torch.__and__', function(left, right) {
  4805. return left && right;
  4806. });
  4807. this.registerFunction('torch.__contains__', function(dict, key) {
  4808. return dict[key] !== undefined;
  4809. });
  4810. this.registerFunction('torch.__derive_index', function(index, start, step) {
  4811. return start + index * step;
  4812. });
  4813. this.registerFunction('torch.__is__', function(left, right) {
  4814. if (left === null && right === null) {
  4815. return true;
  4816. }
  4817. if ((left !== null && right === null) || (left === null && right !== null)) {
  4818. return false;
  4819. }
  4820. throw new python.Error("Unsupported 'torch.__is__' expression type.");
  4821. });
  4822. this.registerFunction('torch.__isnot__', function(left, right) {
  4823. if (left === null && right === null) {
  4824. return false;
  4825. }
  4826. if ((left !== null && right === null) || (left === null && right !== null)) {
  4827. return true;
  4828. }
  4829. throw new python.Error("Unsupported 'torch.__isnot__' expression type.");
  4830. });
  4831. this.registerFunction('torch.__not__', function(value) {
  4832. if (typeof value === 'boolean') {
  4833. return !value;
  4834. }
  4835. throw new python.Error("Unsupported 'torch.__not__' expression type.");
  4836. });
  4837. this.registerFunction('torch.__range_length', function(lo, hi, step) {
  4838. if (step === 0) {
  4839. throw new python.Error('range() arg 3 must not be zero');
  4840. }
  4841. if (step > 0 && lo < hi) {
  4842. return 1 + (hi - 1 - lo) / step;
  4843. } else if (step < 0 && lo > hi) {
  4844. return 1 + (lo - 1 - hi) / (0 - step);
  4845. }
  4846. return 0;
  4847. });
  4848. this.registerFunction('torch._unwrap_optional', function(value) {
  4849. return value; // TODO
  4850. });
  4851. this.registerFunction('torch.empty_strided', function(/* size, stride, dtype, layout, device, pin_memory, requires_grad */) {
  4852. return null;
  4853. // TODO throw new python.Error("'torch.empty_strided' not implemented.");
  4854. });
  4855. this.registerFunction('torch.add', function(left, right) {
  4856. if (typeof left === 'number' && typeof right === 'number') {
  4857. return left * right;
  4858. }
  4859. if (Array.isArray(left) && Array.isArray(right)) {
  4860. return left.concat(right);
  4861. }
  4862. if (typeof left === 'string' && typeof right === 'string') {
  4863. return left + right;
  4864. }
  4865. throw new python.Error('Unsupported torch.add expression type.');
  4866. });
  4867. this.registerFunction('torch.append', function(list, value) {
  4868. list.push(value);
  4869. return value;
  4870. });
  4871. this.registerFunction('torch.extend', function(list, value) {
  4872. list.push(...value);
  4873. });
  4874. this.registerFunction('torch.insert', function(list, index, value) {
  4875. list.splice(index, 0, value);
  4876. return value;
  4877. });
  4878. this.registerFunction('torch.clear', function(value) {
  4879. if (Object(value) === value) {
  4880. for (const key of Object.keys(value)) {
  4881. delete value[key];
  4882. }
  4883. }
  4884. });
  4885. this.registerFunction('torch.replace', function(value) {
  4886. return value;
  4887. });
  4888. this.registerFunction('torch.dict', function(args) {
  4889. const obj = {};
  4890. if (args) {
  4891. if (Array.isArray(args)) {
  4892. for (const [key, value] of args) {
  4893. obj[key] = value;
  4894. }
  4895. } else {
  4896. throw new python.Error("'torch.dict' arguments not supported.");
  4897. }
  4898. }
  4899. return obj;
  4900. });
  4901. this.registerFunction('torch.dim', function(tensor) {
  4902. if (tensor && tensor.size) {
  4903. const size = tensor.size();
  4904. if (size) {
  4905. return size.length;
  4906. }
  4907. }
  4908. return NaN; // TODO
  4909. });
  4910. this.registerFunction('torch.numel', function(tensor) {
  4911. if (tensor && tensor.size) {
  4912. const size = tensor.size();
  4913. if (size) {
  4914. return size.reduce((a, b) => a * b, 1);
  4915. }
  4916. }
  4917. return NaN;
  4918. });
  4919. this.registerFunction('torch.eq', function(left, right) {
  4920. if (typeof left === 'string' && typeof right === 'string') {
  4921. return left === right;
  4922. }
  4923. if (typeof left === 'number' && typeof right === 'number') {
  4924. if (isNaN(left) && isNaN(right)) {
  4925. return true;
  4926. }
  4927. return left === right;
  4928. }
  4929. if (left === undefined || right === undefined) {
  4930. return true;
  4931. }
  4932. if (Array.isArray(left) && Array.isArray(right)) {
  4933. return left.length === right.length && left.every((item, index) => item === right[index]);
  4934. }
  4935. throw new python.Error("Unsupported 'torch.eq' expression type.");
  4936. });
  4937. this.registerFunction('torch.floor', function(value) {
  4938. return Math.floor(value);
  4939. });
  4940. this.registerFunction('torch.ceil', function(value) {
  4941. return Math.ceil(value);
  4942. });
  4943. this.registerFunction('torch.floordiv', function(left, right) {
  4944. return Math.floor(left / right);
  4945. });
  4946. this.registerFunction('torch.format', function() {
  4947. const args = Array.from(arguments);
  4948. const list = args.shift().split(/({}D?)/);
  4949. return list.map((text) => {
  4950. if (text === '{}' || text === '{}D') {
  4951. const arg = args.shift();
  4952. return Array.isArray(arg) ? `[${arg.map((item) => item.toString()).join(', ')}]` : arg ? arg.toString() : '?';
  4953. }
  4954. return text;
  4955. }).join('');
  4956. });
  4957. this.registerFunction('torch.gt', function(left, right) {
  4958. if (typeof left === 'number' && typeof right === 'number') {
  4959. if (!isNaN(left) && !isNaN(right)) {
  4960. return left > right;
  4961. }
  4962. }
  4963. if (isNaN(left) && !isNaN(right)) {
  4964. return true;
  4965. }
  4966. throw new python.Error("Unsupported 'torch.gt' expression type.");
  4967. });
  4968. this.registerFunction('torch.ge', function(left, right) {
  4969. if (typeof left === 'number' && typeof right === 'number') {
  4970. if (!isNaN(left) && !isNaN(right)) {
  4971. return left > right;
  4972. }
  4973. }
  4974. if (isNaN(left) && !isNaN(right)) {
  4975. return true;
  4976. }
  4977. throw new python.Error("Unsupported 'torch.ge' expression type.");
  4978. });
  4979. this.registerFunction('torch.is_floating_point', function(tensor) {
  4980. const type = tensor.dtype.scalar_type();
  4981. return (type === 5 || type === 6 || type === 7);
  4982. });
  4983. this.registerFunction('torch.is_grad_enabled', function() {
  4984. return false;
  4985. });
  4986. this.registerFunction('torch.set_grad_enabled', function(/* value */) {
  4987. });
  4988. this.registerFunction('torch.serialization._get_layout', function(name) {
  4989. const value = name.startsWith('torch.') ? torch[name.split('.')[1]] : null;
  4990. return value instanceof torch.layout ? value : null;
  4991. });
  4992. this.registerFunction('torch.storage._load_from_bytes', function(b) {
  4993. return torch.load(b);
  4994. });
  4995. this.registerFunction('torch.jit._pickle.build_boollist', function(data) {
  4996. return data;
  4997. });
  4998. this.registerFunction('torch.jit._pickle.build_doublelist', function(data) {
  4999. return data;
  5000. });
  5001. this.registerFunction('torch.jit._pickle.build_intlist', function(data) {
  5002. return data;
  5003. });
  5004. this.registerFunction('torch.jit._pickle.build_tensorlist', function(data) {
  5005. return data;
  5006. });
  5007. this.registerFunction('torch.jit._pickle.build_tensor_from_id', function(data) {
  5008. return self.builtins.CONSTANTS[`c${data}`];
  5009. });
  5010. this.registerFunction('torch.jit._pickle.restore_type_tag', function(value /*, type_str */) {
  5011. return value;
  5012. });
  5013. this.registerFunction('torch.keys', function(dict) {
  5014. return Object.keys(dict);
  5015. });
  5016. this.registerFunction('torch.len', function(value) {
  5017. if (Array.isArray(value)) {
  5018. return value.length;
  5019. }
  5020. if (value && value.shape && value.__len__) {
  5021. return value.__len__();
  5022. }
  5023. return NaN;
  5024. });
  5025. this.registerFunction('torch.le', function(left, right) {
  5026. if (typeof left === 'number' && typeof right === 'number') {
  5027. if (isNaN(left) || isNaN(right)) {
  5028. return false;
  5029. }
  5030. return left <= right;
  5031. }
  5032. if (left === undefined || right === undefined) {
  5033. return true;
  5034. }
  5035. throw new python.Error("Unsupported 'torch.le' expression type.");
  5036. });
  5037. this.registerFunction('torch.list', function(args) {
  5038. return args;
  5039. });
  5040. this.registerFunction('torch.list_with_default', function(size /*, defaults */) {
  5041. return size;
  5042. });
  5043. this.registerType('torch.PyTorchFileReader', class {
  5044. constructor(entries) {
  5045. let prefix = 0;
  5046. const paths = Array.from(entries.keys()).map((path) => path.replace(/\\/g, '/').split('/').reverse());
  5047. for (let set = new Set(); set && paths.length > 0;) {
  5048. set = new Set(paths.map((path) => path.length > 1 ? path.pop() : null));
  5049. set = set.size > 1 || set.keys().next().value === null ? null : set;
  5050. prefix += set ? set.keys().next().value.length + 1 : 0;
  5051. }
  5052. this._records = new Map(Array.from(entries).map(([name, value]) => [ name.substring(prefix), value ]));
  5053. this._version = '0';
  5054. const stream = this.get_record('.data/version') || this.get_record('version') || null;
  5055. if (stream) {
  5056. const decoder = new TextDecoder('utf-8');
  5057. const buffer = stream.peek();
  5058. const text = decoder.decode(buffer);
  5059. this._version = text.split('\n').shift().trim();
  5060. }
  5061. }
  5062. has_record(name) {
  5063. return this._records.has(name);
  5064. }
  5065. get_record(name) {
  5066. return this._records.get(name);
  5067. }
  5068. get_all_records() {
  5069. return Array.from(this._records.keys());
  5070. }
  5071. version() {
  5072. return this._version;
  5073. }
  5074. });
  5075. this.registerFunction('torch.load', function(f) {
  5076. const legacy_load = (entries) => {
  5077. const deserialized_objects = {};
  5078. if (entries.has('storages')) {
  5079. const data = entries.get('storages');
  5080. const unpickler = execution.invoke('pickle.Unpickler', [ data ]);
  5081. const num_storages = unpickler.load();
  5082. for (let i = 0; i < num_storages; i++) {
  5083. const args = unpickler.load();
  5084. const [key, , storage_type] = args;
  5085. const obj = storage_type._new_with_file(unpickler);
  5086. deserialized_objects[key] = obj;
  5087. }
  5088. /*
  5089. let storage_views = unpickler.load();
  5090. for target_cdata, root_cdata, offset, size in storage_views:
  5091. root = deserialized_objects[root_cdata]
  5092. deserialized_objects[target_cdata] = root[offset:offset + size]
  5093. */
  5094. }
  5095. if (entries.has('tensors')) {
  5096. const data = entries.get('tensors');
  5097. const unpickler = execution.invoke('pickle.Unpickler', [ data ]);
  5098. const num_tensors = unpickler.load();
  5099. const int32 = (unpickler) => {
  5100. const buffer = unpickler.read(4);
  5101. return buffer[0] + (buffer[1] << 8) + (buffer[2] << 16) + (buffer[3] << 24);
  5102. };
  5103. const int64 = (unpickler) => {
  5104. const buffer = unpickler.read(8);
  5105. if (buffer[6] !== 0 && buffer[7] !== 0) {
  5106. throw new python.Error('Unsigned 64-bit value exceeds 32-bit range.');
  5107. }
  5108. return buffer[0] + (buffer[1] << 8) + (buffer[2] << 16) + (buffer[3] << 24) + (buffer[4] * 4294967296) + (buffer[5] * 1099511627776);
  5109. };
  5110. for (let i = 0; i < num_tensors; i++) {
  5111. const args = unpickler.load();
  5112. const [key, storage_id] = args;
  5113. const storage = deserialized_objects[storage_id];
  5114. const ndim = int32(unpickler);
  5115. unpickler.read(4);
  5116. const shape = Array.from(new Array(ndim)).map(() => int64(unpickler));
  5117. const stride = Array.from(new Array(ndim)).map(() => int64(unpickler));
  5118. const storage_offset = int64(unpickler);
  5119. const tensor = execution.invoke('torch._utils._rebuild_tensor', [ storage, storage_offset, shape, stride ]);
  5120. deserialized_objects[key] = tensor;
  5121. }
  5122. }
  5123. const data = entries.get('pickle');
  5124. const unpickler = execution.invoke('pickle.Unpickler', [ data ]);
  5125. unpickler.persistent_load = (saved_id) => deserialized_objects[saved_id];
  5126. return unpickler.load();
  5127. };
  5128. const _legacy_load = () => {
  5129. const unpickler = execution.invoke('pickle.Unpickler', [ f ]);
  5130. unpickler.load(); // magic_number
  5131. const protocol_version = unpickler.load();
  5132. if (protocol_version != 1001) {
  5133. throw new python.Error(`Unsupported protocol version '${protocol_version}'.`);
  5134. }
  5135. const sys_info = unpickler.load();
  5136. if (sys_info.protocol_version != 1001) {
  5137. throw new python.Error(`Unsupported protocol version '${sys_info.protocol_version}'.`);
  5138. }
  5139. if (sys_info.little_endian === false) {
  5140. throw new python.Error("Unsupported big-endian storage data.");
  5141. }
  5142. const module_source_map = new Map();
  5143. const deserialized_objects = new Map();
  5144. unpickler.persistent_load = (saved_id) => {
  5145. switch (saved_id[0]) {
  5146. case 'module': {
  5147. const [, module, ,source] = saved_id;
  5148. module_source_map.set(module, source);
  5149. return saved_id[1];
  5150. }
  5151. case 'storage': {
  5152. const [, storage_type, key, , size, view_metadata] = saved_id;
  5153. if (!deserialized_objects.has(key)) {
  5154. const obj = new storage_type(size);
  5155. deserialized_objects.set(key, obj);
  5156. }
  5157. if (view_metadata) {
  5158. const view_key = view_metadata.shift();
  5159. view_metadata.shift(); // view_offset
  5160. view_metadata.shift(); // view_size
  5161. if (!deserialized_objects.has(view_key)) {
  5162. const view = null; // storage.slice(view_offset, view_offset + view_size);
  5163. deserialized_objects.set(view_key, view);
  5164. }
  5165. return deserialized_objects.get(view_key);
  5166. }
  5167. return deserialized_objects.get(key);
  5168. }
  5169. default: {
  5170. throw new python.Error(`Unsupported persistent load type '${saved_id[0]}'.`);
  5171. }
  5172. }
  5173. };
  5174. const obj = unpickler.load();
  5175. const deserialized_storage_keys = unpickler.load();
  5176. for (const deserialized_storage_key of deserialized_storage_keys) {
  5177. const storage = deserialized_objects.get(deserialized_storage_key);
  5178. storage._set_from_file(unpickler);
  5179. }
  5180. if (!obj) {
  5181. throw new python.Error('File format is not PyTorch.');
  5182. }
  5183. if (obj === 'None') {
  5184. throw new python.Error("File contains 'None' root object.");
  5185. }
  5186. return obj;
  5187. };
  5188. const _load = (entries) => {
  5189. if (f.has('constant.pkl')) {
  5190. throw python.Error("TorchScript 'torch.load' not supported.");
  5191. }
  5192. const loaded_storages = new Map();
  5193. const persistent_load = (saved_id) => {
  5194. switch (saved_id[0]) {
  5195. case 'storage': {
  5196. const [, storage_type, key, , numel] = saved_id;
  5197. if (!loaded_storages.has(key)) {
  5198. const storage = new storage_type(numel);
  5199. const name = `data/${key}`;
  5200. const stream = entries.get(name);
  5201. storage._set_cdata(stream);
  5202. loaded_storages.set(key, storage);
  5203. }
  5204. return loaded_storages.get(key);
  5205. }
  5206. default: {
  5207. throw new python.Error(`Unsupported persistent load type '${saved_id[0]}'.`);
  5208. }
  5209. }
  5210. };
  5211. const data_file = entries.get('data.pkl');
  5212. const unpickler = execution.invoke('pickle.Unpickler', [ data_file ]);
  5213. unpickler.persistent_load = persistent_load;
  5214. const result = unpickler.load();
  5215. return result;
  5216. };
  5217. if (f instanceof Map) {
  5218. const reader = new torch.PyTorchFileReader(f);
  5219. const records = reader.get_all_records().map((name) => [ name, reader.get_record(name) ]);
  5220. f = new Map(records);
  5221. if (f.has('pickle')) {
  5222. return legacy_load(f);
  5223. }
  5224. if (f.has('data.pkl')) {
  5225. return _load(f);
  5226. }
  5227. throw new python.Error(`Unsupported 'torch.load' input '${JSON.stringify(Array.from(f.keys()))}'.`);
  5228. }
  5229. return _legacy_load(f);
  5230. });
  5231. this.registerFunction('torch.log10', function(/* x */) {
  5232. throw new python.Error("'torch.log10' not implemented.");
  5233. });
  5234. this.registerFunction('torch.lt', function(left, right) {
  5235. if (typeof left === 'number' && typeof right === 'number') {
  5236. return left < right;
  5237. }
  5238. throw new python.Error("Unsupported 'torch.lt' expression type.");
  5239. });
  5240. this.registerFunction('torch.mul', function(left, right) {
  5241. if (typeof left === 'number' && typeof right === 'number') {
  5242. return left * right;
  5243. }
  5244. if (isNaN(left) || isNaN(right)) {
  5245. return NaN;
  5246. }
  5247. if (Array.isArray(left) && left.every((value) => typeof value === 'number') && typeof right === 'number') {
  5248. return left.map((value) => value * right);
  5249. }
  5250. throw new python.Error("Unsupported 'torch.mul' expression type.");
  5251. });
  5252. this.registerFunction('torch.div', function(left, right) {
  5253. if (typeof left === 'number' && typeof right === 'number') {
  5254. return left / right;
  5255. }
  5256. if (isNaN(left) || isNaN(right)) {
  5257. return NaN;
  5258. }
  5259. throw new python.Error("Unsupported 'torch.div' expression type.");
  5260. });
  5261. this.registerFunction('torch.round', function(value) {
  5262. if (typeof value === 'number') {
  5263. return Math.round(value);
  5264. }
  5265. if (isNaN(value)) {
  5266. return value;
  5267. }
  5268. throw new python.Error("Unsupported 'torch.round' expression type.");
  5269. });
  5270. this.registerFunction('torch.remainder', function(left, right) {
  5271. if (typeof left === 'number' && typeof right === 'number') {
  5272. return left % right;
  5273. }
  5274. if (isNaN(left) || isNaN(right)) {
  5275. return NaN;
  5276. }
  5277. throw new python.Error("Unsupported 'torch.remainder' expression type.");
  5278. });
  5279. this.registerFunction('torch.ne', function(left, right) {
  5280. if (typeof left === 'boolean' && typeof right === 'boolean') {
  5281. return left !== right;
  5282. }
  5283. if (typeof left === 'number' && typeof right === 'number') {
  5284. if (isNaN(left) || isNaN(right)) {
  5285. return false;
  5286. }
  5287. return left !== right;
  5288. }
  5289. if (Array.isArray(left) && Array.isArray(right) && left.length === right.length) {
  5290. return false;
  5291. }
  5292. if (typeof left === 'string' && typeof right === 'string') {
  5293. return left !== right;
  5294. }
  5295. if (left === undefined || right === undefined) {
  5296. return true;
  5297. }
  5298. throw new python.Error("Unsupported 'torch.ne' expression type.");
  5299. });
  5300. this.registerFunction('torch.neg', function(value) {
  5301. if (typeof value === 'number') {
  5302. return -value;
  5303. }
  5304. throw new python.Error("Unsupported 'torch.neg' expression type.");
  5305. });
  5306. this.registerFunction('torch.pow', function(left, right) {
  5307. if (typeof left === 'number' && typeof right === 'number') {
  5308. return Math.pow(left, right);
  5309. }
  5310. throw new python.Error("Unsupported 'torch.pow' expression type.");
  5311. });
  5312. this.registerFunction('torch.q_scale', function(/* tensor */) {
  5313. return -1; // TODO
  5314. });
  5315. this.registerFunction('torch.t', function(tensor) {
  5316. return tensor;
  5317. });
  5318. this.registerFunction('torch.size', function(tensor, dim) {
  5319. if (tensor && tensor.size) {
  5320. const size = tensor.size();
  5321. if (Array.isArray(size)) {
  5322. if (dim === undefined) {
  5323. return size;
  5324. }
  5325. if (Number.isInteger(dim)) {
  5326. if (dim >= 0 && dim < size.length) {
  5327. return size[dim];
  5328. }
  5329. if (dim < 0 && -dim < size.length) {
  5330. return size[size.length + dim];
  5331. }
  5332. }
  5333. throw new python.Error(`Dimension out of range (expected to be in range of ${JSON.stringify(size)}, but got ${JSON.stringify(dim)}).`);
  5334. }
  5335. }
  5336. if (Number.isInteger(dim)) {
  5337. return NaN;
  5338. }
  5339. return [];
  5340. });
  5341. this.registerFunction('torch.sqrt', function(x) {
  5342. return Math.sqrt(x);
  5343. });
  5344. this.registerFunction('torch.slice', function(l, start, end, step) {
  5345. if (!Array.isArray(l)) {
  5346. throw new python.Error('Slicing expected array');
  5347. }
  5348. step = step || 1;
  5349. if (step !== 1) {
  5350. throw new python.Error('Slicing only supports step=1');
  5351. }
  5352. start = Math.max(0, start >= 0 ? start : l.length + start);
  5353. end = Math.min(l.length, end || Number.MAX_SAFE_INTEGER);
  5354. return l.slice(start, end);
  5355. });
  5356. this.registerFunction('torch.sub', function(left, right) {
  5357. if (typeof left === 'number' && typeof right === 'number') {
  5358. return left - right;
  5359. }
  5360. throw new python.Error("Unsupported 'torch.sub' expression type.");
  5361. });
  5362. this.registerFunction('torch.functional.einsum', function() {
  5363. throw new python.Error("'torch.functional.einsum' not implemented.");
  5364. });
  5365. this.registerFunction('torch.functional.split', function() {
  5366. throw new python.Error("'torch.functional.split' not implemented.");
  5367. });
  5368. this.registerFunction('torch.nn.functional.adaptive_avg_pool2d', function(/* input */) {
  5369. throw new python.Error("'torch.nn.functional.adaptive_avg_pool2d' not implemented.");
  5370. });
  5371. this.registerFunction('torch.nn.functional.cross_entropy', function() {
  5372. throw new python.Error("'torch.nn.functional.cross_entropy' not implemented.");
  5373. });
  5374. this.registerFunction('torch.nn.functional.elu', function(/* input */) {
  5375. throw new python.Error("'torch.nn.functional.elu' not implemented.");
  5376. });
  5377. this.registerFunction('torch.nn.functional.gelu', function(/* input */) {
  5378. throw new python.Error("'torch.nn.functional.gelu' not implemented.");
  5379. });
  5380. this.registerFunction('torch.nn.functional.hardsigmoid', function(/* input */) {
  5381. throw new python.Error("'torch.nn.functional.hardsigmoid' not implemented.");
  5382. });
  5383. this.registerFunction('torch.nn.functional.hardswish', function(/* input */) {
  5384. throw new python.Error("'torch.nn.functional.hardswish' not implemented.");
  5385. });
  5386. this.registerFunction('torch.nn.functional.hardtanh', function(/* input */) {
  5387. throw new python.Error("'torch.nn.functional.hardtanh' not implemented.");
  5388. });
  5389. this.registerFunction('torch.nn.functional.interpolate', function(/* input */) {
  5390. throw new python.Error("'torch.nn.functional.interpolate' not implemented.");
  5391. });
  5392. this.registerFunction('torch.nn.functional.leaky_relu', function(/* input */) {
  5393. throw new python.Error("'torch.nn.functional.leaky_relu' not implemented.");
  5394. });
  5395. this.registerFunction('torch.nn.functional.linear', function(/* input */) {
  5396. throw new python.Error("'torch.nn.functional.linear' not implemented.");
  5397. });
  5398. this.registerFunction('torch.nn.functional._max_pool2d', function(/* input */) {
  5399. throw new python.Error("'torch.nn.functional._max_pool2d' not implemented.");
  5400. });
  5401. this.registerFunction('torch.nn.functional.max_pool2d_with_indices', function(/* input */) {
  5402. throw new python.Error("'torch.nn.functional.max_pool2d_with_indices' not implemented.");
  5403. });
  5404. this.registerFunction('torch.nn.functional.mse_loss', function(/* input */) {
  5405. throw new python.Error("'torch.nn.functional.mse_loss' not implemented.");
  5406. });
  5407. this.registerFunction('torch.nn.functional.pad', function(/* input */) {
  5408. throw new python.Error("'torch.nn.functional.pad' not implemented.");
  5409. });
  5410. this.registerFunction('torch.nn.functional.relu', function(/* input */) {
  5411. throw new python.Error("'torch.nn.functional.relu' not implemented.");
  5412. });
  5413. this.registerFunction('torch.nn.functional.relu6', function(/* input */) {
  5414. throw new python.Error("'torch.nn.functional.relu6' not implemented.");
  5415. });
  5416. this.registerFunction('torch.nn.functional.sigmoid', function(/* input */) {
  5417. throw new python.Error("'torch.nn.functional.sigmoid' not implemented.");
  5418. });
  5419. this.registerFunction('torch.nn.functional.silu', function(/* input */) {
  5420. throw new python.Error("'torch.nn.functional.silu' not implemented.");
  5421. });
  5422. this.registerFunction('torch.nn.functional.softmax', function(/* input */) {
  5423. throw new python.Error("'torch.nn.functional.softmax' not implemented.");
  5424. });
  5425. this.registerFunction('torch.nn.functional.tanh', function(/* input */) {
  5426. throw new python.Error("'torch.nn.functional.tanh' not implemented.");
  5427. });
  5428. this.registerFunction('torch.values', function(dict) {
  5429. return Object.values(dict);
  5430. });
  5431. this.registerFunction('torch.warn', function() {
  5432. });
  5433. this.registerType('torch._ops.OperatorBase', class {});
  5434. this.registerType('torch._ops.HigherOrderOperator', class extends torch._ops.OperatorBase {});
  5435. this.registerType('torch._ops.OpOverload', class extends torch._ops.OperatorBase {});
  5436. this.registerType('torch.export.unflatten.UnflattenedModule', class extends torch.nn.modules.module.Module {
  5437. constructor(/* export_module, flat_args_adapter */) {
  5438. super();
  5439. }
  5440. });
  5441. this.registerType('torch.export.exported_program.ExportedProgram', class {
  5442. constructor(/* root, graph, graph_signature, state_dict, range_constraints, module_call_graph, example_inputs, verifier, tensor_constants */) {
  5443. }
  5444. });
  5445. this.registerFunction('torch.export.unflatten', function(/* module, flat_args_adapter */) {
  5446. throw new python.Error("'torch.export.unflatten' not implemented.");
  5447. });
  5448. this.registerFunction('torch._export.exported_program._create_graph_module_for_export', function(root, graph) {
  5449. return new torch.fx.graph_module.GraphModule(root, graph);
  5450. });
  5451. this.registerType('torch._export.serde.serialize.SerializedArtifact', class {
  5452. constructor(exported_program, state_dict, constants) {
  5453. this.exported_program = exported_program;
  5454. this.state_dict = state_dict;
  5455. this.constants = constants;
  5456. }
  5457. });
  5458. this.registerType('torch.fx.experimental.symbolic_shapes.ShapeEnv', class {
  5459. constructor() {
  5460. }
  5461. create_symintnode(/* sym, hint, source */) {
  5462. return new torch.SymInt();
  5463. }
  5464. });
  5465. this.registerFunction('torch.fx.graph_module._deserialize_graph_module', function(/* forward, body */) {
  5466. return execution.invoke('torch.fx.graph_module.GraphModule', []);
  5467. });
  5468. this.registerFunction('torch.fx.graph_module._forward_from_src', function(src, globals /*, co_fields */) {
  5469. globals = Object.assign({}, globals);
  5470. const context = new python.Execution.Context(globals, null);
  5471. execution.exec(src, context);
  5472. const forward_fn = globals.forward;
  5473. delete globals.forward;
  5474. return forward_fn;
  5475. });
  5476. this.registerFunction('torch.fx.graph_module.reduce_graph_module', function(body, import_block) {
  5477. // https://github.com/pytorch/pytorch/blob/master/torch/fx/graph_module.py
  5478. const fn_src = body._code || body.code;
  5479. const forward = execution.invoke('torch.fx.graph_module._forward_from_src', [ import_block + fn_src, {} ]);
  5480. return execution.invoke('torch.fx.graph_module._deserialize_graph_module', [ forward, body ]);
  5481. });
  5482. this.registerFunction('torch.fx.graph_module.reduce_package_graph_module', function(importer, body, generated_module_name) {
  5483. const forward = importer.import_module(generated_module_name).forward;
  5484. return execution.invoke('torch.fx.graph_module._deserialize_graph_module', [ forward, body ]);
  5485. });
  5486. this.registerType('torch.fx.graph.CodeGen', class {});
  5487. this.registerType('torch.fx.graph._Namespace', class {
  5488. constructor() {
  5489. this._obj_to_name = new Map();
  5490. this._unassociated_names = new Set();
  5491. this._used_names = new Set();
  5492. this._base_count = {};
  5493. }
  5494. create_name(candidate, obj) {
  5495. if (obj && this._obj_to_name.has(obj)) {
  5496. return self._obj_to_name.get(obj);
  5497. }
  5498. candidate = candidate || '_unnamed';
  5499. candidate = /^\d+$/.test(candidate) ? `_${candidate}` : candidate;
  5500. candidate = candidate.replace(/[^0-9a-zA-Z_]+/, '_');
  5501. const match = candidate.match(/(.*)_(\d+)$"/);
  5502. let base = candidate;
  5503. let num = null;
  5504. if (match) {
  5505. [, base] = match;
  5506. num = parseInt(match[2], 10);
  5507. }
  5508. candidate = num ? `${base}_${num}` : base;
  5509. if (!num) {
  5510. num = this._base_count[base] || 0;
  5511. }
  5512. while (this._used_names.has(candidate) || this._is_illegal_name(candidate, obj)) {
  5513. num += 1;
  5514. candidate = `${base}_${num}`;
  5515. }
  5516. this._used_names.add(candidate);
  5517. this._base_count[base] = num;
  5518. if (obj) {
  5519. this._obj_to_name[obj] = candidate;
  5520. } else {
  5521. this._unassociated_names.add(candidate);
  5522. }
  5523. return candidate;
  5524. }
  5525. _is_illegal_name(/* name, obj */) {
  5526. /*
  5527. if name in keyword.kwlist:
  5528. return True
  5529. if name in builtins.__dict__:
  5530. return obj is not builtins.__dict__[name]
  5531. if name in _custom_builtins:
  5532. return obj is not _custom_builtins[name].obj
  5533. */
  5534. return false;
  5535. }
  5536. associate_name_with_obj() {
  5537. }
  5538. });
  5539. this.registerType('torch.fx.node.Node', class {
  5540. constructor(graph, name, op, target, args, kwargs, return_type) {
  5541. this.graph = graph;
  5542. this.name = name;
  5543. this.op = op;
  5544. this.target = target;
  5545. this._input_nodes = new Map();
  5546. this.users = new Map();
  5547. this.type = return_type;
  5548. this._prev = this;
  5549. this._next = this;
  5550. this._erased = false;
  5551. this._repr_fn = null;
  5552. this.meta = {};
  5553. }
  5554. prepend(x) {
  5555. x._remove_from_list();
  5556. const p = this._prev;
  5557. p._next = x;
  5558. x._prev = p;
  5559. x._next = this;
  5560. this._prev = x;
  5561. }
  5562. _remove_from_list() {
  5563. const p = this._prev;
  5564. const n = this._next;
  5565. p._next = n;
  5566. n._prev = p;
  5567. }
  5568. });
  5569. torch.fx.Node = torch.fx.node.Node;
  5570. this.registerType('torch.fx.graph.Graph', class {
  5571. constructor() {
  5572. this._root = new torch.fx.node.Node(self, '', 'root', '', [], {});
  5573. this._used_names = new Map();
  5574. this._len = 0;
  5575. this._graph_namespace = new torch.fx.graph._Namespace();
  5576. // this._owning_module = owning_module
  5577. // this._tracer_cls = tracer_cls
  5578. // this._tracer_extras = tracer_extras
  5579. // this._codegen = CodeGen()
  5580. // this._co_fields = {}
  5581. }
  5582. placeholder(name, type_expr /*, default_value */) {
  5583. const args = []; // () if default_value is inspect.Signature.empty else (default_value,)
  5584. return this.create_node('placeholder', name, args, type_expr);
  5585. }
  5586. create_node(op, target, args, kwargs, name, type_expr) {
  5587. args = args || [];
  5588. kwargs = kwargs || {};
  5589. const candidate = name || this._target_to_str(target);
  5590. name = this._graph_namespace.create_name(candidate, null);
  5591. const n = new torch.fx.Node(this, name, op, target, args, kwargs, type_expr);
  5592. this._graph_namespace.associate_name_with_obj(name, n);
  5593. this._insert(n);
  5594. this._len += 1;
  5595. return n;
  5596. }
  5597. _insert(n) {
  5598. this._root.prepend(n);
  5599. }
  5600. _target_to_str(target) {
  5601. if (typeof target === 'string') {
  5602. if (target.startsWith('__') && target.endswith('__')) {
  5603. target = target.substring(2, target.length - 2);
  5604. }
  5605. } else {
  5606. target = target.__name__;
  5607. }
  5608. return this._snake_case(target);
  5609. }
  5610. _snake_case(s) {
  5611. const chars = [];
  5612. let prev_lower = false;
  5613. for (const c of s) {
  5614. const x = c.toLowerCase();
  5615. if (prev_lower && x !== c) {
  5616. chars.push('_');
  5617. } else {
  5618. prev_lower = true;
  5619. }
  5620. chars.push(x);
  5621. }
  5622. return chars.join('');
  5623. }
  5624. });
  5625. torch.fx.Graph = torch.fx.graph.Graph;
  5626. this.registerType('torch.fx.graph_module.GraphModule', class extends torch.nn.modules.module.Module {
  5627. constructor(root, graph) {
  5628. super();
  5629. this.graph = graph;
  5630. }
  5631. });
  5632. this.registerFunction('torch.fx._symbolic_trace.wrap', function(fn_or_name) {
  5633. return fn_or_name;
  5634. });
  5635. this.registerType('torch.fx._symbolic_trace.Tracer', class {});
  5636. this.registerFunction('torch._export.load', function(f, expected_opset_version) {
  5637. const serialized_exported_program = f.get('serialized_exported_program.json');
  5638. const serialized_state_dict = f.get('serialized_state_dict.pt');
  5639. const serialized_constants = f.get('serialized_constants.pt');
  5640. const artifact = new torch._export.serde.serialize.SerializedArtifact(serialized_exported_program, serialized_state_dict, serialized_constants);
  5641. return torch._export.serde.serialize.deserialize(artifact, expected_opset_version);
  5642. });
  5643. this.registerFunction('torch._export.serde.serialize._dict_to_dataclass', function(cls, data) {
  5644. if (data === null) {
  5645. return data;
  5646. }
  5647. if (data.$type) {
  5648. const res = {};
  5649. res[data.$type] = data.$value;
  5650. return res;
  5651. }
  5652. if (Array.isArray(data)) {
  5653. for (let i = 0; i < data.length; i++) {
  5654. data[i] = torch._export.serde.serialize._dict_to_dataclass(null, data[i]);
  5655. }
  5656. return data;
  5657. }
  5658. if (data === Object(data)) {
  5659. for (const key of Object.keys(data)) {
  5660. data[key] = torch._export.serde.serialize._dict_to_dataclass(null, data[key]);
  5661. }
  5662. return data;
  5663. }
  5664. return data;
  5665. });
  5666. this.registerFunction('torch._export.serde.serialize.deserialize', function(artifact, expected_opset_version) {
  5667. artifact.exported_program = torch._export.serde.serialize._dict_to_dataclass(null, artifact.exported_program);
  5668. return new torch._export.serde.serialize.ExportedProgramDeserializer(expected_opset_version).deserialize(artifact);
  5669. });
  5670. this.registerType('torch._export.serde.serialize.ExportedProgramDeserializer', class {
  5671. constructor(expected_opset_version) {
  5672. this.expected_opset_version = expected_opset_version;
  5673. }
  5674. deserialize(serialized_artifact) {
  5675. const symbol_name_to_range = new Map(Object.entries(serialized_artifact.exported_program.range_constraints));
  5676. /*
  5677. symbol_name_to_range = {
  5678. k: symbolic_shapes.ValueRanges(_int_to_sympy_int(v.min_val), _int_to_sympy_int(v.max_val))
  5679. for k, v in serialized_artifact.exported_program.range_constraints.items()
  5680. }
  5681. */
  5682. const constants = serialized_artifact.constants ? torch.load(serialized_artifact.constants) : null;
  5683. const tensor_constants = constants ? new Map(Object.entries(constants).filter(([, tensor]) => tensor instanceof torch.Tensor)) : null;
  5684. const deserializer = new torch._export.serde.serialize.GraphModuleDeserializer();
  5685. const res = deserializer.deserialize(serialized_artifact.exported_program.graph_module, symbol_name_to_range, constants);
  5686. const range_constraints = null;
  5687. /*
  5688. range_constraints = self.deserialize_range_constraints(
  5689. symbol_name_to_range, res.names_to_symbols,
  5690. )
  5691. model_opset_version: Optional[Dict[str, int]] = serialized_artifact.exported_program.opset_version
  5692. self._validate_model_opset_version(model_opset_version)
  5693. upgrader = GraphModuleOpUpgrader(self.expected_opset_version, model_opset_version)
  5694. */
  5695. const state_dict = serialized_artifact.state_dict ? torch.load(serialized_artifact.state_dict) : null;
  5696. const exported_program = new torch.export.exported_program.ExportedProgram(
  5697. res.graph_module, res.graph_module.graph, res.signature,
  5698. state_dict, range_constraints, res.module_call_graph, null,
  5699. null, // verifier=load_verifier(serialized_artifact.exported_program.dialect),
  5700. tensor_constants);
  5701. return exported_program;
  5702. // return upgrader.upgrade(exported_program)
  5703. }
  5704. });
  5705. this.registerType('torch._export.serde.serialize.GraphModuleDeserializer', class {
  5706. constructor() {
  5707. this.serialized_name_to_node = new Map();
  5708. this.serialized_name_to_meta = new Map();
  5709. this.graph = new torch.fx.Graph();
  5710. this.module = new torch.nn.Module();
  5711. this._SYM_INT_OPS = new Set([
  5712. operator.mul, operator.add, operator.sub, operator.floordiv, operator.mod,
  5713. torch.sym_sqrt, torch.sym_int, torch.sym_ite, torch.sym_max, torch.sym_min, torch.sym_sqrt
  5714. ]);
  5715. this._SYM_BOOL_OPS = new Set([
  5716. operator.eq, operator.ne, operator.le, operator.ge, operator.lt, operator.gt,
  5717. torch.sym_not
  5718. ]);
  5719. }
  5720. deserialize_graph_output(/* output */) {
  5721. /* TODO
  5722. if (output.type == 'as_tensor') {
  5723. return self.serialized_name_to_node[output.as_tensor.name]
  5724. }
  5725. else if (output.type == 'as_sym_int') {
  5726. return self.serialized_name_to_node[output.as_sym_int.as_name]
  5727. }
  5728. elif output.type == 'as_sym_bool':
  5729. return self.serialized_name_to_node[output.as_sym_bool.as_name]
  5730. else:
  5731. raise SerializeError(f'Unable to deserialize output node {output}')
  5732. */
  5733. }
  5734. deserialize_graph(serialized_graph) {
  5735. if (serialized_graph.constants) {
  5736. this.constants = new Map(Object.entries(serialized_graph.constants).map(([k, v]) => [ k, torch.load(v) ]));
  5737. }
  5738. for (const [name, tensor_value] of Object.entries(serialized_graph.tensor_values)) {
  5739. const meta_val = this.deserialize_tensor_meta(tensor_value.meta || tensor_value, this.fake_tensor_mode);
  5740. this.serialized_name_to_meta.set(name, meta_val);
  5741. }
  5742. for (const [name, sym_int_value] of Object.entries(serialized_graph.sym_int_values)) {
  5743. this.serialized_name_to_meta.set(name, this.deserialize_sym_int(sym_int_value));
  5744. }
  5745. for (const [name, sym_bool_value] in Object.entries(serialized_graph.sym_bool_values)) {
  5746. this.serialized_name_to_meta.set(name, this.deserialize_sym_bool(sym_bool_value));
  5747. }
  5748. for (const input of serialized_graph.inputs) {
  5749. const placeholder_node = this.graph.placeholder(input.as_tensor.name);
  5750. this.sync_fx_node(input.as_tensor.name, placeholder_node);
  5751. }
  5752. for (const serialized_node of serialized_graph.nodes) {
  5753. const target = this.deserialize_operator(serialized_node.target);
  5754. this.deserialize_node(serialized_node, target);
  5755. }
  5756. const outputs = [];
  5757. for (const output of serialized_graph.outputs) {
  5758. outputs.push(this.deserialize_graph_output(output));
  5759. }
  5760. }
  5761. deserialize_operator(serialized_target) {
  5762. let target = null;
  5763. if (serialized_target.startsWith('_operator')) {
  5764. target = operator;
  5765. } else if (serialized_target.startsWith('torch')) {
  5766. target = torch;
  5767. } else {
  5768. return serialized_target;
  5769. }
  5770. const serialized_target_names = serialized_target.split('.').reverse();
  5771. serialized_target_names.pop();
  5772. for (const name of serialized_target_names) {
  5773. target = target[name];
  5774. if (!target) {
  5775. return serialized_target;
  5776. }
  5777. }
  5778. return target;
  5779. }
  5780. deserialize_node(serialized_node, target) {
  5781. let fx_node = null;
  5782. if (this._SYM_BOOL_OPS.has(target) || this._SYM_INT_OPS.has(target)) {
  5783. /*
  5784. const name = serialized_node.outputs[0].value.as_name;
  5785. const args = self.deserialize_sym_op_inputs(serialized_node.inputs);
  5786. fx_node = self.graph.create_node("call_function", target, args, {}, name);
  5787. self.deserialize_sym_op_outputs(serialized_node, fx_node);
  5788. */
  5789. } else if (builtins.isinstance(target, torch._ops.HigherOrderOperator)) {
  5790. // assert(len(serialized_node.outputs) == 1 && serialized_node.outputs[0].type in ('as_tensors', 'as_tensor')), 'Only single tensor output or list of tensor output is supported for higher order operators.')
  5791. const [output] = serialized_node.outputs;
  5792. const name = output.type == 'as_tensor' ? output.value.name : null;
  5793. const args = serialized_node.inputs.map((input) => this.deserialize_input(input.arg));
  5794. fx_node = this.graph.create_node('call_function', target, args, {}, name);
  5795. if (output.as_tensor !== null) {
  5796. this.sync_fx_node(name, fx_node);
  5797. }
  5798. if (output.as_tensors !== null) {
  5799. this.deserialize_multiple_outputs(serialized_node, fx_node);
  5800. }
  5801. } else if (builtins.isinstance(target, torch._ops.OpOverload)) {
  5802. const name = this._is_single_tensor_return(target) ? serialized_node.outputs[0].as_tensor.name : null;
  5803. const [args, kwargs] = this.deserialize_inputs(target, serialized_node);
  5804. fx_node = self.graph.create_node('call_function', target, args, kwargs, name);
  5805. this.deserialize_outputs(serialized_node, fx_node);
  5806. } else {
  5807. // TODO
  5808. // throw new python.Error(`Unsupported target type '${target}'.`);
  5809. }
  5810. fx_node && Object.assign(fx_node.meta, this.deserialize_metadata(serialized_node.metadata));
  5811. }
  5812. deserialize(serialized_graph_module, symbol_name_to_range, constants) {
  5813. this.shape_env = new torch.fx.experimental.symbolic_shapes.ShapeEnv(/* assume_static_by_default = True */);
  5814. /*
  5815. this.fake_tensor_mode = FakeTensorMode(
  5816. allow_fallback_kernels=False,
  5817. allow_non_fake_inputs=True,
  5818. shape_env=self.shape_env,
  5819. )
  5820. */
  5821. this.symbol_name_to_symbol = new Map();
  5822. this.symbol_name_to_range = symbol_name_to_range || new Map();
  5823. this.constants = constants || new Map();
  5824. this.deserialize_graph(serialized_graph_module.graph);
  5825. const sig = null; // self.deserialize_signature(serialized_graph_module.signature)
  5826. const module_call_graph = null; // self.deserialize_module_call_graph(serialized_graph_module.module_call_graph)
  5827. return {
  5828. graph_module: torch._export.exported_program._create_graph_module_for_export(this.module, this.graph),
  5829. signature: sig,
  5830. module_call_graph: module_call_graph,
  5831. names_to_symbols: this.symbol_name_to_symbol
  5832. };
  5833. }
  5834. sync_fx_node(name, fx_node) {
  5835. if (this.serialized_name_to_node.has(name)) {
  5836. throw new python.Error(`Node ${name} has already been deserialized before.`);
  5837. }
  5838. this.serialized_name_to_node.set(name, fx_node);
  5839. fx_node.meta['val'] = this.serialized_name_to_meta.get(name);
  5840. }
  5841. deserialize_sym_op_inputs(inputs) {
  5842. return inputs.map((input) => this.deserialize_input(input.arg));
  5843. }
  5844. deserialize_inputs(target /* , serialized_node */) {
  5845. const schema_args = target._schema.arguments;
  5846. const actual_args = null;
  5847. /*
  5848. actual_args = {
  5849. input.name: self.deserialize_input(input.arg) for input in serialized_node.inputs
  5850. }
  5851. */
  5852. const args = [];
  5853. const kwargs = {};
  5854. for (const schema_arg of schema_args) {
  5855. const is_positional = !schema_arg.has_default_value() && !schema_arg.kwarg_only;
  5856. if (is_positional) {
  5857. args.push(actual_args[schema_arg.name]);
  5858. } else if (schema_arg.name in actual_args) {
  5859. kwargs[schema_arg.name] = actual_args[schema_arg.name];
  5860. }
  5861. }
  5862. return [ args, kwargs ];
  5863. }
  5864. deserialize_input(/* inp */) {
  5865. /*
  5866. value = inp.value
  5867. typ_ = inp.type
  5868. if typ_ == 'as_none':
  5869. # None should converted as None, but is encoded as bool in serialized
  5870. # Convert serialized object to torch equivalent
  5871. return None
  5872. elif typ_ == 'as_tensor':
  5873. return self.serialized_name_to_node[inp.as_tensor.name]
  5874. elif typ_ == 'as_scalar_type':
  5875. return _SERIALIZE_TO_TORCH_DTYPE[inp.as_scalar_type]
  5876. elif typ_ == 'as_memory_format':
  5877. return _SERIALIZE_TO_TORCH_MEMORY_FORMAT[inp.as_memory_format]
  5878. elif typ_ == 'as_layout':
  5879. return _SERIALIZE_TO_TORCH_LAYOUT[inp.as_layout]
  5880. elif typ_ == 'as_graph':
  5881. assert isinstance(value, GraphArgument)
  5882. with self.save_graph_module():
  5883. self.deserialize_graph(value.graph)
  5884. submodule = torch._export.exported_program._create_graph_module_for_export(self.module, self.graph)
  5885. self.module.register_module(value.name, submodule)
  5886. return self.graph.create_node(
  5887. 'get_attr',
  5888. value.name,
  5889. name=value.name,
  5890. )
  5891. elif typ_ == 'as_device':
  5892. return deserialize_device(inp.as_device)
  5893. elif typ_ == 'as_int':
  5894. return inp.as_int
  5895. elif typ_ == 'as_float':
  5896. return inp.as_float
  5897. elif typ_ == 'as_bool':
  5898. return inp.as_bool
  5899. elif typ_ == 'as_string':
  5900. return inp.as_string
  5901. elif typ_ == 'as_sym_int':
  5902. return self.deserialize_sym_argument(inp.as_sym_int)
  5903. elif typ_ == 'as_sym_bool':
  5904. return self.deserialize_sym_argument(inp.as_sym_bool)
  5905. elif isinstance(value, list):
  5906. if len(value) == 0:
  5907. return []
  5908. elif isinstance(value[0], TensorArgument):
  5909. result = []
  5910. for arg in value:
  5911. result.append(self.serialized_name_to_node[arg.name])
  5912. return result
  5913. elif isinstance(value[0], (int, float, bool)):
  5914. # convert from serialized.python.types.List to python list
  5915. return list(value)
  5916. elif isinstance(value[0], (SymIntArgument, SymBoolArgument)):
  5917. return [self.deserialize_sym_argument(arg) for arg in value]
  5918. elif isinstance(value[0], OptionalTensorArgument):
  5919. def deserialize_optional_tensor_args(a):
  5920. if a.type == 'as_none':
  5921. return None
  5922. elif a.type == 'as_tensor':
  5923. return self.serialized_name_to_node[a.value]
  5924. else:
  5925. raise SerializeError(f'Unhandled argument {inp}')
  5926. return list(map(deserialize_optional_tensor_args, value))
  5927. else:
  5928. raise SerializeError(f'Unhandled argument {inp}')
  5929. elif typ_ == 'as_custom_obj':
  5930. return self.constants[inp.as_custom_obj.name]
  5931. else {
  5932. raise SerializeError(`Unhandled argument ${inp}.`);
  5933. }
  5934. */
  5935. }
  5936. deserialize_metadata(metadata) {
  5937. const ret = {};
  5938. const stack_trace = metadata['stack_trace'];
  5939. if (stack_trace) {
  5940. ret['stack_trace'] = stack_trace;
  5941. }
  5942. const deserialize_meta_func = (serialized_target) => {
  5943. let module = null;
  5944. let serialized_target_names = [];
  5945. if (serialized_target.startsWith('torch.nn')) {
  5946. module = torch.nn;
  5947. serialized_target_names = serialized_target.split('.').slice(1);
  5948. } else if (serialized_target.startsWith('torch')) {
  5949. module = torch;
  5950. serialized_target_names = serialized_target.split('.').slice(1);
  5951. } else {
  5952. return this.deserialize_operator(serialized_target);
  5953. }
  5954. let target = module;
  5955. for (const name of serialized_target_names) {
  5956. if (!builtins.hasattr(target, name)) {
  5957. return serialized_target;
  5958. }
  5959. target = builtins.getattr(target, name);
  5960. }
  5961. return target;
  5962. };
  5963. const nn_module_stack_str = metadata['nn_module_stack'];
  5964. if (nn_module_stack_str) {
  5965. const import_nn_module_stack = (key, path, ty) => {
  5966. return [ key, [ path, ty ] ];
  5967. };
  5968. const nn_module_stack = new Map(nn_module_stack_str.split(';').map((item) => import_nn_module_stack(...item.split(','))));
  5969. ret['nn_module_stack'] = nn_module_stack;
  5970. }
  5971. const source_fn_st_str = metadata['source_fn_stack'];
  5972. if (source_fn_st_str) {
  5973. const source_fn_st = [];
  5974. for (const source_fn_str of source_fn_st_str.split(';')) {
  5975. const [name, target_str] = source_fn_str.split(',');
  5976. source_fn_st.push([ name, deserialize_meta_func(target_str) ]);
  5977. }
  5978. ret['source_fn_stack'] = source_fn_st;
  5979. }
  5980. return ret;
  5981. }
  5982. deserialize_tensor_meta(tensor_meta) {
  5983. const sizes = tensor_meta.sizes.map((val) => this.deserialize_sym_int(val));
  5984. const strides = tensor_meta.strides.map((val) => this.deserialize_sym_int(val));
  5985. const device = this.deserialize_device(tensor_meta.device);
  5986. const dtype = null; // TODO _SERIALIZE_TO_TORCH_DTYPE[tensor_meta.dtype],
  5987. return torch.empty_strided(sizes, strides, device, dtype);
  5988. }
  5989. deserialize_sym_int(s) {
  5990. if (s.as_expr !== undefined && s.as_expr !== null) {
  5991. let sym;
  5992. if (this.symbol_name_to_symbol.has(s.as_expr.expr_str)) {
  5993. sym = this.symbol_name_to_symbol.get(s.as_expr.expr_str);
  5994. } else {
  5995. sym = {};
  5996. /* TODO
  5997. sym = sympy.sympify(val.expr_str, locals=self.symbol_name_to_symbol)
  5998. if isinstance(sym, sympy.Symbol) {
  5999. self.symbol_name_to_symbol[val.expr_str] = sym
  6000. if vr := self.symbol_name_to_range.get(val.expr_str):
  6001. symbolic_shapes._constrain_symbol_range(
  6002. self.shape_env,
  6003. sym,
  6004. compiler_min=vr.lower, # type: ignore[arg-type]
  6005. compiler_max=vr.upper, # type: ignore[arg-type]
  6006. runtime_min=vr.lower, # type: ignore[arg-type]
  6007. runtime_max=vr.upper # type: ignore[arg-type]
  6008. )
  6009. }
  6010. */
  6011. }
  6012. const hint = s.as_expr.hint || null;
  6013. return this.shape_env.create_symintnode(sym, hint);
  6014. } else if (s.as_int !== undefined && s.as_int !== null) {
  6015. return s.as_int;
  6016. }
  6017. throw new python.Error('SymInt has invalid field type.');
  6018. }
  6019. deserialize_device(d) {
  6020. if (d.index !== undefined) {
  6021. return new torch.device(d.type, d.index);
  6022. }
  6023. return new torch.device(d.type);
  6024. }
  6025. });
  6026. this.registerFunction('torch_utils.persistence._reconstruct_persistent_obj', function(meta) {
  6027. const name = `_imported_module_${Math.floor(Math.random() * 10000)}`;
  6028. const module = execution.invoke('types.ModuleType', [ name ]);
  6029. execution.register('sys').modules.set(name, module);
  6030. const context = new python.Execution.Context(module, null);
  6031. execution.exec(meta.module_src, context);
  6032. const obj = execution.invoke(`${name}.${meta.class_name}`, []);
  6033. if (meta.state) {
  6034. if (obj.__setstate__) {
  6035. obj.__setstate__(meta.state);
  6036. } else {
  6037. Object.assign(obj, meta.state);
  6038. }
  6039. }
  6040. return obj;
  6041. });
  6042. this.registerFunction('torch_utils.misc.assert_shape', function(/* tensor, ref_shape */) {});
  6043. this.registerFunction('torch_utils.ops.conv2d_resample.conv2d_resample', function(/* x, w, f, up, down, padding, groups, flip_weight, flip_filter */) {});
  6044. this.registerFunction('torch_utils.ops.upfirdn2d.setup_filter', function(/* x, f, up, down, padding, flip_filter, gain, impl */) {});
  6045. this.registerFunction('torch_utils.ops.bias_act', function(/* x, b, dim, act, alpha, gain, clamp, impl */) {});
  6046. this.registerFunction('torch_utils.ops.fma.fma', function(/* a, b, c */) {});
  6047. this.registerType('torch.device', class {
  6048. constructor(type, index) {
  6049. this.type = type;
  6050. if (index) {
  6051. this.index = index;
  6052. }
  6053. }
  6054. });
  6055. this.registerType('torch.dtype', class {
  6056. constructor(scalar_type, name, itemsize) {
  6057. this._scalar_type = scalar_type;
  6058. this._name = name;
  6059. this._itemsize = itemsize;
  6060. }
  6061. scalar_type() {
  6062. return this._scalar_type;
  6063. }
  6064. itemsize() {
  6065. return this._itemsize;
  6066. }
  6067. __reduce__() {
  6068. return this._name;
  6069. }
  6070. __str__() {
  6071. return `torch.${this._name}`;
  6072. }
  6073. toString() {
  6074. return this.__str__();
  6075. }
  6076. });
  6077. this.registerType('torch.layout', class {
  6078. constructor(name) {
  6079. this._name = name;
  6080. }
  6081. __str__() {
  6082. return this._name;
  6083. }
  6084. toString() {
  6085. return this.__str__();
  6086. }
  6087. });
  6088. this.registerType('torch.qscheme', class {
  6089. constructor(name) {
  6090. this._name = name;
  6091. }
  6092. __str__() {
  6093. return this._name;
  6094. }
  6095. toString() {
  6096. return this.__str__();
  6097. }
  6098. });
  6099. this.registerType('torch.utils.hooks.RemovableHandle', class {
  6100. __setstate__(state) {
  6101. [this.hooks_dict_ref, this.id] = state;
  6102. this.hooks_dict_ref = this.hooks_dict_ref || new Map();
  6103. }
  6104. });
  6105. this.registerType('torch.storage._StorageBase', class {
  6106. constructor(size, dtype) {
  6107. this._size = size;
  6108. this._dtype = dtype;
  6109. this._device = null;
  6110. }
  6111. get device() {
  6112. return this._device;
  6113. }
  6114. get dtype() {
  6115. return this._dtype;
  6116. }
  6117. element_size() {
  6118. return this._dtype.element_size;
  6119. }
  6120. size() {
  6121. return this._size;
  6122. }
  6123. get data() {
  6124. return this._cdata;
  6125. }
  6126. _set_cdata(data) {
  6127. const length = this.size() * this.dtype.itemsize();
  6128. if (length !== data.length) {
  6129. throw new python.Error('Storage data size mismatch.');
  6130. }
  6131. this._cdata = data;
  6132. }
  6133. _set_from_file(unpickler) {
  6134. const buffer = unpickler.read(8);
  6135. const size = buffer.reverse().reduce((a, b) => (a*256)+b, 0);
  6136. if (size !== this.size()) {
  6137. throw new python.Error('Storage size mismatch.');
  6138. }
  6139. const itemsize = this.dtype.itemsize();
  6140. const data = unpickler.stream(itemsize * size);
  6141. this._set_cdata(data);
  6142. }
  6143. static _new_with_file(unpickler) {
  6144. const buffer = unpickler.read(8);
  6145. const size = buffer.reverse().reduce((a, b) => (a * 256) + b, 0);
  6146. const storage = new this(size);
  6147. const itemsize = storage.dtype.itemsize();
  6148. const data = unpickler.stream(itemsize * size);
  6149. storage._set_cdata(data);
  6150. return storage;
  6151. }
  6152. });
  6153. this.registerType('torch.storage._UntypedStorage', class extends torch.storage._StorageBase {
  6154. constructor() {
  6155. super();
  6156. throw new python.Error('_UntypedStorage not implemented.');
  6157. }
  6158. });
  6159. this.registerType('torch.storage._TypedStorage', class {
  6160. constructor() {
  6161. if (arguments.length >= 2 && Number.isInteger(arguments[0]) && arguments[1] instanceof torch.dtype) {
  6162. if (arguments[3] instanceof torch.device) {
  6163. [this._size, this._dtype, , this._device] = arguments;
  6164. } else {
  6165. [this._size, this._dtype] = arguments;
  6166. }
  6167. } else {
  6168. throw new python.Error(`Unsupported _TypedStorage arguments '${JSON.stringify(arguments)}'.`);
  6169. }
  6170. }
  6171. get device() {
  6172. return this._device;
  6173. }
  6174. get dtype() {
  6175. return this._dtype;
  6176. }
  6177. element_size() {
  6178. return this._dtype.element_size;
  6179. }
  6180. size() {
  6181. return this._size;
  6182. }
  6183. get data() {
  6184. return this._cdata;
  6185. }
  6186. _set_cdata(data) {
  6187. const length = this.size() * this.dtype.itemsize();
  6188. if (length !== data.length) {
  6189. throw new python.Error('Storage data size mismatch.');
  6190. }
  6191. this._cdata = data;
  6192. }
  6193. _set_from_file(unpickler) {
  6194. const buffer = unpickler.read(8);
  6195. const size = buffer.reverse().reduce((a, b) => (a*256)+b, 0);
  6196. if (size !== this.size()) {
  6197. throw new python.Error('Storage size mismatch.');
  6198. }
  6199. const itemsize = this.dtype.itemsize();
  6200. const data = unpickler.stream(itemsize * size);
  6201. this._set_cdata(data);
  6202. }
  6203. static _new_with_file(unpickler) {
  6204. const buffer = unpickler.read(8);
  6205. const size = buffer.reverse().reduce((a, b) => (a*256)+b, 0);
  6206. const storage = new this(size);
  6207. const itemsize = storage.dtype.itemsize();
  6208. const data = unpickler.stream(itemsize * size);
  6209. storage._set_cdata(data);
  6210. return storage;
  6211. }
  6212. });
  6213. this.registerType('torch.storage._LegacyStorage', class extends torch.storage._TypedStorage {
  6214. constructor() {
  6215. super();
  6216. throw new python.Error('_LegacyStorage not implemented.');
  6217. }
  6218. });
  6219. this.registerType('torch.BoolStorage', class extends torch.storage._StorageBase {
  6220. constructor(size) {
  6221. super(size, torch.bool);
  6222. }
  6223. });
  6224. this.registerType('torch.ByteStorage', class extends torch.storage._StorageBase {
  6225. constructor(size) {
  6226. super(size, torch.uint8);
  6227. }
  6228. });
  6229. this.registerType('torch.CharStorage', class extends torch.storage._StorageBase {
  6230. constructor(size) {
  6231. super(size, torch.int8);
  6232. }
  6233. });
  6234. this.registerType('torch.ShortStorage', class extends torch.storage._StorageBase {
  6235. constructor(size) {
  6236. super(size, torch.int16);
  6237. }
  6238. });
  6239. this.registerType('torch.IntStorage', class extends torch.storage._StorageBase {
  6240. constructor(size) {
  6241. super(size, torch.int32);
  6242. }
  6243. });
  6244. this.registerType('torch.LongStorage', class extends torch.storage._StorageBase {
  6245. constructor(size) {
  6246. super(size, torch.int64);
  6247. }
  6248. });
  6249. this.registerType('torch.HalfStorage', class extends torch.storage._StorageBase {
  6250. constructor(size) {
  6251. super(size, torch.float16);
  6252. }
  6253. });
  6254. this.registerType('torch.FloatStorage', class extends torch.storage._StorageBase {
  6255. constructor(size) {
  6256. super(size, torch.float32);
  6257. }
  6258. });
  6259. this.registerType('torch.DoubleStorage', class extends torch.storage._StorageBase {
  6260. constructor(size) {
  6261. super(size, torch.float64);
  6262. }
  6263. });
  6264. this.registerType('torch.ComplexHalfStorage', class extends torch.storage._StorageBase {
  6265. constructor(size) {
  6266. super(size, torch.complex32);
  6267. }
  6268. });
  6269. this.registerType('torch.ComplexFloatStorage', class extends torch.storage._StorageBase {
  6270. constructor(size) {
  6271. super(size, torch.complex64);
  6272. }
  6273. });
  6274. this.registerType('torch.ComplexDoubleStorage', class extends torch.storage._StorageBase {
  6275. constructor(size) {
  6276. super(size, torch.complex128);
  6277. }
  6278. });
  6279. this.registerType('torch.QInt8Storage', class extends torch.storage._StorageBase {
  6280. constructor(size) {
  6281. super(size, torch.qint8);
  6282. }
  6283. });
  6284. this.registerType('torch.QUInt8Storage', class extends torch.storage._StorageBase {
  6285. constructor(size) {
  6286. super(size, torch.quint8);
  6287. }
  6288. });
  6289. this.registerType('torch.QInt32Storage', class extends torch.storage._StorageBase {
  6290. constructor(size) {
  6291. super(size, torch.qint32);
  6292. }
  6293. });
  6294. this.registerType('torch.BFloat16Storage', class extends torch.storage._StorageBase {
  6295. constructor(size) {
  6296. super(size, torch.bfloat16);
  6297. }
  6298. });
  6299. this.registerType('torch.Size', class extends Array {
  6300. constructor(size) {
  6301. super(size.length);
  6302. for (let i = 0; i < size.length; i++) {
  6303. this[i] = size[i];
  6304. }
  6305. }
  6306. __len__() {
  6307. return this.length;
  6308. }
  6309. });
  6310. this.registerType('torch.Tensor', class {
  6311. constructor() {
  6312. this._layout = torch.strided;
  6313. }
  6314. get device() {
  6315. return this.storage().device;
  6316. }
  6317. get dtype() {
  6318. if (this._layout === torch.sparse_coo) {
  6319. return this._values.dtype();
  6320. }
  6321. return this.storage().dtype;
  6322. }
  6323. get shape() {
  6324. return this._shape;
  6325. }
  6326. get layout() {
  6327. return this._layout;
  6328. }
  6329. get values() {
  6330. if (this._layout === torch.sparse_coo) {
  6331. return this._values;
  6332. }
  6333. throw new python.Error(`Unsupported values in layout'${this._layout.__str__()}'.`);
  6334. }
  6335. get indices() {
  6336. if (this._layout === torch.sparse_coo) {
  6337. return this._indices;
  6338. }
  6339. throw new python.Error(`Unsupported indices in layout'${this._indices.__str__()}'.`);
  6340. }
  6341. get is_quantized() {
  6342. return this.__quantized__ === true;
  6343. }
  6344. get is_nested() {
  6345. return this.__nested__ === true;
  6346. }
  6347. get is_sparse() {
  6348. return this.layout !== torch.strided;
  6349. }
  6350. size() {
  6351. return this._shape;
  6352. }
  6353. storage() {
  6354. if (!this._storage) {
  6355. const name = this.__class__.__name__ == 'Tensor' ? 'FloatStorage' : this.__storage__.__name__.replace('Tensor', 'Storage');
  6356. this._storage = self.invoke(`${this.__class__.__module__}.${name}`, []);
  6357. }
  6358. return this._storage;
  6359. }
  6360. storage_offset() {
  6361. return this._storage_offset;
  6362. }
  6363. stride() {
  6364. return this._stride;
  6365. }
  6366. resize_(shape) {
  6367. this._shape = shape;
  6368. }
  6369. __len__() {
  6370. return this._shape[0];
  6371. }
  6372. __setstate__(state) {
  6373. switch (state.length) {
  6374. case 3:
  6375. break;
  6376. case 4:
  6377. [this._storage, this._storage_offset, this._shape, this._stride] = state;
  6378. break;
  6379. case 5:
  6380. [this.data, ,this._backward_hooks, this.requires_grad] = state;
  6381. break;
  6382. default:
  6383. throw new python.Error(`Unsupported tensor state length '${state.length}'.`);
  6384. }
  6385. }
  6386. __bool__() {
  6387. return true;
  6388. }
  6389. __int__() {
  6390. const storage = this.storage();
  6391. if (storage && storage.dtype.__reduce__() === 'int64' && storage.data.length === 8) {
  6392. const buffer = storage.data.peek ? storage.data.peek() : storage.data;
  6393. const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
  6394. return view.getInt64(0, true);
  6395. }
  6396. return NaN;
  6397. }
  6398. __float__() {
  6399. const storage = this.storage();
  6400. if (storage && storage.dtype.__reduce__() === 'float32') {
  6401. if (storage.size() !== undefined && storage.data.length === 4) {
  6402. const buffer = storage.data.peek ? storage.data.peek() : storage.data;
  6403. const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
  6404. return view.getFloat32(0, true);
  6405. }
  6406. }
  6407. return NaN;
  6408. }
  6409. __str__() {
  6410. return 'tensor(...)';
  6411. }
  6412. });
  6413. this.registerType('torch.nn.parameter.Parameter', class extends torch.Tensor {
  6414. constructor(data, requires_grad) {
  6415. super();
  6416. if (!data) {
  6417. data = self.invoke('torch.Tensor', [[]]);
  6418. }
  6419. this.data = data;
  6420. this.requires_grad = requires_grad !== undefined ? requires_grad : true;
  6421. }
  6422. });
  6423. this.registerType('torch.nn.parameter.UninitializedParameter', class extends torch.nn.parameter.Parameter {
  6424. constructor(requires_grad /*, device, dtype */) {
  6425. super(undefined, requires_grad);
  6426. }
  6427. });
  6428. this.registerType('torch.nn.parameter.UninitializedBuffer', class extends torch.Tensor {});
  6429. this.registerType('torch.BoolTensor', class extends torch.Tensor {});
  6430. this.registerType('torch.ByteTensor', class extends torch.Tensor {});
  6431. this.registerType('torch.CharTensor', class extends torch.Tensor {});
  6432. this.registerType('torch.ShortTensor', class extends torch.Tensor {});
  6433. this.registerType('torch.IntTensor', class extends torch.Tensor {});
  6434. this.registerType('torch.LongTensor', class extends torch.Tensor {});
  6435. this.registerType('torch.HalfTensor', class extends torch.Tensor {});
  6436. this.registerType('torch.FloatTensor', class extends torch.Tensor {});
  6437. this.registerType('torch.DoubleTensor', class extends torch.Tensor {});
  6438. this.registerType('torch.ComplexFloatTensor', class extends torch.Tensor {});
  6439. this.registerType('torch.ComplexDoubleTensor', class extends torch.Tensor {});
  6440. this.registerType('torch.QInt8Tensor', class extends torch.Tensor {});
  6441. this.registerType('torch.QUInt8Tensor', class extends torch.Tensor {});
  6442. this.registerType('torch.QInt32Tensor', class extends torch.Tensor {});
  6443. this.registerType('torch.BFloat16Tensor', class extends torch.Tensor {});
  6444. this.registerType('torch.cuda.FloatTensor', class extends torch.Tensor {});
  6445. this.registerType('torch.cuda.DoubleTensor', class extends torch.Tensor {});
  6446. this.registerType('torch.SymInt', class {
  6447. constructor(node) {
  6448. this.node = node;
  6449. }
  6450. });
  6451. this.registerType('torch._C._TensorBase', class {});
  6452. this.registerType('torch._C._VariableFunctionsClass', class {});
  6453. this.register('torch.nn').Module = this.register('torch.nn.modules.module').Module;
  6454. this.register('torch.optim').Adam = this.register('torch.optim.adam').Adam;
  6455. this.register('torch.nn').ReLU = this.register('torch.nn.modules.activation').ReLU;
  6456. this.register('sklearn.utils').Bunch = this.register('sklearn.utils._bunch').Bunch;
  6457. torch.uint8 = torch.ByteStorage.dtype = new torch.dtype(0, 'uint8', 1);
  6458. torch.int8 = torch.CharStorage.dtype = new torch.dtype(1, 'int8', 1);
  6459. torch.int16 = torch.ShortStorage.dtype = new torch.dtype(2, 'int16', 2);
  6460. torch.int32 = torch.IntStorage.dtype = new torch.dtype(3, 'int32', 4);
  6461. torch.int64 = torch.LongStorage.dtype = new torch.dtype(4, 'int64', 8);
  6462. torch.float16 = torch.HalfStorage.dtype = new torch.dtype(5, 'float16', 2);
  6463. torch.float32 = torch.FloatStorage.dtype = new torch.dtype(6, 'float32', 4);
  6464. torch.float64 = torch.DoubleStorage.dtype = new torch.dtype(7, 'float64', 8);
  6465. torch.complex32 = torch.ComplexHalfStorage.dtype = new torch.dtype(8, 'complex32', 4);
  6466. torch.complex64 = torch.ComplexFloatStorage.dtype = new torch.dtype(9, 'complex64', 8);
  6467. torch.complex128 = torch.ComplexDoubleStorage.dtype = new torch.dtype(10, 'complex128', 16);
  6468. torch.bool = torch.BoolStorage.dtype = new torch.dtype(11, 'boolean', 1);
  6469. torch.qint8 = torch.QInt8Storage.dtype = new torch.dtype(12, 'qint8', 1);
  6470. torch.quint8 = torch.QUInt8Storage.dtype = new torch.dtype(13, 'quint8', 1);
  6471. torch.qint32 = torch.QInt32Storage.dtype = new torch.dtype(14, 'qint32', 4);
  6472. torch.bfloat16 = torch.BFloat16Storage.dtype = new torch.dtype(15, 'bfloat16', 2);
  6473. torch.quint4x2 = new torch.dtype(16, 'quint4x2');
  6474. torch.strided = new torch.layout('torch.strided');
  6475. torch.sparse_coo = new torch.layout('torch.sparse_coo');
  6476. torch.sparse_csr = new torch.layout('torch.sparse_csr');
  6477. torch.sparse_csc = new torch.layout('torch.sparse_csc');
  6478. torch.sparse_bsr = new torch.layout('torch.sparse_bsr');
  6479. torch.sparse_bsc = new torch.layout('torch.sparse_bsc');
  6480. torch._mkldnn = new torch.layout('torch._mkldnn');
  6481. torch.per_tensor_affine = new torch.qscheme('torch.per_tensor_affine');
  6482. torch.per_channel_affine = new torch.qscheme('torch.per_channel_affine');
  6483. torch.per_tensor_symmetric = new torch.qscheme('torch.per_tensor_symmetric');
  6484. torch.per_channel_symmetric = new torch.qscheme('torch.per_channel_symmetric');
  6485. torch.per_channel_affine_float_qparams = new torch.qscheme('torch.per_channel_affine_float_qparams');
  6486. torch.inf = this.register('math').inf;
  6487. this.registerType('fastcore.basics.fastuple', class {});
  6488. this.registerType('fastcore.basics.GetAttr', class {});
  6489. this.registerType('fastcore.dispatch._TypeDict', class {});
  6490. this.registerType('fastcore.dispatch.TypeDispatch', class {});
  6491. this.registerType('fastcore.foundation.L', class {});
  6492. this.registerType('fastcore.transform.Pipeline', class {});
  6493. this.registerType('fastcore.transform.Transform', class {});
  6494. this.registerType('fastcore.transform.ItemTransform', class extends fastcore.transform.Transform {});
  6495. this.registerType('fastai.callback.core.TrainEvalCallback', class {});
  6496. this.registerType('fastai.callback.hook._hook_inner', class {});
  6497. this.registerType('fastai.callback.hook.Hook', class {});
  6498. this.registerType('fastai.callback.hook.Hooks', class {});
  6499. this.registerType('fastai.callback.progress.ProgressCallback', class {});
  6500. this.registerType('fastai.callback.core.Callback', class extends fastcore.basics.GetAttr {});
  6501. this.registerType('fastai.data.core.DataLoaders', class extends fastcore.basics.GetAttr {});
  6502. this.registerType('fastai.data.core.Datasets', class {});
  6503. this.registerType('fastai.data.load.DataLoader', class extends fastcore.basics.GetAttr {});
  6504. this.registerType('fastai.data.core.TfmdDL', class extends fastai.data.load.DataLoader {});
  6505. this.registerType('fastai.data.core.TfmdLists', class {});
  6506. this.registerType('fastai.data.load._FakeLoader', class {});
  6507. this.registerFunction('fastai.data.load._wif', function() {
  6508. throw new python.Error("'fastai.data.load._wif' not implemented.");
  6509. });
  6510. this.registerType('fastai.data.transforms.Categorize', class {});
  6511. this.registerType('fastai.data.transforms.Category', class {});
  6512. this.registerType('fastai.data.transforms.CategoryMap', class {});
  6513. this.registerType('fastai.data.transforms.IntToFloatTensor', class {});
  6514. this.registerType('fastai.data.transforms.Normalize', class {});
  6515. this.registerType('fastai.data.transforms.parent_label', class {});
  6516. this.registerType('fastai.data.transforms.RegressionSetup', class {});
  6517. this.registerType('fastai.data.transforms.ToTensor', class {});
  6518. this.registerType('fastai.imports.noop', class {});
  6519. this.registerType('fastai.layers.AdaptiveConcatPool2d', class {});
  6520. this.registerType('fastai.layers.ConvLayer', class {});
  6521. this.registerType('fastai.layers.Embedding', class {});
  6522. this.registerType('fastai.layers.Flatten', class {});
  6523. this.registerType('fastai.layers.LinBnDrop', class {});
  6524. this.registerType('fastai.layers.MergeLayer', class {});
  6525. this.registerType('fastai.layers.PixelShuffle_ICNR', class {});
  6526. this.registerType('fastai.layers.ResBlock', class {});
  6527. this.registerType('fastai.layers.SelfAttention', class {});
  6528. this.registerType('fastai.layers.SigmoidRange', class {});
  6529. this.registerType('fastai.learner.Metric', class {});
  6530. this.registerType('fastai.learner.AvgLoss', class extends fastai.learner.Metric {});
  6531. this.registerType('fastai.learner.AvgMetric', class extends fastai.learner.Metric {});
  6532. this.registerType('fastai.learner.AvgSmoothLoss', class extends fastai.learner.Metric {});
  6533. this.registerType('fastai.learner.CastToTensor', class extends fastai.callback.core.Callback {});
  6534. this.registerType('fastai.learner.Learner', class extends fastcore.basics.GetAttr {});
  6535. this.registerType('fastai.learner.Recorder', class {});
  6536. this.registerType('fastai.losses.BaseLoss', class {});
  6537. this.registerType('fastai.losses.CrossEntropyLossFlat', class {});
  6538. this.registerType('fastai.metrics.AccumMetric', class extends fastai.learner.Metric {});
  6539. this.registerFunction('fastai.metrics._rmse', function() {
  6540. throw new python.Error("'fastai.metrics._rmse' not implemented.");
  6541. });
  6542. this.registerFunction('fastai.metrics.accuracy', function() {
  6543. throw new python.Error("'fastai.metrics.error_rate' not implemented.");
  6544. });
  6545. this.registerFunction('fastai.metrics.error_rate', function() {
  6546. throw new python.Error("'fastai.metrics.error_rate' not implemented.");
  6547. });
  6548. this.registerFunction('fastai.optimizer.Adam', function() {
  6549. throw new python.Error("'fastai.optimizer.Adam' not implemented.");
  6550. });
  6551. this.registerType('fastai.tabular.core.Categorify', class {});
  6552. this.registerType('fastai.tabular.core.FillMissing', class {});
  6553. this.registerType('fastai.tabular.core.FillStrategy', class {});
  6554. this.registerType('fastai.tabular.core.ReadTabBatch', class extends fastcore.transform.ItemTransform {});
  6555. this.registerType('fastai.tabular.core.TabDataLoader', class extends fastai.data.core.TfmdDL {});
  6556. this.registerType('fastai.tabular.data.TabularDataLoaders', class extends fastai.data.core.DataLoaders {});
  6557. this.registerType('fastai.tabular.core.Tabular', class {});
  6558. this.registerType('fastai.tabular.core.TabularPandas', class extends fastai.tabular.core.Tabular {});
  6559. this.registerType('fastai.tabular.learner.TabularLearner', class extends fastai.learner.Learner {});
  6560. this.registerType('fastai.tabular.model.TabularModel', class {});
  6561. this.registerFunction('fastai.torch_core._fa_rebuild_tensor', function(cls) {
  6562. const tensor = self.invoke('torch._utils._rebuild_tensor_v2', Array.from(arguments).slice(1));
  6563. return self.invoke(cls, tensor);
  6564. });
  6565. this.registerType('fastai.torch_core.TensorBase', class extends torch.Tensor {
  6566. constructor(x) {
  6567. super();
  6568. Object.assign(this, x);
  6569. }
  6570. });
  6571. this.registerType('fastai.torch_core.TensorCategory', class {});
  6572. this.registerType('fastai.torch_core.TensorImage', class {});
  6573. this.registerFunction('fastai.torch_core.trainable_params', function() {
  6574. throw new python.Error("'fastai.torch_core.trainable_params' not implemented.");
  6575. });
  6576. this.registerFunction('fastai.torch_core._rebuild_from_type', function(func, type, args, dict) {
  6577. const tensor = self.invoke(type, [ func(...args) ]);
  6578. Object.assign(tensor, dict);
  6579. return tensor;
  6580. });
  6581. this.registerType('fastai.vision.augment._BrightnessLogit', class {});
  6582. this.registerType('fastai.vision.augment._ContrastLogit', class {});
  6583. this.registerType('fastai.vision.augment._WarpCoord', class {});
  6584. this.registerType('fastai.vision.augment.Brightness', class {});
  6585. this.registerType('fastai.vision.augment.flip_mat', class {});
  6586. this.registerType('fastai.vision.augment.Flip', class {});
  6587. this.registerType('fastai.vision.augment.RandomResizedCropGPU', class {});
  6588. this.registerType('fastai.vision.augment.Resize', class {});
  6589. this.registerType('fastai.vision.augment.rotate_mat', class {});
  6590. this.registerType('fastai.vision.augment.zoom_mat', class {});
  6591. this.registerType('fastai.vision.core.PILImage', class {});
  6592. this.registerType('fastai.vision.learner._resnet_split', class {});
  6593. this.registerType('fastai.vision.models.unet.DynamicUnet', class {});
  6594. this.registerType('fastai.vision.models.unet.ResizeToOrig', class {});
  6595. this.registerType('fastai.vision.models.unet.UnetBlock', class {});
  6596. }
  6597. get builtins() {
  6598. return this._builtins;
  6599. }
  6600. source(file) {
  6601. return this._sources.has(file) ? this._sources.get(file) : null;
  6602. }
  6603. debug(/* file */) {
  6604. }
  6605. exec(code , context) {
  6606. const reader = new python.Parser(code, '', null);
  6607. const program = reader.parse();
  6608. if (!program) {
  6609. throw new python.Error("Module '" + '?' + "' parse error.");
  6610. }
  6611. this.block(program.body, context);
  6612. }
  6613. parse(file) {
  6614. const buffer = this.source(file);
  6615. if (buffer) {
  6616. const debug = this.debug(file);
  6617. const code = this._utf8Decoder.decode(buffer);
  6618. const parser = new python.Parser(code, file, debug);
  6619. const program = parser.parse();
  6620. if (!program) {
  6621. throw new python.Error(`Module '${file}' parse error.`);
  6622. }
  6623. return program;
  6624. }
  6625. return null;
  6626. }
  6627. import(name, current, level) {
  6628. if (level) {
  6629. let bits = current.split('.');
  6630. if (bits.length < level) {
  6631. throw new python.Error('Invalid relative import beyond top-level package.');
  6632. }
  6633. bits = bits.slice(0, bits.length - level);
  6634. const base = bits.join('.');
  6635. name = name ? [ base, name ].join('.') : base;
  6636. }
  6637. const index = name.lastIndexOf('.');
  6638. let parent = null;
  6639. let child = null;
  6640. if (index > 0) {
  6641. parent = name.substring(0, index);
  6642. child = name.substring(index + 1);
  6643. this.import(parent);
  6644. }
  6645. if (!this._modules.has(name)) {
  6646. const module = this._registry.get(name) || new this._builtins.module(name);
  6647. module.__package__ = name;
  6648. this._modules.set(name, module);
  6649. const path = name.split('.').join('/');
  6650. module.__path__ = [ path ];
  6651. const file = `${path}.py`;
  6652. const program = this.parse(file);
  6653. if (program) {
  6654. module.__file__ = file;
  6655. for (const [name, value] of Object.entries(this.builtins)) {
  6656. switch (name) {
  6657. case '__class__':
  6658. case '__package__':
  6659. case '__module__':
  6660. case '__name__':
  6661. case '__path__':
  6662. case '__file__':
  6663. break;
  6664. default:
  6665. module[name] = value;
  6666. break;
  6667. }
  6668. }
  6669. const context = new python.Execution.Context(module, null);
  6670. if (name !== 'builtins') {
  6671. context.set('__builtins__', this._modules.get('builtins'));
  6672. }
  6673. this.block(program.body, context);
  6674. }
  6675. if (parent) {
  6676. const parent_module = this._modules.get(parent);
  6677. parent_module[child] = module;
  6678. }
  6679. }
  6680. return this._modules.get(name);
  6681. }
  6682. __import__(name, globals, locals, fromlist, level) {
  6683. let module = null;
  6684. level = level || 0;
  6685. if (level === 0) {
  6686. module = this.import(name);
  6687. } else {
  6688. globals = globals || {};
  6689. let current = globals.__package__;
  6690. if (!current) {
  6691. const spec = globals.__spec__;
  6692. if (spec) {
  6693. current = spec.parent;
  6694. } else {
  6695. const name = globals.__name__;
  6696. const bits = name.split('.');
  6697. bits.pop();
  6698. current = bits.join('.');
  6699. }
  6700. }
  6701. module = this.import(name, current, level);
  6702. }
  6703. if (!fromlist) {
  6704. if (level === 0) {
  6705. return this.import(name.split('.')[0]);
  6706. } else if (name) {
  6707. throw new python.Error(`Unsupported relative import '${name}'.`);
  6708. // cut_off = len(name) - len(name.partition('.')[0])
  6709. // return sys.modules[module.__name__[:len(module.__name__)-cut_off]]
  6710. }
  6711. } else if (module.__path__) {
  6712. const handle_fromlist = (module, fromlist, recursive) => {
  6713. for (const name of fromlist) {
  6714. if (name == '*') {
  6715. if (!recursive && module.__all__) {
  6716. handle_fromlist(module, module.__all__, true);
  6717. }
  6718. } else if (!module[name]) {
  6719. this.import(`${module.__name__}.${name}`);
  6720. }
  6721. }
  6722. return module;
  6723. };
  6724. handle_fromlist(module, fromlist);
  6725. }
  6726. return module;
  6727. }
  6728. module(name) {
  6729. return this._modules.get(name);
  6730. }
  6731. resolve(name) {
  6732. const index = name.lastIndexOf('.');
  6733. const memberName = index === -1 ? name : name.substring(index + 1, name.length);
  6734. const moduleName = index === -1 ? '' : name.substring(0, index);
  6735. const module = this.import(moduleName);
  6736. let type = module ? module[memberName] : null;
  6737. if (!type) {
  6738. if (!this._unresolved.has(name)) {
  6739. const moduleName = name.split('.').shift();
  6740. if (this._registry.has(moduleName) && moduleName !== '__main__') {
  6741. this.emit('resolve', name);
  6742. }
  6743. const type = this._createType(name, class {});
  6744. this._unresolved.set(name, type);
  6745. }
  6746. type = this._unresolved.get(name);
  6747. }
  6748. return type;
  6749. }
  6750. invoke(target, args) {
  6751. if (typeof target === 'string') {
  6752. target = this.resolve(target);
  6753. }
  6754. if (target) {
  6755. if (target.__class__ === this._builtins.type) {
  6756. if (target.prototype && target.prototype.__class__ === target) {
  6757. return Reflect.construct(target, args);
  6758. }
  6759. const obj = Object.create(target);
  6760. if (obj.__init__ && typeof obj.__init__ === 'function') {
  6761. obj.__init__.apply(obj, args);
  6762. }
  6763. return obj;
  6764. } else if (target.__class__ === this._builtins.function) {
  6765. if (target.__call__) {
  6766. return target.__call__(args);
  6767. }
  6768. return target.apply(null, args);
  6769. }
  6770. }
  6771. throw new python.Error('Unsupported invoke target.');
  6772. }
  6773. call(target, name, args, context) {
  6774. const callTarget = this.target(target, context);
  6775. const callArguments = args.map((argument) => this.expression(argument, context));
  6776. if (!callTarget || (name !== null && !callTarget[name])) {
  6777. if (name === '__new__' && callArguments.length === 1 && callArguments[0] == callTarget) {
  6778. name = null;
  6779. callArguments.shift();
  6780. } else {
  6781. const format = (expression) => {
  6782. if (expression.type == 'id') {
  6783. return expression.value;
  6784. }
  6785. if (expression.type == '.') {
  6786. return `${format(expression.target)}.${format(expression.member)}`;
  6787. }
  6788. return null;
  6789. };
  6790. const targetName = `${format(target)}.${name}`;
  6791. throw new python.Error(`Unknown function '${targetName}'.`);
  6792. }
  6793. }
  6794. const func = name ? callTarget[name] : callTarget;
  6795. if (func.__class__ === this._builtins.type) {
  6796. if (func.prototype && func.prototype.__class__ === func) {
  6797. return Reflect.construct(func, args);
  6798. }
  6799. const obj = Object.create(func);
  6800. obj.__class__ = func;
  6801. if (obj.__init__ && typeof obj.__init__ === 'function') {
  6802. obj.__init__.apply(obj, args);
  6803. }
  6804. return obj;
  6805. }
  6806. if (func.__class__ === this._builtins.function) {
  6807. if (func.__call__) {
  6808. return func.__call__(callArguments);
  6809. }
  6810. }
  6811. if (func.__class__ === this._builtins.method) {
  6812. if (func.__call__) {
  6813. return func.__call__([ callTarget ].concat(callArguments));
  6814. }
  6815. }
  6816. if (typeof func === 'function') {
  6817. return func.apply(callTarget, callArguments);
  6818. }
  6819. throw new python.Error("Unsupported call expression.");
  6820. }
  6821. apply(method, args, context) {
  6822. const locals = Array.prototype.slice.call(args);
  6823. context = new python.Execution.Context(context.globals, {});
  6824. for (const parameter of method.parameters) {
  6825. let value = locals.shift();
  6826. if (value === undefined && parameter.initializer) {
  6827. value = this.expression(parameter.initializer, context);
  6828. }
  6829. context.set(parameter.name, value);
  6830. }
  6831. return this.block(method.body.statements, context);
  6832. }
  6833. block(statements, context) {
  6834. statements = Array.prototype.slice.call(statements);
  6835. while (statements.length > 0) {
  6836. const statement = statements.shift();
  6837. const value = this.statement(statement, context);
  6838. if (value !== undefined) {
  6839. return value;
  6840. }
  6841. }
  6842. return undefined;
  6843. }
  6844. statement(statement, context) {
  6845. switch (statement.type) {
  6846. case 'pass': {
  6847. break;
  6848. }
  6849. case 'return': {
  6850. return this.expression(statement.expression, context);
  6851. }
  6852. case 'def': {
  6853. const module = context.get('__name__');
  6854. const self = this;
  6855. const parent = context.get('__class__');
  6856. const type = (parent === this._builtins.module) ? this._builtins.function : this._builtins.method;
  6857. const func = {
  6858. __class__: type,
  6859. __globals__: context,
  6860. __module__: module,
  6861. __name__: statement.name,
  6862. __code__: statement,
  6863. __call__: function(args) {
  6864. return self.apply(this.__code__, args, this.__globals__);
  6865. }
  6866. };
  6867. context.set(statement.name, func);
  6868. break;
  6869. }
  6870. case 'class': {
  6871. const bases = statement.bases.map((arg) => this.expression(arg, context));
  6872. if (bases.length > 1) {
  6873. throw new python.Error(`Unsupported multiple bases for class '${statement.name}'.`);
  6874. }
  6875. const base = bases.length === 1 ? bases[0] : null;
  6876. const name = `${context.get('__name__')}.${statement.name}`;
  6877. const value = this._createType(name, base ? class extends base {} : class {});
  6878. value.__bases__ = bases;
  6879. context.set(statement.name, value);
  6880. this.block(statement.body.statements, new python.Execution.Context(context.globals, value.prototype));
  6881. break;
  6882. }
  6883. case 'var': {
  6884. context.set(statement.name, statement.initializer ? this.expression(statement.initializer, context) : undefined);
  6885. break;
  6886. }
  6887. case '=': {
  6888. this.expression(statement, context);
  6889. break;
  6890. }
  6891. case 'if': {
  6892. const condition = this.expression(statement.condition, context);
  6893. if (condition === true || condition) {
  6894. const value = this.block(statement.then.statements, context);
  6895. if (value !== undefined) {
  6896. return value;
  6897. }
  6898. break;
  6899. } else if (condition === false) {
  6900. const value = this.block(statement.else.statements, context);
  6901. if (value !== undefined) {
  6902. return value;
  6903. }
  6904. break;
  6905. }
  6906. throw new python.Error("Unsupported condition.");
  6907. }
  6908. case 'for': {
  6909. if (statement.target.length == 1 &&
  6910. statement.variable.length === 1 && statement.variable[0].type === 'id') {
  6911. const range = this.expression(statement.target[0], context);
  6912. const [variable] = statement.variable;
  6913. for (const current of range) {
  6914. this.statement({ type: '=', target: variable, expression: { type: 'number', value: current } }, context);
  6915. const value = this.block(statement.body.statements, context);
  6916. if (value !== undefined) {
  6917. return value;
  6918. }
  6919. }
  6920. break;
  6921. }
  6922. throw new python.Error("Unsupported 'for' statement.");
  6923. }
  6924. case 'while': {
  6925. const condition = this.expression(statement.condition, context);
  6926. if (condition) {
  6927. const value = this.block(statement.body.statements, context);
  6928. if (value !== undefined) {
  6929. return value;
  6930. }
  6931. }
  6932. break;
  6933. }
  6934. case 'with': {
  6935. const items = [];
  6936. for (const item of statement.item) {
  6937. items.push(this.expression(item.expression, context));
  6938. }
  6939. for (const item of items) {
  6940. if (item.__enter__ && item.__enter__.__call__) {
  6941. item.__enter__.__call__([ item ]);
  6942. }
  6943. }
  6944. const value = this.block(statement.body.statements, context);
  6945. for (const item of items) {
  6946. if (item.__exit__ && item.__exit__.__call__) {
  6947. item.__exit__.__call__([ item ]);
  6948. }
  6949. }
  6950. if (value !== undefined) {
  6951. return value;
  6952. }
  6953. break;
  6954. }
  6955. case 'call': {
  6956. this.expression(statement, context);
  6957. break;
  6958. }
  6959. case 'import': {
  6960. for (const alias of statement.names) {
  6961. let module = this.__import__(alias.name, context);
  6962. if (alias.asname) {
  6963. const bits = alias.name.split('.').reverse();
  6964. bits.pop();
  6965. while (bits.length > 0) {
  6966. module = module[bits.pop()];
  6967. }
  6968. context.set(alias.asname, module);
  6969. } else {
  6970. context.set(alias.name.split('.')[0], module);
  6971. }
  6972. }
  6973. break;
  6974. }
  6975. case 'import_from': {
  6976. const fromlist = statement.names.map((name) => name.name);
  6977. const module = this.__import__(statement.module, context.globals, context.locals, fromlist, statement.level);
  6978. for (const entry of statement.names) {
  6979. const name = entry.name;
  6980. const asname = entry.asname ? entry.asname : null;
  6981. if (!module[name]) {
  6982. throw new python.Error(`Cannot import '${name}' from '${statement.module}'.`);
  6983. }
  6984. context.set(asname ? asname : name, module[name]);
  6985. }
  6986. break;
  6987. }
  6988. case 'string': {
  6989. break;
  6990. }
  6991. default: {
  6992. throw new python.Error(`Unsupported statement '${statement.type}'.`);
  6993. }
  6994. }
  6995. return undefined;
  6996. }
  6997. expression(expression, context) {
  6998. const self = context.get('self');
  6999. switch (expression.type) {
  7000. case '=': {
  7001. const target = expression.target;
  7002. if (target.type === 'id') {
  7003. context.set(target.value, this.expression(expression.expression, context));
  7004. return undefined;
  7005. } else if (target.type === '[]') {
  7006. if (target.target.type === 'id' &&
  7007. target.arguments.type === 'list' &&
  7008. target.arguments.value.length === 1) {
  7009. const index = this.expression(target.arguments.value[0], context);
  7010. if (target.target.value === '__annotations__') {
  7011. context.set(target.target.value, context.get(target.target.value) || {});
  7012. }
  7013. const obj = context.get(target.target.value);
  7014. const value = this.expression(expression.expression, context);
  7015. if (obj instanceof Map) {
  7016. obj.set(index, value);
  7017. } else {
  7018. obj[index] = value;
  7019. }
  7020. return undefined;
  7021. }
  7022. } else if (target.type === '.' &&
  7023. target.member.type === 'id') {
  7024. this.expression(target.target, context)[target.member.value] = this.expression(expression.expression, context);
  7025. return undefined;
  7026. } else if (target.type === 'tuple') {
  7027. context.target.push(target.value);
  7028. const value = this.expression(expression.expression, context);
  7029. context.target.pop();
  7030. if (target.value.every((item) => item.type === 'id')) {
  7031. if (target.value.length < value.length) {
  7032. throw new python.Error(`ValueError: too many values to unpack (expected ${target.value.length}, actual ${value.length}).`);
  7033. }
  7034. if (target.value.length > value.length) {
  7035. throw new python.Error(`ValueError: not enough values to unpack (expected ${target.value.length}, actual ${value.length}).`);
  7036. }
  7037. for (let i = 0; i < value.length; i++) {
  7038. context.set(target.value[i].value, value[i]);
  7039. }
  7040. return undefined;
  7041. }
  7042. }
  7043. break;
  7044. }
  7045. case 'list': {
  7046. return expression.value.map((item) => this.expression(item, context));
  7047. }
  7048. case 'string': {
  7049. return expression.value.substring(1, expression.value.length - 1);
  7050. }
  7051. case 'number': {
  7052. return Number(expression.value);
  7053. }
  7054. case '[]': {
  7055. if (expression.target.type === 'id' &&
  7056. expression.arguments.type === 'list' &&
  7057. expression.arguments.value.length === 1) {
  7058. if (context.get(expression.target.value)) {
  7059. const index = this.expression(expression.arguments.value[0], context);
  7060. const target = context.get(expression.target.value);
  7061. if (target instanceof Map) {
  7062. return target.get(index);
  7063. }
  7064. return target[index < 0 ? target.length + index : index];
  7065. }
  7066. }
  7067. const target = this.expression(expression.target, context);
  7068. if (target && expression.arguments.type === 'list' &&
  7069. (target.__class__ === this._typing._TupleType ||
  7070. target.__class__ === this._typing._SpecialGenericAlias ||
  7071. target.__class__ === this._typing._SpecialForm)) {
  7072. const type = Object.assign({}, target);
  7073. type.__args__ = expression.arguments.value.map((arg) => this.expression(arg, context));
  7074. return type;
  7075. }
  7076. if (expression.arguments.type === 'list' && expression.arguments.value.length === 1) {
  7077. const index = this.expression(expression.arguments.value[0], context);
  7078. if (target instanceof Map) {
  7079. return target.get(index);
  7080. }
  7081. return target[index < 0 ? target.length + index : index];
  7082. }
  7083. break;
  7084. }
  7085. case '.': {
  7086. if (expression.member.type == 'id') {
  7087. const target = this.target(expression.target, context);
  7088. return target[expression.member.value];
  7089. }
  7090. throw new python.Error("Unsupported field expression.");
  7091. }
  7092. case 'call': {
  7093. if (expression.target.type === '.') {
  7094. return this.call(expression.target.target, expression.target.member.value, expression.args, context);
  7095. }
  7096. return this.call(expression.target, null, expression.args, context);
  7097. }
  7098. case 'id': {
  7099. switch (expression.value) {
  7100. case 'self': return self;
  7101. case 'None': return null;
  7102. case 'True': return true;
  7103. case 'False': return false;
  7104. default: {
  7105. const type = (value) => {
  7106. return value &&
  7107. (value.__class__ === this._builtins.type ||
  7108. value.__class__ === this._typing._TupleType ||
  7109. value.__class__ === this._typing._SpecialGenericAlias ||
  7110. value.__class__ === this._typing._SpecialForm);
  7111. };
  7112. const builtin = this._builtins[expression.value];
  7113. if (type(builtin)) {
  7114. return builtin;
  7115. }
  7116. const value = context.get(expression.value);
  7117. if (value === undefined) {
  7118. const typing = this._typing[expression.value];
  7119. if (type(typing)) {
  7120. return typing;
  7121. }
  7122. }
  7123. return value;
  7124. }
  7125. }
  7126. }
  7127. case 'tuple': {
  7128. return expression.value.map((expression) => this.expression(expression, context));
  7129. }
  7130. case 'dict': {
  7131. const dict = {};
  7132. for (const pair of expression.value) {
  7133. if (pair.type !== 'pair') {
  7134. throw new python.Error(`Unsupported dict item type '${pair.type}'.`);
  7135. }
  7136. const key = this.expression(pair.key, context);
  7137. const value = this.expression(pair.value, context);
  7138. dict[key] = value;
  7139. }
  7140. return dict;
  7141. }
  7142. case 'unary': {
  7143. switch (expression.op) {
  7144. case '-': {
  7145. return -this.expression(expression.operand, context);
  7146. }
  7147. default: {
  7148. throw new python.Error(`Unsupported unary expression '${expression.op}'.`);
  7149. }
  7150. }
  7151. }
  7152. default: {
  7153. throw new python.Error(`Unsupported expression '${expression.type}'.`);
  7154. }
  7155. }
  7156. return undefined;
  7157. }
  7158. target(expression, context) {
  7159. let current = expression;
  7160. let path = [];
  7161. for (;;) {
  7162. if (current.type === '.' && current.member && current.member.type === 'id') {
  7163. path.push(current.member.value);
  7164. current = current.target;
  7165. } else if (current.type === 'id' && current.value !== 'self' && current.value !== 'CONSTANTS') {
  7166. path.push(current.value);
  7167. break;
  7168. } else {
  7169. path = null;
  7170. break;
  7171. }
  7172. }
  7173. if (path) {
  7174. let target = null;
  7175. for (let i = path.length - 1; i >= 0; i--) {
  7176. target = target ? target[path[i]] : context.get(path[i]);
  7177. if (!target) {
  7178. break;
  7179. }
  7180. }
  7181. if (!target) {
  7182. path.reverse();
  7183. const name = path.join('.');
  7184. const file = `${path.join('/')}.py`;
  7185. if (this._sources.has(file)) {
  7186. target = this.import(name);
  7187. } else {
  7188. target = this.resolve(name);
  7189. }
  7190. }
  7191. return target;
  7192. }
  7193. return this.expression(expression, context);
  7194. }
  7195. add(name, source) {
  7196. this._sources.set(name, source);
  7197. }
  7198. on(event, listener) {
  7199. const value = this._events.get(event) || [];
  7200. value.push(listener);
  7201. this._events.set(event, value);
  7202. }
  7203. emit(event, ...args) {
  7204. if (this._events.has(event)) {
  7205. for (const callback of this._events.get(event)) {
  7206. callback(this, ...args);
  7207. }
  7208. }
  7209. }
  7210. register(name, value) {
  7211. if (!this._registry.has(name)) {
  7212. value = value || new (this._registry.get('builtins').module)(name);
  7213. this._registry.set(name, value);
  7214. let current = name;
  7215. for (;;) {
  7216. const index = current.lastIndexOf('.');
  7217. if (index === -1) {
  7218. break;
  7219. }
  7220. const child = current.substring(index + 1);
  7221. current = current.substring(0, index);
  7222. const parent = this.register(current);
  7223. parent[child] = value;
  7224. value = parent;
  7225. }
  7226. }
  7227. return this._registry.get(name);
  7228. }
  7229. registerFunction(name, value) {
  7230. const parts = name.split('.');
  7231. value.__class__ = this._builtins.function;
  7232. value.__name__ = parts.pop();
  7233. value.__module__ = parts.join('.');
  7234. const module = this.register(value.__module__);
  7235. if (module[name]) {
  7236. throw new python.Error(`Function '${name}' is already registered.`);
  7237. }
  7238. module[value.__name__] = value;
  7239. return value;
  7240. }
  7241. _createType(name, value) {
  7242. const parts = name.split('.');
  7243. value.__class__ = this._builtins.type;
  7244. value.__name__ = parts.pop();
  7245. value.__module__ = parts.join('.');
  7246. value.prototype.__class__ = value;
  7247. return value;
  7248. }
  7249. registerType(name, value) {
  7250. value = this._createType(name, value);
  7251. const parts = name.split('.');
  7252. const memberName = parts.pop();
  7253. const moduleName = parts.join('.');
  7254. const module = this.register(moduleName);
  7255. if (module[memberName]) {
  7256. throw new python.Error(`Class '${memberName}' is already registered.`);
  7257. }
  7258. module[memberName] = value;
  7259. return value;
  7260. }
  7261. };
  7262. python.Execution.Context = class {
  7263. constructor(globals, locals) {
  7264. this.globals = globals;
  7265. this.locals = locals;
  7266. }
  7267. set(name, value) {
  7268. if (this.locals) {
  7269. this.locals[name] = value;
  7270. } else {
  7271. this.globals[name] = value;
  7272. }
  7273. }
  7274. get(name) {
  7275. if (this.locals && name in this.locals) {
  7276. return this.locals[name];
  7277. }
  7278. if (name in this.globals) {
  7279. return this.globals[name];
  7280. }
  7281. return undefined;
  7282. }
  7283. get target() {
  7284. this._target = this._target || [];
  7285. return this._target;
  7286. }
  7287. };
  7288. python.BinaryReader = class {
  7289. constructor(buffer) {
  7290. this._buffer = buffer;
  7291. this._length = buffer.length;
  7292. this._position = 0;
  7293. this._view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
  7294. this._utf8Decoder = new TextDecoder('utf-8');
  7295. this._asciiDecoder = new TextDecoder('ascii');
  7296. }
  7297. get position() {
  7298. return this._position;
  7299. }
  7300. get length() {
  7301. return this._length;
  7302. }
  7303. seek(position) {
  7304. this._position = position >= 0 ? position : this._length + position;
  7305. if (this._position > this._buffer.length) {
  7306. throw new python.Error(`Expected ${this._position - this._buffer.length} more bytes. The file might be corrupted. Unexpected end of file.`);
  7307. }
  7308. }
  7309. skip(offset) {
  7310. this._position += offset;
  7311. if (this._position > this._buffer.length) {
  7312. throw new python.Error(`Expected ${this._position - this._buffer.length} more bytes. The file might be corrupted. Unexpected end of file.`);
  7313. }
  7314. }
  7315. stream(length) {
  7316. const buffer = this.read(length);
  7317. return new python.BinaryReader(buffer);
  7318. }
  7319. peek(length) {
  7320. const position = this._position;
  7321. length = length !== undefined ? length : this._length - this._position;
  7322. this.skip(length);
  7323. const end = this._position;
  7324. this.skip(-length);
  7325. if (position === 0 && length === this._length) {
  7326. return this._buffer;
  7327. }
  7328. return this._buffer.subarray(position, end);
  7329. }
  7330. read(length) {
  7331. const position = this._position;
  7332. length = length !== undefined ? length : this._length - this._position;
  7333. this.skip(length);
  7334. if (position === 0 && length === this._length) {
  7335. return this._buffer;
  7336. }
  7337. return this._buffer.subarray(position, this._position);
  7338. }
  7339. byte() {
  7340. const position = this._position;
  7341. this.skip(1);
  7342. return this._view.getUint8(position);
  7343. }
  7344. uint16() {
  7345. const position = this._position;
  7346. this.skip(2);
  7347. return this._view.getUint16(position, true);
  7348. }
  7349. int32() {
  7350. const position = this._position;
  7351. this.skip(4);
  7352. return this._view.getInt32(position, true);
  7353. }
  7354. uint32() {
  7355. const position = this._position;
  7356. this.skip(4);
  7357. return this._view.getUint32(position, true);
  7358. }
  7359. int64() {
  7360. const position = this._position;
  7361. this.skip(8);
  7362. return this._view.getInt64(position, true).toNumber();
  7363. }
  7364. float64() {
  7365. const position = this._position;
  7366. this.skip(8);
  7367. return this._view.getFloat64(position, false);
  7368. }
  7369. string(size, encoding) {
  7370. const data = this.read(size);
  7371. return (encoding == 'utf-8') ?
  7372. this._utf8Decoder.decode(data) :
  7373. this._asciiDecoder.decode(data);
  7374. }
  7375. line() {
  7376. const index = this._buffer.indexOf(0x0A, this._position);
  7377. if (index == -1) {
  7378. throw new python.Error("Could not find end of line.");
  7379. }
  7380. const size = index - this._position;
  7381. const text = this.string(size, 'ascii');
  7382. this.skip(1);
  7383. return text;
  7384. }
  7385. };
  7386. python.StreamReader = class {
  7387. constructor(stream) {
  7388. this._stream = stream;
  7389. this._length = stream.length;
  7390. this._position = 0;
  7391. this._utf8Decoder = new TextDecoder('utf-8');
  7392. this._asciiDecoder = new TextDecoder('ascii');
  7393. }
  7394. get position() {
  7395. return this._position;
  7396. }
  7397. get length() {
  7398. return this._length;
  7399. }
  7400. seek(position) {
  7401. this._stream.seek(position);
  7402. this._position = this._stream.position;
  7403. }
  7404. skip(offset) {
  7405. this._position += offset;
  7406. if (this._position > this._length) {
  7407. throw new python.Error(`Expected ${this._position - this._length} more bytes. The file might be corrupted. Unexpected end of file.`);
  7408. }
  7409. }
  7410. stream(length) {
  7411. this._stream.seek(this._position);
  7412. this.skip(length);
  7413. return this._stream.stream(length);
  7414. }
  7415. peek(length) {
  7416. this._stream.seek(this._position);
  7417. return this._stream.peek(length);
  7418. }
  7419. read(length) {
  7420. this._stream.seek(this._position);
  7421. this.skip(length);
  7422. return this._stream.read(length);
  7423. }
  7424. byte() {
  7425. const position = this._fill(1);
  7426. return this._view.getUint8(position);
  7427. }
  7428. uint16() {
  7429. const position = this._fill(2);
  7430. return this._view.getUint16(position, true);
  7431. }
  7432. int32() {
  7433. const position = this._fill(4);
  7434. return this._view.getInt32(position, true);
  7435. }
  7436. uint32() {
  7437. const position = this._fill(4);
  7438. return this._view.getUint32(position, true);
  7439. }
  7440. int64() {
  7441. const position = this._fill(8);
  7442. return this._view.getInt64(position, true).toNumber();
  7443. }
  7444. float64() {
  7445. const position = this._fill(8);
  7446. return this._view.getFloat64(position, true);
  7447. }
  7448. string(size, encoding) {
  7449. const data = this.read(size);
  7450. return (encoding == 'utf-8') ?
  7451. this._utf8Decoder.decode(data) :
  7452. this._asciiDecoder.decode(data);
  7453. }
  7454. line() {
  7455. let position = this._fill(0);
  7456. let index = this._buffer.indexOf(0x0A, position);
  7457. if (index == -1) {
  7458. const size = Math.min(0x1000000, this._stream.length - this._position);
  7459. this._fill(size);
  7460. this.skip(-size);
  7461. position = this._fill(0);
  7462. index = this._buffer.indexOf(0x0A, position);
  7463. if (index == -1) {
  7464. throw new python.Error("Could not find end of line.");
  7465. }
  7466. }
  7467. const size = index - position;
  7468. const text = this.string(size, 'ascii');
  7469. this.skip(1);
  7470. return text;
  7471. }
  7472. _fill(length) {
  7473. if (this._position + length > this._length) {
  7474. throw new Error(`Expected ${this._position + length - this._length} more bytes. The file might be corrupted. Unexpected end of file.`);
  7475. }
  7476. if (!this._buffer || this._position < this._offset || this._position + length > this._offset + this._buffer.length) {
  7477. this._offset = this._position;
  7478. this._stream.seek(this._offset);
  7479. this._buffer = this._stream.read(Math.min(0x10000000, this._length - this._offset));
  7480. this._view = new DataView(this._buffer.buffer, this._buffer.byteOffset, this._buffer.byteLength);
  7481. }
  7482. const position = this._position;
  7483. this._position += length;
  7484. return position - this._offset;
  7485. }
  7486. };
  7487. python.Error = class extends Error {
  7488. constructor(message) {
  7489. super(message);
  7490. this.name = 'Python Error';
  7491. }
  7492. };
  7493. export const Execution = python.Execution;