12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054 |
- """This module is the core of the ggml-python library, it exposes a low-level [ctypes](https://docs.python.org/3/library/ctypes.html)-based interface for ggml.
- Structures and functions in the `ggml.ggml` module map directly to the original ggml C library and
- they operate at a fairly low level.
- No additional runtime checks checks are performed nor is memory management handled automatically.
- You've been warned :).
- With that in mind here are some useful things to keep in mind
- - Functions accept both ctypes types (c_int, c_bool, c_float, etc.) and Python types (int, bool, float, etc.) as parameters.
- - Functions return Python types for simple values (int, bool, float, etc.) and ctypes types for complex values ([ggml_context_p][ggml.ggml_context_p], [ggml_tensor_p][ggml.ggml_tensor_p], etc.).
- - Memory management is the responsibility of the user. The user must call [ggml.ggml_free][] on the context after calling [ggml.ggml_init][].
- Example
- ```python
- import ggml
- import ctypes
- # Allocate a new context with 16 MB of memory
- params = ggml.ggml_init_params(mem_size=16 * 1024 * 1024, mem_buffer=None)
- ctx = ggml.ggml_init(params=params)
- # Instantiate tensors
- x = ggml.ggml_new_tensor_1d(ctx, ggml.GGML_TYPE_F32, 1)
- a = ggml.ggml_new_tensor_1d(ctx, ggml.GGML_TYPE_F32, 1)
- b = ggml.ggml_new_tensor_1d(ctx, ggml.GGML_TYPE_F32, 1)
- # Use ggml operations to build a computational graph
- x2 = ggml.ggml_mul(ctx, x, x)
- f = ggml.ggml_add(ctx, ggml.ggml_mul(ctx, a, x2), b)
- gf = ggml.ggml_build_forward(f)
- # Set the input values
- ggml.ggml_set_f32(x, 2.0)
- ggml.ggml_set_f32(a, 3.0)
- ggml.ggml_set_f32(b, 4.0)
- # Compute the graph
- ggml.ggml_graph_compute_with_ctx(ctx, ctypes.pointer(gf), 1)
- # Get the output value
- output = ggml.ggml_get_f32_1d(f, 0)
- assert output == 16.0
- # Free the context
- ggml.ggml_free(ctx)
- ```
- """
- import os
- import sys
- import ctypes
- import pathlib
- import importlib.resources
- import numpy as np
- from typing import Union
- from typing import Type
- from typing import Callable
- from typing import Tuple
- from typing import Dict
- from typing_extensions import Self
- from typing import Any
- from pathlib import Path
- from typing import List, Optional, Sequence, Union
- from typing_extensions import TypeAlias
- NULL: ctypes.c_void_p = None # ignore: type
- GGML_MEM_ALIGN = 16
- # Load the library
- def load_shared_library(base_path: Path, lib_base_name: str):
- # Construct the paths to the possible shared library names
- # Searching for the library in the current directory under the name "libggml" (default name
- # for ggml) and "ggml" (default name for this repo)
- lib_names: List[str] = [
- f"lib{lib_base_name}.so",
- f"lib{lib_base_name}.dylib",
- f"{lib_base_name}.dll",
- ]
- path = None
- cdll_args = dict() # type: ignore
- # Add the library directory to the DLL search path on Windows (if needed)
- if sys.platform == "win32" and sys.version_info >= (3, 8):
- os.add_dll_directory(str(base_path))
- cdll_args["winmode"] = 0
- for lib_name in lib_names:
- # Try to load the shared library, handling potential errors
- path = base_path / lib_name
- if not path.exists():
- continue
- try:
- return ctypes.CDLL(str(path), **cdll_args)
- except Exception as e:
- raise RuntimeError(f"Failed to load shared library '{path}': {e}")
- raise FileNotFoundError(
- f"Shared library with base name '{lib_base_name}' not found in {base_path}"
- )
- base_path = Path(__file__).parent.resolve() / "build/src"
- lib_base_name = "ggml"
- lib = load_shared_library(base_path, lib_base_name)
- #####################################################
- # GGML Utility Types
- #####################################################
- CFloatArray: TypeAlias = "ctypes.Array[ctypes.c_float]"
- CInt64Array: TypeAlias = "ctypes.Array[ctypes.c_int64]"
- CIntPointer: TypeAlias = "ctypes._Pointer[ctypes.c_int]" # type: ignore
- CCharPointer: TypeAlias = "ctypes._Pointer[ctypes.c_char]" # type: ignore
- #####################################################
- # source: ggml.h
- # GGML API
- #####################################################
- # #define GGML_FILE_MAGIC 0x67676d6c // "ggml"
- GGML_FILE_MAGIC = int("0x67676d6c", 16)
- # #define GGML_FILE_VERSION 1
- GGML_FILE_VERSION = 1
- # #define GGML_QNT_VERSION 2 // bump this on quantization format changes
- GGML_QNT_VERSION = 2
- # #define GGML_QNT_VERSION_FACTOR 1000 // do not change this
- GGML_QNT_VERSION_FACTOR = 1000
- # #define GGML_MAX_DIMS 4
- GGML_MAX_DIMS = 4
- # #define GGML_MAX_NODES 4096
- GGML_MAX_NODES = 4096
- # #define GGML_MAX_PARAMS 256
- GGML_MAX_PARAMS = 256
- # #define GGML_MAX_CONTEXTS 64
- GGML_MAX_CONTEXTS = 64
- # #define GGML_MAX_SRC 6
- GGML_MAX_SRC = 6
- # #define GGML_MAX_NAME 64
- GGML_MAX_NAME = 64
- # #define GGML_MAX_OP_PARAMS 32
- GGML_MAX_OP_PARAMS = 32
- # #define GGML_DEFAULT_N_THREADS 4
- GGML_DEFAULT_N_THREADS = 4
- # #if UINTPTR_MAX == 0XFFFFFFFF
- # #define GGML_MEMALIGN 4
- # #else
- # # define GGML_MEMALIGN 16
- # #endif
- GGML_MEMALIGN = (
- 16 if ctypes.sizeof(ctypes.c_void_p) == 4 else 32
- ) # FIXME: Check if this is correct
- # #define GGML_EXIT_SUCCESS 0
- GGML_EXIT_SUCCESS = 0
- # #define GGML_EXIT_ABORTED 1
- GGML_EXIT_ABORTED = 1
- # #define GGUF_MAGIC 0x46554747 // "GGUF"
- GGUF_MAGIC = int("0x46554747", 16)
- # #define GGUF_VERSION 2
- GGUF_VERSION = 2
- # #define GGUF_DEFAULT_ALIGNMENT 32
- GGUF_DEFAULT_ALIGNMENT = 32
- # TODO: Check if this is correct
- # typedef uint16_t ggml_fp16_t;
- ggml_fp16_t = ctypes.c_uint16
- CFP16Array: TypeAlias = "ctypes.Array[ggml_fp16_t]"
- # GGML_API float ggml_fp16_to_fp32(ggml_fp16_t x);
- def ggml_fp16_to_fp32(x: ggml_fp16_t) -> float:
- return lib.ggml_fp16_to_fp32(x)
- lib.ggml_fp16_to_fp32.argtypes = [ggml_fp16_t]
- lib.ggml_fp16_to_fp32.restype = ctypes.c_float
- # GGML_API ggml_fp16_t ggml_fp32_to_fp16(float x);
- def ggml_fp32_to_fp16(x: ctypes.c_float) -> int:
- return lib.ggml_fp32_to_fp16(x)
- lib.ggml_fp32_to_fp16.argtypes = [ctypes.c_float]
- lib.ggml_fp32_to_fp16.restype = ggml_fp16_t
- # GGML_API void ggml_fp16_to_fp32_row(const ggml_fp16_t * x, float * y, size_t n);
- def ggml_fp16_to_fp32_row(
- x: CFP16Array,
- y: CFloatArray,
- n: Union[ctypes.c_int, int],
- ) -> None:
- return lib.ggml_fp16_to_fp32_row(x, y, n)
- lib.ggml_fp16_to_fp32_row.argtypes = [
- ctypes.POINTER(ggml_fp16_t),
- ctypes.POINTER(ctypes.c_float),
- ctypes.c_int,
- ]
- lib.ggml_fp16_to_fp32_row.restype = None
- # GGML_API void ggml_fp32_to_fp16_row(const float * x, ggml_fp16_t * y, size_t n);
- def ggml_fp32_to_fp16_row(
- x: CFloatArray,
- y: CFP16Array,
- n: Union[ctypes.c_int, int],
- ) -> None:
- return lib.ggml_fp32_to_fp16_row(x, y, n)
- lib.ggml_fp32_to_fp16_row.argtypes = [
- ctypes.POINTER(ctypes.c_float),
- ctypes.POINTER(ggml_fp16_t),
- ctypes.c_int,
- ]
- lib.ggml_fp32_to_fp16_row.restype = None
- # struct ggml_context;
- ggml_context_p = ctypes.c_void_p
- """Opaque pointer to a ggml_context.
- ggml_context structs are not accessed directly instead they must be created using [ggml_init](ggml.ggml_init) and freed using [ggml_free](ggml.ggml_free)."""
- # enum ggml_type {
- # GGML_TYPE_F32 = 0,
- # GGML_TYPE_F16 = 1,
- # GGML_TYPE_Q4_0 = 2,
- # GGML_TYPE_Q4_1 = 3,
- # // GGML_TYPE_Q4_2 = 4, support has been removed
- # // GGML_TYPE_Q4_3 (5) support has been removed
- # GGML_TYPE_Q5_0 = 6,
- # GGML_TYPE_Q5_1 = 7,
- # GGML_TYPE_Q8_0 = 8,
- # GGML_TYPE_Q8_1 = 9,
- # GGML_TYPE_Q2_K = 10,
- # GGML_TYPE_Q3_K = 11,
- # GGML_TYPE_Q4_K = 12,
- # GGML_TYPE_Q5_K = 13,
- # GGML_TYPE_Q6_K = 14,
- # GGML_TYPE_Q8_K = 15,
- # GGML_TYPE_I8,
- # GGML_TYPE_I16,
- # GGML_TYPE_I32,
- # GGML_TYPE_COUNT,
- # };
- GGML_TYPE_F32 = 0
- GGML_TYPE_F16 = 1
- GGML_TYPE_Q4_0 = 2
- GGML_TYPE_Q4_1 = 3
- GGML_TYPE_Q5_0 = 6
- GGML_TYPE_Q5_1 = 7
- GGML_TYPE_Q8_0 = 8
- GGML_TYPE_Q8_1 = 9
- GGML_TYPE_Q2_K = 10
- GGML_TYPE_Q3_K = 11
- GGML_TYPE_Q4_K = 12
- GGML_TYPE_Q5_K = 13
- GGML_TYPE_Q6_K = 14
- GGML_TYPE_Q8_K = 15
- GGML_TYPE_I8 = 16
- GGML_TYPE_I16 = 17
- GGML_TYPE_I32 = 18
- GGML_TYPE_COUNT = 19
- # enum ggml_backend {
- # GGML_BACKEND_CPU = 0,
- # GGML_BACKEND_GPU = 10,
- # GGML_BACKEND_GPU_SPLIT = 20,
- # };
- GGML_BACKEND_CPU = 0
- GGML_BACKEND_GPU = 10
- GGML_BACKEND_GPU_SPLIT = 20
- # // model file types
- # enum ggml_ftype {
- # GGML_FTYPE_UNKNOWN = -1,
- # GGML_FTYPE_ALL_F32 = 0,
- # GGML_FTYPE_MOSTLY_F16 = 1, // except 1d tensors
- # GGML_FTYPE_MOSTLY_Q4_0 = 2, // except 1d tensors
- # GGML_FTYPE_MOSTLY_Q4_1 = 3, // except 1d tensors
- # GGML_FTYPE_MOSTLY_Q4_1_SOME_F16 = 4, // tok_embeddings.weight and output.weight are F16
- # GGML_FTYPE_MOSTLY_Q8_0 = 7, // except 1d tensors
- # GGML_FTYPE_MOSTLY_Q5_0 = 8, // except 1d tensors
- # GGML_FTYPE_MOSTLY_Q5_1 = 9, // except 1d tensors
- # GGML_FTYPE_MOSTLY_Q2_K = 10, // except 1d tensors
- # GGML_FTYPE_MOSTLY_Q3_K = 11, // except 1d tensors
- # GGML_FTYPE_MOSTLY_Q4_K = 12, // except 1d tensors
- # GGML_FTYPE_MOSTLY_Q5_K = 13, // except 1d tensors
- # GGML_FTYPE_MOSTLY_Q6_K = 14, // except 1d tensors
- # };
- GGML_FTYPE_UNKNOWN = -1
- GGML_FTYPE_ALL_F32 = 0
- GGML_FTYPE_MOSTLY_F16 = 1
- GGML_FTYPE_MOSTLY_Q4_0 = 2
- GGML_FTYPE_MOSTLY_Q4_1 = 3
- GGML_FTYPE_MOSTLY_Q4_1_SOME_F16 = 4
- GGML_FTYPE_MOSTLY_Q8_0 = 7
- GGML_FTYPE_MOSTLY_Q5_0 = 8
- GGML_FTYPE_MOSTLY_Q5_1 = 9
- GGML_FTYPE_MOSTLY_Q2_K = 10
- GGML_FTYPE_MOSTLY_Q3_K = 11
- GGML_FTYPE_MOSTLY_Q4_K = 12
- GGML_FTYPE_MOSTLY_Q5_K = 13
- GGML_FTYPE_MOSTLY_Q6_K = 14
- # // available tensor operations:
- # enum ggml_op {
- # GGML_OP_NONE = 0,
- # GGML_OP_DUP,
- # GGML_OP_ADD,
- # GGML_OP_ADD1,
- # GGML_OP_ACC,
- # GGML_OP_SUB,
- # GGML_OP_MUL,
- # GGML_OP_DIV,
- # GGML_OP_SQR,
- # GGML_OP_SQRT,
- # GGML_OP_LOG,
- # GGML_OP_SUM,
- # GGML_OP_SUM_ROWS,
- # GGML_OP_MEAN,
- # GGML_OP_ARGMAX,
- # GGML_OP_REPEAT,
- # GGML_OP_REPEAT_BACK,
- # GGML_OP_CONCAT,
- # GGML_OP_SILU_BACK,
- # GGML_OP_NORM, // normalize
- # GGML_OP_RMS_NORM,
- # GGML_OP_RMS_NORM_BACK,
- # GGML_OP_GROUP_NORM,
- # GGML_OP_MUL_MAT,
- # GGML_OP_OUT_PROD,
- # GGML_OP_SCALE,
- # GGML_OP_SET,
- # GGML_OP_CPY,
- # GGML_OP_CONT,
- # GGML_OP_RESHAPE,
- # GGML_OP_VIEW,
- # GGML_OP_PERMUTE,
- # GGML_OP_TRANSPOSE,
- # GGML_OP_GET_ROWS,
- # GGML_OP_GET_ROWS_BACK,
- # GGML_OP_DIAG,
- # GGML_OP_DIAG_MASK_INF,
- # GGML_OP_DIAG_MASK_ZERO,
- # GGML_OP_SOFT_MAX,
- # GGML_OP_SOFT_MAX_BACK,
- # GGML_OP_ROPE,
- # GGML_OP_ROPE_BACK,
- # GGML_OP_ALIBI,
- # GGML_OP_CLAMP,
- # GGML_OP_CONV_1D,
- # GGML_OP_CONV_2D,
- # GGML_OP_CONV_TRANSPOSE_2D,
- # GGML_OP_POOL_1D,
- # GGML_OP_POOL_2D,
- # GGML_OP_UPSCALE, // nearest interpolate
- # GGML_OP_FLASH_ATTN,
- # GGML_OP_FLASH_FF,
- # GGML_OP_FLASH_ATTN_BACK,
- # GGML_OP_WIN_PART,
- # GGML_OP_WIN_UNPART,
- # GGML_OP_GET_REL_POS,
- # GGML_OP_ADD_REL_POS,
- # GGML_OP_UNARY,
- # GGML_OP_MAP_UNARY,
- # GGML_OP_MAP_BINARY,
- # GGML_OP_MAP_CUSTOM1_F32,
- # GGML_OP_MAP_CUSTOM2_F32,
- # GGML_OP_MAP_CUSTOM3_F32,
- # GGML_OP_MAP_CUSTOM1,
- # GGML_OP_MAP_CUSTOM2,
- # GGML_OP_MAP_CUSTOM3,
- # GGML_OP_CROSS_ENTROPY_LOSS,
- # GGML_OP_CROSS_ENTROPY_LOSS_BACK,
- # GGML_OP_COUNT,
- # };
- GGML_OP_NONE = 0
- GGML_OP_DUP = 1
- GGML_OP_ADD = 2
- GGML_OP_ADD1 = 3
- GGML_OP_ACC = 4
- GGML_OP_SUB = 5
- GGML_OP_MUL = 6
- GGML_OP_DIV = 7
- GGML_OP_SQR = 8
- GGML_OP_SQRT = 9
- GGML_OP_LOG = 10
- GGML_OP_SUM = 11
- GGML_OP_SUM_ROWS = 12
- GGML_OP_MEAN = 13
- GGML_OP_ARGMAX = 14
- GGML_OP_REPEAT = 15
- GGML_OP_REPEAT_BACK = 16
- GGML_OP_CONCAT = 17
- GGML_OP_SILU_BACK = 18
- GGML_OP_NORM = 19
- GGML_OP_RMS_NORM = 20
- GGML_OP_RMS_NORM_BACK = 21
- GGML_OP_GROUP_NORM = 22
- GGML_OP_MUL_MAT = 23
- GGML_OP_OUT_PROD = 24
- GGML_OP_SCALE = 25
- GGML_OP_SET = 26
- GGML_OP_CPY = 27
- GGML_OP_CONT = 28
- GGML_OP_RESHAPE = 29
- GGML_OP_VIEW = 30
- GGML_OP_PERMUTE = 31
- GGML_OP_TRANSPOSE = 32
- GGML_OP_GET_ROWS = 33
- GGML_OP_GET_ROWS_BACK = 34
- GGML_OP_DIAG = 35
- GGML_OP_DIAG_MASK_INF = 36
- GGML_OP_DIAG_MASK_ZERO = 37
- GGML_OP_SOFT_MAX = 38
- GGML_OP_SOFT_MAX_BACK = 39
- GGML_OP_ROPE = 40
- GGML_OP_ROPE_BACK = 41
- GGML_OP_ALIBI = 42
- GGML_OP_CLAMP = 43
- GGML_OP_CONV_1D = 44
- GGML_OP_CONV_2D = 45
- GGML_OP_CONV_TRANSPOSE_2D = 46
- GGML_OP_POOL_1D = 47
- GGML_OP_POOL_2D = 48
- GGML_OP_UPSCALE = 49
- GGML_OP_FLASH_ATTN = 50
- GGML_OP_FLASH_FF = 51
- GGML_OP_FLASH_ATTN_BACK = 52
- GGML_OP_WIN_PART = 53
- GGML_OP_WIN_UNPART = 54
- GGML_OP_GET_REL_POS = 55
- GGML_OP_ADD_REL_POS = 56
- GGML_OP_UNARY = 57
- GGML_OP_MAP_UNARY = 58
- GGML_OP_MAP_BINARY = 59
- GGML_OP_MAP_CUSTOM1_F32 = 60
- GGML_OP_MAP_CUSTOM2_F32 = 61
- GGML_OP_MAP_CUSTOM3_F32 = 62
- GGML_OP_MAP_CUSTOM1 = 63
- GGML_OP_MAP_CUSTOM2 = 64
- GGML_OP_MAP_CUSTOM3 = 65
- GGML_OP_CROSS_ENTROPY_LOSS = 66
- GGML_OP_CROSS_ENTROPY_LOSS_BACK = 67
- GGML_OP_COUNT = 68
- # enum ggml_unary_op {
- # GGML_UNARY_OP_ABS,
- # GGML_UNARY_OP_SGN,
- # GGML_UNARY_OP_NEG,
- # GGML_UNARY_OP_STEP,
- # GGML_UNARY_OP_TANH,
- # GGML_UNARY_OP_ELU,
- # GGML_UNARY_OP_RELU,
- # GGML_UNARY_OP_GELU,
- # GGML_UNARY_OP_GELU_QUICK,
- # GGML_UNARY_OP_SILU,
- # };
- GGML_UNARY_OP_ABS = 0
- GGML_UNARY_OP_SGN = 1
- GGML_UNARY_OP_NEG = 2
- GGML_UNARY_OP_STEP = 3
- GGML_UNARY_OP_TANH = 4
- GGML_UNARY_OP_ELU = 5
- GGML_UNARY_OP_RELU = 6
- GGML_UNARY_OP_GELU = 7
- GGML_UNARY_OP_GELU_QUICK = 8
- GGML_UNARY_OP_SILU = 9
- # enum ggml_object_type {
- # GGML_OBJECT_TENSOR,
- # GGML_OBJECT_GRAPH,
- # GGML_OBJECT_WORK_BUFFER
- # };
- GGML_OBJECT_TENSOR = 0
- GGML_OBJECT_GRAPH = 1
- GGML_OBJECT_WORK_BUFFER = 2
- # // ggml object
- # struct ggml_object {
- # size_t offs;
- # size_t size;
- # struct ggml_object * next;
- # enum ggml_object_type type;
- # char padding[4];
- # };
- class ggml_object(ctypes.Structure):
- pass
- ggml_object._fields_ = [
- ("offs", ctypes.c_size_t),
- ("size", ctypes.c_size_t),
- ("next", ctypes.POINTER(ggml_object)),
- ("type", ctypes.c_int),
- ("padding", ctypes.c_char * 4),
- ]
- ggml_object_p: TypeAlias = "ctypes._Pointer[ggml_object]" # type: ignore
- GGML_OBJECT_SIZE = ctypes.sizeof(ggml_object)
- # // n-dimensional tensor
- # struct ggml_tensor {
- # enum ggml_type type;
- # enum ggml_backend backend;
- # int n_dims;
- # int64_t ne[GGML_MAX_DIMS]; // number of elements
- # size_t nb[GGML_MAX_DIMS]; // stride in bytes:
- # // nb[0] = sizeof(type)
- # // nb[1] = nb[0] * ne[0] + padding
- # // nb[i] = nb[i-1] * ne[i-1]
- # // compute data
- # enum ggml_op op;
- # // op params - allocated as int32_t for alignment
- # int32_t op_params[GGML_MAX_OP_PARAMS / sizeof(int32_t)];
- # bool is_param;
- # struct ggml_tensor * grad;
- # struct ggml_tensor * src[GGML_MAX_SRC];
- # // performance
- # int perf_runs;
- # int64_t perf_cycles;
- # int64_t perf_time_us;
- # struct ggml_tensor * view_src;
- # size_t view_offs;
- # void * data;
- # char name[GGML_MAX_NAME];
- # void * extra; // extra things e.g. for ggml-cuda.cu
- # char padding[4];
- # };
- class ggml_tensor(ctypes.Structure):
- """n-dimensional tensor
- Attributes:
- type (int): ggml_type
- backend (int): ggml_backend
- n_dims (int): number of dimensions
- ne (ctypes.Array[ctypes.c_int64]): number of elements in each dimension
- nb (ctypes.Array[ctypes.c_size_t]): stride in bytes for each dimension
- op (int): ggml operation
- op_params (ctypes.Array[ctypes.c_int32]): `GGML_MAX_OP_PARAMS`-length array of operation parameters
- is_param (bool): is this a parameter tensor
- grad (ggml_tensor_p): reference to gradient tensor
- src (ctypes.Array[ggml_tensor_p]): `GGML_MAX_SRC`-length array of source tensors
- perf_runs (int): number of performance runs
- perf_cycles (int): number of cycles
- perf_time_us (int): time in microseconds
- view_src (ggml_tensor_p): pointer to tensor if this tensor is a view, None if the tensor is not a view
- view_offs (ctypes.c_size_t): offset into the data pointer of the view tensor
- data (ctypes.c_void_p): reference to raw tensor data
- name (bytes): name of tensor
- extra (ctypes.c_void_p): extra data (e.g. for CUDA)
- """
- pass
- ggml_tensor._fields_ = [
- ("type", ctypes.c_int),
- ("backend", ctypes.c_int),
- ("n_dims", ctypes.c_int),
- ("ne", ctypes.c_int64 * GGML_MAX_DIMS),
- ("nb", ctypes.c_size_t * GGML_MAX_DIMS),
- ("op", ctypes.c_int),
- (
- "op_params",
- ctypes.c_int32 * (GGML_MAX_OP_PARAMS // ctypes.sizeof(ctypes.c_int32)),
- ),
- ("is_param", ctypes.c_bool),
- ("grad", ctypes.POINTER(ggml_tensor)),
- ("src", ctypes.POINTER(ggml_tensor) * GGML_MAX_SRC),
- ("perf_runs", ctypes.c_int),
- ("perf_cycles", ctypes.c_int64),
- ("perf_time_us", ctypes.c_int64),
- ("view_src", ctypes.POINTER(ggml_tensor)),
- ("view_offs", ctypes.c_size_t),
- ("data", ctypes.c_void_p),
- ("name", ctypes.c_char * GGML_MAX_NAME),
- ("extra", ctypes.c_void_p),
- ("padding", ctypes.c_char * 4),
- ]
- GGML_TENSOR_SIZE = ctypes.sizeof(ggml_tensor)
- ggml_tensor_p: TypeAlias = "ctypes._Pointer[ggml_tensor]" # type: ignore
- """ctypes pointer to a [ggml_tensor][ggml.ggml_tensor]
- Can be dereferenced to a [ggml_tensor][ggml.ggml_tensor] object using
- the `.contents` attribute."""
- abort_callback_t = ctypes.CFUNCTYPE(ctypes.c_bool, ctypes.c_void_p)
- # // the compute plan that needs to be prepared for ggml_graph_compute()
- # // since https://github.com/ggerganov/ggml/issues/287
- # struct ggml_cplan {
- # size_t work_size; // size of work buffer, calculated by `ggml_graph_plan()`
- # uint8_t * work_data; // work buffer, to be allocated by caller before calling to `ggml_graph_compute()`
- # int n_threads;
- # // the `n_tasks` of nodes, 1:1 mapping to cgraph nodes
- # int n_tasks[GGML_MAX_NODES];
- # // abort ggml_graph_compute when true
- # bool (*abort_callback)(void * data);
- # void * abort_callback_data;
- # };
- class ggml_cplan(ctypes.Structure):
- """Compute plan for a ggml computation graph
- Attributes:
- work_size (int): size of work buffer
- work_data (ctypes.POINTER(ctypes.c_uint8)): work buffer
- n_threads (int): number of threads to use when computing the graph using [ggml_graph_compute][ggml.ggml_graph_compute]
- n_tasks (ctypes.Array[ctypes.c_int]): `n_tasks` of nodes, 1:1 mapping to cgraph nodes
- abort_callback (abort_callback_t): abort callback
- abort_callback_data (ctypes.c_void_p): abort callback data
- """
- _fields_ = [
- ("work_size", ctypes.c_size_t),
- ("work_data", ctypes.POINTER(ctypes.c_uint8)),
- ("n_threads", ctypes.c_int),
- ("n_tasks", ctypes.c_int * GGML_MAX_NODES),
- (
- "abort_callback",
- abort_callback_t,
- ),
- ("abort_callback_data", ctypes.c_void_p),
- ]
- GGML_CPLAN_SIZE = ctypes.sizeof(ggml_cplan)
- ggml_cplan_p: TypeAlias = "ctypes._Pointer[ggml_cplan]" # type: ignore
- """ctypes pointer to a [ggml_cplan][ggml.ggml_cplan]
- Can be dereferenced to a [ggml_cplan][ggml.ggml_cplan] object using
- the `.contents` attribute."""
- # // next prime after GGML_MAX_NODES
- # // #define GGML_GRAPH_HASHTABLE_SIZE 4099
- # // next prime after GGML_MAX_NODES * 2 (nodes + leafs)
- # #define GGML_GRAPH_HASHTABLE_SIZE 8273
- GGML_GRAPH_HASHTABLE_SIZE = 8273
- # // computation graph
- # struct ggml_cgraph {
- # int n_nodes;
- # int n_leafs;
- # struct ggml_tensor * nodes[GGML_MAX_NODES];
- # struct ggml_tensor * grads[GGML_MAX_NODES];
- # struct ggml_tensor * leafs[GGML_MAX_NODES];
- # void * visited_hash_table[GGML_GRAPH_HASHTABLE_SIZE];
- # // performance
- # int perf_runs;
- # int64_t perf_cycles;
- # int64_t perf_time_us;
- # };
- class ggml_cgraph(ctypes.Structure):
- """ggml computation graph
- Attributes:
- n_nodes (int): number of nodes
- n_leafs (int): number of leafs
- nodes (ctypes.Array[ggml_tensor_p]): `n_nodes`-length array of compute tensors
- grads (ctypes.Array[ggml_tensor_p]): `n_nodes`-length array of gradient tensors
- leafs (ctypes.Array[ggml_tensor_p]): `n_leafs`-length array of parameter tensors
- visited_hash_table (ctypes.Array[ctypes.c_void_p]): `GGML_GRAPH_HASHTABLE_SIZE`-length array of visited nodes
- perf_runs (int): number of runs
- perf_cycles (int): number of cycles
- perf_time_us (int): computation time in microseconds"""
- _fields_ = [
- ("n_nodes", ctypes.c_int),
- ("n_leafs", ctypes.c_int),
- ("nodes", ctypes.POINTER(ggml_tensor) * GGML_MAX_NODES),
- ("grads", ctypes.POINTER(ggml_tensor) * GGML_MAX_NODES),
- ("leafs", ctypes.POINTER(ggml_tensor) * GGML_MAX_NODES),
- ("visited_hash_table", ctypes.c_void_p * GGML_GRAPH_HASHTABLE_SIZE),
- ("perf_runs", ctypes.c_int),
- ("perf_cycles", ctypes.c_int64),
- ("perf_time_us", ctypes.c_int64),
- ]
- ggml_cgraph_p: TypeAlias = "ctypes._Pointer[ggml_cgraph]" # type: ignore
- """ctypes pointer to a [ggml_cgraph][ggml.ggml_cgraph]
- Can be dereferenced to a [ggml_cgraph][ggml.ggml_cgraph] object using
- the `.contents` attribute."""
- # static const size_t GGML_GRAPH_SIZE = sizeof(struct ggml_cgraph);
- GGML_GRAPH_SIZE = ctypes.sizeof(ggml_cgraph)
- # struct ggml_scratch {
- # size_t offs;
- # size_t size;
- # void * data;
- # };
- class ggml_scratch(ctypes.Structure):
- _fields_ = [
- ("offs", ctypes.c_size_t),
- ("size", ctypes.c_size_t),
- ("data", ctypes.c_void_p),
- ]
- # struct ggml_init_params {
- # // memory pool
- # size_t mem_size; // bytes
- # void * mem_buffer; // if NULL, memory will be allocated internally
- # bool no_alloc; // don't allocate memory for the tensor data
- # };
- class ggml_init_params(ctypes.Structure):
- """Initialization parameters for a ggml context
- **NOTE**: Reference counting does not cross into ggml, if you allocate a memory buffer
- in python using ctypes Arrays or a numpy array, you must keep a reference to it until
- you free the ggml context otherwise you will encounter a segmentation fault.
- Attributes:
- mem_size (int): size of memory pool in bytes
- mem_buffer (ctypes.c_void_p): pointer to memory pool, if None, memory will be allocated internally
- no_alloc (bool): don't allocate memory for tensor data
- """
- _fields_ = [
- ("mem_size", ctypes.c_size_t),
- ("mem_buffer", ctypes.c_void_p),
- ("no_alloc", ctypes.c_bool),
- ]
- # // compute types
- # // NOTE: the INIT or FINALIZE pass is not scheduled unless explicitly enabled.
- # // This behavior was changed since https://github.com/ggerganov/llama.cpp/pull/1995.
- # enum ggml_task_type {
- # GGML_TASK_INIT = 0,
- # GGML_TASK_COMPUTE,
- # GGML_TASK_FINALIZE,
- # };
- GGML_TASK_INIT = 0
- GGML_TASK_COMPUTE = 1
- GGML_TASK_FINALIZE = 2
- # struct ggml_compute_params {
- # enum ggml_task_type type;
- # // ith = thread index, nth = number of threads
- # int ith, nth;
- # // work buffer for all threads
- # size_t wsize;
- # void * wdata;
- # };
- class ggml_compute_params(ctypes.Structure):
- _fields_ = [
- ("type", ctypes.c_int),
- ("ith", ctypes.c_int),
- ("nth", ctypes.c_int),
- ("wsize", ctypes.c_size_t),
- ("wdata", ctypes.c_void_p),
- ]
- ggml_compute_params_p: TypeAlias = "ctypes._Pointer[ggml_compute_params]" # type: ignore
- # // misc
- # GGML_API void ggml_time_init(void); // call this once at the beginning of the program
- def ggml_time_init():
- return lib.ggml_time_init()
- lib.ggml_time_init.argtypes = []
- lib.ggml_time_init.restype = None
- # GGML_API int64_t ggml_time_ms(void);
- def ggml_time_ms() -> int:
- return lib.ggml_time_ms()
- lib.ggml_time_ms.argtypes = []
- lib.ggml_time_ms.restype = ctypes.c_int64
- # GGML_API int64_t ggml_time_us(void);
- def ggml_time_us() -> int:
- return lib.ggml_time_us()
- lib.ggml_time_us.argtypes = []
- lib.ggml_time_us.restype = ctypes.c_int64
- # GGML_API int64_t ggml_cycles(void);
- def ggml_cycles() -> int:
- return lib.ggml_cycles()
- lib.ggml_cycles.argtypes = []
- lib.ggml_cycles.restype = ctypes.c_int64
- # GGML_API int64_t ggml_cycles_per_ms(void);
- def ggml_cycles_per_ms() -> int:
- return lib.ggml_cycles_per_ms()
- lib.ggml_cycles_per_ms.argtypes = []
- lib.ggml_cycles_per_ms.restype = ctypes.c_int64
- # GGML_API void ggml_numa_init(void); // call once for better performance on NUMA systems
- def ggml_numa_init():
- return lib.ggml_numa_init()
- lib.ggml_numa_init.argtypes = []
- lib.ggml_numa_init.restype = None
- # GGML_API bool ggml_is_numa(void); // true if init detected that system has >1 NUMA node
- def ggml_is_numa() -> bool:
- return lib.ggml_is_numa()
- lib.ggml_is_numa.argtypes = []
- lib.ggml_is_numa.restype = ctypes.c_bool
- # GGML_API void ggml_print_object (const struct ggml_object * obj);
- def ggml_print_object(obj: ggml_object_p):
- return lib.ggml_print_object(obj)
- lib.ggml_print_object.argtypes = [ctypes.POINTER(ggml_object)]
- lib.ggml_print_object.restype = None
- # GGML_API void ggml_print_objects(const struct ggml_context * ctx);
- def ggml_print_objects(ctx: ggml_context_p):
- return lib.ggml_print_objects(ctx)
- lib.ggml_print_objects.argtypes = [ggml_context_p]
- lib.ggml_print_objects.restype = None
- # GGML_API int64_t ggml_nelements (const struct ggml_tensor * tensor);
- def ggml_nelements(
- tensor: ggml_tensor_p,
- ) -> int:
- """Get the number of elements in a tensor
- Parameters:
- tensor: tensor
- Returns:
- number of elements"""
- return lib.ggml_nelements(tensor)
- lib.ggml_nelements.argtypes = [ctypes.POINTER(ggml_tensor)]
- lib.ggml_nelements.restype = ctypes.c_int64
- # GGML_API int64_t ggml_nrows (const struct ggml_tensor * tensor);
- def ggml_nrows(
- tensor: ggml_tensor_p,
- ) -> int:
- """Get the number of rows in a tensor
- Parameters:
- tensor: tensor
- Returns:
- number of rows"""
- return lib.ggml_nrows(tensor)
- lib.ggml_nrows.argtypes = [ctypes.POINTER(ggml_tensor)]
- lib.ggml_nrows.restype = ctypes.c_int64
- # GGML_API size_t ggml_nbytes (const struct ggml_tensor * tensor);
- def ggml_nbytes(
- tensor: ggml_tensor_p,
- ) -> int:
- """Get the number of bytes required to store tensor data
- Parameters:
- tensor: tensor
- Returns:
- number of bytes"""
- return lib.ggml_nbytes(tensor)
- lib.ggml_nbytes.argtypes = [ctypes.POINTER(ggml_tensor)]
- lib.ggml_nbytes.restype = ctypes.c_size_t
- # GGML_API size_t ggml_nbytes_pad (const struct ggml_tensor * tensor); // same as ggml_nbytes() but padded to GGML_MEM_ALIGN
- def ggml_nbytes_pad(
- tensor: ggml_tensor_p,
- ) -> int:
- """Get the number of bytes required to store tensor data, padded to GGML_MEM_ALIGN
- Parameters:
- tensor: tensor
- Returns:
- number of bytes"""
- return lib.ggml_nbytes_pad(tensor)
- lib.ggml_nbytes_pad.argtypes = [ctypes.POINTER(ggml_tensor)]
- lib.ggml_nbytes_pad.restype = ctypes.c_size_t
- # GGML_API size_t ggml_nbytes_split(const struct ggml_tensor * tensor, int nrows_split);
- def ggml_nbytes_split(
- tensor: ggml_tensor_p,
- nrows_split: Union[ctypes.c_int, int],
- ) -> int:
- return lib.ggml_nbytes_split(tensor, nrows_split)
- lib.ggml_nbytes_split.argtypes = [ctypes.POINTER(ggml_tensor), ctypes.c_int]
- lib.ggml_nbytes_split.restype = ctypes.c_size_t
- # GGML_API int ggml_blck_size (enum ggml_type type);
- def ggml_blck_size(type: Union[ctypes.c_int, int]) -> int:
- return lib.ggml_blck_size(type)
- lib.ggml_blck_size.argtypes = [ctypes.c_int]
- lib.ggml_blck_size.restype = ctypes.c_int
- # GGML_API size_t ggml_type_size (enum ggml_type type); // size in bytes for all elements in a block
- def ggml_type_size(type: Union[ctypes.c_int, int]) -> int:
- return lib.ggml_type_size(type)
- lib.ggml_type_size.argtypes = [ctypes.c_int]
- lib.ggml_type_size.restype = ctypes.c_size_t
- # GGML_API float ggml_type_sizef(enum ggml_type type); // ggml_type_size()/ggml_blck_size() as float
- def ggml_type_sizef(type: Union[ctypes.c_int, int]) -> float:
- return lib.ggml_type_sizef(type)
- lib.ggml_type_sizef.argtypes = [ctypes.c_int]
- lib.ggml_type_sizef.restype = ctypes.c_float
- # GGML_API const char * ggml_type_name(enum ggml_type type);
- def ggml_type_name(type: Union[ctypes.c_int, int]) -> bytes:
- return lib.ggml_type_name(type)
- lib.ggml_type_name.argtypes = [ctypes.c_int]
- lib.ggml_type_name.restype = ctypes.c_char_p
- # GGML_API const char * ggml_op_name (enum ggml_op op);
- def ggml_op_name(op: Union[ctypes.c_int, int]) -> bytes:
- return lib.ggml_op_name(op)
- lib.ggml_op_name.argtypes = [ctypes.c_int]
- lib.ggml_op_name.restype = ctypes.c_char_p
- # GGML_API const char * ggml_op_symbol(enum ggml_op op);
- def ggml_op_symbol(op: Union[ctypes.c_int, int]) -> bytes:
- return lib.ggml_op_symbol(op)
- lib.ggml_op_symbol.argtypes = [ctypes.c_int]
- lib.ggml_op_symbol.restype = ctypes.c_char_p
- # GGML_API size_t ggml_element_size(const struct ggml_tensor * tensor);
- def ggml_element_size(
- tensor: ggml_tensor_p,
- ) -> int:
- return lib.ggml_element_size(tensor)
- lib.ggml_element_size.argtypes = [ctypes.POINTER(ggml_tensor)]
- lib.ggml_element_size.restype = ctypes.c_size_t
- # GGML_API bool ggml_is_quantized(enum ggml_type type);
- def ggml_is_quantized(type: Union[ctypes.c_int, int]) -> bool:
- return lib.ggml_is_quantized(type)
- lib.ggml_is_quantized.argtypes = [ctypes.c_int]
- lib.ggml_is_quantized.restype = ctypes.c_bool
- # // TODO: temporary until model loading of ggml examples is refactored
- # GGML_API enum ggml_type ggml_ftype_to_ggml_type(enum ggml_ftype ftype);
- def ggml_ftype_to_ggml_type(ftype: Union[ctypes.c_int, int]) -> int:
- return lib.ggml_ftype_to_ggml_type(ftype)
- lib.ggml_ftype_to_ggml_type.argtypes = [ctypes.c_int]
- lib.ggml_ftype_to_ggml_type.restype = ctypes.c_int
- # GGML_API bool ggml_is_transposed(const struct ggml_tensor * tensor);
- def ggml_is_transposed(
- tensor: ggml_tensor_p,
- ) -> bool:
- """Check if a tensor is transposed
- Parameters:
- tensor: tensor
- Returns:
- True if tensor is transposed else False"""
- return lib.ggml_is_transposed(tensor)
- lib.ggml_is_transposed.argtypes = [ctypes.POINTER(ggml_tensor)]
- lib.ggml_is_transposed.restype = ctypes.c_bool
- # GGML_API bool ggml_is_contiguous(const struct ggml_tensor * tensor);
- def ggml_is_contiguous(
- tensor: ggml_tensor_p,
- ) -> bool:
- """Check if a tensor is contiguous
- Parameters:
- tensor: tensor
- Returns:
- True if tensor is contiguous else False"""
- return lib.ggml_is_contiguous(tensor)
- lib.ggml_is_contiguous.argtypes = [ctypes.POINTER(ggml_tensor)]
- lib.ggml_is_contiguous.restype = ctypes.c_bool
- # GGML_API bool ggml_is_permuted (const struct ggml_tensor * tensor);
- def ggml_is_permuted(
- tensor: ggml_tensor_p,
- ) -> bool:
- """Check if a tensor is permuted
- Parameters:
- tensor: tensor
- Returns:
- True if tensor is permuted else False"""
- return lib.ggml_is_permuted(tensor)
- lib.ggml_is_permuted.argtypes = [ctypes.POINTER(ggml_tensor)]
- lib.ggml_is_permuted.restype = ctypes.c_bool
- # GGML_API bool ggml_are_same_shape(const struct ggml_tensor * t0, const struct ggml_tensor * t1);
- def ggml_are_same_shape(
- t0: ggml_tensor_p,
- t1: ggml_tensor_p,
- ) -> bool:
- """Check if two tensors have the same shape
- Parameters:
- t0: tensor 0
- t1: tensor 1
- Returns:
- True if tensors have the same shape else False"""
- return lib.ggml_are_same_shape(t0, t1)
- lib.ggml_are_same_shape.argtypes = [
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_are_same_shape.restype = ctypes.c_bool
- # // use this to compute the memory overhead of a tensor
- # GGML_API size_t ggml_tensor_overhead(void);
- def ggml_tensor_overhead() -> int:
- """Overhead required for a tensor struct in bytes
- Returns:
- size of tensor struct in bytes"""
- return lib.ggml_tensor_overhead()
- lib.ggml_tensor_overhead.argtypes = []
- lib.ggml_tensor_overhead.restype = ctypes.c_size_t
- # // main
- # GGML_API struct ggml_context * ggml_init(struct ggml_init_params params);
- def ggml_init(
- params: ggml_init_params,
- ) -> ggml_context_p:
- """Instantiate a new ggml context with params.
- You must call `ggml_free()` to free the context.
- Parameters:
- params: ggml init params
- Returns:
- Pointer to ggml_context"""
- return lib.ggml_init(params)
- lib.ggml_init.argtypes = [ggml_init_params]
- lib.ggml_init.restype = ggml_context_p
- # GGML_API void ggml_free(struct ggml_context * ctx);
- def ggml_free(ctx: ggml_context_p):
- """Free the ggml context.
- Parameters:
- ctx: ggml context"""
- return lib.ggml_free(ctx)
- lib.ggml_free.argtypes = [ggml_context_p]
- lib.ggml_free.restype = None
- # GGML_API size_t ggml_used_mem(const struct ggml_context * ctx);
- def ggml_used_mem(ctx: ggml_context_p) -> int:
- """Return the amount of memory used by the ggml context in bytes.
- Parameters:
- ctx: ggml context
- Returns:
- amount of memory used in bytes"""
- return lib.ggml_used_mem(ctx)
- lib.ggml_used_mem.argtypes = [ggml_context_p]
- lib.ggml_used_mem.restype = ctypes.c_size_t
- # GGML_API size_t ggml_set_scratch(struct ggml_context * ctx, struct ggml_scratch scratch);
- def ggml_set_scratch(ctx: ggml_context_p, scratch: ggml_scratch) -> int:
- """Set the scratch buffer for the ggml context."""
- return lib.ggml_set_scratch(ctx, scratch)
- lib.ggml_set_scratch.argtypes = [ggml_context_p, ggml_scratch]
- lib.ggml_set_scratch.restype = ctypes.c_size_t
- # GGML_API bool ggml_get_no_alloc(struct ggml_context * ctx);
- def ggml_get_no_alloc(ctx: ggml_context_p) -> bool:
- """Return the no_alloc flag for the ggml context."""
- return lib.ggml_get_no_alloc(ctx)
- lib.ggml_get_no_alloc.argtypes = [ggml_context_p]
- lib.ggml_get_no_alloc.restype = ctypes.c_bool
- # GGML_API void ggml_set_no_alloc(struct ggml_context * ctx, bool no_alloc);
- def ggml_set_no_alloc(ctx: ggml_context_p, no_alloc: Union[ctypes.c_bool, bool]):
- """Set the no_alloc flag for the ggml context."""
- return lib.ggml_set_no_alloc(ctx, no_alloc)
- lib.ggml_set_no_alloc.argtypes = [ggml_context_p, ctypes.c_bool]
- lib.ggml_set_no_alloc.restype = None
- # GGML_API void * ggml_get_mem_buffer (struct ggml_context * ctx);
- def ggml_get_mem_buffer(ctx: ggml_context_p) -> Optional[ctypes.c_void_p]:
- """Return the memory buffer for the ggml context."""
- return lib.ggml_get_mem_buffer(ctx)
- lib.ggml_get_mem_buffer.argtypes = [ggml_context_p]
- lib.ggml_get_mem_buffer.restype = ctypes.c_void_p
- # GGML_API size_t ggml_get_mem_size (struct ggml_context * ctx);
- def ggml_get_mem_size(ctx: ggml_context_p) -> int:
- """Return the size of the memory buffer for the ggml context in bytes."""
- return lib.ggml_get_mem_size(ctx)
- lib.ggml_get_mem_size.argtypes = [ggml_context_p]
- lib.ggml_get_mem_size.restype = ctypes.c_size_t
- # GGML_API size_t ggml_get_max_tensor_size(const struct ggml_context * ctx);
- def ggml_get_max_tensor_size(ctx: ggml_context_p) -> int:
- """Return the maximum size of a tensor in bytes."""
- return lib.ggml_get_max_tensor_size(ctx)
- lib.ggml_get_max_tensor_size.argtypes = [ggml_context_p]
- lib.ggml_get_max_tensor_size.restype = ctypes.c_size_t
- # GGML_API struct ggml_tensor * ggml_new_tensor(
- # struct ggml_context * ctx,
- # enum ggml_type type,
- # int n_dims,
- # const int64_t *ne);
- def ggml_new_tensor(
- ctx: ggml_context_p,
- type: Union[ctypes.c_int, int],
- n_dims: Union[ctypes.c_int, int],
- ne: CInt64Array,
- ) -> ggml_tensor_p:
- """Create a new tensor with the given type, number of dimensions, and number of elements in each dimension.
- Parameters:
- ctx: ggml context
- type: ggml type
- n_dims: number of dimensions
- ne (ctypes.Array[ctypes.c_int64]): number of elements in each dimension (array of length n_dims)
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_new_tensor(ctx, type, n_dims, ne)
- lib.ggml_new_tensor.argtypes = [
- ggml_context_p,
- ctypes.c_int,
- ctypes.c_int,
- ctypes.POINTER(ctypes.c_int64),
- ]
- lib.ggml_new_tensor.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_new_tensor_1d(
- # struct ggml_context * ctx,
- # enum ggml_type type,
- # int64_t ne0);
- def ggml_new_tensor_1d(
- ctx: ggml_context_p, type: Union[ctypes.c_int, int], ne0: Union[ctypes.c_int64, int]
- ) -> ggml_tensor_p:
- """Create a new 1-dimensional tensor with the given type and number of elements.
- Parameters:
- ctx: ggml context
- type: ggml type
- ne0: number of elements in dimension 0
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_new_tensor_1d(ctx, type, ne0)
- lib.ggml_new_tensor_1d.argtypes = [ggml_context_p, ctypes.c_int, ctypes.c_int64]
- lib.ggml_new_tensor_1d.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_new_tensor_2d(
- # struct ggml_context * ctx,
- # enum ggml_type type,
- # int64_t ne0,
- # int64_t ne1);
- def ggml_new_tensor_2d(
- ctx: ggml_context_p,
- type: Union[ctypes.c_int, int],
- ne0: Union[ctypes.c_int64, int],
- ne1: Union[ctypes.c_int64, int],
- ) -> ggml_tensor_p:
- """Create a new 2-dimensional tensor with the given type and number of elements in each dimension.
- Parameters:
- ctx: ggml context
- type: ggml type
- ne0: number of elements in dimension 0
- ne1: number of elements in dimension 1
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_new_tensor_2d(ctx, type, ne0, ne1)
- lib.ggml_new_tensor_2d.argtypes = [
- ggml_context_p,
- ctypes.c_int,
- ctypes.c_int64,
- ctypes.c_int64,
- ]
- lib.ggml_new_tensor_2d.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_new_tensor_3d(
- # struct ggml_context * ctx,
- # enum ggml_type type,
- # int64_t ne0,
- # int64_t ne1,
- # int64_t ne2);
- def ggml_new_tensor_3d(
- ctx: ggml_context_p,
- type: Union[ctypes.c_int, int],
- ne0: Union[ctypes.c_int64, int],
- ne1: Union[ctypes.c_int64, int],
- ne2: Union[ctypes.c_int64, int],
- ) -> ggml_tensor_p:
- """Create a new 3-dimensional tensor with the given type and number of elements in each dimension.
- Parameters:
- ctx: ggml context
- type: ggml type
- ne0: number of elements in dimension 0
- ne1: number of elements in dimension 1
- ne2: number of elements in dimension 2
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_new_tensor_3d(ctx, type, ne0, ne1, ne2)
- lib.ggml_new_tensor_3d.argtypes = [
- ggml_context_p,
- ctypes.c_int,
- ctypes.c_int64,
- ctypes.c_int64,
- ctypes.c_int64,
- ]
- lib.ggml_new_tensor_3d.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_new_tensor_4d(
- # struct ggml_context * ctx,
- # enum ggml_type type,
- # int64_t ne0,
- # int64_t ne1,
- # int64_t ne2,
- # int64_t ne3);
- def ggml_new_tensor_4d(
- ctx: ggml_context_p,
- type: Union[ctypes.c_int, int],
- ne0: Union[ctypes.c_int64, int],
- ne1: Union[ctypes.c_int64, int],
- ne2: Union[ctypes.c_int64, int],
- ne3: Union[ctypes.c_int64, int],
- ) -> ggml_tensor_p:
- """Create a new 4-dimensional tensor with the given type and number of elements in each dimension.
- Parameters:
- ctx: ggml context
- type: ggml type
- ne0: number of elements in dimension 0
- ne1: number of elements in dimension 1
- ne2: number of elements in dimension 2
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_new_tensor_4d(ctx, type, ne0, ne1, ne2, ne3)
- lib.ggml_new_tensor_4d.argtypes = [
- ggml_context_p,
- ctypes.c_int,
- ctypes.c_int64,
- ctypes.c_int64,
- ctypes.c_int64,
- ctypes.c_int64,
- ]
- lib.ggml_new_tensor_4d.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_new_i32(struct ggml_context * ctx, int32_t value);
- def ggml_new_i32(
- ctx: ggml_context_p, value: Union[ctypes.c_int32, int]
- ) -> ggml_tensor_p:
- """Create a 1 element tensor with the given integer value.
- Parameters:
- ctx: ggml context
- value: integer value
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_new_i32(ctx, value)
- lib.ggml_new_i32.argtypes = [ggml_context_p, ctypes.c_int32]
- lib.ggml_new_i32.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_new_f32(struct ggml_context * ctx, float value);
- def ggml_new_f32(
- ctx: ggml_context_p,
- value: Union[ctypes.c_float, float],
- ) -> ggml_tensor_p:
- """Create a 1 element tensor with the given float value.
- Parameters:
- ctx: ggml context
- value: float value
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_new_f32(ctx, value)
- lib.ggml_new_f32.argtypes = [ggml_context_p, ctypes.c_float]
- lib.ggml_new_f32.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_dup_tensor (struct ggml_context * ctx, const struct ggml_tensor * src);
- def ggml_dup_tensor(ctx: ggml_context_p, src: ggml_tensor_p) -> ggml_tensor_p:
- """Create a new tensor with the same type and dimensions as the source tensor.
- Parameters:
- ctx: ggml context
- src: source tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_dup_tensor(ctx, src)
- lib.ggml_dup_tensor.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_dup_tensor.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_view_tensor(struct ggml_context * ctx, struct ggml_tensor * src);
- def ggml_view_tensor(ctx: ggml_context_p, src: ggml_tensor_p) -> ggml_tensor_p:
- """Create a new tensor with the same type, dimensions and data as the source tensor.
- Parameters:
- ctx: ggml context
- src: source tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_view_tensor(ctx, src)
- lib.ggml_view_tensor.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_view_tensor.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_get_tensor(struct ggml_context * ctx, const char * name);
- def ggml_get_tensor(ctx: ggml_context_p, name: bytes) -> ggml_tensor_p:
- """Get a tensor from the ggml context by name.
- Parameters:
- ctx: ggml context
- name: name of tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_get_tensor(ctx, name)
- lib.ggml_get_tensor.argtypes = [ggml_context_p, ctypes.c_char_p]
- lib.ggml_get_tensor.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_set_zero(struct ggml_tensor * tensor);
- def ggml_set_zero(
- tensor: ggml_tensor_p,
- ) -> ggml_tensor_p:
- """Zero all elements in a tensor.
- Parameters:
- tensor: tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_set_zero(tensor)
- lib.ggml_set_zero.argtypes = [ctypes.POINTER(ggml_tensor)]
- lib.ggml_set_zero.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_set_i32 (struct ggml_tensor * tensor, int32_t value);
- def ggml_set_i32(
- tensor: ggml_tensor_p,
- value: Union[ctypes.c_int32, int],
- ) -> ggml_tensor_p:
- """Set all elements in a tensor to the given integer value.
- Parameters:
- tensor: tensor
- value: integer value
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_set_i32(tensor, value)
- lib.ggml_set_i32.argtypes = [ctypes.POINTER(ggml_tensor), ctypes.c_int32]
- lib.ggml_set_i32.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_set_f32 (struct ggml_tensor * tensor, float value);
- def ggml_set_f32(
- tensor: ggml_tensor_p,
- value: Union[ctypes.c_float, float],
- ) -> ggml_tensor_p:
- """Set all elements in a tensor to the given float value.
- Parameters:
- tensor: tensor
- value: float value
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_set_f32(tensor, value)
- lib.ggml_set_f32.argtypes = [ctypes.POINTER(ggml_tensor), ctypes.c_float]
- lib.ggml_set_f32.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API int32_t ggml_get_i32_1d(const struct ggml_tensor * tensor, int i);
- def ggml_get_i32_1d(
- tensor: ggml_tensor_p,
- i: Union[ctypes.c_int, int],
- ) -> int:
- """Get the integer value of the i-th element in a 1-dimensional tensor.
- Parameters:
- tensor: tensor
- i: index of element
- Returns:
- integer value of element at index i"""
- return lib.ggml_get_i32_1d(tensor, i)
- lib.ggml_get_i32_1d.argtypes = [ctypes.POINTER(ggml_tensor), ctypes.c_int]
- lib.ggml_get_i32_1d.restype = ctypes.c_int32
- # GGML_API void ggml_set_i32_1d(const struct ggml_tensor * tensor, int i, int32_t value);
- def ggml_set_i32_1d(
- tensor: ggml_tensor_p,
- i: Union[ctypes.c_int, int],
- value: Union[ctypes.c_int32, int],
- ):
- """Set the integer value of the i-th element in a 1-dimensional tensor.
- Parameters:
- tensor: tensor
- i: index of element
- value: integer value to set element to"""
- return lib.ggml_set_i32_1d(tensor, i, value)
- lib.ggml_set_i32_1d.argtypes = [
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int,
- ctypes.c_int32,
- ]
- lib.ggml_set_i32_1d.restype = None
- # GGML_API float ggml_get_f32_1d(const struct ggml_tensor * tensor, int i);
- def ggml_get_f32_1d(
- tensor: ggml_tensor_p,
- i: Union[ctypes.c_int, int],
- ) -> float:
- """Get the float value of the i-th element in a 1-dimensional tensor.
- Parameters:
- tensor: tensor
- Returns:
- float value of element at index i"""
- return lib.ggml_get_f32_1d(tensor, i)
- lib.ggml_get_f32_1d.argtypes = [ctypes.POINTER(ggml_tensor), ctypes.c_int]
- lib.ggml_get_f32_1d.restype = ctypes.c_float
- # GGML_API void ggml_set_f32_1d(const struct ggml_tensor * tensor, int i, float value);
- def ggml_set_f32_1d(
- tensor: ggml_tensor_p,
- i: Union[ctypes.c_int, int],
- value: Union[ctypes.c_float, float],
- ):
- """Set the float value of the i-th element in a 1-dimensional tensor.
- Parameters:
- tensor: tensor
- i: index of element
- value: float value to set element to"""
- return lib.ggml_set_f32_1d(tensor, i, value)
- lib.ggml_set_f32_1d.argtypes = [
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int,
- ctypes.c_float,
- ]
- lib.ggml_set_f32_1d.restype = None
- # GGML_API void * ggml_get_data (const struct ggml_tensor * tensor);
- def ggml_get_data(
- tensor: ggml_tensor_p,
- ) -> Optional[ctypes.c_void_p]:
- """Get the data pointer of a tensor.
- Parameters:
- tensor: tensor
- Returns:
- Pointer to data, or None if tensor has no data"""
- return lib.ggml_get_data(tensor)
- lib.ggml_get_data.argtypes = [ctypes.POINTER(ggml_tensor)]
- lib.ggml_get_data.restype = ctypes.c_void_p
- # GGML_API float * ggml_get_data_f32(const struct ggml_tensor * tensor);
- def ggml_get_data_f32(
- tensor: ggml_tensor_p,
- ) -> Optional[CFloatArray]:
- """Get the data pointer of a tensor as a float array.
- Parameters:
- tensor: tensor
- Returns:
- (Optional[ctypes.Array[ctypes.c_float]]): array of float to data, or None if tensor has no data
- """
- return lib.ggml_get_data_f32(tensor)
- lib.ggml_get_data_f32.argtypes = [ctypes.POINTER(ggml_tensor)]
- lib.ggml_get_data_f32.restype = ctypes.POINTER(ctypes.c_float)
- # GGML_API enum ggml_unary_op ggml_get_unary_op(const struct ggml_tensor * tensor);
- def ggml_get_unary_op(
- tensor: ggml_tensor_p,
- ) -> int:
- """Get the unary operation of a tensor.
- Parameters:
- tensor: tensor
- Returns:
- unary operation"""
- return lib.ggml_get_unary_op(tensor)
- lib.ggml_get_unary_op.argtypes = [ctypes.POINTER(ggml_tensor)]
- lib.ggml_get_unary_op.restype = ctypes.c_int
- # GGML_API const char * ggml_get_name(const struct ggml_tensor * tensor);
- def ggml_get_name(
- tensor: ggml_tensor_p,
- ) -> bytes:
- """Get the name of a tensor.
- Parameters:
- tensor: tensor
- Returns:
- name of tensor"""
- return lib.ggml_get_name(tensor)
- lib.ggml_get_name.argtypes = [ctypes.POINTER(ggml_tensor)]
- lib.ggml_get_name.restype = ctypes.c_char_p
- # GGML_API struct ggml_tensor * ggml_set_name(struct ggml_tensor * tensor, const char * name);
- def ggml_set_name(
- tensor: ggml_tensor_p,
- name: bytes,
- ) -> ggml_tensor_p:
- """Set the name of a tensor.
- Parameters:
- tensor: tensor
- name: name to set tensor to
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_set_name(tensor, name)
- lib.ggml_set_name.argtypes = [ctypes.POINTER(ggml_tensor), ctypes.c_char_p]
- lib.ggml_set_name.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_format_name(struct ggml_tensor * tensor, const char * fmt, ...);
- def ggml_format_name(
- tensor: ggml_tensor_p,
- fmt: bytes,
- *args: Sequence[Union[bool, int, float, str]],
- ) -> ggml_tensor_p:
- """Format the name of a tensor using the given format c string and arguments.
- Parameters:
- tensor: tensor
- fmt: format c string
- args: arguments to format string
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_format_name(tensor, fmt, *args)
- lib.ggml_format_name.argtypes = [ctypes.POINTER(ggml_tensor), ctypes.c_char_p]
- lib.ggml_format_name.restype = ctypes.POINTER(ggml_tensor)
- # //
- # // operations on tensors with backpropagation
- # //
- # GGML_API struct ggml_tensor * ggml_dup(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a);
- def ggml_dup(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
- return lib.ggml_dup(ctx, a)
- lib.ggml_dup.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_dup.restype = ctypes.POINTER(ggml_tensor)
- # // in-place, returns view(a)
- # GGML_API struct ggml_tensor * ggml_dup_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a);
- def ggml_dup_inplace(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
- return lib.ggml_dup_inplace(ctx, a)
- lib.ggml_dup_inplace.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_dup_inplace.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_add(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b);
- def ggml_add(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- ) -> ggml_tensor_p:
- """Add two tensors together and return the result.
- Parameters:
- ctx: ggml context
- a: first tensor
- b: second tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_add(ctx, a, b)
- lib.ggml_add.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_add.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_add_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b);
- def ggml_add_inplace(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- ) -> ggml_tensor_p:
- """Add two tensors together and store the result in the first tensor.
- Parameters:
- ctx: ggml context
- a: first tensor
- b: second tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_add_inplace(ctx, a, b)
- lib.ggml_add_inplace.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_add_inplace.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_add1(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b);
- def ggml_add1(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- ) -> ggml_tensor_p:
- return lib.ggml_add1(ctx, a, b)
- lib.ggml_add1.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_add1.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_add1_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b);
- def ggml_add1_inplace(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- ) -> ggml_tensor_p:
- return lib.ggml_add1_inplace(ctx, a, b)
- lib.ggml_add1_inplace.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_add1_inplace.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_acc(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b,
- # size_t nb1,
- # size_t nb2,
- # size_t nb3,
- # size_t offset);
- def ggml_acc(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- nb1: Union[ctypes.c_size_t, int],
- nb2: Union[ctypes.c_size_t, int],
- nb3: Union[ctypes.c_size_t, int],
- offset: Union[ctypes.c_size_t, int],
- ) -> ggml_tensor_p:
- return lib.ggml_acc(ctx, a, b, nb1, nb2, nb3, offset)
- lib.ggml_acc.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.c_size_t,
- ctypes.c_size_t,
- ctypes.c_size_t,
- ctypes.c_size_t,
- ]
- lib.ggml_acc.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_acc_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b,
- # size_t nb1,
- # size_t nb2,
- # size_t nb3,
- # size_t offset);
- def ggml_acc_inplace(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- nb1: Union[ctypes.c_size_t, int],
- nb2: Union[ctypes.c_size_t, int],
- nb3: Union[ctypes.c_size_t, int],
- offset: Union[ctypes.c_size_t, int],
- ) -> ggml_tensor_p:
- return lib.ggml_acc_inplace(ctx, a, b, nb1, nb2, nb3, offset)
- lib.ggml_acc_inplace.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.c_size_t,
- ctypes.c_size_t,
- ctypes.c_size_t,
- ctypes.c_size_t,
- ]
- lib.ggml_acc_inplace.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_sub(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b);
- def ggml_sub(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- ) -> ggml_tensor_p:
- """Subtract two tensors and return the result.
- Parameters:
- ctx: ggml context
- a: first tensor
- b: second tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_sub(ctx, a, b)
- lib.ggml_sub.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_sub.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_sub_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b);
- def ggml_sub_inplace(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- ) -> ggml_tensor_p:
- """Subtract two tensors and store the result in the first tensor.
- Parameters:
- ctx: ggml context
- a: first tensor
- b: second tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_sub_inplace(ctx, a, b)
- lib.ggml_sub_inplace.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_sub_inplace.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_mul(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b);
- def ggml_mul(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- ) -> ggml_tensor_p:
- """Element-wise multiply two tensors and return the result.
- Parameters:
- ctx: ggml context
- a: first tensor
- b: second tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_mul(ctx, a, b)
- lib.ggml_mul.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_mul.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_mul_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b);
- def ggml_mul_inplace(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- ) -> ggml_tensor_p:
- """Element-wise multiply two tensors and store the result in the first tensor.
- Parameters:
- ctx: ggml context
- a: first tensor
- b: second tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_mul_inplace(ctx, a, b)
- lib.ggml_mul_inplace.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_mul_inplace.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_div(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b);
- def ggml_div(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- ) -> ggml_tensor_p:
- """Element-wise divide two tensors and return the result.
- Parameters:
- ctx: ggml context
- a: first tensor
- b: second tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_div(ctx, a, b)
- lib.ggml_div.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_div.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_div_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b);
- def ggml_div_inplace(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- ) -> ggml_tensor_p:
- """Element-wise divide two tensors and store the result in the first tensor.
- Parameters:
- ctx: ggml context
- a: first tensor
- b: second tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_div_inplace(ctx, a, b)
- lib.ggml_div_inplace.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_div_inplace.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_sqr(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a);
- def ggml_sqr(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- ) -> ggml_tensor_p:
- """Square all elements in a tensor and return the result.
- Parameters:
- ctx: ggml context
- a: tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_sqr(ctx, a)
- lib.ggml_sqr.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_sqr.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_sqr_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a);
- def ggml_sqr_inplace(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- ) -> ggml_tensor_p:
- """Square all elements in a tensor and store the result in the first tensor.
- Parameters:
- ctx: ggml context
- a: tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_sqr_inplace(ctx, a)
- lib.ggml_sqr_inplace.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_sqr_inplace.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_sqrt(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a);
- def ggml_sqrt(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- ) -> ggml_tensor_p:
- """Square root all elements in a tensor and return the result.
- Parameters:
- ctx: ggml context
- a: tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_sqrt(ctx, a)
- lib.ggml_sqrt.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_sqrt.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_sqrt_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a);
- def ggml_sqrt_inplace(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- ) -> ggml_tensor_p:
- """Square root all elements in a tensor and store the result in the first tensor.
- Parameters:
- ctx: ggml context
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_sqrt_inplace(ctx, a)
- lib.ggml_sqrt_inplace.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_sqrt_inplace.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_log(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a);
- def ggml_log(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
- """Take the natural logarithm of all elements in a tensor and return the result.
- Parameters:
- ctx: ggml context
- a: tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_log(ctx, a)
- lib.ggml_log.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_log.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_log_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a);
- def ggml_log_inplace(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- ) -> ggml_tensor_p:
- """Take the natural logarithm of all elements in a tensor and store the result in the first tensor.
- Parameters:
- ctx: ggml context
- a: tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_log_inplace(ctx, a)
- lib.ggml_log_inplace.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_log_inplace.restype = ctypes.POINTER(ggml_tensor)
- # // return scalar
- # GGML_API struct ggml_tensor * ggml_sum(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a);
- def ggml_sum(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
- """Sum all elements in a tensor and return the result.
- Parameters:
- ctx: ggml context
- a: tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_sum(ctx, a)
- lib.ggml_sum.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_sum.restype = ctypes.POINTER(ggml_tensor)
- # // sums along rows, with input shape [a,b,c,d] return shape [1,b,c,d]
- # GGML_API struct ggml_tensor * ggml_sum_rows(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a);
- def ggml_sum_rows(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
- """Sum all elements in a tensor along the first axis and return the result.
- sums along rows, with input shape [a,b,c,d] return shape [1,b,c,d]
- Parameters:
- ctx: ggml context
- a: tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_sum_rows(ctx, a)
- lib.ggml_sum_rows.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_sum_rows.restype = ctypes.POINTER(ggml_tensor)
- # // mean along rows
- # GGML_API struct ggml_tensor * ggml_mean(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a);
- def ggml_mean(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
- """Take the mean of all elements in a tensor and return the result.
- Parameters:
- ctx: ggml context
- a: tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_mean(ctx, a)
- lib.ggml_mean.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_mean.restype = ctypes.POINTER(ggml_tensor)
- # // argmax along rows
- # GGML_API struct ggml_tensor * ggml_argmax(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a);
- def ggml_argmax(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
- """Take the argmax of all elements in a tensor and return the result.
- argmax along rows
- Parameters:
- ctx: ggml context
- a: tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_argmax(ctx, a)
- lib.ggml_argmax.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_argmax.restype = ctypes.POINTER(ggml_tensor)
- # // if a is the same shape as b, and a is not parameter, return a
- # // otherwise, return a new tensor: repeat(a) to fit in b
- # GGML_API struct ggml_tensor * ggml_repeat(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b);
- def ggml_repeat(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- ) -> ggml_tensor_p:
- """Repeat a tensor to fit the shape of another tensor.
- If a is the same shape as b, and a is not parameter, return a
- Parameters:
- ctx: ggml context
- a: tensor to repeat
- b: tensor to fit
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_repeat(ctx, a, b)
- lib.ggml_repeat.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_repeat.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_repeat_back(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b);
- def ggml_repeat_back(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- ) -> ggml_tensor_p:
- return lib.ggml_repeat_back(ctx, a, b)
- lib.ggml_repeat_back.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_repeat_back.restype = ctypes.POINTER(ggml_tensor)
- # // concat a and b on dim 2
- # // used in stable-diffusion
- # GGML_API struct ggml_tensor * ggml_concat(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b);
- def ggml_concat(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- ) -> ggml_tensor_p:
- """Concatenate two tensors along the second axis and return the result.
- Parameters:
- ctx: ggml context
- a: first tensor
- b: second tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_concat(ctx, a, b)
- lib.ggml_concat.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_concat.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_abs(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a);
- def ggml_abs(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
- """Take the absolute value of all elements in a tensor and return the result.
- Parameters:
- ctx: ggml context
- a: tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_abs(ctx, a)
- lib.ggml_abs.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_abs.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_abs_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a);
- def ggml_abs_inplace(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- ) -> ggml_tensor_p:
- """Take the absolute value of all elements in a tensor and store the result in the first tensor.
- Parameters:
- ctx: ggml context
- a: tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_abs_inplace(ctx, a)
- lib.ggml_abs_inplace.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_abs_inplace.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_sgn(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a);
- def ggml_sgn(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
- """Get the sign of all elements in a tensor and return the result.
- Parameters:
- ctx: ggml context
- a: tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_sgn(ctx, a)
- lib.ggml_sgn.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_sgn.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_sgn_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a);
- def ggml_sgn_inplace(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- ) -> ggml_tensor_p:
- """Get the sign of all elements in a tensor and store the result in the first tensor.
- Parameters:
- ctx: ggml context
- a: tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_sgn_inplace(ctx, a)
- lib.ggml_sgn_inplace.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_sgn_inplace.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_neg(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a);
- def ggml_neg(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
- """Negate all elements in a tensor and return the result.
- Parameters:
- ctx: ggml context
- a: tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_neg(ctx, a)
- lib.ggml_neg.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_neg.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_neg_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a);
- def ggml_neg_inplace(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- ) -> ggml_tensor_p:
- """Negate all elements in a tensor and store the result in the first tensor.
- Parameters:
- ctx: ggml context
- a: tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_neg_inplace(ctx, a)
- lib.ggml_neg_inplace.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_neg_inplace.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_step(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a);
- def ggml_step(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
- return lib.ggml_step(ctx, a)
- lib.ggml_step.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_step.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_tanh(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a);
- def ggml_tanh(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
- """Apply the tanh activation function to all elements in a tensor and return the result.
- Parameters:
- ctx: ggml context
- a: tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_tanh(ctx, a)
- lib.ggml_tanh.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_tanh.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_tanh_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a);
- def ggml_tanh_inplace(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- ) -> ggml_tensor_p:
- """Apply the tanh activation function to all elements in a tensor and store the result in the first tensor.
- Parameters:
- ctx: ggml context
- a: tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_tanh_inplace(ctx, a)
- lib.ggml_tanh_inplace.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_tanh_inplace.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_elu(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a);
- def ggml_elu(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
- """Apply the ELU activation function to all elements in a tensor and return the result.
- Parameters:
- ctx: ggml context
- a: tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_elu(ctx, a)
- lib.ggml_elu.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_elu.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_elu_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a);
- def ggml_elu_inplace(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- ) -> ggml_tensor_p:
- """Apply the ELU activation function to all elements in a tensor and store the result in the first tensor.
- Parameters:
- ctx: ggml context
- a: tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_elu_inplace(ctx, a)
- lib.ggml_elu_inplace.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_elu_inplace.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_relu(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a);
- def ggml_relu(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
- """Apply the ReLU activation function to all elements in a tensor and return the result.
- Parameters:
- ctx: ggml context
- a: tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_relu(ctx, a)
- lib.ggml_relu.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_relu.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_relu_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a);
- def ggml_relu_inplace(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- ) -> ggml_tensor_p:
- """Apply the ReLU activation function to all elements in a tensor and store the result in the first tensor.
- Parameters:
- ctx: ggml context
- a: tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_relu_inplace(ctx, a)
- lib.ggml_relu_inplace.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_relu_inplace.restype = ctypes.POINTER(ggml_tensor)
- # // TODO: double-check this computation is correct
- # GGML_API struct ggml_tensor * ggml_gelu(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a);
- def ggml_gelu(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
- """Apply the Gaussian Error Linear Unit activation function to all elements in a tensor and return the result.
- Parameters:
- ctx: ggml context
- a: tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_gelu(ctx, a)
- lib.ggml_gelu.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_gelu.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_gelu_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a);
- def ggml_gelu_inplace(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- ) -> ggml_tensor_p:
- """Apply the Gaussian Error Linear Unit activation function to all elements in a tensor and store the result in the first tensor.
- Parameters:
- ctx: ggml context
- a: tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_gelu_inplace(ctx, a)
- lib.ggml_gelu_inplace.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_gelu_inplace.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_gelu_quick(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a);
- def ggml_gelu_quick(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
- """Apply the Gaussian Error Linear Unit activation function to all elements in a tensor and return the result.
- Parameters:
- ctx: ggml context
- a: tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_gelu_quick(ctx, a)
- lib.ggml_gelu_quick.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_gelu_quick.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_gelu_quick_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a);
- def ggml_gelu_quick_inplace(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- ) -> ggml_tensor_p:
- """Apply the Gaussian Error Linear Unit activation function to all elements in a tensor and store the result in the first tensor.
- Parameters:
- ctx: ggml context
- a: tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_gelu_quick_inplace(ctx, a)
- lib.ggml_gelu_quick_inplace.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_gelu_quick_inplace.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_silu(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a);
- def ggml_silu(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
- """Apply the Sigmoid Linear Unit activation function to all elements in a tensor and return the result.
- Parameters:
- ctx: ggml context
- a: tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_silu(ctx, a)
- lib.ggml_silu.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_silu.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_silu_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a);
- def ggml_silu_inplace(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- ) -> ggml_tensor_p:
- """Apply the Sigmoid Linear Unit activation function to all elements in a tensor and store the result in the first tensor.
- Parameters:
- ctx: ggml context
- a: tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_silu_inplace(ctx, a)
- lib.ggml_silu_inplace.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_silu_inplace.restype = ctypes.POINTER(ggml_tensor)
- # // a - x
- # // b - dy
- # GGML_API struct ggml_tensor * ggml_silu_back(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b);
- def ggml_silu_back(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- ) -> ggml_tensor_p:
- return lib.ggml_silu_back(ctx, a, b)
- lib.ggml_silu_back.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_silu_back.restype = ctypes.POINTER(ggml_tensor)
- # // normalize along rows
- # GGML_API struct ggml_tensor * ggml_norm(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a
- # float eps);
- def ggml_norm(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- eps: Union[ctypes.c_float, float],
- ) -> ggml_tensor_p:
- """Normalize all elements in a tensor along the first axis and return the result.
- normalize along rows.
- Parameters:
- ctx: ggml context
- a: tensor
- eps: minimum value to avoid division by zero
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_norm(ctx, a, eps)
- lib.ggml_norm.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor), ctypes.c_float]
- lib.ggml_norm.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_norm_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a
- # float eps);
- def ggml_norm_inplace(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- eps: Union[ctypes.c_float, float],
- ) -> ggml_tensor_p:
- """Normalize all elements in a tensor along the first axis and store the result in the first tensor.
- normalize along rows.
- Parameters:
- ctx: ggml context
- a: tensor
- eps: minimum value to avoid division by zero
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_norm_inplace(ctx, a, eps)
- lib.ggml_norm_inplace.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.c_float,
- ]
- lib.ggml_norm_inplace.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_rms_norm(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # float eps);
- def ggml_rms_norm(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- eps: Union[ctypes.c_float, float],
- ) -> ggml_tensor_p:
- """Compute the RMS norm of a tensor and return the result.
- Parameters:
- ctx: ggml context
- a: tensor
- eps: float
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_rms_norm(ctx, a, eps)
- lib.ggml_rms_norm.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.c_float,
- ]
- lib.ggml_rms_norm.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_rms_norm_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # float eps);
- def ggml_rms_norm_inplace(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- eps: Union[ctypes.c_float, float],
- ) -> ggml_tensor_p:
- return lib.ggml_rms_norm_inplace(ctx, a, eps)
- lib.ggml_rms_norm_inplace.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.c_float,
- ]
- lib.ggml_rms_norm_inplace.restype = ctypes.POINTER(ggml_tensor)
- # // group normalize along ne0*ne1*n_groups
- # // used in stable-diffusion
- # // TODO: eps is hardcoded to 1e-6 for now
- # GGML_API struct ggml_tensor * ggml_group_norm(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # int n_groups);
- def ggml_group_norm(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- n_groups: int,
- ) -> ggml_tensor_p:
- """Group normalize a tensor and return the result.
- Parameters:
- ctx: ggml context
- a: tensor
- n_groups: int
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_group_norm(ctx, a, n_groups)
- lib.ggml_group_norm.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int,
- ]
- lib.ggml_group_norm.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_group_norm_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # int n_groups);
- def ggml_group_norm_inplace(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- n_groups: int,
- ) -> ggml_tensor_p:
- """Group normalize a tensor and store the result in the first tensor.
- Parameters:
- ctx: ggml context
- a: tensor
- n_groups: int
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_group_norm_inplace(ctx, a, n_groups)
- lib.ggml_group_norm_inplace.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int,
- ]
- lib.ggml_group_norm_inplace.restype = ctypes.POINTER(ggml_tensor)
- # // a - x
- # // b - dy
- # GGML_API struct ggml_tensor * ggml_rms_norm_back(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b
- # float eps);
- def ggml_rms_norm_back(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- eps: Union[ctypes.c_float, float],
- ) -> ggml_tensor_p:
- return lib.ggml_rms_norm_back(ctx, a, b, eps)
- lib.ggml_rms_norm_back.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.c_float,
- ]
- lib.ggml_rms_norm_back.restype = ctypes.POINTER(ggml_tensor)
- # // A: m rows, n columns
- # // B: p rows, n columns (i.e. we transpose it internally)
- # // result is m columns, p rows
- # GGML_API struct ggml_tensor * ggml_mul_mat(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b);
- def ggml_mul_mat(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- ) -> ggml_tensor_p:
- """Multiply two matrices and return the result.
- A: m rows, n columns
- B: p rows, n columns (i.e. we transpose it internally)
- result is m columns, p rows
- Parameters:
- ctx: ggml context
- a: tensor
- b: tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_mul_mat(ctx, a, b)
- lib.ggml_mul_mat.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_mul_mat.restype = ctypes.POINTER(ggml_tensor)
- # // A: m columns, n rows,
- # // B: p columns, n rows,
- # // result is m columns, p rows
- # GGML_API struct ggml_tensor * ggml_out_prod(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b);
- def ggml_out_prod(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- ) -> ggml_tensor_p:
- """Compute the outer product of two matrices and return the result.
- A: m columns, n rows,
- B: p columns, n rows,
- result is m columns, p rows
- Parameters:
- ctx: ggml context
- a: tensor
- b: tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_out_prod(ctx, a, b)
- lib.ggml_out_prod.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_out_prod.restype = ctypes.POINTER(ggml_tensor)
- # //
- # // operations on tensors without backpropagation
- # //
- # GGML_API struct ggml_tensor * ggml_scale(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b);
- def ggml_scale(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- ) -> ggml_tensor_p:
- """Scale a tensor by another tensor and return the result.
- Parameters:
- ctx: ggml context
- a: tensor
- b: tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_scale(ctx, a, b)
- lib.ggml_scale.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_scale.restype = ctypes.POINTER(ggml_tensor)
- # // in-place, returns view(a)
- # GGML_API struct ggml_tensor * ggml_scale_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b);
- def ggml_scale_inplace(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- ) -> ggml_tensor_p:
- """Scale a tensor by another tensor and store the result in the first tensor.
- Parameters:
- ctx: ggml context
- a: tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_scale_inplace(ctx, a, b)
- lib.ggml_scale_inplace.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_scale_inplace.restype = ctypes.POINTER(ggml_tensor)
- # // b -> view(a,offset,nb1,nb2,3), return modified a
- # GGML_API struct ggml_tensor * ggml_set(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b,
- # size_t nb1,
- # size_t nb2,
- # size_t nb3,
- # size_t offset);
- def ggml_set(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- nb1: Union[ctypes.c_size_t, int],
- nb2: Union[ctypes.c_size_t, int],
- nb3: Union[ctypes.c_size_t, int],
- offset: Union[ctypes.c_size_t, int],
- ) -> ggml_tensor_p:
- return lib.ggml_set(ctx, a, b, nb1, nb2, nb3, offset)
- lib.ggml_set.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.c_size_t,
- ctypes.c_size_t,
- ctypes.c_size_t,
- ctypes.c_size_t,
- ]
- lib.ggml_set.restype = ctypes.POINTER(ggml_tensor)
- # // b -> view(a,offset,nb1,nb2,3), return view(a)
- # GGML_API struct ggml_tensor * ggml_set_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b,
- # size_t nb1,
- # size_t nb2,
- # size_t nb3,
- # size_t offset);
- def ggml_set_inplace(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- nb1: Union[ctypes.c_size_t, int],
- nb2: Union[ctypes.c_size_t, int],
- nb3: Union[ctypes.c_size_t, int],
- offset: Union[ctypes.c_size_t, int],
- ) -> ggml_tensor_p:
- return lib.ggml_set_inplace(ctx, a, b, nb1, nb2, nb3, offset)
- lib.ggml_set_inplace.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.c_size_t,
- ctypes.c_size_t,
- ctypes.c_size_t,
- ctypes.c_size_t,
- ]
- lib.ggml_set_inplace.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_set_1d(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b,
- # size_t offset);
- def ggml_set_1d(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- offset: Union[ctypes.c_size_t, int],
- ) -> ggml_tensor_p:
- return lib.ggml_set_1d(ctx, a, b, offset)
- lib.ggml_set_1d.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.c_size_t,
- ]
- lib.ggml_set_1d.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_set_1d_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b,
- # size_t offset);
- def ggml_set_1d_inplace(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- offset: Union[ctypes.c_size_t, int],
- ) -> ggml_tensor_p:
- return lib.ggml_set_1d_inplace(ctx, a, b, offset)
- lib.ggml_set_1d_inplace.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.c_size_t,
- ]
- lib.ggml_set_1d_inplace.restype = ctypes.POINTER(ggml_tensor)
- # // b -> view(a,offset,nb1,nb2,3), return modified a
- # GGML_API struct ggml_tensor * ggml_set_2d(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b,
- # size_t nb1,
- # size_t offset);
- def ggml_set_2d(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- nb1: Union[ctypes.c_size_t, int],
- offset: Union[ctypes.c_size_t, int],
- ) -> ggml_tensor_p:
- return lib.ggml_set_2d(ctx, a, b, nb1, offset)
- lib.ggml_set_2d.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.c_size_t,
- ctypes.c_size_t,
- ]
- lib.ggml_set_2d.restype = ctypes.POINTER(ggml_tensor)
- # // b -> view(a,offset,nb1,nb2,3), return view(a)
- # GGML_API struct ggml_tensor * ggml_set_2d_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b,
- # size_t nb1,
- # size_t offset);
- def ggml_set_2d_inplace(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- nb1: Union[ctypes.c_size_t, int],
- offset: Union[ctypes.c_size_t, int],
- ) -> ggml_tensor_p:
- return lib.ggml_set_2d_inplace(ctx, a, b, nb1, offset)
- lib.ggml_set_2d_inplace.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.c_size_t,
- ctypes.c_size_t,
- ]
- lib.ggml_set_2d_inplace.restype = ctypes.POINTER(ggml_tensor)
- # // a -> b, return view(b)
- # GGML_API struct ggml_tensor * ggml_cpy(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b);
- def ggml_cpy(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- ) -> ggml_tensor_p:
- return lib.ggml_cpy(ctx, a, b)
- lib.ggml_cpy.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_cpy.restype = ctypes.POINTER(ggml_tensor)
- # // a -> b, in-place, return view(b)
- # GGML_API struct ggml_tensor * ggml_cpy_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b);
- def ggml_cpy_inplace(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- ) -> ggml_tensor_p:
- return lib.ggml_cpy_inplace(ctx, a, b)
- lib.ggml_cpy_inplace.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_cpy_inplace.restype = ctypes.POINTER(ggml_tensor)
- # // make contiguous
- # GGML_API struct ggml_tensor * ggml_cont(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a);
- def ggml_cont(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
- """Make a tensor contiguous and return the result.
- Parameters:
- ctx: ggml context
- a: tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_cont(ctx, a)
- lib.ggml_cont.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_cont.restype = ctypes.POINTER(ggml_tensor)
- # // make contiguous, in-place
- # GGML_API struct ggml_tensor * ggml_cont_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a);
- def ggml_cont_inplace(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- ) -> ggml_tensor_p:
- """Make a tensor contiguous and store the result in the first tensor.
- Parameters:
- ctx: ggml context
- a: tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_cont_inplace(ctx, a)
- lib.ggml_cont_inplace.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_cont_inplace.restype = ctypes.POINTER(ggml_tensor)
- # // return view(a), b specifies the new shape
- # // TODO: when we start computing gradient, make a copy instead of view
- # GGML_API struct ggml_tensor * ggml_reshape(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b);
- def ggml_reshape(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- ) -> ggml_tensor_p:
- return lib.ggml_reshape(ctx, a, b)
- lib.ggml_reshape.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_reshape.restype = ctypes.POINTER(ggml_tensor)
- # // return view(a)
- # // TODO: when we start computing gradient, make a copy instead of view
- # GGML_API struct ggml_tensor * ggml_reshape_1d(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # int64_t ne0);
- def ggml_reshape_1d(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- ne0: Union[ctypes.c_int64, int],
- ) -> ggml_tensor_p:
- return lib.ggml_reshape_1d(ctx, a, ne0)
- lib.ggml_reshape_1d.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int64,
- ]
- lib.ggml_reshape_1d.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_reshape_2d(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # int64_t ne0,
- # int64_t ne1);
- def ggml_reshape_2d(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- ne0: Union[ctypes.c_int64, int],
- ne1: Union[ctypes.c_int64, int],
- ) -> ggml_tensor_p:
- return lib.ggml_reshape_2d(ctx, a, ne0, ne1)
- lib.ggml_reshape_2d.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int64,
- ctypes.c_int64,
- ]
- lib.ggml_reshape_2d.restype = ctypes.POINTER(ggml_tensor)
- # // return view(a)
- # // TODO: when we start computing gradient, make a copy instead of view
- # GGML_API struct ggml_tensor * ggml_reshape_3d(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # int64_t ne0,
- # int64_t ne1,
- # int64_t ne2);
- def ggml_reshape_3d(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- ne0: Union[ctypes.c_int64, int],
- ne1: Union[ctypes.c_int64, int],
- ne2: Union[ctypes.c_int64, int],
- ) -> ggml_tensor_p:
- return lib.ggml_reshape_3d(ctx, a, ne0, ne1, ne2)
- lib.ggml_reshape_3d.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int64,
- ctypes.c_int64,
- ctypes.c_int64,
- ]
- lib.ggml_reshape_3d.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_reshape_4d(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # int64_t ne0,
- # int64_t ne1,
- # int64_t ne2,
- # int64_t ne3);
- def ggml_reshape_4d(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- ne0: Union[ctypes.c_int64, int],
- ne1: Union[ctypes.c_int64, int],
- ne2: Union[ctypes.c_int64, int],
- ne3: Union[ctypes.c_int64, int],
- ) -> ggml_tensor_p:
- return lib.ggml_reshape_4d(ctx, a, ne0, ne1, ne2, ne3)
- lib.ggml_reshape_4d.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int64,
- ctypes.c_int64,
- ctypes.c_int64,
- ctypes.c_int64,
- ]
- lib.ggml_reshape_4d.restype = ctypes.POINTER(ggml_tensor)
- # // offset in bytes
- # GGML_API struct ggml_tensor * ggml_view_1d(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # int64_t ne0,
- # size_t offset);
- def ggml_view_1d(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- ne0: Union[ctypes.c_int64, int],
- offset: Union[ctypes.c_size_t, int],
- ) -> ggml_tensor_p:
- return lib.ggml_view_1d(ctx, a, ne0, offset)
- lib.ggml_view_1d.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int64,
- ctypes.c_size_t,
- ]
- lib.ggml_view_1d.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_view_2d(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # int64_t ne0,
- # int64_t ne1,
- # size_t nb1, // row stride in bytes
- # size_t offset);
- def ggml_view_2d(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- ne0: Union[ctypes.c_int64, int],
- ne1: Union[ctypes.c_int64, int],
- nb1: Union[ctypes.c_size_t, int],
- offset: Union[ctypes.c_size_t, int],
- ) -> ggml_tensor_p:
- return lib.ggml_view_2d(ctx, a, ne0, ne1, nb1, offset)
- lib.ggml_view_2d.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int64,
- ctypes.c_int64,
- ctypes.c_size_t,
- ctypes.c_size_t,
- ]
- lib.ggml_view_2d.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_view_3d(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # int64_t ne0,
- # int64_t ne1,
- # int64_t ne2,
- # size_t nb1, // row stride in bytes
- # size_t nb2, // slice stride in bytes
- # size_t offset);
- def ggml_view_3d(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- ne0: Union[ctypes.c_int64, int],
- ne1: Union[ctypes.c_int64, int],
- ne2: Union[ctypes.c_int64, int],
- nb1: Union[ctypes.c_size_t, int],
- nb2: Union[ctypes.c_size_t, int],
- offset: Union[ctypes.c_size_t, int],
- ) -> ggml_tensor_p:
- return lib.ggml_view_3d(ctx, a, ne0, ne1, ne2, nb1, nb2, offset)
- lib.ggml_view_3d.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int64,
- ctypes.c_int64,
- ctypes.c_int64,
- ctypes.c_size_t,
- ctypes.c_size_t,
- ctypes.c_size_t,
- ]
- lib.ggml_view_3d.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_view_4d(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # int64_t ne0,
- # int64_t ne1,
- # int64_t ne2,
- # int64_t ne3,
- # size_t nb1, // row stride in bytes
- # size_t nb2, // slice stride in bytes
- # size_t nb3,
- # size_t offset);
- def ggml_view_4d(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- ne0: Union[ctypes.c_int64, int],
- ne1: Union[ctypes.c_int64, int],
- ne2: Union[ctypes.c_int64, int],
- ne3: Union[ctypes.c_int64, int],
- nb1: Union[ctypes.c_size_t, int],
- nb2: Union[ctypes.c_size_t, int],
- nb3: Union[ctypes.c_size_t, int],
- offset: Union[ctypes.c_size_t, int],
- ) -> ggml_tensor_p:
- return lib.ggml_view_4d(ctx, a, ne0, ne1, ne2, ne3, nb1, nb2, nb3, offset)
- lib.ggml_view_4d.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int64,
- ctypes.c_int64,
- ctypes.c_int64,
- ctypes.c_int64,
- ctypes.c_size_t,
- ctypes.c_size_t,
- ctypes.c_size_t,
- ctypes.c_size_t,
- ]
- lib.ggml_view_4d.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_permute(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # int axis0,
- # int axis1,
- # int axis2,
- # int axis3);
- def ggml_permute(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- axis0: Union[ctypes.c_int, int],
- axis1: Union[ctypes.c_int, int],
- axis2: Union[ctypes.c_int, int],
- axis3: Union[ctypes.c_int, int],
- ) -> ggml_tensor_p:
- return lib.ggml_permute(ctx, a, axis0, axis1, axis2, axis3)
- lib.ggml_permute.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int,
- ctypes.c_int,
- ctypes.c_int,
- ctypes.c_int,
- ]
- lib.ggml_permute.restype = ctypes.POINTER(ggml_tensor)
- # // alias for ggml_permute(ctx, a, 1, 0, 2, 3)
- # GGML_API struct ggml_tensor * ggml_transpose(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a);
- def ggml_transpose(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
- """Transpose *the first two dimensions* of a tensor and return the result.
- alias for `ggml_permute(ctx, a, 1, 0, 2, 3)`
- Parameters:
- ctx: ggml context
- a: tensor
- Returns:
- Pointer to ggml_tensor"""
- return lib.ggml_transpose(ctx, a)
- lib.ggml_transpose.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_transpose.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_get_rows(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b);
- def ggml_get_rows(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- ) -> ggml_tensor_p:
- return lib.ggml_get_rows(ctx, a, b)
- lib.ggml_get_rows.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_get_rows.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_get_rows_back(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b,
- # struct ggml_tensor * c);
- def ggml_get_rows_back(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- c: ggml_tensor_p,
- ) -> ggml_tensor_p:
- return lib.ggml_get_rows_back(ctx, a, b, c)
- lib.ggml_get_rows_back.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_get_rows_back.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_diag(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a);
- def ggml_diag(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
- return lib.ggml_diag(ctx, a)
- lib.ggml_diag.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_diag.restype = ctypes.POINTER(ggml_tensor)
- # // set elements above the diagonal to -INF
- # GGML_API struct ggml_tensor * ggml_diag_mask_inf(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # int n_past);
- def ggml_diag_mask_inf(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- n_past: Union[ctypes.c_int, int],
- ) -> ggml_tensor_p:
- return lib.ggml_diag_mask_inf(ctx, a, n_past)
- lib.ggml_diag_mask_inf.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int,
- ]
- lib.ggml_diag_mask_inf.restype = ctypes.POINTER(ggml_tensor)
- # // in-place, returns view(a)
- # GGML_API struct ggml_tensor * ggml_diag_mask_inf_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # int n_past);
- def ggml_diag_mask_inf_inplace(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- n_past: Union[ctypes.c_int, int],
- ) -> ggml_tensor_p:
- return lib.ggml_diag_mask_inf_inplace(ctx, a, n_past)
- lib.ggml_diag_mask_inf_inplace.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int,
- ]
- lib.ggml_diag_mask_inf_inplace.restype = ctypes.POINTER(ggml_tensor)
- # // set elements above the diagonal to 0
- # GGML_API struct ggml_tensor * ggml_diag_mask_zero(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # int n_past);
- def ggml_diag_mask_zero(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- n_past: Union[ctypes.c_int, int],
- ) -> ggml_tensor_p:
- return lib.ggml_diag_mask_zero(ctx, a, n_past)
- lib.ggml_diag_mask_zero.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int,
- ]
- lib.ggml_diag_mask_zero.restype = ctypes.POINTER(ggml_tensor)
- # // in-place, returns view(a)
- # GGML_API struct ggml_tensor * ggml_diag_mask_zero_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # int n_past);
- def ggml_diag_mask_zero_inplace(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- n_past: Union[ctypes.c_int, int],
- ) -> ggml_tensor_p:
- return lib.ggml_diag_mask_zero_inplace(ctx, a, n_past)
- lib.ggml_diag_mask_zero_inplace.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int,
- ]
- lib.ggml_diag_mask_zero_inplace.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_soft_max(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a);
- def ggml_soft_max(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
- return lib.ggml_soft_max(ctx, a)
- lib.ggml_soft_max.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_soft_max.restype = ctypes.POINTER(ggml_tensor)
- # // in-place, returns view(a)
- # GGML_API struct ggml_tensor * ggml_soft_max_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a);
- def ggml_soft_max_inplace(ctx: ggml_context_p, a: ggml_tensor_p) -> ggml_tensor_p:
- return lib.ggml_soft_max_inplace(ctx, a)
- lib.ggml_soft_max_inplace.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_soft_max_inplace.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_soft_max_back(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b);
- def ggml_soft_max_back(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- ) -> ggml_tensor_p:
- return lib.ggml_soft_max_back(ctx, a, b)
- lib.ggml_soft_max_back.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_soft_max_back.restype = ctypes.POINTER(ggml_tensor)
- # // in-place, returns view(a)
- # GGML_API struct ggml_tensor * ggml_soft_max_back_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b);
- def ggml_soft_max_back_inplace(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- ) -> ggml_tensor_p:
- return lib.ggml_soft_max_back_inplace(ctx, a, b)
- lib.ggml_soft_max_back_inplace.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_soft_max_back_inplace.restype = ctypes.POINTER(ggml_tensor)
- # // rotary position embedding
- # // if mode & 1 == 1, skip n_past elements
- # // if mode & 2 == 1, GPT-NeoX style
- # // if mode & 4 == 1, ChatGLM style
- # // TODO: avoid creating a new tensor every time
- # GGML_API struct ggml_tensor * ggml_rope(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # int n_past,
- # int n_dims,
- # int mode,
- # int n_ctx);
- def ggml_rope(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- n_past: Union[ctypes.c_int, int],
- n_dims: Union[ctypes.c_int, int],
- mode: Union[ctypes.c_int, int],
- n_ctx: Union[ctypes.c_int, int],
- ) -> ggml_tensor_p:
- return lib.ggml_rope(ctx, a, n_past, n_dims, mode, n_ctx)
- lib.ggml_rope.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int,
- ctypes.c_int,
- ctypes.c_int,
- ctypes.c_int,
- ]
- lib.ggml_rope.restype = ctypes.POINTER(ggml_tensor)
- # // in-place, returns view(a)
- # GGML_API struct ggml_tensor * ggml_rope_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # int n_past,
- # int n_dims,
- # int mode,
- # int n_ctx);
- def ggml_rope_inplace(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- n_past: Union[ctypes.c_int, int],
- n_dims: Union[ctypes.c_int, int],
- mode: Union[ctypes.c_int, int],
- n_ctx: Union[ctypes.c_int, int],
- ) -> ggml_tensor_p:
- return lib.ggml_rope_inplace(ctx, a, n_past, n_dims, mode, n_ctx)
- lib.ggml_rope_inplace.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int,
- ctypes.c_int,
- ctypes.c_int,
- ctypes.c_int,
- ]
- lib.ggml_rope_inplace.restype = ctypes.POINTER(ggml_tensor)
- # // custom RoPE
- # GGML_API struct ggml_tensor * ggml_rope_custom(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # int n_past,
- # int n_dims,
- # int mode,
- # int n_ctx,
- # float freq_base,
- # float freq_scale);
- def ggml_rope_custom(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- n_past: Union[ctypes.c_int, int],
- n_dims: Union[ctypes.c_int, int],
- mode: Union[ctypes.c_int, int],
- n_ctx: Union[ctypes.c_int, int],
- freq_base: Union[ctypes.c_float, float],
- freq_scale: Union[ctypes.c_float, float],
- ) -> ggml_tensor_p:
- return lib.ggml_rope_custom(
- ctx, a, n_past, n_dims, mode, n_ctx, freq_base, freq_scale
- )
- lib.ggml_rope_custom.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int,
- ctypes.c_int,
- ctypes.c_int,
- ctypes.c_int,
- ctypes.c_float,
- ctypes.c_float,
- ]
- lib.ggml_rope_custom.restype = ctypes.POINTER(ggml_tensor)
- # // in-place, returns view(a)
- # GGML_API struct ggml_tensor * ggml_rope_custom_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # int n_past,
- # int n_dims,
- # int mode,
- # int n_ctx,
- # float freq_base,
- # float freq_scale);
- def ggml_rope_custom_inplace(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- n_past: Union[ctypes.c_int, int],
- n_dims: Union[ctypes.c_int, int],
- mode: Union[ctypes.c_int, int],
- n_ctx: Union[ctypes.c_int, int],
- freq_base: Union[ctypes.c_float, float],
- freq_scale: Union[ctypes.c_float, float],
- ) -> ggml_tensor_p:
- return lib.ggml_rope_custom_inplace(
- ctx, a, n_past, n_dims, mode, n_ctx, freq_base, freq_scale
- )
- lib.ggml_rope_custom_inplace.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int,
- ctypes.c_int,
- ctypes.c_int,
- ctypes.c_int,
- ctypes.c_float,
- ctypes.c_float,
- ]
- lib.ggml_rope_custom_inplace.restype = ctypes.POINTER(ggml_tensor)
- # // xPos RoPE, in-place, returns view(a)
- # GGML_API struct ggml_tensor * ggml_rope_xpos_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # int n_past,
- # int n_dims,
- # float base,
- # bool down);
- def ggml_rope_xpos_inplace(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- n_past: Union[ctypes.c_int, int],
- n_dims: Union[ctypes.c_int, int],
- base: Union[ctypes.c_float, float],
- down: Union[ctypes.c_bool, bool],
- ) -> ggml_tensor_p:
- return lib.ggml_rope_xpos_inplace(ctx, a, n_past, n_dims, base, down)
- lib.ggml_rope_xpos_inplace.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int,
- ctypes.c_int,
- ctypes.c_float,
- ctypes.c_bool,
- ]
- lib.ggml_rope_xpos_inplace.restype = ctypes.POINTER(ggml_tensor)
- # // rotary position embedding backward, i.e compute dx from dy
- # // a - dy
- # GGML_API struct ggml_tensor * ggml_rope_back(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # int n_past,
- # int n_dims,
- # int mode,
- # int n_ctx,
- # float freq_base,
- # float freq_scale,
- # float xpos_base,
- # bool xpos_down);
- def ggml_rope_back(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- n_past: Union[ctypes.c_int, int],
- n_dims: Union[ctypes.c_int, int],
- mode: Union[ctypes.c_int, int],
- n_ctx: Union[ctypes.c_int, int],
- freq_base: Union[ctypes.c_float, float],
- freq_scale: Union[ctypes.c_float, float],
- xpos_base: Union[ctypes.c_float, float],
- xpos_down: Union[ctypes.c_bool, bool],
- ) -> ggml_tensor_p:
- return lib.ggml_rope_back(
- ctx, a, n_past, n_dims, mode, n_ctx, freq_base, freq_scale, xpos_base, xpos_down
- )
- lib.ggml_rope_back.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int,
- ctypes.c_int,
- ctypes.c_int,
- ctypes.c_int,
- ctypes.c_float,
- ctypes.c_float,
- ctypes.c_float,
- ctypes.c_bool,
- ]
- lib.ggml_rope_back.restype = ctypes.POINTER(ggml_tensor)
- # // alibi position embedding
- # // in-place, returns view(a)
- # struct ggml_tensor * ggml_alibi(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # int n_past,
- # int n_head,
- # float bias_max);
- def ggml_alibi(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- n_past: Union[ctypes.c_int, int],
- n_head: Union[ctypes.c_int, int],
- bias_max: Union[ctypes.c_float, float],
- ) -> ggml_tensor_p:
- return lib.ggml_alibi(ctx, a, n_past, n_head, bias_max)
- lib.ggml_alibi.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int,
- ctypes.c_int,
- ctypes.c_float,
- ]
- lib.ggml_alibi.restype = ctypes.POINTER(ggml_tensor)
- # // clamp
- # // in-place, returns view(a)
- # struct ggml_tensor * ggml_clamp(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # float min,
- # float max);
- def ggml_clamp(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- min: Union[ctypes.c_float, float],
- max: Union[ctypes.c_float, float],
- ) -> ggml_tensor_p:
- return lib.ggml_clamp(ctx, a, min, max)
- lib.ggml_clamp.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.c_float,
- ctypes.c_float,
- ]
- lib.ggml_clamp.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_conv_1d(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b,
- # int s0, // stride
- # int p0, // padding
- # int d0); // dilation
- def ggml_conv_1d(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- s0: Union[ctypes.c_int, int],
- p0: Union[ctypes.c_int, int],
- d0: Union[ctypes.c_int, int],
- ) -> ggml_tensor_p:
- """Convolution 1D
- Parameters:
- a: input tensor
- b: filter tensor
- s0: stride
- p0: padding
- d0: dilation
- Returns:
- output tensor"""
- return lib.ggml_conv_1d(ctx, a, b, s0, p0, d0)
- lib.ggml_conv_1d.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int,
- ctypes.c_int,
- ctypes.c_int,
- ]
- lib.ggml_conv_1d.restype = ctypes.POINTER(ggml_tensor)
- # // conv_1d with padding = half
- # // alias for ggml_conv_1d(a, b, s, a->ne[0]/2, d)
- # GGML_API struct ggml_tensor* ggml_conv_1d_ph(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b,
- # int s,
- # int d);
- def ggml_conv_1d_ph(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- s: Union[ctypes.c_int, int],
- d: Union[ctypes.c_int, int],
- ) -> ggml_tensor_p:
- """Convolution 1D with padding = half
- Parameters:
- a: input tensor
- b: filter tensor
- s: stride
- d: dilation
- Returns:
- output tensor"""
- return lib.ggml_conv_1d_ph(ctx, a, b, s, d)
- lib.ggml_conv_1d_ph.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int,
- ctypes.c_int,
- ]
- lib.ggml_conv_1d_ph.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_conv_2d(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b,
- # int s0,
- # int s1,
- # int p0,
- # int p1,
- # int d0,
- # int d1);
- def ggml_conv_2d(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- s0: Union[ctypes.c_int, int],
- s1: Union[ctypes.c_int, int],
- p0: Union[ctypes.c_int, int],
- p1: Union[ctypes.c_int, int],
- d0: Union[ctypes.c_int, int],
- d1: Union[ctypes.c_int, int],
- ) -> ggml_tensor_p:
- """Convolution 2D
- Parameters:
- a: input tensor
- b: filter tensor
- s0: stride
- s1: stride
- p0: padding
- p1: padding
- d0: dilation
- d1: dilation
- Returns:
- output tensor"""
- return lib.ggml_conv_2d(ctx, a, b, s0, s1, p0, p1, d0, d1)
- lib.ggml_conv_2d.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int,
- ctypes.c_int,
- ctypes.c_int,
- ctypes.c_int,
- ctypes.c_int,
- ctypes.c_int,
- ]
- lib.ggml_conv_2d.restype = ctypes.POINTER(ggml_tensor)
- # // kernel size is a->ne[0] x a->ne[1]
- # // stride is equal to kernel size
- # // padding is zero
- # // example:
- # // a: 16 16 3 768
- # // b: 1024 1024 3 1
- # // res: 64 64 768 1
- # // used in sam
- # GGML_API struct ggml_tensor * ggml_conv_2d_sk_p0(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b);
- def ggml_conv_2d_sk_p0(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- ) -> ggml_tensor_p:
- """Convolution 2D
- Parameters:
- a: input tensor
- b: filter tensor
- Returns:
- output tensor"""
- return lib.ggml_conv_2d_sk_p0(ctx, a, b)
- lib.ggml_conv_2d_sk_p0.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_conv_2d_sk_p0.restype = ctypes.POINTER(ggml_tensor)
- # // kernel size is a->ne[0] x a->ne[1]
- # // stride is 1
- # // padding is half
- # // example:
- # // a: 3 3 256 256
- # // b: 64 64 256 1
- # // res: 64 64 256 1
- # // used in sam
- # GGML_API struct ggml_tensor * ggml_conv_2d_s1_ph(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b);
- def ggml_conv_2d_s1_ph(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- ) -> ggml_tensor_p:
- """Convolution 2D with stride = 1 and padding = half
- Parameters:
- a: input tensor
- b: filter tensor
- Returns:
- output tensor"""
- return lib.ggml_conv_2d_s1_ph(ctx, a, b)
- lib.ggml_conv_2d_s1_ph.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_conv_2d_s1_ph.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_conv_transpose_2d_p0(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b,
- # int stride);
- def ggml_conv_transpose_2d_p0(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- stride: Union[ctypes.c_int, int],
- ) -> ggml_tensor_p:
- """Convolution Transpose 2D with padding = zero
- Parameters:
- a: input tensor
- b: filter tensor
- stride: stride
- Returns:
- output tensor"""
- return lib.ggml_conv_transpose_2d_p0(ctx, a, b, stride)
- lib.ggml_conv_transpose_2d_p0.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int,
- ]
- lib.ggml_conv_transpose_2d_p0.restype = ctypes.POINTER(ggml_tensor)
- # enum ggml_op_pool {
- # GGML_OP_POOL_MAX,
- # GGML_OP_POOL_AVG,
- # GGML_OP_POOL_COUNT,
- # };
- GGML_OP_POOL_MAX = 0
- GGML_OP_POOL_AVG = 1
- GGML_OP_POOL_COUNT = 2
- # GGML_API struct ggml_tensor * ggml_pool_1d(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # enum ggml_op_pool op,
- # int k0, // kernel size
- # int s0, // stride
- # int p0); // padding
- def ggml_pool_1d(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- op: Union[ctypes.c_int, int],
- k0: Union[ctypes.c_int, int],
- s0: Union[ctypes.c_int, int],
- p0: Union[ctypes.c_int, int],
- ) -> ggml_tensor_p:
- """1D Pooling
- Parameters:
- a: input tensor
- op: pooling operation
- k0: kernel size
- s0: stride
- p0: padding
- Returns:
- output tensor"""
- return lib.ggml_pool_1d(ctx, a, op, k0, s0, p0)
- lib.ggml_pool_1d.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int,
- ctypes.c_int,
- ctypes.c_int,
- ctypes.c_int,
- ]
- lib.ggml_pool_1d.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_pool_2d(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # enum ggml_op_pool op,
- # int k0,
- # int k1,
- # int s0,
- # int s1,
- # int p0,
- # int p1);
- def ggml_pool_2d(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- op: Union[ctypes.c_int, int],
- k0: Union[ctypes.c_int, int],
- k1: Union[ctypes.c_int, int],
- s0: Union[ctypes.c_int, int],
- s1: Union[ctypes.c_int, int],
- p0: Union[ctypes.c_int, int],
- p1: Union[ctypes.c_int, int],
- ) -> ggml_tensor_p:
- """2D Pooling
- Parameters:
- a: input tensor
- op: pooling operation
- k0: kernel size
- k1: kernel size
- s0: stride
- s1: stride
- p0: padding
- p1: padding
- Returns:
- output tensor"""
- return lib.ggml_pool_2d(ctx, a, op, k0, k1, s0, s1, p0, p1)
- lib.ggml_pool_2d.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int,
- ctypes.c_int,
- ctypes.c_int,
- ctypes.c_int,
- ctypes.c_int,
- ctypes.c_int,
- ]
- lib.ggml_pool_2d.restype = ctypes.POINTER(ggml_tensor)
- # // nearest interpolate
- # // used in stable-diffusion
- # GGML_API struct ggml_tensor * ggml_upscale(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # int scale_factor);
- def ggml_upscale(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- scale_factor: Union[ctypes.c_int, int],
- ) -> ggml_tensor_p:
- """Upscale
- Parameters:
- a: input tensor
- scale_factor: scale factor
- Returns:
- output tensor"""
- return lib.ggml_upscale(ctx, a, scale_factor)
- lib.ggml_upscale.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int,
- ]
- lib.ggml_upscale.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_flash_attn(
- # struct ggml_context * ctx,
- # struct ggml_tensor * q,
- # struct ggml_tensor * k,
- # struct ggml_tensor * v,
- # bool masked);
- def ggml_flash_attn(
- ctx: ggml_context_p,
- q: ggml_tensor_p,
- k: ggml_tensor_p,
- v: ggml_tensor_p,
- masked: Union[ctypes.c_bool, bool],
- ) -> ggml_tensor_p:
- return lib.ggml_flash_attn(ctx, q, k, v, masked)
- lib.ggml_flash_attn.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.c_bool,
- ]
- lib.ggml_flash_attn.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_flash_attn_back(
- # struct ggml_context * ctx,
- # struct ggml_tensor * q,
- # struct ggml_tensor * k,
- # struct ggml_tensor * v,
- # struct ggml_tensor * d,
- # bool masked);
- def ggml_flash_attn_back(
- ctx: ggml_context_p,
- q: ggml_tensor_p,
- k: ggml_tensor_p,
- v: ggml_tensor_p,
- d: ggml_tensor_p,
- masked: Union[ctypes.c_bool, bool],
- ) -> ggml_tensor_p:
- return lib.ggml_flash_attn_back(ctx, q, k, v, d, masked)
- lib.ggml_flash_attn_back.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.c_bool,
- ]
- lib.ggml_flash_attn_back.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_flash_ff(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b0,
- # struct ggml_tensor * b1,
- # struct ggml_tensor * c0,
- # struct ggml_tensor * c1);
- def ggml_flash_ff(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b0: ggml_tensor_p,
- b1: ggml_tensor_p,
- c0: ggml_tensor_p,
- c1: ggml_tensor_p,
- ) -> ggml_tensor_p:
- return lib.ggml_flash_ff(ctx, a, b0, b1, c0, c1)
- lib.ggml_flash_ff.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_flash_ff.restype = ctypes.POINTER(ggml_tensor)
- # // partition into non-overlapping windows with padding if needed
- # // example:
- # // a: 768 64 64 1
- # // w: 14
- # // res: 768 14 14 25
- # // used in sam
- # GGML_API struct ggml_tensor * ggml_win_part(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # int w);
- def ggml_win_part(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- w: Union[ctypes.c_int, int],
- ) -> ggml_tensor_p:
- return lib.ggml_win_part(ctx, a, w)
- lib.ggml_win_part.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int,
- ]
- lib.ggml_win_part.restype = ctypes.POINTER(ggml_tensor)
- # // reverse of ggml_win_part
- # // used in sam
- # GGML_API struct ggml_tensor * ggml_win_unpart(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # int w0,
- # int h0,
- # int w);
- def ggml_win_unpart(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- w0: Union[ctypes.c_int, int],
- h0: Union[ctypes.c_int, int],
- w: Union[ctypes.c_int, int],
- ) -> ggml_tensor_p:
- return lib.ggml_win_unpart(ctx, a, w0, h0, w)
- lib.ggml_win_unpart.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int,
- ctypes.c_int,
- ctypes.c_int,
- ]
- lib.ggml_win_unpart.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_unary(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # enum ggml_unary_op op);
- def ggml_unary(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- op: Union[ctypes.c_int, int],
- ) -> ggml_tensor_p:
- return lib.ggml_unary(ctx, a, op)
- lib.ggml_unary.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int,
- ]
- lib.ggml_unary.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_unary_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # enum ggml_unary_op op);
- def ggml_unary_inplace(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- op: Union[ctypes.c_int, int],
- ) -> ggml_tensor_p:
- return lib.ggml_unary_inplace(ctx, a, op)
- lib.ggml_unary_inplace.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int,
- ]
- lib.ggml_unary_inplace.restype = ctypes.POINTER(ggml_tensor)
- # // used in sam
- # GGML_API struct ggml_tensor * ggml_get_rel_pos(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # int qh,
- # int kh);
- def ggml_get_rel_pos(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- qh: Union[ctypes.c_int, int],
- kh: Union[ctypes.c_int, int],
- ) -> ggml_tensor_p:
- return lib.ggml_get_rel_pos(ctx, a, qh, kh)
- lib.ggml_get_rel_pos.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int,
- ctypes.c_int,
- ]
- lib.ggml_get_rel_pos.restype = ctypes.POINTER(ggml_tensor)
- # // used in sam
- # GGML_API struct ggml_tensor * ggml_add_rel_pos(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * pw,
- # struct ggml_tensor * ph);
- def ggml_add_rel_pos(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- pw: ggml_tensor_p,
- ph: ggml_tensor_p,
- ) -> ggml_tensor_p:
- return lib.ggml_add_rel_pos(ctx, a, pw, ph)
- lib.ggml_add_rel_pos.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_add_rel_pos.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_add_rel_pos_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * pw,
- # struct ggml_tensor * ph);
- def ggml_add_rel_pos_inplace(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- pw: ggml_tensor_p,
- ph: ggml_tensor_p,
- ) -> ggml_tensor_p:
- return lib.ggml_add_rel_pos_inplace(ctx, a, pw, ph)
- lib.ggml_add_rel_pos_inplace.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_add_rel_pos_inplace.restype = ctypes.POINTER(ggml_tensor)
- # // custom operators (DEPRECATED)
- # typedef void (*ggml_unary_op_f32_t)(const int, float *, const float *);
- ggml_unary_op_f32_t = ctypes.CFUNCTYPE(
- None, ctypes.c_int, ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_float)
- )
- # typedef void (*ggml_binary_op_f32_t)(const int, float *, const float *, const float *);
- ggml_binary_op_f32_t = ctypes.CFUNCTYPE(
- None,
- ctypes.c_int,
- ctypes.POINTER(ctypes.c_float),
- ctypes.POINTER(ctypes.c_float),
- ctypes.POINTER(ctypes.c_float),
- )
- # typedef void (*ggml_custom1_op_f32_t)(struct ggml_tensor *, const struct ggml_tensor *);
- ggml_custom1_op_f32_t = ctypes.CFUNCTYPE(
- None, ctypes.POINTER(ggml_tensor), ctypes.POINTER(ggml_tensor)
- )
- """Unary operator function type"""
- # typedef void (*ggml_custom2_op_f32_t)(struct ggml_tensor *, const struct ggml_tensor *, const struct ggml_tensor *);
- ggml_custom2_op_f32_t = ctypes.CFUNCTYPE(
- None,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- )
- """Binary operator function type"""
- # typedef void (*ggml_custom3_op_f32_t)(struct ggml_tensor *, const struct ggml_tensor *, const struct ggml_tensor *, const struct ggml_tensor *);
- ggml_custom3_op_f32_t = ctypes.CFUNCTYPE(
- None,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- )
- """Ternary operator function type"""
- # GGML_API struct ggml_tensor * ggml_map_unary_f32(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # ggml_unary_op_f32_t fun);
- def ggml_map_unary_f32(
- ctx: ggml_context_p, a: ggml_tensor_p, fun: "ctypes._FuncPointer" # type: ignore
- ) -> ggml_tensor_p:
- return lib.ggml_map_unary_f32(ctx, a, fun)
- lib.ggml_map_unary_f32.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ggml_unary_op_f32_t,
- ]
- lib.ggml_map_unary_f32.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_map_unary_inplace_f32(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # ggml_unary_op_f32_t fun);
- def ggml_map_unary_inplace_f32(
- ctx: ggml_context_p, a: ggml_tensor_p, fun: "ctypes._FuncPointer" # type: ignore
- ) -> ggml_tensor_p:
- return lib.ggml_map_unary_inplace_f32(ctx, a, fun)
- lib.ggml_map_unary_inplace_f32.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ggml_unary_op_f32_t,
- ]
- lib.ggml_map_unary_inplace_f32.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_map_binary_f32(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b,
- # ggml_binary_op_f32_t fun);
- def ggml_map_binary_f32(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- fun: "ctypes._FuncPointer", # type: ignore
- ) -> ggml_tensor_p:
- return lib.ggml_map_binary_f32(ctx, a, b, fun)
- lib.ggml_map_binary_f32.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ggml_binary_op_f32_t,
- ]
- lib.ggml_map_binary_f32.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_map_binary_inplace_f32(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b,
- # ggml_binary_op_f32_t fun);
- def ggml_map_binary_inplace_f32(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- fun: "ctypes._FuncPointer", # type: ignore
- ) -> ggml_tensor_p:
- return lib.ggml_map_binary_inplace_f32(ctx, a, b, fun)
- lib.ggml_map_binary_inplace_f32.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ggml_binary_op_f32_t,
- ]
- lib.ggml_map_binary_inplace_f32.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_map_custom1_f32(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # ggml_custom1_op_f32_t fun);
- def ggml_map_custom1_f32(
- ctx: ggml_context_p, a: ggml_tensor_p, fun: "ctypes._FuncPointer" # type: ignore
- ) -> ggml_tensor_p:
- """Custom unary operator on a tensor.
- Example:
- ```python
- import ggml
- @ggml.ggml_custom1_op_f32_t
- def custom_op(b: ggml.tensor_p, a: ggml.tensor_p):
- # do something with a and copy to b
- return
- ...
- b = ggml.ggml_map_custom1_f32(ctx, a, custom_op)
- ```
- Parameters:
- a: input tensor
- fun (ggml.ggml_custom1_op_f32_t): function to apply to each element
- Returns:
- output tensor"""
- return lib.ggml_map_custom1_f32(ctx, a, fun)
- lib.ggml_map_custom1_f32.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ggml_custom1_op_f32_t,
- ]
- lib.ggml_map_custom1_f32.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_map_custom1_inplace_f32(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # ggml_custom1_op_f32_t fun);
- def ggml_map_custom1_inplace_f32(
- ctx: ggml_context_p, a: ggml_tensor_p, fun: "ctypes._CFuncPtr" # type: ignore
- ) -> ggml_tensor_p:
- """Custom unary operator on a tensor inplace.
- Parameters:
- a: input tensor
- fun (ggml.ggml_custom1_op_f32_t): function to apply to each element
- Returns:
- output tensor"""
- return lib.ggml_map_custom1_inplace_f32(ctx, a, fun)
- lib.ggml_map_custom1_inplace_f32.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ggml_custom1_op_f32_t,
- ]
- lib.ggml_map_custom1_inplace_f32.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_map_custom2_f32(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b,
- # ggml_custom2_op_f32_t fun);
- def ggml_map_custom2_f32(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- fun: "ctypes._FuncPointer", # type: ignore
- ) -> ggml_tensor_p:
- """Custom binary operator on two tensors.
- Parameters:
- a: input tensor
- b: input tensor
- fun (ggml.ggml_custom2_op_f32_t): function to apply to each element
- Returns:
- output tensor"""
- return lib.ggml_map_custom2_f32(ctx, a, b, fun)
- lib.ggml_map_custom2_f32.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ggml_custom2_op_f32_t,
- ]
- lib.ggml_map_custom2_f32.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_map_custom2_inplace_f32(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b,
- # ggml_custom2_op_f32_t fun);
- def ggml_map_custom2_inplace_f32(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- fun: "ctypes._FuncPointer", # type: ignore
- ) -> ggml_tensor_p:
- """Custom binary operator on two tensors inplace.
- Parameters:
- a: input tensor
- b: input tensor
- fun (ggml.ggml_custom2_op_f32_t): function to apply to each element
- Returns:
- output tensor"""
- return lib.ggml_map_custom2_inplace_f32(ctx, a, b, fun)
- lib.ggml_map_custom2_inplace_f32.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ggml_custom2_op_f32_t,
- ]
- lib.ggml_map_custom2_inplace_f32.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_map_custom3_f32(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b,
- # struct ggml_tensor * c,
- # ggml_custom3_op_f32_t fun);
- def ggml_map_custom3_f32(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- c: ggml_tensor_p,
- fun: "ctypes._FuncPointer", # type: ignore
- ) -> ggml_tensor_p:
- """Custom ternary operator on three tensors.
- Parameters:
- a: input tensor
- b: input tensor
- c: input tensor
- fun (ggml.ggml_custom3_op_f32_t): function to apply to each element
- Returns:
- output tensor"""
- return lib.ggml_map_custom3_f32(ctx, a, b, c, fun)
- lib.ggml_map_custom3_f32.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ggml_custom3_op_f32_t,
- ]
- lib.ggml_map_custom3_f32.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_map_custom3_inplace_f32(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b,
- # struct ggml_tensor * c,
- # ggml_custom3_op_f32_t fun);
- def ggml_map_custom3_inplace_f32(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- c: ggml_tensor_p,
- fun: "ctypes._FuncPointer", # type: ignore
- ) -> ggml_tensor_p:
- """Custom ternary operator on three tensors inplace.
- Parameters:
- a: input tensor
- b: input tensor
- c: input tensor
- fun (ggml.ggml_custom3_op_f32_t): function to apply to each element
- Returns:
- output tensor"""
- return lib.ggml_map_custom3_inplace_f32(ctx, a, b, c, fun)
- lib.ggml_map_custom3_inplace_f32.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ggml_custom3_op_f32_t,
- ]
- lib.ggml_map_custom3_inplace_f32.restype = ctypes.POINTER(ggml_tensor)
- # // custom operators v2
- # typedef void (*ggml_custom1_op_t)(struct ggml_tensor * dst , const struct ggml_tensor * a, int ith, int nth, void * userdata);
- ggml_custom1_op_t = ctypes.CFUNCTYPE(
- None,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int,
- ctypes.c_int,
- ctypes.c_void_p,
- )
- """Custom unary operator on a tensor."""
- # typedef void (*ggml_custom2_op_t)(struct ggml_tensor * dst , const struct ggml_tensor * a, const struct ggml_tensor * b, int ith, int nth, void * userdata);
- ggml_custom2_op_t = ctypes.CFUNCTYPE(
- None,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int,
- ctypes.c_int,
- ctypes.c_void_p,
- )
- """Custom binary operator on two tensors."""
- # typedef void (*ggml_custom3_op_t)(struct ggml_tensor * dst , const struct ggml_tensor * a, const struct ggml_tensor * b, const struct ggml_tensor * c, int ith, int nth, void * userdata);
- ggml_custom3_op_t = ctypes.CFUNCTYPE(
- None,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.c_int,
- ctypes.c_int,
- ctypes.c_void_p,
- )
- """Custom ternary operator on three tensors."""
- # #define GGML_N_TASKS_MAX -1
- GGML_N_TASKS_MAX = -1
- # GGML_API struct ggml_tensor * ggml_map_custom1(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # ggml_custom1_op_t fun,
- # int n_tasks,
- # void * userdata);
- def ggml_map_custom1(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- fun: "ctypes._FuncPointer", # type: ignore
- n_tasks: Union[ctypes.c_int, int],
- userdata: Optional[ctypes.c_void_p],
- ) -> ggml_tensor_p:
- return lib.ggml_map_custom1(ctx, a, fun, n_tasks, userdata)
- lib.ggml_map_custom1.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ggml_custom1_op_t,
- ctypes.c_int,
- ctypes.c_void_p,
- ]
- lib.ggml_map_custom1.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_map_custom1_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # ggml_custom1_op_t fun,
- # int n_tasks,
- # void * userdata);
- def ggml_map_custom1_inplace(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- fun: "ctypes._FuncPointer", # type: ignore
- n_tasks: Union[ctypes.c_int, int],
- userdata: Optional[ctypes.c_void_p],
- ) -> ggml_tensor_p:
- return lib.ggml_map_custom1_inplace(ctx, a, fun, n_tasks, userdata)
- lib.ggml_map_custom1_inplace.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ggml_custom1_op_t,
- ctypes.c_int,
- ctypes.c_void_p,
- ]
- lib.ggml_map_custom1_inplace.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_map_custom2(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b,
- # ggml_custom2_op_t fun,
- # int n_tasks,
- # void * userdata);
- def ggml_map_custom2(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- fun: "ctypes._FuncPointer", # type: ignore
- n_tasks: Union[ctypes.c_int, int],
- userdata: Optional[ctypes.c_void_p],
- ) -> ggml_tensor_p:
- return lib.ggml_map_custom2(ctx, a, b, fun, n_tasks, userdata)
- lib.ggml_map_custom2.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ggml_custom2_op_t,
- ctypes.c_int,
- ctypes.c_void_p,
- ]
- lib.ggml_map_custom2.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_map_custom2_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b,
- # ggml_custom2_op_t fun,
- # int n_tasks,
- # void * userdata);
- def ggml_map_custom2_inplace(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- fun: "ctypes._FuncPointer", # type: ignore
- n_tasks: Union[ctypes.c_int, int],
- userdata: Optional[ctypes.c_void_p],
- ) -> ggml_tensor_p:
- return lib.ggml_map_custom2_inplace(ctx, a, b, fun, n_tasks, userdata)
- lib.ggml_map_custom2_inplace.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ggml_custom2_op_t,
- ctypes.c_int,
- ctypes.c_void_p,
- ]
- lib.ggml_map_custom2_inplace.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_map_custom3(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b,
- # struct ggml_tensor * c,
- # ggml_custom3_op_t fun,
- # int n_tasks,
- # void * userdata);
- def ggml_map_custom3(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- c: ggml_tensor_p,
- fun: "ctypes._FuncPointer", # type: ignore
- n_tasks: Union[ctypes.c_int, int],
- userdata: Optional[ctypes.c_void_p],
- ) -> ggml_tensor_p:
- return lib.ggml_map_custom3(ctx, a, b, c, fun, n_tasks, userdata)
- lib.ggml_map_custom3.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ggml_custom3_op_t,
- ctypes.c_int,
- ctypes.c_void_p,
- ]
- lib.ggml_map_custom3.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_map_custom3_inplace(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b,
- # struct ggml_tensor * c,
- # ggml_custom3_op_t fun,
- # int n_tasks,
- # void * userdata);
- def ggml_map_custom3_inplace(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- c: ggml_tensor_p,
- fun: "ctypes._FuncPointer", # type: ignore
- n_tasks: Union[ctypes.c_int, int],
- userdata: Optional[ctypes.c_void_p],
- ) -> ggml_tensor_p:
- return lib.ggml_map_custom3_inplace(ctx, a, b, c, fun, n_tasks, userdata)
- lib.ggml_map_custom3_inplace.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ggml_custom3_op_t,
- ctypes.c_int,
- ctypes.c_void_p,
- ]
- lib.ggml_map_custom3_inplace.restype = ctypes.POINTER(ggml_tensor)
- # // loss function
- # GGML_API struct ggml_tensor * ggml_cross_entropy_loss(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b);
- def ggml_cross_entropy_loss(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- ) -> ggml_tensor_p:
- return lib.ggml_cross_entropy_loss(ctx, a, b)
- lib.ggml_cross_entropy_loss.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_cross_entropy_loss.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API struct ggml_tensor * ggml_cross_entropy_loss_back(
- # struct ggml_context * ctx,
- # struct ggml_tensor * a,
- # struct ggml_tensor * b,
- # struct ggml_tensor * c);
- def ggml_cross_entropy_loss_back(
- ctx: ggml_context_p,
- a: ggml_tensor_p,
- b: ggml_tensor_p,
- c: ggml_tensor_p,
- ) -> ggml_tensor_p:
- return lib.ggml_cross_entropy_loss_back(ctx, a, b, c)
- lib.ggml_cross_entropy_loss_back.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_cross_entropy_loss_back.restype = ctypes.POINTER(ggml_tensor)
- # //
- # // automatic differentiation
- # //
- # GGML_API void ggml_set_param(
- # struct ggml_context * ctx,
- # struct ggml_tensor * tensor);
- def ggml_set_param(ctx: ggml_context_p, tensor: ggml_tensor_p):
- return lib.ggml_set_param(ctx, tensor)
- lib.ggml_set_param.argtypes = [ggml_context_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_set_param.restype = None
- # GGML_API void ggml_build_forward_expand (struct ggml_cgraph * cgraph, struct ggml_tensor * tensor);
- def ggml_build_forward_expand(
- cgraph: ggml_cgraph_p,
- tensor: ggml_tensor_p,
- ):
- """Add a tensor to the forward computation graph. This is used to
- compute and save the value of the tensor.
- Parameters:
- cgraph: The graph.
- tensor: The tensor."""
- return lib.ggml_build_forward_expand(cgraph, tensor)
- lib.ggml_build_forward_expand.argtypes = [
- ctypes.POINTER(ggml_cgraph),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_build_forward_expand.restype = None
- # GGML_API void ggml_build_backward_expand(struct ggml_context * ctx, struct ggml_cgraph * gf, struct ggml_cgraph * gb, bool keep);
- def ggml_build_backward_expand(
- ctx: ggml_context_p,
- gf: ggml_cgraph_p,
- gb: ggml_cgraph_p,
- keep: Union[ctypes.c_bool, bool],
- ):
- """Add a tensor to the backward computation graph. This is used to
- compute the gradient of the tensor.
- Parameters:
- ctx: The context.
- gf: The forward graph.
- gb: The backward graph.
- keep: Whether to keep the tensor."""
- return lib.ggml_build_backward_expand(ctx, gf, gb, keep)
- lib.ggml_build_backward_expand.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_cgraph),
- ctypes.POINTER(ggml_cgraph),
- ctypes.c_bool,
- ]
- lib.ggml_build_backward_expand.restype = None
- # GGML_API struct ggml_cgraph ggml_build_forward (struct ggml_tensor * tensor);
- def ggml_build_forward(
- tensor: ggml_tensor_p,
- ) -> ggml_cgraph:
- """Build the forward computation graph.
- Parameters:
- tensor: The tensor.
- Returns:
- The graph."""
- return lib.ggml_build_forward(tensor)
- lib.ggml_build_forward.argtypes = [ctypes.POINTER(ggml_tensor)]
- lib.ggml_build_forward.restype = ggml_cgraph
- # GGML_API struct ggml_cgraph ggml_build_backward(struct ggml_context * ctx, struct ggml_cgraph * gf, bool keep);
- def ggml_build_backward(
- ctx: ggml_context_p,
- gf: ggml_cgraph_p,
- keep: Union[ctypes.c_bool, bool],
- ) -> ggml_cgraph:
- return lib.ggml_build_backward(ctx, gf, keep)
- lib.ggml_build_backward.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_cgraph),
- ctypes.c_bool,
- ]
- lib.ggml_build_backward.restype = ggml_cgraph
- # // graph allocation in a context
- # GGML_API struct ggml_cgraph * ggml_new_graph (struct ggml_context * ctx);
- def ggml_new_graph(
- ctx: ggml_context_p,
- ) -> ggml_cgraph:
- """Create a new graph.
- Parameters:
- ctx: The context.
- Returns:
- The graph."""
- return lib.ggml_new_graph(ctx)
- lib.ggml_new_graph.argtypes = [ggml_context_p]
- lib.ggml_new_graph.restype = ggml_cgraph
- # GGML_API struct ggml_cgraph * ggml_build_forward_ctx(struct ggml_context * ctx, struct ggml_tensor * tensor);
- def ggml_build_forward_ctx(
- ctx: ggml_context_p,
- tensor: ggml_tensor_p,
- ) -> ggml_cgraph:
- """Build the forward computation graph in a context.
- Parameters:
- ctx: The context.
- tensor: The tensor.
- Returns:
- The graph."""
- return lib.ggml_build_forward_ctx(ctx, tensor)
- lib.ggml_build_forward_ctx.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_build_forward_ctx.restype = ggml_cgraph
- # GGML_API size_t ggml_graph_overhead(void);
- def ggml_graph_overhead() -> int:
- """Get the overhead of the graph."""
- return lib.ggml_graph_overhead()
- lib.ggml_graph_overhead.argtypes = []
- lib.ggml_graph_overhead.restype = ctypes.c_size_t
- # // ggml_graph_plan() has to be called before ggml_graph_compute()
- # // when plan.work_size > 0, caller must allocate memory for plan.work_data
- # GGML_API struct ggml_cplan ggml_graph_plan (struct ggml_cgraph * cgraph, int n_threads /*= GGML_DEFAULT_N_THREADS*/);
- def ggml_graph_plan(
- cgraph: ggml_cgraph_p,
- n_threads: Union[ctypes.c_int, int] = GGML_DEFAULT_N_THREADS,
- ) -> ggml_cplan:
- """Plan the computation graph.
- Parameters:
- cgraph: The graph.
- n_threads: The number of threads to use.
- Returns:
- The plan."""
- return lib.ggml_graph_plan(cgraph, n_threads)
- lib.ggml_graph_plan.argtypes = [
- ctypes.POINTER(ggml_cgraph),
- ctypes.c_int,
- ]
- lib.ggml_graph_plan.restype = ggml_cplan
- # GGML_API int ggml_graph_compute(struct ggml_cgraph * cgraph, struct ggml_cplan * cplan);
- def ggml_graph_compute(
- cgraph: ggml_cgraph_p,
- cplan: ggml_cplan_p,
- ) -> int:
- """Compute the graph.
- Parameters:
- cgraph: The graph.
- cplan: The plan."""
- return lib.ggml_graph_compute(cgraph, cplan)
- lib.ggml_graph_compute.argtypes = [
- ctypes.POINTER(ggml_cgraph),
- ctypes.POINTER(ggml_cplan),
- ]
- lib.ggml_graph_compute.restype = ctypes.c_int
- # GGML_API void ggml_graph_reset (struct ggml_cgraph * cgraph);
- def ggml_graph_reset(
- cgraph: ggml_cgraph_p,
- ):
- """Reset the graph.
- Parameters:
- cgraph: The graph."""
- return lib.ggml_graph_reset(cgraph)
- # // same as ggml_graph_compute() but the work data is allocated as a part of the context
- # // note: the drawback of this API is that you must have ensured that the context has enough memory for the work data
- # GGML_API void ggml_graph_compute_with_ctx(struct ggml_context * ctx, struct ggml_cgraph * cgraph, int n_threads);
- def ggml_graph_compute_with_ctx(
- ctx: ggml_context_p,
- cgraph: ggml_cgraph_p,
- n_threads: Union[ctypes.c_int, int],
- ):
- """Compute the graph with a context.
- Parameters:
- ctx: The context.
- cgraph: The graph.
- n_threads: The number of threads to use."""
- return lib.ggml_graph_compute_with_ctx(ctx, cgraph, n_threads)
- lib.ggml_graph_compute_with_ctx.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_cgraph),
- ctypes.c_int,
- ]
- lib.ggml_graph_compute_with_ctx.restype = None
- # GGML_API struct ggml_tensor * ggml_graph_get_tensor(struct ggml_cgraph * cgraph, const char * name);
- def ggml_graph_get_tensor(
- cgraph: ggml_cgraph_p,
- name: bytes,
- ) -> ggml_tensor_p:
- """Get a tensor from the graph by name.
- Parameters:
- cgraph: The graph.
- name: The name of the tensor.
- Returns:
- The tensor."""
- return lib.ggml_graph_get_tensor(cgraph, name)
- lib.ggml_graph_get_tensor.argtypes = [
- ctypes.POINTER(ggml_cgraph),
- ctypes.c_char_p,
- ]
- lib.ggml_graph_get_tensor.restype = ctypes.POINTER(ggml_tensor)
- # GGML_API void ggml_graph_export(const struct ggml_cgraph * cgraph, const char * fname);
- def ggml_graph_export(
- cgraph: ggml_cgraph_p,
- fname: bytes,
- ):
- return lib.ggml_graph_export(cgraph, fname)
- lib.ggml_graph_export.argtypes = [
- ctypes.POINTER(ggml_cgraph),
- ctypes.c_char_p,
- ]
- lib.ggml_graph_export.restype = None
- # GGML_API struct ggml_cgraph ggml_graph_import(const char * fname, struct ggml_context ** ctx_data, struct ggml_context ** ctx_eval);
- def ggml_graph_import(
- fname: bytes,
- ctx_data: "ctypes._Pointer[ggml_context_p]", # type: ignore
- ctx_eval: "ctypes._Pointer[ggml_context_p]", # type: ignore
- ) -> ggml_cgraph:
- return lib.ggml_graph_import(fname, ctx_data, ctx_eval)
- lib.ggml_graph_import.argtypes = [
- ctypes.c_char_p,
- ctypes.POINTER(ggml_context_p),
- ctypes.POINTER(ggml_context_p),
- ]
- lib.ggml_graph_import.restype = ggml_cgraph
- # // print info and performance information for the graph
- # GGML_API void ggml_graph_print(const struct ggml_cgraph * cgraph);
- def ggml_graph_print(
- cgraph: ggml_cgraph_p,
- ):
- return lib.ggml_graph_print(cgraph)
- lib.ggml_graph_print.argtypes = [ctypes.POINTER(ggml_cgraph)]
- lib.ggml_graph_print.restype = None
- # // dump the graph into a file using the dot format
- # GGML_API void ggml_graph_dump_dot(const struct ggml_cgraph * gb, const struct ggml_cgraph * gf, const char * filename);
- def ggml_graph_dump_dot(
- gb: ggml_cgraph_p,
- gf: ggml_cgraph_p,
- filename: bytes,
- ):
- return lib.ggml_graph_dump_dot(gb, gf, filename)
- lib.ggml_graph_dump_dot.argtypes = [
- ctypes.POINTER(ggml_cgraph),
- ctypes.POINTER(ggml_cgraph),
- ctypes.c_char_p,
- ]
- lib.ggml_graph_dump_dot.restype = None
- # //
- # // optimization
- # //
- # // optimization methods
- # enum ggml_opt_type {
- # GGML_OPT_ADAM,
- # GGML_OPT_LBFGS,
- # };
- GGML_OPT_ADAM = 0
- GGML_OPT_LBFGS = 1
- # // linesearch methods
- # enum ggml_linesearch {
- # GGML_LINESEARCH_DEFAULT = 1,
- # GGML_LINESEARCH_BACKTRACKING_ARMIJO = 0,
- # GGML_LINESEARCH_BACKTRACKING_WOLFE = 1,
- # GGML_LINESEARCH_BACKTRACKING_STRONG_WOLFE = 2,
- # };
- GGML_LINESEARCH_DEFAULT = 1
- GGML_LINESEARCH_BACKTRACKING_ARMIJO = 0
- GGML_LINESEARCH_BACKTRACKING_WOLFE = 1
- GGML_LINESEARCH_BACKTRACKING_STRONG_WOLFE = 2
- # // optimization return values
- # enum ggml_opt_result {
- # GGML_OPT_OK = 0,
- # GGML_OPT_DID_NOT_CONVERGE,
- # GGML_OPT_NO_CONTEXT,
- # GGML_OPT_INVALID_WOLFE,
- # GGML_OPT_FAIL,
- # GGML_LINESEARCH_FAIL = -128,
- # GGML_LINESEARCH_MINIMUM_STEP,
- # GGML_LINESEARCH_MAXIMUM_STEP,
- # GGML_LINESEARCH_MAXIMUM_ITERATIONS,
- # GGML_LINESEARCH_INVALID_PARAMETERS,
- # };
- GGML_OPT_OK = 0
- GGML_OPT_DID_NOT_CONVERGE = 1
- GGML_OPT_NO_CONTEXT = 2
- GGML_OPT_INVALID_WOLFE = 3
- GGML_OPT_FAIL = 4
- GGML_LINESEARCH_FAIL = -128
- GGML_LINESEARCH_MINIMUM_STEP = -127
- GGML_LINESEARCH_MAXIMUM_STEP = -126
- GGML_LINESEARCH_MAXIMUM_ITERATIONS = -125
- GGML_LINESEARCH_INVALID_PARAMETERS = -124
- # typedef void (*ggml_opt_callback)(void * data, float * sched);
- ggml_opt_callback = ctypes.CFUNCTYPE(
- None,
- ctypes.c_void_p,
- ctypes.POINTER(ctypes.c_float),
- )
- # // optimization parameters
- # //
- # // see ggml.c (ggml_opt_default_params) for default values
- # //
- # struct ggml_opt_params {
- # enum ggml_opt_type type;
- # int n_threads;
- # // delta-based convergence test
- # //
- # // if past == 0 - disabled
- # // if past > 0:
- # // stop if |f(x) - f(x_past)| < delta * max(1, |f(x)|)
- # //
- # int past;
- # float delta;
- # // maximum number of iterations without improvement
- # //
- # // if 0 - disabled
- # // if > 0:
- # // assume convergence if no cost improvement in this number of iterations
- # //
- # int max_no_improvement;
- # bool print_forward_graph;
- # bool print_backward_graph;
- # // ADAM parameters
- # struct {
- # int n_iter;
- # float sched; // schedule multiplier (fixed, decay or warmup)
- # float decay; // weight decay for AdamW, use 0.0f to disable
- # int decay_min_ndim; // minimum number of tensor dimension to apply weight decay
- # float alpha; // learning rate
- # float beta1;
- # float beta2;
- # float eps; // epsilon for numerical stability
- # float eps_f; // epsilon for convergence test
- # float eps_g; // epsilon for convergence test
- # float gclip; // gradient clipping
- # } adam;
- # // LBFGS parameters
- # struct {
- # int m; // number of corrections to approximate the inv. Hessian
- # int n_iter;
- # int max_linesearch;
- # float eps; // convergence tolerance
- # float ftol; // line search tolerance
- # float wolfe;
- # float min_step;
- # float max_step;
- # enum ggml_linesearch linesearch;
- # } lbfgs;
- # };
- class ggml_opt_params_adam(ctypes.Structure):
- _fields_ = [
- ("n_iter", ctypes.c_int),
- ("sched", ctypes.c_float),
- ("decay", ctypes.c_float),
- ("decay_min_ndim", ctypes.c_int),
- ("alpha", ctypes.c_float),
- ("beta1", ctypes.c_float),
- ("beta2", ctypes.c_float),
- ("eps", ctypes.c_float),
- ("eps_f", ctypes.c_float),
- ("eps_g", ctypes.c_float),
- ("gclip", ctypes.c_float),
- ]
- class ggml_opt_params_lbfgs(ctypes.Structure):
- _fields_ = [
- ("m", ctypes.c_int),
- ("n_iter", ctypes.c_int),
- ("max_linesearch", ctypes.c_int),
- ("eps", ctypes.c_float),
- ("ftol", ctypes.c_float),
- ("wolfe", ctypes.c_float),
- ("min_step", ctypes.c_float),
- ("max_step", ctypes.c_float),
- ("linesearch", ctypes.c_int),
- ]
- class ggml_opt_params(ctypes.Structure):
- _fields_ = [
- ("type", ctypes.c_int),
- ("n_threads", ctypes.c_int),
- ("past", ctypes.c_int),
- ("delta", ctypes.c_float),
- ("max_no_improvement", ctypes.c_int),
- ("print_forward_graph", ctypes.c_bool),
- ("print_backward_graph", ctypes.c_bool),
- ("adam", ggml_opt_params_adam),
- ("lbfgs", ggml_opt_params_lbfgs),
- ]
- # struct ggml_opt_context {
- # struct ggml_context * ctx;
- # struct ggml_opt_params params;
- # int iter;
- # int64_t nx; // number of parameter elements
- # bool just_initialized;
- # float loss_before;
- # float loss_after;
- # struct {
- # struct ggml_tensor * m; // first moment
- # struct ggml_tensor * v; // second moment
- # struct ggml_tensor * pf; // past function values
- # float fx_best;
- # float fx_prev;
- # int n_no_improvement;
- # } adam;
- # struct {
- # struct ggml_tensor * x; // current parameters
- # struct ggml_tensor * xp; // previous parameters
- # struct ggml_tensor * g; // current gradient
- # struct ggml_tensor * gp; // previous gradient
- # struct ggml_tensor * d; // search direction
- # struct ggml_tensor * pf; // past function values
- # struct ggml_tensor * lmal; // the L-BFGS memory alpha
- # struct ggml_tensor * lmys; // the L-BFGS memory ys
- # struct ggml_tensor * lms; // the L-BFGS memory s
- # struct ggml_tensor * lmy; // the L-BFGS memory y
- # float fx_best;
- # float step;
- # int j;
- # int k;
- # int end;
- # int n_no_improvement;
- # } lbfgs;
- # };
- class ggml_opt_context_adam(ctypes.Structure):
- _fields_ = [
- ("m", ctypes.POINTER(ggml_tensor)),
- ("v", ctypes.POINTER(ggml_tensor)),
- ("pf", ctypes.POINTER(ggml_tensor)),
- ("fx_best", ctypes.c_float),
- ("fx_prev", ctypes.c_float),
- ("n_no_improvement", ctypes.c_int),
- ]
- class ggml_opt_context_lbfgs(ctypes.Structure):
- _fields_ = [
- ("x", ctypes.POINTER(ggml_tensor)),
- ("xp", ctypes.POINTER(ggml_tensor)),
- ("g", ctypes.POINTER(ggml_tensor)),
- ("gp", ctypes.POINTER(ggml_tensor)),
- ("d", ctypes.POINTER(ggml_tensor)),
- ("pf", ctypes.POINTER(ggml_tensor)),
- ("lmal", ctypes.POINTER(ggml_tensor)),
- ("lmys", ctypes.POINTER(ggml_tensor)),
- ("lms", ctypes.POINTER(ggml_tensor)),
- ("lmy", ctypes.POINTER(ggml_tensor)),
- ("fx_best", ctypes.c_float),
- ("step", ctypes.c_float),
- ("j", ctypes.c_int),
- ("k", ctypes.c_int),
- ("end", ctypes.c_int),
- ("n_no_improvement", ctypes.c_int),
- ]
- class ggml_opt_context(ctypes.Structure):
- _fields_ = [
- ("ctx", ggml_context_p),
- ("params", ggml_opt_params),
- ("iter", ctypes.c_int),
- ("nx", ctypes.c_int64),
- ("just_initialized", ctypes.c_bool),
- ("loss_before", ctypes.c_float),
- ("loss_after", ctypes.c_float),
- ("adam", ggml_opt_context_adam),
- ("lbfgs", ggml_opt_context_lbfgs),
- ]
- ggml_opt_context_p = ctypes.POINTER(ggml_opt_context)
- # GGML_API struct ggml_opt_params ggml_opt_default_params(enum ggml_opt_type type);
- def ggml_opt_default_params(type: Union[ctypes.c_int, bool]) -> ggml_opt_params:
- return lib.ggml_opt_default_params(type)
- lib.ggml_opt_default_params.argtypes = [ctypes.c_int]
- lib.ggml_opt_default_params.restype = ggml_opt_params
- # // optimize the function defined by the tensor f
- # GGML_API enum ggml_opt_result ggml_opt(
- # struct ggml_context * ctx,
- # struct ggml_opt_params params,
- # struct ggml_tensor * f);
- def ggml_opt(
- ctx: ggml_context_p,
- params: ggml_opt_params,
- f: ggml_tensor_p,
- ) -> int:
- return lib.ggml_opt(ctx, params, f)
- lib.ggml_opt.argtypes = [ggml_context_p, ggml_opt_params, ctypes.POINTER(ggml_tensor)]
- lib.ggml_opt.restype = ctypes.c_int
- # // initialize optimizer context
- # GGML_API void ggml_opt_init(
- # struct ggml_context * ctx,
- # struct ggml_opt_context * opt,
- # struct ggml_opt_params params,
- # int64_t nx);
- def ggml_opt_init(
- ctx: ggml_context_p,
- opt: "ctypes._Pointer[ggml_opt_context]", # type: ignore
- params: ggml_opt_params,
- nx: Union[ctypes.c_int64, int],
- ):
- return lib.ggml_opt_init(ctx, opt, params, nx)
- lib.ggml_opt_init.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_opt_context),
- ggml_opt_params,
- ctypes.c_int64,
- ]
- lib.ggml_opt_init.restype = None
- # // continue optimizing the function defined by the tensor f
- # GGML_API enum ggml_opt_result ggml_opt_resume(
- # struct ggml_context * ctx,
- # struct ggml_opt_context * opt,
- # struct ggml_tensor * f);
- def ggml_opt_resume(
- ctx: ggml_context_p,
- opt: "ctypes._Pointer[ggml_opt_context]", # type: ignore
- f: ggml_tensor_p,
- ) -> int:
- return lib.ggml_opt_resume(ctx, opt, f)
- lib.ggml_opt_resume.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_opt_context),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_opt_resume.restype = ctypes.c_int
- # // continue optimizing the function defined by the tensor f
- # GGML_API enum ggml_opt_result ggml_opt_resume_g(
- # struct ggml_context * ctx,
- # struct ggml_opt_context * opt,
- # struct ggml_tensor * f,
- # struct ggml_cgraph * gf,
- # struct ggml_cgraph * gb,
- # ggml_opt_callback callback,
- # void * callback_data);
- # // continue optimizing the function defined by the tensor f
- # GGML_API enum ggml_opt_result ggml_opt_resume_g(
- # struct ggml_context * ctx,
- # struct ggml_opt_context * opt,
- # struct ggml_tensor * f,
- # struct ggml_cgraph * gf,
- # struct ggml_cgraph * gb);
- def ggml_opt_resume_g(
- ctx: ggml_context_p,
- opt: "ctypes._Pointer[ggml_opt_context]", # type: ignore
- f: ggml_tensor_p,
- gf: ggml_cgraph_p,
- gb: ggml_cgraph_p,
- callback: ggml_opt_callback = None,
- callback_data: ctypes.c_void_p = None,
- ) -> int:
- return lib.ggml_opt_resume_g(ctx, opt, f, gf, gb, callback, callback_data)
- lib.ggml_opt_resume_g.argtypes = [
- ggml_context_p,
- ctypes.POINTER(ggml_opt_context),
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_cgraph),
- ctypes.POINTER(ggml_cgraph),
- ggml_opt_callback,
- ctypes.c_void_p,
- ]
- lib.ggml_opt_resume_g.restype = ctypes.c_int
- # //
- # // quantization
- # //
- # GGML_API size_t ggml_quantize_q4_0(const float * src, void * dst, int n, int k, int64_t * hist);
- def ggml_quantize_q4_0(
- src: CFloatArray,
- dst: ctypes.c_void_p,
- n: Union[ctypes.c_int, int],
- k: Union[ctypes.c_int, int],
- hist: CInt64Array,
- ) -> int:
- return lib.ggml_quantize_q4_0(src, dst, n, k, hist)
- lib.ggml_quantize_q4_0.argtypes = [
- ctypes.POINTER(ctypes.c_float),
- ctypes.c_void_p,
- ctypes.c_int,
- ctypes.c_int,
- ctypes.POINTER(ctypes.c_int64),
- ]
- lib.ggml_quantize_q4_0.restype = ctypes.c_size_t
- # GGML_API size_t ggml_quantize_q4_1(const float * src, void * dst, int n, int k, int64_t * hist);
- def ggml_quantize_q4_1(
- src: CFloatArray,
- dst: ctypes.c_void_p,
- n: Union[ctypes.c_int, int],
- k: Union[ctypes.c_int, int],
- hist: CInt64Array,
- ) -> int:
- return lib.ggml_quantize_q4_1(src, dst, n, k, hist)
- lib.ggml_quantize_q4_1.argtypes = [
- ctypes.POINTER(ctypes.c_float),
- ctypes.c_void_p,
- ctypes.c_int,
- ctypes.c_int,
- ctypes.POINTER(ctypes.c_int64),
- ]
- lib.ggml_quantize_q4_1.restype = ctypes.c_size_t
- # GGML_API size_t ggml_quantize_q5_0(const float * src, void * dst, int n, int k, int64_t * hist);
- def ggml_quantize_q5_0(
- src: CFloatArray,
- dst: ctypes.c_void_p,
- n: Union[ctypes.c_int, int],
- k: Union[ctypes.c_int, int],
- hist: CInt64Array,
- ) -> int:
- return lib.ggml_quantize_q5_0(src, dst, n, k, hist)
- lib.ggml_quantize_q5_0.argtypes = [
- ctypes.POINTER(ctypes.c_float),
- ctypes.c_void_p,
- ctypes.c_int,
- ctypes.c_int,
- ctypes.POINTER(ctypes.c_int64),
- ]
- lib.ggml_quantize_q5_0.restype = ctypes.c_size_t
- # GGML_API size_t ggml_quantize_q5_1(const float * src, void * dst, int n, int k, int64_t * hist);
- def ggml_quantize_q5_1(
- src: CFloatArray,
- dst: ctypes.c_void_p,
- n: Union[ctypes.c_int, int],
- k: Union[ctypes.c_int, int],
- hist: CInt64Array,
- ) -> int:
- return lib.ggml_quantize_q5_1(src, dst, n, k, hist)
- lib.ggml_quantize_q5_1.argtypes = [
- ctypes.POINTER(ctypes.c_float),
- ctypes.c_void_p,
- ctypes.c_int,
- ctypes.c_int,
- ctypes.POINTER(ctypes.c_int64),
- ]
- lib.ggml_quantize_q5_1.restype = ctypes.c_size_t
- # GGML_API size_t ggml_quantize_q8_0(const float * src, void * dst, int n, int k, int64_t * hist);
- def ggml_quantize_q8_0(
- src: CFloatArray,
- dst: ctypes.c_void_p,
- n: Union[ctypes.c_int, int],
- k: Union[ctypes.c_int, int],
- hist: CInt64Array,
- ) -> int:
- return lib.ggml_quantize_q8_0(src, dst, n, k, hist)
- lib.ggml_quantize_q8_0.argtypes = [
- ctypes.POINTER(ctypes.c_float),
- ctypes.c_void_p,
- ctypes.c_int,
- ctypes.c_int,
- ctypes.POINTER(ctypes.c_int64),
- ]
- lib.ggml_quantize_q8_0.restype = ctypes.c_size_t
- # GGML_API size_t ggml_quantize_chunk(enum ggml_type type, const float * src, void * dst, int start, int n, int64_t * hist);
- def ggml_quantize_chunk(
- type: Union[ctypes.c_int, int],
- src: CFloatArray,
- dst: ctypes.c_void_p,
- start: Union[ctypes.c_int, int],
- n: Union[ctypes.c_int, int],
- hist: CInt64Array,
- ) -> int:
- return lib.ggml_quantize_chunk(type, src, dst, start, n, hist)
- lib.ggml_quantize_chunk.argtypes = [
- ctypes.c_int,
- ctypes.POINTER(ctypes.c_float),
- ctypes.c_void_p,
- ctypes.c_int,
- ctypes.c_int,
- ctypes.POINTER(ctypes.c_int64),
- ]
- lib.ggml_quantize_chunk.restype = ctypes.c_size_t
- # //
- # // gguf
- # //
- # enum gguf_type {
- # GGUF_TYPE_UINT8 = 0,
- # GGUF_TYPE_INT8 = 1,
- # GGUF_TYPE_UINT16 = 2,
- # GGUF_TYPE_INT16 = 3,
- # GGUF_TYPE_UINT32 = 4,
- # GGUF_TYPE_INT32 = 5,
- # GGUF_TYPE_FLOAT32 = 6,
- # GGUF_TYPE_BOOL = 7,
- # GGUF_TYPE_STRING = 8,
- # GGUF_TYPE_ARRAY = 9,
- # GGUF_TYPE_UINT64 = 10,
- # GGUF_TYPE_INT64 = 11,
- # GGUF_TYPE_FLOAT64 = 12,
- # GGUF_TYPE_COUNT, // marks the end of the enum
- # };
- GGUF_TYPE_UINT8 = 0
- GGUF_TYPE_INT8 = 1
- GGUF_TYPE_UINT16 = 2
- GGUF_TYPE_INT16 = 3
- GGUF_TYPE_UINT32 = 4
- GGUF_TYPE_INT32 = 5
- GGUF_TYPE_FLOAT32 = 6
- GGUF_TYPE_BOOL = 7
- GGUF_TYPE_STRING = 8
- GGUF_TYPE_ARRAY = 9
- GGUF_TYPE_COUNT = 10
- # struct gguf_context;
- gguf_context_p = ctypes.c_void_p
- # struct gguf_init_params {
- # bool no_alloc;
- # // if not NULL, create a ggml_context and allocate the tensor data in it
- # struct ggml_context ** ctx;
- # };
- class gguf_init_params(ctypes.Structure):
- _fields_ = [
- ("no_alloc", ctypes.c_bool),
- ("ctx", ctypes.POINTER(ggml_context_p)),
- ]
- # GGML_API struct gguf_context * gguf_init_empty(void);
- def gguf_init_empty() -> gguf_context_p:
- return lib.gguf_init_empty()
- lib.gguf_init_empty.argtypes = []
- lib.gguf_init_empty.restype = gguf_context_p
- # GGML_API struct gguf_context * gguf_init_from_file(const char * fname, struct gguf_init_params params);
- def gguf_init_from_file(
- fname: bytes,
- params: gguf_init_params,
- ) -> gguf_context_p:
- return lib.gguf_init_from_file(fname, params)
- lib.gguf_init_from_file.argtypes = [
- ctypes.c_char_p,
- gguf_init_params,
- ]
- lib.gguf_init_from_file.restype = gguf_context_p
- # //GGML_API struct gguf_context * gguf_init_from_buffer(..);
- # GGML_API void gguf_free(struct gguf_context * ctx);
- def gguf_free(
- ctx: gguf_context_p,
- ):
- return lib.gguf_free(ctx)
- lib.gguf_free.argtypes = [
- gguf_context_p,
- ]
- lib.gguf_free.restype = None
- # GGML_API const char * gguf_type_name(enum gguf_type type);
- def gguf_type_name(
- type: Union[ctypes.c_int, int],
- ) -> bytes:
- return lib.gguf_type_name(type)
- lib.gguf_type_name.argtypes = [
- ctypes.c_int,
- ]
- lib.gguf_type_name.restype = ctypes.c_char_p
- # GGML_API int gguf_get_version (const struct gguf_context * ctx);
- def gguf_get_version(
- ctx: gguf_context_p,
- ) -> int:
- return lib.gguf_get_version(ctx)
- lib.gguf_get_version.argtypes = [
- gguf_context_p,
- ]
- lib.gguf_get_version.restype = ctypes.c_int
- # GGML_API size_t gguf_get_alignment (const struct gguf_context * ctx);
- def gguf_get_alignment(
- ctx: gguf_context_p,
- ) -> int:
- return lib.gguf_get_alignment(ctx)
- lib.gguf_get_alignment.argtypes = [
- gguf_context_p,
- ]
- lib.gguf_get_alignment.restype = ctypes.c_size_t
- # GGML_API size_t gguf_get_data_offset(const struct gguf_context * ctx);
- def gguf_get_data_offset(
- ctx: gguf_context_p,
- ) -> int:
- return lib.gguf_get_data_offset(ctx)
- lib.gguf_get_data_offset.argtypes = [
- gguf_context_p,
- ]
- lib.gguf_get_data_offset.restype = ctypes.c_size_t
- # GGML_API void * gguf_get_data (const struct gguf_context * ctx);
- def gguf_get_data(
- ctx: gguf_context_p,
- ) -> ctypes.c_void_p:
- return lib.gguf_get_data(ctx)
- lib.gguf_get_data.argtypes = [
- gguf_context_p,
- ]
- lib.gguf_get_data.restype = ctypes.c_void_p
- # GGML_API int gguf_get_n_kv(const struct gguf_context * ctx);
- def gguf_get_n_kv(
- ctx: gguf_context_p,
- ) -> int:
- return lib.gguf_get_n_kv(ctx)
- lib.gguf_get_n_kv.argtypes = [
- gguf_context_p,
- ]
- lib.gguf_get_n_kv.restype = ctypes.c_int
- # GGML_API int gguf_find_key(const struct gguf_context * ctx, const char * key);
- def gguf_find_key(
- ctx: gguf_context_p,
- key: bytes,
- ) -> int:
- return lib.gguf_find_key(ctx, key)
- lib.gguf_find_key.argtypes = [
- gguf_context_p,
- ctypes.c_char_p,
- ]
- lib.gguf_find_key.restype = ctypes.c_int
- # GGML_API const char * gguf_get_key (const struct gguf_context * ctx, int i);
- def gguf_get_key(
- ctx: gguf_context_p,
- i: Union[ctypes.c_int, int],
- ) -> bytes:
- return lib.gguf_get_key(ctx, i)
- lib.gguf_get_key.argtypes = [
- gguf_context_p,
- ctypes.c_int,
- ]
- lib.gguf_get_key.restype = ctypes.c_char_p
- # GGML_API enum gguf_type gguf_get_kv_type (const struct gguf_context * ctx, int i);
- def gguf_get_kv_type(
- ctx: gguf_context_p,
- i: Union[ctypes.c_int, int],
- ) -> int:
- return lib.gguf_get_kv_type(ctx, i)
- lib.gguf_get_kv_type.argtypes = [
- gguf_context_p,
- ctypes.c_int,
- ]
- lib.gguf_get_kv_type.restype = ctypes.c_int
- # GGML_API enum gguf_type gguf_get_arr_type(const struct gguf_context * ctx, int i);
- def gguf_get_arr_type(
- ctx: gguf_context_p,
- i: Union[ctypes.c_int, int],
- ) -> int:
- return lib.gguf_get_arr_type(ctx, i)
- lib.gguf_get_arr_type.argtypes = [
- gguf_context_p,
- ctypes.c_int,
- ]
- lib.gguf_get_arr_type.restype = ctypes.c_int
- # // results are undefined if the wrong type is used for the key
- # GGML_API uint8_t gguf_get_val_u8 (const struct gguf_context * ctx, int i);
- def gguf_get_val_u8(
- ctx: gguf_context_p,
- i: Union[ctypes.c_int, int],
- ) -> int:
- return lib.gguf_get_val_u8(ctx, i)
- lib.gguf_get_val_u8.argtypes = [
- gguf_context_p,
- ctypes.c_int,
- ]
- lib.gguf_get_val_u8.restype = ctypes.c_uint8
- # GGML_API int8_t gguf_get_val_i8 (const struct gguf_context * ctx, int i);
- def gguf_get_val_i8(
- ctx: gguf_context_p,
- i: Union[ctypes.c_int, int],
- ) -> int:
- return lib.gguf_get_val_i8(ctx, i)
- lib.gguf_get_val_i8.argtypes = [
- gguf_context_p,
- ctypes.c_int,
- ]
- lib.gguf_get_val_i8.restype = ctypes.c_int8
- # GGML_API uint16_t gguf_get_val_u16 (const struct gguf_context * ctx, int i);
- def gguf_get_val_u16(
- ctx: gguf_context_p,
- i: Union[ctypes.c_int, int],
- ) -> int:
- return lib.gguf_get_val_u16(ctx, i)
- lib.gguf_get_val_u16.argtypes = [
- gguf_context_p,
- ctypes.c_int,
- ]
- lib.gguf_get_val_u16.restype = ctypes.c_uint16
- # GGML_API int16_t gguf_get_val_i16 (const struct gguf_context * ctx, int i);
- def gguf_get_val_i16(
- ctx: gguf_context_p,
- i: Union[ctypes.c_int, int],
- ) -> int:
- return lib.gguf_get_val_i16(ctx, i)
- lib.gguf_get_val_i16.argtypes = [
- gguf_context_p,
- ctypes.c_int,
- ]
- lib.gguf_get_val_i16.restype = ctypes.c_int16
- # GGML_API uint32_t gguf_get_val_u32 (const struct gguf_context * ctx, int i);
- def gguf_get_val_u32(
- ctx: gguf_context_p,
- i: Union[ctypes.c_int, int],
- ) -> int:
- return lib.gguf_get_val_u32(ctx, i)
- lib.gguf_get_val_u32.argtypes = [
- gguf_context_p,
- ctypes.c_int,
- ]
- lib.gguf_get_val_u32.restype = ctypes.c_uint32
- # GGML_API int32_t gguf_get_val_i32 (const struct gguf_context * ctx, int i);
- def gguf_get_val_i32(
- ctx: gguf_context_p,
- i: Union[ctypes.c_int, int],
- ) -> int:
- return lib.gguf_get_val_i32(ctx, i)
- lib.gguf_get_val_i32.argtypes = [
- gguf_context_p,
- ctypes.c_int,
- ]
- lib.gguf_get_val_i32.restype = ctypes.c_int32
- # GGML_API float gguf_get_val_f32 (const struct gguf_context * ctx, int i);
- def gguf_get_val_f32(
- ctx: gguf_context_p,
- i: Union[ctypes.c_int, int],
- ) -> float:
- return lib.gguf_get_val_f32(ctx, i)
- lib.gguf_get_val_f32.argtypes = [
- gguf_context_p,
- ctypes.c_int,
- ]
- lib.gguf_get_val_f32.restype = ctypes.c_float
- # GGML_API uint64_t gguf_get_val_u64 (const struct gguf_context * ctx, int i);
- def gguf_get_val_u64(
- ctx: gguf_context_p,
- i: Union[ctypes.c_int, int],
- ) -> int:
- return lib.gguf_get_val_u64(ctx, i)
- lib.gguf_get_val_u64.argtypes = [
- gguf_context_p,
- ctypes.c_int,
- ]
- lib.gguf_get_val_u64.restype = ctypes.c_uint64
- # GGML_API int64_t gguf_get_val_i64 (const struct gguf_context * ctx, int i);
- def gguf_get_val_i64(
- ctx: gguf_context_p,
- i: Union[ctypes.c_int, int],
- ) -> int:
- return lib.gguf_get_val_i64(ctx, i)
- lib.gguf_get_val_i64.argtypes = [
- gguf_context_p,
- ctypes.c_int,
- ]
- lib.gguf_get_val_i64.restype = ctypes.c_int64
- # GGML_API double gguf_get_val_f64 (const struct gguf_context * ctx, int i);
- def gguf_get_val_f64(
- ctx: gguf_context_p,
- i: Union[ctypes.c_int, int],
- ) -> float:
- return lib.gguf_get_val_f64(ctx, i)
- lib.gguf_get_val_f64.argtypes = [
- gguf_context_p,
- ctypes.c_int,
- ]
- lib.gguf_get_val_f64.restype = ctypes.c_double
- # GGML_API bool gguf_get_val_bool(const struct gguf_context * ctx, int i);
- def gguf_get_val_bool(
- ctx: gguf_context_p,
- i: Union[ctypes.c_int, int],
- ) -> bool:
- return lib.gguf_get_val_bool(ctx, i)
- lib.gguf_get_val_bool.argtypes = [
- gguf_context_p,
- ctypes.c_int,
- ]
- lib.gguf_get_val_bool.restype = ctypes.c_bool
- # GGML_API const char * gguf_get_val_str (const struct gguf_context * ctx, int i);
- def gguf_get_val_str(
- ctx: gguf_context_p,
- i: Union[ctypes.c_int, int],
- ) -> bytes:
- return lib.gguf_get_val_str(ctx, i)
- lib.gguf_get_val_str.argtypes = [
- gguf_context_p,
- ctypes.c_int,
- ]
- lib.gguf_get_val_str.restype = ctypes.c_char_p
- # GGML_API int gguf_get_arr_n (const struct gguf_context * ctx, int i);
- def gguf_get_arr_n(
- ctx: gguf_context_p,
- i: Union[ctypes.c_int, int],
- ) -> int:
- return lib.gguf_get_arr_n(ctx, i)
- lib.gguf_get_arr_n.argtypes = [
- gguf_context_p,
- ctypes.c_int,
- ]
- lib.gguf_get_arr_n.restype = ctypes.c_int
- # GGML_API const void * gguf_get_arr_data(const struct gguf_context * ctx, int i);
- def gguf_get_arr_data(
- ctx: gguf_context_p,
- i: Union[ctypes.c_int, int],
- ) -> ctypes.c_void_p:
- return lib.gguf_get_arr_data(ctx, i)
- lib.gguf_get_arr_data.argtypes = [
- gguf_context_p,
- ctypes.c_int,
- ]
- lib.gguf_get_arr_data.restype = ctypes.c_void_p
- # GGML_API const char * gguf_get_arr_str (const struct gguf_context * ctx, int key_id, int i);
- def gguf_get_arr_str(
- ctx: gguf_context_p,
- key_id: Union[ctypes.c_int, int],
- i: Union[ctypes.c_int, int],
- ) -> bytes:
- return lib.gguf_get_arr_str(ctx, key_id, i)
- lib.gguf_get_arr_str.argtypes = [
- gguf_context_p,
- ctypes.c_int,
- ctypes.c_int,
- ]
- lib.gguf_get_arr_str.restype = ctypes.c_char_p
- # GGML_API int gguf_get_n_tensors (const struct gguf_context * ctx);
- def gguf_get_n_tensors(
- ctx: gguf_context_p,
- ) -> int:
- return lib.gguf_get_n_tensors(ctx)
- lib.gguf_get_n_tensors.argtypes = [
- gguf_context_p,
- ]
- lib.gguf_get_n_tensors.restype = ctypes.c_int
- # GGML_API int gguf_find_tensor (const struct gguf_context * ctx, const char * name);
- def gguf_find_tensor(
- ctx: gguf_context_p,
- name: bytes,
- ) -> int:
- return lib.gguf_find_tensor(ctx, name)
- lib.gguf_find_tensor.argtypes = [
- gguf_context_p,
- ctypes.c_char_p,
- ]
- lib.gguf_find_tensor.restype = ctypes.c_int
- # GGML_API size_t gguf_get_tensor_offset(const struct gguf_context * ctx, int i);
- def gguf_get_tensor_offset(
- ctx: gguf_context_p,
- i: Union[ctypes.c_int, int],
- ) -> int:
- return lib.gguf_get_tensor_offset(ctx, i)
- lib.gguf_get_tensor_offset.argtypes = [
- gguf_context_p,
- ctypes.c_int,
- ]
- lib.gguf_get_tensor_offset.restype = ctypes.c_size_t
- # GGML_API char * gguf_get_tensor_name (const struct gguf_context * ctx, int i);
- def gguf_get_tensor_name(
- ctx: gguf_context_p,
- i: Union[ctypes.c_int, int],
- ) -> bytes:
- return lib.gguf_get_tensor_name(ctx, i)
- lib.gguf_get_tensor_name.argtypes = [
- gguf_context_p,
- ctypes.c_int,
- ]
- lib.gguf_get_tensor_name.restype = ctypes.c_char_p
- # // overrides existing values or adds a new one
- # GGML_API void gguf_set_val_u8 (struct gguf_context * ctx, const char * key, uint8_t val);
- def gguf_set_val_u8(
- ctx: gguf_context_p,
- key: bytes,
- val: Union[ctypes.c_uint8, int],
- ):
- return lib.gguf_set_val_u8(ctx, key, val)
- lib.gguf_set_val_u8.argtypes = [
- gguf_context_p,
- ctypes.c_char_p,
- ctypes.c_uint8,
- ]
- lib.gguf_set_val_u8.restype = None
- # GGML_API void gguf_set_val_i8 (struct gguf_context * ctx, const char * key, int8_t val);
- def gguf_set_val_i8(
- ctx: gguf_context_p,
- key: bytes,
- val: Union[ctypes.c_int8, int],
- ):
- return lib.gguf_set_val_i8(ctx, key, val)
- lib.gguf_set_val_i8.argtypes = [
- gguf_context_p,
- ctypes.c_char_p,
- ctypes.c_int8,
- ]
- lib.gguf_set_val_i8.restype = None
- # GGML_API void gguf_set_val_u16 (struct gguf_context * ctx, const char * key, uint16_t val);
- def gguf_set_val_u16(
- ctx: gguf_context_p,
- key: bytes,
- val: Union[ctypes.c_uint16, int],
- ):
- return lib.gguf_set_val_u16(ctx, key, val)
- lib.gguf_set_val_u16.argtypes = [
- gguf_context_p,
- ctypes.c_char_p,
- ctypes.c_uint16,
- ]
- lib.gguf_set_val_u16.restype = None
- # GGML_API void gguf_set_val_i16 (struct gguf_context * ctx, const char * key, int16_t val);
- def gguf_set_val_i16(
- ctx: gguf_context_p,
- key: bytes,
- val: Union[ctypes.c_int16, int],
- ):
- return lib.gguf_set_val_i16(ctx, key, val)
- lib.gguf_set_val_i16.argtypes = [
- gguf_context_p,
- ctypes.c_char_p,
- ctypes.c_int16,
- ]
- lib.gguf_set_val_i16.restype = None
- # GGML_API void gguf_set_val_u32 (struct gguf_context * ctx, const char * key, uint32_t val);
- def gguf_set_val_u32(
- ctx: gguf_context_p,
- key: bytes,
- val: Union[ctypes.c_uint32, int],
- ):
- return lib.gguf_set_val_u32(ctx, key, val)
- lib.gguf_set_val_u32.argtypes = [
- gguf_context_p,
- ctypes.c_char_p,
- ctypes.c_uint32,
- ]
- lib.gguf_set_val_u32.restype = None
- # GGML_API void gguf_set_val_i32 (struct gguf_context * ctx, const char * key, int32_t val);
- def gguf_set_val_i32(
- ctx: gguf_context_p,
- key: bytes,
- val: Union[ctypes.c_int32, int],
- ):
- return lib.gguf_set_val_i32(ctx, key, val)
- lib.gguf_set_val_i32.argtypes = [
- gguf_context_p,
- ctypes.c_char_p,
- ctypes.c_int32,
- ]
- lib.gguf_set_val_i32.restype = None
- # GGML_API void gguf_set_val_f32 (struct gguf_context * ctx, const char * key, float val);
- def gguf_set_val_f32(
- ctx: gguf_context_p,
- key: bytes,
- val: Union[ctypes.c_float, float],
- ):
- return lib.gguf_set_val_f32(ctx, key, val)
- lib.gguf_set_val_f32.argtypes = [
- gguf_context_p,
- ctypes.c_char_p,
- ctypes.c_float,
- ]
- lib.gguf_set_val_f32.restype = None
- # GGML_API void gguf_set_val_u64 (struct gguf_context * ctx, const char * key, uint64_t val);
- def gguf_set_val_u64(
- ctx: gguf_context_p,
- key: bytes,
- val: Union[ctypes.c_uint64, int],
- ):
- return lib.gguf_set_val_u64(ctx, key, val)
- lib.gguf_set_val_u64.argtypes = [
- gguf_context_p,
- ctypes.c_char_p,
- ctypes.c_uint64,
- ]
- lib.gguf_set_val_u64.restype = None
- # GGML_API void gguf_set_val_i64 (struct gguf_context * ctx, const char * key, int64_t val);
- def gguf_set_val_i64(
- ctx: gguf_context_p,
- key: bytes,
- val: Union[ctypes.c_int64, int],
- ):
- return lib.gguf_set_val_i64(ctx, key, val)
- lib.gguf_set_val_i64.argtypes = [
- gguf_context_p,
- ctypes.c_char_p,
- ctypes.c_int64,
- ]
- lib.gguf_set_val_i64.restype = None
- # GGML_API void gguf_set_val_f64 (struct gguf_context * ctx, const char * key, double val);
- def gguf_set_val_f64(
- ctx: gguf_context_p,
- key: bytes,
- val: Union[ctypes.c_double, float],
- ):
- return lib.gguf_set_val_f64(ctx, key, val)
- lib.gguf_set_val_f64.argtypes = [
- gguf_context_p,
- ctypes.c_char_p,
- ctypes.c_double,
- ]
- lib.gguf_set_val_f64.restype = None
- # GGML_API void gguf_set_val_bool(struct gguf_context * ctx, const char * key, bool val);
- def gguf_set_val_bool(
- ctx: gguf_context_p,
- key: bytes,
- val: Union[ctypes.c_bool, bool],
- ):
- return lib.gguf_set_val_bool(ctx, key, val)
- lib.gguf_set_val_bool.argtypes = [
- gguf_context_p,
- ctypes.c_char_p,
- ctypes.c_bool,
- ]
- lib.gguf_set_val_bool.restype = None
- # GGML_API void gguf_set_val_str (struct gguf_context * ctx, const char * key, const char * val);
- def gguf_set_val_str(
- ctx: gguf_context_p,
- key: bytes,
- val: bytes,
- ):
- return lib.gguf_set_val_str(ctx, key, val)
- lib.gguf_set_val_str.argtypes = [
- gguf_context_p,
- ctypes.c_char_p,
- ctypes.c_char_p,
- ]
- lib.gguf_set_val_str.restype = None
- # GGML_API void gguf_set_arr_data(struct gguf_context * ctx, const char * key, enum gguf_type type, const void * data, int n);
- def gguf_set_arr_data(
- ctx: gguf_context_p,
- key: bytes,
- type: Union[ctypes.c_int, int],
- data: ctypes.c_void_p,
- n: Union[ctypes.c_int, int],
- ):
- return lib.gguf_set_arr_data(ctx, key, type, data, n)
- lib.gguf_set_arr_data.argtypes = [
- gguf_context_p,
- ctypes.c_char_p,
- ctypes.c_int,
- ctypes.c_void_p,
- ctypes.c_int,
- ]
- lib.gguf_set_arr_data.restype = None
- # GGML_API void gguf_set_arr_str (struct gguf_context * ctx, const char * key, const char ** data, int n);
- def gguf_set_arr_str(
- ctx: gguf_context_p,
- key: bytes,
- data: CCharPointer,
- n: Union[ctypes.c_int, int],
- ):
- return lib.gguf_set_arr_str(ctx, key, data, n)
- lib.gguf_set_arr_str.argtypes = [
- gguf_context_p,
- ctypes.c_char_p,
- ctypes.POINTER(ctypes.c_char_p),
- ctypes.c_int,
- ]
- lib.gguf_set_arr_str.restype = None
- # // set or add KV pairs from another context
- # GGML_API void gguf_set_kv(struct gguf_context * ctx, struct gguf_context * src);
- def gguf_set_kv(
- ctx: gguf_context_p,
- src: gguf_context_p,
- ):
- return lib.gguf_set_kv(ctx, src)
- lib.gguf_set_kv.argtypes = [
- gguf_context_p,
- gguf_context_p,
- ]
- lib.gguf_set_kv.restype = None
- # // manage tensor info
- # GGML_API void gguf_add_tensor(struct gguf_context * ctx, const struct ggml_tensor * tensor);
- def gguf_add_tensor(
- ctx: gguf_context_p,
- tensor: ggml_tensor_p,
- ):
- return lib.gguf_add_tensor(ctx, tensor)
- lib.gguf_add_tensor.argtypes = [
- gguf_context_p,
- ctypes.POINTER(ggml_tensor),
- ]
- lib.gguf_add_tensor.restype = None
- # GGML_API void gguf_set_tensor_type(struct gguf_context * ctx, const char * name, enum ggml_type type);
- def gguf_set_tensor_type(
- ctx: gguf_context_p,
- name: bytes,
- type: Union[ctypes.c_int, int],
- ):
- return lib.gguf_set_tensor_type(ctx, name, type)
- lib.gguf_set_tensor_type.argtypes = [
- gguf_context_p,
- ctypes.c_char_p,
- ctypes.c_int,
- ]
- lib.gguf_set_tensor_type.restype = None
- # GGML_API void gguf_set_tensor_data(struct gguf_context * ctx, const char * name, const void * data, size_t size);
- def gguf_set_tensor_data(
- ctx: gguf_context_p,
- name: bytes,
- data: ctypes.c_void_p,
- size: Union[ctypes.c_size_t, int],
- ):
- return lib.gguf_set_tensor_data(ctx, name, data, size)
- lib.gguf_set_tensor_data.argtypes = [
- gguf_context_p,
- ctypes.c_char_p,
- ctypes.c_void_p,
- ctypes.c_size_t,
- ]
- lib.gguf_set_tensor_data.restype = None
- # // writing gguf files can be done in 2 ways:
- # //
- # // - write the entire gguf_context to a binary file in a single pass:
- # //
- # // gguf_write_to_file(ctx, fname);
- # //
- # // - first prepare a file with a placeholder for the meta data, write the tensor data, then write the meta data:
- # //
- # // FILE * f = fopen(fname, "wb");
- # // fseek(f, gguf_get_meta_size(ctx), SEEK_SET);
- # // fwrite(f, ...);
- # // void * data = gguf_meta_get_meta_data(ctx);
- # // fseek(f, 0, SEEK_SET);
- # // fwrite(f, data, gguf_get_meta_size(ctx));
- # // free(data);
- # // fclose(f);
- # //
- # // write the entire context to a binary file
- # GGML_API void gguf_write_to_file(const struct gguf_context * ctx, const char * fname, bool only_meta);
- def gguf_write_to_file(
- ctx: gguf_context_p,
- fname: bytes,
- only_meta: Union[ctypes.c_bool, bool],
- ):
- return lib.gguf_write_to_file(ctx, fname, only_meta)
- lib.gguf_write_to_file.argtypes = [
- gguf_context_p,
- ctypes.c_char_p,
- ctypes.c_bool,
- ]
- lib.gguf_write_to_file.restype = None
- # // get the size in bytes of the meta data (header, kv pairs, tensor info) including padding
- # GGML_API size_t gguf_get_meta_size(const struct gguf_context * ctx);
- def gguf_get_meta_size(
- ctx: gguf_context_p,
- ) -> int:
- return lib.gguf_get_meta_size(ctx)
- lib.gguf_get_meta_size.argtypes = [
- gguf_context_p,
- ]
- lib.gguf_get_meta_size.restype = ctypes.c_size_t
- # GGML_API void gguf_get_meta_data(const struct gguf_context * ctx, void * data);
- def gguf_get_meta_data(
- ctx: gguf_context_p,
- data: ctypes.c_void_p,
- ):
- return lib.gguf_get_meta_data(ctx, data)
- lib.gguf_get_meta_data.argtypes = [
- gguf_context_p,
- ctypes.c_void_p,
- ]
- lib.gguf_get_meta_data.restype = None
- # //
- # // system info
- # //
- # GGML_API int ggml_cpu_has_avx (void);
- def ggml_cpu_has_avx() -> int:
- return lib.ggml_cpu_has_avx()
- lib.ggml_cpu_has_avx.argtypes = []
- lib.ggml_cpu_has_avx.restype = ctypes.c_int
- # GGML_API int ggml_cpu_has_avx2 (void);
- def ggml_cpu_has_avx2() -> int:
- return lib.ggml_cpu_has_avx2()
- lib.ggml_cpu_has_avx2.argtypes = []
- lib.ggml_cpu_has_avx2.restype = ctypes.c_int
- # GGML_API int ggml_cpu_has_avx512 (void);
- def ggml_cpu_has_avx512() -> int:
- return lib.ggml_cpu_has_avx512()
- lib.ggml_cpu_has_avx512.argtypes = []
- lib.ggml_cpu_has_avx512.restype = ctypes.c_int
- # GGML_API int ggml_cpu_has_avx512_vbmi(void);
- def ggml_cpu_has_avx512_vbmi() -> int:
- return lib.ggml_cpu_has_avx512_vbmi()
- lib.ggml_cpu_has_avx512_vbmi.argtypes = []
- lib.ggml_cpu_has_avx512_vbmi.restype = ctypes.c_int
- # GGML_API int ggml_cpu_has_avx512_vnni(void);
- def ggml_cpu_has_avx512_vnni() -> int:
- return lib.ggml_cpu_has_avx512_vnni()
- lib.ggml_cpu_has_avx512_vnni.argtypes = []
- lib.ggml_cpu_has_avx512_vnni.restype = ctypes.c_int
- # GGML_API int ggml_cpu_has_fma (void);
- def ggml_cpu_has_fma() -> int:
- return lib.ggml_cpu_has_fma()
- lib.ggml_cpu_has_fma.argtypes = []
- lib.ggml_cpu_has_fma.restype = ctypes.c_int
- # GGML_API int ggml_cpu_has_neon (void);
- def ggml_cpu_has_neon() -> int:
- return lib.ggml_cpu_has_neon()
- lib.ggml_cpu_has_neon.argtypes = []
- lib.ggml_cpu_has_neon.restype = ctypes.c_int
- # GGML_API int ggml_cpu_has_arm_fma (void);
- def ggml_cpu_has_arm_fma() -> int:
- return lib.ggml_cpu_has_arm_fma()
- lib.ggml_cpu_has_arm_fma.argtypes = []
- lib.ggml_cpu_has_arm_fma.restype = ctypes.c_int
- # GGML_API int ggml_cpu_has_f16c (void);
- def ggml_cpu_has_f16c() -> int:
- return lib.ggml_cpu_has_f16c()
- lib.ggml_cpu_has_f16c.argtypes = []
- lib.ggml_cpu_has_f16c.restype = ctypes.c_int
- # GGML_API int ggml_cpu_has_fp16_va (void);
- def ggml_cpu_has_fp16_va() -> int:
- return lib.ggml_cpu_has_fp16_va()
- lib.ggml_cpu_has_fp16_va.argtypes = []
- lib.ggml_cpu_has_fp16_va.restype = ctypes.c_int
- # GGML_API int ggml_cpu_has_wasm_simd (void);
- def ggml_cpu_has_wasm_simd() -> int:
- return lib.ggml_cpu_has_wasm_simd()
- lib.ggml_cpu_has_wasm_simd.argtypes = []
- lib.ggml_cpu_has_wasm_simd.restype = ctypes.c_int
- # GGML_API int ggml_cpu_has_blas (void);
- def ggml_cpu_has_blas() -> int:
- return lib.ggml_cpu_has_blas()
- lib.ggml_cpu_has_blas.argtypes = []
- lib.ggml_cpu_has_blas.restype = ctypes.c_int
- # GGML_API int ggml_cpu_has_cublas (void);
- def ggml_cpu_has_cublas() -> int:
- return lib.ggml_cpu_has_cublas()
- lib.ggml_cpu_has_cublas.argtypes = []
- lib.ggml_cpu_has_cublas.restype = ctypes.c_int
- # GGML_API int ggml_cpu_has_clblast (void);
- def ggml_cpu_has_clblast() -> int:
- return lib.ggml_cpu_has_clblast()
- lib.ggml_cpu_has_clblast.argtypes = []
- lib.ggml_cpu_has_clblast.restype = ctypes.c_int
- # GGML_API int ggml_cpu_has_gpublas (void);
- def ggml_cpu_has_gpublas() -> int:
- return lib.ggml_cpu_has_gpublas()
- lib.ggml_cpu_has_gpublas.argtypes = []
- lib.ggml_cpu_has_gpublas.restype = ctypes.c_int
- # GGML_API int ggml_cpu_has_sse3 (void);
- def ggml_cpu_has_sse3() -> int:
- return lib.ggml_cpu_has_sse3()
- lib.ggml_cpu_has_sse3.argtypes = []
- lib.ggml_cpu_has_sse3.restype = ctypes.c_int
- # GGML_API int ggml_cpu_has_ssse3 (void);
- def ggml_cpu_has_ssse3() -> int:
- return lib.ggml_cpu_has_ssse3()
- lib.ggml_cpu_has_ssse3.argtypes = []
- lib.ggml_cpu_has_ssse3.restype = ctypes.c_int
- # GGML_API int ggml_cpu_has_vsx (void);
- def ggml_cpu_has_vsx() -> int:
- return lib.ggml_cpu_has_vsx()
- lib.ggml_cpu_has_vsx.argtypes = []
- lib.ggml_cpu_has_vsx.restype = ctypes.c_int
- # //
- # // Internal types and functions exposed for tests and benchmarks
- # //
- # typedef void (*ggml_to_float_t)(const void * x, float * y, int k);
- ggml_to_float_t = ctypes.CFUNCTYPE(
- None, ctypes.c_void_p, ctypes.POINTER(ctypes.c_float), ctypes.c_int
- )
- # typedef void (*ggml_from_float_t)(const float * x, void * y, int k);
- ggml_from_float_t = ctypes.CFUNCTYPE(
- None, ctypes.POINTER(ctypes.c_float), ctypes.c_void_p, ctypes.c_int
- )
- # typedef void (*ggml_vec_dot_t)(const int n, float * s, const void * x, const void * y);
- ggml_vec_dot_t = ctypes.CFUNCTYPE(
- None, ctypes.c_int, ctypes.POINTER(ctypes.c_float), ctypes.c_void_p, ctypes.c_void_p
- )
- # typedef struct {
- # const char * type_name;
- # int blck_size;
- # size_t type_size;
- # bool is_quantized;
- # ggml_to_float_t to_float;
- # ggml_from_float_t from_float;
- # ggml_from_float_t from_float_reference;
- # ggml_vec_dot_t vec_dot;
- # enum ggml_type vec_dot_type;
- # } ggml_type_traits_t;
- class ggml_type_traits_t(ctypes.Structure):
- _fields_ = [
- ("type_name", ctypes.c_char_p),
- ("blck_size", ctypes.c_int),
- ("type_size", ctypes.c_size_t),
- ("is_quantized", ctypes.c_bool),
- ("to_float", ggml_to_float_t),
- ("from_float", ggml_from_float_t),
- ("from_float_reference", ggml_from_float_t),
- ("vec_dot", ggml_vec_dot_t),
- ("vec_dot_type", ctypes.c_int),
- ]
- # ggml_type_traits_t ggml_internal_get_type_traits(enum ggml_type type);
- def ggml_internal_get_type_traits(type: Union[ctypes.c_int, int]) -> ggml_type_traits_t:
- return lib.ggml_internal_get_type_traits(type)
- lib.ggml_internal_get_type_traits.argtypes = [ctypes.c_int]
- lib.ggml_internal_get_type_traits.restype = ggml_type_traits_t
- #####################################################
- # GGML ALLOC API
- # source: ggml-alloc.h
- #####################################################
- ggml_allocr_p = ctypes.c_void_p
- # GGML_API struct ggml_allocr * ggml_allocr_new(void * data, size_t size, size_t alignment);
- def ggml_allocr_new(
- data: ctypes.c_void_p,
- size: Union[ctypes.c_size_t, int],
- alignment: Union[ctypes.c_size_t, int],
- ) -> ggml_allocr_p:
- return lib.ggml_allocr_new(data, size, alignment)
- lib.ggml_allocr_new.argtypes = [ctypes.c_void_p, ctypes.c_size_t, ctypes.c_size_t]
- lib.ggml_allocr_new.restype = ggml_allocr_p
- # GGML_API struct ggml_allocr * ggml_allocr_new_measure(size_t alignment);
- def ggml_allocr_new_measure(
- alignment: Union[ctypes.c_size_t, int],
- ) -> ggml_allocr_p:
- return lib.ggml_allocr_new_measure(alignment)
- lib.ggml_allocr_new_measure.argtypes = [ctypes.c_size_t]
- lib.ggml_allocr_new_measure.restype = ggml_allocr_p
- # // tell the allocator to parse nodes following the order described in the list
- # // you should call this if your graph are optimized to execute out-of-order
- # GGML_API void ggml_allocr_set_parse_seq(struct ggml_allocr * alloc, const int * list, int n);
- def ggml_allocr_set_parse_seq(
- alloc: ggml_allocr_p,
- list: CIntPointer,
- n: Union[ctypes.c_int, int],
- ):
- return lib.ggml_allocr_set_parse_seq(alloc, list, n)
- lib.ggml_allocr_set_parse_seq.argtypes = [
- ggml_allocr_p,
- ctypes.POINTER(ctypes.c_int),
- ctypes.c_int,
- ]
- lib.ggml_allocr_set_parse_seq.restype = None
- # GGML_API void ggml_allocr_free(struct ggml_allocr * alloc);
- def ggml_allocr_free(
- alloc: ggml_allocr_p,
- ):
- return lib.ggml_allocr_free(alloc)
- lib.ggml_allocr_free.argtypes = [ggml_allocr_p]
- lib.ggml_allocr_free.restype = None
- # GGML_API bool ggml_allocr_is_measure(struct ggml_allocr * alloc);
- def ggml_allocr_is_measure(
- alloc: ggml_allocr_p,
- ) -> bool:
- return lib.ggml_allocr_is_measure(alloc)
- lib.ggml_allocr_is_measure.argtypes = [ggml_allocr_p]
- lib.ggml_allocr_is_measure.restype = ctypes.c_bool
- # GGML_API void ggml_allocr_reset(struct ggml_allocr * alloc);
- def ggml_allocr_reset(
- alloc: ggml_allocr_p,
- ):
- return lib.ggml_allocr_reset(alloc)
- lib.ggml_allocr_reset.argtypes = [ggml_allocr_p]
- lib.ggml_allocr_reset.restype = None
- # GGML_API void ggml_allocr_alloc(struct ggml_allocr * alloc, struct ggml_tensor * tensor);
- def ggml_allocr_alloc(
- alloc: ggml_allocr_p,
- tensor: ggml_tensor_p,
- ):
- return lib.ggml_allocr_alloc(alloc, tensor)
- lib.ggml_allocr_alloc.argtypes = [ggml_allocr_p, ctypes.POINTER(ggml_tensor)]
- lib.ggml_allocr_alloc.restype = None
- # GGML_API size_t ggml_allocr_alloc_graph(struct ggml_allocr * alloc, struct ggml_cgraph * graph);
- def ggml_allocr_alloc_graph(
- alloc: ggml_allocr_p,
- graph: ggml_cgraph_p,
- ) -> int:
- return lib.ggml_allocr_alloc_graph(alloc, graph)
- lib.ggml_allocr_alloc_graph.argtypes = [ggml_allocr_p, ctypes.POINTER(ggml_cgraph)]
- lib.ggml_allocr_alloc_graph.restype = ctypes.c_size_t
- #####################################################
- # GGML CUDA API
- # source: ggml-cuda.h
- #####################################################
- GGML_USE_CUBLAS = hasattr(lib, "ggml_init_cublas")
- GGML_CUDA_MAX_DEVICES = 16
- # GGML_API void ggml_init_cublas(void);
- def ggml_init_cublas():
- return lib.ggml_init_cublas()
- if GGML_USE_CUBLAS:
- lib.ggml_init_cublas.argtypes = []
- lib.ggml_init_cublas.restype = None
- # void * ggml_cuda_host_malloc(size_t size);
- def ggml_cuda_host_malloc(
- size: Union[ctypes.c_size_t, int],
- ) -> Optional[ctypes.c_void_p]:
- return lib.ggml_cuda_host_malloc(size)
- if GGML_USE_CUBLAS:
- lib.ggml_cuda_host_malloc.argtypes = [ctypes.c_size_t]
- lib.ggml_cuda_host_malloc.restype = ctypes.c_void_p
- # void ggml_cuda_host_free(void * ptr);
- def ggml_cuda_host_free(
- ptr: ctypes.c_void_p,
- ):
- return lib.ggml_cuda_host_free(ptr)
- if GGML_USE_CUBLAS:
- lib.ggml_cuda_host_free.argtypes = [ctypes.c_void_p]
- lib.ggml_cuda_host_free.restype = None
- # GGML_API bool ggml_cuda_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst);
- def ggml_cuda_can_mul_mat(
- src0: ggml_tensor_p,
- src1: ggml_tensor_p,
- dst: ggml_tensor_p,
- ) -> bool:
- return lib.ggml_cuda_can_mul_mat(src0, src1, dst)
- if GGML_USE_CUBLAS:
- lib.ggml_cuda_can_mul_mat.argtypes = [
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_cuda_can_mul_mat.restype = ctypes.c_bool
- # GGML_API void ggml_cuda_set_tensor_split(const float * tensor_split);
- def ggml_cuda_set_tensor_split(
- tensor_split: CFloatArray,
- ):
- return lib.ggml_cuda_set_tensor_split(tensor_split)
- if GGML_USE_CUBLAS:
- lib.ggml_cuda_set_tensor_split.argtypes = [ctypes.POINTER(ctypes.c_float)]
- lib.ggml_cuda_set_tensor_split.restype = None
- # void ggml_cuda_transform_tensor(void * data, struct ggml_tensor * tensor);
- def ggml_cuda_transform_tensor(
- data: ctypes.c_void_p,
- tensor: ggml_tensor_p,
- ):
- return lib.ggml_cuda_transform_tensor(data, tensor)
- if GGML_USE_CUBLAS:
- lib.ggml_cuda_transform_tensor.argtypes = [
- ctypes.c_void_p,
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_cuda_transform_tensor.restype = None
- # void ggml_cuda_free_data(struct ggml_tensor * tensor);
- def ggml_cuda_free_data(
- tensor: ggml_tensor_p,
- ):
- return lib.ggml_cuda_free_data(tensor)
- if GGML_USE_CUBLAS:
- lib.ggml_cuda_free_data.argtypes = [
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_cuda_free_data.restype = None
- # void ggml_cuda_assign_buffers(struct ggml_tensor * tensor);
- def ggml_cuda_assign_buffers(
- tensor: ggml_tensor_p,
- ):
- return lib.ggml_cuda_assign_buffers(tensor)
- if GGML_USE_CUBLAS:
- lib.ggml_cuda_assign_buffers.argtypes = [
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_cuda_assign_buffers.restype = None
- # void ggml_cuda_assign_buffers_no_scratch(struct ggml_tensor * tensor);
- def ggml_cuda_assign_buffers_no_scratch(
- tensor: ggml_tensor_p,
- ):
- return lib.ggml_cuda_assign_buffers_no_scratch(tensor)
- if GGML_USE_CUBLAS:
- lib.ggml_cuda_assign_buffers_no_scratch.argtypes = [
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_cuda_assign_buffers_no_scratch.restype = None
- # GGML_API void ggml_cuda_assign_buffers_force_inplace(struct ggml_tensor * tensor);
- def ggml_cuda_assign_buffers_force_inplace(
- tensor: ggml_tensor_p,
- ):
- return lib.ggml_cuda_assign_buffers_force_inplace(tensor)
- if GGML_USE_CUBLAS:
- lib.ggml_cuda_assign_buffers_force_inplace.argtypes = [
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_cuda_assign_buffers_force_inplace.restype = None
- # GGML_API void ggml_cuda_assign_buffers_no_alloc(struct ggml_tensor * tensor);
- def ggml_cuda_assign_buffers_no_alloc(
- tensor: ggml_tensor_p,
- ):
- return lib.ggml_cuda_assign_buffers_no_alloc(tensor)
- if GGML_USE_CUBLAS:
- lib.ggml_cuda_assign_buffers_no_alloc.argtypes = [
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_cuda_assign_buffers_no_alloc.restype = None
- # GGML_API void ggml_cuda_assign_scratch_offset(struct ggml_tensor * tensor, size_t offset);
- def ggml_cuda_assign_scratch_offset(
- tensor: ggml_tensor_p,
- offset: Union[ctypes.c_size_t, int],
- ):
- return lib.ggml_cuda_assign_scratch_offset(tensor, offset)
- if GGML_USE_CUBLAS:
- lib.ggml_cuda_assign_scratch_offset.argtypes = [
- ctypes.POINTER(ggml_tensor),
- ctypes.c_size_t,
- ]
- lib.ggml_cuda_assign_scratch_offset.restype = None
- # void ggml_cuda_set_main_device(int main_device);
- def ggml_cuda_set_main_device(
- main_device: Union[ctypes.c_int, int],
- ):
- return lib.ggml_cuda_set_main_device(main_device)
- if GGML_USE_CUBLAS:
- lib.ggml_cuda_set_main_device.argtypes = [
- ctypes.c_int,
- ]
- lib.ggml_cuda_set_main_device.restype = None
- # GGML_API void ggml_cuda_set_mul_mat_q(bool mul_mat_q);
- def ggml_cuda_set_mul_mat_q(
- mul_mat_q: Union[ctypes.c_bool, bool],
- ):
- return lib.ggml_cuda_set_mul_mat_q(mul_mat_q)
- if GGML_USE_CUBLAS:
- lib.ggml_cuda_set_mul_mat_q.argtypes = [
- ctypes.c_bool,
- ]
- lib.ggml_cuda_set_mul_mat_q.restype = None
- # void ggml_cuda_set_scratch_size(size_t scratch_size);
- def ggml_cuda_set_scratch_size(
- scratch_size: Union[ctypes.c_size_t, int],
- ):
- return lib.ggml_cuda_set_scratch_size(scratch_size)
- if GGML_USE_CUBLAS:
- lib.ggml_cuda_set_scratch_size.argtypes = [
- ctypes.c_size_t,
- ]
- lib.ggml_cuda_set_scratch_size.restype = None
- # void ggml_cuda_free_scratch(void);
- def ggml_cuda_free_scratch():
- return lib.ggml_cuda_free_scratch()
- if GGML_USE_CUBLAS:
- lib.ggml_cuda_free_scratch.argtypes = []
- lib.ggml_cuda_free_scratch.restype = None
- # GGML_API bool ggml_cuda_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor);
- def ggml_cuda_compute_forward(
- params: ggml_compute_params_p,
- tensor: ggml_tensor_p,
- ) -> bool:
- return lib.ggml_cuda_compute_forward(params, tensor)
- if GGML_USE_CUBLAS:
- lib.ggml_cuda_compute_forward.argtypes = [
- ctypes.POINTER(ggml_compute_params),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_cuda_compute_forward.restype = ctypes.c_bool
- # GGML_API int ggml_cuda_get_device_count(void);
- def ggml_cuda_get_device_count() -> int:
- return lib.ggml_cuda_get_device_count()
- if GGML_USE_CUBLAS:
- lib.ggml_cuda_get_device_count.argtypes = []
- lib.ggml_cuda_get_device_count.restype = ctypes.c_int
- # GGML_API void ggml_cuda_get_device_description(int device, char * description, size_t description_size);
- def ggml_cuda_get_device_description(
- device: Union[ctypes.c_int, int],
- description: bytes,
- description_size: Union[ctypes.c_size_t, int],
- ):
- return lib.ggml_cuda_get_device_description(device, description, description_size)
- if GGML_USE_CUBLAS:
- lib.ggml_cuda_get_device_description.argtypes = [
- ctypes.c_int,
- ctypes.c_char_p,
- ctypes.c_size_t,
- ]
- lib.ggml_cuda_get_device_description.restype = None
- #####################################################
- # GGML METAL API
- # source: ggml-metal.h
- #####################################################
- GGML_USE_METAL = hasattr(lib, "ggml_metal_init")
- # // max memory buffers that can be mapped to the device
- # #define GGML_METAL_MAX_BUFFERS 16
- GGML_METAL_MAX_BUFFERS = 16
- # #define GGML_METAL_MAX_COMMAND_BUFFERS 32
- GGML_METAL_MAX_COMMAND_BUFFERS = 32
- # struct ggml_metal_context;
- ggml_metal_context_p = ctypes.c_void_p
- # struct ggml_metal_context * ggml_metal_init(int n_cb);
- def ggml_metal_init(
- n_cb: Union[ctypes.c_int, int],
- ) -> ggml_metal_context_p:
- return lib.ggml_metal_init(n_cb)
- if GGML_USE_METAL:
- lib.ggml_metal_init.argtypes = [ctypes.c_int]
- lib.ggml_metal_init.restype = ggml_metal_context_p
- # void ggml_metal_free(struct ggml_metal_context * ctx);
- def ggml_metal_free(
- ctx: ggml_metal_context_p,
- ):
- return lib.ggml_metal_free(ctx)
- if GGML_USE_METAL:
- lib.ggml_metal_free.argtypes = [ggml_metal_context_p]
- lib.ggml_metal_free.restype = None
- # // set the number of command buffers to use
- # void ggml_metal_set_n_cb(struct ggml_metal_context * ctx, int n_cb);
- def ggml_metal_set_n_cb(
- ctx: ggml_metal_context_p,
- n_cb: Union[ctypes.c_int, int],
- ):
- return lib.ggml_metal_set_n_cb(ctx, n_cb)
- if GGML_USE_METAL:
- lib.ggml_metal_set_n_cb.argtypes = [ggml_metal_context_p, ctypes.c_int]
- lib.ggml_metal_set_n_cb.restype = None
- # // creates a mapping between a host memory buffer and a device memory buffer
- # // - make sure to map all buffers used in the graph before calling ggml_metal_graph_compute
- # // - the mapping is used during computation to determine the arguments of the compute kernels
- # // - you don't need to keep the host memory buffer allocated as it is never accessed by Metal
- # // - max_size specifies the maximum size of a tensor and is used to create shared views such
- # // that it is guaranteed that the tensor will fit in at least one of the views
- # //
- # bool ggml_metal_add_buffer(
- # struct ggml_metal_context * ctx,
- # const char * name,
- # void * data,
- # size_t size,
- # size_t max_size);
- def ggml_metal_add_buffer(
- ctx: ggml_metal_context_p,
- name: bytes,
- data: ctypes.c_void_p,
- size: Union[ctypes.c_size_t, int],
- max_size: Union[ctypes.c_size_t, int],
- ) -> bool:
- return lib.ggml_metal_add_buffer(ctx, name, data, size, max_size)
- if GGML_USE_METAL:
- lib.ggml_metal_add_buffer.argtypes = [
- ggml_metal_context_p,
- ctypes.c_char_p,
- ctypes.c_void_p,
- ctypes.c_size_t,
- ctypes.c_size_t,
- ]
- lib.ggml_metal_add_buffer.restype = ctypes.c_bool
- # // set data from host memory into the device
- # void ggml_metal_set_tensor(struct ggml_metal_context * ctx, struct ggml_tensor * t);
- def ggml_metal_set_tensor(
- ctx: ggml_metal_context_p,
- t: ggml_tensor_p,
- ):
- return lib.ggml_metal_set_tensor(ctx, t)
- if GGML_USE_METAL:
- lib.ggml_metal_set_tensor.argtypes = [
- ggml_metal_context_p,
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_metal_set_tensor.restype = None
- # // get data from the device into host memory
- # void ggml_metal_get_tensor(struct ggml_metal_context * ctx, struct ggml_tensor * t);
- def ggml_metal_get_tensor(
- ctx: ggml_metal_context_p,
- t: ggml_tensor_p,
- ):
- return lib.ggml_metal_get_tensor(ctx, t)
- if GGML_USE_METAL:
- lib.ggml_metal_get_tensor.argtypes = [
- ggml_metal_context_p,
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_metal_get_tensor.restype = None
- # // try to find operations that can be run concurrently in the graph
- # // you should run it again if the topology of your graph changes
- # void ggml_metal_graph_find_concurrency(struct ggml_metal_context * ctx, struct ggml_cgraph * gf, bool check_mem);
- def ggml_metal_graph_find_concurrency(
- ctx: ggml_metal_context_p,
- gf: ggml_cgraph_p,
- check_mem: Union[ctypes.c_bool, bool],
- ):
- return lib.ggml_metal_graph_find_concurrency(ctx, gf, check_mem)
- if GGML_USE_METAL:
- lib.ggml_metal_graph_find_concurrency.argtypes = [
- ggml_metal_context_p,
- ctypes.POINTER(ggml_cgraph),
- ctypes.c_bool,
- ]
- lib.ggml_metal_graph_find_concurrency.restype = None
- # // if the graph has been optimized for concurrently dispatch, return length of the concur_list if optimized
- # int ggml_metal_if_optimized(struct ggml_metal_context * ctx);
- def ggml_metal_if_optimized(
- ctx: ggml_metal_context_p,
- ) -> int:
- return lib.ggml_metal_if_optimized(ctx)
- if GGML_USE_METAL:
- lib.ggml_metal_if_optimized.argtypes = [
- ggml_metal_context_p,
- ]
- lib.ggml_metal_if_optimized.restype = ctypes.c_int
- # // output the concur_list for ggml_alloc
- # int * ggml_metal_get_concur_list(struct ggml_metal_context * ctx);
- def ggml_metal_get_concur_list(
- ctx: ggml_metal_context_p,
- ) -> CIntPointer:
- return lib.ggml_metal_get_concur_list(ctx)
- if GGML_USE_METAL:
- lib.ggml_metal_get_concur_list.argtypes = [
- ggml_metal_context_p,
- ]
- lib.ggml_metal_get_concur_list.restype = ctypes.POINTER(ctypes.c_int)
- # // same as ggml_graph_compute but uses Metal
- # // creates gf->n_threads command buffers in parallel
- # void ggml_metal_graph_compute(struct ggml_metal_context * ctx, struct ggml_cgraph * gf);
- def ggml_metal_graph_compute(
- ctx: ggml_metal_context_p,
- gf: ggml_cgraph_p,
- ):
- return lib.ggml_metal_graph_compute(ctx, gf)
- if GGML_USE_METAL:
- lib.ggml_metal_graph_compute.argtypes = [
- ggml_metal_context_p,
- ctypes.POINTER(ggml_cgraph),
- ]
- lib.ggml_metal_graph_compute.restype = None
- #####################################################
- # GGML OPENCL API
- # source: ggml-opencl.h
- #####################################################
- GGML_USE_CLBLAST = hasattr(lib, "ggml_cl_init")
- # void ggml_cl_init(void);
- def ggml_cl_init():
- return lib.ggml_cl_init()
- if GGML_USE_CLBLAST:
- lib.ggml_cl_init.argtypes = []
- lib.ggml_cl_init.restype = None
- # void ggml_cl_mul(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst);
- def ggml_cl_mul(
- src0: ggml_tensor_p,
- src1: ggml_tensor_p,
- dst: ggml_tensor_p,
- ):
- return lib.ggml_cl_mul(src0, src1, dst)
- if GGML_USE_CLBLAST:
- lib.ggml_cl_mul.argtypes = [
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_cl_mul.restype = None
- # bool ggml_cl_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst);
- def ggml_cl_can_mul_mat(
- src0: ggml_tensor_p,
- src1: ggml_tensor_p,
- dst: ggml_tensor_p,
- ) -> bool:
- return lib.ggml_cl_can_mul_mat(src0, src1, dst)
- if GGML_USE_CLBLAST:
- lib.ggml_cl_can_mul_mat.argtypes = [
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_cl_can_mul_mat.restype = ctypes.c_bool
- # size_t ggml_cl_mul_mat_get_wsize(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst);
- def ggml_cl_mul_mat_get_wsize(
- src0: ggml_tensor_p,
- src1: ggml_tensor_p,
- dst: ggml_tensor_p,
- ) -> int:
- return lib.ggml_cl_mul_mat_get_wsize(src0, src1, dst)
- if GGML_USE_CLBLAST:
- lib.ggml_cl_mul_mat_get_wsize.argtypes = [
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_cl_mul_mat_get_wsize.restype = ctypes.c_size_t
- # void ggml_cl_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst, void * wdata, size_t wsize);
- def ggml_cl_mul_mat(
- src0: ggml_tensor_p,
- src1: ggml_tensor_p,
- dst: ggml_tensor_p,
- wdata: ctypes.c_void_p,
- wsize: Union[ctypes.c_size_t, int],
- ):
- return lib.ggml_cl_mul_mat(src0, src1, dst, wdata, wsize)
- if GGML_USE_CLBLAST:
- lib.ggml_cl_mul_mat.argtypes = [
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.POINTER(ggml_tensor),
- ctypes.c_void_p,
- ctypes.c_size_t,
- ]
- lib.ggml_cl_mul_mat.restype = None
- # void * ggml_cl_host_malloc(size_t size);
- def ggml_cl_host_malloc(
- size: Union[ctypes.c_size_t, int],
- ) -> Optional[ctypes.c_void_p]:
- return lib.ggml_cl_host_malloc(size)
- if GGML_USE_CLBLAST:
- lib.ggml_cl_host_malloc.argtypes = [
- ctypes.c_size_t,
- ]
- lib.ggml_cl_host_malloc.restype = ctypes.c_void_p
- # void ggml_cl_host_free(void * ptr);
- def ggml_cl_host_free(
- ptr: ctypes.c_void_p,
- ):
- return lib.ggml_cl_host_free(ptr)
- if GGML_USE_CLBLAST:
- lib.ggml_cl_host_free.argtypes = [
- ctypes.c_void_p,
- ]
- lib.ggml_cl_host_free.restype = None
- # void ggml_cl_free_data(const struct ggml_tensor* tensor);
- def ggml_cl_free_data(
- tensor: ggml_tensor_p,
- ):
- return lib.ggml_cl_free_data(tensor)
- if GGML_USE_CLBLAST:
- lib.ggml_cl_free_data.argtypes = [
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_cl_free_data.restype = None
- # void ggml_cl_transform_tensor(void * data, struct ggml_tensor * tensor);
- def ggml_cl_transform_tensor(
- data: ctypes.c_void_p,
- tensor: ggml_tensor_p,
- ):
- return lib.ggml_cl_transform_tensor(data, tensor)
- if GGML_USE_CLBLAST:
- lib.ggml_cl_transform_tensor.argtypes = [
- ctypes.c_void_p,
- ctypes.POINTER(ggml_tensor),
- ]
- lib.ggml_cl_transform_tensor.restype = None
|