ggml-quants.c 600 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297
  1. #define GGML_COMMON_IMPL_C
  2. #include "ggml-common.h"
  3. #include "ggml-quants.h"
  4. #include "ggml-impl.h"
  5. #include <math.h>
  6. #include <string.h>
  7. #include <assert.h>
  8. #include <float.h>
  9. #include <stdlib.h> // for qsort
  10. #include <stdio.h> // for GGML_ASSERT
  11. #define GROUP_MAX_EPS 1e-15f
  12. #define GROUP_MAX_EPS_IQ3_XXS 1e-8f
  13. #define GROUP_MAX_EPS_IQ2_S 1e-8f
  14. #define GROUP_MAX_EPS_IQ1_M 1e-7f
  15. #define GROUP_MAX_EPS_IQ1_S 1e-12f
  16. #if defined(_MSC_VER)
  17. // disable "possible loss of data" to avoid warnings for hundreds of casts
  18. // we should just be careful :)
  19. #pragma warning(disable: 4244 4267)
  20. #endif
  21. #define UNUSED GGML_UNUSED
  22. // some compilers don't provide _mm256_set_m128i, e.g. gcc 7
  23. #define MM256_SET_M128I(a, b) _mm256_insertf128_si256(_mm256_castsi128_si256(b), (a), 1)
  24. #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__)
  25. // multiply int8_t, add results pairwise twice
  26. static inline __m128i mul_sum_i8_pairs(const __m128i x, const __m128i y) {
  27. // Get absolute values of x vectors
  28. const __m128i ax = _mm_sign_epi8(x, x);
  29. // Sign the values of the y vectors
  30. const __m128i sy = _mm_sign_epi8(y, x);
  31. // Perform multiplication and create 16-bit values
  32. const __m128i dot = _mm_maddubs_epi16(ax, sy);
  33. const __m128i ones = _mm_set1_epi16(1);
  34. return _mm_madd_epi16(ones, dot);
  35. }
  36. #if __AVX__ || __AVX2__ || __AVX512F__
  37. // horizontally add 8 floats
  38. static inline float hsum_float_8(const __m256 x) {
  39. __m128 res = _mm256_extractf128_ps(x, 1);
  40. res = _mm_add_ps(res, _mm256_castps256_ps128(x));
  41. res = _mm_add_ps(res, _mm_movehl_ps(res, res));
  42. res = _mm_add_ss(res, _mm_movehdup_ps(res));
  43. return _mm_cvtss_f32(res);
  44. }
  45. // horizontally add 8 int32_t
  46. static inline int hsum_i32_8(const __m256i a) {
  47. const __m128i sum128 = _mm_add_epi32(_mm256_castsi256_si128(a), _mm256_extractf128_si256(a, 1));
  48. const __m128i hi64 = _mm_unpackhi_epi64(sum128, sum128);
  49. const __m128i sum64 = _mm_add_epi32(hi64, sum128);
  50. const __m128i hi32 = _mm_shuffle_epi32(sum64, _MM_SHUFFLE(2, 3, 0, 1));
  51. return _mm_cvtsi128_si32(_mm_add_epi32(sum64, hi32));
  52. }
  53. // horizontally add 4 int32_t
  54. static inline int hsum_i32_4(const __m128i a) {
  55. const __m128i hi64 = _mm_unpackhi_epi64(a, a);
  56. const __m128i sum64 = _mm_add_epi32(hi64, a);
  57. const __m128i hi32 = _mm_shuffle_epi32(sum64, _MM_SHUFFLE(2, 3, 0, 1));
  58. return _mm_cvtsi128_si32(_mm_add_epi32(sum64, hi32));
  59. }
  60. #if defined(__AVX2__) || defined(__AVX512F__)
  61. // spread 32 bits to 32 bytes { 0x00, 0xFF }
  62. static inline __m256i bytes_from_bits_32(const uint8_t * x) {
  63. uint32_t x32;
  64. memcpy(&x32, x, sizeof(uint32_t));
  65. const __m256i shuf_mask = _mm256_set_epi64x(
  66. 0x0303030303030303, 0x0202020202020202,
  67. 0x0101010101010101, 0x0000000000000000);
  68. __m256i bytes = _mm256_shuffle_epi8(_mm256_set1_epi32(x32), shuf_mask);
  69. const __m256i bit_mask = _mm256_set1_epi64x(0x7fbfdfeff7fbfdfe);
  70. bytes = _mm256_or_si256(bytes, bit_mask);
  71. return _mm256_cmpeq_epi8(bytes, _mm256_set1_epi64x(-1));
  72. }
  73. // Unpack 32 4-bit fields into 32 bytes
  74. // The output vector contains 32 bytes, each one in [ 0 .. 15 ] interval
  75. static inline __m256i bytes_from_nibbles_32(const uint8_t * rsi)
  76. {
  77. const __m128i tmp = _mm_loadu_si128((const __m128i *)rsi);
  78. const __m256i bytes = MM256_SET_M128I(_mm_srli_epi16(tmp, 4), tmp);
  79. const __m256i lowMask = _mm256_set1_epi8( 0xF );
  80. return _mm256_and_si256(lowMask, bytes);
  81. }
  82. // add int16_t pairwise and return as float vector
  83. static inline __m256 sum_i16_pairs_float(const __m256i x) {
  84. const __m256i ones = _mm256_set1_epi16(1);
  85. const __m256i summed_pairs = _mm256_madd_epi16(ones, x);
  86. return _mm256_cvtepi32_ps(summed_pairs);
  87. }
  88. static inline __m256 mul_sum_us8_pairs_float(const __m256i ax, const __m256i sy) {
  89. #if defined(__AVXVNNI__) || (defined(__AVX512VNNI__) && defined(__AVX512VL__))
  90. const __m256i zero = _mm256_setzero_si256();
  91. const __m256i summed_pairs = _mm256_dpbusd_epi32(zero, ax, sy);
  92. return _mm256_cvtepi32_ps(summed_pairs);
  93. #else
  94. // Perform multiplication and create 16-bit values
  95. const __m256i dot = _mm256_maddubs_epi16(ax, sy);
  96. return sum_i16_pairs_float(dot);
  97. #endif
  98. }
  99. // multiply int8_t, add results pairwise twice and return as float vector
  100. static inline __m256 mul_sum_i8_pairs_float(const __m256i x, const __m256i y) {
  101. #if __AVXVNNIINT8__
  102. const __m256i zero = _mm256_setzero_si256();
  103. const __m256i summed_pairs = _mm256_dpbssd_epi32(zero, x, y);
  104. return _mm256_cvtepi32_ps(summed_pairs);
  105. #else
  106. // Get absolute values of x vectors
  107. const __m256i ax = _mm256_sign_epi8(x, x);
  108. // Sign the values of the y vectors
  109. const __m256i sy = _mm256_sign_epi8(y, x);
  110. return mul_sum_us8_pairs_float(ax, sy);
  111. #endif
  112. }
  113. static inline __m128i packNibbles( __m256i bytes )
  114. {
  115. // Move bits within 16-bit lanes from 0000_abcd_0000_efgh into 0000_0000_abcd_efgh
  116. #if __AVX512F__
  117. const __m256i bytes_srli_4 = _mm256_srli_epi16(bytes, 4); // 0000_0000_abcd_0000
  118. bytes = _mm256_or_si256(bytes, bytes_srli_4); // 0000_abcd_abcd_efgh
  119. return _mm256_cvtepi16_epi8(bytes); // abcd_efgh
  120. #else
  121. const __m256i lowByte = _mm256_set1_epi16( 0xFF );
  122. __m256i high = _mm256_andnot_si256( lowByte, bytes );
  123. __m256i low = _mm256_and_si256( lowByte, bytes );
  124. high = _mm256_srli_epi16( high, 4 );
  125. bytes = _mm256_or_si256( low, high );
  126. // Compress uint16_t lanes into bytes
  127. __m128i r0 = _mm256_castsi256_si128( bytes );
  128. __m128i r1 = _mm256_extracti128_si256( bytes, 1 );
  129. return _mm_packus_epi16( r0, r1 );
  130. #endif
  131. }
  132. #elif defined(__AVX__)
  133. // spread 32 bits to 32 bytes { 0x00, 0xFF }
  134. static inline __m256i bytes_from_bits_32(const uint8_t * x) {
  135. uint32_t x32;
  136. memcpy(&x32, x, sizeof(uint32_t));
  137. const __m128i shuf_maskl = _mm_set_epi64x(0x0101010101010101, 0x0000000000000000);
  138. const __m128i shuf_maskh = _mm_set_epi64x(0x0303030303030303, 0x0202020202020202);
  139. __m128i bytesl = _mm_shuffle_epi8(_mm_set1_epi32(x32), shuf_maskl);
  140. __m128i bytesh = _mm_shuffle_epi8(_mm_set1_epi32(x32), shuf_maskh);
  141. const __m128i bit_mask = _mm_set1_epi64x(0x7fbfdfeff7fbfdfe);
  142. bytesl = _mm_or_si128(bytesl, bit_mask);
  143. bytesh = _mm_or_si128(bytesh, bit_mask);
  144. bytesl = _mm_cmpeq_epi8(bytesl, _mm_set1_epi64x(-1));
  145. bytesh = _mm_cmpeq_epi8(bytesh, _mm_set1_epi64x(-1));
  146. return MM256_SET_M128I(bytesh, bytesl);
  147. }
  148. // Unpack 32 4-bit fields into 32 bytes
  149. // The output vector contains 32 bytes, each one in [ 0 .. 15 ] interval
  150. static inline __m256i bytes_from_nibbles_32(const uint8_t * rsi)
  151. {
  152. // Load 16 bytes from memory
  153. __m128i tmpl = _mm_loadu_si128((const __m128i *)rsi);
  154. __m128i tmph = _mm_srli_epi16(tmpl, 4);
  155. const __m128i lowMask = _mm_set1_epi8(0xF);
  156. tmpl = _mm_and_si128(lowMask, tmpl);
  157. tmph = _mm_and_si128(lowMask, tmph);
  158. return MM256_SET_M128I(tmph, tmpl);
  159. }
  160. // add int16_t pairwise and return as float vector
  161. static inline __m256 sum_i16_pairs_float(const __m128i xh, const __m128i xl) {
  162. const __m128i ones = _mm_set1_epi16(1);
  163. const __m128i summed_pairsl = _mm_madd_epi16(ones, xl);
  164. const __m128i summed_pairsh = _mm_madd_epi16(ones, xh);
  165. const __m256i summed_pairs = MM256_SET_M128I(summed_pairsh, summed_pairsl);
  166. return _mm256_cvtepi32_ps(summed_pairs);
  167. }
  168. static inline __m256 mul_sum_us8_pairs_float(const __m256i ax, const __m256i sy) {
  169. const __m128i axl = _mm256_castsi256_si128(ax);
  170. const __m128i axh = _mm256_extractf128_si256(ax, 1);
  171. const __m128i syl = _mm256_castsi256_si128(sy);
  172. const __m128i syh = _mm256_extractf128_si256(sy, 1);
  173. // Perform multiplication and create 16-bit values
  174. const __m128i dotl = _mm_maddubs_epi16(axl, syl);
  175. const __m128i doth = _mm_maddubs_epi16(axh, syh);
  176. return sum_i16_pairs_float(doth, dotl);
  177. }
  178. // multiply int8_t, add results pairwise twice and return as float vector
  179. static inline __m256 mul_sum_i8_pairs_float(const __m256i x, const __m256i y) {
  180. const __m128i xl = _mm256_castsi256_si128(x);
  181. const __m128i xh = _mm256_extractf128_si256(x, 1);
  182. const __m128i yl = _mm256_castsi256_si128(y);
  183. const __m128i yh = _mm256_extractf128_si256(y, 1);
  184. // Get absolute values of x vectors
  185. const __m128i axl = _mm_sign_epi8(xl, xl);
  186. const __m128i axh = _mm_sign_epi8(xh, xh);
  187. // Sign the values of the y vectors
  188. const __m128i syl = _mm_sign_epi8(yl, xl);
  189. const __m128i syh = _mm_sign_epi8(yh, xh);
  190. // Perform multiplication and create 16-bit values
  191. const __m128i dotl = _mm_maddubs_epi16(axl, syl);
  192. const __m128i doth = _mm_maddubs_epi16(axh, syh);
  193. return sum_i16_pairs_float(doth, dotl);
  194. }
  195. static inline __m128i packNibbles( __m128i bytes1, __m128i bytes2 )
  196. {
  197. // Move bits within 16-bit lanes from 0000_abcd_0000_efgh into 0000_0000_abcd_efgh
  198. const __m128i lowByte = _mm_set1_epi16( 0xFF );
  199. __m128i high = _mm_andnot_si128( lowByte, bytes1 );
  200. __m128i low = _mm_and_si128( lowByte, bytes1 );
  201. high = _mm_srli_epi16( high, 4 );
  202. bytes1 = _mm_or_si128( low, high );
  203. high = _mm_andnot_si128( lowByte, bytes2 );
  204. low = _mm_and_si128( lowByte, bytes2 );
  205. high = _mm_srli_epi16( high, 4 );
  206. bytes2 = _mm_or_si128( low, high );
  207. return _mm_packus_epi16( bytes1, bytes2);
  208. }
  209. #endif
  210. #elif defined(__SSSE3__)
  211. // horizontally add 4x4 floats
  212. static inline float hsum_float_4x4(const __m128 a, const __m128 b, const __m128 c, const __m128 d) {
  213. __m128 res_0 =_mm_hadd_ps(a, b);
  214. __m128 res_1 =_mm_hadd_ps(c, d);
  215. __m128 res =_mm_hadd_ps(res_0, res_1);
  216. res =_mm_hadd_ps(res, res);
  217. res =_mm_hadd_ps(res, res);
  218. return _mm_cvtss_f32(res);
  219. }
  220. #endif // __AVX__ || __AVX2__ || __AVX512F__
  221. #endif // defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__)
  222. #if defined(__ARM_NEON) || defined(__wasm_simd128__) || defined(__POWER9_VECTOR__)
  223. #define B1(c,s,n) 0x ## n ## c , 0x ## n ## s
  224. #define B2(c,s,n) B1(c,s,n ## c), B1(c,s,n ## s)
  225. #define B3(c,s,n) B2(c,s,n ## c), B2(c,s,n ## s)
  226. #define B4(c,s,n) B3(c,s,n ## c), B3(c,s,n ## s)
  227. #define B5(c,s,n) B4(c,s,n ## c), B4(c,s,n ## s)
  228. #define B6(c,s,n) B5(c,s,n ## c), B5(c,s,n ## s)
  229. #define B7(c,s,n) B6(c,s,n ## c), B6(c,s,n ## s)
  230. #define B8(c,s ) B7(c,s, c), B7(c,s, s)
  231. // precomputed tables for expanding 8bits to 8 bytes:
  232. static const uint64_t table_b2b_0[1 << 8] = { B8(00, 10) }; // ( b) << 4
  233. static const uint64_t table_b2b_1[1 << 8] = { B8(10, 00) }; // (!b) << 4
  234. #endif
  235. #if defined(__loongarch_asx)
  236. #ifdef __clang__
  237. #define VREGS_PREFIX "$vr"
  238. #define XREGS_PREFIX "$xr"
  239. #else // GCC
  240. #define VREGS_PREFIX "$f"
  241. #define XREGS_PREFIX "$f"
  242. #endif
  243. #define __ALL_REGS "0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31"
  244. // Convert __m128i to __m256i
  245. static inline __m256i ____m256i(__m128i in) {
  246. __m256i out = __lasx_xvldi(0);
  247. __asm__ volatile (
  248. ".irp i," __ALL_REGS "\n\t"
  249. " .ifc %[out], " XREGS_PREFIX"\\i \n\t"
  250. " .irp j," __ALL_REGS "\n\t"
  251. " .ifc %[in], " VREGS_PREFIX "\\j \n\t"
  252. " xvpermi.q $xr\\i, $xr\\j, 0x20 \n\t"
  253. " .endif \n\t"
  254. " .endr \n\t"
  255. " .endif \n\t"
  256. ".endr \n\t"
  257. : [out] "+f" (out) : [in] "f" (in)
  258. );
  259. return out;
  260. }
  261. // Convert two __m128i to __m256i
  262. static inline __m256i lasx_set_q(__m128i inhi, __m128i inlo) {
  263. __m256i out;
  264. __asm__ volatile (
  265. ".irp i," __ALL_REGS "\n\t"
  266. " .ifc %[hi], " VREGS_PREFIX "\\i \n\t"
  267. " .irp j," __ALL_REGS "\n\t"
  268. " .ifc %[lo], " VREGS_PREFIX "\\j \n\t"
  269. " xvpermi.q $xr\\i, $xr\\j, 0x20 \n\t"
  270. " .endif \n\t"
  271. " .endr \n\t"
  272. " .endif \n\t"
  273. ".endr \n\t"
  274. ".ifnc %[out], %[hi] \n\t"
  275. ".irp i," __ALL_REGS "\n\t"
  276. " .ifc %[out], " XREGS_PREFIX "\\i \n\t"
  277. " .irp j," __ALL_REGS "\n\t"
  278. " .ifc %[hi], " VREGS_PREFIX "\\j \n\t"
  279. " xvori.b $xr\\i, $xr\\j, 0 \n\t"
  280. " .endif \n\t"
  281. " .endr \n\t"
  282. " .endif \n\t"
  283. ".endr \n\t"
  284. ".endif \n\t"
  285. : [out] "=f" (out), [hi] "+f" (inhi)
  286. : [lo] "f" (inlo)
  287. );
  288. return out;
  289. }
  290. // Convert __m256i low part to __m128i
  291. static inline __m128i lasx_extracti128_lo(__m256i in) {
  292. __m128i out;
  293. __asm__ volatile (
  294. ".ifnc %[out], %[in] \n\t"
  295. ".irp i," __ALL_REGS "\n\t"
  296. " .ifc %[out], " VREGS_PREFIX "\\i \n\t"
  297. " .irp j," __ALL_REGS "\n\t"
  298. " .ifc %[in], " XREGS_PREFIX "\\j \n\t"
  299. " vori.b $vr\\i, $vr\\j, 0 \n\t"
  300. " .endif \n\t"
  301. " .endr \n\t"
  302. " .endif \n\t"
  303. ".endr \n\t"
  304. ".endif \n\t"
  305. : [out] "=f" (out) : [in] "f" (in)
  306. );
  307. return out;
  308. }
  309. // Convert __m256i high part to __m128i
  310. static inline __m128i lasx_extracti128_hi(__m256i in) {
  311. __m128i out;
  312. __asm__ volatile (
  313. ".irp i," __ALL_REGS "\n\t"
  314. " .ifc %[out], " VREGS_PREFIX "\\i \n\t"
  315. " .irp j," __ALL_REGS "\n\t"
  316. " .ifc %[in], " XREGS_PREFIX "\\j \n\t"
  317. " xvpermi.q $xr\\i, $xr\\j, 0x11 \n\t"
  318. " .endif \n\t"
  319. " .endr \n\t"
  320. " .endif \n\t"
  321. ".endr \n\t"
  322. : [out] "=f" (out) : [in] "f" (in)
  323. );
  324. return out;
  325. }
  326. static __m256i lasx_set_w(int e7, int e6, int e5, int e4, int e3, int e2, int e1, int e0) {
  327. v8i32 __ret = {e0, e1, e2, e3, e4, e5, e6, e7};
  328. return (__m256i)__ret;
  329. }
  330. static __m128i lsx_set_w(int32_t a, int32_t b, int32_t c, int32_t d) {
  331. v4i32 __ret = {d, c, b, a};
  332. return (__m128i)__ret;
  333. }
  334. static __m256i lasx_set_d(int64_t a, int64_t b, int64_t c, int64_t d) {
  335. v4i64 __ret = {d, c, b, a};
  336. return (__m256i)__ret;
  337. }
  338. static __m256i lasx_insertf128( __m128i x, __m128i y) {
  339. return lasx_set_q(x, y);
  340. }
  341. static __m128i lsx_shuffle_b(__m128i a, __m128i b) {
  342. __m128i mask_f, zero, tmp0, tmp2, mask;
  343. int f = 0x8f;
  344. mask_f = __lsx_vreplgr2vr_b(f);
  345. zero = __lsx_vldi(0);
  346. tmp0 = __lsx_vand_v(b, mask_f); // get mask with low 4 bit and sign bits
  347. tmp0 = __lsx_vori_b(tmp0, 0x10); // make each mask or with 0x10 prepare for positive
  348. mask = __lsx_vsle_b(zero, tmp0); // if mask >= 0, set mask
  349. tmp2 = __lsx_vand_v(tmp0, mask); // maskout the in2 < ones
  350. return __lsx_vshuf_b(a, zero, tmp2);
  351. }
  352. static __m256i lasx_shuffle_b(__m256i a, __m256i b) {
  353. __m256i mask_f, zero, tmp0, tmp2, mask;
  354. int f = 0x8f;
  355. mask_f = __lasx_xvreplgr2vr_b(f);
  356. zero = __lasx_xvldi(0);
  357. tmp0 = __lasx_xvand_v(b, mask_f); // get mask with low 4 bit and sign bits
  358. tmp0 = __lasx_xvori_b(tmp0, 0x10); // make each mask or with 0x10 prepare for positive
  359. mask = __lasx_xvsle_b(zero, tmp0); // if mask >= 0, set mask
  360. tmp2 = __lasx_xvand_v(tmp0, mask); // maskout the in2 < ones
  361. return __lasx_xvshuf_b(a, zero, tmp2);
  362. }
  363. static __m256i lasx_extu8_16(__m128i a) {
  364. __m128i zero = __lsx_vldi(0);
  365. __m128i vlo = __lsx_vilvl_b(zero, a);
  366. __m128i vhi = __lsx_vilvh_b(zero, a);
  367. return lasx_set_q(vhi, vlo);
  368. }
  369. static __m256i lasx_ext8_16(__m128i a) {
  370. __m128i sign = __lsx_vslti_b(a, 0);
  371. __m128i vlo = __lsx_vilvl_b(sign, a);
  372. __m128i vhi = __lsx_vilvh_b(sign, a);
  373. return lasx_set_q(vhi, vlo);
  374. }
  375. static __m256i lasx_ext16_32(__m128i a) {
  376. __m256i tmp1;
  377. tmp1 = __lasx_xvinsgr2vr_w(tmp1, __lsx_vpickve2gr_h(a, 0), 0);
  378. tmp1 = __lasx_xvinsgr2vr_w(tmp1, __lsx_vpickve2gr_h(a, 1), 1);
  379. tmp1 = __lasx_xvinsgr2vr_w(tmp1, __lsx_vpickve2gr_h(a, 2), 2);
  380. tmp1 = __lasx_xvinsgr2vr_w(tmp1, __lsx_vpickve2gr_h(a, 3), 3);
  381. tmp1 = __lasx_xvinsgr2vr_w(tmp1, __lsx_vpickve2gr_h(a, 4), 4);
  382. tmp1 = __lasx_xvinsgr2vr_w(tmp1, __lsx_vpickve2gr_h(a, 5), 5);
  383. tmp1 = __lasx_xvinsgr2vr_w(tmp1, __lsx_vpickve2gr_h(a, 6), 6);
  384. tmp1 = __lasx_xvinsgr2vr_w(tmp1, __lsx_vpickve2gr_h(a, 7), 7);
  385. return tmp1;
  386. }
  387. static __m128i lasx_extracti128( __m256i a, int pos) {
  388. __m128i ret;
  389. if( pos == 0)
  390. {
  391. ret = lasx_extracti128_lo(a);
  392. } else {
  393. ret = lasx_extracti128_hi(a);
  394. }
  395. return ret;
  396. }
  397. static __m128 lasx_extractf128( __m256 a, int pos) {
  398. __m128 ret;
  399. if( pos == 0)
  400. {
  401. ret = (__m128)lasx_extracti128_lo((__m256i)a);
  402. } else {
  403. ret = (__m128)lasx_extracti128_hi((__m256i)a);
  404. }
  405. return ret;
  406. }
  407. static __m128i lsx_hadd_h(__m128i a, __m128i b) {
  408. __m128i tmp1 = __lsx_vpickev_h(b, a);
  409. __m128i tmp2 = __lsx_vpickod_h(b, a);
  410. return __lsx_vadd_h(tmp1, tmp2);
  411. }
  412. static __m128i lsx_hadd_w(__m128i a, __m128i b) {
  413. __m128i tmp1 = __lsx_vpickev_w(b, a);
  414. __m128i tmp2 = __lsx_vpickod_w(b, a);
  415. return __lsx_vadd_w(tmp1, tmp2);
  416. }
  417. static __m128 lsx_hadd_s(__m128 a, __m128 b) {
  418. __m128 tmp1 = (__m128)__lsx_vpickev_w((__m128i)b, (__m128i)a);
  419. __m128 tmp2 = (__m128)__lsx_vpickod_w((__m128i)b, (__m128i)a);
  420. return __lsx_vfadd_s(tmp1, tmp2);
  421. }
  422. static __m256i lasx_maddubs_h(__m256i a, __m256i b) {
  423. __m256i tmp1, tmp2;
  424. tmp1 = __lasx_xvmulwev_h_b(a, b);
  425. tmp2 = __lasx_xvmulwod_h_b(a, b);
  426. return __lasx_xvsadd_h(tmp1, tmp2);
  427. }
  428. static __m256i lasx_madd_h(__m256i a, __m256i b) {
  429. __m256i tmp1, tmp2;
  430. tmp1 = __lasx_xvmulwev_w_h(a, b);
  431. tmp2 = __lasx_xvmulwod_w_h(a, b);
  432. return __lasx_xvadd_w(tmp1, tmp2);
  433. }
  434. static __m256i lasx_packs_w(__m256i a, __m256i b) {
  435. __m256i tmp, tmp1;
  436. tmp = __lasx_xvsat_w(a, 15);
  437. tmp1 = __lasx_xvsat_w(b, 15);
  438. return __lasx_xvpickev_h(tmp1, tmp);
  439. }
  440. static __m256i lasx_packs_h(__m256i a, __m256i b) {
  441. __m256i tmp, tmp1;
  442. tmp = __lasx_xvsat_h(a, 7);
  443. tmp1 = __lasx_xvsat_h(b, 7);
  444. return __lasx_xvpickev_b(tmp1, tmp);
  445. }
  446. static __m128i lsx_packs_w(__m128i a, __m128i b) {
  447. __m128i tmp, tmp1;
  448. tmp = __lsx_vsat_w(a, 15);
  449. tmp1 = __lsx_vsat_w(b, 15);
  450. return __lsx_vpickev_h(tmp1, tmp);
  451. }
  452. static __m128i lsx_packs_h(__m128i a, __m128i b) {
  453. __m128i tmp, tmp1;
  454. tmp = __lsx_vsat_h(a, 7);
  455. tmp1 = __lsx_vsat_h(b, 7);
  456. return __lsx_vpickev_b(tmp1, tmp);
  457. }
  458. static __m128i lsx_packus_h(__m128i a, __m128i b) {
  459. __m128i tmp, tmp1;
  460. tmp = __lsx_vsat_hu(a, 7);
  461. tmp1 = __lsx_vsat_hu(b, 7);
  462. return __lsx_vpickev_b(tmp1, tmp);
  463. }
  464. static __m128i lsx_maddubs_h(__m128i a, __m128i b) {
  465. __m128i tmp1, tmp2;
  466. tmp1 = __lsx_vmulwev_h_b(a, b);
  467. tmp2 = __lsx_vmulwod_h_b(a, b);
  468. return __lsx_vsadd_h(tmp1, tmp2);
  469. }
  470. static __m128i lsx_madd_h(__m128i a, __m128i b) {
  471. __m128i tmp1, tmp2;
  472. tmp1 = __lsx_vmulwev_w_h(a, b);
  473. tmp2 = __lsx_vmulwod_w_h(a, b);
  474. return __lsx_vadd_w(tmp1, tmp2);
  475. }
  476. // multiply int8_t, add results pairwise twice
  477. static inline __m128i mul_sum_i8_pairs(const __m128i x, const __m128i y) {
  478. // Get absolute values of x vectors
  479. const __m128i ax = __lsx_vsigncov_b(x, x);
  480. // Sign the values of the y vectors
  481. const __m128i sy = __lsx_vsigncov_b(x, y);
  482. // Perform multiplication and create 16-bit values
  483. const __m128i dot = lsx_maddubs_h(ax, sy);
  484. const __m128i ones = __lsx_vreplgr2vr_h(1);
  485. return lsx_madd_h(ones, dot);
  486. }
  487. // horizontally add 8 floats
  488. static inline float hsum_float_8(const __m256 x) {
  489. __m128 res = lasx_extractf128(x, 1);
  490. ft_union tmp;
  491. res = __lsx_vfadd_s(res, lasx_extractf128(x, 0));
  492. res = __lsx_vfadd_s(res, (__m128)__lsx_vpickod_d((__m128i)res, (__m128i)res));
  493. res = __lsx_vfadd_s(res, (__m128)__lsx_vinsgr2vr_w(__lsx_vldi(0), __lsx_vpickve2gr_w(res, 1), 0));
  494. tmp.i = __lsx_vpickve2gr_w(res, 0);
  495. return tmp.f;
  496. }
  497. // horizontally add 8 int32_t
  498. static inline int hsum_i32_8(const __m256i a) {
  499. __m256i tmp1 = __lasx_xvpermi_q(a, a, 0x11);
  500. __m256i tmp2 = __lasx_xvpermi_q(a, a, 0x00);
  501. __m128i tmp1_128 = lasx_extracti128_lo(tmp1);
  502. __m128i tmp2_128 = lasx_extracti128_lo(tmp2);
  503. __m128i sum128 = __lsx_vadd_w(tmp1_128, tmp2_128);
  504. __m128i ev = __lsx_vpickev_w(sum128, sum128);
  505. __m128i od = __lsx_vpickod_w(sum128, sum128);
  506. __m128i sum64 = __lsx_vadd_w(ev, od);
  507. int sum64_1, sum64_2;
  508. sum64_1 = __lsx_vpickve2gr_w(sum64, 0);
  509. sum64_2 = __lsx_vpickve2gr_w(sum64, 1);
  510. return sum64_1 + sum64_2;
  511. }
  512. // horizontally add 4 int32_t
  513. static inline int hsum_i32_4(const __m128i a) {
  514. __m128i ev = __lsx_vpickev_w(a, a);
  515. __m128i od = __lsx_vpickod_w(a, a);
  516. __m128i sum64 = __lsx_vadd_w(ev, od);
  517. int sum64_1, sum64_2;
  518. sum64_1 = __lsx_vpickve2gr_w(sum64, 0);
  519. sum64_2 = __lsx_vpickve2gr_w(sum64, 1);
  520. return sum64_1 + sum64_2;
  521. }
  522. // spread 32 bits to 32 bytes { 0x00, 0xFF }
  523. static inline __m256i bytes_from_bits_32(const uint8_t * x) {
  524. uint32_t x32;
  525. memcpy(&x32, x, sizeof(uint32_t));
  526. const __m256i shuf_mask = lasx_set_d(
  527. 0x0303030303030303, 0x0202020202020202,
  528. 0x0101010101010101, 0x0000000000000000);
  529. __m256i bytes = lasx_shuffle_b(__lasx_xvreplgr2vr_w(x32), shuf_mask);
  530. const __m256i bit_mask = __lasx_xvreplgr2vr_d(0x7fbfdfeff7fbfdfe);
  531. bytes = __lasx_xvor_v(bytes, bit_mask);
  532. return __lasx_xvseq_b(bytes, __lasx_xvreplgr2vr_d(-1));
  533. }
  534. // Unpack 32 4-bit fields into 32 bytes
  535. // The output vector contains 32 bytes, each one in [ 0 .. 15 ] interval
  536. static inline __m256i bytes_from_nibbles_32(const uint8_t * rsi) {
  537. const __m128i lo = __lsx_vld((const __m128i *)rsi, 0);
  538. __m128i hi = __lsx_vsrli_h(lo, 4);
  539. return __lasx_xvandi_b(lasx_insertf128(hi, lo), 0xf);
  540. }
  541. // add int16_t pairwise and return as float vector
  542. static inline __m256 sum_i16_pairs_float(const __m256i x) {
  543. __m256i v = __lasx_xvpackod_h(x, x);
  544. __m256i summed_pairs = __lasx_xvaddwev_w_h(x, v);
  545. return __lasx_xvffint_s_w(summed_pairs);
  546. }
  547. static inline __m256 mul_sum_us8_pairs_float(const __m256i ax, const __m256i sy) {
  548. // Perform multiplication and create 16-bit values
  549. const __m256i dot = lasx_maddubs_h(ax, sy);
  550. return sum_i16_pairs_float(dot);
  551. }
  552. // multiply int8_t, add results pairwise twice and return as float vector
  553. static inline __m256 mul_sum_i8_pairs_float(const __m256i x, const __m256i y) {
  554. // Get absolute values of x vectors
  555. const __m256i ax = __lasx_xvsigncov_b(x, x);
  556. // Sign the values of the y vectors
  557. const __m256i sy = __lasx_xvsigncov_b(x, y);
  558. return mul_sum_us8_pairs_float(ax, sy);
  559. }
  560. static inline __m128i packNibbles( __m256i bytes ) {
  561. // Move bits within 16-bit lanes from 0000_abcd_0000_efgh into 0000_0000_abcd_efgh
  562. const __m256i lowByte = __lasx_xvreplgr2vr_h(0xFF);
  563. __m256i high = __lasx_xvandn_v(lowByte, bytes);
  564. __m256i low = __lasx_xvand_v(lowByte, bytes);
  565. high = __lasx_xvsrli_h(high, 4);
  566. bytes = __lasx_xvor_v(low, high);
  567. // Compress uint16_t lanes into bytes
  568. __m128i *r0 = (__m128i *)&bytes;
  569. __m256i tmp_h128 = __lasx_xvpermi_q(bytes, bytes, 0x11);
  570. __m128i *r1 = (__m128i *)&tmp_h128;
  571. __m128i zero = __lsx_vldi(0);
  572. __m128i tmp, tmp2, tmp3;
  573. tmp = __lsx_vmax_h(zero, *r0);
  574. tmp2 = __lsx_vsat_hu(tmp, 7);
  575. tmp = __lsx_vmax_h(zero, *r1);
  576. tmp3 = __lsx_vsat_hu(tmp, 7);
  577. return __lsx_vpickev_b(tmp3, tmp2);
  578. }
  579. #endif //__loongarch_asx
  580. // reference implementation for deterministic creation of model files
  581. void quantize_row_q4_0_reference(const float * restrict x, block_q4_0 * restrict y, int64_t k) {
  582. static const int qk = QK4_0;
  583. assert(k % qk == 0);
  584. const int nb = k / qk;
  585. for (int i = 0; i < nb; i++) {
  586. float amax = 0.0f; // absolute max
  587. float max = 0.0f;
  588. for (int j = 0; j < qk; j++) {
  589. const float v = x[i*qk + j];
  590. if (amax < fabsf(v)) {
  591. amax = fabsf(v);
  592. max = v;
  593. }
  594. }
  595. const float d = max / -8;
  596. const float id = d ? 1.0f/d : 0.0f;
  597. y[i].d = GGML_FP32_TO_FP16(d);
  598. for (int j = 0; j < qk/2; ++j) {
  599. const float x0 = x[i*qk + 0 + j]*id;
  600. const float x1 = x[i*qk + qk/2 + j]*id;
  601. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 8.5f));
  602. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 8.5f));
  603. y[i].qs[j] = xi0;
  604. y[i].qs[j] |= xi1 << 4;
  605. }
  606. }
  607. }
  608. void quantize_row_q4_0(const float * restrict x, void * restrict y, int64_t k) {
  609. quantize_row_q4_0_reference(x, y, k);
  610. }
  611. void quantize_row_q4_1_reference(const float * restrict x, block_q4_1 * restrict y, int64_t k) {
  612. const int qk = QK4_1;
  613. assert(k % qk == 0);
  614. const int nb = k / qk;
  615. for (int i = 0; i < nb; i++) {
  616. float min = FLT_MAX;
  617. float max = -FLT_MAX;
  618. for (int j = 0; j < qk; j++) {
  619. const float v = x[i*qk + j];
  620. if (v < min) min = v;
  621. if (v > max) max = v;
  622. }
  623. const float d = (max - min) / ((1 << 4) - 1);
  624. const float id = d ? 1.0f/d : 0.0f;
  625. y[i].d = GGML_FP32_TO_FP16(d);
  626. y[i].m = GGML_FP32_TO_FP16(min);
  627. for (int j = 0; j < qk/2; ++j) {
  628. const float x0 = (x[i*qk + 0 + j] - min)*id;
  629. const float x1 = (x[i*qk + qk/2 + j] - min)*id;
  630. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 0.5f));
  631. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 0.5f));
  632. y[i].qs[j] = xi0;
  633. y[i].qs[j] |= xi1 << 4;
  634. }
  635. }
  636. }
  637. void quantize_row_q4_1(const float * restrict x, void * restrict y, int64_t k) {
  638. quantize_row_q4_1_reference(x, y, k);
  639. }
  640. void quantize_row_q5_0_reference(const float * restrict x, block_q5_0 * restrict y, int64_t k) {
  641. static const int qk = QK5_0;
  642. assert(k % qk == 0);
  643. const int nb = k / qk;
  644. for (int i = 0; i < nb; i++) {
  645. float amax = 0.0f; // absolute max
  646. float max = 0.0f;
  647. for (int j = 0; j < qk; j++) {
  648. const float v = x[i*qk + j];
  649. if (amax < fabsf(v)) {
  650. amax = fabsf(v);
  651. max = v;
  652. }
  653. }
  654. const float d = max / -16;
  655. const float id = d ? 1.0f/d : 0.0f;
  656. y[i].d = GGML_FP32_TO_FP16(d);
  657. uint32_t qh = 0;
  658. for (int j = 0; j < qk/2; ++j) {
  659. const float x0 = x[i*qk + 0 + j]*id;
  660. const float x1 = x[i*qk + qk/2 + j]*id;
  661. const uint8_t xi0 = MIN(31, (int8_t)(x0 + 16.5f));
  662. const uint8_t xi1 = MIN(31, (int8_t)(x1 + 16.5f));
  663. y[i].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  664. // get the 5-th bit and store it in qh at the right position
  665. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  666. qh |= ((xi1 & 0x10u) >> 4) << (j + qk/2);
  667. }
  668. memcpy(&y[i].qh, &qh, sizeof(qh));
  669. }
  670. }
  671. void quantize_row_q5_0(const float * restrict x, void * restrict y, int64_t k) {
  672. quantize_row_q5_0_reference(x, y, k);
  673. }
  674. void quantize_row_q5_1_reference(const float * restrict x, block_q5_1 * restrict y, int64_t k) {
  675. const int qk = QK5_1;
  676. assert(k % qk == 0);
  677. const int nb = k / qk;
  678. for (int i = 0; i < nb; i++) {
  679. float min = FLT_MAX;
  680. float max = -FLT_MAX;
  681. for (int j = 0; j < qk; j++) {
  682. const float v = x[i*qk + j];
  683. if (v < min) min = v;
  684. if (v > max) max = v;
  685. }
  686. const float d = (max - min) / ((1 << 5) - 1);
  687. const float id = d ? 1.0f/d : 0.0f;
  688. y[i].d = GGML_FP32_TO_FP16(d);
  689. y[i].m = GGML_FP32_TO_FP16(min);
  690. uint32_t qh = 0;
  691. for (int j = 0; j < qk/2; ++j) {
  692. const float x0 = (x[i*qk + 0 + j] - min)*id;
  693. const float x1 = (x[i*qk + qk/2 + j] - min)*id;
  694. const uint8_t xi0 = (uint8_t)(x0 + 0.5f);
  695. const uint8_t xi1 = (uint8_t)(x1 + 0.5f);
  696. y[i].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  697. // get the 5-th bit and store it in qh at the right position
  698. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  699. qh |= ((xi1 & 0x10u) >> 4) << (j + qk/2);
  700. }
  701. memcpy(&y[i].qh, &qh, sizeof(y[i].qh));
  702. }
  703. }
  704. void quantize_row_q5_1(const float * restrict x, void * restrict y, int64_t k) {
  705. quantize_row_q5_1_reference(x, y, k);
  706. }
  707. // reference implementation for deterministic creation of model files
  708. void quantize_row_q8_0_reference(const float * restrict x, block_q8_0 * restrict y, int64_t k) {
  709. assert(k % QK8_0 == 0);
  710. const int nb = k / QK8_0;
  711. for (int i = 0; i < nb; i++) {
  712. float amax = 0.0f; // absolute max
  713. for (int j = 0; j < QK8_0; j++) {
  714. const float v = x[i*QK8_0 + j];
  715. amax = MAX(amax, fabsf(v));
  716. }
  717. const float d = amax / ((1 << 7) - 1);
  718. const float id = d ? 1.0f/d : 0.0f;
  719. y[i].d = GGML_FP32_TO_FP16(d);
  720. for (int j = 0; j < QK8_0; ++j) {
  721. const float x0 = x[i*QK8_0 + j]*id;
  722. y[i].qs[j] = roundf(x0);
  723. }
  724. }
  725. }
  726. void quantize_row_q8_0(const float * restrict x, void * restrict vy, int64_t k) {
  727. assert(QK8_0 == 32);
  728. assert(k % QK8_0 == 0);
  729. const int nb = k / QK8_0;
  730. block_q8_0 * restrict y = vy;
  731. #if defined(__ARM_NEON)
  732. for (int i = 0; i < nb; i++) {
  733. float32x4_t srcv [8];
  734. float32x4_t asrcv[8];
  735. float32x4_t amaxv[8];
  736. for (int j = 0; j < 8; j++) srcv[j] = vld1q_f32(x + i*32 + 4*j);
  737. for (int j = 0; j < 8; j++) asrcv[j] = vabsq_f32(srcv[j]);
  738. for (int j = 0; j < 4; j++) amaxv[2*j] = vmaxq_f32(asrcv[2*j], asrcv[2*j+1]);
  739. for (int j = 0; j < 2; j++) amaxv[4*j] = vmaxq_f32(amaxv[4*j], amaxv[4*j+2]);
  740. for (int j = 0; j < 1; j++) amaxv[8*j] = vmaxq_f32(amaxv[8*j], amaxv[8*j+4]);
  741. const float amax = vmaxvq_f32(amaxv[0]);
  742. const float d = amax / ((1 << 7) - 1);
  743. const float id = d ? 1.0f/d : 0.0f;
  744. y[i].d = GGML_FP32_TO_FP16(d);
  745. for (int j = 0; j < 8; j++) {
  746. const float32x4_t v = vmulq_n_f32(srcv[j], id);
  747. const int32x4_t vi = vcvtnq_s32_f32(v);
  748. y[i].qs[4*j + 0] = vgetq_lane_s32(vi, 0);
  749. y[i].qs[4*j + 1] = vgetq_lane_s32(vi, 1);
  750. y[i].qs[4*j + 2] = vgetq_lane_s32(vi, 2);
  751. y[i].qs[4*j + 3] = vgetq_lane_s32(vi, 3);
  752. }
  753. }
  754. #elif defined(__wasm_simd128__)
  755. for (int i = 0; i < nb; i++) {
  756. v128_t srcv [8];
  757. v128_t asrcv[8];
  758. v128_t amaxv[8];
  759. for (int j = 0; j < 8; j++) srcv[j] = wasm_v128_load(x + i*32 + 4*j);
  760. for (int j = 0; j < 8; j++) asrcv[j] = wasm_f32x4_abs(srcv[j]);
  761. for (int j = 0; j < 4; j++) amaxv[2*j] = wasm_f32x4_max(asrcv[2*j], asrcv[2*j+1]);
  762. for (int j = 0; j < 2; j++) amaxv[4*j] = wasm_f32x4_max(amaxv[4*j], amaxv[4*j+2]);
  763. for (int j = 0; j < 1; j++) amaxv[8*j] = wasm_f32x4_max(amaxv[8*j], amaxv[8*j+4]);
  764. const float amax = MAX(MAX(wasm_f32x4_extract_lane(amaxv[0], 0),
  765. wasm_f32x4_extract_lane(amaxv[0], 1)),
  766. MAX(wasm_f32x4_extract_lane(amaxv[0], 2),
  767. wasm_f32x4_extract_lane(amaxv[0], 3)));
  768. const float d = amax / ((1 << 7) - 1);
  769. const float id = d ? 1.0f/d : 0.0f;
  770. y[i].d = GGML_FP32_TO_FP16(d);
  771. for (int j = 0; j < 8; j++) {
  772. const v128_t v = wasm_f32x4_mul(srcv[j], wasm_f32x4_splat(id));
  773. const v128_t vi = wasm_i32x4_trunc_sat_f32x4(v);
  774. y[i].qs[4*j + 0] = wasm_i32x4_extract_lane(vi, 0);
  775. y[i].qs[4*j + 1] = wasm_i32x4_extract_lane(vi, 1);
  776. y[i].qs[4*j + 2] = wasm_i32x4_extract_lane(vi, 2);
  777. y[i].qs[4*j + 3] = wasm_i32x4_extract_lane(vi, 3);
  778. }
  779. }
  780. #elif defined(__AVX2__) || defined(__AVX__)
  781. for (int i = 0; i < nb; i++) {
  782. // Load elements into 4 AVX vectors
  783. __m256 v0 = _mm256_loadu_ps( x );
  784. __m256 v1 = _mm256_loadu_ps( x + 8 );
  785. __m256 v2 = _mm256_loadu_ps( x + 16 );
  786. __m256 v3 = _mm256_loadu_ps( x + 24 );
  787. x += 32;
  788. // Compute max(abs(e)) for the block
  789. const __m256 signBit = _mm256_set1_ps( -0.0f );
  790. __m256 maxAbs = _mm256_andnot_ps( signBit, v0 );
  791. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v1 ) );
  792. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v2 ) );
  793. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v3 ) );
  794. __m128 max4 = _mm_max_ps( _mm256_extractf128_ps( maxAbs, 1 ), _mm256_castps256_ps128( maxAbs ) );
  795. max4 = _mm_max_ps( max4, _mm_movehl_ps( max4, max4 ) );
  796. max4 = _mm_max_ss( max4, _mm_movehdup_ps( max4 ) );
  797. const float maxScalar = _mm_cvtss_f32( max4 );
  798. // Quantize these floats
  799. const float d = maxScalar / 127.f;
  800. y[i].d = GGML_FP32_TO_FP16(d);
  801. const float id = ( maxScalar != 0.0f ) ? 127.f / maxScalar : 0.0f;
  802. const __m256 mul = _mm256_set1_ps( id );
  803. // Apply the multiplier
  804. v0 = _mm256_mul_ps( v0, mul );
  805. v1 = _mm256_mul_ps( v1, mul );
  806. v2 = _mm256_mul_ps( v2, mul );
  807. v3 = _mm256_mul_ps( v3, mul );
  808. // Round to nearest integer
  809. v0 = _mm256_round_ps( v0, _MM_ROUND_NEAREST );
  810. v1 = _mm256_round_ps( v1, _MM_ROUND_NEAREST );
  811. v2 = _mm256_round_ps( v2, _MM_ROUND_NEAREST );
  812. v3 = _mm256_round_ps( v3, _MM_ROUND_NEAREST );
  813. // Convert floats to integers
  814. __m256i i0 = _mm256_cvtps_epi32( v0 );
  815. __m256i i1 = _mm256_cvtps_epi32( v1 );
  816. __m256i i2 = _mm256_cvtps_epi32( v2 );
  817. __m256i i3 = _mm256_cvtps_epi32( v3 );
  818. #if defined(__AVX2__)
  819. // Convert int32 to int16
  820. i0 = _mm256_packs_epi32( i0, i1 ); // 0, 1, 2, 3, 8, 9, 10, 11, 4, 5, 6, 7, 12, 13, 14, 15
  821. i2 = _mm256_packs_epi32( i2, i3 ); // 16, 17, 18, 19, 24, 25, 26, 27, 20, 21, 22, 23, 28, 29, 30, 31
  822. // Convert int16 to int8
  823. i0 = _mm256_packs_epi16( i0, i2 ); // 0, 1, 2, 3, 8, 9, 10, 11, 16, 17, 18, 19, 24, 25, 26, 27, 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31
  824. // We got our precious signed bytes, but the order is now wrong
  825. // These AVX2 pack instructions process 16-byte pieces independently
  826. // The following instruction is fixing the order
  827. const __m256i perm = _mm256_setr_epi32( 0, 4, 1, 5, 2, 6, 3, 7 );
  828. i0 = _mm256_permutevar8x32_epi32( i0, perm );
  829. _mm256_storeu_si256((__m256i *)y[i].qs, i0);
  830. #else
  831. // Since we don't have in AVX some necessary functions,
  832. // we split the registers in half and call AVX2 analogs from SSE
  833. __m128i ni0 = _mm256_castsi256_si128( i0 );
  834. __m128i ni1 = _mm256_extractf128_si256( i0, 1);
  835. __m128i ni2 = _mm256_castsi256_si128( i1 );
  836. __m128i ni3 = _mm256_extractf128_si256( i1, 1);
  837. __m128i ni4 = _mm256_castsi256_si128( i2 );
  838. __m128i ni5 = _mm256_extractf128_si256( i2, 1);
  839. __m128i ni6 = _mm256_castsi256_si128( i3 );
  840. __m128i ni7 = _mm256_extractf128_si256( i3, 1);
  841. // Convert int32 to int16
  842. ni0 = _mm_packs_epi32( ni0, ni1 );
  843. ni2 = _mm_packs_epi32( ni2, ni3 );
  844. ni4 = _mm_packs_epi32( ni4, ni5 );
  845. ni6 = _mm_packs_epi32( ni6, ni7 );
  846. // Convert int16 to int8
  847. ni0 = _mm_packs_epi16( ni0, ni2 );
  848. ni4 = _mm_packs_epi16( ni4, ni6 );
  849. _mm_storeu_si128((__m128i *)(y[i].qs + 0), ni0);
  850. _mm_storeu_si128((__m128i *)(y[i].qs + 16), ni4);
  851. #endif
  852. }
  853. #elif defined(__riscv_v_intrinsic)
  854. size_t vl = __riscv_vsetvl_e32m4(QK8_0);
  855. for (int i = 0; i < nb; i++) {
  856. // load elements
  857. vfloat32m4_t v_x = __riscv_vle32_v_f32m4(x+i*QK8_0, vl);
  858. vfloat32m4_t vfabs = __riscv_vfabs_v_f32m4(v_x, vl);
  859. vfloat32m1_t tmp = __riscv_vfmv_v_f_f32m1(0.0f, vl);
  860. vfloat32m1_t vmax = __riscv_vfredmax_vs_f32m4_f32m1(vfabs, tmp, vl);
  861. float amax = __riscv_vfmv_f_s_f32m1_f32(vmax);
  862. const float d = amax / ((1 << 7) - 1);
  863. const float id = d ? 1.0f/d : 0.0f;
  864. y[i].d = GGML_FP32_TO_FP16(d);
  865. vfloat32m4_t x0 = __riscv_vfmul_vf_f32m4(v_x, id, vl);
  866. // convert to integer
  867. vint16m2_t vi = __riscv_vfncvt_x_f_w_i16m2(x0, vl);
  868. vint8m1_t vs = __riscv_vncvt_x_x_w_i8m1(vi, vl);
  869. // store result
  870. __riscv_vse8_v_i8m1(y[i].qs , vs, vl);
  871. }
  872. #elif defined(__POWER9_VECTOR__)
  873. for (int i = 0; i < nb; i++) {
  874. vector float srcv [8];
  875. vector float asrcv[8];
  876. vector float amaxv[8];
  877. vector signed int vi[8];
  878. for (int j = 0; j < 8; j++) srcv[j] = vec_xl(0, x + i*32 + 4*j);
  879. for (int j = 0; j < 8; j++) asrcv[j] = vec_abs(srcv[j]);
  880. for (int j = 0; j < 4; j++) amaxv[2*j] = vec_max(asrcv[2*j], asrcv[2*j+1]);
  881. for (int j = 0; j < 2; j++) amaxv[4*j] = vec_max(amaxv[4*j], amaxv[4*j+2]);
  882. for (int j = 0; j < 1; j++) amaxv[8*j] = vec_max(amaxv[8*j], amaxv[8*j+4]);
  883. const float amax = MAX(MAX(vec_extract(amaxv[0], 0),
  884. vec_extract(amaxv[0], 1)),
  885. MAX(vec_extract(amaxv[0], 2),
  886. vec_extract(amaxv[0], 3)));
  887. const float d = amax / ((1 << 7) - 1);
  888. const float id = d ? 1.0f/d : 0.0f;
  889. const vector float vid = vec_splats(id);
  890. y[i].d = GGML_FP32_TO_FP16(d);
  891. for (int j = 0; j < 8; j++) {
  892. const vector float v = vec_round(vec_mul(srcv[j], vid));
  893. vi[j] = vec_cts(v, 0);
  894. }
  895. vec_xst(vec_pack(vec_pack(vi[0], vi[1]), vec_pack(vi[2], vi[3])), 0, &y[i].qs[0]);
  896. vec_xst(vec_pack(vec_pack(vi[4], vi[5]), vec_pack(vi[6], vi[7])), 16, &y[i].qs[0]);
  897. }
  898. #elif defined(__loongarch_asx)
  899. for (int i = 0; i < nb; i++) {
  900. ft_union fi;
  901. __m256 v0 = (__m256)__lasx_xvld( x , 0);
  902. __m256 v1 = (__m256)__lasx_xvld( x , 32);
  903. __m256 v2 = (__m256)__lasx_xvld( x , 64);
  904. __m256 v3 = (__m256)__lasx_xvld( x , 96);
  905. x += 32;
  906. // Compute max(abs(e)) for the block
  907. const __m256 sign_bit = __lasx_xvreplfr2vr_s( -0.0f );
  908. __m256 max_abs = (__m256)__lasx_xvandn_v( (__m256i)sign_bit, (__m256i)v0 );
  909. max_abs = __lasx_xvfmax_s( max_abs, (__m256)__lasx_xvandn_v( (__m256i)sign_bit, (__m256i)v1 ) );
  910. max_abs = __lasx_xvfmax_s( max_abs, (__m256)__lasx_xvandn_v( (__m256i)sign_bit, (__m256i)v2 ) );
  911. max_abs = __lasx_xvfmax_s( max_abs, (__m256)__lasx_xvandn_v( (__m256i)sign_bit, (__m256i)v3 ) );
  912. __m128 max4 = __lsx_vfmax_s( lasx_extractf128( max_abs, 1 ), lasx_extractf128( max_abs , 0) );
  913. max4 = __lsx_vfmax_s( max4, (__m128)__lsx_vpickod_d((__m128i) max4, (__m128i)max4 ) );
  914. __m128 tmp = max4;
  915. max4 = __lsx_vfmax_s( max4, (__m128)__lsx_vinsgr2vr_w(tmp, __lsx_vpickve2gr_w( max4, 1 ), 0 ));
  916. fi.i = __lsx_vpickve2gr_w( (__m128i)max4, 0 );
  917. const float max_scalar = fi.f;
  918. // Quantize these floats
  919. const float d = max_scalar / 127.f;
  920. y[i].d = GGML_FP32_TO_FP16(d);
  921. const float id = ( max_scalar != 0.0f ) ? 127.f / max_scalar : 0.0f;
  922. const __m256 mul = (__m256)__lasx_xvreplfr2vr_s( id );
  923. // Apply the multiplier
  924. v0 = __lasx_xvfmul_s( v0, mul );
  925. v1 = __lasx_xvfmul_s( v1, mul );
  926. v2 = __lasx_xvfmul_s( v2, mul );
  927. v3 = __lasx_xvfmul_s( v3, mul );
  928. // Round to nearest integer
  929. __m256i i0 = __lasx_xvftintrne_w_s( v0 );
  930. __m256i i1 = __lasx_xvftintrne_w_s( v1 );
  931. __m256i i2 = __lasx_xvftintrne_w_s( v2 );
  932. __m256i i3 = __lasx_xvftintrne_w_s( v3 );
  933. __m128i ni0 = lasx_extracti128( i0, 0 );
  934. __m128i ni1 = lasx_extracti128( i0, 1);
  935. __m128i ni2 = lasx_extracti128( i1, 0);
  936. __m128i ni3 = lasx_extracti128( i1, 1);
  937. __m128i ni4 = lasx_extracti128( i2, 0);
  938. __m128i ni5 = lasx_extracti128( i2, 1);
  939. __m128i ni6 = lasx_extracti128( i3, 0);
  940. __m128i ni7 = lasx_extracti128( i3, 1);
  941. // Convert int32 to int16
  942. ni0 = lsx_packs_w( ni0, ni1 );
  943. ni2 = lsx_packs_w( ni2, ni3 );
  944. ni4 = lsx_packs_w( ni4, ni5 );
  945. ni6 = lsx_packs_w( ni6, ni7 );
  946. // Convert int16 to int8
  947. ni0 = lsx_packs_h( ni0, ni2 );
  948. ni4 = lsx_packs_h( ni4, ni6 );
  949. __lsx_vst(ni0, (__m128i *)(y[i].qs + 0), 0);
  950. __lsx_vst(ni4, (__m128i *)(y[i].qs + 16), 0);
  951. }
  952. #else
  953. GGML_UNUSED(nb);
  954. // scalar
  955. quantize_row_q8_0_reference(x, y, k);
  956. #endif
  957. }
  958. // reference implementation for deterministic creation of model files
  959. void quantize_row_q8_1_reference(const float * restrict x, block_q8_1 * restrict y, int64_t k) {
  960. assert(QK8_1 == 32);
  961. assert(k % QK8_1 == 0);
  962. const int nb = k / QK8_1;
  963. for (int i = 0; i < nb; i++) {
  964. float amax = 0.0f; // absolute max
  965. for (int j = 0; j < QK8_1; j++) {
  966. const float v = x[i*QK8_1 + j];
  967. amax = MAX(amax, fabsf(v));
  968. }
  969. const float d = amax / ((1 << 7) - 1);
  970. const float id = d ? 1.0f/d : 0.0f;
  971. y[i].d = GGML_FP32_TO_FP16(d);
  972. int sum = 0;
  973. for (int j = 0; j < QK8_1/2; ++j) {
  974. const float v0 = x[i*QK8_1 + j]*id;
  975. const float v1 = x[i*QK8_1 + QK8_1/2 + j]*id;
  976. y[i].qs[ j] = roundf(v0);
  977. y[i].qs[QK8_1/2 + j] = roundf(v1);
  978. sum += y[i].qs[ j];
  979. sum += y[i].qs[QK8_1/2 + j];
  980. }
  981. y[i].s = GGML_FP32_TO_FP16(sum*d);
  982. }
  983. }
  984. void quantize_row_q8_1(const float * restrict x, void * restrict vy, int64_t k) {
  985. assert(k % QK8_1 == 0);
  986. const int nb = k / QK8_1;
  987. block_q8_1 * restrict y = vy;
  988. #if defined(__ARM_NEON)
  989. for (int i = 0; i < nb; i++) {
  990. float32x4_t srcv [8];
  991. float32x4_t asrcv[8];
  992. float32x4_t amaxv[8];
  993. for (int j = 0; j < 8; j++) srcv[j] = vld1q_f32(x + i*32 + 4*j);
  994. for (int j = 0; j < 8; j++) asrcv[j] = vabsq_f32(srcv[j]);
  995. for (int j = 0; j < 4; j++) amaxv[2*j] = vmaxq_f32(asrcv[2*j], asrcv[2*j+1]);
  996. for (int j = 0; j < 2; j++) amaxv[4*j] = vmaxq_f32(amaxv[4*j], amaxv[4*j+2]);
  997. for (int j = 0; j < 1; j++) amaxv[8*j] = vmaxq_f32(amaxv[8*j], amaxv[8*j+4]);
  998. const float amax = vmaxvq_f32(amaxv[0]);
  999. const float d = amax / ((1 << 7) - 1);
  1000. const float id = d ? 1.0f/d : 0.0f;
  1001. y[i].d = GGML_FP32_TO_FP16(d);
  1002. int32x4_t accv = vdupq_n_s32(0);
  1003. for (int j = 0; j < 8; j++) {
  1004. const float32x4_t v = vmulq_n_f32(srcv[j], id);
  1005. const int32x4_t vi = vcvtnq_s32_f32(v);
  1006. y[i].qs[4*j + 0] = vgetq_lane_s32(vi, 0);
  1007. y[i].qs[4*j + 1] = vgetq_lane_s32(vi, 1);
  1008. y[i].qs[4*j + 2] = vgetq_lane_s32(vi, 2);
  1009. y[i].qs[4*j + 3] = vgetq_lane_s32(vi, 3);
  1010. accv = vaddq_s32(accv, vi);
  1011. }
  1012. y[i].s = GGML_FP32_TO_FP16(d * vaddvq_s32(accv));
  1013. }
  1014. #elif defined(__wasm_simd128__)
  1015. for (int i = 0; i < nb; i++) {
  1016. v128_t srcv [8];
  1017. v128_t asrcv[8];
  1018. v128_t amaxv[8];
  1019. for (int j = 0; j < 8; j++) srcv[j] = wasm_v128_load(x + i*32 + 4*j);
  1020. for (int j = 0; j < 8; j++) asrcv[j] = wasm_f32x4_abs(srcv[j]);
  1021. for (int j = 0; j < 4; j++) amaxv[2*j] = wasm_f32x4_max(asrcv[2*j], asrcv[2*j+1]);
  1022. for (int j = 0; j < 2; j++) amaxv[4*j] = wasm_f32x4_max(amaxv[4*j], amaxv[4*j+2]);
  1023. for (int j = 0; j < 1; j++) amaxv[8*j] = wasm_f32x4_max(amaxv[8*j], amaxv[8*j+4]);
  1024. const float amax = MAX(MAX(wasm_f32x4_extract_lane(amaxv[0], 0),
  1025. wasm_f32x4_extract_lane(amaxv[0], 1)),
  1026. MAX(wasm_f32x4_extract_lane(amaxv[0], 2),
  1027. wasm_f32x4_extract_lane(amaxv[0], 3)));
  1028. const float d = amax / ((1 << 7) - 1);
  1029. const float id = d ? 1.0f/d : 0.0f;
  1030. y[i].d = GGML_FP32_TO_FP16(d);
  1031. v128_t accv = wasm_i32x4_splat(0);
  1032. for (int j = 0; j < 8; j++) {
  1033. const v128_t v = wasm_f32x4_mul(srcv[j], wasm_f32x4_splat(id));
  1034. const v128_t vi = wasm_i32x4_trunc_sat_f32x4(v);
  1035. y[i].qs[4*j + 0] = wasm_i32x4_extract_lane(vi, 0);
  1036. y[i].qs[4*j + 1] = wasm_i32x4_extract_lane(vi, 1);
  1037. y[i].qs[4*j + 2] = wasm_i32x4_extract_lane(vi, 2);
  1038. y[i].qs[4*j + 3] = wasm_i32x4_extract_lane(vi, 3);
  1039. accv = wasm_i32x4_add(accv, vi);
  1040. }
  1041. y[i].s = GGML_FP32_TO_FP16(
  1042. d * (wasm_i32x4_extract_lane(accv, 0) +
  1043. wasm_i32x4_extract_lane(accv, 1) +
  1044. wasm_i32x4_extract_lane(accv, 2) +
  1045. wasm_i32x4_extract_lane(accv, 3)));
  1046. }
  1047. #elif defined(__AVX2__) || defined(__AVX__)
  1048. for (int i = 0; i < nb; i++) {
  1049. // Load elements into 4 AVX vectors
  1050. __m256 v0 = _mm256_loadu_ps( x );
  1051. __m256 v1 = _mm256_loadu_ps( x + 8 );
  1052. __m256 v2 = _mm256_loadu_ps( x + 16 );
  1053. __m256 v3 = _mm256_loadu_ps( x + 24 );
  1054. x += 32;
  1055. // Compute max(abs(e)) for the block
  1056. const __m256 signBit = _mm256_set1_ps( -0.0f );
  1057. __m256 maxAbs = _mm256_andnot_ps( signBit, v0 );
  1058. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v1 ) );
  1059. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v2 ) );
  1060. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v3 ) );
  1061. __m128 max4 = _mm_max_ps( _mm256_extractf128_ps( maxAbs, 1 ), _mm256_castps256_ps128( maxAbs ) );
  1062. max4 = _mm_max_ps( max4, _mm_movehl_ps( max4, max4 ) );
  1063. max4 = _mm_max_ss( max4, _mm_movehdup_ps( max4 ) );
  1064. const float max_scalar = _mm_cvtss_f32( max4 );
  1065. // Quantize these floats
  1066. const float d = max_scalar / 127.f;
  1067. y[i].d = GGML_FP32_TO_FP16(d);
  1068. const float id = ( max_scalar != 0.0f ) ? 127.f / max_scalar : 0.0f;
  1069. const __m256 mul = _mm256_set1_ps( id );
  1070. // Apply the multiplier
  1071. v0 = _mm256_mul_ps( v0, mul );
  1072. v1 = _mm256_mul_ps( v1, mul );
  1073. v2 = _mm256_mul_ps( v2, mul );
  1074. v3 = _mm256_mul_ps( v3, mul );
  1075. // Round to nearest integer
  1076. v0 = _mm256_round_ps( v0, _MM_ROUND_NEAREST );
  1077. v1 = _mm256_round_ps( v1, _MM_ROUND_NEAREST );
  1078. v2 = _mm256_round_ps( v2, _MM_ROUND_NEAREST );
  1079. v3 = _mm256_round_ps( v3, _MM_ROUND_NEAREST );
  1080. // Convert floats to integers
  1081. __m256i i0 = _mm256_cvtps_epi32( v0 );
  1082. __m256i i1 = _mm256_cvtps_epi32( v1 );
  1083. __m256i i2 = _mm256_cvtps_epi32( v2 );
  1084. __m256i i3 = _mm256_cvtps_epi32( v3 );
  1085. #if defined(__AVX2__)
  1086. // Compute the sum of the quants and set y[i].s
  1087. y[i].s = GGML_FP32_TO_FP16(d * hsum_i32_8(_mm256_add_epi32(_mm256_add_epi32(i0, i1), _mm256_add_epi32(i2, i3))));
  1088. // Convert int32 to int16
  1089. i0 = _mm256_packs_epi32( i0, i1 ); // 0, 1, 2, 3, 8, 9, 10, 11, 4, 5, 6, 7, 12, 13, 14, 15
  1090. i2 = _mm256_packs_epi32( i2, i3 ); // 16, 17, 18, 19, 24, 25, 26, 27, 20, 21, 22, 23, 28, 29, 30, 31
  1091. // Convert int16 to int8
  1092. i0 = _mm256_packs_epi16( i0, i2 ); // 0, 1, 2, 3, 8, 9, 10, 11, 16, 17, 18, 19, 24, 25, 26, 27, 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31
  1093. // We got our precious signed bytes, but the order is now wrong
  1094. // These AVX2 pack instructions process 16-byte pieces independently
  1095. // The following instruction is fixing the order
  1096. const __m256i perm = _mm256_setr_epi32( 0, 4, 1, 5, 2, 6, 3, 7 );
  1097. i0 = _mm256_permutevar8x32_epi32( i0, perm );
  1098. _mm256_storeu_si256((__m256i *)y[i].qs, i0);
  1099. #else
  1100. // Since we don't have in AVX some necessary functions,
  1101. // we split the registers in half and call AVX2 analogs from SSE
  1102. __m128i ni0 = _mm256_castsi256_si128( i0 );
  1103. __m128i ni1 = _mm256_extractf128_si256( i0, 1);
  1104. __m128i ni2 = _mm256_castsi256_si128( i1 );
  1105. __m128i ni3 = _mm256_extractf128_si256( i1, 1);
  1106. __m128i ni4 = _mm256_castsi256_si128( i2 );
  1107. __m128i ni5 = _mm256_extractf128_si256( i2, 1);
  1108. __m128i ni6 = _mm256_castsi256_si128( i3 );
  1109. __m128i ni7 = _mm256_extractf128_si256( i3, 1);
  1110. // Compute the sum of the quants and set y[i].s
  1111. const __m128i s0 = _mm_add_epi32(_mm_add_epi32(ni0, ni1), _mm_add_epi32(ni2, ni3));
  1112. const __m128i s1 = _mm_add_epi32(_mm_add_epi32(ni4, ni5), _mm_add_epi32(ni6, ni7));
  1113. y[i].s = GGML_FP32_TO_FP16(d * hsum_i32_4(_mm_add_epi32(s0, s1)));
  1114. // Convert int32 to int16
  1115. ni0 = _mm_packs_epi32( ni0, ni1 );
  1116. ni2 = _mm_packs_epi32( ni2, ni3 );
  1117. ni4 = _mm_packs_epi32( ni4, ni5 );
  1118. ni6 = _mm_packs_epi32( ni6, ni7 );
  1119. // Convert int16 to int8
  1120. ni0 = _mm_packs_epi16( ni0, ni2 );
  1121. ni4 = _mm_packs_epi16( ni4, ni6 );
  1122. _mm_storeu_si128((__m128i *)(y[i].qs + 0), ni0);
  1123. _mm_storeu_si128((__m128i *)(y[i].qs + 16), ni4);
  1124. #endif
  1125. }
  1126. #elif defined(__riscv_v_intrinsic)
  1127. size_t vl = __riscv_vsetvl_e32m4(QK8_1);
  1128. for (int i = 0; i < nb; i++) {
  1129. // load elements
  1130. vfloat32m4_t v_x = __riscv_vle32_v_f32m4(x+i*QK8_1, vl);
  1131. vfloat32m4_t vfabs = __riscv_vfabs_v_f32m4(v_x, vl);
  1132. vfloat32m1_t tmp = __riscv_vfmv_v_f_f32m1(0.0, vl);
  1133. vfloat32m1_t vmax = __riscv_vfredmax_vs_f32m4_f32m1(vfabs, tmp, vl);
  1134. float amax = __riscv_vfmv_f_s_f32m1_f32(vmax);
  1135. const float d = amax / ((1 << 7) - 1);
  1136. const float id = d ? 1.0f/d : 0.0f;
  1137. y[i].d = GGML_FP32_TO_FP16(d);
  1138. vfloat32m4_t x0 = __riscv_vfmul_vf_f32m4(v_x, id, vl);
  1139. // convert to integer
  1140. vint16m2_t vi = __riscv_vfncvt_x_f_w_i16m2(x0, vl);
  1141. vint8m1_t vs = __riscv_vncvt_x_x_w_i8m1(vi, vl);
  1142. // store result
  1143. __riscv_vse8_v_i8m1(y[i].qs , vs, vl);
  1144. // compute sum for y[i].s
  1145. vint16m1_t tmp2 = __riscv_vmv_v_x_i16m1(0, vl);
  1146. vint16m1_t vwrs = __riscv_vwredsum_vs_i8m1_i16m1(vs, tmp2, vl);
  1147. // set y[i].s
  1148. int sum = __riscv_vmv_x_s_i16m1_i16(vwrs);
  1149. y[i].s = GGML_FP32_TO_FP16(sum*d);
  1150. }
  1151. #elif defined(__POWER9_VECTOR__)
  1152. for (int i = 0; i < nb; i++) {
  1153. vector float srcv [8];
  1154. vector float asrcv[8];
  1155. vector float amaxv[8];
  1156. vector signed int vi[8];
  1157. for (int j = 0; j < 8; j++) srcv[j] = vec_xl(0, x + i*32 + 4*j);
  1158. for (int j = 0; j < 8; j++) asrcv[j] = vec_abs(srcv[j]);
  1159. for (int j = 0; j < 4; j++) amaxv[2*j] = vec_max(asrcv[2*j], asrcv[2*j+1]);
  1160. for (int j = 0; j < 2; j++) amaxv[4*j] = vec_max(amaxv[4*j], amaxv[4*j+2]);
  1161. for (int j = 0; j < 1; j++) amaxv[8*j] = vec_max(amaxv[8*j], amaxv[8*j+4]);
  1162. const float amax = MAX(MAX(vec_extract(amaxv[0], 0),
  1163. vec_extract(amaxv[0], 1)),
  1164. MAX(vec_extract(amaxv[0], 2),
  1165. vec_extract(amaxv[0], 3)));
  1166. const float d = amax / ((1 << 7) - 1);
  1167. const float id = d ? 1.0f/d : 0.0f;
  1168. const vector float vid = vec_splats(id);
  1169. y[i].d = GGML_FP32_TO_FP16(d);
  1170. vector int accv = vec_splats(0);
  1171. for (int j = 0; j < 8; j++) {
  1172. const vector float v = vec_round(vec_mul(srcv[j], vid));
  1173. vi[j] = vec_cts(v, 0);
  1174. accv = vec_add(accv, vi[j]);
  1175. }
  1176. vec_xst(vec_pack(vec_pack(vi[0], vi[1]), vec_pack(vi[2], vi[3])), 0, &y[i].qs[0]);
  1177. vec_xst(vec_pack(vec_pack(vi[4], vi[5]), vec_pack(vi[6], vi[7])), 16, &y[i].qs[0]);
  1178. accv = vec_add(accv, vec_sld(accv, accv, 4));
  1179. accv = vec_add(accv, vec_sld(accv, accv, 8));
  1180. y[i].s = GGML_FP32_TO_FP16(d * vec_extract(accv, 0));
  1181. }
  1182. #elif defined(__loongarch_asx)
  1183. for (int i = 0; i < nb; i++) {
  1184. ft_union ft;
  1185. __m256 v0 = (__m256)__lasx_xvld( x , 0 );
  1186. __m256 v1 = (__m256)__lasx_xvld( x , 32 );
  1187. __m256 v2 = (__m256)__lasx_xvld( x , 64 );
  1188. __m256 v3 = (__m256)__lasx_xvld( x , 96 );
  1189. x += 32;
  1190. // Compute max(abs(e)) for the block
  1191. const __m256 sign_bit = __lasx_xvreplfr2vr_s( -0.0f );
  1192. __m256 max_abs = (__m256)__lasx_xvandn_v( (__m256i)sign_bit, (__m256i)v0 );
  1193. max_abs = __lasx_xvfmax_s( max_abs, (__m256)__lasx_xvandn_v( (__m256i)sign_bit, (__m256i)v1 ) );
  1194. max_abs = __lasx_xvfmax_s( max_abs, (__m256)__lasx_xvandn_v( (__m256i)sign_bit, (__m256i)v2 ) );
  1195. max_abs = __lasx_xvfmax_s( max_abs, (__m256)__lasx_xvandn_v( (__m256i)sign_bit, (__m256i)v3 ) );
  1196. __m128 max4 = __lsx_vfmax_s( lasx_extractf128( max_abs, 1 ), lasx_extractf128( max_abs, 0) );
  1197. max4 = __lsx_vfmax_s( max4, (__m128)__lsx_vpickod_d((__m128i) max4, (__m128i)max4 ) );
  1198. __m128 tmp = max4;
  1199. max4 = __lsx_vfmax_s( max4, (__m128)__lsx_vextrins_w((__m128i)tmp, (__m128i)max4, 0x10 ));
  1200. ft.i = __lsx_vpickve2gr_w( (__m128i)max4, 0 );
  1201. const float max_scalar = ft.f;
  1202. // Quantize these floats
  1203. const float d = max_scalar / 127.f;
  1204. y[i].d = GGML_FP32_TO_FP16(d);
  1205. const float id = ( max_scalar != 0.0f ) ? 127.f / max_scalar : 0.0f;
  1206. const __m256 mul = __lasx_xvreplfr2vr_s( id );
  1207. // Apply the multiplier
  1208. v0 = __lasx_xvfmul_s( v0, mul );
  1209. v1 = __lasx_xvfmul_s( v1, mul );
  1210. v2 = __lasx_xvfmul_s( v2, mul );
  1211. v3 = __lasx_xvfmul_s( v3, mul );
  1212. // Round to nearest integer
  1213. __m256i i0 = __lasx_xvftintrne_w_s( v0 );
  1214. __m256i i1 = __lasx_xvftintrne_w_s( v1 );
  1215. __m256i i2 = __lasx_xvftintrne_w_s( v2 );
  1216. __m256i i3 = __lasx_xvftintrne_w_s( v3 );
  1217. __m128i ni0 = lasx_extracti128(i0, 0);
  1218. __m128i ni1 = lasx_extracti128( i0, 1);
  1219. __m128i ni2 = lasx_extracti128( i1, 0);
  1220. __m128i ni3 = lasx_extracti128( i1, 1);
  1221. __m128i ni4 = lasx_extracti128( i2, 0 );
  1222. __m128i ni5 = lasx_extracti128( i2, 1);
  1223. __m128i ni6 = lasx_extracti128( i3, 0);
  1224. __m128i ni7 = lasx_extracti128( i3, 1);
  1225. // Compute the sum of the quants and set y[i].s
  1226. const __m128i s0 = __lsx_vadd_w(__lsx_vadd_w(ni0, ni1), __lsx_vadd_w(ni2, ni3));
  1227. const __m128i s1 = __lsx_vadd_w(__lsx_vadd_w(ni4, ni5), __lsx_vadd_w(ni6, ni7));
  1228. y[i].s = GGML_FP32_TO_FP16(d * hsum_i32_4(__lsx_vadd_w(s0, s1)));
  1229. // Convert int32 to int16
  1230. ni0 = lsx_packs_w( ni0, ni1 );
  1231. ni2 = lsx_packs_w( ni2, ni3 );
  1232. ni4 = lsx_packs_w( ni4, ni5 );
  1233. ni6 = lsx_packs_w( ni6, ni7 );
  1234. // Convert int16 to int8
  1235. ni0 = lsx_packs_h( ni0, ni2 );
  1236. ni4 = lsx_packs_h( ni4, ni6 );
  1237. __lsx_vst(ni0, (__m128i *)(y[i].qs + 0), 0);
  1238. __lsx_vst(ni4, (__m128i *)(y[i].qs + 16), 0);
  1239. }
  1240. #else
  1241. GGML_UNUSED(nb);
  1242. // scalar
  1243. quantize_row_q8_1_reference(x, y, k);
  1244. #endif
  1245. }
  1246. void dequantize_row_q4_0(const block_q4_0 * restrict x, float * restrict y, int64_t k) {
  1247. static const int qk = QK4_0;
  1248. assert(k % qk == 0);
  1249. const int nb = k / qk;
  1250. for (int i = 0; i < nb; i++) {
  1251. const float d = GGML_FP16_TO_FP32(x[i].d);
  1252. for (int j = 0; j < qk/2; ++j) {
  1253. const int x0 = (x[i].qs[j] & 0x0F) - 8;
  1254. const int x1 = (x[i].qs[j] >> 4) - 8;
  1255. y[i*qk + j + 0 ] = x0*d;
  1256. y[i*qk + j + qk/2] = x1*d;
  1257. }
  1258. }
  1259. }
  1260. void dequantize_row_q4_1(const block_q4_1 * restrict x, float * restrict y, int64_t k) {
  1261. static const int qk = QK4_1;
  1262. assert(k % qk == 0);
  1263. const int nb = k / qk;
  1264. for (int i = 0; i < nb; i++) {
  1265. const float d = GGML_FP16_TO_FP32(x[i].d);
  1266. const float m = GGML_FP16_TO_FP32(x[i].m);
  1267. for (int j = 0; j < qk/2; ++j) {
  1268. const int x0 = (x[i].qs[j] & 0x0F);
  1269. const int x1 = (x[i].qs[j] >> 4);
  1270. y[i*qk + j + 0 ] = x0*d + m;
  1271. y[i*qk + j + qk/2] = x1*d + m;
  1272. }
  1273. }
  1274. }
  1275. void dequantize_row_q5_0(const block_q5_0 * restrict x, float * restrict y, int64_t k) {
  1276. static const int qk = QK5_0;
  1277. assert(k % qk == 0);
  1278. const int nb = k / qk;
  1279. for (int i = 0; i < nb; i++) {
  1280. const float d = GGML_FP16_TO_FP32(x[i].d);
  1281. uint32_t qh;
  1282. memcpy(&qh, x[i].qh, sizeof(qh));
  1283. for (int j = 0; j < qk/2; ++j) {
  1284. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  1285. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  1286. const int32_t x0 = ((x[i].qs[j] & 0x0F) | xh_0) - 16;
  1287. const int32_t x1 = ((x[i].qs[j] >> 4) | xh_1) - 16;
  1288. y[i*qk + j + 0 ] = x0*d;
  1289. y[i*qk + j + qk/2] = x1*d;
  1290. }
  1291. }
  1292. }
  1293. void dequantize_row_q5_1(const block_q5_1 * restrict x, float * restrict y, int64_t k) {
  1294. static const int qk = QK5_1;
  1295. assert(k % qk == 0);
  1296. const int nb = k / qk;
  1297. for (int i = 0; i < nb; i++) {
  1298. const float d = GGML_FP16_TO_FP32(x[i].d);
  1299. const float m = GGML_FP16_TO_FP32(x[i].m);
  1300. uint32_t qh;
  1301. memcpy(&qh, x[i].qh, sizeof(qh));
  1302. for (int j = 0; j < qk/2; ++j) {
  1303. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  1304. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  1305. const int x0 = (x[i].qs[j] & 0x0F) | xh_0;
  1306. const int x1 = (x[i].qs[j] >> 4) | xh_1;
  1307. y[i*qk + j + 0 ] = x0*d + m;
  1308. y[i*qk + j + qk/2] = x1*d + m;
  1309. }
  1310. }
  1311. }
  1312. void dequantize_row_q8_0(const block_q8_0 * restrict x, float * restrict y, int64_t k) {
  1313. static const int qk = QK8_0;
  1314. assert(k % qk == 0);
  1315. const int nb = k / qk;
  1316. for (int i = 0; i < nb; i++) {
  1317. const float d = GGML_FP16_TO_FP32(x[i].d);
  1318. for (int j = 0; j < qk; ++j) {
  1319. y[i*qk + j] = x[i].qs[j]*d;
  1320. }
  1321. }
  1322. }
  1323. //
  1324. // 2-6 bit quantization in super-blocks
  1325. //
  1326. //
  1327. // ===================== Helper functions
  1328. //
  1329. static inline int nearest_int(float fval) {
  1330. assert(fval <= 4194303.f);
  1331. float val = fval + 12582912.f;
  1332. int i; memcpy(&i, &val, sizeof(int));
  1333. return (i & 0x007fffff) - 0x00400000;
  1334. }
  1335. static float make_qx_quants(int n, int nmax, const float * restrict x, int8_t * restrict L, int rmse_type,
  1336. const float * restrict qw) {
  1337. float max = 0;
  1338. float amax = 0;
  1339. for (int i = 0; i < n; ++i) {
  1340. float ax = fabsf(x[i]);
  1341. if (ax > amax) { amax = ax; max = x[i]; }
  1342. }
  1343. if (amax < GROUP_MAX_EPS) { // all zero
  1344. for (int i = 0; i < n; ++i) {
  1345. L[i] = 0;
  1346. }
  1347. return 0.f;
  1348. }
  1349. float iscale = -nmax / max;
  1350. if (rmse_type == 0) {
  1351. for (int i = 0; i < n; ++i) {
  1352. int l = nearest_int(iscale * x[i]);
  1353. L[i] = nmax + MAX(-nmax, MIN(nmax-1, l));
  1354. }
  1355. return 1/iscale;
  1356. }
  1357. bool return_early = false;
  1358. if (rmse_type < 0) {
  1359. rmse_type = -rmse_type;
  1360. return_early = true;
  1361. }
  1362. float sumlx = 0;
  1363. float suml2 = 0;
  1364. #ifdef HAVE_BUGGY_APPLE_LINKER
  1365. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  1366. for (volatile int i = 0; i < n; ++i) {
  1367. #else
  1368. for (int i = 0; i < n; ++i) {
  1369. #endif
  1370. int l = nearest_int(iscale * x[i]);
  1371. l = MAX(-nmax, MIN(nmax-1, l));
  1372. L[i] = l + nmax;
  1373. float w = qw ? qw[i] : rmse_type == 1 ? x[i] * x[i] : rmse_type == 2 ? 1 : rmse_type == 3 ? fabsf(x[i]) : sqrtf(fabsf(x[i]));
  1374. sumlx += w*x[i]*l;
  1375. suml2 += w*l*l;
  1376. }
  1377. float scale = suml2 ? sumlx/suml2 : 0.0f;
  1378. if (return_early) return suml2 > 0 ? 0.5f*(scale + 1/iscale) : 1/iscale;
  1379. float best = scale * sumlx;
  1380. for (int is = -9; is <= 9; ++is) {
  1381. if (is == 0) {
  1382. continue;
  1383. }
  1384. iscale = -(nmax + 0.1f*is) / max;
  1385. sumlx = suml2 = 0;
  1386. for (int i = 0; i < n; ++i) {
  1387. int l = nearest_int(iscale * x[i]);
  1388. l = MAX(-nmax, MIN(nmax-1, l));
  1389. float w = qw ? qw[i] : rmse_type == 1 ? x[i] * x[i] : rmse_type == 2 ? 1 : rmse_type == 3 ? fabsf(x[i]) : sqrtf(fabsf(x[i]));
  1390. sumlx += w*x[i]*l;
  1391. suml2 += w*l*l;
  1392. }
  1393. if (suml2 > 0 && sumlx*sumlx > best*suml2) {
  1394. for (int i = 0; i < n; ++i) {
  1395. int l = nearest_int(iscale * x[i]);
  1396. L[i] = nmax + MAX(-nmax, MIN(nmax-1, l));
  1397. }
  1398. scale = sumlx/suml2; best = scale*sumlx;
  1399. }
  1400. }
  1401. return scale;
  1402. }
  1403. static float make_q3_quants(int n, int nmax, const float * restrict x, int8_t * restrict L, bool do_rmse) {
  1404. float max = 0;
  1405. float amax = 0;
  1406. for (int i = 0; i < n; ++i) {
  1407. float ax = fabsf(x[i]);
  1408. if (ax > amax) { amax = ax; max = x[i]; }
  1409. }
  1410. if (amax < GROUP_MAX_EPS) { // all zero
  1411. for (int i = 0; i < n; ++i) { L[i] = 0; }
  1412. return 0.f;
  1413. }
  1414. float iscale = -nmax / max;
  1415. if (do_rmse) {
  1416. float sumlx = 0;
  1417. float suml2 = 0;
  1418. for (int i = 0; i < n; ++i) {
  1419. int l = nearest_int(iscale * x[i]);
  1420. l = MAX(-nmax, MIN(nmax-1, l));
  1421. L[i] = l;
  1422. float w = x[i]*x[i];
  1423. sumlx += w*x[i]*l;
  1424. suml2 += w*l*l;
  1425. }
  1426. for (int itry = 0; itry < 5; ++itry) {
  1427. int n_changed = 0;
  1428. for (int i = 0; i < n; ++i) {
  1429. float w = x[i]*x[i];
  1430. float slx = sumlx - w*x[i]*L[i];
  1431. if (slx > 0) {
  1432. float sl2 = suml2 - w*L[i]*L[i];
  1433. int new_l = nearest_int(x[i] * sl2 / slx);
  1434. new_l = MAX(-nmax, MIN(nmax-1, new_l));
  1435. if (new_l != L[i]) {
  1436. slx += w*x[i]*new_l;
  1437. sl2 += w*new_l*new_l;
  1438. if (sl2 > 0 && slx*slx*suml2 > sumlx*sumlx*sl2) {
  1439. L[i] = new_l; sumlx = slx; suml2 = sl2;
  1440. ++n_changed;
  1441. }
  1442. }
  1443. }
  1444. }
  1445. if (!n_changed) {
  1446. break;
  1447. }
  1448. }
  1449. for (int i = 0; i < n; ++i) {
  1450. L[i] += nmax;
  1451. }
  1452. return sumlx / suml2;
  1453. }
  1454. for (int i = 0; i < n; ++i) {
  1455. int l = nearest_int(iscale * x[i]);
  1456. l = MAX(-nmax, MIN(nmax-1, l));
  1457. L[i] = l + nmax;
  1458. }
  1459. return 1/iscale;
  1460. }
  1461. static float make_qkx1_quants(int n, int nmax, const float * restrict x, uint8_t * restrict L, float * restrict the_min,
  1462. int ntry, float alpha) {
  1463. float min = x[0];
  1464. float max = x[0];
  1465. for (int i = 1; i < n; ++i) {
  1466. if (x[i] < min) min = x[i];
  1467. if (x[i] > max) max = x[i];
  1468. }
  1469. if (max == min) {
  1470. for (int i = 0; i < n; ++i) L[i] = 0;
  1471. *the_min = 0;
  1472. return 0.f;
  1473. }
  1474. if (min > 0) min = 0;
  1475. float iscale = nmax/(max - min);
  1476. float scale = 1/iscale;
  1477. for (int itry = 0; itry < ntry; ++itry) {
  1478. float sumlx = 0; int suml2 = 0;
  1479. bool did_change = false;
  1480. for (int i = 0; i < n; ++i) {
  1481. int l = nearest_int(iscale*(x[i] - min));
  1482. l = MAX(0, MIN(nmax, l));
  1483. if (l != L[i]) {
  1484. L[i] = l;
  1485. did_change = true;
  1486. }
  1487. sumlx += (x[i] - min)*l;
  1488. suml2 += l*l;
  1489. }
  1490. scale = sumlx/suml2;
  1491. float sum = 0;
  1492. for (int i = 0; i < n; ++i) {
  1493. sum += x[i] - scale*L[i];
  1494. }
  1495. min = alpha*min + (1 - alpha)*sum/n;
  1496. if (min > 0) min = 0;
  1497. iscale = 1/scale;
  1498. if (!did_change) break;
  1499. }
  1500. *the_min = -min;
  1501. return scale;
  1502. }
  1503. static float make_qkx2_quants(int n, int nmax, const float * restrict x, const float * restrict weights,
  1504. uint8_t * restrict L, float * restrict the_min, uint8_t * restrict Laux,
  1505. float rmin, float rdelta, int nstep, bool use_mad) {
  1506. float min = x[0];
  1507. float max = x[0];
  1508. float sum_w = weights[0];
  1509. float sum_x = sum_w * x[0];
  1510. #ifdef HAVE_BUGGY_APPLE_LINKER
  1511. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  1512. for (volatile int i = 1; i < n; ++i) {
  1513. #else
  1514. for (int i = 1; i < n; ++i) {
  1515. #endif
  1516. if (x[i] < min) min = x[i];
  1517. if (x[i] > max) max = x[i];
  1518. float w = weights[i];
  1519. sum_w += w;
  1520. sum_x += w * x[i];
  1521. }
  1522. if (min > 0) min = 0;
  1523. if (max == min) {
  1524. for (int i = 0; i < n; ++i) L[i] = 0;
  1525. *the_min = -min;
  1526. return 0.f;
  1527. }
  1528. float iscale = nmax/(max - min);
  1529. float scale = 1/iscale;
  1530. float best_mad = 0;
  1531. for (int i = 0; i < n; ++i) {
  1532. int l = nearest_int(iscale*(x[i] - min));
  1533. L[i] = MAX(0, MIN(nmax, l));
  1534. float diff = scale * L[i] + min - x[i];
  1535. diff = use_mad ? fabsf(diff) : diff * diff;
  1536. float w = weights[i];
  1537. best_mad += w * diff;
  1538. }
  1539. if (nstep < 1) {
  1540. *the_min = -min;
  1541. return scale;
  1542. }
  1543. for (int is = 0; is <= nstep; ++is) {
  1544. iscale = (rmin + rdelta*is + nmax)/(max - min);
  1545. float sum_l = 0, sum_l2 = 0, sum_xl = 0;
  1546. for (int i = 0; i < n; ++i) {
  1547. int l = nearest_int(iscale*(x[i] - min));
  1548. l = MAX(0, MIN(nmax, l));
  1549. Laux[i] = l;
  1550. float w = weights[i];
  1551. sum_l += w*l;
  1552. sum_l2 += w*l*l;
  1553. sum_xl += w*l*x[i];
  1554. }
  1555. float D = sum_w * sum_l2 - sum_l * sum_l;
  1556. if (D > 0) {
  1557. float this_scale = (sum_w * sum_xl - sum_x * sum_l)/D;
  1558. float this_min = (sum_l2 * sum_x - sum_l * sum_xl)/D;
  1559. if (this_min > 0) {
  1560. this_min = 0;
  1561. this_scale = sum_xl / sum_l2;
  1562. }
  1563. float mad = 0;
  1564. for (int i = 0; i < n; ++i) {
  1565. float diff = this_scale * Laux[i] + this_min - x[i];
  1566. diff = use_mad ? fabsf(diff) : diff * diff;
  1567. float w = weights[i];
  1568. mad += w * diff;
  1569. }
  1570. if (mad < best_mad) {
  1571. for (int i = 0; i < n; ++i) {
  1572. L[i] = Laux[i];
  1573. }
  1574. best_mad = mad;
  1575. scale = this_scale;
  1576. min = this_min;
  1577. }
  1578. }
  1579. }
  1580. *the_min = -min;
  1581. return scale;
  1582. }
  1583. static inline void get_scale_min_k4(int j, const uint8_t * restrict q, uint8_t * restrict d, uint8_t * restrict m) {
  1584. if (j < 4) {
  1585. *d = q[j] & 63; *m = q[j + 4] & 63;
  1586. } else {
  1587. *d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  1588. *m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  1589. }
  1590. }
  1591. //========================- 2-bit (de)-quantization
  1592. void quantize_row_q2_K_reference(const float * restrict x, block_q2_K * restrict y, int64_t k) {
  1593. assert(k % QK_K == 0);
  1594. const int nb = k / QK_K;
  1595. uint8_t L[QK_K];
  1596. uint8_t Laux[16];
  1597. float weights[16];
  1598. float mins[QK_K/16];
  1599. float scales[QK_K/16];
  1600. const float q4scale = 15.f;
  1601. for (int i = 0; i < nb; i++) {
  1602. float max_scale = 0; // as we are deducting the min, scales are always positive
  1603. float max_min = 0;
  1604. for (int j = 0; j < QK_K/16; ++j) {
  1605. for (int l = 0; l < 16; ++l) weights[l] = fabsf(x[16*j + l]);
  1606. scales[j] = make_qkx2_quants(16, 3, x + 16*j, weights, L + 16*j, &mins[j], Laux, -0.5f, 0.1f, 15, true);
  1607. float scale = scales[j];
  1608. if (scale > max_scale) {
  1609. max_scale = scale;
  1610. }
  1611. float min = mins[j];
  1612. if (min > max_min) {
  1613. max_min = min;
  1614. }
  1615. }
  1616. if (max_scale > 0) {
  1617. float iscale = q4scale/max_scale;
  1618. for (int j = 0; j < QK_K/16; ++j) {
  1619. int l = nearest_int(iscale*scales[j]);
  1620. y[i].scales[j] = l;
  1621. }
  1622. y[i].d = GGML_FP32_TO_FP16(max_scale/q4scale);
  1623. } else {
  1624. for (int j = 0; j < QK_K/16; ++j) y[i].scales[j] = 0;
  1625. y[i].d = GGML_FP32_TO_FP16(0.f);
  1626. }
  1627. if (max_min > 0) {
  1628. float iscale = q4scale/max_min;
  1629. for (int j = 0; j < QK_K/16; ++j) {
  1630. int l = nearest_int(iscale*mins[j]);
  1631. y[i].scales[j] |= (l << 4);
  1632. }
  1633. y[i].dmin = GGML_FP32_TO_FP16(max_min/q4scale);
  1634. } else {
  1635. y[i].dmin = GGML_FP32_TO_FP16(0.f);
  1636. }
  1637. for (int j = 0; j < QK_K/16; ++j) {
  1638. const float d = GGML_FP16_TO_FP32(y[i].d) * (y[i].scales[j] & 0xF);
  1639. if (!d) continue;
  1640. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * (y[i].scales[j] >> 4);
  1641. for (int ii = 0; ii < 16; ++ii) {
  1642. int l = nearest_int((x[16*j + ii] + dm)/d);
  1643. l = MAX(0, MIN(3, l));
  1644. L[16*j + ii] = l;
  1645. }
  1646. }
  1647. for (int j = 0; j < QK_K; j += 128) {
  1648. for (int l = 0; l < 32; ++l) {
  1649. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1650. }
  1651. }
  1652. x += QK_K;
  1653. }
  1654. }
  1655. void dequantize_row_q2_K(const block_q2_K * restrict x, float * restrict y, int64_t k) {
  1656. assert(k % QK_K == 0);
  1657. const int nb = k / QK_K;
  1658. for (int i = 0; i < nb; i++) {
  1659. const float d = GGML_FP16_TO_FP32(x[i].d);
  1660. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  1661. const uint8_t * q = x[i].qs;
  1662. int is = 0;
  1663. float dl, ml;
  1664. for (int n = 0; n < QK_K; n += 128) {
  1665. int shift = 0;
  1666. for (int j = 0; j < 4; ++j) {
  1667. uint8_t sc = x[i].scales[is++];
  1668. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  1669. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l] >> shift) & 3)) - ml;
  1670. sc = x[i].scales[is++];
  1671. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  1672. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3)) - ml;
  1673. shift += 2;
  1674. }
  1675. q += 32;
  1676. }
  1677. }
  1678. }
  1679. void quantize_row_q2_K(const float * restrict x, void * restrict vy, int64_t k) {
  1680. quantize_row_q2_K_reference(x, vy, k);
  1681. }
  1682. static float make_qkx3_quants(int n, int nmax, const float * restrict x, const float * restrict weights,
  1683. uint8_t * restrict L, float * restrict the_min, uint8_t * restrict Laux,
  1684. float rmin, float rdelta, int nstep, bool use_mad) {
  1685. float min = x[0];
  1686. float max = x[0];
  1687. float sum_w = weights ? weights[0] : x[0]*x[0];
  1688. float sum_x = sum_w * x[0];
  1689. #ifdef HAVE_BUGGY_APPLE_LINKER
  1690. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  1691. for (volatile int i = 1; i < n; ++i) {
  1692. #else
  1693. for (int i = 1; i < n; ++i) {
  1694. #endif
  1695. if (x[i] < min) min = x[i];
  1696. if (x[i] > max) max = x[i];
  1697. float w = weights ? weights[i] : x[i]*x[i];
  1698. sum_w += w;
  1699. sum_x += w * x[i];
  1700. }
  1701. if (min > 0) {
  1702. min = 0;
  1703. }
  1704. if (max <= min) {
  1705. memset(L, 0, n);
  1706. *the_min = -min;
  1707. return 0.f;
  1708. }
  1709. float iscale = nmax/(max - min);
  1710. float scale = 1/iscale;
  1711. float best_mad = 0;
  1712. for (int i = 0; i < n; ++i) {
  1713. int l = nearest_int(iscale*(x[i] - min));
  1714. L[i] = MAX(0, MIN(nmax, l));
  1715. float diff = scale * L[i] + min - x[i];
  1716. diff = use_mad ? fabsf(diff) : diff*diff;
  1717. float w = weights ? weights[i] : x[i]*x[i];
  1718. best_mad += w * diff;
  1719. }
  1720. if (nstep < 1) {
  1721. *the_min = -min;
  1722. return scale;
  1723. }
  1724. for (int is = 0; is <= nstep; ++is) {
  1725. iscale = (rmin + rdelta*is + nmax)/(max - min);
  1726. float sum_l = 0, sum_l2 = 0, sum_xl = 0;
  1727. for (int i = 0; i < n; ++i) {
  1728. int l = nearest_int(iscale*(x[i] - min));
  1729. l = MAX(0, MIN(nmax, l));
  1730. Laux[i] = l;
  1731. float w = weights ? weights[i] : x[i]*x[i];
  1732. sum_l += w*l;
  1733. sum_l2 += w*l*l;
  1734. sum_xl += w*l*x[i];
  1735. }
  1736. float D = sum_w * sum_l2 - sum_l * sum_l;
  1737. if (D > 0) {
  1738. float this_scale = (sum_w * sum_xl - sum_x * sum_l)/D;
  1739. float this_min = (sum_l2 * sum_x - sum_l * sum_xl)/D;
  1740. if (this_min > 0) {
  1741. this_min = 0;
  1742. this_scale = sum_xl / sum_l2;
  1743. }
  1744. float mad = 0;
  1745. for (int i = 0; i < n; ++i) {
  1746. float diff = this_scale * Laux[i] + this_min - x[i];
  1747. diff = use_mad ? fabsf(diff) : diff*diff;
  1748. float w = weights ? weights[i] : x[i]*x[i];
  1749. mad += w * diff;
  1750. }
  1751. if (mad < best_mad) {
  1752. for (int i = 0; i < n; ++i) {
  1753. L[i] = Laux[i];
  1754. }
  1755. best_mad = mad;
  1756. scale = this_scale;
  1757. min = this_min;
  1758. }
  1759. }
  1760. }
  1761. *the_min = -min;
  1762. return scale;
  1763. }
  1764. static float make_qp_quants(int n, int nmax, const float * restrict x, uint8_t * restrict L, const float * quant_weights) {
  1765. float max = 0;
  1766. for (int i = 0; i < n; ++i) {
  1767. max = MAX(max, x[i]);
  1768. }
  1769. if (!max) { // all zero
  1770. for (int i = 0; i < n; ++i) { L[i] = 0; }
  1771. return 0.f;
  1772. }
  1773. float iscale = nmax / max;
  1774. for (int i = 0; i < n; ++i) {
  1775. L[i] = nearest_int(iscale * x[i]);
  1776. }
  1777. float scale = 1/iscale;
  1778. float best_mse = 0;
  1779. for (int i = 0; i < n; ++i) {
  1780. float diff = x[i] - scale*L[i];
  1781. float w = quant_weights[i];
  1782. best_mse += w*diff*diff;
  1783. }
  1784. for (int is = -4; is <= 4; ++is) {
  1785. if (is == 0) continue;
  1786. float iscale_is = (0.1f*is + nmax)/max;
  1787. float scale_is = 1/iscale_is;
  1788. float mse = 0;
  1789. for (int i = 0; i < n; ++i) {
  1790. int l = nearest_int(iscale_is*x[i]);
  1791. l = MIN(nmax, l);
  1792. float diff = x[i] - scale_is*l;
  1793. float w = quant_weights[i];
  1794. mse += w*diff*diff;
  1795. }
  1796. if (mse < best_mse) {
  1797. best_mse = mse;
  1798. iscale = iscale_is;
  1799. }
  1800. }
  1801. float sumlx = 0;
  1802. float suml2 = 0;
  1803. for (int i = 0; i < n; ++i) {
  1804. int l = nearest_int(iscale * x[i]);
  1805. l = MIN(nmax, l);
  1806. L[i] = l;
  1807. float w = quant_weights[i];
  1808. sumlx += w*x[i]*l;
  1809. suml2 += w*l*l;
  1810. }
  1811. for (int itry = 0; itry < 5; ++itry) {
  1812. int n_changed = 0;
  1813. for (int i = 0; i < n; ++i) {
  1814. float w = quant_weights[i];
  1815. float slx = sumlx - w*x[i]*L[i];
  1816. float sl2 = suml2 - w*L[i]*L[i];
  1817. if (slx > 0 && sl2 > 0) {
  1818. int new_l = nearest_int(x[i] * sl2 / slx);
  1819. new_l = MIN(nmax, new_l);
  1820. if (new_l != L[i]) {
  1821. slx += w*x[i]*new_l;
  1822. sl2 += w*new_l*new_l;
  1823. if (slx*slx*suml2 > sumlx*sumlx*sl2) {
  1824. L[i] = new_l; sumlx = slx; suml2 = sl2;
  1825. ++n_changed;
  1826. }
  1827. }
  1828. }
  1829. }
  1830. if (!n_changed) {
  1831. break;
  1832. }
  1833. }
  1834. return sumlx/suml2;
  1835. }
  1836. static void quantize_row_q2_K_impl(const float * restrict x, block_q2_K * restrict y, int k, const float * restrict quant_weights) {
  1837. GGML_ASSERT(quant_weights);
  1838. assert(k % QK_K == 0);
  1839. const int nb = k / QK_K;
  1840. const bool requantize = true;
  1841. uint8_t L[QK_K];
  1842. uint8_t Laux[16];
  1843. float mins[QK_K/16];
  1844. float scales[QK_K/16];
  1845. float sw[QK_K/16];
  1846. float weight[16];
  1847. uint8_t Ls[QK_K/16], Lm[QK_K/16];
  1848. for (int i = 0; i < nb; i++) {
  1849. memset(sw, 0, QK_K/16*sizeof(float));
  1850. float sumx2 = 0;
  1851. for (int j = 0; j < QK_K; ++j) sumx2 += x[j]*x[j];
  1852. float sigma2 = sumx2/QK_K;
  1853. for (int j = 0; j < QK_K/16; ++j) {
  1854. const float * restrict qw = quant_weights + QK_K * i + 16*j;
  1855. for (int l = 0; l < 16; ++l) weight[l] = qw[l] * sqrtf(sigma2 + x[16*j + l]*x[16*j + l]);
  1856. for (int l = 0; l < QK_K/16; ++l) sw[j] += weight[l];
  1857. scales[j] = make_qkx3_quants(16, 3, x + 16*j, weight, L + 16*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  1858. }
  1859. float dm, mm;
  1860. dm = make_qp_quants(QK_K/16, 15, scales, Ls, sw);
  1861. mm = make_qp_quants(QK_K/16, 15, mins, Lm, sw);
  1862. y[i].d = GGML_FP32_TO_FP16(dm);
  1863. y[i].dmin = GGML_FP32_TO_FP16(mm);
  1864. dm = GGML_FP16_TO_FP32(y[i].d);
  1865. mm = GGML_FP16_TO_FP32(y[i].dmin);
  1866. for (int j = 0; j < QK_K/16; ++j) {
  1867. y[i].scales[j] = Ls[j] | (Lm[j] << 4);
  1868. }
  1869. if (requantize) {
  1870. for (int j = 0; j < QK_K/16; ++j) {
  1871. const float d = dm * (y[i].scales[j] & 0xF);
  1872. if (!d) continue;
  1873. const float m = mm * (y[i].scales[j] >> 4);
  1874. for (int ii = 0; ii < 16; ++ii) {
  1875. int l = nearest_int((x[16*j + ii] + m)/d);
  1876. l = MAX(0, MIN(3, l));
  1877. L[16*j + ii] = l;
  1878. }
  1879. }
  1880. }
  1881. for (int j = 0; j < QK_K; j += 128) {
  1882. for (int l = 0; l < 32; ++l) {
  1883. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1884. }
  1885. }
  1886. x += QK_K;
  1887. }
  1888. }
  1889. size_t quantize_q2_K(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  1890. size_t row_size = ggml_row_size(GGML_TYPE_Q2_K, n_per_row);
  1891. if (!quant_weights) {
  1892. quantize_row_q2_K_reference(src, dst, (int64_t)nrow*n_per_row);
  1893. }
  1894. else {
  1895. char * qrow = (char *)dst;
  1896. for (int64_t row = 0; row < nrow; ++row) {
  1897. quantize_row_q2_K_impl(src, (block_q2_K*)qrow, n_per_row, quant_weights);
  1898. src += n_per_row;
  1899. qrow += row_size;
  1900. }
  1901. }
  1902. return nrow * row_size;
  1903. }
  1904. //========================= 3-bit (de)-quantization
  1905. void quantize_row_q3_K_reference(const float * restrict x, block_q3_K * restrict y, int64_t k) {
  1906. assert(k % QK_K == 0);
  1907. const int nb = k / QK_K;
  1908. int8_t L[QK_K];
  1909. float scales[QK_K / 16];
  1910. for (int i = 0; i < nb; i++) {
  1911. float max_scale = 0;
  1912. float amax = 0;
  1913. for (int j = 0; j < QK_K/16; ++j) {
  1914. scales[j] = make_q3_quants(16, 4, x + 16*j, L + 16*j, true);
  1915. float scale = fabsf(scales[j]);
  1916. if (scale > amax) {
  1917. amax = scale; max_scale = scales[j];
  1918. }
  1919. }
  1920. memset(y[i].scales, 0, 12);
  1921. if (max_scale) {
  1922. float iscale = -32.f/max_scale;
  1923. for (int j = 0; j < QK_K/16; ++j) {
  1924. int8_t l = nearest_int(iscale*scales[j]);
  1925. l = MAX(-32, MIN(31, l)) + 32;
  1926. if (j < 8) {
  1927. y[i].scales[j] = l & 0xF;
  1928. } else {
  1929. y[i].scales[j-8] |= ((l & 0xF) << 4);
  1930. }
  1931. l >>= 4;
  1932. y[i].scales[j%4 + 8] |= (l << (2*(j/4)));
  1933. }
  1934. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  1935. } else {
  1936. y[i].d = GGML_FP32_TO_FP16(0.f);
  1937. }
  1938. int8_t sc;
  1939. for (int j = 0; j < QK_K/16; ++j) {
  1940. sc = j < 8 ? y[i].scales[j] & 0xF : y[i].scales[j-8] >> 4;
  1941. sc = (sc | (((y[i].scales[8 + j%4] >> (2*(j/4))) & 3) << 4)) - 32;
  1942. float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1943. if (!d) {
  1944. continue;
  1945. }
  1946. for (int ii = 0; ii < 16; ++ii) {
  1947. int l = nearest_int(x[16*j + ii]/d);
  1948. l = MAX(-4, MIN(3, l));
  1949. L[16*j + ii] = l + 4;
  1950. }
  1951. }
  1952. memset(y[i].hmask, 0, QK_K/8);
  1953. // We put the high-bit for the 1st 8 quants into bit 0, the next 8 into bit 1, etc.
  1954. int m = 0;
  1955. uint8_t hm = 1;
  1956. for (int j = 0; j < QK_K; ++j) {
  1957. if (L[j] > 3) {
  1958. y[i].hmask[m] |= hm;
  1959. L[j] -= 4;
  1960. }
  1961. if (++m == QK_K/8) {
  1962. m = 0; hm <<= 1;
  1963. }
  1964. }
  1965. for (int j = 0; j < QK_K; j += 128) {
  1966. for (int l = 0; l < 32; ++l) {
  1967. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1968. }
  1969. }
  1970. x += QK_K;
  1971. }
  1972. }
  1973. void dequantize_row_q3_K(const block_q3_K * restrict x, float * restrict y, int64_t k) {
  1974. assert(k % QK_K == 0);
  1975. const int nb = k / QK_K;
  1976. const uint32_t kmask1 = 0x03030303;
  1977. const uint32_t kmask2 = 0x0f0f0f0f;
  1978. uint32_t aux[4];
  1979. const int8_t * scales = (const int8_t*)aux;
  1980. for (int i = 0; i < nb; i++) {
  1981. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  1982. const uint8_t * restrict q = x[i].qs;
  1983. const uint8_t * restrict hm = x[i].hmask;
  1984. uint8_t m = 1;
  1985. memcpy(aux, x[i].scales, 12);
  1986. uint32_t tmp = aux[2];
  1987. aux[2] = ((aux[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
  1988. aux[3] = ((aux[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
  1989. aux[0] = (aux[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
  1990. aux[1] = (aux[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
  1991. int is = 0;
  1992. float dl;
  1993. for (int n = 0; n < QK_K; n += 128) {
  1994. int shift = 0;
  1995. for (int j = 0; j < 4; ++j) {
  1996. dl = d_all * (scales[is++] - 32);
  1997. for (int l = 0; l < 16; ++l) {
  1998. *y++ = dl * ((int8_t)((q[l+ 0] >> shift) & 3) - ((hm[l+ 0] & m) ? 0 : 4));
  1999. }
  2000. dl = d_all * (scales[is++] - 32);
  2001. for (int l = 0; l < 16; ++l) {
  2002. *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3) - ((hm[l+16] & m) ? 0 : 4));
  2003. }
  2004. shift += 2;
  2005. m <<= 1;
  2006. }
  2007. q += 32;
  2008. }
  2009. }
  2010. }
  2011. void quantize_row_q3_K(const float * restrict x, void * restrict vy, int64_t k) {
  2012. quantize_row_q3_K_reference(x, vy, k);
  2013. }
  2014. static void quantize_row_q3_K_impl(const float * restrict x, block_q3_K * restrict y, int64_t n_per_row, const float * restrict quant_weights) {
  2015. assert(n_per_row % QK_K == 0);
  2016. const int nb = n_per_row / QK_K;
  2017. int8_t L[QK_K];
  2018. float scales[QK_K / 16];
  2019. float weight[16];
  2020. float sw[QK_K / 16];
  2021. int8_t Ls[QK_K / 16];
  2022. for (int i = 0; i < nb; i++) {
  2023. float sumx2 = 0;
  2024. for (int j = 0; j < QK_K; ++j) sumx2 += x[j]*x[j];
  2025. float sigma2 = 2*sumx2/QK_K;
  2026. for (int j = 0; j < QK_K/16; ++j) {
  2027. if (quant_weights) {
  2028. const float * qw = quant_weights + QK_K * i + 16*j;
  2029. for (int l = 0; l < 16; ++l) weight[l] = qw[l] * sqrtf(sigma2 + x[16*j+l]*x[16*j+l]);
  2030. } else {
  2031. for (int l = 0; l < 16; ++l) weight[l] = x[16*j+l]*x[16*j+l];
  2032. }
  2033. float sumw = 0;
  2034. for (int l = 0; l < 16; ++l) sumw += weight[l];
  2035. sw[j] = sumw;
  2036. scales[j] = make_qx_quants(16, 4, x + 16*j, L + 16*j, 1, weight);
  2037. }
  2038. memset(y[i].scales, 0, 12);
  2039. float d_block = make_qx_quants(QK_K/16, 32, scales, Ls, 1, sw);
  2040. for (int j = 0; j < QK_K/16; ++j) {
  2041. int l = Ls[j];
  2042. if (j < 8) {
  2043. y[i].scales[j] = l & 0xF;
  2044. } else {
  2045. y[i].scales[j-8] |= ((l & 0xF) << 4);
  2046. }
  2047. l >>= 4;
  2048. y[i].scales[j%4 + 8] |= (l << (2*(j/4)));
  2049. }
  2050. y[i].d = GGML_FP32_TO_FP16(d_block);
  2051. int8_t sc;
  2052. for (int j = 0; j < QK_K/16; ++j) {
  2053. sc = j < 8 ? y[i].scales[j] & 0xF : y[i].scales[j-8] >> 4;
  2054. sc = (sc | (((y[i].scales[8 + j%4] >> (2*(j/4))) & 3) << 4)) - 32;
  2055. float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  2056. if (!d) {
  2057. continue;
  2058. }
  2059. for (int ii = 0; ii < 16; ++ii) {
  2060. int l = nearest_int(x[16*j + ii]/d);
  2061. l = MAX(-4, MIN(3, l));
  2062. L[16*j + ii] = l + 4;
  2063. }
  2064. }
  2065. memset(y[i].hmask, 0, QK_K/8);
  2066. // We put the high-bit for the 1st 8 quants into bit 0, the next 8 into bit 1, etc.
  2067. int m = 0;
  2068. uint8_t hm = 1;
  2069. for (int j = 0; j < QK_K; ++j) {
  2070. if (L[j] > 3) {
  2071. y[i].hmask[m] |= hm;
  2072. L[j] -= 4;
  2073. }
  2074. if (++m == QK_K/8) {
  2075. m = 0; hm <<= 1;
  2076. }
  2077. }
  2078. for (int j = 0; j < QK_K; j += 128) {
  2079. for (int l = 0; l < 32; ++l) {
  2080. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  2081. }
  2082. }
  2083. x += QK_K;
  2084. }
  2085. }
  2086. size_t quantize_q3_K(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2087. size_t row_size = ggml_row_size(GGML_TYPE_Q3_K, n_per_row);
  2088. if (!quant_weights) {
  2089. quantize_row_q3_K_reference(src, dst, (int64_t)nrow*n_per_row);
  2090. }
  2091. else {
  2092. char * qrow = (char *)dst;
  2093. for (int64_t row = 0; row < nrow; ++row) {
  2094. quantize_row_q3_K_impl(src, (block_q3_K*)qrow, n_per_row, quant_weights);
  2095. src += n_per_row;
  2096. qrow += row_size;
  2097. }
  2098. }
  2099. return nrow * row_size;
  2100. }
  2101. // ====================== 4-bit (de)-quantization
  2102. void quantize_row_q4_K_reference(const float * restrict x, block_q4_K * restrict y, int64_t k) {
  2103. assert(k % QK_K == 0);
  2104. const int nb = k / QK_K;
  2105. uint8_t L[QK_K];
  2106. uint8_t Laux[32];
  2107. float weights[32];
  2108. float mins[QK_K/32];
  2109. float scales[QK_K/32];
  2110. for (int i = 0; i < nb; i++) {
  2111. float max_scale = 0; // as we are deducting the min, scales are always positive
  2112. float max_min = 0;
  2113. for (int j = 0; j < QK_K/32; ++j) {
  2114. //scales[j] = make_qkx1_quants(32, 15, x + 32*j, L + 32*j, &mins[j], 9, 0.5f);
  2115. float sum_x2 = 0;
  2116. for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l];
  2117. float av_x = sqrtf(sum_x2/32);
  2118. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  2119. scales[j] = make_qkx2_quants(32, 15, x + 32*j, weights, L + 32*j, &mins[j], Laux, -1.f, 0.1f, 20, false);
  2120. float scale = scales[j];
  2121. if (scale > max_scale) {
  2122. max_scale = scale;
  2123. }
  2124. float min = mins[j];
  2125. if (min > max_min) {
  2126. max_min = min;
  2127. }
  2128. }
  2129. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  2130. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  2131. for (int j = 0; j < QK_K/32; ++j) {
  2132. uint8_t ls = nearest_int(inv_scale*scales[j]);
  2133. uint8_t lm = nearest_int(inv_min*mins[j]);
  2134. ls = MIN(63, ls);
  2135. lm = MIN(63, lm);
  2136. if (j < 4) {
  2137. y[i].scales[j] = ls;
  2138. y[i].scales[j+4] = lm;
  2139. } else {
  2140. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  2141. y[i].scales[j-4] |= ((ls >> 4) << 6);
  2142. y[i].scales[j-0] |= ((lm >> 4) << 6);
  2143. }
  2144. }
  2145. y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
  2146. y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
  2147. uint8_t sc, m;
  2148. for (int j = 0; j < QK_K/32; ++j) {
  2149. get_scale_min_k4(j, y[i].scales, &sc, &m);
  2150. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  2151. if (!d) continue;
  2152. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  2153. for (int ii = 0; ii < 32; ++ii) {
  2154. int l = nearest_int((x[32*j + ii] + dm)/d);
  2155. l = MAX(0, MIN(15, l));
  2156. L[32*j + ii] = l;
  2157. }
  2158. }
  2159. uint8_t * q = y[i].qs;
  2160. for (int j = 0; j < QK_K; j += 64) {
  2161. for (int l = 0; l < 32; ++l) q[l] = L[j + l] | (L[j + l + 32] << 4);
  2162. q += 32;
  2163. }
  2164. x += QK_K;
  2165. }
  2166. }
  2167. void dequantize_row_q4_K(const block_q4_K * restrict x, float * restrict y, int64_t k) {
  2168. assert(k % QK_K == 0);
  2169. const int nb = k / QK_K;
  2170. for (int i = 0; i < nb; i++) {
  2171. const uint8_t * q = x[i].qs;
  2172. const float d = GGML_FP16_TO_FP32(x[i].d);
  2173. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  2174. int is = 0;
  2175. uint8_t sc, m;
  2176. for (int j = 0; j < QK_K; j += 64) {
  2177. get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
  2178. const float d1 = d * sc; const float m1 = min * m;
  2179. get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
  2180. const float d2 = d * sc; const float m2 = min * m;
  2181. for (int l = 0; l < 32; ++l) *y++ = d1 * (q[l] & 0xF) - m1;
  2182. for (int l = 0; l < 32; ++l) *y++ = d2 * (q[l] >> 4) - m2;
  2183. q += 32; is += 2;
  2184. }
  2185. }
  2186. }
  2187. void quantize_row_q4_K(const float * restrict x, void * restrict vy, int64_t k) {
  2188. assert(k % QK_K == 0);
  2189. block_q4_K * restrict y = vy;
  2190. quantize_row_q4_K_reference(x, y, k);
  2191. }
  2192. static void quantize_row_q4_K_impl(const float * restrict x, block_q4_K * restrict y, int64_t n_per_row, const float * quant_weights) {
  2193. assert(n_per_row % QK_K == 0);
  2194. const int64_t nb = n_per_row / QK_K;
  2195. uint8_t L[QK_K];
  2196. uint8_t Laux[32];
  2197. uint8_t Ls[QK_K/32];
  2198. uint8_t Lm[QK_K/32];
  2199. float weights[32];
  2200. float sw[QK_K/32];
  2201. float mins[QK_K/32];
  2202. float scales[QK_K/32];
  2203. for (int i = 0; i < nb; i++) {
  2204. float sum_x2 = 0;
  2205. for (int l = 0; l < QK_K; ++l) sum_x2 += x[l] * x[l];
  2206. float sigma2 = 2*sum_x2/QK_K;
  2207. float av_x = sqrtf(sigma2);
  2208. for (int j = 0; j < QK_K/32; ++j) {
  2209. if (quant_weights) {
  2210. const float * qw = quant_weights + QK_K*i + 32*j;
  2211. for (int l = 0; l < 32; ++l) weights[l] = qw[l] * sqrtf(sigma2 + x[32*j + l]*x[32*j + l]);
  2212. } else {
  2213. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  2214. }
  2215. float sumw = 0;
  2216. for (int l = 0; l < 32; ++l) sumw += weights[l];
  2217. sw[j] = sumw;
  2218. scales[j] = make_qkx3_quants(32, 15, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  2219. }
  2220. float d_block = make_qp_quants(QK_K/32, 63, scales, Ls, sw);
  2221. float m_block = make_qp_quants(QK_K/32, 63, mins, Lm, sw);
  2222. for (int j = 0; j < QK_K/32; ++j) {
  2223. uint8_t ls = Ls[j];
  2224. uint8_t lm = Lm[j];
  2225. if (j < 4) {
  2226. y[i].scales[j] = ls;
  2227. y[i].scales[j+4] = lm;
  2228. } else {
  2229. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  2230. y[i].scales[j-4] |= ((ls >> 4) << 6);
  2231. y[i].scales[j-0] |= ((lm >> 4) << 6);
  2232. }
  2233. }
  2234. y[i].d = GGML_FP32_TO_FP16(d_block);
  2235. y[i].dmin = GGML_FP32_TO_FP16(m_block);
  2236. uint8_t sc, m;
  2237. for (int j = 0; j < QK_K/32; ++j) {
  2238. get_scale_min_k4(j, y[i].scales, &sc, &m);
  2239. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  2240. if (!d) continue;
  2241. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  2242. for (int ii = 0; ii < 32; ++ii) {
  2243. int l = nearest_int((x[32*j + ii] + dm)/d);
  2244. l = MAX(0, MIN(15, l));
  2245. L[32*j + ii] = l;
  2246. }
  2247. }
  2248. uint8_t * q = y[i].qs;
  2249. for (int j = 0; j < QK_K; j += 64) {
  2250. for (int l = 0; l < 32; ++l) q[l] = L[j + l] | (L[j + l + 32] << 4);
  2251. q += 32;
  2252. }
  2253. x += QK_K;
  2254. }
  2255. }
  2256. size_t quantize_q4_K(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2257. size_t row_size = ggml_row_size(GGML_TYPE_Q4_K, n_per_row);
  2258. if (!quant_weights) {
  2259. quantize_row_q4_K_reference(src, dst, (int64_t)nrow*n_per_row);
  2260. }
  2261. else {
  2262. char * qrow = (char *)dst;
  2263. for (int64_t row = 0; row < nrow; ++row) {
  2264. quantize_row_q4_K_impl(src, (block_q4_K*)qrow, n_per_row, quant_weights);
  2265. src += n_per_row;
  2266. qrow += row_size;
  2267. }
  2268. }
  2269. return nrow * row_size;
  2270. }
  2271. // ====================== 5-bit (de)-quantization
  2272. void quantize_row_q5_K_reference(const float * restrict x, block_q5_K * restrict y, int64_t k) {
  2273. assert(k % QK_K == 0);
  2274. const int64_t nb = k / QK_K;
  2275. uint8_t L[QK_K];
  2276. float mins[QK_K/32];
  2277. float scales[QK_K/32];
  2278. float weights[32];
  2279. uint8_t Laux[32];
  2280. for (int i = 0; i < nb; i++) {
  2281. float max_scale = 0; // as we are deducting the min, scales are always positive
  2282. float max_min = 0;
  2283. for (int j = 0; j < QK_K/32; ++j) {
  2284. //scales[j] = make_qkx1_quants(32, 31, x + 32*j, L + 32*j, &mins[j], 9, 0.5f);
  2285. float sum_x2 = 0;
  2286. for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l];
  2287. float av_x = sqrtf(sum_x2/32);
  2288. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  2289. scales[j] = make_qkx2_quants(32, 31, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.5f, 0.1f, 15, false);
  2290. float scale = scales[j];
  2291. if (scale > max_scale) {
  2292. max_scale = scale;
  2293. }
  2294. float min = mins[j];
  2295. if (min > max_min) {
  2296. max_min = min;
  2297. }
  2298. }
  2299. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  2300. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  2301. for (int j = 0; j < QK_K/32; ++j) {
  2302. uint8_t ls = nearest_int(inv_scale*scales[j]);
  2303. uint8_t lm = nearest_int(inv_min*mins[j]);
  2304. ls = MIN(63, ls);
  2305. lm = MIN(63, lm);
  2306. if (j < 4) {
  2307. y[i].scales[j] = ls;
  2308. y[i].scales[j+4] = lm;
  2309. } else {
  2310. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  2311. y[i].scales[j-4] |= ((ls >> 4) << 6);
  2312. y[i].scales[j-0] |= ((lm >> 4) << 6);
  2313. }
  2314. }
  2315. y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
  2316. y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
  2317. uint8_t sc, m;
  2318. for (int j = 0; j < QK_K/32; ++j) {
  2319. get_scale_min_k4(j, y[i].scales, &sc, &m);
  2320. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  2321. if (!d) continue;
  2322. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  2323. for (int ii = 0; ii < 32; ++ii) {
  2324. int l = nearest_int((x[32*j + ii] + dm)/d);
  2325. l = MAX(0, MIN(31, l));
  2326. L[32*j + ii] = l;
  2327. }
  2328. }
  2329. uint8_t * restrict qh = y[i].qh;
  2330. uint8_t * restrict ql = y[i].qs;
  2331. memset(qh, 0, QK_K/8);
  2332. uint8_t m1 = 1, m2 = 2;
  2333. for (int n = 0; n < QK_K; n += 64) {
  2334. for (int j = 0; j < 32; ++j) {
  2335. int l1 = L[n + j];
  2336. if (l1 > 15) {
  2337. l1 -= 16; qh[j] |= m1;
  2338. }
  2339. int l2 = L[n + j + 32];
  2340. if (l2 > 15) {
  2341. l2 -= 16; qh[j] |= m2;
  2342. }
  2343. ql[j] = l1 | (l2 << 4);
  2344. }
  2345. m1 <<= 2; m2 <<= 2;
  2346. ql += 32;
  2347. }
  2348. x += QK_K;
  2349. }
  2350. }
  2351. void dequantize_row_q5_K(const block_q5_K * restrict x, float * restrict y, int64_t k) {
  2352. assert(k % QK_K == 0);
  2353. const int64_t nb = k / QK_K;
  2354. for (int i = 0; i < nb; i++) {
  2355. const uint8_t * ql = x[i].qs;
  2356. const uint8_t * qh = x[i].qh;
  2357. const float d = GGML_FP16_TO_FP32(x[i].d);
  2358. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  2359. int is = 0;
  2360. uint8_t sc, m;
  2361. uint8_t u1 = 1, u2 = 2;
  2362. for (int j = 0; j < QK_K; j += 64) {
  2363. get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
  2364. const float d1 = d * sc; const float m1 = min * m;
  2365. get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
  2366. const float d2 = d * sc; const float m2 = min * m;
  2367. for (int l = 0; l < 32; ++l) *y++ = d1 * ((ql[l] & 0xF) + (qh[l] & u1 ? 16 : 0)) - m1;
  2368. for (int l = 0; l < 32; ++l) *y++ = d2 * ((ql[l] >> 4) + (qh[l] & u2 ? 16 : 0)) - m2;
  2369. ql += 32; is += 2;
  2370. u1 <<= 2; u2 <<= 2;
  2371. }
  2372. }
  2373. }
  2374. void quantize_row_q5_K(const float * restrict x, void * restrict vy, int64_t k) {
  2375. assert(k % QK_K == 0);
  2376. block_q5_K * restrict y = vy;
  2377. quantize_row_q5_K_reference(x, y, k);
  2378. }
  2379. static void quantize_row_q5_K_impl(const float * restrict x, block_q5_K * restrict y, int64_t n_per_row, const float * quant_weights) {
  2380. assert(n_per_row % QK_K == 0);
  2381. const int64_t nb = n_per_row / QK_K;
  2382. uint8_t L[QK_K];
  2383. uint8_t Laux[32];
  2384. uint8_t Ls[QK_K/32];
  2385. uint8_t Lm[QK_K/32];
  2386. float mins[QK_K/32];
  2387. float scales[QK_K/32];
  2388. float sw[QK_K/32];
  2389. float weights[32];
  2390. for (int i = 0; i < nb; i++) {
  2391. float sum_x2 = 0;
  2392. for (int l = 0; l < QK_K; ++l) sum_x2 += x[l] * x[l];
  2393. float sigma2 = 2*sum_x2/QK_K;
  2394. float av_x = sqrtf(sigma2);
  2395. for (int j = 0; j < QK_K/32; ++j) {
  2396. if (quant_weights) {
  2397. const float * qw = quant_weights + QK_K*i + 32*j;
  2398. for (int l = 0; l < 32; ++l) weights[l] = qw[l] * sqrtf(sigma2 + x[32*j + l]*x[32*j + l]);
  2399. } else {
  2400. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  2401. }
  2402. float sumw = 0;
  2403. for (int l = 0; l < 32; ++l) sumw += weights[l];
  2404. sw[j] = sumw;
  2405. scales[j] = make_qkx3_quants(32, 31, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  2406. }
  2407. float d_block = make_qp_quants(QK_K/32, 63, scales, Ls, sw);
  2408. float m_block = make_qp_quants(QK_K/32, 63, mins, Lm, sw);
  2409. for (int j = 0; j < QK_K/32; ++j) {
  2410. uint8_t ls = Ls[j];
  2411. uint8_t lm = Lm[j];
  2412. ls = MIN(63, ls);
  2413. lm = MIN(63, lm);
  2414. if (j < 4) {
  2415. y[i].scales[j] = ls;
  2416. y[i].scales[j+4] = lm;
  2417. } else {
  2418. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  2419. y[i].scales[j-4] |= ((ls >> 4) << 6);
  2420. y[i].scales[j-0] |= ((lm >> 4) << 6);
  2421. }
  2422. }
  2423. y[i].d = GGML_FP32_TO_FP16(d_block);
  2424. y[i].dmin = GGML_FP32_TO_FP16(m_block);
  2425. uint8_t sc, m;
  2426. for (int j = 0; j < QK_K/32; ++j) {
  2427. get_scale_min_k4(j, y[i].scales, &sc, &m);
  2428. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  2429. if (!d) continue;
  2430. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  2431. for (int ii = 0; ii < 32; ++ii) {
  2432. int l = nearest_int((x[32*j + ii] + dm)/d);
  2433. l = MAX(0, MIN(31, l));
  2434. L[32*j + ii] = l;
  2435. }
  2436. }
  2437. uint8_t * restrict qh = y[i].qh;
  2438. uint8_t * restrict ql = y[i].qs;
  2439. memset(qh, 0, QK_K/8);
  2440. uint8_t m1 = 1, m2 = 2;
  2441. for (int n = 0; n < QK_K; n += 64) {
  2442. for (int j = 0; j < 32; ++j) {
  2443. int l1 = L[n + j];
  2444. if (l1 > 15) {
  2445. l1 -= 16; qh[j] |= m1;
  2446. }
  2447. int l2 = L[n + j + 32];
  2448. if (l2 > 15) {
  2449. l2 -= 16; qh[j] |= m2;
  2450. }
  2451. ql[j] = l1 | (l2 << 4);
  2452. }
  2453. m1 <<= 2; m2 <<= 2;
  2454. ql += 32;
  2455. }
  2456. x += QK_K;
  2457. }
  2458. }
  2459. size_t quantize_q5_K(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2460. size_t row_size = ggml_row_size(GGML_TYPE_Q5_K, n_per_row);
  2461. if (!quant_weights) {
  2462. quantize_row_q5_K_reference(src, dst, (int64_t)nrow*n_per_row);
  2463. }
  2464. else {
  2465. char * qrow = (char *)dst;
  2466. for (int64_t row = 0; row < nrow; ++row) {
  2467. quantize_row_q5_K_impl(src, (block_q5_K*)qrow, n_per_row, quant_weights);
  2468. src += n_per_row;
  2469. qrow += row_size;
  2470. }
  2471. }
  2472. return nrow * row_size;
  2473. }
  2474. // ====================== 6-bit (de)-quantization
  2475. void quantize_row_q6_K_reference(const float * restrict x, block_q6_K * restrict y, int64_t k) {
  2476. assert(k % QK_K == 0);
  2477. const int64_t nb = k / QK_K;
  2478. int8_t L[QK_K];
  2479. float scales[QK_K/16];
  2480. for (int i = 0; i < nb; i++) {
  2481. float max_scale = 0;
  2482. float max_abs_scale = 0;
  2483. for (int ib = 0; ib < QK_K/16; ++ib) {
  2484. const float scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, NULL);
  2485. scales[ib] = scale;
  2486. const float abs_scale = fabsf(scale);
  2487. if (abs_scale > max_abs_scale) {
  2488. max_abs_scale = abs_scale;
  2489. max_scale = scale;
  2490. }
  2491. }
  2492. if (max_abs_scale < GROUP_MAX_EPS) {
  2493. memset(&y[i], 0, sizeof(block_q6_K));
  2494. y[i].d = GGML_FP32_TO_FP16(0.f);
  2495. x += QK_K;
  2496. continue;
  2497. }
  2498. float iscale = -128.f/max_scale;
  2499. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2500. for (int ib = 0; ib < QK_K/16; ++ib) {
  2501. y[i].scales[ib] = MIN(127, nearest_int(iscale*scales[ib]));
  2502. }
  2503. for (int j = 0; j < QK_K/16; ++j) {
  2504. float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2505. if (!d) {
  2506. continue;
  2507. }
  2508. for (int ii = 0; ii < 16; ++ii) {
  2509. int l = nearest_int(x[16*j + ii]/d);
  2510. l = MAX(-32, MIN(31, l));
  2511. L[16*j + ii] = l + 32;
  2512. }
  2513. }
  2514. uint8_t * restrict ql = y[i].ql;
  2515. uint8_t * restrict qh = y[i].qh;
  2516. for (int j = 0; j < QK_K; j += 128) {
  2517. for (int l = 0; l < 32; ++l) {
  2518. const uint8_t q1 = L[j + l + 0] & 0xF;
  2519. const uint8_t q2 = L[j + l + 32] & 0xF;
  2520. const uint8_t q3 = L[j + l + 64] & 0xF;
  2521. const uint8_t q4 = L[j + l + 96] & 0xF;
  2522. ql[l+ 0] = q1 | (q3 << 4);
  2523. ql[l+32] = q2 | (q4 << 4);
  2524. qh[l] = (L[j + l] >> 4) | ((L[j + l + 32] >> 4) << 2) | ((L[j + l + 64] >> 4) << 4) | ((L[j + l + 96] >> 4) << 6);
  2525. }
  2526. ql += 64;
  2527. qh += 32;
  2528. }
  2529. x += QK_K;
  2530. }
  2531. }
  2532. void dequantize_row_q6_K(const block_q6_K * restrict x, float * restrict y, int64_t k) {
  2533. assert(k % QK_K == 0);
  2534. const int64_t nb = k / QK_K;
  2535. for (int i = 0; i < nb; i++) {
  2536. const float d = GGML_FP16_TO_FP32(x[i].d);
  2537. const uint8_t * restrict ql = x[i].ql;
  2538. const uint8_t * restrict qh = x[i].qh;
  2539. const int8_t * restrict sc = x[i].scales;
  2540. for (int n = 0; n < QK_K; n += 128) {
  2541. for (int l = 0; l < 32; ++l) {
  2542. int is = l/16;
  2543. const int8_t q1 = (int8_t)((ql[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  2544. const int8_t q2 = (int8_t)((ql[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  2545. const int8_t q3 = (int8_t)((ql[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  2546. const int8_t q4 = (int8_t)((ql[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  2547. y[l + 0] = d * sc[is + 0] * q1;
  2548. y[l + 32] = d * sc[is + 2] * q2;
  2549. y[l + 64] = d * sc[is + 4] * q3;
  2550. y[l + 96] = d * sc[is + 6] * q4;
  2551. }
  2552. y += 128;
  2553. ql += 64;
  2554. qh += 32;
  2555. sc += 8;
  2556. }
  2557. }
  2558. }
  2559. void quantize_row_q6_K(const float * restrict x, void * restrict vy, int64_t k) {
  2560. assert(k % QK_K == 0);
  2561. block_q6_K * restrict y = vy;
  2562. quantize_row_q6_K_reference(x, y, k);
  2563. }
  2564. static void quantize_row_q6_K_impl(const float * restrict x, block_q6_K * restrict y, int64_t n_per_row, const float * quant_weights) {
  2565. assert(n_per_row % QK_K == 0);
  2566. const int64_t nb = n_per_row / QK_K;
  2567. int8_t L[QK_K];
  2568. float scales[QK_K/16];
  2569. //float weights[16];
  2570. for (int i = 0; i < nb; i++) {
  2571. //float sum_x2 = 0;
  2572. //for (int j = 0; j < QK_K; ++j) sum_x2 += x[j]*x[j];
  2573. //float sigma2 = sum_x2/QK_K;
  2574. float max_scale = 0;
  2575. float max_abs_scale = 0;
  2576. for (int ib = 0; ib < QK_K/16; ++ib) {
  2577. float scale;
  2578. if (quant_weights) {
  2579. const float * qw = quant_weights + QK_K*i + 16*ib;
  2580. //for (int j = 0; j < 16; ++j) weights[j] = qw[j] * sqrtf(sigma2 + x[16*ib + j]*x[16*ib + j]);
  2581. //scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, weights);
  2582. scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, qw);
  2583. } else {
  2584. scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, NULL);
  2585. }
  2586. scales[ib] = scale;
  2587. const float abs_scale = fabsf(scale);
  2588. if (abs_scale > max_abs_scale) {
  2589. max_abs_scale = abs_scale;
  2590. max_scale = scale;
  2591. }
  2592. }
  2593. if (max_abs_scale < GROUP_MAX_EPS) {
  2594. memset(&y[i], 0, sizeof(block_q6_K));
  2595. y[i].d = GGML_FP32_TO_FP16(0.f);
  2596. x += QK_K;
  2597. continue;
  2598. }
  2599. float iscale = -128.f/max_scale;
  2600. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2601. for (int ib = 0; ib < QK_K/16; ++ib) {
  2602. y[i].scales[ib] = MIN(127, nearest_int(iscale*scales[ib]));
  2603. }
  2604. for (int j = 0; j < QK_K/16; ++j) {
  2605. float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2606. if (!d) {
  2607. continue;
  2608. }
  2609. for (int ii = 0; ii < 16; ++ii) {
  2610. int l = nearest_int(x[16*j + ii]/d);
  2611. l = MAX(-32, MIN(31, l));
  2612. L[16*j + ii] = l + 32;
  2613. }
  2614. }
  2615. uint8_t * restrict ql = y[i].ql;
  2616. uint8_t * restrict qh = y[i].qh;
  2617. for (int j = 0; j < QK_K; j += 128) {
  2618. for (int l = 0; l < 32; ++l) {
  2619. const uint8_t q1 = L[j + l + 0] & 0xF;
  2620. const uint8_t q2 = L[j + l + 32] & 0xF;
  2621. const uint8_t q3 = L[j + l + 64] & 0xF;
  2622. const uint8_t q4 = L[j + l + 96] & 0xF;
  2623. ql[l+ 0] = q1 | (q3 << 4);
  2624. ql[l+32] = q2 | (q4 << 4);
  2625. qh[l] = (L[j + l] >> 4) | ((L[j + l + 32] >> 4) << 2) | ((L[j + l + 64] >> 4) << 4) | ((L[j + l + 96] >> 4) << 6);
  2626. }
  2627. ql += 64;
  2628. qh += 32;
  2629. }
  2630. x += QK_K;
  2631. }
  2632. }
  2633. size_t quantize_q6_K(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2634. size_t row_size = ggml_row_size(GGML_TYPE_Q6_K, n_per_row);
  2635. if (!quant_weights) {
  2636. quantize_row_q6_K_reference(src, dst, (int64_t)nrow*n_per_row);
  2637. }
  2638. else {
  2639. char * qrow = (char *)dst;
  2640. for (int64_t row = 0; row < nrow; ++row) {
  2641. quantize_row_q6_K_impl(src, (block_q6_K*)qrow, n_per_row, quant_weights);
  2642. src += n_per_row;
  2643. qrow += row_size;
  2644. }
  2645. }
  2646. return nrow * row_size;
  2647. }
  2648. static void quantize_row_q4_0_impl(const float * restrict x, block_q4_0 * restrict y, int64_t n_per_row, const float * quant_weights) {
  2649. static_assert(QK4_0 == 32, "QK4_0 must be 32");
  2650. if (!quant_weights) {
  2651. quantize_row_q4_0_reference(x, y, n_per_row);
  2652. return;
  2653. }
  2654. float weight[QK4_0];
  2655. int8_t L[QK4_0];
  2656. float sum_x2 = 0;
  2657. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2658. float sigma2 = sum_x2/n_per_row;
  2659. const int64_t nb = n_per_row/QK4_0;
  2660. for (int ib = 0; ib < nb; ++ib) {
  2661. const float * xb = x + QK4_0 * ib;
  2662. const float * qw = quant_weights + QK4_0 * ib;
  2663. for (int j = 0; j < QK4_0; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2664. float d = make_qx_quants(QK4_0, 8, xb, L, 1, weight);
  2665. y[ib].d = GGML_FP32_TO_FP16(d);
  2666. for (int j = 0; j < 16; ++j) {
  2667. y[ib].qs[j] = L[j] | (L[j+16] << 4);
  2668. }
  2669. }
  2670. }
  2671. size_t quantize_q4_0(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2672. if (!quant_weights) {
  2673. quantize_row_q4_0_reference(src, dst, (int64_t)nrow*n_per_row);
  2674. return nrow * ggml_row_size(GGML_TYPE_Q4_0, n_per_row);
  2675. }
  2676. size_t row_size = ggml_row_size(GGML_TYPE_Q4_0, n_per_row);
  2677. char * qrow = (char *)dst;
  2678. for (int64_t row = 0; row < nrow; ++row) {
  2679. quantize_row_q4_0_impl(src, (block_q4_0*)qrow, n_per_row, quant_weights);
  2680. src += n_per_row;
  2681. qrow += row_size;
  2682. }
  2683. return nrow * row_size;
  2684. }
  2685. static void quantize_row_q4_1_impl(const float * restrict x, block_q4_1 * restrict y, int64_t n_per_row, const float * quant_weights) {
  2686. static_assert(QK4_1 == 32, "QK4_1 must be 32");
  2687. if (!quant_weights) {
  2688. quantize_row_q4_1_reference(x, y, n_per_row);
  2689. return;
  2690. }
  2691. float weight[QK4_1];
  2692. uint8_t L[QK4_1], Laux[QK4_1];
  2693. float sum_x2 = 0;
  2694. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2695. float sigma2 = sum_x2/n_per_row;
  2696. const int64_t nb = n_per_row/QK4_1;
  2697. for (int ib = 0; ib < nb; ++ib) {
  2698. const float * xb = x + QK4_1 * ib;
  2699. const float * qw = quant_weights + QK4_1 * ib;
  2700. for (int j = 0; j < QK4_1; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2701. float min;
  2702. float d = make_qkx3_quants(QK4_1, 15, xb, weight, L, &min, Laux, -0.9f, 0.05f, 36, false);
  2703. y[ib].d = GGML_FP32_TO_FP16(d);
  2704. y[ib].m = GGML_FP32_TO_FP16(-min);
  2705. for (int j = 0; j < 16; ++j) {
  2706. y[ib].qs[j] = L[j] | (L[j+16] << 4);
  2707. }
  2708. }
  2709. }
  2710. size_t quantize_q4_1(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2711. if (!quant_weights) {
  2712. quantize_row_q4_1_reference(src, dst, (int64_t)nrow*n_per_row);
  2713. return nrow * ggml_row_size(GGML_TYPE_Q4_1, n_per_row);
  2714. }
  2715. size_t row_size = ggml_row_size(GGML_TYPE_Q4_1, n_per_row);
  2716. char * qrow = (char *)dst;
  2717. for (int64_t row = 0; row < nrow; ++row) {
  2718. quantize_row_q4_1_impl(src, (block_q4_1*)qrow, n_per_row, quant_weights);
  2719. src += n_per_row;
  2720. qrow += row_size;
  2721. }
  2722. return nrow * row_size;
  2723. }
  2724. static void quantize_row_q5_0_impl(const float * restrict x, block_q5_0 * restrict y, int64_t n_per_row, const float * quant_weights) {
  2725. static_assert(QK5_0 == 32, "QK5_0 must be 32");
  2726. if (!quant_weights) {
  2727. quantize_row_q5_0_reference(x, y, n_per_row);
  2728. return;
  2729. }
  2730. float weight[QK5_0];
  2731. int8_t L[QK5_0];
  2732. float sum_x2 = 0;
  2733. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2734. float sigma2 = sum_x2/n_per_row;
  2735. const int64_t nb = n_per_row/QK5_0;
  2736. for (int ib = 0; ib < nb; ++ib) {
  2737. const float * xb = x + QK5_0 * ib;
  2738. const float * qw = quant_weights + QK5_0 * ib;
  2739. for (int j = 0; j < QK5_0; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2740. float d = make_qx_quants(QK5_0, 16, xb, L, 1, weight);
  2741. y[ib].d = GGML_FP32_TO_FP16(d);
  2742. uint32_t qh = 0;
  2743. for (int j = 0; j < 16; ++j) {
  2744. const uint8_t xi0 = L[j];
  2745. const uint8_t xi1 = L[j+16];
  2746. y[ib].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  2747. // get the 5-th bit and store it in qh at the right position
  2748. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  2749. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_0/2);
  2750. }
  2751. memcpy(&y[ib].qh, &qh, sizeof(qh));
  2752. }
  2753. }
  2754. size_t quantize_q5_0(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2755. if (!quant_weights) {
  2756. quantize_row_q5_0_reference(src, dst, (int64_t)nrow*n_per_row);
  2757. return nrow * ggml_row_size(GGML_TYPE_Q5_0, n_per_row);
  2758. }
  2759. size_t row_size = ggml_row_size(GGML_TYPE_Q5_0, n_per_row);
  2760. char * qrow = (char *)dst;
  2761. for (int64_t row = 0; row < nrow; ++row) {
  2762. quantize_row_q5_0_impl(src, (block_q5_0*)qrow, n_per_row, quant_weights);
  2763. src += n_per_row;
  2764. qrow += row_size;
  2765. }
  2766. return nrow * row_size;
  2767. }
  2768. static void quantize_row_q5_1_impl(const float * restrict x, block_q5_1 * restrict y, int64_t n_per_row, const float * quant_weights) {
  2769. static_assert(QK5_1 == 32, "QK5_1 must be 32");
  2770. if (!quant_weights) {
  2771. quantize_row_q5_1_reference(x, y, n_per_row);
  2772. return;
  2773. }
  2774. float weight[QK5_1];
  2775. uint8_t L[QK5_1], Laux[QK5_1];
  2776. float sum_x2 = 0;
  2777. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2778. float sigma2 = sum_x2/n_per_row;
  2779. const int64_t nb = n_per_row/QK5_1;
  2780. for (int ib = 0; ib < nb; ++ib) {
  2781. const float * xb = x + QK5_1 * ib;
  2782. const float * qw = quant_weights + QK5_1 * ib;
  2783. for (int j = 0; j < QK5_1; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2784. float min;
  2785. float d = make_qkx3_quants(QK5_1, 31, xb, weight, L, &min, Laux, -0.9f, 0.05f, 36, false);
  2786. y[ib].d = GGML_FP32_TO_FP16(d);
  2787. y[ib].m = GGML_FP32_TO_FP16(-min);
  2788. uint32_t qh = 0;
  2789. for (int j = 0; j < 16; ++j) {
  2790. const uint8_t xi0 = L[j];
  2791. const uint8_t xi1 = L[j+16];
  2792. y[ib].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  2793. // get the 5-th bit and store it in qh at the right position
  2794. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  2795. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_0/2);
  2796. }
  2797. memcpy(&y[ib].qh, &qh, sizeof(qh));
  2798. }
  2799. }
  2800. size_t quantize_q5_1(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2801. if (!quant_weights) {
  2802. quantize_row_q5_1_reference(src, dst, (int64_t)nrow*n_per_row);
  2803. return nrow * ggml_row_size(GGML_TYPE_Q5_1, n_per_row);
  2804. }
  2805. size_t row_size = ggml_row_size(GGML_TYPE_Q5_1, n_per_row);
  2806. char * qrow = (char *)dst;
  2807. for (int64_t row = 0; row < nrow; ++row) {
  2808. quantize_row_q5_1_impl(src, (block_q5_1*)qrow, n_per_row, quant_weights);
  2809. src += n_per_row;
  2810. qrow += row_size;
  2811. }
  2812. return nrow * row_size;
  2813. }
  2814. size_t quantize_q8_0(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2815. (void)quant_weights; // not used
  2816. const size_t row_size = ggml_row_size(GGML_TYPE_Q8_0, n_per_row);
  2817. quantize_row_q8_0_reference(src, dst, (int64_t)nrow*n_per_row);
  2818. return nrow * row_size;
  2819. }
  2820. // ====================== "True" 2-bit (de)-quantization
  2821. void dequantize_row_iq2_xxs(const block_iq2_xxs * restrict x, float * restrict y, int64_t k) {
  2822. assert(k % QK_K == 0);
  2823. const int64_t nb = k / QK_K;
  2824. uint32_t aux32[2];
  2825. const uint8_t * aux8 = (const uint8_t *)aux32;
  2826. for (int i = 0; i < nb; i++) {
  2827. const float d = GGML_FP16_TO_FP32(x[i].d);
  2828. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2829. memcpy(aux32, x[i].qs + 4*ib32, 2*sizeof(uint32_t));
  2830. const float db = d * (0.5f + (aux32[1] >> 28)) * 0.25f;
  2831. for (int l = 0; l < 4; ++l) {
  2832. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  2833. const uint8_t signs = ksigns_iq2xs[(aux32[1] >> 7*l) & 127];
  2834. for (int j = 0; j < 8; ++j) {
  2835. y[j] = db * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  2836. }
  2837. y += 8;
  2838. }
  2839. }
  2840. }
  2841. }
  2842. // ====================== 2.3125 bpw (de)-quantization
  2843. void dequantize_row_iq2_xs(const block_iq2_xs * restrict x, float * restrict y, int64_t k) {
  2844. assert(k % QK_K == 0);
  2845. const int64_t nb = k / QK_K;
  2846. float db[2];
  2847. for (int i = 0; i < nb; i++) {
  2848. const float d = GGML_FP16_TO_FP32(x[i].d);
  2849. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2850. db[0] = d * (0.5f + (x[i].scales[ib32] & 0xf)) * 0.25f;
  2851. db[1] = d * (0.5f + (x[i].scales[ib32] >> 4)) * 0.25f;
  2852. for (int l = 0; l < 4; ++l) {
  2853. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (x[i].qs[4*ib32 + l] & 511));
  2854. const uint8_t signs = ksigns_iq2xs[x[i].qs[4*ib32 + l] >> 9];
  2855. for (int j = 0; j < 8; ++j) {
  2856. y[j] = db[l/2] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  2857. }
  2858. y += 8;
  2859. }
  2860. }
  2861. }
  2862. }
  2863. // ====================== 2.5625 bpw (de)-quantization
  2864. void dequantize_row_iq2_s(const block_iq2_s * restrict x, float * restrict y, int64_t k) {
  2865. assert(k % QK_K == 0);
  2866. const int64_t nb = k / QK_K;
  2867. float db[2];
  2868. for (int i = 0; i < nb; i++) {
  2869. const float d = GGML_FP16_TO_FP32(x[i].d);
  2870. const uint8_t * qs = x[i].qs;
  2871. const uint8_t * qh = x[i].qh;
  2872. const uint8_t * signs = qs + QK_K/8;
  2873. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2874. db[0] = d * (0.5f + (x[i].scales[ib32] & 0xf)) * 0.25f;
  2875. db[1] = d * (0.5f + (x[i].scales[ib32] >> 4)) * 0.25f;
  2876. for (int l = 0; l < 4; ++l) {
  2877. const float dl = db[l/2];
  2878. const uint8_t * grid = (const uint8_t *)(iq2s_grid + (qs[l] | (qh[ib32] << (8-2*l) & 0x300)));
  2879. for (int j = 0; j < 8; ++j) {
  2880. y[j] = dl * grid[j] * (signs[l] & kmask_iq2xs[j] ? -1.f : 1.f);
  2881. }
  2882. y += 8;
  2883. }
  2884. qs += 4;
  2885. signs += 4;
  2886. }
  2887. }
  2888. }
  2889. // ====================== 3.0625 bpw (de)-quantization
  2890. void dequantize_row_iq3_xxs(const block_iq3_xxs * restrict x, float * restrict y, int64_t k) {
  2891. assert(k % QK_K == 0);
  2892. const int64_t nb = k / QK_K;
  2893. uint32_t aux32;
  2894. for (int i = 0; i < nb; i++) {
  2895. const float d = GGML_FP16_TO_FP32(x[i].d);
  2896. const uint8_t * qs = x[i].qs;
  2897. const uint8_t * scales_and_signs = qs + QK_K/4;
  2898. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2899. memcpy(&aux32, scales_and_signs + 4*ib32, sizeof(uint32_t));
  2900. const float db = d * (0.5f + (aux32 >> 28)) * 0.5f;
  2901. for (int l = 0; l < 4; ++l) {
  2902. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*l) & 127];
  2903. const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + qs[2*l+0]);
  2904. const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + qs[2*l+1]);
  2905. for (int j = 0; j < 4; ++j) {
  2906. y[j+0] = db * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  2907. y[j+4] = db * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  2908. }
  2909. y += 8;
  2910. }
  2911. qs += 8;
  2912. }
  2913. }
  2914. }
  2915. // ====================== 3.3125 bpw (de)-quantization
  2916. void dequantize_row_iq3_s(const block_iq3_s * restrict x, float * restrict y, int64_t k) {
  2917. assert(k % QK_K == 0);
  2918. const int64_t nb = k / QK_K;
  2919. for (int i = 0; i < nb; i++) {
  2920. const float d = GGML_FP16_TO_FP32(x[i].d);
  2921. const uint8_t * qs = x[i].qs;
  2922. const uint8_t * qh = x[i].qh;
  2923. const uint8_t * signs = x[i].signs;
  2924. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  2925. const float db1 = d * (1 + 2*(x[i].scales[ib32/2] & 0xf));
  2926. const float db2 = d * (1 + 2*(x[i].scales[ib32/2] >> 4));
  2927. for (int l = 0; l < 4; ++l) {
  2928. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[0] << (8-2*l)) & 256)));
  2929. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[0] << (7-2*l)) & 256)));
  2930. for (int j = 0; j < 4; ++j) {
  2931. y[j+0] = db1 * grid1[j] * (signs[l] & kmask_iq2xs[j+0] ? -1.f : 1.f);
  2932. y[j+4] = db1 * grid2[j] * (signs[l] & kmask_iq2xs[j+4] ? -1.f : 1.f);
  2933. }
  2934. y += 8;
  2935. }
  2936. qs += 8;
  2937. signs += 4;
  2938. for (int l = 0; l < 4; ++l) {
  2939. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[1] << (8-2*l)) & 256)));
  2940. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[1] << (7-2*l)) & 256)));
  2941. for (int j = 0; j < 4; ++j) {
  2942. y[j+0] = db2 * grid1[j] * (signs[l] & kmask_iq2xs[j+0] ? -1.f : 1.f);
  2943. y[j+4] = db2 * grid2[j] * (signs[l] & kmask_iq2xs[j+4] ? -1.f : 1.f);
  2944. }
  2945. y += 8;
  2946. }
  2947. qh += 2;
  2948. qs += 8;
  2949. signs += 4;
  2950. }
  2951. }
  2952. }
  2953. // ====================== 1.5625 bpw (de)-quantization
  2954. void dequantize_row_iq1_s(const block_iq1_s * restrict x, float * restrict y, int64_t k) {
  2955. assert(k % QK_K == 0);
  2956. const int64_t nb = k / QK_K;
  2957. for (int i = 0; i < nb; i++) {
  2958. const float d = GGML_FP16_TO_FP32(x[i].d);
  2959. const uint8_t * qs = x[i].qs;
  2960. const uint16_t * qh = x[i].qh;
  2961. for (int ib = 0; ib < QK_K/32; ++ib) {
  2962. const float dl = d * (2*((qh[ib] >> 12) & 7) + 1);
  2963. const float delta = qh[ib] & 0x8000 ? -IQ1S_DELTA : IQ1S_DELTA;
  2964. for (int l = 0; l < 4; ++l) {
  2965. const int8_t * grid = (const int8_t *)(iq1s_grid + (qs[l] | (((qh[ib] >> 3*l) & 7) << 8)));
  2966. for (int j = 0; j < 8; ++j) {
  2967. y[j] = dl * (grid[j] + delta);
  2968. }
  2969. y += 8;
  2970. }
  2971. qs += 4;
  2972. }
  2973. }
  2974. }
  2975. void dequantize_row_iq1_m(const block_iq1_m * restrict x, float * restrict y, int64_t k) {
  2976. assert(k % QK_K == 0);
  2977. const int64_t nb = k / QK_K;
  2978. float delta[4];
  2979. uint16_t idx[4];
  2980. iq1m_scale_t scale;
  2981. for (int i = 0; i < nb; i++) {
  2982. const uint16_t * sc = (const uint16_t *)x[i].scales;
  2983. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  2984. const float d = GGML_FP16_TO_FP32(scale.f16);
  2985. const uint8_t * qs = x[i].qs;
  2986. const uint8_t * qh = x[i].qh;
  2987. for (int ib = 0; ib < QK_K/32; ++ib) {
  2988. const float dl1 = d * (2*((sc[ib/2] >> (6*(ib%2)+0)) & 0x7) + 1);
  2989. const float dl2 = d * (2*((sc[ib/2] >> (6*(ib%2)+3)) & 0x7) + 1);
  2990. idx[0] = qs[0] | ((qh[0] << 8) & 0x700);
  2991. idx[1] = qs[1] | ((qh[0] << 4) & 0x700);
  2992. idx[2] = qs[2] | ((qh[1] << 8) & 0x700);
  2993. idx[3] = qs[3] | ((qh[1] << 4) & 0x700);
  2994. delta[0] = qh[0] & 0x08 ? -IQ1S_DELTA : IQ1S_DELTA;
  2995. delta[1] = qh[0] & 0x80 ? -IQ1S_DELTA : IQ1S_DELTA;
  2996. delta[2] = qh[1] & 0x08 ? -IQ1S_DELTA : IQ1S_DELTA;
  2997. delta[3] = qh[1] & 0x80 ? -IQ1S_DELTA : IQ1S_DELTA;
  2998. for (int l = 0; l < 2; ++l) {
  2999. const int8_t * grid = (const int8_t *)(iq1s_grid + idx[l]);
  3000. for (int j = 0; j < 8; ++j) {
  3001. y[j] = dl1 * (grid[j] + delta[l]);
  3002. }
  3003. y += 8;
  3004. }
  3005. for (int l = 2; l < 4; ++l) {
  3006. const int8_t * grid = (const int8_t *)(iq1s_grid + idx[l]);
  3007. for (int j = 0; j < 8; ++j) {
  3008. y[j] = dl2 * (grid[j] + delta[l]);
  3009. }
  3010. y += 8;
  3011. }
  3012. qs += 4;
  3013. qh += 2;
  3014. }
  3015. }
  3016. }
  3017. static const int8_t kvalues_iq4nl[16] = {-127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113};
  3018. void dequantize_row_iq4_nl(const block_iq4_nl * restrict x, float * restrict y, int64_t k) {
  3019. assert(k % QK4_NL == 0);
  3020. const int64_t nb = k / QK4_NL;
  3021. for (int i = 0; i < nb; i++) {
  3022. const uint8_t * qs = x[i].qs;
  3023. const float d = GGML_FP16_TO_FP32(x[i].d);
  3024. for (int j = 0; j < QK4_NL/2; ++j) {
  3025. y[j+ 0] = d * kvalues_iq4nl[qs[j] & 0xf];
  3026. y[j+QK4_NL/2] = d * kvalues_iq4nl[qs[j] >> 4];
  3027. }
  3028. y += QK4_NL;
  3029. qs += QK4_NL/2;
  3030. }
  3031. }
  3032. void dequantize_row_iq4_xs(const block_iq4_xs * restrict x, float * restrict y, int64_t k) {
  3033. assert(k % QK_K == 0);
  3034. const int64_t nb = k / QK_K;
  3035. for (int i = 0; i < nb; i++) {
  3036. const uint8_t * qs = x[i].qs;
  3037. const float d = GGML_FP16_TO_FP32(x[i].d);
  3038. for (int ib = 0; ib < QK_K/32; ++ib) {
  3039. const int ls = ((x[i].scales_l[ib/2] >> 4*(ib%2)) & 0xf) | (((x[i].scales_h >> 2*ib) & 3) << 4);
  3040. const float dl = d * (ls - 32);
  3041. for (int j = 0; j < 16; ++j) {
  3042. y[j+ 0] = dl * kvalues_iq4nl[qs[j] & 0xf];
  3043. y[j+16] = dl * kvalues_iq4nl[qs[j] >> 4];
  3044. }
  3045. y += 32;
  3046. qs += 16;
  3047. }
  3048. }
  3049. }
  3050. //===================================== Q8_K ==============================================
  3051. void quantize_row_q8_K_reference(const float * restrict x, block_q8_K * restrict y, int64_t k) {
  3052. assert(k % QK_K == 0);
  3053. const int64_t nb = k / QK_K;
  3054. for (int i = 0; i < nb; i++) {
  3055. float max = 0;
  3056. float amax = 0;
  3057. for (int j = 0; j < QK_K; ++j) {
  3058. float ax = fabsf(x[j]);
  3059. if (ax > amax) {
  3060. amax = ax; max = x[j];
  3061. }
  3062. }
  3063. if (!amax) {
  3064. y[i].d = 0;
  3065. memset(y[i].qs, 0, QK_K);
  3066. x += QK_K;
  3067. continue;
  3068. }
  3069. //const float iscale = -128.f/max;
  3070. // We need this change for IQ2_XXS, else the AVX implementation becomes very awkward
  3071. const float iscale = -127.f/max;
  3072. for (int j = 0; j < QK_K; ++j) {
  3073. int v = nearest_int(iscale*x[j]);
  3074. y[i].qs[j] = MIN(127, v);
  3075. }
  3076. for (int j = 0; j < QK_K/16; ++j) {
  3077. int sum = 0;
  3078. for (int ii = 0; ii < 16; ++ii) {
  3079. sum += y[i].qs[j*16 + ii];
  3080. }
  3081. y[i].bsums[j] = sum;
  3082. }
  3083. y[i].d = 1/iscale;
  3084. x += QK_K;
  3085. }
  3086. }
  3087. void dequantize_row_q8_K(const block_q8_K * restrict x, float * restrict y, int64_t k) {
  3088. assert(k % QK_K == 0);
  3089. const int64_t nb = k / QK_K;
  3090. for (int i = 0; i < nb; i++) {
  3091. for (int j = 0; j < QK_K; ++j) {
  3092. *y++ = x[i].d * x[i].qs[j];
  3093. }
  3094. }
  3095. }
  3096. void quantize_row_q8_K(const float * restrict x, void * restrict y, int64_t k) {
  3097. quantize_row_q8_K_reference(x, y, k);
  3098. }
  3099. //===================================== Dot ptoducts =================================
  3100. //
  3101. // Helper functions
  3102. //
  3103. #if __AVX__ || __AVX2__ || __AVX512F__
  3104. // shuffles to pick the required scales in dot products
  3105. static inline __m256i get_scale_shuffle_q3k(int i) {
  3106. static const uint8_t k_shuffle[128] = {
  3107. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
  3108. 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7,
  3109. 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,
  3110. 12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13, 14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,
  3111. };
  3112. return _mm256_loadu_si256((const __m256i*)k_shuffle + i);
  3113. }
  3114. static inline __m256i get_scale_shuffle_k4(int i) {
  3115. static const uint8_t k_shuffle[256] = {
  3116. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  3117. 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
  3118. 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5,
  3119. 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7,
  3120. 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9,
  3121. 10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,
  3122. 12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,
  3123. 14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15
  3124. };
  3125. return _mm256_loadu_si256((const __m256i*)k_shuffle + i);
  3126. }
  3127. static inline __m128i get_scale_shuffle(int i) {
  3128. static const uint8_t k_shuffle[128] = {
  3129. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
  3130. 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
  3131. 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
  3132. 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7,
  3133. 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,
  3134. 10,10,10,10,10,10,10,10, 11,11,11,11,11,11,11,11,
  3135. 12,12,12,12,12,12,12,12, 13,13,13,13,13,13,13,13,
  3136. 14,14,14,14,14,14,14,14, 15,15,15,15,15,15,15,15
  3137. };
  3138. return _mm_loadu_si128((const __m128i*)k_shuffle + i);
  3139. }
  3140. #elif defined(__loongarch_asx)
  3141. // shuffles to pick the required scales in dot products
  3142. static inline __m256i get_scale_shuffle_q3k(int i) {
  3143. static const uint8_t k_shuffle[128] = {
  3144. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
  3145. 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7,
  3146. 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,
  3147. 12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13, 14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,
  3148. };
  3149. return __lasx_xvld((const __m256i*)k_shuffle + i, 0);
  3150. }
  3151. static inline __m256i get_scale_shuffle_k4(int i) {
  3152. static const uint8_t k_shuffle[256] = {
  3153. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  3154. 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
  3155. 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5,
  3156. 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7,
  3157. 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9,
  3158. 10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,
  3159. 12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,
  3160. 14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15
  3161. };
  3162. return __lasx_xvld((const __m256i*)k_shuffle + i, 0);
  3163. }
  3164. static inline __m128i get_scale_shuffle(int i) {
  3165. static const uint8_t k_shuffle[128] = {
  3166. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
  3167. 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
  3168. 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
  3169. 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7,
  3170. 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,
  3171. 10,10,10,10,10,10,10,10, 11,11,11,11,11,11,11,11,
  3172. 12,12,12,12,12,12,12,12, 13,13,13,13,13,13,13,13,
  3173. 14,14,14,14,14,14,14,14, 15,15,15,15,15,15,15,15
  3174. };
  3175. return __lsx_vld((const __m128i*)k_shuffle + i, 0);
  3176. }
  3177. #endif
  3178. void ggml_vec_dot_q4_0_q8_0(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  3179. const int qk = QK8_0;
  3180. const int nb = n / qk;
  3181. assert(n % qk == 0);
  3182. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3183. assert((nrc == 2) || (nrc == 1));
  3184. #else
  3185. assert(nrc == 1);
  3186. #endif
  3187. UNUSED(nrc);
  3188. UNUSED(bx);
  3189. UNUSED(by);
  3190. UNUSED(bs);
  3191. const block_q4_0 * restrict x = vx;
  3192. const block_q8_0 * restrict y = vy;
  3193. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3194. if (nrc == 2) {
  3195. const block_q4_0 * restrict vx0 = vx;
  3196. const block_q4_0 * restrict vx1 = (const block_q4_0 *) ((const uint8_t*)vx + bx);
  3197. const block_q8_0 * restrict vy0 = vy;
  3198. const block_q8_0 * restrict vy1 = (const block_q8_0 *) ((const uint8_t*)vy + by);
  3199. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3200. for (int i = 0; i < nb; i++) {
  3201. const block_q4_0 * restrict b_x0 = &vx0[i];
  3202. const block_q4_0 * restrict b_x1 = &vx1[i];
  3203. const block_q8_0 * restrict b_y0 = &vy0[i];
  3204. const block_q8_0 * restrict b_y1 = &vy1[i];
  3205. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3206. const int8x16_t s8b = vdupq_n_s8(0x8);
  3207. const uint8x16_t v0_0 = vld1q_u8(b_x0->qs);
  3208. const uint8x16_t v0_1 = vld1q_u8(b_x1->qs);
  3209. // 4-bit -> 8-bit
  3210. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3211. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3212. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3213. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3214. // sub 8
  3215. const int8x16_t x0_l = vsubq_s8(v0_0l, s8b);
  3216. const int8x16_t x0_h = vsubq_s8(v0_0h, s8b);
  3217. const int8x16_t x1_l = vsubq_s8(v0_1l, s8b);
  3218. const int8x16_t x1_h = vsubq_s8(v0_1h, s8b);
  3219. // load y
  3220. const int8x16_t y0_l = vld1q_s8(b_y0->qs);
  3221. const int8x16_t y0_h = vld1q_s8(b_y0->qs + 16);
  3222. const int8x16_t y1_l = vld1q_s8(b_y1->qs);
  3223. const int8x16_t y1_h = vld1q_s8(b_y1->qs + 16);
  3224. float32_t _scale[4] = { GGML_FP16_TO_FP32(b_x0->d)*GGML_FP16_TO_FP32(b_y0->d),
  3225. GGML_FP16_TO_FP32(b_x0->d)*GGML_FP16_TO_FP32(b_y1->d),
  3226. GGML_FP16_TO_FP32(b_x1->d)*GGML_FP16_TO_FP32(b_y0->d),
  3227. GGML_FP16_TO_FP32(b_x1->d)*GGML_FP16_TO_FP32(b_y1->d)};
  3228. float32x4_t scale = vld1q_f32(_scale);
  3229. int8x16_t l0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  3230. int8x16_t l1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  3231. int8x16_t l2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  3232. int8x16_t l3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  3233. int8x16_t r0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  3234. int8x16_t r1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  3235. int8x16_t r2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  3236. int8x16_t r3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  3237. sumv0 = vmlaq_f32(sumv0,(vcvtq_f32_s32(vmmlaq_s32((vmmlaq_s32((vmmlaq_s32((vmmlaq_s32(vdupq_n_s32(0), l0, r0)),
  3238. l1, r1)), l2, r2)), l3, r3))), scale);
  3239. }
  3240. float32x4_t sumv1 = vextq_f32(sumv0, sumv0, 2);
  3241. float32x4_t sumv2 = vzip1q_f32(sumv0, sumv1);
  3242. vst1_f32(s, vget_low_f32(sumv2));
  3243. vst1_f32(s + bs, vget_high_f32(sumv2));
  3244. return;
  3245. }
  3246. #endif
  3247. #if defined(__ARM_FEATURE_SVE)
  3248. const svbool_t ptrueh = svptrue_pat_b8(SV_VL16);
  3249. const svbool_t ptruel = svnot_b_z(svptrue_b8(), ptrueh);
  3250. svfloat32_t sumv0 = svdup_n_f32(0.0f);
  3251. svfloat32_t sumv1 = svdup_n_f32(0.0f);
  3252. assert(nb % 2 == 0); // TODO: handle odd nb
  3253. for (int i = 0; i < nb; i += 2) {
  3254. const block_q4_0 * restrict x0 = &x[i + 0];
  3255. const block_q4_0 * restrict x1 = &x[i + 1];
  3256. const block_q8_0 * restrict y0 = &y[i + 0];
  3257. const block_q8_0 * restrict y1 = &y[i + 1];
  3258. // load x
  3259. const svuint8_t qx0r = svld1rq_u8(svptrue_b8(), x0->qs);
  3260. const svuint8_t qx1r = svld1rq_u8(svptrue_b8(), x1->qs);
  3261. // 4-bit -> 8-bit
  3262. const svint8_t qx0 = svreinterpret_s8_u8(svlsr_n_u8_m(ptruel, svand_n_u8_m(ptrueh, qx0r, 0x0F), 0x04));
  3263. const svint8_t qx1 = svreinterpret_s8_u8(svlsr_n_u8_m(ptruel, svand_n_u8_m(ptrueh, qx1r, 0x0F), 0x04));
  3264. // sub 8
  3265. const svint8_t qx0s = svsub_n_s8_x(svptrue_b8(), qx0, 8);
  3266. const svint8_t qx1s = svsub_n_s8_x(svptrue_b8(), qx1, 8);
  3267. // load y
  3268. const svint8_t qy0 = svld1_s8(svptrue_b8(), y0->qs);
  3269. const svint8_t qy1 = svld1_s8(svptrue_b8(), y1->qs);
  3270. // dot product
  3271. sumv0 = svmla_n_f32_x(svptrue_b32(), sumv0, svcvt_f32_s32_x(svptrue_b32(), svdot_s32(svdup_n_s32(0), qx0s, qy0)), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3272. sumv1 = svmla_n_f32_x(svptrue_b32(), sumv1, svcvt_f32_s32_x(svptrue_b32(), svdot_s32(svdup_n_s32(0), qx1s, qy1)), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3273. }
  3274. *s = svaddv_f32(svptrue_b32(), svadd_f32_x(svptrue_b32(), sumv0, sumv1));
  3275. #elif defined(__ARM_NEON)
  3276. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3277. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3278. assert(nb % 2 == 0); // TODO: handle odd nb
  3279. for (int i = 0; i < nb; i += 2) {
  3280. const block_q4_0 * restrict x0 = &x[i + 0];
  3281. const block_q4_0 * restrict x1 = &x[i + 1];
  3282. const block_q8_0 * restrict y0 = &y[i + 0];
  3283. const block_q8_0 * restrict y1 = &y[i + 1];
  3284. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3285. const int8x16_t s8b = vdupq_n_s8(0x8);
  3286. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3287. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3288. // 4-bit -> 8-bit
  3289. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3290. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3291. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3292. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3293. // sub 8
  3294. const int8x16_t v0_0ls = vsubq_s8(v0_0l, s8b);
  3295. const int8x16_t v0_0hs = vsubq_s8(v0_0h, s8b);
  3296. const int8x16_t v0_1ls = vsubq_s8(v0_1l, s8b);
  3297. const int8x16_t v0_1hs = vsubq_s8(v0_1h, s8b);
  3298. // load y
  3299. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3300. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3301. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3302. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3303. // dot product into int32x4_t
  3304. const int32x4_t p_0 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_0ls, v1_0l), v0_0hs, v1_0h);
  3305. const int32x4_t p_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_1ls, v1_1l), v0_1hs, v1_1h);
  3306. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(p_0), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3307. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(p_1), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3308. }
  3309. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  3310. #elif defined(__AVX2__)
  3311. // Initialize accumulator with zeros
  3312. __m256 acc = _mm256_setzero_ps();
  3313. // Main loop
  3314. for (int i = 0; i < nb; ++i) {
  3315. /* Compute combined scale for the block */
  3316. const __m256 d = _mm256_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3317. __m256i qx = bytes_from_nibbles_32(x[i].qs);
  3318. // Now we have a vector with bytes in [ 0 .. 15 ] interval. Offset them into [ -8 .. +7 ] interval.
  3319. const __m256i off = _mm256_set1_epi8( 8 );
  3320. qx = _mm256_sub_epi8( qx, off );
  3321. __m256i qy = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3322. const __m256 q = mul_sum_i8_pairs_float(qx, qy);
  3323. /* Multiply q with scale and accumulate */
  3324. acc = _mm256_fmadd_ps( d, q, acc );
  3325. }
  3326. *s = hsum_float_8(acc);
  3327. #elif defined(__AVX__)
  3328. // Initialize accumulator with zeros
  3329. __m256 acc = _mm256_setzero_ps();
  3330. // Main loop
  3331. for (int i = 0; i < nb; ++i) {
  3332. // Compute combined scale for the block
  3333. const __m256 d = _mm256_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3334. const __m128i lowMask = _mm_set1_epi8(0xF);
  3335. const __m128i off = _mm_set1_epi8(8);
  3336. const __m128i tmp = _mm_loadu_si128((const __m128i *)x[i].qs);
  3337. __m128i bx_0 = _mm_and_si128(lowMask, tmp);
  3338. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[i].qs);
  3339. bx_0 = _mm_sub_epi8(bx_0, off);
  3340. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3341. bx_0 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp, 4));
  3342. by_0 = _mm_loadu_si128((const __m128i *)(y[i].qs + 16));
  3343. bx_0 = _mm_sub_epi8(bx_0, off);
  3344. const __m128i i32_1 = mul_sum_i8_pairs(bx_0, by_0);
  3345. // Convert int32_t to float
  3346. __m256 p = _mm256_cvtepi32_ps(MM256_SET_M128I(i32_0, i32_1));
  3347. // Apply the scale, and accumulate
  3348. acc = _mm256_add_ps(_mm256_mul_ps( d, p ), acc);
  3349. }
  3350. *s = hsum_float_8(acc);
  3351. #elif defined(__SSSE3__)
  3352. // set constants
  3353. const __m128i lowMask = _mm_set1_epi8(0xF);
  3354. const __m128i off = _mm_set1_epi8(8);
  3355. // Initialize accumulator with zeros
  3356. __m128 acc_0 = _mm_setzero_ps();
  3357. __m128 acc_1 = _mm_setzero_ps();
  3358. __m128 acc_2 = _mm_setzero_ps();
  3359. __m128 acc_3 = _mm_setzero_ps();
  3360. // First round without accumulation
  3361. {
  3362. _mm_prefetch(&x[0] + sizeof(block_q4_0), _MM_HINT_T0);
  3363. _mm_prefetch(&y[0] + sizeof(block_q8_0), _MM_HINT_T0);
  3364. // Compute combined scale for the block 0 and 1
  3365. const __m128 d_0_1 = _mm_set1_ps( GGML_FP16_TO_FP32(x[0].d) * GGML_FP16_TO_FP32(y[0].d) );
  3366. const __m128i tmp_0_1 = _mm_loadu_si128((const __m128i *)x[0].qs);
  3367. __m128i bx_0 = _mm_and_si128(lowMask, tmp_0_1);
  3368. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[0].qs);
  3369. bx_0 = _mm_sub_epi8(bx_0, off);
  3370. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3371. __m128i bx_1 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_0_1, 4));
  3372. __m128i by_1 = _mm_loadu_si128((const __m128i *)(y[0].qs + 16));
  3373. bx_1 = _mm_sub_epi8(bx_1, off);
  3374. const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
  3375. _mm_prefetch(&x[1] + sizeof(block_q4_0), _MM_HINT_T0);
  3376. _mm_prefetch(&y[1] + sizeof(block_q8_0), _MM_HINT_T0);
  3377. // Compute combined scale for the block 2 and 3
  3378. const __m128 d_2_3 = _mm_set1_ps( GGML_FP16_TO_FP32(x[1].d) * GGML_FP16_TO_FP32(y[1].d) );
  3379. const __m128i tmp_2_3 = _mm_loadu_si128((const __m128i *)x[1].qs);
  3380. __m128i bx_2 = _mm_and_si128(lowMask, tmp_2_3);
  3381. __m128i by_2 = _mm_loadu_si128((const __m128i *)y[1].qs);
  3382. bx_2 = _mm_sub_epi8(bx_2, off);
  3383. const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
  3384. __m128i bx_3 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_2_3, 4));
  3385. __m128i by_3 = _mm_loadu_si128((const __m128i *)(y[1].qs + 16));
  3386. bx_3 = _mm_sub_epi8(bx_3, off);
  3387. const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
  3388. // Convert int32_t to float
  3389. __m128 p0 = _mm_cvtepi32_ps(i32_0);
  3390. __m128 p1 = _mm_cvtepi32_ps(i32_1);
  3391. __m128 p2 = _mm_cvtepi32_ps(i32_2);
  3392. __m128 p3 = _mm_cvtepi32_ps(i32_3);
  3393. // Apply the scale
  3394. acc_0 = _mm_mul_ps( d_0_1, p0 );
  3395. acc_1 = _mm_mul_ps( d_0_1, p1 );
  3396. acc_2 = _mm_mul_ps( d_2_3, p2 );
  3397. acc_3 = _mm_mul_ps( d_2_3, p3 );
  3398. }
  3399. assert(nb % 2 == 0); // TODO: handle odd nb
  3400. // Main loop
  3401. for (int i = 2; i < nb; i+=2) {
  3402. _mm_prefetch(&x[i] + sizeof(block_q4_0), _MM_HINT_T0);
  3403. _mm_prefetch(&y[i] + sizeof(block_q8_0), _MM_HINT_T0);
  3404. // Compute combined scale for the block 0 and 1
  3405. const __m128 d_0_1 = _mm_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3406. const __m128i tmp_0_1 = _mm_loadu_si128((const __m128i *)x[i].qs);
  3407. __m128i bx_0 = _mm_and_si128(lowMask, tmp_0_1);
  3408. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[i].qs);
  3409. bx_0 = _mm_sub_epi8(bx_0, off);
  3410. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3411. __m128i bx_1 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_0_1, 4));
  3412. __m128i by_1 = _mm_loadu_si128((const __m128i *)(y[i].qs + 16));
  3413. bx_1 = _mm_sub_epi8(bx_1, off);
  3414. const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
  3415. _mm_prefetch(&x[i] + 2 * sizeof(block_q4_0), _MM_HINT_T0);
  3416. _mm_prefetch(&y[i] + 2 * sizeof(block_q8_0), _MM_HINT_T0);
  3417. // Compute combined scale for the block 2 and 3
  3418. const __m128 d_2_3 = _mm_set1_ps( GGML_FP16_TO_FP32(x[i + 1].d) * GGML_FP16_TO_FP32(y[i + 1].d) );
  3419. const __m128i tmp_2_3 = _mm_loadu_si128((const __m128i *)x[i + 1].qs);
  3420. __m128i bx_2 = _mm_and_si128(lowMask, tmp_2_3);
  3421. __m128i by_2 = _mm_loadu_si128((const __m128i *)y[i + 1].qs);
  3422. bx_2 = _mm_sub_epi8(bx_2, off);
  3423. const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
  3424. __m128i bx_3 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_2_3, 4));
  3425. __m128i by_3 = _mm_loadu_si128((const __m128i *)(y[i + 1].qs + 16));
  3426. bx_3 = _mm_sub_epi8(bx_3, off);
  3427. const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
  3428. // Convert int32_t to float
  3429. __m128 p0 = _mm_cvtepi32_ps(i32_0);
  3430. __m128 p1 = _mm_cvtepi32_ps(i32_1);
  3431. __m128 p2 = _mm_cvtepi32_ps(i32_2);
  3432. __m128 p3 = _mm_cvtepi32_ps(i32_3);
  3433. // Apply the scale
  3434. __m128 p0_d = _mm_mul_ps( d_0_1, p0 );
  3435. __m128 p1_d = _mm_mul_ps( d_0_1, p1 );
  3436. __m128 p2_d = _mm_mul_ps( d_2_3, p2 );
  3437. __m128 p3_d = _mm_mul_ps( d_2_3, p3 );
  3438. // Acummulate
  3439. acc_0 = _mm_add_ps(p0_d, acc_0);
  3440. acc_1 = _mm_add_ps(p1_d, acc_1);
  3441. acc_2 = _mm_add_ps(p2_d, acc_2);
  3442. acc_3 = _mm_add_ps(p3_d, acc_3);
  3443. }
  3444. *s = hsum_float_4x4(acc_0, acc_1, acc_2, acc_3);
  3445. #elif defined(__riscv_v_intrinsic)
  3446. float sumf = 0.0;
  3447. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3448. for (int i = 0; i < nb; i++) {
  3449. // load elements
  3450. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3451. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3452. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3453. // mask and store lower part of x, and then upper part
  3454. vuint8mf2_t x_a = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3455. vuint8mf2_t x_l = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3456. vint8mf2_t x_ai = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3457. vint8mf2_t x_li = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3458. // subtract offset
  3459. vint8mf2_t v0 = __riscv_vsub_vx_i8mf2(x_ai, 8, vl);
  3460. vint8mf2_t v1 = __riscv_vsub_vx_i8mf2(x_li, 8, vl);
  3461. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3462. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3463. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3464. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3465. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3466. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3467. sumf += sumi*GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d);
  3468. }
  3469. *s = sumf;
  3470. #elif defined(__POWER9_VECTOR__)
  3471. const vector signed char lowMask = vec_splats((signed char)0xF);
  3472. const vector signed int v0 = vec_splats((int32_t)0);
  3473. const vector unsigned char v4 = vec_splats((unsigned char)0x4);
  3474. const vector signed char v8 = vec_splats((signed char)0x8);
  3475. vector float vsumf0 = vec_splats(0.0f);
  3476. #pragma GCC unroll 8
  3477. for (int i = 0; i < nb; i++) {
  3478. __builtin_prefetch(x[i].qs, 0, 1);
  3479. __builtin_prefetch(y[i].qs, 0, 1);
  3480. vector float vxd = vec_splats(GGML_FP16_TO_FP32(x[i].d));
  3481. vector float vyd = vec_splats(GGML_FP16_TO_FP32(y[i].d));
  3482. vector float vd = vec_mul(vxd, vyd);
  3483. vector signed char qxs = (vector signed char)vec_xl( 0, x[i].qs);
  3484. vector signed char q8y0 = vec_xl( 0, y[i].qs);
  3485. vector signed char q8y1 = vec_xl(16, y[i].qs);
  3486. vector signed char q4x0 = vec_and(qxs, lowMask);
  3487. vector signed char q4x1 = vec_sr(qxs, v4);
  3488. q4x0 = vec_sub(q4x0, v8);
  3489. q4x1 = vec_sub(q4x1, v8);
  3490. vector signed short qv0 = vec_add(vec_mule(q4x0, q8y0), vec_mulo(q4x0, q8y0));
  3491. vector signed short qv1 = vec_add(vec_mule(q4x1, q8y1), vec_mulo(q4x1, q8y1));
  3492. vector signed int vsumi0 = v0;
  3493. vsumi0 = vec_sum4s(qv0, vsumi0);
  3494. vsumi0 = vec_sum4s(qv1, vsumi0);
  3495. vsumf0 = vec_madd(vec_ctf(vsumi0, 0), vd, vsumf0);
  3496. }
  3497. vsumf0 = vec_add(vsumf0, vec_sld(vsumf0, vsumf0, 4));
  3498. vsumf0 = vec_add(vsumf0, vec_sld(vsumf0, vsumf0, 8));
  3499. *s = vec_extract(vsumf0, 0);
  3500. #elif defined(__loongarch_asx)
  3501. // Initialize accumulator with zeros
  3502. __m256 acc = (__m256)__lasx_xvldi(0);
  3503. // Main loop
  3504. for (int i = 0; i < nb; ++i) {
  3505. /* Compute combined scale for the block */
  3506. const __m256 d = __lasx_xvreplfr2vr_s( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3507. __m256i qx = bytes_from_nibbles_32(x[i].qs);
  3508. // Now we have a vector with bytes in [ 0 .. 15 ] interval. Offset them into [ -8 .. +7 ] interval.
  3509. const __m256i off = __lasx_xvreplgr2vr_b( 8 );
  3510. qx = __lasx_xvsub_b( qx, off );
  3511. __m256i qy = __lasx_xvld((const __m256i *)y[i].qs, 0);
  3512. const __m256 q = mul_sum_i8_pairs_float(qx, qy);
  3513. /* Multiply q with scale and accumulate */
  3514. acc = __lasx_xvfmadd_s( d, q, acc );
  3515. }
  3516. *s = hsum_float_8(acc);
  3517. #elif defined(__loongarch_sx)
  3518. // set constants
  3519. const __m128i low_mask = __lsx_vreplgr2vr_b(0xF);
  3520. const __m128i off = __lsx_vreplgr2vr_b(8);
  3521. // Initialize accumulator with zeros
  3522. __m128 acc_0 = __lsx_vldi(0);
  3523. __m128 acc_1 = __lsx_vldi(0);
  3524. __m128 acc_2 = __lsx_vldi(0);
  3525. __m128 acc_3 = __lsx_vldi(0);
  3526. // First round without accumulation
  3527. {
  3528. _mm_prefetch(&x[0] + sizeof(block_q4_0), _MM_HINT_T0);
  3529. _mm_prefetch(&y[0] + sizeof(block_q8_0), _MM_HINT_T0);
  3530. // Compute combined scale for the block 0 and 1
  3531. const __m128 d_0_1 = __lsx_vreplgr2vr_w( GGML_FP16_TO_FP32(x[0].d) * GGML_FP16_TO_FP32(y[0].d) );
  3532. const __m128i tmp_0_1 = __lsx_vld((const __m128i *)x[0].qs, 0);
  3533. __m128i bx_0 = __lsx_vand_v(low_mask, tmp_0_1);
  3534. __m128i by_0 = __lsx_vld((const __m128i *)y[0].qs, 0);
  3535. bx_0 = __lsx_vsub_b(bx_0, off);
  3536. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3537. __m128i bx_1 = __lsx_vand_v(low_mask, __lsx_vsrli_d(tmp_0_1, 4));
  3538. __m128i by_1 = __lsx_vld((const __m128i *)(y[0].qs + 16), 0);
  3539. bx_1 = __lsx_vsub_b(bx_1, off);
  3540. const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
  3541. // Compute combined scale for the block 2 and 3
  3542. const __m128 d_2_3 = __lsx_vreplgr2vr_w( GGML_FP16_TO_FP32(x[1].d) * GGML_FP16_TO_FP32(y[1].d) );
  3543. const __m128i tmp_2_3 = __lsx_vld((const __m128i *)x[1].qs, 0);
  3544. __m128i bx_2 = __lsx_vand_v(low_mask, tmp_2_3);
  3545. __m128i by_2 = __lsx_vld((const __m128i *)y[1].qs, 0);
  3546. bx_2 = __lsx_vsub_b(bx_2, off);
  3547. const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
  3548. __m128i bx_3 = __lsx_vand_v(low_mask, __lsx_vsrli_d(tmp_2_3, 4));
  3549. __m128i by_3 = __lsx_vld((const __m128i *)(y[1].qs + 16), 0);
  3550. bx_3 = __lsx_vsub_b(bx_3, off);
  3551. const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
  3552. // Convert int32_t to float
  3553. __m128 p0 = __lsx_vffint_s_w(i32_0);
  3554. __m128 p1 = __lsx_vffint_s_w(i32_1);
  3555. __m128 p2 = __lsx_vffint_s_w(i32_2);
  3556. __m128 p3 = __lsx_vffint_s_w(i32_3);
  3557. // Apply the scale
  3558. acc_0 = __lsx_vfmul_s( d_0_1, p0 );
  3559. acc_1 = __lsx_vfmul_s( d_0_1, p1 );
  3560. acc_2 = __lsx_vfmul_s( d_2_3, p2 );
  3561. acc_3 = __lsx_vfmul_s( d_2_3, p3 );
  3562. }
  3563. assert(nb % 2 == 0); // TODO: handle odd nb
  3564. // Main loop
  3565. for (int i = 2; i < nb; i+=2) {
  3566. // Compute combined scale for the block 0 and 1
  3567. const __m128 d_0_1 = __lsx_vreplgr2vr_w( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3568. const __m128i tmp_0_1 = __lsx_vld((const __m128i *)x[i].qs, 0);
  3569. __m128i bx_0 = __lsx_vand_v(low_mask, tmp_0_1);
  3570. __m128i by_0 = __lsx_vld((const __m128i *)y[i].qs, 0);
  3571. bx_0 = __lsx_vsub_b(bx_0, off);
  3572. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3573. __m128i bx_1 = __lsx_vand_v(low_mask, __lsx_vsrli_d(tmp_0_1, 4));
  3574. __m128i by_1 = __lsx_vld((const __m128i *)(y[i].qs + 16), 0);
  3575. bx_1 = __lsx_vsub_b(bx_1, off);
  3576. const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
  3577. //_mm_prefetch(&x[i] + 2 * sizeof(block_q4_0), _MM_HINT_T0);
  3578. //_mm_prefetch(&y[i] + 2 * sizeof(block_q8_0), _MM_HINT_T0);
  3579. // Compute combined scale for the block 2 and 3
  3580. const __m128 d_2_3 = __lsx_vreplgr2vr_w( GGML_FP16_TO_FP32(x[i + 1].d) * GGML_FP16_TO_FP32(y[i + 1].d) );
  3581. const __m128i tmp_2_3 = __lsx_vld((const __m128i *)x[i + 1].qs, 0);
  3582. __m128i bx_2 = __lsx_vand_v(low_mask, tmp_2_3);
  3583. __m128i by_2 = __lsx_vld((const __m128i *)y[i + 1].qs, 0);
  3584. bx_2 = __lsx_vsub_b(bx_2, off);
  3585. const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
  3586. __m128i bx_3 = __lsx_vand_v(low_mask, __lsx_vsrli_d(tmp_2_3, 4));
  3587. __m128i by_3 = __lsx_vld((const __m128i *)(y[i + 1].qs + 16), 0);
  3588. bx_3 = __lsx_vsub_b(bx_3, off);
  3589. const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
  3590. // Convert int32_t to float
  3591. __m128 p0 = __lsx_vffint_s_w(i32_0);
  3592. __m128 p1 = __lsx_vffint_s_w(i32_1);
  3593. __m128 p2 = __lsx_vffint_s_w(i32_2);
  3594. __m128 p3 = __lsx_vffint_s_w(i32_3);
  3595. // Apply the scale
  3596. __m128 p0_d = __lsx_vfmul_s( d_0_1, p0 );
  3597. __m128 p1_d = __lsx_vfmul_s( d_0_1, p1 );
  3598. __m128 p2_d = __lsx_vfmul_s( d_2_3, p2 );
  3599. __m128 p3_d = __lsx_vfmul_s( d_2_3, p3 );
  3600. // Acummulate
  3601. acc_0 = __lsx_vfadd_s(p0_d, acc_0);
  3602. acc_1 = __lsx_vfadd_s(p1_d, acc_1);
  3603. acc_2 = __lsx_vfadd_s(p2_d, acc_2);
  3604. acc_3 = __lsx_vfadd_s(p3_d, acc_3);
  3605. }
  3606. *s = hsum_float_4x4(acc_0, acc_1, acc_2, acc_3);
  3607. #else
  3608. // scalar
  3609. float sumf = 0.0;
  3610. for (int i = 0; i < nb; i++) {
  3611. int sumi = 0;
  3612. for (int j = 0; j < qk/2; ++j) {
  3613. const int v0 = (x[i].qs[j] & 0x0F) - 8;
  3614. const int v1 = (x[i].qs[j] >> 4) - 8;
  3615. sumi += (v0 * y[i].qs[j]) + (v1 * y[i].qs[j + qk/2]);
  3616. }
  3617. sumf += sumi*GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d);
  3618. }
  3619. *s = sumf;
  3620. #endif
  3621. }
  3622. void ggml_vec_dot_q4_1_q8_1(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  3623. const int qk = QK8_1;
  3624. const int nb = n / qk;
  3625. assert(n % qk == 0);
  3626. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3627. assert((nrc == 2) || (nrc == 1));
  3628. #else
  3629. assert(nrc == 1);
  3630. #endif
  3631. UNUSED(nrc);
  3632. UNUSED(bx);
  3633. UNUSED(by);
  3634. UNUSED(bs);
  3635. const block_q4_1 * restrict x = vx;
  3636. const block_q8_1 * restrict y = vy;
  3637. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3638. if (nrc == 2) {
  3639. const block_q4_1 * restrict vx0 = vx;
  3640. const block_q4_1 * restrict vx1 = (const block_q4_1 *) ((const uint8_t*)vx + bx);
  3641. const block_q8_1 * restrict vy0 = vy;
  3642. const block_q8_1 * restrict vy1 = (const block_q8_1 *) ((const uint8_t*)vy + by);
  3643. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3644. float32x4_t summs0 = vdupq_n_f32(0.0f);
  3645. for (int i = 0; i < nb; i++) {
  3646. const block_q4_1 * restrict b_x0 = &vx0[i];
  3647. const block_q4_1 * restrict b_x1 = &vx1[i];
  3648. const block_q8_1 * restrict b_y0 = &vy0[i];
  3649. const block_q8_1 * restrict b_y1 = &vy1[i];
  3650. float32_t summs_t[4] = {GGML_FP16_TO_FP32(b_x0->m) * GGML_FP16_TO_FP32(b_y0->s),
  3651. GGML_FP16_TO_FP32(b_x1->m) * GGML_FP16_TO_FP32(b_y0->s),
  3652. GGML_FP16_TO_FP32(b_x0->m) * GGML_FP16_TO_FP32(b_y1->s),
  3653. GGML_FP16_TO_FP32(b_x1->m) * GGML_FP16_TO_FP32(b_y1->s)};
  3654. summs0 = vaddq_f32(summs0, vld1q_f32(summs_t));
  3655. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3656. const uint8x16_t v0_0 = vld1q_u8(b_x0->qs);
  3657. const uint8x16_t v0_1 = vld1q_u8(b_x1->qs);
  3658. // 4-bit -> 8-bit
  3659. const int8x16_t x0_l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3660. const int8x16_t x0_h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3661. const int8x16_t x1_l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3662. const int8x16_t x1_h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3663. // load y
  3664. const int8x16_t y0_l = vld1q_s8(b_y0->qs);
  3665. const int8x16_t y0_h = vld1q_s8(b_y0->qs + 16);
  3666. const int8x16_t y1_l = vld1q_s8(b_y1->qs);
  3667. const int8x16_t y1_h = vld1q_s8(b_y1->qs + 16);
  3668. // mmla into int32x4_t
  3669. float32_t _scale[4] = {GGML_FP16_TO_FP32(b_x0->d)*b_y0->d,
  3670. GGML_FP16_TO_FP32(b_x0->d)*b_y1->d,
  3671. GGML_FP16_TO_FP32(b_x1->d)*b_y0->d,
  3672. GGML_FP16_TO_FP32(b_x1->d)*b_y1->d};
  3673. float32x4_t scale = vld1q_f32(_scale);
  3674. int8x16_t l0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  3675. int8x16_t l1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  3676. int8x16_t l2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  3677. int8x16_t l3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  3678. int8x16_t r0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  3679. int8x16_t r1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  3680. int8x16_t r2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  3681. int8x16_t r3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  3682. sumv0 = vmlaq_f32(sumv0,(vcvtq_f32_s32(vmmlaq_s32((vmmlaq_s32((vmmlaq_s32((vmmlaq_s32(vdupq_n_s32(0), l0, r0)),
  3683. l1, r1)), l2, r2)), l3, r3))), scale);
  3684. }
  3685. float32x4_t sumv1 = vextq_f32(sumv0, sumv0, 2);
  3686. float32x4_t sumv2 = vzip1q_f32(sumv0, sumv1);
  3687. sumv2 = vaddq_f32(sumv2, summs0);
  3688. vst1_f32(s, vget_low_f32(sumv2));
  3689. vst1_f32(s + bs, vget_high_f32(sumv2));
  3690. return;
  3691. }
  3692. #endif
  3693. // TODO: add WASM SIMD
  3694. #if defined(__ARM_NEON)
  3695. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3696. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3697. float summs = 0;
  3698. assert(nb % 2 == 0); // TODO: handle odd nb
  3699. for (int i = 0; i < nb; i += 2) {
  3700. const block_q4_1 * restrict x0 = &x[i + 0];
  3701. const block_q4_1 * restrict x1 = &x[i + 1];
  3702. const block_q8_1 * restrict y0 = &y[i + 0];
  3703. const block_q8_1 * restrict y1 = &y[i + 1];
  3704. summs += GGML_FP16_TO_FP32(x0->m) * GGML_FP16_TO_FP32(y0->s) + GGML_FP16_TO_FP32(x1->m) * GGML_FP16_TO_FP32(y1->s);
  3705. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3706. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3707. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3708. // 4-bit -> 8-bit
  3709. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3710. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3711. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3712. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3713. // load y
  3714. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3715. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3716. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3717. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3718. // dot product into int32x4_t
  3719. const int32x4_t p_0 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_0l, v1_0l), v0_0h, v1_0h);
  3720. const int32x4_t p_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_1l, v1_1l), v0_1h, v1_1h);
  3721. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(p_0), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3722. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(p_1), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3723. }
  3724. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1) + summs;
  3725. #elif defined(__AVX2__) || defined(__AVX__)
  3726. // Initialize accumulator with zeros
  3727. __m256 acc = _mm256_setzero_ps();
  3728. float summs = 0;
  3729. // Main loop
  3730. for (int i = 0; i < nb; ++i) {
  3731. const float d0 = GGML_FP16_TO_FP32(x[i].d);
  3732. const float d1 = GGML_FP16_TO_FP32(y[i].d);
  3733. summs += GGML_FP16_TO_FP32(x[i].m) * GGML_FP16_TO_FP32(y[i].s);
  3734. const __m256 d0v = _mm256_set1_ps( d0 );
  3735. const __m256 d1v = _mm256_set1_ps( d1 );
  3736. // Compute combined scales
  3737. const __m256 d0d1 = _mm256_mul_ps( d0v, d1v );
  3738. // Load 16 bytes, and unpack 4 bit fields into bytes, making 32 bytes
  3739. const __m256i qx = bytes_from_nibbles_32(x[i].qs);
  3740. const __m256i qy = _mm256_loadu_si256( (const __m256i *)y[i].qs );
  3741. const __m256 xy = mul_sum_us8_pairs_float(qx, qy);
  3742. // Accumulate d0*d1*x*y
  3743. #if defined(__AVX2__)
  3744. acc = _mm256_fmadd_ps( d0d1, xy, acc );
  3745. #else
  3746. acc = _mm256_add_ps( _mm256_mul_ps( d0d1, xy ), acc );
  3747. #endif
  3748. }
  3749. *s = hsum_float_8(acc) + summs;
  3750. #elif defined(__riscv_v_intrinsic)
  3751. float sumf = 0.0;
  3752. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3753. for (int i = 0; i < nb; i++) {
  3754. // load elements
  3755. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3756. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3757. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3758. // mask and store lower part of x, and then upper part
  3759. vuint8mf2_t x_a = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3760. vuint8mf2_t x_l = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3761. vint8mf2_t v0 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3762. vint8mf2_t v1 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3763. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3764. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3765. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3766. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3767. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3768. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3769. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d))*sumi + GGML_FP16_TO_FP32(x[i].m)*GGML_FP16_TO_FP32(y[i].s);
  3770. }
  3771. *s = sumf;
  3772. #elif defined(__POWER9_VECTOR__)
  3773. const vector signed char lowMask = vec_splats((signed char)0xF);
  3774. const vector signed int v0 = vec_splats((int32_t)0);
  3775. const vector unsigned char v4 = vec_splats((unsigned char)0x4);
  3776. vector float vsumf0 = vec_splats(0.0f);
  3777. #pragma GCC unroll 4
  3778. for (int i = 0; i < nb; i++) {
  3779. __builtin_prefetch(x[i].qs, 0, 1);
  3780. __builtin_prefetch(y[i].qs, 0, 1);
  3781. vector float vxd = vec_splats(GGML_FP16_TO_FP32(x[i].d));
  3782. vector float vyd = vec_splats(GGML_FP16_TO_FP32(y[i].d));
  3783. vector float vd = vec_mul(vxd, vyd);
  3784. vector float vxmin = vec_splats(GGML_FP16_TO_FP32(x[i].m));
  3785. vector float vys = {GGML_FP16_TO_FP32(y[i].s), 0.0f, 0.0f, 0.0f};
  3786. vsumf0 = vec_madd(vxmin, vys, vsumf0);
  3787. vector signed char qxs = (vector signed char)vec_xl( 0, x[i].qs);
  3788. vector signed char q8y0 = vec_xl( 0, y[i].qs);
  3789. vector signed char q8y1 = vec_xl(16, y[i].qs);
  3790. vector unsigned char q4x0 = (vector unsigned char)vec_and(qxs, lowMask);
  3791. vector unsigned char q4x1 = (vector unsigned char)vec_sr(qxs, v4);
  3792. vector signed int vsumi0 = v0;
  3793. vsumi0 = vec_msum(q8y0, q4x0, vsumi0);
  3794. vsumi0 = vec_msum(q8y1, q4x1, vsumi0);
  3795. vsumf0 = vec_madd(vec_ctf(vsumi0, 0), vd, vsumf0);
  3796. }
  3797. vsumf0 = vec_add(vsumf0, vec_sld(vsumf0, vsumf0, 4));
  3798. vsumf0 = vec_add(vsumf0, vec_sld(vsumf0, vsumf0, 8));
  3799. *s = vec_extract(vsumf0, 0);
  3800. #elif defined(__loongarch_asx)
  3801. // Initialize accumulator with zeros
  3802. __m256 acc = (__m256)__lasx_xvldi(0);
  3803. float summs = 0;
  3804. // Main loop
  3805. for (int i = 0; i < nb; ++i) {
  3806. const float d0 = GGML_FP16_TO_FP32(x[i].d);
  3807. const float d1 = GGML_FP16_TO_FP32(y[i].d);
  3808. summs += GGML_FP16_TO_FP32(x[i].m) * GGML_FP16_TO_FP32(y[i].s);
  3809. const __m256 d0v = __lasx_xvreplfr2vr_s( d0 );
  3810. const __m256 d1v = __lasx_xvreplfr2vr_s( d1 );
  3811. // Compute combined scales
  3812. const __m256 d0d1 = __lasx_xvfmul_s( d0v, d1v );
  3813. // Load 16 bytes, and unpack 4 bit fields into bytes, making 32 bytes
  3814. const __m256i qx = bytes_from_nibbles_32(x[i].qs);
  3815. const __m256i qy = __lasx_xvld( (const __m256i *)y[i].qs, 0);
  3816. const __m256 xy = mul_sum_us8_pairs_float(qx, qy);
  3817. // Accumulate d0*d1*x*y
  3818. acc = __lasx_xvfmadd_s( d0d1, xy, acc );
  3819. }
  3820. *s = hsum_float_8(acc) + summs;
  3821. #else
  3822. // scalar
  3823. float sumf = 0.0;
  3824. for (int i = 0; i < nb; i++) {
  3825. int sumi = 0;
  3826. for (int j = 0; j < qk/2; ++j) {
  3827. const int v0 = (x[i].qs[j] & 0x0F);
  3828. const int v1 = (x[i].qs[j] >> 4);
  3829. sumi += (v0 * y[i].qs[j]) + (v1 * y[i].qs[j + qk/2]);
  3830. }
  3831. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d))*sumi + GGML_FP16_TO_FP32(x[i].m)*GGML_FP16_TO_FP32(y[i].s);
  3832. }
  3833. *s = sumf;
  3834. #endif
  3835. }
  3836. void ggml_vec_dot_q5_0_q8_0(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  3837. const int qk = QK8_0;
  3838. const int nb = n / qk;
  3839. assert(n % qk == 0);
  3840. assert(qk == QK5_0);
  3841. assert(nrc == 1);
  3842. UNUSED(nrc);
  3843. UNUSED(bx);
  3844. UNUSED(by);
  3845. UNUSED(bs);
  3846. const block_q5_0 * restrict x = vx;
  3847. const block_q8_0 * restrict y = vy;
  3848. #if defined(__ARM_NEON)
  3849. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3850. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3851. uint32_t qh0;
  3852. uint32_t qh1;
  3853. uint64_t tmp0[4];
  3854. uint64_t tmp1[4];
  3855. assert(nb % 2 == 0); // TODO: handle odd nb
  3856. for (int i = 0; i < nb; i += 2) {
  3857. const block_q5_0 * restrict x0 = &x[i];
  3858. const block_q5_0 * restrict x1 = &x[i + 1];
  3859. const block_q8_0 * restrict y0 = &y[i];
  3860. const block_q8_0 * restrict y1 = &y[i + 1];
  3861. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3862. // extract the 5th bit via lookup table ((!b) << 4)
  3863. memcpy(&qh0, x0->qh, sizeof(qh0));
  3864. memcpy(&qh1, x1->qh, sizeof(qh1));
  3865. tmp0[0] = table_b2b_1[(qh0 >> 0) & 0xFF];
  3866. tmp0[1] = table_b2b_1[(qh0 >> 8) & 0xFF];
  3867. tmp0[2] = table_b2b_1[(qh0 >> 16) & 0xFF];
  3868. tmp0[3] = table_b2b_1[(qh0 >> 24) ];
  3869. tmp1[0] = table_b2b_1[(qh1 >> 0) & 0xFF];
  3870. tmp1[1] = table_b2b_1[(qh1 >> 8) & 0xFF];
  3871. tmp1[2] = table_b2b_1[(qh1 >> 16) & 0xFF];
  3872. tmp1[3] = table_b2b_1[(qh1 >> 24) ];
  3873. const int8x16_t qhl0 = vld1q_s8((const int8_t *)(tmp0 + 0));
  3874. const int8x16_t qhh0 = vld1q_s8((const int8_t *)(tmp0 + 2));
  3875. const int8x16_t qhl1 = vld1q_s8((const int8_t *)(tmp1 + 0));
  3876. const int8x16_t qhh1 = vld1q_s8((const int8_t *)(tmp1 + 2));
  3877. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3878. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3879. // 4-bit -> 8-bit
  3880. int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3881. int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3882. int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3883. int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3884. // add high bit and sub 16 (equivalent to sub 0x10 when bit is zero)
  3885. const int8x16_t v0_0lf = vsubq_s8(v0_0l, qhl0);
  3886. const int8x16_t v0_0hf = vsubq_s8(v0_0h, qhh0);
  3887. const int8x16_t v0_1lf = vsubq_s8(v0_1l, qhl1);
  3888. const int8x16_t v0_1hf = vsubq_s8(v0_1h, qhh1);
  3889. // load y
  3890. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3891. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3892. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3893. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3894. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3895. ggml_vdotq_s32(vdupq_n_s32(0), v0_0lf, v1_0l),
  3896. ggml_vdotq_s32(vdupq_n_s32(0), v0_0hf, v1_0h))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3897. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3898. ggml_vdotq_s32(vdupq_n_s32(0), v0_1lf, v1_1l),
  3899. ggml_vdotq_s32(vdupq_n_s32(0), v0_1hf, v1_1h))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3900. }
  3901. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  3902. #elif defined(__wasm_simd128__)
  3903. v128_t sumv = wasm_f32x4_splat(0.0f);
  3904. uint32_t qh;
  3905. uint64_t tmp[4];
  3906. // TODO: check if unrolling this is better
  3907. for (int i = 0; i < nb; ++i) {
  3908. const block_q5_0 * restrict x0 = &x[i];
  3909. const block_q8_0 * restrict y0 = &y[i];
  3910. const v128_t m4b = wasm_i8x16_splat(0x0F);
  3911. // extract the 5th bit
  3912. memcpy(&qh, x0->qh, sizeof(qh));
  3913. tmp[0] = table_b2b_1[(qh >> 0) & 0xFF];
  3914. tmp[1] = table_b2b_1[(qh >> 8) & 0xFF];
  3915. tmp[2] = table_b2b_1[(qh >> 16) & 0xFF];
  3916. tmp[3] = table_b2b_1[(qh >> 24) ];
  3917. const v128_t qhl = wasm_v128_load(tmp + 0);
  3918. const v128_t qhh = wasm_v128_load(tmp + 2);
  3919. const v128_t v0 = wasm_v128_load(x0->qs);
  3920. // 4-bit -> 8-bit
  3921. const v128_t v0l = wasm_v128_and (v0, m4b);
  3922. const v128_t v0h = wasm_u8x16_shr(v0, 4);
  3923. // add high bit and sub 16 (equivalent to sub 0x10 when bit is zero)
  3924. const v128_t v0lf = wasm_i8x16_sub(v0l, qhl);
  3925. const v128_t v0hf = wasm_i8x16_sub(v0h, qhh);
  3926. // load y
  3927. const v128_t v1l = wasm_v128_load(y0->qs);
  3928. const v128_t v1h = wasm_v128_load(y0->qs + 16);
  3929. // int8x16 -> int16x8
  3930. const v128_t v0lfl = wasm_i16x8_extend_low_i8x16 (v0lf);
  3931. const v128_t v0lfh = wasm_i16x8_extend_high_i8x16(v0lf);
  3932. const v128_t v0hfl = wasm_i16x8_extend_low_i8x16 (v0hf);
  3933. const v128_t v0hfh = wasm_i16x8_extend_high_i8x16(v0hf);
  3934. const v128_t v1ll = wasm_i16x8_extend_low_i8x16 (v1l);
  3935. const v128_t v1lh = wasm_i16x8_extend_high_i8x16(v1l);
  3936. const v128_t v1hl = wasm_i16x8_extend_low_i8x16 (v1h);
  3937. const v128_t v1hh = wasm_i16x8_extend_high_i8x16(v1h);
  3938. // dot product
  3939. sumv = wasm_f32x4_add(sumv, wasm_f32x4_mul(wasm_f32x4_convert_i32x4(
  3940. wasm_i32x4_add(
  3941. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0lfl, v1ll),
  3942. wasm_i32x4_dot_i16x8(v0lfh, v1lh)),
  3943. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0hfl, v1hl),
  3944. wasm_i32x4_dot_i16x8(v0hfh, v1hh)))),
  3945. wasm_f32x4_splat(GGML_FP16_TO_FP32(x0->d) * GGML_FP16_TO_FP32(y0->d))));
  3946. }
  3947. *s = wasm_f32x4_extract_lane(sumv, 0) + wasm_f32x4_extract_lane(sumv, 1) +
  3948. wasm_f32x4_extract_lane(sumv, 2) + wasm_f32x4_extract_lane(sumv, 3);
  3949. #elif defined(__AVX2__)
  3950. // Initialize accumulator with zeros
  3951. __m256 acc = _mm256_setzero_ps();
  3952. // Main loop
  3953. for (int i = 0; i < nb; i++) {
  3954. /* Compute combined scale for the block */
  3955. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3956. __m256i qx = bytes_from_nibbles_32(x[i].qs);
  3957. __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3958. bxhi = _mm256_andnot_si256(bxhi, _mm256_set1_epi8((char)0xF0));
  3959. qx = _mm256_or_si256(qx, bxhi);
  3960. __m256i qy = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3961. const __m256 q = mul_sum_i8_pairs_float(qx, qy);
  3962. /* Multiply q with scale and accumulate */
  3963. acc = _mm256_fmadd_ps(d, q, acc);
  3964. }
  3965. *s = hsum_float_8(acc);
  3966. #elif defined(__AVX__)
  3967. // Initialize accumulator with zeros
  3968. __m256 acc = _mm256_setzero_ps();
  3969. __m128i mask = _mm_set1_epi8((char)0xF0);
  3970. // Main loop
  3971. for (int i = 0; i < nb; i++) {
  3972. /* Compute combined scale for the block */
  3973. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3974. __m256i bx_0 = bytes_from_nibbles_32(x[i].qs);
  3975. const __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3976. __m128i bxhil = _mm256_castsi256_si128(bxhi);
  3977. __m128i bxhih = _mm256_extractf128_si256(bxhi, 1);
  3978. bxhil = _mm_andnot_si128(bxhil, mask);
  3979. bxhih = _mm_andnot_si128(bxhih, mask);
  3980. __m128i bxl = _mm256_castsi256_si128(bx_0);
  3981. __m128i bxh = _mm256_extractf128_si256(bx_0, 1);
  3982. bxl = _mm_or_si128(bxl, bxhil);
  3983. bxh = _mm_or_si128(bxh, bxhih);
  3984. bx_0 = MM256_SET_M128I(bxh, bxl);
  3985. const __m256i by_0 = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3986. const __m256 q = mul_sum_i8_pairs_float(bx_0, by_0);
  3987. /* Multiply q with scale and accumulate */
  3988. acc = _mm256_add_ps(_mm256_mul_ps(d, q), acc);
  3989. }
  3990. *s = hsum_float_8(acc);
  3991. #elif defined(__riscv_v_intrinsic)
  3992. float sumf = 0.0;
  3993. uint32_t qh;
  3994. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3995. // These temporary registers are for masking and shift operations
  3996. vuint32m2_t vt_1 = __riscv_vid_v_u32m2(vl);
  3997. vuint32m2_t vt_2 = __riscv_vsll_vv_u32m2(__riscv_vmv_v_x_u32m2(1, vl), vt_1, vl);
  3998. vuint32m2_t vt_3 = __riscv_vsll_vx_u32m2(vt_2, 16, vl);
  3999. vuint32m2_t vt_4 = __riscv_vadd_vx_u32m2(vt_1, 12, vl);
  4000. for (int i = 0; i < nb; i++) {
  4001. memcpy(&qh, x[i].qh, sizeof(uint32_t));
  4002. // ((qh & (1u << (j + 0 ))) >> (j + 0 )) << 4;
  4003. vuint32m2_t xha_0 = __riscv_vand_vx_u32m2(vt_2, qh, vl);
  4004. vuint32m2_t xhr_0 = __riscv_vsrl_vv_u32m2(xha_0, vt_1, vl);
  4005. vuint32m2_t xhl_0 = __riscv_vsll_vx_u32m2(xhr_0, 4, vl);
  4006. // ((qh & (1u << (j + 16))) >> (j + 12));
  4007. vuint32m2_t xha_1 = __riscv_vand_vx_u32m2(vt_3, qh, vl);
  4008. vuint32m2_t xhl_1 = __riscv_vsrl_vv_u32m2(xha_1, vt_4, vl);
  4009. // narrowing
  4010. vuint16m1_t xhc_0 = __riscv_vncvt_x_x_w_u16m1(xhl_0, vl);
  4011. vuint8mf2_t xh_0 = __riscv_vncvt_x_x_w_u8mf2(xhc_0, vl);
  4012. vuint16m1_t xhc_1 = __riscv_vncvt_x_x_w_u16m1(xhl_1, vl);
  4013. vuint8mf2_t xh_1 = __riscv_vncvt_x_x_w_u8mf2(xhc_1, vl);
  4014. // load
  4015. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  4016. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  4017. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  4018. vuint8mf2_t x_at = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  4019. vuint8mf2_t x_lt = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  4020. vuint8mf2_t x_a = __riscv_vor_vv_u8mf2(x_at, xh_0, vl);
  4021. vuint8mf2_t x_l = __riscv_vor_vv_u8mf2(x_lt, xh_1, vl);
  4022. vint8mf2_t x_ai = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  4023. vint8mf2_t x_li = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  4024. vint8mf2_t v0 = __riscv_vsub_vx_i8mf2(x_ai, 16, vl);
  4025. vint8mf2_t v1 = __riscv_vsub_vx_i8mf2(x_li, 16, vl);
  4026. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  4027. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  4028. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  4029. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  4030. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  4031. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  4032. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d)) * sumi;
  4033. }
  4034. *s = sumf;
  4035. #elif defined(__POWER9_VECTOR__)
  4036. const vector signed char lowMask = vec_splats((signed char)0xF);
  4037. const vector unsigned char v4 = vec_splats((unsigned char)4);
  4038. vector float vsumf0 = vec_splats(0.0f);
  4039. #pragma GCC unroll 4
  4040. for (int i = 0; i < nb; ++i) {
  4041. __builtin_prefetch(x[i].qs, 0, 1);
  4042. __builtin_prefetch(y[i].qs, 0, 1);
  4043. vector float vxd = vec_splats(GGML_FP16_TO_FP32(x[i].d));
  4044. vector float vyd = vec_splats(GGML_FP16_TO_FP32(y[i].d));
  4045. vector float vd = vec_mul(vxd, vyd);
  4046. vector signed long long aux64x2_0 = {(uint64_t)(table_b2b_1[x[i].qh[0]]), (uint64_t)(table_b2b_1[x[i].qh[1]])};
  4047. vector signed long long aux64x2_1 = {(uint64_t)(table_b2b_1[x[i].qh[2]]), (uint64_t)(table_b2b_1[x[i].qh[3]])};
  4048. vector signed char qh0 = (vector signed char)aux64x2_0;
  4049. vector signed char qh1 = (vector signed char)aux64x2_1;
  4050. vector signed char qxs = (vector signed char)vec_xl( 0, x[i].qs);
  4051. vector signed char q5x0 = vec_sub(vec_and (qxs, lowMask), qh0);
  4052. vector signed char q5x1 = vec_sub(vec_sr(qxs, v4), qh1);
  4053. vector signed char q8y0 = vec_xl( 0, y[i].qs);
  4054. vector signed char q8y1 = vec_xl( 16, y[i].qs);
  4055. vector signed short qv0 = vec_add(vec_mule(q5x0, q8y0), vec_mulo(q5x0, q8y0));
  4056. vector signed short qv1 = vec_add(vec_mule(q5x1, q8y1), vec_mulo(q5x1, q8y1));
  4057. qv0 = vec_add(qv0, qv1);
  4058. vector signed int vsumi0 = vec_add(vec_unpackh(qv0), vec_unpackl(qv0));
  4059. vsumf0 = vec_madd(vec_ctf(vsumi0, 0), vd, vsumf0);
  4060. }
  4061. vsumf0 = vec_add(vsumf0, vec_sld(vsumf0, vsumf0, 4));
  4062. vsumf0 = vec_add(vsumf0, vec_sld(vsumf0, vsumf0, 8));
  4063. *s = vec_extract(vsumf0, 0);
  4064. #elif defined(__loongarch_asx)
  4065. // Initialize accumulator with zeros
  4066. __m256 acc = (__m256)__lasx_xvldi(0);
  4067. // Main loop
  4068. for (int i = 0; i < nb; i++) {
  4069. /* Compute combined scale for the block */
  4070. const __m256 d = __lasx_xvreplfr2vr_s(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d)); //FIXME
  4071. __m256i qx = bytes_from_nibbles_32(x[i].qs);
  4072. __m256i bxhi = bytes_from_bits_32(x[i].qh);
  4073. bxhi = __lasx_xvandn_v(bxhi, __lasx_xvreplgr2vr_b((char)0xF0));
  4074. qx = __lasx_xvor_v(qx, bxhi);
  4075. __m256i qy = __lasx_xvld((const __m256i *)y[i].qs, 0);
  4076. const __m256 q = mul_sum_i8_pairs_float(qx, qy);
  4077. /* Multiply q with scale and accumulate */
  4078. acc = __lasx_xvfmadd_s(d, q, acc);
  4079. }
  4080. *s = hsum_float_8(acc);
  4081. #else
  4082. // scalar
  4083. float sumf = 0.0;
  4084. for (int i = 0; i < nb; i++) {
  4085. uint32_t qh;
  4086. memcpy(&qh, x[i].qh, sizeof(qh));
  4087. int sumi = 0;
  4088. for (int j = 0; j < qk/2; ++j) {
  4089. const uint8_t xh_0 = ((qh & (1u << (j + 0 ))) >> (j + 0 )) << 4;
  4090. const uint8_t xh_1 = ((qh & (1u << (j + 16))) >> (j + 12));
  4091. const int32_t x0 = ((x[i].qs[j] & 0x0F) | xh_0) - 16;
  4092. const int32_t x1 = ((x[i].qs[j] >> 4) | xh_1) - 16;
  4093. sumi += (x0 * y[i].qs[j]) + (x1 * y[i].qs[j + qk/2]);
  4094. }
  4095. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d)) * sumi;
  4096. }
  4097. *s = sumf;
  4098. #endif
  4099. }
  4100. void ggml_vec_dot_q5_1_q8_1(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  4101. const int qk = QK8_1;
  4102. const int nb = n / qk;
  4103. assert(n % qk == 0);
  4104. assert(qk == QK5_1);
  4105. assert(nrc == 1);
  4106. UNUSED(nrc);
  4107. UNUSED(bx);
  4108. UNUSED(by);
  4109. UNUSED(bs);
  4110. const block_q5_1 * restrict x = vx;
  4111. const block_q8_1 * restrict y = vy;
  4112. #if defined(__ARM_NEON)
  4113. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  4114. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  4115. float summs0 = 0.0f;
  4116. float summs1 = 0.0f;
  4117. uint32_t qh0;
  4118. uint32_t qh1;
  4119. uint64_t tmp0[4];
  4120. uint64_t tmp1[4];
  4121. assert(nb % 2 == 0); // TODO: handle odd nb
  4122. for (int i = 0; i < nb; i += 2) {
  4123. const block_q5_1 * restrict x0 = &x[i];
  4124. const block_q5_1 * restrict x1 = &x[i + 1];
  4125. const block_q8_1 * restrict y0 = &y[i];
  4126. const block_q8_1 * restrict y1 = &y[i + 1];
  4127. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  4128. summs0 += GGML_FP16_TO_FP32(x0->m) * GGML_FP16_TO_FP32(y0->s);
  4129. summs1 += GGML_FP16_TO_FP32(x1->m) * GGML_FP16_TO_FP32(y1->s);
  4130. // extract the 5th bit via lookup table ((b) << 4)
  4131. memcpy(&qh0, x0->qh, sizeof(qh0));
  4132. memcpy(&qh1, x1->qh, sizeof(qh1));
  4133. tmp0[0] = table_b2b_0[(qh0 >> 0) & 0xFF];
  4134. tmp0[1] = table_b2b_0[(qh0 >> 8) & 0xFF];
  4135. tmp0[2] = table_b2b_0[(qh0 >> 16) & 0xFF];
  4136. tmp0[3] = table_b2b_0[(qh0 >> 24) ];
  4137. tmp1[0] = table_b2b_0[(qh1 >> 0) & 0xFF];
  4138. tmp1[1] = table_b2b_0[(qh1 >> 8) & 0xFF];
  4139. tmp1[2] = table_b2b_0[(qh1 >> 16) & 0xFF];
  4140. tmp1[3] = table_b2b_0[(qh1 >> 24) ];
  4141. const int8x16_t qhl0 = vld1q_s8((const int8_t *)(tmp0 + 0));
  4142. const int8x16_t qhh0 = vld1q_s8((const int8_t *)(tmp0 + 2));
  4143. const int8x16_t qhl1 = vld1q_s8((const int8_t *)(tmp1 + 0));
  4144. const int8x16_t qhh1 = vld1q_s8((const int8_t *)(tmp1 + 2));
  4145. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  4146. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  4147. // 4-bit -> 8-bit
  4148. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  4149. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  4150. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  4151. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  4152. // add high bit
  4153. const int8x16_t v0_0lf = vorrq_s8(v0_0l, qhl0);
  4154. const int8x16_t v0_0hf = vorrq_s8(v0_0h, qhh0);
  4155. const int8x16_t v0_1lf = vorrq_s8(v0_1l, qhl1);
  4156. const int8x16_t v0_1hf = vorrq_s8(v0_1h, qhh1);
  4157. // load y
  4158. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  4159. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  4160. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  4161. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  4162. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  4163. ggml_vdotq_s32(vdupq_n_s32(0), v0_0lf, v1_0l),
  4164. ggml_vdotq_s32(vdupq_n_s32(0), v0_0hf, v1_0h))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  4165. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  4166. ggml_vdotq_s32(vdupq_n_s32(0), v0_1lf, v1_1l),
  4167. ggml_vdotq_s32(vdupq_n_s32(0), v0_1hf, v1_1h))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  4168. }
  4169. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1) + summs0 + summs1;
  4170. #elif defined(__wasm_simd128__)
  4171. v128_t sumv = wasm_f32x4_splat(0.0f);
  4172. float summs = 0.0f;
  4173. uint32_t qh;
  4174. uint64_t tmp[4];
  4175. // TODO: check if unrolling this is better
  4176. for (int i = 0; i < nb; ++i) {
  4177. const block_q5_1 * restrict x0 = &x[i];
  4178. const block_q8_1 * restrict y0 = &y[i];
  4179. summs += GGML_FP16_TO_FP32(x0->m) * GGML_FP16_TO_FP32(y0->s);
  4180. const v128_t m4b = wasm_i8x16_splat(0x0F);
  4181. // extract the 5th bit
  4182. memcpy(&qh, x0->qh, sizeof(qh));
  4183. tmp[0] = table_b2b_0[(qh >> 0) & 0xFF];
  4184. tmp[1] = table_b2b_0[(qh >> 8) & 0xFF];
  4185. tmp[2] = table_b2b_0[(qh >> 16) & 0xFF];
  4186. tmp[3] = table_b2b_0[(qh >> 24) ];
  4187. const v128_t qhl = wasm_v128_load(tmp + 0);
  4188. const v128_t qhh = wasm_v128_load(tmp + 2);
  4189. const v128_t v0 = wasm_v128_load(x0->qs);
  4190. // 4-bit -> 8-bit
  4191. const v128_t v0l = wasm_v128_and (v0, m4b);
  4192. const v128_t v0h = wasm_u8x16_shr(v0, 4);
  4193. // add high bit
  4194. const v128_t v0lf = wasm_v128_or(v0l, qhl);
  4195. const v128_t v0hf = wasm_v128_or(v0h, qhh);
  4196. // load y
  4197. const v128_t v1l = wasm_v128_load(y0->qs);
  4198. const v128_t v1h = wasm_v128_load(y0->qs + 16);
  4199. // int8x16 -> int16x8
  4200. const v128_t v0lfl = wasm_i16x8_extend_low_i8x16 (v0lf);
  4201. const v128_t v0lfh = wasm_i16x8_extend_high_i8x16(v0lf);
  4202. const v128_t v0hfl = wasm_i16x8_extend_low_i8x16 (v0hf);
  4203. const v128_t v0hfh = wasm_i16x8_extend_high_i8x16(v0hf);
  4204. const v128_t v1ll = wasm_i16x8_extend_low_i8x16 (v1l);
  4205. const v128_t v1lh = wasm_i16x8_extend_high_i8x16(v1l);
  4206. const v128_t v1hl = wasm_i16x8_extend_low_i8x16 (v1h);
  4207. const v128_t v1hh = wasm_i16x8_extend_high_i8x16(v1h);
  4208. // dot product
  4209. sumv = wasm_f32x4_add(sumv,
  4210. wasm_f32x4_mul(wasm_f32x4_convert_i32x4(wasm_i32x4_add(
  4211. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0lfl, v1ll),
  4212. wasm_i32x4_dot_i16x8(v0lfh, v1lh)),
  4213. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0hfl, v1hl),
  4214. wasm_i32x4_dot_i16x8(v0hfh, v1hh)))),
  4215. wasm_f32x4_splat(GGML_FP16_TO_FP32(x0->d) * GGML_FP16_TO_FP32(y0->d))));
  4216. }
  4217. *s = wasm_f32x4_extract_lane(sumv, 0) + wasm_f32x4_extract_lane(sumv, 1) +
  4218. wasm_f32x4_extract_lane(sumv, 2) + wasm_f32x4_extract_lane(sumv, 3) + summs;
  4219. #elif defined(__AVX2__)
  4220. // Initialize accumulator with zeros
  4221. __m256 acc = _mm256_setzero_ps();
  4222. float summs = 0.0f;
  4223. // Main loop
  4224. for (int i = 0; i < nb; i++) {
  4225. const __m256 dx = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d));
  4226. summs += GGML_FP16_TO_FP32(x[i].m) * GGML_FP16_TO_FP32(y[i].s);
  4227. __m256i qx = bytes_from_nibbles_32(x[i].qs);
  4228. __m256i bxhi = bytes_from_bits_32(x[i].qh);
  4229. bxhi = _mm256_and_si256(bxhi, _mm256_set1_epi8(0x10));
  4230. qx = _mm256_or_si256(qx, bxhi);
  4231. const __m256 dy = _mm256_set1_ps(GGML_FP16_TO_FP32(y[i].d));
  4232. const __m256i qy = _mm256_loadu_si256((const __m256i *)y[i].qs);
  4233. const __m256 q = mul_sum_us8_pairs_float(qx, qy);
  4234. acc = _mm256_fmadd_ps(q, _mm256_mul_ps(dx, dy), acc);
  4235. }
  4236. *s = hsum_float_8(acc) + summs;
  4237. #elif defined(__AVX__)
  4238. // Initialize accumulator with zeros
  4239. __m256 acc = _mm256_setzero_ps();
  4240. __m128i mask = _mm_set1_epi8(0x10);
  4241. float summs = 0.0f;
  4242. // Main loop
  4243. for (int i = 0; i < nb; i++) {
  4244. const __m256 dx = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d));
  4245. summs += GGML_FP16_TO_FP32(x[i].m) * GGML_FP16_TO_FP32(y[i].s);
  4246. __m256i bx_0 = bytes_from_nibbles_32(x[i].qs);
  4247. const __m256i bxhi = bytes_from_bits_32(x[i].qh);
  4248. __m128i bxhil = _mm256_castsi256_si128(bxhi);
  4249. __m128i bxhih = _mm256_extractf128_si256(bxhi, 1);
  4250. bxhil = _mm_and_si128(bxhil, mask);
  4251. bxhih = _mm_and_si128(bxhih, mask);
  4252. __m128i bxl = _mm256_castsi256_si128(bx_0);
  4253. __m128i bxh = _mm256_extractf128_si256(bx_0, 1);
  4254. bxl = _mm_or_si128(bxl, bxhil);
  4255. bxh = _mm_or_si128(bxh, bxhih);
  4256. bx_0 = MM256_SET_M128I(bxh, bxl);
  4257. const __m256 dy = _mm256_set1_ps(GGML_FP16_TO_FP32(y[i].d));
  4258. const __m256i by_0 = _mm256_loadu_si256((const __m256i *)y[i].qs);
  4259. const __m256 q = mul_sum_us8_pairs_float(bx_0, by_0);
  4260. acc = _mm256_add_ps(_mm256_mul_ps(q, _mm256_mul_ps(dx, dy)), acc);
  4261. }
  4262. *s = hsum_float_8(acc) + summs;
  4263. #elif defined(__riscv_v_intrinsic)
  4264. float sumf = 0.0;
  4265. uint32_t qh;
  4266. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  4267. // temporary registers for shift operations
  4268. vuint32m2_t vt_1 = __riscv_vid_v_u32m2(vl);
  4269. vuint32m2_t vt_2 = __riscv_vadd_vx_u32m2(vt_1, 12, vl);
  4270. for (int i = 0; i < nb; i++) {
  4271. memcpy(&qh, x[i].qh, sizeof(uint32_t));
  4272. // load qh
  4273. vuint32m2_t vqh = __riscv_vmv_v_x_u32m2(qh, vl);
  4274. // ((qh >> (j + 0)) << 4) & 0x10;
  4275. vuint32m2_t xhr_0 = __riscv_vsrl_vv_u32m2(vqh, vt_1, vl);
  4276. vuint32m2_t xhl_0 = __riscv_vsll_vx_u32m2(xhr_0, 4, vl);
  4277. vuint32m2_t xha_0 = __riscv_vand_vx_u32m2(xhl_0, 0x10, vl);
  4278. // ((qh >> (j + 12)) ) & 0x10;
  4279. vuint32m2_t xhr_1 = __riscv_vsrl_vv_u32m2(vqh, vt_2, vl);
  4280. vuint32m2_t xha_1 = __riscv_vand_vx_u32m2(xhr_1, 0x10, vl);
  4281. // narrowing
  4282. vuint16m1_t xhc_0 = __riscv_vncvt_x_x_w_u16m1(xha_0, vl);
  4283. vuint8mf2_t xh_0 = __riscv_vncvt_x_x_w_u8mf2(xhc_0, vl);
  4284. vuint16m1_t xhc_1 = __riscv_vncvt_x_x_w_u16m1(xha_1, vl);
  4285. vuint8mf2_t xh_1 = __riscv_vncvt_x_x_w_u8mf2(xhc_1, vl);
  4286. // load
  4287. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  4288. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  4289. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  4290. vuint8mf2_t x_at = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  4291. vuint8mf2_t x_lt = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  4292. vuint8mf2_t x_a = __riscv_vor_vv_u8mf2(x_at, xh_0, vl);
  4293. vuint8mf2_t x_l = __riscv_vor_vv_u8mf2(x_lt, xh_1, vl);
  4294. vint8mf2_t v0 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  4295. vint8mf2_t v1 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  4296. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  4297. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  4298. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  4299. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  4300. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  4301. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  4302. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d))*sumi + GGML_FP16_TO_FP32(x[i].m)*GGML_FP16_TO_FP32(y[i].s);
  4303. }
  4304. *s = sumf;
  4305. #elif defined(__POWER9_VECTOR__)
  4306. const vector signed char lowMask = vec_splats((signed char)0xF);
  4307. const vector signed int v0 = vec_splats((int32_t)0);
  4308. const vector unsigned char v4 = vec_splats((unsigned char)0x4);
  4309. vector float vsumf0 = vec_splats(0.0f);
  4310. #pragma GCC unroll 4
  4311. for (int i = 0; i < nb; ++i) {
  4312. __builtin_prefetch(x[i].qs, 0, 1);
  4313. __builtin_prefetch(y[i].qs, 0, 1);
  4314. vector float vxd = vec_splats(GGML_FP16_TO_FP32(x[i].d));
  4315. vector float vyd = vec_splats(GGML_FP16_TO_FP32(y[i].d));
  4316. vector float vd = vec_mul(vxd, vyd);
  4317. vector float vxmin = vec_splats(GGML_FP16_TO_FP32(x[i].m));
  4318. vector float vys = {GGML_FP16_TO_FP32(y[i].s), 0.f, 0.f, 0.f};
  4319. vsumf0 = vec_madd(vxmin, vys, vsumf0);
  4320. vector unsigned long long aux64x2_0 = {(uint64_t)(table_b2b_0[x[i].qh[0]]), (uint64_t)(table_b2b_0[x[i].qh[1]])};
  4321. vector unsigned long long aux64x2_1 = {(uint64_t)(table_b2b_0[x[i].qh[2]]), (uint64_t)(table_b2b_0[x[i].qh[3]])};
  4322. vector signed char qh0 = (vector signed char)aux64x2_0;
  4323. vector signed char qh1 = (vector signed char)aux64x2_1;
  4324. vector signed char qxs = (vector signed char)vec_xl( 0, x[i].qs);
  4325. vector unsigned char q5x0 = (vector unsigned char)vec_or(vec_and(qxs, lowMask), qh0);
  4326. vector unsigned char q5x1 = (vector unsigned char)vec_or(vec_sr(qxs, v4), qh1);
  4327. vector signed char q8y0 = vec_xl( 0, y[i].qs);
  4328. vector signed char q8y1 = vec_xl( 16, y[i].qs);
  4329. vector signed int vsumi0 = v0;
  4330. vsumi0 = vec_msum(q8y0, q5x0, vsumi0);
  4331. vsumi0 = vec_msum(q8y1, q5x1, vsumi0);
  4332. vsumf0 = vec_madd(vec_ctf(vsumi0, 0), vd, vsumf0);
  4333. }
  4334. vsumf0 = vec_add(vsumf0, vec_sld(vsumf0, vsumf0, 4));
  4335. vsumf0 = vec_add(vsumf0, vec_sld(vsumf0, vsumf0, 8));
  4336. *s = vec_extract(vsumf0, 0);
  4337. #elif defined(__loongarch_asx)
  4338. // Initialize accumulator with zeros
  4339. __m256 acc = (__m256)__lasx_xvldi(0);
  4340. float summs = 0.0f;
  4341. // Main loop
  4342. for (int i = 0; i < nb; i++) {
  4343. const __m256 dx = __lasx_xvreplfr2vr_s(GGML_FP16_TO_FP32(x[i].d));
  4344. summs += GGML_FP16_TO_FP32(x[i].m) * GGML_FP16_TO_FP32(y[i].s);
  4345. __m256i qx = bytes_from_nibbles_32(x[i].qs);
  4346. __m256i bxhi = bytes_from_bits_32(x[i].qh);
  4347. bxhi = __lasx_xvand_v(bxhi, __lasx_xvreplgr2vr_b(0x10));
  4348. qx = __lasx_xvor_v(qx, bxhi);
  4349. const __m256 dy = __lasx_xvreplfr2vr_s(GGML_FP16_TO_FP32(y[i].d));
  4350. const __m256i qy = __lasx_xvld((const __m256i *)y[i].qs, 0);
  4351. const __m256 q = mul_sum_us8_pairs_float(qx, qy);
  4352. acc = __lasx_xvfmadd_s(q, __lasx_xvfmul_s(dx, dy), acc);
  4353. }
  4354. *s = hsum_float_8(acc) + summs;
  4355. #else
  4356. // scalar
  4357. float sumf = 0.0;
  4358. for (int i = 0; i < nb; i++) {
  4359. uint32_t qh;
  4360. memcpy(&qh, x[i].qh, sizeof(qh));
  4361. int sumi = 0;
  4362. for (int j = 0; j < qk/2; ++j) {
  4363. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  4364. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  4365. const int32_t x0 = (x[i].qs[j] & 0xF) | xh_0;
  4366. const int32_t x1 = (x[i].qs[j] >> 4) | xh_1;
  4367. sumi += (x0 * y[i].qs[j]) + (x1 * y[i].qs[j + qk/2]);
  4368. }
  4369. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d))*sumi + GGML_FP16_TO_FP32(x[i].m)*GGML_FP16_TO_FP32(y[i].s);
  4370. }
  4371. *s = sumf;
  4372. #endif
  4373. }
  4374. void ggml_vec_dot_q8_0_q8_0(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  4375. const int qk = QK8_0;
  4376. const int nb = n / qk;
  4377. assert(n % qk == 0);
  4378. #if defined(__ARM_FEATURE_MATMUL_INT8)
  4379. assert((nrc == 2) || (nrc == 1));
  4380. #else
  4381. assert(nrc == 1);
  4382. #endif
  4383. UNUSED(nrc);
  4384. UNUSED(bx);
  4385. UNUSED(by);
  4386. UNUSED(bs);
  4387. const block_q8_0 * restrict x = vx;
  4388. const block_q8_0 * restrict y = vy;
  4389. #if defined(__ARM_FEATURE_MATMUL_INT8)
  4390. if (nrc == 2) {
  4391. const block_q8_0 * restrict vx0 = vx;
  4392. const block_q8_0 * restrict vx1 = (const block_q8_0 *) ((const uint8_t*)vx + bx);
  4393. const block_q8_0 * restrict vy0 = vy;
  4394. const block_q8_0 * restrict vy1 = (const block_q8_0 *) ((const uint8_t*)vy + by);
  4395. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  4396. for (int i = 0; i < nb; i++) {
  4397. const block_q8_0 * restrict b_x0 = &vx0[i];
  4398. const block_q8_0 * restrict b_y0 = &vy0[i];
  4399. const block_q8_0 * restrict b_x1 = &vx1[i];
  4400. const block_q8_0 * restrict b_y1 = &vy1[i];
  4401. const int8x16_t x0_l = vld1q_s8(b_x0->qs);
  4402. const int8x16_t x0_h = vld1q_s8(b_x0->qs + 16);
  4403. const int8x16_t x1_l = vld1q_s8(b_x1->qs);
  4404. const int8x16_t x1_h = vld1q_s8(b_x1->qs + 16);
  4405. // load y
  4406. const int8x16_t y0_l = vld1q_s8(b_y0->qs);
  4407. const int8x16_t y0_h = vld1q_s8(b_y0->qs + 16);
  4408. const int8x16_t y1_l = vld1q_s8(b_y1->qs);
  4409. const int8x16_t y1_h = vld1q_s8(b_y1->qs + 16);
  4410. float32_t _scale[4] = {GGML_FP16_TO_FP32(b_x0->d)*GGML_FP16_TO_FP32(b_y0->d),
  4411. GGML_FP16_TO_FP32(b_x0->d)*GGML_FP16_TO_FP32(b_y1->d),
  4412. GGML_FP16_TO_FP32(b_x1->d)*GGML_FP16_TO_FP32(b_y0->d),
  4413. GGML_FP16_TO_FP32(b_x1->d)*GGML_FP16_TO_FP32(b_y1->d)};
  4414. float32x4_t scale = vld1q_f32(_scale);
  4415. int8x16_t l0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  4416. int8x16_t l1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  4417. int8x16_t l2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  4418. int8x16_t l3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  4419. int8x16_t r0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  4420. int8x16_t r1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  4421. int8x16_t r2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  4422. int8x16_t r3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  4423. sumv0 = vmlaq_f32(sumv0,(vcvtq_f32_s32(vmmlaq_s32((vmmlaq_s32((vmmlaq_s32((vmmlaq_s32(vdupq_n_s32(0), l0, r0)),
  4424. l1, r1)), l2, r2)), l3, r3))), scale);
  4425. }
  4426. float32x4_t sumv1 = vextq_f32(sumv0, sumv0, 2);
  4427. float32x4_t sumv2 = vzip1q_f32(sumv0, sumv1);
  4428. vst1_f32(s, vget_low_f32(sumv2));
  4429. vst1_f32(s + bs, vget_high_f32(sumv2));
  4430. return;
  4431. }
  4432. #endif
  4433. #if defined(__ARM_FEATURE_SVE)
  4434. svfloat32_t sumv0 = svdup_n_f32(0.0f);
  4435. svfloat32_t sumv1 = svdup_n_f32(0.0f);
  4436. assert(nb % 2 == 0); // TODO: handle odd nb
  4437. for (int i = 0; i < nb; i += 2) {
  4438. const block_q8_0 * restrict x0 = &x[i + 0];
  4439. const block_q8_0 * restrict x1 = &x[i + 1];
  4440. const block_q8_0 * restrict y0 = &y[i + 0];
  4441. const block_q8_0 * restrict y1 = &y[i + 1];
  4442. // load x
  4443. const svint8_t qx0 = svld1_s8(svptrue_b8(), x0->qs);
  4444. const svint8_t qx1 = svld1_s8(svptrue_b8(), x1->qs);
  4445. // load y
  4446. const svint8_t qy0 = svld1_s8(svptrue_b8(), y0->qs);
  4447. const svint8_t qy1 = svld1_s8(svptrue_b8(), y1->qs);
  4448. sumv0 = svmla_n_f32_x(svptrue_b32(), sumv0, svcvt_f32_s32_x(svptrue_b32(), svdot_s32(svdup_n_s32(0), qx0, qy0)), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  4449. sumv1 = svmla_n_f32_x(svptrue_b32(), sumv1, svcvt_f32_s32_x(svptrue_b32(), svdot_s32(svdup_n_s32(0), qx1, qy1)), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  4450. }
  4451. *s = svaddv_f32(svptrue_b32(), svadd_f32_x(svptrue_b32(), sumv0, sumv1));
  4452. #elif defined(__ARM_NEON)
  4453. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  4454. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  4455. assert(nb % 2 == 0); // TODO: handle odd nb
  4456. for (int i = 0; i < nb; i += 2) {
  4457. const block_q8_0 * restrict x0 = &x[i + 0];
  4458. const block_q8_0 * restrict x1 = &x[i + 1];
  4459. const block_q8_0 * restrict y0 = &y[i + 0];
  4460. const block_q8_0 * restrict y1 = &y[i + 1];
  4461. const int8x16_t x0_0 = vld1q_s8(x0->qs);
  4462. const int8x16_t x0_1 = vld1q_s8(x0->qs + 16);
  4463. const int8x16_t x1_0 = vld1q_s8(x1->qs);
  4464. const int8x16_t x1_1 = vld1q_s8(x1->qs + 16);
  4465. // load y
  4466. const int8x16_t y0_0 = vld1q_s8(y0->qs);
  4467. const int8x16_t y0_1 = vld1q_s8(y0->qs + 16);
  4468. const int8x16_t y1_0 = vld1q_s8(y1->qs);
  4469. const int8x16_t y1_1 = vld1q_s8(y1->qs + 16);
  4470. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  4471. ggml_vdotq_s32(vdupq_n_s32(0), x0_0, y0_0),
  4472. ggml_vdotq_s32(vdupq_n_s32(0), x0_1, y0_1))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  4473. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  4474. ggml_vdotq_s32(vdupq_n_s32(0), x1_0, y1_0),
  4475. ggml_vdotq_s32(vdupq_n_s32(0), x1_1, y1_1))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  4476. }
  4477. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  4478. #elif defined(__AVX2__) || defined(__AVX__)
  4479. // Initialize accumulator with zeros
  4480. __m256 acc = _mm256_setzero_ps();
  4481. // Main loop
  4482. for (int i = 0; i < nb; ++i) {
  4483. // Compute combined scale for the block
  4484. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  4485. __m256i qx = _mm256_loadu_si256((const __m256i *)x[i].qs);
  4486. __m256i qy = _mm256_loadu_si256((const __m256i *)y[i].qs);
  4487. const __m256 q = mul_sum_i8_pairs_float(qx, qy);
  4488. // Multiply q with scale and accumulate
  4489. #if defined(__AVX2__)
  4490. acc = _mm256_fmadd_ps( d, q, acc );
  4491. #else
  4492. acc = _mm256_add_ps( _mm256_mul_ps( d, q ), acc );
  4493. #endif
  4494. }
  4495. *s = hsum_float_8(acc);
  4496. #elif defined(__riscv_v_intrinsic)
  4497. float sumf = 0.0;
  4498. size_t vl = __riscv_vsetvl_e8m1(qk);
  4499. for (int i = 0; i < nb; i++) {
  4500. // load elements
  4501. vint8m1_t bx_0 = __riscv_vle8_v_i8m1(x[i].qs, vl);
  4502. vint8m1_t by_0 = __riscv_vle8_v_i8m1(y[i].qs, vl);
  4503. vint16m2_t vw_mul = __riscv_vwmul_vv_i16m2(bx_0, by_0, vl);
  4504. vint32m1_t v_zero = __riscv_vmv_v_x_i32m1(0, vl);
  4505. vint32m1_t v_sum = __riscv_vwredsum_vs_i16m2_i32m1(vw_mul, v_zero, vl);
  4506. int sumi = __riscv_vmv_x_s_i32m1_i32(v_sum);
  4507. sumf += sumi*(GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d));
  4508. }
  4509. *s = sumf;
  4510. #elif defined(__POWER9_VECTOR__)
  4511. const vector signed int v0 = vec_splats((int32_t)0);
  4512. vector float vsumf0 = vec_splats(0.0f);
  4513. #pragma GCC unroll 8
  4514. for (int i = 0; i < nb; i++) {
  4515. __builtin_prefetch(x[i].qs, 0, 1);
  4516. __builtin_prefetch(y[i].qs, 0, 1);
  4517. vector float vxd = vec_splats(GGML_FP16_TO_FP32(x[i].d));
  4518. vector float vyd = vec_splats(GGML_FP16_TO_FP32(y[i].d));
  4519. vector float vd = vec_mul(vxd, vyd);
  4520. vector signed char q8x0 = vec_xl( 0, x[i].qs);
  4521. vector signed char q8x1 = vec_xl(16, x[i].qs);
  4522. vector signed char q8y0 = vec_xl( 0, y[i].qs);
  4523. vector signed char q8y1 = vec_xl(16, y[i].qs);
  4524. vector signed short qv0 = vec_mule(q8x0, q8y0);
  4525. vector signed short qv1 = vec_mulo(q8x0, q8y0);
  4526. vector signed short qv2 = vec_mule(q8x1, q8y1);
  4527. vector signed short qv3 = vec_mulo(q8x1, q8y1);
  4528. vector signed int vsumi0 = v0;
  4529. vector signed int vsumi1 = v0;
  4530. vsumi0 = vec_sum4s(qv0, vsumi0);
  4531. vsumi1 = vec_sum4s(qv1, vsumi1);
  4532. vsumi0 = vec_sum4s(qv2, vsumi0);
  4533. vsumi1 = vec_sum4s(qv3, vsumi1);
  4534. vsumi0 = vec_add(vsumi0, vsumi1);
  4535. vsumf0 = vec_madd(vec_ctf(vsumi0, 0), vd, vsumf0);
  4536. }
  4537. vsumf0 = vec_add(vsumf0, vec_sld(vsumf0, vsumf0, 4));
  4538. vsumf0 = vec_add(vsumf0, vec_sld(vsumf0, vsumf0, 8));
  4539. *s = vec_extract(vsumf0, 0);
  4540. #elif defined(__loongarch_asx)
  4541. // Initialize accumulator with zeros
  4542. __m256 acc = (__m256)__lasx_xvldi(0);
  4543. // Main loop
  4544. for (int i = 0; i < nb; ++i) {
  4545. // Compute combined scale for the block
  4546. const __m256 d = __lasx_xvreplfr2vr_s(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  4547. __m256i qx = __lasx_xvld((const __m256i *)x[i].qs, 0);
  4548. __m256i qy = __lasx_xvld((const __m256i *)y[i].qs, 0);
  4549. const __m256 q = mul_sum_i8_pairs_float(qx, qy);
  4550. // Multiply q with scale and accumulate
  4551. acc = __lasx_xvfmadd_s( d, q, acc );
  4552. }
  4553. *s = hsum_float_8(acc);
  4554. #else
  4555. // scalar
  4556. float sumf = 0.0;
  4557. for (int i = 0; i < nb; i++) {
  4558. int sumi = 0;
  4559. for (int j = 0; j < qk; j++) {
  4560. sumi += x[i].qs[j]*y[i].qs[j];
  4561. }
  4562. sumf += sumi*(GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d));
  4563. }
  4564. *s = sumf;
  4565. #endif
  4566. }
  4567. void ggml_vec_dot_q2_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  4568. assert(nrc == 1);
  4569. UNUSED(nrc);
  4570. UNUSED(bx);
  4571. UNUSED(by);
  4572. UNUSED(bs);
  4573. const block_q2_K * restrict x = vx;
  4574. const block_q8_K * restrict y = vy;
  4575. const int nb = n / QK_K;
  4576. #ifdef __ARM_NEON
  4577. const uint8x16_t m3 = vdupq_n_u8(0x3);
  4578. const uint8x16_t m4 = vdupq_n_u8(0xF);
  4579. const int32x4_t vzero = vdupq_n_s32(0);
  4580. ggml_int8x16x2_t q2bytes;
  4581. uint8_t aux[16];
  4582. float sum = 0;
  4583. for (int i = 0; i < nb; ++i) {
  4584. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4585. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4586. const uint8_t * restrict q2 = x[i].qs;
  4587. const int8_t * restrict q8 = y[i].qs;
  4588. const uint8_t * restrict sc = x[i].scales;
  4589. const uint8x16_t mins_and_scales = vld1q_u8(sc);
  4590. const uint8x16_t scales = vandq_u8(mins_and_scales, m4);
  4591. vst1q_u8(aux, scales);
  4592. const uint8x16_t mins = vshrq_n_u8(mins_and_scales, 4);
  4593. const ggml_int16x8x2_t q8sums = ggml_vld1q_s16_x2(y[i].bsums);
  4594. const ggml_int16x8x2_t mins16 = {{vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mins))), vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mins)))}};
  4595. const int32x4_t s0 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[0]), vget_low_s16 (q8sums.val[0])),
  4596. vmull_s16(vget_high_s16(mins16.val[0]), vget_high_s16(q8sums.val[0])));
  4597. const int32x4_t s1 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[1]), vget_low_s16 (q8sums.val[1])),
  4598. vmull_s16(vget_high_s16(mins16.val[1]), vget_high_s16(q8sums.val[1])));
  4599. sum += dmin * vaddvq_s32(vaddq_s32(s0, s1));
  4600. int isum = 0;
  4601. int is = 0;
  4602. // We use this macro instead of a function call because for some reason
  4603. // the code runs 2-3% slower, even if the function is declared inline
  4604. #define MULTIPLY_ACCUM_WITH_SCALE(index)\
  4605. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[0], q8bytes.val[0])) * aux[is+(index)];\
  4606. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[1], q8bytes.val[1])) * aux[is+1+(index)];
  4607. #define SHIFT_MULTIPLY_ACCUM_WITH_SCALE(shift, index)\
  4608. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;\
  4609. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[0], (shift)), m3));\
  4610. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[1], (shift)), m3));\
  4611. MULTIPLY_ACCUM_WITH_SCALE((index));
  4612. for (int j = 0; j < QK_K/128; ++j) {
  4613. const ggml_uint8x16x2_t q2bits = ggml_vld1q_u8_x2(q2); q2 += 32;
  4614. ggml_int8x16x2_t q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  4615. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[0], m3));
  4616. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[1], m3));
  4617. MULTIPLY_ACCUM_WITH_SCALE(0);
  4618. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(2, 2);
  4619. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(4, 4);
  4620. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(6, 6);
  4621. is += 8;
  4622. }
  4623. sum += d * isum;
  4624. }
  4625. *s = sum;
  4626. #elif defined __AVX2__
  4627. const __m256i m3 = _mm256_set1_epi8(3);
  4628. const __m128i m4 = _mm_set1_epi8(0xF);
  4629. __m256 acc = _mm256_setzero_ps();
  4630. for (int i = 0; i < nb; ++i) {
  4631. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4632. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4633. const uint8_t * restrict q2 = x[i].qs;
  4634. const int8_t * restrict q8 = y[i].qs;
  4635. const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  4636. const __m128i scales8 = _mm_and_si128(mins_and_scales, m4);
  4637. const __m128i mins8 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
  4638. const __m256i mins = _mm256_cvtepi8_epi16(mins8);
  4639. const __m256i prod = _mm256_madd_epi16(mins, _mm256_loadu_si256((const __m256i*)y[i].bsums));
  4640. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(prod), acc);
  4641. const __m256i all_scales = _mm256_cvtepi8_epi16(scales8);
  4642. const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
  4643. const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
  4644. const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
  4645. __m256i sumi = _mm256_setzero_si256();
  4646. for (int j = 0; j < QK_K/128; ++j) {
  4647. const __m256i q2bits = _mm256_loadu_si256((const __m256i*)q2); q2 += 32;
  4648. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4649. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4650. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4651. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4652. const __m256i q2_0 = _mm256_and_si256(q2bits, m3);
  4653. const __m256i q2_1 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 2), m3);
  4654. const __m256i q2_2 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 4), m3);
  4655. const __m256i q2_3 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 6), m3);
  4656. __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0);
  4657. __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1);
  4658. __m256i p2 = _mm256_maddubs_epi16(q2_2, q8_2);
  4659. __m256i p3 = _mm256_maddubs_epi16(q2_3, q8_3);
  4660. p0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(0)), p0);
  4661. p1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(1)), p1);
  4662. p2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(2)), p2);
  4663. p3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(3)), p3);
  4664. p0 = _mm256_add_epi32(p0, p1);
  4665. p2 = _mm256_add_epi32(p2, p3);
  4666. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p0, p2));
  4667. }
  4668. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  4669. }
  4670. *s = hsum_float_8(acc);
  4671. #elif defined __AVX__
  4672. const __m128i m3 = _mm_set1_epi8(0x3);
  4673. const __m128i m4 = _mm_set1_epi8(0xF);
  4674. const __m128i m2 = _mm_set1_epi8(0x2);
  4675. __m256 acc = _mm256_setzero_ps();
  4676. for (int i = 0; i < nb; ++i) {
  4677. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4678. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4679. const uint8_t * restrict q2 = x[i].qs;
  4680. const int8_t * restrict q8 = y[i].qs;
  4681. // load mins and scales from block_q2_K.scales[QK_K/16]
  4682. const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  4683. const __m128i scales16 = _mm_and_si128(mins_and_scales, m4);
  4684. const __m128i mins16 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
  4685. const __m128i mins_0 = _mm_cvtepi8_epi16(mins16);
  4686. const __m128i mins_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(mins16, mins16));
  4687. // summs = y[i].bsums * (x[i].scales >> 4) in 16bits*8*2 to 32bits*4*2
  4688. const __m128i summs_0 = _mm_madd_epi16(mins_0, _mm_loadu_si128((const __m128i*)&y[i].bsums[0]));
  4689. const __m128i summs_1 = _mm_madd_epi16(mins_1, _mm_loadu_si128((const __m128i*)&y[i].bsums[8]));
  4690. // sumf += -dmin * summs in 32bits*8
  4691. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(MM256_SET_M128I(summs_1, summs_0))), acc);
  4692. const __m128i scales_0 = _mm_cvtepi8_epi16(scales16);
  4693. const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales16, scales16));
  4694. const __m128i scales[2] = { scales_0, scales_1 };
  4695. __m128i sumi_0 = _mm_setzero_si128();
  4696. __m128i sumi_1 = _mm_setzero_si128();
  4697. for (int j = 0; j < QK_K/128; ++j) {
  4698. // load Q8 quants int8*16*8 from block_q8_K.qs[QK_K]
  4699. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4700. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4701. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4702. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4703. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4704. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4705. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4706. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4707. // load 2bits*16*8 from block_q2_K.qs[QK_K/4]
  4708. __m128i q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
  4709. const __m128i q2_0 = _mm_and_si128(q2bits, m3);
  4710. const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4711. const __m128i q2_4 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4712. const __m128i q2_6 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4713. q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
  4714. const __m128i q2_1 = _mm_and_si128(q2bits, m3);
  4715. const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4716. const __m128i q2_5 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4717. const __m128i q2_7 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4718. // isuml = q8[l] * ((q2[l] >> shift) & 3) in 8bits*16*8 to 16bits*8*8
  4719. __m128i p0 = _mm_maddubs_epi16(q2_0, q8_0);
  4720. __m128i p1 = _mm_maddubs_epi16(q2_1, q8_1);
  4721. __m128i p2 = _mm_maddubs_epi16(q2_2, q8_2);
  4722. __m128i p3 = _mm_maddubs_epi16(q2_3, q8_3);
  4723. __m128i p4 = _mm_maddubs_epi16(q2_4, q8_4);
  4724. __m128i p5 = _mm_maddubs_epi16(q2_5, q8_5);
  4725. __m128i p6 = _mm_maddubs_epi16(q2_6, q8_6);
  4726. __m128i p7 = _mm_maddubs_epi16(q2_7, q8_7);
  4727. // isum += (x[i].scales[is++] & 0xF) * isuml in 16bits*8*8 to 32bits*4*8
  4728. __m128i shuffle = _mm_set1_epi16(0x0100);
  4729. p0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p0);
  4730. shuffle = _mm_add_epi16(shuffle, m2);
  4731. p1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p1);
  4732. shuffle = _mm_add_epi16(shuffle, m2);
  4733. p2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p2);
  4734. shuffle = _mm_add_epi16(shuffle, m2);
  4735. p3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p3);
  4736. shuffle = _mm_add_epi16(shuffle, m2);
  4737. p4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p4);
  4738. shuffle = _mm_add_epi16(shuffle, m2);
  4739. p5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p5);
  4740. shuffle = _mm_add_epi16(shuffle, m2);
  4741. p6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p6);
  4742. shuffle = _mm_add_epi16(shuffle, m2);
  4743. p7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p7);
  4744. p0 = _mm_add_epi32(p0, p1);
  4745. p2 = _mm_add_epi32(p2, p3);
  4746. p4 = _mm_add_epi32(p4, p5);
  4747. p6 = _mm_add_epi32(p6, p7);
  4748. // isum in 32bits*4*2
  4749. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p0, p2));
  4750. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p4, p6));
  4751. }
  4752. // sumf += dall * isum - dmin * summs in 32bits
  4753. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  4754. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dall), _mm256_cvtepi32_ps(sumi)), acc);
  4755. }
  4756. *s = hsum_float_8(acc);
  4757. #elif defined __riscv_v_intrinsic
  4758. float sumf = 0;
  4759. uint8_t temp_01[32] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  4760. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
  4761. for (int i = 0; i < nb; ++i) {
  4762. const uint8_t * q2 = x[i].qs;
  4763. const int8_t * q8 = y[i].qs;
  4764. const uint8_t * sc = x[i].scales;
  4765. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4766. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4767. size_t vl = 16;
  4768. vuint8m1_t scales = __riscv_vle8_v_u8m1(sc, vl);
  4769. vuint8m1_t aux = __riscv_vand_vx_u8m1(scales, 0x0F, vl);
  4770. vint16m1_t q8sums = __riscv_vle16_v_i16m1(y[i].bsums, vl);
  4771. vuint8mf2_t scales_2 = __riscv_vle8_v_u8mf2(sc, vl);
  4772. vuint8mf2_t mins8 = __riscv_vsrl_vx_u8mf2(scales_2, 0x4, vl);
  4773. vint16m1_t mins = __riscv_vreinterpret_v_u16m1_i16m1(__riscv_vzext_vf2_u16m1(mins8, vl));
  4774. vint32m2_t prod = __riscv_vwmul_vv_i32m2(q8sums, mins, vl);
  4775. vint32m1_t vsums = __riscv_vredsum_vs_i32m2_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  4776. sumf += dmin * __riscv_vmv_x_s_i32m1_i32(vsums);
  4777. vl = 32;
  4778. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4779. vuint8m1_t v_b = __riscv_vle8_v_u8m1(temp_01, vl);
  4780. uint8_t is=0;
  4781. int isum=0;
  4782. for (int j = 0; j < QK_K/128; ++j) {
  4783. // load Q2
  4784. vuint8m1_t q2_x = __riscv_vle8_v_u8m1(q2, vl);
  4785. vuint8m1_t q2_0 = __riscv_vand_vx_u8m1(q2_x, 0x03, vl);
  4786. vuint8m1_t q2_1 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x2, vl), 0x03 , vl);
  4787. vuint8m1_t q2_2 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x4, vl), 0x03 , vl);
  4788. vuint8m1_t q2_3 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x6, vl), 0x03 , vl);
  4789. // duplicate scale elements for product
  4790. vuint8m1_t sc0 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 0+is, vl), vl);
  4791. vuint8m1_t sc1 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 2+is, vl), vl);
  4792. vuint8m1_t sc2 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 4+is, vl), vl);
  4793. vuint8m1_t sc3 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 6+is, vl), vl);
  4794. vint16m2_t p0 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_0, sc0, vl));
  4795. vint16m2_t p1 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_1, sc1, vl));
  4796. vint16m2_t p2 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_2, sc2, vl));
  4797. vint16m2_t p3 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_3, sc3, vl));
  4798. // load Q8
  4799. vint8m1_t q8_0 = __riscv_vle8_v_i8m1(q8, vl);
  4800. vint8m1_t q8_1 = __riscv_vle8_v_i8m1(q8+32, vl);
  4801. vint8m1_t q8_2 = __riscv_vle8_v_i8m1(q8+64, vl);
  4802. vint8m1_t q8_3 = __riscv_vle8_v_i8m1(q8+96, vl);
  4803. vint32m4_t s0 = __riscv_vwmul_vv_i32m4(p0, __riscv_vwcvt_x_x_v_i16m2(q8_0, vl), vl);
  4804. vint32m4_t s1 = __riscv_vwmul_vv_i32m4(p1, __riscv_vwcvt_x_x_v_i16m2(q8_1, vl), vl);
  4805. vint32m4_t s2 = __riscv_vwmul_vv_i32m4(p2, __riscv_vwcvt_x_x_v_i16m2(q8_2, vl), vl);
  4806. vint32m4_t s3 = __riscv_vwmul_vv_i32m4(p3, __riscv_vwcvt_x_x_v_i16m2(q8_3, vl), vl);
  4807. vint32m1_t isum0 = __riscv_vredsum_vs_i32m4_i32m1(__riscv_vadd_vv_i32m4(s0, s1, vl), vzero, vl);
  4808. vint32m1_t isum1 = __riscv_vredsum_vs_i32m4_i32m1(__riscv_vadd_vv_i32m4(s2, s3, vl), isum0, vl);
  4809. isum += __riscv_vmv_x_s_i32m1_i32(isum1);
  4810. q2+=32; q8+=128; is=8;
  4811. }
  4812. sumf += dall * isum;
  4813. }
  4814. *s = sumf;
  4815. #elif defined(__POWER9_VECTOR__)
  4816. const vector signed char lowMask = vec_splats((signed char)0x3);
  4817. const vector signed char lowScaleMask = vec_splats((signed char)0xF);
  4818. const vector int v0 = vec_splats((int32_t)0);
  4819. const vector unsigned char v2 = vec_splats((unsigned char)0x2);
  4820. const vector unsigned char v6 = vec_splats((unsigned char)0x6);
  4821. const vector unsigned char v4 = vec_splats((unsigned char)0x4);
  4822. vector float vsumf0 = vec_splats(0.0f);
  4823. vector float vsumf1 = vec_splats(0.0f);
  4824. vector float vsumf2 = vec_splats(0.0f);
  4825. vector float vsumf3 = vec_splats(0.0f);
  4826. for (int i = 0; i < nb; ++i) {
  4827. vector float vxd = vec_splats(GGML_FP16_TO_FP32(x[i].d));
  4828. vector float vyd = vec_splats(y[i].d);
  4829. vector float vd = vec_mul(vxd, vyd);
  4830. vector float vxmin = vec_splats(GGML_FP16_TO_FP32(x[i].dmin));
  4831. vector float vdmin = vec_mul(vxmin, vyd);
  4832. vector signed short q8ysums0 = vec_xl( 0, y[i].bsums);
  4833. vector signed short q8ysums1 = vec_xl(16, y[i].bsums);
  4834. vector signed char q2xmins = (vector signed char)vec_xl( 0, x[i].scales);
  4835. vector signed char vscales = vec_and(q2xmins, lowScaleMask);
  4836. q2xmins = vec_sr(q2xmins, v4);
  4837. vector signed short q2xmins0 = vec_unpackh(q2xmins);
  4838. vector signed short q2xmins1 = vec_unpackl(q2xmins);
  4839. vector signed int prod0 = vec_mule(q2xmins0, q8ysums0);
  4840. vector signed int prod1 = vec_mulo(q2xmins0, q8ysums0);
  4841. vector signed int prod2 = vec_mule(q2xmins1, q8ysums1);
  4842. vector signed int prod3 = vec_mulo(q2xmins1, q8ysums1);
  4843. vsumf0 = vec_nmsub(vec_ctf(prod0, 0), vdmin, vsumf0);
  4844. vsumf1 = vec_nmsub(vec_ctf(prod1, 0), vdmin, vsumf1);
  4845. vsumf2 = vec_nmsub(vec_ctf(prod2, 0), vdmin, vsumf2);
  4846. vsumf3 = vec_nmsub(vec_ctf(prod3, 0), vdmin, vsumf3);
  4847. vector signed int vsumi0 = v0;
  4848. vector signed int vsumi1 = v0;
  4849. vector signed int vsumi2 = v0;
  4850. vector signed int vsumi3 = v0;
  4851. vector signed int vsumi4 = v0;
  4852. vector signed int vsumi5 = v0;
  4853. vector signed int vsumi6 = v0;
  4854. vector signed int vsumi7 = v0;
  4855. const uint8_t * restrict q2 = x[i].qs;
  4856. const int8_t * restrict q8 = y[i].qs;
  4857. for (int j = 0; j < QK_K/128; ++j) {
  4858. __builtin_prefetch(q2, 0, 1);
  4859. __builtin_prefetch(q8, 0, 1);
  4860. vector signed char qxs0 = (vector signed char)vec_xl( 0, q2);
  4861. vector signed char qxs1 = (vector signed char)vec_xl(16, q2);
  4862. q2 += 32;
  4863. vector unsigned char q2x00 = (vector unsigned char)vec_and(qxs0, lowMask);
  4864. vector unsigned char q2x01 = (vector unsigned char)vec_and(vec_sr(qxs0, v2), lowMask);
  4865. vector unsigned char q2x02 = (vector unsigned char)vec_and(vec_sr(qxs0, v4), lowMask);
  4866. vector unsigned char q2x03 = (vector unsigned char)vec_and(vec_sr(qxs0, v6), lowMask);
  4867. vector unsigned char q2x10 = (vector unsigned char)vec_and(qxs1, lowMask);
  4868. vector unsigned char q2x11 = (vector unsigned char)vec_and(vec_sr(qxs1, v2), lowMask);
  4869. vector unsigned char q2x12 = (vector unsigned char)vec_and(vec_sr(qxs1, v4), lowMask);
  4870. vector unsigned char q2x13 = (vector unsigned char)vec_and(vec_sr(qxs1, v6), lowMask);
  4871. vector signed char q8y00 = vec_xl( 0, q8);
  4872. vector signed char q8y10 = vec_xl( 16, q8);
  4873. vector signed char q8y01 = vec_xl( 32, q8);
  4874. vector signed char q8y11 = vec_xl( 48, q8);
  4875. vector signed char q8y02 = vec_xl( 64, q8);
  4876. vector signed char q8y12 = vec_xl( 80, q8);
  4877. vector signed char q8y03 = vec_xl( 96, q8);
  4878. vector signed char q8y13 = vec_xl(112, q8);
  4879. q8 += 128;
  4880. vector signed int qv0 = vec_msum(q8y00, q2x00, v0);
  4881. vector signed int qv1 = vec_msum(q8y01, q2x01, v0);
  4882. vector signed int qv2 = vec_msum(q8y02, q2x02, v0);
  4883. vector signed int qv3 = vec_msum(q8y03, q2x03, v0);
  4884. vector signed int qv4 = vec_msum(q8y10, q2x10, v0);
  4885. vector signed int qv5 = vec_msum(q8y11, q2x11, v0);
  4886. vector signed int qv6 = vec_msum(q8y12, q2x12, v0);
  4887. vector signed int qv7 = vec_msum(q8y13, q2x13, v0);
  4888. vector signed short vscales_07 = vec_unpackh(vscales);
  4889. vector signed int vscales_03 = vec_unpackh(vscales_07);
  4890. vector signed int vscales_47 = vec_unpackl(vscales_07);
  4891. vector signed int vs0 = vec_splat(vscales_03, 0);
  4892. vector signed int vs1 = vec_splat(vscales_03, 1);
  4893. vector signed int vs2 = vec_splat(vscales_03, 2);
  4894. vector signed int vs3 = vec_splat(vscales_03, 3);
  4895. vector signed int vs4 = vec_splat(vscales_47, 0);
  4896. vector signed int vs5 = vec_splat(vscales_47, 1);
  4897. vector signed int vs6 = vec_splat(vscales_47, 2);
  4898. vector signed int vs7 = vec_splat(vscales_47, 3);
  4899. vscales = vec_sld(vscales, vscales, 8);
  4900. vsumi0 = vec_add(vec_mul(qv0, vs0), vsumi0);
  4901. vsumi1 = vec_add(vec_mul(qv1, vs2), vsumi1);
  4902. vsumi2 = vec_add(vec_mul(qv2, vs4), vsumi2);
  4903. vsumi3 = vec_add(vec_mul(qv3, vs6), vsumi3);
  4904. vsumi4 = vec_add(vec_mul(qv4, vs1), vsumi4);
  4905. vsumi5 = vec_add(vec_mul(qv5, vs3), vsumi5);
  4906. vsumi6 = vec_add(vec_mul(qv6, vs5), vsumi6);
  4907. vsumi7 = vec_add(vec_mul(qv7, vs7), vsumi7);
  4908. }
  4909. vsumi0 = vec_add(vsumi0, vsumi4);
  4910. vsumi1 = vec_add(vsumi1, vsumi5);
  4911. vsumi2 = vec_add(vsumi2, vsumi6);
  4912. vsumi3 = vec_add(vsumi3, vsumi7);
  4913. vsumf0 = vec_madd(vec_ctf(vsumi0, 0), vd, vsumf0);
  4914. vsumf1 = vec_madd(vec_ctf(vsumi1, 0), vd, vsumf1);
  4915. vsumf2 = vec_madd(vec_ctf(vsumi2, 0), vd, vsumf2);
  4916. vsumf3 = vec_madd(vec_ctf(vsumi3, 0), vd, vsumf3);
  4917. }
  4918. vsumf0 = vec_add(vsumf0, vsumf2);
  4919. vsumf1 = vec_add(vsumf1, vsumf3);
  4920. vsumf0 = vec_add(vsumf0, vsumf1);
  4921. vsumf0 = vec_add(vsumf0, vec_sld(vsumf0, vsumf0, 4));
  4922. vsumf0 = vec_add(vsumf0, vec_sld(vsumf0, vsumf0, 8));
  4923. *s = vec_extract(vsumf0, 0);
  4924. #elif defined __loongarch_asx
  4925. const __m256i m3 = __lasx_xvreplgr2vr_b(3);
  4926. const __m128i m4 = __lsx_vreplgr2vr_b(0xF);
  4927. __m256 acc = (__m256)__lasx_xvldi(0);
  4928. for (int i = 0; i < nb; ++i) {
  4929. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4930. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4931. const uint8_t * restrict q2 = x[i].qs;
  4932. const int8_t * restrict q8 = y[i].qs;
  4933. const __m128i mins_and_scales = __lsx_vld((const __m128i*)x[i].scales, 0);
  4934. const __m128i scales8 = __lsx_vand_v(mins_and_scales, m4);
  4935. const __m128i mins8 = __lsx_vand_v(__lsx_vsrli_h(mins_and_scales, 4), m4);
  4936. const __m256i mins = lasx_ext8_16(mins8);
  4937. const __m256i prod = lasx_madd_h(mins, __lasx_xvld((const __m256i*)y[i].bsums, 0));
  4938. acc = __lasx_xvfmadd_s(__lasx_xvreplfr2vr_s(dmin), __lasx_xvffint_s_w(prod), acc);
  4939. const __m256i all_scales = lasx_ext8_16(scales8);
  4940. const __m128i l_scales = lasx_extracti128(all_scales, 0);
  4941. const __m128i h_scales = lasx_extracti128(all_scales, 1);
  4942. const __m256i scales[2] = {lasx_insertf128(l_scales, l_scales), lasx_insertf128(h_scales, h_scales)};
  4943. __m256i sumi = __lasx_xvldi(0);
  4944. for (int j = 0; j < QK_K/128; ++j) {
  4945. const __m256i q2bits = __lasx_xvld((const __m256i*)q2, 0); q2 += 32;
  4946. const __m256i q8_0 = __lasx_xvld((const __m256i*)q8, 0); q8 += 32;
  4947. const __m256i q8_1 = __lasx_xvld((const __m256i*)q8, 0); q8 += 32;
  4948. const __m256i q8_2 = __lasx_xvld((const __m256i*)q8, 0); q8 += 32;
  4949. const __m256i q8_3 = __lasx_xvld((const __m256i*)q8, 0); q8 += 32;
  4950. const __m256i q2_0 = __lasx_xvand_v(q2bits, m3);
  4951. const __m256i q2_1 = __lasx_xvand_v(__lasx_xvsrli_h(q2bits, 2), m3);
  4952. const __m256i q2_2 = __lasx_xvand_v(__lasx_xvsrli_h(q2bits, 4), m3);
  4953. const __m256i q2_3 = __lasx_xvand_v(__lasx_xvsrli_h(q2bits, 6), m3);
  4954. __m256i p0 = lasx_maddubs_h(q2_0, q8_0);
  4955. __m256i p1 = lasx_maddubs_h(q2_1, q8_1);
  4956. __m256i p2 = lasx_maddubs_h(q2_2, q8_2);
  4957. __m256i p3 = lasx_maddubs_h(q2_3, q8_3);
  4958. p0 = lasx_madd_h(lasx_shuffle_b(scales[j], get_scale_shuffle_q3k(0)), p0);
  4959. p1 = lasx_madd_h(lasx_shuffle_b(scales[j], get_scale_shuffle_q3k(1)), p1);
  4960. p2 = lasx_madd_h(lasx_shuffle_b(scales[j], get_scale_shuffle_q3k(2)), p2);
  4961. p3 = lasx_madd_h(lasx_shuffle_b(scales[j], get_scale_shuffle_q3k(3)), p3);
  4962. p0 = __lasx_xvadd_w(p0, p1);
  4963. p2 = __lasx_xvadd_w(p2, p3);
  4964. sumi = __lasx_xvadd_w(sumi, __lasx_xvadd_w(p0, p2));
  4965. }
  4966. acc = __lasx_xvfmadd_s(__lasx_xvreplfr2vr_s(d), __lasx_xvffint_s_w(sumi), acc);
  4967. }
  4968. *s = hsum_float_8(acc);
  4969. #else
  4970. float sumf = 0;
  4971. for (int i = 0; i < nb; ++i) {
  4972. const uint8_t * q2 = x[i].qs;
  4973. const int8_t * q8 = y[i].qs;
  4974. const uint8_t * sc = x[i].scales;
  4975. int summs = 0;
  4976. for (int j = 0; j < 16; ++j) {
  4977. summs += y[i].bsums[j] * (sc[j] >> 4);
  4978. }
  4979. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4980. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4981. int isum = 0;
  4982. int is = 0;
  4983. int d;
  4984. for (int k = 0; k < QK_K/128; ++k) {
  4985. int shift = 0;
  4986. for (int j = 0; j < 4; ++j) {
  4987. d = sc[is++] & 0xF;
  4988. int isuml = 0;
  4989. for (int l = 0; l < 16; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
  4990. isum += d * isuml;
  4991. d = sc[is++] & 0xF;
  4992. isuml = 0;
  4993. for (int l = 16; l < 32; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
  4994. isum += d * isuml;
  4995. shift += 2;
  4996. q8 += 32;
  4997. }
  4998. q2 += 32;
  4999. }
  5000. sumf += dall * isum - dmin * summs;
  5001. }
  5002. *s = sumf;
  5003. #endif
  5004. }
  5005. void ggml_vec_dot_q3_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  5006. assert(n % QK_K == 0);
  5007. assert(nrc == 1);
  5008. UNUSED(nrc);
  5009. UNUSED(bx);
  5010. UNUSED(by);
  5011. UNUSED(bs);
  5012. const uint32_t kmask1 = 0x03030303;
  5013. const uint32_t kmask2 = 0x0f0f0f0f;
  5014. const block_q3_K * restrict x = vx;
  5015. const block_q8_K * restrict y = vy;
  5016. const int nb = n / QK_K;
  5017. #ifdef __ARM_NEON
  5018. uint32_t aux[3];
  5019. uint32_t utmp[4];
  5020. const uint8x16_t m3b = vdupq_n_u8(0x3);
  5021. const int32x4_t vzero = vdupq_n_s32(0);
  5022. const uint8x16_t m0 = vdupq_n_u8(1);
  5023. const uint8x16_t m1 = vshlq_n_u8(m0, 1);
  5024. const uint8x16_t m2 = vshlq_n_u8(m0, 2);
  5025. const uint8x16_t m3 = vshlq_n_u8(m0, 3);
  5026. const int8_t m32 = 32;
  5027. ggml_int8x16x4_t q3bytes;
  5028. float sum = 0;
  5029. for (int i = 0; i < nb; ++i) {
  5030. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5031. const uint8_t * restrict q3 = x[i].qs;
  5032. const uint8_t * restrict qh = x[i].hmask;
  5033. const int8_t * restrict q8 = y[i].qs;
  5034. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh);
  5035. ggml_uint8x16x4_t q3h;
  5036. int32_t isum = 0;
  5037. // Set up scales
  5038. memcpy(aux, x[i].scales, 12);
  5039. utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
  5040. utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
  5041. utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
  5042. utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
  5043. int8_t * scale = (int8_t *)utmp;
  5044. for (int j = 0; j < 16; ++j) scale[j] -= m32;
  5045. for (int j = 0; j < QK_K/128; ++j) {
  5046. const ggml_uint8x16x2_t q3bits = ggml_vld1q_u8_x2(q3); q3 += 32;
  5047. const ggml_int8x16x4_t q8bytes_1 = ggml_vld1q_s8_x4(q8); q8 += 64;
  5048. const ggml_int8x16x4_t q8bytes_2 = ggml_vld1q_s8_x4(q8); q8 += 64;
  5049. q3h.val[0] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[0]), 2);
  5050. q3h.val[1] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[1]), 2);
  5051. q3h.val[2] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[0]), 1);
  5052. q3h.val[3] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[1]), 1);
  5053. q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[0], m3b)), vreinterpretq_s8_u8(q3h.val[0]));
  5054. q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[1], m3b)), vreinterpretq_s8_u8(q3h.val[1]));
  5055. q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 2), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
  5056. q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 2), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
  5057. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes_1.val[0])) * scale[0];
  5058. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes_1.val[1])) * scale[1];
  5059. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes_1.val[2])) * scale[2];
  5060. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes_1.val[3])) * scale[3];
  5061. scale += 4;
  5062. q3h.val[0] = vbicq_u8(m2, qhbits.val[0]);
  5063. q3h.val[1] = vbicq_u8(m2, qhbits.val[1]);
  5064. q3h.val[2] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[0]), 1);
  5065. q3h.val[3] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[1]), 1);
  5066. q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 4), m3b)), vreinterpretq_s8_u8(q3h.val[0]));
  5067. q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 4), m3b)), vreinterpretq_s8_u8(q3h.val[1]));
  5068. q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 6), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
  5069. q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 6), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
  5070. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes_2.val[0])) * scale[0];
  5071. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes_2.val[1])) * scale[1];
  5072. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes_2.val[2])) * scale[2];
  5073. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes_2.val[3])) * scale[3];
  5074. scale += 4;
  5075. if (j == 0) {
  5076. qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 4);
  5077. qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 4);
  5078. }
  5079. }
  5080. sum += d * isum;
  5081. }
  5082. *s = sum;
  5083. #elif defined __AVX2__
  5084. const __m256i m3 = _mm256_set1_epi8(3);
  5085. const __m256i mone = _mm256_set1_epi8(1);
  5086. const __m128i m32 = _mm_set1_epi8(32);
  5087. __m256 acc = _mm256_setzero_ps();
  5088. uint32_t aux[3];
  5089. for (int i = 0; i < nb; ++i) {
  5090. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5091. const uint8_t * restrict q3 = x[i].qs;
  5092. const int8_t * restrict q8 = y[i].qs;
  5093. // Set up scales
  5094. memcpy(aux, x[i].scales, 12);
  5095. __m128i scales128 = _mm_set_epi32(
  5096. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  5097. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  5098. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  5099. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  5100. scales128 = _mm_sub_epi8(scales128, m32);
  5101. const __m256i all_scales = _mm256_cvtepi8_epi16(scales128);
  5102. const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
  5103. const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
  5104. const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
  5105. // high bit
  5106. const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].hmask);
  5107. // integer accumulator
  5108. __m256i sumi = _mm256_setzero_si256();
  5109. int bit = 0;
  5110. int is = 0;
  5111. for (int j = 0; j < QK_K/128; ++j) {
  5112. // load low 2 bits
  5113. const __m256i q3bits = _mm256_loadu_si256((const __m256i*)q3); q3 += 32;
  5114. // prepare low and high bits
  5115. const __m256i q3l_0 = _mm256_and_si256(q3bits, m3);
  5116. const __m256i q3h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  5117. ++bit;
  5118. const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 2), m3);
  5119. const __m256i q3h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  5120. ++bit;
  5121. const __m256i q3l_2 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 4), m3);
  5122. const __m256i q3h_2 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  5123. ++bit;
  5124. const __m256i q3l_3 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 6), m3);
  5125. const __m256i q3h_3 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  5126. ++bit;
  5127. // load Q8 quants
  5128. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5129. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5130. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5131. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5132. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  5133. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  5134. // and 2 if the high bit was set)
  5135. __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0);
  5136. __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1);
  5137. __m256i q8s_2 = _mm256_maddubs_epi16(q3h_2, q8_2);
  5138. __m256i q8s_3 = _mm256_maddubs_epi16(q3h_3, q8_3);
  5139. __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0);
  5140. __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1);
  5141. __m256i p16_2 = _mm256_maddubs_epi16(q3l_2, q8_2);
  5142. __m256i p16_3 = _mm256_maddubs_epi16(q3l_3, q8_3);
  5143. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  5144. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  5145. p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
  5146. p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
  5147. // multiply with scales
  5148. p16_0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 0)), p16_0);
  5149. p16_1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 1)), p16_1);
  5150. p16_2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 2)), p16_2);
  5151. p16_3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 3)), p16_3);
  5152. // accumulate
  5153. p16_0 = _mm256_add_epi32(p16_0, p16_1);
  5154. p16_2 = _mm256_add_epi32(p16_2, p16_3);
  5155. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_2));
  5156. }
  5157. // multiply with block scale and accumulate
  5158. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  5159. }
  5160. *s = hsum_float_8(acc);
  5161. #elif defined __AVX__
  5162. const __m128i m3 = _mm_set1_epi8(3);
  5163. const __m128i mone = _mm_set1_epi8(1);
  5164. const __m128i m32 = _mm_set1_epi8(32);
  5165. const __m128i m2 = _mm_set1_epi8(2);
  5166. __m256 acc = _mm256_setzero_ps();
  5167. const uint32_t *aux;
  5168. for (int i = 0; i < nb; ++i) {
  5169. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5170. const uint8_t * restrict q3 = x[i].qs;
  5171. const int8_t * restrict q8 = y[i].qs;
  5172. // Set up scales
  5173. aux = (const uint32_t *)x[i].scales;
  5174. __m128i scales128 = _mm_set_epi32(
  5175. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  5176. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  5177. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  5178. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  5179. scales128 = _mm_sub_epi8(scales128, m32);
  5180. const __m128i scales_0 = _mm_cvtepi8_epi16(scales128);
  5181. const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales128, scales128));
  5182. const __m128i scales[2] = { scales_0, scales_1 };
  5183. // high bit *128*2 from block_q3_K.hmask[QK_K/8]
  5184. const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].hmask[0]);
  5185. const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].hmask[16]);
  5186. // integer accumulator
  5187. __m128i sumi_0 = _mm_setzero_si128();
  5188. __m128i sumi_1 = _mm_setzero_si128();
  5189. for (int j = 0; j < QK_K/128; ++j) {
  5190. // load low 2 bits *64*2 from block_q3_K.qs[QK_K/4]
  5191. const __m128i q3bits_0 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
  5192. const __m128i q3bits_1 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
  5193. // prepare low and high bits
  5194. const int bit = j << 2;
  5195. const __m128i q3l_0 = _mm_and_si128(q3bits_0, m3);
  5196. const __m128i q3l_1 = _mm_and_si128(q3bits_1, m3);
  5197. const __m128i q3h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit)), bit), 2);
  5198. const __m128i q3h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit)), bit), 2);
  5199. const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 2), m3);
  5200. const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 2), m3);
  5201. const __m128i q3h_2 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
  5202. const __m128i q3h_3 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
  5203. const __m128i q3l_4 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 4), m3);
  5204. const __m128i q3l_5 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 4), m3);
  5205. const __m128i q3h_4 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
  5206. const __m128i q3h_5 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
  5207. const __m128i q3l_6 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 6), m3);
  5208. const __m128i q3l_7 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 6), m3);
  5209. const __m128i q3h_6 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
  5210. const __m128i q3h_7 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
  5211. // load Q8 quants from block_q8_K.qs[QK_K]
  5212. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5213. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5214. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5215. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5216. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5217. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5218. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5219. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5220. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  5221. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  5222. // and 2 if the high bit was set)
  5223. __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, q8_0);
  5224. __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, q8_1);
  5225. __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, q8_2);
  5226. __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, q8_3);
  5227. __m128i q8s_4 = _mm_maddubs_epi16(q3h_4, q8_4);
  5228. __m128i q8s_5 = _mm_maddubs_epi16(q3h_5, q8_5);
  5229. __m128i q8s_6 = _mm_maddubs_epi16(q3h_6, q8_6);
  5230. __m128i q8s_7 = _mm_maddubs_epi16(q3h_7, q8_7);
  5231. __m128i p16_0 = _mm_maddubs_epi16(q3l_0, q8_0);
  5232. __m128i p16_1 = _mm_maddubs_epi16(q3l_1, q8_1);
  5233. __m128i p16_2 = _mm_maddubs_epi16(q3l_2, q8_2);
  5234. __m128i p16_3 = _mm_maddubs_epi16(q3l_3, q8_3);
  5235. __m128i p16_4 = _mm_maddubs_epi16(q3l_4, q8_4);
  5236. __m128i p16_5 = _mm_maddubs_epi16(q3l_5, q8_5);
  5237. __m128i p16_6 = _mm_maddubs_epi16(q3l_6, q8_6);
  5238. __m128i p16_7 = _mm_maddubs_epi16(q3l_7, q8_7);
  5239. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  5240. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  5241. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  5242. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  5243. p16_4 = _mm_sub_epi16(p16_4, q8s_4);
  5244. p16_5 = _mm_sub_epi16(p16_5, q8s_5);
  5245. p16_6 = _mm_sub_epi16(p16_6, q8s_6);
  5246. p16_7 = _mm_sub_epi16(p16_7, q8s_7);
  5247. // multiply with scales
  5248. __m128i shuffle = _mm_set1_epi16(0x0100);
  5249. p16_0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_0);
  5250. shuffle = _mm_add_epi16(shuffle, m2);
  5251. p16_1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_1);
  5252. shuffle = _mm_add_epi16(shuffle, m2);
  5253. p16_2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_2);
  5254. shuffle = _mm_add_epi16(shuffle, m2);
  5255. p16_3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_3);
  5256. shuffle = _mm_add_epi16(shuffle, m2);
  5257. p16_4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_4);
  5258. shuffle = _mm_add_epi16(shuffle, m2);
  5259. p16_5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_5);
  5260. shuffle = _mm_add_epi16(shuffle, m2);
  5261. p16_6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_6);
  5262. shuffle = _mm_add_epi16(shuffle, m2);
  5263. p16_7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_7);
  5264. // accumulate
  5265. p16_0 = _mm_add_epi32(p16_0, p16_1);
  5266. p16_2 = _mm_add_epi32(p16_2, p16_3);
  5267. p16_4 = _mm_add_epi32(p16_4, p16_5);
  5268. p16_6 = _mm_add_epi32(p16_6, p16_7);
  5269. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  5270. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_4, p16_6));
  5271. }
  5272. // multiply with block scale and accumulate
  5273. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  5274. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
  5275. }
  5276. *s = hsum_float_8(acc);
  5277. #elif defined __riscv_v_intrinsic
  5278. uint32_t aux[3];
  5279. uint32_t utmp[4];
  5280. float sumf = 0;
  5281. for (int i = 0; i < nb; ++i) {
  5282. const uint8_t * restrict q3 = x[i].qs;
  5283. const uint8_t * restrict qh = x[i].hmask;
  5284. const int8_t * restrict q8 = y[i].qs;
  5285. memcpy(aux, x[i].scales, 12);
  5286. utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
  5287. utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
  5288. utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
  5289. utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
  5290. int8_t * scale = (int8_t *)utmp;
  5291. for (int j = 0; j < 16; ++j) scale[j] -= 32;
  5292. size_t vl = 32;
  5293. uint8_t m = 1;
  5294. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5295. vuint8m1_t vqh = __riscv_vle8_v_u8m1(qh, vl);
  5296. int sum_t = 0;
  5297. for (int j = 0; j < QK_K; j += 128) {
  5298. vl = 32;
  5299. // load Q3
  5300. vuint8m1_t q3_x = __riscv_vle8_v_u8m1(q3, vl);
  5301. vint8m1_t q3_0 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q3_x, 0x03, vl));
  5302. vint8m1_t q3_1 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x2, vl), 0x03 , vl));
  5303. vint8m1_t q3_2 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x4, vl), 0x03 , vl));
  5304. vint8m1_t q3_3 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x6, vl), 0x03 , vl));
  5305. // compute mask for subtraction
  5306. vuint8m1_t qh_m0 = __riscv_vand_vx_u8m1(vqh, m, vl);
  5307. vbool8_t vmask_0 = __riscv_vmseq_vx_u8m1_b8(qh_m0, 0, vl);
  5308. vint8m1_t q3_m0 = __riscv_vsub_vx_i8m1_m(vmask_0, q3_0, 0x4, vl);
  5309. m <<= 1;
  5310. vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
  5311. vbool8_t vmask_1 = __riscv_vmseq_vx_u8m1_b8(qh_m1, 0, vl);
  5312. vint8m1_t q3_m1 = __riscv_vsub_vx_i8m1_m(vmask_1, q3_1, 0x4, vl);
  5313. m <<= 1;
  5314. vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
  5315. vbool8_t vmask_2 = __riscv_vmseq_vx_u8m1_b8(qh_m2, 0, vl);
  5316. vint8m1_t q3_m2 = __riscv_vsub_vx_i8m1_m(vmask_2, q3_2, 0x4, vl);
  5317. m <<= 1;
  5318. vuint8m1_t qh_m3 = __riscv_vand_vx_u8m1(vqh, m, vl);
  5319. vbool8_t vmask_3 = __riscv_vmseq_vx_u8m1_b8(qh_m3, 0, vl);
  5320. vint8m1_t q3_m3 = __riscv_vsub_vx_i8m1_m(vmask_3, q3_3, 0x4, vl);
  5321. m <<= 1;
  5322. // load Q8 and take product with Q3
  5323. vint16m2_t a0 = __riscv_vwmul_vv_i16m2(q3_m0, __riscv_vle8_v_i8m1(q8, vl), vl);
  5324. vint16m2_t a1 = __riscv_vwmul_vv_i16m2(q3_m1, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  5325. vint16m2_t a2 = __riscv_vwmul_vv_i16m2(q3_m2, __riscv_vle8_v_i8m1(q8+64, vl), vl);
  5326. vint16m2_t a3 = __riscv_vwmul_vv_i16m2(q3_m3, __riscv_vle8_v_i8m1(q8+96, vl), vl);
  5327. vl = 16;
  5328. // retrieve lane to multiply with scale
  5329. vint32m2_t aux0_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a0, 0), (scale[0]), vl);
  5330. vint32m2_t aux0_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a0, 1), (scale[1]), vl);
  5331. vint32m2_t aux1_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a1, 0), (scale[2]), vl);
  5332. vint32m2_t aux1_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a1, 1), (scale[3]), vl);
  5333. vint32m2_t aux2_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a2, 0), (scale[4]), vl);
  5334. vint32m2_t aux2_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a2, 1), (scale[5]), vl);
  5335. vint32m2_t aux3_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a3, 0), (scale[6]), vl);
  5336. vint32m2_t aux3_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a3, 1), (scale[7]), vl);
  5337. vint32m1_t isum0 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux0_0, aux0_1, vl), vzero, vl);
  5338. vint32m1_t isum1 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux1_0, aux1_1, vl), isum0, vl);
  5339. vint32m1_t isum2 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux2_0, aux2_1, vl), isum1, vl);
  5340. vint32m1_t isum3 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux3_0, aux3_1, vl), isum2, vl);
  5341. sum_t += __riscv_vmv_x_s_i32m1_i32(isum3);
  5342. q3 += 32; q8 += 128; scale += 8;
  5343. }
  5344. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5345. sumf += d*sum_t;
  5346. }
  5347. *s = sumf;
  5348. #elif defined(__POWER9_VECTOR__)
  5349. const vector signed char lowMask = vec_splats((signed char)0x3);
  5350. const vector signed char lowMask1 = vec_splats((int8_t)0xf);
  5351. const vector signed char lowMask2 = vec_splats((int8_t)0x30);
  5352. const vector int v0 = vec_splats((int32_t)0);
  5353. const vector signed char v1 = vec_splats((signed char)0x1);
  5354. const vector unsigned char v2 = vec_splats((unsigned char)0x2);
  5355. const vector unsigned char v3 = vec_splats((unsigned char)0x3);
  5356. const vector unsigned char v4 = vec_splats((unsigned char)0x4);
  5357. const vector unsigned char v6 = vec_splats((unsigned char)0x6);
  5358. const vector signed char off = vec_splats((signed char)0x20);
  5359. vector float vsumf0 = vec_splats(0.0f);
  5360. vector float vsumf1 = vec_splats(0.0f);
  5361. vector float vsumf2 = vec_splats(0.0f);
  5362. vector float vsumf3 = vec_splats(0.0f);
  5363. for (int i = 0; i < nb; ++i) {
  5364. vector float vxd = vec_splats(GGML_FP16_TO_FP32(x[i].d));
  5365. vector float vyd = vec_splats(y[i].d);
  5366. vector float vd = vec_mul(vxd, vyd);
  5367. UNUSED(kmask1);
  5368. UNUSED(kmask2);
  5369. vector signed char u0 = (vector signed char)vec_xl_len(x[i].scales, 8);
  5370. vector signed char u1 = vec_and(u0, lowMask1);
  5371. vector signed char u2 = (vector signed char)vec_xl_len(x[i].scales + 8, 4);
  5372. vector signed char u3 = (vector signed char)vec_mergeh((vector signed int)u2, (vector signed int)vec_sr(u2, v2));
  5373. vector signed char u30 = vec_sl(vec_and(u3, lowMask), v4);
  5374. vector signed char u31 = vec_and(u3, lowMask2);
  5375. u1 = vec_or(u1, u30);
  5376. u2 = vec_or(vec_sr(u0, v4), u31);
  5377. vector signed char vscales = (vector signed char)vec_mergeh((vector signed long long)u1, (vector signed long long)u2);
  5378. vector signed char qxhs0 = (vector signed char)vec_xl( 0, x[i].hmask);
  5379. vector signed char qxhs1 = (vector signed char)vec_xl(16, x[i].hmask);
  5380. vscales = vec_sub(vscales, off);
  5381. vector signed int vsumi0 = v0;
  5382. vector signed int vsumi1 = v0;
  5383. vector signed int vsumi2 = v0;
  5384. vector signed int vsumi3 = v0;
  5385. vector signed int vsumi4 = v0;
  5386. vector signed int vsumi5 = v0;
  5387. vector signed int vsumi6 = v0;
  5388. vector signed int vsumi7 = v0;
  5389. const uint8_t * restrict q3 = x[i].qs;
  5390. const int8_t * restrict q8 = y[i].qs;
  5391. for (int j = 0; j < QK_K/128; ++j) {
  5392. __builtin_prefetch(q3, 0, 1);
  5393. __builtin_prefetch(q8, 0, 1);
  5394. vector signed char qxs0 = (vector signed char)vec_xl( 0, q3);
  5395. vector signed char qxs1 = (vector signed char)vec_xl(16, q3);
  5396. q3 += 32;
  5397. //the low 2 bits
  5398. vector signed char qxs00 = vec_and(qxs0, lowMask);
  5399. vector signed char qxs01 = vec_and(vec_sr(qxs0, v2), lowMask);
  5400. vector signed char qxs02 = vec_and(vec_sr(qxs0, v4), lowMask);
  5401. vector signed char qxs03 = vec_and(vec_sr(qxs0, v6), lowMask);
  5402. vector signed char qxs10 = vec_and(qxs1, lowMask);
  5403. vector signed char qxs11 = vec_and(vec_sr(qxs1, v2), lowMask);
  5404. vector signed char qxs12 = vec_and(vec_sr(qxs1, v4), lowMask);
  5405. vector signed char qxs13 = vec_and(vec_sr(qxs1, v6), lowMask);
  5406. //the 3rd bit
  5407. vector signed char qxh00 = vec_sl(vec_andc(v1, qxhs0), v2);
  5408. vector signed char qxh01 = vec_sl(vec_andc(v1, vec_sr(qxhs0, (vector unsigned char)v1)), v2);
  5409. vector signed char qxh02 = vec_sl(vec_andc(v1, vec_sr(qxhs0, v2)), v2);
  5410. vector signed char qxh03 = vec_sl(vec_andc(v1, vec_sr(qxhs0, v3)), v2);
  5411. vector signed char qxh10 = vec_sl(vec_andc(v1, qxhs1), v2);
  5412. vector signed char qxh11 = vec_sl(vec_andc(v1, vec_sr(qxhs1, (vector unsigned char)v1)), v2);
  5413. vector signed char qxh12 = vec_sl(vec_andc(v1, vec_sr(qxhs1, v2)), v2);
  5414. vector signed char qxh13 = vec_sl(vec_andc(v1, vec_sr(qxhs1, v3)), v2);
  5415. qxhs0 = vec_sr(qxhs0, v4);
  5416. qxhs1 = vec_sr(qxhs1, v4);
  5417. vector signed char q3x00 = vec_sub(qxs00, qxh00);
  5418. vector signed char q3x01 = vec_sub(qxs01, qxh01);
  5419. vector signed char q3x02 = vec_sub(qxs02, qxh02);
  5420. vector signed char q3x03 = vec_sub(qxs03, qxh03);
  5421. vector signed char q3x10 = vec_sub(qxs10, qxh10);
  5422. vector signed char q3x11 = vec_sub(qxs11, qxh11);
  5423. vector signed char q3x12 = vec_sub(qxs12, qxh12);
  5424. vector signed char q3x13 = vec_sub(qxs13, qxh13);
  5425. vector signed char q8y00 = vec_xl( 0, q8);
  5426. vector signed char q8y10 = vec_xl( 16, q8);
  5427. vector signed char q8y01 = vec_xl( 32, q8);
  5428. vector signed char q8y11 = vec_xl( 48, q8);
  5429. vector signed char q8y02 = vec_xl( 64, q8);
  5430. vector signed char q8y12 = vec_xl( 80, q8);
  5431. vector signed char q8y03 = vec_xl( 96, q8);
  5432. vector signed char q8y13 = vec_xl(112, q8);
  5433. q8 += 128;
  5434. vector signed short vscales_h = vec_unpackh(vscales);
  5435. vector signed short vs0 = vec_splat(vscales_h, 0);
  5436. vector signed short vs1 = vec_splat(vscales_h, 1);
  5437. vector signed short vs2 = vec_splat(vscales_h, 2);
  5438. vector signed short vs3 = vec_splat(vscales_h, 3);
  5439. vector signed short vs4 = vec_splat(vscales_h, 4);
  5440. vector signed short vs5 = vec_splat(vscales_h, 5);
  5441. vector signed short vs6 = vec_splat(vscales_h, 6);
  5442. vector signed short vs7 = vec_splat(vscales_h, 7);
  5443. vscales = vec_sld(vscales, vscales, 8);
  5444. vector signed short qv00 = vec_add(vec_mule(q3x00, q8y00), vec_mulo(q3x00, q8y00));
  5445. vector signed short qv01 = vec_add(vec_mule(q3x01, q8y01), vec_mulo(q3x01, q8y01));
  5446. vector signed short qv02 = vec_add(vec_mule(q3x02, q8y02), vec_mulo(q3x02, q8y02));
  5447. vector signed short qv03 = vec_add(vec_mule(q3x03, q8y03), vec_mulo(q3x03, q8y03));
  5448. vector signed short qv10 = vec_add(vec_mule(q3x10, q8y10), vec_mulo(q3x10, q8y10));
  5449. vector signed short qv11 = vec_add(vec_mule(q3x11, q8y11), vec_mulo(q3x11, q8y11));
  5450. vector signed short qv12 = vec_add(vec_mule(q3x12, q8y12), vec_mulo(q3x12, q8y12));
  5451. vector signed short qv13 = vec_add(vec_mule(q3x13, q8y13), vec_mulo(q3x13, q8y13));
  5452. vsumi0 = vec_msum(qv00, vs0, vsumi0);
  5453. vsumi1 = vec_msum(qv01, vs2, vsumi1);
  5454. vsumi2 = vec_msum(qv02, vs4, vsumi2);
  5455. vsumi3 = vec_msum(qv03, vs6, vsumi3);
  5456. vsumi4 = vec_msum(qv10, vs1, vsumi4);
  5457. vsumi5 = vec_msum(qv11, vs3, vsumi5);
  5458. vsumi6 = vec_msum(qv12, vs5, vsumi6);
  5459. vsumi7 = vec_msum(qv13, vs7, vsumi7);
  5460. }
  5461. vsumi0 = vec_add(vsumi0, vsumi4);
  5462. vsumi1 = vec_add(vsumi1, vsumi5);
  5463. vsumi2 = vec_add(vsumi2, vsumi6);
  5464. vsumi3 = vec_add(vsumi3, vsumi7);
  5465. vsumf0 = vec_madd(vec_ctf(vsumi0, 0), vd, vsumf0);
  5466. vsumf1 = vec_madd(vec_ctf(vsumi1, 0), vd, vsumf1);
  5467. vsumf2 = vec_madd(vec_ctf(vsumi2, 0), vd, vsumf2);
  5468. vsumf3 = vec_madd(vec_ctf(vsumi3, 0), vd, vsumf3);
  5469. }
  5470. vsumf0 = vec_add(vsumf0, vsumf2);
  5471. vsumf1 = vec_add(vsumf1, vsumf3);
  5472. vsumf0 = vec_add(vsumf0, vsumf1);
  5473. vsumf0 = vec_add(vsumf0, vec_sld(vsumf0, vsumf0, 4));
  5474. vsumf0 = vec_add(vsumf0, vec_sld(vsumf0, vsumf0, 8));
  5475. *s = vec_extract(vsumf0, 0);
  5476. #elif defined __loongarch_asx
  5477. const __m256i m3 = __lasx_xvreplgr2vr_b(3);
  5478. const __m256i mone = __lasx_xvreplgr2vr_b(1);
  5479. const __m128i m32 = __lsx_vreplgr2vr_b(32);
  5480. __m256 acc = (__m256)__lasx_xvldi(0);
  5481. uint32_t aux[3];
  5482. for (int i = 0; i < nb; ++i) {
  5483. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5484. const uint8_t * restrict q3 = x[i].qs;
  5485. const int8_t * restrict q8 = y[i].qs;
  5486. // Set up scales
  5487. memcpy(aux, x[i].scales, 12);
  5488. __m128i scales128 = lsx_set_w(
  5489. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  5490. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  5491. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  5492. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  5493. scales128 = __lsx_vsub_b(scales128, m32);
  5494. const __m256i all_scales = lasx_ext8_16(scales128);
  5495. const __m128i l_scales = lasx_extracti128(all_scales, 0);
  5496. const __m128i h_scales = lasx_extracti128(all_scales, 1);
  5497. const __m256i scales[2] = {lasx_insertf128(l_scales, l_scales), lasx_insertf128(h_scales, h_scales)};
  5498. // high bit
  5499. const __m256i hbits = __lasx_xvld((const __m256i*)x[i].hmask, 0);
  5500. // integer accumulator
  5501. __m256i sumi = __lasx_xvldi(0);
  5502. int bit = 0;
  5503. int is = 0;
  5504. __m256i xvbit;
  5505. for (int j = 0; j < QK_K/128; ++j) {
  5506. // load low 2 bits
  5507. const __m256i q3bits = __lasx_xvld((const __m256i*)q3, 0); q3 += 32;
  5508. xvbit = __lasx_xvreplgr2vr_h(bit);
  5509. // prepare low and high bits
  5510. const __m256i q3l_0 = __lasx_xvand_v(q3bits, m3);
  5511. const __m256i q3h_0 = __lasx_xvslli_h(__lasx_xvsrl_h(__lasx_xvandn_v(hbits, __lasx_xvsll_h(mone, xvbit)), xvbit), 2);
  5512. ++bit;
  5513. xvbit = __lasx_xvreplgr2vr_h(bit);
  5514. const __m256i q3l_1 = __lasx_xvand_v(__lasx_xvsrli_h(q3bits, 2), m3);
  5515. const __m256i q3h_1 = __lasx_xvslli_h(__lasx_xvsrl_h(__lasx_xvandn_v(hbits, __lasx_xvsll_h(mone, xvbit)), xvbit), 2);
  5516. ++bit;
  5517. xvbit = __lasx_xvreplgr2vr_h(bit);
  5518. const __m256i q3l_2 = __lasx_xvand_v(__lasx_xvsrli_h(q3bits, 4), m3);
  5519. const __m256i q3h_2 = __lasx_xvslli_h(__lasx_xvsrl_h(__lasx_xvandn_v(hbits, __lasx_xvsll_h(mone, xvbit)), xvbit), 2);
  5520. ++bit;
  5521. xvbit = __lasx_xvreplgr2vr_h(bit);
  5522. const __m256i q3l_3 = __lasx_xvand_v(__lasx_xvsrli_h(q3bits, 6), m3);
  5523. const __m256i q3h_3 = __lasx_xvslli_h(__lasx_xvsrl_h(__lasx_xvandn_v(hbits, __lasx_xvsll_h(mone, xvbit)), xvbit), 2);
  5524. ++bit;
  5525. // load Q8 quants
  5526. const __m256i q8_0 = __lasx_xvld((const __m256i*)q8, 0); q8 += 32;
  5527. const __m256i q8_1 = __lasx_xvld((const __m256i*)q8, 0); q8 += 32;
  5528. const __m256i q8_2 = __lasx_xvld((const __m256i*)q8, 0); q8 += 32;
  5529. const __m256i q8_3 = __lasx_xvld((const __m256i*)q8, 0); q8 += 32;
  5530. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use lasx_maddubs_h,
  5531. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  5532. // and 2 if the high bit was set)
  5533. __m256i q8s_0 = lasx_maddubs_h(q3h_0, q8_0);
  5534. __m256i q8s_1 = lasx_maddubs_h(q3h_1, q8_1);
  5535. __m256i q8s_2 = lasx_maddubs_h(q3h_2, q8_2);
  5536. __m256i q8s_3 = lasx_maddubs_h(q3h_3, q8_3);
  5537. __m256i p16_0 = lasx_maddubs_h(q3l_0, q8_0);
  5538. __m256i p16_1 = lasx_maddubs_h(q3l_1, q8_1);
  5539. __m256i p16_2 = lasx_maddubs_h(q3l_2, q8_2);
  5540. __m256i p16_3 = lasx_maddubs_h(q3l_3, q8_3);
  5541. p16_0 = __lasx_xvsub_h(p16_0, q8s_0);
  5542. p16_1 = __lasx_xvsub_h(p16_1, q8s_1);
  5543. p16_2 = __lasx_xvsub_h(p16_2, q8s_2);
  5544. p16_3 = __lasx_xvsub_h(p16_3, q8s_3);
  5545. // multiply with scales
  5546. p16_0 = lasx_madd_h(lasx_shuffle_b(scales[j], get_scale_shuffle_q3k(is + 0)), p16_0);
  5547. p16_1 = lasx_madd_h(lasx_shuffle_b(scales[j], get_scale_shuffle_q3k(is + 1)), p16_1);
  5548. p16_2 = lasx_madd_h(lasx_shuffle_b(scales[j], get_scale_shuffle_q3k(is + 2)), p16_2);
  5549. p16_3 = lasx_madd_h(lasx_shuffle_b(scales[j], get_scale_shuffle_q3k(is + 3)), p16_3);
  5550. // accumulate
  5551. p16_0 = __lasx_xvadd_w(p16_0, p16_1);
  5552. p16_2 = __lasx_xvadd_w(p16_2, p16_3);
  5553. sumi = __lasx_xvadd_w(sumi, __lasx_xvadd_w(p16_0, p16_2));
  5554. }
  5555. // multiply with block scale and accumulate
  5556. acc = __lasx_xvfmadd_s(__lasx_xvreplfr2vr_s(d), __lasx_xvffint_s_w(sumi), acc);//FIXME
  5557. }
  5558. *s = hsum_float_8(acc);
  5559. #else
  5560. // scalar version
  5561. // This function is written like this so the compiler can manage to vectorize most of it
  5562. // Using -Ofast, GCC and clang manage to produce code that is within a factor of 2 or so from the
  5563. // manually vectorized version above. Every other version I tried would run at least 4 times slower.
  5564. // The ideal situation would be if we could just write the code once, and the compiler would
  5565. // automatically produce the best possible set of machine instructions, instead of us having to manually
  5566. // write vectorized versions for AVX, ARM_NEON, etc.
  5567. int8_t aux8[QK_K];
  5568. int16_t aux16[8];
  5569. float sums [8];
  5570. int32_t aux32[8];
  5571. memset(sums, 0, 8*sizeof(float));
  5572. uint32_t auxs[4];
  5573. const int8_t * scales = (const int8_t*)auxs;
  5574. float sumf = 0;
  5575. for (int i = 0; i < nb; ++i) {
  5576. const uint8_t * restrict q3 = x[i].qs;
  5577. const uint8_t * restrict hm = x[i].hmask;
  5578. const int8_t * restrict q8 = y[i].qs;
  5579. memset(aux32, 0, 8*sizeof(int32_t));
  5580. int8_t * restrict a = aux8;
  5581. uint8_t m = 1;
  5582. for (int j = 0; j < QK_K; j += 128) {
  5583. for (int l = 0; l < 32; ++l) a[l] = q3[l] & 3;
  5584. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  5585. a += 32; m <<= 1;
  5586. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 2) & 3;
  5587. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  5588. a += 32; m <<= 1;
  5589. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 4) & 3;
  5590. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  5591. a += 32; m <<= 1;
  5592. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 6) & 3;
  5593. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  5594. a += 32; m <<= 1;
  5595. q3 += 32;
  5596. }
  5597. a = aux8;
  5598. memcpy(auxs, x[i].scales, 12);
  5599. uint32_t tmp = auxs[2];
  5600. auxs[2] = ((auxs[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
  5601. auxs[3] = ((auxs[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
  5602. auxs[0] = (auxs[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
  5603. auxs[1] = (auxs[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
  5604. for (int j = 0; j < QK_K/16; ++j) {
  5605. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5606. for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
  5607. q8 += 8; a += 8;
  5608. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5609. for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
  5610. q8 += 8; a += 8;
  5611. }
  5612. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5613. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5614. }
  5615. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5616. *s = sumf;
  5617. #endif
  5618. }
  5619. void ggml_vec_dot_q4_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  5620. assert(n % QK_K == 0);
  5621. assert(nrc == 1);
  5622. UNUSED(nrc);
  5623. UNUSED(bx);
  5624. UNUSED(by);
  5625. UNUSED(bs);
  5626. const block_q4_K * restrict x = vx;
  5627. const block_q8_K * restrict y = vy;
  5628. const int nb = n / QK_K;
  5629. static const uint32_t kmask1 = 0x3f3f3f3f;
  5630. static const uint32_t kmask2 = 0x0f0f0f0f;
  5631. static const uint32_t kmask3 = 0x03030303;
  5632. uint32_t utmp[4];
  5633. #ifdef __ARM_NEON
  5634. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5635. const int32x4_t mzero = vdupq_n_s32(0);
  5636. ggml_int8x16x2_t q4bytes;
  5637. ggml_int8x16x2_t q8bytes;
  5638. float sumf = 0;
  5639. for (int i = 0; i < nb; ++i) {
  5640. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5641. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5642. const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
  5643. memcpy(utmp, x[i].scales, 12);
  5644. uint32x2_t mins8 = { 0 };
  5645. mins8 = vset_lane_u32(utmp[1] & kmask1, mins8, 0);
  5646. mins8 = vset_lane_u32(((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4), mins8, 1);
  5647. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5648. utmp[0] &= kmask1;
  5649. const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(vreinterpret_u8_u32(mins8)));
  5650. const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
  5651. vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
  5652. sumf -= dmin * vaddvq_s32(prod);
  5653. const uint8_t * scales = (const uint8_t *)utmp;
  5654. const uint8_t * restrict q4 = x[i].qs;
  5655. const int8_t * restrict q8 = y[i].qs;
  5656. int32_t sumi1 = 0;
  5657. int32_t sumi2 = 0;
  5658. for (int j = 0; j < QK_K/64; ++j) {
  5659. const ggml_uint8x16x2_t q4bits = ggml_vld1q_u8_x2(q4); q4 += 32;
  5660. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  5661. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  5662. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  5663. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5664. sumi1 += vaddvq_s32(p1) * scales[2*j+0];
  5665. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  5666. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  5667. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  5668. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5669. sumi2 += vaddvq_s32(p2) * scales[2*j+1];
  5670. }
  5671. sumf += d * (sumi1 + sumi2);
  5672. }
  5673. *s = sumf;
  5674. #elif defined __AVX2__
  5675. const __m256i m4 = _mm256_set1_epi8(0xF);
  5676. __m256 acc = _mm256_setzero_ps();
  5677. __m128 acc_m = _mm_setzero_ps();
  5678. for (int i = 0; i < nb; ++i) {
  5679. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5680. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5681. memcpy(utmp, x[i].scales, 12);
  5682. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5683. const uint32_t uaux = utmp[1] & kmask1;
  5684. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5685. utmp[2] = uaux;
  5686. utmp[0] &= kmask1;
  5687. const uint8_t * restrict q4 = x[i].qs;
  5688. const int8_t * restrict q8 = y[i].qs;
  5689. const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
  5690. const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
  5691. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  5692. const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
  5693. acc_m = _mm_fmadd_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod), acc_m);
  5694. const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
  5695. const __m256i scales = MM256_SET_M128I(sc128, sc128);
  5696. __m256i sumi = _mm256_setzero_si256();
  5697. for (int j = 0; j < QK_K/64; ++j) {
  5698. const __m256i scale_l = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
  5699. const __m256i scale_h = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
  5700. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  5701. const __m256i q4l = _mm256_and_si256(q4bits, m4);
  5702. const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
  5703. const __m256i q8l = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5704. __m256i p16l = _mm256_maddubs_epi16(q4l, q8l);
  5705. p16l = _mm256_madd_epi16(scale_l, p16l);
  5706. const __m256i q8h = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5707. __m256i p16h = _mm256_maddubs_epi16(q4h, q8h);
  5708. p16h = _mm256_madd_epi16(scale_h, p16h);
  5709. const __m256i sumj = _mm256_add_epi32(p16l, p16h);
  5710. sumi = _mm256_add_epi32(sumi, sumj);
  5711. }
  5712. __m256 vd = _mm256_set1_ps(d);
  5713. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
  5714. }
  5715. acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
  5716. acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
  5717. *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
  5718. #elif defined __AVX__
  5719. const __m128i m4 = _mm_set1_epi8(0xF);
  5720. const __m128i m2 = _mm_set1_epi8(0x2);
  5721. __m256 acc = _mm256_setzero_ps();
  5722. __m128 acc_m = _mm_setzero_ps();
  5723. for (int i = 0; i < nb; ++i) {
  5724. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5725. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5726. const uint8_t * restrict q4 = x[i].qs;
  5727. const int8_t * restrict q8 = y[i].qs;
  5728. memcpy(utmp, x[i].scales, 12);
  5729. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5730. const uint32_t uaux = utmp[1] & kmask1;
  5731. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5732. utmp[2] = uaux;
  5733. utmp[0] &= kmask1;
  5734. const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
  5735. const __m128i scales = _mm_cvtepu8_epi16(utmps);
  5736. const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
  5737. const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
  5738. const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
  5739. const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
  5740. const __m128i prod = _mm_madd_epi16(mins, q8s);
  5741. acc_m = _mm_add_ps(_mm_mul_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod)), acc_m);
  5742. __m128i sumi_0 = _mm_setzero_si128();
  5743. __m128i sumi_1 = _mm_setzero_si128();
  5744. __m128i shuffle = _mm_set1_epi16(0x0100);
  5745. for (int j = 0; j < QK_K/64; ++j) {
  5746. const __m128i scale_l = _mm_shuffle_epi8(scales, shuffle);
  5747. shuffle = _mm_add_epi16(shuffle, m2);
  5748. const __m128i scale_h = _mm_shuffle_epi8(scales, shuffle);
  5749. shuffle = _mm_add_epi16(shuffle, m2);
  5750. __m128i q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5751. const __m128i q4l_0 = _mm_and_si128(q4bits, m4);
  5752. const __m128i q4h_0 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
  5753. q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5754. const __m128i q4l_1 = _mm_and_si128(q4bits, m4);
  5755. const __m128i q4h_1 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
  5756. const __m128i q8l_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5757. __m128i p16l = _mm_maddubs_epi16(q4l_0, q8l_0);
  5758. p16l = _mm_madd_epi16(scale_l, p16l);
  5759. sumi_0 = _mm_add_epi32(sumi_0, p16l);
  5760. const __m128i q8l_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5761. p16l = _mm_maddubs_epi16(q4l_1, q8l_1);
  5762. p16l = _mm_madd_epi16(scale_l, p16l);
  5763. sumi_1 = _mm_add_epi32(sumi_1, p16l);
  5764. const __m128i q8h_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5765. __m128i p16h = _mm_maddubs_epi16(q4h_0, q8h_0);
  5766. p16h = _mm_madd_epi16(scale_h, p16h);
  5767. sumi_0 = _mm_add_epi32(sumi_0, p16h);
  5768. const __m128i q8h_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5769. p16h = _mm_maddubs_epi16(q4h_1, q8h_1);
  5770. p16h = _mm_madd_epi16(scale_h, p16h);
  5771. sumi_1 = _mm_add_epi32(sumi_1, p16h);
  5772. }
  5773. __m256 vd = _mm256_set1_ps(d);
  5774. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  5775. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
  5776. }
  5777. acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
  5778. acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
  5779. *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
  5780. #elif defined __riscv_v_intrinsic
  5781. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5782. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5783. float sumf = 0;
  5784. for (int i = 0; i < nb; ++i) {
  5785. size_t vl = 8;
  5786. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5787. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5788. vint16mf2_t q8sums_0 = __riscv_vlse16_v_i16mf2(y[i].bsums, 4, vl);
  5789. vint16mf2_t q8sums_1 = __riscv_vlse16_v_i16mf2(y[i].bsums+1, 4, vl);
  5790. vint16mf2_t q8sums = __riscv_vadd_vv_i16mf2(q8sums_0, q8sums_1, vl);
  5791. memcpy(utmp, x[i].scales, 12);
  5792. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5793. const uint32_t uaux = utmp[1] & kmask1;
  5794. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5795. utmp[2] = uaux;
  5796. utmp[0] &= kmask1;
  5797. vuint8mf4_t mins8 = __riscv_vle8_v_u8mf4(mins, vl);
  5798. vint16mf2_t v_mins = __riscv_vreinterpret_v_u16mf2_i16mf2(__riscv_vzext_vf2_u16mf2(mins8, vl));
  5799. vint32m1_t prod = __riscv_vwmul_vv_i32m1(q8sums, v_mins, vl);
  5800. vint32m1_t sumi = __riscv_vredsum_vs_i32m1_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  5801. sumf -= dmin * __riscv_vmv_x_s_i32m1_i32(sumi);
  5802. const uint8_t * restrict q4 = x[i].qs;
  5803. const int8_t * restrict q8 = y[i].qs;
  5804. vl = 32;
  5805. int32_t sum_1 = 0;
  5806. int32_t sum_2 = 0;
  5807. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  5808. for (int j = 0; j < QK_K/64; ++j) {
  5809. // load Q4
  5810. vuint8m1_t q4_x = __riscv_vle8_v_u8m1(q4, vl);
  5811. // load Q8 and multiply it with lower Q4 nibble
  5812. vint8m1_t q8_0 = __riscv_vle8_v_i8m1(q8, vl);
  5813. vint8m1_t q4_0 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q4_x, 0x0F, vl));
  5814. vint16m2_t qv_0 = __riscv_vwmul_vv_i16m2(q4_0, q8_0, vl);
  5815. vint16m1_t vs_0 = __riscv_vredsum_vs_i16m2_i16m1(qv_0, vzero, vl);
  5816. sum_1 += __riscv_vmv_x_s_i16m1_i16(vs_0) * scales[2*j+0];
  5817. // load Q8 and multiply it with upper Q4 nibble
  5818. vint8m1_t q8_1 = __riscv_vle8_v_i8m1(q8+32, vl);
  5819. vint8m1_t q4_1 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q4_x, 0x04, vl));
  5820. vint16m2_t qv_1 = __riscv_vwmul_vv_i16m2(q4_1, q8_1, vl);
  5821. vint16m1_t vs_1 = __riscv_vredsum_vs_i16m2_i16m1(qv_1, vzero, vl);
  5822. sum_2 += __riscv_vmv_x_s_i16m1_i16(vs_1) * scales[2*j+1];
  5823. q4 += 32; q8 += 64;
  5824. }
  5825. sumf += d*(sum_1 + sum_2);
  5826. }
  5827. *s = sumf;
  5828. #elif defined(__POWER9_VECTOR__)
  5829. const vector signed char lowMask = vec_splats((signed char)0xF);
  5830. const vector signed char lowMask1 = vec_splats((int8_t)0x3f);
  5831. const vector signed char lowMask2 = vec_splats((int8_t)0x30);
  5832. const vector int v0 = vec_splats((int32_t)0);
  5833. const vector unsigned char v2 = vec_splats((uint8_t)2);
  5834. const vector unsigned char v4 = vec_splats((unsigned char)0x4);
  5835. vector float vsumf0 = vec_splats(0.0f);
  5836. vector float vsumf1 = vec_splats(0.0f);
  5837. vector float vsumf2 = vec_splats(0.0f);
  5838. vector float vsumf3 = vec_splats(0.0f);
  5839. for (int i = 0; i < nb; ++i) {
  5840. vector float vxd = vec_splats(GGML_FP16_TO_FP32(x[i].d));
  5841. vector float vyd = vec_splats(y[i].d);
  5842. vector float vd = vec_mul(vxd, vyd);
  5843. vector float vxmin = vec_splats(GGML_FP16_TO_FP32(x[i].dmin));
  5844. vector float vdmin = vec_mul(vxmin, vyd);
  5845. vector signed short q8ysums0 = vec_xl( 0, y[i].bsums);
  5846. vector signed short q8ysums1 = vec_xl(16, y[i].bsums);
  5847. UNUSED(kmask1);
  5848. UNUSED(kmask2);
  5849. UNUSED(kmask3);
  5850. UNUSED(utmp);
  5851. vector signed char u0 = (vector signed char)vec_xl_len(x[i].scales, 8);
  5852. vector signed char u1 = vec_and(vec_sr(u0, v2), lowMask2);
  5853. vector signed char u2 = (vector signed char)vec_xl_len(x[i].scales + 8, 4);
  5854. vector signed char u3 = vec_sr(u2, v4);
  5855. vector signed char u30 = u1;
  5856. vector signed char u31 = (vector signed char)vec_mergeh((vector signed int)vec_and(u2, lowMask), (vector signed int)u3);
  5857. u1 = vec_and(u0, lowMask1);
  5858. u2 = vec_or(u30, u31);
  5859. vector signed char utmps = (vector signed char)vec_mergeh((vector signed int)u1, (vector signed int)u2);
  5860. vector signed short vscales = vec_unpackh(utmps);
  5861. vector signed short q4xmins = vec_unpackl(utmps);
  5862. vector signed short q4xmins0 = vec_mergeh(q4xmins, q4xmins);
  5863. vector signed short q4xmins1 = vec_mergel(q4xmins, q4xmins);
  5864. vector signed int prod0 = vec_mule(q4xmins0, q8ysums0);
  5865. vector signed int prod1 = vec_mule(q4xmins1, q8ysums1);
  5866. vector signed int prod2 = vec_mulo(q4xmins0, q8ysums0);
  5867. vector signed int prod3 = vec_mulo(q4xmins1, q8ysums1);
  5868. vsumf0 = vec_nmsub(vec_ctf(prod0, 0), vdmin, vsumf0);
  5869. vsumf1 = vec_nmsub(vec_ctf(prod1, 0), vdmin, vsumf1);
  5870. vsumf2 = vec_nmsub(vec_ctf(prod2, 0), vdmin, vsumf2);
  5871. vsumf3 = vec_nmsub(vec_ctf(prod3, 0), vdmin, vsumf3);
  5872. vector signed int vsumi0 = v0;
  5873. vector signed int vsumi1 = v0;
  5874. vector signed int vsumi2 = v0;
  5875. vector signed int vsumi3 = v0;
  5876. const uint8_t * restrict q4 = x[i].qs;
  5877. const int8_t * restrict q8 = y[i].qs;
  5878. for (int j = 0; j < QK_K/64; j+=2) {
  5879. __builtin_prefetch(q4, 0, 1);
  5880. __builtin_prefetch(q8, 0, 1);
  5881. vector signed char qxs0 = (vector signed char)vec_xl( 0, q4);
  5882. vector signed char qxs1 = (vector signed char)vec_xl(16, q4);
  5883. vector signed char qxs2 = (vector signed char)vec_xl(32, q4);
  5884. vector signed char qxs3 = (vector signed char)vec_xl(48, q4);
  5885. q4 += 64;
  5886. vector unsigned char q4x00 = (vector unsigned char)vec_and(qxs0, lowMask);
  5887. vector unsigned char q4x01 = (vector unsigned char)vec_sr(qxs0, v4);
  5888. vector unsigned char q4x10 = (vector unsigned char)vec_and(qxs1, lowMask);
  5889. vector unsigned char q4x11 = (vector unsigned char)vec_sr(qxs1, v4);
  5890. vector unsigned char q4x20 = (vector unsigned char)vec_and(qxs2, lowMask);
  5891. vector unsigned char q4x21 = (vector unsigned char)vec_sr(qxs2, v4);
  5892. vector unsigned char q4x30 = (vector unsigned char)vec_and(qxs3, lowMask);
  5893. vector unsigned char q4x31 = (vector unsigned char)vec_sr(qxs3, v4);
  5894. vector signed char q8y00 = vec_xl( 0, q8);
  5895. vector signed char q8y10 = vec_xl( 16, q8);
  5896. vector signed char q8y01 = vec_xl( 32, q8);
  5897. vector signed char q8y11 = vec_xl( 48, q8);
  5898. vector signed char q8y20 = vec_xl( 64, q8);
  5899. vector signed char q8y30 = vec_xl( 80, q8);
  5900. vector signed char q8y21 = vec_xl( 96, q8);
  5901. vector signed char q8y31 = vec_xl(112, q8);
  5902. q8 += 128;
  5903. vector signed int qv00 = vec_msum(q8y00, q4x00, v0);
  5904. vector signed int qv01 = vec_msum(q8y01, q4x01, v0);
  5905. vector signed int qv10 = vec_msum(q8y10, q4x10, v0);
  5906. vector signed int qv11 = vec_msum(q8y11, q4x11, v0);
  5907. vector signed int qv20 = vec_msum(q8y20, q4x20, v0);
  5908. vector signed int qv21 = vec_msum(q8y21, q4x21, v0);
  5909. vector signed int qv30 = vec_msum(q8y30, q4x30, v0);
  5910. vector signed int qv31 = vec_msum(q8y31, q4x31, v0);
  5911. vector signed int vscales_h = vec_unpackh(vscales);
  5912. vector signed int vs0 = vec_splat(vscales_h, 0);
  5913. vector signed int vs1 = vec_splat(vscales_h, 1);
  5914. vector signed int vs2 = vec_splat(vscales_h, 2);
  5915. vector signed int vs3 = vec_splat(vscales_h, 3);
  5916. vscales = vec_sld(vscales, vscales, 8);
  5917. vsumi0 = vec_add(vec_mul(qv00, vs0), vsumi0);
  5918. vsumi1 = vec_add(vec_mul(qv01, vs1), vsumi1);
  5919. vsumi2 = vec_add(vec_mul(qv20, vs2), vsumi2);
  5920. vsumi3 = vec_add(vec_mul(qv21, vs3), vsumi3);
  5921. vsumi0 = vec_add(vec_mul(qv10, vs0), vsumi0);
  5922. vsumi1 = vec_add(vec_mul(qv11, vs1), vsumi1);
  5923. vsumi2 = vec_add(vec_mul(qv30, vs2), vsumi2);
  5924. vsumi3 = vec_add(vec_mul(qv31, vs3), vsumi3);
  5925. }
  5926. vsumf0 = vec_madd(vec_ctf(vsumi0, 0), vd, vsumf0);
  5927. vsumf1 = vec_madd(vec_ctf(vsumi1, 0), vd, vsumf1);
  5928. vsumf2 = vec_madd(vec_ctf(vsumi2, 0), vd, vsumf2);
  5929. vsumf3 = vec_madd(vec_ctf(vsumi3, 0), vd, vsumf3);
  5930. }
  5931. vsumf0 = vec_add(vsumf0, vsumf2);
  5932. vsumf1 = vec_add(vsumf1, vsumf3);
  5933. vsumf0 = vec_add(vsumf0, vsumf1);
  5934. vsumf0 = vec_add(vsumf0, vec_sld(vsumf0, vsumf0, 4));
  5935. vsumf0 = vec_add(vsumf0, vec_sld(vsumf0, vsumf0, 8));
  5936. *s = vec_extract(vsumf0, 0);
  5937. #elif defined __loongarch_asx
  5938. GGML_UNUSED(kmask1);
  5939. GGML_UNUSED(kmask2);
  5940. GGML_UNUSED(kmask3);
  5941. const __m256i m4 = __lasx_xvreplgr2vr_b(0xF);
  5942. __m256 acc = (__m256)__lasx_xvldi(0);
  5943. __m128 acc_m = (__m128)__lsx_vldi(0);
  5944. for (int i = 0; i < nb; ++i) {
  5945. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5946. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5947. memcpy(utmp, x[i].scales, 12);
  5948. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5949. const uint32_t uaux = utmp[1] & kmask1;
  5950. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5951. utmp[2] = uaux;
  5952. utmp[0] &= kmask1;
  5953. const uint8_t * restrict q4 = x[i].qs;
  5954. const int8_t * restrict q8 = y[i].qs;
  5955. const __m256i mins_and_scales = lasx_extu8_16(lsx_set_w(utmp[3], utmp[2], utmp[1], utmp[0]));
  5956. const __m256i q8sums = __lasx_xvld((const __m256i*)y[i].bsums, 0);
  5957. const __m128i q8s = lsx_hadd_h(lasx_extracti128(q8sums, 0), lasx_extracti128(q8sums, 1));
  5958. const __m128i prod = lsx_madd_h(lasx_extracti128(mins_and_scales, 1), q8s);
  5959. acc_m = __lsx_vfmadd_s(__lsx_vreplfr2vr_s(dmin), __lsx_vffint_s_w(prod), acc_m);
  5960. const __m128i sc128 = lasx_extracti128(mins_and_scales, 0);
  5961. const __m256i scales = lasx_insertf128(sc128, sc128);
  5962. __m256i sumi = __lasx_xvldi(0);
  5963. for (int j = 0; j < QK_K/64; ++j) {
  5964. const __m256i scale_l = lasx_shuffle_b(scales, get_scale_shuffle_k4(2*j+0));
  5965. const __m256i scale_h = lasx_shuffle_b(scales, get_scale_shuffle_k4(2*j+1));
  5966. const __m256i q4bits = __lasx_xvld((const __m256i*)q4, 0); q4 += 32;
  5967. const __m256i q4l = __lasx_xvand_v(q4bits, m4);
  5968. const __m256i q4h = __lasx_xvand_v(__lasx_xvsrli_h(q4bits, 4), m4);
  5969. const __m256i q8l = __lasx_xvld((const __m256i*)q8, 0); q8 += 32;
  5970. __m256i p16l = lasx_maddubs_h(q4l, q8l);
  5971. p16l = lasx_madd_h(scale_l, p16l);
  5972. const __m256i q8h = __lasx_xvld((const __m256i*)q8, 0); q8 += 32;
  5973. __m256i p16h = lasx_maddubs_h(q4h, q8h);
  5974. p16h = lasx_madd_h(scale_h, p16h);
  5975. const __m256i sumj = __lasx_xvadd_w(p16l, p16h);
  5976. sumi = __lasx_xvadd_w(sumi, sumj);
  5977. }
  5978. __m256 vd = __lasx_xvreplfr2vr_s(d);
  5979. acc = __lasx_xvfmadd_s(vd, __lasx_xvffint_s_w(sumi), acc);
  5980. }
  5981. acc_m = __lsx_vfadd_s(acc_m, (__m128)__lsx_vpermi_w((__m128i)acc_m, (__m128i)acc_m, 0xee));
  5982. __m128i tmp1 = __lsx_vinsgr2vr_w(__lsx_vldi(0), __lsx_vpickve2gr_w((__m128i)acc_m, 1), 0);
  5983. acc_m = __lsx_vfadd_s(acc_m, (__m128)tmp1);
  5984. ft_union fi;
  5985. fi.i = __lsx_vpickve2gr_w(acc_m, 0);
  5986. *s = hsum_float_8(acc) + fi.f ;
  5987. #else
  5988. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5989. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5990. int8_t aux8[QK_K];
  5991. int16_t aux16[8];
  5992. float sums [8];
  5993. int32_t aux32[8];
  5994. memset(sums, 0, 8*sizeof(float));
  5995. float sumf = 0;
  5996. for (int i = 0; i < nb; ++i) {
  5997. const uint8_t * restrict q4 = x[i].qs;
  5998. const int8_t * restrict q8 = y[i].qs;
  5999. memset(aux32, 0, 8*sizeof(int32_t));
  6000. int8_t * restrict a = aux8;
  6001. for (int j = 0; j < QK_K/64; ++j) {
  6002. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
  6003. a += 32;
  6004. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] >> 4);
  6005. a += 32; q4 += 32;
  6006. }
  6007. memcpy(utmp, x[i].scales, 12);
  6008. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  6009. const uint32_t uaux = utmp[1] & kmask1;
  6010. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  6011. utmp[2] = uaux;
  6012. utmp[0] &= kmask1;
  6013. int sumi = 0;
  6014. for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
  6015. a = aux8;
  6016. int is = 0;
  6017. for (int j = 0; j < QK_K/32; ++j) {
  6018. int32_t scale = scales[is++];
  6019. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6020. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6021. q8 += 8; a += 8;
  6022. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6023. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6024. q8 += 8; a += 8;
  6025. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6026. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6027. q8 += 8; a += 8;
  6028. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6029. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6030. q8 += 8; a += 8;
  6031. }
  6032. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6033. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  6034. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  6035. sumf -= dmin * sumi;
  6036. }
  6037. for (int l = 0; l < 8; ++l) sumf += sums[l];
  6038. *s = sumf;
  6039. #endif
  6040. }
  6041. void ggml_vec_dot_q5_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  6042. assert(n % QK_K == 0);
  6043. assert(nrc == 1);
  6044. UNUSED(nrc);
  6045. UNUSED(bx);
  6046. UNUSED(by);
  6047. UNUSED(bs);
  6048. const block_q5_K * restrict x = vx;
  6049. const block_q8_K * restrict y = vy;
  6050. const int nb = n / QK_K;
  6051. static const uint32_t kmask1 = 0x3f3f3f3f;
  6052. static const uint32_t kmask2 = 0x0f0f0f0f;
  6053. static const uint32_t kmask3 = 0x03030303;
  6054. uint32_t utmp[4];
  6055. #ifdef __ARM_NEON
  6056. const uint8x16_t m4b = vdupq_n_u8(0xf);
  6057. const uint8x16_t mone = vdupq_n_u8(1);
  6058. const uint8x16_t mtwo = vdupq_n_u8(2);
  6059. const int32x4_t mzero = vdupq_n_s32(0);
  6060. ggml_int8x16x4_t q5bytes;
  6061. float sumf = 0;
  6062. for (int i = 0; i < nb; ++i) {
  6063. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6064. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  6065. const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
  6066. memcpy(utmp, x[i].scales, 12);
  6067. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  6068. const uint32_t uaux = utmp[1] & kmask1;
  6069. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  6070. utmp[2] = uaux;
  6071. utmp[0] &= kmask1;
  6072. const uint8x8_t mins8 = vld1_u8((const uint8_t*)utmp + 8);
  6073. const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(mins8));
  6074. const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
  6075. vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
  6076. int32_t sumi_mins = vaddvq_s32(prod);
  6077. const uint8_t * scales = (const uint8_t *)utmp;
  6078. const uint8_t * restrict q5 = x[i].qs;
  6079. const uint8_t * restrict qh = x[i].qh;
  6080. const int8_t * restrict q8 = y[i].qs;
  6081. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh);
  6082. ggml_uint8x16x4_t q5h;
  6083. int32_t sumi = 0;
  6084. for (int j = 0; j < QK_K/64; ++j) {
  6085. const ggml_uint8x16x2_t q5bits = ggml_vld1q_u8_x2(q5); q5 += 32;
  6086. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  6087. q5h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
  6088. q5h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
  6089. q5h.val[2] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[0]), 3);
  6090. q5h.val[3] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[1]), 3);
  6091. qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 2);
  6092. qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 2);
  6093. q5bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[0], m4b), q5h.val[0]));
  6094. q5bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[1], m4b), q5h.val[1]));
  6095. q5bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[0], 4), q5h.val[2]));
  6096. q5bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[1], 4), q5h.val[3]));
  6097. sumi += vaddvq_s32(ggml_vdotq_s32(ggml_vdotq_s32(mzero, q5bytes.val[0], q8bytes.val[0]), q5bytes.val[1], q8bytes.val[1])) * *scales++;
  6098. sumi += vaddvq_s32(ggml_vdotq_s32(ggml_vdotq_s32(mzero, q5bytes.val[2], q8bytes.val[2]), q5bytes.val[3], q8bytes.val[3])) * *scales++;
  6099. }
  6100. sumf += d * sumi - dmin * sumi_mins;
  6101. }
  6102. *s = sumf;
  6103. #elif defined __AVX2__
  6104. const __m256i m4 = _mm256_set1_epi8(0xF);
  6105. const __m128i mzero = _mm_setzero_si128();
  6106. const __m256i mone = _mm256_set1_epi8(1);
  6107. __m256 acc = _mm256_setzero_ps();
  6108. float summs = 0.f;
  6109. for (int i = 0; i < nb; ++i) {
  6110. const uint8_t * restrict q5 = x[i].qs;
  6111. const int8_t * restrict q8 = y[i].qs;
  6112. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6113. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  6114. memcpy(utmp, x[i].scales, 12);
  6115. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  6116. const uint32_t uaux = utmp[1] & kmask1;
  6117. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  6118. utmp[2] = uaux;
  6119. utmp[0] &= kmask1;
  6120. const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
  6121. const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
  6122. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  6123. const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
  6124. const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
  6125. summs += dmin * _mm_extract_epi32(hsum, 0);
  6126. const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
  6127. const __m256i scales = MM256_SET_M128I(sc128, sc128);
  6128. const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].qh);
  6129. __m256i hmask = mone;
  6130. __m256i sumi = _mm256_setzero_si256();
  6131. int bit = 0;
  6132. for (int j = 0; j < QK_K/64; ++j) {
  6133. const __m256i scale_0 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
  6134. const __m256i scale_1 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
  6135. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5); q5 += 32;
  6136. const __m256i q5l_0 = _mm256_and_si256(q5bits, m4);
  6137. const __m256i q5h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
  6138. const __m256i q5_0 = _mm256_add_epi8(q5l_0, q5h_0);
  6139. hmask = _mm256_slli_epi16(hmask, 1);
  6140. const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4);
  6141. const __m256i q5h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
  6142. const __m256i q5_1 = _mm256_add_epi8(q5l_1, q5h_1);
  6143. hmask = _mm256_slli_epi16(hmask, 1);
  6144. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6145. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6146. __m256i p16_0 = _mm256_maddubs_epi16(q5_0, q8_0);
  6147. __m256i p16_1 = _mm256_maddubs_epi16(q5_1, q8_1);
  6148. p16_0 = _mm256_madd_epi16(scale_0, p16_0);
  6149. p16_1 = _mm256_madd_epi16(scale_1, p16_1);
  6150. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  6151. }
  6152. __m256 vd = _mm256_set1_ps(d);
  6153. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
  6154. }
  6155. *s = hsum_float_8(acc) + summs;
  6156. #elif defined __AVX__
  6157. const __m128i m4 = _mm_set1_epi8(0xF);
  6158. const __m128i mzero = _mm_setzero_si128();
  6159. const __m128i mone = _mm_set1_epi8(1);
  6160. const __m128i m2 = _mm_set1_epi8(2);
  6161. __m256 acc = _mm256_setzero_ps();
  6162. float summs = 0.f;
  6163. for (int i = 0; i < nb; ++i) {
  6164. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6165. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  6166. const uint8_t * restrict q5 = x[i].qs;
  6167. const int8_t * restrict q8 = y[i].qs;
  6168. memcpy(utmp, x[i].scales, 12);
  6169. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  6170. const uint32_t uaux = utmp[1] & kmask1;
  6171. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  6172. utmp[2] = uaux;
  6173. utmp[0] &= kmask1;
  6174. const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
  6175. const __m128i scales = _mm_cvtepu8_epi16(utmps);
  6176. const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
  6177. const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
  6178. const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
  6179. const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
  6180. const __m128i prod = _mm_madd_epi16(mins, q8s);
  6181. const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
  6182. summs += dmin * _mm_extract_epi32(hsum, 0);
  6183. const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].qh[0]);
  6184. const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].qh[16]);
  6185. __m128i hmask = mone;
  6186. __m128i sumi_0 = _mm_setzero_si128();
  6187. __m128i sumi_1 = _mm_setzero_si128();
  6188. int bit = 0;
  6189. __m128i shuffle = _mm_set1_epi16(0x0100);
  6190. for (int j = 0; j < QK_K/64; ++j) {
  6191. const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
  6192. shuffle = _mm_add_epi16(shuffle, m2);
  6193. const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
  6194. shuffle = _mm_add_epi16(shuffle, m2);
  6195. const __m128i q5bits_0 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
  6196. const __m128i q5bits_1 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
  6197. __m128i q5l_0 = _mm_and_si128(q5bits_0, m4);
  6198. __m128i q5l_1 = _mm_and_si128(q5bits_1, m4);
  6199. __m128i q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
  6200. __m128i q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
  6201. __m128i q5_0 = _mm_add_epi8(q5l_0, q5h_0);
  6202. __m128i q5_1 = _mm_add_epi8(q5l_1, q5h_1);
  6203. hmask = _mm_slli_epi16(hmask, 1);
  6204. __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6205. __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6206. __m128i p16_0 = _mm_maddubs_epi16(q5_0, q8_0);
  6207. __m128i p16_1 = _mm_maddubs_epi16(q5_1, q8_1);
  6208. p16_0 = _mm_madd_epi16(scale_0, p16_0);
  6209. p16_1 = _mm_madd_epi16(scale_0, p16_1);
  6210. q5l_0 = _mm_and_si128(_mm_srli_epi16(q5bits_0, 4), m4);
  6211. q5l_1 = _mm_and_si128(_mm_srli_epi16(q5bits_1, 4), m4);
  6212. q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
  6213. q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
  6214. q5_0 = _mm_add_epi8(q5l_0, q5h_0);
  6215. q5_1 = _mm_add_epi8(q5l_1, q5h_1);
  6216. hmask = _mm_slli_epi16(hmask, 1);
  6217. q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6218. q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6219. __m128i p16_2 = _mm_maddubs_epi16(q5_0, q8_0);
  6220. __m128i p16_3 = _mm_maddubs_epi16(q5_1, q8_1);
  6221. p16_2 = _mm_madd_epi16(scale_1, p16_2);
  6222. p16_3 = _mm_madd_epi16(scale_1, p16_3);
  6223. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  6224. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  6225. }
  6226. __m256 vd = _mm256_set1_ps(d);
  6227. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  6228. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
  6229. }
  6230. *s = hsum_float_8(acc) + summs;
  6231. #elif defined __riscv_v_intrinsic
  6232. const uint8_t * scales = (const uint8_t*)&utmp[0];
  6233. const uint8_t * mins = (const uint8_t*)&utmp[2];
  6234. float sumf = 0;
  6235. float sums = 0.0;
  6236. size_t vl;
  6237. for (int i = 0; i < nb; ++i) {
  6238. vl = 8;
  6239. const uint8_t * restrict q5 = x[i].qs;
  6240. const uint8_t * restrict hm = x[i].qh;
  6241. const int8_t * restrict q8 = y[i].qs;
  6242. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6243. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  6244. vint16mf2_t q8sums_0 = __riscv_vlse16_v_i16mf2(y[i].bsums, 4, vl);
  6245. vint16mf2_t q8sums_1 = __riscv_vlse16_v_i16mf2(y[i].bsums+1, 4, vl);
  6246. vint16mf2_t q8sums = __riscv_vadd_vv_i16mf2(q8sums_0, q8sums_1, vl);
  6247. memcpy(utmp, x[i].scales, 12);
  6248. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  6249. const uint32_t uaux = utmp[1] & kmask1;
  6250. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  6251. utmp[2] = uaux;
  6252. utmp[0] &= kmask1;
  6253. vuint8mf4_t mins8 = __riscv_vle8_v_u8mf4(mins, vl);
  6254. vint16mf2_t v_mins = __riscv_vreinterpret_v_u16mf2_i16mf2(__riscv_vzext_vf2_u16mf2(mins8, vl));
  6255. vint32m1_t prod = __riscv_vwmul_vv_i32m1(q8sums, v_mins, vl);
  6256. vint32m1_t sumi = __riscv_vredsum_vs_i32m1_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  6257. sumf -= dmin * __riscv_vmv_x_s_i32m1_i32(sumi);
  6258. vl = 32;
  6259. int32_t aux32 = 0;
  6260. int is = 0;
  6261. uint8_t m = 1;
  6262. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  6263. vuint8m1_t vqh = __riscv_vle8_v_u8m1(hm, vl);
  6264. for (int j = 0; j < QK_K/64; ++j) {
  6265. // load Q5 and Q8
  6266. vuint8m1_t q5_x = __riscv_vle8_v_u8m1(q5, vl);
  6267. vint8m1_t q8_y1 = __riscv_vle8_v_i8m1(q8, vl);
  6268. vint8m1_t q8_y2 = __riscv_vle8_v_i8m1(q8+32, vl);
  6269. // compute mask for addition
  6270. vint8m1_t q5_a = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q5_x, 0x0F, vl));
  6271. vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
  6272. vbool8_t vmask_1 = __riscv_vmsne_vx_u8m1_b8(qh_m1, 0, vl);
  6273. vint8m1_t q5_m1 = __riscv_vadd_vx_i8m1_m(vmask_1, q5_a, 16, vl);
  6274. m <<= 1;
  6275. vint8m1_t q5_l = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q5_x, 0x04, vl));
  6276. vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
  6277. vbool8_t vmask_2 = __riscv_vmsne_vx_u8m1_b8(qh_m2, 0, vl);
  6278. vint8m1_t q5_m2 = __riscv_vadd_vx_i8m1_m(vmask_2, q5_l, 16, vl);
  6279. m <<= 1;
  6280. vint16m2_t v0 = __riscv_vwmul_vv_i16m2(q5_m1, q8_y1, vl);
  6281. vint16m2_t v1 = __riscv_vwmul_vv_i16m2(q5_m2, q8_y2, vl);
  6282. vint32m4_t vs1 = __riscv_vwmul_vx_i32m4(v0, scales[is++], vl);
  6283. vint32m4_t vs2 = __riscv_vwmul_vx_i32m4(v1, scales[is++], vl);
  6284. vint32m1_t vacc1 = __riscv_vredsum_vs_i32m4_i32m1(vs1, vzero, vl);
  6285. vint32m1_t vacc2 = __riscv_vredsum_vs_i32m4_i32m1(vs2, vzero, vl);
  6286. aux32 += __riscv_vmv_x_s_i32m1_i32(vacc1) + __riscv_vmv_x_s_i32m1_i32(vacc2);
  6287. q5 += 32; q8 += 64;
  6288. }
  6289. vfloat32m1_t vaux = __riscv_vfmul_vf_f32m1(__riscv_vfmv_v_f_f32m1(aux32, 1), d, 1);
  6290. sums += __riscv_vfmv_f_s_f32m1_f32(vaux);
  6291. }
  6292. *s = sumf+sums;
  6293. #elif defined(__POWER9_VECTOR__)
  6294. const vector signed char lowMask = vec_splats((signed char)0xF);
  6295. const vector signed char lowMask1 = vec_splats((int8_t)0x3f);
  6296. const vector signed char lowMask2 = vec_splats((int8_t)0x30);
  6297. const vector int v0 = vec_splats((int32_t)0);
  6298. const vector unsigned char v1 = vec_splats((unsigned char)0x1);
  6299. const vector unsigned char v2 = vec_splats((unsigned char)0x2);
  6300. const vector unsigned char v3 = vec_splats((unsigned char)0x3);
  6301. const vector unsigned char v4 = vec_splats((unsigned char)0x4);
  6302. vector float vsumf0 = vec_splats(0.0f);
  6303. vector float vsumf1 = vec_splats(0.0f);
  6304. vector float vsumf2 = vec_splats(0.0f);
  6305. vector float vsumf3 = vec_splats(0.0f);
  6306. for (int i = 0; i < nb; ++i) {
  6307. vector float vxd = vec_splats(GGML_FP16_TO_FP32(x[i].d));
  6308. vector float vyd = vec_splats(y[i].d);
  6309. vector float vd = vec_mul(vxd, vyd);
  6310. vector float vxmin = vec_splats(GGML_FP16_TO_FP32(x[i].dmin));
  6311. vector float vdmin = vec_mul(vxmin, vyd);
  6312. UNUSED(kmask1);
  6313. UNUSED(kmask2);
  6314. UNUSED(kmask3);
  6315. UNUSED(utmp);
  6316. vector signed char u0 = (vector signed char)vec_xl_len(x[i].scales, 8);
  6317. vector signed char u1 = vec_and(vec_sr(u0, v2), lowMask2);
  6318. vector signed char u2 = (vector signed char)vec_xl_len(x[i].scales + 8, 4);
  6319. vector signed char u3 = vec_sr(u2, v4);
  6320. vector signed char u30 = u1;
  6321. vector signed char u31 = (vector signed char)vec_mergeh((vector signed int)vec_and(u2, lowMask), (vector signed int)u3);
  6322. u1 = vec_and(u0, lowMask1);
  6323. u2 = vec_or(u30, u31);
  6324. vector signed char utmps = (vector signed char)vec_mergeh((vector signed int)u1, (vector signed int)u2);
  6325. vector signed short q8ysums0 = vec_xl( 0, y[i].bsums);
  6326. vector signed short q8ysums1 = vec_xl(16, y[i].bsums);
  6327. vector signed short vscales = vec_unpackh(utmps);
  6328. vector signed short q5xmins = vec_unpackl(utmps);
  6329. vector signed short q5xmins0 = vec_mergeh(q5xmins, q5xmins);
  6330. vector signed short q5xmins1 = vec_mergel(q5xmins, q5xmins);
  6331. vector signed int prod0 = vec_mule(q5xmins0, q8ysums0);
  6332. vector signed int prod1 = vec_mule(q5xmins1, q8ysums1);
  6333. vector signed int prod2 = vec_mulo(q5xmins0, q8ysums0);
  6334. vector signed int prod3 = vec_mulo(q5xmins1, q8ysums1);
  6335. vsumf0 = vec_nmsub(vec_ctf(prod0, 0), vdmin, vsumf0);
  6336. vsumf1 = vec_nmsub(vec_ctf(prod1, 0), vdmin, vsumf1);
  6337. vsumf2 = vec_nmsub(vec_ctf(prod2, 0), vdmin, vsumf2);
  6338. vsumf3 = vec_nmsub(vec_ctf(prod3, 0), vdmin, vsumf3);
  6339. vector signed char qxhs0 = (vector signed char)vec_xl( 0, x[i].qh);
  6340. vector signed char qxhs1 = (vector signed char)vec_xl(16, x[i].qh);
  6341. vector signed int vsumi0 = v0;
  6342. vector signed int vsumi1 = v0;
  6343. vector signed int vsumi2 = v0;
  6344. vector signed int vsumi3 = v0;
  6345. const uint8_t * restrict q5 = x[i].qs;
  6346. const int8_t * restrict q8 = y[i].qs;
  6347. for (int j = 0; j < QK_K/64; ++j) {
  6348. __builtin_prefetch(q5, 0, 1);
  6349. __builtin_prefetch(q8, 0, 1);
  6350. vector signed char qxs0 = (vector signed char)vec_xl( 0, q5);
  6351. vector signed char qxs1 = (vector signed char)vec_xl(16, q5);
  6352. q5 += 32;
  6353. vector signed char qxs00 = vec_and(qxs0, lowMask);
  6354. vector signed char qxs01 = vec_sr(qxs0, v4);
  6355. vector signed char qxs10 = vec_and(qxs1, lowMask);
  6356. vector signed char qxs11 = vec_sr(qxs1, v4);
  6357. vector signed char q5h00 = vec_sl(vec_and((vector signed char)v1, qxhs0), v4);
  6358. vector signed char q5h01 = vec_sl(vec_and((vector signed char)v2, qxhs0), v3);
  6359. vector signed char q5h10 = vec_sl(vec_and((vector signed char)v1, qxhs1), v4);
  6360. vector signed char q5h11 = vec_sl(vec_and((vector signed char)v2, qxhs1), v3);
  6361. qxhs0 = vec_sr(qxhs0, v2);
  6362. qxhs1 = vec_sr(qxhs1, v2);
  6363. vector unsigned char q5x00 = (vector unsigned char)vec_or(q5h00, qxs00);
  6364. vector unsigned char q5x01 = (vector unsigned char)vec_or(q5h01, qxs01);
  6365. vector unsigned char q5x10 = (vector unsigned char)vec_or(q5h10, qxs10);
  6366. vector unsigned char q5x11 = (vector unsigned char)vec_or(q5h11, qxs11);
  6367. vector signed char q8y00 = vec_xl( 0, q8);
  6368. vector signed char q8y10 = vec_xl(16, q8);
  6369. vector signed char q8y01 = vec_xl(32, q8);
  6370. vector signed char q8y11 = vec_xl(48, q8);
  6371. q8 += 64;
  6372. vector signed int qv00 = vec_msum(q8y00, q5x00, v0);
  6373. vector signed int qv01 = vec_msum(q8y01, q5x01, v0);
  6374. vector signed int qv10 = vec_msum(q8y10, q5x10, v0);
  6375. vector signed int qv11 = vec_msum(q8y11, q5x11, v0);
  6376. vector signed int vscales_h = vec_unpackh(vscales);
  6377. vector signed int vs0 = vec_splat(vscales_h, 0);
  6378. vector signed int vs1 = vec_splat(vscales_h, 1);
  6379. vscales = vec_sld(vscales, vscales, 12);
  6380. vsumi0 = vec_add(vec_mul(qv00, vs0), vsumi0);
  6381. vsumi1 = vec_add(vec_mul(qv10, vs0), vsumi1);
  6382. vsumi2 = vec_add(vec_mul(qv01, vs1), vsumi2);
  6383. vsumi3 = vec_add(vec_mul(qv11, vs1), vsumi3);
  6384. }
  6385. vsumf0 = vec_madd(vec_ctf(vsumi0, 0), vd, vsumf0);
  6386. vsumf1 = vec_madd(vec_ctf(vsumi1, 0), vd, vsumf1);
  6387. vsumf2 = vec_madd(vec_ctf(vsumi2, 0), vd, vsumf2);
  6388. vsumf3 = vec_madd(vec_ctf(vsumi3, 0), vd, vsumf3);
  6389. }
  6390. vsumf0 = vec_add(vsumf0, vsumf2);
  6391. vsumf1 = vec_add(vsumf1, vsumf3);
  6392. vsumf0 = vec_add(vsumf0, vsumf1);
  6393. vsumf0 = vec_add(vsumf0, vec_sld(vsumf0, vsumf0, 4));
  6394. vsumf0 = vec_add(vsumf0, vec_sld(vsumf0, vsumf0, 8));
  6395. *s = vec_extract(vsumf0, 0);
  6396. #elif defined __loongarch_asx
  6397. GGML_UNUSED(kmask1);
  6398. GGML_UNUSED(kmask2);
  6399. GGML_UNUSED(kmask3);
  6400. const __m256i m4 = __lasx_xvreplgr2vr_b(0xF);
  6401. const __m128i mzero = __lsx_vldi(0);
  6402. const __m256i mone = __lasx_xvreplgr2vr_b(1);
  6403. __m256 acc = (__m256)__lasx_xvldi(0);
  6404. float summs = 0.f;
  6405. for (int i = 0; i < nb; ++i) {
  6406. const uint8_t * restrict q5 = x[i].qs;
  6407. const int8_t * restrict q8 = y[i].qs;
  6408. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6409. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  6410. memcpy(utmp, x[i].scales, 12);
  6411. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  6412. const uint32_t uaux = utmp[1] & kmask1;
  6413. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  6414. utmp[2] = uaux;
  6415. utmp[0] &= kmask1;
  6416. const __m256i mins_and_scales = lasx_extu8_16(lsx_set_w(utmp[3], utmp[2], utmp[1], utmp[0]));
  6417. const __m256i q8sums = __lasx_xvld((const __m256i*)y[i].bsums, 0);
  6418. const __m128i q8s = lsx_hadd_h(lasx_extracti128(q8sums, 0), lasx_extracti128(q8sums, 1));
  6419. const __m128i prod = lsx_madd_h(lasx_extracti128(mins_and_scales, 1), q8s);
  6420. const __m128i hsum = lsx_hadd_w(lsx_hadd_w(prod, mzero), mzero);
  6421. summs += dmin * __lsx_vpickve2gr_w(hsum, 0); //TODO check
  6422. const __m128i sc128 = lasx_extracti128(mins_and_scales, 0);
  6423. const __m256i scales = lasx_insertf128(sc128, sc128);
  6424. const __m256i hbits = __lasx_xvld((const __m256i*)x[i].qh, 0);
  6425. __m256i hmask = mone;
  6426. __m256i sumi = __lasx_xvldi(0);
  6427. int bit = 0;
  6428. __m256i xvbit;
  6429. for (int j = 0; j < QK_K/64; ++j) {
  6430. const __m256i scale_0 = lasx_shuffle_b(scales, get_scale_shuffle_k4(2*j+0));
  6431. const __m256i scale_1 = lasx_shuffle_b(scales, get_scale_shuffle_k4(2*j+1));
  6432. const __m256i q5bits = __lasx_xvld((const __m256i*)q5, 0); q5 += 32;
  6433. xvbit = __lasx_xvreplgr2vr_h(bit++);
  6434. const __m256i q5l_0 = __lasx_xvand_v(q5bits, m4);
  6435. const __m256i q5h_0 = __lasx_xvslli_h(__lasx_xvsrl_h(__lasx_xvand_v(hbits, hmask), xvbit), 4);
  6436. const __m256i q5_0 = __lasx_xvadd_b(q5l_0, q5h_0);
  6437. hmask = __lasx_xvslli_h(hmask, 1);
  6438. xvbit = __lasx_xvreplgr2vr_h(bit++);
  6439. const __m256i q5l_1 = __lasx_xvand_v(__lasx_xvsrli_h(q5bits, 4), m4);
  6440. const __m256i q5h_1 = __lasx_xvslli_h(__lasx_xvsrl_h(__lasx_xvand_v(hbits, hmask), xvbit), 4);
  6441. const __m256i q5_1 = __lasx_xvadd_b(q5l_1, q5h_1);
  6442. hmask = __lasx_xvslli_h(hmask, 1);
  6443. const __m256i q8_0 = __lasx_xvld((const __m256i*)q8, 0); q8 += 32;
  6444. const __m256i q8_1 = __lasx_xvld((const __m256i*)q8, 0); q8 += 32;
  6445. __m256i p16_0 = lasx_maddubs_h(q5_0, q8_0);
  6446. __m256i p16_1 = lasx_maddubs_h(q5_1, q8_1);
  6447. p16_0 = lasx_madd_h(scale_0, p16_0);
  6448. p16_1 = lasx_madd_h(scale_1, p16_1);
  6449. sumi = __lasx_xvadd_w(sumi, __lasx_xvadd_w(p16_0, p16_1));
  6450. }
  6451. __m256 vd = __lasx_xvreplfr2vr_s(d);
  6452. acc = __lasx_xvfmadd_s(vd, __lasx_xvffint_s_w(sumi), acc);
  6453. }
  6454. *s = hsum_float_8(acc) + summs;
  6455. #else
  6456. const uint8_t * scales = (const uint8_t*)&utmp[0];
  6457. const uint8_t * mins = (const uint8_t*)&utmp[2];
  6458. int8_t aux8[QK_K];
  6459. int16_t aux16[8];
  6460. float sums [8];
  6461. int32_t aux32[8];
  6462. memset(sums, 0, 8*sizeof(float));
  6463. float sumf = 0;
  6464. for (int i = 0; i < nb; ++i) {
  6465. const uint8_t * restrict q4 = x[i].qs;
  6466. const uint8_t * restrict hm = x[i].qh;
  6467. const int8_t * restrict q8 = y[i].qs;
  6468. memset(aux32, 0, 8*sizeof(int32_t));
  6469. int8_t * restrict a = aux8;
  6470. uint8_t m = 1;
  6471. for (int j = 0; j < QK_K/64; ++j) {
  6472. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
  6473. for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
  6474. a += 32; m <<= 1;
  6475. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] >> 4);
  6476. for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
  6477. a += 32; m <<= 1;
  6478. q4 += 32;
  6479. }
  6480. memcpy(utmp, x[i].scales, 12);
  6481. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  6482. const uint32_t uaux = utmp[1] & kmask1;
  6483. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  6484. utmp[2] = uaux;
  6485. utmp[0] &= kmask1;
  6486. int sumi = 0;
  6487. for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
  6488. a = aux8;
  6489. int is = 0;
  6490. for (int j = 0; j < QK_K/32; ++j) {
  6491. int32_t scale = scales[is++];
  6492. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6493. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6494. q8 += 8; a += 8;
  6495. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6496. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6497. q8 += 8; a += 8;
  6498. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6499. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6500. q8 += 8; a += 8;
  6501. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6502. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6503. q8 += 8; a += 8;
  6504. }
  6505. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6506. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  6507. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  6508. sumf -= dmin * sumi;
  6509. }
  6510. for (int l = 0; l < 8; ++l) sumf += sums[l];
  6511. *s = sumf;
  6512. #endif
  6513. }
  6514. void ggml_vec_dot_q6_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  6515. assert(n % QK_K == 0);
  6516. assert(nrc == 1);
  6517. UNUSED(nrc);
  6518. UNUSED(bx);
  6519. UNUSED(by);
  6520. UNUSED(bs);
  6521. const block_q6_K * restrict x = vx;
  6522. const block_q8_K * restrict y = vy;
  6523. const int nb = n / QK_K;
  6524. #ifdef __ARM_NEON
  6525. float sum = 0;
  6526. const uint8x16_t m4b = vdupq_n_u8(0xF);
  6527. const int32x4_t vzero = vdupq_n_s32(0);
  6528. //const int8x16_t m32s = vdupq_n_s8(32);
  6529. const uint8x16_t mone = vdupq_n_u8(3);
  6530. ggml_int8x16x4_t q6bytes;
  6531. ggml_uint8x16x4_t q6h;
  6532. for (int i = 0; i < nb; ++i) {
  6533. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  6534. const uint8_t * restrict q6 = x[i].ql;
  6535. const uint8_t * restrict qh = x[i].qh;
  6536. const int8_t * restrict q8 = y[i].qs;
  6537. const int8_t * restrict scale = x[i].scales;
  6538. const ggml_int16x8x2_t q8sums = ggml_vld1q_s16_x2(y[i].bsums);
  6539. const int8x16_t scales = vld1q_s8(scale);
  6540. const ggml_int16x8x2_t q6scales = {{vmovl_s8(vget_low_s8(scales)), vmovl_s8(vget_high_s8(scales))}};
  6541. const int32x4_t prod = vaddq_s32(vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[0]), vget_low_s16 (q6scales.val[0])),
  6542. vmull_s16(vget_high_s16(q8sums.val[0]), vget_high_s16(q6scales.val[0]))),
  6543. vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[1]), vget_low_s16 (q6scales.val[1])),
  6544. vmull_s16(vget_high_s16(q8sums.val[1]), vget_high_s16(q6scales.val[1]))));
  6545. int32_t isum_mins = vaddvq_s32(prod);
  6546. int32_t isum = 0;
  6547. for (int j = 0; j < QK_K/128; ++j) {
  6548. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh); qh += 32;
  6549. ggml_uint8x16x4_t q6bits = ggml_vld1q_u8_x4(q6); q6 += 64;
  6550. ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  6551. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
  6552. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
  6553. uint8x16_t shifted = vshrq_n_u8(qhbits.val[0], 2);
  6554. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6555. shifted = vshrq_n_u8(qhbits.val[1], 2);
  6556. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6557. //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0])), m32s);
  6558. //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1])), m32s);
  6559. //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2])), m32s);
  6560. //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3])), m32s);
  6561. q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0]));
  6562. q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1]));
  6563. q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2]));
  6564. q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3]));
  6565. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  6566. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  6567. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  6568. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  6569. scale += 4;
  6570. q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  6571. shifted = vshrq_n_u8(qhbits.val[0], 4);
  6572. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6573. shifted = vshrq_n_u8(qhbits.val[1], 4);
  6574. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6575. shifted = vshrq_n_u8(qhbits.val[0], 6);
  6576. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6577. shifted = vshrq_n_u8(qhbits.val[1], 6);
  6578. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6579. //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0])), m32s);
  6580. //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1])), m32s);
  6581. //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2])), m32s);
  6582. //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3])), m32s);
  6583. q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0]));
  6584. q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1]));
  6585. q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2]));
  6586. q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3]));
  6587. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  6588. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  6589. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  6590. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  6591. scale += 4;
  6592. }
  6593. //sum += isum * d_all * y[i].d;
  6594. sum += d_all * y[i].d * (isum - 32 * isum_mins);
  6595. }
  6596. *s = sum;
  6597. #elif defined __AVX2__
  6598. const __m256i m4 = _mm256_set1_epi8(0xF);
  6599. const __m256i m2 = _mm256_set1_epi8(3);
  6600. const __m256i m32s = _mm256_set1_epi8(32);
  6601. __m256 acc = _mm256_setzero_ps();
  6602. for (int i = 0; i < nb; ++i) {
  6603. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6604. const uint8_t * restrict q4 = x[i].ql;
  6605. const uint8_t * restrict qh = x[i].qh;
  6606. const int8_t * restrict q8 = y[i].qs;
  6607. const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  6608. __m256i sumi = _mm256_setzero_si256();
  6609. int is = 0;
  6610. for (int j = 0; j < QK_K/128; ++j) {
  6611. const __m128i scale_0 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 0));
  6612. const __m128i scale_1 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 1));
  6613. const __m128i scale_2 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 2));
  6614. const __m128i scale_3 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 3));
  6615. is += 4;
  6616. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  6617. const __m256i q4bits2 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  6618. const __m256i q4bitsH = _mm256_loadu_si256((const __m256i*)qh); qh += 32;
  6619. const __m256i q4h_0 = _mm256_slli_epi16(_mm256_and_si256(q4bitsH, m2), 4);
  6620. const __m256i q4h_1 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 2), m2), 4);
  6621. const __m256i q4h_2 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 4), m2), 4);
  6622. const __m256i q4h_3 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 6), m2), 4);
  6623. const __m256i q4_0 = _mm256_or_si256(_mm256_and_si256(q4bits1, m4), q4h_0);
  6624. const __m256i q4_1 = _mm256_or_si256(_mm256_and_si256(q4bits2, m4), q4h_1);
  6625. const __m256i q4_2 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits1, 4), m4), q4h_2);
  6626. const __m256i q4_3 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits2, 4), m4), q4h_3);
  6627. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6628. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6629. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6630. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6631. __m256i q8s_0 = _mm256_maddubs_epi16(m32s, q8_0);
  6632. __m256i q8s_1 = _mm256_maddubs_epi16(m32s, q8_1);
  6633. __m256i q8s_2 = _mm256_maddubs_epi16(m32s, q8_2);
  6634. __m256i q8s_3 = _mm256_maddubs_epi16(m32s, q8_3);
  6635. __m256i p16_0 = _mm256_maddubs_epi16(q4_0, q8_0);
  6636. __m256i p16_1 = _mm256_maddubs_epi16(q4_1, q8_1);
  6637. __m256i p16_2 = _mm256_maddubs_epi16(q4_2, q8_2);
  6638. __m256i p16_3 = _mm256_maddubs_epi16(q4_3, q8_3);
  6639. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  6640. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  6641. p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
  6642. p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
  6643. p16_0 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_0), p16_0);
  6644. p16_1 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_1), p16_1);
  6645. p16_2 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_2), p16_2);
  6646. p16_3 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_3), p16_3);
  6647. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  6648. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_2, p16_3));
  6649. }
  6650. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  6651. }
  6652. *s = hsum_float_8(acc);
  6653. #elif defined __AVX__
  6654. const __m128i m4 = _mm_set1_epi8(0xF);
  6655. const __m128i m3 = _mm_set1_epi8(3);
  6656. const __m128i m32s = _mm_set1_epi8(32);
  6657. const __m128i m2 = _mm_set1_epi8(2);
  6658. __m256 acc = _mm256_setzero_ps();
  6659. for (int i = 0; i < nb; ++i) {
  6660. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6661. const uint8_t * restrict q4 = x[i].ql;
  6662. const uint8_t * restrict qh = x[i].qh;
  6663. const int8_t * restrict q8 = y[i].qs;
  6664. const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  6665. __m128i sumi_0 = _mm_setzero_si128();
  6666. __m128i sumi_1 = _mm_setzero_si128();
  6667. __m128i shuffle = _mm_set_epi64x(0x0101010101010101, 0x0000000000000000);
  6668. for (int j = 0; j < QK_K/128; ++j) {
  6669. const __m128i q4bitsH_0 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
  6670. const __m128i q4bitsH_1 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
  6671. const __m128i q4h_0 = _mm_slli_epi16(_mm_and_si128(q4bitsH_0, m3), 4);
  6672. const __m128i q4h_1 = _mm_slli_epi16(_mm_and_si128(q4bitsH_1, m3), 4);
  6673. const __m128i q4h_2 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 2), m3), 4);
  6674. const __m128i q4h_3 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 2), m3), 4);
  6675. const __m128i q4h_4 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 4), m3), 4);
  6676. const __m128i q4h_5 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 4), m3), 4);
  6677. const __m128i q4h_6 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 6), m3), 4);
  6678. const __m128i q4h_7 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 6), m3), 4);
  6679. const __m128i q4bits1_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6680. const __m128i q4bits1_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6681. const __m128i q4bits2_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6682. const __m128i q4bits2_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6683. const __m128i q4_0 = _mm_or_si128(_mm_and_si128(q4bits1_0, m4), q4h_0);
  6684. const __m128i q4_1 = _mm_or_si128(_mm_and_si128(q4bits1_1, m4), q4h_1);
  6685. const __m128i q4_2 = _mm_or_si128(_mm_and_si128(q4bits2_0, m4), q4h_2);
  6686. const __m128i q4_3 = _mm_or_si128(_mm_and_si128(q4bits2_1, m4), q4h_3);
  6687. const __m128i q4_4 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_0, 4), m4), q4h_4);
  6688. const __m128i q4_5 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_1, 4), m4), q4h_5);
  6689. const __m128i q4_6 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_0, 4), m4), q4h_6);
  6690. const __m128i q4_7 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_1, 4), m4), q4h_7);
  6691. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6692. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6693. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6694. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6695. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6696. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6697. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6698. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6699. __m128i q8s_0 = _mm_maddubs_epi16(m32s, q8_0);
  6700. __m128i q8s_1 = _mm_maddubs_epi16(m32s, q8_1);
  6701. __m128i q8s_2 = _mm_maddubs_epi16(m32s, q8_2);
  6702. __m128i q8s_3 = _mm_maddubs_epi16(m32s, q8_3);
  6703. __m128i q8s_4 = _mm_maddubs_epi16(m32s, q8_4);
  6704. __m128i q8s_5 = _mm_maddubs_epi16(m32s, q8_5);
  6705. __m128i q8s_6 = _mm_maddubs_epi16(m32s, q8_6);
  6706. __m128i q8s_7 = _mm_maddubs_epi16(m32s, q8_7);
  6707. __m128i p16_0 = _mm_maddubs_epi16(q4_0, q8_0);
  6708. __m128i p16_1 = _mm_maddubs_epi16(q4_1, q8_1);
  6709. __m128i p16_2 = _mm_maddubs_epi16(q4_2, q8_2);
  6710. __m128i p16_3 = _mm_maddubs_epi16(q4_3, q8_3);
  6711. __m128i p16_4 = _mm_maddubs_epi16(q4_4, q8_4);
  6712. __m128i p16_5 = _mm_maddubs_epi16(q4_5, q8_5);
  6713. __m128i p16_6 = _mm_maddubs_epi16(q4_6, q8_6);
  6714. __m128i p16_7 = _mm_maddubs_epi16(q4_7, q8_7);
  6715. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  6716. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  6717. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  6718. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  6719. p16_4 = _mm_sub_epi16(p16_4, q8s_4);
  6720. p16_5 = _mm_sub_epi16(p16_5, q8s_5);
  6721. p16_6 = _mm_sub_epi16(p16_6, q8s_6);
  6722. p16_7 = _mm_sub_epi16(p16_7, q8s_7);
  6723. const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
  6724. shuffle = _mm_add_epi8(shuffle, m2);
  6725. const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
  6726. shuffle = _mm_add_epi8(shuffle, m2);
  6727. const __m128i scale_2 = _mm_shuffle_epi8(scales, shuffle);
  6728. shuffle = _mm_add_epi8(shuffle, m2);
  6729. const __m128i scale_3 = _mm_shuffle_epi8(scales, shuffle);
  6730. shuffle = _mm_add_epi8(shuffle, m2);
  6731. p16_0 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_0), p16_0);
  6732. p16_1 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_0, scale_0)), p16_1);
  6733. p16_2 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_1), p16_2);
  6734. p16_3 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_1, scale_1)), p16_3);
  6735. p16_4 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_2), p16_4);
  6736. p16_5 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_2, scale_2)), p16_5);
  6737. p16_6 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_3), p16_6);
  6738. p16_7 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_3, scale_3)), p16_7);
  6739. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  6740. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  6741. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_4, p16_6));
  6742. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_5, p16_7));
  6743. }
  6744. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  6745. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
  6746. }
  6747. *s = hsum_float_8(acc);
  6748. #elif defined __riscv_v_intrinsic
  6749. float sumf = 0;
  6750. for (int i = 0; i < nb; ++i) {
  6751. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6752. const uint8_t * restrict q6 = x[i].ql;
  6753. const uint8_t * restrict qh = x[i].qh;
  6754. const int8_t * restrict q8 = y[i].qs;
  6755. const int8_t * restrict scale = x[i].scales;
  6756. size_t vl;
  6757. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  6758. int sum_t = 0;
  6759. int is = 0;
  6760. for (int j = 0; j < QK_K/128; ++j) {
  6761. vl = 32;
  6762. // load qh
  6763. vuint8m1_t qh_x = __riscv_vle8_v_u8m1(qh, vl);
  6764. // load Q6
  6765. vuint8m1_t q6_0 = __riscv_vle8_v_u8m1(q6, vl);
  6766. vuint8m1_t q6_1 = __riscv_vle8_v_u8m1(q6+32, vl);
  6767. vuint8m1_t q6a_0 = __riscv_vand_vx_u8m1(q6_0, 0x0F, vl);
  6768. vuint8m1_t q6a_1 = __riscv_vand_vx_u8m1(q6_1, 0x0F, vl);
  6769. vuint8m1_t q6s_0 = __riscv_vsrl_vx_u8m1(q6_0, 0x04, vl);
  6770. vuint8m1_t q6s_1 = __riscv_vsrl_vx_u8m1(q6_1, 0x04, vl);
  6771. vuint8m1_t qh_0 = __riscv_vand_vx_u8m1(qh_x, 0x03, vl);
  6772. vuint8m1_t qh_1 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x2, vl), 0x03 , vl);
  6773. vuint8m1_t qh_2 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x4, vl), 0x03 , vl);
  6774. vuint8m1_t qh_3 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x6, vl), 0x03 , vl);
  6775. vuint8m1_t qhi_0 = __riscv_vor_vv_u8m1(q6a_0, __riscv_vsll_vx_u8m1(qh_0, 0x04, vl), vl);
  6776. vuint8m1_t qhi_1 = __riscv_vor_vv_u8m1(q6a_1, __riscv_vsll_vx_u8m1(qh_1, 0x04, vl), vl);
  6777. vuint8m1_t qhi_2 = __riscv_vor_vv_u8m1(q6s_0, __riscv_vsll_vx_u8m1(qh_2, 0x04, vl), vl);
  6778. vuint8m1_t qhi_3 = __riscv_vor_vv_u8m1(q6s_1, __riscv_vsll_vx_u8m1(qh_3, 0x04, vl), vl);
  6779. vint8m1_t a_0 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_0), 32, vl);
  6780. vint8m1_t a_1 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_1), 32, vl);
  6781. vint8m1_t a_2 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_2), 32, vl);
  6782. vint8m1_t a_3 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_3), 32, vl);
  6783. // load Q8 and take product
  6784. vint16m2_t va_q_0 = __riscv_vwmul_vv_i16m2(a_0, __riscv_vle8_v_i8m1(q8, vl), vl);
  6785. vint16m2_t va_q_1 = __riscv_vwmul_vv_i16m2(a_1, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  6786. vint16m2_t va_q_2 = __riscv_vwmul_vv_i16m2(a_2, __riscv_vle8_v_i8m1(q8+64, vl), vl);
  6787. vint16m2_t va_q_3 = __riscv_vwmul_vv_i16m2(a_3, __riscv_vle8_v_i8m1(q8+96, vl), vl);
  6788. vl = 16;
  6789. vint32m2_t vaux_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_0, 0), scale[is+0], vl);
  6790. vint32m2_t vaux_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_0, 1), scale[is+1], vl);
  6791. vint32m2_t vaux_2 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_1, 0), scale[is+2], vl);
  6792. vint32m2_t vaux_3 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_1, 1), scale[is+3], vl);
  6793. vint32m2_t vaux_4 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_2, 0), scale[is+4], vl);
  6794. vint32m2_t vaux_5 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_2, 1), scale[is+5], vl);
  6795. vint32m2_t vaux_6 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_3, 0), scale[is+6], vl);
  6796. vint32m2_t vaux_7 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_3, 1), scale[is+7], vl);
  6797. vint32m1_t isum0 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_0, vaux_1, vl), vzero, vl);
  6798. vint32m1_t isum1 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_2, vaux_3, vl), isum0, vl);
  6799. vint32m1_t isum2 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_4, vaux_5, vl), isum1, vl);
  6800. vint32m1_t isum3 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_6, vaux_7, vl), isum2, vl);
  6801. sum_t += __riscv_vmv_x_s_i32m1_i32(isum3);
  6802. q6 += 64; qh += 32; q8 += 128; is=8;
  6803. }
  6804. sumf += d * sum_t;
  6805. }
  6806. *s = sumf;
  6807. #elif defined(__POWER9_VECTOR__)
  6808. const vector signed char lowMask = vec_splats((signed char)0xF);
  6809. const vector int v0 = vec_splats((int32_t)0);
  6810. const vector unsigned char v2 = vec_splats((unsigned char)0x2);
  6811. const vector unsigned char v3 = vec_splats((unsigned char)0x3);
  6812. const vector unsigned char v4 = vec_splats((unsigned char)0x4);
  6813. const vector unsigned char v6 = vec_splats((unsigned char)0x6);
  6814. const vector signed char off = vec_splats((signed char)0x20);
  6815. vector float vsumf0 = vec_splats(0.0f);
  6816. vector float vsumf1 = vec_splats(0.0f);
  6817. vector float vsumf2 = vec_splats(0.0f);
  6818. vector float vsumf3 = vec_splats(0.0f);
  6819. for (int i = 0; i < nb; ++i) {
  6820. vector float vxd = vec_splats(GGML_FP16_TO_FP32(x[i].d));
  6821. vector float vyd = vec_splats(y[i].d);
  6822. vector float vd = vec_mul(vxd, vyd);
  6823. vector signed int vsumi0 = v0;
  6824. vector signed int vsumi1 = v0;
  6825. vector signed int vsumi2 = v0;
  6826. vector signed int vsumi3 = v0;
  6827. vector signed int vsumi4 = v0;
  6828. vector signed int vsumi5 = v0;
  6829. vector signed int vsumi6 = v0;
  6830. vector signed int vsumi7 = v0;
  6831. const uint8_t * restrict q6 = x[i].ql;
  6832. const uint8_t * restrict qh = x[i].qh;
  6833. const int8_t * restrict qs = x[i].scales;
  6834. const int8_t * restrict q8 = y[i].qs;
  6835. for (int j = 0; j < QK_K/128; ++j) {
  6836. __builtin_prefetch(q6, 0, 0);
  6837. __builtin_prefetch(qh, 0, 0);
  6838. __builtin_prefetch(q8, 0, 0);
  6839. vector signed char qxs0 = (vector signed char)vec_xl( 0, q6);
  6840. vector signed char qxs1 = (vector signed char)vec_xl(16, q6);
  6841. vector signed char qxs2 = (vector signed char)vec_xl(32, q6);
  6842. vector signed char qxs3 = (vector signed char)vec_xl(48, q6);
  6843. q6 += 64;
  6844. vector signed char qxs00 = vec_and(qxs0, lowMask);
  6845. vector signed char qxs01 = vec_sr(qxs0, v4);
  6846. vector signed char qxs10 = vec_and(qxs1, lowMask);
  6847. vector signed char qxs11 = vec_sr(qxs1, v4);
  6848. vector signed char qxs20 = vec_and(qxs2, lowMask);
  6849. vector signed char qxs21 = vec_sr(qxs2, v4);
  6850. vector signed char qxs30 = vec_and(qxs3, lowMask);
  6851. vector signed char qxs31 = vec_sr(qxs3, v4);
  6852. vector signed char qxhs0 = (vector signed char)vec_xl( 0, qh);
  6853. vector signed char qxhs1 = (vector signed char)vec_xl(16, qh);
  6854. qh += 32;
  6855. vector signed char qxh00 = vec_sl(vec_and((vector signed char)v3, qxhs0), v4);
  6856. vector signed char qxh01 = vec_sl(vec_and((vector signed char)v3, vec_sr(qxhs0, v4)), v4);
  6857. vector signed char qxh10 = vec_sl(vec_and((vector signed char)v3, qxhs1), v4);
  6858. vector signed char qxh11 = vec_sl(vec_and((vector signed char)v3, vec_sr(qxhs1, v4)), v4);
  6859. vector signed char qxh20 = vec_sl(vec_and((vector signed char)v3, vec_sr(qxhs0, v2)), v4);
  6860. vector signed char qxh21 = vec_sl(vec_and((vector signed char)v3, vec_sr(qxhs0, v6)), v4);
  6861. vector signed char qxh30 = vec_sl(vec_and((vector signed char)v3, vec_sr(qxhs1, v2)), v4);
  6862. vector signed char qxh31 = vec_sl(vec_and((vector signed char)v3, vec_sr(qxhs1, v6)), v4);
  6863. vector signed char q6x00 = vec_sub(vec_or(qxh00, qxs00), off);
  6864. vector signed char q6x01 = vec_sub(vec_or(qxh01, qxs01), off);
  6865. vector signed char q6x10 = vec_sub(vec_or(qxh10, qxs10), off);
  6866. vector signed char q6x11 = vec_sub(vec_or(qxh11, qxs11), off);
  6867. vector signed char q6x20 = vec_sub(vec_or(qxh20, qxs20), off);
  6868. vector signed char q6x21 = vec_sub(vec_or(qxh21, qxs21), off);
  6869. vector signed char q6x30 = vec_sub(vec_or(qxh30, qxs30), off);
  6870. vector signed char q6x31 = vec_sub(vec_or(qxh31, qxs31), off);
  6871. vector signed char q8y00 = vec_xl( 0, q8);
  6872. vector signed char q8y10 = vec_xl( 16, q8);
  6873. vector signed char q8y20 = vec_xl( 32, q8);
  6874. vector signed char q8y30 = vec_xl( 48, q8);
  6875. vector signed char q8y01 = vec_xl( 64, q8);
  6876. vector signed char q8y11 = vec_xl( 80, q8);
  6877. vector signed char q8y21 = vec_xl( 96, q8);
  6878. vector signed char q8y31 = vec_xl(112, q8);
  6879. q8 += 128;
  6880. vector signed short qv00 = vec_add(vec_mule(q6x00, q8y00), vec_mulo(q6x00, q8y00));
  6881. vector signed short qv10 = vec_add(vec_mule(q6x10, q8y10), vec_mulo(q6x10, q8y10));
  6882. vector signed short qv20 = vec_add(vec_mule(q6x20, q8y20), vec_mulo(q6x20, q8y20));
  6883. vector signed short qv30 = vec_add(vec_mule(q6x30, q8y30), vec_mulo(q6x30, q8y30));
  6884. vector signed short qv01 = vec_add(vec_mule(q6x01, q8y01), vec_mulo(q6x01, q8y01));
  6885. vector signed short qv11 = vec_add(vec_mule(q6x11, q8y11), vec_mulo(q6x11, q8y11));
  6886. vector signed short qv21 = vec_add(vec_mule(q6x21, q8y21), vec_mulo(q6x21, q8y21));
  6887. vector signed short qv31 = vec_add(vec_mule(q6x31, q8y31), vec_mulo(q6x31, q8y31));
  6888. vector signed short vscales = vec_unpackh(vec_xl_len(qs, 8));
  6889. qs += 8;
  6890. vector signed short vs0 = vec_splat(vscales, 0);
  6891. vector signed short vs1 = vec_splat(vscales, 1);
  6892. vector signed short vs2 = vec_splat(vscales, 2);
  6893. vector signed short vs3 = vec_splat(vscales, 3);
  6894. vector signed short vs4 = vec_splat(vscales, 4);
  6895. vector signed short vs5 = vec_splat(vscales, 5);
  6896. vector signed short vs6 = vec_splat(vscales, 6);
  6897. vector signed short vs7 = vec_splat(vscales, 7);
  6898. vsumi0 = vec_msum(qv00, vs0, vsumi0);
  6899. vsumi1 = vec_msum(qv01, vs4, vsumi1);
  6900. vsumi2 = vec_msum(qv10, vs1, vsumi2);
  6901. vsumi3 = vec_msum(qv11, vs5, vsumi3);
  6902. vsumi4 = vec_msum(qv20, vs2, vsumi4);
  6903. vsumi5 = vec_msum(qv21, vs6, vsumi5);
  6904. vsumi6 = vec_msum(qv30, vs3, vsumi6);
  6905. vsumi7 = vec_msum(qv31, vs7, vsumi7);
  6906. }
  6907. vsumi0 = vec_add(vsumi0, vsumi4);
  6908. vsumi1 = vec_add(vsumi1, vsumi5);
  6909. vsumi2 = vec_add(vsumi2, vsumi6);
  6910. vsumi3 = vec_add(vsumi3, vsumi7);
  6911. vsumf0 = vec_madd(vec_ctf(vsumi0, 0), vd, vsumf0);
  6912. vsumf1 = vec_madd(vec_ctf(vsumi1, 0), vd, vsumf1);
  6913. vsumf2 = vec_madd(vec_ctf(vsumi2, 0), vd, vsumf2);
  6914. vsumf3 = vec_madd(vec_ctf(vsumi3, 0), vd, vsumf3);
  6915. }
  6916. vsumf0 = vec_add(vsumf0, vsumf2);
  6917. vsumf1 = vec_add(vsumf1, vsumf3);
  6918. vsumf0 = vec_add(vsumf0, vsumf1);
  6919. vsumf0 = vec_add(vsumf0, vec_sld(vsumf0, vsumf0, 4));
  6920. vsumf0 = vec_add(vsumf0, vec_sld(vsumf0, vsumf0, 8));
  6921. *s = vec_extract(vsumf0, 0);
  6922. #elif defined __loongarch_asx
  6923. const __m256i m4 = __lasx_xvreplgr2vr_b(0xF);
  6924. const __m256i m2 = __lasx_xvreplgr2vr_b(3);
  6925. const __m256i m32s = __lasx_xvreplgr2vr_b(32);
  6926. __m256 acc = (__m256)__lasx_xvldi(0);
  6927. for (int i = 0; i < nb; ++i) {
  6928. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6929. const uint8_t * restrict q4 = x[i].ql;
  6930. const uint8_t * restrict qh = x[i].qh;
  6931. const int8_t * restrict q8 = y[i].qs;
  6932. const __m128i scales = __lsx_vld((const __m128i*)x[i].scales, 0);
  6933. __m256i sumi = __lasx_xvldi(0);
  6934. int is = 0;
  6935. for (int j = 0; j < QK_K/128; ++j) {
  6936. const __m128i scale_0 = lsx_shuffle_b(scales, get_scale_shuffle(is + 0));
  6937. const __m128i scale_1 = lsx_shuffle_b(scales, get_scale_shuffle(is + 1));
  6938. const __m128i scale_2 = lsx_shuffle_b(scales, get_scale_shuffle(is + 2));
  6939. const __m128i scale_3 = lsx_shuffle_b(scales, get_scale_shuffle(is + 3));
  6940. is += 4;
  6941. const __m256i q4bits1 = __lasx_xvld((const __m256i*)q4, 0); q4 += 32;
  6942. const __m256i q4bits2 = __lasx_xvld((const __m256i*)q4, 0); q4 += 32;
  6943. const __m256i q4bitsH = __lasx_xvld((const __m256i*)qh, 0); qh += 32;
  6944. const __m256i q4h_0 = __lasx_xvslli_h(__lasx_xvand_v(q4bitsH, m2), 4);
  6945. const __m256i q4h_1 = __lasx_xvslli_h(__lasx_xvand_v(__lasx_xvsrli_h(q4bitsH, 2), m2), 4);
  6946. const __m256i q4h_2 = __lasx_xvslli_h(__lasx_xvand_v(__lasx_xvsrli_h(q4bitsH, 4), m2), 4);
  6947. const __m256i q4h_3 = __lasx_xvslli_h(__lasx_xvand_v(__lasx_xvsrli_h(q4bitsH, 6), m2), 4);
  6948. const __m256i q4_0 = __lasx_xvor_v(__lasx_xvand_v(q4bits1, m4), q4h_0);
  6949. const __m256i q4_1 = __lasx_xvor_v(__lasx_xvand_v(q4bits2, m4), q4h_1);
  6950. const __m256i q4_2 = __lasx_xvor_v(__lasx_xvand_v(__lasx_xvsrli_h(q4bits1, 4), m4), q4h_2);
  6951. const __m256i q4_3 = __lasx_xvor_v(__lasx_xvand_v(__lasx_xvsrli_h(q4bits2, 4), m4), q4h_3);
  6952. const __m256i q8_0 = __lasx_xvld((const __m256i*)q8, 0); q8 += 32;
  6953. const __m256i q8_1 = __lasx_xvld((const __m256i*)q8, 0); q8 += 32;
  6954. const __m256i q8_2 = __lasx_xvld((const __m256i*)q8, 0); q8 += 32;
  6955. const __m256i q8_3 = __lasx_xvld((const __m256i*)q8, 0); q8 += 32;
  6956. __m256i q8s_0 = lasx_maddubs_h(m32s, q8_0);
  6957. __m256i q8s_1 = lasx_maddubs_h(m32s, q8_1);
  6958. __m256i q8s_2 = lasx_maddubs_h(m32s, q8_2);
  6959. __m256i q8s_3 = lasx_maddubs_h(m32s, q8_3);
  6960. __m256i p16_0 = lasx_maddubs_h(q4_0, q8_0);
  6961. __m256i p16_1 = lasx_maddubs_h(q4_1, q8_1);
  6962. __m256i p16_2 = lasx_maddubs_h(q4_2, q8_2);
  6963. __m256i p16_3 = lasx_maddubs_h(q4_3, q8_3);
  6964. p16_0 = __lasx_xvsub_h(p16_0, q8s_0);
  6965. p16_1 = __lasx_xvsub_h(p16_1, q8s_1);
  6966. p16_2 = __lasx_xvsub_h(p16_2, q8s_2);
  6967. p16_3 = __lasx_xvsub_h(p16_3, q8s_3);
  6968. p16_0 = lasx_madd_h(lasx_ext8_16(scale_0), p16_0);
  6969. p16_1 = lasx_madd_h(lasx_ext8_16(scale_1), p16_1);
  6970. p16_2 = lasx_madd_h(lasx_ext8_16(scale_2), p16_2);
  6971. p16_3 = lasx_madd_h(lasx_ext8_16(scale_3), p16_3);
  6972. sumi = __lasx_xvadd_w(sumi, __lasx_xvadd_w(p16_0, p16_1));
  6973. sumi = __lasx_xvadd_w(sumi, __lasx_xvadd_w(p16_2, p16_3));
  6974. }
  6975. acc = __lasx_xvfmadd_s((__m256)__lasx_xvreplfr2vr_s(d), __lasx_xvffint_s_w(sumi), acc);
  6976. }
  6977. *s = hsum_float_8(acc);
  6978. #else
  6979. int8_t aux8[QK_K];
  6980. int16_t aux16[8];
  6981. float sums [8];
  6982. int32_t aux32[8];
  6983. memset(sums, 0, 8*sizeof(float));
  6984. float sumf = 0;
  6985. for (int i = 0; i < nb; ++i) {
  6986. const uint8_t * restrict q4 = x[i].ql;
  6987. const uint8_t * restrict qh = x[i].qh;
  6988. const int8_t * restrict q8 = y[i].qs;
  6989. memset(aux32, 0, 8*sizeof(int32_t));
  6990. int8_t * restrict a = aux8;
  6991. for (int j = 0; j < QK_K; j += 128) {
  6992. for (int l = 0; l < 32; ++l) {
  6993. a[l + 0] = (int8_t)((q4[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  6994. a[l + 32] = (int8_t)((q4[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  6995. a[l + 64] = (int8_t)((q4[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  6996. a[l + 96] = (int8_t)((q4[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  6997. }
  6998. a += 128;
  6999. q4 += 64;
  7000. qh += 32;
  7001. }
  7002. a = aux8;
  7003. int is = 0;
  7004. for (int j = 0; j < QK_K/16; ++j) {
  7005. int scale = x[i].scales[is++];
  7006. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  7007. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  7008. q8 += 8; a += 8;
  7009. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  7010. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  7011. q8 += 8; a += 8;
  7012. }
  7013. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7014. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  7015. }
  7016. for (int l = 0; l < 8; ++l) sumf += sums[l];
  7017. *s = sumf;
  7018. #endif
  7019. }
  7020. #if defined (__AVX2__) || defined (__ARM_NEON) || defined (__POWER9_VECTOR__) || defined(__loongarch_asx)
  7021. static const int8_t keven_signs_q2xs[1024] = {
  7022. 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1,
  7023. 1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, -1,
  7024. 1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, -1,
  7025. 1, 1, -1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, 1,
  7026. 1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, -1,
  7027. 1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, 1,
  7028. 1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, 1,
  7029. 1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, -1,
  7030. 1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, 1, -1,
  7031. 1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, 1,
  7032. 1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, 1,
  7033. 1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, -1,
  7034. 1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, 1,
  7035. 1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, -1,
  7036. 1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, -1,
  7037. 1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, 1,
  7038. 1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, -1,
  7039. 1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, 1,
  7040. 1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1,
  7041. 1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, -1,
  7042. 1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, 1,
  7043. 1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, -1,
  7044. 1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, -1,
  7045. 1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 1,
  7046. 1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, 1,
  7047. 1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, -1,
  7048. 1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, -1,
  7049. 1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 1,
  7050. 1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, -1,
  7051. 1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, 1,
  7052. 1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, 1,
  7053. 1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
  7054. };
  7055. #endif
  7056. void ggml_vec_dot_iq2_xxs_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  7057. assert(n % QK_K == 0);
  7058. assert(nrc == 1);
  7059. UNUSED(nrc);
  7060. UNUSED(bx);
  7061. UNUSED(by);
  7062. UNUSED(bs);
  7063. const block_iq2_xxs * restrict x = vx;
  7064. const block_q8_K * restrict y = vy;
  7065. const int nb = n / QK_K;
  7066. #if defined(__ARM_NEON)
  7067. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  7068. uint32_t aux32[4];
  7069. const uint8_t * aux8 = (const uint8_t *)aux32;
  7070. ggml_int8x16x4_t q2u;
  7071. ggml_int8x16x4_t q2s;
  7072. ggml_int8x16x4_t q8b;
  7073. float sumf = 0;
  7074. for (int i = 0; i < nb; ++i) {
  7075. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7076. const uint16_t * restrict q2 = x[i].qs;
  7077. const int8_t * restrict q8 = y[i].qs;
  7078. float sumf1 = 0, sumf2 = 0;
  7079. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7080. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7081. memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8;
  7082. q2u.val[0] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 0])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 1])));
  7083. q2u.val[1] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 2])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 3])));
  7084. q2u.val[2] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 8])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 9])));
  7085. q2u.val[3] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[10])), vld1_s8((const void *)(iq2xxs_grid + aux8[11])));
  7086. q2s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 7) & 127))));
  7087. q2s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 21) & 127))));
  7088. q2s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[3] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[3] >> 7) & 127))));
  7089. q2s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[3] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[3] >> 21) & 127))));
  7090. q2u.val[0] = vmulq_s8(q2u.val[0], q2s.val[0]);
  7091. q2u.val[1] = vmulq_s8(q2u.val[1], q2s.val[1]);
  7092. q2u.val[2] = vmulq_s8(q2u.val[2], q2s.val[2]);
  7093. q2u.val[3] = vmulq_s8(q2u.val[3], q2s.val[3]);
  7094. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[0], q8b.val[0]), q2u.val[1], q8b.val[1]);
  7095. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[2], q8b.val[2]), q2u.val[3], q8b.val[3]);
  7096. sumf1 += vaddvq_s32(p1) * (0.5f + (aux32[1] >> 28));
  7097. sumf2 += vaddvq_s32(p2) * (0.5f + (aux32[3] >> 28));
  7098. }
  7099. sumf += d*(sumf1 + sumf2);
  7100. }
  7101. *s = 0.25f * sumf;
  7102. #elif defined(__AVX2__)
  7103. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  7104. uint32_t aux32[4];
  7105. const uint8_t * aux8 = (const uint8_t *)aux32;
  7106. __m256 accumf = _mm256_setzero_ps();
  7107. for (int i = 0; i < nb; ++i) {
  7108. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7109. const uint16_t * restrict q2 = x[i].qs;
  7110. const int8_t * restrict q8 = y[i].qs;
  7111. __m256i sumi1 = _mm256_setzero_si256();
  7112. __m256i sumi2 = _mm256_setzero_si256();
  7113. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7114. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7115. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7116. memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8;
  7117. const __m256i q2_1 = _mm256_set_epi64x(iq2xxs_grid[aux8[ 3]], iq2xxs_grid[aux8[ 2]], iq2xxs_grid[aux8[1]], iq2xxs_grid[aux8[0]]);
  7118. const __m256i q2_2 = _mm256_set_epi64x(iq2xxs_grid[aux8[11]], iq2xxs_grid[aux8[10]], iq2xxs_grid[aux8[9]], iq2xxs_grid[aux8[8]]);
  7119. const __m256i s2_1 = _mm256_set_epi64x(signs64[(aux32[1] >> 21) & 127], signs64[(aux32[1] >> 14) & 127],
  7120. signs64[(aux32[1] >> 7) & 127], signs64[(aux32[1] >> 0) & 127]);
  7121. const __m256i s2_2 = _mm256_set_epi64x(signs64[(aux32[3] >> 21) & 127], signs64[(aux32[3] >> 14) & 127],
  7122. signs64[(aux32[3] >> 7) & 127], signs64[(aux32[3] >> 0) & 127]);
  7123. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, s2_1);
  7124. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, s2_2);
  7125. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  7126. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  7127. const uint16_t ls1 = aux32[1] >> 28;
  7128. const uint16_t ls2 = aux32[3] >> 28;
  7129. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
  7130. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
  7131. sumi1 = _mm256_add_epi32(sumi1, p1);
  7132. sumi2 = _mm256_add_epi32(sumi2, p2);
  7133. }
  7134. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  7135. }
  7136. *s = 0.125f * hsum_float_8(accumf);
  7137. #elif defined(__POWER9_VECTOR__)
  7138. const vector int v0 = vec_splats((int32_t)0);
  7139. vector float vsumf0 = vec_splats(0.0f);
  7140. vector float vsumf1 = vec_splats(0.0f);
  7141. vector float vsumf2 = vec_splats(0.0f);
  7142. vector float vsumf3 = vec_splats(0.0f);
  7143. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  7144. for (int i = 0; i < nb; ++i) {
  7145. vector float vxd = vec_splats(GGML_FP16_TO_FP32(x[i].d));
  7146. vector float vyd = vec_splats(y[i].d);
  7147. vector float vd = vec_mul(vxd, vyd);
  7148. vector signed int vsumi0 = v0;
  7149. vector signed int vsumi1 = v0;
  7150. vector signed int vsumi2 = v0;
  7151. vector signed int vsumi3 = v0;
  7152. const uint16_t * restrict q2 = x[i].qs;
  7153. const int8_t * restrict q8 = y[i].qs;
  7154. for (int j = 0; j < QK_K/32; j += 2) {
  7155. __builtin_prefetch(q2, 0, 1);
  7156. __builtin_prefetch(q8, 0, 1);
  7157. uint32_t aux32[4];
  7158. const uint8_t * aux8 = (const uint8_t *)aux32;
  7159. memcpy(aux32, q2, 4*sizeof(uint32_t));
  7160. q2 += 8;
  7161. vector signed long long aux64x2_0 = {*(const int64_t *)(iq2xxs_grid + aux8[ 0]), *(const int64_t *)(iq2xxs_grid + aux8[ 1])};
  7162. vector signed long long aux64x2_1 = {*(const int64_t *)(iq2xxs_grid + aux8[ 2]), *(const int64_t *)(iq2xxs_grid + aux8[ 3])};
  7163. vector signed long long aux64x2_2 = {*(const int64_t *)(iq2xxs_grid + aux8[ 8]), *(const int64_t *)(iq2xxs_grid + aux8[ 9])};
  7164. vector signed long long aux64x2_3 = {*(const int64_t *)(iq2xxs_grid + aux8[10]), *(const int64_t *)(iq2xxs_grid + aux8[11])};
  7165. vector signed long long vsigns0 = {*(const int64_t *)(signs64 + ((aux32[1] >> 0) & 127)), *(const int64_t *)(signs64 + ((aux32[1] >> 7) & 127))};
  7166. vector signed long long vsigns1 = {*(const int64_t *)(signs64 + ((aux32[1] >> 14) & 127)), *(const int64_t *)(signs64 + ((aux32[1] >> 21) & 127))};
  7167. vector signed long long vsigns2 = {*(const int64_t *)(signs64 + ((aux32[3] >> 0) & 127)), *(const int64_t *)(signs64 + ((aux32[3] >> 7) & 127))};
  7168. vector signed long long vsigns3 = {*(const int64_t *)(signs64 + ((aux32[3] >> 14) & 127)), *(const int64_t *)(signs64 + ((aux32[3] >> 21) & 127))};
  7169. vector signed char q2x0 = (vector signed char)vec_mul((vector signed char)vsigns0, (vector signed char)aux64x2_0);
  7170. vector signed char q2x1 = (vector signed char)vec_mul((vector signed char)vsigns1, (vector signed char)aux64x2_1);
  7171. vector signed char q2x2 = (vector signed char)vec_mul((vector signed char)vsigns2, (vector signed char)aux64x2_2);
  7172. vector signed char q2x3 = (vector signed char)vec_mul((vector signed char)vsigns3, (vector signed char)aux64x2_3);
  7173. vector signed char q8y0 = vec_xl( 0, q8);
  7174. vector signed char q8y1 = vec_xl(16, q8);
  7175. vector signed char q8y2 = vec_xl(32, q8);
  7176. vector signed char q8y3 = vec_xl(48, q8);
  7177. q8 += 64;
  7178. vector signed short qv0 = vec_add(vec_mule(q2x0, q8y0), vec_mulo(q2x0, q8y0));
  7179. vector signed short qv1 = vec_add(vec_mule(q2x1, q8y1), vec_mulo(q2x1, q8y1));
  7180. vector signed short qv2 = vec_add(vec_mule(q2x2, q8y2), vec_mulo(q2x2, q8y2));
  7181. vector signed short qv3 = vec_add(vec_mule(q2x3, q8y3), vec_mulo(q2x3, q8y3));
  7182. const uint16_t ls0 = aux32[1] >> 28;
  7183. const uint16_t ls1 = aux32[3] >> 28;
  7184. vector signed short vscales01 = vec_splats((int16_t)(2*ls0+1));
  7185. vector signed short vscales23 = vec_splats((int16_t)(2*ls1+1));
  7186. vsumi0 = vec_msum(qv0, vscales01, vsumi0);
  7187. vsumi1 = vec_msum(qv1, vscales01, vsumi1);
  7188. vsumi2 = vec_msum(qv2, vscales23, vsumi2);
  7189. vsumi3 = vec_msum(qv3, vscales23, vsumi3);
  7190. }
  7191. vsumf0 = vec_madd(vec_ctf(vsumi0, 0), vd, vsumf0);
  7192. vsumf1 = vec_madd(vec_ctf(vsumi1, 0), vd, vsumf1);
  7193. vsumf2 = vec_madd(vec_ctf(vsumi2, 0), vd, vsumf2);
  7194. vsumf3 = vec_madd(vec_ctf(vsumi3, 0), vd, vsumf3);
  7195. }
  7196. vsumf0 = vec_add(vsumf0, vsumf2);
  7197. vsumf1 = vec_add(vsumf1, vsumf3);
  7198. vsumf0 = vec_add(vsumf0, vsumf1);
  7199. vsumf0 = vec_add(vsumf0, vec_sld(vsumf0, vsumf0, 4));
  7200. vsumf0 = vec_add(vsumf0, vec_sld(vsumf0, vsumf0, 8));
  7201. *s = 0.125f * vec_extract(vsumf0, 0);
  7202. #elif defined(__loongarch_asx)
  7203. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  7204. uint32_t aux32[4];
  7205. const uint8_t * aux8 = (const uint8_t *)aux32;
  7206. __m256 accumf = (__m256)__lasx_xvldi(0);
  7207. for (int i = 0; i < nb; ++i) {
  7208. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7209. const uint16_t * restrict q2 = x[i].qs;
  7210. const int8_t * restrict q8 = y[i].qs;
  7211. __m256i sumi1 = __lasx_xvldi(0);
  7212. __m256i sumi2 = __lasx_xvldi(0);
  7213. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7214. const __m256i q8_1 = __lasx_xvld((const __m256i *)q8, 0); q8 += 32;
  7215. const __m256i q8_2 = __lasx_xvld((const __m256i *)q8, 0); q8 += 32;
  7216. memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8;
  7217. const __m256i q2_1 = lasx_set_d(iq2xxs_grid[aux8[ 3]], iq2xxs_grid[aux8[ 2]], iq2xxs_grid[aux8[1]], iq2xxs_grid[aux8[0]]);
  7218. const __m256i q2_2 = lasx_set_d(iq2xxs_grid[aux8[11]], iq2xxs_grid[aux8[10]], iq2xxs_grid[aux8[9]], iq2xxs_grid[aux8[8]]);
  7219. const __m256i s2_1 = lasx_set_d(signs64[(aux32[1] >> 21) & 127], signs64[(aux32[1] >> 14) & 127],
  7220. signs64[(aux32[1] >> 7) & 127], signs64[(aux32[1] >> 0) & 127]);
  7221. const __m256i s2_2 = lasx_set_d(signs64[(aux32[3] >> 21) & 127], signs64[(aux32[3] >> 14) & 127],
  7222. signs64[(aux32[3] >> 7) & 127], signs64[(aux32[3] >> 0) & 127]);
  7223. const __m256i q8s_1 = __lasx_xvsigncov_b(s2_1, q8_1);
  7224. const __m256i q8s_2 = __lasx_xvsigncov_b(s2_2, q8_2);
  7225. const __m256i dot1 = lasx_maddubs_h(q2_1, q8s_1);
  7226. const __m256i dot2 = lasx_maddubs_h(q2_2, q8s_2);
  7227. const uint16_t ls1 = aux32[1] >> 28;
  7228. const uint16_t ls2 = aux32[3] >> 28;
  7229. const __m256i p1 = lasx_madd_h(dot1, __lasx_xvreplgr2vr_h(2*ls1+1));
  7230. const __m256i p2 = lasx_madd_h(dot2, __lasx_xvreplgr2vr_h(2*ls2+1));
  7231. sumi1 = __lasx_xvadd_w(sumi1, p1);
  7232. sumi2 = __lasx_xvadd_w(sumi2, p2);
  7233. }
  7234. accumf = __lasx_xvfmadd_s(__lasx_xvreplfr2vr_s(d), __lasx_xvffint_s_w(__lasx_xvadd_w(sumi1, sumi2)), accumf);
  7235. }
  7236. *s = 0.125f * hsum_float_8(accumf);
  7237. #else
  7238. uint32_t aux32[2];
  7239. const uint8_t * aux8 = (const uint8_t *)aux32;
  7240. float sumf = 0.f;
  7241. for (int i = 0; i < nb; ++i) {
  7242. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7243. const uint16_t * restrict q2 = x[i].qs;
  7244. const int8_t * restrict q8 = y[i].qs;
  7245. int32_t bsum = 0;
  7246. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  7247. memcpy(aux32, q2, 2*sizeof(uint32_t));
  7248. q2 += 4;
  7249. const uint32_t ls = 2*(aux32[1] >> 28) + 1;
  7250. int32_t sumi = 0;
  7251. for (int l = 0; l < 4; ++l) {
  7252. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  7253. const uint8_t signs = ksigns_iq2xs[(aux32[1] >> 7*l) & 127];
  7254. for (int j = 0; j < 8; ++j) {
  7255. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  7256. }
  7257. q8 += 8;
  7258. }
  7259. bsum += sumi * ls;
  7260. }
  7261. sumf += d * bsum;
  7262. }
  7263. *s = 0.125f * sumf;
  7264. #endif
  7265. }
  7266. void ggml_vec_dot_iq2_xs_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  7267. assert(n % QK_K == 0);
  7268. assert(nrc == 1);
  7269. UNUSED(nrc);
  7270. UNUSED(bx);
  7271. UNUSED(by);
  7272. UNUSED(bs);
  7273. const block_iq2_xs * restrict x = vx;
  7274. const block_q8_K * restrict y = vy;
  7275. const int nb = n / QK_K;
  7276. #if defined(__ARM_NEON)
  7277. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  7278. ggml_int8x16x4_t q2u;
  7279. ggml_int8x16x4_t q2s;
  7280. ggml_int8x16x4_t q8b;
  7281. int32x4x4_t scales32;
  7282. float sumf = 0;
  7283. for (int i = 0; i < nb; ++i) {
  7284. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7285. const uint16_t * restrict q2 = x[i].qs;
  7286. const int8_t * restrict q8 = y[i].qs;
  7287. const uint8x8_t scales8 = vld1_u8(x[i].scales);
  7288. const uint8x8_t scales_l = vand_u8(scales8, vdup_n_u8(0xf));
  7289. const uint8x8_t scales_h = vshr_n_u8(scales8, 4);
  7290. uint8x16_t scales = vcombine_u8(vzip1_u8(scales_l, scales_h), vzip2_u8(scales_l, scales_h));
  7291. scales = vaddq_u8(vshlq_n_u8(scales, 1), vdupq_n_u8(1));
  7292. const uint16x8_t scales1 = vmovl_u8(vget_low_u8(scales));
  7293. const uint16x8_t scales2 = vmovl_u8(vget_high_u8(scales));
  7294. scales32.val[0] = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(scales1)));
  7295. scales32.val[1] = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(scales1)));
  7296. scales32.val[2] = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(scales2)));
  7297. scales32.val[3] = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(scales2)));
  7298. int32x4_t sumi = vdupq_n_s32(0);
  7299. for (int ib64 = 0; ib64 < QK_K/64; ++ib64) {
  7300. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7301. q2u.val[0] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[0] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[1] & 511))));
  7302. q2u.val[1] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[2] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[3] & 511))));
  7303. q2u.val[2] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[4] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[5] & 511))));
  7304. q2u.val[3] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[6] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[7] & 511))));
  7305. q2s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[0] >> 9))), vld1_s8((const void *)(signs64 + (q2[1] >> 9))));
  7306. q2s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[2] >> 9))), vld1_s8((const void *)(signs64 + (q2[3] >> 9))));
  7307. q2s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[4] >> 9))), vld1_s8((const void *)(signs64 + (q2[5] >> 9))));
  7308. q2s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[6] >> 9))), vld1_s8((const void *)(signs64 + (q2[7] >> 9))));
  7309. q2u.val[0] = vmulq_s8(q2u.val[0], q2s.val[0]);
  7310. q2u.val[1] = vmulq_s8(q2u.val[1], q2s.val[1]);
  7311. q2u.val[2] = vmulq_s8(q2u.val[2], q2s.val[2]);
  7312. q2u.val[3] = vmulq_s8(q2u.val[3], q2s.val[3]);
  7313. const int32x4_t p1 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[0], q8b.val[0]);
  7314. const int32x4_t p2 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[1], q8b.val[1]);
  7315. const int32x4_t p3 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[2], q8b.val[2]);
  7316. const int32x4_t p4 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[3], q8b.val[3]);
  7317. const int32x4_t p = vpaddq_s32(vpaddq_s32(p1, p2), vpaddq_s32(p3, p4));
  7318. sumi = vmlaq_s32(sumi, p, scales32.val[ib64]);
  7319. q2 += 8;
  7320. }
  7321. sumf += d*vaddvq_s32(sumi);
  7322. }
  7323. *s = 0.125f * sumf;
  7324. #elif defined(__AVX2__)
  7325. const __m256i mone = _mm256_set1_epi8(1);
  7326. static const char block_sign_shuffle_mask_1[32] = {
  7327. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
  7328. 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  7329. };
  7330. static const char block_sign_shuffle_mask_2[32] = {
  7331. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
  7332. 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e,
  7333. };
  7334. static const uint8_t bit_selector_mask_bytes[32] = {
  7335. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  7336. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  7337. };
  7338. const __m256i bit_selector_mask = _mm256_loadu_si256((const __m256i*)bit_selector_mask_bytes);
  7339. const __m256i block_sign_shuffle_1 = _mm256_loadu_si256((const __m256i*)block_sign_shuffle_mask_1);
  7340. const __m256i block_sign_shuffle_2 = _mm256_loadu_si256((const __m256i*)block_sign_shuffle_mask_2);
  7341. static const uint8_t k_bit_helper[32] = {
  7342. 0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00,
  7343. 0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00,
  7344. };
  7345. const __m256i bit_helper = _mm256_loadu_si256((const __m256i*)k_bit_helper);
  7346. const __m256i m511 = _mm256_set1_epi16(511);
  7347. const __m128i m4 = _mm_set1_epi8(0xf);
  7348. const __m128i m1 = _mm_set1_epi8(1);
  7349. uint64_t aux64;
  7350. // somewhat hacky, but gives a significant boost in performance
  7351. __m256i aux_gindex;
  7352. const uint16_t * gindex = (const uint16_t *)&aux_gindex;
  7353. __m256 accumf = _mm256_setzero_ps();
  7354. for (int i = 0; i < nb; ++i) {
  7355. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7356. const uint16_t * restrict q2 = x[i].qs;
  7357. const int8_t * restrict q8 = y[i].qs;
  7358. memcpy(&aux64, x[i].scales, 8);
  7359. __m128i stmp = _mm_set1_epi64x(aux64);
  7360. stmp = _mm_unpacklo_epi8(_mm_and_si128(stmp, m4), _mm_and_si128(_mm_srli_epi16(stmp, 4), m4));
  7361. const __m128i scales = _mm_add_epi8(_mm_slli_epi16(stmp, 1), m1);
  7362. __m256i sumi1 = _mm256_setzero_si256();
  7363. __m256i sumi2 = _mm256_setzero_si256();
  7364. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 4) {
  7365. const __m256i q2_data = _mm256_loadu_si256((const __m256i*)q2); q2 += 16;
  7366. aux_gindex = _mm256_and_si256(q2_data, m511);
  7367. const __m256i partial_sign_bits = _mm256_srli_epi16(q2_data, 9);
  7368. const __m256i partial_sign_bits_upper = _mm256_srli_epi16(q2_data, 13);
  7369. const __m256i partial_sign_bits_for_counting = _mm256_xor_si256(partial_sign_bits, partial_sign_bits_upper);
  7370. const __m256i odd_bits = _mm256_shuffle_epi8(bit_helper, partial_sign_bits_for_counting);
  7371. const __m256i full_sign_bits = _mm256_or_si256(partial_sign_bits, odd_bits);
  7372. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7373. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7374. const __m256i q8_3 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7375. const __m256i q8_4 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7376. const __m256i q2_1 = _mm256_set_epi64x(iq2xs_grid[gindex[ 3]], iq2xs_grid[gindex[ 2]],
  7377. iq2xs_grid[gindex[ 1]], iq2xs_grid[gindex[ 0]]);
  7378. const __m256i q2_2 = _mm256_set_epi64x(iq2xs_grid[gindex[ 7]], iq2xs_grid[gindex[ 6]],
  7379. iq2xs_grid[gindex[ 5]], iq2xs_grid[gindex[ 4]]);
  7380. const __m256i q2_3 = _mm256_set_epi64x(iq2xs_grid[gindex[11]], iq2xs_grid[gindex[10]],
  7381. iq2xs_grid[gindex[ 9]], iq2xs_grid[gindex[ 8]]);
  7382. const __m256i q2_4 = _mm256_set_epi64x(iq2xs_grid[gindex[15]], iq2xs_grid[gindex[14]],
  7383. iq2xs_grid[gindex[13]], iq2xs_grid[gindex[12]]);
  7384. const __m128i full_signs_l = _mm256_castsi256_si128(full_sign_bits);
  7385. const __m128i full_signs_h = _mm256_extractf128_si256(full_sign_bits, 1);
  7386. const __m256i full_signs_1 = MM256_SET_M128I(full_signs_l, full_signs_l);
  7387. const __m256i full_signs_2 = MM256_SET_M128I(full_signs_h, full_signs_h);
  7388. __m256i signs;
  7389. signs = _mm256_shuffle_epi8(full_signs_1, block_sign_shuffle_1);
  7390. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  7391. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, _mm256_or_si256(signs, mone));
  7392. signs = _mm256_shuffle_epi8(full_signs_1, block_sign_shuffle_2);
  7393. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  7394. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, _mm256_or_si256(signs, mone));
  7395. signs = _mm256_shuffle_epi8(full_signs_2, block_sign_shuffle_1);
  7396. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  7397. const __m256i q8s_3 = _mm256_sign_epi8(q8_3, _mm256_or_si256(signs, mone));
  7398. signs = _mm256_shuffle_epi8(full_signs_2, block_sign_shuffle_2);
  7399. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  7400. const __m256i q8s_4 = _mm256_sign_epi8(q8_4, _mm256_or_si256(signs, mone));
  7401. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  7402. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  7403. const __m256i dot3 = _mm256_maddubs_epi16(q2_3, q8s_3);
  7404. const __m256i dot4 = _mm256_maddubs_epi16(q2_4, q8s_4);
  7405. const __m256i sc1 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+0)));
  7406. const __m256i sc2 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+1)));
  7407. const __m256i sc3 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+2)));
  7408. const __m256i sc4 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+3)));
  7409. sumi1 = _mm256_add_epi32(sumi1, _mm256_madd_epi16(dot1, sc1));
  7410. sumi2 = _mm256_add_epi32(sumi2, _mm256_madd_epi16(dot2, sc2));
  7411. sumi1 = _mm256_add_epi32(sumi1, _mm256_madd_epi16(dot3, sc3));
  7412. sumi2 = _mm256_add_epi32(sumi2, _mm256_madd_epi16(dot4, sc4));
  7413. }
  7414. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  7415. }
  7416. *s = 0.125f * hsum_float_8(accumf);
  7417. #elif defined(__loongarch_asx)
  7418. const __m256i mone = __lasx_xvreplgr2vr_b(1);
  7419. static const char block_sign_shuffle_mask_1[32] = {
  7420. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
  7421. 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  7422. };
  7423. static const char block_sign_shuffle_mask_2[32] = {
  7424. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
  7425. 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e,
  7426. };
  7427. static const uint8_t bit_selector_mask_bytes[32] = {
  7428. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  7429. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  7430. };
  7431. const __m256i bit_selector_mask = __lasx_xvld((const __m256i*)bit_selector_mask_bytes, 0);
  7432. const __m256i block_sign_shuffle_1 = __lasx_xvld((const __m256i*)block_sign_shuffle_mask_1, 0);
  7433. const __m256i block_sign_shuffle_2 = __lasx_xvld((const __m256i*)block_sign_shuffle_mask_2, 0);
  7434. static const uint8_t k_bit_helper[32] = {
  7435. 0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00,
  7436. 0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00,
  7437. };
  7438. const __m256i bit_helper = __lasx_xvld((const __m256i*)k_bit_helper, 0);
  7439. const __m256i m511 = __lasx_xvreplgr2vr_h(511);
  7440. const __m128i m4 = __lsx_vreplgr2vr_b(0xf);
  7441. const __m128i m1 = __lsx_vreplgr2vr_b(1);
  7442. uint64_t aux64;
  7443. // somewhat hacky, but gives a significant boost in performance
  7444. __m256i aux_gindex;
  7445. const uint16_t * gindex = (const uint16_t *)&aux_gindex;
  7446. __m256 accumf = (__m256)__lasx_xvldi(0);
  7447. for (int i = 0; i < nb; ++i) {
  7448. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7449. const uint16_t * restrict q2 = x[i].qs;
  7450. const int8_t * restrict q8 = y[i].qs;
  7451. memcpy(&aux64, x[i].scales, 8);
  7452. __m128i stmp = __lsx_vreplgr2vr_d(aux64);
  7453. stmp = __lsx_vilvl_b( __lsx_vand_v(__lsx_vsrli_h(stmp, 4), m4), __lsx_vand_v(stmp, m4));
  7454. const __m128i scales = __lsx_vadd_b(__lsx_vslli_h(stmp, 1), m1);
  7455. __m256i sumi1 = __lasx_xvldi(0);
  7456. __m256i sumi2 = __lasx_xvldi(0);
  7457. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 4) {
  7458. const __m256i q2_data = __lasx_xvld((const __m256i*)q2, 0); q2 += 16;
  7459. aux_gindex = __lasx_xvand_v(q2_data, m511);
  7460. const __m256i partial_sign_bits = __lasx_xvsrli_h(q2_data, 9);
  7461. const __m256i partial_sign_bits_upper = __lasx_xvsrli_h(q2_data, 13);
  7462. const __m256i partial_sign_bits_for_counting = __lasx_xvxor_v(partial_sign_bits, partial_sign_bits_upper);
  7463. const __m256i odd_bits = lasx_shuffle_b(bit_helper, partial_sign_bits_for_counting);
  7464. const __m256i full_sign_bits = __lasx_xvor_v(partial_sign_bits, odd_bits);
  7465. const __m256i q8_1 = __lasx_xvld((const __m256i *)q8, 0); q8 += 32;
  7466. const __m256i q8_2 = __lasx_xvld((const __m256i *)q8, 0); q8 += 32;
  7467. const __m256i q8_3 = __lasx_xvld((const __m256i *)q8, 0); q8 += 32;
  7468. const __m256i q8_4 = __lasx_xvld((const __m256i *)q8, 0); q8 += 32;
  7469. const __m256i q2_1 = lasx_set_d(iq2xs_grid[gindex[ 3]], iq2xs_grid[gindex[ 2]],
  7470. iq2xs_grid[gindex[ 1]], iq2xs_grid[gindex[ 0]]);
  7471. const __m256i q2_2 = lasx_set_d(iq2xs_grid[gindex[ 7]], iq2xs_grid[gindex[ 6]],
  7472. iq2xs_grid[gindex[ 5]], iq2xs_grid[gindex[ 4]]);
  7473. const __m256i q2_3 = lasx_set_d(iq2xs_grid[gindex[11]], iq2xs_grid[gindex[10]],
  7474. iq2xs_grid[gindex[ 9]], iq2xs_grid[gindex[ 8]]);
  7475. const __m256i q2_4 = lasx_set_d(iq2xs_grid[gindex[15]], iq2xs_grid[gindex[14]],
  7476. iq2xs_grid[gindex[13]], iq2xs_grid[gindex[12]]);
  7477. const __m128i full_signs_l = lasx_extracti128(full_sign_bits, 0);
  7478. const __m128i full_signs_h = lasx_extracti128(full_sign_bits, 1);
  7479. const __m256i full_signs_1 = lasx_insertf128(full_signs_l, full_signs_l);
  7480. const __m256i full_signs_2 = lasx_insertf128(full_signs_h, full_signs_h);
  7481. __m256i signs;
  7482. signs = lasx_shuffle_b(full_signs_1, block_sign_shuffle_1);
  7483. signs = __lasx_xvseq_b(__lasx_xvand_v(signs, bit_selector_mask), bit_selector_mask);
  7484. const __m256i q8s_1 = __lasx_xvsigncov_b(__lasx_xvor_v(signs, mone), q8_1);
  7485. signs = lasx_shuffle_b(full_signs_1, block_sign_shuffle_2);
  7486. signs = __lasx_xvseq_b(__lasx_xvand_v(signs, bit_selector_mask), bit_selector_mask);
  7487. const __m256i q8s_2 = __lasx_xvsigncov_b(__lasx_xvor_v(signs, mone), q8_2);
  7488. signs = lasx_shuffle_b(full_signs_2, block_sign_shuffle_1);
  7489. signs = __lasx_xvseq_b(__lasx_xvand_v(signs, bit_selector_mask), bit_selector_mask);
  7490. const __m256i q8s_3 = __lasx_xvsigncov_b(__lasx_xvor_v(signs, mone), q8_3);
  7491. signs = lasx_shuffle_b(full_signs_2, block_sign_shuffle_2);
  7492. signs = __lasx_xvseq_b(__lasx_xvand_v(signs, bit_selector_mask), bit_selector_mask);
  7493. const __m256i q8s_4 = __lasx_xvsigncov_b(__lasx_xvor_v(signs, mone), q8_4);
  7494. const __m256i dot1 = lasx_maddubs_h(q2_1, q8s_1);
  7495. const __m256i dot2 = lasx_maddubs_h(q2_2, q8s_2);
  7496. const __m256i dot3 = lasx_maddubs_h(q2_3, q8s_3);
  7497. const __m256i dot4 = lasx_maddubs_h(q2_4, q8s_4);
  7498. const __m256i sc1 = lasx_ext8_16(lsx_shuffle_b(scales, get_scale_shuffle(ib32+0)));
  7499. const __m256i sc2 = lasx_ext8_16(lsx_shuffle_b(scales, get_scale_shuffle(ib32+1)));
  7500. const __m256i sc3 = lasx_ext8_16(lsx_shuffle_b(scales, get_scale_shuffle(ib32+2)));
  7501. const __m256i sc4 = lasx_ext8_16(lsx_shuffle_b(scales, get_scale_shuffle(ib32+3)));
  7502. sumi1 = __lasx_xvadd_w(sumi1, lasx_madd_h(dot1, sc1));
  7503. sumi2 = __lasx_xvadd_w(sumi2, lasx_madd_h(dot2, sc2));
  7504. sumi1 = __lasx_xvadd_w(sumi1, lasx_madd_h(dot3, sc3));
  7505. sumi2 = __lasx_xvadd_w(sumi2, lasx_madd_h(dot4, sc4));
  7506. }
  7507. accumf = __lasx_xvfmadd_s(__lasx_xvreplfr2vr_s(d), __lasx_xvffint_s_w(__lasx_xvadd_w(sumi1, sumi2)), accumf);
  7508. }
  7509. *s = 0.125f * hsum_float_8(accumf);
  7510. #elif defined(__POWER9_VECTOR__)
  7511. const vector int v0 = vec_splats((int32_t)0);
  7512. vector float vsumf0 = vec_splats(0.0f);
  7513. vector float vsumf1 = vec_splats(0.0f);
  7514. vector float vsumf2 = vec_splats(0.0f);
  7515. vector float vsumf3 = vec_splats(0.0f);
  7516. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  7517. for (int i = 0; i < nb; ++i) {
  7518. vector float vxd = vec_splats(GGML_FP16_TO_FP32(x[i].d));
  7519. vector float vyd = vec_splats(y[i].d);
  7520. vector float vd = vec_mul(vxd, vyd);
  7521. vector signed int vsumi0 = v0;
  7522. vector signed int vsumi1 = v0;
  7523. vector signed int vsumi2 = v0;
  7524. vector signed int vsumi3 = v0;
  7525. const uint16_t * restrict q2 = x[i].qs;
  7526. const uint8_t * restrict sc = x[i].scales;
  7527. const int8_t * restrict q8 = y[i].qs;
  7528. for (int j = 0; j < QK_K/64; ++j) {
  7529. __builtin_prefetch(q2, 0, 1);
  7530. __builtin_prefetch(q8, 0, 1);
  7531. vector signed long long aux64x2_0 = {*(const int64_t *)(iq2xs_grid + (q2[0] & 511)), *(const int64_t *)(iq2xs_grid + (q2[1] & 511))};
  7532. vector signed long long aux64x2_1 = {*(const int64_t *)(iq2xs_grid + (q2[2] & 511)), *(const int64_t *)(iq2xs_grid + (q2[3] & 511))};
  7533. vector signed long long aux64x2_2 = {*(const int64_t *)(iq2xs_grid + (q2[4] & 511)), *(const int64_t *)(iq2xs_grid + (q2[5] & 511))};
  7534. vector signed long long aux64x2_3 = {*(const int64_t *)(iq2xs_grid + (q2[6] & 511)), *(const int64_t *)(iq2xs_grid + (q2[7] & 511))};
  7535. vector signed long long vsigns0 = {*(const int64_t *)(signs64 + ((q2[0] >> 9))), *(const int64_t *)(signs64 + ((q2[1] >> 9)))};
  7536. vector signed long long vsigns1 = {*(const int64_t *)(signs64 + ((q2[2] >> 9))), *(const int64_t *)(signs64 + ((q2[3] >> 9)))};
  7537. vector signed long long vsigns2 = {*(const int64_t *)(signs64 + ((q2[4] >> 9))), *(const int64_t *)(signs64 + ((q2[5] >> 9)))};
  7538. vector signed long long vsigns3 = {*(const int64_t *)(signs64 + ((q2[6] >> 9))), *(const int64_t *)(signs64 + ((q2[7] >> 9)))};
  7539. q2 += 8;
  7540. vector signed char q2x0 = (vector signed char)vec_mul((vector signed char)vsigns0, (vector signed char)aux64x2_0);
  7541. vector signed char q2x1 = (vector signed char)vec_mul((vector signed char)vsigns1, (vector signed char)aux64x2_1);
  7542. vector signed char q2x2 = (vector signed char)vec_mul((vector signed char)vsigns2, (vector signed char)aux64x2_2);
  7543. vector signed char q2x3 = (vector signed char)vec_mul((vector signed char)vsigns3, (vector signed char)aux64x2_3);
  7544. vector signed char q8y0 = vec_xl( 0, q8);
  7545. vector signed char q8y1 = vec_xl(16, q8);
  7546. vector signed char q8y2 = vec_xl(32, q8);
  7547. vector signed char q8y3 = vec_xl(48, q8);
  7548. q8 += 64;
  7549. vector signed short qv0 = vec_add(vec_mule(q2x0, q8y0), vec_mulo(q2x0, q8y0));
  7550. vector signed short qv1 = vec_add(vec_mule(q2x1, q8y1), vec_mulo(q2x1, q8y1));
  7551. vector signed short qv2 = vec_add(vec_mule(q2x2, q8y2), vec_mulo(q2x2, q8y2));
  7552. vector signed short qv3 = vec_add(vec_mule(q2x3, q8y3), vec_mulo(q2x3, q8y3));
  7553. const uint16_t ls0 = (uint16_t)(sc[0] & 0xf);
  7554. const uint16_t ls1 = (uint16_t)(sc[0] >> 4);
  7555. const uint16_t ls2 = (uint16_t)(sc[1] & 0xf);
  7556. const uint16_t ls3 = (uint16_t)(sc[1] >> 4);
  7557. sc += 2;
  7558. vector signed short vscales0 = vec_splats((int16_t)(2*ls0+1));
  7559. vector signed short vscales1 = vec_splats((int16_t)(2*ls1+1));
  7560. vector signed short vscales2 = vec_splats((int16_t)(2*ls2+1));
  7561. vector signed short vscales3 = vec_splats((int16_t)(2*ls3+1));
  7562. vsumi0 = vec_msum(qv0, vscales0, vsumi0);
  7563. vsumi1 = vec_msum(qv1, vscales1, vsumi1);
  7564. vsumi2 = vec_msum(qv2, vscales2, vsumi2);
  7565. vsumi3 = vec_msum(qv3, vscales3, vsumi3);
  7566. }
  7567. vsumf0 = vec_madd(vec_ctf(vsumi0, 0), vd, vsumf0);
  7568. vsumf1 = vec_madd(vec_ctf(vsumi1, 0), vd, vsumf1);
  7569. vsumf2 = vec_madd(vec_ctf(vsumi2, 0), vd, vsumf2);
  7570. vsumf3 = vec_madd(vec_ctf(vsumi3, 0), vd, vsumf3);
  7571. }
  7572. vsumf0 = vec_add(vsumf0, vsumf2);
  7573. vsumf1 = vec_add(vsumf1, vsumf3);
  7574. vsumf0 = vec_add(vsumf0, vsumf1);
  7575. vsumf0 = vec_add(vsumf0, vec_sld(vsumf0, vsumf0, 4));
  7576. vsumf0 = vec_add(vsumf0, vec_sld(vsumf0, vsumf0, 8));
  7577. *s = 0.125f * vec_extract(vsumf0, 0);
  7578. #else
  7579. float sumf = 0.f;
  7580. for (int i = 0; i < nb; ++i) {
  7581. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7582. const uint16_t * restrict q2 = x[i].qs;
  7583. const uint8_t * restrict sc = x[i].scales;
  7584. const int8_t * restrict q8 = y[i].qs;
  7585. int32_t bsum = 0;
  7586. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  7587. const uint16_t ls1 = 2*(sc[ib32] & 0xf) + 1;
  7588. const uint16_t ls2 = 2*(sc[ib32] >> 4) + 1;
  7589. int32_t sumi = 0;
  7590. for (int l = 0; l < 2; ++l) {
  7591. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[l] & 511));
  7592. const uint8_t signs = ksigns_iq2xs[q2[l] >> 9];
  7593. for (int j = 0; j < 8; ++j) {
  7594. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  7595. }
  7596. q8 += 8;
  7597. }
  7598. bsum += sumi * ls1;
  7599. sumi = 0;
  7600. for (int l = 2; l < 4; ++l) {
  7601. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[l] & 511));
  7602. const uint8_t signs = ksigns_iq2xs[q2[l] >> 9];
  7603. for (int j = 0; j < 8; ++j) {
  7604. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  7605. }
  7606. q8 += 8;
  7607. }
  7608. bsum += sumi * ls2;
  7609. q2 += 4;
  7610. }
  7611. sumf += d * bsum;
  7612. }
  7613. *s = 0.125f * sumf;
  7614. #endif
  7615. }
  7616. void ggml_vec_dot_iq2_s_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  7617. assert(n % QK_K == 0);
  7618. assert(nrc == 1);
  7619. UNUSED(nrc);
  7620. UNUSED(bx);
  7621. UNUSED(by);
  7622. UNUSED(bs);
  7623. const block_iq2_s * restrict x = vx;
  7624. const block_q8_K * restrict y = vy;
  7625. const int nb = n / QK_K;
  7626. #if defined(__ARM_NEON)
  7627. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  7628. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  7629. };
  7630. static const uint8_t k_mask2[16] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,};
  7631. const ggml_uint8x16x2_t mask1 = ggml_vld1q_u8_x2(k_mask1);
  7632. const uint8x16_t mask2 = vld1q_u8(k_mask2);
  7633. const uint8x16_t m1 = vdupq_n_u8(1);
  7634. const int32x4_t vzero = vdupq_n_s32(0);
  7635. uint8x16x2_t vs;
  7636. ggml_int8x16x4_t q2s;
  7637. ggml_int8x16x4_t q8b;
  7638. float sumf = 0;
  7639. for (int i = 0; i < nb; ++i) {
  7640. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7641. const uint8_t * restrict qs = x[i].qs;
  7642. const uint8_t * restrict qh = x[i].qh;
  7643. const uint16_t * restrict signs = (const uint16_t *)(x[i].qs + QK_K/8);
  7644. const int8_t * restrict q8 = y[i].qs;
  7645. int sumi1 = 0, sumi2 = 0;
  7646. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7647. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7648. q2s.val[0] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[0] | ((qh[ib32+0] << 8) & 0x300)))),
  7649. vld1_s8((const int8_t *)(iq2s_grid + (qs[1] | ((qh[ib32+0] << 6) & 0x300)))));
  7650. q2s.val[1] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[2] | ((qh[ib32+0] << 4) & 0x300)))),
  7651. vld1_s8((const int8_t *)(iq2s_grid + (qs[3] | ((qh[ib32+0] << 2) & 0x300)))));
  7652. q2s.val[2] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[4] | ((qh[ib32+1] << 8) & 0x300)))),
  7653. vld1_s8((const int8_t *)(iq2s_grid + (qs[5] | ((qh[ib32+1] << 6) & 0x300)))));
  7654. q2s.val[3] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[6] | ((qh[ib32+1] << 4) & 0x300)))),
  7655. vld1_s8((const int8_t *)(iq2s_grid + (qs[7] | ((qh[ib32+1] << 2) & 0x300)))));
  7656. qs += 8;
  7657. vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[0] | ((uint32_t) signs[1] << 16)));
  7658. vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
  7659. vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
  7660. vs.val[0] = vceqq_u8(vs.val[0], mask2);
  7661. vs.val[1] = vceqq_u8(vs.val[1], mask2);
  7662. q2s.val[0] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[0], m1)), q2s.val[0]);
  7663. q2s.val[1] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[1], m1)), q2s.val[1]);
  7664. vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[2] | ((uint32_t) signs[3] << 16)));
  7665. vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
  7666. vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
  7667. vs.val[0] = vceqq_u8(vs.val[0], mask2);
  7668. vs.val[1] = vceqq_u8(vs.val[1], mask2);
  7669. signs += 4;
  7670. q2s.val[2] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[0], m1)), q2s.val[2]);
  7671. q2s.val[3] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[1], m1)), q2s.val[3]);
  7672. const int32x4_t p1 = ggml_vdotq_s32(vzero, q2s.val[0], q8b.val[0]);
  7673. const int32x4_t p2 = ggml_vdotq_s32(vzero, q2s.val[1], q8b.val[1]);
  7674. const int32x4_t p3 = ggml_vdotq_s32(vzero, q2s.val[2], q8b.val[2]);
  7675. const int32x4_t p4 = ggml_vdotq_s32(vzero, q2s.val[3], q8b.val[3]);
  7676. sumi1 += vaddvq_s32(p1) * (1 + 2*(x[i].scales[ib32+0] & 0xf));
  7677. sumi2 += vaddvq_s32(p2) * (1 + 2*(x[i].scales[ib32+0] >> 4));
  7678. sumi1 += vaddvq_s32(p3) * (1 + 2*(x[i].scales[ib32+1] & 0xf));
  7679. sumi2 += vaddvq_s32(p4) * (1 + 2*(x[i].scales[ib32+1] >> 4));
  7680. }
  7681. sumf += d*(sumi1 + sumi2);
  7682. }
  7683. *s = 0.125f * sumf;
  7684. #elif defined(__AVX2__)
  7685. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  7686. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  7687. };
  7688. static const uint8_t k_mask2[32] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  7689. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  7690. };
  7691. const __m128i m4 = _mm_set1_epi8(0xf);
  7692. const __m128i m1 = _mm_set1_epi8(1);
  7693. const __m256i mask1 = _mm256_loadu_si256((const __m256i*)k_mask1);
  7694. const __m256i mask2 = _mm256_loadu_si256((const __m256i*)k_mask2);
  7695. uint64_t aux64;
  7696. __m256 accumf = _mm256_setzero_ps();
  7697. for (int i = 0; i < nb; ++i) {
  7698. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7699. const uint8_t * restrict qs = x[i].qs;
  7700. const uint8_t * restrict qh = x[i].qh;
  7701. const uint16_t * restrict signs = (const uint16_t *)(x[i].qs + QK_K/8);
  7702. const int8_t * restrict q8 = y[i].qs;
  7703. memcpy(&aux64, x[i].scales, 8);
  7704. const __m128i scales8 = _mm_add_epi8(_mm_slli_epi16(_mm_and_si128(_mm_set_epi64x(aux64 >> 4, aux64), m4), 1), m1);
  7705. const __m256i scales16 = _mm256_cvtepi8_epi16(scales8); // 0 2 4 6 8 10 12 14 1 3 5 7 9 11 13 15
  7706. __m256i sumi1 = _mm256_setzero_si256();
  7707. __m256i sumi2 = _mm256_setzero_si256();
  7708. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7709. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7710. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7711. const __m256i q2_1 = _mm256_set_epi64x(iq2s_grid[qs[3] | ((qh[ib32+0] << 2) & 0x300)],
  7712. iq2s_grid[qs[2] | ((qh[ib32+0] << 4) & 0x300)],
  7713. iq2s_grid[qs[1] | ((qh[ib32+0] << 6) & 0x300)],
  7714. iq2s_grid[qs[0] | ((qh[ib32+0] << 8) & 0x300)]);
  7715. const __m256i q2_2 = _mm256_set_epi64x(iq2s_grid[qs[7] | ((qh[ib32+1] << 2) & 0x300)],
  7716. iq2s_grid[qs[6] | ((qh[ib32+1] << 4) & 0x300)],
  7717. iq2s_grid[qs[5] | ((qh[ib32+1] << 6) & 0x300)],
  7718. iq2s_grid[qs[4] | ((qh[ib32+1] << 8) & 0x300)]);
  7719. qs += 8;
  7720. __m256i aux256 = _mm256_set1_epi32(signs[0] | ((uint32_t) signs[1] << 16));
  7721. aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
  7722. const __m256i s2_1 = _mm256_cmpeq_epi8(aux256, mask2);
  7723. const __m256i q8s_1 = _mm256_sub_epi8(_mm256_xor_si256(s2_1, q8_1), s2_1);
  7724. aux256 = _mm256_set1_epi32(signs[2] | ((uint32_t) signs[3] << 16));
  7725. aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
  7726. const __m256i s2_2 = _mm256_cmpeq_epi8(aux256, mask2);
  7727. const __m256i q8s_2 = _mm256_sub_epi8(_mm256_xor_si256(s2_2, q8_2), s2_2);
  7728. signs += 4;
  7729. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1); // blocks 2*ib32+0, 2*ib32+1
  7730. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2); // blocks 2*ib32+2, 2*ib32+3
  7731. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_shuffle_epi8(scales16, get_scale_shuffle_k4(ib32+0)));
  7732. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_shuffle_epi8(scales16, get_scale_shuffle_k4(ib32+1)));
  7733. sumi1 = _mm256_add_epi32(sumi1, p1);
  7734. sumi2 = _mm256_add_epi32(sumi2, p2);
  7735. }
  7736. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  7737. }
  7738. *s = 0.125f * hsum_float_8(accumf);
  7739. #elif defined(__POWER9_VECTOR__)
  7740. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  7741. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  7742. };
  7743. static const uint8_t k_mask2[16] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,};
  7744. const vector int v0 = vec_splats((int32_t)0);
  7745. vector float vsumf0 = vec_splats(0.0f);
  7746. vector float vsumf1 = vec_splats(0.0f);
  7747. vector float vsumf2 = vec_splats(0.0f);
  7748. vector float vsumf3 = vec_splats(0.0f);
  7749. const vector unsigned char mask0 = vec_xl( 0, k_mask1);
  7750. const vector unsigned char mask1 = vec_xl(16, k_mask1);
  7751. const vector signed char mask2 = (vector signed char)vec_xl( 0, k_mask2);
  7752. for (int i = 0; i < nb; ++i) {
  7753. vector float vxd = vec_splats(GGML_FP16_TO_FP32(x[i].d));
  7754. vector float vyd = vec_splats(y[i].d);
  7755. vector float vd = vec_mul(vxd, vyd);
  7756. vector signed int vsumi0 = v0;
  7757. vector signed int vsumi1 = v0;
  7758. vector signed int vsumi2 = v0;
  7759. vector signed int vsumi3 = v0;
  7760. const uint8_t * restrict q2 = x[i].qs;
  7761. const uint8_t * restrict qh = x[i].qh;
  7762. const uint16_t * restrict signs = (const uint16_t *)(x[i].qs + QK_K/8);
  7763. const uint8_t * restrict sc = x[i].scales;
  7764. const int8_t * restrict q8 = y[i].qs;
  7765. for (int j = 0; j < QK_K/32; j += 2) {
  7766. __builtin_prefetch(q2, 0, 1);
  7767. __builtin_prefetch(q8, 0, 1);
  7768. vector signed long long aux64x2_0 = {*(const int64_t *)(iq2s_grid + (q2[0] | ((qh[0] << 8) & 0x300))), *(const int64_t *)(iq2s_grid + (q2[1] | ((qh[0] << 6) & 0x300)))};
  7769. vector signed long long aux64x2_1 = {*(const int64_t *)(iq2s_grid + (q2[2] | ((qh[0] << 4) & 0x300))), *(const int64_t *)(iq2s_grid + (q2[3] | ((qh[0] << 2) & 0x300)))};
  7770. vector signed long long aux64x2_2 = {*(const int64_t *)(iq2s_grid + (q2[4] | ((qh[1] << 8) & 0x300))), *(const int64_t *)(iq2s_grid + (q2[5] | ((qh[1] << 6) & 0x300)))};
  7771. vector signed long long aux64x2_3 = {*(const int64_t *)(iq2s_grid + (q2[6] | ((qh[1] << 4) & 0x300))), *(const int64_t *)(iq2s_grid + (q2[7] | ((qh[1] << 2) & 0x300)))};
  7772. q2 += 8;
  7773. qh += 2;
  7774. vector signed char vsigns01 = (vector signed char)vec_splats(*(const uint32_t *)&signs[0]);
  7775. vector signed char vsigns23 = (vector signed char)vec_splats(*(const uint32_t *)&signs[2]);
  7776. signs += 4;
  7777. vector signed char vsigns0 = vec_perm(vsigns01, vsigns01, mask0);
  7778. vector signed char vsigns1 = vec_perm(vsigns01, vsigns01, mask1);
  7779. vector signed char vsigns2 = vec_perm(vsigns23, vsigns23, mask0);
  7780. vector signed char vsigns3 = vec_perm(vsigns23, vsigns23, mask1);
  7781. vsigns0 = (vector signed char)vec_cmpeq(vec_and(vsigns0, mask2), mask2);
  7782. vsigns1 = (vector signed char)vec_cmpeq(vec_and(vsigns1, mask2), mask2);
  7783. vsigns2 = (vector signed char)vec_cmpeq(vec_and(vsigns2, mask2), mask2);
  7784. vsigns3 = (vector signed char)vec_cmpeq(vec_and(vsigns3, mask2), mask2);
  7785. vector signed char q2x0 = vec_sub(vec_xor(vsigns0, (vector signed char)aux64x2_0), vsigns0);
  7786. vector signed char q2x1 = vec_sub(vec_xor(vsigns1, (vector signed char)aux64x2_1), vsigns1);
  7787. vector signed char q2x2 = vec_sub(vec_xor(vsigns2, (vector signed char)aux64x2_2), vsigns2);
  7788. vector signed char q2x3 = vec_sub(vec_xor(vsigns3, (vector signed char)aux64x2_3), vsigns3);
  7789. vector signed char q8y0 = vec_xl( 0, q8);
  7790. vector signed char q8y1 = vec_xl(16, q8);
  7791. vector signed char q8y2 = vec_xl(32, q8);
  7792. vector signed char q8y3 = vec_xl(48, q8);
  7793. q8 += 64;
  7794. vector signed short qv0 = vec_add(vec_mule(q2x0, q8y0), vec_mulo(q2x0, q8y0));
  7795. vector signed short qv1 = vec_add(vec_mule(q2x1, q8y1), vec_mulo(q2x1, q8y1));
  7796. vector signed short qv2 = vec_add(vec_mule(q2x2, q8y2), vec_mulo(q2x2, q8y2));
  7797. vector signed short qv3 = vec_add(vec_mule(q2x3, q8y3), vec_mulo(q2x3, q8y3));
  7798. const uint16_t ls0 = (uint16_t)(sc[0] & 0xf);
  7799. const uint16_t ls1 = (uint16_t)(sc[0] >> 4);
  7800. const uint16_t ls2 = (uint16_t)(sc[1] & 0xf);
  7801. const uint16_t ls3 = (uint16_t)(sc[1] >> 4);
  7802. sc += 2;
  7803. vector signed short vscales0 = vec_splats((int16_t)(2*ls0+1));
  7804. vector signed short vscales1 = vec_splats((int16_t)(2*ls1+1));
  7805. vector signed short vscales2 = vec_splats((int16_t)(2*ls2+1));
  7806. vector signed short vscales3 = vec_splats((int16_t)(2*ls3+1));
  7807. vsumi0 = vec_msum(qv0, vscales0, vsumi0);
  7808. vsumi1 = vec_msum(qv1, vscales1, vsumi1);
  7809. vsumi2 = vec_msum(qv2, vscales2, vsumi2);
  7810. vsumi3 = vec_msum(qv3, vscales3, vsumi3);
  7811. }
  7812. vsumf0 = vec_madd(vec_ctf(vsumi0, 0), vd, vsumf0);
  7813. vsumf1 = vec_madd(vec_ctf(vsumi1, 0), vd, vsumf1);
  7814. vsumf2 = vec_madd(vec_ctf(vsumi2, 0), vd, vsumf2);
  7815. vsumf3 = vec_madd(vec_ctf(vsumi3, 0), vd, vsumf3);
  7816. }
  7817. vsumf0 = vec_add(vsumf0, vsumf2);
  7818. vsumf1 = vec_add(vsumf1, vsumf3);
  7819. vsumf0 = vec_add(vsumf0, vsumf1);
  7820. vsumf0 = vec_add(vsumf0, vec_sld(vsumf0, vsumf0, 4));
  7821. vsumf0 = vec_add(vsumf0, vec_sld(vsumf0, vsumf0, 8));
  7822. *s = 0.125f * vec_extract(vsumf0, 0);
  7823. #elif defined(__loongarch_asx)
  7824. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  7825. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  7826. };
  7827. static const uint8_t k_mask2[32] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  7828. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  7829. };
  7830. const __m128i m4 = __lsx_vreplgr2vr_b(0xf);
  7831. const __m128i m1 = __lsx_vreplgr2vr_b(1);
  7832. const __m256i mask1 = __lasx_xvld((const __m256i*)k_mask1, 0);
  7833. const __m256i mask2 = __lasx_xvld((const __m256i*)k_mask2, 0);
  7834. uint64_t aux64;
  7835. __m256 accumf = (__m256)__lasx_xvldi(0);
  7836. for (int i = 0; i < nb; ++i) {
  7837. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7838. const uint8_t * restrict qs = x[i].qs;
  7839. const uint8_t * restrict qh = x[i].qh;
  7840. const uint16_t * restrict signs = (const uint16_t *)(x[i].qs + QK_K/8);
  7841. const int8_t * restrict q8 = y[i].qs;
  7842. __m128i tmp1;
  7843. memcpy(&aux64, x[i].scales, 8);
  7844. tmp1 = __lsx_vinsgr2vr_d(tmp1, aux64, 0);
  7845. tmp1 = __lsx_vinsgr2vr_d(tmp1, aux64 >> 4, 1);
  7846. const __m128i scales8 = __lsx_vadd_b(__lsx_vslli_h(__lsx_vand_v(tmp1, m4), 1), m1);
  7847. const __m256i scales16 = lasx_ext8_16(scales8); // 0 2 4 6 8 10 12 14 1 3 5 7 9 11 13 15
  7848. __m256i sumi1 = __lasx_xvldi(0);
  7849. __m256i sumi2 = __lasx_xvldi(0);
  7850. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7851. const __m256i q8_1 = __lasx_xvld((const __m256i *)q8, 0); q8 += 32;
  7852. const __m256i q8_2 = __lasx_xvld((const __m256i *)q8, 0); q8 += 32;
  7853. const __m256i q2_1 = lasx_set_d(iq2s_grid[qs[3] | ((qh[ib32+0] << 2) & 0x300)],
  7854. iq2s_grid[qs[2] | ((qh[ib32+0] << 4) & 0x300)],
  7855. iq2s_grid[qs[1] | ((qh[ib32+0] << 6) & 0x300)],
  7856. iq2s_grid[qs[0] | ((qh[ib32+0] << 8) & 0x300)]);
  7857. const __m256i q2_2 = lasx_set_d(iq2s_grid[qs[7] | ((qh[ib32+1] << 2) & 0x300)],
  7858. iq2s_grid[qs[6] | ((qh[ib32+1] << 4) & 0x300)],
  7859. iq2s_grid[qs[5] | ((qh[ib32+1] << 6) & 0x300)],
  7860. iq2s_grid[qs[4] | ((qh[ib32+1] << 8) & 0x300)]);
  7861. qs += 8;
  7862. __m256i aux256 = __lasx_xvreplgr2vr_w(signs[0] | ((uint32_t) signs[1] << 16));
  7863. aux256 = __lasx_xvand_v(lasx_shuffle_b(aux256,mask1), mask2);
  7864. const __m256i s2_1 = __lasx_xvseq_b(aux256, mask2);
  7865. const __m256i q8s_1 = __lasx_xvsub_b(__lasx_xvxor_v(s2_1, q8_1), s2_1);
  7866. aux256 = __lasx_xvreplgr2vr_w(signs[2] | ((uint32_t) signs[3] << 16));
  7867. aux256 = __lasx_xvand_v(lasx_shuffle_b(aux256,mask1), mask2);
  7868. const __m256i s2_2 = __lasx_xvseq_b(aux256, mask2);
  7869. const __m256i q8s_2 = __lasx_xvsub_b(__lasx_xvxor_v(s2_2, q8_2), s2_2);
  7870. signs += 4;
  7871. const __m256i dot1 = lasx_maddubs_h(q2_1, q8s_1); // blocks 2*ib32+0, 2*ib32+1
  7872. const __m256i dot2 = lasx_maddubs_h(q2_2, q8s_2); // blocks 2*ib32+2, 2*ib32+3
  7873. const __m256i p1 = lasx_madd_h(dot1, lasx_shuffle_b(scales16, get_scale_shuffle_k4(ib32+0)));
  7874. const __m256i p2 = lasx_madd_h(dot2, lasx_shuffle_b(scales16, get_scale_shuffle_k4(ib32+1)));
  7875. sumi1 = __lasx_xvadd_w(sumi1, p1);
  7876. sumi2 = __lasx_xvadd_w(sumi2, p2);
  7877. }
  7878. accumf = __lasx_xvfmadd_s(__lasx_xvreplfr2vr_s(d), __lasx_xvffint_s_w(__lasx_xvadd_w(sumi1, sumi2)), accumf);
  7879. }
  7880. *s = 0.125f * hsum_float_8(accumf);
  7881. #else
  7882. float sumf = 0;
  7883. for (int i = 0; i < nb; i++) {
  7884. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7885. const int8_t * q8 = y[i].qs;
  7886. const uint8_t * qs = x[i].qs;
  7887. const uint8_t * qh = x[i].qh;
  7888. const uint8_t * signs = qs + QK_K/8;
  7889. int bsum = 0;
  7890. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  7891. int ls1 = 1 + 2*(x[i].scales[ib32] & 0xf);
  7892. int ls2 = 1 + 2*(x[i].scales[ib32] >> 4);
  7893. int sumi1 = 0, sumi2 = 0;
  7894. for (int l = 0; l < 2; ++l) {
  7895. const uint8_t * grid = (const uint8_t *)(iq2s_grid + (qs[l] | (qh[ib32] << (8-2*l) & 0x300)));
  7896. for (int j = 0; j < 8; ++j) {
  7897. sumi1 += q8[j] * grid[j] * (signs[l] & kmask_iq2xs[j] ? -1 : 1);
  7898. }
  7899. q8 += 8;
  7900. }
  7901. for (int l = 2; l < 4; ++l) {
  7902. const uint8_t * grid = (const uint8_t *)(iq2s_grid + (qs[l] | (qh[ib32] << (8-2*l) & 0x300)));
  7903. for (int j = 0; j < 8; ++j) {
  7904. sumi2 += q8[j] * grid[j] * (signs[l] & kmask_iq2xs[j] ? -1 : 1);
  7905. }
  7906. q8 += 8;
  7907. }
  7908. bsum += ls1 * sumi1 + ls2 * sumi2;
  7909. qs += 4;
  7910. signs += 4;
  7911. }
  7912. sumf += d * bsum;
  7913. }
  7914. *s = 0.125f * sumf;
  7915. #endif
  7916. }
  7917. void ggml_vec_dot_iq3_xxs_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  7918. assert(n % QK_K == 0);
  7919. assert(nrc == 1);
  7920. UNUSED(nrc);
  7921. UNUSED(bx);
  7922. UNUSED(by);
  7923. UNUSED(bs);
  7924. const block_iq3_xxs * restrict x = vx;
  7925. const block_q8_K * restrict y = vy;
  7926. const int nb = n / QK_K;
  7927. #if defined(__ARM_NEON)
  7928. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  7929. uint32_t aux32[2];
  7930. ggml_int8x16x4_t q3s;
  7931. ggml_int8x16x4_t q8b;
  7932. float sumf = 0;
  7933. for (int i = 0; i < nb; ++i) {
  7934. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7935. const uint8_t * restrict q3 = x[i].qs;
  7936. const uint8_t * restrict gas = x[i].qs + QK_K/4;
  7937. const int8_t * restrict q8 = y[i].qs;
  7938. float sumf1 = 0, sumf2 = 0;
  7939. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7940. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7941. memcpy(aux32, gas, 2*sizeof(uint32_t)); gas += 2*sizeof(uint32_t);
  7942. const uint32x4_t aux32x4_0 = ggml_vld1q_u32(iq3xxs_grid[q3[ 0]], iq3xxs_grid[q3[ 1]], iq3xxs_grid[q3[ 2]], iq3xxs_grid[q3[ 3]]);
  7943. const uint32x4_t aux32x4_1 = ggml_vld1q_u32(iq3xxs_grid[q3[ 4]], iq3xxs_grid[q3[ 5]], iq3xxs_grid[q3[ 6]], iq3xxs_grid[q3[ 7]]);
  7944. const uint32x4_t aux32x4_2 = ggml_vld1q_u32(iq3xxs_grid[q3[ 8]], iq3xxs_grid[q3[ 9]], iq3xxs_grid[q3[10]], iq3xxs_grid[q3[11]]);
  7945. const uint32x4_t aux32x4_3 = ggml_vld1q_u32(iq3xxs_grid[q3[12]], iq3xxs_grid[q3[13]], iq3xxs_grid[q3[14]], iq3xxs_grid[q3[15]]);
  7946. q3 += 16;
  7947. q3s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[0] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[0] >> 7) & 127))));
  7948. q3s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[0] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[0] >> 21) & 127))));
  7949. q3s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 7) & 127))));
  7950. q3s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 21) & 127))));
  7951. q3s.val[0] = vmulq_s8(q3s.val[0], vreinterpretq_s8_u32(aux32x4_0));
  7952. q3s.val[1] = vmulq_s8(q3s.val[1], vreinterpretq_s8_u32(aux32x4_1));
  7953. q3s.val[2] = vmulq_s8(q3s.val[2], vreinterpretq_s8_u32(aux32x4_2));
  7954. q3s.val[3] = vmulq_s8(q3s.val[3], vreinterpretq_s8_u32(aux32x4_3));
  7955. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q3s.val[0], q8b.val[0]), q3s.val[1], q8b.val[1]);
  7956. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q3s.val[2], q8b.val[2]), q3s.val[3], q8b.val[3]);
  7957. sumf1 += vaddvq_s32(p1) * (0.5f + (aux32[0] >> 28));
  7958. sumf2 += vaddvq_s32(p2) * (0.5f + (aux32[1] >> 28));
  7959. }
  7960. sumf += d*(sumf1 + sumf2);
  7961. }
  7962. *s = 0.5f * sumf;
  7963. #elif defined(__AVX2__)
  7964. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  7965. uint32_t aux32[2];
  7966. __m256 accumf = _mm256_setzero_ps();
  7967. for (int i = 0; i < nb; ++i) {
  7968. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7969. const uint8_t * restrict q3 = x[i].qs;
  7970. const uint8_t * restrict gas = x[i].qs + QK_K/4;
  7971. const int8_t * restrict q8 = y[i].qs;
  7972. __m256i sumi1 = _mm256_setzero_si256();
  7973. __m256i sumi2 = _mm256_setzero_si256();
  7974. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7975. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7976. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7977. const __m256i q2_1 = _mm256_set_epi32(iq3xxs_grid[q3[7]], iq3xxs_grid[q3[6]], iq3xxs_grid[q3[5]], iq3xxs_grid[q3[4]],
  7978. iq3xxs_grid[q3[3]], iq3xxs_grid[q3[2]], iq3xxs_grid[q3[1]], iq3xxs_grid[q3[0]]);
  7979. q3 += 8;
  7980. const __m256i q2_2 = _mm256_set_epi32(iq3xxs_grid[q3[7]], iq3xxs_grid[q3[6]], iq3xxs_grid[q3[5]], iq3xxs_grid[q3[4]],
  7981. iq3xxs_grid[q3[3]], iq3xxs_grid[q3[2]], iq3xxs_grid[q3[1]], iq3xxs_grid[q3[0]]);
  7982. q3 += 8;
  7983. memcpy(aux32, gas, 8); gas += 8;
  7984. const __m256i s2_1 = _mm256_set_epi64x(signs64[(aux32[0] >> 21) & 127], signs64[(aux32[0] >> 14) & 127],
  7985. signs64[(aux32[0] >> 7) & 127], signs64[(aux32[0] >> 0) & 127]);
  7986. const __m256i s2_2 = _mm256_set_epi64x(signs64[(aux32[1] >> 21) & 127], signs64[(aux32[1] >> 14) & 127],
  7987. signs64[(aux32[1] >> 7) & 127], signs64[(aux32[1] >> 0) & 127]);
  7988. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, s2_1);
  7989. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, s2_2);
  7990. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  7991. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  7992. const uint16_t ls1 = aux32[0] >> 28;
  7993. const uint16_t ls2 = aux32[1] >> 28;
  7994. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
  7995. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
  7996. sumi1 = _mm256_add_epi32(sumi1, p1);
  7997. sumi2 = _mm256_add_epi32(sumi2, p2);
  7998. }
  7999. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  8000. }
  8001. *s = 0.25f * hsum_float_8(accumf);
  8002. #elif defined(__POWER9_VECTOR__)
  8003. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  8004. const vector int v0 = vec_splats((int32_t)0);
  8005. vector float vsumf0 = vec_splats(0.0f);
  8006. vector float vsumf1 = vec_splats(0.0f);
  8007. vector float vsumf2 = vec_splats(0.0f);
  8008. vector float vsumf3 = vec_splats(0.0f);
  8009. for (int i = 0; i < nb; ++i) {
  8010. vector float vxd = vec_splats(GGML_FP16_TO_FP32(x[i].d));
  8011. vector float vyd = vec_splats(y[i].d);
  8012. vector float vd = vec_mul(vxd, vyd);
  8013. vector signed int vsumi0 = v0;
  8014. vector signed int vsumi1 = v0;
  8015. vector signed int vsumi2 = v0;
  8016. vector signed int vsumi3 = v0;
  8017. const uint8_t * restrict q3 = x[i].qs;
  8018. const uint32_t * restrict signs = (const uint32_t *)(x[i].qs + QK_K/4);
  8019. const int8_t * restrict q8 = y[i].qs;
  8020. #pragma GCC unroll 1
  8021. for (int j = 0; j < QK_K/32; j += 2) {
  8022. __builtin_prefetch(q3, 0, 1);
  8023. __builtin_prefetch(q8, 0, 1);
  8024. vector unsigned int aux32x4_0 = {iq3xxs_grid[q3[ 0]], iq3xxs_grid[q3[ 1]], iq3xxs_grid[q3[ 2]], iq3xxs_grid[q3[ 3]]};
  8025. vector unsigned int aux32x4_1 = {iq3xxs_grid[q3[ 4]], iq3xxs_grid[q3[ 5]], iq3xxs_grid[q3[ 6]], iq3xxs_grid[q3[ 7]]};
  8026. vector unsigned int aux32x4_2 = {iq3xxs_grid[q3[ 8]], iq3xxs_grid[q3[ 9]], iq3xxs_grid[q3[10]], iq3xxs_grid[q3[11]]};
  8027. vector unsigned int aux32x4_3 = {iq3xxs_grid[q3[12]], iq3xxs_grid[q3[13]], iq3xxs_grid[q3[14]], iq3xxs_grid[q3[15]]};
  8028. q3 += 16;
  8029. vector unsigned long long aux64x2_0 = {(uint64_t)(signs64[(signs[0] >> 0) & 127]), (uint64_t)(signs64[(signs[0] >> 7) & 127])};
  8030. vector unsigned long long aux64x2_1 = {(uint64_t)(signs64[(signs[0] >> 14) & 127]), (uint64_t)(signs64[(signs[0] >> 21) & 127])};
  8031. vector unsigned long long aux64x2_2 = {(uint64_t)(signs64[(signs[1] >> 0) & 127]), (uint64_t)(signs64[(signs[1] >> 7) & 127])};
  8032. vector unsigned long long aux64x2_3 = {(uint64_t)(signs64[(signs[1] >> 14) & 127]), (uint64_t)(signs64[(signs[1] >> 21) & 127])};
  8033. vector signed char q3x0 = vec_mul((vector signed char)aux64x2_0, (vector signed char)aux32x4_0);
  8034. vector signed char q3x1 = vec_mul((vector signed char)aux64x2_1, (vector signed char)aux32x4_1);
  8035. vector signed char q3x2 = vec_mul((vector signed char)aux64x2_2, (vector signed char)aux32x4_2);
  8036. vector signed char q3x3 = vec_mul((vector signed char)aux64x2_3, (vector signed char)aux32x4_3);
  8037. vector signed char q8y0 = vec_xl( 0, q8);
  8038. vector signed char q8y1 = vec_xl(16, q8);
  8039. vector signed char q8y2 = vec_xl(32, q8);
  8040. vector signed char q8y3 = vec_xl(48, q8);
  8041. q8 += 64;
  8042. vector signed short qv0 = vec_add(vec_mule(q3x0, q8y0), vec_mulo(q3x0, q8y0));
  8043. vector signed short qv1 = vec_add(vec_mule(q3x1, q8y1), vec_mulo(q3x1, q8y1));
  8044. vector signed short qv2 = vec_add(vec_mule(q3x2, q8y2), vec_mulo(q3x2, q8y2));
  8045. vector signed short qv3 = vec_add(vec_mule(q3x3, q8y3), vec_mulo(q3x3, q8y3));
  8046. const uint16_t ls0 = (uint16_t)(signs[0] >> 28);
  8047. const uint16_t ls1 = (uint16_t)(signs[1] >> 28);
  8048. signs += 2;
  8049. vector signed short vscales01 = (vector signed short)vec_splats((uint16_t)(2*ls0+1));
  8050. vector signed short vscales23 = (vector signed short)vec_splats((uint16_t)(2*ls1+1));
  8051. vsumi0 = vec_msum(qv0, vscales01, vsumi0);
  8052. vsumi1 = vec_msum(qv1, vscales01, vsumi1);
  8053. vsumi2 = vec_msum(qv2, vscales23, vsumi2);
  8054. vsumi3 = vec_msum(qv3, vscales23, vsumi3);
  8055. }
  8056. vsumf0 = vec_madd(vec_ctf(vsumi0, 0), vd, vsumf0);
  8057. vsumf1 = vec_madd(vec_ctf(vsumi1, 0), vd, vsumf1);
  8058. vsumf2 = vec_madd(vec_ctf(vsumi2, 0), vd, vsumf2);
  8059. vsumf3 = vec_madd(vec_ctf(vsumi3, 0), vd, vsumf3);
  8060. }
  8061. vsumf0 = vec_add(vsumf0, vsumf2);
  8062. vsumf1 = vec_add(vsumf1, vsumf3);
  8063. vsumf0 = vec_add(vsumf0, vsumf1);
  8064. vsumf0 = vec_add(vsumf0, vec_sld(vsumf0, vsumf0, 4));
  8065. vsumf0 = vec_add(vsumf0, vec_sld(vsumf0, vsumf0, 8));
  8066. *s = 0.25f * vec_extract(vsumf0, 0);
  8067. #elif defined(__loongarch_asx)
  8068. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  8069. uint32_t aux32[2];
  8070. __m256 accumf = (__m256)__lasx_xvldi(0);
  8071. for (int i = 0; i < nb; ++i) {
  8072. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  8073. const uint8_t * restrict q3 = x[i].qs;
  8074. const uint8_t * restrict gas = x[i].qs + QK_K/4;
  8075. const int8_t * restrict q8 = y[i].qs;
  8076. __m256i sumi1 = __lasx_xvldi(0);
  8077. __m256i sumi2 = __lasx_xvldi(0);
  8078. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  8079. const __m256i q8_1 = __lasx_xvld((const __m256i *)q8, 0); q8 += 32;
  8080. const __m256i q8_2 = __lasx_xvld((const __m256i *)q8, 0); q8 += 32;
  8081. const __m256i q2_1 = lasx_set_w(iq3xxs_grid[q3[7]], iq3xxs_grid[q3[6]], iq3xxs_grid[q3[5]], iq3xxs_grid[q3[4]],
  8082. iq3xxs_grid[q3[3]], iq3xxs_grid[q3[2]], iq3xxs_grid[q3[1]], iq3xxs_grid[q3[0]]);
  8083. q3 += 8;
  8084. const __m256i q2_2 = lasx_set_w(iq3xxs_grid[q3[7]], iq3xxs_grid[q3[6]], iq3xxs_grid[q3[5]], iq3xxs_grid[q3[4]],
  8085. iq3xxs_grid[q3[3]], iq3xxs_grid[q3[2]], iq3xxs_grid[q3[1]], iq3xxs_grid[q3[0]]);
  8086. q3 += 8;
  8087. memcpy(aux32, gas, 8); gas += 8;
  8088. const __m256i s2_1 = lasx_set_d(signs64[(aux32[0] >> 21) & 127], signs64[(aux32[0] >> 14) & 127],
  8089. signs64[(aux32[0] >> 7) & 127], signs64[(aux32[0] >> 0) & 127]);
  8090. const __m256i s2_2 = lasx_set_d(signs64[(aux32[1] >> 21) & 127], signs64[(aux32[1] >> 14) & 127],
  8091. signs64[(aux32[1] >> 7) & 127], signs64[(aux32[1] >> 0) & 127]);
  8092. const __m256i q8s_1 = __lasx_xvsigncov_b(s2_1, q8_1);
  8093. const __m256i q8s_2 = __lasx_xvsigncov_b(s2_2, q8_2);
  8094. const __m256i dot1 = lasx_maddubs_h(q2_1, q8s_1);
  8095. const __m256i dot2 = lasx_maddubs_h(q2_2, q8s_2);
  8096. const uint16_t ls1 = aux32[0] >> 28;
  8097. const uint16_t ls2 = aux32[1] >> 28;
  8098. const __m256i p1 = lasx_madd_h(dot1, __lasx_xvreplgr2vr_h(2*ls1+1));
  8099. const __m256i p2 = lasx_madd_h(dot2, __lasx_xvreplgr2vr_h(2*ls2+1));
  8100. sumi1 = __lasx_xvadd_w(sumi1, p1);
  8101. sumi2 = __lasx_xvadd_w(sumi2, p2);
  8102. }
  8103. accumf = __lasx_xvfmadd_s(__lasx_xvreplfr2vr_s(d), __lasx_xvffint_s_w(__lasx_xvadd_w(sumi1, sumi2)), accumf);
  8104. }
  8105. *s = 0.25f * hsum_float_8(accumf);
  8106. #else
  8107. uint32_t aux32;
  8108. float sumf = 0.f;
  8109. for (int i = 0; i < nb; ++i) {
  8110. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  8111. const uint8_t * restrict q3 = x[i].qs;
  8112. const uint8_t * restrict gas = x[i].qs + QK_K/4;
  8113. const int8_t * restrict q8 = y[i].qs;
  8114. int32_t bsum = 0;
  8115. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  8116. memcpy(&aux32, gas, sizeof(uint32_t)); gas += sizeof(uint32_t);
  8117. const uint32_t ls = 2*(aux32 >> 28) + 1;
  8118. int32_t sumi = 0;
  8119. for (int l = 0; l < 4; ++l) {
  8120. const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + q3[2*l+0]);
  8121. const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + q3[2*l+1]);
  8122. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*l) & 127];
  8123. for (int j = 0; j < 4; ++j) {
  8124. sumi += grid1[j] * q8[j+0] * (signs & kmask_iq2xs[j+0] ? -1 : 1);
  8125. sumi += grid2[j] * q8[j+4] * (signs & kmask_iq2xs[j+4] ? -1 : 1);
  8126. }
  8127. q8 += 8;
  8128. }
  8129. q3 += 8;
  8130. bsum += sumi * ls;
  8131. }
  8132. sumf += d * bsum;
  8133. }
  8134. *s = 0.25f * sumf;
  8135. #endif
  8136. }
  8137. void ggml_vec_dot_iq3_s_q8_K (int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  8138. assert(n % QK_K == 0);
  8139. assert(nrc == 1);
  8140. UNUSED(nrc);
  8141. UNUSED(bx);
  8142. UNUSED(by);
  8143. UNUSED(bs);
  8144. const block_iq3_s * restrict x = vx;
  8145. const block_q8_K * restrict y = vy;
  8146. const int nb = n / QK_K;
  8147. #if defined(__ARM_NEON)
  8148. typedef union {
  8149. uint16x8_t vec_index;
  8150. uint16_t index[8];
  8151. } vec_index_t;
  8152. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  8153. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  8154. };
  8155. static const uint8_t k_mask2[16] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,};
  8156. static const int16_t k_shift[8] = {8, 7, 6, 5, 4, 3, 2, 1};
  8157. const ggml_uint8x16x2_t mask1 = ggml_vld1q_u8_x2(k_mask1);
  8158. const uint8x16_t mask2 = vld1q_u8(k_mask2);
  8159. const int16x8_t hshift = vld1q_s16(k_shift);
  8160. const uint16x8_t m256 = vdupq_n_u16(256);
  8161. const uint8x16_t m1 = vdupq_n_u8(1);
  8162. uint8x16x2_t vs;
  8163. ggml_int8x16x4_t q3s;
  8164. ggml_int8x16x4_t q8b;
  8165. vec_index_t idx;
  8166. uint32_t scales32[2];
  8167. const uint8_t * scales8 = (const uint8_t *)scales32;
  8168. float sumf = 0;
  8169. for (int i = 0; i < nb; ++i) {
  8170. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  8171. const uint8_t * restrict qs = x[i].qs;
  8172. const uint8_t * restrict qh = x[i].qh;
  8173. const uint16_t * restrict signs = (const uint16_t *)x[i].signs;
  8174. const int8_t * restrict q8 = y[i].qs;
  8175. memcpy(scales32, x[i].scales, 4);
  8176. scales32[1] = (((scales32[0] >> 4) & 0x0f0f0f0f) << 1) | 0x01010101;
  8177. scales32[0] = ((scales32[0] & 0x0f0f0f0f) << 1) | 0x01010101;
  8178. int sumi1 = 0, sumi2 = 0;
  8179. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  8180. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  8181. const uint8x16_t idx_l = vld1q_u8(qs); qs += 16;
  8182. idx.vec_index = vorrq_u16(vmovl_u8(vget_low_u8 (idx_l)), vandq_u16(vshlq_u16(vdupq_n_u16(qh[ib32+0]), hshift), m256));
  8183. const uint32x4_t aux32x4_0 = ggml_vld1q_u32(iq3s_grid[idx.index[0]], iq3s_grid[idx.index[1]],
  8184. iq3s_grid[idx.index[2]], iq3s_grid[idx.index[3]]);
  8185. const uint32x4_t aux32x4_1 = ggml_vld1q_u32(iq3s_grid[idx.index[4]], iq3s_grid[idx.index[5]],
  8186. iq3s_grid[idx.index[6]], iq3s_grid[idx.index[7]]);
  8187. idx.vec_index = vorrq_u16(vmovl_u8(vget_high_u8(idx_l)), vandq_u16(vshlq_u16(vdupq_n_u16(qh[ib32+1]), hshift), m256));
  8188. const uint32x4_t aux32x4_2 = ggml_vld1q_u32(iq3s_grid[idx.index[0]], iq3s_grid[idx.index[1]],
  8189. iq3s_grid[idx.index[2]], iq3s_grid[idx.index[3]]);
  8190. const uint32x4_t aux32x4_3 = ggml_vld1q_u32(iq3s_grid[idx.index[4]], iq3s_grid[idx.index[5]],
  8191. iq3s_grid[idx.index[6]], iq3s_grid[idx.index[7]]);
  8192. vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[0] | ((uint32_t) signs[1] << 16)));
  8193. vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
  8194. vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
  8195. vs.val[0] = vorrq_u8(vceqq_u8(vs.val[0], mask2), m1);
  8196. vs.val[1] = vorrq_u8(vceqq_u8(vs.val[1], mask2), m1);
  8197. q3s.val[0] = vmulq_s8(vreinterpretq_s8_u8(vs.val[0]), vreinterpretq_s8_u32(aux32x4_0));
  8198. q3s.val[1] = vmulq_s8(vreinterpretq_s8_u8(vs.val[1]), vreinterpretq_s8_u32(aux32x4_1));
  8199. vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[2] | ((uint32_t) signs[3] << 16)));
  8200. vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
  8201. vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
  8202. vs.val[0] = vorrq_u8(vceqq_u8(vs.val[0], mask2), m1);
  8203. vs.val[1] = vorrq_u8(vceqq_u8(vs.val[1], mask2), m1);
  8204. signs += 4;
  8205. q3s.val[2] = vmulq_s8(vreinterpretq_s8_u8(vs.val[0]), vreinterpretq_s8_u32(aux32x4_2));
  8206. q3s.val[3] = vmulq_s8(vreinterpretq_s8_u8(vs.val[1]), vreinterpretq_s8_u32(aux32x4_3));
  8207. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q3s.val[0], q8b.val[0]), q3s.val[1], q8b.val[1]);
  8208. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q3s.val[2], q8b.val[2]), q3s.val[3], q8b.val[3]);
  8209. sumi1 += vaddvq_s32(p1) * scales8[ib32/2+0];
  8210. sumi2 += vaddvq_s32(p2) * scales8[ib32/2+4];
  8211. }
  8212. sumf += d*(sumi1 + sumi2);
  8213. }
  8214. *s = sumf;
  8215. #elif defined(__AVX2__)
  8216. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  8217. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  8218. };
  8219. static const uint8_t k_mask2[32] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  8220. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  8221. };
  8222. const __m256i mask1 = _mm256_loadu_si256((const __m256i*)k_mask1);
  8223. const __m256i mask2 = _mm256_loadu_si256((const __m256i*)k_mask2);
  8224. const __m256i idx_shift = _mm256_set_epi32(1, 2, 3, 4, 5, 6, 7, 8);
  8225. const __m256i idx_mask = _mm256_set1_epi32(256);
  8226. typedef union {
  8227. __m256i vec[2];
  8228. uint32_t index[16];
  8229. } index_t;
  8230. index_t idx;
  8231. __m256 accumf = _mm256_setzero_ps();
  8232. for (int i = 0; i < nb; ++i) {
  8233. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  8234. const uint8_t * restrict qs = x[i].qs;
  8235. const uint8_t * restrict qh = x[i].qh;
  8236. const uint16_t * restrict signs = (const uint16_t *)x[i].signs;
  8237. const int8_t * restrict q8 = y[i].qs;
  8238. __m256i sumi1 = _mm256_setzero_si256();
  8239. __m256i sumi2 = _mm256_setzero_si256();
  8240. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  8241. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  8242. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  8243. const __m256i idx_l = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)qs)); qs += 16;
  8244. idx.vec[0] = _mm256_set1_epi32(qh[ib32+0]);
  8245. idx.vec[1] = _mm256_set1_epi32(qh[ib32+1]);
  8246. idx.vec[0] = _mm256_and_si256(_mm256_sllv_epi32(idx.vec[0], idx_shift), idx_mask);
  8247. idx.vec[1] = _mm256_and_si256(_mm256_sllv_epi32(idx.vec[1], idx_shift), idx_mask);
  8248. idx.vec[0] = _mm256_or_si256(idx.vec[0], _mm256_cvtepi16_epi32(_mm256_castsi256_si128(idx_l)));
  8249. idx.vec[1] = _mm256_or_si256(idx.vec[1], _mm256_cvtepi16_epi32(_mm256_extractf128_si256(idx_l, 1)));
  8250. // At leat on my CPU (Ryzen 7950X), using _mm256_i32gather_epi32 is slower than _mm256_set_epi32. Strange.
  8251. //const __m256i q2_1 = _mm256_i32gather_epi32((const int *)iq3s_grid, idx.vec[0], 4);
  8252. //const __m256i q2_2 = _mm256_i32gather_epi32((const int *)iq3s_grid, idx.vec[1], 4);
  8253. const __m256i q2_1 = _mm256_set_epi32(
  8254. iq3s_grid[idx.index[7]], iq3s_grid[idx.index[6]], iq3s_grid[idx.index[5]], iq3s_grid[idx.index[4]],
  8255. iq3s_grid[idx.index[3]], iq3s_grid[idx.index[2]], iq3s_grid[idx.index[1]], iq3s_grid[idx.index[0]]
  8256. );
  8257. const __m256i q2_2 = _mm256_set_epi32(
  8258. iq3s_grid[idx.index[15]], iq3s_grid[idx.index[14]], iq3s_grid[idx.index[13]], iq3s_grid[idx.index[12]],
  8259. iq3s_grid[idx.index[11]], iq3s_grid[idx.index[10]], iq3s_grid[idx.index[ 9]], iq3s_grid[idx.index[ 8]]
  8260. );
  8261. __m256i aux256 = _mm256_set1_epi32(signs[0] | (signs[1] << 16));
  8262. aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
  8263. const __m256i s2_1 = _mm256_cmpeq_epi8(aux256, mask2);
  8264. const __m256i q8s_1 = _mm256_sub_epi8(_mm256_xor_si256(s2_1, q8_1), s2_1);
  8265. aux256 = _mm256_set1_epi32(signs[2] | (signs[3] << 16));
  8266. aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
  8267. const __m256i s2_2 = _mm256_cmpeq_epi8(aux256, mask2);
  8268. const __m256i q8s_2 = _mm256_sub_epi8(_mm256_xor_si256(s2_2, q8_2), s2_2);
  8269. signs += 4;
  8270. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  8271. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  8272. const uint16_t ls1 = x[i].scales[ib32/2] & 0xf;
  8273. const uint16_t ls2 = x[i].scales[ib32/2] >> 4;
  8274. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
  8275. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
  8276. sumi1 = _mm256_add_epi32(sumi1, p1);
  8277. sumi2 = _mm256_add_epi32(sumi2, p2);
  8278. }
  8279. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  8280. }
  8281. *s = hsum_float_8(accumf);
  8282. #elif defined(__POWER9_VECTOR__)
  8283. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  8284. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  8285. };
  8286. static const uint8_t k_mask2[16] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,};
  8287. const vector int v0 = vec_splats((int32_t)0);
  8288. vector float vsumf0 = vec_splats(0.0f);
  8289. vector float vsumf1 = vec_splats(0.0f);
  8290. vector float vsumf2 = vec_splats(0.0f);
  8291. vector float vsumf3 = vec_splats(0.0f);
  8292. const vector unsigned char mask0 = vec_xl( 0, k_mask1);
  8293. const vector unsigned char mask1 = vec_xl(16, k_mask1);
  8294. const vector signed char mask2 = (vector signed char)vec_xl( 0, k_mask2);
  8295. for (int i = 0; i < nb; ++i) {
  8296. vector float vxd = vec_splats(GGML_FP16_TO_FP32(x[i].d));
  8297. vector float vyd = vec_splats(y[i].d);
  8298. vector float vd = vec_mul(vxd, vyd);
  8299. const uint8_t * restrict q3 = x[i].qs;
  8300. const uint8_t * restrict qh = x[i].qh;
  8301. const uint16_t * restrict signs = (const uint16_t *)(x[i].signs);
  8302. const uint8_t * restrict sc = x[i].scales;
  8303. const int8_t * restrict q8 = y[i].qs;
  8304. vector signed int vsumi0 = v0;
  8305. vector signed int vsumi1 = v0;
  8306. vector signed int vsumi2 = v0;
  8307. vector signed int vsumi3 = v0;
  8308. for (int j = 0; j < QK_K/32; j += 2) {
  8309. __builtin_prefetch(q3, 0, 1);
  8310. __builtin_prefetch(q8, 0, 1);
  8311. vector unsigned int aux32x4_0 = {iq3s_grid[q3[ 0] | ((qh[0] << 8) & 256)], iq3s_grid[q3[ 1] | ((qh[0] << 7) & 256)],
  8312. iq3s_grid[q3[ 2] | ((qh[0] << 6) & 256)], iq3s_grid[q3[ 3] | ((qh[0] << 5) & 256)]};
  8313. vector unsigned int aux32x4_1 = {iq3s_grid[q3[ 4] | ((qh[0] << 4) & 256)], iq3s_grid[q3[ 5] | ((qh[0] << 3) & 256)],
  8314. iq3s_grid[q3[ 6] | ((qh[0] << 2) & 256)], iq3s_grid[q3[ 7] | ((qh[0] << 1) & 256)]};
  8315. vector unsigned int aux32x4_2 = {iq3s_grid[q3[ 8] | ((qh[1] << 8) & 256)], iq3s_grid[q3[ 9] | ((qh[1] << 7) & 256)],
  8316. iq3s_grid[q3[10] | ((qh[1] << 6) & 256)], iq3s_grid[q3[11] | ((qh[1] << 5) & 256)]};
  8317. vector unsigned int aux32x4_3 = {iq3s_grid[q3[12] | ((qh[1] << 4) & 256)], iq3s_grid[q3[13] | ((qh[1] << 3) & 256)],
  8318. iq3s_grid[q3[14] | ((qh[1] << 2) & 256)], iq3s_grid[q3[15] | ((qh[1] << 1) & 256)]};
  8319. q3 += 16;
  8320. qh += 2;
  8321. vector signed char vsigns01 = (vector signed char)vec_splats(*(const uint32_t *)&signs[0]);
  8322. vector signed char vsigns02 = (vector signed char)vec_splats(*(const uint32_t *)&signs[2]);
  8323. signs += 4;
  8324. vector signed char vsigns0 = vec_perm(vsigns01, vsigns01, mask0);
  8325. vector signed char vsigns1 = vec_perm(vsigns01, vsigns01, mask1);
  8326. vector signed char vsigns2 = vec_perm(vsigns02, vsigns02, mask0);
  8327. vector signed char vsigns3 = vec_perm(vsigns02, vsigns02, mask1);
  8328. vsigns0 = (vector signed char)vec_cmpeq(vec_and(vsigns0, mask2), mask2);
  8329. vsigns1 = (vector signed char)vec_cmpeq(vec_and(vsigns1, mask2), mask2);
  8330. vsigns2 = (vector signed char)vec_cmpeq(vec_and(vsigns2, mask2), mask2);
  8331. vsigns3 = (vector signed char)vec_cmpeq(vec_and(vsigns3, mask2), mask2);
  8332. vector signed char q3x0 = vec_sub(vec_xor(vsigns0, (vector signed char)aux32x4_0), vsigns0);
  8333. vector signed char q3x1 = vec_sub(vec_xor(vsigns1, (vector signed char)aux32x4_1), vsigns1);
  8334. vector signed char q3x2 = vec_sub(vec_xor(vsigns2, (vector signed char)aux32x4_2), vsigns2);
  8335. vector signed char q3x3 = vec_sub(vec_xor(vsigns3, (vector signed char)aux32x4_3), vsigns3);
  8336. vector signed char q8y0 = vec_xl( 0, q8);
  8337. vector signed char q8y1 = vec_xl(16, q8);
  8338. vector signed char q8y2 = vec_xl(32, q8);
  8339. vector signed char q8y3 = vec_xl(48, q8);
  8340. q8 += 64;
  8341. vector signed short qv0 = vec_add(vec_mule(q3x0, q8y0), vec_mulo(q3x0, q8y0));
  8342. vector signed short qv1 = vec_add(vec_mule(q3x1, q8y1), vec_mulo(q3x1, q8y1));
  8343. vector signed short qv2 = vec_add(vec_mule(q3x2, q8y2), vec_mulo(q3x2, q8y2));
  8344. vector signed short qv3 = vec_add(vec_mule(q3x3, q8y3), vec_mulo(q3x3, q8y3));
  8345. const uint16_t ls0 = (uint16_t)(sc[0] & 0xf);
  8346. const uint16_t ls1 = (uint16_t)(sc[0] >> 4);
  8347. sc ++;
  8348. vector signed short vscales01 = (vector signed short)vec_splats((uint16_t)(2*ls0+1));
  8349. vector signed short vscales23 = (vector signed short)vec_splats((uint16_t)(2*ls1+1));
  8350. vsumi0 = vec_msum(qv0, vscales01, vsumi0);
  8351. vsumi1 = vec_msum(qv1, vscales01, vsumi1);
  8352. vsumi2 = vec_msum(qv2, vscales23, vsumi2);
  8353. vsumi3 = vec_msum(qv3, vscales23, vsumi3);
  8354. }
  8355. vsumf0 = vec_madd(vec_ctf(vsumi0, 0), vd, vsumf0);
  8356. vsumf1 = vec_madd(vec_ctf(vsumi1, 0), vd, vsumf1);
  8357. vsumf2 = vec_madd(vec_ctf(vsumi2, 0), vd, vsumf2);
  8358. vsumf3 = vec_madd(vec_ctf(vsumi3, 0), vd, vsumf3);
  8359. }
  8360. vsumf0 = vec_add(vsumf0, vsumf2);
  8361. vsumf1 = vec_add(vsumf1, vsumf3);
  8362. vsumf0 = vec_add(vsumf0, vsumf1);
  8363. vsumf0 = vec_add(vsumf0, vec_sld(vsumf0, vsumf0, 4));
  8364. vsumf0 = vec_add(vsumf0, vec_sld(vsumf0, vsumf0, 8));
  8365. *s = vec_extract(vsumf0, 0);
  8366. #elif defined(__loongarch_asx)
  8367. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  8368. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  8369. };
  8370. static const uint8_t k_mask2[32] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  8371. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  8372. };
  8373. const __m256i mask1 = __lasx_xvld((const __m256i*)k_mask1, 0);
  8374. const __m256i mask2 = __lasx_xvld((const __m256i*)k_mask2, 0);
  8375. __m256i idx_shift = lasx_set_w(1, 2, 3, 4, 5, 6, 7, 8);
  8376. const __m256i idx_mask = __lasx_xvreplgr2vr_w(256);
  8377. typedef union {
  8378. __m256i vec[2];
  8379. uint32_t index[16];
  8380. } index_t;
  8381. index_t idx;
  8382. __m256 accumf = (__m256)__lasx_xvldi(0);
  8383. for (int i = 0; i < nb; ++i) {
  8384. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  8385. const uint8_t * restrict qs = x[i].qs;
  8386. const uint8_t * restrict qh = x[i].qh;
  8387. const uint16_t * restrict signs = (const uint16_t *)x[i].signs;
  8388. const int8_t * restrict q8 = y[i].qs;
  8389. __m256i sumi1 = __lasx_xvldi(0);
  8390. __m256i sumi2 = __lasx_xvldi(0);
  8391. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  8392. const __m256i q8_1 = __lasx_xvld((const __m256i *)q8, 0); q8 += 32;
  8393. const __m256i q8_2 = __lasx_xvld((const __m256i *)q8, 0); q8 += 32;
  8394. const __m256i idx_l = lasx_extu8_16(__lsx_vld(qs, 0)); qs += 16;
  8395. idx.vec[0] = __lasx_xvreplgr2vr_w(qh[ib32+0]);
  8396. idx.vec[1] = __lasx_xvreplgr2vr_w(qh[ib32+1]);
  8397. idx.vec[0] = __lasx_xvand_v(__lasx_xvsll_w(idx.vec[0], idx_shift), idx_mask);
  8398. idx.vec[1] = __lasx_xvand_v(__lasx_xvsll_w(idx.vec[1], idx_shift), idx_mask);
  8399. idx.vec[0] = __lasx_xvor_v(idx.vec[0], lasx_ext16_32(lasx_extracti128(idx_l, 0)));
  8400. idx.vec[1] = __lasx_xvor_v(idx.vec[1], lasx_ext16_32(lasx_extracti128(idx_l, 1)));
  8401. // At leat on my CPU (Ryzen 7950X), using _mm256_i32gather_epi32 is slower than _mm256_set_epi32. Strange.
  8402. //const __m256i q2_1 = _mm256_i32gather_epi32((const int *)iq3s_grid, idx.vec[0], 4);
  8403. //const __m256i q2_2 = _mm256_i32gather_epi32((const int *)iq3s_grid, idx.vec[1], 4);
  8404. const __m256i q2_1 = lasx_set_w(
  8405. iq3s_grid[idx.index[7]], iq3s_grid[idx.index[6]], iq3s_grid[idx.index[5]], iq3s_grid[idx.index[4]],
  8406. iq3s_grid[idx.index[3]], iq3s_grid[idx.index[2]], iq3s_grid[idx.index[1]], iq3s_grid[idx.index[0]]
  8407. );
  8408. const __m256i q2_2 = lasx_set_w(
  8409. iq3s_grid[idx.index[15]], iq3s_grid[idx.index[14]], iq3s_grid[idx.index[13]], iq3s_grid[idx.index[12]],
  8410. iq3s_grid[idx.index[11]], iq3s_grid[idx.index[10]], iq3s_grid[idx.index[ 9]], iq3s_grid[idx.index[ 8]]
  8411. );
  8412. __m256i aux256 = __lasx_xvreplgr2vr_w(signs[0] | (signs[1] << 16));
  8413. aux256 = __lasx_xvand_v(lasx_shuffle_b(aux256,mask1), mask2);
  8414. const __m256i s2_1 = __lasx_xvseq_b(aux256, mask2);
  8415. const __m256i q8s_1 = __lasx_xvsub_b(__lasx_xvxor_v(s2_1, q8_1), s2_1);
  8416. aux256 = __lasx_xvreplgr2vr_w(signs[2] | (signs[3] << 16));
  8417. aux256 = __lasx_xvand_v(lasx_shuffle_b(aux256,mask1), mask2);
  8418. const __m256i s2_2 = __lasx_xvseq_b(aux256, mask2);
  8419. const __m256i q8s_2 = __lasx_xvsub_b(__lasx_xvxor_v(s2_2, q8_2), s2_2);
  8420. signs += 4;
  8421. const __m256i dot1 = lasx_maddubs_h(q2_1, q8s_1);
  8422. const __m256i dot2 = lasx_maddubs_h(q2_2, q8s_2);
  8423. const uint16_t ls1 = x[i].scales[ib32/2] & 0xf;
  8424. const uint16_t ls2 = x[i].scales[ib32/2] >> 4;
  8425. const __m256i p1 = lasx_madd_h(dot1, __lasx_xvreplgr2vr_h(2*ls1+1));
  8426. const __m256i p2 = lasx_madd_h(dot2, __lasx_xvreplgr2vr_h(2*ls2+1));
  8427. sumi1 = __lasx_xvadd_w(sumi1, p1);
  8428. sumi2 = __lasx_xvadd_w(sumi2, p2);
  8429. }
  8430. accumf = __lasx_xvfmadd_s(__lasx_xvreplfr2vr_s(d), __lasx_xvffint_s_w(__lasx_xvadd_w(sumi1, sumi2)), accumf);
  8431. }
  8432. *s = hsum_float_8(accumf);
  8433. #else
  8434. float sumf = 0.f;
  8435. for (int i = 0; i < nb; ++i) {
  8436. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  8437. const uint8_t * restrict qs = x[i].qs;
  8438. const uint8_t * restrict qh = x[i].qh;
  8439. const uint8_t * restrict signs = x[i].signs;
  8440. const int8_t * restrict q8 = y[i].qs;
  8441. int32_t bsum = 0;
  8442. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  8443. const uint32_t ls1 = 2*(x[i].scales[ib32/2] & 0xf) + 1;
  8444. const uint32_t ls2 = 2*(x[i].scales[ib32/2] >> 4) + 1;
  8445. int32_t sumi = 0;
  8446. for (int l = 0; l < 4; ++l) {
  8447. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[ib32+0] << (8-2*l)) & 256)));
  8448. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[ib32+0] << (7-2*l)) & 256)));
  8449. for (int j = 0; j < 4; ++j) {
  8450. sumi += grid1[j] * q8[j+0] * (signs[l] & kmask_iq2xs[j+0] ? -1 : 1);
  8451. sumi += grid2[j] * q8[j+4] * (signs[l] & kmask_iq2xs[j+4] ? -1 : 1);
  8452. }
  8453. q8 += 8;
  8454. }
  8455. qs += 8;
  8456. signs += 4;
  8457. bsum += sumi * ls1;
  8458. sumi = 0;
  8459. for (int l = 0; l < 4; ++l) {
  8460. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[ib32+1] << (8-2*l)) & 256)));
  8461. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[ib32+1] << (7-2*l)) & 256)));
  8462. for (int j = 0; j < 4; ++j) {
  8463. sumi += grid1[j] * q8[j+0] * (signs[l] & kmask_iq2xs[j+0] ? -1 : 1);
  8464. sumi += grid2[j] * q8[j+4] * (signs[l] & kmask_iq2xs[j+4] ? -1 : 1);
  8465. }
  8466. q8 += 8;
  8467. }
  8468. qs += 8;
  8469. signs += 4;
  8470. bsum += sumi * ls2;
  8471. }
  8472. sumf += d * bsum;
  8473. }
  8474. *s = sumf;
  8475. #endif
  8476. }
  8477. #if defined(__AVX2__)
  8478. static inline __m256i mul_add_epi8(const __m256i x, const __m256i y) {
  8479. const __m256i ax = _mm256_sign_epi8(x, x);
  8480. const __m256i sy = _mm256_sign_epi8(y, x);
  8481. return _mm256_maddubs_epi16(ax, sy);
  8482. }
  8483. #elif defined(__loongarch_asx)
  8484. static inline __m256i mul_add_epi8(const __m256i x, const __m256i y) {
  8485. const __m256i ax = __lasx_xvsigncov_b(x, x);
  8486. const __m256i sy = __lasx_xvsigncov_b(x, y);
  8487. __m256i tmp1, tmp2, tmp3;
  8488. tmp1 = __lasx_xvmulwev_h_bu_b(ax, sy);
  8489. tmp2 = __lasx_xvmulwod_h_bu_b(ax, sy);
  8490. tmp3 = __lasx_xvadd_h(tmp1, tmp2);
  8491. return __lasx_xvsat_h(tmp3, 15);
  8492. }
  8493. #endif
  8494. void ggml_vec_dot_iq1_s_q8_K (int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  8495. assert(n % QK_K == 0);
  8496. assert(nrc == 1);
  8497. UNUSED(nrc);
  8498. UNUSED(bx);
  8499. UNUSED(by);
  8500. UNUSED(bs);
  8501. const block_iq1_s * restrict x = vx;
  8502. const block_q8_K * restrict y = vy;
  8503. const int nb = n / QK_K;
  8504. #if defined __ARM_NEON
  8505. ggml_int8x16x4_t q1b;
  8506. ggml_int8x16x4_t q8b;
  8507. float sumf = 0;
  8508. for (int i = 0; i < nb; ++i) {
  8509. const int8_t * q8 = y[i].qs;
  8510. const uint8_t * qs = x[i].qs;
  8511. const uint16_t * qh = x[i].qh;
  8512. int sumi1 = 0, sumi2 = 0, sumi3 = 0;
  8513. for (int ib = 0; ib < QK_K/32; ib += 2) {
  8514. q1b.val[0] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[0] | ((qh[ib+0] << 8) & 0x700)))),
  8515. vld1_s8((const int8_t *)(iq1s_grid + (qs[1] | ((qh[ib+0] << 5) & 0x700)))));
  8516. q1b.val[1] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[2] | ((qh[ib+0] << 2) & 0x700)))),
  8517. vld1_s8((const int8_t *)(iq1s_grid + (qs[3] | ((qh[ib+0] >> 1) & 0x700)))));
  8518. q1b.val[2] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[4] | ((qh[ib+1] << 8) & 0x700)))),
  8519. vld1_s8((const int8_t *)(iq1s_grid + (qs[5] | ((qh[ib+1] << 5) & 0x700)))));
  8520. q1b.val[3] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[6] | ((qh[ib+1] << 2) & 0x700)))),
  8521. vld1_s8((const int8_t *)(iq1s_grid + (qs[7] | ((qh[ib+1] >> 1) & 0x700)))));
  8522. qs += 8;
  8523. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  8524. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q1b.val[0], q8b.val[0]), q1b.val[1], q8b.val[1]);
  8525. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q1b.val[2], q8b.val[2]), q1b.val[3], q8b.val[3]);
  8526. const int ls1 = 2*((qh[ib+0] >> 12) & 7) + 1;
  8527. const int ls2 = 2*((qh[ib+1] >> 12) & 7) + 1;
  8528. sumi1 += vaddvq_s32(p1) * ls1;
  8529. sumi2 += vaddvq_s32(p2) * ls2;
  8530. sumi3 += (y[i].bsums[2*ib+0] + y[i].bsums[2*ib+1]) * ls1 * (qh[ib+0] & 0x8000 ? -1 : 1)
  8531. + (y[i].bsums[2*ib+2] + y[i].bsums[2*ib+3]) * ls2 * (qh[ib+1] & 0x8000 ? -1 : 1);
  8532. }
  8533. sumf += y[i].d * GGML_FP16_TO_FP32(x[i].d) * (sumi1 + sumi2 + IQ1S_DELTA * sumi3);
  8534. }
  8535. *s = sumf;
  8536. #elif defined __AVX2__
  8537. __m256 accum = _mm256_setzero_ps();
  8538. float accum1 = 0;
  8539. for (int i = 0; i < nb; ++i) {
  8540. const int8_t * q8 = y[i].qs;
  8541. const uint8_t * qs = x[i].qs;
  8542. const uint16_t * qh = x[i].qh;
  8543. __m256i sumi = _mm256_setzero_si256();
  8544. int sumi1 = 0;
  8545. for (int ib = 0; ib < QK_K/32; ib += 2) {
  8546. const __m256i q1b_1 = _mm256_set_epi64x(iq1s_grid[qs[3] | ((qh[ib+0] >> 1) & 0x700)], iq1s_grid[qs[2] | ((qh[ib+0] << 2) & 0x700)],
  8547. iq1s_grid[qs[1] | ((qh[ib+0] << 5) & 0x700)], iq1s_grid[qs[0] | ((qh[ib+0] << 8) & 0x700)]);
  8548. const __m256i q1b_2 = _mm256_set_epi64x(iq1s_grid[qs[7] | ((qh[ib+1] >> 1) & 0x700)], iq1s_grid[qs[6] | ((qh[ib+1] << 2) & 0x700)],
  8549. iq1s_grid[qs[5] | ((qh[ib+1] << 5) & 0x700)], iq1s_grid[qs[4] | ((qh[ib+1] << 8) & 0x700)]);
  8550. qs += 8;
  8551. const __m256i q8b_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  8552. const __m256i q8b_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  8553. const __m256i dot1 = mul_add_epi8(q1b_1, q8b_1);
  8554. const __m256i dot2 = mul_add_epi8(q1b_2, q8b_2);
  8555. const int16_t ls1 = 2*((qh[ib+0] >> 12) & 7) + 1;
  8556. const int16_t ls2 = 2*((qh[ib+1] >> 12) & 7) + 1;
  8557. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(ls1));
  8558. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(ls2));
  8559. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p1, p2));
  8560. sumi1 += (y[i].bsums[2*ib+0] + y[i].bsums[2*ib+1]) * (qh[ib+0] & 0x8000 ? -1 : 1) * ls1
  8561. + (y[i].bsums[2*ib+2] + y[i].bsums[2*ib+3]) * (qh[ib+1] & 0x8000 ? -1 : 1) * ls2;
  8562. }
  8563. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  8564. accum = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(sumi), accum);
  8565. accum1 += d * sumi1;
  8566. }
  8567. *s = hsum_float_8(accum) + IQ1S_DELTA * accum1;
  8568. #elif defined(__POWER9_VECTOR__)
  8569. const vector unsigned char v0 = vec_splats((unsigned char)0x0);
  8570. const vector unsigned short vsign = vec_splats((unsigned short)0x8000);
  8571. vector float vsumf0 = vec_splats(0.0f);
  8572. vector float vsumf1 = vec_splats(0.0f);
  8573. vector float vsumf2 = vec_splats(0.0f);
  8574. vector float vsumf3 = vec_splats(0.0f);
  8575. for (int i = 0; i < nb; ++i) {
  8576. vector float vxd = vec_splats(GGML_FP16_TO_FP32(x[i].d));
  8577. vector float vyd = vec_splats(y[i].d);
  8578. vector float vd = vec_mul(vxd, vyd);
  8579. vector signed int vsumi0 = vec_splats((int32_t)0);
  8580. vector signed int vsumi1 = vec_splats((int32_t)0);
  8581. vector signed int vsumi2 = vec_splats((int32_t)0);
  8582. vector signed int vsumi3 = vec_splats((int32_t)0);
  8583. vector signed int vsumi8 = vec_splats((int32_t)0);
  8584. const uint8_t * restrict q1 = x[i].qs;
  8585. const uint16_t * restrict qh = x[i].qh;
  8586. const int8_t * restrict q8 = y[i].qs;
  8587. const int16_t * restrict qs = y[i].bsums;
  8588. for (int j = 0; j < QK_K/32; j += 2) {
  8589. __builtin_prefetch(q1, 0, 1);
  8590. __builtin_prefetch(qh, 0, 1);
  8591. __builtin_prefetch(q8, 0, 1);
  8592. vector signed long long aux64x2_0 = {*(const int64_t *)(iq1s_grid + (q1[0] | ((qh[0] << 8) & 0x700))), *(const int64_t *)(iq1s_grid + (q1[1] | ((qh[0] << 5) & 0x700)))};
  8593. vector signed long long aux64x2_1 = {*(const int64_t *)(iq1s_grid + (q1[2] | ((qh[0] << 2) & 0x700))), *(const int64_t *)(iq1s_grid + (q1[3] | ((qh[0] >> 1) & 0x700)))};
  8594. vector signed long long aux64x2_2 = {*(const int64_t *)(iq1s_grid + (q1[4] | ((qh[1] << 8) & 0x700))), *(const int64_t *)(iq1s_grid + (q1[5] | ((qh[1] << 5) & 0x700)))};
  8595. vector signed long long aux64x2_3 = {*(const int64_t *)(iq1s_grid + (q1[6] | ((qh[1] << 2) & 0x700))), *(const int64_t *)(iq1s_grid + (q1[7] | ((qh[1] >> 1) & 0x700)))};
  8596. q1 += 8;
  8597. vector signed char q1x0 = (vector signed char)aux64x2_0;
  8598. vector signed char q1x1 = (vector signed char)aux64x2_1;
  8599. vector signed char q1x2 = (vector signed char)aux64x2_2;
  8600. vector signed char q1x3 = (vector signed char)aux64x2_3;
  8601. vector signed char q8y0 = vec_xl( 0, q8);
  8602. vector signed char q8y1 = vec_xl(16, q8);
  8603. vector signed char q8y2 = vec_xl(32, q8);
  8604. vector signed char q8y3 = vec_xl(48, q8);
  8605. q8 += 64;
  8606. vector signed short qv0 = vec_add(vec_mule(q1x0, q8y0), vec_mulo(q1x0, q8y0));
  8607. vector signed short qv1 = vec_add(vec_mule(q1x1, q8y1), vec_mulo(q1x1, q8y1));
  8608. vector signed short qv2 = vec_add(vec_mule(q1x2, q8y2), vec_mulo(q1x2, q8y2));
  8609. vector signed short qv3 = vec_add(vec_mule(q1x3, q8y3), vec_mulo(q1x3, q8y3));
  8610. const uint16_t ls0 = (uint16_t)((qh[0] >> 12) & 7);
  8611. const uint16_t ls1 = (uint16_t)((qh[1] >> 12) & 7);
  8612. vector signed short vscales01 = (vector signed short)vec_splats((uint16_t)(2*ls0+1));
  8613. vector signed short vscales23 = (vector signed short)vec_splats((uint16_t)(2*ls1+1));
  8614. vector signed short vscales = vec_sld(vscales23, vscales01, 8);
  8615. vsumi0 = vec_msum(qv0, vscales01, vsumi0);
  8616. vsumi1 = vec_msum(qv1, vscales01, vsumi1);
  8617. vsumi2 = vec_msum(qv2, vscales23, vsumi2);
  8618. vsumi3 = vec_msum(qv3, vscales23, vsumi3);
  8619. vector signed short q8ysums = vec_xl_len(qs, 8);
  8620. qs += 4;
  8621. q8ysums = vec_mergeh(q8ysums, (vector signed short)v0);
  8622. vector signed short qxh = (vector signed short)vec_sld(vec_splats(qh[1]), vec_splats(qh[0]), 8);
  8623. qh += 2;
  8624. vector __bool short vsel = vec_cmpge(qxh, (vector signed short)v0);
  8625. vector signed short q8ysum = vec_sel((vector signed short)vec_xor((vector unsigned short)q8ysums, vsign), q8ysums, vsel);
  8626. vsumi8 = vec_add(vec_mule(q8ysum, vscales), vsumi8);
  8627. }
  8628. vsumf0 = vec_madd(vec_ctf(vsumi0, 0), vd, vsumf0);
  8629. vsumf1 = vec_madd(vec_ctf(vsumi1, 0), vd, vsumf1);
  8630. vsumf2 = vec_madd(vec_ctf(vsumi2, 0), vd, vsumf2);
  8631. vsumf3 = vec_madd(vec_ctf(vsumi3, 0), vd, vsumf3);
  8632. vsumf0 = vec_madd(vec_ctf(vsumi8, 0), vec_mul(vd, vec_splats(IQ1S_DELTA)), vsumf0);
  8633. }
  8634. vsumf0 = vec_add(vsumf0, vsumf2);
  8635. vsumf1 = vec_add(vsumf1, vsumf3);
  8636. vsumf0 = vec_add(vsumf0, vsumf1);
  8637. vsumf0 = vec_add(vsumf0, vec_sld(vsumf0, vsumf0, 4));
  8638. vsumf0 = vec_add(vsumf0, vec_sld(vsumf0, vsumf0, 8));
  8639. *s = vec_extract(vsumf0, 0);
  8640. #elif defined(__loongarch_asx)
  8641. __m256 accum = (__m256)__lasx_xvldi(0);
  8642. float accum1 = 0;
  8643. for (int i = 0; i < nb; ++i) {
  8644. const int8_t * q8 = y[i].qs;
  8645. const uint8_t * qs = x[i].qs;
  8646. const uint16_t * qh = x[i].qh;
  8647. __m256i sumi = __lasx_xvldi(0);
  8648. int sumi1 = 0;
  8649. for (int ib = 0; ib < QK_K/32; ib += 2) {
  8650. __m256i q1b_1 = __lasx_xvinsgr2vr_d(q1b_1, iq1s_grid[qs[0] | ((qh[ib+0] << 8) & 0x700)], 0);
  8651. q1b_1 = __lasx_xvinsgr2vr_d(q1b_1, iq1s_grid[qs[1] | ((qh[ib+0] << 5) & 0x700)], 1);
  8652. q1b_1 = __lasx_xvinsgr2vr_d(q1b_1, iq1s_grid[qs[2] | ((qh[ib+0] << 2) & 0x700)], 2);
  8653. q1b_1 = __lasx_xvinsgr2vr_d(q1b_1, iq1s_grid[qs[3] | ((qh[ib+0] >> 1) & 0x700)], 3);
  8654. __m256i q1b_2 = __lasx_xvinsgr2vr_d(q1b_2, iq1s_grid[qs[4] | ((qh[ib+1] << 8) & 0x700)], 0);
  8655. q1b_2 = __lasx_xvinsgr2vr_d(q1b_2, iq1s_grid[qs[5] | ((qh[ib+1] << 5) & 0x700)], 1);
  8656. q1b_2 = __lasx_xvinsgr2vr_d(q1b_2, iq1s_grid[qs[6] | ((qh[ib+1] << 2) & 0x700)], 2);
  8657. q1b_2 = __lasx_xvinsgr2vr_d(q1b_2, iq1s_grid[qs[7] | ((qh[ib+1] >> 1) & 0x700)], 3);
  8658. qs += 8;
  8659. const __m256i q8b_1 = __lasx_xvld((const __m256i*)q8, 0); q8 += 32;
  8660. const __m256i q8b_2 = __lasx_xvld((const __m256i*)q8, 0); q8 += 32;
  8661. const __m256i dot1 = mul_add_epi8(q1b_1, q8b_1);
  8662. const __m256i dot2 = mul_add_epi8(q1b_2, q8b_2);
  8663. const int16_t ls1 = 2*((qh[ib+0] >> 12) & 7) + 1;
  8664. const int16_t ls2 = 2*((qh[ib+1] >> 12) & 7) + 1;
  8665. __m256i tmp1, tmp5, tmp6;
  8666. tmp1 = __lasx_xvreplgr2vr_h(ls1);
  8667. tmp5 = __lasx_xvmulwev_w_h(dot1, tmp1);
  8668. tmp6 = __lasx_xvmulwod_w_h(dot1, tmp1);
  8669. const __m256i p1 = __lasx_xvadd_w(tmp5, tmp6);
  8670. tmp1 = __lasx_xvreplgr2vr_h(ls2);
  8671. tmp5 = __lasx_xvmulwev_w_h(dot2, tmp1);
  8672. tmp6 = __lasx_xvmulwod_w_h(dot2, tmp1);
  8673. const __m256i p2 = __lasx_xvadd_w(tmp5, tmp6);
  8674. sumi = __lasx_xvadd_w(sumi, __lasx_xvadd_w(p1, p2));
  8675. sumi1 += (y[i].bsums[2*ib+0] + y[i].bsums[2*ib+1]) * (qh[ib+0] & 0x8000 ? -1 : 1) * ls1
  8676. + (y[i].bsums[2*ib+2] + y[i].bsums[2*ib+3]) * (qh[ib+1] & 0x8000 ? -1 : 1) * ls2;
  8677. }
  8678. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  8679. accum = __lasx_xvfmadd_s(__lasx_xvreplfr2vr_s(d), __lasx_xvffint_s_w(sumi), accum);
  8680. accum1 += d * sumi1;
  8681. }
  8682. *s = hsum_float_8(accum) + IQ1S_DELTA * accum1;
  8683. #else
  8684. float sumf = 0;
  8685. for (int i = 0; i < nb; i++) {
  8686. const int8_t * q8 = y[i].qs;
  8687. const uint8_t * qs = x[i].qs;
  8688. const uint16_t * qh = x[i].qh;
  8689. int sumi = 0, sumi1 = 0;
  8690. for (int ib = 0; ib < QK_K/32; ++ib) {
  8691. const int ls = 2*((qh[ib] >> 12) & 7) + 1;
  8692. const int delta = qh[ib] & 0x8000 ? -1 : 1;
  8693. int lsum = 0;
  8694. for (int l = 0; l < 4; ++l) {
  8695. const int8_t * grid = (const int8_t *)(iq1s_grid + (qs[l] | (((qh[ib] >> 3*l) & 7) << 8)));
  8696. for (int j = 0; j < 8; ++j) {
  8697. lsum += q8[j] * grid[j];
  8698. }
  8699. q8 += 8;
  8700. }
  8701. sumi += ls * lsum;
  8702. sumi1 += ls * delta * (y[i].bsums[2*ib+0] + y[i].bsums[2*ib+1]);
  8703. qs += 4;
  8704. }
  8705. sumf += GGML_FP16_TO_FP32(x[i].d) * y[i].d * (sumi + IQ1S_DELTA * sumi1);
  8706. }
  8707. *s = sumf;
  8708. #endif
  8709. }
  8710. void ggml_vec_dot_iq1_m_q8_K (int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  8711. assert(n % QK_K == 0);
  8712. assert(nrc == 1);
  8713. UNUSED(nrc);
  8714. UNUSED(bx);
  8715. UNUSED(by);
  8716. UNUSED(bs);
  8717. const block_iq1_m * restrict x = vx;
  8718. const block_q8_K * restrict y = vy;
  8719. const int nb = n / QK_K;
  8720. iq1m_scale_t scale;
  8721. #if defined __ARM_NEON
  8722. const int32x4_t mask = vdupq_n_s32(0x7);
  8723. const int32x4_t mone = vdupq_n_s32(1);
  8724. const int32x4_t mzero = vdupq_n_s32(0);
  8725. ggml_int8x16x4_t deltas;
  8726. deltas.val[0] = vcombine_s8(vdup_n_s8(+1), vdup_n_s8(+1));
  8727. deltas.val[1] = vcombine_s8(vdup_n_s8(-1), vdup_n_s8(+1));
  8728. deltas.val[2] = vcombine_s8(vdup_n_s8(+1), vdup_n_s8(-1));
  8729. deltas.val[3] = vcombine_s8(vdup_n_s8(-1), vdup_n_s8(-1));
  8730. ggml_int8x16x4_t q1b;
  8731. ggml_int8x16x4_t q8b;
  8732. uint32_t aux32;
  8733. const uint8_t * aux8 = (const uint8_t *)&aux32;
  8734. float sumf = 0;
  8735. for (int i = 0; i < nb; ++i) {
  8736. const int8_t * q8 = y[i].qs;
  8737. const uint8_t * qs = x[i].qs;
  8738. const uint8_t * qh = x[i].qh;
  8739. const uint16_t * sc = (const uint16_t *)x[i].scales;
  8740. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  8741. int32x4_t sumi1 = mzero;
  8742. int32x4_t sumi2 = mzero;
  8743. for (int ib = 0; ib < QK_K/32; ib += 2) {
  8744. q1b.val[0] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[0] | ((qh[0] << 8) & 0x700)))),
  8745. vld1_s8((const int8_t *)(iq1s_grid + (qs[1] | ((qh[0] << 4) & 0x700)))));
  8746. q1b.val[1] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[2] | ((qh[1] << 8) & 0x700)))),
  8747. vld1_s8((const int8_t *)(iq1s_grid + (qs[3] | ((qh[1] << 4) & 0x700)))));
  8748. q1b.val[2] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[4] | ((qh[2] << 8) & 0x700)))),
  8749. vld1_s8((const int8_t *)(iq1s_grid + (qs[5] | ((qh[2] << 4) & 0x700)))));
  8750. q1b.val[3] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[6] | ((qh[3] << 8) & 0x700)))),
  8751. vld1_s8((const int8_t *)(iq1s_grid + (qs[7] | ((qh[3] << 4) & 0x700)))));
  8752. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  8753. const int32x4_t p1 = vpaddq_s32(ggml_vdotq_s32(mzero, q1b.val[0], q8b.val[0]), ggml_vdotq_s32(mzero, q1b.val[1], q8b.val[1]));
  8754. const int32x4_t p2 = vpaddq_s32(ggml_vdotq_s32(mzero, q1b.val[2], q8b.val[2]), ggml_vdotq_s32(mzero, q1b.val[3], q8b.val[3]));
  8755. const int32x4_t p12 = vpaddq_s32(p1, p2);
  8756. const uint32_t * qh32 = (const uint32_t *)qh; // we are 4-byte aligned, so we can do that
  8757. aux32 = ((qh32[0] >> 3) & 0x01010101) | ((qh32[0] >> 6) & 0x02020202);
  8758. const int32x4_t p3 = vpaddq_s32(ggml_vdotq_s32(mzero, deltas.val[aux8[0]], q8b.val[0]), ggml_vdotq_s32(mzero, deltas.val[aux8[1]], q8b.val[1]));
  8759. const int32x4_t p4 = vpaddq_s32(ggml_vdotq_s32(mzero, deltas.val[aux8[2]], q8b.val[2]), ggml_vdotq_s32(mzero, deltas.val[aux8[3]], q8b.val[3]));
  8760. const int32x4_t p34 = vpaddq_s32(p3, p4);
  8761. int32x4_t scales_4 = ggml_vld1q_u32(sc[ib/2] >> 0, sc[ib/2] >> 3, sc[ib/2] >> 6, sc[ib/2] >> 9);
  8762. scales_4 = vaddq_s32(vshlq_n_s32(vandq_s32(scales_4, mask), 1), mone);
  8763. sumi1 = vmlaq_s32(sumi1, scales_4, p12);
  8764. sumi2 = vmlaq_s32(sumi2, scales_4, p34);
  8765. qs += 8; qh += 4;
  8766. }
  8767. sumf += y[i].d * GGML_FP16_TO_FP32(scale.f16) * (vaddvq_s32(sumi1) + IQ1M_DELTA * vaddvq_s32(sumi2));
  8768. }
  8769. *s = sumf;
  8770. #elif defined __AVX2__
  8771. const __m256i mask = _mm256_set1_epi16(0x7);
  8772. const __m256i mone = _mm256_set1_epi16(1);
  8773. __m256 accum1 = _mm256_setzero_ps();
  8774. __m256 accum2 = _mm256_setzero_ps();
  8775. for (int i = 0; i < nb; ++i) {
  8776. const int8_t * q8 = y[i].qs;
  8777. const uint8_t * qs = x[i].qs;
  8778. const uint8_t * qh = x[i].qh;
  8779. const uint16_t * sc = (const uint16_t *)x[i].scales;
  8780. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  8781. __m256i sumi1 = _mm256_setzero_si256();
  8782. __m256i sumi2 = _mm256_setzero_si256();
  8783. for (int ib = 0; ib < QK_K/32; ib += 2) {
  8784. const __m256i q1b_1 = _mm256_set_epi64x(
  8785. iq1s_grid[qs[3] | (((uint16_t)qh[1] << 4) & 0x700)], iq1s_grid[qs[2] | (((uint16_t)qh[1] << 8) & 0x700)],
  8786. iq1s_grid[qs[1] | (((uint16_t)qh[0] << 4) & 0x700)], iq1s_grid[qs[0] | (((uint16_t)qh[0] << 8) & 0x700)]
  8787. );
  8788. const __m256i q1b_2 = _mm256_set_epi64x(
  8789. iq1s_grid[qs[7] | (((uint16_t)qh[3] << 4) & 0x700)], iq1s_grid[qs[6] | (((uint16_t)qh[3] << 8) & 0x700)],
  8790. iq1s_grid[qs[5] | (((uint16_t)qh[2] << 4) & 0x700)], iq1s_grid[qs[4] | (((uint16_t)qh[2] << 8) & 0x700)]
  8791. );
  8792. const __m256i q8b_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  8793. const __m256i q8b_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  8794. const __m256i dot1 = mul_add_epi8(q1b_1, q8b_1);
  8795. const __m256i dot2 = mul_add_epi8(q1b_2, q8b_2);
  8796. const __m256i delta1 = _mm256_set_epi64x(qh[1] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101,
  8797. qh[1] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101,
  8798. qh[0] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101,
  8799. qh[0] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101);
  8800. const __m256i delta2 = _mm256_set_epi64x(qh[3] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101,
  8801. qh[3] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101,
  8802. qh[2] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101,
  8803. qh[2] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101);
  8804. const __m256i dot3 = mul_add_epi8(delta1, q8b_1);
  8805. const __m256i dot4 = mul_add_epi8(delta2, q8b_2);
  8806. __m256i scale1 = MM256_SET_M128I(_mm_set1_epi16(sc[ib/2] >> 3), _mm_set1_epi16(sc[ib/2] >> 0));
  8807. __m256i scale2 = MM256_SET_M128I(_mm_set1_epi16(sc[ib/2] >> 9), _mm_set1_epi16(sc[ib/2] >> 6));
  8808. scale1 = _mm256_add_epi16(_mm256_slli_epi16(_mm256_and_si256(scale1, mask), 1), mone);
  8809. scale2 = _mm256_add_epi16(_mm256_slli_epi16(_mm256_and_si256(scale2, mask), 1), mone);
  8810. const __m256i p1 = _mm256_madd_epi16(dot1, scale1);
  8811. const __m256i p2 = _mm256_madd_epi16(dot2, scale2);
  8812. const __m256i p3 = _mm256_madd_epi16(dot3, scale1);
  8813. const __m256i p4 = _mm256_madd_epi16(dot4, scale2);
  8814. sumi1 = _mm256_add_epi32(sumi1, _mm256_add_epi32(p1, p2));
  8815. sumi2 = _mm256_add_epi32(sumi2, _mm256_add_epi32(p3, p4));
  8816. qs += 8; qh += 4;
  8817. }
  8818. const __m256 d = _mm256_set1_ps(y[i].d * GGML_FP16_TO_FP32(scale.f16));
  8819. accum1 = _mm256_fmadd_ps(d, _mm256_cvtepi32_ps(sumi1), accum1);
  8820. accum2 = _mm256_fmadd_ps(d, _mm256_cvtepi32_ps(sumi2), accum2);
  8821. }
  8822. *s = hsum_float_8(accum1) + IQ1M_DELTA * hsum_float_8(accum2);
  8823. #else
  8824. int sum1[2], sum2[2], delta[4];
  8825. float sumf = 0;
  8826. for (int i = 0; i < nb; i++) {
  8827. const int8_t * q8 = y[i].qs;
  8828. const uint8_t * qs = x[i].qs;
  8829. const uint8_t * qh = x[i].qh;
  8830. const uint16_t * sc = (const uint16_t *)x[i].scales;
  8831. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  8832. int sumi1 = 0, sumi2 = 0;
  8833. for (int ib = 0; ib < QK_K/32; ++ib) {
  8834. delta[0] = qh[0] & 0x08 ? -1 : 1;
  8835. delta[1] = qh[0] & 0x80 ? -1 : 1;
  8836. delta[2] = qh[1] & 0x08 ? -1 : 1;
  8837. delta[3] = qh[1] & 0x80 ? -1 : 1;
  8838. sum1[0] = sum1[1] = sum2[0] = sum2[1] = 0;
  8839. for (int l = 0; l < 4; ++l) {
  8840. const int8_t * grid = (const int8_t *)(iq1s_grid + (qs[l] | (((uint16_t)qh[l/2] << (8 - 4*(l%2))) & 0x700)));
  8841. int lsum1 = 0, lsum2 = 0;
  8842. for (int j = 0; j < 8; ++j) {
  8843. lsum1 += q8[j] * grid[j];
  8844. lsum2 += q8[j];
  8845. }
  8846. q8 += 8;
  8847. sum1[l/2] += lsum1;
  8848. sum2[l/2] += lsum2*delta[l];
  8849. }
  8850. const int ls1 = 2*((sc[ib/2] >> (6*(ib%2)+0)) & 0x7) + 1;
  8851. const int ls2 = 2*((sc[ib/2] >> (6*(ib%2)+3)) & 0x7) + 1;
  8852. sumi1 += sum1[0] * ls1 + sum1[1] * ls2;
  8853. sumi2 += sum2[0] * ls1 + sum2[1] * ls2;
  8854. qs += 4;
  8855. qh += 2;
  8856. }
  8857. sumf += GGML_FP16_TO_FP32(scale.f16) * y[i].d * (sumi1 + IQ1M_DELTA * sumi2);
  8858. }
  8859. *s = sumf;
  8860. #endif
  8861. }
  8862. void ggml_vec_dot_iq4_nl_q8_0(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  8863. assert(nrc == 1);
  8864. UNUSED(nrc);
  8865. UNUSED(bx);
  8866. UNUSED(by);
  8867. UNUSED(bs);
  8868. assert(n % QK4_NL == 0);
  8869. static_assert(QK4_NL == QK8_0, "QK4_NL and QK8_0 must be the same");
  8870. const block_iq4_nl * restrict x = vx;
  8871. const block_q8_0 * restrict y = vy;
  8872. const int nb = n / QK4_NL;
  8873. #if defined __ARM_NEON
  8874. const int8x16_t values = vld1q_s8(kvalues_iq4nl);
  8875. const uint8x16_t m4b = vdupq_n_u8(0x0f);
  8876. uint8x16x2_t q4bits;
  8877. int8x16x4_t q4b;
  8878. int8x16x4_t q8b;
  8879. int32x4_t prod_1, prod_2;
  8880. float sumf = 0;
  8881. for (int ib = 0; ib < nb; ib += 2) {
  8882. q4bits.val[0] = vld1q_u8(x[ib+0].qs);
  8883. q4bits.val[1] = vld1q_u8(x[ib+1].qs);
  8884. q8b.val[0] = vld1q_s8(y[ib+0].qs);
  8885. q8b.val[1] = vld1q_s8(y[ib+0].qs + 16);
  8886. q8b.val[2] = vld1q_s8(y[ib+1].qs);
  8887. q8b.val[3] = vld1q_s8(y[ib+1].qs + 16);
  8888. q4b.val[0] = ggml_vqtbl1q_s8(values, vandq_u8 (q4bits.val[0], m4b));
  8889. q4b.val[1] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[0], 4));
  8890. q4b.val[2] = ggml_vqtbl1q_s8(values, vandq_u8 (q4bits.val[1], m4b));
  8891. q4b.val[3] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[1], 4));
  8892. prod_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[0], q8b.val[0]), q4b.val[1], q8b.val[1]);
  8893. prod_2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[2], q8b.val[2]), q4b.val[3], q8b.val[3]);
  8894. sumf +=
  8895. GGML_FP16_TO_FP32(x[ib+0].d) * GGML_FP16_TO_FP32(y[ib+0].d) * vaddvq_s32(prod_1) +
  8896. GGML_FP16_TO_FP32(x[ib+1].d) * GGML_FP16_TO_FP32(y[ib+1].d) * vaddvq_s32(prod_2);
  8897. }
  8898. *s = sumf;
  8899. #elif defined __AVX2__
  8900. const __m128i values128 = _mm_loadu_si128((const __m128i*)kvalues_iq4nl);
  8901. const __m128i m4b = _mm_set1_epi8(0x0f);
  8902. const __m256i mone = _mm256_set1_epi16(1);
  8903. __m256 accum1 = _mm256_setzero_ps();
  8904. __m256 accum2 = _mm256_setzero_ps();
  8905. for (int ib = 0; ib < nb; ib += 2) {
  8906. const __m128i q4bits_1 = _mm_loadu_si128((const __m128i*)x[0].qs);
  8907. const __m128i q4bits_2 = _mm_loadu_si128((const __m128i*)x[1].qs);
  8908. const __m256i q8b_1 = _mm256_loadu_si256((const __m256i *)y[0].qs);
  8909. const __m256i q8b_2 = _mm256_loadu_si256((const __m256i *)y[1].qs);
  8910. const __m256i q4b_1 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4b)),
  8911. _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_1, m4b)));
  8912. const __m256i q4b_2 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_2, 4), m4b)),
  8913. _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_2, m4b)));
  8914. const __m256i p16_1 = mul_add_epi8(q4b_1, q8b_1);
  8915. const __m256i p16_2 = mul_add_epi8(q4b_2, q8b_2);
  8916. const __m256i p_1 = _mm256_madd_epi16(p16_1, mone);
  8917. const __m256i p_2 = _mm256_madd_epi16(p16_2, mone);
  8918. accum1 = _mm256_fmadd_ps(_mm256_set1_ps(GGML_FP16_TO_FP32(y[0].d)*GGML_FP16_TO_FP32(x[0].d)),
  8919. _mm256_cvtepi32_ps(p_1), accum1);
  8920. accum2 = _mm256_fmadd_ps(_mm256_set1_ps(GGML_FP16_TO_FP32(y[1].d)*GGML_FP16_TO_FP32(x[1].d)),
  8921. _mm256_cvtepi32_ps(p_2), accum2);
  8922. y += 2;
  8923. x += 2;
  8924. }
  8925. *s = hsum_float_8(_mm256_add_ps(accum1, accum2));
  8926. #elif defined(__POWER9_VECTOR__)
  8927. const vector signed char lowMask = vec_splats((signed char)0xF);
  8928. const vector signed int v0 = vec_splats((int32_t)0);
  8929. const vector unsigned char v4 = vec_splats((unsigned char)0x4);
  8930. vector float vsumf0 = vec_splats(0.0f);
  8931. vector float vsumf1 = vec_splats(0.0f);
  8932. const vector signed char values = vec_xl( 0, kvalues_iq4nl);
  8933. #pragma GCC unroll 4
  8934. for (int ib = 0; ib < nb; ++ib) {
  8935. __builtin_prefetch(x[ib].qs, 0, 1);
  8936. __builtin_prefetch(y[ib].qs, 0, 1);
  8937. vector float vxd = vec_splats(GGML_FP16_TO_FP32(x[ib].d));
  8938. vector float vyd = vec_splats(GGML_FP16_TO_FP32(y[ib].d));
  8939. vector float vd = vec_mul(vxd, vyd);
  8940. vector signed char qxs = (vector signed char)vec_xl( 0, x[ib].qs);
  8941. vector signed char q4x0 = vec_and(qxs, lowMask);
  8942. vector signed char q4x1 = vec_sr(qxs, v4);
  8943. q4x0 = vec_perm(values, values, (vector unsigned char)q4x0);
  8944. q4x1 = vec_perm(values, values, (vector unsigned char)q4x1);
  8945. vector signed char q8y0 = vec_xl( 0, y[ib].qs);
  8946. vector signed char q8y1 = vec_xl(16, y[ib].qs);
  8947. vector signed short qv0 = vec_add(vec_mule(q4x0, q8y0), vec_mulo(q4x0, q8y0));
  8948. vector signed short qv1 = vec_add(vec_mule(q4x1, q8y1), vec_mulo(q4x1, q8y1));
  8949. vector signed int vsumi0 = v0;
  8950. vector signed int vsumi1 = v0;
  8951. vsumi0 = vec_sum4s(qv0, vsumi0);
  8952. vsumi1 = vec_sum4s(qv1, vsumi1);
  8953. vsumf0 = vec_madd(vec_ctf(vsumi0, 0), vd, vsumf0);
  8954. vsumf1 = vec_madd(vec_ctf(vsumi1, 0), vd, vsumf1);
  8955. }
  8956. vsumf0 = vec_add(vsumf0, vsumf1);
  8957. vsumf0 = vec_add(vsumf0, vec_sld(vsumf0, vsumf0, 4));
  8958. vsumf0 = vec_add(vsumf0, vec_sld(vsumf0, vsumf0, 8));
  8959. *s = vec_extract(vsumf0, 0);
  8960. #elif defined (__loongarch_asx)
  8961. const __m128i values128 = __lsx_vld((const __m128i*)kvalues_iq4nl, 0);
  8962. const __m128i m4b = __lsx_vreplgr2vr_b(0x0f);
  8963. const __m256i mone = __lasx_xvreplgr2vr_h(1);
  8964. __m256 accum1 = (__m256)__lasx_xvldi(0);
  8965. __m256 accum2 = (__m256)__lasx_xvldi(0);
  8966. for (int ib = 0; ib < nb; ib += 2) {
  8967. const __m128i q4bits_1 = __lsx_vld((const __m128i*)x[0].qs, 0);
  8968. const __m128i q4bits_2 = __lsx_vld((const __m128i*)x[1].qs, 0);
  8969. const __m256i q8b_1 = __lasx_xvld((const __m256i *)y[0].qs, 0);
  8970. const __m256i q8b_2 = __lasx_xvld((const __m256i *)y[1].qs, 0);
  8971. const __m256i q4b_1 = lasx_insertf128(lsx_shuffle_b(values128, __lsx_vand_v(__lsx_vsrli_h(q4bits_1, 4), m4b)),
  8972. lsx_shuffle_b(values128, __lsx_vand_v(q4bits_1, m4b)));
  8973. const __m256i q4b_2 = lasx_insertf128(lsx_shuffle_b(values128, __lsx_vand_v(__lsx_vsrli_h(q4bits_2, 4), m4b)),
  8974. lsx_shuffle_b(values128, __lsx_vand_v(q4bits_2, m4b)));
  8975. const __m256i p16_1 = mul_add_epi8(q4b_1, q8b_1);
  8976. const __m256i p16_2 = mul_add_epi8(q4b_2, q8b_2);
  8977. const __m256i p_1 = lasx_madd_h(p16_1, mone);
  8978. const __m256i p_2 = lasx_madd_h(p16_2, mone);
  8979. accum1 = __lasx_xvfmadd_s(__lasx_xvreplfr2vr_s(GGML_FP16_TO_FP32(y[0].d)*GGML_FP16_TO_FP32(x[0].d)),
  8980. __lasx_xvffint_s_w(p_1), accum1);
  8981. accum2 = __lasx_xvfmadd_s(__lasx_xvreplfr2vr_s(GGML_FP16_TO_FP32(y[1].d)*GGML_FP16_TO_FP32(x[1].d)),
  8982. __lasx_xvffint_s_w(p_2), accum2);
  8983. y += 2;
  8984. x += 2;
  8985. }
  8986. *s = hsum_float_8(__lasx_xvfadd_s(accum1, accum2));
  8987. #else
  8988. float sumf = 0;
  8989. for (int ib = 0; ib < nb; ++ib) {
  8990. const float d = GGML_FP16_TO_FP32(y[ib].d)*GGML_FP16_TO_FP32(x[ib].d);
  8991. int sumi1 = 0, sumi2 = 0;
  8992. for (int j = 0; j < QK4_NL/2; ++j) {
  8993. sumi1 += y[ib].qs[j+ 0] * kvalues_iq4nl[x[ib].qs[j] & 0xf];
  8994. sumi2 += y[ib].qs[j+QK4_NL/2] * kvalues_iq4nl[x[ib].qs[j] >> 4];
  8995. }
  8996. sumf += d * (sumi1 + sumi2);
  8997. }
  8998. *s = sumf;
  8999. #endif
  9000. }
  9001. void ggml_vec_dot_iq4_xs_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  9002. assert(nrc == 1);
  9003. UNUSED(nrc);
  9004. UNUSED(bx);
  9005. UNUSED(by);
  9006. UNUSED(bs);
  9007. assert(n % QK_K == 0);
  9008. const block_iq4_xs * restrict x = vx;
  9009. const block_q8_K * restrict y = vy;
  9010. const int nb = n / QK_K;
  9011. #if defined __ARM_NEON
  9012. const int8x16_t values = vld1q_s8(kvalues_iq4nl);
  9013. const uint8x16_t m4b = vdupq_n_u8(0x0f);
  9014. ggml_uint8x16x2_t q4bits;
  9015. ggml_int8x16x4_t q4b;
  9016. ggml_int8x16x4_t q8b;
  9017. int32x4_t prod_1, prod_2;
  9018. float sumf = 0;
  9019. for (int ibl = 0; ibl < nb; ++ibl) {
  9020. const int8_t * q8 = y[ibl].qs;
  9021. const uint8_t * q4 = x[ibl].qs;
  9022. uint16_t h = x[ibl].scales_h;
  9023. int sumi1 = 0, sumi2 = 0;
  9024. for (int ib = 0; ib < QK_K/64; ++ib) {
  9025. q4bits = ggml_vld1q_u8_x2(q4); q4 += 32;
  9026. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  9027. q4b.val[0] = ggml_vqtbl1q_s8(values, vandq_u8 (q4bits.val[0], m4b));
  9028. q4b.val[1] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[0], 4));
  9029. q4b.val[2] = ggml_vqtbl1q_s8(values, vandq_u8 (q4bits.val[1], m4b));
  9030. q4b.val[3] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[1], 4));
  9031. prod_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[0], q8b.val[0]), q4b.val[1], q8b.val[1]);
  9032. prod_2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[2], q8b.val[2]), q4b.val[3], q8b.val[3]);
  9033. int ls1 = ((x[ibl].scales_l[ib] & 0xf) | ((h << 4) & 0x30)) - 32;
  9034. int ls2 = ((x[ibl].scales_l[ib] >> 4) | ((h << 2) & 0x30)) - 32;
  9035. h >>= 4;
  9036. sumi1 += vaddvq_s32(prod_1) * ls1;
  9037. sumi2 += vaddvq_s32(prod_2) * ls2;
  9038. }
  9039. sumf += GGML_FP16_TO_FP32(x[ibl].d) * y[ibl].d * (sumi1 + sumi2);
  9040. }
  9041. *s = sumf;
  9042. #elif defined __AVX2__
  9043. const __m128i values128 = _mm_loadu_si128((const __m128i*)kvalues_iq4nl);
  9044. const __m128i m4b = _mm_set1_epi8(0x0f);
  9045. __m256 accum = _mm256_setzero_ps();
  9046. for (int ibl = 0; ibl < nb; ++ibl) {
  9047. const uint8_t * qs = x[ibl].qs;
  9048. const int8_t * q8 = y[ibl].qs;
  9049. uint16_t sh = x[ibl].scales_h;
  9050. __m256i sumi1 = _mm256_setzero_si256();
  9051. __m256i sumi2 = _mm256_setzero_si256();
  9052. for (int ib = 0; ib < QK_K/32; ib += 2) {
  9053. const __m128i q4bits_1 = _mm_loadu_si128((const __m128i*)qs); qs += 16;
  9054. const __m128i q4bits_2 = _mm_loadu_si128((const __m128i*)qs); qs += 16;
  9055. const __m256i q8b_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  9056. const __m256i q8b_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  9057. const __m256i q4b_1 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4b)),
  9058. _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_1, m4b)));
  9059. const __m256i q4b_2 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_2, 4), m4b)),
  9060. _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_2, m4b)));
  9061. const __m256i p16_1 = mul_add_epi8(q4b_1, q8b_1);
  9062. const __m256i p16_2 = mul_add_epi8(q4b_2, q8b_2);
  9063. const int16_t ls1 = ((x[ibl].scales_l[ib/2] & 0xf) | ((sh << 4) & 0x30)) - 32;
  9064. const int16_t ls2 = ((x[ibl].scales_l[ib/2] >> 4) | ((sh << 2) & 0x30)) - 32;
  9065. sh >>= 4;
  9066. const __m256i p_1 = _mm256_madd_epi16(p16_1, _mm256_set1_epi16(ls1));
  9067. const __m256i p_2 = _mm256_madd_epi16(p16_2, _mm256_set1_epi16(ls2));
  9068. sumi1 = _mm256_add_epi32(p_1, sumi1);
  9069. sumi2 = _mm256_add_epi32(p_2, sumi2);
  9070. }
  9071. accum = _mm256_fmadd_ps(_mm256_set1_ps(GGML_FP16_TO_FP32(x[ibl].d)*y[ibl].d),
  9072. _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accum);
  9073. }
  9074. *s = hsum_float_8(accum);
  9075. #elif defined(__POWER9_VECTOR__)
  9076. const vector signed char lowMask = vec_splats((signed char)0xF);
  9077. const vector int v0 = vec_splats((int32_t)0);
  9078. const vector unsigned char v4 = vec_splats((unsigned char)0x4);
  9079. vector float vsumf0 = vec_splats(0.0f);
  9080. vector float vsumf1 = vec_splats(0.0f);
  9081. vector float vsumf2 = vec_splats(0.0f);
  9082. vector float vsumf3 = vec_splats(0.0f);
  9083. const vector signed char values = vec_xl( 0, kvalues_iq4nl);
  9084. for (int ibl = 0; ibl < nb; ++ibl) {
  9085. vector float vxd = vec_splats(GGML_FP16_TO_FP32(x[ibl].d));
  9086. vector float vyd = vec_splats(y[ibl].d);
  9087. vector float vd = vec_mul(vxd, vyd);
  9088. vector signed int vsumi0 = v0;
  9089. vector signed int vsumi1 = v0;
  9090. vector signed int vsumi2 = v0;
  9091. vector signed int vsumi3 = v0;
  9092. uint16_t h = x[ibl].scales_h;
  9093. const uint8_t * restrict q4 = x[ibl].qs;
  9094. const uint8_t * restrict sc = x[ibl].scales_l;
  9095. const int8_t * restrict q8 = y[ibl].qs;
  9096. for (int ib = 0; ib < QK_K/64; ib ++ ) {
  9097. __builtin_prefetch(q4, 0, 1);
  9098. __builtin_prefetch(q8, 0, 1);
  9099. vector signed char qxs0 = (vector signed char)vec_xl( 0, q4);
  9100. vector signed char qxs1 = (vector signed char)vec_xl(16, q4);
  9101. q4 += 32;
  9102. vector signed char q4x00 = (vector signed char)vec_and(qxs0, lowMask);
  9103. vector signed char q4x01 = (vector signed char)vec_sr(qxs0, v4);
  9104. vector signed char q4x10 = (vector signed char)vec_and(qxs1, lowMask);
  9105. vector signed char q4x11 = (vector signed char)vec_sr(qxs1, v4);
  9106. q4x00 = vec_perm(values, values, (vector unsigned char)q4x00);
  9107. q4x01 = vec_perm(values, values, (vector unsigned char)q4x01);
  9108. q4x10 = vec_perm(values, values, (vector unsigned char)q4x10);
  9109. q4x11 = vec_perm(values, values, (vector unsigned char)q4x11);
  9110. vector signed char q8y0 = vec_xl( 0, q8);
  9111. vector signed char q8y1 = vec_xl(16, q8);
  9112. vector signed char q8y2 = vec_xl(32, q8);
  9113. vector signed char q8y3 = vec_xl(48, q8);
  9114. q8 += 64;
  9115. vector signed short qv0 = vec_add(vec_mule(q4x00, q8y0), vec_mulo(q4x00, q8y0));
  9116. vector signed short qv1 = vec_add(vec_mule(q4x01, q8y1), vec_mulo(q4x01, q8y1));
  9117. vector signed short qv2 = vec_add(vec_mule(q4x10, q8y2), vec_mulo(q4x10, q8y2));
  9118. vector signed short qv3 = vec_add(vec_mule(q4x11, q8y3), vec_mulo(q4x11, q8y3));
  9119. const uint16_t ls0 = (uint16_t)(((sc[0] & 0xf) | ((h << 4) & 0x30)) - 32);
  9120. const uint16_t ls1 = (uint16_t)(((sc[0] >> 4) | ((h << 2) & 0x30)) - 32);
  9121. h >>= 4;
  9122. sc ++;
  9123. vector signed short vscales01 = vec_splats((int16_t)ls0);
  9124. vector signed short vscales23 = vec_splats((int16_t)ls1);
  9125. vsumi0 = vec_msum(qv0, vscales01, vsumi0);
  9126. vsumi1 = vec_msum(qv1, vscales01, vsumi1);
  9127. vsumi2 = vec_msum(qv2, vscales23, vsumi2);
  9128. vsumi3 = vec_msum(qv3, vscales23, vsumi3);
  9129. }
  9130. vsumf0 = vec_madd(vec_ctf(vsumi0, 0), vd, vsumf0);
  9131. vsumf1 = vec_madd(vec_ctf(vsumi1, 0), vd, vsumf1);
  9132. vsumf2 = vec_madd(vec_ctf(vsumi2, 0), vd, vsumf2);
  9133. vsumf3 = vec_madd(vec_ctf(vsumi3, 0), vd, vsumf3);
  9134. }
  9135. vsumf0 = vec_add(vsumf0, vsumf2);
  9136. vsumf1 = vec_add(vsumf1, vsumf3);
  9137. vsumf0 = vec_add(vsumf0, vsumf1);
  9138. vsumf0 = vec_add(vsumf0, vec_sld(vsumf0, vsumf0, 4));
  9139. vsumf0 = vec_add(vsumf0, vec_sld(vsumf0, vsumf0, 8));
  9140. *s = vec_extract(vsumf0, 0);
  9141. #elif defined(__loongarch_asx)
  9142. const __m128i values128 = __lsx_vld((const __m128i*)kvalues_iq4nl, 0);
  9143. const __m128i m4b = __lsx_vreplgr2vr_b(0x0f);
  9144. __m256 accum = (__m256)__lasx_xvldi(0);
  9145. __m256i tmp1;
  9146. __m128i tmp0, tmp2, tmp3, tmp4, mask_8f, mask;
  9147. mask_8f = __lsx_vreplgr2vr_b(0x8f);
  9148. for (int ibl = 0; ibl < nb; ++ibl) {
  9149. const uint8_t * qs = x[ibl].qs;
  9150. const int8_t * q8 = y[ibl].qs;
  9151. uint16_t sh = x[ibl].scales_h;
  9152. __m256i sumi1 = __lasx_xvldi(0);
  9153. __m256i sumi2 = __lasx_xvldi(0);
  9154. __m128i zero = __lsx_vldi(0);
  9155. for (int ib = 0; ib < QK_K/32; ib += 2) {
  9156. const __m128i q4bits_1 = __lsx_vld((const __m128i*)qs, 0); qs += 16;
  9157. const __m128i q4bits_2 = __lsx_vld((const __m128i*)qs, 0); qs += 16;
  9158. const __m256i q8b_1 = __lasx_xvld((const __m256i *)q8, 0); q8 += 32;
  9159. const __m256i q8b_2 = __lasx_xvld((const __m256i *)q8, 0); q8 += 32;
  9160. tmp2 = __lsx_vand_v(__lsx_vand_v(__lsx_vsrli_h(q4bits_1, 4), m4b), mask_8f);
  9161. tmp0 = __lsx_vori_b(tmp2, 0x10);
  9162. mask = __lsx_vsle_b(zero, tmp2);
  9163. tmp3 = __lsx_vand_v(tmp0, mask);
  9164. tmp3 = __lsx_vshuf_b(values128, zero, tmp3);
  9165. tmp2 = __lsx_vand_v(__lsx_vand_v(q4bits_1, m4b), mask_8f);
  9166. tmp0 = __lsx_vori_b(tmp2, 0x10);
  9167. mask = __lsx_vsle_b(zero, tmp2);
  9168. tmp4 = __lsx_vand_v(tmp0, mask);
  9169. tmp4 = __lsx_vshuf_b(values128, zero, tmp4);
  9170. const __m256i q4b_1 = lasx_insertf128(tmp3, tmp4);
  9171. tmp2 = __lsx_vand_v(__lsx_vand_v(__lsx_vsrli_h(q4bits_2, 4), m4b), mask_8f);
  9172. tmp0 = __lsx_vori_b(tmp2, 0x10);
  9173. mask = __lsx_vsle_b(zero, tmp2);
  9174. tmp3 = __lsx_vand_v(tmp0, mask);
  9175. tmp3 = __lsx_vshuf_b(values128, zero, tmp3);
  9176. tmp2 = __lsx_vand_v(__lsx_vand_v(q4bits_2, m4b), mask_8f);
  9177. tmp0 = __lsx_vori_b(tmp2, 0x10);
  9178. mask = __lsx_vsle_b(zero, tmp2);
  9179. tmp4 = __lsx_vand_v(tmp0, mask);
  9180. tmp4 = __lsx_vshuf_b(values128, zero, tmp4);
  9181. const __m256i q4b_2 = lasx_insertf128(tmp3, tmp4);
  9182. const __m256i p16_1 = mul_add_epi8(q4b_1, q8b_1);
  9183. const __m256i p16_2 = mul_add_epi8(q4b_2, q8b_2);
  9184. const int16_t ls1 = ((x[ibl].scales_l[ib/2] & 0xf) | ((sh << 4) & 0x30)) - 32;
  9185. const int16_t ls2 = ((x[ibl].scales_l[ib/2] >> 4) | ((sh << 2) & 0x30)) - 32;
  9186. sh >>= 4;
  9187. __m256i tmp5, tmp6;
  9188. tmp1 = __lasx_xvreplgr2vr_h(ls1);
  9189. tmp5 = __lasx_xvmulwev_w_h(p16_1, tmp1);
  9190. tmp6 = __lasx_xvmulwod_w_h(p16_1, tmp1);
  9191. const __m256i p_1 = __lasx_xvadd_w(tmp5, tmp6);
  9192. tmp1 = __lasx_xvreplgr2vr_h(ls2);
  9193. tmp5 = __lasx_xvmulwev_w_h(p16_2, tmp1);
  9194. tmp6 = __lasx_xvmulwod_w_h(p16_2, tmp1);
  9195. const __m256i p_2 = __lasx_xvadd_w(tmp5, tmp6);
  9196. sumi1 = __lasx_xvadd_w(p_1, sumi1);
  9197. sumi2 = __lasx_xvadd_w(p_2, sumi2);
  9198. }
  9199. accum = __lasx_xvfmadd_s(__lasx_xvreplfr2vr_s(GGML_FP16_TO_FP32(x[ibl].d)*y[ibl].d),
  9200. __lasx_xvffint_s_w(__lasx_xvadd_w(sumi1, sumi2)), accum);
  9201. }
  9202. *s = hsum_float_8(accum);
  9203. #else
  9204. float sumf = 0;
  9205. for (int ibl = 0; ibl < nb; ++ibl) {
  9206. const float d4d8 = GGML_FP16_TO_FP32(x[ibl].d) * y[ibl].d;
  9207. uint16_t h = x[ibl].scales_h;
  9208. const uint8_t * qs = x[ibl].qs;
  9209. const int8_t * q8 = y[ibl].qs;
  9210. for (int ib = 0; ib < QK_K/32; ib += 2) {
  9211. const uint8_t ls1 = (x[ibl].scales_l[ib/2] & 0xf) | ((h << 4) & 0x30);
  9212. const uint8_t ls2 = (x[ibl].scales_l[ib/2] >> 4) | ((h << 2) & 0x30);
  9213. h >>= 4;
  9214. const float d1 = d4d8*(ls1 - 32);
  9215. const float d2 = d4d8*(ls2 - 32);
  9216. int sumi1 = 0, sumi2 = 0;
  9217. for (int j = 0; j < 16; ++j) {
  9218. sumi1 += q8[j+ 0] * kvalues_iq4nl[qs[j] & 0xf];
  9219. sumi2 += q8[j+16] * kvalues_iq4nl[qs[j] >> 4];
  9220. }
  9221. sumf += d1 * (sumi1 + sumi2);
  9222. qs += 16;
  9223. q8 += 32;
  9224. sumi1 = sumi2 = 0;
  9225. for (int j = 0; j < 16; ++j) {
  9226. sumi1 += q8[j+ 0] * kvalues_iq4nl[qs[j] & 0xf];
  9227. sumi2 += q8[j+16] * kvalues_iq4nl[qs[j] >> 4];
  9228. }
  9229. sumf += d2 * (sumi1 + sumi2);
  9230. qs += 16;
  9231. q8 += 32;
  9232. }
  9233. }
  9234. *s = sumf;
  9235. #endif
  9236. }
  9237. // ================================ IQ2 quantization =============================================
  9238. typedef struct {
  9239. uint64_t * grid;
  9240. int * map;
  9241. uint16_t * neighbours;
  9242. } iq2_entry_t;
  9243. static iq2_entry_t iq2_data[4] = {
  9244. {NULL, NULL, NULL},
  9245. {NULL, NULL, NULL},
  9246. {NULL, NULL, NULL},
  9247. {NULL, NULL, NULL},
  9248. };
  9249. static inline int iq2_data_index(enum ggml_type type) {
  9250. GGML_ASSERT(type == GGML_TYPE_IQ2_XXS || type == GGML_TYPE_IQ2_XS || type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M || type == GGML_TYPE_IQ2_S);
  9251. return type == GGML_TYPE_IQ2_XXS ? 0 :
  9252. type == GGML_TYPE_IQ2_XS ? 1 :
  9253. type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M ? 2 : 3;
  9254. }
  9255. static inline int iq2_grid_size(enum ggml_type type) {
  9256. GGML_ASSERT(type == GGML_TYPE_IQ2_XXS || type == GGML_TYPE_IQ2_XS || type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M || type == GGML_TYPE_IQ2_S);
  9257. return type == GGML_TYPE_IQ2_XXS ? 256 :
  9258. type == GGML_TYPE_IQ2_XS ? 512 :
  9259. type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M ? NGRID_IQ1S : 1024;
  9260. }
  9261. static int iq2_compare_func(const void * left, const void * right) {
  9262. const int * l = (const int *)left;
  9263. const int * r = (const int *)right;
  9264. return l[0] < r[0] ? -1 : l[0] > r[0] ? 1 : l[1] < r[1] ? -1 : l[1] > r[1] ? 1 : 0;
  9265. }
  9266. void iq2xs_init_impl(enum ggml_type type) {
  9267. const int gindex = iq2_data_index(type);
  9268. const int grid_size = iq2_grid_size(type);
  9269. if (iq2_data[gindex].grid) {
  9270. return;
  9271. }
  9272. static const uint16_t kgrid_2bit_256[256] = {
  9273. 0, 2, 5, 8, 10, 17, 20, 32, 34, 40, 42, 65, 68, 80, 88, 97,
  9274. 100, 128, 130, 138, 162, 257, 260, 272, 277, 320, 388, 408, 512, 514, 546, 642,
  9275. 1025, 1028, 1040, 1057, 1060, 1088, 1090, 1096, 1120, 1153, 1156, 1168, 1188, 1280, 1282, 1288,
  9276. 1312, 1350, 1385, 1408, 1425, 1545, 1552, 1600, 1668, 1700, 2048, 2053, 2056, 2068, 2088, 2113,
  9277. 2116, 2128, 2130, 2184, 2308, 2368, 2562, 2580, 4097, 4100, 4112, 4129, 4160, 4192, 4228, 4240,
  9278. 4245, 4352, 4360, 4384, 4432, 4442, 4480, 4644, 4677, 5120, 5128, 5152, 5157, 5193, 5248, 5400,
  9279. 5474, 5632, 5654, 6145, 6148, 6160, 6208, 6273, 6400, 6405, 6560, 6737, 8192, 8194, 8202, 8260,
  9280. 8289, 8320, 8322, 8489, 8520, 8704, 8706, 9217, 9220, 9232, 9280, 9302, 9472, 9537, 9572, 9872,
  9281. 10248, 10272, 10388, 10820, 16385, 16388, 16400, 16408, 16417, 16420, 16448, 16456, 16470, 16480, 16513, 16516,
  9282. 16528, 16640, 16672, 16737, 16768, 16773, 16897, 16912, 16968, 16982, 17000, 17408, 17416, 17440, 17536, 17561,
  9283. 17682, 17700, 17920, 18433, 18436, 18448, 18496, 18501, 18688, 18776, 18785, 18818, 19013, 19088, 20480, 20488,
  9284. 20497, 20505, 20512, 20608, 20616, 20740, 20802, 20900, 21137, 21648, 21650, 21770, 22017, 22100, 22528, 22545,
  9285. 22553, 22628, 22848, 23048, 24580, 24592, 24640, 24680, 24832, 24917, 25112, 25184, 25600, 25605, 25872, 25874,
  9286. 25988, 26690, 32768, 32770, 32778, 32833, 32898, 33028, 33048, 33088, 33297, 33793, 33796, 33808, 33813, 33856,
  9287. 33888, 34048, 34118, 34196, 34313, 34368, 34400, 34818, 35076, 35345, 36868, 36880, 36900, 36928, 37025, 37142,
  9288. 37248, 37445, 37888, 37922, 37956, 38225, 39041, 39200, 40962, 41040, 41093, 41225, 41472, 42008, 43088, 43268,
  9289. };
  9290. static const uint16_t kgrid_2bit_512[512] = {
  9291. 0, 2, 5, 8, 10, 17, 20, 22, 25, 32, 34, 37, 40, 65, 68, 70,
  9292. 73, 80, 82, 85, 88, 97, 100, 128, 130, 133, 136, 145, 148, 153, 160, 257,
  9293. 260, 262, 265, 272, 274, 277, 280, 282, 289, 292, 320, 322, 325, 328, 337, 340,
  9294. 352, 360, 385, 388, 400, 512, 514, 517, 520, 529, 532, 544, 577, 580, 592, 597,
  9295. 640, 650, 1025, 1028, 1030, 1033, 1040, 1042, 1045, 1048, 1057, 1060, 1088, 1090, 1093, 1096,
  9296. 1105, 1108, 1110, 1120, 1153, 1156, 1168, 1280, 1282, 1285, 1288, 1297, 1300, 1312, 1345, 1348,
  9297. 1360, 1377, 1408, 1537, 1540, 1552, 1574, 1600, 1602, 1668, 2048, 2050, 2053, 2056, 2058, 2065,
  9298. 2068, 2080, 2085, 2113, 2116, 2128, 2136, 2176, 2208, 2218, 2305, 2308, 2320, 2368, 2433, 2441,
  9299. 2560, 2592, 2600, 2710, 2720, 4097, 4100, 4102, 4105, 4112, 4114, 4117, 4120, 4129, 4132, 4160,
  9300. 4162, 4165, 4168, 4177, 4180, 4192, 4202, 4225, 4228, 4240, 4352, 4354, 4357, 4360, 4369, 4372,
  9301. 4384, 4417, 4420, 4432, 4480, 4500, 4502, 4609, 4612, 4614, 4624, 4672, 4704, 5120, 5122, 5125,
  9302. 5128, 5137, 5140, 5152, 5185, 5188, 5193, 5200, 5220, 5248, 5377, 5380, 5392, 5440, 5632, 5652,
  9303. 5705, 6145, 6148, 6160, 6162, 6208, 6228, 6278, 6400, 6405, 6502, 6737, 6825, 8192, 8194, 8197,
  9304. 8200, 8202, 8209, 8212, 8224, 8257, 8260, 8272, 8320, 8352, 8449, 8452, 8464, 8512, 8520, 8549,
  9305. 8704, 8738, 8832, 8872, 9217, 9220, 9232, 9257, 9280, 9472, 9537, 9554, 9625, 9729, 9754, 9894,
  9306. 10240, 10248, 10250, 10272, 10325, 10376, 10402, 10600, 10640, 10760, 10784, 10882, 10888, 10890, 16385, 16388,
  9307. 16390, 16393, 16400, 16402, 16405, 16408, 16417, 16420, 16448, 16450, 16453, 16456, 16458, 16465, 16468, 16480,
  9308. 16485, 16513, 16516, 16528, 16640, 16642, 16645, 16648, 16657, 16660, 16672, 16705, 16708, 16720, 16768, 16773,
  9309. 16802, 16897, 16900, 16912, 16914, 16937, 16960, 17408, 17410, 17413, 17416, 17425, 17428, 17433, 17440, 17473,
  9310. 17476, 17488, 17536, 17556, 17665, 17668, 17680, 17700, 17728, 17818, 17920, 17930, 17988, 18000, 18433, 18436,
  9311. 18448, 18496, 18501, 18516, 18530, 18688, 18705, 18756, 18768, 18793, 18948, 20480, 20482, 20485, 20488, 20497,
  9312. 20500, 20512, 20520, 20545, 20548, 20560, 20608, 20737, 20740, 20752, 20757, 20800, 20802, 20992, 21060, 21162,
  9313. 21505, 21508, 21520, 21537, 21568, 21600, 21633, 21665, 21760, 21768, 21888, 21896, 22049, 22120, 22177, 22528,
  9314. 22548, 22593, 22608, 22681, 22810, 22848, 22850, 23173, 24577, 24580, 24592, 24640, 24660, 24674, 24710, 24745,
  9315. 24832, 25124, 25162, 25234, 25600, 25622, 25872, 25920, 25925, 26020, 26625, 26730, 26917, 27142, 27220, 27234,
  9316. 32768, 32770, 32773, 32776, 32785, 32788, 32800, 32810, 32833, 32836, 32848, 32896, 32898, 32936, 32938, 33025,
  9317. 33028, 33030, 33040, 33088, 33105, 33113, 33280, 33312, 33408, 33410, 33440, 33448, 33793, 33796, 33808, 33810,
  9318. 33813, 33856, 33888, 33929, 34048, 34116, 34213, 34328, 34410, 34816, 34824, 34853, 34906, 34944, 34946, 34984,
  9319. 35078, 35362, 35456, 35464, 35478, 35496, 36865, 36868, 36880, 36928, 36950, 36996, 37120, 37154, 37220, 37462,
  9320. 37513, 37888, 37893, 37956, 37968, 37976, 38185, 38288, 38290, 38465, 38993, 39078, 39241, 39445, 39520, 40960,
  9321. 40962, 40968, 40970, 40992, 41002, 41120, 41297, 41305, 41382, 41472, 41474, 41480, 41514, 41600, 41632, 42048,
  9322. 42133, 42597, 42648, 43018, 43040, 43042, 43048, 43168, 43176, 43268, 43396, 43398, 43560, 43562, 43665, 43690,
  9323. };
  9324. static const uint16_t kgrid_1bit_2048[NGRID_IQ1S] = {
  9325. 0, 2, 5, 8, 10, 17, 21, 32, 34, 40, 42, 69, 81, 84, 86, 101,
  9326. 128, 130, 136, 138, 149, 160, 162, 168, 170, 260, 261, 273, 276, 278, 281, 282,
  9327. 293, 321, 326, 329, 338, 341, 346, 353, 356, 358, 360, 389, 401, 404, 406, 421,
  9328. 512, 514, 520, 522, 533, 544, 546, 552, 554, 581, 593, 601, 612, 617, 640, 642,
  9329. 648, 650, 657, 661, 665, 672, 674, 680, 682, 1041, 1044, 1046, 1061, 1089, 1097, 1109,
  9330. 1114, 1124, 1125, 1169, 1177, 1189, 1281, 1284, 1285, 1286, 1301, 1304, 1306, 1321, 1344, 1349,
  9331. 1354, 1360, 1361, 1364, 1365, 1366, 1369, 1376, 1378, 1381, 1384, 1386, 1409, 1425, 1429, 1432,
  9332. 1434, 1441, 1444, 1445, 1446, 1449, 1556, 1561, 1601, 1604, 1616, 1618, 1621, 1624, 1632, 1633,
  9333. 1638, 1641, 1669, 1681, 1684, 1689, 2048, 2050, 2056, 2058, 2069, 2080, 2082, 2088, 2090, 2117,
  9334. 2129, 2134, 2149, 2176, 2178, 2184, 2186, 2197, 2208, 2210, 2216, 2218, 2309, 2321, 2324, 2329,
  9335. 2340, 2341, 2369, 2384, 2385, 2389, 2401, 2404, 2409, 2449, 2452, 2454, 2457, 2469, 2560, 2562,
  9336. 2568, 2570, 2581, 2592, 2594, 2600, 2602, 2629, 2641, 2649, 2657, 2661, 2688, 2690, 2693, 2696,
  9337. 2698, 2709, 2720, 2722, 2728, 2730, 4112, 4113, 4116, 4121, 4132, 4133, 4161, 4164, 4176, 4181,
  9338. 4184, 4193, 4196, 4197, 4201, 4241, 4244, 4246, 4257, 4261, 4353, 4356, 4358, 4361, 4368, 4370,
  9339. 4373, 4376, 4385, 4388, 4393, 4421, 4426, 4432, 4433, 4434, 4436, 4437, 4438, 4441, 4448, 4453,
  9340. 4484, 4498, 4501, 4513, 4516, 4625, 4628, 4630, 4645, 4672, 4678, 4681, 4690, 4693, 4696, 4698,
  9341. 4708, 4710, 4741, 4753, 4756, 4758, 4773, 5121, 5126, 5129, 5140, 5141, 5144, 5145, 5153, 5158,
  9342. 5185, 5189, 5190, 5192, 5194, 5201, 5204, 5205, 5206, 5209, 5218, 5221, 5224, 5252, 5257, 5264,
  9343. 5268, 5269, 5272, 5273, 5274, 5281, 5284, 5285, 5289, 5378, 5381, 5386, 5393, 5396, 5397, 5398,
  9344. 5401, 5408, 5410, 5413, 5416, 5418, 5441, 5444, 5445, 5446, 5457, 5458, 5460, 5461, 5462, 5465,
  9345. 5466, 5473, 5476, 5477, 5478, 5481, 5504, 5506, 5508, 5509, 5512, 5514, 5520, 5521, 5524, 5525,
  9346. 5526, 5529, 5530, 5536, 5538, 5541, 5633, 5636, 5637, 5638, 5653, 5654, 5656, 5658, 5665, 5670,
  9347. 5696, 5698, 5700, 5701, 5704, 5706, 5713, 5717, 5718, 5720, 5721, 5729, 5732, 5733, 5736, 5737,
  9348. 5738, 5766, 5770, 5778, 5781, 5796, 5801, 6161, 6166, 6181, 6209, 6212, 6214, 6217, 6224, 6229,
  9349. 6232, 6234, 6240, 6241, 6244, 6246, 6249, 6277, 6289, 6292, 6309, 6416, 6418, 6421, 6426, 6433,
  9350. 6437, 6466, 6468, 6469, 6472, 6481, 6484, 6485, 6486, 6489, 6490, 6496, 6501, 6506, 6537, 6545,
  9351. 6546, 6549, 6552, 6561, 6566, 6569, 6665, 6678, 6692, 6694, 6724, 6726, 6729, 6736, 6738, 6741,
  9352. 6744, 6753, 6758, 6761, 6789, 6801, 6806, 6810, 8192, 8194, 8200, 8202, 8213, 8224, 8226, 8229,
  9353. 8232, 8234, 8261, 8273, 8281, 8289, 8293, 8320, 8322, 8328, 8330, 8341, 8352, 8354, 8357, 8360,
  9354. 8362, 8453, 8465, 8468, 8473, 8485, 8514, 8516, 8521, 8533, 8536, 8538, 8545, 8548, 8549, 8550,
  9355. 8581, 8592, 8598, 8601, 8613, 8705, 8712, 8714, 8721, 8725, 8736, 8738, 8744, 8746, 8773, 8785,
  9356. 8790, 8793, 8805, 8833, 8840, 8842, 8849, 8853, 8864, 8866, 8872, 8874, 9221, 9236, 9238, 9241,
  9357. 9253, 9284, 9285, 9286, 9289, 9298, 9301, 9304, 9306, 9318, 9349, 9361, 9364, 9369, 9377, 9381,
  9358. 9481, 9493, 9505, 9513, 9536, 9541, 9544, 9553, 9556, 9557, 9561, 9570, 9573, 9576, 9609, 9616,
  9359. 9620, 9621, 9624, 9626, 9633, 9636, 9638, 9641, 9733, 9744, 9746, 9753, 9765, 9793, 9801, 9813,
  9360. 9824, 9825, 9833, 9860, 9862, 9872, 9882, 10240, 10242, 10248, 10250, 10261, 10272, 10274, 10280, 10282,
  9361. 10309, 10321, 10324, 10341, 10368, 10370, 10376, 10378, 10400, 10402, 10408, 10410, 10505, 10513, 10516, 10521,
  9362. 10533, 10566, 10569, 10578, 10581, 10593, 10596, 10598, 10601, 10629, 10640, 10646, 10649, 10660, 10661, 10752,
  9363. 10754, 10760, 10762, 10784, 10786, 10792, 10794, 10821, 10833, 10838, 10841, 10853, 10880, 10882, 10888, 10890,
  9364. 10901, 10912, 10914, 10920, 10922, 16389, 16401, 16406, 16421, 16457, 16466, 16469, 16472, 16474, 16481, 16484,
  9365. 16486, 16532, 16537, 16545, 16550, 16640, 16641, 16644, 16646, 16649, 16658, 16661, 16662, 16664, 16666, 16673,
  9366. 16678, 16681, 16709, 16712, 16714, 16721, 16724, 16725, 16726, 16729, 16730, 16741, 16744, 16746, 16769, 16772,
  9367. 16774, 16784, 16786, 16789, 16800, 16801, 16802, 16901, 16913, 16916, 16918, 16933, 16961, 16978, 16981, 16986,
  9368. 16996, 17001, 17033, 17044, 17061, 17409, 17429, 17433, 17449, 17477, 17480, 17482, 17489, 17492, 17493, 17494,
  9369. 17505, 17506, 17509, 17512, 17514, 17537, 17542, 17545, 17552, 17554, 17557, 17568, 17569, 17577, 17665, 17666,
  9370. 17669, 17674, 17681, 17684, 17685, 17686, 17689, 17696, 17701, 17706, 17729, 17732, 17733, 17734, 17737, 17744,
  9371. 17745, 17748, 17749, 17750, 17752, 17753, 17761, 17764, 17765, 17766, 17769, 17794, 17796, 17797, 17800, 17809,
  9372. 17812, 17813, 17814, 17817, 17818, 17829, 17832, 17834, 17921, 17925, 17929, 17940, 17941, 17944, 17946, 17953,
  9373. 17956, 17961, 17984, 17986, 17989, 17992, 18000, 18001, 18002, 18005, 18006, 18009, 18018, 18021, 18024, 18049,
  9374. 18053, 18058, 18068, 18069, 18081, 18084, 18086, 18437, 18449, 18453, 18458, 18469, 18498, 18505, 18512, 18517,
  9375. 18520, 18529, 18532, 18534, 18537, 18565, 18577, 18580, 18582, 18585, 18597, 18689, 18693, 18694, 18698, 18704,
  9376. 18708, 18709, 18712, 18721, 18724, 18726, 18752, 18757, 18762, 18769, 18770, 18772, 18773, 18774, 18777, 18784,
  9377. 18786, 18789, 18790, 18794, 18822, 18825, 18834, 18837, 18838, 18840, 18849, 18852, 18854, 18857, 18966, 19012,
  9378. 19014, 19017, 19029, 19032, 19034, 19044, 19049, 19092, 19109, 20481, 20484, 20485, 20486, 20489, 20498, 20501,
  9379. 20506, 20513, 20516, 20521, 20544, 20549, 20552, 20561, 20564, 20565, 20566, 20569, 20581, 20584, 20614, 20617,
  9380. 20629, 20632, 20640, 20641, 20646, 20649, 20741, 20744, 20745, 20746, 20753, 20756, 20757, 20758, 20760, 20761,
  9381. 20768, 20773, 20774, 20776, 20778, 20801, 20804, 20805, 20806, 20809, 20816, 20817, 20818, 20820, 20821, 20822,
  9382. 20824, 20825, 20826, 20833, 20836, 20837, 20838, 20841, 20866, 20869, 20881, 20884, 20885, 20886, 20889, 20896,
  9383. 20901, 20906, 20993, 20998, 21010, 21013, 21018, 21025, 21028, 21058, 21061, 21066, 21073, 21076, 21077, 21078,
  9384. 21081, 21090, 21093, 21125, 21136, 21138, 21141, 21145, 21146, 21156, 21508, 21509, 21521, 21524, 21525, 21526,
  9385. 21528, 21529, 21537, 21541, 21544, 21546, 21569, 21572, 21573, 21574, 21577, 21578, 21584, 21585, 21588, 21589,
  9386. 21590, 21592, 21593, 21594, 21601, 21602, 21604, 21605, 21606, 21609, 21632, 21640, 21642, 21649, 21652, 21653,
  9387. 21654, 21657, 21665, 21668, 21669, 21674, 21761, 21762, 21764, 21765, 21766, 21769, 21776, 21777, 21778, 21780,
  9388. 21781, 21782, 21785, 21786, 21793, 21796, 21797, 21798, 21801, 21824, 21825, 21826, 21828, 21829, 21830, 21832,
  9389. 21833, 21840, 21841, 21842, 21844, 21845, 21846, 21848, 21849, 21850, 21856, 21857, 21860, 21861, 21862, 21864,
  9390. 21865, 21866, 21889, 21892, 21893, 21897, 21898, 21904, 21905, 21908, 21909, 21910, 21912, 21913, 21921, 21924,
  9391. 21925, 21926, 21929, 22016, 22017, 22018, 22020, 22022, 22024, 22025, 22033, 22036, 22037, 22040, 22041, 22048,
  9392. 22049, 22050, 22052, 22053, 22054, 22056, 22057, 22081, 22085, 22086, 22088, 22089, 22090, 22096, 22097, 22098,
  9393. 22100, 22101, 22102, 22104, 22105, 22106, 22113, 22116, 22117, 22121, 22146, 22149, 22150, 22152, 22153, 22154,
  9394. 22161, 22165, 22170, 22178, 22181, 22182, 22184, 22185, 22532, 22533, 22534, 22537, 22544, 22549, 22552, 22561,
  9395. 22570, 22597, 22600, 22602, 22609, 22612, 22613, 22614, 22616, 22617, 22624, 22626, 22628, 22629, 22658, 22665,
  9396. 22672, 22674, 22677, 22680, 22689, 22697, 22785, 22786, 22789, 22794, 22801, 22804, 22805, 22806, 22809, 22821,
  9397. 22849, 22852, 22853, 22854, 22857, 22864, 22865, 22866, 22868, 22869, 22870, 22872, 22873, 22874, 22881, 22884,
  9398. 22885, 22886, 22889, 22913, 22917, 22921, 22929, 22932, 22933, 22934, 22936, 22937, 22949, 23044, 23048, 23061,
  9399. 23066, 23072, 23077, 23078, 23081, 23109, 23112, 23113, 23121, 23125, 23126, 23128, 23129, 23138, 23141, 23144,
  9400. 23146, 23169, 23178, 23186, 23189, 23190, 23192, 23194, 23201, 24581, 24596, 24598, 24601, 24613, 24644, 24656,
  9401. 24661, 24662, 24664, 24666, 24673, 24676, 24678, 24681, 24705, 24726, 24741, 24833, 24836, 24838, 24841, 24850,
  9402. 24853, 24865, 24866, 24870, 24873, 24901, 24905, 24913, 24917, 24918, 24921, 24933, 24934, 24938, 24964, 24970,
  9403. 24978, 24981, 24993, 24998, 25001, 25105, 25110, 25113, 25152, 25153, 25158, 25173, 25174, 25176, 25184, 25221,
  9404. 25233, 25238, 25253, 25617, 25618, 25621, 25622, 25626, 25633, 25638, 25641, 25664, 25666, 25669, 25672, 25674,
  9405. 25681, 25684, 25685, 25686, 25689, 25690, 25696, 25698, 25701, 25732, 25733, 25737, 25744, 25746, 25748, 25749,
  9406. 25750, 25752, 25754, 25761, 25764, 25769, 25861, 25864, 25866, 25873, 25877, 25878, 25881, 25924, 25925, 25926,
  9407. 25929, 25936, 25937, 25940, 25941, 25942, 25945, 25953, 25956, 25957, 25958, 25961, 25990, 25993, 25994, 26001,
  9408. 26005, 26006, 26009, 26010, 26018, 26021, 26022, 26024, 26114, 26121, 26133, 26144, 26150, 26152, 26153, 26176,
  9409. 26181, 26184, 26186, 26193, 26196, 26197, 26198, 26200, 26202, 26208, 26213, 26216, 26240, 26242, 26245, 26250,
  9410. 26260, 26262, 26264, 26265, 26272, 26276, 26278, 26282, 26646, 26649, 26661, 26689, 26706, 26709, 26714, 26721,
  9411. 26729, 26757, 26769, 26776, 26790, 26881, 26884, 26896, 26901, 26913, 26916, 26918, 26921, 26944, 26945, 26949,
  9412. 26950, 26952, 26961, 26964, 26965, 26966, 26969, 26976, 26981, 26986, 27010, 27012, 27018, 27029, 27041, 27044,
  9413. 27045, 27049, 27153, 27158, 27160, 27201, 27204, 27209, 27216, 27221, 27224, 27226, 27236, 27237, 27241, 27270,
  9414. 27284, 27288, 27290, 27302, 32768, 32770, 32776, 32778, 32800, 32802, 32808, 32810, 32837, 32848, 32849, 32852,
  9415. 32854, 32857, 32869, 32896, 32898, 32904, 32906, 32917, 32928, 32930, 32936, 32938, 33029, 33041, 33044, 33046,
  9416. 33049, 33061, 33089, 33092, 33097, 33104, 33106, 33109, 33110, 33112, 33113, 33124, 33126, 33129, 33157, 33161,
  9417. 33172, 33174, 33177, 33189, 33280, 33282, 33288, 33290, 33301, 33312, 33314, 33320, 33322, 33361, 33364, 33369,
  9418. 33381, 33408, 33410, 33416, 33418, 33429, 33440, 33442, 33448, 33450, 33812, 33817, 33857, 33860, 33873, 33877,
  9419. 33882, 33889, 33892, 33897, 33940, 33945, 34049, 34057, 34066, 34069, 34074, 34086, 34089, 34112, 34113, 34117,
  9420. 34120, 34129, 34132, 34133, 34134, 34137, 34138, 34149, 34150, 34152, 34154, 34177, 34180, 34182, 34185, 34192,
  9421. 34194, 34197, 34200, 34214, 34321, 34326, 34329, 34341, 34369, 34372, 34377, 34378, 34384, 34389, 34393, 34394,
  9422. 34401, 34406, 34410, 34437, 34449, 34458, 34468, 34816, 34818, 34824, 34826, 34837, 34848, 34850, 34856, 34858,
  9423. 34881, 34885, 34897, 34900, 34905, 34917, 34921, 34944, 34946, 34952, 34954, 34965, 34976, 34978, 34984, 34986,
  9424. 35077, 35078, 35089, 35092, 35094, 35109, 35137, 35140, 35142, 35145, 35152, 35154, 35157, 35162, 35169, 35172,
  9425. 35205, 35222, 35225, 35237, 35328, 35330, 35336, 35338, 35349, 35360, 35362, 35368, 35370, 35397, 35409, 35412,
  9426. 35414, 35456, 35458, 35464, 35466, 35477, 35488, 35490, 35496, 35498, 36869, 36881, 36886, 36888, 36889, 36901,
  9427. 36929, 36934, 36937, 36949, 36952, 36954, 36969, 36970, 36997, 37009, 37012, 37014, 37017, 37029, 37121, 37124,
  9428. 37126, 37129, 37136, 37141, 37144, 37146, 37153, 37156, 37158, 37161, 37184, 37189, 37200, 37201, 37204, 37205,
  9429. 37206, 37209, 37218, 37221, 37252, 37254, 37266, 37269, 37272, 37281, 37284, 37286, 37289, 37381, 37393, 37396,
  9430. 37401, 37413, 37444, 37446, 37449, 37456, 37458, 37461, 37464, 37478, 37481, 37509, 37524, 37526, 37545, 37889,
  9431. 37892, 37894, 37904, 37909, 37912, 37926, 37952, 37962, 37969, 37972, 37973, 37974, 37976, 37977, 37984, 37985,
  9432. 37986, 37989, 38020, 38022, 38034, 38036, 38037, 38040, 38049, 38057, 38144, 38149, 38152, 38154, 38160, 38161,
  9433. 38164, 38165, 38166, 38169, 38177, 38181, 38185, 38186, 38209, 38212, 38213, 38214, 38217, 38224, 38225, 38226,
  9434. 38228, 38229, 38230, 38232, 38233, 38234, 38241, 38244, 38245, 38246, 38249, 38273, 38277, 38280, 38289, 38290,
  9435. 38292, 38293, 38294, 38297, 38298, 38304, 38306, 38309, 38312, 38314, 38401, 38404, 38416, 38421, 38425, 38432,
  9436. 38438, 38441, 38469, 38472, 38473, 38481, 38482, 38485, 38486, 38489, 38501, 38504, 38530, 38532, 38537, 38538,
  9437. 38546, 38548, 38549, 38564, 38566, 38569, 38917, 38934, 38937, 38949, 38977, 38982, 38992, 38994, 38997, 38998,
  9438. 39002, 39012, 39013, 39045, 39057, 39062, 39065, 39077, 39172, 39174, 39177, 39184, 39186, 39189, 39192, 39194,
  9439. 39200, 39201, 39204, 39206, 39232, 39234, 39237, 39240, 39242, 39249, 39252, 39253, 39254, 39257, 39266, 39269,
  9440. 39270, 39274, 39297, 39300, 39312, 39314, 39317, 39322, 39329, 39334, 39429, 39445, 39461, 39492, 39494, 39497,
  9441. 39504, 39509, 39512, 39521, 39557, 39569, 39572, 39573, 39574, 40960, 40962, 40968, 40970, 40981, 40992, 40994,
  9442. 41000, 41002, 41029, 41041, 41044, 41046, 41049, 41088, 41090, 41096, 41098, 41109, 41120, 41122, 41128, 41130,
  9443. 41221, 41225, 41233, 41236, 41238, 41241, 41242, 41286, 41289, 41297, 41301, 41304, 41306, 41313, 41316, 41349,
  9444. 41360, 41362, 41366, 41369, 41474, 41480, 41482, 41488, 41497, 41506, 41512, 41514, 41541, 41553, 41558, 41561,
  9445. 41573, 41600, 41602, 41608, 41610, 41621, 41632, 41634, 41640, 41642, 42009, 42021, 42049, 42052, 42064, 42068,
  9446. 42069, 42072, 42074, 42081, 42085, 42086, 42088, 42089, 42117, 42246, 42249, 42256, 42258, 42261, 42264, 42278,
  9447. 42281, 42306, 42309, 42321, 42324, 42325, 42326, 42329, 42341, 42346, 42369, 42372, 42373, 42374, 42377, 42386,
  9448. 42389, 42392, 42501, 42513, 42518, 42522, 42529, 42533, 42564, 42566, 42570, 42578, 42581, 42582, 42584, 42592,
  9449. 42594, 42630, 42640, 42645, 42646, 42649, 42657, 42660, 42662, 43008, 43010, 43016, 43018, 43040, 43042, 43048,
  9450. 43050, 43089, 43092, 43094, 43097, 43136, 43138, 43144, 43146, 43157, 43168, 43170, 43176, 43178, 43269, 43284,
  9451. 43289, 43297, 43301, 43329, 43344, 43349, 43354, 43361, 43366, 43369, 43408, 43414, 43520, 43522, 43528, 43530,
  9452. 43552, 43554, 43560, 43562, 43601, 43604, 43606, 43648, 43650, 43656, 43658, 43669, 43680, 43682, 43688, 43690,
  9453. };
  9454. static const uint16_t kgrid_2bit_1024[1024] = {
  9455. 0, 2, 5, 8, 10, 17, 20, 22, 25, 32, 34, 37, 40, 65, 68, 70,
  9456. 73, 80, 82, 85, 88, 97, 100, 102, 105, 128, 130, 133, 136, 145, 148, 160,
  9457. 165, 170, 257, 260, 262, 265, 272, 274, 277, 280, 289, 292, 320, 322, 325, 328,
  9458. 337, 340, 342, 345, 352, 357, 360, 385, 388, 400, 402, 405, 417, 420, 512, 514,
  9459. 517, 520, 529, 532, 544, 554, 577, 580, 582, 585, 592, 597, 640, 645, 650, 660,
  9460. 674, 1025, 1028, 1030, 1033, 1040, 1042, 1045, 1048, 1057, 1060, 1062, 1065, 1088, 1090, 1093,
  9461. 1096, 1098, 1105, 1108, 1110, 1113, 1120, 1122, 1125, 1153, 1156, 1158, 1161, 1168, 1173, 1176,
  9462. 1185, 1188, 1280, 1282, 1285, 1288, 1290, 1297, 1300, 1302, 1305, 1312, 1317, 1320, 1345, 1348,
  9463. 1350, 1353, 1360, 1362, 1365, 1368, 1377, 1380, 1408, 1410, 1413, 1416, 1425, 1428, 1440, 1537,
  9464. 1540, 1542, 1545, 1552, 1557, 1600, 1605, 1608, 1617, 1620, 1632, 1665, 1668, 1680, 2048, 2050,
  9465. 2053, 2056, 2065, 2068, 2070, 2073, 2080, 2085, 2090, 2113, 2116, 2118, 2121, 2128, 2130, 2133,
  9466. 2136, 2145, 2148, 2176, 2181, 2196, 2218, 2305, 2308, 2320, 2322, 2325, 2328, 2337, 2368, 2373,
  9467. 2376, 2385, 2388, 2400, 2433, 2448, 2560, 2577, 2580, 2594, 2600, 2602, 2640, 2713, 4097, 4100,
  9468. 4102, 4105, 4112, 4114, 4117, 4120, 4129, 4132, 4134, 4160, 4162, 4165, 4168, 4177, 4180, 4182,
  9469. 4185, 4192, 4194, 4197, 4200, 4225, 4228, 4230, 4240, 4245, 4248, 4257, 4260, 4352, 4354, 4357,
  9470. 4360, 4362, 4369, 4372, 4374, 4377, 4384, 4386, 4389, 4392, 4417, 4420, 4422, 4425, 4432, 4434,
  9471. 4437, 4440, 4449, 4452, 4480, 4482, 4485, 4488, 4497, 4500, 4609, 4612, 4617, 4624, 4629, 4641,
  9472. 4644, 4672, 4677, 4689, 4692, 4737, 4740, 4752, 5120, 5122, 5125, 5128, 5137, 5140, 5142, 5145,
  9473. 5152, 5157, 5160, 5185, 5188, 5190, 5193, 5200, 5202, 5205, 5208, 5217, 5220, 5248, 5250, 5253,
  9474. 5256, 5265, 5268, 5280, 5377, 5380, 5382, 5385, 5392, 5394, 5397, 5400, 5409, 5412, 5440, 5442,
  9475. 5445, 5448, 5457, 5460, 5472, 5505, 5508, 5520, 5632, 5637, 5640, 5649, 5652, 5664, 5697, 5700,
  9476. 5712, 5760, 5802, 6145, 6148, 6150, 6153, 6160, 6165, 6168, 6177, 6208, 6210, 6213, 6216, 6225,
  9477. 6228, 6240, 6273, 6276, 6400, 6402, 6405, 6408, 6417, 6420, 6432, 6465, 6468, 6480, 6505, 6562,
  9478. 6660, 6672, 6720, 6742, 8192, 8194, 8197, 8200, 8209, 8212, 8214, 8217, 8224, 8229, 8234, 8257,
  9479. 8260, 8272, 8274, 8277, 8292, 8320, 8330, 8340, 8362, 8449, 8452, 8464, 8466, 8469, 8481, 8512,
  9480. 8514, 8517, 8529, 8532, 8544, 8577, 8580, 8592, 8704, 8714, 8738, 8744, 8746, 8772, 8784, 8840,
  9481. 8842, 8872, 9217, 9220, 9222, 9225, 9232, 9237, 9240, 9249, 9252, 9280, 9282, 9285, 9288, 9297,
  9482. 9300, 9312, 9345, 9348, 9360, 9472, 9477, 9480, 9489, 9492, 9504, 9537, 9540, 9552, 9574, 9600,
  9483. 9729, 9732, 9744, 9792, 9817, 10240, 10245, 10257, 10260, 10305, 10308, 10320, 10378, 10410, 10497, 10500,
  9484. 10512, 10645, 10762, 10786, 10852, 10888, 10890, 16385, 16388, 16390, 16393, 16400, 16402, 16405, 16408, 16410,
  9485. 16417, 16420, 16422, 16448, 16450, 16453, 16456, 16458, 16465, 16468, 16470, 16473, 16480, 16482, 16485, 16513,
  9486. 16516, 16528, 16533, 16536, 16545, 16548, 16640, 16642, 16645, 16648, 16657, 16660, 16662, 16665, 16672, 16674,
  9487. 16677, 16705, 16708, 16710, 16713, 16720, 16722, 16725, 16728, 16737, 16740, 16768, 16770, 16773, 16776, 16785,
  9488. 16788, 16800, 16897, 16900, 16912, 16914, 16917, 16920, 16932, 16960, 16965, 16968, 16977, 16980, 16992, 17025,
  9489. 17028, 17408, 17410, 17413, 17416, 17418, 17425, 17428, 17430, 17433, 17440, 17442, 17445, 17448, 17473, 17476,
  9490. 17478, 17481, 17488, 17490, 17493, 17496, 17505, 17508, 17536, 17538, 17541, 17544, 17553, 17556, 17568, 17665,
  9491. 17668, 17670, 17673, 17680, 17682, 17685, 17688, 17697, 17700, 17728, 17730, 17733, 17736, 17745, 17748, 17760,
  9492. 17770, 17793, 17796, 17808, 17920, 17922, 17925, 17928, 17937, 17940, 17952, 17985, 17988, 18000, 18048, 18085,
  9493. 18433, 18436, 18441, 18448, 18450, 18453, 18456, 18465, 18468, 18496, 18498, 18501, 18504, 18513, 18516, 18528,
  9494. 18564, 18576, 18688, 18690, 18693, 18696, 18705, 18708, 18720, 18753, 18756, 18768, 18816, 18838, 18945, 18948,
  9495. 18960, 19008, 20480, 20482, 20485, 20488, 20497, 20500, 20502, 20505, 20512, 20514, 20517, 20520, 20545, 20548,
  9496. 20550, 20553, 20560, 20562, 20565, 20568, 20577, 20580, 20608, 20610, 20613, 20616, 20625, 20628, 20737, 20740,
  9497. 20742, 20745, 20752, 20754, 20757, 20760, 20769, 20772, 20800, 20802, 20805, 20808, 20817, 20820, 20832, 20865,
  9498. 20868, 20880, 20992, 20997, 21000, 21009, 21012, 21024, 21057, 21060, 21072, 21097, 21120, 21505, 21508, 21510,
  9499. 21513, 21520, 21522, 21525, 21528, 21537, 21540, 21568, 21570, 21573, 21576, 21585, 21588, 21600, 21633, 21636,
  9500. 21648, 21760, 21762, 21765, 21768, 21777, 21780, 21792, 21825, 21828, 21840, 21888, 22017, 22020, 22032, 22054,
  9501. 22080, 22528, 22530, 22533, 22536, 22545, 22548, 22560, 22593, 22596, 22608, 22618, 22656, 22785, 22788, 22800,
  9502. 22848, 23040, 23065, 23173, 23208, 24577, 24580, 24582, 24592, 24594, 24597, 24600, 24609, 24612, 24640, 24645,
  9503. 24648, 24657, 24660, 24672, 24708, 24720, 24832, 24834, 24837, 24840, 24849, 24852, 24864, 24897, 24900, 24912,
  9504. 24960, 24985, 25092, 25104, 25152, 25174, 25249, 25600, 25605, 25608, 25617, 25620, 25632, 25665, 25668, 25680,
  9505. 25728, 25857, 25860, 25872, 25920, 25930, 25960, 26002, 26112, 26260, 26625, 26628, 26640, 26725, 26776, 26880,
  9506. 26922, 27202, 27297, 32768, 32770, 32773, 32776, 32785, 32788, 32793, 32800, 32805, 32833, 32836, 32848, 32850,
  9507. 32853, 32856, 32865, 32896, 32901, 32913, 32916, 33025, 33028, 33033, 33040, 33042, 33045, 33048, 33057, 33060,
  9508. 33088, 33090, 33093, 33096, 33105, 33108, 33153, 33156, 33168, 33193, 33280, 33285, 33290, 33297, 33300, 33345,
  9509. 33348, 33360, 33793, 33796, 33798, 33801, 33808, 33810, 33813, 33816, 33825, 33856, 33858, 33861, 33864, 33873,
  9510. 33876, 33888, 33921, 33924, 33936, 34048, 34050, 34053, 34056, 34065, 34068, 34080, 34113, 34116, 34128, 34176,
  9511. 34186, 34305, 34308, 34320, 34345, 34368, 34816, 34821, 34833, 34836, 34881, 34884, 34896, 34978, 35073, 35076,
  9512. 35136, 35173, 35362, 35416, 35418, 35458, 35490, 36865, 36868, 36873, 36880, 36882, 36885, 36888, 36900, 36928,
  9513. 36930, 36933, 36936, 36945, 36948, 36960, 36993, 36996, 37008, 37120, 37125, 37137, 37140, 37185, 37188, 37200,
  9514. 37210, 37377, 37380, 37392, 37440, 37542, 37888, 37890, 37893, 37896, 37905, 37908, 37920, 37953, 37956, 37968,
  9515. 38016, 38038, 38145, 38148, 38160, 38208, 38296, 38305, 38400, 38470, 38500, 38913, 38916, 38928, 38950, 38976,
  9516. 39081, 39168, 39241, 39250, 39568, 40960, 40965, 40970, 40980, 40994, 41002, 41025, 41028, 41040, 41122, 41130,
  9517. 41280, 41317, 41474, 41482, 41506, 41512, 41514, 41602, 41608, 41610, 41640, 41985, 41988, 42000, 42048, 42121,
  9518. 42148, 42240, 42265, 42577, 43018, 43048, 43170, 43348, 43398, 43528, 43530, 43552, 43554, 43560, 43656, 43690,
  9519. };
  9520. const int kmap_size = 43692;
  9521. //const int nwant = type == GGML_TYPE_IQ1_S ? 3 : 2;
  9522. const int nwant = type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M ? 3 : type == GGML_TYPE_IQ2_S ? 1 : 2;
  9523. const uint16_t * kgrid = type == GGML_TYPE_IQ2_XXS ? kgrid_2bit_256 :
  9524. type == GGML_TYPE_IQ2_XS ? kgrid_2bit_512 :
  9525. type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M ? kgrid_1bit_2048 : kgrid_2bit_1024;
  9526. uint64_t * kgrid_q2xs;
  9527. int * kmap_q2xs;
  9528. uint16_t * kneighbors_q2xs;
  9529. //printf("================================================================= %s(grid_size = %d)\n", __func__, grid_size);
  9530. uint64_t * the_grid = (uint64_t *)malloc(grid_size*sizeof(uint64_t));
  9531. for (int k = 0; k < grid_size; ++k) {
  9532. int8_t * pos = (int8_t *)(the_grid + k);
  9533. for (int i = 0; i < 8; ++i) {
  9534. int l = (kgrid[k] >> 2*i) & 0x3;
  9535. pos[i] = 2*l + 1;
  9536. }
  9537. }
  9538. kgrid_q2xs = the_grid;
  9539. iq2_data[gindex].grid = the_grid;
  9540. kmap_q2xs = (int *)malloc(kmap_size*sizeof(int));
  9541. iq2_data[gindex].map = kmap_q2xs;
  9542. for (int i = 0; i < kmap_size; ++i) kmap_q2xs[i] = -1;
  9543. uint64_t aux64;
  9544. uint8_t * aux8 = (uint8_t *)&aux64;
  9545. for (int i = 0; i < grid_size; ++i) {
  9546. aux64 = kgrid_q2xs[i];
  9547. uint16_t index = 0;
  9548. for (int k=0; k<8; ++k) {
  9549. uint16_t q = (aux8[k] - 1)/2;
  9550. index |= (q << 2*k);
  9551. }
  9552. kmap_q2xs[index] = i;
  9553. }
  9554. int8_t pos[8];
  9555. int * dist2 = (int *)malloc(2*grid_size*sizeof(int));
  9556. int num_neighbors = 0, num_not_in_map = 0;
  9557. for (int i = 0; i < kmap_size; ++i) {
  9558. if (kmap_q2xs[i] >= 0) continue;
  9559. ++num_not_in_map;
  9560. for (int k = 0; k < 8; ++k) {
  9561. int l = (i >> 2*k) & 0x3;
  9562. pos[k] = 2*l + 1;
  9563. }
  9564. for (int j = 0; j < grid_size; ++j) {
  9565. const int8_t * pg = (const int8_t *)(kgrid_q2xs + j);
  9566. int d2 = 0;
  9567. for (int k = 0; k < 8; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  9568. dist2[2*j+0] = d2;
  9569. dist2[2*j+1] = j;
  9570. }
  9571. qsort(dist2, grid_size, 2*sizeof(int), iq2_compare_func);
  9572. int n = 0; int d2 = dist2[0];
  9573. int nhave = 1;
  9574. for (int j = 0; j < grid_size; ++j) {
  9575. if (dist2[2*j] > d2) {
  9576. if (nhave == nwant) break;
  9577. d2 = dist2[2*j];
  9578. ++nhave;
  9579. }
  9580. ++n;
  9581. }
  9582. num_neighbors += n;
  9583. }
  9584. //printf("%s: %d neighbours in total\n", __func__, num_neighbors);
  9585. kneighbors_q2xs = (uint16_t *)malloc((num_neighbors + num_not_in_map)*sizeof(uint16_t));
  9586. iq2_data[gindex].neighbours = kneighbors_q2xs;
  9587. int counter = 0;
  9588. for (int i = 0; i < kmap_size; ++i) {
  9589. if (kmap_q2xs[i] >= 0) continue;
  9590. for (int k = 0; k < 8; ++k) {
  9591. int l = (i >> 2*k) & 0x3;
  9592. pos[k] = 2*l + 1;
  9593. }
  9594. for (int j = 0; j < grid_size; ++j) {
  9595. const int8_t * pg = (const int8_t *)(kgrid_q2xs + j);
  9596. int d2 = 0;
  9597. for (int k = 0; k < 8; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  9598. dist2[2*j+0] = d2;
  9599. dist2[2*j+1] = j;
  9600. }
  9601. qsort(dist2, grid_size, 2*sizeof(int), iq2_compare_func);
  9602. kmap_q2xs[i] = -(counter + 1);
  9603. int d2 = dist2[0];
  9604. uint16_t * start = &kneighbors_q2xs[counter++];
  9605. int n = 0, nhave = 1;
  9606. for (int j = 0; j < grid_size; ++j) {
  9607. if (dist2[2*j] > d2) {
  9608. if (nhave == nwant) break;
  9609. d2 = dist2[2*j];
  9610. ++nhave;
  9611. }
  9612. kneighbors_q2xs[counter++] = dist2[2*j+1];
  9613. ++n;
  9614. }
  9615. *start = n;
  9616. }
  9617. free(dist2);
  9618. }
  9619. void iq2xs_free_impl(enum ggml_type type) {
  9620. GGML_ASSERT(type == GGML_TYPE_IQ2_XXS || type == GGML_TYPE_IQ2_XS || type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M || type == GGML_TYPE_IQ2_S);
  9621. const int gindex = iq2_data_index(type);
  9622. if (iq2_data[gindex].grid) {
  9623. free(iq2_data[gindex].grid); iq2_data[gindex].grid = NULL;
  9624. free(iq2_data[gindex].map); iq2_data[gindex].map = NULL;
  9625. free(iq2_data[gindex].neighbours); iq2_data[gindex].neighbours = NULL;
  9626. }
  9627. }
  9628. static int iq2_find_best_neighbour(const uint16_t * restrict neighbours, const uint64_t * restrict grid,
  9629. const float * restrict xval, const float * restrict weight, float scale, int8_t * restrict L) {
  9630. int num_neighbors = neighbours[0];
  9631. GGML_ASSERT(num_neighbors > 0);
  9632. float best_d2 = FLT_MAX;
  9633. int grid_index = -1;
  9634. for (int j = 1; j <= num_neighbors; ++j) {
  9635. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  9636. float d2 = 0;
  9637. for (int i = 0; i < 8; ++i) {
  9638. float q = pg[i];
  9639. float diff = scale*q - xval[i];
  9640. d2 += weight[i]*diff*diff;
  9641. }
  9642. if (d2 < best_d2) {
  9643. best_d2 = d2; grid_index = neighbours[j];
  9644. }
  9645. }
  9646. GGML_ASSERT(grid_index >= 0);
  9647. const int8_t * pg = (const int8_t *)(grid + grid_index);
  9648. for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
  9649. return grid_index;
  9650. }
  9651. static void quantize_row_iq2_xxs_impl(const float * restrict x, void * restrict vy, int64_t n, const float * restrict quant_weights) {
  9652. const int gindex = iq2_data_index(GGML_TYPE_IQ2_XXS);
  9653. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  9654. const int * kmap_q2xs = iq2_data[gindex].map;
  9655. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  9656. GGML_ASSERT(quant_weights && "missing quantization weights");
  9657. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  9658. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  9659. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  9660. GGML_ASSERT(n%QK_K == 0);
  9661. const int kMaxQ = 3;
  9662. const int64_t nbl = n/QK_K;
  9663. block_iq2_xxs * y = vy;
  9664. float scales[QK_K/32];
  9665. float weight[32];
  9666. float xval[32];
  9667. int8_t L[32];
  9668. int8_t Laux[32];
  9669. float waux[32];
  9670. uint8_t block_signs[4];
  9671. uint32_t q2[2*(QK_K/32)];
  9672. for (int ibl = 0; ibl < nbl; ++ibl) {
  9673. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  9674. memset(q2, 0, QK_K/4);
  9675. float max_scale = 0;
  9676. const float * xbl = x + QK_K*ibl;
  9677. float sumx2 = 0;
  9678. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  9679. float sigma2 = sumx2/QK_K;
  9680. for (int ib = 0; ib < QK_K/32; ++ib) {
  9681. const float * xb = xbl + 32*ib;
  9682. const float * qw = quant_weights + QK_K*ibl + 32*ib;
  9683. for (int i = 0; i < 32; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  9684. for (int i = 0; i < 32; ++i) waux[i] = sqrtf(weight[i]);
  9685. for (int k = 0; k < 4; ++k) {
  9686. int nflip = 0;
  9687. uint8_t s = 0;
  9688. for (int i = 0; i < 8; ++i) {
  9689. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  9690. else {
  9691. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  9692. }
  9693. }
  9694. if (nflip%2) {
  9695. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  9696. for (int i = 1; i < 8; ++i) {
  9697. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  9698. if (ax < min) {
  9699. min = ax; imin = i;
  9700. }
  9701. }
  9702. xval[8*k+imin] = -xval[8*k+imin];
  9703. s ^= (1 << imin);
  9704. }
  9705. block_signs[k] = s & 127;
  9706. }
  9707. float max = xval[0];
  9708. for (int i = 1; i < 32; ++i) max = MAX(max, xval[i]);
  9709. if (max < GROUP_MAX_EPS) {
  9710. scales[ib] = 0;
  9711. memset(L, 0, 32);
  9712. continue;
  9713. }
  9714. float scale = make_qp_quants(32, kMaxQ+1, xval, (uint8_t*)L, weight);
  9715. float eff_max = scale*kMaxQ;
  9716. float best = 0;
  9717. for (int is = -6; is <= 6; ++is) {
  9718. float id = (2*kMaxQ-1+is*0.1f)/eff_max;
  9719. float this_scale = 1/id;
  9720. for (int k = 0; k < 4; ++k) {
  9721. for (int i = 0; i < 8; ++i) {
  9722. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  9723. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  9724. }
  9725. uint16_t u = 0;
  9726. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  9727. int grid_index = kmap_q2xs[u];
  9728. if (grid_index < 0) {
  9729. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  9730. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  9731. }
  9732. }
  9733. float sumqx = 0, sumq2 = 0;
  9734. for (int i = 0; i < 32; ++i) {
  9735. float w = weight[i];
  9736. float q = 2*Laux[i] + 1;
  9737. sumqx += w*xval[i]*q;
  9738. sumq2 += w*q*q;
  9739. }
  9740. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  9741. scale = sumqx/sumq2; best = scale*sumqx;
  9742. memcpy(L, Laux, 32);
  9743. }
  9744. }
  9745. if (scale > 0) {
  9746. float id = 1/scale;
  9747. for (int k = 0; k < 4; ++k) {
  9748. uint16_t u = 0;
  9749. for (int i = 0; i < 8; ++i) {
  9750. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  9751. l = MAX(0, MIN(kMaxQ-1, l));
  9752. u |= (l << 2*i);
  9753. }
  9754. int grid_index = kmap_q2xs[u];
  9755. if (grid_index < 0) {
  9756. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  9757. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  9758. }
  9759. const int8_t * pg = (const int8_t *)(kgrid_q2xs + grid_index);
  9760. for (int i = 0; i < 8; ++i) L[8*k+i] = (pg[i] - 1)/2;
  9761. }
  9762. float sumqx = 0, sumq2 = 0;
  9763. for (int i = 0; i < 32; ++i) {
  9764. float w = weight[i];
  9765. float q = 2*L[i] + 1;
  9766. sumqx += w*xval[i]*q;
  9767. sumq2 += w*q*q;
  9768. }
  9769. if (sumq2 > 0) scale = sumqx/sumq2;
  9770. }
  9771. if (scale < 0) {
  9772. // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
  9773. // and correspondingly flip quant signs.
  9774. scale = -scale;
  9775. for (int k = 0; k < 4; ++k) block_signs[k] = (~block_signs[k]) & 127;
  9776. }
  9777. for (int k = 0; k < 4; ++k) {
  9778. uint16_t u = 0;
  9779. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  9780. int grid_index = kmap_q2xs[u];
  9781. if (grid_index < 0) {
  9782. printf("Oops: found point %u not on grid:", u);
  9783. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  9784. printf("\n");
  9785. GGML_ASSERT(false);
  9786. }
  9787. q2[2*ib+0] |= ((uint32_t) grid_index << 8*k);
  9788. q2[2*ib+1] |= (block_signs[k] << 7*k);
  9789. }
  9790. GGML_ASSERT(scale >= 0);
  9791. scales[ib] = scale;
  9792. max_scale = MAX(max_scale, scale);
  9793. }
  9794. if (!max_scale) {
  9795. memset(y[ibl].qs, 0, QK_K/4);
  9796. continue;
  9797. }
  9798. float d = max_scale/31;
  9799. y[ibl].d = GGML_FP32_TO_FP16(d);
  9800. float id = 1/d;
  9801. for (int ib = 0; ib < QK_K/32; ++ib) {
  9802. int l = nearest_int(0.5f*(id*scales[ib]-1));
  9803. l = MAX(0, MIN(15, l));
  9804. q2[2*ib+1] |= ((uint32_t)l << 28);
  9805. }
  9806. memcpy(y[ibl].qs, q2, QK_K/4);
  9807. }
  9808. }
  9809. static void quantize_row_iq2_xs_impl(const float * restrict x, void * restrict vy, int64_t n, const float * restrict quant_weights) {
  9810. const int gindex = iq2_data_index(GGML_TYPE_IQ2_XS);
  9811. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  9812. const int * kmap_q2xs = iq2_data[gindex].map;
  9813. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  9814. GGML_ASSERT(quant_weights && "missing quantization weights");
  9815. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  9816. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  9817. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  9818. GGML_ASSERT(n%QK_K == 0);
  9819. const int kMaxQ = 3;
  9820. const int64_t nbl = n/QK_K;
  9821. block_iq2_xs * y = vy;
  9822. float scales[QK_K/16];
  9823. float weight[16];
  9824. float xval[16];
  9825. int8_t L[16];
  9826. int8_t Laux[16];
  9827. float waux[16];
  9828. bool is_on_grid[2];
  9829. bool is_on_grid_aux[2];
  9830. uint8_t block_signs[2];
  9831. uint16_t q2[2*(QK_K/16)];
  9832. for (int ibl = 0; ibl < nbl; ++ibl) {
  9833. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  9834. memset(q2, 0, QK_K/4);
  9835. memset(y[ibl].scales, 0, QK_K/32);
  9836. float max_scale = 0;
  9837. const float * xbl = x + QK_K*ibl;
  9838. float sumx2 = 0;
  9839. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  9840. float sigma2 = sumx2/QK_K;
  9841. for (int ib = 0; ib < QK_K/16; ++ib) {
  9842. const float * xb = xbl + 16*ib;
  9843. const float * qw = quant_weights + QK_K*ibl + 16*ib;
  9844. for (int i = 0; i < 16; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  9845. for (int i = 0; i < 16; ++i) waux[i] = sqrtf(weight[i]);
  9846. for (int k = 0; k < 2; ++k) {
  9847. int nflip = 0;
  9848. uint8_t s = 0;
  9849. for (int i = 0; i < 8; ++i) {
  9850. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  9851. else {
  9852. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  9853. }
  9854. }
  9855. if (nflip%2) {
  9856. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  9857. for (int i = 1; i < 8; ++i) {
  9858. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  9859. if (ax < min) {
  9860. min = ax; imin = i;
  9861. }
  9862. }
  9863. xval[8*k+imin] = -xval[8*k+imin];
  9864. s ^= (1 << imin);
  9865. }
  9866. block_signs[k] = s & 127;
  9867. }
  9868. float max = xval[0];
  9869. for (int i = 1; i < 16; ++i) max = MAX(max, xval[i]);
  9870. if (max < GROUP_MAX_EPS) {
  9871. scales[ib] = 0;
  9872. memset(L, 0, 16);
  9873. continue;
  9874. }
  9875. float best = 0;
  9876. float scale = max/(2*kMaxQ-1);
  9877. is_on_grid[0] = is_on_grid[1] = true;
  9878. for (int is = -9; is <= 9; ++is) {
  9879. float id = (2*kMaxQ-1+is*0.1f)/max;
  9880. float this_scale = 1/id;
  9881. for (int k = 0; k < 2; ++k) {
  9882. for (int i = 0; i < 8; ++i) {
  9883. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  9884. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  9885. }
  9886. uint16_t u = 0;
  9887. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  9888. int grid_index = kmap_q2xs[u];
  9889. is_on_grid_aux[k] = true;
  9890. if (grid_index < 0) {
  9891. is_on_grid_aux[k] = false;
  9892. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  9893. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  9894. }
  9895. }
  9896. float sumqx = 0, sumq2 = 0;
  9897. for (int i = 0; i < 16; ++i) {
  9898. float w = weight[i];
  9899. float q = 2*Laux[i] + 1;
  9900. sumqx += w*xval[i]*q;
  9901. sumq2 += w*q*q;
  9902. }
  9903. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  9904. scale = sumqx/sumq2; best = scale*sumqx;
  9905. for (int i = 0; i < 16; ++i) L[i] = Laux[i];
  9906. for (int k = 0; k < 2; ++k) is_on_grid[k] = is_on_grid_aux[k];
  9907. }
  9908. }
  9909. int n_not_ongrid = 0;
  9910. for (int k = 0; k < 2; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  9911. if (n_not_ongrid > 0 && scale > 0) {
  9912. float id = 1/scale;
  9913. for (int k = 0; k < 2; ++k) {
  9914. if (is_on_grid[k]) continue;
  9915. uint16_t u = 0;
  9916. for (int i = 0; i < 8; ++i) {
  9917. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  9918. l = MAX(0, MIN(kMaxQ-1, l));
  9919. u |= (l << 2*i);
  9920. L[8*k + i] = l;
  9921. }
  9922. int grid_index = kmap_q2xs[u];
  9923. if (grid_index < 0) {
  9924. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  9925. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  9926. }
  9927. }
  9928. float sumqx = 0, sumq2 = 0;
  9929. for (int i = 0; i < 16; ++i) {
  9930. float w = weight[i];
  9931. float q = 2*L[i] + 1;
  9932. sumqx += w*xval[i]*q;
  9933. sumq2 += w*q*q;
  9934. }
  9935. if (sumq2 > 0) scale = sumqx/sumq2;
  9936. }
  9937. if (scale < 0) {
  9938. scale = -scale;
  9939. for (int k = 0; k < 2; ++k) block_signs[k] = (~block_signs[k]) & 127;
  9940. }
  9941. for (int k = 0; k < 2; ++k) {
  9942. uint16_t u = 0;
  9943. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  9944. int grid_index = kmap_q2xs[u];
  9945. if (grid_index < 0) {
  9946. printf("Oops: found point %u not on grid:", u);
  9947. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  9948. printf("\n");
  9949. GGML_ASSERT(false);
  9950. }
  9951. q2[2*ib+k] = grid_index | (block_signs[k] << 9);
  9952. }
  9953. GGML_ASSERT(scale >= 0);
  9954. scales[ib] = scale;
  9955. max_scale = MAX(max_scale, scale);
  9956. }
  9957. if (!max_scale) {
  9958. memset(y[ibl].qs, 0, QK_K/4);
  9959. continue;
  9960. }
  9961. float d = max_scale/31;
  9962. y[ibl].d = GGML_FP32_TO_FP16(d);
  9963. float id = 1/d;
  9964. for (int ib = 0; ib < QK_K/16; ++ib) {
  9965. int l = nearest_int(0.5f*(id*scales[ib]-1));
  9966. l = MAX(0, MIN(15, l));
  9967. if (ib%2 == 0) y[ibl].scales[ib/2] = l;
  9968. else y[ibl].scales[ib/2] |= (l << 4);
  9969. }
  9970. memcpy(y[ibl].qs, q2, QK_K/4);
  9971. }
  9972. }
  9973. size_t quantize_iq2_xxs(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  9974. GGML_ASSERT(n_per_row%QK_K == 0);
  9975. int64_t nblock = n_per_row/QK_K;
  9976. char * qrow = (char *)dst;
  9977. for (int64_t row = 0; row < nrow; ++row) {
  9978. quantize_row_iq2_xxs_impl(src, qrow, n_per_row, quant_weights);
  9979. src += n_per_row;
  9980. qrow += nblock*sizeof(block_iq2_xxs);
  9981. }
  9982. return nrow * nblock * sizeof(block_iq2_xxs);
  9983. }
  9984. size_t quantize_iq2_xs(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  9985. GGML_ASSERT(n_per_row%QK_K == 0);
  9986. int64_t nblock = n_per_row/QK_K;
  9987. char * qrow = (char *)dst;
  9988. for (int64_t row = 0; row < nrow; ++row) {
  9989. quantize_row_iq2_xs_impl(src, qrow, n_per_row, quant_weights);
  9990. src += n_per_row;
  9991. qrow += nblock*sizeof(block_iq2_xs);
  9992. }
  9993. return nrow * nblock * sizeof(block_iq2_xs);
  9994. }
  9995. //
  9996. // ============================================= 3-bit using D4 lattice
  9997. //
  9998. typedef struct {
  9999. uint32_t * grid;
  10000. int * map;
  10001. uint16_t * neighbours;
  10002. } iq3_entry_t;
  10003. static iq3_entry_t iq3_data[2] = {
  10004. {NULL, NULL, NULL},
  10005. {NULL, NULL, NULL},
  10006. };
  10007. static inline int iq3_data_index(int grid_size) {
  10008. (void)grid_size;
  10009. GGML_ASSERT(grid_size == 256 || grid_size == 512);
  10010. return grid_size == 256 ? 0 : 1;
  10011. }
  10012. static int iq3_compare_func(const void * left, const void * right) {
  10013. const int * l = (const int *)left;
  10014. const int * r = (const int *)right;
  10015. return l[0] < r[0] ? -1 : l[0] > r[0] ? 1 : l[1] < r[1] ? -1 : l[1] > r[1] ? 1 : 0;
  10016. }
  10017. void iq3xs_init_impl(int grid_size) {
  10018. const int gindex = iq3_data_index(grid_size);
  10019. if (iq3_data[gindex].grid) {
  10020. return;
  10021. }
  10022. static const uint16_t kgrid_256[256] = {
  10023. 0, 2, 4, 9, 11, 15, 16, 18, 25, 34, 59, 61, 65, 67, 72, 74,
  10024. 81, 85, 88, 90, 97, 108, 120, 128, 130, 132, 137, 144, 146, 153, 155, 159,
  10025. 169, 175, 189, 193, 199, 200, 202, 213, 248, 267, 287, 292, 303, 315, 317, 321,
  10026. 327, 346, 362, 413, 436, 456, 460, 462, 483, 497, 513, 515, 520, 522, 529, 531,
  10027. 536, 538, 540, 551, 552, 576, 578, 585, 592, 594, 641, 643, 648, 650, 657, 664,
  10028. 698, 704, 706, 720, 729, 742, 758, 769, 773, 808, 848, 852, 870, 889, 901, 978,
  10029. 992, 1024, 1026, 1033, 1035, 1040, 1042, 1046, 1049, 1058, 1089, 1091, 1093, 1096, 1098, 1105,
  10030. 1112, 1139, 1143, 1144, 1152, 1154, 1161, 1167, 1168, 1170, 1183, 1184, 1197, 1217, 1224, 1228,
  10031. 1272, 1276, 1309, 1323, 1347, 1367, 1377, 1404, 1473, 1475, 1486, 1509, 1537, 1544, 1546, 1553,
  10032. 1555, 1576, 1589, 1594, 1600, 1602, 1616, 1625, 1636, 1638, 1665, 1667, 1672, 1685, 1706, 1722,
  10033. 1737, 1755, 1816, 1831, 1850, 1856, 1862, 1874, 1901, 1932, 1950, 1971, 2011, 2032, 2052, 2063,
  10034. 2077, 2079, 2091, 2095, 2172, 2192, 2207, 2208, 2224, 2230, 2247, 2277, 2308, 2345, 2356, 2389,
  10035. 2403, 2424, 2501, 2504, 2506, 2520, 2570, 2593, 2616, 2624, 2630, 2646, 2669, 2700, 2714, 2746,
  10036. 2754, 2795, 2824, 2835, 2839, 2874, 2882, 2905, 2984, 3028, 3042, 3092, 3108, 3110, 3124, 3153,
  10037. 3185, 3215, 3252, 3288, 3294, 3364, 3397, 3434, 3483, 3523, 3537, 3587, 3589, 3591, 3592, 3610,
  10038. 3626, 3670, 3680, 3722, 3749, 3754, 3776, 3789, 3803, 3824, 3857, 3873, 3904, 3906, 3924, 3992,
  10039. };
  10040. static const uint16_t kgrid_512[512] = {
  10041. 0, 1, 2, 5, 7, 8, 9, 10, 12, 14, 16, 17, 21, 27, 32, 34,
  10042. 37, 39, 41, 43, 48, 50, 57, 60, 63, 64, 65, 66, 68, 72, 73, 77,
  10043. 80, 83, 87, 89, 93, 100, 113, 117, 122, 128, 129, 133, 135, 136, 139, 142,
  10044. 145, 149, 152, 156, 162, 165, 167, 169, 171, 184, 187, 195, 201, 205, 208, 210,
  10045. 217, 219, 222, 228, 232, 234, 247, 249, 253, 256, 267, 271, 273, 276, 282, 288,
  10046. 291, 297, 312, 322, 324, 336, 338, 342, 347, 353, 357, 359, 374, 379, 390, 393,
  10047. 395, 409, 426, 441, 448, 450, 452, 464, 466, 470, 475, 488, 492, 512, 513, 514,
  10048. 516, 520, 521, 523, 525, 527, 528, 530, 537, 540, 542, 556, 558, 561, 570, 576,
  10049. 577, 579, 582, 584, 588, 593, 600, 603, 609, 616, 618, 632, 638, 640, 650, 653,
  10050. 655, 656, 660, 666, 672, 675, 685, 688, 698, 705, 708, 711, 712, 715, 721, 727,
  10051. 728, 732, 737, 754, 760, 771, 773, 778, 780, 793, 795, 802, 806, 808, 812, 833,
  10052. 840, 843, 849, 856, 858, 873, 912, 916, 919, 932, 934, 961, 963, 968, 970, 977,
  10053. 989, 993, 1010, 1016, 1024, 1025, 1027, 1029, 1031, 1032, 1034, 1036, 1038, 1041, 1043, 1047,
  10054. 1048, 1050, 1057, 1059, 1061, 1064, 1066, 1079, 1080, 1083, 1085, 1088, 1090, 1096, 1099, 1103,
  10055. 1106, 1109, 1113, 1116, 1122, 1129, 1153, 1156, 1159, 1169, 1171, 1176, 1183, 1185, 1195, 1199,
  10056. 1209, 1212, 1216, 1218, 1221, 1225, 1234, 1236, 1241, 1243, 1250, 1256, 1270, 1281, 1287, 1296,
  10057. 1299, 1306, 1309, 1313, 1338, 1341, 1348, 1353, 1362, 1375, 1376, 1387, 1400, 1408, 1410, 1415,
  10058. 1425, 1453, 1457, 1477, 1481, 1494, 1496, 1507, 1512, 1538, 1545, 1547, 1549, 1551, 1554, 1561,
  10059. 1563, 1565, 1570, 1572, 1575, 1577, 1587, 1593, 1601, 1603, 1605, 1612, 1617, 1619, 1632, 1648,
  10060. 1658, 1662, 1664, 1674, 1680, 1690, 1692, 1704, 1729, 1736, 1740, 1745, 1747, 1751, 1752, 1761,
  10061. 1763, 1767, 1773, 1787, 1795, 1801, 1806, 1810, 1817, 1834, 1840, 1844, 1857, 1864, 1866, 1877,
  10062. 1882, 1892, 1902, 1915, 1934, 1953, 1985, 1987, 2000, 2002, 2013, 2048, 2052, 2058, 2064, 2068,
  10063. 2071, 2074, 2081, 2088, 2104, 2114, 2119, 2121, 2123, 2130, 2136, 2141, 2147, 2153, 2157, 2177,
  10064. 2179, 2184, 2189, 2193, 2203, 2208, 2223, 2226, 2232, 2244, 2249, 2251, 2256, 2258, 2265, 2269,
  10065. 2304, 2306, 2324, 2335, 2336, 2361, 2373, 2375, 2385, 2418, 2443, 2460, 2480, 2504, 2509, 2520,
  10066. 2531, 2537, 2562, 2568, 2572, 2578, 2592, 2596, 2599, 2602, 2614, 2620, 2625, 2627, 2629, 2634,
  10067. 2641, 2650, 2682, 2688, 2697, 2707, 2712, 2718, 2731, 2754, 2759, 2760, 2775, 2788, 2793, 2805,
  10068. 2811, 2817, 2820, 2832, 2842, 2854, 2890, 2902, 2921, 2923, 2978, 3010, 3012, 3026, 3081, 3083,
  10069. 3085, 3097, 3099, 3120, 3136, 3152, 3159, 3188, 3210, 3228, 3234, 3245, 3250, 3256, 3264, 3276,
  10070. 3281, 3296, 3349, 3363, 3378, 3392, 3395, 3420, 3440, 3461, 3488, 3529, 3531, 3584, 3588, 3591,
  10071. 3600, 3602, 3614, 3616, 3628, 3634, 3650, 3657, 3668, 3683, 3685, 3713, 3716, 3720, 3726, 3729,
  10072. 3736, 3753, 3778, 3802, 3805, 3819, 3841, 3845, 3851, 3856, 3880, 3922, 3938, 3970, 3993, 4032,
  10073. };
  10074. const int kmap_size = 4096;
  10075. const int nwant = grid_size == 256 ? 2 : 3;
  10076. const uint16_t * kgrid = grid_size == 256 ? kgrid_256 : kgrid_512;
  10077. uint32_t * kgrid_q3xs;
  10078. int * kmap_q3xs;
  10079. uint16_t * kneighbors_q3xs;
  10080. //printf("================================================================= %s(grid_size = %d)\n", __func__, grid_size);
  10081. uint32_t * the_grid = (uint32_t *)malloc(grid_size*sizeof(uint32_t));
  10082. for (int k = 0; k < grid_size; ++k) {
  10083. int8_t * pos = (int8_t *)(the_grid + k);
  10084. for (int i = 0; i < 4; ++i) {
  10085. int l = (kgrid[k] >> 3*i) & 0x7;
  10086. pos[i] = 2*l + 1;
  10087. }
  10088. }
  10089. kgrid_q3xs = the_grid;
  10090. iq3_data[gindex].grid = the_grid;
  10091. kmap_q3xs = (int *)malloc(kmap_size*sizeof(int));
  10092. iq3_data[gindex].map = kmap_q3xs;
  10093. for (int i = 0; i < kmap_size; ++i) kmap_q3xs[i] = -1;
  10094. uint32_t aux32;
  10095. uint8_t * aux8 = (uint8_t *)&aux32;
  10096. for (int i = 0; i < grid_size; ++i) {
  10097. aux32 = kgrid_q3xs[i];
  10098. uint16_t index = 0;
  10099. for (int k=0; k<4; ++k) {
  10100. uint16_t q = (aux8[k] - 1)/2;
  10101. index |= (q << 3*k);
  10102. }
  10103. kmap_q3xs[index] = i;
  10104. }
  10105. int8_t pos[4];
  10106. int * dist2 = (int *)malloc(2*grid_size*sizeof(int));
  10107. int num_neighbors = 0, num_not_in_map = 0;
  10108. for (int i = 0; i < kmap_size; ++i) {
  10109. if (kmap_q3xs[i] >= 0) continue;
  10110. ++num_not_in_map;
  10111. for (int k = 0; k < 4; ++k) {
  10112. int l = (i >> 3*k) & 0x7;
  10113. pos[k] = 2*l + 1;
  10114. }
  10115. for (int j = 0; j < grid_size; ++j) {
  10116. const int8_t * pg = (const int8_t *)(kgrid_q3xs + j);
  10117. int d2 = 0;
  10118. for (int k = 0; k < 4; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  10119. dist2[2*j+0] = d2;
  10120. dist2[2*j+1] = j;
  10121. }
  10122. qsort(dist2, grid_size, 2*sizeof(int), iq3_compare_func);
  10123. int n = 0; int d2 = dist2[0];
  10124. int nhave = 1;
  10125. for (int j = 0; j < grid_size; ++j) {
  10126. if (dist2[2*j] > d2) {
  10127. if (nhave == nwant) break;
  10128. d2 = dist2[2*j];
  10129. ++nhave;
  10130. }
  10131. ++n;
  10132. }
  10133. num_neighbors += n;
  10134. }
  10135. //printf("%s: %d neighbours in total\n", __func__, num_neighbors);
  10136. kneighbors_q3xs = (uint16_t *)malloc((num_neighbors + num_not_in_map)*sizeof(uint16_t));
  10137. iq3_data[gindex].neighbours = kneighbors_q3xs;
  10138. int counter = 0;
  10139. for (int i = 0; i < kmap_size; ++i) {
  10140. if (kmap_q3xs[i] >= 0) continue;
  10141. for (int k = 0; k < 4; ++k) {
  10142. int l = (i >> 3*k) & 0x7;
  10143. pos[k] = 2*l + 1;
  10144. }
  10145. for (int j = 0; j < grid_size; ++j) {
  10146. const int8_t * pg = (const int8_t *)(kgrid_q3xs + j);
  10147. int d2 = 0;
  10148. for (int k = 0; k < 4; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  10149. dist2[2*j+0] = d2;
  10150. dist2[2*j+1] = j;
  10151. }
  10152. qsort(dist2, grid_size, 2*sizeof(int), iq3_compare_func);
  10153. kmap_q3xs[i] = -(counter + 1);
  10154. int d2 = dist2[0];
  10155. uint16_t * start = &kneighbors_q3xs[counter++];
  10156. int n = 0, nhave = 1;
  10157. for (int j = 0; j < grid_size; ++j) {
  10158. if (dist2[2*j] > d2) {
  10159. if (nhave == nwant) break;
  10160. d2 = dist2[2*j];
  10161. ++nhave;
  10162. }
  10163. kneighbors_q3xs[counter++] = dist2[2*j+1];
  10164. ++n;
  10165. }
  10166. *start = n;
  10167. }
  10168. free(dist2);
  10169. }
  10170. void iq3xs_free_impl(int grid_size) {
  10171. GGML_ASSERT(grid_size == 256 || grid_size == 512);
  10172. const int gindex = iq3_data_index(grid_size);
  10173. if (iq3_data[gindex].grid) {
  10174. free(iq3_data[gindex].grid); iq3_data[gindex].grid = NULL;
  10175. free(iq3_data[gindex].map); iq3_data[gindex].map = NULL;
  10176. free(iq3_data[gindex].neighbours); iq3_data[gindex].neighbours = NULL;
  10177. }
  10178. }
  10179. static int iq3_find_best_neighbour(const uint16_t * restrict neighbours, const uint32_t * restrict grid,
  10180. const float * restrict xval, const float * restrict weight, float scale, int8_t * restrict L) {
  10181. int num_neighbors = neighbours[0];
  10182. GGML_ASSERT(num_neighbors > 0);
  10183. float best_d2 = FLT_MAX;
  10184. int grid_index = -1;
  10185. for (int j = 1; j <= num_neighbors; ++j) {
  10186. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  10187. float d2 = 0;
  10188. for (int i = 0; i < 4; ++i) {
  10189. float q = pg[i];
  10190. float diff = scale*q - xval[i];
  10191. d2 += weight[i]*diff*diff;
  10192. }
  10193. if (d2 < best_d2) {
  10194. best_d2 = d2; grid_index = neighbours[j];
  10195. }
  10196. }
  10197. GGML_ASSERT(grid_index >= 0);
  10198. const int8_t * pg = (const int8_t *)(grid + grid_index);
  10199. for (int i = 0; i < 4; ++i) L[i] = (pg[i] - 1)/2;
  10200. return grid_index;
  10201. }
  10202. static void quantize_row_iq3_xxs_impl(int grid_size, const float * restrict x, void * restrict vy, int64_t n,
  10203. const float * restrict quant_weights) {
  10204. const int gindex = iq3_data_index(grid_size);
  10205. const uint32_t * kgrid_q3xs = iq3_data[gindex].grid;
  10206. const int * kmap_q3xs = iq3_data[gindex].map;
  10207. const uint16_t * kneighbors_q3xs = iq3_data[gindex].neighbours;
  10208. //GGML_ASSERT(quant_weights && "missing quantization weights");
  10209. GGML_ASSERT(kgrid_q3xs && "forgot to call ggml_quantize_init()?");
  10210. GGML_ASSERT(kmap_q3xs && "forgot to call ggml_quantize_init()?");
  10211. GGML_ASSERT(kneighbors_q3xs && "forgot to call ggml_quantize_init()?");
  10212. GGML_ASSERT(n%QK_K == 0);
  10213. const int kMaxQ = 8;
  10214. const int64_t nbl = n/QK_K;
  10215. ggml_fp16_t * dh;
  10216. uint8_t * qs;
  10217. int block_size;
  10218. if (grid_size == 256) {
  10219. block_iq3_xxs * y = vy;
  10220. dh = &y->d;
  10221. qs = y->qs;
  10222. block_size = sizeof(block_iq3_xxs);
  10223. } else {
  10224. block_iq3_s * y = vy;
  10225. dh = &y->d;
  10226. qs = y->qs;
  10227. block_size = sizeof(block_iq3_s);
  10228. }
  10229. int quant_size = block_size - sizeof(ggml_fp16_t);
  10230. float scales[QK_K/32];
  10231. float weight[32];
  10232. float xval[32];
  10233. int8_t L[32];
  10234. int8_t Laux[32];
  10235. float waux[32];
  10236. bool is_on_grid[8];
  10237. bool is_on_grid_aux[8];
  10238. uint8_t block_signs[8];
  10239. uint8_t q3[3*(QK_K/8)+QK_K/32];
  10240. uint32_t * scales_and_signs = (uint32_t *)(q3 + QK_K/4);
  10241. uint8_t * qh = q3 + 3*(QK_K/8);
  10242. for (int ibl = 0; ibl < nbl; ++ibl) {
  10243. dh[0] = GGML_FP32_TO_FP16(0.f);
  10244. memset(q3, 0, 3*QK_K/8+QK_K/32);
  10245. float max_scale = 0;
  10246. const float * xbl = x + QK_K*ibl;
  10247. float sumx2 = 0;
  10248. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  10249. float sigma2 = 2*sumx2/QK_K;
  10250. for (int ib = 0; ib < QK_K/32; ++ib) {
  10251. const float * xb = xbl + 32*ib;
  10252. if (quant_weights) {
  10253. const float * qw = quant_weights + QK_K*ibl + 32*ib;
  10254. for (int i = 0; i < 32; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  10255. } else {
  10256. for (int i = 0; i < 32; ++i) weight[i] = xb[i]*xb[i];
  10257. }
  10258. for (int i = 0; i < 32; ++i) waux[i] = sqrtf(weight[i]);
  10259. for (int k = 0; k < 4; ++k) {
  10260. int nflip = 0;
  10261. uint8_t s = 0;
  10262. for (int i = 0; i < 8; ++i) {
  10263. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  10264. else {
  10265. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  10266. }
  10267. }
  10268. if (nflip%2) {
  10269. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  10270. for (int i = 1; i < 8; ++i) {
  10271. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  10272. if (ax < min) {
  10273. min = ax; imin = i;
  10274. }
  10275. }
  10276. xval[8*k+imin] = -xval[8*k+imin];
  10277. s ^= (1 << imin);
  10278. }
  10279. block_signs[k] = s & 127;
  10280. }
  10281. float max = xval[0];
  10282. for (int i = 1; i < 32; ++i) max = MAX(max, xval[i]);
  10283. if (max < GROUP_MAX_EPS_IQ3_XXS) {
  10284. scales[ib] = 0;
  10285. memset(L, 0, 32);
  10286. continue;
  10287. }
  10288. float best = 0;
  10289. float scale = max/(2*kMaxQ-1);
  10290. for (int is = -15; is <= 15; ++is) {
  10291. float id = (2*kMaxQ-1+is*0.2f)/max;
  10292. float this_scale = 1/id;
  10293. for (int k = 0; k < 8; ++k) {
  10294. for (int i = 0; i < 4; ++i) {
  10295. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  10296. Laux[4*k+i] = MAX(0, MIN(kMaxQ-1, l));
  10297. }
  10298. uint16_t u = 0;
  10299. for (int i = 0; i < 4; ++i) u |= (Laux[4*k+i] << 3*i);
  10300. int grid_index = kmap_q3xs[u];
  10301. is_on_grid_aux[k] = true;
  10302. if (grid_index < 0) {
  10303. is_on_grid_aux[k] = false;
  10304. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  10305. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, this_scale, Laux + 4*k);
  10306. }
  10307. }
  10308. float sumqx = 0, sumq2 = 0;
  10309. for (int i = 0; i < 32; ++i) {
  10310. float w = weight[i];
  10311. float q = 2*Laux[i] + 1;
  10312. sumqx += w*xval[i]*q;
  10313. sumq2 += w*q*q;
  10314. }
  10315. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  10316. scale = sumqx/sumq2; best = scale*sumqx;
  10317. for (int i = 0; i < 32; ++i) L[i] = Laux[i];
  10318. for (int k = 0; k < 8; ++k) is_on_grid[k] = is_on_grid_aux[k];
  10319. }
  10320. }
  10321. int n_not_ongrid = 0;
  10322. for (int k = 0; k < 8; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  10323. if (n_not_ongrid > 0 && scale > 0) {
  10324. float id = 1/scale;
  10325. for (int k = 0; k < 8; ++k) {
  10326. if (is_on_grid[k]) continue;
  10327. uint16_t u = 0;
  10328. for (int i = 0; i < 4; ++i) {
  10329. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  10330. l = MAX(0, MIN(kMaxQ-1, l));
  10331. u |= (l << 3*i);
  10332. }
  10333. int grid_index = kmap_q3xs[u];
  10334. if (grid_index < 0) {
  10335. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  10336. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, scale, L + 4*k);
  10337. }
  10338. const int8_t * pg = (const int8_t *)(kgrid_q3xs + grid_index);
  10339. for (int i = 0; i < 4; ++i) L[4*k+i] = (pg[i] - 1)/2;
  10340. }
  10341. float sumqx = 0, sumq2 = 0;
  10342. for (int i = 0; i < 32; ++i) {
  10343. float w = weight[i];
  10344. float q = 2*L[i] + 1;
  10345. sumqx += w*xval[i]*q;
  10346. sumq2 += w*q*q;
  10347. }
  10348. if (sumq2 > 0) scale = sumqx/sumq2;
  10349. }
  10350. if (scale < 0) {
  10351. // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
  10352. // and correspondingly flip quant signs.
  10353. scale = -scale;
  10354. for (int k = 0; k < 4; ++k) block_signs[k] = (~block_signs[k]) & 127;
  10355. }
  10356. for (int k = 0; k < 8; ++k) {
  10357. uint16_t u = 0;
  10358. for (int i = 0; i < 4; ++i) u |= (L[4*k+i] << 3*i);
  10359. int grid_index = kmap_q3xs[u];
  10360. if (grid_index < 0) {
  10361. printf("Oops: found point %u not on grid:", u);
  10362. for (int i = 0; i < 4; ++i) printf(" %d", L[4*k+i]);
  10363. printf("\n");
  10364. GGML_ASSERT(false);
  10365. }
  10366. if (grid_size == 256) {
  10367. q3[8*ib+k] = grid_index;
  10368. } else {
  10369. q3[8*ib+k] = grid_index & 255;
  10370. qh[ib] |= ((grid_index >> 8) << k);
  10371. }
  10372. }
  10373. scales_and_signs[ib] = block_signs[0] | (block_signs[1] << 7) | (block_signs[2] << 14) | (block_signs[3] << 21);
  10374. GGML_ASSERT(scale >= 0);
  10375. scales[ib] = scale;
  10376. max_scale = MAX(max_scale, scale);
  10377. }
  10378. if (!max_scale) {
  10379. memset(qs, 0, quant_size);
  10380. dh += block_size/sizeof(ggml_fp16_t);
  10381. qs += block_size;
  10382. continue;
  10383. }
  10384. float d = max_scale/31;
  10385. dh[0] = GGML_FP32_TO_FP16(d * 1.0125f); // small improvement via this fudge factor
  10386. float id = 1/d;
  10387. for (int ib = 0; ib < QK_K/32; ++ib) {
  10388. int l = nearest_int(0.5f*(id*scales[ib]-1));
  10389. l = MAX(0, MIN(15, l));
  10390. scales_and_signs[ib] |= ((uint32_t)l << 28);
  10391. }
  10392. memcpy(qs, q3, quant_size);
  10393. dh += block_size/sizeof(ggml_fp16_t);
  10394. qs += block_size;
  10395. }
  10396. }
  10397. size_t quantize_iq3_xxs(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  10398. GGML_ASSERT(n_per_row%QK_K == 0);
  10399. int64_t nblock = n_per_row/QK_K;
  10400. char * qrow = (char *)dst;
  10401. for (int64_t row = 0; row < nrow; ++row) {
  10402. quantize_row_iq3_xxs_impl(256, src, qrow, n_per_row, quant_weights);
  10403. src += n_per_row;
  10404. qrow += nblock*sizeof(block_iq3_xxs);
  10405. }
  10406. return nrow * nblock * sizeof(block_iq3_xxs);
  10407. }
  10408. void quantize_row_iq3_xxs(const float * restrict x, void * restrict vy, int64_t k) {
  10409. assert(k % QK_K == 0);
  10410. block_iq3_xxs * restrict y = vy;
  10411. quantize_row_iq3_xxs_reference(x, y, k);
  10412. }
  10413. void quantize_row_iq3_xxs_reference(const float * restrict x, block_iq3_xxs * restrict y, int64_t k) {
  10414. assert(k % QK_K == 0);
  10415. quantize_row_iq3_xxs_impl(256, x, y, k, NULL);
  10416. }
  10417. static void quantize_row_iq3_s_impl(int block_size, const float * restrict x, void * restrict vy, int n,
  10418. const float * restrict quant_weights,
  10419. float * scales,
  10420. float * weight,
  10421. float * xval,
  10422. int8_t * L,
  10423. int8_t * Laux,
  10424. float * waux,
  10425. bool * is_on_grid,
  10426. bool * is_on_grid_aux,
  10427. uint8_t * block_signs) {
  10428. const int gindex = iq3_data_index(512);
  10429. const uint32_t * kgrid_q3xs = iq3_data[gindex].grid;
  10430. const int * kmap_q3xs = iq3_data[gindex].map;
  10431. const uint16_t * kneighbors_q3xs = iq3_data[gindex].neighbours;
  10432. //GGML_ASSERT(quant_weights && "missing quantization weights");
  10433. GGML_ASSERT(kgrid_q3xs && "forgot to call ggml_quantize_init()?");
  10434. GGML_ASSERT(kmap_q3xs && "forgot to call ggml_quantize_init()?");
  10435. GGML_ASSERT(kneighbors_q3xs && "forgot to call ggml_quantize_init()?");
  10436. GGML_ASSERT(n%QK_K == 0);
  10437. const int kMaxQ = 8;
  10438. const int64_t nbl = n/QK_K;
  10439. block_iq3_s * y = vy;
  10440. const int bs4 = block_size/4;
  10441. const int bs8 = block_size/8;
  10442. for (int ibl = 0; ibl < nbl; ++ibl) {
  10443. memset(&y[ibl], 0, sizeof(block_iq3_s));
  10444. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  10445. uint8_t * qs = y[ibl].qs;
  10446. uint8_t * qh = y[ibl].qh;
  10447. uint8_t * signs = y[ibl].signs;
  10448. float max_scale = 0;
  10449. const float * xbl = x + QK_K*ibl;
  10450. float sumx2 = 0;
  10451. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  10452. float sigma2 = 2*sumx2/QK_K;
  10453. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  10454. const float * xb = xbl + block_size*ib;
  10455. if (quant_weights) {
  10456. const float * qw = quant_weights + QK_K*ibl + block_size*ib;
  10457. for (int i = 0; i < block_size; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  10458. } else {
  10459. for (int i = 0; i < block_size; ++i) weight[i] = xb[i]*xb[i];
  10460. }
  10461. for (int i = 0; i < block_size; ++i) waux[i] = sqrtf(weight[i]);
  10462. for (int k = 0; k < bs8; ++k) {
  10463. uint8_t s = 0;
  10464. for (int i = 0; i < 8; ++i) {
  10465. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  10466. else {
  10467. xval[8*k + i] = -xb[8*k + i]; s |= (1 << i);
  10468. }
  10469. }
  10470. block_signs[k] = s;
  10471. }
  10472. float max = xval[0];
  10473. for (int i = 1; i < block_size; ++i) max = MAX(max, xval[i]);
  10474. if (!max) {
  10475. scales[ib] = 0;
  10476. continue;
  10477. }
  10478. float best = 0;
  10479. float scale = max/(2*kMaxQ-1);
  10480. for (int k = 0; k < bs4; ++k) is_on_grid[k] = false;
  10481. for (int is = -9; is <= 9; ++is) {
  10482. float id = (2*kMaxQ-1+is*0.2f)/max;
  10483. float this_scale = 1/id;
  10484. for (int k = 0; k < bs4; ++k) {
  10485. for (int i = 0; i < 4; ++i) {
  10486. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  10487. Laux[4*k+i] = MAX(0, MIN(kMaxQ-1, l));
  10488. }
  10489. uint16_t u = 0;
  10490. for (int i = 0; i < 4; ++i) u |= (Laux[4*k+i] << 3*i);
  10491. int grid_index = kmap_q3xs[u];
  10492. is_on_grid_aux[k] = true;
  10493. if (grid_index < 0) {
  10494. is_on_grid_aux[k] = false;
  10495. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  10496. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, this_scale, Laux + 4*k);
  10497. }
  10498. }
  10499. float sumqx = 0, sumq2 = 0;
  10500. for (int i = 0; i < block_size; ++i) {
  10501. float w = weight[i];
  10502. float q = 2*Laux[i] + 1;
  10503. sumqx += w*xval[i]*q;
  10504. sumq2 += w*q*q;
  10505. }
  10506. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  10507. scale = sumqx/sumq2; best = scale*sumqx;
  10508. for (int i = 0; i < block_size; ++i) L[i] = Laux[i];
  10509. for (int k = 0; k < bs4; ++k) is_on_grid[k] = is_on_grid_aux[k];
  10510. }
  10511. }
  10512. int n_not_ongrid = 0;
  10513. for (int k = 0; k < bs4; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  10514. if (n_not_ongrid > 0 && scale > 0) {
  10515. float id = 1/scale;
  10516. for (int k = 0; k < bs4; ++k) {
  10517. //if (is_on_grid[k]) continue;
  10518. uint16_t u = 0;
  10519. for (int i = 0; i < 4; ++i) {
  10520. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  10521. l = MAX(0, MIN(kMaxQ-1, l));
  10522. u |= (l << 3*i);
  10523. }
  10524. int grid_index = kmap_q3xs[u];
  10525. if (grid_index < 0) {
  10526. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  10527. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, scale, L + 4*k);
  10528. }
  10529. const int8_t * pg = (const int8_t *)(kgrid_q3xs + grid_index);
  10530. for (int i = 0; i < 4; ++i) L[4*k+i] = (pg[i] - 1)/2;
  10531. }
  10532. float sumqx = 0, sumq2 = 0;
  10533. for (int i = 0; i < block_size; ++i) {
  10534. float w = weight[i];
  10535. float q = 2*L[i] + 1;
  10536. sumqx += w*xval[i]*q;
  10537. sumq2 += w*q*q;
  10538. }
  10539. if (sumq2 > 0) scale = sumqx/sumq2;
  10540. }
  10541. if (scale < 0) {
  10542. // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
  10543. // and correspondingly flip quant signs.
  10544. scale = -scale;
  10545. for (int k = 0; k < bs8; ++k) block_signs[k] = ~block_signs[k];
  10546. }
  10547. for (int k = 0; k < bs4; ++k) {
  10548. uint16_t u = 0;
  10549. for (int i = 0; i < 4; ++i) u |= (L[4*k+i] << 3*i);
  10550. int grid_index = kmap_q3xs[u];
  10551. if (grid_index < 0) {
  10552. printf("Oops: found point %u not on grid:", u);
  10553. for (int i = 0; i < 4; ++i) printf(" %d", L[4*k+i]);
  10554. printf("\n");
  10555. GGML_ASSERT(false);
  10556. }
  10557. qs[k] = grid_index & 255;
  10558. qh[(ib*bs4+k)/8] |= ((grid_index >> 8) << ((ib*bs4+k)%8));
  10559. }
  10560. qs += bs4;
  10561. for (int k = 0; k < bs8; ++k) signs[k] = block_signs[k];
  10562. signs += bs8;
  10563. GGML_ASSERT(scale >= 0);
  10564. scales[ib] = scale;
  10565. max_scale = MAX(max_scale, scale);
  10566. }
  10567. if (!max_scale) {
  10568. continue;
  10569. }
  10570. float d = max_scale/31;
  10571. y[ibl].d = GGML_FP32_TO_FP16(d * 1.033f);
  10572. float id = 1/d;
  10573. for (int ib = 0; ib < QK_K/block_size; ib += 2) {
  10574. int l1 = nearest_int(0.5f*(id*scales[ib+0]-1));
  10575. l1 = MAX(0, MIN(15, l1));
  10576. int l2 = nearest_int(0.5f*(id*scales[ib+1]-1));
  10577. l2 = MAX(0, MIN(15, l2));
  10578. y[ibl].scales[ib/2] = l1 | (l2 << 4);
  10579. }
  10580. }
  10581. }
  10582. #define IQ3S_BLOCK_SIZE 32
  10583. size_t quantize_iq3_s(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  10584. GGML_ASSERT(n_per_row%QK_K == 0);
  10585. int64_t nblock = n_per_row/QK_K;
  10586. float scales[QK_K/IQ3S_BLOCK_SIZE];
  10587. float weight[IQ3S_BLOCK_SIZE];
  10588. float xval[IQ3S_BLOCK_SIZE];
  10589. int8_t L[IQ3S_BLOCK_SIZE];
  10590. int8_t Laux[IQ3S_BLOCK_SIZE];
  10591. float waux[IQ3S_BLOCK_SIZE];
  10592. bool is_on_grid[IQ3S_BLOCK_SIZE/4];
  10593. bool is_on_grid_aux[IQ3S_BLOCK_SIZE/4];
  10594. uint8_t block_signs[IQ3S_BLOCK_SIZE/8];
  10595. char * qrow = (char *)dst;
  10596. for (int64_t row = 0; row < nrow; ++row) {
  10597. quantize_row_iq3_s_impl(IQ3S_BLOCK_SIZE, src, qrow, n_per_row, quant_weights,
  10598. scales, weight, xval, L, Laux, waux, is_on_grid, is_on_grid_aux, block_signs);
  10599. src += n_per_row;
  10600. qrow += nblock*sizeof(block_iq3_s);
  10601. }
  10602. return nrow * nblock * sizeof(block_iq3_s);
  10603. }
  10604. void quantize_row_iq3_s(const float * restrict x, void * restrict vy, int64_t k) {
  10605. assert(k % QK_K == 0);
  10606. block_iq3_s * restrict y = vy;
  10607. quantize_row_iq3_s_reference(x, y, k);
  10608. }
  10609. void quantize_row_iq3_s_reference(const float * restrict x, block_iq3_s * restrict y, int64_t k) {
  10610. assert(k % QK_K == 0);
  10611. quantize_iq3_s(x, y, 1, k, NULL);
  10612. }
  10613. // =================================== 1.5 bpw ===================================================
  10614. static int iq1_find_best_neighbour(const uint16_t * restrict neighbours, const uint64_t * restrict grid,
  10615. const float * restrict xval, const float * restrict weight, float * scale, int8_t * restrict L, int ngrid) {
  10616. int num_neighbors = neighbours[0];
  10617. GGML_ASSERT(num_neighbors > 0);
  10618. float best_score = -FLT_MAX;
  10619. int grid_index = -1;
  10620. for (int j = 1; j <= num_neighbors; ++j) {
  10621. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  10622. float sumqx = 0, sumq2 = 0;
  10623. for (int i = 0; i < 8; ++i) {
  10624. float q = (pg[i] - 3)/2;
  10625. float w = weight[i];
  10626. sumqx += w*q*xval[i];
  10627. sumq2 += w*q*q;
  10628. }
  10629. if (sumqx > 0 && sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  10630. *scale = sumqx/sumq2; best_score = *scale * sumqx;
  10631. grid_index = neighbours[j];
  10632. }
  10633. }
  10634. if (grid_index < 0) {
  10635. for (int i = 0; i < ngrid; ++i) {
  10636. const int8_t * grid_i = (const int8_t *)(grid + i);
  10637. float sumqx = 0, sumq2 = 0;
  10638. for (int j = 0; j < 8; ++j) {
  10639. float w = weight[j];
  10640. float q = (grid_i[j] - 3)/2;
  10641. sumqx += w*q*xval[j];
  10642. sumq2 += w*q*q;
  10643. }
  10644. if (sumqx > 0 && sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  10645. *scale = sumqx/sumq2; best_score = *scale*sumqx;
  10646. grid_index = i;
  10647. }
  10648. }
  10649. }
  10650. if (grid_index < 0) {
  10651. printf("Oops, did not find grid point\n");
  10652. printf("Have %d neighbours\n", num_neighbors);
  10653. for (int j = 1; j <= num_neighbors; ++j) {
  10654. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  10655. float sumqx = 0, sumq2 = 0;
  10656. for (int i = 0; i < 8; ++i) {
  10657. float q = (pg[i] - 3)/2;
  10658. float w = weight[i];
  10659. sumqx += w*q*xval[i];
  10660. sumq2 += w*q*q;
  10661. }
  10662. printf(" neighbour %d: sumqx = %g sumq2 = %g\n", j, (double)sumqx, (double)sumq2);
  10663. }
  10664. }
  10665. GGML_ASSERT(grid_index >= 0);
  10666. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  10667. *scale *= 1.05f; // This is a fudge factor. Don't ask me why it improves the result.
  10668. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  10669. const int8_t * pg = (const int8_t *)(grid + grid_index);
  10670. for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
  10671. return grid_index;
  10672. }
  10673. static int iq1_find_best_neighbour2(const uint16_t * restrict neighbours, const uint64_t * restrict grid,
  10674. const float * restrict xval, const float * restrict weight, float scale, const float * restrict xg, int8_t * restrict L, int ngrid) {
  10675. int num_neighbors = neighbours[0];
  10676. GGML_ASSERT(num_neighbors > 0);
  10677. float best_score = FLT_MAX;
  10678. int grid_index = -1;
  10679. for (int j = 1; j <= num_neighbors; ++j) {
  10680. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  10681. float d2 = 0;
  10682. for (int i = 0; i < 8; ++i) {
  10683. float q = xg[(pg[i] - 1)/2];
  10684. float w = weight[i];
  10685. float diff = scale*q - xval[i];
  10686. d2 += w*diff*diff;
  10687. }
  10688. if (d2 < best_score) {
  10689. best_score = d2;
  10690. grid_index = neighbours[j];
  10691. }
  10692. }
  10693. if (grid_index < 0) {
  10694. for (int i = 0; i < ngrid; ++i) {
  10695. const int8_t * grid_i = (const int8_t *)(grid + i);
  10696. float d2 = 0;
  10697. for (int j = 0; j < 8; ++j) {
  10698. float w = weight[j];
  10699. float q = xg[(grid_i[j] - 1)/2];
  10700. float diff = scale*q - xval[i];
  10701. d2 += w*diff*diff;
  10702. }
  10703. if (d2 < best_score) {
  10704. best_score = d2;
  10705. grid_index = i;
  10706. }
  10707. }
  10708. }
  10709. if (grid_index < 0) {
  10710. printf("Oops, did not find grid point\n");
  10711. printf("Have %d neighbours\n", num_neighbors);
  10712. for (int j = 1; j <= num_neighbors; ++j) {
  10713. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  10714. float sumqx = 0, sumq2 = 0;
  10715. for (int i = 0; i < 8; ++i) {
  10716. float q = xg[(pg[i] - 1)/2];
  10717. float w = weight[i];
  10718. sumqx += w*q*xval[i];
  10719. sumq2 += w*q*q;
  10720. }
  10721. printf(" neighbour %d: sumqx = %g sumq2 = %g\n", j, (double)sumqx, (double)sumq2);
  10722. }
  10723. }
  10724. GGML_ASSERT(grid_index >= 0);
  10725. const int8_t * pg = (const int8_t *)(grid + grid_index);
  10726. for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
  10727. return grid_index;
  10728. }
  10729. static int iq1_sort_helper(const void * left, const void * right) {
  10730. const float * l = left;
  10731. const float * r = right;
  10732. return *l < *r ? -1 : *l > *r ? 1 : 0;
  10733. }
  10734. #define IQ1S_BLOCK_SIZE 32
  10735. #define IQ1M_BLOCK_SIZE 16
  10736. static void quantize_row_iq1_s_impl(const float * restrict x, void * restrict vy, int64_t n, const float * restrict quant_weights,
  10737. float * scales,
  10738. float * weight,
  10739. float * sumx,
  10740. float * sumw,
  10741. float * pairs,
  10742. int8_t * L,
  10743. uint16_t * index,
  10744. int8_t * shifts) {
  10745. const int gindex = iq2_data_index(GGML_TYPE_IQ1_S);
  10746. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  10747. const int * kmap_q2xs = iq2_data[gindex].map;
  10748. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  10749. GGML_ASSERT(quant_weights && "missing quantization weights");
  10750. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  10751. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  10752. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  10753. GGML_ASSERT(n%QK_K == 0);
  10754. block_iq1_s * y = vy;
  10755. const int64_t nbl = n/QK_K;
  10756. const int block_size = IQ1S_BLOCK_SIZE;
  10757. const float x_p[3] = {-1 + IQ1S_DELTA, IQ1S_DELTA, 1 + IQ1S_DELTA};
  10758. const float x_m[3] = {-1 - IQ1S_DELTA, -IQ1S_DELTA, 1 - IQ1S_DELTA};
  10759. int * idx = (int *)(pairs + 1);
  10760. for (int ibl = 0; ibl < nbl; ++ibl) {
  10761. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  10762. memset(y[ibl].qs, 0, QK_K/8);
  10763. memset(y[ibl].qh, 0, QK_K/16);
  10764. float max_scale = 0;
  10765. const float * xbl = x + QK_K*ibl;
  10766. float sumx2 = 0;
  10767. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  10768. float sigma2 = 2*sumx2/QK_K;
  10769. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  10770. const float * xb = xbl + block_size*ib;
  10771. const float * qw = quant_weights + QK_K*ibl + block_size*ib;
  10772. for (int i = 0; i < block_size; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  10773. float max = fabsf(xb[0]);
  10774. for (int i = 1; i < block_size; ++i) max = MAX(max, fabsf(xb[i]));
  10775. if (max < GROUP_MAX_EPS_IQ1_S) {
  10776. scales[ib] = 0;
  10777. memset(L, 1, block_size);
  10778. continue;
  10779. }
  10780. // Here we solve exactly the sum of squared difference (SSD) weighted minimization problem.
  10781. // With just 3 allowed quant values (-1, 0, 1), we can search exhaustively for the two
  10782. // boundaries that split the weights xb[i] into 3 groups. To do so, we sort the weights
  10783. // in ascending order, compute Si = sum[weight[j] xb[j], j = 0...i] and
  10784. // Wi = sum[weight[j], j = 0...i], and use these to quckly get get the optimum scale
  10785. // for each possible and score for each split.
  10786. for (int j = 0; j < block_size; ++j) {
  10787. pairs[2*j] = xb[j];
  10788. idx[2*j] = j;
  10789. }
  10790. qsort(pairs, block_size, 2*sizeof(float), iq1_sort_helper);
  10791. {
  10792. sumx[0] = sumw[0] = 0;
  10793. for (int j = 0; j < block_size; ++j) {
  10794. int i = idx[2*j];
  10795. sumx[j+1] = sumx[j] + weight[i]*xb[i];
  10796. sumw[j+1] = sumw[j] + weight[i];
  10797. }
  10798. }
  10799. float best_score = -FLT_MIN, scale = max;
  10800. int besti1 = -1, besti2 = -1, best_shift = 0;
  10801. for (int i1 = 0; i1 <= block_size; ++i1) {
  10802. for (int i2 = i1; i2 <= block_size; ++i2) {
  10803. float sumqx = (sumx[i1] - sumx[0])*x_p[0] + (sumx[i2] - sumx[i1])*x_p[1] + (sumx[block_size] - sumx[i2])*x_p[2];
  10804. float sumq2 = (sumw[i1] - sumw[0])*x_p[0]*x_p[0] + (sumw[i2] - sumw[i1])*x_p[1]*x_p[1] + (sumw[block_size] - sumw[i2])*x_p[2]*x_p[2];
  10805. if (sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  10806. scale = sumqx/sumq2; best_score = scale*sumqx;
  10807. besti1 = i1; besti2 = i2; best_shift = 1;
  10808. }
  10809. sumqx = (sumx[i1] - sumx[0])*x_m[0] + (sumx[i2] - sumx[i1])*x_m[1] + (sumx[block_size] - sumx[i2])*x_m[2];
  10810. sumq2 = (sumw[i1] - sumw[0])*x_m[0]*x_m[0] + (sumw[i2] - sumw[i1])*x_m[1]*x_m[1] + (sumw[block_size] - sumw[i2])*x_m[2]*x_m[2];
  10811. if (sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  10812. scale = sumqx/sumq2; best_score = scale*sumqx;
  10813. besti1 = i1; besti2 = i2; best_shift = -1;
  10814. }
  10815. }
  10816. }
  10817. GGML_ASSERT(besti1 >= 0 && besti2 >= 0 && best_shift != 0);
  10818. for (int j = 0; j < besti1; ++j) L[idx[2*j]] = 0;
  10819. for (int j = besti1; j < besti2; ++j) L[idx[2*j]] = 1;
  10820. for (int j = besti2; j < block_size; ++j) L[idx[2*j]] = 2;
  10821. if (scale < 0) {
  10822. for (int j = 0; j < block_size; ++j) L[j] = 2 - L[j];
  10823. scale = -scale; best_shift = -best_shift;
  10824. }
  10825. bool all_on_grid = true;
  10826. const float * xx = best_shift == 1 ? x_p : x_m;
  10827. for (int k = 0; k < block_size/8; ++k) {
  10828. uint16_t u = 0;
  10829. for (int j = 0; j < 8; ++j) u |= (L[8*k+j] << 2*j);
  10830. int grid_index = kmap_q2xs[u];
  10831. if (grid_index < 0) {
  10832. all_on_grid = false;
  10833. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  10834. grid_index = iq1_find_best_neighbour2(neighbours, kgrid_q2xs, xb + 8*k, weight + 8*k, scale, xx, L + 8*k, NGRID_IQ1S);
  10835. GGML_ASSERT(grid_index >= 0);
  10836. }
  10837. index[k] = grid_index;
  10838. }
  10839. if (!all_on_grid) {
  10840. float sumqx = 0, sumq2 = 0;
  10841. for (int k = 0; k < block_size/8; ++k) {
  10842. const int8_t * pg = (const int8_t *)(kgrid_q2xs + index[k]);
  10843. for (int j = 0; j < 8; ++j) {
  10844. float w = weight[8*k + j];
  10845. float q = xx[(pg[j] - 1)/2];
  10846. sumqx += w*q*xb[8*k+j];
  10847. sumq2 += w*q*q;
  10848. }
  10849. }
  10850. if (sumqx > 0 && sumq2 > 0) scale = sumqx/sumq2;
  10851. }
  10852. uint16_t h = 0;
  10853. for (int k = 0; k < block_size/8; ++k) {
  10854. y[ibl].qs[(block_size/8)*ib + k] = index[k] & 255;
  10855. h |= (index[k] >> 8) << 3*k;
  10856. }
  10857. y[ibl].qh[ib] = h;
  10858. GGML_ASSERT(scale >= 0);
  10859. scales[ib] = scale;
  10860. shifts[ib] = best_shift;
  10861. max_scale = MAX(max_scale, scale);
  10862. }
  10863. if (!max_scale) {
  10864. continue;
  10865. }
  10866. float d = max_scale/15;
  10867. y[ibl].d = GGML_FP32_TO_FP16(d*1.125f); // 1.125f is another fudge factor. Don't ask me why it is needed.
  10868. float id = 1/d;
  10869. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  10870. int l = nearest_int(0.5f*(id*scales[ib]-1));
  10871. l = MAX(0, MIN(7, l));
  10872. if (shifts[ib] == -1) l |= 8;
  10873. y[ibl].qh[ib] |= (l << 12);
  10874. }
  10875. }
  10876. }
  10877. size_t quantize_iq1_s(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  10878. GGML_ASSERT(n_per_row%QK_K == 0);
  10879. float scales[QK_K/IQ1S_BLOCK_SIZE];
  10880. float weight[IQ1S_BLOCK_SIZE];
  10881. int8_t L[IQ1S_BLOCK_SIZE];
  10882. float sumx[IQ1S_BLOCK_SIZE+1];
  10883. float sumw[IQ1S_BLOCK_SIZE+1];
  10884. float pairs[2*IQ1S_BLOCK_SIZE];
  10885. uint16_t index[IQ1S_BLOCK_SIZE/8];
  10886. int8_t shifts[QK_K/IQ1S_BLOCK_SIZE];
  10887. int64_t nblock = n_per_row/QK_K;
  10888. char * qrow = (char *)dst;
  10889. for (int64_t row = 0; row < nrow; ++row) {
  10890. quantize_row_iq1_s_impl(src, qrow, n_per_row, quant_weights, scales, weight, sumx, sumw, pairs, L, index, shifts);
  10891. src += n_per_row;
  10892. qrow += nblock*sizeof(block_iq1_s);
  10893. }
  10894. return nrow * nblock * sizeof(block_iq1_s);
  10895. }
  10896. static void quantize_row_iq1_m_impl(const float * restrict x, void * restrict vy, int64_t n, const float * restrict quant_weights,
  10897. float * scales,
  10898. float * weight,
  10899. float * pairs,
  10900. int8_t * L,
  10901. uint16_t * index,
  10902. int8_t * shifts) {
  10903. const int gindex = iq2_data_index(GGML_TYPE_IQ1_M);
  10904. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  10905. const int * kmap_q2xs = iq2_data[gindex].map;
  10906. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  10907. //GGML_ASSERT(quant_weights && "missing quantization weights");
  10908. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  10909. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  10910. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  10911. GGML_ASSERT(n%QK_K == 0);
  10912. block_iq1_m * y = vy;
  10913. const int64_t nbl = n/QK_K;
  10914. const int block_size = IQ1M_BLOCK_SIZE;
  10915. const float x_p[3] = {-1 + IQ1M_DELTA, IQ1M_DELTA, 1 + IQ1M_DELTA};
  10916. const float x_m[3] = {-1 - IQ1M_DELTA, -IQ1M_DELTA, 1 - IQ1M_DELTA};
  10917. const uint8_t masks[4] = {0x00, 0x80, 0x08, 0x88};
  10918. int * idx = (int *)(pairs + 1);
  10919. float sumqx[4], sumq2[4];
  10920. iq1m_scale_t s;
  10921. const float * xx;
  10922. for (int ibl = 0; ibl < nbl; ++ibl) {
  10923. memset(y[ibl].qs, 0, QK_K/8);
  10924. memset(y[ibl].qh, 0, QK_K/16);
  10925. memset(y[ibl].scales, 0, QK_K/32);
  10926. float max_scale = 0;
  10927. const float * xbl = x + QK_K*ibl;
  10928. float sumx2 = 0;
  10929. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  10930. float sigma2 = 2*sumx2/QK_K;
  10931. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  10932. const float * xb = xbl + block_size*ib;
  10933. if (quant_weights) {
  10934. const float * qw = quant_weights + QK_K*ibl + block_size*ib;
  10935. for (int i = 0; i < block_size; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  10936. } else {
  10937. for (int i = 0; i < block_size; ++i) weight[i] = xb[i]*xb[i];
  10938. }
  10939. float max = fabsf(xb[0]);
  10940. for (int i = 1; i < block_size; ++i) max = MAX(max, fabsf(xb[i]));
  10941. if (max < GROUP_MAX_EPS_IQ1_M) {
  10942. scales[ib] = 0;
  10943. memset(L, 1, block_size);
  10944. continue;
  10945. }
  10946. // Here we solve exactly the sum of squared difference (SSD) weighted minimization problem.
  10947. // With just 3 allowed quant values (-1, 0, 1), we can search exhaustively for the two
  10948. // boundaries that split the weights xb[i] into 3 groups. To do so, we sort the weights
  10949. // in ascending order, compute Si = sum[weight[j] xb[j], j = 0...i] and
  10950. // Wi = sum[weight[j], j = 0...i], and use these to quckly get get the optimum scale
  10951. // for each possible and score for each split.
  10952. for (int j = 0; j < block_size; ++j) {
  10953. pairs[2*j] = xb[j];
  10954. idx[2*j] = j;
  10955. }
  10956. qsort(pairs, block_size, 2*sizeof(float), iq1_sort_helper);
  10957. float best_score = -FLT_MIN, scale = max;
  10958. int besti1 = -1, besti2 = -1, best_k = -1;
  10959. // 0: +, +
  10960. // 1: +, -
  10961. // 2: -, +
  10962. // 3: -, -
  10963. for (int i1 = 0; i1 <= block_size; ++i1) {
  10964. for (int i2 = i1; i2 <= block_size; ++i2) {
  10965. memset(sumqx, 0, 4*sizeof(float));
  10966. memset(sumq2, 0, 4*sizeof(float));
  10967. for (int j = 0; j < i1; ++j) {
  10968. int i = idx[2*j];
  10969. if (i < block_size/2) {
  10970. sumqx[0] += weight[i]*x_p[0]*xb[i];
  10971. sumqx[1] += weight[i]*x_p[0]*xb[i];
  10972. sumqx[2] += weight[i]*x_m[0]*xb[i];
  10973. sumqx[3] += weight[i]*x_m[0]*xb[i];
  10974. sumq2[0] += weight[i]*x_p[0]*x_p[0];
  10975. sumq2[1] += weight[i]*x_p[0]*x_p[0];
  10976. sumq2[2] += weight[i]*x_m[0]*x_m[0];
  10977. sumq2[3] += weight[i]*x_m[0]*x_m[0];
  10978. } else {
  10979. sumqx[0] += weight[i]*x_p[0]*xb[i];
  10980. sumqx[2] += weight[i]*x_p[0]*xb[i];
  10981. sumqx[1] += weight[i]*x_m[0]*xb[i];
  10982. sumqx[3] += weight[i]*x_m[0]*xb[i];
  10983. sumq2[0] += weight[i]*x_p[0]*x_p[0];
  10984. sumq2[2] += weight[i]*x_p[0]*x_p[0];
  10985. sumq2[1] += weight[i]*x_m[0]*x_m[0];
  10986. sumq2[3] += weight[i]*x_m[0]*x_m[0];
  10987. }
  10988. }
  10989. for (int j = i1; j < i2; ++j) {
  10990. int i = idx[2*j];
  10991. if (i < block_size/2) {
  10992. sumqx[0] += weight[i]*x_p[1]*xb[i];
  10993. sumqx[1] += weight[i]*x_p[1]*xb[i];
  10994. sumqx[2] += weight[i]*x_m[1]*xb[i];
  10995. sumqx[3] += weight[i]*x_m[1]*xb[i];
  10996. sumq2[0] += weight[i]*x_p[1]*x_p[1];
  10997. sumq2[1] += weight[i]*x_p[1]*x_p[1];
  10998. sumq2[2] += weight[i]*x_m[1]*x_m[1];
  10999. sumq2[3] += weight[i]*x_m[1]*x_m[1];
  11000. } else {
  11001. sumqx[0] += weight[i]*x_p[1]*xb[i];
  11002. sumqx[2] += weight[i]*x_p[1]*xb[i];
  11003. sumqx[1] += weight[i]*x_m[1]*xb[i];
  11004. sumqx[3] += weight[i]*x_m[1]*xb[i];
  11005. sumq2[0] += weight[i]*x_p[1]*x_p[1];
  11006. sumq2[2] += weight[i]*x_p[1]*x_p[1];
  11007. sumq2[1] += weight[i]*x_m[1]*x_m[1];
  11008. sumq2[3] += weight[i]*x_m[1]*x_m[1];
  11009. }
  11010. }
  11011. for (int j = i2; j < block_size; ++j) {
  11012. int i = idx[2*j];
  11013. if (i < block_size/2) {
  11014. sumqx[0] += weight[i]*x_p[2]*xb[i];
  11015. sumqx[1] += weight[i]*x_p[2]*xb[i];
  11016. sumqx[2] += weight[i]*x_m[2]*xb[i];
  11017. sumqx[3] += weight[i]*x_m[2]*xb[i];
  11018. sumq2[0] += weight[i]*x_p[2]*x_p[2];
  11019. sumq2[1] += weight[i]*x_p[2]*x_p[2];
  11020. sumq2[2] += weight[i]*x_m[2]*x_m[2];
  11021. sumq2[3] += weight[i]*x_m[2]*x_m[2];
  11022. } else {
  11023. sumqx[0] += weight[i]*x_p[2]*xb[i];
  11024. sumqx[2] += weight[i]*x_p[2]*xb[i];
  11025. sumqx[1] += weight[i]*x_m[2]*xb[i];
  11026. sumqx[3] += weight[i]*x_m[2]*xb[i];
  11027. sumq2[0] += weight[i]*x_p[2]*x_p[2];
  11028. sumq2[2] += weight[i]*x_p[2]*x_p[2];
  11029. sumq2[1] += weight[i]*x_m[2]*x_m[2];
  11030. sumq2[3] += weight[i]*x_m[2]*x_m[2];
  11031. }
  11032. }
  11033. for (int k = 0; k < 4; ++k) {
  11034. if (sumq2[k] > 0 && sumqx[k]*sumqx[k] > best_score*sumq2[k]) {
  11035. scale = sumqx[k]/sumq2[k]; best_score = scale*sumqx[k];
  11036. besti1 = i1; besti2 = i2; best_k = k;
  11037. }
  11038. }
  11039. }
  11040. }
  11041. GGML_ASSERT(besti1 >= 0 && besti2 >= 0 && best_k >= 0);
  11042. for (int j = 0; j < besti1; ++j) L[idx[2*j]] = 0;
  11043. for (int j = besti1; j < besti2; ++j) L[idx[2*j]] = 1;
  11044. for (int j = besti2; j < block_size; ++j) L[idx[2*j]] = 2;
  11045. if (scale < 0) {
  11046. for (int j = 0; j < block_size; ++j) L[j] = 2 - L[j];
  11047. scale = -scale;
  11048. best_k = best_k == 0 ? 3 : best_k == 1 ? 2 : best_k == 2 ? 1 : 0;
  11049. }
  11050. bool all_on_grid = true;
  11051. for (int k = 0; k < block_size/8; ++k) {
  11052. if (k == 0) xx = best_k < 2 ? x_p : x_m;
  11053. else xx = best_k%2 == 0 ? x_p : x_m;
  11054. uint16_t u = 0;
  11055. for (int j = 0; j < 8; ++j) u |= (L[8*k+j] << 2*j);
  11056. int grid_index = kmap_q2xs[u];
  11057. if (grid_index < 0) {
  11058. all_on_grid = false;
  11059. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  11060. grid_index = iq1_find_best_neighbour2(neighbours, kgrid_q2xs, xb + 8*k, weight + 8*k, scale, xx, L + 8*k, NGRID_IQ1S);
  11061. GGML_ASSERT(grid_index >= 0);
  11062. }
  11063. index[k] = grid_index;
  11064. }
  11065. if (!all_on_grid) {
  11066. float sumqx_f = 0, sumq2_f = 0;
  11067. for (int k = 0; k < block_size/8; ++k) {
  11068. if (k == 0) xx = best_k < 2 ? x_p : x_m;
  11069. else xx = best_k%2 == 0 ? x_p : x_m;
  11070. const int8_t * pg = (const int8_t *)(kgrid_q2xs + index[k]);
  11071. for (int j = 0; j < 8; ++j) {
  11072. float w = weight[8*k + j];
  11073. float q = xx[(pg[j] - 1)/2];
  11074. sumqx_f += w*q*xb[8*k+j];
  11075. sumq2_f += w*q*q;
  11076. }
  11077. }
  11078. if (sumqx_f > 0 && sumq2_f > 0) scale = sumqx_f/sumq2_f;
  11079. }
  11080. y[ibl].qs[2*ib + 0] = index[0] & 255;
  11081. y[ibl].qs[2*ib + 1] = index[1] & 255;
  11082. y[ibl].qh[ib] = (index[0] >> 8) | ((index[1] >> 8) << 4);
  11083. GGML_ASSERT(scale >= 0);
  11084. scales[ib] = scale;
  11085. shifts[ib] = best_k;
  11086. max_scale = MAX(max_scale, scale);
  11087. }
  11088. if (!max_scale) {
  11089. continue;
  11090. }
  11091. uint16_t * sc = (uint16_t *)y[ibl].scales;
  11092. float d = max_scale/15;
  11093. float id = 1/d;
  11094. float sumqx_f = 0, sumq2_f = 0;
  11095. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  11096. int l = nearest_int(0.5f*(id*scales[ib+0]-1));
  11097. l = MAX(0, MIN(7, l));
  11098. sc[ib/4] |= (l << 3*(ib%4));
  11099. y[ibl].qh[ib] |= masks[shifts[ib]];
  11100. const float * xb = xbl + block_size*ib;
  11101. if (quant_weights) {
  11102. const float * qw = quant_weights + QK_K*ibl + block_size*ib;
  11103. for (int i = 0; i < block_size; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  11104. } else {
  11105. for (int i = 0; i < block_size; ++i) weight[i] = xb[i]*xb[i];
  11106. }
  11107. for (int k = 0; k < block_size/8; ++k) {
  11108. if (k == 0) xx = shifts[ib] < 2 ? x_p : x_m;
  11109. else xx = shifts[ib]%2 == 0 ? x_p : x_m;
  11110. const int8_t * pg = (const int8_t *)(kgrid_q2xs + y[ibl].qs[2*ib+k] + ((y[ibl].qh[ib] << (8 - 4*k)) & 0x700));
  11111. for (int j = 0; j < 8; ++j) {
  11112. float w = weight[8*k + j];
  11113. float q = xx[(pg[j] - 1)/2]*(2*l+1);
  11114. sumqx_f += w*q*xb[8*k+j];
  11115. sumq2_f += w*q*q;
  11116. }
  11117. }
  11118. }
  11119. if (sumq2_f > 0) d = sumqx_f/sumq2_f;
  11120. s.f16 = GGML_FP32_TO_FP16(d*1.1125f); // 1.1125f is another fudge factor. Don't ask me why it is needed.
  11121. sc[0] |= ((s.u16 & 0x000f) << 12);
  11122. sc[1] |= ((s.u16 & 0x00f0) << 8);
  11123. sc[2] |= ((s.u16 & 0x0f00) << 4);
  11124. sc[3] |= ((s.u16 & 0xf000) << 0);
  11125. }
  11126. }
  11127. size_t quantize_iq1_m(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  11128. GGML_ASSERT(n_per_row%QK_K == 0);
  11129. float scales[QK_K/IQ1M_BLOCK_SIZE];
  11130. float weight[IQ1M_BLOCK_SIZE];
  11131. int8_t L[IQ1M_BLOCK_SIZE];
  11132. float pairs[2*IQ1M_BLOCK_SIZE];
  11133. uint16_t index[IQ1M_BLOCK_SIZE/8];
  11134. int8_t shifts[QK_K/IQ1M_BLOCK_SIZE];
  11135. int64_t nblock = n_per_row/QK_K;
  11136. char * qrow = (char *)dst;
  11137. for (int64_t row = 0; row < nrow; ++row) {
  11138. quantize_row_iq1_m_impl(src, qrow, n_per_row, quant_weights, scales, weight, pairs, L, index, shifts);
  11139. src += n_per_row;
  11140. qrow += nblock*sizeof(block_iq1_m);
  11141. }
  11142. return nrow * nblock * sizeof(block_iq1_m);
  11143. }
  11144. // ============================ 4-bit non-linear quants
  11145. static inline int best_index_int8(int n, const int8_t * val, float x) {
  11146. if (x <= val[0]) return 0;
  11147. if (x >= val[n-1]) return n-1;
  11148. int ml = 0, mu = n-1;
  11149. while (mu-ml > 1) {
  11150. int mav = (ml+mu)/2;
  11151. if (x < val[mav]) mu = mav; else ml = mav;
  11152. }
  11153. return x - val[mu-1] < val[mu] - x ? mu-1 : mu;
  11154. }
  11155. static void quantize_row_iq4_nl_impl(const int super_block_size, const int block_size, const float * restrict x,
  11156. ggml_fp16_t * dh, uint8_t * q4, uint16_t * scales_h, uint8_t * scales_l,
  11157. float * scales, float * weight, uint8_t * L,
  11158. const int8_t * values,
  11159. const float * quant_weights,
  11160. const int ntry) {
  11161. float sigma2 = 0;
  11162. for (int j = 0; j < super_block_size; ++j) sigma2 += x[j]*x[j];
  11163. sigma2 *= 2.f/super_block_size;
  11164. memset(q4, 0, super_block_size/2);
  11165. dh[0] = GGML_FP32_TO_FP16(0.f);
  11166. float max_scale = 0, amax_scale = 0;
  11167. for (int ib = 0; ib < super_block_size/block_size; ++ib) {
  11168. const float * xb = x + ib*block_size;
  11169. uint8_t * Lb = L + ib*block_size;
  11170. if (quant_weights) {
  11171. const float * qw = quant_weights + ib*block_size;
  11172. for (int j = 0; j < block_size; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  11173. } else {
  11174. for (int j = 0; j < block_size; ++j) weight[j] = xb[j]*xb[j];
  11175. }
  11176. float amax = 0, max = 0;
  11177. for (int j = 0; j < block_size; ++j) {
  11178. float ax = fabsf(xb[j]);
  11179. if (ax > amax) {
  11180. amax = ax; max = xb[j];
  11181. }
  11182. }
  11183. if (amax < GROUP_MAX_EPS) {
  11184. scales[ib] = 0;
  11185. continue;
  11186. }
  11187. float d = ntry > 0 ? -max/values[0] : max/values[0];
  11188. float id = 1/d;
  11189. float sumqx = 0, sumq2 = 0;
  11190. for (int j = 0; j < block_size; ++j) {
  11191. float al = id*xb[j];
  11192. int l = best_index_int8(16, values, al);
  11193. Lb[j] = l;
  11194. float q = values[l];
  11195. float w = weight[j];
  11196. sumqx += w*q*xb[j];
  11197. sumq2 += w*q*q;
  11198. }
  11199. d = sumqx/sumq2;
  11200. float best = d*sumqx;
  11201. for (int itry = -ntry; itry <= ntry; ++itry) {
  11202. id = (itry + values[0])/max;
  11203. sumqx = sumq2 = 0;
  11204. for (int j = 0; j < block_size; ++j) {
  11205. float al = id*xb[j];
  11206. int l = best_index_int8(16, values, al);
  11207. float q = values[l];
  11208. float w = weight[j];
  11209. sumqx += w*q*xb[j];
  11210. sumq2 += w*q*q;
  11211. }
  11212. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  11213. d = sumqx/sumq2; best = d * sumqx;
  11214. }
  11215. }
  11216. scales[ib] = d;
  11217. float abs_d = fabsf(d);
  11218. if (abs_d > amax_scale) {
  11219. amax_scale = abs_d; max_scale = d;
  11220. }
  11221. }
  11222. if (super_block_size/block_size > 1) {
  11223. int nb = super_block_size/block_size;
  11224. memset(scales_h, 0, ((nb+7)/8)*sizeof(uint16_t));
  11225. float d = -max_scale/32;
  11226. dh[0] = GGML_FP32_TO_FP16(d);
  11227. float id = d ? 1/d : 0.f;
  11228. for (int ib = 0; ib < super_block_size/block_size; ++ib) {
  11229. int l = nearest_int(id*scales[ib]);
  11230. l = MAX(-32, MIN(31, l));
  11231. float dl = d * l;
  11232. float idl = dl ? 1/dl : 0.f;
  11233. uint8_t * Lb = L + ib*block_size;
  11234. const float * xb = x + ib*block_size;
  11235. for (int j = 0; j < block_size; ++j) {
  11236. Lb[j] = best_index_int8(16, values, idl*xb[j]);
  11237. }
  11238. l += 32;
  11239. uint8_t l_l = l & 0xf;
  11240. uint8_t l_h = l >> 4;
  11241. if (ib%2 == 0) scales_l[ib/2] = l_l;
  11242. else scales_l[ib/2] |= (l_l << 4);
  11243. scales_h[ib/8] |= (l_h << 2*(ib%8));
  11244. }
  11245. } else {
  11246. dh[0] = GGML_FP32_TO_FP16(scales[0]);
  11247. if (ntry > 0) {
  11248. float id = scales[0] ? 1/scales[0] : 0;
  11249. for (int j = 0; j < super_block_size; ++j) {
  11250. L[j] = best_index_int8(16, values, id*x[j]);
  11251. }
  11252. }
  11253. }
  11254. for (int i = 0; i < super_block_size/32; ++i) {
  11255. for (int j = 0; j < 16; ++j) {
  11256. q4[16*i + j] = L[32*i + j] | (L[32*i + 16 + j] << 4);
  11257. }
  11258. }
  11259. }
  11260. size_t quantize_iq4_nl(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  11261. GGML_ASSERT(n_per_row%QK4_NL == 0);
  11262. int64_t nblock = n_per_row/QK4_NL;
  11263. char * qrow = (char *)dst;
  11264. uint8_t L[QK4_NL];
  11265. float weight[QK4_NL];
  11266. uint16_t unused_h;
  11267. uint8_t * unused_l = NULL;
  11268. float scale;
  11269. for (int64_t row = 0; row < nrow; ++row) {
  11270. block_iq4_nl * iq4 = (block_iq4_nl *)qrow;
  11271. for (int ibl = 0; ibl < nblock; ++ibl) {
  11272. const float * qw = quant_weights ? quant_weights + QK4_NL*ibl : NULL;
  11273. quantize_row_iq4_nl_impl(QK4_NL, 32, src + QK4_NL*ibl, &iq4[ibl].d, iq4[ibl].qs, &unused_h, unused_l,
  11274. &scale, weight, L, kvalues_iq4nl, qw, 7);
  11275. }
  11276. src += n_per_row;
  11277. qrow += nblock*sizeof(block_iq4_nl);
  11278. }
  11279. return nrow * nblock * sizeof(block_iq4_nl);
  11280. }
  11281. void quantize_row_iq4_nl(const float * restrict x, void * restrict vy, int64_t k) {
  11282. GGML_ASSERT(k%QK4_NL == 0);
  11283. int64_t nblock = k/QK4_NL;
  11284. uint8_t L[QK4_NL];
  11285. float weight[QK4_NL];
  11286. uint16_t unused_h;
  11287. uint8_t * unused_l = NULL;
  11288. float scale;
  11289. block_iq4_nl * iq4 = (block_iq4_nl *)vy;
  11290. for (int ibl = 0; ibl < nblock; ++ibl) {
  11291. quantize_row_iq4_nl_impl(QK4_NL, 32, x + QK4_NL*ibl, &iq4[ibl].d, iq4[ibl].qs, &unused_h, unused_l,
  11292. &scale, weight, L, kvalues_iq4nl, NULL, -1);
  11293. }
  11294. }
  11295. void quantize_row_iq4_nl_reference(const float * restrict x, block_iq4_nl * restrict y, int64_t k) {
  11296. assert(k % QK4_NL == 0);
  11297. quantize_row_iq4_nl(x, y, k);
  11298. }
  11299. size_t quantize_iq4_xs(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  11300. GGML_ASSERT(n_per_row%QK_K == 0);
  11301. int64_t nblock = n_per_row/QK_K;
  11302. char * qrow = (char *)dst;
  11303. uint8_t L[QK_K];
  11304. float weight[32];
  11305. float scales[QK_K/32];
  11306. for (int64_t row = 0; row < nrow; ++row) {
  11307. block_iq4_xs * iq4 = (block_iq4_xs *)qrow;
  11308. for (int ibl = 0; ibl < nblock; ++ibl) {
  11309. const float * qw = quant_weights ? quant_weights + QK_K*ibl : NULL;
  11310. quantize_row_iq4_nl_impl(QK_K, 32, src + QK_K*ibl, &iq4[ibl].d, iq4[ibl].qs, &iq4[ibl].scales_h, iq4[ibl].scales_l,
  11311. scales, weight, L, kvalues_iq4nl, qw, 7);
  11312. }
  11313. src += n_per_row;
  11314. qrow += nblock*sizeof(block_iq4_xs);
  11315. }
  11316. return nrow * nblock * sizeof(block_iq4_xs);
  11317. }
  11318. void quantize_row_iq4_xs(const float * restrict x, void * restrict vy, int64_t k) {
  11319. assert(k % QK_K == 0);
  11320. block_iq4_xs * restrict y = vy;
  11321. quantize_row_iq4_xs_reference(x, y, k);
  11322. }
  11323. void quantize_row_iq4_xs_reference(const float * restrict x, block_iq4_xs * restrict y, int64_t k) {
  11324. assert(k % QK_K == 0);
  11325. quantize_iq4_xs(x, y, 1, k, NULL);
  11326. }
  11327. // =============================== 2.5625 bpw
  11328. static void quantize_row_iq2_s_impl(const float * restrict x, void * restrict vy, int64_t n, const float * restrict quant_weights) {
  11329. const int gindex = iq2_data_index(GGML_TYPE_IQ2_S);
  11330. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  11331. const int * kmap_q2xs = iq2_data[gindex].map;
  11332. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  11333. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  11334. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  11335. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  11336. GGML_ASSERT(n%QK_K == 0);
  11337. const int kMaxQ = 3;
  11338. const int64_t nbl = n/QK_K;
  11339. block_iq2_s * y = vy;
  11340. float scales[QK_K/16];
  11341. float weight[16];
  11342. float xval[16];
  11343. int8_t L[16];
  11344. int8_t Laux[16];
  11345. float waux[16];
  11346. bool is_on_grid[2];
  11347. bool is_on_grid_aux[2];
  11348. uint8_t block_signs[2];
  11349. for (int ibl = 0; ibl < nbl; ++ibl) {
  11350. memset(&y[ibl], 0, sizeof(block_iq2_s));
  11351. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  11352. float max_scale = 0;
  11353. const float * xbl = x + QK_K*ibl;
  11354. float sumx2 = 0;
  11355. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  11356. float sigma2 = 2*sumx2/QK_K;
  11357. for (int ib = 0; ib < QK_K/16; ++ib) {
  11358. const float * xb = xbl + 16*ib;
  11359. if (quant_weights) {
  11360. const float * qw = quant_weights + QK_K*ibl + 16*ib;
  11361. for (int i = 0; i < 16; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  11362. } else {
  11363. for (int i = 0; i < 16; ++i) weight[i] = 0.25f*sigma2 + xb[i]*xb[i];
  11364. }
  11365. for (int i = 0; i < 16; ++i) waux[i] = sqrtf(weight[i]);
  11366. for (int k = 0; k < 2; ++k) {
  11367. uint8_t s = 0;
  11368. for (int i = 0; i < 8; ++i) {
  11369. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  11370. else {
  11371. xval[8*k + i] = -xb[8*k + i]; s |= (1 << i);
  11372. }
  11373. }
  11374. block_signs[k] = s;
  11375. }
  11376. float max = xval[0];
  11377. for (int i = 1; i < 16; ++i) max = MAX(max, xval[i]);
  11378. if (max < GROUP_MAX_EPS_IQ2_S) {
  11379. scales[ib] = 0;
  11380. continue;
  11381. }
  11382. float best = 0;
  11383. float scale = max/(2*kMaxQ-1);
  11384. is_on_grid[0] = is_on_grid[1] = true;
  11385. for (int is = -9; is <= 9; ++is) {
  11386. float id = (2*kMaxQ-1+is*0.1f)/max;
  11387. float this_scale = 1/id;
  11388. for (int k = 0; k < 2; ++k) {
  11389. for (int i = 0; i < 8; ++i) {
  11390. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  11391. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  11392. }
  11393. uint16_t u = 0;
  11394. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  11395. int grid_index = kmap_q2xs[u];
  11396. is_on_grid_aux[k] = true;
  11397. if (grid_index < 0) {
  11398. is_on_grid_aux[k] = false;
  11399. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  11400. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  11401. }
  11402. }
  11403. float sumqx = 0, sumq2 = 0;
  11404. for (int i = 0; i < 16; ++i) {
  11405. float w = weight[i];
  11406. float q = 2*Laux[i] + 1;
  11407. sumqx += w*xval[i]*q;
  11408. sumq2 += w*q*q;
  11409. }
  11410. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  11411. scale = sumqx/sumq2; best = scale*sumqx;
  11412. for (int i = 0; i < 16; ++i) L[i] = Laux[i];
  11413. for (int k = 0; k < 2; ++k) is_on_grid[k] = is_on_grid_aux[k];
  11414. }
  11415. }
  11416. int n_not_ongrid = 0;
  11417. for (int k = 0; k < 2; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  11418. if (n_not_ongrid > 0 && scale > 0) {
  11419. float id = 1/scale;
  11420. for (int k = 0; k < 2; ++k) {
  11421. if (is_on_grid[k]) continue;
  11422. uint16_t u = 0;
  11423. for (int i = 0; i < 8; ++i) {
  11424. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  11425. l = MAX(0, MIN(kMaxQ-1, l));
  11426. u |= (l << 2*i);
  11427. L[8*k + i] = l;
  11428. }
  11429. int grid_index = kmap_q2xs[u];
  11430. if (grid_index < 0) {
  11431. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  11432. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  11433. }
  11434. }
  11435. float sumqx = 0, sumq2 = 0;
  11436. for (int i = 0; i < 16; ++i) {
  11437. float w = weight[i];
  11438. float q = 2*L[i] + 1;
  11439. sumqx += w*xval[i]*q;
  11440. sumq2 += w*q*q;
  11441. }
  11442. if (sumq2 > 0) scale = sumqx/sumq2;
  11443. }
  11444. if (scale < 0) {
  11445. scale = -scale;
  11446. for (int k = 0; k < 2; ++k) block_signs[k] = ~block_signs[k];
  11447. }
  11448. for (int k = 0; k < 2; ++k) {
  11449. uint16_t u = 0;
  11450. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  11451. int grid_index = kmap_q2xs[u];
  11452. if (grid_index < 0) {
  11453. printf("Oops: found point %u not on grid:", u);
  11454. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  11455. printf("\n");
  11456. GGML_ASSERT(false);
  11457. }
  11458. const int i8 = 2*ib + k;
  11459. y[ibl].qs[i8] = grid_index & 255;
  11460. y[ibl].qh[i8/4] |= ((grid_index >> 8) << 2*(i8%4));
  11461. y[ibl].qs[QK_K/8 + i8] = block_signs[k];
  11462. }
  11463. GGML_ASSERT(scale >= 0);
  11464. scales[ib] = scale;
  11465. max_scale = MAX(max_scale, scale);
  11466. }
  11467. if (!max_scale) {
  11468. continue;
  11469. }
  11470. float d = max_scale/31;
  11471. y[ibl].d = GGML_FP32_TO_FP16(d * 0.9875f);
  11472. float id = 1/d;
  11473. for (int ib = 0; ib < QK_K/16; ++ib) {
  11474. int l = nearest_int(0.5f*(id*scales[ib]-1));
  11475. l = MAX(0, MIN(15, l));
  11476. if (ib%2 == 0) y[ibl].scales[ib/2] = l;
  11477. else y[ibl].scales[ib/2] |= (l << 4);
  11478. }
  11479. }
  11480. }
  11481. size_t quantize_iq2_s(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  11482. GGML_ASSERT(n_per_row%QK_K == 0);
  11483. int64_t nblock = n_per_row/QK_K;
  11484. char * qrow = (char *)dst;
  11485. for (int64_t row = 0; row < nrow; ++row) {
  11486. quantize_row_iq2_s_impl(src, qrow, n_per_row, quant_weights);
  11487. src += n_per_row;
  11488. qrow += nblock*sizeof(block_iq2_s);
  11489. }
  11490. return nrow * nblock * sizeof(block_iq2_s);
  11491. }
  11492. void quantize_row_iq2_s_reference(const float * restrict x, block_iq2_s * restrict y, int64_t k) {
  11493. assert(k % QK_K == 0);
  11494. quantize_iq2_s(x, y, 1, k, NULL);
  11495. }
  11496. void quantize_row_iq2_s(const float * restrict x, void * restrict vy, int64_t k) {
  11497. assert(k % QK_K == 0);
  11498. block_iq2_s * restrict y = vy;
  11499. quantize_row_iq2_s_reference(x, y, k);
  11500. }
  11501. static bool validate_float(float f, size_t i) {
  11502. if (isinf(f)) {
  11503. fprintf(stderr, "ggml_validate_row_data: found inf value at block %zu\n", i);
  11504. return false;
  11505. }
  11506. if (isnan(f)) {
  11507. fprintf(stderr, "ggml_validate_row_data: found nan value at block %zu\n", i);
  11508. return false;
  11509. }
  11510. return true;
  11511. }
  11512. static bool isinf_fp16(ggml_fp16_t f) {
  11513. return (f & 0x7c00) == 0x7c00 && (f & 0x03ff) == 0;
  11514. }
  11515. static bool isnan_fp16(ggml_fp16_t f) {
  11516. return (f & 0x7c00) == 0x7c00 && (f & 0x03ff) != 0;
  11517. }
  11518. static bool validate_fp16(ggml_fp16_t f, size_t i) {
  11519. if (isinf_fp16(f)) {
  11520. fprintf(stderr, "ggml_validate_row_data: found inf value at block %zu\n", i);
  11521. return false;
  11522. }
  11523. if (isnan_fp16(f)) {
  11524. fprintf(stderr, "ggml_validate_row_data: found nan value at block %zu\n", i);
  11525. return false;
  11526. }
  11527. return true;
  11528. }
  11529. #define VALIDATE_ROW_DATA_D_F16_IMPL(type, data, nb) \
  11530. const type * q = (const type *) (data); \
  11531. for (size_t i = 0; i < (nb); ++i) { \
  11532. if (!validate_fp16(q[i].d, i)) { \
  11533. return false; \
  11534. } \
  11535. }
  11536. #define VALIDATE_ROW_DATA_DM_F16_IMPL(type, data, nb, d, m) \
  11537. const type * q = (const type *) (data); \
  11538. for (size_t i = 0; i < (nb); ++i) { \
  11539. if (!validate_fp16(q[i].d, i) || !validate_fp16(q[i].m, i)) { \
  11540. return false; \
  11541. } \
  11542. }
  11543. bool ggml_validate_row_data(enum ggml_type type, const void * data, size_t nbytes) {
  11544. if (type < 0 || type >= GGML_TYPE_COUNT) {
  11545. fprintf(stderr, "%s: invalid type %d\n", __func__, type);
  11546. return false;
  11547. }
  11548. if (nbytes % ggml_type_size(type) != 0) {
  11549. fprintf(stderr, "%s: invalid size %zu for type %d\n", __func__, nbytes, type);
  11550. return false;
  11551. }
  11552. const size_t nb = nbytes/ggml_type_size(type);
  11553. switch (type) {
  11554. case GGML_TYPE_BF16:
  11555. {
  11556. int nans = 0;
  11557. int infs = 0;
  11558. const unsigned short * f = (const unsigned short *) data;
  11559. for (size_t i = 0; i < nb; ++i) {
  11560. nans += (f[i] & 0x7fff) > 0x7f80;
  11561. infs += (f[i] & 0x7fff) == 0x7f80;
  11562. }
  11563. if (nans) {
  11564. fprintf(stderr, "%s: found %d NaNs in row of %zu BF16 values\n", __func__, nans, nb);
  11565. return false;
  11566. }
  11567. if (infs) {
  11568. fprintf(stderr, "%s: found %d infinities in row of %zu BF16 values\n", __func__, infs, nb);
  11569. return false;
  11570. }
  11571. } break;
  11572. case GGML_TYPE_F16:
  11573. {
  11574. const ggml_fp16_t * f = (const ggml_fp16_t *) data;
  11575. size_t i = 0;
  11576. #if defined(__AVX2__)
  11577. for (; i + 15 < nb; i += 16) {
  11578. __m256i v = _mm256_loadu_si256((const __m256i *)(f + i));
  11579. __m256i vexp = _mm256_and_si256(v, _mm256_set1_epi16(0x7c00));
  11580. __m256i cmp = _mm256_cmpeq_epi16(vexp, _mm256_set1_epi16(0x7c00));
  11581. int mask = _mm256_movemask_epi8(cmp);
  11582. if (mask) {
  11583. for (size_t j = 0; j < 16; ++j) {
  11584. if (!validate_fp16(f[i + j], i + j)) {
  11585. return false;
  11586. }
  11587. }
  11588. GGML_UNREACHABLE();
  11589. }
  11590. }
  11591. #elif defined(__ARM_NEON)
  11592. for (; i + 7 < nb; i += 8) {
  11593. uint16x8_t v = vld1q_u16(f + i);
  11594. uint16x8_t vexp = vandq_u16(v, vdupq_n_u16(0x7c00));
  11595. uint16x8_t cmp = vceqq_u16(vexp, vdupq_n_u16(0x7c00));
  11596. uint64_t mask = vget_lane_u64(vreinterpret_u64_u8(vshrn_n_u16(cmp, 4)), 0);
  11597. if (mask) {
  11598. for (size_t j = 0; j < 8; ++j) {
  11599. if (!validate_fp16(f[i + j], i + j)) {
  11600. return false;
  11601. }
  11602. }
  11603. GGML_UNREACHABLE();
  11604. }
  11605. }
  11606. #endif
  11607. for (; i < nb; ++i) {
  11608. if (!validate_fp16(f[i], i)) {
  11609. return false;
  11610. }
  11611. }
  11612. } break;
  11613. case GGML_TYPE_F32:
  11614. {
  11615. const float * f = (const float *) data;
  11616. size_t i = 0;
  11617. #if defined(__AVX2__)
  11618. for (; i + 7 < nb; i += 8) {
  11619. __m256i v = _mm256_loadu_si256((const __m256i *)(f + i));
  11620. __m256i vexp = _mm256_and_si256(v, _mm256_set1_epi32(0x7f800000));
  11621. __m256i cmp = _mm256_cmpeq_epi32(vexp, _mm256_set1_epi32(0x7f800000));
  11622. int mask = _mm256_movemask_epi8(cmp);
  11623. if (mask) {
  11624. for (size_t j = 0; j < 8; ++j) {
  11625. if (!validate_float(f[i + j], i + j)) {
  11626. return false;
  11627. }
  11628. }
  11629. GGML_UNREACHABLE();
  11630. }
  11631. }
  11632. #elif defined(__ARM_NEON)
  11633. for (; i + 3 < nb; i += 4) {
  11634. uint32x4_t v = vld1q_u32((const uint32_t *)f + i);
  11635. uint32x4_t vexp = vandq_u32(v, vdupq_n_u32(0x7f800000));
  11636. uint32x4_t cmp = vceqq_u32(vexp, vdupq_n_u32(0x7f800000));
  11637. uint64_t mask = vget_lane_u64(vreinterpret_u64_u16(vshrn_n_u32(cmp, 8)), 0);
  11638. if (mask) {
  11639. for (size_t j = 0; j < 4; ++j) {
  11640. if (!validate_float(f[i + j], i + j)) {
  11641. return false;
  11642. }
  11643. }
  11644. GGML_UNREACHABLE();
  11645. }
  11646. }
  11647. #endif
  11648. for (; i < nb; ++i) {
  11649. if (!validate_float(f[i], i)) {
  11650. return false;
  11651. }
  11652. }
  11653. } break;
  11654. case GGML_TYPE_F64:
  11655. {
  11656. const double * f = (const double *) data;
  11657. for (size_t i = 0; i < nb; ++i) {
  11658. if (!validate_float(f[i], i)) {
  11659. return false;
  11660. }
  11661. }
  11662. } break;
  11663. case GGML_TYPE_Q4_0:
  11664. {
  11665. VALIDATE_ROW_DATA_D_F16_IMPL(block_q4_0, data, nb);
  11666. } break;
  11667. case GGML_TYPE_Q4_1:
  11668. {
  11669. VALIDATE_ROW_DATA_DM_F16_IMPL(block_q4_1, data, nb, d, m);
  11670. } break;
  11671. case GGML_TYPE_Q5_0:
  11672. {
  11673. VALIDATE_ROW_DATA_D_F16_IMPL(block_q5_0, data, nb);
  11674. } break;
  11675. case GGML_TYPE_Q5_1:
  11676. {
  11677. VALIDATE_ROW_DATA_DM_F16_IMPL(block_q5_1, data, nb, d, m);
  11678. } break;
  11679. case GGML_TYPE_Q8_0:
  11680. {
  11681. VALIDATE_ROW_DATA_D_F16_IMPL(block_q8_0, data, nb);
  11682. } break;
  11683. case GGML_TYPE_Q2_K:
  11684. {
  11685. VALIDATE_ROW_DATA_DM_F16_IMPL(block_q2_K, data, nb, d, dmin);
  11686. } break;
  11687. case GGML_TYPE_Q3_K:
  11688. {
  11689. VALIDATE_ROW_DATA_D_F16_IMPL(block_q3_K, data, nb);
  11690. } break;
  11691. case GGML_TYPE_Q4_K:
  11692. {
  11693. VALIDATE_ROW_DATA_DM_F16_IMPL(block_q4_K, data, nb, d, dmin);
  11694. } break;
  11695. case GGML_TYPE_Q5_K:
  11696. {
  11697. VALIDATE_ROW_DATA_DM_F16_IMPL(block_q5_K, data, nb, d, dmin);
  11698. } break;
  11699. case GGML_TYPE_Q6_K:
  11700. {
  11701. VALIDATE_ROW_DATA_D_F16_IMPL(block_q6_K, data, nb);
  11702. } break;
  11703. case GGML_TYPE_Q8_K:
  11704. {
  11705. const block_q8_K * q = (const block_q8_K *) data;
  11706. for (size_t i = 0; i < nb; ++i) {
  11707. if (!validate_float(q[i].d, i)) {
  11708. return false;
  11709. }
  11710. }
  11711. } break;
  11712. case GGML_TYPE_IQ1_S:
  11713. {
  11714. VALIDATE_ROW_DATA_D_F16_IMPL(block_iq1_s, data, nb);
  11715. } break;
  11716. case GGML_TYPE_IQ1_M:
  11717. {
  11718. const block_iq1_m * q = (const block_iq1_m *) data;
  11719. for (size_t i = 0; i < nb; ++i) {
  11720. iq1m_scale_t scale;
  11721. const uint16_t * sc = (const uint16_t *)q[i].scales;
  11722. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  11723. if (!validate_fp16(scale.f16, i)) {
  11724. return false;
  11725. }
  11726. }
  11727. } break;
  11728. case GGML_TYPE_IQ2_XXS:
  11729. {
  11730. VALIDATE_ROW_DATA_D_F16_IMPL(block_iq2_xxs, data, nb);
  11731. } break;
  11732. case GGML_TYPE_IQ2_XS:
  11733. {
  11734. VALIDATE_ROW_DATA_D_F16_IMPL(block_iq2_xs, data, nb);
  11735. } break;
  11736. case GGML_TYPE_IQ2_S:
  11737. {
  11738. VALIDATE_ROW_DATA_D_F16_IMPL(block_iq2_s, data, nb);
  11739. } break;
  11740. case GGML_TYPE_IQ3_XXS:
  11741. {
  11742. VALIDATE_ROW_DATA_D_F16_IMPL(block_iq3_xxs, data, nb);
  11743. } break;
  11744. case GGML_TYPE_IQ3_S:
  11745. {
  11746. VALIDATE_ROW_DATA_D_F16_IMPL(block_iq3_s, data, nb);
  11747. } break;
  11748. case GGML_TYPE_IQ4_XS:
  11749. {
  11750. VALIDATE_ROW_DATA_D_F16_IMPL(block_iq4_xs, data, nb);
  11751. } break;
  11752. case GGML_TYPE_IQ4_NL:
  11753. {
  11754. VALIDATE_ROW_DATA_D_F16_IMPL(block_iq4_nl, data, nb);
  11755. } break;
  11756. case GGML_TYPE_I8:
  11757. case GGML_TYPE_I16:
  11758. case GGML_TYPE_I32:
  11759. case GGML_TYPE_I64:
  11760. // nothing to validate
  11761. break;
  11762. default:
  11763. {
  11764. fprintf(stderr, "%s: invalid type %d\n", __func__, type);
  11765. return false;
  11766. }
  11767. }
  11768. return true;
  11769. }