ggml-quants.c 390 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838
  1. #include "ggml-quants.h"
  2. #include "ggml-impl.h"
  3. #include <math.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include <float.h>
  7. #include <stdlib.h> // for qsort
  8. #include <stdio.h> // for GGML_ASSERT
  9. #ifdef __ARM_NEON
  10. // if YCM cannot find <arm_neon.h>, make a symbolic link to it, for example:
  11. //
  12. // $ ln -sfn /Library/Developer/CommandLineTools/usr/lib/clang/13.1.6/include/arm_neon.h ./src/
  13. //
  14. #include <arm_neon.h>
  15. #else
  16. #ifdef __wasm_simd128__
  17. #include <wasm_simd128.h>
  18. #else
  19. #if defined(__POWER9_VECTOR__) || defined(__powerpc64__)
  20. #include <altivec.h>
  21. #undef bool
  22. #define bool _Bool
  23. #else
  24. #if defined(_MSC_VER) || defined(__MINGW32__)
  25. #include <intrin.h>
  26. #else
  27. #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__) || defined(__SSE3__)
  28. #if !defined(__riscv)
  29. #include <immintrin.h>
  30. #endif
  31. #endif
  32. #endif
  33. #endif
  34. #endif
  35. #endif
  36. #ifdef __riscv_v_intrinsic
  37. #include <riscv_vector.h>
  38. #endif
  39. #undef MIN
  40. #undef MAX
  41. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  42. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  43. #define MM256_SET_M128I(a, b) _mm256_insertf128_si256(_mm256_castsi128_si256(b), (a), 1)
  44. #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__)
  45. // multiply int8_t, add results pairwise twice
  46. static inline __m128i mul_sum_i8_pairs(const __m128i x, const __m128i y) {
  47. // Get absolute values of x vectors
  48. const __m128i ax = _mm_sign_epi8(x, x);
  49. // Sign the values of the y vectors
  50. const __m128i sy = _mm_sign_epi8(y, x);
  51. // Perform multiplication and create 16-bit values
  52. const __m128i dot = _mm_maddubs_epi16(ax, sy);
  53. const __m128i ones = _mm_set1_epi16(1);
  54. return _mm_madd_epi16(ones, dot);
  55. }
  56. #if __AVX__ || __AVX2__ || __AVX512F__
  57. // horizontally add 8 floats
  58. static inline float hsum_float_8(const __m256 x) {
  59. __m128 res = _mm256_extractf128_ps(x, 1);
  60. res = _mm_add_ps(res, _mm256_castps256_ps128(x));
  61. res = _mm_add_ps(res, _mm_movehl_ps(res, res));
  62. res = _mm_add_ss(res, _mm_movehdup_ps(res));
  63. return _mm_cvtss_f32(res);
  64. }
  65. // horizontally add 8 int32_t
  66. static inline int hsum_i32_8(const __m256i a) {
  67. const __m128i sum128 = _mm_add_epi32(_mm256_castsi256_si128(a), _mm256_extractf128_si256(a, 1));
  68. const __m128i hi64 = _mm_unpackhi_epi64(sum128, sum128);
  69. const __m128i sum64 = _mm_add_epi32(hi64, sum128);
  70. const __m128i hi32 = _mm_shuffle_epi32(sum64, _MM_SHUFFLE(2, 3, 0, 1));
  71. return _mm_cvtsi128_si32(_mm_add_epi32(sum64, hi32));
  72. }
  73. // horizontally add 4 int32_t
  74. static inline int hsum_i32_4(const __m128i a) {
  75. const __m128i hi64 = _mm_unpackhi_epi64(a, a);
  76. const __m128i sum64 = _mm_add_epi32(hi64, a);
  77. const __m128i hi32 = _mm_shuffle_epi32(sum64, _MM_SHUFFLE(2, 3, 0, 1));
  78. return _mm_cvtsi128_si32(_mm_add_epi32(sum64, hi32));
  79. }
  80. #if defined(__AVX2__) || defined(__AVX512F__)
  81. // spread 32 bits to 32 bytes { 0x00, 0xFF }
  82. static inline __m256i bytes_from_bits_32(const uint8_t * x) {
  83. uint32_t x32;
  84. memcpy(&x32, x, sizeof(uint32_t));
  85. const __m256i shuf_mask = _mm256_set_epi64x(
  86. 0x0303030303030303, 0x0202020202020202,
  87. 0x0101010101010101, 0x0000000000000000);
  88. __m256i bytes = _mm256_shuffle_epi8(_mm256_set1_epi32(x32), shuf_mask);
  89. const __m256i bit_mask = _mm256_set1_epi64x(0x7fbfdfeff7fbfdfe);
  90. bytes = _mm256_or_si256(bytes, bit_mask);
  91. return _mm256_cmpeq_epi8(bytes, _mm256_set1_epi64x(-1));
  92. }
  93. // Unpack 32 4-bit fields into 32 bytes
  94. // The output vector contains 32 bytes, each one in [ 0 .. 15 ] interval
  95. static inline __m256i bytes_from_nibbles_32(const uint8_t * rsi)
  96. {
  97. const __m128i tmp = _mm_loadu_si128((const __m128i *)rsi);
  98. const __m256i bytes = MM256_SET_M128I(_mm_srli_epi16(tmp, 4), tmp);
  99. const __m256i lowMask = _mm256_set1_epi8( 0xF );
  100. return _mm256_and_si256(lowMask, bytes);
  101. }
  102. // add int16_t pairwise and return as float vector
  103. static inline __m256 sum_i16_pairs_float(const __m256i x) {
  104. const __m256i ones = _mm256_set1_epi16(1);
  105. const __m256i summed_pairs = _mm256_madd_epi16(ones, x);
  106. return _mm256_cvtepi32_ps(summed_pairs);
  107. }
  108. static inline __m256 mul_sum_us8_pairs_float(const __m256i ax, const __m256i sy) {
  109. #if __AVXVNNI__
  110. const __m256i zero = _mm256_setzero_si256();
  111. const __m256i summed_pairs = _mm256_dpbusd_epi32(zero, ax, sy);
  112. return _mm256_cvtepi32_ps(summed_pairs);
  113. #else
  114. // Perform multiplication and create 16-bit values
  115. const __m256i dot = _mm256_maddubs_epi16(ax, sy);
  116. return sum_i16_pairs_float(dot);
  117. #endif
  118. }
  119. // multiply int8_t, add results pairwise twice and return as float vector
  120. static inline __m256 mul_sum_i8_pairs_float(const __m256i x, const __m256i y) {
  121. #if __AVXVNNIINT8__
  122. const __m256i zero = _mm256_setzero_si256();
  123. const __m256i summed_pairs = _mm256_dpbssd_epi32(zero, x, y);
  124. return _mm256_cvtepi32_ps(summed_pairs);
  125. #else
  126. // Get absolute values of x vectors
  127. const __m256i ax = _mm256_sign_epi8(x, x);
  128. // Sign the values of the y vectors
  129. const __m256i sy = _mm256_sign_epi8(y, x);
  130. return mul_sum_us8_pairs_float(ax, sy);
  131. #endif
  132. }
  133. static inline __m128i packNibbles( __m256i bytes )
  134. {
  135. // Move bits within 16-bit lanes from 0000_abcd_0000_efgh into 0000_0000_abcd_efgh
  136. #if __AVX512F__
  137. const __m256i bytes_srli_4 = _mm256_srli_epi16(bytes, 4); // 0000_0000_abcd_0000
  138. bytes = _mm256_or_si256(bytes, bytes_srli_4); // 0000_abcd_abcd_efgh
  139. return _mm256_cvtepi16_epi8(bytes); // abcd_efgh
  140. #else
  141. const __m256i lowByte = _mm256_set1_epi16( 0xFF );
  142. __m256i high = _mm256_andnot_si256( lowByte, bytes );
  143. __m256i low = _mm256_and_si256( lowByte, bytes );
  144. high = _mm256_srli_epi16( high, 4 );
  145. bytes = _mm256_or_si256( low, high );
  146. // Compress uint16_t lanes into bytes
  147. __m128i r0 = _mm256_castsi256_si128( bytes );
  148. __m128i r1 = _mm256_extracti128_si256( bytes, 1 );
  149. return _mm_packus_epi16( r0, r1 );
  150. #endif
  151. }
  152. #elif defined(__AVX__)
  153. // spread 32 bits to 32 bytes { 0x00, 0xFF }
  154. static inline __m256i bytes_from_bits_32(const uint8_t * x) {
  155. uint32_t x32;
  156. memcpy(&x32, x, sizeof(uint32_t));
  157. const __m128i shuf_maskl = _mm_set_epi64x(0x0101010101010101, 0x0000000000000000);
  158. const __m128i shuf_maskh = _mm_set_epi64x(0x0303030303030303, 0x0202020202020202);
  159. __m128i bytesl = _mm_shuffle_epi8(_mm_set1_epi32(x32), shuf_maskl);
  160. __m128i bytesh = _mm_shuffle_epi8(_mm_set1_epi32(x32), shuf_maskh);
  161. const __m128i bit_mask = _mm_set1_epi64x(0x7fbfdfeff7fbfdfe);
  162. bytesl = _mm_or_si128(bytesl, bit_mask);
  163. bytesh = _mm_or_si128(bytesh, bit_mask);
  164. bytesl = _mm_cmpeq_epi8(bytesl, _mm_set1_epi64x(-1));
  165. bytesh = _mm_cmpeq_epi8(bytesh, _mm_set1_epi64x(-1));
  166. return MM256_SET_M128I(bytesh, bytesl);
  167. }
  168. // Unpack 32 4-bit fields into 32 bytes
  169. // The output vector contains 32 bytes, each one in [ 0 .. 15 ] interval
  170. static inline __m256i bytes_from_nibbles_32(const uint8_t * rsi)
  171. {
  172. // Load 16 bytes from memory
  173. __m128i tmpl = _mm_loadu_si128((const __m128i *)rsi);
  174. __m128i tmph = _mm_srli_epi16(tmpl, 4);
  175. const __m128i lowMask = _mm_set1_epi8(0xF);
  176. tmpl = _mm_and_si128(lowMask, tmpl);
  177. tmph = _mm_and_si128(lowMask, tmph);
  178. return MM256_SET_M128I(tmph, tmpl);
  179. }
  180. // add int16_t pairwise and return as float vector
  181. static inline __m256 sum_i16_pairs_float(const __m128i xh, const __m128i xl) {
  182. const __m128i ones = _mm_set1_epi16(1);
  183. const __m128i summed_pairsl = _mm_madd_epi16(ones, xl);
  184. const __m128i summed_pairsh = _mm_madd_epi16(ones, xh);
  185. const __m256i summed_pairs = MM256_SET_M128I(summed_pairsh, summed_pairsl);
  186. return _mm256_cvtepi32_ps(summed_pairs);
  187. }
  188. static inline __m256 mul_sum_us8_pairs_float(const __m256i ax, const __m256i sy) {
  189. const __m128i axl = _mm256_castsi256_si128(ax);
  190. const __m128i axh = _mm256_extractf128_si256(ax, 1);
  191. const __m128i syl = _mm256_castsi256_si128(sy);
  192. const __m128i syh = _mm256_extractf128_si256(sy, 1);
  193. // Perform multiplication and create 16-bit values
  194. const __m128i dotl = _mm_maddubs_epi16(axl, syl);
  195. const __m128i doth = _mm_maddubs_epi16(axh, syh);
  196. return sum_i16_pairs_float(doth, dotl);
  197. }
  198. // multiply int8_t, add results pairwise twice and return as float vector
  199. static inline __m256 mul_sum_i8_pairs_float(const __m256i x, const __m256i y) {
  200. const __m128i xl = _mm256_castsi256_si128(x);
  201. const __m128i xh = _mm256_extractf128_si256(x, 1);
  202. const __m128i yl = _mm256_castsi256_si128(y);
  203. const __m128i yh = _mm256_extractf128_si256(y, 1);
  204. // Get absolute values of x vectors
  205. const __m128i axl = _mm_sign_epi8(xl, xl);
  206. const __m128i axh = _mm_sign_epi8(xh, xh);
  207. // Sign the values of the y vectors
  208. const __m128i syl = _mm_sign_epi8(yl, xl);
  209. const __m128i syh = _mm_sign_epi8(yh, xh);
  210. // Perform multiplication and create 16-bit values
  211. const __m128i dotl = _mm_maddubs_epi16(axl, syl);
  212. const __m128i doth = _mm_maddubs_epi16(axh, syh);
  213. return sum_i16_pairs_float(doth, dotl);
  214. }
  215. static inline __m128i packNibbles( __m128i bytes1, __m128i bytes2 )
  216. {
  217. // Move bits within 16-bit lanes from 0000_abcd_0000_efgh into 0000_0000_abcd_efgh
  218. const __m128i lowByte = _mm_set1_epi16( 0xFF );
  219. __m128i high = _mm_andnot_si128( lowByte, bytes1 );
  220. __m128i low = _mm_and_si128( lowByte, bytes1 );
  221. high = _mm_srli_epi16( high, 4 );
  222. bytes1 = _mm_or_si128( low, high );
  223. high = _mm_andnot_si128( lowByte, bytes2 );
  224. low = _mm_and_si128( lowByte, bytes2 );
  225. high = _mm_srli_epi16( high, 4 );
  226. bytes2 = _mm_or_si128( low, high );
  227. return _mm_packus_epi16( bytes1, bytes2);
  228. }
  229. #endif
  230. #elif defined(__SSSE3__)
  231. // horizontally add 4x4 floats
  232. static inline float hsum_float_4x4(const __m128 a, const __m128 b, const __m128 c, const __m128 d) {
  233. __m128 res_0 =_mm_hadd_ps(a, b);
  234. __m128 res_1 =_mm_hadd_ps(c, d);
  235. __m128 res =_mm_hadd_ps(res_0, res_1);
  236. res =_mm_hadd_ps(res, res);
  237. res =_mm_hadd_ps(res, res);
  238. return _mm_cvtss_f32(res);
  239. }
  240. #endif // __AVX__ || __AVX2__ || __AVX512F__
  241. #endif // defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__)
  242. #if defined(__ARM_NEON)
  243. #ifdef _MSC_VER
  244. #define ggml_vld1q_u32(w,x,y,z) { ((w) + ((uint64_t)(x) << 32)), ((y) + ((uint64_t)(z) << 32)) }
  245. #else
  246. #define ggml_vld1q_u32(w,x,y,z) { (w), (x), (y), (z) }
  247. #endif
  248. #if !defined(__aarch64__)
  249. // 64-bit compatibility
  250. // vaddvq_s16
  251. // vpaddq_s16
  252. // vpaddq_s32
  253. // vaddvq_s32
  254. // vaddvq_f32
  255. // vmaxvq_f32
  256. // vcvtnq_s32_f32
  257. // vzip1_u8
  258. // vzip2_u8
  259. inline static int32_t vaddvq_s16(int16x8_t v) {
  260. return
  261. (int32_t)vgetq_lane_s16(v, 0) + (int32_t)vgetq_lane_s16(v, 1) +
  262. (int32_t)vgetq_lane_s16(v, 2) + (int32_t)vgetq_lane_s16(v, 3) +
  263. (int32_t)vgetq_lane_s16(v, 4) + (int32_t)vgetq_lane_s16(v, 5) +
  264. (int32_t)vgetq_lane_s16(v, 6) + (int32_t)vgetq_lane_s16(v, 7);
  265. }
  266. inline static int16x8_t vpaddq_s16(int16x8_t a, int16x8_t b) {
  267. int16x4_t a0 = vpadd_s16(vget_low_s16(a), vget_high_s16(a));
  268. int16x4_t b0 = vpadd_s16(vget_low_s16(b), vget_high_s16(b));
  269. return vcombine_s16(a0, b0);
  270. }
  271. inline static int32x4_t vpaddq_s32(int32x4_t a, int32x4_t b) {
  272. int32x2_t a0 = vpadd_s32(vget_low_s32(a), vget_high_s32(a));
  273. int32x2_t b0 = vpadd_s32(vget_low_s32(b), vget_high_s32(b));
  274. return vcombine_s32(a0, b0);
  275. }
  276. inline static int32_t vaddvq_s32(int32x4_t v) {
  277. return vgetq_lane_s32(v, 0) + vgetq_lane_s32(v, 1) + vgetq_lane_s32(v, 2) + vgetq_lane_s32(v, 3);
  278. }
  279. inline static float vaddvq_f32(float32x4_t v) {
  280. return vgetq_lane_f32(v, 0) + vgetq_lane_f32(v, 1) + vgetq_lane_f32(v, 2) + vgetq_lane_f32(v, 3);
  281. }
  282. inline static float vmaxvq_f32(float32x4_t v) {
  283. return
  284. MAX(MAX(vgetq_lane_f32(v, 0), vgetq_lane_f32(v, 1)),
  285. MAX(vgetq_lane_f32(v, 2), vgetq_lane_f32(v, 3)));
  286. }
  287. inline static int32x4_t vcvtnq_s32_f32(float32x4_t v) {
  288. int32x4_t res;
  289. res[0] = roundf(vgetq_lane_f32(v, 0));
  290. res[1] = roundf(vgetq_lane_f32(v, 1));
  291. res[2] = roundf(vgetq_lane_f32(v, 2));
  292. res[3] = roundf(vgetq_lane_f32(v, 3));
  293. return res;
  294. }
  295. inline static uint8x8_t vzip1_u8(uint8x8_t a, uint8x8_t b) {
  296. uint8x8_t res;
  297. res[0] = a[0]; res[1] = b[0];
  298. res[2] = a[1]; res[3] = b[1];
  299. res[4] = a[2]; res[5] = b[2];
  300. res[6] = a[3]; res[7] = b[3];
  301. return res;
  302. }
  303. inline static uint8x8_t vzip2_u8(uint8x8_t a, uint8x8_t b) {
  304. uint8x8_t res;
  305. res[0] = a[4]; res[1] = b[4];
  306. res[2] = a[5]; res[3] = b[5];
  307. res[4] = a[6]; res[5] = b[6];
  308. res[6] = a[7]; res[7] = b[7];
  309. return res;
  310. }
  311. // vld1q_s16_x2
  312. // vld1q_u8_x2
  313. // vld1q_u8_x4
  314. // vld1q_s8_x2
  315. // vld1q_s8_x4
  316. // TODO: double-check these work correctly
  317. typedef struct ggml_int16x8x2_t {
  318. int16x8_t val[2];
  319. } ggml_int16x8x2_t;
  320. inline static ggml_int16x8x2_t ggml_vld1q_s16_x2(const int16_t * ptr) {
  321. ggml_int16x8x2_t res;
  322. res.val[0] = vld1q_s16(ptr + 0);
  323. res.val[1] = vld1q_s16(ptr + 8);
  324. return res;
  325. }
  326. typedef struct ggml_uint8x16x2_t {
  327. uint8x16_t val[2];
  328. } ggml_uint8x16x2_t;
  329. inline static ggml_uint8x16x2_t ggml_vld1q_u8_x2(const uint8_t * ptr) {
  330. ggml_uint8x16x2_t res;
  331. res.val[0] = vld1q_u8(ptr + 0);
  332. res.val[1] = vld1q_u8(ptr + 16);
  333. return res;
  334. }
  335. typedef struct ggml_uint8x16x4_t {
  336. uint8x16_t val[4];
  337. } ggml_uint8x16x4_t;
  338. inline static ggml_uint8x16x4_t ggml_vld1q_u8_x4(const uint8_t * ptr) {
  339. ggml_uint8x16x4_t res;
  340. res.val[0] = vld1q_u8(ptr + 0);
  341. res.val[1] = vld1q_u8(ptr + 16);
  342. res.val[2] = vld1q_u8(ptr + 32);
  343. res.val[3] = vld1q_u8(ptr + 48);
  344. return res;
  345. }
  346. typedef struct ggml_int8x16x2_t {
  347. int8x16_t val[2];
  348. } ggml_int8x16x2_t;
  349. inline static ggml_int8x16x2_t ggml_vld1q_s8_x2(const int8_t * ptr) {
  350. ggml_int8x16x2_t res;
  351. res.val[0] = vld1q_s8(ptr + 0);
  352. res.val[1] = vld1q_s8(ptr + 16);
  353. return res;
  354. }
  355. typedef struct ggml_int8x16x4_t {
  356. int8x16_t val[4];
  357. } ggml_int8x16x4_t;
  358. inline static ggml_int8x16x4_t ggml_vld1q_s8_x4(const int8_t * ptr) {
  359. ggml_int8x16x4_t res;
  360. res.val[0] = vld1q_s8(ptr + 0);
  361. res.val[1] = vld1q_s8(ptr + 16);
  362. res.val[2] = vld1q_s8(ptr + 32);
  363. res.val[3] = vld1q_s8(ptr + 48);
  364. return res;
  365. }
  366. #else
  367. #define ggml_int16x8x2_t int16x8x2_t
  368. #define ggml_uint8x16x2_t uint8x16x2_t
  369. #define ggml_uint8x16x4_t uint8x16x4_t
  370. #define ggml_int8x16x2_t int8x16x2_t
  371. #define ggml_int8x16x4_t int8x16x4_t
  372. #define ggml_vld1q_s16_x2 vld1q_s16_x2
  373. #define ggml_vld1q_u8_x2 vld1q_u8_x2
  374. #define ggml_vld1q_u8_x4 vld1q_u8_x4
  375. #define ggml_vld1q_s8_x2 vld1q_s8_x2
  376. #define ggml_vld1q_s8_x4 vld1q_s8_x4
  377. #endif
  378. #if !defined(__ARM_FEATURE_DOTPROD)
  379. inline static int32x4_t ggml_vdotq_s32(int32x4_t acc, int8x16_t a, int8x16_t b) {
  380. const int16x8_t p0 = vmull_s8(vget_low_s8 (a), vget_low_s8 (b));
  381. const int16x8_t p1 = vmull_s8(vget_high_s8(a), vget_high_s8(b));
  382. return vaddq_s32(acc, vaddq_s32(vpaddlq_s16(p0), vpaddlq_s16(p1)));
  383. }
  384. #else
  385. #define ggml_vdotq_s32(a, b, c) vdotq_s32(a, b, c)
  386. #endif
  387. #endif
  388. #if defined(__ARM_NEON) || defined(__wasm_simd128__)
  389. #define B1(c,s,n) 0x ## n ## c , 0x ## n ## s
  390. #define B2(c,s,n) B1(c,s,n ## c), B1(c,s,n ## s)
  391. #define B3(c,s,n) B2(c,s,n ## c), B2(c,s,n ## s)
  392. #define B4(c,s,n) B3(c,s,n ## c), B3(c,s,n ## s)
  393. #define B5(c,s,n) B4(c,s,n ## c), B4(c,s,n ## s)
  394. #define B6(c,s,n) B5(c,s,n ## c), B5(c,s,n ## s)
  395. #define B7(c,s,n) B6(c,s,n ## c), B6(c,s,n ## s)
  396. #define B8(c,s ) B7(c,s, c), B7(c,s, s)
  397. // precomputed tables for expanding 8bits to 8 bytes:
  398. static const uint64_t table_b2b_0[1 << 8] = { B8(00, 10) }; // ( b) << 4
  399. static const uint64_t table_b2b_1[1 << 8] = { B8(10, 00) }; // (!b) << 4
  400. #endif
  401. // reference implementation for deterministic creation of model files
  402. void quantize_row_q4_0_reference(const float * restrict x, block_q4_0 * restrict y, int k) {
  403. static const int qk = QK4_0;
  404. assert(k % qk == 0);
  405. const int nb = k / qk;
  406. for (int i = 0; i < nb; i++) {
  407. float amax = 0.0f; // absolute max
  408. float max = 0.0f;
  409. for (int j = 0; j < qk; j++) {
  410. const float v = x[i*qk + j];
  411. if (amax < fabsf(v)) {
  412. amax = fabsf(v);
  413. max = v;
  414. }
  415. }
  416. const float d = max / -8;
  417. const float id = d ? 1.0f/d : 0.0f;
  418. y[i].d = GGML_FP32_TO_FP16(d);
  419. for (int j = 0; j < qk/2; ++j) {
  420. const float x0 = x[i*qk + 0 + j]*id;
  421. const float x1 = x[i*qk + qk/2 + j]*id;
  422. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 8.5f));
  423. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 8.5f));
  424. y[i].qs[j] = xi0;
  425. y[i].qs[j] |= xi1 << 4;
  426. }
  427. }
  428. }
  429. void quantize_row_q4_0(const float * restrict x, void * restrict y, int k) {
  430. quantize_row_q4_0_reference(x, y, k);
  431. }
  432. void quantize_row_q4_1_reference(const float * restrict x, block_q4_1 * restrict y, int k) {
  433. const int qk = QK4_1;
  434. assert(k % qk == 0);
  435. const int nb = k / qk;
  436. for (int i = 0; i < nb; i++) {
  437. float min = FLT_MAX;
  438. float max = -FLT_MAX;
  439. for (int j = 0; j < qk; j++) {
  440. const float v = x[i*qk + j];
  441. if (v < min) min = v;
  442. if (v > max) max = v;
  443. }
  444. const float d = (max - min) / ((1 << 4) - 1);
  445. const float id = d ? 1.0f/d : 0.0f;
  446. y[i].d = GGML_FP32_TO_FP16(d);
  447. y[i].m = GGML_FP32_TO_FP16(min);
  448. for (int j = 0; j < qk/2; ++j) {
  449. const float x0 = (x[i*qk + 0 + j] - min)*id;
  450. const float x1 = (x[i*qk + qk/2 + j] - min)*id;
  451. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 0.5f));
  452. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 0.5f));
  453. y[i].qs[j] = xi0;
  454. y[i].qs[j] |= xi1 << 4;
  455. }
  456. }
  457. }
  458. void quantize_row_q4_1(const float * restrict x, void * restrict y, int k) {
  459. quantize_row_q4_1_reference(x, y, k);
  460. }
  461. void quantize_row_q5_0_reference(const float * restrict x, block_q5_0 * restrict y, int k) {
  462. static const int qk = QK5_0;
  463. assert(k % qk == 0);
  464. const int nb = k / qk;
  465. for (int i = 0; i < nb; i++) {
  466. float amax = 0.0f; // absolute max
  467. float max = 0.0f;
  468. for (int j = 0; j < qk; j++) {
  469. const float v = x[i*qk + j];
  470. if (amax < fabsf(v)) {
  471. amax = fabsf(v);
  472. max = v;
  473. }
  474. }
  475. const float d = max / -16;
  476. const float id = d ? 1.0f/d : 0.0f;
  477. y[i].d = GGML_FP32_TO_FP16(d);
  478. uint32_t qh = 0;
  479. for (int j = 0; j < qk/2; ++j) {
  480. const float x0 = x[i*qk + 0 + j]*id;
  481. const float x1 = x[i*qk + qk/2 + j]*id;
  482. const uint8_t xi0 = MIN(31, (int8_t)(x0 + 16.5f));
  483. const uint8_t xi1 = MIN(31, (int8_t)(x1 + 16.5f));
  484. y[i].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  485. // get the 5-th bit and store it in qh at the right position
  486. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  487. qh |= ((xi1 & 0x10u) >> 4) << (j + qk/2);
  488. }
  489. memcpy(&y[i].qh, &qh, sizeof(qh));
  490. }
  491. }
  492. void quantize_row_q5_0(const float * restrict x, void * restrict y, int k) {
  493. quantize_row_q5_0_reference(x, y, k);
  494. }
  495. void quantize_row_q5_1_reference(const float * restrict x, block_q5_1 * restrict y, int k) {
  496. const int qk = QK5_1;
  497. assert(k % qk == 0);
  498. const int nb = k / qk;
  499. for (int i = 0; i < nb; i++) {
  500. float min = FLT_MAX;
  501. float max = -FLT_MAX;
  502. for (int j = 0; j < qk; j++) {
  503. const float v = x[i*qk + j];
  504. if (v < min) min = v;
  505. if (v > max) max = v;
  506. }
  507. const float d = (max - min) / ((1 << 5) - 1);
  508. const float id = d ? 1.0f/d : 0.0f;
  509. y[i].d = GGML_FP32_TO_FP16(d);
  510. y[i].m = GGML_FP32_TO_FP16(min);
  511. uint32_t qh = 0;
  512. for (int j = 0; j < qk/2; ++j) {
  513. const float x0 = (x[i*qk + 0 + j] - min)*id;
  514. const float x1 = (x[i*qk + qk/2 + j] - min)*id;
  515. const uint8_t xi0 = (uint8_t)(x0 + 0.5f);
  516. const uint8_t xi1 = (uint8_t)(x1 + 0.5f);
  517. y[i].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  518. // get the 5-th bit and store it in qh at the right position
  519. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  520. qh |= ((xi1 & 0x10u) >> 4) << (j + qk/2);
  521. }
  522. memcpy(&y[i].qh, &qh, sizeof(y[i].qh));
  523. }
  524. }
  525. void quantize_row_q5_1(const float * restrict x, void * restrict y, int k) {
  526. quantize_row_q5_1_reference(x, y, k);
  527. }
  528. // reference implementation for deterministic creation of model files
  529. void quantize_row_q8_0_reference(const float * restrict x, block_q8_0 * restrict y, int k) {
  530. assert(k % QK8_0 == 0);
  531. const int nb = k / QK8_0;
  532. for (int i = 0; i < nb; i++) {
  533. float amax = 0.0f; // absolute max
  534. for (int j = 0; j < QK8_0; j++) {
  535. const float v = x[i*QK8_0 + j];
  536. amax = MAX(amax, fabsf(v));
  537. }
  538. const float d = amax / ((1 << 7) - 1);
  539. const float id = d ? 1.0f/d : 0.0f;
  540. y[i].d = GGML_FP32_TO_FP16(d);
  541. for (int j = 0; j < QK8_0; ++j) {
  542. const float x0 = x[i*QK8_0 + j]*id;
  543. y[i].qs[j] = roundf(x0);
  544. }
  545. }
  546. }
  547. void quantize_row_q8_0(const float * restrict x, void * restrict vy, int k) {
  548. assert(QK8_0 == 32);
  549. assert(k % QK8_0 == 0);
  550. const int nb = k / QK8_0;
  551. block_q8_0 * restrict y = vy;
  552. #if defined(__ARM_NEON)
  553. for (int i = 0; i < nb; i++) {
  554. float32x4_t srcv [8];
  555. float32x4_t asrcv[8];
  556. float32x4_t amaxv[8];
  557. for (int j = 0; j < 8; j++) srcv[j] = vld1q_f32(x + i*32 + 4*j);
  558. for (int j = 0; j < 8; j++) asrcv[j] = vabsq_f32(srcv[j]);
  559. for (int j = 0; j < 4; j++) amaxv[2*j] = vmaxq_f32(asrcv[2*j], asrcv[2*j+1]);
  560. for (int j = 0; j < 2; j++) amaxv[4*j] = vmaxq_f32(amaxv[4*j], amaxv[4*j+2]);
  561. for (int j = 0; j < 1; j++) amaxv[8*j] = vmaxq_f32(amaxv[8*j], amaxv[8*j+4]);
  562. const float amax = vmaxvq_f32(amaxv[0]);
  563. const float d = amax / ((1 << 7) - 1);
  564. const float id = d ? 1.0f/d : 0.0f;
  565. y[i].d = GGML_FP32_TO_FP16(d);
  566. for (int j = 0; j < 8; j++) {
  567. const float32x4_t v = vmulq_n_f32(srcv[j], id);
  568. const int32x4_t vi = vcvtnq_s32_f32(v);
  569. y[i].qs[4*j + 0] = vgetq_lane_s32(vi, 0);
  570. y[i].qs[4*j + 1] = vgetq_lane_s32(vi, 1);
  571. y[i].qs[4*j + 2] = vgetq_lane_s32(vi, 2);
  572. y[i].qs[4*j + 3] = vgetq_lane_s32(vi, 3);
  573. }
  574. }
  575. #elif defined(__wasm_simd128__)
  576. for (int i = 0; i < nb; i++) {
  577. v128_t srcv [8];
  578. v128_t asrcv[8];
  579. v128_t amaxv[8];
  580. for (int j = 0; j < 8; j++) srcv[j] = wasm_v128_load(x + i*32 + 4*j);
  581. for (int j = 0; j < 8; j++) asrcv[j] = wasm_f32x4_abs(srcv[j]);
  582. for (int j = 0; j < 4; j++) amaxv[2*j] = wasm_f32x4_max(asrcv[2*j], asrcv[2*j+1]);
  583. for (int j = 0; j < 2; j++) amaxv[4*j] = wasm_f32x4_max(amaxv[4*j], amaxv[4*j+2]);
  584. for (int j = 0; j < 1; j++) amaxv[8*j] = wasm_f32x4_max(amaxv[8*j], amaxv[8*j+4]);
  585. const float amax = MAX(MAX(wasm_f32x4_extract_lane(amaxv[0], 0),
  586. wasm_f32x4_extract_lane(amaxv[0], 1)),
  587. MAX(wasm_f32x4_extract_lane(amaxv[0], 2),
  588. wasm_f32x4_extract_lane(amaxv[0], 3)));
  589. const float d = amax / ((1 << 7) - 1);
  590. const float id = d ? 1.0f/d : 0.0f;
  591. y[i].d = GGML_FP32_TO_FP16(d);
  592. for (int j = 0; j < 8; j++) {
  593. const v128_t v = wasm_f32x4_mul(srcv[j], wasm_f32x4_splat(id));
  594. const v128_t vi = wasm_i32x4_trunc_sat_f32x4(v);
  595. y[i].qs[4*j + 0] = wasm_i32x4_extract_lane(vi, 0);
  596. y[i].qs[4*j + 1] = wasm_i32x4_extract_lane(vi, 1);
  597. y[i].qs[4*j + 2] = wasm_i32x4_extract_lane(vi, 2);
  598. y[i].qs[4*j + 3] = wasm_i32x4_extract_lane(vi, 3);
  599. }
  600. }
  601. #elif defined(__AVX2__) || defined(__AVX__)
  602. for (int i = 0; i < nb; i++) {
  603. // Load elements into 4 AVX vectors
  604. __m256 v0 = _mm256_loadu_ps( x );
  605. __m256 v1 = _mm256_loadu_ps( x + 8 );
  606. __m256 v2 = _mm256_loadu_ps( x + 16 );
  607. __m256 v3 = _mm256_loadu_ps( x + 24 );
  608. x += 32;
  609. // Compute max(abs(e)) for the block
  610. const __m256 signBit = _mm256_set1_ps( -0.0f );
  611. __m256 maxAbs = _mm256_andnot_ps( signBit, v0 );
  612. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v1 ) );
  613. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v2 ) );
  614. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v3 ) );
  615. __m128 max4 = _mm_max_ps( _mm256_extractf128_ps( maxAbs, 1 ), _mm256_castps256_ps128( maxAbs ) );
  616. max4 = _mm_max_ps( max4, _mm_movehl_ps( max4, max4 ) );
  617. max4 = _mm_max_ss( max4, _mm_movehdup_ps( max4 ) );
  618. const float maxScalar = _mm_cvtss_f32( max4 );
  619. // Quantize these floats
  620. const float d = maxScalar / 127.f;
  621. y[i].d = GGML_FP32_TO_FP16(d);
  622. const float id = ( maxScalar != 0.0f ) ? 127.f / maxScalar : 0.0f;
  623. const __m256 mul = _mm256_set1_ps( id );
  624. // Apply the multiplier
  625. v0 = _mm256_mul_ps( v0, mul );
  626. v1 = _mm256_mul_ps( v1, mul );
  627. v2 = _mm256_mul_ps( v2, mul );
  628. v3 = _mm256_mul_ps( v3, mul );
  629. // Round to nearest integer
  630. v0 = _mm256_round_ps( v0, _MM_ROUND_NEAREST );
  631. v1 = _mm256_round_ps( v1, _MM_ROUND_NEAREST );
  632. v2 = _mm256_round_ps( v2, _MM_ROUND_NEAREST );
  633. v3 = _mm256_round_ps( v3, _MM_ROUND_NEAREST );
  634. // Convert floats to integers
  635. __m256i i0 = _mm256_cvtps_epi32( v0 );
  636. __m256i i1 = _mm256_cvtps_epi32( v1 );
  637. __m256i i2 = _mm256_cvtps_epi32( v2 );
  638. __m256i i3 = _mm256_cvtps_epi32( v3 );
  639. #if defined(__AVX2__)
  640. // Convert int32 to int16
  641. i0 = _mm256_packs_epi32( i0, i1 ); // 0, 1, 2, 3, 8, 9, 10, 11, 4, 5, 6, 7, 12, 13, 14, 15
  642. i2 = _mm256_packs_epi32( i2, i3 ); // 16, 17, 18, 19, 24, 25, 26, 27, 20, 21, 22, 23, 28, 29, 30, 31
  643. // Convert int16 to int8
  644. 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
  645. // We got our precious signed bytes, but the order is now wrong
  646. // These AVX2 pack instructions process 16-byte pieces independently
  647. // The following instruction is fixing the order
  648. const __m256i perm = _mm256_setr_epi32( 0, 4, 1, 5, 2, 6, 3, 7 );
  649. i0 = _mm256_permutevar8x32_epi32( i0, perm );
  650. _mm256_storeu_si256((__m256i *)y[i].qs, i0);
  651. #else
  652. // Since we don't have in AVX some necessary functions,
  653. // we split the registers in half and call AVX2 analogs from SSE
  654. __m128i ni0 = _mm256_castsi256_si128( i0 );
  655. __m128i ni1 = _mm256_extractf128_si256( i0, 1);
  656. __m128i ni2 = _mm256_castsi256_si128( i1 );
  657. __m128i ni3 = _mm256_extractf128_si256( i1, 1);
  658. __m128i ni4 = _mm256_castsi256_si128( i2 );
  659. __m128i ni5 = _mm256_extractf128_si256( i2, 1);
  660. __m128i ni6 = _mm256_castsi256_si128( i3 );
  661. __m128i ni7 = _mm256_extractf128_si256( i3, 1);
  662. // Convert int32 to int16
  663. ni0 = _mm_packs_epi32( ni0, ni1 );
  664. ni2 = _mm_packs_epi32( ni2, ni3 );
  665. ni4 = _mm_packs_epi32( ni4, ni5 );
  666. ni6 = _mm_packs_epi32( ni6, ni7 );
  667. // Convert int16 to int8
  668. ni0 = _mm_packs_epi16( ni0, ni2 );
  669. ni4 = _mm_packs_epi16( ni4, ni6 );
  670. _mm_storeu_si128((__m128i *)(y[i].qs + 0), ni0);
  671. _mm_storeu_si128((__m128i *)(y[i].qs + 16), ni4);
  672. #endif
  673. }
  674. #elif defined(__riscv_v_intrinsic)
  675. size_t vl = __riscv_vsetvl_e32m4(QK8_0);
  676. for (int i = 0; i < nb; i++) {
  677. // load elements
  678. vfloat32m4_t v_x = __riscv_vle32_v_f32m4(x+i*QK8_0, vl);
  679. vfloat32m4_t vfabs = __riscv_vfabs_v_f32m4(v_x, vl);
  680. vfloat32m1_t tmp = __riscv_vfmv_v_f_f32m1(0.0f, vl);
  681. vfloat32m1_t vmax = __riscv_vfredmax_vs_f32m4_f32m1(vfabs, tmp, vl);
  682. float amax = __riscv_vfmv_f_s_f32m1_f32(vmax);
  683. const float d = amax / ((1 << 7) - 1);
  684. const float id = d ? 1.0f/d : 0.0f;
  685. y[i].d = GGML_FP32_TO_FP16(d);
  686. vfloat32m4_t x0 = __riscv_vfmul_vf_f32m4(v_x, id, vl);
  687. // convert to integer
  688. vint16m2_t vi = __riscv_vfncvt_x_f_w_i16m2(x0, vl);
  689. vint8m1_t vs = __riscv_vncvt_x_x_w_i8m1(vi, vl);
  690. // store result
  691. __riscv_vse8_v_i8m1(y[i].qs , vs, vl);
  692. }
  693. #else
  694. GGML_UNUSED(nb);
  695. // scalar
  696. quantize_row_q8_0_reference(x, y, k);
  697. #endif
  698. }
  699. // reference implementation for deterministic creation of model files
  700. void quantize_row_q8_1_reference(const float * restrict x, block_q8_1 * restrict y, int k) {
  701. assert(QK8_1 == 32);
  702. assert(k % QK8_1 == 0);
  703. const int nb = k / QK8_1;
  704. for (int i = 0; i < nb; i++) {
  705. float amax = 0.0f; // absolute max
  706. for (int j = 0; j < QK8_1; j++) {
  707. const float v = x[i*QK8_1 + j];
  708. amax = MAX(amax, fabsf(v));
  709. }
  710. const float d = amax / ((1 << 7) - 1);
  711. const float id = d ? 1.0f/d : 0.0f;
  712. y[i].d = d;
  713. int sum = 0;
  714. for (int j = 0; j < QK8_1/2; ++j) {
  715. const float v0 = x[i*QK8_1 + j]*id;
  716. const float v1 = x[i*QK8_1 + QK8_1/2 + j]*id;
  717. y[i].qs[ j] = roundf(v0);
  718. y[i].qs[QK8_1/2 + j] = roundf(v1);
  719. sum += y[i].qs[ j];
  720. sum += y[i].qs[QK8_1/2 + j];
  721. }
  722. y[i].s = sum*d;
  723. }
  724. }
  725. void quantize_row_q8_1(const float * restrict x, void * restrict vy, int k) {
  726. assert(k % QK8_1 == 0);
  727. const int nb = k / QK8_1;
  728. block_q8_1 * restrict y = vy;
  729. #if defined(__ARM_NEON)
  730. for (int i = 0; i < nb; i++) {
  731. float32x4_t srcv [8];
  732. float32x4_t asrcv[8];
  733. float32x4_t amaxv[8];
  734. for (int j = 0; j < 8; j++) srcv[j] = vld1q_f32(x + i*32 + 4*j);
  735. for (int j = 0; j < 8; j++) asrcv[j] = vabsq_f32(srcv[j]);
  736. for (int j = 0; j < 4; j++) amaxv[2*j] = vmaxq_f32(asrcv[2*j], asrcv[2*j+1]);
  737. for (int j = 0; j < 2; j++) amaxv[4*j] = vmaxq_f32(amaxv[4*j], amaxv[4*j+2]);
  738. for (int j = 0; j < 1; j++) amaxv[8*j] = vmaxq_f32(amaxv[8*j], amaxv[8*j+4]);
  739. const float amax = vmaxvq_f32(amaxv[0]);
  740. const float d = amax / ((1 << 7) - 1);
  741. const float id = d ? 1.0f/d : 0.0f;
  742. y[i].d = d;
  743. int32x4_t accv = vdupq_n_s32(0);
  744. for (int j = 0; j < 8; j++) {
  745. const float32x4_t v = vmulq_n_f32(srcv[j], id);
  746. const int32x4_t vi = vcvtnq_s32_f32(v);
  747. y[i].qs[4*j + 0] = vgetq_lane_s32(vi, 0);
  748. y[i].qs[4*j + 1] = vgetq_lane_s32(vi, 1);
  749. y[i].qs[4*j + 2] = vgetq_lane_s32(vi, 2);
  750. y[i].qs[4*j + 3] = vgetq_lane_s32(vi, 3);
  751. accv = vaddq_s32(accv, vi);
  752. }
  753. y[i].s = d * vaddvq_s32(accv);
  754. }
  755. #elif defined(__wasm_simd128__)
  756. for (int i = 0; i < nb; i++) {
  757. v128_t srcv [8];
  758. v128_t asrcv[8];
  759. v128_t amaxv[8];
  760. for (int j = 0; j < 8; j++) srcv[j] = wasm_v128_load(x + i*32 + 4*j);
  761. for (int j = 0; j < 8; j++) asrcv[j] = wasm_f32x4_abs(srcv[j]);
  762. for (int j = 0; j < 4; j++) amaxv[2*j] = wasm_f32x4_max(asrcv[2*j], asrcv[2*j+1]);
  763. for (int j = 0; j < 2; j++) amaxv[4*j] = wasm_f32x4_max(amaxv[4*j], amaxv[4*j+2]);
  764. for (int j = 0; j < 1; j++) amaxv[8*j] = wasm_f32x4_max(amaxv[8*j], amaxv[8*j+4]);
  765. const float amax = MAX(MAX(wasm_f32x4_extract_lane(amaxv[0], 0),
  766. wasm_f32x4_extract_lane(amaxv[0], 1)),
  767. MAX(wasm_f32x4_extract_lane(amaxv[0], 2),
  768. wasm_f32x4_extract_lane(amaxv[0], 3)));
  769. const float d = amax / ((1 << 7) - 1);
  770. const float id = d ? 1.0f/d : 0.0f;
  771. y[i].d = d;
  772. v128_t accv = wasm_i32x4_splat(0);
  773. for (int j = 0; j < 8; j++) {
  774. const v128_t v = wasm_f32x4_mul(srcv[j], wasm_f32x4_splat(id));
  775. const v128_t vi = wasm_i32x4_trunc_sat_f32x4(v);
  776. y[i].qs[4*j + 0] = wasm_i32x4_extract_lane(vi, 0);
  777. y[i].qs[4*j + 1] = wasm_i32x4_extract_lane(vi, 1);
  778. y[i].qs[4*j + 2] = wasm_i32x4_extract_lane(vi, 2);
  779. y[i].qs[4*j + 3] = wasm_i32x4_extract_lane(vi, 3);
  780. accv = wasm_i32x4_add(accv, vi);
  781. }
  782. y[i].s = d * (wasm_i32x4_extract_lane(accv, 0) +
  783. wasm_i32x4_extract_lane(accv, 1) +
  784. wasm_i32x4_extract_lane(accv, 2) +
  785. wasm_i32x4_extract_lane(accv, 3));
  786. }
  787. #elif defined(__AVX2__) || defined(__AVX__)
  788. for (int i = 0; i < nb; i++) {
  789. // Load elements into 4 AVX vectors
  790. __m256 v0 = _mm256_loadu_ps( x );
  791. __m256 v1 = _mm256_loadu_ps( x + 8 );
  792. __m256 v2 = _mm256_loadu_ps( x + 16 );
  793. __m256 v3 = _mm256_loadu_ps( x + 24 );
  794. x += 32;
  795. // Compute max(abs(e)) for the block
  796. const __m256 signBit = _mm256_set1_ps( -0.0f );
  797. __m256 maxAbs = _mm256_andnot_ps( signBit, v0 );
  798. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v1 ) );
  799. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v2 ) );
  800. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v3 ) );
  801. __m128 max4 = _mm_max_ps( _mm256_extractf128_ps( maxAbs, 1 ), _mm256_castps256_ps128( maxAbs ) );
  802. max4 = _mm_max_ps( max4, _mm_movehl_ps( max4, max4 ) );
  803. max4 = _mm_max_ss( max4, _mm_movehdup_ps( max4 ) );
  804. const float maxScalar = _mm_cvtss_f32( max4 );
  805. // Quantize these floats
  806. const float d = maxScalar / 127.f;
  807. y[i].d = d;
  808. const float id = ( maxScalar != 0.0f ) ? 127.f / maxScalar : 0.0f;
  809. const __m256 mul = _mm256_set1_ps( id );
  810. // Apply the multiplier
  811. v0 = _mm256_mul_ps( v0, mul );
  812. v1 = _mm256_mul_ps( v1, mul );
  813. v2 = _mm256_mul_ps( v2, mul );
  814. v3 = _mm256_mul_ps( v3, mul );
  815. // Round to nearest integer
  816. v0 = _mm256_round_ps( v0, _MM_ROUND_NEAREST );
  817. v1 = _mm256_round_ps( v1, _MM_ROUND_NEAREST );
  818. v2 = _mm256_round_ps( v2, _MM_ROUND_NEAREST );
  819. v3 = _mm256_round_ps( v3, _MM_ROUND_NEAREST );
  820. // Convert floats to integers
  821. __m256i i0 = _mm256_cvtps_epi32( v0 );
  822. __m256i i1 = _mm256_cvtps_epi32( v1 );
  823. __m256i i2 = _mm256_cvtps_epi32( v2 );
  824. __m256i i3 = _mm256_cvtps_epi32( v3 );
  825. #if defined(__AVX2__)
  826. // Compute the sum of the quants and set y[i].s
  827. y[i].s = d * hsum_i32_8(_mm256_add_epi32(_mm256_add_epi32(i0, i1), _mm256_add_epi32(i2, i3)));
  828. // Convert int32 to int16
  829. i0 = _mm256_packs_epi32( i0, i1 ); // 0, 1, 2, 3, 8, 9, 10, 11, 4, 5, 6, 7, 12, 13, 14, 15
  830. i2 = _mm256_packs_epi32( i2, i3 ); // 16, 17, 18, 19, 24, 25, 26, 27, 20, 21, 22, 23, 28, 29, 30, 31
  831. // Convert int16 to int8
  832. 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
  833. // We got our precious signed bytes, but the order is now wrong
  834. // These AVX2 pack instructions process 16-byte pieces independently
  835. // The following instruction is fixing the order
  836. const __m256i perm = _mm256_setr_epi32( 0, 4, 1, 5, 2, 6, 3, 7 );
  837. i0 = _mm256_permutevar8x32_epi32( i0, perm );
  838. _mm256_storeu_si256((__m256i *)y[i].qs, i0);
  839. #else
  840. // Since we don't have in AVX some necessary functions,
  841. // we split the registers in half and call AVX2 analogs from SSE
  842. __m128i ni0 = _mm256_castsi256_si128( i0 );
  843. __m128i ni1 = _mm256_extractf128_si256( i0, 1);
  844. __m128i ni2 = _mm256_castsi256_si128( i1 );
  845. __m128i ni3 = _mm256_extractf128_si256( i1, 1);
  846. __m128i ni4 = _mm256_castsi256_si128( i2 );
  847. __m128i ni5 = _mm256_extractf128_si256( i2, 1);
  848. __m128i ni6 = _mm256_castsi256_si128( i3 );
  849. __m128i ni7 = _mm256_extractf128_si256( i3, 1);
  850. // Compute the sum of the quants and set y[i].s
  851. const __m128i s0 = _mm_add_epi32(_mm_add_epi32(ni0, ni1), _mm_add_epi32(ni2, ni3));
  852. const __m128i s1 = _mm_add_epi32(_mm_add_epi32(ni4, ni5), _mm_add_epi32(ni6, ni7));
  853. y[i].s = d * hsum_i32_4(_mm_add_epi32(s0, s1));
  854. // Convert int32 to int16
  855. ni0 = _mm_packs_epi32( ni0, ni1 );
  856. ni2 = _mm_packs_epi32( ni2, ni3 );
  857. ni4 = _mm_packs_epi32( ni4, ni5 );
  858. ni6 = _mm_packs_epi32( ni6, ni7 );
  859. // Convert int16 to int8
  860. ni0 = _mm_packs_epi16( ni0, ni2 );
  861. ni4 = _mm_packs_epi16( ni4, ni6 );
  862. _mm_storeu_si128((__m128i *)(y[i].qs + 0), ni0);
  863. _mm_storeu_si128((__m128i *)(y[i].qs + 16), ni4);
  864. #endif
  865. }
  866. #elif defined(__riscv_v_intrinsic)
  867. size_t vl = __riscv_vsetvl_e32m4(QK8_1);
  868. for (int i = 0; i < nb; i++) {
  869. // load elements
  870. vfloat32m4_t v_x = __riscv_vle32_v_f32m4(x+i*QK8_1, vl);
  871. vfloat32m4_t vfabs = __riscv_vfabs_v_f32m4(v_x, vl);
  872. vfloat32m1_t tmp = __riscv_vfmv_v_f_f32m1(0.0, vl);
  873. vfloat32m1_t vmax = __riscv_vfredmax_vs_f32m4_f32m1(vfabs, tmp, vl);
  874. float amax = __riscv_vfmv_f_s_f32m1_f32(vmax);
  875. const float d = amax / ((1 << 7) - 1);
  876. const float id = d ? 1.0f/d : 0.0f;
  877. y[i].d = d;
  878. vfloat32m4_t x0 = __riscv_vfmul_vf_f32m4(v_x, id, vl);
  879. // convert to integer
  880. vint16m2_t vi = __riscv_vfncvt_x_f_w_i16m2(x0, vl);
  881. vint8m1_t vs = __riscv_vncvt_x_x_w_i8m1(vi, vl);
  882. // store result
  883. __riscv_vse8_v_i8m1(y[i].qs , vs, vl);
  884. // compute sum for y[i].s
  885. vint16m1_t tmp2 = __riscv_vmv_v_x_i16m1(0, vl);
  886. vint16m1_t vwrs = __riscv_vwredsum_vs_i8m1_i16m1(vs, tmp2, vl);
  887. // set y[i].s
  888. int sum = __riscv_vmv_x_s_i16m1_i16(vwrs);
  889. y[i].s = sum*d;
  890. }
  891. #else
  892. GGML_UNUSED(nb);
  893. // scalar
  894. quantize_row_q8_1_reference(x, y, k);
  895. #endif
  896. }
  897. void dequantize_row_q4_0(const block_q4_0 * restrict x, float * restrict y, int k) {
  898. static const int qk = QK4_0;
  899. assert(k % qk == 0);
  900. const int nb = k / qk;
  901. for (int i = 0; i < nb; i++) {
  902. const float d = GGML_FP16_TO_FP32(x[i].d);
  903. for (int j = 0; j < qk/2; ++j) {
  904. const int x0 = (x[i].qs[j] & 0x0F) - 8;
  905. const int x1 = (x[i].qs[j] >> 4) - 8;
  906. y[i*qk + j + 0 ] = x0*d;
  907. y[i*qk + j + qk/2] = x1*d;
  908. }
  909. }
  910. }
  911. void dequantize_row_q4_1(const block_q4_1 * restrict x, float * restrict y, int k) {
  912. static const int qk = QK4_1;
  913. assert(k % qk == 0);
  914. const int nb = k / qk;
  915. for (int i = 0; i < nb; i++) {
  916. const float d = GGML_FP16_TO_FP32(x[i].d);
  917. const float m = GGML_FP16_TO_FP32(x[i].m);
  918. for (int j = 0; j < qk/2; ++j) {
  919. const int x0 = (x[i].qs[j] & 0x0F);
  920. const int x1 = (x[i].qs[j] >> 4);
  921. y[i*qk + j + 0 ] = x0*d + m;
  922. y[i*qk + j + qk/2] = x1*d + m;
  923. }
  924. }
  925. }
  926. void dequantize_row_q5_0(const block_q5_0 * restrict x, float * restrict y, int k) {
  927. static const int qk = QK5_0;
  928. assert(k % qk == 0);
  929. const int nb = k / qk;
  930. for (int i = 0; i < nb; i++) {
  931. const float d = GGML_FP16_TO_FP32(x[i].d);
  932. uint32_t qh;
  933. memcpy(&qh, x[i].qh, sizeof(qh));
  934. for (int j = 0; j < qk/2; ++j) {
  935. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  936. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  937. const int32_t x0 = ((x[i].qs[j] & 0x0F) | xh_0) - 16;
  938. const int32_t x1 = ((x[i].qs[j] >> 4) | xh_1) - 16;
  939. y[i*qk + j + 0 ] = x0*d;
  940. y[i*qk + j + qk/2] = x1*d;
  941. }
  942. }
  943. }
  944. void dequantize_row_q5_1(const block_q5_1 * restrict x, float * restrict y, int k) {
  945. static const int qk = QK5_1;
  946. assert(k % qk == 0);
  947. const int nb = k / qk;
  948. for (int i = 0; i < nb; i++) {
  949. const float d = GGML_FP16_TO_FP32(x[i].d);
  950. const float m = GGML_FP16_TO_FP32(x[i].m);
  951. uint32_t qh;
  952. memcpy(&qh, x[i].qh, sizeof(qh));
  953. for (int j = 0; j < qk/2; ++j) {
  954. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  955. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  956. const int x0 = (x[i].qs[j] & 0x0F) | xh_0;
  957. const int x1 = (x[i].qs[j] >> 4) | xh_1;
  958. y[i*qk + j + 0 ] = x0*d + m;
  959. y[i*qk + j + qk/2] = x1*d + m;
  960. }
  961. }
  962. }
  963. void dequantize_row_q8_0(const block_q8_0 * restrict x, float * restrict y, int k) {
  964. static const int qk = QK8_0;
  965. assert(k % qk == 0);
  966. const int nb = k / qk;
  967. for (int i = 0; i < nb; i++) {
  968. const float d = GGML_FP16_TO_FP32(x[i].d);
  969. for (int j = 0; j < qk; ++j) {
  970. y[i*qk + j] = x[i].qs[j]*d;
  971. }
  972. }
  973. }
  974. //
  975. // 2-6 bit quantization in super-blocks
  976. //
  977. //
  978. // ===================== Helper functions
  979. //
  980. static inline int nearest_int(float fval) {
  981. assert(fval <= 4194303.f);
  982. float val = fval + 12582912.f;
  983. int i; memcpy(&i, &val, sizeof(int));
  984. return (i & 0x007fffff) - 0x00400000;
  985. }
  986. static float make_qx_quants(int n, int nmax, const float * restrict x, int8_t * restrict L, int rmse_type,
  987. const float * restrict qw) {
  988. float max = 0;
  989. float amax = 0;
  990. for (int i = 0; i < n; ++i) {
  991. float ax = fabsf(x[i]);
  992. if (ax > amax) { amax = ax; max = x[i]; }
  993. }
  994. if (amax < 1e-30f) { // all zero
  995. for (int i = 0; i < n; ++i) {
  996. L[i] = 0;
  997. }
  998. return 0.f;
  999. }
  1000. float iscale = -nmax / max;
  1001. if (rmse_type == 0) {
  1002. for (int i = 0; i < n; ++i) {
  1003. int l = nearest_int(iscale * x[i]);
  1004. L[i] = nmax + MAX(-nmax, MIN(nmax-1, l));
  1005. }
  1006. return 1/iscale;
  1007. }
  1008. bool return_early = false;
  1009. if (rmse_type < 0) {
  1010. rmse_type = -rmse_type;
  1011. return_early = true;
  1012. }
  1013. float sumlx = 0;
  1014. float suml2 = 0;
  1015. #ifdef HAVE_BUGGY_APPLE_LINKER
  1016. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  1017. for (volatile int i = 0; i < n; ++i) {
  1018. #else
  1019. for (int i = 0; i < n; ++i) {
  1020. #endif
  1021. int l = nearest_int(iscale * x[i]);
  1022. l = MAX(-nmax, MIN(nmax-1, l));
  1023. L[i] = l + nmax;
  1024. 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]));
  1025. sumlx += w*x[i]*l;
  1026. suml2 += w*l*l;
  1027. }
  1028. float scale = sumlx/suml2;
  1029. if (return_early) return suml2 > 0 ? 0.5f*(scale + 1/iscale) : 1/iscale;
  1030. float best = scale * sumlx;
  1031. for (int is = -9; is <= 9; ++is) {
  1032. if (is == 0) {
  1033. continue;
  1034. }
  1035. iscale = -(nmax + 0.1f*is) / max;
  1036. sumlx = suml2 = 0;
  1037. for (int i = 0; i < n; ++i) {
  1038. int l = nearest_int(iscale * x[i]);
  1039. l = MAX(-nmax, MIN(nmax-1, l));
  1040. 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]));
  1041. sumlx += w*x[i]*l;
  1042. suml2 += w*l*l;
  1043. }
  1044. if (suml2 > 0 && sumlx*sumlx > best*suml2) {
  1045. for (int i = 0; i < n; ++i) {
  1046. int l = nearest_int(iscale * x[i]);
  1047. L[i] = nmax + MAX(-nmax, MIN(nmax-1, l));
  1048. }
  1049. scale = sumlx/suml2; best = scale*sumlx;
  1050. }
  1051. }
  1052. return scale;
  1053. }
  1054. static float make_q3_quants(int n, int nmax, const float * restrict x, int8_t * restrict L, bool do_rmse) {
  1055. float max = 0;
  1056. float amax = 0;
  1057. for (int i = 0; i < n; ++i) {
  1058. float ax = fabsf(x[i]);
  1059. if (ax > amax) { amax = ax; max = x[i]; }
  1060. }
  1061. if (!amax) { // all zero
  1062. for (int i = 0; i < n; ++i) { L[i] = 0; }
  1063. return 0.f;
  1064. }
  1065. float iscale = -nmax / max;
  1066. if (do_rmse) {
  1067. float sumlx = 0;
  1068. float suml2 = 0;
  1069. for (int i = 0; i < n; ++i) {
  1070. int l = nearest_int(iscale * x[i]);
  1071. l = MAX(-nmax, MIN(nmax-1, l));
  1072. L[i] = l;
  1073. float w = x[i]*x[i];
  1074. sumlx += w*x[i]*l;
  1075. suml2 += w*l*l;
  1076. }
  1077. for (int itry = 0; itry < 5; ++itry) {
  1078. int n_changed = 0;
  1079. for (int i = 0; i < n; ++i) {
  1080. float w = x[i]*x[i];
  1081. float slx = sumlx - w*x[i]*L[i];
  1082. if (slx > 0) {
  1083. float sl2 = suml2 - w*L[i]*L[i];
  1084. int new_l = nearest_int(x[i] * sl2 / slx);
  1085. new_l = MAX(-nmax, MIN(nmax-1, new_l));
  1086. if (new_l != L[i]) {
  1087. slx += w*x[i]*new_l;
  1088. sl2 += w*new_l*new_l;
  1089. if (sl2 > 0 && slx*slx*suml2 > sumlx*sumlx*sl2) {
  1090. L[i] = new_l; sumlx = slx; suml2 = sl2;
  1091. ++n_changed;
  1092. }
  1093. }
  1094. }
  1095. }
  1096. if (!n_changed) {
  1097. break;
  1098. }
  1099. }
  1100. for (int i = 0; i < n; ++i) {
  1101. L[i] += nmax;
  1102. }
  1103. return sumlx / suml2;
  1104. }
  1105. for (int i = 0; i < n; ++i) {
  1106. int l = nearest_int(iscale * x[i]);
  1107. l = MAX(-nmax, MIN(nmax-1, l));
  1108. L[i] = l + nmax;
  1109. }
  1110. return 1/iscale;
  1111. }
  1112. static float make_qkx1_quants(int n, int nmax, const float * restrict x, uint8_t * restrict L, float * restrict the_min,
  1113. int ntry, float alpha) {
  1114. float min = x[0];
  1115. float max = x[0];
  1116. for (int i = 1; i < n; ++i) {
  1117. if (x[i] < min) min = x[i];
  1118. if (x[i] > max) max = x[i];
  1119. }
  1120. if (max == min) {
  1121. for (int i = 0; i < n; ++i) L[i] = 0;
  1122. *the_min = 0;
  1123. return 0.f;
  1124. }
  1125. if (min > 0) min = 0;
  1126. float iscale = nmax/(max - min);
  1127. float scale = 1/iscale;
  1128. for (int itry = 0; itry < ntry; ++itry) {
  1129. float sumlx = 0; int suml2 = 0;
  1130. bool did_change = false;
  1131. for (int i = 0; i < n; ++i) {
  1132. int l = nearest_int(iscale*(x[i] - min));
  1133. l = MAX(0, MIN(nmax, l));
  1134. if (l != L[i]) {
  1135. L[i] = l;
  1136. did_change = true;
  1137. }
  1138. sumlx += (x[i] - min)*l;
  1139. suml2 += l*l;
  1140. }
  1141. scale = sumlx/suml2;
  1142. float sum = 0;
  1143. for (int i = 0; i < n; ++i) {
  1144. sum += x[i] - scale*L[i];
  1145. }
  1146. min = alpha*min + (1 - alpha)*sum/n;
  1147. if (min > 0) min = 0;
  1148. iscale = 1/scale;
  1149. if (!did_change) break;
  1150. }
  1151. *the_min = -min;
  1152. return scale;
  1153. }
  1154. static float make_qkx2_quants(int n, int nmax, const float * restrict x, const float * restrict weights,
  1155. uint8_t * restrict L, float * restrict the_min, uint8_t * restrict Laux,
  1156. float rmin, float rdelta, int nstep, bool use_mad) {
  1157. float min = x[0];
  1158. float max = x[0];
  1159. float sum_w = weights[0];
  1160. float sum_x = sum_w * x[0];
  1161. #ifdef HAVE_BUGGY_APPLE_LINKER
  1162. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  1163. for (volatile int i = 1; i < n; ++i) {
  1164. #else
  1165. for (int i = 1; i < n; ++i) {
  1166. #endif
  1167. if (x[i] < min) min = x[i];
  1168. if (x[i] > max) max = x[i];
  1169. float w = weights[i];
  1170. sum_w += w;
  1171. sum_x += w * x[i];
  1172. }
  1173. if (min > 0) min = 0;
  1174. if (max == min) {
  1175. for (int i = 0; i < n; ++i) L[i] = 0;
  1176. *the_min = -min;
  1177. return 0.f;
  1178. }
  1179. float iscale = nmax/(max - min);
  1180. float scale = 1/iscale;
  1181. float best_mad = 0;
  1182. for (int i = 0; i < n; ++i) {
  1183. int l = nearest_int(iscale*(x[i] - min));
  1184. L[i] = MAX(0, MIN(nmax, l));
  1185. float diff = scale * L[i] + min - x[i];
  1186. diff = use_mad ? fabsf(diff) : diff * diff;
  1187. float w = weights[i];
  1188. best_mad += w * diff;
  1189. }
  1190. if (nstep < 1) {
  1191. *the_min = -min;
  1192. return scale;
  1193. }
  1194. for (int is = 0; is <= nstep; ++is) {
  1195. iscale = (rmin + rdelta*is + nmax)/(max - min);
  1196. float sum_l = 0, sum_l2 = 0, sum_xl = 0;
  1197. for (int i = 0; i < n; ++i) {
  1198. int l = nearest_int(iscale*(x[i] - min));
  1199. l = MAX(0, MIN(nmax, l));
  1200. Laux[i] = l;
  1201. float w = weights[i];
  1202. sum_l += w*l;
  1203. sum_l2 += w*l*l;
  1204. sum_xl += w*l*x[i];
  1205. }
  1206. float D = sum_w * sum_l2 - sum_l * sum_l;
  1207. if (D > 0) {
  1208. float this_scale = (sum_w * sum_xl - sum_x * sum_l)/D;
  1209. float this_min = (sum_l2 * sum_x - sum_l * sum_xl)/D;
  1210. if (this_min > 0) {
  1211. this_min = 0;
  1212. this_scale = sum_xl / sum_l2;
  1213. }
  1214. float mad = 0;
  1215. for (int i = 0; i < n; ++i) {
  1216. float diff = this_scale * Laux[i] + this_min - x[i];
  1217. diff = use_mad ? fabsf(diff) : diff * diff;
  1218. float w = weights[i];
  1219. mad += w * diff;
  1220. }
  1221. if (mad < best_mad) {
  1222. for (int i = 0; i < n; ++i) {
  1223. L[i] = Laux[i];
  1224. }
  1225. best_mad = mad;
  1226. scale = this_scale;
  1227. min = this_min;
  1228. }
  1229. }
  1230. }
  1231. *the_min = -min;
  1232. return scale;
  1233. }
  1234. #if QK_K == 256
  1235. static inline void get_scale_min_k4(int j, const uint8_t * restrict q, uint8_t * restrict d, uint8_t * restrict m) {
  1236. if (j < 4) {
  1237. *d = q[j] & 63; *m = q[j + 4] & 63;
  1238. } else {
  1239. *d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  1240. *m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  1241. }
  1242. }
  1243. #endif
  1244. //========================- 2-bit (de)-quantization
  1245. void quantize_row_q2_K_reference(const float * restrict x, block_q2_K * restrict y, int k) {
  1246. assert(k % QK_K == 0);
  1247. const int nb = k / QK_K;
  1248. uint8_t L[QK_K];
  1249. uint8_t Laux[16];
  1250. float weights[16];
  1251. float mins[QK_K/16];
  1252. float scales[QK_K/16];
  1253. const float q4scale = 15.f;
  1254. for (int i = 0; i < nb; i++) {
  1255. float max_scale = 0; // as we are deducting the min, scales are always positive
  1256. float max_min = 0;
  1257. for (int j = 0; j < QK_K/16; ++j) {
  1258. for (int l = 0; l < 16; ++l) weights[l] = fabsf(x[16*j + l]);
  1259. scales[j] = make_qkx2_quants(16, 3, x + 16*j, weights, L + 16*j, &mins[j], Laux, -0.5f, 0.1f, 15, true);
  1260. float scale = scales[j];
  1261. if (scale > max_scale) {
  1262. max_scale = scale;
  1263. }
  1264. float min = mins[j];
  1265. if (min > max_min) {
  1266. max_min = min;
  1267. }
  1268. }
  1269. if (max_scale > 0) {
  1270. float iscale = q4scale/max_scale;
  1271. for (int j = 0; j < QK_K/16; ++j) {
  1272. int l = nearest_int(iscale*scales[j]);
  1273. y[i].scales[j] = l;
  1274. }
  1275. y[i].d = GGML_FP32_TO_FP16(max_scale/q4scale);
  1276. } else {
  1277. for (int j = 0; j < QK_K/16; ++j) y[i].scales[j] = 0;
  1278. y[i].d = GGML_FP32_TO_FP16(0.f);
  1279. }
  1280. if (max_min > 0) {
  1281. float iscale = q4scale/max_min;
  1282. for (int j = 0; j < QK_K/16; ++j) {
  1283. int l = nearest_int(iscale*mins[j]);
  1284. y[i].scales[j] |= (l << 4);
  1285. }
  1286. y[i].dmin = GGML_FP32_TO_FP16(max_min/q4scale);
  1287. } else {
  1288. y[i].dmin = GGML_FP32_TO_FP16(0.f);
  1289. }
  1290. for (int j = 0; j < QK_K/16; ++j) {
  1291. const float d = GGML_FP16_TO_FP32(y[i].d) * (y[i].scales[j] & 0xF);
  1292. if (!d) continue;
  1293. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * (y[i].scales[j] >> 4);
  1294. for (int ii = 0; ii < 16; ++ii) {
  1295. int l = nearest_int((x[16*j + ii] + dm)/d);
  1296. l = MAX(0, MIN(3, l));
  1297. L[16*j + ii] = l;
  1298. }
  1299. }
  1300. #if QK_K == 256
  1301. for (int j = 0; j < QK_K; j += 128) {
  1302. for (int l = 0; l < 32; ++l) {
  1303. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1304. }
  1305. }
  1306. #else
  1307. for (int l = 0; l < 16; ++l) {
  1308. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1309. }
  1310. #endif
  1311. x += QK_K;
  1312. }
  1313. }
  1314. void dequantize_row_q2_K(const block_q2_K * restrict x, float * restrict y, int k) {
  1315. assert(k % QK_K == 0);
  1316. const int nb = k / QK_K;
  1317. for (int i = 0; i < nb; i++) {
  1318. const float d = GGML_FP16_TO_FP32(x[i].d);
  1319. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  1320. const uint8_t * q = x[i].qs;
  1321. #if QK_K == 256
  1322. int is = 0;
  1323. float dl, ml;
  1324. for (int n = 0; n < QK_K; n += 128) {
  1325. int shift = 0;
  1326. for (int j = 0; j < 4; ++j) {
  1327. uint8_t sc = x[i].scales[is++];
  1328. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  1329. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l] >> shift) & 3)) - ml;
  1330. sc = x[i].scales[is++];
  1331. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  1332. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3)) - ml;
  1333. shift += 2;
  1334. }
  1335. q += 32;
  1336. }
  1337. #else
  1338. float dl1 = d * (x[i].scales[0] & 0xF), ml1 = min * (x[i].scales[0] >> 4);
  1339. float dl2 = d * (x[i].scales[1] & 0xF), ml2 = min * (x[i].scales[1] >> 4);
  1340. float dl3 = d * (x[i].scales[2] & 0xF), ml3 = min * (x[i].scales[2] >> 4);
  1341. float dl4 = d * (x[i].scales[3] & 0xF), ml4 = min * (x[i].scales[3] >> 4);
  1342. for (int l = 0; l < 16; ++l) {
  1343. y[l+ 0] = dl1 * ((int8_t)((q[l] >> 0) & 3)) - ml1;
  1344. y[l+16] = dl2 * ((int8_t)((q[l] >> 2) & 3)) - ml2;
  1345. y[l+32] = dl3 * ((int8_t)((q[l] >> 4) & 3)) - ml3;
  1346. y[l+48] = dl4 * ((int8_t)((q[l] >> 6) & 3)) - ml4;
  1347. }
  1348. y += QK_K;
  1349. #endif
  1350. }
  1351. }
  1352. void quantize_row_q2_K(const float * restrict x, void * restrict vy, int k) {
  1353. quantize_row_q2_K_reference(x, vy, k);
  1354. }
  1355. size_t ggml_quantize_q2_K(const float * restrict src, void * restrict dst, int n, int k, int64_t * restrict hist) {
  1356. (void)hist; // TODO: collect histograms
  1357. for (int j = 0; j < n; j += k) {
  1358. block_q2_K * restrict y = (block_q2_K *)dst + j/QK_K;
  1359. quantize_row_q2_K_reference(src + j, y, k);
  1360. }
  1361. return (n/QK_K*sizeof(block_q2_K));
  1362. }
  1363. static float make_qkx3_quants(int n, int nmax, const float * restrict x, const float * restrict weights,
  1364. uint8_t * restrict L, float * restrict the_min, uint8_t * restrict Laux,
  1365. float rmin, float rdelta, int nstep, bool use_mad) {
  1366. float min = x[0];
  1367. float max = x[0];
  1368. float sum_w = weights ? weights[0] : x[0]*x[0];
  1369. float sum_x = sum_w * x[0];
  1370. #ifdef HAVE_BUGGY_APPLE_LINKER
  1371. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  1372. for (volatile int i = 1; i < n; ++i) {
  1373. #else
  1374. for (int i = 1; i < n; ++i) {
  1375. #endif
  1376. if (x[i] < min) min = x[i];
  1377. if (x[i] > max) max = x[i];
  1378. float w = weights ? weights[i] : x[i]*x[i];
  1379. sum_w += w;
  1380. sum_x += w * x[i];
  1381. }
  1382. if (min > 0) {
  1383. min = 0;
  1384. }
  1385. if (max <= min) {
  1386. memset(L, 0, n);
  1387. *the_min = -min;
  1388. return 0.f;
  1389. }
  1390. float iscale = nmax/(max - min);
  1391. float scale = 1/iscale;
  1392. float best_mad = 0;
  1393. for (int i = 0; i < n; ++i) {
  1394. int l = nearest_int(iscale*(x[i] - min));
  1395. L[i] = MAX(0, MIN(nmax, l));
  1396. float diff = scale * L[i] + min - x[i];
  1397. diff = use_mad ? fabsf(diff) : diff*diff;
  1398. float w = weights ? weights[i] : x[i]*x[i];
  1399. best_mad += w * diff;
  1400. }
  1401. if (nstep < 1) {
  1402. *the_min = -min;
  1403. return scale;
  1404. }
  1405. for (int is = 0; is <= nstep; ++is) {
  1406. iscale = (rmin + rdelta*is + nmax)/(max - min);
  1407. float sum_l = 0, sum_l2 = 0, sum_xl = 0;
  1408. for (int i = 0; i < n; ++i) {
  1409. int l = nearest_int(iscale*(x[i] - min));
  1410. l = MAX(0, MIN(nmax, l));
  1411. Laux[i] = l;
  1412. float w = weights ? weights[i] : x[i]*x[i];
  1413. sum_l += w*l;
  1414. sum_l2 += w*l*l;
  1415. sum_xl += w*l*x[i];
  1416. }
  1417. float D = sum_w * sum_l2 - sum_l * sum_l;
  1418. if (D > 0) {
  1419. float this_scale = (sum_w * sum_xl - sum_x * sum_l)/D;
  1420. float this_min = (sum_l2 * sum_x - sum_l * sum_xl)/D;
  1421. if (this_min > 0) {
  1422. this_min = 0;
  1423. this_scale = sum_xl / sum_l2;
  1424. }
  1425. float mad = 0;
  1426. for (int i = 0; i < n; ++i) {
  1427. float diff = this_scale * Laux[i] + this_min - x[i];
  1428. diff = use_mad ? fabsf(diff) : diff*diff;
  1429. float w = weights ? weights[i] : x[i]*x[i];
  1430. mad += w * diff;
  1431. }
  1432. if (mad < best_mad) {
  1433. for (int i = 0; i < n; ++i) {
  1434. L[i] = Laux[i];
  1435. }
  1436. best_mad = mad;
  1437. scale = this_scale;
  1438. min = this_min;
  1439. }
  1440. }
  1441. }
  1442. *the_min = -min;
  1443. return scale;
  1444. }
  1445. static float make_qp_quants(int n, int nmax, const float * restrict x, uint8_t * restrict L, const float * quant_weights) {
  1446. float max = 0;
  1447. for (int i = 0; i < n; ++i) {
  1448. max = MAX(max, x[i]);
  1449. }
  1450. if (!max) { // all zero
  1451. for (int i = 0; i < n; ++i) { L[i] = 0; }
  1452. return 0.f;
  1453. }
  1454. float iscale = nmax / max;
  1455. for (int i = 0; i < n; ++i) {
  1456. L[i] = nearest_int(iscale * x[i]);
  1457. }
  1458. float scale = 1/iscale;
  1459. float best_mse = 0;
  1460. for (int i = 0; i < n; ++i) {
  1461. float diff = x[i] - scale*L[i];
  1462. float w = quant_weights[i];
  1463. best_mse += w*diff*diff;
  1464. }
  1465. for (int is = -4; is <= 4; ++is) {
  1466. if (is == 0) continue;
  1467. float iscale_is = (0.1f*is + nmax)/max;
  1468. float scale_is = 1/iscale_is;
  1469. float mse = 0;
  1470. for (int i = 0; i < n; ++i) {
  1471. int l = nearest_int(iscale_is*x[i]);
  1472. l = MIN(nmax, l);
  1473. float diff = x[i] - scale_is*l;
  1474. float w = quant_weights[i];
  1475. mse += w*diff*diff;
  1476. }
  1477. if (mse < best_mse) {
  1478. best_mse = mse;
  1479. iscale = iscale_is;
  1480. }
  1481. }
  1482. float sumlx = 0;
  1483. float suml2 = 0;
  1484. for (int i = 0; i < n; ++i) {
  1485. int l = nearest_int(iscale * x[i]);
  1486. l = MIN(nmax, l);
  1487. L[i] = l;
  1488. float w = quant_weights[i];
  1489. sumlx += w*x[i]*l;
  1490. suml2 += w*l*l;
  1491. }
  1492. for (int itry = 0; itry < 5; ++itry) {
  1493. int n_changed = 0;
  1494. for (int i = 0; i < n; ++i) {
  1495. float w = quant_weights[i];
  1496. float slx = sumlx - w*x[i]*L[i];
  1497. float sl2 = suml2 - w*L[i]*L[i];
  1498. if (slx > 0 && sl2 > 0) {
  1499. int new_l = nearest_int(x[i] * sl2 / slx);
  1500. new_l = MIN(nmax, new_l);
  1501. if (new_l != L[i]) {
  1502. slx += w*x[i]*new_l;
  1503. sl2 += w*new_l*new_l;
  1504. if (slx*slx*suml2 > sumlx*sumlx*sl2) {
  1505. L[i] = new_l; sumlx = slx; suml2 = sl2;
  1506. ++n_changed;
  1507. }
  1508. }
  1509. }
  1510. }
  1511. if (!n_changed) {
  1512. break;
  1513. }
  1514. }
  1515. return sumlx / suml2;
  1516. }
  1517. static void quantize_row_q2_K_impl(const float * restrict x, block_q2_K * restrict y, int k, const float * restrict quant_weights) {
  1518. GGML_ASSERT(quant_weights);
  1519. assert(k % QK_K == 0);
  1520. const int nb = k / QK_K;
  1521. const bool requantize = true;
  1522. uint8_t L[QK_K];
  1523. uint8_t Laux[16];
  1524. float mins[QK_K/16];
  1525. float scales[QK_K/16];
  1526. float sw[QK_K/16];
  1527. float weight[QK_K/16];
  1528. uint8_t Ls[QK_K/16], Lm[QK_K/16];
  1529. for (int i = 0; i < nb; i++) {
  1530. memset(sw, 0, QK_K/16*sizeof(float));
  1531. float sumx2 = 0;
  1532. for (int j = 0; j < QK_K; ++j) sumx2 += x[j]*x[j];
  1533. float sigma2 = sumx2/QK_K;
  1534. for (int j = 0; j < QK_K/16; ++j) {
  1535. const float * restrict qw = quant_weights + QK_K * i + 16*j;
  1536. for (int l = 0; l < 16; ++l) weight[l] = qw[l] * sqrtf(sigma2 + x[16*j + l]*x[16*j + l]);
  1537. for (int l = 0; l < 16; ++l) sw[j] += weight[l];
  1538. scales[j] = make_qkx3_quants(16, 3, x + 16*j, weight, L + 16*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  1539. }
  1540. float dm = make_qp_quants(QK_K/16, 15, scales, Ls, sw);
  1541. float mm = make_qp_quants(QK_K/16, 15, mins, Lm, sw);
  1542. y[i].d = GGML_FP32_TO_FP16(dm);
  1543. y[i].dmin = GGML_FP32_TO_FP16(mm);
  1544. dm = GGML_FP16_TO_FP32(y[i].d);
  1545. mm = GGML_FP16_TO_FP32(y[i].dmin);
  1546. for (int j = 0; j < QK_K/16; ++j) {
  1547. y[i].scales[j] = Ls[j] | (Lm[j] << 4);
  1548. }
  1549. if (requantize) {
  1550. for (int j = 0; j < QK_K/16; ++j) {
  1551. const float d = dm * (y[i].scales[j] & 0xF);
  1552. if (!d) continue;
  1553. const float m = mm * (y[i].scales[j] >> 4);
  1554. for (int ii = 0; ii < 16; ++ii) {
  1555. int l = nearest_int((x[16*j + ii] + m)/d);
  1556. l = MAX(0, MIN(3, l));
  1557. L[16*j + ii] = l;
  1558. }
  1559. }
  1560. }
  1561. #if QK_K == 256
  1562. for (int j = 0; j < QK_K; j += 128) {
  1563. for (int l = 0; l < 32; ++l) {
  1564. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1565. }
  1566. }
  1567. #else
  1568. for (int l = 0; l < 16; ++l) {
  1569. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1570. }
  1571. #endif
  1572. x += QK_K;
  1573. }
  1574. }
  1575. size_t quantize_q2_K(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  1576. (void)hist;
  1577. size_t row_size = ggml_row_size(GGML_TYPE_Q2_K, n_per_row);
  1578. if (!quant_weights) {
  1579. quantize_row_q2_K_reference(src, dst, nrow*n_per_row);
  1580. }
  1581. else {
  1582. char * qrow = (char *)dst;
  1583. for (int row = 0; row < nrow; ++row) {
  1584. quantize_row_q2_K_impl(src, (block_q2_K*)qrow, n_per_row, quant_weights);
  1585. src += n_per_row;
  1586. qrow += row_size;
  1587. }
  1588. }
  1589. return nrow * row_size;
  1590. }
  1591. //========================= 3-bit (de)-quantization
  1592. void quantize_row_q3_K_reference(const float * restrict x, block_q3_K * restrict y, int k) {
  1593. assert(k % QK_K == 0);
  1594. const int nb = k / QK_K;
  1595. int8_t L[QK_K];
  1596. float scales[QK_K / 16];
  1597. for (int i = 0; i < nb; i++) {
  1598. float max_scale = 0;
  1599. float amax = 0;
  1600. for (int j = 0; j < QK_K/16; ++j) {
  1601. scales[j] = make_q3_quants(16, 4, x + 16*j, L + 16*j, true);
  1602. float scale = fabsf(scales[j]);
  1603. if (scale > amax) {
  1604. amax = scale; max_scale = scales[j];
  1605. }
  1606. }
  1607. #if QK_K == 256
  1608. memset(y[i].scales, 0, 12);
  1609. if (max_scale) {
  1610. float iscale = -32.f/max_scale;
  1611. for (int j = 0; j < QK_K/16; ++j) {
  1612. int8_t l = nearest_int(iscale*scales[j]);
  1613. l = MAX(-32, MIN(31, l)) + 32;
  1614. if (j < 8) {
  1615. y[i].scales[j] = l & 0xF;
  1616. } else {
  1617. y[i].scales[j-8] |= ((l & 0xF) << 4);
  1618. }
  1619. l >>= 4;
  1620. y[i].scales[j%4 + 8] |= (l << (2*(j/4)));
  1621. }
  1622. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  1623. } else {
  1624. y[i].d = GGML_FP32_TO_FP16(0.f);
  1625. }
  1626. int8_t sc;
  1627. for (int j = 0; j < QK_K/16; ++j) {
  1628. sc = j < 8 ? y[i].scales[j] & 0xF : y[i].scales[j-8] >> 4;
  1629. sc = (sc | (((y[i].scales[8 + j%4] >> (2*(j/4))) & 3) << 4)) - 32;
  1630. float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1631. if (!d) {
  1632. continue;
  1633. }
  1634. for (int ii = 0; ii < 16; ++ii) {
  1635. int l = nearest_int(x[16*j + ii]/d);
  1636. l = MAX(-4, MIN(3, l));
  1637. L[16*j + ii] = l + 4;
  1638. }
  1639. }
  1640. #else
  1641. if (max_scale) {
  1642. float iscale = -8.f/max_scale;
  1643. for (int j = 0; j < QK_K/16; j+=2) {
  1644. int l1 = nearest_int(iscale*scales[j]);
  1645. l1 = 8 + MAX(-8, MIN(7, l1));
  1646. int l2 = nearest_int(iscale*scales[j+1]);
  1647. l2 = 8 + MAX(-8, MIN(7, l2));
  1648. y[i].scales[j/2] = l1 | (l2 << 4);
  1649. }
  1650. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  1651. } else {
  1652. for (int j = 0; j < QK_K/16; j+=2) {
  1653. y[i].scales[j/2] = 0;
  1654. }
  1655. y[i].d = GGML_FP32_TO_FP16(0.f);
  1656. }
  1657. for (int j = 0; j < QK_K/16; ++j) {
  1658. int s = j%2 == 0 ? y[i].scales[j/2] & 0xF : y[i].scales[j/2] >> 4;
  1659. float d = GGML_FP16_TO_FP32(y[i].d) * (s - 8);
  1660. if (!d) {
  1661. continue;
  1662. }
  1663. for (int ii = 0; ii < 16; ++ii) {
  1664. int l = nearest_int(x[16*j + ii]/d);
  1665. l = MAX(-4, MIN(3, l));
  1666. L[16*j + ii] = l + 4;
  1667. }
  1668. }
  1669. #endif
  1670. memset(y[i].hmask, 0, QK_K/8);
  1671. // We put the high-bit for the 1st 8 quants into bit 0, the next 8 into bit 1, etc.
  1672. int m = 0;
  1673. uint8_t hm = 1;
  1674. for (int j = 0; j < QK_K; ++j) {
  1675. if (L[j] > 3) {
  1676. y[i].hmask[m] |= hm;
  1677. L[j] -= 4;
  1678. }
  1679. if (++m == QK_K/8) {
  1680. m = 0; hm <<= 1;
  1681. }
  1682. }
  1683. #if QK_K == 256
  1684. for (int j = 0; j < QK_K; j += 128) {
  1685. for (int l = 0; l < 32; ++l) {
  1686. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1687. }
  1688. }
  1689. #else
  1690. for (int l = 0; l < 16; ++l) {
  1691. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1692. }
  1693. #endif
  1694. x += QK_K;
  1695. }
  1696. }
  1697. #if QK_K == 256
  1698. void dequantize_row_q3_K(const block_q3_K * restrict x, float * restrict y, int k) {
  1699. assert(k % QK_K == 0);
  1700. const int nb = k / QK_K;
  1701. const uint32_t kmask1 = 0x03030303;
  1702. const uint32_t kmask2 = 0x0f0f0f0f;
  1703. uint32_t aux[4];
  1704. const int8_t * scales = (const int8_t*)aux;
  1705. for (int i = 0; i < nb; i++) {
  1706. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  1707. const uint8_t * restrict q = x[i].qs;
  1708. const uint8_t * restrict hm = x[i].hmask;
  1709. uint8_t m = 1;
  1710. memcpy(aux, x[i].scales, 12);
  1711. uint32_t tmp = aux[2];
  1712. aux[2] = ((aux[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
  1713. aux[3] = ((aux[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
  1714. aux[0] = (aux[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
  1715. aux[1] = (aux[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
  1716. int is = 0;
  1717. float dl;
  1718. for (int n = 0; n < QK_K; n += 128) {
  1719. int shift = 0;
  1720. for (int j = 0; j < 4; ++j) {
  1721. dl = d_all * (scales[is++] - 32);
  1722. for (int l = 0; l < 16; ++l) {
  1723. *y++ = dl * ((int8_t)((q[l+ 0] >> shift) & 3) - ((hm[l+ 0] & m) ? 0 : 4));
  1724. }
  1725. dl = d_all * (scales[is++] - 32);
  1726. for (int l = 0; l < 16; ++l) {
  1727. *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3) - ((hm[l+16] & m) ? 0 : 4));
  1728. }
  1729. shift += 2;
  1730. m <<= 1;
  1731. }
  1732. q += 32;
  1733. }
  1734. }
  1735. }
  1736. #else
  1737. void dequantize_row_q3_K(const block_q3_K * restrict x, float * restrict y, int k) {
  1738. assert(k % QK_K == 0);
  1739. assert(QK_K == 64);
  1740. const int nb = k / QK_K;
  1741. for (int i = 0; i < nb; i++) {
  1742. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  1743. const uint8_t * restrict q = x[i].qs;
  1744. const uint8_t * restrict hm = x[i].hmask;
  1745. const float d1 = d_all * ((x[i].scales[0] & 0xF) - 8);
  1746. const float d2 = d_all * ((x[i].scales[0] >> 4) - 8);
  1747. const float d3 = d_all * ((x[i].scales[1] & 0xF) - 8);
  1748. const float d4 = d_all * ((x[i].scales[1] >> 4) - 8);
  1749. for (int l=0; l<8; ++l) {
  1750. uint8_t h = hm[l];
  1751. y[l+ 0] = d1 * ((int8_t)((q[l+0] >> 0) & 3) - ((h & 0x01) ? 0 : 4));
  1752. y[l+ 8] = d1 * ((int8_t)((q[l+8] >> 0) & 3) - ((h & 0x02) ? 0 : 4));
  1753. y[l+16] = d2 * ((int8_t)((q[l+0] >> 2) & 3) - ((h & 0x04) ? 0 : 4));
  1754. y[l+24] = d2 * ((int8_t)((q[l+8] >> 2) & 3) - ((h & 0x08) ? 0 : 4));
  1755. y[l+32] = d3 * ((int8_t)((q[l+0] >> 4) & 3) - ((h & 0x10) ? 0 : 4));
  1756. y[l+40] = d3 * ((int8_t)((q[l+8] >> 4) & 3) - ((h & 0x20) ? 0 : 4));
  1757. y[l+48] = d4 * ((int8_t)((q[l+0] >> 6) & 3) - ((h & 0x40) ? 0 : 4));
  1758. y[l+56] = d4 * ((int8_t)((q[l+8] >> 6) & 3) - ((h & 0x80) ? 0 : 4));
  1759. }
  1760. y += QK_K;
  1761. }
  1762. }
  1763. #endif
  1764. void quantize_row_q3_K(const float * restrict x, void * restrict vy, int k) {
  1765. quantize_row_q3_K_reference(x, vy, k);
  1766. }
  1767. size_t ggml_quantize_q3_K(const float * restrict src, void * restrict dst, int n, int k, int64_t * restrict hist) {
  1768. (void)hist; // TODO: collect histograms
  1769. for (int j = 0; j < n; j += k) {
  1770. block_q3_K * restrict y = (block_q3_K *)dst + j/QK_K;
  1771. quantize_row_q3_K_reference(src + j, y, k);
  1772. }
  1773. return (n/QK_K*sizeof(block_q3_K));
  1774. }
  1775. static void quantize_row_q3_K_impl(const float * restrict x, block_q3_K * restrict y, int n_per_row, const float * restrict quant_weights) {
  1776. #if QK_K != 256
  1777. (void)quant_weights;
  1778. quantize_row_q3_K_reference(x, y, n_per_row);
  1779. #else
  1780. assert(n_per_row % QK_K == 0);
  1781. const int nb = n_per_row / QK_K;
  1782. int8_t L[QK_K];
  1783. float scales[QK_K / 16];
  1784. float weight[16];
  1785. float sw[QK_K / 16];
  1786. int8_t Ls[QK_K / 16];
  1787. for (int i = 0; i < nb; i++) {
  1788. float sumx2 = 0;
  1789. for (int j = 0; j < QK_K; ++j) sumx2 += x[j]*x[j];
  1790. float sigma2 = 2*sumx2/QK_K;
  1791. for (int j = 0; j < QK_K/16; ++j) {
  1792. if (quant_weights) {
  1793. const float * qw = quant_weights ? quant_weights + QK_K * i + 16*j : NULL;
  1794. for (int l = 0; l < 16; ++l) weight[l] = qw[l] * sqrtf(sigma2 + x[16*j+l]*x[16*j+l]);
  1795. } else {
  1796. for (int l = 0; l < 16; ++l) weight[l] = x[16*j+l]*x[16*j+l];
  1797. }
  1798. float sumw = 0;
  1799. for (int l = 0; l < 16; ++l) sumw += weight[l];
  1800. sw[j] = sumw;
  1801. scales[j] = make_qx_quants(16, 4, x + 16*j, L + 16*j, 1, weight);
  1802. }
  1803. memset(y[i].scales, 0, 12);
  1804. float d_block = make_qx_quants(QK_K/16, 32, scales, Ls, 1, sw);
  1805. for (int j = 0; j < QK_K/16; ++j) {
  1806. int l = Ls[j];
  1807. if (j < 8) {
  1808. y[i].scales[j] = l & 0xF;
  1809. } else {
  1810. y[i].scales[j-8] |= ((l & 0xF) << 4);
  1811. }
  1812. l >>= 4;
  1813. y[i].scales[j%4 + 8] |= (l << (2*(j/4)));
  1814. }
  1815. y[i].d = GGML_FP32_TO_FP16(d_block);
  1816. int8_t sc;
  1817. for (int j = 0; j < QK_K/16; ++j) {
  1818. sc = j < 8 ? y[i].scales[j] & 0xF : y[i].scales[j-8] >> 4;
  1819. sc = (sc | (((y[i].scales[8 + j%4] >> (2*(j/4))) & 3) << 4)) - 32;
  1820. float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1821. if (!d) {
  1822. continue;
  1823. }
  1824. for (int ii = 0; ii < 16; ++ii) {
  1825. int l = nearest_int(x[16*j + ii]/d);
  1826. l = MAX(-4, MIN(3, l));
  1827. L[16*j + ii] = l + 4;
  1828. }
  1829. }
  1830. memset(y[i].hmask, 0, QK_K/8);
  1831. // We put the high-bit for the 1st 8 quants into bit 0, the next 8 into bit 1, etc.
  1832. int m = 0;
  1833. uint8_t hm = 1;
  1834. for (int j = 0; j < QK_K; ++j) {
  1835. if (L[j] > 3) {
  1836. y[i].hmask[m] |= hm;
  1837. L[j] -= 4;
  1838. }
  1839. if (++m == QK_K/8) {
  1840. m = 0; hm <<= 1;
  1841. }
  1842. }
  1843. for (int j = 0; j < QK_K; j += 128) {
  1844. for (int l = 0; l < 32; ++l) {
  1845. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1846. }
  1847. }
  1848. x += QK_K;
  1849. }
  1850. #endif
  1851. }
  1852. size_t quantize_q3_K(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  1853. (void)hist;
  1854. size_t row_size = ggml_row_size(GGML_TYPE_Q3_K, n_per_row);
  1855. if (!quant_weights) {
  1856. quantize_row_q3_K_reference(src, dst, nrow*n_per_row);
  1857. }
  1858. else {
  1859. char * qrow = (char *)dst;
  1860. for (int row = 0; row < nrow; ++row) {
  1861. quantize_row_q3_K_impl(src, (block_q3_K*)qrow, n_per_row, quant_weights);
  1862. src += n_per_row;
  1863. qrow += row_size;
  1864. }
  1865. }
  1866. return nrow * row_size;
  1867. }
  1868. // ====================== 4-bit (de)-quantization
  1869. void quantize_row_q4_K_reference(const float * restrict x, block_q4_K * restrict y, int k) {
  1870. assert(k % QK_K == 0);
  1871. const int nb = k / QK_K;
  1872. uint8_t L[QK_K];
  1873. uint8_t Laux[32];
  1874. float weights[32];
  1875. float mins[QK_K/32];
  1876. float scales[QK_K/32];
  1877. for (int i = 0; i < nb; i++) {
  1878. float max_scale = 0; // as we are deducting the min, scales are always positive
  1879. float max_min = 0;
  1880. for (int j = 0; j < QK_K/32; ++j) {
  1881. //scales[j] = make_qkx1_quants(32, 15, x + 32*j, L + 32*j, &mins[j], 9, 0.5f);
  1882. float sum_x2 = 0;
  1883. for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l];
  1884. float av_x = sqrtf(sum_x2/32);
  1885. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  1886. scales[j] = make_qkx2_quants(32, 15, x + 32*j, weights, L + 32*j, &mins[j], Laux, -1.f, 0.1f, 20, false);
  1887. float scale = scales[j];
  1888. if (scale > max_scale) {
  1889. max_scale = scale;
  1890. }
  1891. float min = mins[j];
  1892. if (min > max_min) {
  1893. max_min = min;
  1894. }
  1895. }
  1896. #if QK_K == 256
  1897. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  1898. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  1899. for (int j = 0; j < QK_K/32; ++j) {
  1900. uint8_t ls = nearest_int(inv_scale*scales[j]);
  1901. uint8_t lm = nearest_int(inv_min*mins[j]);
  1902. ls = MIN(63, ls);
  1903. lm = MIN(63, lm);
  1904. if (j < 4) {
  1905. y[i].scales[j] = ls;
  1906. y[i].scales[j+4] = lm;
  1907. } else {
  1908. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  1909. y[i].scales[j-4] |= ((ls >> 4) << 6);
  1910. y[i].scales[j-0] |= ((lm >> 4) << 6);
  1911. }
  1912. }
  1913. y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
  1914. y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
  1915. uint8_t sc, m;
  1916. for (int j = 0; j < QK_K/32; ++j) {
  1917. get_scale_min_k4(j, y[i].scales, &sc, &m);
  1918. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1919. if (!d) continue;
  1920. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  1921. for (int ii = 0; ii < 32; ++ii) {
  1922. int l = nearest_int((x[32*j + ii] + dm)/d);
  1923. l = MAX(0, MIN(15, l));
  1924. L[32*j + ii] = l;
  1925. }
  1926. }
  1927. #else
  1928. const float s_factor = 15.f;
  1929. float inv_scale = max_scale > 0 ? s_factor/max_scale : 0.f;
  1930. float inv_min = max_min > 0 ? s_factor/max_min : 0.f;
  1931. int d1 = nearest_int(inv_scale*scales[0]);
  1932. int m1 = nearest_int(inv_min*mins[0]);
  1933. int d2 = nearest_int(inv_scale*scales[1]);
  1934. int m2 = nearest_int(inv_min*mins[1]);
  1935. y[i].scales[0] = d1 | (m1 << 4);
  1936. y[i].scales[1] = d2 | (m2 << 4);
  1937. y[i].d[0] = GGML_FP32_TO_FP16(max_scale/s_factor);
  1938. y[i].d[1] = GGML_FP32_TO_FP16(max_min/s_factor);
  1939. float sumlx = 0;
  1940. int suml2 = 0;
  1941. for (int j = 0; j < QK_K/32; ++j) {
  1942. const uint8_t sd = y[i].scales[j] & 0xF;
  1943. const uint8_t sm = y[i].scales[j] >> 4;
  1944. const float d = GGML_FP16_TO_FP32(y[i].d[0]) * sd;
  1945. if (!d) continue;
  1946. const float m = GGML_FP16_TO_FP32(y[i].d[1]) * sm;
  1947. for (int ii = 0; ii < 32; ++ii) {
  1948. int l = nearest_int((x[32*j + ii] + m)/d);
  1949. l = MAX(0, MIN(15, l));
  1950. L[32*j + ii] = l;
  1951. sumlx += (x[32*j + ii] + m)*l*sd;
  1952. suml2 += l*l*sd*sd;
  1953. }
  1954. }
  1955. if (suml2) {
  1956. y[i].d[0] = GGML_FP32_TO_FP16(sumlx/suml2);
  1957. }
  1958. #endif
  1959. uint8_t * q = y[i].qs;
  1960. for (int j = 0; j < QK_K; j += 64) {
  1961. for (int l = 0; l < 32; ++l) q[l] = L[j + l] | (L[j + l + 32] << 4);
  1962. q += 32;
  1963. }
  1964. x += QK_K;
  1965. }
  1966. }
  1967. void dequantize_row_q4_K(const block_q4_K * restrict x, float * restrict y, int k) {
  1968. assert(k % QK_K == 0);
  1969. const int nb = k / QK_K;
  1970. for (int i = 0; i < nb; i++) {
  1971. const uint8_t * q = x[i].qs;
  1972. #if QK_K == 256
  1973. const float d = GGML_FP16_TO_FP32(x[i].d);
  1974. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  1975. int is = 0;
  1976. uint8_t sc, m;
  1977. for (int j = 0; j < QK_K; j += 64) {
  1978. get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
  1979. const float d1 = d * sc; const float m1 = min * m;
  1980. get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
  1981. const float d2 = d * sc; const float m2 = min * m;
  1982. for (int l = 0; l < 32; ++l) *y++ = d1 * (q[l] & 0xF) - m1;
  1983. for (int l = 0; l < 32; ++l) *y++ = d2 * (q[l] >> 4) - m2;
  1984. q += 32; is += 2;
  1985. }
  1986. #else
  1987. const float dall = GGML_FP16_TO_FP32(x[i].d[0]);
  1988. const float mall = GGML_FP16_TO_FP32(x[i].d[1]);
  1989. const float d1 = dall * (x[i].scales[0] & 0xF), m1 = mall * (x[i].scales[0] >> 4);
  1990. const float d2 = dall * (x[i].scales[1] & 0xF), m2 = mall * (x[i].scales[1] >> 4);
  1991. for (int l = 0; l < 32; ++l) {
  1992. y[l+ 0] = d1 * (q[l] & 0xF) - m1;
  1993. y[l+32] = d2 * (q[l] >> 4) - m2;
  1994. }
  1995. y += QK_K;
  1996. #endif
  1997. }
  1998. }
  1999. void quantize_row_q4_K(const float * restrict x, void * restrict vy, int k) {
  2000. assert(k % QK_K == 0);
  2001. block_q4_K * restrict y = vy;
  2002. quantize_row_q4_K_reference(x, y, k);
  2003. }
  2004. size_t ggml_quantize_q4_K(const float * restrict src, void * restrict dst, int n, int k, int64_t * restrict hist) {
  2005. assert(k % QK_K == 0);
  2006. (void)hist; // TODO: collect histograms
  2007. for (int j = 0; j < n; j += k) {
  2008. block_q4_K * restrict y = (block_q4_K *)dst + j/QK_K;
  2009. quantize_row_q4_K_reference(src + j, y, k);
  2010. }
  2011. return (n/QK_K*sizeof(block_q4_K));
  2012. }
  2013. static void quantize_row_q4_K_impl(const float * restrict x, block_q4_K * restrict y, int n_per_row, const float * quant_weights) {
  2014. #if QK_K != 256
  2015. (void)quant_weights;
  2016. quantize_row_q4_K_reference(x, y, n_per_row);
  2017. #else
  2018. assert(n_per_row % QK_K == 0);
  2019. const int nb = n_per_row / QK_K;
  2020. uint8_t L[QK_K];
  2021. uint8_t Laux[32];
  2022. uint8_t Ls[QK_K/32];
  2023. uint8_t Lm[QK_K/32];
  2024. float weights[32];
  2025. float sw[QK_K/32];
  2026. float mins[QK_K/32];
  2027. float scales[QK_K/32];
  2028. for (int i = 0; i < nb; i++) {
  2029. float sum_x2 = 0;
  2030. for (int l = 0; l < QK_K; ++l) sum_x2 += x[l] * x[l];
  2031. float sigma2 = 2*sum_x2/QK_K;
  2032. float av_x = sqrtf(sigma2);
  2033. for (int j = 0; j < QK_K/32; ++j) {
  2034. if (quant_weights) {
  2035. const float * qw = quant_weights + QK_K*i + 32*j;
  2036. for (int l = 0; l < 32; ++l) weights[l] = qw[l] * sqrtf(sigma2 + x[32*j + l]*x[32*j + l]);
  2037. } else {
  2038. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  2039. }
  2040. float sumw = 0;
  2041. for (int l = 0; l < 32; ++l) sumw += weights[l];
  2042. sw[j] = sumw;
  2043. scales[j] = make_qkx3_quants(32, 15, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  2044. }
  2045. float d_block = make_qp_quants(QK_K/32, 63, scales, Ls, sw);
  2046. float m_block = make_qp_quants(QK_K/32, 63, mins, Lm, sw);
  2047. for (int j = 0; j < QK_K/32; ++j) {
  2048. uint8_t ls = Ls[j];
  2049. uint8_t lm = Lm[j];
  2050. if (j < 4) {
  2051. y[i].scales[j] = ls;
  2052. y[i].scales[j+4] = lm;
  2053. } else {
  2054. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  2055. y[i].scales[j-4] |= ((ls >> 4) << 6);
  2056. y[i].scales[j-0] |= ((lm >> 4) << 6);
  2057. }
  2058. }
  2059. y[i].d = GGML_FP32_TO_FP16(d_block);
  2060. y[i].dmin = GGML_FP32_TO_FP16(m_block);
  2061. uint8_t sc, m;
  2062. for (int j = 0; j < QK_K/32; ++j) {
  2063. get_scale_min_k4(j, y[i].scales, &sc, &m);
  2064. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  2065. if (!d) continue;
  2066. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  2067. for (int ii = 0; ii < 32; ++ii) {
  2068. int l = nearest_int((x[32*j + ii] + dm)/d);
  2069. l = MAX(0, MIN(15, l));
  2070. L[32*j + ii] = l;
  2071. }
  2072. }
  2073. uint8_t * q = y[i].qs;
  2074. for (int j = 0; j < QK_K; j += 64) {
  2075. for (int l = 0; l < 32; ++l) q[l] = L[j + l] | (L[j + l + 32] << 4);
  2076. q += 32;
  2077. }
  2078. x += QK_K;
  2079. }
  2080. #endif
  2081. }
  2082. size_t quantize_q4_K(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  2083. (void)hist;
  2084. size_t row_size = ggml_row_size(GGML_TYPE_Q4_K, n_per_row);
  2085. if (!quant_weights) {
  2086. quantize_row_q4_K_reference(src, dst, nrow*n_per_row);
  2087. }
  2088. else {
  2089. char * qrow = (char *)dst;
  2090. for (int row = 0; row < nrow; ++row) {
  2091. quantize_row_q4_K_impl(src, (block_q4_K*)qrow, n_per_row, quant_weights);
  2092. src += n_per_row;
  2093. qrow += row_size;
  2094. }
  2095. }
  2096. return nrow * row_size;
  2097. }
  2098. // ====================== 5-bit (de)-quantization
  2099. void quantize_row_q5_K_reference(const float * restrict x, block_q5_K * restrict y, int k) {
  2100. assert(k % QK_K == 0);
  2101. const int nb = k / QK_K;
  2102. #if QK_K == 256
  2103. uint8_t L[QK_K];
  2104. float mins[QK_K/32];
  2105. float scales[QK_K/32];
  2106. float weights[32];
  2107. uint8_t Laux[32];
  2108. #else
  2109. int8_t L[QK_K];
  2110. float scales[QK_K/16];
  2111. #endif
  2112. for (int i = 0; i < nb; i++) {
  2113. #if QK_K == 256
  2114. float max_scale = 0; // as we are deducting the min, scales are always positive
  2115. float max_min = 0;
  2116. for (int j = 0; j < QK_K/32; ++j) {
  2117. //scales[j] = make_qkx1_quants(32, 31, x + 32*j, L + 32*j, &mins[j], 9, 0.5f);
  2118. float sum_x2 = 0;
  2119. for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l];
  2120. float av_x = sqrtf(sum_x2/32);
  2121. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  2122. scales[j] = make_qkx2_quants(32, 31, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.5f, 0.1f, 15, false);
  2123. float scale = scales[j];
  2124. if (scale > max_scale) {
  2125. max_scale = scale;
  2126. }
  2127. float min = mins[j];
  2128. if (min > max_min) {
  2129. max_min = min;
  2130. }
  2131. }
  2132. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  2133. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  2134. for (int j = 0; j < QK_K/32; ++j) {
  2135. uint8_t ls = nearest_int(inv_scale*scales[j]);
  2136. uint8_t lm = nearest_int(inv_min*mins[j]);
  2137. ls = MIN(63, ls);
  2138. lm = MIN(63, lm);
  2139. if (j < 4) {
  2140. y[i].scales[j] = ls;
  2141. y[i].scales[j+4] = lm;
  2142. } else {
  2143. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  2144. y[i].scales[j-4] |= ((ls >> 4) << 6);
  2145. y[i].scales[j-0] |= ((lm >> 4) << 6);
  2146. }
  2147. }
  2148. y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
  2149. y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
  2150. uint8_t sc, m;
  2151. for (int j = 0; j < QK_K/32; ++j) {
  2152. get_scale_min_k4(j, y[i].scales, &sc, &m);
  2153. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  2154. if (!d) continue;
  2155. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  2156. for (int ii = 0; ii < 32; ++ii) {
  2157. int l = nearest_int((x[32*j + ii] + dm)/d);
  2158. l = MAX(0, MIN(31, l));
  2159. L[32*j + ii] = l;
  2160. }
  2161. }
  2162. uint8_t * restrict qh = y[i].qh;
  2163. uint8_t * restrict ql = y[i].qs;
  2164. memset(qh, 0, QK_K/8);
  2165. uint8_t m1 = 1, m2 = 2;
  2166. for (int n = 0; n < QK_K; n += 64) {
  2167. for (int j = 0; j < 32; ++j) {
  2168. int l1 = L[n + j];
  2169. if (l1 > 15) {
  2170. l1 -= 16; qh[j] |= m1;
  2171. }
  2172. int l2 = L[n + j + 32];
  2173. if (l2 > 15) {
  2174. l2 -= 16; qh[j] |= m2;
  2175. }
  2176. ql[j] = l1 | (l2 << 4);
  2177. }
  2178. m1 <<= 2; m2 <<= 2;
  2179. ql += 32;
  2180. }
  2181. #else
  2182. float max_scale = 0, amax = 0;
  2183. for (int j = 0; j < QK_K/16; ++j) {
  2184. scales[j] = make_qx_quants(16, 16, x + 16*j, L + 16*j, 1, NULL);
  2185. float abs_scale = fabsf(scales[j]);
  2186. if (abs_scale > amax) {
  2187. amax = abs_scale;
  2188. max_scale = scales[j];
  2189. }
  2190. }
  2191. float iscale = -128.f/max_scale;
  2192. for (int j = 0; j < QK_K/16; ++j) {
  2193. int l = nearest_int(iscale*scales[j]);
  2194. y[i].scales[j] = MAX(-128, MIN(127, l));
  2195. }
  2196. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2197. for (int j = 0; j < QK_K/16; ++j) {
  2198. const float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2199. if (!d) continue;
  2200. for (int ii = 0; ii < 16; ++ii) {
  2201. int l = nearest_int(x[16*j + ii]/d);
  2202. l = MAX(-16, MIN(15, l));
  2203. L[16*j + ii] = l + 16;
  2204. }
  2205. }
  2206. uint8_t * restrict qh = y[i].qh;
  2207. uint8_t * restrict ql = y[i].qs;
  2208. memset(qh, 0, QK_K/8);
  2209. for (int j = 0; j < 32; ++j) {
  2210. int jm = j%8;
  2211. int is = j/8;
  2212. int l1 = L[j];
  2213. if (l1 > 15) {
  2214. l1 -= 16; qh[jm] |= (1 << is);
  2215. }
  2216. int l2 = L[j + 32];
  2217. if (l2 > 15) {
  2218. l2 -= 16; qh[jm] |= (1 << (4 + is));
  2219. }
  2220. ql[j] = l1 | (l2 << 4);
  2221. }
  2222. #endif
  2223. x += QK_K;
  2224. }
  2225. }
  2226. void dequantize_row_q5_K(const block_q5_K * restrict x, float * restrict y, int k) {
  2227. assert(k % QK_K == 0);
  2228. const int nb = k / QK_K;
  2229. for (int i = 0; i < nb; i++) {
  2230. const uint8_t * ql = x[i].qs;
  2231. const uint8_t * qh = x[i].qh;
  2232. #if QK_K == 256
  2233. const float d = GGML_FP16_TO_FP32(x[i].d);
  2234. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  2235. int is = 0;
  2236. uint8_t sc, m;
  2237. uint8_t u1 = 1, u2 = 2;
  2238. for (int j = 0; j < QK_K; j += 64) {
  2239. get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
  2240. const float d1 = d * sc; const float m1 = min * m;
  2241. get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
  2242. const float d2 = d * sc; const float m2 = min * m;
  2243. for (int l = 0; l < 32; ++l) *y++ = d1 * ((ql[l] & 0xF) + (qh[l] & u1 ? 16 : 0)) - m1;
  2244. for (int l = 0; l < 32; ++l) *y++ = d2 * ((ql[l] >> 4) + (qh[l] & u2 ? 16 : 0)) - m2;
  2245. ql += 32; is += 2;
  2246. u1 <<= 2; u2 <<= 2;
  2247. }
  2248. #else
  2249. float d = GGML_FP16_TO_FP32(x[i].d);
  2250. const int8_t * restrict s = x[i].scales;
  2251. for (int l = 0; l < 8; ++l) {
  2252. y[l+ 0] = d * s[0] * ((ql[l+ 0] & 0xF) - (qh[l] & 0x01 ? 0 : 16));
  2253. y[l+ 8] = d * s[0] * ((ql[l+ 8] & 0xF) - (qh[l] & 0x02 ? 0 : 16));
  2254. y[l+16] = d * s[1] * ((ql[l+16] & 0xF) - (qh[l] & 0x04 ? 0 : 16));
  2255. y[l+24] = d * s[1] * ((ql[l+24] & 0xF) - (qh[l] & 0x08 ? 0 : 16));
  2256. y[l+32] = d * s[2] * ((ql[l+ 0] >> 4) - (qh[l] & 0x10 ? 0 : 16));
  2257. y[l+40] = d * s[2] * ((ql[l+ 8] >> 4) - (qh[l] & 0x20 ? 0 : 16));
  2258. y[l+48] = d * s[3] * ((ql[l+16] >> 4) - (qh[l] & 0x40 ? 0 : 16));
  2259. y[l+56] = d * s[3] * ((ql[l+24] >> 4) - (qh[l] & 0x80 ? 0 : 16));
  2260. }
  2261. y += QK_K;
  2262. #endif
  2263. }
  2264. }
  2265. void quantize_row_q5_K(const float * restrict x, void * restrict vy, int k) {
  2266. assert(k % QK_K == 0);
  2267. block_q5_K * restrict y = vy;
  2268. quantize_row_q5_K_reference(x, y, k);
  2269. }
  2270. size_t ggml_quantize_q5_K(const float * restrict src, void * restrict dst, int n, int k, int64_t * restrict hist) {
  2271. assert(k % QK_K == 0);
  2272. (void)hist; // TODO: collect histograms
  2273. for (int j = 0; j < n; j += k) {
  2274. block_q5_K * restrict y = (block_q5_K *)dst + j/QK_K;
  2275. quantize_row_q5_K_reference(src + j, y, k);
  2276. }
  2277. return (n/QK_K*sizeof(block_q5_K));
  2278. }
  2279. static void quantize_row_q5_K_impl(const float * restrict x, block_q5_K * restrict y, int n_per_row, const float * quant_weights) {
  2280. #if QK_K != 256
  2281. (void)quant_weights;
  2282. quantize_row_q5_K_reference(x, y, n_per_row);
  2283. #else
  2284. assert(n_per_row % QK_K == 0);
  2285. const int nb = n_per_row / QK_K;
  2286. uint8_t L[QK_K];
  2287. uint8_t Laux[32];
  2288. uint8_t Ls[QK_K/32];
  2289. uint8_t Lm[QK_K/32];
  2290. float mins[QK_K/32];
  2291. float scales[QK_K/32];
  2292. float sw[QK_K/32];
  2293. float weights[32];
  2294. for (int i = 0; i < nb; i++) {
  2295. float sum_x2 = 0;
  2296. for (int l = 0; l < QK_K; ++l) sum_x2 += x[l] * x[l];
  2297. float sigma2 = 2*sum_x2/QK_K;
  2298. float av_x = sqrtf(sigma2);
  2299. for (int j = 0; j < QK_K/32; ++j) {
  2300. if (quant_weights) {
  2301. const float * qw = quant_weights + QK_K*i + 32*j;
  2302. for (int l = 0; l < 32; ++l) weights[l] = qw[l] * sqrtf(sigma2 + x[32*j + l]*x[32*j + l]);
  2303. } else {
  2304. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  2305. }
  2306. float sumw = 0;
  2307. for (int l = 0; l < 32; ++l) sumw += weights[l];
  2308. sw[j] = sumw;
  2309. scales[j] = make_qkx3_quants(32, 31, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  2310. }
  2311. float d_block = make_qp_quants(QK_K/32, 63, scales, Ls, sw);
  2312. float m_block = make_qp_quants(QK_K/32, 63, mins, Lm, sw);
  2313. for (int j = 0; j < QK_K/32; ++j) {
  2314. uint8_t ls = Ls[j];
  2315. uint8_t lm = Lm[j];
  2316. ls = MIN(63, ls);
  2317. lm = MIN(63, lm);
  2318. if (j < 4) {
  2319. y[i].scales[j] = ls;
  2320. y[i].scales[j+4] = lm;
  2321. } else {
  2322. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  2323. y[i].scales[j-4] |= ((ls >> 4) << 6);
  2324. y[i].scales[j-0] |= ((lm >> 4) << 6);
  2325. }
  2326. }
  2327. y[i].d = GGML_FP32_TO_FP16(d_block);
  2328. y[i].dmin = GGML_FP32_TO_FP16(m_block);
  2329. uint8_t sc, m;
  2330. for (int j = 0; j < QK_K/32; ++j) {
  2331. get_scale_min_k4(j, y[i].scales, &sc, &m);
  2332. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  2333. if (!d) continue;
  2334. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  2335. for (int ii = 0; ii < 32; ++ii) {
  2336. int l = nearest_int((x[32*j + ii] + dm)/d);
  2337. l = MAX(0, MIN(31, l));
  2338. L[32*j + ii] = l;
  2339. }
  2340. }
  2341. uint8_t * restrict qh = y[i].qh;
  2342. uint8_t * restrict ql = y[i].qs;
  2343. memset(qh, 0, QK_K/8);
  2344. uint8_t m1 = 1, m2 = 2;
  2345. for (int n = 0; n < QK_K; n += 64) {
  2346. for (int j = 0; j < 32; ++j) {
  2347. int l1 = L[n + j];
  2348. if (l1 > 15) {
  2349. l1 -= 16; qh[j] |= m1;
  2350. }
  2351. int l2 = L[n + j + 32];
  2352. if (l2 > 15) {
  2353. l2 -= 16; qh[j] |= m2;
  2354. }
  2355. ql[j] = l1 | (l2 << 4);
  2356. }
  2357. m1 <<= 2; m2 <<= 2;
  2358. ql += 32;
  2359. }
  2360. x += QK_K;
  2361. }
  2362. #endif
  2363. }
  2364. size_t quantize_q5_K(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  2365. (void)hist;
  2366. size_t row_size = ggml_row_size(GGML_TYPE_Q5_K, n_per_row);
  2367. if (!quant_weights) {
  2368. quantize_row_q5_K_reference(src, dst, nrow*n_per_row);
  2369. }
  2370. else {
  2371. char * qrow = (char *)dst;
  2372. for (int row = 0; row < nrow; ++row) {
  2373. quantize_row_q5_K_impl(src, (block_q5_K*)qrow, n_per_row, quant_weights);
  2374. src += n_per_row;
  2375. qrow += row_size;
  2376. }
  2377. }
  2378. return nrow * row_size;
  2379. }
  2380. // ====================== 6-bit (de)-quantization
  2381. void quantize_row_q6_K_reference(const float * restrict x, block_q6_K * restrict y, int k) {
  2382. assert(k % QK_K == 0);
  2383. const int nb = k / QK_K;
  2384. int8_t L[QK_K];
  2385. float scales[QK_K/16];
  2386. for (int i = 0; i < nb; i++) {
  2387. float max_scale = 0;
  2388. float max_abs_scale = 0;
  2389. for (int ib = 0; ib < QK_K/16; ++ib) {
  2390. const float scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, NULL);
  2391. scales[ib] = scale;
  2392. const float abs_scale = fabsf(scale);
  2393. if (abs_scale > max_abs_scale) {
  2394. max_abs_scale = abs_scale;
  2395. max_scale = scale;
  2396. }
  2397. }
  2398. if (!max_abs_scale) {
  2399. memset(&y[i], 0, sizeof(block_q6_K));
  2400. y[i].d = GGML_FP32_TO_FP16(0.f);
  2401. x += QK_K;
  2402. continue;
  2403. }
  2404. float iscale = -128.f/max_scale;
  2405. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2406. for (int ib = 0; ib < QK_K/16; ++ib) {
  2407. y[i].scales[ib] = MIN(127, nearest_int(iscale*scales[ib]));
  2408. }
  2409. for (int j = 0; j < QK_K/16; ++j) {
  2410. float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2411. if (!d) {
  2412. continue;
  2413. }
  2414. for (int ii = 0; ii < 16; ++ii) {
  2415. int l = nearest_int(x[16*j + ii]/d);
  2416. l = MAX(-32, MIN(31, l));
  2417. L[16*j + ii] = l + 32;
  2418. }
  2419. }
  2420. uint8_t * restrict ql = y[i].ql;
  2421. uint8_t * restrict qh = y[i].qh;
  2422. #if QK_K == 256
  2423. for (int j = 0; j < QK_K; j += 128) {
  2424. for (int l = 0; l < 32; ++l) {
  2425. const uint8_t q1 = L[j + l + 0] & 0xF;
  2426. const uint8_t q2 = L[j + l + 32] & 0xF;
  2427. const uint8_t q3 = L[j + l + 64] & 0xF;
  2428. const uint8_t q4 = L[j + l + 96] & 0xF;
  2429. ql[l+ 0] = q1 | (q3 << 4);
  2430. ql[l+32] = q2 | (q4 << 4);
  2431. qh[l] = (L[j + l] >> 4) | ((L[j + l + 32] >> 4) << 2) | ((L[j + l + 64] >> 4) << 4) | ((L[j + l + 96] >> 4) << 6);
  2432. }
  2433. ql += 64;
  2434. qh += 32;
  2435. }
  2436. #else
  2437. for (int l = 0; l < 32; ++l) {
  2438. const uint8_t q1 = L[l + 0] & 0xF;
  2439. const uint8_t q2 = L[l + 32] & 0xF;
  2440. ql[l] = q1 | (q2 << 4);
  2441. }
  2442. for (int l = 0; l < 16; ++l) {
  2443. qh[l] = (L[l] >> 4) | ((L[l + 16] >> 4) << 2) | ((L[l + 32] >> 4) << 4) | ((L[l + 48] >> 4) << 6);
  2444. }
  2445. #endif
  2446. x += QK_K;
  2447. }
  2448. }
  2449. void dequantize_row_q6_K(const block_q6_K * restrict x, float * restrict y, int k) {
  2450. assert(k % QK_K == 0);
  2451. const int nb = k / QK_K;
  2452. for (int i = 0; i < nb; i++) {
  2453. const float d = GGML_FP16_TO_FP32(x[i].d);
  2454. const uint8_t * restrict ql = x[i].ql;
  2455. const uint8_t * restrict qh = x[i].qh;
  2456. const int8_t * restrict sc = x[i].scales;
  2457. #if QK_K == 256
  2458. for (int n = 0; n < QK_K; n += 128) {
  2459. for (int l = 0; l < 32; ++l) {
  2460. int is = l/16;
  2461. const int8_t q1 = (int8_t)((ql[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  2462. const int8_t q2 = (int8_t)((ql[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  2463. const int8_t q3 = (int8_t)((ql[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  2464. const int8_t q4 = (int8_t)((ql[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  2465. y[l + 0] = d * sc[is + 0] * q1;
  2466. y[l + 32] = d * sc[is + 2] * q2;
  2467. y[l + 64] = d * sc[is + 4] * q3;
  2468. y[l + 96] = d * sc[is + 6] * q4;
  2469. }
  2470. y += 128;
  2471. ql += 64;
  2472. qh += 32;
  2473. sc += 8;
  2474. }
  2475. #else
  2476. for (int l = 0; l < 16; ++l) {
  2477. const int8_t q1 = (int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  2478. const int8_t q2 = (int8_t)((ql[l+16] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  2479. const int8_t q3 = (int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  2480. const int8_t q4 = (int8_t)((ql[l+16] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  2481. y[l+ 0] = d * sc[0] * q1;
  2482. y[l+16] = d * sc[1] * q2;
  2483. y[l+32] = d * sc[2] * q3;
  2484. y[l+48] = d * sc[3] * q4;
  2485. }
  2486. y += 64;
  2487. #endif
  2488. }
  2489. }
  2490. void quantize_row_q6_K(const float * restrict x, void * restrict vy, int k) {
  2491. assert(k % QK_K == 0);
  2492. block_q6_K * restrict y = vy;
  2493. quantize_row_q6_K_reference(x, y, k);
  2494. }
  2495. size_t ggml_quantize_q6_K(const float * src, void * dst, int n, int k, int64_t * hist) {
  2496. assert(k % QK_K == 0);
  2497. (void)hist; // TODO: collect histograms
  2498. for (int j = 0; j < n; j += k) {
  2499. block_q6_K * restrict y = (block_q6_K *)dst + j/QK_K;
  2500. quantize_row_q6_K_reference(src + j, y, k);
  2501. }
  2502. return (n/QK_K*sizeof(block_q6_K));
  2503. }
  2504. static void quantize_row_q6_K_impl(const float * restrict x, block_q6_K * restrict y, int n_per_row, const float * quant_weights) {
  2505. #if QK_K != 256
  2506. (void)quant_weights;
  2507. quantize_row_q6_K_reference(x, y, n_per_row);
  2508. #else
  2509. assert(n_per_row % QK_K == 0);
  2510. const int nb = n_per_row / QK_K;
  2511. int8_t L[QK_K];
  2512. float scales[QK_K/16];
  2513. //float weights[16];
  2514. for (int i = 0; i < nb; i++) {
  2515. //float sum_x2 = 0;
  2516. //for (int j = 0; j < QK_K; ++j) sum_x2 += x[j]*x[j];
  2517. //float sigma2 = sum_x2/QK_K;
  2518. float max_scale = 0;
  2519. float max_abs_scale = 0;
  2520. for (int ib = 0; ib < QK_K/16; ++ib) {
  2521. float scale;
  2522. if (quant_weights) {
  2523. const float * qw = quant_weights + QK_K*i + 16*ib;
  2524. //for (int j = 0; j < 16; ++j) weights[j] = qw[j] * sqrtf(sigma2 + x[16*ib + j]*x[16*ib + j]);
  2525. //scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, weights);
  2526. scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, qw);
  2527. } else {
  2528. scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, NULL);
  2529. }
  2530. scales[ib] = scale;
  2531. const float abs_scale = fabsf(scale);
  2532. if (abs_scale > max_abs_scale) {
  2533. max_abs_scale = abs_scale;
  2534. max_scale = scale;
  2535. }
  2536. }
  2537. if (!max_abs_scale) {
  2538. memset(&y[i], 0, sizeof(block_q6_K));
  2539. y[i].d = GGML_FP32_TO_FP16(0.f);
  2540. x += QK_K;
  2541. continue;
  2542. }
  2543. float iscale = -128.f/max_scale;
  2544. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2545. for (int ib = 0; ib < QK_K/16; ++ib) {
  2546. y[i].scales[ib] = MIN(127, nearest_int(iscale*scales[ib]));
  2547. }
  2548. for (int j = 0; j < QK_K/16; ++j) {
  2549. float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2550. if (!d) {
  2551. continue;
  2552. }
  2553. for (int ii = 0; ii < 16; ++ii) {
  2554. int l = nearest_int(x[16*j + ii]/d);
  2555. l = MAX(-32, MIN(31, l));
  2556. L[16*j + ii] = l + 32;
  2557. }
  2558. }
  2559. uint8_t * restrict ql = y[i].ql;
  2560. uint8_t * restrict qh = y[i].qh;
  2561. for (int j = 0; j < QK_K; j += 128) {
  2562. for (int l = 0; l < 32; ++l) {
  2563. const uint8_t q1 = L[j + l + 0] & 0xF;
  2564. const uint8_t q2 = L[j + l + 32] & 0xF;
  2565. const uint8_t q3 = L[j + l + 64] & 0xF;
  2566. const uint8_t q4 = L[j + l + 96] & 0xF;
  2567. ql[l+ 0] = q1 | (q3 << 4);
  2568. ql[l+32] = q2 | (q4 << 4);
  2569. qh[l] = (L[j + l] >> 4) | ((L[j + l + 32] >> 4) << 2) | ((L[j + l + 64] >> 4) << 4) | ((L[j + l + 96] >> 4) << 6);
  2570. }
  2571. ql += 64;
  2572. qh += 32;
  2573. }
  2574. x += QK_K;
  2575. }
  2576. #endif
  2577. }
  2578. size_t quantize_q6_K(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  2579. (void)hist;
  2580. size_t row_size = ggml_row_size(GGML_TYPE_Q6_K, n_per_row);
  2581. if (!quant_weights) {
  2582. quantize_row_q6_K_reference(src, dst, nrow*n_per_row);
  2583. }
  2584. else {
  2585. char * qrow = (char *)dst;
  2586. for (int row = 0; row < nrow; ++row) {
  2587. quantize_row_q6_K_impl(src, (block_q6_K*)qrow, n_per_row, quant_weights);
  2588. src += n_per_row;
  2589. qrow += row_size;
  2590. }
  2591. }
  2592. return nrow * row_size;
  2593. }
  2594. static void quantize_row_q4_0_impl(const float * restrict x, block_q4_0 * restrict y, int n_per_row, const float * quant_weights) {
  2595. static_assert(QK4_0 == 32, "QK4_0 must be 32");
  2596. if (!quant_weights) {
  2597. quantize_row_q4_0_reference(x, y, n_per_row);
  2598. return;
  2599. }
  2600. float weight[QK4_0];
  2601. int8_t L[QK4_0];
  2602. float sum_x2 = 0;
  2603. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2604. float sigma2 = sum_x2/n_per_row;
  2605. const int nb = n_per_row/QK4_0;
  2606. for (int ib = 0; ib < nb; ++ib) {
  2607. const float * xb = x + QK4_0 * ib;
  2608. const float * qw = quant_weights + QK4_0 * ib;
  2609. for (int j = 0; j < QK4_0; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2610. float d = make_qx_quants(QK4_0, 8, xb, L, 1, weight);
  2611. y[ib].d = GGML_FP32_TO_FP16(d);
  2612. for (int j = 0; j < 16; ++j) {
  2613. y[ib].qs[j] = L[j] | (L[j+16] << 4);
  2614. }
  2615. }
  2616. }
  2617. size_t quantize_q4_0(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  2618. if (!quant_weights) {
  2619. return ggml_quantize_q4_0(src, dst, nrow*n_per_row, n_per_row, hist);
  2620. }
  2621. size_t row_size = ggml_row_size(GGML_TYPE_Q4_0, n_per_row);
  2622. char * qrow = (char *)dst;
  2623. for (int row = 0; row < nrow; ++row) {
  2624. quantize_row_q4_0_impl(src, (block_q4_0*)qrow, n_per_row, quant_weights);
  2625. src += n_per_row;
  2626. qrow += row_size;
  2627. }
  2628. return nrow * row_size;
  2629. }
  2630. static void quantize_row_q4_1_impl(const float * restrict x, block_q4_1 * restrict y, int n_per_row, const float * quant_weights) {
  2631. static_assert(QK4_1 == 32, "QK4_1 must be 32");
  2632. if (!quant_weights) {
  2633. quantize_row_q4_1_reference(x, y, n_per_row);
  2634. return;
  2635. }
  2636. float weight[QK4_1];
  2637. uint8_t L[QK4_1], Laux[QK4_1];
  2638. float sum_x2 = 0;
  2639. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2640. float sigma2 = sum_x2/n_per_row;
  2641. const int nb = n_per_row/QK4_1;
  2642. for (int ib = 0; ib < nb; ++ib) {
  2643. const float * xb = x + QK4_1 * ib;
  2644. const float * qw = quant_weights + QK4_1 * ib;
  2645. for (int j = 0; j < QK4_1; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2646. float min;
  2647. float d = make_qkx3_quants(QK4_1, 15, xb, weight, L, &min, Laux, -0.9f, 0.05f, 36, false);
  2648. y[ib].d = GGML_FP32_TO_FP16(d);
  2649. y[ib].m = GGML_FP32_TO_FP16(-min);
  2650. for (int j = 0; j < 16; ++j) {
  2651. y[ib].qs[j] = L[j] | (L[j+16] << 4);
  2652. }
  2653. }
  2654. }
  2655. size_t quantize_q4_1(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  2656. if (!quant_weights) {
  2657. return ggml_quantize_q4_1(src, dst, nrow*n_per_row, n_per_row, hist);
  2658. }
  2659. size_t row_size = ggml_row_size(GGML_TYPE_Q4_1, n_per_row);
  2660. char * qrow = (char *)dst;
  2661. for (int row = 0; row < nrow; ++row) {
  2662. quantize_row_q4_1_impl(src, (block_q4_1*)qrow, n_per_row, quant_weights);
  2663. src += n_per_row;
  2664. qrow += row_size;
  2665. }
  2666. return nrow * row_size;
  2667. }
  2668. static void quantize_row_q5_0_impl(const float * restrict x, block_q5_0 * restrict y, int n_per_row, const float * quant_weights) {
  2669. static_assert(QK5_0 == 32, "QK5_0 must be 32");
  2670. if (!quant_weights) {
  2671. quantize_row_q5_0_reference(x, y, n_per_row);
  2672. return;
  2673. }
  2674. float weight[QK5_0];
  2675. int8_t L[QK5_0];
  2676. float sum_x2 = 0;
  2677. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2678. float sigma2 = sum_x2/n_per_row;
  2679. const int nb = n_per_row/QK5_0;
  2680. for (int ib = 0; ib < nb; ++ib) {
  2681. const float * xb = x + QK5_0 * ib;
  2682. const float * qw = quant_weights + QK5_0 * ib;
  2683. for (int j = 0; j < QK5_0; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2684. float d = make_qx_quants(QK5_0, 16, xb, L, 1, weight);
  2685. y[ib].d = GGML_FP32_TO_FP16(d);
  2686. uint32_t qh = 0;
  2687. for (int j = 0; j < 16; ++j) {
  2688. const uint8_t xi0 = L[j];
  2689. const uint8_t xi1 = L[j+16];
  2690. y[ib].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  2691. // get the 5-th bit and store it in qh at the right position
  2692. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  2693. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_0/2);
  2694. }
  2695. memcpy(&y[ib].qh, &qh, sizeof(qh));
  2696. }
  2697. }
  2698. size_t quantize_q5_0(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  2699. if (!quant_weights) {
  2700. return ggml_quantize_q5_0(src, dst, nrow*n_per_row, n_per_row, hist);
  2701. }
  2702. size_t row_size = ggml_row_size(GGML_TYPE_Q5_0, n_per_row);
  2703. char * qrow = (char *)dst;
  2704. for (int row = 0; row < nrow; ++row) {
  2705. quantize_row_q5_0_impl(src, (block_q5_0*)qrow, n_per_row, quant_weights);
  2706. src += n_per_row;
  2707. qrow += row_size;
  2708. }
  2709. return nrow * row_size;
  2710. }
  2711. static void quantize_row_q5_1_impl(const float * restrict x, block_q5_1 * restrict y, int n_per_row, const float * quant_weights) {
  2712. static_assert(QK5_1 == 32, "QK5_1 must be 32");
  2713. if (!quant_weights) {
  2714. quantize_row_q5_1_reference(x, y, n_per_row);
  2715. return;
  2716. }
  2717. float weight[QK5_1];
  2718. uint8_t L[QK5_1], Laux[QK5_1];
  2719. float sum_x2 = 0;
  2720. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2721. float sigma2 = sum_x2/n_per_row;
  2722. const int nb = n_per_row/QK5_1;
  2723. for (int ib = 0; ib < nb; ++ib) {
  2724. const float * xb = x + QK5_1 * ib;
  2725. const float * qw = quant_weights + QK5_1 * ib;
  2726. for (int j = 0; j < QK5_1; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2727. float min;
  2728. float d = make_qkx3_quants(QK5_1, 31, xb, weight, L, &min, Laux, -0.9f, 0.05f, 36, false);
  2729. y[ib].d = GGML_FP32_TO_FP16(d);
  2730. y[ib].m = GGML_FP32_TO_FP16(-min);
  2731. uint32_t qh = 0;
  2732. for (int j = 0; j < 16; ++j) {
  2733. const uint8_t xi0 = L[j];
  2734. const uint8_t xi1 = L[j+16];
  2735. y[ib].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  2736. // get the 5-th bit and store it in qh at the right position
  2737. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  2738. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_0/2);
  2739. }
  2740. memcpy(&y[ib].qh, &qh, sizeof(qh));
  2741. }
  2742. }
  2743. size_t quantize_q5_1(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  2744. if (!quant_weights) {
  2745. return ggml_quantize_q5_1(src, dst, nrow*n_per_row, n_per_row, hist);
  2746. }
  2747. size_t row_size = ggml_row_size(GGML_TYPE_Q5_1, n_per_row);
  2748. char * qrow = (char *)dst;
  2749. for (int row = 0; row < nrow; ++row) {
  2750. quantize_row_q5_1_impl(src, (block_q5_1*)qrow, n_per_row, quant_weights);
  2751. src += n_per_row;
  2752. qrow += row_size;
  2753. }
  2754. return nrow * row_size;
  2755. }
  2756. // ====================== "True" 2-bit (de)-quantization
  2757. static const uint64_t iq2xxs_grid[256] = {
  2758. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  2759. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x08080808082b0808,
  2760. 0x08080808082b082b, 0x08080808082b2b08, 0x08080808082b2b2b, 0x0808080819080819,
  2761. 0x0808080819081908, 0x0808080819190808, 0x0808080819192b08, 0x08080808192b0819,
  2762. 0x08080808192b1908, 0x080808082b080808, 0x080808082b08082b, 0x080808082b082b2b,
  2763. 0x080808082b2b082b, 0x0808081908080819, 0x0808081908081908, 0x0808081908190808,
  2764. 0x0808081908191919, 0x0808081919080808, 0x080808192b081908, 0x080808192b192b08,
  2765. 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b082b082b, 0x0808082b2b08082b,
  2766. 0x0808190808080819, 0x0808190808081908, 0x0808190808190808, 0x08081908082b0819,
  2767. 0x08081908082b1908, 0x0808190819080808, 0x080819081908082b, 0x0808190819082b08,
  2768. 0x08081908192b0808, 0x080819082b080819, 0x080819082b081908, 0x080819082b190808,
  2769. 0x080819082b2b1908, 0x0808191908080808, 0x080819190808082b, 0x0808191908082b08,
  2770. 0x08081919082b0808, 0x080819191908192b, 0x08081919192b2b19, 0x080819192b080808,
  2771. 0x080819192b190819, 0x0808192b08082b19, 0x0808192b08190808, 0x0808192b19080808,
  2772. 0x0808192b2b081908, 0x0808192b2b2b1908, 0x08082b0808080808, 0x08082b0808081919,
  2773. 0x08082b0808082b08, 0x08082b0808191908, 0x08082b08082b2b08, 0x08082b0819080819,
  2774. 0x08082b0819081908, 0x08082b0819190808, 0x08082b081919082b, 0x08082b082b082b08,
  2775. 0x08082b1908081908, 0x08082b1919080808, 0x08082b2b0808082b, 0x08082b2b08191908,
  2776. 0x0819080808080819, 0x0819080808081908, 0x0819080808190808, 0x08190808082b0819,
  2777. 0x0819080819080808, 0x08190808192b0808, 0x081908082b081908, 0x081908082b190808,
  2778. 0x081908082b191919, 0x0819081908080808, 0x0819081908082b08, 0x08190819082b0808,
  2779. 0x0819081919190808, 0x0819081919192b2b, 0x081908192b080808, 0x0819082b082b1908,
  2780. 0x0819082b19081919, 0x0819190808080808, 0x0819190808082b08, 0x08191908082b0808,
  2781. 0x08191908082b1919, 0x0819190819082b19, 0x081919082b080808, 0x0819191908192b08,
  2782. 0x08191919192b082b, 0x0819192b08080808, 0x0819192b0819192b, 0x08192b0808080819,
  2783. 0x08192b0808081908, 0x08192b0808190808, 0x08192b0819080808, 0x08192b082b080819,
  2784. 0x08192b1908080808, 0x08192b1908081919, 0x08192b192b2b0808, 0x08192b2b19190819,
  2785. 0x082b080808080808, 0x082b08080808082b, 0x082b080808082b2b, 0x082b080819081908,
  2786. 0x082b0808192b0819, 0x082b08082b080808, 0x082b08082b08082b, 0x082b0819082b2b19,
  2787. 0x082b081919082b08, 0x082b082b08080808, 0x082b082b0808082b, 0x082b190808080819,
  2788. 0x082b190808081908, 0x082b190808190808, 0x082b190819080808, 0x082b19081919192b,
  2789. 0x082b191908080808, 0x082b191919080819, 0x082b1919192b1908, 0x082b192b2b190808,
  2790. 0x082b2b0808082b08, 0x082b2b08082b0808, 0x082b2b082b191908, 0x082b2b2b19081908,
  2791. 0x1908080808080819, 0x1908080808081908, 0x1908080808190808, 0x1908080808192b08,
  2792. 0x19080808082b0819, 0x19080808082b1908, 0x1908080819080808, 0x1908080819082b08,
  2793. 0x190808081919192b, 0x19080808192b0808, 0x190808082b080819, 0x190808082b081908,
  2794. 0x190808082b190808, 0x1908081908080808, 0x19080819082b0808, 0x19080819192b0819,
  2795. 0x190808192b080808, 0x190808192b081919, 0x1908082b08080819, 0x1908082b08190808,
  2796. 0x1908082b19082b08, 0x1908082b1919192b, 0x1908082b192b2b08, 0x1908190808080808,
  2797. 0x1908190808082b08, 0x19081908082b0808, 0x190819082b080808, 0x190819082b192b19,
  2798. 0x190819190819082b, 0x19081919082b1908, 0x1908192b08080808, 0x19082b0808080819,
  2799. 0x19082b0808081908, 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919,
  2800. 0x19082b1908080808, 0x19082b1919192b08, 0x19082b19192b0819, 0x19082b192b08082b,
  2801. 0x19082b2b19081919, 0x19082b2b2b190808, 0x1919080808080808, 0x1919080808082b08,
  2802. 0x1919080808190819, 0x1919080808192b19, 0x19190808082b0808, 0x191908082b080808,
  2803. 0x191908082b082b08, 0x1919081908081908, 0x191908191908082b, 0x191908192b2b1908,
  2804. 0x1919082b2b190819, 0x191919082b190808, 0x191919082b19082b, 0x1919191908082b2b,
  2805. 0x1919192b08080819, 0x1919192b19191908, 0x19192b0808080808, 0x19192b0808190819,
  2806. 0x19192b0808192b19, 0x19192b08192b1908, 0x19192b1919080808, 0x19192b2b08082b08,
  2807. 0x192b080808081908, 0x192b080808190808, 0x192b080819080808, 0x192b0808192b2b08,
  2808. 0x192b081908080808, 0x192b081919191919, 0x192b082b08192b08, 0x192b082b192b0808,
  2809. 0x192b190808080808, 0x192b190808081919, 0x192b191908190808, 0x192b19190819082b,
  2810. 0x192b19192b081908, 0x192b2b081908082b, 0x2b08080808080808, 0x2b0808080808082b,
  2811. 0x2b08080808082b2b, 0x2b08080819080819, 0x2b0808082b08082b, 0x2b08081908081908,
  2812. 0x2b08081908192b08, 0x2b08081919080808, 0x2b08082b08190819, 0x2b08190808080819,
  2813. 0x2b08190808081908, 0x2b08190808190808, 0x2b08190808191919, 0x2b08190819080808,
  2814. 0x2b081908192b0808, 0x2b08191908080808, 0x2b0819191908192b, 0x2b0819192b191908,
  2815. 0x2b08192b08082b19, 0x2b08192b19080808, 0x2b08192b192b0808, 0x2b082b080808082b,
  2816. 0x2b082b1908081908, 0x2b082b2b08190819, 0x2b19080808081908, 0x2b19080808190808,
  2817. 0x2b190808082b1908, 0x2b19080819080808, 0x2b1908082b2b0819, 0x2b1908190819192b,
  2818. 0x2b1908192b080808, 0x2b19082b19081919, 0x2b19190808080808, 0x2b191908082b082b,
  2819. 0x2b19190819081908, 0x2b19191919190819, 0x2b192b082b080819, 0x2b192b19082b0808,
  2820. 0x2b2b08080808082b, 0x2b2b080819190808, 0x2b2b08082b081919, 0x2b2b081908082b19,
  2821. 0x2b2b082b08080808, 0x2b2b190808192b08, 0x2b2b2b0819190808, 0x2b2b2b1908081908,
  2822. };
  2823. static const uint64_t iq2xs_grid[512] = {
  2824. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  2825. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x080808080819192b,
  2826. 0x0808080808192b19, 0x08080808082b0808, 0x08080808082b082b, 0x08080808082b1919,
  2827. 0x08080808082b2b08, 0x0808080819080819, 0x0808080819081908, 0x080808081908192b,
  2828. 0x0808080819082b19, 0x0808080819190808, 0x080808081919082b, 0x0808080819191919,
  2829. 0x0808080819192b08, 0x08080808192b0819, 0x08080808192b1908, 0x080808082b080808,
  2830. 0x080808082b08082b, 0x080808082b081919, 0x080808082b082b08, 0x080808082b190819,
  2831. 0x080808082b191908, 0x080808082b192b19, 0x080808082b2b0808, 0x0808081908080819,
  2832. 0x0808081908081908, 0x080808190808192b, 0x0808081908082b19, 0x0808081908190808,
  2833. 0x080808190819082b, 0x0808081908191919, 0x0808081908192b08, 0x0808081908192b2b,
  2834. 0x08080819082b0819, 0x08080819082b1908, 0x0808081919080808, 0x080808191908082b,
  2835. 0x0808081919081919, 0x0808081919082b08, 0x0808081919190819, 0x0808081919191908,
  2836. 0x08080819192b0808, 0x08080819192b2b08, 0x080808192b080819, 0x080808192b081908,
  2837. 0x080808192b190808, 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b08081919,
  2838. 0x0808082b08082b08, 0x0808082b08190819, 0x0808082b08191908, 0x0808082b082b0808,
  2839. 0x0808082b19080819, 0x0808082b19081908, 0x0808082b19190808, 0x0808082b19191919,
  2840. 0x0808082b2b080808, 0x0808082b2b082b2b, 0x0808190808080819, 0x0808190808081908,
  2841. 0x080819080808192b, 0x0808190808082b19, 0x0808190808190808, 0x080819080819082b,
  2842. 0x0808190808191919, 0x0808190808192b08, 0x08081908082b0819, 0x08081908082b1908,
  2843. 0x0808190819080808, 0x080819081908082b, 0x0808190819081919, 0x0808190819082b08,
  2844. 0x0808190819190819, 0x0808190819191908, 0x080819081919192b, 0x08081908192b0808,
  2845. 0x080819082b080819, 0x080819082b081908, 0x080819082b190808, 0x0808191908080808,
  2846. 0x080819190808082b, 0x0808191908081919, 0x0808191908082b08, 0x0808191908190819,
  2847. 0x0808191908191908, 0x08081919082b0808, 0x0808191919080819, 0x0808191919081908,
  2848. 0x0808191919190808, 0x08081919192b0819, 0x080819192b080808, 0x0808192b08080819,
  2849. 0x0808192b08081908, 0x0808192b08190808, 0x0808192b082b192b, 0x0808192b19080808,
  2850. 0x0808192b1908082b, 0x0808192b2b081908, 0x08082b0808080808, 0x08082b080808082b,
  2851. 0x08082b0808081919, 0x08082b0808082b08, 0x08082b0808082b2b, 0x08082b0808190819,
  2852. 0x08082b0808191908, 0x08082b08082b0808, 0x08082b08082b1919, 0x08082b0819080819,
  2853. 0x08082b0819081908, 0x08082b0819190808, 0x08082b0819192b08, 0x08082b082b080808,
  2854. 0x08082b082b2b0808, 0x08082b082b2b2b2b, 0x08082b1908080819, 0x08082b1908081908,
  2855. 0x08082b1908190808, 0x08082b1919080808, 0x08082b192b080819, 0x08082b192b082b19,
  2856. 0x08082b2b08080808, 0x08082b2b082b0808, 0x08082b2b082b2b08, 0x08082b2b2b19192b,
  2857. 0x08082b2b2b2b0808, 0x0819080808080819, 0x0819080808081908, 0x081908080808192b,
  2858. 0x0819080808082b19, 0x0819080808190808, 0x081908080819082b, 0x0819080808191919,
  2859. 0x0819080808192b08, 0x08190808082b0819, 0x08190808082b1908, 0x0819080819080808,
  2860. 0x081908081908082b, 0x0819080819081919, 0x0819080819082b08, 0x0819080819190819,
  2861. 0x0819080819191908, 0x08190808192b0808, 0x08190808192b2b2b, 0x081908082b080819,
  2862. 0x081908082b081908, 0x081908082b190808, 0x0819081908080808, 0x081908190808082b,
  2863. 0x0819081908081919, 0x0819081908082b08, 0x0819081908190819, 0x0819081908191908,
  2864. 0x08190819082b0808, 0x0819081919080819, 0x0819081919081908, 0x0819081919190808,
  2865. 0x081908192b080808, 0x081908192b191908, 0x081908192b19192b, 0x0819082b08080819,
  2866. 0x0819082b08081908, 0x0819082b0808192b, 0x0819082b08190808, 0x0819082b19080808,
  2867. 0x0819082b192b0808, 0x0819190808080808, 0x081919080808082b, 0x0819190808081919,
  2868. 0x0819190808082b08, 0x0819190808190819, 0x0819190808191908, 0x08191908082b0808,
  2869. 0x0819190819080819, 0x0819190819081908, 0x0819190819082b19, 0x0819190819190808,
  2870. 0x08191908192b1908, 0x081919082b080808, 0x0819191908080819, 0x0819191908081908,
  2871. 0x0819191908190808, 0x0819191919080808, 0x0819192b08080808, 0x0819192b08191908,
  2872. 0x0819192b19082b19, 0x08192b0808080819, 0x08192b0808081908, 0x08192b0808190808,
  2873. 0x08192b080819082b, 0x08192b0819080808, 0x08192b0819191908, 0x08192b082b08192b,
  2874. 0x08192b1908080808, 0x08192b1908081919, 0x08192b19192b192b, 0x08192b2b19190819,
  2875. 0x08192b2b2b2b2b19, 0x082b080808080808, 0x082b08080808082b, 0x082b080808081919,
  2876. 0x082b080808082b08, 0x082b080808082b2b, 0x082b080808190819, 0x082b080808191908,
  2877. 0x082b0808082b0808, 0x082b080819080819, 0x082b080819081908, 0x082b080819190808,
  2878. 0x082b08082b080808, 0x082b08082b2b0808, 0x082b081908080819, 0x082b081908081908,
  2879. 0x082b081908190808, 0x082b081919080808, 0x082b081919082b08, 0x082b0819192b1919,
  2880. 0x082b082b08080808, 0x082b082b082b082b, 0x082b082b2b080808, 0x082b082b2b2b2b08,
  2881. 0x082b190808080819, 0x082b190808081908, 0x082b190808190808, 0x082b1908082b2b19,
  2882. 0x082b190819080808, 0x082b191908080808, 0x082b191919080819, 0x082b19191919082b,
  2883. 0x082b19192b192b19, 0x082b192b08080819, 0x082b192b08192b2b, 0x082b192b2b2b192b,
  2884. 0x082b2b0808080808, 0x082b2b0808082b08, 0x082b2b0808082b2b, 0x082b2b08082b0808,
  2885. 0x082b2b0819191919, 0x082b2b082b082b08, 0x082b2b082b2b082b, 0x082b2b19192b2b08,
  2886. 0x082b2b192b190808, 0x082b2b2b08082b08, 0x082b2b2b082b0808, 0x082b2b2b2b08082b,
  2887. 0x082b2b2b2b082b08, 0x082b2b2b2b082b2b, 0x1908080808080819, 0x1908080808081908,
  2888. 0x190808080808192b, 0x1908080808082b19, 0x1908080808190808, 0x190808080819082b,
  2889. 0x1908080808191919, 0x1908080808192b08, 0x19080808082b0819, 0x19080808082b1908,
  2890. 0x1908080819080808, 0x190808081908082b, 0x1908080819081919, 0x1908080819082b08,
  2891. 0x1908080819082b2b, 0x1908080819190819, 0x1908080819191908, 0x19080808192b0808,
  2892. 0x19080808192b1919, 0x190808082b080819, 0x190808082b081908, 0x190808082b190808,
  2893. 0x1908081908080808, 0x190808190808082b, 0x1908081908081919, 0x1908081908082b08,
  2894. 0x1908081908190819, 0x1908081908191908, 0x19080819082b0808, 0x1908081919080819,
  2895. 0x1908081919081908, 0x1908081919190808, 0x190808192b080808, 0x190808192b081919,
  2896. 0x190808192b2b082b, 0x1908082b08080819, 0x1908082b08081908, 0x1908082b08190808,
  2897. 0x1908082b0819082b, 0x1908082b082b2b19, 0x1908082b19080808, 0x1908190808080808,
  2898. 0x190819080808082b, 0x1908190808081919, 0x1908190808082b08, 0x1908190808190819,
  2899. 0x1908190808191908, 0x1908190808192b19, 0x19081908082b0808, 0x1908190819080819,
  2900. 0x1908190819081908, 0x1908190819190808, 0x190819082b080808, 0x190819082b191908,
  2901. 0x1908191908080819, 0x1908191908081908, 0x1908191908190808, 0x19081919082b1908,
  2902. 0x1908191919080808, 0x190819192b192b2b, 0x1908192b08080808, 0x1908192b08082b2b,
  2903. 0x1908192b19081908, 0x1908192b19190808, 0x19082b0808080819, 0x19082b0808081908,
  2904. 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919, 0x19082b0819191908,
  2905. 0x19082b08192b082b, 0x19082b1908080808, 0x19082b1908190819, 0x19082b1919081908,
  2906. 0x19082b1919190808, 0x19082b19192b2b19, 0x19082b2b08081908, 0x1919080808080808,
  2907. 0x191908080808082b, 0x1919080808081919, 0x1919080808082b08, 0x1919080808190819,
  2908. 0x1919080808191908, 0x19190808082b0808, 0x19190808082b2b08, 0x1919080819080819,
  2909. 0x1919080819081908, 0x1919080819190808, 0x191908082b080808, 0x1919081908080819,
  2910. 0x1919081908081908, 0x1919081908190808, 0x1919081908191919, 0x1919081919080808,
  2911. 0x191908191908082b, 0x1919082b08080808, 0x1919082b19081908, 0x1919082b2b2b2b2b,
  2912. 0x1919190808080819, 0x1919190808081908, 0x1919190808190808, 0x19191908082b0819,
  2913. 0x1919190819080808, 0x19191908192b0808, 0x191919082b080819, 0x191919082b2b0819,
  2914. 0x1919191908080808, 0x1919191908082b08, 0x191919192b080808, 0x191919192b082b08,
  2915. 0x1919192b082b0819, 0x1919192b192b2b08, 0x1919192b2b2b0819, 0x19192b0808080808,
  2916. 0x19192b0808191908, 0x19192b0819080819, 0x19192b0819190808, 0x19192b082b192b19,
  2917. 0x19192b1908192b2b, 0x19192b1919080808, 0x19192b191908082b, 0x19192b2b2b081919,
  2918. 0x192b080808080819, 0x192b080808081908, 0x192b080808190808, 0x192b080819080808,
  2919. 0x192b080819191908, 0x192b0808192b082b, 0x192b08082b08192b, 0x192b08082b2b2b19,
  2920. 0x192b081908080808, 0x192b082b082b1908, 0x192b082b19082b2b, 0x192b082b2b19082b,
  2921. 0x192b190808080808, 0x192b19080819192b, 0x192b191908190808, 0x192b191919080808,
  2922. 0x192b191919081919, 0x192b19192b2b1908, 0x192b2b0808080819, 0x192b2b08192b2b2b,
  2923. 0x192b2b19082b1919, 0x192b2b2b0808192b, 0x192b2b2b19191908, 0x192b2b2b192b082b,
  2924. 0x2b08080808080808, 0x2b0808080808082b, 0x2b08080808081919, 0x2b08080808082b08,
  2925. 0x2b08080808190819, 0x2b08080808191908, 0x2b080808082b0808, 0x2b080808082b2b2b,
  2926. 0x2b08080819080819, 0x2b08080819081908, 0x2b08080819190808, 0x2b0808082b080808,
  2927. 0x2b0808082b08082b, 0x2b0808082b2b2b08, 0x2b0808082b2b2b2b, 0x2b08081908080819,
  2928. 0x2b08081908081908, 0x2b0808190808192b, 0x2b08081908190808, 0x2b08081919080808,
  2929. 0x2b08081919190819, 0x2b08081919192b19, 0x2b08082b08080808, 0x2b08082b082b0808,
  2930. 0x2b08082b2b080808, 0x2b08082b2b08082b, 0x2b08082b2b2b0808, 0x2b08082b2b2b2b08,
  2931. 0x2b08190808080819, 0x2b08190808081908, 0x2b08190808190808, 0x2b0819080819082b,
  2932. 0x2b08190808191919, 0x2b08190819080808, 0x2b081908192b0808, 0x2b0819082b082b19,
  2933. 0x2b08191908080808, 0x2b08191919081908, 0x2b0819192b2b1919, 0x2b08192b08192b08,
  2934. 0x2b08192b192b2b2b, 0x2b082b0808080808, 0x2b082b0808082b08, 0x2b082b08082b1919,
  2935. 0x2b082b0819192b2b, 0x2b082b082b080808, 0x2b082b082b08082b, 0x2b082b082b2b2b08,
  2936. 0x2b082b190808192b, 0x2b082b2b082b082b, 0x2b082b2b2b080808, 0x2b082b2b2b082b08,
  2937. 0x2b082b2b2b19192b, 0x2b082b2b2b2b2b08, 0x2b19080808080819, 0x2b19080808081908,
  2938. 0x2b19080808190808, 0x2b19080819080808, 0x2b1908081919192b, 0x2b1908082b081908,
  2939. 0x2b19081908080808, 0x2b190819082b082b, 0x2b190819192b1908, 0x2b19082b1919192b,
  2940. 0x2b19082b2b082b19, 0x2b19190808080808, 0x2b19190808081919, 0x2b19190819081908,
  2941. 0x2b19190819190808, 0x2b19190819192b08, 0x2b191919082b2b19, 0x2b1919192b190808,
  2942. 0x2b1919192b19082b, 0x2b19192b19080819, 0x2b192b0819190819, 0x2b192b082b2b192b,
  2943. 0x2b192b1919082b19, 0x2b192b2b08191919, 0x2b192b2b192b0808, 0x2b2b080808080808,
  2944. 0x2b2b08080808082b, 0x2b2b080808082b08, 0x2b2b080808082b2b, 0x2b2b0808082b0808,
  2945. 0x2b2b0808082b2b2b, 0x2b2b08082b2b0808, 0x2b2b081919190819, 0x2b2b081919192b19,
  2946. 0x2b2b08192b2b192b, 0x2b2b082b08080808, 0x2b2b082b0808082b, 0x2b2b082b08082b08,
  2947. 0x2b2b082b082b2b2b, 0x2b2b082b2b080808, 0x2b2b082b2b2b0808, 0x2b2b190819080808,
  2948. 0x2b2b19082b191919, 0x2b2b192b192b1919, 0x2b2b192b2b192b08, 0x2b2b2b0808082b2b,
  2949. 0x2b2b2b08082b0808, 0x2b2b2b08082b082b, 0x2b2b2b08082b2b08, 0x2b2b2b082b2b0808,
  2950. 0x2b2b2b082b2b2b08, 0x2b2b2b1908081908, 0x2b2b2b192b081908, 0x2b2b2b192b08192b,
  2951. 0x2b2b2b2b082b2b08, 0x2b2b2b2b082b2b2b, 0x2b2b2b2b2b190819, 0x2b2b2b2b2b2b2b2b,
  2952. };
  2953. static const uint32_t iq3xxs_grid[256] = {
  2954. 0x04040404, 0x04040414, 0x04040424, 0x04040c0c, 0x04040c1c, 0x04040c3e, 0x04041404, 0x04041414,
  2955. 0x04041c0c, 0x04042414, 0x04043e1c, 0x04043e2c, 0x040c040c, 0x040c041c, 0x040c0c04, 0x040c0c14,
  2956. 0x040c140c, 0x040c142c, 0x040c1c04, 0x040c1c14, 0x040c240c, 0x040c2c24, 0x040c3e04, 0x04140404,
  2957. 0x04140414, 0x04140424, 0x04140c0c, 0x04141404, 0x04141414, 0x04141c0c, 0x04141c1c, 0x04141c3e,
  2958. 0x04142c0c, 0x04142c3e, 0x04143e2c, 0x041c040c, 0x041c043e, 0x041c0c04, 0x041c0c14, 0x041c142c,
  2959. 0x041c3e04, 0x04240c1c, 0x04241c3e, 0x04242424, 0x04242c3e, 0x04243e1c, 0x04243e2c, 0x042c040c,
  2960. 0x042c043e, 0x042c1c14, 0x042c2c14, 0x04341c2c, 0x04343424, 0x043e0c04, 0x043e0c24, 0x043e0c34,
  2961. 0x043e241c, 0x043e340c, 0x0c04040c, 0x0c04041c, 0x0c040c04, 0x0c040c14, 0x0c04140c, 0x0c04141c,
  2962. 0x0c041c04, 0x0c041c14, 0x0c041c24, 0x0c04243e, 0x0c042c04, 0x0c0c0404, 0x0c0c0414, 0x0c0c0c0c,
  2963. 0x0c0c1404, 0x0c0c1414, 0x0c14040c, 0x0c14041c, 0x0c140c04, 0x0c140c14, 0x0c14140c, 0x0c141c04,
  2964. 0x0c143e14, 0x0c1c0404, 0x0c1c0414, 0x0c1c1404, 0x0c1c1c0c, 0x0c1c2434, 0x0c1c3434, 0x0c24040c,
  2965. 0x0c24042c, 0x0c242c04, 0x0c2c1404, 0x0c2c1424, 0x0c2c2434, 0x0c2c3e0c, 0x0c34042c, 0x0c3e1414,
  2966. 0x0c3e2404, 0x14040404, 0x14040414, 0x14040c0c, 0x14040c1c, 0x14041404, 0x14041414, 0x14041434,
  2967. 0x14041c0c, 0x14042414, 0x140c040c, 0x140c041c, 0x140c042c, 0x140c0c04, 0x140c0c14, 0x140c140c,
  2968. 0x140c1c04, 0x140c341c, 0x140c343e, 0x140c3e04, 0x14140404, 0x14140414, 0x14140c0c, 0x14140c3e,
  2969. 0x14141404, 0x14141414, 0x14141c3e, 0x14142404, 0x14142c2c, 0x141c040c, 0x141c0c04, 0x141c0c24,
  2970. 0x141c3e04, 0x141c3e24, 0x14241c2c, 0x14242c1c, 0x142c041c, 0x142c143e, 0x142c240c, 0x142c3e24,
  2971. 0x143e040c, 0x143e041c, 0x143e0c34, 0x143e242c, 0x1c04040c, 0x1c040c04, 0x1c040c14, 0x1c04140c,
  2972. 0x1c04141c, 0x1c042c04, 0x1c04342c, 0x1c043e14, 0x1c0c0404, 0x1c0c0414, 0x1c0c1404, 0x1c0c1c0c,
  2973. 0x1c0c2424, 0x1c0c2434, 0x1c14040c, 0x1c14041c, 0x1c140c04, 0x1c14142c, 0x1c142c14, 0x1c143e14,
  2974. 0x1c1c0c0c, 0x1c1c1c1c, 0x1c241c04, 0x1c24243e, 0x1c243e14, 0x1c2c0404, 0x1c2c0434, 0x1c2c1414,
  2975. 0x1c2c2c2c, 0x1c340c24, 0x1c341c34, 0x1c34341c, 0x1c3e1c1c, 0x1c3e3404, 0x24040424, 0x24040c3e,
  2976. 0x24041c2c, 0x24041c3e, 0x24042c1c, 0x24042c3e, 0x240c3e24, 0x24141404, 0x24141c3e, 0x24142404,
  2977. 0x24143404, 0x24143434, 0x241c043e, 0x241c242c, 0x24240424, 0x24242c0c, 0x24243424, 0x242c142c,
  2978. 0x242c241c, 0x242c3e04, 0x243e042c, 0x243e0c04, 0x243e0c14, 0x243e1c04, 0x2c040c14, 0x2c04240c,
  2979. 0x2c043e04, 0x2c0c0404, 0x2c0c0434, 0x2c0c1434, 0x2c0c2c2c, 0x2c140c24, 0x2c141c14, 0x2c143e14,
  2980. 0x2c1c0414, 0x2c1c2c1c, 0x2c240c04, 0x2c24141c, 0x2c24143e, 0x2c243e14, 0x2c2c0414, 0x2c2c1c0c,
  2981. 0x2c342c04, 0x2c3e1424, 0x2c3e2414, 0x34041424, 0x34042424, 0x34042434, 0x34043424, 0x340c140c,
  2982. 0x340c340c, 0x34140c3e, 0x34143424, 0x341c1c04, 0x341c1c34, 0x34242424, 0x342c042c, 0x342c2c14,
  2983. 0x34341c1c, 0x343e041c, 0x343e140c, 0x3e04041c, 0x3e04042c, 0x3e04043e, 0x3e040c04, 0x3e041c14,
  2984. 0x3e042c14, 0x3e0c1434, 0x3e0c2404, 0x3e140c14, 0x3e14242c, 0x3e142c14, 0x3e1c0404, 0x3e1c0c2c,
  2985. 0x3e1c1c1c, 0x3e1c3404, 0x3e24140c, 0x3e24240c, 0x3e2c0404, 0x3e2c0414, 0x3e2c1424, 0x3e341c04,
  2986. };
  2987. static const uint8_t ksigns_iq2xs[128] = {
  2988. 0, 129, 130, 3, 132, 5, 6, 135, 136, 9, 10, 139, 12, 141, 142, 15,
  2989. 144, 17, 18, 147, 20, 149, 150, 23, 24, 153, 154, 27, 156, 29, 30, 159,
  2990. 160, 33, 34, 163, 36, 165, 166, 39, 40, 169, 170, 43, 172, 45, 46, 175,
  2991. 48, 177, 178, 51, 180, 53, 54, 183, 184, 57, 58, 187, 60, 189, 190, 63,
  2992. 192, 65, 66, 195, 68, 197, 198, 71, 72, 201, 202, 75, 204, 77, 78, 207,
  2993. 80, 209, 210, 83, 212, 85, 86, 215, 216, 89, 90, 219, 92, 221, 222, 95,
  2994. 96, 225, 226, 99, 228, 101, 102, 231, 232, 105, 106, 235, 108, 237, 238, 111,
  2995. 240, 113, 114, 243, 116, 245, 246, 119, 120, 249, 250, 123, 252, 125, 126, 255,
  2996. };
  2997. static const uint8_t kmask_iq2xs[8] = {1, 2, 4, 8, 16, 32, 64, 128};
  2998. void dequantize_row_iq2_xxs(const block_iq2_xxs * restrict x, float * restrict y, int k) {
  2999. assert(k % QK_K == 0);
  3000. const int nb = k / QK_K;
  3001. uint32_t aux32[2];
  3002. const uint8_t * aux8 = (const uint8_t *)aux32;
  3003. for (int i = 0; i < nb; i++) {
  3004. const float d = GGML_FP16_TO_FP32(x[i].d);
  3005. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  3006. memcpy(aux32, x[i].qs + 4*ib32, 2*sizeof(uint32_t));
  3007. const float db = d * (0.5f + (aux32[1] >> 28)) * 0.25f;
  3008. for (int l = 0; l < 4; ++l) {
  3009. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  3010. const uint8_t signs = ksigns_iq2xs[(aux32[1] >> 7*l) & 127];
  3011. for (int j = 0; j < 8; ++j) {
  3012. y[j] = db * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  3013. }
  3014. y += 8;
  3015. }
  3016. }
  3017. }
  3018. }
  3019. // ====================== 2.3125 bpw (de)-quantization
  3020. void dequantize_row_iq2_xs(const block_iq2_xs * restrict x, float * restrict y, int k) {
  3021. assert(k % QK_K == 0);
  3022. const int nb = k / QK_K;
  3023. float db[2];
  3024. for (int i = 0; i < nb; i++) {
  3025. const float d = GGML_FP16_TO_FP32(x[i].d);
  3026. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  3027. db[0] = d * (0.5f + (x[i].scales[ib32] & 0xf)) * 0.25f;
  3028. db[1] = d * (0.5f + (x[i].scales[ib32] >> 4)) * 0.25f;
  3029. for (int l = 0; l < 4; ++l) {
  3030. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (x[i].qs[4*ib32 + l] & 511));
  3031. const uint8_t signs = ksigns_iq2xs[x[i].qs[4*ib32 + l] >> 9];
  3032. for (int j = 0; j < 8; ++j) {
  3033. y[j] = db[l/2] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  3034. }
  3035. y += 8;
  3036. }
  3037. }
  3038. }
  3039. }
  3040. // ====================== 3.0625 bpw (de)-quantization
  3041. void dequantize_row_iq3_xxs(const block_iq3_xxs * restrict x, float * restrict y, int k) {
  3042. assert(k % QK_K == 0);
  3043. const int nb = k / QK_K;
  3044. uint32_t aux32;
  3045. for (int i = 0; i < nb; i++) {
  3046. const float d = GGML_FP16_TO_FP32(x[i].d);
  3047. const uint8_t * qs = x[i].qs;
  3048. const uint8_t * scales_and_signs = qs + QK_K/4;
  3049. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  3050. memcpy(&aux32, scales_and_signs + 4*ib32, sizeof(uint32_t));
  3051. const float db = d * (0.5f + (aux32 >> 28)) * 0.5f;
  3052. for (int l = 0; l < 4; ++l) {
  3053. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*l) & 127];
  3054. const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + qs[2*l+0]);
  3055. const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + qs[2*l+1]);
  3056. for (int j = 0; j < 4; ++j) {
  3057. y[j+0] = db * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  3058. y[j+4] = db * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  3059. }
  3060. y += 8;
  3061. }
  3062. qs += 8;
  3063. }
  3064. }
  3065. }
  3066. //===================================== Q8_K ==============================================
  3067. void quantize_row_q8_K_reference(const float * restrict x, block_q8_K * restrict y, int k) {
  3068. assert(k % QK_K == 0);
  3069. const int nb = k / QK_K;
  3070. for (int i = 0; i < nb; i++) {
  3071. float max = 0;
  3072. float amax = 0;
  3073. for (int j = 0; j < QK_K; ++j) {
  3074. float ax = fabsf(x[j]);
  3075. if (ax > amax) {
  3076. amax = ax; max = x[j];
  3077. }
  3078. }
  3079. if (!amax) {
  3080. y[i].d = 0;
  3081. memset(y[i].qs, 0, QK_K);
  3082. x += QK_K;
  3083. continue;
  3084. }
  3085. //const float iscale = -128.f/max;
  3086. // We need this change for IQ2_XXS, else the AVX implementation becomes very awkward
  3087. const float iscale = -127.f/max;
  3088. for (int j = 0; j < QK_K; ++j) {
  3089. int v = nearest_int(iscale*x[j]);
  3090. y[i].qs[j] = MIN(127, v);
  3091. }
  3092. for (int j = 0; j < QK_K/16; ++j) {
  3093. int sum = 0;
  3094. for (int ii = 0; ii < 16; ++ii) {
  3095. sum += y[i].qs[j*16 + ii];
  3096. }
  3097. y[i].bsums[j] = sum;
  3098. }
  3099. y[i].d = 1/iscale;
  3100. x += QK_K;
  3101. }
  3102. }
  3103. void dequantize_row_q8_K(const block_q8_K * restrict x, float * restrict y, int k) {
  3104. assert(k % QK_K == 0);
  3105. const int nb = k / QK_K;
  3106. for (int i = 0; i < nb; i++) {
  3107. for (int j = 0; j < QK_K; ++j) {
  3108. *y++ = x[i].d * x[i].qs[j];
  3109. }
  3110. }
  3111. }
  3112. void quantize_row_q8_K(const float * restrict x, void * restrict y, int k) {
  3113. quantize_row_q8_K_reference(x, y, k);
  3114. }
  3115. //===================================== Dot ptoducts =================================
  3116. //
  3117. // Helper functions
  3118. //
  3119. #if __AVX__ || __AVX2__ || __AVX512F__
  3120. // shuffles to pick the required scales in dot products
  3121. static inline __m256i get_scale_shuffle_q3k(int i) {
  3122. static const uint8_t k_shuffle[128] = {
  3123. 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,
  3124. 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,
  3125. 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,
  3126. 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,
  3127. };
  3128. return _mm256_loadu_si256((const __m256i*)k_shuffle + i);
  3129. }
  3130. static inline __m256i get_scale_shuffle_k4(int i) {
  3131. static const uint8_t k_shuffle[256] = {
  3132. 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,
  3133. 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,
  3134. 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,
  3135. 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,
  3136. 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,
  3137. 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,
  3138. 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,
  3139. 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
  3140. };
  3141. return _mm256_loadu_si256((const __m256i*)k_shuffle + i);
  3142. }
  3143. static inline __m128i get_scale_shuffle(int i) {
  3144. static const uint8_t k_shuffle[128] = {
  3145. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
  3146. 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
  3147. 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
  3148. 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7,
  3149. 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,
  3150. 10,10,10,10,10,10,10,10, 11,11,11,11,11,11,11,11,
  3151. 12,12,12,12,12,12,12,12, 13,13,13,13,13,13,13,13,
  3152. 14,14,14,14,14,14,14,14, 15,15,15,15,15,15,15,15
  3153. };
  3154. return _mm_loadu_si128((const __m128i*)k_shuffle + i);
  3155. }
  3156. #endif
  3157. void ggml_vec_dot_q4_0_q8_0(int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3158. const int qk = QK8_0;
  3159. const int nb = n / qk;
  3160. assert(n % qk == 0);
  3161. const block_q4_0 * restrict x = vx;
  3162. const block_q8_0 * restrict y = vy;
  3163. #if defined(__ARM_NEON)
  3164. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3165. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3166. assert(nb % 2 == 0); // TODO: handle odd nb
  3167. for (int i = 0; i < nb; i += 2) {
  3168. const block_q4_0 * restrict x0 = &x[i + 0];
  3169. const block_q4_0 * restrict x1 = &x[i + 1];
  3170. const block_q8_0 * restrict y0 = &y[i + 0];
  3171. const block_q8_0 * restrict y1 = &y[i + 1];
  3172. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3173. const int8x16_t s8b = vdupq_n_s8(0x8);
  3174. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3175. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3176. // 4-bit -> 8-bit
  3177. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3178. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3179. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3180. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3181. // sub 8
  3182. const int8x16_t v0_0ls = vsubq_s8(v0_0l, s8b);
  3183. const int8x16_t v0_0hs = vsubq_s8(v0_0h, s8b);
  3184. const int8x16_t v0_1ls = vsubq_s8(v0_1l, s8b);
  3185. const int8x16_t v0_1hs = vsubq_s8(v0_1h, s8b);
  3186. // load y
  3187. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3188. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3189. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3190. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3191. // dot product into int32x4_t
  3192. const int32x4_t p_0 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_0ls, v1_0l), v0_0hs, v1_0h);
  3193. const int32x4_t p_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_1ls, v1_1l), v0_1hs, v1_1h);
  3194. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(p_0), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3195. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(p_1), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3196. }
  3197. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  3198. #elif defined(__AVX2__)
  3199. // Initialize accumulator with zeros
  3200. __m256 acc = _mm256_setzero_ps();
  3201. // Main loop
  3202. for (int i = 0; i < nb; ++i) {
  3203. /* Compute combined scale for the block */
  3204. const __m256 d = _mm256_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3205. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  3206. // Now we have a vector with bytes in [ 0 .. 15 ] interval. Offset them into [ -8 .. +7 ] interval.
  3207. const __m256i off = _mm256_set1_epi8( 8 );
  3208. bx = _mm256_sub_epi8( bx, off );
  3209. __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3210. const __m256 q = mul_sum_i8_pairs_float(bx, by);
  3211. /* Multiply q with scale and accumulate */
  3212. acc = _mm256_fmadd_ps( d, q, acc );
  3213. }
  3214. *s = hsum_float_8(acc);
  3215. #elif defined(__AVX__)
  3216. // Initialize accumulator with zeros
  3217. __m256 acc = _mm256_setzero_ps();
  3218. // Main loop
  3219. for (int i = 0; i < nb; ++i) {
  3220. // Compute combined scale for the block
  3221. const __m256 d = _mm256_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3222. const __m128i lowMask = _mm_set1_epi8(0xF);
  3223. const __m128i off = _mm_set1_epi8(8);
  3224. const __m128i tmp = _mm_loadu_si128((const __m128i *)x[i].qs);
  3225. __m128i bx = _mm_and_si128(lowMask, tmp);
  3226. __m128i by = _mm_loadu_si128((const __m128i *)y[i].qs);
  3227. bx = _mm_sub_epi8(bx, off);
  3228. const __m128i i32_0 = mul_sum_i8_pairs(bx, by);
  3229. bx = _mm_and_si128(lowMask, _mm_srli_epi64(tmp, 4));
  3230. by = _mm_loadu_si128((const __m128i *)(y[i].qs + 16));
  3231. bx = _mm_sub_epi8(bx, off);
  3232. const __m128i i32_1 = mul_sum_i8_pairs(bx, by);
  3233. // Convert int32_t to float
  3234. __m256 p = _mm256_cvtepi32_ps(MM256_SET_M128I(i32_0, i32_1));
  3235. // Apply the scale, and accumulate
  3236. acc = _mm256_add_ps(_mm256_mul_ps( d, p ), acc);
  3237. }
  3238. *s = hsum_float_8(acc);
  3239. #elif defined(__SSSE3__)
  3240. // set constants
  3241. const __m128i lowMask = _mm_set1_epi8(0xF);
  3242. const __m128i off = _mm_set1_epi8(8);
  3243. // Initialize accumulator with zeros
  3244. __m128 acc_0 = _mm_setzero_ps();
  3245. __m128 acc_1 = _mm_setzero_ps();
  3246. __m128 acc_2 = _mm_setzero_ps();
  3247. __m128 acc_3 = _mm_setzero_ps();
  3248. // First round without accumulation
  3249. {
  3250. _mm_prefetch(&x[0] + sizeof(block_q4_0), _MM_HINT_T0);
  3251. _mm_prefetch(&y[0] + sizeof(block_q8_0), _MM_HINT_T0);
  3252. // Compute combined scale for the block 0 and 1
  3253. const __m128 d_0_1 = _mm_set1_ps( GGML_FP16_TO_FP32(x[0].d) * GGML_FP16_TO_FP32(y[0].d) );
  3254. const __m128i tmp_0_1 = _mm_loadu_si128((const __m128i *)x[0].qs);
  3255. __m128i bx_0 = _mm_and_si128(lowMask, tmp_0_1);
  3256. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[0].qs);
  3257. bx_0 = _mm_sub_epi8(bx_0, off);
  3258. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3259. __m128i bx_1 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_0_1, 4));
  3260. __m128i by_1 = _mm_loadu_si128((const __m128i *)(y[0].qs + 16));
  3261. bx_1 = _mm_sub_epi8(bx_1, off);
  3262. const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
  3263. _mm_prefetch(&x[1] + sizeof(block_q4_0), _MM_HINT_T0);
  3264. _mm_prefetch(&y[1] + sizeof(block_q8_0), _MM_HINT_T0);
  3265. // Compute combined scale for the block 2 and 3
  3266. const __m128 d_2_3 = _mm_set1_ps( GGML_FP16_TO_FP32(x[1].d) * GGML_FP16_TO_FP32(y[1].d) );
  3267. const __m128i tmp_2_3 = _mm_loadu_si128((const __m128i *)x[1].qs);
  3268. __m128i bx_2 = _mm_and_si128(lowMask, tmp_2_3);
  3269. __m128i by_2 = _mm_loadu_si128((const __m128i *)y[1].qs);
  3270. bx_2 = _mm_sub_epi8(bx_2, off);
  3271. const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
  3272. __m128i bx_3 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_2_3, 4));
  3273. __m128i by_3 = _mm_loadu_si128((const __m128i *)(y[1].qs + 16));
  3274. bx_3 = _mm_sub_epi8(bx_3, off);
  3275. const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
  3276. // Convert int32_t to float
  3277. __m128 p0 = _mm_cvtepi32_ps(i32_0);
  3278. __m128 p1 = _mm_cvtepi32_ps(i32_1);
  3279. __m128 p2 = _mm_cvtepi32_ps(i32_2);
  3280. __m128 p3 = _mm_cvtepi32_ps(i32_3);
  3281. // Apply the scale
  3282. acc_0 = _mm_mul_ps( d_0_1, p0 );
  3283. acc_1 = _mm_mul_ps( d_0_1, p1 );
  3284. acc_2 = _mm_mul_ps( d_2_3, p2 );
  3285. acc_3 = _mm_mul_ps( d_2_3, p3 );
  3286. }
  3287. assert(nb % 2 == 0); // TODO: handle odd nb
  3288. // Main loop
  3289. for (int i = 2; i < nb; i+=2) {
  3290. _mm_prefetch(&x[i] + sizeof(block_q4_0), _MM_HINT_T0);
  3291. _mm_prefetch(&y[i] + sizeof(block_q8_0), _MM_HINT_T0);
  3292. // Compute combined scale for the block 0 and 1
  3293. const __m128 d_0_1 = _mm_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3294. const __m128i tmp_0_1 = _mm_loadu_si128((const __m128i *)x[i].qs);
  3295. __m128i bx_0 = _mm_and_si128(lowMask, tmp_0_1);
  3296. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[i].qs);
  3297. bx_0 = _mm_sub_epi8(bx_0, off);
  3298. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3299. __m128i bx_1 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_0_1, 4));
  3300. __m128i by_1 = _mm_loadu_si128((const __m128i *)(y[i].qs + 16));
  3301. bx_1 = _mm_sub_epi8(bx_1, off);
  3302. const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
  3303. _mm_prefetch(&x[i] + 2 * sizeof(block_q4_0), _MM_HINT_T0);
  3304. _mm_prefetch(&y[i] + 2 * sizeof(block_q8_0), _MM_HINT_T0);
  3305. // Compute combined scale for the block 2 and 3
  3306. const __m128 d_2_3 = _mm_set1_ps( GGML_FP16_TO_FP32(x[i + 1].d) * GGML_FP16_TO_FP32(y[i + 1].d) );
  3307. const __m128i tmp_2_3 = _mm_loadu_si128((const __m128i *)x[i + 1].qs);
  3308. __m128i bx_2 = _mm_and_si128(lowMask, tmp_2_3);
  3309. __m128i by_2 = _mm_loadu_si128((const __m128i *)y[i + 1].qs);
  3310. bx_2 = _mm_sub_epi8(bx_2, off);
  3311. const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
  3312. __m128i bx_3 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_2_3, 4));
  3313. __m128i by_3 = _mm_loadu_si128((const __m128i *)(y[i + 1].qs + 16));
  3314. bx_3 = _mm_sub_epi8(bx_3, off);
  3315. const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
  3316. // Convert int32_t to float
  3317. __m128 p0 = _mm_cvtepi32_ps(i32_0);
  3318. __m128 p1 = _mm_cvtepi32_ps(i32_1);
  3319. __m128 p2 = _mm_cvtepi32_ps(i32_2);
  3320. __m128 p3 = _mm_cvtepi32_ps(i32_3);
  3321. // Apply the scale
  3322. __m128 p0_d = _mm_mul_ps( d_0_1, p0 );
  3323. __m128 p1_d = _mm_mul_ps( d_0_1, p1 );
  3324. __m128 p2_d = _mm_mul_ps( d_2_3, p2 );
  3325. __m128 p3_d = _mm_mul_ps( d_2_3, p3 );
  3326. // Acummulate
  3327. acc_0 = _mm_add_ps(p0_d, acc_0);
  3328. acc_1 = _mm_add_ps(p1_d, acc_1);
  3329. acc_2 = _mm_add_ps(p2_d, acc_2);
  3330. acc_3 = _mm_add_ps(p3_d, acc_3);
  3331. }
  3332. *s = hsum_float_4x4(acc_0, acc_1, acc_2, acc_3);
  3333. #elif defined(__riscv_v_intrinsic)
  3334. float sumf = 0.0;
  3335. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3336. for (int i = 0; i < nb; i++) {
  3337. // load elements
  3338. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3339. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3340. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3341. // mask and store lower part of x, and then upper part
  3342. vuint8mf2_t x_a = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3343. vuint8mf2_t x_l = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3344. vint8mf2_t x_ai = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3345. vint8mf2_t x_li = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3346. // subtract offset
  3347. vint8mf2_t v0 = __riscv_vsub_vx_i8mf2(x_ai, 8, vl);
  3348. vint8mf2_t v1 = __riscv_vsub_vx_i8mf2(x_li, 8, vl);
  3349. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3350. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3351. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3352. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3353. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3354. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3355. sumf += sumi*GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d);
  3356. }
  3357. *s = sumf;
  3358. #else
  3359. // scalar
  3360. float sumf = 0.0;
  3361. for (int i = 0; i < nb; i++) {
  3362. int sumi = 0;
  3363. for (int j = 0; j < qk/2; ++j) {
  3364. const int v0 = (x[i].qs[j] & 0x0F) - 8;
  3365. const int v1 = (x[i].qs[j] >> 4) - 8;
  3366. sumi += (v0 * y[i].qs[j]) + (v1 * y[i].qs[j + qk/2]);
  3367. }
  3368. sumf += sumi*GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d);
  3369. }
  3370. *s = sumf;
  3371. #endif
  3372. }
  3373. void ggml_vec_dot_q4_1_q8_1(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3374. const int qk = QK8_1;
  3375. const int nb = n / qk;
  3376. assert(n % qk == 0);
  3377. const block_q4_1 * restrict x = vx;
  3378. const block_q8_1 * restrict y = vy;
  3379. // TODO: add WASM SIMD
  3380. #if defined(__ARM_NEON)
  3381. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3382. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3383. float summs = 0;
  3384. assert(nb % 2 == 0); // TODO: handle odd nb
  3385. for (int i = 0; i < nb; i += 2) {
  3386. const block_q4_1 * restrict x0 = &x[i + 0];
  3387. const block_q4_1 * restrict x1 = &x[i + 1];
  3388. const block_q8_1 * restrict y0 = &y[i + 0];
  3389. const block_q8_1 * restrict y1 = &y[i + 1];
  3390. summs += GGML_FP16_TO_FP32(x0->m) * y0->s + GGML_FP16_TO_FP32(x1->m) * y1->s;
  3391. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3392. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3393. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3394. // 4-bit -> 8-bit
  3395. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3396. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3397. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3398. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3399. // load y
  3400. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3401. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3402. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3403. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3404. // dot product into int32x4_t
  3405. const int32x4_t p_0 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_0l, v1_0l), v0_0h, v1_0h);
  3406. const int32x4_t p_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_1l, v1_1l), v0_1h, v1_1h);
  3407. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(p_0), GGML_FP16_TO_FP32(x0->d)*y0->d);
  3408. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(p_1), GGML_FP16_TO_FP32(x1->d)*y1->d);
  3409. }
  3410. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1) + summs;
  3411. #elif defined(__AVX2__) || defined(__AVX__)
  3412. // Initialize accumulator with zeros
  3413. __m256 acc = _mm256_setzero_ps();
  3414. float summs = 0;
  3415. // Main loop
  3416. for (int i = 0; i < nb; ++i) {
  3417. const float d0 = GGML_FP16_TO_FP32(x[i].d);
  3418. const float d1 = y[i].d;
  3419. summs += GGML_FP16_TO_FP32(x[i].m) * y[i].s;
  3420. const __m256 d0v = _mm256_set1_ps( d0 );
  3421. const __m256 d1v = _mm256_set1_ps( d1 );
  3422. // Compute combined scales
  3423. const __m256 d0d1 = _mm256_mul_ps( d0v, d1v );
  3424. // Load 16 bytes, and unpack 4 bit fields into bytes, making 32 bytes
  3425. const __m256i bx = bytes_from_nibbles_32(x[i].qs);
  3426. const __m256i by = _mm256_loadu_si256( (const __m256i *)y[i].qs );
  3427. const __m256 xy = mul_sum_us8_pairs_float(bx, by);
  3428. // Accumulate d0*d1*x*y
  3429. #if defined(__AVX2__)
  3430. acc = _mm256_fmadd_ps( d0d1, xy, acc );
  3431. #else
  3432. acc = _mm256_add_ps( _mm256_mul_ps( d0d1, xy ), acc );
  3433. #endif
  3434. }
  3435. *s = hsum_float_8(acc) + summs;
  3436. #elif defined(__riscv_v_intrinsic)
  3437. float sumf = 0.0;
  3438. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3439. for (int i = 0; i < nb; i++) {
  3440. // load elements
  3441. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3442. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3443. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3444. // mask and store lower part of x, and then upper part
  3445. vuint8mf2_t x_a = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3446. vuint8mf2_t x_l = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3447. vint8mf2_t v0 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3448. vint8mf2_t v1 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3449. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3450. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3451. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3452. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3453. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3454. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3455. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  3456. }
  3457. *s = sumf;
  3458. #else
  3459. // scalar
  3460. float sumf = 0.0;
  3461. for (int i = 0; i < nb; i++) {
  3462. int sumi = 0;
  3463. for (int j = 0; j < qk/2; ++j) {
  3464. const int v0 = (x[i].qs[j] & 0x0F);
  3465. const int v1 = (x[i].qs[j] >> 4);
  3466. sumi += (v0 * y[i].qs[j]) + (v1 * y[i].qs[j + qk/2]);
  3467. }
  3468. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  3469. }
  3470. *s = sumf;
  3471. #endif
  3472. }
  3473. void ggml_vec_dot_q5_0_q8_0(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3474. const int qk = QK8_0;
  3475. const int nb = n / qk;
  3476. assert(n % qk == 0);
  3477. assert(qk == QK5_0);
  3478. const block_q5_0 * restrict x = vx;
  3479. const block_q8_0 * restrict y = vy;
  3480. #if defined(__ARM_NEON)
  3481. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3482. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3483. uint32_t qh0;
  3484. uint32_t qh1;
  3485. uint64_t tmp0[4];
  3486. uint64_t tmp1[4];
  3487. assert(nb % 2 == 0); // TODO: handle odd nb
  3488. for (int i = 0; i < nb; i += 2) {
  3489. const block_q5_0 * restrict x0 = &x[i];
  3490. const block_q5_0 * restrict x1 = &x[i + 1];
  3491. const block_q8_0 * restrict y0 = &y[i];
  3492. const block_q8_0 * restrict y1 = &y[i + 1];
  3493. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3494. // extract the 5th bit via lookup table ((!b) << 4)
  3495. memcpy(&qh0, x0->qh, sizeof(qh0));
  3496. memcpy(&qh1, x1->qh, sizeof(qh1));
  3497. tmp0[0] = table_b2b_1[(qh0 >> 0) & 0xFF];
  3498. tmp0[1] = table_b2b_1[(qh0 >> 8) & 0xFF];
  3499. tmp0[2] = table_b2b_1[(qh0 >> 16) & 0xFF];
  3500. tmp0[3] = table_b2b_1[(qh0 >> 24) ];
  3501. tmp1[0] = table_b2b_1[(qh1 >> 0) & 0xFF];
  3502. tmp1[1] = table_b2b_1[(qh1 >> 8) & 0xFF];
  3503. tmp1[2] = table_b2b_1[(qh1 >> 16) & 0xFF];
  3504. tmp1[3] = table_b2b_1[(qh1 >> 24) ];
  3505. const int8x16_t qhl0 = vld1q_s8((const int8_t *)(tmp0 + 0));
  3506. const int8x16_t qhh0 = vld1q_s8((const int8_t *)(tmp0 + 2));
  3507. const int8x16_t qhl1 = vld1q_s8((const int8_t *)(tmp1 + 0));
  3508. const int8x16_t qhh1 = vld1q_s8((const int8_t *)(tmp1 + 2));
  3509. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3510. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3511. // 4-bit -> 8-bit
  3512. int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3513. int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3514. int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3515. int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3516. // add high bit and sub 16 (equivalent to sub 0x10 when bit is zero)
  3517. const int8x16_t v0_0lf = vsubq_s8(v0_0l, qhl0);
  3518. const int8x16_t v0_0hf = vsubq_s8(v0_0h, qhh0);
  3519. const int8x16_t v0_1lf = vsubq_s8(v0_1l, qhl1);
  3520. const int8x16_t v0_1hf = vsubq_s8(v0_1h, qhh1);
  3521. // load y
  3522. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3523. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3524. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3525. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3526. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3527. ggml_vdotq_s32(vdupq_n_s32(0), v0_0lf, v1_0l),
  3528. ggml_vdotq_s32(vdupq_n_s32(0), v0_0hf, v1_0h))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3529. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3530. ggml_vdotq_s32(vdupq_n_s32(0), v0_1lf, v1_1l),
  3531. ggml_vdotq_s32(vdupq_n_s32(0), v0_1hf, v1_1h))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3532. }
  3533. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  3534. #elif defined(__wasm_simd128__)
  3535. v128_t sumv = wasm_f32x4_splat(0.0f);
  3536. uint32_t qh;
  3537. uint64_t tmp[4];
  3538. // TODO: check if unrolling this is better
  3539. for (int i = 0; i < nb; ++i) {
  3540. const block_q5_0 * restrict x0 = &x[i];
  3541. const block_q8_0 * restrict y0 = &y[i];
  3542. const v128_t m4b = wasm_i8x16_splat(0x0F);
  3543. // extract the 5th bit
  3544. memcpy(&qh, x0->qh, sizeof(qh));
  3545. tmp[0] = table_b2b_1[(qh >> 0) & 0xFF];
  3546. tmp[1] = table_b2b_1[(qh >> 8) & 0xFF];
  3547. tmp[2] = table_b2b_1[(qh >> 16) & 0xFF];
  3548. tmp[3] = table_b2b_1[(qh >> 24) ];
  3549. const v128_t qhl = wasm_v128_load(tmp + 0);
  3550. const v128_t qhh = wasm_v128_load(tmp + 2);
  3551. const v128_t v0 = wasm_v128_load(x0->qs);
  3552. // 4-bit -> 8-bit
  3553. const v128_t v0l = wasm_v128_and (v0, m4b);
  3554. const v128_t v0h = wasm_u8x16_shr(v0, 4);
  3555. // add high bit and sub 16 (equivalent to sub 0x10 when bit is zero)
  3556. const v128_t v0lf = wasm_i8x16_sub(v0l, qhl);
  3557. const v128_t v0hf = wasm_i8x16_sub(v0h, qhh);
  3558. // load y
  3559. const v128_t v1l = wasm_v128_load(y0->qs);
  3560. const v128_t v1h = wasm_v128_load(y0->qs + 16);
  3561. // int8x16 -> int16x8
  3562. const v128_t v0lfl = wasm_i16x8_extend_low_i8x16 (v0lf);
  3563. const v128_t v0lfh = wasm_i16x8_extend_high_i8x16(v0lf);
  3564. const v128_t v0hfl = wasm_i16x8_extend_low_i8x16 (v0hf);
  3565. const v128_t v0hfh = wasm_i16x8_extend_high_i8x16(v0hf);
  3566. const v128_t v1ll = wasm_i16x8_extend_low_i8x16 (v1l);
  3567. const v128_t v1lh = wasm_i16x8_extend_high_i8x16(v1l);
  3568. const v128_t v1hl = wasm_i16x8_extend_low_i8x16 (v1h);
  3569. const v128_t v1hh = wasm_i16x8_extend_high_i8x16(v1h);
  3570. // dot product
  3571. sumv = wasm_f32x4_add(sumv, wasm_f32x4_mul(wasm_f32x4_convert_i32x4(
  3572. wasm_i32x4_add(
  3573. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0lfl, v1ll),
  3574. wasm_i32x4_dot_i16x8(v0lfh, v1lh)),
  3575. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0hfl, v1hl),
  3576. wasm_i32x4_dot_i16x8(v0hfh, v1hh)))),
  3577. wasm_f32x4_splat(GGML_FP16_TO_FP32(x0->d) * GGML_FP16_TO_FP32(y0->d))));
  3578. }
  3579. *s = wasm_f32x4_extract_lane(sumv, 0) + wasm_f32x4_extract_lane(sumv, 1) +
  3580. wasm_f32x4_extract_lane(sumv, 2) + wasm_f32x4_extract_lane(sumv, 3);
  3581. #elif defined(__AVX2__)
  3582. // Initialize accumulator with zeros
  3583. __m256 acc = _mm256_setzero_ps();
  3584. // Main loop
  3585. for (int i = 0; i < nb; i++) {
  3586. /* Compute combined scale for the block */
  3587. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3588. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  3589. __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3590. bxhi = _mm256_andnot_si256(bxhi, _mm256_set1_epi8((char)0xF0));
  3591. bx = _mm256_or_si256(bx, bxhi);
  3592. __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3593. const __m256 q = mul_sum_i8_pairs_float(bx, by);
  3594. /* Multiply q with scale and accumulate */
  3595. acc = _mm256_fmadd_ps(d, q, acc);
  3596. }
  3597. *s = hsum_float_8(acc);
  3598. #elif defined(__AVX__)
  3599. // Initialize accumulator with zeros
  3600. __m256 acc = _mm256_setzero_ps();
  3601. __m128i mask = _mm_set1_epi8((char)0xF0);
  3602. // Main loop
  3603. for (int i = 0; i < nb; i++) {
  3604. /* Compute combined scale for the block */
  3605. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3606. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  3607. const __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3608. __m128i bxhil = _mm256_castsi256_si128(bxhi);
  3609. __m128i bxhih = _mm256_extractf128_si256(bxhi, 1);
  3610. bxhil = _mm_andnot_si128(bxhil, mask);
  3611. bxhih = _mm_andnot_si128(bxhih, mask);
  3612. __m128i bxl = _mm256_castsi256_si128(bx);
  3613. __m128i bxh = _mm256_extractf128_si256(bx, 1);
  3614. bxl = _mm_or_si128(bxl, bxhil);
  3615. bxh = _mm_or_si128(bxh, bxhih);
  3616. bx = MM256_SET_M128I(bxh, bxl);
  3617. const __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3618. const __m256 q = mul_sum_i8_pairs_float(bx, by);
  3619. /* Multiply q with scale and accumulate */
  3620. acc = _mm256_add_ps(_mm256_mul_ps(d, q), acc);
  3621. }
  3622. *s = hsum_float_8(acc);
  3623. #elif defined(__riscv_v_intrinsic)
  3624. float sumf = 0.0;
  3625. uint32_t qh;
  3626. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3627. // These temporary registers are for masking and shift operations
  3628. vuint32m2_t vt_1 = __riscv_vid_v_u32m2(vl);
  3629. vuint32m2_t vt_2 = __riscv_vsll_vv_u32m2(__riscv_vmv_v_x_u32m2(1, vl), vt_1, vl);
  3630. vuint32m2_t vt_3 = __riscv_vsll_vx_u32m2(vt_2, 16, vl);
  3631. vuint32m2_t vt_4 = __riscv_vadd_vx_u32m2(vt_1, 12, vl);
  3632. for (int i = 0; i < nb; i++) {
  3633. memcpy(&qh, x[i].qh, sizeof(uint32_t));
  3634. // ((qh & (1u << (j + 0 ))) >> (j + 0 )) << 4;
  3635. vuint32m2_t xha_0 = __riscv_vand_vx_u32m2(vt_2, qh, vl);
  3636. vuint32m2_t xhr_0 = __riscv_vsrl_vv_u32m2(xha_0, vt_1, vl);
  3637. vuint32m2_t xhl_0 = __riscv_vsll_vx_u32m2(xhr_0, 4, vl);
  3638. // ((qh & (1u << (j + 16))) >> (j + 12));
  3639. vuint32m2_t xha_1 = __riscv_vand_vx_u32m2(vt_3, qh, vl);
  3640. vuint32m2_t xhl_1 = __riscv_vsrl_vv_u32m2(xha_1, vt_4, vl);
  3641. // narrowing
  3642. vuint16m1_t xhc_0 = __riscv_vncvt_x_x_w_u16m1(xhl_0, vl);
  3643. vuint8mf2_t xh_0 = __riscv_vncvt_x_x_w_u8mf2(xhc_0, vl);
  3644. vuint16m1_t xhc_1 = __riscv_vncvt_x_x_w_u16m1(xhl_1, vl);
  3645. vuint8mf2_t xh_1 = __riscv_vncvt_x_x_w_u8mf2(xhc_1, vl);
  3646. // load
  3647. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3648. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3649. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3650. vuint8mf2_t x_at = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3651. vuint8mf2_t x_lt = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3652. vuint8mf2_t x_a = __riscv_vor_vv_u8mf2(x_at, xh_0, vl);
  3653. vuint8mf2_t x_l = __riscv_vor_vv_u8mf2(x_lt, xh_1, vl);
  3654. vint8mf2_t x_ai = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3655. vint8mf2_t x_li = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3656. vint8mf2_t v0 = __riscv_vsub_vx_i8mf2(x_ai, 16, vl);
  3657. vint8mf2_t v1 = __riscv_vsub_vx_i8mf2(x_li, 16, vl);
  3658. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3659. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3660. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3661. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3662. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3663. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3664. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d)) * sumi;
  3665. }
  3666. *s = sumf;
  3667. #else
  3668. // scalar
  3669. float sumf = 0.0;
  3670. for (int i = 0; i < nb; i++) {
  3671. uint32_t qh;
  3672. memcpy(&qh, x[i].qh, sizeof(qh));
  3673. int sumi = 0;
  3674. for (int j = 0; j < qk/2; ++j) {
  3675. const uint8_t xh_0 = ((qh & (1u << (j + 0 ))) >> (j + 0 )) << 4;
  3676. const uint8_t xh_1 = ((qh & (1u << (j + 16))) >> (j + 12));
  3677. const int32_t x0 = ((x[i].qs[j] & 0x0F) | xh_0) - 16;
  3678. const int32_t x1 = ((x[i].qs[j] >> 4) | xh_1) - 16;
  3679. sumi += (x0 * y[i].qs[j]) + (x1 * y[i].qs[j + qk/2]);
  3680. }
  3681. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d)) * sumi;
  3682. }
  3683. *s = sumf;
  3684. #endif
  3685. }
  3686. void ggml_vec_dot_q5_1_q8_1(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3687. const int qk = QK8_1;
  3688. const int nb = n / qk;
  3689. assert(n % qk == 0);
  3690. assert(qk == QK5_1);
  3691. const block_q5_1 * restrict x = vx;
  3692. const block_q8_1 * restrict y = vy;
  3693. #if defined(__ARM_NEON)
  3694. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3695. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3696. float summs0 = 0.0f;
  3697. float summs1 = 0.0f;
  3698. uint32_t qh0;
  3699. uint32_t qh1;
  3700. uint64_t tmp0[4];
  3701. uint64_t tmp1[4];
  3702. assert(nb % 2 == 0); // TODO: handle odd nb
  3703. for (int i = 0; i < nb; i += 2) {
  3704. const block_q5_1 * restrict x0 = &x[i];
  3705. const block_q5_1 * restrict x1 = &x[i + 1];
  3706. const block_q8_1 * restrict y0 = &y[i];
  3707. const block_q8_1 * restrict y1 = &y[i + 1];
  3708. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3709. summs0 += GGML_FP16_TO_FP32(x0->m) * y0->s;
  3710. summs1 += GGML_FP16_TO_FP32(x1->m) * y1->s;
  3711. // extract the 5th bit via lookup table ((b) << 4)
  3712. memcpy(&qh0, x0->qh, sizeof(qh0));
  3713. memcpy(&qh1, x1->qh, sizeof(qh1));
  3714. tmp0[0] = table_b2b_0[(qh0 >> 0) & 0xFF];
  3715. tmp0[1] = table_b2b_0[(qh0 >> 8) & 0xFF];
  3716. tmp0[2] = table_b2b_0[(qh0 >> 16) & 0xFF];
  3717. tmp0[3] = table_b2b_0[(qh0 >> 24) ];
  3718. tmp1[0] = table_b2b_0[(qh1 >> 0) & 0xFF];
  3719. tmp1[1] = table_b2b_0[(qh1 >> 8) & 0xFF];
  3720. tmp1[2] = table_b2b_0[(qh1 >> 16) & 0xFF];
  3721. tmp1[3] = table_b2b_0[(qh1 >> 24) ];
  3722. const int8x16_t qhl0 = vld1q_s8((const int8_t *)(tmp0 + 0));
  3723. const int8x16_t qhh0 = vld1q_s8((const int8_t *)(tmp0 + 2));
  3724. const int8x16_t qhl1 = vld1q_s8((const int8_t *)(tmp1 + 0));
  3725. const int8x16_t qhh1 = vld1q_s8((const int8_t *)(tmp1 + 2));
  3726. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3727. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3728. // 4-bit -> 8-bit
  3729. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3730. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3731. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3732. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3733. // add high bit
  3734. const int8x16_t v0_0lf = vorrq_s8(v0_0l, qhl0);
  3735. const int8x16_t v0_0hf = vorrq_s8(v0_0h, qhh0);
  3736. const int8x16_t v0_1lf = vorrq_s8(v0_1l, qhl1);
  3737. const int8x16_t v0_1hf = vorrq_s8(v0_1h, qhh1);
  3738. // load y
  3739. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3740. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3741. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3742. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3743. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3744. ggml_vdotq_s32(vdupq_n_s32(0), v0_0lf, v1_0l),
  3745. ggml_vdotq_s32(vdupq_n_s32(0), v0_0hf, v1_0h))), GGML_FP16_TO_FP32(x0->d)*y0->d);
  3746. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3747. ggml_vdotq_s32(vdupq_n_s32(0), v0_1lf, v1_1l),
  3748. ggml_vdotq_s32(vdupq_n_s32(0), v0_1hf, v1_1h))), GGML_FP16_TO_FP32(x1->d)*y1->d);
  3749. }
  3750. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1) + summs0 + summs1;
  3751. #elif defined(__wasm_simd128__)
  3752. v128_t sumv = wasm_f32x4_splat(0.0f);
  3753. float summs = 0.0f;
  3754. uint32_t qh;
  3755. uint64_t tmp[4];
  3756. // TODO: check if unrolling this is better
  3757. for (int i = 0; i < nb; ++i) {
  3758. const block_q5_1 * restrict x0 = &x[i];
  3759. const block_q8_1 * restrict y0 = &y[i];
  3760. summs += GGML_FP16_TO_FP32(x0->m) * y0->s;
  3761. const v128_t m4b = wasm_i8x16_splat(0x0F);
  3762. // extract the 5th bit
  3763. memcpy(&qh, x0->qh, sizeof(qh));
  3764. tmp[0] = table_b2b_0[(qh >> 0) & 0xFF];
  3765. tmp[1] = table_b2b_0[(qh >> 8) & 0xFF];
  3766. tmp[2] = table_b2b_0[(qh >> 16) & 0xFF];
  3767. tmp[3] = table_b2b_0[(qh >> 24) ];
  3768. const v128_t qhl = wasm_v128_load(tmp + 0);
  3769. const v128_t qhh = wasm_v128_load(tmp + 2);
  3770. const v128_t v0 = wasm_v128_load(x0->qs);
  3771. // 4-bit -> 8-bit
  3772. const v128_t v0l = wasm_v128_and (v0, m4b);
  3773. const v128_t v0h = wasm_u8x16_shr(v0, 4);
  3774. // add high bit
  3775. const v128_t v0lf = wasm_v128_or(v0l, qhl);
  3776. const v128_t v0hf = wasm_v128_or(v0h, qhh);
  3777. // load y
  3778. const v128_t v1l = wasm_v128_load(y0->qs);
  3779. const v128_t v1h = wasm_v128_load(y0->qs + 16);
  3780. // int8x16 -> int16x8
  3781. const v128_t v0lfl = wasm_i16x8_extend_low_i8x16 (v0lf);
  3782. const v128_t v0lfh = wasm_i16x8_extend_high_i8x16(v0lf);
  3783. const v128_t v0hfl = wasm_i16x8_extend_low_i8x16 (v0hf);
  3784. const v128_t v0hfh = wasm_i16x8_extend_high_i8x16(v0hf);
  3785. const v128_t v1ll = wasm_i16x8_extend_low_i8x16 (v1l);
  3786. const v128_t v1lh = wasm_i16x8_extend_high_i8x16(v1l);
  3787. const v128_t v1hl = wasm_i16x8_extend_low_i8x16 (v1h);
  3788. const v128_t v1hh = wasm_i16x8_extend_high_i8x16(v1h);
  3789. // dot product
  3790. sumv = wasm_f32x4_add(sumv,
  3791. wasm_f32x4_mul(wasm_f32x4_convert_i32x4(wasm_i32x4_add(
  3792. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0lfl, v1ll),
  3793. wasm_i32x4_dot_i16x8(v0lfh, v1lh)),
  3794. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0hfl, v1hl),
  3795. wasm_i32x4_dot_i16x8(v0hfh, v1hh)))),
  3796. wasm_f32x4_splat(GGML_FP16_TO_FP32(x0->d) * y0->d)));
  3797. }
  3798. *s = wasm_f32x4_extract_lane(sumv, 0) + wasm_f32x4_extract_lane(sumv, 1) +
  3799. wasm_f32x4_extract_lane(sumv, 2) + wasm_f32x4_extract_lane(sumv, 3) + summs;
  3800. #elif defined(__AVX2__)
  3801. // Initialize accumulator with zeros
  3802. __m256 acc = _mm256_setzero_ps();
  3803. float summs = 0.0f;
  3804. // Main loop
  3805. for (int i = 0; i < nb; i++) {
  3806. const __m256 dx = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d));
  3807. summs += GGML_FP16_TO_FP32(x[i].m) * y[i].s;
  3808. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  3809. __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3810. bxhi = _mm256_and_si256(bxhi, _mm256_set1_epi8(0x10));
  3811. bx = _mm256_or_si256(bx, bxhi);
  3812. const __m256 dy = _mm256_set1_ps(y[i].d);
  3813. const __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3814. const __m256 q = mul_sum_us8_pairs_float(bx, by);
  3815. acc = _mm256_fmadd_ps(q, _mm256_mul_ps(dx, dy), acc);
  3816. }
  3817. *s = hsum_float_8(acc) + summs;
  3818. #elif defined(__AVX__)
  3819. // Initialize accumulator with zeros
  3820. __m256 acc = _mm256_setzero_ps();
  3821. __m128i mask = _mm_set1_epi8(0x10);
  3822. float summs = 0.0f;
  3823. // Main loop
  3824. for (int i = 0; i < nb; i++) {
  3825. const __m256 dx = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d));
  3826. summs += GGML_FP16_TO_FP32(x[i].m) * y[i].s;
  3827. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  3828. const __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3829. __m128i bxhil = _mm256_castsi256_si128(bxhi);
  3830. __m128i bxhih = _mm256_extractf128_si256(bxhi, 1);
  3831. bxhil = _mm_and_si128(bxhil, mask);
  3832. bxhih = _mm_and_si128(bxhih, mask);
  3833. __m128i bxl = _mm256_castsi256_si128(bx);
  3834. __m128i bxh = _mm256_extractf128_si256(bx, 1);
  3835. bxl = _mm_or_si128(bxl, bxhil);
  3836. bxh = _mm_or_si128(bxh, bxhih);
  3837. bx = MM256_SET_M128I(bxh, bxl);
  3838. const __m256 dy = _mm256_set1_ps(y[i].d);
  3839. const __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3840. const __m256 q = mul_sum_us8_pairs_float(bx, by);
  3841. acc = _mm256_add_ps(_mm256_mul_ps(q, _mm256_mul_ps(dx, dy)), acc);
  3842. }
  3843. *s = hsum_float_8(acc) + summs;
  3844. #elif defined(__riscv_v_intrinsic)
  3845. float sumf = 0.0;
  3846. uint32_t qh;
  3847. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3848. // temporary registers for shift operations
  3849. vuint32m2_t vt_1 = __riscv_vid_v_u32m2(vl);
  3850. vuint32m2_t vt_2 = __riscv_vadd_vx_u32m2(vt_1, 12, vl);
  3851. for (int i = 0; i < nb; i++) {
  3852. memcpy(&qh, x[i].qh, sizeof(uint32_t));
  3853. // load qh
  3854. vuint32m2_t vqh = __riscv_vmv_v_x_u32m2(qh, vl);
  3855. // ((qh >> (j + 0)) << 4) & 0x10;
  3856. vuint32m2_t xhr_0 = __riscv_vsrl_vv_u32m2(vqh, vt_1, vl);
  3857. vuint32m2_t xhl_0 = __riscv_vsll_vx_u32m2(xhr_0, 4, vl);
  3858. vuint32m2_t xha_0 = __riscv_vand_vx_u32m2(xhl_0, 0x10, vl);
  3859. // ((qh >> (j + 12)) ) & 0x10;
  3860. vuint32m2_t xhr_1 = __riscv_vsrl_vv_u32m2(vqh, vt_2, vl);
  3861. vuint32m2_t xha_1 = __riscv_vand_vx_u32m2(xhr_1, 0x10, vl);
  3862. // narrowing
  3863. vuint16m1_t xhc_0 = __riscv_vncvt_x_x_w_u16m1(xha_0, vl);
  3864. vuint8mf2_t xh_0 = __riscv_vncvt_x_x_w_u8mf2(xhc_0, vl);
  3865. vuint16m1_t xhc_1 = __riscv_vncvt_x_x_w_u16m1(xha_1, vl);
  3866. vuint8mf2_t xh_1 = __riscv_vncvt_x_x_w_u8mf2(xhc_1, vl);
  3867. // load
  3868. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3869. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3870. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3871. vuint8mf2_t x_at = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3872. vuint8mf2_t x_lt = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3873. vuint8mf2_t x_a = __riscv_vor_vv_u8mf2(x_at, xh_0, vl);
  3874. vuint8mf2_t x_l = __riscv_vor_vv_u8mf2(x_lt, xh_1, vl);
  3875. vint8mf2_t v0 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3876. vint8mf2_t v1 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3877. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3878. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3879. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3880. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3881. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3882. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3883. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  3884. }
  3885. *s = sumf;
  3886. #else
  3887. // scalar
  3888. float sumf = 0.0;
  3889. for (int i = 0; i < nb; i++) {
  3890. uint32_t qh;
  3891. memcpy(&qh, x[i].qh, sizeof(qh));
  3892. int sumi = 0;
  3893. for (int j = 0; j < qk/2; ++j) {
  3894. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  3895. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  3896. const int32_t x0 = (x[i].qs[j] & 0xF) | xh_0;
  3897. const int32_t x1 = (x[i].qs[j] >> 4) | xh_1;
  3898. sumi += (x0 * y[i].qs[j]) + (x1 * y[i].qs[j + qk/2]);
  3899. }
  3900. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  3901. }
  3902. *s = sumf;
  3903. #endif
  3904. }
  3905. void ggml_vec_dot_q8_0_q8_0(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3906. const int qk = QK8_0;
  3907. const int nb = n / qk;
  3908. assert(n % qk == 0);
  3909. const block_q8_0 * restrict x = vx;
  3910. const block_q8_0 * restrict y = vy;
  3911. #if defined(__ARM_NEON)
  3912. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3913. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3914. assert(nb % 2 == 0); // TODO: handle odd nb
  3915. for (int i = 0; i < nb; i += 2) {
  3916. const block_q8_0 * restrict x0 = &x[i + 0];
  3917. const block_q8_0 * restrict x1 = &x[i + 1];
  3918. const block_q8_0 * restrict y0 = &y[i + 0];
  3919. const block_q8_0 * restrict y1 = &y[i + 1];
  3920. const int8x16_t x0_0 = vld1q_s8(x0->qs);
  3921. const int8x16_t x0_1 = vld1q_s8(x0->qs + 16);
  3922. const int8x16_t x1_0 = vld1q_s8(x1->qs);
  3923. const int8x16_t x1_1 = vld1q_s8(x1->qs + 16);
  3924. // load y
  3925. const int8x16_t y0_0 = vld1q_s8(y0->qs);
  3926. const int8x16_t y0_1 = vld1q_s8(y0->qs + 16);
  3927. const int8x16_t y1_0 = vld1q_s8(y1->qs);
  3928. const int8x16_t y1_1 = vld1q_s8(y1->qs + 16);
  3929. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3930. ggml_vdotq_s32(vdupq_n_s32(0), x0_0, y0_0),
  3931. ggml_vdotq_s32(vdupq_n_s32(0), x0_1, y0_1))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3932. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3933. ggml_vdotq_s32(vdupq_n_s32(0), x1_0, y1_0),
  3934. ggml_vdotq_s32(vdupq_n_s32(0), x1_1, y1_1))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3935. }
  3936. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  3937. #elif defined(__AVX2__) || defined(__AVX__)
  3938. // Initialize accumulator with zeros
  3939. __m256 acc = _mm256_setzero_ps();
  3940. // Main loop
  3941. for (int i = 0; i < nb; ++i) {
  3942. // Compute combined scale for the block
  3943. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3944. __m256i bx = _mm256_loadu_si256((const __m256i *)x[i].qs);
  3945. __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3946. const __m256 q = mul_sum_i8_pairs_float(bx, by);
  3947. // Multiply q with scale and accumulate
  3948. #if defined(__AVX2__)
  3949. acc = _mm256_fmadd_ps( d, q, acc );
  3950. #else
  3951. acc = _mm256_add_ps( _mm256_mul_ps( d, q ), acc );
  3952. #endif
  3953. }
  3954. *s = hsum_float_8(acc);
  3955. #elif defined(__riscv_v_intrinsic)
  3956. float sumf = 0.0;
  3957. size_t vl = __riscv_vsetvl_e8m1(qk);
  3958. for (int i = 0; i < nb; i++) {
  3959. // load elements
  3960. vint8m1_t bx = __riscv_vle8_v_i8m1(x[i].qs, vl);
  3961. vint8m1_t by = __riscv_vle8_v_i8m1(y[i].qs, vl);
  3962. vint16m2_t vw_mul = __riscv_vwmul_vv_i16m2(bx, by, vl);
  3963. vint32m1_t v_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3964. vint32m1_t v_sum = __riscv_vwredsum_vs_i16m2_i32m1(vw_mul, v_zero, vl);
  3965. int sumi = __riscv_vmv_x_s_i32m1_i32(v_sum);
  3966. sumf += sumi*(GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d));
  3967. }
  3968. *s = sumf;
  3969. #else
  3970. // scalar
  3971. float sumf = 0.0;
  3972. for (int i = 0; i < nb; i++) {
  3973. int sumi = 0;
  3974. for (int j = 0; j < qk; j++) {
  3975. sumi += x[i].qs[j]*y[i].qs[j];
  3976. }
  3977. sumf += sumi*(GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d));
  3978. }
  3979. *s = sumf;
  3980. #endif
  3981. }
  3982. #if QK_K == 256
  3983. void ggml_vec_dot_q2_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3984. const block_q2_K * restrict x = vx;
  3985. const block_q8_K * restrict y = vy;
  3986. const int nb = n / QK_K;
  3987. #ifdef __ARM_NEON
  3988. const uint8x16_t m3 = vdupq_n_u8(0x3);
  3989. const uint8x16_t m4 = vdupq_n_u8(0xF);
  3990. const int32x4_t vzero = vdupq_n_s32(0);
  3991. ggml_int8x16x2_t q2bytes;
  3992. uint8_t aux[16];
  3993. float sum = 0;
  3994. for (int i = 0; i < nb; ++i) {
  3995. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3996. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3997. const uint8_t * restrict q2 = x[i].qs;
  3998. const int8_t * restrict q8 = y[i].qs;
  3999. const uint8_t * restrict sc = x[i].scales;
  4000. const uint8x16_t mins_and_scales = vld1q_u8(sc);
  4001. const uint8x16_t scales = vandq_u8(mins_and_scales, m4);
  4002. vst1q_u8(aux, scales);
  4003. const uint8x16_t mins = vshrq_n_u8(mins_and_scales, 4);
  4004. const ggml_int16x8x2_t q8sums = ggml_vld1q_s16_x2(y[i].bsums);
  4005. const ggml_int16x8x2_t mins16 = {{vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mins))), vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mins)))}};
  4006. const int32x4_t s0 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[0]), vget_low_s16 (q8sums.val[0])),
  4007. vmull_s16(vget_high_s16(mins16.val[0]), vget_high_s16(q8sums.val[0])));
  4008. const int32x4_t s1 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[1]), vget_low_s16 (q8sums.val[1])),
  4009. vmull_s16(vget_high_s16(mins16.val[1]), vget_high_s16(q8sums.val[1])));
  4010. sum += dmin * vaddvq_s32(vaddq_s32(s0, s1));
  4011. int isum = 0;
  4012. int is = 0;
  4013. // We use this macro instead of a function call because for some reason
  4014. // the code runs 2-3% slower, even if the function is declared inline
  4015. #define MULTIPLY_ACCUM_WITH_SCALE(index)\
  4016. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[0], q8bytes.val[0])) * aux[is+(index)];\
  4017. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[1], q8bytes.val[1])) * aux[is+1+(index)];
  4018. #define SHIFT_MULTIPLY_ACCUM_WITH_SCALE(shift, index)\
  4019. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;\
  4020. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[0], (shift)), m3));\
  4021. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[1], (shift)), m3));\
  4022. MULTIPLY_ACCUM_WITH_SCALE((index));
  4023. for (int j = 0; j < QK_K/128; ++j) {
  4024. const ggml_uint8x16x2_t q2bits = ggml_vld1q_u8_x2(q2); q2 += 32;
  4025. ggml_int8x16x2_t q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  4026. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[0], m3));
  4027. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[1], m3));
  4028. MULTIPLY_ACCUM_WITH_SCALE(0);
  4029. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(2, 2);
  4030. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(4, 4);
  4031. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(6, 6);
  4032. is += 8;
  4033. }
  4034. sum += d * isum;
  4035. }
  4036. *s = sum;
  4037. #elif defined __AVX2__
  4038. const __m256i m3 = _mm256_set1_epi8(3);
  4039. const __m128i m4 = _mm_set1_epi8(0xF);
  4040. __m256 acc = _mm256_setzero_ps();
  4041. for (int i = 0; i < nb; ++i) {
  4042. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4043. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4044. const uint8_t * restrict q2 = x[i].qs;
  4045. const int8_t * restrict q8 = y[i].qs;
  4046. const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  4047. const __m128i scales8 = _mm_and_si128(mins_and_scales, m4);
  4048. const __m128i mins8 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
  4049. const __m256i mins = _mm256_cvtepi8_epi16(mins8);
  4050. const __m256i prod = _mm256_madd_epi16(mins, _mm256_loadu_si256((const __m256i*)y[i].bsums));
  4051. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(prod), acc);
  4052. const __m256i all_scales = _mm256_cvtepi8_epi16(scales8);
  4053. const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
  4054. const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
  4055. const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
  4056. __m256i sumi = _mm256_setzero_si256();
  4057. for (int j = 0; j < QK_K/128; ++j) {
  4058. const __m256i q2bits = _mm256_loadu_si256((const __m256i*)q2); q2 += 32;
  4059. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4060. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4061. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4062. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4063. const __m256i q2_0 = _mm256_and_si256(q2bits, m3);
  4064. const __m256i q2_1 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 2), m3);
  4065. const __m256i q2_2 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 4), m3);
  4066. const __m256i q2_3 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 6), m3);
  4067. __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0);
  4068. __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1);
  4069. __m256i p2 = _mm256_maddubs_epi16(q2_2, q8_2);
  4070. __m256i p3 = _mm256_maddubs_epi16(q2_3, q8_3);
  4071. p0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(0)), p0);
  4072. p1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(1)), p1);
  4073. p2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(2)), p2);
  4074. p3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(3)), p3);
  4075. p0 = _mm256_add_epi32(p0, p1);
  4076. p2 = _mm256_add_epi32(p2, p3);
  4077. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p0, p2));
  4078. }
  4079. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  4080. }
  4081. *s = hsum_float_8(acc);
  4082. #elif defined __AVX__
  4083. const __m128i m3 = _mm_set1_epi8(0x3);
  4084. const __m128i m4 = _mm_set1_epi8(0xF);
  4085. const __m128i m2 = _mm_set1_epi8(0x2);
  4086. __m256 acc = _mm256_setzero_ps();
  4087. for (int i = 0; i < nb; ++i) {
  4088. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4089. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4090. const uint8_t * restrict q2 = x[i].qs;
  4091. const int8_t * restrict q8 = y[i].qs;
  4092. // load mins and scales from block_q2_K.scales[QK_K/16]
  4093. const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  4094. const __m128i scales16 = _mm_and_si128(mins_and_scales, m4);
  4095. const __m128i mins16 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
  4096. const __m128i mins_0 = _mm_cvtepi8_epi16(mins16);
  4097. const __m128i mins_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(mins16, mins16));
  4098. // summs = y[i].bsums * (x[i].scales >> 4) in 16bits*8*2 to 32bits*4*2
  4099. const __m128i summs_0 = _mm_madd_epi16(mins_0, _mm_loadu_si128((const __m128i*)&y[i].bsums[0]));
  4100. const __m128i summs_1 = _mm_madd_epi16(mins_1, _mm_loadu_si128((const __m128i*)&y[i].bsums[8]));
  4101. // sumf += -dmin * summs in 32bits*8
  4102. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(MM256_SET_M128I(summs_1, summs_0))), acc);
  4103. const __m128i scales_0 = _mm_cvtepi8_epi16(scales16);
  4104. const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales16, scales16));
  4105. const __m128i scales[2] = { scales_0, scales_1 };
  4106. __m128i sumi_0 = _mm_setzero_si128();
  4107. __m128i sumi_1 = _mm_setzero_si128();
  4108. for (int j = 0; j < QK_K/128; ++j) {
  4109. // load Q8 quants int8*16*8 from block_q8_K.qs[QK_K]
  4110. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4111. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4112. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4113. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4114. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4115. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4116. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4117. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4118. // load 2bits*16*8 from block_q2_K.qs[QK_K/4]
  4119. __m128i q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
  4120. const __m128i q2_0 = _mm_and_si128(q2bits, m3);
  4121. const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4122. const __m128i q2_4 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4123. const __m128i q2_6 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4124. q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
  4125. const __m128i q2_1 = _mm_and_si128(q2bits, m3);
  4126. const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4127. const __m128i q2_5 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4128. const __m128i q2_7 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4129. // isuml = q8[l] * ((q2[l] >> shift) & 3) in 8bits*16*8 to 16bits*8*8
  4130. __m128i p0 = _mm_maddubs_epi16(q2_0, q8_0);
  4131. __m128i p1 = _mm_maddubs_epi16(q2_1, q8_1);
  4132. __m128i p2 = _mm_maddubs_epi16(q2_2, q8_2);
  4133. __m128i p3 = _mm_maddubs_epi16(q2_3, q8_3);
  4134. __m128i p4 = _mm_maddubs_epi16(q2_4, q8_4);
  4135. __m128i p5 = _mm_maddubs_epi16(q2_5, q8_5);
  4136. __m128i p6 = _mm_maddubs_epi16(q2_6, q8_6);
  4137. __m128i p7 = _mm_maddubs_epi16(q2_7, q8_7);
  4138. // isum += (x[i].scales[is++] & 0xF) * isuml in 16bits*8*8 to 32bits*4*8
  4139. __m128i shuffle = _mm_set1_epi16(0x0100);
  4140. p0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p0);
  4141. shuffle = _mm_add_epi16(shuffle, m2);
  4142. p1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p1);
  4143. shuffle = _mm_add_epi16(shuffle, m2);
  4144. p2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p2);
  4145. shuffle = _mm_add_epi16(shuffle, m2);
  4146. p3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p3);
  4147. shuffle = _mm_add_epi16(shuffle, m2);
  4148. p4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p4);
  4149. shuffle = _mm_add_epi16(shuffle, m2);
  4150. p5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p5);
  4151. shuffle = _mm_add_epi16(shuffle, m2);
  4152. p6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p6);
  4153. shuffle = _mm_add_epi16(shuffle, m2);
  4154. p7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p7);
  4155. p0 = _mm_add_epi32(p0, p1);
  4156. p2 = _mm_add_epi32(p2, p3);
  4157. p4 = _mm_add_epi32(p4, p5);
  4158. p6 = _mm_add_epi32(p6, p7);
  4159. // isum in 32bits*4*2
  4160. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p0, p2));
  4161. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p4, p6));
  4162. }
  4163. // sumf += dall * isum - dmin * summs in 32bits
  4164. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  4165. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dall), _mm256_cvtepi32_ps(sumi)), acc);
  4166. }
  4167. *s = hsum_float_8(acc);
  4168. #elif defined __riscv_v_intrinsic
  4169. float sumf = 0;
  4170. uint8_t temp_01[32] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  4171. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
  4172. for (int i = 0; i < nb; ++i) {
  4173. const uint8_t * q2 = x[i].qs;
  4174. const int8_t * q8 = y[i].qs;
  4175. const uint8_t * sc = x[i].scales;
  4176. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4177. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4178. size_t vl = 16;
  4179. vuint8m1_t scales = __riscv_vle8_v_u8m1(sc, vl);
  4180. vuint8m1_t aux = __riscv_vand_vx_u8m1(scales, 0x0F, vl);
  4181. vint16m1_t q8sums = __riscv_vle16_v_i16m1(y[i].bsums, vl);
  4182. vuint8mf2_t scales_2 = __riscv_vle8_v_u8mf2(sc, vl);
  4183. vuint8mf2_t mins8 = __riscv_vsrl_vx_u8mf2(scales_2, 0x4, vl);
  4184. vint16m1_t mins = __riscv_vreinterpret_v_u16m1_i16m1(__riscv_vzext_vf2_u16m1(mins8, vl));
  4185. vint32m2_t prod = __riscv_vwmul_vv_i32m2(q8sums, mins, vl);
  4186. vint32m1_t vsums = __riscv_vredsum_vs_i32m2_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  4187. sumf += dmin * __riscv_vmv_x_s_i32m1_i32(vsums);
  4188. vl = 32;
  4189. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4190. vuint8m1_t v_b = __riscv_vle8_v_u8m1(temp_01, vl);
  4191. uint8_t is=0;
  4192. int isum=0;
  4193. for (int j = 0; j < QK_K/128; ++j) {
  4194. // load Q2
  4195. vuint8m1_t q2_x = __riscv_vle8_v_u8m1(q2, vl);
  4196. vuint8m1_t q2_0 = __riscv_vand_vx_u8m1(q2_x, 0x03, vl);
  4197. vuint8m1_t q2_1 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x2, vl), 0x03 , vl);
  4198. vuint8m1_t q2_2 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x4, vl), 0x03 , vl);
  4199. vuint8m1_t q2_3 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x6, vl), 0x03 , vl);
  4200. // duplicate scale elements for product
  4201. vuint8m1_t sc0 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 0+is, vl), vl);
  4202. vuint8m1_t sc1 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 2+is, vl), vl);
  4203. vuint8m1_t sc2 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 4+is, vl), vl);
  4204. vuint8m1_t sc3 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 6+is, vl), vl);
  4205. vint16m2_t p0 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_0, sc0, vl));
  4206. vint16m2_t p1 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_1, sc1, vl));
  4207. vint16m2_t p2 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_2, sc2, vl));
  4208. vint16m2_t p3 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_3, sc3, vl));
  4209. // load Q8
  4210. vint8m1_t q8_0 = __riscv_vle8_v_i8m1(q8, vl);
  4211. vint8m1_t q8_1 = __riscv_vle8_v_i8m1(q8+32, vl);
  4212. vint8m1_t q8_2 = __riscv_vle8_v_i8m1(q8+64, vl);
  4213. vint8m1_t q8_3 = __riscv_vle8_v_i8m1(q8+96, vl);
  4214. vint32m4_t s0 = __riscv_vwmul_vv_i32m4(p0, __riscv_vwcvt_x_x_v_i16m2(q8_0, vl), vl);
  4215. vint32m4_t s1 = __riscv_vwmul_vv_i32m4(p1, __riscv_vwcvt_x_x_v_i16m2(q8_1, vl), vl);
  4216. vint32m4_t s2 = __riscv_vwmul_vv_i32m4(p2, __riscv_vwcvt_x_x_v_i16m2(q8_2, vl), vl);
  4217. vint32m4_t s3 = __riscv_vwmul_vv_i32m4(p3, __riscv_vwcvt_x_x_v_i16m2(q8_3, vl), vl);
  4218. vint32m1_t isum0 = __riscv_vredsum_vs_i32m4_i32m1(__riscv_vadd_vv_i32m4(s0, s1, vl), vzero, vl);
  4219. vint32m1_t isum1 = __riscv_vredsum_vs_i32m4_i32m1(__riscv_vadd_vv_i32m4(s2, s3, vl), isum0, vl);
  4220. isum += __riscv_vmv_x_s_i32m1_i32(isum1);
  4221. q2+=32; q8+=128; is=8;
  4222. }
  4223. sumf += dall * isum;
  4224. }
  4225. *s = sumf;
  4226. #else
  4227. float sumf = 0;
  4228. for (int i = 0; i < nb; ++i) {
  4229. const uint8_t * q2 = x[i].qs;
  4230. const int8_t * q8 = y[i].qs;
  4231. const uint8_t * sc = x[i].scales;
  4232. int summs = 0;
  4233. for (int j = 0; j < 16; ++j) {
  4234. summs += y[i].bsums[j] * (sc[j] >> 4);
  4235. }
  4236. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4237. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4238. int isum = 0;
  4239. int is = 0;
  4240. int d;
  4241. for (int k = 0; k < QK_K/128; ++k) {
  4242. int shift = 0;
  4243. for (int j = 0; j < 4; ++j) {
  4244. d = sc[is++] & 0xF;
  4245. int isuml = 0;
  4246. for (int l = 0; l < 16; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
  4247. isum += d * isuml;
  4248. d = sc[is++] & 0xF;
  4249. isuml = 0;
  4250. for (int l = 16; l < 32; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
  4251. isum += d * isuml;
  4252. shift += 2;
  4253. q8 += 32;
  4254. }
  4255. q2 += 32;
  4256. }
  4257. sumf += dall * isum - dmin * summs;
  4258. }
  4259. *s = sumf;
  4260. #endif
  4261. }
  4262. #else
  4263. void ggml_vec_dot_q2_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  4264. const block_q2_K * restrict x = vx;
  4265. const block_q8_K * restrict y = vy;
  4266. const int nb = n / QK_K;
  4267. #ifdef __ARM_NEON
  4268. const uint8x16_t m3 = vdupq_n_u8(0x3);
  4269. const int32x4_t vzero = vdupq_n_s32(0);
  4270. ggml_int8x16x4_t q2bytes;
  4271. uint32_t aux32[2];
  4272. const uint8_t * scales = (const uint8_t *)aux32;
  4273. float sum = 0;
  4274. for (int i = 0; i < nb; ++i) {
  4275. const float d = y[i].d * (float)x[i].d;
  4276. const float dmin = -y[i].d * (float)x[i].dmin;
  4277. const uint8_t * restrict q2 = x[i].qs;
  4278. const int8_t * restrict q8 = y[i].qs;
  4279. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4280. aux32[0] = sc[0] & 0x0f0f0f0f;
  4281. aux32[1] = (sc[0] >> 4) & 0x0f0f0f0f;
  4282. sum += dmin * (scales[4] * y[i].bsums[0] + scales[5] * y[i].bsums[1] + scales[6] * y[i].bsums[2] + scales[7] * y[i].bsums[3]);
  4283. int isum1 = 0, isum2 = 0;
  4284. const uint8x16_t q2bits = vld1q_u8(q2);
  4285. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  4286. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(q2bits, m3));
  4287. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 2), m3));
  4288. q2bytes.val[2] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 4), m3));
  4289. q2bytes.val[3] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 6), m3));
  4290. isum1 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[0], q8bytes.val[0])) * scales[0];
  4291. isum2 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[1], q8bytes.val[1])) * scales[1];
  4292. isum1 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[2], q8bytes.val[2])) * scales[2];
  4293. isum2 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[3], q8bytes.val[3])) * scales[3];
  4294. sum += d * (isum1 + isum2);
  4295. }
  4296. *s = sum;
  4297. #elif defined __AVX2__
  4298. const __m256i m3 = _mm256_set1_epi8(3);
  4299. __m256 acc = _mm256_setzero_ps();
  4300. uint32_t ud, um;
  4301. const uint8_t * restrict db = (const uint8_t *)&ud;
  4302. const uint8_t * restrict mb = (const uint8_t *)&um;
  4303. float summs = 0;
  4304. // TODO: optimize this
  4305. for (int i = 0; i < nb; ++i) {
  4306. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4307. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4308. const uint8_t * restrict q2 = x[i].qs;
  4309. const int8_t * restrict q8 = y[i].qs;
  4310. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4311. ud = (sc[0] >> 0) & 0x0f0f0f0f;
  4312. um = (sc[0] >> 4) & 0x0f0f0f0f;
  4313. int32_t smin = mb[0] * y[i].bsums[0] + mb[1] * y[i].bsums[1] + mb[2] * y[i].bsums[2] + mb[3] * y[i].bsums[3];
  4314. summs += dmin * smin;
  4315. const __m128i q2bits = _mm_loadu_si128((const __m128i*)q2);
  4316. const __m256i q2_0 = _mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q2bits, 2), q2bits), m3);
  4317. const __m256i q2_1 = _mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q2bits, 6), _mm_srli_epi16(q2bits, 4)), m3);
  4318. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4319. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4320. const __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0);
  4321. const __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1);
  4322. const __m256i p_0 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p0, 0));
  4323. const __m256i p_1 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p0, 1));
  4324. const __m256i p_2 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p1, 0));
  4325. const __m256i p_3 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p1, 1));
  4326. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[0]), _mm256_cvtepi32_ps(p_0), acc);
  4327. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[1]), _mm256_cvtepi32_ps(p_1), acc);
  4328. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[2]), _mm256_cvtepi32_ps(p_2), acc);
  4329. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[3]), _mm256_cvtepi32_ps(p_3), acc);
  4330. }
  4331. *s = hsum_float_8(acc) + summs;
  4332. #elif defined __AVX__
  4333. const __m128i m3 = _mm_set1_epi8(3);
  4334. __m256 acc = _mm256_setzero_ps();
  4335. uint32_t ud, um;
  4336. const uint8_t * restrict db = (const uint8_t *)&ud;
  4337. const uint8_t * restrict mb = (const uint8_t *)&um;
  4338. float summs = 0;
  4339. // TODO: optimize this
  4340. for (int i = 0; i < nb; ++i) {
  4341. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4342. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4343. const uint8_t * restrict q2 = x[i].qs;
  4344. const int8_t * restrict q8 = y[i].qs;
  4345. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4346. ud = (sc[0] >> 0) & 0x0f0f0f0f;
  4347. um = (sc[0] >> 4) & 0x0f0f0f0f;
  4348. int32_t smin = mb[0] * y[i].bsums[0] + mb[1] * y[i].bsums[1] + mb[2] * y[i].bsums[2] + mb[3] * y[i].bsums[3];
  4349. summs += dmin * smin;
  4350. const __m128i q2bits = _mm_loadu_si128((const __m128i*)q2);
  4351. const __m128i q2_0 = _mm_and_si128(q2bits, m3);
  4352. const __m128i q2_1 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4353. const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4354. const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4355. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4356. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4357. const __m128i p0 = _mm_maddubs_epi16(q2_0, _mm256_extractf128_si256(q8_0, 0));
  4358. const __m128i p1 = _mm_maddubs_epi16(q2_1, _mm256_extractf128_si256(q8_0, 1));
  4359. const __m128i p2 = _mm_maddubs_epi16(q2_2, _mm256_extractf128_si256(q8_1, 0));
  4360. const __m128i p3 = _mm_maddubs_epi16(q2_3, _mm256_extractf128_si256(q8_1, 1));
  4361. const __m256i p_0 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p0, p0)), _mm_cvtepi16_epi32(p0));
  4362. const __m256i p_1 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p1, p1)), _mm_cvtepi16_epi32(p1));
  4363. const __m256i p_2 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p2, p2)), _mm_cvtepi16_epi32(p2));
  4364. const __m256i p_3 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p3, p3)), _mm_cvtepi16_epi32(p3));
  4365. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[0]), _mm256_cvtepi32_ps(p_0)), acc);
  4366. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[1]), _mm256_cvtepi32_ps(p_1)), acc);
  4367. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[2]), _mm256_cvtepi32_ps(p_2)), acc);
  4368. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[3]), _mm256_cvtepi32_ps(p_3)), acc);
  4369. }
  4370. *s = hsum_float_8(acc) + summs;
  4371. #elif defined __riscv_v_intrinsic
  4372. uint32_t aux32[2];
  4373. const uint8_t * scales = (const uint8_t *)aux32;
  4374. float sumf = 0;
  4375. for (int i = 0; i < nb; ++i) {
  4376. const float d = y[i].d * (float)x[i].d;
  4377. const float dmin = -y[i].d * (float)x[i].dmin;
  4378. const uint8_t * restrict q2 = x[i].qs;
  4379. const int8_t * restrict q8 = y[i].qs;
  4380. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4381. aux32[0] = sc[0] & 0x0f0f0f0f;
  4382. aux32[1] = (sc[0] >> 4) & 0x0f0f0f0f;
  4383. sumf += dmin * (scales[4] * y[i].bsums[0] + scales[5] * y[i].bsums[1] + scales[6] * y[i].bsums[2] + scales[7] * y[i].bsums[3]);
  4384. int isum1 = 0;
  4385. int isum2 = 0;
  4386. size_t vl = 16;
  4387. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  4388. // load Q2
  4389. vuint8mf2_t q2_x = __riscv_vle8_v_u8mf2(q2, vl);
  4390. vint8mf2_t q2_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q2_x, 0x03, vl));
  4391. vint8mf2_t q2_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x2, vl), 0x03 , vl));
  4392. vint8mf2_t q2_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x4, vl), 0x03 , vl));
  4393. vint8mf2_t q2_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x6, vl), 0x03 , vl));
  4394. // load Q8, and take product with Q2
  4395. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q2_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  4396. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q2_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  4397. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q2_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  4398. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q2_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  4399. vint16m1_t vs_0 = __riscv_vredsum_vs_i16m1_i16m1(p0, vzero, vl);
  4400. vint16m1_t vs_1 = __riscv_vredsum_vs_i16m1_i16m1(p1, vzero, vl);
  4401. vint16m1_t vs_2 = __riscv_vredsum_vs_i16m1_i16m1(p2, vzero, vl);
  4402. vint16m1_t vs_3 = __riscv_vredsum_vs_i16m1_i16m1(p3, vzero, vl);
  4403. isum1 += __riscv_vmv_x_s_i16m1_i16(vs_0) * scales[0];
  4404. isum2 += __riscv_vmv_x_s_i16m1_i16(vs_1) * scales[1];
  4405. isum1 += __riscv_vmv_x_s_i16m1_i16(vs_2) * scales[2];
  4406. isum2 += __riscv_vmv_x_s_i16m1_i16(vs_3) * scales[3];
  4407. sumf += d * (isum1 + isum2);
  4408. }
  4409. *s = sumf;
  4410. #else
  4411. float sumf = 0;
  4412. int isum[4];
  4413. for (int i = 0; i < nb; ++i) {
  4414. const uint8_t * q2 = x[i].qs;
  4415. const int8_t * q8 = y[i].qs;
  4416. const uint8_t * sc = x[i].scales;
  4417. int summs = 0;
  4418. for (int j = 0; j < QK_K/16; ++j) {
  4419. summs += y[i].bsums[j] * (sc[j] >> 4);
  4420. }
  4421. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4422. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4423. isum[0] = isum[1] = isum[2] = isum[3] = 0;
  4424. for (int l = 0; l < 16; ++l) {
  4425. isum[0] += q8[l+ 0] * ((q2[l] >> 0) & 3);
  4426. isum[1] += q8[l+16] * ((q2[l] >> 2) & 3);
  4427. isum[2] += q8[l+32] * ((q2[l] >> 4) & 3);
  4428. isum[3] += q8[l+48] * ((q2[l] >> 6) & 3);
  4429. }
  4430. for (int l = 0; l < 4; ++l) {
  4431. isum[l] *= (sc[l] & 0xF);
  4432. }
  4433. sumf += dall * (isum[0] + isum[1] + isum[2] + isum[3]) - dmin * summs;
  4434. }
  4435. *s = sumf;
  4436. #endif
  4437. }
  4438. #endif
  4439. #if QK_K == 256
  4440. void ggml_vec_dot_q3_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  4441. assert(n % QK_K == 0);
  4442. const uint32_t kmask1 = 0x03030303;
  4443. const uint32_t kmask2 = 0x0f0f0f0f;
  4444. const block_q3_K * restrict x = vx;
  4445. const block_q8_K * restrict y = vy;
  4446. const int nb = n / QK_K;
  4447. #ifdef __ARM_NEON
  4448. uint32_t aux[3];
  4449. uint32_t utmp[4];
  4450. const uint8x16_t m3b = vdupq_n_u8(0x3);
  4451. const int32x4_t vzero = vdupq_n_s32(0);
  4452. const uint8x16_t m0 = vdupq_n_u8(1);
  4453. const uint8x16_t m1 = vshlq_n_u8(m0, 1);
  4454. const uint8x16_t m2 = vshlq_n_u8(m0, 2);
  4455. const uint8x16_t m3 = vshlq_n_u8(m0, 3);
  4456. const int8_t m32 = 32;
  4457. ggml_int8x16x4_t q3bytes;
  4458. float sum = 0;
  4459. for (int i = 0; i < nb; ++i) {
  4460. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4461. const uint8_t * restrict q3 = x[i].qs;
  4462. const uint8_t * restrict qh = x[i].hmask;
  4463. const int8_t * restrict q8 = y[i].qs;
  4464. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh);
  4465. ggml_uint8x16x4_t q3h;
  4466. int32_t isum = 0;
  4467. // Set up scales
  4468. memcpy(aux, x[i].scales, 12);
  4469. utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
  4470. utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
  4471. utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
  4472. utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
  4473. int8_t * scale = (int8_t *)utmp;
  4474. for (int j = 0; j < 16; ++j) scale[j] -= m32;
  4475. for (int j = 0; j < QK_K/128; ++j) {
  4476. const ggml_uint8x16x2_t q3bits = ggml_vld1q_u8_x2(q3); q3 += 32;
  4477. const ggml_int8x16x4_t q8bytes_1 = ggml_vld1q_s8_x4(q8); q8 += 64;
  4478. const ggml_int8x16x4_t q8bytes_2 = ggml_vld1q_s8_x4(q8); q8 += 64;
  4479. q3h.val[0] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[0]), 2);
  4480. q3h.val[1] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[1]), 2);
  4481. q3h.val[2] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[0]), 1);
  4482. q3h.val[3] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[1]), 1);
  4483. q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[0], m3b)), vreinterpretq_s8_u8(q3h.val[0]));
  4484. q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[1], m3b)), vreinterpretq_s8_u8(q3h.val[1]));
  4485. q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 2), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
  4486. q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 2), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
  4487. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes_1.val[0])) * scale[0];
  4488. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes_1.val[1])) * scale[1];
  4489. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes_1.val[2])) * scale[2];
  4490. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes_1.val[3])) * scale[3];
  4491. scale += 4;
  4492. q3h.val[0] = vbicq_u8(m2, qhbits.val[0]);
  4493. q3h.val[1] = vbicq_u8(m2, qhbits.val[1]);
  4494. q3h.val[2] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[0]), 1);
  4495. q3h.val[3] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[1]), 1);
  4496. q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 4), m3b)), vreinterpretq_s8_u8(q3h.val[0]));
  4497. q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 4), m3b)), vreinterpretq_s8_u8(q3h.val[1]));
  4498. q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 6), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
  4499. q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 6), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
  4500. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes_2.val[0])) * scale[0];
  4501. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes_2.val[1])) * scale[1];
  4502. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes_2.val[2])) * scale[2];
  4503. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes_2.val[3])) * scale[3];
  4504. scale += 4;
  4505. if (j == 0) {
  4506. qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 4);
  4507. qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 4);
  4508. }
  4509. }
  4510. sum += d * isum;
  4511. }
  4512. *s = sum;
  4513. #elif defined __AVX2__
  4514. const __m256i m3 = _mm256_set1_epi8(3);
  4515. const __m256i mone = _mm256_set1_epi8(1);
  4516. const __m128i m32 = _mm_set1_epi8(32);
  4517. __m256 acc = _mm256_setzero_ps();
  4518. uint32_t aux[3];
  4519. for (int i = 0; i < nb; ++i) {
  4520. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4521. const uint8_t * restrict q3 = x[i].qs;
  4522. const int8_t * restrict q8 = y[i].qs;
  4523. // Set up scales
  4524. memcpy(aux, x[i].scales, 12);
  4525. __m128i scales128 = _mm_set_epi32(
  4526. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  4527. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  4528. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  4529. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  4530. scales128 = _mm_sub_epi8(scales128, m32);
  4531. const __m256i all_scales = _mm256_cvtepi8_epi16(scales128);
  4532. const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
  4533. const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
  4534. const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
  4535. // high bit
  4536. const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].hmask);
  4537. // integer accumulator
  4538. __m256i sumi = _mm256_setzero_si256();
  4539. int bit = 0;
  4540. int is = 0;
  4541. for (int j = 0; j < QK_K/128; ++j) {
  4542. // load low 2 bits
  4543. const __m256i q3bits = _mm256_loadu_si256((const __m256i*)q3); q3 += 32;
  4544. // prepare low and high bits
  4545. const __m256i q3l_0 = _mm256_and_si256(q3bits, m3);
  4546. const __m256i q3h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4547. ++bit;
  4548. const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 2), m3);
  4549. const __m256i q3h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4550. ++bit;
  4551. const __m256i q3l_2 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 4), m3);
  4552. const __m256i q3h_2 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4553. ++bit;
  4554. const __m256i q3l_3 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 6), m3);
  4555. const __m256i q3h_3 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4556. ++bit;
  4557. // load Q8 quants
  4558. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4559. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4560. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4561. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4562. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  4563. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4564. // and 2 if the high bit was set)
  4565. __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0);
  4566. __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1);
  4567. __m256i q8s_2 = _mm256_maddubs_epi16(q3h_2, q8_2);
  4568. __m256i q8s_3 = _mm256_maddubs_epi16(q3h_3, q8_3);
  4569. __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0);
  4570. __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1);
  4571. __m256i p16_2 = _mm256_maddubs_epi16(q3l_2, q8_2);
  4572. __m256i p16_3 = _mm256_maddubs_epi16(q3l_3, q8_3);
  4573. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  4574. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  4575. p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
  4576. p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
  4577. // multiply with scales
  4578. p16_0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 0)), p16_0);
  4579. p16_1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 1)), p16_1);
  4580. p16_2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 2)), p16_2);
  4581. p16_3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 3)), p16_3);
  4582. // accumulate
  4583. p16_0 = _mm256_add_epi32(p16_0, p16_1);
  4584. p16_2 = _mm256_add_epi32(p16_2, p16_3);
  4585. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_2));
  4586. }
  4587. // multiply with block scale and accumulate
  4588. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  4589. }
  4590. *s = hsum_float_8(acc);
  4591. #elif defined __AVX__
  4592. const __m128i m3 = _mm_set1_epi8(3);
  4593. const __m128i mone = _mm_set1_epi8(1);
  4594. const __m128i m32 = _mm_set1_epi8(32);
  4595. const __m128i m2 = _mm_set1_epi8(2);
  4596. __m256 acc = _mm256_setzero_ps();
  4597. const uint32_t *aux;
  4598. for (int i = 0; i < nb; ++i) {
  4599. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4600. const uint8_t * restrict q3 = x[i].qs;
  4601. const int8_t * restrict q8 = y[i].qs;
  4602. // Set up scales
  4603. aux = (const uint32_t *)x[i].scales;
  4604. __m128i scales128 = _mm_set_epi32(
  4605. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  4606. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  4607. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  4608. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  4609. scales128 = _mm_sub_epi8(scales128, m32);
  4610. const __m128i scales_0 = _mm_cvtepi8_epi16(scales128);
  4611. const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales128, scales128));
  4612. const __m128i scales[2] = { scales_0, scales_1 };
  4613. // high bit *128*2 from block_q3_K.hmask[QK_K/8]
  4614. const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].hmask[0]);
  4615. const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].hmask[16]);
  4616. // integer accumulator
  4617. __m128i sumi_0 = _mm_setzero_si128();
  4618. __m128i sumi_1 = _mm_setzero_si128();
  4619. for (int j = 0; j < QK_K/128; ++j) {
  4620. // load low 2 bits *64*2 from block_q3_K.qs[QK_K/4]
  4621. const __m128i q3bits_0 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
  4622. const __m128i q3bits_1 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
  4623. // prepare low and high bits
  4624. const int bit = j << 2;
  4625. const __m128i q3l_0 = _mm_and_si128(q3bits_0, m3);
  4626. const __m128i q3l_1 = _mm_and_si128(q3bits_1, m3);
  4627. const __m128i q3h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit)), bit), 2);
  4628. const __m128i q3h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit)), bit), 2);
  4629. const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 2), m3);
  4630. const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 2), m3);
  4631. const __m128i q3h_2 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
  4632. const __m128i q3h_3 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
  4633. const __m128i q3l_4 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 4), m3);
  4634. const __m128i q3l_5 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 4), m3);
  4635. const __m128i q3h_4 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
  4636. const __m128i q3h_5 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
  4637. const __m128i q3l_6 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 6), m3);
  4638. const __m128i q3l_7 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 6), m3);
  4639. const __m128i q3h_6 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
  4640. const __m128i q3h_7 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
  4641. // load Q8 quants from block_q8_K.qs[QK_K]
  4642. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4643. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4644. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4645. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4646. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4647. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4648. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4649. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4650. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  4651. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4652. // and 2 if the high bit was set)
  4653. __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, q8_0);
  4654. __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, q8_1);
  4655. __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, q8_2);
  4656. __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, q8_3);
  4657. __m128i q8s_4 = _mm_maddubs_epi16(q3h_4, q8_4);
  4658. __m128i q8s_5 = _mm_maddubs_epi16(q3h_5, q8_5);
  4659. __m128i q8s_6 = _mm_maddubs_epi16(q3h_6, q8_6);
  4660. __m128i q8s_7 = _mm_maddubs_epi16(q3h_7, q8_7);
  4661. __m128i p16_0 = _mm_maddubs_epi16(q3l_0, q8_0);
  4662. __m128i p16_1 = _mm_maddubs_epi16(q3l_1, q8_1);
  4663. __m128i p16_2 = _mm_maddubs_epi16(q3l_2, q8_2);
  4664. __m128i p16_3 = _mm_maddubs_epi16(q3l_3, q8_3);
  4665. __m128i p16_4 = _mm_maddubs_epi16(q3l_4, q8_4);
  4666. __m128i p16_5 = _mm_maddubs_epi16(q3l_5, q8_5);
  4667. __m128i p16_6 = _mm_maddubs_epi16(q3l_6, q8_6);
  4668. __m128i p16_7 = _mm_maddubs_epi16(q3l_7, q8_7);
  4669. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  4670. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  4671. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  4672. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  4673. p16_4 = _mm_sub_epi16(p16_4, q8s_4);
  4674. p16_5 = _mm_sub_epi16(p16_5, q8s_5);
  4675. p16_6 = _mm_sub_epi16(p16_6, q8s_6);
  4676. p16_7 = _mm_sub_epi16(p16_7, q8s_7);
  4677. // multiply with scales
  4678. __m128i shuffle = _mm_set1_epi16(0x0100);
  4679. p16_0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_0);
  4680. shuffle = _mm_add_epi16(shuffle, m2);
  4681. p16_1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_1);
  4682. shuffle = _mm_add_epi16(shuffle, m2);
  4683. p16_2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_2);
  4684. shuffle = _mm_add_epi16(shuffle, m2);
  4685. p16_3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_3);
  4686. shuffle = _mm_add_epi16(shuffle, m2);
  4687. p16_4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_4);
  4688. shuffle = _mm_add_epi16(shuffle, m2);
  4689. p16_5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_5);
  4690. shuffle = _mm_add_epi16(shuffle, m2);
  4691. p16_6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_6);
  4692. shuffle = _mm_add_epi16(shuffle, m2);
  4693. p16_7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_7);
  4694. // accumulate
  4695. p16_0 = _mm_add_epi32(p16_0, p16_1);
  4696. p16_2 = _mm_add_epi32(p16_2, p16_3);
  4697. p16_4 = _mm_add_epi32(p16_4, p16_5);
  4698. p16_6 = _mm_add_epi32(p16_6, p16_7);
  4699. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  4700. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_4, p16_6));
  4701. }
  4702. // multiply with block scale and accumulate
  4703. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  4704. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
  4705. }
  4706. *s = hsum_float_8(acc);
  4707. #elif defined __riscv_v_intrinsic
  4708. uint32_t aux[3];
  4709. uint32_t utmp[4];
  4710. float sumf = 0;
  4711. for (int i = 0; i < nb; ++i) {
  4712. const uint8_t * restrict q3 = x[i].qs;
  4713. const uint8_t * restrict qh = x[i].hmask;
  4714. const int8_t * restrict q8 = y[i].qs;
  4715. memcpy(aux, x[i].scales, 12);
  4716. utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
  4717. utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
  4718. utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
  4719. utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
  4720. int8_t * scale = (int8_t *)utmp;
  4721. for (int j = 0; j < 16; ++j) scale[j] -= 32;
  4722. size_t vl = 32;
  4723. uint8_t m = 1;
  4724. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4725. vuint8m1_t vqh = __riscv_vle8_v_u8m1(qh, vl);
  4726. int sum_t = 0;
  4727. for (int j = 0; j < QK_K; j += 128) {
  4728. vl = 32;
  4729. // load Q3
  4730. vuint8m1_t q3_x = __riscv_vle8_v_u8m1(q3, vl);
  4731. vint8m1_t q3_0 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q3_x, 0x03, vl));
  4732. vint8m1_t q3_1 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x2, vl), 0x03 , vl));
  4733. vint8m1_t q3_2 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x4, vl), 0x03 , vl));
  4734. vint8m1_t q3_3 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x6, vl), 0x03 , vl));
  4735. // compute mask for subtraction
  4736. vuint8m1_t qh_m0 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4737. vbool8_t vmask_0 = __riscv_vmseq_vx_u8m1_b8(qh_m0, 0, vl);
  4738. vint8m1_t q3_m0 = __riscv_vsub_vx_i8m1_m(vmask_0, q3_0, 0x4, vl);
  4739. m <<= 1;
  4740. vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4741. vbool8_t vmask_1 = __riscv_vmseq_vx_u8m1_b8(qh_m1, 0, vl);
  4742. vint8m1_t q3_m1 = __riscv_vsub_vx_i8m1_m(vmask_1, q3_1, 0x4, vl);
  4743. m <<= 1;
  4744. vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4745. vbool8_t vmask_2 = __riscv_vmseq_vx_u8m1_b8(qh_m2, 0, vl);
  4746. vint8m1_t q3_m2 = __riscv_vsub_vx_i8m1_m(vmask_2, q3_2, 0x4, vl);
  4747. m <<= 1;
  4748. vuint8m1_t qh_m3 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4749. vbool8_t vmask_3 = __riscv_vmseq_vx_u8m1_b8(qh_m3, 0, vl);
  4750. vint8m1_t q3_m3 = __riscv_vsub_vx_i8m1_m(vmask_3, q3_3, 0x4, vl);
  4751. m <<= 1;
  4752. // load Q8 and take product with Q3
  4753. vint16m2_t a0 = __riscv_vwmul_vv_i16m2(q3_m0, __riscv_vle8_v_i8m1(q8, vl), vl);
  4754. vint16m2_t a1 = __riscv_vwmul_vv_i16m2(q3_m1, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  4755. vint16m2_t a2 = __riscv_vwmul_vv_i16m2(q3_m2, __riscv_vle8_v_i8m1(q8+64, vl), vl);
  4756. vint16m2_t a3 = __riscv_vwmul_vv_i16m2(q3_m3, __riscv_vle8_v_i8m1(q8+96, vl), vl);
  4757. vl = 16;
  4758. // retrieve lane to multiply with scale
  4759. vint32m2_t aux0_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a0, 0), (scale[0]), vl);
  4760. vint32m2_t aux0_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a0, 1), (scale[1]), vl);
  4761. vint32m2_t aux1_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a1, 0), (scale[2]), vl);
  4762. vint32m2_t aux1_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a1, 1), (scale[3]), vl);
  4763. vint32m2_t aux2_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a2, 0), (scale[4]), vl);
  4764. vint32m2_t aux2_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a2, 1), (scale[5]), vl);
  4765. vint32m2_t aux3_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a3, 0), (scale[6]), vl);
  4766. vint32m2_t aux3_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a3, 1), (scale[7]), vl);
  4767. vint32m1_t isum0 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux0_0, aux0_1, vl), vzero, vl);
  4768. vint32m1_t isum1 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux1_0, aux1_1, vl), isum0, vl);
  4769. vint32m1_t isum2 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux2_0, aux2_1, vl), isum1, vl);
  4770. vint32m1_t isum3 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux3_0, aux3_1, vl), isum2, vl);
  4771. sum_t += __riscv_vmv_x_s_i32m1_i32(isum3);
  4772. q3 += 32; q8 += 128; scale += 8;
  4773. }
  4774. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4775. sumf += d*sum_t;
  4776. }
  4777. *s = sumf;
  4778. #else
  4779. // scalar version
  4780. // This function is written like this so the compiler can manage to vectorize most of it
  4781. // Using -Ofast, GCC and clang manage to produce code that is within a factor of 2 or so from the
  4782. // manually vectorized version above. Every other version I tried would run at least 4 times slower.
  4783. // The ideal situation would be if we could just write the code once, and the compiler would
  4784. // automatically produce the best possible set of machine instructions, instead of us having to manually
  4785. // write vectorized versions for AVX, ARM_NEON, etc.
  4786. int8_t aux8[QK_K];
  4787. int16_t aux16[8];
  4788. float sums [8];
  4789. int32_t aux32[8];
  4790. memset(sums, 0, 8*sizeof(float));
  4791. uint32_t auxs[4];
  4792. const int8_t * scales = (const int8_t*)auxs;
  4793. float sumf = 0;
  4794. for (int i = 0; i < nb; ++i) {
  4795. const uint8_t * restrict q3 = x[i].qs;
  4796. const uint8_t * restrict hm = x[i].hmask;
  4797. const int8_t * restrict q8 = y[i].qs;
  4798. memset(aux32, 0, 8*sizeof(int32_t));
  4799. int8_t * restrict a = aux8;
  4800. uint8_t m = 1;
  4801. for (int j = 0; j < QK_K; j += 128) {
  4802. for (int l = 0; l < 32; ++l) a[l] = q3[l] & 3;
  4803. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4804. a += 32; m <<= 1;
  4805. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 2) & 3;
  4806. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4807. a += 32; m <<= 1;
  4808. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 4) & 3;
  4809. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4810. a += 32; m <<= 1;
  4811. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 6) & 3;
  4812. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4813. a += 32; m <<= 1;
  4814. q3 += 32;
  4815. }
  4816. a = aux8;
  4817. memcpy(auxs, x[i].scales, 12);
  4818. uint32_t tmp = auxs[2];
  4819. auxs[2] = ((auxs[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
  4820. auxs[3] = ((auxs[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
  4821. auxs[0] = (auxs[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
  4822. auxs[1] = (auxs[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
  4823. for (int j = 0; j < QK_K/16; ++j) {
  4824. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4825. for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
  4826. q8 += 8; a += 8;
  4827. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4828. for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
  4829. q8 += 8; a += 8;
  4830. }
  4831. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4832. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  4833. }
  4834. for (int l = 0; l < 8; ++l) sumf += sums[l];
  4835. *s = sumf;
  4836. #endif
  4837. }
  4838. #else
  4839. void ggml_vec_dot_q3_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  4840. assert(n % QK_K == 0);
  4841. const block_q3_K * restrict x = vx;
  4842. const block_q8_K * restrict y = vy;
  4843. const int nb = n / QK_K;
  4844. #ifdef __ARM_NEON
  4845. const int32x4_t vzero = vdupq_n_s32(0);
  4846. const uint8x16_t m3b = vdupq_n_u8(0x3);
  4847. const uint8x16_t mh = vdupq_n_u8(4);
  4848. ggml_int8x16x4_t q3bytes;
  4849. uint16_t aux16[2];
  4850. int8_t * scales = (int8_t *)aux16;
  4851. float sum = 0;
  4852. for (int i = 0; i < nb; ++i) {
  4853. ggml_uint8x16x4_t q3h;
  4854. const uint8x8_t hbits = vld1_u8(x[i].hmask);
  4855. const uint8x16_t q3bits = vld1q_u8(x[i].qs);
  4856. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(y[i].qs);
  4857. const uint16_t a = *(const uint16_t *)x[i].scales;
  4858. aux16[0] = a & 0x0f0f;
  4859. aux16[1] = (a >> 4) & 0x0f0f;
  4860. for (int j = 0; j < 4; ++j) scales[j] -= 8;
  4861. int32_t isum = -4*(scales[0] * y[i].bsums[0] + scales[2] * y[i].bsums[1] + scales[1] * y[i].bsums[2] + scales[3] * y[i].bsums[3]);
  4862. const float d = y[i].d * (float)x[i].d;
  4863. const uint8x16_t htmp = vcombine_u8(hbits, vshr_n_u8(hbits, 1));
  4864. q3h.val[0] = vandq_u8(mh, vshlq_n_u8(htmp, 2));
  4865. q3h.val[1] = vandq_u8(mh, htmp);
  4866. q3h.val[2] = vandq_u8(mh, vshrq_n_u8(htmp, 2));
  4867. q3h.val[3] = vandq_u8(mh, vshrq_n_u8(htmp, 4));
  4868. q3bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q3bits, m3b), q3h.val[0]));
  4869. q3bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(vshrq_n_u8(q3bits, 2), m3b), q3h.val[1]));
  4870. q3bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(vshrq_n_u8(q3bits, 4), m3b), q3h.val[2]));
  4871. q3bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q3bits, 6), q3h.val[3]));
  4872. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes.val[0])) * scales[0];
  4873. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes.val[1])) * scales[2];
  4874. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes.val[2])) * scales[1];
  4875. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes.val[3])) * scales[3];
  4876. sum += d * isum;
  4877. }
  4878. *s = sum;
  4879. #elif defined __AVX2__
  4880. const __m256i m3 = _mm256_set1_epi8(3);
  4881. const __m256i m1 = _mm256_set1_epi8(1);
  4882. __m256 acc = _mm256_setzero_ps();
  4883. uint64_t aux64;
  4884. uint16_t aux16[2];
  4885. const int8_t * aux8 = (const int8_t *)aux16;
  4886. for (int i = 0; i < nb; ++i) {
  4887. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4888. const uint8_t * restrict q3 = x[i].qs;
  4889. const int8_t * restrict q8 = y[i].qs;
  4890. const uint16_t a = *(const uint16_t *)x[i].scales;
  4891. aux16[0] = a & 0x0f0f;
  4892. aux16[1] = (a >> 4) & 0x0f0f;
  4893. const __m256i scale_0 = MM256_SET_M128I(_mm_set1_epi16(aux8[2] - 8), _mm_set1_epi16(aux8[0] - 8));
  4894. const __m256i scale_1 = MM256_SET_M128I(_mm_set1_epi16(aux8[3] - 8), _mm_set1_epi16(aux8[1] - 8));
  4895. memcpy(&aux64, x[i].hmask, 8);
  4896. const __m128i haux = _mm_set_epi64x(aux64 >> 1, aux64 >> 0);
  4897. __m256i q3h_0 = MM256_SET_M128I(_mm_srli_epi16(haux, 2), haux);
  4898. __m256i q3h_1 = _mm256_srli_epi16(q3h_0, 4);
  4899. q3h_0 = _mm256_slli_epi16(_mm256_andnot_si256(q3h_0, m1), 2);
  4900. q3h_1 = _mm256_slli_epi16(_mm256_andnot_si256(q3h_1, m1), 2);
  4901. // load low 2 bits
  4902. const __m128i q3bits = _mm_loadu_si128((const __m128i*)q3);
  4903. // prepare low and high bits
  4904. const __m256i q3aux = MM256_SET_M128I(_mm_srli_epi16(q3bits, 2), q3bits);
  4905. const __m256i q3l_0 = _mm256_and_si256(q3aux, m3);
  4906. const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3aux, 4), m3);
  4907. // load Q8 quants
  4908. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4909. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4910. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  4911. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4912. // and 2 if the high bit was set)
  4913. const __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0);
  4914. const __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1);
  4915. __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0);
  4916. __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1);
  4917. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  4918. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  4919. // multiply with scales
  4920. p16_0 = _mm256_madd_epi16(scale_0, p16_0);
  4921. p16_1 = _mm256_madd_epi16(scale_1, p16_1);
  4922. p16_0 = _mm256_add_epi32(p16_0, p16_1);
  4923. // multiply with block scale and accumulate
  4924. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(p16_0), acc);
  4925. }
  4926. *s = hsum_float_8(acc);
  4927. #elif defined __AVX__
  4928. const __m128i m3 = _mm_set1_epi8(3);
  4929. const __m128i m1 = _mm_set1_epi8(1);
  4930. __m256 acc = _mm256_setzero_ps();
  4931. uint64_t aux64;
  4932. uint16_t aux16[2];
  4933. const int8_t * aux8 = (const int8_t *)aux16;
  4934. for (int i = 0; i < nb; ++i) {
  4935. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4936. const uint8_t * restrict q3 = x[i].qs;
  4937. const int8_t * restrict q8 = y[i].qs;
  4938. const uint16_t a = *(const uint16_t *)x[i].scales;
  4939. aux16[0] = a & 0x0f0f;
  4940. aux16[1] = (a >> 4) & 0x0f0f;
  4941. const __m128i scale_0 = _mm_set1_epi16(aux8[0] - 8);
  4942. const __m128i scale_1 = _mm_set1_epi16(aux8[2] - 8);
  4943. const __m128i scale_2 = _mm_set1_epi16(aux8[1] - 8);
  4944. const __m128i scale_3 = _mm_set1_epi16(aux8[3] - 8);
  4945. memcpy(&aux64, x[i].hmask, 8);
  4946. __m128i q3h_0 = _mm_set_epi64x(aux64 >> 1, aux64 >> 0);
  4947. __m128i q3h_1 = _mm_srli_epi16(q3h_0, 2);
  4948. __m128i q3h_2 = _mm_srli_epi16(q3h_0, 4);
  4949. __m128i q3h_3 = _mm_srli_epi16(q3h_0, 6);
  4950. q3h_0 = _mm_slli_epi16(_mm_andnot_si128(q3h_0, m1), 2);
  4951. q3h_1 = _mm_slli_epi16(_mm_andnot_si128(q3h_1, m1), 2);
  4952. q3h_2 = _mm_slli_epi16(_mm_andnot_si128(q3h_2, m1), 2);
  4953. q3h_3 = _mm_slli_epi16(_mm_andnot_si128(q3h_3, m1), 2);
  4954. // load low 2 bits
  4955. const __m128i q3bits = _mm_loadu_si128((const __m128i*)q3);
  4956. // prepare low and high bits
  4957. const __m128i q3l_0 = _mm_and_si128(q3bits, m3);
  4958. const __m128i q3l_1 = _mm_and_si128(_mm_srli_epi16(q3bits, 2), m3);
  4959. const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits, 4), m3);
  4960. const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits, 6), m3);
  4961. // load Q8 quants
  4962. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4963. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4964. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm_maddubs_epi16,
  4965. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4966. // and 2 if the high bit was set)
  4967. const __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, _mm256_extractf128_si256(q8_0, 0));
  4968. const __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, _mm256_extractf128_si256(q8_0, 1));
  4969. const __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, _mm256_extractf128_si256(q8_1, 0));
  4970. const __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, _mm256_extractf128_si256(q8_1, 1));
  4971. __m128i p16_0 = _mm_maddubs_epi16(q3l_0, _mm256_extractf128_si256(q8_0, 0));
  4972. __m128i p16_1 = _mm_maddubs_epi16(q3l_1, _mm256_extractf128_si256(q8_0, 1));
  4973. __m128i p16_2 = _mm_maddubs_epi16(q3l_2, _mm256_extractf128_si256(q8_1, 0));
  4974. __m128i p16_3 = _mm_maddubs_epi16(q3l_3, _mm256_extractf128_si256(q8_1, 1));
  4975. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  4976. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  4977. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  4978. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  4979. // multiply with scales
  4980. p16_0 = _mm_madd_epi16(scale_0, p16_0);
  4981. p16_1 = _mm_madd_epi16(scale_1, p16_1);
  4982. p16_2 = _mm_madd_epi16(scale_2, p16_2);
  4983. p16_3 = _mm_madd_epi16(scale_3, p16_3);
  4984. p16_0 = _mm_add_epi32(p16_0, p16_2);
  4985. p16_1 = _mm_add_epi32(p16_1, p16_3);
  4986. __m256i p16 = MM256_SET_M128I(p16_1, p16_0);
  4987. // multiply with block scale and accumulate
  4988. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(p16)), acc);
  4989. }
  4990. *s = hsum_float_8(acc);
  4991. #elif defined __riscv_v_intrinsic
  4992. uint16_t aux16[2];
  4993. int8_t * scales = (int8_t *)aux16;
  4994. float sumf = 0;
  4995. for (int i = 0; i < nb; ++i) {
  4996. const uint8_t * restrict q3 = x[i].qs;
  4997. const int8_t * restrict q8 = y[i].qs;
  4998. const uint16_t a = *(const uint16_t *)x[i].scales;
  4999. aux16[0] = a & 0x0f0f;
  5000. aux16[1] = (a >> 4) & 0x0f0f;
  5001. for (int j = 0; j < 4; ++j) scales[j] -= 8;
  5002. int32_t isum = -4*(scales[0] * y[i].bsums[0] + scales[2] * y[i].bsums[1] + scales[1] * y[i].bsums[2] + scales[3] * y[i].bsums[3]);
  5003. const float d = y[i].d * (float)x[i].d;
  5004. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5005. // load qh
  5006. vuint8mf4_t qh_x1 = __riscv_vle8_v_u8mf4(x[i].hmask, 8);
  5007. vuint8mf2_t qh_x2 = __riscv_vlmul_ext_v_u8mf4_u8mf2(__riscv_vsrl_vx_u8mf4(qh_x1, 1, 8));
  5008. size_t vl = 16;
  5009. // extend and combine both qh_x1 and qh_x2
  5010. vuint8mf2_t qh_x = __riscv_vslideup_vx_u8mf2(__riscv_vlmul_ext_v_u8mf4_u8mf2(qh_x1), qh_x2, vl/2, vl);
  5011. vuint8mf2_t qh_0 = __riscv_vand_vx_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x2, vl), 0x4, vl);
  5012. vuint8mf2_t qh_1 = __riscv_vand_vx_u8mf2(qh_x, 0x4, vl);
  5013. vuint8mf2_t qh_2 = __riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl), 0x4, vl);
  5014. vuint8mf2_t qh_3 = __riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x4, vl), 0x4, vl);
  5015. // load Q3
  5016. vuint8mf2_t q3_x = __riscv_vle8_v_u8mf2(q3, vl);
  5017. vuint8mf2_t q3h_0 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q3_x, 0x3, vl), qh_0, vl);
  5018. vuint8mf2_t q3h_1 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q3_x, 2, vl), 0x3, vl), qh_1, vl);
  5019. vuint8mf2_t q3h_2 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q3_x, 4, vl), 0x3, vl), qh_2, vl);
  5020. vuint8mf2_t q3h_3 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q3_x, 0x6, vl), qh_3, vl);
  5021. vint8mf2_t q3_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_0);
  5022. vint8mf2_t q3_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_1);
  5023. vint8mf2_t q3_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_2);
  5024. vint8mf2_t q3_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_3);
  5025. // load Q8 and take product with Q3
  5026. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q3_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  5027. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q3_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  5028. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q3_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  5029. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q3_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  5030. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  5031. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  5032. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  5033. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  5034. isum += __riscv_vmv_x_s_i32m1_i32(vs_0) * scales[0];
  5035. isum += __riscv_vmv_x_s_i32m1_i32(vs_1) * scales[2];
  5036. isum += __riscv_vmv_x_s_i32m1_i32(vs_2) * scales[1];
  5037. isum += __riscv_vmv_x_s_i32m1_i32(vs_3) * scales[3];
  5038. sumf += d * isum;
  5039. }
  5040. *s = sumf;
  5041. #else
  5042. int8_t aux8[QK_K];
  5043. int16_t aux16[8];
  5044. float sums [8];
  5045. int32_t aux32[8];
  5046. int32_t scales[4];
  5047. memset(sums, 0, 8*sizeof(float));
  5048. float sumf = 0;
  5049. for (int i = 0; i < nb; ++i) {
  5050. const uint8_t * restrict q3 = x[i].qs;
  5051. const uint8_t * restrict hm = x[i].hmask;
  5052. const int8_t * restrict q8 = y[i].qs;
  5053. int8_t * restrict a = aux8;
  5054. for (int l = 0; l < 8; ++l) {
  5055. a[l+ 0] = (int8_t)((q3[l+0] >> 0) & 3) - (hm[l] & 0x01 ? 0 : 4);
  5056. a[l+ 8] = (int8_t)((q3[l+8] >> 0) & 3) - (hm[l] & 0x02 ? 0 : 4);
  5057. a[l+16] = (int8_t)((q3[l+0] >> 2) & 3) - (hm[l] & 0x04 ? 0 : 4);
  5058. a[l+24] = (int8_t)((q3[l+8] >> 2) & 3) - (hm[l] & 0x08 ? 0 : 4);
  5059. a[l+32] = (int8_t)((q3[l+0] >> 4) & 3) - (hm[l] & 0x10 ? 0 : 4);
  5060. a[l+40] = (int8_t)((q3[l+8] >> 4) & 3) - (hm[l] & 0x20 ? 0 : 4);
  5061. a[l+48] = (int8_t)((q3[l+0] >> 6) & 3) - (hm[l] & 0x40 ? 0 : 4);
  5062. a[l+56] = (int8_t)((q3[l+8] >> 6) & 3) - (hm[l] & 0x80 ? 0 : 4);
  5063. }
  5064. scales[0] = (x[i].scales[0] & 0xF) - 8;
  5065. scales[1] = (x[i].scales[0] >> 4) - 8;
  5066. scales[2] = (x[i].scales[1] & 0xF) - 8;
  5067. scales[3] = (x[i].scales[1] >> 4) - 8;
  5068. memset(aux32, 0, 8*sizeof(int32_t));
  5069. for (int j = 0; j < QK_K/16; ++j) {
  5070. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5071. q8 += 8; a += 8;
  5072. for (int l = 0; l < 8; ++l) aux16[l] += q8[l] * a[l];
  5073. q8 += 8; a += 8;
  5074. for (int l = 0; l < 8; ++l) aux32[l] += scales[j] * aux16[l];
  5075. }
  5076. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5077. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5078. }
  5079. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5080. *s = sumf;
  5081. #endif
  5082. }
  5083. #endif
  5084. #if QK_K == 256
  5085. void ggml_vec_dot_q4_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  5086. assert(n % QK_K == 0);
  5087. const block_q4_K * restrict x = vx;
  5088. const block_q8_K * restrict y = vy;
  5089. const int nb = n / QK_K;
  5090. static const uint32_t kmask1 = 0x3f3f3f3f;
  5091. static const uint32_t kmask2 = 0x0f0f0f0f;
  5092. static const uint32_t kmask3 = 0x03030303;
  5093. uint32_t utmp[4];
  5094. #ifdef __ARM_NEON
  5095. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5096. const int32x4_t mzero = vdupq_n_s32(0);
  5097. ggml_int8x16x2_t q4bytes;
  5098. ggml_int8x16x2_t q8bytes;
  5099. float sumf = 0;
  5100. for (int i = 0; i < nb; ++i) {
  5101. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5102. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5103. const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
  5104. memcpy(utmp, x[i].scales, 12);
  5105. uint32x2_t mins8 = { 0 };
  5106. mins8 = vset_lane_u32(utmp[1] & kmask1, mins8, 0);
  5107. mins8 = vset_lane_u32(((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4), mins8, 1);
  5108. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5109. utmp[0] &= kmask1;
  5110. const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(vreinterpret_u8_u32(mins8)));
  5111. const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
  5112. vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
  5113. sumf -= dmin * vaddvq_s32(prod);
  5114. const uint8_t * scales = (const uint8_t *)utmp;
  5115. const uint8_t * restrict q4 = x[i].qs;
  5116. const int8_t * restrict q8 = y[i].qs;
  5117. int32_t sumi1 = 0;
  5118. int32_t sumi2 = 0;
  5119. for (int j = 0; j < QK_K/64; ++j) {
  5120. const ggml_uint8x16x2_t q4bits = ggml_vld1q_u8_x2(q4); q4 += 32;
  5121. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  5122. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  5123. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  5124. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5125. sumi1 += vaddvq_s32(p1) * scales[2*j+0];
  5126. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  5127. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  5128. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  5129. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5130. sumi2 += vaddvq_s32(p2) * scales[2*j+1];
  5131. }
  5132. sumf += d * (sumi1 + sumi2);
  5133. }
  5134. *s = sumf;
  5135. #elif defined __AVX2__
  5136. const __m256i m4 = _mm256_set1_epi8(0xF);
  5137. __m256 acc = _mm256_setzero_ps();
  5138. __m128 acc_m = _mm_setzero_ps();
  5139. for (int i = 0; i < nb; ++i) {
  5140. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5141. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5142. memcpy(utmp, x[i].scales, 12);
  5143. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5144. const uint32_t uaux = utmp[1] & kmask1;
  5145. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5146. utmp[2] = uaux;
  5147. utmp[0] &= kmask1;
  5148. const uint8_t * restrict q4 = x[i].qs;
  5149. const int8_t * restrict q8 = y[i].qs;
  5150. const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
  5151. const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
  5152. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  5153. const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
  5154. acc_m = _mm_fmadd_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod), acc_m);
  5155. const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
  5156. const __m256i scales = MM256_SET_M128I(sc128, sc128);
  5157. __m256i sumi = _mm256_setzero_si256();
  5158. for (int j = 0; j < QK_K/64; ++j) {
  5159. const __m256i scale_l = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
  5160. const __m256i scale_h = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
  5161. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  5162. const __m256i q4l = _mm256_and_si256(q4bits, m4);
  5163. const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
  5164. const __m256i q8l = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5165. __m256i p16l = _mm256_maddubs_epi16(q4l, q8l);
  5166. p16l = _mm256_madd_epi16(scale_l, p16l);
  5167. const __m256i q8h = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5168. __m256i p16h = _mm256_maddubs_epi16(q4h, q8h);
  5169. p16h = _mm256_madd_epi16(scale_h, p16h);
  5170. const __m256i sumj = _mm256_add_epi32(p16l, p16h);
  5171. sumi = _mm256_add_epi32(sumi, sumj);
  5172. }
  5173. __m256 vd = _mm256_set1_ps(d);
  5174. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
  5175. }
  5176. acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
  5177. acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
  5178. *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
  5179. #elif defined __AVX__
  5180. const __m128i m4 = _mm_set1_epi8(0xF);
  5181. const __m128i m2 = _mm_set1_epi8(0x2);
  5182. __m256 acc = _mm256_setzero_ps();
  5183. __m128 acc_m = _mm_setzero_ps();
  5184. for (int i = 0; i < nb; ++i) {
  5185. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5186. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5187. const uint8_t * restrict q4 = x[i].qs;
  5188. const int8_t * restrict q8 = y[i].qs;
  5189. memcpy(utmp, x[i].scales, 12);
  5190. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5191. const uint32_t uaux = utmp[1] & kmask1;
  5192. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5193. utmp[2] = uaux;
  5194. utmp[0] &= kmask1;
  5195. const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
  5196. const __m128i scales = _mm_cvtepu8_epi16(utmps);
  5197. const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
  5198. const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
  5199. const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
  5200. const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
  5201. const __m128i prod = _mm_madd_epi16(mins, q8s);
  5202. acc_m = _mm_add_ps(_mm_mul_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod)), acc_m);
  5203. __m128i sumi_0 = _mm_setzero_si128();
  5204. __m128i sumi_1 = _mm_setzero_si128();
  5205. __m128i shuffle = _mm_set1_epi16(0x0100);
  5206. for (int j = 0; j < QK_K/64; ++j) {
  5207. const __m128i scale_l = _mm_shuffle_epi8(scales, shuffle);
  5208. shuffle = _mm_add_epi16(shuffle, m2);
  5209. const __m128i scale_h = _mm_shuffle_epi8(scales, shuffle);
  5210. shuffle = _mm_add_epi16(shuffle, m2);
  5211. __m128i q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5212. const __m128i q4l_0 = _mm_and_si128(q4bits, m4);
  5213. const __m128i q4h_0 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
  5214. q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5215. const __m128i q4l_1 = _mm_and_si128(q4bits, m4);
  5216. const __m128i q4h_1 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
  5217. const __m128i q8l_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5218. __m128i p16l = _mm_maddubs_epi16(q4l_0, q8l_0);
  5219. p16l = _mm_madd_epi16(scale_l, p16l);
  5220. sumi_0 = _mm_add_epi32(sumi_0, p16l);
  5221. const __m128i q8l_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5222. p16l = _mm_maddubs_epi16(q4l_1, q8l_1);
  5223. p16l = _mm_madd_epi16(scale_l, p16l);
  5224. sumi_1 = _mm_add_epi32(sumi_1, p16l);
  5225. const __m128i q8h_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5226. __m128i p16h = _mm_maddubs_epi16(q4h_0, q8h_0);
  5227. p16h = _mm_madd_epi16(scale_h, p16h);
  5228. sumi_0 = _mm_add_epi32(sumi_0, p16h);
  5229. const __m128i q8h_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5230. p16h = _mm_maddubs_epi16(q4h_1, q8h_1);
  5231. p16h = _mm_madd_epi16(scale_h, p16h);
  5232. sumi_1 = _mm_add_epi32(sumi_1, p16h);
  5233. }
  5234. __m256 vd = _mm256_set1_ps(d);
  5235. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  5236. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
  5237. }
  5238. acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
  5239. acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
  5240. *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
  5241. #elif defined __riscv_v_intrinsic
  5242. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5243. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5244. float sumf = 0;
  5245. for (int i = 0; i < nb; ++i) {
  5246. size_t vl = 8;
  5247. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5248. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5249. vint16mf2_t q8sums_0 = __riscv_vlse16_v_i16mf2(y[i].bsums, 4, vl);
  5250. vint16mf2_t q8sums_1 = __riscv_vlse16_v_i16mf2(y[i].bsums+1, 4, vl);
  5251. vint16mf2_t q8sums = __riscv_vadd_vv_i16mf2(q8sums_0, q8sums_1, vl);
  5252. memcpy(utmp, x[i].scales, 12);
  5253. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5254. const uint32_t uaux = utmp[1] & kmask1;
  5255. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5256. utmp[2] = uaux;
  5257. utmp[0] &= kmask1;
  5258. vuint8mf4_t mins8 = __riscv_vle8_v_u8mf4(mins, vl);
  5259. vint16mf2_t v_mins = __riscv_vreinterpret_v_u16mf2_i16mf2(__riscv_vzext_vf2_u16mf2(mins8, vl));
  5260. vint32m1_t prod = __riscv_vwmul_vv_i32m1(q8sums, v_mins, vl);
  5261. vint32m1_t sumi = __riscv_vredsum_vs_i32m1_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  5262. sumf -= dmin * __riscv_vmv_x_s_i32m1_i32(sumi);
  5263. const uint8_t * restrict q4 = x[i].qs;
  5264. const int8_t * restrict q8 = y[i].qs;
  5265. vl = 32;
  5266. int32_t sum_1 = 0;
  5267. int32_t sum_2 = 0;
  5268. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  5269. for (int j = 0; j < QK_K/64; ++j) {
  5270. // load Q4
  5271. vuint8m1_t q4_x = __riscv_vle8_v_u8m1(q4, vl);
  5272. // load Q8 and multiply it with lower Q4 nibble
  5273. vint8m1_t q8_0 = __riscv_vle8_v_i8m1(q8, vl);
  5274. vint8m1_t q4_0 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q4_x, 0x0F, vl));
  5275. vint16m2_t qv_0 = __riscv_vwmul_vv_i16m2(q4_0, q8_0, vl);
  5276. vint16m1_t vs_0 = __riscv_vredsum_vs_i16m2_i16m1(qv_0, vzero, vl);
  5277. sum_1 += __riscv_vmv_x_s_i16m1_i16(vs_0) * scales[2*j+0];
  5278. // load Q8 and multiply it with upper Q4 nibble
  5279. vint8m1_t q8_1 = __riscv_vle8_v_i8m1(q8+32, vl);
  5280. vint8m1_t q4_1 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q4_x, 0x04, vl));
  5281. vint16m2_t qv_1 = __riscv_vwmul_vv_i16m2(q4_1, q8_1, vl);
  5282. vint16m1_t vs_1 = __riscv_vredsum_vs_i16m2_i16m1(qv_1, vzero, vl);
  5283. sum_2 += __riscv_vmv_x_s_i16m1_i16(vs_1) * scales[2*j+1];
  5284. q4 += 32; q8 += 64;
  5285. }
  5286. sumf += d*(sum_1 + sum_2);
  5287. }
  5288. *s = sumf;
  5289. #else
  5290. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5291. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5292. int8_t aux8[QK_K];
  5293. int16_t aux16[8];
  5294. float sums [8];
  5295. int32_t aux32[8];
  5296. memset(sums, 0, 8*sizeof(float));
  5297. float sumf = 0;
  5298. for (int i = 0; i < nb; ++i) {
  5299. const uint8_t * restrict q4 = x[i].qs;
  5300. const int8_t * restrict q8 = y[i].qs;
  5301. memset(aux32, 0, 8*sizeof(int32_t));
  5302. int8_t * restrict a = aux8;
  5303. for (int j = 0; j < QK_K/64; ++j) {
  5304. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
  5305. a += 32;
  5306. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] >> 4);
  5307. a += 32; q4 += 32;
  5308. }
  5309. memcpy(utmp, x[i].scales, 12);
  5310. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5311. const uint32_t uaux = utmp[1] & kmask1;
  5312. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5313. utmp[2] = uaux;
  5314. utmp[0] &= kmask1;
  5315. int sumi = 0;
  5316. for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
  5317. a = aux8;
  5318. int is = 0;
  5319. for (int j = 0; j < QK_K/32; ++j) {
  5320. int32_t scale = scales[is++];
  5321. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5322. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5323. q8 += 8; a += 8;
  5324. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5325. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5326. q8 += 8; a += 8;
  5327. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5328. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5329. q8 += 8; a += 8;
  5330. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5331. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5332. q8 += 8; a += 8;
  5333. }
  5334. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5335. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5336. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5337. sumf -= dmin * sumi;
  5338. }
  5339. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5340. *s = sumf;
  5341. #endif
  5342. }
  5343. #else
  5344. void ggml_vec_dot_q4_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  5345. assert(n % QK_K == 0);
  5346. const block_q4_K * restrict x = vx;
  5347. const block_q8_K * restrict y = vy;
  5348. const int nb = n / QK_K;
  5349. #ifdef __ARM_NEON
  5350. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5351. const int32x4_t mzero = vdupq_n_s32(0);
  5352. float sumf = 0;
  5353. ggml_int8x16x2_t q4bytes;
  5354. ggml_int8x16x4_t q8bytes;
  5355. float sum_mins = 0.f;
  5356. uint16_t aux16[2];
  5357. const uint8_t * restrict scales = (const uint8_t *)aux16;
  5358. for (int i = 0; i < nb; ++i) {
  5359. const uint8_t * restrict q4 = x[i].qs;
  5360. const int8_t * restrict q8 = y[i].qs;
  5361. const uint16_t * restrict a = (const uint16_t *)x[i].scales;
  5362. aux16[0] = a[0] & 0x0f0f;
  5363. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5364. const int32_t summi = scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]);
  5365. sum_mins += y[i].d * (float)x[i].d[1] * summi;
  5366. const float d = y[i].d * (float)x[i].d[0];
  5367. const ggml_uint8x16x2_t q4bits = ggml_vld1q_u8_x2(q4);
  5368. q8bytes = ggml_vld1q_s8_x4(q8);
  5369. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  5370. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  5371. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5372. const int32_t sumi1 = vaddvq_s32(p1) * scales[0];
  5373. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  5374. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  5375. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[2]), q4bytes.val[1], q8bytes.val[3]);
  5376. const int32_t sumi2 = vaddvq_s32(p2) * scales[1];
  5377. sumf += d * (sumi1 + sumi2);
  5378. }
  5379. *s = sumf - sum_mins;
  5380. #elif defined __AVX2__
  5381. const __m256i m4 = _mm256_set1_epi8(0xF);
  5382. __m256 acc = _mm256_setzero_ps();
  5383. float summs = 0;
  5384. uint16_t aux16[2];
  5385. const uint8_t * scales = (const uint8_t *)aux16;
  5386. for (int i = 0; i < nb; ++i) {
  5387. const float d = GGML_FP16_TO_FP32(x[i].d[0]) * y[i].d;
  5388. const float m = GGML_FP16_TO_FP32(x[i].d[1]) * y[i].d;
  5389. const __m256 vd = _mm256_set1_ps(d);
  5390. const uint16_t * a = (const uint16_t *)x[i].scales;
  5391. aux16[0] = a[0] & 0x0f0f;
  5392. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5393. summs += m * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  5394. const uint8_t * restrict q4 = x[i].qs;
  5395. const int8_t * restrict q8 = y[i].qs;
  5396. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4);
  5397. const __m256i q4l = _mm256_and_si256(q4bits, m4);
  5398. const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
  5399. const __m256i q8l = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5400. const __m256i q8h = _mm256_loadu_si256((const __m256i*)(q8+32));
  5401. const __m256i p16l = _mm256_maddubs_epi16(q4l, q8l);
  5402. const __m256i p16h = _mm256_maddubs_epi16(q4h, q8h);
  5403. const __m256i p32l = _mm256_madd_epi16(_mm256_set1_epi16(scales[0]), p16l);
  5404. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(p32l), acc);
  5405. const __m256i p32h = _mm256_madd_epi16(_mm256_set1_epi16(scales[1]), p16h);
  5406. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(p32h), acc);
  5407. }
  5408. *s = hsum_float_8(acc) - summs;
  5409. #elif defined __AVX__
  5410. const __m128i m4 = _mm_set1_epi8(0xF);
  5411. __m256 acc = _mm256_setzero_ps();
  5412. float summs = 0;
  5413. uint16_t aux16[2];
  5414. const uint8_t * scales = (const uint8_t *)aux16;
  5415. for (int i = 0; i < nb; ++i) {
  5416. const float d = GGML_FP16_TO_FP32(x[i].d[0]) * y[i].d;
  5417. const float m = GGML_FP16_TO_FP32(x[i].d[1]) * y[i].d;
  5418. const __m256 vd = _mm256_set1_ps(d);
  5419. const uint16_t * a = (const uint16_t *)x[i].scales;
  5420. aux16[0] = a[0] & 0x0f0f;
  5421. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5422. summs += m * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  5423. const uint8_t * restrict q4 = x[i].qs;
  5424. const int8_t * restrict q8 = y[i].qs;
  5425. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4);
  5426. const __m128i q4bits_0 = _mm256_extractf128_si256(q4bits, 0);
  5427. const __m128i q4bits_1 = _mm256_extractf128_si256(q4bits, 1);
  5428. const __m128i q4_0 = _mm_and_si128(q4bits_0, m4);
  5429. const __m128i q4_1 = _mm_and_si128(q4bits_1, m4);
  5430. const __m128i q4_2 = _mm_and_si128(_mm_srli_epi16(q4bits_0, 4), m4);
  5431. const __m128i q4_3 = _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4);
  5432. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5433. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5434. const __m128i p16_0 = _mm_maddubs_epi16(q4_0, _mm256_extractf128_si256(q8_0, 0));
  5435. const __m128i p16_1 = _mm_maddubs_epi16(q4_1, _mm256_extractf128_si256(q8_0, 1));
  5436. const __m128i p16_2 = _mm_maddubs_epi16(q4_2, _mm256_extractf128_si256(q8_1, 0));
  5437. const __m128i p16_3 = _mm_maddubs_epi16(q4_3, _mm256_extractf128_si256(q8_1, 1));
  5438. const __m128i p32_0 = _mm_madd_epi16(_mm_set1_epi16(scales[0]), p16_0);
  5439. const __m128i p32_1 = _mm_madd_epi16(_mm_set1_epi16(scales[0]), p16_1);
  5440. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(MM256_SET_M128I(p32_1, p32_0))), acc);
  5441. const __m128i p32_2 = _mm_madd_epi16(_mm_set1_epi16(scales[1]), p16_2);
  5442. const __m128i p32_3 = _mm_madd_epi16(_mm_set1_epi16(scales[1]), p16_3);
  5443. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(MM256_SET_M128I(p32_3, p32_2))), acc);
  5444. }
  5445. *s = hsum_float_8(acc) - summs;
  5446. #elif defined __riscv_v_intrinsic
  5447. uint16_t s16[2];
  5448. const uint8_t * restrict scales = (const uint8_t *)s16;
  5449. float sumf = 0;
  5450. for (int i = 0; i < nb; ++i) {
  5451. const uint8_t * restrict q4 = x[i].qs;
  5452. const int8_t * restrict q8 = y[i].qs;
  5453. const uint16_t * restrict b = (const uint16_t *)x[i].scales;
  5454. s16[0] = b[0] & 0x0f0f;
  5455. s16[1] = (b[0] >> 4) & 0x0f0f;
  5456. sumf -= y[i].d * GGML_FP16_TO_FP32(x[i].d[1]) * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  5457. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  5458. size_t vl = 32;
  5459. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  5460. // load Q4
  5461. vuint8m1_t q4_x = __riscv_vle8_v_u8m1(q4, vl);
  5462. // load Q8 and multiply it with lower Q4 nibble
  5463. vint8m1_t q4_a = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q4_x, 0x0F, vl));
  5464. vint16m2_t va_0 = __riscv_vwmul_vv_i16m2(q4_a, __riscv_vle8_v_i8m1(q8, vl), vl);
  5465. vint16m1_t aux1 = __riscv_vredsum_vs_i16m2_i16m1(va_0, vzero, vl);
  5466. sumf += d*scales[0]*__riscv_vmv_x_s_i16m1_i16(aux1);
  5467. // load Q8 and multiply it with upper Q4 nibble
  5468. vint8m1_t q4_s = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q4_x, 0x04, vl));
  5469. vint16m2_t va_1 = __riscv_vwmul_vv_i16m2(q4_s, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  5470. vint16m1_t aux2 = __riscv_vredsum_vs_i16m2_i16m1(va_1, vzero, vl);
  5471. sumf += d*scales[1]*__riscv_vmv_x_s_i16m1_i16(aux2);
  5472. }
  5473. *s = sumf;
  5474. #else
  5475. uint8_t aux8[QK_K];
  5476. int16_t aux16[16];
  5477. float sums [8];
  5478. memset(sums, 0, 8*sizeof(float));
  5479. uint16_t s16[2];
  5480. const uint8_t * restrict scales = (const uint8_t *)s16;
  5481. float sumf = 0;
  5482. for (int i = 0; i < nb; ++i) {
  5483. const uint8_t * restrict q4 = x[i].qs;
  5484. const int8_t * restrict q8 = y[i].qs;
  5485. uint8_t * restrict a = aux8;
  5486. for (int l = 0; l < 32; ++l) a[l+ 0] = q4[l] & 0xF;
  5487. for (int l = 0; l < 32; ++l) a[l+32] = q4[l] >> 4;
  5488. const uint16_t * restrict b = (const uint16_t *)x[i].scales;
  5489. s16[0] = b[0] & 0x0f0f;
  5490. s16[1] = (b[0] >> 4) & 0x0f0f;
  5491. sumf -= y[i].d * GGML_FP16_TO_FP32(x[i].d[1]) * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  5492. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  5493. for (int j = 0; j < QK_K/32; ++j) {
  5494. for (int l = 0; l < 16; ++l) aux16[l] = q8[l] * a[l];
  5495. q8 += 16; a += 16;
  5496. for (int l = 0; l < 16; ++l) aux16[l] += q8[l] * a[l];
  5497. q8 += 16; a += 16;
  5498. const float dl = d * scales[j];
  5499. for (int l = 0; l < 8; ++l) sums[l] += dl * (aux16[l] + aux16[l+8]);
  5500. }
  5501. }
  5502. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5503. *s = sumf;
  5504. #endif
  5505. }
  5506. #endif
  5507. #if QK_K == 256
  5508. void ggml_vec_dot_q5_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  5509. assert(n % QK_K == 0);
  5510. const block_q5_K * restrict x = vx;
  5511. const block_q8_K * restrict y = vy;
  5512. const int nb = n / QK_K;
  5513. static const uint32_t kmask1 = 0x3f3f3f3f;
  5514. static const uint32_t kmask2 = 0x0f0f0f0f;
  5515. static const uint32_t kmask3 = 0x03030303;
  5516. uint32_t utmp[4];
  5517. #ifdef __ARM_NEON
  5518. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5519. const uint8x16_t mone = vdupq_n_u8(1);
  5520. const uint8x16_t mtwo = vdupq_n_u8(2);
  5521. const int32x4_t mzero = vdupq_n_s32(0);
  5522. ggml_int8x16x4_t q5bytes;
  5523. float sumf = 0;
  5524. for (int i = 0; i < nb; ++i) {
  5525. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5526. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5527. const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
  5528. memcpy(utmp, x[i].scales, 12);
  5529. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5530. const uint32_t uaux = utmp[1] & kmask1;
  5531. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5532. utmp[2] = uaux;
  5533. utmp[0] &= kmask1;
  5534. const uint8x8_t mins8 = vld1_u8((const uint8_t*)utmp + 8);
  5535. const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(mins8));
  5536. const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
  5537. vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
  5538. int32_t sumi_mins = vaddvq_s32(prod);
  5539. const uint8_t * scales = (const uint8_t *)utmp;
  5540. const uint8_t * restrict q5 = x[i].qs;
  5541. const uint8_t * restrict qh = x[i].qh;
  5542. const int8_t * restrict q8 = y[i].qs;
  5543. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh);
  5544. ggml_uint8x16x4_t q5h;
  5545. int32_t sumi = 0;
  5546. for (int j = 0; j < QK_K/64; ++j) {
  5547. const ggml_uint8x16x2_t q5bits = ggml_vld1q_u8_x2(q5); q5 += 32;
  5548. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  5549. q5h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
  5550. q5h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
  5551. q5h.val[2] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[0]), 3);
  5552. q5h.val[3] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[1]), 3);
  5553. qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 2);
  5554. qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 2);
  5555. q5bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[0], m4b), q5h.val[0]));
  5556. q5bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[1], m4b), q5h.val[1]));
  5557. q5bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[0], 4), q5h.val[2]));
  5558. q5bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[1], 4), q5h.val[3]));
  5559. sumi += vaddvq_s32(ggml_vdotq_s32(ggml_vdotq_s32(mzero, q5bytes.val[0], q8bytes.val[0]), q5bytes.val[1], q8bytes.val[1])) * *scales++;
  5560. sumi += vaddvq_s32(ggml_vdotq_s32(ggml_vdotq_s32(mzero, q5bytes.val[2], q8bytes.val[2]), q5bytes.val[3], q8bytes.val[3])) * *scales++;
  5561. }
  5562. sumf += d * sumi - dmin * sumi_mins;
  5563. }
  5564. *s = sumf;
  5565. #elif defined __AVX2__
  5566. const __m256i m4 = _mm256_set1_epi8(0xF);
  5567. const __m128i mzero = _mm_setzero_si128();
  5568. const __m256i mone = _mm256_set1_epi8(1);
  5569. __m256 acc = _mm256_setzero_ps();
  5570. float summs = 0.f;
  5571. for (int i = 0; i < nb; ++i) {
  5572. const uint8_t * restrict q5 = x[i].qs;
  5573. const int8_t * restrict q8 = y[i].qs;
  5574. #if QK_K == 256
  5575. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5576. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5577. memcpy(utmp, x[i].scales, 12);
  5578. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5579. const uint32_t uaux = utmp[1] & kmask1;
  5580. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5581. utmp[2] = uaux;
  5582. utmp[0] &= kmask1;
  5583. #else
  5584. // TODO
  5585. const float d = 0, dmin = 0;
  5586. #endif
  5587. const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
  5588. const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
  5589. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  5590. const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
  5591. const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
  5592. summs += dmin * _mm_extract_epi32(hsum, 0);
  5593. const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
  5594. const __m256i scales = MM256_SET_M128I(sc128, sc128);
  5595. const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].qh);
  5596. __m256i hmask = mone;
  5597. __m256i sumi = _mm256_setzero_si256();
  5598. int bit = 0;
  5599. for (int j = 0; j < QK_K/64; ++j) {
  5600. const __m256i scale_0 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
  5601. const __m256i scale_1 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
  5602. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5); q5 += 32;
  5603. const __m256i q5l_0 = _mm256_and_si256(q5bits, m4);
  5604. const __m256i q5h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
  5605. const __m256i q5_0 = _mm256_add_epi8(q5l_0, q5h_0);
  5606. hmask = _mm256_slli_epi16(hmask, 1);
  5607. const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4);
  5608. const __m256i q5h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
  5609. const __m256i q5_1 = _mm256_add_epi8(q5l_1, q5h_1);
  5610. hmask = _mm256_slli_epi16(hmask, 1);
  5611. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5612. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5613. __m256i p16_0 = _mm256_maddubs_epi16(q5_0, q8_0);
  5614. __m256i p16_1 = _mm256_maddubs_epi16(q5_1, q8_1);
  5615. p16_0 = _mm256_madd_epi16(scale_0, p16_0);
  5616. p16_1 = _mm256_madd_epi16(scale_1, p16_1);
  5617. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  5618. }
  5619. __m256 vd = _mm256_set1_ps(d);
  5620. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
  5621. }
  5622. *s = hsum_float_8(acc) + summs;
  5623. #elif defined __AVX__
  5624. const __m128i m4 = _mm_set1_epi8(0xF);
  5625. const __m128i mzero = _mm_setzero_si128();
  5626. const __m128i mone = _mm_set1_epi8(1);
  5627. const __m128i m2 = _mm_set1_epi8(2);
  5628. __m256 acc = _mm256_setzero_ps();
  5629. float summs = 0.f;
  5630. for (int i = 0; i < nb; ++i) {
  5631. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5632. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5633. const uint8_t * restrict q5 = x[i].qs;
  5634. const int8_t * restrict q8 = y[i].qs;
  5635. memcpy(utmp, x[i].scales, 12);
  5636. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5637. const uint32_t uaux = utmp[1] & kmask1;
  5638. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5639. utmp[2] = uaux;
  5640. utmp[0] &= kmask1;
  5641. const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
  5642. const __m128i scales = _mm_cvtepu8_epi16(utmps);
  5643. const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
  5644. const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
  5645. const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
  5646. const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
  5647. const __m128i prod = _mm_madd_epi16(mins, q8s);
  5648. const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
  5649. summs += dmin * _mm_extract_epi32(hsum, 0);
  5650. const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].qh[0]);
  5651. const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].qh[16]);
  5652. __m128i hmask = mone;
  5653. __m128i sumi_0 = _mm_setzero_si128();
  5654. __m128i sumi_1 = _mm_setzero_si128();
  5655. int bit = 0;
  5656. __m128i shuffle = _mm_set1_epi16(0x0100);
  5657. for (int j = 0; j < QK_K/64; ++j) {
  5658. const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
  5659. shuffle = _mm_add_epi16(shuffle, m2);
  5660. const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
  5661. shuffle = _mm_add_epi16(shuffle, m2);
  5662. const __m128i q5bits_0 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
  5663. const __m128i q5bits_1 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
  5664. __m128i q5l_0 = _mm_and_si128(q5bits_0, m4);
  5665. __m128i q5l_1 = _mm_and_si128(q5bits_1, m4);
  5666. __m128i q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
  5667. __m128i q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
  5668. __m128i q5_0 = _mm_add_epi8(q5l_0, q5h_0);
  5669. __m128i q5_1 = _mm_add_epi8(q5l_1, q5h_1);
  5670. hmask = _mm_slli_epi16(hmask, 1);
  5671. __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5672. __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5673. __m128i p16_0 = _mm_maddubs_epi16(q5_0, q8_0);
  5674. __m128i p16_1 = _mm_maddubs_epi16(q5_1, q8_1);
  5675. p16_0 = _mm_madd_epi16(scale_0, p16_0);
  5676. p16_1 = _mm_madd_epi16(scale_0, p16_1);
  5677. q5l_0 = _mm_and_si128(_mm_srli_epi16(q5bits_0, 4), m4);
  5678. q5l_1 = _mm_and_si128(_mm_srli_epi16(q5bits_1, 4), m4);
  5679. q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
  5680. q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
  5681. q5_0 = _mm_add_epi8(q5l_0, q5h_0);
  5682. q5_1 = _mm_add_epi8(q5l_1, q5h_1);
  5683. hmask = _mm_slli_epi16(hmask, 1);
  5684. q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5685. q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5686. __m128i p16_2 = _mm_maddubs_epi16(q5_0, q8_0);
  5687. __m128i p16_3 = _mm_maddubs_epi16(q5_1, q8_1);
  5688. p16_2 = _mm_madd_epi16(scale_1, p16_2);
  5689. p16_3 = _mm_madd_epi16(scale_1, p16_3);
  5690. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  5691. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  5692. }
  5693. __m256 vd = _mm256_set1_ps(d);
  5694. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  5695. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
  5696. }
  5697. *s = hsum_float_8(acc) + summs;
  5698. #elif defined __riscv_v_intrinsic
  5699. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5700. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5701. float sumf = 0;
  5702. float sums = 0.0;
  5703. size_t vl;
  5704. for (int i = 0; i < nb; ++i) {
  5705. vl = 8;
  5706. const uint8_t * restrict q5 = x[i].qs;
  5707. const uint8_t * restrict hm = x[i].qh;
  5708. const int8_t * restrict q8 = y[i].qs;
  5709. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5710. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5711. vint16mf2_t q8sums_0 = __riscv_vlse16_v_i16mf2(y[i].bsums, 4, vl);
  5712. vint16mf2_t q8sums_1 = __riscv_vlse16_v_i16mf2(y[i].bsums+1, 4, vl);
  5713. vint16mf2_t q8sums = __riscv_vadd_vv_i16mf2(q8sums_0, q8sums_1, vl);
  5714. memcpy(utmp, x[i].scales, 12);
  5715. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5716. const uint32_t uaux = utmp[1] & kmask1;
  5717. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5718. utmp[2] = uaux;
  5719. utmp[0] &= kmask1;
  5720. vuint8mf4_t mins8 = __riscv_vle8_v_u8mf4(mins, vl);
  5721. vint16mf2_t v_mins = __riscv_vreinterpret_v_u16mf2_i16mf2(__riscv_vzext_vf2_u16mf2(mins8, vl));
  5722. vint32m1_t prod = __riscv_vwmul_vv_i32m1(q8sums, v_mins, vl);
  5723. vint32m1_t sumi = __riscv_vredsum_vs_i32m1_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  5724. sumf -= dmin * __riscv_vmv_x_s_i32m1_i32(sumi);
  5725. vl = 32;
  5726. int32_t aux32 = 0;
  5727. int is = 0;
  5728. uint8_t m = 1;
  5729. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5730. vuint8m1_t vqh = __riscv_vle8_v_u8m1(hm, vl);
  5731. for (int j = 0; j < QK_K/64; ++j) {
  5732. // load Q5 and Q8
  5733. vuint8m1_t q5_x = __riscv_vle8_v_u8m1(q5, vl);
  5734. vint8m1_t q8_y1 = __riscv_vle8_v_i8m1(q8, vl);
  5735. vint8m1_t q8_y2 = __riscv_vle8_v_i8m1(q8+32, vl);
  5736. // compute mask for addition
  5737. vint8m1_t q5_a = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q5_x, 0x0F, vl));
  5738. vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
  5739. vbool8_t vmask_1 = __riscv_vmsne_vx_u8m1_b8(qh_m1, 0, vl);
  5740. vint8m1_t q5_m1 = __riscv_vadd_vx_i8m1_m(vmask_1, q5_a, 16, vl);
  5741. m <<= 1;
  5742. vint8m1_t q5_l = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q5_x, 0x04, vl));
  5743. vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
  5744. vbool8_t vmask_2 = __riscv_vmsne_vx_u8m1_b8(qh_m2, 0, vl);
  5745. vint8m1_t q5_m2 = __riscv_vadd_vx_i8m1_m(vmask_2, q5_l, 16, vl);
  5746. m <<= 1;
  5747. vint16m2_t v0 = __riscv_vwmul_vv_i16m2(q5_m1, q8_y1, vl);
  5748. vint16m2_t v1 = __riscv_vwmul_vv_i16m2(q5_m2, q8_y2, vl);
  5749. vint32m4_t vs1 = __riscv_vwmul_vx_i32m4(v0, scales[is++], vl);
  5750. vint32m4_t vs2 = __riscv_vwmul_vx_i32m4(v1, scales[is++], vl);
  5751. vint32m1_t vacc1 = __riscv_vredsum_vs_i32m4_i32m1(vs1, vzero, vl);
  5752. vint32m1_t vacc2 = __riscv_vredsum_vs_i32m4_i32m1(vs2, vzero, vl);
  5753. aux32 += __riscv_vmv_x_s_i32m1_i32(vacc1) + __riscv_vmv_x_s_i32m1_i32(vacc2);
  5754. q5 += 32; q8 += 64;
  5755. }
  5756. vfloat32m1_t vaux = __riscv_vfmul_vf_f32m1(__riscv_vfmv_v_f_f32m1(aux32, 1), d, 1);
  5757. sums += __riscv_vfmv_f_s_f32m1_f32(vaux);
  5758. }
  5759. *s = sumf+sums;
  5760. #else
  5761. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5762. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5763. int8_t aux8[QK_K];
  5764. int16_t aux16[8];
  5765. float sums [8];
  5766. int32_t aux32[8];
  5767. memset(sums, 0, 8*sizeof(float));
  5768. float sumf = 0;
  5769. for (int i = 0; i < nb; ++i) {
  5770. const uint8_t * restrict q4 = x[i].qs;
  5771. const uint8_t * restrict hm = x[i].qh;
  5772. const int8_t * restrict q8 = y[i].qs;
  5773. memset(aux32, 0, 8*sizeof(int32_t));
  5774. int8_t * restrict a = aux8;
  5775. uint8_t m = 1;
  5776. for (int j = 0; j < QK_K/64; ++j) {
  5777. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
  5778. for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
  5779. a += 32; m <<= 1;
  5780. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] >> 4);
  5781. for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
  5782. a += 32; m <<= 1;
  5783. q4 += 32;
  5784. }
  5785. memcpy(utmp, x[i].scales, 12);
  5786. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5787. const uint32_t uaux = utmp[1] & kmask1;
  5788. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5789. utmp[2] = uaux;
  5790. utmp[0] &= kmask1;
  5791. int sumi = 0;
  5792. for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
  5793. a = aux8;
  5794. int is = 0;
  5795. for (int j = 0; j < QK_K/32; ++j) {
  5796. int32_t scale = scales[is++];
  5797. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5798. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5799. q8 += 8; a += 8;
  5800. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5801. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5802. q8 += 8; a += 8;
  5803. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5804. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5805. q8 += 8; a += 8;
  5806. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5807. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5808. q8 += 8; a += 8;
  5809. }
  5810. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5811. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5812. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5813. sumf -= dmin * sumi;
  5814. }
  5815. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5816. *s = sumf;
  5817. #endif
  5818. }
  5819. #else
  5820. void ggml_vec_dot_q5_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  5821. assert(n % QK_K == 0);
  5822. const block_q5_K * restrict x = vx;
  5823. const block_q8_K * restrict y = vy;
  5824. const int nb = n / QK_K;
  5825. #ifdef __ARM_NEON
  5826. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5827. const uint8x16_t mh = vdupq_n_u8(16);
  5828. const int32x4_t mzero = vdupq_n_s32(0);
  5829. ggml_int8x16x4_t q5bytes;
  5830. ggml_uint8x16x4_t q5h;
  5831. float sumf = 0;
  5832. for (int i = 0; i < nb; ++i) {
  5833. const float d = y[i].d * (float)x[i].d;
  5834. const int8_t * sc = x[i].scales;
  5835. const uint8_t * restrict q5 = x[i].qs;
  5836. const uint8_t * restrict qh = x[i].qh;
  5837. const int8_t * restrict q8 = y[i].qs;
  5838. const uint8x8_t qhbits = vld1_u8(qh);
  5839. const ggml_uint8x16x2_t q5bits = ggml_vld1q_u8_x2(q5);
  5840. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  5841. const uint8x16_t htmp = vcombine_u8(qhbits, vshr_n_u8(qhbits, 1));
  5842. q5h.val[0] = vbicq_u8(mh, vshlq_n_u8(htmp, 4));
  5843. q5h.val[1] = vbicq_u8(mh, vshlq_n_u8(htmp, 2));
  5844. q5h.val[2] = vbicq_u8(mh, htmp);
  5845. q5h.val[3] = vbicq_u8(mh, vshrq_n_u8(htmp, 2));
  5846. q5bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q5bits.val[0], m4b)), vreinterpretq_s8_u8(q5h.val[0]));
  5847. q5bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q5bits.val[1], m4b)), vreinterpretq_s8_u8(q5h.val[1]));
  5848. q5bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(q5bits.val[0], 4)), vreinterpretq_s8_u8(q5h.val[2]));
  5849. q5bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(q5bits.val[1], 4)), vreinterpretq_s8_u8(q5h.val[3]));
  5850. int32_t sumi1 = sc[0] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[0], q8bytes.val[0]));
  5851. int32_t sumi2 = sc[1] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[1], q8bytes.val[1]));
  5852. int32_t sumi3 = sc[2] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[2], q8bytes.val[2]));
  5853. int32_t sumi4 = sc[3] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[3], q8bytes.val[3]));
  5854. sumf += d * (sumi1 + sumi2 + sumi3 + sumi4);
  5855. }
  5856. *s = sumf;
  5857. #elif defined __AVX2__
  5858. const __m256i m4 = _mm256_set1_epi8(0xF);
  5859. const __m256i mone = _mm256_set1_epi8(1);
  5860. __m256 acc = _mm256_setzero_ps();
  5861. for (int i = 0; i < nb; ++i) {
  5862. const uint8_t * restrict q5 = x[i].qs;
  5863. const int8_t * restrict q8 = y[i].qs;
  5864. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5865. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5);
  5866. const __m256i scale_l = MM256_SET_M128I(_mm_set1_epi16(x[i].scales[1]), _mm_set1_epi16(x[i].scales[0]));
  5867. const __m256i scale_h = MM256_SET_M128I(_mm_set1_epi16(x[i].scales[3]), _mm_set1_epi16(x[i].scales[2]));
  5868. int64_t aux64;
  5869. memcpy(&aux64, x[i].qh, 8);
  5870. const __m128i haux128 = _mm_set_epi64x(aux64 >> 1, aux64);
  5871. const __m256i haux256 = MM256_SET_M128I(_mm_srli_epi16(haux128, 2), haux128);
  5872. const __m256i q5h_0 = _mm256_slli_epi16(_mm256_andnot_si256(haux256, mone), 4);
  5873. const __m256i q5h_1 = _mm256_slli_epi16(_mm256_andnot_si256(_mm256_srli_epi16(haux256, 4), mone), 4);
  5874. const __m256i q5l_0 = _mm256_and_si256(q5bits, m4);
  5875. const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4);
  5876. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5877. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5878. const __m256i p16_0 = _mm256_madd_epi16(scale_l, _mm256_maddubs_epi16(q5l_0, q8_0));
  5879. const __m256i p16_1 = _mm256_madd_epi16(scale_h, _mm256_maddubs_epi16(q5l_1, q8_1));
  5880. const __m256i s16_0 = _mm256_madd_epi16(scale_l, _mm256_maddubs_epi16(q5h_0, q8_0));
  5881. const __m256i s16_1 = _mm256_madd_epi16(scale_h, _mm256_maddubs_epi16(q5h_1, q8_1));
  5882. const __m256i dot = _mm256_sub_epi32(_mm256_add_epi32(p16_0, p16_1), _mm256_add_epi32(s16_0, s16_1));
  5883. acc = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(dot), acc);
  5884. }
  5885. *s = hsum_float_8(acc);
  5886. #elif defined __AVX__
  5887. const __m128i m4 = _mm_set1_epi8(0xF);
  5888. const __m128i mone = _mm_set1_epi8(1);
  5889. __m256 acc = _mm256_setzero_ps();
  5890. for (int i = 0; i < nb; ++i) {
  5891. const uint8_t * restrict q5 = x[i].qs;
  5892. const int8_t * restrict q8 = y[i].qs;
  5893. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5894. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5);
  5895. const __m128i scale_0 = _mm_set1_epi16(x[i].scales[0]);
  5896. const __m128i scale_1 = _mm_set1_epi16(x[i].scales[1]);
  5897. const __m128i scale_2 = _mm_set1_epi16(x[i].scales[2]);
  5898. const __m128i scale_3 = _mm_set1_epi16(x[i].scales[3]);
  5899. int64_t aux64;
  5900. memcpy(&aux64, x[i].qh, 8);
  5901. const __m128i haux128_0 = _mm_set_epi64x(aux64 >> 1, aux64);
  5902. const __m128i haux128_1 = _mm_srli_epi16(haux128_0, 2);
  5903. const __m128i q5h_0 = _mm_slli_epi16(_mm_andnot_si128(haux128_0, mone), 4);
  5904. const __m128i q5h_1 = _mm_slli_epi16(_mm_andnot_si128(haux128_1, mone), 4);
  5905. const __m128i q5h_2 = _mm_slli_epi16(_mm_andnot_si128(_mm_srli_epi16(haux128_0, 4), mone), 4);
  5906. const __m128i q5h_3 = _mm_slli_epi16(_mm_andnot_si128(_mm_srli_epi16(haux128_1, 4), mone), 4);
  5907. const __m128i q5l_0 = _mm_and_si128(_mm256_extractf128_si256(q5bits, 0), m4);
  5908. const __m128i q5l_1 = _mm_and_si128(_mm256_extractf128_si256(q5bits, 1), m4);
  5909. const __m128i q5l_2 = _mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q5bits, 0), 4), m4);
  5910. const __m128i q5l_3 = _mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q5bits, 1), 4), m4);
  5911. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5912. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5913. const __m128i p16_0 = _mm_madd_epi16(scale_0, _mm_maddubs_epi16(q5l_0, _mm256_extractf128_si256(q8_0, 0)));
  5914. const __m128i p16_1 = _mm_madd_epi16(scale_1, _mm_maddubs_epi16(q5l_1, _mm256_extractf128_si256(q8_0, 1)));
  5915. const __m128i p16_2 = _mm_madd_epi16(scale_2, _mm_maddubs_epi16(q5l_2, _mm256_extractf128_si256(q8_1, 0)));
  5916. const __m128i p16_3 = _mm_madd_epi16(scale_3, _mm_maddubs_epi16(q5l_3, _mm256_extractf128_si256(q8_1, 1)));
  5917. const __m128i s16_0 = _mm_madd_epi16(scale_0, _mm_maddubs_epi16(q5h_0, _mm256_extractf128_si256(q8_0, 0)));
  5918. const __m128i s16_1 = _mm_madd_epi16(scale_1, _mm_maddubs_epi16(q5h_1, _mm256_extractf128_si256(q8_0, 1)));
  5919. const __m128i s16_2 = _mm_madd_epi16(scale_2, _mm_maddubs_epi16(q5h_2, _mm256_extractf128_si256(q8_1, 0)));
  5920. const __m128i s16_3 = _mm_madd_epi16(scale_3, _mm_maddubs_epi16(q5h_3, _mm256_extractf128_si256(q8_1, 1)));
  5921. const __m128i dot_0 = _mm_sub_epi32(_mm_add_epi32(p16_0, p16_2), _mm_add_epi32(s16_0, s16_2));
  5922. const __m128i dot_1 = _mm_sub_epi32(_mm_add_epi32(p16_1, p16_3), _mm_add_epi32(s16_1, s16_3));
  5923. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(MM256_SET_M128I(dot_1, dot_0))), acc);
  5924. }
  5925. *s = hsum_float_8(acc);
  5926. #elif defined __riscv_v_intrinsic
  5927. float sumf = 0;
  5928. for (int i = 0; i < nb; ++i) {
  5929. const float d = y[i].d * (float)x[i].d;
  5930. const int8_t * sc = x[i].scales;
  5931. const uint8_t * restrict q5 = x[i].qs;
  5932. const uint8_t * restrict qh = x[i].qh;
  5933. const int8_t * restrict q8 = y[i].qs;
  5934. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5935. // load qh
  5936. vuint8mf4_t qh_x1 = __riscv_vle8_v_u8mf4(qh, 8);
  5937. vuint8mf2_t qh_x2 = __riscv_vlmul_ext_v_u8mf4_u8mf2(__riscv_vsrl_vx_u8mf4(qh_x1, 1, 8));
  5938. size_t vl = 16;
  5939. // combine both qh_1 and qh_2
  5940. vuint8mf2_t qh_x = __riscv_vslideup_vx_u8mf2(__riscv_vlmul_ext_v_u8mf4_u8mf2(qh_x1), qh_x2, vl/2, vl);
  5941. vuint8mf2_t qh_h0 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x4, vl), vl), 16, vl);
  5942. vuint8mf2_t qh_h1 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x2, vl), vl), 16, vl);
  5943. vuint8mf2_t qh_h2 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(qh_x, vl), 16, vl);
  5944. vuint8mf2_t qh_h3 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x4, vl), vl), 16, vl);
  5945. vint8mf2_t qh_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h0);
  5946. vint8mf2_t qh_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h1);
  5947. vint8mf2_t qh_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h2);
  5948. vint8mf2_t qh_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h3);
  5949. // load q5
  5950. vuint8mf2_t q5_x1 = __riscv_vle8_v_u8mf2(q5, vl);
  5951. vuint8mf2_t q5_x2 = __riscv_vle8_v_u8mf2(q5+16, vl);
  5952. vint8mf2_t q5s_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q5_x1, 0xF, vl));
  5953. vint8mf2_t q5s_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q5_x2, 0xF, vl));
  5954. vint8mf2_t q5s_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vsrl_vx_u8mf2(q5_x1, 0x4, vl));
  5955. vint8mf2_t q5s_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vsrl_vx_u8mf2(q5_x2, 0x4, vl));
  5956. vint8mf2_t q5_0 = __riscv_vsub_vv_i8mf2(q5s_0, qh_0, vl);
  5957. vint8mf2_t q5_1 = __riscv_vsub_vv_i8mf2(q5s_1, qh_1, vl);
  5958. vint8mf2_t q5_2 = __riscv_vsub_vv_i8mf2(q5s_2, qh_2, vl);
  5959. vint8mf2_t q5_3 = __riscv_vsub_vv_i8mf2(q5s_3, qh_3, vl);
  5960. // load Q8 and multiply it with Q5
  5961. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q5_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  5962. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q5_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  5963. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q5_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  5964. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q5_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  5965. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  5966. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  5967. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  5968. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  5969. int32_t sumi1 = sc[0] * __riscv_vmv_x_s_i32m1_i32(vs_0);
  5970. int32_t sumi2 = sc[1] * __riscv_vmv_x_s_i32m1_i32(vs_1);
  5971. int32_t sumi3 = sc[2] * __riscv_vmv_x_s_i32m1_i32(vs_2);
  5972. int32_t sumi4 = sc[3] * __riscv_vmv_x_s_i32m1_i32(vs_3);
  5973. sumf += d * (sumi1 + sumi2 + sumi3 + sumi4);
  5974. }
  5975. *s = sumf;
  5976. #else
  5977. int8_t aux8[QK_K];
  5978. int16_t aux16[16];
  5979. float sums [8];
  5980. memset(sums, 0, 8*sizeof(float));
  5981. float sumf = 0;
  5982. for (int i = 0; i < nb; ++i) {
  5983. const uint8_t * restrict q4 = x[i].qs;
  5984. const uint8_t * restrict hm = x[i].qh;
  5985. const int8_t * restrict q8 = y[i].qs;
  5986. int8_t * restrict a = aux8;
  5987. for (int l = 0; l < 32; ++l) {
  5988. a[l+ 0] = q4[l] & 0xF;
  5989. a[l+32] = q4[l] >> 4;
  5990. }
  5991. for (int is = 0; is < 8; ++is) {
  5992. uint8_t m = 1 << is;
  5993. for (int l = 0; l < 8; ++l) a[8*is + l] -= (hm[l] & m ? 0 : 16);
  5994. }
  5995. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5996. const int8_t * restrict sc = x[i].scales;
  5997. for (int j = 0; j < QK_K/16; ++j) {
  5998. const float dl = d * sc[j];
  5999. for (int l = 0; l < 16; ++l) aux16[l] = q8[l] * a[l];
  6000. for (int l = 0; l < 8; ++l) sums[l] += dl * (aux16[l] + aux16[8+l]);
  6001. q8 += 16; a += 16;
  6002. }
  6003. }
  6004. for (int l = 0; l < 8; ++l) sumf += sums[l];
  6005. *s = sumf;
  6006. #endif
  6007. }
  6008. #endif
  6009. #if QK_K == 256
  6010. void ggml_vec_dot_q6_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  6011. assert(n % QK_K == 0);
  6012. const block_q6_K * restrict x = vx;
  6013. const block_q8_K * restrict y = vy;
  6014. const int nb = n / QK_K;
  6015. #ifdef __ARM_NEON
  6016. float sum = 0;
  6017. const uint8x16_t m4b = vdupq_n_u8(0xF);
  6018. const int32x4_t vzero = vdupq_n_s32(0);
  6019. //const int8x16_t m32s = vdupq_n_s8(32);
  6020. const uint8x16_t mone = vdupq_n_u8(3);
  6021. ggml_int8x16x4_t q6bytes;
  6022. ggml_uint8x16x4_t q6h;
  6023. for (int i = 0; i < nb; ++i) {
  6024. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  6025. const uint8_t * restrict q6 = x[i].ql;
  6026. const uint8_t * restrict qh = x[i].qh;
  6027. const int8_t * restrict q8 = y[i].qs;
  6028. const int8_t * restrict scale = x[i].scales;
  6029. const ggml_int16x8x2_t q8sums = ggml_vld1q_s16_x2(y[i].bsums);
  6030. const int8x16_t scales = vld1q_s8(scale);
  6031. const ggml_int16x8x2_t q6scales = {{vmovl_s8(vget_low_s8(scales)), vmovl_s8(vget_high_s8(scales))}};
  6032. const int32x4_t prod = vaddq_s32(vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[0]), vget_low_s16 (q6scales.val[0])),
  6033. vmull_s16(vget_high_s16(q8sums.val[0]), vget_high_s16(q6scales.val[0]))),
  6034. vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[1]), vget_low_s16 (q6scales.val[1])),
  6035. vmull_s16(vget_high_s16(q8sums.val[1]), vget_high_s16(q6scales.val[1]))));
  6036. int32_t isum_mins = vaddvq_s32(prod);
  6037. int32_t isum = 0;
  6038. for (int j = 0; j < QK_K/128; ++j) {
  6039. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh); qh += 32;
  6040. ggml_uint8x16x4_t q6bits = ggml_vld1q_u8_x4(q6); q6 += 64;
  6041. ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  6042. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
  6043. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
  6044. uint8x16_t shifted = vshrq_n_u8(qhbits.val[0], 2);
  6045. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6046. shifted = vshrq_n_u8(qhbits.val[1], 2);
  6047. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6048. //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0])), m32s);
  6049. //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1])), m32s);
  6050. //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2])), m32s);
  6051. //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3])), m32s);
  6052. q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0]));
  6053. q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1]));
  6054. q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2]));
  6055. q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3]));
  6056. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  6057. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  6058. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  6059. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  6060. scale += 4;
  6061. q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  6062. shifted = vshrq_n_u8(qhbits.val[0], 4);
  6063. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6064. shifted = vshrq_n_u8(qhbits.val[1], 4);
  6065. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6066. shifted = vshrq_n_u8(qhbits.val[0], 6);
  6067. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6068. shifted = vshrq_n_u8(qhbits.val[1], 6);
  6069. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6070. //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0])), m32s);
  6071. //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1])), m32s);
  6072. //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2])), m32s);
  6073. //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3])), m32s);
  6074. q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0]));
  6075. q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1]));
  6076. q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2]));
  6077. q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3]));
  6078. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  6079. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  6080. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  6081. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  6082. scale += 4;
  6083. }
  6084. //sum += isum * d_all * y[i].d;
  6085. sum += d_all * y[i].d * (isum - 32 * isum_mins);
  6086. }
  6087. *s = sum;
  6088. #elif defined __AVX2__
  6089. const __m256i m4 = _mm256_set1_epi8(0xF);
  6090. const __m256i m2 = _mm256_set1_epi8(3);
  6091. const __m256i m32s = _mm256_set1_epi8(32);
  6092. __m256 acc = _mm256_setzero_ps();
  6093. for (int i = 0; i < nb; ++i) {
  6094. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6095. const uint8_t * restrict q4 = x[i].ql;
  6096. const uint8_t * restrict qh = x[i].qh;
  6097. const int8_t * restrict q8 = y[i].qs;
  6098. const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  6099. __m256i sumi = _mm256_setzero_si256();
  6100. int is = 0;
  6101. for (int j = 0; j < QK_K/128; ++j) {
  6102. const __m128i scale_0 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 0));
  6103. const __m128i scale_1 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 1));
  6104. const __m128i scale_2 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 2));
  6105. const __m128i scale_3 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 3));
  6106. is += 4;
  6107. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  6108. const __m256i q4bits2 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  6109. const __m256i q4bitsH = _mm256_loadu_si256((const __m256i*)qh); qh += 32;
  6110. const __m256i q4h_0 = _mm256_slli_epi16(_mm256_and_si256(q4bitsH, m2), 4);
  6111. const __m256i q4h_1 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 2), m2), 4);
  6112. const __m256i q4h_2 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 4), m2), 4);
  6113. const __m256i q4h_3 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 6), m2), 4);
  6114. const __m256i q4_0 = _mm256_or_si256(_mm256_and_si256(q4bits1, m4), q4h_0);
  6115. const __m256i q4_1 = _mm256_or_si256(_mm256_and_si256(q4bits2, m4), q4h_1);
  6116. const __m256i q4_2 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits1, 4), m4), q4h_2);
  6117. const __m256i q4_3 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits2, 4), m4), q4h_3);
  6118. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6119. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6120. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6121. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6122. __m256i q8s_0 = _mm256_maddubs_epi16(m32s, q8_0);
  6123. __m256i q8s_1 = _mm256_maddubs_epi16(m32s, q8_1);
  6124. __m256i q8s_2 = _mm256_maddubs_epi16(m32s, q8_2);
  6125. __m256i q8s_3 = _mm256_maddubs_epi16(m32s, q8_3);
  6126. __m256i p16_0 = _mm256_maddubs_epi16(q4_0, q8_0);
  6127. __m256i p16_1 = _mm256_maddubs_epi16(q4_1, q8_1);
  6128. __m256i p16_2 = _mm256_maddubs_epi16(q4_2, q8_2);
  6129. __m256i p16_3 = _mm256_maddubs_epi16(q4_3, q8_3);
  6130. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  6131. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  6132. p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
  6133. p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
  6134. p16_0 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_0), p16_0);
  6135. p16_1 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_1), p16_1);
  6136. p16_2 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_2), p16_2);
  6137. p16_3 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_3), p16_3);
  6138. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  6139. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_2, p16_3));
  6140. }
  6141. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  6142. }
  6143. *s = hsum_float_8(acc);
  6144. #elif defined __AVX__
  6145. const __m128i m4 = _mm_set1_epi8(0xF);
  6146. const __m128i m3 = _mm_set1_epi8(3);
  6147. const __m128i m32s = _mm_set1_epi8(32);
  6148. const __m128i m2 = _mm_set1_epi8(2);
  6149. __m256 acc = _mm256_setzero_ps();
  6150. for (int i = 0; i < nb; ++i) {
  6151. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6152. const uint8_t * restrict q4 = x[i].ql;
  6153. const uint8_t * restrict qh = x[i].qh;
  6154. const int8_t * restrict q8 = y[i].qs;
  6155. const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  6156. __m128i sumi_0 = _mm_setzero_si128();
  6157. __m128i sumi_1 = _mm_setzero_si128();
  6158. __m128i shuffle = _mm_set_epi64x(0x0101010101010101, 0x0000000000000000);
  6159. for (int j = 0; j < QK_K/128; ++j) {
  6160. const __m128i q4bitsH_0 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
  6161. const __m128i q4bitsH_1 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
  6162. const __m128i q4h_0 = _mm_slli_epi16(_mm_and_si128(q4bitsH_0, m3), 4);
  6163. const __m128i q4h_1 = _mm_slli_epi16(_mm_and_si128(q4bitsH_1, m3), 4);
  6164. const __m128i q4h_2 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 2), m3), 4);
  6165. const __m128i q4h_3 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 2), m3), 4);
  6166. const __m128i q4h_4 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 4), m3), 4);
  6167. const __m128i q4h_5 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 4), m3), 4);
  6168. const __m128i q4h_6 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 6), m3), 4);
  6169. const __m128i q4h_7 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 6), m3), 4);
  6170. const __m128i q4bits1_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6171. const __m128i q4bits1_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6172. const __m128i q4bits2_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6173. const __m128i q4bits2_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6174. const __m128i q4_0 = _mm_or_si128(_mm_and_si128(q4bits1_0, m4), q4h_0);
  6175. const __m128i q4_1 = _mm_or_si128(_mm_and_si128(q4bits1_1, m4), q4h_1);
  6176. const __m128i q4_2 = _mm_or_si128(_mm_and_si128(q4bits2_0, m4), q4h_2);
  6177. const __m128i q4_3 = _mm_or_si128(_mm_and_si128(q4bits2_1, m4), q4h_3);
  6178. const __m128i q4_4 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_0, 4), m4), q4h_4);
  6179. const __m128i q4_5 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_1, 4), m4), q4h_5);
  6180. const __m128i q4_6 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_0, 4), m4), q4h_6);
  6181. const __m128i q4_7 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_1, 4), m4), q4h_7);
  6182. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6183. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6184. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6185. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6186. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6187. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6188. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6189. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6190. __m128i q8s_0 = _mm_maddubs_epi16(m32s, q8_0);
  6191. __m128i q8s_1 = _mm_maddubs_epi16(m32s, q8_1);
  6192. __m128i q8s_2 = _mm_maddubs_epi16(m32s, q8_2);
  6193. __m128i q8s_3 = _mm_maddubs_epi16(m32s, q8_3);
  6194. __m128i q8s_4 = _mm_maddubs_epi16(m32s, q8_4);
  6195. __m128i q8s_5 = _mm_maddubs_epi16(m32s, q8_5);
  6196. __m128i q8s_6 = _mm_maddubs_epi16(m32s, q8_6);
  6197. __m128i q8s_7 = _mm_maddubs_epi16(m32s, q8_7);
  6198. __m128i p16_0 = _mm_maddubs_epi16(q4_0, q8_0);
  6199. __m128i p16_1 = _mm_maddubs_epi16(q4_1, q8_1);
  6200. __m128i p16_2 = _mm_maddubs_epi16(q4_2, q8_2);
  6201. __m128i p16_3 = _mm_maddubs_epi16(q4_3, q8_3);
  6202. __m128i p16_4 = _mm_maddubs_epi16(q4_4, q8_4);
  6203. __m128i p16_5 = _mm_maddubs_epi16(q4_5, q8_5);
  6204. __m128i p16_6 = _mm_maddubs_epi16(q4_6, q8_6);
  6205. __m128i p16_7 = _mm_maddubs_epi16(q4_7, q8_7);
  6206. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  6207. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  6208. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  6209. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  6210. p16_4 = _mm_sub_epi16(p16_4, q8s_4);
  6211. p16_5 = _mm_sub_epi16(p16_5, q8s_5);
  6212. p16_6 = _mm_sub_epi16(p16_6, q8s_6);
  6213. p16_7 = _mm_sub_epi16(p16_7, q8s_7);
  6214. const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
  6215. shuffle = _mm_add_epi8(shuffle, m2);
  6216. const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
  6217. shuffle = _mm_add_epi8(shuffle, m2);
  6218. const __m128i scale_2 = _mm_shuffle_epi8(scales, shuffle);
  6219. shuffle = _mm_add_epi8(shuffle, m2);
  6220. const __m128i scale_3 = _mm_shuffle_epi8(scales, shuffle);
  6221. shuffle = _mm_add_epi8(shuffle, m2);
  6222. p16_0 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_0), p16_0);
  6223. p16_1 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_0, scale_0)), p16_1);
  6224. p16_2 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_1), p16_2);
  6225. p16_3 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_1, scale_1)), p16_3);
  6226. p16_4 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_2), p16_4);
  6227. p16_5 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_2, scale_2)), p16_5);
  6228. p16_6 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_3), p16_6);
  6229. p16_7 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_3, scale_3)), p16_7);
  6230. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  6231. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  6232. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_4, p16_6));
  6233. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_5, p16_7));
  6234. }
  6235. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  6236. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
  6237. }
  6238. *s = hsum_float_8(acc);
  6239. #elif defined __riscv_v_intrinsic
  6240. float sumf = 0;
  6241. for (int i = 0; i < nb; ++i) {
  6242. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6243. const uint8_t * restrict q6 = x[i].ql;
  6244. const uint8_t * restrict qh = x[i].qh;
  6245. const int8_t * restrict q8 = y[i].qs;
  6246. const int8_t * restrict scale = x[i].scales;
  6247. size_t vl;
  6248. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  6249. int sum_t = 0;
  6250. int is = 0;
  6251. for (int j = 0; j < QK_K/128; ++j) {
  6252. vl = 32;
  6253. // load qh
  6254. vuint8m1_t qh_x = __riscv_vle8_v_u8m1(qh, vl);
  6255. // load Q6
  6256. vuint8m1_t q6_0 = __riscv_vle8_v_u8m1(q6, vl);
  6257. vuint8m1_t q6_1 = __riscv_vle8_v_u8m1(q6+32, vl);
  6258. vuint8m1_t q6a_0 = __riscv_vand_vx_u8m1(q6_0, 0x0F, vl);
  6259. vuint8m1_t q6a_1 = __riscv_vand_vx_u8m1(q6_1, 0x0F, vl);
  6260. vuint8m1_t q6s_0 = __riscv_vsrl_vx_u8m1(q6_0, 0x04, vl);
  6261. vuint8m1_t q6s_1 = __riscv_vsrl_vx_u8m1(q6_1, 0x04, vl);
  6262. vuint8m1_t qh_0 = __riscv_vand_vx_u8m1(qh_x, 0x03, vl);
  6263. vuint8m1_t qh_1 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x2, vl), 0x03 , vl);
  6264. vuint8m1_t qh_2 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x4, vl), 0x03 , vl);
  6265. vuint8m1_t qh_3 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x6, vl), 0x03 , vl);
  6266. vuint8m1_t qhi_0 = __riscv_vor_vv_u8m1(q6a_0, __riscv_vsll_vx_u8m1(qh_0, 0x04, vl), vl);
  6267. vuint8m1_t qhi_1 = __riscv_vor_vv_u8m1(q6a_1, __riscv_vsll_vx_u8m1(qh_1, 0x04, vl), vl);
  6268. vuint8m1_t qhi_2 = __riscv_vor_vv_u8m1(q6s_0, __riscv_vsll_vx_u8m1(qh_2, 0x04, vl), vl);
  6269. vuint8m1_t qhi_3 = __riscv_vor_vv_u8m1(q6s_1, __riscv_vsll_vx_u8m1(qh_3, 0x04, vl), vl);
  6270. vint8m1_t a_0 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_0), 32, vl);
  6271. vint8m1_t a_1 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_1), 32, vl);
  6272. vint8m1_t a_2 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_2), 32, vl);
  6273. vint8m1_t a_3 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_3), 32, vl);
  6274. // load Q8 and take product
  6275. vint16m2_t va_q_0 = __riscv_vwmul_vv_i16m2(a_0, __riscv_vle8_v_i8m1(q8, vl), vl);
  6276. vint16m2_t va_q_1 = __riscv_vwmul_vv_i16m2(a_1, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  6277. vint16m2_t va_q_2 = __riscv_vwmul_vv_i16m2(a_2, __riscv_vle8_v_i8m1(q8+64, vl), vl);
  6278. vint16m2_t va_q_3 = __riscv_vwmul_vv_i16m2(a_3, __riscv_vle8_v_i8m1(q8+96, vl), vl);
  6279. vl = 16;
  6280. vint32m2_t vaux_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_0, 0), scale[is+0], vl);
  6281. vint32m2_t vaux_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_0, 1), scale[is+1], vl);
  6282. vint32m2_t vaux_2 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_1, 0), scale[is+2], vl);
  6283. vint32m2_t vaux_3 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_1, 1), scale[is+3], vl);
  6284. vint32m2_t vaux_4 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_2, 0), scale[is+4], vl);
  6285. vint32m2_t vaux_5 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_2, 1), scale[is+5], vl);
  6286. vint32m2_t vaux_6 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_3, 0), scale[is+6], vl);
  6287. vint32m2_t vaux_7 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_3, 1), scale[is+7], vl);
  6288. vint32m1_t isum0 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_0, vaux_1, vl), vzero, vl);
  6289. vint32m1_t isum1 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_2, vaux_3, vl), isum0, vl);
  6290. vint32m1_t isum2 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_4, vaux_5, vl), isum1, vl);
  6291. vint32m1_t isum3 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_6, vaux_7, vl), isum2, vl);
  6292. sum_t += __riscv_vmv_x_s_i32m1_i32(isum3);
  6293. q6 += 64; qh += 32; q8 += 128; is=8;
  6294. }
  6295. sumf += d * sum_t;
  6296. }
  6297. *s = sumf;
  6298. #else
  6299. int8_t aux8[QK_K];
  6300. int16_t aux16[8];
  6301. float sums [8];
  6302. int32_t aux32[8];
  6303. memset(sums, 0, 8*sizeof(float));
  6304. float sumf = 0;
  6305. for (int i = 0; i < nb; ++i) {
  6306. const uint8_t * restrict q4 = x[i].ql;
  6307. const uint8_t * restrict qh = x[i].qh;
  6308. const int8_t * restrict q8 = y[i].qs;
  6309. memset(aux32, 0, 8*sizeof(int32_t));
  6310. int8_t * restrict a = aux8;
  6311. for (int j = 0; j < QK_K; j += 128) {
  6312. for (int l = 0; l < 32; ++l) {
  6313. a[l + 0] = (int8_t)((q4[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  6314. a[l + 32] = (int8_t)((q4[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  6315. a[l + 64] = (int8_t)((q4[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  6316. a[l + 96] = (int8_t)((q4[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  6317. }
  6318. a += 128;
  6319. q4 += 64;
  6320. qh += 32;
  6321. }
  6322. a = aux8;
  6323. int is = 0;
  6324. for (int j = 0; j < QK_K/16; ++j) {
  6325. int scale = x[i].scales[is++];
  6326. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6327. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6328. q8 += 8; a += 8;
  6329. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6330. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6331. q8 += 8; a += 8;
  6332. }
  6333. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6334. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  6335. }
  6336. for (int l = 0; l < 8; ++l) sumf += sums[l];
  6337. *s = sumf;
  6338. #endif
  6339. }
  6340. #else
  6341. void ggml_vec_dot_q6_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  6342. assert(n % QK_K == 0);
  6343. const block_q6_K * restrict x = vx;
  6344. const block_q8_K * restrict y = vy;
  6345. const int nb = n / QK_K;
  6346. #ifdef __ARM_NEON
  6347. float sum = 0;
  6348. const uint8x16_t m4b = vdupq_n_u8(0xF);
  6349. const int8x16_t m32s = vdupq_n_s8(32);
  6350. const int32x4_t vzero = vdupq_n_s32(0);
  6351. const uint8x16_t mone = vdupq_n_u8(3);
  6352. ggml_int8x16x4_t q6bytes;
  6353. ggml_uint8x16x4_t q6h;
  6354. for (int i = 0; i < nb; ++i) {
  6355. const float d_all = (float)x[i].d;
  6356. const uint8_t * restrict q6 = x[i].ql;
  6357. const uint8_t * restrict qh = x[i].qh;
  6358. const int8_t * restrict q8 = y[i].qs;
  6359. const int8_t * restrict scale = x[i].scales;
  6360. int32_t isum = 0;
  6361. uint8x16_t qhbits = vld1q_u8(qh);
  6362. ggml_uint8x16x2_t q6bits = ggml_vld1q_u8_x2(q6);
  6363. ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  6364. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits), 4);
  6365. uint8x16_t shifted = vshrq_n_u8(qhbits, 2);
  6366. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6367. shifted = vshrq_n_u8(qhbits, 4);
  6368. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6369. shifted = vshrq_n_u8(qhbits, 6);
  6370. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6371. q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0])), m32s);
  6372. q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1])), m32s);
  6373. q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[2])), m32s);
  6374. q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[3])), m32s);
  6375. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  6376. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  6377. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  6378. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  6379. sum += isum * d_all * y[i].d;
  6380. }
  6381. *s = sum;
  6382. #elif defined __AVX2__
  6383. const __m256i m4 = _mm256_set1_epi8(0xF);
  6384. const __m256i m2 = _mm256_set1_epi8(3);
  6385. const __m256i m32s = _mm256_set1_epi8(32);
  6386. __m256 acc = _mm256_setzero_ps();
  6387. for (int i = 0; i < nb; ++i) {
  6388. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6389. const uint8_t * restrict q4 = x[i].ql;
  6390. const uint8_t * restrict qh = x[i].qh;
  6391. const int8_t * restrict q8 = y[i].qs;
  6392. const __m64 scales_1 = _mm_set1_pi8(x[i].scales[0]);
  6393. const __m64 scales_2 = _mm_set1_pi8(x[i].scales[1]);
  6394. const __m64 scales_3 = _mm_set1_pi8(x[i].scales[2]);
  6395. const __m64 scales_4 = _mm_set1_pi8(x[i].scales[3]);
  6396. __m256i sumi = _mm256_setzero_si256();
  6397. const __m128i scale_0 = _mm_set_epi64(scales_2, scales_1);
  6398. const __m128i scale_1 = _mm_set_epi64(scales_4, scales_3);
  6399. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4);
  6400. const __m128i q4bitsH = _mm_loadu_si128((const __m128i*)qh);
  6401. const __m256i q4h_0 = _mm256_slli_epi16(_mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q4bitsH, 2), q4bitsH), m2), 4);
  6402. const __m256i q4h_1 = _mm256_slli_epi16(_mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q4bitsH, 6), _mm_srli_epi16(q4bitsH, 4)), m2), 4);
  6403. const __m256i q4_0 = _mm256_or_si256(_mm256_and_si256(q4bits1, m4), q4h_0);
  6404. const __m256i q4_1 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits1, 4), m4), q4h_1);
  6405. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  6406. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  6407. __m256i q8s_0 = _mm256_maddubs_epi16(m32s, q8_0);
  6408. __m256i q8s_1 = _mm256_maddubs_epi16(m32s, q8_1);
  6409. __m256i p16_0 = _mm256_maddubs_epi16(q4_0, q8_0);
  6410. __m256i p16_1 = _mm256_maddubs_epi16(q4_1, q8_1);
  6411. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  6412. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  6413. p16_0 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_0), p16_0);
  6414. p16_1 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_1), p16_1);
  6415. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  6416. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  6417. }
  6418. *s = hsum_float_8(acc);
  6419. #elif defined __AVX__
  6420. const __m128i m4 = _mm_set1_epi8(0xF);
  6421. const __m128i m2 = _mm_set1_epi8(3);
  6422. const __m128i m32s = _mm_set1_epi8(32);
  6423. __m256 acc = _mm256_setzero_ps();
  6424. for (int i = 0; i < nb; ++i) {
  6425. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6426. const uint8_t * restrict q4 = x[i].ql;
  6427. const uint8_t * restrict qh = x[i].qh;
  6428. const int8_t * restrict q8 = y[i].qs;
  6429. const __m64 scales_1 = _mm_set1_pi8(x[i].scales[0]);
  6430. const __m64 scales_2 = _mm_set1_pi8(x[i].scales[1]);
  6431. const __m64 scales_3 = _mm_set1_pi8(x[i].scales[2]);
  6432. const __m64 scales_4 = _mm_set1_pi8(x[i].scales[3]);
  6433. __m128i sumi_0 = _mm_setzero_si128();
  6434. __m128i sumi_1 = _mm_setzero_si128();
  6435. const __m128i scale_0 = _mm_set_epi64(scales_2, scales_1);
  6436. const __m128i scale_1 = _mm_set_epi64(scales_4, scales_3);
  6437. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4);
  6438. const __m128i q4bitsH = _mm_loadu_si128((const __m128i*)qh);
  6439. const __m128i q4h_0 = _mm_slli_epi16(_mm_and_si128(q4bitsH, m2), 4);
  6440. const __m128i q4h_1 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 2), m2), 4);
  6441. const __m128i q4h_2 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 4), m2), 4);
  6442. const __m128i q4h_3 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 6), m2), 4);
  6443. const __m128i q4_0 = _mm_or_si128(_mm_and_si128(_mm256_extractf128_si256(q4bits1, 0), m4), q4h_0);
  6444. const __m128i q4_1 = _mm_or_si128(_mm_and_si128(_mm256_extractf128_si256(q4bits1, 1), m4), q4h_1);
  6445. const __m128i q4_2 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q4bits1, 0), 4), m4), q4h_2);
  6446. const __m128i q4_3 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q4bits1, 1), 4), m4), q4h_3);
  6447. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  6448. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  6449. __m128i q8s_0 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_0, 0));
  6450. __m128i q8s_1 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_0, 1));
  6451. __m128i q8s_2 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_1, 0));
  6452. __m128i q8s_3 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_1, 1));
  6453. __m128i p16_0 = _mm_maddubs_epi16(q4_0, _mm256_extractf128_si256(q8_0, 0));
  6454. __m128i p16_1 = _mm_maddubs_epi16(q4_1, _mm256_extractf128_si256(q8_0, 1));
  6455. __m128i p16_2 = _mm_maddubs_epi16(q4_2, _mm256_extractf128_si256(q8_1, 0));
  6456. __m128i p16_3 = _mm_maddubs_epi16(q4_3, _mm256_extractf128_si256(q8_1, 1));
  6457. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  6458. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  6459. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  6460. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  6461. p16_0 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_0), p16_0);
  6462. p16_1 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_0, scale_0)), p16_1);
  6463. p16_2 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_1), p16_2);
  6464. p16_3 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_1, scale_1)), p16_3);
  6465. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  6466. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  6467. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(MM256_SET_M128I(sumi_1, sumi_0))), acc);
  6468. }
  6469. *s = hsum_float_8(acc);
  6470. #elif defined __riscv_v_intrinsic
  6471. float sumf = 0;
  6472. for (int i = 0; i < nb; ++i) {
  6473. const float d_all = (float)x[i].d;
  6474. const uint8_t * restrict q6 = x[i].ql;
  6475. const uint8_t * restrict qh = x[i].qh;
  6476. const int8_t * restrict q8 = y[i].qs;
  6477. const int8_t * restrict scale = x[i].scales;
  6478. int32_t isum = 0;
  6479. size_t vl = 16;
  6480. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  6481. // load Q6
  6482. vuint8mf2_t q6_0 = __riscv_vle8_v_u8mf2(q6, vl);
  6483. vuint8mf2_t q6_1 = __riscv_vle8_v_u8mf2(q6+16, vl);
  6484. // load qh
  6485. vuint8mf2_t qh_x = __riscv_vle8_v_u8mf2(qh, vl);
  6486. vuint8mf2_t qh0 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6487. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  6488. vuint8mf2_t qh1 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6489. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  6490. vuint8mf2_t qh2 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6491. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  6492. vuint8mf2_t qh3 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6493. vuint8mf2_t q6h_0 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q6_0, 0xF, vl), qh0, vl);
  6494. vuint8mf2_t q6h_1 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q6_1, 0xF, vl), qh1, vl);
  6495. vuint8mf2_t q6h_2 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q6_0, 0x4, vl), qh2, vl);
  6496. vuint8mf2_t q6h_3 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q6_1, 0x4, vl), qh3, vl);
  6497. vint8mf2_t q6v_0 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_0), 32, vl);
  6498. vint8mf2_t q6v_1 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_1), 32, vl);
  6499. vint8mf2_t q6v_2 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_2), 32, vl);
  6500. vint8mf2_t q6v_3 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_3), 32, vl);
  6501. // load Q8 and take product
  6502. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q6v_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  6503. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q6v_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  6504. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q6v_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  6505. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q6v_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  6506. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  6507. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  6508. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  6509. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  6510. isum += __riscv_vmv_x_s_i32m1_i32(vs_0) * scale[0];
  6511. isum += __riscv_vmv_x_s_i32m1_i32(vs_1) * scale[1];
  6512. isum += __riscv_vmv_x_s_i32m1_i32(vs_2) * scale[2];
  6513. isum += __riscv_vmv_x_s_i32m1_i32(vs_3) * scale[3];
  6514. sumf += isum * d_all * y[i].d;
  6515. }
  6516. *s = sumf;
  6517. #else
  6518. int8_t aux8[QK_K];
  6519. int16_t aux16[8];
  6520. float sums [8];
  6521. int32_t aux32[8];
  6522. memset(sums, 0, 8*sizeof(float));
  6523. float sumf = 0;
  6524. for (int i = 0; i < nb; ++i) {
  6525. const uint8_t * restrict q4 = x[i].ql;
  6526. const uint8_t * restrict qh = x[i].qh;
  6527. const int8_t * restrict q8 = y[i].qs;
  6528. memset(aux32, 0, 8*sizeof(int32_t));
  6529. int8_t * restrict a = aux8;
  6530. for (int l = 0; l < 16; ++l) {
  6531. a[l+ 0] = (int8_t)((q4[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  6532. a[l+16] = (int8_t)((q4[l+16] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  6533. a[l+32] = (int8_t)((q4[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  6534. a[l+48] = (int8_t)((q4[l+16] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  6535. }
  6536. int is = 0;
  6537. for (int j = 0; j < QK_K/16; ++j) {
  6538. int scale = x[i].scales[is++];
  6539. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6540. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6541. q8 += 8; a += 8;
  6542. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6543. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6544. q8 += 8; a += 8;
  6545. }
  6546. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6547. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  6548. }
  6549. for (int l = 0; l < 8; ++l) sumf += sums[l];
  6550. *s = sumf;
  6551. #endif
  6552. }
  6553. #endif
  6554. static const int8_t keven_signs_q2xs[1024] = {
  6555. 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,
  6556. 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,
  6557. 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,
  6558. 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,
  6559. 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,
  6560. 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,
  6561. 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,
  6562. 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,
  6563. 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,
  6564. 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,
  6565. 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,
  6566. 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,
  6567. 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,
  6568. 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,
  6569. 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,
  6570. 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,
  6571. 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,
  6572. 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,
  6573. 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,
  6574. 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,
  6575. 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,
  6576. 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,
  6577. 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,
  6578. 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,
  6579. 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,
  6580. 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,
  6581. 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,
  6582. 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,
  6583. 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,
  6584. 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,
  6585. 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,
  6586. 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,
  6587. };
  6588. void ggml_vec_dot_iq2_xxs_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  6589. assert(n % QK_K == 0);
  6590. const block_iq2_xxs * restrict x = vx;
  6591. const block_q8_K * restrict y = vy;
  6592. const int nb = n / QK_K;
  6593. #if defined(__ARM_NEON)
  6594. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6595. uint32_t aux32[4];
  6596. const uint8_t * aux8 = (const uint8_t *)aux32;
  6597. ggml_int8x16x4_t q2u;
  6598. ggml_int8x16x4_t q2s;
  6599. ggml_int8x16x4_t q8b;
  6600. float sumf = 0;
  6601. for (int i = 0; i < nb; ++i) {
  6602. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6603. const uint16_t * restrict q2 = x[i].qs;
  6604. const int8_t * restrict q8 = y[i].qs;
  6605. float sumf1 = 0, sumf2 = 0;
  6606. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6607. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  6608. memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8;
  6609. q2u.val[0] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 0])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 1])));
  6610. q2u.val[1] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 2])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 3])));
  6611. q2u.val[2] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 8])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 9])));
  6612. q2u.val[3] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[10])), vld1_s8((const void *)(iq2xxs_grid + aux8[11])));
  6613. q2s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 7) & 127))));
  6614. q2s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 21) & 127))));
  6615. q2s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[3] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[3] >> 7) & 127))));
  6616. q2s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[3] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[3] >> 21) & 127))));
  6617. q2u.val[0] = vmulq_s8(q2u.val[0], q2s.val[0]);
  6618. q2u.val[1] = vmulq_s8(q2u.val[1], q2s.val[1]);
  6619. q2u.val[2] = vmulq_s8(q2u.val[2], q2s.val[2]);
  6620. q2u.val[3] = vmulq_s8(q2u.val[3], q2s.val[3]);
  6621. 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]);
  6622. 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]);
  6623. sumf1 += vaddvq_s32(p1) * (0.5f + (aux32[1] >> 28));
  6624. sumf2 += vaddvq_s32(p2) * (0.5f + (aux32[3] >> 28));
  6625. }
  6626. sumf += d*(sumf1 + sumf2);
  6627. }
  6628. *s = 0.25f * sumf;
  6629. #elif defined(__AVX2__)
  6630. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6631. uint32_t aux32[4];
  6632. const uint8_t * aux8 = (const uint8_t *)aux32;
  6633. __m256 accumf = _mm256_setzero_ps();
  6634. for (int i = 0; i < nb; ++i) {
  6635. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6636. const uint16_t * restrict q2 = x[i].qs;
  6637. const int8_t * restrict q8 = y[i].qs;
  6638. __m256i sumi1 = _mm256_setzero_si256();
  6639. __m256i sumi2 = _mm256_setzero_si256();
  6640. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6641. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6642. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6643. memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8;
  6644. const __m256i q2_1 = _mm256_set_epi64x(iq2xxs_grid[aux8[ 3]], iq2xxs_grid[aux8[ 2]], iq2xxs_grid[aux8[1]], iq2xxs_grid[aux8[0]]);
  6645. const __m256i q2_2 = _mm256_set_epi64x(iq2xxs_grid[aux8[11]], iq2xxs_grid[aux8[10]], iq2xxs_grid[aux8[9]], iq2xxs_grid[aux8[8]]);
  6646. const __m256i s2_1 = _mm256_set_epi64x(signs64[(aux32[1] >> 21) & 127], signs64[(aux32[1] >> 14) & 127],
  6647. signs64[(aux32[1] >> 7) & 127], signs64[(aux32[1] >> 0) & 127]);
  6648. const __m256i s2_2 = _mm256_set_epi64x(signs64[(aux32[3] >> 21) & 127], signs64[(aux32[3] >> 14) & 127],
  6649. signs64[(aux32[3] >> 7) & 127], signs64[(aux32[3] >> 0) & 127]);
  6650. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, s2_1);
  6651. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, s2_2);
  6652. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  6653. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  6654. const uint16_t ls1 = aux32[1] >> 28;
  6655. const uint16_t ls2 = aux32[3] >> 28;
  6656. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
  6657. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
  6658. sumi1 = _mm256_add_epi32(sumi1, p1);
  6659. sumi2 = _mm256_add_epi32(sumi2, p2);
  6660. }
  6661. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  6662. }
  6663. *s = 0.125f * hsum_float_8(accumf);
  6664. #else
  6665. uint32_t aux32[2];
  6666. const uint8_t * aux8 = (const uint8_t *)aux32;
  6667. float sumf = 0.f;
  6668. for (int i = 0; i < nb; ++i) {
  6669. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6670. const uint16_t * restrict q2 = x[i].qs;
  6671. const int8_t * restrict q8 = y[i].qs;
  6672. int32_t bsum = 0;
  6673. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  6674. memcpy(aux32, q2, 2*sizeof(uint32_t));
  6675. q2 += 4;
  6676. const uint32_t ls = 2*(aux32[1] >> 28) + 1;
  6677. int32_t sumi = 0;
  6678. for (int l = 0; l < 4; ++l) {
  6679. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  6680. const uint8_t signs = ksigns_iq2xs[(aux32[1] >> 7*l) & 127];
  6681. for (int j = 0; j < 8; ++j) {
  6682. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6683. }
  6684. q8 += 8;
  6685. }
  6686. bsum += sumi * ls;
  6687. }
  6688. sumf += d * bsum;
  6689. }
  6690. *s = 0.125f * sumf;
  6691. #endif
  6692. }
  6693. void ggml_vec_dot_iq2_xs_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  6694. assert(n % QK_K == 0);
  6695. const block_iq2_xs * restrict x = vx;
  6696. const block_q8_K * restrict y = vy;
  6697. const int nb = n / QK_K;
  6698. #if defined(__ARM_NEON)
  6699. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6700. ggml_int8x16x4_t q2u;
  6701. ggml_int8x16x4_t q2s;
  6702. ggml_int8x16x4_t q8b;
  6703. int32x4x4_t scales32;
  6704. float sumf = 0;
  6705. for (int i = 0; i < nb; ++i) {
  6706. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6707. const uint16_t * restrict q2 = x[i].qs;
  6708. const int8_t * restrict q8 = y[i].qs;
  6709. const uint8x8_t scales8 = vld1_u8(x[i].scales);
  6710. const uint8x8_t scales_l = vand_u8(scales8, vdup_n_u8(0xf));
  6711. const uint8x8_t scales_h = vshr_n_u8(scales8, 4);
  6712. uint8x16_t scales = vcombine_u8(vzip1_u8(scales_l, scales_h), vzip2_u8(scales_l, scales_h));
  6713. scales = vaddq_u8(vshlq_n_u8(scales, 1), vdupq_n_u8(1));
  6714. const uint16x8_t scales1 = vmovl_u8(vget_low_u8(scales));
  6715. const uint16x8_t scales2 = vmovl_u8(vget_high_u8(scales));
  6716. scales32.val[0] = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(scales1)));
  6717. scales32.val[1] = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(scales1)));
  6718. scales32.val[2] = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(scales2)));
  6719. scales32.val[3] = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(scales2)));
  6720. int32x4_t sumi = vdupq_n_s32(0);
  6721. for (int ib64 = 0; ib64 < QK_K/64; ++ib64) {
  6722. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  6723. q2u.val[0] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[0] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[1] & 511))));
  6724. q2u.val[1] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[2] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[3] & 511))));
  6725. q2u.val[2] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[4] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[5] & 511))));
  6726. q2u.val[3] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[6] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[7] & 511))));
  6727. q2s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[0] >> 9))), vld1_s8((const void *)(signs64 + (q2[1] >> 9))));
  6728. q2s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[2] >> 9))), vld1_s8((const void *)(signs64 + (q2[3] >> 9))));
  6729. q2s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[4] >> 9))), vld1_s8((const void *)(signs64 + (q2[5] >> 9))));
  6730. q2s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[6] >> 9))), vld1_s8((const void *)(signs64 + (q2[7] >> 9))));
  6731. q2u.val[0] = vmulq_s8(q2u.val[0], q2s.val[0]);
  6732. q2u.val[1] = vmulq_s8(q2u.val[1], q2s.val[1]);
  6733. q2u.val[2] = vmulq_s8(q2u.val[2], q2s.val[2]);
  6734. q2u.val[3] = vmulq_s8(q2u.val[3], q2s.val[3]);
  6735. const int32x4_t p1 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[0], q8b.val[0]);
  6736. const int32x4_t p2 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[1], q8b.val[1]);
  6737. const int32x4_t p3 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[2], q8b.val[2]);
  6738. const int32x4_t p4 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[3], q8b.val[3]);
  6739. const int32x4_t p = vpaddq_s32(vpaddq_s32(p1, p2), vpaddq_s32(p3, p4));
  6740. sumi = vmlaq_s32(sumi, p, scales32.val[ib64]);
  6741. q2 += 8;
  6742. }
  6743. sumf += d*vaddvq_s32(sumi);
  6744. }
  6745. *s = 0.125f * sumf;
  6746. #elif defined(__AVX2__)
  6747. const __m128i m4 = _mm_set1_epi8(0xf);
  6748. const __m128i m1 = _mm_set1_epi8(1);
  6749. const __m256i m511 = _mm256_set1_epi16(511);
  6750. const __m256i mone = _mm256_set1_epi8(1);
  6751. static const uint8_t k_bit_helper[32] = {
  6752. 0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00,
  6753. 0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00,
  6754. };
  6755. static const char block_sign_shuffle_mask_1[32] = {
  6756. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
  6757. 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  6758. };
  6759. static const char block_sign_shuffle_mask_2[32] = {
  6760. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
  6761. 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e,
  6762. };
  6763. static const uint8_t bit_selector_mask_bytes[32] = {
  6764. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  6765. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  6766. };
  6767. const __m256i bit_helper = _mm256_loadu_si256((const __m256i*)k_bit_helper);
  6768. const __m256i bit_selector_mask = _mm256_loadu_si256((const __m256i*)bit_selector_mask_bytes);
  6769. const __m256i block_sign_shuffle_1 = _mm256_loadu_si256((const __m256i*)block_sign_shuffle_mask_1);
  6770. const __m256i block_sign_shuffle_2 = _mm256_loadu_si256((const __m256i*)block_sign_shuffle_mask_2);
  6771. uint64_t aux64;
  6772. // somewhat hacky, but gives a significant boost in performance
  6773. __m256i aux_gindex;
  6774. const uint16_t * gindex = (const uint16_t *)&aux_gindex;
  6775. __m256 accumf = _mm256_setzero_ps();
  6776. for (int i = 0; i < nb; ++i) {
  6777. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6778. const uint16_t * restrict q2 = x[i].qs;
  6779. const int8_t * restrict q8 = y[i].qs;
  6780. memcpy(&aux64, x[i].scales, 8);
  6781. __m128i stmp = _mm_set1_epi64x(aux64);
  6782. stmp = _mm_unpacklo_epi8(_mm_and_si128(stmp, m4), _mm_and_si128(_mm_srli_epi16(stmp, 4), m4));
  6783. const __m128i scales = _mm_add_epi8(_mm_slli_epi16(stmp, 1), m1);
  6784. __m256i sumi1 = _mm256_setzero_si256();
  6785. __m256i sumi2 = _mm256_setzero_si256();
  6786. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 4) {
  6787. const __m256i q2_data = _mm256_loadu_si256((const __m256i*)q2); q2 += 16;
  6788. aux_gindex = _mm256_and_si256(q2_data, m511);
  6789. const __m256i partial_sign_bits = _mm256_srli_epi16(q2_data, 9);
  6790. const __m256i partial_sign_bits_upper = _mm256_srli_epi16(q2_data, 13);
  6791. const __m256i partial_sign_bits_for_counting = _mm256_xor_si256(partial_sign_bits, partial_sign_bits_upper);
  6792. const __m256i odd_bits = _mm256_shuffle_epi8(bit_helper, partial_sign_bits_for_counting);
  6793. const __m256i full_sign_bits = _mm256_or_si256(partial_sign_bits, odd_bits);
  6794. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6795. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6796. const __m256i q8_3 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6797. const __m256i q8_4 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6798. const __m256i q2_1 = _mm256_set_epi64x(iq2xs_grid[gindex[ 3]], iq2xs_grid[gindex[ 2]],
  6799. iq2xs_grid[gindex[ 1]], iq2xs_grid[gindex[ 0]]);
  6800. const __m256i q2_2 = _mm256_set_epi64x(iq2xs_grid[gindex[ 7]], iq2xs_grid[gindex[ 6]],
  6801. iq2xs_grid[gindex[ 5]], iq2xs_grid[gindex[ 4]]);
  6802. const __m256i q2_3 = _mm256_set_epi64x(iq2xs_grid[gindex[11]], iq2xs_grid[gindex[10]],
  6803. iq2xs_grid[gindex[ 9]], iq2xs_grid[gindex[ 8]]);
  6804. const __m256i q2_4 = _mm256_set_epi64x(iq2xs_grid[gindex[15]], iq2xs_grid[gindex[14]],
  6805. iq2xs_grid[gindex[13]], iq2xs_grid[gindex[12]]);
  6806. const __m128i full_signs_l = _mm256_castsi256_si128(full_sign_bits);
  6807. const __m128i full_signs_h = _mm256_extractf128_si256(full_sign_bits, 1);
  6808. const __m256i full_signs_1 = _mm256_set_m128i(full_signs_l, full_signs_l);
  6809. const __m256i full_signs_2 = _mm256_set_m128i(full_signs_h, full_signs_h);
  6810. __m256i signs;
  6811. signs = _mm256_shuffle_epi8(full_signs_1, block_sign_shuffle_1);
  6812. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  6813. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, _mm256_or_si256(signs, mone));
  6814. signs = _mm256_shuffle_epi8(full_signs_1, block_sign_shuffle_2);
  6815. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  6816. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, _mm256_or_si256(signs, mone));
  6817. signs = _mm256_shuffle_epi8(full_signs_2, block_sign_shuffle_1);
  6818. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  6819. const __m256i q8s_3 = _mm256_sign_epi8(q8_3, _mm256_or_si256(signs, mone));
  6820. signs = _mm256_shuffle_epi8(full_signs_2, block_sign_shuffle_2);
  6821. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  6822. const __m256i q8s_4 = _mm256_sign_epi8(q8_4, _mm256_or_si256(signs, mone));
  6823. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  6824. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  6825. const __m256i dot3 = _mm256_maddubs_epi16(q2_3, q8s_3);
  6826. const __m256i dot4 = _mm256_maddubs_epi16(q2_4, q8s_4);
  6827. const __m256i sc1 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+0)));
  6828. const __m256i sc2 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+1)));
  6829. const __m256i sc3 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+2)));
  6830. const __m256i sc4 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+3)));
  6831. sumi1 = _mm256_add_epi32(sumi1, _mm256_madd_epi16(dot1, sc1));
  6832. sumi2 = _mm256_add_epi32(sumi2, _mm256_madd_epi16(dot2, sc2));
  6833. sumi1 = _mm256_add_epi32(sumi1, _mm256_madd_epi16(dot3, sc3));
  6834. sumi2 = _mm256_add_epi32(sumi2, _mm256_madd_epi16(dot4, sc4));
  6835. }
  6836. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  6837. }
  6838. *s = 0.125f * hsum_float_8(accumf);
  6839. #else
  6840. float sumf = 0.f;
  6841. for (int i = 0; i < nb; ++i) {
  6842. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6843. const uint16_t * restrict q2 = x[i].qs;
  6844. const uint8_t * restrict sc = x[i].scales;
  6845. const int8_t * restrict q8 = y[i].qs;
  6846. int32_t bsum = 0;
  6847. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  6848. const uint16_t ls1 = 2*(sc[ib32] & 0xf) + 1;
  6849. const uint16_t ls2 = 2*(sc[ib32] >> 4) + 1;
  6850. int32_t sumi = 0;
  6851. for (int l = 0; l < 2; ++l) {
  6852. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[l] & 511));
  6853. const uint8_t signs = ksigns_iq2xs[q2[l] >> 9];
  6854. for (int j = 0; j < 8; ++j) {
  6855. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6856. }
  6857. q8 += 8;
  6858. }
  6859. bsum += sumi * ls1;
  6860. sumi = 0;
  6861. for (int l = 2; l < 4; ++l) {
  6862. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[l] & 511));
  6863. const uint8_t signs = ksigns_iq2xs[q2[l] >> 9];
  6864. for (int j = 0; j < 8; ++j) {
  6865. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6866. }
  6867. q8 += 8;
  6868. }
  6869. bsum += sumi * ls2;
  6870. q2 += 4;
  6871. }
  6872. sumf += d * bsum;
  6873. }
  6874. *s = 0.125f * sumf;
  6875. #endif
  6876. }
  6877. // TODO
  6878. void ggml_vec_dot_iq3_xxs_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  6879. assert(n % QK_K == 0);
  6880. const block_iq3_xxs * restrict x = vx;
  6881. const block_q8_K * restrict y = vy;
  6882. const int nb = n / QK_K;
  6883. #if defined(__ARM_NEON)
  6884. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6885. uint32_t aux32[2];
  6886. ggml_int8x16x4_t q3s;
  6887. ggml_int8x16x4_t q8b;
  6888. float sumf = 0;
  6889. for (int i = 0; i < nb; ++i) {
  6890. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6891. const uint8_t * restrict q3 = x[i].qs;
  6892. const uint8_t * restrict gas = x[i].qs + QK_K/4;
  6893. const int8_t * restrict q8 = y[i].qs;
  6894. float sumf1 = 0, sumf2 = 0;
  6895. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6896. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  6897. memcpy(aux32, gas, 2*sizeof(uint32_t)); gas += 2*sizeof(uint32_t);
  6898. const uint32x4_t aux32x4_0 = ggml_vld1q_u32(iq3xxs_grid[q3[ 0]], iq3xxs_grid[q3[ 1]], iq3xxs_grid[q3[ 2]], iq3xxs_grid[q3[ 3]]);
  6899. const uint32x4_t aux32x4_1 = ggml_vld1q_u32(iq3xxs_grid[q3[ 4]], iq3xxs_grid[q3[ 5]], iq3xxs_grid[q3[ 6]], iq3xxs_grid[q3[ 7]]);
  6900. const uint32x4_t aux32x4_2 = ggml_vld1q_u32(iq3xxs_grid[q3[ 8]], iq3xxs_grid[q3[ 9]], iq3xxs_grid[q3[10]], iq3xxs_grid[q3[11]]);
  6901. const uint32x4_t aux32x4_3 = ggml_vld1q_u32(iq3xxs_grid[q3[12]], iq3xxs_grid[q3[13]], iq3xxs_grid[q3[14]], iq3xxs_grid[q3[15]]);
  6902. q3 += 16;
  6903. q3s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[0] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[0] >> 7) & 127))));
  6904. q3s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[0] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[0] >> 21) & 127))));
  6905. q3s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 7) & 127))));
  6906. q3s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 21) & 127))));
  6907. q3s.val[0] = vmulq_s8(q3s.val[0], vreinterpretq_s8_u32(aux32x4_0));
  6908. q3s.val[1] = vmulq_s8(q3s.val[1], vreinterpretq_s8_u32(aux32x4_1));
  6909. q3s.val[2] = vmulq_s8(q3s.val[2], vreinterpretq_s8_u32(aux32x4_2));
  6910. q3s.val[3] = vmulq_s8(q3s.val[3], vreinterpretq_s8_u32(aux32x4_3));
  6911. 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]);
  6912. 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]);
  6913. sumf1 += vaddvq_s32(p1) * (0.5f + (aux32[0] >> 28));
  6914. sumf2 += vaddvq_s32(p2) * (0.5f + (aux32[1] >> 28));
  6915. }
  6916. sumf += d*(sumf1 + sumf2);
  6917. }
  6918. *s = 0.5f * sumf;
  6919. #elif defined(__AVX2__)
  6920. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6921. uint32_t aux32[2];
  6922. __m256 accumf = _mm256_setzero_ps();
  6923. for (int i = 0; i < nb; ++i) {
  6924. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6925. const uint8_t * restrict q3 = x[i].qs;
  6926. const uint8_t * restrict gas = x[i].qs + QK_K/4;
  6927. const int8_t * restrict q8 = y[i].qs;
  6928. __m256i sumi1 = _mm256_setzero_si256();
  6929. __m256i sumi2 = _mm256_setzero_si256();
  6930. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6931. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6932. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6933. const __m256i q2_1 = _mm256_set_epi32(iq3xxs_grid[q3[7]], iq3xxs_grid[q3[6]], iq3xxs_grid[q3[5]], iq3xxs_grid[q3[4]],
  6934. iq3xxs_grid[q3[3]], iq3xxs_grid[q3[2]], iq3xxs_grid[q3[1]], iq3xxs_grid[q3[0]]);
  6935. q3 += 8;
  6936. const __m256i q2_2 = _mm256_set_epi32(iq3xxs_grid[q3[7]], iq3xxs_grid[q3[6]], iq3xxs_grid[q3[5]], iq3xxs_grid[q3[4]],
  6937. iq3xxs_grid[q3[3]], iq3xxs_grid[q3[2]], iq3xxs_grid[q3[1]], iq3xxs_grid[q3[0]]);
  6938. q3 += 8;
  6939. memcpy(aux32, gas, 8); gas += 8;
  6940. const __m256i s2_1 = _mm256_set_epi64x(signs64[(aux32[0] >> 21) & 127], signs64[(aux32[0] >> 14) & 127],
  6941. signs64[(aux32[0] >> 7) & 127], signs64[(aux32[0] >> 0) & 127]);
  6942. const __m256i s2_2 = _mm256_set_epi64x(signs64[(aux32[1] >> 21) & 127], signs64[(aux32[1] >> 14) & 127],
  6943. signs64[(aux32[1] >> 7) & 127], signs64[(aux32[1] >> 0) & 127]);
  6944. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, s2_1);
  6945. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, s2_2);
  6946. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  6947. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  6948. const uint16_t ls1 = aux32[0] >> 28;
  6949. const uint16_t ls2 = aux32[1] >> 28;
  6950. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
  6951. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
  6952. sumi1 = _mm256_add_epi32(sumi1, p1);
  6953. sumi2 = _mm256_add_epi32(sumi2, p2);
  6954. }
  6955. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  6956. }
  6957. *s = 0.25f * hsum_float_8(accumf);
  6958. #else
  6959. uint32_t aux32;
  6960. float sumf = 0.f;
  6961. for (int i = 0; i < nb; ++i) {
  6962. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6963. const uint8_t * restrict q3 = x[i].qs;
  6964. const uint8_t * restrict gas = x[i].qs + QK_K/4;
  6965. const int8_t * restrict q8 = y[i].qs;
  6966. int32_t bsum = 0;
  6967. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  6968. memcpy(&aux32, gas, sizeof(uint32_t)); gas += sizeof(uint32_t);
  6969. const uint32_t ls = 2*(aux32 >> 28) + 1;
  6970. int32_t sumi = 0;
  6971. for (int l = 0; l < 4; ++l) {
  6972. const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + q3[2*l+0]);
  6973. const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + q3[2*l+1]);
  6974. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*l) & 127];
  6975. for (int j = 0; j < 4; ++j) {
  6976. sumi += grid1[j] * q8[j+0] * (signs & kmask_iq2xs[j+0] ? -1 : 1);
  6977. sumi += grid2[j] * q8[j+4] * (signs & kmask_iq2xs[j+4] ? -1 : 1);
  6978. }
  6979. q8 += 8;
  6980. }
  6981. q3 += 8;
  6982. bsum += sumi * ls;
  6983. }
  6984. sumf += d * bsum;
  6985. }
  6986. *s = 0.25f * sumf;
  6987. #endif
  6988. }
  6989. // ================================ IQ2 quantization =============================================
  6990. typedef struct {
  6991. uint64_t * grid;
  6992. int * map;
  6993. uint16_t * neighbours;
  6994. } iq2_entry_t;
  6995. static iq2_entry_t iq2_data[2] = {
  6996. {NULL, NULL, NULL},
  6997. {NULL, NULL, NULL},
  6998. };
  6999. static inline int iq2_data_index(int grid_size) {
  7000. GGML_ASSERT(grid_size == 256 || grid_size == 512);
  7001. return grid_size == 256 ? 0 : 1;
  7002. }
  7003. static int iq2_compare_func(const void * left, const void * right) {
  7004. const int * l = (const int *)left;
  7005. const int * r = (const int *)right;
  7006. return l[0] < r[0] ? -1 : l[0] > r[0] ? 1 : l[1] < r[1] ? -1 : l[1] > r[1] ? 1 : 0;
  7007. }
  7008. void iq2xs_init_impl(int grid_size) {
  7009. const int gindex = iq2_data_index(grid_size);
  7010. if (iq2_data[gindex].grid) {
  7011. return;
  7012. }
  7013. static const uint16_t kgrid_256[256] = {
  7014. 0, 2, 5, 8, 10, 17, 20, 32, 34, 40, 42, 65, 68, 80, 88, 97,
  7015. 100, 128, 130, 138, 162, 257, 260, 272, 277, 320, 388, 408, 512, 514, 546, 642,
  7016. 1025, 1028, 1040, 1057, 1060, 1088, 1090, 1096, 1120, 1153, 1156, 1168, 1188, 1280, 1282, 1288,
  7017. 1312, 1350, 1385, 1408, 1425, 1545, 1552, 1600, 1668, 1700, 2048, 2053, 2056, 2068, 2088, 2113,
  7018. 2116, 2128, 2130, 2184, 2308, 2368, 2562, 2580, 4097, 4100, 4112, 4129, 4160, 4192, 4228, 4240,
  7019. 4245, 4352, 4360, 4384, 4432, 4442, 4480, 4644, 4677, 5120, 5128, 5152, 5157, 5193, 5248, 5400,
  7020. 5474, 5632, 5654, 6145, 6148, 6160, 6208, 6273, 6400, 6405, 6560, 6737, 8192, 8194, 8202, 8260,
  7021. 8289, 8320, 8322, 8489, 8520, 8704, 8706, 9217, 9220, 9232, 9280, 9302, 9472, 9537, 9572, 9872,
  7022. 10248, 10272, 10388, 10820, 16385, 16388, 16400, 16408, 16417, 16420, 16448, 16456, 16470, 16480, 16513, 16516,
  7023. 16528, 16640, 16672, 16737, 16768, 16773, 16897, 16912, 16968, 16982, 17000, 17408, 17416, 17440, 17536, 17561,
  7024. 17682, 17700, 17920, 18433, 18436, 18448, 18496, 18501, 18688, 18776, 18785, 18818, 19013, 19088, 20480, 20488,
  7025. 20497, 20505, 20512, 20608, 20616, 20740, 20802, 20900, 21137, 21648, 21650, 21770, 22017, 22100, 22528, 22545,
  7026. 22553, 22628, 22848, 23048, 24580, 24592, 24640, 24680, 24832, 24917, 25112, 25184, 25600, 25605, 25872, 25874,
  7027. 25988, 26690, 32768, 32770, 32778, 32833, 32898, 33028, 33048, 33088, 33297, 33793, 33796, 33808, 33813, 33856,
  7028. 33888, 34048, 34118, 34196, 34313, 34368, 34400, 34818, 35076, 35345, 36868, 36880, 36900, 36928, 37025, 37142,
  7029. 37248, 37445, 37888, 37922, 37956, 38225, 39041, 39200, 40962, 41040, 41093, 41225, 41472, 42008, 43088, 43268,
  7030. };
  7031. static const uint16_t kgrid_512[512] = {
  7032. 0, 2, 5, 8, 10, 17, 20, 22, 25, 32, 34, 37, 40, 65, 68, 70,
  7033. 73, 80, 82, 85, 88, 97, 100, 128, 130, 133, 136, 145, 148, 153, 160, 257,
  7034. 260, 262, 265, 272, 274, 277, 280, 282, 289, 292, 320, 322, 325, 328, 337, 340,
  7035. 352, 360, 385, 388, 400, 512, 514, 517, 520, 529, 532, 544, 577, 580, 592, 597,
  7036. 640, 650, 1025, 1028, 1030, 1033, 1040, 1042, 1045, 1048, 1057, 1060, 1088, 1090, 1093, 1096,
  7037. 1105, 1108, 1110, 1120, 1153, 1156, 1168, 1280, 1282, 1285, 1288, 1297, 1300, 1312, 1345, 1348,
  7038. 1360, 1377, 1408, 1537, 1540, 1552, 1574, 1600, 1602, 1668, 2048, 2050, 2053, 2056, 2058, 2065,
  7039. 2068, 2080, 2085, 2113, 2116, 2128, 2136, 2176, 2208, 2218, 2305, 2308, 2320, 2368, 2433, 2441,
  7040. 2560, 2592, 2600, 2710, 2720, 4097, 4100, 4102, 4105, 4112, 4114, 4117, 4120, 4129, 4132, 4160,
  7041. 4162, 4165, 4168, 4177, 4180, 4192, 4202, 4225, 4228, 4240, 4352, 4354, 4357, 4360, 4369, 4372,
  7042. 4384, 4417, 4420, 4432, 4480, 4500, 4502, 4609, 4612, 4614, 4624, 4672, 4704, 5120, 5122, 5125,
  7043. 5128, 5137, 5140, 5152, 5185, 5188, 5193, 5200, 5220, 5248, 5377, 5380, 5392, 5440, 5632, 5652,
  7044. 5705, 6145, 6148, 6160, 6162, 6208, 6228, 6278, 6400, 6405, 6502, 6737, 6825, 8192, 8194, 8197,
  7045. 8200, 8202, 8209, 8212, 8224, 8257, 8260, 8272, 8320, 8352, 8449, 8452, 8464, 8512, 8520, 8549,
  7046. 8704, 8738, 8832, 8872, 9217, 9220, 9232, 9257, 9280, 9472, 9537, 9554, 9625, 9729, 9754, 9894,
  7047. 10240, 10248, 10250, 10272, 10325, 10376, 10402, 10600, 10640, 10760, 10784, 10882, 10888, 10890, 16385, 16388,
  7048. 16390, 16393, 16400, 16402, 16405, 16408, 16417, 16420, 16448, 16450, 16453, 16456, 16458, 16465, 16468, 16480,
  7049. 16485, 16513, 16516, 16528, 16640, 16642, 16645, 16648, 16657, 16660, 16672, 16705, 16708, 16720, 16768, 16773,
  7050. 16802, 16897, 16900, 16912, 16914, 16937, 16960, 17408, 17410, 17413, 17416, 17425, 17428, 17433, 17440, 17473,
  7051. 17476, 17488, 17536, 17556, 17665, 17668, 17680, 17700, 17728, 17818, 17920, 17930, 17988, 18000, 18433, 18436,
  7052. 18448, 18496, 18501, 18516, 18530, 18688, 18705, 18756, 18768, 18793, 18948, 20480, 20482, 20485, 20488, 20497,
  7053. 20500, 20512, 20520, 20545, 20548, 20560, 20608, 20737, 20740, 20752, 20757, 20800, 20802, 20992, 21060, 21162,
  7054. 21505, 21508, 21520, 21537, 21568, 21600, 21633, 21665, 21760, 21768, 21888, 21896, 22049, 22120, 22177, 22528,
  7055. 22548, 22593, 22608, 22681, 22810, 22848, 22850, 23173, 24577, 24580, 24592, 24640, 24660, 24674, 24710, 24745,
  7056. 24832, 25124, 25162, 25234, 25600, 25622, 25872, 25920, 25925, 26020, 26625, 26730, 26917, 27142, 27220, 27234,
  7057. 32768, 32770, 32773, 32776, 32785, 32788, 32800, 32810, 32833, 32836, 32848, 32896, 32898, 32936, 32938, 33025,
  7058. 33028, 33030, 33040, 33088, 33105, 33113, 33280, 33312, 33408, 33410, 33440, 33448, 33793, 33796, 33808, 33810,
  7059. 33813, 33856, 33888, 33929, 34048, 34116, 34213, 34328, 34410, 34816, 34824, 34853, 34906, 34944, 34946, 34984,
  7060. 35078, 35362, 35456, 35464, 35478, 35496, 36865, 36868, 36880, 36928, 36950, 36996, 37120, 37154, 37220, 37462,
  7061. 37513, 37888, 37893, 37956, 37968, 37976, 38185, 38288, 38290, 38465, 38993, 39078, 39241, 39445, 39520, 40960,
  7062. 40962, 40968, 40970, 40992, 41002, 41120, 41297, 41305, 41382, 41472, 41474, 41480, 41514, 41600, 41632, 42048,
  7063. 42133, 42597, 42648, 43018, 43040, 43042, 43048, 43168, 43176, 43268, 43396, 43398, 43560, 43562, 43665, 43690,
  7064. };
  7065. const int kmap_size = 43692;
  7066. const int nwant = 2;
  7067. const uint16_t * kgrid = grid_size == 256 ? kgrid_256 : kgrid_512;
  7068. uint64_t * kgrid_q2xs;
  7069. int * kmap_q2xs;
  7070. uint16_t * kneighbors_q2xs;
  7071. printf("================================================================= %s(grid_size = %d)\n", __func__, grid_size);
  7072. uint64_t * the_grid = (uint64_t *)malloc(grid_size*sizeof(uint64_t));
  7073. for (int k = 0; k < grid_size; ++k) {
  7074. int8_t * pos = (int8_t *)(the_grid + k);
  7075. for (int i = 0; i < 8; ++i) {
  7076. int l = (kgrid[k] >> 2*i) & 0x3;
  7077. pos[i] = 2*l + 1;
  7078. }
  7079. }
  7080. kgrid_q2xs = the_grid;
  7081. iq2_data[gindex].grid = the_grid;
  7082. kmap_q2xs = (int *)malloc(kmap_size*sizeof(int));
  7083. iq2_data[gindex].map = kmap_q2xs;
  7084. for (int i = 0; i < kmap_size; ++i) kmap_q2xs[i] = -1;
  7085. uint64_t aux64;
  7086. uint8_t * aux8 = (uint8_t *)&aux64;
  7087. for (int i = 0; i < grid_size; ++i) {
  7088. aux64 = kgrid_q2xs[i];
  7089. uint16_t index = 0;
  7090. for (int k=0; k<8; ++k) {
  7091. uint16_t q = (aux8[k] - 1)/2;
  7092. index |= (q << 2*k);
  7093. }
  7094. kmap_q2xs[index] = i;
  7095. }
  7096. int8_t pos[8];
  7097. int * dist2 = (int *)malloc(2*grid_size*sizeof(int));
  7098. int num_neighbors = 0, num_not_in_map = 0;
  7099. for (int i = 0; i < kmap_size; ++i) {
  7100. if (kmap_q2xs[i] >= 0) continue;
  7101. ++num_not_in_map;
  7102. for (int k = 0; k < 8; ++k) {
  7103. int l = (i >> 2*k) & 0x3;
  7104. pos[k] = 2*l + 1;
  7105. }
  7106. for (int j = 0; j < grid_size; ++j) {
  7107. const int8_t * pg = (const int8_t *)(kgrid_q2xs + j);
  7108. int d2 = 0;
  7109. for (int k = 0; k < 8; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  7110. dist2[2*j+0] = d2;
  7111. dist2[2*j+1] = j;
  7112. }
  7113. qsort(dist2, grid_size, 2*sizeof(int), iq2_compare_func);
  7114. int n = 0; int d2 = dist2[0];
  7115. int nhave = 1;
  7116. for (int j = 0; j < grid_size; ++j) {
  7117. if (dist2[2*j] > d2) {
  7118. if (nhave == nwant) break;
  7119. d2 = dist2[2*j];
  7120. ++nhave;
  7121. }
  7122. ++n;
  7123. }
  7124. num_neighbors += n;
  7125. }
  7126. printf("%s: %d neighbours in total\n", __func__, num_neighbors);
  7127. kneighbors_q2xs = (uint16_t *)malloc((num_neighbors + num_not_in_map)*sizeof(uint16_t));
  7128. iq2_data[gindex].neighbours = kneighbors_q2xs;
  7129. int counter = 0;
  7130. for (int i = 0; i < kmap_size; ++i) {
  7131. if (kmap_q2xs[i] >= 0) continue;
  7132. for (int k = 0; k < 8; ++k) {
  7133. int l = (i >> 2*k) & 0x3;
  7134. pos[k] = 2*l + 1;
  7135. }
  7136. for (int j = 0; j < grid_size; ++j) {
  7137. const int8_t * pg = (const int8_t *)(kgrid_q2xs + j);
  7138. int d2 = 0;
  7139. for (int k = 0; k < 8; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  7140. dist2[2*j+0] = d2;
  7141. dist2[2*j+1] = j;
  7142. }
  7143. qsort(dist2, grid_size, 2*sizeof(int), iq2_compare_func);
  7144. kmap_q2xs[i] = -(counter + 1);
  7145. int d2 = dist2[0];
  7146. uint16_t * start = &kneighbors_q2xs[counter++];
  7147. int n = 0, nhave = 1;
  7148. for (int j = 0; j < grid_size; ++j) {
  7149. if (dist2[2*j] > d2) {
  7150. if (nhave == nwant) break;
  7151. d2 = dist2[2*j];
  7152. ++nhave;
  7153. }
  7154. kneighbors_q2xs[counter++] = dist2[2*j+1];
  7155. ++n;
  7156. }
  7157. *start = n;
  7158. }
  7159. free(dist2);
  7160. }
  7161. void iq2xs_free_impl(int grid_size) {
  7162. GGML_ASSERT(grid_size == 256 || grid_size == 512 || grid_size == 1024);
  7163. const int gindex = iq2_data_index(grid_size);
  7164. if (iq2_data[gindex].grid) {
  7165. free(iq2_data[gindex].grid); iq2_data[gindex].grid = NULL;
  7166. free(iq2_data[gindex].map); iq2_data[gindex].map = NULL;
  7167. free(iq2_data[gindex].neighbours); iq2_data[gindex].neighbours = NULL;
  7168. }
  7169. }
  7170. static int iq2_find_best_neighbour(const uint16_t * restrict neighbours, const uint64_t * restrict grid,
  7171. const float * restrict xval, const float * restrict weight, float scale, int8_t * restrict L) {
  7172. int num_neighbors = neighbours[0];
  7173. GGML_ASSERT(num_neighbors > 0);
  7174. float best_d2 = FLT_MAX;
  7175. int grid_index = -1;
  7176. for (int j = 1; j <= num_neighbors; ++j) {
  7177. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  7178. float d2 = 0;
  7179. for (int i = 0; i < 8; ++i) {
  7180. float q = pg[i];
  7181. float diff = scale*q - xval[i];
  7182. d2 += weight[i]*diff*diff;
  7183. }
  7184. if (d2 < best_d2) {
  7185. best_d2 = d2; grid_index = neighbours[j];
  7186. }
  7187. }
  7188. GGML_ASSERT(grid_index >= 0);
  7189. const int8_t * pg = (const int8_t *)(grid + grid_index);
  7190. for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
  7191. return grid_index;
  7192. }
  7193. static void quantize_row_iq2_xxs_impl(const float * restrict x, void * restrict vy, int n, const float * restrict quant_weights) {
  7194. const int gindex = iq2_data_index(256);
  7195. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  7196. const int * kmap_q2xs = iq2_data[gindex].map;
  7197. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  7198. GGML_ASSERT(quant_weights && "missing quantization weights");
  7199. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  7200. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  7201. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  7202. GGML_ASSERT(n%QK_K == 0);
  7203. const int kMaxQ = 3;
  7204. const int nbl = n/256;
  7205. block_iq2_xxs * y = vy;
  7206. float scales[QK_K/32];
  7207. float weight[32];
  7208. float xval[32];
  7209. int8_t L[32];
  7210. int8_t Laux[32];
  7211. float waux[32];
  7212. uint8_t block_signs[4];
  7213. uint32_t q2[2*(QK_K/32)];
  7214. for (int ibl = 0; ibl < nbl; ++ibl) {
  7215. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  7216. memset(q2, 0, QK_K/4);
  7217. float max_scale = 0;
  7218. const float * xbl = x + QK_K*ibl;
  7219. float sumx2 = 0;
  7220. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  7221. float sigma2 = sumx2/QK_K;
  7222. for (int ib = 0; ib < QK_K/32; ++ib) {
  7223. const float * xb = xbl + 32*ib;
  7224. const float * qw = quant_weights + QK_K*ibl + 32*ib;
  7225. for (int i = 0; i < 32; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  7226. for (int i = 0; i < 32; ++i) waux[i] = sqrtf(weight[i]);
  7227. for (int k = 0; k < 4; ++k) {
  7228. int nflip = 0;
  7229. uint8_t s = 0;
  7230. for (int i = 0; i < 8; ++i) {
  7231. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  7232. else {
  7233. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  7234. }
  7235. }
  7236. if (nflip%2) {
  7237. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  7238. for (int i = 1; i < 8; ++i) {
  7239. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  7240. if (ax < min) {
  7241. min = ax; imin = i;
  7242. }
  7243. }
  7244. xval[8*k+imin] = -xval[8*k+imin];
  7245. s ^= (1 << imin);
  7246. }
  7247. block_signs[k] = s & 127;
  7248. }
  7249. float max = xval[0];
  7250. for (int i = 1; i < 32; ++i) max = MAX(max, xval[i]);
  7251. if (!max) {
  7252. scales[ib] = 0;
  7253. memset(L, 0, 32);
  7254. continue;
  7255. }
  7256. float scale = make_qp_quants(32, kMaxQ+1, xval, (uint8_t*)L, weight);
  7257. float eff_max = scale*kMaxQ;
  7258. float best = 0;
  7259. for (int is = -6; is <= 6; ++is) {
  7260. float id = (2*kMaxQ-1+is*0.1f)/eff_max;
  7261. float this_scale = 1/id;
  7262. for (int k = 0; k < 4; ++k) {
  7263. for (int i = 0; i < 8; ++i) {
  7264. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  7265. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  7266. }
  7267. uint16_t u = 0;
  7268. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  7269. int grid_index = kmap_q2xs[u];
  7270. if (grid_index < 0) {
  7271. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  7272. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  7273. }
  7274. }
  7275. float sumqx = 0, sumq2 = 0;
  7276. for (int i = 0; i < 32; ++i) {
  7277. float w = weight[i];
  7278. float q = 2*Laux[i] + 1;
  7279. sumqx += w*xval[i]*q;
  7280. sumq2 += w*q*q;
  7281. }
  7282. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  7283. scale = sumqx/sumq2; best = scale*sumqx;
  7284. memcpy(L, Laux, 32);
  7285. }
  7286. }
  7287. if (scale > 0) {
  7288. float id = 1/scale;
  7289. for (int k = 0; k < 4; ++k) {
  7290. uint16_t u = 0;
  7291. for (int i = 0; i < 8; ++i) {
  7292. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  7293. l = MAX(0, MIN(kMaxQ-1, l));
  7294. u |= (l << 2*i);
  7295. }
  7296. int grid_index = kmap_q2xs[u];
  7297. if (grid_index < 0) {
  7298. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  7299. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  7300. }
  7301. const int8_t * pg = (const int8_t *)(kgrid_q2xs + grid_index);
  7302. for (int i = 0; i < 8; ++i) L[8*k+i] = (pg[i] - 1)/2;
  7303. }
  7304. float sumqx = 0, sumq2 = 0;
  7305. for (int i = 0; i < 32; ++i) {
  7306. float w = weight[i];
  7307. float q = 2*L[i] + 1;
  7308. sumqx += w*xval[i]*q;
  7309. sumq2 += w*q*q;
  7310. }
  7311. if (sumq2 > 0) scale = sumqx/sumq2;
  7312. }
  7313. if (scale < 0) {
  7314. // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
  7315. // and correspondingly flip quant signs.
  7316. scale = -scale;
  7317. for (int k = 0; k < 4; ++k) block_signs[k] = (~block_signs[k]) & 127;
  7318. }
  7319. for (int k = 0; k < 4; ++k) {
  7320. uint16_t u = 0;
  7321. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  7322. int grid_index = kmap_q2xs[u];
  7323. if (grid_index < 0) {
  7324. printf("Oops: found point %u not on grid:", u);
  7325. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  7326. printf("\n");
  7327. GGML_ASSERT(false);
  7328. }
  7329. q2[2*ib+0] |= (grid_index << 8*k);
  7330. q2[2*ib+1] |= (block_signs[k] << 7*k);
  7331. }
  7332. GGML_ASSERT(scale >= 0);
  7333. scales[ib] = scale;
  7334. max_scale = MAX(max_scale, scale);
  7335. }
  7336. if (!max_scale) {
  7337. memset(y[ibl].qs, 0, QK_K/4);
  7338. continue;
  7339. }
  7340. float d = max_scale/31;
  7341. y[ibl].d = GGML_FP32_TO_FP16(d);
  7342. float id = 1/d;
  7343. for (int ib = 0; ib < QK_K/32; ++ib) {
  7344. int l = nearest_int(0.5f*(id*scales[ib]-1));
  7345. l = MAX(0, MIN(15, l));
  7346. q2[2*ib+1] |= ((uint32_t)l << 28);
  7347. }
  7348. memcpy(y[ibl].qs, q2, QK_K/4);
  7349. }
  7350. }
  7351. static void quantize_row_iq2_xs_impl(const float * restrict x, void * restrict vy, int n, const float * restrict quant_weights) {
  7352. const int gindex = iq2_data_index(512);
  7353. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  7354. const int * kmap_q2xs = iq2_data[gindex].map;
  7355. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  7356. GGML_ASSERT(quant_weights && "missing quantization weights");
  7357. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  7358. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  7359. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  7360. GGML_ASSERT(n%QK_K == 0);
  7361. const int kMaxQ = 3;
  7362. const int nbl = n/256;
  7363. block_iq2_xs * y = vy;
  7364. float scales[QK_K/16];
  7365. float weight[16];
  7366. float xval[16];
  7367. int8_t L[16];
  7368. int8_t Laux[16];
  7369. float waux[16];
  7370. bool is_on_grid[2];
  7371. bool is_on_grid_aux[2];
  7372. uint8_t block_signs[2];
  7373. uint16_t q2[2*(QK_K/16)];
  7374. for (int ibl = 0; ibl < nbl; ++ibl) {
  7375. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  7376. memset(q2, 0, QK_K/4);
  7377. memset(y[ibl].scales, 0, QK_K/32);
  7378. float max_scale = 0;
  7379. const float * xbl = x + QK_K*ibl;
  7380. float sumx2 = 0;
  7381. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  7382. float sigma2 = sumx2/QK_K;
  7383. for (int ib = 0; ib < QK_K/16; ++ib) {
  7384. const float * xb = xbl + 16*ib;
  7385. const float * qw = quant_weights + QK_K*ibl + 16*ib;
  7386. for (int i = 0; i < 16; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  7387. for (int i = 0; i < 16; ++i) waux[i] = sqrtf(weight[i]);
  7388. for (int k = 0; k < 2; ++k) {
  7389. int nflip = 0;
  7390. uint8_t s = 0;
  7391. for (int i = 0; i < 8; ++i) {
  7392. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  7393. else {
  7394. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  7395. }
  7396. }
  7397. if (nflip%2) {
  7398. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  7399. for (int i = 1; i < 8; ++i) {
  7400. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  7401. if (ax < min) {
  7402. min = ax; imin = i;
  7403. }
  7404. }
  7405. xval[8*k+imin] = -xval[8*k+imin];
  7406. s ^= (1 << imin);
  7407. }
  7408. block_signs[k] = s & 127;
  7409. }
  7410. float max = xval[0];
  7411. for (int i = 1; i < 16; ++i) max = MAX(max, xval[i]);
  7412. if (!max) {
  7413. scales[ib] = 0;
  7414. memset(L, 0, 16);
  7415. continue;
  7416. }
  7417. float best = 0;
  7418. float scale = max/(2*kMaxQ-1);
  7419. is_on_grid[0] = is_on_grid[1] = true;
  7420. for (int is = -9; is <= 9; ++is) {
  7421. float id = (2*kMaxQ-1+is*0.1f)/max;
  7422. float this_scale = 1/id;
  7423. for (int k = 0; k < 2; ++k) {
  7424. for (int i = 0; i < 8; ++i) {
  7425. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  7426. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  7427. }
  7428. uint16_t u = 0;
  7429. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  7430. int grid_index = kmap_q2xs[u];
  7431. is_on_grid_aux[k] = true;
  7432. if (grid_index < 0) {
  7433. is_on_grid_aux[k] = false;
  7434. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  7435. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  7436. }
  7437. }
  7438. float sumqx = 0, sumq2 = 0;
  7439. for (int i = 0; i < 16; ++i) {
  7440. float w = weight[i];
  7441. float q = 2*Laux[i] + 1;
  7442. sumqx += w*xval[i]*q;
  7443. sumq2 += w*q*q;
  7444. }
  7445. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  7446. scale = sumqx/sumq2; best = scale*sumqx;
  7447. for (int i = 0; i < 16; ++i) L[i] = Laux[i];
  7448. for (int k = 0; k < 2; ++k) is_on_grid[k] = is_on_grid_aux[k];
  7449. }
  7450. }
  7451. int n_not_ongrid = 0;
  7452. for (int k = 0; k < 2; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  7453. if (n_not_ongrid > 0 && scale > 0) {
  7454. float id = 1/scale;
  7455. for (int k = 0; k < 2; ++k) {
  7456. if (is_on_grid[k]) continue;
  7457. uint16_t u = 0;
  7458. for (int i = 0; i < 8; ++i) {
  7459. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  7460. l = MAX(0, MIN(kMaxQ-1, l));
  7461. u |= (l << 2*i);
  7462. L[8*k + i] = l;
  7463. }
  7464. int grid_index = kmap_q2xs[u];
  7465. if (grid_index < 0) {
  7466. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  7467. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  7468. }
  7469. }
  7470. float sumqx = 0, sumq2 = 0;
  7471. for (int i = 0; i < 16; ++i) {
  7472. float w = weight[i];
  7473. float q = 2*L[i] + 1;
  7474. sumqx += w*xval[i]*q;
  7475. sumq2 += w*q*q;
  7476. }
  7477. if (sumq2 > 0) scale = sumqx/sumq2;
  7478. }
  7479. if (scale < 0) {
  7480. scale = -scale;
  7481. for (int k = 0; k < 2; ++k) block_signs[k] = (~block_signs[k]) & 127;
  7482. }
  7483. for (int k = 0; k < 2; ++k) {
  7484. uint16_t u = 0;
  7485. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  7486. int grid_index = kmap_q2xs[u];
  7487. if (grid_index < 0) {
  7488. printf("Oops: found point %u not on grid:", u);
  7489. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  7490. printf("\n");
  7491. GGML_ASSERT(false);
  7492. }
  7493. q2[2*ib+k] = grid_index | (block_signs[k] << 9);
  7494. }
  7495. GGML_ASSERT(scale >= 0);
  7496. scales[ib] = scale;
  7497. max_scale = MAX(max_scale, scale);
  7498. }
  7499. if (!max_scale) {
  7500. memset(y[ibl].qs, 0, QK_K/4);
  7501. continue;
  7502. }
  7503. float d = max_scale/31;
  7504. y[ibl].d = GGML_FP32_TO_FP16(d);
  7505. float id = 1/d;
  7506. for (int ib = 0; ib < QK_K/16; ++ib) {
  7507. int l = nearest_int(0.5f*(id*scales[ib]-1));
  7508. l = MAX(0, MIN(15, l));
  7509. if (ib%2 == 0) y[ibl].scales[ib/2] = l;
  7510. else y[ibl].scales[ib/2] |= (l << 4);
  7511. }
  7512. memcpy(y[ibl].qs, q2, QK_K/4);
  7513. }
  7514. }
  7515. size_t quantize_iq2_xxs(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  7516. (void)hist;
  7517. GGML_ASSERT(n_per_row%QK_K == 0);
  7518. int nblock = n_per_row/QK_K;
  7519. char * qrow = (char *)dst;
  7520. for (int row = 0; row < nrow; ++row) {
  7521. quantize_row_iq2_xxs_impl(src, qrow, n_per_row, quant_weights);
  7522. src += n_per_row;
  7523. qrow += nblock*sizeof(block_iq2_xxs);
  7524. }
  7525. return nrow * nblock * sizeof(block_iq2_xxs);
  7526. }
  7527. size_t quantize_iq2_xs(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  7528. (void)hist;
  7529. GGML_ASSERT(n_per_row%QK_K == 0);
  7530. int nblock = n_per_row/QK_K;
  7531. char * qrow = (char *)dst;
  7532. for (int row = 0; row < nrow; ++row) {
  7533. quantize_row_iq2_xs_impl(src, qrow, n_per_row, quant_weights);
  7534. src += n_per_row;
  7535. qrow += nblock*sizeof(block_iq2_xs);
  7536. }
  7537. return nrow * nblock * sizeof(block_iq2_xs);
  7538. }
  7539. //
  7540. // ============================================= 3-bit using D4 lattice
  7541. //
  7542. typedef struct {
  7543. uint32_t * grid;
  7544. int * map;
  7545. uint16_t * neighbours;
  7546. } iq3_entry_t;
  7547. static iq3_entry_t iq3_data[1] = {
  7548. {NULL, NULL, NULL},
  7549. };
  7550. static inline int iq3_data_index(int grid_size) {
  7551. (void)grid_size;
  7552. GGML_ASSERT(grid_size == 256);
  7553. return 0;
  7554. }
  7555. static int iq3_compare_func(const void * left, const void * right) {
  7556. const int * l = (const int *)left;
  7557. const int * r = (const int *)right;
  7558. return l[0] < r[0] ? -1 : l[0] > r[0] ? 1 : l[1] < r[1] ? -1 : l[1] > r[1] ? 1 : 0;
  7559. }
  7560. void iq3xs_init_impl(int grid_size) {
  7561. const int gindex = iq3_data_index(grid_size);
  7562. if (iq3_data[gindex].grid) {
  7563. return;
  7564. }
  7565. static const uint16_t kgrid_256[256] = {
  7566. 0, 2, 4, 9, 11, 15, 16, 18, 25, 34, 59, 61, 65, 67, 72, 74,
  7567. 81, 85, 88, 90, 97, 108, 120, 128, 130, 132, 137, 144, 146, 153, 155, 159,
  7568. 169, 175, 189, 193, 199, 200, 202, 213, 248, 267, 287, 292, 303, 315, 317, 321,
  7569. 327, 346, 362, 413, 436, 456, 460, 462, 483, 497, 513, 515, 520, 522, 529, 531,
  7570. 536, 538, 540, 551, 552, 576, 578, 585, 592, 594, 641, 643, 648, 650, 657, 664,
  7571. 698, 704, 706, 720, 729, 742, 758, 769, 773, 808, 848, 852, 870, 889, 901, 978,
  7572. 992, 1024, 1026, 1033, 1035, 1040, 1042, 1046, 1049, 1058, 1089, 1091, 1093, 1096, 1098, 1105,
  7573. 1112, 1139, 1143, 1144, 1152, 1154, 1161, 1167, 1168, 1170, 1183, 1184, 1197, 1217, 1224, 1228,
  7574. 1272, 1276, 1309, 1323, 1347, 1367, 1377, 1404, 1473, 1475, 1486, 1509, 1537, 1544, 1546, 1553,
  7575. 1555, 1576, 1589, 1594, 1600, 1602, 1616, 1625, 1636, 1638, 1665, 1667, 1672, 1685, 1706, 1722,
  7576. 1737, 1755, 1816, 1831, 1850, 1856, 1862, 1874, 1901, 1932, 1950, 1971, 2011, 2032, 2052, 2063,
  7577. 2077, 2079, 2091, 2095, 2172, 2192, 2207, 2208, 2224, 2230, 2247, 2277, 2308, 2345, 2356, 2389,
  7578. 2403, 2424, 2501, 2504, 2506, 2520, 2570, 2593, 2616, 2624, 2630, 2646, 2669, 2700, 2714, 2746,
  7579. 2754, 2795, 2824, 2835, 2839, 2874, 2882, 2905, 2984, 3028, 3042, 3092, 3108, 3110, 3124, 3153,
  7580. 3185, 3215, 3252, 3288, 3294, 3364, 3397, 3434, 3483, 3523, 3537, 3587, 3589, 3591, 3592, 3610,
  7581. 3626, 3670, 3680, 3722, 3749, 3754, 3776, 3789, 3803, 3824, 3857, 3873, 3904, 3906, 3924, 3992,
  7582. };
  7583. const int kmap_size = 4096;
  7584. const int nwant = 2;
  7585. const uint16_t * kgrid = kgrid_256;
  7586. uint32_t * kgrid_q3xs;
  7587. int * kmap_q3xs;
  7588. uint16_t * kneighbors_q3xs;
  7589. printf("================================================================= %s(grid_size = %d)\n", __func__, grid_size);
  7590. uint32_t * the_grid = (uint32_t *)malloc(grid_size*sizeof(uint32_t));
  7591. for (int k = 0; k < grid_size; ++k) {
  7592. int8_t * pos = (int8_t *)(the_grid + k);
  7593. for (int i = 0; i < 4; ++i) {
  7594. int l = (kgrid[k] >> 3*i) & 0x7;
  7595. pos[i] = 2*l + 1;
  7596. }
  7597. }
  7598. kgrid_q3xs = the_grid;
  7599. iq3_data[gindex].grid = the_grid;
  7600. kmap_q3xs = (int *)malloc(kmap_size*sizeof(int));
  7601. iq3_data[gindex].map = kmap_q3xs;
  7602. for (int i = 0; i < kmap_size; ++i) kmap_q3xs[i] = -1;
  7603. uint32_t aux32;
  7604. uint8_t * aux8 = (uint8_t *)&aux32;
  7605. for (int i = 0; i < grid_size; ++i) {
  7606. aux32 = kgrid_q3xs[i];
  7607. uint16_t index = 0;
  7608. for (int k=0; k<4; ++k) {
  7609. uint16_t q = (aux8[k] - 1)/2;
  7610. index |= (q << 3*k);
  7611. }
  7612. kmap_q3xs[index] = i;
  7613. }
  7614. int8_t pos[4];
  7615. int * dist2 = (int *)malloc(2*grid_size*sizeof(int));
  7616. int num_neighbors = 0, num_not_in_map = 0;
  7617. for (int i = 0; i < kmap_size; ++i) {
  7618. if (kmap_q3xs[i] >= 0) continue;
  7619. ++num_not_in_map;
  7620. for (int k = 0; k < 4; ++k) {
  7621. int l = (i >> 3*k) & 0x7;
  7622. pos[k] = 2*l + 1;
  7623. }
  7624. for (int j = 0; j < grid_size; ++j) {
  7625. const int8_t * pg = (const int8_t *)(kgrid_q3xs + j);
  7626. int d2 = 0;
  7627. for (int k = 0; k < 4; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  7628. dist2[2*j+0] = d2;
  7629. dist2[2*j+1] = j;
  7630. }
  7631. qsort(dist2, grid_size, 2*sizeof(int), iq3_compare_func);
  7632. int n = 0; int d2 = dist2[0];
  7633. int nhave = 1;
  7634. for (int j = 0; j < grid_size; ++j) {
  7635. if (dist2[2*j] > d2) {
  7636. if (nhave == nwant) break;
  7637. d2 = dist2[2*j];
  7638. ++nhave;
  7639. }
  7640. ++n;
  7641. }
  7642. num_neighbors += n;
  7643. }
  7644. printf("%s: %d neighbours in total\n", __func__, num_neighbors);
  7645. kneighbors_q3xs = (uint16_t *)malloc((num_neighbors + num_not_in_map)*sizeof(uint16_t));
  7646. iq3_data[gindex].neighbours = kneighbors_q3xs;
  7647. int counter = 0;
  7648. for (int i = 0; i < kmap_size; ++i) {
  7649. if (kmap_q3xs[i] >= 0) continue;
  7650. for (int k = 0; k < 4; ++k) {
  7651. int l = (i >> 3*k) & 0x7;
  7652. pos[k] = 2*l + 1;
  7653. }
  7654. for (int j = 0; j < grid_size; ++j) {
  7655. const int8_t * pg = (const int8_t *)(kgrid_q3xs + j);
  7656. int d2 = 0;
  7657. for (int k = 0; k < 4; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  7658. dist2[2*j+0] = d2;
  7659. dist2[2*j+1] = j;
  7660. }
  7661. qsort(dist2, grid_size, 2*sizeof(int), iq3_compare_func);
  7662. kmap_q3xs[i] = -(counter + 1);
  7663. int d2 = dist2[0];
  7664. uint16_t * start = &kneighbors_q3xs[counter++];
  7665. int n = 0, nhave = 1;
  7666. for (int j = 0; j < grid_size; ++j) {
  7667. if (dist2[2*j] > d2) {
  7668. if (nhave == nwant) break;
  7669. d2 = dist2[2*j];
  7670. ++nhave;
  7671. }
  7672. kneighbors_q3xs[counter++] = dist2[2*j+1];
  7673. ++n;
  7674. }
  7675. *start = n;
  7676. }
  7677. free(dist2);
  7678. }
  7679. void iq3xs_free_impl(int grid_size) {
  7680. GGML_ASSERT(grid_size == 256);
  7681. const int gindex = iq3_data_index(grid_size);
  7682. if (iq3_data[gindex].grid) {
  7683. free(iq3_data[gindex].grid); iq3_data[gindex].grid = NULL;
  7684. free(iq3_data[gindex].map); iq3_data[gindex].map = NULL;
  7685. free(iq3_data[gindex].neighbours); iq3_data[gindex].neighbours = NULL;
  7686. }
  7687. }
  7688. static int iq3_find_best_neighbour(const uint16_t * restrict neighbours, const uint32_t * restrict grid,
  7689. const float * restrict xval, const float * restrict weight, float scale, int8_t * restrict L) {
  7690. int num_neighbors = neighbours[0];
  7691. GGML_ASSERT(num_neighbors > 0);
  7692. float best_d2 = FLT_MAX;
  7693. int grid_index = -1;
  7694. for (int j = 1; j <= num_neighbors; ++j) {
  7695. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  7696. float d2 = 0;
  7697. for (int i = 0; i < 4; ++i) {
  7698. float q = pg[i];
  7699. float diff = scale*q - xval[i];
  7700. d2 += weight[i]*diff*diff;
  7701. }
  7702. if (d2 < best_d2) {
  7703. best_d2 = d2; grid_index = neighbours[j];
  7704. }
  7705. }
  7706. GGML_ASSERT(grid_index >= 0);
  7707. const int8_t * pg = (const int8_t *)(grid + grid_index);
  7708. for (int i = 0; i < 4; ++i) L[i] = (pg[i] - 1)/2;
  7709. return grid_index;
  7710. }
  7711. static void quantize_row_iq3_xxs_impl(const float * restrict x, void * restrict vy, int n, const float * restrict quant_weights) {
  7712. const int gindex = iq3_data_index(256);
  7713. const uint32_t * kgrid_q3xs = iq3_data[gindex].grid;
  7714. const int * kmap_q3xs = iq3_data[gindex].map;
  7715. const uint16_t * kneighbors_q3xs = iq3_data[gindex].neighbours;
  7716. //GGML_ASSERT(quant_weights && "missing quantization weights");
  7717. GGML_ASSERT(kgrid_q3xs && "forgot to call ggml_quantize_init()?");
  7718. GGML_ASSERT(kmap_q3xs && "forgot to call ggml_quantize_init()?");
  7719. GGML_ASSERT(kneighbors_q3xs && "forgot to call ggml_quantize_init()?");
  7720. GGML_ASSERT(n%QK_K == 0);
  7721. const int kMaxQ = 8;
  7722. const int nbl = n/256;
  7723. block_iq3_xxs * y = vy;
  7724. float scales[QK_K/32];
  7725. float weight[32];
  7726. float xval[32];
  7727. int8_t L[32];
  7728. int8_t Laux[32];
  7729. float waux[32];
  7730. bool is_on_grid[8];
  7731. bool is_on_grid_aux[8];
  7732. uint8_t block_signs[8];
  7733. uint8_t q3[3*(QK_K/8)];
  7734. uint32_t * scales_and_signs = (uint32_t *)(q3 + QK_K/4);
  7735. for (int ibl = 0; ibl < nbl; ++ibl) {
  7736. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  7737. memset(q3, 0, 3*QK_K/8);
  7738. float max_scale = 0;
  7739. const float * xbl = x + QK_K*ibl;
  7740. float sumx2 = 0;
  7741. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  7742. float sigma2 = sumx2/QK_K;
  7743. for (int ib = 0; ib < QK_K/32; ++ib) {
  7744. const float * xb = xbl + 32*ib;
  7745. if (quant_weights) {
  7746. const float * qw = quant_weights + QK_K*ibl + 32*ib;
  7747. for (int i = 0; i < 32; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  7748. } else {
  7749. for (int i = 0; i < 32; ++i) weight[i] = xb[i]*xb[i];
  7750. }
  7751. for (int i = 0; i < 32; ++i) waux[i] = sqrtf(weight[i]);
  7752. for (int k = 0; k < 4; ++k) {
  7753. int nflip = 0;
  7754. uint8_t s = 0;
  7755. for (int i = 0; i < 8; ++i) {
  7756. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  7757. else {
  7758. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  7759. }
  7760. }
  7761. if (nflip%2) {
  7762. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  7763. for (int i = 1; i < 8; ++i) {
  7764. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  7765. if (ax < min) {
  7766. min = ax; imin = i;
  7767. }
  7768. }
  7769. xval[8*k+imin] = -xval[8*k+imin];
  7770. s ^= (1 << imin);
  7771. }
  7772. block_signs[k] = s & 127;
  7773. }
  7774. float max = xval[0];
  7775. for (int i = 1; i < 32; ++i) max = MAX(max, xval[i]);
  7776. if (!max) {
  7777. scales[ib] = 0;
  7778. memset(L, 0, 32);
  7779. continue;
  7780. }
  7781. float best = 0;
  7782. float scale = max/(2*kMaxQ-1);
  7783. for (int is = -15; is <= 15; ++is) {
  7784. float id = (2*kMaxQ-1+is*0.2f)/max;
  7785. float this_scale = 1/id;
  7786. for (int k = 0; k < 8; ++k) {
  7787. for (int i = 0; i < 4; ++i) {
  7788. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  7789. Laux[4*k+i] = MAX(0, MIN(kMaxQ-1, l));
  7790. }
  7791. uint16_t u = 0;
  7792. for (int i = 0; i < 4; ++i) u |= (Laux[4*k+i] << 3*i);
  7793. int grid_index = kmap_q3xs[u];
  7794. is_on_grid_aux[k] = true;
  7795. if (grid_index < 0) {
  7796. is_on_grid_aux[k] = false;
  7797. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  7798. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, this_scale, Laux + 4*k);
  7799. }
  7800. }
  7801. float sumqx = 0, sumq2 = 0;
  7802. for (int i = 0; i < 32; ++i) {
  7803. float w = weight[i];
  7804. float q = 2*Laux[i] + 1;
  7805. sumqx += w*xval[i]*q;
  7806. sumq2 += w*q*q;
  7807. }
  7808. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  7809. scale = sumqx/sumq2; best = scale*sumqx;
  7810. for (int i = 0; i < 32; ++i) L[i] = Laux[i];
  7811. for (int k = 0; k < 8; ++k) is_on_grid[k] = is_on_grid_aux[k];
  7812. }
  7813. }
  7814. int n_not_ongrid = 0;
  7815. for (int k = 0; k < 8; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  7816. if (n_not_ongrid > 0 && scale > 0) {
  7817. float id = 1/scale;
  7818. for (int k = 0; k < 8; ++k) {
  7819. if (is_on_grid[k]) continue;
  7820. uint16_t u = 0;
  7821. for (int i = 0; i < 4; ++i) {
  7822. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  7823. l = MAX(0, MIN(kMaxQ-1, l));
  7824. u |= (l << 3*i);
  7825. }
  7826. int grid_index = kmap_q3xs[u];
  7827. if (grid_index < 0) {
  7828. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  7829. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, scale, L + 4*k);
  7830. }
  7831. const int8_t * pg = (const int8_t *)(kgrid_q3xs + grid_index);
  7832. for (int i = 0; i < 4; ++i) L[4*k+i] = (pg[i] - 1)/2;
  7833. }
  7834. float sumqx = 0, sumq2 = 0;
  7835. for (int i = 0; i < 32; ++i) {
  7836. float w = weight[i];
  7837. float q = 2*L[i] + 1;
  7838. sumqx += w*xval[i]*q;
  7839. sumq2 += w*q*q;
  7840. }
  7841. if (sumq2 > 0) scale = sumqx/sumq2;
  7842. }
  7843. if (scale < 0) {
  7844. // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
  7845. // and correspondingly flip quant signs.
  7846. scale = -scale;
  7847. for (int k = 0; k < 4; ++k) block_signs[k] = (~block_signs[k]) & 127;
  7848. }
  7849. for (int k = 0; k < 8; ++k) {
  7850. uint16_t u = 0;
  7851. for (int i = 0; i < 4; ++i) u |= (L[4*k+i] << 3*i);
  7852. int grid_index = kmap_q3xs[u];
  7853. if (grid_index < 0) {
  7854. printf("Oops: found point %u not on grid:", u);
  7855. for (int i = 0; i < 4; ++i) printf(" %d", L[4*k+i]);
  7856. printf("\n");
  7857. GGML_ASSERT(false);
  7858. }
  7859. q3[8*ib+k] = grid_index;
  7860. }
  7861. scales_and_signs[ib] = block_signs[0] | (block_signs[1] << 7) | (block_signs[2] << 14) | (block_signs[3] << 21);
  7862. GGML_ASSERT(scale >= 0);
  7863. scales[ib] = scale;
  7864. max_scale = MAX(max_scale, scale);
  7865. }
  7866. if (!max_scale) {
  7867. memset(y[ibl].qs, 0, 3*QK_K/8);
  7868. continue;
  7869. }
  7870. float d = max_scale/31;
  7871. y[ibl].d = GGML_FP32_TO_FP16(d);
  7872. float id = 1/d;
  7873. float sumqx = 0, sumq2 = 0;
  7874. for (int ib = 0; ib < QK_K/32; ++ib) {
  7875. int l = nearest_int(0.5f*(id*scales[ib]-1));
  7876. l = MAX(0, MIN(15, l));
  7877. scales_and_signs[ib] |= ((uint32_t)l << 28);
  7878. if (false) {
  7879. const float * xb = xbl + 32*ib;
  7880. if (quant_weights) {
  7881. const float * qw = quant_weights + QK_K*ibl + 32*ib;
  7882. for (int i = 0; i < 32; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  7883. } else {
  7884. for (int i = 0; i < 32; ++i) weight[i] = xb[i]*xb[i];
  7885. }
  7886. const float db = 0.25f * d * (1 + 2*l);
  7887. for (int k = 0; k < 8; ++k) {
  7888. const int8_t * signs = keven_signs_q2xs + 8*((scales_and_signs[ib] >> 7*(k/2)) & 127) + 4*(k%2);
  7889. const float * xk = xb + 4*k;
  7890. const float * wk = weight + 4*k;
  7891. //const uint8_t * grid = (const uint8_t *)(kgrid_q3xs + q3[8*ib+k]);
  7892. const uint8_t * grid = (const uint8_t *)(iq3xxs_grid + q3[8*ib+k]);
  7893. float best_mse = 0; int best_index = q3[8*ib+k];
  7894. for (int j = 0; j < 4; ++j) {
  7895. float diff = db * grid[j] * signs[j] - xk[j];
  7896. best_mse += wk[j] * diff * diff;
  7897. }
  7898. for (int idx = 0; idx < 256; ++idx) {
  7899. //grid = (const uint8_t *)(kgrid_q3xs + idx);
  7900. grid = (const uint8_t *)(iq3xxs_grid + idx);
  7901. float mse = 0;
  7902. for (int j = 0; j < 4; ++j) {
  7903. float diff = db * grid[j] * signs[j] - xk[j];
  7904. mse += wk[j] * diff * diff;
  7905. }
  7906. if (mse < best_mse) {
  7907. best_mse = mse; best_index = idx;
  7908. }
  7909. }
  7910. q3[8*ib+k] = best_index;
  7911. //grid = (const uint8_t *)(kgrid_q3xs + best_index);
  7912. grid = (const uint8_t *)(iq3xxs_grid + best_index);
  7913. for (int j = 0; j < 4; ++j) {
  7914. float q = db * grid[j] * signs[j];
  7915. sumqx += wk[j] * q * xk[j];
  7916. sumq2 += wk[j] * q * q;
  7917. }
  7918. }
  7919. if (sumq2 > 0) y[ibl].d = GGML_FP32_TO_FP16(d*sumqx/sumq2);
  7920. }
  7921. }
  7922. memcpy(y[ibl].qs, q3, 3*QK_K/8);
  7923. }
  7924. }
  7925. size_t quantize_iq3_xxs(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  7926. (void)hist;
  7927. GGML_ASSERT(n_per_row%QK_K == 0);
  7928. int nblock = n_per_row/QK_K;
  7929. char * qrow = (char *)dst;
  7930. for (int row = 0; row < nrow; ++row) {
  7931. quantize_row_iq3_xxs_impl(src, qrow, n_per_row, quant_weights);
  7932. src += n_per_row;
  7933. qrow += nblock*sizeof(block_iq3_xxs);
  7934. }
  7935. return nrow * nblock * sizeof(block_iq3_xxs);
  7936. }
  7937. void quantize_row_iq3_xxs(const float * restrict x, void * restrict vy, int k) {
  7938. assert(k % QK_K == 0);
  7939. block_iq3_xxs * restrict y = vy;
  7940. quantize_row_iq3_xxs_reference(x, y, k);
  7941. }
  7942. void quantize_row_iq3_xxs_reference(const float * restrict x, block_iq3_xxs * restrict y, int k) {
  7943. assert(k % QK_K == 0);
  7944. quantize_row_iq3_xxs_impl(x, y, k, NULL);
  7945. }